* doc/extend.texi (Java Exceptions): Remove.
[official-gcc.git] / gcc / cp / name-lookup.c
blob1bce63bb2dd75f8d92bb36767be361fd8e7edda3
1 /* Definitions for C++ name lookup routines.
2 Copyright (C) 2003-2016 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "cp-tree.h"
25 #include "timevar.h"
26 #include "stringpool.h"
27 #include "print-tree.h"
28 #include "attribs.h"
29 #include "debug.h"
30 #include "c-family/c-pragma.h"
31 #include "params.h"
32 #include "gcc-rich-location.h"
33 #include "spellcheck-tree.h"
34 #include "parser.h"
36 /* The bindings for a particular name in a particular scope. */
38 struct scope_binding {
39 tree value;
40 tree type;
42 #define EMPTY_SCOPE_BINDING { NULL_TREE, NULL_TREE }
44 static cp_binding_level *innermost_nonclass_level (void);
45 static cxx_binding *binding_for_name (cp_binding_level *, tree);
46 static tree push_overloaded_decl (tree, int, bool);
47 static bool lookup_using_namespace (tree, struct scope_binding *, tree,
48 tree, int);
49 static bool qualified_lookup_using_namespace (tree, tree,
50 struct scope_binding *, int);
51 static tree lookup_type_current_level (tree);
52 static tree push_using_directive (tree);
53 static tree lookup_extern_c_fun_in_all_ns (tree);
54 static void diagnose_name_conflict (tree, tree);
56 /* The :: namespace. */
58 tree global_namespace;
60 /* The name of the anonymous namespace, throughout this translation
61 unit. */
62 static GTY(()) tree anonymous_namespace_name;
64 /* Initialize anonymous_namespace_name if necessary, and return it. */
66 static tree
67 get_anonymous_namespace_name (void)
69 if (!anonymous_namespace_name)
71 /* We used to use get_file_function_name here, but that isn't
72 necessary now that anonymous namespace typeinfos
73 are !TREE_PUBLIC, and thus compared by address. */
74 /* The demangler expects anonymous namespaces to be called
75 something starting with '_GLOBAL__N_'. */
76 anonymous_namespace_name = get_identifier ("_GLOBAL__N_1");
78 return anonymous_namespace_name;
81 /* Compute the chain index of a binding_entry given the HASH value of its
82 name and the total COUNT of chains. COUNT is assumed to be a power
83 of 2. */
85 #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
87 /* A free list of "binding_entry"s awaiting for re-use. */
89 static GTY((deletable)) binding_entry free_binding_entry = NULL;
91 /* Create a binding_entry object for (NAME, TYPE). */
93 static inline binding_entry
94 binding_entry_make (tree name, tree type)
96 binding_entry entry;
98 if (free_binding_entry)
100 entry = free_binding_entry;
101 free_binding_entry = entry->chain;
103 else
104 entry = ggc_alloc<binding_entry_s> ();
106 entry->name = name;
107 entry->type = type;
108 entry->chain = NULL;
110 return entry;
113 /* Put ENTRY back on the free list. */
114 #if 0
115 static inline void
116 binding_entry_free (binding_entry entry)
118 entry->name = NULL;
119 entry->type = NULL;
120 entry->chain = free_binding_entry;
121 free_binding_entry = entry;
123 #endif
125 /* The datatype used to implement the mapping from names to types at
126 a given scope. */
127 struct GTY(()) binding_table_s {
128 /* Array of chains of "binding_entry"s */
129 binding_entry * GTY((length ("%h.chain_count"))) chain;
131 /* The number of chains in this table. This is the length of the
132 member "chain" considered as an array. */
133 size_t chain_count;
135 /* Number of "binding_entry"s in this table. */
136 size_t entry_count;
139 /* Construct TABLE with an initial CHAIN_COUNT. */
141 static inline void
142 binding_table_construct (binding_table table, size_t chain_count)
144 table->chain_count = chain_count;
145 table->entry_count = 0;
146 table->chain = ggc_cleared_vec_alloc<binding_entry> (table->chain_count);
149 /* Make TABLE's entries ready for reuse. */
150 #if 0
151 static void
152 binding_table_free (binding_table table)
154 size_t i;
155 size_t count;
157 if (table == NULL)
158 return;
160 for (i = 0, count = table->chain_count; i < count; ++i)
162 binding_entry temp = table->chain[i];
163 while (temp != NULL)
165 binding_entry entry = temp;
166 temp = entry->chain;
167 binding_entry_free (entry);
169 table->chain[i] = NULL;
171 table->entry_count = 0;
173 #endif
175 /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */
177 static inline binding_table
178 binding_table_new (size_t chain_count)
180 binding_table table = ggc_alloc<binding_table_s> ();
181 table->chain = NULL;
182 binding_table_construct (table, chain_count);
183 return table;
186 /* Expand TABLE to twice its current chain_count. */
188 static void
189 binding_table_expand (binding_table table)
191 const size_t old_chain_count = table->chain_count;
192 const size_t old_entry_count = table->entry_count;
193 const size_t new_chain_count = 2 * old_chain_count;
194 binding_entry *old_chains = table->chain;
195 size_t i;
197 binding_table_construct (table, new_chain_count);
198 for (i = 0; i < old_chain_count; ++i)
200 binding_entry entry = old_chains[i];
201 for (; entry != NULL; entry = old_chains[i])
203 const unsigned int hash = IDENTIFIER_HASH_VALUE (entry->name);
204 const size_t j = ENTRY_INDEX (hash, new_chain_count);
206 old_chains[i] = entry->chain;
207 entry->chain = table->chain[j];
208 table->chain[j] = entry;
211 table->entry_count = old_entry_count;
214 /* Insert a binding for NAME to TYPE into TABLE. */
216 static void
217 binding_table_insert (binding_table table, tree name, tree type)
219 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
220 const size_t i = ENTRY_INDEX (hash, table->chain_count);
221 binding_entry entry = binding_entry_make (name, type);
223 entry->chain = table->chain[i];
224 table->chain[i] = entry;
225 ++table->entry_count;
227 if (3 * table->chain_count < 5 * table->entry_count)
228 binding_table_expand (table);
231 /* Return the binding_entry, if any, that maps NAME. */
233 binding_entry
234 binding_table_find (binding_table table, tree name)
236 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
237 binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)];
239 while (entry != NULL && entry->name != name)
240 entry = entry->chain;
242 return entry;
245 /* Apply PROC -- with DATA -- to all entries in TABLE. */
247 void
248 binding_table_foreach (binding_table table, bt_foreach_proc proc, void *data)
250 size_t chain_count;
251 size_t i;
253 if (!table)
254 return;
256 chain_count = table->chain_count;
257 for (i = 0; i < chain_count; ++i)
259 binding_entry entry = table->chain[i];
260 for (; entry != NULL; entry = entry->chain)
261 proc (entry, data);
265 #ifndef ENABLE_SCOPE_CHECKING
266 # define ENABLE_SCOPE_CHECKING 0
267 #else
268 # define ENABLE_SCOPE_CHECKING 1
269 #endif
271 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
273 static GTY((deletable)) cxx_binding *free_bindings;
275 /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
276 field to NULL. */
278 static inline void
279 cxx_binding_init (cxx_binding *binding, tree value, tree type)
281 binding->value = value;
282 binding->type = type;
283 binding->previous = NULL;
286 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
288 static cxx_binding *
289 cxx_binding_make (tree value, tree type)
291 cxx_binding *binding;
292 if (free_bindings)
294 binding = free_bindings;
295 free_bindings = binding->previous;
297 else
298 binding = ggc_alloc<cxx_binding> ();
300 cxx_binding_init (binding, value, type);
302 return binding;
305 /* Put BINDING back on the free list. */
307 static inline void
308 cxx_binding_free (cxx_binding *binding)
310 binding->scope = NULL;
311 binding->previous = free_bindings;
312 free_bindings = binding;
315 /* Create a new binding for NAME (with the indicated VALUE and TYPE
316 bindings) in the class scope indicated by SCOPE. */
318 static cxx_binding *
319 new_class_binding (tree name, tree value, tree type, cp_binding_level *scope)
321 cp_class_binding cb = {cxx_binding_make (value, type), name};
322 cxx_binding *binding = cb.base;
323 vec_safe_push (scope->class_shadowed, cb);
324 binding->scope = scope;
325 return binding;
328 /* Make DECL the innermost binding for ID. The LEVEL is the binding
329 level at which this declaration is being bound. */
331 void
332 push_binding (tree id, tree decl, cp_binding_level* level)
334 cxx_binding *binding;
336 if (level != class_binding_level)
338 binding = cxx_binding_make (decl, NULL_TREE);
339 binding->scope = level;
341 else
342 binding = new_class_binding (id, decl, /*type=*/NULL_TREE, level);
344 /* Now, fill in the binding information. */
345 binding->previous = IDENTIFIER_BINDING (id);
346 INHERITED_VALUE_BINDING_P (binding) = 0;
347 LOCAL_BINDING_P (binding) = (level != class_binding_level);
349 /* And put it on the front of the list of bindings for ID. */
350 IDENTIFIER_BINDING (id) = binding;
353 /* Remove the binding for DECL which should be the innermost binding
354 for ID. */
356 void
357 pop_binding (tree id, tree decl)
359 cxx_binding *binding;
361 if (id == NULL_TREE)
362 /* It's easiest to write the loops that call this function without
363 checking whether or not the entities involved have names. We
364 get here for such an entity. */
365 return;
367 /* Get the innermost binding for ID. */
368 binding = IDENTIFIER_BINDING (id);
370 /* The name should be bound. */
371 gcc_assert (binding != NULL);
373 /* The DECL will be either the ordinary binding or the type
374 binding for this identifier. Remove that binding. */
375 if (binding->value == decl)
376 binding->value = NULL_TREE;
377 else
379 gcc_assert (binding->type == decl);
380 binding->type = NULL_TREE;
383 if (!binding->value && !binding->type)
385 /* We're completely done with the innermost binding for this
386 identifier. Unhook it from the list of bindings. */
387 IDENTIFIER_BINDING (id) = binding->previous;
389 /* Add it to the free list. */
390 cxx_binding_free (binding);
394 /* Remove the bindings for the decls of the current level and leave
395 the current scope. */
397 void
398 pop_bindings_and_leave_scope (void)
400 for (tree t = getdecls (); t; t = DECL_CHAIN (t))
401 pop_binding (DECL_NAME (t), t);
402 leave_scope ();
405 /* Strip non dependent using declarations. If DECL is dependent,
406 surreptitiously create a typename_type and return it. */
408 tree
409 strip_using_decl (tree decl)
411 if (decl == NULL_TREE)
412 return NULL_TREE;
414 while (TREE_CODE (decl) == USING_DECL && !DECL_DEPENDENT_P (decl))
415 decl = USING_DECL_DECLS (decl);
417 if (TREE_CODE (decl) == USING_DECL && DECL_DEPENDENT_P (decl)
418 && USING_DECL_TYPENAME_P (decl))
420 /* We have found a type introduced by a using
421 declaration at class scope that refers to a dependent
422 type.
424 using typename :: [opt] nested-name-specifier unqualified-id ;
426 decl = make_typename_type (TREE_TYPE (decl),
427 DECL_NAME (decl),
428 typename_type, tf_error);
429 if (decl != error_mark_node)
430 decl = TYPE_NAME (decl);
433 return decl;
436 /* BINDING records an existing declaration for a name in the current scope.
437 But, DECL is another declaration for that same identifier in the
438 same scope. This is the `struct stat' hack whereby a non-typedef
439 class name or enum-name can be bound at the same level as some other
440 kind of entity.
441 3.3.7/1
443 A class name (9.1) or enumeration name (7.2) can be hidden by the
444 name of an object, function, or enumerator declared in the same scope.
445 If a class or enumeration name and an object, function, or enumerator
446 are declared in the same scope (in any order) with the same name, the
447 class or enumeration name is hidden wherever the object, function, or
448 enumerator name is visible.
450 It's the responsibility of the caller to check that
451 inserting this name is valid here. Returns nonzero if the new binding
452 was successful. */
454 static bool
455 supplement_binding_1 (cxx_binding *binding, tree decl)
457 tree bval = binding->value;
458 bool ok = true;
459 tree target_bval = strip_using_decl (bval);
460 tree target_decl = strip_using_decl (decl);
462 if (TREE_CODE (target_decl) == TYPE_DECL && DECL_ARTIFICIAL (target_decl)
463 && target_decl != target_bval
464 && (TREE_CODE (target_bval) != TYPE_DECL
465 /* We allow pushing an enum multiple times in a class
466 template in order to handle late matching of underlying
467 type on an opaque-enum-declaration followed by an
468 enum-specifier. */
469 || (processing_template_decl
470 && TREE_CODE (TREE_TYPE (target_decl)) == ENUMERAL_TYPE
471 && TREE_CODE (TREE_TYPE (target_bval)) == ENUMERAL_TYPE
472 && (dependent_type_p (ENUM_UNDERLYING_TYPE
473 (TREE_TYPE (target_decl)))
474 || dependent_type_p (ENUM_UNDERLYING_TYPE
475 (TREE_TYPE (target_bval)))))))
476 /* The new name is the type name. */
477 binding->type = decl;
478 else if (/* TARGET_BVAL is null when push_class_level_binding moves
479 an inherited type-binding out of the way to make room
480 for a new value binding. */
481 !target_bval
482 /* TARGET_BVAL is error_mark_node when TARGET_DECL's name
483 has been used in a non-class scope prior declaration.
484 In that case, we should have already issued a
485 diagnostic; for graceful error recovery purpose, pretend
486 this was the intended declaration for that name. */
487 || target_bval == error_mark_node
488 /* If TARGET_BVAL is anticipated but has not yet been
489 declared, pretend it is not there at all. */
490 || (TREE_CODE (target_bval) == FUNCTION_DECL
491 && DECL_ANTICIPATED (target_bval)
492 && !DECL_HIDDEN_FRIEND_P (target_bval)))
493 binding->value = decl;
494 else if (TREE_CODE (target_bval) == TYPE_DECL
495 && DECL_ARTIFICIAL (target_bval)
496 && target_decl != target_bval
497 && (TREE_CODE (target_decl) != TYPE_DECL
498 || same_type_p (TREE_TYPE (target_decl),
499 TREE_TYPE (target_bval))))
501 /* The old binding was a type name. It was placed in
502 VALUE field because it was thought, at the point it was
503 declared, to be the only entity with such a name. Move the
504 type name into the type slot; it is now hidden by the new
505 binding. */
506 binding->type = bval;
507 binding->value = decl;
508 binding->value_is_inherited = false;
510 else if (TREE_CODE (target_bval) == TYPE_DECL
511 && TREE_CODE (target_decl) == TYPE_DECL
512 && DECL_NAME (target_decl) == DECL_NAME (target_bval)
513 && binding->scope->kind != sk_class
514 && (same_type_p (TREE_TYPE (target_decl), TREE_TYPE (target_bval))
515 /* If either type involves template parameters, we must
516 wait until instantiation. */
517 || uses_template_parms (TREE_TYPE (target_decl))
518 || uses_template_parms (TREE_TYPE (target_bval))))
519 /* We have two typedef-names, both naming the same type to have
520 the same name. In general, this is OK because of:
522 [dcl.typedef]
524 In a given scope, a typedef specifier can be used to redefine
525 the name of any type declared in that scope to refer to the
526 type to which it already refers.
528 However, in class scopes, this rule does not apply due to the
529 stricter language in [class.mem] prohibiting redeclarations of
530 members. */
531 ok = false;
532 /* There can be two block-scope declarations of the same variable,
533 so long as they are `extern' declarations. However, there cannot
534 be two declarations of the same static data member:
536 [class.mem]
538 A member shall not be declared twice in the
539 member-specification. */
540 else if (VAR_P (target_decl)
541 && VAR_P (target_bval)
542 && DECL_EXTERNAL (target_decl) && DECL_EXTERNAL (target_bval)
543 && !DECL_CLASS_SCOPE_P (target_decl))
545 duplicate_decls (decl, binding->value, /*newdecl_is_friend=*/false);
546 ok = false;
548 else if (TREE_CODE (decl) == NAMESPACE_DECL
549 && TREE_CODE (bval) == NAMESPACE_DECL
550 && DECL_NAMESPACE_ALIAS (decl)
551 && DECL_NAMESPACE_ALIAS (bval)
552 && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl))
553 /* [namespace.alias]
555 In a declarative region, a namespace-alias-definition can be
556 used to redefine a namespace-alias declared in that declarative
557 region to refer only to the namespace to which it already
558 refers. */
559 ok = false;
560 else if (maybe_remove_implicit_alias (bval))
562 /* There was a mangling compatibility alias using this mangled name,
563 but now we have a real decl that wants to use it instead. */
564 binding->value = decl;
566 else
568 diagnose_name_conflict (decl, bval);
569 ok = false;
572 return ok;
575 /* Diagnose a name conflict between DECL and BVAL. */
577 static void
578 diagnose_name_conflict (tree decl, tree bval)
580 if (TREE_CODE (decl) == TREE_CODE (bval)
581 && (TREE_CODE (decl) != TYPE_DECL
582 || (DECL_ARTIFICIAL (decl) && DECL_ARTIFICIAL (bval))
583 || (!DECL_ARTIFICIAL (decl) && !DECL_ARTIFICIAL (bval)))
584 && !is_overloaded_fn (decl))
585 error ("redeclaration of %q#D", decl);
586 else
587 error ("%q#D conflicts with a previous declaration", decl);
589 inform (location_of (bval), "previous declaration %q#D", bval);
592 /* Wrapper for supplement_binding_1. */
594 static bool
595 supplement_binding (cxx_binding *binding, tree decl)
597 bool ret;
598 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
599 ret = supplement_binding_1 (binding, decl);
600 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
601 return ret;
604 /* Add DECL to the list of things declared in B. */
606 static void
607 add_decl_to_level (tree decl, cp_binding_level *b)
609 /* We used to record virtual tables as if they were ordinary
610 variables, but no longer do so. */
611 gcc_assert (!(VAR_P (decl) && DECL_VIRTUAL_P (decl)));
613 if (TREE_CODE (decl) == NAMESPACE_DECL
614 && !DECL_NAMESPACE_ALIAS (decl))
616 DECL_CHAIN (decl) = b->namespaces;
617 b->namespaces = decl;
619 else
621 /* We build up the list in reverse order, and reverse it later if
622 necessary. */
623 TREE_CHAIN (decl) = b->names;
624 b->names = decl;
626 /* If appropriate, add decl to separate list of statics. We
627 include extern variables because they might turn out to be
628 static later. It's OK for this list to contain a few false
629 positives. */
630 if (b->kind == sk_namespace)
631 if ((VAR_P (decl)
632 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
633 || (TREE_CODE (decl) == FUNCTION_DECL
634 && (!TREE_PUBLIC (decl)
635 || decl_anon_ns_mem_p (decl)
636 || DECL_DECLARED_INLINE_P (decl))))
637 vec_safe_push (b->static_decls, decl);
641 /* Record a decl-node X as belonging to the current lexical scope.
642 Check for errors (such as an incompatible declaration for the same
643 name already seen in the same scope). IS_FRIEND is true if X is
644 declared as a friend.
646 Returns either X or an old decl for the same name.
647 If an old decl is returned, it may have been smashed
648 to agree with what X says. */
650 static tree
651 pushdecl_maybe_friend_1 (tree x, bool is_friend)
653 tree t;
654 tree name;
655 int need_new_binding;
657 if (x == error_mark_node)
658 return error_mark_node;
660 need_new_binding = 1;
662 if (DECL_TEMPLATE_PARM_P (x))
663 /* Template parameters have no context; they are not X::T even
664 when declared within a class or namespace. */
666 else
668 if (current_function_decl && x != current_function_decl
669 /* A local declaration for a function doesn't constitute
670 nesting. */
671 && TREE_CODE (x) != FUNCTION_DECL
672 /* A local declaration for an `extern' variable is in the
673 scope of the current namespace, not the current
674 function. */
675 && !(VAR_P (x) && DECL_EXTERNAL (x))
676 /* When parsing the parameter list of a function declarator,
677 don't set DECL_CONTEXT to an enclosing function. When we
678 push the PARM_DECLs in order to process the function body,
679 current_binding_level->this_entity will be set. */
680 && !(TREE_CODE (x) == PARM_DECL
681 && current_binding_level->kind == sk_function_parms
682 && current_binding_level->this_entity == NULL)
683 && !DECL_CONTEXT (x))
684 DECL_CONTEXT (x) = current_function_decl;
686 /* If this is the declaration for a namespace-scope function,
687 but the declaration itself is in a local scope, mark the
688 declaration. */
689 if (TREE_CODE (x) == FUNCTION_DECL
690 && DECL_NAMESPACE_SCOPE_P (x)
691 && current_function_decl
692 && x != current_function_decl)
693 DECL_LOCAL_FUNCTION_P (x) = 1;
696 name = DECL_NAME (x);
697 if (name)
699 int different_binding_level = 0;
701 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
702 name = TREE_OPERAND (name, 0);
704 /* In case this decl was explicitly namespace-qualified, look it
705 up in its namespace context. */
706 if (DECL_NAMESPACE_SCOPE_P (x) && namespace_bindings_p ())
707 t = namespace_binding (name, DECL_CONTEXT (x));
708 else
709 t = lookup_name_innermost_nonclass_level (name);
711 /* [basic.link] If there is a visible declaration of an entity
712 with linkage having the same name and type, ignoring entities
713 declared outside the innermost enclosing namespace scope, the
714 block scope declaration declares that same entity and
715 receives the linkage of the previous declaration. */
716 if (! t && current_function_decl && x != current_function_decl
717 && VAR_OR_FUNCTION_DECL_P (x)
718 && DECL_EXTERNAL (x))
720 /* Look in block scope. */
721 t = innermost_non_namespace_value (name);
722 /* Or in the innermost namespace. */
723 if (! t)
724 t = namespace_binding (name, DECL_CONTEXT (x));
725 /* Does it have linkage? Note that if this isn't a DECL, it's an
726 OVERLOAD, which is OK. */
727 if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t)))
728 t = NULL_TREE;
729 if (t)
730 different_binding_level = 1;
733 /* If we are declaring a function, and the result of name-lookup
734 was an OVERLOAD, look for an overloaded instance that is
735 actually the same as the function we are declaring. (If
736 there is one, we have to merge our declaration with the
737 previous declaration.) */
738 if (t && TREE_CODE (t) == OVERLOAD)
740 tree match;
742 if (TREE_CODE (x) == FUNCTION_DECL)
743 for (match = t; match; match = OVL_NEXT (match))
745 if (decls_match (OVL_CURRENT (match), x))
746 break;
748 else
749 /* Just choose one. */
750 match = t;
752 if (match)
753 t = OVL_CURRENT (match);
754 else
755 t = NULL_TREE;
758 if (t && t != error_mark_node)
760 if (different_binding_level)
762 if (decls_match (x, t))
763 /* The standard only says that the local extern
764 inherits linkage from the previous decl; in
765 particular, default args are not shared. Add
766 the decl into a hash table to make sure only
767 the previous decl in this case is seen by the
768 middle end. */
770 struct cxx_int_tree_map *h;
772 TREE_PUBLIC (x) = TREE_PUBLIC (t);
774 if (cp_function_chain->extern_decl_map == NULL)
775 cp_function_chain->extern_decl_map
776 = hash_table<cxx_int_tree_map_hasher>::create_ggc (20);
778 h = ggc_alloc<cxx_int_tree_map> ();
779 h->uid = DECL_UID (x);
780 h->to = t;
781 cxx_int_tree_map **loc = cp_function_chain->extern_decl_map
782 ->find_slot (h, INSERT);
783 *loc = h;
786 else if (TREE_CODE (t) == PARM_DECL)
788 /* Check for duplicate params. */
789 tree d = duplicate_decls (x, t, is_friend);
790 if (d)
791 return d;
793 else if ((DECL_EXTERN_C_FUNCTION_P (x)
794 || DECL_FUNCTION_TEMPLATE_P (x))
795 && is_overloaded_fn (t))
796 /* Don't do anything just yet. */;
797 else if (t == wchar_decl_node)
799 if (! DECL_IN_SYSTEM_HEADER (x))
800 pedwarn (input_location, OPT_Wpedantic, "redeclaration of %<wchar_t%> as %qT",
801 TREE_TYPE (x));
803 /* Throw away the redeclaration. */
804 return t;
806 else
808 tree olddecl = duplicate_decls (x, t, is_friend);
810 /* If the redeclaration failed, we can stop at this
811 point. */
812 if (olddecl == error_mark_node)
813 return error_mark_node;
815 if (olddecl)
817 if (TREE_CODE (t) == TYPE_DECL)
818 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
820 return t;
822 else if (DECL_MAIN_P (x) && TREE_CODE (t) == FUNCTION_DECL)
824 /* A redeclaration of main, but not a duplicate of the
825 previous one.
827 [basic.start.main]
829 This function shall not be overloaded. */
830 error ("invalid redeclaration of %q+D", t);
831 error ("as %qD", x);
832 /* We don't try to push this declaration since that
833 causes a crash. */
834 return x;
839 /* If x has C linkage-specification, (extern "C"),
840 lookup its binding, in case it's already bound to an object.
841 The lookup is done in all namespaces.
842 If we find an existing binding, make sure it has the same
843 exception specification as x, otherwise, bail in error [7.5, 7.6]. */
844 if ((TREE_CODE (x) == FUNCTION_DECL)
845 && DECL_EXTERN_C_P (x)
846 /* We should ignore declarations happening in system headers. */
847 && !DECL_ARTIFICIAL (x)
848 && !DECL_IN_SYSTEM_HEADER (x))
850 tree previous = lookup_extern_c_fun_in_all_ns (x);
851 if (previous
852 && !DECL_ARTIFICIAL (previous)
853 && !DECL_IN_SYSTEM_HEADER (previous)
854 && DECL_CONTEXT (previous) != DECL_CONTEXT (x))
856 /* In case either x or previous is declared to throw an exception,
857 make sure both exception specifications are equal. */
858 if (decls_match (x, previous))
860 tree x_exception_spec = NULL_TREE;
861 tree previous_exception_spec = NULL_TREE;
863 x_exception_spec =
864 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (x));
865 previous_exception_spec =
866 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (previous));
867 if (!comp_except_specs (previous_exception_spec,
868 x_exception_spec,
869 ce_normal))
871 pedwarn (input_location, 0,
872 "declaration of %q#D with C language linkage",
874 pedwarn (DECL_SOURCE_LOCATION (previous), 0,
875 "conflicts with previous declaration %q#D",
876 previous);
877 pedwarn (input_location, 0,
878 "due to different exception specifications");
879 return error_mark_node;
881 if (DECL_ASSEMBLER_NAME_SET_P (previous))
882 SET_DECL_ASSEMBLER_NAME (x,
883 DECL_ASSEMBLER_NAME (previous));
885 else
887 pedwarn (input_location, 0,
888 "declaration of %q#D with C language linkage", x);
889 pedwarn (DECL_SOURCE_LOCATION (previous), 0,
890 "conflicts with previous declaration %q#D",
891 previous);
896 check_template_shadow (x);
898 /* If this is a function conjured up by the back end, massage it
899 so it looks friendly. */
900 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x))
902 retrofit_lang_decl (x);
903 SET_DECL_LANGUAGE (x, lang_c);
906 t = x;
907 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x))
909 t = push_overloaded_decl (x, PUSH_LOCAL, is_friend);
910 if (!namespace_bindings_p ())
911 /* We do not need to create a binding for this name;
912 push_overloaded_decl will have already done so if
913 necessary. */
914 need_new_binding = 0;
916 else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
918 t = push_overloaded_decl (x, PUSH_GLOBAL, is_friend);
919 if (t == x)
920 add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
923 if (DECL_DECLARES_FUNCTION_P (t))
925 check_default_args (t);
927 if (is_friend && t == x && !flag_friend_injection)
929 /* This is a new friend declaration of a function or a
930 function template, so hide it from ordinary function
931 lookup. */
932 DECL_ANTICIPATED (t) = 1;
933 DECL_HIDDEN_FRIEND_P (t) = 1;
937 if (t != x || DECL_FUNCTION_TEMPLATE_P (t))
938 return t;
940 /* If declaring a type as a typedef, copy the type (unless we're
941 at line 0), and install this TYPE_DECL as the new type's typedef
942 name. See the extensive comment of set_underlying_type (). */
943 if (TREE_CODE (x) == TYPE_DECL)
945 tree type = TREE_TYPE (x);
947 if (DECL_IS_BUILTIN (x)
948 || (TREE_TYPE (x) != error_mark_node
949 && TYPE_NAME (type) != x
950 /* We don't want to copy the type when all we're
951 doing is making a TYPE_DECL for the purposes of
952 inlining. */
953 && (!TYPE_NAME (type)
954 || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x))))
955 set_underlying_type (x);
957 if (type != error_mark_node
958 && TYPE_IDENTIFIER (type))
959 set_identifier_type_value (DECL_NAME (x), x);
961 /* If this is a locally defined typedef in a function that
962 is not a template instantation, record it to implement
963 -Wunused-local-typedefs. */
964 if (!instantiating_current_function_p ())
965 record_locally_defined_typedef (x);
968 /* Multiple external decls of the same identifier ought to match.
970 We get warnings about inline functions where they are defined.
971 We get warnings about other functions from push_overloaded_decl.
973 Avoid duplicate warnings where they are used. */
974 if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
976 tree decl;
978 decl = IDENTIFIER_NAMESPACE_VALUE (name);
979 if (decl && TREE_CODE (decl) == OVERLOAD)
980 decl = OVL_FUNCTION (decl);
982 if (decl && decl != error_mark_node
983 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl))
984 /* If different sort of thing, we already gave an error. */
985 && TREE_CODE (decl) == TREE_CODE (x)
986 && !comptypes (TREE_TYPE (x), TREE_TYPE (decl),
987 COMPARE_REDECLARATION))
989 if (permerror (input_location, "type mismatch with previous "
990 "external decl of %q#D", x))
991 inform (DECL_SOURCE_LOCATION (decl),
992 "previous external decl of %q#D", decl);
996 /* This name is new in its binding level.
997 Install the new declaration and return it. */
998 if (namespace_bindings_p ())
1000 /* Install a global value. */
1002 /* If the first global decl has external linkage,
1003 warn if we later see static one. */
1004 if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
1005 TREE_PUBLIC (name) = 1;
1007 /* Bind the name for the entity. */
1008 if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
1009 && t != NULL_TREE)
1010 && (TREE_CODE (x) == TYPE_DECL
1011 || VAR_P (x)
1012 || TREE_CODE (x) == NAMESPACE_DECL
1013 || TREE_CODE (x) == CONST_DECL
1014 || TREE_CODE (x) == TEMPLATE_DECL))
1015 SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
1017 /* If new decl is `static' and an `extern' was seen previously,
1018 warn about it. */
1019 if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
1020 warn_extern_redeclared_static (x, t);
1022 else
1024 /* Here to install a non-global value. */
1025 tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
1026 tree oldlocal = NULL_TREE;
1027 cp_binding_level *oldscope = NULL;
1028 cxx_binding *oldbinding = outer_binding (name, NULL, true);
1029 if (oldbinding)
1031 oldlocal = oldbinding->value;
1032 oldscope = oldbinding->scope;
1035 if (need_new_binding)
1037 push_local_binding (name, x, 0);
1038 /* Because push_local_binding will hook X on to the
1039 current_binding_level's name list, we don't want to
1040 do that again below. */
1041 need_new_binding = 0;
1044 /* If this is a TYPE_DECL, push it into the type value slot. */
1045 if (TREE_CODE (x) == TYPE_DECL)
1046 set_identifier_type_value (name, x);
1048 /* Clear out any TYPE_DECL shadowed by a namespace so that
1049 we won't think this is a type. The C struct hack doesn't
1050 go through namespaces. */
1051 if (TREE_CODE (x) == NAMESPACE_DECL)
1052 set_identifier_type_value (name, NULL_TREE);
1054 if (oldlocal)
1056 tree d = oldlocal;
1058 while (oldlocal
1059 && VAR_P (oldlocal)
1060 && DECL_DEAD_FOR_LOCAL (oldlocal))
1061 oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal);
1063 if (oldlocal == NULL_TREE)
1064 oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d));
1067 /* If this is an extern function declaration, see if we
1068 have a global definition or declaration for the function. */
1069 if (oldlocal == NULL_TREE
1070 && DECL_EXTERNAL (x)
1071 && oldglobal != NULL_TREE
1072 && TREE_CODE (x) == FUNCTION_DECL
1073 && TREE_CODE (oldglobal) == FUNCTION_DECL)
1075 /* We have one. Their types must agree. */
1076 if (decls_match (x, oldglobal))
1077 /* OK */;
1078 else
1080 warning (0, "extern declaration of %q#D doesn%'t match", x);
1081 warning_at (DECL_SOURCE_LOCATION (oldglobal), 0,
1082 "global declaration %q#D", oldglobal);
1085 /* If we have a local external declaration,
1086 and no file-scope declaration has yet been seen,
1087 then if we later have a file-scope decl it must not be static. */
1088 if (oldlocal == NULL_TREE
1089 && oldglobal == NULL_TREE
1090 && DECL_EXTERNAL (x)
1091 && TREE_PUBLIC (x))
1092 TREE_PUBLIC (name) = 1;
1094 /* Don't complain about the parms we push and then pop
1095 while tentatively parsing a function declarator. */
1096 if (TREE_CODE (x) == PARM_DECL && DECL_CONTEXT (x) == NULL_TREE)
1097 /* Ignore. */;
1099 /* Warn if shadowing an argument at the top level of the body. */
1100 else if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
1101 /* Inline decls shadow nothing. */
1102 && !DECL_FROM_INLINE (x)
1103 && (TREE_CODE (oldlocal) == PARM_DECL
1104 || VAR_P (oldlocal)
1105 /* If the old decl is a type decl, only warn if the
1106 old decl is an explicit typedef or if both the old
1107 and new decls are type decls. */
1108 || (TREE_CODE (oldlocal) == TYPE_DECL
1109 && (!DECL_ARTIFICIAL (oldlocal)
1110 || TREE_CODE (x) == TYPE_DECL)))
1111 /* Don't check for internally generated vars unless
1112 it's an implicit typedef (see create_implicit_typedef
1113 in decl.c). */
1114 && (!DECL_ARTIFICIAL (x) || DECL_IMPLICIT_TYPEDEF_P (x)))
1116 bool nowarn = false;
1118 /* Don't complain if it's from an enclosing function. */
1119 if (DECL_CONTEXT (oldlocal) == current_function_decl
1120 && TREE_CODE (x) != PARM_DECL
1121 && TREE_CODE (oldlocal) == PARM_DECL)
1123 /* Go to where the parms should be and see if we find
1124 them there. */
1125 cp_binding_level *b = current_binding_level->level_chain;
1127 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
1128 /* Skip the ctor/dtor cleanup level. */
1129 b = b->level_chain;
1131 /* ARM $8.3 */
1132 if (b->kind == sk_function_parms)
1134 error ("declaration of %q#D shadows a parameter", x);
1135 nowarn = true;
1139 /* The local structure or class can't use parameters of
1140 the containing function anyway. */
1141 if (DECL_CONTEXT (oldlocal) != current_function_decl)
1143 cp_binding_level *scope = current_binding_level;
1144 tree context = DECL_CONTEXT (oldlocal);
1145 for (; scope; scope = scope->level_chain)
1147 if (scope->kind == sk_function_parms
1148 && scope->this_entity == context)
1149 break;
1150 if (scope->kind == sk_class
1151 && !LAMBDA_TYPE_P (scope->this_entity))
1153 nowarn = true;
1154 break;
1158 /* Error if redeclaring a local declared in a
1159 for-init-statement or in the condition of an if or
1160 switch statement when the new declaration is in the
1161 outermost block of the controlled statement.
1162 Redeclaring a variable from a for or while condition is
1163 detected elsewhere. */
1164 else if (VAR_P (oldlocal)
1165 && oldscope == current_binding_level->level_chain
1166 && (oldscope->kind == sk_cond
1167 || oldscope->kind == sk_for))
1169 error ("redeclaration of %q#D", x);
1170 inform (DECL_SOURCE_LOCATION (oldlocal),
1171 "%q#D previously declared here", oldlocal);
1172 nowarn = true;
1174 /* C++11:
1175 3.3.3/3: The name declared in an exception-declaration (...)
1176 shall not be redeclared in the outermost block of the handler.
1177 3.3.3/2: A parameter name shall not be redeclared (...) in
1178 the outermost block of any handler associated with a
1179 function-try-block.
1180 3.4.1/15: The function parameter names shall not be redeclared
1181 in the exception-declaration nor in the outermost block of a
1182 handler for the function-try-block. */
1183 else if ((VAR_P (oldlocal)
1184 && oldscope == current_binding_level->level_chain
1185 && oldscope->kind == sk_catch)
1186 || (TREE_CODE (oldlocal) == PARM_DECL
1187 && (current_binding_level->kind == sk_catch
1188 || (current_binding_level->level_chain->kind
1189 == sk_catch))
1190 && in_function_try_handler))
1192 if (permerror (input_location, "redeclaration of %q#D", x))
1193 inform (DECL_SOURCE_LOCATION (oldlocal),
1194 "%q#D previously declared here", oldlocal);
1195 nowarn = true;
1198 if (warn_shadow && !nowarn)
1200 bool warned;
1202 if (TREE_CODE (oldlocal) == PARM_DECL)
1203 warned = warning_at (input_location, OPT_Wshadow,
1204 "declaration of %q#D shadows a parameter", x);
1205 else if (is_capture_proxy (oldlocal))
1206 warned = warning_at (input_location, OPT_Wshadow,
1207 "declaration of %qD shadows a lambda capture",
1209 else
1210 warned = warning_at (input_location, OPT_Wshadow,
1211 "declaration of %qD shadows a previous local",
1214 if (warned)
1215 inform (DECL_SOURCE_LOCATION (oldlocal),
1216 "shadowed declaration is here");
1220 /* Maybe warn if shadowing something else. */
1221 else if (warn_shadow && !DECL_EXTERNAL (x)
1222 /* No shadow warnings for internally generated vars unless
1223 it's an implicit typedef (see create_implicit_typedef
1224 in decl.c). */
1225 && (! DECL_ARTIFICIAL (x) || DECL_IMPLICIT_TYPEDEF_P (x))
1226 /* No shadow warnings for vars made for inlining. */
1227 && ! DECL_FROM_INLINE (x))
1229 tree member;
1231 if (nonlambda_method_basetype ())
1232 member = lookup_member (current_nonlambda_class_type (),
1233 name,
1234 /*protect=*/0,
1235 /*want_type=*/false,
1236 tf_warning_or_error);
1237 else
1238 member = NULL_TREE;
1240 if (member && !TREE_STATIC (member))
1242 if (BASELINK_P (member))
1243 member = BASELINK_FUNCTIONS (member);
1244 member = OVL_CURRENT (member);
1246 /* Do not warn if a variable shadows a function, unless
1247 the variable is a function or a pointer-to-function. */
1248 if (TREE_CODE (member) != FUNCTION_DECL
1249 || TREE_CODE (x) == FUNCTION_DECL
1250 || TYPE_PTRFN_P (TREE_TYPE (x))
1251 || TYPE_PTRMEMFUNC_P (TREE_TYPE (x)))
1253 if (warning_at (input_location, OPT_Wshadow,
1254 "declaration of %qD shadows a member of %qT",
1255 x, current_nonlambda_class_type ())
1256 && DECL_P (member))
1257 inform (DECL_SOURCE_LOCATION (member),
1258 "shadowed declaration is here");
1261 else if (oldglobal != NULL_TREE
1262 && (VAR_P (oldglobal)
1263 /* If the old decl is a type decl, only warn if the
1264 old decl is an explicit typedef or if both the
1265 old and new decls are type decls. */
1266 || (TREE_CODE (oldglobal) == TYPE_DECL
1267 && (!DECL_ARTIFICIAL (oldglobal)
1268 || TREE_CODE (x) == TYPE_DECL)))
1269 && !instantiating_current_function_p ())
1270 /* XXX shadow warnings in outer-more namespaces */
1272 if (warning_at (input_location, OPT_Wshadow,
1273 "declaration of %qD shadows a "
1274 "global declaration", x))
1275 inform (DECL_SOURCE_LOCATION (oldglobal),
1276 "shadowed declaration is here");
1281 if (VAR_P (x))
1282 maybe_register_incomplete_var (x);
1285 if (need_new_binding)
1286 add_decl_to_level (x,
1287 DECL_NAMESPACE_SCOPE_P (x)
1288 ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x))
1289 : current_binding_level);
1291 return x;
1294 /* Wrapper for pushdecl_maybe_friend_1. */
1296 tree
1297 pushdecl_maybe_friend (tree x, bool is_friend)
1299 tree ret;
1300 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
1301 ret = pushdecl_maybe_friend_1 (x, is_friend);
1302 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
1303 return ret;
1306 /* Record a decl-node X as belonging to the current lexical scope. */
1308 tree
1309 pushdecl (tree x)
1311 return pushdecl_maybe_friend (x, false);
1314 /* Enter DECL into the symbol table, if that's appropriate. Returns
1315 DECL, or a modified version thereof. */
1317 tree
1318 maybe_push_decl (tree decl)
1320 tree type = TREE_TYPE (decl);
1322 /* Add this decl to the current binding level, but not if it comes
1323 from another scope, e.g. a static member variable. TEM may equal
1324 DECL or it may be a previous decl of the same name. */
1325 if (decl == error_mark_node
1326 || (TREE_CODE (decl) != PARM_DECL
1327 && DECL_CONTEXT (decl) != NULL_TREE
1328 /* Definitions of namespace members outside their namespace are
1329 possible. */
1330 && !DECL_NAMESPACE_SCOPE_P (decl))
1331 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
1332 || type == unknown_type_node
1333 /* The declaration of a template specialization does not affect
1334 the functions available for overload resolution, so we do not
1335 call pushdecl. */
1336 || (TREE_CODE (decl) == FUNCTION_DECL
1337 && DECL_TEMPLATE_SPECIALIZATION (decl)))
1338 return decl;
1339 else
1340 return pushdecl (decl);
1343 /* Bind DECL to ID in the current_binding_level, assumed to be a local
1344 binding level. If PUSH_USING is set in FLAGS, we know that DECL
1345 doesn't really belong to this binding level, that it got here
1346 through a using-declaration. */
1348 void
1349 push_local_binding (tree id, tree decl, int flags)
1351 cp_binding_level *b;
1353 /* Skip over any local classes. This makes sense if we call
1354 push_local_binding with a friend decl of a local class. */
1355 b = innermost_nonclass_level ();
1357 if (lookup_name_innermost_nonclass_level (id))
1359 /* Supplement the existing binding. */
1360 if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
1361 /* It didn't work. Something else must be bound at this
1362 level. Do not add DECL to the list of things to pop
1363 later. */
1364 return;
1366 else
1367 /* Create a new binding. */
1368 push_binding (id, decl, b);
1370 if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
1371 /* We must put the OVERLOAD into a TREE_LIST since the
1372 TREE_CHAIN of an OVERLOAD is already used. Similarly for
1373 decls that got here through a using-declaration. */
1374 decl = build_tree_list (NULL_TREE, decl);
1376 /* And put DECL on the list of things declared by the current
1377 binding level. */
1378 add_decl_to_level (decl, b);
1381 /* Check to see whether or not DECL is a variable that would have been
1382 in scope under the ARM, but is not in scope under the ANSI/ISO
1383 standard. If so, issue an error message. If name lookup would
1384 work in both cases, but return a different result, this function
1385 returns the result of ANSI/ISO lookup. Otherwise, it returns
1386 DECL. */
1388 tree
1389 check_for_out_of_scope_variable (tree decl)
1391 tree shadowed;
1393 /* We only care about out of scope variables. */
1394 if (!(VAR_P (decl) && DECL_DEAD_FOR_LOCAL (decl)))
1395 return decl;
1397 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (decl)
1398 ? DECL_SHADOWED_FOR_VAR (decl) : NULL_TREE ;
1399 while (shadowed != NULL_TREE && VAR_P (shadowed)
1400 && DECL_DEAD_FOR_LOCAL (shadowed))
1401 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (shadowed)
1402 ? DECL_SHADOWED_FOR_VAR (shadowed) : NULL_TREE;
1403 if (!shadowed)
1404 shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl));
1405 if (shadowed)
1407 if (!DECL_ERROR_REPORTED (decl))
1409 warning (0, "name lookup of %qD changed", DECL_NAME (decl));
1410 warning_at (DECL_SOURCE_LOCATION (shadowed), 0,
1411 " matches this %qD under ISO standard rules",
1412 shadowed);
1413 warning_at (DECL_SOURCE_LOCATION (decl), 0,
1414 " matches this %qD under old rules", decl);
1415 DECL_ERROR_REPORTED (decl) = 1;
1417 return shadowed;
1420 /* If we have already complained about this declaration, there's no
1421 need to do it again. */
1422 if (DECL_ERROR_REPORTED (decl))
1423 return decl;
1425 DECL_ERROR_REPORTED (decl) = 1;
1427 if (TREE_TYPE (decl) == error_mark_node)
1428 return decl;
1430 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
1432 error ("name lookup of %qD changed for ISO %<for%> scoping",
1433 DECL_NAME (decl));
1434 error (" cannot use obsolete binding at %q+D because "
1435 "it has a destructor", decl);
1436 return error_mark_node;
1438 else
1440 permerror (input_location, "name lookup of %qD changed for ISO %<for%> scoping",
1441 DECL_NAME (decl));
1442 if (flag_permissive)
1443 permerror (DECL_SOURCE_LOCATION (decl),
1444 " using obsolete binding at %qD", decl);
1445 else
1447 static bool hint;
1448 if (!hint)
1450 inform (input_location, "(if you use %<-fpermissive%> G++ will accept your code)");
1451 hint = true;
1456 return decl;
1459 /* true means unconditionally make a BLOCK for the next level pushed. */
1461 static bool keep_next_level_flag;
1463 static int binding_depth = 0;
1465 static void
1466 indent (int depth)
1468 int i;
1470 for (i = 0; i < depth * 2; i++)
1471 putc (' ', stderr);
1474 /* Return a string describing the kind of SCOPE we have. */
1475 static const char *
1476 cp_binding_level_descriptor (cp_binding_level *scope)
1478 /* The order of this table must match the "scope_kind"
1479 enumerators. */
1480 static const char* scope_kind_names[] = {
1481 "block-scope",
1482 "cleanup-scope",
1483 "try-scope",
1484 "catch-scope",
1485 "for-scope",
1486 "function-parameter-scope",
1487 "class-scope",
1488 "namespace-scope",
1489 "template-parameter-scope",
1490 "template-explicit-spec-scope"
1492 const scope_kind kind = scope->explicit_spec_p
1493 ? sk_template_spec : scope->kind;
1495 return scope_kind_names[kind];
1498 /* Output a debugging information about SCOPE when performing
1499 ACTION at LINE. */
1500 static void
1501 cp_binding_level_debug (cp_binding_level *scope, int line, const char *action)
1503 const char *desc = cp_binding_level_descriptor (scope);
1504 if (scope->this_entity)
1505 verbatim ("%s %s(%E) %p %d\n", action, desc,
1506 scope->this_entity, (void *) scope, line);
1507 else
1508 verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
1511 /* Return the estimated initial size of the hashtable of a NAMESPACE
1512 scope. */
1514 static inline size_t
1515 namespace_scope_ht_size (tree ns)
1517 tree name = DECL_NAME (ns);
1519 return name == std_identifier
1520 ? NAMESPACE_STD_HT_SIZE
1521 : (name == global_scope_name
1522 ? GLOBAL_SCOPE_HT_SIZE
1523 : NAMESPACE_ORDINARY_HT_SIZE);
1526 /* A chain of binding_level structures awaiting reuse. */
1528 static GTY((deletable)) cp_binding_level *free_binding_level;
1530 /* Insert SCOPE as the innermost binding level. */
1532 void
1533 push_binding_level (cp_binding_level *scope)
1535 /* Add it to the front of currently active scopes stack. */
1536 scope->level_chain = current_binding_level;
1537 current_binding_level = scope;
1538 keep_next_level_flag = false;
1540 if (ENABLE_SCOPE_CHECKING)
1542 scope->binding_depth = binding_depth;
1543 indent (binding_depth);
1544 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
1545 "push");
1546 binding_depth++;
1550 /* Create a new KIND scope and make it the top of the active scopes stack.
1551 ENTITY is the scope of the associated C++ entity (namespace, class,
1552 function, C++0x enumeration); it is NULL otherwise. */
1554 cp_binding_level *
1555 begin_scope (scope_kind kind, tree entity)
1557 cp_binding_level *scope;
1559 /* Reuse or create a struct for this binding level. */
1560 if (!ENABLE_SCOPE_CHECKING && free_binding_level)
1562 scope = free_binding_level;
1563 free_binding_level = scope->level_chain;
1564 memset (scope, 0, sizeof (cp_binding_level));
1566 else
1567 scope = ggc_cleared_alloc<cp_binding_level> ();
1569 scope->this_entity = entity;
1570 scope->more_cleanups_ok = true;
1571 switch (kind)
1573 case sk_cleanup:
1574 scope->keep = true;
1575 break;
1577 case sk_template_spec:
1578 scope->explicit_spec_p = true;
1579 kind = sk_template_parms;
1580 /* Fall through. */
1581 case sk_template_parms:
1582 case sk_block:
1583 case sk_try:
1584 case sk_catch:
1585 case sk_for:
1586 case sk_cond:
1587 case sk_class:
1588 case sk_scoped_enum:
1589 case sk_function_parms:
1590 case sk_transaction:
1591 case sk_omp:
1592 scope->keep = keep_next_level_flag;
1593 break;
1595 case sk_namespace:
1596 NAMESPACE_LEVEL (entity) = scope;
1597 vec_alloc (scope->static_decls,
1598 (DECL_NAME (entity) == std_identifier
1599 || DECL_NAME (entity) == global_scope_name) ? 200 : 10);
1600 break;
1602 default:
1603 /* Should not happen. */
1604 gcc_unreachable ();
1605 break;
1607 scope->kind = kind;
1609 push_binding_level (scope);
1611 return scope;
1614 /* We're about to leave current scope. Pop the top of the stack of
1615 currently active scopes. Return the enclosing scope, now active. */
1617 cp_binding_level *
1618 leave_scope (void)
1620 cp_binding_level *scope = current_binding_level;
1622 if (scope->kind == sk_namespace && class_binding_level)
1623 current_binding_level = class_binding_level;
1625 /* We cannot leave a scope, if there are none left. */
1626 if (NAMESPACE_LEVEL (global_namespace))
1627 gcc_assert (!global_scope_p (scope));
1629 if (ENABLE_SCOPE_CHECKING)
1631 indent (--binding_depth);
1632 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
1633 "leave");
1636 /* Move one nesting level up. */
1637 current_binding_level = scope->level_chain;
1639 /* Namespace-scopes are left most probably temporarily, not
1640 completely; they can be reopened later, e.g. in namespace-extension
1641 or any name binding activity that requires us to resume a
1642 namespace. For classes, we cache some binding levels. For other
1643 scopes, we just make the structure available for reuse. */
1644 if (scope->kind != sk_namespace
1645 && scope->kind != sk_class)
1647 scope->level_chain = free_binding_level;
1648 gcc_assert (!ENABLE_SCOPE_CHECKING
1649 || scope->binding_depth == binding_depth);
1650 free_binding_level = scope;
1653 if (scope->kind == sk_class)
1655 /* Reset DEFINING_CLASS_P to allow for reuse of a
1656 class-defining scope in a non-defining context. */
1657 scope->defining_class_p = 0;
1659 /* Find the innermost enclosing class scope, and reset
1660 CLASS_BINDING_LEVEL appropriately. */
1661 class_binding_level = NULL;
1662 for (scope = current_binding_level; scope; scope = scope->level_chain)
1663 if (scope->kind == sk_class)
1665 class_binding_level = scope;
1666 break;
1670 return current_binding_level;
1673 static void
1674 resume_scope (cp_binding_level* b)
1676 /* Resuming binding levels is meant only for namespaces,
1677 and those cannot nest into classes. */
1678 gcc_assert (!class_binding_level);
1679 /* Also, resuming a non-directly nested namespace is a no-no. */
1680 gcc_assert (b->level_chain == current_binding_level);
1681 current_binding_level = b;
1682 if (ENABLE_SCOPE_CHECKING)
1684 b->binding_depth = binding_depth;
1685 indent (binding_depth);
1686 cp_binding_level_debug (b, LOCATION_LINE (input_location), "resume");
1687 binding_depth++;
1691 /* Return the innermost binding level that is not for a class scope. */
1693 static cp_binding_level *
1694 innermost_nonclass_level (void)
1696 cp_binding_level *b;
1698 b = current_binding_level;
1699 while (b->kind == sk_class)
1700 b = b->level_chain;
1702 return b;
1705 /* We're defining an object of type TYPE. If it needs a cleanup, but
1706 we're not allowed to add any more objects with cleanups to the current
1707 scope, create a new binding level. */
1709 void
1710 maybe_push_cleanup_level (tree type)
1712 if (type != error_mark_node
1713 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
1714 && current_binding_level->more_cleanups_ok == 0)
1716 begin_scope (sk_cleanup, NULL);
1717 current_binding_level->statement_list = push_stmt_list ();
1721 /* Return true if we are in the global binding level. */
1723 bool
1724 global_bindings_p (void)
1726 return global_scope_p (current_binding_level);
1729 /* True if we are currently in a toplevel binding level. This
1730 means either the global binding level or a namespace in a toplevel
1731 binding level. Since there are no non-toplevel namespace levels,
1732 this really means any namespace or template parameter level. We
1733 also include a class whose context is toplevel. */
1735 bool
1736 toplevel_bindings_p (void)
1738 cp_binding_level *b = innermost_nonclass_level ();
1740 return b->kind == sk_namespace || b->kind == sk_template_parms;
1743 /* True if this is a namespace scope, or if we are defining a class
1744 which is itself at namespace scope, or whose enclosing class is
1745 such a class, etc. */
1747 bool
1748 namespace_bindings_p (void)
1750 cp_binding_level *b = innermost_nonclass_level ();
1752 return b->kind == sk_namespace;
1755 /* True if the innermost non-class scope is a block scope. */
1757 bool
1758 local_bindings_p (void)
1760 cp_binding_level *b = innermost_nonclass_level ();
1761 return b->kind < sk_function_parms || b->kind == sk_omp;
1764 /* True if the current level needs to have a BLOCK made. */
1766 bool
1767 kept_level_p (void)
1769 return (current_binding_level->blocks != NULL_TREE
1770 || current_binding_level->keep
1771 || current_binding_level->kind == sk_cleanup
1772 || current_binding_level->names != NULL_TREE
1773 || current_binding_level->using_directives);
1776 /* Returns the kind of the innermost scope. */
1778 scope_kind
1779 innermost_scope_kind (void)
1781 return current_binding_level->kind;
1784 /* Returns true if this scope was created to store template parameters. */
1786 bool
1787 template_parm_scope_p (void)
1789 return innermost_scope_kind () == sk_template_parms;
1792 /* If KEEP is true, make a BLOCK node for the next binding level,
1793 unconditionally. Otherwise, use the normal logic to decide whether
1794 or not to create a BLOCK. */
1796 void
1797 keep_next_level (bool keep)
1799 keep_next_level_flag = keep;
1802 /* Return the list of declarations of the current level.
1803 Note that this list is in reverse order unless/until
1804 you nreverse it; and when you do nreverse it, you must
1805 store the result back using `storedecls' or you will lose. */
1807 tree
1808 getdecls (void)
1810 return current_binding_level->names;
1813 /* Return how many function prototypes we are currently nested inside. */
1816 function_parm_depth (void)
1818 int level = 0;
1819 cp_binding_level *b;
1821 for (b = current_binding_level;
1822 b->kind == sk_function_parms;
1823 b = b->level_chain)
1824 ++level;
1826 return level;
1829 /* For debugging. */
1830 static int no_print_functions = 0;
1831 static int no_print_builtins = 0;
1833 static void
1834 print_binding_level (cp_binding_level* lvl)
1836 tree t;
1837 int i = 0, len;
1838 fprintf (stderr, " blocks=%p", (void *) lvl->blocks);
1839 if (lvl->more_cleanups_ok)
1840 fprintf (stderr, " more-cleanups-ok");
1841 if (lvl->have_cleanups)
1842 fprintf (stderr, " have-cleanups");
1843 fprintf (stderr, "\n");
1844 if (lvl->names)
1846 fprintf (stderr, " names:\t");
1847 /* We can probably fit 3 names to a line? */
1848 for (t = lvl->names; t; t = TREE_CHAIN (t))
1850 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
1851 continue;
1852 if (no_print_builtins
1853 && (TREE_CODE (t) == TYPE_DECL)
1854 && DECL_IS_BUILTIN (t))
1855 continue;
1857 /* Function decls tend to have longer names. */
1858 if (TREE_CODE (t) == FUNCTION_DECL)
1859 len = 3;
1860 else
1861 len = 2;
1862 i += len;
1863 if (i > 6)
1865 fprintf (stderr, "\n\t");
1866 i = len;
1868 print_node_brief (stderr, "", t, 0);
1869 if (t == error_mark_node)
1870 break;
1872 if (i)
1873 fprintf (stderr, "\n");
1875 if (vec_safe_length (lvl->class_shadowed))
1877 size_t i;
1878 cp_class_binding *b;
1879 fprintf (stderr, " class-shadowed:");
1880 FOR_EACH_VEC_ELT (*lvl->class_shadowed, i, b)
1881 fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
1882 fprintf (stderr, "\n");
1884 if (lvl->type_shadowed)
1886 fprintf (stderr, " type-shadowed:");
1887 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
1889 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1891 fprintf (stderr, "\n");
1895 DEBUG_FUNCTION void
1896 debug (cp_binding_level &ref)
1898 print_binding_level (&ref);
1901 DEBUG_FUNCTION void
1902 debug (cp_binding_level *ptr)
1904 if (ptr)
1905 debug (*ptr);
1906 else
1907 fprintf (stderr, "<nil>\n");
1911 void
1912 print_other_binding_stack (cp_binding_level *stack)
1914 cp_binding_level *level;
1915 for (level = stack; !global_scope_p (level); level = level->level_chain)
1917 fprintf (stderr, "binding level %p\n", (void *) level);
1918 print_binding_level (level);
1922 void
1923 print_binding_stack (void)
1925 cp_binding_level *b;
1926 fprintf (stderr, "current_binding_level=%p\n"
1927 "class_binding_level=%p\n"
1928 "NAMESPACE_LEVEL (global_namespace)=%p\n",
1929 (void *) current_binding_level, (void *) class_binding_level,
1930 (void *) NAMESPACE_LEVEL (global_namespace));
1931 if (class_binding_level)
1933 for (b = class_binding_level; b; b = b->level_chain)
1934 if (b == current_binding_level)
1935 break;
1936 if (b)
1937 b = class_binding_level;
1938 else
1939 b = current_binding_level;
1941 else
1942 b = current_binding_level;
1943 print_other_binding_stack (b);
1944 fprintf (stderr, "global:\n");
1945 print_binding_level (NAMESPACE_LEVEL (global_namespace));
1948 /* Return the type associated with ID. */
1950 static tree
1951 identifier_type_value_1 (tree id)
1953 /* There is no type with that name, anywhere. */
1954 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
1955 return NULL_TREE;
1956 /* This is not the type marker, but the real thing. */
1957 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
1958 return REAL_IDENTIFIER_TYPE_VALUE (id);
1959 /* Have to search for it. It must be on the global level, now.
1960 Ask lookup_name not to return non-types. */
1961 id = lookup_name_real (id, 2, 1, /*block_p=*/true, 0, 0);
1962 if (id)
1963 return TREE_TYPE (id);
1964 return NULL_TREE;
1967 /* Wrapper for identifier_type_value_1. */
1969 tree
1970 identifier_type_value (tree id)
1972 tree ret;
1973 timevar_start (TV_NAME_LOOKUP);
1974 ret = identifier_type_value_1 (id);
1975 timevar_stop (TV_NAME_LOOKUP);
1976 return ret;
1980 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
1981 the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */
1983 tree
1984 identifier_global_value (tree t)
1986 return IDENTIFIER_GLOBAL_VALUE (t);
1989 /* Push a definition of struct, union or enum tag named ID. into
1990 binding_level B. DECL is a TYPE_DECL for the type. We assume that
1991 the tag ID is not already defined. */
1993 static void
1994 set_identifier_type_value_with_scope (tree id, tree decl, cp_binding_level *b)
1996 tree type;
1998 if (b->kind != sk_namespace)
2000 /* Shadow the marker, not the real thing, so that the marker
2001 gets restored later. */
2002 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
2003 b->type_shadowed
2004 = tree_cons (id, old_type_value, b->type_shadowed);
2005 type = decl ? TREE_TYPE (decl) : NULL_TREE;
2006 TREE_TYPE (b->type_shadowed) = type;
2008 else
2010 cxx_binding *binding =
2011 binding_for_name (NAMESPACE_LEVEL (current_namespace), id);
2012 gcc_assert (decl);
2013 if (binding->value)
2014 supplement_binding (binding, decl);
2015 else
2016 binding->value = decl;
2018 /* Store marker instead of real type. */
2019 type = global_type_node;
2021 SET_IDENTIFIER_TYPE_VALUE (id, type);
2024 /* As set_identifier_type_value_with_scope, but using
2025 current_binding_level. */
2027 void
2028 set_identifier_type_value (tree id, tree decl)
2030 set_identifier_type_value_with_scope (id, decl, current_binding_level);
2033 /* Return the name for the constructor (or destructor) for the
2034 specified class TYPE. When given a template, this routine doesn't
2035 lose the specialization. */
2037 static inline tree
2038 constructor_name_full (tree type)
2040 return TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
2043 /* Return the name for the constructor (or destructor) for the
2044 specified class. When given a template, return the plain
2045 unspecialized name. */
2047 tree
2048 constructor_name (tree type)
2050 tree name;
2051 name = constructor_name_full (type);
2052 if (IDENTIFIER_TEMPLATE (name))
2053 name = IDENTIFIER_TEMPLATE (name);
2054 return name;
2057 /* Returns TRUE if NAME is the name for the constructor for TYPE,
2058 which must be a class type. */
2060 bool
2061 constructor_name_p (tree name, tree type)
2063 tree ctor_name;
2065 gcc_assert (MAYBE_CLASS_TYPE_P (type));
2067 if (!name)
2068 return false;
2070 if (!identifier_p (name))
2071 return false;
2073 /* These don't have names. */
2074 if (TREE_CODE (type) == DECLTYPE_TYPE
2075 || TREE_CODE (type) == TYPEOF_TYPE)
2076 return false;
2078 ctor_name = constructor_name_full (type);
2079 if (name == ctor_name)
2080 return true;
2081 if (IDENTIFIER_TEMPLATE (ctor_name)
2082 && name == IDENTIFIER_TEMPLATE (ctor_name))
2083 return true;
2084 return false;
2087 /* Counter used to create anonymous type names. */
2089 static GTY(()) int anon_cnt;
2091 /* Return an IDENTIFIER which can be used as a name for
2092 unnamed structs and unions. */
2094 tree
2095 make_anon_name (void)
2097 char buf[32];
2099 sprintf (buf, anon_aggrname_format (), anon_cnt++);
2100 return get_identifier (buf);
2103 /* This code is practically identical to that for creating
2104 anonymous names, but is just used for lambdas instead. This isn't really
2105 necessary, but it's convenient to avoid treating lambdas like other
2106 unnamed types. */
2108 static GTY(()) int lambda_cnt = 0;
2110 tree
2111 make_lambda_name (void)
2113 char buf[32];
2115 sprintf (buf, LAMBDANAME_FORMAT, lambda_cnt++);
2116 return get_identifier (buf);
2119 /* Return (from the stack of) the BINDING, if any, established at SCOPE. */
2121 static inline cxx_binding *
2122 find_binding (cp_binding_level *scope, cxx_binding *binding)
2124 for (; binding != NULL; binding = binding->previous)
2125 if (binding->scope == scope)
2126 return binding;
2128 return (cxx_binding *)0;
2131 /* Return the binding for NAME in SCOPE, if any. Otherwise, return NULL. */
2133 static inline cxx_binding *
2134 cp_binding_level_find_binding_for_name (cp_binding_level *scope, tree name)
2136 cxx_binding *b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2137 if (b)
2139 /* Fold-in case where NAME is used only once. */
2140 if (scope == b->scope && b->previous == NULL)
2141 return b;
2142 return find_binding (scope, b);
2144 return NULL;
2147 /* Always returns a binding for name in scope. If no binding is
2148 found, make a new one. */
2150 static cxx_binding *
2151 binding_for_name (cp_binding_level *scope, tree name)
2153 cxx_binding *result;
2155 result = cp_binding_level_find_binding_for_name (scope, name);
2156 if (result)
2157 return result;
2158 /* Not found, make a new one. */
2159 result = cxx_binding_make (NULL, NULL);
2160 result->previous = IDENTIFIER_NAMESPACE_BINDINGS (name);
2161 result->scope = scope;
2162 result->is_local = false;
2163 result->value_is_inherited = false;
2164 IDENTIFIER_NAMESPACE_BINDINGS (name) = result;
2165 return result;
2168 /* Walk through the bindings associated to the name of FUNCTION,
2169 and return the first declaration of a function with a
2170 "C" linkage specification, a.k.a 'extern "C"'.
2171 This function looks for the binding, regardless of which scope it
2172 has been defined in. It basically looks in all the known scopes.
2173 Note that this function does not lookup for bindings of builtin functions
2174 or for functions declared in system headers. */
2175 static tree
2176 lookup_extern_c_fun_in_all_ns (tree function)
2178 tree name;
2179 cxx_binding *iter;
2181 gcc_assert (function && TREE_CODE (function) == FUNCTION_DECL);
2183 name = DECL_NAME (function);
2184 gcc_assert (name && identifier_p (name));
2186 for (iter = IDENTIFIER_NAMESPACE_BINDINGS (name);
2187 iter;
2188 iter = iter->previous)
2190 tree ovl;
2191 for (ovl = iter->value; ovl; ovl = OVL_NEXT (ovl))
2193 tree decl = OVL_CURRENT (ovl);
2194 if (decl
2195 && TREE_CODE (decl) == FUNCTION_DECL
2196 && DECL_EXTERN_C_P (decl)
2197 && !DECL_ARTIFICIAL (decl))
2199 return decl;
2203 return NULL;
2206 /* Returns a list of C-linkage decls with the name NAME. */
2208 tree
2209 c_linkage_bindings (tree name)
2211 tree decls = NULL_TREE;
2212 cxx_binding *iter;
2214 for (iter = IDENTIFIER_NAMESPACE_BINDINGS (name);
2215 iter;
2216 iter = iter->previous)
2218 tree ovl;
2219 for (ovl = iter->value; ovl; ovl = OVL_NEXT (ovl))
2221 tree decl = OVL_CURRENT (ovl);
2222 if (decl
2223 && DECL_EXTERN_C_P (decl)
2224 && !DECL_ARTIFICIAL (decl))
2226 if (decls == NULL_TREE)
2227 decls = decl;
2228 else
2229 decls = tree_cons (NULL_TREE, decl, decls);
2233 return decls;
2236 /* Insert another USING_DECL into the current binding level, returning
2237 this declaration. If this is a redeclaration, do nothing, and
2238 return NULL_TREE if this not in namespace scope (in namespace
2239 scope, a using decl might extend any previous bindings). */
2241 static tree
2242 push_using_decl_1 (tree scope, tree name)
2244 tree decl;
2246 gcc_assert (TREE_CODE (scope) == NAMESPACE_DECL);
2247 gcc_assert (identifier_p (name));
2248 for (decl = current_binding_level->usings; decl; decl = DECL_CHAIN (decl))
2249 if (USING_DECL_SCOPE (decl) == scope && DECL_NAME (decl) == name)
2250 break;
2251 if (decl)
2252 return namespace_bindings_p () ? decl : NULL_TREE;
2253 decl = build_lang_decl (USING_DECL, name, NULL_TREE);
2254 USING_DECL_SCOPE (decl) = scope;
2255 DECL_CHAIN (decl) = current_binding_level->usings;
2256 current_binding_level->usings = decl;
2257 return decl;
2260 /* Wrapper for push_using_decl_1. */
2262 static tree
2263 push_using_decl (tree scope, tree name)
2265 tree ret;
2266 timevar_start (TV_NAME_LOOKUP);
2267 ret = push_using_decl_1 (scope, name);
2268 timevar_stop (TV_NAME_LOOKUP);
2269 return ret;
2272 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
2273 caller to set DECL_CONTEXT properly.
2275 Note that this must only be used when X will be the new innermost
2276 binding for its name, as we tack it onto the front of IDENTIFIER_BINDING
2277 without checking to see if the current IDENTIFIER_BINDING comes from a
2278 closer binding level than LEVEL. */
2280 static tree
2281 pushdecl_with_scope_1 (tree x, cp_binding_level *level, bool is_friend)
2283 cp_binding_level *b;
2284 tree function_decl = current_function_decl;
2286 current_function_decl = NULL_TREE;
2287 if (level->kind == sk_class)
2289 b = class_binding_level;
2290 class_binding_level = level;
2291 pushdecl_class_level (x);
2292 class_binding_level = b;
2294 else
2296 b = current_binding_level;
2297 current_binding_level = level;
2298 x = pushdecl_maybe_friend (x, is_friend);
2299 current_binding_level = b;
2301 current_function_decl = function_decl;
2302 return x;
2305 /* Wrapper for pushdecl_with_scope_1. */
2307 tree
2308 pushdecl_with_scope (tree x, cp_binding_level *level, bool is_friend)
2310 tree ret;
2311 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2312 ret = pushdecl_with_scope_1 (x, level, is_friend);
2313 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2314 return ret;
2317 /* Helper function for push_overloaded_decl_1 and do_nonmember_using_decl.
2318 Compares the parameter-type-lists of DECL1 and DECL2 and returns false
2319 if they are different. If the DECLs are template functions, the return
2320 types and the template parameter lists are compared too (DR 565). */
2322 static bool
2323 compparms_for_decl_and_using_decl (tree decl1, tree decl2)
2325 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (decl1)),
2326 TYPE_ARG_TYPES (TREE_TYPE (decl2))))
2327 return false;
2329 if (! DECL_FUNCTION_TEMPLATE_P (decl1)
2330 || ! DECL_FUNCTION_TEMPLATE_P (decl2))
2331 return true;
2333 return (comp_template_parms (DECL_TEMPLATE_PARMS (decl1),
2334 DECL_TEMPLATE_PARMS (decl2))
2335 && same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
2336 TREE_TYPE (TREE_TYPE (decl2))));
2339 /* DECL is a FUNCTION_DECL for a non-member function, which may have
2340 other definitions already in place. We get around this by making
2341 the value of the identifier point to a list of all the things that
2342 want to be referenced by that name. It is then up to the users of
2343 that name to decide what to do with that list.
2345 DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its
2346 DECL_TEMPLATE_RESULT. It is dealt with the same way.
2348 FLAGS is a bitwise-or of the following values:
2349 PUSH_LOCAL: Bind DECL in the current scope, rather than at
2350 namespace scope.
2351 PUSH_USING: DECL is being pushed as the result of a using
2352 declaration.
2354 IS_FRIEND is true if this is a friend declaration.
2356 The value returned may be a previous declaration if we guessed wrong
2357 about what language DECL should belong to (C or C++). Otherwise,
2358 it's always DECL (and never something that's not a _DECL). */
2360 static tree
2361 push_overloaded_decl_1 (tree decl, int flags, bool is_friend)
2363 tree name = DECL_NAME (decl);
2364 tree old;
2365 tree new_binding;
2366 int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
2368 if (doing_global)
2369 old = namespace_binding (name, DECL_CONTEXT (decl));
2370 else
2371 old = lookup_name_innermost_nonclass_level (name);
2373 if (old)
2375 if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
2377 tree t = TREE_TYPE (old);
2378 if (MAYBE_CLASS_TYPE_P (t) && warn_shadow
2379 && (! DECL_IN_SYSTEM_HEADER (decl)
2380 || ! DECL_IN_SYSTEM_HEADER (old)))
2381 warning (OPT_Wshadow, "%q#D hides constructor for %q#T", decl, t);
2382 old = NULL_TREE;
2384 else if (is_overloaded_fn (old))
2386 tree tmp;
2388 for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
2390 tree fn = OVL_CURRENT (tmp);
2391 tree dup;
2393 if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp)
2394 && !(flags & PUSH_USING)
2395 && compparms_for_decl_and_using_decl (fn, decl)
2396 && ! decls_match (fn, decl))
2397 diagnose_name_conflict (decl, fn);
2399 dup = duplicate_decls (decl, fn, is_friend);
2400 /* If DECL was a redeclaration of FN -- even an invalid
2401 one -- pass that information along to our caller. */
2402 if (dup == fn || dup == error_mark_node)
2403 return dup;
2406 /* We don't overload implicit built-ins. duplicate_decls()
2407 may fail to merge the decls if the new decl is e.g. a
2408 template function. */
2409 if (TREE_CODE (old) == FUNCTION_DECL
2410 && DECL_ANTICIPATED (old)
2411 && !DECL_HIDDEN_FRIEND_P (old))
2412 old = NULL;
2414 else if (old == error_mark_node)
2415 /* Ignore the undefined symbol marker. */
2416 old = NULL_TREE;
2417 else
2419 error ("previous non-function declaration %q+#D", old);
2420 error ("conflicts with function declaration %q#D", decl);
2421 return decl;
2425 if (old || TREE_CODE (decl) == TEMPLATE_DECL
2426 /* If it's a using declaration, we always need to build an OVERLOAD,
2427 because it's the only way to remember that the declaration comes
2428 from 'using', and have the lookup behave correctly. */
2429 || (flags & PUSH_USING))
2431 if (old && TREE_CODE (old) != OVERLOAD)
2432 new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE));
2433 else
2434 new_binding = ovl_cons (decl, old);
2435 if (flags & PUSH_USING)
2436 OVL_USED (new_binding) = 1;
2438 else
2439 /* NAME is not ambiguous. */
2440 new_binding = decl;
2442 if (doing_global)
2443 set_namespace_binding (name, current_namespace, new_binding);
2444 else
2446 /* We only create an OVERLOAD if there was a previous binding at
2447 this level, or if decl is a template. In the former case, we
2448 need to remove the old binding and replace it with the new
2449 binding. We must also run through the NAMES on the binding
2450 level where the name was bound to update the chain. */
2452 if (TREE_CODE (new_binding) == OVERLOAD && old)
2454 tree *d;
2456 for (d = &IDENTIFIER_BINDING (name)->scope->names;
2458 d = &TREE_CHAIN (*d))
2459 if (*d == old
2460 || (TREE_CODE (*d) == TREE_LIST
2461 && TREE_VALUE (*d) == old))
2463 if (TREE_CODE (*d) == TREE_LIST)
2464 /* Just replace the old binding with the new. */
2465 TREE_VALUE (*d) = new_binding;
2466 else
2467 /* Build a TREE_LIST to wrap the OVERLOAD. */
2468 *d = tree_cons (NULL_TREE, new_binding,
2469 TREE_CHAIN (*d));
2471 /* And update the cxx_binding node. */
2472 IDENTIFIER_BINDING (name)->value = new_binding;
2473 return decl;
2476 /* We should always find a previous binding in this case. */
2477 gcc_unreachable ();
2480 /* Install the new binding. */
2481 push_local_binding (name, new_binding, flags);
2484 return decl;
2487 /* Wrapper for push_overloaded_decl_1. */
2489 static tree
2490 push_overloaded_decl (tree decl, int flags, bool is_friend)
2492 tree ret;
2493 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2494 ret = push_overloaded_decl_1 (decl, flags, is_friend);
2495 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2496 return ret;
2499 /* Check a non-member using-declaration. Return the name and scope
2500 being used, and the USING_DECL, or NULL_TREE on failure. */
2502 static tree
2503 validate_nonmember_using_decl (tree decl, tree scope, tree name)
2505 /* [namespace.udecl]
2506 A using-declaration for a class member shall be a
2507 member-declaration. */
2508 if (TYPE_P (scope))
2510 error ("%qT is not a namespace or unscoped enum", scope);
2511 return NULL_TREE;
2513 else if (scope == error_mark_node)
2514 return NULL_TREE;
2516 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR)
2518 /* 7.3.3/5
2519 A using-declaration shall not name a template-id. */
2520 error ("a using-declaration cannot specify a template-id. "
2521 "Try %<using %D%>", name);
2522 return NULL_TREE;
2525 if (TREE_CODE (decl) == NAMESPACE_DECL)
2527 error ("namespace %qD not allowed in using-declaration", decl);
2528 return NULL_TREE;
2531 if (TREE_CODE (decl) == SCOPE_REF)
2533 /* It's a nested name with template parameter dependent scope.
2534 This can only be using-declaration for class member. */
2535 error ("%qT is not a namespace", TREE_OPERAND (decl, 0));
2536 return NULL_TREE;
2539 if (is_overloaded_fn (decl))
2540 decl = get_first_fn (decl);
2542 gcc_assert (DECL_P (decl));
2544 /* Make a USING_DECL. */
2545 tree using_decl = push_using_decl (scope, name);
2547 if (using_decl == NULL_TREE
2548 && at_function_scope_p ()
2549 && VAR_P (decl))
2550 /* C++11 7.3.3/10. */
2551 error ("%qD is already declared in this scope", name);
2553 return using_decl;
2556 /* Process local and global using-declarations. */
2558 static void
2559 do_nonmember_using_decl (tree scope, tree name, tree oldval, tree oldtype,
2560 tree *newval, tree *newtype)
2562 struct scope_binding decls = EMPTY_SCOPE_BINDING;
2564 *newval = *newtype = NULL_TREE;
2565 if (!qualified_lookup_using_namespace (name, scope, &decls, 0))
2566 /* Lookup error */
2567 return;
2569 if (!decls.value && !decls.type)
2571 error ("%qD not declared", name);
2572 return;
2575 /* Shift the old and new bindings around so we're comparing class and
2576 enumeration names to each other. */
2577 if (oldval && DECL_IMPLICIT_TYPEDEF_P (oldval))
2579 oldtype = oldval;
2580 oldval = NULL_TREE;
2583 if (decls.value && DECL_IMPLICIT_TYPEDEF_P (decls.value))
2585 decls.type = decls.value;
2586 decls.value = NULL_TREE;
2589 if (decls.value)
2591 /* Check for using functions. */
2592 if (is_overloaded_fn (decls.value))
2594 tree tmp, tmp1;
2596 if (oldval && !is_overloaded_fn (oldval))
2598 error ("%qD is already declared in this scope", name);
2599 oldval = NULL_TREE;
2602 *newval = oldval;
2603 for (tmp = decls.value; tmp; tmp = OVL_NEXT (tmp))
2605 tree new_fn = OVL_CURRENT (tmp);
2607 /* Don't import functions that haven't been declared. */
2608 if (DECL_ANTICIPATED (new_fn))
2609 continue;
2611 /* [namespace.udecl]
2613 If a function declaration in namespace scope or block
2614 scope has the same name and the same parameter types as a
2615 function introduced by a using declaration the program is
2616 ill-formed. */
2617 for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1))
2619 tree old_fn = OVL_CURRENT (tmp1);
2621 if (new_fn == old_fn)
2622 /* The function already exists in the current namespace. */
2623 break;
2624 else if (TREE_CODE (tmp1) == OVERLOAD && OVL_USED (tmp1))
2625 continue; /* this is a using decl */
2626 else if (compparms_for_decl_and_using_decl (new_fn, old_fn))
2628 /* There was already a non-using declaration in
2629 this scope with the same parameter types. If both
2630 are the same extern "C" functions, that's ok. */
2631 if (DECL_ANTICIPATED (old_fn)
2632 && !DECL_HIDDEN_FRIEND_P (old_fn))
2633 /* Ignore anticipated built-ins. */;
2634 else if (decls_match (new_fn, old_fn))
2635 break;
2636 else
2638 diagnose_name_conflict (new_fn, old_fn);
2639 break;
2644 /* If we broke out of the loop, there's no reason to add
2645 this function to the using declarations for this
2646 scope. */
2647 if (tmp1)
2648 continue;
2650 /* If we are adding to an existing OVERLOAD, then we no
2651 longer know the type of the set of functions. */
2652 if (*newval && TREE_CODE (*newval) == OVERLOAD)
2653 TREE_TYPE (*newval) = unknown_type_node;
2654 /* Add this new function to the set. */
2655 *newval = build_overload (OVL_CURRENT (tmp), *newval);
2656 /* If there is only one function, then we use its type. (A
2657 using-declaration naming a single function can be used in
2658 contexts where overload resolution cannot be
2659 performed.) */
2660 if (TREE_CODE (*newval) != OVERLOAD)
2662 *newval = ovl_cons (*newval, NULL_TREE);
2663 TREE_TYPE (*newval) = TREE_TYPE (OVL_CURRENT (tmp));
2665 OVL_USED (*newval) = 1;
2668 else
2670 /* If we're declaring a non-function and OLDVAL is an anticipated
2671 built-in, just pretend it isn't there. */
2672 if (oldval
2673 && TREE_CODE (oldval) == FUNCTION_DECL
2674 && DECL_ANTICIPATED (oldval)
2675 && !DECL_HIDDEN_FRIEND_P (oldval))
2676 oldval = NULL_TREE;
2678 *newval = decls.value;
2679 if (oldval && !decls_match (*newval, oldval))
2680 error ("%qD is already declared in this scope", name);
2683 else
2684 *newval = oldval;
2686 if (decls.type && TREE_CODE (decls.type) == TREE_LIST)
2688 error ("reference to %qD is ambiguous", name);
2689 print_candidates (decls.type);
2691 else
2693 *newtype = decls.type;
2694 if (oldtype && *newtype && !decls_match (oldtype, *newtype))
2695 error ("%qD is already declared in this scope", name);
2698 /* If *newval is empty, shift any class or enumeration name down. */
2699 if (!*newval)
2701 *newval = *newtype;
2702 *newtype = NULL_TREE;
2706 /* Process a using-declaration at function scope. */
2708 void
2709 do_local_using_decl (tree decl, tree scope, tree name)
2711 tree oldval, oldtype, newval, newtype;
2712 tree orig_decl = decl;
2714 decl = validate_nonmember_using_decl (decl, scope, name);
2715 if (decl == NULL_TREE)
2716 return;
2718 if (building_stmt_list_p ()
2719 && at_function_scope_p ())
2720 add_decl_expr (decl);
2722 oldval = lookup_name_innermost_nonclass_level (name);
2723 oldtype = lookup_type_current_level (name);
2725 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
2727 if (newval)
2729 if (is_overloaded_fn (newval))
2731 tree fn, term;
2733 /* We only need to push declarations for those functions
2734 that were not already bound in the current level.
2735 The old value might be NULL_TREE, it might be a single
2736 function, or an OVERLOAD. */
2737 if (oldval && TREE_CODE (oldval) == OVERLOAD)
2738 term = OVL_FUNCTION (oldval);
2739 else
2740 term = oldval;
2741 for (fn = newval; fn && OVL_CURRENT (fn) != term;
2742 fn = OVL_NEXT (fn))
2743 push_overloaded_decl (OVL_CURRENT (fn),
2744 PUSH_LOCAL | PUSH_USING,
2745 false);
2747 else
2748 push_local_binding (name, newval, PUSH_USING);
2750 if (newtype)
2752 push_local_binding (name, newtype, PUSH_USING);
2753 set_identifier_type_value (name, newtype);
2756 /* Emit debug info. */
2757 if (!processing_template_decl)
2758 cp_emit_debug_info_for_using (orig_decl, current_scope());
2761 /* Returns true if ROOT (a namespace, class, or function) encloses
2762 CHILD. CHILD may be either a class type or a namespace. */
2764 bool
2765 is_ancestor (tree root, tree child)
2767 gcc_assert ((TREE_CODE (root) == NAMESPACE_DECL
2768 || TREE_CODE (root) == FUNCTION_DECL
2769 || CLASS_TYPE_P (root)));
2770 gcc_assert ((TREE_CODE (child) == NAMESPACE_DECL
2771 || CLASS_TYPE_P (child)));
2773 /* The global namespace encloses everything. */
2774 if (root == global_namespace)
2775 return true;
2777 while (true)
2779 /* If we've run out of scopes, stop. */
2780 if (!child)
2781 return false;
2782 /* If we've reached the ROOT, it encloses CHILD. */
2783 if (root == child)
2784 return true;
2785 /* Go out one level. */
2786 if (TYPE_P (child))
2787 child = TYPE_NAME (child);
2788 child = DECL_CONTEXT (child);
2792 /* Enter the class or namespace scope indicated by T suitable for name
2793 lookup. T can be arbitrary scope, not necessary nested inside the
2794 current scope. Returns a non-null scope to pop iff pop_scope
2795 should be called later to exit this scope. */
2797 tree
2798 push_scope (tree t)
2800 if (TREE_CODE (t) == NAMESPACE_DECL)
2801 push_decl_namespace (t);
2802 else if (CLASS_TYPE_P (t))
2804 if (!at_class_scope_p ()
2805 || !same_type_p (current_class_type, t))
2806 push_nested_class (t);
2807 else
2808 /* T is the same as the current scope. There is therefore no
2809 need to re-enter the scope. Since we are not actually
2810 pushing a new scope, our caller should not call
2811 pop_scope. */
2812 t = NULL_TREE;
2815 return t;
2818 /* Leave scope pushed by push_scope. */
2820 void
2821 pop_scope (tree t)
2823 if (t == NULL_TREE)
2824 return;
2825 if (TREE_CODE (t) == NAMESPACE_DECL)
2826 pop_decl_namespace ();
2827 else if CLASS_TYPE_P (t)
2828 pop_nested_class ();
2831 /* Subroutine of push_inner_scope. */
2833 static void
2834 push_inner_scope_r (tree outer, tree inner)
2836 tree prev;
2838 if (outer == inner
2839 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
2840 return;
2842 prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
2843 if (outer != prev)
2844 push_inner_scope_r (outer, prev);
2845 if (TREE_CODE (inner) == NAMESPACE_DECL)
2847 cp_binding_level *save_template_parm = 0;
2848 /* Temporary take out template parameter scopes. They are saved
2849 in reversed order in save_template_parm. */
2850 while (current_binding_level->kind == sk_template_parms)
2852 cp_binding_level *b = current_binding_level;
2853 current_binding_level = b->level_chain;
2854 b->level_chain = save_template_parm;
2855 save_template_parm = b;
2858 resume_scope (NAMESPACE_LEVEL (inner));
2859 current_namespace = inner;
2861 /* Restore template parameter scopes. */
2862 while (save_template_parm)
2864 cp_binding_level *b = save_template_parm;
2865 save_template_parm = b->level_chain;
2866 b->level_chain = current_binding_level;
2867 current_binding_level = b;
2870 else
2871 pushclass (inner);
2874 /* Enter the scope INNER from current scope. INNER must be a scope
2875 nested inside current scope. This works with both name lookup and
2876 pushing name into scope. In case a template parameter scope is present,
2877 namespace is pushed under the template parameter scope according to
2878 name lookup rule in 14.6.1/6.
2880 Return the former current scope suitable for pop_inner_scope. */
2882 tree
2883 push_inner_scope (tree inner)
2885 tree outer = current_scope ();
2886 if (!outer)
2887 outer = current_namespace;
2889 push_inner_scope_r (outer, inner);
2890 return outer;
2893 /* Exit the current scope INNER back to scope OUTER. */
2895 void
2896 pop_inner_scope (tree outer, tree inner)
2898 if (outer == inner
2899 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
2900 return;
2902 while (outer != inner)
2904 if (TREE_CODE (inner) == NAMESPACE_DECL)
2906 cp_binding_level *save_template_parm = 0;
2907 /* Temporary take out template parameter scopes. They are saved
2908 in reversed order in save_template_parm. */
2909 while (current_binding_level->kind == sk_template_parms)
2911 cp_binding_level *b = current_binding_level;
2912 current_binding_level = b->level_chain;
2913 b->level_chain = save_template_parm;
2914 save_template_parm = b;
2917 pop_namespace ();
2919 /* Restore template parameter scopes. */
2920 while (save_template_parm)
2922 cp_binding_level *b = save_template_parm;
2923 save_template_parm = b->level_chain;
2924 b->level_chain = current_binding_level;
2925 current_binding_level = b;
2928 else
2929 popclass ();
2931 inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
2935 /* Do a pushlevel for class declarations. */
2937 void
2938 pushlevel_class (void)
2940 class_binding_level = begin_scope (sk_class, current_class_type);
2943 /* ...and a poplevel for class declarations. */
2945 void
2946 poplevel_class (void)
2948 cp_binding_level *level = class_binding_level;
2949 cp_class_binding *cb;
2950 size_t i;
2951 tree shadowed;
2953 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2954 gcc_assert (level != 0);
2956 /* If we're leaving a toplevel class, cache its binding level. */
2957 if (current_class_depth == 1)
2958 previous_class_level = level;
2959 for (shadowed = level->type_shadowed;
2960 shadowed;
2961 shadowed = TREE_CHAIN (shadowed))
2962 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
2964 /* Remove the bindings for all of the class-level declarations. */
2965 if (level->class_shadowed)
2967 FOR_EACH_VEC_ELT (*level->class_shadowed, i, cb)
2969 IDENTIFIER_BINDING (cb->identifier) = cb->base->previous;
2970 cxx_binding_free (cb->base);
2972 ggc_free (level->class_shadowed);
2973 level->class_shadowed = NULL;
2976 /* Now, pop out of the binding level which we created up in the
2977 `pushlevel_class' routine. */
2978 gcc_assert (current_binding_level == level);
2979 leave_scope ();
2980 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2983 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
2984 appropriate. DECL is the value to which a name has just been
2985 bound. CLASS_TYPE is the class in which the lookup occurred. */
2987 static void
2988 set_inherited_value_binding_p (cxx_binding *binding, tree decl,
2989 tree class_type)
2991 if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
2993 tree context;
2995 if (TREE_CODE (decl) == OVERLOAD)
2996 context = ovl_scope (decl);
2997 else
2999 gcc_assert (DECL_P (decl));
3000 context = context_for_name_lookup (decl);
3003 if (is_properly_derived_from (class_type, context))
3004 INHERITED_VALUE_BINDING_P (binding) = 1;
3005 else
3006 INHERITED_VALUE_BINDING_P (binding) = 0;
3008 else if (binding->value == decl)
3009 /* We only encounter a TREE_LIST when there is an ambiguity in the
3010 base classes. Such an ambiguity can be overridden by a
3011 definition in this class. */
3012 INHERITED_VALUE_BINDING_P (binding) = 1;
3013 else
3014 INHERITED_VALUE_BINDING_P (binding) = 0;
3017 /* Make the declaration of X appear in CLASS scope. */
3019 bool
3020 pushdecl_class_level (tree x)
3022 tree name;
3023 bool is_valid = true;
3024 bool subtime;
3026 /* Do nothing if we're adding to an outer lambda closure type,
3027 outer_binding will add it later if it's needed. */
3028 if (current_class_type != class_binding_level->this_entity)
3029 return true;
3031 subtime = timevar_cond_start (TV_NAME_LOOKUP);
3032 /* Get the name of X. */
3033 if (TREE_CODE (x) == OVERLOAD)
3034 name = DECL_NAME (get_first_fn (x));
3035 else
3036 name = DECL_NAME (x);
3038 if (name)
3040 is_valid = push_class_level_binding (name, x);
3041 if (TREE_CODE (x) == TYPE_DECL)
3042 set_identifier_type_value (name, x);
3044 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3046 /* If X is an anonymous aggregate, all of its members are
3047 treated as if they were members of the class containing the
3048 aggregate, for naming purposes. */
3049 tree f;
3051 for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = DECL_CHAIN (f))
3053 location_t save_location = input_location;
3054 input_location = DECL_SOURCE_LOCATION (f);
3055 if (!pushdecl_class_level (f))
3056 is_valid = false;
3057 input_location = save_location;
3060 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3061 return is_valid;
3064 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
3065 scope. If the value returned is non-NULL, and the PREVIOUS field
3066 is not set, callers must set the PREVIOUS field explicitly. */
3068 static cxx_binding *
3069 get_class_binding (tree name, cp_binding_level *scope)
3071 tree class_type;
3072 tree type_binding;
3073 tree value_binding;
3074 cxx_binding *binding;
3076 class_type = scope->this_entity;
3078 /* Get the type binding. */
3079 type_binding = lookup_member (class_type, name,
3080 /*protect=*/2, /*want_type=*/true,
3081 tf_warning_or_error);
3082 /* Get the value binding. */
3083 value_binding = lookup_member (class_type, name,
3084 /*protect=*/2, /*want_type=*/false,
3085 tf_warning_or_error);
3087 if (value_binding
3088 && (TREE_CODE (value_binding) == TYPE_DECL
3089 || DECL_CLASS_TEMPLATE_P (value_binding)
3090 || (TREE_CODE (value_binding) == TREE_LIST
3091 && TREE_TYPE (value_binding) == error_mark_node
3092 && (TREE_CODE (TREE_VALUE (value_binding))
3093 == TYPE_DECL))))
3094 /* We found a type binding, even when looking for a non-type
3095 binding. This means that we already processed this binding
3096 above. */
3098 else if (value_binding)
3100 if (TREE_CODE (value_binding) == TREE_LIST
3101 && TREE_TYPE (value_binding) == error_mark_node)
3102 /* NAME is ambiguous. */
3104 else if (BASELINK_P (value_binding))
3105 /* NAME is some overloaded functions. */
3106 value_binding = BASELINK_FUNCTIONS (value_binding);
3109 /* If we found either a type binding or a value binding, create a
3110 new binding object. */
3111 if (type_binding || value_binding)
3113 binding = new_class_binding (name,
3114 value_binding,
3115 type_binding,
3116 scope);
3117 /* This is a class-scope binding, not a block-scope binding. */
3118 LOCAL_BINDING_P (binding) = 0;
3119 set_inherited_value_binding_p (binding, value_binding, class_type);
3121 else
3122 binding = NULL;
3124 return binding;
3127 /* Make the declaration(s) of X appear in CLASS scope under the name
3128 NAME. Returns true if the binding is valid. */
3130 static bool
3131 push_class_level_binding_1 (tree name, tree x)
3133 cxx_binding *binding;
3134 tree decl = x;
3135 bool ok;
3137 /* The class_binding_level will be NULL if x is a template
3138 parameter name in a member template. */
3139 if (!class_binding_level)
3140 return true;
3142 if (name == error_mark_node)
3143 return false;
3145 /* Can happen for an erroneous declaration (c++/60384). */
3146 if (!identifier_p (name))
3148 gcc_assert (errorcount || sorrycount);
3149 return false;
3152 /* Check for invalid member names. But don't worry about a default
3153 argument-scope lambda being pushed after the class is complete. */
3154 gcc_assert (TYPE_BEING_DEFINED (current_class_type)
3155 || LAMBDA_TYPE_P (TREE_TYPE (decl)));
3156 /* Check that we're pushing into the right binding level. */
3157 gcc_assert (current_class_type == class_binding_level->this_entity);
3159 /* We could have been passed a tree list if this is an ambiguous
3160 declaration. If so, pull the declaration out because
3161 check_template_shadow will not handle a TREE_LIST. */
3162 if (TREE_CODE (decl) == TREE_LIST
3163 && TREE_TYPE (decl) == error_mark_node)
3164 decl = TREE_VALUE (decl);
3166 if (!check_template_shadow (decl))
3167 return false;
3169 /* [class.mem]
3171 If T is the name of a class, then each of the following shall
3172 have a name different from T:
3174 -- every static data member of class T;
3176 -- every member of class T that is itself a type;
3178 -- every enumerator of every member of class T that is an
3179 enumerated type;
3181 -- every member of every anonymous union that is a member of
3182 class T.
3184 (Non-static data members were also forbidden to have the same
3185 name as T until TC1.) */
3186 if ((VAR_P (x)
3187 || TREE_CODE (x) == CONST_DECL
3188 || (TREE_CODE (x) == TYPE_DECL
3189 && !DECL_SELF_REFERENCE_P (x))
3190 /* A data member of an anonymous union. */
3191 || (TREE_CODE (x) == FIELD_DECL
3192 && DECL_CONTEXT (x) != current_class_type))
3193 && DECL_NAME (x) == constructor_name (current_class_type))
3195 tree scope = context_for_name_lookup (x);
3196 if (TYPE_P (scope) && same_type_p (scope, current_class_type))
3198 error ("%qD has the same name as the class in which it is "
3199 "declared",
3201 return false;
3205 /* Get the current binding for NAME in this class, if any. */
3206 binding = IDENTIFIER_BINDING (name);
3207 if (!binding || binding->scope != class_binding_level)
3209 binding = get_class_binding (name, class_binding_level);
3210 /* If a new binding was created, put it at the front of the
3211 IDENTIFIER_BINDING list. */
3212 if (binding)
3214 binding->previous = IDENTIFIER_BINDING (name);
3215 IDENTIFIER_BINDING (name) = binding;
3219 /* If there is already a binding, then we may need to update the
3220 current value. */
3221 if (binding && binding->value)
3223 tree bval = binding->value;
3224 tree old_decl = NULL_TREE;
3225 tree target_decl = strip_using_decl (decl);
3226 tree target_bval = strip_using_decl (bval);
3228 if (INHERITED_VALUE_BINDING_P (binding))
3230 /* If the old binding was from a base class, and was for a
3231 tag name, slide it over to make room for the new binding.
3232 The old binding is still visible if explicitly qualified
3233 with a class-key. */
3234 if (TREE_CODE (target_bval) == TYPE_DECL
3235 && DECL_ARTIFICIAL (target_bval)
3236 && !(TREE_CODE (target_decl) == TYPE_DECL
3237 && DECL_ARTIFICIAL (target_decl)))
3239 old_decl = binding->type;
3240 binding->type = bval;
3241 binding->value = NULL_TREE;
3242 INHERITED_VALUE_BINDING_P (binding) = 0;
3244 else
3246 old_decl = bval;
3247 /* Any inherited type declaration is hidden by the type
3248 declaration in the derived class. */
3249 if (TREE_CODE (target_decl) == TYPE_DECL
3250 && DECL_ARTIFICIAL (target_decl))
3251 binding->type = NULL_TREE;
3254 else if (TREE_CODE (target_decl) == OVERLOAD
3255 && is_overloaded_fn (target_bval))
3256 old_decl = bval;
3257 else if (TREE_CODE (decl) == USING_DECL
3258 && TREE_CODE (bval) == USING_DECL
3259 && same_type_p (USING_DECL_SCOPE (decl),
3260 USING_DECL_SCOPE (bval)))
3261 /* This is a using redeclaration that will be diagnosed later
3262 in supplement_binding */
3264 else if (TREE_CODE (decl) == USING_DECL
3265 && TREE_CODE (bval) == USING_DECL
3266 && DECL_DEPENDENT_P (decl)
3267 && DECL_DEPENDENT_P (bval))
3268 return true;
3269 else if (TREE_CODE (decl) == USING_DECL
3270 && is_overloaded_fn (target_bval))
3271 old_decl = bval;
3272 else if (TREE_CODE (bval) == USING_DECL
3273 && is_overloaded_fn (target_decl))
3274 return true;
3276 if (old_decl && binding->scope == class_binding_level)
3278 binding->value = x;
3279 /* It is always safe to clear INHERITED_VALUE_BINDING_P
3280 here. This function is only used to register bindings
3281 from with the class definition itself. */
3282 INHERITED_VALUE_BINDING_P (binding) = 0;
3283 return true;
3287 /* Note that we declared this value so that we can issue an error if
3288 this is an invalid redeclaration of a name already used for some
3289 other purpose. */
3290 note_name_declared_in_class (name, decl);
3292 /* If we didn't replace an existing binding, put the binding on the
3293 stack of bindings for the identifier, and update the shadowed
3294 list. */
3295 if (binding && binding->scope == class_binding_level)
3296 /* Supplement the existing binding. */
3297 ok = supplement_binding (binding, decl);
3298 else
3300 /* Create a new binding. */
3301 push_binding (name, decl, class_binding_level);
3302 ok = true;
3305 return ok;
3308 /* Wrapper for push_class_level_binding_1. */
3310 bool
3311 push_class_level_binding (tree name, tree x)
3313 bool ret;
3314 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3315 ret = push_class_level_binding_1 (name, x);
3316 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3317 return ret;
3320 /* Process "using SCOPE::NAME" in a class scope. Return the
3321 USING_DECL created. */
3323 tree
3324 do_class_using_decl (tree scope, tree name)
3326 /* The USING_DECL returned by this function. */
3327 tree value;
3328 /* The declaration (or declarations) name by this using
3329 declaration. NULL if we are in a template and cannot figure out
3330 what has been named. */
3331 tree decl;
3332 /* True if SCOPE is a dependent type. */
3333 bool scope_dependent_p;
3334 /* True if SCOPE::NAME is dependent. */
3335 bool name_dependent_p;
3336 /* True if any of the bases of CURRENT_CLASS_TYPE are dependent. */
3337 bool bases_dependent_p;
3338 tree binfo;
3340 if (name == error_mark_node)
3341 return NULL_TREE;
3343 if (!scope || !TYPE_P (scope))
3345 error ("using-declaration for non-member at class scope");
3346 return NULL_TREE;
3349 /* Make sure the name is not invalid */
3350 if (TREE_CODE (name) == BIT_NOT_EXPR)
3352 error ("%<%T::%D%> names destructor", scope, name);
3353 return NULL_TREE;
3355 /* Using T::T declares inheriting ctors, even if T is a typedef. */
3356 if (MAYBE_CLASS_TYPE_P (scope)
3357 && (name == TYPE_IDENTIFIER (scope)
3358 || constructor_name_p (name, scope)))
3360 maybe_warn_cpp0x (CPP0X_INHERITING_CTORS);
3361 name = ctor_identifier;
3363 if (constructor_name_p (name, current_class_type))
3365 error ("%<%T::%D%> names constructor in %qT",
3366 scope, name, current_class_type);
3367 return NULL_TREE;
3370 scope_dependent_p = dependent_scope_p (scope);
3371 name_dependent_p = (scope_dependent_p
3372 || (IDENTIFIER_TYPENAME_P (name)
3373 && dependent_type_p (TREE_TYPE (name))));
3375 bases_dependent_p = any_dependent_bases_p ();
3377 decl = NULL_TREE;
3379 /* From [namespace.udecl]:
3381 A using-declaration used as a member-declaration shall refer to a
3382 member of a base class of the class being defined.
3384 In general, we cannot check this constraint in a template because
3385 we do not know the entire set of base classes of the current
3386 class type. Morover, if SCOPE is dependent, it might match a
3387 non-dependent base. */
3389 if (!scope_dependent_p)
3391 base_kind b_kind;
3392 binfo = lookup_base (current_class_type, scope, ba_any, &b_kind,
3393 tf_warning_or_error);
3394 if (b_kind < bk_proper_base)
3396 if (!bases_dependent_p || b_kind == bk_same_type)
3398 error_not_base_type (scope, current_class_type);
3399 return NULL_TREE;
3402 else if (!name_dependent_p)
3404 decl = lookup_member (binfo, name, 0, false, tf_warning_or_error);
3405 if (!decl)
3407 error ("no members matching %<%T::%D%> in %q#T", scope, name,
3408 scope);
3409 return NULL_TREE;
3411 /* The binfo from which the functions came does not matter. */
3412 if (BASELINK_P (decl))
3413 decl = BASELINK_FUNCTIONS (decl);
3417 value = build_lang_decl (USING_DECL, name, NULL_TREE);
3418 USING_DECL_DECLS (value) = decl;
3419 USING_DECL_SCOPE (value) = scope;
3420 DECL_DEPENDENT_P (value) = !decl;
3422 return value;
3426 /* Return the binding value for name in scope. */
3429 static tree
3430 namespace_binding_1 (tree name, tree scope)
3432 cxx_binding *binding;
3434 if (SCOPE_FILE_SCOPE_P (scope))
3435 scope = global_namespace;
3436 else
3437 /* Unnecessary for the global namespace because it can't be an alias. */
3438 scope = ORIGINAL_NAMESPACE (scope);
3440 binding = cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
3442 return binding ? binding->value : NULL_TREE;
3445 tree
3446 namespace_binding (tree name, tree scope)
3448 tree ret;
3449 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3450 ret = namespace_binding_1 (name, scope);
3451 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3452 return ret;
3455 /* Set the binding value for name in scope. */
3457 static void
3458 set_namespace_binding_1 (tree name, tree scope, tree val)
3460 cxx_binding *b;
3462 if (scope == NULL_TREE)
3463 scope = global_namespace;
3464 b = binding_for_name (NAMESPACE_LEVEL (scope), name);
3465 if (!b->value || TREE_CODE (val) == OVERLOAD || val == error_mark_node)
3466 b->value = val;
3467 else
3468 supplement_binding (b, val);
3471 /* Wrapper for set_namespace_binding_1. */
3473 void
3474 set_namespace_binding (tree name, tree scope, tree val)
3476 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3477 set_namespace_binding_1 (name, scope, val);
3478 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3481 /* Set the context of a declaration to scope. Complain if we are not
3482 outside scope. */
3484 void
3485 set_decl_namespace (tree decl, tree scope, bool friendp)
3487 tree old;
3489 /* Get rid of namespace aliases. */
3490 scope = ORIGINAL_NAMESPACE (scope);
3492 /* It is ok for friends to be qualified in parallel space. */
3493 if (!friendp && !is_ancestor (current_namespace, scope))
3494 error ("declaration of %qD not in a namespace surrounding %qD",
3495 decl, scope);
3496 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3498 /* Writing "int N::i" to declare a variable within "N" is invalid. */
3499 if (scope == current_namespace)
3501 if (at_namespace_scope_p ())
3502 error ("explicit qualification in declaration of %qD",
3503 decl);
3504 return;
3507 /* See whether this has been declared in the namespace. */
3508 old = lookup_qualified_name (scope, DECL_NAME (decl), /*type*/false,
3509 /*complain*/true, /*hidden*/true);
3510 if (old == error_mark_node)
3511 /* No old declaration at all. */
3512 goto complain;
3513 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
3514 if (TREE_CODE (old) == TREE_LIST)
3516 error ("reference to %qD is ambiguous", decl);
3517 print_candidates (old);
3518 return;
3520 if (!is_overloaded_fn (decl))
3522 /* We might have found OLD in an inline namespace inside SCOPE. */
3523 if (TREE_CODE (decl) == TREE_CODE (old))
3524 DECL_CONTEXT (decl) = DECL_CONTEXT (old);
3525 /* Don't compare non-function decls with decls_match here, since
3526 it can't check for the correct constness at this
3527 point. pushdecl will find those errors later. */
3528 return;
3530 /* Since decl is a function, old should contain a function decl. */
3531 if (!is_overloaded_fn (old))
3532 goto complain;
3533 /* A template can be explicitly specialized in any namespace. */
3534 if (processing_explicit_instantiation)
3535 return;
3536 if (processing_template_decl || processing_specialization)
3537 /* We have not yet called push_template_decl to turn a
3538 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
3539 match. But, we'll check later, when we construct the
3540 template. */
3541 return;
3542 /* Instantiations or specializations of templates may be declared as
3543 friends in any namespace. */
3544 if (friendp && DECL_USE_TEMPLATE (decl))
3545 return;
3546 if (is_overloaded_fn (old))
3548 tree found = NULL_TREE;
3549 tree elt = old;
3550 for (; elt; elt = OVL_NEXT (elt))
3552 tree ofn = OVL_CURRENT (elt);
3553 /* Adjust DECL_CONTEXT first so decls_match will return true
3554 if DECL will match a declaration in an inline namespace. */
3555 DECL_CONTEXT (decl) = DECL_CONTEXT (ofn);
3556 if (decls_match (decl, ofn))
3558 if (found && !decls_match (found, ofn))
3560 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3561 error ("reference to %qD is ambiguous", decl);
3562 print_candidates (old);
3563 return;
3565 found = ofn;
3568 if (found)
3570 if (!is_associated_namespace (scope, CP_DECL_CONTEXT (found)))
3571 goto complain;
3572 if (DECL_HIDDEN_FRIEND_P (found))
3574 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
3575 "%qD has not been declared within %D", decl, scope);
3576 inform (DECL_SOURCE_LOCATION (found), "only here as a friend");
3578 DECL_CONTEXT (decl) = DECL_CONTEXT (found);
3579 return;
3582 else
3584 DECL_CONTEXT (decl) = DECL_CONTEXT (old);
3585 if (decls_match (decl, old))
3586 return;
3589 /* It didn't work, go back to the explicit scope. */
3590 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3591 complain:
3592 error ("%qD should have been declared inside %qD", decl, scope);
3595 /* Return the namespace where the current declaration is declared. */
3597 tree
3598 current_decl_namespace (void)
3600 tree result;
3601 /* If we have been pushed into a different namespace, use it. */
3602 if (!vec_safe_is_empty (decl_namespace_list))
3603 return decl_namespace_list->last ();
3605 if (current_class_type)
3606 result = decl_namespace_context (current_class_type);
3607 else if (current_function_decl)
3608 result = decl_namespace_context (current_function_decl);
3609 else
3610 result = current_namespace;
3611 return result;
3614 /* Process any ATTRIBUTES on a namespace definition. Returns true if
3615 attribute visibility is seen. */
3617 bool
3618 handle_namespace_attrs (tree ns, tree attributes)
3620 tree d;
3621 bool saw_vis = false;
3623 for (d = attributes; d; d = TREE_CHAIN (d))
3625 tree name = get_attribute_name (d);
3626 tree args = TREE_VALUE (d);
3628 if (is_attribute_p ("visibility", name))
3630 /* attribute visibility is a property of the syntactic block
3631 rather than the namespace as a whole, so we don't touch the
3632 NAMESPACE_DECL at all. */
3633 tree x = args ? TREE_VALUE (args) : NULL_TREE;
3634 if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args))
3636 warning (OPT_Wattributes,
3637 "%qD attribute requires a single NTBS argument",
3638 name);
3639 continue;
3642 if (!TREE_PUBLIC (ns))
3643 warning (OPT_Wattributes,
3644 "%qD attribute is meaningless since members of the "
3645 "anonymous namespace get local symbols", name);
3647 push_visibility (TREE_STRING_POINTER (x), 1);
3648 saw_vis = true;
3650 else if (is_attribute_p ("abi_tag", name))
3652 if (!DECL_NAMESPACE_ASSOCIATIONS (ns))
3654 warning (OPT_Wattributes, "ignoring %qD attribute on non-inline "
3655 "namespace", name);
3656 continue;
3658 if (!DECL_NAME (ns))
3660 warning (OPT_Wattributes, "ignoring %qD attribute on anonymous "
3661 "namespace", name);
3662 continue;
3664 if (!args)
3666 tree dn = DECL_NAME (ns);
3667 args = build_string (IDENTIFIER_LENGTH (dn) + 1,
3668 IDENTIFIER_POINTER (dn));
3669 TREE_TYPE (args) = char_array_type_node;
3670 args = fix_string_type (args);
3671 args = build_tree_list (NULL_TREE, args);
3673 if (check_abi_tag_args (args, name))
3674 DECL_ATTRIBUTES (ns) = tree_cons (name, args,
3675 DECL_ATTRIBUTES (ns));
3677 else
3679 warning (OPT_Wattributes, "%qD attribute directive ignored",
3680 name);
3681 continue;
3685 return saw_vis;
3688 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we
3689 select a name that is unique to this compilation unit. Returns FALSE if
3690 pushdecl fails, TRUE otherwise. */
3692 bool
3693 push_namespace (tree name)
3695 tree d = NULL_TREE;
3696 bool need_new = true;
3697 bool implicit_use = false;
3698 bool anon = !name;
3700 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3702 /* We should not get here if the global_namespace is not yet constructed
3703 nor if NAME designates the global namespace: The global scope is
3704 constructed elsewhere. */
3705 gcc_assert (global_namespace != NULL && name != global_scope_name);
3707 if (anon)
3709 name = get_anonymous_namespace_name();
3710 d = IDENTIFIER_NAMESPACE_VALUE (name);
3711 if (d)
3712 /* Reopening anonymous namespace. */
3713 need_new = false;
3714 implicit_use = true;
3716 else
3718 /* Check whether this is an extended namespace definition. */
3719 d = IDENTIFIER_NAMESPACE_VALUE (name);
3720 if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
3722 tree dna = DECL_NAMESPACE_ALIAS (d);
3723 if (dna)
3725 /* We do some error recovery for, eg, the redeclaration
3726 of M here:
3728 namespace N {}
3729 namespace M = N;
3730 namespace M {}
3732 However, in nasty cases like:
3734 namespace N
3736 namespace M = N;
3737 namespace M {}
3740 we just error out below, in duplicate_decls. */
3741 if (NAMESPACE_LEVEL (dna)->level_chain
3742 == current_binding_level)
3744 error ("namespace alias %qD not allowed here, "
3745 "assuming %qD", d, dna);
3746 d = dna;
3747 need_new = false;
3750 else
3751 need_new = false;
3755 if (need_new)
3757 /* Make a new namespace, binding the name to it. */
3758 d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
3759 DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace);
3760 /* The name of this namespace is not visible to other translation
3761 units if it is an anonymous namespace or member thereof. */
3762 if (anon || decl_anon_ns_mem_p (current_namespace))
3763 TREE_PUBLIC (d) = 0;
3764 else
3765 TREE_PUBLIC (d) = 1;
3766 if (pushdecl (d) == error_mark_node)
3768 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3769 return false;
3771 if (anon)
3773 /* Clear DECL_NAME for the benefit of debugging back ends. */
3774 SET_DECL_ASSEMBLER_NAME (d, name);
3775 DECL_NAME (d) = NULL_TREE;
3777 begin_scope (sk_namespace, d);
3779 else
3780 resume_scope (NAMESPACE_LEVEL (d));
3782 if (implicit_use)
3783 do_using_directive (d);
3784 /* Enter the name space. */
3785 current_namespace = d;
3787 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3788 return true;
3791 /* Pop from the scope of the current namespace. */
3793 void
3794 pop_namespace (void)
3796 gcc_assert (current_namespace != global_namespace);
3797 current_namespace = CP_DECL_CONTEXT (current_namespace);
3798 /* The binding level is not popped, as it might be re-opened later. */
3799 leave_scope ();
3802 /* Push into the scope of the namespace NS, even if it is deeply
3803 nested within another namespace. */
3805 void
3806 push_nested_namespace (tree ns)
3808 if (ns == global_namespace)
3809 push_to_top_level ();
3810 else
3812 push_nested_namespace (CP_DECL_CONTEXT (ns));
3813 push_namespace (DECL_NAME (ns));
3817 /* Pop back from the scope of the namespace NS, which was previously
3818 entered with push_nested_namespace. */
3820 void
3821 pop_nested_namespace (tree ns)
3823 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3824 gcc_assert (current_namespace == ns);
3825 while (ns != global_namespace)
3827 pop_namespace ();
3828 ns = CP_DECL_CONTEXT (ns);
3831 pop_from_top_level ();
3832 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3835 /* Temporarily set the namespace for the current declaration. */
3837 void
3838 push_decl_namespace (tree decl)
3840 if (TREE_CODE (decl) != NAMESPACE_DECL)
3841 decl = decl_namespace_context (decl);
3842 vec_safe_push (decl_namespace_list, ORIGINAL_NAMESPACE (decl));
3845 /* [namespace.memdef]/2 */
3847 void
3848 pop_decl_namespace (void)
3850 decl_namespace_list->pop ();
3853 /* Return the namespace that is the common ancestor
3854 of two given namespaces. */
3856 static tree
3857 namespace_ancestor_1 (tree ns1, tree ns2)
3859 tree nsr;
3860 if (is_ancestor (ns1, ns2))
3861 nsr = ns1;
3862 else
3863 nsr = namespace_ancestor_1 (CP_DECL_CONTEXT (ns1), ns2);
3864 return nsr;
3867 /* Wrapper for namespace_ancestor_1. */
3869 static tree
3870 namespace_ancestor (tree ns1, tree ns2)
3872 tree nsr;
3873 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3874 nsr = namespace_ancestor_1 (ns1, ns2);
3875 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3876 return nsr;
3879 /* Process a namespace-alias declaration. */
3881 void
3882 do_namespace_alias (tree alias, tree name_space)
3884 if (name_space == error_mark_node)
3885 return;
3887 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
3889 name_space = ORIGINAL_NAMESPACE (name_space);
3891 /* Build the alias. */
3892 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
3893 DECL_NAMESPACE_ALIAS (alias) = name_space;
3894 DECL_EXTERNAL (alias) = 1;
3895 DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ());
3896 pushdecl (alias);
3898 /* Emit debug info for namespace alias. */
3899 if (!building_stmt_list_p ())
3900 (*debug_hooks->early_global_decl) (alias);
3903 /* Like pushdecl, only it places X in the current namespace,
3904 if appropriate. */
3906 tree
3907 pushdecl_namespace_level (tree x, bool is_friend)
3909 cp_binding_level *b = current_binding_level;
3910 tree t;
3912 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3913 t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace), is_friend);
3915 /* Now, the type_shadowed stack may screw us. Munge it so it does
3916 what we want. */
3917 if (TREE_CODE (t) == TYPE_DECL)
3919 tree name = DECL_NAME (t);
3920 tree newval;
3921 tree *ptr = (tree *)0;
3922 for (; !global_scope_p (b); b = b->level_chain)
3924 tree shadowed = b->type_shadowed;
3925 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
3926 if (TREE_PURPOSE (shadowed) == name)
3928 ptr = &TREE_VALUE (shadowed);
3929 /* Can't break out of the loop here because sometimes
3930 a binding level will have duplicate bindings for
3931 PT names. It's gross, but I haven't time to fix it. */
3934 newval = TREE_TYPE (t);
3935 if (ptr == (tree *)0)
3937 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
3938 up here if this is changed to an assertion. --KR */
3939 SET_IDENTIFIER_TYPE_VALUE (name, t);
3941 else
3943 *ptr = newval;
3946 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3947 return t;
3950 /* Insert USED into the using list of USER. Set INDIRECT_flag if this
3951 directive is not directly from the source. Also find the common
3952 ancestor and let our users know about the new namespace */
3954 static void
3955 add_using_namespace_1 (tree user, tree used, bool indirect)
3957 tree t;
3958 /* Using oneself is a no-op. */
3959 if (user == used)
3960 return;
3961 gcc_assert (TREE_CODE (user) == NAMESPACE_DECL);
3962 gcc_assert (TREE_CODE (used) == NAMESPACE_DECL);
3963 /* Check if we already have this. */
3964 t = purpose_member (used, DECL_NAMESPACE_USING (user));
3965 if (t != NULL_TREE)
3967 if (!indirect)
3968 /* Promote to direct usage. */
3969 TREE_INDIRECT_USING (t) = 0;
3970 return;
3973 /* Add used to the user's using list. */
3974 DECL_NAMESPACE_USING (user)
3975 = tree_cons (used, namespace_ancestor (user, used),
3976 DECL_NAMESPACE_USING (user));
3978 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect;
3980 /* Add user to the used's users list. */
3981 DECL_NAMESPACE_USERS (used)
3982 = tree_cons (user, 0, DECL_NAMESPACE_USERS (used));
3984 /* Recursively add all namespaces used. */
3985 for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t))
3986 /* indirect usage */
3987 add_using_namespace_1 (user, TREE_PURPOSE (t), 1);
3989 /* Tell everyone using us about the new used namespaces. */
3990 for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t))
3991 add_using_namespace_1 (TREE_PURPOSE (t), used, 1);
3994 /* Wrapper for add_using_namespace_1. */
3996 static void
3997 add_using_namespace (tree user, tree used, bool indirect)
3999 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4000 add_using_namespace_1 (user, used, indirect);
4001 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4004 /* Process a using-declaration not appearing in class or local scope. */
4006 void
4007 do_toplevel_using_decl (tree decl, tree scope, tree name)
4009 tree oldval, oldtype, newval, newtype;
4010 tree orig_decl = decl;
4011 cxx_binding *binding;
4013 decl = validate_nonmember_using_decl (decl, scope, name);
4014 if (decl == NULL_TREE)
4015 return;
4017 binding = binding_for_name (NAMESPACE_LEVEL (current_namespace), name);
4019 oldval = binding->value;
4020 oldtype = binding->type;
4022 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
4024 /* Emit debug info. */
4025 if (!processing_template_decl)
4026 cp_emit_debug_info_for_using (orig_decl, current_namespace);
4028 /* Copy declarations found. */
4029 if (newval)
4030 binding->value = newval;
4031 if (newtype)
4032 binding->type = newtype;
4035 /* Process a using-directive. */
4037 void
4038 do_using_directive (tree name_space)
4040 tree context = NULL_TREE;
4042 if (name_space == error_mark_node)
4043 return;
4045 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
4047 if (building_stmt_list_p ())
4048 add_stmt (build_stmt (input_location, USING_STMT, name_space));
4049 name_space = ORIGINAL_NAMESPACE (name_space);
4051 if (!toplevel_bindings_p ())
4053 push_using_directive (name_space);
4055 else
4057 /* direct usage */
4058 add_using_namespace (current_namespace, name_space, 0);
4059 if (current_namespace != global_namespace)
4060 context = current_namespace;
4062 /* Emit debugging info. */
4063 if (!processing_template_decl)
4064 (*debug_hooks->imported_module_or_decl) (name_space, NULL_TREE,
4065 context, false);
4069 /* Deal with a using-directive seen by the parser. Currently we only
4070 handle attributes here, since they cannot appear inside a template. */
4072 void
4073 parse_using_directive (tree name_space, tree attribs)
4075 do_using_directive (name_space);
4077 if (attribs == error_mark_node)
4078 return;
4080 for (tree a = attribs; a; a = TREE_CHAIN (a))
4082 tree name = get_attribute_name (a);
4083 if (is_attribute_p ("strong", name))
4085 if (!toplevel_bindings_p ())
4086 error ("strong using only meaningful at namespace scope");
4087 else if (name_space != error_mark_node)
4089 if (!is_ancestor (current_namespace, name_space))
4090 error ("current namespace %qD does not enclose strongly used namespace %qD",
4091 current_namespace, name_space);
4092 DECL_NAMESPACE_ASSOCIATIONS (name_space)
4093 = tree_cons (current_namespace, 0,
4094 DECL_NAMESPACE_ASSOCIATIONS (name_space));
4097 else
4098 warning (OPT_Wattributes, "%qD attribute directive ignored", name);
4102 /* Like pushdecl, only it places X in the global scope if appropriate.
4103 Calls cp_finish_decl to register the variable, initializing it with
4104 *INIT, if INIT is non-NULL. */
4106 static tree
4107 pushdecl_top_level_1 (tree x, tree *init, bool is_friend)
4109 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4110 push_to_top_level ();
4111 x = pushdecl_namespace_level (x, is_friend);
4112 if (init)
4113 cp_finish_decl (x, *init, false, NULL_TREE, 0);
4114 pop_from_top_level ();
4115 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4116 return x;
4119 /* Like pushdecl, only it places X in the global scope if appropriate. */
4121 tree
4122 pushdecl_top_level (tree x)
4124 return pushdecl_top_level_1 (x, NULL, false);
4127 /* Like pushdecl_top_level, but adding the IS_FRIEND parameter. */
4129 tree
4130 pushdecl_top_level_maybe_friend (tree x, bool is_friend)
4132 return pushdecl_top_level_1 (x, NULL, is_friend);
4135 /* Like pushdecl, only it places X in the global scope if
4136 appropriate. Calls cp_finish_decl to register the variable,
4137 initializing it with INIT. */
4139 tree
4140 pushdecl_top_level_and_finish (tree x, tree init)
4142 return pushdecl_top_level_1 (x, &init, false);
4145 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
4146 duplicates. The first list becomes the tail of the result.
4148 The algorithm is O(n^2). We could get this down to O(n log n) by
4149 doing a sort on the addresses of the functions, if that becomes
4150 necessary. */
4152 static tree
4153 merge_functions (tree s1, tree s2)
4155 for (; s2; s2 = OVL_NEXT (s2))
4157 tree fn2 = OVL_CURRENT (s2);
4158 tree fns1;
4160 for (fns1 = s1; fns1; fns1 = OVL_NEXT (fns1))
4162 tree fn1 = OVL_CURRENT (fns1);
4164 /* If the function from S2 is already in S1, there is no
4165 need to add it again. For `extern "C"' functions, we
4166 might have two FUNCTION_DECLs for the same function, in
4167 different namespaces, but let's leave them in case
4168 they have different default arguments. */
4169 if (fn1 == fn2)
4170 break;
4173 /* If we exhausted all of the functions in S1, FN2 is new. */
4174 if (!fns1)
4175 s1 = build_overload (fn2, s1);
4177 return s1;
4180 /* Returns TRUE iff OLD and NEW are the same entity.
4182 3 [basic]/3: An entity is a value, object, reference, function,
4183 enumerator, type, class member, template, template specialization,
4184 namespace, parameter pack, or this.
4186 7.3.4 [namespace.udir]/4: If name lookup finds a declaration for a name
4187 in two different namespaces, and the declarations do not declare the
4188 same entity and do not declare functions, the use of the name is
4189 ill-formed. */
4191 static bool
4192 same_entity_p (tree one, tree two)
4194 if (one == two)
4195 return true;
4196 if (!one || !two)
4197 return false;
4198 if (TREE_CODE (one) == TYPE_DECL
4199 && TREE_CODE (two) == TYPE_DECL
4200 && same_type_p (TREE_TYPE (one), TREE_TYPE (two)))
4201 return true;
4202 return false;
4205 /* This should return an error not all definitions define functions.
4206 It is not an error if we find two functions with exactly the
4207 same signature, only if these are selected in overload resolution.
4208 old is the current set of bindings, new_binding the freshly-found binding.
4209 XXX Do we want to give *all* candidates in case of ambiguity?
4210 XXX In what way should I treat extern declarations?
4211 XXX I don't want to repeat the entire duplicate_decls here */
4213 static void
4214 ambiguous_decl (struct scope_binding *old, cxx_binding *new_binding, int flags)
4216 tree val, type;
4217 gcc_assert (old != NULL);
4219 /* Copy the type. */
4220 type = new_binding->type;
4221 if (LOOKUP_NAMESPACES_ONLY (flags)
4222 || (type && hidden_name_p (type) && !(flags & LOOKUP_HIDDEN)))
4223 type = NULL_TREE;
4225 /* Copy the value. */
4226 val = new_binding->value;
4227 if (val)
4229 if (!(flags & LOOKUP_HIDDEN))
4230 val = remove_hidden_names (val);
4231 if (val)
4232 switch (TREE_CODE (val))
4234 case TEMPLATE_DECL:
4235 /* If we expect types or namespaces, and not templates,
4236 or this is not a template class. */
4237 if ((LOOKUP_QUALIFIERS_ONLY (flags)
4238 && !DECL_TYPE_TEMPLATE_P (val)))
4239 val = NULL_TREE;
4240 break;
4241 case TYPE_DECL:
4242 if (LOOKUP_NAMESPACES_ONLY (flags)
4243 || (type && (flags & LOOKUP_PREFER_TYPES)))
4244 val = NULL_TREE;
4245 break;
4246 case NAMESPACE_DECL:
4247 if (LOOKUP_TYPES_ONLY (flags))
4248 val = NULL_TREE;
4249 break;
4250 case FUNCTION_DECL:
4251 /* Ignore built-in functions that are still anticipated. */
4252 if (LOOKUP_QUALIFIERS_ONLY (flags))
4253 val = NULL_TREE;
4254 break;
4255 default:
4256 if (LOOKUP_QUALIFIERS_ONLY (flags))
4257 val = NULL_TREE;
4261 /* If val is hidden, shift down any class or enumeration name. */
4262 if (!val)
4264 val = type;
4265 type = NULL_TREE;
4268 if (!old->value)
4269 old->value = val;
4270 else if (val && !same_entity_p (val, old->value))
4272 if (is_overloaded_fn (old->value) && is_overloaded_fn (val))
4273 old->value = merge_functions (old->value, val);
4274 else
4276 old->value = tree_cons (NULL_TREE, old->value,
4277 build_tree_list (NULL_TREE, val));
4278 TREE_TYPE (old->value) = error_mark_node;
4282 if (!old->type)
4283 old->type = type;
4284 else if (type && old->type != type)
4286 old->type = tree_cons (NULL_TREE, old->type,
4287 build_tree_list (NULL_TREE, type));
4288 TREE_TYPE (old->type) = error_mark_node;
4292 /* Return the declarations that are members of the namespace NS. */
4294 tree
4295 cp_namespace_decls (tree ns)
4297 return NAMESPACE_LEVEL (ns)->names;
4300 /* Combine prefer_type and namespaces_only into flags. */
4302 static int
4303 lookup_flags (int prefer_type, int namespaces_only)
4305 if (namespaces_only)
4306 return LOOKUP_PREFER_NAMESPACES;
4307 if (prefer_type > 1)
4308 return LOOKUP_PREFER_TYPES;
4309 if (prefer_type > 0)
4310 return LOOKUP_PREFER_BOTH;
4311 return 0;
4314 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
4315 ignore it or not. Subroutine of lookup_name_real and
4316 lookup_type_scope. */
4318 static bool
4319 qualify_lookup (tree val, int flags)
4321 if (val == NULL_TREE)
4322 return false;
4323 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
4324 return true;
4325 if (flags & LOOKUP_PREFER_TYPES)
4327 tree target_val = strip_using_decl (val);
4328 if (TREE_CODE (target_val) == TYPE_DECL
4329 || TREE_CODE (target_val) == TEMPLATE_DECL)
4330 return true;
4332 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
4333 return false;
4334 /* Look through lambda things that we shouldn't be able to see. */
4335 if (is_lambda_ignored_entity (val))
4336 return false;
4337 return true;
4340 /* Given a lookup that returned VAL, decide if we want to ignore it or
4341 not based on DECL_ANTICIPATED. */
4343 bool
4344 hidden_name_p (tree val)
4346 if (DECL_P (val)
4347 && DECL_LANG_SPECIFIC (val)
4348 && TYPE_FUNCTION_OR_TEMPLATE_DECL_P (val)
4349 && DECL_ANTICIPATED (val))
4350 return true;
4351 if (TREE_CODE (val) == OVERLOAD)
4353 for (tree o = val; o; o = OVL_CHAIN (o))
4354 if (!hidden_name_p (OVL_FUNCTION (o)))
4355 return false;
4356 return true;
4358 return false;
4361 /* Remove any hidden declarations from a possibly overloaded set
4362 of functions. */
4364 tree
4365 remove_hidden_names (tree fns)
4367 if (!fns)
4368 return fns;
4370 if (DECL_P (fns) && hidden_name_p (fns))
4371 fns = NULL_TREE;
4372 else if (TREE_CODE (fns) == OVERLOAD)
4374 tree o;
4376 for (o = fns; o; o = OVL_NEXT (o))
4377 if (hidden_name_p (OVL_CURRENT (o)))
4378 break;
4379 if (o)
4381 tree n = NULL_TREE;
4383 for (o = fns; o; o = OVL_NEXT (o))
4384 if (!hidden_name_p (OVL_CURRENT (o)))
4385 n = build_overload (OVL_CURRENT (o), n);
4386 fns = n;
4390 return fns;
4393 /* Suggest alternatives for NAME, an IDENTIFIER_NODE for which name
4394 lookup failed. Search through all available namespaces and print out
4395 possible candidates. */
4397 void
4398 suggest_alternatives_for (location_t location, tree name)
4400 vec<tree> candidates = vNULL;
4401 vec<tree> namespaces_to_search = vNULL;
4402 int max_to_search = PARAM_VALUE (CXX_MAX_NAMESPACES_FOR_DIAGNOSTIC_HELP);
4403 int n_searched = 0;
4404 tree t;
4405 unsigned ix;
4407 namespaces_to_search.safe_push (global_namespace);
4409 while (!namespaces_to_search.is_empty ()
4410 && n_searched < max_to_search)
4412 tree scope = namespaces_to_search.pop ();
4413 struct scope_binding binding = EMPTY_SCOPE_BINDING;
4414 cp_binding_level *level = NAMESPACE_LEVEL (scope);
4416 /* Look in this namespace. */
4417 qualified_lookup_using_namespace (name, scope, &binding, 0);
4419 n_searched++;
4421 if (binding.value)
4422 candidates.safe_push (binding.value);
4424 /* Add child namespaces. */
4425 for (t = level->namespaces; t; t = DECL_CHAIN (t))
4426 namespaces_to_search.safe_push (t);
4429 /* If we stopped before we could examine all namespaces, inform the
4430 user. Do this even if we don't have any candidates, since there
4431 might be more candidates further down that we weren't able to
4432 find. */
4433 if (n_searched >= max_to_search
4434 && !namespaces_to_search.is_empty ())
4435 inform (location,
4436 "maximum limit of %d namespaces searched for %qE",
4437 max_to_search, name);
4439 namespaces_to_search.release ();
4441 /* Nothing useful to report for NAME. Report on likely misspellings,
4442 or do nothing. */
4443 if (candidates.is_empty ())
4445 const char *fuzzy_name = lookup_name_fuzzy (name, FUZZY_LOOKUP_NAME);
4446 if (fuzzy_name)
4448 gcc_rich_location richloc (location);
4449 richloc.add_fixit_replace (fuzzy_name);
4450 inform_at_rich_loc (&richloc, "suggested alternative: %qs",
4451 fuzzy_name);
4453 return;
4456 inform_n (location, candidates.length (),
4457 "suggested alternative:",
4458 "suggested alternatives:");
4460 FOR_EACH_VEC_ELT (candidates, ix, t)
4461 inform (location_of (t), " %qE", t);
4463 candidates.release ();
4466 /* Unscoped lookup of a global: iterate over current namespaces,
4467 considering using-directives. */
4469 static tree
4470 unqualified_namespace_lookup_1 (tree name, int flags)
4472 tree initial = current_decl_namespace ();
4473 tree scope = initial;
4474 tree siter;
4475 cp_binding_level *level;
4476 tree val = NULL_TREE;
4478 for (; !val; scope = CP_DECL_CONTEXT (scope))
4480 struct scope_binding binding = EMPTY_SCOPE_BINDING;
4481 cxx_binding *b =
4482 cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
4484 if (b)
4485 ambiguous_decl (&binding, b, flags);
4487 /* Add all _DECLs seen through local using-directives. */
4488 for (level = current_binding_level;
4489 level->kind != sk_namespace;
4490 level = level->level_chain)
4491 if (!lookup_using_namespace (name, &binding, level->using_directives,
4492 scope, flags))
4493 /* Give up because of error. */
4494 return error_mark_node;
4496 /* Add all _DECLs seen through global using-directives. */
4497 /* XXX local and global using lists should work equally. */
4498 siter = initial;
4499 while (1)
4501 if (!lookup_using_namespace (name, &binding,
4502 DECL_NAMESPACE_USING (siter),
4503 scope, flags))
4504 /* Give up because of error. */
4505 return error_mark_node;
4506 if (siter == scope) break;
4507 siter = CP_DECL_CONTEXT (siter);
4510 val = binding.value;
4511 if (scope == global_namespace)
4512 break;
4514 return val;
4517 /* Wrapper for unqualified_namespace_lookup_1. */
4519 static tree
4520 unqualified_namespace_lookup (tree name, int flags)
4522 tree ret;
4523 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4524 ret = unqualified_namespace_lookup_1 (name, flags);
4525 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4526 return ret;
4529 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
4530 or a class TYPE).
4532 If PREFER_TYPE is > 0, we only return TYPE_DECLs or namespaces.
4533 If PREFER_TYPE is > 1, we only return TYPE_DECLs.
4535 Returns a DECL (or OVERLOAD, or BASELINK) representing the
4536 declaration found. If no suitable declaration can be found,
4537 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
4538 neither a class-type nor a namespace a diagnostic is issued. */
4540 tree
4541 lookup_qualified_name (tree scope, tree name, int prefer_type, bool complain,
4542 bool find_hidden)
4544 tree t = NULL_TREE;
4546 if (TREE_CODE (scope) == NAMESPACE_DECL)
4548 struct scope_binding binding = EMPTY_SCOPE_BINDING;
4550 int flags = lookup_flags (prefer_type, /*namespaces_only*/false);
4551 if (find_hidden)
4552 flags |= LOOKUP_HIDDEN;
4553 if (qualified_lookup_using_namespace (name, scope, &binding, flags))
4554 t = binding.value;
4556 else if (cxx_dialect != cxx98 && TREE_CODE (scope) == ENUMERAL_TYPE)
4557 t = lookup_enumerator (scope, name);
4558 else if (is_class_type (scope, complain))
4559 t = lookup_member (scope, name, 2, prefer_type, tf_warning_or_error);
4561 if (!t)
4562 return error_mark_node;
4563 return t;
4566 /* Subroutine of unqualified_namespace_lookup:
4567 Add the bindings of NAME in used namespaces to VAL.
4568 We are currently looking for names in namespace SCOPE, so we
4569 look through USINGS for using-directives of namespaces
4570 which have SCOPE as a common ancestor with the current scope.
4571 Returns false on errors. */
4573 static bool
4574 lookup_using_namespace (tree name, struct scope_binding *val,
4575 tree usings, tree scope, int flags)
4577 tree iter;
4578 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4579 /* Iterate over all used namespaces in current, searching for using
4580 directives of scope. */
4581 for (iter = usings; iter; iter = TREE_CHAIN (iter))
4582 if (TREE_VALUE (iter) == scope)
4584 tree used = ORIGINAL_NAMESPACE (TREE_PURPOSE (iter));
4585 cxx_binding *val1 =
4586 cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (used), name);
4587 /* Resolve ambiguities. */
4588 if (val1)
4589 ambiguous_decl (val, val1, flags);
4591 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4592 return val->value != error_mark_node;
4595 /* Returns true iff VEC contains TARGET. */
4597 static bool
4598 tree_vec_contains (vec<tree, va_gc> *vec, tree target)
4600 unsigned int i;
4601 tree elt;
4602 FOR_EACH_VEC_SAFE_ELT (vec,i,elt)
4603 if (elt == target)
4604 return true;
4605 return false;
4608 /* [namespace.qual]
4609 Accepts the NAME to lookup and its qualifying SCOPE.
4610 Returns the name/type pair found into the cxx_binding *RESULT,
4611 or false on error. */
4613 static bool
4614 qualified_lookup_using_namespace (tree name, tree scope,
4615 struct scope_binding *result, int flags)
4617 /* Maintain a list of namespaces visited... */
4618 vec<tree, va_gc> *seen = NULL;
4619 vec<tree, va_gc> *seen_inline = NULL;
4620 /* ... and a list of namespace yet to see. */
4621 vec<tree, va_gc> *todo = NULL;
4622 vec<tree, va_gc> *todo_maybe = NULL;
4623 vec<tree, va_gc> *todo_inline = NULL;
4624 tree usings;
4625 timevar_start (TV_NAME_LOOKUP);
4626 /* Look through namespace aliases. */
4627 scope = ORIGINAL_NAMESPACE (scope);
4629 /* Algorithm: Starting with SCOPE, walk through the set of used
4630 namespaces. For each used namespace, look through its inline
4631 namespace set for any bindings and usings. If no bindings are
4632 found, add any usings seen to the set of used namespaces. */
4633 vec_safe_push (todo, scope);
4635 while (todo->length ())
4637 bool found_here;
4638 scope = todo->pop ();
4639 if (tree_vec_contains (seen, scope))
4640 continue;
4641 vec_safe_push (seen, scope);
4642 vec_safe_push (todo_inline, scope);
4644 found_here = false;
4645 while (todo_inline->length ())
4647 cxx_binding *binding;
4649 scope = todo_inline->pop ();
4650 if (tree_vec_contains (seen_inline, scope))
4651 continue;
4652 vec_safe_push (seen_inline, scope);
4654 binding =
4655 cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
4656 if (binding)
4658 ambiguous_decl (result, binding, flags);
4659 if (result->type || result->value)
4660 found_here = true;
4663 for (usings = DECL_NAMESPACE_USING (scope); usings;
4664 usings = TREE_CHAIN (usings))
4665 if (!TREE_INDIRECT_USING (usings))
4667 if (is_associated_namespace (scope, TREE_PURPOSE (usings)))
4668 vec_safe_push (todo_inline, TREE_PURPOSE (usings));
4669 else
4670 vec_safe_push (todo_maybe, TREE_PURPOSE (usings));
4674 if (found_here)
4675 vec_safe_truncate (todo_maybe, 0);
4676 else
4677 while (vec_safe_length (todo_maybe))
4678 vec_safe_push (todo, todo_maybe->pop ());
4680 vec_free (todo);
4681 vec_free (todo_maybe);
4682 vec_free (todo_inline);
4683 vec_free (seen);
4684 vec_free (seen_inline);
4685 timevar_stop (TV_NAME_LOOKUP);
4686 return result->value != error_mark_node;
4689 /* Helper function for lookup_name_fuzzy.
4690 Traverse binding level LVL, looking for good name matches for NAME
4691 (and BM). */
4692 static void
4693 consider_binding_level (tree name, best_match <tree, tree> &bm,
4694 cp_binding_level *lvl, bool look_within_fields,
4695 enum lookup_name_fuzzy_kind kind)
4697 if (look_within_fields)
4698 if (lvl->this_entity && TREE_CODE (lvl->this_entity) == RECORD_TYPE)
4700 tree type = lvl->this_entity;
4701 bool want_type_p = (kind == FUZZY_LOOKUP_TYPENAME);
4702 tree best_matching_field
4703 = lookup_member_fuzzy (type, name, want_type_p);
4704 if (best_matching_field)
4705 bm.consider (best_matching_field);
4708 for (tree t = lvl->names; t; t = TREE_CHAIN (t))
4710 tree d = t;
4712 /* OVERLOADs or decls from using declaration are wrapped into
4713 TREE_LIST. */
4714 if (TREE_CODE (d) == TREE_LIST)
4716 d = TREE_VALUE (d);
4717 d = OVL_CURRENT (d);
4720 /* Don't use bindings from implicitly declared functions,
4721 as they were likely misspellings themselves. */
4722 if (TREE_TYPE (d) == error_mark_node)
4723 continue;
4725 /* Skip anticipated decls of builtin functions. */
4726 if (TREE_CODE (d) == FUNCTION_DECL
4727 && DECL_BUILT_IN (d)
4728 && DECL_ANTICIPATED (d))
4729 continue;
4731 if (DECL_NAME (d))
4732 bm.consider (DECL_NAME (d));
4736 /* Search for near-matches for NAME within the current bindings, and within
4737 macro names, returning the best match as a const char *, or NULL if
4738 no reasonable match is found. */
4740 const char *
4741 lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind)
4743 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
4745 best_match <tree, tree> bm (name);
4747 cp_binding_level *lvl;
4748 for (lvl = scope_chain->class_bindings; lvl; lvl = lvl->level_chain)
4749 consider_binding_level (name, bm, lvl, true, kind);
4751 for (lvl = current_binding_level; lvl; lvl = lvl->level_chain)
4752 consider_binding_level (name, bm, lvl, false, kind);
4754 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
4756 x = SOME_OTHER_MACRO (y);
4757 then "SOME_OTHER_MACRO" will survive to the frontend and show up
4758 as a misspelled identifier.
4760 Use the best distance so far so that a candidate is only set if
4761 a macro is better than anything so far. This allows early rejection
4762 (without calculating the edit distance) of macro names that must have
4763 distance >= bm.get_best_distance (), and means that we only get a
4764 non-NULL result for best_macro_match if it's better than any of
4765 the identifiers already checked. */
4766 best_macro_match bmm (name, bm.get_best_distance (), parse_in);
4767 cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
4768 /* If a macro is the closest so far to NAME, suggest it. */
4769 if (best_macro)
4770 return (const char *)best_macro->ident.str;
4772 /* Try the "starts_decl_specifier_p" keywords to detect
4773 "singed" vs "signed" typos. */
4774 for (unsigned i = 0; i < num_c_common_reswords; i++)
4776 const c_common_resword *resword = &c_common_reswords[i];
4778 if (!cp_keyword_starts_decl_specifier_p (resword->rid))
4779 continue;
4781 tree resword_identifier = ridpointers [resword->rid];
4782 if (!resword_identifier)
4783 continue;
4784 gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
4785 bm.consider (resword_identifier);
4788 /* See if we have a good suggesion for the user. */
4789 tree best_id = bm.get_best_meaningful_candidate ();
4790 if (best_id)
4791 return IDENTIFIER_POINTER (best_id);
4793 /* No meaningful suggestion available. */
4794 return NULL;
4797 /* Subroutine of outer_binding.
4799 Returns TRUE if BINDING is a binding to a template parameter of
4800 SCOPE. In that case SCOPE is the scope of a primary template
4801 parameter -- in the sense of G++, i.e, a template that has its own
4802 template header.
4804 Returns FALSE otherwise. */
4806 static bool
4807 binding_to_template_parms_of_scope_p (cxx_binding *binding,
4808 cp_binding_level *scope)
4810 tree binding_value, tmpl, tinfo;
4811 int level;
4813 if (!binding || !scope || !scope->this_entity)
4814 return false;
4816 binding_value = binding->value ? binding->value : binding->type;
4817 tinfo = get_template_info (scope->this_entity);
4819 /* BINDING_VALUE must be a template parm. */
4820 if (binding_value == NULL_TREE
4821 || (!DECL_P (binding_value)
4822 || !DECL_TEMPLATE_PARM_P (binding_value)))
4823 return false;
4825 /* The level of BINDING_VALUE. */
4826 level =
4827 template_type_parameter_p (binding_value)
4828 ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX
4829 (TREE_TYPE (binding_value)))
4830 : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value));
4832 /* The template of the current scope, iff said scope is a primary
4833 template. */
4834 tmpl = (tinfo
4835 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
4836 ? TI_TEMPLATE (tinfo)
4837 : NULL_TREE);
4839 /* If the level of the parm BINDING_VALUE equals the depth of TMPL,
4840 then BINDING_VALUE is a parameter of TMPL. */
4841 return (tmpl && level == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
4844 /* Return the innermost non-namespace binding for NAME from a scope
4845 containing BINDING, or, if BINDING is NULL, the current scope.
4846 Please note that for a given template, the template parameters are
4847 considered to be in the scope containing the current scope.
4848 If CLASS_P is false, then class bindings are ignored. */
4850 cxx_binding *
4851 outer_binding (tree name,
4852 cxx_binding *binding,
4853 bool class_p)
4855 cxx_binding *outer;
4856 cp_binding_level *scope;
4857 cp_binding_level *outer_scope;
4859 if (binding)
4861 scope = binding->scope->level_chain;
4862 outer = binding->previous;
4864 else
4866 scope = current_binding_level;
4867 outer = IDENTIFIER_BINDING (name);
4869 outer_scope = outer ? outer->scope : NULL;
4871 /* Because we create class bindings lazily, we might be missing a
4872 class binding for NAME. If there are any class binding levels
4873 between the LAST_BINDING_LEVEL and the scope in which OUTER was
4874 declared, we must lookup NAME in those class scopes. */
4875 if (class_p)
4876 while (scope && scope != outer_scope && scope->kind != sk_namespace)
4878 if (scope->kind == sk_class)
4880 cxx_binding *class_binding;
4882 class_binding = get_class_binding (name, scope);
4883 if (class_binding)
4885 /* Thread this new class-scope binding onto the
4886 IDENTIFIER_BINDING list so that future lookups
4887 find it quickly. */
4888 class_binding->previous = outer;
4889 if (binding)
4890 binding->previous = class_binding;
4891 else
4892 IDENTIFIER_BINDING (name) = class_binding;
4893 return class_binding;
4896 /* If we are in a member template, the template parms of the member
4897 template are considered to be inside the scope of the containing
4898 class, but within G++ the class bindings are all pushed between the
4899 template parms and the function body. So if the outer binding is
4900 a template parm for the current scope, return it now rather than
4901 look for a class binding. */
4902 if (outer_scope && outer_scope->kind == sk_template_parms
4903 && binding_to_template_parms_of_scope_p (outer, scope))
4904 return outer;
4906 scope = scope->level_chain;
4909 return outer;
4912 /* Return the innermost block-scope or class-scope value binding for
4913 NAME, or NULL_TREE if there is no such binding. */
4915 tree
4916 innermost_non_namespace_value (tree name)
4918 cxx_binding *binding;
4919 binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true);
4920 return binding ? binding->value : NULL_TREE;
4923 /* Look up NAME in the current binding level and its superiors in the
4924 namespace of variables, functions and typedefs. Return a ..._DECL
4925 node of some kind representing its definition if there is only one
4926 such declaration, or return a TREE_LIST with all the overloaded
4927 definitions if there are many, or return 0 if it is undefined.
4928 Hidden name, either friend declaration or built-in function, are
4929 not ignored.
4931 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
4932 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
4933 Otherwise we prefer non-TYPE_DECLs.
4935 If NONCLASS is nonzero, bindings in class scopes are ignored. If
4936 BLOCK_P is false, bindings in block scopes are ignored. */
4938 static tree
4939 lookup_name_real_1 (tree name, int prefer_type, int nonclass, bool block_p,
4940 int namespaces_only, int flags)
4942 cxx_binding *iter;
4943 tree val = NULL_TREE;
4945 /* Conversion operators are handled specially because ordinary
4946 unqualified name lookup will not find template conversion
4947 operators. */
4948 if (IDENTIFIER_TYPENAME_P (name))
4950 cp_binding_level *level;
4952 for (level = current_binding_level;
4953 level && level->kind != sk_namespace;
4954 level = level->level_chain)
4956 tree class_type;
4957 tree operators;
4959 /* A conversion operator can only be declared in a class
4960 scope. */
4961 if (level->kind != sk_class)
4962 continue;
4964 /* Lookup the conversion operator in the class. */
4965 class_type = level->this_entity;
4966 operators = lookup_fnfields (class_type, name, /*protect=*/0);
4967 if (operators)
4968 return operators;
4971 return NULL_TREE;
4974 flags |= lookup_flags (prefer_type, namespaces_only);
4976 /* First, look in non-namespace scopes. */
4978 if (current_class_type == NULL_TREE)
4979 nonclass = 1;
4981 if (block_p || !nonclass)
4982 for (iter = outer_binding (name, NULL, !nonclass);
4983 iter;
4984 iter = outer_binding (name, iter, !nonclass))
4986 tree binding;
4988 /* Skip entities we don't want. */
4989 if (LOCAL_BINDING_P (iter) ? !block_p : nonclass)
4990 continue;
4992 /* If this is the kind of thing we're looking for, we're done. */
4993 if (qualify_lookup (iter->value, flags))
4994 binding = iter->value;
4995 else if ((flags & LOOKUP_PREFER_TYPES)
4996 && qualify_lookup (iter->type, flags))
4997 binding = iter->type;
4998 else
4999 binding = NULL_TREE;
5001 if (binding)
5003 if (hidden_name_p (binding))
5005 /* A non namespace-scope binding can only be hidden in the
5006 presence of a local class, due to friend declarations.
5008 In particular, consider:
5010 struct C;
5011 void f() {
5012 struct A {
5013 friend struct B;
5014 friend struct C;
5015 void g() {
5016 B* b; // error: B is hidden
5017 C* c; // OK, finds ::C
5020 B *b; // error: B is hidden
5021 C *c; // OK, finds ::C
5022 struct B {};
5023 B *bb; // OK
5026 The standard says that "B" is a local class in "f"
5027 (but not nested within "A") -- but that name lookup
5028 for "B" does not find this declaration until it is
5029 declared directly with "f".
5031 In particular:
5033 [class.friend]
5035 If a friend declaration appears in a local class and
5036 the name specified is an unqualified name, a prior
5037 declaration is looked up without considering scopes
5038 that are outside the innermost enclosing non-class
5039 scope. For a friend function declaration, if there is
5040 no prior declaration, the program is ill-formed. For a
5041 friend class declaration, if there is no prior
5042 declaration, the class that is specified belongs to the
5043 innermost enclosing non-class scope, but if it is
5044 subsequently referenced, its name is not found by name
5045 lookup until a matching declaration is provided in the
5046 innermost enclosing nonclass scope.
5048 So just keep looking for a non-hidden binding.
5050 gcc_assert (TREE_CODE (binding) == TYPE_DECL);
5051 continue;
5053 val = binding;
5054 break;
5058 /* Now lookup in namespace scopes. */
5059 if (!val)
5060 val = unqualified_namespace_lookup (name, flags);
5062 /* Anticipated built-ins and friends aren't found by normal lookup. */
5063 if (val && !(flags & LOOKUP_HIDDEN))
5064 val = remove_hidden_names (val);
5066 /* If we have a single function from a using decl, pull it out. */
5067 if (val && TREE_CODE (val) == OVERLOAD && !really_overloaded_fn (val))
5068 val = OVL_FUNCTION (val);
5070 return val;
5073 /* Wrapper for lookup_name_real_1. */
5075 tree
5076 lookup_name_real (tree name, int prefer_type, int nonclass, bool block_p,
5077 int namespaces_only, int flags)
5079 tree ret;
5080 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5081 ret = lookup_name_real_1 (name, prefer_type, nonclass, block_p,
5082 namespaces_only, flags);
5083 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5084 return ret;
5087 tree
5088 lookup_name_nonclass (tree name)
5090 return lookup_name_real (name, 0, 1, /*block_p=*/true, 0, 0);
5093 tree
5094 lookup_function_nonclass (tree name, vec<tree, va_gc> *args, bool block_p)
5096 return
5097 lookup_arg_dependent (name,
5098 lookup_name_real (name, 0, 1, block_p, 0, 0),
5099 args);
5102 tree
5103 lookup_name (tree name)
5105 return lookup_name_real (name, 0, 0, /*block_p=*/true, 0, 0);
5108 tree
5109 lookup_name_prefer_type (tree name, int prefer_type)
5111 return lookup_name_real (name, prefer_type, 0, /*block_p=*/true, 0, 0);
5114 /* Look up NAME for type used in elaborated name specifier in
5115 the scopes given by SCOPE. SCOPE can be either TS_CURRENT or
5116 TS_WITHIN_ENCLOSING_NON_CLASS. Although not implied by the
5117 name, more scopes are checked if cleanup or template parameter
5118 scope is encountered.
5120 Unlike lookup_name_real, we make sure that NAME is actually
5121 declared in the desired scope, not from inheritance, nor using
5122 directive. For using declaration, there is DR138 still waiting
5123 to be resolved. Hidden name coming from an earlier friend
5124 declaration is also returned.
5126 A TYPE_DECL best matching the NAME is returned. Catching error
5127 and issuing diagnostics are caller's responsibility. */
5129 static tree
5130 lookup_type_scope_1 (tree name, tag_scope scope)
5132 cxx_binding *iter = NULL;
5133 tree val = NULL_TREE;
5135 /* Look in non-namespace scope first. */
5136 if (current_binding_level->kind != sk_namespace)
5137 iter = outer_binding (name, NULL, /*class_p=*/ true);
5138 for (; iter; iter = outer_binding (name, iter, /*class_p=*/ true))
5140 /* Check if this is the kind of thing we're looking for.
5141 If SCOPE is TS_CURRENT, also make sure it doesn't come from
5142 base class. For ITER->VALUE, we can simply use
5143 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use
5144 our own check.
5146 We check ITER->TYPE before ITER->VALUE in order to handle
5147 typedef struct C {} C;
5148 correctly. */
5150 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES)
5151 && (scope != ts_current
5152 || LOCAL_BINDING_P (iter)
5153 || DECL_CONTEXT (iter->type) == iter->scope->this_entity))
5154 val = iter->type;
5155 else if ((scope != ts_current
5156 || !INHERITED_VALUE_BINDING_P (iter))
5157 && qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
5158 val = iter->value;
5160 if (val)
5161 break;
5164 /* Look in namespace scope. */
5165 if (!val)
5167 iter = cp_binding_level_find_binding_for_name
5168 (NAMESPACE_LEVEL (current_decl_namespace ()), name);
5170 if (iter)
5172 /* If this is the kind of thing we're looking for, we're done. */
5173 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES))
5174 val = iter->type;
5175 else if (qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
5176 val = iter->value;
5181 /* Type found, check if it is in the allowed scopes, ignoring cleanup
5182 and template parameter scopes. */
5183 if (val)
5185 cp_binding_level *b = current_binding_level;
5186 while (b)
5188 if (iter->scope == b)
5189 return val;
5191 if (b->kind == sk_cleanup || b->kind == sk_template_parms
5192 || b->kind == sk_function_parms)
5193 b = b->level_chain;
5194 else if (b->kind == sk_class
5195 && scope == ts_within_enclosing_non_class)
5196 b = b->level_chain;
5197 else
5198 break;
5202 return NULL_TREE;
5205 /* Wrapper for lookup_type_scope_1. */
5207 tree
5208 lookup_type_scope (tree name, tag_scope scope)
5210 tree ret;
5211 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5212 ret = lookup_type_scope_1 (name, scope);
5213 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5214 return ret;
5218 /* Similar to `lookup_name' but look only in the innermost non-class
5219 binding level. */
5221 static tree
5222 lookup_name_innermost_nonclass_level_1 (tree name)
5224 cp_binding_level *b;
5225 tree t = NULL_TREE;
5227 b = innermost_nonclass_level ();
5229 if (b->kind == sk_namespace)
5231 t = IDENTIFIER_NAMESPACE_VALUE (name);
5233 /* extern "C" function() */
5234 if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
5235 t = TREE_VALUE (t);
5237 else if (IDENTIFIER_BINDING (name)
5238 && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
5240 cxx_binding *binding;
5241 binding = IDENTIFIER_BINDING (name);
5242 while (1)
5244 if (binding->scope == b
5245 && !(VAR_P (binding->value)
5246 && DECL_DEAD_FOR_LOCAL (binding->value)))
5247 return binding->value;
5249 if (b->kind == sk_cleanup)
5250 b = b->level_chain;
5251 else
5252 break;
5256 return t;
5259 /* Wrapper for lookup_name_innermost_nonclass_level_1. */
5261 tree
5262 lookup_name_innermost_nonclass_level (tree name)
5264 tree ret;
5265 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5266 ret = lookup_name_innermost_nonclass_level_1 (name);
5267 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5268 return ret;
5272 /* Returns true iff DECL is a block-scope extern declaration of a function
5273 or variable. */
5275 bool
5276 is_local_extern (tree decl)
5278 cxx_binding *binding;
5280 /* For functions, this is easy. */
5281 if (TREE_CODE (decl) == FUNCTION_DECL)
5282 return DECL_LOCAL_FUNCTION_P (decl);
5284 if (!VAR_P (decl))
5285 return false;
5286 if (!current_function_decl)
5287 return false;
5289 /* For variables, this is not easy. We need to look at the binding stack
5290 for the identifier to see whether the decl we have is a local. */
5291 for (binding = IDENTIFIER_BINDING (DECL_NAME (decl));
5292 binding && binding->scope->kind != sk_namespace;
5293 binding = binding->previous)
5294 if (binding->value == decl)
5295 return LOCAL_BINDING_P (binding);
5297 return false;
5300 /* Like lookup_name_innermost_nonclass_level, but for types. */
5302 static tree
5303 lookup_type_current_level (tree name)
5305 tree t = NULL_TREE;
5307 timevar_start (TV_NAME_LOOKUP);
5308 gcc_assert (current_binding_level->kind != sk_namespace);
5310 if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
5311 && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
5313 cp_binding_level *b = current_binding_level;
5314 while (1)
5316 if (purpose_member (name, b->type_shadowed))
5318 t = REAL_IDENTIFIER_TYPE_VALUE (name);
5319 break;
5321 if (b->kind == sk_cleanup)
5322 b = b->level_chain;
5323 else
5324 break;
5328 timevar_stop (TV_NAME_LOOKUP);
5329 return t;
5332 /* [basic.lookup.koenig] */
5333 /* A nonzero return value in the functions below indicates an error. */
5335 struct arg_lookup
5337 tree name;
5338 vec<tree, va_gc> *args;
5339 vec<tree, va_gc> *namespaces;
5340 vec<tree, va_gc> *classes;
5341 tree functions;
5342 hash_set<tree> *fn_set;
5345 static bool arg_assoc (struct arg_lookup*, tree);
5346 static bool arg_assoc_args (struct arg_lookup*, tree);
5347 static bool arg_assoc_args_vec (struct arg_lookup*, vec<tree, va_gc> *);
5348 static bool arg_assoc_type (struct arg_lookup*, tree);
5349 static bool add_function (struct arg_lookup *, tree);
5350 static bool arg_assoc_namespace (struct arg_lookup *, tree);
5351 static bool arg_assoc_class_only (struct arg_lookup *, tree);
5352 static bool arg_assoc_bases (struct arg_lookup *, tree);
5353 static bool arg_assoc_class (struct arg_lookup *, tree);
5354 static bool arg_assoc_template_arg (struct arg_lookup*, tree);
5356 /* Add a function to the lookup structure.
5357 Returns true on error. */
5359 static bool
5360 add_function (struct arg_lookup *k, tree fn)
5362 if (!is_overloaded_fn (fn))
5363 /* All names except those of (possibly overloaded) functions and
5364 function templates are ignored. */;
5365 else if (k->fn_set && k->fn_set->add (fn))
5366 /* It's already in the list. */;
5367 else if (!k->functions)
5368 k->functions = fn;
5369 else if (fn == k->functions)
5371 else
5373 k->functions = build_overload (fn, k->functions);
5374 if (TREE_CODE (k->functions) == OVERLOAD)
5375 OVL_ARG_DEPENDENT (k->functions) = true;
5378 return false;
5381 /* Returns true iff CURRENT has declared itself to be an associated
5382 namespace of SCOPE via a strong using-directive (or transitive chain
5383 thereof). Both are namespaces. */
5385 bool
5386 is_associated_namespace (tree current, tree scope)
5388 vec<tree, va_gc> *seen = make_tree_vector ();
5389 vec<tree, va_gc> *todo = make_tree_vector ();
5390 tree t;
5391 bool ret;
5393 while (1)
5395 if (scope == current)
5397 ret = true;
5398 break;
5400 vec_safe_push (seen, scope);
5401 for (t = DECL_NAMESPACE_ASSOCIATIONS (scope); t; t = TREE_CHAIN (t))
5402 if (!vec_member (TREE_PURPOSE (t), seen))
5403 vec_safe_push (todo, TREE_PURPOSE (t));
5404 if (!todo->is_empty ())
5406 scope = todo->last ();
5407 todo->pop ();
5409 else
5411 ret = false;
5412 break;
5416 release_tree_vector (seen);
5417 release_tree_vector (todo);
5419 return ret;
5422 /* Add functions of a namespace to the lookup structure.
5423 Returns true on error. */
5425 static bool
5426 arg_assoc_namespace (struct arg_lookup *k, tree scope)
5428 tree value;
5430 if (vec_member (scope, k->namespaces))
5431 return false;
5432 vec_safe_push (k->namespaces, scope);
5434 /* Check out our super-users. */
5435 for (value = DECL_NAMESPACE_ASSOCIATIONS (scope); value;
5436 value = TREE_CHAIN (value))
5437 if (arg_assoc_namespace (k, TREE_PURPOSE (value)))
5438 return true;
5440 /* Also look down into inline namespaces. */
5441 for (value = DECL_NAMESPACE_USING (scope); value;
5442 value = TREE_CHAIN (value))
5443 if (is_associated_namespace (scope, TREE_PURPOSE (value)))
5444 if (arg_assoc_namespace (k, TREE_PURPOSE (value)))
5445 return true;
5447 value = namespace_binding (k->name, scope);
5448 if (!value)
5449 return false;
5451 for (; value; value = OVL_NEXT (value))
5453 /* We don't want to find arbitrary hidden functions via argument
5454 dependent lookup. We only want to find friends of associated
5455 classes, which we'll do via arg_assoc_class. */
5456 if (hidden_name_p (OVL_CURRENT (value)))
5457 continue;
5459 if (add_function (k, OVL_CURRENT (value)))
5460 return true;
5463 return false;
5466 /* Adds everything associated with a template argument to the lookup
5467 structure. Returns true on error. */
5469 static bool
5470 arg_assoc_template_arg (struct arg_lookup *k, tree arg)
5472 /* [basic.lookup.koenig]
5474 If T is a template-id, its associated namespaces and classes are
5475 ... the namespaces and classes associated with the types of the
5476 template arguments provided for template type parameters
5477 (excluding template template parameters); the namespaces in which
5478 any template template arguments are defined; and the classes in
5479 which any member templates used as template template arguments
5480 are defined. [Note: non-type template arguments do not
5481 contribute to the set of associated namespaces. ] */
5483 /* Consider first template template arguments. */
5484 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
5485 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
5486 return false;
5487 else if (TREE_CODE (arg) == TEMPLATE_DECL)
5489 tree ctx = CP_DECL_CONTEXT (arg);
5491 /* It's not a member template. */
5492 if (TREE_CODE (ctx) == NAMESPACE_DECL)
5493 return arg_assoc_namespace (k, ctx);
5494 /* Otherwise, it must be member template. */
5495 else
5496 return arg_assoc_class_only (k, ctx);
5498 /* It's an argument pack; handle it recursively. */
5499 else if (ARGUMENT_PACK_P (arg))
5501 tree args = ARGUMENT_PACK_ARGS (arg);
5502 int i, len = TREE_VEC_LENGTH (args);
5503 for (i = 0; i < len; ++i)
5504 if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, i)))
5505 return true;
5507 return false;
5509 /* It's not a template template argument, but it is a type template
5510 argument. */
5511 else if (TYPE_P (arg))
5512 return arg_assoc_type (k, arg);
5513 /* It's a non-type template argument. */
5514 else
5515 return false;
5518 /* Adds the class and its friends to the lookup structure.
5519 Returns true on error. */
5521 static bool
5522 arg_assoc_class_only (struct arg_lookup *k, tree type)
5524 tree list, friends, context;
5526 /* Backend-built structures, such as __builtin_va_list, aren't
5527 affected by all this. */
5528 if (!CLASS_TYPE_P (type))
5529 return false;
5531 context = decl_namespace_context (type);
5532 if (arg_assoc_namespace (k, context))
5533 return true;
5535 complete_type (type);
5537 /* Process friends. */
5538 for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
5539 list = TREE_CHAIN (list))
5540 if (k->name == FRIEND_NAME (list))
5541 for (friends = FRIEND_DECLS (list); friends;
5542 friends = TREE_CHAIN (friends))
5544 tree fn = TREE_VALUE (friends);
5546 /* Only interested in global functions with potentially hidden
5547 (i.e. unqualified) declarations. */
5548 if (CP_DECL_CONTEXT (fn) != context)
5549 continue;
5550 /* Template specializations are never found by name lookup.
5551 (Templates themselves can be found, but not template
5552 specializations.) */
5553 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn))
5554 continue;
5555 if (add_function (k, fn))
5556 return true;
5559 return false;
5562 /* Adds the class and its bases to the lookup structure.
5563 Returns true on error. */
5565 static bool
5566 arg_assoc_bases (struct arg_lookup *k, tree type)
5568 if (arg_assoc_class_only (k, type))
5569 return true;
5571 if (TYPE_BINFO (type))
5573 /* Process baseclasses. */
5574 tree binfo, base_binfo;
5575 int i;
5577 for (binfo = TYPE_BINFO (type), i = 0;
5578 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5579 if (arg_assoc_bases (k, BINFO_TYPE (base_binfo)))
5580 return true;
5583 return false;
5586 /* Adds everything associated with a class argument type to the lookup
5587 structure. Returns true on error.
5589 If T is a class type (including unions), its associated classes are: the
5590 class itself; the class of which it is a member, if any; and its direct
5591 and indirect base classes. Its associated namespaces are the namespaces
5592 of which its associated classes are members. Furthermore, if T is a
5593 class template specialization, its associated namespaces and classes
5594 also include: the namespaces and classes associated with the types of
5595 the template arguments provided for template type parameters (excluding
5596 template template parameters); the namespaces of which any template
5597 template arguments are members; and the classes of which any member
5598 templates used as template template arguments are members. [ Note:
5599 non-type template arguments do not contribute to the set of associated
5600 namespaces. --end note] */
5602 static bool
5603 arg_assoc_class (struct arg_lookup *k, tree type)
5605 tree list;
5606 int i;
5608 /* Backend build structures, such as __builtin_va_list, aren't
5609 affected by all this. */
5610 if (!CLASS_TYPE_P (type))
5611 return false;
5613 if (vec_member (type, k->classes))
5614 return false;
5615 vec_safe_push (k->classes, type);
5617 if (TYPE_CLASS_SCOPE_P (type)
5618 && arg_assoc_class_only (k, TYPE_CONTEXT (type)))
5619 return true;
5621 if (arg_assoc_bases (k, type))
5622 return true;
5624 /* Process template arguments. */
5625 if (CLASSTYPE_TEMPLATE_INFO (type)
5626 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
5628 list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
5629 for (i = 0; i < TREE_VEC_LENGTH (list); ++i)
5630 if (arg_assoc_template_arg (k, TREE_VEC_ELT (list, i)))
5631 return true;
5634 return false;
5637 /* Adds everything associated with a given type.
5638 Returns 1 on error. */
5640 static bool
5641 arg_assoc_type (struct arg_lookup *k, tree type)
5643 /* As we do not get the type of non-type dependent expressions
5644 right, we can end up with such things without a type. */
5645 if (!type)
5646 return false;
5648 if (TYPE_PTRDATAMEM_P (type))
5650 /* Pointer to member: associate class type and value type. */
5651 if (arg_assoc_type (k, TYPE_PTRMEM_CLASS_TYPE (type)))
5652 return true;
5653 return arg_assoc_type (k, TYPE_PTRMEM_POINTED_TO_TYPE (type));
5655 else switch (TREE_CODE (type))
5657 case ERROR_MARK:
5658 return false;
5659 case VOID_TYPE:
5660 case INTEGER_TYPE:
5661 case REAL_TYPE:
5662 case COMPLEX_TYPE:
5663 case VECTOR_TYPE:
5664 case BOOLEAN_TYPE:
5665 case FIXED_POINT_TYPE:
5666 case DECLTYPE_TYPE:
5667 case NULLPTR_TYPE:
5668 return false;
5669 case RECORD_TYPE:
5670 if (TYPE_PTRMEMFUNC_P (type))
5671 return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type));
5672 /* FALLTHRU */
5673 case UNION_TYPE:
5674 return arg_assoc_class (k, type);
5675 case POINTER_TYPE:
5676 case REFERENCE_TYPE:
5677 case ARRAY_TYPE:
5678 return arg_assoc_type (k, TREE_TYPE (type));
5679 case ENUMERAL_TYPE:
5680 if (TYPE_CLASS_SCOPE_P (type)
5681 && arg_assoc_class_only (k, TYPE_CONTEXT (type)))
5682 return true;
5683 return arg_assoc_namespace (k, decl_namespace_context (type));
5684 case METHOD_TYPE:
5685 /* The basetype is referenced in the first arg type, so just
5686 fall through. */
5687 case FUNCTION_TYPE:
5688 /* Associate the parameter types. */
5689 if (arg_assoc_args (k, TYPE_ARG_TYPES (type)))
5690 return true;
5691 /* Associate the return type. */
5692 return arg_assoc_type (k, TREE_TYPE (type));
5693 case TEMPLATE_TYPE_PARM:
5694 case BOUND_TEMPLATE_TEMPLATE_PARM:
5695 return false;
5696 case TYPENAME_TYPE:
5697 return false;
5698 case LANG_TYPE:
5699 gcc_assert (type == unknown_type_node
5700 || type == init_list_type_node);
5701 return false;
5702 case TYPE_PACK_EXPANSION:
5703 return arg_assoc_type (k, PACK_EXPANSION_PATTERN (type));
5705 default:
5706 gcc_unreachable ();
5708 return false;
5711 /* Adds everything associated with arguments. Returns true on error. */
5713 static bool
5714 arg_assoc_args (struct arg_lookup *k, tree args)
5716 for (; args; args = TREE_CHAIN (args))
5717 if (arg_assoc (k, TREE_VALUE (args)))
5718 return true;
5719 return false;
5722 /* Adds everything associated with an argument vector. Returns true
5723 on error. */
5725 static bool
5726 arg_assoc_args_vec (struct arg_lookup *k, vec<tree, va_gc> *args)
5728 unsigned int ix;
5729 tree arg;
5731 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
5732 if (arg_assoc (k, arg))
5733 return true;
5734 return false;
5737 /* Adds everything associated with a given tree_node. Returns 1 on error. */
5739 static bool
5740 arg_assoc (struct arg_lookup *k, tree n)
5742 if (n == error_mark_node)
5743 return false;
5745 if (TYPE_P (n))
5746 return arg_assoc_type (k, n);
5748 if (! type_unknown_p (n))
5749 return arg_assoc_type (k, TREE_TYPE (n));
5751 if (TREE_CODE (n) == ADDR_EXPR)
5752 n = TREE_OPERAND (n, 0);
5753 if (TREE_CODE (n) == COMPONENT_REF)
5754 n = TREE_OPERAND (n, 1);
5755 if (TREE_CODE (n) == OFFSET_REF)
5756 n = TREE_OPERAND (n, 1);
5757 while (TREE_CODE (n) == TREE_LIST)
5758 n = TREE_VALUE (n);
5759 if (BASELINK_P (n))
5760 n = BASELINK_FUNCTIONS (n);
5762 if (TREE_CODE (n) == FUNCTION_DECL)
5763 return arg_assoc_type (k, TREE_TYPE (n));
5764 if (TREE_CODE (n) == TEMPLATE_ID_EXPR)
5766 /* The working paper doesn't currently say how to handle template-id
5767 arguments. The sensible thing would seem to be to handle the list
5768 of template candidates like a normal overload set, and handle the
5769 template arguments like we do for class template
5770 specializations. */
5771 tree templ = TREE_OPERAND (n, 0);
5772 tree args = TREE_OPERAND (n, 1);
5773 int ix;
5775 /* First the templates. */
5776 if (arg_assoc (k, templ))
5777 return true;
5779 /* Now the arguments. */
5780 if (args)
5781 for (ix = TREE_VEC_LENGTH (args); ix--;)
5782 if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, ix)) == 1)
5783 return true;
5785 else if (TREE_CODE (n) == OVERLOAD)
5787 for (; n; n = OVL_NEXT (n))
5788 if (arg_assoc_type (k, TREE_TYPE (OVL_CURRENT (n))))
5789 return true;
5792 return false;
5795 /* Performs Koenig lookup depending on arguments, where fns
5796 are the functions found in normal lookup. */
5798 static cp_expr
5799 lookup_arg_dependent_1 (tree name, tree fns, vec<tree, va_gc> *args)
5801 struct arg_lookup k;
5803 /* Remove any hidden friend functions from the list of functions
5804 found so far. They will be added back by arg_assoc_class as
5805 appropriate. */
5806 fns = remove_hidden_names (fns);
5808 k.name = name;
5809 k.args = args;
5810 k.functions = fns;
5811 k.classes = make_tree_vector ();
5813 /* We previously performed an optimization here by setting
5814 NAMESPACES to the current namespace when it was safe. However, DR
5815 164 says that namespaces that were already searched in the first
5816 stage of template processing are searched again (potentially
5817 picking up later definitions) in the second stage. */
5818 k.namespaces = make_tree_vector ();
5820 /* We used to allow duplicates and let joust discard them, but
5821 since the above change for DR 164 we end up with duplicates of
5822 all the functions found by unqualified lookup. So keep track
5823 of which ones we've seen. */
5824 if (fns)
5826 tree ovl;
5827 /* We shouldn't be here if lookup found something other than
5828 namespace-scope functions. */
5829 gcc_assert (DECL_NAMESPACE_SCOPE_P (OVL_CURRENT (fns)));
5830 k.fn_set = new hash_set<tree>;
5831 for (ovl = fns; ovl; ovl = OVL_NEXT (ovl))
5832 k.fn_set->add (OVL_CURRENT (ovl));
5834 else
5835 k.fn_set = NULL;
5837 arg_assoc_args_vec (&k, args);
5839 fns = k.functions;
5841 if (fns
5842 && !VAR_P (fns)
5843 && !is_overloaded_fn (fns))
5845 error ("argument dependent lookup finds %q+D", fns);
5846 error (" in call to %qD", name);
5847 fns = error_mark_node;
5850 release_tree_vector (k.classes);
5851 release_tree_vector (k.namespaces);
5852 delete k.fn_set;
5854 return fns;
5857 /* Wrapper for lookup_arg_dependent_1. */
5859 cp_expr
5860 lookup_arg_dependent (tree name, tree fns, vec<tree, va_gc> *args)
5862 cp_expr ret;
5863 bool subtime;
5864 subtime = timevar_cond_start (TV_NAME_LOOKUP);
5865 ret = lookup_arg_dependent_1 (name, fns, args);
5866 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5867 return ret;
5871 /* Add namespace to using_directives. Return NULL_TREE if nothing was
5872 changed (i.e. there was already a directive), or the fresh
5873 TREE_LIST otherwise. */
5875 static tree
5876 push_using_directive_1 (tree used)
5878 tree ud = current_binding_level->using_directives;
5879 tree iter, ancestor;
5881 /* Check if we already have this. */
5882 if (purpose_member (used, ud) != NULL_TREE)
5883 return NULL_TREE;
5885 ancestor = namespace_ancestor (current_decl_namespace (), used);
5886 ud = current_binding_level->using_directives;
5887 ud = tree_cons (used, ancestor, ud);
5888 current_binding_level->using_directives = ud;
5890 /* Recursively add all namespaces used. */
5891 for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
5892 push_using_directive (TREE_PURPOSE (iter));
5894 return ud;
5897 /* Wrapper for push_using_directive_1. */
5899 static tree
5900 push_using_directive (tree used)
5902 tree ret;
5903 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5904 ret = push_using_directive_1 (used);
5905 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5906 return ret;
5909 /* The type TYPE is being declared. If it is a class template, or a
5910 specialization of a class template, do any processing required and
5911 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
5912 being declared a friend. B is the binding level at which this TYPE
5913 should be bound.
5915 Returns the TYPE_DECL for TYPE, which may have been altered by this
5916 processing. */
5918 static tree
5919 maybe_process_template_type_declaration (tree type, int is_friend,
5920 cp_binding_level *b)
5922 tree decl = TYPE_NAME (type);
5924 if (processing_template_parmlist)
5925 /* You can't declare a new template type in a template parameter
5926 list. But, you can declare a non-template type:
5928 template <class A*> struct S;
5930 is a forward-declaration of `A'. */
5932 else if (b->kind == sk_namespace
5933 && current_binding_level->kind != sk_namespace)
5934 /* If this new type is being injected into a containing scope,
5935 then it's not a template type. */
5937 else
5939 gcc_assert (MAYBE_CLASS_TYPE_P (type)
5940 || TREE_CODE (type) == ENUMERAL_TYPE);
5942 if (processing_template_decl)
5944 /* This may change after the call to
5945 push_template_decl_real, but we want the original value. */
5946 tree name = DECL_NAME (decl);
5948 decl = push_template_decl_real (decl, is_friend);
5949 if (decl == error_mark_node)
5950 return error_mark_node;
5952 /* If the current binding level is the binding level for the
5953 template parameters (see the comment in
5954 begin_template_parm_list) and the enclosing level is a class
5955 scope, and we're not looking at a friend, push the
5956 declaration of the member class into the class scope. In the
5957 friend case, push_template_decl will already have put the
5958 friend into global scope, if appropriate. */
5959 if (TREE_CODE (type) != ENUMERAL_TYPE
5960 && !is_friend && b->kind == sk_template_parms
5961 && b->level_chain->kind == sk_class)
5963 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
5965 if (!COMPLETE_TYPE_P (current_class_type))
5967 maybe_add_class_template_decl_list (current_class_type,
5968 type, /*friend_p=*/0);
5969 /* Put this UTD in the table of UTDs for the class. */
5970 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
5971 CLASSTYPE_NESTED_UTDS (current_class_type) =
5972 binding_table_new (SCOPE_DEFAULT_HT_SIZE);
5974 binding_table_insert
5975 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
5981 return decl;
5984 /* Push a tag name NAME for struct/class/union/enum type TYPE. In case
5985 that the NAME is a class template, the tag is processed but not pushed.
5987 The pushed scope depend on the SCOPE parameter:
5988 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
5989 scope.
5990 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
5991 non-template-parameter scope. This case is needed for forward
5992 declarations.
5993 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
5994 TS_GLOBAL case except that names within template-parameter scopes
5995 are not pushed at all.
5997 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
5999 static tree
6000 pushtag_1 (tree name, tree type, tag_scope scope)
6002 cp_binding_level *b;
6003 tree decl;
6005 b = current_binding_level;
6006 while (/* Cleanup scopes are not scopes from the point of view of
6007 the language. */
6008 b->kind == sk_cleanup
6009 /* Neither are function parameter scopes. */
6010 || b->kind == sk_function_parms
6011 /* Neither are the scopes used to hold template parameters
6012 for an explicit specialization. For an ordinary template
6013 declaration, these scopes are not scopes from the point of
6014 view of the language. */
6015 || (b->kind == sk_template_parms
6016 && (b->explicit_spec_p || scope == ts_global))
6017 || (b->kind == sk_class
6018 && (scope != ts_current
6019 /* We may be defining a new type in the initializer
6020 of a static member variable. We allow this when
6021 not pedantic, and it is particularly useful for
6022 type punning via an anonymous union. */
6023 || COMPLETE_TYPE_P (b->this_entity))))
6024 b = b->level_chain;
6026 gcc_assert (identifier_p (name));
6028 /* Do C++ gratuitous typedefing. */
6029 if (identifier_type_value_1 (name) != type)
6031 tree tdef;
6032 int in_class = 0;
6033 tree context = TYPE_CONTEXT (type);
6035 if (! context)
6037 tree cs = current_scope ();
6039 if (scope == ts_current
6040 || (cs && TREE_CODE (cs) == FUNCTION_DECL))
6041 context = cs;
6042 else if (cs != NULL_TREE && TYPE_P (cs))
6043 /* When declaring a friend class of a local class, we want
6044 to inject the newly named class into the scope
6045 containing the local class, not the namespace
6046 scope. */
6047 context = decl_function_context (get_type_decl (cs));
6049 if (!context)
6050 context = current_namespace;
6052 if (b->kind == sk_class
6053 || (b->kind == sk_template_parms
6054 && b->level_chain->kind == sk_class))
6055 in_class = 1;
6057 tdef = create_implicit_typedef (name, type);
6058 DECL_CONTEXT (tdef) = FROB_CONTEXT (context);
6059 if (scope == ts_within_enclosing_non_class)
6061 /* This is a friend. Make this TYPE_DECL node hidden from
6062 ordinary name lookup. Its corresponding TEMPLATE_DECL
6063 will be marked in push_template_decl_real. */
6064 retrofit_lang_decl (tdef);
6065 DECL_ANTICIPATED (tdef) = 1;
6066 DECL_FRIEND_P (tdef) = 1;
6069 decl = maybe_process_template_type_declaration
6070 (type, scope == ts_within_enclosing_non_class, b);
6071 if (decl == error_mark_node)
6072 return decl;
6074 if (b->kind == sk_class)
6076 if (!TYPE_BEING_DEFINED (current_class_type))
6077 return error_mark_node;
6079 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
6080 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
6081 class. But if it's a member template class, we want
6082 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
6083 later. */
6084 finish_member_declaration (decl);
6085 else
6086 pushdecl_class_level (decl);
6088 else if (b->kind != sk_template_parms)
6090 decl = pushdecl_with_scope_1 (decl, b, /*is_friend=*/false);
6091 if (decl == error_mark_node)
6092 return decl;
6095 if (! in_class)
6096 set_identifier_type_value_with_scope (name, tdef, b);
6098 TYPE_CONTEXT (type) = DECL_CONTEXT (decl);
6100 /* If this is a local class, keep track of it. We need this
6101 information for name-mangling, and so that it is possible to
6102 find all function definitions in a translation unit in a
6103 convenient way. (It's otherwise tricky to find a member
6104 function definition it's only pointed to from within a local
6105 class.) */
6106 if (TYPE_FUNCTION_SCOPE_P (type))
6108 if (processing_template_decl)
6110 /* Push a DECL_EXPR so we call pushtag at the right time in
6111 template instantiation rather than in some nested context. */
6112 add_decl_expr (decl);
6114 else
6115 vec_safe_push (local_classes, type);
6118 if (b->kind == sk_class
6119 && !COMPLETE_TYPE_P (current_class_type))
6121 maybe_add_class_template_decl_list (current_class_type,
6122 type, /*friend_p=*/0);
6124 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
6125 CLASSTYPE_NESTED_UTDS (current_class_type)
6126 = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
6128 binding_table_insert
6129 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
6132 decl = TYPE_NAME (type);
6133 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
6135 /* Set type visibility now if this is a forward declaration. */
6136 TREE_PUBLIC (decl) = 1;
6137 determine_visibility (decl);
6139 return type;
6142 /* Wrapper for pushtag_1. */
6144 tree
6145 pushtag (tree name, tree type, tag_scope scope)
6147 tree ret;
6148 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6149 ret = pushtag_1 (name, type, scope);
6150 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6151 return ret;
6154 /* Subroutines for reverting temporarily to top-level for instantiation
6155 of templates and such. We actually need to clear out the class- and
6156 local-value slots of all identifiers, so that only the global values
6157 are at all visible. Simply setting current_binding_level to the global
6158 scope isn't enough, because more binding levels may be pushed. */
6159 struct saved_scope *scope_chain;
6161 /* Return true if ID has not already been marked. */
6163 static inline bool
6164 store_binding_p (tree id)
6166 if (!id || !IDENTIFIER_BINDING (id))
6167 return false;
6169 if (IDENTIFIER_MARKED (id))
6170 return false;
6172 return true;
6175 /* Add an appropriate binding to *OLD_BINDINGS which needs to already
6176 have enough space reserved. */
6178 static void
6179 store_binding (tree id, vec<cxx_saved_binding, va_gc> **old_bindings)
6181 cxx_saved_binding saved;
6183 gcc_checking_assert (store_binding_p (id));
6185 IDENTIFIER_MARKED (id) = 1;
6187 saved.identifier = id;
6188 saved.binding = IDENTIFIER_BINDING (id);
6189 saved.real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
6190 (*old_bindings)->quick_push (saved);
6191 IDENTIFIER_BINDING (id) = NULL;
6194 static void
6195 store_bindings (tree names, vec<cxx_saved_binding, va_gc> **old_bindings)
6197 static vec<tree> bindings_need_stored;
6198 tree t, id;
6199 size_t i;
6201 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6202 for (t = names; t; t = TREE_CHAIN (t))
6204 if (TREE_CODE (t) == TREE_LIST)
6205 id = TREE_PURPOSE (t);
6206 else
6207 id = DECL_NAME (t);
6209 if (store_binding_p (id))
6210 bindings_need_stored.safe_push (id);
6212 if (!bindings_need_stored.is_empty ())
6214 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
6215 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
6217 /* We can appearantly have duplicates in NAMES. */
6218 if (store_binding_p (id))
6219 store_binding (id, old_bindings);
6221 bindings_need_stored.truncate (0);
6223 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6226 /* Like store_bindings, but NAMES is a vector of cp_class_binding
6227 objects, rather than a TREE_LIST. */
6229 static void
6230 store_class_bindings (vec<cp_class_binding, va_gc> *names,
6231 vec<cxx_saved_binding, va_gc> **old_bindings)
6233 static vec<tree> bindings_need_stored;
6234 size_t i;
6235 cp_class_binding *cb;
6237 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6238 for (i = 0; vec_safe_iterate (names, i, &cb); ++i)
6239 if (store_binding_p (cb->identifier))
6240 bindings_need_stored.safe_push (cb->identifier);
6241 if (!bindings_need_stored.is_empty ())
6243 tree id;
6244 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
6245 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
6246 store_binding (id, old_bindings);
6247 bindings_need_stored.truncate (0);
6249 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6252 /* A chain of saved_scope structures awaiting reuse. */
6254 static GTY((deletable)) struct saved_scope *free_saved_scope;
6256 void
6257 push_to_top_level (void)
6259 struct saved_scope *s;
6260 cp_binding_level *b;
6261 cxx_saved_binding *sb;
6262 size_t i;
6263 bool need_pop;
6265 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6267 /* Reuse or create a new structure for this saved scope. */
6268 if (free_saved_scope != NULL)
6270 s = free_saved_scope;
6271 free_saved_scope = s->prev;
6273 vec<cxx_saved_binding, va_gc> *old_bindings = s->old_bindings;
6274 memset (s, 0, sizeof (*s));
6275 /* Also reuse the structure's old_bindings vector. */
6276 vec_safe_truncate (old_bindings, 0);
6277 s->old_bindings = old_bindings;
6279 else
6280 s = ggc_cleared_alloc<saved_scope> ();
6282 b = scope_chain ? current_binding_level : 0;
6284 /* If we're in the middle of some function, save our state. */
6285 if (cfun)
6287 need_pop = true;
6288 push_function_context ();
6290 else
6291 need_pop = false;
6293 if (scope_chain && previous_class_level)
6294 store_class_bindings (previous_class_level->class_shadowed,
6295 &s->old_bindings);
6297 /* Have to include the global scope, because class-scope decls
6298 aren't listed anywhere useful. */
6299 for (; b; b = b->level_chain)
6301 tree t;
6303 /* Template IDs are inserted into the global level. If they were
6304 inserted into namespace level, finish_file wouldn't find them
6305 when doing pending instantiations. Therefore, don't stop at
6306 namespace level, but continue until :: . */
6307 if (global_scope_p (b))
6308 break;
6310 store_bindings (b->names, &s->old_bindings);
6311 /* We also need to check class_shadowed to save class-level type
6312 bindings, since pushclass doesn't fill in b->names. */
6313 if (b->kind == sk_class)
6314 store_class_bindings (b->class_shadowed, &s->old_bindings);
6316 /* Unwind type-value slots back to top level. */
6317 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
6318 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
6321 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, sb)
6322 IDENTIFIER_MARKED (sb->identifier) = 0;
6324 s->prev = scope_chain;
6325 s->bindings = b;
6326 s->need_pop_function_context = need_pop;
6327 s->function_decl = current_function_decl;
6328 s->unevaluated_operand = cp_unevaluated_operand;
6329 s->inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
6330 s->x_stmt_tree.stmts_are_full_exprs_p = true;
6332 scope_chain = s;
6333 current_function_decl = NULL_TREE;
6334 vec_alloc (current_lang_base, 10);
6335 current_lang_name = lang_name_cplusplus;
6336 current_namespace = global_namespace;
6337 push_class_stack ();
6338 cp_unevaluated_operand = 0;
6339 c_inhibit_evaluation_warnings = 0;
6340 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6343 static void
6344 pop_from_top_level_1 (void)
6346 struct saved_scope *s = scope_chain;
6347 cxx_saved_binding *saved;
6348 size_t i;
6350 /* Clear out class-level bindings cache. */
6351 if (previous_class_level)
6352 invalidate_class_lookup_cache ();
6353 pop_class_stack ();
6355 current_lang_base = 0;
6357 scope_chain = s->prev;
6358 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, saved)
6360 tree id = saved->identifier;
6362 IDENTIFIER_BINDING (id) = saved->binding;
6363 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
6366 /* If we were in the middle of compiling a function, restore our
6367 state. */
6368 if (s->need_pop_function_context)
6369 pop_function_context ();
6370 current_function_decl = s->function_decl;
6371 cp_unevaluated_operand = s->unevaluated_operand;
6372 c_inhibit_evaluation_warnings = s->inhibit_evaluation_warnings;
6374 /* Make this saved_scope structure available for reuse by
6375 push_to_top_level. */
6376 s->prev = free_saved_scope;
6377 free_saved_scope = s;
6380 /* Wrapper for pop_from_top_level_1. */
6382 void
6383 pop_from_top_level (void)
6385 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6386 pop_from_top_level_1 ();
6387 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6391 /* Pop off extraneous binding levels left over due to syntax errors.
6393 We don't pop past namespaces, as they might be valid. */
6395 void
6396 pop_everything (void)
6398 if (ENABLE_SCOPE_CHECKING)
6399 verbatim ("XXX entering pop_everything ()\n");
6400 while (!toplevel_bindings_p ())
6402 if (current_binding_level->kind == sk_class)
6403 pop_nested_class ();
6404 else
6405 poplevel (0, 0, 0);
6407 if (ENABLE_SCOPE_CHECKING)
6408 verbatim ("XXX leaving pop_everything ()\n");
6411 /* Emit debugging information for using declarations and directives.
6412 If input tree is overloaded fn then emit debug info for all
6413 candidates. */
6415 void
6416 cp_emit_debug_info_for_using (tree t, tree context)
6418 /* Don't try to emit any debug information if we have errors. */
6419 if (seen_error ())
6420 return;
6422 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
6423 of a builtin function. */
6424 if (TREE_CODE (t) == FUNCTION_DECL
6425 && DECL_EXTERNAL (t)
6426 && DECL_BUILT_IN (t))
6427 return;
6429 /* Do not supply context to imported_module_or_decl, if
6430 it is a global namespace. */
6431 if (context == global_namespace)
6432 context = NULL_TREE;
6434 if (BASELINK_P (t))
6435 t = BASELINK_FUNCTIONS (t);
6437 /* FIXME: Handle TEMPLATE_DECLs. */
6438 for (t = OVL_CURRENT (t); t; t = OVL_NEXT (t))
6439 if (TREE_CODE (t) != TEMPLATE_DECL)
6441 if (building_stmt_list_p ())
6442 add_stmt (build_stmt (input_location, USING_STMT, t));
6443 else
6444 (*debug_hooks->imported_module_or_decl) (t, NULL_TREE, context, false);
6448 #include "gt-cp-name-lookup.h"