* name-lookup.c (push_overloaded_decl_1): Refactor OVERLOAD creation.
[official-gcc.git] / gcc / cp / name-lookup.c
blobba900cbf01fceb55c23070b3c76109d1ab46eb37
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 tree lookup_type_current_level (tree);
52 static tree push_using_directive (tree);
53 static tree lookup_extern_c_fun_in_all_ns (tree);
54 static void diagnose_name_conflict (tree, tree);
56 /* The :: namespace. */
58 tree global_namespace;
60 /* The name of the anonymous namespace, throughout this translation
61 unit. */
62 static GTY(()) tree anonymous_namespace_name;
64 /* Initialize anonymous_namespace_name if necessary, and return it. */
66 static tree
67 get_anonymous_namespace_name (void)
69 if (!anonymous_namespace_name)
71 /* We used to use get_file_function_name here, but that isn't
72 necessary now that anonymous namespace typeinfos
73 are !TREE_PUBLIC, and thus compared by address. */
74 /* The demangler expects anonymous namespaces to be called
75 something starting with '_GLOBAL__N_'. */
76 anonymous_namespace_name = get_identifier ("_GLOBAL__N_1");
78 return anonymous_namespace_name;
81 /* Compute the chain index of a binding_entry given the HASH value of its
82 name and the total COUNT of chains. COUNT is assumed to be a power
83 of 2. */
85 #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
87 /* A free list of "binding_entry"s awaiting for re-use. */
89 static GTY((deletable)) binding_entry free_binding_entry = NULL;
91 /* Create a binding_entry object for (NAME, TYPE). */
93 static inline binding_entry
94 binding_entry_make (tree name, tree type)
96 binding_entry entry;
98 if (free_binding_entry)
100 entry = free_binding_entry;
101 free_binding_entry = entry->chain;
103 else
104 entry = ggc_alloc<binding_entry_s> ();
106 entry->name = name;
107 entry->type = type;
108 entry->chain = NULL;
110 return entry;
113 /* Put ENTRY back on the free list. */
114 #if 0
115 static inline void
116 binding_entry_free (binding_entry entry)
118 entry->name = NULL;
119 entry->type = NULL;
120 entry->chain = free_binding_entry;
121 free_binding_entry = entry;
123 #endif
125 /* The datatype used to implement the mapping from names to types at
126 a given scope. */
127 struct GTY(()) binding_table_s {
128 /* Array of chains of "binding_entry"s */
129 binding_entry * GTY((length ("%h.chain_count"))) chain;
131 /* The number of chains in this table. This is the length of the
132 member "chain" considered as an array. */
133 size_t chain_count;
135 /* Number of "binding_entry"s in this table. */
136 size_t entry_count;
139 /* Construct TABLE with an initial CHAIN_COUNT. */
141 static inline void
142 binding_table_construct (binding_table table, size_t chain_count)
144 table->chain_count = chain_count;
145 table->entry_count = 0;
146 table->chain = ggc_cleared_vec_alloc<binding_entry> (table->chain_count);
149 /* Make TABLE's entries ready for reuse. */
150 #if 0
151 static void
152 binding_table_free (binding_table table)
154 size_t i;
155 size_t count;
157 if (table == NULL)
158 return;
160 for (i = 0, count = table->chain_count; i < count; ++i)
162 binding_entry temp = table->chain[i];
163 while (temp != NULL)
165 binding_entry entry = temp;
166 temp = entry->chain;
167 binding_entry_free (entry);
169 table->chain[i] = NULL;
171 table->entry_count = 0;
173 #endif
175 /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */
177 static inline binding_table
178 binding_table_new (size_t chain_count)
180 binding_table table = ggc_alloc<binding_table_s> ();
181 table->chain = NULL;
182 binding_table_construct (table, chain_count);
183 return table;
186 /* Expand TABLE to twice its current chain_count. */
188 static void
189 binding_table_expand (binding_table table)
191 const size_t old_chain_count = table->chain_count;
192 const size_t old_entry_count = table->entry_count;
193 const size_t new_chain_count = 2 * old_chain_count;
194 binding_entry *old_chains = table->chain;
195 size_t i;
197 binding_table_construct (table, new_chain_count);
198 for (i = 0; i < old_chain_count; ++i)
200 binding_entry entry = old_chains[i];
201 for (; entry != NULL; entry = old_chains[i])
203 const unsigned int hash = IDENTIFIER_HASH_VALUE (entry->name);
204 const size_t j = ENTRY_INDEX (hash, new_chain_count);
206 old_chains[i] = entry->chain;
207 entry->chain = table->chain[j];
208 table->chain[j] = entry;
211 table->entry_count = old_entry_count;
214 /* Insert a binding for NAME to TYPE into TABLE. */
216 static void
217 binding_table_insert (binding_table table, tree name, tree type)
219 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
220 const size_t i = ENTRY_INDEX (hash, table->chain_count);
221 binding_entry entry = binding_entry_make (name, type);
223 entry->chain = table->chain[i];
224 table->chain[i] = entry;
225 ++table->entry_count;
227 if (3 * table->chain_count < 5 * table->entry_count)
228 binding_table_expand (table);
231 /* Return the binding_entry, if any, that maps NAME. */
233 binding_entry
234 binding_table_find (binding_table table, tree name)
236 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
237 binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)];
239 while (entry != NULL && entry->name != name)
240 entry = entry->chain;
242 return entry;
245 /* Apply PROC -- with DATA -- to all entries in TABLE. */
247 void
248 binding_table_foreach (binding_table table, bt_foreach_proc proc, void *data)
250 size_t chain_count;
251 size_t i;
253 if (!table)
254 return;
256 chain_count = table->chain_count;
257 for (i = 0; i < chain_count; ++i)
259 binding_entry entry = table->chain[i];
260 for (; entry != NULL; entry = entry->chain)
261 proc (entry, data);
265 #ifndef ENABLE_SCOPE_CHECKING
266 # define ENABLE_SCOPE_CHECKING 0
267 #else
268 # define ENABLE_SCOPE_CHECKING 1
269 #endif
271 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
273 static GTY((deletable)) cxx_binding *free_bindings;
275 /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
276 field to NULL. */
278 static inline void
279 cxx_binding_init (cxx_binding *binding, tree value, tree type)
281 binding->value = value;
282 binding->type = type;
283 binding->previous = NULL;
286 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
288 static cxx_binding *
289 cxx_binding_make (tree value, tree type)
291 cxx_binding *binding;
292 if (free_bindings)
294 binding = free_bindings;
295 free_bindings = binding->previous;
297 else
298 binding = ggc_alloc<cxx_binding> ();
300 cxx_binding_init (binding, value, type);
302 return binding;
305 /* Put BINDING back on the free list. */
307 static inline void
308 cxx_binding_free (cxx_binding *binding)
310 binding->scope = NULL;
311 binding->previous = free_bindings;
312 free_bindings = binding;
315 /* Create a new binding for NAME (with the indicated VALUE and TYPE
316 bindings) in the class scope indicated by SCOPE. */
318 static cxx_binding *
319 new_class_binding (tree name, tree value, tree type, cp_binding_level *scope)
321 cp_class_binding cb = {cxx_binding_make (value, type), name};
322 cxx_binding *binding = cb.base;
323 vec_safe_push (scope->class_shadowed, cb);
324 binding->scope = scope;
325 return binding;
328 /* Make DECL the innermost binding for ID. The LEVEL is the binding
329 level at which this declaration is being bound. */
331 void
332 push_binding (tree id, tree decl, cp_binding_level* level)
334 cxx_binding *binding;
336 if (level != class_binding_level)
338 binding = cxx_binding_make (decl, NULL_TREE);
339 binding->scope = level;
341 else
342 binding = new_class_binding (id, decl, /*type=*/NULL_TREE, level);
344 /* Now, fill in the binding information. */
345 binding->previous = IDENTIFIER_BINDING (id);
346 INHERITED_VALUE_BINDING_P (binding) = 0;
347 LOCAL_BINDING_P (binding) = (level != class_binding_level);
349 /* And put it on the front of the list of bindings for ID. */
350 IDENTIFIER_BINDING (id) = binding;
353 /* Remove the binding for DECL which should be the innermost binding
354 for ID. */
356 void
357 pop_binding (tree id, tree decl)
359 cxx_binding *binding;
361 if (id == NULL_TREE)
362 /* It's easiest to write the loops that call this function without
363 checking whether or not the entities involved have names. We
364 get here for such an entity. */
365 return;
367 /* Get the innermost binding for ID. */
368 binding = IDENTIFIER_BINDING (id);
370 /* The name should be bound. */
371 gcc_assert (binding != NULL);
373 /* The DECL will be either the ordinary binding or the type
374 binding for this identifier. Remove that binding. */
375 if (binding->value == decl)
376 binding->value = NULL_TREE;
377 else
379 gcc_assert (binding->type == decl);
380 binding->type = NULL_TREE;
383 if (!binding->value && !binding->type)
385 /* We're completely done with the innermost binding for this
386 identifier. Unhook it from the list of bindings. */
387 IDENTIFIER_BINDING (id) = binding->previous;
389 /* Add it to the free list. */
390 cxx_binding_free (binding);
394 /* Remove the bindings for the decls of the current level and leave
395 the current scope. */
397 void
398 pop_bindings_and_leave_scope (void)
400 for (tree t = getdecls (); t; t = DECL_CHAIN (t))
401 pop_binding (DECL_NAME (t), t);
402 leave_scope ();
405 /* Strip non dependent using declarations. If DECL is dependent,
406 surreptitiously create a typename_type and return it. */
408 tree
409 strip_using_decl (tree decl)
411 if (decl == NULL_TREE)
412 return NULL_TREE;
414 while (TREE_CODE (decl) == USING_DECL && !DECL_DEPENDENT_P (decl))
415 decl = USING_DECL_DECLS (decl);
417 if (TREE_CODE (decl) == USING_DECL && DECL_DEPENDENT_P (decl)
418 && USING_DECL_TYPENAME_P (decl))
420 /* We have found a type introduced by a using
421 declaration at class scope that refers to a dependent
422 type.
424 using typename :: [opt] nested-name-specifier unqualified-id ;
426 decl = make_typename_type (TREE_TYPE (decl),
427 DECL_NAME (decl),
428 typename_type, tf_error);
429 if (decl != error_mark_node)
430 decl = TYPE_NAME (decl);
433 return decl;
436 /* BINDING records an existing declaration for a name in the current scope.
437 But, DECL is another declaration for that same identifier in the
438 same scope. This is the `struct stat' hack whereby a non-typedef
439 class name or enum-name can be bound at the same level as some other
440 kind of entity.
441 3.3.7/1
443 A class name (9.1) or enumeration name (7.2) can be hidden by the
444 name of an object, function, or enumerator declared in the same scope.
445 If a class or enumeration name and an object, function, or enumerator
446 are declared in the same scope (in any order) with the same name, the
447 class or enumeration name is hidden wherever the object, function, or
448 enumerator name is visible.
450 It's the responsibility of the caller to check that
451 inserting this name is valid here. Returns nonzero if the new binding
452 was successful. */
454 static bool
455 supplement_binding_1 (cxx_binding *binding, tree decl)
457 tree bval = binding->value;
458 bool ok = true;
459 tree target_bval = strip_using_decl (bval);
460 tree target_decl = strip_using_decl (decl);
462 if (TREE_CODE (target_decl) == TYPE_DECL && DECL_ARTIFICIAL (target_decl)
463 && target_decl != target_bval
464 && (TREE_CODE (target_bval) != TYPE_DECL
465 /* We allow pushing an enum multiple times in a class
466 template in order to handle late matching of underlying
467 type on an opaque-enum-declaration followed by an
468 enum-specifier. */
469 || (processing_template_decl
470 && TREE_CODE (TREE_TYPE (target_decl)) == ENUMERAL_TYPE
471 && TREE_CODE (TREE_TYPE (target_bval)) == ENUMERAL_TYPE
472 && (dependent_type_p (ENUM_UNDERLYING_TYPE
473 (TREE_TYPE (target_decl)))
474 || dependent_type_p (ENUM_UNDERLYING_TYPE
475 (TREE_TYPE (target_bval)))))))
476 /* The new name is the type name. */
477 binding->type = decl;
478 else if (/* TARGET_BVAL is null when push_class_level_binding moves
479 an inherited type-binding out of the way to make room
480 for a new value binding. */
481 !target_bval
482 /* TARGET_BVAL is error_mark_node when TARGET_DECL's name
483 has been used in a non-class scope prior declaration.
484 In that case, we should have already issued a
485 diagnostic; for graceful error recovery purpose, pretend
486 this was the intended declaration for that name. */
487 || target_bval == error_mark_node
488 /* If TARGET_BVAL is anticipated but has not yet been
489 declared, pretend it is not there at all. */
490 || (TREE_CODE (target_bval) == FUNCTION_DECL
491 && DECL_ANTICIPATED (target_bval)
492 && !DECL_HIDDEN_FRIEND_P (target_bval)))
493 binding->value = decl;
494 else if (TREE_CODE (target_bval) == TYPE_DECL
495 && DECL_ARTIFICIAL (target_bval)
496 && target_decl != target_bval
497 && (TREE_CODE (target_decl) != TYPE_DECL
498 || same_type_p (TREE_TYPE (target_decl),
499 TREE_TYPE (target_bval))))
501 /* The old binding was a type name. It was placed in
502 VALUE field because it was thought, at the point it was
503 declared, to be the only entity with such a name. Move the
504 type name into the type slot; it is now hidden by the new
505 binding. */
506 binding->type = bval;
507 binding->value = decl;
508 binding->value_is_inherited = false;
510 else if (TREE_CODE (target_bval) == TYPE_DECL
511 && TREE_CODE (target_decl) == TYPE_DECL
512 && DECL_NAME (target_decl) == DECL_NAME (target_bval)
513 && binding->scope->kind != sk_class
514 && (same_type_p (TREE_TYPE (target_decl), TREE_TYPE (target_bval))
515 /* If either type involves template parameters, we must
516 wait until instantiation. */
517 || uses_template_parms (TREE_TYPE (target_decl))
518 || uses_template_parms (TREE_TYPE (target_bval))))
519 /* We have two typedef-names, both naming the same type to have
520 the same name. In general, this is OK because of:
522 [dcl.typedef]
524 In a given scope, a typedef specifier can be used to redefine
525 the name of any type declared in that scope to refer to the
526 type to which it already refers.
528 However, in class scopes, this rule does not apply due to the
529 stricter language in [class.mem] prohibiting redeclarations of
530 members. */
531 ok = false;
532 /* There can be two block-scope declarations of the same variable,
533 so long as they are `extern' declarations. However, there cannot
534 be two declarations of the same static data member:
536 [class.mem]
538 A member shall not be declared twice in the
539 member-specification. */
540 else if (VAR_P (target_decl)
541 && VAR_P (target_bval)
542 && DECL_EXTERNAL (target_decl) && DECL_EXTERNAL (target_bval)
543 && !DECL_CLASS_SCOPE_P (target_decl))
545 duplicate_decls (decl, binding->value, /*newdecl_is_friend=*/false);
546 ok = false;
548 else if (TREE_CODE (decl) == NAMESPACE_DECL
549 && TREE_CODE (bval) == NAMESPACE_DECL
550 && DECL_NAMESPACE_ALIAS (decl)
551 && DECL_NAMESPACE_ALIAS (bval)
552 && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl))
553 /* [namespace.alias]
555 In a declarative region, a namespace-alias-definition can be
556 used to redefine a namespace-alias declared in that declarative
557 region to refer only to the namespace to which it already
558 refers. */
559 ok = false;
560 else if (maybe_remove_implicit_alias (bval))
562 /* There was a mangling compatibility alias using this mangled name,
563 but now we have a real decl that wants to use it instead. */
564 binding->value = decl;
566 else
568 if (!error_operand_p (bval))
569 diagnose_name_conflict (decl, bval);
570 ok = false;
573 return ok;
576 /* Diagnose a name conflict between DECL and BVAL. */
578 static void
579 diagnose_name_conflict (tree decl, tree bval)
581 if (TREE_CODE (decl) == TREE_CODE (bval)
582 && (TREE_CODE (decl) != TYPE_DECL
583 || (DECL_ARTIFICIAL (decl) && DECL_ARTIFICIAL (bval))
584 || (!DECL_ARTIFICIAL (decl) && !DECL_ARTIFICIAL (bval)))
585 && !is_overloaded_fn (decl))
586 error ("redeclaration of %q#D", decl);
587 else
588 error ("%q#D conflicts with a previous declaration", decl);
590 inform (location_of (bval), "previous declaration %q#D", bval);
593 /* Wrapper for supplement_binding_1. */
595 static bool
596 supplement_binding (cxx_binding *binding, tree decl)
598 bool ret;
599 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
600 ret = supplement_binding_1 (binding, decl);
601 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
602 return ret;
605 /* Add DECL to the list of things declared in B. */
607 static void
608 add_decl_to_level (tree decl, cp_binding_level *b)
610 /* We used to record virtual tables as if they were ordinary
611 variables, but no longer do so. */
612 gcc_assert (!(VAR_P (decl) && DECL_VIRTUAL_P (decl)));
614 if (TREE_CODE (decl) == NAMESPACE_DECL
615 && !DECL_NAMESPACE_ALIAS (decl))
617 DECL_CHAIN (decl) = b->namespaces;
618 b->namespaces = decl;
620 else
622 /* We build up the list in reverse order, and reverse it later if
623 necessary. */
624 TREE_CHAIN (decl) = b->names;
625 b->names = decl;
627 /* If appropriate, add decl to separate list of statics. We
628 include extern variables because they might turn out to be
629 static later. It's OK for this list to contain a few false
630 positives. */
631 if (b->kind == sk_namespace)
632 if ((VAR_P (decl)
633 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
634 || (TREE_CODE (decl) == FUNCTION_DECL
635 && (!TREE_PUBLIC (decl)
636 || decl_anon_ns_mem_p (decl)
637 || DECL_DECLARED_INLINE_P (decl))))
638 vec_safe_push (b->static_decls, decl);
642 /* Record a decl-node X as belonging to the current lexical scope.
643 Check for errors (such as an incompatible declaration for the same
644 name already seen in the same scope). IS_FRIEND is true if X is
645 declared as a friend.
647 Returns either X or an old decl for the same name.
648 If an old decl is returned, it may have been smashed
649 to agree with what X says. */
651 static tree
652 pushdecl_maybe_friend_1 (tree x, bool is_friend)
654 tree t;
655 tree name;
656 int need_new_binding;
658 if (x == error_mark_node)
659 return error_mark_node;
661 need_new_binding = 1;
663 if (DECL_TEMPLATE_PARM_P (x))
664 /* Template parameters have no context; they are not X::T even
665 when declared within a class or namespace. */
667 else
669 if (current_function_decl && x != current_function_decl
670 /* A local declaration for a function doesn't constitute
671 nesting. */
672 && TREE_CODE (x) != FUNCTION_DECL
673 /* A local declaration for an `extern' variable is in the
674 scope of the current namespace, not the current
675 function. */
676 && !(VAR_P (x) && DECL_EXTERNAL (x))
677 /* When parsing the parameter list of a function declarator,
678 don't set DECL_CONTEXT to an enclosing function. When we
679 push the PARM_DECLs in order to process the function body,
680 current_binding_level->this_entity will be set. */
681 && !(TREE_CODE (x) == PARM_DECL
682 && current_binding_level->kind == sk_function_parms
683 && current_binding_level->this_entity == NULL)
684 && !DECL_CONTEXT (x))
685 DECL_CONTEXT (x) = current_function_decl;
687 /* If this is the declaration for a namespace-scope function,
688 but the declaration itself is in a local scope, mark the
689 declaration. */
690 if (TREE_CODE (x) == FUNCTION_DECL
691 && DECL_NAMESPACE_SCOPE_P (x)
692 && current_function_decl
693 && x != current_function_decl)
694 DECL_LOCAL_FUNCTION_P (x) = 1;
697 name = DECL_NAME (x);
698 if (name)
700 int different_binding_level = 0;
702 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
703 name = TREE_OPERAND (name, 0);
705 /* In case this decl was explicitly namespace-qualified, look it
706 up in its namespace context. */
707 if (DECL_NAMESPACE_SCOPE_P (x) && namespace_bindings_p ())
708 t = namespace_binding (name, DECL_CONTEXT (x));
709 else
710 t = lookup_name_innermost_nonclass_level (name);
712 /* [basic.link] If there is a visible declaration of an entity
713 with linkage having the same name and type, ignoring entities
714 declared outside the innermost enclosing namespace scope, the
715 block scope declaration declares that same entity and
716 receives the linkage of the previous declaration. */
717 if (! t && current_function_decl && x != current_function_decl
718 && VAR_OR_FUNCTION_DECL_P (x)
719 && DECL_EXTERNAL (x))
721 /* Look in block scope. */
722 t = innermost_non_namespace_value (name);
723 /* Or in the innermost namespace. */
724 if (! t)
725 t = namespace_binding (name, DECL_CONTEXT (x));
726 /* Does it have linkage? Note that if this isn't a DECL, it's an
727 OVERLOAD, which is OK. */
728 if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t)))
729 t = NULL_TREE;
730 if (t)
731 different_binding_level = 1;
734 /* If we are declaring a function, and the result of name-lookup
735 was an OVERLOAD, look for an overloaded instance that is
736 actually the same as the function we are declaring. (If
737 there is one, we have to merge our declaration with the
738 previous declaration.) */
739 if (t && TREE_CODE (t) == OVERLOAD)
741 tree match;
743 if (TREE_CODE (x) == FUNCTION_DECL)
744 for (match = t; match; match = OVL_NEXT (match))
746 if (decls_match (OVL_CURRENT (match), x))
747 break;
749 else
750 /* Just choose one. */
751 match = t;
753 if (match)
754 t = OVL_CURRENT (match);
755 else
756 t = NULL_TREE;
759 if (t && t != error_mark_node)
761 if (different_binding_level)
763 if (decls_match (x, t))
764 /* The standard only says that the local extern
765 inherits linkage from the previous decl; in
766 particular, default args are not shared. Add
767 the decl into a hash table to make sure only
768 the previous decl in this case is seen by the
769 middle end. */
771 struct cxx_int_tree_map *h;
773 TREE_PUBLIC (x) = TREE_PUBLIC (t);
775 if (cp_function_chain->extern_decl_map == NULL)
776 cp_function_chain->extern_decl_map
777 = hash_table<cxx_int_tree_map_hasher>::create_ggc (20);
779 h = ggc_alloc<cxx_int_tree_map> ();
780 h->uid = DECL_UID (x);
781 h->to = t;
782 cxx_int_tree_map **loc = cp_function_chain->extern_decl_map
783 ->find_slot (h, INSERT);
784 *loc = h;
787 else if (TREE_CODE (t) == PARM_DECL)
789 /* Check for duplicate params. */
790 tree d = duplicate_decls (x, t, is_friend);
791 if (d)
792 return d;
794 else if ((DECL_EXTERN_C_FUNCTION_P (x)
795 || DECL_FUNCTION_TEMPLATE_P (x))
796 && is_overloaded_fn (t))
797 /* Don't do anything just yet. */;
798 else if (t == wchar_decl_node)
800 if (! DECL_IN_SYSTEM_HEADER (x))
801 pedwarn (input_location, OPT_Wpedantic, "redeclaration of %<wchar_t%> as %qT",
802 TREE_TYPE (x));
804 /* Throw away the redeclaration. */
805 return t;
807 else
809 tree olddecl = duplicate_decls (x, t, is_friend);
811 /* If the redeclaration failed, we can stop at this
812 point. */
813 if (olddecl == error_mark_node)
814 return error_mark_node;
816 if (olddecl)
818 if (TREE_CODE (t) == TYPE_DECL)
819 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
821 return t;
823 else if (DECL_MAIN_P (x) && TREE_CODE (t) == FUNCTION_DECL)
825 /* A redeclaration of main, but not a duplicate of the
826 previous one.
828 [basic.start.main]
830 This function shall not be overloaded. */
831 error ("invalid redeclaration of %q+D", t);
832 error ("as %qD", x);
833 /* We don't try to push this declaration since that
834 causes a crash. */
835 return x;
840 /* If x has C linkage-specification, (extern "C"),
841 lookup its binding, in case it's already bound to an object.
842 The lookup is done in all namespaces.
843 If we find an existing binding, make sure it has the same
844 exception specification as x, otherwise, bail in error [7.5, 7.6]. */
845 if ((TREE_CODE (x) == FUNCTION_DECL)
846 && DECL_EXTERN_C_P (x)
847 /* We should ignore declarations happening in system headers. */
848 && !DECL_ARTIFICIAL (x)
849 && !DECL_IN_SYSTEM_HEADER (x))
851 tree previous = lookup_extern_c_fun_in_all_ns (x);
852 if (previous
853 && !DECL_ARTIFICIAL (previous)
854 && !DECL_IN_SYSTEM_HEADER (previous)
855 && DECL_CONTEXT (previous) != DECL_CONTEXT (x))
857 /* In case either x or previous is declared to throw an exception,
858 make sure both exception specifications are equal. */
859 if (decls_match (x, previous))
861 tree x_exception_spec = NULL_TREE;
862 tree previous_exception_spec = NULL_TREE;
864 x_exception_spec =
865 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (x));
866 previous_exception_spec =
867 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (previous));
868 if (!comp_except_specs (previous_exception_spec,
869 x_exception_spec,
870 ce_normal))
872 pedwarn (input_location, 0,
873 "declaration of %q#D with C language linkage",
875 pedwarn (DECL_SOURCE_LOCATION (previous), 0,
876 "conflicts with previous declaration %q#D",
877 previous);
878 pedwarn (input_location, 0,
879 "due to different exception specifications");
880 return error_mark_node;
882 if (DECL_ASSEMBLER_NAME_SET_P (previous))
883 SET_DECL_ASSEMBLER_NAME (x,
884 DECL_ASSEMBLER_NAME (previous));
886 else
888 pedwarn (input_location, 0,
889 "declaration of %q#D with C language linkage", x);
890 pedwarn (DECL_SOURCE_LOCATION (previous), 0,
891 "conflicts with previous declaration %q#D",
892 previous);
897 check_template_shadow (x);
899 /* If this is a function conjured up by the back end, massage it
900 so it looks friendly. */
901 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x))
903 retrofit_lang_decl (x);
904 SET_DECL_LANGUAGE (x, lang_c);
907 t = x;
908 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x))
910 t = push_overloaded_decl (x, PUSH_LOCAL, is_friend);
911 if (!namespace_bindings_p ())
912 /* We do not need to create a binding for this name;
913 push_overloaded_decl will have already done so if
914 necessary. */
915 need_new_binding = 0;
917 else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
919 t = push_overloaded_decl (x, PUSH_GLOBAL, is_friend);
920 if (t == x)
921 add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
924 if (DECL_DECLARES_FUNCTION_P (t))
926 check_default_args (t);
928 if (is_friend && t == x && !flag_friend_injection)
930 /* This is a new friend declaration of a function or a
931 function template, so hide it from ordinary function
932 lookup. */
933 DECL_ANTICIPATED (t) = 1;
934 DECL_HIDDEN_FRIEND_P (t) = 1;
938 if (t != x || DECL_FUNCTION_TEMPLATE_P (t))
939 return t;
941 /* If declaring a type as a typedef, copy the type (unless we're
942 at line 0), and install this TYPE_DECL as the new type's typedef
943 name. See the extensive comment of set_underlying_type (). */
944 if (TREE_CODE (x) == TYPE_DECL)
946 tree type = TREE_TYPE (x);
948 if (DECL_IS_BUILTIN (x)
949 || (TREE_TYPE (x) != error_mark_node
950 && TYPE_NAME (type) != x
951 /* We don't want to copy the type when all we're
952 doing is making a TYPE_DECL for the purposes of
953 inlining. */
954 && (!TYPE_NAME (type)
955 || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x))))
956 set_underlying_type (x);
958 if (type != error_mark_node
959 && TYPE_IDENTIFIER (type))
960 set_identifier_type_value (DECL_NAME (x), x);
962 /* If this is a locally defined typedef in a function that
963 is not a template instantation, record it to implement
964 -Wunused-local-typedefs. */
965 if (!instantiating_current_function_p ())
966 record_locally_defined_typedef (x);
969 /* Multiple external decls of the same identifier ought to match.
971 We get warnings about inline functions where they are defined.
972 We get warnings about other functions from push_overloaded_decl.
974 Avoid duplicate warnings where they are used. */
975 if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
977 tree decl;
979 decl = IDENTIFIER_NAMESPACE_VALUE (name);
980 if (decl && TREE_CODE (decl) == OVERLOAD)
981 decl = OVL_FUNCTION (decl);
983 if (decl && decl != error_mark_node
984 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl))
985 /* If different sort of thing, we already gave an error. */
986 && TREE_CODE (decl) == TREE_CODE (x)
987 && !comptypes (TREE_TYPE (x), TREE_TYPE (decl),
988 COMPARE_REDECLARATION))
990 if (permerror (input_location, "type mismatch with previous "
991 "external decl of %q#D", x))
992 inform (DECL_SOURCE_LOCATION (decl),
993 "previous external decl of %q#D", decl);
997 /* This name is new in its binding level.
998 Install the new declaration and return it. */
999 if (namespace_bindings_p ())
1001 /* Install a global value. */
1003 /* If the first global decl has external linkage,
1004 warn if we later see static one. */
1005 if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
1006 TREE_PUBLIC (name) = 1;
1008 /* Bind the name for the entity. */
1009 if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
1010 && t != NULL_TREE)
1011 && (TREE_CODE (x) == TYPE_DECL
1012 || VAR_P (x)
1013 || TREE_CODE (x) == NAMESPACE_DECL
1014 || TREE_CODE (x) == CONST_DECL
1015 || TREE_CODE (x) == TEMPLATE_DECL))
1016 SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
1018 /* If new decl is `static' and an `extern' was seen previously,
1019 warn about it. */
1020 if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
1021 warn_extern_redeclared_static (x, t);
1023 else
1025 /* Here to install a non-global value. */
1026 tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
1027 tree oldlocal = NULL_TREE;
1028 cp_binding_level *oldscope = NULL;
1029 cxx_binding *oldbinding = outer_binding (name, NULL, true);
1030 if (oldbinding)
1032 oldlocal = oldbinding->value;
1033 oldscope = oldbinding->scope;
1036 if (need_new_binding)
1038 push_local_binding (name, x, 0);
1039 /* Because push_local_binding will hook X on to the
1040 current_binding_level's name list, we don't want to
1041 do that again below. */
1042 need_new_binding = 0;
1045 /* If this is a TYPE_DECL, push it into the type value slot. */
1046 if (TREE_CODE (x) == TYPE_DECL)
1047 set_identifier_type_value (name, x);
1049 /* Clear out any TYPE_DECL shadowed by a namespace so that
1050 we won't think this is a type. The C struct hack doesn't
1051 go through namespaces. */
1052 if (TREE_CODE (x) == NAMESPACE_DECL)
1053 set_identifier_type_value (name, NULL_TREE);
1055 if (oldlocal)
1057 tree d = oldlocal;
1059 while (oldlocal
1060 && VAR_P (oldlocal)
1061 && DECL_DEAD_FOR_LOCAL (oldlocal))
1062 oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal);
1064 if (oldlocal == NULL_TREE)
1065 oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d));
1068 /* If this is an extern function declaration, see if we
1069 have a global definition or declaration for the function. */
1070 if (oldlocal == NULL_TREE
1071 && DECL_EXTERNAL (x)
1072 && oldglobal != NULL_TREE
1073 && TREE_CODE (x) == FUNCTION_DECL
1074 && TREE_CODE (oldglobal) == FUNCTION_DECL)
1076 /* We have one. Their types must agree. */
1077 if (decls_match (x, oldglobal))
1078 /* OK */;
1079 else
1081 warning (0, "extern declaration of %q#D doesn%'t match", x);
1082 warning_at (DECL_SOURCE_LOCATION (oldglobal), 0,
1083 "global declaration %q#D", oldglobal);
1086 /* If we have a local external declaration,
1087 and no file-scope declaration has yet been seen,
1088 then if we later have a file-scope decl it must not be static. */
1089 if (oldlocal == NULL_TREE
1090 && oldglobal == NULL_TREE
1091 && DECL_EXTERNAL (x)
1092 && TREE_PUBLIC (x))
1093 TREE_PUBLIC (name) = 1;
1095 /* Don't complain about the parms we push and then pop
1096 while tentatively parsing a function declarator. */
1097 if (TREE_CODE (x) == PARM_DECL && DECL_CONTEXT (x) == NULL_TREE)
1098 /* Ignore. */;
1100 /* Warn if shadowing an argument at the top level of the body. */
1101 else if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
1102 /* Inline decls shadow nothing. */
1103 && !DECL_FROM_INLINE (x)
1104 && (TREE_CODE (oldlocal) == PARM_DECL
1105 || VAR_P (oldlocal)
1106 /* If the old decl is a type decl, only warn if the
1107 old decl is an explicit typedef or if both the old
1108 and new decls are type decls. */
1109 || (TREE_CODE (oldlocal) == TYPE_DECL
1110 && (!DECL_ARTIFICIAL (oldlocal)
1111 || TREE_CODE (x) == TYPE_DECL)))
1112 /* Don't check for internally generated vars unless
1113 it's an implicit typedef (see create_implicit_typedef
1114 in decl.c) or anonymous union variable. */
1115 && (!DECL_ARTIFICIAL (x)
1116 || DECL_IMPLICIT_TYPEDEF_P (x)
1117 || (VAR_P (x) && DECL_ANON_UNION_VAR_P (x))))
1119 bool nowarn = false;
1121 /* Don't complain if it's from an enclosing function. */
1122 if (DECL_CONTEXT (oldlocal) == current_function_decl
1123 && TREE_CODE (x) != PARM_DECL
1124 && TREE_CODE (oldlocal) == PARM_DECL)
1126 /* Go to where the parms should be and see if we find
1127 them there. */
1128 cp_binding_level *b = current_binding_level->level_chain;
1130 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
1131 /* Skip the ctor/dtor cleanup level. */
1132 b = b->level_chain;
1134 /* ARM $8.3 */
1135 if (b->kind == sk_function_parms)
1137 error ("declaration of %q#D shadows a parameter", x);
1138 nowarn = true;
1142 /* The local structure or class can't use parameters of
1143 the containing function anyway. */
1144 if (DECL_CONTEXT (oldlocal) != current_function_decl)
1146 cp_binding_level *scope = current_binding_level;
1147 tree context = DECL_CONTEXT (oldlocal);
1148 for (; scope; scope = scope->level_chain)
1150 if (scope->kind == sk_function_parms
1151 && scope->this_entity == context)
1152 break;
1153 if (scope->kind == sk_class
1154 && !LAMBDA_TYPE_P (scope->this_entity))
1156 nowarn = true;
1157 break;
1161 /* Error if redeclaring a local declared in a
1162 init-statement or in the condition of an if or
1163 switch statement when the new declaration is in the
1164 outermost block of the controlled statement.
1165 Redeclaring a variable from a for or while condition is
1166 detected elsewhere. */
1167 else if (VAR_P (oldlocal)
1168 && oldscope == current_binding_level->level_chain
1169 && (oldscope->kind == sk_cond
1170 || oldscope->kind == sk_for))
1172 error ("redeclaration of %q#D", x);
1173 inform (DECL_SOURCE_LOCATION (oldlocal),
1174 "%q#D previously declared here", oldlocal);
1175 nowarn = true;
1177 /* C++11:
1178 3.3.3/3: The name declared in an exception-declaration (...)
1179 shall not be redeclared in the outermost block of the handler.
1180 3.3.3/2: A parameter name shall not be redeclared (...) in
1181 the outermost block of any handler associated with a
1182 function-try-block.
1183 3.4.1/15: The function parameter names shall not be redeclared
1184 in the exception-declaration nor in the outermost block of a
1185 handler for the function-try-block. */
1186 else if ((VAR_P (oldlocal)
1187 && oldscope == current_binding_level->level_chain
1188 && oldscope->kind == sk_catch)
1189 || (TREE_CODE (oldlocal) == PARM_DECL
1190 && (current_binding_level->kind == sk_catch
1191 || (current_binding_level->level_chain->kind
1192 == sk_catch))
1193 && in_function_try_handler))
1195 if (permerror (input_location, "redeclaration of %q#D", x))
1196 inform (DECL_SOURCE_LOCATION (oldlocal),
1197 "%q#D previously declared here", oldlocal);
1198 nowarn = true;
1201 if ((warn_shadow
1202 || warn_shadow_local
1203 || warn_shadow_compatible_local)
1204 && !nowarn)
1206 bool warned;
1207 enum opt_code warning_code;
1208 /* If '-Wshadow=compatible-local' is specified without other
1209 -Wshadow= flags, we will warn only when the type of the
1210 shadowing variable (i.e. x) can be converted to that of
1211 the shadowed parameter (oldlocal). The reason why we only
1212 check if x's type can be converted to oldlocal's type
1213 (but not the other way around) is because when users
1214 accidentally shadow a parameter, more than often they
1215 would use the variable thinking (mistakenly) it's still
1216 the parameter. It would be rare that users would use the
1217 variable in the place that expects the parameter but
1218 thinking it's a new decl. */
1219 if (warn_shadow)
1220 warning_code = OPT_Wshadow;
1221 else if (can_convert (TREE_TYPE (oldlocal), TREE_TYPE (x),
1222 tf_none))
1223 warning_code = OPT_Wshadow_compatible_local;
1224 else
1225 warning_code = OPT_Wshadow_local;
1227 if (TREE_CODE (oldlocal) == PARM_DECL)
1228 warned = warning_at (input_location, warning_code,
1229 "declaration of %q#D shadows a parameter", x);
1230 else if (is_capture_proxy (oldlocal))
1231 warned = warning_at (input_location, warning_code,
1232 "declaration of %qD shadows a lambda capture",
1234 else
1235 warned = warning_at (input_location, warning_code,
1236 "declaration of %qD shadows a previous local",
1239 if (warned)
1240 inform (DECL_SOURCE_LOCATION (oldlocal),
1241 "shadowed declaration is here");
1245 /* Maybe warn if shadowing something else. */
1246 else if (warn_shadow && !DECL_EXTERNAL (x)
1247 /* No shadow warnings for internally generated vars unless
1248 it's an implicit typedef (see create_implicit_typedef
1249 in decl.c). */
1250 && (! DECL_ARTIFICIAL (x) || DECL_IMPLICIT_TYPEDEF_P (x))
1251 /* No shadow warnings for vars made for inlining. */
1252 && ! DECL_FROM_INLINE (x))
1254 tree member;
1256 if (nonlambda_method_basetype ())
1257 member = lookup_member (current_nonlambda_class_type (),
1258 name,
1259 /*protect=*/0,
1260 /*want_type=*/false,
1261 tf_warning_or_error);
1262 else
1263 member = NULL_TREE;
1265 if (member && !TREE_STATIC (member))
1267 if (BASELINK_P (member))
1268 member = BASELINK_FUNCTIONS (member);
1269 member = OVL_CURRENT (member);
1271 /* Do not warn if a variable shadows a function, unless
1272 the variable is a function or a pointer-to-function. */
1273 if (TREE_CODE (member) != FUNCTION_DECL
1274 || TREE_CODE (x) == FUNCTION_DECL
1275 || TYPE_PTRFN_P (TREE_TYPE (x))
1276 || TYPE_PTRMEMFUNC_P (TREE_TYPE (x)))
1278 if (warning_at (input_location, OPT_Wshadow,
1279 "declaration of %qD shadows a member of %qT",
1280 x, current_nonlambda_class_type ())
1281 && DECL_P (member))
1282 inform (DECL_SOURCE_LOCATION (member),
1283 "shadowed declaration is here");
1286 else if (oldglobal != NULL_TREE
1287 && (VAR_P (oldglobal)
1288 /* If the old decl is a type decl, only warn if the
1289 old decl is an explicit typedef or if both the
1290 old and new decls are type decls. */
1291 || (TREE_CODE (oldglobal) == TYPE_DECL
1292 && (!DECL_ARTIFICIAL (oldglobal)
1293 || TREE_CODE (x) == TYPE_DECL)))
1294 && !instantiating_current_function_p ())
1295 /* XXX shadow warnings in outer-more namespaces */
1297 if (warning_at (input_location, OPT_Wshadow,
1298 "declaration of %qD shadows a "
1299 "global declaration", x))
1300 inform (DECL_SOURCE_LOCATION (oldglobal),
1301 "shadowed declaration is here");
1306 if (VAR_P (x))
1307 maybe_register_incomplete_var (x);
1310 if (need_new_binding)
1311 add_decl_to_level (x,
1312 DECL_NAMESPACE_SCOPE_P (x)
1313 ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x))
1314 : current_binding_level);
1316 return x;
1319 /* Wrapper for pushdecl_maybe_friend_1. */
1321 tree
1322 pushdecl_maybe_friend (tree x, bool is_friend)
1324 tree ret;
1325 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
1326 ret = pushdecl_maybe_friend_1 (x, is_friend);
1327 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
1328 return ret;
1331 /* Record a decl-node X as belonging to the current lexical scope. */
1333 tree
1334 pushdecl (tree x)
1336 return pushdecl_maybe_friend (x, false);
1339 /* Enter DECL into the symbol table, if that's appropriate. Returns
1340 DECL, or a modified version thereof. */
1342 tree
1343 maybe_push_decl (tree decl)
1345 tree type = TREE_TYPE (decl);
1347 /* Add this decl to the current binding level, but not if it comes
1348 from another scope, e.g. a static member variable. TEM may equal
1349 DECL or it may be a previous decl of the same name. */
1350 if (decl == error_mark_node
1351 || (TREE_CODE (decl) != PARM_DECL
1352 && DECL_CONTEXT (decl) != NULL_TREE
1353 /* Definitions of namespace members outside their namespace are
1354 possible. */
1355 && !DECL_NAMESPACE_SCOPE_P (decl))
1356 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
1357 || type == unknown_type_node
1358 /* The declaration of a template specialization does not affect
1359 the functions available for overload resolution, so we do not
1360 call pushdecl. */
1361 || (TREE_CODE (decl) == FUNCTION_DECL
1362 && DECL_TEMPLATE_SPECIALIZATION (decl)))
1363 return decl;
1364 else
1365 return pushdecl (decl);
1368 /* Bind DECL to ID in the current_binding_level, assumed to be a local
1369 binding level. If PUSH_USING is set in FLAGS, we know that DECL
1370 doesn't really belong to this binding level, that it got here
1371 through a using-declaration. */
1373 void
1374 push_local_binding (tree id, tree decl, int flags)
1376 cp_binding_level *b;
1378 /* Skip over any local classes. This makes sense if we call
1379 push_local_binding with a friend decl of a local class. */
1380 b = innermost_nonclass_level ();
1382 if (lookup_name_innermost_nonclass_level (id))
1384 /* Supplement the existing binding. */
1385 if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
1386 /* It didn't work. Something else must be bound at this
1387 level. Do not add DECL to the list of things to pop
1388 later. */
1389 return;
1391 else
1392 /* Create a new binding. */
1393 push_binding (id, decl, b);
1395 if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
1396 /* We must put the OVERLOAD into a TREE_LIST since the
1397 TREE_CHAIN of an OVERLOAD is already used. Similarly for
1398 decls that got here through a using-declaration. */
1399 decl = build_tree_list (NULL_TREE, decl);
1401 /* And put DECL on the list of things declared by the current
1402 binding level. */
1403 add_decl_to_level (decl, b);
1406 /* Check to see whether or not DECL is a variable that would have been
1407 in scope under the ARM, but is not in scope under the ANSI/ISO
1408 standard. If so, issue an error message. If name lookup would
1409 work in both cases, but return a different result, this function
1410 returns the result of ANSI/ISO lookup. Otherwise, it returns
1411 DECL. */
1413 tree
1414 check_for_out_of_scope_variable (tree decl)
1416 tree shadowed;
1418 /* We only care about out of scope variables. */
1419 if (!(VAR_P (decl) && DECL_DEAD_FOR_LOCAL (decl)))
1420 return decl;
1422 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (decl)
1423 ? DECL_SHADOWED_FOR_VAR (decl) : NULL_TREE ;
1424 while (shadowed != NULL_TREE && VAR_P (shadowed)
1425 && DECL_DEAD_FOR_LOCAL (shadowed))
1426 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (shadowed)
1427 ? DECL_SHADOWED_FOR_VAR (shadowed) : NULL_TREE;
1428 if (!shadowed)
1429 shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl));
1430 if (shadowed)
1432 if (!DECL_ERROR_REPORTED (decl))
1434 warning (0, "name lookup of %qD changed", DECL_NAME (decl));
1435 warning_at (DECL_SOURCE_LOCATION (shadowed), 0,
1436 " matches this %qD under ISO standard rules",
1437 shadowed);
1438 warning_at (DECL_SOURCE_LOCATION (decl), 0,
1439 " matches this %qD under old rules", decl);
1440 DECL_ERROR_REPORTED (decl) = 1;
1442 return shadowed;
1445 /* If we have already complained about this declaration, there's no
1446 need to do it again. */
1447 if (DECL_ERROR_REPORTED (decl))
1448 return decl;
1450 DECL_ERROR_REPORTED (decl) = 1;
1452 if (TREE_TYPE (decl) == error_mark_node)
1453 return decl;
1455 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
1457 error ("name lookup of %qD changed for ISO %<for%> scoping",
1458 DECL_NAME (decl));
1459 error (" cannot use obsolete binding at %q+D because "
1460 "it has a destructor", decl);
1461 return error_mark_node;
1463 else
1465 permerror (input_location, "name lookup of %qD changed for ISO %<for%> scoping",
1466 DECL_NAME (decl));
1467 if (flag_permissive)
1468 permerror (DECL_SOURCE_LOCATION (decl),
1469 " using obsolete binding at %qD", decl);
1470 else
1472 static bool hint;
1473 if (!hint)
1475 inform (input_location, "(if you use %<-fpermissive%> G++ will accept your code)");
1476 hint = true;
1481 return decl;
1484 /* true means unconditionally make a BLOCK for the next level pushed. */
1486 static bool keep_next_level_flag;
1488 static int binding_depth = 0;
1490 static void
1491 indent (int depth)
1493 int i;
1495 for (i = 0; i < depth * 2; i++)
1496 putc (' ', stderr);
1499 /* Return a string describing the kind of SCOPE we have. */
1500 static const char *
1501 cp_binding_level_descriptor (cp_binding_level *scope)
1503 /* The order of this table must match the "scope_kind"
1504 enumerators. */
1505 static const char* scope_kind_names[] = {
1506 "block-scope",
1507 "cleanup-scope",
1508 "try-scope",
1509 "catch-scope",
1510 "for-scope",
1511 "function-parameter-scope",
1512 "class-scope",
1513 "namespace-scope",
1514 "template-parameter-scope",
1515 "template-explicit-spec-scope"
1517 const scope_kind kind = scope->explicit_spec_p
1518 ? sk_template_spec : scope->kind;
1520 return scope_kind_names[kind];
1523 /* Output a debugging information about SCOPE when performing
1524 ACTION at LINE. */
1525 static void
1526 cp_binding_level_debug (cp_binding_level *scope, int line, const char *action)
1528 const char *desc = cp_binding_level_descriptor (scope);
1529 if (scope->this_entity)
1530 verbatim ("%s %s(%E) %p %d\n", action, desc,
1531 scope->this_entity, (void *) scope, line);
1532 else
1533 verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
1536 /* Return the estimated initial size of the hashtable of a NAMESPACE
1537 scope. */
1539 static inline size_t
1540 namespace_scope_ht_size (tree ns)
1542 tree name = DECL_NAME (ns);
1544 return name == std_identifier
1545 ? NAMESPACE_STD_HT_SIZE
1546 : (name == global_scope_name
1547 ? GLOBAL_SCOPE_HT_SIZE
1548 : NAMESPACE_ORDINARY_HT_SIZE);
1551 /* A chain of binding_level structures awaiting reuse. */
1553 static GTY((deletable)) cp_binding_level *free_binding_level;
1555 /* Insert SCOPE as the innermost binding level. */
1557 void
1558 push_binding_level (cp_binding_level *scope)
1560 /* Add it to the front of currently active scopes stack. */
1561 scope->level_chain = current_binding_level;
1562 current_binding_level = scope;
1563 keep_next_level_flag = false;
1565 if (ENABLE_SCOPE_CHECKING)
1567 scope->binding_depth = binding_depth;
1568 indent (binding_depth);
1569 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
1570 "push");
1571 binding_depth++;
1575 /* Create a new KIND scope and make it the top of the active scopes stack.
1576 ENTITY is the scope of the associated C++ entity (namespace, class,
1577 function, C++0x enumeration); it is NULL otherwise. */
1579 cp_binding_level *
1580 begin_scope (scope_kind kind, tree entity)
1582 cp_binding_level *scope;
1584 /* Reuse or create a struct for this binding level. */
1585 if (!ENABLE_SCOPE_CHECKING && free_binding_level)
1587 scope = free_binding_level;
1588 free_binding_level = scope->level_chain;
1589 memset (scope, 0, sizeof (cp_binding_level));
1591 else
1592 scope = ggc_cleared_alloc<cp_binding_level> ();
1594 scope->this_entity = entity;
1595 scope->more_cleanups_ok = true;
1596 switch (kind)
1598 case sk_cleanup:
1599 scope->keep = true;
1600 break;
1602 case sk_template_spec:
1603 scope->explicit_spec_p = true;
1604 kind = sk_template_parms;
1605 /* Fall through. */
1606 case sk_template_parms:
1607 case sk_block:
1608 case sk_try:
1609 case sk_catch:
1610 case sk_for:
1611 case sk_cond:
1612 case sk_class:
1613 case sk_scoped_enum:
1614 case sk_function_parms:
1615 case sk_transaction:
1616 case sk_omp:
1617 scope->keep = keep_next_level_flag;
1618 break;
1620 case sk_namespace:
1621 NAMESPACE_LEVEL (entity) = scope;
1622 vec_alloc (scope->static_decls,
1623 (DECL_NAME (entity) == std_identifier
1624 || DECL_NAME (entity) == global_scope_name) ? 200 : 10);
1625 break;
1627 default:
1628 /* Should not happen. */
1629 gcc_unreachable ();
1630 break;
1632 scope->kind = kind;
1634 push_binding_level (scope);
1636 return scope;
1639 /* We're about to leave current scope. Pop the top of the stack of
1640 currently active scopes. Return the enclosing scope, now active. */
1642 cp_binding_level *
1643 leave_scope (void)
1645 cp_binding_level *scope = current_binding_level;
1647 if (scope->kind == sk_namespace && class_binding_level)
1648 current_binding_level = class_binding_level;
1650 /* We cannot leave a scope, if there are none left. */
1651 if (NAMESPACE_LEVEL (global_namespace))
1652 gcc_assert (!global_scope_p (scope));
1654 if (ENABLE_SCOPE_CHECKING)
1656 indent (--binding_depth);
1657 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
1658 "leave");
1661 /* Move one nesting level up. */
1662 current_binding_level = scope->level_chain;
1664 /* Namespace-scopes are left most probably temporarily, not
1665 completely; they can be reopened later, e.g. in namespace-extension
1666 or any name binding activity that requires us to resume a
1667 namespace. For classes, we cache some binding levels. For other
1668 scopes, we just make the structure available for reuse. */
1669 if (scope->kind != sk_namespace
1670 && scope->kind != sk_class)
1672 scope->level_chain = free_binding_level;
1673 gcc_assert (!ENABLE_SCOPE_CHECKING
1674 || scope->binding_depth == binding_depth);
1675 free_binding_level = scope;
1678 if (scope->kind == sk_class)
1680 /* Reset DEFINING_CLASS_P to allow for reuse of a
1681 class-defining scope in a non-defining context. */
1682 scope->defining_class_p = 0;
1684 /* Find the innermost enclosing class scope, and reset
1685 CLASS_BINDING_LEVEL appropriately. */
1686 class_binding_level = NULL;
1687 for (scope = current_binding_level; scope; scope = scope->level_chain)
1688 if (scope->kind == sk_class)
1690 class_binding_level = scope;
1691 break;
1695 return current_binding_level;
1698 static void
1699 resume_scope (cp_binding_level* b)
1701 /* Resuming binding levels is meant only for namespaces,
1702 and those cannot nest into classes. */
1703 gcc_assert (!class_binding_level);
1704 /* Also, resuming a non-directly nested namespace is a no-no. */
1705 gcc_assert (b->level_chain == current_binding_level);
1706 current_binding_level = b;
1707 if (ENABLE_SCOPE_CHECKING)
1709 b->binding_depth = binding_depth;
1710 indent (binding_depth);
1711 cp_binding_level_debug (b, LOCATION_LINE (input_location), "resume");
1712 binding_depth++;
1716 /* Return the innermost binding level that is not for a class scope. */
1718 static cp_binding_level *
1719 innermost_nonclass_level (void)
1721 cp_binding_level *b;
1723 b = current_binding_level;
1724 while (b->kind == sk_class)
1725 b = b->level_chain;
1727 return b;
1730 /* We're defining an object of type TYPE. If it needs a cleanup, but
1731 we're not allowed to add any more objects with cleanups to the current
1732 scope, create a new binding level. */
1734 void
1735 maybe_push_cleanup_level (tree type)
1737 if (type != error_mark_node
1738 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
1739 && current_binding_level->more_cleanups_ok == 0)
1741 begin_scope (sk_cleanup, NULL);
1742 current_binding_level->statement_list = push_stmt_list ();
1746 /* Return true if we are in the global binding level. */
1748 bool
1749 global_bindings_p (void)
1751 return global_scope_p (current_binding_level);
1754 /* True if we are currently in a toplevel binding level. This
1755 means either the global binding level or a namespace in a toplevel
1756 binding level. Since there are no non-toplevel namespace levels,
1757 this really means any namespace or template parameter level. We
1758 also include a class whose context is toplevel. */
1760 bool
1761 toplevel_bindings_p (void)
1763 cp_binding_level *b = innermost_nonclass_level ();
1765 return b->kind == sk_namespace || b->kind == sk_template_parms;
1768 /* True if this is a namespace scope, or if we are defining a class
1769 which is itself at namespace scope, or whose enclosing class is
1770 such a class, etc. */
1772 bool
1773 namespace_bindings_p (void)
1775 cp_binding_level *b = innermost_nonclass_level ();
1777 return b->kind == sk_namespace;
1780 /* True if the innermost non-class scope is a block scope. */
1782 bool
1783 local_bindings_p (void)
1785 cp_binding_level *b = innermost_nonclass_level ();
1786 return b->kind < sk_function_parms || b->kind == sk_omp;
1789 /* True if the current level needs to have a BLOCK made. */
1791 bool
1792 kept_level_p (void)
1794 return (current_binding_level->blocks != NULL_TREE
1795 || current_binding_level->keep
1796 || current_binding_level->kind == sk_cleanup
1797 || current_binding_level->names != NULL_TREE
1798 || current_binding_level->using_directives);
1801 /* Returns the kind of the innermost scope. */
1803 scope_kind
1804 innermost_scope_kind (void)
1806 return current_binding_level->kind;
1809 /* Returns true if this scope was created to store template parameters. */
1811 bool
1812 template_parm_scope_p (void)
1814 return innermost_scope_kind () == sk_template_parms;
1817 /* If KEEP is true, make a BLOCK node for the next binding level,
1818 unconditionally. Otherwise, use the normal logic to decide whether
1819 or not to create a BLOCK. */
1821 void
1822 keep_next_level (bool keep)
1824 keep_next_level_flag = keep;
1827 /* Return the list of declarations of the current level.
1828 Note that this list is in reverse order unless/until
1829 you nreverse it; and when you do nreverse it, you must
1830 store the result back using `storedecls' or you will lose. */
1832 tree
1833 getdecls (void)
1835 return current_binding_level->names;
1838 /* Return how many function prototypes we are currently nested inside. */
1841 function_parm_depth (void)
1843 int level = 0;
1844 cp_binding_level *b;
1846 for (b = current_binding_level;
1847 b->kind == sk_function_parms;
1848 b = b->level_chain)
1849 ++level;
1851 return level;
1854 /* For debugging. */
1855 static int no_print_functions = 0;
1856 static int no_print_builtins = 0;
1858 static void
1859 print_binding_level (cp_binding_level* lvl)
1861 tree t;
1862 int i = 0, len;
1863 fprintf (stderr, " blocks=%p", (void *) lvl->blocks);
1864 if (lvl->more_cleanups_ok)
1865 fprintf (stderr, " more-cleanups-ok");
1866 if (lvl->have_cleanups)
1867 fprintf (stderr, " have-cleanups");
1868 fprintf (stderr, "\n");
1869 if (lvl->names)
1871 fprintf (stderr, " names:\t");
1872 /* We can probably fit 3 names to a line? */
1873 for (t = lvl->names; t; t = TREE_CHAIN (t))
1875 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
1876 continue;
1877 if (no_print_builtins
1878 && (TREE_CODE (t) == TYPE_DECL)
1879 && DECL_IS_BUILTIN (t))
1880 continue;
1882 /* Function decls tend to have longer names. */
1883 if (TREE_CODE (t) == FUNCTION_DECL)
1884 len = 3;
1885 else
1886 len = 2;
1887 i += len;
1888 if (i > 6)
1890 fprintf (stderr, "\n\t");
1891 i = len;
1893 print_node_brief (stderr, "", t, 0);
1894 if (t == error_mark_node)
1895 break;
1897 if (i)
1898 fprintf (stderr, "\n");
1900 if (vec_safe_length (lvl->class_shadowed))
1902 size_t i;
1903 cp_class_binding *b;
1904 fprintf (stderr, " class-shadowed:");
1905 FOR_EACH_VEC_ELT (*lvl->class_shadowed, i, b)
1906 fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
1907 fprintf (stderr, "\n");
1909 if (lvl->type_shadowed)
1911 fprintf (stderr, " type-shadowed:");
1912 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
1914 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1916 fprintf (stderr, "\n");
1920 DEBUG_FUNCTION void
1921 debug (cp_binding_level &ref)
1923 print_binding_level (&ref);
1926 DEBUG_FUNCTION void
1927 debug (cp_binding_level *ptr)
1929 if (ptr)
1930 debug (*ptr);
1931 else
1932 fprintf (stderr, "<nil>\n");
1936 void
1937 print_other_binding_stack (cp_binding_level *stack)
1939 cp_binding_level *level;
1940 for (level = stack; !global_scope_p (level); level = level->level_chain)
1942 fprintf (stderr, "binding level %p\n", (void *) level);
1943 print_binding_level (level);
1947 void
1948 print_binding_stack (void)
1950 cp_binding_level *b;
1951 fprintf (stderr, "current_binding_level=%p\n"
1952 "class_binding_level=%p\n"
1953 "NAMESPACE_LEVEL (global_namespace)=%p\n",
1954 (void *) current_binding_level, (void *) class_binding_level,
1955 (void *) NAMESPACE_LEVEL (global_namespace));
1956 if (class_binding_level)
1958 for (b = class_binding_level; b; b = b->level_chain)
1959 if (b == current_binding_level)
1960 break;
1961 if (b)
1962 b = class_binding_level;
1963 else
1964 b = current_binding_level;
1966 else
1967 b = current_binding_level;
1968 print_other_binding_stack (b);
1969 fprintf (stderr, "global:\n");
1970 print_binding_level (NAMESPACE_LEVEL (global_namespace));
1973 /* Return the type associated with ID. */
1975 static tree
1976 identifier_type_value_1 (tree id)
1978 /* There is no type with that name, anywhere. */
1979 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
1980 return NULL_TREE;
1981 /* This is not the type marker, but the real thing. */
1982 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
1983 return REAL_IDENTIFIER_TYPE_VALUE (id);
1984 /* Have to search for it. It must be on the global level, now.
1985 Ask lookup_name not to return non-types. */
1986 id = lookup_name_real (id, 2, 1, /*block_p=*/true, 0, 0);
1987 if (id)
1988 return TREE_TYPE (id);
1989 return NULL_TREE;
1992 /* Wrapper for identifier_type_value_1. */
1994 tree
1995 identifier_type_value (tree id)
1997 tree ret;
1998 timevar_start (TV_NAME_LOOKUP);
1999 ret = identifier_type_value_1 (id);
2000 timevar_stop (TV_NAME_LOOKUP);
2001 return ret;
2005 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
2006 the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */
2008 tree
2009 identifier_global_value (tree t)
2011 return IDENTIFIER_GLOBAL_VALUE (t);
2014 /* Push a definition of struct, union or enum tag named ID. into
2015 binding_level B. DECL is a TYPE_DECL for the type. We assume that
2016 the tag ID is not already defined. */
2018 static void
2019 set_identifier_type_value_with_scope (tree id, tree decl, cp_binding_level *b)
2021 tree type;
2023 if (b->kind != sk_namespace)
2025 /* Shadow the marker, not the real thing, so that the marker
2026 gets restored later. */
2027 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
2028 b->type_shadowed
2029 = tree_cons (id, old_type_value, b->type_shadowed);
2030 type = decl ? TREE_TYPE (decl) : NULL_TREE;
2031 TREE_TYPE (b->type_shadowed) = type;
2033 else
2035 cxx_binding *binding =
2036 binding_for_name (NAMESPACE_LEVEL (current_namespace), id);
2037 gcc_assert (decl);
2038 if (binding->value)
2039 supplement_binding (binding, decl);
2040 else
2041 binding->value = decl;
2043 /* Store marker instead of real type. */
2044 type = global_type_node;
2046 SET_IDENTIFIER_TYPE_VALUE (id, type);
2049 /* As set_identifier_type_value_with_scope, but using
2050 current_binding_level. */
2052 void
2053 set_identifier_type_value (tree id, tree decl)
2055 set_identifier_type_value_with_scope (id, decl, current_binding_level);
2058 /* Return the name for the constructor (or destructor) for the
2059 specified class TYPE. When given a template, this routine doesn't
2060 lose the specialization. */
2062 static inline tree
2063 constructor_name_full (tree type)
2065 return TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
2068 /* Return the name for the constructor (or destructor) for the
2069 specified class. When given a template, return the plain
2070 unspecialized name. */
2072 tree
2073 constructor_name (tree type)
2075 tree name;
2076 name = constructor_name_full (type);
2077 if (IDENTIFIER_TEMPLATE (name))
2078 name = IDENTIFIER_TEMPLATE (name);
2079 return name;
2082 /* Returns TRUE if NAME is the name for the constructor for TYPE,
2083 which must be a class type. */
2085 bool
2086 constructor_name_p (tree name, tree type)
2088 tree ctor_name;
2090 gcc_assert (MAYBE_CLASS_TYPE_P (type));
2092 if (!name)
2093 return false;
2095 if (!identifier_p (name))
2096 return false;
2098 /* These don't have names. */
2099 if (TREE_CODE (type) == DECLTYPE_TYPE
2100 || TREE_CODE (type) == TYPEOF_TYPE)
2101 return false;
2103 ctor_name = constructor_name_full (type);
2104 if (name == ctor_name)
2105 return true;
2106 if (IDENTIFIER_TEMPLATE (ctor_name)
2107 && name == IDENTIFIER_TEMPLATE (ctor_name))
2108 return true;
2109 return false;
2112 /* Counter used to create anonymous type names. */
2114 static GTY(()) int anon_cnt;
2116 /* Return an IDENTIFIER which can be used as a name for
2117 unnamed structs and unions. */
2119 tree
2120 make_anon_name (void)
2122 char buf[32];
2124 sprintf (buf, anon_aggrname_format (), anon_cnt++);
2125 return get_identifier (buf);
2128 /* This code is practically identical to that for creating
2129 anonymous names, but is just used for lambdas instead. This isn't really
2130 necessary, but it's convenient to avoid treating lambdas like other
2131 unnamed types. */
2133 static GTY(()) int lambda_cnt = 0;
2135 tree
2136 make_lambda_name (void)
2138 char buf[32];
2140 sprintf (buf, LAMBDANAME_FORMAT, lambda_cnt++);
2141 return get_identifier (buf);
2144 /* Return (from the stack of) the BINDING, if any, established at SCOPE. */
2146 static inline cxx_binding *
2147 find_binding (cp_binding_level *scope, cxx_binding *binding)
2149 for (; binding != NULL; binding = binding->previous)
2150 if (binding->scope == scope)
2151 return binding;
2153 return (cxx_binding *)0;
2156 /* Return the binding for NAME in SCOPE, if any. Otherwise, return NULL. */
2158 static inline cxx_binding *
2159 cp_binding_level_find_binding_for_name (cp_binding_level *scope, tree name)
2161 cxx_binding *b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2162 if (b)
2164 /* Fold-in case where NAME is used only once. */
2165 if (scope == b->scope && b->previous == NULL)
2166 return b;
2167 return find_binding (scope, b);
2169 return NULL;
2172 /* Always returns a binding for name in scope. If no binding is
2173 found, make a new one. */
2175 static cxx_binding *
2176 binding_for_name (cp_binding_level *scope, tree name)
2178 cxx_binding *result;
2180 result = cp_binding_level_find_binding_for_name (scope, name);
2181 if (result)
2182 return result;
2183 /* Not found, make a new one. */
2184 result = cxx_binding_make (NULL, NULL);
2185 result->previous = IDENTIFIER_NAMESPACE_BINDINGS (name);
2186 result->scope = scope;
2187 result->is_local = false;
2188 result->value_is_inherited = false;
2189 IDENTIFIER_NAMESPACE_BINDINGS (name) = result;
2190 return result;
2193 /* Walk through the bindings associated to the name of FUNCTION,
2194 and return the first declaration of a function with a
2195 "C" linkage specification, a.k.a 'extern "C"'.
2196 This function looks for the binding, regardless of which scope it
2197 has been defined in. It basically looks in all the known scopes.
2198 Note that this function does not lookup for bindings of builtin functions
2199 or for functions declared in system headers. */
2200 static tree
2201 lookup_extern_c_fun_in_all_ns (tree function)
2203 tree name;
2204 cxx_binding *iter;
2206 gcc_assert (function && TREE_CODE (function) == FUNCTION_DECL);
2208 name = DECL_NAME (function);
2209 gcc_assert (name && identifier_p (name));
2211 for (iter = IDENTIFIER_NAMESPACE_BINDINGS (name);
2212 iter;
2213 iter = iter->previous)
2215 tree ovl;
2216 for (ovl = iter->value; ovl; ovl = OVL_NEXT (ovl))
2218 tree decl = OVL_CURRENT (ovl);
2219 if (decl
2220 && TREE_CODE (decl) == FUNCTION_DECL
2221 && DECL_EXTERN_C_P (decl)
2222 && !DECL_ARTIFICIAL (decl))
2224 return decl;
2228 return NULL;
2231 /* Returns a list of C-linkage decls with the name NAME. */
2233 tree
2234 c_linkage_bindings (tree name)
2236 tree decls = NULL_TREE;
2237 cxx_binding *iter;
2239 for (iter = IDENTIFIER_NAMESPACE_BINDINGS (name);
2240 iter;
2241 iter = iter->previous)
2243 tree ovl;
2244 for (ovl = iter->value; ovl; ovl = OVL_NEXT (ovl))
2246 tree decl = OVL_CURRENT (ovl);
2247 if (decl
2248 && DECL_EXTERN_C_P (decl)
2249 && !DECL_ARTIFICIAL (decl))
2251 if (decls == NULL_TREE)
2252 decls = decl;
2253 else
2254 decls = tree_cons (NULL_TREE, decl, decls);
2258 return decls;
2261 /* Insert another USING_DECL into the current binding level, returning
2262 this declaration. If this is a redeclaration, do nothing, and
2263 return NULL_TREE if this not in namespace scope (in namespace
2264 scope, a using decl might extend any previous bindings). */
2266 static tree
2267 push_using_decl_1 (tree scope, tree name)
2269 tree decl;
2271 gcc_assert (TREE_CODE (scope) == NAMESPACE_DECL);
2272 gcc_assert (identifier_p (name));
2273 for (decl = current_binding_level->usings; decl; decl = DECL_CHAIN (decl))
2274 if (USING_DECL_SCOPE (decl) == scope && DECL_NAME (decl) == name)
2275 break;
2276 if (decl)
2277 return namespace_bindings_p () ? decl : NULL_TREE;
2278 decl = build_lang_decl (USING_DECL, name, NULL_TREE);
2279 USING_DECL_SCOPE (decl) = scope;
2280 DECL_CHAIN (decl) = current_binding_level->usings;
2281 current_binding_level->usings = decl;
2282 return decl;
2285 /* Wrapper for push_using_decl_1. */
2287 static tree
2288 push_using_decl (tree scope, tree name)
2290 tree ret;
2291 timevar_start (TV_NAME_LOOKUP);
2292 ret = push_using_decl_1 (scope, name);
2293 timevar_stop (TV_NAME_LOOKUP);
2294 return ret;
2297 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
2298 caller to set DECL_CONTEXT properly.
2300 Note that this must only be used when X will be the new innermost
2301 binding for its name, as we tack it onto the front of IDENTIFIER_BINDING
2302 without checking to see if the current IDENTIFIER_BINDING comes from a
2303 closer binding level than LEVEL. */
2305 static tree
2306 pushdecl_with_scope_1 (tree x, cp_binding_level *level, bool is_friend)
2308 cp_binding_level *b;
2309 tree function_decl = current_function_decl;
2311 current_function_decl = NULL_TREE;
2312 if (level->kind == sk_class)
2314 b = class_binding_level;
2315 class_binding_level = level;
2316 pushdecl_class_level (x);
2317 class_binding_level = b;
2319 else
2321 b = current_binding_level;
2322 current_binding_level = level;
2323 x = pushdecl_maybe_friend (x, is_friend);
2324 current_binding_level = b;
2326 current_function_decl = function_decl;
2327 return x;
2330 /* Wrapper for pushdecl_with_scope_1. */
2332 tree
2333 pushdecl_with_scope (tree x, cp_binding_level *level, bool is_friend)
2335 tree ret;
2336 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2337 ret = pushdecl_with_scope_1 (x, level, is_friend);
2338 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2339 return ret;
2342 /* Helper function for push_overloaded_decl_1 and do_nonmember_using_decl.
2343 Compares the parameter-type-lists of DECL1 and DECL2 and returns false
2344 if they are different. If the DECLs are template functions, the return
2345 types and the template parameter lists are compared too (DR 565). */
2347 static bool
2348 compparms_for_decl_and_using_decl (tree decl1, tree decl2)
2350 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (decl1)),
2351 TYPE_ARG_TYPES (TREE_TYPE (decl2))))
2352 return false;
2354 if (! DECL_FUNCTION_TEMPLATE_P (decl1)
2355 || ! DECL_FUNCTION_TEMPLATE_P (decl2))
2356 return true;
2358 return (comp_template_parms (DECL_TEMPLATE_PARMS (decl1),
2359 DECL_TEMPLATE_PARMS (decl2))
2360 && same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
2361 TREE_TYPE (TREE_TYPE (decl2))));
2364 /* DECL is a FUNCTION_DECL for a non-member function, which may have
2365 other definitions already in place. We get around this by making
2366 the value of the identifier point to a list of all the things that
2367 want to be referenced by that name. It is then up to the users of
2368 that name to decide what to do with that list.
2370 DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its
2371 DECL_TEMPLATE_RESULT. It is dealt with the same way.
2373 FLAGS is a bitwise-or of the following values:
2374 PUSH_LOCAL: Bind DECL in the current scope, rather than at
2375 namespace scope.
2376 PUSH_USING: DECL is being pushed as the result of a using
2377 declaration.
2379 IS_FRIEND is true if this is a friend declaration.
2381 The value returned may be a previous declaration if we guessed wrong
2382 about what language DECL should belong to (C or C++). Otherwise,
2383 it's always DECL (and never something that's not a _DECL). */
2385 static tree
2386 push_overloaded_decl_1 (tree decl, int flags, bool is_friend)
2388 tree name = DECL_NAME (decl);
2389 tree old;
2390 tree new_binding;
2391 int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
2393 if (doing_global)
2394 old = namespace_binding (name, DECL_CONTEXT (decl));
2395 else
2396 old = lookup_name_innermost_nonclass_level (name);
2398 if (old)
2400 if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
2402 tree t = TREE_TYPE (old);
2403 if (MAYBE_CLASS_TYPE_P (t) && warn_shadow
2404 && (! DECL_IN_SYSTEM_HEADER (decl)
2405 || ! DECL_IN_SYSTEM_HEADER (old)))
2406 warning (OPT_Wshadow, "%q#D hides constructor for %q#T", decl, t);
2407 old = NULL_TREE;
2409 else if (is_overloaded_fn (old))
2411 tree tmp;
2413 for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
2415 tree fn = OVL_CURRENT (tmp);
2416 tree dup;
2418 if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp)
2419 && !(flags & PUSH_USING)
2420 && compparms_for_decl_and_using_decl (fn, decl)
2421 && ! decls_match (fn, decl))
2422 diagnose_name_conflict (decl, fn);
2424 dup = duplicate_decls (decl, fn, is_friend);
2425 /* If DECL was a redeclaration of FN -- even an invalid
2426 one -- pass that information along to our caller. */
2427 if (dup == fn || dup == error_mark_node)
2428 return dup;
2431 /* We don't overload implicit built-ins. duplicate_decls()
2432 may fail to merge the decls if the new decl is e.g. a
2433 template function. */
2434 if (TREE_CODE (old) == FUNCTION_DECL
2435 && DECL_ANTICIPATED (old)
2436 && !DECL_HIDDEN_FRIEND_P (old))
2437 old = NULL;
2439 else if (old == error_mark_node)
2440 /* Ignore the undefined symbol marker. */
2441 old = NULL_TREE;
2442 else
2444 error ("previous non-function declaration %q+#D", old);
2445 error ("conflicts with function declaration %q#D", decl);
2446 return decl;
2450 if (old || TREE_CODE (decl) == TEMPLATE_DECL
2451 /* If it's a using declaration, we always need to build an OVERLOAD,
2452 because it's the only way to remember that the declaration comes
2453 from 'using', and have the lookup behave correctly. */
2454 || (flags & PUSH_USING))
2456 if (old && TREE_CODE (old) != OVERLOAD)
2457 /* Wrap the existing single decl in an overload. */
2458 new_binding = ovl_cons (old, NULL_TREE);
2459 else
2460 new_binding = old;
2461 new_binding = ovl_cons (decl, new_binding);
2462 if (flags & PUSH_USING)
2463 OVL_USED (new_binding) = 1;
2465 else
2466 /* NAME is not ambiguous. */
2467 new_binding = decl;
2469 if (doing_global)
2470 set_namespace_binding (name, current_namespace, new_binding);
2471 else
2473 /* We only create an OVERLOAD if there was a previous binding at
2474 this level, or if decl is a template. In the former case, we
2475 need to remove the old binding and replace it with the new
2476 binding. We must also run through the NAMES on the binding
2477 level where the name was bound to update the chain. */
2479 if (TREE_CODE (new_binding) == OVERLOAD && old)
2481 tree *d;
2483 for (d = &IDENTIFIER_BINDING (name)->scope->names;
2485 d = &TREE_CHAIN (*d))
2486 if (*d == old
2487 || (TREE_CODE (*d) == TREE_LIST
2488 && TREE_VALUE (*d) == old))
2490 if (TREE_CODE (*d) == TREE_LIST)
2491 /* Just replace the old binding with the new. */
2492 TREE_VALUE (*d) = new_binding;
2493 else
2494 /* Build a TREE_LIST to wrap the OVERLOAD. */
2495 *d = tree_cons (NULL_TREE, new_binding,
2496 TREE_CHAIN (*d));
2498 /* And update the cxx_binding node. */
2499 IDENTIFIER_BINDING (name)->value = new_binding;
2500 return decl;
2503 /* We should always find a previous binding in this case. */
2504 gcc_unreachable ();
2507 /* Install the new binding. */
2508 push_local_binding (name, new_binding, flags);
2511 return decl;
2514 /* Wrapper for push_overloaded_decl_1. */
2516 static tree
2517 push_overloaded_decl (tree decl, int flags, bool is_friend)
2519 tree ret;
2520 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2521 ret = push_overloaded_decl_1 (decl, flags, is_friend);
2522 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2523 return ret;
2526 /* Check a non-member using-declaration. Return the name and scope
2527 being used, and the USING_DECL, or NULL_TREE on failure. */
2529 static tree
2530 validate_nonmember_using_decl (tree decl, tree scope, tree name)
2532 /* [namespace.udecl]
2533 A using-declaration for a class member shall be a
2534 member-declaration. */
2535 if (TYPE_P (scope))
2537 error ("%qT is not a namespace or unscoped enum", scope);
2538 return NULL_TREE;
2540 else if (scope == error_mark_node)
2541 return NULL_TREE;
2543 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR)
2545 /* 7.3.3/5
2546 A using-declaration shall not name a template-id. */
2547 error ("a using-declaration cannot specify a template-id. "
2548 "Try %<using %D%>", name);
2549 return NULL_TREE;
2552 if (TREE_CODE (decl) == NAMESPACE_DECL)
2554 error ("namespace %qD not allowed in using-declaration", decl);
2555 return NULL_TREE;
2558 if (TREE_CODE (decl) == SCOPE_REF)
2560 /* It's a nested name with template parameter dependent scope.
2561 This can only be using-declaration for class member. */
2562 error ("%qT is not a namespace", TREE_OPERAND (decl, 0));
2563 return NULL_TREE;
2566 if (is_overloaded_fn (decl))
2567 decl = get_first_fn (decl);
2569 gcc_assert (DECL_P (decl));
2571 /* Make a USING_DECL. */
2572 tree using_decl = push_using_decl (scope, name);
2574 if (using_decl == NULL_TREE
2575 && at_function_scope_p ()
2576 && VAR_P (decl))
2577 /* C++11 7.3.3/10. */
2578 error ("%qD is already declared in this scope", name);
2580 return using_decl;
2583 /* Process local and global using-declarations. */
2585 static void
2586 do_nonmember_using_decl (tree scope, tree name, tree oldval, tree oldtype,
2587 tree *newval, tree *newtype)
2589 struct scope_binding decls = EMPTY_SCOPE_BINDING;
2591 *newval = *newtype = NULL_TREE;
2592 if (!qualified_lookup_using_namespace (name, scope, &decls, 0))
2593 /* Lookup error */
2594 return;
2596 if (!decls.value && !decls.type)
2598 error ("%qD not declared", name);
2599 return;
2602 /* Shift the old and new bindings around so we're comparing class and
2603 enumeration names to each other. */
2604 if (oldval && DECL_IMPLICIT_TYPEDEF_P (oldval))
2606 oldtype = oldval;
2607 oldval = NULL_TREE;
2610 if (decls.value && DECL_IMPLICIT_TYPEDEF_P (decls.value))
2612 decls.type = decls.value;
2613 decls.value = NULL_TREE;
2616 if (decls.value)
2618 /* Check for using functions. */
2619 if (is_overloaded_fn (decls.value))
2621 tree tmp, tmp1;
2623 if (oldval && !is_overloaded_fn (oldval))
2625 error ("%qD is already declared in this scope", name);
2626 oldval = NULL_TREE;
2629 *newval = oldval;
2630 for (tmp = decls.value; tmp; tmp = OVL_NEXT (tmp))
2632 tree new_fn = OVL_CURRENT (tmp);
2634 /* Don't import functions that haven't been declared. */
2635 if (DECL_ANTICIPATED (new_fn))
2636 continue;
2638 /* [namespace.udecl]
2640 If a function declaration in namespace scope or block
2641 scope has the same name and the same parameter types as a
2642 function introduced by a using declaration the program is
2643 ill-formed. */
2644 for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1))
2646 tree old_fn = OVL_CURRENT (tmp1);
2648 if (new_fn == old_fn)
2649 /* The function already exists in the current namespace. */
2650 break;
2651 else if (TREE_CODE (tmp1) == OVERLOAD && OVL_USED (tmp1))
2652 continue; /* this is a using decl */
2653 else if (compparms_for_decl_and_using_decl (new_fn, old_fn))
2655 /* There was already a non-using declaration in
2656 this scope with the same parameter types. If both
2657 are the same extern "C" functions, that's ok. */
2658 if (DECL_ANTICIPATED (old_fn)
2659 && !DECL_HIDDEN_FRIEND_P (old_fn))
2660 /* Ignore anticipated built-ins. */;
2661 else if (decls_match (new_fn, old_fn))
2662 break;
2663 else
2665 diagnose_name_conflict (new_fn, old_fn);
2666 break;
2671 /* If we broke out of the loop, there's no reason to add
2672 this function to the using declarations for this
2673 scope. */
2674 if (tmp1)
2675 continue;
2677 /* If we are adding to an existing OVERLOAD, then we no
2678 longer know the type of the set of functions. */
2679 if (*newval && TREE_CODE (*newval) == OVERLOAD)
2680 TREE_TYPE (*newval) = unknown_type_node;
2681 /* Add this new function to the set. */
2682 *newval = build_overload (OVL_CURRENT (tmp), *newval);
2683 /* If there is only one function, then we use its type. (A
2684 using-declaration naming a single function can be used in
2685 contexts where overload resolution cannot be
2686 performed.) */
2687 if (TREE_CODE (*newval) != OVERLOAD)
2689 *newval = ovl_cons (*newval, NULL_TREE);
2690 TREE_TYPE (*newval) = TREE_TYPE (OVL_CURRENT (tmp));
2692 OVL_USED (*newval) = 1;
2695 else
2697 /* If we're declaring a non-function and OLDVAL is an anticipated
2698 built-in, just pretend it isn't there. */
2699 if (oldval
2700 && TREE_CODE (oldval) == FUNCTION_DECL
2701 && DECL_ANTICIPATED (oldval)
2702 && !DECL_HIDDEN_FRIEND_P (oldval))
2703 oldval = NULL_TREE;
2705 *newval = decls.value;
2706 if (oldval && !decls_match (*newval, oldval))
2707 error ("%qD is already declared in this scope", name);
2710 else
2711 *newval = oldval;
2713 if (decls.type && TREE_CODE (decls.type) == TREE_LIST)
2715 error ("reference to %qD is ambiguous", name);
2716 print_candidates (decls.type);
2718 else
2720 *newtype = decls.type;
2721 if (oldtype && *newtype && !decls_match (oldtype, *newtype))
2722 error ("%qD is already declared in this scope", name);
2725 /* If *newval is empty, shift any class or enumeration name down. */
2726 if (!*newval)
2728 *newval = *newtype;
2729 *newtype = NULL_TREE;
2733 /* Process a using-declaration at function scope. */
2735 void
2736 do_local_using_decl (tree decl, tree scope, tree name)
2738 tree oldval, oldtype, newval, newtype;
2739 tree orig_decl = decl;
2741 decl = validate_nonmember_using_decl (decl, scope, name);
2742 if (decl == NULL_TREE)
2743 return;
2745 if (building_stmt_list_p ()
2746 && at_function_scope_p ())
2747 add_decl_expr (decl);
2749 oldval = lookup_name_innermost_nonclass_level (name);
2750 oldtype = lookup_type_current_level (name);
2752 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
2754 if (newval)
2756 if (is_overloaded_fn (newval))
2758 tree fn, term;
2760 /* We only need to push declarations for those functions
2761 that were not already bound in the current level.
2762 The old value might be NULL_TREE, it might be a single
2763 function, or an OVERLOAD. */
2764 if (oldval && TREE_CODE (oldval) == OVERLOAD)
2765 term = OVL_FUNCTION (oldval);
2766 else
2767 term = oldval;
2768 for (fn = newval; fn && OVL_CURRENT (fn) != term;
2769 fn = OVL_NEXT (fn))
2770 push_overloaded_decl (OVL_CURRENT (fn),
2771 PUSH_LOCAL | PUSH_USING,
2772 false);
2774 else
2775 push_local_binding (name, newval, PUSH_USING);
2777 if (newtype)
2779 push_local_binding (name, newtype, PUSH_USING);
2780 set_identifier_type_value (name, newtype);
2783 /* Emit debug info. */
2784 if (!processing_template_decl)
2785 cp_emit_debug_info_for_using (orig_decl, current_scope());
2788 /* Returns true if ROOT (a namespace, class, or function) encloses
2789 CHILD. CHILD may be either a class type or a namespace. */
2791 bool
2792 is_ancestor (tree root, tree child)
2794 gcc_assert ((TREE_CODE (root) == NAMESPACE_DECL
2795 || TREE_CODE (root) == FUNCTION_DECL
2796 || CLASS_TYPE_P (root)));
2797 gcc_assert ((TREE_CODE (child) == NAMESPACE_DECL
2798 || CLASS_TYPE_P (child)));
2800 /* The global namespace encloses everything. */
2801 if (root == global_namespace)
2802 return true;
2804 while (true)
2806 /* If we've run out of scopes, stop. */
2807 if (!child)
2808 return false;
2809 /* If we've reached the ROOT, it encloses CHILD. */
2810 if (root == child)
2811 return true;
2812 /* Go out one level. */
2813 if (TYPE_P (child))
2814 child = TYPE_NAME (child);
2815 child = DECL_CONTEXT (child);
2819 /* Enter the class or namespace scope indicated by T suitable for name
2820 lookup. T can be arbitrary scope, not necessary nested inside the
2821 current scope. Returns a non-null scope to pop iff pop_scope
2822 should be called later to exit this scope. */
2824 tree
2825 push_scope (tree t)
2827 if (TREE_CODE (t) == NAMESPACE_DECL)
2828 push_decl_namespace (t);
2829 else if (CLASS_TYPE_P (t))
2831 if (!at_class_scope_p ()
2832 || !same_type_p (current_class_type, t))
2833 push_nested_class (t);
2834 else
2835 /* T is the same as the current scope. There is therefore no
2836 need to re-enter the scope. Since we are not actually
2837 pushing a new scope, our caller should not call
2838 pop_scope. */
2839 t = NULL_TREE;
2842 return t;
2845 /* Leave scope pushed by push_scope. */
2847 void
2848 pop_scope (tree t)
2850 if (t == NULL_TREE)
2851 return;
2852 if (TREE_CODE (t) == NAMESPACE_DECL)
2853 pop_decl_namespace ();
2854 else if CLASS_TYPE_P (t)
2855 pop_nested_class ();
2858 /* Subroutine of push_inner_scope. */
2860 static void
2861 push_inner_scope_r (tree outer, tree inner)
2863 tree prev;
2865 if (outer == inner
2866 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
2867 return;
2869 prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
2870 if (outer != prev)
2871 push_inner_scope_r (outer, prev);
2872 if (TREE_CODE (inner) == NAMESPACE_DECL)
2874 cp_binding_level *save_template_parm = 0;
2875 /* Temporary take out template parameter scopes. They are saved
2876 in reversed order in save_template_parm. */
2877 while (current_binding_level->kind == sk_template_parms)
2879 cp_binding_level *b = current_binding_level;
2880 current_binding_level = b->level_chain;
2881 b->level_chain = save_template_parm;
2882 save_template_parm = b;
2885 resume_scope (NAMESPACE_LEVEL (inner));
2886 current_namespace = inner;
2888 /* Restore template parameter scopes. */
2889 while (save_template_parm)
2891 cp_binding_level *b = save_template_parm;
2892 save_template_parm = b->level_chain;
2893 b->level_chain = current_binding_level;
2894 current_binding_level = b;
2897 else
2898 pushclass (inner);
2901 /* Enter the scope INNER from current scope. INNER must be a scope
2902 nested inside current scope. This works with both name lookup and
2903 pushing name into scope. In case a template parameter scope is present,
2904 namespace is pushed under the template parameter scope according to
2905 name lookup rule in 14.6.1/6.
2907 Return the former current scope suitable for pop_inner_scope. */
2909 tree
2910 push_inner_scope (tree inner)
2912 tree outer = current_scope ();
2913 if (!outer)
2914 outer = current_namespace;
2916 push_inner_scope_r (outer, inner);
2917 return outer;
2920 /* Exit the current scope INNER back to scope OUTER. */
2922 void
2923 pop_inner_scope (tree outer, tree inner)
2925 if (outer == inner
2926 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
2927 return;
2929 while (outer != inner)
2931 if (TREE_CODE (inner) == NAMESPACE_DECL)
2933 cp_binding_level *save_template_parm = 0;
2934 /* Temporary take out template parameter scopes. They are saved
2935 in reversed order in save_template_parm. */
2936 while (current_binding_level->kind == sk_template_parms)
2938 cp_binding_level *b = current_binding_level;
2939 current_binding_level = b->level_chain;
2940 b->level_chain = save_template_parm;
2941 save_template_parm = b;
2944 pop_namespace ();
2946 /* Restore template parameter scopes. */
2947 while (save_template_parm)
2949 cp_binding_level *b = save_template_parm;
2950 save_template_parm = b->level_chain;
2951 b->level_chain = current_binding_level;
2952 current_binding_level = b;
2955 else
2956 popclass ();
2958 inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
2962 /* Do a pushlevel for class declarations. */
2964 void
2965 pushlevel_class (void)
2967 class_binding_level = begin_scope (sk_class, current_class_type);
2970 /* ...and a poplevel for class declarations. */
2972 void
2973 poplevel_class (void)
2975 cp_binding_level *level = class_binding_level;
2976 cp_class_binding *cb;
2977 size_t i;
2978 tree shadowed;
2980 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2981 gcc_assert (level != 0);
2983 /* If we're leaving a toplevel class, cache its binding level. */
2984 if (current_class_depth == 1)
2985 previous_class_level = level;
2986 for (shadowed = level->type_shadowed;
2987 shadowed;
2988 shadowed = TREE_CHAIN (shadowed))
2989 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
2991 /* Remove the bindings for all of the class-level declarations. */
2992 if (level->class_shadowed)
2994 FOR_EACH_VEC_ELT (*level->class_shadowed, i, cb)
2996 IDENTIFIER_BINDING (cb->identifier) = cb->base->previous;
2997 cxx_binding_free (cb->base);
2999 ggc_free (level->class_shadowed);
3000 level->class_shadowed = NULL;
3003 /* Now, pop out of the binding level which we created up in the
3004 `pushlevel_class' routine. */
3005 gcc_assert (current_binding_level == level);
3006 leave_scope ();
3007 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3010 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
3011 appropriate. DECL is the value to which a name has just been
3012 bound. CLASS_TYPE is the class in which the lookup occurred. */
3014 static void
3015 set_inherited_value_binding_p (cxx_binding *binding, tree decl,
3016 tree class_type)
3018 if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
3020 tree context;
3022 if (TREE_CODE (decl) == OVERLOAD)
3023 context = ovl_scope (decl);
3024 else
3026 gcc_assert (DECL_P (decl));
3027 context = context_for_name_lookup (decl);
3030 if (is_properly_derived_from (class_type, context))
3031 INHERITED_VALUE_BINDING_P (binding) = 1;
3032 else
3033 INHERITED_VALUE_BINDING_P (binding) = 0;
3035 else if (binding->value == decl)
3036 /* We only encounter a TREE_LIST when there is an ambiguity in the
3037 base classes. Such an ambiguity can be overridden by a
3038 definition in this class. */
3039 INHERITED_VALUE_BINDING_P (binding) = 1;
3040 else
3041 INHERITED_VALUE_BINDING_P (binding) = 0;
3044 /* Make the declaration of X appear in CLASS scope. */
3046 bool
3047 pushdecl_class_level (tree x)
3049 tree name;
3050 bool is_valid = true;
3051 bool subtime;
3053 /* Do nothing if we're adding to an outer lambda closure type,
3054 outer_binding will add it later if it's needed. */
3055 if (current_class_type != class_binding_level->this_entity)
3056 return true;
3058 subtime = timevar_cond_start (TV_NAME_LOOKUP);
3059 /* Get the name of X. */
3060 if (TREE_CODE (x) == OVERLOAD)
3061 name = DECL_NAME (get_first_fn (x));
3062 else
3063 name = DECL_NAME (x);
3065 if (name)
3067 is_valid = push_class_level_binding (name, x);
3068 if (TREE_CODE (x) == TYPE_DECL)
3069 set_identifier_type_value (name, x);
3071 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3073 /* If X is an anonymous aggregate, all of its members are
3074 treated as if they were members of the class containing the
3075 aggregate, for naming purposes. */
3076 tree f;
3078 for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = DECL_CHAIN (f))
3080 location_t save_location = input_location;
3081 input_location = DECL_SOURCE_LOCATION (f);
3082 if (!pushdecl_class_level (f))
3083 is_valid = false;
3084 input_location = save_location;
3087 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3088 return is_valid;
3091 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
3092 scope. If the value returned is non-NULL, and the PREVIOUS field
3093 is not set, callers must set the PREVIOUS field explicitly. */
3095 static cxx_binding *
3096 get_class_binding (tree name, cp_binding_level *scope)
3098 tree class_type;
3099 tree type_binding;
3100 tree value_binding;
3101 cxx_binding *binding;
3103 class_type = scope->this_entity;
3105 /* Get the type binding. */
3106 type_binding = lookup_member (class_type, name,
3107 /*protect=*/2, /*want_type=*/true,
3108 tf_warning_or_error);
3109 /* Get the value binding. */
3110 value_binding = lookup_member (class_type, name,
3111 /*protect=*/2, /*want_type=*/false,
3112 tf_warning_or_error);
3114 if (value_binding
3115 && (TREE_CODE (value_binding) == TYPE_DECL
3116 || DECL_CLASS_TEMPLATE_P (value_binding)
3117 || (TREE_CODE (value_binding) == TREE_LIST
3118 && TREE_TYPE (value_binding) == error_mark_node
3119 && (TREE_CODE (TREE_VALUE (value_binding))
3120 == TYPE_DECL))))
3121 /* We found a type binding, even when looking for a non-type
3122 binding. This means that we already processed this binding
3123 above. */
3125 else if (value_binding)
3127 if (TREE_CODE (value_binding) == TREE_LIST
3128 && TREE_TYPE (value_binding) == error_mark_node)
3129 /* NAME is ambiguous. */
3131 else if (BASELINK_P (value_binding))
3132 /* NAME is some overloaded functions. */
3133 value_binding = BASELINK_FUNCTIONS (value_binding);
3136 /* If we found either a type binding or a value binding, create a
3137 new binding object. */
3138 if (type_binding || value_binding)
3140 binding = new_class_binding (name,
3141 value_binding,
3142 type_binding,
3143 scope);
3144 /* This is a class-scope binding, not a block-scope binding. */
3145 LOCAL_BINDING_P (binding) = 0;
3146 set_inherited_value_binding_p (binding, value_binding, class_type);
3148 else
3149 binding = NULL;
3151 return binding;
3154 /* Make the declaration(s) of X appear in CLASS scope under the name
3155 NAME. Returns true if the binding is valid. */
3157 static bool
3158 push_class_level_binding_1 (tree name, tree x)
3160 cxx_binding *binding;
3161 tree decl = x;
3162 bool ok;
3164 /* The class_binding_level will be NULL if x is a template
3165 parameter name in a member template. */
3166 if (!class_binding_level)
3167 return true;
3169 if (name == error_mark_node)
3170 return false;
3172 /* Can happen for an erroneous declaration (c++/60384). */
3173 if (!identifier_p (name))
3175 gcc_assert (errorcount || sorrycount);
3176 return false;
3179 /* Check for invalid member names. But don't worry about a default
3180 argument-scope lambda being pushed after the class is complete. */
3181 gcc_assert (TYPE_BEING_DEFINED (current_class_type)
3182 || LAMBDA_TYPE_P (TREE_TYPE (decl)));
3183 /* Check that we're pushing into the right binding level. */
3184 gcc_assert (current_class_type == class_binding_level->this_entity);
3186 /* We could have been passed a tree list if this is an ambiguous
3187 declaration. If so, pull the declaration out because
3188 check_template_shadow will not handle a TREE_LIST. */
3189 if (TREE_CODE (decl) == TREE_LIST
3190 && TREE_TYPE (decl) == error_mark_node)
3191 decl = TREE_VALUE (decl);
3193 if (!check_template_shadow (decl))
3194 return false;
3196 /* [class.mem]
3198 If T is the name of a class, then each of the following shall
3199 have a name different from T:
3201 -- every static data member of class T;
3203 -- every member of class T that is itself a type;
3205 -- every enumerator of every member of class T that is an
3206 enumerated type;
3208 -- every member of every anonymous union that is a member of
3209 class T.
3211 (Non-static data members were also forbidden to have the same
3212 name as T until TC1.) */
3213 if ((VAR_P (x)
3214 || TREE_CODE (x) == CONST_DECL
3215 || (TREE_CODE (x) == TYPE_DECL
3216 && !DECL_SELF_REFERENCE_P (x))
3217 /* A data member of an anonymous union. */
3218 || (TREE_CODE (x) == FIELD_DECL
3219 && DECL_CONTEXT (x) != current_class_type))
3220 && DECL_NAME (x) == constructor_name (current_class_type))
3222 tree scope = context_for_name_lookup (x);
3223 if (TYPE_P (scope) && same_type_p (scope, current_class_type))
3225 error ("%qD has the same name as the class in which it is "
3226 "declared",
3228 return false;
3232 /* Get the current binding for NAME in this class, if any. */
3233 binding = IDENTIFIER_BINDING (name);
3234 if (!binding || binding->scope != class_binding_level)
3236 binding = get_class_binding (name, class_binding_level);
3237 /* If a new binding was created, put it at the front of the
3238 IDENTIFIER_BINDING list. */
3239 if (binding)
3241 binding->previous = IDENTIFIER_BINDING (name);
3242 IDENTIFIER_BINDING (name) = binding;
3246 /* If there is already a binding, then we may need to update the
3247 current value. */
3248 if (binding && binding->value)
3250 tree bval = binding->value;
3251 tree old_decl = NULL_TREE;
3252 tree target_decl = strip_using_decl (decl);
3253 tree target_bval = strip_using_decl (bval);
3255 if (INHERITED_VALUE_BINDING_P (binding))
3257 /* If the old binding was from a base class, and was for a
3258 tag name, slide it over to make room for the new binding.
3259 The old binding is still visible if explicitly qualified
3260 with a class-key. */
3261 if (TREE_CODE (target_bval) == TYPE_DECL
3262 && DECL_ARTIFICIAL (target_bval)
3263 && !(TREE_CODE (target_decl) == TYPE_DECL
3264 && DECL_ARTIFICIAL (target_decl)))
3266 old_decl = binding->type;
3267 binding->type = bval;
3268 binding->value = NULL_TREE;
3269 INHERITED_VALUE_BINDING_P (binding) = 0;
3271 else
3273 old_decl = bval;
3274 /* Any inherited type declaration is hidden by the type
3275 declaration in the derived class. */
3276 if (TREE_CODE (target_decl) == TYPE_DECL
3277 && DECL_ARTIFICIAL (target_decl))
3278 binding->type = NULL_TREE;
3281 else if (TREE_CODE (target_decl) == OVERLOAD
3282 && is_overloaded_fn (target_bval))
3283 old_decl = bval;
3284 else if (TREE_CODE (decl) == USING_DECL
3285 && TREE_CODE (bval) == USING_DECL
3286 && same_type_p (USING_DECL_SCOPE (decl),
3287 USING_DECL_SCOPE (bval)))
3288 /* This is a using redeclaration that will be diagnosed later
3289 in supplement_binding */
3291 else if (TREE_CODE (decl) == USING_DECL
3292 && TREE_CODE (bval) == USING_DECL
3293 && DECL_DEPENDENT_P (decl)
3294 && DECL_DEPENDENT_P (bval))
3295 return true;
3296 else if (TREE_CODE (decl) == USING_DECL
3297 && is_overloaded_fn (target_bval))
3298 old_decl = bval;
3299 else if (TREE_CODE (bval) == USING_DECL
3300 && is_overloaded_fn (target_decl))
3301 return true;
3303 if (old_decl && binding->scope == class_binding_level)
3305 binding->value = x;
3306 /* It is always safe to clear INHERITED_VALUE_BINDING_P
3307 here. This function is only used to register bindings
3308 from with the class definition itself. */
3309 INHERITED_VALUE_BINDING_P (binding) = 0;
3310 return true;
3314 /* Note that we declared this value so that we can issue an error if
3315 this is an invalid redeclaration of a name already used for some
3316 other purpose. */
3317 note_name_declared_in_class (name, decl);
3319 /* If we didn't replace an existing binding, put the binding on the
3320 stack of bindings for the identifier, and update the shadowed
3321 list. */
3322 if (binding && binding->scope == class_binding_level)
3323 /* Supplement the existing binding. */
3324 ok = supplement_binding (binding, decl);
3325 else
3327 /* Create a new binding. */
3328 push_binding (name, decl, class_binding_level);
3329 ok = true;
3332 return ok;
3335 /* Wrapper for push_class_level_binding_1. */
3337 bool
3338 push_class_level_binding (tree name, tree x)
3340 bool ret;
3341 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3342 ret = push_class_level_binding_1 (name, x);
3343 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3344 return ret;
3347 /* Process "using SCOPE::NAME" in a class scope. Return the
3348 USING_DECL created. */
3350 tree
3351 do_class_using_decl (tree scope, tree name)
3353 /* The USING_DECL returned by this function. */
3354 tree value;
3355 /* The declaration (or declarations) name by this using
3356 declaration. NULL if we are in a template and cannot figure out
3357 what has been named. */
3358 tree decl;
3359 /* True if SCOPE is a dependent type. */
3360 bool scope_dependent_p;
3361 /* True if SCOPE::NAME is dependent. */
3362 bool name_dependent_p;
3363 /* True if any of the bases of CURRENT_CLASS_TYPE are dependent. */
3364 bool bases_dependent_p;
3365 tree binfo;
3367 if (name == error_mark_node)
3368 return NULL_TREE;
3370 if (!scope || !TYPE_P (scope))
3372 error ("using-declaration for non-member at class scope");
3373 return NULL_TREE;
3376 /* Make sure the name is not invalid */
3377 if (TREE_CODE (name) == BIT_NOT_EXPR)
3379 error ("%<%T::%D%> names destructor", scope, name);
3380 return NULL_TREE;
3382 /* Using T::T declares inheriting ctors, even if T is a typedef. */
3383 if (MAYBE_CLASS_TYPE_P (scope)
3384 && (name == TYPE_IDENTIFIER (scope)
3385 || constructor_name_p (name, scope)))
3387 maybe_warn_cpp0x (CPP0X_INHERITING_CTORS);
3388 name = ctor_identifier;
3389 CLASSTYPE_NON_AGGREGATE (current_class_type) = true;
3391 if (constructor_name_p (name, current_class_type))
3393 error ("%<%T::%D%> names constructor in %qT",
3394 scope, name, current_class_type);
3395 return NULL_TREE;
3398 scope_dependent_p = dependent_scope_p (scope);
3399 name_dependent_p = (scope_dependent_p
3400 || (IDENTIFIER_TYPENAME_P (name)
3401 && dependent_type_p (TREE_TYPE (name))));
3403 bases_dependent_p = any_dependent_bases_p ();
3405 decl = NULL_TREE;
3407 /* From [namespace.udecl]:
3409 A using-declaration used as a member-declaration shall refer to a
3410 member of a base class of the class being defined.
3412 In general, we cannot check this constraint in a template because
3413 we do not know the entire set of base classes of the current
3414 class type. Morover, if SCOPE is dependent, it might match a
3415 non-dependent base. */
3417 if (!scope_dependent_p)
3419 base_kind b_kind;
3420 binfo = lookup_base (current_class_type, scope, ba_any, &b_kind,
3421 tf_warning_or_error);
3422 if (b_kind < bk_proper_base)
3424 if (!bases_dependent_p || b_kind == bk_same_type)
3426 error_not_base_type (scope, current_class_type);
3427 return NULL_TREE;
3430 else if (name == ctor_identifier
3431 && BINFO_INHERITANCE_CHAIN (BINFO_INHERITANCE_CHAIN (binfo)))
3433 error ("cannot inherit constructors from indirect base %qT", scope);
3434 return NULL_TREE;
3436 else if (!name_dependent_p)
3438 decl = lookup_member (binfo, name, 0, false, tf_warning_or_error);
3439 if (!decl)
3441 error ("no members matching %<%T::%D%> in %q#T", scope, name,
3442 scope);
3443 return NULL_TREE;
3445 /* The binfo from which the functions came does not matter. */
3446 if (BASELINK_P (decl))
3447 decl = BASELINK_FUNCTIONS (decl);
3451 value = build_lang_decl (USING_DECL, name, NULL_TREE);
3452 USING_DECL_DECLS (value) = decl;
3453 USING_DECL_SCOPE (value) = scope;
3454 DECL_DEPENDENT_P (value) = !decl;
3456 return value;
3460 /* Return the binding value for name in scope. */
3463 static tree
3464 namespace_binding_1 (tree name, tree scope)
3466 cxx_binding *binding;
3468 if (SCOPE_FILE_SCOPE_P (scope))
3469 scope = global_namespace;
3470 else
3471 /* Unnecessary for the global namespace because it can't be an alias. */
3472 scope = ORIGINAL_NAMESPACE (scope);
3474 binding = cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
3476 return binding ? binding->value : NULL_TREE;
3479 tree
3480 namespace_binding (tree name, tree scope)
3482 tree ret;
3483 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3484 ret = namespace_binding_1 (name, scope);
3485 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3486 return ret;
3489 /* Set the binding value for name in scope. */
3491 static void
3492 set_namespace_binding_1 (tree name, tree scope, tree val)
3494 cxx_binding *b;
3496 if (scope == NULL_TREE)
3497 scope = global_namespace;
3498 b = binding_for_name (NAMESPACE_LEVEL (scope), name);
3499 if (!b->value || TREE_CODE (val) == OVERLOAD || val == error_mark_node)
3500 b->value = val;
3501 else
3502 supplement_binding (b, val);
3505 /* Wrapper for set_namespace_binding_1. */
3507 void
3508 set_namespace_binding (tree name, tree scope, tree val)
3510 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3511 set_namespace_binding_1 (name, scope, val);
3512 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3515 /* Set the context of a declaration to scope. Complain if we are not
3516 outside scope. */
3518 void
3519 set_decl_namespace (tree decl, tree scope, bool friendp)
3521 tree old;
3523 /* Get rid of namespace aliases. */
3524 scope = ORIGINAL_NAMESPACE (scope);
3526 /* It is ok for friends to be qualified in parallel space. */
3527 if (!friendp && !is_ancestor (current_namespace, scope))
3528 error ("declaration of %qD not in a namespace surrounding %qD",
3529 decl, scope);
3530 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3532 /* Writing "int N::i" to declare a variable within "N" is invalid. */
3533 if (scope == current_namespace)
3535 if (at_namespace_scope_p ())
3536 error ("explicit qualification in declaration of %qD",
3537 decl);
3538 return;
3541 /* See whether this has been declared in the namespace. */
3542 old = lookup_qualified_name (scope, DECL_NAME (decl), /*type*/false,
3543 /*complain*/true, /*hidden*/true);
3544 if (old == error_mark_node)
3545 /* No old declaration at all. */
3546 goto complain;
3547 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
3548 if (TREE_CODE (old) == TREE_LIST)
3550 error ("reference to %qD is ambiguous", decl);
3551 print_candidates (old);
3552 return;
3554 if (!is_overloaded_fn (decl))
3556 /* We might have found OLD in an inline namespace inside SCOPE. */
3557 if (TREE_CODE (decl) == TREE_CODE (old))
3558 DECL_CONTEXT (decl) = DECL_CONTEXT (old);
3559 /* Don't compare non-function decls with decls_match here, since
3560 it can't check for the correct constness at this
3561 point. pushdecl will find those errors later. */
3562 return;
3564 /* Since decl is a function, old should contain a function decl. */
3565 if (!is_overloaded_fn (old))
3566 goto complain;
3567 /* We handle these in check_explicit_instantiation_namespace. */
3568 if (processing_explicit_instantiation)
3569 return;
3570 if (processing_template_decl || processing_specialization)
3571 /* We have not yet called push_template_decl to turn a
3572 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
3573 match. But, we'll check later, when we construct the
3574 template. */
3575 return;
3576 /* Instantiations or specializations of templates may be declared as
3577 friends in any namespace. */
3578 if (friendp && DECL_USE_TEMPLATE (decl))
3579 return;
3580 if (is_overloaded_fn (old))
3582 tree found = NULL_TREE;
3583 tree elt = old;
3584 for (; elt; elt = OVL_NEXT (elt))
3586 tree ofn = OVL_CURRENT (elt);
3587 /* Adjust DECL_CONTEXT first so decls_match will return true
3588 if DECL will match a declaration in an inline namespace. */
3589 DECL_CONTEXT (decl) = DECL_CONTEXT (ofn);
3590 if (decls_match (decl, ofn))
3592 if (found && !decls_match (found, ofn))
3594 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3595 error ("reference to %qD is ambiguous", decl);
3596 print_candidates (old);
3597 return;
3599 found = ofn;
3602 if (found)
3604 if (!is_associated_namespace (scope, CP_DECL_CONTEXT (found)))
3605 goto complain;
3606 if (DECL_HIDDEN_FRIEND_P (found))
3608 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
3609 "%qD has not been declared within %D", decl, scope);
3610 inform (DECL_SOURCE_LOCATION (found), "only here as a friend");
3612 DECL_CONTEXT (decl) = DECL_CONTEXT (found);
3613 return;
3616 else
3618 DECL_CONTEXT (decl) = DECL_CONTEXT (old);
3619 if (decls_match (decl, old))
3620 return;
3623 /* It didn't work, go back to the explicit scope. */
3624 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3625 complain:
3626 error ("%qD should have been declared inside %qD", decl, scope);
3629 /* Return the namespace where the current declaration is declared. */
3631 tree
3632 current_decl_namespace (void)
3634 tree result;
3635 /* If we have been pushed into a different namespace, use it. */
3636 if (!vec_safe_is_empty (decl_namespace_list))
3637 return decl_namespace_list->last ();
3639 if (current_class_type)
3640 result = decl_namespace_context (current_class_type);
3641 else if (current_function_decl)
3642 result = decl_namespace_context (current_function_decl);
3643 else
3644 result = current_namespace;
3645 return result;
3648 /* Process any ATTRIBUTES on a namespace definition. Returns true if
3649 attribute visibility is seen. */
3651 bool
3652 handle_namespace_attrs (tree ns, tree attributes)
3654 tree d;
3655 bool saw_vis = false;
3657 for (d = attributes; d; d = TREE_CHAIN (d))
3659 tree name = get_attribute_name (d);
3660 tree args = TREE_VALUE (d);
3662 if (is_attribute_p ("visibility", name))
3664 /* attribute visibility is a property of the syntactic block
3665 rather than the namespace as a whole, so we don't touch the
3666 NAMESPACE_DECL at all. */
3667 tree x = args ? TREE_VALUE (args) : NULL_TREE;
3668 if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args))
3670 warning (OPT_Wattributes,
3671 "%qD attribute requires a single NTBS argument",
3672 name);
3673 continue;
3676 if (!TREE_PUBLIC (ns))
3677 warning (OPT_Wattributes,
3678 "%qD attribute is meaningless since members of the "
3679 "anonymous namespace get local symbols", name);
3681 push_visibility (TREE_STRING_POINTER (x), 1);
3682 saw_vis = true;
3684 else if (is_attribute_p ("abi_tag", name))
3686 if (!DECL_NAMESPACE_ASSOCIATIONS (ns))
3688 warning (OPT_Wattributes, "ignoring %qD attribute on non-inline "
3689 "namespace", name);
3690 continue;
3692 if (!DECL_NAME (ns))
3694 warning (OPT_Wattributes, "ignoring %qD attribute on anonymous "
3695 "namespace", name);
3696 continue;
3698 if (!args)
3700 tree dn = DECL_NAME (ns);
3701 args = build_string (IDENTIFIER_LENGTH (dn) + 1,
3702 IDENTIFIER_POINTER (dn));
3703 TREE_TYPE (args) = char_array_type_node;
3704 args = fix_string_type (args);
3705 args = build_tree_list (NULL_TREE, args);
3707 if (check_abi_tag_args (args, name))
3708 DECL_ATTRIBUTES (ns) = tree_cons (name, args,
3709 DECL_ATTRIBUTES (ns));
3711 else
3713 warning (OPT_Wattributes, "%qD attribute directive ignored",
3714 name);
3715 continue;
3719 return saw_vis;
3722 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we
3723 select a name that is unique to this compilation unit. Returns FALSE if
3724 pushdecl fails, TRUE otherwise. */
3726 bool
3727 push_namespace (tree name)
3729 tree d = NULL_TREE;
3730 bool need_new = true;
3731 bool implicit_use = false;
3732 bool anon = !name;
3734 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3736 /* We should not get here if the global_namespace is not yet constructed
3737 nor if NAME designates the global namespace: The global scope is
3738 constructed elsewhere. */
3739 gcc_assert (global_namespace != NULL && name != global_scope_name);
3741 if (anon)
3743 name = get_anonymous_namespace_name();
3744 d = IDENTIFIER_NAMESPACE_VALUE (name);
3745 if (d)
3746 /* Reopening anonymous namespace. */
3747 need_new = false;
3748 implicit_use = true;
3750 else
3752 /* Check whether this is an extended namespace definition. */
3753 d = IDENTIFIER_NAMESPACE_VALUE (name);
3754 if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
3756 tree dna = DECL_NAMESPACE_ALIAS (d);
3757 if (dna)
3759 /* We do some error recovery for, eg, the redeclaration
3760 of M here:
3762 namespace N {}
3763 namespace M = N;
3764 namespace M {}
3766 However, in nasty cases like:
3768 namespace N
3770 namespace M = N;
3771 namespace M {}
3774 we just error out below, in duplicate_decls. */
3775 if (NAMESPACE_LEVEL (dna)->level_chain
3776 == current_binding_level)
3778 error ("namespace alias %qD not allowed here, "
3779 "assuming %qD", d, dna);
3780 d = dna;
3781 need_new = false;
3784 else
3785 need_new = false;
3789 if (need_new)
3791 /* Make a new namespace, binding the name to it. */
3792 d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
3793 DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace);
3794 /* The name of this namespace is not visible to other translation
3795 units if it is an anonymous namespace or member thereof. */
3796 if (anon || decl_anon_ns_mem_p (current_namespace))
3797 TREE_PUBLIC (d) = 0;
3798 else
3799 TREE_PUBLIC (d) = 1;
3800 if (pushdecl (d) == error_mark_node)
3802 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3803 return false;
3805 if (anon)
3807 /* Clear DECL_NAME for the benefit of debugging back ends. */
3808 SET_DECL_ASSEMBLER_NAME (d, name);
3809 DECL_NAME (d) = NULL_TREE;
3811 begin_scope (sk_namespace, d);
3813 else
3814 resume_scope (NAMESPACE_LEVEL (d));
3816 if (implicit_use)
3817 do_using_directive (d);
3818 /* Enter the name space. */
3819 current_namespace = d;
3821 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3822 return true;
3825 /* Pop from the scope of the current namespace. */
3827 void
3828 pop_namespace (void)
3830 gcc_assert (current_namespace != global_namespace);
3831 current_namespace = CP_DECL_CONTEXT (current_namespace);
3832 /* The binding level is not popped, as it might be re-opened later. */
3833 leave_scope ();
3836 /* Push into the scope of the namespace NS, even if it is deeply
3837 nested within another namespace. */
3839 void
3840 push_nested_namespace (tree ns)
3842 if (ns == global_namespace)
3843 push_to_top_level ();
3844 else
3846 push_nested_namespace (CP_DECL_CONTEXT (ns));
3847 push_namespace (DECL_NAME (ns));
3851 /* Pop back from the scope of the namespace NS, which was previously
3852 entered with push_nested_namespace. */
3854 void
3855 pop_nested_namespace (tree ns)
3857 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3858 gcc_assert (current_namespace == ns);
3859 while (ns != global_namespace)
3861 pop_namespace ();
3862 ns = CP_DECL_CONTEXT (ns);
3865 pop_from_top_level ();
3866 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3869 /* Temporarily set the namespace for the current declaration. */
3871 void
3872 push_decl_namespace (tree decl)
3874 if (TREE_CODE (decl) != NAMESPACE_DECL)
3875 decl = decl_namespace_context (decl);
3876 vec_safe_push (decl_namespace_list, ORIGINAL_NAMESPACE (decl));
3879 /* [namespace.memdef]/2 */
3881 void
3882 pop_decl_namespace (void)
3884 decl_namespace_list->pop ();
3887 /* Return the namespace that is the common ancestor
3888 of two given namespaces. */
3890 static tree
3891 namespace_ancestor_1 (tree ns1, tree ns2)
3893 tree nsr;
3894 if (is_ancestor (ns1, ns2))
3895 nsr = ns1;
3896 else
3897 nsr = namespace_ancestor_1 (CP_DECL_CONTEXT (ns1), ns2);
3898 return nsr;
3901 /* Wrapper for namespace_ancestor_1. */
3903 static tree
3904 namespace_ancestor (tree ns1, tree ns2)
3906 tree nsr;
3907 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3908 nsr = namespace_ancestor_1 (ns1, ns2);
3909 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3910 return nsr;
3913 /* Process a namespace-alias declaration. */
3915 void
3916 do_namespace_alias (tree alias, tree name_space)
3918 if (name_space == error_mark_node)
3919 return;
3921 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
3923 name_space = ORIGINAL_NAMESPACE (name_space);
3925 /* Build the alias. */
3926 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
3927 DECL_NAMESPACE_ALIAS (alias) = name_space;
3928 DECL_EXTERNAL (alias) = 1;
3929 DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ());
3930 pushdecl (alias);
3932 /* Emit debug info for namespace alias. */
3933 if (!building_stmt_list_p ())
3934 (*debug_hooks->early_global_decl) (alias);
3937 /* Like pushdecl, only it places X in the current namespace,
3938 if appropriate. */
3940 tree
3941 pushdecl_namespace_level (tree x, bool is_friend)
3943 cp_binding_level *b = current_binding_level;
3944 tree t;
3946 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3947 t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace), is_friend);
3949 /* Now, the type_shadowed stack may screw us. Munge it so it does
3950 what we want. */
3951 if (TREE_CODE (t) == TYPE_DECL)
3953 tree name = DECL_NAME (t);
3954 tree newval;
3955 tree *ptr = (tree *)0;
3956 for (; !global_scope_p (b); b = b->level_chain)
3958 tree shadowed = b->type_shadowed;
3959 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
3960 if (TREE_PURPOSE (shadowed) == name)
3962 ptr = &TREE_VALUE (shadowed);
3963 /* Can't break out of the loop here because sometimes
3964 a binding level will have duplicate bindings for
3965 PT names. It's gross, but I haven't time to fix it. */
3968 newval = TREE_TYPE (t);
3969 if (ptr == (tree *)0)
3971 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
3972 up here if this is changed to an assertion. --KR */
3973 SET_IDENTIFIER_TYPE_VALUE (name, t);
3975 else
3977 *ptr = newval;
3980 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3981 return t;
3984 /* Insert USED into the using list of USER. Set INDIRECT_flag if this
3985 directive is not directly from the source. Also find the common
3986 ancestor and let our users know about the new namespace */
3988 static void
3989 add_using_namespace_1 (tree user, tree used, bool indirect)
3991 tree t;
3992 /* Using oneself is a no-op. */
3993 if (user == used)
3994 return;
3995 gcc_assert (TREE_CODE (user) == NAMESPACE_DECL);
3996 gcc_assert (TREE_CODE (used) == NAMESPACE_DECL);
3997 /* Check if we already have this. */
3998 t = purpose_member (used, DECL_NAMESPACE_USING (user));
3999 if (t != NULL_TREE)
4001 if (!indirect)
4002 /* Promote to direct usage. */
4003 TREE_INDIRECT_USING (t) = 0;
4004 return;
4007 /* Add used to the user's using list. */
4008 DECL_NAMESPACE_USING (user)
4009 = tree_cons (used, namespace_ancestor (user, used),
4010 DECL_NAMESPACE_USING (user));
4012 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect;
4014 /* Add user to the used's users list. */
4015 DECL_NAMESPACE_USERS (used)
4016 = tree_cons (user, 0, DECL_NAMESPACE_USERS (used));
4018 /* Recursively add all namespaces used. */
4019 for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t))
4020 /* indirect usage */
4021 add_using_namespace_1 (user, TREE_PURPOSE (t), 1);
4023 /* Tell everyone using us about the new used namespaces. */
4024 for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t))
4025 add_using_namespace_1 (TREE_PURPOSE (t), used, 1);
4028 /* Wrapper for add_using_namespace_1. */
4030 static void
4031 add_using_namespace (tree user, tree used, bool indirect)
4033 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4034 add_using_namespace_1 (user, used, indirect);
4035 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4038 /* Process a using-declaration not appearing in class or local scope. */
4040 void
4041 do_toplevel_using_decl (tree decl, tree scope, tree name)
4043 tree oldval, oldtype, newval, newtype;
4044 tree orig_decl = decl;
4045 cxx_binding *binding;
4047 decl = validate_nonmember_using_decl (decl, scope, name);
4048 if (decl == NULL_TREE)
4049 return;
4051 binding = binding_for_name (NAMESPACE_LEVEL (current_namespace), name);
4053 oldval = binding->value;
4054 oldtype = binding->type;
4056 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
4058 /* Emit debug info. */
4059 if (!processing_template_decl)
4060 cp_emit_debug_info_for_using (orig_decl, current_namespace);
4062 /* Copy declarations found. */
4063 if (newval)
4064 binding->value = newval;
4065 if (newtype)
4066 binding->type = newtype;
4069 /* Process a using-directive. */
4071 void
4072 do_using_directive (tree name_space)
4074 tree context = NULL_TREE;
4076 if (name_space == error_mark_node)
4077 return;
4079 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
4081 if (building_stmt_list_p ())
4082 add_stmt (build_stmt (input_location, USING_STMT, name_space));
4083 name_space = ORIGINAL_NAMESPACE (name_space);
4085 if (!toplevel_bindings_p ())
4087 push_using_directive (name_space);
4089 else
4091 /* direct usage */
4092 add_using_namespace (current_namespace, name_space, 0);
4093 if (current_namespace != global_namespace)
4094 context = current_namespace;
4096 /* Emit debugging info. */
4097 if (!processing_template_decl)
4098 (*debug_hooks->imported_module_or_decl) (name_space, NULL_TREE,
4099 context, false);
4103 /* Deal with a using-directive seen by the parser. Currently we only
4104 handle attributes here, since they cannot appear inside a template. */
4106 void
4107 parse_using_directive (tree name_space, tree attribs)
4109 do_using_directive (name_space);
4111 if (attribs == error_mark_node)
4112 return;
4114 for (tree a = attribs; a; a = TREE_CHAIN (a))
4116 tree name = get_attribute_name (a);
4117 if (is_attribute_p ("strong", name))
4119 if (!toplevel_bindings_p ())
4120 error ("strong using only meaningful at namespace scope");
4121 else if (name_space != error_mark_node)
4123 if (!is_ancestor (current_namespace, name_space))
4124 error ("current namespace %qD does not enclose strongly used namespace %qD",
4125 current_namespace, name_space);
4126 DECL_NAMESPACE_ASSOCIATIONS (name_space)
4127 = tree_cons (current_namespace, 0,
4128 DECL_NAMESPACE_ASSOCIATIONS (name_space));
4131 else
4132 warning (OPT_Wattributes, "%qD attribute directive ignored", name);
4136 /* Like pushdecl, only it places X in the global scope if appropriate.
4137 Calls cp_finish_decl to register the variable, initializing it with
4138 *INIT, if INIT is non-NULL. */
4140 static tree
4141 pushdecl_top_level_1 (tree x, tree *init, bool is_friend)
4143 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4144 push_to_top_level ();
4145 x = pushdecl_namespace_level (x, is_friend);
4146 if (init)
4147 cp_finish_decl (x, *init, false, NULL_TREE, 0);
4148 pop_from_top_level ();
4149 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4150 return x;
4153 /* Like pushdecl, only it places X in the global scope if appropriate. */
4155 tree
4156 pushdecl_top_level (tree x)
4158 return pushdecl_top_level_1 (x, NULL, false);
4161 /* Like pushdecl_top_level, but adding the IS_FRIEND parameter. */
4163 tree
4164 pushdecl_top_level_maybe_friend (tree x, bool is_friend)
4166 return pushdecl_top_level_1 (x, NULL, is_friend);
4169 /* Like pushdecl, only it places X in the global scope if
4170 appropriate. Calls cp_finish_decl to register the variable,
4171 initializing it with INIT. */
4173 tree
4174 pushdecl_top_level_and_finish (tree x, tree init)
4176 return pushdecl_top_level_1 (x, &init, false);
4179 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
4180 duplicates. The first list becomes the tail of the result.
4182 The algorithm is O(n^2). We could get this down to O(n log n) by
4183 doing a sort on the addresses of the functions, if that becomes
4184 necessary. */
4186 static tree
4187 merge_functions (tree s1, tree s2)
4189 for (; s2; s2 = OVL_NEXT (s2))
4191 tree fn2 = OVL_CURRENT (s2);
4192 tree fns1;
4194 for (fns1 = s1; fns1; fns1 = OVL_NEXT (fns1))
4196 tree fn1 = OVL_CURRENT (fns1);
4198 /* If the function from S2 is already in S1, there is no
4199 need to add it again. For `extern "C"' functions, we
4200 might have two FUNCTION_DECLs for the same function, in
4201 different namespaces, but let's leave them in case
4202 they have different default arguments. */
4203 if (fn1 == fn2)
4204 break;
4207 /* If we exhausted all of the functions in S1, FN2 is new. */
4208 if (!fns1)
4209 s1 = build_overload (fn2, s1);
4211 return s1;
4214 /* Returns TRUE iff OLD and NEW are the same entity.
4216 3 [basic]/3: An entity is a value, object, reference, function,
4217 enumerator, type, class member, template, template specialization,
4218 namespace, parameter pack, or this.
4220 7.3.4 [namespace.udir]/4: If name lookup finds a declaration for a name
4221 in two different namespaces, and the declarations do not declare the
4222 same entity and do not declare functions, the use of the name is
4223 ill-formed. */
4225 static bool
4226 same_entity_p (tree one, tree two)
4228 if (one == two)
4229 return true;
4230 if (!one || !two)
4231 return false;
4232 if (TREE_CODE (one) == TYPE_DECL
4233 && TREE_CODE (two) == TYPE_DECL
4234 && same_type_p (TREE_TYPE (one), TREE_TYPE (two)))
4235 return true;
4236 return false;
4239 /* This should return an error not all definitions define functions.
4240 It is not an error if we find two functions with exactly the
4241 same signature, only if these are selected in overload resolution.
4242 old is the current set of bindings, new_binding the freshly-found binding.
4243 XXX Do we want to give *all* candidates in case of ambiguity?
4244 XXX In what way should I treat extern declarations?
4245 XXX I don't want to repeat the entire duplicate_decls here */
4247 static void
4248 ambiguous_decl (struct scope_binding *old, cxx_binding *new_binding, int flags)
4250 tree val, type;
4251 gcc_assert (old != NULL);
4253 /* Copy the type. */
4254 type = new_binding->type;
4255 if (LOOKUP_NAMESPACES_ONLY (flags)
4256 || (type && hidden_name_p (type) && !(flags & LOOKUP_HIDDEN)))
4257 type = NULL_TREE;
4259 /* Copy the value. */
4260 val = new_binding->value;
4261 if (val)
4263 if (!(flags & LOOKUP_HIDDEN))
4264 val = remove_hidden_names (val);
4265 if (val)
4266 switch (TREE_CODE (val))
4268 case TEMPLATE_DECL:
4269 /* If we expect types or namespaces, and not templates,
4270 or this is not a template class. */
4271 if ((LOOKUP_QUALIFIERS_ONLY (flags)
4272 && !DECL_TYPE_TEMPLATE_P (val)))
4273 val = NULL_TREE;
4274 break;
4275 case TYPE_DECL:
4276 if (LOOKUP_NAMESPACES_ONLY (flags)
4277 || (type && (flags & LOOKUP_PREFER_TYPES)))
4278 val = NULL_TREE;
4279 break;
4280 case NAMESPACE_DECL:
4281 if (LOOKUP_TYPES_ONLY (flags))
4282 val = NULL_TREE;
4283 break;
4284 case FUNCTION_DECL:
4285 /* Ignore built-in functions that are still anticipated. */
4286 if (LOOKUP_QUALIFIERS_ONLY (flags))
4287 val = NULL_TREE;
4288 break;
4289 default:
4290 if (LOOKUP_QUALIFIERS_ONLY (flags))
4291 val = NULL_TREE;
4295 /* If val is hidden, shift down any class or enumeration name. */
4296 if (!val)
4298 val = type;
4299 type = NULL_TREE;
4302 if (!old->value)
4303 old->value = val;
4304 else if (val && !same_entity_p (val, old->value))
4306 if (is_overloaded_fn (old->value) && is_overloaded_fn (val))
4307 old->value = merge_functions (old->value, val);
4308 else
4310 old->value = tree_cons (NULL_TREE, old->value,
4311 build_tree_list (NULL_TREE, val));
4312 TREE_TYPE (old->value) = error_mark_node;
4316 if (!old->type)
4317 old->type = type;
4318 else if (type && old->type != type)
4320 old->type = tree_cons (NULL_TREE, old->type,
4321 build_tree_list (NULL_TREE, type));
4322 TREE_TYPE (old->type) = error_mark_node;
4326 /* Return the declarations that are members of the namespace NS. */
4328 tree
4329 cp_namespace_decls (tree ns)
4331 return NAMESPACE_LEVEL (ns)->names;
4334 /* Combine prefer_type and namespaces_only into flags. */
4336 static int
4337 lookup_flags (int prefer_type, int namespaces_only)
4339 if (namespaces_only)
4340 return LOOKUP_PREFER_NAMESPACES;
4341 if (prefer_type > 1)
4342 return LOOKUP_PREFER_TYPES;
4343 if (prefer_type > 0)
4344 return LOOKUP_PREFER_BOTH;
4345 return 0;
4348 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
4349 ignore it or not. Subroutine of lookup_name_real and
4350 lookup_type_scope. */
4352 static bool
4353 qualify_lookup (tree val, int flags)
4355 if (val == NULL_TREE)
4356 return false;
4357 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
4358 return true;
4359 if (flags & LOOKUP_PREFER_TYPES)
4361 tree target_val = strip_using_decl (val);
4362 if (TREE_CODE (target_val) == TYPE_DECL
4363 || TREE_CODE (target_val) == TEMPLATE_DECL)
4364 return true;
4366 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
4367 return false;
4368 /* Look through lambda things that we shouldn't be able to see. */
4369 if (is_lambda_ignored_entity (val))
4370 return false;
4371 return true;
4374 /* Given a lookup that returned VAL, decide if we want to ignore it or
4375 not based on DECL_ANTICIPATED. */
4377 bool
4378 hidden_name_p (tree val)
4380 if (DECL_P (val)
4381 && DECL_LANG_SPECIFIC (val)
4382 && TYPE_FUNCTION_OR_TEMPLATE_DECL_P (val)
4383 && DECL_ANTICIPATED (val))
4384 return true;
4385 if (TREE_CODE (val) == OVERLOAD)
4387 for (tree o = val; o; o = OVL_CHAIN (o))
4388 if (!hidden_name_p (OVL_FUNCTION (o)))
4389 return false;
4390 return true;
4392 return false;
4395 /* Remove any hidden declarations from a possibly overloaded set
4396 of functions. */
4398 tree
4399 remove_hidden_names (tree fns)
4401 if (!fns)
4402 return fns;
4404 if (DECL_P (fns) && hidden_name_p (fns))
4405 fns = NULL_TREE;
4406 else if (TREE_CODE (fns) == OVERLOAD)
4408 tree o;
4410 for (o = fns; o; o = OVL_NEXT (o))
4411 if (hidden_name_p (OVL_CURRENT (o)))
4412 break;
4413 if (o)
4415 tree n = NULL_TREE;
4417 for (o = fns; o; o = OVL_NEXT (o))
4418 if (!hidden_name_p (OVL_CURRENT (o)))
4419 n = build_overload (OVL_CURRENT (o), n);
4420 fns = n;
4424 return fns;
4427 /* Suggest alternatives for NAME, an IDENTIFIER_NODE for which name
4428 lookup failed. Search through all available namespaces and print out
4429 possible candidates. */
4431 void
4432 suggest_alternatives_for (location_t location, tree name)
4434 vec<tree> candidates = vNULL;
4435 vec<tree> namespaces_to_search = vNULL;
4436 int max_to_search = PARAM_VALUE (CXX_MAX_NAMESPACES_FOR_DIAGNOSTIC_HELP);
4437 int n_searched = 0;
4438 tree t;
4439 unsigned ix;
4441 namespaces_to_search.safe_push (global_namespace);
4443 while (!namespaces_to_search.is_empty ()
4444 && n_searched < max_to_search)
4446 tree scope = namespaces_to_search.pop ();
4447 struct scope_binding binding = EMPTY_SCOPE_BINDING;
4448 cp_binding_level *level = NAMESPACE_LEVEL (scope);
4450 /* Look in this namespace. */
4451 qualified_lookup_using_namespace (name, scope, &binding, 0);
4453 n_searched++;
4455 if (binding.value)
4456 candidates.safe_push (binding.value);
4458 /* Add child namespaces. */
4459 for (t = level->namespaces; t; t = DECL_CHAIN (t))
4460 namespaces_to_search.safe_push (t);
4463 /* If we stopped before we could examine all namespaces, inform the
4464 user. Do this even if we don't have any candidates, since there
4465 might be more candidates further down that we weren't able to
4466 find. */
4467 if (n_searched >= max_to_search
4468 && !namespaces_to_search.is_empty ())
4469 inform (location,
4470 "maximum limit of %d namespaces searched for %qE",
4471 max_to_search, name);
4473 namespaces_to_search.release ();
4475 /* Nothing useful to report for NAME. Report on likely misspellings,
4476 or do nothing. */
4477 if (candidates.is_empty ())
4479 const char *fuzzy_name = lookup_name_fuzzy (name, FUZZY_LOOKUP_NAME);
4480 if (fuzzy_name)
4482 gcc_rich_location richloc (location);
4483 richloc.add_fixit_replace (fuzzy_name);
4484 inform_at_rich_loc (&richloc, "suggested alternative: %qs",
4485 fuzzy_name);
4487 return;
4490 inform_n (location, candidates.length (),
4491 "suggested alternative:",
4492 "suggested alternatives:");
4494 FOR_EACH_VEC_ELT (candidates, ix, t)
4495 inform (location_of (t), " %qE", t);
4497 candidates.release ();
4500 /* Unscoped lookup of a global: iterate over current namespaces,
4501 considering using-directives. */
4503 static tree
4504 unqualified_namespace_lookup_1 (tree name, int flags)
4506 tree initial = current_decl_namespace ();
4507 tree scope = initial;
4508 tree siter;
4509 cp_binding_level *level;
4510 tree val = NULL_TREE;
4512 for (; !val; scope = CP_DECL_CONTEXT (scope))
4514 struct scope_binding binding = EMPTY_SCOPE_BINDING;
4515 cxx_binding *b =
4516 cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
4518 if (b)
4519 ambiguous_decl (&binding, b, flags);
4521 /* Add all _DECLs seen through local using-directives. */
4522 for (level = current_binding_level;
4523 level->kind != sk_namespace;
4524 level = level->level_chain)
4525 if (!lookup_using_namespace (name, &binding, level->using_directives,
4526 scope, flags))
4527 /* Give up because of error. */
4528 return error_mark_node;
4530 /* Add all _DECLs seen through global using-directives. */
4531 /* XXX local and global using lists should work equally. */
4532 siter = initial;
4533 while (1)
4535 if (!lookup_using_namespace (name, &binding,
4536 DECL_NAMESPACE_USING (siter),
4537 scope, flags))
4538 /* Give up because of error. */
4539 return error_mark_node;
4540 if (siter == scope) break;
4541 siter = CP_DECL_CONTEXT (siter);
4544 val = binding.value;
4545 if (scope == global_namespace)
4546 break;
4548 return val;
4551 /* Wrapper for unqualified_namespace_lookup_1. */
4553 static tree
4554 unqualified_namespace_lookup (tree name, int flags)
4556 tree ret;
4557 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4558 ret = unqualified_namespace_lookup_1 (name, flags);
4559 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4560 return ret;
4563 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
4564 or a class TYPE).
4566 If PREFER_TYPE is > 0, we only return TYPE_DECLs or namespaces.
4567 If PREFER_TYPE is > 1, we only return TYPE_DECLs.
4569 Returns a DECL (or OVERLOAD, or BASELINK) representing the
4570 declaration found. If no suitable declaration can be found,
4571 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
4572 neither a class-type nor a namespace a diagnostic is issued. */
4574 tree
4575 lookup_qualified_name (tree scope, tree name, int prefer_type, bool complain,
4576 bool find_hidden)
4578 tree t = NULL_TREE;
4580 if (TREE_CODE (scope) == NAMESPACE_DECL)
4582 struct scope_binding binding = EMPTY_SCOPE_BINDING;
4584 int flags = lookup_flags (prefer_type, /*namespaces_only*/false);
4585 if (find_hidden)
4586 flags |= LOOKUP_HIDDEN;
4587 if (qualified_lookup_using_namespace (name, scope, &binding, flags))
4588 t = binding.value;
4590 else if (cxx_dialect != cxx98 && TREE_CODE (scope) == ENUMERAL_TYPE)
4591 t = lookup_enumerator (scope, name);
4592 else if (is_class_type (scope, complain))
4593 t = lookup_member (scope, name, 2, prefer_type, tf_warning_or_error);
4595 if (!t)
4596 return error_mark_node;
4597 return t;
4600 /* Subroutine of unqualified_namespace_lookup:
4601 Add the bindings of NAME in used namespaces to VAL.
4602 We are currently looking for names in namespace SCOPE, so we
4603 look through USINGS for using-directives of namespaces
4604 which have SCOPE as a common ancestor with the current scope.
4605 Returns false on errors. */
4607 static bool
4608 lookup_using_namespace (tree name, struct scope_binding *val,
4609 tree usings, tree scope, int flags)
4611 tree iter;
4612 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4613 /* Iterate over all used namespaces in current, searching for using
4614 directives of scope. */
4615 for (iter = usings; iter; iter = TREE_CHAIN (iter))
4616 if (TREE_VALUE (iter) == scope)
4618 tree used = ORIGINAL_NAMESPACE (TREE_PURPOSE (iter));
4619 cxx_binding *val1 =
4620 cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (used), name);
4621 /* Resolve ambiguities. */
4622 if (val1)
4623 ambiguous_decl (val, val1, flags);
4625 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4626 return val->value != error_mark_node;
4629 /* Returns true iff VEC contains TARGET. */
4631 static bool
4632 tree_vec_contains (vec<tree, va_gc> *vec, tree target)
4634 unsigned int i;
4635 tree elt;
4636 FOR_EACH_VEC_SAFE_ELT (vec,i,elt)
4637 if (elt == target)
4638 return true;
4639 return false;
4642 /* [namespace.qual]
4643 Accepts the NAME to lookup and its qualifying SCOPE.
4644 Returns the name/type pair found into the cxx_binding *RESULT,
4645 or false on error. */
4647 static bool
4648 qualified_lookup_using_namespace (tree name, tree scope,
4649 struct scope_binding *result, int flags)
4651 /* Maintain a list of namespaces visited... */
4652 vec<tree, va_gc> *seen = NULL;
4653 vec<tree, va_gc> *seen_inline = NULL;
4654 /* ... and a list of namespace yet to see. */
4655 vec<tree, va_gc> *todo = NULL;
4656 vec<tree, va_gc> *todo_maybe = NULL;
4657 vec<tree, va_gc> *todo_inline = NULL;
4658 tree usings;
4659 timevar_start (TV_NAME_LOOKUP);
4660 /* Look through namespace aliases. */
4661 scope = ORIGINAL_NAMESPACE (scope);
4663 /* Algorithm: Starting with SCOPE, walk through the set of used
4664 namespaces. For each used namespace, look through its inline
4665 namespace set for any bindings and usings. If no bindings are
4666 found, add any usings seen to the set of used namespaces. */
4667 vec_safe_push (todo, scope);
4669 while (todo->length ())
4671 bool found_here;
4672 scope = todo->pop ();
4673 if (tree_vec_contains (seen, scope))
4674 continue;
4675 vec_safe_push (seen, scope);
4676 vec_safe_push (todo_inline, scope);
4678 found_here = false;
4679 while (todo_inline->length ())
4681 cxx_binding *binding;
4683 scope = todo_inline->pop ();
4684 if (tree_vec_contains (seen_inline, scope))
4685 continue;
4686 vec_safe_push (seen_inline, scope);
4688 binding =
4689 cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
4690 if (binding)
4692 ambiguous_decl (result, binding, flags);
4693 if (result->type || result->value)
4694 found_here = true;
4697 for (usings = DECL_NAMESPACE_USING (scope); usings;
4698 usings = TREE_CHAIN (usings))
4699 if (!TREE_INDIRECT_USING (usings))
4701 if (is_associated_namespace (scope, TREE_PURPOSE (usings)))
4702 vec_safe_push (todo_inline, TREE_PURPOSE (usings));
4703 else
4704 vec_safe_push (todo_maybe, TREE_PURPOSE (usings));
4708 if (found_here)
4709 vec_safe_truncate (todo_maybe, 0);
4710 else
4711 while (vec_safe_length (todo_maybe))
4712 vec_safe_push (todo, todo_maybe->pop ());
4714 vec_free (todo);
4715 vec_free (todo_maybe);
4716 vec_free (todo_inline);
4717 vec_free (seen);
4718 vec_free (seen_inline);
4719 timevar_stop (TV_NAME_LOOKUP);
4720 return result->value != error_mark_node;
4723 /* Helper function for lookup_name_fuzzy.
4724 Traverse binding level LVL, looking for good name matches for NAME
4725 (and BM). */
4726 static void
4727 consider_binding_level (tree name, best_match <tree, tree> &bm,
4728 cp_binding_level *lvl, bool look_within_fields,
4729 enum lookup_name_fuzzy_kind kind)
4731 if (look_within_fields)
4732 if (lvl->this_entity && TREE_CODE (lvl->this_entity) == RECORD_TYPE)
4734 tree type = lvl->this_entity;
4735 bool want_type_p = (kind == FUZZY_LOOKUP_TYPENAME);
4736 tree best_matching_field
4737 = lookup_member_fuzzy (type, name, want_type_p);
4738 if (best_matching_field)
4739 bm.consider (best_matching_field);
4742 for (tree t = lvl->names; t; t = TREE_CHAIN (t))
4744 tree d = t;
4746 /* OVERLOADs or decls from using declaration are wrapped into
4747 TREE_LIST. */
4748 if (TREE_CODE (d) == TREE_LIST)
4750 d = TREE_VALUE (d);
4751 d = OVL_CURRENT (d);
4754 /* Don't use bindings from implicitly declared functions,
4755 as they were likely misspellings themselves. */
4756 if (TREE_TYPE (d) == error_mark_node)
4757 continue;
4759 /* Skip anticipated decls of builtin functions. */
4760 if (TREE_CODE (d) == FUNCTION_DECL
4761 && DECL_BUILT_IN (d)
4762 && DECL_ANTICIPATED (d))
4763 continue;
4765 if (tree name = DECL_NAME (d))
4766 /* Ignore internal names with spaces in them. */
4767 if (!strchr (IDENTIFIER_POINTER (name), ' '))
4768 bm.consider (DECL_NAME (d));
4772 /* Search for near-matches for NAME within the current bindings, and within
4773 macro names, returning the best match as a const char *, or NULL if
4774 no reasonable match is found. */
4776 const char *
4777 lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind)
4779 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
4781 best_match <tree, tree> bm (name);
4783 cp_binding_level *lvl;
4784 for (lvl = scope_chain->class_bindings; lvl; lvl = lvl->level_chain)
4785 consider_binding_level (name, bm, lvl, true, kind);
4787 for (lvl = current_binding_level; lvl; lvl = lvl->level_chain)
4788 consider_binding_level (name, bm, lvl, false, kind);
4790 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
4792 x = SOME_OTHER_MACRO (y);
4793 then "SOME_OTHER_MACRO" will survive to the frontend and show up
4794 as a misspelled identifier.
4796 Use the best distance so far so that a candidate is only set if
4797 a macro is better than anything so far. This allows early rejection
4798 (without calculating the edit distance) of macro names that must have
4799 distance >= bm.get_best_distance (), and means that we only get a
4800 non-NULL result for best_macro_match if it's better than any of
4801 the identifiers already checked. */
4802 best_macro_match bmm (name, bm.get_best_distance (), parse_in);
4803 cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
4804 /* If a macro is the closest so far to NAME, suggest it. */
4805 if (best_macro)
4806 return (const char *)best_macro->ident.str;
4808 /* Try the "starts_decl_specifier_p" keywords to detect
4809 "singed" vs "signed" typos. */
4810 for (unsigned i = 0; i < num_c_common_reswords; i++)
4812 const c_common_resword *resword = &c_common_reswords[i];
4814 if (!cp_keyword_starts_decl_specifier_p (resword->rid))
4815 continue;
4817 tree resword_identifier = ridpointers [resword->rid];
4818 if (!resword_identifier)
4819 continue;
4820 gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
4822 /* Only consider reserved words that survived the
4823 filtering in init_reswords (e.g. for -std). */
4824 if (!C_IS_RESERVED_WORD (resword_identifier))
4825 continue;
4827 bm.consider (resword_identifier);
4830 /* See if we have a good suggesion for the user. */
4831 tree best_id = bm.get_best_meaningful_candidate ();
4832 if (best_id)
4833 return IDENTIFIER_POINTER (best_id);
4835 /* No meaningful suggestion available. */
4836 return NULL;
4839 /* Subroutine of outer_binding.
4841 Returns TRUE if BINDING is a binding to a template parameter of
4842 SCOPE. In that case SCOPE is the scope of a primary template
4843 parameter -- in the sense of G++, i.e, a template that has its own
4844 template header.
4846 Returns FALSE otherwise. */
4848 static bool
4849 binding_to_template_parms_of_scope_p (cxx_binding *binding,
4850 cp_binding_level *scope)
4852 tree binding_value, tmpl, tinfo;
4853 int level;
4855 if (!binding || !scope || !scope->this_entity)
4856 return false;
4858 binding_value = binding->value ? binding->value : binding->type;
4859 tinfo = get_template_info (scope->this_entity);
4861 /* BINDING_VALUE must be a template parm. */
4862 if (binding_value == NULL_TREE
4863 || (!DECL_P (binding_value)
4864 || !DECL_TEMPLATE_PARM_P (binding_value)))
4865 return false;
4867 /* The level of BINDING_VALUE. */
4868 level =
4869 template_type_parameter_p (binding_value)
4870 ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX
4871 (TREE_TYPE (binding_value)))
4872 : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value));
4874 /* The template of the current scope, iff said scope is a primary
4875 template. */
4876 tmpl = (tinfo
4877 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
4878 ? TI_TEMPLATE (tinfo)
4879 : NULL_TREE);
4881 /* If the level of the parm BINDING_VALUE equals the depth of TMPL,
4882 then BINDING_VALUE is a parameter of TMPL. */
4883 return (tmpl && level == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
4886 /* Return the innermost non-namespace binding for NAME from a scope
4887 containing BINDING, or, if BINDING is NULL, the current scope.
4888 Please note that for a given template, the template parameters are
4889 considered to be in the scope containing the current scope.
4890 If CLASS_P is false, then class bindings are ignored. */
4892 cxx_binding *
4893 outer_binding (tree name,
4894 cxx_binding *binding,
4895 bool class_p)
4897 cxx_binding *outer;
4898 cp_binding_level *scope;
4899 cp_binding_level *outer_scope;
4901 if (binding)
4903 scope = binding->scope->level_chain;
4904 outer = binding->previous;
4906 else
4908 scope = current_binding_level;
4909 outer = IDENTIFIER_BINDING (name);
4911 outer_scope = outer ? outer->scope : NULL;
4913 /* Because we create class bindings lazily, we might be missing a
4914 class binding for NAME. If there are any class binding levels
4915 between the LAST_BINDING_LEVEL and the scope in which OUTER was
4916 declared, we must lookup NAME in those class scopes. */
4917 if (class_p)
4918 while (scope && scope != outer_scope && scope->kind != sk_namespace)
4920 if (scope->kind == sk_class)
4922 cxx_binding *class_binding;
4924 class_binding = get_class_binding (name, scope);
4925 if (class_binding)
4927 /* Thread this new class-scope binding onto the
4928 IDENTIFIER_BINDING list so that future lookups
4929 find it quickly. */
4930 class_binding->previous = outer;
4931 if (binding)
4932 binding->previous = class_binding;
4933 else
4934 IDENTIFIER_BINDING (name) = class_binding;
4935 return class_binding;
4938 /* If we are in a member template, the template parms of the member
4939 template are considered to be inside the scope of the containing
4940 class, but within G++ the class bindings are all pushed between the
4941 template parms and the function body. So if the outer binding is
4942 a template parm for the current scope, return it now rather than
4943 look for a class binding. */
4944 if (outer_scope && outer_scope->kind == sk_template_parms
4945 && binding_to_template_parms_of_scope_p (outer, scope))
4946 return outer;
4948 scope = scope->level_chain;
4951 return outer;
4954 /* Return the innermost block-scope or class-scope value binding for
4955 NAME, or NULL_TREE if there is no such binding. */
4957 tree
4958 innermost_non_namespace_value (tree name)
4960 cxx_binding *binding;
4961 binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true);
4962 return binding ? binding->value : NULL_TREE;
4965 /* Look up NAME in the current binding level and its superiors in the
4966 namespace of variables, functions and typedefs. Return a ..._DECL
4967 node of some kind representing its definition if there is only one
4968 such declaration, or return a TREE_LIST with all the overloaded
4969 definitions if there are many, or return 0 if it is undefined.
4970 Hidden name, either friend declaration or built-in function, are
4971 not ignored.
4973 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
4974 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
4975 Otherwise we prefer non-TYPE_DECLs.
4977 If NONCLASS is nonzero, bindings in class scopes are ignored. If
4978 BLOCK_P is false, bindings in block scopes are ignored. */
4980 static tree
4981 lookup_name_real_1 (tree name, int prefer_type, int nonclass, bool block_p,
4982 int namespaces_only, int flags)
4984 cxx_binding *iter;
4985 tree val = NULL_TREE;
4987 /* Conversion operators are handled specially because ordinary
4988 unqualified name lookup will not find template conversion
4989 operators. */
4990 if (IDENTIFIER_TYPENAME_P (name))
4992 cp_binding_level *level;
4994 for (level = current_binding_level;
4995 level && level->kind != sk_namespace;
4996 level = level->level_chain)
4998 tree class_type;
4999 tree operators;
5001 /* A conversion operator can only be declared in a class
5002 scope. */
5003 if (level->kind != sk_class)
5004 continue;
5006 /* Lookup the conversion operator in the class. */
5007 class_type = level->this_entity;
5008 operators = lookup_fnfields (class_type, name, /*protect=*/0);
5009 if (operators)
5010 return operators;
5013 return NULL_TREE;
5016 flags |= lookup_flags (prefer_type, namespaces_only);
5018 /* First, look in non-namespace scopes. */
5020 if (current_class_type == NULL_TREE)
5021 nonclass = 1;
5023 if (block_p || !nonclass)
5024 for (iter = outer_binding (name, NULL, !nonclass);
5025 iter;
5026 iter = outer_binding (name, iter, !nonclass))
5028 tree binding;
5030 /* Skip entities we don't want. */
5031 if (LOCAL_BINDING_P (iter) ? !block_p : nonclass)
5032 continue;
5034 /* If this is the kind of thing we're looking for, we're done. */
5035 if (qualify_lookup (iter->value, flags))
5036 binding = iter->value;
5037 else if ((flags & LOOKUP_PREFER_TYPES)
5038 && qualify_lookup (iter->type, flags))
5039 binding = iter->type;
5040 else
5041 binding = NULL_TREE;
5043 if (binding)
5045 if (hidden_name_p (binding))
5047 /* A non namespace-scope binding can only be hidden in the
5048 presence of a local class, due to friend declarations.
5050 In particular, consider:
5052 struct C;
5053 void f() {
5054 struct A {
5055 friend struct B;
5056 friend struct C;
5057 void g() {
5058 B* b; // error: B is hidden
5059 C* c; // OK, finds ::C
5062 B *b; // error: B is hidden
5063 C *c; // OK, finds ::C
5064 struct B {};
5065 B *bb; // OK
5068 The standard says that "B" is a local class in "f"
5069 (but not nested within "A") -- but that name lookup
5070 for "B" does not find this declaration until it is
5071 declared directly with "f".
5073 In particular:
5075 [class.friend]
5077 If a friend declaration appears in a local class and
5078 the name specified is an unqualified name, a prior
5079 declaration is looked up without considering scopes
5080 that are outside the innermost enclosing non-class
5081 scope. For a friend function declaration, if there is
5082 no prior declaration, the program is ill-formed. For a
5083 friend class declaration, if there is no prior
5084 declaration, the class that is specified belongs to the
5085 innermost enclosing non-class scope, but if it is
5086 subsequently referenced, its name is not found by name
5087 lookup until a matching declaration is provided in the
5088 innermost enclosing nonclass scope.
5090 So just keep looking for a non-hidden binding.
5092 gcc_assert (TREE_CODE (binding) == TYPE_DECL);
5093 continue;
5095 val = binding;
5096 break;
5100 /* Now lookup in namespace scopes. */
5101 if (!val)
5102 val = unqualified_namespace_lookup (name, flags);
5104 /* Anticipated built-ins and friends aren't found by normal lookup. */
5105 if (val && !(flags & LOOKUP_HIDDEN))
5106 val = remove_hidden_names (val);
5108 /* If we have a single function from a using decl, pull it out. */
5109 if (val && TREE_CODE (val) == OVERLOAD && !really_overloaded_fn (val))
5110 val = OVL_FUNCTION (val);
5112 return val;
5115 /* Wrapper for lookup_name_real_1. */
5117 tree
5118 lookup_name_real (tree name, int prefer_type, int nonclass, bool block_p,
5119 int namespaces_only, int flags)
5121 tree ret;
5122 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5123 ret = lookup_name_real_1 (name, prefer_type, nonclass, block_p,
5124 namespaces_only, flags);
5125 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5126 return ret;
5129 tree
5130 lookup_name_nonclass (tree name)
5132 return lookup_name_real (name, 0, 1, /*block_p=*/true, 0, 0);
5135 tree
5136 lookup_function_nonclass (tree name, vec<tree, va_gc> *args, bool block_p)
5138 return
5139 lookup_arg_dependent (name,
5140 lookup_name_real (name, 0, 1, block_p, 0, 0),
5141 args);
5144 tree
5145 lookup_name (tree name)
5147 return lookup_name_real (name, 0, 0, /*block_p=*/true, 0, 0);
5150 tree
5151 lookup_name_prefer_type (tree name, int prefer_type)
5153 return lookup_name_real (name, prefer_type, 0, /*block_p=*/true, 0, 0);
5156 /* Look up NAME for type used in elaborated name specifier in
5157 the scopes given by SCOPE. SCOPE can be either TS_CURRENT or
5158 TS_WITHIN_ENCLOSING_NON_CLASS. Although not implied by the
5159 name, more scopes are checked if cleanup or template parameter
5160 scope is encountered.
5162 Unlike lookup_name_real, we make sure that NAME is actually
5163 declared in the desired scope, not from inheritance, nor using
5164 directive. For using declaration, there is DR138 still waiting
5165 to be resolved. Hidden name coming from an earlier friend
5166 declaration is also returned.
5168 A TYPE_DECL best matching the NAME is returned. Catching error
5169 and issuing diagnostics are caller's responsibility. */
5171 static tree
5172 lookup_type_scope_1 (tree name, tag_scope scope)
5174 cxx_binding *iter = NULL;
5175 tree val = NULL_TREE;
5177 /* Look in non-namespace scope first. */
5178 if (current_binding_level->kind != sk_namespace)
5179 iter = outer_binding (name, NULL, /*class_p=*/ true);
5180 for (; iter; iter = outer_binding (name, iter, /*class_p=*/ true))
5182 /* Check if this is the kind of thing we're looking for.
5183 If SCOPE is TS_CURRENT, also make sure it doesn't come from
5184 base class. For ITER->VALUE, we can simply use
5185 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use
5186 our own check.
5188 We check ITER->TYPE before ITER->VALUE in order to handle
5189 typedef struct C {} C;
5190 correctly. */
5192 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES)
5193 && (scope != ts_current
5194 || LOCAL_BINDING_P (iter)
5195 || DECL_CONTEXT (iter->type) == iter->scope->this_entity))
5196 val = iter->type;
5197 else if ((scope != ts_current
5198 || !INHERITED_VALUE_BINDING_P (iter))
5199 && qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
5200 val = iter->value;
5202 if (val)
5203 break;
5206 /* Look in namespace scope. */
5207 if (!val)
5209 iter = cp_binding_level_find_binding_for_name
5210 (NAMESPACE_LEVEL (current_decl_namespace ()), name);
5212 if (iter)
5214 /* If this is the kind of thing we're looking for, we're done. */
5215 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES))
5216 val = iter->type;
5217 else if (qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
5218 val = iter->value;
5223 /* Type found, check if it is in the allowed scopes, ignoring cleanup
5224 and template parameter scopes. */
5225 if (val)
5227 cp_binding_level *b = current_binding_level;
5228 while (b)
5230 if (iter->scope == b)
5231 return val;
5233 if (b->kind == sk_cleanup || b->kind == sk_template_parms
5234 || b->kind == sk_function_parms)
5235 b = b->level_chain;
5236 else if (b->kind == sk_class
5237 && scope == ts_within_enclosing_non_class)
5238 b = b->level_chain;
5239 else
5240 break;
5244 return NULL_TREE;
5247 /* Wrapper for lookup_type_scope_1. */
5249 tree
5250 lookup_type_scope (tree name, tag_scope scope)
5252 tree ret;
5253 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5254 ret = lookup_type_scope_1 (name, scope);
5255 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5256 return ret;
5260 /* Similar to `lookup_name' but look only in the innermost non-class
5261 binding level. */
5263 static tree
5264 lookup_name_innermost_nonclass_level_1 (tree name)
5266 cp_binding_level *b;
5267 tree t = NULL_TREE;
5269 b = innermost_nonclass_level ();
5271 if (b->kind == sk_namespace)
5273 t = IDENTIFIER_NAMESPACE_VALUE (name);
5275 /* extern "C" function() */
5276 if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
5277 t = TREE_VALUE (t);
5279 else if (IDENTIFIER_BINDING (name)
5280 && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
5282 cxx_binding *binding;
5283 binding = IDENTIFIER_BINDING (name);
5284 while (1)
5286 if (binding->scope == b
5287 && !(VAR_P (binding->value)
5288 && DECL_DEAD_FOR_LOCAL (binding->value)))
5289 return binding->value;
5291 if (b->kind == sk_cleanup)
5292 b = b->level_chain;
5293 else
5294 break;
5298 return t;
5301 /* Wrapper for lookup_name_innermost_nonclass_level_1. */
5303 tree
5304 lookup_name_innermost_nonclass_level (tree name)
5306 tree ret;
5307 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5308 ret = lookup_name_innermost_nonclass_level_1 (name);
5309 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5310 return ret;
5314 /* Returns true iff DECL is a block-scope extern declaration of a function
5315 or variable. */
5317 bool
5318 is_local_extern (tree decl)
5320 cxx_binding *binding;
5322 /* For functions, this is easy. */
5323 if (TREE_CODE (decl) == FUNCTION_DECL)
5324 return DECL_LOCAL_FUNCTION_P (decl);
5326 if (!VAR_P (decl))
5327 return false;
5328 if (!current_function_decl)
5329 return false;
5331 /* For variables, this is not easy. We need to look at the binding stack
5332 for the identifier to see whether the decl we have is a local. */
5333 for (binding = IDENTIFIER_BINDING (DECL_NAME (decl));
5334 binding && binding->scope->kind != sk_namespace;
5335 binding = binding->previous)
5336 if (binding->value == decl)
5337 return LOCAL_BINDING_P (binding);
5339 return false;
5342 /* Like lookup_name_innermost_nonclass_level, but for types. */
5344 static tree
5345 lookup_type_current_level (tree name)
5347 tree t = NULL_TREE;
5349 timevar_start (TV_NAME_LOOKUP);
5350 gcc_assert (current_binding_level->kind != sk_namespace);
5352 if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
5353 && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
5355 cp_binding_level *b = current_binding_level;
5356 while (1)
5358 if (purpose_member (name, b->type_shadowed))
5360 t = REAL_IDENTIFIER_TYPE_VALUE (name);
5361 break;
5363 if (b->kind == sk_cleanup)
5364 b = b->level_chain;
5365 else
5366 break;
5370 timevar_stop (TV_NAME_LOOKUP);
5371 return t;
5374 /* [basic.lookup.koenig] */
5375 /* A nonzero return value in the functions below indicates an error. */
5377 struct arg_lookup
5379 tree name;
5380 vec<tree, va_gc> *args;
5381 vec<tree, va_gc> *namespaces;
5382 vec<tree, va_gc> *classes;
5383 tree functions;
5384 hash_set<tree> *fn_set;
5387 static bool arg_assoc (struct arg_lookup*, tree);
5388 static bool arg_assoc_args (struct arg_lookup*, tree);
5389 static bool arg_assoc_args_vec (struct arg_lookup*, vec<tree, va_gc> *);
5390 static bool arg_assoc_type (struct arg_lookup*, tree);
5391 static bool add_function (struct arg_lookup *, tree);
5392 static bool arg_assoc_namespace (struct arg_lookup *, tree);
5393 static bool arg_assoc_class_only (struct arg_lookup *, tree);
5394 static bool arg_assoc_bases (struct arg_lookup *, tree);
5395 static bool arg_assoc_class (struct arg_lookup *, tree);
5396 static bool arg_assoc_template_arg (struct arg_lookup*, tree);
5398 /* Add a function to the lookup structure.
5399 Returns true on error. */
5401 static bool
5402 add_function (struct arg_lookup *k, tree fn)
5404 if (!is_overloaded_fn (fn))
5405 /* All names except those of (possibly overloaded) functions and
5406 function templates are ignored. */;
5407 else if (k->fn_set && k->fn_set->add (fn))
5408 /* It's already in the list. */;
5409 else if (!k->functions && TREE_CODE (fn) != TEMPLATE_DECL)
5410 k->functions = fn;
5411 else if (fn == k->functions)
5413 else
5415 k->functions = build_overload (fn, k->functions);
5416 if (TREE_CODE (k->functions) == OVERLOAD)
5417 OVL_ARG_DEPENDENT (k->functions) = true;
5420 return false;
5423 /* Returns true iff CURRENT has declared itself to be an associated
5424 namespace of SCOPE via a strong using-directive (or transitive chain
5425 thereof). Both are namespaces. */
5427 bool
5428 is_associated_namespace (tree current, tree scope)
5430 vec<tree, va_gc> *seen = make_tree_vector ();
5431 vec<tree, va_gc> *todo = make_tree_vector ();
5432 tree t;
5433 bool ret;
5435 while (1)
5437 if (scope == current)
5439 ret = true;
5440 break;
5442 vec_safe_push (seen, scope);
5443 for (t = DECL_NAMESPACE_ASSOCIATIONS (scope); t; t = TREE_CHAIN (t))
5444 if (!vec_member (TREE_PURPOSE (t), seen))
5445 vec_safe_push (todo, TREE_PURPOSE (t));
5446 if (!todo->is_empty ())
5448 scope = todo->last ();
5449 todo->pop ();
5451 else
5453 ret = false;
5454 break;
5458 release_tree_vector (seen);
5459 release_tree_vector (todo);
5461 return ret;
5464 /* Add functions of a namespace to the lookup structure.
5465 Returns true on error. */
5467 static bool
5468 arg_assoc_namespace (struct arg_lookup *k, tree scope)
5470 tree value;
5472 if (vec_member (scope, k->namespaces))
5473 return false;
5474 vec_safe_push (k->namespaces, scope);
5476 /* Check out our super-users. */
5477 for (value = DECL_NAMESPACE_ASSOCIATIONS (scope); value;
5478 value = TREE_CHAIN (value))
5479 if (arg_assoc_namespace (k, TREE_PURPOSE (value)))
5480 return true;
5482 /* Also look down into inline namespaces. */
5483 for (value = DECL_NAMESPACE_USING (scope); value;
5484 value = TREE_CHAIN (value))
5485 if (is_associated_namespace (scope, TREE_PURPOSE (value)))
5486 if (arg_assoc_namespace (k, TREE_PURPOSE (value)))
5487 return true;
5489 value = namespace_binding (k->name, scope);
5490 if (!value)
5491 return false;
5493 for (; value; value = OVL_NEXT (value))
5495 /* We don't want to find arbitrary hidden functions via argument
5496 dependent lookup. We only want to find friends of associated
5497 classes, which we'll do via arg_assoc_class. */
5498 if (hidden_name_p (OVL_CURRENT (value)))
5499 continue;
5501 if (add_function (k, OVL_CURRENT (value)))
5502 return true;
5505 return false;
5508 /* Adds everything associated with a template argument to the lookup
5509 structure. Returns true on error. */
5511 static bool
5512 arg_assoc_template_arg (struct arg_lookup *k, tree arg)
5514 /* [basic.lookup.koenig]
5516 If T is a template-id, its associated namespaces and classes are
5517 ... the namespaces and classes associated with the types of the
5518 template arguments provided for template type parameters
5519 (excluding template template parameters); the namespaces in which
5520 any template template arguments are defined; and the classes in
5521 which any member templates used as template template arguments
5522 are defined. [Note: non-type template arguments do not
5523 contribute to the set of associated namespaces. ] */
5525 /* Consider first template template arguments. */
5526 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
5527 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
5528 return false;
5529 else if (TREE_CODE (arg) == TEMPLATE_DECL)
5531 tree ctx = CP_DECL_CONTEXT (arg);
5533 /* It's not a member template. */
5534 if (TREE_CODE (ctx) == NAMESPACE_DECL)
5535 return arg_assoc_namespace (k, ctx);
5536 /* Otherwise, it must be member template. */
5537 else
5538 return arg_assoc_class_only (k, ctx);
5540 /* It's an argument pack; handle it recursively. */
5541 else if (ARGUMENT_PACK_P (arg))
5543 tree args = ARGUMENT_PACK_ARGS (arg);
5544 int i, len = TREE_VEC_LENGTH (args);
5545 for (i = 0; i < len; ++i)
5546 if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, i)))
5547 return true;
5549 return false;
5551 /* It's not a template template argument, but it is a type template
5552 argument. */
5553 else if (TYPE_P (arg))
5554 return arg_assoc_type (k, arg);
5555 /* It's a non-type template argument. */
5556 else
5557 return false;
5560 /* Adds the class and its friends to the lookup structure.
5561 Returns true on error. */
5563 static bool
5564 arg_assoc_class_only (struct arg_lookup *k, tree type)
5566 tree list, friends, context;
5568 /* Backend-built structures, such as __builtin_va_list, aren't
5569 affected by all this. */
5570 if (!CLASS_TYPE_P (type))
5571 return false;
5573 context = decl_namespace_context (type);
5574 if (arg_assoc_namespace (k, context))
5575 return true;
5577 complete_type (type);
5579 /* Process friends. */
5580 for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
5581 list = TREE_CHAIN (list))
5582 if (k->name == FRIEND_NAME (list))
5583 for (friends = FRIEND_DECLS (list); friends;
5584 friends = TREE_CHAIN (friends))
5586 tree fn = TREE_VALUE (friends);
5588 /* Only interested in global functions with potentially hidden
5589 (i.e. unqualified) declarations. */
5590 if (CP_DECL_CONTEXT (fn) != context)
5591 continue;
5592 /* Template specializations are never found by name lookup.
5593 (Templates themselves can be found, but not template
5594 specializations.) */
5595 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn))
5596 continue;
5597 if (add_function (k, fn))
5598 return true;
5601 return false;
5604 /* Adds the class and its bases to the lookup structure.
5605 Returns true on error. */
5607 static bool
5608 arg_assoc_bases (struct arg_lookup *k, tree type)
5610 if (arg_assoc_class_only (k, type))
5611 return true;
5613 if (TYPE_BINFO (type))
5615 /* Process baseclasses. */
5616 tree binfo, base_binfo;
5617 int i;
5619 for (binfo = TYPE_BINFO (type), i = 0;
5620 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5621 if (arg_assoc_bases (k, BINFO_TYPE (base_binfo)))
5622 return true;
5625 return false;
5628 /* Adds everything associated with a class argument type to the lookup
5629 structure. Returns true on error.
5631 If T is a class type (including unions), its associated classes are: the
5632 class itself; the class of which it is a member, if any; and its direct
5633 and indirect base classes. Its associated namespaces are the namespaces
5634 of which its associated classes are members. Furthermore, if T is a
5635 class template specialization, its associated namespaces and classes
5636 also include: the namespaces and classes associated with the types of
5637 the template arguments provided for template type parameters (excluding
5638 template template parameters); the namespaces of which any template
5639 template arguments are members; and the classes of which any member
5640 templates used as template template arguments are members. [ Note:
5641 non-type template arguments do not contribute to the set of associated
5642 namespaces. --end note] */
5644 static bool
5645 arg_assoc_class (struct arg_lookup *k, tree type)
5647 tree list;
5648 int i;
5650 /* Backend build structures, such as __builtin_va_list, aren't
5651 affected by all this. */
5652 if (!CLASS_TYPE_P (type))
5653 return false;
5655 if (vec_member (type, k->classes))
5656 return false;
5657 vec_safe_push (k->classes, type);
5659 if (TYPE_CLASS_SCOPE_P (type)
5660 && arg_assoc_class_only (k, TYPE_CONTEXT (type)))
5661 return true;
5663 if (arg_assoc_bases (k, type))
5664 return true;
5666 /* Process template arguments. */
5667 if (CLASSTYPE_TEMPLATE_INFO (type)
5668 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
5670 list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
5671 for (i = 0; i < TREE_VEC_LENGTH (list); ++i)
5672 if (arg_assoc_template_arg (k, TREE_VEC_ELT (list, i)))
5673 return true;
5676 return false;
5679 /* Adds everything associated with a given type.
5680 Returns 1 on error. */
5682 static bool
5683 arg_assoc_type (struct arg_lookup *k, tree type)
5685 /* As we do not get the type of non-type dependent expressions
5686 right, we can end up with such things without a type. */
5687 if (!type)
5688 return false;
5690 if (TYPE_PTRDATAMEM_P (type))
5692 /* Pointer to member: associate class type and value type. */
5693 if (arg_assoc_type (k, TYPE_PTRMEM_CLASS_TYPE (type)))
5694 return true;
5695 return arg_assoc_type (k, TYPE_PTRMEM_POINTED_TO_TYPE (type));
5697 else switch (TREE_CODE (type))
5699 case ERROR_MARK:
5700 return false;
5701 case VOID_TYPE:
5702 case INTEGER_TYPE:
5703 case REAL_TYPE:
5704 case COMPLEX_TYPE:
5705 case VECTOR_TYPE:
5706 case BOOLEAN_TYPE:
5707 case FIXED_POINT_TYPE:
5708 case DECLTYPE_TYPE:
5709 case NULLPTR_TYPE:
5710 return false;
5711 case RECORD_TYPE:
5712 if (TYPE_PTRMEMFUNC_P (type))
5713 return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type));
5714 /* FALLTHRU */
5715 case UNION_TYPE:
5716 return arg_assoc_class (k, type);
5717 case POINTER_TYPE:
5718 case REFERENCE_TYPE:
5719 case ARRAY_TYPE:
5720 return arg_assoc_type (k, TREE_TYPE (type));
5721 case ENUMERAL_TYPE:
5722 if (TYPE_CLASS_SCOPE_P (type)
5723 && arg_assoc_class_only (k, TYPE_CONTEXT (type)))
5724 return true;
5725 return arg_assoc_namespace (k, decl_namespace_context (type));
5726 case METHOD_TYPE:
5727 /* The basetype is referenced in the first arg type, so just
5728 fall through. */
5729 case FUNCTION_TYPE:
5730 /* Associate the parameter types. */
5731 if (arg_assoc_args (k, TYPE_ARG_TYPES (type)))
5732 return true;
5733 /* Associate the return type. */
5734 return arg_assoc_type (k, TREE_TYPE (type));
5735 case TEMPLATE_TYPE_PARM:
5736 case BOUND_TEMPLATE_TEMPLATE_PARM:
5737 return false;
5738 case TYPENAME_TYPE:
5739 return false;
5740 case LANG_TYPE:
5741 gcc_assert (type == unknown_type_node
5742 || type == init_list_type_node);
5743 return false;
5744 case TYPE_PACK_EXPANSION:
5745 return arg_assoc_type (k, PACK_EXPANSION_PATTERN (type));
5747 default:
5748 gcc_unreachable ();
5750 return false;
5753 /* Adds everything associated with arguments. Returns true on error. */
5755 static bool
5756 arg_assoc_args (struct arg_lookup *k, tree args)
5758 for (; args; args = TREE_CHAIN (args))
5759 if (arg_assoc (k, TREE_VALUE (args)))
5760 return true;
5761 return false;
5764 /* Adds everything associated with an argument vector. Returns true
5765 on error. */
5767 static bool
5768 arg_assoc_args_vec (struct arg_lookup *k, vec<tree, va_gc> *args)
5770 unsigned int ix;
5771 tree arg;
5773 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
5774 if (arg_assoc (k, arg))
5775 return true;
5776 return false;
5779 /* Adds everything associated with a given tree_node. Returns 1 on error. */
5781 static bool
5782 arg_assoc (struct arg_lookup *k, tree n)
5784 if (n == error_mark_node)
5785 return false;
5787 if (TYPE_P (n))
5788 return arg_assoc_type (k, n);
5790 if (! type_unknown_p (n))
5791 return arg_assoc_type (k, TREE_TYPE (n));
5793 if (TREE_CODE (n) == ADDR_EXPR)
5794 n = TREE_OPERAND (n, 0);
5795 if (TREE_CODE (n) == COMPONENT_REF)
5796 n = TREE_OPERAND (n, 1);
5797 if (TREE_CODE (n) == OFFSET_REF)
5798 n = TREE_OPERAND (n, 1);
5799 while (TREE_CODE (n) == TREE_LIST)
5800 n = TREE_VALUE (n);
5801 if (BASELINK_P (n))
5802 n = BASELINK_FUNCTIONS (n);
5804 if (TREE_CODE (n) == FUNCTION_DECL)
5805 return arg_assoc_type (k, TREE_TYPE (n));
5806 if (TREE_CODE (n) == TEMPLATE_ID_EXPR)
5808 /* The working paper doesn't currently say how to handle template-id
5809 arguments. The sensible thing would seem to be to handle the list
5810 of template candidates like a normal overload set, and handle the
5811 template arguments like we do for class template
5812 specializations. */
5813 tree templ = TREE_OPERAND (n, 0);
5814 tree args = TREE_OPERAND (n, 1);
5815 int ix;
5817 /* First the templates. */
5818 if (arg_assoc (k, templ))
5819 return true;
5821 /* Now the arguments. */
5822 if (args)
5823 for (ix = TREE_VEC_LENGTH (args); ix--;)
5824 if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, ix)) == 1)
5825 return true;
5827 else if (TREE_CODE (n) == OVERLOAD)
5829 for (; n; n = OVL_NEXT (n))
5830 if (arg_assoc_type (k, TREE_TYPE (OVL_CURRENT (n))))
5831 return true;
5834 return false;
5837 /* Performs Koenig lookup depending on arguments, where fns
5838 are the functions found in normal lookup. */
5840 static cp_expr
5841 lookup_arg_dependent_1 (tree name, tree fns, vec<tree, va_gc> *args)
5843 struct arg_lookup k;
5845 /* Remove any hidden friend functions from the list of functions
5846 found so far. They will be added back by arg_assoc_class as
5847 appropriate. */
5848 fns = remove_hidden_names (fns);
5850 k.name = name;
5851 k.args = args;
5852 k.functions = fns;
5853 k.classes = make_tree_vector ();
5855 /* We previously performed an optimization here by setting
5856 NAMESPACES to the current namespace when it was safe. However, DR
5857 164 says that namespaces that were already searched in the first
5858 stage of template processing are searched again (potentially
5859 picking up later definitions) in the second stage. */
5860 k.namespaces = make_tree_vector ();
5862 /* We used to allow duplicates and let joust discard them, but
5863 since the above change for DR 164 we end up with duplicates of
5864 all the functions found by unqualified lookup. So keep track
5865 of which ones we've seen. */
5866 if (fns)
5868 tree ovl;
5869 /* We shouldn't be here if lookup found something other than
5870 namespace-scope functions. */
5871 gcc_assert (DECL_NAMESPACE_SCOPE_P (OVL_CURRENT (fns)));
5872 k.fn_set = new hash_set<tree>;
5873 for (ovl = fns; ovl; ovl = OVL_NEXT (ovl))
5874 k.fn_set->add (OVL_CURRENT (ovl));
5876 else
5877 k.fn_set = NULL;
5879 arg_assoc_args_vec (&k, args);
5881 fns = k.functions;
5883 if (fns
5884 && !VAR_P (fns)
5885 && !is_overloaded_fn (fns))
5887 error ("argument dependent lookup finds %q+D", fns);
5888 error (" in call to %qD", name);
5889 fns = error_mark_node;
5892 release_tree_vector (k.classes);
5893 release_tree_vector (k.namespaces);
5894 delete k.fn_set;
5896 return fns;
5899 /* Wrapper for lookup_arg_dependent_1. */
5901 cp_expr
5902 lookup_arg_dependent (tree name, tree fns, vec<tree, va_gc> *args)
5904 cp_expr ret;
5905 bool subtime;
5906 subtime = timevar_cond_start (TV_NAME_LOOKUP);
5907 ret = lookup_arg_dependent_1 (name, fns, args);
5908 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5909 return ret;
5913 /* Add namespace to using_directives. Return NULL_TREE if nothing was
5914 changed (i.e. there was already a directive), or the fresh
5915 TREE_LIST otherwise. */
5917 static tree
5918 push_using_directive_1 (tree used)
5920 tree ud = current_binding_level->using_directives;
5921 tree iter, ancestor;
5923 /* Check if we already have this. */
5924 if (purpose_member (used, ud) != NULL_TREE)
5925 return NULL_TREE;
5927 ancestor = namespace_ancestor (current_decl_namespace (), used);
5928 ud = current_binding_level->using_directives;
5929 ud = tree_cons (used, ancestor, ud);
5930 current_binding_level->using_directives = ud;
5932 /* Recursively add all namespaces used. */
5933 for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
5934 push_using_directive (TREE_PURPOSE (iter));
5936 return ud;
5939 /* Wrapper for push_using_directive_1. */
5941 static tree
5942 push_using_directive (tree used)
5944 tree ret;
5945 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5946 ret = push_using_directive_1 (used);
5947 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5948 return ret;
5951 /* The type TYPE is being declared. If it is a class template, or a
5952 specialization of a class template, do any processing required and
5953 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
5954 being declared a friend. B is the binding level at which this TYPE
5955 should be bound.
5957 Returns the TYPE_DECL for TYPE, which may have been altered by this
5958 processing. */
5960 static tree
5961 maybe_process_template_type_declaration (tree type, int is_friend,
5962 cp_binding_level *b)
5964 tree decl = TYPE_NAME (type);
5966 if (processing_template_parmlist)
5967 /* You can't declare a new template type in a template parameter
5968 list. But, you can declare a non-template type:
5970 template <class A*> struct S;
5972 is a forward-declaration of `A'. */
5974 else if (b->kind == sk_namespace
5975 && current_binding_level->kind != sk_namespace)
5976 /* If this new type is being injected into a containing scope,
5977 then it's not a template type. */
5979 else
5981 gcc_assert (MAYBE_CLASS_TYPE_P (type)
5982 || TREE_CODE (type) == ENUMERAL_TYPE);
5984 if (processing_template_decl)
5986 /* This may change after the call to
5987 push_template_decl_real, but we want the original value. */
5988 tree name = DECL_NAME (decl);
5990 decl = push_template_decl_real (decl, is_friend);
5991 if (decl == error_mark_node)
5992 return error_mark_node;
5994 /* If the current binding level is the binding level for the
5995 template parameters (see the comment in
5996 begin_template_parm_list) and the enclosing level is a class
5997 scope, and we're not looking at a friend, push the
5998 declaration of the member class into the class scope. In the
5999 friend case, push_template_decl will already have put the
6000 friend into global scope, if appropriate. */
6001 if (TREE_CODE (type) != ENUMERAL_TYPE
6002 && !is_friend && b->kind == sk_template_parms
6003 && b->level_chain->kind == sk_class)
6005 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
6007 if (!COMPLETE_TYPE_P (current_class_type))
6009 maybe_add_class_template_decl_list (current_class_type,
6010 type, /*friend_p=*/0);
6011 /* Put this UTD in the table of UTDs for the class. */
6012 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
6013 CLASSTYPE_NESTED_UTDS (current_class_type) =
6014 binding_table_new (SCOPE_DEFAULT_HT_SIZE);
6016 binding_table_insert
6017 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
6023 return decl;
6026 /* Push a tag name NAME for struct/class/union/enum type TYPE. In case
6027 that the NAME is a class template, the tag is processed but not pushed.
6029 The pushed scope depend on the SCOPE parameter:
6030 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
6031 scope.
6032 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
6033 non-template-parameter scope. This case is needed for forward
6034 declarations.
6035 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
6036 TS_GLOBAL case except that names within template-parameter scopes
6037 are not pushed at all.
6039 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
6041 static tree
6042 pushtag_1 (tree name, tree type, tag_scope scope)
6044 cp_binding_level *b;
6045 tree decl;
6047 b = current_binding_level;
6048 while (/* Cleanup scopes are not scopes from the point of view of
6049 the language. */
6050 b->kind == sk_cleanup
6051 /* Neither are function parameter scopes. */
6052 || b->kind == sk_function_parms
6053 /* Neither are the scopes used to hold template parameters
6054 for an explicit specialization. For an ordinary template
6055 declaration, these scopes are not scopes from the point of
6056 view of the language. */
6057 || (b->kind == sk_template_parms
6058 && (b->explicit_spec_p || scope == ts_global))
6059 || (b->kind == sk_class
6060 && (scope != ts_current
6061 /* We may be defining a new type in the initializer
6062 of a static member variable. We allow this when
6063 not pedantic, and it is particularly useful for
6064 type punning via an anonymous union. */
6065 || COMPLETE_TYPE_P (b->this_entity))))
6066 b = b->level_chain;
6068 gcc_assert (identifier_p (name));
6070 /* Do C++ gratuitous typedefing. */
6071 if (identifier_type_value_1 (name) != type)
6073 tree tdef;
6074 int in_class = 0;
6075 tree context = TYPE_CONTEXT (type);
6077 if (! context)
6079 tree cs = current_scope ();
6081 if (scope == ts_current
6082 || (cs && TREE_CODE (cs) == FUNCTION_DECL))
6083 context = cs;
6084 else if (cs != NULL_TREE && TYPE_P (cs))
6085 /* When declaring a friend class of a local class, we want
6086 to inject the newly named class into the scope
6087 containing the local class, not the namespace
6088 scope. */
6089 context = decl_function_context (get_type_decl (cs));
6091 if (!context)
6092 context = current_namespace;
6094 if (b->kind == sk_class
6095 || (b->kind == sk_template_parms
6096 && b->level_chain->kind == sk_class))
6097 in_class = 1;
6099 tdef = create_implicit_typedef (name, type);
6100 DECL_CONTEXT (tdef) = FROB_CONTEXT (context);
6101 if (scope == ts_within_enclosing_non_class)
6103 /* This is a friend. Make this TYPE_DECL node hidden from
6104 ordinary name lookup. Its corresponding TEMPLATE_DECL
6105 will be marked in push_template_decl_real. */
6106 retrofit_lang_decl (tdef);
6107 DECL_ANTICIPATED (tdef) = 1;
6108 DECL_FRIEND_P (tdef) = 1;
6111 decl = maybe_process_template_type_declaration
6112 (type, scope == ts_within_enclosing_non_class, b);
6113 if (decl == error_mark_node)
6114 return decl;
6116 if (b->kind == sk_class)
6118 if (!TYPE_BEING_DEFINED (current_class_type))
6119 return error_mark_node;
6121 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
6122 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
6123 class. But if it's a member template class, we want
6124 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
6125 later. */
6126 finish_member_declaration (decl);
6127 else
6128 pushdecl_class_level (decl);
6130 else if (b->kind != sk_template_parms)
6132 decl = pushdecl_with_scope_1 (decl, b, /*is_friend=*/false);
6133 if (decl == error_mark_node)
6134 return decl;
6137 if (! in_class)
6138 set_identifier_type_value_with_scope (name, tdef, b);
6140 TYPE_CONTEXT (type) = DECL_CONTEXT (decl);
6142 /* If this is a local class, keep track of it. We need this
6143 information for name-mangling, and so that it is possible to
6144 find all function definitions in a translation unit in a
6145 convenient way. (It's otherwise tricky to find a member
6146 function definition it's only pointed to from within a local
6147 class.) */
6148 if (TYPE_FUNCTION_SCOPE_P (type))
6150 if (processing_template_decl)
6152 /* Push a DECL_EXPR so we call pushtag at the right time in
6153 template instantiation rather than in some nested context. */
6154 add_decl_expr (decl);
6156 else
6157 vec_safe_push (local_classes, type);
6160 if (b->kind == sk_class
6161 && !COMPLETE_TYPE_P (current_class_type))
6163 maybe_add_class_template_decl_list (current_class_type,
6164 type, /*friend_p=*/0);
6166 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
6167 CLASSTYPE_NESTED_UTDS (current_class_type)
6168 = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
6170 binding_table_insert
6171 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
6174 decl = TYPE_NAME (type);
6175 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
6177 /* Set type visibility now if this is a forward declaration. */
6178 TREE_PUBLIC (decl) = 1;
6179 determine_visibility (decl);
6181 return type;
6184 /* Wrapper for pushtag_1. */
6186 tree
6187 pushtag (tree name, tree type, tag_scope scope)
6189 tree ret;
6190 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6191 ret = pushtag_1 (name, type, scope);
6192 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6193 return ret;
6196 /* Subroutines for reverting temporarily to top-level for instantiation
6197 of templates and such. We actually need to clear out the class- and
6198 local-value slots of all identifiers, so that only the global values
6199 are at all visible. Simply setting current_binding_level to the global
6200 scope isn't enough, because more binding levels may be pushed. */
6201 struct saved_scope *scope_chain;
6203 /* Return true if ID has not already been marked. */
6205 static inline bool
6206 store_binding_p (tree id)
6208 if (!id || !IDENTIFIER_BINDING (id))
6209 return false;
6211 if (IDENTIFIER_MARKED (id))
6212 return false;
6214 return true;
6217 /* Add an appropriate binding to *OLD_BINDINGS which needs to already
6218 have enough space reserved. */
6220 static void
6221 store_binding (tree id, vec<cxx_saved_binding, va_gc> **old_bindings)
6223 cxx_saved_binding saved;
6225 gcc_checking_assert (store_binding_p (id));
6227 IDENTIFIER_MARKED (id) = 1;
6229 saved.identifier = id;
6230 saved.binding = IDENTIFIER_BINDING (id);
6231 saved.real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
6232 (*old_bindings)->quick_push (saved);
6233 IDENTIFIER_BINDING (id) = NULL;
6236 static void
6237 store_bindings (tree names, vec<cxx_saved_binding, va_gc> **old_bindings)
6239 static vec<tree> bindings_need_stored;
6240 tree t, id;
6241 size_t i;
6243 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6244 for (t = names; t; t = TREE_CHAIN (t))
6246 if (TREE_CODE (t) == TREE_LIST)
6247 id = TREE_PURPOSE (t);
6248 else
6249 id = DECL_NAME (t);
6251 if (store_binding_p (id))
6252 bindings_need_stored.safe_push (id);
6254 if (!bindings_need_stored.is_empty ())
6256 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
6257 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
6259 /* We can appearantly have duplicates in NAMES. */
6260 if (store_binding_p (id))
6261 store_binding (id, old_bindings);
6263 bindings_need_stored.truncate (0);
6265 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6268 /* Like store_bindings, but NAMES is a vector of cp_class_binding
6269 objects, rather than a TREE_LIST. */
6271 static void
6272 store_class_bindings (vec<cp_class_binding, va_gc> *names,
6273 vec<cxx_saved_binding, va_gc> **old_bindings)
6275 static vec<tree> bindings_need_stored;
6276 size_t i;
6277 cp_class_binding *cb;
6279 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6280 for (i = 0; vec_safe_iterate (names, i, &cb); ++i)
6281 if (store_binding_p (cb->identifier))
6282 bindings_need_stored.safe_push (cb->identifier);
6283 if (!bindings_need_stored.is_empty ())
6285 tree id;
6286 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
6287 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
6288 store_binding (id, old_bindings);
6289 bindings_need_stored.truncate (0);
6291 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6294 /* A chain of saved_scope structures awaiting reuse. */
6296 static GTY((deletable)) struct saved_scope *free_saved_scope;
6298 void
6299 push_to_top_level (void)
6301 struct saved_scope *s;
6302 cp_binding_level *b;
6303 cxx_saved_binding *sb;
6304 size_t i;
6305 bool need_pop;
6307 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6309 /* Reuse or create a new structure for this saved scope. */
6310 if (free_saved_scope != NULL)
6312 s = free_saved_scope;
6313 free_saved_scope = s->prev;
6315 vec<cxx_saved_binding, va_gc> *old_bindings = s->old_bindings;
6316 memset (s, 0, sizeof (*s));
6317 /* Also reuse the structure's old_bindings vector. */
6318 vec_safe_truncate (old_bindings, 0);
6319 s->old_bindings = old_bindings;
6321 else
6322 s = ggc_cleared_alloc<saved_scope> ();
6324 b = scope_chain ? current_binding_level : 0;
6326 /* If we're in the middle of some function, save our state. */
6327 if (cfun)
6329 need_pop = true;
6330 push_function_context ();
6332 else
6333 need_pop = false;
6335 if (scope_chain && previous_class_level)
6336 store_class_bindings (previous_class_level->class_shadowed,
6337 &s->old_bindings);
6339 /* Have to include the global scope, because class-scope decls
6340 aren't listed anywhere useful. */
6341 for (; b; b = b->level_chain)
6343 tree t;
6345 /* Template IDs are inserted into the global level. If they were
6346 inserted into namespace level, finish_file wouldn't find them
6347 when doing pending instantiations. Therefore, don't stop at
6348 namespace level, but continue until :: . */
6349 if (global_scope_p (b))
6350 break;
6352 store_bindings (b->names, &s->old_bindings);
6353 /* We also need to check class_shadowed to save class-level type
6354 bindings, since pushclass doesn't fill in b->names. */
6355 if (b->kind == sk_class)
6356 store_class_bindings (b->class_shadowed, &s->old_bindings);
6358 /* Unwind type-value slots back to top level. */
6359 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
6360 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
6363 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, sb)
6364 IDENTIFIER_MARKED (sb->identifier) = 0;
6366 s->prev = scope_chain;
6367 s->bindings = b;
6368 s->need_pop_function_context = need_pop;
6369 s->function_decl = current_function_decl;
6370 s->unevaluated_operand = cp_unevaluated_operand;
6371 s->inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
6372 s->x_stmt_tree.stmts_are_full_exprs_p = true;
6374 scope_chain = s;
6375 current_function_decl = NULL_TREE;
6376 vec_alloc (current_lang_base, 10);
6377 current_lang_name = lang_name_cplusplus;
6378 current_namespace = global_namespace;
6379 push_class_stack ();
6380 cp_unevaluated_operand = 0;
6381 c_inhibit_evaluation_warnings = 0;
6382 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6385 static void
6386 pop_from_top_level_1 (void)
6388 struct saved_scope *s = scope_chain;
6389 cxx_saved_binding *saved;
6390 size_t i;
6392 /* Clear out class-level bindings cache. */
6393 if (previous_class_level)
6394 invalidate_class_lookup_cache ();
6395 pop_class_stack ();
6397 current_lang_base = 0;
6399 scope_chain = s->prev;
6400 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, saved)
6402 tree id = saved->identifier;
6404 IDENTIFIER_BINDING (id) = saved->binding;
6405 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
6408 /* If we were in the middle of compiling a function, restore our
6409 state. */
6410 if (s->need_pop_function_context)
6411 pop_function_context ();
6412 current_function_decl = s->function_decl;
6413 cp_unevaluated_operand = s->unevaluated_operand;
6414 c_inhibit_evaluation_warnings = s->inhibit_evaluation_warnings;
6416 /* Make this saved_scope structure available for reuse by
6417 push_to_top_level. */
6418 s->prev = free_saved_scope;
6419 free_saved_scope = s;
6422 /* Wrapper for pop_from_top_level_1. */
6424 void
6425 pop_from_top_level (void)
6427 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6428 pop_from_top_level_1 ();
6429 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6433 /* Pop off extraneous binding levels left over due to syntax errors.
6435 We don't pop past namespaces, as they might be valid. */
6437 void
6438 pop_everything (void)
6440 if (ENABLE_SCOPE_CHECKING)
6441 verbatim ("XXX entering pop_everything ()\n");
6442 while (!toplevel_bindings_p ())
6444 if (current_binding_level->kind == sk_class)
6445 pop_nested_class ();
6446 else
6447 poplevel (0, 0, 0);
6449 if (ENABLE_SCOPE_CHECKING)
6450 verbatim ("XXX leaving pop_everything ()\n");
6453 /* Emit debugging information for using declarations and directives.
6454 If input tree is overloaded fn then emit debug info for all
6455 candidates. */
6457 void
6458 cp_emit_debug_info_for_using (tree t, tree context)
6460 /* Don't try to emit any debug information if we have errors. */
6461 if (seen_error ())
6462 return;
6464 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
6465 of a builtin function. */
6466 if (TREE_CODE (t) == FUNCTION_DECL
6467 && DECL_EXTERNAL (t)
6468 && DECL_BUILT_IN (t))
6469 return;
6471 /* Do not supply context to imported_module_or_decl, if
6472 it is a global namespace. */
6473 if (context == global_namespace)
6474 context = NULL_TREE;
6476 if (BASELINK_P (t))
6477 t = BASELINK_FUNCTIONS (t);
6479 /* FIXME: Handle TEMPLATE_DECLs. */
6480 for (t = OVL_CURRENT (t); t; t = OVL_NEXT (t))
6481 if (TREE_CODE (t) != TEMPLATE_DECL)
6483 if (building_stmt_list_p ())
6484 add_stmt (build_stmt (input_location, USING_STMT, t));
6485 else
6486 (*debug_hooks->imported_module_or_decl) (t, NULL_TREE, context, false);
6490 #include "gt-cp-name-lookup.h"