C++: fix fix-it hints for misspellings within explicit namespaces
[official-gcc.git] / gcc / cp / name-lookup.c
blob4004640a60cbb3596f117d2edc2996848564d981
1 /* Definitions for C++ name lookup routines.
2 Copyright (C) 2003-2017 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 void consider_binding_level (tree name, best_match <tree, tree> &bm,
52 cp_binding_level *lvl,
53 bool look_within_fields,
54 enum lookup_name_fuzzy_kind kind);
55 static tree lookup_type_current_level (tree);
56 static tree push_using_directive (tree);
57 static tree lookup_extern_c_fun_in_all_ns (tree);
58 static void diagnose_name_conflict (tree, tree);
60 /* The :: namespace. */
62 tree global_namespace;
64 /* The name of the anonymous namespace, throughout this translation
65 unit. */
66 static GTY(()) tree anonymous_namespace_name;
68 /* Initialize anonymous_namespace_name if necessary, and return it. */
70 static tree
71 get_anonymous_namespace_name (void)
73 if (!anonymous_namespace_name)
75 /* We used to use get_file_function_name here, but that isn't
76 necessary now that anonymous namespace typeinfos
77 are !TREE_PUBLIC, and thus compared by address. */
78 /* The demangler expects anonymous namespaces to be called
79 something starting with '_GLOBAL__N_'. */
80 anonymous_namespace_name = get_identifier ("_GLOBAL__N_1");
82 return anonymous_namespace_name;
85 /* Compute the chain index of a binding_entry given the HASH value of its
86 name and the total COUNT of chains. COUNT is assumed to be a power
87 of 2. */
89 #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
91 /* A free list of "binding_entry"s awaiting for re-use. */
93 static GTY((deletable)) binding_entry free_binding_entry = NULL;
95 /* Create a binding_entry object for (NAME, TYPE). */
97 static inline binding_entry
98 binding_entry_make (tree name, tree type)
100 binding_entry entry;
102 if (free_binding_entry)
104 entry = free_binding_entry;
105 free_binding_entry = entry->chain;
107 else
108 entry = ggc_alloc<binding_entry_s> ();
110 entry->name = name;
111 entry->type = type;
112 entry->chain = NULL;
114 return entry;
117 /* Put ENTRY back on the free list. */
118 #if 0
119 static inline void
120 binding_entry_free (binding_entry entry)
122 entry->name = NULL;
123 entry->type = NULL;
124 entry->chain = free_binding_entry;
125 free_binding_entry = entry;
127 #endif
129 /* The datatype used to implement the mapping from names to types at
130 a given scope. */
131 struct GTY(()) binding_table_s {
132 /* Array of chains of "binding_entry"s */
133 binding_entry * GTY((length ("%h.chain_count"))) chain;
135 /* The number of chains in this table. This is the length of the
136 member "chain" considered as an array. */
137 size_t chain_count;
139 /* Number of "binding_entry"s in this table. */
140 size_t entry_count;
143 /* Construct TABLE with an initial CHAIN_COUNT. */
145 static inline void
146 binding_table_construct (binding_table table, size_t chain_count)
148 table->chain_count = chain_count;
149 table->entry_count = 0;
150 table->chain = ggc_cleared_vec_alloc<binding_entry> (table->chain_count);
153 /* Make TABLE's entries ready for reuse. */
154 #if 0
155 static void
156 binding_table_free (binding_table table)
158 size_t i;
159 size_t count;
161 if (table == NULL)
162 return;
164 for (i = 0, count = table->chain_count; i < count; ++i)
166 binding_entry temp = table->chain[i];
167 while (temp != NULL)
169 binding_entry entry = temp;
170 temp = entry->chain;
171 binding_entry_free (entry);
173 table->chain[i] = NULL;
175 table->entry_count = 0;
177 #endif
179 /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */
181 static inline binding_table
182 binding_table_new (size_t chain_count)
184 binding_table table = ggc_alloc<binding_table_s> ();
185 table->chain = NULL;
186 binding_table_construct (table, chain_count);
187 return table;
190 /* Expand TABLE to twice its current chain_count. */
192 static void
193 binding_table_expand (binding_table table)
195 const size_t old_chain_count = table->chain_count;
196 const size_t old_entry_count = table->entry_count;
197 const size_t new_chain_count = 2 * old_chain_count;
198 binding_entry *old_chains = table->chain;
199 size_t i;
201 binding_table_construct (table, new_chain_count);
202 for (i = 0; i < old_chain_count; ++i)
204 binding_entry entry = old_chains[i];
205 for (; entry != NULL; entry = old_chains[i])
207 const unsigned int hash = IDENTIFIER_HASH_VALUE (entry->name);
208 const size_t j = ENTRY_INDEX (hash, new_chain_count);
210 old_chains[i] = entry->chain;
211 entry->chain = table->chain[j];
212 table->chain[j] = entry;
215 table->entry_count = old_entry_count;
218 /* Insert a binding for NAME to TYPE into TABLE. */
220 static void
221 binding_table_insert (binding_table table, tree name, tree type)
223 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
224 const size_t i = ENTRY_INDEX (hash, table->chain_count);
225 binding_entry entry = binding_entry_make (name, type);
227 entry->chain = table->chain[i];
228 table->chain[i] = entry;
229 ++table->entry_count;
231 if (3 * table->chain_count < 5 * table->entry_count)
232 binding_table_expand (table);
235 /* Return the binding_entry, if any, that maps NAME. */
237 binding_entry
238 binding_table_find (binding_table table, tree name)
240 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
241 binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)];
243 while (entry != NULL && entry->name != name)
244 entry = entry->chain;
246 return entry;
249 /* Apply PROC -- with DATA -- to all entries in TABLE. */
251 void
252 binding_table_foreach (binding_table table, bt_foreach_proc proc, void *data)
254 size_t chain_count;
255 size_t i;
257 if (!table)
258 return;
260 chain_count = table->chain_count;
261 for (i = 0; i < chain_count; ++i)
263 binding_entry entry = table->chain[i];
264 for (; entry != NULL; entry = entry->chain)
265 proc (entry, data);
269 #ifndef ENABLE_SCOPE_CHECKING
270 # define ENABLE_SCOPE_CHECKING 0
271 #else
272 # define ENABLE_SCOPE_CHECKING 1
273 #endif
275 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
277 static GTY((deletable)) cxx_binding *free_bindings;
279 /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
280 field to NULL. */
282 static inline void
283 cxx_binding_init (cxx_binding *binding, tree value, tree type)
285 binding->value = value;
286 binding->type = type;
287 binding->previous = NULL;
290 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
292 static cxx_binding *
293 cxx_binding_make (tree value, tree type)
295 cxx_binding *binding;
296 if (free_bindings)
298 binding = free_bindings;
299 free_bindings = binding->previous;
301 else
302 binding = ggc_alloc<cxx_binding> ();
304 cxx_binding_init (binding, value, type);
306 return binding;
309 /* Put BINDING back on the free list. */
311 static inline void
312 cxx_binding_free (cxx_binding *binding)
314 binding->scope = NULL;
315 binding->previous = free_bindings;
316 free_bindings = binding;
319 /* Create a new binding for NAME (with the indicated VALUE and TYPE
320 bindings) in the class scope indicated by SCOPE. */
322 static cxx_binding *
323 new_class_binding (tree name, tree value, tree type, cp_binding_level *scope)
325 cp_class_binding cb = {cxx_binding_make (value, type), name};
326 cxx_binding *binding = cb.base;
327 vec_safe_push (scope->class_shadowed, cb);
328 binding->scope = scope;
329 return binding;
332 /* Make DECL the innermost binding for ID. The LEVEL is the binding
333 level at which this declaration is being bound. */
335 void
336 push_binding (tree id, tree decl, cp_binding_level* level)
338 cxx_binding *binding;
340 if (level != class_binding_level)
342 binding = cxx_binding_make (decl, NULL_TREE);
343 binding->scope = level;
345 else
346 binding = new_class_binding (id, decl, /*type=*/NULL_TREE, level);
348 /* Now, fill in the binding information. */
349 binding->previous = IDENTIFIER_BINDING (id);
350 INHERITED_VALUE_BINDING_P (binding) = 0;
351 LOCAL_BINDING_P (binding) = (level != class_binding_level);
353 /* And put it on the front of the list of bindings for ID. */
354 IDENTIFIER_BINDING (id) = binding;
357 /* Remove the binding for DECL which should be the innermost binding
358 for ID. */
360 void
361 pop_binding (tree id, tree decl)
363 cxx_binding *binding;
365 if (id == NULL_TREE)
366 /* It's easiest to write the loops that call this function without
367 checking whether or not the entities involved have names. We
368 get here for such an entity. */
369 return;
371 /* Get the innermost binding for ID. */
372 binding = IDENTIFIER_BINDING (id);
374 /* The name should be bound. */
375 gcc_assert (binding != NULL);
377 /* The DECL will be either the ordinary binding or the type
378 binding for this identifier. Remove that binding. */
379 if (binding->value == decl)
380 binding->value = NULL_TREE;
381 else
383 gcc_assert (binding->type == decl);
384 binding->type = NULL_TREE;
387 if (!binding->value && !binding->type)
389 /* We're completely done with the innermost binding for this
390 identifier. Unhook it from the list of bindings. */
391 IDENTIFIER_BINDING (id) = binding->previous;
393 /* Add it to the free list. */
394 cxx_binding_free (binding);
398 /* Remove the bindings for the decls of the current level and leave
399 the current scope. */
401 void
402 pop_bindings_and_leave_scope (void)
404 for (tree t = getdecls (); t; t = DECL_CHAIN (t))
405 pop_binding (DECL_NAME (t), t);
406 leave_scope ();
409 /* Strip non dependent using declarations. If DECL is dependent,
410 surreptitiously create a typename_type and return it. */
412 tree
413 strip_using_decl (tree decl)
415 if (decl == NULL_TREE)
416 return NULL_TREE;
418 while (TREE_CODE (decl) == USING_DECL && !DECL_DEPENDENT_P (decl))
419 decl = USING_DECL_DECLS (decl);
421 if (TREE_CODE (decl) == USING_DECL && DECL_DEPENDENT_P (decl)
422 && USING_DECL_TYPENAME_P (decl))
424 /* We have found a type introduced by a using
425 declaration at class scope that refers to a dependent
426 type.
428 using typename :: [opt] nested-name-specifier unqualified-id ;
430 decl = make_typename_type (TREE_TYPE (decl),
431 DECL_NAME (decl),
432 typename_type, tf_error);
433 if (decl != error_mark_node)
434 decl = TYPE_NAME (decl);
437 return decl;
440 /* BINDING records an existing declaration for a name in the current scope.
441 But, DECL is another declaration for that same identifier in the
442 same scope. This is the `struct stat' hack whereby a non-typedef
443 class name or enum-name can be bound at the same level as some other
444 kind of entity.
445 3.3.7/1
447 A class name (9.1) or enumeration name (7.2) can be hidden by the
448 name of an object, function, or enumerator declared in the same scope.
449 If a class or enumeration name and an object, function, or enumerator
450 are declared in the same scope (in any order) with the same name, the
451 class or enumeration name is hidden wherever the object, function, or
452 enumerator name is visible.
454 It's the responsibility of the caller to check that
455 inserting this name is valid here. Returns nonzero if the new binding
456 was successful. */
458 static bool
459 supplement_binding_1 (cxx_binding *binding, tree decl)
461 tree bval = binding->value;
462 bool ok = true;
463 tree target_bval = strip_using_decl (bval);
464 tree target_decl = strip_using_decl (decl);
466 if (TREE_CODE (target_decl) == TYPE_DECL && DECL_ARTIFICIAL (target_decl)
467 && target_decl != target_bval
468 && (TREE_CODE (target_bval) != TYPE_DECL
469 /* We allow pushing an enum multiple times in a class
470 template in order to handle late matching of underlying
471 type on an opaque-enum-declaration followed by an
472 enum-specifier. */
473 || (processing_template_decl
474 && TREE_CODE (TREE_TYPE (target_decl)) == ENUMERAL_TYPE
475 && TREE_CODE (TREE_TYPE (target_bval)) == ENUMERAL_TYPE
476 && (dependent_type_p (ENUM_UNDERLYING_TYPE
477 (TREE_TYPE (target_decl)))
478 || dependent_type_p (ENUM_UNDERLYING_TYPE
479 (TREE_TYPE (target_bval)))))))
480 /* The new name is the type name. */
481 binding->type = decl;
482 else if (/* TARGET_BVAL is null when push_class_level_binding moves
483 an inherited type-binding out of the way to make room
484 for a new value binding. */
485 !target_bval
486 /* TARGET_BVAL is error_mark_node when TARGET_DECL's name
487 has been used in a non-class scope prior declaration.
488 In that case, we should have already issued a
489 diagnostic; for graceful error recovery purpose, pretend
490 this was the intended declaration for that name. */
491 || target_bval == error_mark_node
492 /* If TARGET_BVAL is anticipated but has not yet been
493 declared, pretend it is not there at all. */
494 || (TREE_CODE (target_bval) == FUNCTION_DECL
495 && DECL_ANTICIPATED (target_bval)
496 && !DECL_HIDDEN_FRIEND_P (target_bval)))
497 binding->value = decl;
498 else if (TREE_CODE (target_bval) == TYPE_DECL
499 && DECL_ARTIFICIAL (target_bval)
500 && target_decl != target_bval
501 && (TREE_CODE (target_decl) != TYPE_DECL
502 || same_type_p (TREE_TYPE (target_decl),
503 TREE_TYPE (target_bval))))
505 /* The old binding was a type name. It was placed in
506 VALUE field because it was thought, at the point it was
507 declared, to be the only entity with such a name. Move the
508 type name into the type slot; it is now hidden by the new
509 binding. */
510 binding->type = bval;
511 binding->value = decl;
512 binding->value_is_inherited = false;
514 else if (TREE_CODE (target_bval) == TYPE_DECL
515 && TREE_CODE (target_decl) == TYPE_DECL
516 && DECL_NAME (target_decl) == DECL_NAME (target_bval)
517 && binding->scope->kind != sk_class
518 && (same_type_p (TREE_TYPE (target_decl), TREE_TYPE (target_bval))
519 /* If either type involves template parameters, we must
520 wait until instantiation. */
521 || uses_template_parms (TREE_TYPE (target_decl))
522 || uses_template_parms (TREE_TYPE (target_bval))))
523 /* We have two typedef-names, both naming the same type to have
524 the same name. In general, this is OK because of:
526 [dcl.typedef]
528 In a given scope, a typedef specifier can be used to redefine
529 the name of any type declared in that scope to refer to the
530 type to which it already refers.
532 However, in class scopes, this rule does not apply due to the
533 stricter language in [class.mem] prohibiting redeclarations of
534 members. */
535 ok = false;
536 /* There can be two block-scope declarations of the same variable,
537 so long as they are `extern' declarations. However, there cannot
538 be two declarations of the same static data member:
540 [class.mem]
542 A member shall not be declared twice in the
543 member-specification. */
544 else if (VAR_P (target_decl)
545 && VAR_P (target_bval)
546 && DECL_EXTERNAL (target_decl) && DECL_EXTERNAL (target_bval)
547 && !DECL_CLASS_SCOPE_P (target_decl))
549 duplicate_decls (decl, binding->value, /*newdecl_is_friend=*/false);
550 ok = false;
552 else if (TREE_CODE (decl) == NAMESPACE_DECL
553 && TREE_CODE (bval) == NAMESPACE_DECL
554 && DECL_NAMESPACE_ALIAS (decl)
555 && DECL_NAMESPACE_ALIAS (bval)
556 && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl))
557 /* [namespace.alias]
559 In a declarative region, a namespace-alias-definition can be
560 used to redefine a namespace-alias declared in that declarative
561 region to refer only to the namespace to which it already
562 refers. */
563 ok = false;
564 else if (maybe_remove_implicit_alias (bval))
566 /* There was a mangling compatibility alias using this mangled name,
567 but now we have a real decl that wants to use it instead. */
568 binding->value = decl;
570 else
572 if (!error_operand_p (bval))
573 diagnose_name_conflict (decl, bval);
574 ok = false;
577 return ok;
580 /* Diagnose a name conflict between DECL and BVAL. */
582 static void
583 diagnose_name_conflict (tree decl, tree bval)
585 if (TREE_CODE (decl) == TREE_CODE (bval)
586 && (TREE_CODE (decl) != TYPE_DECL
587 || (DECL_ARTIFICIAL (decl) && DECL_ARTIFICIAL (bval))
588 || (!DECL_ARTIFICIAL (decl) && !DECL_ARTIFICIAL (bval)))
589 && !is_overloaded_fn (decl))
590 error ("redeclaration of %q#D", decl);
591 else
592 error ("%q#D conflicts with a previous declaration", decl);
594 inform (location_of (bval), "previous declaration %q#D", bval);
597 /* Wrapper for supplement_binding_1. */
599 static bool
600 supplement_binding (cxx_binding *binding, tree decl)
602 bool ret;
603 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
604 ret = supplement_binding_1 (binding, decl);
605 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
606 return ret;
609 /* Add DECL to the list of things declared in B. */
611 static void
612 add_decl_to_level (tree decl, cp_binding_level *b)
614 /* We used to record virtual tables as if they were ordinary
615 variables, but no longer do so. */
616 gcc_assert (!(VAR_P (decl) && DECL_VIRTUAL_P (decl)));
618 if (TREE_CODE (decl) == NAMESPACE_DECL
619 && !DECL_NAMESPACE_ALIAS (decl))
621 DECL_CHAIN (decl) = b->namespaces;
622 b->namespaces = decl;
624 else
626 /* We build up the list in reverse order, and reverse it later if
627 necessary. */
628 TREE_CHAIN (decl) = b->names;
629 b->names = decl;
631 /* If appropriate, add decl to separate list of statics. We
632 include extern variables because they might turn out to be
633 static later. It's OK for this list to contain a few false
634 positives. */
635 if (b->kind == sk_namespace)
636 if ((VAR_P (decl)
637 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
638 || (TREE_CODE (decl) == FUNCTION_DECL
639 && (!TREE_PUBLIC (decl)
640 || decl_anon_ns_mem_p (decl)
641 || DECL_DECLARED_INLINE_P (decl))))
642 vec_safe_push (b->static_decls, decl);
646 /* Record a decl-node X as belonging to the current lexical scope.
647 Check for errors (such as an incompatible declaration for the same
648 name already seen in the same scope). IS_FRIEND is true if X is
649 declared as a friend.
651 Returns either X or an old decl for the same name.
652 If an old decl is returned, it may have been smashed
653 to agree with what X says. */
655 static tree
656 pushdecl_maybe_friend_1 (tree x, bool is_friend)
658 tree t;
659 tree name;
660 int need_new_binding;
662 if (x == error_mark_node)
663 return error_mark_node;
665 need_new_binding = 1;
667 if (DECL_TEMPLATE_PARM_P (x))
668 /* Template parameters have no context; they are not X::T even
669 when declared within a class or namespace. */
671 else
673 if (current_function_decl && x != current_function_decl
674 /* A local declaration for a function doesn't constitute
675 nesting. */
676 && TREE_CODE (x) != FUNCTION_DECL
677 /* A local declaration for an `extern' variable is in the
678 scope of the current namespace, not the current
679 function. */
680 && !(VAR_P (x) && DECL_EXTERNAL (x))
681 /* When parsing the parameter list of a function declarator,
682 don't set DECL_CONTEXT to an enclosing function. When we
683 push the PARM_DECLs in order to process the function body,
684 current_binding_level->this_entity will be set. */
685 && !(TREE_CODE (x) == PARM_DECL
686 && current_binding_level->kind == sk_function_parms
687 && current_binding_level->this_entity == NULL)
688 && !DECL_CONTEXT (x))
689 DECL_CONTEXT (x) = current_function_decl;
691 /* If this is the declaration for a namespace-scope function,
692 but the declaration itself is in a local scope, mark the
693 declaration. */
694 if (TREE_CODE (x) == FUNCTION_DECL
695 && DECL_NAMESPACE_SCOPE_P (x)
696 && current_function_decl
697 && x != current_function_decl)
698 DECL_LOCAL_FUNCTION_P (x) = 1;
701 name = DECL_NAME (x);
702 if (name)
704 int different_binding_level = 0;
706 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
707 name = TREE_OPERAND (name, 0);
709 /* In case this decl was explicitly namespace-qualified, look it
710 up in its namespace context. */
711 if (DECL_NAMESPACE_SCOPE_P (x) && namespace_bindings_p ())
712 t = namespace_binding (name, DECL_CONTEXT (x));
713 else
714 t = lookup_name_innermost_nonclass_level (name);
716 /* [basic.link] If there is a visible declaration of an entity
717 with linkage having the same name and type, ignoring entities
718 declared outside the innermost enclosing namespace scope, the
719 block scope declaration declares that same entity and
720 receives the linkage of the previous declaration. */
721 if (! t && current_function_decl && x != current_function_decl
722 && VAR_OR_FUNCTION_DECL_P (x)
723 && DECL_EXTERNAL (x))
725 /* Look in block scope. */
726 t = innermost_non_namespace_value (name);
727 /* Or in the innermost namespace. */
728 if (! t)
729 t = namespace_binding (name, DECL_CONTEXT (x));
730 /* Does it have linkage? Note that if this isn't a DECL, it's an
731 OVERLOAD, which is OK. */
732 if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t)))
733 t = NULL_TREE;
734 if (t)
735 different_binding_level = 1;
738 /* If we are declaring a function, and the result of name-lookup
739 was an OVERLOAD, look for an overloaded instance that is
740 actually the same as the function we are declaring. (If
741 there is one, we have to merge our declaration with the
742 previous declaration.) */
743 if (t && TREE_CODE (t) == OVERLOAD)
745 tree match;
747 if (TREE_CODE (x) == FUNCTION_DECL)
748 for (match = t; match; match = OVL_NEXT (match))
750 if (decls_match (OVL_CURRENT (match), x))
751 break;
753 else
754 /* Just choose one. */
755 match = t;
757 if (match)
758 t = OVL_CURRENT (match);
759 else
760 t = NULL_TREE;
763 if (t && t != error_mark_node)
765 if (different_binding_level)
767 if (decls_match (x, t))
768 /* The standard only says that the local extern
769 inherits linkage from the previous decl; in
770 particular, default args are not shared. Add
771 the decl into a hash table to make sure only
772 the previous decl in this case is seen by the
773 middle end. */
775 struct cxx_int_tree_map *h;
777 TREE_PUBLIC (x) = TREE_PUBLIC (t);
779 if (cp_function_chain->extern_decl_map == NULL)
780 cp_function_chain->extern_decl_map
781 = hash_table<cxx_int_tree_map_hasher>::create_ggc (20);
783 h = ggc_alloc<cxx_int_tree_map> ();
784 h->uid = DECL_UID (x);
785 h->to = t;
786 cxx_int_tree_map **loc = cp_function_chain->extern_decl_map
787 ->find_slot (h, INSERT);
788 *loc = h;
791 else if (TREE_CODE (t) == PARM_DECL)
793 /* Check for duplicate params. */
794 tree d = duplicate_decls (x, t, is_friend);
795 if (d)
796 return d;
798 else if ((DECL_EXTERN_C_FUNCTION_P (x)
799 || DECL_FUNCTION_TEMPLATE_P (x))
800 && is_overloaded_fn (t))
801 /* Don't do anything just yet. */;
802 else if (t == wchar_decl_node)
804 if (! DECL_IN_SYSTEM_HEADER (x))
805 pedwarn (input_location, OPT_Wpedantic, "redeclaration of %<wchar_t%> as %qT",
806 TREE_TYPE (x));
808 /* Throw away the redeclaration. */
809 return t;
811 else
813 tree olddecl = duplicate_decls (x, t, is_friend);
815 /* If the redeclaration failed, we can stop at this
816 point. */
817 if (olddecl == error_mark_node)
818 return error_mark_node;
820 if (olddecl)
822 if (TREE_CODE (t) == TYPE_DECL)
823 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
825 return t;
827 else if (DECL_MAIN_P (x) && TREE_CODE (t) == FUNCTION_DECL)
829 /* A redeclaration of main, but not a duplicate of the
830 previous one.
832 [basic.start.main]
834 This function shall not be overloaded. */
835 error ("invalid redeclaration of %q+D", t);
836 error ("as %qD", x);
837 /* We don't try to push this declaration since that
838 causes a crash. */
839 return x;
844 /* If x has C linkage-specification, (extern "C"),
845 lookup its binding, in case it's already bound to an object.
846 The lookup is done in all namespaces.
847 If we find an existing binding, make sure it has the same
848 exception specification as x, otherwise, bail in error [7.5, 7.6]. */
849 if ((TREE_CODE (x) == FUNCTION_DECL)
850 && DECL_EXTERN_C_P (x)
851 /* We should ignore declarations happening in system headers. */
852 && !DECL_ARTIFICIAL (x)
853 && !DECL_IN_SYSTEM_HEADER (x))
855 tree previous = lookup_extern_c_fun_in_all_ns (x);
856 if (previous
857 && !DECL_ARTIFICIAL (previous)
858 && !DECL_IN_SYSTEM_HEADER (previous)
859 && DECL_CONTEXT (previous) != DECL_CONTEXT (x))
861 /* In case either x or previous is declared to throw an exception,
862 make sure both exception specifications are equal. */
863 if (decls_match (x, previous))
865 tree x_exception_spec = NULL_TREE;
866 tree previous_exception_spec = NULL_TREE;
868 x_exception_spec =
869 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (x));
870 previous_exception_spec =
871 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (previous));
872 if (!comp_except_specs (previous_exception_spec,
873 x_exception_spec,
874 ce_normal))
876 pedwarn (input_location, 0,
877 "declaration of %q#D with C language linkage",
879 pedwarn (DECL_SOURCE_LOCATION (previous), 0,
880 "conflicts with previous declaration %q#D",
881 previous);
882 pedwarn (input_location, 0,
883 "due to different exception specifications");
884 return error_mark_node;
886 if (DECL_ASSEMBLER_NAME_SET_P (previous))
887 SET_DECL_ASSEMBLER_NAME (x,
888 DECL_ASSEMBLER_NAME (previous));
890 else
892 pedwarn (input_location, 0,
893 "declaration of %q#D with C language linkage", x);
894 pedwarn (DECL_SOURCE_LOCATION (previous), 0,
895 "conflicts with previous declaration %q#D",
896 previous);
901 check_template_shadow (x);
903 /* If this is a function conjured up by the back end, massage it
904 so it looks friendly. */
905 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x))
907 retrofit_lang_decl (x);
908 SET_DECL_LANGUAGE (x, lang_c);
911 t = x;
912 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x))
914 t = push_overloaded_decl (x, PUSH_LOCAL, is_friend);
915 if (!namespace_bindings_p ())
916 /* We do not need to create a binding for this name;
917 push_overloaded_decl will have already done so if
918 necessary. */
919 need_new_binding = 0;
921 else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
923 t = push_overloaded_decl (x, PUSH_GLOBAL, is_friend);
924 if (t == x)
925 add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
928 if (DECL_DECLARES_FUNCTION_P (t))
930 check_default_args (t);
932 if (is_friend && t == x && !flag_friend_injection)
934 /* This is a new friend declaration of a function or a
935 function template, so hide it from ordinary function
936 lookup. */
937 DECL_ANTICIPATED (t) = 1;
938 DECL_HIDDEN_FRIEND_P (t) = 1;
942 if (t != x || DECL_FUNCTION_TEMPLATE_P (t))
943 return t;
945 /* If declaring a type as a typedef, copy the type (unless we're
946 at line 0), and install this TYPE_DECL as the new type's typedef
947 name. See the extensive comment of set_underlying_type (). */
948 if (TREE_CODE (x) == TYPE_DECL)
950 tree type = TREE_TYPE (x);
952 if (DECL_IS_BUILTIN (x)
953 || (TREE_TYPE (x) != error_mark_node
954 && TYPE_NAME (type) != x
955 /* We don't want to copy the type when all we're
956 doing is making a TYPE_DECL for the purposes of
957 inlining. */
958 && (!TYPE_NAME (type)
959 || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x))))
960 set_underlying_type (x);
962 if (type != error_mark_node
963 && TYPE_IDENTIFIER (type))
964 set_identifier_type_value (DECL_NAME (x), x);
966 /* If this is a locally defined typedef in a function that
967 is not a template instantation, record it to implement
968 -Wunused-local-typedefs. */
969 if (!instantiating_current_function_p ())
970 record_locally_defined_typedef (x);
973 /* Multiple external decls of the same identifier ought to match.
975 We get warnings about inline functions where they are defined.
976 We get warnings about other functions from push_overloaded_decl.
978 Avoid duplicate warnings where they are used. */
979 if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
981 tree decl;
983 decl = IDENTIFIER_NAMESPACE_VALUE (name);
984 if (decl && TREE_CODE (decl) == OVERLOAD)
985 decl = OVL_FUNCTION (decl);
987 if (decl && decl != error_mark_node
988 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl))
989 /* If different sort of thing, we already gave an error. */
990 && TREE_CODE (decl) == TREE_CODE (x)
991 && !comptypes (TREE_TYPE (x), TREE_TYPE (decl),
992 COMPARE_REDECLARATION))
994 if (permerror (input_location, "type mismatch with previous "
995 "external decl of %q#D", x))
996 inform (DECL_SOURCE_LOCATION (decl),
997 "previous external decl of %q#D", decl);
1001 /* This name is new in its binding level.
1002 Install the new declaration and return it. */
1003 if (namespace_bindings_p ())
1005 /* Install a global value. */
1007 /* If the first global decl has external linkage,
1008 warn if we later see static one. */
1009 if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
1010 TREE_PUBLIC (name) = 1;
1012 /* Bind the name for the entity. */
1013 if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
1014 && t != NULL_TREE)
1015 && (TREE_CODE (x) == TYPE_DECL
1016 || VAR_P (x)
1017 || TREE_CODE (x) == NAMESPACE_DECL
1018 || TREE_CODE (x) == CONST_DECL
1019 || TREE_CODE (x) == TEMPLATE_DECL))
1020 SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
1022 /* If new decl is `static' and an `extern' was seen previously,
1023 warn about it. */
1024 if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
1025 warn_extern_redeclared_static (x, t);
1027 else
1029 /* Here to install a non-global value. */
1030 tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
1031 tree oldlocal = NULL_TREE;
1032 cp_binding_level *oldscope = NULL;
1033 cxx_binding *oldbinding = outer_binding (name, NULL, true);
1034 if (oldbinding)
1036 oldlocal = oldbinding->value;
1037 oldscope = oldbinding->scope;
1040 if (need_new_binding)
1042 push_local_binding (name, x, 0);
1043 /* Because push_local_binding will hook X on to the
1044 current_binding_level's name list, we don't want to
1045 do that again below. */
1046 need_new_binding = 0;
1049 /* If this is a TYPE_DECL, push it into the type value slot. */
1050 if (TREE_CODE (x) == TYPE_DECL)
1051 set_identifier_type_value (name, x);
1053 /* Clear out any TYPE_DECL shadowed by a namespace so that
1054 we won't think this is a type. The C struct hack doesn't
1055 go through namespaces. */
1056 if (TREE_CODE (x) == NAMESPACE_DECL)
1057 set_identifier_type_value (name, NULL_TREE);
1059 if (oldlocal)
1061 tree d = oldlocal;
1063 while (oldlocal
1064 && VAR_P (oldlocal)
1065 && DECL_DEAD_FOR_LOCAL (oldlocal))
1066 oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal);
1068 if (oldlocal == NULL_TREE)
1069 oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d));
1072 /* If this is an extern function declaration, see if we
1073 have a global definition or declaration for the function. */
1074 if (oldlocal == NULL_TREE
1075 && DECL_EXTERNAL (x)
1076 && oldglobal != NULL_TREE
1077 && TREE_CODE (x) == FUNCTION_DECL
1078 && TREE_CODE (oldglobal) == FUNCTION_DECL)
1080 /* We have one. Their types must agree. */
1081 if (decls_match (x, oldglobal))
1082 /* OK */;
1083 else
1085 warning (0, "extern declaration of %q#D doesn%'t match", x);
1086 warning_at (DECL_SOURCE_LOCATION (oldglobal), 0,
1087 "global declaration %q#D", oldglobal);
1090 /* If we have a local external declaration,
1091 and no file-scope declaration has yet been seen,
1092 then if we later have a file-scope decl it must not be static. */
1093 if (oldlocal == NULL_TREE
1094 && oldglobal == NULL_TREE
1095 && DECL_EXTERNAL (x)
1096 && TREE_PUBLIC (x))
1097 TREE_PUBLIC (name) = 1;
1099 /* Don't complain about the parms we push and then pop
1100 while tentatively parsing a function declarator. */
1101 if (TREE_CODE (x) == PARM_DECL && DECL_CONTEXT (x) == NULL_TREE)
1102 /* Ignore. */;
1104 /* Warn if shadowing an argument at the top level of the body. */
1105 else if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
1106 /* Inline decls shadow nothing. */
1107 && !DECL_FROM_INLINE (x)
1108 && (TREE_CODE (oldlocal) == PARM_DECL
1109 || VAR_P (oldlocal)
1110 /* If the old decl is a type decl, only warn if the
1111 old decl is an explicit typedef or if both the old
1112 and new decls are type decls. */
1113 || (TREE_CODE (oldlocal) == TYPE_DECL
1114 && (!DECL_ARTIFICIAL (oldlocal)
1115 || TREE_CODE (x) == TYPE_DECL)))
1116 /* Don't check for internally generated vars unless
1117 it's an implicit typedef (see create_implicit_typedef
1118 in decl.c) or anonymous union variable. */
1119 && (!DECL_ARTIFICIAL (x)
1120 || DECL_IMPLICIT_TYPEDEF_P (x)
1121 || (VAR_P (x) && DECL_ANON_UNION_VAR_P (x))))
1123 bool nowarn = false;
1125 /* Don't complain if it's from an enclosing function. */
1126 if (DECL_CONTEXT (oldlocal) == current_function_decl
1127 && TREE_CODE (x) != PARM_DECL
1128 && TREE_CODE (oldlocal) == PARM_DECL)
1130 /* Go to where the parms should be and see if we find
1131 them there. */
1132 cp_binding_level *b = current_binding_level->level_chain;
1134 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
1135 /* Skip the ctor/dtor cleanup level. */
1136 b = b->level_chain;
1138 /* ARM $8.3 */
1139 if (b->kind == sk_function_parms)
1141 error ("declaration of %q#D shadows a parameter", x);
1142 nowarn = true;
1146 /* The local structure or class can't use parameters of
1147 the containing function anyway. */
1148 if (DECL_CONTEXT (oldlocal) != current_function_decl)
1150 cp_binding_level *scope = current_binding_level;
1151 tree context = DECL_CONTEXT (oldlocal);
1152 for (; scope; scope = scope->level_chain)
1154 if (scope->kind == sk_function_parms
1155 && scope->this_entity == context)
1156 break;
1157 if (scope->kind == sk_class
1158 && !LAMBDA_TYPE_P (scope->this_entity))
1160 nowarn = true;
1161 break;
1165 /* Error if redeclaring a local declared in a
1166 init-statement or in the condition of an if or
1167 switch statement when the new declaration is in the
1168 outermost block of the controlled statement.
1169 Redeclaring a variable from a for or while condition is
1170 detected elsewhere. */
1171 else if (VAR_P (oldlocal)
1172 && oldscope == current_binding_level->level_chain
1173 && (oldscope->kind == sk_cond
1174 || oldscope->kind == sk_for))
1176 error ("redeclaration of %q#D", x);
1177 inform (DECL_SOURCE_LOCATION (oldlocal),
1178 "%q#D previously declared here", oldlocal);
1179 nowarn = true;
1181 /* C++11:
1182 3.3.3/3: The name declared in an exception-declaration (...)
1183 shall not be redeclared in the outermost block of the handler.
1184 3.3.3/2: A parameter name shall not be redeclared (...) in
1185 the outermost block of any handler associated with a
1186 function-try-block.
1187 3.4.1/15: The function parameter names shall not be redeclared
1188 in the exception-declaration nor in the outermost block of a
1189 handler for the function-try-block. */
1190 else if ((VAR_P (oldlocal)
1191 && oldscope == current_binding_level->level_chain
1192 && oldscope->kind == sk_catch)
1193 || (TREE_CODE (oldlocal) == PARM_DECL
1194 && (current_binding_level->kind == sk_catch
1195 || (current_binding_level->level_chain->kind
1196 == sk_catch))
1197 && in_function_try_handler))
1199 if (permerror (input_location, "redeclaration of %q#D", x))
1200 inform (DECL_SOURCE_LOCATION (oldlocal),
1201 "%q#D previously declared here", oldlocal);
1202 nowarn = true;
1205 if ((warn_shadow
1206 || warn_shadow_local
1207 || warn_shadow_compatible_local)
1208 && !nowarn)
1210 bool warned;
1211 enum opt_code warning_code;
1212 /* If '-Wshadow=compatible-local' is specified without other
1213 -Wshadow= flags, we will warn only when the type of the
1214 shadowing variable (i.e. x) can be converted to that of
1215 the shadowed parameter (oldlocal). The reason why we only
1216 check if x's type can be converted to oldlocal's type
1217 (but not the other way around) is because when users
1218 accidentally shadow a parameter, more than often they
1219 would use the variable thinking (mistakenly) it's still
1220 the parameter. It would be rare that users would use the
1221 variable in the place that expects the parameter but
1222 thinking it's a new decl. */
1223 if (warn_shadow)
1224 warning_code = OPT_Wshadow;
1225 else if (can_convert (TREE_TYPE (oldlocal), TREE_TYPE (x),
1226 tf_none))
1227 warning_code = OPT_Wshadow_compatible_local;
1228 else
1229 warning_code = OPT_Wshadow_local;
1231 if (TREE_CODE (oldlocal) == PARM_DECL)
1232 warned = warning_at (input_location, warning_code,
1233 "declaration of %q#D shadows a parameter", x);
1234 else if (is_capture_proxy (oldlocal))
1235 warned = warning_at (input_location, warning_code,
1236 "declaration of %qD shadows a lambda capture",
1238 else
1239 warned = warning_at (input_location, warning_code,
1240 "declaration of %qD shadows a previous local",
1243 if (warned)
1244 inform (DECL_SOURCE_LOCATION (oldlocal),
1245 "shadowed declaration is here");
1249 /* Maybe warn if shadowing something else. */
1250 else if (warn_shadow && !DECL_EXTERNAL (x)
1251 /* No shadow warnings for internally generated vars unless
1252 it's an implicit typedef (see create_implicit_typedef
1253 in decl.c). */
1254 && (! DECL_ARTIFICIAL (x) || DECL_IMPLICIT_TYPEDEF_P (x))
1255 /* No shadow warnings for vars made for inlining. */
1256 && ! DECL_FROM_INLINE (x))
1258 tree member;
1260 if (nonlambda_method_basetype ())
1261 member = lookup_member (current_nonlambda_class_type (),
1262 name,
1263 /*protect=*/0,
1264 /*want_type=*/false,
1265 tf_warning_or_error);
1266 else
1267 member = NULL_TREE;
1269 if (member && !TREE_STATIC (member))
1271 if (BASELINK_P (member))
1272 member = BASELINK_FUNCTIONS (member);
1273 member = OVL_CURRENT (member);
1275 /* Do not warn if a variable shadows a function, unless
1276 the variable is a function or a pointer-to-function. */
1277 if (TREE_CODE (member) != FUNCTION_DECL
1278 || TREE_CODE (x) == FUNCTION_DECL
1279 || TYPE_PTRFN_P (TREE_TYPE (x))
1280 || TYPE_PTRMEMFUNC_P (TREE_TYPE (x)))
1282 if (warning_at (input_location, OPT_Wshadow,
1283 "declaration of %qD shadows a member of %qT",
1284 x, current_nonlambda_class_type ())
1285 && DECL_P (member))
1286 inform (DECL_SOURCE_LOCATION (member),
1287 "shadowed declaration is here");
1290 else if (oldglobal != NULL_TREE
1291 && (VAR_P (oldglobal)
1292 /* If the old decl is a type decl, only warn if the
1293 old decl is an explicit typedef or if both the
1294 old and new decls are type decls. */
1295 || (TREE_CODE (oldglobal) == TYPE_DECL
1296 && (!DECL_ARTIFICIAL (oldglobal)
1297 || TREE_CODE (x) == TYPE_DECL)))
1298 && !instantiating_current_function_p ())
1299 /* XXX shadow warnings in outer-more namespaces */
1301 if (warning_at (input_location, OPT_Wshadow,
1302 "declaration of %qD shadows a "
1303 "global declaration", x))
1304 inform (DECL_SOURCE_LOCATION (oldglobal),
1305 "shadowed declaration is here");
1310 if (VAR_P (x))
1311 maybe_register_incomplete_var (x);
1314 if (need_new_binding)
1315 add_decl_to_level (x,
1316 DECL_NAMESPACE_SCOPE_P (x)
1317 ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x))
1318 : current_binding_level);
1320 return x;
1323 /* Wrapper for pushdecl_maybe_friend_1. */
1325 tree
1326 pushdecl_maybe_friend (tree x, bool is_friend)
1328 tree ret;
1329 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
1330 ret = pushdecl_maybe_friend_1 (x, is_friend);
1331 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
1332 return ret;
1335 /* Record a decl-node X as belonging to the current lexical scope. */
1337 tree
1338 pushdecl (tree x)
1340 return pushdecl_maybe_friend (x, false);
1343 /* Enter DECL into the symbol table, if that's appropriate. Returns
1344 DECL, or a modified version thereof. */
1346 tree
1347 maybe_push_decl (tree decl)
1349 tree type = TREE_TYPE (decl);
1351 /* Add this decl to the current binding level, but not if it comes
1352 from another scope, e.g. a static member variable. TEM may equal
1353 DECL or it may be a previous decl of the same name. */
1354 if (decl == error_mark_node
1355 || (TREE_CODE (decl) != PARM_DECL
1356 && DECL_CONTEXT (decl) != NULL_TREE
1357 /* Definitions of namespace members outside their namespace are
1358 possible. */
1359 && !DECL_NAMESPACE_SCOPE_P (decl))
1360 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
1361 || type == unknown_type_node
1362 /* The declaration of a template specialization does not affect
1363 the functions available for overload resolution, so we do not
1364 call pushdecl. */
1365 || (TREE_CODE (decl) == FUNCTION_DECL
1366 && DECL_TEMPLATE_SPECIALIZATION (decl)))
1367 return decl;
1368 else
1369 return pushdecl (decl);
1372 /* Bind DECL to ID in the current_binding_level, assumed to be a local
1373 binding level. If PUSH_USING is set in FLAGS, we know that DECL
1374 doesn't really belong to this binding level, that it got here
1375 through a using-declaration. */
1377 void
1378 push_local_binding (tree id, tree decl, int flags)
1380 cp_binding_level *b;
1382 /* Skip over any local classes. This makes sense if we call
1383 push_local_binding with a friend decl of a local class. */
1384 b = innermost_nonclass_level ();
1386 if (lookup_name_innermost_nonclass_level (id))
1388 /* Supplement the existing binding. */
1389 if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
1390 /* It didn't work. Something else must be bound at this
1391 level. Do not add DECL to the list of things to pop
1392 later. */
1393 return;
1395 else
1396 /* Create a new binding. */
1397 push_binding (id, decl, b);
1399 if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
1400 /* We must put the OVERLOAD into a TREE_LIST since the
1401 TREE_CHAIN of an OVERLOAD is already used. Similarly for
1402 decls that got here through a using-declaration. */
1403 decl = build_tree_list (NULL_TREE, decl);
1405 /* And put DECL on the list of things declared by the current
1406 binding level. */
1407 add_decl_to_level (decl, b);
1410 /* Check to see whether or not DECL is a variable that would have been
1411 in scope under the ARM, but is not in scope under the ANSI/ISO
1412 standard. If so, issue an error message. If name lookup would
1413 work in both cases, but return a different result, this function
1414 returns the result of ANSI/ISO lookup. Otherwise, it returns
1415 DECL. */
1417 tree
1418 check_for_out_of_scope_variable (tree decl)
1420 tree shadowed;
1422 /* We only care about out of scope variables. */
1423 if (!(VAR_P (decl) && DECL_DEAD_FOR_LOCAL (decl)))
1424 return decl;
1426 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (decl)
1427 ? DECL_SHADOWED_FOR_VAR (decl) : NULL_TREE ;
1428 while (shadowed != NULL_TREE && VAR_P (shadowed)
1429 && DECL_DEAD_FOR_LOCAL (shadowed))
1430 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (shadowed)
1431 ? DECL_SHADOWED_FOR_VAR (shadowed) : NULL_TREE;
1432 if (!shadowed)
1433 shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl));
1434 if (shadowed)
1436 if (!DECL_ERROR_REPORTED (decl))
1438 warning (0, "name lookup of %qD changed", DECL_NAME (decl));
1439 warning_at (DECL_SOURCE_LOCATION (shadowed), 0,
1440 " matches this %qD under ISO standard rules",
1441 shadowed);
1442 warning_at (DECL_SOURCE_LOCATION (decl), 0,
1443 " matches this %qD under old rules", decl);
1444 DECL_ERROR_REPORTED (decl) = 1;
1446 return shadowed;
1449 /* If we have already complained about this declaration, there's no
1450 need to do it again. */
1451 if (DECL_ERROR_REPORTED (decl))
1452 return decl;
1454 DECL_ERROR_REPORTED (decl) = 1;
1456 if (TREE_TYPE (decl) == error_mark_node)
1457 return decl;
1459 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
1461 error ("name lookup of %qD changed for ISO %<for%> scoping",
1462 DECL_NAME (decl));
1463 error (" cannot use obsolete binding at %q+D because "
1464 "it has a destructor", decl);
1465 return error_mark_node;
1467 else
1469 permerror (input_location, "name lookup of %qD changed for ISO %<for%> scoping",
1470 DECL_NAME (decl));
1471 if (flag_permissive)
1472 permerror (DECL_SOURCE_LOCATION (decl),
1473 " using obsolete binding at %qD", decl);
1474 else
1476 static bool hint;
1477 if (!hint)
1479 inform (input_location, "(if you use %<-fpermissive%> G++ will accept your code)");
1480 hint = true;
1485 return decl;
1488 /* true means unconditionally make a BLOCK for the next level pushed. */
1490 static bool keep_next_level_flag;
1492 static int binding_depth = 0;
1494 static void
1495 indent (int depth)
1497 int i;
1499 for (i = 0; i < depth * 2; i++)
1500 putc (' ', stderr);
1503 /* Return a string describing the kind of SCOPE we have. */
1504 static const char *
1505 cp_binding_level_descriptor (cp_binding_level *scope)
1507 /* The order of this table must match the "scope_kind"
1508 enumerators. */
1509 static const char* scope_kind_names[] = {
1510 "block-scope",
1511 "cleanup-scope",
1512 "try-scope",
1513 "catch-scope",
1514 "for-scope",
1515 "function-parameter-scope",
1516 "class-scope",
1517 "namespace-scope",
1518 "template-parameter-scope",
1519 "template-explicit-spec-scope"
1521 const scope_kind kind = scope->explicit_spec_p
1522 ? sk_template_spec : scope->kind;
1524 return scope_kind_names[kind];
1527 /* Output a debugging information about SCOPE when performing
1528 ACTION at LINE. */
1529 static void
1530 cp_binding_level_debug (cp_binding_level *scope, int line, const char *action)
1532 const char *desc = cp_binding_level_descriptor (scope);
1533 if (scope->this_entity)
1534 verbatim ("%s %s(%E) %p %d\n", action, desc,
1535 scope->this_entity, (void *) scope, line);
1536 else
1537 verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
1540 /* Return the estimated initial size of the hashtable of a NAMESPACE
1541 scope. */
1543 static inline size_t
1544 namespace_scope_ht_size (tree ns)
1546 tree name = DECL_NAME (ns);
1548 return name == std_identifier
1549 ? NAMESPACE_STD_HT_SIZE
1550 : (name == global_scope_name
1551 ? GLOBAL_SCOPE_HT_SIZE
1552 : NAMESPACE_ORDINARY_HT_SIZE);
1555 /* A chain of binding_level structures awaiting reuse. */
1557 static GTY((deletable)) cp_binding_level *free_binding_level;
1559 /* Insert SCOPE as the innermost binding level. */
1561 void
1562 push_binding_level (cp_binding_level *scope)
1564 /* Add it to the front of currently active scopes stack. */
1565 scope->level_chain = current_binding_level;
1566 current_binding_level = scope;
1567 keep_next_level_flag = false;
1569 if (ENABLE_SCOPE_CHECKING)
1571 scope->binding_depth = binding_depth;
1572 indent (binding_depth);
1573 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
1574 "push");
1575 binding_depth++;
1579 /* Create a new KIND scope and make it the top of the active scopes stack.
1580 ENTITY is the scope of the associated C++ entity (namespace, class,
1581 function, C++0x enumeration); it is NULL otherwise. */
1583 cp_binding_level *
1584 begin_scope (scope_kind kind, tree entity)
1586 cp_binding_level *scope;
1588 /* Reuse or create a struct for this binding level. */
1589 if (!ENABLE_SCOPE_CHECKING && free_binding_level)
1591 scope = free_binding_level;
1592 free_binding_level = scope->level_chain;
1593 memset (scope, 0, sizeof (cp_binding_level));
1595 else
1596 scope = ggc_cleared_alloc<cp_binding_level> ();
1598 scope->this_entity = entity;
1599 scope->more_cleanups_ok = true;
1600 switch (kind)
1602 case sk_cleanup:
1603 scope->keep = true;
1604 break;
1606 case sk_template_spec:
1607 scope->explicit_spec_p = true;
1608 kind = sk_template_parms;
1609 /* Fall through. */
1610 case sk_template_parms:
1611 case sk_block:
1612 case sk_try:
1613 case sk_catch:
1614 case sk_for:
1615 case sk_cond:
1616 case sk_class:
1617 case sk_scoped_enum:
1618 case sk_function_parms:
1619 case sk_transaction:
1620 case sk_omp:
1621 scope->keep = keep_next_level_flag;
1622 break;
1624 case sk_namespace:
1625 NAMESPACE_LEVEL (entity) = scope;
1626 vec_alloc (scope->static_decls,
1627 (DECL_NAME (entity) == std_identifier
1628 || DECL_NAME (entity) == global_scope_name) ? 200 : 10);
1629 break;
1631 default:
1632 /* Should not happen. */
1633 gcc_unreachable ();
1634 break;
1636 scope->kind = kind;
1638 push_binding_level (scope);
1640 return scope;
1643 /* We're about to leave current scope. Pop the top of the stack of
1644 currently active scopes. Return the enclosing scope, now active. */
1646 cp_binding_level *
1647 leave_scope (void)
1649 cp_binding_level *scope = current_binding_level;
1651 if (scope->kind == sk_namespace && class_binding_level)
1652 current_binding_level = class_binding_level;
1654 /* We cannot leave a scope, if there are none left. */
1655 if (NAMESPACE_LEVEL (global_namespace))
1656 gcc_assert (!global_scope_p (scope));
1658 if (ENABLE_SCOPE_CHECKING)
1660 indent (--binding_depth);
1661 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
1662 "leave");
1665 /* Move one nesting level up. */
1666 current_binding_level = scope->level_chain;
1668 /* Namespace-scopes are left most probably temporarily, not
1669 completely; they can be reopened later, e.g. in namespace-extension
1670 or any name binding activity that requires us to resume a
1671 namespace. For classes, we cache some binding levels. For other
1672 scopes, we just make the structure available for reuse. */
1673 if (scope->kind != sk_namespace
1674 && scope->kind != sk_class)
1676 scope->level_chain = free_binding_level;
1677 gcc_assert (!ENABLE_SCOPE_CHECKING
1678 || scope->binding_depth == binding_depth);
1679 free_binding_level = scope;
1682 if (scope->kind == sk_class)
1684 /* Reset DEFINING_CLASS_P to allow for reuse of a
1685 class-defining scope in a non-defining context. */
1686 scope->defining_class_p = 0;
1688 /* Find the innermost enclosing class scope, and reset
1689 CLASS_BINDING_LEVEL appropriately. */
1690 class_binding_level = NULL;
1691 for (scope = current_binding_level; scope; scope = scope->level_chain)
1692 if (scope->kind == sk_class)
1694 class_binding_level = scope;
1695 break;
1699 return current_binding_level;
1702 static void
1703 resume_scope (cp_binding_level* b)
1705 /* Resuming binding levels is meant only for namespaces,
1706 and those cannot nest into classes. */
1707 gcc_assert (!class_binding_level);
1708 /* Also, resuming a non-directly nested namespace is a no-no. */
1709 gcc_assert (b->level_chain == current_binding_level);
1710 current_binding_level = b;
1711 if (ENABLE_SCOPE_CHECKING)
1713 b->binding_depth = binding_depth;
1714 indent (binding_depth);
1715 cp_binding_level_debug (b, LOCATION_LINE (input_location), "resume");
1716 binding_depth++;
1720 /* Return the innermost binding level that is not for a class scope. */
1722 static cp_binding_level *
1723 innermost_nonclass_level (void)
1725 cp_binding_level *b;
1727 b = current_binding_level;
1728 while (b->kind == sk_class)
1729 b = b->level_chain;
1731 return b;
1734 /* We're defining an object of type TYPE. If it needs a cleanup, but
1735 we're not allowed to add any more objects with cleanups to the current
1736 scope, create a new binding level. */
1738 void
1739 maybe_push_cleanup_level (tree type)
1741 if (type != error_mark_node
1742 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
1743 && current_binding_level->more_cleanups_ok == 0)
1745 begin_scope (sk_cleanup, NULL);
1746 current_binding_level->statement_list = push_stmt_list ();
1750 /* Return true if we are in the global binding level. */
1752 bool
1753 global_bindings_p (void)
1755 return global_scope_p (current_binding_level);
1758 /* True if we are currently in a toplevel binding level. This
1759 means either the global binding level or a namespace in a toplevel
1760 binding level. Since there are no non-toplevel namespace levels,
1761 this really means any namespace or template parameter level. We
1762 also include a class whose context is toplevel. */
1764 bool
1765 toplevel_bindings_p (void)
1767 cp_binding_level *b = innermost_nonclass_level ();
1769 return b->kind == sk_namespace || b->kind == sk_template_parms;
1772 /* True if this is a namespace scope, or if we are defining a class
1773 which is itself at namespace scope, or whose enclosing class is
1774 such a class, etc. */
1776 bool
1777 namespace_bindings_p (void)
1779 cp_binding_level *b = innermost_nonclass_level ();
1781 return b->kind == sk_namespace;
1784 /* True if the innermost non-class scope is a block scope. */
1786 bool
1787 local_bindings_p (void)
1789 cp_binding_level *b = innermost_nonclass_level ();
1790 return b->kind < sk_function_parms || b->kind == sk_omp;
1793 /* True if the current level needs to have a BLOCK made. */
1795 bool
1796 kept_level_p (void)
1798 return (current_binding_level->blocks != NULL_TREE
1799 || current_binding_level->keep
1800 || current_binding_level->kind == sk_cleanup
1801 || current_binding_level->names != NULL_TREE
1802 || current_binding_level->using_directives);
1805 /* Returns the kind of the innermost scope. */
1807 scope_kind
1808 innermost_scope_kind (void)
1810 return current_binding_level->kind;
1813 /* Returns true if this scope was created to store template parameters. */
1815 bool
1816 template_parm_scope_p (void)
1818 return innermost_scope_kind () == sk_template_parms;
1821 /* If KEEP is true, make a BLOCK node for the next binding level,
1822 unconditionally. Otherwise, use the normal logic to decide whether
1823 or not to create a BLOCK. */
1825 void
1826 keep_next_level (bool keep)
1828 keep_next_level_flag = keep;
1831 /* Return the list of declarations of the current level.
1832 Note that this list is in reverse order unless/until
1833 you nreverse it; and when you do nreverse it, you must
1834 store the result back using `storedecls' or you will lose. */
1836 tree
1837 getdecls (void)
1839 return current_binding_level->names;
1842 /* Return how many function prototypes we are currently nested inside. */
1845 function_parm_depth (void)
1847 int level = 0;
1848 cp_binding_level *b;
1850 for (b = current_binding_level;
1851 b->kind == sk_function_parms;
1852 b = b->level_chain)
1853 ++level;
1855 return level;
1858 /* For debugging. */
1859 static int no_print_functions = 0;
1860 static int no_print_builtins = 0;
1862 static void
1863 print_binding_level (cp_binding_level* lvl)
1865 tree t;
1866 int i = 0, len;
1867 fprintf (stderr, " blocks=%p", (void *) lvl->blocks);
1868 if (lvl->more_cleanups_ok)
1869 fprintf (stderr, " more-cleanups-ok");
1870 if (lvl->have_cleanups)
1871 fprintf (stderr, " have-cleanups");
1872 fprintf (stderr, "\n");
1873 if (lvl->names)
1875 fprintf (stderr, " names:\t");
1876 /* We can probably fit 3 names to a line? */
1877 for (t = lvl->names; t; t = TREE_CHAIN (t))
1879 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
1880 continue;
1881 if (no_print_builtins
1882 && (TREE_CODE (t) == TYPE_DECL)
1883 && DECL_IS_BUILTIN (t))
1884 continue;
1886 /* Function decls tend to have longer names. */
1887 if (TREE_CODE (t) == FUNCTION_DECL)
1888 len = 3;
1889 else
1890 len = 2;
1891 i += len;
1892 if (i > 6)
1894 fprintf (stderr, "\n\t");
1895 i = len;
1897 print_node_brief (stderr, "", t, 0);
1898 if (t == error_mark_node)
1899 break;
1901 if (i)
1902 fprintf (stderr, "\n");
1904 if (vec_safe_length (lvl->class_shadowed))
1906 size_t i;
1907 cp_class_binding *b;
1908 fprintf (stderr, " class-shadowed:");
1909 FOR_EACH_VEC_ELT (*lvl->class_shadowed, i, b)
1910 fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
1911 fprintf (stderr, "\n");
1913 if (lvl->type_shadowed)
1915 fprintf (stderr, " type-shadowed:");
1916 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
1918 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1920 fprintf (stderr, "\n");
1924 DEBUG_FUNCTION void
1925 debug (cp_binding_level &ref)
1927 print_binding_level (&ref);
1930 DEBUG_FUNCTION void
1931 debug (cp_binding_level *ptr)
1933 if (ptr)
1934 debug (*ptr);
1935 else
1936 fprintf (stderr, "<nil>\n");
1940 void
1941 print_other_binding_stack (cp_binding_level *stack)
1943 cp_binding_level *level;
1944 for (level = stack; !global_scope_p (level); level = level->level_chain)
1946 fprintf (stderr, "binding level %p\n", (void *) level);
1947 print_binding_level (level);
1951 void
1952 print_binding_stack (void)
1954 cp_binding_level *b;
1955 fprintf (stderr, "current_binding_level=%p\n"
1956 "class_binding_level=%p\n"
1957 "NAMESPACE_LEVEL (global_namespace)=%p\n",
1958 (void *) current_binding_level, (void *) class_binding_level,
1959 (void *) NAMESPACE_LEVEL (global_namespace));
1960 if (class_binding_level)
1962 for (b = class_binding_level; b; b = b->level_chain)
1963 if (b == current_binding_level)
1964 break;
1965 if (b)
1966 b = class_binding_level;
1967 else
1968 b = current_binding_level;
1970 else
1971 b = current_binding_level;
1972 print_other_binding_stack (b);
1973 fprintf (stderr, "global:\n");
1974 print_binding_level (NAMESPACE_LEVEL (global_namespace));
1977 /* Return the type associated with ID. */
1979 static tree
1980 identifier_type_value_1 (tree id)
1982 /* There is no type with that name, anywhere. */
1983 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
1984 return NULL_TREE;
1985 /* This is not the type marker, but the real thing. */
1986 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
1987 return REAL_IDENTIFIER_TYPE_VALUE (id);
1988 /* Have to search for it. It must be on the global level, now.
1989 Ask lookup_name not to return non-types. */
1990 id = lookup_name_real (id, 2, 1, /*block_p=*/true, 0, 0);
1991 if (id)
1992 return TREE_TYPE (id);
1993 return NULL_TREE;
1996 /* Wrapper for identifier_type_value_1. */
1998 tree
1999 identifier_type_value (tree id)
2001 tree ret;
2002 timevar_start (TV_NAME_LOOKUP);
2003 ret = identifier_type_value_1 (id);
2004 timevar_stop (TV_NAME_LOOKUP);
2005 return ret;
2009 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
2010 the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */
2012 tree
2013 identifier_global_value (tree t)
2015 return IDENTIFIER_GLOBAL_VALUE (t);
2018 /* Push a definition of struct, union or enum tag named ID. into
2019 binding_level B. DECL is a TYPE_DECL for the type. We assume that
2020 the tag ID is not already defined. */
2022 static void
2023 set_identifier_type_value_with_scope (tree id, tree decl, cp_binding_level *b)
2025 tree type;
2027 if (b->kind != sk_namespace)
2029 /* Shadow the marker, not the real thing, so that the marker
2030 gets restored later. */
2031 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
2032 b->type_shadowed
2033 = tree_cons (id, old_type_value, b->type_shadowed);
2034 type = decl ? TREE_TYPE (decl) : NULL_TREE;
2035 TREE_TYPE (b->type_shadowed) = type;
2037 else
2039 cxx_binding *binding =
2040 binding_for_name (NAMESPACE_LEVEL (current_namespace), id);
2041 gcc_assert (decl);
2042 if (binding->value)
2043 supplement_binding (binding, decl);
2044 else
2045 binding->value = decl;
2047 /* Store marker instead of real type. */
2048 type = global_type_node;
2050 SET_IDENTIFIER_TYPE_VALUE (id, type);
2053 /* As set_identifier_type_value_with_scope, but using
2054 current_binding_level. */
2056 void
2057 set_identifier_type_value (tree id, tree decl)
2059 set_identifier_type_value_with_scope (id, decl, current_binding_level);
2062 /* Return the name for the constructor (or destructor) for the
2063 specified class TYPE. When given a template, this routine doesn't
2064 lose the specialization. */
2066 static inline tree
2067 constructor_name_full (tree type)
2069 return TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
2072 /* Return the name for the constructor (or destructor) for the
2073 specified class. When given a template, return the plain
2074 unspecialized name. */
2076 tree
2077 constructor_name (tree type)
2079 tree name;
2080 name = constructor_name_full (type);
2081 if (IDENTIFIER_TEMPLATE (name))
2082 name = IDENTIFIER_TEMPLATE (name);
2083 return name;
2086 /* Returns TRUE if NAME is the name for the constructor for TYPE,
2087 which must be a class type. */
2089 bool
2090 constructor_name_p (tree name, tree type)
2092 tree ctor_name;
2094 gcc_assert (MAYBE_CLASS_TYPE_P (type));
2096 if (!name)
2097 return false;
2099 if (!identifier_p (name))
2100 return false;
2102 /* These don't have names. */
2103 if (TREE_CODE (type) == DECLTYPE_TYPE
2104 || TREE_CODE (type) == TYPEOF_TYPE)
2105 return false;
2107 ctor_name = constructor_name_full (type);
2108 if (name == ctor_name)
2109 return true;
2110 if (IDENTIFIER_TEMPLATE (ctor_name)
2111 && name == IDENTIFIER_TEMPLATE (ctor_name))
2112 return true;
2113 return false;
2116 /* Counter used to create anonymous type names. */
2118 static GTY(()) int anon_cnt;
2120 /* Return an IDENTIFIER which can be used as a name for
2121 unnamed structs and unions. */
2123 tree
2124 make_anon_name (void)
2126 char buf[32];
2128 sprintf (buf, anon_aggrname_format (), anon_cnt++);
2129 return get_identifier (buf);
2132 /* This code is practically identical to that for creating
2133 anonymous names, but is just used for lambdas instead. This isn't really
2134 necessary, but it's convenient to avoid treating lambdas like other
2135 unnamed types. */
2137 static GTY(()) int lambda_cnt = 0;
2139 tree
2140 make_lambda_name (void)
2142 char buf[32];
2144 sprintf (buf, LAMBDANAME_FORMAT, lambda_cnt++);
2145 return get_identifier (buf);
2148 /* Return (from the stack of) the BINDING, if any, established at SCOPE. */
2150 static inline cxx_binding *
2151 find_binding (cp_binding_level *scope, cxx_binding *binding)
2153 for (; binding != NULL; binding = binding->previous)
2154 if (binding->scope == scope)
2155 return binding;
2157 return (cxx_binding *)0;
2160 /* Return the binding for NAME in SCOPE, if any. Otherwise, return NULL. */
2162 static inline cxx_binding *
2163 cp_binding_level_find_binding_for_name (cp_binding_level *scope, tree name)
2165 cxx_binding *b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2166 if (b)
2168 /* Fold-in case where NAME is used only once. */
2169 if (scope == b->scope && b->previous == NULL)
2170 return b;
2171 return find_binding (scope, b);
2173 return NULL;
2176 /* Always returns a binding for name in scope. If no binding is
2177 found, make a new one. */
2179 static cxx_binding *
2180 binding_for_name (cp_binding_level *scope, tree name)
2182 cxx_binding *result;
2184 result = cp_binding_level_find_binding_for_name (scope, name);
2185 if (result)
2186 return result;
2187 /* Not found, make a new one. */
2188 result = cxx_binding_make (NULL, NULL);
2189 result->previous = IDENTIFIER_NAMESPACE_BINDINGS (name);
2190 result->scope = scope;
2191 result->is_local = false;
2192 result->value_is_inherited = false;
2193 IDENTIFIER_NAMESPACE_BINDINGS (name) = result;
2194 return result;
2197 /* Walk through the bindings associated to the name of FUNCTION,
2198 and return the first declaration of a function with a
2199 "C" linkage specification, a.k.a 'extern "C"'.
2200 This function looks for the binding, regardless of which scope it
2201 has been defined in. It basically looks in all the known scopes.
2202 Note that this function does not lookup for bindings of builtin functions
2203 or for functions declared in system headers. */
2204 static tree
2205 lookup_extern_c_fun_in_all_ns (tree function)
2207 tree name;
2208 cxx_binding *iter;
2210 gcc_assert (function && TREE_CODE (function) == FUNCTION_DECL);
2212 name = DECL_NAME (function);
2213 gcc_assert (name && identifier_p (name));
2215 for (iter = IDENTIFIER_NAMESPACE_BINDINGS (name);
2216 iter;
2217 iter = iter->previous)
2219 tree ovl;
2220 for (ovl = iter->value; ovl; ovl = OVL_NEXT (ovl))
2222 tree decl = OVL_CURRENT (ovl);
2223 if (decl
2224 && TREE_CODE (decl) == FUNCTION_DECL
2225 && DECL_EXTERN_C_P (decl)
2226 && !DECL_ARTIFICIAL (decl))
2228 return decl;
2232 return NULL;
2235 /* Returns a list of C-linkage decls with the name NAME. */
2237 tree
2238 c_linkage_bindings (tree name)
2240 tree decls = NULL_TREE;
2241 cxx_binding *iter;
2243 for (iter = IDENTIFIER_NAMESPACE_BINDINGS (name);
2244 iter;
2245 iter = iter->previous)
2247 tree ovl;
2248 for (ovl = iter->value; ovl; ovl = OVL_NEXT (ovl))
2250 tree decl = OVL_CURRENT (ovl);
2251 if (decl
2252 && DECL_EXTERN_C_P (decl)
2253 && !DECL_ARTIFICIAL (decl))
2255 if (decls == NULL_TREE)
2256 decls = decl;
2257 else
2258 decls = tree_cons (NULL_TREE, decl, decls);
2262 return decls;
2265 /* Insert another USING_DECL into the current binding level, returning
2266 this declaration. If this is a redeclaration, do nothing, and
2267 return NULL_TREE if this not in namespace scope (in namespace
2268 scope, a using decl might extend any previous bindings). */
2270 static tree
2271 push_using_decl_1 (tree scope, tree name)
2273 tree decl;
2275 gcc_assert (TREE_CODE (scope) == NAMESPACE_DECL);
2276 gcc_assert (identifier_p (name));
2277 for (decl = current_binding_level->usings; decl; decl = DECL_CHAIN (decl))
2278 if (USING_DECL_SCOPE (decl) == scope && DECL_NAME (decl) == name)
2279 break;
2280 if (decl)
2281 return namespace_bindings_p () ? decl : NULL_TREE;
2282 decl = build_lang_decl (USING_DECL, name, NULL_TREE);
2283 USING_DECL_SCOPE (decl) = scope;
2284 DECL_CHAIN (decl) = current_binding_level->usings;
2285 current_binding_level->usings = decl;
2286 return decl;
2289 /* Wrapper for push_using_decl_1. */
2291 static tree
2292 push_using_decl (tree scope, tree name)
2294 tree ret;
2295 timevar_start (TV_NAME_LOOKUP);
2296 ret = push_using_decl_1 (scope, name);
2297 timevar_stop (TV_NAME_LOOKUP);
2298 return ret;
2301 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
2302 caller to set DECL_CONTEXT properly.
2304 Note that this must only be used when X will be the new innermost
2305 binding for its name, as we tack it onto the front of IDENTIFIER_BINDING
2306 without checking to see if the current IDENTIFIER_BINDING comes from a
2307 closer binding level than LEVEL. */
2309 static tree
2310 pushdecl_with_scope_1 (tree x, cp_binding_level *level, bool is_friend)
2312 cp_binding_level *b;
2313 tree function_decl = current_function_decl;
2315 current_function_decl = NULL_TREE;
2316 if (level->kind == sk_class)
2318 b = class_binding_level;
2319 class_binding_level = level;
2320 pushdecl_class_level (x);
2321 class_binding_level = b;
2323 else
2325 b = current_binding_level;
2326 current_binding_level = level;
2327 x = pushdecl_maybe_friend (x, is_friend);
2328 current_binding_level = b;
2330 current_function_decl = function_decl;
2331 return x;
2334 /* Wrapper for pushdecl_with_scope_1. */
2336 tree
2337 pushdecl_with_scope (tree x, cp_binding_level *level, bool is_friend)
2339 tree ret;
2340 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2341 ret = pushdecl_with_scope_1 (x, level, is_friend);
2342 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2343 return ret;
2346 /* Helper function for push_overloaded_decl_1 and do_nonmember_using_decl.
2347 Compares the parameter-type-lists of DECL1 and DECL2 and returns false
2348 if they are different. If the DECLs are template functions, the return
2349 types and the template parameter lists are compared too (DR 565). */
2351 static bool
2352 compparms_for_decl_and_using_decl (tree decl1, tree decl2)
2354 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (decl1)),
2355 TYPE_ARG_TYPES (TREE_TYPE (decl2))))
2356 return false;
2358 if (! DECL_FUNCTION_TEMPLATE_P (decl1)
2359 || ! DECL_FUNCTION_TEMPLATE_P (decl2))
2360 return true;
2362 return (comp_template_parms (DECL_TEMPLATE_PARMS (decl1),
2363 DECL_TEMPLATE_PARMS (decl2))
2364 && same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
2365 TREE_TYPE (TREE_TYPE (decl2))));
2368 /* DECL is a FUNCTION_DECL for a non-member function, which may have
2369 other definitions already in place. We get around this by making
2370 the value of the identifier point to a list of all the things that
2371 want to be referenced by that name. It is then up to the users of
2372 that name to decide what to do with that list.
2374 DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its
2375 DECL_TEMPLATE_RESULT. It is dealt with the same way.
2377 FLAGS is a bitwise-or of the following values:
2378 PUSH_LOCAL: Bind DECL in the current scope, rather than at
2379 namespace scope.
2380 PUSH_USING: DECL is being pushed as the result of a using
2381 declaration.
2383 IS_FRIEND is true if this is a friend declaration.
2385 The value returned may be a previous declaration if we guessed wrong
2386 about what language DECL should belong to (C or C++). Otherwise,
2387 it's always DECL (and never something that's not a _DECL). */
2389 static tree
2390 push_overloaded_decl_1 (tree decl, int flags, bool is_friend)
2392 tree name = DECL_NAME (decl);
2393 tree old;
2394 tree new_binding;
2395 int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
2397 if (doing_global)
2398 old = namespace_binding (name, DECL_CONTEXT (decl));
2399 else
2400 old = lookup_name_innermost_nonclass_level (name);
2402 if (old)
2404 if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
2406 tree t = TREE_TYPE (old);
2407 if (MAYBE_CLASS_TYPE_P (t) && warn_shadow
2408 && (! DECL_IN_SYSTEM_HEADER (decl)
2409 || ! DECL_IN_SYSTEM_HEADER (old)))
2410 warning (OPT_Wshadow, "%q#D hides constructor for %q#T", decl, t);
2411 old = NULL_TREE;
2413 else if (is_overloaded_fn (old))
2415 tree tmp;
2417 for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
2419 tree fn = OVL_CURRENT (tmp);
2420 tree dup;
2422 if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp)
2423 && !(flags & PUSH_USING)
2424 && compparms_for_decl_and_using_decl (fn, decl)
2425 && ! decls_match (fn, decl))
2426 diagnose_name_conflict (decl, fn);
2428 dup = duplicate_decls (decl, fn, is_friend);
2429 /* If DECL was a redeclaration of FN -- even an invalid
2430 one -- pass that information along to our caller. */
2431 if (dup == fn || dup == error_mark_node)
2432 return dup;
2435 /* We don't overload implicit built-ins. duplicate_decls()
2436 may fail to merge the decls if the new decl is e.g. a
2437 template function. */
2438 if (TREE_CODE (old) == FUNCTION_DECL
2439 && DECL_ANTICIPATED (old)
2440 && !DECL_HIDDEN_FRIEND_P (old))
2441 old = NULL;
2443 else if (old == error_mark_node)
2444 /* Ignore the undefined symbol marker. */
2445 old = NULL_TREE;
2446 else
2448 error ("previous non-function declaration %q+#D", old);
2449 error ("conflicts with function declaration %q#D", decl);
2450 return decl;
2454 if (old || TREE_CODE (decl) == TEMPLATE_DECL
2455 /* If it's a using declaration, we always need to build an OVERLOAD,
2456 because it's the only way to remember that the declaration comes
2457 from 'using', and have the lookup behave correctly. */
2458 || (flags & PUSH_USING))
2460 if (old && TREE_CODE (old) != OVERLOAD)
2461 /* Wrap the existing single decl in an overload. */
2462 new_binding = ovl_cons (old, NULL_TREE);
2463 else
2464 new_binding = old;
2465 new_binding = ovl_cons (decl, new_binding);
2466 if (flags & PUSH_USING)
2467 OVL_USED (new_binding) = 1;
2469 else
2470 /* NAME is not ambiguous. */
2471 new_binding = decl;
2473 if (doing_global)
2474 set_namespace_binding (name, current_namespace, new_binding);
2475 else
2477 /* We only create an OVERLOAD if there was a previous binding at
2478 this level, or if decl is a template. In the former case, we
2479 need to remove the old binding and replace it with the new
2480 binding. We must also run through the NAMES on the binding
2481 level where the name was bound to update the chain. */
2483 if (TREE_CODE (new_binding) == OVERLOAD && old)
2485 tree *d;
2487 for (d = &IDENTIFIER_BINDING (name)->scope->names;
2489 d = &TREE_CHAIN (*d))
2490 if (*d == old
2491 || (TREE_CODE (*d) == TREE_LIST
2492 && TREE_VALUE (*d) == old))
2494 if (TREE_CODE (*d) == TREE_LIST)
2495 /* Just replace the old binding with the new. */
2496 TREE_VALUE (*d) = new_binding;
2497 else
2498 /* Build a TREE_LIST to wrap the OVERLOAD. */
2499 *d = tree_cons (NULL_TREE, new_binding,
2500 TREE_CHAIN (*d));
2502 /* And update the cxx_binding node. */
2503 IDENTIFIER_BINDING (name)->value = new_binding;
2504 return decl;
2507 /* We should always find a previous binding in this case. */
2508 gcc_unreachable ();
2511 /* Install the new binding. */
2512 push_local_binding (name, new_binding, flags);
2515 return decl;
2518 /* Wrapper for push_overloaded_decl_1. */
2520 static tree
2521 push_overloaded_decl (tree decl, int flags, bool is_friend)
2523 tree ret;
2524 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2525 ret = push_overloaded_decl_1 (decl, flags, is_friend);
2526 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2527 return ret;
2530 /* Check a non-member using-declaration. Return the name and scope
2531 being used, and the USING_DECL, or NULL_TREE on failure. */
2533 static tree
2534 validate_nonmember_using_decl (tree decl, tree scope, tree name)
2536 /* [namespace.udecl]
2537 A using-declaration for a class member shall be a
2538 member-declaration. */
2539 if (TYPE_P (scope))
2541 error ("%qT is not a namespace or unscoped enum", scope);
2542 return NULL_TREE;
2544 else if (scope == error_mark_node)
2545 return NULL_TREE;
2547 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR)
2549 /* 7.3.3/5
2550 A using-declaration shall not name a template-id. */
2551 error ("a using-declaration cannot specify a template-id. "
2552 "Try %<using %D%>", name);
2553 return NULL_TREE;
2556 if (TREE_CODE (decl) == NAMESPACE_DECL)
2558 error ("namespace %qD not allowed in using-declaration", decl);
2559 return NULL_TREE;
2562 if (TREE_CODE (decl) == SCOPE_REF)
2564 /* It's a nested name with template parameter dependent scope.
2565 This can only be using-declaration for class member. */
2566 error ("%qT is not a namespace", TREE_OPERAND (decl, 0));
2567 return NULL_TREE;
2570 if (is_overloaded_fn (decl))
2571 decl = get_first_fn (decl);
2573 gcc_assert (DECL_P (decl));
2575 /* Make a USING_DECL. */
2576 tree using_decl = push_using_decl (scope, name);
2578 if (using_decl == NULL_TREE
2579 && at_function_scope_p ()
2580 && VAR_P (decl))
2581 /* C++11 7.3.3/10. */
2582 error ("%qD is already declared in this scope", name);
2584 return using_decl;
2587 /* Process local and global using-declarations. */
2589 static void
2590 do_nonmember_using_decl (tree scope, tree name, tree oldval, tree oldtype,
2591 tree *newval, tree *newtype)
2593 struct scope_binding decls = EMPTY_SCOPE_BINDING;
2595 *newval = *newtype = NULL_TREE;
2596 if (!qualified_lookup_using_namespace (name, scope, &decls, 0))
2597 /* Lookup error */
2598 return;
2600 if (!decls.value && !decls.type)
2602 error ("%qD not declared", name);
2603 return;
2606 /* Shift the old and new bindings around so we're comparing class and
2607 enumeration names to each other. */
2608 if (oldval && DECL_IMPLICIT_TYPEDEF_P (oldval))
2610 oldtype = oldval;
2611 oldval = NULL_TREE;
2614 if (decls.value && DECL_IMPLICIT_TYPEDEF_P (decls.value))
2616 decls.type = decls.value;
2617 decls.value = NULL_TREE;
2620 if (decls.value)
2622 /* Check for using functions. */
2623 if (is_overloaded_fn (decls.value))
2625 tree tmp, tmp1;
2627 if (oldval && !is_overloaded_fn (oldval))
2629 error ("%qD is already declared in this scope", name);
2630 oldval = NULL_TREE;
2633 *newval = oldval;
2634 for (tmp = decls.value; tmp; tmp = OVL_NEXT (tmp))
2636 tree new_fn = OVL_CURRENT (tmp);
2638 /* Don't import functions that haven't been declared. */
2639 if (DECL_ANTICIPATED (new_fn))
2640 continue;
2642 /* [namespace.udecl]
2644 If a function declaration in namespace scope or block
2645 scope has the same name and the same parameter types as a
2646 function introduced by a using declaration the program is
2647 ill-formed. */
2648 for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1))
2650 tree old_fn = OVL_CURRENT (tmp1);
2652 if (new_fn == old_fn)
2653 /* The function already exists in the current namespace. */
2654 break;
2655 else if (TREE_CODE (tmp1) == OVERLOAD && OVL_USED (tmp1))
2656 continue; /* this is a using decl */
2657 else if (compparms_for_decl_and_using_decl (new_fn, old_fn))
2659 /* There was already a non-using declaration in
2660 this scope with the same parameter types. If both
2661 are the same extern "C" functions, that's ok. */
2662 if (DECL_ANTICIPATED (old_fn)
2663 && !DECL_HIDDEN_FRIEND_P (old_fn))
2664 /* Ignore anticipated built-ins. */;
2665 else if (decls_match (new_fn, old_fn))
2666 break;
2667 else
2669 diagnose_name_conflict (new_fn, old_fn);
2670 break;
2675 /* If we broke out of the loop, there's no reason to add
2676 this function to the using declarations for this
2677 scope. */
2678 if (tmp1)
2679 continue;
2681 /* If we are adding to an existing OVERLOAD, then we no
2682 longer know the type of the set of functions. */
2683 if (*newval && TREE_CODE (*newval) == OVERLOAD)
2684 TREE_TYPE (*newval) = unknown_type_node;
2685 /* Add this new function to the set. */
2686 *newval = build_overload (OVL_CURRENT (tmp), *newval);
2687 /* If there is only one function, then we use its type. (A
2688 using-declaration naming a single function can be used in
2689 contexts where overload resolution cannot be
2690 performed.) */
2691 if (TREE_CODE (*newval) != OVERLOAD)
2693 *newval = ovl_cons (*newval, NULL_TREE);
2694 TREE_TYPE (*newval) = TREE_TYPE (OVL_CURRENT (tmp));
2696 OVL_USED (*newval) = 1;
2699 else
2701 /* If we're declaring a non-function and OLDVAL is an anticipated
2702 built-in, just pretend it isn't there. */
2703 if (oldval
2704 && TREE_CODE (oldval) == FUNCTION_DECL
2705 && DECL_ANTICIPATED (oldval)
2706 && !DECL_HIDDEN_FRIEND_P (oldval))
2707 oldval = NULL_TREE;
2709 *newval = decls.value;
2710 if (oldval && !decls_match (*newval, oldval))
2711 error ("%qD is already declared in this scope", name);
2714 else
2715 *newval = oldval;
2717 if (decls.type && TREE_CODE (decls.type) == TREE_LIST)
2719 error ("reference to %qD is ambiguous", name);
2720 print_candidates (decls.type);
2722 else
2724 *newtype = decls.type;
2725 if (oldtype && *newtype && !decls_match (oldtype, *newtype))
2726 error ("%qD is already declared in this scope", name);
2729 /* If *newval is empty, shift any class or enumeration name down. */
2730 if (!*newval)
2732 *newval = *newtype;
2733 *newtype = NULL_TREE;
2737 /* Process a using-declaration at function scope. */
2739 void
2740 do_local_using_decl (tree decl, tree scope, tree name)
2742 tree oldval, oldtype, newval, newtype;
2743 tree orig_decl = decl;
2745 decl = validate_nonmember_using_decl (decl, scope, name);
2746 if (decl == NULL_TREE)
2747 return;
2749 if (building_stmt_list_p ()
2750 && at_function_scope_p ())
2751 add_decl_expr (decl);
2753 oldval = lookup_name_innermost_nonclass_level (name);
2754 oldtype = lookup_type_current_level (name);
2756 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
2758 if (newval)
2760 if (is_overloaded_fn (newval))
2762 tree fn, term;
2764 /* We only need to push declarations for those functions
2765 that were not already bound in the current level.
2766 The old value might be NULL_TREE, it might be a single
2767 function, or an OVERLOAD. */
2768 if (oldval && TREE_CODE (oldval) == OVERLOAD)
2769 term = OVL_FUNCTION (oldval);
2770 else
2771 term = oldval;
2772 for (fn = newval; fn && OVL_CURRENT (fn) != term;
2773 fn = OVL_NEXT (fn))
2774 push_overloaded_decl (OVL_CURRENT (fn),
2775 PUSH_LOCAL | PUSH_USING,
2776 false);
2778 else
2779 push_local_binding (name, newval, PUSH_USING);
2781 if (newtype)
2783 push_local_binding (name, newtype, PUSH_USING);
2784 set_identifier_type_value (name, newtype);
2787 /* Emit debug info. */
2788 if (!processing_template_decl)
2789 cp_emit_debug_info_for_using (orig_decl, current_scope());
2792 /* Returns true if ROOT (a namespace, class, or function) encloses
2793 CHILD. CHILD may be either a class type or a namespace. */
2795 bool
2796 is_ancestor (tree root, tree child)
2798 gcc_assert ((TREE_CODE (root) == NAMESPACE_DECL
2799 || TREE_CODE (root) == FUNCTION_DECL
2800 || CLASS_TYPE_P (root)));
2801 gcc_assert ((TREE_CODE (child) == NAMESPACE_DECL
2802 || CLASS_TYPE_P (child)));
2804 /* The global namespace encloses everything. */
2805 if (root == global_namespace)
2806 return true;
2808 while (true)
2810 /* If we've run out of scopes, stop. */
2811 if (!child)
2812 return false;
2813 /* If we've reached the ROOT, it encloses CHILD. */
2814 if (root == child)
2815 return true;
2816 /* Go out one level. */
2817 if (TYPE_P (child))
2818 child = TYPE_NAME (child);
2819 child = DECL_CONTEXT (child);
2823 /* Enter the class or namespace scope indicated by T suitable for name
2824 lookup. T can be arbitrary scope, not necessary nested inside the
2825 current scope. Returns a non-null scope to pop iff pop_scope
2826 should be called later to exit this scope. */
2828 tree
2829 push_scope (tree t)
2831 if (TREE_CODE (t) == NAMESPACE_DECL)
2832 push_decl_namespace (t);
2833 else if (CLASS_TYPE_P (t))
2835 if (!at_class_scope_p ()
2836 || !same_type_p (current_class_type, t))
2837 push_nested_class (t);
2838 else
2839 /* T is the same as the current scope. There is therefore no
2840 need to re-enter the scope. Since we are not actually
2841 pushing a new scope, our caller should not call
2842 pop_scope. */
2843 t = NULL_TREE;
2846 return t;
2849 /* Leave scope pushed by push_scope. */
2851 void
2852 pop_scope (tree t)
2854 if (t == NULL_TREE)
2855 return;
2856 if (TREE_CODE (t) == NAMESPACE_DECL)
2857 pop_decl_namespace ();
2858 else if CLASS_TYPE_P (t)
2859 pop_nested_class ();
2862 /* Subroutine of push_inner_scope. */
2864 static void
2865 push_inner_scope_r (tree outer, tree inner)
2867 tree prev;
2869 if (outer == inner
2870 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
2871 return;
2873 prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
2874 if (outer != prev)
2875 push_inner_scope_r (outer, prev);
2876 if (TREE_CODE (inner) == NAMESPACE_DECL)
2878 cp_binding_level *save_template_parm = 0;
2879 /* Temporary take out template parameter scopes. They are saved
2880 in reversed order in save_template_parm. */
2881 while (current_binding_level->kind == sk_template_parms)
2883 cp_binding_level *b = current_binding_level;
2884 current_binding_level = b->level_chain;
2885 b->level_chain = save_template_parm;
2886 save_template_parm = b;
2889 resume_scope (NAMESPACE_LEVEL (inner));
2890 current_namespace = inner;
2892 /* Restore template parameter scopes. */
2893 while (save_template_parm)
2895 cp_binding_level *b = save_template_parm;
2896 save_template_parm = b->level_chain;
2897 b->level_chain = current_binding_level;
2898 current_binding_level = b;
2901 else
2902 pushclass (inner);
2905 /* Enter the scope INNER from current scope. INNER must be a scope
2906 nested inside current scope. This works with both name lookup and
2907 pushing name into scope. In case a template parameter scope is present,
2908 namespace is pushed under the template parameter scope according to
2909 name lookup rule in 14.6.1/6.
2911 Return the former current scope suitable for pop_inner_scope. */
2913 tree
2914 push_inner_scope (tree inner)
2916 tree outer = current_scope ();
2917 if (!outer)
2918 outer = current_namespace;
2920 push_inner_scope_r (outer, inner);
2921 return outer;
2924 /* Exit the current scope INNER back to scope OUTER. */
2926 void
2927 pop_inner_scope (tree outer, tree inner)
2929 if (outer == inner
2930 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
2931 return;
2933 while (outer != inner)
2935 if (TREE_CODE (inner) == NAMESPACE_DECL)
2937 cp_binding_level *save_template_parm = 0;
2938 /* Temporary take out template parameter scopes. They are saved
2939 in reversed order in save_template_parm. */
2940 while (current_binding_level->kind == sk_template_parms)
2942 cp_binding_level *b = current_binding_level;
2943 current_binding_level = b->level_chain;
2944 b->level_chain = save_template_parm;
2945 save_template_parm = b;
2948 pop_namespace ();
2950 /* Restore template parameter scopes. */
2951 while (save_template_parm)
2953 cp_binding_level *b = save_template_parm;
2954 save_template_parm = b->level_chain;
2955 b->level_chain = current_binding_level;
2956 current_binding_level = b;
2959 else
2960 popclass ();
2962 inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
2966 /* Do a pushlevel for class declarations. */
2968 void
2969 pushlevel_class (void)
2971 class_binding_level = begin_scope (sk_class, current_class_type);
2974 /* ...and a poplevel for class declarations. */
2976 void
2977 poplevel_class (void)
2979 cp_binding_level *level = class_binding_level;
2980 cp_class_binding *cb;
2981 size_t i;
2982 tree shadowed;
2984 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2985 gcc_assert (level != 0);
2987 /* If we're leaving a toplevel class, cache its binding level. */
2988 if (current_class_depth == 1)
2989 previous_class_level = level;
2990 for (shadowed = level->type_shadowed;
2991 shadowed;
2992 shadowed = TREE_CHAIN (shadowed))
2993 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
2995 /* Remove the bindings for all of the class-level declarations. */
2996 if (level->class_shadowed)
2998 FOR_EACH_VEC_ELT (*level->class_shadowed, i, cb)
3000 IDENTIFIER_BINDING (cb->identifier) = cb->base->previous;
3001 cxx_binding_free (cb->base);
3003 ggc_free (level->class_shadowed);
3004 level->class_shadowed = NULL;
3007 /* Now, pop out of the binding level which we created up in the
3008 `pushlevel_class' routine. */
3009 gcc_assert (current_binding_level == level);
3010 leave_scope ();
3011 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3014 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
3015 appropriate. DECL is the value to which a name has just been
3016 bound. CLASS_TYPE is the class in which the lookup occurred. */
3018 static void
3019 set_inherited_value_binding_p (cxx_binding *binding, tree decl,
3020 tree class_type)
3022 if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
3024 tree context;
3026 if (TREE_CODE (decl) == OVERLOAD)
3027 context = ovl_scope (decl);
3028 else
3030 gcc_assert (DECL_P (decl));
3031 context = context_for_name_lookup (decl);
3034 if (is_properly_derived_from (class_type, context))
3035 INHERITED_VALUE_BINDING_P (binding) = 1;
3036 else
3037 INHERITED_VALUE_BINDING_P (binding) = 0;
3039 else if (binding->value == decl)
3040 /* We only encounter a TREE_LIST when there is an ambiguity in the
3041 base classes. Such an ambiguity can be overridden by a
3042 definition in this class. */
3043 INHERITED_VALUE_BINDING_P (binding) = 1;
3044 else
3045 INHERITED_VALUE_BINDING_P (binding) = 0;
3048 /* Make the declaration of X appear in CLASS scope. */
3050 bool
3051 pushdecl_class_level (tree x)
3053 tree name;
3054 bool is_valid = true;
3055 bool subtime;
3057 /* Do nothing if we're adding to an outer lambda closure type,
3058 outer_binding will add it later if it's needed. */
3059 if (current_class_type != class_binding_level->this_entity)
3060 return true;
3062 subtime = timevar_cond_start (TV_NAME_LOOKUP);
3063 /* Get the name of X. */
3064 if (TREE_CODE (x) == OVERLOAD)
3065 name = DECL_NAME (get_first_fn (x));
3066 else
3067 name = DECL_NAME (x);
3069 if (name)
3071 is_valid = push_class_level_binding (name, x);
3072 if (TREE_CODE (x) == TYPE_DECL)
3073 set_identifier_type_value (name, x);
3075 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3077 /* If X is an anonymous aggregate, all of its members are
3078 treated as if they were members of the class containing the
3079 aggregate, for naming purposes. */
3080 tree f;
3082 for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = DECL_CHAIN (f))
3084 location_t save_location = input_location;
3085 input_location = DECL_SOURCE_LOCATION (f);
3086 if (!pushdecl_class_level (f))
3087 is_valid = false;
3088 input_location = save_location;
3091 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3092 return is_valid;
3095 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
3096 scope. If the value returned is non-NULL, and the PREVIOUS field
3097 is not set, callers must set the PREVIOUS field explicitly. */
3099 static cxx_binding *
3100 get_class_binding (tree name, cp_binding_level *scope)
3102 tree class_type;
3103 tree type_binding;
3104 tree value_binding;
3105 cxx_binding *binding;
3107 class_type = scope->this_entity;
3109 /* Get the type binding. */
3110 type_binding = lookup_member (class_type, name,
3111 /*protect=*/2, /*want_type=*/true,
3112 tf_warning_or_error);
3113 /* Get the value binding. */
3114 value_binding = lookup_member (class_type, name,
3115 /*protect=*/2, /*want_type=*/false,
3116 tf_warning_or_error);
3118 if (value_binding
3119 && (TREE_CODE (value_binding) == TYPE_DECL
3120 || DECL_CLASS_TEMPLATE_P (value_binding)
3121 || (TREE_CODE (value_binding) == TREE_LIST
3122 && TREE_TYPE (value_binding) == error_mark_node
3123 && (TREE_CODE (TREE_VALUE (value_binding))
3124 == TYPE_DECL))))
3125 /* We found a type binding, even when looking for a non-type
3126 binding. This means that we already processed this binding
3127 above. */
3129 else if (value_binding)
3131 if (TREE_CODE (value_binding) == TREE_LIST
3132 && TREE_TYPE (value_binding) == error_mark_node)
3133 /* NAME is ambiguous. */
3135 else if (BASELINK_P (value_binding))
3136 /* NAME is some overloaded functions. */
3137 value_binding = BASELINK_FUNCTIONS (value_binding);
3140 /* If we found either a type binding or a value binding, create a
3141 new binding object. */
3142 if (type_binding || value_binding)
3144 binding = new_class_binding (name,
3145 value_binding,
3146 type_binding,
3147 scope);
3148 /* This is a class-scope binding, not a block-scope binding. */
3149 LOCAL_BINDING_P (binding) = 0;
3150 set_inherited_value_binding_p (binding, value_binding, class_type);
3152 else
3153 binding = NULL;
3155 return binding;
3158 /* Make the declaration(s) of X appear in CLASS scope under the name
3159 NAME. Returns true if the binding is valid. */
3161 static bool
3162 push_class_level_binding_1 (tree name, tree x)
3164 cxx_binding *binding;
3165 tree decl = x;
3166 bool ok;
3168 /* The class_binding_level will be NULL if x is a template
3169 parameter name in a member template. */
3170 if (!class_binding_level)
3171 return true;
3173 if (name == error_mark_node)
3174 return false;
3176 /* Can happen for an erroneous declaration (c++/60384). */
3177 if (!identifier_p (name))
3179 gcc_assert (errorcount || sorrycount);
3180 return false;
3183 /* Check for invalid member names. But don't worry about a default
3184 argument-scope lambda being pushed after the class is complete. */
3185 gcc_assert (TYPE_BEING_DEFINED (current_class_type)
3186 || LAMBDA_TYPE_P (TREE_TYPE (decl)));
3187 /* Check that we're pushing into the right binding level. */
3188 gcc_assert (current_class_type == class_binding_level->this_entity);
3190 /* We could have been passed a tree list if this is an ambiguous
3191 declaration. If so, pull the declaration out because
3192 check_template_shadow will not handle a TREE_LIST. */
3193 if (TREE_CODE (decl) == TREE_LIST
3194 && TREE_TYPE (decl) == error_mark_node)
3195 decl = TREE_VALUE (decl);
3197 if (!check_template_shadow (decl))
3198 return false;
3200 /* [class.mem]
3202 If T is the name of a class, then each of the following shall
3203 have a name different from T:
3205 -- every static data member of class T;
3207 -- every member of class T that is itself a type;
3209 -- every enumerator of every member of class T that is an
3210 enumerated type;
3212 -- every member of every anonymous union that is a member of
3213 class T.
3215 (Non-static data members were also forbidden to have the same
3216 name as T until TC1.) */
3217 if ((VAR_P (x)
3218 || TREE_CODE (x) == CONST_DECL
3219 || (TREE_CODE (x) == TYPE_DECL
3220 && !DECL_SELF_REFERENCE_P (x))
3221 /* A data member of an anonymous union. */
3222 || (TREE_CODE (x) == FIELD_DECL
3223 && DECL_CONTEXT (x) != current_class_type))
3224 && DECL_NAME (x) == constructor_name (current_class_type))
3226 tree scope = context_for_name_lookup (x);
3227 if (TYPE_P (scope) && same_type_p (scope, current_class_type))
3229 error ("%qD has the same name as the class in which it is "
3230 "declared",
3232 return false;
3236 /* Get the current binding for NAME in this class, if any. */
3237 binding = IDENTIFIER_BINDING (name);
3238 if (!binding || binding->scope != class_binding_level)
3240 binding = get_class_binding (name, class_binding_level);
3241 /* If a new binding was created, put it at the front of the
3242 IDENTIFIER_BINDING list. */
3243 if (binding)
3245 binding->previous = IDENTIFIER_BINDING (name);
3246 IDENTIFIER_BINDING (name) = binding;
3250 /* If there is already a binding, then we may need to update the
3251 current value. */
3252 if (binding && binding->value)
3254 tree bval = binding->value;
3255 tree old_decl = NULL_TREE;
3256 tree target_decl = strip_using_decl (decl);
3257 tree target_bval = strip_using_decl (bval);
3259 if (INHERITED_VALUE_BINDING_P (binding))
3261 /* If the old binding was from a base class, and was for a
3262 tag name, slide it over to make room for the new binding.
3263 The old binding is still visible if explicitly qualified
3264 with a class-key. */
3265 if (TREE_CODE (target_bval) == TYPE_DECL
3266 && DECL_ARTIFICIAL (target_bval)
3267 && !(TREE_CODE (target_decl) == TYPE_DECL
3268 && DECL_ARTIFICIAL (target_decl)))
3270 old_decl = binding->type;
3271 binding->type = bval;
3272 binding->value = NULL_TREE;
3273 INHERITED_VALUE_BINDING_P (binding) = 0;
3275 else
3277 old_decl = bval;
3278 /* Any inherited type declaration is hidden by the type
3279 declaration in the derived class. */
3280 if (TREE_CODE (target_decl) == TYPE_DECL
3281 && DECL_ARTIFICIAL (target_decl))
3282 binding->type = NULL_TREE;
3285 else if (TREE_CODE (target_decl) == OVERLOAD
3286 && is_overloaded_fn (target_bval))
3287 old_decl = bval;
3288 else if (TREE_CODE (decl) == USING_DECL
3289 && TREE_CODE (bval) == USING_DECL
3290 && same_type_p (USING_DECL_SCOPE (decl),
3291 USING_DECL_SCOPE (bval)))
3292 /* This is a using redeclaration that will be diagnosed later
3293 in supplement_binding */
3295 else if (TREE_CODE (decl) == USING_DECL
3296 && TREE_CODE (bval) == USING_DECL
3297 && DECL_DEPENDENT_P (decl)
3298 && DECL_DEPENDENT_P (bval))
3299 return true;
3300 else if (TREE_CODE (decl) == USING_DECL
3301 && is_overloaded_fn (target_bval))
3302 old_decl = bval;
3303 else if (TREE_CODE (bval) == USING_DECL
3304 && is_overloaded_fn (target_decl))
3305 return true;
3307 if (old_decl && binding->scope == class_binding_level)
3309 binding->value = x;
3310 /* It is always safe to clear INHERITED_VALUE_BINDING_P
3311 here. This function is only used to register bindings
3312 from with the class definition itself. */
3313 INHERITED_VALUE_BINDING_P (binding) = 0;
3314 return true;
3318 /* Note that we declared this value so that we can issue an error if
3319 this is an invalid redeclaration of a name already used for some
3320 other purpose. */
3321 note_name_declared_in_class (name, decl);
3323 /* If we didn't replace an existing binding, put the binding on the
3324 stack of bindings for the identifier, and update the shadowed
3325 list. */
3326 if (binding && binding->scope == class_binding_level)
3327 /* Supplement the existing binding. */
3328 ok = supplement_binding (binding, decl);
3329 else
3331 /* Create a new binding. */
3332 push_binding (name, decl, class_binding_level);
3333 ok = true;
3336 return ok;
3339 /* Wrapper for push_class_level_binding_1. */
3341 bool
3342 push_class_level_binding (tree name, tree x)
3344 bool ret;
3345 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3346 ret = push_class_level_binding_1 (name, x);
3347 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3348 return ret;
3351 /* Process "using SCOPE::NAME" in a class scope. Return the
3352 USING_DECL created. */
3354 tree
3355 do_class_using_decl (tree scope, tree name)
3357 /* The USING_DECL returned by this function. */
3358 tree value;
3359 /* The declaration (or declarations) name by this using
3360 declaration. NULL if we are in a template and cannot figure out
3361 what has been named. */
3362 tree decl;
3363 /* True if SCOPE is a dependent type. */
3364 bool scope_dependent_p;
3365 /* True if SCOPE::NAME is dependent. */
3366 bool name_dependent_p;
3367 /* True if any of the bases of CURRENT_CLASS_TYPE are dependent. */
3368 bool bases_dependent_p;
3369 tree binfo;
3371 if (name == error_mark_node)
3372 return NULL_TREE;
3374 if (!scope || !TYPE_P (scope))
3376 error ("using-declaration for non-member at class scope");
3377 return NULL_TREE;
3380 /* Make sure the name is not invalid */
3381 if (TREE_CODE (name) == BIT_NOT_EXPR)
3383 error ("%<%T::%D%> names destructor", scope, name);
3384 return NULL_TREE;
3386 /* Using T::T declares inheriting ctors, even if T is a typedef. */
3387 if (MAYBE_CLASS_TYPE_P (scope)
3388 && (name == TYPE_IDENTIFIER (scope)
3389 || constructor_name_p (name, scope)))
3391 maybe_warn_cpp0x (CPP0X_INHERITING_CTORS);
3392 name = ctor_identifier;
3393 CLASSTYPE_NON_AGGREGATE (current_class_type) = true;
3395 if (constructor_name_p (name, current_class_type))
3397 error ("%<%T::%D%> names constructor in %qT",
3398 scope, name, current_class_type);
3399 return NULL_TREE;
3402 scope_dependent_p = dependent_scope_p (scope);
3403 name_dependent_p = (scope_dependent_p
3404 || (IDENTIFIER_TYPENAME_P (name)
3405 && dependent_type_p (TREE_TYPE (name))));
3407 bases_dependent_p = any_dependent_bases_p ();
3409 decl = NULL_TREE;
3411 /* From [namespace.udecl]:
3413 A using-declaration used as a member-declaration shall refer to a
3414 member of a base class of the class being defined.
3416 In general, we cannot check this constraint in a template because
3417 we do not know the entire set of base classes of the current
3418 class type. Morover, if SCOPE is dependent, it might match a
3419 non-dependent base. */
3421 if (!scope_dependent_p)
3423 base_kind b_kind;
3424 binfo = lookup_base (current_class_type, scope, ba_any, &b_kind,
3425 tf_warning_or_error);
3426 if (b_kind < bk_proper_base)
3428 if (!bases_dependent_p || b_kind == bk_same_type)
3430 error_not_base_type (scope, current_class_type);
3431 return NULL_TREE;
3434 else if (name == ctor_identifier
3435 && BINFO_INHERITANCE_CHAIN (BINFO_INHERITANCE_CHAIN (binfo)))
3437 error ("cannot inherit constructors from indirect base %qT", scope);
3438 return NULL_TREE;
3440 else if (!name_dependent_p)
3442 decl = lookup_member (binfo, name, 0, false, tf_warning_or_error);
3443 if (!decl)
3445 error ("no members matching %<%T::%D%> in %q#T", scope, name,
3446 scope);
3447 return NULL_TREE;
3449 /* The binfo from which the functions came does not matter. */
3450 if (BASELINK_P (decl))
3451 decl = BASELINK_FUNCTIONS (decl);
3455 value = build_lang_decl (USING_DECL, name, NULL_TREE);
3456 USING_DECL_DECLS (value) = decl;
3457 USING_DECL_SCOPE (value) = scope;
3458 DECL_DEPENDENT_P (value) = !decl;
3460 return value;
3464 /* Return the binding value for name in scope. */
3467 static tree
3468 namespace_binding_1 (tree name, tree scope)
3470 cxx_binding *binding;
3472 if (SCOPE_FILE_SCOPE_P (scope))
3473 scope = global_namespace;
3474 else
3475 /* Unnecessary for the global namespace because it can't be an alias. */
3476 scope = ORIGINAL_NAMESPACE (scope);
3478 binding = cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
3480 return binding ? binding->value : NULL_TREE;
3483 tree
3484 namespace_binding (tree name, tree scope)
3486 tree ret;
3487 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3488 ret = namespace_binding_1 (name, scope);
3489 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3490 return ret;
3493 /* Set the binding value for name in scope. */
3495 static void
3496 set_namespace_binding_1 (tree name, tree scope, tree val)
3498 cxx_binding *b;
3500 if (scope == NULL_TREE)
3501 scope = global_namespace;
3502 b = binding_for_name (NAMESPACE_LEVEL (scope), name);
3503 if (!b->value
3504 /* For templates and using we create a single element OVERLOAD.
3505 Look for the chain to know whether this is really augmenting
3506 an existing overload. */
3507 || (TREE_CODE (val) == OVERLOAD && OVL_CHAIN (val))
3508 || val == error_mark_node)
3509 b->value = val;
3510 else
3511 supplement_binding (b, val);
3514 /* Wrapper for set_namespace_binding_1. */
3516 void
3517 set_namespace_binding (tree name, tree scope, tree val)
3519 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3520 set_namespace_binding_1 (name, scope, val);
3521 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3524 /* Set the context of a declaration to scope. Complain if we are not
3525 outside scope. */
3527 void
3528 set_decl_namespace (tree decl, tree scope, bool friendp)
3530 tree old;
3532 /* Get rid of namespace aliases. */
3533 scope = ORIGINAL_NAMESPACE (scope);
3535 /* It is ok for friends to be qualified in parallel space. */
3536 if (!friendp && !is_ancestor (current_namespace, scope))
3537 error ("declaration of %qD not in a namespace surrounding %qD",
3538 decl, scope);
3539 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3541 /* Writing "int N::i" to declare a variable within "N" is invalid. */
3542 if (scope == current_namespace)
3544 if (at_namespace_scope_p ())
3545 error ("explicit qualification in declaration of %qD",
3546 decl);
3547 return;
3550 /* See whether this has been declared in the namespace. */
3551 old = lookup_qualified_name (scope, DECL_NAME (decl), /*type*/false,
3552 /*complain*/true, /*hidden*/true);
3553 if (old == error_mark_node)
3554 /* No old declaration at all. */
3555 goto complain;
3556 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
3557 if (TREE_CODE (old) == TREE_LIST)
3559 error ("reference to %qD is ambiguous", decl);
3560 print_candidates (old);
3561 return;
3563 if (!is_overloaded_fn (decl))
3565 /* We might have found OLD in an inline namespace inside SCOPE. */
3566 if (TREE_CODE (decl) == TREE_CODE (old))
3567 DECL_CONTEXT (decl) = DECL_CONTEXT (old);
3568 /* Don't compare non-function decls with decls_match here, since
3569 it can't check for the correct constness at this
3570 point. pushdecl will find those errors later. */
3571 return;
3573 /* Since decl is a function, old should contain a function decl. */
3574 if (!is_overloaded_fn (old))
3575 goto complain;
3576 /* We handle these in check_explicit_instantiation_namespace. */
3577 if (processing_explicit_instantiation)
3578 return;
3579 if (processing_template_decl || processing_specialization)
3580 /* We have not yet called push_template_decl to turn a
3581 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
3582 match. But, we'll check later, when we construct the
3583 template. */
3584 return;
3585 /* Instantiations or specializations of templates may be declared as
3586 friends in any namespace. */
3587 if (friendp && DECL_USE_TEMPLATE (decl))
3588 return;
3589 if (is_overloaded_fn (old))
3591 tree found = NULL_TREE;
3592 tree elt = old;
3593 for (; elt; elt = OVL_NEXT (elt))
3595 tree ofn = OVL_CURRENT (elt);
3596 /* Adjust DECL_CONTEXT first so decls_match will return true
3597 if DECL will match a declaration in an inline namespace. */
3598 DECL_CONTEXT (decl) = DECL_CONTEXT (ofn);
3599 if (decls_match (decl, ofn))
3601 if (found && !decls_match (found, ofn))
3603 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3604 error ("reference to %qD is ambiguous", decl);
3605 print_candidates (old);
3606 return;
3608 found = ofn;
3611 if (found)
3613 if (!is_associated_namespace (scope, CP_DECL_CONTEXT (found)))
3614 goto complain;
3615 if (DECL_HIDDEN_FRIEND_P (found))
3617 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
3618 "%qD has not been declared within %D", decl, scope);
3619 inform (DECL_SOURCE_LOCATION (found), "only here as a friend");
3621 DECL_CONTEXT (decl) = DECL_CONTEXT (found);
3622 return;
3625 else
3627 DECL_CONTEXT (decl) = DECL_CONTEXT (old);
3628 if (decls_match (decl, old))
3629 return;
3632 /* It didn't work, go back to the explicit scope. */
3633 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3634 complain:
3635 error ("%qD should have been declared inside %qD", decl, scope);
3638 /* Return the namespace where the current declaration is declared. */
3640 tree
3641 current_decl_namespace (void)
3643 tree result;
3644 /* If we have been pushed into a different namespace, use it. */
3645 if (!vec_safe_is_empty (decl_namespace_list))
3646 return decl_namespace_list->last ();
3648 if (current_class_type)
3649 result = decl_namespace_context (current_class_type);
3650 else if (current_function_decl)
3651 result = decl_namespace_context (current_function_decl);
3652 else
3653 result = current_namespace;
3654 return result;
3657 /* Process any ATTRIBUTES on a namespace definition. Returns true if
3658 attribute visibility is seen. */
3660 bool
3661 handle_namespace_attrs (tree ns, tree attributes)
3663 tree d;
3664 bool saw_vis = false;
3666 for (d = attributes; d; d = TREE_CHAIN (d))
3668 tree name = get_attribute_name (d);
3669 tree args = TREE_VALUE (d);
3671 if (is_attribute_p ("visibility", name))
3673 /* attribute visibility is a property of the syntactic block
3674 rather than the namespace as a whole, so we don't touch the
3675 NAMESPACE_DECL at all. */
3676 tree x = args ? TREE_VALUE (args) : NULL_TREE;
3677 if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args))
3679 warning (OPT_Wattributes,
3680 "%qD attribute requires a single NTBS argument",
3681 name);
3682 continue;
3685 if (!TREE_PUBLIC (ns))
3686 warning (OPT_Wattributes,
3687 "%qD attribute is meaningless since members of the "
3688 "anonymous namespace get local symbols", name);
3690 push_visibility (TREE_STRING_POINTER (x), 1);
3691 saw_vis = true;
3693 else if (is_attribute_p ("abi_tag", name))
3695 if (!DECL_NAMESPACE_ASSOCIATIONS (ns))
3697 warning (OPT_Wattributes, "ignoring %qD attribute on non-inline "
3698 "namespace", name);
3699 continue;
3701 if (!DECL_NAME (ns))
3703 warning (OPT_Wattributes, "ignoring %qD attribute on anonymous "
3704 "namespace", name);
3705 continue;
3707 if (!args)
3709 tree dn = DECL_NAME (ns);
3710 args = build_string (IDENTIFIER_LENGTH (dn) + 1,
3711 IDENTIFIER_POINTER (dn));
3712 TREE_TYPE (args) = char_array_type_node;
3713 args = fix_string_type (args);
3714 args = build_tree_list (NULL_TREE, args);
3716 if (check_abi_tag_args (args, name))
3717 DECL_ATTRIBUTES (ns) = tree_cons (name, args,
3718 DECL_ATTRIBUTES (ns));
3720 else
3722 warning (OPT_Wattributes, "%qD attribute directive ignored",
3723 name);
3724 continue;
3728 return saw_vis;
3731 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we
3732 select a name that is unique to this compilation unit. Returns FALSE if
3733 pushdecl fails, TRUE otherwise. */
3735 bool
3736 push_namespace (tree name)
3738 tree d = NULL_TREE;
3739 bool need_new = true;
3740 bool implicit_use = false;
3741 bool anon = !name;
3743 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3745 /* We should not get here if the global_namespace is not yet constructed
3746 nor if NAME designates the global namespace: The global scope is
3747 constructed elsewhere. */
3748 gcc_assert (global_namespace != NULL && name != global_scope_name);
3750 if (anon)
3752 name = get_anonymous_namespace_name();
3753 d = IDENTIFIER_NAMESPACE_VALUE (name);
3754 if (d)
3755 /* Reopening anonymous namespace. */
3756 need_new = false;
3757 implicit_use = true;
3759 else
3761 /* Check whether this is an extended namespace definition. */
3762 d = IDENTIFIER_NAMESPACE_VALUE (name);
3763 if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
3765 tree dna = DECL_NAMESPACE_ALIAS (d);
3766 if (dna)
3768 /* We do some error recovery for, eg, the redeclaration
3769 of M here:
3771 namespace N {}
3772 namespace M = N;
3773 namespace M {}
3775 However, in nasty cases like:
3777 namespace N
3779 namespace M = N;
3780 namespace M {}
3783 we just error out below, in duplicate_decls. */
3784 if (NAMESPACE_LEVEL (dna)->level_chain
3785 == current_binding_level)
3787 error ("namespace alias %qD not allowed here, "
3788 "assuming %qD", d, dna);
3789 d = dna;
3790 need_new = false;
3793 else
3794 need_new = false;
3798 if (need_new)
3800 /* Make a new namespace, binding the name to it. */
3801 d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
3802 DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace);
3803 /* The name of this namespace is not visible to other translation
3804 units if it is an anonymous namespace or member thereof. */
3805 if (anon || decl_anon_ns_mem_p (current_namespace))
3806 TREE_PUBLIC (d) = 0;
3807 else
3808 TREE_PUBLIC (d) = 1;
3809 if (pushdecl (d) == error_mark_node)
3811 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3812 return false;
3814 if (anon)
3816 /* Clear DECL_NAME for the benefit of debugging back ends. */
3817 SET_DECL_ASSEMBLER_NAME (d, name);
3818 DECL_NAME (d) = NULL_TREE;
3820 begin_scope (sk_namespace, d);
3822 else
3823 resume_scope (NAMESPACE_LEVEL (d));
3825 if (implicit_use)
3826 do_using_directive (d);
3827 /* Enter the name space. */
3828 current_namespace = d;
3830 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3831 return true;
3834 /* Pop from the scope of the current namespace. */
3836 void
3837 pop_namespace (void)
3839 gcc_assert (current_namespace != global_namespace);
3840 current_namespace = CP_DECL_CONTEXT (current_namespace);
3841 /* The binding level is not popped, as it might be re-opened later. */
3842 leave_scope ();
3845 /* Push into the scope of the namespace NS, even if it is deeply
3846 nested within another namespace. */
3848 void
3849 push_nested_namespace (tree ns)
3851 if (ns == global_namespace)
3852 push_to_top_level ();
3853 else
3855 push_nested_namespace (CP_DECL_CONTEXT (ns));
3856 push_namespace (DECL_NAME (ns));
3860 /* Pop back from the scope of the namespace NS, which was previously
3861 entered with push_nested_namespace. */
3863 void
3864 pop_nested_namespace (tree ns)
3866 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3867 gcc_assert (current_namespace == ns);
3868 while (ns != global_namespace)
3870 pop_namespace ();
3871 ns = CP_DECL_CONTEXT (ns);
3874 pop_from_top_level ();
3875 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3878 /* Temporarily set the namespace for the current declaration. */
3880 void
3881 push_decl_namespace (tree decl)
3883 if (TREE_CODE (decl) != NAMESPACE_DECL)
3884 decl = decl_namespace_context (decl);
3885 vec_safe_push (decl_namespace_list, ORIGINAL_NAMESPACE (decl));
3888 /* [namespace.memdef]/2 */
3890 void
3891 pop_decl_namespace (void)
3893 decl_namespace_list->pop ();
3896 /* Return the namespace that is the common ancestor
3897 of two given namespaces. */
3899 static tree
3900 namespace_ancestor_1 (tree ns1, tree ns2)
3902 tree nsr;
3903 if (is_ancestor (ns1, ns2))
3904 nsr = ns1;
3905 else
3906 nsr = namespace_ancestor_1 (CP_DECL_CONTEXT (ns1), ns2);
3907 return nsr;
3910 /* Wrapper for namespace_ancestor_1. */
3912 static tree
3913 namespace_ancestor (tree ns1, tree ns2)
3915 tree nsr;
3916 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3917 nsr = namespace_ancestor_1 (ns1, ns2);
3918 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3919 return nsr;
3922 /* Process a namespace-alias declaration. */
3924 void
3925 do_namespace_alias (tree alias, tree name_space)
3927 if (name_space == error_mark_node)
3928 return;
3930 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
3932 name_space = ORIGINAL_NAMESPACE (name_space);
3934 /* Build the alias. */
3935 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
3936 DECL_NAMESPACE_ALIAS (alias) = name_space;
3937 DECL_EXTERNAL (alias) = 1;
3938 DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ());
3939 pushdecl (alias);
3941 /* Emit debug info for namespace alias. */
3942 if (!building_stmt_list_p ())
3943 (*debug_hooks->early_global_decl) (alias);
3946 /* Like pushdecl, only it places X in the current namespace,
3947 if appropriate. */
3949 tree
3950 pushdecl_namespace_level (tree x, bool is_friend)
3952 cp_binding_level *b = current_binding_level;
3953 tree t;
3955 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3956 t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace), is_friend);
3958 /* Now, the type_shadowed stack may screw us. Munge it so it does
3959 what we want. */
3960 if (TREE_CODE (t) == TYPE_DECL)
3962 tree name = DECL_NAME (t);
3963 tree newval;
3964 tree *ptr = (tree *)0;
3965 for (; !global_scope_p (b); b = b->level_chain)
3967 tree shadowed = b->type_shadowed;
3968 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
3969 if (TREE_PURPOSE (shadowed) == name)
3971 ptr = &TREE_VALUE (shadowed);
3972 /* Can't break out of the loop here because sometimes
3973 a binding level will have duplicate bindings for
3974 PT names. It's gross, but I haven't time to fix it. */
3977 newval = TREE_TYPE (t);
3978 if (ptr == (tree *)0)
3980 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
3981 up here if this is changed to an assertion. --KR */
3982 SET_IDENTIFIER_TYPE_VALUE (name, t);
3984 else
3986 *ptr = newval;
3989 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3990 return t;
3993 /* Insert USED into the using list of USER. Set INDIRECT_flag if this
3994 directive is not directly from the source. Also find the common
3995 ancestor and let our users know about the new namespace */
3997 static void
3998 add_using_namespace_1 (tree user, tree used, bool indirect)
4000 tree t;
4001 /* Using oneself is a no-op. */
4002 if (user == used)
4003 return;
4004 gcc_assert (TREE_CODE (user) == NAMESPACE_DECL);
4005 gcc_assert (TREE_CODE (used) == NAMESPACE_DECL);
4006 /* Check if we already have this. */
4007 t = purpose_member (used, DECL_NAMESPACE_USING (user));
4008 if (t != NULL_TREE)
4010 if (!indirect)
4011 /* Promote to direct usage. */
4012 TREE_INDIRECT_USING (t) = 0;
4013 return;
4016 /* Add used to the user's using list. */
4017 DECL_NAMESPACE_USING (user)
4018 = tree_cons (used, namespace_ancestor (user, used),
4019 DECL_NAMESPACE_USING (user));
4021 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect;
4023 /* Add user to the used's users list. */
4024 DECL_NAMESPACE_USERS (used)
4025 = tree_cons (user, 0, DECL_NAMESPACE_USERS (used));
4027 /* Recursively add all namespaces used. */
4028 for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t))
4029 /* indirect usage */
4030 add_using_namespace_1 (user, TREE_PURPOSE (t), 1);
4032 /* Tell everyone using us about the new used namespaces. */
4033 for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t))
4034 add_using_namespace_1 (TREE_PURPOSE (t), used, 1);
4037 /* Wrapper for add_using_namespace_1. */
4039 static void
4040 add_using_namespace (tree user, tree used, bool indirect)
4042 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4043 add_using_namespace_1 (user, used, indirect);
4044 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4047 /* Process a using-declaration not appearing in class or local scope. */
4049 void
4050 do_toplevel_using_decl (tree decl, tree scope, tree name)
4052 tree oldval, oldtype, newval, newtype;
4053 tree orig_decl = decl;
4054 cxx_binding *binding;
4056 decl = validate_nonmember_using_decl (decl, scope, name);
4057 if (decl == NULL_TREE)
4058 return;
4060 binding = binding_for_name (NAMESPACE_LEVEL (current_namespace), name);
4062 oldval = binding->value;
4063 oldtype = binding->type;
4065 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
4067 /* Emit debug info. */
4068 if (!processing_template_decl)
4069 cp_emit_debug_info_for_using (orig_decl, current_namespace);
4071 /* Copy declarations found. */
4072 if (newval)
4073 binding->value = newval;
4074 if (newtype)
4075 binding->type = newtype;
4078 /* Process a using-directive. */
4080 void
4081 do_using_directive (tree name_space)
4083 tree context = NULL_TREE;
4085 if (name_space == error_mark_node)
4086 return;
4088 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
4090 if (building_stmt_list_p ())
4091 add_stmt (build_stmt (input_location, USING_STMT, name_space));
4092 name_space = ORIGINAL_NAMESPACE (name_space);
4094 if (!toplevel_bindings_p ())
4096 push_using_directive (name_space);
4098 else
4100 /* direct usage */
4101 add_using_namespace (current_namespace, name_space, 0);
4102 if (current_namespace != global_namespace)
4103 context = current_namespace;
4105 /* Emit debugging info. */
4106 if (!processing_template_decl)
4107 (*debug_hooks->imported_module_or_decl) (name_space, NULL_TREE,
4108 context, false);
4112 /* Deal with a using-directive seen by the parser. Currently we only
4113 handle attributes here, since they cannot appear inside a template. */
4115 void
4116 parse_using_directive (tree name_space, tree attribs)
4118 do_using_directive (name_space);
4120 if (attribs == error_mark_node)
4121 return;
4123 for (tree a = attribs; a; a = TREE_CHAIN (a))
4125 tree name = get_attribute_name (a);
4126 if (is_attribute_p ("strong", name))
4128 if (!toplevel_bindings_p ())
4129 error ("strong using only meaningful at namespace scope");
4130 else if (name_space != error_mark_node)
4132 if (!is_ancestor (current_namespace, name_space))
4133 error ("current namespace %qD does not enclose strongly used namespace %qD",
4134 current_namespace, name_space);
4135 DECL_NAMESPACE_ASSOCIATIONS (name_space)
4136 = tree_cons (current_namespace, 0,
4137 DECL_NAMESPACE_ASSOCIATIONS (name_space));
4140 else
4141 warning (OPT_Wattributes, "%qD attribute directive ignored", name);
4145 /* Like pushdecl, only it places X in the global scope if appropriate.
4146 Calls cp_finish_decl to register the variable, initializing it with
4147 *INIT, if INIT is non-NULL. */
4149 static tree
4150 pushdecl_top_level_1 (tree x, tree *init, bool is_friend)
4152 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4153 push_to_top_level ();
4154 x = pushdecl_namespace_level (x, is_friend);
4155 if (init)
4156 cp_finish_decl (x, *init, false, NULL_TREE, 0);
4157 pop_from_top_level ();
4158 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4159 return x;
4162 /* Like pushdecl, only it places X in the global scope if appropriate. */
4164 tree
4165 pushdecl_top_level (tree x)
4167 return pushdecl_top_level_1 (x, NULL, false);
4170 /* Like pushdecl_top_level, but adding the IS_FRIEND parameter. */
4172 tree
4173 pushdecl_top_level_maybe_friend (tree x, bool is_friend)
4175 return pushdecl_top_level_1 (x, NULL, is_friend);
4178 /* Like pushdecl, only it places X in the global scope if
4179 appropriate. Calls cp_finish_decl to register the variable,
4180 initializing it with INIT. */
4182 tree
4183 pushdecl_top_level_and_finish (tree x, tree init)
4185 return pushdecl_top_level_1 (x, &init, false);
4188 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
4189 duplicates. The first list becomes the tail of the result.
4191 The algorithm is O(n^2). We could get this down to O(n log n) by
4192 doing a sort on the addresses of the functions, if that becomes
4193 necessary. */
4195 static tree
4196 merge_functions (tree s1, tree s2)
4198 for (; s2; s2 = OVL_NEXT (s2))
4200 tree fn2 = OVL_CURRENT (s2);
4201 tree fns1;
4203 for (fns1 = s1; fns1; fns1 = OVL_NEXT (fns1))
4205 tree fn1 = OVL_CURRENT (fns1);
4207 /* If the function from S2 is already in S1, there is no
4208 need to add it again. For `extern "C"' functions, we
4209 might have two FUNCTION_DECLs for the same function, in
4210 different namespaces, but let's leave them in case
4211 they have different default arguments. */
4212 if (fn1 == fn2)
4213 break;
4216 /* If we exhausted all of the functions in S1, FN2 is new. */
4217 if (!fns1)
4218 s1 = build_overload (fn2, s1);
4220 return s1;
4223 /* Returns TRUE iff OLD and NEW are the same entity.
4225 3 [basic]/3: An entity is a value, object, reference, function,
4226 enumerator, type, class member, template, template specialization,
4227 namespace, parameter pack, or this.
4229 7.3.4 [namespace.udir]/4: If name lookup finds a declaration for a name
4230 in two different namespaces, and the declarations do not declare the
4231 same entity and do not declare functions, the use of the name is
4232 ill-formed. */
4234 static bool
4235 same_entity_p (tree one, tree two)
4237 if (one == two)
4238 return true;
4239 if (!one || !two)
4240 return false;
4241 if (TREE_CODE (one) == TYPE_DECL
4242 && TREE_CODE (two) == TYPE_DECL
4243 && same_type_p (TREE_TYPE (one), TREE_TYPE (two)))
4244 return true;
4245 return false;
4248 /* This should return an error not all definitions define functions.
4249 It is not an error if we find two functions with exactly the
4250 same signature, only if these are selected in overload resolution.
4251 old is the current set of bindings, new_binding the freshly-found binding.
4252 XXX Do we want to give *all* candidates in case of ambiguity?
4253 XXX In what way should I treat extern declarations?
4254 XXX I don't want to repeat the entire duplicate_decls here */
4256 static void
4257 ambiguous_decl (struct scope_binding *old, cxx_binding *new_binding, int flags)
4259 tree val, type;
4260 gcc_assert (old != NULL);
4262 /* Copy the type. */
4263 type = new_binding->type;
4264 if (LOOKUP_NAMESPACES_ONLY (flags)
4265 || (type && hidden_name_p (type) && !(flags & LOOKUP_HIDDEN)))
4266 type = NULL_TREE;
4268 /* Copy the value. */
4269 val = new_binding->value;
4270 if (val)
4272 if (!(flags & LOOKUP_HIDDEN))
4273 val = remove_hidden_names (val);
4274 if (val)
4275 switch (TREE_CODE (val))
4277 case TEMPLATE_DECL:
4278 /* If we expect types or namespaces, and not templates,
4279 or this is not a template class. */
4280 if ((LOOKUP_QUALIFIERS_ONLY (flags)
4281 && !DECL_TYPE_TEMPLATE_P (val)))
4282 val = NULL_TREE;
4283 break;
4284 case TYPE_DECL:
4285 if (LOOKUP_NAMESPACES_ONLY (flags)
4286 || (type && (flags & LOOKUP_PREFER_TYPES)))
4287 val = NULL_TREE;
4288 break;
4289 case NAMESPACE_DECL:
4290 if (LOOKUP_TYPES_ONLY (flags))
4291 val = NULL_TREE;
4292 break;
4293 case FUNCTION_DECL:
4294 /* Ignore built-in functions that are still anticipated. */
4295 if (LOOKUP_QUALIFIERS_ONLY (flags))
4296 val = NULL_TREE;
4297 break;
4298 default:
4299 if (LOOKUP_QUALIFIERS_ONLY (flags))
4300 val = NULL_TREE;
4304 /* If val is hidden, shift down any class or enumeration name. */
4305 if (!val)
4307 val = type;
4308 type = NULL_TREE;
4311 if (!old->value)
4312 old->value = val;
4313 else if (val && !same_entity_p (val, old->value))
4315 if (is_overloaded_fn (old->value) && is_overloaded_fn (val))
4316 old->value = merge_functions (old->value, val);
4317 else
4319 old->value = tree_cons (NULL_TREE, old->value,
4320 build_tree_list (NULL_TREE, val));
4321 TREE_TYPE (old->value) = error_mark_node;
4325 if (!old->type)
4326 old->type = type;
4327 else if (type && old->type != type)
4329 old->type = tree_cons (NULL_TREE, old->type,
4330 build_tree_list (NULL_TREE, type));
4331 TREE_TYPE (old->type) = error_mark_node;
4335 /* Return the declarations that are members of the namespace NS. */
4337 tree
4338 cp_namespace_decls (tree ns)
4340 return NAMESPACE_LEVEL (ns)->names;
4343 /* Combine prefer_type and namespaces_only into flags. */
4345 static int
4346 lookup_flags (int prefer_type, int namespaces_only)
4348 if (namespaces_only)
4349 return LOOKUP_PREFER_NAMESPACES;
4350 if (prefer_type > 1)
4351 return LOOKUP_PREFER_TYPES;
4352 if (prefer_type > 0)
4353 return LOOKUP_PREFER_BOTH;
4354 return 0;
4357 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
4358 ignore it or not. Subroutine of lookup_name_real and
4359 lookup_type_scope. */
4361 static bool
4362 qualify_lookup (tree val, int flags)
4364 if (val == NULL_TREE)
4365 return false;
4366 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
4367 return true;
4368 if (flags & LOOKUP_PREFER_TYPES)
4370 tree target_val = strip_using_decl (val);
4371 if (TREE_CODE (target_val) == TYPE_DECL
4372 || TREE_CODE (target_val) == TEMPLATE_DECL)
4373 return true;
4375 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
4376 return false;
4377 /* Look through lambda things that we shouldn't be able to see. */
4378 if (is_lambda_ignored_entity (val))
4379 return false;
4380 return true;
4383 /* Given a lookup that returned VAL, decide if we want to ignore it or
4384 not based on DECL_ANTICIPATED. */
4386 bool
4387 hidden_name_p (tree val)
4389 if (DECL_P (val)
4390 && DECL_LANG_SPECIFIC (val)
4391 && TYPE_FUNCTION_OR_TEMPLATE_DECL_P (val)
4392 && DECL_ANTICIPATED (val))
4393 return true;
4394 if (TREE_CODE (val) == OVERLOAD)
4396 for (tree o = val; o; o = OVL_CHAIN (o))
4397 if (!hidden_name_p (OVL_FUNCTION (o)))
4398 return false;
4399 return true;
4401 return false;
4404 /* Remove any hidden declarations from a possibly overloaded set
4405 of functions. */
4407 tree
4408 remove_hidden_names (tree fns)
4410 if (!fns)
4411 return fns;
4413 if (DECL_P (fns) && hidden_name_p (fns))
4414 fns = NULL_TREE;
4415 else if (TREE_CODE (fns) == OVERLOAD)
4417 tree o;
4419 for (o = fns; o; o = OVL_NEXT (o))
4420 if (hidden_name_p (OVL_CURRENT (o)))
4421 break;
4422 if (o)
4424 tree n = NULL_TREE;
4426 for (o = fns; o; o = OVL_NEXT (o))
4427 if (!hidden_name_p (OVL_CURRENT (o)))
4428 n = build_overload (OVL_CURRENT (o), n);
4429 fns = n;
4433 return fns;
4436 /* Suggest alternatives for NAME, an IDENTIFIER_NODE for which name
4437 lookup failed. Search through all available namespaces and print out
4438 possible candidates. If no exact matches are found, and
4439 SUGGEST_MISSPELLINGS is true, then also look for near-matches and
4440 suggest the best near-match, if there is one. */
4442 void
4443 suggest_alternatives_for (location_t location, tree name,
4444 bool suggest_misspellings)
4446 vec<tree> candidates = vNULL;
4447 vec<tree> namespaces_to_search = vNULL;
4448 int max_to_search = PARAM_VALUE (CXX_MAX_NAMESPACES_FOR_DIAGNOSTIC_HELP);
4449 int n_searched = 0;
4450 tree t;
4451 unsigned ix;
4453 namespaces_to_search.safe_push (global_namespace);
4455 while (!namespaces_to_search.is_empty ()
4456 && n_searched < max_to_search)
4458 tree scope = namespaces_to_search.pop ();
4459 struct scope_binding binding = EMPTY_SCOPE_BINDING;
4460 cp_binding_level *level = NAMESPACE_LEVEL (scope);
4462 /* Look in this namespace. */
4463 qualified_lookup_using_namespace (name, scope, &binding, 0);
4465 n_searched++;
4467 if (binding.value)
4468 candidates.safe_push (binding.value);
4470 /* Add child namespaces. */
4471 for (t = level->namespaces; t; t = DECL_CHAIN (t))
4472 namespaces_to_search.safe_push (t);
4475 /* If we stopped before we could examine all namespaces, inform the
4476 user. Do this even if we don't have any candidates, since there
4477 might be more candidates further down that we weren't able to
4478 find. */
4479 if (n_searched >= max_to_search
4480 && !namespaces_to_search.is_empty ())
4481 inform (location,
4482 "maximum limit of %d namespaces searched for %qE",
4483 max_to_search, name);
4485 namespaces_to_search.release ();
4487 /* Nothing useful to report for NAME. Report on likely misspellings,
4488 or do nothing. */
4489 if (candidates.is_empty ())
4491 if (suggest_misspellings)
4493 const char *fuzzy_name = lookup_name_fuzzy (name, FUZZY_LOOKUP_NAME);
4494 if (fuzzy_name)
4496 gcc_rich_location richloc (location);
4497 richloc.add_fixit_replace (fuzzy_name);
4498 inform_at_rich_loc (&richloc, "suggested alternative: %qs",
4499 fuzzy_name);
4502 return;
4505 inform_n (location, candidates.length (),
4506 "suggested alternative:",
4507 "suggested alternatives:");
4509 FOR_EACH_VEC_ELT (candidates, ix, t)
4510 inform (location_of (t), " %qE", t);
4512 candidates.release ();
4515 /* Look for alternatives for NAME, an IDENTIFIER_NODE for which name
4516 lookup failed within the explicitly provided SCOPE. Suggest the
4517 the best meaningful candidates (if any) as a fix-it hint.
4518 Return true iff a suggestion was provided. */
4520 bool
4521 suggest_alternative_in_explicit_scope (location_t location, tree name,
4522 tree scope)
4524 cp_binding_level *level = NAMESPACE_LEVEL (scope);
4526 best_match <tree, tree> bm (name);
4527 consider_binding_level (name, bm, level, false, FUZZY_LOOKUP_NAME);
4529 /* See if we have a good suggesion for the user. */
4530 tree best_id = bm.get_best_meaningful_candidate ();
4531 if (best_id)
4533 const char *fuzzy_name = IDENTIFIER_POINTER (best_id);
4534 gcc_rich_location richloc (location);
4535 richloc.add_fixit_replace (fuzzy_name);
4536 inform_at_rich_loc (&richloc, "suggested alternative: %qs",
4537 fuzzy_name);
4538 return true;
4541 return false;
4544 /* Unscoped lookup of a global: iterate over current namespaces,
4545 considering using-directives. */
4547 static tree
4548 unqualified_namespace_lookup_1 (tree name, int flags)
4550 tree initial = current_decl_namespace ();
4551 tree scope = initial;
4552 tree siter;
4553 cp_binding_level *level;
4554 tree val = NULL_TREE;
4556 for (; !val; scope = CP_DECL_CONTEXT (scope))
4558 struct scope_binding binding = EMPTY_SCOPE_BINDING;
4559 cxx_binding *b =
4560 cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
4562 if (b)
4563 ambiguous_decl (&binding, b, flags);
4565 /* Add all _DECLs seen through local using-directives. */
4566 for (level = current_binding_level;
4567 level->kind != sk_namespace;
4568 level = level->level_chain)
4569 if (!lookup_using_namespace (name, &binding, level->using_directives,
4570 scope, flags))
4571 /* Give up because of error. */
4572 return error_mark_node;
4574 /* Add all _DECLs seen through global using-directives. */
4575 /* XXX local and global using lists should work equally. */
4576 siter = initial;
4577 while (1)
4579 if (!lookup_using_namespace (name, &binding,
4580 DECL_NAMESPACE_USING (siter),
4581 scope, flags))
4582 /* Give up because of error. */
4583 return error_mark_node;
4584 if (siter == scope) break;
4585 siter = CP_DECL_CONTEXT (siter);
4588 val = binding.value;
4589 if (scope == global_namespace)
4590 break;
4592 return val;
4595 /* Wrapper for unqualified_namespace_lookup_1. */
4597 static tree
4598 unqualified_namespace_lookup (tree name, int flags)
4600 tree ret;
4601 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4602 ret = unqualified_namespace_lookup_1 (name, flags);
4603 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4604 return ret;
4607 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
4608 or a class TYPE).
4610 If PREFER_TYPE is > 0, we only return TYPE_DECLs or namespaces.
4611 If PREFER_TYPE is > 1, we only return TYPE_DECLs.
4613 Returns a DECL (or OVERLOAD, or BASELINK) representing the
4614 declaration found. If no suitable declaration can be found,
4615 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
4616 neither a class-type nor a namespace a diagnostic is issued. */
4618 tree
4619 lookup_qualified_name (tree scope, tree name, int prefer_type, bool complain,
4620 bool find_hidden)
4622 tree t = NULL_TREE;
4624 if (TREE_CODE (scope) == NAMESPACE_DECL)
4626 struct scope_binding binding = EMPTY_SCOPE_BINDING;
4628 int flags = lookup_flags (prefer_type, /*namespaces_only*/false);
4629 if (find_hidden)
4630 flags |= LOOKUP_HIDDEN;
4631 if (qualified_lookup_using_namespace (name, scope, &binding, flags))
4632 t = binding.value;
4634 else if (cxx_dialect != cxx98 && TREE_CODE (scope) == ENUMERAL_TYPE)
4635 t = lookup_enumerator (scope, name);
4636 else if (is_class_type (scope, complain))
4637 t = lookup_member (scope, name, 2, prefer_type, tf_warning_or_error);
4639 if (!t)
4640 return error_mark_node;
4641 return t;
4644 /* Subroutine of unqualified_namespace_lookup:
4645 Add the bindings of NAME in used namespaces to VAL.
4646 We are currently looking for names in namespace SCOPE, so we
4647 look through USINGS for using-directives of namespaces
4648 which have SCOPE as a common ancestor with the current scope.
4649 Returns false on errors. */
4651 static bool
4652 lookup_using_namespace (tree name, struct scope_binding *val,
4653 tree usings, tree scope, int flags)
4655 tree iter;
4656 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4657 /* Iterate over all used namespaces in current, searching for using
4658 directives of scope. */
4659 for (iter = usings; iter; iter = TREE_CHAIN (iter))
4660 if (TREE_VALUE (iter) == scope)
4662 tree used = ORIGINAL_NAMESPACE (TREE_PURPOSE (iter));
4663 cxx_binding *val1 =
4664 cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (used), name);
4665 /* Resolve ambiguities. */
4666 if (val1)
4667 ambiguous_decl (val, val1, flags);
4669 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4670 return val->value != error_mark_node;
4673 /* Returns true iff VEC contains TARGET. */
4675 static bool
4676 tree_vec_contains (vec<tree, va_gc> *vec, tree target)
4678 unsigned int i;
4679 tree elt;
4680 FOR_EACH_VEC_SAFE_ELT (vec,i,elt)
4681 if (elt == target)
4682 return true;
4683 return false;
4686 /* [namespace.qual]
4687 Accepts the NAME to lookup and its qualifying SCOPE.
4688 Returns the name/type pair found into the cxx_binding *RESULT,
4689 or false on error. */
4691 static bool
4692 qualified_lookup_using_namespace (tree name, tree scope,
4693 struct scope_binding *result, int flags)
4695 /* Maintain a list of namespaces visited... */
4696 vec<tree, va_gc> *seen = NULL;
4697 vec<tree, va_gc> *seen_inline = NULL;
4698 /* ... and a list of namespace yet to see. */
4699 vec<tree, va_gc> *todo = NULL;
4700 vec<tree, va_gc> *todo_maybe = NULL;
4701 vec<tree, va_gc> *todo_inline = NULL;
4702 tree usings;
4703 timevar_start (TV_NAME_LOOKUP);
4704 /* Look through namespace aliases. */
4705 scope = ORIGINAL_NAMESPACE (scope);
4707 /* Algorithm: Starting with SCOPE, walk through the set of used
4708 namespaces. For each used namespace, look through its inline
4709 namespace set for any bindings and usings. If no bindings are
4710 found, add any usings seen to the set of used namespaces. */
4711 vec_safe_push (todo, scope);
4713 while (todo->length ())
4715 bool found_here;
4716 scope = todo->pop ();
4717 if (tree_vec_contains (seen, scope))
4718 continue;
4719 vec_safe_push (seen, scope);
4720 vec_safe_push (todo_inline, scope);
4722 found_here = false;
4723 while (todo_inline->length ())
4725 cxx_binding *binding;
4727 scope = todo_inline->pop ();
4728 if (tree_vec_contains (seen_inline, scope))
4729 continue;
4730 vec_safe_push (seen_inline, scope);
4732 binding =
4733 cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
4734 if (binding)
4736 ambiguous_decl (result, binding, flags);
4737 if (result->type || result->value)
4738 found_here = true;
4741 for (usings = DECL_NAMESPACE_USING (scope); usings;
4742 usings = TREE_CHAIN (usings))
4743 if (!TREE_INDIRECT_USING (usings))
4745 if (is_associated_namespace (scope, TREE_PURPOSE (usings)))
4746 vec_safe_push (todo_inline, TREE_PURPOSE (usings));
4747 else
4748 vec_safe_push (todo_maybe, TREE_PURPOSE (usings));
4752 if (found_here)
4753 vec_safe_truncate (todo_maybe, 0);
4754 else
4755 while (vec_safe_length (todo_maybe))
4756 vec_safe_push (todo, todo_maybe->pop ());
4758 vec_free (todo);
4759 vec_free (todo_maybe);
4760 vec_free (todo_inline);
4761 vec_free (seen);
4762 vec_free (seen_inline);
4763 timevar_stop (TV_NAME_LOOKUP);
4764 return result->value != error_mark_node;
4767 /* Helper function for lookup_name_fuzzy.
4768 Traverse binding level LVL, looking for good name matches for NAME
4769 (and BM). */
4770 static void
4771 consider_binding_level (tree name, best_match <tree, tree> &bm,
4772 cp_binding_level *lvl, bool look_within_fields,
4773 enum lookup_name_fuzzy_kind kind)
4775 if (look_within_fields)
4776 if (lvl->this_entity && TREE_CODE (lvl->this_entity) == RECORD_TYPE)
4778 tree type = lvl->this_entity;
4779 bool want_type_p = (kind == FUZZY_LOOKUP_TYPENAME);
4780 tree best_matching_field
4781 = lookup_member_fuzzy (type, name, want_type_p);
4782 if (best_matching_field)
4783 bm.consider (best_matching_field);
4786 for (tree t = lvl->names; t; t = TREE_CHAIN (t))
4788 tree d = t;
4790 /* OVERLOADs or decls from using declaration are wrapped into
4791 TREE_LIST. */
4792 if (TREE_CODE (d) == TREE_LIST)
4794 d = TREE_VALUE (d);
4795 d = OVL_CURRENT (d);
4798 /* Don't use bindings from implicitly declared functions,
4799 as they were likely misspellings themselves. */
4800 if (TREE_TYPE (d) == error_mark_node)
4801 continue;
4803 /* Skip anticipated decls of builtin functions. */
4804 if (TREE_CODE (d) == FUNCTION_DECL
4805 && DECL_BUILT_IN (d)
4806 && DECL_ANTICIPATED (d))
4807 continue;
4809 if (tree name = DECL_NAME (d))
4810 /* Ignore internal names with spaces in them. */
4811 if (!strchr (IDENTIFIER_POINTER (name), ' '))
4812 bm.consider (DECL_NAME (d));
4816 /* Search for near-matches for NAME within the current bindings, and within
4817 macro names, returning the best match as a const char *, or NULL if
4818 no reasonable match is found. */
4820 const char *
4821 lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind)
4823 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
4825 best_match <tree, tree> bm (name);
4827 cp_binding_level *lvl;
4828 for (lvl = scope_chain->class_bindings; lvl; lvl = lvl->level_chain)
4829 consider_binding_level (name, bm, lvl, true, kind);
4831 for (lvl = current_binding_level; lvl; lvl = lvl->level_chain)
4832 consider_binding_level (name, bm, lvl, false, kind);
4834 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
4836 x = SOME_OTHER_MACRO (y);
4837 then "SOME_OTHER_MACRO" will survive to the frontend and show up
4838 as a misspelled identifier.
4840 Use the best distance so far so that a candidate is only set if
4841 a macro is better than anything so far. This allows early rejection
4842 (without calculating the edit distance) of macro names that must have
4843 distance >= bm.get_best_distance (), and means that we only get a
4844 non-NULL result for best_macro_match if it's better than any of
4845 the identifiers already checked. */
4846 best_macro_match bmm (name, bm.get_best_distance (), parse_in);
4847 cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
4848 /* If a macro is the closest so far to NAME, suggest it. */
4849 if (best_macro)
4850 return (const char *)best_macro->ident.str;
4852 /* Try the "starts_decl_specifier_p" keywords to detect
4853 "singed" vs "signed" typos. */
4854 for (unsigned i = 0; i < num_c_common_reswords; i++)
4856 const c_common_resword *resword = &c_common_reswords[i];
4858 if (!cp_keyword_starts_decl_specifier_p (resword->rid))
4859 continue;
4861 tree resword_identifier = ridpointers [resword->rid];
4862 if (!resword_identifier)
4863 continue;
4864 gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
4866 /* Only consider reserved words that survived the
4867 filtering in init_reswords (e.g. for -std). */
4868 if (!C_IS_RESERVED_WORD (resword_identifier))
4869 continue;
4871 bm.consider (resword_identifier);
4874 /* See if we have a good suggesion for the user. */
4875 tree best_id = bm.get_best_meaningful_candidate ();
4876 if (best_id)
4877 return IDENTIFIER_POINTER (best_id);
4879 /* No meaningful suggestion available. */
4880 return NULL;
4883 /* Subroutine of outer_binding.
4885 Returns TRUE if BINDING is a binding to a template parameter of
4886 SCOPE. In that case SCOPE is the scope of a primary template
4887 parameter -- in the sense of G++, i.e, a template that has its own
4888 template header.
4890 Returns FALSE otherwise. */
4892 static bool
4893 binding_to_template_parms_of_scope_p (cxx_binding *binding,
4894 cp_binding_level *scope)
4896 tree binding_value, tmpl, tinfo;
4897 int level;
4899 if (!binding || !scope || !scope->this_entity)
4900 return false;
4902 binding_value = binding->value ? binding->value : binding->type;
4903 tinfo = get_template_info (scope->this_entity);
4905 /* BINDING_VALUE must be a template parm. */
4906 if (binding_value == NULL_TREE
4907 || (!DECL_P (binding_value)
4908 || !DECL_TEMPLATE_PARM_P (binding_value)))
4909 return false;
4911 /* The level of BINDING_VALUE. */
4912 level =
4913 template_type_parameter_p (binding_value)
4914 ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX
4915 (TREE_TYPE (binding_value)))
4916 : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value));
4918 /* The template of the current scope, iff said scope is a primary
4919 template. */
4920 tmpl = (tinfo
4921 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
4922 ? TI_TEMPLATE (tinfo)
4923 : NULL_TREE);
4925 /* If the level of the parm BINDING_VALUE equals the depth of TMPL,
4926 then BINDING_VALUE is a parameter of TMPL. */
4927 return (tmpl && level == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
4930 /* Return the innermost non-namespace binding for NAME from a scope
4931 containing BINDING, or, if BINDING is NULL, the current scope.
4932 Please note that for a given template, the template parameters are
4933 considered to be in the scope containing the current scope.
4934 If CLASS_P is false, then class bindings are ignored. */
4936 cxx_binding *
4937 outer_binding (tree name,
4938 cxx_binding *binding,
4939 bool class_p)
4941 cxx_binding *outer;
4942 cp_binding_level *scope;
4943 cp_binding_level *outer_scope;
4945 if (binding)
4947 scope = binding->scope->level_chain;
4948 outer = binding->previous;
4950 else
4952 scope = current_binding_level;
4953 outer = IDENTIFIER_BINDING (name);
4955 outer_scope = outer ? outer->scope : NULL;
4957 /* Because we create class bindings lazily, we might be missing a
4958 class binding for NAME. If there are any class binding levels
4959 between the LAST_BINDING_LEVEL and the scope in which OUTER was
4960 declared, we must lookup NAME in those class scopes. */
4961 if (class_p)
4962 while (scope && scope != outer_scope && scope->kind != sk_namespace)
4964 if (scope->kind == sk_class)
4966 cxx_binding *class_binding;
4968 class_binding = get_class_binding (name, scope);
4969 if (class_binding)
4971 /* Thread this new class-scope binding onto the
4972 IDENTIFIER_BINDING list so that future lookups
4973 find it quickly. */
4974 class_binding->previous = outer;
4975 if (binding)
4976 binding->previous = class_binding;
4977 else
4978 IDENTIFIER_BINDING (name) = class_binding;
4979 return class_binding;
4982 /* If we are in a member template, the template parms of the member
4983 template are considered to be inside the scope of the containing
4984 class, but within G++ the class bindings are all pushed between the
4985 template parms and the function body. So if the outer binding is
4986 a template parm for the current scope, return it now rather than
4987 look for a class binding. */
4988 if (outer_scope && outer_scope->kind == sk_template_parms
4989 && binding_to_template_parms_of_scope_p (outer, scope))
4990 return outer;
4992 scope = scope->level_chain;
4995 return outer;
4998 /* Return the innermost block-scope or class-scope value binding for
4999 NAME, or NULL_TREE if there is no such binding. */
5001 tree
5002 innermost_non_namespace_value (tree name)
5004 cxx_binding *binding;
5005 binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true);
5006 return binding ? binding->value : NULL_TREE;
5009 /* Look up NAME in the current binding level and its superiors in the
5010 namespace of variables, functions and typedefs. Return a ..._DECL
5011 node of some kind representing its definition if there is only one
5012 such declaration, or return a TREE_LIST with all the overloaded
5013 definitions if there are many, or return 0 if it is undefined.
5014 Hidden name, either friend declaration or built-in function, are
5015 not ignored.
5017 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
5018 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
5019 Otherwise we prefer non-TYPE_DECLs.
5021 If NONCLASS is nonzero, bindings in class scopes are ignored. If
5022 BLOCK_P is false, bindings in block scopes are ignored. */
5024 static tree
5025 lookup_name_real_1 (tree name, int prefer_type, int nonclass, bool block_p,
5026 int namespaces_only, int flags)
5028 cxx_binding *iter;
5029 tree val = NULL_TREE;
5031 /* Conversion operators are handled specially because ordinary
5032 unqualified name lookup will not find template conversion
5033 operators. */
5034 if (IDENTIFIER_TYPENAME_P (name))
5036 cp_binding_level *level;
5038 for (level = current_binding_level;
5039 level && level->kind != sk_namespace;
5040 level = level->level_chain)
5042 tree class_type;
5043 tree operators;
5045 /* A conversion operator can only be declared in a class
5046 scope. */
5047 if (level->kind != sk_class)
5048 continue;
5050 /* Lookup the conversion operator in the class. */
5051 class_type = level->this_entity;
5052 operators = lookup_fnfields (class_type, name, /*protect=*/0);
5053 if (operators)
5054 return operators;
5057 return NULL_TREE;
5060 flags |= lookup_flags (prefer_type, namespaces_only);
5062 /* First, look in non-namespace scopes. */
5064 if (current_class_type == NULL_TREE)
5065 nonclass = 1;
5067 if (block_p || !nonclass)
5068 for (iter = outer_binding (name, NULL, !nonclass);
5069 iter;
5070 iter = outer_binding (name, iter, !nonclass))
5072 tree binding;
5074 /* Skip entities we don't want. */
5075 if (LOCAL_BINDING_P (iter) ? !block_p : nonclass)
5076 continue;
5078 /* If this is the kind of thing we're looking for, we're done. */
5079 if (qualify_lookup (iter->value, flags))
5080 binding = iter->value;
5081 else if ((flags & LOOKUP_PREFER_TYPES)
5082 && qualify_lookup (iter->type, flags))
5083 binding = iter->type;
5084 else
5085 binding = NULL_TREE;
5087 if (binding)
5089 if (hidden_name_p (binding))
5091 /* A non namespace-scope binding can only be hidden in the
5092 presence of a local class, due to friend declarations.
5094 In particular, consider:
5096 struct C;
5097 void f() {
5098 struct A {
5099 friend struct B;
5100 friend struct C;
5101 void g() {
5102 B* b; // error: B is hidden
5103 C* c; // OK, finds ::C
5106 B *b; // error: B is hidden
5107 C *c; // OK, finds ::C
5108 struct B {};
5109 B *bb; // OK
5112 The standard says that "B" is a local class in "f"
5113 (but not nested within "A") -- but that name lookup
5114 for "B" does not find this declaration until it is
5115 declared directly with "f".
5117 In particular:
5119 [class.friend]
5121 If a friend declaration appears in a local class and
5122 the name specified is an unqualified name, a prior
5123 declaration is looked up without considering scopes
5124 that are outside the innermost enclosing non-class
5125 scope. For a friend function declaration, if there is
5126 no prior declaration, the program is ill-formed. For a
5127 friend class declaration, if there is no prior
5128 declaration, the class that is specified belongs to the
5129 innermost enclosing non-class scope, but if it is
5130 subsequently referenced, its name is not found by name
5131 lookup until a matching declaration is provided in the
5132 innermost enclosing nonclass scope.
5134 So just keep looking for a non-hidden binding.
5136 gcc_assert (TREE_CODE (binding) == TYPE_DECL);
5137 continue;
5139 val = binding;
5140 break;
5144 /* Now lookup in namespace scopes. */
5145 if (!val)
5146 val = unqualified_namespace_lookup (name, flags);
5148 /* Anticipated built-ins and friends aren't found by normal lookup. */
5149 if (val && !(flags & LOOKUP_HIDDEN))
5150 val = remove_hidden_names (val);
5152 /* If we have a single function from a using decl, pull it out. */
5153 if (val && TREE_CODE (val) == OVERLOAD && !really_overloaded_fn (val))
5154 val = OVL_FUNCTION (val);
5156 return val;
5159 /* Wrapper for lookup_name_real_1. */
5161 tree
5162 lookup_name_real (tree name, int prefer_type, int nonclass, bool block_p,
5163 int namespaces_only, int flags)
5165 tree ret;
5166 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5167 ret = lookup_name_real_1 (name, prefer_type, nonclass, block_p,
5168 namespaces_only, flags);
5169 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5170 return ret;
5173 tree
5174 lookup_name_nonclass (tree name)
5176 return lookup_name_real (name, 0, 1, /*block_p=*/true, 0, 0);
5179 tree
5180 lookup_function_nonclass (tree name, vec<tree, va_gc> *args, bool block_p)
5182 return
5183 lookup_arg_dependent (name,
5184 lookup_name_real (name, 0, 1, block_p, 0, 0),
5185 args);
5188 tree
5189 lookup_name (tree name)
5191 return lookup_name_real (name, 0, 0, /*block_p=*/true, 0, 0);
5194 tree
5195 lookup_name_prefer_type (tree name, int prefer_type)
5197 return lookup_name_real (name, prefer_type, 0, /*block_p=*/true, 0, 0);
5200 /* Look up NAME for type used in elaborated name specifier in
5201 the scopes given by SCOPE. SCOPE can be either TS_CURRENT or
5202 TS_WITHIN_ENCLOSING_NON_CLASS. Although not implied by the
5203 name, more scopes are checked if cleanup or template parameter
5204 scope is encountered.
5206 Unlike lookup_name_real, we make sure that NAME is actually
5207 declared in the desired scope, not from inheritance, nor using
5208 directive. For using declaration, there is DR138 still waiting
5209 to be resolved. Hidden name coming from an earlier friend
5210 declaration is also returned.
5212 A TYPE_DECL best matching the NAME is returned. Catching error
5213 and issuing diagnostics are caller's responsibility. */
5215 static tree
5216 lookup_type_scope_1 (tree name, tag_scope scope)
5218 cxx_binding *iter = NULL;
5219 tree val = NULL_TREE;
5221 /* Look in non-namespace scope first. */
5222 if (current_binding_level->kind != sk_namespace)
5223 iter = outer_binding (name, NULL, /*class_p=*/ true);
5224 for (; iter; iter = outer_binding (name, iter, /*class_p=*/ true))
5226 /* Check if this is the kind of thing we're looking for.
5227 If SCOPE is TS_CURRENT, also make sure it doesn't come from
5228 base class. For ITER->VALUE, we can simply use
5229 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use
5230 our own check.
5232 We check ITER->TYPE before ITER->VALUE in order to handle
5233 typedef struct C {} C;
5234 correctly. */
5236 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES)
5237 && (scope != ts_current
5238 || LOCAL_BINDING_P (iter)
5239 || DECL_CONTEXT (iter->type) == iter->scope->this_entity))
5240 val = iter->type;
5241 else if ((scope != ts_current
5242 || !INHERITED_VALUE_BINDING_P (iter))
5243 && qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
5244 val = iter->value;
5246 if (val)
5247 break;
5250 /* Look in namespace scope. */
5251 if (!val)
5253 iter = cp_binding_level_find_binding_for_name
5254 (NAMESPACE_LEVEL (current_decl_namespace ()), name);
5256 if (iter)
5258 /* If this is the kind of thing we're looking for, we're done. */
5259 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES))
5260 val = iter->type;
5261 else if (qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
5262 val = iter->value;
5267 /* Type found, check if it is in the allowed scopes, ignoring cleanup
5268 and template parameter scopes. */
5269 if (val)
5271 cp_binding_level *b = current_binding_level;
5272 while (b)
5274 if (iter->scope == b)
5275 return val;
5277 if (b->kind == sk_cleanup || b->kind == sk_template_parms
5278 || b->kind == sk_function_parms)
5279 b = b->level_chain;
5280 else if (b->kind == sk_class
5281 && scope == ts_within_enclosing_non_class)
5282 b = b->level_chain;
5283 else
5284 break;
5288 return NULL_TREE;
5291 /* Wrapper for lookup_type_scope_1. */
5293 tree
5294 lookup_type_scope (tree name, tag_scope scope)
5296 tree ret;
5297 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5298 ret = lookup_type_scope_1 (name, scope);
5299 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5300 return ret;
5304 /* Similar to `lookup_name' but look only in the innermost non-class
5305 binding level. */
5307 static tree
5308 lookup_name_innermost_nonclass_level_1 (tree name)
5310 cp_binding_level *b;
5311 tree t = NULL_TREE;
5313 b = innermost_nonclass_level ();
5315 if (b->kind == sk_namespace)
5317 t = IDENTIFIER_NAMESPACE_VALUE (name);
5319 /* extern "C" function() */
5320 if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
5321 t = TREE_VALUE (t);
5323 else if (IDENTIFIER_BINDING (name)
5324 && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
5326 cxx_binding *binding;
5327 binding = IDENTIFIER_BINDING (name);
5328 while (1)
5330 if (binding->scope == b
5331 && !(VAR_P (binding->value)
5332 && DECL_DEAD_FOR_LOCAL (binding->value)))
5333 return binding->value;
5335 if (b->kind == sk_cleanup)
5336 b = b->level_chain;
5337 else
5338 break;
5342 return t;
5345 /* Wrapper for lookup_name_innermost_nonclass_level_1. */
5347 tree
5348 lookup_name_innermost_nonclass_level (tree name)
5350 tree ret;
5351 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5352 ret = lookup_name_innermost_nonclass_level_1 (name);
5353 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5354 return ret;
5358 /* Returns true iff DECL is a block-scope extern declaration of a function
5359 or variable. */
5361 bool
5362 is_local_extern (tree decl)
5364 cxx_binding *binding;
5366 /* For functions, this is easy. */
5367 if (TREE_CODE (decl) == FUNCTION_DECL)
5368 return DECL_LOCAL_FUNCTION_P (decl);
5370 if (!VAR_P (decl))
5371 return false;
5372 if (!current_function_decl)
5373 return false;
5375 /* For variables, this is not easy. We need to look at the binding stack
5376 for the identifier to see whether the decl we have is a local. */
5377 for (binding = IDENTIFIER_BINDING (DECL_NAME (decl));
5378 binding && binding->scope->kind != sk_namespace;
5379 binding = binding->previous)
5380 if (binding->value == decl)
5381 return LOCAL_BINDING_P (binding);
5383 return false;
5386 /* Like lookup_name_innermost_nonclass_level, but for types. */
5388 static tree
5389 lookup_type_current_level (tree name)
5391 tree t = NULL_TREE;
5393 timevar_start (TV_NAME_LOOKUP);
5394 gcc_assert (current_binding_level->kind != sk_namespace);
5396 if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
5397 && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
5399 cp_binding_level *b = current_binding_level;
5400 while (1)
5402 if (purpose_member (name, b->type_shadowed))
5404 t = REAL_IDENTIFIER_TYPE_VALUE (name);
5405 break;
5407 if (b->kind == sk_cleanup)
5408 b = b->level_chain;
5409 else
5410 break;
5414 timevar_stop (TV_NAME_LOOKUP);
5415 return t;
5418 /* [basic.lookup.koenig] */
5419 /* A nonzero return value in the functions below indicates an error. */
5421 struct arg_lookup
5423 tree name;
5424 vec<tree, va_gc> *args;
5425 vec<tree, va_gc> *namespaces;
5426 vec<tree, va_gc> *classes;
5427 tree functions;
5428 hash_set<tree> *fn_set;
5431 static bool arg_assoc (struct arg_lookup*, tree);
5432 static bool arg_assoc_args (struct arg_lookup*, tree);
5433 static bool arg_assoc_args_vec (struct arg_lookup*, vec<tree, va_gc> *);
5434 static bool arg_assoc_type (struct arg_lookup*, tree);
5435 static bool add_function (struct arg_lookup *, tree);
5436 static bool arg_assoc_namespace (struct arg_lookup *, tree);
5437 static bool arg_assoc_class_only (struct arg_lookup *, tree);
5438 static bool arg_assoc_bases (struct arg_lookup *, tree);
5439 static bool arg_assoc_class (struct arg_lookup *, tree);
5440 static bool arg_assoc_template_arg (struct arg_lookup*, tree);
5442 /* Add a function to the lookup structure.
5443 Returns true on error. */
5445 static bool
5446 add_function (struct arg_lookup *k, tree fn)
5448 if (!is_overloaded_fn (fn))
5449 /* All names except those of (possibly overloaded) functions and
5450 function templates are ignored. */;
5451 else if (k->fn_set && k->fn_set->add (fn))
5452 /* It's already in the list. */;
5453 else if (!k->functions && TREE_CODE (fn) != TEMPLATE_DECL)
5454 k->functions = fn;
5455 else if (fn == k->functions)
5457 else
5459 k->functions = build_overload (fn, k->functions);
5460 if (TREE_CODE (k->functions) == OVERLOAD)
5461 OVL_ARG_DEPENDENT (k->functions) = true;
5464 return false;
5467 /* Returns true iff CURRENT has declared itself to be an associated
5468 namespace of SCOPE via a strong using-directive (or transitive chain
5469 thereof). Both are namespaces. */
5471 bool
5472 is_associated_namespace (tree current, tree scope)
5474 vec<tree, va_gc> *seen = make_tree_vector ();
5475 vec<tree, va_gc> *todo = make_tree_vector ();
5476 tree t;
5477 bool ret;
5479 while (1)
5481 if (scope == current)
5483 ret = true;
5484 break;
5486 vec_safe_push (seen, scope);
5487 for (t = DECL_NAMESPACE_ASSOCIATIONS (scope); t; t = TREE_CHAIN (t))
5488 if (!vec_member (TREE_PURPOSE (t), seen))
5489 vec_safe_push (todo, TREE_PURPOSE (t));
5490 if (!todo->is_empty ())
5492 scope = todo->last ();
5493 todo->pop ();
5495 else
5497 ret = false;
5498 break;
5502 release_tree_vector (seen);
5503 release_tree_vector (todo);
5505 return ret;
5508 /* Add functions of a namespace to the lookup structure.
5509 Returns true on error. */
5511 static bool
5512 arg_assoc_namespace (struct arg_lookup *k, tree scope)
5514 tree value;
5516 if (vec_member (scope, k->namespaces))
5517 return false;
5518 vec_safe_push (k->namespaces, scope);
5520 /* Check out our super-users. */
5521 for (value = DECL_NAMESPACE_ASSOCIATIONS (scope); value;
5522 value = TREE_CHAIN (value))
5523 if (arg_assoc_namespace (k, TREE_PURPOSE (value)))
5524 return true;
5526 /* Also look down into inline namespaces. */
5527 for (value = DECL_NAMESPACE_USING (scope); value;
5528 value = TREE_CHAIN (value))
5529 if (is_associated_namespace (scope, TREE_PURPOSE (value)))
5530 if (arg_assoc_namespace (k, TREE_PURPOSE (value)))
5531 return true;
5533 value = namespace_binding (k->name, scope);
5534 if (!value)
5535 return false;
5537 for (; value; value = OVL_NEXT (value))
5539 /* We don't want to find arbitrary hidden functions via argument
5540 dependent lookup. We only want to find friends of associated
5541 classes, which we'll do via arg_assoc_class. */
5542 if (hidden_name_p (OVL_CURRENT (value)))
5543 continue;
5545 if (add_function (k, OVL_CURRENT (value)))
5546 return true;
5549 return false;
5552 /* Adds everything associated with a template argument to the lookup
5553 structure. Returns true on error. */
5555 static bool
5556 arg_assoc_template_arg (struct arg_lookup *k, tree arg)
5558 /* [basic.lookup.koenig]
5560 If T is a template-id, its associated namespaces and classes are
5561 ... the namespaces and classes associated with the types of the
5562 template arguments provided for template type parameters
5563 (excluding template template parameters); the namespaces in which
5564 any template template arguments are defined; and the classes in
5565 which any member templates used as template template arguments
5566 are defined. [Note: non-type template arguments do not
5567 contribute to the set of associated namespaces. ] */
5569 /* Consider first template template arguments. */
5570 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
5571 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
5572 return false;
5573 else if (TREE_CODE (arg) == TEMPLATE_DECL)
5575 tree ctx = CP_DECL_CONTEXT (arg);
5577 /* It's not a member template. */
5578 if (TREE_CODE (ctx) == NAMESPACE_DECL)
5579 return arg_assoc_namespace (k, ctx);
5580 /* Otherwise, it must be member template. */
5581 else
5582 return arg_assoc_class_only (k, ctx);
5584 /* It's an argument pack; handle it recursively. */
5585 else if (ARGUMENT_PACK_P (arg))
5587 tree args = ARGUMENT_PACK_ARGS (arg);
5588 int i, len = TREE_VEC_LENGTH (args);
5589 for (i = 0; i < len; ++i)
5590 if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, i)))
5591 return true;
5593 return false;
5595 /* It's not a template template argument, but it is a type template
5596 argument. */
5597 else if (TYPE_P (arg))
5598 return arg_assoc_type (k, arg);
5599 /* It's a non-type template argument. */
5600 else
5601 return false;
5604 /* Adds the class and its friends to the lookup structure.
5605 Returns true on error. */
5607 static bool
5608 arg_assoc_class_only (struct arg_lookup *k, tree type)
5610 tree list, friends, context;
5612 /* Backend-built structures, such as __builtin_va_list, aren't
5613 affected by all this. */
5614 if (!CLASS_TYPE_P (type))
5615 return false;
5617 context = decl_namespace_context (type);
5618 if (arg_assoc_namespace (k, context))
5619 return true;
5621 complete_type (type);
5623 /* Process friends. */
5624 for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
5625 list = TREE_CHAIN (list))
5626 if (k->name == FRIEND_NAME (list))
5627 for (friends = FRIEND_DECLS (list); friends;
5628 friends = TREE_CHAIN (friends))
5630 tree fn = TREE_VALUE (friends);
5632 /* Only interested in global functions with potentially hidden
5633 (i.e. unqualified) declarations. */
5634 if (CP_DECL_CONTEXT (fn) != context)
5635 continue;
5636 /* Template specializations are never found by name lookup.
5637 (Templates themselves can be found, but not template
5638 specializations.) */
5639 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn))
5640 continue;
5641 if (add_function (k, fn))
5642 return true;
5645 return false;
5648 /* Adds the class and its bases to the lookup structure.
5649 Returns true on error. */
5651 static bool
5652 arg_assoc_bases (struct arg_lookup *k, tree type)
5654 if (arg_assoc_class_only (k, type))
5655 return true;
5657 if (TYPE_BINFO (type))
5659 /* Process baseclasses. */
5660 tree binfo, base_binfo;
5661 int i;
5663 for (binfo = TYPE_BINFO (type), i = 0;
5664 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5665 if (arg_assoc_bases (k, BINFO_TYPE (base_binfo)))
5666 return true;
5669 return false;
5672 /* Adds everything associated with a class argument type to the lookup
5673 structure. Returns true on error.
5675 If T is a class type (including unions), its associated classes are: the
5676 class itself; the class of which it is a member, if any; and its direct
5677 and indirect base classes. Its associated namespaces are the namespaces
5678 of which its associated classes are members. Furthermore, if T is a
5679 class template specialization, its associated namespaces and classes
5680 also include: the namespaces and classes associated with the types of
5681 the template arguments provided for template type parameters (excluding
5682 template template parameters); the namespaces of which any template
5683 template arguments are members; and the classes of which any member
5684 templates used as template template arguments are members. [ Note:
5685 non-type template arguments do not contribute to the set of associated
5686 namespaces. --end note] */
5688 static bool
5689 arg_assoc_class (struct arg_lookup *k, tree type)
5691 tree list;
5692 int i;
5694 /* Backend build structures, such as __builtin_va_list, aren't
5695 affected by all this. */
5696 if (!CLASS_TYPE_P (type))
5697 return false;
5699 if (vec_member (type, k->classes))
5700 return false;
5701 vec_safe_push (k->classes, type);
5703 if (TYPE_CLASS_SCOPE_P (type)
5704 && arg_assoc_class_only (k, TYPE_CONTEXT (type)))
5705 return true;
5707 if (arg_assoc_bases (k, type))
5708 return true;
5710 /* Process template arguments. */
5711 if (CLASSTYPE_TEMPLATE_INFO (type)
5712 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
5714 list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
5715 for (i = 0; i < TREE_VEC_LENGTH (list); ++i)
5716 if (arg_assoc_template_arg (k, TREE_VEC_ELT (list, i)))
5717 return true;
5720 return false;
5723 /* Adds everything associated with a given type.
5724 Returns 1 on error. */
5726 static bool
5727 arg_assoc_type (struct arg_lookup *k, tree type)
5729 /* As we do not get the type of non-type dependent expressions
5730 right, we can end up with such things without a type. */
5731 if (!type)
5732 return false;
5734 if (TYPE_PTRDATAMEM_P (type))
5736 /* Pointer to member: associate class type and value type. */
5737 if (arg_assoc_type (k, TYPE_PTRMEM_CLASS_TYPE (type)))
5738 return true;
5739 return arg_assoc_type (k, TYPE_PTRMEM_POINTED_TO_TYPE (type));
5741 else switch (TREE_CODE (type))
5743 case ERROR_MARK:
5744 return false;
5745 case VOID_TYPE:
5746 case INTEGER_TYPE:
5747 case REAL_TYPE:
5748 case COMPLEX_TYPE:
5749 case VECTOR_TYPE:
5750 case BOOLEAN_TYPE:
5751 case FIXED_POINT_TYPE:
5752 case DECLTYPE_TYPE:
5753 case NULLPTR_TYPE:
5754 return false;
5755 case RECORD_TYPE:
5756 if (TYPE_PTRMEMFUNC_P (type))
5757 return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type));
5758 /* FALLTHRU */
5759 case UNION_TYPE:
5760 return arg_assoc_class (k, type);
5761 case POINTER_TYPE:
5762 case REFERENCE_TYPE:
5763 case ARRAY_TYPE:
5764 return arg_assoc_type (k, TREE_TYPE (type));
5765 case ENUMERAL_TYPE:
5766 if (TYPE_CLASS_SCOPE_P (type)
5767 && arg_assoc_class_only (k, TYPE_CONTEXT (type)))
5768 return true;
5769 return arg_assoc_namespace (k, decl_namespace_context (type));
5770 case METHOD_TYPE:
5771 /* The basetype is referenced in the first arg type, so just
5772 fall through. */
5773 case FUNCTION_TYPE:
5774 /* Associate the parameter types. */
5775 if (arg_assoc_args (k, TYPE_ARG_TYPES (type)))
5776 return true;
5777 /* Associate the return type. */
5778 return arg_assoc_type (k, TREE_TYPE (type));
5779 case TEMPLATE_TYPE_PARM:
5780 case BOUND_TEMPLATE_TEMPLATE_PARM:
5781 return false;
5782 case TYPENAME_TYPE:
5783 return false;
5784 case LANG_TYPE:
5785 gcc_assert (type == unknown_type_node
5786 || type == init_list_type_node);
5787 return false;
5788 case TYPE_PACK_EXPANSION:
5789 return arg_assoc_type (k, PACK_EXPANSION_PATTERN (type));
5791 default:
5792 gcc_unreachable ();
5794 return false;
5797 /* Adds everything associated with arguments. Returns true on error. */
5799 static bool
5800 arg_assoc_args (struct arg_lookup *k, tree args)
5802 for (; args; args = TREE_CHAIN (args))
5803 if (arg_assoc (k, TREE_VALUE (args)))
5804 return true;
5805 return false;
5808 /* Adds everything associated with an argument vector. Returns true
5809 on error. */
5811 static bool
5812 arg_assoc_args_vec (struct arg_lookup *k, vec<tree, va_gc> *args)
5814 unsigned int ix;
5815 tree arg;
5817 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
5818 if (arg_assoc (k, arg))
5819 return true;
5820 return false;
5823 /* Adds everything associated with a given tree_node. Returns 1 on error. */
5825 static bool
5826 arg_assoc (struct arg_lookup *k, tree n)
5828 if (n == error_mark_node)
5829 return false;
5831 if (TYPE_P (n))
5832 return arg_assoc_type (k, n);
5834 if (! type_unknown_p (n))
5835 return arg_assoc_type (k, TREE_TYPE (n));
5837 if (TREE_CODE (n) == ADDR_EXPR)
5838 n = TREE_OPERAND (n, 0);
5839 if (TREE_CODE (n) == COMPONENT_REF)
5840 n = TREE_OPERAND (n, 1);
5841 if (TREE_CODE (n) == OFFSET_REF)
5842 n = TREE_OPERAND (n, 1);
5843 while (TREE_CODE (n) == TREE_LIST)
5844 n = TREE_VALUE (n);
5845 if (BASELINK_P (n))
5846 n = BASELINK_FUNCTIONS (n);
5848 if (TREE_CODE (n) == FUNCTION_DECL)
5849 return arg_assoc_type (k, TREE_TYPE (n));
5850 if (TREE_CODE (n) == TEMPLATE_ID_EXPR)
5852 /* The working paper doesn't currently say how to handle template-id
5853 arguments. The sensible thing would seem to be to handle the list
5854 of template candidates like a normal overload set, and handle the
5855 template arguments like we do for class template
5856 specializations. */
5857 tree templ = TREE_OPERAND (n, 0);
5858 tree args = TREE_OPERAND (n, 1);
5859 int ix;
5861 /* First the templates. */
5862 if (arg_assoc (k, templ))
5863 return true;
5865 /* Now the arguments. */
5866 if (args)
5867 for (ix = TREE_VEC_LENGTH (args); ix--;)
5868 if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, ix)) == 1)
5869 return true;
5871 else if (TREE_CODE (n) == OVERLOAD)
5873 for (; n; n = OVL_NEXT (n))
5874 if (arg_assoc_type (k, TREE_TYPE (OVL_CURRENT (n))))
5875 return true;
5878 return false;
5881 /* Performs Koenig lookup depending on arguments, where fns
5882 are the functions found in normal lookup. */
5884 static cp_expr
5885 lookup_arg_dependent_1 (tree name, tree fns, vec<tree, va_gc> *args)
5887 struct arg_lookup k;
5889 /* Remove any hidden friend functions from the list of functions
5890 found so far. They will be added back by arg_assoc_class as
5891 appropriate. */
5892 fns = remove_hidden_names (fns);
5894 k.name = name;
5895 k.args = args;
5896 k.functions = fns;
5897 k.classes = make_tree_vector ();
5899 /* We previously performed an optimization here by setting
5900 NAMESPACES to the current namespace when it was safe. However, DR
5901 164 says that namespaces that were already searched in the first
5902 stage of template processing are searched again (potentially
5903 picking up later definitions) in the second stage. */
5904 k.namespaces = make_tree_vector ();
5906 /* We used to allow duplicates and let joust discard them, but
5907 since the above change for DR 164 we end up with duplicates of
5908 all the functions found by unqualified lookup. So keep track
5909 of which ones we've seen. */
5910 if (fns)
5912 tree ovl;
5913 /* We shouldn't be here if lookup found something other than
5914 namespace-scope functions. */
5915 gcc_assert (DECL_NAMESPACE_SCOPE_P (OVL_CURRENT (fns)));
5916 k.fn_set = new hash_set<tree>;
5917 for (ovl = fns; ovl; ovl = OVL_NEXT (ovl))
5918 k.fn_set->add (OVL_CURRENT (ovl));
5920 else
5921 k.fn_set = NULL;
5923 arg_assoc_args_vec (&k, args);
5925 fns = k.functions;
5927 if (fns
5928 && !VAR_P (fns)
5929 && !is_overloaded_fn (fns))
5931 error ("argument dependent lookup finds %q+D", fns);
5932 error (" in call to %qD", name);
5933 fns = error_mark_node;
5936 release_tree_vector (k.classes);
5937 release_tree_vector (k.namespaces);
5938 delete k.fn_set;
5940 return fns;
5943 /* Wrapper for lookup_arg_dependent_1. */
5945 cp_expr
5946 lookup_arg_dependent (tree name, tree fns, vec<tree, va_gc> *args)
5948 cp_expr ret;
5949 bool subtime;
5950 subtime = timevar_cond_start (TV_NAME_LOOKUP);
5951 ret = lookup_arg_dependent_1 (name, fns, args);
5952 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5953 return ret;
5957 /* Add namespace to using_directives. Return NULL_TREE if nothing was
5958 changed (i.e. there was already a directive), or the fresh
5959 TREE_LIST otherwise. */
5961 static tree
5962 push_using_directive_1 (tree used)
5964 tree ud = current_binding_level->using_directives;
5965 tree iter, ancestor;
5967 /* Check if we already have this. */
5968 if (purpose_member (used, ud) != NULL_TREE)
5969 return NULL_TREE;
5971 ancestor = namespace_ancestor (current_decl_namespace (), used);
5972 ud = current_binding_level->using_directives;
5973 ud = tree_cons (used, ancestor, ud);
5974 current_binding_level->using_directives = ud;
5976 /* Recursively add all namespaces used. */
5977 for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
5978 push_using_directive (TREE_PURPOSE (iter));
5980 return ud;
5983 /* Wrapper for push_using_directive_1. */
5985 static tree
5986 push_using_directive (tree used)
5988 tree ret;
5989 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5990 ret = push_using_directive_1 (used);
5991 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5992 return ret;
5995 /* The type TYPE is being declared. If it is a class template, or a
5996 specialization of a class template, do any processing required and
5997 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
5998 being declared a friend. B is the binding level at which this TYPE
5999 should be bound.
6001 Returns the TYPE_DECL for TYPE, which may have been altered by this
6002 processing. */
6004 static tree
6005 maybe_process_template_type_declaration (tree type, int is_friend,
6006 cp_binding_level *b)
6008 tree decl = TYPE_NAME (type);
6010 if (processing_template_parmlist)
6011 /* You can't declare a new template type in a template parameter
6012 list. But, you can declare a non-template type:
6014 template <class A*> struct S;
6016 is a forward-declaration of `A'. */
6018 else if (b->kind == sk_namespace
6019 && current_binding_level->kind != sk_namespace)
6020 /* If this new type is being injected into a containing scope,
6021 then it's not a template type. */
6023 else
6025 gcc_assert (MAYBE_CLASS_TYPE_P (type)
6026 || TREE_CODE (type) == ENUMERAL_TYPE);
6028 if (processing_template_decl)
6030 /* This may change after the call to
6031 push_template_decl_real, but we want the original value. */
6032 tree name = DECL_NAME (decl);
6034 decl = push_template_decl_real (decl, is_friend);
6035 if (decl == error_mark_node)
6036 return error_mark_node;
6038 /* If the current binding level is the binding level for the
6039 template parameters (see the comment in
6040 begin_template_parm_list) and the enclosing level is a class
6041 scope, and we're not looking at a friend, push the
6042 declaration of the member class into the class scope. In the
6043 friend case, push_template_decl will already have put the
6044 friend into global scope, if appropriate. */
6045 if (TREE_CODE (type) != ENUMERAL_TYPE
6046 && !is_friend && b->kind == sk_template_parms
6047 && b->level_chain->kind == sk_class)
6049 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
6051 if (!COMPLETE_TYPE_P (current_class_type))
6053 maybe_add_class_template_decl_list (current_class_type,
6054 type, /*friend_p=*/0);
6055 /* Put this UTD in the table of UTDs for the class. */
6056 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
6057 CLASSTYPE_NESTED_UTDS (current_class_type) =
6058 binding_table_new (SCOPE_DEFAULT_HT_SIZE);
6060 binding_table_insert
6061 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
6067 return decl;
6070 /* Push a tag name NAME for struct/class/union/enum type TYPE. In case
6071 that the NAME is a class template, the tag is processed but not pushed.
6073 The pushed scope depend on the SCOPE parameter:
6074 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
6075 scope.
6076 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
6077 non-template-parameter scope. This case is needed for forward
6078 declarations.
6079 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
6080 TS_GLOBAL case except that names within template-parameter scopes
6081 are not pushed at all.
6083 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
6085 static tree
6086 pushtag_1 (tree name, tree type, tag_scope scope)
6088 cp_binding_level *b;
6089 tree decl;
6091 b = current_binding_level;
6092 while (/* Cleanup scopes are not scopes from the point of view of
6093 the language. */
6094 b->kind == sk_cleanup
6095 /* Neither are function parameter scopes. */
6096 || b->kind == sk_function_parms
6097 /* Neither are the scopes used to hold template parameters
6098 for an explicit specialization. For an ordinary template
6099 declaration, these scopes are not scopes from the point of
6100 view of the language. */
6101 || (b->kind == sk_template_parms
6102 && (b->explicit_spec_p || scope == ts_global))
6103 || (b->kind == sk_class
6104 && (scope != ts_current
6105 /* We may be defining a new type in the initializer
6106 of a static member variable. We allow this when
6107 not pedantic, and it is particularly useful for
6108 type punning via an anonymous union. */
6109 || COMPLETE_TYPE_P (b->this_entity))))
6110 b = b->level_chain;
6112 gcc_assert (identifier_p (name));
6114 /* Do C++ gratuitous typedefing. */
6115 if (identifier_type_value_1 (name) != type)
6117 tree tdef;
6118 int in_class = 0;
6119 tree context = TYPE_CONTEXT (type);
6121 if (! context)
6123 tree cs = current_scope ();
6125 if (scope == ts_current
6126 || (cs && TREE_CODE (cs) == FUNCTION_DECL))
6127 context = cs;
6128 else if (cs != NULL_TREE && TYPE_P (cs))
6129 /* When declaring a friend class of a local class, we want
6130 to inject the newly named class into the scope
6131 containing the local class, not the namespace
6132 scope. */
6133 context = decl_function_context (get_type_decl (cs));
6135 if (!context)
6136 context = current_namespace;
6138 if (b->kind == sk_class
6139 || (b->kind == sk_template_parms
6140 && b->level_chain->kind == sk_class))
6141 in_class = 1;
6143 tdef = create_implicit_typedef (name, type);
6144 DECL_CONTEXT (tdef) = FROB_CONTEXT (context);
6145 if (scope == ts_within_enclosing_non_class)
6147 /* This is a friend. Make this TYPE_DECL node hidden from
6148 ordinary name lookup. Its corresponding TEMPLATE_DECL
6149 will be marked in push_template_decl_real. */
6150 retrofit_lang_decl (tdef);
6151 DECL_ANTICIPATED (tdef) = 1;
6152 DECL_FRIEND_P (tdef) = 1;
6155 decl = maybe_process_template_type_declaration
6156 (type, scope == ts_within_enclosing_non_class, b);
6157 if (decl == error_mark_node)
6158 return decl;
6160 if (b->kind == sk_class)
6162 if (!TYPE_BEING_DEFINED (current_class_type))
6163 return error_mark_node;
6165 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
6166 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
6167 class. But if it's a member template class, we want
6168 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
6169 later. */
6170 finish_member_declaration (decl);
6171 else
6172 pushdecl_class_level (decl);
6174 else if (b->kind != sk_template_parms)
6176 decl = pushdecl_with_scope_1 (decl, b, /*is_friend=*/false);
6177 if (decl == error_mark_node)
6178 return decl;
6181 if (! in_class)
6182 set_identifier_type_value_with_scope (name, tdef, b);
6184 TYPE_CONTEXT (type) = DECL_CONTEXT (decl);
6186 /* If this is a local class, keep track of it. We need this
6187 information for name-mangling, and so that it is possible to
6188 find all function definitions in a translation unit in a
6189 convenient way. (It's otherwise tricky to find a member
6190 function definition it's only pointed to from within a local
6191 class.) */
6192 if (TYPE_FUNCTION_SCOPE_P (type))
6194 if (processing_template_decl)
6196 /* Push a DECL_EXPR so we call pushtag at the right time in
6197 template instantiation rather than in some nested context. */
6198 add_decl_expr (decl);
6200 else
6201 vec_safe_push (local_classes, type);
6204 if (b->kind == sk_class
6205 && !COMPLETE_TYPE_P (current_class_type))
6207 maybe_add_class_template_decl_list (current_class_type,
6208 type, /*friend_p=*/0);
6210 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
6211 CLASSTYPE_NESTED_UTDS (current_class_type)
6212 = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
6214 binding_table_insert
6215 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
6218 decl = TYPE_NAME (type);
6219 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
6221 /* Set type visibility now if this is a forward declaration. */
6222 TREE_PUBLIC (decl) = 1;
6223 determine_visibility (decl);
6225 return type;
6228 /* Wrapper for pushtag_1. */
6230 tree
6231 pushtag (tree name, tree type, tag_scope scope)
6233 tree ret;
6234 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6235 ret = pushtag_1 (name, type, scope);
6236 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6237 return ret;
6240 /* Subroutines for reverting temporarily to top-level for instantiation
6241 of templates and such. We actually need to clear out the class- and
6242 local-value slots of all identifiers, so that only the global values
6243 are at all visible. Simply setting current_binding_level to the global
6244 scope isn't enough, because more binding levels may be pushed. */
6245 struct saved_scope *scope_chain;
6247 /* Return true if ID has not already been marked. */
6249 static inline bool
6250 store_binding_p (tree id)
6252 if (!id || !IDENTIFIER_BINDING (id))
6253 return false;
6255 if (IDENTIFIER_MARKED (id))
6256 return false;
6258 return true;
6261 /* Add an appropriate binding to *OLD_BINDINGS which needs to already
6262 have enough space reserved. */
6264 static void
6265 store_binding (tree id, vec<cxx_saved_binding, va_gc> **old_bindings)
6267 cxx_saved_binding saved;
6269 gcc_checking_assert (store_binding_p (id));
6271 IDENTIFIER_MARKED (id) = 1;
6273 saved.identifier = id;
6274 saved.binding = IDENTIFIER_BINDING (id);
6275 saved.real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
6276 (*old_bindings)->quick_push (saved);
6277 IDENTIFIER_BINDING (id) = NULL;
6280 static void
6281 store_bindings (tree names, vec<cxx_saved_binding, va_gc> **old_bindings)
6283 static vec<tree> bindings_need_stored;
6284 tree t, id;
6285 size_t i;
6287 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6288 for (t = names; t; t = TREE_CHAIN (t))
6290 if (TREE_CODE (t) == TREE_LIST)
6291 id = TREE_PURPOSE (t);
6292 else
6293 id = DECL_NAME (t);
6295 if (store_binding_p (id))
6296 bindings_need_stored.safe_push (id);
6298 if (!bindings_need_stored.is_empty ())
6300 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
6301 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
6303 /* We can appearantly have duplicates in NAMES. */
6304 if (store_binding_p (id))
6305 store_binding (id, old_bindings);
6307 bindings_need_stored.truncate (0);
6309 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6312 /* Like store_bindings, but NAMES is a vector of cp_class_binding
6313 objects, rather than a TREE_LIST. */
6315 static void
6316 store_class_bindings (vec<cp_class_binding, va_gc> *names,
6317 vec<cxx_saved_binding, va_gc> **old_bindings)
6319 static vec<tree> bindings_need_stored;
6320 size_t i;
6321 cp_class_binding *cb;
6323 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6324 for (i = 0; vec_safe_iterate (names, i, &cb); ++i)
6325 if (store_binding_p (cb->identifier))
6326 bindings_need_stored.safe_push (cb->identifier);
6327 if (!bindings_need_stored.is_empty ())
6329 tree id;
6330 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
6331 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
6332 store_binding (id, old_bindings);
6333 bindings_need_stored.truncate (0);
6335 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6338 /* A chain of saved_scope structures awaiting reuse. */
6340 static GTY((deletable)) struct saved_scope *free_saved_scope;
6342 void
6343 push_to_top_level (void)
6345 struct saved_scope *s;
6346 cp_binding_level *b;
6347 cxx_saved_binding *sb;
6348 size_t i;
6349 bool need_pop;
6351 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6353 /* Reuse or create a new structure for this saved scope. */
6354 if (free_saved_scope != NULL)
6356 s = free_saved_scope;
6357 free_saved_scope = s->prev;
6359 vec<cxx_saved_binding, va_gc> *old_bindings = s->old_bindings;
6360 memset (s, 0, sizeof (*s));
6361 /* Also reuse the structure's old_bindings vector. */
6362 vec_safe_truncate (old_bindings, 0);
6363 s->old_bindings = old_bindings;
6365 else
6366 s = ggc_cleared_alloc<saved_scope> ();
6368 b = scope_chain ? current_binding_level : 0;
6370 /* If we're in the middle of some function, save our state. */
6371 if (cfun)
6373 need_pop = true;
6374 push_function_context ();
6376 else
6377 need_pop = false;
6379 if (scope_chain && previous_class_level)
6380 store_class_bindings (previous_class_level->class_shadowed,
6381 &s->old_bindings);
6383 /* Have to include the global scope, because class-scope decls
6384 aren't listed anywhere useful. */
6385 for (; b; b = b->level_chain)
6387 tree t;
6389 /* Template IDs are inserted into the global level. If they were
6390 inserted into namespace level, finish_file wouldn't find them
6391 when doing pending instantiations. Therefore, don't stop at
6392 namespace level, but continue until :: . */
6393 if (global_scope_p (b))
6394 break;
6396 store_bindings (b->names, &s->old_bindings);
6397 /* We also need to check class_shadowed to save class-level type
6398 bindings, since pushclass doesn't fill in b->names. */
6399 if (b->kind == sk_class)
6400 store_class_bindings (b->class_shadowed, &s->old_bindings);
6402 /* Unwind type-value slots back to top level. */
6403 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
6404 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
6407 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, sb)
6408 IDENTIFIER_MARKED (sb->identifier) = 0;
6410 s->prev = scope_chain;
6411 s->bindings = b;
6412 s->need_pop_function_context = need_pop;
6413 s->function_decl = current_function_decl;
6414 s->unevaluated_operand = cp_unevaluated_operand;
6415 s->inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
6416 s->x_stmt_tree.stmts_are_full_exprs_p = true;
6418 scope_chain = s;
6419 current_function_decl = NULL_TREE;
6420 vec_alloc (current_lang_base, 10);
6421 current_lang_name = lang_name_cplusplus;
6422 current_namespace = global_namespace;
6423 push_class_stack ();
6424 cp_unevaluated_operand = 0;
6425 c_inhibit_evaluation_warnings = 0;
6426 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6429 static void
6430 pop_from_top_level_1 (void)
6432 struct saved_scope *s = scope_chain;
6433 cxx_saved_binding *saved;
6434 size_t i;
6436 /* Clear out class-level bindings cache. */
6437 if (previous_class_level)
6438 invalidate_class_lookup_cache ();
6439 pop_class_stack ();
6441 current_lang_base = 0;
6443 scope_chain = s->prev;
6444 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, saved)
6446 tree id = saved->identifier;
6448 IDENTIFIER_BINDING (id) = saved->binding;
6449 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
6452 /* If we were in the middle of compiling a function, restore our
6453 state. */
6454 if (s->need_pop_function_context)
6455 pop_function_context ();
6456 current_function_decl = s->function_decl;
6457 cp_unevaluated_operand = s->unevaluated_operand;
6458 c_inhibit_evaluation_warnings = s->inhibit_evaluation_warnings;
6460 /* Make this saved_scope structure available for reuse by
6461 push_to_top_level. */
6462 s->prev = free_saved_scope;
6463 free_saved_scope = s;
6466 /* Wrapper for pop_from_top_level_1. */
6468 void
6469 pop_from_top_level (void)
6471 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6472 pop_from_top_level_1 ();
6473 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6477 /* Pop off extraneous binding levels left over due to syntax errors.
6479 We don't pop past namespaces, as they might be valid. */
6481 void
6482 pop_everything (void)
6484 if (ENABLE_SCOPE_CHECKING)
6485 verbatim ("XXX entering pop_everything ()\n");
6486 while (!toplevel_bindings_p ())
6488 if (current_binding_level->kind == sk_class)
6489 pop_nested_class ();
6490 else
6491 poplevel (0, 0, 0);
6493 if (ENABLE_SCOPE_CHECKING)
6494 verbatim ("XXX leaving pop_everything ()\n");
6497 /* Emit debugging information for using declarations and directives.
6498 If input tree is overloaded fn then emit debug info for all
6499 candidates. */
6501 void
6502 cp_emit_debug_info_for_using (tree t, tree context)
6504 /* Don't try to emit any debug information if we have errors. */
6505 if (seen_error ())
6506 return;
6508 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
6509 of a builtin function. */
6510 if (TREE_CODE (t) == FUNCTION_DECL
6511 && DECL_EXTERNAL (t)
6512 && DECL_BUILT_IN (t))
6513 return;
6515 /* Do not supply context to imported_module_or_decl, if
6516 it is a global namespace. */
6517 if (context == global_namespace)
6518 context = NULL_TREE;
6520 if (BASELINK_P (t))
6521 t = BASELINK_FUNCTIONS (t);
6523 /* FIXME: Handle TEMPLATE_DECLs. */
6524 for (t = OVL_CURRENT (t); t; t = OVL_NEXT (t))
6525 if (TREE_CODE (t) != TEMPLATE_DECL)
6527 if (building_stmt_list_p ())
6528 add_stmt (build_stmt (input_location, USING_STMT, t));
6529 else
6530 (*debug_hooks->imported_module_or_decl) (t, NULL_TREE, context, false);
6534 #include "gt-cp-name-lookup.h"