OSDN Git Service

a14389f12565443f875894d4f8651a3566aae1d6
[pf3gnuchains/gcc-fork.git] / gcc / cp / decl.c
1 /* Process declarations and variables for C++ compiler.
2    Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3    2001, 2002, 2003  Free Software Foundation, Inc.
4    Contributed by Michael Tiemann (tiemann@cygnus.com)
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING.  If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.  */
22
23
24 /* Process declarations and symbol lookup for C++ front end.
25    Also constructs types; the standard scalar types at initialization,
26    and structure, union, array and enum types when they are declared.  */
27
28 /* ??? not all decl nodes are given the most useful possible
29    line numbers.  For example, the CONST_DECLs for enum values.  */
30
31 #include "config.h"
32 #include "system.h"
33 #include "coretypes.h"
34 #include "tm.h"
35 #include "tree.h"
36 #include "rtl.h"
37 #include "expr.h"
38 #include "flags.h"
39 #include "cp-tree.h"
40 #include "tree-inline.h"
41 #include "decl.h"
42 #include "lex.h"
43 #include "output.h"
44 #include "except.h"
45 #include "toplev.h"
46 #include "hashtab.h"
47 #include "tm_p.h"
48 #include "target.h"
49 #include "c-common.h"
50 #include "c-pragma.h"
51 #include "diagnostic.h"
52 #include "debug.h"
53 #include "timevar.h"
54
55 static tree grokparms (tree);
56 static const char *redeclaration_error_message (tree, tree);
57
58 static void push_binding_level (struct cp_binding_level *, int,
59                                 int);
60 static void pop_binding_level (void);
61 static void suspend_binding_level (void);
62 static void resume_binding_level (struct cp_binding_level *);
63 static struct cp_binding_level *make_binding_level (void);
64 static void declare_namespace_level (void);
65 static int decl_jump_unsafe (tree);
66 static void storedecls (tree);
67 static void require_complete_types_for_parms (tree);
68 static int ambi_op_p (enum tree_code);
69 static int unary_op_p (enum tree_code);
70 static cxx_saved_binding *store_bindings (tree, cxx_saved_binding *);
71 static tree lookup_tag_reverse (tree, tree);
72 static void push_local_name (tree);
73 static void warn_extern_redeclared_static (tree, tree);
74 static tree grok_reference_init (tree, tree, tree);
75 static tree grokfndecl (tree, tree, tree, tree, int,
76                         enum overload_flags, tree,
77                         tree, int, int, int, int, int, int, tree);
78 static tree grokvardecl (tree, tree, RID_BIT_TYPE *, int, int, tree);
79 static tree follow_tag_typedef (tree);
80 static tree lookup_tag (enum tree_code, tree,
81                         struct cp_binding_level *, int);
82 static void set_identifier_type_value_with_scope
83         (tree, tree, struct cp_binding_level *);
84 static void record_unknown_type (tree, const char *);
85 static tree builtin_function_1 (const char *, tree, tree, int,
86                                 enum built_in_class, const char *,
87                                 tree);
88 static tree build_library_fn_1 (tree, enum tree_code, tree);
89 static int member_function_or_else (tree, tree, enum overload_flags);
90 static void bad_specifiers (tree, const char *, int, int, int, int,
91                             int);
92 static tree maybe_process_template_type_declaration 
93         (tree, int, struct cp_binding_level*);
94 static void check_for_uninitialized_const_var (tree);
95 static hashval_t typename_hash (const void *);
96 static int typename_compare (const void *, const void *);
97 static void push_binding (tree, tree, struct cp_binding_level*);
98 static int add_binding (tree, tree);
99 static void pop_binding (tree, tree);
100 static tree local_variable_p_walkfn (tree *, int *, void *);
101 static tree select_decl (cxx_binding *, int);
102 static int lookup_flags (int, int);
103 static tree qualify_lookup (tree, int);
104 static tree record_builtin_java_type (const char *, int);
105 static const char *tag_name (enum tag_types code);
106 static void find_class_binding_level (void);
107 static struct cp_binding_level *innermost_nonclass_level (void);
108 static int walk_namespaces_r (tree, walk_namespaces_fn, void *);
109 static int walk_globals_r (tree, void*);
110 static int walk_vtables_r (tree, void*);
111 static void add_decl_to_level (tree, struct cp_binding_level *);
112 static tree make_label_decl (tree, int);
113 static void use_label (tree);
114 static void check_previous_goto_1 (tree, struct cp_binding_level *, tree,
115                                    const char *, int);
116 static void check_previous_goto (struct named_label_use_list *);
117 static void check_switch_goto (struct cp_binding_level *);
118 static void check_previous_gotos (tree);
119 static void pop_label (tree, tree);
120 static void pop_labels (tree);
121 static void maybe_deduce_size_from_array_init (tree, tree);
122 static void layout_var_decl (tree);
123 static void maybe_commonize_var (tree);
124 static tree check_initializer (tree, tree, int);
125 static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
126 static void save_function_data (tree);
127 static void check_function_type (tree, tree);
128 static void begin_constructor_body (void);
129 static void finish_constructor_body (void);
130 static void begin_destructor_body (void);
131 static void finish_destructor_body (void);
132 static tree create_array_type_for_decl (tree, tree, tree);
133 static tree get_atexit_node (void);
134 static tree get_dso_handle_node (void);
135 static tree start_cleanup_fn (void);
136 static void end_cleanup_fn (void);
137 static tree cp_make_fname_decl (tree, int);
138 static void initialize_predefined_identifiers (void);
139 static tree check_special_function_return_type 
140         (special_function_kind, tree, tree);
141 static tree push_cp_library_fn (enum tree_code, tree);
142 static tree build_cp_library_fn (tree, enum tree_code, tree);
143 static void store_parm_decls (tree);
144 static int cp_missing_noreturn_ok_p (tree);
145 static void initialize_local_var (tree, tree);
146 static void expand_static_init (tree, tree);
147 static tree next_initializable_field (tree);
148 static tree reshape_init (tree, tree *);
149 static tree build_typename_type (tree, tree, tree);
150
151 #if defined (DEBUG_BINDING_LEVELS)
152 static void indent (void);
153 #endif
154
155 /* Erroneous argument lists can use this *IFF* they do not modify it.  */
156 tree error_mark_list;
157
158 /* The following symbols are subsumed in the cp_global_trees array, and
159    listed here individually for documentation purposes.
160
161    C++ extensions
162         tree wchar_decl_node;
163
164         tree vtable_entry_type;
165         tree delta_type_node;
166         tree __t_desc_type_node;
167         tree ti_desc_type_node;
168         tree bltn_desc_type_node, ptr_desc_type_node;
169         tree ary_desc_type_node, func_desc_type_node, enum_desc_type_node;
170         tree class_desc_type_node, si_class_desc_type_node, vmi_class_desc_type_node;
171         tree ptm_desc_type_node;
172         tree base_desc_type_node;
173
174         tree class_type_node, record_type_node, union_type_node, enum_type_node;
175         tree unknown_type_node;
176
177    Array type `vtable_entry_type[]'
178
179         tree vtbl_type_node;
180         tree vtbl_ptr_type_node;
181
182    Namespaces,
183
184         tree std_node;
185         tree abi_node;
186
187    A FUNCTION_DECL which can call `abort'.  Not necessarily the
188    one that the user will declare, but sufficient to be called
189    by routines that want to abort the program.
190
191         tree abort_fndecl;
192
193    The FUNCTION_DECL for the default `::operator delete'.
194
195         tree global_delete_fndecl;
196
197    Used by RTTI
198         tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
199         tree tinfo_var_id;
200
201 */
202
203 tree cp_global_trees[CPTI_MAX];
204
205 /* Indicates that there is a type value in some namespace, although
206    that is not necessarily in scope at the moment.  */
207
208 static GTY(()) tree global_type_node;
209
210 /* Used only for jumps to as-yet undefined labels, since jumps to
211    defined labels can have their validity checked immediately.  */
212
213 struct named_label_use_list GTY(())
214 {
215   struct cp_binding_level *binding_level;
216   tree names_in_scope;
217   tree label_decl;
218   const char *filename_o_goto;
219   int lineno_o_goto;
220   struct named_label_use_list *next;
221 };
222
223 #define named_label_uses cp_function_chain->x_named_label_uses
224
225 #define local_names cp_function_chain->x_local_names
226
227 /* A list of objects which have constructors or destructors
228    which reside in the global scope.  The decl is stored in
229    the TREE_VALUE slot and the initializer is stored
230    in the TREE_PURPOSE slot.  */
231 tree static_aggregates;
232
233 /* -- end of C++ */
234
235 /* A node for the integer constants 2, and 3.  */
236
237 tree integer_two_node, integer_three_node;
238
239 /* Similar, for last_function_parm_tags.  */
240 tree last_function_parms;
241
242 /* A list of all LABEL_DECLs in the function that have names.  Here so
243    we can clear out their names' definitions at the end of the
244    function, and so we can check the validity of jumps to these labels.  */
245
246 struct named_label_list GTY(())
247 {
248   struct cp_binding_level *binding_level;
249   tree names_in_scope;
250   tree old_value;
251   tree label_decl;
252   tree bad_decls;
253   struct named_label_list *next;
254   unsigned int in_try_scope : 1;
255   unsigned int in_catch_scope : 1;
256 };
257
258 #define named_labels cp_function_chain->x_named_labels
259 \f
260 /* The name of the anonymous namespace, throughout this translation
261    unit.  */
262 tree anonymous_namespace_name;
263
264 /* The number of function bodies which we are currently processing.
265    (Zero if we are at namespace scope, one inside the body of a
266    function, two inside the body of a function in a local class, etc.)  */
267 int function_depth;
268
269 /* States indicating how grokdeclarator() should handle declspecs marked
270    with __attribute__((deprecated)).  An object declared as
271    __attribute__((deprecated)) suppresses warnings of uses of other
272    deprecated items.  */
273    
274 enum deprecated_states {
275   DEPRECATED_NORMAL,
276   DEPRECATED_SUPPRESS
277 };
278
279 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
280
281 /* Set by add_implicitly_declared_members() to keep those members from
282    being flagged as deprecated or reported as using deprecated
283    types.  */
284 int adding_implicit_members = 0;
285
286 /* True if a declaration with an `extern' linkage specifier is being
287    processed.  */
288 bool have_extern_spec;
289
290 \f
291 /* For each binding contour we allocate a binding_level structure
292    which records the names defined in that contour.
293    Contours include:
294     0) the global one
295     1) one for each function definition,
296        where internal declarations of the parameters appear.
297     2) one for each compound statement,
298        to record its declarations.
299
300    The current meaning of a name can be found by searching the levels
301    from the current one out to the global one.
302
303    Off to the side, may be the class_binding_level.  This exists only
304    to catch class-local declarations.  It is otherwise nonexistent.
305
306    Also there may be binding levels that catch cleanups that must be
307    run when exceptions occur.  Thus, to see whether a name is bound in
308    the current scope, it is not enough to look in the
309    CURRENT_BINDING_LEVEL.  You should use lookup_name_current_level
310    instead.  */
311
312 /* Note that the information in the `names' component of the global contour
313    is duplicated in the IDENTIFIER_GLOBAL_VALUEs of all identifiers.  */
314
315 struct cp_binding_level GTY(())
316   {
317     /* A chain of _DECL nodes for all variables, constants, functions,
318        and typedef types.  These are in the reverse of the order
319        supplied.  There may be OVERLOADs on this list, too, but they
320        are wrapped in TREE_LISTs; the TREE_VALUE is the OVERLOAD.  */
321     tree names;
322
323     /* Count of elements in names chain.  */
324     size_t names_size;
325
326     /* A chain of NAMESPACE_DECL nodes.  */
327     tree namespaces;
328
329     /* An array of static functions and variables (for namespaces only) */
330     varray_type static_decls;
331
332     /* A chain of VTABLE_DECL nodes.  */
333     tree vtables; 
334
335     /* A list of structure, union and enum definitions, for looking up
336        tag names.
337        It is a chain of TREE_LIST nodes, each of whose TREE_PURPOSE is a name,
338        or NULL_TREE; and whose TREE_VALUE is a RECORD_TYPE, UNION_TYPE,
339        or ENUMERAL_TYPE node.
340
341        C++: the TREE_VALUE nodes can be simple types for
342        component_bindings.  */
343     tree tags;
344
345     /* A list of USING_DECL nodes.  */
346     tree usings;
347
348     /* A list of used namespaces. PURPOSE is the namespace,
349        VALUE the common ancestor with this binding_level's namespace.  */
350     tree using_directives;
351
352     /* If this binding level is the binding level for a class, then
353        class_shadowed is a TREE_LIST.  The TREE_PURPOSE of each node
354        is the name of an entity bound in the class.  The TREE_TYPE is
355        the DECL bound by this name in the class.  */
356     tree class_shadowed;
357
358     /* Similar to class_shadowed, but for IDENTIFIER_TYPE_VALUE, and
359        is used for all binding levels. In addition the TREE_VALUE is the
360        IDENTIFIER_TYPE_VALUE before we entered the class.  */
361     tree type_shadowed;
362
363     /* A TREE_LIST.  Each TREE_VALUE is the LABEL_DECL for a local
364        label in this scope.  The TREE_PURPOSE is the previous value of
365        the IDENTIFIER_LABEL VALUE.  */
366     tree shadowed_labels;
367
368     /* For each level (except not the global one),
369        a chain of BLOCK nodes for all the levels
370        that were entered and exited one level down.  */
371     tree blocks;
372
373     /* The _TYPE node for this level, if parm_flag == 2.  */
374     tree this_class;
375
376     /* The binding level which this one is contained in (inherits from).  */
377     struct cp_binding_level *level_chain;
378
379     /* List of VAR_DECLS saved from a previous for statement.
380        These would be dead in ISO-conforming code, but might
381        be referenced in ARM-era code.  These are stored in a
382        TREE_LIST; the TREE_VALUE is the actual declaration.  */
383     tree dead_vars_from_for;
384
385     /* 1 for the level that holds the parameters of a function.
386        2 for the level that holds a class declaration.  */
387     unsigned parm_flag : 2;
388
389     /* 1 means make a BLOCK for this level regardless of all else.
390        2 for temporary binding contours created by the compiler.  */
391     unsigned keep : 2;
392
393     /* Nonzero if this level "doesn't exist" for tags.  */
394     unsigned tag_transparent : 1;
395
396     /* Nonzero if this level can safely have additional
397        cleanup-needing variables added to it.  */
398     unsigned more_cleanups_ok : 1;
399     unsigned have_cleanups : 1;
400
401     /* Nonzero if this scope is for storing the decls for template
402        parameters and generic decls; these decls will be discarded and
403        replaced with a TEMPLATE_DECL.  */
404     unsigned template_parms_p : 1;
405
406     /* Nonzero if this scope corresponds to the `<>' in a
407        `template <>' clause.  Whenever this flag is set,
408        TEMPLATE_PARMS_P will be set as well.  */
409     unsigned template_spec_p : 1;
410
411     /* This is set for a namespace binding level.  */
412     unsigned namespace_p : 1;
413
414     /* True if this level is that of a for-statement where we need to
415        worry about ambiguous (ARM or ISO) scope rules.  */
416     unsigned is_for_scope : 1;
417
418     /* True if this level corresponds to a TRY block.  Currently this
419        information is only available while building the tree structure.  */
420     unsigned is_try_scope : 1;
421
422     /* True if this level corresponds to a CATCH block.  Currently this
423        information is only available while building the tree structure.  */
424     unsigned is_catch_scope : 1;
425
426     /* Three bits left for this word.  */
427
428     /* Binding depth at which this level began.  */
429     unsigned binding_depth;
430   };
431
432 #define NULL_BINDING_LEVEL ((struct cp_binding_level *) NULL)
433
434 /* The binding level currently in effect.  */
435
436 #define current_binding_level                   \
437   (*(cfun && cp_function_chain->bindings        \
438    ? &cp_function_chain->bindings               \
439    : &scope_chain->bindings))
440
441 /* The binding level of the current class, if any.  */
442
443 #define class_binding_level scope_chain->class_bindings
444
445 /* A chain of binding_level structures awaiting reuse.  */
446
447 static GTY((deletable (""))) struct cp_binding_level *free_binding_level;
448
449 /* The outermost binding level, for names of file scope.
450    This is created when the compiler is started and exists
451    through the entire run.  */
452
453 static GTY(()) struct cp_binding_level *global_binding_level;
454
455 /* Nonzero means unconditionally make a BLOCK for the next level pushed.  */
456
457 static int keep_next_level_flag;
458
459 /* A TREE_LIST of VAR_DECLs.  The TREE_PURPOSE is a RECORD_TYPE or
460    UNION_TYPE; the TREE_VALUE is a VAR_DECL with that type.  At the
461    time the VAR_DECL was declared, the type was incomplete.  */
462
463 static GTY(()) tree incomplete_vars;
464
465 #if defined(DEBUG_BINDING_LEVELS)
466 static int binding_depth = 0;
467 static int is_class_level = 0;
468
469 static void
470 indent (void)
471 {
472   register unsigned i;
473
474   for (i = 0; i < binding_depth*2; i++)
475     putc (' ', stderr);
476 }
477 #endif /* defined(DEBUG_BINDING_LEVELS) */
478
479 static tree pushdecl_with_scope (tree, struct cp_binding_level *);
480
481 static void
482 push_binding_level (struct cp_binding_level *newlevel, 
483                     int tag_transparent, 
484                     int keep)
485 {
486   /* Add this level to the front of the chain (stack) of levels that
487      are active.  */
488   memset ((char*) newlevel, 0, sizeof (struct cp_binding_level));
489   newlevel->level_chain = current_binding_level;
490   current_binding_level = newlevel;
491   newlevel->tag_transparent = tag_transparent;
492   newlevel->more_cleanups_ok = 1;
493
494   newlevel->keep = keep;
495 #if defined(DEBUG_BINDING_LEVELS)
496   newlevel->binding_depth = binding_depth;
497   indent ();
498   fprintf (stderr, "push %s level 0x%08x line %d\n",
499            (is_class_level) ? "class" : "block", newlevel, lineno);
500   is_class_level = 0;
501   binding_depth++;
502 #endif /* defined(DEBUG_BINDING_LEVELS) */
503 }
504
505 /* Find the innermost enclosing class scope, and reset
506    CLASS_BINDING_LEVEL appropriately.  */
507
508 static void
509 find_class_binding_level (void)
510 {
511   struct cp_binding_level *level = current_binding_level;
512
513   while (level && level->parm_flag != 2)
514     level = level->level_chain;
515   if (level && level->parm_flag == 2)
516     class_binding_level = level;
517   else
518     class_binding_level = 0;
519 }
520
521 static void
522 pop_binding_level (void)
523 {
524   if (global_binding_level)
525     {
526       /* Cannot pop a level, if there are none left to pop.  */
527       if (current_binding_level == global_binding_level)
528         abort ();
529     }
530   /* Pop the current level, and free the structure for reuse.  */
531 #if defined(DEBUG_BINDING_LEVELS)
532   binding_depth--;
533   indent ();
534   fprintf (stderr, "pop  %s level 0x%08x line %d\n",
535           (is_class_level) ? "class" : "block",
536           current_binding_level, lineno);
537   if (is_class_level != (current_binding_level == class_binding_level))
538     {
539       indent ();
540       fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
541     }
542   is_class_level = 0;
543 #endif /* defined(DEBUG_BINDING_LEVELS) */
544   {
545     register struct cp_binding_level *level = current_binding_level;
546     current_binding_level = current_binding_level->level_chain;
547     level->level_chain = free_binding_level;
548 #if 0 /* defined(DEBUG_BINDING_LEVELS) */
549     if (level->binding_depth != binding_depth)
550       abort ();
551 #endif /* defined(DEBUG_BINDING_LEVELS) */
552     free_binding_level = level;
553     find_class_binding_level ();
554   }
555 }
556
557 static void
558 suspend_binding_level (void)
559 {
560   if (class_binding_level)
561     current_binding_level = class_binding_level;
562
563   if (global_binding_level)
564     {
565       /* Cannot suspend a level, if there are none left to suspend.  */
566       if (current_binding_level == global_binding_level)
567         abort ();
568     }
569   /* Suspend the current level.  */
570 #if defined(DEBUG_BINDING_LEVELS)
571   binding_depth--;
572   indent ();
573   fprintf (stderr, "suspend  %s level 0x%08x line %d\n",
574           (is_class_level) ? "class" : "block",
575           current_binding_level, lineno);
576   if (is_class_level != (current_binding_level == class_binding_level))
577     {
578       indent ();
579       fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
580     }
581   is_class_level = 0;
582 #endif /* defined(DEBUG_BINDING_LEVELS) */
583   current_binding_level = current_binding_level->level_chain;
584   find_class_binding_level ();
585 }
586
587 static void
588 resume_binding_level (struct cp_binding_level* b)
589 {
590   /* Resuming binding levels is meant only for namespaces,
591      and those cannot nest into classes.  */
592   my_friendly_assert(!class_binding_level, 386);
593   /* Also, resuming a non-directly nested namespace is a no-no.  */
594   my_friendly_assert(b->level_chain == current_binding_level, 386);
595   current_binding_level = b;
596 #if defined(DEBUG_BINDING_LEVELS)
597   b->binding_depth = binding_depth;
598   indent ();
599   fprintf (stderr, "resume %s level 0x%08x line %d\n",
600            (is_class_level) ? "class" : "block", b, lineno);
601   is_class_level = 0;
602   binding_depth++;
603 #endif /* defined(DEBUG_BINDING_LEVELS) */
604 }
605 \f
606 /* Create a new `struct cp_binding_level'.  */
607
608 static
609 struct cp_binding_level *
610 make_binding_level (void)
611 {
612   /* NOSTRICT */
613   return (struct cp_binding_level *) ggc_alloc (sizeof (struct cp_binding_level));
614 }
615
616 /* Nonzero if we are currently in the global binding level.  */
617
618 int
619 global_bindings_p (void)
620 {
621   return current_binding_level == global_binding_level;
622 }
623
624 /* Return the innermost binding level that is not for a class scope.  */
625
626 static struct cp_binding_level *
627 innermost_nonclass_level (void)
628 {
629   struct cp_binding_level *b;
630
631   b = current_binding_level;
632   while (b->parm_flag == 2)
633     b = b->level_chain;
634
635   return b;
636 }
637
638 /* Nonzero if we are currently in a toplevel binding level.  This
639    means either the global binding level or a namespace in a toplevel
640    binding level.  Since there are no non-toplevel namespace levels,
641    this really means any namespace or template parameter level.  We
642    also include a class whose context is toplevel.  */
643
644 int
645 toplevel_bindings_p (void)
646 {
647   struct cp_binding_level *b = innermost_nonclass_level ();
648
649   return b->namespace_p || b->template_parms_p;
650 }
651
652 /* Nonzero if this is a namespace scope, or if we are defining a class
653    which is itself at namespace scope, or whose enclosing class is
654    such a class, etc.  */
655
656 int
657 namespace_bindings_p (void)
658 {
659   struct cp_binding_level *b = innermost_nonclass_level ();
660
661   return b->namespace_p;
662 }
663
664 /* If KEEP is nonzero, make a BLOCK node for the next binding level,
665    unconditionally.  Otherwise, use the normal logic to decide whether
666    or not to create a BLOCK.  */
667
668 void
669 keep_next_level (int keep)
670 {
671   keep_next_level_flag = keep;
672 }
673
674 /* Nonzero if the current level needs to have a BLOCK made.  */
675
676 int
677 kept_level_p (void)
678 {
679   return (current_binding_level->blocks != NULL_TREE
680           || current_binding_level->keep
681           || current_binding_level->names != NULL_TREE
682           || (current_binding_level->tags != NULL_TREE
683               && !current_binding_level->tag_transparent));
684 }
685
686 static void
687 declare_namespace_level (void)
688 {
689   current_binding_level->namespace_p = 1;
690 }
691
692 /* Returns nonzero if this scope was created to store template
693    parameters.  */
694
695 int
696 template_parm_scope_p (void)
697 {
698   return current_binding_level->template_parms_p;
699 }
700
701 /* Returns the kind of template specialization we are currently
702    processing, given that it's declaration contained N_CLASS_SCOPES
703    explicit scope qualifications.  */
704
705 tmpl_spec_kind
706 current_tmpl_spec_kind (int n_class_scopes)
707 {
708   int n_template_parm_scopes = 0;
709   int seen_specialization_p = 0;
710   int innermost_specialization_p = 0;
711   struct cp_binding_level *b;
712
713   /* Scan through the template parameter scopes.  */
714   for (b = current_binding_level; b->template_parms_p; b = b->level_chain)
715     {
716       /* If we see a specialization scope inside a parameter scope,
717          then something is wrong.  That corresponds to a declaration
718          like:
719
720             template <class T> template <> ...
721
722          which is always invalid since [temp.expl.spec] forbids the
723          specialization of a class member template if the enclosing
724          class templates are not explicitly specialized as well.  */
725       if (b->template_spec_p)
726         {
727           if (n_template_parm_scopes == 0)
728             innermost_specialization_p = 1;
729           else
730             seen_specialization_p = 1;
731         }
732       else if (seen_specialization_p == 1)
733         return tsk_invalid_member_spec;
734
735       ++n_template_parm_scopes;
736     }
737
738   /* Handle explicit instantiations.  */
739   if (processing_explicit_instantiation)
740     {
741       if (n_template_parm_scopes != 0)
742         /* We've seen a template parameter list during an explicit
743            instantiation.  For example:
744
745              template <class T> template void f(int);
746
747            This is erroneous.  */
748         return tsk_invalid_expl_inst;
749       else
750         return tsk_expl_inst;
751     }
752
753   if (n_template_parm_scopes < n_class_scopes)
754     /* We've not seen enough template headers to match all the
755        specialized classes present.  For example:
756
757          template <class T> void R<T>::S<T>::f(int);
758
759        This is invalid; there needs to be one set of template
760        parameters for each class.  */
761     return tsk_insufficient_parms;
762   else if (n_template_parm_scopes == n_class_scopes)
763     /* We're processing a non-template declaration (even though it may
764        be a member of a template class.)  For example:
765
766          template <class T> void S<T>::f(int);
767
768        The `class T' maches the `S<T>', leaving no template headers
769        corresponding to the `f'.  */
770     return tsk_none;
771   else if (n_template_parm_scopes > n_class_scopes + 1)
772     /* We've got too many template headers.  For example:
773
774          template <> template <class T> void f (T);
775
776        There need to be more enclosing classes.  */
777     return tsk_excessive_parms;
778   else
779     /* This must be a template.  It's of the form:
780
781          template <class T> template <class U> void S<T>::f(U);
782
783        This is a specialization if the innermost level was a
784        specialization; otherwise it's just a definition of the
785        template.  */
786     return innermost_specialization_p ? tsk_expl_spec : tsk_template;
787 }
788
789 void
790 set_class_shadows (tree shadows)
791 {
792   class_binding_level->class_shadowed = shadows;
793 }
794
795 /* Enter a new binding level.
796    If TAG_TRANSPARENT is nonzero, do so only for the name space of variables,
797    not for that of tags.  */
798
799 void
800 pushlevel (int tag_transparent)
801 {
802   struct cp_binding_level *newlevel;
803
804   if (cfun && !doing_semantic_analysis_p ())
805     return;
806
807   /* Reuse or create a struct for this binding level.  */
808 #if defined(DEBUG_BINDING_LEVELS)
809   if (0)
810 #else /* !defined(DEBUG_BINDING_LEVELS) */
811   if (free_binding_level)
812 #endif /* !defined(DEBUG_BINDING_LEVELS) */
813     {
814       newlevel = free_binding_level;
815       free_binding_level = free_binding_level->level_chain;
816     }
817   else
818     newlevel = make_binding_level ();
819
820   push_binding_level (newlevel, tag_transparent, keep_next_level_flag);
821   keep_next_level_flag = 0;
822 }
823
824 /* We're defining an object of type TYPE.  If it needs a cleanup, but
825    we're not allowed to add any more objects with cleanups to the current
826    scope, create a new binding level.  */
827
828 void
829 maybe_push_cleanup_level (tree type)
830 {
831   if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
832       && current_binding_level->more_cleanups_ok == 0)
833     {
834       keep_next_level (2);
835       pushlevel (1);
836       clear_last_expr ();
837       add_scope_stmt (/*begin_p=*/1, /*partial_p=*/1);
838     }
839 }
840   
841 /* Enter a new scope.  The KIND indicates what kind of scope is being
842    created.  */
843
844 void
845 begin_scope (scope_kind sk)
846 {
847   pushlevel (0);
848
849   switch (sk)
850     {
851     case sk_block:
852       break;
853
854     case sk_try:
855       current_binding_level->is_try_scope = 1;
856       break;
857
858     case sk_catch:
859       current_binding_level->is_catch_scope = 1;
860       break;
861
862     case sk_for:
863       current_binding_level->is_for_scope = 1;
864       break;
865
866     case sk_template_spec:
867       current_binding_level->template_spec_p = 1;
868       /* Fall through.  */
869
870     case sk_template_parms:
871       current_binding_level->template_parms_p = 1;
872       break;
873
874     default:
875       abort ();
876     }
877 }
878
879 /* Exit the current scope.  */
880
881 void
882 finish_scope (void)
883 {
884   poplevel (0, 0, 0);
885 }
886
887 /* Make DECL the innermost binding for ID.  The LEVEL is the binding
888    level at which this declaration is being bound.  */
889
890 static void
891 push_binding (tree id, tree decl, cxx_scope* level)
892 {
893    cxx_binding *binding = cxx_binding_make (decl, NULL);
894
895   /* Now, fill in the binding information.  */
896   binding->previous = IDENTIFIER_BINDING (id);
897   BINDING_SCOPE (binding) = level;
898   INHERITED_VALUE_BINDING_P (binding) = 0;
899   LOCAL_BINDING_P (binding) = (level != class_binding_level);
900
901   /* And put it on the front of the list of bindings for ID.  */
902   IDENTIFIER_BINDING (id) = binding;
903 }
904
905 /* ID is already bound in the current scope.  But, DECL is an
906    additional binding for ID in the same scope.  This is the `struct
907    stat' hack whereby a non-typedef class-name or enum-name can be
908    bound at the same level as some other kind of entity.  It's the
909    responsibility of the caller to check that inserting this name is
910    valid here.  Returns nonzero if the new binding was successful.  */
911 static int
912 add_binding (tree id, tree decl)
913 {
914   cxx_binding *binding = IDENTIFIER_BINDING (id);
915   int ok = 1;
916
917   timevar_push (TV_NAME_LOOKUP);
918   if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
919     /* The new name is the type name.  */
920     BINDING_TYPE (binding) = decl;
921   else if (!BINDING_VALUE (binding))
922     /* This situation arises when push_class_level_binding moves an
923        inherited type-binding out of the way to make room for a new
924        value binding.  */
925     BINDING_VALUE (binding) = decl;
926   else if (TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
927            && DECL_ARTIFICIAL (BINDING_VALUE (binding)))
928     {
929       /* The old binding was a type name.  It was placed in
930          BINDING_VALUE because it was thought, at the point it was
931          declared, to be the only entity with such a name.  Move the
932          type name into the type slot; it is now hidden by the new
933          binding.  */
934       BINDING_TYPE (binding) = BINDING_VALUE (binding);
935       BINDING_VALUE (binding) = decl;
936       INHERITED_VALUE_BINDING_P (binding) = 0;
937     }
938   else if (TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
939            && TREE_CODE (decl) == TYPE_DECL
940            && DECL_NAME (decl) == DECL_NAME (BINDING_VALUE (binding))
941            && (same_type_p (TREE_TYPE (decl),
942                             TREE_TYPE (BINDING_VALUE (binding)))
943                /* If either type involves template parameters, we must
944                   wait until instantiation.  */
945                || uses_template_parms (TREE_TYPE (decl))
946                || uses_template_parms (TREE_TYPE (BINDING_VALUE (binding)))))
947     /* We have two typedef-names, both naming the same type to have
948        the same name.  This is OK because of:
949
950          [dcl.typedef]
951
952          In a given scope, a typedef specifier can be used to redefine
953          the name of any type declared in that scope to refer to the
954          type to which it already refers.  */
955     ok = 0;
956   /* There can be two block-scope declarations of the same variable,
957      so long as they are `extern' declarations.  */
958   else if (TREE_CODE (decl) == VAR_DECL
959            && TREE_CODE (BINDING_VALUE (binding)) == VAR_DECL
960            && DECL_EXTERNAL (decl)
961            && DECL_EXTERNAL (BINDING_VALUE (binding)))
962     {
963       duplicate_decls (decl, BINDING_VALUE (binding));
964       ok = 0;
965     }
966   else
967     {
968       error ("declaration of `%#D'", decl);
969       cp_error_at ("conflicts with previous declaration `%#D'",
970                    BINDING_VALUE (binding));
971       ok = 0;
972     }
973
974   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ok);
975 }
976
977 /* Add DECL to the list of things declared in B.  */
978
979 static void
980 add_decl_to_level (tree decl, 
981                    struct cp_binding_level* b)
982 {
983   if (TREE_CODE (decl) == NAMESPACE_DECL 
984       && !DECL_NAMESPACE_ALIAS (decl))
985     {
986       TREE_CHAIN (decl) = b->namespaces;
987       b->namespaces = decl;
988     }
989   else if (TREE_CODE (decl) == VAR_DECL && DECL_VIRTUAL_P (decl))
990     {
991       TREE_CHAIN (decl) = b->vtables;
992       b->vtables = decl;
993     }
994   else       
995     {
996       /* We build up the list in reverse order, and reverse it later if
997          necessary.  */
998       TREE_CHAIN (decl) = b->names;
999       b->names = decl;
1000       b->names_size++;
1001
1002       /* If appropriate, add decl to separate list of statics */
1003       if (b->namespace_p)
1004         if ((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
1005             || (TREE_CODE (decl) == FUNCTION_DECL
1006                 && (!TREE_PUBLIC (decl) || DECL_DECLARED_INLINE_P (decl))))
1007           VARRAY_PUSH_TREE (b->static_decls, decl);
1008     }
1009 }
1010
1011 /* Bind DECL to ID in the current_binding_level, assumed to be a local
1012    binding level.  If PUSH_USING is set in FLAGS, we know that DECL
1013    doesn't really belong to this binding level, that it got here
1014    through a using-declaration.  */
1015
1016 void
1017 push_local_binding (tree id, tree decl, int flags)
1018 {
1019   struct cp_binding_level *b;
1020
1021   /* Skip over any local classes.  This makes sense if we call
1022      push_local_binding with a friend decl of a local class.  */
1023   b = current_binding_level;
1024   while (b->parm_flag == 2)
1025     b = b->level_chain;
1026
1027   if (lookup_name_current_level (id))
1028     {
1029       /* Supplement the existing binding.  */
1030       if (!add_binding (id, decl))
1031         /* It didn't work.  Something else must be bound at this
1032            level.  Do not add DECL to the list of things to pop
1033            later.  */
1034         return;
1035     }
1036   else
1037     /* Create a new binding.  */
1038     push_binding (id, decl, b);
1039
1040   if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
1041     /* We must put the OVERLOAD into a TREE_LIST since the
1042        TREE_CHAIN of an OVERLOAD is already used.  Similarly for
1043        decls that got here through a using-declaration.  */
1044     decl = build_tree_list (NULL_TREE, decl);
1045
1046   /* And put DECL on the list of things declared by the current
1047      binding level.  */
1048   add_decl_to_level (decl, b);
1049 }
1050
1051 /* Bind DECL to ID in the class_binding_level.  Returns nonzero if the
1052    binding was successful.  */
1053
1054 int
1055 push_class_binding (tree id, tree decl)
1056 {
1057   int result = 1;
1058   cxx_binding *binding = IDENTIFIER_BINDING (id);
1059   tree context;
1060
1061   timevar_push (TV_NAME_LOOKUP);
1062   /* Note that we declared this value so that we can issue an error if
1063      this is an invalid redeclaration of a name already used for some
1064      other purpose.  */
1065   note_name_declared_in_class (id, decl);
1066
1067   if (binding && BINDING_SCOPE (binding) == class_binding_level)
1068     /* Supplement the existing binding.  */
1069     result = add_binding (id, decl);
1070   else
1071     /* Create a new binding.  */
1072     push_binding (id, decl, class_binding_level);
1073
1074   /* Update the IDENTIFIER_CLASS_VALUE for this ID to be the
1075      class-level declaration.  Note that we do not use DECL here
1076      because of the possibility of the `struct stat' hack; if DECL is
1077      a class-name or enum-name we might prefer a field-name, or some
1078      such.  */
1079   IDENTIFIER_CLASS_VALUE (id) = BINDING_VALUE (IDENTIFIER_BINDING (id));
1080
1081   /* If this is a binding from a base class, mark it as such.  */
1082   binding = IDENTIFIER_BINDING (id);
1083   if (BINDING_VALUE (binding) == decl && TREE_CODE (decl) != TREE_LIST)
1084     {
1085       if (TREE_CODE (decl) == OVERLOAD)
1086         context = CP_DECL_CONTEXT (OVL_CURRENT (decl));
1087       else
1088         {
1089           my_friendly_assert (DECL_P (decl), 0);
1090           context = context_for_name_lookup (decl);
1091         }
1092
1093       if (is_properly_derived_from (current_class_type, context))
1094         INHERITED_VALUE_BINDING_P (binding) = 1;
1095       else
1096         INHERITED_VALUE_BINDING_P (binding) = 0;
1097     }
1098   else if (BINDING_VALUE (binding) == decl)
1099     /* We only encounter a TREE_LIST when push_class_decls detects an
1100        ambiguity.  Such an ambiguity can be overridden by a definition
1101        in this class.  */
1102     INHERITED_VALUE_BINDING_P (binding) = 1;
1103
1104   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, result);
1105 }
1106
1107 /* Remove the binding for DECL which should be the innermost binding
1108    for ID.  */
1109
1110 static void
1111 pop_binding (tree id, tree decl)
1112 {
1113   cxx_binding *binding;
1114
1115   if (id == NULL_TREE)
1116     /* It's easiest to write the loops that call this function without
1117        checking whether or not the entities involved have names.  We
1118        get here for such an entity.  */
1119     return;
1120
1121   /* Get the innermost binding for ID.  */
1122   binding = IDENTIFIER_BINDING (id);
1123
1124   /* The name should be bound.  */
1125   my_friendly_assert (binding != NULL, 0);
1126
1127   /* The DECL will be either the ordinary binding or the type
1128      binding for this identifier.  Remove that binding.  */
1129   if (BINDING_VALUE (binding) == decl)
1130     BINDING_VALUE (binding) = NULL_TREE;
1131   else if (BINDING_TYPE (binding) == decl)
1132     BINDING_TYPE (binding) = NULL_TREE;
1133   else
1134     abort ();
1135
1136   if (!BINDING_VALUE (binding) && !BINDING_TYPE (binding))
1137     {
1138       /* We're completely done with the innermost binding for this
1139          identifier.  Unhook it from the list of bindings.  */
1140       IDENTIFIER_BINDING (id) = binding->previous;
1141
1142       /* Add it to the free list.  */
1143       cxx_binding_free (binding);
1144
1145       /* Clear the BINDING_SCOPE so the garbage collector doesn't walk
1146          it.  */
1147       BINDING_SCOPE (binding) = NULL;
1148     }
1149 }
1150
1151 /* When a label goes out of scope, check to see if that label was used
1152    in a valid manner, and issue any appropriate warnings or errors.  */
1153
1154 static void
1155 pop_label (tree label, tree old_value)
1156 {
1157   if (!processing_template_decl && doing_semantic_analysis_p ())
1158     {
1159       if (DECL_INITIAL (label) == NULL_TREE)
1160         {
1161           cp_error_at ("label `%D' used but not defined", label);
1162           /* Avoid crashing later.  */
1163           define_label (input_filename, 1, DECL_NAME (label));
1164         }
1165       else if (warn_unused_label && !TREE_USED (label))
1166         cp_warning_at ("label `%D' defined but not used", label);
1167     }
1168
1169   SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), old_value);
1170 }
1171
1172 /* At the end of a function, all labels declared within the function
1173    go out of scope.  BLOCK is the top-level block for the
1174    function.  */
1175
1176 static void
1177 pop_labels (tree block)
1178 {
1179   struct named_label_list *link;
1180
1181   /* Clear out the definitions of all label names, since their scopes
1182      end here.  */
1183   for (link = named_labels; link; link = link->next)
1184     {
1185       pop_label (link->label_decl, link->old_value);
1186       /* Put the labels into the "variables" of the top-level block,
1187          so debugger can see them.  */
1188       TREE_CHAIN (link->label_decl) = BLOCK_VARS (block);
1189       BLOCK_VARS (block) = link->label_decl;
1190     }
1191
1192   named_labels = NULL;
1193 }
1194
1195 /* Exit a binding level.
1196    Pop the level off, and restore the state of the identifier-decl mappings
1197    that were in effect when this level was entered.
1198
1199    If KEEP == 1, this level had explicit declarations, so
1200    and create a "block" (a BLOCK node) for the level
1201    to record its declarations and subblocks for symbol table output.
1202
1203    If FUNCTIONBODY is nonzero, this level is the body of a function,
1204    so create a block as if KEEP were set and also clear out all
1205    label names.
1206
1207    If REVERSE is nonzero, reverse the order of decls before putting
1208    them into the BLOCK.  */
1209
1210 tree
1211 poplevel (int keep, int reverse, int functionbody)
1212 {
1213   register tree link;
1214   /* The chain of decls was accumulated in reverse order.
1215      Put it into forward order, just for cleanliness.  */
1216   tree decls;
1217   int tmp = functionbody;
1218   int real_functionbody;
1219   tree tags;
1220   tree subblocks;
1221   tree block = NULL_TREE;
1222   tree decl;
1223   int leaving_for_scope;
1224
1225   timevar_push (TV_NAME_LOOKUP);
1226   if (cfun && !doing_semantic_analysis_p ())
1227     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
1228
1229   my_friendly_assert (current_binding_level->parm_flag != 2,
1230                       19990916);
1231
1232   real_functionbody = (current_binding_level->keep == 2
1233                        ? ((functionbody = 0), tmp) : functionbody);
1234   tags = functionbody >= 0 ? current_binding_level->tags : 0;
1235   subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
1236
1237   my_friendly_assert (!current_binding_level->class_shadowed,
1238                       19990414);
1239
1240   /* We used to use KEEP == 2 to indicate that the new block should go
1241      at the beginning of the list of blocks at this binding level,
1242      rather than the end.  This hack is no longer used.  */
1243   my_friendly_assert (keep == 0 || keep == 1, 0);
1244
1245   if (current_binding_level->keep == 1)
1246     keep = 1;
1247
1248   /* Any uses of undefined labels, and any defined labels, now operate
1249      under constraints of next binding contour.  */
1250   if (cfun && !functionbody)
1251     {
1252       struct cp_binding_level *level_chain;
1253       level_chain = current_binding_level->level_chain;
1254       if (level_chain)
1255         {
1256           struct named_label_use_list *uses;
1257           struct named_label_list *labels;
1258           for (labels = named_labels; labels; labels = labels->next)
1259             if (labels->binding_level == current_binding_level)
1260               {
1261                 tree decl;
1262                 if (current_binding_level->is_try_scope)
1263                   labels->in_try_scope = 1;
1264                 if (current_binding_level->is_catch_scope)
1265                   labels->in_catch_scope = 1;
1266                 for (decl = labels->names_in_scope; decl;
1267                      decl = TREE_CHAIN (decl))
1268                   if (decl_jump_unsafe (decl))
1269                     labels->bad_decls = tree_cons (NULL_TREE, decl,
1270                                                    labels->bad_decls);
1271                 labels->binding_level = level_chain;
1272                 labels->names_in_scope = level_chain->names;
1273               }
1274
1275           for (uses = named_label_uses; uses; uses = uses->next)
1276             if (uses->binding_level == current_binding_level)
1277               {
1278                 uses->binding_level = level_chain;
1279                 uses->names_in_scope = level_chain->names;
1280               }
1281         }
1282     }
1283
1284   /* Get the decls in the order they were written.
1285      Usually current_binding_level->names is in reverse order.
1286      But parameter decls were previously put in forward order.  */
1287
1288   if (reverse)
1289     current_binding_level->names
1290       = decls = nreverse (current_binding_level->names);
1291   else
1292     decls = current_binding_level->names;
1293
1294   /* Output any nested inline functions within this block
1295      if they weren't already output.  */
1296   for (decl = decls; decl; decl = TREE_CHAIN (decl))
1297     if (TREE_CODE (decl) == FUNCTION_DECL
1298         && ! TREE_ASM_WRITTEN (decl)
1299         && DECL_INITIAL (decl) != NULL_TREE
1300         && TREE_ADDRESSABLE (decl)
1301         && decl_function_context (decl) == current_function_decl)
1302       {
1303         /* If this decl was copied from a file-scope decl
1304            on account of a block-scope extern decl,
1305            propagate TREE_ADDRESSABLE to the file-scope decl.  */
1306         if (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE)
1307           TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (decl)) = 1;
1308         else
1309           {
1310             push_function_context ();
1311             output_inline_function (decl);
1312             pop_function_context ();
1313           }
1314       }
1315
1316   /* When not in function-at-a-time mode, expand_end_bindings will
1317      warn about unused variables.  But, in function-at-a-time mode
1318      expand_end_bindings is not passed the list of variables in the
1319      current scope, and therefore no warning is emitted.  So, we
1320      explicitly warn here.  */
1321   if (!processing_template_decl)
1322     warn_about_unused_variables (getdecls ());
1323
1324   /* If there were any declarations or structure tags in that level,
1325      or if this level is a function body,
1326      create a BLOCK to record them for the life of this function.  */
1327   block = NULL_TREE;
1328   if (keep == 1 || functionbody)
1329     block = make_node (BLOCK);
1330   if (block != NULL_TREE)
1331     {
1332       BLOCK_VARS (block) = decls;
1333       BLOCK_SUBBLOCKS (block) = subblocks;
1334     }
1335
1336   /* In each subblock, record that this is its superior.  */
1337   if (keep >= 0)
1338     for (link = subblocks; link; link = TREE_CHAIN (link))
1339       BLOCK_SUPERCONTEXT (link) = block;
1340
1341   /* We still support the old for-scope rules, whereby the variables
1342      in a for-init statement were in scope after the for-statement
1343      ended.  We only use the new rules if flag_new_for_scope is
1344      nonzero.  */
1345   leaving_for_scope
1346     = current_binding_level->is_for_scope && flag_new_for_scope == 1;
1347
1348   /* Remove declarations for all the DECLs in this level.  */
1349   for (link = decls; link; link = TREE_CHAIN (link))
1350     {
1351       if (leaving_for_scope && TREE_CODE (link) == VAR_DECL
1352           && DECL_NAME (link))
1353         {
1354           cxx_binding *outer_binding
1355             = IDENTIFIER_BINDING (DECL_NAME (link))->previous;
1356           tree ns_binding;
1357
1358           if (!outer_binding)
1359             ns_binding = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (link));
1360           else
1361             ns_binding = NULL_TREE;
1362
1363           if (outer_binding
1364               && (BINDING_SCOPE (outer_binding)
1365                   == current_binding_level->level_chain))
1366             /* We have something like:
1367
1368                  int i;
1369                  for (int i; ;);
1370
1371                and we are leaving the `for' scope.  There's no reason to
1372                keep the binding of the inner `i' in this case.  */
1373             pop_binding (DECL_NAME (link), link);
1374           else if ((outer_binding
1375                     && (TREE_CODE (BINDING_VALUE (outer_binding))
1376                         == TYPE_DECL))
1377                    || (ns_binding
1378                        && TREE_CODE (ns_binding) == TYPE_DECL))
1379             /* Here, we have something like:
1380
1381                  typedef int I;
1382
1383                  void f () {
1384                    for (int I; ;);
1385                  }
1386
1387                We must pop the for-scope binding so we know what's a
1388                type and what isn't.  */
1389             pop_binding (DECL_NAME (link), link);
1390           else
1391             {
1392               /* Mark this VAR_DECL as dead so that we can tell we left it
1393                  there only for backward compatibility.  */
1394               DECL_DEAD_FOR_LOCAL (link) = 1;
1395
1396               /* Keep track of what should of have happenned when we
1397                  popped the binding.  */
1398               if (outer_binding && BINDING_VALUE (outer_binding))
1399                 DECL_SHADOWED_FOR_VAR (link)
1400                   = BINDING_VALUE (outer_binding);
1401
1402               /* Add it to the list of dead variables in the next
1403                  outermost binding to that we can remove these when we
1404                  leave that binding.  */
1405               current_binding_level->level_chain->dead_vars_from_for
1406                 = tree_cons (NULL_TREE, link,
1407                              current_binding_level->level_chain->
1408                              dead_vars_from_for);
1409
1410               /* Although we don't pop the cxx_binding, we do clear
1411                  its BINDING_SCOPE since the level is going away now.  */
1412               BINDING_SCOPE (IDENTIFIER_BINDING (DECL_NAME (link))) = 0;
1413             }
1414         }
1415       else
1416         {
1417           /* Remove the binding.  */
1418           decl = link;
1419           if (TREE_CODE (decl) == TREE_LIST)
1420             decl = TREE_VALUE (decl);
1421           if (DECL_P (decl))
1422             pop_binding (DECL_NAME (decl), decl);
1423           else if (TREE_CODE (decl) == OVERLOAD)
1424             pop_binding (DECL_NAME (OVL_FUNCTION (decl)), decl);
1425           else
1426             abort ();
1427         }
1428     }
1429
1430   /* Remove declarations for any `for' variables from inner scopes
1431      that we kept around.  */
1432   for (link = current_binding_level->dead_vars_from_for;
1433        link; link = TREE_CHAIN (link))
1434     pop_binding (DECL_NAME (TREE_VALUE (link)), TREE_VALUE (link));
1435
1436   /* Restore the IDENTIFIER_TYPE_VALUEs.  */
1437   for (link = current_binding_level->type_shadowed;
1438        link; link = TREE_CHAIN (link))
1439     SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
1440
1441   /* Restore the IDENTIFIER_LABEL_VALUEs for local labels.  */
1442   for (link = current_binding_level->shadowed_labels;
1443        link;
1444        link = TREE_CHAIN (link))
1445     pop_label (TREE_VALUE (link), TREE_PURPOSE (link));
1446
1447   /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
1448      list if a `using' declaration put them there.  The debugging
1449      back-ends won't understand OVERLOAD, so we remove them here.
1450      Because the BLOCK_VARS are (temporarily) shared with
1451      CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
1452      popped all the bindings.  */
1453   if (block)
1454     {
1455       tree* d;
1456
1457       for (d = &BLOCK_VARS (block); *d; )
1458         {
1459           if (TREE_CODE (*d) == TREE_LIST)
1460             *d = TREE_CHAIN (*d);
1461           else
1462             d = &TREE_CHAIN (*d);
1463         }
1464     }
1465
1466   /* If the level being exited is the top level of a function,
1467      check over all the labels.  */
1468   if (functionbody)
1469     {
1470       /* Since this is the top level block of a function, the vars are
1471          the function's parameters.  Don't leave them in the BLOCK
1472          because they are found in the FUNCTION_DECL instead.  */
1473       BLOCK_VARS (block) = 0;
1474       pop_labels (block);
1475     }
1476
1477   tmp = current_binding_level->keep;
1478
1479   pop_binding_level ();
1480   if (functionbody)
1481     DECL_INITIAL (current_function_decl) = block;
1482   else if (block)
1483     current_binding_level->blocks
1484       = chainon (current_binding_level->blocks, block);
1485
1486   /* If we did not make a block for the level just exited,
1487      any blocks made for inner levels
1488      (since they cannot be recorded as subblocks in that level)
1489      must be carried forward so they will later become subblocks
1490      of something else.  */
1491   else if (subblocks)
1492     current_binding_level->blocks
1493       = chainon (current_binding_level->blocks, subblocks);
1494
1495   /* Each and every BLOCK node created here in `poplevel' is important
1496      (e.g. for proper debugging information) so if we created one
1497      earlier, mark it as "used".  */
1498   if (block)
1499     TREE_USED (block) = 1;
1500
1501   /* Take care of compiler's internal binding structures.  */
1502   if (tmp == 2)
1503     {
1504       tree scope_stmts;
1505
1506       scope_stmts
1507         = add_scope_stmt (/*begin_p=*/0, /*partial_p=*/1);
1508       if (block)
1509         {
1510           SCOPE_STMT_BLOCK (TREE_PURPOSE (scope_stmts)) = block;
1511           SCOPE_STMT_BLOCK (TREE_VALUE (scope_stmts)) = block;
1512         }
1513
1514       block = poplevel (keep, reverse, functionbody);
1515     }
1516
1517   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, block);
1518 }
1519
1520 /* Delete the node BLOCK from the current binding level.
1521    This is used for the block inside a stmt expr ({...})
1522    so that the block can be reinserted where appropriate.  */
1523
1524 void
1525 delete_block (tree block)
1526 {
1527   tree t;
1528   if (current_binding_level->blocks == block)
1529     current_binding_level->blocks = TREE_CHAIN (block);
1530   for (t = current_binding_level->blocks; t;)
1531     {
1532       if (TREE_CHAIN (t) == block)
1533         TREE_CHAIN (t) = TREE_CHAIN (block);
1534       else
1535         t = TREE_CHAIN (t);
1536     }
1537   TREE_CHAIN (block) = NULL_TREE;
1538   /* Clear TREE_USED which is always set by poplevel.
1539      The flag is set again if insert_block is called.  */
1540   TREE_USED (block) = 0;
1541 }
1542
1543 /* Insert BLOCK at the end of the list of subblocks of the
1544    current binding level.  This is used when a BIND_EXPR is expanded,
1545    to handle the BLOCK node inside the BIND_EXPR.  */
1546
1547 void
1548 insert_block (tree block)
1549 {
1550   TREE_USED (block) = 1;
1551   current_binding_level->blocks
1552     = chainon (current_binding_level->blocks, block);
1553 }
1554
1555 /* Set the BLOCK node for the innermost scope
1556    (the one we are currently in).  */
1557
1558 void
1559 set_block (tree block ATTRIBUTE_UNUSED )
1560 {
1561   /* The RTL expansion machinery requires us to provide this callback,
1562      but it is not applicable in function-at-a-time mode.  */
1563   my_friendly_assert (cfun && !doing_semantic_analysis_p (), 20000911);
1564 }
1565
1566 /* Do a pushlevel for class declarations.  */
1567
1568 void
1569 pushlevel_class (void)
1570 {
1571   register struct cp_binding_level *newlevel;
1572
1573   /* Reuse or create a struct for this binding level.  */
1574 #if defined(DEBUG_BINDING_LEVELS)
1575   if (0)
1576 #else /* !defined(DEBUG_BINDING_LEVELS) */
1577   if (free_binding_level)
1578 #endif /* !defined(DEBUG_BINDING_LEVELS) */
1579     {
1580       newlevel = free_binding_level;
1581       free_binding_level = free_binding_level->level_chain;
1582     }
1583   else
1584     newlevel = make_binding_level ();
1585
1586 #if defined(DEBUG_BINDING_LEVELS)
1587   is_class_level = 1;
1588 #endif /* defined(DEBUG_BINDING_LEVELS) */
1589
1590   push_binding_level (newlevel, 0, 0);
1591
1592   class_binding_level = current_binding_level;
1593   class_binding_level->parm_flag = 2;
1594   class_binding_level->this_class = current_class_type;
1595 }
1596
1597 /* ...and a poplevel for class declarations.  */
1598
1599 void
1600 poplevel_class (void)
1601 {
1602   register struct cp_binding_level *level = class_binding_level;
1603   tree shadowed;
1604
1605   timevar_push (TV_NAME_LOOKUP);
1606   my_friendly_assert (level != 0, 354);
1607
1608   /* If we're leaving a toplevel class, don't bother to do the setting
1609      of IDENTIFIER_CLASS_VALUE to NULL_TREE, since first of all this slot
1610      shouldn't even be used when current_class_type isn't set, and second,
1611      if we don't touch it here, we're able to use the cache effect if the
1612      next time we're entering a class scope, it is the same class.  */
1613   if (current_class_depth != 1)
1614     {
1615       struct cp_binding_level* b;
1616
1617       /* Clear out our IDENTIFIER_CLASS_VALUEs.  */
1618       for (shadowed = level->class_shadowed;
1619            shadowed;
1620            shadowed = TREE_CHAIN (shadowed))
1621         IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed)) = NULL_TREE;
1622
1623       /* Find the next enclosing class, and recreate
1624          IDENTIFIER_CLASS_VALUEs appropriate for that class.  */
1625       b = level->level_chain;
1626       while (b && b->parm_flag != 2)
1627         b = b->level_chain;
1628
1629       if (b)
1630         for (shadowed = b->class_shadowed;
1631              shadowed;
1632              shadowed = TREE_CHAIN (shadowed))
1633           {
1634             cxx_binding *binding;
1635             
1636             binding = IDENTIFIER_BINDING (TREE_PURPOSE (shadowed));
1637             while (binding && BINDING_SCOPE (binding) != b)
1638               binding = binding->previous;
1639
1640             if (binding)
1641               IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed))
1642                 = BINDING_VALUE (binding);
1643           }
1644     }
1645   else
1646     /* Remember to save what IDENTIFIER's were bound in this scope so we
1647        can recover from cache misses.  */
1648     {
1649       previous_class_type = current_class_type;
1650       previous_class_values = class_binding_level->class_shadowed;
1651     }
1652   for (shadowed = level->type_shadowed;
1653        shadowed;
1654        shadowed = TREE_CHAIN (shadowed))
1655     SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
1656
1657   /* Remove the bindings for all of the class-level declarations.  */
1658   for (shadowed = level->class_shadowed;
1659        shadowed;
1660        shadowed = TREE_CHAIN (shadowed))
1661     pop_binding (TREE_PURPOSE (shadowed), TREE_TYPE (shadowed));
1662
1663   /* Now, pop out of the binding level which we created up in the
1664      `pushlevel_class' routine.  */
1665 #if defined(DEBUG_BINDING_LEVELS)
1666   is_class_level = 1;
1667 #endif /* defined(DEBUG_BINDING_LEVELS) */
1668
1669   pop_binding_level ();
1670   timevar_pop (TV_NAME_LOOKUP);
1671 }
1672
1673 /* We are entering the scope of a class.  Clear IDENTIFIER_CLASS_VALUE
1674    for any names in enclosing classes.  */
1675
1676 void
1677 clear_identifier_class_values (void)
1678 {
1679   tree t;
1680
1681   if (!class_binding_level)
1682     return;
1683
1684   for (t = class_binding_level->class_shadowed;
1685        t;
1686        t = TREE_CHAIN (t))
1687     IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
1688 }
1689
1690 /* Returns nonzero if T is a virtual function table.  */
1691
1692 int
1693 vtable_decl_p (tree t, void* data ATTRIBUTE_UNUSED )
1694 {
1695   return (TREE_CODE (t) == VAR_DECL && DECL_VIRTUAL_P (t));
1696 }
1697
1698 /* Returns nonzero if T is a TYPE_DECL for a type with virtual
1699    functions.  */
1700
1701 int
1702 vtype_decl_p (tree t, void *data ATTRIBUTE_UNUSED )
1703 {
1704   return (TREE_CODE (t) == TYPE_DECL
1705           && TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE
1706           && TYPE_POLYMORPHIC_P (TREE_TYPE (t)));
1707 }
1708
1709 /* Return the declarations that are members of the namespace NS.  */
1710
1711 tree
1712 cp_namespace_decls (tree ns)
1713 {
1714   return NAMESPACE_LEVEL (ns)->names;
1715 }
1716
1717 struct walk_globals_data {
1718   walk_globals_pred p;
1719   walk_globals_fn f;
1720   void *data;
1721 };
1722
1723 /* Walk the vtable declarations in NAMESPACE.  Whenever one is found
1724    for which P returns nonzero, call F with its address.  If any call
1725    to F returns a nonzero value, return a nonzero value.  */
1726
1727 static int
1728 walk_vtables_r (tree namespace, void* data)
1729 {
1730   struct walk_globals_data* wgd = (struct walk_globals_data *) data;
1731   walk_globals_fn f = wgd->f;
1732   void *d = wgd->data;
1733   tree decl = NAMESPACE_LEVEL (namespace)->vtables;
1734   int result = 0;
1735
1736   for (; decl ; decl = TREE_CHAIN (decl))
1737     result |= (*f) (&decl, d);
1738
1739   return result;
1740 }
1741
1742 /* Walk the vtable declarations.  Whenever one is found for which P
1743    returns nonzero, call F with its address.  If any call to F
1744    returns a nonzero value, return a nonzero value.  */
1745 bool
1746 walk_vtables (walk_globals_pred p, walk_globals_fn f, void *data)
1747 {    
1748   struct walk_globals_data wgd;
1749   wgd.p = p;    
1750   wgd.f = f;
1751   wgd.data = data;
1752
1753   return walk_namespaces (walk_vtables_r, &wgd);
1754 }
1755
1756 /* Walk all the namespaces contained NAMESPACE, including NAMESPACE
1757    itself, calling F for each.  The DATA is passed to F as well.  */
1758
1759 static int
1760 walk_namespaces_r (tree namespace, walk_namespaces_fn f, void* data)
1761 {
1762   int result = 0;
1763   tree current = NAMESPACE_LEVEL (namespace)->namespaces;     
1764
1765   result |= (*f) (namespace, data);
1766
1767   for (; current; current = TREE_CHAIN (current))
1768     result |= walk_namespaces_r (current, f, data);
1769
1770   return result;
1771 }
1772
1773 /* Walk all the namespaces, calling F for each.  The DATA is passed to
1774    F as well.  */
1775
1776 int
1777 walk_namespaces (walk_namespaces_fn f, void* data)
1778 {
1779   return walk_namespaces_r (global_namespace, f, data);
1780 }
1781
1782 /* Walk the global declarations in NAMESPACE.  Whenever one is found
1783    for which P returns nonzero, call F with its address.  If any call
1784    to F returns a nonzero value, return a nonzero value.  */
1785
1786 static int
1787 walk_globals_r (tree namespace, void* data)
1788 {
1789   struct walk_globals_data* wgd = (struct walk_globals_data *) data;
1790   walk_globals_pred p = wgd->p;
1791   walk_globals_fn f = wgd->f;
1792   void *d = wgd->data;
1793   tree *t;
1794   int result = 0;
1795
1796   t = &NAMESPACE_LEVEL (namespace)->names;
1797
1798   while (*t)
1799     {
1800       tree glbl = *t;
1801
1802       if ((*p) (glbl, d))
1803         result |= (*f) (t, d);
1804
1805       /* If F changed *T, then *T still points at the next item to
1806          examine.  */
1807       if (*t == glbl)
1808         t = &TREE_CHAIN (*t);
1809     }
1810
1811   return result;
1812 }
1813
1814 /* Walk the global declarations.  Whenever one is found for which P
1815    returns true, call F with its address.  If any call to F
1816    returns true, return true.  */
1817
1818 bool
1819 walk_globals (walk_globals_pred p, walk_globals_fn f, void *data)
1820 {
1821   struct walk_globals_data wgd;
1822   wgd.p = p;
1823   wgd.f = f;
1824   wgd.data = data;
1825
1826   return walk_namespaces (walk_globals_r, &wgd);
1827 }
1828
1829 /* Call wrapup_globals_declarations for the globals in NAMESPACE.  If
1830    DATA is non-NULL, this is the last time we will call
1831    wrapup_global_declarations for this NAMESPACE.  */
1832
1833 int
1834 wrapup_globals_for_namespace (tree namespace, void* data)
1835 {
1836   struct cp_binding_level *level = NAMESPACE_LEVEL (namespace);
1837   varray_type statics = level->static_decls;
1838   tree *vec = &VARRAY_TREE (statics, 0);
1839   int len = VARRAY_ACTIVE_SIZE (statics);
1840   int last_time = (data != 0);
1841
1842   if (last_time)
1843     {
1844       check_global_declarations (vec, len);
1845       return 0;
1846     }
1847
1848   /* Write out any globals that need to be output.  */
1849   return wrapup_global_declarations (vec, len);
1850 }
1851
1852 \f
1853 /* For debugging.  */
1854 static int no_print_functions = 0;
1855 static int no_print_builtins = 0;
1856
1857 void
1858 print_binding_level (struct cp_binding_level* lvl)
1859 {
1860   tree t;
1861   int i = 0, len;
1862   fprintf (stderr, " blocks=");
1863   fprintf (stderr, HOST_PTR_PRINTF, (void *) lvl->blocks);
1864   if (lvl->tag_transparent)
1865     fprintf (stderr, " tag-transparent");
1866   if (lvl->more_cleanups_ok)
1867     fprintf (stderr, " more-cleanups-ok");
1868   if (lvl->have_cleanups)
1869     fprintf (stderr, " have-cleanups");
1870   fprintf (stderr, "\n");
1871   if (lvl->names)
1872     {
1873       fprintf (stderr, " names:\t");
1874       /* We can probably fit 3 names to a line?  */
1875       for (t = lvl->names; t; t = TREE_CHAIN (t))
1876         {
1877           if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
1878             continue;
1879           if (no_print_builtins
1880               && (TREE_CODE (t) == TYPE_DECL)
1881               && (!strcmp (DECL_SOURCE_FILE (t),"<built-in>")))
1882             continue;
1883
1884           /* Function decls tend to have longer names.  */
1885           if (TREE_CODE (t) == FUNCTION_DECL)
1886             len = 3;
1887           else
1888             len = 2;
1889           i += len;
1890           if (i > 6)
1891             {
1892               fprintf (stderr, "\n\t");
1893               i = len;
1894             }
1895           print_node_brief (stderr, "", t, 0);
1896           if (t == error_mark_node)
1897             break;
1898         }
1899       if (i)
1900         fprintf (stderr, "\n");
1901     }
1902   if (lvl->tags)
1903     {
1904       fprintf (stderr, " tags:\t");
1905       i = 0;
1906       for (t = lvl->tags; t; t = TREE_CHAIN (t))
1907         {
1908           if (TREE_PURPOSE (t) == NULL_TREE)
1909             len = 3;
1910           else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t)))
1911             len = 2;
1912           else
1913             len = 4;
1914           i += len;
1915           if (i > 5)
1916             {
1917               fprintf (stderr, "\n\t");
1918               i = len;
1919             }
1920           if (TREE_PURPOSE (t) == NULL_TREE)
1921             {
1922               print_node_brief (stderr, "<unnamed-typedef", TREE_VALUE (t), 0);
1923               fprintf (stderr, ">");
1924             }
1925           else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t)))
1926             print_node_brief (stderr, "", TREE_VALUE (t), 0);
1927           else
1928             {
1929               print_node_brief (stderr, "<typedef", TREE_PURPOSE (t), 0);
1930               print_node_brief (stderr, "", TREE_VALUE (t), 0);
1931               fprintf (stderr, ">");
1932             }
1933         }
1934       if (i)
1935         fprintf (stderr, "\n");
1936     }
1937   if (lvl->class_shadowed)
1938     {
1939       fprintf (stderr, " class-shadowed:");
1940       for (t = lvl->class_shadowed; t; t = TREE_CHAIN (t))
1941         {
1942           fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1943         }
1944       fprintf (stderr, "\n");
1945     }
1946   if (lvl->type_shadowed)
1947     {
1948       fprintf (stderr, " type-shadowed:");
1949       for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
1950         {
1951           fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1952         }
1953       fprintf (stderr, "\n");
1954     }
1955 }
1956
1957 void
1958 print_other_binding_stack (struct cp_binding_level *stack)
1959 {
1960   struct cp_binding_level *level;
1961   for (level = stack; level != global_binding_level; level = level->level_chain)
1962     {
1963       fprintf (stderr, "binding level ");
1964       fprintf (stderr, HOST_PTR_PRINTF, (void *) level);
1965       fprintf (stderr, "\n");
1966       print_binding_level (level);
1967     }
1968 }
1969
1970 void
1971 print_binding_stack (void)
1972 {
1973   struct cp_binding_level *b;
1974   fprintf (stderr, "current_binding_level=");
1975   fprintf (stderr, HOST_PTR_PRINTF, (void *) current_binding_level);
1976   fprintf (stderr, "\nclass_binding_level=");
1977   fprintf (stderr, HOST_PTR_PRINTF, (void *) class_binding_level);
1978   fprintf (stderr, "\nglobal_binding_level=");
1979   fprintf (stderr, HOST_PTR_PRINTF, (void *) global_binding_level);
1980   fprintf (stderr, "\n");
1981   if (class_binding_level)
1982     {
1983       for (b = class_binding_level; b; b = b->level_chain)
1984         if (b == current_binding_level)
1985           break;
1986       if (b)
1987         b = class_binding_level;
1988       else
1989         b = current_binding_level;
1990     }
1991   else
1992     b = current_binding_level;
1993   print_other_binding_stack (b);
1994   fprintf (stderr, "global:\n");
1995   print_binding_level (global_binding_level);
1996 }
1997
1998 /* Namespace binding access routines: The namespace_bindings field of
1999    the identifier is polymorphic, with three possible values:
2000    NULL_TREE, a list of "cxx_binding"s.  */
2001
2002 /* Push into the scope of the NAME namespace.  If NAME is NULL_TREE, then we
2003    select a name that is unique to this compilation unit.  */
2004
2005 void
2006 push_namespace (tree name)
2007 {
2008   tree d = NULL_TREE;
2009   int need_new = 1;
2010   int implicit_use = 0;
2011   int global = 0;
2012
2013   timevar_push (TV_NAME_LOOKUP);
2014   
2015   if (!global_namespace)
2016     {
2017       /* This must be ::.  */
2018       my_friendly_assert (name == get_identifier ("::"), 377);
2019       global = 1;
2020     }
2021   else if (!name)
2022     {
2023       /* The name of anonymous namespace is unique for the translation
2024          unit.  */
2025       if (!anonymous_namespace_name)
2026         anonymous_namespace_name = get_file_function_name ('N');
2027       name = anonymous_namespace_name;
2028       d = IDENTIFIER_NAMESPACE_VALUE (name);
2029       if (d)
2030         /* Reopening anonymous namespace.  */
2031         need_new = 0;
2032       implicit_use = 1;
2033     }
2034   else
2035     {
2036       /* Check whether this is an extended namespace definition.  */
2037       d = IDENTIFIER_NAMESPACE_VALUE (name);
2038       if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
2039         {
2040           need_new = 0;
2041           if (DECL_NAMESPACE_ALIAS (d))
2042             {
2043               error ("namespace alias `%D' not allowed here, assuming `%D'",
2044                         d, DECL_NAMESPACE_ALIAS (d));
2045               d = DECL_NAMESPACE_ALIAS (d);
2046             }
2047         }
2048     }
2049
2050   if (need_new)
2051     {
2052       /* Make a new namespace, binding the name to it.  */
2053       d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
2054       /* The global namespace is not pushed, and the global binding
2055          level is set elsewhere.  */
2056       if (!global)
2057         {
2058           DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace);
2059           d = pushdecl (d);
2060           pushlevel (0);
2061           declare_namespace_level ();
2062           NAMESPACE_LEVEL (d) = current_binding_level;
2063           VARRAY_TREE_INIT (current_binding_level->static_decls,
2064                             name != std_identifier ? 10 : 200,
2065                             "Static declarations");
2066         }
2067     }
2068   else
2069     resume_binding_level (NAMESPACE_LEVEL (d));
2070
2071   if (implicit_use)
2072     do_using_directive (d);
2073   /* Enter the name space.  */
2074   current_namespace = d;
2075
2076   timevar_pop (TV_NAME_LOOKUP);
2077 }
2078
2079 /* Pop from the scope of the current namespace.  */
2080
2081 void
2082 pop_namespace (void)
2083 {
2084   my_friendly_assert (current_namespace != global_namespace, 20010801);
2085   current_namespace = CP_DECL_CONTEXT (current_namespace);
2086   /* The binding level is not popped, as it might be re-opened later.  */
2087   suspend_binding_level ();
2088 }
2089
2090 /* Push into the scope of the namespace NS, even if it is deeply
2091    nested within another namespace.  */
2092
2093 void
2094 push_nested_namespace (tree ns)
2095 {
2096   if (ns == global_namespace)
2097     push_to_top_level ();
2098   else
2099     {
2100       push_nested_namespace (CP_DECL_CONTEXT (ns));
2101       push_namespace (DECL_NAME (ns));
2102     }
2103 }
2104
2105 /* Pop back from the scope of the namespace NS, which was previously
2106    entered with push_nested_namespace.  */
2107
2108 void
2109 pop_nested_namespace (tree ns)
2110 {
2111   timevar_push (TV_NAME_LOOKUP);
2112   while (ns != global_namespace)
2113     {
2114       pop_namespace ();
2115       ns = CP_DECL_CONTEXT (ns);
2116     }
2117
2118   pop_from_top_level ();
2119   timevar_pop (TV_NAME_LOOKUP);
2120 }
2121
2122 \f
2123 /* Allocate storage for saving a C++ binding.  */
2124 #define cxx_saved_binding_make() \
2125   (ggc_alloc (sizeof (cxx_saved_binding)))
2126
2127 struct cxx_saved_binding GTY(())
2128 {
2129   /* Link that chains saved C++ bindings for a given name into a stack.  */
2130   cxx_saved_binding *previous;
2131   /* The name of the current binding.  */
2132   tree identifier;
2133   /* The binding we're saving.  */
2134   cxx_binding *binding;
2135   tree class_value;
2136   tree real_type_value;
2137 };
2138
2139 /* Subroutines for reverting temporarily to top-level for instantiation
2140    of templates and such.  We actually need to clear out the class- and
2141    local-value slots of all identifiers, so that only the global values
2142    are at all visible.  Simply setting current_binding_level to the global
2143    scope isn't enough, because more binding levels may be pushed.  */
2144 struct saved_scope *scope_chain;
2145
2146 static cxx_saved_binding *
2147 store_bindings (tree names, cxx_saved_binding *old_bindings)
2148 {
2149   tree t;
2150   cxx_saved_binding *search_bindings = old_bindings;
2151
2152   timevar_push (TV_NAME_LOOKUP);
2153   for (t = names; t; t = TREE_CHAIN (t))
2154     {
2155       tree id;
2156       cxx_saved_binding *saved;
2157       cxx_saved_binding *t1;
2158
2159       if (TREE_CODE (t) == TREE_LIST)
2160         id = TREE_PURPOSE (t);
2161       else
2162         id = DECL_NAME (t);
2163
2164       if (!id
2165           /* Note that we may have an IDENTIFIER_CLASS_VALUE even when
2166              we have no IDENTIFIER_BINDING if we have left the class
2167              scope, but cached the class-level declarations.  */
2168           || !(IDENTIFIER_BINDING (id) || IDENTIFIER_CLASS_VALUE (id)))
2169         continue;
2170
2171       for (t1 = search_bindings; t1; t1 = t1->previous)
2172         if (t1->identifier == id)
2173           goto skip_it;
2174
2175       my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 135);
2176       saved = cxx_saved_binding_make ();
2177       saved->previous = old_bindings;
2178       saved->identifier = id;
2179       saved->binding = IDENTIFIER_BINDING (id);
2180       saved->class_value = IDENTIFIER_CLASS_VALUE (id);;
2181       saved->real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
2182       IDENTIFIER_BINDING (id) = NULL;
2183       IDENTIFIER_CLASS_VALUE (id) = NULL_TREE;
2184       old_bindings = saved;
2185     skip_it:
2186       ;
2187     }
2188   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, old_bindings);
2189 }
2190
2191 void
2192 maybe_push_to_top_level (int pseudo)
2193 {
2194   struct saved_scope *s;
2195   struct cp_binding_level *b;
2196   cxx_saved_binding *old_bindings;
2197   int need_pop;
2198
2199   timevar_push (TV_NAME_LOOKUP);
2200   s = (struct saved_scope *) ggc_alloc_cleared (sizeof (struct saved_scope));
2201
2202   b = scope_chain ? current_binding_level : 0;
2203
2204   /* If we're in the middle of some function, save our state.  */
2205   if (cfun)
2206     {
2207       need_pop = 1;
2208       push_function_context_to (NULL_TREE);
2209     }
2210   else
2211     need_pop = 0;
2212
2213   old_bindings = NULL;
2214   if (scope_chain && previous_class_type)
2215     old_bindings = store_bindings (previous_class_values, old_bindings);
2216
2217   /* Have to include global_binding_level, because class-level decls
2218      aren't listed anywhere useful.  */
2219   for (; b; b = b->level_chain)
2220     {
2221       tree t;
2222
2223       /* Template IDs are inserted into the global level. If they were
2224          inserted into namespace level, finish_file wouldn't find them
2225          when doing pending instantiations. Therefore, don't stop at
2226          namespace level, but continue until :: .  */
2227       if (b == global_binding_level || (pseudo && b->template_parms_p))
2228         break;
2229
2230       old_bindings = store_bindings (b->names, old_bindings);
2231       /* We also need to check class_shadowed to save class-level type
2232          bindings, since pushclass doesn't fill in b->names.  */
2233       if (b->parm_flag == 2)
2234         old_bindings = store_bindings (b->class_shadowed, old_bindings);
2235
2236       /* Unwind type-value slots back to top level.  */
2237       for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
2238         SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
2239     }
2240   s->prev = scope_chain;
2241   s->old_bindings = old_bindings;
2242   s->bindings = b;
2243   s->need_pop_function_context = need_pop;
2244   s->function_decl = current_function_decl;
2245   s->last_parms = last_function_parms;
2246   s->check_access = flag_access_control;
2247
2248   scope_chain = s;
2249   current_function_decl = NULL_TREE;
2250   VARRAY_TREE_INIT (current_lang_base, 10, "current_lang_base");
2251   current_lang_name = lang_name_cplusplus;
2252   current_namespace = global_namespace;
2253   timevar_pop (TV_NAME_LOOKUP);
2254 }
2255
2256 void
2257 push_to_top_level (void)
2258 {
2259   maybe_push_to_top_level (0);
2260 }
2261
2262 void
2263 pop_from_top_level (void)
2264 {
2265   struct saved_scope *s = scope_chain;
2266   cxx_saved_binding *saved;
2267
2268   timevar_push (TV_NAME_LOOKUP); 
2269   /* Clear out class-level bindings cache.  */
2270   if (previous_class_type)
2271     invalidate_class_lookup_cache ();
2272
2273   current_lang_base = 0;
2274
2275   scope_chain = s->prev;
2276   for (saved = s->old_bindings; saved; saved = saved->previous)
2277     {
2278       tree id = saved->identifier;
2279
2280       IDENTIFIER_BINDING (id) = saved->binding;
2281       IDENTIFIER_CLASS_VALUE (id) = saved->class_value;
2282       SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
2283     }
2284
2285   /* If we were in the middle of compiling a function, restore our
2286      state.  */
2287   if (s->need_pop_function_context)
2288     pop_function_context_from (NULL_TREE);
2289   current_function_decl = s->function_decl;
2290   last_function_parms = s->last_parms;
2291   timevar_pop (TV_NAME_LOOKUP);
2292 }
2293 \f
2294 /* Push a definition of struct, union or enum tag "name".
2295    into binding_level "b".   "type" should be the type node,
2296    We assume that the tag "name" is not already defined.
2297
2298    Note that the definition may really be just a forward reference.
2299    In that case, the TYPE_SIZE will be a NULL_TREE.
2300
2301    C++ gratuitously puts all these tags in the name space.  */
2302
2303 /* When setting the IDENTIFIER_TYPE_VALUE field of an identifier ID,
2304    record the shadowed value for this binding contour.  TYPE is
2305    the type that ID maps to.  */
2306
2307 static void
2308 set_identifier_type_value_with_scope (tree id, 
2309                                       tree type, 
2310                                       struct cp_binding_level* b)
2311 {
2312   if (!b->namespace_p)
2313     {
2314       /* Shadow the marker, not the real thing, so that the marker
2315          gets restored later.  */
2316       tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
2317       b->type_shadowed
2318         = tree_cons (id, old_type_value, b->type_shadowed);
2319     }
2320   else
2321     {
2322       cxx_binding *binding =
2323          binding_for_name (NAMESPACE_LEVEL (current_namespace), id);
2324       BINDING_TYPE (binding) = type;
2325       /* Store marker instead of real type.  */
2326       type = global_type_node;
2327     }
2328   SET_IDENTIFIER_TYPE_VALUE (id, type);
2329 }
2330
2331 /* As set_identifier_type_value_with_scope, but using current_binding_level.  */
2332
2333 void
2334 set_identifier_type_value (tree id, tree type)
2335 {
2336   set_identifier_type_value_with_scope (id, type, current_binding_level);
2337 }
2338
2339 /* Return the type associated with id.  */
2340
2341 tree
2342 identifier_type_value (tree id)
2343 {
2344   timevar_push (TV_NAME_LOOKUP);
2345   /* There is no type with that name, anywhere.  */
2346   if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
2347     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2348   /* This is not the type marker, but the real thing.  */
2349   if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
2350     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, REAL_IDENTIFIER_TYPE_VALUE (id));
2351   /* Have to search for it. It must be on the global level, now.
2352      Ask lookup_name not to return non-types. */
2353   id = lookup_name_real (id, 2, 1, 0, LOOKUP_COMPLAIN);
2354   if (id)
2355     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_TYPE (id));
2356   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2357 }
2358
2359 /* Pop off extraneous binding levels left over due to syntax errors.
2360
2361    We don't pop past namespaces, as they might be valid.  */
2362
2363 void
2364 pop_everything (void)
2365 {
2366 #ifdef DEBUG_BINDING_LEVELS
2367   fprintf (stderr, "XXX entering pop_everything ()\n");
2368 #endif
2369   while (!toplevel_bindings_p ())
2370     {
2371       if (current_binding_level->parm_flag == 2)
2372         pop_nested_class ();
2373       else
2374         poplevel (0, 0, 0);
2375     }
2376 #ifdef DEBUG_BINDING_LEVELS
2377   fprintf (stderr, "XXX leaving pop_everything ()\n");
2378 #endif
2379 }
2380
2381 /* The type TYPE is being declared.  If it is a class template, or a
2382    specialization of a class template, do any processing required and
2383    perform error-checking.  If IS_FRIEND is nonzero, this TYPE is
2384    being declared a friend.  B is the binding level at which this TYPE
2385    should be bound.
2386
2387    Returns the TYPE_DECL for TYPE, which may have been altered by this
2388    processing.  */
2389
2390 static tree
2391 maybe_process_template_type_declaration (tree type, 
2392                                          int globalize, 
2393                                          struct cp_binding_level* b)
2394 {
2395   tree decl = TYPE_NAME (type);
2396
2397   if (processing_template_parmlist)
2398     /* You can't declare a new template type in a template parameter
2399        list.  But, you can declare a non-template type:
2400
2401          template <class A*> struct S;
2402
2403        is a forward-declaration of `A'.  */
2404     ;
2405   else
2406     {
2407       maybe_check_template_type (type);
2408
2409       my_friendly_assert (IS_AGGR_TYPE (type)
2410                           || TREE_CODE (type) == ENUMERAL_TYPE, 0);
2411
2412
2413       if (processing_template_decl)
2414         {
2415           /* This may change after the call to
2416              push_template_decl_real, but we want the original value.  */
2417           tree name = DECL_NAME (decl);
2418
2419           decl = push_template_decl_real (decl, globalize);
2420           /* If the current binding level is the binding level for the
2421              template parameters (see the comment in
2422              begin_template_parm_list) and the enclosing level is a class
2423              scope, and we're not looking at a friend, push the
2424              declaration of the member class into the class scope.  In the
2425              friend case, push_template_decl will already have put the
2426              friend into global scope, if appropriate.  */
2427           if (TREE_CODE (type) != ENUMERAL_TYPE
2428               && !globalize && b->template_parms_p
2429               && b->level_chain->parm_flag == 2)
2430             {
2431               finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
2432               /* Put this tag on the list of tags for the class, since
2433                  that won't happen below because B is not the class
2434                  binding level, but is instead the pseudo-global level.  */
2435               b->level_chain->tags =
2436                 tree_cons (name, type, b->level_chain->tags);
2437               if (!COMPLETE_TYPE_P (current_class_type))
2438                 {
2439                   maybe_add_class_template_decl_list (current_class_type,
2440                                                       type, /*friend_p=*/0);
2441                   CLASSTYPE_TAGS (current_class_type) = b->level_chain->tags;
2442                 }
2443             }
2444         }
2445     }
2446
2447   return decl;
2448 }
2449
2450 /* In C++, you don't have to write `struct S' to refer to `S'; you
2451    can just use `S'.  We accomplish this by creating a TYPE_DECL as
2452    if the user had written `typedef struct S S'.  Create and return
2453    the TYPE_DECL for TYPE.  */
2454
2455 tree
2456 create_implicit_typedef (tree name, tree type)
2457 {
2458   tree decl;
2459
2460   decl = build_decl (TYPE_DECL, name, type);
2461   DECL_ARTIFICIAL (decl) = 1;
2462   /* There are other implicit type declarations, like the one *within*
2463      a class that allows you to write `S::S'.  We must distinguish
2464      amongst these.  */
2465   SET_DECL_IMPLICIT_TYPEDEF_P (decl);
2466   TYPE_NAME (type) = decl;
2467
2468   return decl;
2469 }
2470
2471 /* Remember a local name for name-mangling purposes.  */
2472
2473 static void
2474 push_local_name (tree decl)
2475 {
2476   size_t i, nelts;
2477   tree t, name;
2478
2479   timevar_push (TV_NAME_LOOKUP);
2480   if (!local_names)
2481     VARRAY_TREE_INIT (local_names, 8, "local_names");
2482
2483   name = DECL_NAME (decl);
2484
2485   nelts = VARRAY_ACTIVE_SIZE (local_names);
2486   for (i = 0; i < nelts; i++)
2487     {
2488       t = VARRAY_TREE (local_names, i);
2489       if (DECL_NAME (t) == name)
2490         {
2491           if (!DECL_LANG_SPECIFIC (decl))
2492             retrofit_lang_decl (decl);
2493           DECL_LANG_SPECIFIC (decl)->decl_flags.u2sel = 1;
2494           if (DECL_LANG_SPECIFIC (t))
2495             DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1;
2496           else
2497             DECL_DISCRIMINATOR (decl) = 1;
2498
2499           VARRAY_TREE (local_names, i) = decl;
2500           timevar_pop (TV_NAME_LOOKUP);
2501           return;
2502         }
2503     }
2504
2505   VARRAY_PUSH_TREE (local_names, decl);
2506   timevar_pop (TV_NAME_LOOKUP);
2507 }
2508
2509 /* Push a tag name NAME for struct/class/union/enum type TYPE.
2510    Normally put it into the inner-most non-tag-transparent scope,
2511    but if GLOBALIZE is true, put it in the inner-most non-class scope.
2512    The latter is needed for implicit declarations.  */
2513
2514 void
2515 pushtag (tree name, tree type, int globalize)
2516 {
2517   register struct cp_binding_level *b;
2518
2519   timevar_push (TV_NAME_LOOKUP);
2520   b = current_binding_level;
2521   while (b->tag_transparent
2522          || (b->parm_flag == 2
2523              && (globalize
2524                  /* We may be defining a new type in the initializer
2525                     of a static member variable. We allow this when
2526                     not pedantic, and it is particularly useful for
2527                     type punning via an anonymous union.  */
2528                  || COMPLETE_TYPE_P (b->this_class))))
2529     b = b->level_chain;
2530
2531   b->tags = tree_cons (name, type, b->tags);
2532
2533   if (name)
2534     {
2535       /* Do C++ gratuitous typedefing.  */
2536       if (IDENTIFIER_TYPE_VALUE (name) != type)
2537         {
2538           register tree d = NULL_TREE;
2539           int in_class = 0;
2540           tree context = TYPE_CONTEXT (type);
2541
2542           if (! context)
2543             {
2544               tree cs = current_scope ();
2545
2546               if (! globalize)
2547                 context = cs;
2548               else if (cs != NULL_TREE && TYPE_P (cs))
2549                 /* When declaring a friend class of a local class, we want
2550                    to inject the newly named class into the scope
2551                    containing the local class, not the namespace scope.  */
2552                 context = decl_function_context (get_type_decl (cs));
2553             }
2554           if (!context)
2555             context = current_namespace;
2556
2557           if ((b->template_parms_p && b->level_chain->parm_flag == 2)
2558               || b->parm_flag == 2)
2559             in_class = 1;
2560
2561           if (current_lang_name == lang_name_java)
2562             TYPE_FOR_JAVA (type) = 1;
2563
2564           d = create_implicit_typedef (name, type);
2565           DECL_CONTEXT (d) = FROB_CONTEXT (context);
2566           if (! in_class)
2567             set_identifier_type_value_with_scope (name, type, b);
2568
2569           d = maybe_process_template_type_declaration (type,
2570                                                        globalize, b);
2571
2572           if (b->parm_flag == 2)
2573             {
2574               if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
2575                 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
2576                    class.  But if it's a member template class, we
2577                    want the TEMPLATE_DECL, not the TYPE_DECL, so this
2578                    is done later.  */
2579                 finish_member_declaration (d);
2580               else
2581                 pushdecl_class_level (d);
2582             }
2583           else
2584             d = pushdecl_with_scope (d, b);
2585
2586           /* FIXME what if it gets a name from typedef?  */
2587           if (ANON_AGGRNAME_P (name))
2588             DECL_IGNORED_P (d) = 1;
2589
2590           TYPE_CONTEXT (type) = DECL_CONTEXT (d);
2591
2592           /* If this is a local class, keep track of it.  We need this
2593              information for name-mangling, and so that it is possible to find
2594              all function definitions in a translation unit in a convenient
2595              way.  (It's otherwise tricky to find a member function definition
2596              it's only pointed to from within a local class.)  */
2597           if (TYPE_CONTEXT (type)
2598               && TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL
2599               && !processing_template_decl)
2600             VARRAY_PUSH_TREE (local_classes, type);
2601         }
2602       if (b->parm_flag == 2)
2603         {
2604           if (!COMPLETE_TYPE_P (current_class_type))
2605             {
2606               maybe_add_class_template_decl_list (current_class_type,
2607                                                   type, /*friend_p=*/0);
2608               CLASSTYPE_TAGS (current_class_type) = b->tags;
2609             }
2610         }
2611     }
2612
2613   if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL)
2614     /* Use the canonical TYPE_DECL for this node.  */
2615     TYPE_STUB_DECL (type) = TYPE_NAME (type);
2616   else
2617     {
2618       /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE
2619          will be the tagged type we just added to the current
2620          binding level.  This fake NULL-named TYPE_DECL node helps
2621          dwarfout.c to know when it needs to output a
2622          representation of a tagged type, and it also gives us a
2623          convenient place to record the "scope start" address for
2624          the tagged type.  */
2625
2626       tree d = build_decl (TYPE_DECL, NULL_TREE, type);
2627       TYPE_STUB_DECL (type) = pushdecl_with_scope (d, b);
2628     }
2629   timevar_pop (TV_NAME_LOOKUP);
2630 }
2631
2632 /* Counter used to create anonymous type names.  */
2633
2634 static GTY(()) int anon_cnt;
2635
2636 /* Return an IDENTIFIER which can be used as a name for
2637    anonymous structs and unions.  */
2638
2639 tree
2640 make_anon_name (void)
2641 {
2642   char buf[32];
2643
2644   sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
2645   return get_identifier (buf);
2646 }
2647
2648 /* Clear the TREE_PURPOSE slot of tags which have anonymous typenames.
2649    This keeps dbxout from getting confused.  */
2650
2651 void
2652 clear_anon_tags (void)
2653 {
2654   register struct cp_binding_level *b;
2655   register tree tags;
2656   static int last_cnt = 0;
2657
2658   /* Fast out if no new anon names were declared.  */
2659   if (last_cnt == anon_cnt)
2660     return;
2661
2662   b = current_binding_level;
2663   while (b->tag_transparent)
2664     b = b->level_chain;
2665   tags = b->tags;
2666   while (tags)
2667     {
2668       /* A NULL purpose means we have already processed all tags
2669          from here to the end of the list.  */
2670       if (TREE_PURPOSE (tags) == NULL_TREE)
2671         break;
2672       if (ANON_AGGRNAME_P (TREE_PURPOSE (tags)))
2673         TREE_PURPOSE (tags) = NULL_TREE;
2674       tags = TREE_CHAIN (tags);
2675     }
2676   last_cnt = anon_cnt;
2677 }
2678 \f
2679 /* Subroutine of duplicate_decls: return truthvalue of whether
2680    or not types of these decls match.
2681
2682    For C++, we must compare the parameter list so that `int' can match
2683    `int&' in a parameter position, but `int&' is not confused with
2684    `const int&'.  */
2685
2686 int
2687 decls_match (tree newdecl, tree olddecl)
2688 {
2689   int types_match;
2690
2691   if (newdecl == olddecl)
2692     return 1;
2693
2694   if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
2695     /* If the two DECLs are not even the same kind of thing, we're not
2696        interested in their types.  */
2697     return 0;
2698
2699   if (TREE_CODE (newdecl) == FUNCTION_DECL)
2700     {
2701       tree f1 = TREE_TYPE (newdecl);
2702       tree f2 = TREE_TYPE (olddecl);
2703       tree p1 = TYPE_ARG_TYPES (f1);
2704       tree p2 = TYPE_ARG_TYPES (f2);
2705
2706       if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
2707           && ! (DECL_EXTERN_C_P (newdecl)
2708                 && DECL_EXTERN_C_P (olddecl)))
2709         return 0;
2710
2711       if (TREE_CODE (f1) != TREE_CODE (f2))
2712         return 0;
2713
2714       if (same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
2715         {
2716           if (p2 == NULL_TREE && DECL_EXTERN_C_P (olddecl)
2717               && (DECL_BUILT_IN (olddecl)
2718 #ifndef NO_IMPLICIT_EXTERN_C
2719                   || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl))
2720                   || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl))
2721 #endif
2722               ))
2723             {
2724               types_match = self_promoting_args_p (p1);
2725               if (p1 == void_list_node)
2726                 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
2727             }
2728 #ifndef NO_IMPLICIT_EXTERN_C
2729           else if (p1 == NULL_TREE
2730                    && (DECL_EXTERN_C_P (olddecl)
2731                        && DECL_IN_SYSTEM_HEADER (olddecl)
2732                        && !DECL_CLASS_SCOPE_P (olddecl))
2733                    && (DECL_EXTERN_C_P (newdecl)
2734                        && DECL_IN_SYSTEM_HEADER (newdecl)
2735                        && !DECL_CLASS_SCOPE_P (newdecl)))
2736             {
2737               types_match = self_promoting_args_p (p2);
2738               TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
2739             }
2740 #endif
2741           else
2742             types_match = compparms (p1, p2);
2743         }
2744       else
2745         types_match = 0;
2746     }
2747   else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2748     {
2749       if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
2750                                 DECL_TEMPLATE_PARMS (olddecl)))
2751         return 0;
2752
2753       if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl))
2754           != TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)))
2755         return 0;
2756
2757       if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2758         types_match = 1;
2759       else
2760         types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
2761                                    DECL_TEMPLATE_RESULT (newdecl));
2762     }
2763   else
2764     {
2765       if (TREE_TYPE (newdecl) == error_mark_node)
2766         types_match = TREE_TYPE (olddecl) == error_mark_node;
2767       else if (TREE_TYPE (olddecl) == NULL_TREE)
2768         types_match = TREE_TYPE (newdecl) == NULL_TREE;
2769       else if (TREE_TYPE (newdecl) == NULL_TREE)
2770         types_match = 0;
2771       else
2772         types_match = comptypes (TREE_TYPE (newdecl),
2773                                  TREE_TYPE (olddecl),
2774                                  COMPARE_REDECLARATION);
2775     }
2776
2777   return types_match;
2778 }
2779
2780 /* If NEWDECL is `static' and an `extern' was seen previously,
2781    warn about it.  OLDDECL is the previous declaration.
2782
2783    Note that this does not apply to the C++ case of declaring
2784    a variable `extern const' and then later `const'.
2785
2786    Don't complain about built-in functions, since they are beyond
2787    the user's control.  */
2788
2789 static void
2790 warn_extern_redeclared_static (tree newdecl, tree olddecl)
2791 {
2792   static const char *const explicit_extern_static_warning
2793     = "`%D' was declared `extern' and later `static'";
2794   static const char *const implicit_extern_static_warning
2795     = "`%D' was declared implicitly `extern' and later `static'";
2796
2797   tree name;
2798
2799   if (TREE_CODE (newdecl) == TYPE_DECL
2800       || TREE_CODE (newdecl) == TEMPLATE_DECL
2801       || TREE_CODE (newdecl) == CONST_DECL)
2802     return;
2803
2804   /* Don't get confused by static member functions; that's a different
2805      use of `static'.  */
2806   if (TREE_CODE (newdecl) == FUNCTION_DECL
2807       && DECL_STATIC_FUNCTION_P (newdecl))
2808     return;
2809
2810   /* If the old declaration was `static', or the new one isn't, then
2811      then everything is OK.  */
2812   if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
2813     return;
2814
2815   /* It's OK to declare a builtin function as `static'.  */
2816   if (TREE_CODE (olddecl) == FUNCTION_DECL
2817       && DECL_ARTIFICIAL (olddecl))
2818     return;
2819
2820   name = DECL_ASSEMBLER_NAME (newdecl);
2821   pedwarn (IDENTIFIER_IMPLICIT_DECL (name)
2822               ? implicit_extern_static_warning
2823               : explicit_extern_static_warning, newdecl);
2824   cp_pedwarn_at ("previous declaration of `%D'", olddecl);
2825 }
2826
2827 /* Handle when a new declaration NEWDECL has the same name as an old
2828    one OLDDECL in the same binding contour.  Prints an error message
2829    if appropriate.
2830
2831    If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
2832    Otherwise, return 0.  */
2833
2834 int
2835 duplicate_decls (tree newdecl, tree olddecl)
2836 {
2837   unsigned olddecl_uid = DECL_UID (olddecl);
2838   int olddecl_friend = 0, types_match = 0;
2839   int new_defines_function = 0;
2840
2841   if (newdecl == olddecl)
2842     return 1;
2843
2844   types_match = decls_match (newdecl, olddecl);
2845
2846   /* If either the type of the new decl or the type of the old decl is an
2847      error_mark_node, then that implies that we have already issued an
2848      error (earlier) for some bogus type specification, and in that case,
2849      it is rather pointless to harass the user with yet more error message
2850      about the same declaration, so just pretend the types match here.  */
2851   if (TREE_TYPE (newdecl) == error_mark_node
2852       || TREE_TYPE (olddecl) == error_mark_node)
2853     types_match = 1;
2854
2855   if (DECL_P (olddecl)
2856       && TREE_CODE (newdecl) == FUNCTION_DECL
2857       && TREE_CODE (olddecl) == FUNCTION_DECL
2858       && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
2859     {
2860       if (DECL_DECLARED_INLINE_P (newdecl)
2861           && DECL_UNINLINABLE (newdecl)
2862           && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
2863         /* Already warned elsewhere.  */;
2864       else if (DECL_DECLARED_INLINE_P (olddecl)
2865                && DECL_UNINLINABLE (olddecl)
2866                && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2867         /* Already warned.  */;
2868       else if (DECL_DECLARED_INLINE_P (newdecl)
2869                && DECL_UNINLINABLE (olddecl)
2870                && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2871         {
2872           warning ("%Hfunction '%D' redeclared as inline",
2873                    &DECL_SOURCE_LOCATION (newdecl), newdecl);
2874           warning ("%Hprevious declaration of '%D' with attribute noinline",
2875                    &DECL_SOURCE_LOCATION (olddecl), olddecl);
2876         }
2877       else if (DECL_DECLARED_INLINE_P (olddecl)
2878                && DECL_UNINLINABLE (newdecl)
2879                && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
2880         {
2881           warning ("%Hfunction '%D' redeclared with attribute noinline",
2882                    &DECL_SOURCE_LOCATION (newdecl), newdecl);
2883           warning ("%Hprevious declaration of '%D' was inline",
2884                    &DECL_SOURCE_LOCATION (olddecl), olddecl);
2885         }
2886     }
2887
2888   /* Check for redeclaration and other discrepancies.  */
2889   if (TREE_CODE (olddecl) == FUNCTION_DECL
2890       && DECL_ARTIFICIAL (olddecl))
2891     {
2892       if (TREE_CODE (newdecl) != FUNCTION_DECL)
2893         {
2894           /* Avoid warnings redeclaring anticipated built-ins.  */
2895           if (DECL_ANTICIPATED (olddecl))
2896             return 0;
2897
2898           /* If you declare a built-in or predefined function name as static,
2899              the old definition is overridden, but optionally warn this was a
2900              bad choice of name.  */
2901           if (! TREE_PUBLIC (newdecl))
2902             {
2903               if (warn_shadow)
2904                 warning ("shadowing %s function `%#D'",
2905                             DECL_BUILT_IN (olddecl) ? "built-in" : "library",
2906                             olddecl);
2907               /* Discard the old built-in function.  */
2908               return 0;
2909             }
2910           /* If the built-in is not ansi, then programs can override
2911              it even globally without an error.  */
2912           else if (! DECL_BUILT_IN (olddecl))
2913             warning ("library function `%#D' redeclared as non-function `%#D'",
2914                         olddecl, newdecl);
2915           else
2916             {
2917               error ("declaration of `%#D'", newdecl);
2918               error ("conflicts with built-in declaration `%#D'",
2919                         olddecl);
2920             }
2921           return 0;
2922         }
2923       else if (!types_match)
2924         {
2925           /* Avoid warnings redeclaring anticipated built-ins.  */
2926           if (DECL_ANTICIPATED (olddecl))
2927             ;  /* Do nothing yet.  */
2928           else if ((DECL_EXTERN_C_P (newdecl)
2929                && DECL_EXTERN_C_P (olddecl))
2930               || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
2931                             TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
2932             {
2933               /* A near match; override the builtin.  */
2934
2935               if (TREE_PUBLIC (newdecl))
2936                 {
2937                   warning ("new declaration `%#D'", newdecl);
2938                   warning ("ambiguates built-in declaration `%#D'",
2939                               olddecl);
2940                 }
2941               else if (warn_shadow)
2942                 warning ("shadowing %s function `%#D'",
2943                             DECL_BUILT_IN (olddecl) ? "built-in" : "library",
2944                             olddecl);
2945             }
2946           else
2947             /* Discard the old built-in function.  */
2948             return 0;
2949
2950           /* Replace the old RTL to avoid problems with inlining.  */
2951           SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
2952         }
2953       /* Even if the types match, prefer the new declarations type
2954          for anitipated built-ins, for exception lists, etc...  */
2955       else if (DECL_ANTICIPATED (olddecl))
2956         TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
2957
2958       if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
2959         {
2960           /* If a builtin function is redeclared as `static', merge
2961              the declarations, but make the original one static.  */
2962           DECL_THIS_STATIC (olddecl) = 1;
2963           TREE_PUBLIC (olddecl) = 0;
2964
2965           /* Make the old declaration consistent with the new one so
2966              that all remnants of the builtin-ness of this function
2967              will be banished.  */
2968           SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
2969           SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
2970         }
2971     }
2972   else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
2973     {
2974       if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
2975            && TREE_CODE (newdecl) != TYPE_DECL
2976            && ! (TREE_CODE (newdecl) == TEMPLATE_DECL
2977                  && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL))
2978           || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
2979               && TREE_CODE (olddecl) != TYPE_DECL
2980               && ! (TREE_CODE (olddecl) == TEMPLATE_DECL
2981                     && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
2982                         == TYPE_DECL))))
2983         {
2984           /* We do nothing special here, because C++ does such nasty
2985              things with TYPE_DECLs.  Instead, just let the TYPE_DECL
2986              get shadowed, and know that if we need to find a TYPE_DECL
2987              for a given name, we can look in the IDENTIFIER_TYPE_VALUE
2988              slot of the identifier.  */
2989           return 0;
2990         }
2991
2992       if ((TREE_CODE (newdecl) == FUNCTION_DECL
2993            && DECL_FUNCTION_TEMPLATE_P (olddecl))
2994           || (TREE_CODE (olddecl) == FUNCTION_DECL
2995               && DECL_FUNCTION_TEMPLATE_P (newdecl)))
2996         return 0;
2997
2998       error ("`%#D' redeclared as different kind of symbol", newdecl);
2999       if (TREE_CODE (olddecl) == TREE_LIST)
3000         olddecl = TREE_VALUE (olddecl);
3001       cp_error_at ("previous declaration of `%#D'", olddecl);
3002
3003       /* New decl is completely inconsistent with the old one =>
3004          tell caller to replace the old one.  */
3005
3006       return 0;
3007     }
3008   else if (!types_match)
3009     {
3010       if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
3011         /* These are certainly not duplicate declarations; they're
3012            from different scopes.  */
3013         return 0;
3014
3015       if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3016         {
3017           /* The name of a class template may not be declared to refer to
3018              any other template, class, function, object, namespace, value,
3019              or type in the same scope.  */
3020           if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
3021               || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
3022             {
3023               error ("declaration of template `%#D'", newdecl);
3024               cp_error_at ("conflicts with previous declaration `%#D'",
3025                            olddecl);
3026             }
3027           else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
3028                    && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
3029                    && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
3030                                  TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
3031                    && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
3032                                            DECL_TEMPLATE_PARMS (olddecl))
3033                    /* Template functions can be disambiguated by
3034                       return type.  */
3035                    && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
3036                                    TREE_TYPE (TREE_TYPE (olddecl))))
3037             {
3038               error ("new declaration `%#D'", newdecl);
3039               cp_error_at ("ambiguates old declaration `%#D'", olddecl);
3040             }
3041           return 0;
3042         }
3043       if (TREE_CODE (newdecl) == FUNCTION_DECL)
3044         {
3045           if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
3046             {
3047               error ("declaration of C function `%#D' conflicts with",
3048                         newdecl);
3049               cp_error_at ("previous declaration `%#D' here", olddecl);
3050             }
3051           else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
3052                               TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
3053             {
3054               error ("new declaration `%#D'", newdecl);
3055               cp_error_at ("ambiguates old declaration `%#D'", olddecl);
3056             }
3057           else
3058             return 0;
3059         }
3060
3061       /* Already complained about this, so don't do so again.  */
3062       else if (current_class_type == NULL_TREE
3063           || IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (newdecl)) != current_class_type)
3064         {
3065           error ("conflicting types for `%#D'", newdecl);
3066           cp_error_at ("previous declaration as `%#D'", olddecl);
3067         }
3068     }
3069   else if (TREE_CODE (newdecl) == FUNCTION_DECL
3070             && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
3071                  && (!DECL_TEMPLATE_INFO (newdecl)
3072                      || (DECL_TI_TEMPLATE (newdecl)
3073                          != DECL_TI_TEMPLATE (olddecl))))
3074                 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
3075                     && (!DECL_TEMPLATE_INFO (olddecl)
3076                         || (DECL_TI_TEMPLATE (olddecl)
3077                             != DECL_TI_TEMPLATE (newdecl))))))
3078     /* It's OK to have a template specialization and a non-template
3079        with the same type, or to have specializations of two
3080        different templates with the same type.  Note that if one is a
3081        specialization, and the other is an instantiation of the same
3082        template, that we do not exit at this point.  That situation
3083        can occur if we instantiate a template class, and then
3084        specialize one of its methods.  This situation is valid, but
3085        the declarations must be merged in the usual way.  */
3086     return 0;
3087   else if (TREE_CODE (newdecl) == FUNCTION_DECL
3088            && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
3089                 && !DECL_USE_TEMPLATE (newdecl))
3090                || (DECL_TEMPLATE_INSTANTIATION (newdecl)
3091                    && !DECL_USE_TEMPLATE (olddecl))))
3092     /* One of the declarations is a template instantiation, and the
3093        other is not a template at all.  That's OK.  */
3094     return 0;
3095   else if (TREE_CODE (newdecl) == NAMESPACE_DECL
3096            && DECL_NAMESPACE_ALIAS (newdecl)
3097            && DECL_NAMESPACE_ALIAS (newdecl) == DECL_NAMESPACE_ALIAS (olddecl))
3098     /* Redeclaration of namespace alias, ignore it.  */
3099     return 1;
3100   else
3101     {
3102       const char *errmsg = redeclaration_error_message (newdecl, olddecl);
3103       if (errmsg)
3104         {
3105           error (errmsg, newdecl);
3106           if (DECL_NAME (olddecl) != NULL_TREE)
3107             cp_error_at ((DECL_INITIAL (olddecl)
3108                           && namespace_bindings_p ())
3109                          ? "`%#D' previously defined here"
3110                          : "`%#D' previously declared here", olddecl);
3111           return 0;
3112         }
3113       else if (TREE_CODE (olddecl) == FUNCTION_DECL
3114                && DECL_INITIAL (olddecl) != NULL_TREE
3115                && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE
3116                && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE)
3117         {
3118           /* Prototype decl follows defn w/o prototype.  */
3119           cp_warning_at ("prototype for `%#D'", newdecl);
3120           cp_warning_at ("follows non-prototype definition here", olddecl);
3121         }
3122       else if (TREE_CODE (olddecl) == FUNCTION_DECL
3123                && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
3124         {
3125           /* extern "C" int foo ();
3126              int foo () { bar (); }
3127              is OK.  */
3128           if (current_lang_depth () == 0)
3129             SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
3130           else
3131             {
3132               cp_error_at ("previous declaration of `%#D' with %L linkage",
3133                            olddecl, DECL_LANGUAGE (olddecl));
3134               error ("conflicts with new declaration with %L linkage",
3135                         DECL_LANGUAGE (newdecl));
3136             }
3137         }
3138
3139       if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
3140         ;
3141       else if (TREE_CODE (olddecl) == FUNCTION_DECL)
3142         {
3143           tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
3144           tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
3145           int i = 1;
3146
3147           if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE)
3148             t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2);
3149
3150           for (; t1 && t1 != void_list_node;
3151                t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
3152             if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
3153               {
3154                 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
3155                                            TREE_PURPOSE (t2)))
3156                   {
3157                     pedwarn ("default argument given for parameter %d of `%#D'",
3158                              i, newdecl);
3159                     cp_pedwarn_at ("after previous specification in `%#D'",
3160                                    olddecl);
3161                   }
3162                 else
3163                   {
3164                     error ("default argument given for parameter %d of `%#D'",
3165                               i, newdecl);
3166                     cp_error_at ("after previous specification in `%#D'",
3167                                  olddecl);
3168                   }
3169               }
3170
3171           if (DECL_DECLARED_INLINE_P (newdecl) 
3172               && ! DECL_DECLARED_INLINE_P (olddecl)
3173               && TREE_ADDRESSABLE (olddecl) && warn_inline)
3174             {
3175               warning ("`%#D' was used before it was declared inline",
3176                           newdecl);
3177               cp_warning_at ("previous non-inline declaration here",
3178                              olddecl);
3179             }
3180         }
3181     }
3182
3183   /* Do not merge an implicit typedef with an explicit one.  In:
3184
3185        class A;
3186        ...
3187        typedef class A A __attribute__ ((foo));
3188
3189      the attribute should apply only to the typedef.  */
3190   if (TREE_CODE (olddecl) == TYPE_DECL
3191       && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
3192           || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
3193     return 0;
3194
3195   /* If new decl is `static' and an `extern' was seen previously,
3196      warn about it.  */
3197   warn_extern_redeclared_static (newdecl, olddecl);
3198
3199   /* We have committed to returning 1 at this point.  */
3200   if (TREE_CODE (newdecl) == FUNCTION_DECL)
3201     {
3202       /* Now that functions must hold information normally held
3203          by field decls, there is extra work to do so that
3204          declaration information does not get destroyed during
3205          definition.  */
3206       if (DECL_VINDEX (olddecl))
3207         DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
3208       if (DECL_CONTEXT (olddecl))
3209         DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
3210       DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
3211       DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
3212       DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
3213       DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
3214       DECL_NEEDS_FINAL_OVERRIDER_P (newdecl) |= DECL_NEEDS_FINAL_OVERRIDER_P (olddecl);
3215       DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
3216       if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK)
3217         SET_OVERLOADED_OPERATOR_CODE
3218           (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl));
3219       new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
3220
3221       /* Optionally warn about more than one declaration for the same
3222          name, but don't warn about a function declaration followed by a
3223          definition.  */
3224       if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
3225           && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
3226           /* Don't warn about extern decl followed by definition.  */
3227           && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
3228           /* Don't warn about friends, let add_friend take care of it.  */
3229           && ! (DECL_FRIEND_P (newdecl) || DECL_FRIEND_P (olddecl)))
3230         {
3231           warning ("redundant redeclaration of `%D' in same scope", newdecl);
3232           cp_warning_at ("previous declaration of `%D'", olddecl);
3233         }
3234     }
3235
3236   /* Deal with C++: must preserve virtual function table size.  */
3237   if (TREE_CODE (olddecl) == TYPE_DECL)
3238     {
3239       register tree newtype = TREE_TYPE (newdecl);
3240       register tree oldtype = TREE_TYPE (olddecl);
3241
3242       if (newtype != error_mark_node && oldtype != error_mark_node
3243           && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
3244         CLASSTYPE_FRIEND_CLASSES (newtype)
3245           = CLASSTYPE_FRIEND_CLASSES (oldtype);
3246 \
3247       DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
3248     }
3249
3250   /* Copy all the DECL_... slots specified in the new decl
3251      except for any that we copy here from the old type.  */
3252   DECL_ATTRIBUTES (newdecl)
3253     = (*targetm.merge_decl_attributes) (olddecl, newdecl);
3254
3255   if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3256     {
3257       TREE_TYPE (olddecl) = TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl));
3258       DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
3259         = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
3260                    DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
3261
3262       /* If the new declaration is a definition, update the file and
3263          line information on the declaration.  */
3264       if (DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)) == NULL_TREE
3265           && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl)) != NULL_TREE)
3266         {
3267           DECL_SOURCE_LOCATION (olddecl) 
3268             = DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (olddecl))
3269             = DECL_SOURCE_LOCATION (newdecl);
3270         }
3271
3272       return 1;
3273     }
3274
3275   if (types_match)
3276     {
3277       /* Automatically handles default parameters.  */
3278       tree oldtype = TREE_TYPE (olddecl);
3279       tree newtype;
3280
3281       /* Merge the data types specified in the two decls.  */
3282       newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
3283
3284       /* If merge_types produces a non-typedef type, just use the old type.  */
3285       if (TREE_CODE (newdecl) == TYPE_DECL
3286           && newtype == DECL_ORIGINAL_TYPE (newdecl))
3287         newtype = oldtype;
3288
3289       if (TREE_CODE (newdecl) == VAR_DECL)
3290         {
3291           DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
3292           DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
3293         }
3294
3295       /* Do this after calling `merge_types' so that default
3296          parameters don't confuse us.  */
3297       else if (TREE_CODE (newdecl) == FUNCTION_DECL
3298           && (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl))
3299               != TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl))))
3300         {
3301           TREE_TYPE (newdecl) = build_exception_variant (newtype,
3302                                                          TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)));
3303           TREE_TYPE (olddecl) = build_exception_variant (newtype,
3304                                                          TYPE_RAISES_EXCEPTIONS (oldtype));
3305
3306           if ((pedantic || ! DECL_IN_SYSTEM_HEADER (olddecl))
3307               && DECL_SOURCE_LINE (olddecl) != 0
3308               && flag_exceptions
3309               && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)),
3310                                      TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl)), 1))
3311             {
3312               error ("declaration of `%F' throws different exceptions",
3313                         newdecl);
3314               cp_error_at ("than previous declaration `%F'", olddecl);
3315             }
3316         }
3317       TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
3318
3319       /* Lay the type out, unless already done.  */
3320       if (! same_type_p (newtype, oldtype)
3321           && TREE_TYPE (newdecl) != error_mark_node
3322           && !(processing_template_decl && uses_template_parms (newdecl)))
3323         layout_type (TREE_TYPE (newdecl));
3324
3325       if ((TREE_CODE (newdecl) == VAR_DECL
3326            || TREE_CODE (newdecl) == PARM_DECL
3327            || TREE_CODE (newdecl) == RESULT_DECL
3328            || TREE_CODE (newdecl) == FIELD_DECL
3329            || TREE_CODE (newdecl) == TYPE_DECL)
3330           && !(processing_template_decl && uses_template_parms (newdecl)))
3331         layout_decl (newdecl, 0);
3332
3333       /* Merge the type qualifiers.  */
3334       if (TREE_READONLY (newdecl))
3335         TREE_READONLY (olddecl) = 1;
3336       if (TREE_THIS_VOLATILE (newdecl))
3337         TREE_THIS_VOLATILE (olddecl) = 1;
3338
3339       /* Merge the initialization information.  */
3340       if (DECL_INITIAL (newdecl) == NULL_TREE
3341           && DECL_INITIAL (olddecl) != NULL_TREE)
3342         {
3343           DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
3344           DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
3345           if (CAN_HAVE_FULL_LANG_DECL_P (newdecl)
3346               && DECL_LANG_SPECIFIC (newdecl)
3347               && DECL_LANG_SPECIFIC (olddecl))
3348             DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
3349         }
3350
3351       /* Merge the section attribute.
3352          We want to issue an error if the sections conflict but that must be
3353          done later in decl_attributes since we are called before attributes
3354          are assigned.  */
3355       if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
3356         DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
3357
3358       if (TREE_CODE (newdecl) == FUNCTION_DECL)
3359         {
3360           DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
3361             |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
3362           DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
3363           TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
3364           TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
3365           TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
3366           DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
3367           DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
3368           /* Keep the old RTL.  */
3369           COPY_DECL_RTL (olddecl, newdecl);
3370         }
3371       else if (TREE_CODE (newdecl) == VAR_DECL 
3372                && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
3373         {
3374           /* Keep the old RTL.  We cannot keep the old RTL if the old
3375              declaration was for an incomplete object and the new
3376              declaration is not since many attributes of the RTL will
3377              change.  */
3378           COPY_DECL_RTL (olddecl, newdecl);
3379         }
3380     }
3381   /* If cannot merge, then use the new type and qualifiers,
3382      and don't preserve the old rtl.  */
3383   else
3384     {
3385       /* Clean out any memory we had of the old declaration.  */
3386       tree oldstatic = value_member (olddecl, static_aggregates);
3387       if (oldstatic)
3388         TREE_VALUE (oldstatic) = error_mark_node;
3389
3390       TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
3391       TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
3392       TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
3393       TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
3394     }
3395
3396   /* Merge the storage class information.  */
3397   merge_weak (newdecl, olddecl);
3398
3399   DECL_ONE_ONLY (newdecl) |= DECL_ONE_ONLY (olddecl);
3400   DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
3401   TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
3402   TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
3403   if (! DECL_EXTERNAL (olddecl))
3404     DECL_EXTERNAL (newdecl) = 0;
3405
3406   if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
3407     {
3408       DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
3409       DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
3410       DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
3411       DECL_TEMPLATE_INSTANTIATED (newdecl)
3412         |= DECL_TEMPLATE_INSTANTIATED (olddecl);
3413       /* Don't really know how much of the language-specific
3414          values we should copy from old to new.  */
3415       DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
3416       DECL_LANG_SPECIFIC (newdecl)->decl_flags.u2 = 
3417         DECL_LANG_SPECIFIC (olddecl)->decl_flags.u2;
3418       DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
3419       DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
3420       DECL_INITIALIZED_IN_CLASS_P (newdecl)
3421         |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
3422       olddecl_friend = DECL_FRIEND_P (olddecl);
3423
3424       /* Only functions have DECL_BEFRIENDING_CLASSES.  */
3425       if (TREE_CODE (newdecl) == FUNCTION_DECL
3426           || DECL_FUNCTION_TEMPLATE_P (newdecl))
3427         {
3428           DECL_BEFRIENDING_CLASSES (newdecl)
3429             = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
3430                        DECL_BEFRIENDING_CLASSES (olddecl));
3431           /* DECL_THUNKS is only valid for virtual functions,
3432              otherwise it is a DECL_FRIEND_CONTEXT.  */
3433           if (DECL_VIRTUAL_P (newdecl))
3434             DECL_THUNKS (newdecl) = DECL_THUNKS (olddecl);
3435         }
3436     }
3437
3438   if (TREE_CODE (newdecl) == FUNCTION_DECL)
3439     {
3440       if (DECL_TEMPLATE_INSTANTIATION (olddecl)
3441           && !DECL_TEMPLATE_INSTANTIATION (newdecl))
3442         {
3443           /* If newdecl is not a specialization, then it is not a
3444              template-related function at all.  And that means that we
3445              should have exited above, returning 0.  */
3446           my_friendly_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl),
3447                               0);
3448
3449           if (TREE_USED (olddecl))
3450             /* From [temp.expl.spec]:
3451
3452                If a template, a member template or the member of a class
3453                template is explicitly specialized then that
3454                specialization shall be declared before the first use of
3455                that specialization that would cause an implicit
3456                instantiation to take place, in every translation unit in
3457                which such a use occurs.  */
3458             error ("explicit specialization of %D after first use",
3459                       olddecl);
3460
3461           SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
3462
3463           /* [temp.expl.spec/14] We don't inline explicit specialization
3464              just because the primary template says so.  */
3465         }
3466       else
3467         {
3468           if (DECL_PENDING_INLINE_INFO (newdecl) == 0)
3469             DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
3470
3471           DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
3472
3473           /* If either decl says `inline', this fn is inline, unless 
3474              its definition was passed already.  */
3475           if (DECL_INLINE (newdecl) && DECL_INITIAL (olddecl) == NULL_TREE)
3476             DECL_INLINE (olddecl) = 1;
3477           DECL_INLINE (newdecl) = DECL_INLINE (olddecl);
3478
3479           DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
3480             = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
3481         }
3482
3483       /* Preserve abstractness on cloned [cd]tors.  */
3484       DECL_ABSTRACT (newdecl) = DECL_ABSTRACT (olddecl);
3485
3486       if (! types_match)
3487         {
3488           SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
3489           COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
3490           SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
3491         }
3492       if (! types_match || new_defines_function)
3493         {
3494           /* These need to be copied so that the names are available.
3495              Note that if the types do match, we'll preserve inline
3496              info and other bits, but if not, we won't.  */
3497           DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
3498           DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
3499         }
3500       if (new_defines_function)
3501         /* If defining a function declared with other language
3502            linkage, use the previously declared language linkage.  */
3503         SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
3504       else if (types_match)
3505         {
3506           /* If redeclaring a builtin function, and not a definition,
3507              it stays built in.  */
3508           if (DECL_BUILT_IN (olddecl))
3509             {
3510               DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
3511               DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
3512               /* If we're keeping the built-in definition, keep the rtl,
3513                  regardless of declaration matches.  */
3514               SET_DECL_RTL (newdecl, DECL_RTL (olddecl));
3515             }
3516           else
3517             DECL_NUM_STMTS (newdecl) = DECL_NUM_STMTS (olddecl);
3518
3519           DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
3520           /* Don't clear out the arguments if we're redefining a function.  */
3521           if (DECL_ARGUMENTS (olddecl))
3522             DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
3523         }
3524     }
3525   else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
3526     NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
3527
3528   /* Now preserve various other info from the definition.  */
3529   TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
3530   TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
3531   DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
3532   COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
3533
3534   if (TREE_CODE (newdecl) == FUNCTION_DECL)
3535     {
3536       int function_size;
3537
3538       function_size = sizeof (struct tree_decl);
3539
3540       memcpy ((char *) olddecl + sizeof (struct tree_common),
3541               (char *) newdecl + sizeof (struct tree_common),
3542               function_size - sizeof (struct tree_common));
3543
3544       if (DECL_TEMPLATE_INSTANTIATION (newdecl))
3545         {
3546           /* If newdecl is a template instantiation, it is possible that
3547              the following sequence of events has occurred:
3548
3549              o A friend function was declared in a class template.  The
3550              class template was instantiated.
3551
3552              o The instantiation of the friend declaration was
3553              recorded on the instantiation list, and is newdecl.
3554
3555              o Later, however, instantiate_class_template called pushdecl
3556              on the newdecl to perform name injection.  But, pushdecl in
3557              turn called duplicate_decls when it discovered that another
3558              declaration of a global function with the same name already
3559              existed.
3560
3561              o Here, in duplicate_decls, we decided to clobber newdecl.
3562
3563              If we're going to do that, we'd better make sure that
3564              olddecl, and not newdecl, is on the list of
3565              instantiations so that if we try to do the instantiation
3566              again we won't get the clobbered declaration.  */
3567
3568           tree tmpl = DECL_TI_TEMPLATE (newdecl);
3569           tree decls = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3570
3571           for (; decls; decls = TREE_CHAIN (decls))
3572             if (TREE_VALUE (decls) == newdecl)
3573               TREE_VALUE (decls) = olddecl;
3574         }
3575     }
3576   else
3577     {
3578       memcpy ((char *) olddecl + sizeof (struct tree_common),
3579               (char *) newdecl + sizeof (struct tree_common),
3580               sizeof (struct tree_decl) - sizeof (struct tree_common)
3581               + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3582     }
3583
3584   DECL_UID (olddecl) = olddecl_uid;
3585   if (olddecl_friend)
3586     DECL_FRIEND_P (olddecl) = 1;
3587
3588   /* NEWDECL contains the merged attribute lists.
3589      Update OLDDECL to be the same.  */
3590   DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
3591
3592   return 1;
3593 }
3594
3595 /* Record a decl-node X as belonging to the current lexical scope.
3596    Check for errors (such as an incompatible declaration for the same
3597    name already seen in the same scope).
3598
3599    Returns either X or an old decl for the same name.
3600    If an old decl is returned, it may have been smashed
3601    to agree with what X says.  */
3602
3603 tree
3604 pushdecl (tree x)
3605 {
3606   register tree t;
3607   register tree name;
3608   int need_new_binding;
3609
3610   timevar_push (TV_NAME_LOOKUP);
3611   /* We shouldn't be calling pushdecl when we're generating RTL for a
3612      function that we already did semantic analysis on previously.  */
3613   my_friendly_assert (!cfun || doing_semantic_analysis_p (),
3614                       19990913);
3615
3616   need_new_binding = 1;
3617
3618   if (DECL_TEMPLATE_PARM_P (x))
3619     /* Template parameters have no context; they are not X::T even
3620        when declared within a class or namespace.  */
3621     ;
3622   else
3623     {
3624       if (current_function_decl && x != current_function_decl
3625           /* A local declaration for a function doesn't constitute
3626              nesting.  */
3627           && !(TREE_CODE (x) == FUNCTION_DECL && !DECL_INITIAL (x))
3628           /* A local declaration for an `extern' variable is in the
3629              scope of the current namespace, not the current
3630              function.  */
3631           && !(TREE_CODE (x) == VAR_DECL && DECL_EXTERNAL (x))
3632           && !DECL_CONTEXT (x))
3633         DECL_CONTEXT (x) = current_function_decl;
3634
3635       /* If this is the declaration for a namespace-scope function,
3636          but the declaration itself is in a local scope, mark the
3637          declaration.  */
3638       if (TREE_CODE (x) == FUNCTION_DECL
3639           && DECL_NAMESPACE_SCOPE_P (x)
3640           && current_function_decl
3641           && x != current_function_decl)
3642         DECL_LOCAL_FUNCTION_P (x) = 1;
3643     }
3644
3645   name = DECL_NAME (x);
3646   if (name)
3647     {
3648       int different_binding_level = 0;
3649
3650       if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3651         name = TREE_OPERAND (name, 0);
3652
3653       /* In case this decl was explicitly namespace-qualified, look it
3654          up in its namespace context.  */
3655       if (TREE_CODE (x) == VAR_DECL && DECL_NAMESPACE_SCOPE_P (x)
3656           && namespace_bindings_p ())
3657         t = namespace_binding (name, DECL_CONTEXT (x));
3658       else
3659         t = lookup_name_current_level (name);
3660
3661       /* [basic.link] If there is a visible declaration of an entity
3662          with linkage having the same name and type, ignoring entities
3663          declared outside the innermost enclosing namespace scope, the
3664          block scope declaration declares that same entity and
3665          receives the linkage of the previous declaration.  */
3666       if (! t && current_function_decl && x != current_function_decl
3667           && (TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
3668           && DECL_EXTERNAL (x))
3669         {
3670           /* Look in block scope.  */
3671           t = IDENTIFIER_VALUE (name);
3672           /* Or in the innermost namespace.  */
3673           if (! t)
3674             t = namespace_binding (name, DECL_CONTEXT (x));
3675           /* Does it have linkage?  Note that if this isn't a DECL, it's an
3676              OVERLOAD, which is OK.  */
3677           if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t)))
3678             t = NULL_TREE;
3679           if (t)
3680             different_binding_level = 1;
3681         }
3682
3683       /* If we are declaring a function, and the result of name-lookup
3684          was an OVERLOAD, look for an overloaded instance that is
3685          actually the same as the function we are declaring.  (If
3686          there is one, we have to merge our declaration with the
3687          previous declaration.)  */
3688       if (t && TREE_CODE (t) == OVERLOAD)
3689         {
3690           tree match;
3691
3692           if (TREE_CODE (x) == FUNCTION_DECL)
3693             for (match = t; match; match = OVL_NEXT (match))
3694               {
3695                 if (decls_match (OVL_CURRENT (match), x))
3696                   break;
3697               }
3698           else
3699             /* Just choose one.  */
3700             match = t;
3701
3702           if (match)
3703             t = OVL_CURRENT (match);
3704           else
3705             t = NULL_TREE;
3706         }
3707
3708       if (t == error_mark_node)
3709         {
3710           /* error_mark_node is 0 for a while during initialization!  */
3711           t = NULL_TREE;
3712           cp_error_at ("`%#D' used prior to declaration", x);
3713         }
3714       else if (t != NULL_TREE)
3715         {
3716           if (different_binding_level)
3717             {
3718               if (decls_match (x, t))
3719                 /* The standard only says that the local extern
3720                    inherits linkage from the previous decl; in
3721                    particular, default args are not shared.  It would
3722                    be nice to propagate inlining info, though.  FIXME.  */
3723                 TREE_PUBLIC (x) = TREE_PUBLIC (t);
3724             }
3725           else if (TREE_CODE (t) == PARM_DECL)
3726             {
3727               if (DECL_CONTEXT (t) == NULL_TREE)
3728                 /* This is probaby caused by too many errors, but calling
3729                    abort will say that if errors have occurred.  */
3730                 abort ();
3731
3732               /* Check for duplicate params.  */
3733               if (duplicate_decls (x, t))
3734                 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3735             }
3736           else if ((DECL_EXTERN_C_FUNCTION_P (x)
3737                     || DECL_FUNCTION_TEMPLATE_P (x))
3738                    && is_overloaded_fn (t))
3739             /* Don't do anything just yet.  */;
3740           else if (t == wchar_decl_node)
3741             {
3742               if (pedantic && ! DECL_IN_SYSTEM_HEADER (x))
3743                 pedwarn ("redeclaration of `wchar_t' as `%T'",
3744                             TREE_TYPE (x));
3745
3746               /* Throw away the redeclaration.  */
3747               POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3748             }
3749           else if (TREE_CODE (t) != TREE_CODE (x))
3750             {
3751               if (duplicate_decls (x, t))
3752                 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3753             }
3754           else if (duplicate_decls (x, t))
3755             {
3756               if (TREE_CODE (t) == TYPE_DECL)
3757                 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
3758               else if (TREE_CODE (t) == FUNCTION_DECL)
3759                 check_default_args (t);
3760
3761               POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3762             }
3763           else if (DECL_MAIN_P (x))
3764             {
3765               /* A redeclaration of main, but not a duplicate of the
3766                  previous one.
3767
3768                  [basic.start.main]
3769
3770                  This function shall not be overloaded.  */
3771               cp_error_at ("invalid redeclaration of `%D'", t);
3772               error ("as `%D'", x);
3773               /* We don't try to push this declaration since that
3774                  causes a crash.  */
3775               POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
3776             }
3777         }
3778
3779       check_template_shadow (x);
3780
3781       /* If this is a function conjured up by the backend, massage it
3782          so it looks friendly.  */
3783       if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x))
3784         {
3785           retrofit_lang_decl (x);
3786           SET_DECL_LANGUAGE (x, lang_c);
3787         }
3788
3789       if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x))
3790         {
3791           t = push_overloaded_decl (x, PUSH_LOCAL);
3792           if (t != x)
3793             POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3794           if (!namespace_bindings_p ())
3795             /* We do not need to create a binding for this name;
3796                push_overloaded_decl will have already done so if
3797                necessary.  */
3798             need_new_binding = 0;
3799         }
3800       else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
3801         {
3802           t = push_overloaded_decl (x, PUSH_GLOBAL);
3803           if (t == x)
3804             add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
3805           POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3806         }
3807
3808       /* If declaring a type as a typedef, copy the type (unless we're
3809          at line 0), and install this TYPE_DECL as the new type's typedef
3810          name.  See the extensive comment in ../c-decl.c (pushdecl).  */
3811       if (TREE_CODE (x) == TYPE_DECL)
3812         {
3813           tree type = TREE_TYPE (x);
3814           if (DECL_SOURCE_LINE (x) == 0)
3815             {
3816               if (TYPE_NAME (type) == 0)
3817                 TYPE_NAME (type) = x;
3818             }
3819           else if (type != error_mark_node && TYPE_NAME (type) != x
3820                    /* We don't want to copy the type when all we're
3821                       doing is making a TYPE_DECL for the purposes of
3822                       inlining.  */
3823                    && (!TYPE_NAME (type)
3824                        || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x)))
3825             {
3826               DECL_ORIGINAL_TYPE (x) = type;
3827               type = build_type_copy (type);
3828               TYPE_STUB_DECL (type) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
3829               TYPE_NAME (type) = x;
3830               TREE_TYPE (x) = type;
3831             }
3832
3833           if (type != error_mark_node
3834               && TYPE_NAME (type)
3835               && TYPE_IDENTIFIER (type))
3836             set_identifier_type_value_with_scope (DECL_NAME (x), type,
3837                                                   current_binding_level);
3838
3839         }
3840
3841       /* Multiple external decls of the same identifier ought to match.
3842
3843          We get warnings about inline functions where they are defined.
3844          We get warnings about other functions from push_overloaded_decl.
3845
3846          Avoid duplicate warnings where they are used.  */
3847       if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
3848         {
3849           tree decl;
3850
3851           decl = IDENTIFIER_NAMESPACE_VALUE (name);
3852           if (decl && TREE_CODE (decl) == OVERLOAD)
3853             decl = OVL_FUNCTION (decl);
3854
3855           if (decl && decl != error_mark_node
3856               && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl))
3857               /* If different sort of thing, we already gave an error.  */
3858               && TREE_CODE (decl) == TREE_CODE (x)
3859               && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl)))
3860             {
3861               pedwarn ("type mismatch with previous external decl", x);
3862               cp_pedwarn_at ("previous external decl of `%#D'", decl);
3863             }
3864         }
3865
3866       /* This name is new in its binding level.
3867          Install the new declaration and return it.  */
3868       if (namespace_bindings_p ())
3869         {
3870           /* Install a global value.  */
3871
3872           /* If the first global decl has external linkage,
3873              warn if we later see static one.  */
3874           if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
3875             TREE_PUBLIC (name) = 1;
3876
3877           /* Bind the name for the entity.  */
3878           if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
3879                 && t != NULL_TREE)
3880               && (TREE_CODE (x) == TYPE_DECL
3881                   || TREE_CODE (x) == VAR_DECL
3882                   || TREE_CODE (x) == ALIAS_DECL
3883                   || TREE_CODE (x) == NAMESPACE_DECL
3884                   || TREE_CODE (x) == CONST_DECL
3885                   || TREE_CODE (x) == TEMPLATE_DECL))
3886             SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
3887
3888           /* Don't forget if the function was used via an implicit decl.  */
3889           if (IDENTIFIER_IMPLICIT_DECL (name)
3890               && TREE_USED (IDENTIFIER_IMPLICIT_DECL (name)))
3891             TREE_USED (x) = 1;
3892
3893           /* Don't forget if its address was taken in that way.  */
3894           if (IDENTIFIER_IMPLICIT_DECL (name)
3895               && TREE_ADDRESSABLE (IDENTIFIER_IMPLICIT_DECL (name)))
3896             TREE_ADDRESSABLE (x) = 1;
3897
3898           /* Warn about mismatches against previous implicit decl.  */
3899           if (IDENTIFIER_IMPLICIT_DECL (name) != NULL_TREE
3900               /* If this real decl matches the implicit, don't complain.  */
3901               && ! (TREE_CODE (x) == FUNCTION_DECL
3902                     && TREE_TYPE (TREE_TYPE (x)) == integer_type_node))
3903             warning
3904               ("`%D' was previously implicitly declared to return `int'", x);
3905
3906           /* If new decl is `static' and an `extern' was seen previously,
3907              warn about it.  */
3908           if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
3909             warn_extern_redeclared_static (x, t);
3910         }
3911       else
3912         {
3913           /* Here to install a non-global value.  */
3914           tree oldlocal = IDENTIFIER_VALUE (name);
3915           tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
3916
3917           if (need_new_binding)
3918             {
3919               push_local_binding (name, x, 0);
3920               /* Because push_local_binding will hook X on to the
3921                  current_binding_level's name list, we don't want to
3922                  do that again below.  */
3923               need_new_binding = 0;
3924             }
3925
3926           /* If this is a TYPE_DECL, push it into the type value slot.  */
3927           if (TREE_CODE (x) == TYPE_DECL)
3928             set_identifier_type_value_with_scope (name, TREE_TYPE (x),
3929                                                   current_binding_level);
3930
3931           /* Clear out any TYPE_DECL shadowed by a namespace so that
3932              we won't think this is a type.  The C struct hack doesn't
3933              go through namespaces.  */
3934           if (TREE_CODE (x) == NAMESPACE_DECL)
3935             set_identifier_type_value_with_scope (name, NULL_TREE,
3936                                                   current_binding_level);
3937
3938           if (oldlocal)
3939             {
3940               tree d = oldlocal;
3941
3942               while (oldlocal
3943                      && TREE_CODE (oldlocal) == VAR_DECL
3944                      && DECL_DEAD_FOR_LOCAL (oldlocal))
3945                 oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal);
3946
3947               if (oldlocal == NULL_TREE)
3948                 oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d));
3949             }
3950
3951           /* If this is an extern function declaration, see if we
3952              have a global definition or declaration for the function.  */
3953           if (oldlocal == NULL_TREE
3954               && DECL_EXTERNAL (x)
3955               && oldglobal != NULL_TREE
3956               && TREE_CODE (x) == FUNCTION_DECL
3957               && TREE_CODE (oldglobal) == FUNCTION_DECL)
3958             {
3959               /* We have one.  Their types must agree.  */
3960               if (decls_match (x, oldglobal))
3961                 /* OK */;
3962               else
3963                 {
3964                   warning ("extern declaration of `%#D' doesn't match", x);
3965                   cp_warning_at ("global declaration `%#D'", oldglobal);
3966                 }
3967             }
3968           /* If we have a local external declaration,
3969              and no file-scope declaration has yet been seen,
3970              then if we later have a file-scope decl it must not be static.  */
3971           if (oldlocal == NULL_TREE
3972               && oldglobal == NULL_TREE
3973               && DECL_EXTERNAL (x)
3974               && TREE_PUBLIC (x))
3975             TREE_PUBLIC (name) = 1;
3976
3977           /* Warn if shadowing an argument at the top level of the body.  */
3978           if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
3979               /* Inline decls shadow nothing.  */
3980               && !DECL_FROM_INLINE (x)
3981               && TREE_CODE (oldlocal) == PARM_DECL
3982               /* Don't check the `this' parameter.  */
3983               && !DECL_ARTIFICIAL (oldlocal))
3984             {
3985               bool err = false;
3986
3987               /* Don't complain if it's from an enclosing function.  */
3988               if (DECL_CONTEXT (oldlocal) == current_function_decl
3989                   && TREE_CODE (x) != PARM_DECL)
3990                 {
3991                   /* Go to where the parms should be and see if we find
3992                      them there.  */
3993                   struct cp_binding_level *b = current_binding_level->level_chain;
3994
3995                   /* Skip the ctor/dtor cleanup level.  */
3996                   b = b->level_chain;
3997
3998                   /* ARM $8.3 */
3999                   if (b->parm_flag == 1)
4000                     {
4001                       error ("declaration of `%#D' shadows a parameter",
4002                                 name);
4003                       err = true;
4004                     }
4005                 }
4006
4007               if (warn_shadow && !err)
4008                 shadow_warning (SW_PARAM,
4009                                 IDENTIFIER_POINTER (name), oldlocal);
4010             }
4011
4012           /* Maybe warn if shadowing something else.  */
4013           else if (warn_shadow && !DECL_EXTERNAL (x)
4014               /* No shadow warnings for internally generated vars.  */
4015               && ! DECL_ARTIFICIAL (x)
4016               /* No shadow warnings for vars made for inlining.  */
4017               && ! DECL_FROM_INLINE (x))
4018             {
4019               if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE
4020                        && current_class_ptr
4021                        && !TREE_STATIC (name))
4022                 warning ("declaration of `%s' shadows a member of `this'",
4023                             IDENTIFIER_POINTER (name));
4024               else if (oldlocal != NULL_TREE
4025                        && TREE_CODE (oldlocal) == VAR_DECL)
4026                 shadow_warning (SW_LOCAL,
4027                                 IDENTIFIER_POINTER (name), oldlocal);
4028               else if (oldglobal != NULL_TREE
4029                        && TREE_CODE (oldglobal) == VAR_DECL)
4030                 /* XXX shadow warnings in outer-more namespaces */
4031                 shadow_warning (SW_GLOBAL,
4032                                 IDENTIFIER_POINTER (name), oldglobal);
4033             }
4034         }
4035
4036       if (TREE_CODE (x) == FUNCTION_DECL)
4037         check_default_args (x);
4038
4039       if (TREE_CODE (x) == VAR_DECL)
4040         maybe_register_incomplete_var (x);
4041     }
4042
4043   if (need_new_binding)
4044     add_decl_to_level (x,
4045                        DECL_NAMESPACE_SCOPE_P (x)
4046                        ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x))
4047                        : current_binding_level);
4048
4049   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
4050 }
4051
4052 /* Same as pushdecl, but define X in binding-level LEVEL.  We rely on the
4053    caller to set DECL_CONTEXT properly.  */
4054
4055 static tree
4056 pushdecl_with_scope (tree x, struct cp_binding_level* level)
4057 {
4058   register struct cp_binding_level *b;
4059   tree function_decl = current_function_decl;
4060
4061   timevar_push (TV_NAME_LOOKUP);
4062   current_function_decl = NULL_TREE;
4063   if (level->parm_flag == 2)
4064     {
4065       b = class_binding_level;
4066       class_binding_level = level;
4067       pushdecl_class_level (x);
4068       class_binding_level = b;
4069     }
4070   else
4071     {
4072       b = current_binding_level;
4073       current_binding_level = level;
4074       x = pushdecl (x);
4075       current_binding_level = b;
4076     }
4077   current_function_decl = function_decl;
4078   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
4079 }
4080
4081 /* Like pushdecl, only it places X in the current namespace,
4082    if appropriate.  */
4083
4084 tree
4085 pushdecl_namespace_level (tree x)
4086 {
4087   register struct cp_binding_level *b = current_binding_level;
4088   register tree t;
4089
4090   timevar_push (TV_NAME_LOOKUP);
4091   t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace));
4092
4093   /* Now, the type_shadowed stack may screw us.  Munge it so it does
4094      what we want.  */
4095   if (TREE_CODE (x) == TYPE_DECL)
4096     {
4097       tree name = DECL_NAME (x);
4098       tree newval;
4099       tree *ptr = (tree *)0;
4100       for (; b != global_binding_level; b = b->level_chain)
4101         {
4102           tree shadowed = b->type_shadowed;
4103           for (; shadowed; shadowed = TREE_CHAIN (shadowed))
4104             if (TREE_PURPOSE (shadowed) == name)
4105               {
4106                 ptr = &TREE_VALUE (shadowed);
4107                 /* Can't break out of the loop here because sometimes
4108                    a binding level will have duplicate bindings for
4109                    PT names.  It's gross, but I haven't time to fix it.  */