Break out decl.c (2/n)
[official-gcc.git] / gcc / cp / name-lookup.c
blob4f5999803dbfda785c29b9fe8e06680da1a20526
1 /* Definitions for C++ name lookup routines.
2 Copyright (C) 2003 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "cp-tree.h"
28 #include "name-lookup.h"
29 #include "timevar.h"
30 #include "toplev.h"
31 #include "diagnostic.h"
33 static cxx_scope *innermost_nonclass_level (void);
34 static tree select_decl (cxx_binding *, int);
37 /* Compute the chain index of a binding_entry given the HASH value of its
38 name and the total COUNT of chains. COUNT is assumed to be a power
39 of 2. */
41 #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
43 /* A free list of "binding_entry"s awaiting for re-use. */
45 static GTY((deletable(""))) binding_entry free_binding_entry = NULL;
47 /* Create a binding_entry object for (NAME, TYPE). */
49 static inline binding_entry
50 binding_entry_make (tree name, tree type)
52 binding_entry entry;
54 if (free_binding_entry)
56 entry = free_binding_entry;
57 free_binding_entry = entry->chain;
59 else
60 entry = ggc_alloc (sizeof (struct binding_entry_s));
62 entry->name = name;
63 entry->type = type;
64 entry->chain = NULL;
66 return entry;
69 /* Put ENTRY back on the free list. */
71 static inline void
72 binding_entry_free (binding_entry entry)
74 entry->name = NULL;
75 entry->type = NULL;
76 entry->chain = free_binding_entry;
77 free_binding_entry = entry;
80 /* The datatype used to implement the mapping from names to types at
81 a given scope. */
82 struct binding_table_s GTY(())
84 /* Array of chains of "binding_entry"s */
85 binding_entry * GTY((length ("%h.chain_count"))) chain;
87 /* The number of chains in this table. This is the length of the
88 the member "chain" considered as an array. */
89 size_t chain_count;
91 /* Number of "binding_entry"s in this table. */
92 size_t entry_count;
95 /* Construct TABLE with an initial CHAIN_COUNT. */
97 static inline void
98 binding_table_construct (binding_table table, size_t chain_count)
100 table->chain_count = chain_count;
101 table->entry_count = 0;
102 table->chain = ggc_alloc_cleared
103 (table->chain_count * sizeof (binding_entry));
106 /* Make TABLE's entries ready for reuse. */
108 static void
109 binding_table_free (binding_table table)
111 size_t i;
112 size_t count;
114 if (table == NULL)
115 return;
117 for (i = 0, count = table->chain_count; i < count; ++i)
119 binding_entry temp = table->chain[i];
120 while (temp != NULL)
122 binding_entry entry = temp;
123 temp = entry->chain;
124 binding_entry_free (entry);
126 table->chain[i] = NULL;
128 table->entry_count = 0;
131 /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */
133 static inline binding_table
134 binding_table_new (size_t chain_count)
136 binding_table table = ggc_alloc (sizeof (struct binding_table_s));
137 table->chain = NULL;
138 binding_table_construct (table, chain_count);
139 return table;
142 /* Expand TABLE to twice its current chain_count. */
144 static void
145 binding_table_expand (binding_table table)
147 const size_t old_chain_count = table->chain_count;
148 const size_t old_entry_count = table->entry_count;
149 const size_t new_chain_count = 2 * old_chain_count;
150 binding_entry *old_chains = table->chain;
151 size_t i;
153 binding_table_construct (table, new_chain_count);
154 for (i = 0; i < old_chain_count; ++i)
156 binding_entry entry = old_chains[i];
157 for (; entry != NULL; entry = old_chains[i])
159 const unsigned int hash = IDENTIFIER_HASH_VALUE (entry->name);
160 const size_t j = ENTRY_INDEX (hash, new_chain_count);
162 old_chains[i] = entry->chain;
163 entry->chain = table->chain[j];
164 table->chain[j] = entry;
167 table->entry_count = old_entry_count;
170 /* Insert a binding for NAME to TYPE into TABLE. */
172 static void
173 binding_table_insert (binding_table table, tree name, tree type)
175 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
176 const size_t i = ENTRY_INDEX (hash, table->chain_count);
177 binding_entry entry = binding_entry_make (name, type);
179 entry->chain = table->chain[i];
180 table->chain[i] = entry;
181 ++table->entry_count;
183 if (3 * table->chain_count < 5 * table->entry_count)
184 binding_table_expand (table);
187 /* Return the binding_entry, if any, that maps NAME. */
189 binding_entry
190 binding_table_find (binding_table table, tree name)
192 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
193 binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)];
195 while (entry != NULL && entry->name != name)
196 entry = entry->chain;
198 return entry;
201 /* Return the binding_entry, if any, that maps NAME to an anonymous type. */
203 static tree
204 binding_table_find_anon_type (binding_table table, tree name)
206 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
207 binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)];
209 while (entry != NULL && TYPE_IDENTIFIER (entry->type) != name)
210 entry = entry->chain;
212 return entry ? entry->type : NULL;
215 /* Return the binding_entry, if any, that has TYPE as target. If NAME
216 is non-null, then set the domain and rehash that entry. */
218 static binding_entry
219 binding_table_reverse_maybe_remap (binding_table table, tree type, tree name)
221 const size_t chain_count = table->chain_count;
222 binding_entry entry = NULL;
223 binding_entry *p = NULL;
224 size_t i;
226 for (i = 0; i < chain_count && entry == NULL; ++i)
228 p = &table->chain[i];
229 while (*p != NULL && entry == NULL)
230 if ((*p)->type == type)
231 entry = *p;
232 else
233 p = &(*p)->chain;
236 if (entry != NULL && name != NULL && entry->name != name)
238 /* Remove the bucket from the previous chain. */
239 *p = (*p)->chain;
241 /* Remap the name type to type. */
242 i = ENTRY_INDEX (IDENTIFIER_HASH_VALUE (name), chain_count);
243 entry->chain = table->chain[i];
244 entry->name = name;
245 table->chain[i] = entry;
248 return entry;
251 /* Remove from TABLE all entries that map to anonymous enums or
252 class-types. */
254 void
255 binding_table_remove_anonymous_types (binding_table table)
257 const size_t chain_count = table->chain_count;
258 size_t i;
260 for (i = 0; i < chain_count; ++i)
262 binding_entry *p = &table->chain[i];
264 while (*p != NULL)
265 if (ANON_AGGRNAME_P ((*p)->name))
267 binding_entry e = *p;
268 *p = (*p)->chain;
269 --table->entry_count;
270 binding_entry_free (e);
272 else
273 p = &(*p)->chain;
277 /* Apply PROC -- with DATA -- to all entries in TABLE. */
279 void
280 binding_table_foreach (binding_table table, bt_foreach_proc proc, void *data)
282 const size_t chain_count = table->chain_count;
283 size_t i;
285 for (i = 0; i < chain_count; ++i)
287 binding_entry entry = table->chain[i];
288 for (; entry != NULL; entry = entry->chain)
289 proc (entry, data);
293 #ifndef ENABLE_SCOPE_CHECKING
294 # define ENABLE_SCOPE_CHECKING 0
295 #else
296 # define ENABLE_SCOPE_CHECKING 1
297 #endif
299 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
301 static GTY((deletable (""))) cxx_binding *free_bindings;
303 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
305 static cxx_binding *
306 cxx_binding_make (tree value, tree type)
308 cxx_binding *binding;
309 if (free_bindings)
311 binding = free_bindings;
312 free_bindings = binding->previous;
314 else
315 binding = ggc_alloc (sizeof (cxx_binding));
317 binding->value = value;
318 binding->type = type;
319 binding->previous = NULL;
321 return binding;
324 /* Put BINDING back on the free list. */
326 static inline void
327 cxx_binding_free (cxx_binding *binding)
329 binding->scope = NULL;
330 binding->previous = free_bindings;
331 free_bindings = binding;
334 /* Make DECL the innermost binding for ID. The LEVEL is the binding
335 level at which this declaration is being bound. */
337 static void
338 push_binding (tree id, tree decl, cxx_scope* level)
340 cxx_binding *binding = cxx_binding_make (decl, NULL);
342 /* Now, fill in the binding information. */
343 binding->previous = IDENTIFIER_BINDING (id);
344 binding->scope = level;
345 INHERITED_VALUE_BINDING_P (binding) = 0;
346 LOCAL_BINDING_P (binding) = (level != class_binding_level);
348 /* And put it on the front of the list of bindings for ID. */
349 IDENTIFIER_BINDING (id) = binding;
352 /* Remove the binding for DECL which should be the innermost binding
353 for ID. */
355 void
356 pop_binding (tree id, tree decl)
358 cxx_binding *binding;
360 if (id == NULL_TREE)
361 /* It's easiest to write the loops that call this function without
362 checking whether or not the entities involved have names. We
363 get here for such an entity. */
364 return;
366 /* Get the innermost binding for ID. */
367 binding = IDENTIFIER_BINDING (id);
369 /* The name should be bound. */
370 my_friendly_assert (binding != NULL, 0);
372 /* The DECL will be either the ordinary binding or the type
373 binding for this identifier. Remove that binding. */
374 if (binding->value == decl)
375 binding->value = NULL_TREE;
376 else if (binding->type == decl)
377 binding->type = NULL_TREE;
378 else
379 abort ();
381 if (!binding->value && !binding->type)
383 /* We're completely done with the innermost binding for this
384 identifier. Unhook it from the list of bindings. */
385 IDENTIFIER_BINDING (id) = binding->previous;
387 /* Add it to the free list. */
388 cxx_binding_free (binding);
392 /* BINDING records an existing declaration for a namein the current scope.
393 But, DECL is another declaration for that same identifier in the
394 same scope. This is the `struct stat' hack whereby a non-typedef
395 class name or enum-name can be bound at the same level as some other
396 kind of entity.
397 3.3.7/1
399 A class name (9.1) or enumeration name (7.2) can be hidden by the
400 name of an object, function, or enumerator declared in the same scope.
401 If a class or enumeration name and an object, function, or enumerator
402 are declared in the same scope (in any order) with the same name, the
403 class or enumeration name is hidden wherever the object, function, or
404 enumerator name is visible.
406 It's the responsibility of the caller to check that
407 inserting this name is valid here. Returns nonzero if the new binding
408 was successful. */
410 static bool
411 supplement_binding (cxx_binding *binding, tree decl)
413 tree bval = binding->value;
414 bool ok = true;
416 timevar_push (TV_NAME_LOOKUP);
417 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
418 /* The new name is the type name. */
419 binding->type = decl;
420 else if (!bval)
421 /* This situation arises when push_class_level_binding moves an
422 inherited type-binding out of the way to make room for a new
423 value binding. */
424 binding->value = decl;
425 else if (TREE_CODE (bval) == TYPE_DECL && DECL_ARTIFICIAL (bval))
427 /* The old binding was a type name. It was placed in
428 VALUE field because it was thought, at the point it was
429 declared, to be the only entity with such a name. Move the
430 type name into the type slot; it is now hidden by the new
431 binding. */
432 binding->type = bval;
433 binding->value = decl;
434 binding->value_is_inherited = false;
436 else if (TREE_CODE (bval) == TYPE_DECL
437 && TREE_CODE (decl) == TYPE_DECL
438 && DECL_NAME (decl) == DECL_NAME (bval)
439 && (same_type_p (TREE_TYPE (decl), TREE_TYPE (bval))
440 /* If either type involves template parameters, we must
441 wait until instantiation. */
442 || uses_template_parms (TREE_TYPE (decl))
443 || uses_template_parms (TREE_TYPE (bval))))
444 /* We have two typedef-names, both naming the same type to have
445 the same name. This is OK because of:
447 [dcl.typedef]
449 In a given scope, a typedef specifier can be used to redefine
450 the name of any type declared in that scope to refer to the
451 type to which it already refers. */
452 ok = false;
453 /* There can be two block-scope declarations of the same variable,
454 so long as they are `extern' declarations. However, there cannot
455 be two declarations of the same static data member:
457 [class.mem]
459 A member shall not be declared twice in the
460 member-specification. */
461 else if (TREE_CODE (decl) == VAR_DECL && TREE_CODE (bval) == VAR_DECL
462 && DECL_EXTERNAL (decl) && DECL_EXTERNAL (bval)
463 && !DECL_CLASS_SCOPE_P (decl))
465 duplicate_decls (decl, binding->value);
466 ok = false;
468 else
470 error ("declaration of `%#D'", decl);
471 cp_error_at ("conflicts with previous declaration `%#D'",
472 binding->value);
473 ok = false;
476 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ok);
479 /* Add DECL to the list of things declared in B. */
481 void
482 add_decl_to_level (tree decl, cxx_scope *b)
484 if (TREE_CODE (decl) == NAMESPACE_DECL
485 && !DECL_NAMESPACE_ALIAS (decl))
487 TREE_CHAIN (decl) = b->namespaces;
488 b->namespaces = decl;
490 else if (TREE_CODE (decl) == VAR_DECL && DECL_VIRTUAL_P (decl))
492 TREE_CHAIN (decl) = b->vtables;
493 b->vtables = decl;
495 else
497 /* We build up the list in reverse order, and reverse it later if
498 necessary. */
499 TREE_CHAIN (decl) = b->names;
500 b->names = decl;
501 b->names_size++;
503 /* If appropriate, add decl to separate list of statics */
504 if (b->kind == sk_namespace)
505 if ((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
506 || (TREE_CODE (decl) == FUNCTION_DECL
507 && (!TREE_PUBLIC (decl) || DECL_DECLARED_INLINE_P (decl))))
508 VARRAY_PUSH_TREE (b->static_decls, decl);
512 /* Bind DECL to ID in the current_binding_level, assumed to be a local
513 binding level. If PUSH_USING is set in FLAGS, we know that DECL
514 doesn't really belong to this binding level, that it got here
515 through a using-declaration. */
517 void
518 push_local_binding (tree id, tree decl, int flags)
520 struct cp_binding_level *b;
522 /* Skip over any local classes. This makes sense if we call
523 push_local_binding with a friend decl of a local class. */
524 b = innermost_nonclass_level ();
526 if (lookup_name_current_level (id))
528 /* Supplement the existing binding. */
529 if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
530 /* It didn't work. Something else must be bound at this
531 level. Do not add DECL to the list of things to pop
532 later. */
533 return;
535 else
536 /* Create a new binding. */
537 push_binding (id, decl, b);
539 if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
540 /* We must put the OVERLOAD into a TREE_LIST since the
541 TREE_CHAIN of an OVERLOAD is already used. Similarly for
542 decls that got here through a using-declaration. */
543 decl = build_tree_list (NULL_TREE, decl);
545 /* And put DECL on the list of things declared by the current
546 binding level. */
547 add_decl_to_level (decl, b);
550 /* true means unconditionally make a BLOCK for the next level pushed. */
552 static bool keep_next_level_flag;
554 static int binding_depth = 0;
555 static int is_class_level = 0;
557 static void
558 indent (int depth)
560 int i;
562 for (i = 0; i < depth * 2; i++)
563 putc (' ', stderr);
566 /* Return a string describing the kind of SCOPE we have. */
567 static const char *
568 cxx_scope_descriptor (cxx_scope *scope)
570 /* The order of this table must match the "scope_kind"
571 enumerators. */
572 static const char* scope_kind_names[] = {
573 "block-scope",
574 "cleanup-scope",
575 "try-scope",
576 "catch-scope",
577 "for-scope",
578 "function-parameter-scope",
579 "class-scope",
580 "namespace-scope",
581 "template-parameter-scope",
582 "template-explicit-spec-scope"
584 const scope_kind kind = scope->explicit_spec_p
585 ? sk_template_spec : scope->kind;
587 return scope_kind_names[kind];
590 /* Output a debugging information about SCOPE when performning
591 ACTION at LINE. */
592 static void
593 cxx_scope_debug (cxx_scope *scope, int line, const char *action)
595 const char *desc = cxx_scope_descriptor (scope);
596 if (scope->this_entity)
597 verbatim ("%s %s(%E) %p %d\n", action, desc,
598 scope->this_entity, (void *) scope, line);
599 else
600 verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
603 /* Return the estimated initial size of the hashtable of a NAMESPACE
604 scope. */
606 static inline size_t
607 namespace_scope_ht_size (tree ns)
609 tree name = DECL_NAME (ns);
611 return name == std_identifier
612 ? NAMESPACE_STD_HT_SIZE
613 : (name == global_scope_name
614 ? GLOBAL_SCOPE_HT_SIZE
615 : NAMESPACE_ORDINARY_HT_SIZE);
618 /* A chain of binding_level structures awaiting reuse. */
620 static GTY((deletable (""))) struct cp_binding_level *free_binding_level;
622 /* Create a new KIND scope and make it the top of the active scopes stack.
623 ENTITY is the scope of the associated C++ entity (namespace, class,
624 function); it is NULL otherwise. */
626 cxx_scope *
627 begin_scope (scope_kind kind, tree entity)
629 cxx_scope *scope;
631 /* Reuse or create a struct for this binding level. */
632 if (!ENABLE_SCOPE_CHECKING && free_binding_level)
634 scope = free_binding_level;
635 free_binding_level = scope->level_chain;
637 else
638 scope = ggc_alloc (sizeof (cxx_scope));
639 memset (scope, 0, sizeof (cxx_scope));
641 scope->this_entity = entity;
642 scope->more_cleanups_ok = true;
643 switch (kind)
645 case sk_cleanup:
646 scope->keep = true;
647 break;
649 case sk_template_spec:
650 scope->explicit_spec_p = true;
651 kind = sk_template_parms;
652 /* fall through */
653 case sk_template_parms:
654 case sk_block:
655 case sk_try:
656 case sk_catch:
657 case sk_for:
658 case sk_class:
659 case sk_function_parms:
660 scope->keep = keep_next_level_flag;
661 break;
663 case sk_namespace:
664 scope->type_decls = binding_table_new (namespace_scope_ht_size (entity));
665 NAMESPACE_LEVEL (entity) = scope;
666 VARRAY_TREE_INIT (scope->static_decls,
667 DECL_NAME (entity) == std_identifier
668 || DECL_NAME (entity) == global_scope_name
669 ? 200 : 10,
670 "Static declarations");
671 break;
673 default:
674 /* Should not happen. */
675 my_friendly_assert (false, 20030922);
676 break;
678 scope->kind = kind;
680 /* Add it to the front of currently active scopes stack. */
681 scope->level_chain = current_binding_level;
682 current_binding_level = scope;
683 keep_next_level_flag = false;
685 if (ENABLE_SCOPE_CHECKING)
687 scope->binding_depth = binding_depth;
688 indent (binding_depth);
689 cxx_scope_debug (scope, input_location.line, "push");
690 is_class_level = 0;
691 binding_depth++;
694 return scope;
697 /* We're about to leave current scope. Pop the top of the stack of
698 currently active scopes. Return the enclosing scope, now active. */
700 cxx_scope *
701 leave_scope (void)
703 cxx_scope *scope = current_binding_level;
705 if (scope->kind == sk_namespace && class_binding_level)
706 current_binding_level = class_binding_level;
708 /* We cannot leave a scope, if there are none left. */
709 if (NAMESPACE_LEVEL (global_namespace))
710 my_friendly_assert (!global_scope_p (scope), 20030527);
712 if (ENABLE_SCOPE_CHECKING)
714 indent (--binding_depth);
715 cxx_scope_debug (scope, input_location.line, "leave");
716 if (is_class_level != (scope == class_binding_level))
718 indent (binding_depth);
719 verbatim ("XXX is_class_level != (current_scope == class_scope)\n");
721 is_class_level = 0;
724 /* Move one nesting level up. */
725 current_binding_level = scope->level_chain;
727 /* Namespace-scopes are left most probably temporarily, not completely;
728 they can be reopen later, e.g. in namespace-extension or any name
729 binding acttivity that requires us to resume a namespace. For other
730 scopes, we just make the structure available for reuse. */
731 if (scope->kind != sk_namespace)
733 scope->level_chain = free_binding_level;
734 if (scope->kind == sk_class)
735 scope->type_decls = NULL;
736 else
737 binding_table_free (scope->type_decls);
738 my_friendly_assert (!ENABLE_SCOPE_CHECKING
739 || scope->binding_depth == binding_depth,
740 20030529);
741 free_binding_level = scope;
744 /* Find the innermost enclosing class scope, and reset
745 CLASS_BINDING_LEVEL appropriately. */
746 for (scope = current_binding_level;
747 scope && scope->kind != sk_class;
748 scope = scope->level_chain)
750 class_binding_level = scope && scope->kind == sk_class ? scope : NULL;
752 return current_binding_level;
755 static void
756 resume_scope (struct cp_binding_level* b)
758 /* Resuming binding levels is meant only for namespaces,
759 and those cannot nest into classes. */
760 my_friendly_assert(!class_binding_level, 386);
761 /* Also, resuming a non-directly nested namespace is a no-no. */
762 my_friendly_assert(b->level_chain == current_binding_level, 386);
763 current_binding_level = b;
764 if (ENABLE_SCOPE_CHECKING)
766 b->binding_depth = binding_depth;
767 indent (binding_depth);
768 cxx_scope_debug (b, input_location.line, "resume");
769 is_class_level = 0;
770 binding_depth++;
774 /* Return the innermost binding level that is not for a class scope. */
776 static cxx_scope *
777 innermost_nonclass_level (void)
779 cxx_scope *b;
781 b = current_binding_level;
782 while (b->kind == sk_class)
783 b = b->level_chain;
785 return b;
788 /* We're defining an object of type TYPE. If it needs a cleanup, but
789 we're not allowed to add any more objects with cleanups to the current
790 scope, create a new binding level. */
792 void
793 maybe_push_cleanup_level (tree type)
795 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
796 && current_binding_level->more_cleanups_ok == 0)
798 begin_scope (sk_cleanup, NULL);
799 clear_last_expr ();
800 add_scope_stmt (/*begin_p=*/1, /*partial_p=*/1);
804 /* Nonzero if we are currently in the global binding level. */
807 global_bindings_p (void)
809 return global_scope_p (current_binding_level);
812 /* True if we are currently in a toplevel binding level. This
813 means either the global binding level or a namespace in a toplevel
814 binding level. Since there are no non-toplevel namespace levels,
815 this really means any namespace or template parameter level. We
816 also include a class whose context is toplevel. */
818 bool
819 toplevel_bindings_p (void)
821 struct cp_binding_level *b = innermost_nonclass_level ();
823 return b->kind == sk_namespace || b->kind == sk_template_parms;
826 /* True if this is a namespace scope, or if we are defining a class
827 which is itself at namespace scope, or whose enclosing class is
828 such a class, etc. */
830 bool
831 namespace_bindings_p (void)
833 struct cp_binding_level *b = innermost_nonclass_level ();
835 return b->kind == sk_namespace;
838 /* True if the current level needs to have a BLOCK made. */
840 bool
841 kept_level_p (void)
843 return (current_binding_level->blocks != NULL_TREE
844 || current_binding_level->keep
845 || current_binding_level->kind == sk_cleanup
846 || current_binding_level->names != NULL_TREE
847 || current_binding_level->type_decls != NULL);
850 /* Returns the kind of the innermost scope. */
852 scope_kind
853 innermost_scope_kind (void)
855 return current_binding_level->kind;
858 /* Returns true if this scope was created to store template parameters. */
860 bool
861 template_parm_scope_p (void)
863 return innermost_scope_kind () == sk_template_parms;
866 /* If KEEP is true, make a BLOCK node for the next binding level,
867 unconditionally. Otherwise, use the normal logic to decide whether
868 or not to create a BLOCK. */
870 void
871 keep_next_level (bool keep)
873 keep_next_level_flag = keep;
876 /* Return the list of declarations of the current level.
877 Note that this list is in reverse order unless/until
878 you nreverse it; and when you do nreverse it, you must
879 store the result back using `storedecls' or you will lose. */
881 tree
882 getdecls (void)
884 return current_binding_level->names;
887 /* Set the current binding TABLE for type declarations.. This is a
888 temporary workaround of the fact that the data structure classtypes
889 does not currently carry its allocated cxx_scope structure. */
890 void
891 cxx_remember_type_decls (binding_table table)
893 current_binding_level->type_decls = table;
896 /* For debugging. */
897 static int no_print_functions = 0;
898 static int no_print_builtins = 0;
900 /* Called from print_binding_level through binding_table_foreach to
901 print the content of binding ENTRY. DATA is a pointer to line offset
902 marker. */
903 static void
904 bt_print_entry (binding_entry entry, void *data)
906 int *p = (int *) data;
907 int len;
909 if (entry->name == NULL)
910 len = 3;
911 else if (entry->name == TYPE_IDENTIFIER (entry->type))
912 len = 2;
913 else
914 len = 4;
915 len = 4;
917 *p += len;
919 if (*p > 5)
921 fprintf (stderr, "\n\t");
922 *p = len;
924 if (entry->name == NULL)
926 print_node_brief (stderr, "<unnamed-typedef", entry->type, 0);
927 fprintf (stderr, ">");
929 else if (entry->name == TYPE_IDENTIFIER (entry->type))
930 print_node_brief (stderr, "", entry->type, 0);
931 else
933 print_node_brief (stderr, "<typedef", entry->name, 0);
934 print_node_brief (stderr, "", entry->type, 0);
935 fprintf (stderr, ">");
939 void
940 print_binding_level (struct cp_binding_level* lvl)
942 tree t;
943 int i = 0, len;
944 fprintf (stderr, " blocks=" HOST_PTR_PRINTF, (void *) lvl->blocks);
945 if (lvl->more_cleanups_ok)
946 fprintf (stderr, " more-cleanups-ok");
947 if (lvl->have_cleanups)
948 fprintf (stderr, " have-cleanups");
949 fprintf (stderr, "\n");
950 if (lvl->names)
952 fprintf (stderr, " names:\t");
953 /* We can probably fit 3 names to a line? */
954 for (t = lvl->names; t; t = TREE_CHAIN (t))
956 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
957 continue;
958 if (no_print_builtins
959 && (TREE_CODE (t) == TYPE_DECL)
960 && (!strcmp (DECL_SOURCE_FILE (t),"<built-in>")))
961 continue;
963 /* Function decls tend to have longer names. */
964 if (TREE_CODE (t) == FUNCTION_DECL)
965 len = 3;
966 else
967 len = 2;
968 i += len;
969 if (i > 6)
971 fprintf (stderr, "\n\t");
972 i = len;
974 print_node_brief (stderr, "", t, 0);
975 if (t == error_mark_node)
976 break;
978 if (i)
979 fprintf (stderr, "\n");
981 if (lvl->type_decls)
983 fprintf (stderr, " tags:\t");
984 i = 0;
985 binding_table_foreach (lvl->type_decls, bt_print_entry, &i);
986 if (i)
987 fprintf (stderr, "\n");
989 if (lvl->class_shadowed)
991 fprintf (stderr, " class-shadowed:");
992 for (t = lvl->class_shadowed; t; t = TREE_CHAIN (t))
994 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
996 fprintf (stderr, "\n");
998 if (lvl->type_shadowed)
1000 fprintf (stderr, " type-shadowed:");
1001 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
1003 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1005 fprintf (stderr, "\n");
1009 void
1010 print_other_binding_stack (struct cp_binding_level *stack)
1012 struct cp_binding_level *level;
1013 for (level = stack; !global_scope_p (level); level = level->level_chain)
1015 fprintf (stderr, "binding level " HOST_PTR_PRINTF "\n", (void *) level);
1016 print_binding_level (level);
1020 void
1021 print_binding_stack (void)
1023 struct cp_binding_level *b;
1024 fprintf (stderr, "current_binding_level=" HOST_PTR_PRINTF
1025 "\nclass_binding_level=" HOST_PTR_PRINTF
1026 "\nNAMESPACE_LEVEL (global_namespace)=" HOST_PTR_PRINTF "\n",
1027 (void *) current_binding_level, (void *) class_binding_level,
1028 (void *) NAMESPACE_LEVEL (global_namespace));
1029 if (class_binding_level)
1031 for (b = class_binding_level; b; b = b->level_chain)
1032 if (b == current_binding_level)
1033 break;
1034 if (b)
1035 b = class_binding_level;
1036 else
1037 b = current_binding_level;
1039 else
1040 b = current_binding_level;
1041 print_other_binding_stack (b);
1042 fprintf (stderr, "global:\n");
1043 print_binding_level (NAMESPACE_LEVEL (global_namespace));
1046 /* Return the type associated with id. */
1048 tree
1049 identifier_type_value (tree id)
1051 timevar_push (TV_NAME_LOOKUP);
1052 /* There is no type with that name, anywhere. */
1053 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
1054 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
1055 /* This is not the type marker, but the real thing. */
1056 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
1057 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, REAL_IDENTIFIER_TYPE_VALUE (id));
1058 /* Have to search for it. It must be on the global level, now.
1059 Ask lookup_name not to return non-types. */
1060 id = lookup_name_real (id, 2, 1, 0, LOOKUP_COMPLAIN);
1061 if (id)
1062 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_TYPE (id));
1063 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
1066 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
1067 the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */
1069 tree
1070 identifier_global_value (tree t)
1072 return IDENTIFIER_GLOBAL_VALUE (t);
1075 /* Push a definition of struct, union or enum tag named ID. into
1076 binding_level B. DECL is a TYPE_DECL for the type. We assume that
1077 the tag ID is not already defined. */
1079 static void
1080 set_identifier_type_value_with_scope (tree id, tree decl, cxx_scope *b)
1082 tree type;
1084 if (b->kind != sk_namespace)
1086 /* Shadow the marker, not the real thing, so that the marker
1087 gets restored later. */
1088 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
1089 b->type_shadowed
1090 = tree_cons (id, old_type_value, b->type_shadowed);
1091 type = decl ? TREE_TYPE (decl) : NULL_TREE;
1093 else
1095 cxx_binding *binding =
1096 binding_for_name (NAMESPACE_LEVEL (current_namespace), id);
1097 if (decl)
1099 if (binding->value)
1100 supplement_binding (binding, decl);
1101 else
1102 binding->value = decl;
1104 else
1105 abort ();
1106 /* Store marker instead of real type. */
1107 type = global_type_node;
1109 SET_IDENTIFIER_TYPE_VALUE (id, type);
1112 /* As set_identifier_type_value_with_scope, but using
1113 current_binding_level. */
1115 void
1116 set_identifier_type_value (tree id, tree decl)
1118 set_identifier_type_value_with_scope (id, decl, current_binding_level);
1121 /* Return (from the stack of) the BINDING, if any, establihsed at SCOPE. */
1123 static inline cxx_binding *
1124 find_binding (cxx_scope *scope, cxx_binding *binding)
1126 timevar_push (TV_NAME_LOOKUP);
1128 for (; binding != NULL; binding = binding->previous)
1129 if (binding->scope == scope)
1130 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, binding);
1132 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, (cxx_binding *)0);
1135 /* Return the binding for NAME in SCOPE, if any. Otherwise, return NULL. */
1137 cxx_binding *
1138 cxx_scope_find_binding_for_name (cxx_scope *scope, tree name)
1140 cxx_binding *b = IDENTIFIER_NAMESPACE_BINDINGS (name);
1141 if (b)
1143 /* Fold-in case where NAME is used only once. */
1144 if (scope == b->scope && b->previous == NULL)
1145 return b;
1146 return find_binding (scope, b);
1148 return NULL;
1151 /* Always returns a binding for name in scope. If no binding is
1152 found, make a new one. */
1154 cxx_binding *
1155 binding_for_name (cxx_scope *scope, tree name)
1157 cxx_binding *result;
1159 result = cxx_scope_find_binding_for_name (scope, name);
1160 if (result)
1161 return result;
1162 /* Not found, make a new one. */
1163 result = cxx_binding_make (NULL, NULL);
1164 result->previous = IDENTIFIER_NAMESPACE_BINDINGS (name);
1165 result->scope = scope;
1166 result->is_local = false;
1167 result->value_is_inherited = false;
1168 IDENTIFIER_NAMESPACE_BINDINGS (name) = result;
1169 return result;
1172 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
1173 caller to set DECL_CONTEXT properly. */
1175 tree
1176 pushdecl_with_scope (tree x, cxx_scope *level)
1178 register struct cp_binding_level *b;
1179 tree function_decl = current_function_decl;
1181 timevar_push (TV_NAME_LOOKUP);
1182 current_function_decl = NULL_TREE;
1183 if (level->kind == sk_class)
1185 b = class_binding_level;
1186 class_binding_level = level;
1187 pushdecl_class_level (x);
1188 class_binding_level = b;
1190 else
1192 b = current_binding_level;
1193 current_binding_level = level;
1194 x = pushdecl (x);
1195 current_binding_level = b;
1197 current_function_decl = function_decl;
1198 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
1201 /* Return the type that should be used when TYPE's name is preceded
1202 by a tag such as 'struct' or 'union', or null if the name cannot
1203 be used in this way.
1205 For example, when processing the third line of:
1207 struct A;
1208 typedef struct A A;
1209 struct A;
1211 lookup of A will find the typedef. Given A's typedef, this function
1212 will return the type associated with "struct A". For the tag to be
1213 anything other than TYPE, TYPE must be a typedef whose original type
1214 has the same name and context as TYPE itself.
1216 It is not valid for a typedef of an anonymous type to be used with
1217 an explicit tag:
1219 typedef struct { ... } B;
1220 struct B;
1222 Return null for this case. */
1224 static tree
1225 follow_tag_typedef (tree type)
1227 tree original;
1229 original = original_type (type);
1230 if (! TYPE_NAME (original))
1231 return NULL_TREE;
1232 if (TYPE_IDENTIFIER (original) == TYPE_IDENTIFIER (type)
1233 && (CP_DECL_CONTEXT (TYPE_NAME (original))
1234 == CP_DECL_CONTEXT (TYPE_NAME (type)))
1235 && !(CLASS_TYPE_P (original) && TYPE_WAS_ANONYMOUS (original)))
1236 return original;
1237 else
1238 return NULL_TREE;
1241 /* Given NAME, an IDENTIFIER_NODE,
1242 return the structure (or union or enum) definition for that name.
1243 Searches binding levels from its SCOPE up to the global level.
1244 If THISLEVEL_ONLY is nonzero, searches only the specified context
1245 (but skips any sk_cleanup contexts to find one that is
1246 meaningful for tags).
1247 FORM says which kind of type the caller wants;
1248 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
1249 If the wrong kind of type is found, and it's not a template, an error is
1250 reported. */
1252 tree
1253 lookup_tag (enum tree_code form, tree name,
1254 cxx_scope *binding_level, int thislevel_only)
1256 register struct cp_binding_level *level;
1257 /* Nonzero if, we should look past a template parameter level, even
1258 if THISLEVEL_ONLY. */
1259 int allow_template_parms_p = 1;
1260 bool type_is_anonymous = ANON_AGGRNAME_P (name);
1262 timevar_push (TV_NAME_LOOKUP);
1263 for (level = binding_level; level; level = level->level_chain)
1265 register tree tail;
1266 if (type_is_anonymous && level->type_decls != NULL)
1268 tree type = binding_table_find_anon_type (level->type_decls, name);
1269 /* There is no need for error checking here, because
1270 anon names are unique throughout the compilation. */
1271 if (type != NULL)
1272 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, type);
1274 else if (level->kind == sk_namespace)
1275 /* Do namespace lookup. */
1276 for (tail = current_namespace; 1; tail = CP_DECL_CONTEXT (tail))
1278 cxx_binding *binding =
1279 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (tail), name);
1280 tree old;
1282 /* If we just skipped past a template parameter level,
1283 even though THISLEVEL_ONLY, and we find a template
1284 class declaration, then we use the _TYPE node for the
1285 template. See the example below. */
1286 if (thislevel_only && !allow_template_parms_p
1287 && binding && binding->value
1288 && DECL_CLASS_TEMPLATE_P (binding->value))
1289 old = binding->value;
1290 else if (binding)
1291 old = select_decl (binding, LOOKUP_PREFER_TYPES);
1292 else
1293 old = NULL_TREE;
1295 if (old)
1297 /* We've found something at this binding level. If it is
1298 a typedef, extract the tag it refers to. Lookup fails
1299 if the typedef doesn't refer to a taggable type. */
1300 old = TREE_TYPE (old);
1301 old = follow_tag_typedef (old);
1302 if (!old)
1303 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
1304 if (TREE_CODE (old) != form
1305 && (form == ENUMERAL_TYPE
1306 || TREE_CODE (old) == ENUMERAL_TYPE))
1308 error ("`%#D' redeclared as %C", old, form);
1309 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
1311 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, old);
1313 if (thislevel_only || tail == global_namespace)
1314 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
1316 else if (level->type_decls != NULL)
1318 binding_entry entry = binding_table_find (level->type_decls, name);
1319 if (entry != NULL)
1321 enum tree_code code = TREE_CODE (entry->type);
1323 if (code != form
1324 && (form == ENUMERAL_TYPE || code == ENUMERAL_TYPE))
1326 /* Definition isn't the kind we were looking for. */
1327 error ("`%#D' redeclared as %C", entry->type, form);
1328 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
1330 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, entry->type);
1333 if (thislevel_only && level->kind != sk_cleanup)
1335 if (level->kind == sk_template_parms && allow_template_parms_p)
1337 /* We must deal with cases like this:
1339 template <class T> struct S;
1340 template <class T> struct S {};
1342 When looking up `S', for the second declaration, we
1343 would like to find the first declaration. But, we
1344 are in the pseudo-global level created for the
1345 template parameters, rather than the (surrounding)
1346 namespace level. Thus, we keep going one more level,
1347 even though THISLEVEL_ONLY is nonzero. */
1348 allow_template_parms_p = 0;
1349 continue;
1351 else
1352 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
1355 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
1358 /* Given a type, find the tag that was defined for it and return the tag name.
1359 Otherwise return 0. However, the value can never be 0
1360 in the cases in which this is used.
1362 C++: If NAME is nonzero, this is the new name to install. This is
1363 done when replacing anonymous tags with real tag names. */
1365 tree
1366 lookup_tag_reverse (tree type, tree name)
1368 register struct cp_binding_level *level;
1370 timevar_push (TV_NAME_LOOKUP);
1371 for (level = current_binding_level; level; level = level->level_chain)
1373 binding_entry entry = level->type_decls == NULL
1374 ? NULL
1375 : binding_table_reverse_maybe_remap (level->type_decls, type, name);
1376 if (entry)
1377 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, entry->name);
1379 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
1382 /* Do a pushlevel for class declarations. */
1384 void
1385 pushlevel_class (void)
1387 if (ENABLE_SCOPE_CHECKING)
1388 is_class_level = 1;
1390 class_binding_level = begin_scope (sk_class, current_class_type);
1393 /* ...and a poplevel for class declarations. */
1395 void
1396 poplevel_class (void)
1398 register struct cp_binding_level *level = class_binding_level;
1399 tree shadowed;
1401 timevar_push (TV_NAME_LOOKUP);
1402 my_friendly_assert (level != 0, 354);
1404 /* If we're leaving a toplevel class, don't bother to do the setting
1405 of IDENTIFIER_CLASS_VALUE to NULL_TREE, since first of all this slot
1406 shouldn't even be used when current_class_type isn't set, and second,
1407 if we don't touch it here, we're able to use the cache effect if the
1408 next time we're entering a class scope, it is the same class. */
1409 if (current_class_depth != 1)
1411 struct cp_binding_level* b;
1413 /* Clear out our IDENTIFIER_CLASS_VALUEs. */
1414 for (shadowed = level->class_shadowed;
1415 shadowed;
1416 shadowed = TREE_CHAIN (shadowed))
1417 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed)) = NULL_TREE;
1419 /* Find the next enclosing class, and recreate
1420 IDENTIFIER_CLASS_VALUEs appropriate for that class. */
1421 b = level->level_chain;
1422 while (b && b->kind != sk_class)
1423 b = b->level_chain;
1425 if (b)
1426 for (shadowed = b->class_shadowed;
1427 shadowed;
1428 shadowed = TREE_CHAIN (shadowed))
1430 cxx_binding *binding;
1432 binding = IDENTIFIER_BINDING (TREE_PURPOSE (shadowed));
1433 while (binding && binding->scope != b)
1434 binding = binding->previous;
1436 if (binding)
1437 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed))
1438 = binding->value;
1441 else
1442 /* Remember to save what IDENTIFIER's were bound in this scope so we
1443 can recover from cache misses. */
1445 previous_class_type = current_class_type;
1446 previous_class_values = class_binding_level->class_shadowed;
1448 for (shadowed = level->type_shadowed;
1449 shadowed;
1450 shadowed = TREE_CHAIN (shadowed))
1451 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
1453 /* Remove the bindings for all of the class-level declarations. */
1454 for (shadowed = level->class_shadowed;
1455 shadowed;
1456 shadowed = TREE_CHAIN (shadowed))
1457 pop_binding (TREE_PURPOSE (shadowed), TREE_TYPE (shadowed));
1459 /* Now, pop out of the binding level which we created up in the
1460 `pushlevel_class' routine. */
1461 if (ENABLE_SCOPE_CHECKING)
1462 is_class_level = 1;
1464 leave_scope ();
1465 timevar_pop (TV_NAME_LOOKUP);
1468 /* Bind DECL to ID in the class_binding_level. Returns nonzero if the
1469 binding was successful. */
1472 push_class_binding (tree id, tree decl)
1474 int result = 1;
1475 cxx_binding *binding = IDENTIFIER_BINDING (id);
1476 tree context;
1478 timevar_push (TV_NAME_LOOKUP);
1479 /* Note that we declared this value so that we can issue an error if
1480 this is an invalid redeclaration of a name already used for some
1481 other purpose. */
1482 note_name_declared_in_class (id, decl);
1484 if (binding && binding->scope == class_binding_level)
1485 /* Supplement the existing binding. */
1486 result = supplement_binding (IDENTIFIER_BINDING (id), decl);
1487 else
1488 /* Create a new binding. */
1489 push_binding (id, decl, class_binding_level);
1491 /* Update the IDENTIFIER_CLASS_VALUE for this ID to be the
1492 class-level declaration. Note that we do not use DECL here
1493 because of the possibility of the `struct stat' hack; if DECL is
1494 a class-name or enum-name we might prefer a field-name, or some
1495 such. */
1496 IDENTIFIER_CLASS_VALUE (id) = IDENTIFIER_BINDING (id)->value;
1498 /* If this is a binding from a base class, mark it as such. */
1499 binding = IDENTIFIER_BINDING (id);
1500 if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
1502 if (TREE_CODE (decl) == OVERLOAD)
1503 context = CP_DECL_CONTEXT (OVL_CURRENT (decl));
1504 else
1506 my_friendly_assert (DECL_P (decl), 0);
1507 context = context_for_name_lookup (decl);
1510 if (is_properly_derived_from (current_class_type, context))
1511 INHERITED_VALUE_BINDING_P (binding) = 1;
1512 else
1513 INHERITED_VALUE_BINDING_P (binding) = 0;
1515 else if (binding->value == decl)
1516 /* We only encounter a TREE_LIST when push_class_decls detects an
1517 ambiguity. Such an ambiguity can be overridden by a definition
1518 in this class. */
1519 INHERITED_VALUE_BINDING_P (binding) = 1;
1521 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, result);
1524 /* We are entering the scope of a class. Clear IDENTIFIER_CLASS_VALUE
1525 for any names in enclosing classes. */
1527 void
1528 clear_identifier_class_values (void)
1530 tree t;
1532 if (!class_binding_level)
1533 return;
1535 for (t = class_binding_level->class_shadowed;
1537 t = TREE_CHAIN (t))
1538 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
1541 /* Make the declaration of X appear in CLASS scope. */
1543 bool
1544 pushdecl_class_level (tree x)
1546 tree name;
1547 bool is_valid = true;
1549 timevar_push (TV_NAME_LOOKUP);
1550 /* Get the name of X. */
1551 if (TREE_CODE (x) == OVERLOAD)
1552 name = DECL_NAME (get_first_fn (x));
1553 else
1554 name = DECL_NAME (x);
1556 if (name)
1558 is_valid = push_class_level_binding (name, x);
1559 if (TREE_CODE (x) == TYPE_DECL)
1560 set_identifier_type_value (name, x);
1562 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
1564 /* If X is an anonymous aggregate, all of its members are
1565 treated as if they were members of the class containing the
1566 aggregate, for naming purposes. */
1567 tree f;
1569 for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = TREE_CHAIN (f))
1571 location_t save_location = input_location;
1572 input_location = DECL_SOURCE_LOCATION (f);
1573 if (!pushdecl_class_level (f))
1574 is_valid = false;
1575 input_location = save_location;
1578 timevar_pop (TV_NAME_LOOKUP);
1580 return is_valid;
1583 /* Make the declaration(s) of X appear in CLASS scope under the name
1584 NAME. Returns true if the binding is valid. */
1586 bool
1587 push_class_level_binding (tree name, tree x)
1589 cxx_binding *binding;
1591 timevar_push (TV_NAME_LOOKUP);
1592 /* The class_binding_level will be NULL if x is a template
1593 parameter name in a member template. */
1594 if (!class_binding_level)
1595 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
1597 /* Make sure that this new member does not have the same name
1598 as a template parameter. */
1599 if (TYPE_BEING_DEFINED (current_class_type))
1600 check_template_shadow (x);
1602 /* If this declaration shadows a declaration from an enclosing
1603 class, then we will need to restore IDENTIFIER_CLASS_VALUE when
1604 we leave this class. Record the shadowed declaration here. */
1605 binding = IDENTIFIER_BINDING (name);
1606 if (binding && binding->value)
1608 tree bval = binding->value;
1609 tree old_decl = NULL_TREE;
1611 if (INHERITED_VALUE_BINDING_P (binding))
1613 /* If the old binding was from a base class, and was for a
1614 tag name, slide it over to make room for the new binding.
1615 The old binding is still visible if explicitly qualified
1616 with a class-key. */
1617 if (TREE_CODE (bval) == TYPE_DECL && DECL_ARTIFICIAL (bval)
1618 && !(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)))
1620 old_decl = binding->type;
1621 binding->type = bval;
1622 binding->value = NULL_TREE;
1623 INHERITED_VALUE_BINDING_P (binding) = 0;
1625 else
1626 old_decl = bval;
1628 else if (TREE_CODE (x) == OVERLOAD && is_overloaded_fn (bval))
1629 old_decl = bval;
1630 else if (TREE_CODE (x) == USING_DECL && TREE_CODE (bval) == USING_DECL)
1631 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
1632 else if (TREE_CODE (x) == USING_DECL && is_overloaded_fn (bval))
1633 old_decl = bval;
1634 else if (TREE_CODE (bval) == USING_DECL && is_overloaded_fn (x))
1635 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
1637 if (old_decl)
1639 tree shadow;
1641 /* Find the previous binding of name on the class-shadowed
1642 list, and update it. */
1643 for (shadow = class_binding_level->class_shadowed;
1644 shadow;
1645 shadow = TREE_CHAIN (shadow))
1646 if (TREE_PURPOSE (shadow) == name
1647 && TREE_TYPE (shadow) == old_decl)
1649 binding->value = x;
1650 INHERITED_VALUE_BINDING_P (binding) = 0;
1651 TREE_TYPE (shadow) = x;
1652 IDENTIFIER_CLASS_VALUE (name) = x;
1653 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
1658 /* If we didn't replace an existing binding, put the binding on the
1659 stack of bindings for the identifier, and update the shadowed list. */
1660 if (push_class_binding (name, x))
1662 class_binding_level->class_shadowed
1663 = tree_cons (name, NULL,
1664 class_binding_level->class_shadowed);
1665 /* Record the value we are binding NAME to so that we can know
1666 what to pop later. */
1667 TREE_TYPE (class_binding_level->class_shadowed) = x;
1668 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
1671 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, false);
1674 void
1675 set_class_shadows (tree shadows)
1677 class_binding_level->class_shadowed = shadows;
1680 /* Return the binding value for name in scope. */
1682 tree
1683 namespace_binding (tree name, tree scope)
1685 cxx_binding *binding;
1687 if (scope == NULL)
1688 scope = global_namespace;
1689 scope = ORIGINAL_NAMESPACE (scope);
1690 binding = cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
1692 return binding ? binding->value : NULL_TREE;
1695 /* Set the binding value for name in scope. */
1697 void
1698 set_namespace_binding (tree name, tree scope, tree val)
1700 cxx_binding *b;
1702 timevar_push (TV_NAME_LOOKUP);
1703 if (scope == NULL_TREE)
1704 scope = global_namespace;
1705 b = binding_for_name (NAMESPACE_LEVEL (scope), name);
1706 if (!b->value || TREE_CODE (val) == OVERLOAD || val == error_mark_node)
1707 b->value = val;
1708 else
1709 supplement_binding (b, val);
1710 timevar_pop (TV_NAME_LOOKUP);
1713 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we
1714 select a name that is unique to this compilation unit. */
1716 void
1717 push_namespace (tree name)
1719 tree d = NULL_TREE;
1720 int need_new = 1;
1721 int implicit_use = 0;
1723 timevar_push (TV_NAME_LOOKUP);
1725 /* We should not get here if the global_namespace is not yet constructed
1726 nor if NAME designates the global namespace: The global scope is
1727 constructed elsewhere. */
1728 my_friendly_assert (global_namespace != NULL && name != global_scope_name,
1729 20030531);
1731 if (!name)
1733 /* The name of anonymous namespace is unique for the translation
1734 unit. */
1735 if (!anonymous_namespace_name)
1736 anonymous_namespace_name = get_file_function_name ('N');
1737 name = anonymous_namespace_name;
1738 d = IDENTIFIER_NAMESPACE_VALUE (name);
1739 if (d)
1740 /* Reopening anonymous namespace. */
1741 need_new = 0;
1742 implicit_use = 1;
1744 else
1746 /* Check whether this is an extended namespace definition. */
1747 d = IDENTIFIER_NAMESPACE_VALUE (name);
1748 if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
1750 need_new = 0;
1751 if (DECL_NAMESPACE_ALIAS (d))
1753 error ("namespace alias `%D' not allowed here, assuming `%D'",
1754 d, DECL_NAMESPACE_ALIAS (d));
1755 d = DECL_NAMESPACE_ALIAS (d);
1760 if (need_new)
1762 /* Make a new namespace, binding the name to it. */
1763 d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
1764 DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace);
1765 d = pushdecl (d);
1766 begin_scope (sk_namespace, d);
1768 else
1769 resume_scope (NAMESPACE_LEVEL (d));
1771 if (implicit_use)
1772 do_using_directive (d);
1773 /* Enter the name space. */
1774 current_namespace = d;
1776 timevar_pop (TV_NAME_LOOKUP);
1779 /* Pop from the scope of the current namespace. */
1781 void
1782 pop_namespace (void)
1784 my_friendly_assert (current_namespace != global_namespace, 20010801);
1785 current_namespace = CP_DECL_CONTEXT (current_namespace);
1786 /* The binding level is not popped, as it might be re-opened later. */
1787 leave_scope ();
1790 /* Push into the scope of the namespace NS, even if it is deeply
1791 nested within another namespace. */
1793 void
1794 push_nested_namespace (tree ns)
1796 if (ns == global_namespace)
1797 push_to_top_level ();
1798 else
1800 push_nested_namespace (CP_DECL_CONTEXT (ns));
1801 push_namespace (DECL_NAME (ns));
1805 /* Pop back from the scope of the namespace NS, which was previously
1806 entered with push_nested_namespace. */
1808 void
1809 pop_nested_namespace (tree ns)
1811 timevar_push (TV_NAME_LOOKUP);
1812 while (ns != global_namespace)
1814 pop_namespace ();
1815 ns = CP_DECL_CONTEXT (ns);
1818 pop_from_top_level ();
1819 timevar_pop (TV_NAME_LOOKUP);
1822 /* Like pushdecl, only it places X in the current namespace,
1823 if appropriate. */
1825 tree
1826 pushdecl_namespace_level (tree x)
1828 register struct cp_binding_level *b = current_binding_level;
1829 register tree t;
1831 timevar_push (TV_NAME_LOOKUP);
1832 t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace));
1834 /* Now, the type_shadowed stack may screw us. Munge it so it does
1835 what we want. */
1836 if (TREE_CODE (x) == TYPE_DECL)
1838 tree name = DECL_NAME (x);
1839 tree newval;
1840 tree *ptr = (tree *)0;
1841 for (; !global_scope_p (b); b = b->level_chain)
1843 tree shadowed = b->type_shadowed;
1844 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
1845 if (TREE_PURPOSE (shadowed) == name)
1847 ptr = &TREE_VALUE (shadowed);
1848 /* Can't break out of the loop here because sometimes
1849 a binding level will have duplicate bindings for
1850 PT names. It's gross, but I haven't time to fix it. */
1853 newval = TREE_TYPE (x);
1854 if (ptr == (tree *)0)
1856 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
1857 up here if this is changed to an assertion. --KR */
1858 SET_IDENTIFIER_TYPE_VALUE (name, x);
1860 else
1862 *ptr = newval;
1865 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
1868 /* Return the declarations that are members of the namespace NS. */
1870 tree
1871 cp_namespace_decls (tree ns)
1873 return NAMESPACE_LEVEL (ns)->names;
1876 /* Combine prefer_type and namespaces_only into flags. */
1878 static int
1879 lookup_flags (int prefer_type, int namespaces_only)
1881 if (namespaces_only)
1882 return LOOKUP_PREFER_NAMESPACES;
1883 if (prefer_type > 1)
1884 return LOOKUP_PREFER_TYPES;
1885 if (prefer_type > 0)
1886 return LOOKUP_PREFER_BOTH;
1887 return 0;
1890 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
1891 ignore it or not. Subroutine of lookup_name_real. */
1893 static tree
1894 qualify_lookup (tree val, int flags)
1896 if (val == NULL_TREE)
1897 return val;
1898 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
1899 return val;
1900 if ((flags & LOOKUP_PREFER_TYPES)
1901 && (TREE_CODE (val) == TYPE_DECL || TREE_CODE (val) == TEMPLATE_DECL))
1902 return val;
1903 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
1904 return NULL_TREE;
1905 return val;
1908 /* Look up NAME in the NAMESPACE. */
1910 tree
1911 lookup_namespace_name (tree namespace, tree name)
1913 tree val;
1914 tree template_id = NULL_TREE;
1915 cxx_binding binding;
1917 timevar_push (TV_NAME_LOOKUP);
1918 my_friendly_assert (TREE_CODE (namespace) == NAMESPACE_DECL, 370);
1920 if (TREE_CODE (name) == NAMESPACE_DECL)
1921 /* This happens for A::B<int> when B is a namespace. */
1922 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, name);
1923 else if (TREE_CODE (name) == TEMPLATE_DECL)
1925 /* This happens for A::B where B is a template, and there are no
1926 template arguments. */
1927 error ("invalid use of `%D'", name);
1928 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
1931 namespace = ORIGINAL_NAMESPACE (namespace);
1933 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1935 template_id = name;
1936 name = TREE_OPERAND (name, 0);
1937 if (TREE_CODE (name) == OVERLOAD)
1938 name = DECL_NAME (OVL_CURRENT (name));
1939 else if (DECL_P (name))
1940 name = DECL_NAME (name);
1943 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 373);
1945 cxx_binding_clear (&binding);
1946 if (!qualified_lookup_using_namespace (name, namespace, &binding, 0))
1947 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
1949 if (binding.value)
1951 val = binding.value;
1953 if (template_id)
1955 if (DECL_CLASS_TEMPLATE_P (val))
1956 val = lookup_template_class (val,
1957 TREE_OPERAND (template_id, 1),
1958 /*in_decl=*/NULL_TREE,
1959 /*context=*/NULL_TREE,
1960 /*entering_scope=*/0,
1961 tf_error | tf_warning);
1962 else if (DECL_FUNCTION_TEMPLATE_P (val)
1963 || TREE_CODE (val) == OVERLOAD)
1964 val = lookup_template_function (val,
1965 TREE_OPERAND (template_id, 1));
1966 else
1968 error ("`%D::%D' is not a template",
1969 namespace, name);
1970 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
1974 /* If we have a single function from a using decl, pull it out. */
1975 if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
1976 val = OVL_FUNCTION (val);
1978 /* Ignore built-in functions that haven't been prototyped yet. */
1979 if (!val || !DECL_P(val)
1980 || !DECL_LANG_SPECIFIC(val)
1981 || !DECL_ANTICIPATED (val))
1982 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
1985 error ("`%D' undeclared in namespace `%D'", name, namespace);
1986 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
1989 /* Select the right _DECL from multiple choices. */
1991 static tree
1992 select_decl (cxx_binding *binding, int flags)
1994 tree val;
1995 val = binding->value;
1997 timevar_push (TV_NAME_LOOKUP);
1998 if (LOOKUP_NAMESPACES_ONLY (flags))
2000 /* We are not interested in types. */
2001 if (val && TREE_CODE (val) == NAMESPACE_DECL)
2002 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
2003 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2006 /* If looking for a type, or if there is no non-type binding, select
2007 the value binding. */
2008 if (binding->type && (!val || (flags & LOOKUP_PREFER_TYPES)))
2009 val = binding->type;
2010 /* Don't return non-types if we really prefer types. */
2011 else if (val && LOOKUP_TYPES_ONLY (flags) && TREE_CODE (val) != TYPE_DECL
2012 && (TREE_CODE (val) != TEMPLATE_DECL
2013 || !DECL_CLASS_TEMPLATE_P (val)))
2014 val = NULL_TREE;
2016 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
2019 /* Unscoped lookup of a global: iterate over current namespaces,
2020 considering using-directives. If SPACESP is non-NULL, store a list
2021 of the namespaces we've considered in it. */
2023 tree
2024 unqualified_namespace_lookup (tree name, int flags, tree* spacesp)
2026 tree initial = current_decl_namespace ();
2027 tree scope = initial;
2028 tree siter;
2029 struct cp_binding_level *level;
2030 tree val = NULL_TREE;
2031 cxx_binding binding;
2033 timevar_push (TV_NAME_LOOKUP);
2034 cxx_binding_clear (&binding);
2035 if (spacesp)
2036 *spacesp = NULL_TREE;
2038 for (; !val; scope = CP_DECL_CONTEXT (scope))
2040 cxx_binding *b =
2041 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
2042 if (spacesp)
2043 *spacesp = tree_cons (scope, NULL_TREE, *spacesp);
2045 /* Ignore anticipated built-in functions. */
2046 if (b && b->value && DECL_P (b->value)
2047 && DECL_LANG_SPECIFIC (b->value) && DECL_ANTICIPATED (b->value))
2048 /* Keep binding cleared. */;
2049 else if (b)
2051 /* Initialize binding for this context. */
2052 binding.value = b->value;
2053 binding.type = b->type;
2056 /* Add all _DECLs seen through local using-directives. */
2057 for (level = current_binding_level;
2058 level->kind != sk_namespace;
2059 level = level->level_chain)
2060 if (!lookup_using_namespace (name, &binding, level->using_directives,
2061 scope, flags, spacesp))
2062 /* Give up because of error. */
2063 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
2065 /* Add all _DECLs seen through global using-directives. */
2066 /* XXX local and global using lists should work equally. */
2067 siter = initial;
2068 while (1)
2070 if (!lookup_using_namespace (name, &binding,
2071 DECL_NAMESPACE_USING (siter),
2072 scope, flags, spacesp))
2073 /* Give up because of error. */
2074 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
2075 if (siter == scope) break;
2076 siter = CP_DECL_CONTEXT (siter);
2079 val = select_decl (&binding, flags);
2080 if (scope == global_namespace)
2081 break;
2083 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
2086 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
2087 or a class TYPE). If IS_TYPE_P is TRUE, then ignore non-type
2088 bindings.
2090 Returns a DECL (or OVERLOAD, or BASELINK) representing the
2091 declaration found. If no suitable declaration can be found,
2092 ERROR_MARK_NODE is returned. Iif COMPLAIN is true and SCOPE is
2093 neither a class-type nor a namespace a diagnostic is issued. */
2095 tree
2096 lookup_qualified_name (tree scope, tree name, bool is_type_p, bool complain)
2098 int flags = 0;
2100 if (TREE_CODE (scope) == NAMESPACE_DECL)
2102 cxx_binding binding;
2104 cxx_binding_clear (&binding);
2105 flags |= LOOKUP_COMPLAIN;
2106 if (is_type_p)
2107 flags |= LOOKUP_PREFER_TYPES;
2108 if (qualified_lookup_using_namespace (name, scope, &binding,
2109 flags))
2110 return select_decl (&binding, flags);
2112 else if (is_aggr_type (scope, complain))
2114 tree t;
2115 t = lookup_member (scope, name, 0, is_type_p);
2116 if (t)
2117 return t;
2120 return error_mark_node;
2123 /* Look up NAME in the current binding level and its superiors in the
2124 namespace of variables, functions and typedefs. Return a ..._DECL
2125 node of some kind representing its definition if there is only one
2126 such declaration, or return a TREE_LIST with all the overloaded
2127 definitions if there are many, or return 0 if it is undefined.
2129 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
2130 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
2131 Otherwise we prefer non-TYPE_DECLs.
2133 If NONCLASS is nonzero, we don't look for the NAME in class scope,
2134 using IDENTIFIER_CLASS_VALUE. */
2136 tree
2137 lookup_name_real (tree name, int prefer_type, int nonclass,
2138 int namespaces_only, int flags)
2140 cxx_binding *iter;
2141 tree val = NULL_TREE;
2143 timevar_push (TV_NAME_LOOKUP);
2144 /* Conversion operators are handled specially because ordinary
2145 unqualified name lookup will not find template conversion
2146 operators. */
2147 if (IDENTIFIER_TYPENAME_P (name))
2149 struct cp_binding_level *level;
2151 for (level = current_binding_level;
2152 level && level->kind != sk_namespace;
2153 level = level->level_chain)
2155 tree class_type;
2156 tree operators;
2158 /* A conversion operator can only be declared in a class
2159 scope. */
2160 if (level->kind != sk_class)
2161 continue;
2163 /* Lookup the conversion operator in the class. */
2164 class_type = level->this_entity;
2165 operators = lookup_fnfields (class_type, name, /*protect=*/0);
2166 if (operators)
2167 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, operators);
2170 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2173 flags |= lookup_flags (prefer_type, namespaces_only);
2175 /* First, look in non-namespace scopes. */
2177 if (current_class_type == NULL_TREE)
2178 nonclass = 1;
2180 for (iter = IDENTIFIER_BINDING (name); iter; iter = iter->previous)
2182 tree binding;
2184 if (!LOCAL_BINDING_P (iter) && nonclass)
2185 /* We're not looking for class-scoped bindings, so keep going. */
2186 continue;
2188 /* If this is the kind of thing we're looking for, we're done. */
2189 if (qualify_lookup (iter->value, flags))
2190 binding = iter->value;
2191 else if ((flags & LOOKUP_PREFER_TYPES)
2192 && qualify_lookup (iter->type, flags))
2193 binding = iter->type;
2194 else
2195 binding = NULL_TREE;
2197 if (binding)
2199 val = binding;
2200 break;
2204 /* Now lookup in namespace scopes. */
2205 if (!val)
2207 tree t = unqualified_namespace_lookup (name, flags, 0);
2208 if (t)
2209 val = t;
2212 if (val)
2214 /* If we have a single function from a using decl, pull it out. */
2215 if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
2216 val = OVL_FUNCTION (val);
2219 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
2222 tree
2223 lookup_name_nonclass (tree name)
2225 return lookup_name_real (name, 0, 1, 0, LOOKUP_COMPLAIN);
2228 tree
2229 lookup_function_nonclass (tree name, tree args)
2231 return lookup_arg_dependent (name, lookup_name_nonclass (name), args);
2234 tree
2235 lookup_name (tree name, int prefer_type)
2237 return lookup_name_real (name, prefer_type, 0, 0, LOOKUP_COMPLAIN);
2240 /* Similar to `lookup_name' but look only in the innermost non-class
2241 binding level. */
2243 tree
2244 lookup_name_current_level (tree name)
2246 struct cp_binding_level *b;
2247 tree t = NULL_TREE;
2249 timevar_push (TV_NAME_LOOKUP);
2250 b = innermost_nonclass_level ();
2252 if (b->kind == sk_namespace)
2254 t = IDENTIFIER_NAMESPACE_VALUE (name);
2256 /* extern "C" function() */
2257 if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
2258 t = TREE_VALUE (t);
2260 else if (IDENTIFIER_BINDING (name)
2261 && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
2263 while (1)
2265 if (IDENTIFIER_BINDING (name)->scope == b)
2266 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, IDENTIFIER_VALUE (name));
2268 if (b->kind == sk_cleanup)
2269 b = b->level_chain;
2270 else
2271 break;
2275 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
2278 /* Like lookup_name_current_level, but for types. */
2280 tree
2281 lookup_type_current_level (tree name)
2283 register tree t = NULL_TREE;
2285 timevar_push (TV_NAME_LOOKUP);
2286 my_friendly_assert (current_binding_level->kind != sk_namespace,
2287 980716);
2289 if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
2290 && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
2292 struct cp_binding_level *b = current_binding_level;
2293 while (1)
2295 if (purpose_member (name, b->type_shadowed))
2296 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
2297 REAL_IDENTIFIER_TYPE_VALUE (name));
2298 if (b->kind == sk_cleanup)
2299 b = b->level_chain;
2300 else
2301 break;
2305 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
2308 /* Add namespace to using_directives. Return NULL_TREE if nothing was
2309 changed (i.e. there was already a directive), or the fresh
2310 TREE_LIST otherwise. */
2312 tree
2313 push_using_directive (tree used)
2315 tree ud = current_binding_level->using_directives;
2316 tree iter, ancestor;
2318 timevar_push (TV_NAME_LOOKUP);
2319 /* Check if we already have this. */
2320 if (purpose_member (used, ud) != NULL_TREE)
2321 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2323 ancestor = namespace_ancestor (current_decl_namespace (), used);
2324 ud = current_binding_level->using_directives;
2325 ud = tree_cons (used, ancestor, ud);
2326 current_binding_level->using_directives = ud;
2328 /* Recursively add all namespaces used. */
2329 for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
2330 push_using_directive (TREE_PURPOSE (iter));
2332 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ud);
2335 /* The type TYPE is being declared. If it is a class template, or a
2336 specialization of a class template, do any processing required and
2337 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
2338 being declared a friend. B is the binding level at which this TYPE
2339 should be bound.
2341 Returns the TYPE_DECL for TYPE, which may have been altered by this
2342 processing. */
2344 static tree
2345 maybe_process_template_type_declaration (tree type, int globalize,
2346 cxx_scope *b)
2348 tree decl = TYPE_NAME (type);
2350 if (processing_template_parmlist)
2351 /* You can't declare a new template type in a template parameter
2352 list. But, you can declare a non-template type:
2354 template <class A*> struct S;
2356 is a forward-declaration of `A'. */
2358 else
2360 maybe_check_template_type (type);
2362 my_friendly_assert (IS_AGGR_TYPE (type)
2363 || TREE_CODE (type) == ENUMERAL_TYPE, 0);
2366 if (processing_template_decl)
2368 /* This may change after the call to
2369 push_template_decl_real, but we want the original value. */
2370 tree name = DECL_NAME (decl);
2372 decl = push_template_decl_real (decl, globalize);
2373 /* If the current binding level is the binding level for the
2374 template parameters (see the comment in
2375 begin_template_parm_list) and the enclosing level is a class
2376 scope, and we're not looking at a friend, push the
2377 declaration of the member class into the class scope. In the
2378 friend case, push_template_decl will already have put the
2379 friend into global scope, if appropriate. */
2380 if (TREE_CODE (type) != ENUMERAL_TYPE
2381 && !globalize && b->kind == sk_template_parms
2382 && b->level_chain->kind == sk_class)
2384 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
2385 /* Put this UDT in the table of UDTs for the class, since
2386 that won't happen below because B is not the class
2387 binding level, but is instead the pseudo-global level. */
2388 if (b->level_chain->type_decls == NULL)
2389 b->level_chain->type_decls =
2390 binding_table_new (SCOPE_DEFAULT_HT_SIZE);
2391 binding_table_insert (b->level_chain->type_decls, name, type);
2392 if (!COMPLETE_TYPE_P (current_class_type))
2394 maybe_add_class_template_decl_list (current_class_type,
2395 type, /*friend_p=*/0);
2396 CLASSTYPE_NESTED_UTDS (current_class_type) =
2397 b->level_chain->type_decls;
2403 return decl;
2406 /* Push a tag name NAME for struct/class/union/enum type TYPE.
2407 Normally put it into the inner-most non-sk_cleanup scope,
2408 but if GLOBALIZE is true, put it in the inner-most non-class scope.
2409 The latter is needed for implicit declarations. */
2411 void
2412 pushtag (tree name, tree type, int globalize)
2414 register struct cp_binding_level *b;
2416 timevar_push (TV_NAME_LOOKUP);
2417 b = current_binding_level;
2418 while (b->kind == sk_cleanup
2419 || (b->kind == sk_class
2420 && (globalize
2421 /* We may be defining a new type in the initializer
2422 of a static member variable. We allow this when
2423 not pedantic, and it is particularly useful for
2424 type punning via an anonymous union. */
2425 || COMPLETE_TYPE_P (b->this_entity))))
2426 b = b->level_chain;
2428 if (b->type_decls == NULL)
2429 b->type_decls = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
2430 binding_table_insert (b->type_decls, name, type);
2432 if (name)
2434 /* Do C++ gratuitous typedefing. */
2435 if (IDENTIFIER_TYPE_VALUE (name) != type)
2437 register tree d = NULL_TREE;
2438 int in_class = 0;
2439 tree context = TYPE_CONTEXT (type);
2441 if (! context)
2443 tree cs = current_scope ();
2445 if (! globalize)
2446 context = cs;
2447 else if (cs != NULL_TREE && TYPE_P (cs))
2448 /* When declaring a friend class of a local class, we want
2449 to inject the newly named class into the scope
2450 containing the local class, not the namespace scope. */
2451 context = decl_function_context (get_type_decl (cs));
2453 if (!context)
2454 context = current_namespace;
2456 if (b->kind == sk_class
2457 || (b->kind == sk_template_parms
2458 && b->level_chain->kind == sk_class))
2459 in_class = 1;
2461 if (current_lang_name == lang_name_java)
2462 TYPE_FOR_JAVA (type) = 1;
2464 d = create_implicit_typedef (name, type);
2465 DECL_CONTEXT (d) = FROB_CONTEXT (context);
2466 if (! in_class)
2467 set_identifier_type_value_with_scope (name, d, b);
2469 d = maybe_process_template_type_declaration (type,
2470 globalize, b);
2472 if (b->kind == sk_class)
2474 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
2475 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
2476 class. But if it's a member template class, we
2477 want the TEMPLATE_DECL, not the TYPE_DECL, so this
2478 is done later. */
2479 finish_member_declaration (d);
2480 else
2481 pushdecl_class_level (d);
2483 else
2484 d = pushdecl_with_scope (d, b);
2486 /* FIXME what if it gets a name from typedef? */
2487 if (ANON_AGGRNAME_P (name))
2488 DECL_IGNORED_P (d) = 1;
2490 TYPE_CONTEXT (type) = DECL_CONTEXT (d);
2492 /* If this is a local class, keep track of it. We need this
2493 information for name-mangling, and so that it is possible to find
2494 all function definitions in a translation unit in a convenient
2495 way. (It's otherwise tricky to find a member function definition
2496 it's only pointed to from within a local class.) */
2497 if (TYPE_CONTEXT (type)
2498 && TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL
2499 && !processing_template_decl)
2500 VARRAY_PUSH_TREE (local_classes, type);
2502 if (b->kind == sk_class
2503 && !COMPLETE_TYPE_P (current_class_type))
2505 maybe_add_class_template_decl_list (current_class_type,
2506 type, /*friend_p=*/0);
2507 CLASSTYPE_NESTED_UTDS (current_class_type) = b->type_decls;
2511 if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL)
2512 /* Use the canonical TYPE_DECL for this node. */
2513 TYPE_STUB_DECL (type) = TYPE_NAME (type);
2514 else
2516 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE
2517 will be the tagged type we just added to the current
2518 binding level. This fake NULL-named TYPE_DECL node helps
2519 dwarfout.c to know when it needs to output a
2520 representation of a tagged type, and it also gives us a
2521 convenient place to record the "scope start" address for
2522 the tagged type. */
2524 tree d = build_decl (TYPE_DECL, NULL_TREE, type);
2525 TYPE_STUB_DECL (type) = pushdecl_with_scope (d, b);
2527 timevar_pop (TV_NAME_LOOKUP);
2530 /* Allocate storage for saving a C++ binding. */
2531 #define cxx_saved_binding_make() \
2532 (ggc_alloc (sizeof (cxx_saved_binding)))
2534 struct cxx_saved_binding GTY(())
2536 /* Link that chains saved C++ bindings for a given name into a stack. */
2537 cxx_saved_binding *previous;
2538 /* The name of the current binding. */
2539 tree identifier;
2540 /* The binding we're saving. */
2541 cxx_binding *binding;
2542 tree class_value;
2543 tree real_type_value;
2546 /* Subroutines for reverting temporarily to top-level for instantiation
2547 of templates and such. We actually need to clear out the class- and
2548 local-value slots of all identifiers, so that only the global values
2549 are at all visible. Simply setting current_binding_level to the global
2550 scope isn't enough, because more binding levels may be pushed. */
2551 struct saved_scope *scope_chain;
2553 static cxx_saved_binding *
2554 store_bindings (tree names, cxx_saved_binding *old_bindings)
2556 tree t;
2557 cxx_saved_binding *search_bindings = old_bindings;
2559 timevar_push (TV_NAME_LOOKUP);
2560 for (t = names; t; t = TREE_CHAIN (t))
2562 tree id;
2563 cxx_saved_binding *saved;
2564 cxx_saved_binding *t1;
2566 if (TREE_CODE (t) == TREE_LIST)
2567 id = TREE_PURPOSE (t);
2568 else
2569 id = DECL_NAME (t);
2571 if (!id
2572 /* Note that we may have an IDENTIFIER_CLASS_VALUE even when
2573 we have no IDENTIFIER_BINDING if we have left the class
2574 scope, but cached the class-level declarations. */
2575 || !(IDENTIFIER_BINDING (id) || IDENTIFIER_CLASS_VALUE (id)))
2576 continue;
2578 for (t1 = search_bindings; t1; t1 = t1->previous)
2579 if (t1->identifier == id)
2580 goto skip_it;
2582 my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 135);
2583 saved = cxx_saved_binding_make ();
2584 saved->previous = old_bindings;
2585 saved->identifier = id;
2586 saved->binding = IDENTIFIER_BINDING (id);
2587 saved->class_value = IDENTIFIER_CLASS_VALUE (id);;
2588 saved->real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
2589 IDENTIFIER_BINDING (id) = NULL;
2590 IDENTIFIER_CLASS_VALUE (id) = NULL_TREE;
2591 old_bindings = saved;
2592 skip_it:
2595 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, old_bindings);
2598 void
2599 maybe_push_to_top_level (int pseudo)
2601 struct saved_scope *s;
2602 struct cp_binding_level *b;
2603 cxx_saved_binding *old_bindings;
2604 int need_pop;
2606 timevar_push (TV_NAME_LOOKUP);
2607 s = ggc_alloc_cleared (sizeof (struct saved_scope));
2609 b = scope_chain ? current_binding_level : 0;
2611 /* If we're in the middle of some function, save our state. */
2612 if (cfun)
2614 need_pop = 1;
2615 push_function_context_to (NULL_TREE);
2617 else
2618 need_pop = 0;
2620 old_bindings = NULL;
2621 if (scope_chain && previous_class_type)
2622 old_bindings = store_bindings (previous_class_values, old_bindings);
2624 /* Have to include the global scope, because class-scope decls
2625 aren't listed anywhere useful. */
2626 for (; b; b = b->level_chain)
2628 tree t;
2630 /* Template IDs are inserted into the global level. If they were
2631 inserted into namespace level, finish_file wouldn't find them
2632 when doing pending instantiations. Therefore, don't stop at
2633 namespace level, but continue until :: . */
2634 if (global_scope_p (b) || (pseudo && b->kind == sk_template_parms))
2635 break;
2637 old_bindings = store_bindings (b->names, old_bindings);
2638 /* We also need to check class_shadowed to save class-level type
2639 bindings, since pushclass doesn't fill in b->names. */
2640 if (b->kind == sk_class)
2641 old_bindings = store_bindings (b->class_shadowed, old_bindings);
2643 /* Unwind type-value slots back to top level. */
2644 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
2645 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
2647 s->prev = scope_chain;
2648 s->old_bindings = old_bindings;
2649 s->bindings = b;
2650 s->need_pop_function_context = need_pop;
2651 s->function_decl = current_function_decl;
2652 s->last_parms = last_function_parms;
2654 scope_chain = s;
2655 current_function_decl = NULL_TREE;
2656 VARRAY_TREE_INIT (current_lang_base, 10, "current_lang_base");
2657 current_lang_name = lang_name_cplusplus;
2658 current_namespace = global_namespace;
2659 timevar_pop (TV_NAME_LOOKUP);
2662 void
2663 push_to_top_level (void)
2665 maybe_push_to_top_level (0);
2668 void
2669 pop_from_top_level (void)
2671 struct saved_scope *s = scope_chain;
2672 cxx_saved_binding *saved;
2674 timevar_push (TV_NAME_LOOKUP);
2675 /* Clear out class-level bindings cache. */
2676 if (previous_class_type)
2677 invalidate_class_lookup_cache ();
2679 current_lang_base = 0;
2681 scope_chain = s->prev;
2682 for (saved = s->old_bindings; saved; saved = saved->previous)
2684 tree id = saved->identifier;
2686 IDENTIFIER_BINDING (id) = saved->binding;
2687 IDENTIFIER_CLASS_VALUE (id) = saved->class_value;
2688 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
2691 /* If we were in the middle of compiling a function, restore our
2692 state. */
2693 if (s->need_pop_function_context)
2694 pop_function_context_from (NULL_TREE);
2695 current_function_decl = s->function_decl;
2696 last_function_parms = s->last_parms;
2697 timevar_pop (TV_NAME_LOOKUP);
2700 /* Pop off extraneous binding levels left over due to syntax errors.
2702 We don't pop past namespaces, as they might be valid. */
2704 void
2705 pop_everything (void)
2707 if (ENABLE_SCOPE_CHECKING)
2708 verbatim ("XXX entering pop_everything ()\n");
2709 while (!toplevel_bindings_p ())
2711 if (current_binding_level->kind == sk_class)
2712 pop_nested_class ();
2713 else
2714 poplevel (0, 0, 0);
2716 if (ENABLE_SCOPE_CHECKING)
2717 verbatim ("XXX leaving pop_everything ()\n");
2720 #include "gt-cp-name-lookup.h"