OSDN Git Service

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