OSDN Git Service

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