2017-02-19 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / cp / name-lookup.c
blob994f7f0c5c36eb6e89fcb76bc920c8e45fbbc52e
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 /* The binding oracle; see cp-tree.h. */
97 cp_binding_oracle_function *cp_binding_oracle;
99 /* If we have a binding oracle, ask it for all namespace-scoped
100 definitions of NAME. */
102 static inline void
103 query_oracle (tree name)
105 if (!cp_binding_oracle)
106 return;
108 /* LOOKED_UP holds the set of identifiers that we have already
109 looked up with the oracle. */
110 static hash_set<tree> looked_up;
111 if (looked_up.add (name))
112 return;
114 cp_binding_oracle (CP_ORACLE_IDENTIFIER, name);
117 /* Create a binding_entry object for (NAME, TYPE). */
119 static inline binding_entry
120 binding_entry_make (tree name, tree type)
122 binding_entry entry;
124 if (free_binding_entry)
126 entry = free_binding_entry;
127 free_binding_entry = entry->chain;
129 else
130 entry = ggc_alloc<binding_entry_s> ();
132 entry->name = name;
133 entry->type = type;
134 entry->chain = NULL;
136 return entry;
139 /* Put ENTRY back on the free list. */
140 #if 0
141 static inline void
142 binding_entry_free (binding_entry entry)
144 entry->name = NULL;
145 entry->type = NULL;
146 entry->chain = free_binding_entry;
147 free_binding_entry = entry;
149 #endif
151 /* The datatype used to implement the mapping from names to types at
152 a given scope. */
153 struct GTY(()) binding_table_s {
154 /* Array of chains of "binding_entry"s */
155 binding_entry * GTY((length ("%h.chain_count"))) chain;
157 /* The number of chains in this table. This is the length of the
158 member "chain" considered as an array. */
159 size_t chain_count;
161 /* Number of "binding_entry"s in this table. */
162 size_t entry_count;
165 /* Construct TABLE with an initial CHAIN_COUNT. */
167 static inline void
168 binding_table_construct (binding_table table, size_t chain_count)
170 table->chain_count = chain_count;
171 table->entry_count = 0;
172 table->chain = ggc_cleared_vec_alloc<binding_entry> (table->chain_count);
175 /* Make TABLE's entries ready for reuse. */
176 #if 0
177 static void
178 binding_table_free (binding_table table)
180 size_t i;
181 size_t count;
183 if (table == NULL)
184 return;
186 for (i = 0, count = table->chain_count; i < count; ++i)
188 binding_entry temp = table->chain[i];
189 while (temp != NULL)
191 binding_entry entry = temp;
192 temp = entry->chain;
193 binding_entry_free (entry);
195 table->chain[i] = NULL;
197 table->entry_count = 0;
199 #endif
201 /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */
203 static inline binding_table
204 binding_table_new (size_t chain_count)
206 binding_table table = ggc_alloc<binding_table_s> ();
207 table->chain = NULL;
208 binding_table_construct (table, chain_count);
209 return table;
212 /* Expand TABLE to twice its current chain_count. */
214 static void
215 binding_table_expand (binding_table table)
217 const size_t old_chain_count = table->chain_count;
218 const size_t old_entry_count = table->entry_count;
219 const size_t new_chain_count = 2 * old_chain_count;
220 binding_entry *old_chains = table->chain;
221 size_t i;
223 binding_table_construct (table, new_chain_count);
224 for (i = 0; i < old_chain_count; ++i)
226 binding_entry entry = old_chains[i];
227 for (; entry != NULL; entry = old_chains[i])
229 const unsigned int hash = IDENTIFIER_HASH_VALUE (entry->name);
230 const size_t j = ENTRY_INDEX (hash, new_chain_count);
232 old_chains[i] = entry->chain;
233 entry->chain = table->chain[j];
234 table->chain[j] = entry;
237 table->entry_count = old_entry_count;
240 /* Insert a binding for NAME to TYPE into TABLE. */
242 static void
243 binding_table_insert (binding_table table, tree name, tree type)
245 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
246 const size_t i = ENTRY_INDEX (hash, table->chain_count);
247 binding_entry entry = binding_entry_make (name, type);
249 entry->chain = table->chain[i];
250 table->chain[i] = entry;
251 ++table->entry_count;
253 if (3 * table->chain_count < 5 * table->entry_count)
254 binding_table_expand (table);
257 /* Return the binding_entry, if any, that maps NAME. */
259 binding_entry
260 binding_table_find (binding_table table, tree name)
262 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
263 binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)];
265 while (entry != NULL && entry->name != name)
266 entry = entry->chain;
268 return entry;
271 /* Apply PROC -- with DATA -- to all entries in TABLE. */
273 void
274 binding_table_foreach (binding_table table, bt_foreach_proc proc, void *data)
276 size_t chain_count;
277 size_t i;
279 if (!table)
280 return;
282 chain_count = table->chain_count;
283 for (i = 0; i < chain_count; ++i)
285 binding_entry entry = table->chain[i];
286 for (; entry != NULL; entry = entry->chain)
287 proc (entry, data);
291 #ifndef ENABLE_SCOPE_CHECKING
292 # define ENABLE_SCOPE_CHECKING 0
293 #else
294 # define ENABLE_SCOPE_CHECKING 1
295 #endif
297 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
299 static GTY((deletable)) cxx_binding *free_bindings;
301 /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
302 field to NULL. */
304 static inline void
305 cxx_binding_init (cxx_binding *binding, tree value, tree type)
307 binding->value = value;
308 binding->type = type;
309 binding->previous = NULL;
312 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
314 static cxx_binding *
315 cxx_binding_make (tree value, tree type)
317 cxx_binding *binding;
318 if (free_bindings)
320 binding = free_bindings;
321 free_bindings = binding->previous;
323 else
324 binding = ggc_alloc<cxx_binding> ();
326 cxx_binding_init (binding, value, type);
328 return binding;
331 /* Put BINDING back on the free list. */
333 static inline void
334 cxx_binding_free (cxx_binding *binding)
336 binding->scope = NULL;
337 binding->previous = free_bindings;
338 free_bindings = binding;
341 /* Create a new binding for NAME (with the indicated VALUE and TYPE
342 bindings) in the class scope indicated by SCOPE. */
344 static cxx_binding *
345 new_class_binding (tree name, tree value, tree type, cp_binding_level *scope)
347 cp_class_binding cb = {cxx_binding_make (value, type), name};
348 cxx_binding *binding = cb.base;
349 vec_safe_push (scope->class_shadowed, cb);
350 binding->scope = scope;
351 return binding;
354 /* Make DECL the innermost binding for ID. The LEVEL is the binding
355 level at which this declaration is being bound. */
357 void
358 push_binding (tree id, tree decl, cp_binding_level* level)
360 cxx_binding *binding;
362 if (level != class_binding_level)
364 binding = cxx_binding_make (decl, NULL_TREE);
365 binding->scope = level;
367 else
368 binding = new_class_binding (id, decl, /*type=*/NULL_TREE, level);
370 /* Now, fill in the binding information. */
371 binding->previous = IDENTIFIER_BINDING (id);
372 INHERITED_VALUE_BINDING_P (binding) = 0;
373 LOCAL_BINDING_P (binding) = (level != class_binding_level);
375 /* And put it on the front of the list of bindings for ID. */
376 IDENTIFIER_BINDING (id) = binding;
379 /* Remove the binding for DECL which should be the innermost binding
380 for ID. */
382 void
383 pop_binding (tree id, tree decl)
385 cxx_binding *binding;
387 if (id == NULL_TREE)
388 /* It's easiest to write the loops that call this function without
389 checking whether or not the entities involved have names. We
390 get here for such an entity. */
391 return;
393 /* Get the innermost binding for ID. */
394 binding = IDENTIFIER_BINDING (id);
396 /* The name should be bound. */
397 gcc_assert (binding != NULL);
399 /* The DECL will be either the ordinary binding or the type
400 binding for this identifier. Remove that binding. */
401 if (binding->value == decl)
402 binding->value = NULL_TREE;
403 else
405 gcc_assert (binding->type == decl);
406 binding->type = NULL_TREE;
409 if (!binding->value && !binding->type)
411 /* We're completely done with the innermost binding for this
412 identifier. Unhook it from the list of bindings. */
413 IDENTIFIER_BINDING (id) = binding->previous;
415 /* Add it to the free list. */
416 cxx_binding_free (binding);
420 /* Remove the bindings for the decls of the current level and leave
421 the current scope. */
423 void
424 pop_bindings_and_leave_scope (void)
426 for (tree t = getdecls (); t; t = DECL_CHAIN (t))
427 pop_binding (DECL_NAME (t), t);
428 leave_scope ();
431 /* Strip non dependent using declarations. If DECL is dependent,
432 surreptitiously create a typename_type and return it. */
434 tree
435 strip_using_decl (tree decl)
437 if (decl == NULL_TREE)
438 return NULL_TREE;
440 while (TREE_CODE (decl) == USING_DECL && !DECL_DEPENDENT_P (decl))
441 decl = USING_DECL_DECLS (decl);
443 if (TREE_CODE (decl) == USING_DECL && DECL_DEPENDENT_P (decl)
444 && USING_DECL_TYPENAME_P (decl))
446 /* We have found a type introduced by a using
447 declaration at class scope that refers to a dependent
448 type.
450 using typename :: [opt] nested-name-specifier unqualified-id ;
452 decl = make_typename_type (TREE_TYPE (decl),
453 DECL_NAME (decl),
454 typename_type, tf_error);
455 if (decl != error_mark_node)
456 decl = TYPE_NAME (decl);
459 return decl;
462 /* BINDING records an existing declaration for a name in the current scope.
463 But, DECL is another declaration for that same identifier in the
464 same scope. This is the `struct stat' hack whereby a non-typedef
465 class name or enum-name can be bound at the same level as some other
466 kind of entity.
467 3.3.7/1
469 A class name (9.1) or enumeration name (7.2) can be hidden by the
470 name of an object, function, or enumerator declared in the same scope.
471 If a class or enumeration name and an object, function, or enumerator
472 are declared in the same scope (in any order) with the same name, the
473 class or enumeration name is hidden wherever the object, function, or
474 enumerator name is visible.
476 It's the responsibility of the caller to check that
477 inserting this name is valid here. Returns nonzero if the new binding
478 was successful. */
480 static bool
481 supplement_binding_1 (cxx_binding *binding, tree decl)
483 tree bval = binding->value;
484 bool ok = true;
485 tree target_bval = strip_using_decl (bval);
486 tree target_decl = strip_using_decl (decl);
488 if (TREE_CODE (target_decl) == TYPE_DECL && DECL_ARTIFICIAL (target_decl)
489 && target_decl != target_bval
490 && (TREE_CODE (target_bval) != TYPE_DECL
491 /* We allow pushing an enum multiple times in a class
492 template in order to handle late matching of underlying
493 type on an opaque-enum-declaration followed by an
494 enum-specifier. */
495 || (processing_template_decl
496 && TREE_CODE (TREE_TYPE (target_decl)) == ENUMERAL_TYPE
497 && TREE_CODE (TREE_TYPE (target_bval)) == ENUMERAL_TYPE
498 && (dependent_type_p (ENUM_UNDERLYING_TYPE
499 (TREE_TYPE (target_decl)))
500 || dependent_type_p (ENUM_UNDERLYING_TYPE
501 (TREE_TYPE (target_bval)))))))
502 /* The new name is the type name. */
503 binding->type = decl;
504 else if (/* TARGET_BVAL is null when push_class_level_binding moves
505 an inherited type-binding out of the way to make room
506 for a new value binding. */
507 !target_bval
508 /* TARGET_BVAL is error_mark_node when TARGET_DECL's name
509 has been used in a non-class scope prior declaration.
510 In that case, we should have already issued a
511 diagnostic; for graceful error recovery purpose, pretend
512 this was the intended declaration for that name. */
513 || target_bval == error_mark_node
514 /* If TARGET_BVAL is anticipated but has not yet been
515 declared, pretend it is not there at all. */
516 || (TREE_CODE (target_bval) == FUNCTION_DECL
517 && DECL_ANTICIPATED (target_bval)
518 && !DECL_HIDDEN_FRIEND_P (target_bval)))
519 binding->value = decl;
520 else if (TREE_CODE (target_bval) == TYPE_DECL
521 && DECL_ARTIFICIAL (target_bval)
522 && target_decl != target_bval
523 && (TREE_CODE (target_decl) != TYPE_DECL
524 || same_type_p (TREE_TYPE (target_decl),
525 TREE_TYPE (target_bval))))
527 /* The old binding was a type name. It was placed in
528 VALUE field because it was thought, at the point it was
529 declared, to be the only entity with such a name. Move the
530 type name into the type slot; it is now hidden by the new
531 binding. */
532 binding->type = bval;
533 binding->value = decl;
534 binding->value_is_inherited = false;
536 else if (TREE_CODE (target_bval) == TYPE_DECL
537 && TREE_CODE (target_decl) == TYPE_DECL
538 && DECL_NAME (target_decl) == DECL_NAME (target_bval)
539 && binding->scope->kind != sk_class
540 && (same_type_p (TREE_TYPE (target_decl), TREE_TYPE (target_bval))
541 /* If either type involves template parameters, we must
542 wait until instantiation. */
543 || uses_template_parms (TREE_TYPE (target_decl))
544 || uses_template_parms (TREE_TYPE (target_bval))))
545 /* We have two typedef-names, both naming the same type to have
546 the same name. In general, this is OK because of:
548 [dcl.typedef]
550 In a given scope, a typedef specifier can be used to redefine
551 the name of any type declared in that scope to refer to the
552 type to which it already refers.
554 However, in class scopes, this rule does not apply due to the
555 stricter language in [class.mem] prohibiting redeclarations of
556 members. */
557 ok = false;
558 /* There can be two block-scope declarations of the same variable,
559 so long as they are `extern' declarations. However, there cannot
560 be two declarations of the same static data member:
562 [class.mem]
564 A member shall not be declared twice in the
565 member-specification. */
566 else if (VAR_P (target_decl)
567 && VAR_P (target_bval)
568 && DECL_EXTERNAL (target_decl) && DECL_EXTERNAL (target_bval)
569 && !DECL_CLASS_SCOPE_P (target_decl))
571 duplicate_decls (decl, binding->value, /*newdecl_is_friend=*/false);
572 ok = false;
574 else if (TREE_CODE (decl) == NAMESPACE_DECL
575 && TREE_CODE (bval) == NAMESPACE_DECL
576 && DECL_NAMESPACE_ALIAS (decl)
577 && DECL_NAMESPACE_ALIAS (bval)
578 && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl))
579 /* [namespace.alias]
581 In a declarative region, a namespace-alias-definition can be
582 used to redefine a namespace-alias declared in that declarative
583 region to refer only to the namespace to which it already
584 refers. */
585 ok = false;
586 else if (maybe_remove_implicit_alias (bval))
588 /* There was a mangling compatibility alias using this mangled name,
589 but now we have a real decl that wants to use it instead. */
590 binding->value = decl;
592 else
594 if (!error_operand_p (bval))
595 diagnose_name_conflict (decl, bval);
596 ok = false;
599 return ok;
602 /* Diagnose a name conflict between DECL and BVAL. */
604 static void
605 diagnose_name_conflict (tree decl, tree bval)
607 if (TREE_CODE (decl) == TREE_CODE (bval)
608 && (TREE_CODE (decl) != TYPE_DECL
609 || (DECL_ARTIFICIAL (decl) && DECL_ARTIFICIAL (bval))
610 || (!DECL_ARTIFICIAL (decl) && !DECL_ARTIFICIAL (bval)))
611 && !is_overloaded_fn (decl))
612 error ("redeclaration of %q#D", decl);
613 else
614 error ("%q#D conflicts with a previous declaration", decl);
616 inform (location_of (bval), "previous declaration %q#D", bval);
619 /* Wrapper for supplement_binding_1. */
621 static bool
622 supplement_binding (cxx_binding *binding, tree decl)
624 bool ret;
625 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
626 ret = supplement_binding_1 (binding, decl);
627 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
628 return ret;
631 /* Add DECL to the list of things declared in B. */
633 static void
634 add_decl_to_level (tree decl, cp_binding_level *b)
636 /* We used to record virtual tables as if they were ordinary
637 variables, but no longer do so. */
638 gcc_assert (!(VAR_P (decl) && DECL_VIRTUAL_P (decl)));
640 if (TREE_CODE (decl) == NAMESPACE_DECL
641 && !DECL_NAMESPACE_ALIAS (decl))
643 DECL_CHAIN (decl) = b->namespaces;
644 b->namespaces = decl;
646 else
648 /* We build up the list in reverse order, and reverse it later if
649 necessary. */
650 TREE_CHAIN (decl) = b->names;
651 b->names = decl;
653 /* If appropriate, add decl to separate list of statics. We
654 include extern variables because they might turn out to be
655 static later. It's OK for this list to contain a few false
656 positives. */
657 if (b->kind == sk_namespace)
658 if ((VAR_P (decl)
659 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
660 || (TREE_CODE (decl) == FUNCTION_DECL
661 && (!TREE_PUBLIC (decl)
662 || decl_anon_ns_mem_p (decl)
663 || DECL_DECLARED_INLINE_P (decl))))
664 vec_safe_push (b->static_decls, decl);
668 /* Record a decl-node X as belonging to the current lexical scope.
669 Check for errors (such as an incompatible declaration for the same
670 name already seen in the same scope). IS_FRIEND is true if X is
671 declared as a friend.
673 Returns either X or an old decl for the same name.
674 If an old decl is returned, it may have been smashed
675 to agree with what X says. */
677 static tree
678 pushdecl_maybe_friend_1 (tree x, bool is_friend)
680 tree t;
681 tree name;
682 int need_new_binding;
684 if (x == error_mark_node)
685 return error_mark_node;
687 need_new_binding = 1;
689 if (DECL_TEMPLATE_PARM_P (x))
690 /* Template parameters have no context; they are not X::T even
691 when declared within a class or namespace. */
693 else
695 if (current_function_decl && x != current_function_decl
696 /* A local declaration for a function doesn't constitute
697 nesting. */
698 && TREE_CODE (x) != FUNCTION_DECL
699 /* A local declaration for an `extern' variable is in the
700 scope of the current namespace, not the current
701 function. */
702 && !(VAR_P (x) && DECL_EXTERNAL (x))
703 /* When parsing the parameter list of a function declarator,
704 don't set DECL_CONTEXT to an enclosing function. When we
705 push the PARM_DECLs in order to process the function body,
706 current_binding_level->this_entity will be set. */
707 && !(TREE_CODE (x) == PARM_DECL
708 && current_binding_level->kind == sk_function_parms
709 && current_binding_level->this_entity == NULL)
710 && !DECL_CONTEXT (x))
711 DECL_CONTEXT (x) = current_function_decl;
713 /* If this is the declaration for a namespace-scope function,
714 but the declaration itself is in a local scope, mark the
715 declaration. */
716 if (TREE_CODE (x) == FUNCTION_DECL
717 && DECL_NAMESPACE_SCOPE_P (x)
718 && current_function_decl
719 && x != current_function_decl)
720 DECL_LOCAL_FUNCTION_P (x) = 1;
723 name = DECL_NAME (x);
724 if (name)
726 int different_binding_level = 0;
728 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
729 name = TREE_OPERAND (name, 0);
731 /* In case this decl was explicitly namespace-qualified, look it
732 up in its namespace context. */
733 if (DECL_NAMESPACE_SCOPE_P (x) && namespace_bindings_p ())
734 t = namespace_binding (name, DECL_CONTEXT (x));
735 else
736 t = lookup_name_innermost_nonclass_level (name);
738 /* [basic.link] If there is a visible declaration of an entity
739 with linkage having the same name and type, ignoring entities
740 declared outside the innermost enclosing namespace scope, the
741 block scope declaration declares that same entity and
742 receives the linkage of the previous declaration. */
743 if (! t && current_function_decl && x != current_function_decl
744 && VAR_OR_FUNCTION_DECL_P (x)
745 && DECL_EXTERNAL (x))
747 /* Look in block scope. */
748 t = innermost_non_namespace_value (name);
749 /* Or in the innermost namespace. */
750 if (! t)
751 t = namespace_binding (name, DECL_CONTEXT (x));
752 /* Does it have linkage? Note that if this isn't a DECL, it's an
753 OVERLOAD, which is OK. */
754 if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t)))
755 t = NULL_TREE;
756 if (t)
757 different_binding_level = 1;
760 /* If we are declaring a function, and the result of name-lookup
761 was an OVERLOAD, look for an overloaded instance that is
762 actually the same as the function we are declaring. (If
763 there is one, we have to merge our declaration with the
764 previous declaration.) */
765 if (t && TREE_CODE (t) == OVERLOAD)
767 tree match;
769 if (TREE_CODE (x) == FUNCTION_DECL)
770 for (match = t; match; match = OVL_NEXT (match))
772 if (decls_match (OVL_CURRENT (match), x))
773 break;
775 else
776 /* Just choose one. */
777 match = t;
779 if (match)
780 t = OVL_CURRENT (match);
781 else
782 t = NULL_TREE;
785 if (t && t != error_mark_node)
787 if (different_binding_level)
789 if (decls_match (x, t))
790 /* The standard only says that the local extern
791 inherits linkage from the previous decl; in
792 particular, default args are not shared. Add
793 the decl into a hash table to make sure only
794 the previous decl in this case is seen by the
795 middle end. */
797 struct cxx_int_tree_map *h;
799 TREE_PUBLIC (x) = TREE_PUBLIC (t);
801 if (cp_function_chain->extern_decl_map == NULL)
802 cp_function_chain->extern_decl_map
803 = hash_table<cxx_int_tree_map_hasher>::create_ggc (20);
805 h = ggc_alloc<cxx_int_tree_map> ();
806 h->uid = DECL_UID (x);
807 h->to = t;
808 cxx_int_tree_map **loc = cp_function_chain->extern_decl_map
809 ->find_slot (h, INSERT);
810 *loc = h;
813 else if (TREE_CODE (t) == PARM_DECL)
815 /* Check for duplicate params. */
816 tree d = duplicate_decls (x, t, is_friend);
817 if (d)
818 return d;
820 else if ((DECL_EXTERN_C_FUNCTION_P (x)
821 || DECL_FUNCTION_TEMPLATE_P (x))
822 && is_overloaded_fn (t))
823 /* Don't do anything just yet. */;
824 else if (t == wchar_decl_node)
826 if (! DECL_IN_SYSTEM_HEADER (x))
827 pedwarn (input_location, OPT_Wpedantic, "redeclaration of %<wchar_t%> as %qT",
828 TREE_TYPE (x));
830 /* Throw away the redeclaration. */
831 return t;
833 else
835 tree olddecl = duplicate_decls (x, t, is_friend);
837 /* If the redeclaration failed, we can stop at this
838 point. */
839 if (olddecl == error_mark_node)
840 return error_mark_node;
842 if (olddecl)
844 if (TREE_CODE (t) == TYPE_DECL)
845 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
847 return t;
849 else if (DECL_MAIN_P (x) && TREE_CODE (t) == FUNCTION_DECL)
851 /* A redeclaration of main, but not a duplicate of the
852 previous one.
854 [basic.start.main]
856 This function shall not be overloaded. */
857 error ("invalid redeclaration of %q+D", t);
858 error ("as %qD", x);
859 /* We don't try to push this declaration since that
860 causes a crash. */
861 return x;
866 /* If x has C linkage-specification, (extern "C"),
867 lookup its binding, in case it's already bound to an object.
868 The lookup is done in all namespaces.
869 If we find an existing binding, make sure it has the same
870 exception specification as x, otherwise, bail in error [7.5, 7.6]. */
871 if ((TREE_CODE (x) == FUNCTION_DECL)
872 && DECL_EXTERN_C_P (x)
873 /* We should ignore declarations happening in system headers. */
874 && !DECL_ARTIFICIAL (x)
875 && !DECL_IN_SYSTEM_HEADER (x))
877 tree previous = lookup_extern_c_fun_in_all_ns (x);
878 if (previous
879 && !DECL_ARTIFICIAL (previous)
880 && !DECL_IN_SYSTEM_HEADER (previous)
881 && DECL_CONTEXT (previous) != DECL_CONTEXT (x))
883 /* In case either x or previous is declared to throw an exception,
884 make sure both exception specifications are equal. */
885 if (decls_match (x, previous))
887 tree x_exception_spec = NULL_TREE;
888 tree previous_exception_spec = NULL_TREE;
890 x_exception_spec =
891 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (x));
892 previous_exception_spec =
893 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (previous));
894 if (!comp_except_specs (previous_exception_spec,
895 x_exception_spec,
896 ce_normal))
898 pedwarn (input_location, 0,
899 "declaration of %q#D with C language linkage",
901 pedwarn (DECL_SOURCE_LOCATION (previous), 0,
902 "conflicts with previous declaration %q#D",
903 previous);
904 pedwarn (input_location, 0,
905 "due to different exception specifications");
906 return error_mark_node;
908 if (DECL_ASSEMBLER_NAME_SET_P (previous))
909 SET_DECL_ASSEMBLER_NAME (x,
910 DECL_ASSEMBLER_NAME (previous));
912 else
914 pedwarn (input_location, 0,
915 "declaration of %q#D with C language linkage", x);
916 pedwarn (DECL_SOURCE_LOCATION (previous), 0,
917 "conflicts with previous declaration %q#D",
918 previous);
923 check_template_shadow (x);
925 /* If this is a function conjured up by the back end, massage it
926 so it looks friendly. */
927 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x))
929 retrofit_lang_decl (x);
930 SET_DECL_LANGUAGE (x, lang_c);
933 t = x;
934 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x))
936 t = push_overloaded_decl (x, PUSH_LOCAL, is_friend);
937 if (!namespace_bindings_p ())
938 /* We do not need to create a binding for this name;
939 push_overloaded_decl will have already done so if
940 necessary. */
941 need_new_binding = 0;
943 else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
945 t = push_overloaded_decl (x, PUSH_GLOBAL, is_friend);
946 if (t == x)
947 add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
950 if (DECL_DECLARES_FUNCTION_P (t))
952 check_default_args (t);
954 if (is_friend && t == x && !flag_friend_injection)
956 /* This is a new friend declaration of a function or a
957 function template, so hide it from ordinary function
958 lookup. */
959 DECL_ANTICIPATED (t) = 1;
960 DECL_HIDDEN_FRIEND_P (t) = 1;
964 if (t != x || DECL_FUNCTION_TEMPLATE_P (t))
965 return t;
967 /* If declaring a type as a typedef, copy the type (unless we're
968 at line 0), and install this TYPE_DECL as the new type's typedef
969 name. See the extensive comment of set_underlying_type (). */
970 if (TREE_CODE (x) == TYPE_DECL)
972 tree type = TREE_TYPE (x);
974 if (DECL_IS_BUILTIN (x)
975 || (TREE_TYPE (x) != error_mark_node
976 && TYPE_NAME (type) != x
977 /* We don't want to copy the type when all we're
978 doing is making a TYPE_DECL for the purposes of
979 inlining. */
980 && (!TYPE_NAME (type)
981 || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x))))
982 set_underlying_type (x);
984 if (type != error_mark_node
985 && TYPE_IDENTIFIER (type))
986 set_identifier_type_value (DECL_NAME (x), x);
988 /* If this is a locally defined typedef in a function that
989 is not a template instantation, record it to implement
990 -Wunused-local-typedefs. */
991 if (!instantiating_current_function_p ())
992 record_locally_defined_typedef (x);
995 /* Multiple external decls of the same identifier ought to match.
997 We get warnings about inline functions where they are defined.
998 We get warnings about other functions from push_overloaded_decl.
1000 Avoid duplicate warnings where they are used. */
1001 if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
1003 tree decl;
1005 decl = IDENTIFIER_NAMESPACE_VALUE (name);
1006 if (decl && TREE_CODE (decl) == OVERLOAD)
1007 decl = OVL_FUNCTION (decl);
1009 if (decl && decl != error_mark_node
1010 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl))
1011 /* If different sort of thing, we already gave an error. */
1012 && TREE_CODE (decl) == TREE_CODE (x)
1013 && !comptypes (TREE_TYPE (x), TREE_TYPE (decl),
1014 COMPARE_REDECLARATION))
1016 if (permerror (input_location, "type mismatch with previous "
1017 "external decl of %q#D", x))
1018 inform (DECL_SOURCE_LOCATION (decl),
1019 "previous external decl of %q#D", decl);
1023 /* This name is new in its binding level.
1024 Install the new declaration and return it. */
1025 if (namespace_bindings_p ())
1027 /* Install a global value. */
1029 /* If the first global decl has external linkage,
1030 warn if we later see static one. */
1031 if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
1032 TREE_PUBLIC (name) = 1;
1034 /* Bind the name for the entity. */
1035 if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
1036 && t != NULL_TREE)
1037 && (TREE_CODE (x) == TYPE_DECL
1038 || VAR_P (x)
1039 || TREE_CODE (x) == NAMESPACE_DECL
1040 || TREE_CODE (x) == CONST_DECL
1041 || TREE_CODE (x) == TEMPLATE_DECL))
1042 SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
1044 /* If new decl is `static' and an `extern' was seen previously,
1045 warn about it. */
1046 if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
1047 warn_extern_redeclared_static (x, t);
1049 else
1051 /* Here to install a non-global value. */
1052 tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
1053 tree oldlocal = NULL_TREE;
1054 cp_binding_level *oldscope = NULL;
1055 cxx_binding *oldbinding = outer_binding (name, NULL, true);
1056 if (oldbinding)
1058 oldlocal = oldbinding->value;
1059 oldscope = oldbinding->scope;
1062 if (need_new_binding)
1064 push_local_binding (name, x, 0);
1065 /* Because push_local_binding will hook X on to the
1066 current_binding_level's name list, we don't want to
1067 do that again below. */
1068 need_new_binding = 0;
1071 /* If this is a TYPE_DECL, push it into the type value slot. */
1072 if (TREE_CODE (x) == TYPE_DECL)
1073 set_identifier_type_value (name, x);
1075 /* Clear out any TYPE_DECL shadowed by a namespace so that
1076 we won't think this is a type. The C struct hack doesn't
1077 go through namespaces. */
1078 if (TREE_CODE (x) == NAMESPACE_DECL)
1079 set_identifier_type_value (name, NULL_TREE);
1081 if (oldlocal)
1083 tree d = oldlocal;
1085 while (oldlocal
1086 && VAR_P (oldlocal)
1087 && DECL_DEAD_FOR_LOCAL (oldlocal))
1088 oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal);
1090 if (oldlocal == NULL_TREE)
1091 oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d));
1094 /* If this is an extern function declaration, see if we
1095 have a global definition or declaration for the function. */
1096 if (oldlocal == NULL_TREE
1097 && DECL_EXTERNAL (x)
1098 && oldglobal != NULL_TREE
1099 && TREE_CODE (x) == FUNCTION_DECL
1100 && TREE_CODE (oldglobal) == FUNCTION_DECL)
1102 /* We have one. Their types must agree. */
1103 if (decls_match (x, oldglobal))
1104 /* OK */;
1105 else
1107 warning (0, "extern declaration of %q#D doesn%'t match", x);
1108 warning_at (DECL_SOURCE_LOCATION (oldglobal), 0,
1109 "global declaration %q#D", oldglobal);
1112 /* If we have a local external declaration,
1113 and no file-scope declaration has yet been seen,
1114 then if we later have a file-scope decl it must not be static. */
1115 if (oldlocal == NULL_TREE
1116 && oldglobal == NULL_TREE
1117 && DECL_EXTERNAL (x)
1118 && TREE_PUBLIC (x))
1119 TREE_PUBLIC (name) = 1;
1121 /* Don't complain about the parms we push and then pop
1122 while tentatively parsing a function declarator. */
1123 if (TREE_CODE (x) == PARM_DECL && DECL_CONTEXT (x) == NULL_TREE)
1124 /* Ignore. */;
1126 /* Warn if shadowing an argument at the top level of the body. */
1127 else if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
1128 /* Inline decls shadow nothing. */
1129 && !DECL_FROM_INLINE (x)
1130 && (TREE_CODE (oldlocal) == PARM_DECL
1131 || VAR_P (oldlocal)
1132 /* If the old decl is a type decl, only warn if the
1133 old decl is an explicit typedef or if both the old
1134 and new decls are type decls. */
1135 || (TREE_CODE (oldlocal) == TYPE_DECL
1136 && (!DECL_ARTIFICIAL (oldlocal)
1137 || TREE_CODE (x) == TYPE_DECL)))
1138 /* Don't check for internally generated vars unless
1139 it's an implicit typedef (see create_implicit_typedef
1140 in decl.c) or anonymous union variable. */
1141 && (!DECL_ARTIFICIAL (x)
1142 || DECL_IMPLICIT_TYPEDEF_P (x)
1143 || (VAR_P (x) && DECL_ANON_UNION_VAR_P (x))))
1145 bool nowarn = false;
1147 /* Don't complain if it's from an enclosing function. */
1148 if (DECL_CONTEXT (oldlocal) == current_function_decl
1149 && TREE_CODE (x) != PARM_DECL
1150 && TREE_CODE (oldlocal) == PARM_DECL)
1152 /* Go to where the parms should be and see if we find
1153 them there. */
1154 cp_binding_level *b = current_binding_level->level_chain;
1156 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
1157 /* Skip the ctor/dtor cleanup level. */
1158 b = b->level_chain;
1160 /* ARM $8.3 */
1161 if (b->kind == sk_function_parms)
1163 error ("declaration of %q#D shadows a parameter", x);
1164 nowarn = true;
1168 /* The local structure or class can't use parameters of
1169 the containing function anyway. */
1170 if (DECL_CONTEXT (oldlocal) != current_function_decl)
1172 cp_binding_level *scope = current_binding_level;
1173 tree context = DECL_CONTEXT (oldlocal);
1174 for (; scope; scope = scope->level_chain)
1176 if (scope->kind == sk_function_parms
1177 && scope->this_entity == context)
1178 break;
1179 if (scope->kind == sk_class
1180 && !LAMBDA_TYPE_P (scope->this_entity))
1182 nowarn = true;
1183 break;
1187 /* Error if redeclaring a local declared in a
1188 init-statement or in the condition of an if or
1189 switch statement when the new declaration is in the
1190 outermost block of the controlled statement.
1191 Redeclaring a variable from a for or while condition is
1192 detected elsewhere. */
1193 else if (VAR_P (oldlocal)
1194 && oldscope == current_binding_level->level_chain
1195 && (oldscope->kind == sk_cond
1196 || oldscope->kind == sk_for))
1198 error ("redeclaration of %q#D", x);
1199 inform (DECL_SOURCE_LOCATION (oldlocal),
1200 "%q#D previously declared here", oldlocal);
1201 nowarn = true;
1203 /* C++11:
1204 3.3.3/3: The name declared in an exception-declaration (...)
1205 shall not be redeclared in the outermost block of the handler.
1206 3.3.3/2: A parameter name shall not be redeclared (...) in
1207 the outermost block of any handler associated with a
1208 function-try-block.
1209 3.4.1/15: The function parameter names shall not be redeclared
1210 in the exception-declaration nor in the outermost block of a
1211 handler for the function-try-block. */
1212 else if ((VAR_P (oldlocal)
1213 && oldscope == current_binding_level->level_chain
1214 && oldscope->kind == sk_catch)
1215 || (TREE_CODE (oldlocal) == PARM_DECL
1216 && (current_binding_level->kind == sk_catch
1217 || (current_binding_level->level_chain->kind
1218 == sk_catch))
1219 && in_function_try_handler))
1221 if (permerror (input_location, "redeclaration of %q#D", x))
1222 inform (DECL_SOURCE_LOCATION (oldlocal),
1223 "%q#D previously declared here", oldlocal);
1224 nowarn = true;
1227 if ((warn_shadow
1228 || warn_shadow_local
1229 || warn_shadow_compatible_local)
1230 && !nowarn)
1232 bool warned;
1233 enum opt_code warning_code;
1234 /* If '-Wshadow=compatible-local' is specified without other
1235 -Wshadow= flags, we will warn only when the type of the
1236 shadowing variable (i.e. x) can be converted to that of
1237 the shadowed parameter (oldlocal). The reason why we only
1238 check if x's type can be converted to oldlocal's type
1239 (but not the other way around) is because when users
1240 accidentally shadow a parameter, more than often they
1241 would use the variable thinking (mistakenly) it's still
1242 the parameter. It would be rare that users would use the
1243 variable in the place that expects the parameter but
1244 thinking it's a new decl. */
1245 if (warn_shadow)
1246 warning_code = OPT_Wshadow;
1247 else if (can_convert (TREE_TYPE (oldlocal), TREE_TYPE (x),
1248 tf_none))
1249 warning_code = OPT_Wshadow_compatible_local;
1250 else
1251 warning_code = OPT_Wshadow_local;
1253 if (TREE_CODE (oldlocal) == PARM_DECL)
1254 warned = warning_at (input_location, warning_code,
1255 "declaration of %q#D shadows a parameter", x);
1256 else if (is_capture_proxy (oldlocal))
1257 warned = warning_at (input_location, warning_code,
1258 "declaration of %qD shadows a lambda capture",
1260 else
1261 warned = warning_at (input_location, warning_code,
1262 "declaration of %qD shadows a previous local",
1265 if (warned)
1266 inform (DECL_SOURCE_LOCATION (oldlocal),
1267 "shadowed declaration is here");
1271 /* Maybe warn if shadowing something else. */
1272 else if (warn_shadow && !DECL_EXTERNAL (x)
1273 /* No shadow warnings for internally generated vars unless
1274 it's an implicit typedef (see create_implicit_typedef
1275 in decl.c). */
1276 && (! DECL_ARTIFICIAL (x) || DECL_IMPLICIT_TYPEDEF_P (x))
1277 /* No shadow warnings for vars made for inlining. */
1278 && ! DECL_FROM_INLINE (x))
1280 tree member;
1282 if (nonlambda_method_basetype ())
1283 member = lookup_member (current_nonlambda_class_type (),
1284 name,
1285 /*protect=*/0,
1286 /*want_type=*/false,
1287 tf_warning_or_error);
1288 else
1289 member = NULL_TREE;
1291 if (member && !TREE_STATIC (member))
1293 if (BASELINK_P (member))
1294 member = BASELINK_FUNCTIONS (member);
1295 member = OVL_CURRENT (member);
1297 /* Do not warn if a variable shadows a function, unless
1298 the variable is a function or a pointer-to-function. */
1299 if (TREE_CODE (member) != FUNCTION_DECL
1300 || TREE_CODE (x) == FUNCTION_DECL
1301 || TYPE_PTRFN_P (TREE_TYPE (x))
1302 || TYPE_PTRMEMFUNC_P (TREE_TYPE (x)))
1304 if (warning_at (input_location, OPT_Wshadow,
1305 "declaration of %qD shadows a member of %qT",
1306 x, current_nonlambda_class_type ())
1307 && DECL_P (member))
1308 inform (DECL_SOURCE_LOCATION (member),
1309 "shadowed declaration is here");
1312 else if (oldglobal != NULL_TREE
1313 && (VAR_P (oldglobal)
1314 /* If the old decl is a type decl, only warn if the
1315 old decl is an explicit typedef or if both the
1316 old and new decls are type decls. */
1317 || (TREE_CODE (oldglobal) == TYPE_DECL
1318 && (!DECL_ARTIFICIAL (oldglobal)
1319 || TREE_CODE (x) == TYPE_DECL)))
1320 && !instantiating_current_function_p ())
1321 /* XXX shadow warnings in outer-more namespaces */
1323 if (warning_at (input_location, OPT_Wshadow,
1324 "declaration of %qD shadows a "
1325 "global declaration", x))
1326 inform (DECL_SOURCE_LOCATION (oldglobal),
1327 "shadowed declaration is here");
1332 if (VAR_P (x))
1333 maybe_register_incomplete_var (x);
1336 if (need_new_binding)
1337 add_decl_to_level (x,
1338 DECL_NAMESPACE_SCOPE_P (x)
1339 ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x))
1340 : current_binding_level);
1342 return x;
1345 /* Wrapper for pushdecl_maybe_friend_1. */
1347 tree
1348 pushdecl_maybe_friend (tree x, bool is_friend)
1350 tree ret;
1351 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
1352 ret = pushdecl_maybe_friend_1 (x, is_friend);
1353 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
1354 return ret;
1357 /* Record a decl-node X as belonging to the current lexical scope. */
1359 tree
1360 pushdecl (tree x)
1362 return pushdecl_maybe_friend (x, false);
1365 /* Enter DECL into the symbol table, if that's appropriate. Returns
1366 DECL, or a modified version thereof. */
1368 tree
1369 maybe_push_decl (tree decl)
1371 tree type = TREE_TYPE (decl);
1373 /* Add this decl to the current binding level, but not if it comes
1374 from another scope, e.g. a static member variable. TEM may equal
1375 DECL or it may be a previous decl of the same name. */
1376 if (decl == error_mark_node
1377 || (TREE_CODE (decl) != PARM_DECL
1378 && DECL_CONTEXT (decl) != NULL_TREE
1379 /* Definitions of namespace members outside their namespace are
1380 possible. */
1381 && !DECL_NAMESPACE_SCOPE_P (decl))
1382 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
1383 || type == unknown_type_node
1384 /* The declaration of a template specialization does not affect
1385 the functions available for overload resolution, so we do not
1386 call pushdecl. */
1387 || (TREE_CODE (decl) == FUNCTION_DECL
1388 && DECL_TEMPLATE_SPECIALIZATION (decl)))
1389 return decl;
1390 else
1391 return pushdecl (decl);
1394 /* Bind DECL to ID in the current_binding_level, assumed to be a local
1395 binding level. If PUSH_USING is set in FLAGS, we know that DECL
1396 doesn't really belong to this binding level, that it got here
1397 through a using-declaration. */
1399 void
1400 push_local_binding (tree id, tree decl, int flags)
1402 cp_binding_level *b;
1404 /* Skip over any local classes. This makes sense if we call
1405 push_local_binding with a friend decl of a local class. */
1406 b = innermost_nonclass_level ();
1408 if (lookup_name_innermost_nonclass_level (id))
1410 /* Supplement the existing binding. */
1411 if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
1412 /* It didn't work. Something else must be bound at this
1413 level. Do not add DECL to the list of things to pop
1414 later. */
1415 return;
1417 else
1418 /* Create a new binding. */
1419 push_binding (id, decl, b);
1421 if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
1422 /* We must put the OVERLOAD into a TREE_LIST since the
1423 TREE_CHAIN of an OVERLOAD is already used. Similarly for
1424 decls that got here through a using-declaration. */
1425 decl = build_tree_list (NULL_TREE, decl);
1427 /* And put DECL on the list of things declared by the current
1428 binding level. */
1429 add_decl_to_level (decl, b);
1432 /* Check to see whether or not DECL is a variable that would have been
1433 in scope under the ARM, but is not in scope under the ANSI/ISO
1434 standard. If so, issue an error message. If name lookup would
1435 work in both cases, but return a different result, this function
1436 returns the result of ANSI/ISO lookup. Otherwise, it returns
1437 DECL. */
1439 tree
1440 check_for_out_of_scope_variable (tree decl)
1442 tree shadowed;
1444 /* We only care about out of scope variables. */
1445 if (!(VAR_P (decl) && DECL_DEAD_FOR_LOCAL (decl)))
1446 return decl;
1448 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (decl)
1449 ? DECL_SHADOWED_FOR_VAR (decl) : NULL_TREE ;
1450 while (shadowed != NULL_TREE && VAR_P (shadowed)
1451 && DECL_DEAD_FOR_LOCAL (shadowed))
1452 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (shadowed)
1453 ? DECL_SHADOWED_FOR_VAR (shadowed) : NULL_TREE;
1454 if (!shadowed)
1455 shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl));
1456 if (shadowed)
1458 if (!DECL_ERROR_REPORTED (decl))
1460 warning (0, "name lookup of %qD changed", DECL_NAME (decl));
1461 warning_at (DECL_SOURCE_LOCATION (shadowed), 0,
1462 " matches this %qD under ISO standard rules",
1463 shadowed);
1464 warning_at (DECL_SOURCE_LOCATION (decl), 0,
1465 " matches this %qD under old rules", decl);
1466 DECL_ERROR_REPORTED (decl) = 1;
1468 return shadowed;
1471 /* If we have already complained about this declaration, there's no
1472 need to do it again. */
1473 if (DECL_ERROR_REPORTED (decl))
1474 return decl;
1476 DECL_ERROR_REPORTED (decl) = 1;
1478 if (TREE_TYPE (decl) == error_mark_node)
1479 return decl;
1481 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
1483 error ("name lookup of %qD changed for ISO %<for%> scoping",
1484 DECL_NAME (decl));
1485 error (" cannot use obsolete binding at %q+D because "
1486 "it has a destructor", decl);
1487 return error_mark_node;
1489 else
1491 permerror (input_location, "name lookup of %qD changed for ISO %<for%> scoping",
1492 DECL_NAME (decl));
1493 if (flag_permissive)
1494 permerror (DECL_SOURCE_LOCATION (decl),
1495 " using obsolete binding at %qD", decl);
1496 else
1498 static bool hint;
1499 if (!hint)
1501 inform (input_location, "(if you use %<-fpermissive%> G++ will accept your code)");
1502 hint = true;
1507 return decl;
1510 /* true means unconditionally make a BLOCK for the next level pushed. */
1512 static bool keep_next_level_flag;
1514 static int binding_depth = 0;
1516 static void
1517 indent (int depth)
1519 int i;
1521 for (i = 0; i < depth * 2; i++)
1522 putc (' ', stderr);
1525 /* Return a string describing the kind of SCOPE we have. */
1526 static const char *
1527 cp_binding_level_descriptor (cp_binding_level *scope)
1529 /* The order of this table must match the "scope_kind"
1530 enumerators. */
1531 static const char* scope_kind_names[] = {
1532 "block-scope",
1533 "cleanup-scope",
1534 "try-scope",
1535 "catch-scope",
1536 "for-scope",
1537 "function-parameter-scope",
1538 "class-scope",
1539 "namespace-scope",
1540 "template-parameter-scope",
1541 "template-explicit-spec-scope"
1543 const scope_kind kind = scope->explicit_spec_p
1544 ? sk_template_spec : scope->kind;
1546 return scope_kind_names[kind];
1549 /* Output a debugging information about SCOPE when performing
1550 ACTION at LINE. */
1551 static void
1552 cp_binding_level_debug (cp_binding_level *scope, int line, const char *action)
1554 const char *desc = cp_binding_level_descriptor (scope);
1555 if (scope->this_entity)
1556 verbatim ("%s %s(%E) %p %d\n", action, desc,
1557 scope->this_entity, (void *) scope, line);
1558 else
1559 verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
1562 /* Return the estimated initial size of the hashtable of a NAMESPACE
1563 scope. */
1565 static inline size_t
1566 namespace_scope_ht_size (tree ns)
1568 tree name = DECL_NAME (ns);
1570 return name == std_identifier
1571 ? NAMESPACE_STD_HT_SIZE
1572 : (name == global_scope_name
1573 ? GLOBAL_SCOPE_HT_SIZE
1574 : NAMESPACE_ORDINARY_HT_SIZE);
1577 /* A chain of binding_level structures awaiting reuse. */
1579 static GTY((deletable)) cp_binding_level *free_binding_level;
1581 /* Insert SCOPE as the innermost binding level. */
1583 void
1584 push_binding_level (cp_binding_level *scope)
1586 /* Add it to the front of currently active scopes stack. */
1587 scope->level_chain = current_binding_level;
1588 current_binding_level = scope;
1589 keep_next_level_flag = false;
1591 if (ENABLE_SCOPE_CHECKING)
1593 scope->binding_depth = binding_depth;
1594 indent (binding_depth);
1595 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
1596 "push");
1597 binding_depth++;
1601 /* Create a new KIND scope and make it the top of the active scopes stack.
1602 ENTITY is the scope of the associated C++ entity (namespace, class,
1603 function, C++0x enumeration); it is NULL otherwise. */
1605 cp_binding_level *
1606 begin_scope (scope_kind kind, tree entity)
1608 cp_binding_level *scope;
1610 /* Reuse or create a struct for this binding level. */
1611 if (!ENABLE_SCOPE_CHECKING && free_binding_level)
1613 scope = free_binding_level;
1614 free_binding_level = scope->level_chain;
1615 memset (scope, 0, sizeof (cp_binding_level));
1617 else
1618 scope = ggc_cleared_alloc<cp_binding_level> ();
1620 scope->this_entity = entity;
1621 scope->more_cleanups_ok = true;
1622 switch (kind)
1624 case sk_cleanup:
1625 scope->keep = true;
1626 break;
1628 case sk_template_spec:
1629 scope->explicit_spec_p = true;
1630 kind = sk_template_parms;
1631 /* Fall through. */
1632 case sk_template_parms:
1633 case sk_block:
1634 case sk_try:
1635 case sk_catch:
1636 case sk_for:
1637 case sk_cond:
1638 case sk_class:
1639 case sk_scoped_enum:
1640 case sk_function_parms:
1641 case sk_transaction:
1642 case sk_omp:
1643 scope->keep = keep_next_level_flag;
1644 break;
1646 case sk_namespace:
1647 NAMESPACE_LEVEL (entity) = scope;
1648 vec_alloc (scope->static_decls,
1649 (DECL_NAME (entity) == std_identifier
1650 || DECL_NAME (entity) == global_scope_name) ? 200 : 10);
1651 break;
1653 default:
1654 /* Should not happen. */
1655 gcc_unreachable ();
1656 break;
1658 scope->kind = kind;
1660 push_binding_level (scope);
1662 return scope;
1665 /* We're about to leave current scope. Pop the top of the stack of
1666 currently active scopes. Return the enclosing scope, now active. */
1668 cp_binding_level *
1669 leave_scope (void)
1671 cp_binding_level *scope = current_binding_level;
1673 if (scope->kind == sk_namespace && class_binding_level)
1674 current_binding_level = class_binding_level;
1676 /* We cannot leave a scope, if there are none left. */
1677 if (NAMESPACE_LEVEL (global_namespace))
1678 gcc_assert (!global_scope_p (scope));
1680 if (ENABLE_SCOPE_CHECKING)
1682 indent (--binding_depth);
1683 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
1684 "leave");
1687 /* Move one nesting level up. */
1688 current_binding_level = scope->level_chain;
1690 /* Namespace-scopes are left most probably temporarily, not
1691 completely; they can be reopened later, e.g. in namespace-extension
1692 or any name binding activity that requires us to resume a
1693 namespace. For classes, we cache some binding levels. For other
1694 scopes, we just make the structure available for reuse. */
1695 if (scope->kind != sk_namespace
1696 && scope->kind != sk_class)
1698 scope->level_chain = free_binding_level;
1699 gcc_assert (!ENABLE_SCOPE_CHECKING
1700 || scope->binding_depth == binding_depth);
1701 free_binding_level = scope;
1704 if (scope->kind == sk_class)
1706 /* Reset DEFINING_CLASS_P to allow for reuse of a
1707 class-defining scope in a non-defining context. */
1708 scope->defining_class_p = 0;
1710 /* Find the innermost enclosing class scope, and reset
1711 CLASS_BINDING_LEVEL appropriately. */
1712 class_binding_level = NULL;
1713 for (scope = current_binding_level; scope; scope = scope->level_chain)
1714 if (scope->kind == sk_class)
1716 class_binding_level = scope;
1717 break;
1721 return current_binding_level;
1724 static void
1725 resume_scope (cp_binding_level* b)
1727 /* Resuming binding levels is meant only for namespaces,
1728 and those cannot nest into classes. */
1729 gcc_assert (!class_binding_level);
1730 /* Also, resuming a non-directly nested namespace is a no-no. */
1731 gcc_assert (b->level_chain == current_binding_level);
1732 current_binding_level = b;
1733 if (ENABLE_SCOPE_CHECKING)
1735 b->binding_depth = binding_depth;
1736 indent (binding_depth);
1737 cp_binding_level_debug (b, LOCATION_LINE (input_location), "resume");
1738 binding_depth++;
1742 /* Return the innermost binding level that is not for a class scope. */
1744 static cp_binding_level *
1745 innermost_nonclass_level (void)
1747 cp_binding_level *b;
1749 b = current_binding_level;
1750 while (b->kind == sk_class)
1751 b = b->level_chain;
1753 return b;
1756 /* We're defining an object of type TYPE. If it needs a cleanup, but
1757 we're not allowed to add any more objects with cleanups to the current
1758 scope, create a new binding level. */
1760 void
1761 maybe_push_cleanup_level (tree type)
1763 if (type != error_mark_node
1764 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
1765 && current_binding_level->more_cleanups_ok == 0)
1767 begin_scope (sk_cleanup, NULL);
1768 current_binding_level->statement_list = push_stmt_list ();
1772 /* Return true if we are in the global binding level. */
1774 bool
1775 global_bindings_p (void)
1777 return global_scope_p (current_binding_level);
1780 /* True if we are currently in a toplevel binding level. This
1781 means either the global binding level or a namespace in a toplevel
1782 binding level. Since there are no non-toplevel namespace levels,
1783 this really means any namespace or template parameter level. We
1784 also include a class whose context is toplevel. */
1786 bool
1787 toplevel_bindings_p (void)
1789 cp_binding_level *b = innermost_nonclass_level ();
1791 return b->kind == sk_namespace || b->kind == sk_template_parms;
1794 /* True if this is a namespace scope, or if we are defining a class
1795 which is itself at namespace scope, or whose enclosing class is
1796 such a class, etc. */
1798 bool
1799 namespace_bindings_p (void)
1801 cp_binding_level *b = innermost_nonclass_level ();
1803 return b->kind == sk_namespace;
1806 /* True if the innermost non-class scope is a block scope. */
1808 bool
1809 local_bindings_p (void)
1811 cp_binding_level *b = innermost_nonclass_level ();
1812 return b->kind < sk_function_parms || b->kind == sk_omp;
1815 /* True if the current level needs to have a BLOCK made. */
1817 bool
1818 kept_level_p (void)
1820 return (current_binding_level->blocks != NULL_TREE
1821 || current_binding_level->keep
1822 || current_binding_level->kind == sk_cleanup
1823 || current_binding_level->names != NULL_TREE
1824 || current_binding_level->using_directives);
1827 /* Returns the kind of the innermost scope. */
1829 scope_kind
1830 innermost_scope_kind (void)
1832 return current_binding_level->kind;
1835 /* Returns true if this scope was created to store template parameters. */
1837 bool
1838 template_parm_scope_p (void)
1840 return innermost_scope_kind () == sk_template_parms;
1843 /* If KEEP is true, make a BLOCK node for the next binding level,
1844 unconditionally. Otherwise, use the normal logic to decide whether
1845 or not to create a BLOCK. */
1847 void
1848 keep_next_level (bool keep)
1850 keep_next_level_flag = keep;
1853 /* Return the list of declarations of the current level.
1854 Note that this list is in reverse order unless/until
1855 you nreverse it; and when you do nreverse it, you must
1856 store the result back using `storedecls' or you will lose. */
1858 tree
1859 getdecls (void)
1861 return current_binding_level->names;
1864 /* Return how many function prototypes we are currently nested inside. */
1867 function_parm_depth (void)
1869 int level = 0;
1870 cp_binding_level *b;
1872 for (b = current_binding_level;
1873 b->kind == sk_function_parms;
1874 b = b->level_chain)
1875 ++level;
1877 return level;
1880 /* For debugging. */
1881 static int no_print_functions = 0;
1882 static int no_print_builtins = 0;
1884 static void
1885 print_binding_level (cp_binding_level* lvl)
1887 tree t;
1888 int i = 0, len;
1889 fprintf (stderr, " blocks=%p", (void *) lvl->blocks);
1890 if (lvl->more_cleanups_ok)
1891 fprintf (stderr, " more-cleanups-ok");
1892 if (lvl->have_cleanups)
1893 fprintf (stderr, " have-cleanups");
1894 fprintf (stderr, "\n");
1895 if (lvl->names)
1897 fprintf (stderr, " names:\t");
1898 /* We can probably fit 3 names to a line? */
1899 for (t = lvl->names; t; t = TREE_CHAIN (t))
1901 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
1902 continue;
1903 if (no_print_builtins
1904 && (TREE_CODE (t) == TYPE_DECL)
1905 && DECL_IS_BUILTIN (t))
1906 continue;
1908 /* Function decls tend to have longer names. */
1909 if (TREE_CODE (t) == FUNCTION_DECL)
1910 len = 3;
1911 else
1912 len = 2;
1913 i += len;
1914 if (i > 6)
1916 fprintf (stderr, "\n\t");
1917 i = len;
1919 print_node_brief (stderr, "", t, 0);
1920 if (t == error_mark_node)
1921 break;
1923 if (i)
1924 fprintf (stderr, "\n");
1926 if (vec_safe_length (lvl->class_shadowed))
1928 size_t i;
1929 cp_class_binding *b;
1930 fprintf (stderr, " class-shadowed:");
1931 FOR_EACH_VEC_ELT (*lvl->class_shadowed, i, b)
1932 fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
1933 fprintf (stderr, "\n");
1935 if (lvl->type_shadowed)
1937 fprintf (stderr, " type-shadowed:");
1938 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
1940 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1942 fprintf (stderr, "\n");
1946 DEBUG_FUNCTION void
1947 debug (cp_binding_level &ref)
1949 print_binding_level (&ref);
1952 DEBUG_FUNCTION void
1953 debug (cp_binding_level *ptr)
1955 if (ptr)
1956 debug (*ptr);
1957 else
1958 fprintf (stderr, "<nil>\n");
1962 void
1963 print_other_binding_stack (cp_binding_level *stack)
1965 cp_binding_level *level;
1966 for (level = stack; !global_scope_p (level); level = level->level_chain)
1968 fprintf (stderr, "binding level %p\n", (void *) level);
1969 print_binding_level (level);
1973 void
1974 print_binding_stack (void)
1976 cp_binding_level *b;
1977 fprintf (stderr, "current_binding_level=%p\n"
1978 "class_binding_level=%p\n"
1979 "NAMESPACE_LEVEL (global_namespace)=%p\n",
1980 (void *) current_binding_level, (void *) class_binding_level,
1981 (void *) NAMESPACE_LEVEL (global_namespace));
1982 if (class_binding_level)
1984 for (b = class_binding_level; b; b = b->level_chain)
1985 if (b == current_binding_level)
1986 break;
1987 if (b)
1988 b = class_binding_level;
1989 else
1990 b = current_binding_level;
1992 else
1993 b = current_binding_level;
1994 print_other_binding_stack (b);
1995 fprintf (stderr, "global:\n");
1996 print_binding_level (NAMESPACE_LEVEL (global_namespace));
1999 /* Return the type associated with ID. */
2001 static tree
2002 identifier_type_value_1 (tree id)
2004 /* There is no type with that name, anywhere. */
2005 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
2006 return NULL_TREE;
2007 /* This is not the type marker, but the real thing. */
2008 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
2009 return REAL_IDENTIFIER_TYPE_VALUE (id);
2010 /* Have to search for it. It must be on the global level, now.
2011 Ask lookup_name not to return non-types. */
2012 id = lookup_name_real (id, 2, 1, /*block_p=*/true, 0, 0);
2013 if (id)
2014 return TREE_TYPE (id);
2015 return NULL_TREE;
2018 /* Wrapper for identifier_type_value_1. */
2020 tree
2021 identifier_type_value (tree id)
2023 tree ret;
2024 timevar_start (TV_NAME_LOOKUP);
2025 ret = identifier_type_value_1 (id);
2026 timevar_stop (TV_NAME_LOOKUP);
2027 return ret;
2031 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
2032 the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */
2034 tree
2035 identifier_global_value (tree t)
2037 return IDENTIFIER_GLOBAL_VALUE (t);
2040 /* Push a definition of struct, union or enum tag named ID. into
2041 binding_level B. DECL is a TYPE_DECL for the type. We assume that
2042 the tag ID is not already defined. */
2044 static void
2045 set_identifier_type_value_with_scope (tree id, tree decl, cp_binding_level *b)
2047 tree type;
2049 if (b->kind != sk_namespace)
2051 /* Shadow the marker, not the real thing, so that the marker
2052 gets restored later. */
2053 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
2054 b->type_shadowed
2055 = tree_cons (id, old_type_value, b->type_shadowed);
2056 type = decl ? TREE_TYPE (decl) : NULL_TREE;
2057 TREE_TYPE (b->type_shadowed) = type;
2059 else
2061 cxx_binding *binding =
2062 binding_for_name (NAMESPACE_LEVEL (current_namespace), id);
2063 gcc_assert (decl);
2064 if (binding->value)
2065 supplement_binding (binding, decl);
2066 else
2067 binding->value = decl;
2069 /* Store marker instead of real type. */
2070 type = global_type_node;
2072 SET_IDENTIFIER_TYPE_VALUE (id, type);
2075 /* As set_identifier_type_value_with_scope, but using
2076 current_binding_level. */
2078 void
2079 set_identifier_type_value (tree id, tree decl)
2081 set_identifier_type_value_with_scope (id, decl, current_binding_level);
2084 /* Return the name for the constructor (or destructor) for the
2085 specified class TYPE. When given a template, this routine doesn't
2086 lose the specialization. */
2088 static inline tree
2089 constructor_name_full (tree type)
2091 return TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
2094 /* Return the name for the constructor (or destructor) for the
2095 specified class. When given a template, return the plain
2096 unspecialized name. */
2098 tree
2099 constructor_name (tree type)
2101 tree name;
2102 name = constructor_name_full (type);
2103 if (IDENTIFIER_TEMPLATE (name))
2104 name = IDENTIFIER_TEMPLATE (name);
2105 return name;
2108 /* Returns TRUE if NAME is the name for the constructor for TYPE,
2109 which must be a class type. */
2111 bool
2112 constructor_name_p (tree name, tree type)
2114 tree ctor_name;
2116 gcc_assert (MAYBE_CLASS_TYPE_P (type));
2118 if (!name)
2119 return false;
2121 if (!identifier_p (name))
2122 return false;
2124 /* These don't have names. */
2125 if (TREE_CODE (type) == DECLTYPE_TYPE
2126 || TREE_CODE (type) == TYPEOF_TYPE)
2127 return false;
2129 ctor_name = constructor_name_full (type);
2130 if (name == ctor_name)
2131 return true;
2132 if (IDENTIFIER_TEMPLATE (ctor_name)
2133 && name == IDENTIFIER_TEMPLATE (ctor_name))
2134 return true;
2135 return false;
2138 /* Counter used to create anonymous type names. */
2140 static GTY(()) int anon_cnt;
2142 /* Return an IDENTIFIER which can be used as a name for
2143 unnamed structs and unions. */
2145 tree
2146 make_anon_name (void)
2148 char buf[32];
2150 sprintf (buf, anon_aggrname_format (), anon_cnt++);
2151 return get_identifier (buf);
2154 /* This code is practically identical to that for creating
2155 anonymous names, but is just used for lambdas instead. This isn't really
2156 necessary, but it's convenient to avoid treating lambdas like other
2157 unnamed types. */
2159 static GTY(()) int lambda_cnt = 0;
2161 tree
2162 make_lambda_name (void)
2164 char buf[32];
2166 sprintf (buf, LAMBDANAME_FORMAT, lambda_cnt++);
2167 return get_identifier (buf);
2170 /* Return (from the stack of) the BINDING, if any, established at SCOPE. */
2172 static inline cxx_binding *
2173 find_binding (cp_binding_level *scope, cxx_binding *binding)
2175 for (; binding != NULL; binding = binding->previous)
2176 if (binding->scope == scope)
2177 return binding;
2179 return (cxx_binding *)0;
2182 /* Return the binding for NAME in SCOPE, if any. Otherwise, return NULL. */
2184 static inline cxx_binding *
2185 cp_binding_level_find_binding_for_name (cp_binding_level *scope, tree name)
2187 cxx_binding *b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2188 if (b)
2190 /* Fold-in case where NAME is used only once. */
2191 if (scope == b->scope && b->previous == NULL)
2192 return b;
2193 return find_binding (scope, b);
2195 return NULL;
2198 /* Always returns a binding for name in scope. If no binding is
2199 found, make a new one. */
2201 static cxx_binding *
2202 binding_for_name (cp_binding_level *scope, tree name)
2204 cxx_binding *result;
2206 result = cp_binding_level_find_binding_for_name (scope, name);
2207 if (result)
2208 return result;
2209 /* Not found, make a new one. */
2210 result = cxx_binding_make (NULL, NULL);
2211 result->previous = IDENTIFIER_NAMESPACE_BINDINGS (name);
2212 result->scope = scope;
2213 result->is_local = false;
2214 result->value_is_inherited = false;
2215 IDENTIFIER_NAMESPACE_BINDINGS (name) = result;
2216 return result;
2219 /* Walk through the bindings associated to the name of FUNCTION,
2220 and return the first declaration of a function with a
2221 "C" linkage specification, a.k.a 'extern "C"'.
2222 This function looks for the binding, regardless of which scope it
2223 has been defined in. It basically looks in all the known scopes.
2224 Note that this function does not lookup for bindings of builtin functions
2225 or for functions declared in system headers. */
2226 static tree
2227 lookup_extern_c_fun_in_all_ns (tree function)
2229 tree name;
2230 cxx_binding *iter;
2232 gcc_assert (function && TREE_CODE (function) == FUNCTION_DECL);
2234 name = DECL_NAME (function);
2235 gcc_assert (name && identifier_p (name));
2237 for (iter = IDENTIFIER_NAMESPACE_BINDINGS (name);
2238 iter;
2239 iter = iter->previous)
2241 tree ovl;
2242 for (ovl = iter->value; ovl; ovl = OVL_NEXT (ovl))
2244 tree decl = OVL_CURRENT (ovl);
2245 if (decl
2246 && TREE_CODE (decl) == FUNCTION_DECL
2247 && DECL_EXTERN_C_P (decl)
2248 && !DECL_ARTIFICIAL (decl))
2250 return decl;
2254 return NULL;
2257 /* Returns a list of C-linkage decls with the name NAME. */
2259 tree
2260 c_linkage_bindings (tree name)
2262 tree decls = NULL_TREE;
2263 cxx_binding *iter;
2265 for (iter = IDENTIFIER_NAMESPACE_BINDINGS (name);
2266 iter;
2267 iter = iter->previous)
2269 tree ovl;
2270 for (ovl = iter->value; ovl; ovl = OVL_NEXT (ovl))
2272 tree decl = OVL_CURRENT (ovl);
2273 if (decl
2274 && DECL_EXTERN_C_P (decl)
2275 && !DECL_ARTIFICIAL (decl))
2277 if (decls == NULL_TREE)
2278 decls = decl;
2279 else
2280 decls = tree_cons (NULL_TREE, decl, decls);
2284 return decls;
2287 /* Insert another USING_DECL into the current binding level, returning
2288 this declaration. If this is a redeclaration, do nothing, and
2289 return NULL_TREE if this not in namespace scope (in namespace
2290 scope, a using decl might extend any previous bindings). */
2292 static tree
2293 push_using_decl_1 (tree scope, tree name)
2295 tree decl;
2297 gcc_assert (TREE_CODE (scope) == NAMESPACE_DECL);
2298 gcc_assert (identifier_p (name));
2299 for (decl = current_binding_level->usings; decl; decl = DECL_CHAIN (decl))
2300 if (USING_DECL_SCOPE (decl) == scope && DECL_NAME (decl) == name)
2301 break;
2302 if (decl)
2303 return namespace_bindings_p () ? decl : NULL_TREE;
2304 decl = build_lang_decl (USING_DECL, name, NULL_TREE);
2305 USING_DECL_SCOPE (decl) = scope;
2306 DECL_CHAIN (decl) = current_binding_level->usings;
2307 current_binding_level->usings = decl;
2308 return decl;
2311 /* Wrapper for push_using_decl_1. */
2313 static tree
2314 push_using_decl (tree scope, tree name)
2316 tree ret;
2317 timevar_start (TV_NAME_LOOKUP);
2318 ret = push_using_decl_1 (scope, name);
2319 timevar_stop (TV_NAME_LOOKUP);
2320 return ret;
2323 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
2324 caller to set DECL_CONTEXT properly.
2326 Note that this must only be used when X will be the new innermost
2327 binding for its name, as we tack it onto the front of IDENTIFIER_BINDING
2328 without checking to see if the current IDENTIFIER_BINDING comes from a
2329 closer binding level than LEVEL. */
2331 static tree
2332 pushdecl_with_scope_1 (tree x, cp_binding_level *level, bool is_friend)
2334 cp_binding_level *b;
2335 tree function_decl = current_function_decl;
2337 current_function_decl = NULL_TREE;
2338 if (level->kind == sk_class)
2340 b = class_binding_level;
2341 class_binding_level = level;
2342 pushdecl_class_level (x);
2343 class_binding_level = b;
2345 else
2347 b = current_binding_level;
2348 current_binding_level = level;
2349 x = pushdecl_maybe_friend (x, is_friend);
2350 current_binding_level = b;
2352 current_function_decl = function_decl;
2353 return x;
2356 /* Wrapper for pushdecl_with_scope_1. */
2358 tree
2359 pushdecl_with_scope (tree x, cp_binding_level *level, bool is_friend)
2361 tree ret;
2362 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2363 ret = pushdecl_with_scope_1 (x, level, is_friend);
2364 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2365 return ret;
2368 /* Helper function for push_overloaded_decl_1 and do_nonmember_using_decl.
2369 Compares the parameter-type-lists of DECL1 and DECL2 and returns false
2370 if they are different. If the DECLs are template functions, the return
2371 types and the template parameter lists are compared too (DR 565). */
2373 static bool
2374 compparms_for_decl_and_using_decl (tree decl1, tree decl2)
2376 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (decl1)),
2377 TYPE_ARG_TYPES (TREE_TYPE (decl2))))
2378 return false;
2380 if (! DECL_FUNCTION_TEMPLATE_P (decl1)
2381 || ! DECL_FUNCTION_TEMPLATE_P (decl2))
2382 return true;
2384 return (comp_template_parms (DECL_TEMPLATE_PARMS (decl1),
2385 DECL_TEMPLATE_PARMS (decl2))
2386 && same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
2387 TREE_TYPE (TREE_TYPE (decl2))));
2390 /* DECL is a FUNCTION_DECL for a non-member function, which may have
2391 other definitions already in place. We get around this by making
2392 the value of the identifier point to a list of all the things that
2393 want to be referenced by that name. It is then up to the users of
2394 that name to decide what to do with that list.
2396 DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its
2397 DECL_TEMPLATE_RESULT. It is dealt with the same way.
2399 FLAGS is a bitwise-or of the following values:
2400 PUSH_LOCAL: Bind DECL in the current scope, rather than at
2401 namespace scope.
2402 PUSH_USING: DECL is being pushed as the result of a using
2403 declaration.
2405 IS_FRIEND is true if this is a friend declaration.
2407 The value returned may be a previous declaration if we guessed wrong
2408 about what language DECL should belong to (C or C++). Otherwise,
2409 it's always DECL (and never something that's not a _DECL). */
2411 static tree
2412 push_overloaded_decl_1 (tree decl, int flags, bool is_friend)
2414 tree name = DECL_NAME (decl);
2415 tree old;
2416 tree new_binding;
2417 int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
2419 if (doing_global)
2420 old = namespace_binding (name, DECL_CONTEXT (decl));
2421 else
2422 old = lookup_name_innermost_nonclass_level (name);
2424 if (old)
2426 if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
2428 tree t = TREE_TYPE (old);
2429 if (MAYBE_CLASS_TYPE_P (t) && warn_shadow
2430 && (! DECL_IN_SYSTEM_HEADER (decl)
2431 || ! DECL_IN_SYSTEM_HEADER (old)))
2432 warning (OPT_Wshadow, "%q#D hides constructor for %q#T", decl, t);
2433 old = NULL_TREE;
2435 else if (is_overloaded_fn (old))
2437 tree tmp;
2439 for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
2441 tree fn = OVL_CURRENT (tmp);
2442 tree dup;
2444 if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp)
2445 && !(flags & PUSH_USING)
2446 && compparms_for_decl_and_using_decl (fn, decl)
2447 && ! decls_match (fn, decl))
2448 diagnose_name_conflict (decl, fn);
2450 dup = duplicate_decls (decl, fn, is_friend);
2451 /* If DECL was a redeclaration of FN -- even an invalid
2452 one -- pass that information along to our caller. */
2453 if (dup == fn || dup == error_mark_node)
2454 return dup;
2457 /* We don't overload implicit built-ins. duplicate_decls()
2458 may fail to merge the decls if the new decl is e.g. a
2459 template function. */
2460 if (TREE_CODE (old) == FUNCTION_DECL
2461 && DECL_ANTICIPATED (old)
2462 && !DECL_HIDDEN_FRIEND_P (old))
2463 old = NULL;
2465 else if (old == error_mark_node)
2466 /* Ignore the undefined symbol marker. */
2467 old = NULL_TREE;
2468 else
2470 error ("previous non-function declaration %q+#D", old);
2471 error ("conflicts with function declaration %q#D", decl);
2472 return decl;
2476 if (old || TREE_CODE (decl) == TEMPLATE_DECL
2477 /* If it's a using declaration, we always need to build an OVERLOAD,
2478 because it's the only way to remember that the declaration comes
2479 from 'using', and have the lookup behave correctly. */
2480 || (flags & PUSH_USING))
2482 if (old && TREE_CODE (old) != OVERLOAD)
2483 /* Wrap the existing single decl in an overload. */
2484 new_binding = ovl_cons (old, NULL_TREE);
2485 else
2486 new_binding = old;
2487 new_binding = ovl_cons (decl, new_binding);
2488 if (flags & PUSH_USING)
2489 OVL_USED (new_binding) = 1;
2491 else
2492 /* NAME is not ambiguous. */
2493 new_binding = decl;
2495 if (doing_global)
2496 set_namespace_binding (name, current_namespace, new_binding);
2497 else
2499 /* We only create an OVERLOAD if there was a previous binding at
2500 this level, or if decl is a template. In the former case, we
2501 need to remove the old binding and replace it with the new
2502 binding. We must also run through the NAMES on the binding
2503 level where the name was bound to update the chain. */
2505 if (TREE_CODE (new_binding) == OVERLOAD && old)
2507 tree *d;
2509 for (d = &IDENTIFIER_BINDING (name)->scope->names;
2511 d = &TREE_CHAIN (*d))
2512 if (*d == old
2513 || (TREE_CODE (*d) == TREE_LIST
2514 && TREE_VALUE (*d) == old))
2516 if (TREE_CODE (*d) == TREE_LIST)
2517 /* Just replace the old binding with the new. */
2518 TREE_VALUE (*d) = new_binding;
2519 else
2520 /* Build a TREE_LIST to wrap the OVERLOAD. */
2521 *d = tree_cons (NULL_TREE, new_binding,
2522 TREE_CHAIN (*d));
2524 /* And update the cxx_binding node. */
2525 IDENTIFIER_BINDING (name)->value = new_binding;
2526 return decl;
2529 /* We should always find a previous binding in this case. */
2530 gcc_unreachable ();
2533 /* Install the new binding. */
2534 push_local_binding (name, new_binding, flags);
2537 return decl;
2540 /* Wrapper for push_overloaded_decl_1. */
2542 static tree
2543 push_overloaded_decl (tree decl, int flags, bool is_friend)
2545 tree ret;
2546 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2547 ret = push_overloaded_decl_1 (decl, flags, is_friend);
2548 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2549 return ret;
2552 /* Check a non-member using-declaration. Return the name and scope
2553 being used, and the USING_DECL, or NULL_TREE on failure. */
2555 static tree
2556 validate_nonmember_using_decl (tree decl, tree scope, tree name)
2558 /* [namespace.udecl]
2559 A using-declaration for a class member shall be a
2560 member-declaration. */
2561 if (TYPE_P (scope))
2563 error ("%qT is not a namespace or unscoped enum", scope);
2564 return NULL_TREE;
2566 else if (scope == error_mark_node)
2567 return NULL_TREE;
2569 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR)
2571 /* 7.3.3/5
2572 A using-declaration shall not name a template-id. */
2573 error ("a using-declaration cannot specify a template-id. "
2574 "Try %<using %D%>", name);
2575 return NULL_TREE;
2578 if (TREE_CODE (decl) == NAMESPACE_DECL)
2580 error ("namespace %qD not allowed in using-declaration", decl);
2581 return NULL_TREE;
2584 if (TREE_CODE (decl) == SCOPE_REF)
2586 /* It's a nested name with template parameter dependent scope.
2587 This can only be using-declaration for class member. */
2588 error ("%qT is not a namespace", TREE_OPERAND (decl, 0));
2589 return NULL_TREE;
2592 if (is_overloaded_fn (decl))
2593 decl = get_first_fn (decl);
2595 gcc_assert (DECL_P (decl));
2597 /* Make a USING_DECL. */
2598 tree using_decl = push_using_decl (scope, name);
2600 if (using_decl == NULL_TREE
2601 && at_function_scope_p ()
2602 && VAR_P (decl))
2603 /* C++11 7.3.3/10. */
2604 error ("%qD is already declared in this scope", name);
2606 return using_decl;
2609 /* Process local and global using-declarations. */
2611 static void
2612 do_nonmember_using_decl (tree scope, tree name, tree oldval, tree oldtype,
2613 tree *newval, tree *newtype)
2615 struct scope_binding decls = EMPTY_SCOPE_BINDING;
2617 *newval = *newtype = NULL_TREE;
2618 if (!qualified_lookup_using_namespace (name, scope, &decls, 0))
2619 /* Lookup error */
2620 return;
2622 if (!decls.value && !decls.type)
2624 error ("%qD not declared", name);
2625 return;
2628 /* Shift the old and new bindings around so we're comparing class and
2629 enumeration names to each other. */
2630 if (oldval && DECL_IMPLICIT_TYPEDEF_P (oldval))
2632 oldtype = oldval;
2633 oldval = NULL_TREE;
2636 if (decls.value && DECL_IMPLICIT_TYPEDEF_P (decls.value))
2638 decls.type = decls.value;
2639 decls.value = NULL_TREE;
2642 if (decls.value)
2644 /* Check for using functions. */
2645 if (is_overloaded_fn (decls.value))
2647 tree tmp, tmp1;
2649 if (oldval && !is_overloaded_fn (oldval))
2651 error ("%qD is already declared in this scope", name);
2652 oldval = NULL_TREE;
2655 *newval = oldval;
2656 for (tmp = decls.value; tmp; tmp = OVL_NEXT (tmp))
2658 tree new_fn = OVL_CURRENT (tmp);
2660 /* Don't import functions that haven't been declared. */
2661 if (DECL_ANTICIPATED (new_fn))
2662 continue;
2664 /* [namespace.udecl]
2666 If a function declaration in namespace scope or block
2667 scope has the same name and the same parameter types as a
2668 function introduced by a using declaration the program is
2669 ill-formed. */
2670 for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1))
2672 tree old_fn = OVL_CURRENT (tmp1);
2674 if (new_fn == old_fn)
2675 /* The function already exists in the current namespace. */
2676 break;
2677 else if (TREE_CODE (tmp1) == OVERLOAD && OVL_USED (tmp1))
2678 continue; /* this is a using decl */
2679 else if (compparms_for_decl_and_using_decl (new_fn, old_fn))
2681 /* There was already a non-using declaration in
2682 this scope with the same parameter types. If both
2683 are the same extern "C" functions, that's ok. */
2684 if (DECL_ANTICIPATED (old_fn)
2685 && !DECL_HIDDEN_FRIEND_P (old_fn))
2686 /* Ignore anticipated built-ins. */;
2687 else if (decls_match (new_fn, old_fn))
2688 break;
2689 else
2691 diagnose_name_conflict (new_fn, old_fn);
2692 break;
2697 /* If we broke out of the loop, there's no reason to add
2698 this function to the using declarations for this
2699 scope. */
2700 if (tmp1)
2701 continue;
2703 /* If we are adding to an existing OVERLOAD, then we no
2704 longer know the type of the set of functions. */
2705 if (*newval && TREE_CODE (*newval) == OVERLOAD)
2706 TREE_TYPE (*newval) = unknown_type_node;
2707 /* Add this new function to the set. */
2708 *newval = build_overload (OVL_CURRENT (tmp), *newval);
2709 /* If there is only one function, then we use its type. (A
2710 using-declaration naming a single function can be used in
2711 contexts where overload resolution cannot be
2712 performed.) */
2713 if (TREE_CODE (*newval) != OVERLOAD)
2715 *newval = ovl_cons (*newval, NULL_TREE);
2716 TREE_TYPE (*newval) = TREE_TYPE (OVL_CURRENT (tmp));
2718 OVL_USED (*newval) = 1;
2721 else
2723 /* If we're declaring a non-function and OLDVAL is an anticipated
2724 built-in, just pretend it isn't there. */
2725 if (oldval
2726 && TREE_CODE (oldval) == FUNCTION_DECL
2727 && DECL_ANTICIPATED (oldval)
2728 && !DECL_HIDDEN_FRIEND_P (oldval))
2729 oldval = NULL_TREE;
2731 *newval = decls.value;
2732 if (oldval && !decls_match (*newval, oldval))
2733 error ("%qD is already declared in this scope", name);
2736 else
2737 *newval = oldval;
2739 if (decls.type && TREE_CODE (decls.type) == TREE_LIST)
2741 error ("reference to %qD is ambiguous", name);
2742 print_candidates (decls.type);
2744 else
2746 *newtype = decls.type;
2747 if (oldtype && *newtype && !decls_match (oldtype, *newtype))
2748 error ("%qD is already declared in this scope", name);
2751 /* If *newval is empty, shift any class or enumeration name down. */
2752 if (!*newval)
2754 *newval = *newtype;
2755 *newtype = NULL_TREE;
2759 /* Process a using-declaration at function scope. */
2761 void
2762 do_local_using_decl (tree decl, tree scope, tree name)
2764 tree oldval, oldtype, newval, newtype;
2765 tree orig_decl = decl;
2767 decl = validate_nonmember_using_decl (decl, scope, name);
2768 if (decl == NULL_TREE)
2769 return;
2771 if (building_stmt_list_p ()
2772 && at_function_scope_p ())
2773 add_decl_expr (decl);
2775 oldval = lookup_name_innermost_nonclass_level (name);
2776 oldtype = lookup_type_current_level (name);
2778 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
2780 if (newval)
2782 if (is_overloaded_fn (newval))
2784 tree fn, term;
2786 /* We only need to push declarations for those functions
2787 that were not already bound in the current level.
2788 The old value might be NULL_TREE, it might be a single
2789 function, or an OVERLOAD. */
2790 if (oldval && TREE_CODE (oldval) == OVERLOAD)
2791 term = OVL_FUNCTION (oldval);
2792 else
2793 term = oldval;
2794 for (fn = newval; fn && OVL_CURRENT (fn) != term;
2795 fn = OVL_NEXT (fn))
2796 push_overloaded_decl (OVL_CURRENT (fn),
2797 PUSH_LOCAL | PUSH_USING,
2798 false);
2800 else
2801 push_local_binding (name, newval, PUSH_USING);
2803 if (newtype)
2805 push_local_binding (name, newtype, PUSH_USING);
2806 set_identifier_type_value (name, newtype);
2809 /* Emit debug info. */
2810 if (!processing_template_decl)
2811 cp_emit_debug_info_for_using (orig_decl, current_scope());
2814 /* Returns true if ROOT (a namespace, class, or function) encloses
2815 CHILD. CHILD may be either a class type or a namespace. */
2817 bool
2818 is_ancestor (tree root, tree child)
2820 gcc_assert ((TREE_CODE (root) == NAMESPACE_DECL
2821 || TREE_CODE (root) == FUNCTION_DECL
2822 || CLASS_TYPE_P (root)));
2823 gcc_assert ((TREE_CODE (child) == NAMESPACE_DECL
2824 || CLASS_TYPE_P (child)));
2826 /* The global namespace encloses everything. */
2827 if (root == global_namespace)
2828 return true;
2830 while (true)
2832 /* If we've run out of scopes, stop. */
2833 if (!child)
2834 return false;
2835 /* If we've reached the ROOT, it encloses CHILD. */
2836 if (root == child)
2837 return true;
2838 /* Go out one level. */
2839 if (TYPE_P (child))
2840 child = TYPE_NAME (child);
2841 child = DECL_CONTEXT (child);
2845 /* Enter the class or namespace scope indicated by T suitable for name
2846 lookup. T can be arbitrary scope, not necessary nested inside the
2847 current scope. Returns a non-null scope to pop iff pop_scope
2848 should be called later to exit this scope. */
2850 tree
2851 push_scope (tree t)
2853 if (TREE_CODE (t) == NAMESPACE_DECL)
2854 push_decl_namespace (t);
2855 else if (CLASS_TYPE_P (t))
2857 if (!at_class_scope_p ()
2858 || !same_type_p (current_class_type, t))
2859 push_nested_class (t);
2860 else
2861 /* T is the same as the current scope. There is therefore no
2862 need to re-enter the scope. Since we are not actually
2863 pushing a new scope, our caller should not call
2864 pop_scope. */
2865 t = NULL_TREE;
2868 return t;
2871 /* Leave scope pushed by push_scope. */
2873 void
2874 pop_scope (tree t)
2876 if (t == NULL_TREE)
2877 return;
2878 if (TREE_CODE (t) == NAMESPACE_DECL)
2879 pop_decl_namespace ();
2880 else if CLASS_TYPE_P (t)
2881 pop_nested_class ();
2884 /* Subroutine of push_inner_scope. */
2886 static void
2887 push_inner_scope_r (tree outer, tree inner)
2889 tree prev;
2891 if (outer == inner
2892 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
2893 return;
2895 prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
2896 if (outer != prev)
2897 push_inner_scope_r (outer, prev);
2898 if (TREE_CODE (inner) == NAMESPACE_DECL)
2900 cp_binding_level *save_template_parm = 0;
2901 /* Temporary take out template parameter scopes. They are saved
2902 in reversed order in save_template_parm. */
2903 while (current_binding_level->kind == sk_template_parms)
2905 cp_binding_level *b = current_binding_level;
2906 current_binding_level = b->level_chain;
2907 b->level_chain = save_template_parm;
2908 save_template_parm = b;
2911 resume_scope (NAMESPACE_LEVEL (inner));
2912 current_namespace = inner;
2914 /* Restore template parameter scopes. */
2915 while (save_template_parm)
2917 cp_binding_level *b = save_template_parm;
2918 save_template_parm = b->level_chain;
2919 b->level_chain = current_binding_level;
2920 current_binding_level = b;
2923 else
2924 pushclass (inner);
2927 /* Enter the scope INNER from current scope. INNER must be a scope
2928 nested inside current scope. This works with both name lookup and
2929 pushing name into scope. In case a template parameter scope is present,
2930 namespace is pushed under the template parameter scope according to
2931 name lookup rule in 14.6.1/6.
2933 Return the former current scope suitable for pop_inner_scope. */
2935 tree
2936 push_inner_scope (tree inner)
2938 tree outer = current_scope ();
2939 if (!outer)
2940 outer = current_namespace;
2942 push_inner_scope_r (outer, inner);
2943 return outer;
2946 /* Exit the current scope INNER back to scope OUTER. */
2948 void
2949 pop_inner_scope (tree outer, tree inner)
2951 if (outer == inner
2952 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
2953 return;
2955 while (outer != inner)
2957 if (TREE_CODE (inner) == NAMESPACE_DECL)
2959 cp_binding_level *save_template_parm = 0;
2960 /* Temporary take out template parameter scopes. They are saved
2961 in reversed order in save_template_parm. */
2962 while (current_binding_level->kind == sk_template_parms)
2964 cp_binding_level *b = current_binding_level;
2965 current_binding_level = b->level_chain;
2966 b->level_chain = save_template_parm;
2967 save_template_parm = b;
2970 pop_namespace ();
2972 /* Restore template parameter scopes. */
2973 while (save_template_parm)
2975 cp_binding_level *b = save_template_parm;
2976 save_template_parm = b->level_chain;
2977 b->level_chain = current_binding_level;
2978 current_binding_level = b;
2981 else
2982 popclass ();
2984 inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
2988 /* Do a pushlevel for class declarations. */
2990 void
2991 pushlevel_class (void)
2993 class_binding_level = begin_scope (sk_class, current_class_type);
2996 /* ...and a poplevel for class declarations. */
2998 void
2999 poplevel_class (void)
3001 cp_binding_level *level = class_binding_level;
3002 cp_class_binding *cb;
3003 size_t i;
3004 tree shadowed;
3006 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3007 gcc_assert (level != 0);
3009 /* If we're leaving a toplevel class, cache its binding level. */
3010 if (current_class_depth == 1)
3011 previous_class_level = level;
3012 for (shadowed = level->type_shadowed;
3013 shadowed;
3014 shadowed = TREE_CHAIN (shadowed))
3015 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
3017 /* Remove the bindings for all of the class-level declarations. */
3018 if (level->class_shadowed)
3020 FOR_EACH_VEC_ELT (*level->class_shadowed, i, cb)
3022 IDENTIFIER_BINDING (cb->identifier) = cb->base->previous;
3023 cxx_binding_free (cb->base);
3025 ggc_free (level->class_shadowed);
3026 level->class_shadowed = NULL;
3029 /* Now, pop out of the binding level which we created up in the
3030 `pushlevel_class' routine. */
3031 gcc_assert (current_binding_level == level);
3032 leave_scope ();
3033 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3036 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
3037 appropriate. DECL is the value to which a name has just been
3038 bound. CLASS_TYPE is the class in which the lookup occurred. */
3040 static void
3041 set_inherited_value_binding_p (cxx_binding *binding, tree decl,
3042 tree class_type)
3044 if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
3046 tree context;
3048 if (TREE_CODE (decl) == OVERLOAD)
3049 context = ovl_scope (decl);
3050 else
3052 gcc_assert (DECL_P (decl));
3053 context = context_for_name_lookup (decl);
3056 if (is_properly_derived_from (class_type, context))
3057 INHERITED_VALUE_BINDING_P (binding) = 1;
3058 else
3059 INHERITED_VALUE_BINDING_P (binding) = 0;
3061 else if (binding->value == decl)
3062 /* We only encounter a TREE_LIST when there is an ambiguity in the
3063 base classes. Such an ambiguity can be overridden by a
3064 definition in this class. */
3065 INHERITED_VALUE_BINDING_P (binding) = 1;
3066 else
3067 INHERITED_VALUE_BINDING_P (binding) = 0;
3070 /* Make the declaration of X appear in CLASS scope. */
3072 bool
3073 pushdecl_class_level (tree x)
3075 tree name;
3076 bool is_valid = true;
3077 bool subtime;
3079 /* Do nothing if we're adding to an outer lambda closure type,
3080 outer_binding will add it later if it's needed. */
3081 if (current_class_type != class_binding_level->this_entity)
3082 return true;
3084 subtime = timevar_cond_start (TV_NAME_LOOKUP);
3085 /* Get the name of X. */
3086 if (TREE_CODE (x) == OVERLOAD)
3087 name = DECL_NAME (get_first_fn (x));
3088 else
3089 name = DECL_NAME (x);
3091 if (name)
3093 is_valid = push_class_level_binding (name, x);
3094 if (TREE_CODE (x) == TYPE_DECL)
3095 set_identifier_type_value (name, x);
3097 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3099 /* If X is an anonymous aggregate, all of its members are
3100 treated as if they were members of the class containing the
3101 aggregate, for naming purposes. */
3102 tree f;
3104 for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = DECL_CHAIN (f))
3106 location_t save_location = input_location;
3107 input_location = DECL_SOURCE_LOCATION (f);
3108 if (!pushdecl_class_level (f))
3109 is_valid = false;
3110 input_location = save_location;
3113 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3114 return is_valid;
3117 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
3118 scope. If the value returned is non-NULL, and the PREVIOUS field
3119 is not set, callers must set the PREVIOUS field explicitly. */
3121 static cxx_binding *
3122 get_class_binding (tree name, cp_binding_level *scope)
3124 tree class_type;
3125 tree type_binding;
3126 tree value_binding;
3127 cxx_binding *binding;
3129 class_type = scope->this_entity;
3131 /* Get the type binding. */
3132 type_binding = lookup_member (class_type, name,
3133 /*protect=*/2, /*want_type=*/true,
3134 tf_warning_or_error);
3135 /* Get the value binding. */
3136 value_binding = lookup_member (class_type, name,
3137 /*protect=*/2, /*want_type=*/false,
3138 tf_warning_or_error);
3140 if (value_binding
3141 && (TREE_CODE (value_binding) == TYPE_DECL
3142 || DECL_CLASS_TEMPLATE_P (value_binding)
3143 || (TREE_CODE (value_binding) == TREE_LIST
3144 && TREE_TYPE (value_binding) == error_mark_node
3145 && (TREE_CODE (TREE_VALUE (value_binding))
3146 == TYPE_DECL))))
3147 /* We found a type binding, even when looking for a non-type
3148 binding. This means that we already processed this binding
3149 above. */
3151 else if (value_binding)
3153 if (TREE_CODE (value_binding) == TREE_LIST
3154 && TREE_TYPE (value_binding) == error_mark_node)
3155 /* NAME is ambiguous. */
3157 else if (BASELINK_P (value_binding))
3158 /* NAME is some overloaded functions. */
3159 value_binding = BASELINK_FUNCTIONS (value_binding);
3162 /* If we found either a type binding or a value binding, create a
3163 new binding object. */
3164 if (type_binding || value_binding)
3166 binding = new_class_binding (name,
3167 value_binding,
3168 type_binding,
3169 scope);
3170 /* This is a class-scope binding, not a block-scope binding. */
3171 LOCAL_BINDING_P (binding) = 0;
3172 set_inherited_value_binding_p (binding, value_binding, class_type);
3174 else
3175 binding = NULL;
3177 return binding;
3180 /* Make the declaration(s) of X appear in CLASS scope under the name
3181 NAME. Returns true if the binding is valid. */
3183 static bool
3184 push_class_level_binding_1 (tree name, tree x)
3186 cxx_binding *binding;
3187 tree decl = x;
3188 bool ok;
3190 /* The class_binding_level will be NULL if x is a template
3191 parameter name in a member template. */
3192 if (!class_binding_level)
3193 return true;
3195 if (name == error_mark_node)
3196 return false;
3198 /* Can happen for an erroneous declaration (c++/60384). */
3199 if (!identifier_p (name))
3201 gcc_assert (errorcount || sorrycount);
3202 return false;
3205 /* Check for invalid member names. But don't worry about a default
3206 argument-scope lambda being pushed after the class is complete. */
3207 gcc_assert (TYPE_BEING_DEFINED (current_class_type)
3208 || LAMBDA_TYPE_P (TREE_TYPE (decl)));
3209 /* Check that we're pushing into the right binding level. */
3210 gcc_assert (current_class_type == class_binding_level->this_entity);
3212 /* We could have been passed a tree list if this is an ambiguous
3213 declaration. If so, pull the declaration out because
3214 check_template_shadow will not handle a TREE_LIST. */
3215 if (TREE_CODE (decl) == TREE_LIST
3216 && TREE_TYPE (decl) == error_mark_node)
3217 decl = TREE_VALUE (decl);
3219 if (!check_template_shadow (decl))
3220 return false;
3222 /* [class.mem]
3224 If T is the name of a class, then each of the following shall
3225 have a name different from T:
3227 -- every static data member of class T;
3229 -- every member of class T that is itself a type;
3231 -- every enumerator of every member of class T that is an
3232 enumerated type;
3234 -- every member of every anonymous union that is a member of
3235 class T.
3237 (Non-static data members were also forbidden to have the same
3238 name as T until TC1.) */
3239 if ((VAR_P (x)
3240 || TREE_CODE (x) == CONST_DECL
3241 || (TREE_CODE (x) == TYPE_DECL
3242 && !DECL_SELF_REFERENCE_P (x))
3243 /* A data member of an anonymous union. */
3244 || (TREE_CODE (x) == FIELD_DECL
3245 && DECL_CONTEXT (x) != current_class_type))
3246 && DECL_NAME (x) == constructor_name (current_class_type))
3248 tree scope = context_for_name_lookup (x);
3249 if (TYPE_P (scope) && same_type_p (scope, current_class_type))
3251 error ("%qD has the same name as the class in which it is "
3252 "declared",
3254 return false;
3258 /* Get the current binding for NAME in this class, if any. */
3259 binding = IDENTIFIER_BINDING (name);
3260 if (!binding || binding->scope != class_binding_level)
3262 binding = get_class_binding (name, class_binding_level);
3263 /* If a new binding was created, put it at the front of the
3264 IDENTIFIER_BINDING list. */
3265 if (binding)
3267 binding->previous = IDENTIFIER_BINDING (name);
3268 IDENTIFIER_BINDING (name) = binding;
3272 /* If there is already a binding, then we may need to update the
3273 current value. */
3274 if (binding && binding->value)
3276 tree bval = binding->value;
3277 tree old_decl = NULL_TREE;
3278 tree target_decl = strip_using_decl (decl);
3279 tree target_bval = strip_using_decl (bval);
3281 if (INHERITED_VALUE_BINDING_P (binding))
3283 /* If the old binding was from a base class, and was for a
3284 tag name, slide it over to make room for the new binding.
3285 The old binding is still visible if explicitly qualified
3286 with a class-key. */
3287 if (TREE_CODE (target_bval) == TYPE_DECL
3288 && DECL_ARTIFICIAL (target_bval)
3289 && !(TREE_CODE (target_decl) == TYPE_DECL
3290 && DECL_ARTIFICIAL (target_decl)))
3292 old_decl = binding->type;
3293 binding->type = bval;
3294 binding->value = NULL_TREE;
3295 INHERITED_VALUE_BINDING_P (binding) = 0;
3297 else
3299 old_decl = bval;
3300 /* Any inherited type declaration is hidden by the type
3301 declaration in the derived class. */
3302 if (TREE_CODE (target_decl) == TYPE_DECL
3303 && DECL_ARTIFICIAL (target_decl))
3304 binding->type = NULL_TREE;
3307 else if (TREE_CODE (target_decl) == OVERLOAD
3308 && is_overloaded_fn (target_bval))
3309 old_decl = bval;
3310 else if (TREE_CODE (decl) == USING_DECL
3311 && TREE_CODE (bval) == USING_DECL
3312 && same_type_p (USING_DECL_SCOPE (decl),
3313 USING_DECL_SCOPE (bval)))
3314 /* This is a using redeclaration that will be diagnosed later
3315 in supplement_binding */
3317 else if (TREE_CODE (decl) == USING_DECL
3318 && TREE_CODE (bval) == USING_DECL
3319 && DECL_DEPENDENT_P (decl)
3320 && DECL_DEPENDENT_P (bval))
3321 return true;
3322 else if (TREE_CODE (decl) == USING_DECL
3323 && is_overloaded_fn (target_bval))
3324 old_decl = bval;
3325 else if (TREE_CODE (bval) == USING_DECL
3326 && is_overloaded_fn (target_decl))
3327 return true;
3329 if (old_decl && binding->scope == class_binding_level)
3331 binding->value = x;
3332 /* It is always safe to clear INHERITED_VALUE_BINDING_P
3333 here. This function is only used to register bindings
3334 from with the class definition itself. */
3335 INHERITED_VALUE_BINDING_P (binding) = 0;
3336 return true;
3340 /* Note that we declared this value so that we can issue an error if
3341 this is an invalid redeclaration of a name already used for some
3342 other purpose. */
3343 note_name_declared_in_class (name, decl);
3345 /* If we didn't replace an existing binding, put the binding on the
3346 stack of bindings for the identifier, and update the shadowed
3347 list. */
3348 if (binding && binding->scope == class_binding_level)
3349 /* Supplement the existing binding. */
3350 ok = supplement_binding (binding, decl);
3351 else
3353 /* Create a new binding. */
3354 push_binding (name, decl, class_binding_level);
3355 ok = true;
3358 return ok;
3361 /* Wrapper for push_class_level_binding_1. */
3363 bool
3364 push_class_level_binding (tree name, tree x)
3366 bool ret;
3367 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3368 ret = push_class_level_binding_1 (name, x);
3369 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3370 return ret;
3373 /* Process "using SCOPE::NAME" in a class scope. Return the
3374 USING_DECL created. */
3376 tree
3377 do_class_using_decl (tree scope, tree name)
3379 /* The USING_DECL returned by this function. */
3380 tree value;
3381 /* The declaration (or declarations) name by this using
3382 declaration. NULL if we are in a template and cannot figure out
3383 what has been named. */
3384 tree decl;
3385 /* True if SCOPE is a dependent type. */
3386 bool scope_dependent_p;
3387 /* True if SCOPE::NAME is dependent. */
3388 bool name_dependent_p;
3389 /* True if any of the bases of CURRENT_CLASS_TYPE are dependent. */
3390 bool bases_dependent_p;
3391 tree binfo;
3393 if (name == error_mark_node)
3394 return NULL_TREE;
3396 if (!scope || !TYPE_P (scope))
3398 error ("using-declaration for non-member at class scope");
3399 return NULL_TREE;
3402 /* Make sure the name is not invalid */
3403 if (TREE_CODE (name) == BIT_NOT_EXPR)
3405 error ("%<%T::%D%> names destructor", scope, name);
3406 return NULL_TREE;
3408 /* Using T::T declares inheriting ctors, even if T is a typedef. */
3409 if (MAYBE_CLASS_TYPE_P (scope)
3410 && (name == TYPE_IDENTIFIER (scope)
3411 || constructor_name_p (name, scope)))
3413 maybe_warn_cpp0x (CPP0X_INHERITING_CTORS);
3414 name = ctor_identifier;
3415 CLASSTYPE_NON_AGGREGATE (current_class_type) = true;
3417 if (constructor_name_p (name, current_class_type))
3419 error ("%<%T::%D%> names constructor in %qT",
3420 scope, name, current_class_type);
3421 return NULL_TREE;
3424 scope_dependent_p = dependent_scope_p (scope);
3425 name_dependent_p = (scope_dependent_p
3426 || (IDENTIFIER_TYPENAME_P (name)
3427 && dependent_type_p (TREE_TYPE (name))));
3429 bases_dependent_p = any_dependent_bases_p ();
3431 decl = NULL_TREE;
3433 /* From [namespace.udecl]:
3435 A using-declaration used as a member-declaration shall refer to a
3436 member of a base class of the class being defined.
3438 In general, we cannot check this constraint in a template because
3439 we do not know the entire set of base classes of the current
3440 class type. Morover, if SCOPE is dependent, it might match a
3441 non-dependent base. */
3443 if (!scope_dependent_p)
3445 base_kind b_kind;
3446 binfo = lookup_base (current_class_type, scope, ba_any, &b_kind,
3447 tf_warning_or_error);
3448 if (b_kind < bk_proper_base)
3450 if (!bases_dependent_p || b_kind == bk_same_type)
3452 error_not_base_type (scope, current_class_type);
3453 return NULL_TREE;
3456 else if (name == ctor_identifier
3457 && BINFO_INHERITANCE_CHAIN (BINFO_INHERITANCE_CHAIN (binfo)))
3459 error ("cannot inherit constructors from indirect base %qT", scope);
3460 return NULL_TREE;
3462 else if (!name_dependent_p)
3464 decl = lookup_member (binfo, name, 0, false, tf_warning_or_error);
3465 if (!decl)
3467 error ("no members matching %<%T::%D%> in %q#T", scope, name,
3468 scope);
3469 return NULL_TREE;
3471 /* The binfo from which the functions came does not matter. */
3472 if (BASELINK_P (decl))
3473 decl = BASELINK_FUNCTIONS (decl);
3477 value = build_lang_decl (USING_DECL, name, NULL_TREE);
3478 USING_DECL_DECLS (value) = decl;
3479 USING_DECL_SCOPE (value) = scope;
3480 DECL_DEPENDENT_P (value) = !decl;
3482 return value;
3486 /* Return the binding value for name in scope. */
3489 static tree
3490 namespace_binding_1 (tree name, tree scope)
3492 cxx_binding *binding;
3494 if (SCOPE_FILE_SCOPE_P (scope))
3495 scope = global_namespace;
3496 else
3497 /* Unnecessary for the global namespace because it can't be an alias. */
3498 scope = ORIGINAL_NAMESPACE (scope);
3500 binding = cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
3502 return binding ? binding->value : NULL_TREE;
3505 tree
3506 namespace_binding (tree name, tree scope)
3508 tree ret;
3509 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3510 ret = namespace_binding_1 (name, scope);
3511 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3512 return ret;
3515 /* Set the binding value for name in scope. */
3517 static void
3518 set_namespace_binding_1 (tree name, tree scope, tree val)
3520 cxx_binding *b;
3522 if (scope == NULL_TREE)
3523 scope = global_namespace;
3524 b = binding_for_name (NAMESPACE_LEVEL (scope), name);
3525 if (!b->value
3526 /* For templates and using we create a single element OVERLOAD.
3527 Look for the chain to know whether this is really augmenting
3528 an existing overload. */
3529 || (TREE_CODE (val) == OVERLOAD && OVL_CHAIN (val))
3530 || val == error_mark_node)
3531 b->value = val;
3532 else
3533 supplement_binding (b, val);
3536 /* Wrapper for set_namespace_binding_1. */
3538 void
3539 set_namespace_binding (tree name, tree scope, tree val)
3541 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3542 set_namespace_binding_1 (name, scope, val);
3543 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3546 /* Set the context of a declaration to scope. Complain if we are not
3547 outside scope. */
3549 void
3550 set_decl_namespace (tree decl, tree scope, bool friendp)
3552 tree old;
3554 /* Get rid of namespace aliases. */
3555 scope = ORIGINAL_NAMESPACE (scope);
3557 /* It is ok for friends to be qualified in parallel space. */
3558 if (!friendp && !is_ancestor (current_namespace, scope))
3559 error ("declaration of %qD not in a namespace surrounding %qD",
3560 decl, scope);
3561 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3563 /* Writing "int N::i" to declare a variable within "N" is invalid. */
3564 if (scope == current_namespace)
3566 if (at_namespace_scope_p ())
3567 error ("explicit qualification in declaration of %qD",
3568 decl);
3569 return;
3572 /* See whether this has been declared in the namespace. */
3573 old = lookup_qualified_name (scope, DECL_NAME (decl), /*type*/false,
3574 /*complain*/true, /*hidden*/true);
3575 if (old == error_mark_node)
3576 /* No old declaration at all. */
3577 goto complain;
3578 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
3579 if (TREE_CODE (old) == TREE_LIST)
3581 error ("reference to %qD is ambiguous", decl);
3582 print_candidates (old);
3583 return;
3585 if (!is_overloaded_fn (decl))
3587 /* We might have found OLD in an inline namespace inside SCOPE. */
3588 if (TREE_CODE (decl) == TREE_CODE (old))
3589 DECL_CONTEXT (decl) = DECL_CONTEXT (old);
3590 /* Don't compare non-function decls with decls_match here, since
3591 it can't check for the correct constness at this
3592 point. pushdecl will find those errors later. */
3593 return;
3595 /* Since decl is a function, old should contain a function decl. */
3596 if (!is_overloaded_fn (old))
3597 goto complain;
3598 /* We handle these in check_explicit_instantiation_namespace. */
3599 if (processing_explicit_instantiation)
3600 return;
3601 if (processing_template_decl || processing_specialization)
3602 /* We have not yet called push_template_decl to turn a
3603 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
3604 match. But, we'll check later, when we construct the
3605 template. */
3606 return;
3607 /* Instantiations or specializations of templates may be declared as
3608 friends in any namespace. */
3609 if (friendp && DECL_USE_TEMPLATE (decl))
3610 return;
3611 if (is_overloaded_fn (old))
3613 tree found = NULL_TREE;
3614 tree elt = old;
3615 for (; elt; elt = OVL_NEXT (elt))
3617 tree ofn = OVL_CURRENT (elt);
3618 /* Adjust DECL_CONTEXT first so decls_match will return true
3619 if DECL will match a declaration in an inline namespace. */
3620 DECL_CONTEXT (decl) = DECL_CONTEXT (ofn);
3621 if (decls_match (decl, ofn))
3623 if (found && !decls_match (found, ofn))
3625 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3626 error ("reference to %qD is ambiguous", decl);
3627 print_candidates (old);
3628 return;
3630 found = ofn;
3633 if (found)
3635 if (!is_associated_namespace (scope, CP_DECL_CONTEXT (found)))
3636 goto complain;
3637 if (DECL_HIDDEN_FRIEND_P (found))
3639 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
3640 "%qD has not been declared within %D", decl, scope);
3641 inform (DECL_SOURCE_LOCATION (found), "only here as a friend");
3643 DECL_CONTEXT (decl) = DECL_CONTEXT (found);
3644 return;
3647 else
3649 DECL_CONTEXT (decl) = DECL_CONTEXT (old);
3650 if (decls_match (decl, old))
3651 return;
3654 /* It didn't work, go back to the explicit scope. */
3655 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3656 complain:
3657 error ("%qD should have been declared inside %qD", decl, scope);
3660 /* Return the namespace where the current declaration is declared. */
3662 tree
3663 current_decl_namespace (void)
3665 tree result;
3666 /* If we have been pushed into a different namespace, use it. */
3667 if (!vec_safe_is_empty (decl_namespace_list))
3668 return decl_namespace_list->last ();
3670 if (current_class_type)
3671 result = decl_namespace_context (current_class_type);
3672 else if (current_function_decl)
3673 result = decl_namespace_context (current_function_decl);
3674 else
3675 result = current_namespace;
3676 return result;
3679 /* Process any ATTRIBUTES on a namespace definition. Returns true if
3680 attribute visibility is seen. */
3682 bool
3683 handle_namespace_attrs (tree ns, tree attributes)
3685 tree d;
3686 bool saw_vis = false;
3688 for (d = attributes; d; d = TREE_CHAIN (d))
3690 tree name = get_attribute_name (d);
3691 tree args = TREE_VALUE (d);
3693 if (is_attribute_p ("visibility", name))
3695 /* attribute visibility is a property of the syntactic block
3696 rather than the namespace as a whole, so we don't touch the
3697 NAMESPACE_DECL at all. */
3698 tree x = args ? TREE_VALUE (args) : NULL_TREE;
3699 if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args))
3701 warning (OPT_Wattributes,
3702 "%qD attribute requires a single NTBS argument",
3703 name);
3704 continue;
3707 if (!TREE_PUBLIC (ns))
3708 warning (OPT_Wattributes,
3709 "%qD attribute is meaningless since members of the "
3710 "anonymous namespace get local symbols", name);
3712 push_visibility (TREE_STRING_POINTER (x), 1);
3713 saw_vis = true;
3715 else if (is_attribute_p ("abi_tag", name))
3717 if (!DECL_NAMESPACE_ASSOCIATIONS (ns))
3719 warning (OPT_Wattributes, "ignoring %qD attribute on non-inline "
3720 "namespace", name);
3721 continue;
3723 if (!DECL_NAME (ns))
3725 warning (OPT_Wattributes, "ignoring %qD attribute on anonymous "
3726 "namespace", name);
3727 continue;
3729 if (!args)
3731 tree dn = DECL_NAME (ns);
3732 args = build_string (IDENTIFIER_LENGTH (dn) + 1,
3733 IDENTIFIER_POINTER (dn));
3734 TREE_TYPE (args) = char_array_type_node;
3735 args = fix_string_type (args);
3736 args = build_tree_list (NULL_TREE, args);
3738 if (check_abi_tag_args (args, name))
3739 DECL_ATTRIBUTES (ns) = tree_cons (name, args,
3740 DECL_ATTRIBUTES (ns));
3742 else
3744 warning (OPT_Wattributes, "%qD attribute directive ignored",
3745 name);
3746 continue;
3750 return saw_vis;
3753 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we
3754 select a name that is unique to this compilation unit. Returns FALSE if
3755 pushdecl fails, TRUE otherwise. */
3757 bool
3758 push_namespace (tree name)
3760 tree d = NULL_TREE;
3761 bool need_new = true;
3762 bool implicit_use = false;
3763 bool anon = !name;
3765 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3767 /* We should not get here if the global_namespace is not yet constructed
3768 nor if NAME designates the global namespace: The global scope is
3769 constructed elsewhere. */
3770 gcc_assert (global_namespace != NULL && name != global_scope_name);
3772 if (anon)
3774 name = get_anonymous_namespace_name();
3775 d = IDENTIFIER_NAMESPACE_VALUE (name);
3776 if (d)
3777 /* Reopening anonymous namespace. */
3778 need_new = false;
3779 implicit_use = true;
3781 else
3783 /* Check whether this is an extended namespace definition. */
3784 d = IDENTIFIER_NAMESPACE_VALUE (name);
3785 if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
3787 tree dna = DECL_NAMESPACE_ALIAS (d);
3788 if (dna)
3790 /* We do some error recovery for, eg, the redeclaration
3791 of M here:
3793 namespace N {}
3794 namespace M = N;
3795 namespace M {}
3797 However, in nasty cases like:
3799 namespace N
3801 namespace M = N;
3802 namespace M {}
3805 we just error out below, in duplicate_decls. */
3806 if (NAMESPACE_LEVEL (dna)->level_chain
3807 == current_binding_level)
3809 error ("namespace alias %qD not allowed here, "
3810 "assuming %qD", d, dna);
3811 d = dna;
3812 need_new = false;
3815 else
3816 need_new = false;
3820 if (need_new)
3822 /* Make a new namespace, binding the name to it. */
3823 d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
3824 DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace);
3825 /* The name of this namespace is not visible to other translation
3826 units if it is an anonymous namespace or member thereof. */
3827 if (anon || decl_anon_ns_mem_p (current_namespace))
3828 TREE_PUBLIC (d) = 0;
3829 else
3830 TREE_PUBLIC (d) = 1;
3831 if (pushdecl (d) == error_mark_node)
3833 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3834 return false;
3836 if (anon)
3838 /* Clear DECL_NAME for the benefit of debugging back ends. */
3839 SET_DECL_ASSEMBLER_NAME (d, name);
3840 DECL_NAME (d) = NULL_TREE;
3842 begin_scope (sk_namespace, d);
3844 else
3845 resume_scope (NAMESPACE_LEVEL (d));
3847 if (implicit_use)
3848 do_using_directive (d);
3849 /* Enter the name space. */
3850 current_namespace = d;
3852 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3853 return true;
3856 /* Pop from the scope of the current namespace. */
3858 void
3859 pop_namespace (void)
3861 gcc_assert (current_namespace != global_namespace);
3862 current_namespace = CP_DECL_CONTEXT (current_namespace);
3863 /* The binding level is not popped, as it might be re-opened later. */
3864 leave_scope ();
3867 /* Push into the scope of the namespace NS, even if it is deeply
3868 nested within another namespace. */
3870 void
3871 push_nested_namespace (tree ns)
3873 if (ns == global_namespace)
3874 push_to_top_level ();
3875 else
3877 push_nested_namespace (CP_DECL_CONTEXT (ns));
3878 push_namespace (DECL_NAME (ns));
3882 /* Pop back from the scope of the namespace NS, which was previously
3883 entered with push_nested_namespace. */
3885 void
3886 pop_nested_namespace (tree ns)
3888 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3889 gcc_assert (current_namespace == ns);
3890 while (ns != global_namespace)
3892 pop_namespace ();
3893 ns = CP_DECL_CONTEXT (ns);
3896 pop_from_top_level ();
3897 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3900 /* Temporarily set the namespace for the current declaration. */
3902 void
3903 push_decl_namespace (tree decl)
3905 if (TREE_CODE (decl) != NAMESPACE_DECL)
3906 decl = decl_namespace_context (decl);
3907 vec_safe_push (decl_namespace_list, ORIGINAL_NAMESPACE (decl));
3910 /* [namespace.memdef]/2 */
3912 void
3913 pop_decl_namespace (void)
3915 decl_namespace_list->pop ();
3918 /* Return the namespace that is the common ancestor
3919 of two given namespaces. */
3921 static tree
3922 namespace_ancestor_1 (tree ns1, tree ns2)
3924 tree nsr;
3925 if (is_ancestor (ns1, ns2))
3926 nsr = ns1;
3927 else
3928 nsr = namespace_ancestor_1 (CP_DECL_CONTEXT (ns1), ns2);
3929 return nsr;
3932 /* Wrapper for namespace_ancestor_1. */
3934 static tree
3935 namespace_ancestor (tree ns1, tree ns2)
3937 tree nsr;
3938 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3939 nsr = namespace_ancestor_1 (ns1, ns2);
3940 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3941 return nsr;
3944 /* Process a namespace-alias declaration. */
3946 void
3947 do_namespace_alias (tree alias, tree name_space)
3949 if (name_space == error_mark_node)
3950 return;
3952 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
3954 name_space = ORIGINAL_NAMESPACE (name_space);
3956 /* Build the alias. */
3957 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
3958 DECL_NAMESPACE_ALIAS (alias) = name_space;
3959 DECL_EXTERNAL (alias) = 1;
3960 DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ());
3961 pushdecl (alias);
3963 /* Emit debug info for namespace alias. */
3964 if (!building_stmt_list_p ())
3965 (*debug_hooks->early_global_decl) (alias);
3968 /* Like pushdecl, only it places X in the current namespace,
3969 if appropriate. */
3971 tree
3972 pushdecl_namespace_level (tree x, bool is_friend)
3974 cp_binding_level *b = current_binding_level;
3975 tree t;
3977 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3978 t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace), is_friend);
3980 /* Now, the type_shadowed stack may screw us. Munge it so it does
3981 what we want. */
3982 if (TREE_CODE (t) == TYPE_DECL)
3984 tree name = DECL_NAME (t);
3985 tree newval;
3986 tree *ptr = (tree *)0;
3987 for (; !global_scope_p (b); b = b->level_chain)
3989 tree shadowed = b->type_shadowed;
3990 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
3991 if (TREE_PURPOSE (shadowed) == name)
3993 ptr = &TREE_VALUE (shadowed);
3994 /* Can't break out of the loop here because sometimes
3995 a binding level will have duplicate bindings for
3996 PT names. It's gross, but I haven't time to fix it. */
3999 newval = TREE_TYPE (t);
4000 if (ptr == (tree *)0)
4002 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
4003 up here if this is changed to an assertion. --KR */
4004 SET_IDENTIFIER_TYPE_VALUE (name, t);
4006 else
4008 *ptr = newval;
4011 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4012 return t;
4015 /* Insert USED into the using list of USER. Set INDIRECT_flag if this
4016 directive is not directly from the source. Also find the common
4017 ancestor and let our users know about the new namespace */
4019 static void
4020 add_using_namespace_1 (tree user, tree used, bool indirect)
4022 tree t;
4023 /* Using oneself is a no-op. */
4024 if (user == used)
4025 return;
4026 gcc_assert (TREE_CODE (user) == NAMESPACE_DECL);
4027 gcc_assert (TREE_CODE (used) == NAMESPACE_DECL);
4028 /* Check if we already have this. */
4029 t = purpose_member (used, DECL_NAMESPACE_USING (user));
4030 if (t != NULL_TREE)
4032 if (!indirect)
4033 /* Promote to direct usage. */
4034 TREE_INDIRECT_USING (t) = 0;
4035 return;
4038 /* Add used to the user's using list. */
4039 DECL_NAMESPACE_USING (user)
4040 = tree_cons (used, namespace_ancestor (user, used),
4041 DECL_NAMESPACE_USING (user));
4043 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect;
4045 /* Add user to the used's users list. */
4046 DECL_NAMESPACE_USERS (used)
4047 = tree_cons (user, 0, DECL_NAMESPACE_USERS (used));
4049 /* Recursively add all namespaces used. */
4050 for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t))
4051 /* indirect usage */
4052 add_using_namespace_1 (user, TREE_PURPOSE (t), 1);
4054 /* Tell everyone using us about the new used namespaces. */
4055 for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t))
4056 add_using_namespace_1 (TREE_PURPOSE (t), used, 1);
4059 /* Wrapper for add_using_namespace_1. */
4061 static void
4062 add_using_namespace (tree user, tree used, bool indirect)
4064 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4065 add_using_namespace_1 (user, used, indirect);
4066 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4069 /* Process a using-declaration not appearing in class or local scope. */
4071 void
4072 do_toplevel_using_decl (tree decl, tree scope, tree name)
4074 tree oldval, oldtype, newval, newtype;
4075 tree orig_decl = decl;
4076 cxx_binding *binding;
4078 decl = validate_nonmember_using_decl (decl, scope, name);
4079 if (decl == NULL_TREE)
4080 return;
4082 binding = binding_for_name (NAMESPACE_LEVEL (current_namespace), name);
4084 oldval = binding->value;
4085 oldtype = binding->type;
4087 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
4089 /* Emit debug info. */
4090 if (!processing_template_decl)
4091 cp_emit_debug_info_for_using (orig_decl, current_namespace);
4093 /* Copy declarations found. */
4094 if (newval)
4095 binding->value = newval;
4096 if (newtype)
4097 binding->type = newtype;
4100 /* Process a using-directive. */
4102 void
4103 do_using_directive (tree name_space)
4105 tree context = NULL_TREE;
4107 if (name_space == error_mark_node)
4108 return;
4110 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
4112 if (building_stmt_list_p ())
4113 add_stmt (build_stmt (input_location, USING_STMT, name_space));
4114 name_space = ORIGINAL_NAMESPACE (name_space);
4116 if (!toplevel_bindings_p ())
4118 push_using_directive (name_space);
4120 else
4122 /* direct usage */
4123 add_using_namespace (current_namespace, name_space, 0);
4124 if (current_namespace != global_namespace)
4125 context = current_namespace;
4127 /* Emit debugging info. */
4128 if (!processing_template_decl)
4129 (*debug_hooks->imported_module_or_decl) (name_space, NULL_TREE,
4130 context, false);
4134 /* Deal with a using-directive seen by the parser. Currently we only
4135 handle attributes here, since they cannot appear inside a template. */
4137 void
4138 parse_using_directive (tree name_space, tree attribs)
4140 do_using_directive (name_space);
4142 if (attribs == error_mark_node)
4143 return;
4145 for (tree a = attribs; a; a = TREE_CHAIN (a))
4147 tree name = get_attribute_name (a);
4148 if (is_attribute_p ("strong", name))
4150 warning (OPT_Wdeprecated, "strong using is deprecated; use inline "
4151 "namespaces instead");
4152 if (!toplevel_bindings_p ())
4153 error ("strong using only meaningful at namespace scope");
4154 else if (name_space != error_mark_node)
4156 if (!is_ancestor (current_namespace, name_space))
4157 error ("current namespace %qD does not enclose strongly used namespace %qD",
4158 current_namespace, name_space);
4159 DECL_NAMESPACE_ASSOCIATIONS (name_space)
4160 = tree_cons (current_namespace, 0,
4161 DECL_NAMESPACE_ASSOCIATIONS (name_space));
4164 else
4165 warning (OPT_Wattributes, "%qD attribute directive ignored", name);
4169 /* Like pushdecl, only it places X in the global scope if appropriate.
4170 Calls cp_finish_decl to register the variable, initializing it with
4171 *INIT, if INIT is non-NULL. */
4173 static tree
4174 pushdecl_top_level_1 (tree x, tree *init, bool is_friend)
4176 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4177 push_to_top_level ();
4178 x = pushdecl_namespace_level (x, is_friend);
4179 if (init)
4180 cp_finish_decl (x, *init, false, NULL_TREE, 0);
4181 pop_from_top_level ();
4182 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4183 return x;
4186 /* Like pushdecl, only it places X in the global scope if appropriate. */
4188 tree
4189 pushdecl_top_level (tree x)
4191 return pushdecl_top_level_1 (x, NULL, false);
4194 /* Like pushdecl_top_level, but adding the IS_FRIEND parameter. */
4196 tree
4197 pushdecl_top_level_maybe_friend (tree x, bool is_friend)
4199 return pushdecl_top_level_1 (x, NULL, is_friend);
4202 /* Like pushdecl, only it places X in the global scope if
4203 appropriate. Calls cp_finish_decl to register the variable,
4204 initializing it with INIT. */
4206 tree
4207 pushdecl_top_level_and_finish (tree x, tree init)
4209 return pushdecl_top_level_1 (x, &init, false);
4212 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
4213 duplicates. The first list becomes the tail of the result.
4215 The algorithm is O(n^2). We could get this down to O(n log n) by
4216 doing a sort on the addresses of the functions, if that becomes
4217 necessary. */
4219 static tree
4220 merge_functions (tree s1, tree s2)
4222 for (; s2; s2 = OVL_NEXT (s2))
4224 tree fn2 = OVL_CURRENT (s2);
4225 tree fns1;
4227 for (fns1 = s1; fns1; fns1 = OVL_NEXT (fns1))
4229 tree fn1 = OVL_CURRENT (fns1);
4231 /* If the function from S2 is already in S1, there is no
4232 need to add it again. For `extern "C"' functions, we
4233 might have two FUNCTION_DECLs for the same function, in
4234 different namespaces, but let's leave them in case
4235 they have different default arguments. */
4236 if (fn1 == fn2)
4237 break;
4240 /* If we exhausted all of the functions in S1, FN2 is new. */
4241 if (!fns1)
4242 s1 = build_overload (fn2, s1);
4244 return s1;
4247 /* Returns TRUE iff OLD and NEW are the same entity.
4249 3 [basic]/3: An entity is a value, object, reference, function,
4250 enumerator, type, class member, template, template specialization,
4251 namespace, parameter pack, or this.
4253 7.3.4 [namespace.udir]/4: If name lookup finds a declaration for a name
4254 in two different namespaces, and the declarations do not declare the
4255 same entity and do not declare functions, the use of the name is
4256 ill-formed. */
4258 static bool
4259 same_entity_p (tree one, tree two)
4261 if (one == two)
4262 return true;
4263 if (!one || !two)
4264 return false;
4265 if (TREE_CODE (one) == TYPE_DECL
4266 && TREE_CODE (two) == TYPE_DECL
4267 && same_type_p (TREE_TYPE (one), TREE_TYPE (two)))
4268 return true;
4269 return false;
4272 /* This should return an error not all definitions define functions.
4273 It is not an error if we find two functions with exactly the
4274 same signature, only if these are selected in overload resolution.
4275 old is the current set of bindings, new_binding the freshly-found binding.
4276 XXX Do we want to give *all* candidates in case of ambiguity?
4277 XXX In what way should I treat extern declarations?
4278 XXX I don't want to repeat the entire duplicate_decls here */
4280 static void
4281 ambiguous_decl (struct scope_binding *old, cxx_binding *new_binding, int flags)
4283 tree val, type;
4284 gcc_assert (old != NULL);
4286 /* Copy the type. */
4287 type = new_binding->type;
4288 if (LOOKUP_NAMESPACES_ONLY (flags)
4289 || (type && hidden_name_p (type) && !(flags & LOOKUP_HIDDEN)))
4290 type = NULL_TREE;
4292 /* Copy the value. */
4293 val = new_binding->value;
4294 if (val)
4296 if (!(flags & LOOKUP_HIDDEN))
4297 val = remove_hidden_names (val);
4298 if (val)
4299 switch (TREE_CODE (val))
4301 case TEMPLATE_DECL:
4302 /* If we expect types or namespaces, and not templates,
4303 or this is not a template class. */
4304 if ((LOOKUP_QUALIFIERS_ONLY (flags)
4305 && !DECL_TYPE_TEMPLATE_P (val)))
4306 val = NULL_TREE;
4307 break;
4308 case TYPE_DECL:
4309 if (LOOKUP_NAMESPACES_ONLY (flags)
4310 || (type && (flags & LOOKUP_PREFER_TYPES)))
4311 val = NULL_TREE;
4312 break;
4313 case NAMESPACE_DECL:
4314 if (LOOKUP_TYPES_ONLY (flags))
4315 val = NULL_TREE;
4316 break;
4317 case FUNCTION_DECL:
4318 /* Ignore built-in functions that are still anticipated. */
4319 if (LOOKUP_QUALIFIERS_ONLY (flags))
4320 val = NULL_TREE;
4321 break;
4322 default:
4323 if (LOOKUP_QUALIFIERS_ONLY (flags))
4324 val = NULL_TREE;
4328 /* If val is hidden, shift down any class or enumeration name. */
4329 if (!val)
4331 val = type;
4332 type = NULL_TREE;
4335 if (!old->value)
4336 old->value = val;
4337 else if (val && !same_entity_p (val, old->value))
4339 if (is_overloaded_fn (old->value) && is_overloaded_fn (val))
4340 old->value = merge_functions (old->value, val);
4341 else
4343 old->value = tree_cons (NULL_TREE, old->value,
4344 build_tree_list (NULL_TREE, val));
4345 TREE_TYPE (old->value) = error_mark_node;
4349 if (!old->type)
4350 old->type = type;
4351 else if (type && old->type != type)
4353 old->type = tree_cons (NULL_TREE, old->type,
4354 build_tree_list (NULL_TREE, type));
4355 TREE_TYPE (old->type) = error_mark_node;
4359 /* Return the declarations that are members of the namespace NS. */
4361 tree
4362 cp_namespace_decls (tree ns)
4364 return NAMESPACE_LEVEL (ns)->names;
4367 /* Combine prefer_type and namespaces_only into flags. */
4369 static int
4370 lookup_flags (int prefer_type, int namespaces_only)
4372 if (namespaces_only)
4373 return LOOKUP_PREFER_NAMESPACES;
4374 if (prefer_type > 1)
4375 return LOOKUP_PREFER_TYPES;
4376 if (prefer_type > 0)
4377 return LOOKUP_PREFER_BOTH;
4378 return 0;
4381 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
4382 ignore it or not. Subroutine of lookup_name_real and
4383 lookup_type_scope. */
4385 static bool
4386 qualify_lookup (tree val, int flags)
4388 if (val == NULL_TREE)
4389 return false;
4390 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
4391 return true;
4392 if (flags & LOOKUP_PREFER_TYPES)
4394 tree target_val = strip_using_decl (val);
4395 if (TREE_CODE (target_val) == TYPE_DECL
4396 || TREE_CODE (target_val) == TEMPLATE_DECL)
4397 return true;
4399 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
4400 return false;
4401 /* Look through lambda things that we shouldn't be able to see. */
4402 if (is_lambda_ignored_entity (val))
4403 return false;
4404 return true;
4407 /* Given a lookup that returned VAL, decide if we want to ignore it or
4408 not based on DECL_ANTICIPATED. */
4410 bool
4411 hidden_name_p (tree val)
4413 if (DECL_P (val)
4414 && DECL_LANG_SPECIFIC (val)
4415 && TYPE_FUNCTION_OR_TEMPLATE_DECL_P (val)
4416 && DECL_ANTICIPATED (val))
4417 return true;
4418 if (TREE_CODE (val) == OVERLOAD)
4420 for (tree o = val; o; o = OVL_CHAIN (o))
4421 if (!hidden_name_p (OVL_FUNCTION (o)))
4422 return false;
4423 return true;
4425 return false;
4428 /* Remove any hidden declarations from a possibly overloaded set
4429 of functions. */
4431 tree
4432 remove_hidden_names (tree fns)
4434 if (!fns)
4435 return fns;
4437 if (DECL_P (fns) && hidden_name_p (fns))
4438 fns = NULL_TREE;
4439 else if (TREE_CODE (fns) == OVERLOAD)
4441 tree o;
4443 for (o = fns; o; o = OVL_NEXT (o))
4444 if (hidden_name_p (OVL_CURRENT (o)))
4445 break;
4446 if (o)
4448 tree n = NULL_TREE;
4450 for (o = fns; o; o = OVL_NEXT (o))
4451 if (!hidden_name_p (OVL_CURRENT (o)))
4452 n = build_overload (OVL_CURRENT (o), n);
4453 fns = n;
4457 return fns;
4460 /* Suggest alternatives for NAME, an IDENTIFIER_NODE for which name
4461 lookup failed. Search through all available namespaces and print out
4462 possible candidates. If no exact matches are found, and
4463 SUGGEST_MISSPELLINGS is true, then also look for near-matches and
4464 suggest the best near-match, if there is one. */
4466 void
4467 suggest_alternatives_for (location_t location, tree name,
4468 bool suggest_misspellings)
4470 vec<tree> candidates = vNULL;
4471 vec<tree> namespaces_to_search = vNULL;
4472 int max_to_search = PARAM_VALUE (CXX_MAX_NAMESPACES_FOR_DIAGNOSTIC_HELP);
4473 int n_searched = 0;
4474 tree t;
4475 unsigned ix;
4477 namespaces_to_search.safe_push (global_namespace);
4479 while (!namespaces_to_search.is_empty ()
4480 && n_searched < max_to_search)
4482 tree scope = namespaces_to_search.pop ();
4483 struct scope_binding binding = EMPTY_SCOPE_BINDING;
4484 cp_binding_level *level = NAMESPACE_LEVEL (scope);
4486 /* Look in this namespace. */
4487 qualified_lookup_using_namespace (name, scope, &binding, 0);
4489 n_searched++;
4491 if (binding.value)
4492 candidates.safe_push (binding.value);
4494 /* Add child namespaces. */
4495 for (t = level->namespaces; t; t = DECL_CHAIN (t))
4496 namespaces_to_search.safe_push (t);
4499 /* If we stopped before we could examine all namespaces, inform the
4500 user. Do this even if we don't have any candidates, since there
4501 might be more candidates further down that we weren't able to
4502 find. */
4503 if (n_searched >= max_to_search
4504 && !namespaces_to_search.is_empty ())
4505 inform (location,
4506 "maximum limit of %d namespaces searched for %qE",
4507 max_to_search, name);
4509 namespaces_to_search.release ();
4511 /* Nothing useful to report for NAME. Report on likely misspellings,
4512 or do nothing. */
4513 if (candidates.is_empty ())
4515 if (suggest_misspellings)
4517 const char *fuzzy_name = lookup_name_fuzzy (name, FUZZY_LOOKUP_NAME);
4518 if (fuzzy_name)
4520 gcc_rich_location richloc (location);
4521 richloc.add_fixit_replace (fuzzy_name);
4522 inform_at_rich_loc (&richloc, "suggested alternative: %qs",
4523 fuzzy_name);
4526 return;
4529 inform_n (location, candidates.length (),
4530 "suggested alternative:",
4531 "suggested alternatives:");
4533 FOR_EACH_VEC_ELT (candidates, ix, t)
4534 inform (location_of (t), " %qE", t);
4536 candidates.release ();
4539 /* Look for alternatives for NAME, an IDENTIFIER_NODE for which name
4540 lookup failed within the explicitly provided SCOPE. Suggest the
4541 the best meaningful candidates (if any) as a fix-it hint.
4542 Return true iff a suggestion was provided. */
4544 bool
4545 suggest_alternative_in_explicit_scope (location_t location, tree name,
4546 tree scope)
4548 /* Resolve any namespace aliases. */
4549 scope = ORIGINAL_NAMESPACE (scope);
4551 cp_binding_level *level = NAMESPACE_LEVEL (scope);
4553 best_match <tree, tree> bm (name);
4554 consider_binding_level (name, bm, level, false, FUZZY_LOOKUP_NAME);
4556 /* See if we have a good suggesion for the user. */
4557 tree best_id = bm.get_best_meaningful_candidate ();
4558 if (best_id)
4560 const char *fuzzy_name = IDENTIFIER_POINTER (best_id);
4561 gcc_rich_location richloc (location);
4562 richloc.add_fixit_replace (fuzzy_name);
4563 inform_at_rich_loc (&richloc, "suggested alternative: %qs",
4564 fuzzy_name);
4565 return true;
4568 return false;
4571 /* Unscoped lookup of a global: iterate over current namespaces,
4572 considering using-directives. */
4574 static tree
4575 unqualified_namespace_lookup_1 (tree name, int flags)
4577 tree initial = current_decl_namespace ();
4578 tree scope = initial;
4579 tree siter;
4580 cp_binding_level *level;
4581 tree val = NULL_TREE;
4583 for (; !val; scope = CP_DECL_CONTEXT (scope))
4585 struct scope_binding binding = EMPTY_SCOPE_BINDING;
4586 cxx_binding *b =
4587 cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
4589 if (b)
4590 ambiguous_decl (&binding, b, flags);
4592 /* Add all _DECLs seen through local using-directives. */
4593 for (level = current_binding_level;
4594 level->kind != sk_namespace;
4595 level = level->level_chain)
4596 if (!lookup_using_namespace (name, &binding, level->using_directives,
4597 scope, flags))
4598 /* Give up because of error. */
4599 return error_mark_node;
4601 /* Add all _DECLs seen through global using-directives. */
4602 /* XXX local and global using lists should work equally. */
4603 siter = initial;
4604 while (1)
4606 if (!lookup_using_namespace (name, &binding,
4607 DECL_NAMESPACE_USING (siter),
4608 scope, flags))
4609 /* Give up because of error. */
4610 return error_mark_node;
4611 if (siter == scope) break;
4612 siter = CP_DECL_CONTEXT (siter);
4615 val = binding.value;
4616 if (scope == global_namespace)
4617 break;
4619 return val;
4622 /* Wrapper for unqualified_namespace_lookup_1. */
4624 static tree
4625 unqualified_namespace_lookup (tree name, int flags)
4627 tree ret;
4628 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4629 ret = unqualified_namespace_lookup_1 (name, flags);
4630 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4631 return ret;
4634 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
4635 or a class TYPE).
4637 If PREFER_TYPE is > 0, we only return TYPE_DECLs or namespaces.
4638 If PREFER_TYPE is > 1, we only return TYPE_DECLs.
4640 Returns a DECL (or OVERLOAD, or BASELINK) representing the
4641 declaration found. If no suitable declaration can be found,
4642 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
4643 neither a class-type nor a namespace a diagnostic is issued. */
4645 tree
4646 lookup_qualified_name (tree scope, tree name, int prefer_type, bool complain,
4647 bool find_hidden)
4649 tree t = NULL_TREE;
4651 if (TREE_CODE (scope) == NAMESPACE_DECL)
4653 struct scope_binding binding = EMPTY_SCOPE_BINDING;
4655 int flags = lookup_flags (prefer_type, /*namespaces_only*/false);
4656 if (find_hidden)
4657 flags |= LOOKUP_HIDDEN;
4658 if (qualified_lookup_using_namespace (name, scope, &binding, flags))
4659 t = binding.value;
4661 else if (cxx_dialect != cxx98 && TREE_CODE (scope) == ENUMERAL_TYPE)
4662 t = lookup_enumerator (scope, name);
4663 else if (is_class_type (scope, complain))
4664 t = lookup_member (scope, name, 2, prefer_type, tf_warning_or_error);
4666 if (!t)
4667 return error_mark_node;
4668 return t;
4671 /* Subroutine of unqualified_namespace_lookup:
4672 Add the bindings of NAME in used namespaces to VAL.
4673 We are currently looking for names in namespace SCOPE, so we
4674 look through USINGS for using-directives of namespaces
4675 which have SCOPE as a common ancestor with the current scope.
4676 Returns false on errors. */
4678 static bool
4679 lookup_using_namespace (tree name, struct scope_binding *val,
4680 tree usings, tree scope, int flags)
4682 tree iter;
4683 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4684 /* Iterate over all used namespaces in current, searching for using
4685 directives of scope. */
4686 for (iter = usings; iter; iter = TREE_CHAIN (iter))
4687 if (TREE_VALUE (iter) == scope)
4689 tree used = ORIGINAL_NAMESPACE (TREE_PURPOSE (iter));
4690 cxx_binding *val1 =
4691 cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (used), name);
4692 /* Resolve ambiguities. */
4693 if (val1)
4694 ambiguous_decl (val, val1, flags);
4696 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4697 return val->value != error_mark_node;
4700 /* Returns true iff VEC contains TARGET. */
4702 static bool
4703 tree_vec_contains (vec<tree, va_gc> *vec, tree target)
4705 unsigned int i;
4706 tree elt;
4707 FOR_EACH_VEC_SAFE_ELT (vec,i,elt)
4708 if (elt == target)
4709 return true;
4710 return false;
4713 /* [namespace.qual]
4714 Accepts the NAME to lookup and its qualifying SCOPE.
4715 Returns the name/type pair found into the cxx_binding *RESULT,
4716 or false on error. */
4718 static bool
4719 qualified_lookup_using_namespace (tree name, tree scope,
4720 struct scope_binding *result, int flags)
4722 /* Maintain a list of namespaces visited... */
4723 vec<tree, va_gc> *seen = NULL;
4724 vec<tree, va_gc> *seen_inline = NULL;
4725 /* ... and a list of namespace yet to see. */
4726 vec<tree, va_gc> *todo = NULL;
4727 vec<tree, va_gc> *todo_maybe = NULL;
4728 vec<tree, va_gc> *todo_inline = NULL;
4729 tree usings;
4730 timevar_start (TV_NAME_LOOKUP);
4731 /* Look through namespace aliases. */
4732 scope = ORIGINAL_NAMESPACE (scope);
4734 query_oracle (name);
4736 /* Algorithm: Starting with SCOPE, walk through the set of used
4737 namespaces. For each used namespace, look through its inline
4738 namespace set for any bindings and usings. If no bindings are
4739 found, add any usings seen to the set of used namespaces. */
4740 vec_safe_push (todo, scope);
4742 while (todo->length ())
4744 bool found_here;
4745 scope = todo->pop ();
4746 if (tree_vec_contains (seen, scope))
4747 continue;
4748 vec_safe_push (seen, scope);
4749 vec_safe_push (todo_inline, scope);
4751 found_here = false;
4752 while (todo_inline->length ())
4754 cxx_binding *binding;
4756 scope = todo_inline->pop ();
4757 if (tree_vec_contains (seen_inline, scope))
4758 continue;
4759 vec_safe_push (seen_inline, scope);
4761 binding =
4762 cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
4763 if (binding)
4765 ambiguous_decl (result, binding, flags);
4766 if (result->type || result->value)
4767 found_here = true;
4770 for (usings = DECL_NAMESPACE_USING (scope); usings;
4771 usings = TREE_CHAIN (usings))
4772 if (!TREE_INDIRECT_USING (usings))
4774 if (is_associated_namespace (scope, TREE_PURPOSE (usings)))
4775 vec_safe_push (todo_inline, TREE_PURPOSE (usings));
4776 else
4777 vec_safe_push (todo_maybe, TREE_PURPOSE (usings));
4781 if (found_here)
4782 vec_safe_truncate (todo_maybe, 0);
4783 else
4784 while (vec_safe_length (todo_maybe))
4785 vec_safe_push (todo, todo_maybe->pop ());
4787 vec_free (todo);
4788 vec_free (todo_maybe);
4789 vec_free (todo_inline);
4790 vec_free (seen);
4791 vec_free (seen_inline);
4792 timevar_stop (TV_NAME_LOOKUP);
4793 return result->value != error_mark_node;
4796 /* Helper function for lookup_name_fuzzy.
4797 Traverse binding level LVL, looking for good name matches for NAME
4798 (and BM). */
4799 static void
4800 consider_binding_level (tree name, best_match <tree, tree> &bm,
4801 cp_binding_level *lvl, bool look_within_fields,
4802 enum lookup_name_fuzzy_kind kind)
4804 if (look_within_fields)
4805 if (lvl->this_entity && TREE_CODE (lvl->this_entity) == RECORD_TYPE)
4807 tree type = lvl->this_entity;
4808 bool want_type_p = (kind == FUZZY_LOOKUP_TYPENAME);
4809 tree best_matching_field
4810 = lookup_member_fuzzy (type, name, want_type_p);
4811 if (best_matching_field)
4812 bm.consider (best_matching_field);
4815 for (tree t = lvl->names; t; t = TREE_CHAIN (t))
4817 tree d = t;
4819 /* OVERLOADs or decls from using declaration are wrapped into
4820 TREE_LIST. */
4821 if (TREE_CODE (d) == TREE_LIST)
4823 d = TREE_VALUE (d);
4824 d = OVL_CURRENT (d);
4827 /* Don't use bindings from implicitly declared functions,
4828 as they were likely misspellings themselves. */
4829 if (TREE_TYPE (d) == error_mark_node)
4830 continue;
4832 /* Skip anticipated decls of builtin functions. */
4833 if (TREE_CODE (d) == FUNCTION_DECL
4834 && DECL_BUILT_IN (d)
4835 && DECL_ANTICIPATED (d))
4836 continue;
4838 if (tree name = DECL_NAME (d))
4839 /* Ignore internal names with spaces in them. */
4840 if (!strchr (IDENTIFIER_POINTER (name), ' '))
4841 bm.consider (DECL_NAME (d));
4845 /* Search for near-matches for NAME within the current bindings, and within
4846 macro names, returning the best match as a const char *, or NULL if
4847 no reasonable match is found. */
4849 const char *
4850 lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind)
4852 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
4854 best_match <tree, tree> bm (name);
4856 cp_binding_level *lvl;
4857 for (lvl = scope_chain->class_bindings; lvl; lvl = lvl->level_chain)
4858 consider_binding_level (name, bm, lvl, true, kind);
4860 for (lvl = current_binding_level; lvl; lvl = lvl->level_chain)
4861 consider_binding_level (name, bm, lvl, false, kind);
4863 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
4865 x = SOME_OTHER_MACRO (y);
4866 then "SOME_OTHER_MACRO" will survive to the frontend and show up
4867 as a misspelled identifier.
4869 Use the best distance so far so that a candidate is only set if
4870 a macro is better than anything so far. This allows early rejection
4871 (without calculating the edit distance) of macro names that must have
4872 distance >= bm.get_best_distance (), and means that we only get a
4873 non-NULL result for best_macro_match if it's better than any of
4874 the identifiers already checked. */
4875 best_macro_match bmm (name, bm.get_best_distance (), parse_in);
4876 cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
4877 /* If a macro is the closest so far to NAME, suggest it. */
4878 if (best_macro)
4879 return (const char *)best_macro->ident.str;
4881 /* Try the "starts_decl_specifier_p" keywords to detect
4882 "singed" vs "signed" typos. */
4883 for (unsigned i = 0; i < num_c_common_reswords; i++)
4885 const c_common_resword *resword = &c_common_reswords[i];
4887 if (!cp_keyword_starts_decl_specifier_p (resword->rid))
4888 continue;
4890 tree resword_identifier = ridpointers [resword->rid];
4891 if (!resword_identifier)
4892 continue;
4893 gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
4895 /* Only consider reserved words that survived the
4896 filtering in init_reswords (e.g. for -std). */
4897 if (!C_IS_RESERVED_WORD (resword_identifier))
4898 continue;
4900 bm.consider (resword_identifier);
4903 /* See if we have a good suggesion for the user. */
4904 tree best_id = bm.get_best_meaningful_candidate ();
4905 if (best_id)
4906 return IDENTIFIER_POINTER (best_id);
4908 /* No meaningful suggestion available. */
4909 return NULL;
4912 /* Subroutine of outer_binding.
4914 Returns TRUE if BINDING is a binding to a template parameter of
4915 SCOPE. In that case SCOPE is the scope of a primary template
4916 parameter -- in the sense of G++, i.e, a template that has its own
4917 template header.
4919 Returns FALSE otherwise. */
4921 static bool
4922 binding_to_template_parms_of_scope_p (cxx_binding *binding,
4923 cp_binding_level *scope)
4925 tree binding_value, tmpl, tinfo;
4926 int level;
4928 if (!binding || !scope || !scope->this_entity)
4929 return false;
4931 binding_value = binding->value ? binding->value : binding->type;
4932 tinfo = get_template_info (scope->this_entity);
4934 /* BINDING_VALUE must be a template parm. */
4935 if (binding_value == NULL_TREE
4936 || (!DECL_P (binding_value)
4937 || !DECL_TEMPLATE_PARM_P (binding_value)))
4938 return false;
4940 /* The level of BINDING_VALUE. */
4941 level =
4942 template_type_parameter_p (binding_value)
4943 ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX
4944 (TREE_TYPE (binding_value)))
4945 : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value));
4947 /* The template of the current scope, iff said scope is a primary
4948 template. */
4949 tmpl = (tinfo
4950 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
4951 ? TI_TEMPLATE (tinfo)
4952 : NULL_TREE);
4954 /* If the level of the parm BINDING_VALUE equals the depth of TMPL,
4955 then BINDING_VALUE is a parameter of TMPL. */
4956 return (tmpl && level == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
4959 /* Return the innermost non-namespace binding for NAME from a scope
4960 containing BINDING, or, if BINDING is NULL, the current scope.
4961 Please note that for a given template, the template parameters are
4962 considered to be in the scope containing the current scope.
4963 If CLASS_P is false, then class bindings are ignored. */
4965 cxx_binding *
4966 outer_binding (tree name,
4967 cxx_binding *binding,
4968 bool class_p)
4970 cxx_binding *outer;
4971 cp_binding_level *scope;
4972 cp_binding_level *outer_scope;
4974 if (binding)
4976 scope = binding->scope->level_chain;
4977 outer = binding->previous;
4979 else
4981 scope = current_binding_level;
4982 outer = IDENTIFIER_BINDING (name);
4984 outer_scope = outer ? outer->scope : NULL;
4986 /* Because we create class bindings lazily, we might be missing a
4987 class binding for NAME. If there are any class binding levels
4988 between the LAST_BINDING_LEVEL and the scope in which OUTER was
4989 declared, we must lookup NAME in those class scopes. */
4990 if (class_p)
4991 while (scope && scope != outer_scope && scope->kind != sk_namespace)
4993 if (scope->kind == sk_class)
4995 cxx_binding *class_binding;
4997 class_binding = get_class_binding (name, scope);
4998 if (class_binding)
5000 /* Thread this new class-scope binding onto the
5001 IDENTIFIER_BINDING list so that future lookups
5002 find it quickly. */
5003 class_binding->previous = outer;
5004 if (binding)
5005 binding->previous = class_binding;
5006 else
5007 IDENTIFIER_BINDING (name) = class_binding;
5008 return class_binding;
5011 /* If we are in a member template, the template parms of the member
5012 template are considered to be inside the scope of the containing
5013 class, but within G++ the class bindings are all pushed between the
5014 template parms and the function body. So if the outer binding is
5015 a template parm for the current scope, return it now rather than
5016 look for a class binding. */
5017 if (outer_scope && outer_scope->kind == sk_template_parms
5018 && binding_to_template_parms_of_scope_p (outer, scope))
5019 return outer;
5021 scope = scope->level_chain;
5024 return outer;
5027 /* Return the innermost block-scope or class-scope value binding for
5028 NAME, or NULL_TREE if there is no such binding. */
5030 tree
5031 innermost_non_namespace_value (tree name)
5033 cxx_binding *binding;
5034 binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true);
5035 return binding ? binding->value : NULL_TREE;
5038 /* Look up NAME in the current binding level and its superiors in the
5039 namespace of variables, functions and typedefs. Return a ..._DECL
5040 node of some kind representing its definition if there is only one
5041 such declaration, or return a TREE_LIST with all the overloaded
5042 definitions if there are many, or return 0 if it is undefined.
5043 Hidden name, either friend declaration or built-in function, are
5044 not ignored.
5046 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
5047 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
5048 Otherwise we prefer non-TYPE_DECLs.
5050 If NONCLASS is nonzero, bindings in class scopes are ignored. If
5051 BLOCK_P is false, bindings in block scopes are ignored. */
5053 static tree
5054 lookup_name_real_1 (tree name, int prefer_type, int nonclass, bool block_p,
5055 int namespaces_only, int flags)
5057 cxx_binding *iter;
5058 tree val = NULL_TREE;
5060 query_oracle (name);
5062 /* Conversion operators are handled specially because ordinary
5063 unqualified name lookup will not find template conversion
5064 operators. */
5065 if (IDENTIFIER_TYPENAME_P (name))
5067 cp_binding_level *level;
5069 for (level = current_binding_level;
5070 level && level->kind != sk_namespace;
5071 level = level->level_chain)
5073 tree class_type;
5074 tree operators;
5076 /* A conversion operator can only be declared in a class
5077 scope. */
5078 if (level->kind != sk_class)
5079 continue;
5081 /* Lookup the conversion operator in the class. */
5082 class_type = level->this_entity;
5083 operators = lookup_fnfields (class_type, name, /*protect=*/0);
5084 if (operators)
5085 return operators;
5088 return NULL_TREE;
5091 flags |= lookup_flags (prefer_type, namespaces_only);
5093 /* First, look in non-namespace scopes. */
5095 if (current_class_type == NULL_TREE)
5096 nonclass = 1;
5098 if (block_p || !nonclass)
5099 for (iter = outer_binding (name, NULL, !nonclass);
5100 iter;
5101 iter = outer_binding (name, iter, !nonclass))
5103 tree binding;
5105 /* Skip entities we don't want. */
5106 if (LOCAL_BINDING_P (iter) ? !block_p : nonclass)
5107 continue;
5109 /* If this is the kind of thing we're looking for, we're done. */
5110 if (qualify_lookup (iter->value, flags))
5111 binding = iter->value;
5112 else if ((flags & LOOKUP_PREFER_TYPES)
5113 && qualify_lookup (iter->type, flags))
5114 binding = iter->type;
5115 else
5116 binding = NULL_TREE;
5118 if (binding)
5120 if (hidden_name_p (binding))
5122 /* A non namespace-scope binding can only be hidden in the
5123 presence of a local class, due to friend declarations.
5125 In particular, consider:
5127 struct C;
5128 void f() {
5129 struct A {
5130 friend struct B;
5131 friend struct C;
5132 void g() {
5133 B* b; // error: B is hidden
5134 C* c; // OK, finds ::C
5137 B *b; // error: B is hidden
5138 C *c; // OK, finds ::C
5139 struct B {};
5140 B *bb; // OK
5143 The standard says that "B" is a local class in "f"
5144 (but not nested within "A") -- but that name lookup
5145 for "B" does not find this declaration until it is
5146 declared directly with "f".
5148 In particular:
5150 [class.friend]
5152 If a friend declaration appears in a local class and
5153 the name specified is an unqualified name, a prior
5154 declaration is looked up without considering scopes
5155 that are outside the innermost enclosing non-class
5156 scope. For a friend function declaration, if there is
5157 no prior declaration, the program is ill-formed. For a
5158 friend class declaration, if there is no prior
5159 declaration, the class that is specified belongs to the
5160 innermost enclosing non-class scope, but if it is
5161 subsequently referenced, its name is not found by name
5162 lookup until a matching declaration is provided in the
5163 innermost enclosing nonclass scope.
5165 So just keep looking for a non-hidden binding.
5167 gcc_assert (TREE_CODE (binding) == TYPE_DECL);
5168 continue;
5170 val = binding;
5171 break;
5175 /* Now lookup in namespace scopes. */
5176 if (!val)
5177 val = unqualified_namespace_lookup (name, flags);
5179 /* Anticipated built-ins and friends aren't found by normal lookup. */
5180 if (val && !(flags & LOOKUP_HIDDEN))
5181 val = remove_hidden_names (val);
5183 /* If we have a single function from a using decl, pull it out. */
5184 if (val && TREE_CODE (val) == OVERLOAD && !really_overloaded_fn (val))
5185 val = OVL_FUNCTION (val);
5187 return val;
5190 /* Wrapper for lookup_name_real_1. */
5192 tree
5193 lookup_name_real (tree name, int prefer_type, int nonclass, bool block_p,
5194 int namespaces_only, int flags)
5196 tree ret;
5197 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5198 ret = lookup_name_real_1 (name, prefer_type, nonclass, block_p,
5199 namespaces_only, flags);
5200 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5201 return ret;
5204 tree
5205 lookup_name_nonclass (tree name)
5207 return lookup_name_real (name, 0, 1, /*block_p=*/true, 0, 0);
5210 tree
5211 lookup_function_nonclass (tree name, vec<tree, va_gc> *args, bool block_p)
5213 return
5214 lookup_arg_dependent (name,
5215 lookup_name_real (name, 0, 1, block_p, 0, 0),
5216 args);
5219 tree
5220 lookup_name (tree name)
5222 return lookup_name_real (name, 0, 0, /*block_p=*/true, 0, 0);
5225 tree
5226 lookup_name_prefer_type (tree name, int prefer_type)
5228 return lookup_name_real (name, prefer_type, 0, /*block_p=*/true, 0, 0);
5231 /* Look up NAME for type used in elaborated name specifier in
5232 the scopes given by SCOPE. SCOPE can be either TS_CURRENT or
5233 TS_WITHIN_ENCLOSING_NON_CLASS. Although not implied by the
5234 name, more scopes are checked if cleanup or template parameter
5235 scope is encountered.
5237 Unlike lookup_name_real, we make sure that NAME is actually
5238 declared in the desired scope, not from inheritance, nor using
5239 directive. For using declaration, there is DR138 still waiting
5240 to be resolved. Hidden name coming from an earlier friend
5241 declaration is also returned.
5243 A TYPE_DECL best matching the NAME is returned. Catching error
5244 and issuing diagnostics are caller's responsibility. */
5246 static tree
5247 lookup_type_scope_1 (tree name, tag_scope scope)
5249 cxx_binding *iter = NULL;
5250 tree val = NULL_TREE;
5252 /* Look in non-namespace scope first. */
5253 if (current_binding_level->kind != sk_namespace)
5254 iter = outer_binding (name, NULL, /*class_p=*/ true);
5255 for (; iter; iter = outer_binding (name, iter, /*class_p=*/ true))
5257 /* Check if this is the kind of thing we're looking for.
5258 If SCOPE is TS_CURRENT, also make sure it doesn't come from
5259 base class. For ITER->VALUE, we can simply use
5260 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use
5261 our own check.
5263 We check ITER->TYPE before ITER->VALUE in order to handle
5264 typedef struct C {} C;
5265 correctly. */
5267 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES)
5268 && (scope != ts_current
5269 || LOCAL_BINDING_P (iter)
5270 || DECL_CONTEXT (iter->type) == iter->scope->this_entity))
5271 val = iter->type;
5272 else if ((scope != ts_current
5273 || !INHERITED_VALUE_BINDING_P (iter))
5274 && qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
5275 val = iter->value;
5277 if (val)
5278 break;
5281 /* Look in namespace scope. */
5282 if (!val)
5284 iter = cp_binding_level_find_binding_for_name
5285 (NAMESPACE_LEVEL (current_decl_namespace ()), name);
5287 if (iter)
5289 /* If this is the kind of thing we're looking for, we're done. */
5290 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES))
5291 val = iter->type;
5292 else if (qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
5293 val = iter->value;
5298 /* Type found, check if it is in the allowed scopes, ignoring cleanup
5299 and template parameter scopes. */
5300 if (val)
5302 cp_binding_level *b = current_binding_level;
5303 while (b)
5305 if (iter->scope == b)
5306 return val;
5308 if (b->kind == sk_cleanup || b->kind == sk_template_parms
5309 || b->kind == sk_function_parms)
5310 b = b->level_chain;
5311 else if (b->kind == sk_class
5312 && scope == ts_within_enclosing_non_class)
5313 b = b->level_chain;
5314 else
5315 break;
5319 return NULL_TREE;
5322 /* Wrapper for lookup_type_scope_1. */
5324 tree
5325 lookup_type_scope (tree name, tag_scope scope)
5327 tree ret;
5328 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5329 ret = lookup_type_scope_1 (name, scope);
5330 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5331 return ret;
5335 /* Similar to `lookup_name' but look only in the innermost non-class
5336 binding level. */
5338 static tree
5339 lookup_name_innermost_nonclass_level_1 (tree name)
5341 cp_binding_level *b;
5342 tree t = NULL_TREE;
5344 b = innermost_nonclass_level ();
5346 if (b->kind == sk_namespace)
5348 t = IDENTIFIER_NAMESPACE_VALUE (name);
5350 /* extern "C" function() */
5351 if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
5352 t = TREE_VALUE (t);
5354 else if (IDENTIFIER_BINDING (name)
5355 && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
5357 cxx_binding *binding;
5358 binding = IDENTIFIER_BINDING (name);
5359 while (1)
5361 if (binding->scope == b
5362 && !(VAR_P (binding->value)
5363 && DECL_DEAD_FOR_LOCAL (binding->value)))
5364 return binding->value;
5366 if (b->kind == sk_cleanup)
5367 b = b->level_chain;
5368 else
5369 break;
5373 return t;
5376 /* Wrapper for lookup_name_innermost_nonclass_level_1. */
5378 tree
5379 lookup_name_innermost_nonclass_level (tree name)
5381 tree ret;
5382 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5383 ret = lookup_name_innermost_nonclass_level_1 (name);
5384 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5385 return ret;
5389 /* Returns true iff DECL is a block-scope extern declaration of a function
5390 or variable. */
5392 bool
5393 is_local_extern (tree decl)
5395 cxx_binding *binding;
5397 /* For functions, this is easy. */
5398 if (TREE_CODE (decl) == FUNCTION_DECL)
5399 return DECL_LOCAL_FUNCTION_P (decl);
5401 if (!VAR_P (decl))
5402 return false;
5403 if (!current_function_decl)
5404 return false;
5406 /* For variables, this is not easy. We need to look at the binding stack
5407 for the identifier to see whether the decl we have is a local. */
5408 for (binding = IDENTIFIER_BINDING (DECL_NAME (decl));
5409 binding && binding->scope->kind != sk_namespace;
5410 binding = binding->previous)
5411 if (binding->value == decl)
5412 return LOCAL_BINDING_P (binding);
5414 return false;
5417 /* Like lookup_name_innermost_nonclass_level, but for types. */
5419 static tree
5420 lookup_type_current_level (tree name)
5422 tree t = NULL_TREE;
5424 timevar_start (TV_NAME_LOOKUP);
5425 gcc_assert (current_binding_level->kind != sk_namespace);
5427 if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
5428 && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
5430 cp_binding_level *b = current_binding_level;
5431 while (1)
5433 if (purpose_member (name, b->type_shadowed))
5435 t = REAL_IDENTIFIER_TYPE_VALUE (name);
5436 break;
5438 if (b->kind == sk_cleanup)
5439 b = b->level_chain;
5440 else
5441 break;
5445 timevar_stop (TV_NAME_LOOKUP);
5446 return t;
5449 /* [basic.lookup.koenig] */
5450 /* A nonzero return value in the functions below indicates an error. */
5452 struct arg_lookup
5454 tree name;
5455 vec<tree, va_gc> *args;
5456 vec<tree, va_gc> *namespaces;
5457 vec<tree, va_gc> *classes;
5458 tree functions;
5459 hash_set<tree> *fn_set;
5462 static bool arg_assoc (struct arg_lookup*, tree);
5463 static bool arg_assoc_args (struct arg_lookup*, tree);
5464 static bool arg_assoc_args_vec (struct arg_lookup*, vec<tree, va_gc> *);
5465 static bool arg_assoc_type (struct arg_lookup*, tree);
5466 static bool add_function (struct arg_lookup *, tree);
5467 static bool arg_assoc_namespace (struct arg_lookup *, tree);
5468 static bool arg_assoc_class_only (struct arg_lookup *, tree);
5469 static bool arg_assoc_bases (struct arg_lookup *, tree);
5470 static bool arg_assoc_class (struct arg_lookup *, tree);
5471 static bool arg_assoc_template_arg (struct arg_lookup*, tree);
5473 /* Add a function to the lookup structure.
5474 Returns true on error. */
5476 static bool
5477 add_function (struct arg_lookup *k, tree fn)
5479 if (!is_overloaded_fn (fn))
5480 /* All names except those of (possibly overloaded) functions and
5481 function templates are ignored. */;
5482 else if (k->fn_set && k->fn_set->add (fn))
5483 /* It's already in the list. */;
5484 else if (!k->functions && TREE_CODE (fn) != TEMPLATE_DECL)
5485 k->functions = fn;
5486 else if (fn == k->functions)
5488 else
5490 k->functions = build_overload (fn, k->functions);
5491 if (TREE_CODE (k->functions) == OVERLOAD)
5492 OVL_ARG_DEPENDENT (k->functions) = true;
5495 return false;
5498 /* Returns true iff CURRENT has declared itself to be an associated
5499 namespace of SCOPE via a strong using-directive (or transitive chain
5500 thereof). Both are namespaces. */
5502 bool
5503 is_associated_namespace (tree current, tree scope)
5505 vec<tree, va_gc> *seen = make_tree_vector ();
5506 vec<tree, va_gc> *todo = make_tree_vector ();
5507 tree t;
5508 bool ret;
5510 while (1)
5512 if (scope == current)
5514 ret = true;
5515 break;
5517 vec_safe_push (seen, scope);
5518 for (t = DECL_NAMESPACE_ASSOCIATIONS (scope); t; t = TREE_CHAIN (t))
5519 if (!vec_member (TREE_PURPOSE (t), seen))
5520 vec_safe_push (todo, TREE_PURPOSE (t));
5521 if (!todo->is_empty ())
5523 scope = todo->last ();
5524 todo->pop ();
5526 else
5528 ret = false;
5529 break;
5533 release_tree_vector (seen);
5534 release_tree_vector (todo);
5536 return ret;
5539 /* Add functions of a namespace to the lookup structure.
5540 Returns true on error. */
5542 static bool
5543 arg_assoc_namespace (struct arg_lookup *k, tree scope)
5545 tree value;
5547 if (vec_member (scope, k->namespaces))
5548 return false;
5549 vec_safe_push (k->namespaces, scope);
5551 /* Check out our super-users. */
5552 for (value = DECL_NAMESPACE_ASSOCIATIONS (scope); value;
5553 value = TREE_CHAIN (value))
5554 if (arg_assoc_namespace (k, TREE_PURPOSE (value)))
5555 return true;
5557 /* Also look down into inline namespaces. */
5558 for (value = DECL_NAMESPACE_USING (scope); value;
5559 value = TREE_CHAIN (value))
5560 if (is_associated_namespace (scope, TREE_PURPOSE (value)))
5561 if (arg_assoc_namespace (k, TREE_PURPOSE (value)))
5562 return true;
5564 value = namespace_binding (k->name, scope);
5565 if (!value)
5566 return false;
5568 for (; value; value = OVL_NEXT (value))
5570 /* We don't want to find arbitrary hidden functions via argument
5571 dependent lookup. We only want to find friends of associated
5572 classes, which we'll do via arg_assoc_class. */
5573 if (hidden_name_p (OVL_CURRENT (value)))
5574 continue;
5576 if (add_function (k, OVL_CURRENT (value)))
5577 return true;
5580 return false;
5583 /* Adds everything associated with a template argument to the lookup
5584 structure. Returns true on error. */
5586 static bool
5587 arg_assoc_template_arg (struct arg_lookup *k, tree arg)
5589 /* [basic.lookup.koenig]
5591 If T is a template-id, its associated namespaces and classes are
5592 ... the namespaces and classes associated with the types of the
5593 template arguments provided for template type parameters
5594 (excluding template template parameters); the namespaces in which
5595 any template template arguments are defined; and the classes in
5596 which any member templates used as template template arguments
5597 are defined. [Note: non-type template arguments do not
5598 contribute to the set of associated namespaces. ] */
5600 /* Consider first template template arguments. */
5601 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
5602 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
5603 return false;
5604 else if (TREE_CODE (arg) == TEMPLATE_DECL)
5606 tree ctx = CP_DECL_CONTEXT (arg);
5608 /* It's not a member template. */
5609 if (TREE_CODE (ctx) == NAMESPACE_DECL)
5610 return arg_assoc_namespace (k, ctx);
5611 /* Otherwise, it must be member template. */
5612 else
5613 return arg_assoc_class_only (k, ctx);
5615 /* It's an argument pack; handle it recursively. */
5616 else if (ARGUMENT_PACK_P (arg))
5618 tree args = ARGUMENT_PACK_ARGS (arg);
5619 int i, len = TREE_VEC_LENGTH (args);
5620 for (i = 0; i < len; ++i)
5621 if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, i)))
5622 return true;
5624 return false;
5626 /* It's not a template template argument, but it is a type template
5627 argument. */
5628 else if (TYPE_P (arg))
5629 return arg_assoc_type (k, arg);
5630 /* It's a non-type template argument. */
5631 else
5632 return false;
5635 /* Adds the class and its friends to the lookup structure.
5636 Returns true on error. */
5638 static bool
5639 arg_assoc_class_only (struct arg_lookup *k, tree type)
5641 tree list, friends, context;
5643 /* Backend-built structures, such as __builtin_va_list, aren't
5644 affected by all this. */
5645 if (!CLASS_TYPE_P (type))
5646 return false;
5648 context = decl_namespace_context (type);
5649 if (arg_assoc_namespace (k, context))
5650 return true;
5652 complete_type (type);
5654 /* Process friends. */
5655 for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
5656 list = TREE_CHAIN (list))
5657 if (k->name == FRIEND_NAME (list))
5658 for (friends = FRIEND_DECLS (list); friends;
5659 friends = TREE_CHAIN (friends))
5661 tree fn = TREE_VALUE (friends);
5663 /* Only interested in global functions with potentially hidden
5664 (i.e. unqualified) declarations. */
5665 if (CP_DECL_CONTEXT (fn) != context)
5666 continue;
5667 /* Template specializations are never found by name lookup.
5668 (Templates themselves can be found, but not template
5669 specializations.) */
5670 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn))
5671 continue;
5672 if (add_function (k, fn))
5673 return true;
5676 return false;
5679 /* Adds the class and its bases to the lookup structure.
5680 Returns true on error. */
5682 static bool
5683 arg_assoc_bases (struct arg_lookup *k, tree type)
5685 if (arg_assoc_class_only (k, type))
5686 return true;
5688 if (TYPE_BINFO (type))
5690 /* Process baseclasses. */
5691 tree binfo, base_binfo;
5692 int i;
5694 for (binfo = TYPE_BINFO (type), i = 0;
5695 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5696 if (arg_assoc_bases (k, BINFO_TYPE (base_binfo)))
5697 return true;
5700 return false;
5703 /* Adds everything associated with a class argument type to the lookup
5704 structure. Returns true on error.
5706 If T is a class type (including unions), its associated classes are: the
5707 class itself; the class of which it is a member, if any; and its direct
5708 and indirect base classes. Its associated namespaces are the namespaces
5709 of which its associated classes are members. Furthermore, if T is a
5710 class template specialization, its associated namespaces and classes
5711 also include: the namespaces and classes associated with the types of
5712 the template arguments provided for template type parameters (excluding
5713 template template parameters); the namespaces of which any template
5714 template arguments are members; and the classes of which any member
5715 templates used as template template arguments are members. [ Note:
5716 non-type template arguments do not contribute to the set of associated
5717 namespaces. --end note] */
5719 static bool
5720 arg_assoc_class (struct arg_lookup *k, tree type)
5722 tree list;
5723 int i;
5725 /* Backend build structures, such as __builtin_va_list, aren't
5726 affected by all this. */
5727 if (!CLASS_TYPE_P (type))
5728 return false;
5730 if (vec_member (type, k->classes))
5731 return false;
5732 vec_safe_push (k->classes, type);
5734 if (TYPE_CLASS_SCOPE_P (type)
5735 && arg_assoc_class_only (k, TYPE_CONTEXT (type)))
5736 return true;
5738 if (arg_assoc_bases (k, type))
5739 return true;
5741 /* Process template arguments. */
5742 if (CLASSTYPE_TEMPLATE_INFO (type)
5743 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
5745 list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
5746 for (i = 0; i < TREE_VEC_LENGTH (list); ++i)
5747 if (arg_assoc_template_arg (k, TREE_VEC_ELT (list, i)))
5748 return true;
5751 return false;
5754 /* Adds everything associated with a given type.
5755 Returns 1 on error. */
5757 static bool
5758 arg_assoc_type (struct arg_lookup *k, tree type)
5760 /* As we do not get the type of non-type dependent expressions
5761 right, we can end up with such things without a type. */
5762 if (!type)
5763 return false;
5765 if (TYPE_PTRDATAMEM_P (type))
5767 /* Pointer to member: associate class type and value type. */
5768 if (arg_assoc_type (k, TYPE_PTRMEM_CLASS_TYPE (type)))
5769 return true;
5770 return arg_assoc_type (k, TYPE_PTRMEM_POINTED_TO_TYPE (type));
5772 else switch (TREE_CODE (type))
5774 case ERROR_MARK:
5775 return false;
5776 case VOID_TYPE:
5777 case INTEGER_TYPE:
5778 case REAL_TYPE:
5779 case COMPLEX_TYPE:
5780 case VECTOR_TYPE:
5781 case BOOLEAN_TYPE:
5782 case FIXED_POINT_TYPE:
5783 case DECLTYPE_TYPE:
5784 case NULLPTR_TYPE:
5785 return false;
5786 case RECORD_TYPE:
5787 if (TYPE_PTRMEMFUNC_P (type))
5788 return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type));
5789 /* FALLTHRU */
5790 case UNION_TYPE:
5791 return arg_assoc_class (k, type);
5792 case POINTER_TYPE:
5793 case REFERENCE_TYPE:
5794 case ARRAY_TYPE:
5795 return arg_assoc_type (k, TREE_TYPE (type));
5796 case ENUMERAL_TYPE:
5797 if (TYPE_CLASS_SCOPE_P (type)
5798 && arg_assoc_class_only (k, TYPE_CONTEXT (type)))
5799 return true;
5800 return arg_assoc_namespace (k, decl_namespace_context (type));
5801 case METHOD_TYPE:
5802 /* The basetype is referenced in the first arg type, so just
5803 fall through. */
5804 case FUNCTION_TYPE:
5805 /* Associate the parameter types. */
5806 if (arg_assoc_args (k, TYPE_ARG_TYPES (type)))
5807 return true;
5808 /* Associate the return type. */
5809 return arg_assoc_type (k, TREE_TYPE (type));
5810 case TEMPLATE_TYPE_PARM:
5811 case BOUND_TEMPLATE_TEMPLATE_PARM:
5812 return false;
5813 case TYPENAME_TYPE:
5814 return false;
5815 case LANG_TYPE:
5816 gcc_assert (type == unknown_type_node
5817 || type == init_list_type_node);
5818 return false;
5819 case TYPE_PACK_EXPANSION:
5820 return arg_assoc_type (k, PACK_EXPANSION_PATTERN (type));
5822 default:
5823 gcc_unreachable ();
5825 return false;
5828 /* Adds everything associated with arguments. Returns true on error. */
5830 static bool
5831 arg_assoc_args (struct arg_lookup *k, tree args)
5833 for (; args; args = TREE_CHAIN (args))
5834 if (arg_assoc (k, TREE_VALUE (args)))
5835 return true;
5836 return false;
5839 /* Adds everything associated with an argument vector. Returns true
5840 on error. */
5842 static bool
5843 arg_assoc_args_vec (struct arg_lookup *k, vec<tree, va_gc> *args)
5845 unsigned int ix;
5846 tree arg;
5848 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
5849 if (arg_assoc (k, arg))
5850 return true;
5851 return false;
5854 /* Adds everything associated with a given tree_node. Returns 1 on error. */
5856 static bool
5857 arg_assoc (struct arg_lookup *k, tree n)
5859 if (n == error_mark_node)
5860 return false;
5862 if (TYPE_P (n))
5863 return arg_assoc_type (k, n);
5865 if (! type_unknown_p (n))
5866 return arg_assoc_type (k, TREE_TYPE (n));
5868 if (TREE_CODE (n) == ADDR_EXPR)
5869 n = TREE_OPERAND (n, 0);
5870 if (TREE_CODE (n) == COMPONENT_REF)
5871 n = TREE_OPERAND (n, 1);
5872 if (TREE_CODE (n) == OFFSET_REF)
5873 n = TREE_OPERAND (n, 1);
5874 while (TREE_CODE (n) == TREE_LIST)
5875 n = TREE_VALUE (n);
5876 if (BASELINK_P (n))
5877 n = BASELINK_FUNCTIONS (n);
5879 if (TREE_CODE (n) == FUNCTION_DECL)
5880 return arg_assoc_type (k, TREE_TYPE (n));
5881 if (TREE_CODE (n) == TEMPLATE_ID_EXPR)
5883 /* The working paper doesn't currently say how to handle template-id
5884 arguments. The sensible thing would seem to be to handle the list
5885 of template candidates like a normal overload set, and handle the
5886 template arguments like we do for class template
5887 specializations. */
5888 tree templ = TREE_OPERAND (n, 0);
5889 tree args = TREE_OPERAND (n, 1);
5890 int ix;
5892 /* First the templates. */
5893 if (arg_assoc (k, templ))
5894 return true;
5896 /* Now the arguments. */
5897 if (args)
5898 for (ix = TREE_VEC_LENGTH (args); ix--;)
5899 if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, ix)) == 1)
5900 return true;
5902 else if (TREE_CODE (n) == OVERLOAD)
5904 for (; n; n = OVL_NEXT (n))
5905 if (arg_assoc_type (k, TREE_TYPE (OVL_CURRENT (n))))
5906 return true;
5909 return false;
5912 /* Performs Koenig lookup depending on arguments, where fns
5913 are the functions found in normal lookup. */
5915 static cp_expr
5916 lookup_arg_dependent_1 (tree name, tree fns, vec<tree, va_gc> *args)
5918 struct arg_lookup k;
5920 /* Remove any hidden friend functions from the list of functions
5921 found so far. They will be added back by arg_assoc_class as
5922 appropriate. */
5923 fns = remove_hidden_names (fns);
5925 k.name = name;
5926 k.args = args;
5927 k.functions = fns;
5928 k.classes = make_tree_vector ();
5930 /* We previously performed an optimization here by setting
5931 NAMESPACES to the current namespace when it was safe. However, DR
5932 164 says that namespaces that were already searched in the first
5933 stage of template processing are searched again (potentially
5934 picking up later definitions) in the second stage. */
5935 k.namespaces = make_tree_vector ();
5937 /* We used to allow duplicates and let joust discard them, but
5938 since the above change for DR 164 we end up with duplicates of
5939 all the functions found by unqualified lookup. So keep track
5940 of which ones we've seen. */
5941 if (fns)
5943 tree ovl;
5944 /* We shouldn't be here if lookup found something other than
5945 namespace-scope functions. */
5946 gcc_assert (DECL_NAMESPACE_SCOPE_P (OVL_CURRENT (fns)));
5947 k.fn_set = new hash_set<tree>;
5948 for (ovl = fns; ovl; ovl = OVL_NEXT (ovl))
5949 k.fn_set->add (OVL_CURRENT (ovl));
5951 else
5952 k.fn_set = NULL;
5954 arg_assoc_args_vec (&k, args);
5956 fns = k.functions;
5958 if (fns
5959 && !VAR_P (fns)
5960 && !is_overloaded_fn (fns))
5962 error ("argument dependent lookup finds %q+D", fns);
5963 error (" in call to %qD", name);
5964 fns = error_mark_node;
5967 release_tree_vector (k.classes);
5968 release_tree_vector (k.namespaces);
5969 delete k.fn_set;
5971 return fns;
5974 /* Wrapper for lookup_arg_dependent_1. */
5976 cp_expr
5977 lookup_arg_dependent (tree name, tree fns, vec<tree, va_gc> *args)
5979 cp_expr ret;
5980 bool subtime;
5981 subtime = timevar_cond_start (TV_NAME_LOOKUP);
5982 ret = lookup_arg_dependent_1 (name, fns, args);
5983 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5984 return ret;
5988 /* Add namespace to using_directives. Return NULL_TREE if nothing was
5989 changed (i.e. there was already a directive), or the fresh
5990 TREE_LIST otherwise. */
5992 static tree
5993 push_using_directive_1 (tree used)
5995 tree ud = current_binding_level->using_directives;
5996 tree iter, ancestor;
5998 /* Check if we already have this. */
5999 if (purpose_member (used, ud) != NULL_TREE)
6000 return NULL_TREE;
6002 ancestor = namespace_ancestor (current_decl_namespace (), used);
6003 ud = current_binding_level->using_directives;
6004 ud = tree_cons (used, ancestor, ud);
6005 current_binding_level->using_directives = ud;
6007 /* Recursively add all namespaces used. */
6008 for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
6009 push_using_directive (TREE_PURPOSE (iter));
6011 return ud;
6014 /* Wrapper for push_using_directive_1. */
6016 static tree
6017 push_using_directive (tree used)
6019 tree ret;
6020 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6021 ret = push_using_directive_1 (used);
6022 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6023 return ret;
6026 /* The type TYPE is being declared. If it is a class template, or a
6027 specialization of a class template, do any processing required and
6028 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
6029 being declared a friend. B is the binding level at which this TYPE
6030 should be bound.
6032 Returns the TYPE_DECL for TYPE, which may have been altered by this
6033 processing. */
6035 static tree
6036 maybe_process_template_type_declaration (tree type, int is_friend,
6037 cp_binding_level *b)
6039 tree decl = TYPE_NAME (type);
6041 if (processing_template_parmlist)
6042 /* You can't declare a new template type in a template parameter
6043 list. But, you can declare a non-template type:
6045 template <class A*> struct S;
6047 is a forward-declaration of `A'. */
6049 else if (b->kind == sk_namespace
6050 && current_binding_level->kind != sk_namespace)
6051 /* If this new type is being injected into a containing scope,
6052 then it's not a template type. */
6054 else
6056 gcc_assert (MAYBE_CLASS_TYPE_P (type)
6057 || TREE_CODE (type) == ENUMERAL_TYPE);
6059 if (processing_template_decl)
6061 /* This may change after the call to
6062 push_template_decl_real, but we want the original value. */
6063 tree name = DECL_NAME (decl);
6065 decl = push_template_decl_real (decl, is_friend);
6066 if (decl == error_mark_node)
6067 return error_mark_node;
6069 /* If the current binding level is the binding level for the
6070 template parameters (see the comment in
6071 begin_template_parm_list) and the enclosing level is a class
6072 scope, and we're not looking at a friend, push the
6073 declaration of the member class into the class scope. In the
6074 friend case, push_template_decl will already have put the
6075 friend into global scope, if appropriate. */
6076 if (TREE_CODE (type) != ENUMERAL_TYPE
6077 && !is_friend && b->kind == sk_template_parms
6078 && b->level_chain->kind == sk_class)
6080 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
6082 if (!COMPLETE_TYPE_P (current_class_type))
6084 maybe_add_class_template_decl_list (current_class_type,
6085 type, /*friend_p=*/0);
6086 /* Put this UTD in the table of UTDs for the class. */
6087 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
6088 CLASSTYPE_NESTED_UTDS (current_class_type) =
6089 binding_table_new (SCOPE_DEFAULT_HT_SIZE);
6091 binding_table_insert
6092 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
6098 return decl;
6101 /* Push a tag name NAME for struct/class/union/enum type TYPE. In case
6102 that the NAME is a class template, the tag is processed but not pushed.
6104 The pushed scope depend on the SCOPE parameter:
6105 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
6106 scope.
6107 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
6108 non-template-parameter scope. This case is needed for forward
6109 declarations.
6110 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
6111 TS_GLOBAL case except that names within template-parameter scopes
6112 are not pushed at all.
6114 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
6116 static tree
6117 pushtag_1 (tree name, tree type, tag_scope scope)
6119 cp_binding_level *b;
6120 tree decl;
6122 b = current_binding_level;
6123 while (/* Cleanup scopes are not scopes from the point of view of
6124 the language. */
6125 b->kind == sk_cleanup
6126 /* Neither are function parameter scopes. */
6127 || b->kind == sk_function_parms
6128 /* Neither are the scopes used to hold template parameters
6129 for an explicit specialization. For an ordinary template
6130 declaration, these scopes are not scopes from the point of
6131 view of the language. */
6132 || (b->kind == sk_template_parms
6133 && (b->explicit_spec_p || scope == ts_global))
6134 || (b->kind == sk_class
6135 && (scope != ts_current
6136 /* We may be defining a new type in the initializer
6137 of a static member variable. We allow this when
6138 not pedantic, and it is particularly useful for
6139 type punning via an anonymous union. */
6140 || COMPLETE_TYPE_P (b->this_entity))))
6141 b = b->level_chain;
6143 gcc_assert (identifier_p (name));
6145 /* Do C++ gratuitous typedefing. */
6146 if (identifier_type_value_1 (name) != type)
6148 tree tdef;
6149 int in_class = 0;
6150 tree context = TYPE_CONTEXT (type);
6152 if (! context)
6154 tree cs = current_scope ();
6156 if (scope == ts_current
6157 || (cs && TREE_CODE (cs) == FUNCTION_DECL))
6158 context = cs;
6159 else if (cs != NULL_TREE && TYPE_P (cs))
6160 /* When declaring a friend class of a local class, we want
6161 to inject the newly named class into the scope
6162 containing the local class, not the namespace
6163 scope. */
6164 context = decl_function_context (get_type_decl (cs));
6166 if (!context)
6167 context = current_namespace;
6169 if (b->kind == sk_class
6170 || (b->kind == sk_template_parms
6171 && b->level_chain->kind == sk_class))
6172 in_class = 1;
6174 tdef = create_implicit_typedef (name, type);
6175 DECL_CONTEXT (tdef) = FROB_CONTEXT (context);
6176 if (scope == ts_within_enclosing_non_class)
6178 /* This is a friend. Make this TYPE_DECL node hidden from
6179 ordinary name lookup. Its corresponding TEMPLATE_DECL
6180 will be marked in push_template_decl_real. */
6181 retrofit_lang_decl (tdef);
6182 DECL_ANTICIPATED (tdef) = 1;
6183 DECL_FRIEND_P (tdef) = 1;
6186 decl = maybe_process_template_type_declaration
6187 (type, scope == ts_within_enclosing_non_class, b);
6188 if (decl == error_mark_node)
6189 return decl;
6191 if (b->kind == sk_class)
6193 if (!TYPE_BEING_DEFINED (current_class_type))
6194 return error_mark_node;
6196 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
6197 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
6198 class. But if it's a member template class, we want
6199 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
6200 later. */
6201 finish_member_declaration (decl);
6202 else
6203 pushdecl_class_level (decl);
6205 else if (b->kind != sk_template_parms)
6207 decl = pushdecl_with_scope_1 (decl, b, /*is_friend=*/false);
6208 if (decl == error_mark_node)
6209 return decl;
6212 if (! in_class)
6213 set_identifier_type_value_with_scope (name, tdef, b);
6215 TYPE_CONTEXT (type) = DECL_CONTEXT (decl);
6217 /* If this is a local class, keep track of it. We need this
6218 information for name-mangling, and so that it is possible to
6219 find all function definitions in a translation unit in a
6220 convenient way. (It's otherwise tricky to find a member
6221 function definition it's only pointed to from within a local
6222 class.) */
6223 if (TYPE_FUNCTION_SCOPE_P (type))
6225 if (processing_template_decl)
6227 /* Push a DECL_EXPR so we call pushtag at the right time in
6228 template instantiation rather than in some nested context. */
6229 add_decl_expr (decl);
6231 else
6232 vec_safe_push (local_classes, type);
6235 if (b->kind == sk_class
6236 && !COMPLETE_TYPE_P (current_class_type))
6238 maybe_add_class_template_decl_list (current_class_type,
6239 type, /*friend_p=*/0);
6241 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
6242 CLASSTYPE_NESTED_UTDS (current_class_type)
6243 = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
6245 binding_table_insert
6246 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
6249 decl = TYPE_NAME (type);
6250 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
6252 /* Set type visibility now if this is a forward declaration. */
6253 TREE_PUBLIC (decl) = 1;
6254 determine_visibility (decl);
6256 return type;
6259 /* Wrapper for pushtag_1. */
6261 tree
6262 pushtag (tree name, tree type, tag_scope scope)
6264 tree ret;
6265 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6266 ret = pushtag_1 (name, type, scope);
6267 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6268 return ret;
6272 /* Subroutines for reverting temporarily to top-level for instantiation
6273 of templates and such. We actually need to clear out the class- and
6274 local-value slots of all identifiers, so that only the global values
6275 are at all visible. Simply setting current_binding_level to the global
6276 scope isn't enough, because more binding levels may be pushed. */
6277 struct saved_scope *scope_chain;
6279 /* Return true if ID has not already been marked. */
6281 static inline bool
6282 store_binding_p (tree id)
6284 if (!id || !IDENTIFIER_BINDING (id))
6285 return false;
6287 if (IDENTIFIER_MARKED (id))
6288 return false;
6290 return true;
6293 /* Add an appropriate binding to *OLD_BINDINGS which needs to already
6294 have enough space reserved. */
6296 static void
6297 store_binding (tree id, vec<cxx_saved_binding, va_gc> **old_bindings)
6299 cxx_saved_binding saved;
6301 gcc_checking_assert (store_binding_p (id));
6303 IDENTIFIER_MARKED (id) = 1;
6305 saved.identifier = id;
6306 saved.binding = IDENTIFIER_BINDING (id);
6307 saved.real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
6308 (*old_bindings)->quick_push (saved);
6309 IDENTIFIER_BINDING (id) = NULL;
6312 static void
6313 store_bindings (tree names, vec<cxx_saved_binding, va_gc> **old_bindings)
6315 static vec<tree> bindings_need_stored;
6316 tree t, id;
6317 size_t i;
6319 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6320 for (t = names; t; t = TREE_CHAIN (t))
6322 if (TREE_CODE (t) == TREE_LIST)
6323 id = TREE_PURPOSE (t);
6324 else
6325 id = DECL_NAME (t);
6327 if (store_binding_p (id))
6328 bindings_need_stored.safe_push (id);
6330 if (!bindings_need_stored.is_empty ())
6332 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
6333 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
6335 /* We can appearantly have duplicates in NAMES. */
6336 if (store_binding_p (id))
6337 store_binding (id, old_bindings);
6339 bindings_need_stored.truncate (0);
6341 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6344 /* Like store_bindings, but NAMES is a vector of cp_class_binding
6345 objects, rather than a TREE_LIST. */
6347 static void
6348 store_class_bindings (vec<cp_class_binding, va_gc> *names,
6349 vec<cxx_saved_binding, va_gc> **old_bindings)
6351 static vec<tree> bindings_need_stored;
6352 size_t i;
6353 cp_class_binding *cb;
6355 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6356 for (i = 0; vec_safe_iterate (names, i, &cb); ++i)
6357 if (store_binding_p (cb->identifier))
6358 bindings_need_stored.safe_push (cb->identifier);
6359 if (!bindings_need_stored.is_empty ())
6361 tree id;
6362 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
6363 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
6364 store_binding (id, old_bindings);
6365 bindings_need_stored.truncate (0);
6367 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6370 /* A chain of saved_scope structures awaiting reuse. */
6372 static GTY((deletable)) struct saved_scope *free_saved_scope;
6374 void
6375 push_to_top_level (void)
6377 struct saved_scope *s;
6378 cp_binding_level *b;
6379 cxx_saved_binding *sb;
6380 size_t i;
6381 bool need_pop;
6383 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6385 /* Reuse or create a new structure for this saved scope. */
6386 if (free_saved_scope != NULL)
6388 s = free_saved_scope;
6389 free_saved_scope = s->prev;
6391 vec<cxx_saved_binding, va_gc> *old_bindings = s->old_bindings;
6392 memset (s, 0, sizeof (*s));
6393 /* Also reuse the structure's old_bindings vector. */
6394 vec_safe_truncate (old_bindings, 0);
6395 s->old_bindings = old_bindings;
6397 else
6398 s = ggc_cleared_alloc<saved_scope> ();
6400 b = scope_chain ? current_binding_level : 0;
6402 /* If we're in the middle of some function, save our state. */
6403 if (cfun)
6405 need_pop = true;
6406 push_function_context ();
6408 else
6409 need_pop = false;
6411 if (scope_chain && previous_class_level)
6412 store_class_bindings (previous_class_level->class_shadowed,
6413 &s->old_bindings);
6415 /* Have to include the global scope, because class-scope decls
6416 aren't listed anywhere useful. */
6417 for (; b; b = b->level_chain)
6419 tree t;
6421 /* Template IDs are inserted into the global level. If they were
6422 inserted into namespace level, finish_file wouldn't find them
6423 when doing pending instantiations. Therefore, don't stop at
6424 namespace level, but continue until :: . */
6425 if (global_scope_p (b))
6426 break;
6428 store_bindings (b->names, &s->old_bindings);
6429 /* We also need to check class_shadowed to save class-level type
6430 bindings, since pushclass doesn't fill in b->names. */
6431 if (b->kind == sk_class)
6432 store_class_bindings (b->class_shadowed, &s->old_bindings);
6434 /* Unwind type-value slots back to top level. */
6435 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
6436 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
6439 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, sb)
6440 IDENTIFIER_MARKED (sb->identifier) = 0;
6442 s->prev = scope_chain;
6443 s->bindings = b;
6444 s->need_pop_function_context = need_pop;
6445 s->function_decl = current_function_decl;
6446 s->unevaluated_operand = cp_unevaluated_operand;
6447 s->inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
6448 s->x_stmt_tree.stmts_are_full_exprs_p = true;
6450 scope_chain = s;
6451 current_function_decl = NULL_TREE;
6452 vec_alloc (current_lang_base, 10);
6453 current_lang_name = lang_name_cplusplus;
6454 current_namespace = global_namespace;
6455 push_class_stack ();
6456 cp_unevaluated_operand = 0;
6457 c_inhibit_evaluation_warnings = 0;
6458 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6461 static void
6462 pop_from_top_level_1 (void)
6464 struct saved_scope *s = scope_chain;
6465 cxx_saved_binding *saved;
6466 size_t i;
6468 /* Clear out class-level bindings cache. */
6469 if (previous_class_level)
6470 invalidate_class_lookup_cache ();
6471 pop_class_stack ();
6473 current_lang_base = 0;
6475 scope_chain = s->prev;
6476 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, saved)
6478 tree id = saved->identifier;
6480 IDENTIFIER_BINDING (id) = saved->binding;
6481 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
6484 /* If we were in the middle of compiling a function, restore our
6485 state. */
6486 if (s->need_pop_function_context)
6487 pop_function_context ();
6488 current_function_decl = s->function_decl;
6489 cp_unevaluated_operand = s->unevaluated_operand;
6490 c_inhibit_evaluation_warnings = s->inhibit_evaluation_warnings;
6492 /* Make this saved_scope structure available for reuse by
6493 push_to_top_level. */
6494 s->prev = free_saved_scope;
6495 free_saved_scope = s;
6498 /* Wrapper for pop_from_top_level_1. */
6500 void
6501 pop_from_top_level (void)
6503 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6504 pop_from_top_level_1 ();
6505 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6509 /* Pop off extraneous binding levels left over due to syntax errors.
6511 We don't pop past namespaces, as they might be valid. */
6513 void
6514 pop_everything (void)
6516 if (ENABLE_SCOPE_CHECKING)
6517 verbatim ("XXX entering pop_everything ()\n");
6518 while (!toplevel_bindings_p ())
6520 if (current_binding_level->kind == sk_class)
6521 pop_nested_class ();
6522 else
6523 poplevel (0, 0, 0);
6525 if (ENABLE_SCOPE_CHECKING)
6526 verbatim ("XXX leaving pop_everything ()\n");
6529 /* Emit debugging information for using declarations and directives.
6530 If input tree is overloaded fn then emit debug info for all
6531 candidates. */
6533 void
6534 cp_emit_debug_info_for_using (tree t, tree context)
6536 /* Don't try to emit any debug information if we have errors. */
6537 if (seen_error ())
6538 return;
6540 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
6541 of a builtin function. */
6542 if (TREE_CODE (t) == FUNCTION_DECL
6543 && DECL_EXTERNAL (t)
6544 && DECL_BUILT_IN (t))
6545 return;
6547 /* Do not supply context to imported_module_or_decl, if
6548 it is a global namespace. */
6549 if (context == global_namespace)
6550 context = NULL_TREE;
6552 if (BASELINK_P (t))
6553 t = BASELINK_FUNCTIONS (t);
6555 /* FIXME: Handle TEMPLATE_DECLs. */
6556 for (t = OVL_CURRENT (t); t; t = OVL_NEXT (t))
6557 if (TREE_CODE (t) != TEMPLATE_DECL)
6559 if (building_stmt_list_p ())
6560 add_stmt (build_stmt (input_location, USING_STMT, t));
6561 else
6562 (*debug_hooks->imported_module_or_decl) (t, NULL_TREE, context, false);
6566 #include "gt-cp-name-lookup.h"