OSDN Git Service

* cp-tree.h (lang_identifier): Remove class_value.
[pf3gnuchains/gcc-fork.git] / gcc / cp / name-lookup.c
1 /* Definitions for C++ name lookup routines.
2    Copyright (C) 2003, 2004 Free Software Foundation, Inc.
3    Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
4
5 This file is part of GCC.
6
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.
11
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.
16
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.  */
21
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.h"
33 #include "debug.h"
34
35 /* The bindings for a particular name in a particular scope.  */
36
37 struct scope_binding {
38   tree value;
39   tree type;
40 };
41 #define EMPTY_SCOPE_BINDING { NULL_TREE, NULL_TREE }
42
43 static cxx_scope *innermost_nonclass_level (void);
44 static tree select_decl (const struct scope_binding *, int);
45 static cxx_binding *binding_for_name (cxx_scope *, tree);
46 static tree lookup_name_current_level (tree);
47 static tree push_overloaded_decl (tree, int);
48 static bool lookup_using_namespace (tree, struct scope_binding *, tree,
49                                     tree, int);
50 static bool qualified_lookup_using_namespace (tree, tree,
51                                               struct scope_binding *, int);
52 static tree lookup_type_current_level (tree);
53 static tree push_using_directive (tree);
54 static void cp_emit_debug_info_for_using (tree, tree);
55
56 /* The :: namespace.  */
57
58 tree global_namespace;
59
60 /* The name of the anonymous namespace, throughout this translation
61    unit.  */
62 static GTY(()) tree anonymous_namespace_name;
63
64
65 /* Compute the chain index of a binding_entry given the HASH value of its
66    name and the total COUNT of chains.  COUNT is assumed to be a power
67    of 2.  */
68
69 #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
70
71 /* A free list of "binding_entry"s awaiting for re-use.  */
72
73 static GTY((deletable)) binding_entry free_binding_entry = NULL;
74
75 /* Create a binding_entry object for (NAME, TYPE).  */
76
77 static inline binding_entry
78 binding_entry_make (tree name, tree type)
79 {
80   binding_entry entry;
81
82   if (free_binding_entry)
83     {
84       entry = free_binding_entry;
85       free_binding_entry = entry->chain;
86     }
87   else
88     entry = ggc_alloc (sizeof (struct binding_entry_s));
89
90   entry->name = name;
91   entry->type = type;
92   entry->chain = NULL;
93
94   return entry;
95 }
96
97 /* Put ENTRY back on the free list.  */
98
99 static inline void
100 binding_entry_free (binding_entry entry)
101 {
102   entry->name = NULL;
103   entry->type = NULL;
104   entry->chain = free_binding_entry;
105   free_binding_entry = entry;
106 }
107
108 /* The datatype used to implement the mapping from names to types at
109    a given scope.  */
110 struct binding_table_s GTY(())
111 {
112   /* Array of chains of "binding_entry"s  */
113   binding_entry * GTY((length ("%h.chain_count"))) chain;
114
115   /* The number of chains in this table.  This is the length of the
116      the member "chain" considered as an array.  */
117   size_t chain_count;
118
119   /* Number of "binding_entry"s in this table.  */
120   size_t entry_count;
121 };
122
123 /* Construct TABLE with an initial CHAIN_COUNT.  */
124
125 static inline void
126 binding_table_construct (binding_table table, size_t chain_count)
127 {
128   table->chain_count = chain_count;
129   table->entry_count = 0;
130   table->chain = ggc_alloc_cleared
131     (table->chain_count * sizeof (binding_entry));
132 }
133
134 /* Make TABLE's entries ready for reuse.  */
135
136 static void
137 binding_table_free (binding_table table)
138 {
139   size_t i;
140   size_t count;
141
142   if (table == NULL)
143     return;
144
145   for (i = 0, count = table->chain_count; i < count; ++i)
146     {
147       binding_entry temp = table->chain[i];
148       while (temp != NULL)
149         {
150           binding_entry entry = temp;
151           temp = entry->chain;
152           binding_entry_free (entry);
153         }
154       table->chain[i] = NULL;
155     }
156   table->entry_count = 0;
157 }
158
159 /* Allocate a table with CHAIN_COUNT, assumed to be a power of two.  */
160
161 static inline binding_table
162 binding_table_new (size_t chain_count)
163 {
164   binding_table table = ggc_alloc (sizeof (struct binding_table_s));
165   table->chain = NULL;
166   binding_table_construct (table, chain_count);
167   return table;
168 }
169
170 /* Expand TABLE to twice its current chain_count.  */
171
172 static void
173 binding_table_expand (binding_table table)
174 {
175   const size_t old_chain_count = table->chain_count;
176   const size_t old_entry_count = table->entry_count;
177   const size_t new_chain_count = 2 * old_chain_count;
178   binding_entry *old_chains = table->chain;
179   size_t i;
180
181   binding_table_construct (table, new_chain_count);
182   for (i = 0; i < old_chain_count; ++i)
183     {
184       binding_entry entry = old_chains[i];
185       for (; entry != NULL; entry = old_chains[i])
186         {
187           const unsigned int hash = IDENTIFIER_HASH_VALUE (entry->name);
188           const size_t j = ENTRY_INDEX (hash, new_chain_count);
189
190           old_chains[i] = entry->chain;
191           entry->chain = table->chain[j];
192           table->chain[j] = entry;
193         }
194     }
195   table->entry_count = old_entry_count;
196 }
197
198 /* Insert a binding for NAME to TYPE into TABLE.  */
199
200 static void
201 binding_table_insert (binding_table table, tree name, tree type)
202 {
203   const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
204   const size_t i = ENTRY_INDEX (hash, table->chain_count);
205   binding_entry entry = binding_entry_make (name, type);
206
207   entry->chain = table->chain[i];
208   table->chain[i] = entry;
209   ++table->entry_count;
210
211   if (3 * table->chain_count < 5 * table->entry_count)
212     binding_table_expand (table);
213 }
214
215 /* Return the binding_entry, if any, that maps NAME.  */
216
217 binding_entry
218 binding_table_find (binding_table table, tree name)
219 {
220   const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
221   binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)];
222
223   while (entry != NULL && entry->name != name)
224     entry = entry->chain;
225
226   return entry;
227 }
228
229 /* Return the binding_entry, if any, that maps NAME to an anonymous type.  */
230
231 static tree
232 binding_table_find_anon_type (binding_table table, tree name)
233 {
234   const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
235   binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)];
236
237   while (entry != NULL && TYPE_IDENTIFIER (entry->type) != name)
238     entry = entry->chain;
239
240   return entry ? entry->type : NULL;
241 }
242
243 /* Return the binding_entry, if any, that has TYPE as target.  If NAME
244    is non-null, then set the domain and rehash that entry.  */
245
246 static binding_entry
247 binding_table_reverse_maybe_remap (binding_table table, tree type, tree name)
248 {
249   const size_t chain_count = table->chain_count;
250   binding_entry entry = NULL;
251   binding_entry *p = NULL;
252   size_t i;
253
254   for (i = 0; i < chain_count && entry == NULL; ++i)
255     {
256       p = &table->chain[i];
257       while (*p != NULL && entry == NULL)
258         if ((*p)->type == type)
259           entry = *p;
260         else
261           p = &(*p)->chain;
262     }
263
264   if (entry != NULL && name != NULL && entry->name != name)
265     {
266       /* Remove the bucket from the previous chain.  */
267       *p = (*p)->chain;
268
269       /* Remap the name type to type.  */
270       i = ENTRY_INDEX (IDENTIFIER_HASH_VALUE (name), chain_count);
271       entry->chain = table->chain[i];
272       entry->name = name;
273       table->chain[i] = entry;
274     }
275
276   return entry;
277 }
278
279 /* Remove from TABLE all entries that map to anonymous enums or
280    class-types.  */
281
282 void
283 binding_table_remove_anonymous_types (binding_table table)
284 {
285   const size_t chain_count = table->chain_count;
286   size_t i;
287
288   for (i = 0; i < chain_count; ++i)
289     {
290       binding_entry *p = &table->chain[i];
291
292       while (*p != NULL)
293         if (ANON_AGGRNAME_P ((*p)->name))
294           {
295             binding_entry e = *p;
296             *p = (*p)->chain;
297             --table->entry_count;
298             binding_entry_free (e);
299           }
300         else
301           p = &(*p)->chain;
302     }
303 }
304
305 /* Apply PROC -- with DATA -- to all entries in TABLE.  */
306
307 void
308 binding_table_foreach (binding_table table, bt_foreach_proc proc, void *data)
309 {
310   const size_t chain_count = table->chain_count;
311   size_t i;
312
313   for (i = 0; i < chain_count; ++i)
314     {
315       binding_entry entry = table->chain[i];
316       for (; entry != NULL; entry = entry->chain)
317         proc (entry, data);
318     }
319 }
320 \f
321 #ifndef ENABLE_SCOPE_CHECKING
322 #  define ENABLE_SCOPE_CHECKING 0
323 #else
324 #  define ENABLE_SCOPE_CHECKING 1
325 #endif
326
327 /* A free list of "cxx_binding"s, connected by their PREVIOUS.  */
328
329 static GTY((deletable)) cxx_binding *free_bindings;
330
331 /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
332    field to NULL.  */
333
334 static inline void
335 cxx_binding_init (cxx_binding *binding, tree value, tree type)
336 {
337   binding->value = value;
338   binding->type = type;
339   binding->previous = NULL;
340 }
341
342 /* (GC)-allocate a binding object with VALUE and TYPE member initialized.  */
343
344 static cxx_binding *
345 cxx_binding_make (tree value, tree type)
346 {
347   cxx_binding *binding;
348   if (free_bindings)
349     {
350       binding = free_bindings;
351       free_bindings = binding->previous;
352     }
353   else
354     binding = ggc_alloc (sizeof (cxx_binding));
355
356   cxx_binding_init (binding, value, type);
357
358   return binding;
359 }
360
361 /* Put BINDING back on the free list.  */
362
363 static inline void
364 cxx_binding_free (cxx_binding *binding)
365 {
366   binding->scope = NULL;
367   binding->previous = free_bindings;
368   free_bindings = binding;
369 }
370
371 /* Make DECL the innermost binding for ID.  The LEVEL is the binding
372    level at which this declaration is being bound.  */
373
374 static void
375 push_binding (tree id, tree decl, cxx_scope* level)
376 {
377   cxx_binding *binding;
378
379   if (level != class_binding_level)
380     binding = cxx_binding_make (decl, NULL_TREE);
381   else
382     {
383       cp_class_binding *cb;
384
385       if (VEC_reserve (cp_class_binding, level->class_shadowed, -1))
386         {
387           /* Fixup the current bindings, as they might have moved.  */
388           size_t i;
389           
390           for (i = 0;
391                (cb = VEC_iterate (cp_class_binding, level->class_shadowed, i));
392                i++)
393             IDENTIFIER_BINDING (cb->identifier) = &cb->base;
394         }
395
396       cb = VEC_quick_push (cp_class_binding, level->class_shadowed, NULL);
397       cb->identifier = id;
398       binding = &cb->base;
399       cxx_binding_init (binding, decl, NULL_TREE);
400     }
401                               
402   /* Now, fill in the binding information.  */
403   binding->previous = IDENTIFIER_BINDING (id);
404   binding->scope = level;
405   INHERITED_VALUE_BINDING_P (binding) = 0;
406   LOCAL_BINDING_P (binding) = (level != class_binding_level);
407
408   /* And put it on the front of the list of bindings for ID.  */
409   IDENTIFIER_BINDING (id) = binding;
410 }
411
412 /* Remove the binding for DECL which should be the innermost binding
413    for ID.  */
414
415 void
416 pop_binding (tree id, tree decl)
417 {
418   cxx_binding *binding;
419
420   if (id == NULL_TREE)
421     /* It's easiest to write the loops that call this function without
422        checking whether or not the entities involved have names.  We
423        get here for such an entity.  */
424     return;
425
426   /* Get the innermost binding for ID.  */
427   binding = IDENTIFIER_BINDING (id);
428
429   /* The name should be bound.  */
430   my_friendly_assert (binding != NULL, 0);
431
432   /* The DECL will be either the ordinary binding or the type
433      binding for this identifier.  Remove that binding.  */
434   if (binding->value == decl)
435     binding->value = NULL_TREE;
436   else if (binding->type == decl)
437     binding->type = NULL_TREE;
438   else
439     abort ();
440
441   if (!binding->value && !binding->type)
442     {
443       /* We're completely done with the innermost binding for this
444          identifier.  Unhook it from the list of bindings.  */
445       IDENTIFIER_BINDING (id) = binding->previous;
446
447       /* Add it to the free list.  */
448       cxx_binding_free (binding);
449     }
450 }
451
452 /* BINDING records an existing declaration for a namein the current scope.
453    But, DECL is another declaration for that same identifier in the
454    same scope.  This is the `struct stat' hack whereby a non-typedef
455    class name or enum-name can be bound at the same level as some other
456    kind of entity.
457    3.3.7/1
458
459      A class name (9.1) or enumeration name (7.2) can be hidden by the
460      name of an object, function, or enumerator declared in the same scope.
461      If a class or enumeration name and an object, function, or enumerator
462      are declared in the same scope (in any order) with the same name, the
463      class or enumeration name is hidden wherever the object, function, or
464      enumerator name is visible.
465
466    It's the responsibility of the caller to check that
467    inserting this name is valid here.  Returns nonzero if the new binding
468    was successful.  */
469
470 static bool
471 supplement_binding (cxx_binding *binding, tree decl)
472 {
473   tree bval = binding->value;
474   bool ok = true;
475
476   timevar_push (TV_NAME_LOOKUP);
477   if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
478     /* The new name is the type name.  */
479     binding->type = decl;
480   else if (/* BVAL is null when push_class_level_binding moves an
481               inherited type-binding out of the way to make room for a
482               new value binding.  */
483            !bval 
484            /* BVAL is error_mark_node when DECL's name has been used
485               in a non-class scope prior declaration.  In that case,
486               we should have already issued a diagnostic; for graceful
487               error recovery purpose, pretend this was the intended
488               declaration for that name.  */
489            || bval == error_mark_node
490            /* If BVAL is a built-in that has not yet been declared,
491               pretend it is not there at all.  */
492            || (TREE_CODE (bval) == FUNCTION_DECL
493                && DECL_ANTICIPATED (bval)))
494     binding->value = decl;
495   else if (TREE_CODE (bval) == TYPE_DECL && DECL_ARTIFICIAL (bval))
496     {
497       /* The old binding was a type name.  It was placed in
498          VALUE field because it was thought, at the point it was
499          declared, to be the only entity with such a name.  Move the
500          type name into the type slot; it is now hidden by the new
501          binding.  */
502       binding->type = bval;
503       binding->value = decl;
504       binding->value_is_inherited = false;
505     }
506   else if (TREE_CODE (bval) == TYPE_DECL
507            && TREE_CODE (decl) == TYPE_DECL
508            && DECL_NAME (decl) == DECL_NAME (bval)
509            && (same_type_p (TREE_TYPE (decl), TREE_TYPE (bval))
510                /* If either type involves template parameters, we must
511                   wait until instantiation.  */
512                || uses_template_parms (TREE_TYPE (decl))
513                || uses_template_parms (TREE_TYPE (bval))))
514     /* We have two typedef-names, both naming the same type to have
515        the same name.  This is OK because of:
516
517          [dcl.typedef]
518
519          In a given scope, a typedef specifier can be used to redefine
520          the name of any type declared in that scope to refer to the
521          type to which it already refers.  */
522     ok = false;
523   /* There can be two block-scope declarations of the same variable,
524      so long as they are `extern' declarations.  However, there cannot
525      be two declarations of the same static data member:
526
527        [class.mem]
528
529        A member shall not be declared twice in the
530        member-specification.  */
531   else if (TREE_CODE (decl) == VAR_DECL && TREE_CODE (bval) == VAR_DECL
532            && DECL_EXTERNAL (decl) && DECL_EXTERNAL (bval)
533            && !DECL_CLASS_SCOPE_P (decl))
534     {
535       duplicate_decls (decl, binding->value);
536       ok = false;
537     }
538   else if (TREE_CODE (decl) == NAMESPACE_DECL
539            && TREE_CODE (bval) == NAMESPACE_DECL
540            && DECL_NAMESPACE_ALIAS (decl)
541            && DECL_NAMESPACE_ALIAS (bval)
542            && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl))
543     /* [namespace.alias]
544        
545       In a declarative region, a namespace-alias-definition can be
546       used to redefine a namespace-alias declared in that declarative
547       region to refer only to the namespace to which it already
548       refers.  */
549     ok = false;
550   else
551     {
552       error ("declaration of `%#D'", decl);
553       cp_error_at ("conflicts with previous declaration `%#D'", bval);
554       ok = false;
555     }
556
557   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ok);
558 }
559
560 /* Add DECL to the list of things declared in B.  */
561
562 static void
563 add_decl_to_level (tree decl, cxx_scope *b)
564 {
565   if (TREE_CODE (decl) == NAMESPACE_DECL 
566       && !DECL_NAMESPACE_ALIAS (decl))
567     {
568       TREE_CHAIN (decl) = b->namespaces;
569       b->namespaces = decl;
570     }
571   else if (TREE_CODE (decl) == VAR_DECL && DECL_VIRTUAL_P (decl))
572     {
573       TREE_CHAIN (decl) = b->vtables;
574       b->vtables = decl;
575     }
576   else       
577     {
578       /* We build up the list in reverse order, and reverse it later if
579          necessary.  */
580       TREE_CHAIN (decl) = b->names;
581       b->names = decl;
582       b->names_size++;
583
584       /* If appropriate, add decl to separate list of statics.  We
585          include extern variables because they might turn out to be 
586          static later.  It's OK for this list to contain a few false
587          positives.  */
588       if (b->kind == sk_namespace)
589         if ((TREE_CODE (decl) == VAR_DECL
590              && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
591             || (TREE_CODE (decl) == FUNCTION_DECL
592                 && (!TREE_PUBLIC (decl) || DECL_DECLARED_INLINE_P (decl))))
593           VARRAY_PUSH_TREE (b->static_decls, decl);
594     }
595 }
596
597 /* Record a decl-node X as belonging to the current lexical scope.
598    Check for errors (such as an incompatible declaration for the same
599    name already seen in the same scope).
600
601    Returns either X or an old decl for the same name.
602    If an old decl is returned, it may have been smashed
603    to agree with what X says.  */
604
605 tree
606 pushdecl (tree x)
607 {
608   tree t;
609   tree name;
610   int need_new_binding;
611
612   timevar_push (TV_NAME_LOOKUP);
613
614   need_new_binding = 1;
615
616   if (DECL_TEMPLATE_PARM_P (x))
617     /* Template parameters have no context; they are not X::T even
618        when declared within a class or namespace.  */
619     ;
620   else
621     {
622       if (current_function_decl && x != current_function_decl
623           /* A local declaration for a function doesn't constitute
624              nesting.  */
625           && TREE_CODE (x) != FUNCTION_DECL
626           /* A local declaration for an `extern' variable is in the
627              scope of the current namespace, not the current
628              function.  */
629           && !(TREE_CODE (x) == VAR_DECL && DECL_EXTERNAL (x))
630           && !DECL_CONTEXT (x))
631         DECL_CONTEXT (x) = current_function_decl;
632
633       /* If this is the declaration for a namespace-scope function,
634          but the declaration itself is in a local scope, mark the
635          declaration.  */
636       if (TREE_CODE (x) == FUNCTION_DECL
637           && DECL_NAMESPACE_SCOPE_P (x)
638           && current_function_decl
639           && x != current_function_decl)
640         DECL_LOCAL_FUNCTION_P (x) = 1;
641     }
642
643   name = DECL_NAME (x);
644   if (name)
645     {
646       int different_binding_level = 0;
647
648       if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
649         name = TREE_OPERAND (name, 0);
650
651       /* In case this decl was explicitly namespace-qualified, look it
652          up in its namespace context.  */
653       if (DECL_NAMESPACE_SCOPE_P (x) && namespace_bindings_p ())
654         t = namespace_binding (name, DECL_CONTEXT (x));
655       else
656         t = lookup_name_current_level (name);
657
658       /* [basic.link] If there is a visible declaration of an entity
659          with linkage having the same name and type, ignoring entities
660          declared outside the innermost enclosing namespace scope, the
661          block scope declaration declares that same entity and
662          receives the linkage of the previous declaration.  */
663       if (! t && current_function_decl && x != current_function_decl
664           && (TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
665           && DECL_EXTERNAL (x))
666         {
667           /* Look in block scope.  */
668           t = IDENTIFIER_VALUE (name);
669           /* Or in the innermost namespace.  */
670           if (! t)
671             t = namespace_binding (name, DECL_CONTEXT (x));
672           /* Does it have linkage?  Note that if this isn't a DECL, it's an
673              OVERLOAD, which is OK.  */
674           if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t)))
675             t = NULL_TREE;
676           if (t)
677             different_binding_level = 1;
678         }
679
680       /* If we are declaring a function, and the result of name-lookup
681          was an OVERLOAD, look for an overloaded instance that is
682          actually the same as the function we are declaring.  (If
683          there is one, we have to merge our declaration with the
684          previous declaration.)  */
685       if (t && TREE_CODE (t) == OVERLOAD)
686         {
687           tree match;
688
689           if (TREE_CODE (x) == FUNCTION_DECL)
690             for (match = t; match; match = OVL_NEXT (match))
691               {
692                 if (decls_match (OVL_CURRENT (match), x))
693                   break;
694               }
695           else
696             /* Just choose one.  */
697             match = t;
698
699           if (match)
700             t = OVL_CURRENT (match);
701           else
702             t = NULL_TREE;
703         }
704
705       if (t && t != error_mark_node)
706         {
707           if (different_binding_level)
708             {
709               if (decls_match (x, t))
710                 /* The standard only says that the local extern
711                    inherits linkage from the previous decl; in
712                    particular, default args are not shared.  We must
713                    also tell cgraph to treat these decls as the same,
714                    or we may neglect to emit an "unused" static - we
715                    do this by making the DECL_UIDs equal, which should
716                    be viewed as a kludge.  FIXME.  */
717                 {
718                   TREE_PUBLIC (x) = TREE_PUBLIC (t);
719                   DECL_UID (x) = DECL_UID (t);
720                 }
721             }
722           else if (TREE_CODE (t) == PARM_DECL)
723             {
724               if (DECL_CONTEXT (t) == NULL_TREE)
725                 /* This is probably caused by too many errors, but calling
726                    abort will say that if errors have occurred.  */
727                 abort ();
728
729               /* Check for duplicate params.  */
730               if (duplicate_decls (x, t))
731                 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
732             }
733           else if ((DECL_EXTERN_C_FUNCTION_P (x)
734                     || DECL_FUNCTION_TEMPLATE_P (x))
735                    && is_overloaded_fn (t))
736             /* Don't do anything just yet.  */;
737           else if (t == wchar_decl_node)
738             {
739               if (pedantic && ! DECL_IN_SYSTEM_HEADER (x))
740                 pedwarn ("redeclaration of `wchar_t' as `%T'",
741                             TREE_TYPE (x));
742
743               /* Throw away the redeclaration.  */
744               POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
745             }
746           else
747             {
748               tree olddecl = duplicate_decls (x, t);
749               
750               /* If the redeclaration failed, we can stop at this
751                  point.  */
752               if (olddecl == error_mark_node)
753                 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
754
755               if (olddecl)
756                 {
757                   if (TREE_CODE (t) == TYPE_DECL)
758                     SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
759                   else if (TREE_CODE (t) == FUNCTION_DECL)
760                     check_default_args (t);
761
762                   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
763                 }
764               else if (DECL_MAIN_P (x) && TREE_CODE (t) == FUNCTION_DECL)
765                 {
766                   /* A redeclaration of main, but not a duplicate of the
767                      previous one.
768                      
769                      [basic.start.main]
770                      
771                      This function shall not be overloaded.  */
772                   cp_error_at ("invalid redeclaration of `%D'", t);
773                   error ("as `%D'", x);
774                   /* We don't try to push this declaration since that
775                      causes a crash.  */
776                   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
777                 }
778             }
779         }
780
781       check_template_shadow (x);
782
783       /* If this is a function conjured up by the backend, massage it
784          so it looks friendly.  */
785       if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x))
786         {
787           retrofit_lang_decl (x);
788           SET_DECL_LANGUAGE (x, lang_c);
789         }
790
791       if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x))
792         {
793           t = push_overloaded_decl (x, PUSH_LOCAL);
794           if (t != x)
795             POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
796           if (!namespace_bindings_p ())
797             /* We do not need to create a binding for this name;
798                push_overloaded_decl will have already done so if
799                necessary.  */
800             need_new_binding = 0;
801         }
802       else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
803         {
804           t = push_overloaded_decl (x, PUSH_GLOBAL);
805           if (t == x)
806             add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
807           POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
808         }
809
810       /* If declaring a type as a typedef, copy the type (unless we're
811          at line 0), and install this TYPE_DECL as the new type's typedef
812          name.  See the extensive comment in ../c-decl.c (pushdecl).  */
813       if (TREE_CODE (x) == TYPE_DECL)
814         {
815           tree type = TREE_TYPE (x);
816           if (DECL_IS_BUILTIN (x))
817             {
818               if (TYPE_NAME (type) == 0)
819                 TYPE_NAME (type) = x;
820             }
821           else if (type != error_mark_node && TYPE_NAME (type) != x
822                    /* We don't want to copy the type when all we're
823                       doing is making a TYPE_DECL for the purposes of
824                       inlining.  */
825                    && (!TYPE_NAME (type)
826                        || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x)))
827             {
828               DECL_ORIGINAL_TYPE (x) = type;
829               type = build_type_copy (type);
830               TYPE_STUB_DECL (type) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
831               TYPE_NAME (type) = x;
832               TREE_TYPE (x) = type;
833             }
834
835           if (type != error_mark_node
836               && TYPE_NAME (type)
837               && TYPE_IDENTIFIER (type))
838             set_identifier_type_value (DECL_NAME (x), x);
839         }
840
841       /* Multiple external decls of the same identifier ought to match.
842
843          We get warnings about inline functions where they are defined.
844          We get warnings about other functions from push_overloaded_decl.
845
846          Avoid duplicate warnings where they are used.  */
847       if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
848         {
849           tree decl;
850
851           decl = IDENTIFIER_NAMESPACE_VALUE (name);
852           if (decl && TREE_CODE (decl) == OVERLOAD)
853             decl = OVL_FUNCTION (decl);
854
855           if (decl && decl != error_mark_node
856               && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl))
857               /* If different sort of thing, we already gave an error.  */
858               && TREE_CODE (decl) == TREE_CODE (x)
859               && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl)))
860             {
861               pedwarn ("type mismatch with previous external decl of `%#D'", x);
862               cp_pedwarn_at ("previous external decl of `%#D'", decl);
863             }
864         }
865
866       /* This name is new in its binding level.
867          Install the new declaration and return it.  */
868       if (namespace_bindings_p ())
869         {
870           /* Install a global value.  */
871
872           /* If the first global decl has external linkage,
873              warn if we later see static one.  */
874           if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
875             TREE_PUBLIC (name) = 1;
876
877           /* Bind the name for the entity.  */
878           if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
879                 && t != NULL_TREE)
880               && (TREE_CODE (x) == TYPE_DECL
881                   || TREE_CODE (x) == VAR_DECL
882                   || TREE_CODE (x) == ALIAS_DECL
883                   || TREE_CODE (x) == NAMESPACE_DECL
884                   || TREE_CODE (x) == CONST_DECL
885                   || TREE_CODE (x) == TEMPLATE_DECL))
886             SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
887
888           /* If new decl is `static' and an `extern' was seen previously,
889              warn about it.  */
890           if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
891             warn_extern_redeclared_static (x, t);
892         }
893       else
894         {
895           /* Here to install a non-global value.  */
896           tree oldlocal = IDENTIFIER_VALUE (name);
897           tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
898
899           if (need_new_binding)
900             {
901               push_local_binding (name, x, 0);
902               /* Because push_local_binding will hook X on to the
903                  current_binding_level's name list, we don't want to
904                  do that again below.  */
905               need_new_binding = 0;
906             }
907
908           /* If this is a TYPE_DECL, push it into the type value slot.  */
909           if (TREE_CODE (x) == TYPE_DECL)
910             set_identifier_type_value (name, x);
911
912           /* Clear out any TYPE_DECL shadowed by a namespace so that
913              we won't think this is a type.  The C struct hack doesn't
914              go through namespaces.  */
915           if (TREE_CODE (x) == NAMESPACE_DECL)
916             set_identifier_type_value (name, NULL_TREE);
917
918           if (oldlocal)
919             {
920               tree d = oldlocal;
921
922               while (oldlocal
923                      && TREE_CODE (oldlocal) == VAR_DECL
924                      && DECL_DEAD_FOR_LOCAL (oldlocal))
925                 oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal);
926
927               if (oldlocal == NULL_TREE)
928                 oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d));
929             }
930
931           /* If this is an extern function declaration, see if we
932              have a global definition or declaration for the function.  */
933           if (oldlocal == NULL_TREE
934               && DECL_EXTERNAL (x)
935               && oldglobal != NULL_TREE
936               && TREE_CODE (x) == FUNCTION_DECL
937               && TREE_CODE (oldglobal) == FUNCTION_DECL)
938             {
939               /* We have one.  Their types must agree.  */
940               if (decls_match (x, oldglobal))
941                 /* OK */;
942               else
943                 {
944                   warning ("extern declaration of `%#D' doesn't match", x);
945                   cp_warning_at ("global declaration `%#D'", oldglobal);
946                 }
947             }
948           /* If we have a local external declaration,
949              and no file-scope declaration has yet been seen,
950              then if we later have a file-scope decl it must not be static.  */
951           if (oldlocal == NULL_TREE
952               && oldglobal == NULL_TREE
953               && DECL_EXTERNAL (x)
954               && TREE_PUBLIC (x))
955             TREE_PUBLIC (name) = 1;
956
957           /* Warn if shadowing an argument at the top level of the body.  */
958           if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
959               /* Inline decls shadow nothing.  */
960               && !DECL_FROM_INLINE (x)
961               && TREE_CODE (oldlocal) == PARM_DECL
962               /* Don't check the `this' parameter.  */
963               && !DECL_ARTIFICIAL (oldlocal))
964             {
965               bool err = false;
966
967               /* Don't complain if it's from an enclosing function.  */
968               if (DECL_CONTEXT (oldlocal) == current_function_decl
969                   && TREE_CODE (x) != PARM_DECL)
970                 {
971                   /* Go to where the parms should be and see if we find
972                      them there.  */
973                   struct cp_binding_level *b = current_binding_level->level_chain;
974
975                   /* Skip the ctor/dtor cleanup level.  */
976                   b = b->level_chain;
977
978                   /* ARM $8.3 */
979                   if (b->kind == sk_function_parms)
980                     {
981                       error ("declaration of '%#D' shadows a parameter", x);
982                       err = true;
983                     }
984                 }
985
986               if (warn_shadow && !err)
987                 {
988                   warning ("declaration of '%#D' shadows a parameter", x);
989                   warning ("%Jshadowed declaration is here", oldlocal);
990                 }
991             }
992
993           /* Maybe warn if shadowing something else.  */
994           else if (warn_shadow && !DECL_EXTERNAL (x)
995               /* No shadow warnings for internally generated vars.  */
996               && ! DECL_ARTIFICIAL (x)
997               /* No shadow warnings for vars made for inlining.  */
998               && ! DECL_FROM_INLINE (x))
999             {
1000               tree member;
1001
1002               if (current_class_ptr)
1003                 member = lookup_member (current_class_type,
1004                                         name,
1005                                         /*protect=*/0,
1006                                         /*want_type=*/false);
1007               else
1008                 member = NULL_TREE;
1009                   
1010               if (member && !TREE_STATIC (member))
1011                 {
1012                   /* Location of previous decl is not useful in this case.  */
1013                   warning ("declaration of '%D' shadows a member of 'this'",
1014                            x);
1015                 }
1016               else if (oldlocal != NULL_TREE
1017                        && TREE_CODE (oldlocal) == VAR_DECL)
1018                 {
1019                   warning ("declaration of '%D' shadows a previous local", x);
1020                   warning ("%Jshadowed declaration is here", oldlocal);
1021                 }
1022               else if (oldglobal != NULL_TREE
1023                        && TREE_CODE (oldglobal) == VAR_DECL)
1024                 /* XXX shadow warnings in outer-more namespaces */
1025                 {
1026                   warning ("declaration of '%D' shadows a global declaration",
1027                            x);
1028                   warning ("%Jshadowed declaration is here", oldglobal);
1029                 }
1030             }
1031         }
1032
1033       if (TREE_CODE (x) == FUNCTION_DECL)
1034         check_default_args (x);
1035
1036       if (TREE_CODE (x) == VAR_DECL)
1037         maybe_register_incomplete_var (x);
1038     }
1039
1040   if (need_new_binding)
1041     add_decl_to_level (x,
1042                        DECL_NAMESPACE_SCOPE_P (x)
1043                        ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x))
1044                        : current_binding_level);
1045
1046   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
1047 }
1048
1049 /* Enter DECL into the symbol table, if that's appropriate.  Returns
1050    DECL, or a modified version thereof.  */
1051
1052 tree
1053 maybe_push_decl (tree decl)
1054 {
1055   tree type = TREE_TYPE (decl);
1056
1057   /* Add this decl to the current binding level, but not if it comes
1058      from another scope, e.g. a static member variable.  TEM may equal
1059      DECL or it may be a previous decl of the same name.  */
1060   if (decl == error_mark_node
1061       || (TREE_CODE (decl) != PARM_DECL
1062           && DECL_CONTEXT (decl) != NULL_TREE
1063           /* Definitions of namespace members outside their namespace are
1064              possible.  */
1065           && TREE_CODE (DECL_CONTEXT (decl)) != NAMESPACE_DECL)
1066       || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
1067       || TREE_CODE (type) == UNKNOWN_TYPE
1068       /* The declaration of a template specialization does not affect
1069          the functions available for overload resolution, so we do not
1070          call pushdecl.  */
1071       || (TREE_CODE (decl) == FUNCTION_DECL
1072           && DECL_TEMPLATE_SPECIALIZATION (decl)))
1073     return decl;
1074   else
1075     return pushdecl (decl);
1076 }
1077
1078 /* Bind DECL to ID in the current_binding_level, assumed to be a local
1079    binding level.  If PUSH_USING is set in FLAGS, we know that DECL
1080    doesn't really belong to this binding level, that it got here
1081    through a using-declaration.  */
1082
1083 void
1084 push_local_binding (tree id, tree decl, int flags)
1085 {
1086   struct cp_binding_level *b;
1087
1088   /* Skip over any local classes.  This makes sense if we call
1089      push_local_binding with a friend decl of a local class.  */
1090   b = innermost_nonclass_level ();
1091
1092   if (lookup_name_current_level (id))
1093     {
1094       /* Supplement the existing binding.  */
1095       if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
1096         /* It didn't work.  Something else must be bound at this
1097            level.  Do not add DECL to the list of things to pop
1098            later.  */
1099         return;
1100     }
1101   else
1102     /* Create a new binding.  */
1103     push_binding (id, decl, b);
1104
1105   if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
1106     /* We must put the OVERLOAD into a TREE_LIST since the
1107        TREE_CHAIN of an OVERLOAD is already used.  Similarly for
1108        decls that got here through a using-declaration.  */
1109     decl = build_tree_list (NULL_TREE, decl);
1110
1111   /* And put DECL on the list of things declared by the current
1112      binding level.  */
1113   add_decl_to_level (decl, b);
1114 }
1115
1116 /* The old ARM scoping rules injected variables declared in the
1117    initialization statement of a for-statement into the surrounding
1118    scope.  We support this usage, in order to be backward-compatible.
1119    DECL is a just-declared VAR_DECL; if necessary inject its
1120    declaration into the surrounding scope.  */
1121
1122 void
1123 maybe_inject_for_scope_var (tree decl)
1124 {
1125   timevar_push (TV_NAME_LOOKUP);
1126   if (!DECL_NAME (decl))
1127     {
1128       timevar_pop (TV_NAME_LOOKUP);
1129       return;
1130     }
1131   
1132   /* Declarations of __FUNCTION__ and its ilk appear magically when
1133      the variable is first used.  If that happens to be inside a
1134      for-loop, we don't want to do anything special.  */
1135   if (DECL_PRETTY_FUNCTION_P (decl))
1136     {
1137       timevar_pop (TV_NAME_LOOKUP);
1138       return;
1139     }
1140
1141   if (current_binding_level->kind == sk_for)
1142     {
1143       struct cp_binding_level *outer
1144         = current_binding_level->level_chain;
1145
1146       /* Check to see if the same name is already bound at the outer
1147          level, either because it was directly declared, or because a
1148          dead for-decl got preserved.  In either case, the code would
1149          not have been valid under the ARM scope rules, so clear
1150          is_for_scope for the current_binding_level.
1151
1152          Otherwise, we need to preserve the temp slot for decl to last
1153          into the outer binding level.  */
1154
1155       cxx_binding *outer_binding
1156         = IDENTIFIER_BINDING (DECL_NAME (decl))->previous;
1157
1158       if (outer_binding && outer_binding->scope == outer
1159           && (TREE_CODE (outer_binding->value) == VAR_DECL)
1160           && DECL_DEAD_FOR_LOCAL (outer_binding->value))
1161         {
1162           outer_binding->value = DECL_SHADOWED_FOR_VAR (outer_binding->value);
1163           current_binding_level->kind = sk_block;
1164         }
1165     }
1166   timevar_pop (TV_NAME_LOOKUP);
1167 }
1168
1169 /* Check to see whether or not DECL is a variable that would have been
1170    in scope under the ARM, but is not in scope under the ANSI/ISO
1171    standard.  If so, issue an error message.  If name lookup would
1172    work in both cases, but return a different result, this function
1173    returns the result of ANSI/ISO lookup.  Otherwise, it returns
1174    DECL.  */
1175
1176 tree
1177 check_for_out_of_scope_variable (tree decl)
1178 {
1179   tree shadowed;
1180
1181   /* We only care about out of scope variables.  */
1182   if (!(TREE_CODE (decl) == VAR_DECL && DECL_DEAD_FOR_LOCAL (decl)))
1183     return decl;
1184
1185   shadowed = DECL_SHADOWED_FOR_VAR (decl);
1186   while (shadowed != NULL_TREE && TREE_CODE (shadowed) == VAR_DECL
1187          && DECL_DEAD_FOR_LOCAL (shadowed))
1188     shadowed = DECL_SHADOWED_FOR_VAR (shadowed);
1189   if (!shadowed)
1190     shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl));
1191   if (shadowed)
1192     {
1193       if (!DECL_ERROR_REPORTED (decl))
1194         {
1195           warning ("name lookup of `%D' changed",
1196                       DECL_NAME (decl));
1197           cp_warning_at ("  matches this `%D' under ISO standard rules",
1198                          shadowed);
1199           cp_warning_at ("  matches this `%D' under old rules", decl);
1200           DECL_ERROR_REPORTED (decl) = 1;
1201         }
1202       return shadowed;
1203     }
1204
1205   /* If we have already complained about this declaration, there's no
1206      need to do it again.  */
1207   if (DECL_ERROR_REPORTED (decl))
1208     return decl;
1209
1210   DECL_ERROR_REPORTED (decl) = 1;
1211
1212   if (TREE_TYPE (decl) == error_mark_node)
1213     return decl;
1214
1215   if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
1216     {
1217       error ("name lookup of `%D' changed for new ISO `for' scoping",
1218              DECL_NAME (decl));
1219       cp_error_at ("  cannot use obsolete binding at `%D' because it has a destructor", decl);
1220       return error_mark_node;
1221     }
1222   else
1223     {
1224       pedwarn ("name lookup of `%D' changed for new ISO `for' scoping",
1225                DECL_NAME (decl));
1226       cp_pedwarn_at ("  using obsolete binding at `%D'", decl);
1227     }
1228
1229   return decl;
1230 }
1231 \f
1232 /* true means unconditionally make a BLOCK for the next level pushed.  */
1233
1234 static bool keep_next_level_flag;
1235
1236 static int binding_depth = 0;
1237 static int is_class_level = 0;
1238
1239 static void
1240 indent (int depth)
1241 {
1242   int i;
1243
1244   for (i = 0; i < depth * 2; i++)
1245     putc (' ', stderr);
1246 }
1247
1248 /* Return a string describing the kind of SCOPE we have.  */
1249 static const char *
1250 cxx_scope_descriptor (cxx_scope *scope)
1251 {
1252   /* The order of this table must match the "scope_kind"
1253      enumerators.  */
1254   static const char* scope_kind_names[] = {
1255     "block-scope",
1256     "cleanup-scope",
1257     "try-scope",
1258     "catch-scope",
1259     "for-scope",
1260     "function-parameter-scope",
1261     "class-scope",
1262     "namespace-scope",
1263     "template-parameter-scope",
1264     "template-explicit-spec-scope"
1265   };
1266   const scope_kind kind = scope->explicit_spec_p
1267     ? sk_template_spec : scope->kind;
1268
1269   return scope_kind_names[kind];
1270 }
1271
1272 /* Output a debugging information about SCOPE when performing
1273    ACTION at LINE.  */
1274 static void
1275 cxx_scope_debug (cxx_scope *scope, int line, const char *action)
1276 {
1277   const char *desc = cxx_scope_descriptor (scope);
1278   if (scope->this_entity)
1279     verbatim ("%s %s(%E) %p %d\n", action, desc,
1280               scope->this_entity, (void *) scope, line);
1281   else
1282     verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
1283 }
1284
1285 /* Return the estimated initial size of the hashtable of a NAMESPACE
1286    scope.  */
1287
1288 static inline size_t
1289 namespace_scope_ht_size (tree ns)
1290 {
1291   tree name = DECL_NAME (ns);
1292
1293   return name == std_identifier
1294     ? NAMESPACE_STD_HT_SIZE
1295     : (name == global_scope_name
1296        ? GLOBAL_SCOPE_HT_SIZE
1297        : NAMESPACE_ORDINARY_HT_SIZE);
1298 }
1299
1300 /* A chain of binding_level structures awaiting reuse.  */
1301
1302 static GTY((deletable)) struct cp_binding_level *free_binding_level;
1303
1304 /* Insert SCOPE as the innermost binding level.  */
1305
1306 void
1307 push_binding_level (struct cp_binding_level *scope)
1308 {
1309   /* Add it to the front of currently active scopes stack.  */
1310   scope->level_chain = current_binding_level;
1311   current_binding_level = scope;
1312   keep_next_level_flag = false;
1313
1314   if (ENABLE_SCOPE_CHECKING)
1315     {
1316       scope->binding_depth = binding_depth;
1317       indent (binding_depth);
1318       cxx_scope_debug (scope, input_line, "push");
1319       is_class_level = 0;
1320       binding_depth++;
1321     }
1322 }
1323
1324 /* Create a new KIND scope and make it the top of the active scopes stack.
1325    ENTITY is the scope of the associated C++ entity (namespace, class,
1326    function); it is NULL otherwise.  */
1327
1328 cxx_scope *
1329 begin_scope (scope_kind kind, tree entity)
1330 {
1331   cxx_scope *scope;
1332   
1333   /* Reuse or create a struct for this binding level.  */
1334   if (!ENABLE_SCOPE_CHECKING && free_binding_level)
1335     {
1336       scope = free_binding_level;
1337       free_binding_level = scope->level_chain;
1338     }
1339   else
1340     scope = ggc_alloc (sizeof (cxx_scope));
1341   memset (scope, 0, sizeof (cxx_scope));
1342
1343   scope->this_entity = entity;
1344   scope->more_cleanups_ok = true;
1345   switch (kind)
1346     {
1347     case sk_cleanup:
1348       scope->keep = true;
1349       break;
1350       
1351     case sk_template_spec:
1352       scope->explicit_spec_p = true;
1353       kind = sk_template_parms;
1354       /* Fall through.  */
1355     case sk_template_parms:
1356     case sk_block:
1357     case sk_try:
1358     case sk_catch:
1359     case sk_for:
1360     case sk_class:
1361     case sk_function_parms:
1362       scope->keep = keep_next_level_flag;
1363       break;
1364
1365     case sk_namespace:
1366       scope->type_decls = binding_table_new (namespace_scope_ht_size (entity));
1367       NAMESPACE_LEVEL (entity) = scope;
1368       VARRAY_TREE_INIT (scope->static_decls,
1369                         DECL_NAME (entity) == std_identifier
1370                         || DECL_NAME (entity) == global_scope_name
1371                         ? 200 : 10,
1372                         "Static declarations");
1373       break;
1374
1375     default:
1376       /* Should not happen.  */
1377       my_friendly_assert (false, 20030922);
1378       break;
1379     }
1380   scope->kind = kind;
1381
1382   push_binding_level (scope);
1383
1384   return scope;
1385 }
1386
1387 /* We're about to leave current scope.  Pop the top of the stack of
1388    currently active scopes.  Return the enclosing scope, now active.  */
1389
1390 cxx_scope *
1391 leave_scope (void)
1392 {
1393   cxx_scope *scope = current_binding_level;
1394
1395   if (scope->kind == sk_namespace && class_binding_level)
1396     current_binding_level = class_binding_level;
1397
1398   /* We cannot leave a scope, if there are none left.  */
1399   if (NAMESPACE_LEVEL (global_namespace))
1400     my_friendly_assert (!global_scope_p (scope), 20030527);
1401   
1402   if (ENABLE_SCOPE_CHECKING)
1403     {
1404       indent (--binding_depth);
1405       cxx_scope_debug (scope, input_line, "leave");
1406       if (is_class_level != (scope == class_binding_level))
1407         {
1408           indent (binding_depth);
1409           verbatim ("XXX is_class_level != (current_scope == class_scope)\n");
1410         }
1411       is_class_level = 0;
1412     }
1413
1414   /* Move one nesting level up.  */
1415   current_binding_level = scope->level_chain;
1416
1417   /* Namespace-scopes are left most probably temporarily, not
1418      completely; they can be reopen later, e.g. in namespace-extension
1419      or any name binding activity that requires us to resume a
1420      namespace.  For classes, we cache some binding levels.  For other
1421      scopes, we just make the structure available for reuse.  */
1422   if (scope->kind != sk_namespace
1423       && scope->kind != sk_class)
1424     {
1425       scope->level_chain = free_binding_level;
1426       if (scope->kind == sk_class)
1427         scope->type_decls = NULL;
1428       else
1429         binding_table_free (scope->type_decls);
1430       my_friendly_assert (!ENABLE_SCOPE_CHECKING
1431                           || scope->binding_depth == binding_depth,
1432                           20030529);
1433       free_binding_level = scope;
1434     }
1435
1436   /* Find the innermost enclosing class scope, and reset
1437      CLASS_BINDING_LEVEL appropriately.  */
1438   for (scope = current_binding_level;
1439        scope && scope->kind != sk_class;
1440        scope = scope->level_chain)
1441     ;
1442   class_binding_level = scope && scope->kind == sk_class ? scope : NULL;
1443
1444   return current_binding_level;
1445 }
1446
1447 static void
1448 resume_scope (struct cp_binding_level* b)
1449 {
1450   /* Resuming binding levels is meant only for namespaces,
1451      and those cannot nest into classes.  */
1452   my_friendly_assert(!class_binding_level, 386);
1453   /* Also, resuming a non-directly nested namespace is a no-no.  */
1454   my_friendly_assert(b->level_chain == current_binding_level, 386);
1455   current_binding_level = b;
1456   if (ENABLE_SCOPE_CHECKING)
1457     {
1458       b->binding_depth = binding_depth;
1459       indent (binding_depth);
1460       cxx_scope_debug (b, input_line, "resume");
1461       is_class_level = 0;
1462       binding_depth++;
1463     }
1464 }
1465
1466 /* Return the innermost binding level that is not for a class scope.  */
1467
1468 static cxx_scope *
1469 innermost_nonclass_level (void)
1470 {
1471   cxx_scope *b;
1472
1473   b = current_binding_level;
1474   while (b->kind == sk_class)
1475     b = b->level_chain;
1476
1477   return b;
1478 }
1479
1480 /* We're defining an object of type TYPE.  If it needs a cleanup, but
1481    we're not allowed to add any more objects with cleanups to the current
1482    scope, create a new binding level.  */
1483
1484 void
1485 maybe_push_cleanup_level (tree type)
1486 {
1487   if (type != error_mark_node
1488       && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
1489       && current_binding_level->more_cleanups_ok == 0)
1490     {
1491       begin_scope (sk_cleanup, NULL);
1492       current_binding_level->statement_list = push_stmt_list ();
1493     }
1494 }
1495
1496 /* Nonzero if we are currently in the global binding level.  */
1497
1498 int
1499 global_bindings_p (void)
1500 {
1501   return global_scope_p (current_binding_level);
1502 }
1503
1504 /* True if we are currently in a toplevel binding level.  This
1505    means either the global binding level or a namespace in a toplevel
1506    binding level.  Since there are no non-toplevel namespace levels,
1507    this really means any namespace or template parameter level.  We
1508    also include a class whose context is toplevel.  */
1509
1510 bool
1511 toplevel_bindings_p (void)
1512 {
1513   struct cp_binding_level *b = innermost_nonclass_level ();
1514
1515   return b->kind == sk_namespace || b->kind == sk_template_parms;
1516 }
1517
1518 /* True if this is a namespace scope, or if we are defining a class
1519    which is itself at namespace scope, or whose enclosing class is
1520    such a class, etc.  */
1521
1522 bool
1523 namespace_bindings_p (void)
1524 {
1525   struct cp_binding_level *b = innermost_nonclass_level ();
1526
1527   return b->kind == sk_namespace;
1528 }
1529
1530 /* True if the current level needs to have a BLOCK made.  */
1531
1532 bool
1533 kept_level_p (void)
1534 {
1535   return (current_binding_level->blocks != NULL_TREE
1536           || current_binding_level->keep
1537           || current_binding_level->kind == sk_cleanup
1538           || current_binding_level->names != NULL_TREE
1539           || current_binding_level->type_decls != NULL);
1540 }
1541
1542 /* Returns the kind of the innermost scope.  */
1543
1544 scope_kind
1545 innermost_scope_kind (void)
1546 {
1547   return current_binding_level->kind;
1548 }
1549
1550 /* Returns true if this scope was created to store template parameters.  */
1551
1552 bool
1553 template_parm_scope_p (void)
1554 {
1555   return innermost_scope_kind () == sk_template_parms;
1556 }
1557
1558 /* If KEEP is true, make a BLOCK node for the next binding level,
1559    unconditionally.  Otherwise, use the normal logic to decide whether
1560    or not to create a BLOCK.  */
1561
1562 void
1563 keep_next_level (bool keep)
1564 {
1565   keep_next_level_flag = keep;
1566 }
1567
1568 /* Return the list of declarations of the current level.
1569    Note that this list is in reverse order unless/until
1570    you nreverse it; and when you do nreverse it, you must
1571    store the result back using `storedecls' or you will lose.  */
1572
1573 tree
1574 getdecls (void)
1575 {
1576   return current_binding_level->names;
1577 }
1578
1579 /* Set the current binding TABLE for type declarations..  This is a
1580    temporary workaround of the fact that the data structure classtypes
1581    does not currently carry its allocated cxx_scope structure.  */
1582 void
1583 cxx_remember_type_decls (binding_table table)
1584 {
1585   current_binding_level->type_decls = table;
1586 }
1587
1588 /* For debugging.  */
1589 static int no_print_functions = 0;
1590 static int no_print_builtins = 0;
1591
1592 /* Called from print_binding_level through binding_table_foreach to
1593    print the content of binding ENTRY.  DATA is a pointer to line offset
1594    marker.  */
1595 static void
1596 bt_print_entry (binding_entry entry, void *data)
1597 {
1598   int *p = (int *) data;
1599   int len;
1600
1601   if (entry->name == NULL)
1602     len = 3;
1603   else if (entry->name == TYPE_IDENTIFIER (entry->type))
1604     len = 2;
1605   else
1606     len = 4;
1607     len = 4;
1608
1609   *p += len;
1610
1611   if (*p > 5)
1612     {
1613       fprintf (stderr, "\n\t");
1614       *p = len;
1615     }
1616   if (entry->name == NULL)
1617     {
1618       print_node_brief (stderr, "<unnamed-typedef", entry->type, 0);
1619       fprintf (stderr, ">");
1620     }
1621   else if (entry->name == TYPE_IDENTIFIER (entry->type))
1622     print_node_brief (stderr, "", entry->type, 0);
1623   else
1624     {
1625       print_node_brief (stderr, "<typedef", entry->name, 0);
1626       print_node_brief (stderr, "", entry->type, 0);
1627       fprintf (stderr, ">");
1628     }
1629 }
1630
1631 void
1632 print_binding_level (struct cp_binding_level* lvl)
1633 {
1634   tree t;
1635   int i = 0, len;
1636   fprintf (stderr, " blocks=" HOST_PTR_PRINTF, (void *) lvl->blocks);
1637   if (lvl->more_cleanups_ok)
1638     fprintf (stderr, " more-cleanups-ok");
1639   if (lvl->have_cleanups)
1640     fprintf (stderr, " have-cleanups");
1641   fprintf (stderr, "\n");
1642   if (lvl->names)
1643     {
1644       fprintf (stderr, " names:\t");
1645       /* We can probably fit 3 names to a line?  */
1646       for (t = lvl->names; t; t = TREE_CHAIN (t))
1647         {
1648           if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
1649             continue;
1650           if (no_print_builtins
1651               && (TREE_CODE (t) == TYPE_DECL)
1652               && DECL_IS_BUILTIN (t))
1653             continue;
1654
1655           /* Function decls tend to have longer names.  */
1656           if (TREE_CODE (t) == FUNCTION_DECL)
1657             len = 3;
1658           else
1659             len = 2;
1660           i += len;
1661           if (i > 6)
1662             {
1663               fprintf (stderr, "\n\t");
1664               i = len;
1665             }
1666           print_node_brief (stderr, "", t, 0);
1667           if (t == error_mark_node)
1668             break;
1669         }
1670       if (i)
1671         fprintf (stderr, "\n");
1672     }
1673   if (lvl->type_decls)
1674     {
1675       fprintf (stderr, " tags:\t");
1676       i = 0;
1677       binding_table_foreach (lvl->type_decls, bt_print_entry, &i);
1678       if (i)
1679         fprintf (stderr, "\n");
1680     }
1681   if (VEC_length (cp_class_binding, lvl->class_shadowed))
1682     {
1683       size_t i;
1684       cp_class_binding *b;
1685       fprintf (stderr, " class-shadowed:");
1686       for (i = 0; 
1687            (b = VEC_iterate(cp_class_binding, 
1688                             lvl->class_shadowed,
1689                             i));
1690            ++i) 
1691         fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
1692       fprintf (stderr, "\n");
1693     }
1694   if (lvl->type_shadowed)
1695     {
1696       fprintf (stderr, " type-shadowed:");
1697       for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
1698         {
1699           fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1700         }
1701       fprintf (stderr, "\n");
1702     }
1703 }
1704
1705 void
1706 print_other_binding_stack (struct cp_binding_level *stack)
1707 {
1708   struct cp_binding_level *level;
1709   for (level = stack; !global_scope_p (level); level = level->level_chain)
1710     {
1711       fprintf (stderr, "binding level " HOST_PTR_PRINTF "\n", (void *) level);
1712       print_binding_level (level);
1713     }
1714 }
1715
1716 void
1717 print_binding_stack (void)
1718 {
1719   struct cp_binding_level *b;
1720   fprintf (stderr, "current_binding_level=" HOST_PTR_PRINTF
1721            "\nclass_binding_level=" HOST_PTR_PRINTF
1722            "\nNAMESPACE_LEVEL (global_namespace)=" HOST_PTR_PRINTF "\n",
1723            (void *) current_binding_level, (void *) class_binding_level,
1724            (void *) NAMESPACE_LEVEL (global_namespace));
1725   if (class_binding_level)
1726     {
1727       for (b = class_binding_level; b; b = b->level_chain)
1728         if (b == current_binding_level)
1729           break;
1730       if (b)
1731         b = class_binding_level;
1732       else
1733         b = current_binding_level;
1734     }
1735   else
1736     b = current_binding_level;
1737   print_other_binding_stack (b);
1738   fprintf (stderr, "global:\n");
1739   print_binding_level (NAMESPACE_LEVEL (global_namespace));
1740 }
1741 \f
1742 /* Return the type associated with id.  */
1743
1744 tree
1745 identifier_type_value (tree id)
1746 {
1747   timevar_push (TV_NAME_LOOKUP);
1748   /* There is no type with that name, anywhere.  */
1749   if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
1750     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
1751   /* This is not the type marker, but the real thing.  */
1752   if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
1753     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, REAL_IDENTIFIER_TYPE_VALUE (id));
1754   /* Have to search for it. It must be on the global level, now.
1755      Ask lookup_name not to return non-types.  */
1756   id = lookup_name_real (id, 2, 1, /*block_p=*/true, 0, LOOKUP_COMPLAIN);
1757   if (id)
1758     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_TYPE (id));
1759   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
1760 }
1761
1762 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
1763    the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++.  */
1764
1765 tree
1766 identifier_global_value (tree t)
1767 {
1768   return IDENTIFIER_GLOBAL_VALUE (t);
1769 }
1770
1771 /* Push a definition of struct, union or enum tag named ID.  into
1772    binding_level B.  DECL is a TYPE_DECL for the type.  We assume that
1773    the tag ID is not already defined.  */
1774
1775 static void
1776 set_identifier_type_value_with_scope (tree id, tree decl, cxx_scope *b)
1777 {
1778   tree type;
1779
1780   if (b->kind != sk_namespace)
1781     {
1782       /* Shadow the marker, not the real thing, so that the marker
1783          gets restored later.  */
1784       tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
1785       b->type_shadowed
1786         = tree_cons (id, old_type_value, b->type_shadowed);
1787       type = decl ? TREE_TYPE (decl) : NULL_TREE;
1788       TREE_TYPE (b->type_shadowed) = type;
1789     }
1790   else
1791     {
1792       cxx_binding *binding =
1793         binding_for_name (NAMESPACE_LEVEL (current_namespace), id);
1794       if (decl)
1795         {
1796           if (binding->value)
1797             supplement_binding (binding, decl);
1798           else
1799             binding->value = decl;
1800         }
1801       else
1802         abort ();
1803       /* Store marker instead of real type.  */
1804       type = global_type_node;
1805     }
1806   SET_IDENTIFIER_TYPE_VALUE (id, type);
1807 }
1808
1809 /* As set_identifier_type_value_with_scope, but using
1810    current_binding_level.  */
1811
1812 void
1813 set_identifier_type_value (tree id, tree decl)
1814 {
1815   set_identifier_type_value_with_scope (id, decl, current_binding_level);
1816 }
1817
1818 /* Return the name for the constructor (or destructor) for the
1819    specified class TYPE.  When given a template, this routine doesn't
1820    lose the specialization.  */
1821
1822 tree
1823 constructor_name_full (tree type)
1824 {
1825   type = TYPE_MAIN_VARIANT (type);
1826   if (CLASS_TYPE_P (type) && TYPE_WAS_ANONYMOUS (type) 
1827       && TYPE_HAS_CONSTRUCTOR (type))
1828     return DECL_NAME (OVL_CURRENT (CLASSTYPE_CONSTRUCTORS (type)));
1829   else
1830     return TYPE_IDENTIFIER (type);
1831 }
1832
1833 /* Return the name for the constructor (or destructor) for the
1834    specified class.  When given a template, return the plain
1835    unspecialized name.  */
1836
1837 tree
1838 constructor_name (tree type)
1839 {
1840   tree name;
1841   name = constructor_name_full (type);
1842   if (IDENTIFIER_TEMPLATE (name))
1843     name = IDENTIFIER_TEMPLATE (name);
1844   return name;
1845 }
1846
1847 /* Returns TRUE if NAME is the name for the constructor for TYPE.  */
1848
1849 bool
1850 constructor_name_p (tree name, tree type)
1851 {
1852   tree ctor_name;
1853
1854   if (!name)
1855     return false;
1856   
1857   if (TREE_CODE (name) != IDENTIFIER_NODE)
1858     return false;
1859   
1860   ctor_name = constructor_name_full (type);
1861   if (name == ctor_name)
1862     return true;
1863   if (IDENTIFIER_TEMPLATE (ctor_name)
1864       && name == IDENTIFIER_TEMPLATE (ctor_name))
1865     return true;
1866   return false;
1867 }
1868
1869 /* Counter used to create anonymous type names.  */
1870
1871 static GTY(()) int anon_cnt;
1872
1873 /* Return an IDENTIFIER which can be used as a name for
1874    anonymous structs and unions.  */
1875
1876 tree
1877 make_anon_name (void)
1878 {
1879   char buf[32];
1880
1881   sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
1882   return get_identifier (buf);
1883 }
1884
1885 /* Clear the TREE_PURPOSE slot of UTDs which have anonymous typenames.
1886    This keeps dbxout from getting confused.  */
1887
1888 void
1889 clear_anon_tags (void)
1890 {
1891   struct cp_binding_level *b;
1892   static int last_cnt = 0;
1893
1894   /* Fast out if no new anon names were declared.  */
1895   if (last_cnt == anon_cnt)
1896     return;
1897
1898   b = current_binding_level;
1899   while (b->kind == sk_cleanup)
1900     b = b->level_chain;
1901   if (b->type_decls != NULL)
1902     binding_table_remove_anonymous_types (b->type_decls);
1903   last_cnt = anon_cnt;
1904 }
1905 \f
1906 /* Return (from the stack of) the BINDING, if any, established at SCOPE.  */ 
1907
1908 static inline cxx_binding *
1909 find_binding (cxx_scope *scope, cxx_binding *binding)
1910 {
1911   timevar_push (TV_NAME_LOOKUP);
1912
1913   for (; binding != NULL; binding = binding->previous)
1914     if (binding->scope == scope)
1915       POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, binding);
1916
1917   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, (cxx_binding *)0);
1918 }
1919
1920 /* Return the binding for NAME in SCOPE, if any.  Otherwise, return NULL.  */
1921
1922 static inline cxx_binding *
1923 cxx_scope_find_binding_for_name (cxx_scope *scope, tree name)
1924 {
1925   cxx_binding *b = IDENTIFIER_NAMESPACE_BINDINGS (name);
1926   if (b)
1927     {
1928       /* Fold-in case where NAME is used only once.  */
1929       if (scope == b->scope && b->previous == NULL)
1930         return b;
1931       return find_binding (scope, b);
1932     }
1933   return NULL;
1934 }
1935
1936 /* Always returns a binding for name in scope.  If no binding is
1937    found, make a new one.  */
1938
1939 static cxx_binding *
1940 binding_for_name (cxx_scope *scope, tree name)
1941 {
1942   cxx_binding *result;
1943
1944   result = cxx_scope_find_binding_for_name (scope, name);
1945   if (result)
1946     return result;
1947   /* Not found, make a new one.  */
1948   result = cxx_binding_make (NULL, NULL);
1949   result->previous = IDENTIFIER_NAMESPACE_BINDINGS (name);
1950   result->scope = scope;
1951   result->is_local = false;
1952   result->value_is_inherited = false;
1953   IDENTIFIER_NAMESPACE_BINDINGS (name) = result;
1954   return result;
1955 }
1956
1957 /* Insert another USING_DECL into the current binding level, returning
1958    this declaration. If this is a redeclaration, do nothing, and
1959    return NULL_TREE if this not in namespace scope (in namespace
1960    scope, a using decl might extend any previous bindings).  */
1961
1962 tree
1963 push_using_decl (tree scope, tree name)
1964 {
1965   tree decl;
1966
1967   timevar_push (TV_NAME_LOOKUP);
1968   my_friendly_assert (TREE_CODE (scope) == NAMESPACE_DECL, 383);
1969   my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 384);
1970   for (decl = current_binding_level->usings; decl; decl = TREE_CHAIN (decl))
1971     if (DECL_INITIAL (decl) == scope && DECL_NAME (decl) == name)
1972       break;
1973   if (decl)
1974     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
1975                             namespace_bindings_p () ? decl : NULL_TREE);
1976   decl = build_lang_decl (USING_DECL, name, void_type_node);
1977   DECL_INITIAL (decl) = scope;
1978   TREE_CHAIN (decl) = current_binding_level->usings;
1979   current_binding_level->usings = decl;
1980   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
1981 }
1982
1983 /* Same as pushdecl, but define X in binding-level LEVEL.  We rely on the
1984    caller to set DECL_CONTEXT properly.  */
1985
1986 tree
1987 pushdecl_with_scope (tree x, cxx_scope *level)
1988 {
1989   struct cp_binding_level *b;
1990   tree function_decl = current_function_decl;
1991
1992   timevar_push (TV_NAME_LOOKUP);
1993   current_function_decl = NULL_TREE;
1994   if (level->kind == sk_class)
1995     {
1996       b = class_binding_level;
1997       class_binding_level = level;
1998       pushdecl_class_level (x);
1999       class_binding_level = b;
2000     }
2001   else
2002     {
2003       b = current_binding_level;
2004       current_binding_level = level;
2005       x = pushdecl (x);
2006       current_binding_level = b;
2007     }
2008   current_function_decl = function_decl;
2009   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
2010 }
2011
2012 /* DECL is a FUNCTION_DECL for a non-member function, which may have
2013    other definitions already in place.  We get around this by making
2014    the value of the identifier point to a list of all the things that
2015    want to be referenced by that name.  It is then up to the users of
2016    that name to decide what to do with that list.
2017
2018    DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its
2019    DECL_TEMPLATE_RESULT.  It is dealt with the same way.
2020
2021    FLAGS is a bitwise-or of the following values:
2022      PUSH_LOCAL: Bind DECL in the current scope, rather than at
2023                  namespace scope.
2024      PUSH_USING: DECL is being pushed as the result of a using
2025                  declaration.
2026
2027    The value returned may be a previous declaration if we guessed wrong
2028    about what language DECL should belong to (C or C++).  Otherwise,
2029    it's always DECL (and never something that's not a _DECL).  */
2030
2031 static tree
2032 push_overloaded_decl (tree decl, int flags)
2033 {
2034   tree name = DECL_NAME (decl);
2035   tree old;
2036   tree new_binding;
2037   int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
2038
2039   timevar_push (TV_NAME_LOOKUP);
2040   if (doing_global)
2041     old = namespace_binding (name, DECL_CONTEXT (decl));
2042   else
2043     old = lookup_name_current_level (name);
2044
2045   if (old)
2046     {
2047       if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
2048         {
2049           tree t = TREE_TYPE (old);
2050           if (IS_AGGR_TYPE (t) && warn_shadow
2051               && (! DECL_IN_SYSTEM_HEADER (decl)
2052                   || ! DECL_IN_SYSTEM_HEADER (old)))
2053             warning ("`%#D' hides constructor for `%#T'", decl, t);
2054           old = NULL_TREE;
2055         }
2056       else if (is_overloaded_fn (old))
2057         {
2058           tree tmp;
2059
2060           for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
2061             {
2062               tree fn = OVL_CURRENT (tmp);
2063
2064               if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp)
2065                   && !(flags & PUSH_USING)
2066                   && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2067                                 TYPE_ARG_TYPES (TREE_TYPE (decl))))
2068                 error ("`%#D' conflicts with previous using declaration `%#D'",
2069                           decl, fn);
2070
2071               if (duplicate_decls (decl, fn) == fn)
2072                 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, fn);
2073             }
2074         }
2075       else if (old == error_mark_node)
2076         /* Ignore the undefined symbol marker.  */
2077         old = NULL_TREE;
2078       else
2079         {
2080           cp_error_at ("previous non-function declaration `%#D'", old);
2081           error ("conflicts with function declaration `%#D'", decl);
2082           POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2083         }
2084     }
2085
2086   if (old || TREE_CODE (decl) == TEMPLATE_DECL
2087       /* If it's a using declaration, we always need to build an OVERLOAD,
2088          because it's the only way to remember that the declaration comes
2089          from 'using', and have the lookup behave correctly.  */
2090       || (flags & PUSH_USING))
2091     {
2092       if (old && TREE_CODE (old) != OVERLOAD)
2093         new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE));
2094       else
2095         new_binding = ovl_cons (decl, old);
2096       if (flags & PUSH_USING)
2097         OVL_USED (new_binding) = 1;
2098     }
2099   else
2100     /* NAME is not ambiguous.  */
2101     new_binding = decl;
2102
2103   if (doing_global)
2104     set_namespace_binding (name, current_namespace, new_binding);
2105   else
2106     {
2107       /* We only create an OVERLOAD if there was a previous binding at
2108          this level, or if decl is a template. In the former case, we
2109          need to remove the old binding and replace it with the new
2110          binding.  We must also run through the NAMES on the binding
2111          level where the name was bound to update the chain.  */
2112
2113       if (TREE_CODE (new_binding) == OVERLOAD && old)
2114         {
2115           tree *d;
2116
2117           for (d = &IDENTIFIER_BINDING (name)->scope->names;
2118                *d;
2119                d = &TREE_CHAIN (*d))
2120             if (*d == old
2121                 || (TREE_CODE (*d) == TREE_LIST
2122                     && TREE_VALUE (*d) == old))
2123               {
2124                 if (TREE_CODE (*d) == TREE_LIST)
2125                   /* Just replace the old binding with the new.  */
2126                   TREE_VALUE (*d) = new_binding;
2127                 else
2128                   /* Build a TREE_LIST to wrap the OVERLOAD.  */
2129                   *d = tree_cons (NULL_TREE, new_binding,
2130                                   TREE_CHAIN (*d));
2131
2132                 /* And update the cxx_binding node.  */
2133                 IDENTIFIER_BINDING (name)->value = new_binding;
2134                 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2135               }
2136
2137           /* We should always find a previous binding in this case.  */
2138           abort ();
2139         }
2140
2141       /* Install the new binding.  */
2142       push_local_binding (name, new_binding, flags);
2143     }
2144
2145   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2146 }
2147
2148 /* Check a non-member using-declaration. Return the name and scope
2149    being used, and the USING_DECL, or NULL_TREE on failure.  */
2150
2151 static tree
2152 validate_nonmember_using_decl (tree decl, tree scope, tree name)
2153 {
2154   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR)
2155     {
2156       /* 7.3.3/5
2157            A using-declaration shall not name a template-id.  */
2158       error ("a using-declaration cannot specify a template-id.  Try `using %D'", name);
2159       return NULL_TREE;
2160     }
2161
2162   if (TREE_CODE (decl) == NAMESPACE_DECL)
2163     {
2164       error ("namespace `%D' not allowed in using-declaration", decl);
2165       return NULL_TREE;
2166     }
2167
2168   if (TREE_CODE (decl) == SCOPE_REF)
2169     {
2170       /* It's a nested name with template parameter dependent scope.
2171          This can only be using-declaration for class member.  */
2172       error ("`%T' is not a namespace", TREE_OPERAND (decl, 0));
2173       return NULL_TREE;
2174     }
2175
2176   if (is_overloaded_fn (decl))
2177     decl = get_first_fn (decl);
2178
2179   my_friendly_assert (DECL_P (decl), 20020908);
2180
2181   /* [namespace.udecl]
2182        A using-declaration for a class member shall be a
2183        member-declaration.  */
2184   if (TYPE_P (scope))
2185     {
2186       error ("`%T' is not a namespace", scope);
2187       return NULL_TREE;
2188     }
2189
2190   /* Make a USING_DECL.  */
2191   return push_using_decl (scope, name);
2192 }
2193
2194 /* Process local and global using-declarations.  */
2195
2196 static void
2197 do_nonmember_using_decl (tree scope, tree name, tree oldval, tree oldtype,
2198                          tree *newval, tree *newtype)
2199 {
2200   struct scope_binding decls = EMPTY_SCOPE_BINDING;
2201
2202   *newval = *newtype = NULL_TREE;
2203   if (!qualified_lookup_using_namespace (name, scope, &decls, 0))
2204     /* Lookup error */
2205     return;
2206
2207   if (!decls.value && !decls.type)
2208     {
2209       error ("`%D' not declared", name);
2210       return;
2211     }
2212
2213   /* Check for using functions.  */
2214   if (decls.value && is_overloaded_fn (decls.value))
2215     {
2216       tree tmp, tmp1;
2217
2218       if (oldval && !is_overloaded_fn (oldval))
2219         {
2220           if (!DECL_IMPLICIT_TYPEDEF_P (oldval))
2221             error ("`%D' is already declared in this scope", name);
2222           oldval = NULL_TREE;
2223         }
2224
2225       *newval = oldval;
2226       for (tmp = decls.value; tmp; tmp = OVL_NEXT (tmp))
2227         {
2228           tree new_fn = OVL_CURRENT (tmp);
2229
2230           /* [namespace.udecl]
2231
2232              If a function declaration in namespace scope or block
2233              scope has the same name and the same parameter types as a
2234              function introduced by a using declaration the program is
2235              ill-formed.  */
2236           for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1))
2237             {
2238               tree old_fn = OVL_CURRENT (tmp1);
2239
2240               if (new_fn == old_fn)
2241                 /* The function already exists in the current namespace.  */
2242                 break;
2243               else if (OVL_USED (tmp1))
2244                 continue; /* this is a using decl */
2245               else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (new_fn)),
2246                                   TYPE_ARG_TYPES (TREE_TYPE (old_fn))))
2247                 {
2248                   /* There was already a non-using declaration in
2249                      this scope with the same parameter types. If both
2250                      are the same extern "C" functions, that's ok.  */
2251                   if (decls_match (new_fn, old_fn))
2252                     {
2253                       /* If the OLD_FN was a builtin, there is now a
2254                          real declaration.  */
2255                       if (DECL_ANTICIPATED (old_fn))
2256                         DECL_ANTICIPATED (old_fn) = 0;
2257                       break;
2258                     }
2259                   else if (!DECL_ANTICIPATED (old_fn))
2260                     {
2261                       /* If the OLD_FN was really declared, the
2262                          declarations don't match.  */
2263                       error ("`%D' is already declared in this scope", name);
2264                       break;
2265                     }
2266
2267                   /* If the OLD_FN was not really there, just ignore
2268                      it and keep going.  */
2269                 }
2270             }
2271
2272           /* If we broke out of the loop, there's no reason to add
2273              this function to the using declarations for this
2274              scope.  */
2275           if (tmp1)
2276             continue;
2277             
2278           /* If we are adding to an existing OVERLOAD, then we no
2279              longer know the type of the set of functions.  */
2280           if (*newval && TREE_CODE (*newval) == OVERLOAD)
2281             TREE_TYPE (*newval) = unknown_type_node;
2282           /* Add this new function to the set.  */
2283           *newval = build_overload (OVL_CURRENT (tmp), *newval);
2284           /* If there is only one function, then we use its type.  (A
2285              using-declaration naming a single function can be used in
2286              contexts where overload resolution cannot be
2287              performed.)  */
2288           if (TREE_CODE (*newval) != OVERLOAD)
2289             {
2290               *newval = ovl_cons (*newval, NULL_TREE);
2291               TREE_TYPE (*newval) = TREE_TYPE (OVL_CURRENT (tmp));
2292             }
2293           OVL_USED (*newval) = 1;
2294         }
2295     }
2296   else 
2297     {
2298       *newval = decls.value;
2299       if (oldval && !decls_match (*newval, oldval))
2300         error ("`%D' is already declared in this scope", name);
2301     }
2302
2303   *newtype = decls.type;
2304   if (oldtype && *newtype && !same_type_p (oldtype, *newtype))
2305     {
2306       error ("using declaration `%D' introduced ambiguous type `%T'",
2307                 name, oldtype);
2308       return;
2309     }
2310 }
2311
2312 /* Process a using-declaration at function scope.  */
2313
2314 void
2315 do_local_using_decl (tree decl, tree scope, tree name)
2316 {
2317   tree oldval, oldtype, newval, newtype;
2318   tree orig_decl = decl;
2319
2320   decl = validate_nonmember_using_decl (decl, scope, name);
2321   if (decl == NULL_TREE)
2322     return;
2323
2324   if (building_stmt_tree ()
2325       && at_function_scope_p ())
2326     add_decl_expr (decl);
2327
2328   oldval = lookup_name_current_level (name);
2329   oldtype = lookup_type_current_level (name);
2330
2331   do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
2332
2333   if (newval)
2334     {
2335       if (is_overloaded_fn (newval))
2336         {
2337           tree fn, term;
2338
2339           /* We only need to push declarations for those functions
2340              that were not already bound in the current level.
2341              The old value might be NULL_TREE, it might be a single
2342              function, or an OVERLOAD.  */
2343           if (oldval && TREE_CODE (oldval) == OVERLOAD)
2344             term = OVL_FUNCTION (oldval);
2345           else
2346             term = oldval;
2347           for (fn = newval; fn && OVL_CURRENT (fn) != term; 
2348                fn = OVL_NEXT (fn))
2349             push_overloaded_decl (OVL_CURRENT (fn), 
2350                                   PUSH_LOCAL | PUSH_USING);
2351         }
2352       else
2353         push_local_binding (name, newval, PUSH_USING);
2354     }
2355   if (newtype)
2356     {
2357       push_local_binding (name, newtype, PUSH_USING);
2358       set_identifier_type_value (name, newtype);
2359     }
2360
2361   /* Emit debug info.  */
2362   if (!processing_template_decl)
2363     cp_emit_debug_info_for_using (orig_decl, current_scope());
2364 }
2365
2366 /* Return the type that should be used when TYPE's name is preceded
2367    by a tag such as 'struct' or 'union', or null if the name cannot
2368    be used in this way.
2369
2370    For example, when processing the third line of:
2371
2372         struct A;
2373         typedef struct A A;
2374         struct A;
2375
2376    lookup of A will find the typedef.  Given A's typedef, this function
2377    will return the type associated with "struct A".  For the tag to be
2378    anything other than TYPE, TYPE must be a typedef whose original type
2379    has the same name and context as TYPE itself.
2380
2381    It is not valid for a typedef of an anonymous type to be used with
2382    an explicit tag:
2383
2384        typedef struct { ... } B;
2385        struct B;
2386
2387    Return null for this case.  */
2388
2389 static tree
2390 follow_tag_typedef (tree type)
2391 {
2392   tree original;
2393
2394   original = original_type (type);
2395   if (! TYPE_NAME (original))
2396     return NULL_TREE;
2397   if (TYPE_IDENTIFIER (original) == TYPE_IDENTIFIER (type)
2398       && (CP_DECL_CONTEXT (TYPE_NAME (original))
2399           == CP_DECL_CONTEXT (TYPE_NAME (type)))
2400       && !(CLASS_TYPE_P (original) && TYPE_WAS_ANONYMOUS (original)))
2401     return original;
2402   else
2403     return NULL_TREE;
2404 }
2405
2406 /* Given NAME, an IDENTIFIER_NODE,
2407    return the structure (or union or enum) definition for that name.
2408    Searches binding levels from its SCOPE up to the global level.
2409    If THISLEVEL_ONLY is nonzero, searches only the specified context
2410    (but skips any sk_cleanup contexts to find one that is
2411    meaningful for tags).
2412    FORM says which kind of type the caller wants;
2413    it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2414    If the wrong kind of type is found, and it's not a template, an error is
2415    reported.  */
2416
2417 tree
2418 lookup_tag (enum tree_code form, tree name,
2419             cxx_scope *binding_level, int thislevel_only)
2420 {
2421   struct cp_binding_level *level;
2422   /* Nonzero if, we should look past a template parameter level, even
2423      if THISLEVEL_ONLY.  */
2424   int allow_template_parms_p = 1;
2425   bool type_is_anonymous = ANON_AGGRNAME_P (name);
2426
2427   timevar_push (TV_NAME_LOOKUP);
2428   for (level = binding_level; level; level = level->level_chain)
2429     {
2430       tree tail;
2431       if (type_is_anonymous && level->type_decls != NULL)
2432         {
2433           tree type = binding_table_find_anon_type (level->type_decls, name);
2434           /* There is no need for error checking here, because
2435            anon names are unique throughout the compilation.  */
2436           if (type != NULL)
2437             POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, type);
2438         }
2439       else if (level->kind == sk_namespace)
2440         /* Do namespace lookup.  */
2441         for (tail = current_namespace; 1; tail = CP_DECL_CONTEXT (tail))
2442           {
2443             cxx_binding *binding =
2444               cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (tail), name);
2445
2446             if (binding && (binding->type
2447                             || (binding->value 
2448                                 && DECL_DECLARES_TYPE_P (binding->value))))
2449               {
2450                 tree old;
2451                 
2452                 /* If we just skipped past a template parameter level,
2453                    even though THISLEVEL_ONLY, and we find a template
2454                    class declaration, then we use the _TYPE node for the
2455                    template.  See the example below.  */
2456                 if (thislevel_only && !allow_template_parms_p
2457                     && binding->value
2458                     && DECL_CLASS_TEMPLATE_P (binding->value))
2459                   old = binding->value;
2460                 else
2461                   old = binding->type ? binding->type : binding->value;
2462
2463                 /* We've found something at this binding level.  If it is
2464                    a typedef, extract the tag it refers to.  Lookup fails
2465                    if the typedef doesn't refer to a taggable type.  */
2466                 old = TREE_TYPE (old);
2467                 old = follow_tag_typedef (old);
2468                 if (!old)
2469                   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2470                 if (TREE_CODE (old) != form
2471                     && (form == ENUMERAL_TYPE
2472                         || TREE_CODE (old) == ENUMERAL_TYPE))
2473                   {
2474                     error ("`%#D' redeclared as %C", old, form);
2475                     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2476                   }
2477                 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, old);
2478               }
2479             if (thislevel_only || tail == global_namespace)
2480               POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2481           }
2482       else if (level->type_decls != NULL)
2483         {
2484           binding_entry entry = binding_table_find (level->type_decls, name);
2485           if (entry != NULL)
2486             {
2487               enum tree_code code = TREE_CODE (entry->type);
2488                 
2489               if (code != form
2490                   && (form == ENUMERAL_TYPE || code == ENUMERAL_TYPE))
2491                 {
2492                   /* Definition isn't the kind we were looking for.  */
2493                   error ("`%#D' redeclared as %C", entry->type, form);
2494                   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2495                 }
2496               POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, entry->type);
2497             }
2498           }
2499       if (thislevel_only && level->kind != sk_cleanup)
2500         {
2501           if (level->kind == sk_template_parms && allow_template_parms_p)
2502             {
2503               /* We must deal with cases like this:
2504
2505                    template <class T> struct S;
2506                    template <class T> struct S {};
2507
2508                  When looking up `S', for the second declaration, we
2509                  would like to find the first declaration.  But, we
2510                  are in the pseudo-global level created for the
2511                  template parameters, rather than the (surrounding)
2512                  namespace level.  Thus, we keep going one more level,
2513                  even though THISLEVEL_ONLY is nonzero.  */
2514               allow_template_parms_p = 0;
2515               continue;
2516             }
2517           else
2518             POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2519         }
2520     }
2521   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2522 }
2523
2524 /* Given a type, find the tag that was defined for it and return the tag name.
2525    Otherwise return 0.  However, the value can never be 0
2526    in the cases in which this is used.
2527
2528    C++: If NAME is nonzero, this is the new name to install.  This is
2529    done when replacing anonymous tags with real tag names.  */
2530
2531 tree
2532 lookup_tag_reverse (tree type, tree name)
2533 {
2534   struct cp_binding_level *level;
2535
2536   timevar_push (TV_NAME_LOOKUP);
2537   for (level = current_binding_level; level; level = level->level_chain)
2538     {
2539       binding_entry entry = level->type_decls == NULL
2540         ? NULL
2541         : binding_table_reverse_maybe_remap (level->type_decls, type, name);
2542       if (entry)
2543         POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, entry->name);
2544     }
2545   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2546 }
2547
2548 /* Returns true if ROOT (a namespace, class, or function) encloses
2549    CHILD.  CHILD may be either a class type or a namespace.  */
2550
2551 bool
2552 is_ancestor (tree root, tree child)
2553 {
2554   my_friendly_assert ((TREE_CODE (root) == NAMESPACE_DECL
2555                        || TREE_CODE (root) == FUNCTION_DECL
2556                        || CLASS_TYPE_P (root)), 20030307);
2557   my_friendly_assert ((TREE_CODE (child) == NAMESPACE_DECL
2558                        || CLASS_TYPE_P (child)),
2559                       20030307);
2560   
2561   /* The global namespace encloses everything.  */
2562   if (root == global_namespace)
2563     return true;
2564
2565   while (true)
2566     {
2567       /* If we've run out of scopes, stop.  */
2568       if (!child)
2569         return false;
2570       /* If we've reached the ROOT, it encloses CHILD.  */
2571       if (root == child)
2572         return true;
2573       /* Go out one level.  */
2574       if (TYPE_P (child))
2575         child = TYPE_NAME (child);
2576       child = DECL_CONTEXT (child);
2577     }
2578 }
2579
2580 /* Enter the class or namespace scope indicated by T.  Returns TRUE iff
2581    pop_scope should be called later to exit this scope.  */
2582
2583 bool
2584 push_scope (tree t)
2585 {
2586   bool pop = true;
2587
2588   if (TREE_CODE (t) == NAMESPACE_DECL)
2589     push_decl_namespace (t);
2590   else if (CLASS_TYPE_P (t))
2591     {
2592       if (!at_class_scope_p ()
2593           || !same_type_p (current_class_type, t))
2594         push_nested_class (t);
2595       else
2596         /* T is the same as the current scope.  There is therefore no
2597            need to re-enter the scope.  Since we are not actually
2598            pushing a new scope, our caller should not call
2599            pop_scope.  */
2600         pop = false;
2601     }
2602
2603   return pop;
2604 }
2605
2606 /* Leave scope pushed by push_scope.  */
2607
2608 void
2609 pop_scope (tree t)
2610 {
2611   if (TREE_CODE (t) == NAMESPACE_DECL)
2612     pop_decl_namespace ();
2613   else if CLASS_TYPE_P (t)
2614     pop_nested_class ();
2615 }
2616 \f
2617 /* Do a pushlevel for class declarations.  */
2618
2619 void
2620 pushlevel_class (void)
2621 {
2622   if (ENABLE_SCOPE_CHECKING)
2623     is_class_level = 1;
2624
2625   class_binding_level = begin_scope (sk_class, current_class_type);
2626 }
2627
2628 /* ...and a poplevel for class declarations.  */
2629
2630 void
2631 poplevel_class (void)
2632 {
2633   struct cp_binding_level *level = class_binding_level;
2634   cp_class_binding *cb;
2635   size_t i;
2636   tree shadowed;
2637
2638   timevar_push (TV_NAME_LOOKUP);
2639   my_friendly_assert (level != 0, 354);
2640
2641   /* If we're leaving a toplevel class, cache its binding level.  */
2642   if (current_class_depth == 1)
2643     previous_class_level = level;
2644   for (shadowed = level->type_shadowed;
2645        shadowed;
2646        shadowed = TREE_CHAIN (shadowed))
2647     SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
2648
2649   /* Remove the bindings for all of the class-level declarations.  */
2650   for (i = 0;
2651        (cb = VEC_iterate (cp_class_binding, level->class_shadowed, i));
2652        ++i)
2653     IDENTIFIER_BINDING (cb->identifier) = cb->base.previous;
2654
2655   /* Now, pop out of the binding level which we created up in the
2656      `pushlevel_class' routine.  */
2657   if (ENABLE_SCOPE_CHECKING)
2658     is_class_level = 1;
2659
2660   leave_scope ();
2661   timevar_pop (TV_NAME_LOOKUP);
2662 }
2663
2664 /* Bind DECL to ID in the class_binding_level.  Returns nonzero if the
2665    binding was successful.  */
2666
2667 int
2668 push_class_binding (tree id, tree decl)
2669 {
2670   int result = 1;
2671   cxx_binding *binding = IDENTIFIER_BINDING (id);
2672   tree context;
2673
2674   timevar_push (TV_NAME_LOOKUP);
2675   /* Note that we declared this value so that we can issue an error if
2676      this is an invalid redeclaration of a name already used for some
2677      other purpose.  */
2678   note_name_declared_in_class (id, decl);
2679
2680   if (binding && binding->scope == class_binding_level)
2681     /* Supplement the existing binding.  */
2682     result = supplement_binding (IDENTIFIER_BINDING (id), decl);
2683   else
2684     /* Create a new binding.  */
2685     push_binding (id, decl, class_binding_level);
2686
2687   /* If this is a binding from a base class, mark it as such.  */
2688   binding = IDENTIFIER_BINDING (id);
2689   if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
2690     {
2691       if (TREE_CODE (decl) == OVERLOAD)
2692         context = CP_DECL_CONTEXT (OVL_CURRENT (decl));
2693       else
2694         {
2695           my_friendly_assert (DECL_P (decl), 0);
2696           context = context_for_name_lookup (decl);
2697         }
2698
2699       if (is_properly_derived_from (current_class_type, context))
2700         INHERITED_VALUE_BINDING_P (binding) = 1;
2701       else
2702         INHERITED_VALUE_BINDING_P (binding) = 0;
2703     }
2704   else if (binding->value == decl)
2705     /* We only encounter a TREE_LIST when push_class_decls detects an
2706        ambiguity.  Such an ambiguity can be overridden by a definition
2707        in this class.  */
2708     INHERITED_VALUE_BINDING_P (binding) = 1;
2709
2710   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, result);
2711 }
2712
2713 /* Make the declaration of X appear in CLASS scope.  */
2714
2715 bool
2716 pushdecl_class_level (tree x)
2717 {
2718   tree name;
2719   bool is_valid = true;
2720
2721   timevar_push (TV_NAME_LOOKUP);
2722   /* Get the name of X.  */
2723   if (TREE_CODE (x) == OVERLOAD)
2724     name = DECL_NAME (get_first_fn (x));
2725   else
2726     name = DECL_NAME (x);
2727
2728   if (name)
2729     {
2730       is_valid = push_class_level_binding (name, x);
2731       if (TREE_CODE (x) == TYPE_DECL)
2732         set_identifier_type_value (name, x);
2733     }
2734   else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
2735     {
2736       /* If X is an anonymous aggregate, all of its members are
2737          treated as if they were members of the class containing the
2738          aggregate, for naming purposes.  */
2739       tree f;
2740
2741       for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = TREE_CHAIN (f))
2742         {
2743           location_t save_location = input_location;
2744           input_location = DECL_SOURCE_LOCATION (f);
2745           if (!pushdecl_class_level (f))
2746             is_valid = false;
2747           input_location = save_location;
2748         }
2749     }
2750   timevar_pop (TV_NAME_LOOKUP);
2751
2752   return is_valid;
2753 }
2754
2755 /* Make the declaration(s) of X appear in CLASS scope under the name
2756    NAME.  Returns true if the binding is valid.  */
2757
2758 bool
2759 push_class_level_binding (tree name, tree x)
2760 {
2761   cxx_binding *binding;
2762
2763   timevar_push (TV_NAME_LOOKUP);
2764   /* The class_binding_level will be NULL if x is a template
2765      parameter name in a member template.  */
2766   if (!class_binding_level)
2767     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2768
2769   /* Check for invalid member names, if the class is being defined.
2770      This function is also used to restore bindings when reentering
2771      the class scope, and there is no point in checking again at that
2772      time.  */
2773   if (TYPE_BEING_DEFINED (current_class_type))
2774     {
2775       tree decl = x;
2776
2777       /* We could have been passed a tree list if this is an ambiguous
2778          declaration. If so, pull the declaration out because
2779          check_template_shadow will not handle a TREE_LIST. */
2780       if (TREE_CODE (decl) == TREE_LIST 
2781           && TREE_TYPE (decl) == error_mark_node)
2782         decl = TREE_VALUE (decl);
2783       
2784       check_template_shadow (decl);
2785
2786       /* [class.mem]
2787
2788          If T is the name of a class, then each of the following shall
2789          have a name different from T:
2790
2791          -- every static data member of class T;
2792
2793          -- every member of class T that is itself a type;
2794
2795          -- every enumerator of every member of class T that is an
2796             enumerated type;
2797
2798          -- every member of every anonymous union that is a member of
2799             class T.
2800
2801          (Non-static data members were also forbidden to have the same
2802          name as T until TC1.)  */
2803       if ((TREE_CODE (x) == VAR_DECL
2804            || TREE_CODE (x) == CONST_DECL
2805            || (TREE_CODE (x) == TYPE_DECL
2806                && !DECL_SELF_REFERENCE_P (x))
2807            /* A data member of an anonymous union.  */
2808            || (TREE_CODE (x) == FIELD_DECL
2809                && DECL_CONTEXT (x) != current_class_type))
2810           && DECL_NAME (x) == constructor_name (current_class_type))
2811         {
2812           tree scope = context_for_name_lookup (x);
2813           if (TYPE_P (scope) && same_type_p (scope, current_class_type))
2814             {
2815               error ("`%D' has the same name as the class in which it is "
2816                      "declared",
2817                      x);
2818               POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, false);
2819             }
2820         }
2821     }
2822
2823   /* If this declaration shadows a declaration from an enclosing
2824      class, then we will need to restore bindings when we leave this
2825      class.  Record the shadowed declaration here.  */
2826   binding = IDENTIFIER_BINDING (name);
2827   if (binding && binding->value)
2828     {
2829       tree bval = binding->value;
2830       tree old_decl = NULL_TREE;
2831
2832       if (INHERITED_VALUE_BINDING_P (binding))
2833         {
2834           /* If the old binding was from a base class, and was for a
2835              tag name, slide it over to make room for the new binding.
2836              The old binding is still visible if explicitly qualified
2837              with a class-key.  */
2838           if (TREE_CODE (bval) == TYPE_DECL && DECL_ARTIFICIAL (bval)
2839               && !(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)))
2840             {
2841               old_decl = binding->type;
2842               binding->type = bval;
2843               binding->value = NULL_TREE;
2844               INHERITED_VALUE_BINDING_P (binding) = 0;
2845             }
2846           else
2847             old_decl = bval;
2848         }
2849       else if (TREE_CODE (x) == OVERLOAD && is_overloaded_fn (bval))
2850         old_decl = bval;
2851       else if (TREE_CODE (x) == USING_DECL && TREE_CODE (bval) == USING_DECL)
2852         POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2853       else if (TREE_CODE (x) == USING_DECL && is_overloaded_fn (bval))
2854         old_decl = bval;
2855       else if (TREE_CODE (bval) == USING_DECL && is_overloaded_fn (x))
2856         POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2857
2858       if (old_decl
2859           && binding->scope == class_binding_level)
2860         {
2861           binding->value = x;
2862           /* It is always safe to clear INHERITED_VALUE_BINDING_P
2863              here.  That flag is only set when setup_class_bindings
2864              inserts a binding from a base class, and
2865              setup_class_bindings only inserts a binding once for
2866              every name declared in the class and its base classes.
2867              So, if we see a second binding for this name, it must be
2868              coming from a definition in the body of the class
2869              itself.  */
2870           INHERITED_VALUE_BINDING_P (binding) = 0;
2871           POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2872         }
2873     }
2874
2875   /* If we didn't replace an existing binding, put the binding on the
2876      stack of bindings for the identifier, and update the shadowed list.  */
2877   if (push_class_binding (name, x))
2878     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2879
2880   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, false);
2881 }
2882
2883 tree
2884 do_class_using_decl (tree decl)
2885 {
2886   tree name, value, scope, type;
2887   
2888   if (TREE_CODE (decl) != SCOPE_REF
2889       || !TREE_OPERAND (decl, 0)
2890       || !TYPE_P (TREE_OPERAND (decl, 0)))
2891     {
2892       error ("using-declaration for non-member at class scope");
2893       return NULL_TREE;
2894     }
2895   scope = TREE_OPERAND (decl, 0);
2896   name = TREE_OPERAND (decl, 1);
2897   if (TREE_CODE (name) == BIT_NOT_EXPR)
2898     {
2899       error ("using-declaration cannot name destructor");
2900       return NULL_TREE;
2901     }
2902   if (TREE_CODE (name) == TYPE_DECL)
2903     name = DECL_NAME (name);
2904   else if (TREE_CODE (name) == TEMPLATE_DECL)
2905      name = DECL_NAME (name);
2906   else if (BASELINK_P (name))
2907     {
2908       tree fns = BASELINK_FUNCTIONS (name);
2909       name = DECL_NAME (get_first_fn (fns));
2910     }
2911
2912   my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 980716);
2913
2914   /* Dependent using decls have a NULL type, non-dependent ones have a
2915      void type.  */
2916   type = dependent_type_p (scope) ? NULL_TREE : void_type_node;
2917   value = build_lang_decl (USING_DECL, name, type);
2918   DECL_INITIAL (value) = scope;
2919
2920   if (scope && !processing_template_decl)
2921     {
2922       tree r;
2923
2924       r = lookup_qualified_name (scope, name, false, false);
2925       if (r && TREE_CODE (r) != ERROR_MARK)
2926         cp_emit_debug_info_for_using (r, scope);
2927     }
2928   return value;
2929 }
2930
2931 \f
2932 /* Return the binding value for name in scope.  */
2933
2934 tree
2935 namespace_binding (tree name, tree scope)
2936 {
2937   cxx_binding *binding;
2938
2939   if (scope == NULL)
2940     scope = global_namespace;
2941   scope = ORIGINAL_NAMESPACE (scope);
2942   binding = cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
2943
2944   return binding ? binding->value : NULL_TREE;
2945 }
2946
2947 /* Set the binding value for name in scope.  */
2948
2949 void
2950 set_namespace_binding (tree name, tree scope, tree val)
2951 {
2952   cxx_binding *b;
2953
2954   timevar_push (TV_NAME_LOOKUP);
2955   if (scope == NULL_TREE)
2956     scope = global_namespace;
2957   b = binding_for_name (NAMESPACE_LEVEL (scope), name);
2958   if (!b->value || TREE_CODE (val) == OVERLOAD || val == error_mark_node)
2959     b->value = val;
2960   else
2961     supplement_binding (b, val);
2962   timevar_pop (TV_NAME_LOOKUP);
2963 }
2964
2965 /* Compute the namespace where a declaration is defined.  */
2966
2967 static tree
2968 decl_namespace (tree decl)
2969 {
2970   timevar_push (TV_NAME_LOOKUP);
2971   if (TYPE_P (decl))
2972     decl = TYPE_STUB_DECL (decl);
2973   while (DECL_CONTEXT (decl))
2974     {
2975       decl = DECL_CONTEXT (decl);
2976       if (TREE_CODE (decl) == NAMESPACE_DECL)
2977         POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2978       if (TYPE_P (decl))
2979         decl = TYPE_STUB_DECL (decl);
2980       my_friendly_assert (DECL_P (decl), 390);
2981     }
2982
2983   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, global_namespace);
2984 }
2985
2986 /* Set the context of a declaration to scope. Complain if we are not
2987    outside scope.  */
2988
2989 void
2990 set_decl_namespace (tree decl, tree scope, bool friendp)
2991 {
2992   tree old;
2993   
2994   /* Get rid of namespace aliases.  */
2995   scope = ORIGINAL_NAMESPACE (scope);
2996   
2997   /* It is ok for friends to be qualified in parallel space.  */
2998   if (!friendp && !is_ancestor (current_namespace, scope))
2999     error ("declaration of `%D' not in a namespace surrounding `%D'",
3000               decl, scope);
3001   DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3002   if (scope != current_namespace)
3003     {
3004       /* See whether this has been declared in the namespace.  */
3005       old = namespace_binding (DECL_NAME (decl), scope);
3006       if (!old)
3007         /* No old declaration at all.  */
3008         goto complain;
3009       /* A template can be explicitly specialized in any namespace.  */
3010       if (processing_explicit_instantiation)
3011         return;
3012       if (!is_overloaded_fn (decl))
3013         /* Don't compare non-function decls with decls_match here,
3014            since it can't check for the correct constness at this
3015            point. pushdecl will find those errors later.  */
3016         return;
3017       /* Since decl is a function, old should contain a function decl.  */
3018       if (!is_overloaded_fn (old))
3019         goto complain;
3020       if (processing_template_decl || processing_specialization)
3021         /* We have not yet called push_template_decl to turn a
3022            FUNCTION_DECL into a TEMPLATE_DECL, so the declarations
3023            won't match.  But, we'll check later, when we construct the
3024            template.  */
3025         return;
3026       if (is_overloaded_fn (old))
3027         {
3028           for (; old; old = OVL_NEXT (old))
3029             if (decls_match (decl, OVL_CURRENT (old)))
3030               return;
3031         }
3032       else
3033         if (decls_match (decl, old))
3034           return;
3035     }
3036   else
3037     return;
3038  complain:
3039   error ("`%D' should have been declared inside `%D'",
3040             decl, scope);
3041
3042
3043 /* Return the namespace where the current declaration is declared.  */
3044
3045 tree
3046 current_decl_namespace (void)
3047 {
3048   tree result;
3049   /* If we have been pushed into a different namespace, use it.  */
3050   if (decl_namespace_list)
3051     return TREE_PURPOSE (decl_namespace_list);
3052
3053   if (current_class_type)
3054     result = decl_namespace (TYPE_STUB_DECL (current_class_type));
3055   else if (current_function_decl)
3056     result = decl_namespace (current_function_decl);
3057   else 
3058     result = current_namespace;
3059   return result;
3060 }
3061
3062 /* Push into the scope of the NAME namespace.  If NAME is NULL_TREE, then we
3063    select a name that is unique to this compilation unit.  */
3064
3065 void
3066 push_namespace (tree name)
3067 {
3068   tree d = NULL_TREE;
3069   int need_new = 1;
3070   int implicit_use = 0;
3071   bool anon = !name;
3072
3073   timevar_push (TV_NAME_LOOKUP);
3074   
3075   /* We should not get here if the global_namespace is not yet constructed
3076      nor if NAME designates the global namespace:  The global scope is
3077      constructed elsewhere.  */
3078   my_friendly_assert (global_namespace != NULL && name != global_scope_name,
3079                       20030531);
3080
3081   if (anon)
3082     {
3083       /* The name of anonymous namespace is unique for the translation
3084          unit.  */
3085       if (!anonymous_namespace_name)
3086         anonymous_namespace_name = get_file_function_name ('N');
3087       name = anonymous_namespace_name;
3088       d = IDENTIFIER_NAMESPACE_VALUE (name);
3089       if (d)
3090         /* Reopening anonymous namespace.  */
3091         need_new = 0;
3092       implicit_use = 1;
3093     }
3094   else
3095     {
3096       /* Check whether this is an extended namespace definition.  */
3097       d = IDENTIFIER_NAMESPACE_VALUE (name);
3098       if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
3099         {
3100           need_new = 0;
3101           if (DECL_NAMESPACE_ALIAS (d))
3102             {
3103               error ("namespace alias `%D' not allowed here, assuming `%D'",
3104                         d, DECL_NAMESPACE_ALIAS (d));
3105               d = DECL_NAMESPACE_ALIAS (d);
3106             }
3107         }
3108     }
3109
3110   if (need_new)
3111     {
3112       /* Make a new namespace, binding the name to it.  */
3113       d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
3114       DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace);
3115       pushdecl (d);
3116       if (anon)
3117         {
3118           /* Clear DECL_NAME for the benefit of debugging back ends.  */
3119           SET_DECL_ASSEMBLER_NAME (d, name);
3120           DECL_NAME (d) = NULL_TREE;
3121         }
3122       begin_scope (sk_namespace, d);
3123     }
3124   else
3125     resume_scope (NAMESPACE_LEVEL (d));
3126
3127   if (implicit_use)
3128     do_using_directive (d);
3129   /* Enter the name space.  */
3130   current_namespace = d;
3131
3132   timevar_pop (TV_NAME_LOOKUP);
3133 }
3134
3135 /* Pop from the scope of the current namespace.  */
3136
3137 void
3138 pop_namespace (void)
3139 {
3140   my_friendly_assert (current_namespace != global_namespace, 20010801);
3141   current_namespace = CP_DECL_CONTEXT (current_namespace);
3142   /* The binding level is not popped, as it might be re-opened later.  */
3143   leave_scope ();
3144 }
3145
3146 /* Push into the scope of the namespace NS, even if it is deeply
3147    nested within another namespace.  */
3148
3149 void
3150 push_nested_namespace (tree ns)
3151 {
3152   if (ns == global_namespace)
3153     push_to_top_level ();
3154   else
3155     {
3156       push_nested_namespace (CP_DECL_CONTEXT (ns));
3157       push_namespace (DECL_NAME (ns));
3158     }
3159 }
3160
3161 /* Pop back from the scope of the namespace NS, which was previously
3162    entered with push_nested_namespace.  */
3163
3164 void
3165 pop_nested_namespace (tree ns)
3166 {
3167   timevar_push (TV_NAME_LOOKUP);
3168   while (ns != global_namespace)
3169     {
3170       pop_namespace ();
3171       ns = CP_DECL_CONTEXT (ns);
3172     }
3173
3174   pop_from_top_level ();
3175   timevar_pop (TV_NAME_LOOKUP);
3176 }
3177
3178 /* Temporarily set the namespace for the current declaration.  */
3179
3180 void
3181 push_decl_namespace (tree decl)
3182 {
3183   if (TREE_CODE (decl) != NAMESPACE_DECL)
3184     decl = decl_namespace (decl);
3185   decl_namespace_list = tree_cons (ORIGINAL_NAMESPACE (decl),
3186                                    NULL_TREE, decl_namespace_list);
3187 }
3188
3189 /* [namespace.memdef]/2 */
3190
3191 void
3192 pop_decl_namespace (void)
3193 {
3194   decl_namespace_list = TREE_CHAIN (decl_namespace_list);
3195 }
3196
3197 /* Return the namespace that is the common ancestor 
3198    of two given namespaces.  */
3199
3200 static tree
3201 namespace_ancestor (tree ns1, tree ns2)
3202 {
3203   timevar_push (TV_NAME_LOOKUP);
3204   if (is_ancestor (ns1, ns2))
3205     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ns1);
3206   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
3207                           namespace_ancestor (CP_DECL_CONTEXT (ns1), ns2));
3208 }
3209
3210 /* Process a namespace-alias declaration.  */
3211
3212 void
3213 do_namespace_alias (tree alias, tree namespace)
3214 {
3215   if (TREE_CODE (namespace) != NAMESPACE_DECL)
3216     {
3217       /* The parser did not find it, so it's not there.  */
3218       error ("unknown namespace `%D'", namespace);
3219       return;
3220     }
3221
3222   namespace = ORIGINAL_NAMESPACE (namespace);
3223
3224   /* Build the alias.  */
3225   alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);     
3226   DECL_NAMESPACE_ALIAS (alias) = namespace;
3227   DECL_EXTERNAL (alias) = 1;
3228   pushdecl (alias);
3229
3230   /* Emit debug info for namespace alias.  */
3231   (*debug_hooks->global_decl) (alias);
3232 }
3233
3234 /* Like pushdecl, only it places X in the current namespace,
3235    if appropriate.  */
3236
3237 tree
3238 pushdecl_namespace_level (tree x)
3239 {
3240   struct cp_binding_level *b = current_binding_level;
3241   tree t;
3242
3243   timevar_push (TV_NAME_LOOKUP);
3244   t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace));
3245
3246   /* Now, the type_shadowed stack may screw us.  Munge it so it does
3247      what we want.  */
3248   if (TREE_CODE (x) == TYPE_DECL)
3249     {
3250       tree name = DECL_NAME (x);
3251       tree newval;
3252       tree *ptr = (tree *)0;
3253       for (; !global_scope_p (b); b = b->level_chain)
3254         {
3255           tree shadowed = b->type_shadowed;
3256           for (; shadowed; shadowed = TREE_CHAIN (shadowed))
3257             if (TREE_PURPOSE (shadowed) == name)
3258               {
3259                 ptr = &TREE_VALUE (shadowed);
3260                 /* Can't break out of the loop here because sometimes
3261                    a binding level will have duplicate bindings for
3262                    PT names.  It's gross, but I haven't time to fix it.  */
3263               }
3264         }
3265       newval = TREE_TYPE (x);
3266       if (ptr == (tree *)0)
3267         {
3268           /* @@ This shouldn't be needed.  My test case "zstring.cc" trips
3269              up here if this is changed to an assertion.  --KR  */
3270           SET_IDENTIFIER_TYPE_VALUE (name, x);
3271         }
3272       else
3273         {
3274           *ptr = newval;
3275         }
3276     }
3277   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3278 }
3279
3280 /* Insert USED into the using list of USER. Set INDIRECT_flag if this
3281    directive is not directly from the source. Also find the common
3282    ancestor and let our users know about the new namespace */
3283 static void 
3284 add_using_namespace (tree user, tree used, bool indirect)
3285 {
3286   tree t;
3287   timevar_push (TV_NAME_LOOKUP);
3288   /* Using oneself is a no-op.  */
3289   if (user == used)
3290     {
3291       timevar_pop (TV_NAME_LOOKUP);
3292       return;
3293     }
3294   my_friendly_assert (TREE_CODE (user) == NAMESPACE_DECL, 380);
3295   my_friendly_assert (TREE_CODE (used) == NAMESPACE_DECL, 380);
3296   /* Check if we already have this.  */
3297   t = purpose_member (used, DECL_NAMESPACE_USING (user));
3298   if (t != NULL_TREE)
3299     {
3300       if (!indirect)
3301         /* Promote to direct usage.  */
3302         TREE_INDIRECT_USING (t) = 0;
3303       timevar_pop (TV_NAME_LOOKUP);
3304       return;
3305     }
3306
3307   /* Add used to the user's using list.  */
3308   DECL_NAMESPACE_USING (user) 
3309     = tree_cons (used, namespace_ancestor (user, used), 
3310                  DECL_NAMESPACE_USING (user));
3311
3312   TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect;
3313
3314   /* Add user to the used's users list.  */
3315   DECL_NAMESPACE_USERS (used)
3316     = tree_cons (user, 0, DECL_NAMESPACE_USERS (used));
3317
3318   /* Recursively add all namespaces used.  */
3319   for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t))
3320     /* indirect usage */
3321     add_using_namespace (user, TREE_PURPOSE (t), 1);
3322
3323   /* Tell everyone using us about the new used namespaces.  */
3324   for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t))
3325     add_using_namespace (TREE_PURPOSE (t), used, 1);
3326   timevar_pop (TV_NAME_LOOKUP);
3327 }
3328
3329 /* Process a using-declaration not appearing in class or local scope.  */
3330
3331 void
3332 do_toplevel_using_decl (tree decl, tree scope, tree name)
3333 {
3334   tree oldval, oldtype, newval, newtype;
3335   tree orig_decl = decl;
3336   cxx_binding *binding;
3337
3338   decl = validate_nonmember_using_decl (decl, scope, name);
3339   if (decl == NULL_TREE)
3340     return;
3341   
3342   binding = binding_for_name (NAMESPACE_LEVEL (current_namespace), name);
3343
3344   oldval = binding->value;
3345   oldtype = binding->type;
3346
3347   do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
3348
3349   /* Emit debug info.  */
3350   if (!processing_template_decl)
3351     cp_emit_debug_info_for_using (orig_decl, current_namespace);
3352
3353   /* Copy declarations found.  */
3354   if (newval)
3355     binding->value = newval;
3356   if (newtype)
3357     binding->type = newtype;
3358   return;
3359 }
3360
3361 /* Process a using-directive.  */
3362
3363 void
3364 do_using_directive (tree namespace)
3365 {
3366   tree context = NULL_TREE;
3367
3368   if (building_stmt_tree ())
3369     add_stmt (build_stmt (USING_STMT, namespace));
3370   
3371   /* using namespace A::B::C; */
3372   if (TREE_CODE (namespace) == SCOPE_REF)
3373       namespace = TREE_OPERAND (namespace, 1);
3374   if (TREE_CODE (namespace) == IDENTIFIER_NODE)
3375     {
3376       /* Lookup in lexer did not find a namespace.  */
3377       if (!processing_template_decl)
3378         error ("namespace `%T' undeclared", namespace);
3379       return;
3380     }
3381   if (TREE_CODE (namespace) != NAMESPACE_DECL)
3382     {
3383       if (!processing_template_decl)
3384         error ("`%T' is not a namespace", namespace);
3385       return;
3386     }
3387   namespace = ORIGINAL_NAMESPACE (namespace);
3388   if (!toplevel_bindings_p ())
3389     {
3390       push_using_directive (namespace);
3391       context = current_scope ();
3392     }
3393   else
3394     {
3395       /* direct usage */
3396       add_using_namespace (current_namespace, namespace, 0);
3397       if (current_namespace != global_namespace)
3398         context = current_namespace;
3399     }
3400       
3401   /* Emit debugging info.  */
3402   if (!processing_template_decl)
3403     (*debug_hooks->imported_module_or_decl) (namespace, context);
3404 }
3405
3406 /* Deal with a using-directive seen by the parser.  Currently we only
3407    handle attributes here, since they cannot appear inside a template.  */
3408
3409 void
3410 parse_using_directive (tree namespace, tree attribs)
3411 {
3412   tree a;
3413
3414   do_using_directive (namespace);
3415
3416   for (a = attribs; a; a = TREE_CHAIN (a))
3417     {
3418       tree name = TREE_PURPOSE (a);
3419       if (is_attribute_p ("strong", name))
3420         {
3421           if (!toplevel_bindings_p ())
3422             error ("strong using only meaningful at namespace scope");
3423           else
3424             DECL_NAMESPACE_ASSOCIATIONS (namespace)
3425               = tree_cons (current_namespace, 0,
3426                            DECL_NAMESPACE_ASSOCIATIONS (namespace));
3427         }
3428       else
3429         warning ("`%D' attribute directive ignored", name);
3430     }
3431 }
3432
3433 /* Like pushdecl, only it places X in the global scope if appropriate.
3434    Calls cp_finish_decl to register the variable, initializing it with
3435    *INIT, if INIT is non-NULL.  */
3436
3437 static tree
3438 pushdecl_top_level_1 (tree x, tree *init)
3439 {
3440   timevar_push (TV_NAME_LOOKUP);
3441   push_to_top_level ();
3442   x = pushdecl_namespace_level (x);
3443   if (init)
3444     cp_finish_decl (x, *init, NULL_TREE, 0);
3445   pop_from_top_level ();
3446   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
3447 }
3448
3449 /* Like pushdecl, only it places X in the global scope if appropriate.  */
3450
3451 tree
3452 pushdecl_top_level (tree x)
3453 {
3454   return pushdecl_top_level_1 (x, NULL);
3455 }
3456
3457 /* Like pushdecl, only it places X in the global scope if
3458    appropriate.  Calls cp_finish_decl to register the variable,
3459    initializing it with INIT.  */
3460
3461 tree
3462 pushdecl_top_level_and_finish (tree x, tree init)
3463 {
3464   return pushdecl_top_level_1 (x, &init);
3465 }
3466
3467 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
3468    duplicates.  The first list becomes the tail of the result.
3469
3470    The algorithm is O(n^2).  We could get this down to O(n log n) by
3471    doing a sort on the addresses of the functions, if that becomes
3472    necessary.  */
3473
3474 static tree
3475 merge_functions (tree s1, tree s2)
3476 {
3477   for (; s2; s2 = OVL_NEXT (s2))
3478     {
3479       tree fn2 = OVL_CURRENT (s2);
3480       tree fns1;
3481
3482       for (fns1 = s1; fns1; fns1 = OVL_NEXT (fns1))
3483         {
3484           tree fn1 = OVL_CURRENT (fns1);
3485
3486           /* If the function from S2 is already in S1, there is no
3487              need to add it again.  For `extern "C"' functions, we
3488              might have two FUNCTION_DECLs for the same function, in
3489              different namespaces; again, we only need one of them.  */
3490           if (fn1 == fn2 
3491               || (DECL_EXTERN_C_P (fn1) && DECL_EXTERN_C_P (fn2)
3492                   && DECL_NAME (fn1) == DECL_NAME (fn2)))
3493             break;
3494         }
3495       
3496       /* If we exhausted all of the functions in S1, FN2 is new.  */
3497       if (!fns1)
3498         s1 = build_overload (fn2, s1);
3499     }
3500   return s1;
3501 }
3502
3503 /* This should return an error not all definitions define functions.
3504    It is not an error if we find two functions with exactly the
3505    same signature, only if these are selected in overload resolution.
3506    old is the current set of bindings, new the freshly-found binding.
3507    XXX Do we want to give *all* candidates in case of ambiguity?
3508    XXX In what way should I treat extern declarations?
3509    XXX I don't want to repeat the entire duplicate_decls here */
3510
3511 static void
3512 ambiguous_decl (tree name, struct scope_binding *old, cxx_binding *new,
3513                 int flags)
3514 {
3515   tree val, type;
3516   my_friendly_assert (old != NULL, 393);
3517   /* Copy the value.  */
3518   val = new->value;
3519   if (val)
3520     switch (TREE_CODE (val))
3521       {
3522       case TEMPLATE_DECL:
3523         /* If we expect types or namespaces, and not templates,
3524            or this is not a template class.  */
3525         if (LOOKUP_QUALIFIERS_ONLY (flags)
3526             && !DECL_CLASS_TEMPLATE_P (val))
3527           val = NULL_TREE;
3528         break;
3529       case TYPE_DECL:
3530         if (LOOKUP_NAMESPACES_ONLY (flags))
3531           val = NULL_TREE;
3532         break;
3533       case NAMESPACE_DECL:
3534         if (LOOKUP_TYPES_ONLY (flags))
3535           val = NULL_TREE;
3536         break;
3537       case FUNCTION_DECL:
3538         /* Ignore built-in functions that are still anticipated.  */
3539         if (LOOKUP_QUALIFIERS_ONLY (flags) || DECL_ANTICIPATED (val))
3540           val = NULL_TREE;
3541         break;
3542       default:
3543         if (LOOKUP_QUALIFIERS_ONLY (flags))
3544           val = NULL_TREE;
3545       }
3546         
3547   if (!old->value)
3548     old->value = val;
3549   else if (val && val != old->value)
3550     {
3551       if (is_overloaded_fn (old->value) && is_overloaded_fn (val))
3552         old->value = merge_functions (old->value, val);
3553       else
3554         {
3555           /* Some declarations are functions, some are not.  */
3556           if (flags & LOOKUP_COMPLAIN)
3557             {
3558               /* If we've already given this error for this lookup,
3559                  old->value is error_mark_node, so let's not
3560                  repeat ourselves.  */
3561               if (old->value != error_mark_node)
3562                 {
3563                   error ("use of `%D' is ambiguous", name);
3564                   cp_error_at ("  first declared as `%#D' here",
3565                                old->value);
3566                 }
3567               cp_error_at ("  also declared as `%#D' here", val);
3568             }
3569           old->value = error_mark_node;
3570         }
3571     }
3572   /* ... and copy the type.  */
3573   type = new->type;
3574   if (LOOKUP_NAMESPACES_ONLY (flags))
3575     type = NULL_TREE;
3576   if (!old->type)
3577     old->type = type;
3578   else if (type && old->type != type)
3579     {
3580       if (flags & LOOKUP_COMPLAIN)
3581         {
3582           error ("`%D' denotes an ambiguous type",name);
3583           error ("%J  first type here", TYPE_MAIN_DECL (old->type));
3584           error ("%J  other type here", TYPE_MAIN_DECL (type));
3585         }
3586     }
3587 }
3588
3589 /* Return the declarations that are members of the namespace NS.  */
3590
3591 tree
3592 cp_namespace_decls (tree ns)
3593 {
3594   return NAMESPACE_LEVEL (ns)->names;
3595 }
3596
3597 /* Combine prefer_type and namespaces_only into flags.  */
3598
3599 static int
3600 lookup_flags (int prefer_type, int namespaces_only)
3601 {
3602   if (namespaces_only)
3603     return LOOKUP_PREFER_NAMESPACES;
3604   if (prefer_type > 1)
3605     return LOOKUP_PREFER_TYPES;
3606   if (prefer_type > 0)
3607     return LOOKUP_PREFER_BOTH;
3608   return 0;
3609 }
3610
3611 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
3612    ignore it or not.  Subroutine of lookup_name_real.  */
3613
3614 static tree
3615 qualify_lookup (tree val, int flags)
3616 {
3617   if (val == NULL_TREE)
3618     return val;
3619   if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
3620     return val;
3621   if ((flags & LOOKUP_PREFER_TYPES)
3622       && (TREE_CODE (val) == TYPE_DECL || TREE_CODE (val) == TEMPLATE_DECL))
3623     return val;
3624   if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
3625     return NULL_TREE;
3626   return val;
3627 }
3628
3629 /* Look up NAME in the NAMESPACE.  */
3630
3631 tree
3632 lookup_namespace_name (tree namespace, tree name)
3633 {
3634   tree val;
3635   tree template_id = NULL_TREE;
3636   struct scope_binding binding = EMPTY_SCOPE_BINDING;
3637
3638   timevar_push (TV_NAME_LOOKUP);
3639   my_friendly_assert (TREE_CODE (namespace) == NAMESPACE_DECL, 370);
3640
3641   if (TREE_CODE (name) == NAMESPACE_DECL)
3642     /* This happens for A::B<int> when B is a namespace.  */
3643     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, name);
3644   else if (TREE_CODE (name) == TEMPLATE_DECL)
3645     {
3646       /* This happens for A::B where B is a template, and there are no
3647          template arguments.  */
3648       error ("invalid use of `%D'", name);
3649       POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
3650     }
3651
3652   namespace = ORIGINAL_NAMESPACE (namespace);
3653
3654   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3655     {
3656       template_id = name;
3657       name = TREE_OPERAND (name, 0);
3658       if (TREE_CODE (name) == OVERLOAD)
3659         name = DECL_NAME (OVL_CURRENT (name));
3660       else if (DECL_P (name))
3661         name = DECL_NAME (name);
3662     }
3663
3664   my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 373);
3665
3666   if (!qualified_lookup_using_namespace (name, namespace, &binding, 0))
3667     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
3668
3669   if (binding.value)
3670     {
3671       val = binding.value;
3672
3673       if (template_id)
3674         {
3675           if (DECL_CLASS_TEMPLATE_P (val))
3676             val = lookup_template_class (val,
3677                                          TREE_OPERAND (template_id, 1),
3678                                          /*in_decl=*/NULL_TREE,
3679                                          /*context=*/NULL_TREE,
3680                                          /*entering_scope=*/0,
3681                                          tf_error | tf_warning);
3682           else if (DECL_FUNCTION_TEMPLATE_P (val)
3683                    || TREE_CODE (val) == OVERLOAD)
3684             val = lookup_template_function (val,
3685                                             TREE_OPERAND (template_id, 1));
3686           else
3687             {
3688               error ("`%D::%D' is not a template",
3689                         namespace, name);
3690               POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
3691             }
3692         }
3693
3694       /* If we have a single function from a using decl, pull it out.  */
3695       if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
3696         val = OVL_FUNCTION (val);
3697
3698       /* Ignore built-in functions that haven't been prototyped yet.  */
3699       if (!val || !DECL_P(val)
3700           || !DECL_LANG_SPECIFIC(val)
3701           || !DECL_ANTICIPATED (val))
3702         POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
3703     }
3704
3705   error ("`%D' undeclared in namespace `%D'", name, namespace);
3706   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
3707 }
3708
3709 /* Select the right _DECL from multiple choices.  */
3710
3711 static tree
3712 select_decl (const struct scope_binding *binding, int flags)
3713 {
3714   tree val;
3715   val = binding->value;
3716
3717   timevar_push (TV_NAME_LOOKUP);
3718   if (LOOKUP_NAMESPACES_ONLY (flags))
3719     {
3720       /* We are not interested in types.  */
3721       if (val && TREE_CODE (val) == NAMESPACE_DECL)
3722         POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
3723       POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
3724     }
3725
3726   /* If looking for a type, or if there is no non-type binding, select
3727      the value binding.  */
3728   if (binding->type && (!val || (flags & LOOKUP_PREFER_TYPES)))
3729     val = binding->type;
3730   /* Don't return non-types if we really prefer types.  */
3731   else if (val && LOOKUP_TYPES_ONLY (flags) 
3732            && ! DECL_DECLARES_TYPE_P (val))
3733     val = NULL_TREE;
3734
3735   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
3736 }
3737
3738 /* Unscoped lookup of a global: iterate over current namespaces,
3739    considering using-directives.  */
3740
3741 static tree
3742 unqualified_namespace_lookup (tree name, int flags)
3743 {
3744   tree initial = current_decl_namespace ();
3745   tree scope = initial;
3746   tree siter;
3747   struct cp_binding_level *level;
3748   tree val = NULL_TREE;
3749   struct scope_binding binding = EMPTY_SCOPE_BINDING;
3750
3751   timevar_push (TV_NAME_LOOKUP);
3752
3753   for (; !val; scope = CP_DECL_CONTEXT (scope))
3754     {
3755       cxx_binding *b =
3756          cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
3757
3758       if (b)
3759         {
3760           if (b->value && DECL_P (b->value)
3761               && DECL_LANG_SPECIFIC (b->value) 
3762               && DECL_ANTICIPATED (b->value))
3763             /* Ignore anticipated built-in functions.  */
3764             ;
3765           else
3766             binding.value = b->value;
3767           binding.type = b->type;
3768         }
3769
3770       /* Add all _DECLs seen through local using-directives.  */
3771       for (level = current_binding_level;
3772            level->kind != sk_namespace;
3773            level = level->level_chain)
3774         if (!lookup_using_namespace (name, &binding, level->using_directives,
3775                                      scope, flags))
3776           /* Give up because of error.  */
3777           POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
3778
3779       /* Add all _DECLs seen through global using-directives.  */
3780       /* XXX local and global using lists should work equally.  */
3781       siter = initial;
3782       while (1)
3783         {
3784           if (!lookup_using_namespace (name, &binding,
3785                                        DECL_NAMESPACE_USING (siter),
3786                                        scope, flags))
3787             /* Give up because of error.  */
3788             POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
3789           if (siter == scope) break;
3790           siter = CP_DECL_CONTEXT (siter);
3791         }
3792
3793       val = select_decl (&binding, flags);
3794       if (scope == global_namespace)
3795         break;
3796     }
3797   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
3798 }
3799
3800 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
3801    or a class TYPE).  If IS_TYPE_P is TRUE, then ignore non-type
3802    bindings.  
3803
3804    Returns a DECL (or OVERLOAD, or BASELINK) representing the
3805    declaration found.  If no suitable declaration can be found,
3806    ERROR_MARK_NODE is returned.  Iif COMPLAIN is true and SCOPE is
3807    neither a class-type nor a namespace a diagnostic is issued.  */
3808
3809 tree
3810 lookup_qualified_name (tree scope, tree name, bool is_type_p, bool complain)
3811 {
3812   int flags = 0;
3813
3814   if (TREE_CODE (scope) == NAMESPACE_DECL)
3815     {
3816       struct scope_binding binding = EMPTY_SCOPE_BINDING;
3817
3818       flags |= LOOKUP_COMPLAIN;
3819       if (is_type_p)
3820         flags |= LOOKUP_PREFER_TYPES;
3821       if (qualified_lookup_using_namespace (name, scope, &binding, flags))
3822         return select_decl (&binding, flags);
3823     }
3824   else if (is_aggr_type (scope, complain))
3825     {
3826       tree t;
3827       t = lookup_member (scope, name, 0, is_type_p);
3828       if (t)
3829         return t;
3830     }
3831
3832   return error_mark_node;
3833 }
3834
3835 /* Subroutine of unqualified_namespace_lookup:
3836    Add the bindings of NAME in used namespaces to VAL.
3837    We are currently looking for names in namespace SCOPE, so we
3838    look through USINGS for using-directives of namespaces
3839    which have SCOPE as a common ancestor with the current scope.
3840    Returns false on errors.  */
3841
3842 static bool
3843 lookup_using_namespace (tree name, struct scope_binding *val,
3844                         tree usings, tree scope, int flags)
3845 {
3846   tree iter;
3847   timevar_push (TV_NAME_LOOKUP);
3848   /* Iterate over all used namespaces in current, searching for using
3849      directives of scope.  */
3850   for (iter = usings; iter; iter = TREE_CHAIN (iter))
3851     if (TREE_VALUE (iter) == scope)
3852       {
3853         tree used = ORIGINAL_NAMESPACE (TREE_PURPOSE (iter));
3854         cxx_binding *val1 =
3855           cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (used), name);
3856         /* Resolve ambiguities.  */
3857         if (val1)
3858           ambiguous_decl (name, val, val1, flags);
3859       }
3860   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val->value != error_mark_node);
3861 }
3862
3863 /* [namespace.qual]
3864    Accepts the NAME to lookup and its qualifying SCOPE.
3865    Returns the name/type pair found into the cxx_binding *RESULT,
3866    or false on error.  */
3867
3868 static bool
3869 qualified_lookup_using_namespace (tree name, tree scope,
3870                                   struct scope_binding *result, int flags)
3871 {
3872   /* Maintain a list of namespaces visited...  */
3873   tree seen = NULL_TREE;
3874   /* ... and a list of namespace yet to see.  */
3875   tree todo = NULL_TREE;
3876   tree todo_maybe = NULL_TREE;
3877   tree usings;
3878   timevar_push (TV_NAME_LOOKUP);
3879   /* Look through namespace aliases.  */
3880   scope = ORIGINAL_NAMESPACE (scope);
3881   while (scope && result->value != error_mark_node)
3882     {
3883       cxx_binding *binding =
3884         cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
3885       seen = tree_cons (scope, NULL_TREE, seen);
3886       if (binding)
3887         ambiguous_decl (name, result, binding, flags);
3888
3889       /* Consider strong using directives always, and non-strong ones
3890          if we haven't found a binding yet.  ??? Shouldn't we consider
3891          non-strong ones if the initial RESULT is non-NULL, but the
3892          binding in the given namespace is?  */
3893       for (usings = DECL_NAMESPACE_USING (scope); usings;
3894            usings = TREE_CHAIN (usings))
3895         /* If this was a real directive, and we have not seen it.  */
3896         if (!TREE_INDIRECT_USING (usings))
3897           {
3898             /* Try to avoid queuing the same namespace more than once,
3899                the exception being when a namespace was already
3900                enqueued for todo_maybe and then a strong using is
3901                found for it.  We could try to remove it from
3902                todo_maybe, but it's probably not worth the effort.  */
3903             if (is_associated_namespace (scope, TREE_PURPOSE (usings))
3904                 && !purpose_member (TREE_PURPOSE (usings), seen)
3905                 && !purpose_member (TREE_PURPOSE (usings), todo))
3906               todo = tree_cons (TREE_PURPOSE (usings), NULL_TREE, todo);
3907             else if ((!result->value && !result->type)
3908                      && !purpose_member (TREE_PURPOSE (usings), seen)
3909                      && !purpose_member (TREE_PURPOSE (usings), todo)
3910                      && !purpose_member (TREE_PURPOSE (usings), todo_maybe))
3911               todo_maybe = tree_cons (TREE_PURPOSE (usings), NULL_TREE,
3912                                       todo_maybe);
3913           }
3914       if (todo)
3915         {
3916           scope = TREE_PURPOSE (todo);
3917           todo = TREE_CHAIN (todo);
3918         }
3919       else if (todo_maybe
3920                && (!result->value && !result->type))
3921         {
3922           scope = TREE_PURPOSE (todo_maybe);
3923           todo = TREE_CHAIN (todo_maybe);
3924           todo_maybe = NULL_TREE;
3925         }
3926       else
3927         scope = NULL_TREE; /* If there never was a todo list.  */
3928     }
3929   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, result->value != error_mark_node);
3930 }
3931
3932 /* Look up NAME in the current binding level and its superiors in the
3933    namespace of variables, functions and typedefs.  Return a ..._DECL
3934    node of some kind representing its definition if there is only one
3935    such declaration, or return a TREE_LIST with all the overloaded
3936    definitions if there are many, or return 0 if it is undefined.
3937
3938    If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
3939    If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
3940    Otherwise we prefer non-TYPE_DECLs.
3941
3942    If NONCLASS is nonzero, bindings in class scopes are ignored.  If
3943    BLOCK_P is false, bindings in block scopes are ignored.  */
3944
3945 tree
3946 lookup_name_real (tree name, int prefer_type, int nonclass, bool block_p,
3947                   int namespaces_only, int flags)
3948 {
3949   cxx_binding *iter;
3950   tree val = NULL_TREE;
3951
3952   timevar_push (TV_NAME_LOOKUP);
3953   /* Conversion operators are handled specially because ordinary
3954      unqualified name lookup will not find template conversion
3955      operators.  */
3956   if (IDENTIFIER_TYPENAME_P (name)) 
3957     {
3958       struct cp_binding_level *level;
3959
3960       for (level = current_binding_level; 
3961            level && level->kind != sk_namespace;
3962            level = level->level_chain)
3963         {
3964           tree class_type;
3965           tree operators;
3966           
3967           /* A conversion operator can only be declared in a class 
3968              scope.  */
3969           if (level->kind != sk_class)
3970             continue;
3971           
3972           /* Lookup the conversion operator in the class.  */
3973           class_type = level->this_entity;
3974           operators = lookup_fnfields (class_type, name, /*protect=*/0);
3975           if (operators)
3976             POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, operators);
3977         }
3978
3979       POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
3980     }
3981
3982   flags |= lookup_flags (prefer_type, namespaces_only);
3983
3984   /* First, look in non-namespace scopes.  */
3985
3986   if (current_class_type == NULL_TREE)
3987     nonclass = 1;
3988
3989   if (block_p || !nonclass)
3990     for (iter = IDENTIFIER_BINDING (name); iter; iter = iter->previous)
3991       {
3992         tree binding;
3993         
3994         /* Skip entities we don't want.  */
3995         if (LOCAL_BINDING_P (iter) ? !block_p : nonclass)
3996           continue;
3997         
3998         /* If this is the kind of thing we're looking for, we're done.  */
3999         if (qualify_lookup (iter->value, flags))
4000           binding = iter->value;
4001         else if ((flags & LOOKUP_PREFER_TYPES)
4002                  && qualify_lookup (iter->type, flags))
4003           binding = iter->type;
4004         else
4005           binding = NULL_TREE;
4006         
4007         if (binding)
4008           {
4009             val = binding;
4010             break;
4011           }
4012       }
4013
4014   /* Now lookup in namespace scopes.  */
4015   if (!val)
4016     {
4017       tree t = unqualified_namespace_lookup (name, flags);
4018       if (t)
4019         val = t;
4020     }
4021
4022   if (val)
4023     {
4024       /* If we have a single function from a using decl, pull it out.  */
4025       if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
4026         val = OVL_FUNCTION (val);
4027     }
4028
4029   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
4030 }
4031
4032 tree
4033 lookup_name_nonclass (tree name)
4034 {
4035   return lookup_name_real (name, 0, 1, /*block_p=*/true, 0, LOOKUP_COMPLAIN);
4036 }
4037
4038 tree
4039 lookup_function_nonclass (tree name, tree args, bool block_p)
4040 {
4041   return 
4042     lookup_arg_dependent (name, 
4043                           lookup_name_real (name, 0, 1, block_p, 0, 
4044                                             LOOKUP_COMPLAIN),
4045                           args);
4046 }
4047
4048 tree
4049 lookup_name (tree name, int prefer_type)
4050 {
4051   return lookup_name_real (name, prefer_type, 0, /*block_p=*/true, 
4052                            0, LOOKUP_COMPLAIN);
4053 }
4054
4055 /* Similar to `lookup_name' but look only in the innermost non-class
4056    binding level.  */
4057
4058 static tree
4059 lookup_name_current_level (tree name)
4060 {
4061   struct cp_binding_level *b;
4062   tree t = NULL_TREE;
4063
4064   timevar_push (TV_NAME_LOOKUP);
4065   b = innermost_nonclass_level ();
4066
4067   if (b->kind == sk_namespace)
4068     {
4069       t = IDENTIFIER_NAMESPACE_VALUE (name);
4070
4071       /* extern "C" function() */
4072       if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
4073         t = TREE_VALUE (t);
4074     }
4075   else if (IDENTIFIER_BINDING (name)
4076            && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
4077     {
4078       while (1)
4079         {
4080           if (IDENTIFIER_BINDING (name)->scope == b)
4081             POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, IDENTIFIER_VALUE (name));
4082
4083           if (b->kind == sk_cleanup)
4084             b = b->level_chain;
4085           else
4086             break;
4087         }
4088     }
4089
4090   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4091 }
4092
4093 /* Like lookup_name_current_level, but for types.  */
4094
4095 static tree
4096 lookup_type_current_level (tree name)
4097 {
4098   tree t = NULL_TREE;
4099
4100   timevar_push (TV_NAME_LOOKUP);
4101   my_friendly_assert (current_binding_level->kind != sk_namespace, 
4102                       980716);
4103
4104   if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
4105       && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
4106     {
4107       struct cp_binding_level *b = current_binding_level;
4108       while (1)
4109         {
4110           if (purpose_member (name, b->type_shadowed))
4111             POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
4112                                     REAL_IDENTIFIER_TYPE_VALUE (name));
4113           if (b->kind == sk_cleanup)
4114             b = b->level_chain;
4115           else
4116             break;
4117         }
4118     }
4119
4120   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4121 }
4122
4123 /* [basic.lookup.koenig] */
4124 /* A nonzero return value in the functions below indicates an error.  */
4125
4126 struct arg_lookup
4127 {
4128   tree name;
4129   tree namespaces;
4130   tree classes;
4131   tree functions;
4132 };
4133
4134 static bool arg_assoc (struct arg_lookup*, tree);
4135 static bool arg_assoc_args (struct arg_lookup*, tree);
4136 static bool arg_assoc_type (struct arg_lookup*, tree);
4137 static bool add_function (struct arg_lookup *, tree);
4138 static bool arg_assoc_namespace (struct arg_lookup *, tree);
4139 static bool arg_assoc_class (struct arg_lookup *, tree);
4140 static bool arg_assoc_template_arg (struct arg_lookup*, tree);
4141
4142 /* Add a function to the lookup structure.
4143    Returns true on error.  */
4144
4145 static bool
4146 add_function (struct arg_lookup *k, tree fn)
4147 {
4148   /* We used to check here to see if the function was already in the list,
4149      but that's O(n^2), which is just too expensive for function lookup.
4150      Now we deal with the occasional duplicate in joust.  In doing this, we
4151      assume that the number of duplicates will be small compared to the
4152      total number of functions being compared, which should usually be the
4153      case.  */
4154
4155   /* We must find only functions, or exactly one non-function.  */
4156   if (!k->functions) 
4157     k->functions = fn;
4158   else if (fn == k->functions)
4159     ;
4160   else if (is_overloaded_fn (k->functions) && is_overloaded_fn (fn))
4161     k->functions = build_overload (fn, k->functions);
4162   else
4163     {
4164       tree f1 = OVL_CURRENT (k->functions);
4165       tree f2 = fn;
4166       if (is_overloaded_fn (f1))
4167         {
4168           fn = f1; f1 = f2; f2 = fn;
4169         }
4170       cp_error_at ("`%D' is not a function,", f1);
4171       cp_error_at ("  conflict with `%D'", f2);
4172       error ("  in call to `%D'", k->name);
4173       return true;
4174     }
4175
4176   return false;
4177 }
4178
4179 /* Returns true iff CURRENT has declared itself to be an associated
4180    namespace of SCOPE via a strong using-directive (or transitive chain
4181    thereof).  Both are namespaces.  */
4182
4183 bool
4184 is_associated_namespace (tree current, tree scope)
4185 {
4186   tree seen = NULL_TREE;
4187   tree todo = NULL_TREE;
4188   tree t;
4189   while (1)
4190     {
4191       if (scope == current)
4192         return true;
4193       seen = tree_cons (scope, NULL_TREE, seen);
4194       for (t = DECL_NAMESPACE_ASSOCIATIONS (scope); t; t = TREE_CHAIN (t))
4195         if (!purpose_member (TREE_PURPOSE (t), seen))
4196           todo = tree_cons (TREE_PURPOSE (t), NULL_TREE, todo);
4197       if (todo)
4198         {
4199           scope = TREE_PURPOSE (todo);
4200           todo = TREE_CHAIN (todo);
4201         }
4202       else
4203         return false;
4204     }
4205 }
4206
4207 /* Add functions of a namespace to the lookup structure.
4208    Returns true on error.  */
4209
4210 static bool
4211 arg_assoc_namespace (struct arg_lookup *k, tree scope)
4212 {
4213   tree value;
4214
4215   if (purpose_member (scope, k->namespaces))
4216     return 0;
4217   k->namespaces = tree_cons (scope, NULL_TREE, k->namespaces);
4218
4219   /* Check out our super-users.  */
4220   for (value = DECL_NAMESPACE_ASSOCIATIONS (scope); value;
4221        value = TREE_CHAIN (value))
4222     if (arg_assoc_namespace (k, TREE_PURPOSE (value)))
4223       return true;
4224   
4225   value = namespace_binding (k->name, scope);
4226   if (!value)
4227     return false;
4228
4229   for (; value; value = OVL_NEXT (value))
4230     if (add_function (k, OVL_CURRENT (value)))
4231       return true;
4232   
4233   return false;
4234 }
4235
4236 /* Adds everything associated with a template argument to the lookup
4237    structure.  Returns true on error.  */
4238
4239 static bool
4240 arg_assoc_template_arg (struct arg_lookup *k, tree arg)
4241 {
4242   /* [basic.lookup.koenig]
4243
4244      If T is a template-id, its associated namespaces and classes are
4245      ... the namespaces and classes associated with the types of the
4246      template arguments provided for template type parameters
4247      (excluding template template parameters); the namespaces in which
4248      any template template arguments are defined; and the classes in
4249      which any member templates used as template template arguments
4250      are defined.  [Note: non-type template arguments do not
4251      contribute to the set of associated namespaces.  ]  */
4252
4253   /* Consider first template template arguments.  */
4254   if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
4255       || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
4256     return false;
4257   else if (TREE_CODE (arg) == TEMPLATE_DECL)
4258     {
4259       tree ctx = CP_DECL_CONTEXT (arg);
4260
4261       /* It's not a member template.  */
4262       if (TREE_CODE (ctx) == NAMESPACE_DECL)
4263         return arg_assoc_namespace (k, ctx);
4264       /* Otherwise, it must be member template.  */
4265       else 
4266         return arg_assoc_class (k, ctx);
4267     }
4268   /* It's not a template template argument, but it is a type template
4269      argument.  */
4270   else if (TYPE_P (arg))
4271     return arg_assoc_type (k, arg);
4272   /* It's a non-type template argument.  */
4273   else
4274     return false;
4275 }
4276
4277 /* Adds everything associated with class to the lookup structure.
4278    Returns true on error.  */
4279
4280 static bool
4281 arg_assoc_class (struct arg_lookup *k, tree type)
4282 {
4283   tree list, friends, context;
4284   int i;
4285   
4286   /* Backend build structures, such as __builtin_va_list, aren't
4287      affected by all this.  */
4288   if (!CLASS_TYPE_P (type))
4289     return false;
4290
4291   if (purpose_member (type, k->classes))
4292     return false;
4293   k->classes = tree_cons (type, NULL_TREE, k->classes);
4294   
4295   context = decl_namespace (TYPE_MAIN_DECL (type));
4296   if (arg_assoc_namespace (k, context))
4297     return true;
4298
4299   if (TYPE_BINFO (type))
4300     /* Process baseclasses.  */
4301     for (i = 0; i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type)); i++)
4302       if (arg_assoc_class
4303           (k, BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (type), i))))
4304         return true;
4305   
4306   /* Process friends.  */
4307   for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list; 
4308        list = TREE_CHAIN (list))
4309     if (k->name == FRIEND_NAME (list))
4310       for (friends = FRIEND_DECLS (list); friends; 
4311            friends = TREE_CHAIN (friends))
4312         {
4313           tree fn = TREE_VALUE (friends);
4314
4315           /* Only interested in global functions with potentially hidden
4316              (i.e. unqualified) declarations.  */
4317           if (CP_DECL_CONTEXT (fn) != context)
4318             continue;
4319           /* Template specializations are never found by name lookup.
4320              (Templates themselves can be found, but not template
4321              specializations.)  */
4322           if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn))
4323             continue;
4324           if (add_function (k, fn))
4325             return true;
4326         }
4327
4328   /* Process template arguments.  */
4329   if (CLASSTYPE_TEMPLATE_INFO (type) 
4330       && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
4331     {
4332       list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
4333       for (i = 0; i < TREE_VEC_LENGTH (list); ++i) 
4334         arg_assoc_template_arg (k, TREE_VEC_ELT (list, i));
4335     }
4336
4337   return false;
4338 }
4339
4340 /* Adds everything associated with a given type.
4341    Returns 1 on error.  */
4342
4343 static bool
4344 arg_assoc_type (struct arg_lookup *k, tree type)
4345 {
4346   /* As we do not get the type of non-type dependent expressions
4347      right, we can end up with such things without a type.  */
4348   if (!type)
4349     return false;
4350
4351   if (TYPE_PTRMEM_P (type))
4352     {
4353       /* Pointer to member: associate class type and value type.  */
4354       if (arg_assoc_type (k, TYPE_PTRMEM_CLASS_TYPE (type)))
4355         return true;
4356       return arg_assoc_type (k, TYPE_PTRMEM_POINTED_TO_TYPE (type));
4357     }
4358   else switch (TREE_CODE (type))
4359     {
4360     case ERROR_MARK:
4361       return false;
4362     case VOID_TYPE:
4363     case INTEGER_TYPE:
4364     case REAL_TYPE:
4365     case COMPLEX_TYPE:
4366     case VECTOR_TYPE:
4367     case CHAR_TYPE:
4368     case BOOLEAN_TYPE:
4369       return false;
4370     case RECORD_TYPE:
4371       if (TYPE_PTRMEMFUNC_P (type))
4372         return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type));
4373       return arg_assoc_class (k, type);
4374     case POINTER_TYPE:
4375     case REFERENCE_TYPE:
4376     case ARRAY_TYPE:
4377       return arg_assoc_type (k, TREE_TYPE (type));
4378     case UNION_TYPE:
4379     case ENUMERAL_TYPE:
4380       return arg_assoc_namespace (k, decl_namespace (TYPE_MAIN_DECL (type)));
4381     case METHOD_TYPE:
4382       /* The basetype is referenced in the first arg type, so just
4383          fall through.  */
4384     case FUNCTION_TYPE:
4385       /* Associate the parameter types.  */
4386       if (arg_assoc_args (k, TYPE_ARG_TYPES (type)))
4387         return true;
4388       /* Associate the return type.  */
4389       return arg_assoc_type (k, TREE_TYPE (type));
4390     case TEMPLATE_TYPE_PARM:
4391     case BOUND_TEMPLATE_TEMPLATE_PARM:
4392       return false;
4393     case TYPENAME_TYPE:
4394       return false;
4395     case LANG_TYPE:
4396       if (type == unknown_type_node)
4397         return false;
4398       /* else fall through */
4399     default:
4400       abort ();
4401     }
4402   return false;
4403 }
4404
4405 /* Adds everything associated with arguments.  Returns true on error.  */
4406
4407 static bool
4408 arg_assoc_args (struct arg_lookup *k, tree args)
4409 {
4410   for (; args; args = TREE_CHAIN (args))
4411     if (arg_assoc (k, TREE_VALUE (args)))
4412       return true;
4413   return false;
4414 }
4415
4416 /* Adds everything associated with a given tree_node.  Returns 1 on error.  */
4417
4418 static bool
4419 arg_assoc (struct arg_lookup *k, tree n)
4420 {
4421   if (n == error_mark_node)
4422     return false;
4423
4424   if (TYPE_P (n))
4425     return arg_assoc_type (k, n);
4426
4427   if (! type_unknown_p (n))
4428     return arg_assoc_type (k, TREE_TYPE (n));
4429
4430   if (TREE_CODE (n) == ADDR_EXPR)
4431     n = TREE_OPERAND (n, 0);
4432   if (TREE_CODE (n) == COMPONENT_REF)
4433     n = TREE_OPERAND (n, 1);
4434   if (TREE_CODE (n) == OFFSET_REF)
4435     n = TREE_OPERAND (n, 1);
4436   while (TREE_CODE (n) == TREE_LIST)
4437     n = TREE_VALUE (n);
4438   if (TREE_CODE (n) == BASELINK)
4439     n = BASELINK_FUNCTIONS (n);
4440
4441   if (TREE_CODE (n) == FUNCTION_DECL)
4442     return arg_assoc_type (k, TREE_TYPE (n));
4443   if (TREE_CODE (n) == TEMPLATE_ID_EXPR)
4444     {
4445       /* [basic.lookup.koenig]
4446
4447          If T is a template-id, its associated namespaces and classes
4448          are the namespace in which the template is defined; for
4449          member templates, the member template's class...  */
4450       tree template = TREE_OPERAND (n, 0);
4451       tree args = TREE_OPERAND (n, 1);
4452       tree ctx;
4453       int ix;
4454
4455       if (TREE_CODE (template) == COMPONENT_REF)
4456         template = TREE_OPERAND (template, 1);
4457       
4458       /* First, the template.  There may actually be more than one if
4459          this is an overloaded function template.  But, in that case,
4460          we only need the first; all the functions will be in the same
4461          namespace.  */
4462       template = OVL_CURRENT (template);
4463
4464       ctx = CP_DECL_CONTEXT (template);
4465        
4466       if (TREE_CODE (ctx) == NAMESPACE_DECL)
4467         {
4468           if (arg_assoc_namespace (k, ctx) == 1)
4469             return true;
4470         }
4471       /* It must be a member template.  */
4472       else if (arg_assoc_class (k, ctx) == 1)
4473         return true;
4474
4475       /* Now the arguments.  */
4476       for (ix = TREE_VEC_LENGTH (args); ix--;)
4477         if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, ix)) == 1)
4478           return true;
4479     }
4480   else if (TREE_CODE (n) == OVERLOAD)
4481     {
4482       for (; n; n = OVL_CHAIN (n))
4483         if (arg_assoc_type (k, TREE_TYPE (OVL_FUNCTION (n))))
4484           return true;
4485     }
4486
4487   return false;
4488 }
4489
4490 /* Performs Koenig lookup depending on arguments, where fns
4491    are the functions found in normal lookup.  */
4492
4493 tree
4494 lookup_arg_dependent (tree name, tree fns, tree args)
4495 {
4496   struct arg_lookup k;
4497   tree fn = NULL_TREE;
4498
4499   timevar_push (TV_NAME_LOOKUP);
4500   k.name = name;
4501   k.functions = fns;
4502   k.classes = NULL_TREE;
4503
4504   /* We've already looked at some namespaces during normal unqualified
4505      lookup -- but we don't know exactly which ones.  If the functions
4506      we found were brought into the current namespace via a using
4507      declaration, we have not really checked the namespace from which
4508      they came.  Therefore, we check all namespaces here -- unless the
4509      function we have is from the current namespace.  Even then, we
4510      must check all namespaces if the function is a local
4511      declaration; any other declarations present at namespace scope
4512      should be visible during argument-dependent lookup.  */
4513   if (fns)
4514     fn = OVL_CURRENT (fns);
4515   if (fn && TREE_CODE (fn) == FUNCTION_DECL 
4516       && (CP_DECL_CONTEXT (fn) != current_decl_namespace ()
4517           || DECL_LOCAL_FUNCTION_P (fn)))
4518     k.namespaces = NULL_TREE;
4519   else
4520     /* Setting NAMESPACES is purely an optimization; it prevents
4521        adding functions which are already in FNS.  Adding them would
4522        be safe -- "joust" will eliminate the duplicates -- but
4523        wasteful.  */
4524     k.namespaces = build_tree_list (current_decl_namespace (), NULL_TREE);
4525
4526   arg_assoc_args (&k, args);
4527   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, k.functions);
4528 }
4529
4530 /* Add namespace to using_directives. Return NULL_TREE if nothing was
4531    changed (i.e. there was already a directive), or the fresh
4532    TREE_LIST otherwise.  */
4533
4534 static tree
4535 push_using_directive (tree used)
4536 {
4537   tree ud = current_binding_level->using_directives;
4538   tree iter, ancestor;
4539
4540   timevar_push (TV_NAME_LOOKUP);
4541   /* Check if we already have this.  */
4542   if (purpose_member (used, ud) != NULL_TREE)
4543     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4544
4545   ancestor = namespace_ancestor (current_decl_namespace (), used);
4546   ud = current_binding_level->using_directives;
4547   ud = tree_cons (used, ancestor, ud);
4548   current_binding_level->using_directives = ud;
4549
4550   /* Recursively add all namespaces used.  */
4551   for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
4552     push_using_directive (TREE_PURPOSE (iter));
4553
4554   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ud);
4555 }
4556
4557 /* The type TYPE is being declared.  If it is a class template, or a
4558    specialization of a class template, do any processing required and
4559    perform error-checking.  If IS_FRIEND is nonzero, this TYPE is
4560    being declared a friend.  B is the binding level at which this TYPE
4561    should be bound.
4562
4563    Returns the TYPE_DECL for TYPE, which may have been altered by this
4564    processing.  */
4565
4566 static tree
4567 maybe_process_template_type_declaration (tree type, int globalize,
4568                                          cxx_scope *b)
4569 {
4570   tree decl = TYPE_NAME (type);
4571
4572   if (processing_template_parmlist)
4573     /* You can't declare a new template type in a template parameter
4574        list.  But, you can declare a non-template type:
4575
4576          template <class A*> struct S;
4577
4578        is a forward-declaration of `A'.  */
4579     ;
4580   else
4581     {
4582       maybe_check_template_type (type);
4583
4584       my_friendly_assert (IS_AGGR_TYPE (type)
4585                           || TREE_CODE (type) == ENUMERAL_TYPE, 0);
4586
4587
4588       if (processing_template_decl)
4589         {
4590           /* This may change after the call to
4591              push_template_decl_real, but we want the original value.  */
4592           tree name = DECL_NAME (decl);
4593
4594           decl = push_template_decl_real (decl, globalize);
4595           /* If the current binding level is the binding level for the
4596              template parameters (see the comment in
4597              begin_template_parm_list) and the enclosing level is a class
4598              scope, and we're not looking at a friend, push the
4599              declaration of the member class into the class scope.  In the
4600              friend case, push_template_decl will already have put the
4601              friend into global scope, if appropriate.  */
4602           if (TREE_CODE (type) != ENUMERAL_TYPE
4603               && !globalize && b->kind == sk_template_parms
4604               && b->level_chain->kind == sk_class)
4605             {
4606               finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
4607               /* Put this UDT in the table of UDTs for the class, since
4608                  that won't happen below because B is not the class
4609                  binding level, but is instead the pseudo-global level.  */
4610               if (b->level_chain->type_decls == NULL)
4611                 b->level_chain->type_decls =
4612                   binding_table_new (SCOPE_DEFAULT_HT_SIZE);
4613               binding_table_insert (b->level_chain->type_decls, name, type);
4614               if (!COMPLETE_TYPE_P (current_class_type))
4615                 {
4616                   maybe_add_class_template_decl_list (current_class_type,
4617                                                       type, /*friend_p=*/0);
4618                   CLASSTYPE_NESTED_UTDS (current_class_type) =
4619                     b->level_chain->type_decls;
4620                 }
4621             }
4622         }
4623     }
4624
4625   return decl;
4626 }
4627
4628 /* Push a tag name NAME for struct/class/union/enum type TYPE.
4629    Normally put it into the inner-most non-sk_cleanup scope,
4630    but if GLOBALIZE is true, put it in the inner-most non-class scope.
4631    The latter is needed for implicit declarations.  */
4632
4633 void
4634 pushtag (tree name, tree type, int globalize)
4635 {
4636   struct cp_binding_level *b;
4637
4638   timevar_push (TV_NAME_LOOKUP);
4639   b = current_binding_level;
4640   while (/* Cleanup scopes are not scopes from the point of view of
4641             the language.  */
4642          b->kind == sk_cleanup
4643          /* Neither are the scopes used to hold template parameters
4644             for an explicit specialization.  For an ordinary template
4645             declaration, these scopes are not scopes from the point of
4646             view of the language -- but we need a place to stash
4647             things that will go in the containing namespace when the
4648             template is instantiated.  */
4649          || (b->kind == sk_template_parms && b->explicit_spec_p)
4650          || (b->kind == sk_class
4651              && (globalize
4652                  /* We may be defining a new type in the initializer
4653                     of a static member variable. We allow this when
4654                     not pedantic, and it is particularly useful for
4655                     type punning via an anonymous union.  */
4656                  || COMPLETE_TYPE_P (b->this_entity))))
4657     b = b->level_chain;
4658
4659   if (b->type_decls == NULL)
4660     b->type_decls = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
4661   binding_table_insert (b->type_decls, name, type);
4662
4663   if (name)
4664     {
4665       /* Do C++ gratuitous typedefing.  */
4666       if (IDENTIFIER_TYPE_VALUE (name) != type)
4667         {
4668           tree d = NULL_TREE;
4669           int in_class = 0;
4670           tree context = TYPE_CONTEXT (type);
4671
4672           if (! context)
4673             {
4674               tree cs = current_scope ();
4675
4676               if (! globalize)
4677                 context = cs;
4678               else if (cs != NULL_TREE && TYPE_P (cs))
4679                 /* When declaring a friend class of a local class, we want
4680                    to inject the newly named class into the scope
4681                    containing the local class, not the namespace scope.  */
4682                 context = decl_function_context (get_type_decl (cs));
4683             }
4684           if (!context)
4685             context = current_namespace;
4686
4687           if (b->kind == sk_class
4688               || (b->kind == sk_template_parms 
4689                   && b->level_chain->kind == sk_class))
4690             in_class = 1;
4691
4692           if (current_lang_name == lang_name_java)
4693             TYPE_FOR_JAVA (type) = 1;
4694
4695           d = create_implicit_typedef (name, type);
4696           DECL_CONTEXT (d) = FROB_CONTEXT (context);
4697           if (! in_class)
4698             set_identifier_type_value_with_scope (name, d, b);
4699
4700           d = maybe_process_template_type_declaration (type,
4701                                                        globalize, b);
4702
4703           if (b->kind == sk_class)
4704             {
4705               if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
4706                 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
4707                    class.  But if it's a member template class, we
4708                    want the TEMPLATE_DECL, not the TYPE_DECL, so this
4709                    is done later.  */
4710                 finish_member_declaration (d);
4711               else
4712                 pushdecl_class_level (d);
4713             }
4714           else
4715             d = pushdecl_with_scope (d, b);
4716
4717           /* FIXME what if it gets a name from typedef?  */
4718           if (ANON_AGGRNAME_P (name))
4719             DECL_IGNORED_P (d) = 1;
4720
4721           TYPE_CONTEXT (type) = DECL_CONTEXT (d);
4722
4723           /* If this is a local class, keep track of it.  We need this
4724              information for name-mangling, and so that it is possible to find
4725              all function definitions in a translation unit in a convenient
4726              way.  (It's otherwise tricky to find a member function definition
4727              it's only pointed to from within a local class.)  */
4728           if (TYPE_CONTEXT (type)
4729               && TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL
4730               && !processing_template_decl)
4731             VARRAY_PUSH_TREE (local_classes, type);
4732         }
4733       if (b->kind == sk_class
4734           && !COMPLETE_TYPE_P (current_class_type))
4735         {
4736           maybe_add_class_template_decl_list (current_class_type,
4737                                               type, /*friend_p=*/0);
4738           CLASSTYPE_NESTED_UTDS (current_class_type) = b->type_decls;
4739         }
4740     }
4741
4742   if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL)
4743     /* Use the canonical TYPE_DECL for this node.  */
4744     TYPE_STUB_DECL (type) = TYPE_NAME (type);
4745   else
4746     {
4747       /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE
4748          will be the tagged type we just added to the current
4749          binding level.  This fake NULL-named TYPE_DECL node helps
4750          dwarfout.c to know when it needs to output a
4751          representation of a tagged type, and it also gives us a
4752          convenient place to record the "scope start" address for
4753          the tagged type.  */
4754
4755       tree d = build_decl (TYPE_DECL, NULL_TREE, type);
4756       TYPE_STUB_DECL (type) = pushdecl_with_scope (d, b);
4757     }
4758   timevar_pop (TV_NAME_LOOKUP);
4759 }
4760 \f
4761 /* Subroutines for reverting temporarily to top-level for instantiation
4762    of templates and such.  We actually need to clear out the class- and
4763    local-value slots of all identifiers, so that only the global values
4764    are at all visible.  Simply setting current_binding_level to the global
4765    scope isn't enough, because more binding levels may be pushed.  */
4766 struct saved_scope *scope_chain;
4767
4768 /* If ID has not already been marked, add an appropriate binding to
4769    *OLD_BINDINGS.  */
4770
4771 static void
4772 store_binding (tree id, VEC(cxx_saved_binding) **old_bindings)
4773 {
4774   cxx_saved_binding *saved;
4775
4776   if (!id || !IDENTIFIER_BINDING (id))
4777     return;
4778
4779   if (IDENTIFIER_MARKED (id))
4780     return;
4781   
4782   IDENTIFIER_MARKED (id) = 1;
4783
4784   saved = VEC_safe_push (cxx_saved_binding, *old_bindings, NULL);
4785   saved->identifier = id;
4786   saved->binding = IDENTIFIER_BINDING (id);
4787   saved->real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
4788   IDENTIFIER_BINDING (id) = NULL;
4789 }
4790
4791 static void
4792 store_bindings (tree names, VEC(cxx_saved_binding) **old_bindings)
4793 {
4794   tree t;
4795
4796   timevar_push (TV_NAME_LOOKUP);
4797   for (t = names; t; t = TREE_CHAIN (t))
4798     {
4799       tree id;
4800
4801       if (TREE_CODE (t) == TREE_LIST)
4802         id = TREE_PURPOSE (t);
4803       else
4804         id = DECL_NAME (t);
4805
4806       store_binding (id, old_bindings);
4807     }
4808   timevar_pop (TV_NAME_LOOKUP);
4809 }
4810
4811 /* Like store_bindings, but NAMES is a vector of cp_class_binding
4812    objects, rather than a TREE_LIST.  */
4813
4814 static void
4815 store_class_bindings (VEC(cp_class_binding) *names, 
4816                       VEC(cxx_saved_binding) **old_bindings)
4817 {
4818   size_t i;
4819   cp_class_binding *cb;
4820
4821   timevar_push (TV_NAME_LOOKUP);
4822   for (i = 0; 
4823        (cb = VEC_iterate(cp_class_binding, names, i));
4824        ++i)
4825     store_binding (cb->identifier, old_bindings);
4826   timevar_pop (TV_NAME_LOOKUP);
4827 }
4828
4829 void
4830 push_to_top_level (void)
4831 {
4832   struct saved_scope *s;
4833   struct cp_binding_level *b;
4834   cxx_saved_binding *sb;
4835   size_t i;
4836   int need_pop;
4837
4838   timevar_push (TV_NAME_LOOKUP);
4839   s = ggc_alloc_cleared (sizeof (struct saved_scope));
4840
4841   b = scope_chain ? current_binding_level : 0;
4842
4843   /* If we're in the middle of some function, save our state.  */
4844   if (cfun)
4845     {
4846       need_pop = 1;
4847       push_function_context_to (NULL_TREE);
4848     }
4849   else
4850     need_pop = 0;
4851
4852   if (scope_chain && previous_class_level)
4853     store_class_bindings (previous_class_level->class_shadowed,
4854                           &s->old_bindings);
4855
4856   /* Have to include the global scope, because class-scope decls
4857      aren't listed anywhere useful.  */
4858   for (; b; b = b->level_chain)
4859     {
4860       tree t;
4861
4862       /* Template IDs are inserted into the global level. If they were
4863          inserted into namespace level, finish_file wouldn't find them
4864          when doing pending instantiations. Therefore, don't stop at
4865          namespace level, but continue until :: .  */
4866       if (global_scope_p (b))
4867         break;
4868
4869       store_bindings (b->names, &s->old_bindings);
4870       /* We also need to check class_shadowed to save class-level type
4871          bindings, since pushclass doesn't fill in b->names.  */
4872       if (b->kind == sk_class)
4873         store_class_bindings (b->class_shadowed, &s->old_bindings);
4874
4875       /* Unwind type-value slots back to top level.  */
4876       for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
4877         SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
4878     }
4879
4880   for (i = 0;
4881        (sb = VEC_iterate (cxx_saved_binding, s->old_bindings, i));
4882        ++i)
4883     IDENTIFIER_MARKED (sb->identifier) = 0;
4884
4885   s->prev = scope_chain;
4886   s->bindings = b;
4887   s->need_pop_function_context = need_pop;
4888   s->function_decl = current_function_decl;
4889
4890   scope_chain = s;
4891   current_function_decl = NULL_TREE;
4892   VARRAY_TREE_INIT (current_lang_base, 10, "current_lang_base");
4893   current_lang_name = lang_name_cplusplus;
4894   current_namespace = global_namespace;
4895   timevar_pop (TV_NAME_LOOKUP);
4896 }
4897
4898 void
4899 pop_from_top_level (void)
4900 {
4901   struct saved_scope *s = scope_chain;
4902   cxx_saved_binding *saved;
4903   size_t i;
4904
4905   timevar_push (TV_NAME_LOOKUP); 
4906   /* Clear out class-level bindings cache.  */
4907   if (previous_class_level)
4908     invalidate_class_lookup_cache ();
4909
4910   current_lang_base = 0;
4911
4912   scope_chain = s->prev;
4913   for (i = 0; 
4914        (saved = VEC_iterate (cxx_saved_binding, s->old_bindings, i));
4915        ++i)
4916     {
4917       tree id = saved->identifier;
4918
4919       IDENTIFIER_BINDING (id) = saved->binding;
4920       SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
4921     }
4922
4923   /* If we were in the middle of compiling a function, restore our
4924      state.  */
4925   if (s->need_pop_function_context)
4926     pop_function_context_from (NULL_TREE);
4927   current_function_decl = s->function_decl;
4928   timevar_pop (TV_NAME_LOOKUP);
4929 }
4930
4931 /* Pop off extraneous binding levels left over due to syntax errors.
4932
4933    We don't pop past namespaces, as they might be valid.  */
4934
4935 void
4936 pop_everything (void)
4937 {
4938   if (ENABLE_SCOPE_CHECKING)
4939     verbatim ("XXX entering pop_everything ()\n");
4940   while (!toplevel_bindings_p ())
4941     {
4942       if (current_binding_level->kind == sk_class)
4943         pop_nested_class ();
4944       else
4945         poplevel (0, 0, 0);
4946     }
4947   if (ENABLE_SCOPE_CHECKING)
4948     verbatim ("XXX leaving pop_everything ()\n");
4949 }
4950
4951 /* Emit debugging information for using declarations and directives.
4952    If input tree is overloaded fn then emit debug info for all 
4953    candidates.  */
4954
4955 static void
4956 cp_emit_debug_info_for_using (tree t, tree context)
4957 {
4958   /* Ignore this FUNCTION_DECL if it refers to a builtin declaration 
4959      of a builtin function.  */
4960   if (TREE_CODE (t) == FUNCTION_DECL 
4961       && DECL_EXTERNAL (t)
4962       && DECL_BUILT_IN (t))
4963     return;
4964
4965   /* Do not supply context to imported_module_or_decl, if
4966      it is a global namespace.  */
4967   if (context == global_namespace)
4968     context = NULL_TREE;
4969   
4970   if (BASELINK_P (t))
4971     t = BASELINK_FUNCTIONS (t);
4972   
4973   /* FIXME: Handle TEMPLATE_DECLs.  */
4974   for (t = OVL_CURRENT (t); t; t = OVL_NEXT (t))
4975     if (TREE_CODE (t) != TEMPLATE_DECL)
4976       (*debug_hooks->imported_module_or_decl) (t, context);
4977   }
4978
4979 #include "gt-cp-name-lookup.h"