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