2015-06-23 Paolo Carlini <paolo.carlini@oracle.com>
[official-gcc.git] / gcc / cp / name-lookup.c
blob1d7afea2acaaad2fdd60fe9fa91edc8ca2eed304
1 /* Definitions for C++ name lookup routines.
2 Copyright (C) 2003-2015 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 "tm.h"
25 #include "flags.h"
26 #include "alias.h"
27 #include "symtab.h"
28 #include "tree.h"
29 #include "stringpool.h"
30 #include "print-tree.h"
31 #include "attribs.h"
32 #include "cp-tree.h"
33 #include "name-lookup.h"
34 #include "timevar.h"
35 #include "diagnostic-core.h"
36 #include "intl.h"
37 #include "debug.h"
38 #include "c-family/c-pragma.h"
39 #include "params.h"
41 /* The bindings for a particular name in a particular scope. */
43 struct scope_binding {
44 tree value;
45 tree type;
47 #define EMPTY_SCOPE_BINDING { NULL_TREE, NULL_TREE }
49 static cp_binding_level *innermost_nonclass_level (void);
50 static cxx_binding *binding_for_name (cp_binding_level *, tree);
51 static tree push_overloaded_decl (tree, int, bool);
52 static bool lookup_using_namespace (tree, struct scope_binding *, tree,
53 tree, int);
54 static bool qualified_lookup_using_namespace (tree, tree,
55 struct scope_binding *, int);
56 static tree lookup_type_current_level (tree);
57 static tree push_using_directive (tree);
58 static tree lookup_extern_c_fun_in_all_ns (tree);
59 static void diagnose_name_conflict (tree, tree);
61 /* The :: namespace. */
63 tree global_namespace;
65 /* The name of the anonymous namespace, throughout this translation
66 unit. */
67 static GTY(()) tree anonymous_namespace_name;
69 /* Initialize anonymous_namespace_name if necessary, and return it. */
71 static tree
72 get_anonymous_namespace_name (void)
74 if (!anonymous_namespace_name)
76 /* We used to use get_file_function_name here, but that isn't
77 necessary now that anonymous namespace typeinfos
78 are !TREE_PUBLIC, and thus compared by address. */
79 /* The demangler expects anonymous namespaces to be called
80 something starting with '_GLOBAL__N_'. */
81 anonymous_namespace_name = get_identifier ("_GLOBAL__N_1");
83 return anonymous_namespace_name;
86 /* Compute the chain index of a binding_entry given the HASH value of its
87 name and the total COUNT of chains. COUNT is assumed to be a power
88 of 2. */
90 #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
92 /* A free list of "binding_entry"s awaiting for re-use. */
94 static GTY((deletable)) binding_entry free_binding_entry = NULL;
96 /* Create a binding_entry object for (NAME, TYPE). */
98 static inline binding_entry
99 binding_entry_make (tree name, tree type)
101 binding_entry entry;
103 if (free_binding_entry)
105 entry = free_binding_entry;
106 free_binding_entry = entry->chain;
108 else
109 entry = ggc_alloc<binding_entry_s> ();
111 entry->name = name;
112 entry->type = type;
113 entry->chain = NULL;
115 return entry;
118 /* Put ENTRY back on the free list. */
119 #if 0
120 static inline void
121 binding_entry_free (binding_entry entry)
123 entry->name = NULL;
124 entry->type = NULL;
125 entry->chain = free_binding_entry;
126 free_binding_entry = entry;
128 #endif
130 /* The datatype used to implement the mapping from names to types at
131 a given scope. */
132 struct GTY(()) binding_table_s {
133 /* Array of chains of "binding_entry"s */
134 binding_entry * GTY((length ("%h.chain_count"))) chain;
136 /* The number of chains in this table. This is the length of the
137 member "chain" considered as an array. */
138 size_t chain_count;
140 /* Number of "binding_entry"s in this table. */
141 size_t entry_count;
144 /* Construct TABLE with an initial CHAIN_COUNT. */
146 static inline void
147 binding_table_construct (binding_table table, size_t chain_count)
149 table->chain_count = chain_count;
150 table->entry_count = 0;
151 table->chain = ggc_cleared_vec_alloc<binding_entry> (table->chain_count);
154 /* Make TABLE's entries ready for reuse. */
155 #if 0
156 static void
157 binding_table_free (binding_table table)
159 size_t i;
160 size_t count;
162 if (table == NULL)
163 return;
165 for (i = 0, count = table->chain_count; i < count; ++i)
167 binding_entry temp = table->chain[i];
168 while (temp != NULL)
170 binding_entry entry = temp;
171 temp = entry->chain;
172 binding_entry_free (entry);
174 table->chain[i] = NULL;
176 table->entry_count = 0;
178 #endif
180 /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */
182 static inline binding_table
183 binding_table_new (size_t chain_count)
185 binding_table table = ggc_alloc<binding_table_s> ();
186 table->chain = NULL;
187 binding_table_construct (table, chain_count);
188 return table;
191 /* Expand TABLE to twice its current chain_count. */
193 static void
194 binding_table_expand (binding_table table)
196 const size_t old_chain_count = table->chain_count;
197 const size_t old_entry_count = table->entry_count;
198 const size_t new_chain_count = 2 * old_chain_count;
199 binding_entry *old_chains = table->chain;
200 size_t i;
202 binding_table_construct (table, new_chain_count);
203 for (i = 0; i < old_chain_count; ++i)
205 binding_entry entry = old_chains[i];
206 for (; entry != NULL; entry = old_chains[i])
208 const unsigned int hash = IDENTIFIER_HASH_VALUE (entry->name);
209 const size_t j = ENTRY_INDEX (hash, new_chain_count);
211 old_chains[i] = entry->chain;
212 entry->chain = table->chain[j];
213 table->chain[j] = entry;
216 table->entry_count = old_entry_count;
219 /* Insert a binding for NAME to TYPE into TABLE. */
221 static void
222 binding_table_insert (binding_table table, tree name, tree type)
224 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
225 const size_t i = ENTRY_INDEX (hash, table->chain_count);
226 binding_entry entry = binding_entry_make (name, type);
228 entry->chain = table->chain[i];
229 table->chain[i] = entry;
230 ++table->entry_count;
232 if (3 * table->chain_count < 5 * table->entry_count)
233 binding_table_expand (table);
236 /* Return the binding_entry, if any, that maps NAME. */
238 binding_entry
239 binding_table_find (binding_table table, tree name)
241 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
242 binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)];
244 while (entry != NULL && entry->name != name)
245 entry = entry->chain;
247 return entry;
250 /* Apply PROC -- with DATA -- to all entries in TABLE. */
252 void
253 binding_table_foreach (binding_table table, bt_foreach_proc proc, void *data)
255 size_t chain_count;
256 size_t i;
258 if (!table)
259 return;
261 chain_count = table->chain_count;
262 for (i = 0; i < chain_count; ++i)
264 binding_entry entry = table->chain[i];
265 for (; entry != NULL; entry = entry->chain)
266 proc (entry, data);
270 #ifndef ENABLE_SCOPE_CHECKING
271 # define ENABLE_SCOPE_CHECKING 0
272 #else
273 # define ENABLE_SCOPE_CHECKING 1
274 #endif
276 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
278 static GTY((deletable)) cxx_binding *free_bindings;
280 /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
281 field to NULL. */
283 static inline void
284 cxx_binding_init (cxx_binding *binding, tree value, tree type)
286 binding->value = value;
287 binding->type = type;
288 binding->previous = NULL;
291 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
293 static cxx_binding *
294 cxx_binding_make (tree value, tree type)
296 cxx_binding *binding;
297 if (free_bindings)
299 binding = free_bindings;
300 free_bindings = binding->previous;
302 else
303 binding = ggc_alloc<cxx_binding> ();
305 cxx_binding_init (binding, value, type);
307 return binding;
310 /* Put BINDING back on the free list. */
312 static inline void
313 cxx_binding_free (cxx_binding *binding)
315 binding->scope = NULL;
316 binding->previous = free_bindings;
317 free_bindings = binding;
320 /* Create a new binding for NAME (with the indicated VALUE and TYPE
321 bindings) in the class scope indicated by SCOPE. */
323 static cxx_binding *
324 new_class_binding (tree name, tree value, tree type, cp_binding_level *scope)
326 cp_class_binding cb = {cxx_binding_make (value, type), name};
327 cxx_binding *binding = cb.base;
328 vec_safe_push (scope->class_shadowed, cb);
329 binding->scope = scope;
330 return binding;
333 /* Make DECL the innermost binding for ID. The LEVEL is the binding
334 level at which this declaration is being bound. */
336 static void
337 push_binding (tree id, tree decl, cp_binding_level* level)
339 cxx_binding *binding;
341 if (level != class_binding_level)
343 binding = cxx_binding_make (decl, NULL_TREE);
344 binding->scope = level;
346 else
347 binding = new_class_binding (id, decl, /*type=*/NULL_TREE, level);
349 /* Now, fill in the binding information. */
350 binding->previous = IDENTIFIER_BINDING (id);
351 INHERITED_VALUE_BINDING_P (binding) = 0;
352 LOCAL_BINDING_P (binding) = (level != class_binding_level);
354 /* And put it on the front of the list of bindings for ID. */
355 IDENTIFIER_BINDING (id) = binding;
358 /* Remove the binding for DECL which should be the innermost binding
359 for ID. */
361 void
362 pop_binding (tree id, tree decl)
364 cxx_binding *binding;
366 if (id == NULL_TREE)
367 /* It's easiest to write the loops that call this function without
368 checking whether or not the entities involved have names. We
369 get here for such an entity. */
370 return;
372 /* Get the innermost binding for ID. */
373 binding = IDENTIFIER_BINDING (id);
375 /* The name should be bound. */
376 gcc_assert (binding != NULL);
378 /* The DECL will be either the ordinary binding or the type
379 binding for this identifier. Remove that binding. */
380 if (binding->value == decl)
381 binding->value = NULL_TREE;
382 else
384 gcc_assert (binding->type == decl);
385 binding->type = NULL_TREE;
388 if (!binding->value && !binding->type)
390 /* We're completely done with the innermost binding for this
391 identifier. Unhook it from the list of bindings. */
392 IDENTIFIER_BINDING (id) = binding->previous;
394 /* Add it to the free list. */
395 cxx_binding_free (binding);
399 /* Remove the bindings for the decls of the current level and leave
400 the current scope. */
402 void
403 pop_bindings_and_leave_scope (void)
405 for (tree t = getdecls (); t; t = DECL_CHAIN (t))
406 pop_binding (DECL_NAME (t), t);
407 leave_scope ();
410 /* Strip non dependent using declarations. If DECL is dependent,
411 surreptitiously create a typename_type and return it. */
413 tree
414 strip_using_decl (tree decl)
416 if (decl == NULL_TREE)
417 return NULL_TREE;
419 while (TREE_CODE (decl) == USING_DECL && !DECL_DEPENDENT_P (decl))
420 decl = USING_DECL_DECLS (decl);
422 if (TREE_CODE (decl) == USING_DECL && DECL_DEPENDENT_P (decl)
423 && USING_DECL_TYPENAME_P (decl))
425 /* We have found a type introduced by a using
426 declaration at class scope that refers to a dependent
427 type.
429 using typename :: [opt] nested-name-specifier unqualified-id ;
431 decl = make_typename_type (TREE_TYPE (decl),
432 DECL_NAME (decl),
433 typename_type, tf_error);
434 if (decl != error_mark_node)
435 decl = TYPE_NAME (decl);
438 return decl;
441 /* BINDING records an existing declaration for a name in the current scope.
442 But, DECL is another declaration for that same identifier in the
443 same scope. This is the `struct stat' hack whereby a non-typedef
444 class name or enum-name can be bound at the same level as some other
445 kind of entity.
446 3.3.7/1
448 A class name (9.1) or enumeration name (7.2) can be hidden by the
449 name of an object, function, or enumerator declared in the same scope.
450 If a class or enumeration name and an object, function, or enumerator
451 are declared in the same scope (in any order) with the same name, the
452 class or enumeration name is hidden wherever the object, function, or
453 enumerator name is visible.
455 It's the responsibility of the caller to check that
456 inserting this name is valid here. Returns nonzero if the new binding
457 was successful. */
459 static bool
460 supplement_binding_1 (cxx_binding *binding, tree decl)
462 tree bval = binding->value;
463 bool ok = true;
464 tree target_bval = strip_using_decl (bval);
465 tree target_decl = strip_using_decl (decl);
467 if (TREE_CODE (target_decl) == TYPE_DECL && DECL_ARTIFICIAL (target_decl)
468 && target_decl != target_bval
469 && (TREE_CODE (target_bval) != TYPE_DECL
470 /* We allow pushing an enum multiple times in a class
471 template in order to handle late matching of underlying
472 type on an opaque-enum-declaration followed by an
473 enum-specifier. */
474 || (processing_template_decl
475 && TREE_CODE (TREE_TYPE (target_decl)) == ENUMERAL_TYPE
476 && TREE_CODE (TREE_TYPE (target_bval)) == ENUMERAL_TYPE
477 && (dependent_type_p (ENUM_UNDERLYING_TYPE
478 (TREE_TYPE (target_decl)))
479 || dependent_type_p (ENUM_UNDERLYING_TYPE
480 (TREE_TYPE (target_bval)))))))
481 /* The new name is the type name. */
482 binding->type = decl;
483 else if (/* TARGET_BVAL is null when push_class_level_binding moves
484 an inherited type-binding out of the way to make room
485 for a new value binding. */
486 !target_bval
487 /* TARGET_BVAL is error_mark_node when TARGET_DECL's name
488 has been used in a non-class scope prior declaration.
489 In that case, we should have already issued a
490 diagnostic; for graceful error recovery purpose, pretend
491 this was the intended declaration for that name. */
492 || target_bval == error_mark_node
493 /* If TARGET_BVAL is anticipated but has not yet been
494 declared, pretend it is not there at all. */
495 || (TREE_CODE (target_bval) == FUNCTION_DECL
496 && DECL_ANTICIPATED (target_bval)
497 && !DECL_HIDDEN_FRIEND_P (target_bval)))
498 binding->value = decl;
499 else if (TREE_CODE (target_bval) == TYPE_DECL
500 && DECL_ARTIFICIAL (target_bval)
501 && target_decl != target_bval
502 && (TREE_CODE (target_decl) != TYPE_DECL
503 || same_type_p (TREE_TYPE (target_decl),
504 TREE_TYPE (target_bval))))
506 /* The old binding was a type name. It was placed in
507 VALUE field because it was thought, at the point it was
508 declared, to be the only entity with such a name. Move the
509 type name into the type slot; it is now hidden by the new
510 binding. */
511 binding->type = bval;
512 binding->value = decl;
513 binding->value_is_inherited = false;
515 else if (TREE_CODE (target_bval) == TYPE_DECL
516 && TREE_CODE (target_decl) == TYPE_DECL
517 && DECL_NAME (target_decl) == DECL_NAME (target_bval)
518 && binding->scope->kind != sk_class
519 && (same_type_p (TREE_TYPE (target_decl), TREE_TYPE (target_bval))
520 /* If either type involves template parameters, we must
521 wait until instantiation. */
522 || uses_template_parms (TREE_TYPE (target_decl))
523 || uses_template_parms (TREE_TYPE (target_bval))))
524 /* We have two typedef-names, both naming the same type to have
525 the same name. In general, this is OK because of:
527 [dcl.typedef]
529 In a given scope, a typedef specifier can be used to redefine
530 the name of any type declared in that scope to refer to the
531 type to which it already refers.
533 However, in class scopes, this rule does not apply due to the
534 stricter language in [class.mem] prohibiting redeclarations of
535 members. */
536 ok = false;
537 /* There can be two block-scope declarations of the same variable,
538 so long as they are `extern' declarations. However, there cannot
539 be two declarations of the same static data member:
541 [class.mem]
543 A member shall not be declared twice in the
544 member-specification. */
545 else if (VAR_P (target_decl)
546 && VAR_P (target_bval)
547 && DECL_EXTERNAL (target_decl) && DECL_EXTERNAL (target_bval)
548 && !DECL_CLASS_SCOPE_P (target_decl))
550 duplicate_decls (decl, binding->value, /*newdecl_is_friend=*/false);
551 ok = false;
553 else if (TREE_CODE (decl) == NAMESPACE_DECL
554 && TREE_CODE (bval) == NAMESPACE_DECL
555 && DECL_NAMESPACE_ALIAS (decl)
556 && DECL_NAMESPACE_ALIAS (bval)
557 && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl))
558 /* [namespace.alias]
560 In a declarative region, a namespace-alias-definition can be
561 used to redefine a namespace-alias declared in that declarative
562 region to refer only to the namespace to which it already
563 refers. */
564 ok = false;
565 else if (maybe_remove_implicit_alias (bval))
567 /* There was a mangling compatibility alias using this mangled name,
568 but now we have a real decl that wants to use it instead. */
569 binding->value = decl;
571 else
573 diagnose_name_conflict (decl, bval);
574 ok = false;
577 return ok;
580 /* Diagnose a name conflict between DECL and BVAL. */
582 static void
583 diagnose_name_conflict (tree decl, tree bval)
585 if (TREE_CODE (decl) == TREE_CODE (bval)
586 && (TREE_CODE (decl) != TYPE_DECL
587 || (DECL_ARTIFICIAL (decl) && DECL_ARTIFICIAL (bval))
588 || (!DECL_ARTIFICIAL (decl) && !DECL_ARTIFICIAL (bval)))
589 && !is_overloaded_fn (decl))
590 error ("redeclaration of %q#D", decl);
591 else
592 error ("%q#D conflicts with a previous declaration", decl);
594 inform (input_location, "previous declaration %q+#D", bval);
597 /* Wrapper for supplement_binding_1. */
599 static bool
600 supplement_binding (cxx_binding *binding, tree decl)
602 bool ret;
603 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
604 ret = supplement_binding_1 (binding, decl);
605 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
606 return ret;
609 /* Add DECL to the list of things declared in B. */
611 static void
612 add_decl_to_level (tree decl, cp_binding_level *b)
614 /* We used to record virtual tables as if they were ordinary
615 variables, but no longer do so. */
616 gcc_assert (!(VAR_P (decl) && DECL_VIRTUAL_P (decl)));
618 if (TREE_CODE (decl) == NAMESPACE_DECL
619 && !DECL_NAMESPACE_ALIAS (decl))
621 DECL_CHAIN (decl) = b->namespaces;
622 b->namespaces = decl;
624 else
626 /* We build up the list in reverse order, and reverse it later if
627 necessary. */
628 TREE_CHAIN (decl) = b->names;
629 b->names = decl;
631 /* If appropriate, add decl to separate list of statics. We
632 include extern variables because they might turn out to be
633 static later. It's OK for this list to contain a few false
634 positives. */
635 if (b->kind == sk_namespace)
636 if ((VAR_P (decl)
637 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
638 || (TREE_CODE (decl) == FUNCTION_DECL
639 && (!TREE_PUBLIC (decl)
640 || decl_anon_ns_mem_p (decl)
641 || DECL_DECLARED_INLINE_P (decl))))
642 vec_safe_push (b->static_decls, decl);
646 /* Record a decl-node X as belonging to the current lexical scope.
647 Check for errors (such as an incompatible declaration for the same
648 name already seen in the same scope). IS_FRIEND is true if X is
649 declared as a friend.
651 Returns either X or an old decl for the same name.
652 If an old decl is returned, it may have been smashed
653 to agree with what X says. */
655 static tree
656 pushdecl_maybe_friend_1 (tree x, bool is_friend)
658 tree t;
659 tree name;
660 int need_new_binding;
662 if (x == error_mark_node)
663 return error_mark_node;
665 need_new_binding = 1;
667 if (DECL_TEMPLATE_PARM_P (x))
668 /* Template parameters have no context; they are not X::T even
669 when declared within a class or namespace. */
671 else
673 if (current_function_decl && x != current_function_decl
674 /* A local declaration for a function doesn't constitute
675 nesting. */
676 && TREE_CODE (x) != FUNCTION_DECL
677 /* A local declaration for an `extern' variable is in the
678 scope of the current namespace, not the current
679 function. */
680 && !(VAR_P (x) && DECL_EXTERNAL (x))
681 /* When parsing the parameter list of a function declarator,
682 don't set DECL_CONTEXT to an enclosing function. When we
683 push the PARM_DECLs in order to process the function body,
684 current_binding_level->this_entity will be set. */
685 && !(TREE_CODE (x) == PARM_DECL
686 && current_binding_level->kind == sk_function_parms
687 && current_binding_level->this_entity == NULL)
688 && !DECL_CONTEXT (x))
689 DECL_CONTEXT (x) = current_function_decl;
691 /* If this is the declaration for a namespace-scope function,
692 but the declaration itself is in a local scope, mark the
693 declaration. */
694 if (TREE_CODE (x) == FUNCTION_DECL
695 && DECL_NAMESPACE_SCOPE_P (x)
696 && current_function_decl
697 && x != current_function_decl)
698 DECL_LOCAL_FUNCTION_P (x) = 1;
701 name = DECL_NAME (x);
702 if (name)
704 int different_binding_level = 0;
706 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
707 name = TREE_OPERAND (name, 0);
709 /* In case this decl was explicitly namespace-qualified, look it
710 up in its namespace context. */
711 if (DECL_NAMESPACE_SCOPE_P (x) && namespace_bindings_p ())
712 t = namespace_binding (name, DECL_CONTEXT (x));
713 else
714 t = lookup_name_innermost_nonclass_level (name);
716 /* [basic.link] If there is a visible declaration of an entity
717 with linkage having the same name and type, ignoring entities
718 declared outside the innermost enclosing namespace scope, the
719 block scope declaration declares that same entity and
720 receives the linkage of the previous declaration. */
721 if (! t && current_function_decl && x != current_function_decl
722 && VAR_OR_FUNCTION_DECL_P (x)
723 && DECL_EXTERNAL (x))
725 /* Look in block scope. */
726 t = innermost_non_namespace_value (name);
727 /* Or in the innermost namespace. */
728 if (! t)
729 t = namespace_binding (name, DECL_CONTEXT (x));
730 /* Does it have linkage? Note that if this isn't a DECL, it's an
731 OVERLOAD, which is OK. */
732 if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t)))
733 t = NULL_TREE;
734 if (t)
735 different_binding_level = 1;
738 /* If we are declaring a function, and the result of name-lookup
739 was an OVERLOAD, look for an overloaded instance that is
740 actually the same as the function we are declaring. (If
741 there is one, we have to merge our declaration with the
742 previous declaration.) */
743 if (t && TREE_CODE (t) == OVERLOAD)
745 tree match;
747 if (TREE_CODE (x) == FUNCTION_DECL)
748 for (match = t; match; match = OVL_NEXT (match))
750 if (decls_match (OVL_CURRENT (match), x))
751 break;
753 else
754 /* Just choose one. */
755 match = t;
757 if (match)
758 t = OVL_CURRENT (match);
759 else
760 t = NULL_TREE;
763 if (t && t != error_mark_node)
765 if (different_binding_level)
767 if (decls_match (x, t))
768 /* The standard only says that the local extern
769 inherits linkage from the previous decl; in
770 particular, default args are not shared. Add
771 the decl into a hash table to make sure only
772 the previous decl in this case is seen by the
773 middle end. */
775 struct cxx_int_tree_map *h;
777 TREE_PUBLIC (x) = TREE_PUBLIC (t);
779 if (cp_function_chain->extern_decl_map == NULL)
780 cp_function_chain->extern_decl_map
781 = hash_table<cxx_int_tree_map_hasher>::create_ggc (20);
783 h = ggc_alloc<cxx_int_tree_map> ();
784 h->uid = DECL_UID (x);
785 h->to = t;
786 cxx_int_tree_map **loc = cp_function_chain->extern_decl_map
787 ->find_slot (h, INSERT);
788 *loc = h;
791 else if (TREE_CODE (t) == PARM_DECL)
793 /* Check for duplicate params. */
794 tree d = duplicate_decls (x, t, is_friend);
795 if (d)
796 return d;
798 else if ((DECL_EXTERN_C_FUNCTION_P (x)
799 || DECL_FUNCTION_TEMPLATE_P (x))
800 && is_overloaded_fn (t))
801 /* Don't do anything just yet. */;
802 else if (t == wchar_decl_node)
804 if (! DECL_IN_SYSTEM_HEADER (x))
805 pedwarn (input_location, OPT_Wpedantic, "redeclaration of %<wchar_t%> as %qT",
806 TREE_TYPE (x));
808 /* Throw away the redeclaration. */
809 return t;
811 else
813 tree olddecl = duplicate_decls (x, t, is_friend);
815 /* If the redeclaration failed, we can stop at this
816 point. */
817 if (olddecl == error_mark_node)
818 return error_mark_node;
820 if (olddecl)
822 if (TREE_CODE (t) == TYPE_DECL)
823 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
825 return t;
827 else if (DECL_MAIN_P (x) && TREE_CODE (t) == FUNCTION_DECL)
829 /* A redeclaration of main, but not a duplicate of the
830 previous one.
832 [basic.start.main]
834 This function shall not be overloaded. */
835 error ("invalid redeclaration of %q+D", t);
836 error ("as %qD", x);
837 /* We don't try to push this declaration since that
838 causes a crash. */
839 return x;
844 /* If x has C linkage-specification, (extern "C"),
845 lookup its binding, in case it's already bound to an object.
846 The lookup is done in all namespaces.
847 If we find an existing binding, make sure it has the same
848 exception specification as x, otherwise, bail in error [7.5, 7.6]. */
849 if ((TREE_CODE (x) == FUNCTION_DECL)
850 && DECL_EXTERN_C_P (x)
851 /* We should ignore declarations happening in system headers. */
852 && !DECL_ARTIFICIAL (x)
853 && !DECL_IN_SYSTEM_HEADER (x))
855 tree previous = lookup_extern_c_fun_in_all_ns (x);
856 if (previous
857 && !DECL_ARTIFICIAL (previous)
858 && !DECL_IN_SYSTEM_HEADER (previous)
859 && DECL_CONTEXT (previous) != DECL_CONTEXT (x))
861 /* In case either x or previous is declared to throw an exception,
862 make sure both exception specifications are equal. */
863 if (decls_match (x, previous))
865 tree x_exception_spec = NULL_TREE;
866 tree previous_exception_spec = NULL_TREE;
868 x_exception_spec =
869 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (x));
870 previous_exception_spec =
871 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (previous));
872 if (!comp_except_specs (previous_exception_spec,
873 x_exception_spec,
874 ce_normal))
876 pedwarn (input_location, 0,
877 "declaration of %q#D with C language linkage",
879 pedwarn (input_location, 0,
880 "conflicts with previous declaration %q+#D",
881 previous);
882 pedwarn (input_location, 0,
883 "due to different exception specifications");
884 return error_mark_node;
886 if (DECL_ASSEMBLER_NAME_SET_P (previous))
887 SET_DECL_ASSEMBLER_NAME (x,
888 DECL_ASSEMBLER_NAME (previous));
890 else
892 pedwarn (input_location, 0,
893 "declaration of %q#D with C language linkage", x);
894 pedwarn (input_location, 0,
895 "conflicts with previous declaration %q+#D",
896 previous);
901 check_template_shadow (x);
903 /* If this is a function conjured up by the back end, massage it
904 so it looks friendly. */
905 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x))
907 retrofit_lang_decl (x);
908 SET_DECL_LANGUAGE (x, lang_c);
911 t = x;
912 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x))
914 t = push_overloaded_decl (x, PUSH_LOCAL, is_friend);
915 if (!namespace_bindings_p ())
916 /* We do not need to create a binding for this name;
917 push_overloaded_decl will have already done so if
918 necessary. */
919 need_new_binding = 0;
921 else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
923 t = push_overloaded_decl (x, PUSH_GLOBAL, is_friend);
924 if (t == x)
925 add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
928 if (DECL_DECLARES_FUNCTION_P (t))
930 check_default_args (t);
932 if (is_friend && t == x && !flag_friend_injection)
934 /* This is a new friend declaration of a function or a
935 function template, so hide it from ordinary function
936 lookup. */
937 DECL_ANTICIPATED (t) = 1;
938 DECL_HIDDEN_FRIEND_P (t) = 1;
942 if (t != x || DECL_FUNCTION_TEMPLATE_P (t))
943 return t;
945 /* If declaring a type as a typedef, copy the type (unless we're
946 at line 0), and install this TYPE_DECL as the new type's typedef
947 name. See the extensive comment of set_underlying_type (). */
948 if (TREE_CODE (x) == TYPE_DECL)
950 tree type = TREE_TYPE (x);
952 if (DECL_IS_BUILTIN (x)
953 || (TREE_TYPE (x) != error_mark_node
954 && TYPE_NAME (type) != x
955 /* We don't want to copy the type when all we're
956 doing is making a TYPE_DECL for the purposes of
957 inlining. */
958 && (!TYPE_NAME (type)
959 || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x))))
960 set_underlying_type (x);
962 if (type != error_mark_node
963 && TYPE_IDENTIFIER (type))
964 set_identifier_type_value (DECL_NAME (x), x);
966 /* If this is a locally defined typedef in a function that
967 is not a template instantation, record it to implement
968 -Wunused-local-typedefs. */
969 if (!instantiating_current_function_p ())
970 record_locally_defined_typedef (x);
973 /* Multiple external decls of the same identifier ought to match.
975 We get warnings about inline functions where they are defined.
976 We get warnings about other functions from push_overloaded_decl.
978 Avoid duplicate warnings where they are used. */
979 if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
981 tree decl;
983 decl = IDENTIFIER_NAMESPACE_VALUE (name);
984 if (decl && TREE_CODE (decl) == OVERLOAD)
985 decl = OVL_FUNCTION (decl);
987 if (decl && decl != error_mark_node
988 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl))
989 /* If different sort of thing, we already gave an error. */
990 && TREE_CODE (decl) == TREE_CODE (x)
991 && !comptypes (TREE_TYPE (x), TREE_TYPE (decl),
992 COMPARE_REDECLARATION))
994 if (permerror (input_location, "type mismatch with previous "
995 "external decl of %q#D", x))
996 inform (input_location, "previous external decl of %q+#D",
997 decl);
1001 /* This name is new in its binding level.
1002 Install the new declaration and return it. */
1003 if (namespace_bindings_p ())
1005 /* Install a global value. */
1007 /* If the first global decl has external linkage,
1008 warn if we later see static one. */
1009 if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
1010 TREE_PUBLIC (name) = 1;
1012 /* Bind the name for the entity. */
1013 if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
1014 && t != NULL_TREE)
1015 && (TREE_CODE (x) == TYPE_DECL
1016 || VAR_P (x)
1017 || TREE_CODE (x) == NAMESPACE_DECL
1018 || TREE_CODE (x) == CONST_DECL
1019 || TREE_CODE (x) == TEMPLATE_DECL))
1020 SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
1022 /* If new decl is `static' and an `extern' was seen previously,
1023 warn about it. */
1024 if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
1025 warn_extern_redeclared_static (x, t);
1027 else
1029 /* Here to install a non-global value. */
1030 tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
1031 tree oldlocal = NULL_TREE;
1032 cp_binding_level *oldscope = NULL;
1033 cxx_binding *oldbinding = outer_binding (name, NULL, true);
1034 if (oldbinding)
1036 oldlocal = oldbinding->value;
1037 oldscope = oldbinding->scope;
1040 if (need_new_binding)
1042 push_local_binding (name, x, 0);
1043 /* Because push_local_binding will hook X on to the
1044 current_binding_level's name list, we don't want to
1045 do that again below. */
1046 need_new_binding = 0;
1049 /* If this is a TYPE_DECL, push it into the type value slot. */
1050 if (TREE_CODE (x) == TYPE_DECL)
1051 set_identifier_type_value (name, x);
1053 /* Clear out any TYPE_DECL shadowed by a namespace so that
1054 we won't think this is a type. The C struct hack doesn't
1055 go through namespaces. */
1056 if (TREE_CODE (x) == NAMESPACE_DECL)
1057 set_identifier_type_value (name, NULL_TREE);
1059 if (oldlocal)
1061 tree d = oldlocal;
1063 while (oldlocal
1064 && VAR_P (oldlocal)
1065 && DECL_DEAD_FOR_LOCAL (oldlocal))
1066 oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal);
1068 if (oldlocal == NULL_TREE)
1069 oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d));
1072 /* If this is an extern function declaration, see if we
1073 have a global definition or declaration for the function. */
1074 if (oldlocal == NULL_TREE
1075 && DECL_EXTERNAL (x)
1076 && oldglobal != NULL_TREE
1077 && TREE_CODE (x) == FUNCTION_DECL
1078 && TREE_CODE (oldglobal) == FUNCTION_DECL)
1080 /* We have one. Their types must agree. */
1081 if (decls_match (x, oldglobal))
1082 /* OK */;
1083 else
1085 warning (0, "extern declaration of %q#D doesn%'t match", x);
1086 warning (0, "global declaration %q+#D", oldglobal);
1089 /* If we have a local external declaration,
1090 and no file-scope declaration has yet been seen,
1091 then if we later have a file-scope decl it must not be static. */
1092 if (oldlocal == NULL_TREE
1093 && oldglobal == NULL_TREE
1094 && DECL_EXTERNAL (x)
1095 && TREE_PUBLIC (x))
1096 TREE_PUBLIC (name) = 1;
1098 /* Don't complain about the parms we push and then pop
1099 while tentatively parsing a function declarator. */
1100 if (TREE_CODE (x) == PARM_DECL && DECL_CONTEXT (x) == NULL_TREE)
1101 /* Ignore. */;
1103 /* Warn if shadowing an argument at the top level of the body. */
1104 else if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
1105 /* Inline decls shadow nothing. */
1106 && !DECL_FROM_INLINE (x)
1107 && (TREE_CODE (oldlocal) == PARM_DECL
1108 || VAR_P (oldlocal)
1109 /* If the old decl is a type decl, only warn if the
1110 old decl is an explicit typedef or if both the old
1111 and new decls are type decls. */
1112 || (TREE_CODE (oldlocal) == TYPE_DECL
1113 && (!DECL_ARTIFICIAL (oldlocal)
1114 || TREE_CODE (x) == TYPE_DECL)))
1115 /* Don't check for internally generated vars unless
1116 it's an implicit typedef (see create_implicit_typedef
1117 in decl.c). */
1118 && (!DECL_ARTIFICIAL (x) || DECL_IMPLICIT_TYPEDEF_P (x)))
1120 bool nowarn = false;
1122 /* Don't complain if it's from an enclosing function. */
1123 if (DECL_CONTEXT (oldlocal) == current_function_decl
1124 && TREE_CODE (x) != PARM_DECL
1125 && TREE_CODE (oldlocal) == PARM_DECL)
1127 /* Go to where the parms should be and see if we find
1128 them there. */
1129 cp_binding_level *b = current_binding_level->level_chain;
1131 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
1132 /* Skip the ctor/dtor cleanup level. */
1133 b = b->level_chain;
1135 /* ARM $8.3 */
1136 if (b->kind == sk_function_parms)
1138 error ("declaration of %q#D shadows a parameter", x);
1139 nowarn = true;
1143 /* The local structure or class can't use parameters of
1144 the containing function anyway. */
1145 if (DECL_CONTEXT (oldlocal) != current_function_decl)
1147 cp_binding_level *scope = current_binding_level;
1148 tree context = DECL_CONTEXT (oldlocal);
1149 for (; scope; scope = scope->level_chain)
1151 if (scope->kind == sk_function_parms
1152 && scope->this_entity == context)
1153 break;
1154 if (scope->kind == sk_class
1155 && !LAMBDA_TYPE_P (scope->this_entity))
1157 nowarn = true;
1158 break;
1162 /* Error if redeclaring a local declared in a
1163 for-init-statement or in the condition of an if or
1164 switch statement when the new declaration is in the
1165 outermost block of the controlled statement.
1166 Redeclaring a variable from a for or while condition is
1167 detected elsewhere. */
1168 else if (VAR_P (oldlocal)
1169 && oldscope == current_binding_level->level_chain
1170 && (oldscope->kind == sk_cond
1171 || oldscope->kind == sk_for))
1173 error ("redeclaration of %q#D", x);
1174 inform (input_location, "%q+#D previously declared here",
1175 oldlocal);
1176 nowarn = true;
1178 /* C++11:
1179 3.3.3/3: The name declared in an exception-declaration (...)
1180 shall not be redeclared in the outermost block of the handler.
1181 3.3.3/2: A parameter name shall not be redeclared (...) in
1182 the outermost block of any handler associated with a
1183 function-try-block.
1184 3.4.1/15: The function parameter names shall not be redeclared
1185 in the exception-declaration nor in the outermost block of a
1186 handler for the function-try-block. */
1187 else if ((VAR_P (oldlocal)
1188 && oldscope == current_binding_level->level_chain
1189 && oldscope->kind == sk_catch)
1190 || (TREE_CODE (oldlocal) == PARM_DECL
1191 && (current_binding_level->kind == sk_catch
1192 || (current_binding_level->level_chain->kind
1193 == sk_catch))
1194 && in_function_try_handler))
1196 if (permerror (input_location, "redeclaration of %q#D", x))
1197 inform (input_location, "%q+#D previously declared here",
1198 oldlocal);
1199 nowarn = true;
1202 if (warn_shadow && !nowarn)
1204 bool warned;
1206 if (TREE_CODE (oldlocal) == PARM_DECL)
1207 warned = warning_at (input_location, OPT_Wshadow,
1208 "declaration of %q#D shadows a parameter", x);
1209 else if (is_capture_proxy (oldlocal))
1210 warned = warning_at (input_location, OPT_Wshadow,
1211 "declaration of %qD shadows a lambda capture",
1213 else
1214 warned = warning_at (input_location, OPT_Wshadow,
1215 "declaration of %qD shadows a previous local",
1218 if (warned)
1219 inform (DECL_SOURCE_LOCATION (oldlocal),
1220 "shadowed declaration is here");
1224 /* Maybe warn if shadowing something else. */
1225 else if (warn_shadow && !DECL_EXTERNAL (x)
1226 /* No shadow warnings for internally generated vars unless
1227 it's an implicit typedef (see create_implicit_typedef
1228 in decl.c). */
1229 && (! DECL_ARTIFICIAL (x) || DECL_IMPLICIT_TYPEDEF_P (x))
1230 /* No shadow warnings for vars made for inlining. */
1231 && ! DECL_FROM_INLINE (x))
1233 tree member;
1235 if (nonlambda_method_basetype ())
1236 member = lookup_member (current_nonlambda_class_type (),
1237 name,
1238 /*protect=*/0,
1239 /*want_type=*/false,
1240 tf_warning_or_error);
1241 else
1242 member = NULL_TREE;
1244 if (member && !TREE_STATIC (member))
1246 if (BASELINK_P (member))
1247 member = BASELINK_FUNCTIONS (member);
1248 member = OVL_CURRENT (member);
1250 /* Do not warn if a variable shadows a function, unless
1251 the variable is a function or a pointer-to-function. */
1252 if (TREE_CODE (member) != FUNCTION_DECL
1253 || TREE_CODE (x) == FUNCTION_DECL
1254 || TYPE_PTRFN_P (TREE_TYPE (x))
1255 || TYPE_PTRMEMFUNC_P (TREE_TYPE (x)))
1257 if (warning_at (input_location, OPT_Wshadow,
1258 "declaration of %qD shadows a member of %qT",
1259 x, current_nonlambda_class_type ())
1260 && DECL_P (member))
1261 inform (DECL_SOURCE_LOCATION (member),
1262 "shadowed declaration is here");
1265 else if (oldglobal != NULL_TREE
1266 && (VAR_P (oldglobal)
1267 /* If the old decl is a type decl, only warn if the
1268 old decl is an explicit typedef or if both the
1269 old and new decls are type decls. */
1270 || (TREE_CODE (oldglobal) == TYPE_DECL
1271 && (!DECL_ARTIFICIAL (oldglobal)
1272 || TREE_CODE (x) == TYPE_DECL)))
1273 && !instantiating_current_function_p ())
1274 /* XXX shadow warnings in outer-more namespaces */
1276 if (warning_at (input_location, OPT_Wshadow,
1277 "declaration of %qD shadows a "
1278 "global declaration", x))
1279 inform (DECL_SOURCE_LOCATION (oldglobal),
1280 "shadowed declaration is here");
1285 if (VAR_P (x))
1286 maybe_register_incomplete_var (x);
1289 if (need_new_binding)
1290 add_decl_to_level (x,
1291 DECL_NAMESPACE_SCOPE_P (x)
1292 ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x))
1293 : current_binding_level);
1295 return x;
1298 /* Wrapper for pushdecl_maybe_friend_1. */
1300 tree
1301 pushdecl_maybe_friend (tree x, bool is_friend)
1303 tree ret;
1304 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
1305 ret = pushdecl_maybe_friend_1 (x, is_friend);
1306 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
1307 return ret;
1310 /* Record a decl-node X as belonging to the current lexical scope. */
1312 tree
1313 pushdecl (tree x)
1315 return pushdecl_maybe_friend (x, false);
1318 /* Enter DECL into the symbol table, if that's appropriate. Returns
1319 DECL, or a modified version thereof. */
1321 tree
1322 maybe_push_decl (tree decl)
1324 tree type = TREE_TYPE (decl);
1326 /* Add this decl to the current binding level, but not if it comes
1327 from another scope, e.g. a static member variable. TEM may equal
1328 DECL or it may be a previous decl of the same name. */
1329 if (decl == error_mark_node
1330 || (TREE_CODE (decl) != PARM_DECL
1331 && DECL_CONTEXT (decl) != NULL_TREE
1332 /* Definitions of namespace members outside their namespace are
1333 possible. */
1334 && !DECL_NAMESPACE_SCOPE_P (decl))
1335 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
1336 || type == unknown_type_node
1337 /* The declaration of a template specialization does not affect
1338 the functions available for overload resolution, so we do not
1339 call pushdecl. */
1340 || (TREE_CODE (decl) == FUNCTION_DECL
1341 && DECL_TEMPLATE_SPECIALIZATION (decl)))
1342 return decl;
1343 else
1344 return pushdecl (decl);
1347 /* Bind DECL to ID in the current_binding_level, assumed to be a local
1348 binding level. If PUSH_USING is set in FLAGS, we know that DECL
1349 doesn't really belong to this binding level, that it got here
1350 through a using-declaration. */
1352 void
1353 push_local_binding (tree id, tree decl, int flags)
1355 cp_binding_level *b;
1357 /* Skip over any local classes. This makes sense if we call
1358 push_local_binding with a friend decl of a local class. */
1359 b = innermost_nonclass_level ();
1361 if (lookup_name_innermost_nonclass_level (id))
1363 /* Supplement the existing binding. */
1364 if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
1365 /* It didn't work. Something else must be bound at this
1366 level. Do not add DECL to the list of things to pop
1367 later. */
1368 return;
1370 else
1371 /* Create a new binding. */
1372 push_binding (id, decl, b);
1374 if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
1375 /* We must put the OVERLOAD into a TREE_LIST since the
1376 TREE_CHAIN of an OVERLOAD is already used. Similarly for
1377 decls that got here through a using-declaration. */
1378 decl = build_tree_list (NULL_TREE, decl);
1380 /* And put DECL on the list of things declared by the current
1381 binding level. */
1382 add_decl_to_level (decl, b);
1385 /* Check to see whether or not DECL is a variable that would have been
1386 in scope under the ARM, but is not in scope under the ANSI/ISO
1387 standard. If so, issue an error message. If name lookup would
1388 work in both cases, but return a different result, this function
1389 returns the result of ANSI/ISO lookup. Otherwise, it returns
1390 DECL. */
1392 tree
1393 check_for_out_of_scope_variable (tree decl)
1395 tree shadowed;
1397 /* We only care about out of scope variables. */
1398 if (!(VAR_P (decl) && DECL_DEAD_FOR_LOCAL (decl)))
1399 return decl;
1401 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (decl)
1402 ? DECL_SHADOWED_FOR_VAR (decl) : NULL_TREE ;
1403 while (shadowed != NULL_TREE && VAR_P (shadowed)
1404 && DECL_DEAD_FOR_LOCAL (shadowed))
1405 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (shadowed)
1406 ? DECL_SHADOWED_FOR_VAR (shadowed) : NULL_TREE;
1407 if (!shadowed)
1408 shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl));
1409 if (shadowed)
1411 if (!DECL_ERROR_REPORTED (decl))
1413 warning (0, "name lookup of %qD changed", DECL_NAME (decl));
1414 warning (0, " matches this %q+D under ISO standard rules",
1415 shadowed);
1416 warning (0, " matches this %q+D under old rules", decl);
1417 DECL_ERROR_REPORTED (decl) = 1;
1419 return shadowed;
1422 /* If we have already complained about this declaration, there's no
1423 need to do it again. */
1424 if (DECL_ERROR_REPORTED (decl))
1425 return decl;
1427 DECL_ERROR_REPORTED (decl) = 1;
1429 if (TREE_TYPE (decl) == error_mark_node)
1430 return decl;
1432 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
1434 error ("name lookup of %qD changed for ISO %<for%> scoping",
1435 DECL_NAME (decl));
1436 error (" cannot use obsolete binding at %q+D because "
1437 "it has a destructor", decl);
1438 return error_mark_node;
1440 else
1442 permerror (input_location, "name lookup of %qD changed for ISO %<for%> scoping",
1443 DECL_NAME (decl));
1444 if (flag_permissive)
1445 permerror (input_location, " using obsolete binding at %q+D", decl);
1446 else
1448 static bool hint;
1449 if (!hint)
1451 inform (input_location, "(if you use %<-fpermissive%> G++ will accept your code)");
1452 hint = true;
1457 return decl;
1460 /* true means unconditionally make a BLOCK for the next level pushed. */
1462 static bool keep_next_level_flag;
1464 static int binding_depth = 0;
1466 static void
1467 indent (int depth)
1469 int i;
1471 for (i = 0; i < depth * 2; i++)
1472 putc (' ', stderr);
1475 /* Return a string describing the kind of SCOPE we have. */
1476 static const char *
1477 cp_binding_level_descriptor (cp_binding_level *scope)
1479 /* The order of this table must match the "scope_kind"
1480 enumerators. */
1481 static const char* scope_kind_names[] = {
1482 "block-scope",
1483 "cleanup-scope",
1484 "try-scope",
1485 "catch-scope",
1486 "for-scope",
1487 "function-parameter-scope",
1488 "class-scope",
1489 "namespace-scope",
1490 "template-parameter-scope",
1491 "template-explicit-spec-scope"
1493 const scope_kind kind = scope->explicit_spec_p
1494 ? sk_template_spec : scope->kind;
1496 return scope_kind_names[kind];
1499 /* Output a debugging information about SCOPE when performing
1500 ACTION at LINE. */
1501 static void
1502 cp_binding_level_debug (cp_binding_level *scope, int line, const char *action)
1504 const char *desc = cp_binding_level_descriptor (scope);
1505 if (scope->this_entity)
1506 verbatim ("%s %s(%E) %p %d\n", action, desc,
1507 scope->this_entity, (void *) scope, line);
1508 else
1509 verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
1512 /* Return the estimated initial size of the hashtable of a NAMESPACE
1513 scope. */
1515 static inline size_t
1516 namespace_scope_ht_size (tree ns)
1518 tree name = DECL_NAME (ns);
1520 return name == std_identifier
1521 ? NAMESPACE_STD_HT_SIZE
1522 : (name == global_scope_name
1523 ? GLOBAL_SCOPE_HT_SIZE
1524 : NAMESPACE_ORDINARY_HT_SIZE);
1527 /* A chain of binding_level structures awaiting reuse. */
1529 static GTY((deletable)) cp_binding_level *free_binding_level;
1531 /* Insert SCOPE as the innermost binding level. */
1533 void
1534 push_binding_level (cp_binding_level *scope)
1536 /* Add it to the front of currently active scopes stack. */
1537 scope->level_chain = current_binding_level;
1538 current_binding_level = scope;
1539 keep_next_level_flag = false;
1541 if (ENABLE_SCOPE_CHECKING)
1543 scope->binding_depth = binding_depth;
1544 indent (binding_depth);
1545 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
1546 "push");
1547 binding_depth++;
1551 /* Create a new KIND scope and make it the top of the active scopes stack.
1552 ENTITY is the scope of the associated C++ entity (namespace, class,
1553 function, C++0x enumeration); it is NULL otherwise. */
1555 cp_binding_level *
1556 begin_scope (scope_kind kind, tree entity)
1558 cp_binding_level *scope;
1560 /* Reuse or create a struct for this binding level. */
1561 if (!ENABLE_SCOPE_CHECKING && free_binding_level)
1563 scope = free_binding_level;
1564 memset (scope, 0, sizeof (cp_binding_level));
1565 free_binding_level = scope->level_chain;
1567 else
1568 scope = ggc_cleared_alloc<cp_binding_level> ();
1570 scope->this_entity = entity;
1571 scope->more_cleanups_ok = true;
1572 switch (kind)
1574 case sk_cleanup:
1575 scope->keep = true;
1576 break;
1578 case sk_template_spec:
1579 scope->explicit_spec_p = true;
1580 kind = sk_template_parms;
1581 /* Fall through. */
1582 case sk_template_parms:
1583 case sk_block:
1584 case sk_try:
1585 case sk_catch:
1586 case sk_for:
1587 case sk_cond:
1588 case sk_class:
1589 case sk_scoped_enum:
1590 case sk_function_parms:
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 anonymous 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 anonymous 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 /* It is impossible to overload a built-in function; any explicit
2590 declaration eliminates the built-in declaration. So, if OLDVAL
2591 is a built-in, then we can just pretend it isn't there. */
2592 if (oldval
2593 && TREE_CODE (oldval) == FUNCTION_DECL
2594 && DECL_ANTICIPATED (oldval)
2595 && !DECL_HIDDEN_FRIEND_P (oldval))
2596 oldval = NULL_TREE;
2598 if (decls.value)
2600 /* Check for using functions. */
2601 if (is_overloaded_fn (decls.value))
2603 tree tmp, tmp1;
2605 if (oldval && !is_overloaded_fn (oldval))
2607 error ("%qD is already declared in this scope", name);
2608 oldval = NULL_TREE;
2611 *newval = oldval;
2612 for (tmp = decls.value; tmp; tmp = OVL_NEXT (tmp))
2614 tree new_fn = OVL_CURRENT (tmp);
2616 /* [namespace.udecl]
2618 If a function declaration in namespace scope or block
2619 scope has the same name and the same parameter types as a
2620 function introduced by a using declaration the program is
2621 ill-formed. */
2622 for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1))
2624 tree old_fn = OVL_CURRENT (tmp1);
2626 if (new_fn == old_fn)
2627 /* The function already exists in the current namespace. */
2628 break;
2629 else if (TREE_CODE (tmp1) == OVERLOAD && OVL_USED (tmp1))
2630 continue; /* this is a using decl */
2631 else if (compparms_for_decl_and_using_decl (new_fn, old_fn))
2633 gcc_assert (!DECL_ANTICIPATED (old_fn)
2634 || DECL_HIDDEN_FRIEND_P (old_fn));
2636 /* There was already a non-using declaration in
2637 this scope with the same parameter types. If both
2638 are the same extern "C" functions, that's ok. */
2639 if (decls_match (new_fn, old_fn))
2640 break;
2641 else
2643 diagnose_name_conflict (new_fn, old_fn);
2644 break;
2649 /* If we broke out of the loop, there's no reason to add
2650 this function to the using declarations for this
2651 scope. */
2652 if (tmp1)
2653 continue;
2655 /* If we are adding to an existing OVERLOAD, then we no
2656 longer know the type of the set of functions. */
2657 if (*newval && TREE_CODE (*newval) == OVERLOAD)
2658 TREE_TYPE (*newval) = unknown_type_node;
2659 /* Add this new function to the set. */
2660 *newval = build_overload (OVL_CURRENT (tmp), *newval);
2661 /* If there is only one function, then we use its type. (A
2662 using-declaration naming a single function can be used in
2663 contexts where overload resolution cannot be
2664 performed.) */
2665 if (TREE_CODE (*newval) != OVERLOAD)
2667 *newval = ovl_cons (*newval, NULL_TREE);
2668 TREE_TYPE (*newval) = TREE_TYPE (OVL_CURRENT (tmp));
2670 OVL_USED (*newval) = 1;
2673 else
2675 *newval = decls.value;
2676 if (oldval && !decls_match (*newval, oldval))
2677 error ("%qD is already declared in this scope", name);
2680 else
2681 *newval = oldval;
2683 if (decls.type && TREE_CODE (decls.type) == TREE_LIST)
2685 error ("reference to %qD is ambiguous", name);
2686 print_candidates (decls.type);
2688 else
2690 *newtype = decls.type;
2691 if (oldtype && *newtype && !decls_match (oldtype, *newtype))
2692 error ("%qD is already declared in this scope", name);
2695 /* If *newval is empty, shift any class or enumeration name down. */
2696 if (!*newval)
2698 *newval = *newtype;
2699 *newtype = NULL_TREE;
2703 /* Process a using-declaration at function scope. */
2705 void
2706 do_local_using_decl (tree decl, tree scope, tree name)
2708 tree oldval, oldtype, newval, newtype;
2709 tree orig_decl = decl;
2711 decl = validate_nonmember_using_decl (decl, scope, name);
2712 if (decl == NULL_TREE)
2713 return;
2715 if (building_stmt_list_p ()
2716 && at_function_scope_p ())
2717 add_decl_expr (decl);
2719 oldval = lookup_name_innermost_nonclass_level (name);
2720 oldtype = lookup_type_current_level (name);
2722 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
2724 if (newval)
2726 if (is_overloaded_fn (newval))
2728 tree fn, term;
2730 /* We only need to push declarations for those functions
2731 that were not already bound in the current level.
2732 The old value might be NULL_TREE, it might be a single
2733 function, or an OVERLOAD. */
2734 if (oldval && TREE_CODE (oldval) == OVERLOAD)
2735 term = OVL_FUNCTION (oldval);
2736 else
2737 term = oldval;
2738 for (fn = newval; fn && OVL_CURRENT (fn) != term;
2739 fn = OVL_NEXT (fn))
2740 push_overloaded_decl (OVL_CURRENT (fn),
2741 PUSH_LOCAL | PUSH_USING,
2742 false);
2744 else
2745 push_local_binding (name, newval, PUSH_USING);
2747 if (newtype)
2749 push_local_binding (name, newtype, PUSH_USING);
2750 set_identifier_type_value (name, newtype);
2753 /* Emit debug info. */
2754 if (!processing_template_decl)
2755 cp_emit_debug_info_for_using (orig_decl, current_scope());
2758 /* Returns true if ROOT (a namespace, class, or function) encloses
2759 CHILD. CHILD may be either a class type or a namespace. */
2761 bool
2762 is_ancestor (tree root, tree child)
2764 gcc_assert ((TREE_CODE (root) == NAMESPACE_DECL
2765 || TREE_CODE (root) == FUNCTION_DECL
2766 || CLASS_TYPE_P (root)));
2767 gcc_assert ((TREE_CODE (child) == NAMESPACE_DECL
2768 || CLASS_TYPE_P (child)));
2770 /* The global namespace encloses everything. */
2771 if (root == global_namespace)
2772 return true;
2774 while (true)
2776 /* If we've run out of scopes, stop. */
2777 if (!child)
2778 return false;
2779 /* If we've reached the ROOT, it encloses CHILD. */
2780 if (root == child)
2781 return true;
2782 /* Go out one level. */
2783 if (TYPE_P (child))
2784 child = TYPE_NAME (child);
2785 child = DECL_CONTEXT (child);
2789 /* Enter the class or namespace scope indicated by T suitable for name
2790 lookup. T can be arbitrary scope, not necessary nested inside the
2791 current scope. Returns a non-null scope to pop iff pop_scope
2792 should be called later to exit this scope. */
2794 tree
2795 push_scope (tree t)
2797 if (TREE_CODE (t) == NAMESPACE_DECL)
2798 push_decl_namespace (t);
2799 else if (CLASS_TYPE_P (t))
2801 if (!at_class_scope_p ()
2802 || !same_type_p (current_class_type, t))
2803 push_nested_class (t);
2804 else
2805 /* T is the same as the current scope. There is therefore no
2806 need to re-enter the scope. Since we are not actually
2807 pushing a new scope, our caller should not call
2808 pop_scope. */
2809 t = NULL_TREE;
2812 return t;
2815 /* Leave scope pushed by push_scope. */
2817 void
2818 pop_scope (tree t)
2820 if (t == NULL_TREE)
2821 return;
2822 if (TREE_CODE (t) == NAMESPACE_DECL)
2823 pop_decl_namespace ();
2824 else if CLASS_TYPE_P (t)
2825 pop_nested_class ();
2828 /* Subroutine of push_inner_scope. */
2830 static void
2831 push_inner_scope_r (tree outer, tree inner)
2833 tree prev;
2835 if (outer == inner
2836 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
2837 return;
2839 prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
2840 if (outer != prev)
2841 push_inner_scope_r (outer, prev);
2842 if (TREE_CODE (inner) == NAMESPACE_DECL)
2844 cp_binding_level *save_template_parm = 0;
2845 /* Temporary take out template parameter scopes. They are saved
2846 in reversed order in save_template_parm. */
2847 while (current_binding_level->kind == sk_template_parms)
2849 cp_binding_level *b = current_binding_level;
2850 current_binding_level = b->level_chain;
2851 b->level_chain = save_template_parm;
2852 save_template_parm = b;
2855 resume_scope (NAMESPACE_LEVEL (inner));
2856 current_namespace = inner;
2858 /* Restore template parameter scopes. */
2859 while (save_template_parm)
2861 cp_binding_level *b = save_template_parm;
2862 save_template_parm = b->level_chain;
2863 b->level_chain = current_binding_level;
2864 current_binding_level = b;
2867 else
2868 pushclass (inner);
2871 /* Enter the scope INNER from current scope. INNER must be a scope
2872 nested inside current scope. This works with both name lookup and
2873 pushing name into scope. In case a template parameter scope is present,
2874 namespace is pushed under the template parameter scope according to
2875 name lookup rule in 14.6.1/6.
2877 Return the former current scope suitable for pop_inner_scope. */
2879 tree
2880 push_inner_scope (tree inner)
2882 tree outer = current_scope ();
2883 if (!outer)
2884 outer = current_namespace;
2886 push_inner_scope_r (outer, inner);
2887 return outer;
2890 /* Exit the current scope INNER back to scope OUTER. */
2892 void
2893 pop_inner_scope (tree outer, tree inner)
2895 if (outer == inner
2896 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
2897 return;
2899 while (outer != inner)
2901 if (TREE_CODE (inner) == NAMESPACE_DECL)
2903 cp_binding_level *save_template_parm = 0;
2904 /* Temporary take out template parameter scopes. They are saved
2905 in reversed order in save_template_parm. */
2906 while (current_binding_level->kind == sk_template_parms)
2908 cp_binding_level *b = current_binding_level;
2909 current_binding_level = b->level_chain;
2910 b->level_chain = save_template_parm;
2911 save_template_parm = b;
2914 pop_namespace ();
2916 /* Restore template parameter scopes. */
2917 while (save_template_parm)
2919 cp_binding_level *b = save_template_parm;
2920 save_template_parm = b->level_chain;
2921 b->level_chain = current_binding_level;
2922 current_binding_level = b;
2925 else
2926 popclass ();
2928 inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
2932 /* Do a pushlevel for class declarations. */
2934 void
2935 pushlevel_class (void)
2937 class_binding_level = begin_scope (sk_class, current_class_type);
2940 /* ...and a poplevel for class declarations. */
2942 void
2943 poplevel_class (void)
2945 cp_binding_level *level = class_binding_level;
2946 cp_class_binding *cb;
2947 size_t i;
2948 tree shadowed;
2950 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2951 gcc_assert (level != 0);
2953 /* If we're leaving a toplevel class, cache its binding level. */
2954 if (current_class_depth == 1)
2955 previous_class_level = level;
2956 for (shadowed = level->type_shadowed;
2957 shadowed;
2958 shadowed = TREE_CHAIN (shadowed))
2959 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
2961 /* Remove the bindings for all of the class-level declarations. */
2962 if (level->class_shadowed)
2964 FOR_EACH_VEC_ELT (*level->class_shadowed, i, cb)
2966 IDENTIFIER_BINDING (cb->identifier) = cb->base->previous;
2967 cxx_binding_free (cb->base);
2969 ggc_free (level->class_shadowed);
2970 level->class_shadowed = NULL;
2973 /* Now, pop out of the binding level which we created up in the
2974 `pushlevel_class' routine. */
2975 gcc_assert (current_binding_level == level);
2976 leave_scope ();
2977 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2980 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
2981 appropriate. DECL is the value to which a name has just been
2982 bound. CLASS_TYPE is the class in which the lookup occurred. */
2984 static void
2985 set_inherited_value_binding_p (cxx_binding *binding, tree decl,
2986 tree class_type)
2988 if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
2990 tree context;
2992 if (TREE_CODE (decl) == OVERLOAD)
2993 context = ovl_scope (decl);
2994 else
2996 gcc_assert (DECL_P (decl));
2997 context = context_for_name_lookup (decl);
3000 if (is_properly_derived_from (class_type, context))
3001 INHERITED_VALUE_BINDING_P (binding) = 1;
3002 else
3003 INHERITED_VALUE_BINDING_P (binding) = 0;
3005 else if (binding->value == decl)
3006 /* We only encounter a TREE_LIST when there is an ambiguity in the
3007 base classes. Such an ambiguity can be overridden by a
3008 definition in this class. */
3009 INHERITED_VALUE_BINDING_P (binding) = 1;
3010 else
3011 INHERITED_VALUE_BINDING_P (binding) = 0;
3014 /* Make the declaration of X appear in CLASS scope. */
3016 bool
3017 pushdecl_class_level (tree x)
3019 tree name;
3020 bool is_valid = true;
3021 bool subtime;
3023 /* Do nothing if we're adding to an outer lambda closure type,
3024 outer_binding will add it later if it's needed. */
3025 if (current_class_type != class_binding_level->this_entity)
3026 return true;
3028 subtime = timevar_cond_start (TV_NAME_LOOKUP);
3029 /* Get the name of X. */
3030 if (TREE_CODE (x) == OVERLOAD)
3031 name = DECL_NAME (get_first_fn (x));
3032 else
3033 name = DECL_NAME (x);
3035 if (name)
3037 is_valid = push_class_level_binding (name, x);
3038 if (TREE_CODE (x) == TYPE_DECL)
3039 set_identifier_type_value (name, x);
3041 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3043 /* If X is an anonymous aggregate, all of its members are
3044 treated as if they were members of the class containing the
3045 aggregate, for naming purposes. */
3046 tree f;
3048 for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = DECL_CHAIN (f))
3050 location_t save_location = input_location;
3051 input_location = DECL_SOURCE_LOCATION (f);
3052 if (!pushdecl_class_level (f))
3053 is_valid = false;
3054 input_location = save_location;
3057 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3058 return is_valid;
3061 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
3062 scope. If the value returned is non-NULL, and the PREVIOUS field
3063 is not set, callers must set the PREVIOUS field explicitly. */
3065 static cxx_binding *
3066 get_class_binding (tree name, cp_binding_level *scope)
3068 tree class_type;
3069 tree type_binding;
3070 tree value_binding;
3071 cxx_binding *binding;
3073 class_type = scope->this_entity;
3075 /* Get the type binding. */
3076 type_binding = lookup_member (class_type, name,
3077 /*protect=*/2, /*want_type=*/true,
3078 tf_warning_or_error);
3079 /* Get the value binding. */
3080 value_binding = lookup_member (class_type, name,
3081 /*protect=*/2, /*want_type=*/false,
3082 tf_warning_or_error);
3084 if (value_binding
3085 && (TREE_CODE (value_binding) == TYPE_DECL
3086 || DECL_CLASS_TEMPLATE_P (value_binding)
3087 || (TREE_CODE (value_binding) == TREE_LIST
3088 && TREE_TYPE (value_binding) == error_mark_node
3089 && (TREE_CODE (TREE_VALUE (value_binding))
3090 == TYPE_DECL))))
3091 /* We found a type binding, even when looking for a non-type
3092 binding. This means that we already processed this binding
3093 above. */
3095 else if (value_binding)
3097 if (TREE_CODE (value_binding) == TREE_LIST
3098 && TREE_TYPE (value_binding) == error_mark_node)
3099 /* NAME is ambiguous. */
3101 else if (BASELINK_P (value_binding))
3102 /* NAME is some overloaded functions. */
3103 value_binding = BASELINK_FUNCTIONS (value_binding);
3106 /* If we found either a type binding or a value binding, create a
3107 new binding object. */
3108 if (type_binding || value_binding)
3110 binding = new_class_binding (name,
3111 value_binding,
3112 type_binding,
3113 scope);
3114 /* This is a class-scope binding, not a block-scope binding. */
3115 LOCAL_BINDING_P (binding) = 0;
3116 set_inherited_value_binding_p (binding, value_binding, class_type);
3118 else
3119 binding = NULL;
3121 return binding;
3124 /* Make the declaration(s) of X appear in CLASS scope under the name
3125 NAME. Returns true if the binding is valid. */
3127 static bool
3128 push_class_level_binding_1 (tree name, tree x)
3130 cxx_binding *binding;
3131 tree decl = x;
3132 bool ok;
3134 /* The class_binding_level will be NULL if x is a template
3135 parameter name in a member template. */
3136 if (!class_binding_level)
3137 return true;
3139 if (name == error_mark_node)
3140 return false;
3142 /* Can happen for an erroneous declaration (c++/60384). */
3143 if (!identifier_p (name))
3145 gcc_assert (errorcount || sorrycount);
3146 return false;
3149 /* Check for invalid member names. But don't worry about a default
3150 argument-scope lambda being pushed after the class is complete. */
3151 gcc_assert (TYPE_BEING_DEFINED (current_class_type)
3152 || LAMBDA_TYPE_P (TREE_TYPE (decl)));
3153 /* Check that we're pushing into the right binding level. */
3154 gcc_assert (current_class_type == class_binding_level->this_entity);
3156 /* We could have been passed a tree list if this is an ambiguous
3157 declaration. If so, pull the declaration out because
3158 check_template_shadow will not handle a TREE_LIST. */
3159 if (TREE_CODE (decl) == TREE_LIST
3160 && TREE_TYPE (decl) == error_mark_node)
3161 decl = TREE_VALUE (decl);
3163 if (!check_template_shadow (decl))
3164 return false;
3166 /* [class.mem]
3168 If T is the name of a class, then each of the following shall
3169 have a name different from T:
3171 -- every static data member of class T;
3173 -- every member of class T that is itself a type;
3175 -- every enumerator of every member of class T that is an
3176 enumerated type;
3178 -- every member of every anonymous union that is a member of
3179 class T.
3181 (Non-static data members were also forbidden to have the same
3182 name as T until TC1.) */
3183 if ((VAR_P (x)
3184 || TREE_CODE (x) == CONST_DECL
3185 || (TREE_CODE (x) == TYPE_DECL
3186 && !DECL_SELF_REFERENCE_P (x))
3187 /* A data member of an anonymous union. */
3188 || (TREE_CODE (x) == FIELD_DECL
3189 && DECL_CONTEXT (x) != current_class_type))
3190 && DECL_NAME (x) == constructor_name (current_class_type))
3192 tree scope = context_for_name_lookup (x);
3193 if (TYPE_P (scope) && same_type_p (scope, current_class_type))
3195 error ("%qD has the same name as the class in which it is "
3196 "declared",
3198 return false;
3202 /* Get the current binding for NAME in this class, if any. */
3203 binding = IDENTIFIER_BINDING (name);
3204 if (!binding || binding->scope != class_binding_level)
3206 binding = get_class_binding (name, class_binding_level);
3207 /* If a new binding was created, put it at the front of the
3208 IDENTIFIER_BINDING list. */
3209 if (binding)
3211 binding->previous = IDENTIFIER_BINDING (name);
3212 IDENTIFIER_BINDING (name) = binding;
3216 /* If there is already a binding, then we may need to update the
3217 current value. */
3218 if (binding && binding->value)
3220 tree bval = binding->value;
3221 tree old_decl = NULL_TREE;
3222 tree target_decl = strip_using_decl (decl);
3223 tree target_bval = strip_using_decl (bval);
3225 if (INHERITED_VALUE_BINDING_P (binding))
3227 /* If the old binding was from a base class, and was for a
3228 tag name, slide it over to make room for the new binding.
3229 The old binding is still visible if explicitly qualified
3230 with a class-key. */
3231 if (TREE_CODE (target_bval) == TYPE_DECL
3232 && DECL_ARTIFICIAL (target_bval)
3233 && !(TREE_CODE (target_decl) == TYPE_DECL
3234 && DECL_ARTIFICIAL (target_decl)))
3236 old_decl = binding->type;
3237 binding->type = bval;
3238 binding->value = NULL_TREE;
3239 INHERITED_VALUE_BINDING_P (binding) = 0;
3241 else
3243 old_decl = bval;
3244 /* Any inherited type declaration is hidden by the type
3245 declaration in the derived class. */
3246 if (TREE_CODE (target_decl) == TYPE_DECL
3247 && DECL_ARTIFICIAL (target_decl))
3248 binding->type = NULL_TREE;
3251 else if (TREE_CODE (target_decl) == OVERLOAD
3252 && is_overloaded_fn (target_bval))
3253 old_decl = bval;
3254 else if (TREE_CODE (decl) == USING_DECL
3255 && TREE_CODE (bval) == USING_DECL
3256 && same_type_p (USING_DECL_SCOPE (decl),
3257 USING_DECL_SCOPE (bval)))
3258 /* This is a using redeclaration that will be diagnosed later
3259 in supplement_binding */
3261 else if (TREE_CODE (decl) == USING_DECL
3262 && TREE_CODE (bval) == USING_DECL
3263 && DECL_DEPENDENT_P (decl)
3264 && DECL_DEPENDENT_P (bval))
3265 return true;
3266 else if (TREE_CODE (decl) == USING_DECL
3267 && is_overloaded_fn (target_bval))
3268 old_decl = bval;
3269 else if (TREE_CODE (bval) == USING_DECL
3270 && is_overloaded_fn (target_decl))
3271 return true;
3273 if (old_decl && binding->scope == class_binding_level)
3275 binding->value = x;
3276 /* It is always safe to clear INHERITED_VALUE_BINDING_P
3277 here. This function is only used to register bindings
3278 from with the class definition itself. */
3279 INHERITED_VALUE_BINDING_P (binding) = 0;
3280 return true;
3284 /* Note that we declared this value so that we can issue an error if
3285 this is an invalid redeclaration of a name already used for some
3286 other purpose. */
3287 note_name_declared_in_class (name, decl);
3289 /* If we didn't replace an existing binding, put the binding on the
3290 stack of bindings for the identifier, and update the shadowed
3291 list. */
3292 if (binding && binding->scope == class_binding_level)
3293 /* Supplement the existing binding. */
3294 ok = supplement_binding (binding, decl);
3295 else
3297 /* Create a new binding. */
3298 push_binding (name, decl, class_binding_level);
3299 ok = true;
3302 return ok;
3305 /* Wrapper for push_class_level_binding_1. */
3307 bool
3308 push_class_level_binding (tree name, tree x)
3310 bool ret;
3311 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3312 ret = push_class_level_binding_1 (name, x);
3313 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3314 return ret;
3317 /* Process "using SCOPE::NAME" in a class scope. Return the
3318 USING_DECL created. */
3320 tree
3321 do_class_using_decl (tree scope, tree name)
3323 /* The USING_DECL returned by this function. */
3324 tree value;
3325 /* The declaration (or declarations) name by this using
3326 declaration. NULL if we are in a template and cannot figure out
3327 what has been named. */
3328 tree decl;
3329 /* True if SCOPE is a dependent type. */
3330 bool scope_dependent_p;
3331 /* True if SCOPE::NAME is dependent. */
3332 bool name_dependent_p;
3333 /* True if any of the bases of CURRENT_CLASS_TYPE are dependent. */
3334 bool bases_dependent_p;
3335 tree binfo;
3336 tree base_binfo;
3337 int i;
3339 if (name == error_mark_node)
3340 return NULL_TREE;
3342 if (!scope || !TYPE_P (scope))
3344 error ("using-declaration for non-member at class scope");
3345 return NULL_TREE;
3348 /* Make sure the name is not invalid */
3349 if (TREE_CODE (name) == BIT_NOT_EXPR)
3351 error ("%<%T::%D%> names destructor", scope, name);
3352 return NULL_TREE;
3354 /* Using T::T declares inheriting ctors, even if T is a typedef. */
3355 if (MAYBE_CLASS_TYPE_P (scope)
3356 && (name == TYPE_IDENTIFIER (scope)
3357 || constructor_name_p (name, scope)))
3359 maybe_warn_cpp0x (CPP0X_INHERITING_CTORS);
3360 name = ctor_identifier;
3362 if (constructor_name_p (name, current_class_type))
3364 error ("%<%T::%D%> names constructor in %qT",
3365 scope, name, current_class_type);
3366 return NULL_TREE;
3369 scope_dependent_p = dependent_scope_p (scope);
3370 name_dependent_p = (scope_dependent_p
3371 || (IDENTIFIER_TYPENAME_P (name)
3372 && dependent_type_p (TREE_TYPE (name))));
3374 bases_dependent_p = false;
3375 if (processing_template_decl)
3376 for (binfo = TYPE_BINFO (current_class_type), i = 0;
3377 BINFO_BASE_ITERATE (binfo, i, base_binfo);
3378 i++)
3379 if (dependent_type_p (TREE_TYPE (base_binfo)))
3381 bases_dependent_p = true;
3382 break;
3385 decl = NULL_TREE;
3387 /* From [namespace.udecl]:
3389 A using-declaration used as a member-declaration shall refer to a
3390 member of a base class of the class being defined.
3392 In general, we cannot check this constraint in a template because
3393 we do not know the entire set of base classes of the current
3394 class type. Morover, if SCOPE is dependent, it might match a
3395 non-dependent base. */
3397 if (!scope_dependent_p)
3399 base_kind b_kind;
3400 binfo = lookup_base (current_class_type, scope, ba_any, &b_kind,
3401 tf_warning_or_error);
3402 if (b_kind < bk_proper_base)
3404 if (!bases_dependent_p || b_kind == bk_same_type)
3406 error_not_base_type (scope, current_class_type);
3407 return NULL_TREE;
3410 else if (!name_dependent_p)
3412 decl = lookup_member (binfo, name, 0, false, tf_warning_or_error);
3413 if (!decl)
3415 error ("no members matching %<%T::%D%> in %q#T", scope, name,
3416 scope);
3417 return NULL_TREE;
3419 /* The binfo from which the functions came does not matter. */
3420 if (BASELINK_P (decl))
3421 decl = BASELINK_FUNCTIONS (decl);
3425 value = build_lang_decl (USING_DECL, name, NULL_TREE);
3426 USING_DECL_DECLS (value) = decl;
3427 USING_DECL_SCOPE (value) = scope;
3428 DECL_DEPENDENT_P (value) = !decl;
3430 return value;
3434 /* Return the binding value for name in scope. */
3437 static tree
3438 namespace_binding_1 (tree name, tree scope)
3440 cxx_binding *binding;
3442 if (SCOPE_FILE_SCOPE_P (scope))
3443 scope = global_namespace;
3444 else
3445 /* Unnecessary for the global namespace because it can't be an alias. */
3446 scope = ORIGINAL_NAMESPACE (scope);
3448 binding = cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
3450 return binding ? binding->value : NULL_TREE;
3453 tree
3454 namespace_binding (tree name, tree scope)
3456 tree ret;
3457 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3458 ret = namespace_binding_1 (name, scope);
3459 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3460 return ret;
3463 /* Set the binding value for name in scope. */
3465 static void
3466 set_namespace_binding_1 (tree name, tree scope, tree val)
3468 cxx_binding *b;
3470 if (scope == NULL_TREE)
3471 scope = global_namespace;
3472 b = binding_for_name (NAMESPACE_LEVEL (scope), name);
3473 if (!b->value || TREE_CODE (val) == OVERLOAD || val == error_mark_node)
3474 b->value = val;
3475 else
3476 supplement_binding (b, val);
3479 /* Wrapper for set_namespace_binding_1. */
3481 void
3482 set_namespace_binding (tree name, tree scope, tree val)
3484 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3485 set_namespace_binding_1 (name, scope, val);
3486 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3489 /* Set the context of a declaration to scope. Complain if we are not
3490 outside scope. */
3492 void
3493 set_decl_namespace (tree decl, tree scope, bool friendp)
3495 tree old;
3497 /* Get rid of namespace aliases. */
3498 scope = ORIGINAL_NAMESPACE (scope);
3500 /* It is ok for friends to be qualified in parallel space. */
3501 if (!friendp && !is_ancestor (current_namespace, scope))
3502 error ("declaration of %qD not in a namespace surrounding %qD",
3503 decl, scope);
3504 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3506 /* Writing "int N::i" to declare a variable within "N" is invalid. */
3507 if (scope == current_namespace)
3509 if (at_namespace_scope_p ())
3510 error ("explicit qualification in declaration of %qD",
3511 decl);
3512 return;
3515 /* See whether this has been declared in the namespace. */
3516 old = lookup_qualified_name (scope, DECL_NAME (decl), false, true);
3517 if (old == error_mark_node)
3518 /* No old declaration at all. */
3519 goto complain;
3520 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
3521 if (TREE_CODE (old) == TREE_LIST)
3523 error ("reference to %qD is ambiguous", decl);
3524 print_candidates (old);
3525 return;
3527 if (!is_overloaded_fn (decl))
3529 /* We might have found OLD in an inline namespace inside SCOPE. */
3530 if (TREE_CODE (decl) == TREE_CODE (old))
3531 DECL_CONTEXT (decl) = DECL_CONTEXT (old);
3532 /* Don't compare non-function decls with decls_match here, since
3533 it can't check for the correct constness at this
3534 point. pushdecl will find those errors later. */
3535 return;
3537 /* Since decl is a function, old should contain a function decl. */
3538 if (!is_overloaded_fn (old))
3539 goto complain;
3540 /* A template can be explicitly specialized in any namespace. */
3541 if (processing_explicit_instantiation)
3542 return;
3543 if (processing_template_decl || processing_specialization)
3544 /* We have not yet called push_template_decl to turn a
3545 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
3546 match. But, we'll check later, when we construct the
3547 template. */
3548 return;
3549 /* Instantiations or specializations of templates may be declared as
3550 friends in any namespace. */
3551 if (friendp && DECL_USE_TEMPLATE (decl))
3552 return;
3553 if (is_overloaded_fn (old))
3555 tree found = NULL_TREE;
3556 tree elt = old;
3557 for (; elt; elt = OVL_NEXT (elt))
3559 tree ofn = OVL_CURRENT (elt);
3560 /* Adjust DECL_CONTEXT first so decls_match will return true
3561 if DECL will match a declaration in an inline namespace. */
3562 DECL_CONTEXT (decl) = DECL_CONTEXT (ofn);
3563 if (decls_match (decl, ofn))
3565 if (found && !decls_match (found, ofn))
3567 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3568 error ("reference to %qD is ambiguous", decl);
3569 print_candidates (old);
3570 return;
3572 found = ofn;
3575 if (found)
3577 if (!is_associated_namespace (scope, CP_DECL_CONTEXT (found)))
3578 goto complain;
3579 DECL_CONTEXT (decl) = DECL_CONTEXT (found);
3580 return;
3583 else
3585 DECL_CONTEXT (decl) = DECL_CONTEXT (old);
3586 if (decls_match (decl, old))
3587 return;
3590 /* It didn't work, go back to the explicit scope. */
3591 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3592 complain:
3593 error ("%qD should have been declared inside %qD", decl, scope);
3596 /* Return the namespace where the current declaration is declared. */
3598 tree
3599 current_decl_namespace (void)
3601 tree result;
3602 /* If we have been pushed into a different namespace, use it. */
3603 if (!vec_safe_is_empty (decl_namespace_list))
3604 return decl_namespace_list->last ();
3606 if (current_class_type)
3607 result = decl_namespace_context (current_class_type);
3608 else if (current_function_decl)
3609 result = decl_namespace_context (current_function_decl);
3610 else
3611 result = current_namespace;
3612 return result;
3615 /* Process any ATTRIBUTES on a namespace definition. Returns true if
3616 attribute visibility is seen. */
3618 bool
3619 handle_namespace_attrs (tree ns, tree attributes)
3621 tree d;
3622 bool saw_vis = false;
3624 for (d = attributes; d; d = TREE_CHAIN (d))
3626 tree name = get_attribute_name (d);
3627 tree args = TREE_VALUE (d);
3629 if (is_attribute_p ("visibility", name))
3631 /* attribute visibility is a property of the syntactic block
3632 rather than the namespace as a whole, so we don't touch the
3633 NAMESPACE_DECL at all. */
3634 tree x = args ? TREE_VALUE (args) : NULL_TREE;
3635 if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args))
3637 warning (OPT_Wattributes,
3638 "%qD attribute requires a single NTBS argument",
3639 name);
3640 continue;
3643 if (!TREE_PUBLIC (ns))
3644 warning (OPT_Wattributes,
3645 "%qD attribute is meaningless since members of the "
3646 "anonymous namespace get local symbols", name);
3648 push_visibility (TREE_STRING_POINTER (x), 1);
3649 saw_vis = true;
3651 else if (is_attribute_p ("abi_tag", name))
3653 if (!DECL_NAMESPACE_ASSOCIATIONS (ns))
3655 warning (OPT_Wattributes, "ignoring %qD attribute on non-inline "
3656 "namespace", name);
3657 continue;
3659 if (!DECL_NAME (ns))
3661 warning (OPT_Wattributes, "ignoring %qD attribute on anonymous "
3662 "namespace", name);
3663 continue;
3665 if (!args)
3667 tree dn = DECL_NAME (ns);
3668 args = build_string (IDENTIFIER_LENGTH (dn) + 1,
3669 IDENTIFIER_POINTER (dn));
3670 TREE_TYPE (args) = char_array_type_node;
3671 args = fix_string_type (args);
3672 args = build_tree_list (NULL_TREE, args);
3674 if (check_abi_tag_args (args, name))
3675 DECL_ATTRIBUTES (ns) = tree_cons (name, args,
3676 DECL_ATTRIBUTES (ns));
3678 else
3680 warning (OPT_Wattributes, "%qD attribute directive ignored",
3681 name);
3682 continue;
3686 return saw_vis;
3689 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we
3690 select a name that is unique to this compilation unit. */
3692 void
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 pushdecl (d);
3767 if (anon)
3769 /* Clear DECL_NAME for the benefit of debugging back ends. */
3770 SET_DECL_ASSEMBLER_NAME (d, name);
3771 DECL_NAME (d) = NULL_TREE;
3773 begin_scope (sk_namespace, d);
3775 else
3776 resume_scope (NAMESPACE_LEVEL (d));
3778 if (implicit_use)
3779 do_using_directive (d);
3780 /* Enter the name space. */
3781 current_namespace = d;
3783 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3786 /* Pop from the scope of the current namespace. */
3788 void
3789 pop_namespace (void)
3791 gcc_assert (current_namespace != global_namespace);
3792 current_namespace = CP_DECL_CONTEXT (current_namespace);
3793 /* The binding level is not popped, as it might be re-opened later. */
3794 leave_scope ();
3797 /* Push into the scope of the namespace NS, even if it is deeply
3798 nested within another namespace. */
3800 void
3801 push_nested_namespace (tree ns)
3803 if (ns == global_namespace)
3804 push_to_top_level ();
3805 else
3807 push_nested_namespace (CP_DECL_CONTEXT (ns));
3808 push_namespace (DECL_NAME (ns));
3812 /* Pop back from the scope of the namespace NS, which was previously
3813 entered with push_nested_namespace. */
3815 void
3816 pop_nested_namespace (tree ns)
3818 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3819 gcc_assert (current_namespace == ns);
3820 while (ns != global_namespace)
3822 pop_namespace ();
3823 ns = CP_DECL_CONTEXT (ns);
3826 pop_from_top_level ();
3827 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3830 /* Temporarily set the namespace for the current declaration. */
3832 void
3833 push_decl_namespace (tree decl)
3835 if (TREE_CODE (decl) != NAMESPACE_DECL)
3836 decl = decl_namespace_context (decl);
3837 vec_safe_push (decl_namespace_list, ORIGINAL_NAMESPACE (decl));
3840 /* [namespace.memdef]/2 */
3842 void
3843 pop_decl_namespace (void)
3845 decl_namespace_list->pop ();
3848 /* Return the namespace that is the common ancestor
3849 of two given namespaces. */
3851 static tree
3852 namespace_ancestor_1 (tree ns1, tree ns2)
3854 tree nsr;
3855 if (is_ancestor (ns1, ns2))
3856 nsr = ns1;
3857 else
3858 nsr = namespace_ancestor_1 (CP_DECL_CONTEXT (ns1), ns2);
3859 return nsr;
3862 /* Wrapper for namespace_ancestor_1. */
3864 static tree
3865 namespace_ancestor (tree ns1, tree ns2)
3867 tree nsr;
3868 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3869 nsr = namespace_ancestor_1 (ns1, ns2);
3870 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3871 return nsr;
3874 /* Process a namespace-alias declaration. */
3876 void
3877 do_namespace_alias (tree alias, tree name_space)
3879 if (name_space == error_mark_node)
3880 return;
3882 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
3884 name_space = ORIGINAL_NAMESPACE (name_space);
3886 /* Build the alias. */
3887 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
3888 DECL_NAMESPACE_ALIAS (alias) = name_space;
3889 DECL_EXTERNAL (alias) = 1;
3890 DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ());
3891 pushdecl (alias);
3893 /* Emit debug info for namespace alias. */
3894 if (!building_stmt_list_p ())
3895 (*debug_hooks->early_global_decl) (alias);
3898 /* Like pushdecl, only it places X in the current namespace,
3899 if appropriate. */
3901 tree
3902 pushdecl_namespace_level (tree x, bool is_friend)
3904 cp_binding_level *b = current_binding_level;
3905 tree t;
3907 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3908 t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace), is_friend);
3910 /* Now, the type_shadowed stack may screw us. Munge it so it does
3911 what we want. */
3912 if (TREE_CODE (t) == TYPE_DECL)
3914 tree name = DECL_NAME (t);
3915 tree newval;
3916 tree *ptr = (tree *)0;
3917 for (; !global_scope_p (b); b = b->level_chain)
3919 tree shadowed = b->type_shadowed;
3920 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
3921 if (TREE_PURPOSE (shadowed) == name)
3923 ptr = &TREE_VALUE (shadowed);
3924 /* Can't break out of the loop here because sometimes
3925 a binding level will have duplicate bindings for
3926 PT names. It's gross, but I haven't time to fix it. */
3929 newval = TREE_TYPE (t);
3930 if (ptr == (tree *)0)
3932 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
3933 up here if this is changed to an assertion. --KR */
3934 SET_IDENTIFIER_TYPE_VALUE (name, t);
3936 else
3938 *ptr = newval;
3941 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3942 return t;
3945 /* Insert USED into the using list of USER. Set INDIRECT_flag if this
3946 directive is not directly from the source. Also find the common
3947 ancestor and let our users know about the new namespace */
3949 static void
3950 add_using_namespace_1 (tree user, tree used, bool indirect)
3952 tree t;
3953 /* Using oneself is a no-op. */
3954 if (user == used)
3955 return;
3956 gcc_assert (TREE_CODE (user) == NAMESPACE_DECL);
3957 gcc_assert (TREE_CODE (used) == NAMESPACE_DECL);
3958 /* Check if we already have this. */
3959 t = purpose_member (used, DECL_NAMESPACE_USING (user));
3960 if (t != NULL_TREE)
3962 if (!indirect)
3963 /* Promote to direct usage. */
3964 TREE_INDIRECT_USING (t) = 0;
3965 return;
3968 /* Add used to the user's using list. */
3969 DECL_NAMESPACE_USING (user)
3970 = tree_cons (used, namespace_ancestor (user, used),
3971 DECL_NAMESPACE_USING (user));
3973 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect;
3975 /* Add user to the used's users list. */
3976 DECL_NAMESPACE_USERS (used)
3977 = tree_cons (user, 0, DECL_NAMESPACE_USERS (used));
3979 /* Recursively add all namespaces used. */
3980 for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t))
3981 /* indirect usage */
3982 add_using_namespace_1 (user, TREE_PURPOSE (t), 1);
3984 /* Tell everyone using us about the new used namespaces. */
3985 for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t))
3986 add_using_namespace_1 (TREE_PURPOSE (t), used, 1);
3989 /* Wrapper for add_using_namespace_1. */
3991 static void
3992 add_using_namespace (tree user, tree used, bool indirect)
3994 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3995 add_using_namespace_1 (user, used, indirect);
3996 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3999 /* Process a using-declaration not appearing in class or local scope. */
4001 void
4002 do_toplevel_using_decl (tree decl, tree scope, tree name)
4004 tree oldval, oldtype, newval, newtype;
4005 tree orig_decl = decl;
4006 cxx_binding *binding;
4008 decl = validate_nonmember_using_decl (decl, scope, name);
4009 if (decl == NULL_TREE)
4010 return;
4012 binding = binding_for_name (NAMESPACE_LEVEL (current_namespace), name);
4014 oldval = binding->value;
4015 oldtype = binding->type;
4017 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
4019 /* Emit debug info. */
4020 if (!processing_template_decl)
4021 cp_emit_debug_info_for_using (orig_decl, current_namespace);
4023 /* Copy declarations found. */
4024 if (newval)
4025 binding->value = newval;
4026 if (newtype)
4027 binding->type = newtype;
4030 /* Process a using-directive. */
4032 void
4033 do_using_directive (tree name_space)
4035 tree context = NULL_TREE;
4037 if (name_space == error_mark_node)
4038 return;
4040 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
4042 if (building_stmt_list_p ())
4043 add_stmt (build_stmt (input_location, USING_STMT, name_space));
4044 name_space = ORIGINAL_NAMESPACE (name_space);
4046 if (!toplevel_bindings_p ())
4048 push_using_directive (name_space);
4050 else
4052 /* direct usage */
4053 add_using_namespace (current_namespace, name_space, 0);
4054 if (current_namespace != global_namespace)
4055 context = current_namespace;
4057 /* Emit debugging info. */
4058 if (!processing_template_decl)
4059 (*debug_hooks->imported_module_or_decl) (name_space, NULL_TREE,
4060 context, false);
4064 /* Deal with a using-directive seen by the parser. Currently we only
4065 handle attributes here, since they cannot appear inside a template. */
4067 void
4068 parse_using_directive (tree name_space, tree attribs)
4070 do_using_directive (name_space);
4072 if (attribs == error_mark_node)
4073 return;
4075 for (tree a = attribs; a; a = TREE_CHAIN (a))
4077 tree name = get_attribute_name (a);
4078 if (is_attribute_p ("strong", name))
4080 if (!toplevel_bindings_p ())
4081 error ("strong using only meaningful at namespace scope");
4082 else if (name_space != error_mark_node)
4084 if (!is_ancestor (current_namespace, name_space))
4085 error ("current namespace %qD does not enclose strongly used namespace %qD",
4086 current_namespace, name_space);
4087 DECL_NAMESPACE_ASSOCIATIONS (name_space)
4088 = tree_cons (current_namespace, 0,
4089 DECL_NAMESPACE_ASSOCIATIONS (name_space));
4092 else
4093 warning (OPT_Wattributes, "%qD attribute directive ignored", name);
4097 /* Like pushdecl, only it places X in the global scope if appropriate.
4098 Calls cp_finish_decl to register the variable, initializing it with
4099 *INIT, if INIT is non-NULL. */
4101 static tree
4102 pushdecl_top_level_1 (tree x, tree *init, bool is_friend)
4104 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4105 push_to_top_level ();
4106 x = pushdecl_namespace_level (x, is_friend);
4107 if (init)
4108 cp_finish_decl (x, *init, false, NULL_TREE, 0);
4109 pop_from_top_level ();
4110 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4111 return x;
4114 /* Like pushdecl, only it places X in the global scope if appropriate. */
4116 tree
4117 pushdecl_top_level (tree x)
4119 return pushdecl_top_level_1 (x, NULL, false);
4122 /* Like pushdecl_top_level, but adding the IS_FRIEND parameter. */
4124 tree
4125 pushdecl_top_level_maybe_friend (tree x, bool is_friend)
4127 return pushdecl_top_level_1 (x, NULL, is_friend);
4130 /* Like pushdecl, only it places X in the global scope if
4131 appropriate. Calls cp_finish_decl to register the variable,
4132 initializing it with INIT. */
4134 tree
4135 pushdecl_top_level_and_finish (tree x, tree init)
4137 return pushdecl_top_level_1 (x, &init, false);
4140 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
4141 duplicates. The first list becomes the tail of the result.
4143 The algorithm is O(n^2). We could get this down to O(n log n) by
4144 doing a sort on the addresses of the functions, if that becomes
4145 necessary. */
4147 static tree
4148 merge_functions (tree s1, tree s2)
4150 for (; s2; s2 = OVL_NEXT (s2))
4152 tree fn2 = OVL_CURRENT (s2);
4153 tree fns1;
4155 for (fns1 = s1; fns1; fns1 = OVL_NEXT (fns1))
4157 tree fn1 = OVL_CURRENT (fns1);
4159 /* If the function from S2 is already in S1, there is no
4160 need to add it again. For `extern "C"' functions, we
4161 might have two FUNCTION_DECLs for the same function, in
4162 different namespaces, but let's leave them in in case
4163 they have different default arguments. */
4164 if (fn1 == fn2)
4165 break;
4168 /* If we exhausted all of the functions in S1, FN2 is new. */
4169 if (!fns1)
4170 s1 = build_overload (fn2, s1);
4172 return s1;
4175 /* Returns TRUE iff OLD and NEW are the same entity.
4177 3 [basic]/3: An entity is a value, object, reference, function,
4178 enumerator, type, class member, template, template specialization,
4179 namespace, parameter pack, or this.
4181 7.3.4 [namespace.udir]/4: If name lookup finds a declaration for a name
4182 in two different namespaces, and the declarations do not declare the
4183 same entity and do not declare functions, the use of the name is
4184 ill-formed. */
4186 static bool
4187 same_entity_p (tree one, tree two)
4189 if (one == two)
4190 return true;
4191 if (!one || !two)
4192 return false;
4193 if (TREE_CODE (one) == TYPE_DECL
4194 && TREE_CODE (two) == TYPE_DECL
4195 && same_type_p (TREE_TYPE (one), TREE_TYPE (two)))
4196 return true;
4197 return false;
4200 /* This should return an error not all definitions define functions.
4201 It is not an error if we find two functions with exactly the
4202 same signature, only if these are selected in overload resolution.
4203 old is the current set of bindings, new_binding the freshly-found binding.
4204 XXX Do we want to give *all* candidates in case of ambiguity?
4205 XXX In what way should I treat extern declarations?
4206 XXX I don't want to repeat the entire duplicate_decls here */
4208 static void
4209 ambiguous_decl (struct scope_binding *old, cxx_binding *new_binding, int flags)
4211 tree val, type;
4212 gcc_assert (old != NULL);
4214 /* Copy the type. */
4215 type = new_binding->type;
4216 if (LOOKUP_NAMESPACES_ONLY (flags)
4217 || (type && hidden_name_p (type) && !(flags & LOOKUP_HIDDEN)))
4218 type = NULL_TREE;
4220 /* Copy the value. */
4221 val = new_binding->value;
4222 if (val)
4224 if (hidden_name_p (val) && !(flags & LOOKUP_HIDDEN))
4225 val = NULL_TREE;
4226 else
4227 switch (TREE_CODE (val))
4229 case TEMPLATE_DECL:
4230 /* If we expect types or namespaces, and not templates,
4231 or this is not a template class. */
4232 if ((LOOKUP_QUALIFIERS_ONLY (flags)
4233 && !DECL_TYPE_TEMPLATE_P (val)))
4234 val = NULL_TREE;
4235 break;
4236 case TYPE_DECL:
4237 if (LOOKUP_NAMESPACES_ONLY (flags)
4238 || (type && (flags & LOOKUP_PREFER_TYPES)))
4239 val = NULL_TREE;
4240 break;
4241 case NAMESPACE_DECL:
4242 if (LOOKUP_TYPES_ONLY (flags))
4243 val = NULL_TREE;
4244 break;
4245 case FUNCTION_DECL:
4246 /* Ignore built-in functions that are still anticipated. */
4247 if (LOOKUP_QUALIFIERS_ONLY (flags))
4248 val = NULL_TREE;
4249 break;
4250 default:
4251 if (LOOKUP_QUALIFIERS_ONLY (flags))
4252 val = NULL_TREE;
4256 /* If val is hidden, shift down any class or enumeration name. */
4257 if (!val)
4259 val = type;
4260 type = NULL_TREE;
4263 if (!old->value)
4264 old->value = val;
4265 else if (val && !same_entity_p (val, old->value))
4267 if (is_overloaded_fn (old->value) && is_overloaded_fn (val))
4268 old->value = merge_functions (old->value, val);
4269 else
4271 old->value = tree_cons (NULL_TREE, old->value,
4272 build_tree_list (NULL_TREE, val));
4273 TREE_TYPE (old->value) = error_mark_node;
4277 if (!old->type)
4278 old->type = type;
4279 else if (type && old->type != type)
4281 old->type = tree_cons (NULL_TREE, old->type,
4282 build_tree_list (NULL_TREE, type));
4283 TREE_TYPE (old->type) = error_mark_node;
4287 /* Return the declarations that are members of the namespace NS. */
4289 tree
4290 cp_namespace_decls (tree ns)
4292 return NAMESPACE_LEVEL (ns)->names;
4295 /* Combine prefer_type and namespaces_only into flags. */
4297 static int
4298 lookup_flags (int prefer_type, int namespaces_only)
4300 if (namespaces_only)
4301 return LOOKUP_PREFER_NAMESPACES;
4302 if (prefer_type > 1)
4303 return LOOKUP_PREFER_TYPES;
4304 if (prefer_type > 0)
4305 return LOOKUP_PREFER_BOTH;
4306 return 0;
4309 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
4310 ignore it or not. Subroutine of lookup_name_real and
4311 lookup_type_scope. */
4313 static bool
4314 qualify_lookup (tree val, int flags)
4316 if (val == NULL_TREE)
4317 return false;
4318 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
4319 return true;
4320 if (flags & LOOKUP_PREFER_TYPES)
4322 tree target_val = strip_using_decl (val);
4323 if (TREE_CODE (target_val) == TYPE_DECL
4324 || TREE_CODE (target_val) == TEMPLATE_DECL)
4325 return true;
4327 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
4328 return false;
4329 /* Look through lambda things that we shouldn't be able to see. */
4330 if (is_lambda_ignored_entity (val))
4331 return false;
4332 return true;
4335 /* Given a lookup that returned VAL, decide if we want to ignore it or
4336 not based on DECL_ANTICIPATED. */
4338 bool
4339 hidden_name_p (tree val)
4341 if (DECL_P (val)
4342 && DECL_LANG_SPECIFIC (val)
4343 && TYPE_FUNCTION_OR_TEMPLATE_DECL_P (val)
4344 && DECL_ANTICIPATED (val))
4345 return true;
4346 return false;
4349 /* Remove any hidden friend functions from a possibly overloaded set
4350 of functions. */
4352 tree
4353 remove_hidden_names (tree fns)
4355 if (!fns)
4356 return fns;
4358 if (TREE_CODE (fns) == FUNCTION_DECL && hidden_name_p (fns))
4359 fns = NULL_TREE;
4360 else if (TREE_CODE (fns) == OVERLOAD)
4362 tree o;
4364 for (o = fns; o; o = OVL_NEXT (o))
4365 if (hidden_name_p (OVL_CURRENT (o)))
4366 break;
4367 if (o)
4369 tree n = NULL_TREE;
4371 for (o = fns; o; o = OVL_NEXT (o))
4372 if (!hidden_name_p (OVL_CURRENT (o)))
4373 n = build_overload (OVL_CURRENT (o), n);
4374 fns = n;
4378 return fns;
4381 /* Suggest alternatives for NAME, an IDENTIFIER_NODE for which name
4382 lookup failed. Search through all available namespaces and print out
4383 possible candidates. */
4385 void
4386 suggest_alternatives_for (location_t location, tree name)
4388 vec<tree> candidates = vNULL;
4389 vec<tree> namespaces_to_search = vNULL;
4390 int max_to_search = PARAM_VALUE (CXX_MAX_NAMESPACES_FOR_DIAGNOSTIC_HELP);
4391 int n_searched = 0;
4392 tree t;
4393 unsigned ix;
4395 namespaces_to_search.safe_push (global_namespace);
4397 while (!namespaces_to_search.is_empty ()
4398 && n_searched < max_to_search)
4400 tree scope = namespaces_to_search.pop ();
4401 struct scope_binding binding = EMPTY_SCOPE_BINDING;
4402 cp_binding_level *level = NAMESPACE_LEVEL (scope);
4404 /* Look in this namespace. */
4405 qualified_lookup_using_namespace (name, scope, &binding, 0);
4407 n_searched++;
4409 if (binding.value)
4410 candidates.safe_push (binding.value);
4412 /* Add child namespaces. */
4413 for (t = level->namespaces; t; t = DECL_CHAIN (t))
4414 namespaces_to_search.safe_push (t);
4417 /* If we stopped before we could examine all namespaces, inform the
4418 user. Do this even if we don't have any candidates, since there
4419 might be more candidates further down that we weren't able to
4420 find. */
4421 if (n_searched >= max_to_search
4422 && !namespaces_to_search.is_empty ())
4423 inform (location,
4424 "maximum limit of %d namespaces searched for %qE",
4425 max_to_search, name);
4427 namespaces_to_search.release ();
4429 /* Nothing useful to report. */
4430 if (candidates.is_empty ())
4431 return;
4433 inform_n (location, candidates.length (),
4434 "suggested alternative:",
4435 "suggested alternatives:");
4437 FOR_EACH_VEC_ELT (candidates, ix, t)
4438 inform (location_of (t), " %qE", t);
4440 candidates.release ();
4443 /* Unscoped lookup of a global: iterate over current namespaces,
4444 considering using-directives. */
4446 static tree
4447 unqualified_namespace_lookup_1 (tree name, int flags)
4449 tree initial = current_decl_namespace ();
4450 tree scope = initial;
4451 tree siter;
4452 cp_binding_level *level;
4453 tree val = NULL_TREE;
4455 for (; !val; scope = CP_DECL_CONTEXT (scope))
4457 struct scope_binding binding = EMPTY_SCOPE_BINDING;
4458 cxx_binding *b =
4459 cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
4461 if (b)
4462 ambiguous_decl (&binding, b, flags);
4464 /* Add all _DECLs seen through local using-directives. */
4465 for (level = current_binding_level;
4466 level->kind != sk_namespace;
4467 level = level->level_chain)
4468 if (!lookup_using_namespace (name, &binding, level->using_directives,
4469 scope, flags))
4470 /* Give up because of error. */
4471 return error_mark_node;
4473 /* Add all _DECLs seen through global using-directives. */
4474 /* XXX local and global using lists should work equally. */
4475 siter = initial;
4476 while (1)
4478 if (!lookup_using_namespace (name, &binding,
4479 DECL_NAMESPACE_USING (siter),
4480 scope, flags))
4481 /* Give up because of error. */
4482 return error_mark_node;
4483 if (siter == scope) break;
4484 siter = CP_DECL_CONTEXT (siter);
4487 val = binding.value;
4488 if (scope == global_namespace)
4489 break;
4491 return val;
4494 /* Wrapper for unqualified_namespace_lookup_1. */
4496 static tree
4497 unqualified_namespace_lookup (tree name, int flags)
4499 tree ret;
4500 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4501 ret = unqualified_namespace_lookup_1 (name, flags);
4502 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4503 return ret;
4506 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
4507 or a class TYPE). If IS_TYPE_P is TRUE, then ignore non-type
4508 bindings.
4510 Returns a DECL (or OVERLOAD, or BASELINK) representing the
4511 declaration found. If no suitable declaration can be found,
4512 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
4513 neither a class-type nor a namespace a diagnostic is issued. */
4515 tree
4516 lookup_qualified_name (tree scope, tree name, bool is_type_p, bool complain)
4518 int flags = 0;
4519 tree t = NULL_TREE;
4521 if (TREE_CODE (scope) == NAMESPACE_DECL)
4523 struct scope_binding binding = EMPTY_SCOPE_BINDING;
4525 if (is_type_p)
4526 flags |= LOOKUP_PREFER_TYPES;
4527 if (qualified_lookup_using_namespace (name, scope, &binding, flags))
4528 t = binding.value;
4530 else if (cxx_dialect != cxx98 && TREE_CODE (scope) == ENUMERAL_TYPE)
4531 t = lookup_enumerator (scope, name);
4532 else if (is_class_type (scope, complain))
4533 t = lookup_member (scope, name, 2, is_type_p, tf_warning_or_error);
4535 if (!t)
4536 return error_mark_node;
4537 return t;
4540 /* Subroutine of unqualified_namespace_lookup:
4541 Add the bindings of NAME in used namespaces to VAL.
4542 We are currently looking for names in namespace SCOPE, so we
4543 look through USINGS for using-directives of namespaces
4544 which have SCOPE as a common ancestor with the current scope.
4545 Returns false on errors. */
4547 static bool
4548 lookup_using_namespace (tree name, struct scope_binding *val,
4549 tree usings, tree scope, int flags)
4551 tree iter;
4552 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4553 /* Iterate over all used namespaces in current, searching for using
4554 directives of scope. */
4555 for (iter = usings; iter; iter = TREE_CHAIN (iter))
4556 if (TREE_VALUE (iter) == scope)
4558 tree used = ORIGINAL_NAMESPACE (TREE_PURPOSE (iter));
4559 cxx_binding *val1 =
4560 cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (used), name);
4561 /* Resolve ambiguities. */
4562 if (val1)
4563 ambiguous_decl (val, val1, flags);
4565 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4566 return val->value != error_mark_node;
4569 /* Returns true iff VEC contains TARGET. */
4571 static bool
4572 tree_vec_contains (vec<tree, va_gc> *vec, tree target)
4574 unsigned int i;
4575 tree elt;
4576 FOR_EACH_VEC_SAFE_ELT (vec,i,elt)
4577 if (elt == target)
4578 return true;
4579 return false;
4582 /* [namespace.qual]
4583 Accepts the NAME to lookup and its qualifying SCOPE.
4584 Returns the name/type pair found into the cxx_binding *RESULT,
4585 or false on error. */
4587 static bool
4588 qualified_lookup_using_namespace (tree name, tree scope,
4589 struct scope_binding *result, int flags)
4591 /* Maintain a list of namespaces visited... */
4592 vec<tree, va_gc> *seen = NULL;
4593 vec<tree, va_gc> *seen_inline = NULL;
4594 /* ... and a list of namespace yet to see. */
4595 vec<tree, va_gc> *todo = NULL;
4596 vec<tree, va_gc> *todo_maybe = NULL;
4597 vec<tree, va_gc> *todo_inline = NULL;
4598 tree usings;
4599 timevar_start (TV_NAME_LOOKUP);
4600 /* Look through namespace aliases. */
4601 scope = ORIGINAL_NAMESPACE (scope);
4603 /* Algorithm: Starting with SCOPE, walk through the set of used
4604 namespaces. For each used namespace, look through its inline
4605 namespace set for any bindings and usings. If no bindings are
4606 found, add any usings seen to the set of used namespaces. */
4607 vec_safe_push (todo, scope);
4609 while (todo->length ())
4611 bool found_here;
4612 scope = todo->pop ();
4613 if (tree_vec_contains (seen, scope))
4614 continue;
4615 vec_safe_push (seen, scope);
4616 vec_safe_push (todo_inline, scope);
4618 found_here = false;
4619 while (todo_inline->length ())
4621 cxx_binding *binding;
4623 scope = todo_inline->pop ();
4624 if (tree_vec_contains (seen_inline, scope))
4625 continue;
4626 vec_safe_push (seen_inline, scope);
4628 binding =
4629 cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
4630 if (binding)
4632 found_here = true;
4633 ambiguous_decl (result, binding, flags);
4636 for (usings = DECL_NAMESPACE_USING (scope); usings;
4637 usings = TREE_CHAIN (usings))
4638 if (!TREE_INDIRECT_USING (usings))
4640 if (is_associated_namespace (scope, TREE_PURPOSE (usings)))
4641 vec_safe_push (todo_inline, TREE_PURPOSE (usings));
4642 else
4643 vec_safe_push (todo_maybe, TREE_PURPOSE (usings));
4647 if (found_here)
4648 vec_safe_truncate (todo_maybe, 0);
4649 else
4650 while (vec_safe_length (todo_maybe))
4651 vec_safe_push (todo, todo_maybe->pop ());
4653 vec_free (todo);
4654 vec_free (todo_maybe);
4655 vec_free (todo_inline);
4656 vec_free (seen);
4657 vec_free (seen_inline);
4658 timevar_stop (TV_NAME_LOOKUP);
4659 return result->value != error_mark_node;
4662 /* Subroutine of outer_binding.
4664 Returns TRUE if BINDING is a binding to a template parameter of
4665 SCOPE. In that case SCOPE is the scope of a primary template
4666 parameter -- in the sense of G++, i.e, a template that has its own
4667 template header.
4669 Returns FALSE otherwise. */
4671 static bool
4672 binding_to_template_parms_of_scope_p (cxx_binding *binding,
4673 cp_binding_level *scope)
4675 tree binding_value, tmpl, tinfo;
4676 int level;
4678 if (!binding || !scope || !scope->this_entity)
4679 return false;
4681 binding_value = binding->value ? binding->value : binding->type;
4682 tinfo = get_template_info (scope->this_entity);
4684 /* BINDING_VALUE must be a template parm. */
4685 if (binding_value == NULL_TREE
4686 || (!DECL_P (binding_value)
4687 || !DECL_TEMPLATE_PARM_P (binding_value)))
4688 return false;
4690 /* The level of BINDING_VALUE. */
4691 level =
4692 template_type_parameter_p (binding_value)
4693 ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX
4694 (TREE_TYPE (binding_value)))
4695 : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value));
4697 /* The template of the current scope, iff said scope is a primary
4698 template. */
4699 tmpl = (tinfo
4700 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
4701 ? TI_TEMPLATE (tinfo)
4702 : NULL_TREE);
4704 /* If the level of the parm BINDING_VALUE equals the depth of TMPL,
4705 then BINDING_VALUE is a parameter of TMPL. */
4706 return (tmpl && level == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
4709 /* Return the innermost non-namespace binding for NAME from a scope
4710 containing BINDING, or, if BINDING is NULL, the current scope.
4711 Please note that for a given template, the template parameters are
4712 considered to be in the scope containing the current scope.
4713 If CLASS_P is false, then class bindings are ignored. */
4715 cxx_binding *
4716 outer_binding (tree name,
4717 cxx_binding *binding,
4718 bool class_p)
4720 cxx_binding *outer;
4721 cp_binding_level *scope;
4722 cp_binding_level *outer_scope;
4724 if (binding)
4726 scope = binding->scope->level_chain;
4727 outer = binding->previous;
4729 else
4731 scope = current_binding_level;
4732 outer = IDENTIFIER_BINDING (name);
4734 outer_scope = outer ? outer->scope : NULL;
4736 /* Because we create class bindings lazily, we might be missing a
4737 class binding for NAME. If there are any class binding levels
4738 between the LAST_BINDING_LEVEL and the scope in which OUTER was
4739 declared, we must lookup NAME in those class scopes. */
4740 if (class_p)
4741 while (scope && scope != outer_scope && scope->kind != sk_namespace)
4743 if (scope->kind == sk_class)
4745 cxx_binding *class_binding;
4747 class_binding = get_class_binding (name, scope);
4748 if (class_binding)
4750 /* Thread this new class-scope binding onto the
4751 IDENTIFIER_BINDING list so that future lookups
4752 find it quickly. */
4753 class_binding->previous = outer;
4754 if (binding)
4755 binding->previous = class_binding;
4756 else
4757 IDENTIFIER_BINDING (name) = class_binding;
4758 return class_binding;
4761 /* If we are in a member template, the template parms of the member
4762 template are considered to be inside the scope of the containing
4763 class, but within G++ the class bindings are all pushed between the
4764 template parms and the function body. So if the outer binding is
4765 a template parm for the current scope, return it now rather than
4766 look for a class binding. */
4767 if (outer_scope && outer_scope->kind == sk_template_parms
4768 && binding_to_template_parms_of_scope_p (outer, scope))
4769 return outer;
4771 scope = scope->level_chain;
4774 return outer;
4777 /* Return the innermost block-scope or class-scope value binding for
4778 NAME, or NULL_TREE if there is no such binding. */
4780 tree
4781 innermost_non_namespace_value (tree name)
4783 cxx_binding *binding;
4784 binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true);
4785 return binding ? binding->value : NULL_TREE;
4788 /* Look up NAME in the current binding level and its superiors in the
4789 namespace of variables, functions and typedefs. Return a ..._DECL
4790 node of some kind representing its definition if there is only one
4791 such declaration, or return a TREE_LIST with all the overloaded
4792 definitions if there are many, or return 0 if it is undefined.
4793 Hidden name, either friend declaration or built-in function, are
4794 not ignored.
4796 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
4797 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
4798 Otherwise we prefer non-TYPE_DECLs.
4800 If NONCLASS is nonzero, bindings in class scopes are ignored. If
4801 BLOCK_P is false, bindings in block scopes are ignored. */
4803 static tree
4804 lookup_name_real_1 (tree name, int prefer_type, int nonclass, bool block_p,
4805 int namespaces_only, int flags)
4807 cxx_binding *iter;
4808 tree val = NULL_TREE;
4810 /* Conversion operators are handled specially because ordinary
4811 unqualified name lookup will not find template conversion
4812 operators. */
4813 if (IDENTIFIER_TYPENAME_P (name))
4815 cp_binding_level *level;
4817 for (level = current_binding_level;
4818 level && level->kind != sk_namespace;
4819 level = level->level_chain)
4821 tree class_type;
4822 tree operators;
4824 /* A conversion operator can only be declared in a class
4825 scope. */
4826 if (level->kind != sk_class)
4827 continue;
4829 /* Lookup the conversion operator in the class. */
4830 class_type = level->this_entity;
4831 operators = lookup_fnfields (class_type, name, /*protect=*/0);
4832 if (operators)
4833 return operators;
4836 return NULL_TREE;
4839 flags |= lookup_flags (prefer_type, namespaces_only);
4841 /* First, look in non-namespace scopes. */
4843 if (current_class_type == NULL_TREE)
4844 nonclass = 1;
4846 if (block_p || !nonclass)
4847 for (iter = outer_binding (name, NULL, !nonclass);
4848 iter;
4849 iter = outer_binding (name, iter, !nonclass))
4851 tree binding;
4853 /* Skip entities we don't want. */
4854 if (LOCAL_BINDING_P (iter) ? !block_p : nonclass)
4855 continue;
4857 /* If this is the kind of thing we're looking for, we're done. */
4858 if (qualify_lookup (iter->value, flags))
4859 binding = iter->value;
4860 else if ((flags & LOOKUP_PREFER_TYPES)
4861 && qualify_lookup (iter->type, flags))
4862 binding = iter->type;
4863 else
4864 binding = NULL_TREE;
4866 if (binding)
4868 if (hidden_name_p (binding))
4870 /* A non namespace-scope binding can only be hidden in the
4871 presence of a local class, due to friend declarations.
4873 In particular, consider:
4875 struct C;
4876 void f() {
4877 struct A {
4878 friend struct B;
4879 friend struct C;
4880 void g() {
4881 B* b; // error: B is hidden
4882 C* c; // OK, finds ::C
4885 B *b; // error: B is hidden
4886 C *c; // OK, finds ::C
4887 struct B {};
4888 B *bb; // OK
4891 The standard says that "B" is a local class in "f"
4892 (but not nested within "A") -- but that name lookup
4893 for "B" does not find this declaration until it is
4894 declared directly with "f".
4896 In particular:
4898 [class.friend]
4900 If a friend declaration appears in a local class and
4901 the name specified is an unqualified name, a prior
4902 declaration is looked up without considering scopes
4903 that are outside the innermost enclosing non-class
4904 scope. For a friend function declaration, if there is
4905 no prior declaration, the program is ill-formed. For a
4906 friend class declaration, if there is no prior
4907 declaration, the class that is specified belongs to the
4908 innermost enclosing non-class scope, but if it is
4909 subsequently referenced, its name is not found by name
4910 lookup until a matching declaration is provided in the
4911 innermost enclosing nonclass scope.
4913 So just keep looking for a non-hidden binding.
4915 gcc_assert (TREE_CODE (binding) == TYPE_DECL);
4916 continue;
4918 val = binding;
4919 break;
4923 /* Now lookup in namespace scopes. */
4924 if (!val)
4925 val = unqualified_namespace_lookup (name, flags);
4927 /* If we have a single function from a using decl, pull it out. */
4928 if (val && TREE_CODE (val) == OVERLOAD && !really_overloaded_fn (val))
4929 val = OVL_FUNCTION (val);
4931 return val;
4934 /* Wrapper for lookup_name_real_1. */
4936 tree
4937 lookup_name_real (tree name, int prefer_type, int nonclass, bool block_p,
4938 int namespaces_only, int flags)
4940 tree ret;
4941 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4942 ret = lookup_name_real_1 (name, prefer_type, nonclass, block_p,
4943 namespaces_only, flags);
4944 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4945 return ret;
4948 tree
4949 lookup_name_nonclass (tree name)
4951 return lookup_name_real (name, 0, 1, /*block_p=*/true, 0, 0);
4954 tree
4955 lookup_function_nonclass (tree name, vec<tree, va_gc> *args, bool block_p)
4957 return
4958 lookup_arg_dependent (name,
4959 lookup_name_real (name, 0, 1, block_p, 0, 0),
4960 args);
4963 tree
4964 lookup_name (tree name)
4966 return lookup_name_real (name, 0, 0, /*block_p=*/true, 0, 0);
4969 tree
4970 lookup_name_prefer_type (tree name, int prefer_type)
4972 return lookup_name_real (name, prefer_type, 0, /*block_p=*/true, 0, 0);
4975 /* Look up NAME for type used in elaborated name specifier in
4976 the scopes given by SCOPE. SCOPE can be either TS_CURRENT or
4977 TS_WITHIN_ENCLOSING_NON_CLASS. Although not implied by the
4978 name, more scopes are checked if cleanup or template parameter
4979 scope is encountered.
4981 Unlike lookup_name_real, we make sure that NAME is actually
4982 declared in the desired scope, not from inheritance, nor using
4983 directive. For using declaration, there is DR138 still waiting
4984 to be resolved. Hidden name coming from an earlier friend
4985 declaration is also returned.
4987 A TYPE_DECL best matching the NAME is returned. Catching error
4988 and issuing diagnostics are caller's responsibility. */
4990 static tree
4991 lookup_type_scope_1 (tree name, tag_scope scope)
4993 cxx_binding *iter = NULL;
4994 tree val = NULL_TREE;
4996 /* Look in non-namespace scope first. */
4997 if (current_binding_level->kind != sk_namespace)
4998 iter = outer_binding (name, NULL, /*class_p=*/ true);
4999 for (; iter; iter = outer_binding (name, iter, /*class_p=*/ true))
5001 /* Check if this is the kind of thing we're looking for.
5002 If SCOPE is TS_CURRENT, also make sure it doesn't come from
5003 base class. For ITER->VALUE, we can simply use
5004 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use
5005 our own check.
5007 We check ITER->TYPE before ITER->VALUE in order to handle
5008 typedef struct C {} C;
5009 correctly. */
5011 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES)
5012 && (scope != ts_current
5013 || LOCAL_BINDING_P (iter)
5014 || DECL_CONTEXT (iter->type) == iter->scope->this_entity))
5015 val = iter->type;
5016 else if ((scope != ts_current
5017 || !INHERITED_VALUE_BINDING_P (iter))
5018 && qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
5019 val = iter->value;
5021 if (val)
5022 break;
5025 /* Look in namespace scope. */
5026 if (!val)
5028 iter = cp_binding_level_find_binding_for_name
5029 (NAMESPACE_LEVEL (current_decl_namespace ()), name);
5031 if (iter)
5033 /* If this is the kind of thing we're looking for, we're done. */
5034 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES))
5035 val = iter->type;
5036 else if (qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
5037 val = iter->value;
5042 /* Type found, check if it is in the allowed scopes, ignoring cleanup
5043 and template parameter scopes. */
5044 if (val)
5046 cp_binding_level *b = current_binding_level;
5047 while (b)
5049 if (iter->scope == b)
5050 return val;
5052 if (b->kind == sk_cleanup || b->kind == sk_template_parms
5053 || b->kind == sk_function_parms)
5054 b = b->level_chain;
5055 else if (b->kind == sk_class
5056 && scope == ts_within_enclosing_non_class)
5057 b = b->level_chain;
5058 else
5059 break;
5063 return NULL_TREE;
5066 /* Wrapper for lookup_type_scope_1. */
5068 tree
5069 lookup_type_scope (tree name, tag_scope scope)
5071 tree ret;
5072 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5073 ret = lookup_type_scope_1 (name, scope);
5074 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5075 return ret;
5079 /* Similar to `lookup_name' but look only in the innermost non-class
5080 binding level. */
5082 static tree
5083 lookup_name_innermost_nonclass_level_1 (tree name)
5085 cp_binding_level *b;
5086 tree t = NULL_TREE;
5088 b = innermost_nonclass_level ();
5090 if (b->kind == sk_namespace)
5092 t = IDENTIFIER_NAMESPACE_VALUE (name);
5094 /* extern "C" function() */
5095 if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
5096 t = TREE_VALUE (t);
5098 else if (IDENTIFIER_BINDING (name)
5099 && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
5101 cxx_binding *binding;
5102 binding = IDENTIFIER_BINDING (name);
5103 while (1)
5105 if (binding->scope == b
5106 && !(VAR_P (binding->value)
5107 && DECL_DEAD_FOR_LOCAL (binding->value)))
5108 return binding->value;
5110 if (b->kind == sk_cleanup)
5111 b = b->level_chain;
5112 else
5113 break;
5117 return t;
5120 /* Wrapper for lookup_name_innermost_nonclass_level_1. */
5122 tree
5123 lookup_name_innermost_nonclass_level (tree name)
5125 tree ret;
5126 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5127 ret = lookup_name_innermost_nonclass_level_1 (name);
5128 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5129 return ret;
5133 /* Returns true iff DECL is a block-scope extern declaration of a function
5134 or variable. */
5136 bool
5137 is_local_extern (tree decl)
5139 cxx_binding *binding;
5141 /* For functions, this is easy. */
5142 if (TREE_CODE (decl) == FUNCTION_DECL)
5143 return DECL_LOCAL_FUNCTION_P (decl);
5145 if (!VAR_P (decl))
5146 return false;
5147 if (!current_function_decl)
5148 return false;
5150 /* For variables, this is not easy. We need to look at the binding stack
5151 for the identifier to see whether the decl we have is a local. */
5152 for (binding = IDENTIFIER_BINDING (DECL_NAME (decl));
5153 binding && binding->scope->kind != sk_namespace;
5154 binding = binding->previous)
5155 if (binding->value == decl)
5156 return LOCAL_BINDING_P (binding);
5158 return false;
5161 /* Like lookup_name_innermost_nonclass_level, but for types. */
5163 static tree
5164 lookup_type_current_level (tree name)
5166 tree t = NULL_TREE;
5168 timevar_start (TV_NAME_LOOKUP);
5169 gcc_assert (current_binding_level->kind != sk_namespace);
5171 if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
5172 && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
5174 cp_binding_level *b = current_binding_level;
5175 while (1)
5177 if (purpose_member (name, b->type_shadowed))
5179 t = REAL_IDENTIFIER_TYPE_VALUE (name);
5180 break;
5182 if (b->kind == sk_cleanup)
5183 b = b->level_chain;
5184 else
5185 break;
5189 timevar_stop (TV_NAME_LOOKUP);
5190 return t;
5193 /* [basic.lookup.koenig] */
5194 /* A nonzero return value in the functions below indicates an error. */
5196 struct arg_lookup
5198 tree name;
5199 vec<tree, va_gc> *args;
5200 vec<tree, va_gc> *namespaces;
5201 vec<tree, va_gc> *classes;
5202 tree functions;
5203 hash_set<tree> *fn_set;
5206 static bool arg_assoc (struct arg_lookup*, tree);
5207 static bool arg_assoc_args (struct arg_lookup*, tree);
5208 static bool arg_assoc_args_vec (struct arg_lookup*, vec<tree, va_gc> *);
5209 static bool arg_assoc_type (struct arg_lookup*, tree);
5210 static bool add_function (struct arg_lookup *, tree);
5211 static bool arg_assoc_namespace (struct arg_lookup *, tree);
5212 static bool arg_assoc_class_only (struct arg_lookup *, tree);
5213 static bool arg_assoc_bases (struct arg_lookup *, tree);
5214 static bool arg_assoc_class (struct arg_lookup *, tree);
5215 static bool arg_assoc_template_arg (struct arg_lookup*, tree);
5217 /* Add a function to the lookup structure.
5218 Returns true on error. */
5220 static bool
5221 add_function (struct arg_lookup *k, tree fn)
5223 if (!is_overloaded_fn (fn))
5224 /* All names except those of (possibly overloaded) functions and
5225 function templates are ignored. */;
5226 else if (k->fn_set && k->fn_set->add (fn))
5227 /* It's already in the list. */;
5228 else if (!k->functions)
5229 k->functions = fn;
5230 else if (fn == k->functions)
5232 else
5234 k->functions = build_overload (fn, k->functions);
5235 if (TREE_CODE (k->functions) == OVERLOAD)
5236 OVL_ARG_DEPENDENT (k->functions) = true;
5239 return false;
5242 /* Returns true iff CURRENT has declared itself to be an associated
5243 namespace of SCOPE via a strong using-directive (or transitive chain
5244 thereof). Both are namespaces. */
5246 bool
5247 is_associated_namespace (tree current, tree scope)
5249 vec<tree, va_gc> *seen = make_tree_vector ();
5250 vec<tree, va_gc> *todo = make_tree_vector ();
5251 tree t;
5252 bool ret;
5254 while (1)
5256 if (scope == current)
5258 ret = true;
5259 break;
5261 vec_safe_push (seen, scope);
5262 for (t = DECL_NAMESPACE_ASSOCIATIONS (scope); t; t = TREE_CHAIN (t))
5263 if (!vec_member (TREE_PURPOSE (t), seen))
5264 vec_safe_push (todo, TREE_PURPOSE (t));
5265 if (!todo->is_empty ())
5267 scope = todo->last ();
5268 todo->pop ();
5270 else
5272 ret = false;
5273 break;
5277 release_tree_vector (seen);
5278 release_tree_vector (todo);
5280 return ret;
5283 /* Add functions of a namespace to the lookup structure.
5284 Returns true on error. */
5286 static bool
5287 arg_assoc_namespace (struct arg_lookup *k, tree scope)
5289 tree value;
5291 if (vec_member (scope, k->namespaces))
5292 return false;
5293 vec_safe_push (k->namespaces, scope);
5295 /* Check out our super-users. */
5296 for (value = DECL_NAMESPACE_ASSOCIATIONS (scope); value;
5297 value = TREE_CHAIN (value))
5298 if (arg_assoc_namespace (k, TREE_PURPOSE (value)))
5299 return true;
5301 /* Also look down into inline namespaces. */
5302 for (value = DECL_NAMESPACE_USING (scope); value;
5303 value = TREE_CHAIN (value))
5304 if (is_associated_namespace (scope, TREE_PURPOSE (value)))
5305 if (arg_assoc_namespace (k, TREE_PURPOSE (value)))
5306 return true;
5308 value = namespace_binding (k->name, scope);
5309 if (!value)
5310 return false;
5312 for (; value; value = OVL_NEXT (value))
5314 /* We don't want to find arbitrary hidden functions via argument
5315 dependent lookup. We only want to find friends of associated
5316 classes, which we'll do via arg_assoc_class. */
5317 if (hidden_name_p (OVL_CURRENT (value)))
5318 continue;
5320 if (add_function (k, OVL_CURRENT (value)))
5321 return true;
5324 return false;
5327 /* Adds everything associated with a template argument to the lookup
5328 structure. Returns true on error. */
5330 static bool
5331 arg_assoc_template_arg (struct arg_lookup *k, tree arg)
5333 /* [basic.lookup.koenig]
5335 If T is a template-id, its associated namespaces and classes are
5336 ... the namespaces and classes associated with the types of the
5337 template arguments provided for template type parameters
5338 (excluding template template parameters); the namespaces in which
5339 any template template arguments are defined; and the classes in
5340 which any member templates used as template template arguments
5341 are defined. [Note: non-type template arguments do not
5342 contribute to the set of associated namespaces. ] */
5344 /* Consider first template template arguments. */
5345 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
5346 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
5347 return false;
5348 else if (TREE_CODE (arg) == TEMPLATE_DECL)
5350 tree ctx = CP_DECL_CONTEXT (arg);
5352 /* It's not a member template. */
5353 if (TREE_CODE (ctx) == NAMESPACE_DECL)
5354 return arg_assoc_namespace (k, ctx);
5355 /* Otherwise, it must be member template. */
5356 else
5357 return arg_assoc_class_only (k, ctx);
5359 /* It's an argument pack; handle it recursively. */
5360 else if (ARGUMENT_PACK_P (arg))
5362 tree args = ARGUMENT_PACK_ARGS (arg);
5363 int i, len = TREE_VEC_LENGTH (args);
5364 for (i = 0; i < len; ++i)
5365 if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, i)))
5366 return true;
5368 return false;
5370 /* It's not a template template argument, but it is a type template
5371 argument. */
5372 else if (TYPE_P (arg))
5373 return arg_assoc_type (k, arg);
5374 /* It's a non-type template argument. */
5375 else
5376 return false;
5379 /* Adds the class and its friends to the lookup structure.
5380 Returns true on error. */
5382 static bool
5383 arg_assoc_class_only (struct arg_lookup *k, tree type)
5385 tree list, friends, context;
5387 /* Backend-built structures, such as __builtin_va_list, aren't
5388 affected by all this. */
5389 if (!CLASS_TYPE_P (type))
5390 return false;
5392 context = decl_namespace_context (type);
5393 if (arg_assoc_namespace (k, context))
5394 return true;
5396 complete_type (type);
5398 /* Process friends. */
5399 for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
5400 list = TREE_CHAIN (list))
5401 if (k->name == FRIEND_NAME (list))
5402 for (friends = FRIEND_DECLS (list); friends;
5403 friends = TREE_CHAIN (friends))
5405 tree fn = TREE_VALUE (friends);
5407 /* Only interested in global functions with potentially hidden
5408 (i.e. unqualified) declarations. */
5409 if (CP_DECL_CONTEXT (fn) != context)
5410 continue;
5411 /* Template specializations are never found by name lookup.
5412 (Templates themselves can be found, but not template
5413 specializations.) */
5414 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn))
5415 continue;
5416 if (add_function (k, fn))
5417 return true;
5420 return false;
5423 /* Adds the class and its bases to the lookup structure.
5424 Returns true on error. */
5426 static bool
5427 arg_assoc_bases (struct arg_lookup *k, tree type)
5429 if (arg_assoc_class_only (k, type))
5430 return true;
5432 if (TYPE_BINFO (type))
5434 /* Process baseclasses. */
5435 tree binfo, base_binfo;
5436 int i;
5438 for (binfo = TYPE_BINFO (type), i = 0;
5439 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5440 if (arg_assoc_bases (k, BINFO_TYPE (base_binfo)))
5441 return true;
5444 return false;
5447 /* Adds everything associated with a class argument type to the lookup
5448 structure. Returns true on error.
5450 If T is a class type (including unions), its associated classes are: the
5451 class itself; the class of which it is a member, if any; and its direct
5452 and indirect base classes. Its associated namespaces are the namespaces
5453 of which its associated classes are members. Furthermore, if T is a
5454 class template specialization, its associated namespaces and classes
5455 also include: the namespaces and classes associated with the types of
5456 the template arguments provided for template type parameters (excluding
5457 template template parameters); the namespaces of which any template
5458 template arguments are members; and the classes of which any member
5459 templates used as template template arguments are members. [ Note:
5460 non-type template arguments do not contribute to the set of associated
5461 namespaces. --end note] */
5463 static bool
5464 arg_assoc_class (struct arg_lookup *k, tree type)
5466 tree list;
5467 int i;
5469 /* Backend build structures, such as __builtin_va_list, aren't
5470 affected by all this. */
5471 if (!CLASS_TYPE_P (type))
5472 return false;
5474 if (vec_member (type, k->classes))
5475 return false;
5476 vec_safe_push (k->classes, type);
5478 if (TYPE_CLASS_SCOPE_P (type)
5479 && arg_assoc_class_only (k, TYPE_CONTEXT (type)))
5480 return true;
5482 if (arg_assoc_bases (k, type))
5483 return true;
5485 /* Process template arguments. */
5486 if (CLASSTYPE_TEMPLATE_INFO (type)
5487 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
5489 list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
5490 for (i = 0; i < TREE_VEC_LENGTH (list); ++i)
5491 if (arg_assoc_template_arg (k, TREE_VEC_ELT (list, i)))
5492 return true;
5495 return false;
5498 /* Adds everything associated with a given type.
5499 Returns 1 on error. */
5501 static bool
5502 arg_assoc_type (struct arg_lookup *k, tree type)
5504 /* As we do not get the type of non-type dependent expressions
5505 right, we can end up with such things without a type. */
5506 if (!type)
5507 return false;
5509 if (TYPE_PTRDATAMEM_P (type))
5511 /* Pointer to member: associate class type and value type. */
5512 if (arg_assoc_type (k, TYPE_PTRMEM_CLASS_TYPE (type)))
5513 return true;
5514 return arg_assoc_type (k, TYPE_PTRMEM_POINTED_TO_TYPE (type));
5516 else switch (TREE_CODE (type))
5518 case ERROR_MARK:
5519 return false;
5520 case VOID_TYPE:
5521 case INTEGER_TYPE:
5522 case REAL_TYPE:
5523 case COMPLEX_TYPE:
5524 case VECTOR_TYPE:
5525 case BOOLEAN_TYPE:
5526 case FIXED_POINT_TYPE:
5527 case DECLTYPE_TYPE:
5528 case NULLPTR_TYPE:
5529 return false;
5530 case RECORD_TYPE:
5531 if (TYPE_PTRMEMFUNC_P (type))
5532 return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type));
5533 case UNION_TYPE:
5534 return arg_assoc_class (k, type);
5535 case POINTER_TYPE:
5536 case REFERENCE_TYPE:
5537 case ARRAY_TYPE:
5538 return arg_assoc_type (k, TREE_TYPE (type));
5539 case ENUMERAL_TYPE:
5540 if (TYPE_CLASS_SCOPE_P (type)
5541 && arg_assoc_class_only (k, TYPE_CONTEXT (type)))
5542 return true;
5543 return arg_assoc_namespace (k, decl_namespace_context (type));
5544 case METHOD_TYPE:
5545 /* The basetype is referenced in the first arg type, so just
5546 fall through. */
5547 case FUNCTION_TYPE:
5548 /* Associate the parameter types. */
5549 if (arg_assoc_args (k, TYPE_ARG_TYPES (type)))
5550 return true;
5551 /* Associate the return type. */
5552 return arg_assoc_type (k, TREE_TYPE (type));
5553 case TEMPLATE_TYPE_PARM:
5554 case BOUND_TEMPLATE_TEMPLATE_PARM:
5555 return false;
5556 case TYPENAME_TYPE:
5557 return false;
5558 case LANG_TYPE:
5559 gcc_assert (type == unknown_type_node
5560 || type == init_list_type_node);
5561 return false;
5562 case TYPE_PACK_EXPANSION:
5563 return arg_assoc_type (k, PACK_EXPANSION_PATTERN (type));
5565 default:
5566 gcc_unreachable ();
5568 return false;
5571 /* Adds everything associated with arguments. Returns true on error. */
5573 static bool
5574 arg_assoc_args (struct arg_lookup *k, tree args)
5576 for (; args; args = TREE_CHAIN (args))
5577 if (arg_assoc (k, TREE_VALUE (args)))
5578 return true;
5579 return false;
5582 /* Adds everything associated with an argument vector. Returns true
5583 on error. */
5585 static bool
5586 arg_assoc_args_vec (struct arg_lookup *k, vec<tree, va_gc> *args)
5588 unsigned int ix;
5589 tree arg;
5591 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
5592 if (arg_assoc (k, arg))
5593 return true;
5594 return false;
5597 /* Adds everything associated with a given tree_node. Returns 1 on error. */
5599 static bool
5600 arg_assoc (struct arg_lookup *k, tree n)
5602 if (n == error_mark_node)
5603 return false;
5605 if (TYPE_P (n))
5606 return arg_assoc_type (k, n);
5608 if (! type_unknown_p (n))
5609 return arg_assoc_type (k, TREE_TYPE (n));
5611 if (TREE_CODE (n) == ADDR_EXPR)
5612 n = TREE_OPERAND (n, 0);
5613 if (TREE_CODE (n) == COMPONENT_REF)
5614 n = TREE_OPERAND (n, 1);
5615 if (TREE_CODE (n) == OFFSET_REF)
5616 n = TREE_OPERAND (n, 1);
5617 while (TREE_CODE (n) == TREE_LIST)
5618 n = TREE_VALUE (n);
5619 if (BASELINK_P (n))
5620 n = BASELINK_FUNCTIONS (n);
5622 if (TREE_CODE (n) == FUNCTION_DECL)
5623 return arg_assoc_type (k, TREE_TYPE (n));
5624 if (TREE_CODE (n) == TEMPLATE_ID_EXPR)
5626 /* The working paper doesn't currently say how to handle template-id
5627 arguments. The sensible thing would seem to be to handle the list
5628 of template candidates like a normal overload set, and handle the
5629 template arguments like we do for class template
5630 specializations. */
5631 tree templ = TREE_OPERAND (n, 0);
5632 tree args = TREE_OPERAND (n, 1);
5633 int ix;
5635 /* First the templates. */
5636 if (arg_assoc (k, templ))
5637 return true;
5639 /* Now the arguments. */
5640 if (args)
5641 for (ix = TREE_VEC_LENGTH (args); ix--;)
5642 if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, ix)) == 1)
5643 return true;
5645 else if (TREE_CODE (n) == OVERLOAD)
5647 for (; n; n = OVL_NEXT (n))
5648 if (arg_assoc_type (k, TREE_TYPE (OVL_CURRENT (n))))
5649 return true;
5652 return false;
5655 /* Performs Koenig lookup depending on arguments, where fns
5656 are the functions found in normal lookup. */
5658 static tree
5659 lookup_arg_dependent_1 (tree name, tree fns, vec<tree, va_gc> *args)
5661 struct arg_lookup k;
5663 /* Remove any hidden friend functions from the list of functions
5664 found so far. They will be added back by arg_assoc_class as
5665 appropriate. */
5666 fns = remove_hidden_names (fns);
5668 k.name = name;
5669 k.args = args;
5670 k.functions = fns;
5671 k.classes = make_tree_vector ();
5673 /* We previously performed an optimization here by setting
5674 NAMESPACES to the current namespace when it was safe. However, DR
5675 164 says that namespaces that were already searched in the first
5676 stage of template processing are searched again (potentially
5677 picking up later definitions) in the second stage. */
5678 k.namespaces = make_tree_vector ();
5680 /* We used to allow duplicates and let joust discard them, but
5681 since the above change for DR 164 we end up with duplicates of
5682 all the functions found by unqualified lookup. So keep track
5683 of which ones we've seen. */
5684 if (fns)
5686 tree ovl;
5687 /* We shouldn't be here if lookup found something other than
5688 namespace-scope functions. */
5689 gcc_assert (DECL_NAMESPACE_SCOPE_P (OVL_CURRENT (fns)));
5690 k.fn_set = new hash_set<tree>;
5691 for (ovl = fns; ovl; ovl = OVL_NEXT (ovl))
5692 k.fn_set->add (OVL_CURRENT (ovl));
5694 else
5695 k.fn_set = NULL;
5697 arg_assoc_args_vec (&k, args);
5699 fns = k.functions;
5701 if (fns
5702 && !VAR_P (fns)
5703 && !is_overloaded_fn (fns))
5705 error ("argument dependent lookup finds %q+D", fns);
5706 error (" in call to %qD", name);
5707 fns = error_mark_node;
5710 release_tree_vector (k.classes);
5711 release_tree_vector (k.namespaces);
5712 delete k.fn_set;
5714 return fns;
5717 /* Wrapper for lookup_arg_dependent_1. */
5719 tree
5720 lookup_arg_dependent (tree name, tree fns, vec<tree, va_gc> *args)
5722 tree ret;
5723 bool subtime;
5724 subtime = timevar_cond_start (TV_NAME_LOOKUP);
5725 ret = lookup_arg_dependent_1 (name, fns, args);
5726 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5727 return ret;
5731 /* Add namespace to using_directives. Return NULL_TREE if nothing was
5732 changed (i.e. there was already a directive), or the fresh
5733 TREE_LIST otherwise. */
5735 static tree
5736 push_using_directive_1 (tree used)
5738 tree ud = current_binding_level->using_directives;
5739 tree iter, ancestor;
5741 /* Check if we already have this. */
5742 if (purpose_member (used, ud) != NULL_TREE)
5743 return NULL_TREE;
5745 ancestor = namespace_ancestor (current_decl_namespace (), used);
5746 ud = current_binding_level->using_directives;
5747 ud = tree_cons (used, ancestor, ud);
5748 current_binding_level->using_directives = ud;
5750 /* Recursively add all namespaces used. */
5751 for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
5752 push_using_directive (TREE_PURPOSE (iter));
5754 return ud;
5757 /* Wrapper for push_using_directive_1. */
5759 static tree
5760 push_using_directive (tree used)
5762 tree ret;
5763 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5764 ret = push_using_directive_1 (used);
5765 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5766 return ret;
5769 /* The type TYPE is being declared. If it is a class template, or a
5770 specialization of a class template, do any processing required and
5771 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
5772 being declared a friend. B is the binding level at which this TYPE
5773 should be bound.
5775 Returns the TYPE_DECL for TYPE, which may have been altered by this
5776 processing. */
5778 static tree
5779 maybe_process_template_type_declaration (tree type, int is_friend,
5780 cp_binding_level *b)
5782 tree decl = TYPE_NAME (type);
5784 if (processing_template_parmlist)
5785 /* You can't declare a new template type in a template parameter
5786 list. But, you can declare a non-template type:
5788 template <class A*> struct S;
5790 is a forward-declaration of `A'. */
5792 else if (b->kind == sk_namespace
5793 && current_binding_level->kind != sk_namespace)
5794 /* If this new type is being injected into a containing scope,
5795 then it's not a template type. */
5797 else
5799 gcc_assert (MAYBE_CLASS_TYPE_P (type)
5800 || TREE_CODE (type) == ENUMERAL_TYPE);
5802 if (processing_template_decl)
5804 /* This may change after the call to
5805 push_template_decl_real, but we want the original value. */
5806 tree name = DECL_NAME (decl);
5808 decl = push_template_decl_real (decl, is_friend);
5809 if (decl == error_mark_node)
5810 return error_mark_node;
5812 /* If the current binding level is the binding level for the
5813 template parameters (see the comment in
5814 begin_template_parm_list) and the enclosing level is a class
5815 scope, and we're not looking at a friend, push the
5816 declaration of the member class into the class scope. In the
5817 friend case, push_template_decl will already have put the
5818 friend into global scope, if appropriate. */
5819 if (TREE_CODE (type) != ENUMERAL_TYPE
5820 && !is_friend && b->kind == sk_template_parms
5821 && b->level_chain->kind == sk_class)
5823 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
5825 if (!COMPLETE_TYPE_P (current_class_type))
5827 maybe_add_class_template_decl_list (current_class_type,
5828 type, /*friend_p=*/0);
5829 /* Put this UTD in the table of UTDs for the class. */
5830 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
5831 CLASSTYPE_NESTED_UTDS (current_class_type) =
5832 binding_table_new (SCOPE_DEFAULT_HT_SIZE);
5834 binding_table_insert
5835 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
5841 return decl;
5844 /* Push a tag name NAME for struct/class/union/enum type TYPE. In case
5845 that the NAME is a class template, the tag is processed but not pushed.
5847 The pushed scope depend on the SCOPE parameter:
5848 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
5849 scope.
5850 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
5851 non-template-parameter scope. This case is needed for forward
5852 declarations.
5853 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
5854 TS_GLOBAL case except that names within template-parameter scopes
5855 are not pushed at all.
5857 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
5859 static tree
5860 pushtag_1 (tree name, tree type, tag_scope scope)
5862 cp_binding_level *b;
5863 tree decl;
5865 b = current_binding_level;
5866 while (/* Cleanup scopes are not scopes from the point of view of
5867 the language. */
5868 b->kind == sk_cleanup
5869 /* Neither are function parameter scopes. */
5870 || b->kind == sk_function_parms
5871 /* Neither are the scopes used to hold template parameters
5872 for an explicit specialization. For an ordinary template
5873 declaration, these scopes are not scopes from the point of
5874 view of the language. */
5875 || (b->kind == sk_template_parms
5876 && (b->explicit_spec_p || scope == ts_global))
5877 || (b->kind == sk_class
5878 && (scope != ts_current
5879 /* We may be defining a new type in the initializer
5880 of a static member variable. We allow this when
5881 not pedantic, and it is particularly useful for
5882 type punning via an anonymous union. */
5883 || COMPLETE_TYPE_P (b->this_entity))))
5884 b = b->level_chain;
5886 gcc_assert (identifier_p (name));
5888 /* Do C++ gratuitous typedefing. */
5889 if (identifier_type_value_1 (name) != type)
5891 tree tdef;
5892 int in_class = 0;
5893 tree context = TYPE_CONTEXT (type);
5895 if (! context)
5897 tree cs = current_scope ();
5899 if (scope == ts_current
5900 || (cs && TREE_CODE (cs) == FUNCTION_DECL))
5901 context = cs;
5902 else if (cs != NULL_TREE && TYPE_P (cs))
5903 /* When declaring a friend class of a local class, we want
5904 to inject the newly named class into the scope
5905 containing the local class, not the namespace
5906 scope. */
5907 context = decl_function_context (get_type_decl (cs));
5909 if (!context)
5910 context = current_namespace;
5912 if (b->kind == sk_class
5913 || (b->kind == sk_template_parms
5914 && b->level_chain->kind == sk_class))
5915 in_class = 1;
5917 if (current_lang_name == lang_name_java)
5918 TYPE_FOR_JAVA (type) = 1;
5920 tdef = create_implicit_typedef (name, type);
5921 DECL_CONTEXT (tdef) = FROB_CONTEXT (context);
5922 if (scope == ts_within_enclosing_non_class)
5924 /* This is a friend. Make this TYPE_DECL node hidden from
5925 ordinary name lookup. Its corresponding TEMPLATE_DECL
5926 will be marked in push_template_decl_real. */
5927 retrofit_lang_decl (tdef);
5928 DECL_ANTICIPATED (tdef) = 1;
5929 DECL_FRIEND_P (tdef) = 1;
5932 decl = maybe_process_template_type_declaration
5933 (type, scope == ts_within_enclosing_non_class, b);
5934 if (decl == error_mark_node)
5935 return decl;
5937 if (b->kind == sk_class)
5939 if (!TYPE_BEING_DEFINED (current_class_type))
5940 return error_mark_node;
5942 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
5943 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
5944 class. But if it's a member template class, we want
5945 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
5946 later. */
5947 finish_member_declaration (decl);
5948 else
5949 pushdecl_class_level (decl);
5951 else if (b->kind != sk_template_parms)
5953 decl = pushdecl_with_scope_1 (decl, b, /*is_friend=*/false);
5954 if (decl == error_mark_node)
5955 return decl;
5958 if (! in_class)
5959 set_identifier_type_value_with_scope (name, tdef, b);
5961 TYPE_CONTEXT (type) = DECL_CONTEXT (decl);
5963 /* If this is a local class, keep track of it. We need this
5964 information for name-mangling, and so that it is possible to
5965 find all function definitions in a translation unit in a
5966 convenient way. (It's otherwise tricky to find a member
5967 function definition it's only pointed to from within a local
5968 class.) */
5969 if (TYPE_FUNCTION_SCOPE_P (type))
5971 if (processing_template_decl)
5973 /* Push a DECL_EXPR so we call pushtag at the right time in
5974 template instantiation rather than in some nested context. */
5975 add_decl_expr (decl);
5977 else
5978 vec_safe_push (local_classes, type);
5981 if (b->kind == sk_class
5982 && !COMPLETE_TYPE_P (current_class_type))
5984 maybe_add_class_template_decl_list (current_class_type,
5985 type, /*friend_p=*/0);
5987 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
5988 CLASSTYPE_NESTED_UTDS (current_class_type)
5989 = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
5991 binding_table_insert
5992 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
5995 decl = TYPE_NAME (type);
5996 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
5998 /* Set type visibility now if this is a forward declaration. */
5999 TREE_PUBLIC (decl) = 1;
6000 determine_visibility (decl);
6002 return type;
6005 /* Wrapper for pushtag_1. */
6007 tree
6008 pushtag (tree name, tree type, tag_scope scope)
6010 tree ret;
6011 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6012 ret = pushtag_1 (name, type, scope);
6013 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6014 return ret;
6017 /* Subroutines for reverting temporarily to top-level for instantiation
6018 of templates and such. We actually need to clear out the class- and
6019 local-value slots of all identifiers, so that only the global values
6020 are at all visible. Simply setting current_binding_level to the global
6021 scope isn't enough, because more binding levels may be pushed. */
6022 struct saved_scope *scope_chain;
6024 /* Return true if ID has not already been marked. */
6026 static inline bool
6027 store_binding_p (tree id)
6029 if (!id || !IDENTIFIER_BINDING (id))
6030 return false;
6032 if (IDENTIFIER_MARKED (id))
6033 return false;
6035 return true;
6038 /* Add an appropriate binding to *OLD_BINDINGS which needs to already
6039 have enough space reserved. */
6041 static void
6042 store_binding (tree id, vec<cxx_saved_binding, va_gc> **old_bindings)
6044 cxx_saved_binding saved;
6046 gcc_checking_assert (store_binding_p (id));
6048 IDENTIFIER_MARKED (id) = 1;
6050 saved.identifier = id;
6051 saved.binding = IDENTIFIER_BINDING (id);
6052 saved.real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
6053 (*old_bindings)->quick_push (saved);
6054 IDENTIFIER_BINDING (id) = NULL;
6057 static void
6058 store_bindings (tree names, vec<cxx_saved_binding, va_gc> **old_bindings)
6060 static vec<tree> bindings_need_stored = vNULL;
6061 tree t, id;
6062 size_t i;
6064 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6065 for (t = names; t; t = TREE_CHAIN (t))
6067 if (TREE_CODE (t) == TREE_LIST)
6068 id = TREE_PURPOSE (t);
6069 else
6070 id = DECL_NAME (t);
6072 if (store_binding_p (id))
6073 bindings_need_stored.safe_push (id);
6075 if (!bindings_need_stored.is_empty ())
6077 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
6078 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
6080 /* We can appearantly have duplicates in NAMES. */
6081 if (store_binding_p (id))
6082 store_binding (id, old_bindings);
6084 bindings_need_stored.truncate (0);
6086 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6089 /* Like store_bindings, but NAMES is a vector of cp_class_binding
6090 objects, rather than a TREE_LIST. */
6092 static void
6093 store_class_bindings (vec<cp_class_binding, va_gc> *names,
6094 vec<cxx_saved_binding, va_gc> **old_bindings)
6096 static vec<tree> bindings_need_stored = vNULL;
6097 size_t i;
6098 cp_class_binding *cb;
6100 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6101 for (i = 0; vec_safe_iterate (names, i, &cb); ++i)
6102 if (store_binding_p (cb->identifier))
6103 bindings_need_stored.safe_push (cb->identifier);
6104 if (!bindings_need_stored.is_empty ())
6106 tree id;
6107 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
6108 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
6109 store_binding (id, old_bindings);
6110 bindings_need_stored.truncate (0);
6112 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6115 void
6116 push_to_top_level (void)
6118 struct saved_scope *s;
6119 cp_binding_level *b;
6120 cxx_saved_binding *sb;
6121 size_t i;
6122 bool need_pop;
6124 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6125 s = ggc_cleared_alloc<saved_scope> ();
6127 b = scope_chain ? current_binding_level : 0;
6129 /* If we're in the middle of some function, save our state. */
6130 if (cfun)
6132 need_pop = true;
6133 push_function_context ();
6135 else
6136 need_pop = false;
6138 if (scope_chain && previous_class_level)
6139 store_class_bindings (previous_class_level->class_shadowed,
6140 &s->old_bindings);
6142 /* Have to include the global scope, because class-scope decls
6143 aren't listed anywhere useful. */
6144 for (; b; b = b->level_chain)
6146 tree t;
6148 /* Template IDs are inserted into the global level. If they were
6149 inserted into namespace level, finish_file wouldn't find them
6150 when doing pending instantiations. Therefore, don't stop at
6151 namespace level, but continue until :: . */
6152 if (global_scope_p (b))
6153 break;
6155 store_bindings (b->names, &s->old_bindings);
6156 /* We also need to check class_shadowed to save class-level type
6157 bindings, since pushclass doesn't fill in b->names. */
6158 if (b->kind == sk_class)
6159 store_class_bindings (b->class_shadowed, &s->old_bindings);
6161 /* Unwind type-value slots back to top level. */
6162 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
6163 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
6166 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, sb)
6167 IDENTIFIER_MARKED (sb->identifier) = 0;
6169 s->prev = scope_chain;
6170 s->bindings = b;
6171 s->need_pop_function_context = need_pop;
6172 s->function_decl = current_function_decl;
6173 s->unevaluated_operand = cp_unevaluated_operand;
6174 s->inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
6175 s->x_stmt_tree.stmts_are_full_exprs_p = true;
6177 scope_chain = s;
6178 current_function_decl = NULL_TREE;
6179 vec_alloc (current_lang_base, 10);
6180 current_lang_name = lang_name_cplusplus;
6181 current_namespace = global_namespace;
6182 push_class_stack ();
6183 cp_unevaluated_operand = 0;
6184 c_inhibit_evaluation_warnings = 0;
6185 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6188 static void
6189 pop_from_top_level_1 (void)
6191 struct saved_scope *s = scope_chain;
6192 cxx_saved_binding *saved;
6193 size_t i;
6195 /* Clear out class-level bindings cache. */
6196 if (previous_class_level)
6197 invalidate_class_lookup_cache ();
6198 pop_class_stack ();
6200 current_lang_base = 0;
6202 scope_chain = s->prev;
6203 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, saved)
6205 tree id = saved->identifier;
6207 IDENTIFIER_BINDING (id) = saved->binding;
6208 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
6211 /* If we were in the middle of compiling a function, restore our
6212 state. */
6213 if (s->need_pop_function_context)
6214 pop_function_context ();
6215 current_function_decl = s->function_decl;
6216 cp_unevaluated_operand = s->unevaluated_operand;
6217 c_inhibit_evaluation_warnings = s->inhibit_evaluation_warnings;
6220 /* Wrapper for pop_from_top_level_1. */
6222 void
6223 pop_from_top_level (void)
6225 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6226 pop_from_top_level_1 ();
6227 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6231 /* Pop off extraneous binding levels left over due to syntax errors.
6233 We don't pop past namespaces, as they might be valid. */
6235 void
6236 pop_everything (void)
6238 if (ENABLE_SCOPE_CHECKING)
6239 verbatim ("XXX entering pop_everything ()\n");
6240 while (!toplevel_bindings_p ())
6242 if (current_binding_level->kind == sk_class)
6243 pop_nested_class ();
6244 else
6245 poplevel (0, 0, 0);
6247 if (ENABLE_SCOPE_CHECKING)
6248 verbatim ("XXX leaving pop_everything ()\n");
6251 /* Emit debugging information for using declarations and directives.
6252 If input tree is overloaded fn then emit debug info for all
6253 candidates. */
6255 void
6256 cp_emit_debug_info_for_using (tree t, tree context)
6258 /* Don't try to emit any debug information if we have errors. */
6259 if (seen_error ())
6260 return;
6262 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
6263 of a builtin function. */
6264 if (TREE_CODE (t) == FUNCTION_DECL
6265 && DECL_EXTERNAL (t)
6266 && DECL_BUILT_IN (t))
6267 return;
6269 /* Do not supply context to imported_module_or_decl, if
6270 it is a global namespace. */
6271 if (context == global_namespace)
6272 context = NULL_TREE;
6274 if (BASELINK_P (t))
6275 t = BASELINK_FUNCTIONS (t);
6277 /* FIXME: Handle TEMPLATE_DECLs. */
6278 for (t = OVL_CURRENT (t); t; t = OVL_NEXT (t))
6279 if (TREE_CODE (t) != TEMPLATE_DECL)
6281 if (building_stmt_list_p ())
6282 add_stmt (build_stmt (input_location, USING_STMT, t));
6283 else
6284 (*debug_hooks->imported_module_or_decl) (t, NULL_TREE, context, false);
6288 #include "gt-cp-name-lookup.h"