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