2010-07-27 Paolo Carlini <paolo.carlini@oracle.com>
[official-gcc/alias-decl.git] / gcc / cp / name-lookup.c
blobc6e31c2944520bada98e1aad09458c49ccf29478
1 /* Definitions for C++ name lookup routines.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "flags.h"
27 #include "tree.h"
28 #include "cp-tree.h"
29 #include "name-lookup.h"
30 #include "timevar.h"
31 #include "toplev.h"
32 #include "diagnostic-core.h"
33 #include "debug.h"
34 #include "c-family/c-pragma.h"
36 /* The bindings for a particular name in a particular scope. */
38 struct scope_binding {
39 tree value;
40 tree type;
42 #define EMPTY_SCOPE_BINDING { NULL_TREE, NULL_TREE }
44 static cxx_scope *innermost_nonclass_level (void);
45 static cxx_binding *binding_for_name (cxx_scope *, tree);
46 static tree push_overloaded_decl (tree, int, bool);
47 static bool lookup_using_namespace (tree, struct scope_binding *, tree,
48 tree, int);
49 static bool qualified_lookup_using_namespace (tree, tree,
50 struct scope_binding *, int);
51 static tree lookup_type_current_level (tree);
52 static tree push_using_directive (tree);
53 static cxx_binding* lookup_extern_c_fun_binding_in_all_ns (tree);
55 /* The :: namespace. */
57 tree global_namespace;
59 /* The name of the anonymous namespace, throughout this translation
60 unit. */
61 static GTY(()) tree anonymous_namespace_name;
63 /* Initialize anonymous_namespace_name if necessary, and return it. */
65 static tree
66 get_anonymous_namespace_name (void)
68 if (!anonymous_namespace_name)
70 /* The anonymous namespace has to have a unique name
71 if typeinfo objects are being compared by name. */
72 if (! flag_weak || ! SUPPORTS_ONE_ONLY)
73 anonymous_namespace_name = get_file_function_name ("N");
74 else
75 /* The demangler expects anonymous namespaces to be called
76 something starting with '_GLOBAL__N_'. */
77 anonymous_namespace_name = get_identifier ("_GLOBAL__N_1");
79 return anonymous_namespace_name;
82 /* Compute the chain index of a binding_entry given the HASH value of its
83 name and the total COUNT of chains. COUNT is assumed to be a power
84 of 2. */
86 #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
88 /* A free list of "binding_entry"s awaiting for re-use. */
90 static GTY((deletable)) binding_entry free_binding_entry = NULL;
92 /* Create a binding_entry object for (NAME, TYPE). */
94 static inline binding_entry
95 binding_entry_make (tree name, tree type)
97 binding_entry entry;
99 if (free_binding_entry)
101 entry = free_binding_entry;
102 free_binding_entry = entry->chain;
104 else
105 entry = ggc_alloc_binding_entry_s ();
107 entry->name = name;
108 entry->type = type;
109 entry->chain = NULL;
111 return entry;
114 /* Put ENTRY back on the free list. */
115 #if 0
116 static inline void
117 binding_entry_free (binding_entry entry)
119 entry->name = NULL;
120 entry->type = NULL;
121 entry->chain = free_binding_entry;
122 free_binding_entry = entry;
124 #endif
126 /* The datatype used to implement the mapping from names to types at
127 a given scope. */
128 struct GTY(()) binding_table_s {
129 /* Array of chains of "binding_entry"s */
130 binding_entry * GTY((length ("%h.chain_count"))) chain;
132 /* The number of chains in this table. This is the length of the
133 member "chain" considered as an array. */
134 size_t chain_count;
136 /* Number of "binding_entry"s in this table. */
137 size_t entry_count;
140 /* Construct TABLE with an initial CHAIN_COUNT. */
142 static inline void
143 binding_table_construct (binding_table table, size_t chain_count)
145 table->chain_count = chain_count;
146 table->entry_count = 0;
147 table->chain = ggc_alloc_cleared_vec_binding_entry (table->chain_count);
150 /* Make TABLE's entries ready for reuse. */
151 #if 0
152 static void
153 binding_table_free (binding_table table)
155 size_t i;
156 size_t count;
158 if (table == NULL)
159 return;
161 for (i = 0, count = table->chain_count; i < count; ++i)
163 binding_entry temp = table->chain[i];
164 while (temp != NULL)
166 binding_entry entry = temp;
167 temp = entry->chain;
168 binding_entry_free (entry);
170 table->chain[i] = NULL;
172 table->entry_count = 0;
174 #endif
176 /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */
178 static inline binding_table
179 binding_table_new (size_t chain_count)
181 binding_table table = ggc_alloc_binding_table_s ();
182 table->chain = NULL;
183 binding_table_construct (table, chain_count);
184 return table;
187 /* Expand TABLE to twice its current chain_count. */
189 static void
190 binding_table_expand (binding_table table)
192 const size_t old_chain_count = table->chain_count;
193 const size_t old_entry_count = table->entry_count;
194 const size_t new_chain_count = 2 * old_chain_count;
195 binding_entry *old_chains = table->chain;
196 size_t i;
198 binding_table_construct (table, new_chain_count);
199 for (i = 0; i < old_chain_count; ++i)
201 binding_entry entry = old_chains[i];
202 for (; entry != NULL; entry = old_chains[i])
204 const unsigned int hash = IDENTIFIER_HASH_VALUE (entry->name);
205 const size_t j = ENTRY_INDEX (hash, new_chain_count);
207 old_chains[i] = entry->chain;
208 entry->chain = table->chain[j];
209 table->chain[j] = entry;
212 table->entry_count = old_entry_count;
215 /* Insert a binding for NAME to TYPE into TABLE. */
217 static void
218 binding_table_insert (binding_table table, tree name, tree type)
220 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
221 const size_t i = ENTRY_INDEX (hash, table->chain_count);
222 binding_entry entry = binding_entry_make (name, type);
224 entry->chain = table->chain[i];
225 table->chain[i] = entry;
226 ++table->entry_count;
228 if (3 * table->chain_count < 5 * table->entry_count)
229 binding_table_expand (table);
232 /* Return the binding_entry, if any, that maps NAME. */
234 binding_entry
235 binding_table_find (binding_table table, tree name)
237 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
238 binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)];
240 while (entry != NULL && entry->name != name)
241 entry = entry->chain;
243 return entry;
246 /* Apply PROC -- with DATA -- to all entries in TABLE. */
248 void
249 binding_table_foreach (binding_table table, bt_foreach_proc proc, void *data)
251 const size_t chain_count = table->chain_count;
252 size_t i;
254 for (i = 0; i < chain_count; ++i)
256 binding_entry entry = table->chain[i];
257 for (; entry != NULL; entry = entry->chain)
258 proc (entry, data);
262 #ifndef ENABLE_SCOPE_CHECKING
263 # define ENABLE_SCOPE_CHECKING 0
264 #else
265 # define ENABLE_SCOPE_CHECKING 1
266 #endif
268 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
270 static GTY((deletable)) cxx_binding *free_bindings;
272 /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
273 field to NULL. */
275 static inline void
276 cxx_binding_init (cxx_binding *binding, tree value, tree type)
278 binding->value = value;
279 binding->type = type;
280 binding->previous = NULL;
283 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
285 static cxx_binding *
286 cxx_binding_make (tree value, tree type)
288 cxx_binding *binding;
289 if (free_bindings)
291 binding = free_bindings;
292 free_bindings = binding->previous;
294 else
295 binding = ggc_alloc_cxx_binding ();
297 cxx_binding_init (binding, value, type);
299 return binding;
302 /* Put BINDING back on the free list. */
304 static inline void
305 cxx_binding_free (cxx_binding *binding)
307 binding->scope = NULL;
308 binding->previous = free_bindings;
309 free_bindings = binding;
312 /* Create a new binding for NAME (with the indicated VALUE and TYPE
313 bindings) in the class scope indicated by SCOPE. */
315 static cxx_binding *
316 new_class_binding (tree name, tree value, tree type, cxx_scope *scope)
318 cp_class_binding *cb;
319 cxx_binding *binding;
321 if (VEC_length (cp_class_binding, scope->class_shadowed))
323 cp_class_binding *old_base;
324 old_base = VEC_index (cp_class_binding, scope->class_shadowed, 0);
325 if (VEC_reserve (cp_class_binding, gc, scope->class_shadowed, 1))
327 /* Fixup the current bindings, as they might have moved. */
328 size_t i;
330 for (i = 0;
331 VEC_iterate (cp_class_binding, scope->class_shadowed, i, cb);
332 i++)
334 cxx_binding **b;
335 b = &IDENTIFIER_BINDING (cb->identifier);
336 while (*b != &old_base[i].base)
337 b = &((*b)->previous);
338 *b = &cb->base;
341 cb = VEC_quick_push (cp_class_binding, scope->class_shadowed, NULL);
343 else
344 cb = VEC_safe_push (cp_class_binding, gc, scope->class_shadowed, NULL);
346 cb->identifier = name;
347 binding = &cb->base;
348 binding->scope = scope;
349 cxx_binding_init (binding, value, type);
350 return binding;
353 /* Make DECL the innermost binding for ID. The LEVEL is the binding
354 level at which this declaration is being bound. */
356 static void
357 push_binding (tree id, tree decl, cxx_scope* level)
359 cxx_binding *binding;
361 if (level != class_binding_level)
363 binding = cxx_binding_make (decl, NULL_TREE);
364 binding->scope = level;
366 else
367 binding = new_class_binding (id, decl, /*type=*/NULL_TREE, level);
369 /* Now, fill in the binding information. */
370 binding->previous = IDENTIFIER_BINDING (id);
371 INHERITED_VALUE_BINDING_P (binding) = 0;
372 LOCAL_BINDING_P (binding) = (level != class_binding_level);
374 /* And put it on the front of the list of bindings for ID. */
375 IDENTIFIER_BINDING (id) = binding;
378 /* Remove the binding for DECL which should be the innermost binding
379 for ID. */
381 void
382 pop_binding (tree id, tree decl)
384 cxx_binding *binding;
386 if (id == NULL_TREE)
387 /* It's easiest to write the loops that call this function without
388 checking whether or not the entities involved have names. We
389 get here for such an entity. */
390 return;
392 /* Get the innermost binding for ID. */
393 binding = IDENTIFIER_BINDING (id);
395 /* The name should be bound. */
396 gcc_assert (binding != NULL);
398 /* The DECL will be either the ordinary binding or the type
399 binding for this identifier. Remove that binding. */
400 if (binding->value == decl)
401 binding->value = NULL_TREE;
402 else
404 gcc_assert (binding->type == decl);
405 binding->type = NULL_TREE;
408 if (!binding->value && !binding->type)
410 /* We're completely done with the innermost binding for this
411 identifier. Unhook it from the list of bindings. */
412 IDENTIFIER_BINDING (id) = binding->previous;
414 /* Add it to the free list. */
415 cxx_binding_free (binding);
419 /* BINDING records an existing declaration for a name in the current scope.
420 But, DECL is another declaration for that same identifier in the
421 same scope. This is the `struct stat' hack whereby a non-typedef
422 class name or enum-name can be bound at the same level as some other
423 kind of entity.
424 3.3.7/1
426 A class name (9.1) or enumeration name (7.2) can be hidden by the
427 name of an object, function, or enumerator declared in the same scope.
428 If a class or enumeration name and an object, function, or enumerator
429 are declared in the same scope (in any order) with the same name, the
430 class or enumeration name is hidden wherever the object, function, or
431 enumerator name is visible.
433 It's the responsibility of the caller to check that
434 inserting this name is valid here. Returns nonzero if the new binding
435 was successful. */
437 static bool
438 supplement_binding (cxx_binding *binding, tree decl)
440 tree bval = binding->value;
441 bool ok = true;
443 timevar_push (TV_NAME_LOOKUP);
444 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
445 /* The new name is the type name. */
446 binding->type = decl;
447 else if (/* BVAL is null when push_class_level_binding moves an
448 inherited type-binding out of the way to make room for a
449 new value binding. */
450 !bval
451 /* BVAL is error_mark_node when DECL's name has been used
452 in a non-class scope prior declaration. In that case,
453 we should have already issued a diagnostic; for graceful
454 error recovery purpose, pretend this was the intended
455 declaration for that name. */
456 || bval == error_mark_node
457 /* If BVAL is anticipated but has not yet been declared,
458 pretend it is not there at all. */
459 || (TREE_CODE (bval) == FUNCTION_DECL
460 && DECL_ANTICIPATED (bval)
461 && !DECL_HIDDEN_FRIEND_P (bval)))
462 binding->value = decl;
463 else if (TREE_CODE (bval) == TYPE_DECL && DECL_ARTIFICIAL (bval))
465 /* The old binding was a type name. It was placed in
466 VALUE field because it was thought, at the point it was
467 declared, to be the only entity with such a name. Move the
468 type name into the type slot; it is now hidden by the new
469 binding. */
470 binding->type = bval;
471 binding->value = decl;
472 binding->value_is_inherited = false;
474 else if (TREE_CODE (bval) == TYPE_DECL
475 && TREE_CODE (decl) == TYPE_DECL
476 && DECL_NAME (decl) == DECL_NAME (bval)
477 && binding->scope->kind != sk_class
478 && (same_type_p (TREE_TYPE (decl), TREE_TYPE (bval))
479 /* If either type involves template parameters, we must
480 wait until instantiation. */
481 || uses_template_parms (TREE_TYPE (decl))
482 || uses_template_parms (TREE_TYPE (bval))))
483 /* We have two typedef-names, both naming the same type to have
484 the same name. In general, this is OK because of:
486 [dcl.typedef]
488 In a given scope, a typedef specifier can be used to redefine
489 the name of any type declared in that scope to refer to the
490 type to which it already refers.
492 However, in class scopes, this rule does not apply due to the
493 stricter language in [class.mem] prohibiting redeclarations of
494 members. */
495 ok = false;
496 /* There can be two block-scope declarations of the same variable,
497 so long as they are `extern' declarations. However, there cannot
498 be two declarations of the same static data member:
500 [class.mem]
502 A member shall not be declared twice in the
503 member-specification. */
504 else if (TREE_CODE (decl) == VAR_DECL && TREE_CODE (bval) == VAR_DECL
505 && DECL_EXTERNAL (decl) && DECL_EXTERNAL (bval)
506 && !DECL_CLASS_SCOPE_P (decl))
508 duplicate_decls (decl, binding->value, /*newdecl_is_friend=*/false);
509 ok = false;
511 else if (TREE_CODE (decl) == NAMESPACE_DECL
512 && TREE_CODE (bval) == NAMESPACE_DECL
513 && DECL_NAMESPACE_ALIAS (decl)
514 && DECL_NAMESPACE_ALIAS (bval)
515 && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl))
516 /* [namespace.alias]
518 In a declarative region, a namespace-alias-definition can be
519 used to redefine a namespace-alias declared in that declarative
520 region to refer only to the namespace to which it already
521 refers. */
522 ok = false;
523 else
525 error ("declaration of %q#D", decl);
526 error ("conflicts with previous declaration %q+#D", bval);
527 ok = false;
530 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ok);
533 /* Add DECL to the list of things declared in B. */
535 static void
536 add_decl_to_level (tree decl, cxx_scope *b)
538 /* We used to record virtual tables as if they were ordinary
539 variables, but no longer do so. */
540 gcc_assert (!(TREE_CODE (decl) == VAR_DECL && DECL_VIRTUAL_P (decl)));
542 if (TREE_CODE (decl) == NAMESPACE_DECL
543 && !DECL_NAMESPACE_ALIAS (decl))
545 DECL_CHAIN (decl) = b->namespaces;
546 b->namespaces = decl;
548 else
550 /* We build up the list in reverse order, and reverse it later if
551 necessary. */
552 TREE_CHAIN (decl) = b->names;
553 b->names = decl;
554 b->names_size++;
556 /* If appropriate, add decl to separate list of statics. We
557 include extern variables because they might turn out to be
558 static later. It's OK for this list to contain a few false
559 positives. */
560 if (b->kind == sk_namespace)
561 if ((TREE_CODE (decl) == VAR_DECL
562 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
563 || (TREE_CODE (decl) == FUNCTION_DECL
564 && (!TREE_PUBLIC (decl) || DECL_DECLARED_INLINE_P (decl))))
565 VEC_safe_push (tree, gc, b->static_decls, decl);
569 /* Record a decl-node X as belonging to the current lexical scope.
570 Check for errors (such as an incompatible declaration for the same
571 name already seen in the same scope). IS_FRIEND is true if X is
572 declared as a friend.
574 Returns either X or an old decl for the same name.
575 If an old decl is returned, it may have been smashed
576 to agree with what X says. */
578 tree
579 pushdecl_maybe_friend (tree x, bool is_friend)
581 tree t;
582 tree name;
583 int need_new_binding;
585 timevar_push (TV_NAME_LOOKUP);
587 if (x == error_mark_node)
588 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
590 need_new_binding = 1;
592 if (DECL_TEMPLATE_PARM_P (x))
593 /* Template parameters have no context; they are not X::T even
594 when declared within a class or namespace. */
596 else
598 if (current_function_decl && x != current_function_decl
599 /* A local declaration for a function doesn't constitute
600 nesting. */
601 && TREE_CODE (x) != FUNCTION_DECL
602 /* A local declaration for an `extern' variable is in the
603 scope of the current namespace, not the current
604 function. */
605 && !(TREE_CODE (x) == VAR_DECL && DECL_EXTERNAL (x))
606 /* When parsing the parameter list of a function declarator,
607 don't set DECL_CONTEXT to an enclosing function. When we
608 push the PARM_DECLs in order to process the function body,
609 current_binding_level->this_entity will be set. */
610 && !(TREE_CODE (x) == PARM_DECL
611 && current_binding_level->kind == sk_function_parms
612 && current_binding_level->this_entity == NULL)
613 && !DECL_CONTEXT (x))
614 DECL_CONTEXT (x) = current_function_decl;
616 /* If this is the declaration for a namespace-scope function,
617 but the declaration itself is in a local scope, mark the
618 declaration. */
619 if (TREE_CODE (x) == FUNCTION_DECL
620 && DECL_NAMESPACE_SCOPE_P (x)
621 && current_function_decl
622 && x != current_function_decl)
623 DECL_LOCAL_FUNCTION_P (x) = 1;
626 name = DECL_NAME (x);
627 if (name)
629 int different_binding_level = 0;
631 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
632 name = TREE_OPERAND (name, 0);
634 /* In case this decl was explicitly namespace-qualified, look it
635 up in its namespace context. */
636 if (DECL_NAMESPACE_SCOPE_P (x) && namespace_bindings_p ())
637 t = namespace_binding (name, DECL_CONTEXT (x));
638 else
639 t = lookup_name_innermost_nonclass_level (name);
641 /* [basic.link] If there is a visible declaration of an entity
642 with linkage having the same name and type, ignoring entities
643 declared outside the innermost enclosing namespace scope, the
644 block scope declaration declares that same entity and
645 receives the linkage of the previous declaration. */
646 if (! t && current_function_decl && x != current_function_decl
647 && (TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
648 && DECL_EXTERNAL (x))
650 /* Look in block scope. */
651 t = innermost_non_namespace_value (name);
652 /* Or in the innermost namespace. */
653 if (! t)
654 t = namespace_binding (name, DECL_CONTEXT (x));
655 /* Does it have linkage? Note that if this isn't a DECL, it's an
656 OVERLOAD, which is OK. */
657 if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t)))
658 t = NULL_TREE;
659 if (t)
660 different_binding_level = 1;
663 /* If we are declaring a function, and the result of name-lookup
664 was an OVERLOAD, look for an overloaded instance that is
665 actually the same as the function we are declaring. (If
666 there is one, we have to merge our declaration with the
667 previous declaration.) */
668 if (t && TREE_CODE (t) == OVERLOAD)
670 tree match;
672 if (TREE_CODE (x) == FUNCTION_DECL)
673 for (match = t; match; match = OVL_NEXT (match))
675 if (decls_match (OVL_CURRENT (match), x))
676 break;
678 else
679 /* Just choose one. */
680 match = t;
682 if (match)
683 t = OVL_CURRENT (match);
684 else
685 t = NULL_TREE;
688 if (t && t != error_mark_node)
690 if (different_binding_level)
692 if (decls_match (x, t))
693 /* The standard only says that the local extern
694 inherits linkage from the previous decl; in
695 particular, default args are not shared. Add
696 the decl into a hash table to make sure only
697 the previous decl in this case is seen by the
698 middle end. */
700 struct cxx_int_tree_map *h;
701 void **loc;
703 TREE_PUBLIC (x) = TREE_PUBLIC (t);
705 if (cp_function_chain->extern_decl_map == NULL)
706 cp_function_chain->extern_decl_map
707 = htab_create_ggc (20, cxx_int_tree_map_hash,
708 cxx_int_tree_map_eq, NULL);
710 h = ggc_alloc_cxx_int_tree_map ();
711 h->uid = DECL_UID (x);
712 h->to = t;
713 loc = htab_find_slot_with_hash
714 (cp_function_chain->extern_decl_map, h,
715 h->uid, INSERT);
716 *(struct cxx_int_tree_map **) loc = h;
719 else if (TREE_CODE (t) == PARM_DECL)
721 /* Check for duplicate params. */
722 tree d = duplicate_decls (x, t, is_friend);
723 if (d)
724 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, d);
726 else if ((DECL_EXTERN_C_FUNCTION_P (x)
727 || DECL_FUNCTION_TEMPLATE_P (x))
728 && is_overloaded_fn (t))
729 /* Don't do anything just yet. */;
730 else if (t == wchar_decl_node)
732 if (! DECL_IN_SYSTEM_HEADER (x))
733 pedwarn (input_location, OPT_pedantic, "redeclaration of %<wchar_t%> as %qT",
734 TREE_TYPE (x));
736 /* Throw away the redeclaration. */
737 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
739 else
741 tree olddecl = duplicate_decls (x, t, is_friend);
743 /* If the redeclaration failed, we can stop at this
744 point. */
745 if (olddecl == error_mark_node)
746 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
748 if (olddecl)
750 if (TREE_CODE (t) == TYPE_DECL)
751 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
753 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
755 else if (DECL_MAIN_P (x) && TREE_CODE (t) == FUNCTION_DECL)
757 /* A redeclaration of main, but not a duplicate of the
758 previous one.
760 [basic.start.main]
762 This function shall not be overloaded. */
763 error ("invalid redeclaration of %q+D", t);
764 error ("as %qD", x);
765 /* We don't try to push this declaration since that
766 causes a crash. */
767 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
772 /* If x has C linkage-specification, (extern "C"),
773 lookup its binding, in case it's already bound to an object.
774 The lookup is done in all namespaces.
775 If we find an existing binding, make sure it has the same
776 exception specification as x, otherwise, bail in error [7.5, 7.6]. */
777 if ((TREE_CODE (x) == FUNCTION_DECL)
778 && DECL_EXTERN_C_P (x)
779 /* We should ignore declarations happening in system headers. */
780 && !DECL_ARTIFICIAL (x)
781 && !DECL_IN_SYSTEM_HEADER (x))
783 cxx_binding *function_binding =
784 lookup_extern_c_fun_binding_in_all_ns (x);
785 tree previous = (function_binding
786 ? function_binding->value
787 : NULL_TREE);
788 if (previous
789 && !DECL_ARTIFICIAL (previous)
790 && !DECL_IN_SYSTEM_HEADER (previous)
791 && DECL_CONTEXT (previous) != DECL_CONTEXT (x))
793 tree previous = function_binding->value;
795 /* In case either x or previous is declared to throw an exception,
796 make sure both exception specifications are equal. */
797 if (decls_match (x, previous))
799 tree x_exception_spec = NULL_TREE;
800 tree previous_exception_spec = NULL_TREE;
802 x_exception_spec =
803 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (x));
804 previous_exception_spec =
805 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (previous));
806 if (!comp_except_specs (previous_exception_spec,
807 x_exception_spec,
808 ce_normal))
810 pedwarn (input_location, 0, "declaration of %q#D with C language linkage",
812 pedwarn (input_location, 0, "conflicts with previous declaration %q+#D",
813 previous);
814 pedwarn (input_location, 0, "due to different exception specifications");
815 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
818 else
820 pedwarn (input_location, 0,
821 "declaration of %q#D with C language linkage", x);
822 pedwarn (input_location, 0,
823 "conflicts with previous declaration %q+#D",
824 previous);
829 check_template_shadow (x);
831 /* If this is a function conjured up by the back end, massage it
832 so it looks friendly. */
833 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x))
835 retrofit_lang_decl (x);
836 SET_DECL_LANGUAGE (x, lang_c);
839 t = x;
840 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x))
842 t = push_overloaded_decl (x, PUSH_LOCAL, is_friend);
843 if (!namespace_bindings_p ())
844 /* We do not need to create a binding for this name;
845 push_overloaded_decl will have already done so if
846 necessary. */
847 need_new_binding = 0;
849 else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
851 t = push_overloaded_decl (x, PUSH_GLOBAL, is_friend);
852 if (t == x)
853 add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
856 if (TREE_CODE (t) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (t))
857 check_default_args (t);
859 if (t != x || DECL_FUNCTION_TEMPLATE_P (t))
860 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
862 /* If declaring a type as a typedef, copy the type (unless we're
863 at line 0), and install this TYPE_DECL as the new type's typedef
864 name. See the extensive comment of set_underlying_type (). */
865 if (TREE_CODE (x) == TYPE_DECL)
867 tree type = TREE_TYPE (x);
869 if (DECL_IS_BUILTIN (x)
870 || (TREE_TYPE (x) != error_mark_node
871 && TYPE_NAME (type) != x
872 /* We don't want to copy the type when all we're
873 doing is making a TYPE_DECL for the purposes of
874 inlining. */
875 && (!TYPE_NAME (type)
876 || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x))))
877 cp_set_underlying_type (x);
879 if (type != error_mark_node
880 && TYPE_NAME (type)
881 && TYPE_IDENTIFIER (type))
882 set_identifier_type_value (DECL_NAME (x), x);
885 /* Multiple external decls of the same identifier ought to match.
887 We get warnings about inline functions where they are defined.
888 We get warnings about other functions from push_overloaded_decl.
890 Avoid duplicate warnings where they are used. */
891 if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
893 tree decl;
895 decl = IDENTIFIER_NAMESPACE_VALUE (name);
896 if (decl && TREE_CODE (decl) == OVERLOAD)
897 decl = OVL_FUNCTION (decl);
899 if (decl && decl != error_mark_node
900 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl))
901 /* If different sort of thing, we already gave an error. */
902 && TREE_CODE (decl) == TREE_CODE (x)
903 && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl)))
905 permerror (input_location, "type mismatch with previous external decl of %q#D", x);
906 permerror (input_location, "previous external decl of %q+#D", decl);
910 if (TREE_CODE (x) == FUNCTION_DECL
911 && is_friend
912 && !flag_friend_injection)
914 /* This is a new declaration of a friend function, so hide
915 it from ordinary function lookup. */
916 DECL_ANTICIPATED (x) = 1;
917 DECL_HIDDEN_FRIEND_P (x) = 1;
920 /* This name is new in its binding level.
921 Install the new declaration and return it. */
922 if (namespace_bindings_p ())
924 /* Install a global value. */
926 /* If the first global decl has external linkage,
927 warn if we later see static one. */
928 if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
929 TREE_PUBLIC (name) = 1;
931 /* Bind the name for the entity. */
932 if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
933 && t != NULL_TREE)
934 && (TREE_CODE (x) == TYPE_DECL
935 || TREE_CODE (x) == VAR_DECL
936 || TREE_CODE (x) == NAMESPACE_DECL
937 || TREE_CODE (x) == CONST_DECL
938 || TREE_CODE (x) == TEMPLATE_DECL))
939 SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
941 /* If new decl is `static' and an `extern' was seen previously,
942 warn about it. */
943 if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
944 warn_extern_redeclared_static (x, t);
946 else
948 /* Here to install a non-global value. */
949 tree oldlocal = innermost_non_namespace_value (name);
950 tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
952 if (need_new_binding)
954 push_local_binding (name, x, 0);
955 /* Because push_local_binding will hook X on to the
956 current_binding_level's name list, we don't want to
957 do that again below. */
958 need_new_binding = 0;
961 /* If this is a TYPE_DECL, push it into the type value slot. */
962 if (TREE_CODE (x) == TYPE_DECL)
963 set_identifier_type_value (name, x);
965 /* Clear out any TYPE_DECL shadowed by a namespace so that
966 we won't think this is a type. The C struct hack doesn't
967 go through namespaces. */
968 if (TREE_CODE (x) == NAMESPACE_DECL)
969 set_identifier_type_value (name, NULL_TREE);
971 if (oldlocal)
973 tree d = oldlocal;
975 while (oldlocal
976 && TREE_CODE (oldlocal) == VAR_DECL
977 && DECL_DEAD_FOR_LOCAL (oldlocal))
978 oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal);
980 if (oldlocal == NULL_TREE)
981 oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d));
984 /* If this is an extern function declaration, see if we
985 have a global definition or declaration for the function. */
986 if (oldlocal == NULL_TREE
987 && DECL_EXTERNAL (x)
988 && oldglobal != NULL_TREE
989 && TREE_CODE (x) == FUNCTION_DECL
990 && TREE_CODE (oldglobal) == FUNCTION_DECL)
992 /* We have one. Their types must agree. */
993 if (decls_match (x, oldglobal))
994 /* OK */;
995 else
997 warning (0, "extern declaration of %q#D doesn't match", x);
998 warning (0, "global declaration %q+#D", oldglobal);
1001 /* If we have a local external declaration,
1002 and no file-scope declaration has yet been seen,
1003 then if we later have a file-scope decl it must not be static. */
1004 if (oldlocal == NULL_TREE
1005 && oldglobal == NULL_TREE
1006 && DECL_EXTERNAL (x)
1007 && TREE_PUBLIC (x))
1008 TREE_PUBLIC (name) = 1;
1010 /* Don't complain about the parms we push and then pop
1011 while tentatively parsing a function declarator. */
1012 if (TREE_CODE (x) == PARM_DECL && DECL_CONTEXT (x) == NULL_TREE)
1013 /* Ignore. */;
1015 /* Warn if shadowing an argument at the top level of the body. */
1016 else if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
1017 /* Inline decls shadow nothing. */
1018 && !DECL_FROM_INLINE (x)
1019 && (TREE_CODE (oldlocal) == PARM_DECL
1020 || TREE_CODE (oldlocal) == VAR_DECL
1021 /* If the old decl is a type decl, only warn if the
1022 old decl is an explicit typedef or if both the old
1023 and new decls are type decls. */
1024 || (TREE_CODE (oldlocal) == TYPE_DECL
1025 && (!DECL_ARTIFICIAL (oldlocal)
1026 || TREE_CODE (x) == TYPE_DECL)))
1027 /* Don't check the `this' parameter or internally generated
1028 vars unless it's an implicit typedef (see
1029 create_implicit_typedef in decl.c). */
1030 && (!DECL_ARTIFICIAL (oldlocal)
1031 || DECL_IMPLICIT_TYPEDEF_P (oldlocal))
1032 /* Don't check for internally generated vars unless
1033 it's an implicit typedef (see create_implicit_typedef
1034 in decl.c). */
1035 && (!DECL_ARTIFICIAL (x) || DECL_IMPLICIT_TYPEDEF_P (x)))
1037 bool nowarn = false;
1039 /* Don't complain if it's from an enclosing function. */
1040 if (DECL_CONTEXT (oldlocal) == current_function_decl
1041 && TREE_CODE (x) != PARM_DECL
1042 && TREE_CODE (oldlocal) == PARM_DECL)
1044 /* Go to where the parms should be and see if we find
1045 them there. */
1046 struct cp_binding_level *b = current_binding_level->level_chain;
1048 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
1049 /* Skip the ctor/dtor cleanup level. */
1050 b = b->level_chain;
1052 /* ARM $8.3 */
1053 if (b->kind == sk_function_parms)
1055 error ("declaration of %q#D shadows a parameter", x);
1056 nowarn = true;
1060 /* The local structure or class can't use parameters of
1061 the containing function anyway. */
1062 if (DECL_CONTEXT (oldlocal) != current_function_decl)
1064 cxx_scope *scope = current_binding_level;
1065 tree context = DECL_CONTEXT (oldlocal);
1066 for (; scope; scope = scope->level_chain)
1068 if (scope->kind == sk_function_parms
1069 && scope->this_entity == context)
1070 break;
1071 if (scope->kind == sk_class
1072 && !LAMBDA_TYPE_P (scope->this_entity))
1074 nowarn = true;
1075 break;
1080 if (warn_shadow && !nowarn)
1082 if (TREE_CODE (oldlocal) == PARM_DECL)
1083 warning_at (input_location, OPT_Wshadow,
1084 "declaration of %q#D shadows a parameter", x);
1085 else
1086 warning_at (input_location, OPT_Wshadow,
1087 "declaration of %qD shadows a previous local",
1089 warning_at (DECL_SOURCE_LOCATION (oldlocal), OPT_Wshadow,
1090 "shadowed declaration is here");
1094 /* Maybe warn if shadowing something else. */
1095 else if (warn_shadow && !DECL_EXTERNAL (x)
1096 /* No shadow warnings for internally generated vars unless
1097 it's an implicit typedef (see create_implicit_typedef
1098 in decl.c). */
1099 && (! DECL_ARTIFICIAL (x) || DECL_IMPLICIT_TYPEDEF_P (x))
1100 /* No shadow warnings for vars made for inlining. */
1101 && ! DECL_FROM_INLINE (x))
1103 tree member;
1105 if (current_class_ptr)
1106 member = lookup_member (current_class_type,
1107 name,
1108 /*protect=*/0,
1109 /*want_type=*/false);
1110 else
1111 member = NULL_TREE;
1113 if (member && !TREE_STATIC (member))
1115 /* Location of previous decl is not useful in this case. */
1116 warning (OPT_Wshadow, "declaration of %qD shadows a member of 'this'",
1119 else if (oldglobal != NULL_TREE
1120 && (TREE_CODE (oldglobal) == VAR_DECL
1121 /* If the old decl is a type decl, only warn if the
1122 old decl is an explicit typedef or if both the
1123 old and new decls are type decls. */
1124 || (TREE_CODE (oldglobal) == TYPE_DECL
1125 && (!DECL_ARTIFICIAL (oldglobal)
1126 || TREE_CODE (x) == TYPE_DECL))))
1127 /* XXX shadow warnings in outer-more namespaces */
1129 warning_at (input_location, OPT_Wshadow,
1130 "declaration of %qD shadows a global declaration", x);
1131 warning_at (DECL_SOURCE_LOCATION (oldglobal), OPT_Wshadow,
1132 "shadowed declaration is here");
1137 if (TREE_CODE (x) == VAR_DECL)
1138 maybe_register_incomplete_var (x);
1141 if (need_new_binding)
1142 add_decl_to_level (x,
1143 DECL_NAMESPACE_SCOPE_P (x)
1144 ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x))
1145 : current_binding_level);
1147 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
1150 /* Record a decl-node X as belonging to the current lexical scope. */
1152 tree
1153 pushdecl (tree x)
1155 return pushdecl_maybe_friend (x, false);
1158 /* Enter DECL into the symbol table, if that's appropriate. Returns
1159 DECL, or a modified version thereof. */
1161 tree
1162 maybe_push_decl (tree decl)
1164 tree type = TREE_TYPE (decl);
1166 /* Add this decl to the current binding level, but not if it comes
1167 from another scope, e.g. a static member variable. TEM may equal
1168 DECL or it may be a previous decl of the same name. */
1169 if (decl == error_mark_node
1170 || (TREE_CODE (decl) != PARM_DECL
1171 && DECL_CONTEXT (decl) != NULL_TREE
1172 /* Definitions of namespace members outside their namespace are
1173 possible. */
1174 && TREE_CODE (DECL_CONTEXT (decl)) != NAMESPACE_DECL)
1175 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
1176 || type == unknown_type_node
1177 /* The declaration of a template specialization does not affect
1178 the functions available for overload resolution, so we do not
1179 call pushdecl. */
1180 || (TREE_CODE (decl) == FUNCTION_DECL
1181 && DECL_TEMPLATE_SPECIALIZATION (decl)))
1182 return decl;
1183 else
1184 return pushdecl (decl);
1187 /* Bind DECL to ID in the current_binding_level, assumed to be a local
1188 binding level. If PUSH_USING is set in FLAGS, we know that DECL
1189 doesn't really belong to this binding level, that it got here
1190 through a using-declaration. */
1192 void
1193 push_local_binding (tree id, tree decl, int flags)
1195 struct cp_binding_level *b;
1197 /* Skip over any local classes. This makes sense if we call
1198 push_local_binding with a friend decl of a local class. */
1199 b = innermost_nonclass_level ();
1201 if (lookup_name_innermost_nonclass_level (id))
1203 /* Supplement the existing binding. */
1204 if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
1205 /* It didn't work. Something else must be bound at this
1206 level. Do not add DECL to the list of things to pop
1207 later. */
1208 return;
1210 else
1211 /* Create a new binding. */
1212 push_binding (id, decl, b);
1214 if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
1215 /* We must put the OVERLOAD into a TREE_LIST since the
1216 TREE_CHAIN of an OVERLOAD is already used. Similarly for
1217 decls that got here through a using-declaration. */
1218 decl = build_tree_list (NULL_TREE, decl);
1220 /* And put DECL on the list of things declared by the current
1221 binding level. */
1222 add_decl_to_level (decl, b);
1225 /* Check to see whether or not DECL is a variable that would have been
1226 in scope under the ARM, but is not in scope under the ANSI/ISO
1227 standard. If so, issue an error message. If name lookup would
1228 work in both cases, but return a different result, this function
1229 returns the result of ANSI/ISO lookup. Otherwise, it returns
1230 DECL. */
1232 tree
1233 check_for_out_of_scope_variable (tree decl)
1235 tree shadowed;
1237 /* We only care about out of scope variables. */
1238 if (!(TREE_CODE (decl) == VAR_DECL && DECL_DEAD_FOR_LOCAL (decl)))
1239 return decl;
1241 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (decl)
1242 ? DECL_SHADOWED_FOR_VAR (decl) : NULL_TREE ;
1243 while (shadowed != NULL_TREE && TREE_CODE (shadowed) == VAR_DECL
1244 && DECL_DEAD_FOR_LOCAL (shadowed))
1245 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (shadowed)
1246 ? DECL_SHADOWED_FOR_VAR (shadowed) : NULL_TREE;
1247 if (!shadowed)
1248 shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl));
1249 if (shadowed)
1251 if (!DECL_ERROR_REPORTED (decl))
1253 warning (0, "name lookup of %qD changed", DECL_NAME (decl));
1254 warning (0, " matches this %q+D under ISO standard rules",
1255 shadowed);
1256 warning (0, " matches this %q+D under old rules", decl);
1257 DECL_ERROR_REPORTED (decl) = 1;
1259 return shadowed;
1262 /* If we have already complained about this declaration, there's no
1263 need to do it again. */
1264 if (DECL_ERROR_REPORTED (decl))
1265 return decl;
1267 DECL_ERROR_REPORTED (decl) = 1;
1269 if (TREE_TYPE (decl) == error_mark_node)
1270 return decl;
1272 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
1274 error ("name lookup of %qD changed for ISO %<for%> scoping",
1275 DECL_NAME (decl));
1276 error (" cannot use obsolete binding at %q+D because "
1277 "it has a destructor", decl);
1278 return error_mark_node;
1280 else
1282 permerror (input_location, "name lookup of %qD changed for ISO %<for%> scoping",
1283 DECL_NAME (decl));
1284 if (flag_permissive)
1285 permerror (input_location, " using obsolete binding at %q+D", decl);
1286 else
1288 static bool hint;
1289 if (!hint)
1291 inform (input_location, "(if you use %<-fpermissive%> G++ will accept your code)");
1292 hint = true;
1297 return decl;
1300 /* true means unconditionally make a BLOCK for the next level pushed. */
1302 static bool keep_next_level_flag;
1304 static int binding_depth = 0;
1306 static void
1307 indent (int depth)
1309 int i;
1311 for (i = 0; i < depth * 2; i++)
1312 putc (' ', stderr);
1315 /* Return a string describing the kind of SCOPE we have. */
1316 static const char *
1317 cxx_scope_descriptor (cxx_scope *scope)
1319 /* The order of this table must match the "scope_kind"
1320 enumerators. */
1321 static const char* scope_kind_names[] = {
1322 "block-scope",
1323 "cleanup-scope",
1324 "try-scope",
1325 "catch-scope",
1326 "for-scope",
1327 "function-parameter-scope",
1328 "class-scope",
1329 "namespace-scope",
1330 "template-parameter-scope",
1331 "template-explicit-spec-scope"
1333 const scope_kind kind = scope->explicit_spec_p
1334 ? sk_template_spec : scope->kind;
1336 return scope_kind_names[kind];
1339 /* Output a debugging information about SCOPE when performing
1340 ACTION at LINE. */
1341 static void
1342 cxx_scope_debug (cxx_scope *scope, int line, const char *action)
1344 const char *desc = cxx_scope_descriptor (scope);
1345 if (scope->this_entity)
1346 verbatim ("%s %s(%E) %p %d\n", action, desc,
1347 scope->this_entity, (void *) scope, line);
1348 else
1349 verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
1352 /* Return the estimated initial size of the hashtable of a NAMESPACE
1353 scope. */
1355 static inline size_t
1356 namespace_scope_ht_size (tree ns)
1358 tree name = DECL_NAME (ns);
1360 return name == std_identifier
1361 ? NAMESPACE_STD_HT_SIZE
1362 : (name == global_scope_name
1363 ? GLOBAL_SCOPE_HT_SIZE
1364 : NAMESPACE_ORDINARY_HT_SIZE);
1367 /* A chain of binding_level structures awaiting reuse. */
1369 static GTY((deletable)) struct cp_binding_level *free_binding_level;
1371 /* Insert SCOPE as the innermost binding level. */
1373 void
1374 push_binding_level (struct cp_binding_level *scope)
1376 /* Add it to the front of currently active scopes stack. */
1377 scope->level_chain = current_binding_level;
1378 current_binding_level = scope;
1379 keep_next_level_flag = false;
1381 if (ENABLE_SCOPE_CHECKING)
1383 scope->binding_depth = binding_depth;
1384 indent (binding_depth);
1385 cxx_scope_debug (scope, input_line, "push");
1386 binding_depth++;
1390 /* Create a new KIND scope and make it the top of the active scopes stack.
1391 ENTITY is the scope of the associated C++ entity (namespace, class,
1392 function, C++0x enumeration); it is NULL otherwise. */
1394 cxx_scope *
1395 begin_scope (scope_kind kind, tree entity)
1397 cxx_scope *scope;
1399 /* Reuse or create a struct for this binding level. */
1400 if (!ENABLE_SCOPE_CHECKING && free_binding_level)
1402 scope = free_binding_level;
1403 memset (scope, 0, sizeof (cxx_scope));
1404 free_binding_level = scope->level_chain;
1406 else
1407 scope = ggc_alloc_cleared_cxx_scope ();
1409 scope->this_entity = entity;
1410 scope->more_cleanups_ok = true;
1411 switch (kind)
1413 case sk_cleanup:
1414 scope->keep = true;
1415 break;
1417 case sk_template_spec:
1418 scope->explicit_spec_p = true;
1419 kind = sk_template_parms;
1420 /* Fall through. */
1421 case sk_template_parms:
1422 case sk_block:
1423 case sk_try:
1424 case sk_catch:
1425 case sk_for:
1426 case sk_class:
1427 case sk_scoped_enum:
1428 case sk_function_parms:
1429 case sk_omp:
1430 scope->keep = keep_next_level_flag;
1431 break;
1433 case sk_namespace:
1434 NAMESPACE_LEVEL (entity) = scope;
1435 scope->static_decls =
1436 VEC_alloc (tree, gc,
1437 DECL_NAME (entity) == std_identifier
1438 || DECL_NAME (entity) == global_scope_name
1439 ? 200 : 10);
1440 break;
1442 default:
1443 /* Should not happen. */
1444 gcc_unreachable ();
1445 break;
1447 scope->kind = kind;
1449 push_binding_level (scope);
1451 return scope;
1454 /* We're about to leave current scope. Pop the top of the stack of
1455 currently active scopes. Return the enclosing scope, now active. */
1457 cxx_scope *
1458 leave_scope (void)
1460 cxx_scope *scope = current_binding_level;
1462 if (scope->kind == sk_namespace && class_binding_level)
1463 current_binding_level = class_binding_level;
1465 /* We cannot leave a scope, if there are none left. */
1466 if (NAMESPACE_LEVEL (global_namespace))
1467 gcc_assert (!global_scope_p (scope));
1469 if (ENABLE_SCOPE_CHECKING)
1471 indent (--binding_depth);
1472 cxx_scope_debug (scope, input_line, "leave");
1475 /* Move one nesting level up. */
1476 current_binding_level = scope->level_chain;
1478 /* Namespace-scopes are left most probably temporarily, not
1479 completely; they can be reopened later, e.g. in namespace-extension
1480 or any name binding activity that requires us to resume a
1481 namespace. For classes, we cache some binding levels. For other
1482 scopes, we just make the structure available for reuse. */
1483 if (scope->kind != sk_namespace
1484 && scope->kind != sk_class)
1486 scope->level_chain = free_binding_level;
1487 gcc_assert (!ENABLE_SCOPE_CHECKING
1488 || scope->binding_depth == binding_depth);
1489 free_binding_level = scope;
1492 /* Find the innermost enclosing class scope, and reset
1493 CLASS_BINDING_LEVEL appropriately. */
1494 if (scope->kind == sk_class)
1496 class_binding_level = NULL;
1497 for (scope = current_binding_level; scope; scope = scope->level_chain)
1498 if (scope->kind == sk_class)
1500 class_binding_level = scope;
1501 break;
1505 return current_binding_level;
1508 static void
1509 resume_scope (struct cp_binding_level* b)
1511 /* Resuming binding levels is meant only for namespaces,
1512 and those cannot nest into classes. */
1513 gcc_assert (!class_binding_level);
1514 /* Also, resuming a non-directly nested namespace is a no-no. */
1515 gcc_assert (b->level_chain == current_binding_level);
1516 current_binding_level = b;
1517 if (ENABLE_SCOPE_CHECKING)
1519 b->binding_depth = binding_depth;
1520 indent (binding_depth);
1521 cxx_scope_debug (b, input_line, "resume");
1522 binding_depth++;
1526 /* Return the innermost binding level that is not for a class scope. */
1528 static cxx_scope *
1529 innermost_nonclass_level (void)
1531 cxx_scope *b;
1533 b = current_binding_level;
1534 while (b->kind == sk_class)
1535 b = b->level_chain;
1537 return b;
1540 /* We're defining an object of type TYPE. If it needs a cleanup, but
1541 we're not allowed to add any more objects with cleanups to the current
1542 scope, create a new binding level. */
1544 void
1545 maybe_push_cleanup_level (tree type)
1547 if (type != error_mark_node
1548 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
1549 && current_binding_level->more_cleanups_ok == 0)
1551 begin_scope (sk_cleanup, NULL);
1552 current_binding_level->statement_list = push_stmt_list ();
1556 /* Nonzero if we are currently in the global binding level. */
1559 global_bindings_p (void)
1561 return global_scope_p (current_binding_level);
1564 /* True if we are currently in a toplevel binding level. This
1565 means either the global binding level or a namespace in a toplevel
1566 binding level. Since there are no non-toplevel namespace levels,
1567 this really means any namespace or template parameter level. We
1568 also include a class whose context is toplevel. */
1570 bool
1571 toplevel_bindings_p (void)
1573 struct cp_binding_level *b = innermost_nonclass_level ();
1575 return b->kind == sk_namespace || b->kind == sk_template_parms;
1578 /* True if this is a namespace scope, or if we are defining a class
1579 which is itself at namespace scope, or whose enclosing class is
1580 such a class, etc. */
1582 bool
1583 namespace_bindings_p (void)
1585 struct cp_binding_level *b = innermost_nonclass_level ();
1587 return b->kind == sk_namespace;
1590 /* True if the current level needs to have a BLOCK made. */
1592 bool
1593 kept_level_p (void)
1595 return (current_binding_level->blocks != NULL_TREE
1596 || current_binding_level->keep
1597 || current_binding_level->kind == sk_cleanup
1598 || current_binding_level->names != NULL_TREE
1599 || current_binding_level->using_directives);
1602 /* Returns the kind of the innermost scope. */
1604 scope_kind
1605 innermost_scope_kind (void)
1607 return current_binding_level->kind;
1610 /* Returns true if this scope was created to store template parameters. */
1612 bool
1613 template_parm_scope_p (void)
1615 return innermost_scope_kind () == sk_template_parms;
1618 /* If KEEP is true, make a BLOCK node for the next binding level,
1619 unconditionally. Otherwise, use the normal logic to decide whether
1620 or not to create a BLOCK. */
1622 void
1623 keep_next_level (bool keep)
1625 keep_next_level_flag = keep;
1628 /* Return the list of declarations of the current level.
1629 Note that this list is in reverse order unless/until
1630 you nreverse it; and when you do nreverse it, you must
1631 store the result back using `storedecls' or you will lose. */
1633 tree
1634 getdecls (void)
1636 return current_binding_level->names;
1639 /* For debugging. */
1640 static int no_print_functions = 0;
1641 static int no_print_builtins = 0;
1643 static void
1644 print_binding_level (struct cp_binding_level* lvl)
1646 tree t;
1647 int i = 0, len;
1648 fprintf (stderr, " blocks=%p", (void *) lvl->blocks);
1649 if (lvl->more_cleanups_ok)
1650 fprintf (stderr, " more-cleanups-ok");
1651 if (lvl->have_cleanups)
1652 fprintf (stderr, " have-cleanups");
1653 fprintf (stderr, "\n");
1654 if (lvl->names)
1656 fprintf (stderr, " names:\t");
1657 /* We can probably fit 3 names to a line? */
1658 for (t = lvl->names; t; t = TREE_CHAIN (t))
1660 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
1661 continue;
1662 if (no_print_builtins
1663 && (TREE_CODE (t) == TYPE_DECL)
1664 && DECL_IS_BUILTIN (t))
1665 continue;
1667 /* Function decls tend to have longer names. */
1668 if (TREE_CODE (t) == FUNCTION_DECL)
1669 len = 3;
1670 else
1671 len = 2;
1672 i += len;
1673 if (i > 6)
1675 fprintf (stderr, "\n\t");
1676 i = len;
1678 print_node_brief (stderr, "", t, 0);
1679 if (t == error_mark_node)
1680 break;
1682 if (i)
1683 fprintf (stderr, "\n");
1685 if (VEC_length (cp_class_binding, lvl->class_shadowed))
1687 size_t i;
1688 cp_class_binding *b;
1689 fprintf (stderr, " class-shadowed:");
1690 for (i = 0;
1691 VEC_iterate(cp_class_binding, lvl->class_shadowed, i, b);
1692 ++i)
1693 fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
1694 fprintf (stderr, "\n");
1696 if (lvl->type_shadowed)
1698 fprintf (stderr, " type-shadowed:");
1699 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
1701 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1703 fprintf (stderr, "\n");
1707 void
1708 print_other_binding_stack (struct cp_binding_level *stack)
1710 struct cp_binding_level *level;
1711 for (level = stack; !global_scope_p (level); level = level->level_chain)
1713 fprintf (stderr, "binding level %p\n", (void *) level);
1714 print_binding_level (level);
1718 void
1719 print_binding_stack (void)
1721 struct cp_binding_level *b;
1722 fprintf (stderr, "current_binding_level=%p\n"
1723 "class_binding_level=%p\n"
1724 "NAMESPACE_LEVEL (global_namespace)=%p\n",
1725 (void *) current_binding_level, (void *) class_binding_level,
1726 (void *) NAMESPACE_LEVEL (global_namespace));
1727 if (class_binding_level)
1729 for (b = class_binding_level; b; b = b->level_chain)
1730 if (b == current_binding_level)
1731 break;
1732 if (b)
1733 b = class_binding_level;
1734 else
1735 b = current_binding_level;
1737 else
1738 b = current_binding_level;
1739 print_other_binding_stack (b);
1740 fprintf (stderr, "global:\n");
1741 print_binding_level (NAMESPACE_LEVEL (global_namespace));
1744 /* Return the type associated with id. */
1746 tree
1747 identifier_type_value (tree id)
1749 timevar_push (TV_NAME_LOOKUP);
1750 /* There is no type with that name, anywhere. */
1751 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
1752 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
1753 /* This is not the type marker, but the real thing. */
1754 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
1755 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, REAL_IDENTIFIER_TYPE_VALUE (id));
1756 /* Have to search for it. It must be on the global level, now.
1757 Ask lookup_name not to return non-types. */
1758 id = lookup_name_real (id, 2, 1, /*block_p=*/true, 0, LOOKUP_COMPLAIN);
1759 if (id)
1760 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_TYPE (id));
1761 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
1764 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
1765 the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */
1767 tree
1768 identifier_global_value (tree t)
1770 return IDENTIFIER_GLOBAL_VALUE (t);
1773 /* Push a definition of struct, union or enum tag named ID. into
1774 binding_level B. DECL is a TYPE_DECL for the type. We assume that
1775 the tag ID is not already defined. */
1777 static void
1778 set_identifier_type_value_with_scope (tree id, tree decl, cxx_scope *b)
1780 tree type;
1782 if (b->kind != sk_namespace)
1784 /* Shadow the marker, not the real thing, so that the marker
1785 gets restored later. */
1786 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
1787 b->type_shadowed
1788 = tree_cons (id, old_type_value, b->type_shadowed);
1789 type = decl ? TREE_TYPE (decl) : NULL_TREE;
1790 TREE_TYPE (b->type_shadowed) = type;
1792 else
1794 cxx_binding *binding =
1795 binding_for_name (NAMESPACE_LEVEL (current_namespace), id);
1796 gcc_assert (decl);
1797 if (binding->value)
1798 supplement_binding (binding, decl);
1799 else
1800 binding->value = decl;
1802 /* Store marker instead of real type. */
1803 type = global_type_node;
1805 SET_IDENTIFIER_TYPE_VALUE (id, type);
1808 /* As set_identifier_type_value_with_scope, but using
1809 current_binding_level. */
1811 void
1812 set_identifier_type_value (tree id, tree decl)
1814 set_identifier_type_value_with_scope (id, decl, current_binding_level);
1817 /* Return the name for the constructor (or destructor) for the
1818 specified class TYPE. When given a template, this routine doesn't
1819 lose the specialization. */
1821 static inline tree
1822 constructor_name_full (tree type)
1824 return TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
1827 /* Return the name for the constructor (or destructor) for the
1828 specified class. When given a template, return the plain
1829 unspecialized name. */
1831 tree
1832 constructor_name (tree type)
1834 tree name;
1835 name = constructor_name_full (type);
1836 if (IDENTIFIER_TEMPLATE (name))
1837 name = IDENTIFIER_TEMPLATE (name);
1838 return name;
1841 /* Returns TRUE if NAME is the name for the constructor for TYPE,
1842 which must be a class type. */
1844 bool
1845 constructor_name_p (tree name, tree type)
1847 tree ctor_name;
1849 gcc_assert (MAYBE_CLASS_TYPE_P (type));
1851 if (!name)
1852 return false;
1854 if (TREE_CODE (name) != IDENTIFIER_NODE)
1855 return false;
1857 ctor_name = constructor_name_full (type);
1858 if (name == ctor_name)
1859 return true;
1860 if (IDENTIFIER_TEMPLATE (ctor_name)
1861 && name == IDENTIFIER_TEMPLATE (ctor_name))
1862 return true;
1863 return false;
1866 /* Counter used to create anonymous type names. */
1868 static GTY(()) int anon_cnt;
1870 /* Return an IDENTIFIER which can be used as a name for
1871 anonymous structs and unions. */
1873 tree
1874 make_anon_name (void)
1876 char buf[32];
1878 sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
1879 return get_identifier (buf);
1882 /* This code is practically identical to that for creating
1883 anonymous names, but is just used for lambdas instead. This is necessary
1884 because anonymous names are recognized and cannot be passed to template
1885 functions. */
1886 /* FIXME is this still necessary? */
1888 static GTY(()) int lambda_cnt = 0;
1890 tree
1891 make_lambda_name (void)
1893 char buf[32];
1895 sprintf (buf, LAMBDANAME_FORMAT, lambda_cnt++);
1896 return get_identifier (buf);
1899 /* Return (from the stack of) the BINDING, if any, established at SCOPE. */
1901 static inline cxx_binding *
1902 find_binding (cxx_scope *scope, cxx_binding *binding)
1904 timevar_push (TV_NAME_LOOKUP);
1906 for (; binding != NULL; binding = binding->previous)
1907 if (binding->scope == scope)
1908 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, binding);
1910 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, (cxx_binding *)0);
1913 /* Return the binding for NAME in SCOPE, if any. Otherwise, return NULL. */
1915 static inline cxx_binding *
1916 cxx_scope_find_binding_for_name (cxx_scope *scope, tree name)
1918 cxx_binding *b = IDENTIFIER_NAMESPACE_BINDINGS (name);
1919 if (b)
1921 /* Fold-in case where NAME is used only once. */
1922 if (scope == b->scope && b->previous == NULL)
1923 return b;
1924 return find_binding (scope, b);
1926 return NULL;
1929 /* Always returns a binding for name in scope. If no binding is
1930 found, make a new one. */
1932 static cxx_binding *
1933 binding_for_name (cxx_scope *scope, tree name)
1935 cxx_binding *result;
1937 result = cxx_scope_find_binding_for_name (scope, name);
1938 if (result)
1939 return result;
1940 /* Not found, make a new one. */
1941 result = cxx_binding_make (NULL, NULL);
1942 result->previous = IDENTIFIER_NAMESPACE_BINDINGS (name);
1943 result->scope = scope;
1944 result->is_local = false;
1945 result->value_is_inherited = false;
1946 IDENTIFIER_NAMESPACE_BINDINGS (name) = result;
1947 return result;
1950 /* Walk through the bindings associated to the name of FUNCTION,
1951 and return the first binding that declares a function with a
1952 "C" linkage specification, a.k.a 'extern "C"'.
1953 This function looks for the binding, regardless of which scope it
1954 has been defined in. It basically looks in all the known scopes.
1955 Note that this function does not lookup for bindings of builtin functions
1956 or for functions declared in system headers. */
1957 static cxx_binding*
1958 lookup_extern_c_fun_binding_in_all_ns (tree function)
1960 tree name;
1961 cxx_binding *iter;
1963 gcc_assert (function && TREE_CODE (function) == FUNCTION_DECL);
1965 name = DECL_NAME (function);
1966 gcc_assert (name && TREE_CODE (name) == IDENTIFIER_NODE);
1968 for (iter = IDENTIFIER_NAMESPACE_BINDINGS (name);
1969 iter;
1970 iter = iter->previous)
1972 if (iter->value
1973 && TREE_CODE (iter->value) == FUNCTION_DECL
1974 && DECL_EXTERN_C_P (iter->value)
1975 && !DECL_ARTIFICIAL (iter->value))
1977 return iter;
1980 return NULL;
1983 /* Insert another USING_DECL into the current binding level, returning
1984 this declaration. If this is a redeclaration, do nothing, and
1985 return NULL_TREE if this not in namespace scope (in namespace
1986 scope, a using decl might extend any previous bindings). */
1988 static tree
1989 push_using_decl (tree scope, tree name)
1991 tree decl;
1993 timevar_push (TV_NAME_LOOKUP);
1994 gcc_assert (TREE_CODE (scope) == NAMESPACE_DECL);
1995 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
1996 for (decl = current_binding_level->usings; decl; decl = DECL_CHAIN (decl))
1997 if (USING_DECL_SCOPE (decl) == scope && DECL_NAME (decl) == name)
1998 break;
1999 if (decl)
2000 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
2001 namespace_bindings_p () ? decl : NULL_TREE);
2002 decl = build_lang_decl (USING_DECL, name, NULL_TREE);
2003 USING_DECL_SCOPE (decl) = scope;
2004 DECL_CHAIN (decl) = current_binding_level->usings;
2005 current_binding_level->usings = decl;
2006 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2009 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
2010 caller to set DECL_CONTEXT properly. */
2012 tree
2013 pushdecl_with_scope (tree x, cxx_scope *level, bool is_friend)
2015 struct cp_binding_level *b;
2016 tree function_decl = current_function_decl;
2018 timevar_push (TV_NAME_LOOKUP);
2019 current_function_decl = NULL_TREE;
2020 if (level->kind == sk_class)
2022 b = class_binding_level;
2023 class_binding_level = level;
2024 pushdecl_class_level (x);
2025 class_binding_level = b;
2027 else
2029 b = current_binding_level;
2030 current_binding_level = level;
2031 x = pushdecl_maybe_friend (x, is_friend);
2032 current_binding_level = b;
2034 current_function_decl = function_decl;
2035 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
2038 /* DECL is a FUNCTION_DECL for a non-member function, which may have
2039 other definitions already in place. We get around this by making
2040 the value of the identifier point to a list of all the things that
2041 want to be referenced by that name. It is then up to the users of
2042 that name to decide what to do with that list.
2044 DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its
2045 DECL_TEMPLATE_RESULT. It is dealt with the same way.
2047 FLAGS is a bitwise-or of the following values:
2048 PUSH_LOCAL: Bind DECL in the current scope, rather than at
2049 namespace scope.
2050 PUSH_USING: DECL is being pushed as the result of a using
2051 declaration.
2053 IS_FRIEND is true if this is a friend declaration.
2055 The value returned may be a previous declaration if we guessed wrong
2056 about what language DECL should belong to (C or C++). Otherwise,
2057 it's always DECL (and never something that's not a _DECL). */
2059 static tree
2060 push_overloaded_decl (tree decl, int flags, bool is_friend)
2062 tree name = DECL_NAME (decl);
2063 tree old;
2064 tree new_binding;
2065 int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
2067 timevar_push (TV_NAME_LOOKUP);
2068 if (doing_global)
2069 old = namespace_binding (name, DECL_CONTEXT (decl));
2070 else
2071 old = lookup_name_innermost_nonclass_level (name);
2073 if (old)
2075 if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
2077 tree t = TREE_TYPE (old);
2078 if (MAYBE_CLASS_TYPE_P (t) && warn_shadow
2079 && (! DECL_IN_SYSTEM_HEADER (decl)
2080 || ! DECL_IN_SYSTEM_HEADER (old)))
2081 warning (OPT_Wshadow, "%q#D hides constructor for %q#T", decl, t);
2082 old = NULL_TREE;
2084 else if (is_overloaded_fn (old))
2086 tree tmp;
2088 for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
2090 tree fn = OVL_CURRENT (tmp);
2091 tree dup;
2093 if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp)
2094 && !(flags & PUSH_USING)
2095 && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2096 TYPE_ARG_TYPES (TREE_TYPE (decl)))
2097 && ! decls_match (fn, decl))
2098 error ("%q#D conflicts with previous using declaration %q#D",
2099 decl, fn);
2101 dup = duplicate_decls (decl, fn, is_friend);
2102 /* If DECL was a redeclaration of FN -- even an invalid
2103 one -- pass that information along to our caller. */
2104 if (dup == fn || dup == error_mark_node)
2105 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, dup);
2108 /* We don't overload implicit built-ins. duplicate_decls()
2109 may fail to merge the decls if the new decl is e.g. a
2110 template function. */
2111 if (TREE_CODE (old) == FUNCTION_DECL
2112 && DECL_ANTICIPATED (old)
2113 && !DECL_HIDDEN_FRIEND_P (old))
2114 old = NULL;
2116 else if (old == error_mark_node)
2117 /* Ignore the undefined symbol marker. */
2118 old = NULL_TREE;
2119 else
2121 error ("previous non-function declaration %q+#D", old);
2122 error ("conflicts with function declaration %q#D", decl);
2123 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2127 if (old || TREE_CODE (decl) == TEMPLATE_DECL
2128 /* If it's a using declaration, we always need to build an OVERLOAD,
2129 because it's the only way to remember that the declaration comes
2130 from 'using', and have the lookup behave correctly. */
2131 || (flags & PUSH_USING))
2133 if (old && TREE_CODE (old) != OVERLOAD)
2134 new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE));
2135 else
2136 new_binding = ovl_cons (decl, old);
2137 if (flags & PUSH_USING)
2138 OVL_USED (new_binding) = 1;
2140 else
2141 /* NAME is not ambiguous. */
2142 new_binding = decl;
2144 if (doing_global)
2145 set_namespace_binding (name, current_namespace, new_binding);
2146 else
2148 /* We only create an OVERLOAD if there was a previous binding at
2149 this level, or if decl is a template. In the former case, we
2150 need to remove the old binding and replace it with the new
2151 binding. We must also run through the NAMES on the binding
2152 level where the name was bound to update the chain. */
2154 if (TREE_CODE (new_binding) == OVERLOAD && old)
2156 tree *d;
2158 for (d = &IDENTIFIER_BINDING (name)->scope->names;
2160 d = &DECL_CHAIN (*d))
2161 if (*d == old
2162 || (TREE_CODE (*d) == TREE_LIST
2163 && TREE_VALUE (*d) == old))
2165 if (TREE_CODE (*d) == TREE_LIST)
2166 /* Just replace the old binding with the new. */
2167 TREE_VALUE (*d) = new_binding;
2168 else
2169 /* Build a TREE_LIST to wrap the OVERLOAD. */
2170 *d = tree_cons (NULL_TREE, new_binding,
2171 DECL_CHAIN (*d));
2173 /* And update the cxx_binding node. */
2174 IDENTIFIER_BINDING (name)->value = new_binding;
2175 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2178 /* We should always find a previous binding in this case. */
2179 gcc_unreachable ();
2182 /* Install the new binding. */
2183 push_local_binding (name, new_binding, flags);
2186 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2189 /* Check a non-member using-declaration. Return the name and scope
2190 being used, and the USING_DECL, or NULL_TREE on failure. */
2192 static tree
2193 validate_nonmember_using_decl (tree decl, tree scope, tree name)
2195 /* [namespace.udecl]
2196 A using-declaration for a class member shall be a
2197 member-declaration. */
2198 if (TYPE_P (scope))
2200 error ("%qT is not a namespace", scope);
2201 return NULL_TREE;
2203 else if (scope == error_mark_node)
2204 return NULL_TREE;
2206 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR)
2208 /* 7.3.3/5
2209 A using-declaration shall not name a template-id. */
2210 error ("a using-declaration cannot specify a template-id. "
2211 "Try %<using %D%>", name);
2212 return NULL_TREE;
2215 if (TREE_CODE (decl) == NAMESPACE_DECL)
2217 error ("namespace %qD not allowed in using-declaration", decl);
2218 return NULL_TREE;
2221 if (TREE_CODE (decl) == SCOPE_REF)
2223 /* It's a nested name with template parameter dependent scope.
2224 This can only be using-declaration for class member. */
2225 error ("%qT is not a namespace", TREE_OPERAND (decl, 0));
2226 return NULL_TREE;
2229 if (is_overloaded_fn (decl))
2230 decl = get_first_fn (decl);
2232 gcc_assert (DECL_P (decl));
2234 /* Make a USING_DECL. */
2235 return push_using_decl (scope, name);
2238 /* Process local and global using-declarations. */
2240 static void
2241 do_nonmember_using_decl (tree scope, tree name, tree oldval, tree oldtype,
2242 tree *newval, tree *newtype)
2244 struct scope_binding decls = EMPTY_SCOPE_BINDING;
2246 *newval = *newtype = NULL_TREE;
2247 if (!qualified_lookup_using_namespace (name, scope, &decls, 0))
2248 /* Lookup error */
2249 return;
2251 if (!decls.value && !decls.type)
2253 error ("%qD not declared", name);
2254 return;
2257 /* Shift the old and new bindings around so we're comparing class and
2258 enumeration names to each other. */
2259 if (oldval && DECL_IMPLICIT_TYPEDEF_P (oldval))
2261 oldtype = oldval;
2262 oldval = NULL_TREE;
2265 if (decls.value && DECL_IMPLICIT_TYPEDEF_P (decls.value))
2267 decls.type = decls.value;
2268 decls.value = NULL_TREE;
2271 /* It is impossible to overload a built-in function; any explicit
2272 declaration eliminates the built-in declaration. So, if OLDVAL
2273 is a built-in, then we can just pretend it isn't there. */
2274 if (oldval
2275 && TREE_CODE (oldval) == FUNCTION_DECL
2276 && DECL_ANTICIPATED (oldval)
2277 && !DECL_HIDDEN_FRIEND_P (oldval))
2278 oldval = NULL_TREE;
2280 if (decls.value)
2282 /* Check for using functions. */
2283 if (is_overloaded_fn (decls.value))
2285 tree tmp, tmp1;
2287 if (oldval && !is_overloaded_fn (oldval))
2289 error ("%qD is already declared in this scope", name);
2290 oldval = NULL_TREE;
2293 *newval = oldval;
2294 for (tmp = decls.value; tmp; tmp = OVL_NEXT (tmp))
2296 tree new_fn = OVL_CURRENT (tmp);
2298 /* [namespace.udecl]
2300 If a function declaration in namespace scope or block
2301 scope has the same name and the same parameter types as a
2302 function introduced by a using declaration the program is
2303 ill-formed. */
2304 for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1))
2306 tree old_fn = OVL_CURRENT (tmp1);
2308 if (new_fn == old_fn)
2309 /* The function already exists in the current namespace. */
2310 break;
2311 else if (OVL_USED (tmp1))
2312 continue; /* this is a using decl */
2313 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (new_fn)),
2314 TYPE_ARG_TYPES (TREE_TYPE (old_fn))))
2316 gcc_assert (!DECL_ANTICIPATED (old_fn)
2317 || DECL_HIDDEN_FRIEND_P (old_fn));
2319 /* There was already a non-using declaration in
2320 this scope with the same parameter types. If both
2321 are the same extern "C" functions, that's ok. */
2322 if (decls_match (new_fn, old_fn))
2323 break;
2324 else
2326 error ("%qD is already declared in this scope", name);
2327 break;
2332 /* If we broke out of the loop, there's no reason to add
2333 this function to the using declarations for this
2334 scope. */
2335 if (tmp1)
2336 continue;
2338 /* If we are adding to an existing OVERLOAD, then we no
2339 longer know the type of the set of functions. */
2340 if (*newval && TREE_CODE (*newval) == OVERLOAD)
2341 TREE_TYPE (*newval) = unknown_type_node;
2342 /* Add this new function to the set. */
2343 *newval = build_overload (OVL_CURRENT (tmp), *newval);
2344 /* If there is only one function, then we use its type. (A
2345 using-declaration naming a single function can be used in
2346 contexts where overload resolution cannot be
2347 performed.) */
2348 if (TREE_CODE (*newval) != OVERLOAD)
2350 *newval = ovl_cons (*newval, NULL_TREE);
2351 TREE_TYPE (*newval) = TREE_TYPE (OVL_CURRENT (tmp));
2353 OVL_USED (*newval) = 1;
2356 else
2358 *newval = decls.value;
2359 if (oldval && !decls_match (*newval, oldval))
2360 error ("%qD is already declared in this scope", name);
2363 else
2364 *newval = oldval;
2366 if (decls.type && TREE_CODE (decls.type) == TREE_LIST)
2368 error ("reference to %qD is ambiguous", name);
2369 print_candidates (decls.type);
2371 else
2373 *newtype = decls.type;
2374 if (oldtype && *newtype && !decls_match (oldtype, *newtype))
2375 error ("%qD is already declared in this scope", name);
2378 /* If *newval is empty, shift any class or enumeration name down. */
2379 if (!*newval)
2381 *newval = *newtype;
2382 *newtype = NULL_TREE;
2386 /* Process a using-declaration at function scope. */
2388 void
2389 do_local_using_decl (tree decl, tree scope, tree name)
2391 tree oldval, oldtype, newval, newtype;
2392 tree orig_decl = decl;
2394 decl = validate_nonmember_using_decl (decl, scope, name);
2395 if (decl == NULL_TREE)
2396 return;
2398 if (building_stmt_tree ()
2399 && at_function_scope_p ())
2400 add_decl_expr (decl);
2402 oldval = lookup_name_innermost_nonclass_level (name);
2403 oldtype = lookup_type_current_level (name);
2405 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
2407 if (newval)
2409 if (is_overloaded_fn (newval))
2411 tree fn, term;
2413 /* We only need to push declarations for those functions
2414 that were not already bound in the current level.
2415 The old value might be NULL_TREE, it might be a single
2416 function, or an OVERLOAD. */
2417 if (oldval && TREE_CODE (oldval) == OVERLOAD)
2418 term = OVL_FUNCTION (oldval);
2419 else
2420 term = oldval;
2421 for (fn = newval; fn && OVL_CURRENT (fn) != term;
2422 fn = OVL_NEXT (fn))
2423 push_overloaded_decl (OVL_CURRENT (fn),
2424 PUSH_LOCAL | PUSH_USING,
2425 false);
2427 else
2428 push_local_binding (name, newval, PUSH_USING);
2430 if (newtype)
2432 push_local_binding (name, newtype, PUSH_USING);
2433 set_identifier_type_value (name, newtype);
2436 /* Emit debug info. */
2437 if (!processing_template_decl)
2438 cp_emit_debug_info_for_using (orig_decl, current_scope());
2441 /* Returns true if ROOT (a namespace, class, or function) encloses
2442 CHILD. CHILD may be either a class type or a namespace. */
2444 bool
2445 is_ancestor (tree root, tree child)
2447 gcc_assert ((TREE_CODE (root) == NAMESPACE_DECL
2448 || TREE_CODE (root) == FUNCTION_DECL
2449 || CLASS_TYPE_P (root)));
2450 gcc_assert ((TREE_CODE (child) == NAMESPACE_DECL
2451 || CLASS_TYPE_P (child)));
2453 /* The global namespace encloses everything. */
2454 if (root == global_namespace)
2455 return true;
2457 while (true)
2459 /* If we've run out of scopes, stop. */
2460 if (!child)
2461 return false;
2462 /* If we've reached the ROOT, it encloses CHILD. */
2463 if (root == child)
2464 return true;
2465 /* Go out one level. */
2466 if (TYPE_P (child))
2467 child = TYPE_NAME (child);
2468 child = DECL_CONTEXT (child);
2472 /* Enter the class or namespace scope indicated by T suitable for name
2473 lookup. T can be arbitrary scope, not necessary nested inside the
2474 current scope. Returns a non-null scope to pop iff pop_scope
2475 should be called later to exit this scope. */
2477 tree
2478 push_scope (tree t)
2480 if (TREE_CODE (t) == NAMESPACE_DECL)
2481 push_decl_namespace (t);
2482 else if (CLASS_TYPE_P (t))
2484 if (!at_class_scope_p ()
2485 || !same_type_p (current_class_type, t))
2486 push_nested_class (t);
2487 else
2488 /* T is the same as the current scope. There is therefore no
2489 need to re-enter the scope. Since we are not actually
2490 pushing a new scope, our caller should not call
2491 pop_scope. */
2492 t = NULL_TREE;
2495 return t;
2498 /* Leave scope pushed by push_scope. */
2500 void
2501 pop_scope (tree t)
2503 if (t == NULL_TREE)
2504 return;
2505 if (TREE_CODE (t) == NAMESPACE_DECL)
2506 pop_decl_namespace ();
2507 else if CLASS_TYPE_P (t)
2508 pop_nested_class ();
2511 /* Subroutine of push_inner_scope. */
2513 static void
2514 push_inner_scope_r (tree outer, tree inner)
2516 tree prev;
2518 if (outer == inner
2519 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
2520 return;
2522 prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
2523 if (outer != prev)
2524 push_inner_scope_r (outer, prev);
2525 if (TREE_CODE (inner) == NAMESPACE_DECL)
2527 struct cp_binding_level *save_template_parm = 0;
2528 /* Temporary take out template parameter scopes. They are saved
2529 in reversed order in save_template_parm. */
2530 while (current_binding_level->kind == sk_template_parms)
2532 struct cp_binding_level *b = current_binding_level;
2533 current_binding_level = b->level_chain;
2534 b->level_chain = save_template_parm;
2535 save_template_parm = b;
2538 resume_scope (NAMESPACE_LEVEL (inner));
2539 current_namespace = inner;
2541 /* Restore template parameter scopes. */
2542 while (save_template_parm)
2544 struct cp_binding_level *b = save_template_parm;
2545 save_template_parm = b->level_chain;
2546 b->level_chain = current_binding_level;
2547 current_binding_level = b;
2550 else
2551 pushclass (inner);
2554 /* Enter the scope INNER from current scope. INNER must be a scope
2555 nested inside current scope. This works with both name lookup and
2556 pushing name into scope. In case a template parameter scope is present,
2557 namespace is pushed under the template parameter scope according to
2558 name lookup rule in 14.6.1/6.
2560 Return the former current scope suitable for pop_inner_scope. */
2562 tree
2563 push_inner_scope (tree inner)
2565 tree outer = current_scope ();
2566 if (!outer)
2567 outer = current_namespace;
2569 push_inner_scope_r (outer, inner);
2570 return outer;
2573 /* Exit the current scope INNER back to scope OUTER. */
2575 void
2576 pop_inner_scope (tree outer, tree inner)
2578 if (outer == inner
2579 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
2580 return;
2582 while (outer != inner)
2584 if (TREE_CODE (inner) == NAMESPACE_DECL)
2586 struct cp_binding_level *save_template_parm = 0;
2587 /* Temporary take out template parameter scopes. They are saved
2588 in reversed order in save_template_parm. */
2589 while (current_binding_level->kind == sk_template_parms)
2591 struct cp_binding_level *b = current_binding_level;
2592 current_binding_level = b->level_chain;
2593 b->level_chain = save_template_parm;
2594 save_template_parm = b;
2597 pop_namespace ();
2599 /* Restore template parameter scopes. */
2600 while (save_template_parm)
2602 struct cp_binding_level *b = save_template_parm;
2603 save_template_parm = b->level_chain;
2604 b->level_chain = current_binding_level;
2605 current_binding_level = b;
2608 else
2609 popclass ();
2611 inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
2615 /* Do a pushlevel for class declarations. */
2617 void
2618 pushlevel_class (void)
2620 class_binding_level = begin_scope (sk_class, current_class_type);
2623 /* ...and a poplevel for class declarations. */
2625 void
2626 poplevel_class (void)
2628 struct cp_binding_level *level = class_binding_level;
2629 cp_class_binding *cb;
2630 size_t i;
2631 tree shadowed;
2633 timevar_push (TV_NAME_LOOKUP);
2634 gcc_assert (level != 0);
2636 /* If we're leaving a toplevel class, cache its binding level. */
2637 if (current_class_depth == 1)
2638 previous_class_level = level;
2639 for (shadowed = level->type_shadowed;
2640 shadowed;
2641 shadowed = TREE_CHAIN (shadowed))
2642 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
2644 /* Remove the bindings for all of the class-level declarations. */
2645 if (level->class_shadowed)
2647 for (i = 0;
2648 VEC_iterate (cp_class_binding, level->class_shadowed, i, cb);
2649 ++i)
2650 IDENTIFIER_BINDING (cb->identifier) = cb->base.previous;
2651 ggc_free (level->class_shadowed);
2652 level->class_shadowed = NULL;
2655 /* Now, pop out of the binding level which we created up in the
2656 `pushlevel_class' routine. */
2657 gcc_assert (current_binding_level == level);
2658 leave_scope ();
2659 timevar_pop (TV_NAME_LOOKUP);
2662 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
2663 appropriate. DECL is the value to which a name has just been
2664 bound. CLASS_TYPE is the class in which the lookup occurred. */
2666 static void
2667 set_inherited_value_binding_p (cxx_binding *binding, tree decl,
2668 tree class_type)
2670 if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
2672 tree context;
2674 if (TREE_CODE (decl) == OVERLOAD)
2675 context = CP_DECL_CONTEXT (OVL_CURRENT (decl));
2676 else
2678 gcc_assert (DECL_P (decl));
2679 context = context_for_name_lookup (decl);
2682 if (is_properly_derived_from (class_type, context))
2683 INHERITED_VALUE_BINDING_P (binding) = 1;
2684 else
2685 INHERITED_VALUE_BINDING_P (binding) = 0;
2687 else if (binding->value == decl)
2688 /* We only encounter a TREE_LIST when there is an ambiguity in the
2689 base classes. Such an ambiguity can be overridden by a
2690 definition in this class. */
2691 INHERITED_VALUE_BINDING_P (binding) = 1;
2692 else
2693 INHERITED_VALUE_BINDING_P (binding) = 0;
2696 /* Make the declaration of X appear in CLASS scope. */
2698 bool
2699 pushdecl_class_level (tree x)
2701 tree name;
2702 bool is_valid = true;
2704 /* Do nothing if we're adding to an outer lambda closure type,
2705 outer_binding will add it later if it's needed. */
2706 if (current_class_type != class_binding_level->this_entity)
2707 return true;
2709 timevar_push (TV_NAME_LOOKUP);
2710 /* Get the name of X. */
2711 if (TREE_CODE (x) == OVERLOAD)
2712 name = DECL_NAME (get_first_fn (x));
2713 else
2714 name = DECL_NAME (x);
2716 if (name)
2718 is_valid = push_class_level_binding (name, x);
2719 if (TREE_CODE (x) == TYPE_DECL)
2720 set_identifier_type_value (name, x);
2722 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
2724 /* If X is an anonymous aggregate, all of its members are
2725 treated as if they were members of the class containing the
2726 aggregate, for naming purposes. */
2727 tree f;
2729 for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = DECL_CHAIN (f))
2731 location_t save_location = input_location;
2732 input_location = DECL_SOURCE_LOCATION (f);
2733 if (!pushdecl_class_level (f))
2734 is_valid = false;
2735 input_location = save_location;
2738 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, is_valid);
2741 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
2742 scope. If the value returned is non-NULL, and the PREVIOUS field
2743 is not set, callers must set the PREVIOUS field explicitly. */
2745 static cxx_binding *
2746 get_class_binding (tree name, cxx_scope *scope)
2748 tree class_type;
2749 tree type_binding;
2750 tree value_binding;
2751 cxx_binding *binding;
2753 class_type = scope->this_entity;
2755 /* Get the type binding. */
2756 type_binding = lookup_member (class_type, name,
2757 /*protect=*/2, /*want_type=*/true);
2758 /* Get the value binding. */
2759 value_binding = lookup_member (class_type, name,
2760 /*protect=*/2, /*want_type=*/false);
2762 if (value_binding
2763 && (TREE_CODE (value_binding) == TYPE_DECL
2764 || DECL_CLASS_TEMPLATE_P (value_binding)
2765 || (TREE_CODE (value_binding) == TREE_LIST
2766 && TREE_TYPE (value_binding) == error_mark_node
2767 && (TREE_CODE (TREE_VALUE (value_binding))
2768 == TYPE_DECL))))
2769 /* We found a type binding, even when looking for a non-type
2770 binding. This means that we already processed this binding
2771 above. */
2773 else if (value_binding)
2775 if (TREE_CODE (value_binding) == TREE_LIST
2776 && TREE_TYPE (value_binding) == error_mark_node)
2777 /* NAME is ambiguous. */
2779 else if (BASELINK_P (value_binding))
2780 /* NAME is some overloaded functions. */
2781 value_binding = BASELINK_FUNCTIONS (value_binding);
2784 /* If we found either a type binding or a value binding, create a
2785 new binding object. */
2786 if (type_binding || value_binding)
2788 binding = new_class_binding (name,
2789 value_binding,
2790 type_binding,
2791 scope);
2792 /* This is a class-scope binding, not a block-scope binding. */
2793 LOCAL_BINDING_P (binding) = 0;
2794 set_inherited_value_binding_p (binding, value_binding, class_type);
2796 else
2797 binding = NULL;
2799 return binding;
2802 /* Make the declaration(s) of X appear in CLASS scope under the name
2803 NAME. Returns true if the binding is valid. */
2805 bool
2806 push_class_level_binding (tree name, tree x)
2808 cxx_binding *binding;
2809 tree decl = x;
2810 bool ok;
2812 timevar_push (TV_NAME_LOOKUP);
2813 /* The class_binding_level will be NULL if x is a template
2814 parameter name in a member template. */
2815 if (!class_binding_level)
2816 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2818 if (name == error_mark_node)
2819 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, false);
2821 /* Check for invalid member names. */
2822 gcc_assert (TYPE_BEING_DEFINED (current_class_type));
2823 /* Check that we're pushing into the right binding level. */
2824 gcc_assert (current_class_type == class_binding_level->this_entity);
2826 /* We could have been passed a tree list if this is an ambiguous
2827 declaration. If so, pull the declaration out because
2828 check_template_shadow will not handle a TREE_LIST. */
2829 if (TREE_CODE (decl) == TREE_LIST
2830 && TREE_TYPE (decl) == error_mark_node)
2831 decl = TREE_VALUE (decl);
2833 if (!check_template_shadow (decl))
2834 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, false);
2836 /* [class.mem]
2838 If T is the name of a class, then each of the following shall
2839 have a name different from T:
2841 -- every static data member of class T;
2843 -- every member of class T that is itself a type;
2845 -- every enumerator of every member of class T that is an
2846 enumerated type;
2848 -- every member of every anonymous union that is a member of
2849 class T.
2851 (Non-static data members were also forbidden to have the same
2852 name as T until TC1.) */
2853 if ((TREE_CODE (x) == VAR_DECL
2854 || TREE_CODE (x) == CONST_DECL
2855 || (TREE_CODE (x) == TYPE_DECL
2856 && !DECL_SELF_REFERENCE_P (x))
2857 /* A data member of an anonymous union. */
2858 || (TREE_CODE (x) == FIELD_DECL
2859 && DECL_CONTEXT (x) != current_class_type))
2860 && DECL_NAME (x) == constructor_name (current_class_type))
2862 tree scope = context_for_name_lookup (x);
2863 if (TYPE_P (scope) && same_type_p (scope, current_class_type))
2865 error ("%qD has the same name as the class in which it is "
2866 "declared",
2868 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, false);
2872 /* Get the current binding for NAME in this class, if any. */
2873 binding = IDENTIFIER_BINDING (name);
2874 if (!binding || binding->scope != class_binding_level)
2876 binding = get_class_binding (name, class_binding_level);
2877 /* If a new binding was created, put it at the front of the
2878 IDENTIFIER_BINDING list. */
2879 if (binding)
2881 binding->previous = IDENTIFIER_BINDING (name);
2882 IDENTIFIER_BINDING (name) = binding;
2886 /* If there is already a binding, then we may need to update the
2887 current value. */
2888 if (binding && binding->value)
2890 tree bval = binding->value;
2891 tree old_decl = NULL_TREE;
2893 if (INHERITED_VALUE_BINDING_P (binding))
2895 /* If the old binding was from a base class, and was for a
2896 tag name, slide it over to make room for the new binding.
2897 The old binding is still visible if explicitly qualified
2898 with a class-key. */
2899 if (TREE_CODE (bval) == TYPE_DECL && DECL_ARTIFICIAL (bval)
2900 && !(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)))
2902 old_decl = binding->type;
2903 binding->type = bval;
2904 binding->value = NULL_TREE;
2905 INHERITED_VALUE_BINDING_P (binding) = 0;
2907 else
2909 old_decl = bval;
2910 /* Any inherited type declaration is hidden by the type
2911 declaration in the derived class. */
2912 if (TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x))
2913 binding->type = NULL_TREE;
2916 else if (TREE_CODE (x) == OVERLOAD && is_overloaded_fn (bval))
2917 old_decl = bval;
2918 else if (TREE_CODE (x) == USING_DECL && TREE_CODE (bval) == USING_DECL)
2919 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2920 else if (TREE_CODE (x) == USING_DECL && is_overloaded_fn (bval))
2921 old_decl = bval;
2922 else if (TREE_CODE (bval) == USING_DECL && is_overloaded_fn (x))
2923 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2925 if (old_decl && binding->scope == class_binding_level)
2927 binding->value = x;
2928 /* It is always safe to clear INHERITED_VALUE_BINDING_P
2929 here. This function is only used to register bindings
2930 from with the class definition itself. */
2931 INHERITED_VALUE_BINDING_P (binding) = 0;
2932 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2936 /* Note that we declared this value so that we can issue an error if
2937 this is an invalid redeclaration of a name already used for some
2938 other purpose. */
2939 note_name_declared_in_class (name, decl);
2941 /* If we didn't replace an existing binding, put the binding on the
2942 stack of bindings for the identifier, and update the shadowed
2943 list. */
2944 if (binding && binding->scope == class_binding_level)
2945 /* Supplement the existing binding. */
2946 ok = supplement_binding (binding, decl);
2947 else
2949 /* Create a new binding. */
2950 push_binding (name, decl, class_binding_level);
2951 ok = true;
2954 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ok);
2957 /* Process "using SCOPE::NAME" in a class scope. Return the
2958 USING_DECL created. */
2960 tree
2961 do_class_using_decl (tree scope, tree name)
2963 /* The USING_DECL returned by this function. */
2964 tree value;
2965 /* The declaration (or declarations) name by this using
2966 declaration. NULL if we are in a template and cannot figure out
2967 what has been named. */
2968 tree decl;
2969 /* True if SCOPE is a dependent type. */
2970 bool scope_dependent_p;
2971 /* True if SCOPE::NAME is dependent. */
2972 bool name_dependent_p;
2973 /* True if any of the bases of CURRENT_CLASS_TYPE are dependent. */
2974 bool bases_dependent_p;
2975 tree binfo;
2976 tree base_binfo;
2977 int i;
2979 if (name == error_mark_node)
2980 return NULL_TREE;
2982 if (!scope || !TYPE_P (scope))
2984 error ("using-declaration for non-member at class scope");
2985 return NULL_TREE;
2988 /* Make sure the name is not invalid */
2989 if (TREE_CODE (name) == BIT_NOT_EXPR)
2991 error ("%<%T::%D%> names destructor", scope, name);
2992 return NULL_TREE;
2994 if (MAYBE_CLASS_TYPE_P (scope) && constructor_name_p (name, scope))
2996 error ("%<%T::%D%> names constructor", scope, name);
2997 return NULL_TREE;
2999 if (constructor_name_p (name, current_class_type))
3001 error ("%<%T::%D%> names constructor in %qT",
3002 scope, name, current_class_type);
3003 return NULL_TREE;
3006 scope_dependent_p = dependent_type_p (scope);
3007 name_dependent_p = (scope_dependent_p
3008 || (IDENTIFIER_TYPENAME_P (name)
3009 && dependent_type_p (TREE_TYPE (name))));
3011 bases_dependent_p = false;
3012 if (processing_template_decl)
3013 for (binfo = TYPE_BINFO (current_class_type), i = 0;
3014 BINFO_BASE_ITERATE (binfo, i, base_binfo);
3015 i++)
3016 if (dependent_type_p (TREE_TYPE (base_binfo)))
3018 bases_dependent_p = true;
3019 break;
3022 decl = NULL_TREE;
3024 /* From [namespace.udecl]:
3026 A using-declaration used as a member-declaration shall refer to a
3027 member of a base class of the class being defined.
3029 In general, we cannot check this constraint in a template because
3030 we do not know the entire set of base classes of the current
3031 class type. However, if all of the base classes are
3032 non-dependent, then we can avoid delaying the check until
3033 instantiation. */
3034 if (!scope_dependent_p)
3036 base_kind b_kind;
3037 binfo = lookup_base (current_class_type, scope, ba_any, &b_kind);
3038 if (b_kind < bk_proper_base)
3040 if (!bases_dependent_p)
3042 error_not_base_type (scope, current_class_type);
3043 return NULL_TREE;
3046 else if (!name_dependent_p)
3048 decl = lookup_member (binfo, name, 0, false);
3049 if (!decl)
3051 error ("no members matching %<%T::%D%> in %q#T", scope, name,
3052 scope);
3053 return NULL_TREE;
3055 /* The binfo from which the functions came does not matter. */
3056 if (BASELINK_P (decl))
3057 decl = BASELINK_FUNCTIONS (decl);
3061 value = build_lang_decl (USING_DECL, name, NULL_TREE);
3062 USING_DECL_DECLS (value) = decl;
3063 USING_DECL_SCOPE (value) = scope;
3064 DECL_DEPENDENT_P (value) = !decl;
3066 return value;
3070 /* Return the binding value for name in scope. */
3072 tree
3073 namespace_binding (tree name, tree scope)
3075 cxx_binding *binding;
3077 if (scope == NULL)
3078 scope = global_namespace;
3079 else
3080 /* Unnecessary for the global namespace because it can't be an alias. */
3081 scope = ORIGINAL_NAMESPACE (scope);
3083 binding = cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
3085 return binding ? binding->value : NULL_TREE;
3088 /* Set the binding value for name in scope. */
3090 void
3091 set_namespace_binding (tree name, tree scope, tree val)
3093 cxx_binding *b;
3095 timevar_push (TV_NAME_LOOKUP);
3096 if (scope == NULL_TREE)
3097 scope = global_namespace;
3098 b = binding_for_name (NAMESPACE_LEVEL (scope), name);
3099 if (!b->value || TREE_CODE (val) == OVERLOAD || val == error_mark_node)
3100 b->value = val;
3101 else
3102 supplement_binding (b, val);
3103 timevar_pop (TV_NAME_LOOKUP);
3106 /* Set the context of a declaration to scope. Complain if we are not
3107 outside scope. */
3109 void
3110 set_decl_namespace (tree decl, tree scope, bool friendp)
3112 tree old;
3114 /* Get rid of namespace aliases. */
3115 scope = ORIGINAL_NAMESPACE (scope);
3117 /* It is ok for friends to be qualified in parallel space. */
3118 if (!friendp && !is_ancestor (current_namespace, scope))
3119 error ("declaration of %qD not in a namespace surrounding %qD",
3120 decl, scope);
3121 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3123 /* Writing "int N::i" to declare a variable within "N" is invalid. */
3124 if (scope == current_namespace)
3126 if (at_namespace_scope_p ())
3127 error ("explicit qualification in declaration of %qD",
3128 decl);
3129 return;
3132 /* See whether this has been declared in the namespace. */
3133 old = lookup_qualified_name (scope, DECL_NAME (decl), false, true);
3134 if (old == error_mark_node)
3135 /* No old declaration at all. */
3136 goto complain;
3137 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
3138 if (TREE_CODE (old) == TREE_LIST)
3140 error ("reference to %qD is ambiguous", decl);
3141 print_candidates (old);
3142 return;
3144 if (!is_overloaded_fn (decl))
3146 /* We might have found OLD in an inline namespace inside SCOPE. */
3147 if (TREE_CODE (decl) == TREE_CODE (old))
3148 DECL_CONTEXT (decl) = DECL_CONTEXT (old);
3149 /* Don't compare non-function decls with decls_match here, since
3150 it can't check for the correct constness at this
3151 point. pushdecl will find those errors later. */
3152 return;
3154 /* Since decl is a function, old should contain a function decl. */
3155 if (!is_overloaded_fn (old))
3156 goto complain;
3157 /* A template can be explicitly specialized in any namespace. */
3158 if (processing_explicit_instantiation)
3159 return;
3160 if (processing_template_decl || processing_specialization)
3161 /* We have not yet called push_template_decl to turn a
3162 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
3163 match. But, we'll check later, when we construct the
3164 template. */
3165 return;
3166 /* Instantiations or specializations of templates may be declared as
3167 friends in any namespace. */
3168 if (friendp && DECL_USE_TEMPLATE (decl))
3169 return;
3170 if (is_overloaded_fn (old))
3172 tree found = NULL_TREE;
3173 tree elt = old;
3174 for (; elt; elt = OVL_NEXT (elt))
3176 tree ofn = OVL_CURRENT (elt);
3177 /* Adjust DECL_CONTEXT first so decls_match will return true
3178 if DECL will match a declaration in an inline namespace. */
3179 DECL_CONTEXT (decl) = DECL_CONTEXT (ofn);
3180 if (decls_match (decl, ofn))
3182 if (found && !decls_match (found, ofn))
3184 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3185 error ("reference to %qD is ambiguous", decl);
3186 print_candidates (old);
3187 return;
3189 found = ofn;
3192 if (found)
3194 if (!is_associated_namespace (scope, CP_DECL_CONTEXT (found)))
3195 goto complain;
3196 DECL_CONTEXT (decl) = DECL_CONTEXT (found);
3197 return;
3200 else
3202 DECL_CONTEXT (decl) = DECL_CONTEXT (old);
3203 if (decls_match (decl, old))
3204 return;
3207 /* It didn't work, go back to the explicit scope. */
3208 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3209 complain:
3210 error ("%qD should have been declared inside %qD", decl, scope);
3213 /* Return the namespace where the current declaration is declared. */
3215 tree
3216 current_decl_namespace (void)
3218 tree result;
3219 /* If we have been pushed into a different namespace, use it. */
3220 if (!VEC_empty (tree, decl_namespace_list))
3221 return VEC_last (tree, decl_namespace_list);
3223 if (current_class_type)
3224 result = decl_namespace_context (current_class_type);
3225 else if (current_function_decl)
3226 result = decl_namespace_context (current_function_decl);
3227 else
3228 result = current_namespace;
3229 return result;
3232 /* Process any ATTRIBUTES on a namespace definition. Currently only
3233 attribute visibility is meaningful, which is a property of the syntactic
3234 block rather than the namespace as a whole, so we don't touch the
3235 NAMESPACE_DECL at all. Returns true if attribute visibility is seen. */
3237 bool
3238 handle_namespace_attrs (tree ns, tree attributes)
3240 tree d;
3241 bool saw_vis = false;
3243 for (d = attributes; d; d = TREE_CHAIN (d))
3245 tree name = TREE_PURPOSE (d);
3246 tree args = TREE_VALUE (d);
3248 #ifdef HANDLE_PRAGMA_VISIBILITY
3249 if (is_attribute_p ("visibility", name))
3251 tree x = args ? TREE_VALUE (args) : NULL_TREE;
3252 if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args))
3254 warning (OPT_Wattributes,
3255 "%qD attribute requires a single NTBS argument",
3256 name);
3257 continue;
3260 if (!TREE_PUBLIC (ns))
3261 warning (OPT_Wattributes,
3262 "%qD attribute is meaningless since members of the "
3263 "anonymous namespace get local symbols", name);
3265 push_visibility (TREE_STRING_POINTER (x), 1);
3266 saw_vis = true;
3268 else
3269 #endif
3271 warning (OPT_Wattributes, "%qD attribute directive ignored",
3272 name);
3273 continue;
3277 return saw_vis;
3280 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we
3281 select a name that is unique to this compilation unit. */
3283 void
3284 push_namespace (tree name)
3286 tree d = NULL_TREE;
3287 int need_new = 1;
3288 int implicit_use = 0;
3289 bool anon = !name;
3291 timevar_push (TV_NAME_LOOKUP);
3293 /* We should not get here if the global_namespace is not yet constructed
3294 nor if NAME designates the global namespace: The global scope is
3295 constructed elsewhere. */
3296 gcc_assert (global_namespace != NULL && name != global_scope_name);
3298 if (anon)
3300 name = get_anonymous_namespace_name();
3301 d = IDENTIFIER_NAMESPACE_VALUE (name);
3302 if (d)
3303 /* Reopening anonymous namespace. */
3304 need_new = 0;
3305 implicit_use = 1;
3307 else
3309 /* Check whether this is an extended namespace definition. */
3310 d = IDENTIFIER_NAMESPACE_VALUE (name);
3311 if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
3313 need_new = 0;
3314 if (DECL_NAMESPACE_ALIAS (d))
3316 error ("namespace alias %qD not allowed here, assuming %qD",
3317 d, DECL_NAMESPACE_ALIAS (d));
3318 d = DECL_NAMESPACE_ALIAS (d);
3323 if (need_new)
3325 /* Make a new namespace, binding the name to it. */
3326 d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
3327 DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace);
3328 /* The name of this namespace is not visible to other translation
3329 units if it is an anonymous namespace or member thereof. */
3330 if (anon || decl_anon_ns_mem_p (current_namespace))
3331 TREE_PUBLIC (d) = 0;
3332 else
3333 TREE_PUBLIC (d) = 1;
3334 pushdecl (d);
3335 if (anon)
3337 /* Clear DECL_NAME for the benefit of debugging back ends. */
3338 SET_DECL_ASSEMBLER_NAME (d, name);
3339 DECL_NAME (d) = NULL_TREE;
3341 begin_scope (sk_namespace, d);
3343 else
3344 resume_scope (NAMESPACE_LEVEL (d));
3346 if (implicit_use)
3347 do_using_directive (d);
3348 /* Enter the name space. */
3349 current_namespace = d;
3351 timevar_pop (TV_NAME_LOOKUP);
3354 /* Pop from the scope of the current namespace. */
3356 void
3357 pop_namespace (void)
3359 gcc_assert (current_namespace != global_namespace);
3360 current_namespace = CP_DECL_CONTEXT (current_namespace);
3361 /* The binding level is not popped, as it might be re-opened later. */
3362 leave_scope ();
3365 /* Push into the scope of the namespace NS, even if it is deeply
3366 nested within another namespace. */
3368 void
3369 push_nested_namespace (tree ns)
3371 if (ns == global_namespace)
3372 push_to_top_level ();
3373 else
3375 push_nested_namespace (CP_DECL_CONTEXT (ns));
3376 push_namespace (DECL_NAME (ns));
3380 /* Pop back from the scope of the namespace NS, which was previously
3381 entered with push_nested_namespace. */
3383 void
3384 pop_nested_namespace (tree ns)
3386 timevar_push (TV_NAME_LOOKUP);
3387 gcc_assert (current_namespace == ns);
3388 while (ns != global_namespace)
3390 pop_namespace ();
3391 ns = CP_DECL_CONTEXT (ns);
3394 pop_from_top_level ();
3395 timevar_pop (TV_NAME_LOOKUP);
3398 /* Temporarily set the namespace for the current declaration. */
3400 void
3401 push_decl_namespace (tree decl)
3403 if (TREE_CODE (decl) != NAMESPACE_DECL)
3404 decl = decl_namespace_context (decl);
3405 VEC_safe_push (tree, gc, decl_namespace_list, ORIGINAL_NAMESPACE (decl));
3408 /* [namespace.memdef]/2 */
3410 void
3411 pop_decl_namespace (void)
3413 VEC_pop (tree, decl_namespace_list);
3416 /* Return the namespace that is the common ancestor
3417 of two given namespaces. */
3419 static tree
3420 namespace_ancestor (tree ns1, tree ns2)
3422 timevar_push (TV_NAME_LOOKUP);
3423 if (is_ancestor (ns1, ns2))
3424 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ns1);
3425 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
3426 namespace_ancestor (CP_DECL_CONTEXT (ns1), ns2));
3429 /* Process a namespace-alias declaration. */
3431 void
3432 do_namespace_alias (tree alias, tree name_space)
3434 if (name_space == error_mark_node)
3435 return;
3437 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
3439 name_space = ORIGINAL_NAMESPACE (name_space);
3441 /* Build the alias. */
3442 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
3443 DECL_NAMESPACE_ALIAS (alias) = name_space;
3444 DECL_EXTERNAL (alias) = 1;
3445 DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ());
3446 pushdecl (alias);
3448 /* Emit debug info for namespace alias. */
3449 if (!building_stmt_tree ())
3450 (*debug_hooks->global_decl) (alias);
3453 /* Like pushdecl, only it places X in the current namespace,
3454 if appropriate. */
3456 tree
3457 pushdecl_namespace_level (tree x, bool is_friend)
3459 struct cp_binding_level *b = current_binding_level;
3460 tree t;
3462 timevar_push (TV_NAME_LOOKUP);
3463 t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace), is_friend);
3465 /* Now, the type_shadowed stack may screw us. Munge it so it does
3466 what we want. */
3467 if (TREE_CODE (t) == TYPE_DECL)
3469 tree name = DECL_NAME (t);
3470 tree newval;
3471 tree *ptr = (tree *)0;
3472 for (; !global_scope_p (b); b = b->level_chain)
3474 tree shadowed = b->type_shadowed;
3475 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
3476 if (TREE_PURPOSE (shadowed) == name)
3478 ptr = &TREE_VALUE (shadowed);
3479 /* Can't break out of the loop here because sometimes
3480 a binding level will have duplicate bindings for
3481 PT names. It's gross, but I haven't time to fix it. */
3484 newval = TREE_TYPE (t);
3485 if (ptr == (tree *)0)
3487 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
3488 up here if this is changed to an assertion. --KR */
3489 SET_IDENTIFIER_TYPE_VALUE (name, t);
3491 else
3493 *ptr = newval;
3496 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3499 /* Insert USED into the using list of USER. Set INDIRECT_flag if this
3500 directive is not directly from the source. Also find the common
3501 ancestor and let our users know about the new namespace */
3502 static void
3503 add_using_namespace (tree user, tree used, bool indirect)
3505 tree t;
3506 timevar_push (TV_NAME_LOOKUP);
3507 /* Using oneself is a no-op. */
3508 if (user == used)
3510 timevar_pop (TV_NAME_LOOKUP);
3511 return;
3513 gcc_assert (TREE_CODE (user) == NAMESPACE_DECL);
3514 gcc_assert (TREE_CODE (used) == NAMESPACE_DECL);
3515 /* Check if we already have this. */
3516 t = purpose_member (used, DECL_NAMESPACE_USING (user));
3517 if (t != NULL_TREE)
3519 if (!indirect)
3520 /* Promote to direct usage. */
3521 TREE_INDIRECT_USING (t) = 0;
3522 timevar_pop (TV_NAME_LOOKUP);
3523 return;
3526 /* Add used to the user's using list. */
3527 DECL_NAMESPACE_USING (user)
3528 = tree_cons (used, namespace_ancestor (user, used),
3529 DECL_NAMESPACE_USING (user));
3531 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect;
3533 /* Add user to the used's users list. */
3534 DECL_NAMESPACE_USERS (used)
3535 = tree_cons (user, 0, DECL_NAMESPACE_USERS (used));
3537 /* Recursively add all namespaces used. */
3538 for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t))
3539 /* indirect usage */
3540 add_using_namespace (user, TREE_PURPOSE (t), 1);
3542 /* Tell everyone using us about the new used namespaces. */
3543 for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t))
3544 add_using_namespace (TREE_PURPOSE (t), used, 1);
3545 timevar_pop (TV_NAME_LOOKUP);
3548 /* Process a using-declaration not appearing in class or local scope. */
3550 void
3551 do_toplevel_using_decl (tree decl, tree scope, tree name)
3553 tree oldval, oldtype, newval, newtype;
3554 tree orig_decl = decl;
3555 cxx_binding *binding;
3557 decl = validate_nonmember_using_decl (decl, scope, name);
3558 if (decl == NULL_TREE)
3559 return;
3561 binding = binding_for_name (NAMESPACE_LEVEL (current_namespace), name);
3563 oldval = binding->value;
3564 oldtype = binding->type;
3566 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
3568 /* Emit debug info. */
3569 if (!processing_template_decl)
3570 cp_emit_debug_info_for_using (orig_decl, current_namespace);
3572 /* Copy declarations found. */
3573 if (newval)
3574 binding->value = newval;
3575 if (newtype)
3576 binding->type = newtype;
3579 /* Process a using-directive. */
3581 void
3582 do_using_directive (tree name_space)
3584 tree context = NULL_TREE;
3586 if (name_space == error_mark_node)
3587 return;
3589 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
3591 if (building_stmt_tree ())
3592 add_stmt (build_stmt (input_location, USING_STMT, name_space));
3593 name_space = ORIGINAL_NAMESPACE (name_space);
3595 if (!toplevel_bindings_p ())
3597 push_using_directive (name_space);
3599 else
3601 /* direct usage */
3602 add_using_namespace (current_namespace, name_space, 0);
3603 if (current_namespace != global_namespace)
3604 context = current_namespace;
3606 /* Emit debugging info. */
3607 if (!processing_template_decl)
3608 (*debug_hooks->imported_module_or_decl) (name_space, NULL_TREE,
3609 context, false);
3613 /* Deal with a using-directive seen by the parser. Currently we only
3614 handle attributes here, since they cannot appear inside a template. */
3616 void
3617 parse_using_directive (tree name_space, tree attribs)
3619 tree a;
3621 do_using_directive (name_space);
3623 for (a = attribs; a; a = TREE_CHAIN (a))
3625 tree name = TREE_PURPOSE (a);
3626 if (is_attribute_p ("strong", name))
3628 if (!toplevel_bindings_p ())
3629 error ("strong using only meaningful at namespace scope");
3630 else if (name_space != error_mark_node)
3632 if (!is_ancestor (current_namespace, name_space))
3633 error ("current namespace %qD does not enclose strongly used namespace %qD",
3634 current_namespace, name_space);
3635 DECL_NAMESPACE_ASSOCIATIONS (name_space)
3636 = tree_cons (current_namespace, 0,
3637 DECL_NAMESPACE_ASSOCIATIONS (name_space));
3640 else
3641 warning (OPT_Wattributes, "%qD attribute directive ignored", name);
3645 /* Like pushdecl, only it places X in the global scope if appropriate.
3646 Calls cp_finish_decl to register the variable, initializing it with
3647 *INIT, if INIT is non-NULL. */
3649 static tree
3650 pushdecl_top_level_1 (tree x, tree *init, bool is_friend)
3652 timevar_push (TV_NAME_LOOKUP);
3653 push_to_top_level ();
3654 x = pushdecl_namespace_level (x, is_friend);
3655 if (init)
3656 cp_finish_decl (x, *init, false, NULL_TREE, 0);
3657 pop_from_top_level ();
3658 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
3661 /* Like pushdecl, only it places X in the global scope if appropriate. */
3663 tree
3664 pushdecl_top_level (tree x)
3666 return pushdecl_top_level_1 (x, NULL, false);
3669 /* Like pushdecl_top_level, but adding the IS_FRIEND parameter. */
3671 tree
3672 pushdecl_top_level_maybe_friend (tree x, bool is_friend)
3674 return pushdecl_top_level_1 (x, NULL, is_friend);
3677 /* Like pushdecl, only it places X in the global scope if
3678 appropriate. Calls cp_finish_decl to register the variable,
3679 initializing it with INIT. */
3681 tree
3682 pushdecl_top_level_and_finish (tree x, tree init)
3684 return pushdecl_top_level_1 (x, &init, false);
3687 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
3688 duplicates. The first list becomes the tail of the result.
3690 The algorithm is O(n^2). We could get this down to O(n log n) by
3691 doing a sort on the addresses of the functions, if that becomes
3692 necessary. */
3694 static tree
3695 merge_functions (tree s1, tree s2)
3697 for (; s2; s2 = OVL_NEXT (s2))
3699 tree fn2 = OVL_CURRENT (s2);
3700 tree fns1;
3702 for (fns1 = s1; fns1; fns1 = OVL_NEXT (fns1))
3704 tree fn1 = OVL_CURRENT (fns1);
3706 /* If the function from S2 is already in S1, there is no
3707 need to add it again. For `extern "C"' functions, we
3708 might have two FUNCTION_DECLs for the same function, in
3709 different namespaces, but let's leave them in in case
3710 they have different default arguments. */
3711 if (fn1 == fn2)
3712 break;
3715 /* If we exhausted all of the functions in S1, FN2 is new. */
3716 if (!fns1)
3717 s1 = build_overload (fn2, s1);
3719 return s1;
3722 /* Returns TRUE iff OLD and NEW are the same entity.
3724 3 [basic]/3: An entity is a value, object, reference, function,
3725 enumerator, type, class member, template, template specialization,
3726 namespace, parameter pack, or this.
3728 7.3.4 [namespace.udir]/4: If name lookup finds a declaration for a name
3729 in two different namespaces, and the declarations do not declare the
3730 same entity and do not declare functions, the use of the name is
3731 ill-formed. */
3733 static bool
3734 same_entity_p (tree one, tree two)
3736 if (one == two)
3737 return true;
3738 if (!one || !two)
3739 return false;
3740 if (TREE_CODE (one) == TYPE_DECL
3741 && TREE_CODE (two) == TYPE_DECL
3742 && same_type_p (TREE_TYPE (one), TREE_TYPE (two)))
3743 return true;
3744 return false;
3747 /* This should return an error not all definitions define functions.
3748 It is not an error if we find two functions with exactly the
3749 same signature, only if these are selected in overload resolution.
3750 old is the current set of bindings, new_binding the freshly-found binding.
3751 XXX Do we want to give *all* candidates in case of ambiguity?
3752 XXX In what way should I treat extern declarations?
3753 XXX I don't want to repeat the entire duplicate_decls here */
3755 static void
3756 ambiguous_decl (struct scope_binding *old, cxx_binding *new_binding, int flags)
3758 tree val, type;
3759 gcc_assert (old != NULL);
3761 /* Copy the type. */
3762 type = new_binding->type;
3763 if (LOOKUP_NAMESPACES_ONLY (flags)
3764 || (type && hidden_name_p (type) && !(flags & LOOKUP_HIDDEN)))
3765 type = NULL_TREE;
3767 /* Copy the value. */
3768 val = new_binding->value;
3769 if (val)
3771 if (hidden_name_p (val) && !(flags & LOOKUP_HIDDEN))
3772 val = NULL_TREE;
3773 else
3774 switch (TREE_CODE (val))
3776 case TEMPLATE_DECL:
3777 /* If we expect types or namespaces, and not templates,
3778 or this is not a template class. */
3779 if ((LOOKUP_QUALIFIERS_ONLY (flags)
3780 && !DECL_CLASS_TEMPLATE_P (val)))
3781 val = NULL_TREE;
3782 break;
3783 case TYPE_DECL:
3784 if (LOOKUP_NAMESPACES_ONLY (flags)
3785 || (type && (flags & LOOKUP_PREFER_TYPES)))
3786 val = NULL_TREE;
3787 break;
3788 case NAMESPACE_DECL:
3789 if (LOOKUP_TYPES_ONLY (flags))
3790 val = NULL_TREE;
3791 break;
3792 case FUNCTION_DECL:
3793 /* Ignore built-in functions that are still anticipated. */
3794 if (LOOKUP_QUALIFIERS_ONLY (flags))
3795 val = NULL_TREE;
3796 break;
3797 default:
3798 if (LOOKUP_QUALIFIERS_ONLY (flags))
3799 val = NULL_TREE;
3803 /* If val is hidden, shift down any class or enumeration name. */
3804 if (!val)
3806 val = type;
3807 type = NULL_TREE;
3810 if (!old->value)
3811 old->value = val;
3812 else if (val && !same_entity_p (val, old->value))
3814 if (is_overloaded_fn (old->value) && is_overloaded_fn (val))
3815 old->value = merge_functions (old->value, val);
3816 else
3818 old->value = tree_cons (NULL_TREE, old->value,
3819 build_tree_list (NULL_TREE, val));
3820 TREE_TYPE (old->value) = error_mark_node;
3824 if (!old->type)
3825 old->type = type;
3826 else if (type && old->type != type)
3828 old->type = tree_cons (NULL_TREE, old->type,
3829 build_tree_list (NULL_TREE, type));
3830 TREE_TYPE (old->type) = error_mark_node;
3834 /* Return the declarations that are members of the namespace NS. */
3836 tree
3837 cp_namespace_decls (tree ns)
3839 return NAMESPACE_LEVEL (ns)->names;
3842 /* Combine prefer_type and namespaces_only into flags. */
3844 static int
3845 lookup_flags (int prefer_type, int namespaces_only)
3847 if (namespaces_only)
3848 return LOOKUP_PREFER_NAMESPACES;
3849 if (prefer_type > 1)
3850 return LOOKUP_PREFER_TYPES;
3851 if (prefer_type > 0)
3852 return LOOKUP_PREFER_BOTH;
3853 return 0;
3856 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
3857 ignore it or not. Subroutine of lookup_name_real and
3858 lookup_type_scope. */
3860 static bool
3861 qualify_lookup (tree val, int flags)
3863 if (val == NULL_TREE)
3864 return false;
3865 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
3866 return true;
3867 if ((flags & LOOKUP_PREFER_TYPES)
3868 && (TREE_CODE (val) == TYPE_DECL || TREE_CODE (val) == TEMPLATE_DECL))
3869 return true;
3870 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
3871 return false;
3872 /* In unevaluated context, look past normal capture fields. */
3873 if (cp_unevaluated_operand && TREE_CODE (val) == FIELD_DECL
3874 && DECL_NORMAL_CAPTURE_P (val))
3875 return false;
3876 /* None of the lookups that use qualify_lookup want the op() from the
3877 lambda; they want the one from the enclosing class. */
3878 if (TREE_CODE (val) == FUNCTION_DECL && LAMBDA_FUNCTION_P (val))
3879 return false;
3880 return true;
3883 /* Given a lookup that returned VAL, decide if we want to ignore it or
3884 not based on DECL_ANTICIPATED. */
3886 bool
3887 hidden_name_p (tree val)
3889 if (DECL_P (val)
3890 && DECL_LANG_SPECIFIC (val)
3891 && DECL_ANTICIPATED (val))
3892 return true;
3893 return false;
3896 /* Remove any hidden friend functions from a possibly overloaded set
3897 of functions. */
3899 tree
3900 remove_hidden_names (tree fns)
3902 if (!fns)
3903 return fns;
3905 if (TREE_CODE (fns) == FUNCTION_DECL && hidden_name_p (fns))
3906 fns = NULL_TREE;
3907 else if (TREE_CODE (fns) == OVERLOAD)
3909 tree o;
3911 for (o = fns; o; o = OVL_NEXT (o))
3912 if (hidden_name_p (OVL_CURRENT (o)))
3913 break;
3914 if (o)
3916 tree n = NULL_TREE;
3918 for (o = fns; o; o = OVL_NEXT (o))
3919 if (!hidden_name_p (OVL_CURRENT (o)))
3920 n = build_overload (OVL_CURRENT (o), n);
3921 fns = n;
3925 return fns;
3928 /* Unscoped lookup of a global: iterate over current namespaces,
3929 considering using-directives. */
3931 static tree
3932 unqualified_namespace_lookup (tree name, int flags)
3934 tree initial = current_decl_namespace ();
3935 tree scope = initial;
3936 tree siter;
3937 struct cp_binding_level *level;
3938 tree val = NULL_TREE;
3940 timevar_push (TV_NAME_LOOKUP);
3942 for (; !val; scope = CP_DECL_CONTEXT (scope))
3944 struct scope_binding binding = EMPTY_SCOPE_BINDING;
3945 cxx_binding *b =
3946 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
3948 if (b)
3949 ambiguous_decl (&binding, b, flags);
3951 /* Add all _DECLs seen through local using-directives. */
3952 for (level = current_binding_level;
3953 level->kind != sk_namespace;
3954 level = level->level_chain)
3955 if (!lookup_using_namespace (name, &binding, level->using_directives,
3956 scope, flags))
3957 /* Give up because of error. */
3958 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
3960 /* Add all _DECLs seen through global using-directives. */
3961 /* XXX local and global using lists should work equally. */
3962 siter = initial;
3963 while (1)
3965 if (!lookup_using_namespace (name, &binding,
3966 DECL_NAMESPACE_USING (siter),
3967 scope, flags))
3968 /* Give up because of error. */
3969 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
3970 if (siter == scope) break;
3971 siter = CP_DECL_CONTEXT (siter);
3974 val = binding.value;
3975 if (scope == global_namespace)
3976 break;
3978 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
3981 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
3982 or a class TYPE). If IS_TYPE_P is TRUE, then ignore non-type
3983 bindings.
3985 Returns a DECL (or OVERLOAD, or BASELINK) representing the
3986 declaration found. If no suitable declaration can be found,
3987 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
3988 neither a class-type nor a namespace a diagnostic is issued. */
3990 tree
3991 lookup_qualified_name (tree scope, tree name, bool is_type_p, bool complain)
3993 int flags = 0;
3994 tree t = NULL_TREE;
3996 if (TREE_CODE (scope) == NAMESPACE_DECL)
3998 struct scope_binding binding = EMPTY_SCOPE_BINDING;
4000 flags |= LOOKUP_COMPLAIN;
4001 if (is_type_p)
4002 flags |= LOOKUP_PREFER_TYPES;
4003 if (qualified_lookup_using_namespace (name, scope, &binding, flags))
4004 t = binding.value;
4006 else if (cxx_dialect != cxx98 && TREE_CODE (scope) == ENUMERAL_TYPE)
4007 t = lookup_enumerator (scope, name);
4008 else if (is_class_type (scope, complain))
4009 t = lookup_member (scope, name, 2, is_type_p);
4011 if (!t)
4012 return error_mark_node;
4013 return t;
4016 /* Subroutine of unqualified_namespace_lookup:
4017 Add the bindings of NAME in used namespaces to VAL.
4018 We are currently looking for names in namespace SCOPE, so we
4019 look through USINGS for using-directives of namespaces
4020 which have SCOPE as a common ancestor with the current scope.
4021 Returns false on errors. */
4023 static bool
4024 lookup_using_namespace (tree name, struct scope_binding *val,
4025 tree usings, tree scope, int flags)
4027 tree iter;
4028 timevar_push (TV_NAME_LOOKUP);
4029 /* Iterate over all used namespaces in current, searching for using
4030 directives of scope. */
4031 for (iter = usings; iter; iter = TREE_CHAIN (iter))
4032 if (TREE_VALUE (iter) == scope)
4034 tree used = ORIGINAL_NAMESPACE (TREE_PURPOSE (iter));
4035 cxx_binding *val1 =
4036 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (used), name);
4037 /* Resolve ambiguities. */
4038 if (val1)
4039 ambiguous_decl (val, val1, flags);
4041 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val->value != error_mark_node);
4044 /* Returns true iff VEC contains TARGET. */
4046 static bool
4047 tree_vec_contains (VEC(tree,gc)* vec, tree target)
4049 unsigned int i;
4050 tree elt;
4051 for (i = 0; VEC_iterate(tree,vec,i,elt); ++i)
4052 if (elt == target)
4053 return true;
4054 return false;
4057 /* [namespace.qual]
4058 Accepts the NAME to lookup and its qualifying SCOPE.
4059 Returns the name/type pair found into the cxx_binding *RESULT,
4060 or false on error. */
4062 static bool
4063 qualified_lookup_using_namespace (tree name, tree scope,
4064 struct scope_binding *result, int flags)
4066 /* Maintain a list of namespaces visited... */
4067 VEC(tree,gc) *seen = NULL;
4068 VEC(tree,gc) *seen_inline = NULL;
4069 /* ... and a list of namespace yet to see. */
4070 VEC(tree,gc) *todo = NULL;
4071 VEC(tree,gc) *todo_maybe = NULL;
4072 VEC(tree,gc) *todo_inline = NULL;
4073 tree usings;
4074 timevar_push (TV_NAME_LOOKUP);
4075 /* Look through namespace aliases. */
4076 scope = ORIGINAL_NAMESPACE (scope);
4078 /* Algorithm: Starting with SCOPE, walk through the the set of used
4079 namespaces. For each used namespace, look through its inline
4080 namespace set for any bindings and usings. If no bindings are found,
4081 add any usings seen to the set of used namespaces. */
4082 VEC_safe_push (tree, gc, todo, scope);
4084 while (VEC_length (tree, todo))
4086 bool found_here;
4087 scope = VEC_pop (tree, todo);
4088 if (tree_vec_contains (seen, scope))
4089 continue;
4090 VEC_safe_push (tree, gc, seen, scope);
4091 VEC_safe_push (tree, gc, todo_inline, scope);
4093 found_here = false;
4094 while (VEC_length (tree, todo_inline))
4096 cxx_binding *binding;
4098 scope = VEC_pop (tree, todo_inline);
4099 if (tree_vec_contains (seen_inline, scope))
4100 continue;
4101 VEC_safe_push (tree, gc, seen_inline, scope);
4103 binding =
4104 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
4105 if (binding)
4107 found_here = true;
4108 ambiguous_decl (result, binding, flags);
4111 for (usings = DECL_NAMESPACE_USING (scope); usings;
4112 usings = TREE_CHAIN (usings))
4113 if (!TREE_INDIRECT_USING (usings))
4115 if (is_associated_namespace (scope, TREE_PURPOSE (usings)))
4116 VEC_safe_push (tree, gc, todo_inline, TREE_PURPOSE (usings));
4117 else
4118 VEC_safe_push (tree, gc, todo_maybe, TREE_PURPOSE (usings));
4122 if (found_here)
4123 VEC_truncate (tree, todo_maybe, 0);
4124 else
4125 while (VEC_length (tree, todo_maybe))
4126 VEC_safe_push (tree, gc, todo, VEC_pop (tree, todo_maybe));
4128 VEC_free (tree,gc,todo);
4129 VEC_free (tree,gc,todo_maybe);
4130 VEC_free (tree,gc,todo_inline);
4131 VEC_free (tree,gc,seen);
4132 VEC_free (tree,gc,seen_inline);
4133 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, result->value != error_mark_node);
4136 /* Subroutine of outer_binding.
4137 Returns TRUE if BINDING is a binding to a template parameter of SCOPE,
4138 FALSE otherwise. */
4140 static bool
4141 binding_to_template_parms_of_scope_p (cxx_binding *binding,
4142 cxx_scope *scope)
4144 tree binding_value;
4146 if (!binding || !scope)
4147 return false;
4149 binding_value = binding->value ? binding->value : binding->type;
4151 return (scope
4152 && scope->this_entity
4153 && get_template_info (scope->this_entity)
4154 && parameter_of_template_p (binding_value,
4155 TI_TEMPLATE (get_template_info \
4156 (scope->this_entity))));
4159 /* Return the innermost non-namespace binding for NAME from a scope
4160 containing BINDING, or, if BINDING is NULL, the current scope.
4161 Please note that for a given template, the template parameters are
4162 considered to be in the scope containing the current scope.
4163 If CLASS_P is false, then class bindings are ignored. */
4165 cxx_binding *
4166 outer_binding (tree name,
4167 cxx_binding *binding,
4168 bool class_p)
4170 cxx_binding *outer;
4171 cxx_scope *scope;
4172 cxx_scope *outer_scope;
4174 if (binding)
4176 scope = binding->scope->level_chain;
4177 outer = binding->previous;
4179 else
4181 scope = current_binding_level;
4182 outer = IDENTIFIER_BINDING (name);
4184 outer_scope = outer ? outer->scope : NULL;
4186 /* Because we create class bindings lazily, we might be missing a
4187 class binding for NAME. If there are any class binding levels
4188 between the LAST_BINDING_LEVEL and the scope in which OUTER was
4189 declared, we must lookup NAME in those class scopes. */
4190 if (class_p)
4191 while (scope && scope != outer_scope && scope->kind != sk_namespace)
4193 if (scope->kind == sk_class)
4195 cxx_binding *class_binding;
4197 class_binding = get_class_binding (name, scope);
4198 if (class_binding)
4200 /* Thread this new class-scope binding onto the
4201 IDENTIFIER_BINDING list so that future lookups
4202 find it quickly. */
4203 class_binding->previous = outer;
4204 if (binding)
4205 binding->previous = class_binding;
4206 else
4207 IDENTIFIER_BINDING (name) = class_binding;
4208 return class_binding;
4211 /* If we are in a member template, the template parms of the member
4212 template are considered to be inside the scope of the containing
4213 class, but within G++ the class bindings are all pushed between the
4214 template parms and the function body. So if the outer binding is
4215 a template parm for the current scope, return it now rather than
4216 look for a class binding. */
4217 if (outer_scope && outer_scope->kind == sk_template_parms
4218 && binding_to_template_parms_of_scope_p (outer, scope))
4219 return outer;
4221 scope = scope->level_chain;
4224 return outer;
4227 /* Return the innermost block-scope or class-scope value binding for
4228 NAME, or NULL_TREE if there is no such binding. */
4230 tree
4231 innermost_non_namespace_value (tree name)
4233 cxx_binding *binding;
4234 binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true);
4235 return binding ? binding->value : NULL_TREE;
4238 /* Look up NAME in the current binding level and its superiors in the
4239 namespace of variables, functions and typedefs. Return a ..._DECL
4240 node of some kind representing its definition if there is only one
4241 such declaration, or return a TREE_LIST with all the overloaded
4242 definitions if there are many, or return 0 if it is undefined.
4243 Hidden name, either friend declaration or built-in function, are
4244 not ignored.
4246 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
4247 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
4248 Otherwise we prefer non-TYPE_DECLs.
4250 If NONCLASS is nonzero, bindings in class scopes are ignored. If
4251 BLOCK_P is false, bindings in block scopes are ignored. */
4253 tree
4254 lookup_name_real (tree name, int prefer_type, int nonclass, bool block_p,
4255 int namespaces_only, int flags)
4257 cxx_binding *iter;
4258 tree val = NULL_TREE;
4260 timevar_push (TV_NAME_LOOKUP);
4261 /* Conversion operators are handled specially because ordinary
4262 unqualified name lookup will not find template conversion
4263 operators. */
4264 if (IDENTIFIER_TYPENAME_P (name))
4266 struct cp_binding_level *level;
4268 for (level = current_binding_level;
4269 level && level->kind != sk_namespace;
4270 level = level->level_chain)
4272 tree class_type;
4273 tree operators;
4275 /* A conversion operator can only be declared in a class
4276 scope. */
4277 if (level->kind != sk_class)
4278 continue;
4280 /* Lookup the conversion operator in the class. */
4281 class_type = level->this_entity;
4282 operators = lookup_fnfields (class_type, name, /*protect=*/0);
4283 if (operators)
4284 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, operators);
4287 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4290 flags |= lookup_flags (prefer_type, namespaces_only);
4292 /* First, look in non-namespace scopes. */
4294 if (current_class_type == NULL_TREE)
4295 nonclass = 1;
4297 if (block_p || !nonclass)
4298 for (iter = outer_binding (name, NULL, !nonclass);
4299 iter;
4300 iter = outer_binding (name, iter, !nonclass))
4302 tree binding;
4304 /* Skip entities we don't want. */
4305 if (LOCAL_BINDING_P (iter) ? !block_p : nonclass)
4306 continue;
4308 /* If this is the kind of thing we're looking for, we're done. */
4309 if (qualify_lookup (iter->value, flags))
4310 binding = iter->value;
4311 else if ((flags & LOOKUP_PREFER_TYPES)
4312 && qualify_lookup (iter->type, flags))
4313 binding = iter->type;
4314 else
4315 binding = NULL_TREE;
4317 if (binding)
4319 if (hidden_name_p (binding))
4321 /* A non namespace-scope binding can only be hidden in the
4322 presence of a local class, due to friend declarations.
4324 In particular, consider:
4326 struct C;
4327 void f() {
4328 struct A {
4329 friend struct B;
4330 friend struct C;
4331 void g() {
4332 B* b; // error: B is hidden
4333 C* c; // OK, finds ::C
4336 B *b; // error: B is hidden
4337 C *c; // OK, finds ::C
4338 struct B {};
4339 B *bb; // OK
4342 The standard says that "B" is a local class in "f"
4343 (but not nested within "A") -- but that name lookup
4344 for "B" does not find this declaration until it is
4345 declared directly with "f".
4347 In particular:
4349 [class.friend]
4351 If a friend declaration appears in a local class and
4352 the name specified is an unqualified name, a prior
4353 declaration is looked up without considering scopes
4354 that are outside the innermost enclosing non-class
4355 scope. For a friend function declaration, if there is
4356 no prior declaration, the program is ill-formed. For a
4357 friend class declaration, if there is no prior
4358 declaration, the class that is specified belongs to the
4359 innermost enclosing non-class scope, but if it is
4360 subsequently referenced, its name is not found by name
4361 lookup until a matching declaration is provided in the
4362 innermost enclosing nonclass scope.
4364 So just keep looking for a non-hidden binding.
4366 gcc_assert (TREE_CODE (binding) == TYPE_DECL);
4367 continue;
4369 val = binding;
4370 break;
4374 /* Now lookup in namespace scopes. */
4375 if (!val)
4376 val = unqualified_namespace_lookup (name, flags);
4378 /* If we have a single function from a using decl, pull it out. */
4379 if (val && TREE_CODE (val) == OVERLOAD && !really_overloaded_fn (val))
4380 val = OVL_FUNCTION (val);
4382 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
4385 tree
4386 lookup_name_nonclass (tree name)
4388 return lookup_name_real (name, 0, 1, /*block_p=*/true, 0, LOOKUP_COMPLAIN);
4391 tree
4392 lookup_function_nonclass (tree name, VEC(tree,gc) *args, bool block_p)
4394 return
4395 lookup_arg_dependent (name,
4396 lookup_name_real (name, 0, 1, block_p, 0,
4397 LOOKUP_COMPLAIN),
4398 args);
4401 tree
4402 lookup_name (tree name)
4404 return lookup_name_real (name, 0, 0, /*block_p=*/true, 0, LOOKUP_COMPLAIN);
4407 tree
4408 lookup_name_prefer_type (tree name, int prefer_type)
4410 return lookup_name_real (name, prefer_type, 0, /*block_p=*/true,
4411 0, LOOKUP_COMPLAIN);
4414 /* Look up NAME for type used in elaborated name specifier in
4415 the scopes given by SCOPE. SCOPE can be either TS_CURRENT or
4416 TS_WITHIN_ENCLOSING_NON_CLASS. Although not implied by the
4417 name, more scopes are checked if cleanup or template parameter
4418 scope is encountered.
4420 Unlike lookup_name_real, we make sure that NAME is actually
4421 declared in the desired scope, not from inheritance, nor using
4422 directive. For using declaration, there is DR138 still waiting
4423 to be resolved. Hidden name coming from an earlier friend
4424 declaration is also returned.
4426 A TYPE_DECL best matching the NAME is returned. Catching error
4427 and issuing diagnostics are caller's responsibility. */
4429 tree
4430 lookup_type_scope (tree name, tag_scope scope)
4432 cxx_binding *iter = NULL;
4433 tree val = NULL_TREE;
4435 timevar_push (TV_NAME_LOOKUP);
4437 /* Look in non-namespace scope first. */
4438 if (current_binding_level->kind != sk_namespace)
4439 iter = outer_binding (name, NULL, /*class_p=*/ true);
4440 for (; iter; iter = outer_binding (name, iter, /*class_p=*/ true))
4442 /* Check if this is the kind of thing we're looking for.
4443 If SCOPE is TS_CURRENT, also make sure it doesn't come from
4444 base class. For ITER->VALUE, we can simply use
4445 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use
4446 our own check.
4448 We check ITER->TYPE before ITER->VALUE in order to handle
4449 typedef struct C {} C;
4450 correctly. */
4452 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES)
4453 && (scope != ts_current
4454 || LOCAL_BINDING_P (iter)
4455 || DECL_CONTEXT (iter->type) == iter->scope->this_entity))
4456 val = iter->type;
4457 else if ((scope != ts_current
4458 || !INHERITED_VALUE_BINDING_P (iter))
4459 && qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
4460 val = iter->value;
4462 if (val)
4463 break;
4466 /* Look in namespace scope. */
4467 if (!val)
4469 iter = cxx_scope_find_binding_for_name
4470 (NAMESPACE_LEVEL (current_decl_namespace ()), name);
4472 if (iter)
4474 /* If this is the kind of thing we're looking for, we're done. */
4475 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES))
4476 val = iter->type;
4477 else if (qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
4478 val = iter->value;
4483 /* Type found, check if it is in the allowed scopes, ignoring cleanup
4484 and template parameter scopes. */
4485 if (val)
4487 struct cp_binding_level *b = current_binding_level;
4488 while (b)
4490 if (iter->scope == b)
4491 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
4493 if (b->kind == sk_cleanup || b->kind == sk_template_parms
4494 || b->kind == sk_function_parms)
4495 b = b->level_chain;
4496 else if (b->kind == sk_class
4497 && scope == ts_within_enclosing_non_class)
4498 b = b->level_chain;
4499 else
4500 break;
4504 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4507 /* Similar to `lookup_name' but look only in the innermost non-class
4508 binding level. */
4510 tree
4511 lookup_name_innermost_nonclass_level (tree name)
4513 struct cp_binding_level *b;
4514 tree t = NULL_TREE;
4516 timevar_push (TV_NAME_LOOKUP);
4517 b = innermost_nonclass_level ();
4519 if (b->kind == sk_namespace)
4521 t = IDENTIFIER_NAMESPACE_VALUE (name);
4523 /* extern "C" function() */
4524 if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
4525 t = TREE_VALUE (t);
4527 else if (IDENTIFIER_BINDING (name)
4528 && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
4530 cxx_binding *binding;
4531 binding = IDENTIFIER_BINDING (name);
4532 while (1)
4534 if (binding->scope == b
4535 && !(TREE_CODE (binding->value) == VAR_DECL
4536 && DECL_DEAD_FOR_LOCAL (binding->value)))
4537 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, binding->value);
4539 if (b->kind == sk_cleanup)
4540 b = b->level_chain;
4541 else
4542 break;
4546 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4549 /* Returns true iff DECL is a block-scope extern declaration of a function
4550 or variable. */
4552 bool
4553 is_local_extern (tree decl)
4555 cxx_binding *binding;
4557 /* For functions, this is easy. */
4558 if (TREE_CODE (decl) == FUNCTION_DECL)
4559 return DECL_LOCAL_FUNCTION_P (decl);
4561 if (TREE_CODE (decl) != VAR_DECL)
4562 return false;
4563 if (!current_function_decl)
4564 return false;
4566 /* For variables, this is not easy. We need to look at the binding stack
4567 for the identifier to see whether the decl we have is a local. */
4568 for (binding = IDENTIFIER_BINDING (DECL_NAME (decl));
4569 binding && binding->scope->kind != sk_namespace;
4570 binding = binding->previous)
4571 if (binding->value == decl)
4572 return LOCAL_BINDING_P (binding);
4574 return false;
4577 /* Like lookup_name_innermost_nonclass_level, but for types. */
4579 static tree
4580 lookup_type_current_level (tree name)
4582 tree t = NULL_TREE;
4584 timevar_push (TV_NAME_LOOKUP);
4585 gcc_assert (current_binding_level->kind != sk_namespace);
4587 if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
4588 && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
4590 struct cp_binding_level *b = current_binding_level;
4591 while (1)
4593 if (purpose_member (name, b->type_shadowed))
4594 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
4595 REAL_IDENTIFIER_TYPE_VALUE (name));
4596 if (b->kind == sk_cleanup)
4597 b = b->level_chain;
4598 else
4599 break;
4603 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4606 /* [basic.lookup.koenig] */
4607 /* A nonzero return value in the functions below indicates an error. */
4609 struct arg_lookup
4611 tree name;
4612 VEC(tree,gc) *args;
4613 VEC(tree,gc) *namespaces;
4614 VEC(tree,gc) *classes;
4615 tree functions;
4618 static bool arg_assoc (struct arg_lookup*, tree);
4619 static bool arg_assoc_args (struct arg_lookup*, tree);
4620 static bool arg_assoc_args_vec (struct arg_lookup*, VEC(tree,gc) *);
4621 static bool arg_assoc_type (struct arg_lookup*, tree);
4622 static bool add_function (struct arg_lookup *, tree);
4623 static bool arg_assoc_namespace (struct arg_lookup *, tree);
4624 static bool arg_assoc_class_only (struct arg_lookup *, tree);
4625 static bool arg_assoc_bases (struct arg_lookup *, tree);
4626 static bool arg_assoc_class (struct arg_lookup *, tree);
4627 static bool arg_assoc_template_arg (struct arg_lookup*, tree);
4629 /* Add a function to the lookup structure.
4630 Returns true on error. */
4632 static bool
4633 add_function (struct arg_lookup *k, tree fn)
4635 /* We used to check here to see if the function was already in the list,
4636 but that's O(n^2), which is just too expensive for function lookup.
4637 Now we deal with the occasional duplicate in joust. In doing this, we
4638 assume that the number of duplicates will be small compared to the
4639 total number of functions being compared, which should usually be the
4640 case. */
4642 if (!is_overloaded_fn (fn))
4643 /* All names except those of (possibly overloaded) functions and
4644 function templates are ignored. */;
4645 else if (!k->functions)
4646 k->functions = fn;
4647 else if (fn == k->functions)
4649 else
4650 k->functions = build_overload (fn, k->functions);
4652 return false;
4655 /* Returns true iff CURRENT has declared itself to be an associated
4656 namespace of SCOPE via a strong using-directive (or transitive chain
4657 thereof). Both are namespaces. */
4659 bool
4660 is_associated_namespace (tree current, tree scope)
4662 tree seen = NULL_TREE;
4663 tree todo = NULL_TREE;
4664 tree t;
4665 while (1)
4667 if (scope == current)
4668 return true;
4669 seen = tree_cons (scope, NULL_TREE, seen);
4670 for (t = DECL_NAMESPACE_ASSOCIATIONS (scope); t; t = TREE_CHAIN (t))
4671 if (!purpose_member (TREE_PURPOSE (t), seen))
4672 todo = tree_cons (TREE_PURPOSE (t), NULL_TREE, todo);
4673 if (todo)
4675 scope = TREE_PURPOSE (todo);
4676 todo = TREE_CHAIN (todo);
4678 else
4679 return false;
4683 /* Add functions of a namespace to the lookup structure.
4684 Returns true on error. */
4686 static bool
4687 arg_assoc_namespace (struct arg_lookup *k, tree scope)
4689 tree value;
4691 if (vec_member (scope, k->namespaces))
4692 return false;
4693 VEC_safe_push (tree, gc, k->namespaces, scope);
4695 /* Check out our super-users. */
4696 for (value = DECL_NAMESPACE_ASSOCIATIONS (scope); value;
4697 value = TREE_CHAIN (value))
4698 if (arg_assoc_namespace (k, TREE_PURPOSE (value)))
4699 return true;
4701 /* Also look down into inline namespaces. */
4702 for (value = DECL_NAMESPACE_USING (scope); value;
4703 value = TREE_CHAIN (value))
4704 if (is_associated_namespace (scope, TREE_PURPOSE (value)))
4705 if (arg_assoc_namespace (k, TREE_PURPOSE (value)))
4706 return true;
4708 value = namespace_binding (k->name, scope);
4709 if (!value)
4710 return false;
4712 for (; value; value = OVL_NEXT (value))
4714 /* We don't want to find arbitrary hidden functions via argument
4715 dependent lookup. We only want to find friends of associated
4716 classes, which we'll do via arg_assoc_class. */
4717 if (hidden_name_p (OVL_CURRENT (value)))
4718 continue;
4720 if (add_function (k, OVL_CURRENT (value)))
4721 return true;
4724 return false;
4727 /* Adds everything associated with a template argument to the lookup
4728 structure. Returns true on error. */
4730 static bool
4731 arg_assoc_template_arg (struct arg_lookup *k, tree arg)
4733 /* [basic.lookup.koenig]
4735 If T is a template-id, its associated namespaces and classes are
4736 ... the namespaces and classes associated with the types of the
4737 template arguments provided for template type parameters
4738 (excluding template template parameters); the namespaces in which
4739 any template template arguments are defined; and the classes in
4740 which any member templates used as template template arguments
4741 are defined. [Note: non-type template arguments do not
4742 contribute to the set of associated namespaces. ] */
4744 /* Consider first template template arguments. */
4745 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
4746 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
4747 return false;
4748 else if (TREE_CODE (arg) == TEMPLATE_DECL)
4750 tree ctx = CP_DECL_CONTEXT (arg);
4752 /* It's not a member template. */
4753 if (TREE_CODE (ctx) == NAMESPACE_DECL)
4754 return arg_assoc_namespace (k, ctx);
4755 /* Otherwise, it must be member template. */
4756 else
4757 return arg_assoc_class_only (k, ctx);
4759 /* It's an argument pack; handle it recursively. */
4760 else if (ARGUMENT_PACK_P (arg))
4762 tree args = ARGUMENT_PACK_ARGS (arg);
4763 int i, len = TREE_VEC_LENGTH (args);
4764 for (i = 0; i < len; ++i)
4765 if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, i)))
4766 return true;
4768 return false;
4770 /* It's not a template template argument, but it is a type template
4771 argument. */
4772 else if (TYPE_P (arg))
4773 return arg_assoc_type (k, arg);
4774 /* It's a non-type template argument. */
4775 else
4776 return false;
4779 /* Adds the class and its friends to the lookup structure.
4780 Returns true on error. */
4782 static bool
4783 arg_assoc_class_only (struct arg_lookup *k, tree type)
4785 tree list, friends, context;
4787 /* Backend-built structures, such as __builtin_va_list, aren't
4788 affected by all this. */
4789 if (!CLASS_TYPE_P (type))
4790 return false;
4792 context = decl_namespace_context (type);
4793 if (arg_assoc_namespace (k, context))
4794 return true;
4796 complete_type (type);
4798 /* Process friends. */
4799 for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
4800 list = TREE_CHAIN (list))
4801 if (k->name == FRIEND_NAME (list))
4802 for (friends = FRIEND_DECLS (list); friends;
4803 friends = TREE_CHAIN (friends))
4805 tree fn = TREE_VALUE (friends);
4807 /* Only interested in global functions with potentially hidden
4808 (i.e. unqualified) declarations. */
4809 if (CP_DECL_CONTEXT (fn) != context)
4810 continue;
4811 /* Template specializations are never found by name lookup.
4812 (Templates themselves can be found, but not template
4813 specializations.) */
4814 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn))
4815 continue;
4816 if (add_function (k, fn))
4817 return true;
4820 return false;
4823 /* Adds the class and its bases to the lookup structure.
4824 Returns true on error. */
4826 static bool
4827 arg_assoc_bases (struct arg_lookup *k, tree type)
4829 if (arg_assoc_class_only (k, type))
4830 return true;
4832 if (TYPE_BINFO (type))
4834 /* Process baseclasses. */
4835 tree binfo, base_binfo;
4836 int i;
4838 for (binfo = TYPE_BINFO (type), i = 0;
4839 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
4840 if (arg_assoc_bases (k, BINFO_TYPE (base_binfo)))
4841 return true;
4844 return false;
4847 /* Adds everything associated with a class argument type to the lookup
4848 structure. Returns true on error.
4850 If T is a class type (including unions), its associated classes are: the
4851 class itself; the class of which it is a member, if any; and its direct
4852 and indirect base classes. Its associated namespaces are the namespaces
4853 of which its associated classes are members. Furthermore, if T is a
4854 class template specialization, its associated namespaces and classes
4855 also include: the namespaces and classes associated with the types of
4856 the template arguments provided for template type parameters (excluding
4857 template template parameters); the namespaces of which any template
4858 template arguments are members; and the classes of which any member
4859 templates used as template template arguments are members. [ Note:
4860 non-type template arguments do not contribute to the set of associated
4861 namespaces. --end note] */
4863 static bool
4864 arg_assoc_class (struct arg_lookup *k, tree type)
4866 tree list;
4867 int i;
4869 /* Backend build structures, such as __builtin_va_list, aren't
4870 affected by all this. */
4871 if (!CLASS_TYPE_P (type))
4872 return false;
4874 if (vec_member (type, k->classes))
4875 return false;
4876 VEC_safe_push (tree, gc, k->classes, type);
4878 if (TYPE_CLASS_SCOPE_P (type)
4879 && arg_assoc_class_only (k, TYPE_CONTEXT (type)))
4880 return true;
4882 if (arg_assoc_bases (k, type))
4883 return true;
4885 /* Process template arguments. */
4886 if (CLASSTYPE_TEMPLATE_INFO (type)
4887 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
4889 list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
4890 for (i = 0; i < TREE_VEC_LENGTH (list); ++i)
4891 if (arg_assoc_template_arg (k, TREE_VEC_ELT (list, i)))
4892 return true;
4895 return false;
4898 /* Adds everything associated with a given type.
4899 Returns 1 on error. */
4901 static bool
4902 arg_assoc_type (struct arg_lookup *k, tree type)
4904 /* As we do not get the type of non-type dependent expressions
4905 right, we can end up with such things without a type. */
4906 if (!type)
4907 return false;
4909 if (TYPE_PTRMEM_P (type))
4911 /* Pointer to member: associate class type and value type. */
4912 if (arg_assoc_type (k, TYPE_PTRMEM_CLASS_TYPE (type)))
4913 return true;
4914 return arg_assoc_type (k, TYPE_PTRMEM_POINTED_TO_TYPE (type));
4916 else switch (TREE_CODE (type))
4918 case ERROR_MARK:
4919 return false;
4920 case VOID_TYPE:
4921 case INTEGER_TYPE:
4922 case REAL_TYPE:
4923 case COMPLEX_TYPE:
4924 case VECTOR_TYPE:
4925 case BOOLEAN_TYPE:
4926 case FIXED_POINT_TYPE:
4927 case DECLTYPE_TYPE:
4928 return false;
4929 case RECORD_TYPE:
4930 if (TYPE_PTRMEMFUNC_P (type))
4931 return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type));
4932 case UNION_TYPE:
4933 return arg_assoc_class (k, type);
4934 case POINTER_TYPE:
4935 case REFERENCE_TYPE:
4936 case ARRAY_TYPE:
4937 return arg_assoc_type (k, TREE_TYPE (type));
4938 case ENUMERAL_TYPE:
4939 if (TYPE_CLASS_SCOPE_P (type)
4940 && arg_assoc_class_only (k, TYPE_CONTEXT (type)))
4941 return true;
4942 return arg_assoc_namespace (k, decl_namespace_context (type));
4943 case METHOD_TYPE:
4944 /* The basetype is referenced in the first arg type, so just
4945 fall through. */
4946 case FUNCTION_TYPE:
4947 /* Associate the parameter types. */
4948 if (arg_assoc_args (k, TYPE_ARG_TYPES (type)))
4949 return true;
4950 /* Associate the return type. */
4951 return arg_assoc_type (k, TREE_TYPE (type));
4952 case TEMPLATE_TYPE_PARM:
4953 case BOUND_TEMPLATE_TEMPLATE_PARM:
4954 return false;
4955 case TYPENAME_TYPE:
4956 return false;
4957 case LANG_TYPE:
4958 gcc_assert (type == unknown_type_node
4959 || NULLPTR_TYPE_P (type)
4960 || type == init_list_type_node);
4961 return false;
4962 case TYPE_PACK_EXPANSION:
4963 return arg_assoc_type (k, PACK_EXPANSION_PATTERN (type));
4965 default:
4966 gcc_unreachable ();
4968 return false;
4971 /* Adds everything associated with arguments. Returns true on error. */
4973 static bool
4974 arg_assoc_args (struct arg_lookup *k, tree args)
4976 for (; args; args = TREE_CHAIN (args))
4977 if (arg_assoc (k, TREE_VALUE (args)))
4978 return true;
4979 return false;
4982 /* Adds everything associated with an argument vector. Returns true
4983 on error. */
4985 static bool
4986 arg_assoc_args_vec (struct arg_lookup *k, VEC(tree,gc) *args)
4988 unsigned int ix;
4989 tree arg;
4991 for (ix = 0; VEC_iterate (tree, args, ix, arg); ++ix)
4992 if (arg_assoc (k, arg))
4993 return true;
4994 return false;
4997 /* Adds everything associated with a given tree_node. Returns 1 on error. */
4999 static bool
5000 arg_assoc (struct arg_lookup *k, tree n)
5002 if (n == error_mark_node)
5003 return false;
5005 if (TYPE_P (n))
5006 return arg_assoc_type (k, n);
5008 if (! type_unknown_p (n))
5009 return arg_assoc_type (k, TREE_TYPE (n));
5011 if (TREE_CODE (n) == ADDR_EXPR)
5012 n = TREE_OPERAND (n, 0);
5013 if (TREE_CODE (n) == COMPONENT_REF)
5014 n = TREE_OPERAND (n, 1);
5015 if (TREE_CODE (n) == OFFSET_REF)
5016 n = TREE_OPERAND (n, 1);
5017 while (TREE_CODE (n) == TREE_LIST)
5018 n = TREE_VALUE (n);
5019 if (TREE_CODE (n) == BASELINK)
5020 n = BASELINK_FUNCTIONS (n);
5022 if (TREE_CODE (n) == FUNCTION_DECL)
5023 return arg_assoc_type (k, TREE_TYPE (n));
5024 if (TREE_CODE (n) == TEMPLATE_ID_EXPR)
5026 /* The working paper doesn't currently say how to handle template-id
5027 arguments. The sensible thing would seem to be to handle the list
5028 of template candidates like a normal overload set, and handle the
5029 template arguments like we do for class template
5030 specializations. */
5031 tree templ = TREE_OPERAND (n, 0);
5032 tree args = TREE_OPERAND (n, 1);
5033 int ix;
5035 /* First the templates. */
5036 if (arg_assoc (k, templ))
5037 return true;
5039 /* Now the arguments. */
5040 if (args)
5041 for (ix = TREE_VEC_LENGTH (args); ix--;)
5042 if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, ix)) == 1)
5043 return true;
5045 else if (TREE_CODE (n) == OVERLOAD)
5047 for (; n; n = OVL_CHAIN (n))
5048 if (arg_assoc_type (k, TREE_TYPE (OVL_FUNCTION (n))))
5049 return true;
5052 return false;
5055 /* Performs Koenig lookup depending on arguments, where fns
5056 are the functions found in normal lookup. */
5058 tree
5059 lookup_arg_dependent (tree name, tree fns, VEC(tree,gc) *args)
5061 struct arg_lookup k;
5063 timevar_push (TV_NAME_LOOKUP);
5065 /* Remove any hidden friend functions from the list of functions
5066 found so far. They will be added back by arg_assoc_class as
5067 appropriate. */
5068 fns = remove_hidden_names (fns);
5070 k.name = name;
5071 k.args = args;
5072 k.functions = fns;
5073 k.classes = make_tree_vector ();
5075 /* We previously performed an optimization here by setting
5076 NAMESPACES to the current namespace when it was safe. However, DR
5077 164 says that namespaces that were already searched in the first
5078 stage of template processing are searched again (potentially
5079 picking up later definitions) in the second stage. */
5080 k.namespaces = make_tree_vector ();
5082 arg_assoc_args_vec (&k, args);
5084 fns = k.functions;
5086 if (fns
5087 && TREE_CODE (fns) != VAR_DECL
5088 && !is_overloaded_fn (fns))
5090 error ("argument dependent lookup finds %q+D", fns);
5091 error (" in call to %qD", name);
5092 fns = error_mark_node;
5095 release_tree_vector (k.classes);
5096 release_tree_vector (k.namespaces);
5098 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, fns);
5101 /* Add namespace to using_directives. Return NULL_TREE if nothing was
5102 changed (i.e. there was already a directive), or the fresh
5103 TREE_LIST otherwise. */
5105 static tree
5106 push_using_directive (tree used)
5108 tree ud = current_binding_level->using_directives;
5109 tree iter, ancestor;
5111 timevar_push (TV_NAME_LOOKUP);
5112 /* Check if we already have this. */
5113 if (purpose_member (used, ud) != NULL_TREE)
5114 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5116 ancestor = namespace_ancestor (current_decl_namespace (), used);
5117 ud = current_binding_level->using_directives;
5118 ud = tree_cons (used, ancestor, ud);
5119 current_binding_level->using_directives = ud;
5121 /* Recursively add all namespaces used. */
5122 for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
5123 push_using_directive (TREE_PURPOSE (iter));
5125 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ud);
5128 /* The type TYPE is being declared. If it is a class template, or a
5129 specialization of a class template, do any processing required and
5130 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
5131 being declared a friend. B is the binding level at which this TYPE
5132 should be bound.
5134 Returns the TYPE_DECL for TYPE, which may have been altered by this
5135 processing. */
5137 static tree
5138 maybe_process_template_type_declaration (tree type, int is_friend,
5139 cxx_scope *b)
5141 tree decl = TYPE_NAME (type);
5143 if (processing_template_parmlist)
5144 /* You can't declare a new template type in a template parameter
5145 list. But, you can declare a non-template type:
5147 template <class A*> struct S;
5149 is a forward-declaration of `A'. */
5151 else if (b->kind == sk_namespace
5152 && current_binding_level->kind != sk_namespace)
5153 /* If this new type is being injected into a containing scope,
5154 then it's not a template type. */
5156 else
5158 gcc_assert (MAYBE_CLASS_TYPE_P (type)
5159 || TREE_CODE (type) == ENUMERAL_TYPE);
5161 if (processing_template_decl)
5163 /* This may change after the call to
5164 push_template_decl_real, but we want the original value. */
5165 tree name = DECL_NAME (decl);
5167 decl = push_template_decl_real (decl, is_friend);
5168 if (decl == error_mark_node)
5169 return error_mark_node;
5171 /* If the current binding level is the binding level for the
5172 template parameters (see the comment in
5173 begin_template_parm_list) and the enclosing level is a class
5174 scope, and we're not looking at a friend, push the
5175 declaration of the member class into the class scope. In the
5176 friend case, push_template_decl will already have put the
5177 friend into global scope, if appropriate. */
5178 if (TREE_CODE (type) != ENUMERAL_TYPE
5179 && !is_friend && b->kind == sk_template_parms
5180 && b->level_chain->kind == sk_class)
5182 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
5184 if (!COMPLETE_TYPE_P (current_class_type))
5186 maybe_add_class_template_decl_list (current_class_type,
5187 type, /*friend_p=*/0);
5188 /* Put this UTD in the table of UTDs for the class. */
5189 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
5190 CLASSTYPE_NESTED_UTDS (current_class_type) =
5191 binding_table_new (SCOPE_DEFAULT_HT_SIZE);
5193 binding_table_insert
5194 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
5200 return decl;
5203 /* Push a tag name NAME for struct/class/union/enum type TYPE. In case
5204 that the NAME is a class template, the tag is processed but not pushed.
5206 The pushed scope depend on the SCOPE parameter:
5207 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
5208 scope.
5209 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
5210 non-template-parameter scope. This case is needed for forward
5211 declarations.
5212 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
5213 TS_GLOBAL case except that names within template-parameter scopes
5214 are not pushed at all.
5216 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
5218 tree
5219 pushtag (tree name, tree type, tag_scope scope)
5221 struct cp_binding_level *b;
5222 tree decl;
5224 timevar_push (TV_NAME_LOOKUP);
5225 b = current_binding_level;
5226 while (/* Cleanup scopes are not scopes from the point of view of
5227 the language. */
5228 b->kind == sk_cleanup
5229 /* Neither are function parameter scopes. */
5230 || b->kind == sk_function_parms
5231 /* Neither are the scopes used to hold template parameters
5232 for an explicit specialization. For an ordinary template
5233 declaration, these scopes are not scopes from the point of
5234 view of the language. */
5235 || (b->kind == sk_template_parms
5236 && (b->explicit_spec_p || scope == ts_global))
5237 || (b->kind == sk_class
5238 && (scope != ts_current
5239 /* We may be defining a new type in the initializer
5240 of a static member variable. We allow this when
5241 not pedantic, and it is particularly useful for
5242 type punning via an anonymous union. */
5243 || COMPLETE_TYPE_P (b->this_entity))))
5244 b = b->level_chain;
5246 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
5248 /* Do C++ gratuitous typedefing. */
5249 if (IDENTIFIER_TYPE_VALUE (name) != type)
5251 tree tdef;
5252 int in_class = 0;
5253 tree context = TYPE_CONTEXT (type);
5255 if (! context)
5257 tree cs = current_scope ();
5259 if (scope == ts_current
5260 || (cs && TREE_CODE (cs) == FUNCTION_DECL))
5261 context = cs;
5262 else if (cs != NULL_TREE && TYPE_P (cs))
5263 /* When declaring a friend class of a local class, we want
5264 to inject the newly named class into the scope
5265 containing the local class, not the namespace
5266 scope. */
5267 context = decl_function_context (get_type_decl (cs));
5269 if (!context)
5270 context = current_namespace;
5272 if (b->kind == sk_class
5273 || (b->kind == sk_template_parms
5274 && b->level_chain->kind == sk_class))
5275 in_class = 1;
5277 if (current_lang_name == lang_name_java)
5278 TYPE_FOR_JAVA (type) = 1;
5280 tdef = create_implicit_typedef (name, type);
5281 DECL_CONTEXT (tdef) = FROB_CONTEXT (context);
5282 if (scope == ts_within_enclosing_non_class)
5284 /* This is a friend. Make this TYPE_DECL node hidden from
5285 ordinary name lookup. Its corresponding TEMPLATE_DECL
5286 will be marked in push_template_decl_real. */
5287 retrofit_lang_decl (tdef);
5288 DECL_ANTICIPATED (tdef) = 1;
5289 DECL_FRIEND_P (tdef) = 1;
5292 decl = maybe_process_template_type_declaration
5293 (type, scope == ts_within_enclosing_non_class, b);
5294 if (decl == error_mark_node)
5295 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
5297 if (b->kind == sk_class)
5299 if (!TYPE_BEING_DEFINED (current_class_type))
5300 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5302 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
5303 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
5304 class. But if it's a member template class, we want
5305 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
5306 later. */
5307 finish_member_declaration (decl);
5308 else
5309 pushdecl_class_level (decl);
5311 else if (b->kind != sk_template_parms)
5313 decl = pushdecl_with_scope (decl, b, /*is_friend=*/false);
5314 if (decl == error_mark_node)
5315 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
5318 if (! in_class)
5319 set_identifier_type_value_with_scope (name, tdef, b);
5321 TYPE_CONTEXT (type) = DECL_CONTEXT (decl);
5323 /* If this is a local class, keep track of it. We need this
5324 information for name-mangling, and so that it is possible to
5325 find all function definitions in a translation unit in a
5326 convenient way. (It's otherwise tricky to find a member
5327 function definition it's only pointed to from within a local
5328 class.) */
5329 if (TYPE_CONTEXT (type)
5330 && TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL)
5331 VEC_safe_push (tree, gc, local_classes, type);
5333 if (b->kind == sk_class
5334 && !COMPLETE_TYPE_P (current_class_type))
5336 maybe_add_class_template_decl_list (current_class_type,
5337 type, /*friend_p=*/0);
5339 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
5340 CLASSTYPE_NESTED_UTDS (current_class_type)
5341 = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
5343 binding_table_insert
5344 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
5347 decl = TYPE_NAME (type);
5348 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
5350 /* Set type visibility now if this is a forward declaration. */
5351 TREE_PUBLIC (decl) = 1;
5352 determine_visibility (decl);
5354 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, type);
5357 /* Subroutines for reverting temporarily to top-level for instantiation
5358 of templates and such. We actually need to clear out the class- and
5359 local-value slots of all identifiers, so that only the global values
5360 are at all visible. Simply setting current_binding_level to the global
5361 scope isn't enough, because more binding levels may be pushed. */
5362 struct saved_scope *scope_chain;
5364 /* If ID has not already been marked, add an appropriate binding to
5365 *OLD_BINDINGS. */
5367 static void
5368 store_binding (tree id, VEC(cxx_saved_binding,gc) **old_bindings)
5370 cxx_saved_binding *saved;
5372 if (!id || !IDENTIFIER_BINDING (id))
5373 return;
5375 if (IDENTIFIER_MARKED (id))
5376 return;
5378 IDENTIFIER_MARKED (id) = 1;
5380 saved = VEC_safe_push (cxx_saved_binding, gc, *old_bindings, NULL);
5381 saved->identifier = id;
5382 saved->binding = IDENTIFIER_BINDING (id);
5383 saved->real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
5384 IDENTIFIER_BINDING (id) = NULL;
5387 static void
5388 store_bindings (tree names, VEC(cxx_saved_binding,gc) **old_bindings)
5390 tree t;
5392 timevar_push (TV_NAME_LOOKUP);
5393 for (t = names; t; t = TREE_CHAIN (t))
5395 tree id;
5397 if (TREE_CODE (t) == TREE_LIST)
5398 id = TREE_PURPOSE (t);
5399 else
5400 id = DECL_NAME (t);
5402 store_binding (id, old_bindings);
5404 timevar_pop (TV_NAME_LOOKUP);
5407 /* Like store_bindings, but NAMES is a vector of cp_class_binding
5408 objects, rather than a TREE_LIST. */
5410 static void
5411 store_class_bindings (VEC(cp_class_binding,gc) *names,
5412 VEC(cxx_saved_binding,gc) **old_bindings)
5414 size_t i;
5415 cp_class_binding *cb;
5417 timevar_push (TV_NAME_LOOKUP);
5418 for (i = 0; VEC_iterate(cp_class_binding, names, i, cb); ++i)
5419 store_binding (cb->identifier, old_bindings);
5420 timevar_pop (TV_NAME_LOOKUP);
5423 void
5424 push_to_top_level (void)
5426 struct saved_scope *s;
5427 struct cp_binding_level *b;
5428 cxx_saved_binding *sb;
5429 size_t i;
5430 bool need_pop;
5432 timevar_push (TV_NAME_LOOKUP);
5433 s = ggc_alloc_cleared_saved_scope ();
5435 b = scope_chain ? current_binding_level : 0;
5437 /* If we're in the middle of some function, save our state. */
5438 if (cfun)
5440 need_pop = true;
5441 push_function_context ();
5443 else
5444 need_pop = false;
5446 if (scope_chain && previous_class_level)
5447 store_class_bindings (previous_class_level->class_shadowed,
5448 &s->old_bindings);
5450 /* Have to include the global scope, because class-scope decls
5451 aren't listed anywhere useful. */
5452 for (; b; b = b->level_chain)
5454 tree t;
5456 /* Template IDs are inserted into the global level. If they were
5457 inserted into namespace level, finish_file wouldn't find them
5458 when doing pending instantiations. Therefore, don't stop at
5459 namespace level, but continue until :: . */
5460 if (global_scope_p (b))
5461 break;
5463 store_bindings (b->names, &s->old_bindings);
5464 /* We also need to check class_shadowed to save class-level type
5465 bindings, since pushclass doesn't fill in b->names. */
5466 if (b->kind == sk_class)
5467 store_class_bindings (b->class_shadowed, &s->old_bindings);
5469 /* Unwind type-value slots back to top level. */
5470 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
5471 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
5474 for (i = 0; VEC_iterate (cxx_saved_binding, s->old_bindings, i, sb); ++i)
5475 IDENTIFIER_MARKED (sb->identifier) = 0;
5477 s->prev = scope_chain;
5478 s->bindings = b;
5479 s->need_pop_function_context = need_pop;
5480 s->function_decl = current_function_decl;
5481 s->unevaluated_operand = cp_unevaluated_operand;
5482 s->inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
5484 scope_chain = s;
5485 current_function_decl = NULL_TREE;
5486 current_lang_base = VEC_alloc (tree, gc, 10);
5487 current_lang_name = lang_name_cplusplus;
5488 current_namespace = global_namespace;
5489 push_class_stack ();
5490 cp_unevaluated_operand = 0;
5491 c_inhibit_evaluation_warnings = 0;
5492 timevar_pop (TV_NAME_LOOKUP);
5495 void
5496 pop_from_top_level (void)
5498 struct saved_scope *s = scope_chain;
5499 cxx_saved_binding *saved;
5500 size_t i;
5502 timevar_push (TV_NAME_LOOKUP);
5503 /* Clear out class-level bindings cache. */
5504 if (previous_class_level)
5505 invalidate_class_lookup_cache ();
5506 pop_class_stack ();
5508 current_lang_base = 0;
5510 scope_chain = s->prev;
5511 for (i = 0; VEC_iterate (cxx_saved_binding, s->old_bindings, i, saved); ++i)
5513 tree id = saved->identifier;
5515 IDENTIFIER_BINDING (id) = saved->binding;
5516 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
5519 /* If we were in the middle of compiling a function, restore our
5520 state. */
5521 if (s->need_pop_function_context)
5522 pop_function_context ();
5523 current_function_decl = s->function_decl;
5524 cp_unevaluated_operand = s->unevaluated_operand;
5525 c_inhibit_evaluation_warnings = s->inhibit_evaluation_warnings;
5526 timevar_pop (TV_NAME_LOOKUP);
5529 /* Pop off extraneous binding levels left over due to syntax errors.
5531 We don't pop past namespaces, as they might be valid. */
5533 void
5534 pop_everything (void)
5536 if (ENABLE_SCOPE_CHECKING)
5537 verbatim ("XXX entering pop_everything ()\n");
5538 while (!toplevel_bindings_p ())
5540 if (current_binding_level->kind == sk_class)
5541 pop_nested_class ();
5542 else
5543 poplevel (0, 0, 0);
5545 if (ENABLE_SCOPE_CHECKING)
5546 verbatim ("XXX leaving pop_everything ()\n");
5549 /* Emit debugging information for using declarations and directives.
5550 If input tree is overloaded fn then emit debug info for all
5551 candidates. */
5553 void
5554 cp_emit_debug_info_for_using (tree t, tree context)
5556 /* Don't try to emit any debug information if we have errors. */
5557 if (seen_error ())
5558 return;
5560 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
5561 of a builtin function. */
5562 if (TREE_CODE (t) == FUNCTION_DECL
5563 && DECL_EXTERNAL (t)
5564 && DECL_BUILT_IN (t))
5565 return;
5567 /* Do not supply context to imported_module_or_decl, if
5568 it is a global namespace. */
5569 if (context == global_namespace)
5570 context = NULL_TREE;
5572 if (BASELINK_P (t))
5573 t = BASELINK_FUNCTIONS (t);
5575 /* FIXME: Handle TEMPLATE_DECLs. */
5576 for (t = OVL_CURRENT (t); t; t = OVL_NEXT (t))
5577 if (TREE_CODE (t) != TEMPLATE_DECL)
5579 if (building_stmt_tree ())
5580 add_stmt (build_stmt (input_location, USING_STMT, t));
5581 else
5582 (*debug_hooks->imported_module_or_decl) (t, NULL_TREE, context, false);
5586 #include "gt-cp-name-lookup.h"