OSDN Git Service

* cp-tree.h (calls_setjmp_p): Declare.
[pf3gnuchains/gcc-fork.git] / gcc / cp / decl.c
1 /* Process declarations and variables for C compiler.
2    Copyright (C) 1988, 92-98, 1999 Free Software Foundation, Inc.
3    Contributed by Michael Tiemann (tiemann@cygnus.com)
4
5 This file is part of GNU CC.
6
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22
23 /* Process declarations and symbol lookup for C front end.
24    Also constructs types; the standard scalar types at initialization,
25    and structure, union, array and enum types when they are declared.  */
26
27 /* ??? not all decl nodes are given the most useful possible
28    line numbers.  For example, the CONST_DECLs for enum values.  */
29
30 #include "config.h"
31 #include "system.h"
32 #include "tree.h"
33 #include "rtl.h"
34 #include "flags.h"
35 #include "cp-tree.h"
36 #include "decl.h"
37 #include "lex.h"
38 #include <signal.h>
39 #include "obstack.h"
40 #include "defaults.h"
41 #include "output.h"
42 #include "except.h"
43 #include "toplev.h"
44 #include "../hash.h"
45 #include "defaults.h"
46 #include "ggc.h"
47
48 #define obstack_chunk_alloc xmalloc
49 #define obstack_chunk_free free
50
51 extern struct obstack permanent_obstack;
52
53 extern int current_class_depth;
54
55 extern tree static_ctors, static_dtors;
56
57 extern tree global_namespace;
58
59 extern int (*valid_lang_attribute) PROTO ((tree, tree, tree, tree));
60
61 /* Use garbage collection.  */
62
63 int ggc_p = 1;
64
65 #ifndef WCHAR_UNSIGNED
66 #define WCHAR_UNSIGNED 0
67 #endif
68
69 #ifndef CHAR_TYPE_SIZE
70 #define CHAR_TYPE_SIZE BITS_PER_UNIT
71 #endif
72
73 #ifndef BOOL_TYPE_SIZE
74 #ifdef SLOW_BYTE_ACCESS
75 #define BOOL_TYPE_SIZE ((SLOW_BYTE_ACCESS) ? (POINTER_SIZE) : (CHAR_TYPE_SIZE))
76 #else
77 #define BOOL_TYPE_SIZE CHAR_TYPE_SIZE
78 #endif
79 #endif
80
81 /* We let tm.h override the types used here, to handle trivial differences
82    such as the choice of unsigned int or long unsigned int for size_t.
83    When machines start needing nontrivial differences in the size type,
84    it would be best to do something here to figure out automatically
85    from other information what type to use.  */
86
87 #ifndef SIZE_TYPE
88 #define SIZE_TYPE "long unsigned int"
89 #endif
90
91 #ifndef PTRDIFF_TYPE
92 #define PTRDIFF_TYPE "long int"
93 #endif
94
95 #ifndef WCHAR_TYPE
96 #define WCHAR_TYPE "int"
97 #endif
98
99 static tree grokparms                           PROTO((tree, int));
100 static const char *redeclaration_error_message  PROTO((tree, tree));
101
102 static void push_binding_level PROTO((struct binding_level *, int,
103                                       int));
104 static void pop_binding_level PROTO((void));
105 static void suspend_binding_level PROTO((void));
106 static void resume_binding_level PROTO((struct binding_level *));
107 static struct binding_level *make_binding_level PROTO((void));
108 static void declare_namespace_level PROTO((void));
109 static void signal_catch PROTO((int)) ATTRIBUTE_NORETURN;
110 static void storedecls PROTO((tree));
111 static void require_complete_types_for_parms PROTO((tree));
112 static void push_overloaded_decl_1 PROTO((tree));
113 static int ambi_op_p PROTO((tree));
114 static int unary_op_p PROTO((tree));
115 static tree store_bindings PROTO((tree, tree));
116 static tree lookup_tag_reverse PROTO((tree, tree));
117 static tree obscure_complex_init PROTO((tree, tree));
118 static tree maybe_build_cleanup_1 PROTO((tree, tree));
119 static tree lookup_name_real PROTO((tree, int, int, int));
120 static void warn_extern_redeclared_static PROTO((tree, tree));
121 static void grok_reference_init PROTO((tree, tree, tree));
122 static tree grokfndecl PROTO((tree, tree, tree, tree, int,
123                               enum overload_flags, tree,
124                               tree, int, int, int, int, int, int, tree));
125 static tree grokvardecl PROTO((tree, tree, RID_BIT_TYPE *, int, int, tree));
126 static tree lookup_tag PROTO((enum tree_code, tree,
127                               struct binding_level *, int));
128 static void set_identifier_type_value_with_scope
129         PROTO((tree, tree, struct binding_level *));
130 static void record_builtin_type PROTO((enum rid, const char *, tree));
131 static void record_unknown_type PROTO((tree, const char *));
132 static int member_function_or_else PROTO((tree, tree, const char *));
133 static void bad_specifiers PROTO((tree, const char *, int, int, int, int,
134                                   int));
135 static void lang_print_error_function PROTO((const char *));
136 static tree maybe_process_template_type_declaration PROTO((tree, int, struct binding_level*));
137 static void check_for_uninitialized_const_var PROTO((tree));
138 static unsigned long typename_hash PROTO((hash_table_key));
139 static boolean typename_compare PROTO((hash_table_key, hash_table_key));
140 static void push_binding PROTO((tree, tree, struct binding_level*));
141 static int add_binding PROTO((tree, tree));
142 static void pop_binding PROTO((tree, tree));
143 static tree local_variable_p_walkfn PROTO((tree *, int *, void *));
144 static tree find_binding PROTO((tree, tree));
145 static tree select_decl PROTO((tree, int));
146 static int lookup_flags PROTO((int, int));
147 static tree qualify_lookup PROTO((tree, int));
148 static tree record_builtin_java_type PROTO((const char *, int));
149 static const char *tag_name PROTO((enum tag_types code));
150 static void find_class_binding_level PROTO((void));
151 static struct binding_level *innermost_nonclass_level PROTO((void));
152 static void warn_about_implicit_typename_lookup PROTO((tree, tree));
153 static int walk_namespaces_r PROTO((tree, walk_namespaces_fn, void *));
154 static int walk_globals_r PROTO((tree, void *));
155 static void add_decl_to_level PROTO((tree, struct binding_level *));
156 static tree make_label_decl PROTO((tree, int));
157 static void pop_label PROTO((tree));
158 static void pop_labels PROTO((tree));
159 static void maybe_deduce_size_from_array_init PROTO((tree, tree));
160 static void layout_var_decl PROTO((tree));
161 static void maybe_commonize_var PROTO((tree));
162 static tree check_initializer PROTO((tree, tree));
163 static void make_rtl_for_nonlocal_decl PROTO((tree, tree, const char *));
164 static void push_cp_function_context PROTO((struct function *));
165 static void pop_cp_function_context PROTO((struct function *));
166 static void mark_binding_level PROTO((void *));
167 static void mark_cp_function_context PROTO((struct function *));
168 static void mark_saved_scope PROTO((void *));
169 static void mark_lang_function PROTO((struct language_function *));
170 static void mark_stmt_tree PROTO((struct stmt_tree *));
171 static void save_function_data PROTO((tree));
172 static void check_function_type PROTO((tree));
173 static void destroy_local_static PROTO((tree));
174 static void destroy_local_var PROTO((tree));
175 static void finish_constructor_body PROTO((void));
176 static void finish_destructor_body PROTO((void));
177 static tree create_array_type_for_decl PROTO((tree, tree, tree));
178
179 #if defined (DEBUG_CP_BINDING_LEVELS)
180 static void indent PROTO((void));
181 #endif
182
183 /* Erroneous argument lists can use this *IFF* they do not modify it.  */
184 tree error_mark_list;
185
186 /* The following symbols are subsumed in the cp_global_trees array, and
187    listed here individually for documentation purposes. 
188
189    C++ extensions
190         tree wchar_decl_node;
191         tree void_zero_node;
192
193         tree vtable_entry_type;
194         tree delta_type_node;
195 #if 0
196    Old rtti stuff.
197         tree __baselist_desc_type_node;
198         tree __i_desc_type_node, __m_desc_type_node;
199         tree __t_desc_array_type, __i_desc_array_type, __m_desc_array_type;
200 #endif
201         tree __t_desc_type_node;
202 #if 0
203         tree __tp_desc_type_node;
204 #endif
205         tree __access_mode_type_node;
206         tree __bltn_desc_type_node, __user_desc_type_node, __class_desc_type_node;
207         tree __ptr_desc_type_node, __attr_desc_type_node, __func_desc_type_node;
208         tree __ptmf_desc_type_node, __ptmd_desc_type_node;
209 #if 0
210    Not needed yet?  May be needed one day?
211         tree __bltn_desc_array_type, __user_desc_array_type, __class_desc_array_type;
212         tree __ptr_desc_array_type, __attr_dec_array_type, __func_desc_array_type;
213         tree __ptmf_desc_array_type, __ptmd_desc_array_type;
214 #endif
215
216         tree class_star_type_node;
217         tree class_type_node, record_type_node, union_type_node, enum_type_node;
218         tree unknown_type_node;
219
220    Array type `vtable_entry_type[]'
221
222         tree vtbl_type_node;
223         tree vtbl_ptr_type_node;
224
225    Nnamespace std
226
227         tree std_node;
228
229    A FUNCTION_DECL which can call `abort'.  Not necessarily the
230    one that the user will declare, but sufficient to be called
231    by routines that want to abort the program.
232
233         tree abort_fndecl;
234
235    The FUNCTION_DECL for the default `::operator delete'.
236
237         tree global_delete_fndecl;
238
239    Used by RTTI
240         tree type_info_type_node, tinfo_fn_id, tinfo_fn_type;
241
242 */
243
244 tree cp_global_trees[CPTI_MAX];
245
246 /* Indicates that there is a type value in some namespace, although
247    that is not necessarily in scope at the moment.  */
248
249 static tree global_type_node;
250
251 /* Namespace std.  */
252 int in_std;
253
254 /* Expect only namespace names now. */
255 static int only_namespace_names;
256
257 /* If original DECL_RESULT of current function was a register,
258    but due to being an addressable named return value, would up
259    on the stack, this variable holds the named return value's
260    original location.  */
261
262 #define original_result_rtx cp_function_chain->x_result_rtx
263
264 struct named_label_list
265 {
266   struct binding_level *binding_level;
267   tree names_in_scope;
268   tree label_decl;
269   const char *filename_o_goto;
270   int lineno_o_goto;
271   struct named_label_list *next;
272 };
273
274 /* Used only for jumps to as-yet undefined labels, since jumps to
275    defined labels can have their validity checked by stmt.c.  */
276
277 #define named_label_uses cp_function_chain->x_named_label_uses
278
279 /* A list of objects which have constructors or destructors
280    which reside in the global scope.  The decl is stored in
281    the TREE_VALUE slot and the initializer is stored
282    in the TREE_PURPOSE slot.  */
283 tree static_aggregates;
284
285 /* -- end of C++ */
286
287 /* A node for the integer constants 2, and 3.  */
288
289 tree integer_two_node, integer_three_node;
290
291 /* Parsing a function declarator leaves here a chain of structure
292    and enum types declared in the parmlist.  */
293
294 static tree last_function_parm_tags;
295
296 /* Similar, for last_function_parm_tags.  */
297 tree last_function_parms;
298 static tree current_function_parm_tags;
299
300 /* A list (chain of TREE_LIST nodes) of all LABEL_DECLs in the function
301    that have names.  Here so we can clear out their names' definitions
302    at the end of the function.  The TREE_VALUE is a LABEL_DECL; the
303    TREE_PURPOSE is the previous binding of the label.  */
304
305 #define named_labels cp_function_chain->x_named_labels
306
307 /* The FUNCTION_DECL for the function currently being compiled,
308    or 0 if between functions.  */
309 tree current_function_decl;
310
311 /* Set to 0 at beginning of a function definition, and whenever
312    a label (case or named) is defined.  Set to value of expression
313    returned from function when that value can be transformed into
314    a named return value.  */
315
316 tree current_function_return_value;
317
318 /* Nonzero means give `double' the same size as `float'.  */
319
320 extern int flag_short_double;
321
322 /* Nonzero means don't recognize any builtin functions.  */
323
324 extern int flag_no_builtin;
325
326 /* Nonzero means don't recognize the non-ANSI builtin functions.
327    -ansi sets this.  */
328
329 extern int flag_no_nonansi_builtin;
330
331 /* Nonzero if we want to support huge (> 2^(sizeof(short)*8-1) bytes)
332    objects.  */
333 extern int flag_huge_objects;
334
335 /* Nonzero if we want to conserve space in the .o files.  We do this
336    by putting uninitialized data and runtime initialized data into
337    .common instead of .data at the expense of not flagging multiple
338    definitions.  */
339 extern int flag_conserve_space;
340 \f
341 /* C and C++ flags are in decl2.c.  */
342
343 /* Flag used when debugging spew.c */
344
345 extern int spew_debug;
346
347 /* A expression of value 0 with the same precision as a sizetype
348    node, but signed.  */
349 tree signed_size_zero_node;
350
351 /* The name of the anonymous namespace, throughout this translation
352    unit.  */
353 tree anonymous_namespace_name;
354
355 \f
356 /* For each binding contour we allocate a binding_level structure
357    which records the names defined in that contour.
358    Contours include:
359     0) the global one
360     1) one for each function definition,
361        where internal declarations of the parameters appear.
362     2) one for each compound statement,
363        to record its declarations.
364
365    The current meaning of a name can be found by searching the levels
366    from the current one out to the global one.
367
368    Off to the side, may be the class_binding_level.  This exists only
369    to catch class-local declarations.  It is otherwise nonexistent.
370
371    Also there may be binding levels that catch cleanups that must be
372    run when exceptions occur.  Thus, to see whether a name is bound in
373    the current scope, it is not enough to look in the
374    CURRENT_BINDING_LEVEL.  You should use lookup_name_current_level
375    instead.  */
376
377 /* Note that the information in the `names' component of the global contour
378    is duplicated in the IDENTIFIER_GLOBAL_VALUEs of all identifiers.  */
379
380 struct binding_level
381   {
382     /* A chain of _DECL nodes for all variables, constants, functions,
383        and typedef types.  These are in the reverse of the order
384        supplied.  There may be OVERLOADs on this list, too, but they
385        are wrapped in TREE_LISTs; the TREE_VALUE is the OVERLOAD.  */
386     tree names;
387
388     /* A list of structure, union and enum definitions, for looking up
389        tag names.
390        It is a chain of TREE_LIST nodes, each of whose TREE_PURPOSE is a name,
391        or NULL_TREE; and whose TREE_VALUE is a RECORD_TYPE, UNION_TYPE,
392        or ENUMERAL_TYPE node.
393
394        C++: the TREE_VALUE nodes can be simple types for
395        component_bindings.  */
396     tree tags;
397
398     /* A list of USING_DECL nodes. */
399     tree usings;
400
401     /* A list of used namespaces. PURPOSE is the namespace,
402        VALUE the common ancestor with this binding_level's namespace. */
403     tree using_directives;
404
405     /* If this binding level is the binding level for a class, then
406        class_shadowed is a TREE_LIST.  The TREE_PURPOSE of each node
407        is the name of an entity bound in the class; the TREE_VALUE is
408        the IDENTIFIER_CLASS_VALUE before we entered the class.  Thus,
409        when leaving class scope, we can restore the
410        IDENTIFIER_CLASS_VALUE by walking this list.  The TREE_TYPE is
411        the DECL bound by this name in the class.  */
412     tree class_shadowed;
413
414     /* Similar to class_shadowed, but for IDENTIFIER_TYPE_VALUE, and
415        is used for all binding levels.  */
416     tree type_shadowed;
417
418     /* A TREE_LIST.  Each TREE_VALUE is the LABEL_DECL for a local
419        label in this scope.  The TREE_PURPOSE is the previous value of
420        the IDENTIFIER_LABEL VALUE.  */
421     tree shadowed_labels;
422
423     /* For each level (except not the global one),
424        a chain of BLOCK nodes for all the levels
425        that were entered and exited one level down.  */
426     tree blocks;
427
428     /* The BLOCK node for this level, if one has been preallocated.
429        If 0, the BLOCK is allocated (if needed) when the level is popped.  */
430     tree this_block;
431
432     /* The binding level which this one is contained in (inherits from).  */
433     struct binding_level *level_chain;
434
435     /* List of decls in `names' that have incomplete
436        structure or union types.  */
437     tree incomplete;
438
439     /* List of VAR_DECLS saved from a previous for statement.
440        These would be dead in ANSI-conforming code, but might
441        be referenced in ARM-era code.  These are stored in a
442        TREE_LIST; the TREE_VALUE is the actual declaration.  */
443     tree dead_vars_from_for;
444
445     /* 1 for the level that holds the parameters of a function.
446        2 for the level that holds a class declaration.
447        3 for levels that hold parameter declarations.  */
448     unsigned parm_flag : 4;
449
450     /* 1 means make a BLOCK for this level regardless of all else.
451        2 for temporary binding contours created by the compiler.  */
452     unsigned keep : 3;
453
454     /* Nonzero if this level "doesn't exist" for tags.  */
455     unsigned tag_transparent : 1;
456
457     /* Nonzero if this level can safely have additional
458        cleanup-needing variables added to it.  */
459     unsigned more_cleanups_ok : 1;
460     unsigned have_cleanups : 1;
461
462     /* Nonzero if this level is for storing the decls for template
463        parameters and generic decls; these decls will be discarded and
464        replaced with a TEMPLATE_DECL.  */
465     unsigned pseudo_global : 1;
466
467     /* This is set for a namespace binding level.  */
468     unsigned namespace_p : 1;
469
470     /* True if this level is that of a for-statement where we need to
471        worry about ambiguous (ARM or ANSI) scope rules.  */
472     unsigned is_for_scope : 1;
473
474     /* True if this level corresponds to an EH region, as for a try block.  */
475     unsigned eh_region : 1;
476
477     /* One bit left for this word.  */
478
479 #if defined(DEBUG_CP_BINDING_LEVELS)
480     /* Binding depth at which this level began.  */
481     unsigned binding_depth;
482 #endif /* defined(DEBUG_CP_BINDING_LEVELS) */
483   };
484
485 #define NULL_BINDING_LEVEL ((struct binding_level *) NULL)
486   
487 /* The binding level currently in effect.  */
488
489 #define current_binding_level                   \
490   (current_function                             \
491    ? cp_function_chain->bindings                \
492    : scope_chain->bindings)
493
494 /* The binding level of the current class, if any.  */
495
496 #define class_binding_level scope_chain->class_bindings
497
498 /* A chain of binding_level structures awaiting reuse.  */
499
500 static struct binding_level *free_binding_level;
501
502 /* The outermost binding level, for names of file scope.
503    This is created when the compiler is started and exists
504    through the entire run.  */
505
506 static struct binding_level *global_binding_level;
507
508 /* Nonzero means unconditionally make a BLOCK for the next level pushed.  */
509
510 static int keep_next_level_flag;
511
512 #if defined(DEBUG_CP_BINDING_LEVELS)
513 static int binding_depth = 0;
514 static int is_class_level = 0;
515
516 static void
517 indent ()
518 {
519   register unsigned i;
520
521   for (i = 0; i < binding_depth*2; i++)
522     putc (' ', stderr);
523 }
524 #endif /* defined(DEBUG_CP_BINDING_LEVELS) */
525
526 static tree pushdecl_with_scope PROTO((tree, struct binding_level *));
527
528 static void
529 push_binding_level (newlevel, tag_transparent, keep)
530      struct binding_level *newlevel;
531      int tag_transparent, keep;
532 {
533   /* Add this level to the front of the chain (stack) of levels that
534      are active.  */
535   bzero ((char*) newlevel, sizeof (struct binding_level));
536   newlevel->level_chain = current_binding_level;
537   current_binding_level = newlevel;
538   newlevel->tag_transparent = tag_transparent;
539   newlevel->more_cleanups_ok = 1;
540
541   /* We are called before expand_start_bindings, but after
542      expand_eh_region_start for a try block; so we check this now,
543      before the EH block is covered up.  */
544   newlevel->eh_region = is_eh_region ();
545
546   newlevel->keep = keep;
547 #if defined(DEBUG_CP_BINDING_LEVELS)
548   newlevel->binding_depth = binding_depth;
549   indent ();
550   fprintf (stderr, "push %s level 0x%08x line %d\n",
551            (is_class_level) ? "class" : "block", newlevel, lineno);
552   is_class_level = 0;
553   binding_depth++;
554 #endif /* defined(DEBUG_CP_BINDING_LEVELS) */
555 }
556
557 /* Find the innermost enclosing class scope, and reset
558    CLASS_BINDING_LEVEL appropriately.  */
559
560 static void
561 find_class_binding_level ()
562 {
563   struct binding_level *level = current_binding_level;
564
565   while (level && level->parm_flag != 2)
566     level = level->level_chain;
567   if (level && level->parm_flag == 2)
568     class_binding_level = level;
569   else
570     class_binding_level = 0;
571 }
572
573 static void
574 pop_binding_level ()
575 {
576   if (global_binding_level)
577     {
578       /* Cannot pop a level, if there are none left to pop.  */
579       if (current_binding_level == global_binding_level)
580         my_friendly_abort (123);
581     }
582   /* Pop the current level, and free the structure for reuse.  */
583 #if defined(DEBUG_CP_BINDING_LEVELS)
584   binding_depth--;
585   indent ();
586   fprintf (stderr, "pop  %s level 0x%08x line %d\n",
587           (is_class_level) ? "class" : "block",
588           current_binding_level, lineno);
589   if (is_class_level != (current_binding_level == class_binding_level))
590     {
591       indent ();
592       fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
593     }
594   is_class_level = 0;
595 #endif /* defined(DEBUG_CP_BINDING_LEVELS) */
596   {
597     register struct binding_level *level = current_binding_level;
598     current_binding_level = current_binding_level->level_chain;
599     level->level_chain = free_binding_level;
600 #if 0 /* defined(DEBUG_CP_BINDING_LEVELS) */
601     if (level->binding_depth != binding_depth)
602       abort ();
603 #endif /* defined(DEBUG_CP_BINDING_LEVELS) */
604     free_binding_level = level;
605     find_class_binding_level ();
606   }
607 }
608
609 static void
610 suspend_binding_level ()
611 {
612   if (class_binding_level)
613     current_binding_level = class_binding_level;
614
615   if (global_binding_level)
616     {
617       /* Cannot suspend a level, if there are none left to suspend.  */
618       if (current_binding_level == global_binding_level)
619         my_friendly_abort (123);
620     }
621   /* Suspend the current level.  */
622 #if defined(DEBUG_CP_BINDING_LEVELS)
623   binding_depth--;
624   indent ();
625   fprintf (stderr, "suspend  %s level 0x%08x line %d\n",
626           (is_class_level) ? "class" : "block",
627           current_binding_level, lineno);
628   if (is_class_level != (current_binding_level == class_binding_level))
629     {
630       indent ();
631       fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
632     }
633   is_class_level = 0;
634 #endif /* defined(DEBUG_CP_BINDING_LEVELS) */
635   current_binding_level = current_binding_level->level_chain;
636   find_class_binding_level ();
637 }
638
639 static void
640 resume_binding_level (b)
641      struct binding_level *b;
642 {
643   /* Resuming binding levels is meant only for namespaces,
644      and those cannot nest into classes. */
645   my_friendly_assert(!class_binding_level, 386);
646   /* Also, resuming a non-directly nested namespace is a no-no.  */
647   my_friendly_assert(b->level_chain == current_binding_level, 386);
648   current_binding_level = b;
649 #if defined(DEBUG_CP_BINDING_LEVELS)
650   b->binding_depth = binding_depth;
651   indent ();
652   fprintf (stderr, "resume %s level 0x%08x line %d\n",
653            (is_class_level) ? "class" : "block", b, lineno);
654   is_class_level = 0;
655   binding_depth++;
656 #endif /* defined(DEBUG_CP_BINDING_LEVELS) */
657 }
658 \f
659 /* Create a new `struct binding_level'.  */
660
661 static
662 struct binding_level *
663 make_binding_level ()
664 {
665   /* NOSTRICT */
666   return (struct binding_level *) xmalloc (sizeof (struct binding_level));
667 }
668
669 /* Nonzero if we are currently in the global binding level.  */
670
671 int
672 global_bindings_p ()
673 {
674   return current_binding_level == global_binding_level;
675 }
676
677 /* Return the innermost binding level that is not for a class scope.  */
678
679 static struct binding_level *
680 innermost_nonclass_level ()
681 {
682   struct binding_level *b;
683
684   b = current_binding_level;
685   while (b->parm_flag == 2)
686     b = b->level_chain;
687
688   return b;
689 }
690
691 /* Nonzero if we are currently in a toplevel binding level.  This
692    means either the global binding level or a namespace in a toplevel
693    binding level.  Since there are no non-toplevel namespace levels,
694    this really means any namespace or pseudo-global level.  We also
695    include a class whose context is toplevel.  */
696
697 int
698 toplevel_bindings_p ()
699 {
700   struct binding_level *b = innermost_nonclass_level ();
701
702   return b->namespace_p || b->pseudo_global;
703 }
704
705 /* Nonzero if this is a namespace scope, or if we are defining a class
706    which is itself at namespace scope, or whose enclosing class is
707    such a class, etc.  */
708
709 int
710 namespace_bindings_p ()
711 {
712   struct binding_level *b = innermost_nonclass_level ();
713
714   return b->namespace_p;
715 }
716
717 /* If KEEP is non-zero, make a BLOCK node for the next binding level,
718    unconditionally.  Otherwise, use the normal logic to decide whether
719    or not to create a BLOCK.  */
720
721 void
722 keep_next_level (keep)
723      int keep;
724 {
725   keep_next_level_flag = keep;
726 }
727
728 /* Nonzero if the current level needs to have a BLOCK made.  */
729
730 int
731 kept_level_p ()
732 {
733   return (current_binding_level->blocks != NULL_TREE
734           || current_binding_level->keep
735           || current_binding_level->names != NULL_TREE
736           || (current_binding_level->tags != NULL_TREE
737               && !current_binding_level->tag_transparent));
738 }
739
740 void
741 declare_pseudo_global_level ()
742 {
743   current_binding_level->pseudo_global = 1;
744 }
745
746 static void
747 declare_namespace_level ()
748 {
749   current_binding_level->namespace_p = 1;
750 }
751
752 int
753 pseudo_global_level_p ()
754 {
755   return current_binding_level->pseudo_global;
756 }
757
758 void
759 set_class_shadows (shadows)
760      tree shadows;
761 {
762   class_binding_level->class_shadowed = shadows;
763 }
764
765 /* Enter a new binding level.
766    If TAG_TRANSPARENT is nonzero, do so only for the name space of variables,
767    not for that of tags.  */
768
769 void
770 pushlevel (tag_transparent)
771      int tag_transparent;
772 {
773   struct binding_level *newlevel;
774
775   if (current_function && !doing_semantic_analysis_p ())
776     return;
777
778   /* Reuse or create a struct for this binding level.  */
779 #if defined(DEBUG_CP_BINDING_LEVELS)
780   if (0)
781 #else /* !defined(DEBUG_CP_BINDING_LEVELS) */
782   if (free_binding_level)
783 #endif /* !defined(DEBUG_CP_BINDING_LEVELS) */
784     {
785       newlevel = free_binding_level;
786       free_binding_level = free_binding_level->level_chain;
787     }
788   else
789     newlevel = make_binding_level ();
790
791   push_binding_level (newlevel, tag_transparent, keep_next_level_flag);
792   GNU_xref_start_scope ((HOST_WIDE_INT) newlevel);
793   keep_next_level_flag = 0;
794 }
795
796 void
797 note_level_for_for ()
798 {
799   current_binding_level->is_for_scope = 1;
800 }
801
802 /* For a binding between a name and an entity at a block scope,
803    this is the `struct binding_level' for the block.  */
804 #define BINDING_LEVEL(NODE) \
805    (((struct tree_binding*)NODE)->scope.level)
806
807 /* Make DECL the innermost binding for ID.  The LEVEL is the binding
808    level at which this declaration is being bound.  */
809
810 static void
811 push_binding (id, decl, level)
812      tree id;
813      tree decl;
814      struct binding_level* level;
815 {
816   tree binding;
817
818   binding = make_node (CPLUS_BINDING);
819
820   /* Now, fill in the binding information.  */
821   BINDING_VALUE (binding) = decl;
822   BINDING_TYPE (binding) = NULL_TREE;
823   BINDING_LEVEL (binding) = level;
824   INHERITED_VALUE_BINDING_P (binding) = 0;
825   LOCAL_BINDING_P (binding) = (level != class_binding_level);
826   BINDING_HAS_LEVEL_P (binding) = 1;
827
828   /* And put it on the front of the list of bindings for ID.  */
829   TREE_CHAIN (binding) = IDENTIFIER_BINDING (id);
830   IDENTIFIER_BINDING (id) = binding;
831 }
832
833 /* ID is already bound in the current scope.  But, DECL is an
834    additional binding for ID in the same scope.  This is the `struct
835    stat' hack whereby a non-typedef class-name or enum-name can be
836    bound at the same level as some other kind of entity.  It's the
837    responsibility of the caller to check that inserting this name is
838    legal here.  Returns nonzero if the new binding was successful.  */
839 static int
840 add_binding (id, decl)
841      tree id;
842      tree decl;
843 {
844   tree binding = IDENTIFIER_BINDING (id);
845   int ok = 1;
846
847   if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
848     /* The new name is the type name.  */
849     BINDING_TYPE (binding) = decl;
850   else if (!BINDING_VALUE (binding))
851     /* This situation arises when push_class_level_binding moves an
852        inherited type-binding out of the way to make room for a new
853        value binding.  */
854     BINDING_VALUE (binding) = decl;
855   else if (TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
856            && DECL_ARTIFICIAL (BINDING_VALUE (binding)))
857     {
858       /* The old binding was a type name.  It was placed in
859          BINDING_VALUE because it was thought, at the point it was
860          declared, to be the only entity with such a name.  Move the
861          type name into the type slot; it is now hidden by the new
862          binding.  */
863       BINDING_TYPE (binding) = BINDING_VALUE (binding);
864       BINDING_VALUE (binding) = decl;
865       INHERITED_VALUE_BINDING_P (binding) = 0;
866     }
867   else if (TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
868            && TREE_CODE (decl) == TYPE_DECL
869            && DECL_NAME (decl) == DECL_NAME (BINDING_VALUE (binding))
870            && same_type_p (TREE_TYPE (decl),
871                            TREE_TYPE (BINDING_VALUE (binding))))
872     /* We have two typedef-names, both naming the same type to have
873        the same name.  This is OK because of:
874
875          [dcl.typedef]
876
877          In a given scope, a typedef specifier can be used to redefine
878          the name of any type declared in that scope to refer to the
879          type to which it already refers.  */
880     ok = 0;
881   else
882     {
883       cp_error ("declaration of `%#D'", decl);
884       cp_error_at ("conflicts with previous declaration `%#D'",
885                    BINDING_VALUE (binding));
886       ok = 0;
887     }
888
889   return ok;
890 }
891
892 /* Add DECL to the list of things declared in B.  */
893
894 static void
895 add_decl_to_level (decl, b)
896      tree decl;
897      struct binding_level *b;
898 {
899   /* We build up the list in reverse order, and reverse it later if
900      necessary.  */
901   TREE_CHAIN (decl) = b->names;
902   b->names = decl;
903 }
904
905 /* Bind DECL to ID in the current_binding_level, assumed to be a local
906    binding level.  If PUSH_USING is set in FLAGS, we know that DECL
907    doesn't really belong to this binding level, that it got here
908    through a using-declaration.  */
909
910 void
911 push_local_binding (id, decl, flags)
912      tree id;
913      tree decl;
914      int flags;
915 {
916   struct binding_level *b;
917
918   /* Skip over any local classes.  This makes sense if we call
919      push_local_binding with a friend decl of a local class.  */
920   b = current_binding_level;
921   while (b->parm_flag == 2)
922     b = b->level_chain;
923
924   if (lookup_name_current_level (id))
925     {
926       /* Supplement the existing binding.  */
927       if (!add_binding (id, decl))
928         /* It didn't work.  Something else must be bound at this
929            level.  Do not add DECL to the list of things to pop
930            later.  */
931         return;
932     }
933   else
934     /* Create a new binding.  */
935     push_binding (id, decl, b);
936
937   if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
938     /* We must put the OVERLOAD into a TREE_LIST since the
939        TREE_CHAIN of an OVERLOAD is already used.  Similarly for
940        decls that got here through a using-declaration.  */
941     decl = build_tree_list (NULL_TREE, decl);
942
943   /* And put DECL on the list of things declared by the current
944      binding level.  */
945   add_decl_to_level (decl, b);
946 }
947
948 /* Bind DECL to ID in the class_binding_level.  Returns nonzero if the
949    binding was successful.  */
950
951 int
952 push_class_binding (id, decl)
953      tree id;
954      tree decl;
955 {
956   int result = 1;
957   tree binding = IDENTIFIER_BINDING (id);
958   tree context;
959
960   /* Note that we declared this value so that we can issue an error if
961      this an illegal redeclaration of a name already used for some
962      other purpose.  */
963   note_name_declared_in_class (id, decl);
964
965   if (binding && BINDING_LEVEL (binding) == class_binding_level)
966     /* Supplement the existing binding.  */
967     result = add_binding (id, decl);
968   else
969     /* Create a new binding.  */
970     push_binding (id, decl, class_binding_level);
971
972   /* Update the IDENTIFIER_CLASS_VALUE for this ID to be the
973      class-level declaration.  Note that we do not use DECL here
974      because of the possibility of the `struct stat' hack; if DECL is
975      a class-name or enum-name we might prefer a field-name, or some
976      such.  */
977   IDENTIFIER_CLASS_VALUE (id) = BINDING_VALUE (IDENTIFIER_BINDING (id));
978
979   /* If this is a binding from a base class, mark it as such.  */
980   binding = IDENTIFIER_BINDING (id);
981   if (BINDING_VALUE (binding) == decl && TREE_CODE (decl) != TREE_LIST)
982     {
983       /* Any implicit typename must be from a base-class.  The
984          context for an implicit typename declaration is always
985          the derived class in which the lookup was done, so the checks
986          based on the context of DECL below will not trigger.  */
987       if (IMPLICIT_TYPENAME_TYPE_DECL_P (decl))
988         INHERITED_VALUE_BINDING_P (binding) = 1;
989       else
990         {
991           if (TREE_CODE (decl) == OVERLOAD)
992             context = DECL_REAL_CONTEXT (OVL_CURRENT (decl));
993           else
994             {
995               my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (decl)) == 'd',
996                                   0);
997               context = DECL_REAL_CONTEXT (decl);
998             }
999
1000           if (is_properly_derived_from (current_class_type, context))
1001             INHERITED_VALUE_BINDING_P (binding) = 1;
1002           else
1003             INHERITED_VALUE_BINDING_P (binding) = 0;
1004         }
1005     }
1006   else if (BINDING_VALUE (binding) == decl)
1007     /* We only encounter a TREE_LIST when push_class_decls detects an
1008        ambiguity.  Such an ambiguity can be overridden by a definition
1009        in this class.  */
1010     INHERITED_VALUE_BINDING_P (binding) = 1;
1011
1012   return result;
1013 }
1014
1015 /* Remove the binding for DECL which should be the innermost binding
1016    for ID.  */
1017
1018 static void 
1019 pop_binding (id, decl) 
1020      tree id;
1021      tree decl;
1022 {
1023   tree binding;
1024     
1025   if (id == NULL_TREE)
1026     /* It's easiest to write the loops that call this function without
1027        checking whether or not the entities involved have names.  We
1028        get here for such an entity.  */
1029     return;
1030
1031   /* Get the innermost binding for ID.  */
1032   binding = IDENTIFIER_BINDING (id);
1033
1034   /* The name should be bound.  */
1035   my_friendly_assert (binding != NULL_TREE, 0);
1036
1037   /* The DECL will be either the ordinary binding or the type
1038      binding for this identifier.  Remove that binding.  */
1039   if (BINDING_VALUE (binding) == decl)
1040     BINDING_VALUE (binding) = NULL_TREE;
1041   else if (BINDING_TYPE (binding) == decl)
1042     BINDING_TYPE (binding) = NULL_TREE;
1043   else
1044     my_friendly_abort (0);
1045
1046   if (!BINDING_VALUE (binding) && !BINDING_TYPE (binding))
1047     /* We're completely done with the innermost binding for this
1048        identifier.  Unhook it from the list of bindings.  */
1049     IDENTIFIER_BINDING (id) = TREE_CHAIN (binding);
1050 }
1051
1052 /* When a label goes out of scope, check to see if that label was used
1053    in a valid manner, and issue any appropriate warnings or errors.  */
1054
1055 static void
1056 pop_label (link)
1057      tree link;
1058 {
1059   tree label = TREE_VALUE (link);
1060
1061   if (!processing_template_decl && doing_semantic_analysis_p ())
1062     {
1063       if (DECL_INITIAL (label) == NULL_TREE)
1064         {
1065           cp_error_at ("label `%D' used but not defined", label);
1066           /* Avoid crashing later.  */
1067           define_label (input_filename, 1, DECL_NAME (label));
1068         }
1069       else if (warn_unused && !TREE_USED (label))
1070         cp_warning_at ("label `%D' defined but not used", label);
1071     }
1072
1073   SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), TREE_PURPOSE (link));
1074 }
1075
1076 /* At the end of a function, all labels declared within the fucntion
1077    go out of scope.  BLOCK is the top-level block for the 
1078    function.  */
1079
1080 static void
1081 pop_labels (block)
1082      tree block;
1083 {
1084   tree link;
1085
1086   /* Clear out the definitions of all label names, since their scopes
1087      end here.  */
1088   for (link = named_labels; link; link = TREE_CHAIN (link))
1089     {
1090       pop_label (link);
1091       /* Put the labels into the "variables" of the top-level block,
1092          so debugger can see them.  */
1093       TREE_CHAIN (TREE_VALUE (link)) = BLOCK_VARS (block);
1094       BLOCK_VARS (block) = TREE_VALUE (link);
1095     }
1096
1097   named_labels = NULL_TREE;
1098 }
1099
1100 /* Exit a binding level.
1101    Pop the level off, and restore the state of the identifier-decl mappings
1102    that were in effect when this level was entered.
1103
1104    If KEEP == 1, this level had explicit declarations, so
1105    and create a "block" (a BLOCK node) for the level
1106    to record its declarations and subblocks for symbol table output.
1107
1108    If FUNCTIONBODY is nonzero, this level is the body of a function,
1109    so create a block as if KEEP were set and also clear out all
1110    label names.
1111
1112    If REVERSE is nonzero, reverse the order of decls before putting
1113    them into the BLOCK.  */
1114
1115 tree
1116 poplevel (keep, reverse, functionbody)
1117      int keep;
1118      int reverse;
1119      int functionbody;
1120 {
1121   register tree link;
1122   /* The chain of decls was accumulated in reverse order.
1123      Put it into forward order, just for cleanliness.  */
1124   tree decls;
1125   int tmp = functionbody;
1126   int real_functionbody;
1127   tree tags;
1128   tree subblocks;
1129   tree block = NULL_TREE;
1130   tree decl;
1131   int block_previously_created;
1132   int leaving_for_scope;
1133
1134   if (current_function && !doing_semantic_analysis_p ())
1135     return NULL_TREE;
1136
1137   my_friendly_assert (current_binding_level->parm_flag != 2,
1138                       19990916);
1139
1140   real_functionbody = (current_binding_level->keep == 2
1141                        ? ((functionbody = 0), tmp) : functionbody);
1142   tags = functionbody >= 0 ? current_binding_level->tags : 0;
1143   subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
1144
1145   my_friendly_assert (!current_binding_level->class_shadowed,
1146                       19990414);
1147
1148   /* We used to use KEEP == 2 to indicate that the new block should go
1149      at the beginning of the list of blocks at this binding level,
1150      rather than the end.  This hack is no longer used.  */
1151   my_friendly_assert (keep == 0 || keep == 1, 0);
1152
1153   GNU_xref_end_scope ((HOST_WIDE_INT) current_binding_level,
1154                       (HOST_WIDE_INT) current_binding_level->level_chain,
1155                       current_binding_level->parm_flag,
1156                       current_binding_level->keep);
1157
1158   if (current_binding_level->keep == 1)
1159     keep = 1;
1160
1161   /* Get the decls in the order they were written.
1162      Usually current_binding_level->names is in reverse order.
1163      But parameter decls were previously put in forward order.  */
1164
1165   if (reverse)
1166     current_binding_level->names
1167       = decls = nreverse (current_binding_level->names);
1168   else
1169     decls = current_binding_level->names;
1170
1171   /* Output any nested inline functions within this block
1172      if they weren't already output.  */
1173   for (decl = decls; decl; decl = TREE_CHAIN (decl))
1174     if (TREE_CODE (decl) == FUNCTION_DECL
1175         && ! TREE_ASM_WRITTEN (decl)
1176         && DECL_INITIAL (decl) != NULL_TREE
1177         && TREE_ADDRESSABLE (decl)
1178         && decl_function_context (decl) == current_function_decl)
1179       {
1180         /* If this decl was copied from a file-scope decl
1181            on account of a block-scope extern decl,
1182            propagate TREE_ADDRESSABLE to the file-scope decl.  */
1183         if (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE)
1184           TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (decl)) = 1;
1185         else
1186           {
1187             push_function_context ();
1188             output_inline_function (decl);
1189             pop_function_context ();
1190           }
1191       }
1192
1193   /* When not in function-at-a-time mode, expand_end_bindings will
1194      warn about unused variables.  But, in function-at-a-time mode
1195      expand_end_bindings is not passed the list of variables in the
1196      current scope, and therefore no warning is emitted.  So, we
1197      explicitly warn here.  */
1198   if (!processing_template_decl)
1199     warn_about_unused_variables (getdecls ());
1200
1201   /* If there were any declarations or structure tags in that level,
1202      or if this level is a function body,
1203      create a BLOCK to record them for the life of this function.  */
1204   block = NULL_TREE;
1205   block_previously_created = (current_binding_level->this_block != NULL_TREE);
1206   if (block_previously_created)
1207     block = current_binding_level->this_block;
1208   else if (keep == 1 || functionbody)
1209     block = make_node (BLOCK);
1210   if (block != NULL_TREE)
1211     {
1212       if (block_previously_created)
1213         {
1214           if (decls || tags || subblocks)
1215             {
1216               if (BLOCK_VARS (block))
1217                 warning ("internal compiler error: debugging info corrupted");
1218
1219               BLOCK_VARS (block) = decls;
1220
1221               /* We can have previous subblocks and new subblocks when
1222                  doing fixup_gotos with complex cleanups.  We chain the new
1223                  subblocks onto the end of any pre-existing subblocks.  */
1224               BLOCK_SUBBLOCKS (block) = chainon (BLOCK_SUBBLOCKS (block),
1225                                                  subblocks);
1226             }
1227         }
1228       else
1229         {
1230           BLOCK_VARS (block) = decls;
1231           BLOCK_SUBBLOCKS (block) = subblocks;
1232         }
1233     }
1234
1235   /* In each subblock, record that this is its superior.  */
1236   if (keep >= 0)
1237     for (link = subblocks; link; link = TREE_CHAIN (link))
1238       BLOCK_SUPERCONTEXT (link) = block;
1239
1240   /* We still support the old for-scope rules, whereby the variables
1241      in a for-init statement were in scope after the for-statement
1242      ended.  We only use the new rules in flag_new_for_scope is
1243      nonzero.  */
1244   leaving_for_scope 
1245     = current_binding_level->is_for_scope && flag_new_for_scope == 1;
1246
1247   /* Remove declarations for all the DECLs in this level.  */
1248   for (link = decls; link; link = TREE_CHAIN (link))
1249     {
1250       if (leaving_for_scope && TREE_CODE (link) == VAR_DECL)
1251         {
1252           tree outer_binding 
1253             = TREE_CHAIN (IDENTIFIER_BINDING (DECL_NAME (link)));
1254           tree ns_binding;
1255
1256           if (!outer_binding)
1257             ns_binding = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (link));
1258           else
1259             ns_binding = NULL_TREE;
1260
1261           if (outer_binding 
1262               && (BINDING_LEVEL (outer_binding) 
1263                   == current_binding_level->level_chain))
1264             /* We have something like:
1265                
1266                  int i;
1267                  for (int i; ;);
1268                  
1269                and we are leaving the `for' scope.  There's no reason to
1270                keep the binding of the inner `i' in this case.  */
1271             pop_binding (DECL_NAME (link), link);
1272           else if ((outer_binding 
1273                     && (TREE_CODE (BINDING_VALUE (outer_binding)) 
1274                         == TYPE_DECL))
1275                    || (ns_binding 
1276                        && TREE_CODE (ns_binding) == TYPE_DECL))
1277             /* Here, we have something like:
1278
1279                  typedef int I;
1280
1281                  void f () {
1282                    for (int I; ;);
1283                  }
1284
1285                We must pop the for-scope binding so we know what's a
1286                type and what isn't.  */
1287             pop_binding (DECL_NAME (link), link);
1288           else
1289             {
1290               /* Mark this VAR_DECL as dead so that we can tell we left it
1291                  there only for backward compatibility.  */
1292               DECL_DEAD_FOR_LOCAL (link) = 1;
1293               
1294               /* Keep track of what should of have happenned when we
1295                  popped the binding.  */
1296               if (outer_binding && BINDING_VALUE (outer_binding))
1297                 DECL_SHADOWED_FOR_VAR (link) 
1298                   = BINDING_VALUE (outer_binding);
1299
1300               /* Add it to the list of dead variables in the next
1301                  outermost binding to that we can remove these when we
1302                  leave that binding.  */
1303               current_binding_level->level_chain->dead_vars_from_for
1304                 = tree_cons (NULL_TREE, link,
1305                              current_binding_level->level_chain->
1306                              dead_vars_from_for);
1307
1308               /* Although we don't pop the CPLUS_BINDING, we do clear
1309                  its BINDING_LEVEL since the level is going away now.  */
1310               BINDING_LEVEL (IDENTIFIER_BINDING (DECL_NAME (link)))
1311                 = 0;
1312             }
1313         }
1314       else 
1315         {
1316           /* Remove the binding.  */
1317           decl = link;
1318           if (TREE_CODE (decl) == TREE_LIST)
1319             decl = TREE_VALUE (decl);
1320           if (TREE_CODE_CLASS (TREE_CODE (decl)) == 'd')
1321             pop_binding (DECL_NAME (decl), decl);
1322           else if (TREE_CODE (decl) == OVERLOAD)
1323             pop_binding (DECL_NAME (OVL_FUNCTION (decl)), decl);
1324           else 
1325             my_friendly_abort (0);
1326         }
1327     }
1328
1329   /* Remove declarations for any `for' variables from inner scopes
1330      that we kept around.  */
1331   for (link = current_binding_level->dead_vars_from_for;
1332        link; link = TREE_CHAIN (link))
1333     pop_binding (DECL_NAME (TREE_VALUE (link)), TREE_VALUE (link));
1334
1335   /* Restore the IDENTIFIER_TYPE_VALUEs.  */
1336   for (link = current_binding_level->type_shadowed;
1337        link; link = TREE_CHAIN (link))
1338     SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
1339
1340   /* Restore the IDENTIFIER_LABEL_VALUEs for local labels.  */
1341   for (link = current_binding_level->shadowed_labels;
1342        link; 
1343        link = TREE_CHAIN (link))
1344     pop_label (link);
1345
1346   /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
1347      list if a `using' declaration put them there.  The debugging
1348      back-ends won't understand OVERLOAD, so we remove them here.
1349      Because the BLOCK_VARS are (temporarily) shared with
1350      CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
1351      popped all the bindings.  */
1352   if (block)
1353     {
1354       tree* d;
1355
1356       for (d = &BLOCK_VARS (block); *d; )
1357         {
1358           if (TREE_CODE (*d) == TREE_LIST)
1359             *d = TREE_CHAIN (*d);
1360           else
1361             d = &TREE_CHAIN (*d);
1362         }
1363     }
1364
1365   /* If the level being exited is the top level of a function,
1366      check over all the labels.  */
1367   if (functionbody)
1368     {
1369       /* Since this is the top level block of a function, the vars are
1370          the function's parameters.  Don't leave them in the BLOCK
1371          because they are found in the FUNCTION_DECL instead.  */
1372       BLOCK_VARS (block) = 0;
1373       pop_labels (block);
1374     }
1375
1376   /* Any uses of undefined labels now operate under constraints
1377      of next binding contour.  */
1378   if (current_function)
1379     {
1380       struct binding_level *level_chain;
1381       level_chain = current_binding_level->level_chain;
1382       if (level_chain)
1383         {
1384           struct named_label_list *labels;
1385           for (labels = named_label_uses; labels; labels = labels->next)
1386             if (labels->binding_level == current_binding_level)
1387               {
1388                 labels->binding_level = level_chain;
1389                 labels->names_in_scope = level_chain->names;
1390               }
1391         }
1392     }
1393
1394   tmp = current_binding_level->keep;
1395
1396   pop_binding_level ();
1397   if (functionbody)
1398     DECL_INITIAL (current_function_decl) = block;
1399   else if (block)
1400     {
1401       if (!block_previously_created)
1402         current_binding_level->blocks
1403           = chainon (current_binding_level->blocks, block);
1404     }
1405   /* If we did not make a block for the level just exited,
1406      any blocks made for inner levels
1407      (since they cannot be recorded as subblocks in that level)
1408      must be carried forward so they will later become subblocks
1409      of something else.  */
1410   else if (subblocks)
1411     current_binding_level->blocks
1412       = chainon (current_binding_level->blocks, subblocks);
1413
1414   /* Each and every BLOCK node created here in `poplevel' is important
1415      (e.g. for proper debugging information) so if we created one
1416      earlier, mark it as "used".  */
1417   if (block)
1418     TREE_USED (block) = 1;
1419
1420   /* Take care of compiler's internal binding structures.  */
1421   if (tmp == 2)
1422     {
1423       tree scope_stmts;
1424
1425       scope_stmts 
1426         = add_scope_stmt (/*begin_p=*/0, /*partial_p=*/1);
1427       if (block)
1428         {
1429           SCOPE_STMT_BLOCK (TREE_PURPOSE (scope_stmts)) = block;
1430           SCOPE_STMT_BLOCK (TREE_VALUE (scope_stmts)) = block;
1431         }
1432
1433       block = poplevel (keep, reverse, functionbody);
1434     }
1435
1436   return block;
1437 }
1438
1439 /* Delete the node BLOCK from the current binding level.
1440    This is used for the block inside a stmt expr ({...})
1441    so that the block can be reinserted where appropriate.  */
1442
1443 void
1444 delete_block (block)
1445      tree block;
1446 {
1447   tree t;
1448   if (current_binding_level->blocks == block)
1449     current_binding_level->blocks = TREE_CHAIN (block);
1450   for (t = current_binding_level->blocks; t;)
1451     {
1452       if (TREE_CHAIN (t) == block)
1453         TREE_CHAIN (t) = TREE_CHAIN (block);
1454       else
1455         t = TREE_CHAIN (t);
1456     }
1457   TREE_CHAIN (block) = NULL_TREE;
1458   /* Clear TREE_USED which is always set by poplevel.
1459      The flag is set again if insert_block is called.  */
1460   TREE_USED (block) = 0;
1461 }
1462
1463 /* Insert BLOCK at the end of the list of subblocks of the
1464    current binding level.  This is used when a BIND_EXPR is expanded,
1465    to handle the BLOCK node inside the BIND_EXPR.  */
1466
1467 void
1468 insert_block (block)
1469      tree block;
1470 {
1471   TREE_USED (block) = 1;
1472   current_binding_level->blocks
1473     = chainon (current_binding_level->blocks, block);
1474 }
1475
1476 /* Set the BLOCK node for the innermost scope
1477    (the one we are currently in).  */
1478
1479 void
1480 set_block (block)
1481     register tree block;
1482 {
1483   current_binding_level->this_block = block;
1484 }
1485
1486 /* Do a pushlevel for class declarations.  */
1487
1488 void
1489 pushlevel_class ()
1490 {
1491   register struct binding_level *newlevel;
1492
1493   /* Reuse or create a struct for this binding level.  */
1494 #if defined(DEBUG_CP_BINDING_LEVELS)
1495   if (0)
1496 #else /* !defined(DEBUG_CP_BINDING_LEVELS) */
1497   if (free_binding_level)
1498 #endif /* !defined(DEBUG_CP_BINDING_LEVELS) */
1499     {
1500       newlevel = free_binding_level;
1501       free_binding_level = free_binding_level->level_chain;
1502     }
1503   else
1504     newlevel = make_binding_level ();
1505
1506 #if defined(DEBUG_CP_BINDING_LEVELS)
1507   is_class_level = 1;
1508 #endif /* defined(DEBUG_CP_BINDING_LEVELS) */
1509
1510   push_binding_level (newlevel, 0, 0);
1511
1512   class_binding_level = current_binding_level;
1513   class_binding_level->parm_flag = 2;
1514 }
1515
1516 /* ...and a poplevel for class declarations.  */
1517
1518 void
1519 poplevel_class ()
1520 {
1521   register struct binding_level *level = class_binding_level;
1522   tree shadowed;
1523
1524   my_friendly_assert (level != 0, 354);
1525   
1526   /* If we're leaving a toplevel class, don't bother to do the setting
1527      of IDENTIFIER_CLASS_VALUE to NULL_TREE, since first of all this slot
1528      shouldn't even be used when current_class_type isn't set, and second,
1529      if we don't touch it here, we're able to use the cache effect if the
1530      next time we're entering a class scope, it is the same class.  */
1531   if (current_class_depth != 1)
1532     {
1533       struct binding_level* b;
1534
1535       /* Clear out our IDENTIFIER_CLASS_VALUEs.  */
1536       for (shadowed = level->class_shadowed;
1537            shadowed;
1538            shadowed = TREE_CHAIN (shadowed))
1539         IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed)) = NULL_TREE;
1540         
1541       /* Find the next enclosing class, and recreate
1542          IDENTIFIER_CLASS_VALUEs appropriate for that class.  */
1543       b = level->level_chain;
1544       while (b && b->parm_flag != 2)
1545         b = b->level_chain;
1546
1547       if (b)
1548         for (shadowed = b->class_shadowed; 
1549              shadowed; 
1550              shadowed = TREE_CHAIN (shadowed))
1551           {
1552             tree t;
1553
1554             t = IDENTIFIER_BINDING (TREE_PURPOSE (shadowed));
1555             while (t && BINDING_LEVEL (t) != b)
1556               t = TREE_CHAIN (t);
1557       
1558             if (t)
1559               IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed)) 
1560                 = BINDING_VALUE (t);
1561           }
1562     }
1563   else
1564     /* Remember to save what IDENTIFIER's were bound in this scope so we
1565        can recover from cache misses.  */
1566     {
1567       previous_class_type = current_class_type;
1568       previous_class_values = class_binding_level->class_shadowed;
1569     }
1570   for (shadowed = level->type_shadowed;
1571        shadowed;
1572        shadowed = TREE_CHAIN (shadowed))
1573     SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
1574
1575   /* Remove the bindings for all of the class-level declarations.  */
1576   for (shadowed = level->class_shadowed; 
1577        shadowed; 
1578        shadowed = TREE_CHAIN (shadowed))
1579     pop_binding (TREE_PURPOSE (shadowed), TREE_TYPE (shadowed));
1580
1581   GNU_xref_end_scope ((HOST_WIDE_INT) class_binding_level,
1582                       (HOST_WIDE_INT) class_binding_level->level_chain,
1583                       class_binding_level->parm_flag,
1584                       class_binding_level->keep);
1585
1586   /* Now, pop out of the binding level which we created up in the
1587      `pushlevel_class' routine.  */
1588 #if defined(DEBUG_CP_BINDING_LEVELS)
1589   is_class_level = 1;
1590 #endif /* defined(DEBUG_CP_BINDING_LEVELS) */
1591
1592   pop_binding_level ();
1593 }
1594
1595 /* We are entering the scope of a class.  Clear IDENTIFIER_CLASS_VALUE
1596    for any names in enclosing classes.  */
1597
1598 void
1599 clear_identifier_class_values ()
1600 {
1601   tree t;
1602
1603   if (!class_binding_level)
1604     return;
1605
1606   for (t = class_binding_level->class_shadowed;
1607        t;
1608        t = TREE_CHAIN (t))
1609     IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
1610 }
1611
1612 /* Returns non-zero if T is a virtual function table.  */
1613
1614 int
1615 vtable_decl_p (t, data)
1616      tree t;
1617      void *data ATTRIBUTE_UNUSED;
1618 {
1619   return (TREE_CODE (t) == VAR_DECL && DECL_VIRTUAL_P (t));
1620 }
1621
1622 /* Returns non-zero if T is a TYPE_DECL for a type with virtual
1623    functions.  */
1624
1625 int
1626 vtype_decl_p (t, data)
1627      tree t;
1628      void *data ATTRIBUTE_UNUSED;
1629 {
1630   return (TREE_CODE (t) == TYPE_DECL
1631           && TREE_TYPE (t) != error_mark_node
1632           && TYPE_LANG_SPECIFIC (TREE_TYPE (t))
1633           && CLASSTYPE_VSIZE (TREE_TYPE (t)));
1634 }
1635
1636 /* Return the declarations that are members of the namespace NS.  */
1637
1638 tree
1639 cp_namespace_decls (ns)
1640      tree ns;
1641 {
1642   return NAMESPACE_LEVEL (ns)->names;
1643 }
1644
1645 /* Walk all the namespaces contained NAMESPACE, including NAMESPACE
1646    itself, calling F for each.  The DATA is passed to F as well.  */
1647
1648 static int
1649 walk_namespaces_r (namespace, f, data)
1650      tree namespace;
1651      walk_namespaces_fn f;
1652      void *data;
1653 {
1654   tree current;
1655   int result = 0;
1656
1657   result |= (*f) (namespace, data);
1658
1659   for (current = cp_namespace_decls (namespace);
1660        current;
1661        current = TREE_CHAIN (current))
1662     {
1663       if (TREE_CODE (current) != NAMESPACE_DECL
1664           || DECL_NAMESPACE_ALIAS (current))
1665         continue;
1666       if (!DECL_LANG_SPECIFIC (current))
1667         {
1668           /* Hmm. std. */
1669           my_friendly_assert (current == std_node, 393);
1670           continue;
1671         }
1672
1673       /* We found a namespace.  */
1674       result |= walk_namespaces_r (current, f, data);
1675     }
1676
1677   return result;
1678 }
1679
1680 /* Walk all the namespaces, calling F for each.  The DATA is passed to
1681    F as well.  */
1682
1683 int
1684 walk_namespaces (f, data)
1685      walk_namespaces_fn f;
1686      void *data;
1687 {
1688   return walk_namespaces_r (global_namespace, f, data);
1689 }
1690
1691 struct walk_globals_data {
1692   walk_globals_pred p;
1693   walk_globals_fn f;
1694   void *data;
1695 };
1696
1697 /* Walk the global declarations in NAMESPACE.  Whenever one is found
1698    for which P returns non-zero, call F with its address.  If any call
1699    to F returns a non-zero value, return a non-zero value.  */
1700
1701 static int 
1702 walk_globals_r (namespace, data)
1703      tree namespace;
1704      void *data;
1705 {
1706   struct walk_globals_data* wgd = (struct walk_globals_data *) data;
1707   walk_globals_pred p = wgd->p;
1708   walk_globals_fn f = wgd->f;
1709   void *d = wgd->data;
1710   tree *t;
1711   int result = 0;
1712
1713   t = &NAMESPACE_LEVEL (namespace)->names;
1714
1715   while (*t)
1716     {
1717       tree glbl = *t;
1718
1719       if ((*p) (glbl, d))
1720         result |= (*f) (t, d);
1721
1722       /* If F changed *T, then *T still points at the next item to
1723          examine.  */
1724       if (*t == glbl)
1725         t = &TREE_CHAIN (*t);
1726     }
1727
1728   return result;
1729 }
1730
1731 /* Walk the global declarations.  Whenever one is found for which P
1732    returns non-zero, call F with its address.  If any call to F
1733    returns a non-zero value, return a non-zero value.  */
1734
1735 int
1736 walk_globals (p, f, data)
1737      walk_globals_pred p;
1738      walk_globals_fn f;
1739      void *data;
1740 {
1741   struct walk_globals_data wgd;
1742   wgd.p = p;
1743   wgd.f = f;
1744   wgd.data = data;
1745
1746   return walk_namespaces (walk_globals_r, &wgd);
1747 }
1748
1749 /* Call wrapup_globals_declarations for the globals in NAMESPACE.  If
1750    DATA is non-NULL, this is the last time we will call
1751    wrapup_global_declarations for this NAMESPACE.  */
1752
1753 int
1754 wrapup_globals_for_namespace (namespace, data)
1755      tree namespace;
1756      void *data;
1757 {
1758   tree globals = cp_namespace_decls (namespace);
1759   int len = list_length (globals);
1760   tree *vec = (tree *) alloca (sizeof (tree) * len);
1761   int i;
1762   int result;
1763   tree decl;
1764   int last_time = (data != 0);
1765
1766   if (last_time && namespace == global_namespace)
1767     /* Let compile_file handle the global namespace.  */
1768     return 0;
1769
1770   /* Process the decls in reverse order--earliest first.
1771      Put them into VEC from back to front, then take out from front.  */
1772   
1773   for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
1774     vec[len - i - 1] = decl;
1775   
1776   if (last_time)
1777     {
1778       check_global_declarations (vec, len);
1779       return 0;
1780     }
1781
1782   /* Temporarily mark vtables as external.  That prevents
1783      wrapup_global_declarations from writing them out; we must process
1784      them ourselves in finish_vtable_vardecl.  */
1785   for (i = 0; i < len; ++i)
1786     if (vtable_decl_p (vec[i], /*data=*/0) && !DECL_EXTERNAL (vec[i]))
1787       {
1788         DECL_NOT_REALLY_EXTERN (vec[i]) = 1;
1789         DECL_EXTERNAL (vec[i]) = 1;
1790       }
1791
1792   /* Write out any globals that need to be output.  */
1793   result = wrapup_global_declarations (vec, len);
1794
1795   /* Undo the hack to DECL_EXTERNAL above.  */
1796   for (i = 0; i < len; ++i)
1797     if (vtable_decl_p (vec[i], /*data=*/0)
1798         && DECL_NOT_REALLY_EXTERN (vec[i]))
1799       {
1800         DECL_NOT_REALLY_EXTERN (vec[i]) = 0;
1801         DECL_EXTERNAL (vec[i]) = 0;
1802       }
1803
1804   return result;
1805 }
1806
1807 \f
1808 /* Mark ARG (which is really a struct binding_level **) for GC.  */
1809
1810 static void
1811 mark_binding_level (arg)
1812      void *arg;
1813 {
1814   struct binding_level *lvl = *(struct binding_level **)arg;
1815
1816   while (lvl)
1817     {
1818       ggc_mark_tree (lvl->names);
1819       ggc_mark_tree (lvl->tags);
1820       ggc_mark_tree (lvl->usings);
1821       ggc_mark_tree (lvl->using_directives);
1822       ggc_mark_tree (lvl->class_shadowed);
1823       ggc_mark_tree (lvl->type_shadowed);
1824       ggc_mark_tree (lvl->shadowed_labels);
1825       ggc_mark_tree (lvl->blocks);
1826       ggc_mark_tree (lvl->this_block);
1827       ggc_mark_tree (lvl->incomplete);
1828       ggc_mark_tree (lvl->dead_vars_from_for);
1829
1830       lvl = lvl->level_chain;
1831     }
1832 }
1833 \f
1834 /* For debugging.  */
1835 static int no_print_functions = 0;
1836 static int no_print_builtins = 0;
1837
1838 void
1839 print_binding_level (lvl)
1840      struct binding_level *lvl;
1841 {
1842   tree t;
1843   int i = 0, len;
1844   fprintf (stderr, " blocks=");
1845   fprintf (stderr, HOST_PTR_PRINTF, lvl->blocks);
1846   fprintf (stderr, " n_incomplete=%d parm_flag=%d keep=%d",
1847            list_length (lvl->incomplete), lvl->parm_flag, lvl->keep);
1848   if (lvl->tag_transparent)
1849     fprintf (stderr, " tag-transparent");
1850   if (lvl->more_cleanups_ok)
1851     fprintf (stderr, " more-cleanups-ok");
1852   if (lvl->have_cleanups)
1853     fprintf (stderr, " have-cleanups");
1854   fprintf (stderr, "\n");
1855   if (lvl->names)
1856     {
1857       fprintf (stderr, " names:\t");
1858       /* We can probably fit 3 names to a line?  */
1859       for (t = lvl->names; t; t = TREE_CHAIN (t))
1860         {
1861           if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL)) 
1862             continue;
1863           if (no_print_builtins
1864               && (TREE_CODE (t) == TYPE_DECL)
1865               && (!strcmp (DECL_SOURCE_FILE (t),"<built-in>")))
1866             continue;
1867
1868           /* Function decls tend to have longer names.  */
1869           if (TREE_CODE (t) == FUNCTION_DECL)
1870             len = 3;
1871           else
1872             len = 2;
1873           i += len;
1874           if (i > 6)
1875             {
1876               fprintf (stderr, "\n\t");
1877               i = len;
1878             }
1879           print_node_brief (stderr, "", t, 0);
1880           if (t == error_mark_node)
1881             break;
1882         }
1883       if (i)
1884         fprintf (stderr, "\n");
1885     }
1886   if (lvl->tags)
1887     {
1888       fprintf (stderr, " tags:\t");
1889       i = 0;
1890       for (t = lvl->tags; t; t = TREE_CHAIN (t))
1891         {
1892           if (TREE_PURPOSE (t) == NULL_TREE)
1893             len = 3;
1894           else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t)))
1895             len = 2;
1896           else
1897             len = 4;
1898           i += len;
1899           if (i > 5)
1900             {
1901               fprintf (stderr, "\n\t");
1902               i = len;
1903             }
1904           if (TREE_PURPOSE (t) == NULL_TREE)
1905             {
1906               print_node_brief (stderr, "<unnamed-typedef", TREE_VALUE (t), 0);
1907               fprintf (stderr, ">");
1908             }
1909           else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t)))
1910             print_node_brief (stderr, "", TREE_VALUE (t), 0);
1911           else
1912             {
1913               print_node_brief (stderr, "<typedef", TREE_PURPOSE (t), 0);
1914               print_node_brief (stderr, "", TREE_VALUE (t), 0);
1915               fprintf (stderr, ">");
1916             }
1917         }
1918       if (i)
1919         fprintf (stderr, "\n");
1920     }
1921   if (lvl->class_shadowed)
1922     {
1923       fprintf (stderr, " class-shadowed:");
1924       for (t = lvl->class_shadowed; t; t = TREE_CHAIN (t))
1925         {
1926           fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1927         }
1928       fprintf (stderr, "\n");
1929     }
1930   if (lvl->type_shadowed)
1931     {
1932       fprintf (stderr, " type-shadowed:");
1933       for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
1934         {
1935           fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1936         }
1937       fprintf (stderr, "\n");
1938     }
1939 }
1940
1941 void
1942 print_other_binding_stack (stack)
1943      struct binding_level *stack;
1944 {
1945   struct binding_level *level;
1946   for (level = stack; level != global_binding_level; level = level->level_chain)
1947     {
1948       fprintf (stderr, "binding level ");
1949       fprintf (stderr, HOST_PTR_PRINTF, level);
1950       fprintf (stderr, "\n");
1951       print_binding_level (level);
1952     }
1953 }
1954
1955 void
1956 print_binding_stack ()
1957 {
1958   struct binding_level *b;
1959   fprintf (stderr, "current_binding_level=");
1960   fprintf (stderr, HOST_PTR_PRINTF, current_binding_level);
1961   fprintf (stderr, "\nclass_binding_level=");
1962   fprintf (stderr, HOST_PTR_PRINTF, class_binding_level);
1963   fprintf (stderr, "\nglobal_binding_level=");
1964   fprintf (stderr, HOST_PTR_PRINTF, global_binding_level);
1965   fprintf (stderr, "\n");
1966   if (class_binding_level)
1967     {
1968       for (b = class_binding_level; b; b = b->level_chain)
1969         if (b == current_binding_level)
1970           break;
1971       if (b)
1972         b = class_binding_level;
1973       else
1974         b = current_binding_level;
1975     }
1976   else
1977     b = current_binding_level;
1978   print_other_binding_stack (b);
1979   fprintf (stderr, "global:\n");
1980   print_binding_level (global_binding_level);
1981 }
1982
1983 /* Namespace binding access routines: The namespace_bindings field of
1984    the identifier is polymorphic, with three possible values:
1985    NULL_TREE, a list of CPLUS_BINDINGS, or any other tree_node
1986    indicating the BINDING_VALUE of global_namespace. */
1987
1988 /* Check whether the a binding for the name to scope is known.
1989    Assumes that the bindings of the name are already a list
1990    of bindings. Returns the binding found, or NULL_TREE. */
1991
1992 static tree
1993 find_binding (name, scope)
1994      tree name;
1995      tree scope;
1996 {
1997   tree iter, prev = NULL_TREE;
1998
1999   scope = ORIGINAL_NAMESPACE (scope);
2000   
2001   for (iter = IDENTIFIER_NAMESPACE_BINDINGS (name); iter;
2002        iter = TREE_CHAIN (iter))
2003     {
2004       my_friendly_assert (TREE_CODE (iter) == CPLUS_BINDING, 374);
2005       if (BINDING_SCOPE (iter) == scope)
2006         {
2007           /* Move binding found to the front of the list, so
2008              subsequent lookups will find it faster. */
2009           if (prev)
2010             {
2011               TREE_CHAIN (prev) = TREE_CHAIN (iter);
2012               TREE_CHAIN (iter) = IDENTIFIER_NAMESPACE_BINDINGS (name);
2013               IDENTIFIER_NAMESPACE_BINDINGS (name) = iter;
2014             }
2015           return iter;
2016         }
2017       prev = iter;
2018     }
2019   return NULL_TREE;
2020 }
2021
2022 /* Always returns a binding for name in scope. If the
2023    namespace_bindings is not a list, convert it to one first.
2024    If no binding is found, make a new one. */
2025
2026 tree
2027 binding_for_name (name, scope)
2028      tree name;
2029      tree scope;
2030 {
2031   tree b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2032   tree result;
2033
2034   scope = ORIGINAL_NAMESPACE (scope);
2035   
2036   if (b && TREE_CODE (b) != CPLUS_BINDING)
2037     {
2038       /* Get rid of optimization for global scope. */
2039       IDENTIFIER_NAMESPACE_BINDINGS (name) = NULL_TREE;
2040       BINDING_VALUE (binding_for_name (name, global_namespace)) = b;
2041       b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2042     }
2043   if (b && (result = find_binding (name, scope)))
2044     return result;
2045   /* Not found, make a new one. */
2046   result = make_node (CPLUS_BINDING);
2047   TREE_CHAIN (result) = b;
2048   IDENTIFIER_NAMESPACE_BINDINGS (name) = result;
2049   BINDING_SCOPE (result) = scope;
2050   BINDING_TYPE (result) = NULL_TREE;
2051   BINDING_VALUE (result) = NULL_TREE;
2052   return result;
2053 }
2054
2055 /* Return the binding value for name in scope, considering that
2056    namespace_binding may or may not be a list of CPLUS_BINDINGS. */
2057
2058 tree
2059 namespace_binding (name, scope)
2060      tree name;
2061      tree scope;
2062 {
2063   tree b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2064   if (b == NULL_TREE)
2065     return NULL_TREE;
2066   if (scope == NULL_TREE)
2067     scope = global_namespace;
2068   if (TREE_CODE (b) != CPLUS_BINDING)
2069     return (scope == global_namespace) ? b : NULL_TREE;
2070   name = find_binding (name,scope);
2071   if (name == NULL_TREE)
2072     return name;
2073   return BINDING_VALUE (name);
2074 }
2075
2076 /* Set the binding value for name in scope. If modifying the binding
2077    of global_namespace is attempted, try to optimize it. */
2078
2079 void
2080 set_namespace_binding (name, scope, val)
2081      tree name;
2082      tree scope;
2083      tree val;
2084 {
2085   tree b;
2086
2087   if (scope == NULL_TREE)
2088     scope = global_namespace;
2089   
2090   if (scope == global_namespace)
2091     {
2092       b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2093       if (b == NULL_TREE || TREE_CODE (b) != CPLUS_BINDING)
2094         {
2095           IDENTIFIER_NAMESPACE_BINDINGS (name) = val;
2096           return;
2097         }
2098     }
2099   b = binding_for_name (name, scope);
2100   BINDING_VALUE (b) = val;
2101 }
2102
2103 /* Push into the scope of the NAME namespace.  If NAME is NULL_TREE, then we
2104    select a name that is unique to this compilation unit.  */
2105
2106 void
2107 push_namespace (name)
2108      tree name;
2109 {
2110   tree d = NULL_TREE;
2111   int need_new = 1;
2112   int implicit_use = 0;
2113   int global = 0;
2114   if (!global_namespace)
2115     {
2116       /* This must be ::. */
2117       my_friendly_assert (name == get_identifier ("::"), 377);
2118       global = 1;
2119     }
2120   else if (!name)
2121     {
2122       /* The name of anonymous namespace is unique for the translation
2123          unit.  */
2124       if (!anonymous_namespace_name)
2125         anonymous_namespace_name = get_file_function_name ('N');
2126       name = anonymous_namespace_name;
2127       d = IDENTIFIER_NAMESPACE_VALUE (name);
2128       if (d)
2129         /* Reopening anonymous namespace.  */
2130         need_new = 0;
2131       implicit_use = 1;
2132     }
2133   else if (current_namespace == global_namespace
2134            && name == DECL_NAME (std_node))
2135     {
2136       in_std++;
2137       return;
2138     }
2139   else
2140     {
2141       /* Check whether this is an extended namespace definition. */
2142       d = IDENTIFIER_NAMESPACE_VALUE (name);
2143       if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
2144         {
2145           need_new = 0;
2146           if (DECL_NAMESPACE_ALIAS (d))
2147             {
2148               cp_error ("namespace alias `%D' not allowed here, assuming `%D'",
2149                         d, DECL_NAMESPACE_ALIAS (d));
2150               d = DECL_NAMESPACE_ALIAS (d);
2151             }
2152         }
2153     }
2154   
2155   if (need_new)
2156     {
2157       /* Make a new namespace, binding the name to it. */
2158       d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
2159       /* The global namespace is not pushed, and the global binding
2160          level is set elsewhere.  */
2161       if (!global)
2162         {
2163           d = pushdecl (d);
2164           pushlevel (0);
2165           declare_namespace_level ();
2166           NAMESPACE_LEVEL (d) = current_binding_level;
2167         }
2168     }
2169   else
2170     resume_binding_level (NAMESPACE_LEVEL (d));
2171
2172   if (implicit_use)
2173     do_using_directive (d);
2174   /* Enter the name space. */
2175   current_namespace = d;
2176 }
2177
2178 /* Pop from the scope of the current namespace.  */
2179
2180 void
2181 pop_namespace ()
2182 {
2183   if (current_namespace == global_namespace)
2184     {
2185       my_friendly_assert (in_std>0, 980421);
2186       in_std--;
2187       return;
2188     }
2189   current_namespace = CP_DECL_CONTEXT (current_namespace);
2190   /* The binding level is not popped, as it might be re-opened later.  */
2191   suspend_binding_level ();
2192 }
2193
2194 /* Push into the scope of the namespace NS, even if it is deeply
2195    nested within another namespace.  */
2196
2197 void
2198 push_nested_namespace (ns)
2199      tree ns;
2200 {
2201   if (ns == global_namespace)
2202     push_to_top_level ();
2203   else
2204     {
2205       push_nested_namespace (CP_DECL_CONTEXT (ns));
2206       push_namespace (DECL_NAME (ns));
2207     }
2208 }
2209
2210 /* Pop back from the scope of the namespace NS, which was previously
2211    entered with push_nested_namespace.  */
2212      
2213 void
2214 pop_nested_namespace (ns)
2215      tree ns;
2216 {
2217   while (ns != global_namespace)
2218     {
2219       pop_namespace ();
2220       ns = CP_DECL_CONTEXT (ns);
2221     }
2222
2223   pop_from_top_level ();
2224 }
2225
2226 \f
2227 /* Subroutines for reverting temporarily to top-level for instantiation
2228    of templates and such.  We actually need to clear out the class- and
2229    local-value slots of all identifiers, so that only the global values
2230    are at all visible.  Simply setting current_binding_level to the global
2231    scope isn't enough, because more binding levels may be pushed.  */
2232 struct saved_scope *scope_chain;
2233
2234 /* Mark ST for GC.  */
2235
2236 static void
2237 mark_stmt_tree (st)
2238      struct stmt_tree *st;
2239 {
2240   ggc_mark_tree (st->x_last_stmt);
2241   ggc_mark_tree (st->x_last_expr_type);
2242 }
2243
2244 /* Mark ARG (which is really a struct saved_scope **) for GC.  */
2245
2246 static void
2247 mark_saved_scope (arg)
2248      void *arg;
2249 {
2250   struct saved_scope *t = *(struct saved_scope **)arg;
2251   while (t)
2252     {
2253       mark_binding_level (&t->class_bindings);
2254       ggc_mark_tree (t->old_bindings);
2255       ggc_mark_tree (t->old_namespace);
2256       ggc_mark_tree (t->class_name);
2257       ggc_mark_tree (t->class_type);
2258       ggc_mark_tree (t->access_specifier);
2259       ggc_mark_tree (t->function_decl);
2260       if (t->lang_base)
2261         ggc_mark_tree_varray (t->lang_base);
2262       ggc_mark_tree (t->lang_name);
2263       ggc_mark_tree (t->x_function_parms);
2264       ggc_mark_tree (t->template_parms);
2265       ggc_mark_tree (t->x_previous_class_type);
2266       ggc_mark_tree (t->x_previous_class_values);
2267       ggc_mark_tree (t->x_saved_tree);
2268
2269       mark_stmt_tree (&t->x_stmt_tree);
2270       mark_binding_level (&t->bindings);
2271       t = t->prev;
2272     }
2273 }
2274
2275 static tree
2276 store_bindings (names, old_bindings)
2277      tree names, old_bindings;
2278 {
2279   tree t;
2280   for (t = names; t; t = TREE_CHAIN (t))
2281     {
2282       tree binding, t1, id;
2283
2284       if (TREE_CODE (t) == TREE_LIST)
2285         id = TREE_PURPOSE (t);
2286       else
2287         id = DECL_NAME (t);
2288
2289       if (!id 
2290           /* Note that we may have an IDENTIFIER_CLASS_VALUE even when
2291              we have no IDENTIFIER_BINDING if we have left the class
2292              scope, but cached the class-level declarations.  */
2293           || !(IDENTIFIER_BINDING (id) || IDENTIFIER_CLASS_VALUE (id)))
2294         continue;
2295
2296       for (t1 = old_bindings; t1; t1 = TREE_CHAIN (t1))
2297         if (TREE_VEC_ELT (t1, 0) == id)
2298           goto skip_it;
2299
2300       binding = make_tree_vec (4);
2301
2302       if (id)
2303         {
2304           my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 135);
2305           TREE_VEC_ELT (binding, 0) = id;
2306           TREE_VEC_ELT (binding, 1) = REAL_IDENTIFIER_TYPE_VALUE (id);
2307           TREE_VEC_ELT (binding, 2) = IDENTIFIER_BINDING (id);
2308           TREE_VEC_ELT (binding, 3) = IDENTIFIER_CLASS_VALUE (id);
2309           IDENTIFIER_BINDING (id) = NULL_TREE;
2310           IDENTIFIER_CLASS_VALUE (id) = NULL_TREE;
2311         }
2312       TREE_CHAIN (binding) = old_bindings;
2313       old_bindings = binding;
2314     skip_it:
2315       ;
2316     }
2317   return old_bindings;
2318 }
2319
2320 void
2321 maybe_push_to_top_level (pseudo)
2322      int pseudo;
2323 {
2324   struct saved_scope *s;
2325   struct binding_level *b;
2326   tree old_bindings;
2327   int need_pop;
2328
2329   s = (struct saved_scope *) xcalloc (1, sizeof (struct saved_scope));
2330
2331   b = scope_chain ? current_binding_level : 0;
2332
2333   /* If we're in the middle of some function, save our state.  */
2334   if (current_function)
2335     {
2336       need_pop = 1;
2337       push_function_context_to (NULL_TREE);
2338     }
2339   else
2340     need_pop = 0;
2341
2342   old_bindings = NULL_TREE;
2343   if (scope_chain && previous_class_type)
2344     old_bindings = store_bindings (previous_class_values, old_bindings);
2345
2346   /* Have to include global_binding_level, because class-level decls
2347      aren't listed anywhere useful.  */
2348   for (; b; b = b->level_chain)
2349     {
2350       tree t;
2351
2352       /* Template IDs are inserted into the global level. If they were
2353          inserted into namespace level, finish_file wouldn't find them
2354          when doing pending instantiations. Therefore, don't stop at
2355          namespace level, but continue until :: .  */
2356       if (b == global_binding_level || (pseudo && b->pseudo_global))
2357         break;
2358
2359       old_bindings = store_bindings (b->names, old_bindings);
2360       /* We also need to check class_shadowed to save class-level type
2361          bindings, since pushclass doesn't fill in b->names.  */
2362       if (b->parm_flag == 2)
2363         old_bindings = store_bindings (b->class_shadowed, old_bindings);
2364
2365       /* Unwind type-value slots back to top level.  */
2366       for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
2367         SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
2368     }
2369   s->prev = scope_chain;
2370   s->old_bindings = old_bindings;
2371   s->bindings = b;
2372   s->need_pop_function_context = need_pop;
2373   s->function_decl = current_function_decl;
2374
2375   scope_chain = s;
2376   current_function_decl = NULL_TREE;
2377   VARRAY_TREE_INIT (current_lang_base, 10, "current_lang_base");
2378   current_lang_stack = &VARRAY_TREE (current_lang_base, 0);
2379   current_lang_name = lang_name_cplusplus;
2380   strict_prototype = strict_prototypes_lang_cplusplus;
2381   current_namespace = global_namespace;
2382 }
2383
2384 void
2385 push_to_top_level ()
2386 {
2387   maybe_push_to_top_level (0);
2388 }
2389
2390 void
2391 pop_from_top_level ()
2392 {
2393   struct saved_scope *s = scope_chain;
2394   tree t;
2395
2396   /* Clear out class-level bindings cache.  */
2397   if (previous_class_type)
2398     invalidate_class_lookup_cache ();
2399
2400   VARRAY_FREE (current_lang_base);
2401
2402   scope_chain = s->prev;
2403   for (t = s->old_bindings; t; t = TREE_CHAIN (t))
2404     {
2405       tree id = TREE_VEC_ELT (t, 0);
2406       if (id)
2407         {
2408           SET_IDENTIFIER_TYPE_VALUE (id, TREE_VEC_ELT (t, 1));
2409           IDENTIFIER_BINDING (id) = TREE_VEC_ELT (t, 2);
2410           IDENTIFIER_CLASS_VALUE (id) = TREE_VEC_ELT (t, 3);
2411         }
2412     }
2413
2414   if (current_lang_name == lang_name_cplusplus)
2415     strict_prototype = strict_prototypes_lang_cplusplus;
2416   else if (current_lang_name == lang_name_c)
2417     strict_prototype = strict_prototypes_lang_c;
2418
2419   /* If we were in the middle of compiling a function, restore our
2420      state.  */
2421   if (s->need_pop_function_context)
2422     pop_function_context_from (NULL_TREE);
2423   current_function_decl = s->function_decl;
2424
2425   free (s);
2426 }
2427 \f
2428 /* Push a definition of struct, union or enum tag "name".
2429    into binding_level "b".   "type" should be the type node, 
2430    We assume that the tag "name" is not already defined.
2431
2432    Note that the definition may really be just a forward reference.
2433    In that case, the TYPE_SIZE will be a NULL_TREE.
2434
2435    C++ gratuitously puts all these tags in the name space.  */
2436
2437 /* When setting the IDENTIFIER_TYPE_VALUE field of an identifier ID,
2438    record the shadowed value for this binding contour.  TYPE is
2439    the type that ID maps to.  */
2440
2441 static void
2442 set_identifier_type_value_with_scope (id, type, b)
2443      tree id;
2444      tree type;
2445      struct binding_level *b;
2446 {
2447   if (!b->namespace_p)
2448     {
2449       /* Shadow the marker, not the real thing, so that the marker
2450          gets restored later. */
2451       tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
2452       b->type_shadowed
2453         = tree_cons (id, old_type_value, b->type_shadowed);
2454     }
2455   else
2456     {
2457       tree binding = binding_for_name (id, current_namespace);
2458       BINDING_TYPE (binding) = type;
2459       /* Store marker instead of real type. */
2460       type = global_type_node;
2461     }
2462   SET_IDENTIFIER_TYPE_VALUE (id, type);
2463 }
2464
2465 /* As set_identifier_type_value_with_scope, but using current_binding_level.  */
2466
2467 void
2468 set_identifier_type_value (id, type)
2469      tree id;
2470      tree type;
2471 {
2472   set_identifier_type_value_with_scope (id, type, current_binding_level);
2473 }
2474
2475 /* Return the type associated with id. */
2476
2477 tree
2478 identifier_type_value (id)
2479      tree id;
2480 {
2481   /* There is no type with that name, anywhere. */
2482   if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
2483     return NULL_TREE;
2484   /* This is not the type marker, but the real thing. */
2485   if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
2486     return REAL_IDENTIFIER_TYPE_VALUE (id);
2487   /* Have to search for it. It must be on the global level, now.
2488      Ask lookup_name not to return non-types. */
2489   id = lookup_name_real (id, 2, 1, 0);
2490   if (id)
2491     return TREE_TYPE (id);
2492   return NULL_TREE;
2493 }
2494
2495 /* Pop off extraneous binding levels left over due to syntax errors.
2496
2497    We don't pop past namespaces, as they might be valid.  */
2498
2499 void
2500 pop_everything ()
2501 {
2502 #ifdef DEBUG_CP_BINDING_LEVELS
2503   fprintf (stderr, "XXX entering pop_everything ()\n");
2504 #endif
2505   while (!toplevel_bindings_p ())
2506     {
2507       if (current_binding_level->parm_flag == 2)
2508         pop_nested_class ();
2509       else
2510         poplevel (0, 0, 0);
2511     }
2512 #ifdef DEBUG_CP_BINDING_LEVELS
2513   fprintf (stderr, "XXX leaving pop_everything ()\n");
2514 #endif
2515 }
2516
2517 /* The type TYPE is being declared.  If it is a class template, or a
2518    specialization of a class template, do any processing required and
2519    perform error-checking.  If IS_FRIEND is non-zero, this TYPE is
2520    being declared a friend.  B is the binding level at which this TYPE
2521    should be bound.
2522
2523    Returns the TYPE_DECL for TYPE, which may have been altered by this
2524    processing.  */
2525
2526 static tree 
2527 maybe_process_template_type_declaration (type, globalize, b)
2528      tree type;
2529      int globalize;
2530      struct binding_level* b;
2531 {
2532   tree decl = TYPE_NAME (type);
2533  
2534   if (processing_template_parmlist)
2535     /* You can't declare a new template type in a template parameter
2536        list.  But, you can declare a non-template type:
2537        
2538          template <class A*> struct S;
2539        
2540        is a forward-declaration of `A'.  */
2541     ;
2542   else 
2543     {
2544       maybe_check_template_type (type);
2545
2546       my_friendly_assert (IS_AGGR_TYPE (type) 
2547                           || TREE_CODE (type) == ENUMERAL_TYPE, 0);
2548                           
2549                           
2550       if (processing_template_decl)
2551         {
2552           /* This may change after the call to
2553              push_template_decl_real, but we want the original value.  */
2554           tree name = DECL_NAME (decl);
2555
2556           decl = push_template_decl_real (decl, globalize);
2557           /* If the current binding level is the binding level for the
2558              template parameters (see the comment in
2559              begin_template_parm_list) and the enclosing level is a class
2560              scope, and we're not looking at a friend, push the
2561              declaration of the member class into the class scope.  In the
2562              friend case, push_template_decl will already have put the
2563              friend into global scope, if appropriate.  */
2564           if (TREE_CODE (type) != ENUMERAL_TYPE
2565               && !globalize && b->pseudo_global
2566               && b->level_chain->parm_flag == 2)
2567             {
2568               finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
2569               /* Put this tag on the list of tags for the class, since
2570                  that won't happen below because B is not the class
2571                  binding level, but is instead the pseudo-global level.  */
2572               b->level_chain->tags = 
2573                 tree_cons (name, type, b->level_chain->tags);
2574               if (TYPE_SIZE (current_class_type) == NULL_TREE)
2575                 CLASSTYPE_TAGS (current_class_type) = b->level_chain->tags;
2576             }
2577         }
2578     }
2579
2580   return decl;
2581 }
2582
2583 /* In C++, you don't have to write `struct S' to refer to `S'; you
2584    can just use `S'.  We accomplish this by creating a TYPE_DECL as
2585    if the user had written `typedef struct S S'.  Create and return
2586    the TYPE_DECL for TYPE.  */
2587
2588 tree
2589 create_implicit_typedef (name, type)
2590      tree name;
2591      tree type;
2592 {
2593   tree decl;
2594
2595   decl = build_decl (TYPE_DECL, name, type);
2596   SET_DECL_ARTIFICIAL (decl);
2597   /* There are other implicit type declarations, like the one *within*
2598      a class that allows you to write `S::S'.  We must distinguish
2599      amongst these.  */
2600   SET_DECL_IMPLICIT_TYPEDEF_P (decl);
2601   TYPE_NAME (type) = decl;
2602
2603   return decl;
2604 }
2605
2606 /* Push a tag name NAME for struct/class/union/enum type TYPE.
2607    Normally put it into the inner-most non-tag-transparent scope,
2608    but if GLOBALIZE is true, put it in the inner-most non-class scope.
2609    The latter is needed for implicit declarations.  */
2610
2611 void
2612 pushtag (name, type, globalize)
2613      tree name, type;
2614      int globalize;
2615 {
2616   register struct binding_level *b;
2617
2618   b = current_binding_level;
2619   while (b->tag_transparent
2620          || (globalize && b->parm_flag == 2))
2621     b = b->level_chain;
2622
2623   b->tags = tree_cons (name, type, b->tags);
2624
2625   if (name)
2626     {
2627       /* Do C++ gratuitous typedefing.  */
2628       if (IDENTIFIER_TYPE_VALUE (name) != type)
2629         {
2630           register tree d = NULL_TREE;
2631           int in_class = 0;
2632           tree context = TYPE_CONTEXT (type);
2633
2634           if (! context)
2635             {
2636               tree cs = current_scope ();
2637
2638               if (! globalize)
2639                 context = cs;
2640               else if (cs != NULL_TREE 
2641                        && TREE_CODE_CLASS (TREE_CODE (cs)) == 't')
2642                 /* When declaring a friend class of a local class, we want
2643                    to inject the newly named class into the scope
2644                    containing the local class, not the namespace scope.  */
2645                 context = hack_decl_function_context (get_type_decl (cs));
2646             }
2647           if (!context)
2648             context = current_namespace;
2649
2650           if ((b->pseudo_global && b->level_chain->parm_flag == 2)
2651               || b->parm_flag == 2)
2652             in_class = 1;
2653
2654           if (current_lang_name == lang_name_java)
2655             TYPE_FOR_JAVA (type) = 1;
2656
2657           d = create_implicit_typedef (name, type);
2658           DECL_CONTEXT (d) = FROB_CONTEXT (context);
2659           if (! in_class)
2660             set_identifier_type_value_with_scope (name, type, b);
2661
2662           d = maybe_process_template_type_declaration (type,
2663                                                        globalize, b);
2664
2665           if (b->parm_flag == 2)
2666             {
2667               if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
2668                 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
2669                    class.  But if it's a member template class, we
2670                    want the TEMPLATE_DECL, not the TYPE_DECL, so this
2671                    is done later.  */
2672                 finish_member_declaration (d);
2673               else
2674                 pushdecl_class_level (d);
2675             }
2676           else
2677             d = pushdecl_with_scope (d, b);
2678
2679           if (ANON_AGGRNAME_P (name))
2680             DECL_IGNORED_P (d) = 1;
2681
2682           TYPE_CONTEXT (type) = DECL_CONTEXT (d);
2683           DECL_ASSEMBLER_NAME (d) = DECL_NAME (d);
2684           if (!uses_template_parms (type))
2685             DECL_ASSEMBLER_NAME (d)
2686               = get_identifier (build_overload_name (type, 1, 1));
2687         }
2688       if (b->parm_flag == 2)
2689         {
2690           if (TYPE_SIZE (current_class_type) == NULL_TREE)
2691             CLASSTYPE_TAGS (current_class_type) = b->tags;
2692         }
2693     }
2694
2695   if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL)
2696     /* Use the canonical TYPE_DECL for this node.  */
2697     TYPE_STUB_DECL (type) = TYPE_NAME (type);
2698   else
2699     {
2700       /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE
2701          will be the tagged type we just added to the current
2702          binding level.  This fake NULL-named TYPE_DECL node helps
2703          dwarfout.c to know when it needs to output a
2704          representation of a tagged type, and it also gives us a
2705          convenient place to record the "scope start" address for
2706          the tagged type.  */
2707
2708       tree d = build_decl (TYPE_DECL, NULL_TREE, type);
2709       TYPE_STUB_DECL (type) = pushdecl_with_scope (d, b);
2710     }
2711 }
2712
2713 /* Counter used to create anonymous type names.  */
2714
2715 static int anon_cnt = 0;
2716
2717 /* Return an IDENTIFIER which can be used as a name for
2718    anonymous structs and unions.  */
2719
2720 tree
2721 make_anon_name ()
2722 {
2723   char buf[32];
2724
2725   sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
2726   return get_identifier (buf);
2727 }
2728
2729 /* Clear the TREE_PURPOSE slot of tags which have anonymous typenames.
2730    This keeps dbxout from getting confused.  */
2731
2732 void
2733 clear_anon_tags ()
2734 {
2735   register struct binding_level *b;
2736   register tree tags;
2737   static int last_cnt = 0;
2738
2739   /* Fast out if no new anon names were declared.  */
2740   if (last_cnt == anon_cnt)
2741     return;
2742
2743   b = current_binding_level;
2744   while (b->tag_transparent)
2745     b = b->level_chain;
2746   tags = b->tags;
2747   while (tags)
2748     {
2749       /* A NULL purpose means we have already processed all tags
2750          from here to the end of the list.  */
2751       if (TREE_PURPOSE (tags) == NULL_TREE)
2752         break;
2753       if (ANON_AGGRNAME_P (TREE_PURPOSE (tags)))
2754         TREE_PURPOSE (tags) = NULL_TREE;
2755       tags = TREE_CHAIN (tags);
2756     }
2757   last_cnt = anon_cnt;
2758 }
2759 \f
2760 /* Subroutine of duplicate_decls: return truthvalue of whether
2761    or not types of these decls match.
2762
2763    For C++, we must compare the parameter list so that `int' can match
2764    `int&' in a parameter position, but `int&' is not confused with
2765    `const int&'.  */
2766
2767 int
2768 decls_match (newdecl, olddecl)
2769      tree newdecl, olddecl;
2770 {
2771   int types_match;
2772
2773   if (newdecl == olddecl)
2774     return 1;
2775
2776   if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
2777     /* If the two DECLs are not even the same kind of thing, we're not
2778        interested in their types.  */
2779     return 0;
2780
2781   if (TREE_CODE (newdecl) == FUNCTION_DECL)
2782     {
2783       tree f1 = TREE_TYPE (newdecl);
2784       tree f2 = TREE_TYPE (olddecl);
2785       tree p1 = TYPE_ARG_TYPES (f1);
2786       tree p2 = TYPE_ARG_TYPES (f2);
2787
2788       if (DECL_REAL_CONTEXT (newdecl) != DECL_REAL_CONTEXT (olddecl)
2789           && ! (DECL_LANGUAGE (newdecl) == lang_c
2790                 && DECL_LANGUAGE (olddecl) == lang_c))
2791         return 0;
2792
2793       /* When we parse a static member function definition,
2794          we put together a FUNCTION_DECL which thinks its type
2795          is METHOD_TYPE.  Change that to FUNCTION_TYPE, and
2796          proceed.  */
2797       if (TREE_CODE (f1) == METHOD_TYPE && DECL_STATIC_FUNCTION_P (olddecl))
2798         revert_static_member_fn (&newdecl, &f1, &p1);
2799       else if (TREE_CODE (f2) == METHOD_TYPE
2800                && DECL_STATIC_FUNCTION_P (newdecl))
2801         revert_static_member_fn (&olddecl, &f2, &p2);
2802
2803       /* Here we must take care of the case where new default
2804          parameters are specified.  Also, warn if an old
2805          declaration becomes ambiguous because default
2806          parameters may cause the two to be ambiguous.  */
2807       if (TREE_CODE (f1) != TREE_CODE (f2))
2808         {
2809           if (TREE_CODE (f1) == OFFSET_TYPE)
2810             cp_compiler_error ("`%D' redeclared as member function", newdecl);
2811           else
2812             cp_compiler_error ("`%D' redeclared as non-member function", newdecl);
2813           return 0;
2814         }
2815
2816       if (same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
2817         {
2818           if (! strict_prototypes_lang_c && DECL_LANGUAGE (olddecl) == lang_c
2819               && p2 == NULL_TREE)
2820             {
2821               types_match = self_promoting_args_p (p1);
2822               if (p1 == void_list_node)
2823                 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
2824             }
2825           else if (!strict_prototypes_lang_c && DECL_LANGUAGE (olddecl)==lang_c
2826                    && DECL_LANGUAGE (newdecl) == lang_c && p1 == NULL_TREE)
2827             {
2828               types_match = self_promoting_args_p (p2);
2829               TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
2830             }
2831           else
2832             types_match = compparms (p1, p2);
2833         }
2834       else
2835         types_match = 0;
2836     }
2837   else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2838     {
2839       if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
2840                                 DECL_TEMPLATE_PARMS (olddecl)))
2841         return 0;
2842       
2843       if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2844         types_match = 1;
2845       else
2846         types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
2847                                    DECL_TEMPLATE_RESULT (newdecl));
2848     }
2849   else
2850     {
2851       if (TREE_TYPE (newdecl) == error_mark_node)
2852         types_match = TREE_TYPE (olddecl) == error_mark_node;
2853       else if (TREE_TYPE (olddecl) == NULL_TREE)
2854         types_match = TREE_TYPE (newdecl) == NULL_TREE;
2855       else if (TREE_TYPE (newdecl) == NULL_TREE)
2856         types_match = 0;
2857       else
2858         types_match = comptypes (TREE_TYPE (newdecl),
2859                                  TREE_TYPE (olddecl),
2860                                  COMPARE_REDECLARATION);
2861     }
2862
2863   return types_match;
2864 }
2865
2866 /* If NEWDECL is `static' and an `extern' was seen previously,
2867    warn about it.  OLDDECL is the previous declaration.
2868
2869    Note that this does not apply to the C++ case of declaring
2870    a variable `extern const' and then later `const'.
2871
2872    Don't complain about built-in functions, since they are beyond
2873    the user's control.  */
2874
2875 static void
2876 warn_extern_redeclared_static (newdecl, olddecl)
2877      tree newdecl, olddecl;
2878 {
2879   static const char *explicit_extern_static_warning
2880     = "`%D' was declared `extern' and later `static'";
2881   static const char *implicit_extern_static_warning
2882     = "`%D' was declared implicitly `extern' and later `static'";
2883
2884   tree name;
2885
2886   if (TREE_CODE (newdecl) == TYPE_DECL 
2887       || TREE_CODE (newdecl) == TEMPLATE_DECL)
2888     return;
2889   
2890   /* Don't get confused by static member functions; that's a different
2891      use of `static'.  */
2892   if (TREE_CODE (newdecl) == FUNCTION_DECL
2893       && DECL_STATIC_FUNCTION_P (newdecl))
2894     return;
2895
2896   /* If the old declaration was `static', or the new one isn't, then
2897      then everything is OK.  */
2898   if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
2899     return;
2900
2901   /* It's OK to declare a builtin function as `static'.  */
2902   if (TREE_CODE (olddecl) == FUNCTION_DECL
2903       && DECL_ARTIFICIAL (olddecl))
2904     return;
2905
2906   name = DECL_ASSEMBLER_NAME (newdecl);
2907   cp_pedwarn (IDENTIFIER_IMPLICIT_DECL (name)
2908               ? implicit_extern_static_warning
2909               : explicit_extern_static_warning, newdecl);
2910   cp_pedwarn_at ("previous declaration of `%D'", olddecl);
2911 }
2912
2913 /* Handle when a new declaration NEWDECL has the same name as an old
2914    one OLDDECL in the same binding contour.  Prints an error message
2915    if appropriate.
2916
2917    If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
2918    Otherwise, return 0.  */
2919
2920 int
2921 duplicate_decls (newdecl, olddecl)
2922      tree newdecl, olddecl;
2923 {
2924   unsigned olddecl_uid = DECL_UID (olddecl);
2925   int olddecl_friend = 0, types_match = 0;
2926   int new_defines_function = 0;
2927
2928   if (newdecl == olddecl)
2929     return 1;
2930
2931   types_match = decls_match (newdecl, olddecl);
2932
2933   /* If either the type of the new decl or the type of the old decl is an
2934      error_mark_node, then that implies that we have already issued an
2935      error (earlier) for some bogus type specification, and in that case,
2936      it is rather pointless to harass the user with yet more error message
2937      about the same declaration, so just pretend the types match here.  */
2938   if (TREE_TYPE (newdecl) == error_mark_node
2939       || TREE_TYPE (olddecl) == error_mark_node)
2940     types_match = 1;
2941  
2942   /* Check for redeclaration and other discrepancies. */
2943   if (TREE_CODE (olddecl) == FUNCTION_DECL
2944       && DECL_ARTIFICIAL (olddecl))
2945     {
2946       if (TREE_CODE (newdecl) != FUNCTION_DECL)
2947         {
2948           /* If you declare a built-in or predefined function name as static,
2949              the old definition is overridden, but optionally warn this was a
2950              bad choice of name.  */
2951           if (! TREE_PUBLIC (newdecl))
2952             {
2953               if (warn_shadow)
2954                 cp_warning ("shadowing %s function `%#D'",
2955                             DECL_BUILT_IN (olddecl) ? "built-in" : "library",
2956                             olddecl);
2957               /* Discard the old built-in function.  */
2958               return 0;
2959             }
2960           /* If the built-in is not ansi, then programs can override
2961              it even globally without an error.  */
2962           else if (! DECL_BUILT_IN (olddecl))
2963             cp_warning ("library function `%#D' redeclared as non-function `%#D'",
2964                         olddecl, newdecl);
2965           else
2966             {
2967               cp_error ("declaration of `%#D'", newdecl);
2968               cp_error ("conflicts with built-in declaration `%#D'",
2969                         olddecl);
2970             }
2971           return 0;
2972         }
2973       else if (!types_match)
2974         {
2975           if ((DECL_LANGUAGE (newdecl) == lang_c
2976                && DECL_LANGUAGE (olddecl) == lang_c)
2977               || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
2978                             TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
2979             {
2980               /* A near match; override the builtin.  */
2981
2982               if (TREE_PUBLIC (newdecl))
2983                 {
2984                   cp_warning ("new declaration `%#D'", newdecl);
2985                   cp_warning ("ambiguates built-in declaration `%#D'",
2986                               olddecl);
2987                 }
2988               else if (warn_shadow)
2989                 cp_warning ("shadowing %s function `%#D'",
2990                             DECL_BUILT_IN (olddecl) ? "built-in" : "library",
2991                             olddecl);
2992             }
2993           else
2994             /* Discard the old built-in function.  */
2995             return 0;
2996         }
2997       
2998       if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
2999         {
3000           /* If a builtin function is redeclared as `static', merge
3001              the declarations, but make the original one static.  */
3002           DECL_THIS_STATIC (olddecl) = 1;
3003           TREE_PUBLIC (olddecl) = 0;
3004
3005           /* Make the olddeclaration consistent with the new one so that
3006              all remnants of the builtin-ness of this function will be
3007              banished.  */
3008           DECL_LANGUAGE (olddecl) = DECL_LANGUAGE (newdecl);
3009           DECL_RTL (olddecl) = DECL_RTL (newdecl);
3010           DECL_ASSEMBLER_NAME (olddecl) = DECL_ASSEMBLER_NAME (newdecl);
3011           SET_IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (newdecl),
3012                                        newdecl);
3013         }
3014     }
3015   else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
3016     {
3017       if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
3018            && TREE_CODE (newdecl) != TYPE_DECL
3019            && ! (TREE_CODE (newdecl) == TEMPLATE_DECL
3020                  && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL))
3021           || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
3022               && TREE_CODE (olddecl) != TYPE_DECL
3023               && ! (TREE_CODE (olddecl) == TEMPLATE_DECL
3024                     && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
3025                         == TYPE_DECL))))
3026         {
3027           /* We do nothing special here, because C++ does such nasty
3028              things with TYPE_DECLs.  Instead, just let the TYPE_DECL
3029              get shadowed, and know that if we need to find a TYPE_DECL
3030              for a given name, we can look in the IDENTIFIER_TYPE_VALUE
3031              slot of the identifier.  */
3032           return 0;
3033         }
3034
3035       if ((TREE_CODE (newdecl) == FUNCTION_DECL
3036            && DECL_FUNCTION_TEMPLATE_P (olddecl))
3037           || (TREE_CODE (olddecl) == FUNCTION_DECL
3038               && DECL_FUNCTION_TEMPLATE_P (newdecl)))
3039         return 0;
3040
3041       cp_error ("`%#D' redeclared as different kind of symbol", newdecl);
3042       if (TREE_CODE (olddecl) == TREE_LIST)
3043         olddecl = TREE_VALUE (olddecl);
3044       cp_error_at ("previous declaration of `%#D'", olddecl);
3045
3046       /* New decl is completely inconsistent with the old one =>
3047          tell caller to replace the old one.  */
3048
3049       return 0;
3050     }
3051   else if (!types_match)
3052     {
3053       if (DECL_REAL_CONTEXT (newdecl) != DECL_REAL_CONTEXT (olddecl))
3054         /* These are certainly not duplicate declarations; they're
3055            from different scopes.  */
3056         return 0;
3057
3058       if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3059         {
3060           /* The name of a class template may not be declared to refer to
3061              any other template, class, function, object, namespace, value,
3062              or type in the same scope.  */
3063           if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
3064               || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
3065             {
3066               cp_error ("declaration of template `%#D'", newdecl);
3067               cp_error_at ("conflicts with previous declaration `%#D'",
3068                            olddecl);
3069             }
3070           else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
3071                    && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
3072                    && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
3073                                  TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
3074                    && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
3075                                            DECL_TEMPLATE_PARMS (olddecl)))
3076             {
3077               cp_error ("new declaration `%#D'", newdecl);
3078               cp_error_at ("ambiguates old declaration `%#D'", olddecl);
3079             }
3080           return 0;
3081         }
3082       if (TREE_CODE (newdecl) == FUNCTION_DECL)
3083         {
3084           if (DECL_LANGUAGE (newdecl) == lang_c
3085               && DECL_LANGUAGE (olddecl) == lang_c)
3086             {
3087               cp_error ("declaration of C function `%#D' conflicts with",
3088                         newdecl);
3089               cp_error_at ("previous declaration `%#D' here", olddecl);
3090             }
3091           else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
3092                               TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
3093             {
3094               cp_error ("new declaration `%#D'", newdecl);
3095               cp_error_at ("ambiguates old declaration `%#D'", olddecl);
3096             }
3097           else
3098             return 0;
3099         }
3100
3101       /* Already complained about this, so don't do so again.  */
3102       else if (current_class_type == NULL_TREE
3103           || IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (newdecl)) != current_class_type)
3104         {
3105           cp_error ("conflicting types for `%#D'", newdecl);
3106           cp_error_at ("previous declaration as `%#D'", olddecl);
3107         }
3108     }
3109   else if (TREE_CODE (newdecl) == FUNCTION_DECL 
3110             && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
3111                  && (!DECL_TEMPLATE_INFO (newdecl)
3112                      || (DECL_TI_TEMPLATE (newdecl) 
3113                          != DECL_TI_TEMPLATE (olddecl))))
3114                 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
3115                     && (!DECL_TEMPLATE_INFO (olddecl)
3116                         || (DECL_TI_TEMPLATE (olddecl) 
3117                             != DECL_TI_TEMPLATE (newdecl))))))
3118     /* It's OK to have a template specialization and a non-template
3119        with the same type, or to have specializations of two
3120        different templates with the same type.  Note that if one is a
3121        specialization, and the other is an instantiation of the same
3122        template, that we do not exit at this point.  That situation
3123        can occur if we instantiate a template class, and then
3124        specialize one of its methods.  This situation is legal, but
3125        the declarations must be merged in the usual way.  */
3126     return 0;
3127   else if (TREE_CODE (newdecl) == FUNCTION_DECL 
3128            && ((DECL_TEMPLATE_INSTANTIATION (olddecl) 
3129                 && !DECL_USE_TEMPLATE (newdecl))
3130                || (DECL_TEMPLATE_INSTANTIATION (newdecl)
3131                    && !DECL_USE_TEMPLATE (olddecl))))
3132     /* One of the declarations is a template instantiation, and the
3133        other is not a template at all.  That's OK.  */
3134     return 0;
3135   else if (TREE_CODE (newdecl) == NAMESPACE_DECL
3136            && DECL_NAMESPACE_ALIAS (newdecl)
3137            && DECL_NAMESPACE_ALIAS (newdecl) == DECL_NAMESPACE_ALIAS (olddecl))
3138     /* Redeclaration of namespace alias, ignore it. */
3139     return 1;
3140   else
3141     {
3142       const char *errmsg = redeclaration_error_message (newdecl, olddecl);
3143       if (errmsg)
3144         {
3145           cp_error (errmsg, newdecl);
3146           if (DECL_NAME (olddecl) != NULL_TREE)
3147             cp_error_at ((DECL_INITIAL (olddecl)
3148                           && namespace_bindings_p ())
3149                          ? "`%#D' previously defined here"
3150                          : "`%#D' previously declared here", olddecl);
3151         }
3152       else if (TREE_CODE (olddecl) == FUNCTION_DECL
3153                && DECL_INITIAL (olddecl) != NULL_TREE
3154                && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE
3155                && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE)
3156         {
3157           /* Prototype decl follows defn w/o prototype.  */
3158           cp_warning_at ("prototype for `%#D'", newdecl);
3159           cp_warning_at ("follows non-prototype definition here", olddecl);
3160         }
3161       else if (TREE_CODE (olddecl) == FUNCTION_DECL
3162                && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
3163         {
3164           /* extern "C" int foo ();
3165              int foo () { bar (); }
3166              is OK.  */
3167           if (current_lang_stack
3168               == &VARRAY_TREE (current_lang_base, 0))
3169             DECL_LANGUAGE (newdecl) = DECL_LANGUAGE (olddecl);
3170           else
3171             {
3172               cp_error_at ("previous declaration of `%#D' with %L linkage",
3173                            olddecl, DECL_LANGUAGE (olddecl));
3174               cp_error ("conflicts with new declaration with %L linkage",
3175                         DECL_LANGUAGE (newdecl));
3176             }
3177         }
3178
3179       if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
3180         ;
3181       else if (TREE_CODE (olddecl) == FUNCTION_DECL)
3182         {
3183           tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
3184           tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
3185           int i = 1;
3186
3187           if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE)
3188             t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2);
3189         
3190           for (; t1 && t1 != void_list_node;
3191                t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
3192             if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
3193               {
3194                 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
3195                                            TREE_PURPOSE (t2)))
3196                   {
3197                     if (pedantic)
3198                       {
3199                         cp_pedwarn ("default argument given for parameter %d of `%#D'",
3200                                     i, newdecl);
3201                         cp_pedwarn_at ("after previous specification in `%#D'",
3202                                        olddecl);
3203                       }
3204                   }
3205                 else
3206                   {
3207                     cp_error ("default argument given for parameter %d of `%#D'",
3208                               i, newdecl);
3209                     cp_error_at ("after previous specification in `%#D'",
3210                                  olddecl);
3211                   }
3212               }
3213
3214           if (DECL_THIS_INLINE (newdecl) && ! DECL_THIS_INLINE (olddecl)
3215               && TREE_ADDRESSABLE (olddecl) && warn_inline)
3216             {
3217               cp_warning ("`%#D' was used before it was declared inline",
3218                           newdecl);
3219               cp_warning_at ("previous non-inline declaration here",
3220                              olddecl);
3221             }
3222         }
3223     }
3224
3225   /* If new decl is `static' and an `extern' was seen previously,
3226      warn about it.  */
3227   warn_extern_redeclared_static (newdecl, olddecl);
3228
3229   /* We have committed to returning 1 at this point.  */
3230   if (TREE_CODE (newdecl) == FUNCTION_DECL)
3231     {
3232       /* Now that functions must hold information normally held
3233          by field decls, there is extra work to do so that
3234          declaration information does not get destroyed during
3235          definition.  */
3236       if (DECL_VINDEX (olddecl))
3237         DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
3238       if (DECL_CONTEXT (olddecl))
3239         DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
3240       if (DECL_CLASS_CONTEXT (olddecl))
3241         DECL_CLASS_CONTEXT (newdecl) = DECL_CLASS_CONTEXT (olddecl);
3242       if (DECL_PENDING_INLINE_INFO (newdecl) == (struct pending_inline *)0)
3243         DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
3244       DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
3245       DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
3246       DECL_ABSTRACT_VIRTUAL_P (newdecl) |= DECL_ABSTRACT_VIRTUAL_P (olddecl);
3247       DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
3248       DECL_NEEDS_FINAL_OVERRIDER_P (newdecl) |= DECL_NEEDS_FINAL_OVERRIDER_P (olddecl);
3249       DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
3250       new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
3251       
3252       /* Optionally warn about more than one declaration for the same
3253          name, but don't warn about a function declaration followed by a
3254          definition.  */
3255       if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
3256           && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
3257           /* Don't warn about extern decl followed by definition. */
3258           && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
3259           /* Don't warn about friends, let add_friend take care of it. */
3260           && ! DECL_FRIEND_P (newdecl))
3261         {
3262           cp_warning ("redundant redeclaration of `%D' in same scope", newdecl);
3263           cp_warning_at ("previous declaration of `%D'", olddecl);
3264         }
3265     }
3266
3267   /* Deal with C++: must preserve virtual function table size.  */
3268   if (TREE_CODE (olddecl) == TYPE_DECL)
3269     {
3270       register tree newtype = TREE_TYPE (newdecl);
3271       register tree oldtype = TREE_TYPE (olddecl);
3272
3273       if (newtype != error_mark_node && oldtype != error_mark_node
3274           && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
3275         {
3276           CLASSTYPE_VSIZE (newtype) = CLASSTYPE_VSIZE (oldtype);
3277           CLASSTYPE_FRIEND_CLASSES (newtype)
3278             = CLASSTYPE_FRIEND_CLASSES (oldtype);
3279         }
3280     }
3281
3282   /* Copy all the DECL_... slots specified in the new decl
3283      except for any that we copy here from the old type.  */
3284   DECL_MACHINE_ATTRIBUTES (newdecl) 
3285     = merge_machine_decl_attributes (olddecl, newdecl);
3286
3287   if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3288     {
3289       if (! duplicate_decls (DECL_TEMPLATE_RESULT (newdecl),
3290                              DECL_TEMPLATE_RESULT (olddecl)))
3291         cp_error ("invalid redeclaration of %D", newdecl);
3292       TREE_TYPE (olddecl) = TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl));
3293       DECL_TEMPLATE_SPECIALIZATIONS (olddecl) 
3294         = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
3295                    DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
3296  
3297       return 1;
3298     }
3299     
3300   if (types_match)
3301     {
3302       /* Automatically handles default parameters.  */
3303       tree oldtype = TREE_TYPE (olddecl);
3304       tree newtype;
3305
3306       /* Merge the data types specified in the two decls.  */
3307       newtype = common_type (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
3308
3309       if (TREE_CODE (newdecl) == VAR_DECL)
3310         DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
3311       /* Do this after calling `common_type' so that default
3312          parameters don't confuse us.  */
3313       else if (TREE_CODE (newdecl) == FUNCTION_DECL
3314           && (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl))
3315               != TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl))))
3316         {
3317           TREE_TYPE (newdecl) = build_exception_variant (newtype,
3318                                                          TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)));
3319           TREE_TYPE (olddecl) = build_exception_variant (newtype,
3320                                                          TYPE_RAISES_EXCEPTIONS (oldtype));
3321
3322           if ((pedantic || ! DECL_IN_SYSTEM_HEADER (olddecl))
3323               && DECL_SOURCE_LINE (olddecl) != 0
3324               && flag_exceptions
3325               && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)),
3326                                      TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl)), 1))
3327             {
3328               cp_error ("declaration of `%F' throws different exceptions",
3329                         newdecl);
3330               cp_error_at ("than previous declaration `%F'", olddecl);
3331             }
3332         }
3333       TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
3334
3335       /* Lay the type out, unless already done.  */
3336       if (! same_type_p (newtype, oldtype)
3337           && TREE_TYPE (newdecl) != error_mark_node
3338           && !(processing_template_decl && uses_template_parms (newdecl)))
3339         layout_type (TREE_TYPE (newdecl));
3340
3341       if ((TREE_CODE (newdecl) == VAR_DECL
3342            || TREE_CODE (newdecl) == PARM_DECL
3343            || TREE_CODE (newdecl) == RESULT_DECL
3344            || TREE_CODE (newdecl) == FIELD_DECL
3345            || TREE_CODE (newdecl) == TYPE_DECL)
3346           && !(processing_template_decl && uses_template_parms (newdecl)))
3347         layout_decl (newdecl, 0);
3348
3349       /* Merge the type qualifiers.  */
3350       if (TREE_READONLY (newdecl))
3351         TREE_READONLY (olddecl) = 1;
3352       if (TREE_THIS_VOLATILE (newdecl))
3353         TREE_THIS_VOLATILE (olddecl) = 1;
3354
3355       /* Merge the initialization information.  */
3356       if (DECL_INITIAL (newdecl) == NULL_TREE
3357           && DECL_INITIAL (olddecl) != NULL_TREE)
3358         {
3359           DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
3360           DECL_SOURCE_FILE (newdecl) = DECL_SOURCE_FILE (olddecl);
3361           DECL_SOURCE_LINE (newdecl) = DECL_SOURCE_LINE (olddecl);
3362           if (CAN_HAVE_FULL_LANG_DECL_P (newdecl)
3363               && DECL_LANG_SPECIFIC (newdecl)
3364               && DECL_LANG_SPECIFIC (olddecl))
3365             DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
3366         }
3367
3368       /* Merge the section attribute.
3369          We want to issue an error if the sections conflict but that must be
3370          done later in decl_attributes since we are called before attributes
3371          are assigned.  */
3372       if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
3373         DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
3374
3375       /* Keep the old rtl since we can safely use it.  */
3376       DECL_RTL (newdecl) = DECL_RTL (olddecl);
3377
3378       if (TREE_CODE (newdecl) == FUNCTION_DECL)
3379         {
3380           DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
3381             |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
3382           DECL_NO_CHECK_MEMORY_USAGE (newdecl)
3383             |= DECL_NO_CHECK_MEMORY_USAGE (olddecl);
3384           DECL_NO_LIMIT_STACK (newdecl)
3385             |= DECL_NO_LIMIT_STACK (olddecl);
3386         }
3387     }
3388   /* If cannot merge, then use the new type and qualifiers,
3389      and don't preserve the old rtl.  */
3390   else
3391     {
3392       /* Clean out any memory we had of the old declaration.  */
3393       tree oldstatic = value_member (olddecl, static_aggregates);
3394       if (oldstatic)
3395         TREE_VALUE (oldstatic) = error_mark_node;
3396
3397       TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
3398       TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
3399       TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
3400       TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
3401     }
3402
3403   /* Merge the storage class information.  */
3404   DECL_WEAK (newdecl) |= DECL_WEAK (olddecl);
3405   DECL_ONE_ONLY (newdecl) |= DECL_ONE_ONLY (olddecl);
3406   DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
3407   TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
3408   TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
3409   if (! DECL_EXTERNAL (olddecl))
3410     DECL_EXTERNAL (newdecl) = 0;
3411   
3412   if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
3413     {
3414       DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
3415       DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
3416       DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
3417       DECL_TEMPLATE_INSTANTIATED (newdecl) 
3418         |= DECL_TEMPLATE_INSTANTIATED (olddecl);
3419       /* Don't really know how much of the language-specific
3420          values we should copy from old to new.  */
3421       DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
3422       DECL_ACCESS (newdecl) = DECL_ACCESS (olddecl);
3423       DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
3424       DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
3425       olddecl_friend = DECL_FRIEND_P (olddecl);
3426
3427       /* Only functions have DECL_BEFRIENDING_CLASSES.  */
3428       if (TREE_CODE (newdecl) == FUNCTION_DECL
3429           || DECL_FUNCTION_TEMPLATE_P (newdecl))
3430         DECL_BEFRIENDING_CLASSES (newdecl)
3431           = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
3432                      DECL_BEFRIENDING_CLASSES (olddecl));
3433     }
3434
3435   if (TREE_CODE (newdecl) == FUNCTION_DECL)
3436     {
3437       if (DECL_TEMPLATE_INSTANTIATION (olddecl) 
3438           && !DECL_TEMPLATE_INSTANTIATION (newdecl)) 
3439         {
3440           /* If newdecl is not a specialization, then it is not a
3441              template-related function at all.  And that means that we
3442              shoud have exited above, returning 0.  */
3443           my_friendly_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl),
3444                               0);
3445
3446           if (TREE_USED (olddecl)) 
3447             /* From [temp.expl.spec]:
3448                
3449                If a template, a member template or the member of a class
3450                template is explicitly specialized then that
3451                specialization shall be declared before the first use of
3452                that specialization that would cause an implicit
3453                instantiation to take place, in every translation unit in
3454                which such a use occurs.  */
3455             cp_error ("explicit specialization of %D after first use", 
3456                       olddecl);
3457
3458           SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
3459         }
3460       DECL_THIS_INLINE (newdecl) |= DECL_THIS_INLINE (olddecl);
3461
3462       /* If either decl says `inline', this fn is inline, unless its
3463          definition was passed already.  */
3464       if (DECL_INLINE (newdecl) && DECL_INITIAL (olddecl) == NULL_TREE)
3465         DECL_INLINE (olddecl) = 1;
3466       DECL_INLINE (newdecl) = DECL_INLINE (olddecl);
3467
3468       if (! types_match)
3469         {
3470           DECL_LANGUAGE (olddecl) = DECL_LANGUAGE (newdecl);
3471           DECL_ASSEMBLER_NAME (olddecl) = DECL_ASSEMBLER_NAME (newdecl);
3472           DECL_RTL (olddecl) = DECL_RTL (newdecl);
3473         }
3474       if (! types_match || new_defines_function)
3475         {
3476           /* These need to be copied so that the names are available.
3477              Note that if the types do match, we'll preserve inline
3478              info and other bits, but if not, we won't.  */
3479           DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
3480           DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
3481         }
3482       if (new_defines_function)
3483         /* If defining a function declared with other language
3484            linkage, use the previously declared language linkage.  */
3485         DECL_LANGUAGE (newdecl) = DECL_LANGUAGE (olddecl);
3486       else if (types_match)
3487         {
3488           /* If redeclaring a builtin function, and not a definition,
3489              it stays built in.  */
3490           if (DECL_BUILT_IN (olddecl))
3491             {
3492               DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
3493               DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
3494               /* If we're keeping the built-in definition, keep the rtl,
3495                  regardless of declaration matches.  */
3496               DECL_RTL (newdecl) = DECL_RTL (olddecl);
3497             }
3498           else
3499             DECL_FRAME_SIZE (newdecl) = DECL_FRAME_SIZE (olddecl);
3500
3501           DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
3502           if ((DECL_SAVED_INSNS (newdecl) = DECL_SAVED_INSNS (olddecl)))
3503             /* Previously saved insns go together with
3504                the function's previous definition.  */
3505             DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
3506           /* Don't clear out the arguments if we're redefining a function.  */
3507           if (DECL_ARGUMENTS (olddecl))
3508             DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
3509         }
3510       if (DECL_LANG_SPECIFIC (olddecl))
3511         DECL_MAIN_VARIANT (newdecl) = DECL_MAIN_VARIANT (olddecl);
3512     }
3513
3514   if (TREE_CODE (newdecl) == NAMESPACE_DECL)
3515     {
3516       NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
3517     }
3518
3519   /* Now preserve various other info from the definition.  */
3520   TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
3521   TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
3522   DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
3523   DECL_ASSEMBLER_NAME (newdecl) = DECL_ASSEMBLER_NAME (olddecl);
3524
3525   if (TREE_CODE (newdecl) == FUNCTION_DECL)
3526     {
3527       int function_size;
3528
3529       function_size = sizeof (struct tree_decl);
3530
3531       bcopy ((char *) newdecl + sizeof (struct tree_common),
3532              (char *) olddecl + sizeof (struct tree_common),
3533              function_size - sizeof (struct tree_common));
3534
3535       if (DECL_TEMPLATE_INSTANTIATION (newdecl))
3536         {
3537           /* If newdecl is a template instantiation, it is possible that
3538              the following sequence of events has occurred:
3539
3540              o A friend function was declared in a class template.  The
3541              class template was instantiated.  
3542
3543              o The instantiation of the friend declaration was 
3544              recorded on the instantiation list, and is newdecl.  
3545
3546              o Later, however, instantiate_class_template called pushdecl
3547              on the newdecl to perform name injection.  But, pushdecl in
3548              turn called duplicate_decls when it discovered that another
3549              declaration of a global function with the same name already
3550              existed. 
3551
3552              o Here, in duplicate_decls, we decided to clobber newdecl.
3553
3554              If we're going to do that, we'd better make sure that
3555              olddecl, and not newdecl, is on the list of
3556              instantiations so that if we try to do the instantiation
3557              again we won't get the clobbered declaration.  */
3558
3559           tree tmpl = DECL_TI_TEMPLATE (newdecl); 
3560           tree decls = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); 
3561
3562           for (; decls; decls = TREE_CHAIN (decls))
3563             if (TREE_VALUE (decls) == newdecl)
3564               TREE_VALUE (decls) = olddecl;
3565         }
3566     }
3567   else
3568     {
3569       bcopy ((char *) newdecl + sizeof (struct tree_common),
3570              (char *) olddecl + sizeof (struct tree_common),
3571              sizeof (struct tree_decl) - sizeof (struct tree_common)
3572              + tree_code_length [(int)TREE_CODE (newdecl)] * sizeof (char *));
3573     }
3574
3575   DECL_UID (olddecl) = olddecl_uid;
3576   if (olddecl_friend)
3577     DECL_FRIEND_P (olddecl) = 1;
3578
3579   /* NEWDECL contains the merged attribute lists.
3580      Update OLDDECL to be the same.  */
3581   DECL_MACHINE_ATTRIBUTES (olddecl) = DECL_MACHINE_ATTRIBUTES (newdecl);
3582
3583   return 1;
3584 }
3585
3586 /* Record a decl-node X as belonging to the current lexical scope.
3587    Check for errors (such as an incompatible declaration for the same
3588    name already seen in the same scope).
3589
3590    Returns either X or an old decl for the same name.
3591    If an old decl is returned, it may have been smashed
3592    to agree with what X says.  */
3593
3594 tree
3595 pushdecl (x)
3596      tree x;
3597 {
3598   register tree t;
3599   register tree name;
3600   int need_new_binding;
3601
3602   /* We shouldn't be calling pushdecl when we're generating RTL for a
3603      function that we already did semantic analysis on previously.  */
3604   my_friendly_assert (!current_function || doing_semantic_analysis_p (),
3605                       19990913);
3606
3607   name = DECL_ASSEMBLER_NAME (x);
3608   need_new_binding = 1;
3609
3610   if (DECL_TEMPLATE_PARM_P (x))
3611     /* Template parameters have no context; they are not X::T even
3612        when declared within a class or namespace.  */
3613     ;
3614   else
3615     {
3616       if (current_function_decl && x != current_function_decl
3617           /* A local declaration for a function doesn't constitute
3618              nesting.  */
3619           && !(TREE_CODE (x) == FUNCTION_DECL && !DECL_INITIAL (x))
3620           /* A local declaration for an `extern' variable is in the
3621              scoped of the current namespace, not the current
3622              function.  */
3623           && !(TREE_CODE (x) == VAR_DECL && DECL_EXTERNAL (x))
3624           /* Don't change DECL_CONTEXT of virtual methods.  */
3625           && (TREE_CODE (x) != FUNCTION_DECL || !DECL_VIRTUAL_P (x))
3626           && !DECL_CONTEXT (x))
3627         DECL_CONTEXT (x) = current_function_decl;
3628       if (!DECL_CONTEXT (x))
3629         DECL_CONTEXT (x) = FROB_CONTEXT (current_namespace);
3630
3631       /* If this is the declaration for a namespace-scope function,
3632          but the declaration itself is in a local scope, mark the
3633          declaration.  */
3634       if (TREE_CODE (x) == FUNCTION_DECL 
3635           && DECL_NAMESPACE_SCOPE_P (x)
3636           && current_function_decl
3637           && x != current_function_decl)
3638         DECL_LOCAL_FUNCTION_P (x) = 1;
3639     }
3640
3641   /* Type are looked up using the DECL_NAME, as that is what the rest of the
3642      compiler wants to use.  */
3643   if (TREE_CODE (x) == TYPE_DECL || TREE_CODE (x) == VAR_DECL
3644       || TREE_CODE (x) == NAMESPACE_DECL)
3645     name = DECL_NAME (x);
3646
3647   if (name)
3648     {
3649 #if 0
3650       /* Not needed...see below.  */
3651       char *file;
3652       int line;
3653 #endif
3654       if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3655         name = TREE_OPERAND (name, 0);
3656       
3657       /* Namespace-scoped variables are not found in the current level. */
3658       if (TREE_CODE (x) == VAR_DECL && DECL_NAMESPACE_SCOPE_P (x))
3659         t = namespace_binding (name, DECL_CONTEXT (x));
3660       else
3661         t = lookup_name_current_level (name);
3662       if (t == error_mark_node)
3663         {
3664           /* error_mark_node is 0 for a while during initialization!  */
3665           t = NULL_TREE;
3666           cp_error_at ("`%#D' used prior to declaration", x);
3667         }
3668
3669       else if (t != NULL_TREE)
3670         {
3671 #if 0
3672           /* This is turned off until I have time to do it right (bpk).  */
3673           /* With the code below that uses it...  */
3674           file = DECL_SOURCE_FILE (t);
3675           line = DECL_SOURCE_LINE (t);
3676 #endif
3677           if (TREE_CODE (t) == PARM_DECL)
3678             {
3679               if (DECL_CONTEXT (t) == NULL_TREE)
3680                 fatal ("parse errors have confused me too much");
3681
3682               /* Check for duplicate params.  */
3683               if (duplicate_decls (x, t))
3684                 return t;
3685             }
3686           else if (((TREE_CODE (x) == FUNCTION_DECL && DECL_LANGUAGE (x) == lang_c)
3687                     || DECL_FUNCTION_TEMPLATE_P (x))
3688                    && is_overloaded_fn (t))
3689             /* Don't do anything just yet. */;
3690           else if (t == wchar_decl_node)
3691             {
3692               if (pedantic && ! DECL_IN_SYSTEM_HEADER (x))
3693                 cp_pedwarn ("redeclaration of wchar_t as `%T'", TREE_TYPE (x));
3694
3695               /* Throw away the redeclaration.  */
3696               return t;
3697             }
3698           else if (TREE_CODE (t) != TREE_CODE (x))
3699             {
3700               if (duplicate_decls (x, t))
3701                 return t;
3702             }
3703           else if (duplicate_decls (x, t))
3704             {
3705 #if 0
3706               /* This is turned off until I have time to do it right (bpk).  */
3707
3708               /* Also warn if they did a prototype with `static' on it, but
3709                  then later left the `static' off.  */
3710               if (! TREE_PUBLIC (name) && TREE_PUBLIC (x))
3711                 {
3712                   if (DECL_LANG_SPECIFIC (t) && DECL_FRIEND_P (t))
3713                     return t;
3714
3715                   if (extra_warnings)
3716                     {
3717                       cp_warning ("`static' missing from declaration of `%D'",
3718                                   t);
3719                       warning_with_file_and_line (file, line,
3720                                                   "previous declaration of `%s'",
3721                                                   decl_as_string (t, 0));
3722                     }
3723
3724                   /* Now fix things so it'll do what they expect.  */
3725                   if (current_function_decl)
3726                     TREE_PUBLIC (current_function_decl) = 0;
3727                 }
3728               /* Due to interference in memory reclamation (X may be
3729                  obstack-deallocated at this point), we must guard against
3730                  one really special case.  [jason: This should be handled
3731                  by start_function]  */
3732               if (current_function_decl == x)
3733                 current_function_decl = t;
3734 #endif
3735               if (TREE_CODE (t) == TYPE_DECL)
3736                 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
3737               else if (TREE_CODE (t) == FUNCTION_DECL)
3738                 check_default_args (t);
3739
3740               return t;
3741             }
3742           else if (DECL_MAIN_P (x))
3743             {
3744               /* A redeclaration of main, but not a duplicate of the
3745                  previous one. 
3746
3747                  [basic.start.main]
3748
3749                  This function shall not be overloaded.  */
3750               cp_error_at ("invalid redeclaration of `%D'", t);
3751               cp_error ("as `%D'", x);
3752               /* We don't try to push this declaration since that
3753                  causes a crash.  */
3754               return x;
3755             }
3756         }
3757
3758       check_template_shadow (x);
3759
3760       /* If this is a function conjured up by the backend, massage it
3761          so it looks friendly.  */
3762       if (TREE_CODE (x) == FUNCTION_DECL
3763           && ! DECL_LANG_SPECIFIC (x))
3764         {
3765           retrofit_lang_decl (x);
3766           DECL_LANGUAGE (x) = lang_c;
3767         }
3768
3769       if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_FUNCTION_MEMBER_P (x))
3770         {
3771           t = push_overloaded_decl (x, PUSH_LOCAL);
3772           if (t != x)
3773             return t;
3774           if (!namespace_bindings_p ())
3775             /* We do not need to create a binding for this name;
3776                push_overloaded_decl will have already done so if
3777                necessary.  */
3778             need_new_binding = 0;
3779         }
3780       else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
3781         {
3782           t = push_overloaded_decl (x, PUSH_GLOBAL);
3783           if (t == x)
3784             add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
3785           return t;
3786         }
3787
3788       /* If declaring a type as a typedef, copy the type (unless we're
3789          at line 0), and install this TYPE_DECL as the new type's typedef
3790          name.  See the extensive comment in ../c-decl.c (pushdecl). */
3791       if (TREE_CODE (x) == TYPE_DECL)
3792         {
3793           tree type = TREE_TYPE (x);
3794           if (DECL_SOURCE_LINE (x) == 0)
3795             {
3796               if (TYPE_NAME (type) == 0)
3797                 TYPE_NAME (type) = x;
3798             }
3799           else if (type != error_mark_node && TYPE_NAME (type) != x
3800                    /* We don't want to copy the type when all we're
3801                       doing is making a TYPE_DECL for the purposes of
3802                       inlining.  */
3803                    && (!TYPE_NAME (type) 
3804                        || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x)))
3805             {
3806               DECL_ORIGINAL_TYPE (x) = type;
3807               type = build_type_copy (type);
3808               TYPE_STUB_DECL (type) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
3809               TYPE_NAME (type) = x;
3810               TREE_TYPE (x) = type;
3811             }
3812
3813           if (type != error_mark_node
3814               && TYPE_NAME (type)
3815               && TYPE_IDENTIFIER (type))
3816             set_identifier_type_value_with_scope (DECL_NAME (x), type, 
3817                                                   current_binding_level);
3818
3819         }
3820
3821       /* Multiple external decls of the same identifier ought to match.
3822
3823          We get warnings about inline functions where they are defined.
3824          We get warnings about other functions from push_overloaded_decl.
3825          
3826          Avoid duplicate warnings where they are used.  */
3827       if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
3828         {
3829           tree decl;
3830
3831           if (IDENTIFIER_NAMESPACE_VALUE (name) != NULL_TREE
3832               && (DECL_EXTERNAL (IDENTIFIER_NAMESPACE_VALUE (name))
3833                   || TREE_PUBLIC (IDENTIFIER_NAMESPACE_VALUE (name))))
3834             decl = IDENTIFIER_NAMESPACE_VALUE (name);
3835           else
3836             decl = NULL_TREE;
3837
3838           if (decl
3839               /* If different sort of thing, we already gave an error.  */
3840               && TREE_CODE (decl) == TREE_CODE (x)
3841               && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl)))
3842             {
3843               cp_pedwarn ("type mismatch with previous external decl", x);
3844               cp_pedwarn_at ("previous external decl of `%#D'", decl);
3845             }
3846         }
3847
3848       /* This name is new in its binding level.
3849          Install the new declaration and return it.  */
3850       if (namespace_bindings_p ())
3851         {
3852           /* Install a global value.  */
3853
3854           /* If the first global decl has external linkage,
3855              warn if we later see static one.  */
3856           if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
3857             TREE_PUBLIC (name) = 1;
3858
3859           if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
3860                 && t != NULL_TREE)
3861               /* For an ordinary function, we create a binding from
3862                  the mangled name (i.e., NAME) to the DECL.  But, for
3863                  an `extern "C"' function, the mangled name and the
3864                  ordinary name are the same so we need not do this.  */
3865               && !(TREE_CODE (x) == FUNCTION_DECL && 
3866                    DECL_LANGUAGE (x) == lang_c))
3867             {
3868               if (TREE_CODE (x) == FUNCTION_DECL)
3869                 my_friendly_assert 
3870                   ((IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE)
3871                   || (IDENTIFIER_GLOBAL_VALUE (name) == x), 378);
3872               SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
3873             }
3874
3875           /* Don't forget if the function was used via an implicit decl.  */
3876           if (IDENTIFIER_IMPLICIT_DECL (name)
3877               && TREE_USED (IDENTIFIER_IMPLICIT_DECL (name)))
3878             TREE_USED (x) = 1;
3879
3880           /* Don't forget if its address was taken in that way.  */
3881           if (IDENTIFIER_IMPLICIT_DECL (name)
3882               && TREE_ADDRESSABLE (IDENTIFIER_IMPLICIT_DECL (name)))
3883             TREE_ADDRESSABLE (x) = 1;
3884
3885           /* Warn about mismatches against previous implicit decl.  */
3886           if (IDENTIFIER_IMPLICIT_DECL (name) != NULL_TREE
3887               /* If this real decl matches the implicit, don't complain.  */
3888               && ! (TREE_CODE (x) == FUNCTION_DECL
3889                     && TREE_TYPE (TREE_TYPE (x)) == integer_type_node))
3890             cp_warning
3891               ("`%D' was previously implicitly declared to return `int'", x);
3892
3893           /* If new decl is `static' and an `extern' was seen previously,
3894              warn about it.  */
3895           if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
3896             warn_extern_redeclared_static (x, t);
3897         }
3898       else
3899         {
3900           /* Here to install a non-global value.  */
3901           tree oldlocal = IDENTIFIER_VALUE (name);
3902           tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
3903
3904           if (need_new_binding)
3905             {
3906               push_local_binding (name, x, 0);
3907               /* Because push_local_binding will hook X on to the
3908                  current_binding_level's name list, we don't want to
3909                  do that again below.  */
3910               need_new_binding = 0;
3911             }
3912
3913           /* If this is a TYPE_DECL, push it into the type value slot.  */
3914           if (TREE_CODE (x) == TYPE_DECL)
3915             set_identifier_type_value_with_scope (name, TREE_TYPE (x), 
3916                                                   current_binding_level);
3917
3918           /* Clear out any TYPE_DECL shadowed by a namespace so that
3919              we won't think this is a type.  The C struct hack doesn't
3920              go through namespaces.  */
3921           if (TREE_CODE (x) == NAMESPACE_DECL)
3922             set_identifier_type_value_with_scope (name, NULL_TREE, 
3923                                                   current_binding_level);
3924
3925           /* If this is an extern function declaration, see if we
3926              have a global definition or declaration for the function.  */
3927           if (oldlocal == NULL_TREE
3928               && DECL_EXTERNAL (x)
3929               && oldglobal != NULL_TREE
3930               && TREE_CODE (x) == FUNCTION_DECL
3931               && TREE_CODE (oldglobal) == FUNCTION_DECL)
3932             {
3933               /* We have one.  Their types must agree.  */
3934               if (decls_match (x, oldglobal))
3935                 /* OK */;
3936               else
3937                 {
3938                   cp_warning ("extern declaration of `%#D' doesn't match", x);
3939                   cp_warning_at ("global declaration `%#D'", oldglobal);
3940                 }
3941             }
3942           /* If we have a local external declaration,
3943              and no file-scope declaration has yet been seen,
3944              then if we later have a file-scope decl it must not be static.  */
3945           if (oldlocal == NULL_TREE
3946               && oldglobal == NULL_TREE
3947               && DECL_EXTERNAL (x)
3948               && TREE_PUBLIC (x))
3949             TREE_PUBLIC (name) = 1;
3950
3951           if (DECL_FROM_INLINE (x))
3952             /* Inline decls shadow nothing.  */;
3953
3954           /* Warn if shadowing an argument at the top level of the body.  */
3955           else if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
3956                    && TREE_CODE (oldlocal) == PARM_DECL
3957                    /* Don't complain if it's from an enclosing function.  */
3958                    && DECL_CONTEXT (oldlocal) == current_function_decl
3959                    && TREE_CODE (x) != PARM_DECL)
3960             {
3961               /* Go to where the parms should be and see if we
3962                  find them there.  */
3963               struct binding_level *b = current_binding_level->level_chain;
3964
3965               if (cleanup_label)
3966                 b = b->level_chain;
3967
3968               /* ARM $8.3 */
3969               if (b->parm_flag == 1)
3970                 cp_error ("declaration of `%#D' shadows a parameter", name);
3971             }
3972           else if (warn_shadow && oldlocal != NULL_TREE
3973                    && current_binding_level->is_for_scope
3974                    && !DECL_DEAD_FOR_LOCAL (oldlocal))
3975             {
3976               warning ("variable `%s' shadows local",
3977                        IDENTIFIER_POINTER (name));
3978               cp_warning_at ("  this is the shadowed declaration", oldlocal);
3979             }              
3980           /* Maybe warn if shadowing something else.  */
3981           else if (warn_shadow && !DECL_EXTERNAL (x)
3982                    /* No shadow warnings for internally generated vars.  */
3983                    && ! DECL_ARTIFICIAL (x)
3984                    /* No shadow warnings for vars made for inlining.  */
3985                    && ! DECL_FROM_INLINE (x))
3986             {
3987               if (oldlocal != NULL_TREE && TREE_CODE (oldlocal) == PARM_DECL)
3988                 warning ("declaration of `%s' shadows a parameter",
3989                         IDENTIFIER_POINTER (name));
3990               else if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE
3991                        && current_class_ptr
3992                        && !TREE_STATIC (name))
3993                 warning ("declaration of `%s' shadows a member of `this'",
3994                         IDENTIFIER_POINTER (name));
3995               else if (oldlocal != NULL_TREE)
3996                 warning ("declaration of `%s' shadows previous local",
3997                         IDENTIFIER_POINTER (name));
3998               else if (oldglobal != NULL_TREE)
3999                 /* XXX shadow warnings in outer-more namespaces */
4000                 warning ("declaration of `%s' shadows global declaration",
4001                         IDENTIFIER_POINTER (name));
4002             }
4003         }
4004
4005       if (TREE_CODE (x) == FUNCTION_DECL)
4006         check_default_args (x);
4007
4008       /* Keep count of variables in this level with incomplete type.  */
4009       if (TREE_CODE (x) == VAR_DECL
4010           && TREE_TYPE (x) != error_mark_node
4011           && ((TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
4012                && PROMOTES_TO_AGGR_TYPE (TREE_TYPE (x), ARRAY_TYPE))
4013               /* RTTI TD entries are created while defining the type_info.  */
4014               || (TYPE_LANG_SPECIFIC (TREE_TYPE (x))
4015                   && TYPE_BEING_DEFINED (TREE_TYPE (x)))))
4016         current_binding_level->incomplete 
4017           = tree_cons (NULL_TREE, x, current_binding_level->incomplete);
4018     }
4019
4020   if (need_new_binding)
4021     add_decl_to_level (x, 
4022                        DECL_NAMESPACE_SCOPE_P (x)
4023                        ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x))
4024                        : current_binding_level);
4025
4026   return x;
4027 }
4028
4029 /* Same as pushdecl, but define X in binding-level LEVEL.  We rely on the
4030    caller to set DECL_CONTEXT properly.  */
4031
4032 static tree
4033 pushdecl_with_scope (x, level)
4034      tree x;
4035      struct binding_level *level;
4036 {
4037   register struct binding_level *b;
4038   tree function_decl = current_function_decl;
4039
4040   current_function_decl = NULL_TREE;
4041   if (level->parm_flag == 2)
4042     {
4043       b = class_binding_level;
4044       class_binding_level = level;
4045       pushdecl_class_level (x);
4046       class_binding_level = b;
4047     }
4048   else
4049     {
4050       b = current_binding_level;
4051       current_binding_level = level;
4052       x = pushdecl (x);
4053       current_binding_level = b;
4054     }
4055   current_function_decl = function_decl;
4056   return x;
4057 }
4058
4059 /* Like pushdecl, only it places X in the current namespace,
4060    if appropriate.  */
4061
4062 tree
4063 pushdecl_namespace_level (x)
4064      tree x;
4065 {
4066   register struct binding_level *b = current_binding_level;
4067   register tree t;
4068
4069   t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace));
4070
4071   /* Now, the type_shadowed stack may screw us.  Munge it so it does
4072      what we want.  */
4073   if (TREE_CODE (x) == TYPE_DECL)
4074     {
4075       tree name = DECL_NAME (x);
4076       tree newval;
4077       tree *ptr = (tree *)0;
4078       for (; b != global_binding_level; b = b->level_chain)
4079         {
4080           tree shadowed = b->type_shadowed;
4081           for (; shadowed; shadowed = TREE_CHAIN (shadowed))
4082             if (TREE_PURPOSE (shadowed) == name)
4083               {
4084                 ptr = &TREE_VALUE (shadowed);
4085                 /* Can't break out of the loop here because sometimes
4086                    a binding level will have duplicate bindings for
4087                    PT names.  It's gross, but I haven't time to fix it.  */
4088               }
4089         }
4090       newval = TREE_TYPE (x);
4091       if (ptr == (tree *)0)
4092         {
4093           /* @@ This shouldn't be needed.  My test case "zstring.cc" trips
4094              up here if this is changed to an assertion.  --KR  */
4095           SET_IDENTIFIER_TYPE_VALUE (name, newval);
4096         }
4097       else
4098         {
4099           *ptr = newval;
4100         }
4101     }
4102   return t;
4103 }
4104
4105 /* Like pushdecl, only it places X in GLOBAL_BINDING_LEVEL,
4106    if appropriate.  */
4107
4108 tree
4109 pushdecl_top_level (x)
4110      tree x;
4111 {
4112   push_to_top_level ();
4113   x = pushdecl_namespace_level (x);
4114   pop_from_top_level ();
4115   return x;
4116 }
4117
4118 /* Make the declaration of X appear in CLASS scope.  */
4119
4120 void
4121 pushdecl_class_level (x)
4122      tree x;
4123 {
4124   /* Don't use DECL_ASSEMBLER_NAME here!  Everything that looks in class
4125      scope looks for the pre-mangled name.  */
4126   register tree name;
4127
4128   if (TREE_CODE (x) == OVERLOAD)
4129     x = OVL_CURRENT (x);
4130   name = DECL_NAME (x);
4131
4132   if (name)
4133     {
4134       push_class_level_binding (name, x);
4135       if (TREE_CODE (x) == TYPE_DECL)
4136         set_identifier_type_value (name, TREE_TYPE (x));
4137     }
4138   else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
4139     {
4140       tree f;
4141
4142       for (f = TYPE_FIELDS (TREE_TYPE (x));
4143            f;
4144            f = TREE_CHAIN (f))
4145         pushdecl_class_level (f);
4146     }
4147 }
4148
4149 /* Enter DECL into the symbol table, if that's appropriate.  Returns
4150    DECL, or a modified version thereof.  */
4151
4152 tree
4153 maybe_push_decl (decl)
4154      tree decl;
4155 {
4156   tree type = TREE_TYPE (decl);
4157
4158   /* Add this decl to the current binding level, but not if it comes
4159      from another scope, e.g. a static member variable.  TEM may equal
4160      DECL or it may be a previous decl of the same name.  */
4161   if ((TREE_CODE (decl) != PARM_DECL 
4162        && DECL_CONTEXT (decl) != NULL_TREE 
4163        /* Definitions of namespace members outside their namespace are
4164           possible. */
4165        && TREE_CODE (DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4166       || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
4167       || TREE_CODE (type) == UNKNOWN_TYPE
4168       /* The declaration of a template specialization does not affect
4169          the functions available for overload resolution, so we do not
4170          call pushdecl.  */
4171       || (TREE_CODE (decl) == FUNCTION_DECL
4172           && DECL_TEMPLATE_SPECIALIZATION (decl)))
4173     return decl;
4174   else
4175     return pushdecl (decl);
4176 }
4177
4178 #if 0
4179 /* This function is used to push the mangled decls for nested types into
4180    the appropriate scope.  Previously pushdecl_top_level was used, but that
4181    is incorrect for members of local classes.  */
4182
4183 void
4184 pushdecl_nonclass_level (x)
4185      tree x;
4186 {
4187   struct binding_level *b = current_binding_level;
4188
4189   my_friendly_assert (b->parm_flag != 2, 180);
4190
4191 #if 0
4192   /* Get out of template binding levels */
4193   while (b->pseudo_global)
4194     b = b->level_chain;
4195 #endif
4196
4197   pushdecl_with_scope (x, b);
4198 }
4199 #endif
4200
4201 /* Make the declaration(s) of X appear in CLASS scope
4202    under the name NAME.  */
4203
4204 void
4205 push_class_level_binding (name, x)
4206      tree name;
4207      tree x;
4208 {
4209   tree binding;
4210   /* The class_binding_level will be NULL if x is a template 
4211      parameter name in a member template.  */
4212   if (!class_binding_level)
4213     return;
4214
4215   /* Make sure that this new member does not have the same name
4216      as a template parameter.  */
4217   if (TYPE_BEING_DEFINED (current_class_type))
4218     check_template_shadow (x);
4219
4220   /* If this declaration shadows a declaration from an enclosing
4221      class, then we will need to restore IDENTIFIER_CLASS_VALUE when
4222      we leave this class.  Record the shadowed declaration here.  */
4223   binding = IDENTIFIER_BINDING (name);
4224   if (binding 
4225       && ((TREE_CODE (x) == OVERLOAD
4226            && BINDING_VALUE (binding)
4227            && is_overloaded_fn (BINDING_VALUE (binding)))
4228           || INHERITED_VALUE_BINDING_P (binding)))
4229     {
4230       tree shadow;
4231       tree old_decl;
4232
4233       /* If the old binding was from a base class, and was for a tag
4234          name, slide it over to make room for the new binding.  The
4235          old binding is still visible if explicitly qualified with a
4236          class-key.  */
4237       if (INHERITED_VALUE_BINDING_P (binding)
4238           && BINDING_VALUE (binding)
4239           && TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
4240           && DECL_ARTIFICIAL (BINDING_VALUE (binding))
4241           && !(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)))
4242         {
4243           old_decl = BINDING_TYPE (binding);
4244           BINDING_TYPE (binding) = BINDING_VALUE (binding);
4245           BINDING_VALUE (binding) = NULL_TREE;
4246           INHERITED_VALUE_BINDING_P (binding) = 0;
4247         }
4248       else
4249         old_decl = BINDING_VALUE (binding);
4250
4251       /* There was already a binding for X containing fewer
4252          functions than are named in X.  Find the previous
4253          declaration of X on the class-shadowed list, and update it.  */
4254       for (shadow = class_binding_level->class_shadowed;
4255            shadow;
4256            shadow = TREE_CHAIN (shadow))
4257         if (TREE_PURPOSE (shadow) == name
4258             && TREE_TYPE (shadow) == old_decl)
4259           {
4260             BINDING_VALUE (binding) = x;
4261             INHERITED_VALUE_BINDING_P (binding) = 0;
4262             TREE_TYPE (shadow) = x;
4263             return;
4264           }
4265     }
4266
4267   /* If we didn't replace an existing binding, put the binding on the
4268      stack of bindings for the identifier, and update
4269      IDENTIFIER_CLASS_VALUE.  */
4270   if (push_class_binding (name, x))
4271     {
4272       class_binding_level->class_shadowed
4273         = tree_cons (name, IDENTIFIER_CLASS_VALUE (name),
4274                      class_binding_level->class_shadowed);
4275       /* Record the value we are binding NAME to so that we can know
4276          what to pop later.  */
4277       TREE_TYPE (class_binding_level->class_shadowed) = x;
4278     }
4279 }
4280
4281 /* Insert another USING_DECL into the current binding level,
4282    returning this declaration. If this is a redeclaration,
4283    do nothing and return NULL_TREE.  */
4284
4285 tree
4286 push_using_decl (scope, name)
4287      tree scope;
4288      tree name;
4289 {
4290   tree decl;
4291   
4292   my_friendly_assert (TREE_CODE (scope) == NAMESPACE_DECL, 383);
4293   my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 384);
4294   for (decl = current_binding_level->usings; decl; decl = TREE_CHAIN (decl))
4295     if (DECL_INITIAL (decl) == scope && DECL_NAME (decl) == name)
4296       break;
4297   if (decl)
4298     return NULL_TREE;
4299   decl = build_lang_decl (USING_DECL, name, void_type_node);
4300   DECL_INITIAL (decl) = scope;
4301   TREE_CHAIN (decl) = current_binding_level->usings;
4302   current_binding_level->usings = decl;
4303   return decl;
4304 }
4305
4306 /* Add namespace to using_directives. Return NULL_TREE if nothing was
4307    changed (i.e. there was already a directive), or the fresh
4308    TREE_LIST otherwise.  */
4309
4310 tree
4311 push_using_directive (used)
4312      tree used;
4313 {
4314   tree ud = current_binding_level->using_directives;
4315   tree iter, ancestor;
4316   
4317   /* Check if we already have this. */
4318   if (purpose_member (used, ud) != NULL_TREE)
4319     return NULL_TREE;
4320
4321   /* Recursively add all namespaces used. */
4322   for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
4323     push_using_directive (TREE_PURPOSE (iter));
4324
4325   ancestor = namespace_ancestor (current_decl_namespace (), used);
4326   ud = current_binding_level->using_directives;
4327   ud = tree_cons (used, ancestor, ud);
4328   current_binding_level->using_directives = ud;
4329   return ud;
4330 }
4331
4332 /* DECL is a FUNCTION_DECL for a non-member function, which may have
4333    other definitions already in place.  We get around this by making
4334    the value of the identifier point to a list of all the things that
4335    want to be referenced by that name.  It is then up to the users of
4336    that name to decide what to do with that list.
4337
4338    DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its DECL_RESULT
4339    slot.  It is dealt with the same way.
4340
4341    FLAGS is a bitwise-or of the following values:
4342      PUSH_LOCAL: Bind DECL in the current scope, rather than at
4343                  namespace scope.
4344      PUSH_USING: DECL is being pushed as the result of a using
4345                  declaration. 
4346
4347    The value returned may be a previous declaration if we guessed wrong
4348    about what language DECL should belong to (C or C++).  Otherwise,
4349    it's always DECL (and never something that's not a _DECL).  */
4350
4351 tree
4352 push_overloaded_decl (decl, flags)
4353      tree decl;
4354      int flags;
4355 {
4356   tree name = DECL_NAME (decl);
4357   tree old;
4358   tree new_binding;
4359   int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
4360
4361   if (doing_global)
4362     old = namespace_binding (name, DECL_CONTEXT (decl));
4363   else
4364     old = lookup_name_current_level (name);
4365
4366   if (old)
4367     {
4368       if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
4369         {
4370           tree t = TREE_TYPE (old);
4371           if (IS_AGGR_TYPE (t) && warn_shadow
4372               && (! DECL_IN_SYSTEM_HEADER (decl)
4373                   || ! DECL_IN_SYSTEM_HEADER (old)))
4374             cp_warning ("`%#D' hides constructor for `%#T'", decl, t);
4375           old = NULL_TREE;
4376         }
4377       else if (is_overloaded_fn (old))
4378         {
4379           tree tmp;
4380           
4381           for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
4382             {
4383               tree fn = OVL_CURRENT (tmp);
4384
4385               if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp)
4386                   && !(flags & PUSH_USING)
4387                   && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
4388                                 TYPE_ARG_TYPES (TREE_TYPE (decl))))
4389                 cp_error ("`%#D' conflicts with previous using declaration `%#D'",
4390                           decl, fn);
4391               
4392               if (duplicate_decls (decl, fn))
4393                 return fn;
4394             }
4395         }
4396       else
4397         {
4398           cp_error_at ("previous non-function declaration `%#D'", old);
4399           cp_error ("conflicts with function declaration `%#D'", decl);
4400           return decl;
4401         }
4402     }
4403
4404   if (old || TREE_CODE (decl) == TEMPLATE_DECL)
4405     {
4406       if (old && TREE_CODE (old) != OVERLOAD)
4407         new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE));
4408       else
4409         new_binding = ovl_cons (decl, old);
4410       if (flags & PUSH_USING)
4411         OVL_USED (new_binding) = 1;
4412     }
4413   else
4414     /* NAME is not ambiguous.  */
4415     new_binding = decl;
4416
4417   if (doing_global)
4418     set_namespace_binding (name, current_namespace, new_binding);
4419   else
4420     {
4421       /* We only create an OVERLOAD if there was a previous binding at
4422          this level, or if decl is a template. In the former case, we
4423          need to remove the old binding and replace it with the new
4424          binding.  We must also run through the NAMES on the binding
4425          level where the name was bound to update the chain.  */
4426
4427       if (TREE_CODE (new_binding) == OVERLOAD && old)
4428         {
4429           tree *d;
4430           
4431           for (d = &BINDING_LEVEL (IDENTIFIER_BINDING (name))->names;
4432                *d;
4433                d = &TREE_CHAIN (*d))
4434             if (*d == old
4435                 || (TREE_CODE (*d) == TREE_LIST
4436                     && TREE_VALUE (*d) == old))
4437               {
4438                 if (TREE_CODE (*d) == TREE_LIST)
4439                   /* Just replace the old binding with the new.  */
4440                   TREE_VALUE (*d) = new_binding;
4441                 else
4442                   /* Build a TREE_LIST to wrap the OVERLOAD.  */
4443                   *d = tree_cons (NULL_TREE, new_binding, 
4444                                   TREE_CHAIN (*d));
4445
4446                 /* And update the CPLUS_BINDING node.  */
4447                 BINDING_VALUE (IDENTIFIER_BINDING (name))
4448                   = new_binding;
4449                 return decl;
4450               }
4451
4452           /* We should always find a previous binding in this case.  */
4453           my_friendly_abort (0);
4454         }
4455
4456       /* Install the new binding.  */
4457       push_local_binding (name, new_binding, flags);
4458     }
4459
4460   return decl;
4461 }
4462 \f
4463 /* Generate an implicit declaration for identifier FUNCTIONID
4464    as a function of type int ().  Print a warning if appropriate.  */
4465
4466 tree
4467 implicitly_declare (functionid)
4468      tree functionid;
4469 {
4470   register tree decl;
4471
4472   /* We used to reuse an old implicit decl here,
4473      but this loses with inline functions because it can clobber
4474      the saved decl chains.  */
4475   decl = build_lang_decl (FUNCTION_DECL, functionid, default_function_type);
4476
4477   DECL_EXTERNAL (decl) = 1;
4478   TREE_PUBLIC (decl) = 1;
4479
4480   /* ANSI standard says implicit declarations are in the innermost block.
4481      So we record the decl in the standard fashion.  */
4482   pushdecl (decl);
4483   rest_of_decl_compilation (decl, NULL_PTR, 0, 0);
4484
4485   if (warn_implicit
4486       /* Only one warning per identifier.  */
4487       && IDENTIFIER_IMPLICIT_DECL (functionid) == NULL_TREE)
4488     {
4489       cp_pedwarn ("implicit declaration of function `%#D'", decl);
4490     }
4491
4492   SET_IDENTIFIER_IMPLICIT_DECL (functionid, decl);
4493
4494   return decl;
4495 }
4496
4497 /* Return zero if the declaration NEWDECL is valid
4498    when the declaration OLDDECL (assumed to be for the same name)
4499    has already been seen.
4500    Otherwise return an error message format string with a %s
4501    where the identifier should go.  */
4502
4503 static const char *
4504 redeclaration_error_message (newdecl, olddecl)
4505      tree newdecl, olddecl;
4506 {
4507   if (TREE_CODE (newdecl) == TYPE_DECL)
4508     {
4509       /* Because C++ can put things into name space for free,
4510          constructs like "typedef struct foo { ... } foo"
4511          would look like an erroneous redeclaration.  */
4512       if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
4513         return 0;
4514       else
4515         return "redefinition of `%#D'";
4516     }
4517   else if (TREE_CODE (newdecl) == FUNCTION_DECL)
4518     {
4519       /* If this is a pure function, its olddecl will actually be
4520          the original initialization to `0' (which we force to call
4521          abort()).  Don't complain about redefinition in this case.  */
4522       if (DECL_LANG_SPECIFIC (olddecl) && DECL_ABSTRACT_VIRTUAL_P (olddecl))
4523         return 0;
4524
4525       /* If both functions come from different namespaces, this is not
4526          a redeclaration - this is a conflict with a used function. */
4527       if (DECL_NAMESPACE_SCOPE_P (olddecl)
4528           && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl))
4529         return "`%D' conflicts with used function";
4530
4531       /* We'll complain about linkage mismatches in
4532          warn_extern_redeclared_static.  */
4533
4534       /* Defining the same name twice is no good.  */
4535       if (DECL_INITIAL (olddecl) != NULL_TREE
4536           && DECL_INITIAL (newdecl) != NULL_TREE)
4537         {
4538           if (DECL_NAME (olddecl) == NULL_TREE)
4539             return "`%#D' not declared in class";
4540           else
4541             return "redefinition of `%#D'";
4542         }
4543       return 0;
4544     }
4545   else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
4546     {
4547       if ((TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
4548            && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl))
4549            && DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)))
4550           || (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL
4551               && TYPE_SIZE (TREE_TYPE (newdecl))
4552               && TYPE_SIZE (TREE_TYPE (olddecl))))
4553         return "redefinition of `%#D'";
4554       return 0;
4555     }
4556   else if (toplevel_bindings_p ())
4557     {
4558       /* Objects declared at top level:  */
4559       /* If at least one is a reference, it's ok.  */
4560       if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
4561         return 0;
4562       /* Reject two definitions.  */
4563       return "redefinition of `%#D'";
4564     }
4565   else
4566     {
4567       /* Objects declared with block scope:  */
4568       /* Reject two definitions, and reject a definition
4569          together with an external reference.  */
4570       if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
4571         return "redeclaration of `%#D'";
4572       return 0;
4573     }
4574 }
4575 \f
4576 /* Create a new label, named ID.  */
4577
4578 static tree
4579 make_label_decl (id, local_p)
4580      tree id;
4581      int local_p;
4582 {
4583   tree decl;
4584
4585   decl = build_decl (LABEL_DECL, id, void_type_node);
4586   if (expanding_p)
4587     /* Make sure every label has an rtx.  */
4588     label_rtx (decl);
4589
4590   DECL_CONTEXT (decl) = current_function_decl;
4591   DECL_MODE (decl) = VOIDmode;
4592   C_DECLARED_LABEL_FLAG (decl) = local_p;
4593
4594   /* Say where one reference is to the label, for the sake of the
4595      error if it is not defined.  */
4596   DECL_SOURCE_LINE (decl) = lineno;
4597   DECL_SOURCE_FILE (decl) = input_filename;
4598
4599   /* Record the fact that this identifier is bound to this label.  */
4600   SET_IDENTIFIER_LABEL_VALUE (id, decl);
4601
4602   /* Record this label on the list of used labels so that we can check
4603      at the end of the function to see whether or not the label was
4604      actually defined.  */
4605   if ((named_label_uses == NULL || named_label_uses->label_decl != decl)
4606       && (named_label_uses == NULL
4607           || named_label_uses->names_in_scope != current_binding_level->names
4608           || named_label_uses->label_decl != decl))
4609     {
4610       struct named_label_list *new_ent;
4611       new_ent
4612         = (struct named_label_list*)oballoc (sizeof (struct named_label_list));
4613       new_ent->label_decl = decl;
4614       new_ent->names_in_scope = current_binding_level->names;
4615       new_ent->binding_level = current_binding_level;
4616       new_ent->lineno_o_goto = lineno;
4617       new_ent->filename_o_goto = input_filename;
4618       new_ent->next = named_label_uses;
4619       named_label_uses = new_ent;
4620     }
4621
4622   return decl;
4623 }
4624
4625 /* Look for a label named ID in the current function.  If one cannot
4626    be found, create one.  (We keep track of used, but undefined,
4627    labels, and complain about them at the end of a function.)  */
4628
4629 tree 
4630 lookup_label (id)
4631      tree id;
4632 {
4633   tree decl;
4634
4635   /* You can't use labels at global scope.  */
4636   if (current_function_decl == NULL_TREE)
4637     {
4638       error ("label `%s' referenced outside of any function",
4639              IDENTIFIER_POINTER (id));
4640       return NULL_TREE;
4641     }
4642   
4643   /* See if we've already got this label.  */
4644   decl = IDENTIFIER_LABEL_VALUE (id);
4645   if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl)
4646     return decl;
4647
4648   /* Record this label on the list of labels used in this function.
4649      We do this before calling make_label_decl so that we get the
4650      IDENTIFIER_LABEL_VALUE before the new label is declared.  */
4651   named_labels = tree_cons (IDENTIFIER_LABEL_VALUE (id), NULL_TREE,
4652                             named_labels);
4653   /* We need a new label.  */
4654   decl = make_label_decl (id, /*local_p=*/0);
4655   /* Now fill in the information we didn't have before.  */
4656   TREE_VALUE (named_labels) = decl;
4657
4658   return decl;
4659 }
4660
4661 /* Declare a local label named ID.  */
4662
4663 tree
4664 declare_local_label (id)
4665      tree id;
4666 {
4667   tree decl;
4668
4669   /* Add a new entry to the SHADOWED_LABELS list so that when we leave
4670      this scope we can restore the old value of
4671      IDENTIFIER_TYPE_VALUE.  */
4672   current_binding_level->shadowed_labels 
4673     = tree_cons (IDENTIFIER_LABEL_VALUE (id), NULL_TREE,
4674                  current_binding_level->shadowed_labels);
4675   /* Look for the label.  */
4676   decl = make_label_decl (id, /*local_p=*/1);
4677   /* Now fill in the information we didn't have before.  */
4678   TREE_VALUE (current_binding_level->shadowed_labels) = decl;
4679   
4680   return decl;
4681 }
4682
4683 /* Define a label, specifying the location in the source file.
4684    Return the LABEL_DECL node for the label, if the definition is valid.
4685    Otherwise return 0.  */
4686
4687 tree
4688 define_label (filename, line, name)
4689      char *filename;
4690      int line;
4691      tree name;
4692 {
4693   tree decl = lookup_label (name);
4694
4695   /* After labels, make any new cleanups go into their
4696      own new (temporary) binding contour.  */
4697   current_binding_level->more_cleanups_ok = 0;
4698
4699   if (name == get_identifier ("wchar_t"))
4700     cp_pedwarn ("label named wchar_t");
4701
4702   if (DECL_INITIAL (decl) != NULL_TREE)
4703     {
4704       cp_error ("duplicate label `%D'", decl);
4705       return 0;
4706     }
4707   else
4708     {
4709       struct named_label_list *uses, *prev;
4710       int identified = 0;
4711       int saw_eh = 0;
4712
4713       /* Mark label as having been defined.  */
4714       DECL_INITIAL (decl) = error_mark_node;
4715       /* Say where in the source.  */
4716       DECL_SOURCE_FILE (decl) = filename;
4717       DECL_SOURCE_LINE (decl) = line;
4718
4719       prev = NULL;
4720       uses = named_label_uses;
4721       while (uses != NULL)
4722         if (uses->label_decl == decl)
4723           {
4724             struct binding_level *b = current_binding_level;
4725             while (b)
4726               {
4727                 tree new_decls = b->names;
4728                 tree old_decls = (b == uses->binding_level)
4729                                   ? uses->names_in_scope : NULL_TREE;
4730                 while (new_decls != old_decls)
4731                   {
4732                     if (TREE_CODE (new_decls) == VAR_DECL
4733                         /* Don't complain about crossing initialization
4734                            of internal entities.  They can't be accessed,
4735                            and they should be cleaned up
4736                            by the time we get to the label.  */
4737                         && ! DECL_ARTIFICIAL (new_decls)
4738                         && !(DECL_INITIAL (new_decls) == NULL_TREE
4739                              && pod_type_p (TREE_TYPE (new_decls))))
4740                       {
4741                         /* This is really only important if we're crossing
4742                            an initialization.  The POD stuff is just
4743                            pedantry; why should it matter if the class
4744                            contains a field of pointer to member type?  */
4745                         int problem = (DECL_INITIAL (new_decls)
4746                                        || (TYPE_NEEDS_CONSTRUCTING
4747                                            (TREE_TYPE (new_decls))));
4748
4749                         if (! identified)
4750                           {
4751                             if (problem)
4752                               {
4753                                 cp_error ("jump to label `%D'", decl);
4754                                 error_with_file_and_line
4755                                   (uses->filename_o_goto,
4756                                    uses->lineno_o_goto, "  from here");
4757                               }
4758                             else
4759                               {
4760                                 cp_pedwarn ("jump to label `%D'", decl);
4761                                 pedwarn_with_file_and_line
4762                                   (uses->filename_o_goto,
4763                                    uses->lineno_o_goto, "  from here");
4764                               }
4765                             identified = 1;
4766                           }
4767
4768                         if (problem)
4769                           cp_error_at ("  crosses initialization of `%#D'",
4770                                        new_decls);
4771                         else
4772                           cp_pedwarn_at ("  enters scope of non-POD `%#D'",
4773                                          new_decls);
4774                       }
4775                     new_decls = TREE_CHAIN (new_decls);
4776                   }
4777                 if (b == uses->binding_level)
4778                   break;
4779                 if (b->eh_region && ! saw_eh)
4780                   {
4781                     if (! identified)
4782                       {
4783                         cp_error ("jump to label `%D'", decl);
4784                         error_with_file_and_line
4785                           (uses->filename_o_goto,
4786                            uses->lineno_o_goto, "  from here");
4787                         identified = 1;
4788                       }
4789                     error ("  enters exception handling block");
4790                     saw_eh = 1;
4791                   }
4792                 b = b->level_chain;
4793               }
4794
4795             if (prev != NULL)
4796               prev->next = uses->next;
4797             else
4798               named_label_uses = uses->next;
4799
4800             uses = uses->next;
4801           }
4802         else
4803           {
4804             prev = uses;
4805             uses = uses->next;
4806           }
4807       current_function_return_value = NULL_TREE;
4808       return decl;
4809     }
4810 }
4811
4812 struct cp_switch
4813 {
4814   struct binding_level *level;
4815   struct cp_switch *next;
4816 };
4817
4818 static struct cp_switch *switch_stack;
4819
4820 void
4821 push_switch ()
4822 {
4823   struct cp_switch *p
4824     = (struct cp_switch *) oballoc (sizeof (struct cp_switch));
4825   p->level = current_binding_level;
4826   p->next = switch_stack;
4827   switch_stack = p;
4828 }
4829
4830 void
4831 pop_switch ()
4832 {
4833   switch_stack = switch_stack->next;
4834 }
4835
4836 /* Note that we've seen a definition of a case label, and complain if this
4837    is a bad place for one.  */
4838
4839 void
4840 define_case_label ()
4841 {
4842   tree cleanup = last_cleanup_this_contour ();
4843   struct binding_level *b = current_binding_level;
4844   int identified = 0;
4845
4846   if (! switch_stack)
4847     /* Don't crash; we'll complain in do_case.  */
4848     return;
4849   
4850   if (cleanup)
4851     {
4852       static int explained = 0;
4853       cp_warning_at ("destructor needed for `%#D'", TREE_PURPOSE (cleanup));
4854       warning ("where case label appears here");
4855       if (!explained)
4856         {
4857           warning ("(enclose actions of previous case statements requiring");
4858           warning ("destructors in their own binding contours.)");
4859           explained = 1;
4860         }
4861     }
4862
4863   for (; b && b != switch_stack->level; b = b->level_chain)
4864     {
4865       tree new_decls = b->names;
4866       for (; new_decls; new_decls = TREE_CHAIN (new_decls))
4867         {
4868           if (TREE_CODE (new_decls) == VAR_DECL
4869               /* Don't complain about crossing initialization
4870                  of internal entities.  They can't be accessed,
4871                  and they should be cleaned up
4872                  by the time we get to the label.  */
4873               && ! DECL_ARTIFICIAL (new_decls)
4874               && ((DECL_INITIAL (new_decls) != NULL_TREE
4875                    && DECL_INITIAL (new_decls) != error_mark_node)
4876                   || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (new_decls))))
4877             {
4878               if (! identified)
4879                 error ("jump to case label");
4880               identified = 1;
4881               cp_error_at ("  crosses initialization of `%#D'",
4882                            new_decls);
4883             }
4884         }
4885     }
4886
4887   /* After labels, make any new cleanups go into their
4888      own new (temporary) binding contour.  */
4889
4890   current_binding_level->more_cleanups_ok = 0;
4891   current_function_return_value = NULL_TREE;
4892 }
4893 \f
4894 /* Return the list of declarations of the current level.
4895    Note that this list is in reverse order unless/until
4896    you nreverse it; and when you do nreverse it, you must
4897    store the result back using `storedecls' or you will lose.  */
4898
4899 tree
4900 getdecls ()
4901 {
4902   return current_binding_level->names;
4903 }
4904
4905 /* Return the list of type-tags (for structs, etc) of the current level.  */
4906
4907 tree
4908 gettags ()
4909 {
4910   return current_binding_level->tags;
4911 }
4912
4913 /* Store the list of declarations of the current level.
4914    This is done for the parameter declarations of a function being defined,
4915    after they are modified in the light of any missing parameters.  */
4916
4917 static void
4918 storedecls (decls)
4919      tree decls;
4920 {
4921   current_binding_level->names = decls;
4922 }
4923
4924 /* Similarly, store the list of tags of the current level.  */
4925
4926 void
4927 storetags (tags)
4928      tree tags;
4929 {
4930   current_binding_level->tags = tags;
4931 }
4932 \f
4933 /* Given NAME, an IDENTIFIER_NODE,
4934    return the structure (or union or enum) definition for that name.
4935    Searches binding levels from BINDING_LEVEL up to the global level.
4936    If THISLEVEL_ONLY is nonzero, searches only the specified context
4937    (but skips any tag-transparent contexts to find one that is
4938    meaningful for tags).
4939    FORM says which kind of type the caller wants;
4940    it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
4941    If the wrong kind of type is found, and it's not a template, an error is
4942    reported.  */
4943
4944 static tree
4945 lookup_tag (form, name, binding_level, thislevel_only)
4946      enum tree_code form;
4947      tree name;
4948      struct binding_level *binding_level;
4949      int thislevel_only;
4950 {
4951   register struct binding_level *level;
4952   /* Non-zero if, we should look past a pseudo-global level, even if
4953      THISLEVEL_ONLY.  */
4954   int allow_pseudo_global = 1;
4955
4956   for (level = binding_level; level; level = level->level_chain)
4957     {
4958       register tree tail;
4959       if (ANON_AGGRNAME_P (name))
4960         for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
4961           {
4962             /* There's no need for error checking here, because
4963                anon names are unique throughout the compilation.  */
4964             if (TYPE_IDENTIFIER (TREE_VALUE (tail)) == name)
4965               return TREE_VALUE (tail);
4966           }
4967       else if (level->namespace_p)
4968         /* Do namespace lookup. */
4969         for (tail = current_namespace; 1; tail = CP_DECL_CONTEXT (tail))
4970           {
4971             tree old = binding_for_name (name, tail);
4972
4973             /* If we just skipped past a pseudo global level, even
4974                though THISLEVEL_ONLY, and we find a template class
4975                declaration, then we use the _TYPE node for the
4976                template.  See the example below.  */
4977             if (thislevel_only && !allow_pseudo_global
4978                 && old && BINDING_VALUE (old) 
4979                 && DECL_CLASS_TEMPLATE_P (BINDING_VALUE (old)))
4980               old = TREE_TYPE (BINDING_VALUE (old));
4981             else 
4982               old = BINDING_TYPE (old);
4983
4984             /* If it has an original type, it is a typedef, and we
4985                should not return it.  */
4986             if (old && DECL_ORIGINAL_TYPE (TYPE_NAME (old)))
4987               old = NULL_TREE;
4988             if (old && TREE_CODE (old) != form
4989                 && !(form != ENUMERAL_TYPE && TREE_CODE (old) == TEMPLATE_DECL))
4990               {
4991                 cp_error ("`%#D' redeclared as %C", old, form);
4992                 return NULL_TREE;
4993               }
4994             if (old)
4995               return old;
4996             if (thislevel_only || tail == global_namespace)
4997               return NULL_TREE;
4998           }
4999       else
5000         for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
5001           {
5002             if (TREE_PURPOSE (tail) == name)
5003               {
5004                 enum tree_code code = TREE_CODE (TREE_VALUE (tail));
5005                 /* Should tighten this up; it'll probably permit
5006                    UNION_TYPE and a struct template, for example.  */
5007                 if (code != form
5008                     && !(form != ENUMERAL_TYPE && code == TEMPLATE_DECL))
5009                   {
5010                     /* Definition isn't the kind we were looking for.  */
5011                     cp_error ("`%#D' redeclared as %C", TREE_VALUE (tail),
5012                               form);
5013                     return NULL_TREE;
5014                   }
5015                 return TREE_VALUE (tail);
5016               }
5017           }
5018       if (thislevel_only && ! level->tag_transparent)
5019         {
5020           if (level->pseudo_global && allow_pseudo_global)
5021             {
5022               /* We must deal with cases like this:
5023                  
5024                    template <class T> struct S;
5025                    template <class T> struct S {};
5026                    
5027                  When looking up `S', for the second declaration, we
5028                  would like to find the first declaration.  But, we
5029                  are in the pseudo-global level created for the
5030                  template parameters, rather than the (surrounding)
5031                  namespace level.  Thus, we keep going one more level,
5032                  even though THISLEVEL_ONLY is non-zero.  */
5033               allow_pseudo_global = 0;
5034               continue;
5035             }
5036           else
5037             return NULL_TREE;
5038         }
5039     }
5040   return NULL_TREE;
5041 }
5042
5043 #if 0
5044 void
5045 set_current_level_tags_transparency (tags_transparent)
5046      int tags_transparent;
5047 {
5048   current_binding_level->tag_transparent = tags_transparent;
5049 }
5050 #endif
5051
5052 /* Given a type, find the tag that was defined for it and return the tag name.
5053    Otherwise return 0.  However, the value can never be 0
5054    in the cases in which this is used.
5055
5056    C++: If NAME is non-zero, this is the new name to install.  This is
5057    done when replacing anonymous tags with real tag names.  */
5058
5059 static tree
5060 lookup_tag_reverse (type, name)
5061      tree type;
5062      tree name;
5063 {
5064   register struct binding_level *level;
5065
5066   for (level = current_binding_level; level; level = level->level_chain)
5067     {
5068       register tree tail;
5069       for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
5070         {
5071           if (TREE_VALUE (tail) == type)
5072             {
5073               if (name)
5074                 TREE_PURPOSE (tail) = name;
5075               return TREE_PURPOSE (tail);
5076             }
5077         }
5078     }
5079   return NULL_TREE;
5080 }
5081 \f
5082 /* Look up NAME in the NAMESPACE.  */
5083
5084 tree
5085 lookup_namespace_name (namespace, name)
5086      tree namespace, name;
5087 {
5088   tree val;
5089   tree template_id = NULL_TREE;
5090
5091   my_friendly_assert (TREE_CODE (namespace) == NAMESPACE_DECL, 370);
5092
5093   if (TREE_CODE (name) == NAMESPACE_DECL)
5094     /* This happens for A::B<int> when B is a namespace. */
5095     return name;
5096   else if (TREE_CODE (name) == TEMPLATE_DECL)
5097     {
5098       /* This happens for A::B where B is a template, and there are no
5099          template arguments.  */
5100       cp_error ("invalid use of `%D'", name);
5101       return error_mark_node;
5102     }
5103
5104   namespace = ORIGINAL_NAMESPACE (namespace);
5105
5106   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
5107     {
5108       template_id = name;
5109       name = TREE_OPERAND (name, 0);
5110       if (TREE_CODE (name) == OVERLOAD)
5111         name = DECL_NAME (OVL_CURRENT (name));
5112       else if (TREE_CODE_CLASS (TREE_CODE (name)) == 'd')
5113         name = DECL_NAME (name);
5114     }
5115
5116   my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 373);
5117   
5118   val = make_node (CPLUS_BINDING);
5119   if (!qualified_lookup_using_namespace (name, namespace, val, 0))
5120     return error_mark_node;
5121
5122   if (BINDING_VALUE (val))
5123     {
5124       val = BINDING_VALUE (val);
5125
5126       if (template_id)
5127         {
5128           if (DECL_CLASS_TEMPLATE_P (val))
5129             val = lookup_template_class (val, 
5130                                          TREE_OPERAND (template_id, 1),
5131                                          /*in_decl=*/NULL_TREE,
5132                                          /*context=*/NULL_TREE,
5133                                          /*entering_scope=*/0);
5134           else if (DECL_FUNCTION_TEMPLATE_P (val)
5135                    || TREE_CODE (val) == OVERLOAD)
5136             val = lookup_template_function (val, 
5137                                             TREE_OPERAND (template_id, 1));
5138           else
5139             {
5140               cp_error ("`%D::%D' is not a template",
5141                         namespace, name);
5142               return error_mark_node;
5143             }
5144         }
5145
5146       /* If we have a single function from a using decl, pull it out.  */
5147       if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
5148         val = OVL_FUNCTION (val);
5149       return val;
5150     }
5151
5152   cp_error ("`%D' undeclared in namespace `%D'", name, namespace);
5153   return error_mark_node;
5154 }
5155
5156 /* Hash a TYPENAME_TYPE.  K is really of type `tree'.  */
5157
5158 static unsigned long
5159 typename_hash (k)
5160      hash_table_key k;
5161 {
5162   unsigned long hash;
5163   tree t;
5164
5165   t = (tree) k;
5166   hash = (((unsigned long) TYPE_CONTEXT (t))
5167           ^ ((unsigned long) DECL_NAME (TYPE_NAME (t))));
5168
5169   return hash;
5170 }
5171
5172 /* Compare two TYPENAME_TYPEs.  K1 and K2 are really of type `tree'.  */
5173
5174 static boolean
5175 typename_compare (k1, k2)
5176      hash_table_key k1;
5177      hash_table_key k2;
5178 {
5179   tree t1;
5180   tree t2;
5181   tree d1;
5182   tree d2;
5183
5184   t1 = (tree) k1;
5185   t2 = (tree) k2;
5186   d1 = TYPE_NAME (t1);
5187   d2 = TYPE_NAME (t2);
5188   
5189   return (DECL_NAME (d1) == DECL_NAME (d2)
5190           && same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2))
5191           && ((TREE_TYPE (t1) != NULL_TREE) 
5192               == (TREE_TYPE (t2) != NULL_TREE))
5193           && same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
5194           && TYPENAME_TYPE_FULLNAME (t1) == TYPENAME_TYPE_FULLNAME (t2));
5195 }
5196
5197 /* Build a TYPENAME_TYPE.  If the type is `typename T::t', CONTEXT is
5198    the type of `T', NAME is the IDENTIFIER_NODE for `t'.  If BASE_TYPE
5199    is non-NULL, this type is being created by the implicit typename
5200    extension, and BASE_TYPE is a type named `t' in some base class of
5201    `T' which depends on template parameters.  
5202
5203    Returns the new TYPENAME_TYPE.  */
5204
5205 tree
5206 build_typename_type (context, name, fullname, base_type)
5207      tree context;
5208      tree name;
5209      tree fullname;
5210      tree base_type;
5211 {
5212   tree t;
5213   tree d;
5214   struct hash_entry* e;
5215
5216   static struct hash_table ht;
5217
5218   if (!ht.table)
5219     {
5220       static struct hash_table *h = &ht;
5221       if (!hash_table_init (&ht, &hash_newfunc, &typename_hash, 
5222                             &typename_compare))
5223         fatal ("virtual memory exhausted");
5224       ggc_add_tree_hash_table_root (&h, 1);
5225     }
5226
5227   /* Build the TYPENAME_TYPE.  */
5228   t = make_lang_type (TYPENAME_TYPE);
5229   TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5230   TYPENAME_TYPE_FULLNAME (t) = fullname;
5231   TREE_TYPE (t) = base_type;
5232
5233   /* Build the corresponding TYPE_DECL.  */
5234   d = build_decl (TYPE_DECL, name, t);
5235   TYPE_NAME (TREE_TYPE (d)) = d;
5236   TYPE_STUB_DECL (TREE_TYPE (d)) = d;
5237   DECL_CONTEXT (d) = FROB_CONTEXT (context);
5238   DECL_ARTIFICIAL (d) = 1;
5239
5240   /* See if we already have this type.  */
5241   e = hash_lookup (&ht, t, /*create=*/false, /*copy=*/0);
5242   if (e)
5243     t = (tree) e->key;
5244   else
5245     /* Insert the type into the table.  */
5246     hash_lookup (&ht, t, /*create=*/true, /*copy=*/0);
5247
5248   return t;
5249 }
5250
5251 /* Resolve `typename CONTEXT::NAME'.  Returns an appropriate type,
5252    unless an error occurs, in which case error_mark_node is returned.
5253    If COMPLAIN zero, don't complain about any errors that occur.  */
5254
5255 tree
5256 make_typename_type (context, name, complain)
5257      tree context, name;
5258      int complain;
5259 {
5260   tree fullname;
5261
5262   if (TREE_CODE_CLASS (TREE_CODE (name)) == 't')
5263     {
5264       if (!(TYPE_LANG_SPECIFIC (name) 
5265             && (CLASSTYPE_IS_TEMPLATE (name) 
5266                 || CLASSTYPE_USE_TEMPLATE (name))))
5267         name = TYPE_IDENTIFIER (name);
5268       else
5269         /* Create a TEMPLATE_ID_EXPR for the type.  */
5270         name = build_nt (TEMPLATE_ID_EXPR,
5271                          CLASSTYPE_TI_TEMPLATE (name),
5272                          CLASSTYPE_TI_ARGS (name));
5273     }
5274   else if (TREE_CODE (name) == TYPE_DECL)
5275     name = DECL_NAME (name);
5276
5277   fullname = name;
5278
5279   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
5280     {
5281       name = TREE_OPERAND (name, 0);
5282       if (TREE_CODE (name) == TEMPLATE_DECL)
5283         name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
5284     }
5285   if (TREE_CODE (name) != IDENTIFIER_NODE)
5286     my_friendly_abort (2000);
5287
5288   if (TREE_CODE (context) == NAMESPACE_DECL)
5289     {
5290       /* We can get here from typename_sub0 in the explicit_template_type
5291          expansion.  Just fail.  */
5292       if (complain)
5293         cp_error ("no class template named `%#T' in `%#T'",
5294                   name, context);
5295       return error_mark_node;
5296     }
5297
5298   if (! uses_template_parms (context)
5299       || currently_open_class (context))
5300     {
5301       if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR)
5302         {
5303           tree tmpl = NULL_TREE;
5304           if (IS_AGGR_TYPE (context))
5305             tmpl = lookup_field (context, name, 0, 0);
5306           if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
5307             {
5308               if (complain)
5309                 cp_error ("no class template named `%#T' in `%#T'",
5310                           name, context);
5311               return error_mark_node;
5312             }
5313
5314           return lookup_template_class (tmpl, 
5315                                         TREE_OPERAND (fullname, 1),
5316                                         NULL_TREE, context, 
5317                                         /*entering_scope=*/0);
5318         }
5319       else
5320         {
5321           tree t;
5322           
5323           if (!IS_AGGR_TYPE (context))
5324             {
5325               if (complain)
5326                 cp_error ("no type named `%#T' in `%#T'", name, context);
5327               return error_mark_node;
5328             }
5329
5330           t = lookup_field (context, name, 0, 1);
5331           if (t)
5332             return TREE_TYPE (t);
5333         }
5334     }
5335
5336   /* If the CONTEXT is not a template type, then either the field is
5337      there now or its never going to be.  */
5338   if (!uses_template_parms (context))
5339     {
5340       if (complain)
5341         cp_error ("no type named `%#T' in `%#T'", name, context);
5342       return error_mark_node;
5343     }
5344     
5345   
5346   return build_typename_type (context, name, fullname,  NULL_TREE);
5347 }
5348
5349 /* Select the right _DECL from multiple choices. */
5350
5351 static tree
5352 select_decl (binding, flags)
5353      tree binding;
5354      int flags;
5355 {
5356   tree val;
5357   val = BINDING_VALUE (binding);
5358   if (LOOKUP_NAMESPACES_ONLY (flags))
5359     {
5360       /* We are not interested in types. */
5361       if (val && TREE_CODE (val) == NAMESPACE_DECL)
5362         return val;
5363       return NULL_TREE;
5364     }
5365   
5366   /* If we could have a type and
5367      we have nothing or we need a type and have none.  */
5368   if (BINDING_TYPE (binding)
5369       && (!val || ((flags & LOOKUP_PREFER_TYPES)
5370                    && TREE_CODE (val) != TYPE_DECL)))
5371     val = TYPE_STUB_DECL (BINDING_TYPE (binding));
5372   /* Don't return non-types if we really prefer types. */
5373   else if (val && LOOKUP_TYPES_ONLY (flags)  && TREE_CODE (val) != TYPE_DECL
5374            && (TREE_CODE (val) != TEMPLATE_DECL
5375                || !DECL_CLASS_TEMPLATE_P (val)))
5376     val = NULL_TREE;
5377
5378   return val;
5379 }
5380
5381 /* Unscoped lookup of a global: iterate over current namespaces,
5382    considering using-directives.  If SPACESP is non-NULL, store a list
5383    of the namespaces we've considered in it.  */
5384
5385 tree
5386 unqualified_namespace_lookup (name, flags, spacesp)
5387      tree name;
5388      int flags;
5389      tree *spacesp;
5390 {
5391   tree b = make_node (CPLUS_BINDING);
5392   tree initial = current_decl_namespace();
5393   tree scope = initial;
5394   tree siter;
5395   struct binding_level *level;
5396   tree val = NULL_TREE;
5397
5398   if (spacesp)
5399     *spacesp = NULL_TREE;
5400
5401   for (; !val; scope = CP_DECL_CONTEXT (scope))
5402     {
5403       if (spacesp)
5404         *spacesp = tree_cons (scope, NULL_TREE, *spacesp);
5405       val = binding_for_name (name, scope);
5406
5407       /* Initialize binding for this context. */
5408       BINDING_VALUE (b) = BINDING_VALUE (val);
5409       BINDING_TYPE (b) = BINDING_TYPE (val);
5410
5411       /* Add all _DECLs seen through local using-directives. */
5412       for (level = current_binding_level; 
5413            !level->namespace_p;
5414            level = level->level_chain)
5415         if (!lookup_using_namespace (name, b, level->using_directives,
5416                                      scope, flags, spacesp))
5417           /* Give up because of error. */
5418           return error_mark_node;
5419
5420       /* Add all _DECLs seen through global using-directives. */
5421       /* XXX local and global using lists should work equally. */
5422       siter = initial;
5423       while (1)
5424         {
5425           if (!lookup_using_namespace (name, b, DECL_NAMESPACE_USING (siter), 
5426                                        scope, flags, spacesp))
5427             /* Give up because of error. */
5428             return error_mark_node;
5429           if (siter == scope) break;
5430           siter = CP_DECL_CONTEXT (siter);
5431         }
5432
5433       val = select_decl (b, flags);
5434       if (scope == global_namespace)
5435         break;
5436     }
5437   return val;
5438 }
5439
5440 /* Combine prefer_type and namespaces_only into flags.  */
5441
5442 static int
5443 lookup_flags (prefer_type, namespaces_only)
5444   int prefer_type, namespaces_only;
5445 {
5446   if (namespaces_only)
5447     return LOOKUP_PREFER_NAMESPACES;
5448   if (prefer_type > 1)
5449     return LOOKUP_PREFER_TYPES;
5450   if (prefer_type > 0)
5451     return LOOKUP_PREFER_BOTH;
5452   return 0;
5453 }
5454
5455 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
5456    ignore it or not.  Subroutine of lookup_name_real.  */
5457
5458 static tree
5459 qualify_lookup (val, flags)
5460      tree val;
5461      int flags;
5462 {
5463   if (val == NULL_TREE)
5464     return val;
5465   if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
5466     return val;
5467   if ((flags & LOOKUP_PREFER_TYPES)
5468       && (TREE_CODE (val) == TYPE_DECL
5469           || ((flags & LOOKUP_TEMPLATES_EXPECTED)
5470               && DECL_CLASS_TEMPLATE_P (val))))
5471     return val;
5472   if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
5473     return NULL_TREE;
5474   return val;
5475 }
5476
5477 /* Any other BINDING overrides an implicit TYPENAME.  Warn about
5478    that.  */
5479
5480 static void
5481 warn_about_implicit_typename_lookup (typename, binding)
5482      tree typename;
5483      tree binding;
5484 {
5485   tree subtype = TREE_TYPE (TREE_TYPE (typename));
5486   tree name = DECL_NAME (typename);
5487
5488   if (! (TREE_CODE (binding) == TEMPLATE_DECL
5489          && CLASSTYPE_TEMPLATE_INFO (subtype)
5490          && CLASSTYPE_TI_TEMPLATE (subtype) == binding)
5491       && ! (TREE_CODE (binding) == TYPE_DECL
5492             && same_type_p (TREE_TYPE (binding), subtype)))
5493     {
5494       cp_warning ("lookup of `%D' finds `%#D'", 
5495                   name, binding);
5496       cp_warning ("  instead of `%D' from dependent base class",
5497                   typename);
5498       cp_warning ("  (use `typename %T::%D' if that's what you meant)",
5499                   constructor_name (current_class_type), name);
5500     }
5501 }
5502
5503 /* Look up NAME in the current binding level and its superiors in the
5504    namespace of variables, functions and typedefs.  Return a ..._DECL
5505    node of some kind representing its definition if there is only one
5506    such declaration, or return a TREE_LIST with all the overloaded
5507    definitions if there are many, or return 0 if it is undefined.
5508
5509    If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
5510    If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
5511    If PREFER_TYPE is -2, we're being called from yylex(). (UGLY)
5512    Otherwise we prefer non-TYPE_DECLs.  
5513
5514    If NONCLASS is non-zero, we don't look for the NAME in class scope,
5515    using IDENTIFIER_CLASS_VALUE.  */
5516
5517 static tree
5518 lookup_name_real (name, prefer_type, nonclass, namespaces_only)
5519      tree name;
5520      int prefer_type, nonclass, namespaces_only;
5521 {
5522   tree t;
5523   tree val = NULL_TREE;
5524   int yylex = 0;
5525   tree from_obj = NULL_TREE;
5526   int flags;
5527   int val_is_implicit_typename = 0;
5528
5529   /* Hack: copy flag set by parser, if set. */
5530   if (only_namespace_names)
5531     namespaces_only = 1;
5532
5533   if (prefer_type == -2)
5534     {
5535       extern int looking_for_typename;
5536       tree type = NULL_TREE;
5537
5538       yylex = 1;
5539       prefer_type = looking_for_typename;
5540
5541       flags = lookup_flags (prefer_type, namespaces_only);
5542       /* If the next thing is '<', class templates are types. */
5543       if (looking_for_template)
5544         flags |= LOOKUP_TEMPLATES_EXPECTED;
5545
5546       /* std:: becomes :: for now.  */
5547       if (got_scope == std_node)
5548         got_scope = void_type_node;
5549
5550       if (got_scope)
5551         type = got_scope;
5552       else if (got_object != error_mark_node)
5553         type = got_object;
5554       
5555       if (type)
5556         {
5557           if (type == error_mark_node)
5558             return error_mark_node;
5559           if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
5560             type = TREE_TYPE (type);
5561
5562           if (TYPE_P (type))
5563             type = complete_type (type);
5564
5565           if (TREE_CODE (type) == VOID_TYPE)
5566             type = global_namespace;
5567           if (TREE_CODE (type) == NAMESPACE_DECL)
5568             {
5569               val = make_node (CPLUS_BINDING);
5570               flags |= LOOKUP_COMPLAIN;
5571               if (!qualified_lookup_using_namespace (name, type, val, flags))
5572                 return NULL_TREE;
5573               val = select_decl (val, flags);
5574             }
5575           else if (! IS_AGGR_TYPE (type)
5576                    || TREE_CODE (type) == TEMPLATE_TYPE_PARM
5577                    || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM
5578                    || TREE_CODE (type) == TYPENAME_TYPE)
5579             /* Someone else will give an error about this if needed.  */
5580             val = NULL_TREE;
5581           else if (type == current_class_type)
5582             val = IDENTIFIER_CLASS_VALUE (name);
5583           else
5584             val = lookup_member (type, name, 0, prefer_type);
5585         }
5586       else
5587         val = NULL_TREE;
5588
5589       if (got_scope)
5590         goto done;
5591       else if (got_object && val)
5592         from_obj = val;
5593     }
5594   else
5595     {
5596       flags = lookup_flags (prefer_type, namespaces_only);
5597       /* If we're not parsing, we need to complain. */
5598       flags |= LOOKUP_COMPLAIN;
5599     }
5600
5601   /* First, look in non-namespace scopes.  */
5602
5603   if (current_class_type == NULL_TREE)
5604     nonclass = 1;
5605
5606   for (t = IDENTIFIER_BINDING (name); t; t = TREE_CHAIN (t))
5607     {
5608       tree binding;
5609
5610       if (!LOCAL_BINDING_P (t) && nonclass)
5611         /* We're not looking for class-scoped bindings, so keep going.  */
5612         continue;
5613       
5614       /* If this is the kind of thing we're looking for, we're done.  */
5615       if (qualify_lookup (BINDING_VALUE (t), flags))
5616         binding = BINDING_VALUE (t);
5617       else if ((flags & LOOKUP_PREFER_TYPES) 
5618                && qualify_lookup (BINDING_TYPE (t), flags))
5619         binding = BINDING_TYPE (t);
5620       else
5621         binding = NULL_TREE;
5622
5623       if (binding
5624           && (!val || !IMPLICIT_TYPENAME_TYPE_DECL_P (binding)))
5625         {
5626           if (val_is_implicit_typename && !yylex)
5627             warn_about_implicit_typename_lookup (val, binding);
5628           val = binding;
5629           val_is_implicit_typename 
5630             = IMPLICIT_TYPENAME_TYPE_DECL_P (val);
5631           if (!val_is_implicit_typename)
5632             break;
5633         }
5634     }
5635
5636   /* Now lookup in namespace scopes.  */
5637   if (!val || val_is_implicit_typename)
5638     {
5639       t = unqualified_namespace_lookup (name, flags, 0);
5640       if (t)
5641         {
5642           if (val_is_implicit_typename && !yylex)
5643             warn_about_implicit_typename_lookup (val, t);
5644           val = t;
5645         }
5646     }
5647
5648  done:
5649   if (val)
5650     {
5651       /* This should only warn about types used in qualified-ids.  */
5652       if (from_obj && from_obj != val)
5653         {
5654           if (looking_for_typename && TREE_CODE (from_obj) == TYPE_DECL
5655               && TREE_CODE (val) == TYPE_DECL
5656               && TREE_TYPE (from_obj) != TREE_TYPE (val))
5657             {
5658               cp_pedwarn ("lookup of `%D' in the scope of `%#T' (`%#T')",
5659                           name, got_object, TREE_TYPE (from_obj));
5660               cp_pedwarn ("  does not match lookup in the current scope (`%#T')",
5661                           TREE_TYPE (val));
5662             }
5663
5664           /* We don't change val to from_obj if got_object depends on
5665              template parms because that breaks implicit typename for
5666              destructor calls.  */
5667           if (! uses_template_parms (got_object))
5668             val = from_obj;
5669         }
5670
5671       /* If we have a single function from a using decl, pull it out.  */
5672       if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
5673         val = OVL_FUNCTION (val);
5674     }
5675   else if (from_obj)
5676     val = from_obj;
5677
5678   return val;
5679 }
5680
5681 tree
5682 lookup_name_nonclass (name)
5683      tree name;
5684 {
5685   return lookup_name_real (name, 0, 1, 0);
5686 }
5687
5688 tree
5689 lookup_function_nonclass (name, args)
5690      tree name;
5691      tree args;
5692 {
5693   return lookup_arg_dependent (name, lookup_name_nonclass (name), args);
5694 }
5695
5696 tree
5697 lookup_name_namespace_only (name)
5698      tree name;
5699 {
5700   /* type-or-namespace, nonclass, namespace_only */
5701   return lookup_name_real (name, 1, 1, 1);
5702 }
5703
5704 tree
5705 lookup_name (name, prefer_type)
5706      tree name;
5707      int prefer_type;
5708 {
5709   return lookup_name_real (name, prefer_type, 0, 0);
5710 }
5711
5712 /* Similar to `lookup_name' but look only in the innermost non-class
5713    binding level.  */
5714
5715 tree
5716 lookup_name_current_level (name)
5717      tree name;
5718 {
5719   struct binding_level *b;
5720   tree t = NULL_TREE;
5721
5722   b = current_binding_level;
5723   while (b->parm_flag == 2)
5724     b = b->level_chain;
5725
5726   if (b->namespace_p)
5727     {
5728       t = IDENTIFIER_NAMESPACE_VALUE (name);
5729
5730       /* extern "C" function() */
5731       if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
5732         t = TREE_VALUE (t);
5733     }
5734   else if (IDENTIFIER_BINDING (name) 
5735            && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
5736     {
5737       while (1)
5738         {
5739           if (BINDING_LEVEL (IDENTIFIER_BINDING (name)) == b)
5740             return IDENTIFIER_VALUE (name);
5741           
5742           if (b->keep == 2)
5743             b = b->level_chain;
5744           else
5745             break;
5746         }
5747     }
5748
5749   return t;
5750 }
5751
5752 /* Like lookup_name_current_level, but for types.  */
5753
5754 tree
5755 lookup_type_current_level (name)
5756      tree name;
5757 {
5758   register tree t = NULL_TREE;
5759
5760   my_friendly_assert (! current_binding_level->namespace_p, 980716);
5761
5762   if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
5763       && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
5764     {
5765       struct binding_level *b = current_binding_level;
5766       while (1)
5767         {
5768           if (purpose_member (name, b->type_shadowed))
5769             return REAL_IDENTIFIER_TYPE_VALUE (name);
5770           if (b->keep == 2)
5771             b = b->level_chain;
5772           else
5773             break;
5774         }
5775     }
5776
5777   return t;
5778 }
5779
5780 void
5781 begin_only_namespace_names ()
5782 {
5783   only_namespace_names = 1;
5784 }
5785
5786 void
5787 end_only_namespace_names ()
5788 {
5789   only_namespace_names = 0;
5790 }
5791 \f
5792 /* Arrange for the user to get a source line number, even when the
5793    compiler is going down in flames, so that she at least has a
5794    chance of working around problems in the compiler.  We used to
5795    call error(), but that let the segmentation fault continue
5796    through; now, it's much more passive by asking them to send the
5797    maintainers mail about the problem.  */
5798
5799 static void
5800 signal_catch (sig)
5801      int sig ATTRIBUTE_UNUSED;
5802 {
5803   signal (SIGSEGV, SIG_DFL);
5804 #ifdef SIGIOT
5805   signal (SIGIOT, SIG_DFL);
5806 #endif
5807 #ifdef SIGILL
5808   signal (SIGILL, SIG_DFL);
5809 #endif
5810 #ifdef SIGABRT
5811   signal (SIGABRT, SIG_DFL);
5812 #endif
5813 #ifdef SIGBUS
5814   signal (SIGBUS, SIG_DFL);
5815 #endif
5816   my_friendly_abort (0);
5817 }
5818
5819 /* Push the declarations of builtin types into the namespace.
5820    RID_INDEX, if < RID_MAX is the index of the builtin type
5821    in the array RID_POINTERS.  NAME is the name used when looking
5822    up the builtin type.  TYPE is the _TYPE node for the builtin type.  */
5823
5824 static void
5825 record_builtin_type (rid_index, name, type)
5826      enum rid rid_index;
5827      const char *name;
5828      tree type;
5829 {
5830   tree rname = NULL_TREE, tname = NULL_TREE;
5831   tree tdecl = NULL_TREE;
5832
5833   if ((int) rid_index < (int) RID_MAX)
5834     rname = ridpointers[(int) rid_index];
5835   if (name)
5836     tname = get_identifier (name);
5837
5838   TYPE_BUILT_IN (type) = 1;
5839   
5840   if (tname)
5841     {
5842       tdecl = pushdecl (build_decl (TYPE_DECL, tname, type));
5843       set_identifier_type_value (tname, NULL_TREE);
5844       if ((int) rid_index < (int) RID_MAX)
5845         /* Built-in types live in the global namespace. */
5846         SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
5847     }
5848   if (rname != NULL_TREE)
5849     {
5850       if (tname != NULL_TREE)
5851         {
5852           set_identifier_type_value (rname, NULL_TREE);
5853           SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
5854         }
5855       else
5856         {
5857           tdecl = pushdecl (build_decl (TYPE_DECL, rname, type));
5858           set_identifier_type_value (rname, NULL_TREE);
5859         }
5860     }
5861 }
5862
5863 /* Record one of the standard Java types.
5864  * Declare it as having the given NAME.
5865  * If SIZE > 0, it is the size of one of the integral types;
5866  * otherwise it is the negative of the size of one of the other types.  */
5867
5868 static tree
5869 record_builtin_java_type (name, size)
5870      const char *name;
5871      int size;
5872 {
5873   tree type, decl;
5874   if (size > 0)
5875     type = make_signed_type (size);
5876   else if (size > -32)
5877     { /* "__java_char" or ""__java_boolean". */
5878       type = make_unsigned_type (-size);
5879       /*if (size == -1) TREE_SET_CODE (type, BOOLEAN_TYPE);*/
5880     }
5881   else
5882     { /* "__java_float" or ""__java_double". */
5883       type = make_node (REAL_TYPE);
5884       TYPE_PRECISION (type) = - size;
5885       layout_type (type);
5886     }
5887   record_builtin_type (RID_MAX, name, type);
5888   decl = TYPE_NAME (type);
5889
5890   /* Suppress generate debug symbol entries for these types,
5891      since for normal C++ they are just clutter.
5892      However, push_lang_context undoes this if extern "Java" is seen. */
5893   DECL_IGNORED_P (decl) = 1;
5894
5895   TYPE_FOR_JAVA (type) = 1;
5896   return type;
5897 }
5898
5899 /* Push a type into the namespace so that the back-ends ignore it. */
5900
5901 static void
5902 record_unknown_type (type, name)
5903      tree type;
5904      const char *name;
5905 {
5906   tree decl = pushdecl (build_decl (TYPE_DECL, get_identifier (name), type));
5907   /* Make sure the "unknown type" typedecl gets ignored for debug info.  */
5908   DECL_IGNORED_P (decl) = 1;
5909   TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
5910   TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
5911   TYPE_ALIGN (type) = 1;
5912   TYPE_MODE (type) = TYPE_MODE (void_type_node);
5913
5914
5915 /* Push overloaded decl, in global scope, with one argument so it
5916    can be used as a callback from define_function.  */
5917
5918 static void
5919 push_overloaded_decl_1 (x)
5920      tree x;
5921 {
5922   pushdecl (x);
5923 }
5924
5925 inline tree
5926 auto_function (name, type)
5927      tree name, type;
5928 {
5929   return define_function
5930     (IDENTIFIER_POINTER (name), type, push_overloaded_decl_1,
5931      IDENTIFIER_POINTER (build_decl_overload (name, TYPE_ARG_TYPES (type),
5932                                               0)));
5933 }
5934
5935 /* Create the predefined scalar types of C,
5936    and some nodes representing standard constants (0, 1, (void *)0).
5937    Initialize the global binding level.
5938    Make definitions for built-in primitive functions.  */
5939
5940 void
5941 init_decl_processing ()
5942 {
5943   tree fields[20];
5944   int wchar_type_size;
5945   tree array_domain_type;
5946
5947   /* Have to make these distinct before we try using them.  */
5948   lang_name_cplusplus = get_identifier ("C++");
5949   lang_name_c = get_identifier ("C");
5950   lang_name_java = get_identifier ("Java");
5951
5952   /* Let the back-end now how to save and restore language-specific
5953      per-function globals.  */
5954   init_lang_status = &push_cp_function_context;
5955   free_lang_status = &pop_cp_function_context;
5956   mark_lang_status = &mark_cp_function_context;
5957
5958   cp_parse_init ();
5959   init_decl2 ();
5960   init_pt ();
5961
5962   /* Create the global variables.  */
5963   push_to_top_level ();
5964
5965   /* Enter the global namespace. */
5966   my_friendly_assert (global_namespace == NULL_TREE, 375);
5967   push_namespace (get_identifier ("::"));
5968   global_namespace = current_namespace;
5969   current_lang_name = NULL_TREE;
5970
5971   if (flag_strict_prototype == 2)
5972     flag_strict_prototype = pedantic;
5973   if (! flag_permissive && ! pedantic)
5974     flag_pedantic_errors = 1;
5975
5976   strict_prototypes_lang_c = flag_strict_prototype;
5977
5978   /* Initially, C.  */
5979   current_lang_name = lang_name_c;
5980
5981   current_function_decl = NULL_TREE;
5982   current_binding_level = NULL_BINDING_LEVEL;
5983   free_binding_level = NULL_BINDING_LEVEL;
5984
5985   /* Because most segmentation signals can be traced back into user
5986      code, catch them and at least give the user a chance of working
5987      around compiler bugs.  */
5988   signal (SIGSEGV, signal_catch);
5989
5990   /* We will also catch aborts in the back-end through signal_catch and
5991      give the user a chance to see where the error might be, and to defeat
5992      aborts in the back-end when there have been errors previously in their
5993      code.  */
5994 #ifdef SIGIOT
5995   signal (SIGIOT, signal_catch);
5996 #endif
5997 #ifdef SIGILL
5998   signal (SIGILL, signal_catch);
5999 #endif
6000 #ifdef SIGABRT
6001   signal (SIGABRT, signal_catch);
6002 #endif
6003 #ifdef SIGBUS
6004   signal (SIGBUS, signal_catch);
6005 #endif
6006
6007   build_common_tree_nodes (flag_signed_char);
6008
6009   error_mark_list = build_tree_list (error_mark_node, error_mark_node);
6010   TREE_TYPE (error_mark_list) = error_mark_node;
6011
6012   /* Make the binding_level structure for global names.  */
6013   pushlevel (0);
6014   global_binding_level = current_binding_level;
6015   /* The global level is the namespace level of ::.  */
6016   NAMESPACE_LEVEL (global_namespace) = global_binding_level;
6017   declare_namespace_level ();
6018
6019   this_identifier = get_identifier (THIS_NAME);
6020   in_charge_identifier = get_identifier (IN_CHARGE_NAME);
6021   ctor_identifier = get_identifier (CTOR_NAME);
6022   dtor_identifier = get_identifier (DTOR_NAME);
6023   pfn_identifier = get_identifier (VTABLE_PFN_NAME);
6024   index_identifier = get_identifier (VTABLE_INDEX_NAME);
6025   delta_identifier = get_identifier (VTABLE_DELTA_NAME);
6026   delta2_identifier = get_identifier (VTABLE_DELTA2_NAME);
6027   pfn_or_delta2_identifier = get_identifier ("__pfn_or_delta2");
6028
6029   /* Define `int' and `char' first so that dbx will output them first.  */
6030   record_builtin_type (RID_INT, NULL_PTR, integer_type_node);
6031   record_builtin_type (RID_CHAR, "char", char_type_node);
6032
6033   /* `signed' is the same as `int' */
6034   record_builtin_type (RID_SIGNED, NULL_PTR, integer_type_node);
6035   record_builtin_type (RID_LONG, "long int", long_integer_type_node);
6036   record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
6037   record_builtin_type (RID_MAX, "long unsigned int", long_unsigned_type_node);
6038   record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
6039   record_builtin_type (RID_MAX, "long long int", long_long_integer_type_node);
6040   record_builtin_type (RID_MAX, "long long unsigned int",
6041                        long_long_unsigned_type_node);
6042   record_builtin_type (RID_MAX, "long long unsigned",
6043                        long_long_unsigned_type_node);
6044   record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
6045   record_builtin_type (RID_MAX, "short unsigned int", short_unsigned_type_node);
6046   record_builtin_type (RID_MAX, "unsigned short", short_unsigned_type_node);
6047
6048   ptrdiff_type_node
6049     = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (get_identifier (PTRDIFF_TYPE)));
6050
6051   /* Define both `signed char' and `unsigned char'.  */
6052   record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
6053   record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
6054
6055   /* `unsigned long' is the standard type for sizeof.
6056      Note that stddef.h uses `unsigned long',
6057      and this must agree, even if long and int are the same size.  */
6058   set_sizetype
6059     (TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (get_identifier (SIZE_TYPE))));
6060
6061   /* Create the widest literal types. */
6062   widest_integer_literal_type_node = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
6063   pushdecl (build_decl (TYPE_DECL, NULL_TREE, 
6064                         widest_integer_literal_type_node));
6065
6066   widest_unsigned_literal_type_node = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
6067   pushdecl (build_decl (TYPE_DECL, NULL_TREE, 
6068                         widest_unsigned_literal_type_node));
6069
6070   /* These are types that type_for_size and type_for_mode use.  */
6071   pushdecl (build_decl (TYPE_DECL, NULL_TREE, intQI_type_node));
6072   pushdecl (build_decl (TYPE_DECL, NULL_TREE, intHI_type_node));
6073   pushdecl (build_decl (TYPE_DECL, NULL_TREE, intSI_type_node));
6074   pushdecl (build_decl (TYPE_DECL, NULL_TREE, intDI_type_node));
6075 #if HOST_BITS_PER_WIDE_INT >= 64
6076   pushdecl (build_decl (TYPE_DECL, get_identifier ("__int128_t"), intTI_type_node));
6077 #endif
6078   pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intQI_type_node));
6079   pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intHI_type_node));
6080   pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intSI_type_node));
6081   pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intDI_type_node));
6082 #if HOST_BITS_PER_WIDE_INT >= 64
6083   pushdecl (build_decl (TYPE_DECL, get_identifier ("__uint128_t"), unsigned_intTI_type_node));
6084 #endif
6085
6086   build_common_tree_nodes_2 (flag_short_double);
6087
6088   java_byte_type_node = record_builtin_java_type ("__java_byte", 8);
6089   java_short_type_node = record_builtin_java_type ("__java_short", 16);
6090   java_int_type_node = record_builtin_java_type ("__java_int", 32);
6091   java_long_type_node = record_builtin_java_type ("__java_long", 64);
6092   java_float_type_node = record_builtin_java_type ("__java_float", -32);
6093   java_double_type_node = record_builtin_java_type ("__java_double", -64);
6094   java_char_type_node = record_builtin_java_type ("__java_char", -16);
6095   java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1);
6096
6097   integer_two_node = build_int_2 (2, 0);
6098   TREE_TYPE (integer_two_node) = integer_type_node;
6099   integer_three_node = build_int_2 (3, 0);
6100   TREE_TYPE (integer_three_node) = integer_type_node;
6101
6102   boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
6103   TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
6104   TYPE_MAX_VALUE (boolean_type_node) = build_int_2 (1, 0);
6105   TREE_TYPE (TYPE_MAX_VALUE (boolean_type_node)) = boolean_type_node;
6106   TYPE_PRECISION (boolean_type_node) = 1;
6107   record_builtin_type (RID_BOOL, "bool", boolean_type_node);
6108   boolean_false_node = build_int_2 (0, 0);
6109   TREE_TYPE (boolean_false_node) = boolean_type_node;
6110   boolean_true_node = build_int_2 (1, 0);
6111   TREE_TYPE (boolean_true_node) = boolean_type_node;
6112
6113   signed_size_zero_node = build_int_2 (0, 0);  record_builtin_type (RID_FLOAT, NULL_PTR, float_type_node);
6114   record_builtin_type (RID_DOUBLE, NULL_PTR, double_type_node);
6115   record_builtin_type (RID_MAX, "long double", long_double_type_node);
6116
6117   pushdecl (build_decl (TYPE_DECL, get_identifier ("complex int"),
6118                         complex_integer_type_node));
6119   pushdecl (build_decl (TYPE_DECL, get_identifier ("complex float"),
6120                         complex_float_type_node));
6121   pushdecl (build_decl (TYPE_DECL, get_identifier ("complex double"),
6122                         complex_double_type_node));
6123   pushdecl (build_decl (TYPE_DECL, get_identifier ("complex long double"),
6124                         complex_long_double_type_node));
6125
6126   TREE_TYPE (signed_size_zero_node) = make_signed_type (TYPE_PRECISION (sizetype));
6127
6128   record_builtin_type (RID_VOID, NULL_PTR, void_type_node);
6129   void_list_node = build_tree_list (NULL_TREE, void_type_node);
6130   TREE_PARMLIST (void_list_node) = 1;
6131
6132   /* Used for expressions that do nothing, but are not errors.  */
6133   void_zero_node = build_int_2 (0, 0);
6134   TREE_TYPE (void_zero_node) = void_type_node;
6135
6136   string_type_node = build_pointer_type (char_type_node);
6137   const_string_type_node
6138     = build_pointer_type (build_qualified_type (char_type_node, 
6139                                                 TYPE_QUAL_CONST));
6140   empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
6141 #if 0
6142   record_builtin_type (RID_MAX, NULL_PTR, string_type_node);
6143 #endif
6144
6145   /* Make a type to be the domain of a few array types
6146      whose domains don't really matter.
6147      200 is small enough that it always fits in size_t
6148      and large enough that it can hold most function names for the
6149      initializations of __FUNCTION__ and __PRETTY_FUNCTION__.  */
6150   array_domain_type = build_index_type (build_int_2 (200, 0));
6151
6152   /* Make a type for arrays of characters.
6153      With luck nothing will ever really depend on the length of this
6154      array type.  */
6155   char_array_type_node
6156     = build_array_type (char_type_node, array_domain_type);
6157   /* Likewise for arrays of ints.  */
6158   int_array_type_node
6159     = build_array_type (integer_type_node, array_domain_type);
6160
6161   /* This is just some anonymous class type.  Nobody should ever
6162      need to look inside this envelope.  */
6163   class_star_type_node = build_pointer_type (make_lang_type (RECORD_TYPE));
6164
6165   if (flag_huge_objects)
6166     delta_type_node = long_integer_type_node;
6167   else
6168     delta_type_node = short_integer_type_node;
6169
6170   default_function_type
6171     = build_function_type (integer_type_node, NULL_TREE);
6172
6173   ptr_type_node = build_pointer_type (void_type_node);
6174   const_ptr_type_node
6175     = build_pointer_type (build_qualified_type (void_type_node,
6176                                                 TYPE_QUAL_CONST));
6177   c_common_nodes_and_builtins (1, flag_no_builtin, flag_no_nonansi_builtin);
6178   lang_type_promotes_to = convert_type_from_ellipsis;
6179
6180   void_ftype_ptr
6181     = build_exception_variant (void_ftype_ptr, empty_except_spec);
6182
6183   /* C++ extensions */
6184
6185   unknown_type_node = make_node (UNKNOWN_TYPE);
6186   record_unknown_type (unknown_type_node, "unknown type");
6187
6188   /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node.  */
6189   TREE_TYPE (unknown_type_node) = unknown_type_node;
6190
6191   TREE_TYPE (null_node) = type_for_size (POINTER_SIZE, 0);
6192
6193   /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
6194      result.  */
6195   TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
6196   TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
6197
6198   /* This is special for C++ so functions can be overloaded.  */
6199   wchar_type_node = get_identifier (flag_short_wchar
6200                                     ? "short unsigned int"
6201                                     : WCHAR_TYPE);
6202   wchar_type_node = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (wchar_type_node));
6203   wchar_type_size = TYPE_PRECISION (wchar_type_node);
6204   signed_wchar_type_node = make_signed_type (wchar_type_size);
6205   unsigned_wchar_type_node = make_unsigned_type (wchar_type_size);
6206   wchar_type_node
6207     = TREE_UNSIGNED (wchar_type_node)
6208       ? unsigned_wchar_type_node
6209       : signed_wchar_type_node;
6210   record_builtin_type (RID_WCHAR, "__wchar_t", wchar_type_node);
6211
6212   /* Artificial declaration of wchar_t -- can be bashed */
6213   wchar_decl_node = build_decl (TYPE_DECL, get_identifier ("wchar_t"),
6214                                 wchar_type_node);
6215   pushdecl (wchar_decl_node);
6216
6217   /* This is for wide string constants.  */
6218   wchar_array_type_node
6219     = build_array_type (wchar_type_node, array_domain_type);
6220
6221   if (flag_vtable_thunks)
6222     {
6223       /* Make sure we get a unique function type, so we can give
6224          its pointer type a name.  (This wins for gdb.) */
6225       tree vfunc_type = make_node (FUNCTION_TYPE);
6226       TREE_TYPE (vfunc_type) = integer_type_node;
6227       TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
6228       layout_type (vfunc_type);
6229
6230       vtable_entry_type = build_pointer_type (vfunc_type);
6231     }
6232   else
6233     {
6234       vtable_entry_type = make_lang_type (RECORD_TYPE);
6235       fields[0] = build_lang_decl (FIELD_DECL, delta_identifier,
6236                                    delta_type_node);
6237       fields[1] = build_lang_decl (FIELD_DECL, index_identifier,
6238                                    delta_type_node);
6239       fields[2] = build_lang_decl (FIELD_DECL, pfn_identifier,
6240                                    ptr_type_node);
6241       finish_builtin_type (vtable_entry_type, VTBL_PTR_TYPE, fields, 2,
6242                            double_type_node);
6243
6244       /* Make this part of an invisible union.  */
6245       fields[3] = copy_node (fields[2]);
6246       TREE_TYPE (fields[3]) = delta_type_node;
6247       DECL_NAME (fields[3]) = delta2_identifier;
6248       DECL_MODE (fields[3]) = TYPE_MODE (delta_type_node);
6249       DECL_SIZE (fields[3]) = TYPE_SIZE (delta_type_node);
6250       TREE_UNSIGNED (fields[3]) = 0;
6251       TREE_CHAIN (fields[2]) = fields[3];
6252       vtable_entry_type = build_qualified_type (vtable_entry_type,
6253                                                 TYPE_QUAL_CONST);
6254     }
6255   record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
6256
6257   vtbl_type_node
6258     = build_cplus_array_type (vtable_entry_type, NULL_TREE);
6259   layout_type (vtbl_type_node);
6260   vtbl_type_node = build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
6261   record_builtin_type (RID_MAX, NULL_PTR, vtbl_type_node);
6262   vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
6263   layout_type (vtbl_ptr_type_node);
6264   record_builtin_type (RID_MAX, NULL_PTR, vtbl_ptr_type_node);
6265
6266   std_node = build_decl (NAMESPACE_DECL, 
6267                          get_identifier (flag_honor_std ? "fake std":"std"),
6268                          void_type_node);
6269   pushdecl (std_node);
6270
6271   global_type_node = make_node (LANG_TYPE);
6272   record_unknown_type (global_type_node, "global type");
6273
6274   /* Now, C++.  */
6275   current_lang_name = lang_name_cplusplus;
6276
6277   {
6278     tree bad_alloc_type_node, newtype, deltype;
6279     if (flag_honor_std)
6280       push_namespace (get_identifier ("std"));
6281     bad_alloc_type_node = xref_tag
6282       (class_type_node, get_identifier ("bad_alloc"), 1);
6283     if (flag_honor_std)
6284       pop_namespace ();
6285     newtype = build_exception_variant
6286       (ptr_ftype_sizetype, add_exception_specifier (NULL_TREE, bad_alloc_type_node, -1));
6287     deltype = build_exception_variant (void_ftype_ptr, empty_except_spec);
6288     auto_function (ansi_opname[(int) NEW_EXPR], newtype);
6289     auto_function (ansi_opname[(int) VEC_NEW_EXPR], newtype);
6290     global_delete_fndecl = auto_function (ansi_opname[(int) DELETE_EXPR],
6291                                           deltype);
6292     auto_function (ansi_opname[(int) VEC_DELETE_EXPR], deltype);
6293   }
6294
6295   abort_fndecl
6296     = define_function ("__pure_virtual", void_ftype, 0, 0);
6297
6298   /* Perform other language dependent initializations.  */
6299   init_class_processing ();
6300   init_init_processing ();
6301   init_search_processing ();
6302   init_rtti_processing ();
6303
6304   if (flag_exceptions)
6305     init_exception_processing ();
6306   if (flag_no_inline)
6307     {
6308       flag_inline_functions = 0;
6309     }
6310
6311   if (! supports_one_only ())
6312     flag_weak = 0;
6313
6314   /* Create the global bindings for __FUNCTION__ and __PRETTY_FUNCTION__.  */
6315   declare_function_name ();
6316
6317   /* Prepare to check format strings against argument lists.  */
6318   init_function_format_info ();
6319
6320   /* Show we use EH for cleanups.  */
6321   using_eh_for_cleanups ();
6322
6323   print_error_function = lang_print_error_function;
6324   lang_get_alias_set = &c_get_alias_set;
6325   valid_lang_attribute = cp_valid_lang_attribute;
6326
6327   /* Maintain consistency.  Perhaps we should just complain if they
6328      say -fwritable-strings?  */
6329   if (flag_writable_strings)
6330     flag_const_strings = 0;
6331
6332   /* Add GC roots for all of our global variables.  */
6333   ggc_add_tree_root (c_global_trees, sizeof c_global_trees / sizeof(tree));
6334   ggc_add_tree_root (cp_global_trees, sizeof cp_global_trees / sizeof(tree));
6335   ggc_add_tree_root (&integer_three_node, 1);
6336   ggc_add_tree_root (&integer_two_node, 1);
6337   ggc_add_tree_root (&signed_size_zero_node, 1);
6338   ggc_add_tree_root (&size_one_node, 1);
6339   ggc_add_tree_root (&size_zero_node, 1);
6340   ggc_add_root (&global_binding_level, 1, sizeof global_binding_level,
6341                 mark_binding_level);
6342   ggc_add_root (&scope_chain, 1, sizeof scope_chain, &mark_saved_scope);
6343   ggc_add_tree_root (&static_ctors, 1);
6344   ggc_add_tree_root (&static_dtors, 1);
6345   ggc_add_tree_root (&lastiddecl, 1);
6346
6347   ggc_add_tree_root (&last_function_parm_tags, 1);
6348   ggc_add_tree_root (&current_function_return_value, 1);
6349   ggc_add_tree_root (&current_function_parms, 1);
6350   ggc_add_tree_root (&current_function_parm_tags, 1);
6351   ggc_add_tree_root (&last_function_parms, 1);
6352   ggc_add_tree_root (&error_mark_list, 1);
6353
6354   ggc_add_tree_root (&global_namespace, 1);
6355   ggc_add_tree_root (&global_type_node, 1);
6356   ggc_add_tree_root (&anonymous_namespace_name, 1);
6357
6358   ggc_add_tree_root (&got_object, 1);
6359   ggc_add_tree_root (&got_scope, 1);
6360
6361   ggc_add_tree_root (&current_lang_name, 1);
6362   ggc_add_tree_root (&static_aggregates, 1);
6363 }
6364
6365 /* Function to print any language-specific context for an error message.  */
6366
6367 static void
6368 lang_print_error_function (file)
6369      const char *file;
6370 {
6371   default_print_error_function (file);
6372   maybe_print_template_context ();
6373 }
6374
6375 /* Make a definition for a builtin function named NAME and whose data type
6376    is TYPE.  TYPE should be a function type with argument types.
6377
6378    If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
6379    the name to be called if we can't opencode the function.  */
6380
6381 tree
6382 define_function (name, type, pfn, library_name)
6383      const char *name;
6384      tree type;
6385      void (*pfn) PROTO((tree));
6386      const char *library_name;
6387 {
6388   tree decl = build_lang_decl (FUNCTION_DECL, get_identifier (name), type);
6389   DECL_EXTERNAL (decl) = 1;
6390   TREE_PUBLIC (decl) = 1;
6391   DECL_ARTIFICIAL (decl) = 1;
6392
6393   my_friendly_assert (DECL_CONTEXT (decl) == NULL_TREE, 392);
6394   DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
6395
6396   /* Since `pushdecl' relies on DECL_ASSEMBLER_NAME instead of DECL_NAME,
6397      we cannot change DECL_ASSEMBLER_NAME until we have installed this
6398      function in the namespace.  */
6399   if (pfn) (*pfn) (decl);
6400   if (library_name)
6401     DECL_ASSEMBLER_NAME (decl) = get_identifier (library_name);
6402   make_function_rtl (decl);
6403   return decl;
6404 }
6405
6406
6407 /* Wrapper around define_function, for the benefit of 
6408    c_common_nodes_and_builtins.
6409    FUNCTION_CODE tells later passes how to compile calls to this function.
6410    See tree.h for its possible values.  */
6411
6412 tree
6413 builtin_function (name, type, code, class, libname)
6414      const char *name;
6415      tree type;
6416      int code;
6417      enum built_in_class class;
6418      const char *libname;
6419 {
6420   tree decl = define_function (name, type, (void (*) PROTO((tree)))pushdecl,
6421                                libname);
6422   DECL_BUILT_IN_CLASS (decl) = class;
6423   DECL_FUNCTION_CODE (decl) = code;
6424   return decl;
6425 }
6426 \f
6427 /* When we call finish_struct for an anonymous union, we create
6428    default copy constructors and such.  But, an anonymous union
6429    shouldn't have such things; this function undoes the damage to the
6430    anonymous union type T.
6431
6432    (The reason that we create the synthesized methods is that we don't
6433    distinguish `union { int i; }' from `typedef union { int i; } U'.
6434    The first is an anonymous union; the second is just an ordinary
6435    union type.)  */
6436
6437 void
6438 fixup_anonymous_aggr (t)
6439      tree t;
6440 {
6441   tree *q;
6442
6443   /* Wipe out memory of synthesized methods */
6444   TYPE_HAS_CONSTRUCTOR (t) = 0;
6445   TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
6446   TYPE_HAS_INIT_REF (t) = 0;
6447   TYPE_HAS_CONST_INIT_REF (t) = 0;
6448   TYPE_HAS_ASSIGN_REF (t) = 0;
6449   TYPE_HAS_CONST_ASSIGN_REF (t) = 0;
6450
6451   /* Splice the implicitly generated functions out of the TYPE_METHODS
6452      list.  */
6453   q = &TYPE_METHODS (t);
6454   while (*q)
6455     {
6456       if (DECL_ARTIFICIAL (*q))
6457         *q = TREE_CHAIN (*q);
6458       else
6459         q = &TREE_CHAIN (*q);
6460     }
6461
6462   /* ANSI C++ June 5 1992 WP 9.5.3.  Anonymous unions may not have
6463      function members.  */
6464   if (TYPE_METHODS (t))
6465     error ("an anonymous union cannot have function members");
6466 }
6467
6468 /* Make sure that a declaration with no declarator is well-formed, i.e.
6469    just defines a tagged type or anonymous union.
6470
6471    Returns the type defined, if any.  */
6472
6473 tree
6474 check_tag_decl (declspecs)
6475      tree declspecs;
6476 {
6477   int found_type = 0;
6478   tree ob_modifier = NULL_TREE;
6479   register tree link;
6480   register tree t = NULL_TREE;
6481
6482   for (link = declspecs; link; link = TREE_CHAIN (link))
6483     {
6484       register tree value = TREE_VALUE (link);
6485
6486       if (TYPE_P (value))
6487         {
6488           ++found_type;
6489
6490           if (IS_AGGR_TYPE (value) || TREE_CODE (value) == ENUMERAL_TYPE)
6491             {
6492               my_friendly_assert (TYPE_MAIN_DECL (value) != NULL_TREE, 261);
6493               t = value;
6494             }
6495         }
6496       else if (value == ridpointers[(int) RID_FRIEND])
6497         {
6498           if (current_class_type == NULL_TREE
6499               || current_scope () != current_class_type)
6500             ob_modifier = value;
6501         }
6502       else if (value == ridpointers[(int) RID_STATIC]
6503                || value == ridpointers[(int) RID_EXTERN]
6504                || value == ridpointers[(int) RID_AUTO]
6505                || value == ridpointers[(int) RID_REGISTER]
6506                || value == ridpointers[(int) RID_INLINE]
6507                || value == ridpointers[(int) RID_VIRTUAL]
6508                || value == ridpointers[(int) RID_CONST]
6509                || value == ridpointers[(int) RID_VOLATILE]
6510                || value == ridpointers[(int) RID_EXPLICIT])
6511         ob_modifier = value;
6512     }
6513
6514   if (found_type > 1)
6515     error ("multiple types in one declaration");
6516
6517   /* Inside a class, we might be in a friend or access declaration.
6518      Until we have a good way of detecting the latter, don't warn.  */
6519   if (t == NULL_TREE && ! current_class_type)
6520     pedwarn ("declaration does not declare anything");
6521
6522   /* Check for an anonymous union.  We're careful
6523      accessing TYPE_IDENTIFIER because some built-in types, like
6524      pointer-to-member types, do not have TYPE_NAME.  */
6525   else if (t && IS_AGGR_TYPE_CODE (TREE_CODE (t))
6526            && TYPE_NAME (t)
6527            && ANON_AGGRNAME_P (TYPE_IDENTIFIER (t)))
6528     {
6529       /* Anonymous unions are objects, so they can have specifiers.  */;
6530       SET_ANON_AGGR_TYPE_P (t);
6531
6532       if (TREE_CODE (t) != UNION_TYPE && pedantic && ! in_system_header)
6533         pedwarn ("ISO C++ prohibits anonymous structs");
6534     }
6535
6536   else if (ob_modifier)
6537     {
6538       if (ob_modifier == ridpointers[(int) RID_INLINE]
6539           || ob_modifier == ridpointers[(int) RID_VIRTUAL])
6540         cp_error ("`%D' can only be specified for functions", ob_modifier);
6541       else if (ob_modifier == ridpointers[(int) RID_FRIEND])
6542         cp_error ("`%D' can only be specified inside a class", ob_modifier);
6543       else if (ob_modifier == ridpointers[(int) RID_EXPLICIT])
6544         cp_error ("`%D' can only be specified for constructors",
6545                   ob_modifier);
6546       else
6547         cp_error ("`%D' can only be specified for objects and functions",
6548                   ob_modifier);
6549     }
6550
6551   return t;
6552 }
6553
6554 /* Called when a declaration is seen that contains no names to declare.
6555    If its type is a reference to a structure, union or enum inherited
6556    from a containing scope, shadow that tag name for the current scope
6557    with a forward reference.
6558    If its type defines a new named structure or union
6559    or defines an enum, it is valid but we need not do anything here.
6560    Otherwise, it is an error.
6561
6562    C++: may have to grok the declspecs to learn about static,
6563    complain for anonymous unions.  */
6564
6565 void
6566 shadow_tag (declspecs)
6567      tree declspecs;
6568 {
6569   tree t = check_tag_decl (declspecs);
6570
6571   if (t)
6572     maybe_process_partial_specialization (t);
6573
6574   /* This is where the variables in an anonymous union are
6575      declared.  An anonymous union declaration looks like:
6576      union { ... } ;
6577      because there is no declarator after the union, the parser
6578      sends that declaration here.  */
6579   if (t && ANON_AGGR_TYPE_P (t))
6580     {
6581       fixup_anonymous_aggr (t);
6582
6583       if (TYPE_FIELDS (t))
6584         {
6585           tree decl = grokdeclarator (NULL_TREE, declspecs, NORMAL, 0,
6586                                       NULL_TREE);
6587           finish_anon_union (decl);
6588         }
6589     }
6590 }
6591 \f
6592 /* Decode a "typename", such as "int **", returning a ..._TYPE node.  */
6593
6594 tree
6595 groktypename (typename)
6596      tree typename;
6597 {
6598   if (TREE_CODE (typename) != TREE_LIST)
6599     return typename;
6600   return grokdeclarator (TREE_VALUE (typename),
6601                          TREE_PURPOSE (typename),
6602                          TYPENAME, 0, NULL_TREE);
6603 }
6604
6605 /* Decode a declarator in an ordinary declaration or data definition.
6606    This is called as soon as the type information and variable name
6607    have been parsed, before parsing the initializer if any.
6608    Here we create the ..._DECL node, fill in its type,
6609    and put it on the list of decls for the current context.
6610    The ..._DECL node is returned as the value.
6611
6612    Exception: for arrays where the length is not specified,
6613    the type is left null, to be filled in by `cp_finish_decl'.
6614
6615    Function definitions do not come here; they go to start_function
6616    instead.  However, external and forward declarations of functions
6617    do go through here.  Structure field declarations are done by
6618    grokfield and not through here.  */
6619
6620 /* Set this to zero to debug not using the temporary obstack
6621    to parse initializers.  */
6622 int debug_temp_inits = 1;
6623
6624 tree
6625 start_decl (declarator, declspecs, initialized, attributes, prefix_attributes)
6626      tree declarator, declspecs;
6627      int initialized;
6628      tree attributes, prefix_attributes;
6629 {
6630   register tree decl;
6631   register tree type, tem;
6632   tree context;
6633   extern int have_extern_spec;
6634   extern int used_extern_spec;
6635   tree attrlist;
6636
6637 #if 0
6638   /* See code below that used this.  */
6639   int init_written = initialized;
6640 #endif
6641
6642   /* This should only be done once on the top most decl.  */
6643   if (have_extern_spec && !used_extern_spec)
6644     {
6645       declspecs = decl_tree_cons (NULL_TREE, get_identifier ("extern"),
6646                                   declspecs);
6647       used_extern_spec = 1;
6648     }
6649
6650   if (attributes || prefix_attributes)
6651     attrlist = build_scratch_list (attributes, prefix_attributes);
6652   else
6653     attrlist = NULL_TREE;
6654
6655   decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
6656                          attrlist);
6657                          
6658   if (decl == NULL_TREE || TREE_CODE (decl) == VOID_TYPE)
6659     return NULL_TREE;
6660
6661   type = TREE_TYPE (decl);
6662
6663   if (type == error_mark_node)
6664     return NULL_TREE;
6665
6666   context
6667     = (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
6668       ? DECL_CLASS_CONTEXT (decl)
6669       : DECL_CONTEXT (decl);
6670
6671   if (initialized && context && TREE_CODE (context) == NAMESPACE_DECL
6672       && context != current_namespace && TREE_CODE (decl) == VAR_DECL)
6673     {
6674       /* When parsing the initializer, lookup should use the object's
6675          namespace. */
6676       push_decl_namespace (context);
6677     }
6678
6679   /* We are only interested in class contexts, later. */
6680   if (context && TREE_CODE (context) == NAMESPACE_DECL)
6681     context = NULL_TREE;
6682
6683   if (initialized)
6684     /* Is it valid for this decl to have an initializer at all?
6685        If not, set INITIALIZED to zero, which will indirectly
6686        tell `cp_finish_decl' to ignore the initializer once it is parsed.  */
6687     switch (TREE_CODE (decl))
6688       {
6689       case TYPE_DECL:
6690         /* typedef foo = bar  means give foo the same type as bar.
6691            We haven't parsed bar yet, so `cp_finish_decl' will fix that up.
6692            Any other case of an initialization in a TYPE_DECL is an error.  */
6693         if (pedantic || list_length (declspecs) > 1)
6694           {
6695             cp_error ("typedef `%D' is initialized", decl);
6696             initialized = 0;
6697           }
6698         break;
6699
6700       case FUNCTION_DECL:
6701         cp_error ("function `%#D' is initialized like a variable", decl);
6702         initialized = 0;
6703         break;
6704
6705       default:
6706         break;
6707       }
6708
6709   if (initialized)
6710     {
6711       if (! toplevel_bindings_p ()
6712           && DECL_EXTERNAL (decl))
6713         cp_warning ("declaration of `%#D' has `extern' and is initialized",
6714                     decl);
6715       DECL_EXTERNAL (decl) = 0;
6716       if (toplevel_bindings_p ())
6717         TREE_STATIC (decl) = 1;
6718
6719       /* Tell `pushdecl' this is an initialized decl
6720          even though we don't yet have the initializer expression.
6721          Also tell `cp_finish_decl' it may store the real initializer.  */
6722       DECL_INITIAL (decl) = error_mark_node;
6723     }
6724
6725 #ifdef SET_DEFAULT_DECL_ATTRIBUTES
6726   SET_DEFAULT_DECL_ATTRIBUTES (decl, attributes);
6727 #endif
6728   
6729   /* Set attributes here so if duplicate decl, will have proper attributes.  */
6730   cplus_decl_attributes (decl, attributes, prefix_attributes);
6731
6732   if (context && TYPE_SIZE (complete_type (context)) != NULL_TREE)
6733     {
6734       push_nested_class (context, 2);
6735
6736       if (TREE_CODE (decl) == VAR_DECL)
6737         {
6738           tree field = lookup_field (context, DECL_NAME (decl), 0, 0);
6739           if (field == NULL_TREE || TREE_CODE (field) != VAR_DECL)
6740             cp_error ("`%#D' is not a static member of `%#T'", decl, context);
6741           else
6742             {
6743               if (DECL_CONTEXT (field) != context)
6744                 {
6745                   cp_pedwarn ("ANSI C++ does not permit `%T::%D' to be defined as `%T::%D'",
6746                               DECL_CONTEXT (field), DECL_NAME (decl),
6747                               context, DECL_NAME (decl));
6748                   DECL_CONTEXT (decl) = DECL_CONTEXT (field);
6749                 }
6750               /* Static data member are tricky; an in-class initialization
6751                  still doesn't provide a definition, so the in-class
6752                  declaration will have DECL_EXTERNAL set, but will have an
6753                  initialization.  Thus, duplicate_decls won't warn
6754                  about this situation, and so we check here.  */
6755               if (DECL_INITIAL (decl) && DECL_INITIAL (field))
6756                 cp_error ("duplicate initialization of %D", decl);
6757               if (duplicate_decls (decl, field))
6758                 decl = field;
6759             }
6760         }
6761       else
6762         {
6763           tree field = check_classfn (context, decl);
6764           if (field && duplicate_decls (decl, field))
6765             decl = field;
6766         }
6767
6768       /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set.  */
6769       DECL_IN_AGGR_P (decl) = 0;
6770       if ((DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl)) 
6771           || CLASSTYPE_USE_TEMPLATE (context))
6772         {
6773           SET_DECL_TEMPLATE_SPECIALIZATION (decl);
6774           /* [temp.expl.spec] An explicit specialization of a static data
6775              member of a template is a definition if the declaration
6776              includes an initializer; otherwise, it is a declaration.
6777
6778              We check for processing_specialization so this only applies
6779              to the new specialization syntax.  */
6780           if (DECL_INITIAL (decl) == NULL_TREE && processing_specialization)
6781             DECL_EXTERNAL (decl) = 1;
6782         }
6783
6784       if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl))
6785         cp_pedwarn ("declaration of `%#D' outside of class is not definition",
6786                     decl);
6787     }
6788
6789   /* Enter this declaration into the symbol table.  */
6790   tem = maybe_push_decl (decl);
6791
6792   if (processing_template_decl)
6793     tem = push_template_decl (tem);
6794
6795 #if ! defined (ASM_OUTPUT_BSS) && ! defined (ASM_OUTPUT_ALIGNED_BSS)
6796   /* Tell the back-end to use or not use .common as appropriate.  If we say
6797      -fconserve-space, we want this to save .data space, at the expense of
6798      wrong semantics.  If we say -fno-conserve-space, we want this to
6799      produce errors about redefs; to do this we force variables into the
6800      data segment.  */
6801   DECL_COMMON (tem) = flag_conserve_space || ! TREE_PUBLIC (tem);
6802 #endif
6803   
6804   if (! processing_template_decl)
6805     start_decl_1 (tem);
6806
6807   return tem;
6808 }
6809
6810 void
6811 start_decl_1 (decl)
6812      tree decl;
6813 {
6814   tree type = TREE_TYPE (decl);
6815   int initialized = (DECL_INITIAL (decl) != NULL_TREE);
6816
6817   if (type == error_mark_node)
6818     return;
6819
6820   /* If this type of object needs a cleanup, but we're not allowed to
6821      add any more objects with cleanups to the current scope, create a
6822      new binding level.  */
6823   if (TYPE_NEEDS_DESTRUCTOR (type)
6824       && current_binding_level->more_cleanups_ok == 0)
6825     {
6826       keep_next_level (2);
6827       pushlevel (1);
6828       clear_last_expr ();
6829       add_scope_stmt (/*begin_p=*/1, /*partial_p=*/1);
6830     }
6831
6832   if (initialized)
6833     /* Is it valid for this decl to have an initializer at all?
6834        If not, set INITIALIZED to zero, which will indirectly
6835        tell `cp_finish_decl' to ignore the initializer once it is parsed.  */
6836     {
6837       /* Don't allow initializations for incomplete types except for
6838          arrays which might be completed by the initialization.  */
6839       if (TYPE_SIZE (complete_type (type)) != NULL_TREE)
6840         ;                       /* A complete type is ok.  */
6841       else if (TREE_CODE (type) != ARRAY_TYPE)
6842         {
6843           cp_error ("variable `%#D' has initializer but incomplete type",
6844                     decl);
6845           initialized = 0;
6846           type = TREE_TYPE (decl) = error_mark_node;
6847         }
6848       else if (TYPE_SIZE (complete_type (TREE_TYPE (type))) == NULL_TREE)
6849         {
6850           if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
6851             cp_error ("elements of array `%#D' have incomplete type", decl);
6852           /* else we already gave an error in start_decl.  */
6853           initialized = 0;
6854         }
6855     }
6856
6857   if (!initialized
6858       && TREE_CODE (decl) != TYPE_DECL
6859       && TREE_CODE (decl) != TEMPLATE_DECL
6860       && IS_AGGR_TYPE (type) && ! DECL_EXTERNAL (decl))
6861     {
6862       if ((! processing_template_decl || ! uses_template_parms (type))
6863           && TYPE_SIZE (complete_type (type)) == NULL_TREE)
6864         {
6865           cp_error ("aggregate `%#D' has incomplete type and cannot be initialized",
6866                  decl);
6867           /* Change the type so that assemble_variable will give
6868              DECL an rtl we can live with: (mem (const_int 0)).  */
6869           type = TREE_TYPE (decl) = error_mark_node;
6870         }
6871       else
6872         {
6873           /* If any base type in the hierarchy of TYPE needs a constructor,
6874              then we set initialized to 1.  This way any nodes which are
6875              created for the purposes of initializing this aggregate
6876              will live as long as it does.  This is necessary for global
6877              aggregates which do not have their initializers processed until
6878              the end of the file.  */
6879           initialized = TYPE_NEEDS_CONSTRUCTING (type);
6880         }
6881     }
6882
6883   if (! initialized)
6884     DECL_INITIAL (decl) = NULL_TREE;
6885 }
6886
6887 /* Handle initialization of references.
6888    These three arguments are from `cp_finish_decl', and have the
6889    same meaning here that they do there.
6890
6891    Quotes on semantics can be found in ARM 8.4.3.  */
6892
6893 static void
6894 grok_reference_init (decl, type, init)
6895      tree decl, type, init;
6896 {
6897   tree tmp;
6898
6899   if (init == NULL_TREE)
6900     {
6901       if ((DECL_LANG_SPECIFIC (decl) == 0
6902            || DECL_IN_AGGR_P (decl) == 0)
6903           && ! DECL_THIS_EXTERN (decl))
6904         cp_error ("`%D' declared as reference but not initialized", decl);
6905       return;
6906     }
6907
6908   if (init == error_mark_node)
6909     return;
6910
6911   if (TREE_CODE (init) == CONSTRUCTOR)
6912     {
6913       cp_error ("ANSI C++ forbids use of initializer list to initialize reference `%D'", decl);
6914       return;
6915     }
6916
6917   if (TREE_CODE (init) == TREE_LIST)
6918     init = build_compound_expr (init);
6919
6920   if (TREE_CODE (TREE_TYPE (init)) == REFERENCE_TYPE)
6921     init = convert_from_reference (init);
6922
6923   if (TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE
6924       && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
6925     {
6926       /* Note: default conversion is only called in very special cases.  */
6927       init = default_conversion (init);
6928     }
6929   
6930   /* Convert INIT to the reference type TYPE.  This may involve the
6931      creation of a temporary, whose lifetime must be the same as that
6932      of the reference.  If so, a DECL_STMT for the temporary will be
6933      added just after the DECL_STMT for DECL.  That's why we don't set
6934      DECL_INITIAL for local references (instead assigning to them
6935      explicitly); we need to allow the temporary to be initialized
6936      first.  */
6937   tmp = convert_to_reference
6938     (type, init, CONV_IMPLICIT,
6939      LOOKUP_SPECULATIVELY|LOOKUP_NORMAL|DIRECT_BIND, decl);
6940
6941   if (tmp == error_mark_node)
6942     return;
6943   else if (tmp != NULL_TREE)
6944     {
6945       init = tmp;
6946       tmp = save_expr (tmp);
6947       if (building_stmt_tree ())
6948         {
6949           /* Initialize the declaration.  */
6950           tmp = build (INIT_EXPR, TREE_TYPE (decl), decl, tmp);
6951           finish_expr_stmt (tmp);
6952         }
6953       else
6954         DECL_INITIAL (decl) = tmp;
6955     }
6956   else
6957     {
6958       cp_error ("cannot initialize `%T' from `%T'", type, TREE_TYPE (init));
6959       return;
6960     }
6961
6962   if (TREE_STATIC (decl) && ! TREE_CONSTANT (DECL_INITIAL (decl)))
6963     {
6964       expand_static_init (decl, DECL_INITIAL (decl));
6965       DECL_INITIAL (decl) = NULL_TREE;
6966     }
6967   return;
6968 }
6969
6970 /* Fill in DECL_INITIAL with some magical value to prevent expand_decl from
6971    mucking with forces it does not comprehend (i.e. initialization with a
6972    constructor).  If we are at global scope and won't go into COMMON, fill
6973    it in with a dummy CONSTRUCTOR to force the variable into .data;
6974    otherwise we can use error_mark_node.  */
6975
6976 static tree
6977 obscure_complex_init (decl, init)
6978      tree decl, init;
6979 {
6980   if (! flag_no_inline && TREE_STATIC (decl))
6981     {
6982       if (extract_init (decl, init))
6983         return NULL_TREE;
6984     }
6985
6986 #if ! defined (ASM_OUTPUT_BSS) && ! defined (ASM_OUTPUT_ALIGNED_BSS)
6987   if (toplevel_bindings_p () && ! DECL_COMMON (decl))
6988     DECL_INITIAL (decl) = build (CONSTRUCTOR, TREE_TYPE (decl), NULL_TREE,
6989                                  NULL_TREE);
6990   else
6991 #endif
6992     DECL_INITIAL (decl) = error_mark_node;
6993
6994   return init;
6995 }
6996
6997 /* When parsing `int a[] = {1, 2};' we don't know the size of the
6998    array until we finish parsing the initializer.  If that's the
6999    situation we're in, update DECL accordingly.  */
7000
7001 static void
7002 maybe_deduce_size_from_array_init (decl, init)
7003      tree decl;
7004      tree init;
7005 {
7006   tree type = TREE_TYPE (decl);
7007
7008   if (TREE_CODE (type) == ARRAY_TYPE
7009       && TYPE_DOMAIN (type) == NULL_TREE
7010       && TREE_CODE (decl) != TYPE_DECL)
7011     {
7012       int do_default
7013         = (TREE_STATIC (decl)
7014            /* Even if pedantic, an external linkage array
7015               may have incomplete type at first.  */
7016            ? pedantic && ! DECL_EXTERNAL (decl)
7017            : !DECL_EXTERNAL (decl));
7018       tree initializer = init ? init : DECL_INITIAL (decl);
7019       int failure = complete_array_type (type, initializer, do_default);
7020
7021       if (failure == 1)
7022         cp_error ("initializer fails to determine size of `%D'", decl);
7023
7024       if (failure == 2)
7025         {
7026           if (do_default)
7027             cp_error ("array size missing in `%D'", decl);
7028           /* If a `static' var's size isn't known, make it extern as
7029              well as static, so it does not get allocated.  If it's not
7030              `static', then don't mark it extern; finish_incomplete_decl
7031              will give it a default size and it will get allocated.  */
7032           else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
7033             DECL_EXTERNAL (decl) = 1;
7034         }
7035
7036       if (pedantic && TYPE_DOMAIN (type) != NULL_TREE
7037           && tree_int_cst_lt (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
7038                               integer_zero_node))
7039         cp_error ("zero-size array `%D'", decl);
7040
7041       layout_decl (decl, 0);
7042     }
7043 }
7044
7045 /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
7046    any appropriate error messages regarding the layout.  */
7047
7048 static void
7049 layout_var_decl (decl)
7050      tree decl;
7051 {
7052   tree type = TREE_TYPE (decl);
7053 #if 0
7054   tree ttype = target_type (type);
7055 #endif
7056
7057   /* If we haven't already layed out this declaration, do so now.
7058      Note that we must not call complete type for an external object
7059      because it's type might involve templates that we are not
7060      supposed to isntantiate yet.  (And it's perfectly legal to say 
7061      `extern X x' for some incomplete type `X'.)  */
7062   if (!DECL_EXTERNAL (decl))
7063     complete_type (type);
7064   if (!DECL_SIZE (decl)&& TYPE_SIZE (type))
7065     layout_decl (decl, 0);
7066
7067   if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
7068     {
7069       /* An automatic variable with an incomplete type: that is an error.
7070          Don't talk about array types here, since we took care of that
7071          message in grokdeclarator.  */
7072       cp_error ("storage size of `%D' isn't known", decl);
7073       TREE_TYPE (decl) = error_mark_node;
7074     }
7075 #if 0
7076   /* Keep this code around in case we later want to control debug info
7077      based on whether a type is "used".  (jason 1999-11-11) */
7078
7079   else if (!DECL_EXTERNAL (decl) && IS_AGGR_TYPE (ttype))
7080     /* Let debugger know it should output info for this type.  */
7081     note_debug_info_needed (ttype);
7082
7083   if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
7084     note_debug_info_needed (DECL_CONTEXT (decl));
7085 #endif
7086
7087   if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
7088       && DECL_SIZE (decl) != NULL_TREE
7089       && ! TREE_CONSTANT (DECL_SIZE (decl)))
7090     {
7091       if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
7092         constant_expression_warning (DECL_SIZE (decl));
7093       else
7094         cp_error ("storage size of `%D' isn't constant", decl);
7095     }
7096 }
7097
7098 /* If a local static variable is declared in an inline function, or if
7099    we have a weak definition, we must endeavor to create only one
7100    instance of the variable at link-time.  */
7101
7102 static void
7103 maybe_commonize_var (decl)
7104      tree decl;
7105 {
7106   /* Static data in a function with comdat linkage also has comdat
7107      linkage.  */
7108   if (TREE_STATIC (decl)
7109       /* Don't mess with __FUNCTION__.  */
7110       && ! TREE_ASM_WRITTEN (decl)
7111       && current_function_decl
7112       && DECL_CONTEXT (decl) == current_function_decl
7113       && (DECL_THIS_INLINE (current_function_decl)
7114           || DECL_TEMPLATE_INSTANTIATION (current_function_decl))
7115       && TREE_PUBLIC (current_function_decl))
7116     {
7117       /* Rather than try to get this right with inlining, we suppress
7118          inlining of such functions.  */
7119       current_function_cannot_inline
7120         = "function with static variable cannot be inline";
7121
7122       /* If flag_weak, we don't need to mess with this, as we can just
7123          make the function weak, and let it refer to its unique local
7124          copy.  This works because we don't allow the function to be
7125          inlined.  */
7126       if (! flag_weak)
7127         {
7128           if (DECL_INTERFACE_KNOWN (current_function_decl))
7129             {
7130               TREE_PUBLIC (decl) = 1;
7131               DECL_EXTERNAL (decl) = DECL_EXTERNAL (current_function_decl);
7132             }
7133           else if (DECL_INITIAL (decl) == NULL_TREE
7134                    || DECL_INITIAL (decl) == error_mark_node)
7135             {
7136               TREE_PUBLIC (decl) = 1;
7137               DECL_COMMON (decl) = 1;
7138             }
7139           /* else we lose. We can only do this if we can use common,
7140              which we can't if it has been initialized.  */
7141
7142           if (TREE_PUBLIC (decl))
7143             DECL_ASSEMBLER_NAME (decl)
7144               = build_static_name (current_function_decl, DECL_NAME (decl));
7145           else if (! DECL_ARTIFICIAL (decl))
7146             {
7147               cp_warning_at ("sorry: semantics of inline function static data `%#D' are wrong (you'll wind up with multiple copies)", decl);
7148               cp_warning_at ("  you can work around this by removing the initializer", decl);
7149             }
7150         }
7151     }
7152   else if (DECL_LANG_SPECIFIC (decl) && DECL_COMDAT (decl))
7153     /* Set it up again; we might have set DECL_INITIAL since the last
7154        time.  */
7155     comdat_linkage (decl);
7156 }
7157
7158 /* Issue an error message if DECL is an uninitialized const variable.  */
7159
7160 static void
7161 check_for_uninitialized_const_var (decl)
7162      tree decl;
7163 {
7164   tree type = TREE_TYPE (decl);
7165
7166   /* ``Unless explicitly declared extern, a const object does not have
7167      external linkage and must be initialized. ($8.4; $12.1)'' ARM
7168      7.1.6 */
7169   if (TREE_CODE (decl) == VAR_DECL
7170       && TREE_CODE (type) != REFERENCE_TYPE
7171       && CP_TYPE_CONST_P (type)
7172       && !TYPE_NEEDS_CONSTRUCTING (type)
7173       && !DECL_INITIAL (decl))
7174     cp_error ("uninitialized const `%D'", decl);
7175 }
7176
7177 /* Verify INIT (the initializer for DECL), and record the
7178    initialization in DECL_INITIAL, if appropriate.  Returns a new
7179    value for INIT.  */
7180
7181 static tree
7182 check_initializer (decl, init)
7183      tree decl;
7184      tree init;
7185 {
7186   tree type;
7187
7188   if (TREE_CODE (decl) == FIELD_DECL)
7189     return init;
7190
7191   type = TREE_TYPE (decl);
7192
7193   /* If `start_decl' didn't like having an initialization, ignore it now.  */
7194   if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
7195     init = NULL_TREE;
7196
7197   /* Check the initializer.  */
7198   if (init)
7199     {
7200       /* Things that are going to be initialized need to have complete
7201          type.  */
7202       TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
7203
7204       if (type == error_mark_node)
7205         /* We will have already complained.  */
7206         init = NULL_TREE;
7207       else if (TYPE_SIZE (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
7208         {
7209           cp_error ("variable-sized object `%D' may not be initialized", decl);
7210           init = NULL_TREE;
7211         }
7212       else if (TREE_CODE (type) == ARRAY_TYPE
7213                && !TYPE_SIZE (TREE_TYPE (type)))
7214         {
7215           cp_error ("elements of array `%#D' have incomplete type", decl);
7216           init = NULL_TREE;
7217         }
7218       else if (!TYPE_SIZE (type))
7219         {
7220           cp_error ("`%D' has incomplete type", decl);
7221           TREE_TYPE (decl) = error_mark_node;
7222           init = NULL_TREE;
7223         }
7224     }
7225
7226   if (TREE_CODE (decl) == CONST_DECL)
7227     {
7228       my_friendly_assert (TREE_CODE (decl) != REFERENCE_TYPE, 148);
7229
7230       DECL_INITIAL (decl) = init;
7231
7232       /* This will keep us from needing to worry about our obstacks.  */
7233       my_friendly_assert (init != NULL_TREE, 149);
7234       init = NULL_TREE;
7235     }
7236   else if (!DECL_EXTERNAL (decl) && TREE_CODE (type) == REFERENCE_TYPE)
7237     {
7238       if (TREE_STATIC (decl))
7239         make_decl_rtl (decl, NULL_PTR, toplevel_bindings_p ());
7240       grok_reference_init (decl, type, init);
7241       init = NULL_TREE;
7242     }
7243   else if (init)
7244     {
7245       if (TYPE_HAS_CONSTRUCTOR (type) || TYPE_NEEDS_CONSTRUCTING (type))
7246         {
7247           if (TREE_CODE (type) == ARRAY_TYPE)
7248             init = digest_init (type, init, (tree *) 0);
7249           else if (TREE_CODE (init) == CONSTRUCTOR
7250                    && TREE_HAS_CONSTRUCTOR (init))
7251             {
7252               if (TYPE_NON_AGGREGATE_CLASS (type))
7253                 {
7254                   cp_error ("`%D' must be initialized by constructor, not by `{...}'",
7255                             decl);
7256                   init = error_mark_node;
7257                 }
7258               else
7259                 goto dont_use_constructor;
7260             }
7261         }
7262       else
7263         {
7264         dont_use_constructor:
7265           if (TREE_CODE (init) != TREE_VEC)
7266             init = store_init_value (decl, init);
7267         }
7268
7269       if (init)
7270         /* We must hide the initializer so that expand_decl
7271            won't try to do something it does not understand.  */
7272         init = obscure_complex_init (decl, init);
7273     }
7274   else if (DECL_EXTERNAL (decl))
7275     ;
7276   else if (TREE_CODE_CLASS (TREE_CODE (type)) == 't'
7277            && (IS_AGGR_TYPE (type) || TYPE_NEEDS_CONSTRUCTING (type)))
7278     {
7279       tree core_type = strip_array_types (type);
7280
7281       if (! TYPE_NEEDS_CONSTRUCTING (core_type))
7282         {
7283           if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type))
7284             cp_error ("structure `%D' with uninitialized const members", decl);
7285           if (CLASSTYPE_REF_FIELDS_NEED_INIT (core_type))
7286             cp_error ("structure `%D' with uninitialized reference members",
7287                       decl);
7288         }
7289
7290       check_for_uninitialized_const_var (decl);
7291
7292       if (TYPE_SIZE (type) != NULL_TREE
7293           && TYPE_NEEDS_CONSTRUCTING (type))
7294         init = obscure_complex_init (decl, NULL_TREE);
7295
7296     }
7297   else
7298     check_for_uninitialized_const_var (decl);
7299   
7300   return init;
7301 }
7302
7303 /* If DECL is not a local variable, give it RTL.  */
7304
7305 static void
7306 make_rtl_for_nonlocal_decl (decl, init, asmspec)
7307      tree decl;
7308      tree init;
7309      const char *asmspec;
7310 {
7311   int toplev;
7312   tree type;
7313
7314   type = TREE_TYPE (decl);
7315   toplev = toplevel_bindings_p ();
7316
7317   /* Handle non-variables up front.  */
7318   if (TREE_CODE (decl) != VAR_DECL)
7319     {
7320       rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
7321       return;
7322     }
7323
7324   /* Set the DECL_ASSEMBLER_NAME for the variable.  */
7325   if (asmspec)
7326     DECL_ASSEMBLER_NAME (decl) = get_identifier (asmspec);
7327
7328   if (DECL_VIRTUAL_P (decl))
7329     make_decl_rtl (decl, NULL_PTR, toplev);
7330   else if (TREE_READONLY (decl)
7331            && DECL_INITIAL (decl) != NULL_TREE
7332            && DECL_INITIAL (decl) != error_mark_node
7333            && ! EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
7334     {
7335       DECL_INITIAL (decl) = save_expr (DECL_INITIAL (decl));
7336
7337       if (toplev && ! TREE_PUBLIC (decl))
7338         {
7339           /* If this is a static const, change its apparent linkage
7340              if it belongs to a #pragma interface.  */
7341           if (!interface_unknown)
7342             {
7343               TREE_PUBLIC (decl) = 1;
7344               DECL_EXTERNAL (decl) = interface_only;
7345             }
7346           make_decl_rtl (decl, asmspec, toplev);
7347         }
7348       else if (toplev)
7349         rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
7350     }
7351   else if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
7352     {
7353       my_friendly_assert (TREE_STATIC (decl), 19990828);
7354
7355       if (init == NULL_TREE
7356 #ifdef DEFAULT_STATIC_DEFS
7357           /* If this code is dead, then users must
7358              explicitly declare static member variables
7359              outside the class def'n as well.  */
7360           && TYPE_NEEDS_CONSTRUCTING (type)
7361 #endif
7362           )
7363         {
7364           DECL_EXTERNAL (decl) = 1;
7365           make_decl_rtl (decl, asmspec, 1);
7366         }
7367       else
7368         rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
7369     }
7370   else if (TREE_CODE (CP_DECL_CONTEXT (decl)) == NAMESPACE_DECL
7371            || (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl)))
7372     rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
7373 }
7374
7375 /* Create RTL for the local static variable DECL.  */
7376
7377 void
7378 make_rtl_for_local_static (decl)
7379      tree decl;
7380 {
7381   tree type = TREE_TYPE (decl);
7382   const char *asmspec = NULL;
7383
7384   /* If we inlined this variable, we could see it's declaration
7385      again.  */
7386   if (DECL_RTL (decl))
7387     return;
7388
7389   if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
7390     {
7391       /* The only way this situaton can occur is if the
7392          user specified a name for this DECL using the
7393          `attribute' syntax.  */
7394       asmspec = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
7395       DECL_ASSEMBLER_NAME (decl) = DECL_NAME (decl);
7396     }
7397
7398   rest_of_decl_compilation (decl, asmspec, /*top_level=*/0, /*at_end=*/0);
7399 }
7400
7401 /* The old ARM scoping rules injected variables declared in the
7402    initialization statement of a for-statement into the surrounding
7403    scope.  We support this usage, in order to be backward-compatible.
7404    DECL is a just-declared VAR_DECL; if necessary inject its
7405    declaration into the surrounding scope.  */
7406
7407 void
7408 maybe_inject_for_scope_var (decl)
7409      tree decl;
7410 {
7411   if (current_binding_level->is_for_scope)
7412     {
7413       struct binding_level *outer 
7414         = current_binding_level->level_chain;
7415
7416       /* Check to see if the same name is already bound at the outer
7417          level, either because it was directly declared, or because a
7418          dead for-decl got preserved.  In either case, the code would
7419          not have been valid under the ARM scope rules, so clear
7420          is_for_scope for the current_binding_level.
7421
7422          Otherwise, we need to preserve the temp slot for decl to last
7423          into the outer binding level.  */
7424
7425       tree outer_binding 
7426         = TREE_CHAIN (IDENTIFIER_BINDING (DECL_NAME (decl)));
7427               
7428       if (outer_binding && BINDING_LEVEL (outer_binding) == outer
7429           && (TREE_CODE (BINDING_VALUE (outer_binding)) 
7430               == VAR_DECL)
7431           && DECL_DEAD_FOR_LOCAL (BINDING_VALUE (outer_binding)))
7432         {
7433           BINDING_VALUE (outer_binding)
7434             = DECL_SHADOWED_FOR_VAR (BINDING_VALUE (outer_binding));
7435           current_binding_level->is_for_scope = 0;
7436         }
7437       else if (DECL_IN_MEMORY_P (decl))
7438         preserve_temp_slots (DECL_RTL (decl));
7439     }
7440 }
7441
7442 /* Generate code to initialize DECL (a local variable).  */
7443
7444 void
7445 initialize_local_var (decl, init, flags)
7446      tree decl;
7447      tree init;
7448      int flags;
7449 {
7450   tree type = TREE_TYPE (decl);
7451
7452   /* If the type is bogus, don't bother initializing the variable.  */
7453   if (type == error_mark_node)
7454     return;
7455
7456   if (DECL_SIZE (decl) == NULL_TREE && !TREE_STATIC (decl))
7457     {
7458       /* If we used it already as memory, it must stay in memory.  */
7459       DECL_INITIAL (decl) = NULL_TREE;
7460       TREE_ADDRESSABLE (decl) = TREE_USED (decl);
7461     }
7462
7463   /* Local statics are handled differently from ordinary automatic
7464      variables.  */
7465   if (TREE_STATIC (decl))
7466     {
7467       if (TYPE_NEEDS_CONSTRUCTING (type) || init != NULL_TREE
7468           || TYPE_NEEDS_DESTRUCTOR (type))
7469         expand_static_init (decl, init);
7470       return;
7471     }
7472
7473   if (DECL_SIZE (decl) && type != error_mark_node)
7474     {
7475       int already_used;
7476   
7477       /* Compute and store the initial value.  */
7478       already_used = TREE_USED (decl) || TREE_USED (type);
7479
7480       if (init || TYPE_NEEDS_CONSTRUCTING (type))
7481         {
7482           int saved_stmts_are_full_exprs_p;
7483
7484           emit_line_note (DECL_SOURCE_FILE (decl),
7485                           DECL_SOURCE_LINE (decl));
7486           saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p;
7487           stmts_are_full_exprs_p = 1;
7488           finish_expr_stmt (build_aggr_init (decl, init, flags));
7489           stmts_are_full_exprs_p = saved_stmts_are_full_exprs_p;
7490         }
7491
7492       /* Set this to 0 so we can tell whether an aggregate which was
7493          initialized was ever used.  Don't do this if it has a
7494          destructor, so we don't complain about the 'resource
7495          allocation is initialization' idiom.  Now set
7496          attribute((unused)) on types so decls of that type will be
7497          marked used. (see TREE_USED, above.)  */
7498       if (TYPE_NEEDS_CONSTRUCTING (type)
7499           && ! already_used
7500           && !TYPE_NEEDS_DESTRUCTOR (type) 
7501           && DECL_NAME (decl))
7502         TREE_USED (decl) = 0;
7503       else if (already_used)
7504         TREE_USED (decl) = 1;
7505     }
7506 }
7507
7508 /* Generate code to destroy DECL (a local variable).  */
7509
7510 static void 
7511 destroy_local_var (decl)
7512      tree decl;
7513 {
7514   tree type = TREE_TYPE (decl);
7515   tree cleanup;
7516
7517   /* Only variables get cleaned up.  */
7518   if (TREE_CODE (decl) != VAR_DECL)
7519     return;
7520   
7521   /* And only things with destructors need cleaning up.  */
7522   if (!TYPE_NEEDS_DESTRUCTOR (type))
7523     return;
7524
7525   if (TREE_CODE (decl) == VAR_DECL &&
7526       (DECL_EXTERNAL (decl) || TREE_STATIC (decl)))
7527     /* We don't clean up things that aren't defined in this
7528        translation unit, or that need a static cleanup.  The latter
7529        are handled by finish_file.  */
7530     return;
7531   
7532   /* Compute the cleanup.  */
7533   cleanup = maybe_build_cleanup (decl);
7534
7535   /* Record the cleanup required for this declaration.  */
7536   if (DECL_SIZE (decl) && TREE_TYPE (decl) != error_mark_node
7537       && cleanup)
7538     finish_decl_cleanup (decl, cleanup);
7539 }
7540
7541 /* Let the back-end know about DECL.  */
7542
7543 void
7544 emit_local_var (decl)
7545      tree decl;
7546 {
7547   /* Create RTL for this variable.  */
7548   if (DECL_RTL (decl))
7549     /* Only a RESULT_DECL should have non-NULL RTL when arriving here.
7550        All other local variables are assigned RTL in this function.  */
7551     my_friendly_assert (TREE_CODE (decl) == RESULT_DECL, 
7552                         19990828);
7553   else
7554     {
7555       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
7556         /* The user must have specified an assembler name for this
7557            variable.  Set that up now.  */
7558         rest_of_decl_compilation 
7559           (decl, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
7560            /*top_level=*/0, /*at_end=*/0);
7561       else
7562         expand_decl (decl);
7563     }
7564
7565   /* Actually do the initialization.  */
7566   expand_start_target_temps ();
7567   expand_decl_init (decl);
7568   expand_end_target_temps ();
7569 }
7570
7571 /* Finish processing of a declaration;
7572    install its line number and initial value.
7573    If the length of an array type is not known before,
7574    it must be determined now, from the initial value, or it is an error.
7575
7576    For C++, `cp_finish_decl' must be fairly evasive:  it must keep initializers
7577    for aggregates that have constructors alive on the permanent obstack,
7578    so that the global initializing functions can be written at the end.
7579
7580    INIT0 holds the value of an initializer that should be allowed to escape
7581    the normal rules.
7582
7583    FLAGS is LOOKUP_ONLYCONVERTING is the = init syntax was used, else 0
7584    if the (init) syntax was used.
7585
7586    For functions that take default parameters, DECL points to its
7587    "maximal" instantiation.  `cp_finish_decl' must then also declared its
7588    subsequently lower and lower forms of instantiation, checking for
7589    ambiguity as it goes.  This can be sped up later.  */
7590
7591 void
7592 cp_finish_decl (decl, init, asmspec_tree, flags)
7593      tree decl, init;
7594      tree asmspec_tree;
7595      int flags;
7596 {
7597   register tree type;
7598   tree ttype = NULL_TREE;
7599   const char *asmspec = NULL;
7600   int was_readonly = 0;
7601
7602   /* If this is 0, then we did not change obstacks.  */
7603   if (! decl)
7604     {
7605       if (init)
7606         error ("assignment (not initialization) in declaration");
7607       return;
7608     }
7609
7610   /* Handling __FUNCTION__ and its ilk in a template-function requires
7611      some special processing because we are called from
7612      language-independent code.  */
7613   if (current_function && processing_template_decl 
7614       && current_function_name_declared == 2)
7615     {
7616       /* Since we're in a template function, we need to
7617          push_template_decl.  The language-independent code in
7618          declare_hidden_char_array doesn't know to do this.  */
7619       retrofit_lang_decl (decl);
7620       decl = push_template_decl (decl);
7621
7622       if (strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), 
7623                   "__PRETTY_FUNCTION__") == 0)
7624         {
7625           init = build (FUNCTION_NAME, const_string_type_node);
7626           DECL_PRETTY_FUNCTION_P (decl) = 1;
7627         }
7628     }
7629
7630   /* If a name was specified, get the string.  */
7631   if (asmspec_tree)
7632       asmspec = TREE_STRING_POINTER (asmspec_tree);
7633
7634   if (init && TREE_CODE (init) == NAMESPACE_DECL)
7635     {
7636       cp_error ("Cannot initialize `%D' to namespace `%D'",
7637                 decl, init);
7638       init = NULL_TREE;
7639     }
7640
7641   if (current_class_type
7642       && DECL_REAL_CONTEXT (decl) == current_class_type
7643       && TYPE_BEING_DEFINED (current_class_type)
7644       && (DECL_INITIAL (decl) || init))
7645     DECL_DEFINED_IN_CLASS_P (decl) = 1;
7646
7647   if (TREE_CODE (decl) == VAR_DECL 
7648       && DECL_CONTEXT (decl)
7649       && TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL
7650       && DECL_CONTEXT (decl) != current_namespace
7651       && init)
7652     {
7653       /* Leave the namespace of the object. */
7654       pop_decl_namespace ();
7655     }
7656
7657   type = TREE_TYPE (decl);
7658
7659   if (type == error_mark_node)
7660     return;
7661
7662   /* Add this declaration to the statement-tree.  */
7663   if (building_stmt_tree () && at_function_scope_p ())
7664     add_decl_stmt (decl);
7665
7666   if (TYPE_HAS_MUTABLE_P (type))
7667     TREE_READONLY (decl) = 0;
7668
7669   if (processing_template_decl)
7670     {
7671       if (init && DECL_INITIAL (decl))
7672         DECL_INITIAL (decl) = init;
7673       goto finish_end0;
7674     }
7675
7676   /* Parameters are handled by store_parm_decls, not cp_finish_decl.  */
7677   my_friendly_assert (TREE_CODE (decl) != PARM_DECL, 19990828);
7678
7679   /* Take care of TYPE_DECLs up front.  */
7680   if (TREE_CODE (decl) == TYPE_DECL)
7681     {
7682       if (init && DECL_INITIAL (decl))
7683         {
7684           /* typedef foo = bar; store the type of bar as the type of foo.  */
7685           TREE_TYPE (decl) = type = TREE_TYPE (init);
7686           DECL_INITIAL (decl) = init = NULL_TREE;
7687         }
7688       if (type != error_mark_node
7689           && IS_AGGR_TYPE (type) && DECL_NAME (decl))
7690         {
7691           if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
7692             cp_warning ("shadowing previous type declaration of `%#D'", decl);
7693           set_identifier_type_value (DECL_NAME (decl), type);
7694           CLASSTYPE_GOT_SEMICOLON (type) = 1;
7695         }
7696       GNU_xref_decl (current_function_decl, decl);
7697
7698       /* If we have installed this as the canonical typedef for this
7699          type, and that type has not been defined yet, delay emitting
7700          the debug information for it, as we will emit it later.  */
7701       if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
7702           && TYPE_SIZE (TREE_TYPE (decl)) == NULL_TREE)
7703         TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
7704
7705       rest_of_decl_compilation (decl, NULL_PTR,
7706                                 DECL_CONTEXT (decl) == NULL_TREE, at_eof);
7707       goto finish_end;
7708     }
7709
7710   if (TREE_CODE (decl) != FUNCTION_DECL)
7711     ttype = target_type (type);
7712
7713   if (! DECL_EXTERNAL (decl) && TREE_READONLY (decl)
7714       && TYPE_NEEDS_CONSTRUCTING (type))
7715     {
7716       /* Currently, GNU C++ puts constants in text space, making them
7717          impossible to initialize.  In the future, one would hope for
7718          an operating system which understood the difference between
7719          initialization and the running of a program.  */
7720       was_readonly = 1;
7721       TREE_READONLY (decl) = 0;
7722     }
7723
7724   if (TREE_CODE (decl) == FIELD_DECL && asmspec)
7725     {
7726       /* This must override the asm specifier which was placed by
7727          grokclassfn.  Lay this out fresh.  */
7728       DECL_RTL (TREE_TYPE (decl)) = NULL_RTX;
7729       DECL_ASSEMBLER_NAME (decl) = get_identifier (asmspec);
7730       make_decl_rtl (decl, asmspec, 0);
7731     }
7732
7733   /* Deduce size of array from initialization, if not already known.  */
7734   maybe_deduce_size_from_array_init (decl, init);
7735   init = check_initializer (decl, init);
7736
7737   GNU_xref_decl (current_function_decl, decl);
7738
7739   if (TREE_CODE (decl) == VAR_DECL)
7740     layout_var_decl (decl);
7741
7742   /* Output the assembler code and/or RTL code for variables and functions,
7743      unless the type is an undefined structure or union.
7744      If not, it will get done when the type is completed.  */
7745   if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL
7746       || TREE_CODE (decl) == RESULT_DECL)
7747     {
7748       if (TREE_CODE (decl) == VAR_DECL)
7749         maybe_commonize_var (decl);
7750
7751       make_rtl_for_nonlocal_decl (decl, init, asmspec);
7752
7753       if (TREE_CODE (type) == FUNCTION_TYPE 
7754           || TREE_CODE (type) == METHOD_TYPE)
7755         abstract_virtuals_error (decl, 
7756                                  strip_array_types (TREE_TYPE (type)));
7757       else 
7758         abstract_virtuals_error (decl, strip_array_types (type));
7759
7760       if (TREE_CODE (decl) == FUNCTION_DECL)
7761         ;
7762       else if (DECL_EXTERNAL (decl)
7763                && ! (DECL_LANG_SPECIFIC (decl)
7764                      && DECL_NOT_REALLY_EXTERN (decl)))
7765         {
7766           if (init)
7767             DECL_INITIAL (decl) = init;
7768         }
7769       else if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
7770         {
7771           /* This is a local declaration.  */
7772           if (doing_semantic_analysis_p ())
7773             maybe_inject_for_scope_var (decl);
7774           /* Initialize the local variable.  But, if we're building a
7775              statement-tree, we'll do the initialization when we
7776              expand the tree.  */
7777           if (processing_template_decl)
7778             {
7779               if (init || DECL_INITIAL (decl) == error_mark_node)
7780                 DECL_INITIAL (decl) = init;
7781             }
7782           else
7783             {
7784               /* If we're not building RTL, then we need to do so
7785                  now.  */
7786               if (!building_stmt_tree ())
7787                 emit_local_var (decl);
7788               /* Initialize the variable.  */
7789               initialize_local_var (decl, init, flags);
7790               /* Clean up the variable.  */
7791               destroy_local_var (decl);
7792             }
7793         }
7794       else if (TREE_STATIC (decl) && type != error_mark_node)
7795         {
7796           /* Cleanups for static variables are handled by `finish_file'.  */
7797           if (TYPE_NEEDS_CONSTRUCTING (type) || init != NULL_TREE
7798               || TYPE_NEEDS_DESTRUCTOR (type))
7799             expand_static_init (decl, init);
7800         }
7801     finish_end0:
7802
7803       /* Undo call to `pushclass' that was done in `start_decl'
7804          due to initialization of qualified member variable.
7805          I.e., Foo::x = 10;  */
7806       {
7807         tree context = DECL_REAL_CONTEXT (decl);
7808         if (context
7809             && TREE_CODE_CLASS (TREE_CODE (context)) == 't'
7810             && (TREE_CODE (decl) == VAR_DECL
7811                 /* We also have a pushclass done that we need to undo here
7812                    if we're at top level and declare a method.  */
7813                 || TREE_CODE (decl) == FUNCTION_DECL)
7814             /* If size hasn't been set, we're still defining it,
7815                and therefore inside the class body; don't pop
7816                the binding level..  */
7817             && TYPE_SIZE (context) != NULL_TREE
7818             && context == current_class_type)
7819           pop_nested_class ();
7820       }
7821     }
7822
7823  finish_end:
7824
7825   /* If requested, warn about definitions of large data objects.  */
7826
7827   if (warn_larger_than
7828       && ! processing_template_decl
7829       && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL)
7830       && !DECL_EXTERNAL (decl))
7831     {
7832       register tree decl_size = DECL_SIZE (decl);
7833
7834       if (decl_size && TREE_CODE (decl_size) == INTEGER_CST)
7835         {
7836           unsigned units = TREE_INT_CST_LOW (decl_size) / BITS_PER_UNIT;
7837
7838           if (units > larger_than_size)
7839             warning_with_decl (decl, "size of `%s' is %u bytes", units);
7840         }
7841     }
7842
7843   if (was_readonly)
7844     TREE_READONLY (decl) = 1;
7845 }
7846
7847 /* This is here for a midend callback from c-common.c */
7848
7849 void
7850 finish_decl (decl, init, asmspec_tree)
7851      tree decl, init;
7852      tree asmspec_tree;
7853 {
7854   cp_finish_decl (decl, init, asmspec_tree, 0);
7855 }
7856
7857 /* Generate code to handle the destruction of the function-scoped
7858    static variable DECL.  */
7859
7860 static void
7861 destroy_local_static (decl)
7862      tree decl;
7863 {
7864   tree cleanup, fcall;
7865   tree compound_stmt;
7866   int saved_flag_access_control;
7867
7868   if (atexit_node == 0)
7869     {
7870       tree atexit_fndecl, PFV, pfvlist;
7871
7872       PFV = build_pointer_type (build_function_type
7873                                 (void_type_node, void_list_node));
7874
7875       pfvlist = tree_cons (NULL_TREE, PFV, void_list_node);
7876
7877       push_lang_context (lang_name_c);
7878       /* Note that we do not call pushdecl for this function;
7879          there's no reason that this declaration should be
7880          accessible to anyone.  */
7881       atexit_fndecl
7882         = define_function ("atexit",
7883                            build_function_type (void_type_node,
7884                                                 pfvlist),
7885                            /*pfn=*/0, NULL_PTR);
7886       mark_used (atexit_fndecl);
7887       atexit_node = default_conversion (atexit_fndecl);
7888       pop_lang_context ();
7889     }
7890               
7891   /* Call build_cleanup before we enter the anonymous function so that
7892      any access checks will be done relative to the current scope,
7893      rather than the scope of the anonymous function.  */
7894   build_cleanup (decl);
7895
7896   /* Now start the function.  */
7897   cleanup = start_anon_func ();
7898
7899   /* Now, recompute the cleanup.  It may contain SAVE_EXPRs that refer
7900      to the original function, rather than the anonymous one.  That
7901      will make the back-end think that nested functions are in use,
7902      which causes confusion.  */
7903   saved_flag_access_control = flag_access_control;
7904   flag_access_control = 0;
7905   fcall = build_cleanup (decl);
7906   flag_access_control = saved_flag_access_control;
7907
7908   /* Create the body of the anonymous function.  */
7909   compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
7910   finish_expr_stmt (fcall);
7911   finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
7912   end_anon_func ();
7913
7914   /* Call atexit with the cleanup function.  */
7915   mark_addressable (cleanup);
7916   cleanup = build_unary_op (ADDR_EXPR, cleanup, 0);
7917   fcall = build_function_call (atexit_node,
7918                                tree_cons (NULL_TREE, 
7919                                           cleanup, 
7920                                           NULL_TREE));
7921   finish_expr_stmt (fcall);
7922 }
7923
7924 void
7925 expand_static_init (decl, init)
7926      tree decl;
7927      tree init;
7928 {
7929   tree oldstatic = value_member (decl, static_aggregates);
7930
7931   if (oldstatic)
7932     {
7933       if (TREE_PURPOSE (oldstatic) && init != NULL_TREE)
7934         cp_error ("multiple initializations given for `%D'", decl);
7935     }
7936   else if (! toplevel_bindings_p ())
7937     {
7938       /* Emit code to perform this initialization but once.  */
7939       tree temp;
7940       tree if_stmt;
7941       tree then_clause;
7942       tree assignment;
7943       tree temp_init;
7944
7945       /* Emit code to perform this initialization but once.  This code
7946          looks like:
7947
7948            static int temp = 0;
7949            if (!temp) {
7950              // Do initialization.
7951              temp = 1;
7952              // Register variable for destruction at end of program.
7953            }
7954
7955          Note that the `temp' variable is only set to 1 *after* the
7956          initialization is complete.  This ensures that an exception,
7957          thrown during the construction, will cause the variable to
7958          reinitialized when we pass through this code again, as per:
7959          
7960            [stmt.dcl]
7961
7962            If the initialization exits by throwing an exception, the
7963            initialization is not complete, so it will be tried again
7964            the next time control enters the declaration.
7965
7966          In theory, this process should be thread-safe, too; multiple
7967          threads should not be able to initialize the variable more
7968          than once.  We don't yet attempt to ensure thread-safety.  */
7969       temp = get_temp_name (integer_type_node, 1);
7970       rest_of_decl_compilation (temp, NULL_PTR, 0, 0);
7971
7972       /* Begin the conditional initialization.  */
7973       if_stmt = begin_if_stmt ();
7974       finish_if_stmt_cond (build_binary_op (EQ_EXPR, temp,
7975                                             integer_zero_node), 
7976                            if_stmt);
7977       then_clause = begin_compound_stmt (/*has_no_scope=*/0);
7978
7979       /* Do the initialization itself.  */
7980       if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))
7981           || (init && TREE_CODE (init) == TREE_LIST))
7982         assignment = build_aggr_init (decl, init, 0);
7983       else if (init)
7984         /* The initialization we're doing here is just a bitwise
7985            copy.  */
7986         assignment = build (INIT_EXPR, TREE_TYPE (decl), decl, init);
7987       else
7988         assignment = NULL_TREE;
7989
7990       /* Once the assignment is complete, set TEMP to 1.  Since the
7991          construction of the static object is complete at this point,
7992          we want to make sure TEMP is set to 1 even if a temporary
7993          constructed during the initialization throws an exception
7994          when it is destroyed.  So, we combine the initialization and
7995          the assignment to TEMP into a single expression, ensuring
7996          that when we call finish_expr_stmt the cleanups will not be
7997          run until after TEMP is set to 1.  */
7998       temp_init = build_modify_expr (temp, NOP_EXPR, integer_one_node);
7999       if (assignment)
8000         {
8001           assignment = tree_cons (NULL_TREE, assignment,
8002                                   build_tree_list (NULL_TREE, 
8003                                                    temp_init));
8004           assignment = build_compound_expr (assignment);
8005         }
8006       else
8007         assignment = temp_init;
8008       finish_expr_stmt (assignment);
8009
8010       /* Use atexit to register a function for destroying this static
8011          variable.  */
8012       if (TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (decl)))
8013         destroy_local_static (decl);
8014
8015       finish_compound_stmt (/*has_no_scope=*/0, then_clause);
8016       finish_then_clause (if_stmt);
8017       finish_if_stmt ();
8018     }
8019   else
8020     {
8021       /* This code takes into account memory allocation policy of
8022          `start_decl'.  Namely, if TYPE_NEEDS_CONSTRUCTING does not
8023          hold for this object, then we must make permanent the storage
8024          currently in the temporary obstack.  */
8025       if (!TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
8026         preserve_initializer ();
8027       static_aggregates = tree_cons (init, decl, static_aggregates);
8028     }
8029 }
8030
8031 /* Finish the declaration of a catch-parameter.  */
8032
8033 tree
8034 start_handler_parms (declspecs, declarator)
8035      tree declspecs;
8036      tree declarator;
8037 {
8038   tree decl;
8039   if (declspecs)
8040     {
8041       decl = grokdeclarator (declarator, declspecs, CATCHPARM,
8042                              1, NULL_TREE);
8043       if (decl == NULL_TREE)
8044         error ("invalid catch parameter");
8045     }
8046   else
8047     decl = NULL_TREE;
8048
8049   return decl;
8050 }
8051
8052 \f
8053 /* Make TYPE a complete type based on INITIAL_VALUE.
8054    Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
8055    2 if there was no information (in which case assume 0 if DO_DEFAULT).  */
8056
8057 int
8058 complete_array_type (type, initial_value, do_default)
8059      tree type, initial_value;
8060      int do_default;
8061 {
8062   register tree maxindex = NULL_TREE;
8063   int value = 0;
8064   
8065   if (initial_value)
8066     {
8067       /* Note MAXINDEX  is really the maximum index,
8068          one less than the size.  */
8069       if (TREE_CODE (initial_value) == STRING_CST)
8070         {
8071           int eltsize
8072             = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
8073           maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
8074                                    / eltsize) - 1, 0);
8075         }
8076       else if (TREE_CODE (initial_value) == CONSTRUCTOR)
8077         {
8078           tree elts = CONSTRUCTOR_ELTS (initial_value);
8079           maxindex = size_binop (MINUS_EXPR, integer_zero_node, size_one_node);
8080           for (; elts; elts = TREE_CHAIN (elts))
8081             {
8082               if (TREE_PURPOSE (elts))
8083                 maxindex = TREE_PURPOSE (elts);
8084               else
8085                 maxindex = size_binop (PLUS_EXPR, maxindex, size_one_node);
8086             }
8087           maxindex = copy_node (maxindex);
8088         }
8089       else
8090         {
8091           /* Make an error message unless that happened already.  */
8092           if (initial_value != error_mark_node)
8093             value = 1;
8094           else
8095             initial_value = NULL_TREE;
8096
8097           /* Prevent further error messages.  */
8098           maxindex = build_int_2 (0, 0);
8099         }
8100     }
8101
8102   if (!maxindex)
8103     {
8104       if (do_default)
8105         maxindex = build_int_2 (0, 0);
8106       value = 2;
8107     }
8108
8109   if (maxindex)
8110     {
8111       tree itype;
8112       tree domain;
8113
8114       domain = build_index_type (maxindex);
8115       TYPE_DOMAIN (type) = domain;
8116
8117       if (! TREE_TYPE (maxindex))
8118         TREE_TYPE (maxindex) = domain;
8119       if (initial_value)
8120         itype = TREE_TYPE (initial_value);
8121       else
8122         itype = NULL;
8123       if (itype && !TYPE_DOMAIN (itype))
8124         TYPE_DOMAIN (itype) = domain;
8125       /* The type of the main variant should never be used for arrays
8126          of different sizes.  It should only ever be completed with the
8127          size of the array.  */
8128       if (! TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)))
8129         TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)) = domain;
8130     }
8131
8132   /* Lay out the type now that we can get the real answer.  */
8133
8134   layout_type (type);
8135
8136   return value;
8137 }
8138 \f
8139 /* Return zero if something is declared to be a member of type
8140    CTYPE when in the context of CUR_TYPE.  STRING is the error
8141    message to print in that case.  Otherwise, quietly return 1.  */
8142
8143 static int
8144 member_function_or_else (ctype, cur_type, string)
8145      tree ctype, cur_type;
8146      const char *string;
8147 {
8148   if (ctype && ctype != cur_type)
8149     {
8150       error (string, TYPE_NAME_STRING (ctype));
8151       return 0;
8152     }
8153   return 1;
8154 }
8155 \f
8156 /* Subroutine of `grokdeclarator'.  */
8157
8158 /* Generate errors possibly applicable for a given set of specifiers.
8159    This is for ARM $7.1.2.  */
8160
8161 static void
8162 bad_specifiers (object, type, virtualp, quals, inlinep, friendp, raises)
8163      tree object;
8164      const char *type;
8165      int virtualp, quals, friendp, raises, inlinep;
8166 {
8167   if (virtualp)
8168     cp_error ("`%D' declared as a `virtual' %s", object, type);
8169   if (inlinep)
8170     cp_error ("`%D' declared as an `inline' %s", object, type);
8171   if (quals)
8172     cp_error ("`const' and `volatile' function specifiers on `%D' invalid in %s declaration",
8173               object, type);
8174   if (friendp)
8175     cp_error_at ("`%D' declared as a friend", object);
8176   if (raises)
8177     cp_error_at ("`%D' declared with an exception specification", object);
8178 }
8179
8180 /* CTYPE is class type, or null if non-class.
8181    TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
8182    or METHOD_TYPE.
8183    DECLARATOR is the function's name.
8184    VIRTUALP is truthvalue of whether the function is virtual or not.
8185    FLAGS are to be passed through to `grokclassfn'.
8186    QUALS are qualifiers indicating whether the function is `const'
8187    or `volatile'.
8188    RAISES is a list of exceptions that this function can raise.
8189    CHECK is 1 if we must find this method in CTYPE, 0 if we should
8190    not look, and -1 if we should not call `grokclassfn' at all.  
8191
8192    Returns `NULL_TREE' if something goes wrong, after issuing
8193    applicable error messages.  */
8194
8195 static tree
8196 grokfndecl (ctype, type, declarator, orig_declarator, virtualp, flags, quals,
8197             raises, check, friendp, publicp, inlinep, funcdef_flag,
8198             template_count, in_namespace)
8199      tree ctype, type;
8200      tree declarator;
8201      tree orig_declarator;
8202      int virtualp;
8203      enum overload_flags flags;
8204      tree quals, raises;
8205      int check, friendp, publicp, inlinep, funcdef_flag, template_count;
8206      tree in_namespace;
8207 {
8208   tree cname, decl;
8209   int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
8210   int has_default_arg = 0;
8211   tree t;
8212
8213   if (ctype)
8214     cname = TREE_CODE (TYPE_NAME (ctype)) == TYPE_DECL
8215       ? TYPE_IDENTIFIER (ctype) : TYPE_NAME (ctype);
8216   else
8217     cname = NULL_TREE;
8218
8219   if (raises)
8220     {
8221       type = build_exception_variant (type, raises);
8222     }
8223
8224   decl = build_lang_decl (FUNCTION_DECL, declarator, type);
8225   /* Propagate volatile out from type to decl. */
8226   if (TYPE_VOLATILE (type))
8227     TREE_THIS_VOLATILE (decl) = 1;
8228
8229   /* If this decl has namespace scope, set that up.  */
8230   if (in_namespace)
8231     set_decl_namespace (decl, in_namespace, friendp);
8232   else if (publicp && ! ctype)
8233     DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
8234
8235   /* `main' and builtins have implicit 'C' linkage.  */
8236   if ((MAIN_NAME_P (declarator)
8237        || (IDENTIFIER_LENGTH (declarator) > 10
8238            && IDENTIFIER_POINTER (declarator)[0] == '_'
8239            && IDENTIFIER_POINTER (declarator)[1] == '_'
8240            && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0))
8241       && current_lang_name == lang_name_cplusplus
8242       && ctype == NULL_TREE
8243       /* NULL_TREE means global namespace.  */
8244       && DECL_CONTEXT (decl) == NULL_TREE)
8245     DECL_LANGUAGE (decl) = lang_c;
8246
8247   /* Should probably propagate const out from type to decl I bet (mrs).  */
8248   if (staticp)
8249     {
8250       DECL_STATIC_FUNCTION_P (decl) = 1;
8251       DECL_CONTEXT (decl) = ctype;
8252     }
8253
8254   if (ctype)
8255     DECL_CLASS_CONTEXT (decl) = ctype;
8256
8257   if (ctype == NULL_TREE && DECL_MAIN_P (decl))
8258     {
8259       if (processing_template_decl)
8260         error ("cannot declare `main' to be a template");
8261       if (inlinep)
8262         error ("cannot declare `main' to be inline");
8263       else if (! publicp)
8264         error ("cannot declare `main' to be static");
8265       inlinep = 0;
8266       publicp = 1;
8267     }
8268
8269   /* Members of anonymous types and local classes have no linkage; make
8270      them internal.  */
8271   if (ctype && (ANON_AGGRNAME_P (TYPE_IDENTIFIER (ctype))
8272                 || hack_decl_function_context (TYPE_MAIN_DECL (ctype))))
8273     publicp = 0;
8274
8275   if (publicp)
8276     {
8277       /* [basic.link]: A name with no linkage (notably, the name of a class
8278          or enumeration declared in a local scope) shall not be used to
8279          declare an entity with linkage.
8280
8281          Only check this for public decls for now.  */
8282       t = no_linkage_check (TREE_TYPE (decl));
8283       if (t)
8284         {
8285           if (ANON_AGGRNAME_P (TYPE_IDENTIFIER (t)))
8286             {
8287               if (DECL_LANGUAGE (decl) == lang_c)
8288                 /* Allow this; it's pretty common in C.  */;
8289               else
8290                 cp_pedwarn ("non-local function `%#D' uses anonymous type",
8291                             decl);
8292             }
8293           else
8294             cp_pedwarn ("non-local function `%#D' uses local type `%T'",
8295                         decl, t);
8296         }
8297     }
8298
8299   TREE_PUBLIC (decl) = publicp;
8300   if (! publicp)
8301     {
8302       DECL_INTERFACE_KNOWN (decl) = 1;
8303       DECL_NOT_REALLY_EXTERN (decl) = 1;
8304     }
8305
8306   if (inlinep)
8307     DECL_THIS_INLINE (decl) = DECL_INLINE (decl) = 1;
8308
8309   DECL_EXTERNAL (decl) = 1;
8310   if (quals != NULL_TREE && TREE_CODE (type) == FUNCTION_TYPE)
8311     {
8312       cp_error ("%smember function `%D' cannot have `%T' method qualifier",
8313                 (ctype ? "static " : "non-"), decl, TREE_VALUE (quals));
8314       quals = NULL_TREE;
8315     }
8316
8317   if (IDENTIFIER_OPNAME_P (DECL_NAME (decl)))
8318     grok_op_properties (decl, virtualp, check < 0);
8319
8320   if (ctype && hack_decl_function_context (decl))
8321     DECL_NO_STATIC_CHAIN (decl) = 1;
8322
8323   for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
8324     if (TREE_PURPOSE (t)
8325         && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
8326       {
8327         has_default_arg = 1;
8328         break;
8329       }
8330
8331   if (friendp
8332       && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
8333     {
8334       if (funcdef_flag)
8335         cp_error
8336           ("defining explicit specialization `%D' in friend declaration",
8337            orig_declarator);
8338       else
8339         {
8340           if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8341             {
8342               /* Something like `template <class T> friend void f<T>()'.  */
8343               cp_error ("template-id `%D' in declaration of primary template", 
8344                         orig_declarator);
8345               return NULL_TREE;
8346             }
8347
8348
8349           /* A friend declaration of the form friend void f<>().  Record
8350              the information in the TEMPLATE_ID_EXPR.  */
8351           SET_DECL_IMPLICIT_INSTANTIATION (decl);
8352           DECL_TEMPLATE_INFO (decl)
8353             = tree_cons (TREE_OPERAND (orig_declarator, 0),
8354                          TREE_OPERAND (orig_declarator, 1),
8355                          NULL_TREE);
8356
8357           if (has_default_arg)
8358             {
8359               cp_error ("default arguments are not allowed in declaration of friend template specialization `%D'",
8360                         decl);
8361               return NULL_TREE;
8362             }
8363
8364           if (inlinep)
8365             {
8366               cp_error ("`inline' is not allowed in declaration of friend template specialization `%D'", 
8367                         decl);
8368               return NULL_TREE;
8369             }
8370         }
8371     }
8372
8373   if (has_default_arg)
8374     add_defarg_fn (decl);
8375
8376   /* Plain overloading: will not be grok'd by grokclassfn.  */
8377   if (! ctype && ! processing_template_decl
8378       && DECL_LANGUAGE (decl) != lang_c
8379       && (! DECL_USE_TEMPLATE (decl) || name_mangling_version < 1))
8380     set_mangled_name_for_decl (decl);
8381
8382   if (funcdef_flag)
8383     /* Make the init_value nonzero so pushdecl knows this is not
8384        tentative.  error_mark_node is replaced later with the BLOCK.  */
8385     DECL_INITIAL (decl) = error_mark_node;
8386
8387   /* Caller will do the rest of this.  */
8388   if (check < 0)
8389     return decl;
8390
8391   if (flags == NO_SPECIAL && ctype && constructor_name (cname) == declarator)
8392     {
8393       tree tmp;
8394       /* Just handle constructors here.  We could do this
8395          inside the following if stmt, but I think
8396          that the code is more legible by breaking this
8397          case out.  See comments below for what each of
8398          the following calls is supposed to do.  */
8399       DECL_CONSTRUCTOR_P (decl) = 1;
8400
8401       grokclassfn (ctype, decl, flags, quals);
8402
8403       decl = check_explicit_specialization (orig_declarator, decl,
8404                                             template_count, 
8405                                             2 * (funcdef_flag != 0) + 
8406                                             4 * (friendp != 0));
8407       if (decl == error_mark_node)
8408         return NULL_TREE;
8409
8410       if ((! TYPE_FOR_JAVA (ctype) || check_java_method (decl))
8411           && check)
8412         {
8413           tmp = check_classfn (ctype, decl);
8414
8415           if (tmp && TREE_CODE (tmp) == TEMPLATE_DECL)
8416             tmp = DECL_TEMPLATE_RESULT(tmp);
8417
8418           if (tmp && DECL_ARTIFICIAL (tmp))
8419             cp_error ("definition of implicitly-declared `%D'", tmp);
8420           if (tmp && duplicate_decls (decl, tmp))
8421             return tmp;
8422         }
8423       if (! grok_ctor_properties (ctype, decl))
8424         return NULL_TREE;
8425     }
8426   else
8427     {
8428       tree tmp;
8429
8430       /* Function gets the ugly name, field gets the nice one.
8431          This call may change the type of the function (because
8432          of default parameters)!  */
8433       if (ctype != NULL_TREE)
8434         grokclassfn (ctype, decl, flags, quals);
8435
8436       decl = check_explicit_specialization (orig_declarator, decl,
8437                                             template_count, 
8438                                             2 * (funcdef_flag != 0) + 
8439                                             4 * (friendp != 0));
8440       if (decl == error_mark_node)
8441         return NULL_TREE;
8442
8443       if (ctype != NULL_TREE
8444           && (! TYPE_FOR_JAVA (ctype) || check_java_method (decl))
8445           && check)
8446         {
8447           tmp = check_classfn (ctype, decl);
8448
8449           if (tmp && TREE_CODE (tmp) == TEMPLATE_DECL)
8450             tmp = DECL_TEMPLATE_RESULT (tmp);
8451               
8452           if (tmp && DECL_STATIC_FUNCTION_P (tmp)
8453               && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
8454             {
8455               /* Remove the `this' parm added by grokclassfn.
8456                  XXX Isn't this done in start_function, too?  */
8457               revert_static_member_fn (&decl, NULL, NULL);
8458               last_function_parms = TREE_CHAIN (last_function_parms);
8459             }
8460           if (tmp && DECL_ARTIFICIAL (tmp))
8461             cp_error ("definition of implicitly-declared `%D'", tmp);
8462           if (tmp)
8463             {
8464               /* Attempt to merge the declarations.  This can fail, in
8465                  the case of some illegal specialization declarations.  */
8466               if (!duplicate_decls (decl, tmp))
8467                 cp_error ("no `%#D' member function declared in class `%T'",
8468                           decl, ctype);
8469               return tmp;
8470             }
8471         }
8472
8473       if (ctype == NULL_TREE || check)
8474         return decl;
8475
8476       if (virtualp)
8477         {
8478           DECL_VIRTUAL_P (decl) = 1;
8479           if (DECL_VINDEX (decl) == NULL_TREE)
8480             DECL_VINDEX (decl) = error_mark_node;
8481           IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
8482         }
8483     }
8484   return decl;
8485 }
8486
8487 static tree
8488 grokvardecl (type, declarator, specbits_in, initialized, constp, in_namespace)
8489      tree type;
8490      tree declarator;
8491      RID_BIT_TYPE *specbits_in;
8492      int initialized;
8493      int constp;
8494      tree in_namespace;
8495 {
8496   tree decl;
8497   RID_BIT_TYPE specbits;
8498
8499   specbits = *specbits_in;
8500
8501   if (TREE_CODE (type) == OFFSET_TYPE)
8502     {
8503       /* If you declare a static member so that it
8504          can be initialized, the code will reach here.  */
8505       tree basetype = TYPE_OFFSET_BASETYPE (type);
8506       type = TREE_TYPE (type);
8507       decl = build_lang_decl (VAR_DECL, declarator, type);
8508       DECL_CONTEXT (decl) = basetype;
8509       DECL_CLASS_CONTEXT (decl) = basetype;
8510       DECL_ASSEMBLER_NAME (decl) = build_static_name (basetype, declarator);
8511     }
8512   else
8513     {
8514       tree context;
8515
8516       if (in_namespace)
8517         context = in_namespace;
8518       else if (namespace_bindings_p () || RIDBIT_SETP (RID_EXTERN, specbits))
8519         context = current_namespace;
8520       else
8521         context = NULL_TREE;
8522
8523       if (processing_template_decl) 
8524         /* If we're in a template, we need DECL_LANG_SPECIFIC so that
8525            we can call push_template_decl.  */
8526         decl = build_lang_decl (VAR_DECL, declarator, type);
8527       else
8528         decl = build_decl (VAR_DECL, declarator, type);
8529
8530       if (context)
8531         set_decl_namespace (decl, context, 0);
8532
8533       context = DECL_CONTEXT (decl);
8534       if (declarator && context && current_lang_name != lang_name_c)
8535         DECL_ASSEMBLER_NAME (decl) = build_static_name (context, declarator);
8536     }
8537
8538   if (in_namespace)
8539     set_decl_namespace (decl, in_namespace, 0);
8540
8541   if (RIDBIT_SETP (RID_EXTERN, specbits))
8542     {
8543       DECL_THIS_EXTERN (decl) = 1;
8544       DECL_EXTERNAL (decl) = !initialized;
8545     }
8546
8547   /* In class context, static means one per class,
8548      public access, and static storage.  */
8549   if (DECL_CLASS_SCOPE_P (decl))
8550     {
8551       TREE_PUBLIC (decl) = 1;
8552       TREE_STATIC (decl) = 1;
8553       DECL_EXTERNAL (decl) = 0;
8554     }
8555   /* At top level, either `static' or no s.c. makes a definition
8556      (perhaps tentative), and absence of `static' makes it public.  */
8557   else if (toplevel_bindings_p ())
8558     {
8559       TREE_PUBLIC (decl) = (RIDBIT_NOTSETP (RID_STATIC, specbits)
8560                             && (DECL_THIS_EXTERN (decl) || ! constp));
8561       TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
8562     }
8563   /* Not at top level, only `static' makes a static definition.  */
8564   else
8565     {
8566       TREE_STATIC (decl) = !! RIDBIT_SETP (RID_STATIC, specbits);
8567       TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
8568     }
8569
8570   if (TREE_PUBLIC (decl))
8571     {
8572       /* [basic.link]: A name with no linkage (notably, the name of a class
8573          or enumeration declared in a local scope) shall not be used to
8574          declare an entity with linkage.
8575
8576          Only check this for public decls for now.  */
8577       tree t = no_linkage_check (TREE_TYPE (decl));
8578       if (t)
8579         {
8580           if (ANON_AGGRNAME_P (TYPE_IDENTIFIER (t)))
8581             /* Ignore for now; `enum { foo } e' is pretty common.  */;
8582           else
8583             cp_pedwarn ("non-local variable `%#D' uses local type `%T'",
8584                         decl, t);
8585         }
8586     }
8587
8588   return decl;
8589 }
8590
8591 /* Create and return a canonical pointer to member function type, for
8592    TYPE, which is a POINTER_TYPE to a METHOD_TYPE.  */
8593
8594 tree
8595 build_ptrmemfunc_type (type)
8596      tree type;
8597 {
8598   tree fields[4];
8599   tree t;
8600   tree u;
8601   tree unqualified_variant = NULL_TREE;
8602
8603   /* If a canonical type already exists for this type, use it.  We use
8604      this method instead of type_hash_canon, because it only does a
8605      simple equality check on the list of field members.  */
8606
8607   if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type)))
8608     return t;
8609
8610   /* Make sure that we always have the unqualified pointer-to-member
8611      type first.  */
8612   if (CP_TYPE_QUALS (type) != TYPE_UNQUALIFIED)
8613     unqualified_variant 
8614       = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
8615
8616   u = make_lang_type (UNION_TYPE);
8617   SET_IS_AGGR_TYPE (u, 0);
8618   fields[0] = build_lang_decl (FIELD_DECL, pfn_identifier, type);
8619   fields[1] = build_lang_decl (FIELD_DECL, delta2_identifier,
8620                                delta_type_node);
8621   finish_builtin_type (u, "__ptrmemfunc_type", fields, 1, ptr_type_node);
8622   TYPE_NAME (u) = NULL_TREE;
8623
8624   t = make_lang_type (RECORD_TYPE);
8625
8626   /* Let the front-end know this is a pointer to member function...  */
8627   TYPE_PTRMEMFUNC_FLAG (t) = 1;
8628   /* ... and not really an aggregate.  */
8629   SET_IS_AGGR_TYPE (t, 0);
8630
8631   fields[0] = build_lang_decl (FIELD_DECL, delta_identifier,
8632                                delta_type_node);
8633   fields[1] = build_lang_decl (FIELD_DECL, index_identifier,
8634                                delta_type_node);
8635   fields[2] = build_lang_decl (FIELD_DECL, pfn_or_delta2_identifier, u);
8636   finish_builtin_type (t, "__ptrmemfunc_type", fields, 2, ptr_type_node);
8637
8638   /* Zap out the name so that the back-end will give us the debugging
8639      information for this anonymous RECORD_TYPE.  */
8640   TYPE_NAME (t) = NULL_TREE;
8641
8642   /* If this is not the unqualified form of this pointer-to-member
8643      type, set the TYPE_MAIN_VARIANT for this type to be the
8644      unqualified type.  Since they are actually RECORD_TYPEs that are
8645      not variants of each other, we must do this manually.  */
8646   if (CP_TYPE_QUALS (type) != TYPE_UNQUALIFIED)
8647     {
8648       t = build_qualified_type (t, CP_TYPE_QUALS (type));
8649       TYPE_MAIN_VARIANT (t) = unqualified_variant;
8650       TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (unqualified_variant);
8651       TYPE_NEXT_VARIANT (unqualified_variant) = t;
8652     }
8653
8654   /* Cache this pointer-to-member type so that we can find it again
8655      later.  */
8656   TYPE_SET_PTRMEMFUNC_TYPE (type, t);
8657
8658   /* Seems to be wanted.  */
8659   CLASSTYPE_GOT_SEMICOLON (t) = 1;
8660
8661   return t;
8662 }
8663
8664 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
8665    Check to see that the definition is valid.  Issue appropriate error
8666    messages.  Return 1 if the definition is particularly bad, or 0
8667    otherwise.  */
8668
8669 int
8670 check_static_variable_definition (decl, type)
8671      tree decl;
8672      tree type;
8673 {
8674   /* Motion 10 at San Diego: If a static const integral data member is
8675      initialized with an integral constant expression, the initializer
8676      may appear either in the declaration (within the class), or in
8677      the definition, but not both.  If it appears in the class, the
8678      member is a member constant.  The file-scope definition is always
8679      required.  */
8680   if (CLASS_TYPE_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
8681     {
8682       cp_error ("in-class initialization of static data member of non-integral type `%T'", 
8683                 type);
8684       /* If we just return the declaration, crashes will sometimes
8685          occur.  We therefore return void_type_node, as if this was a
8686          friend declaration, to cause callers to completely ignore
8687          this declaration.  */
8688       return 1;
8689     }
8690   else if (!CP_TYPE_CONST_P (type))
8691     cp_error ("ANSI C++ forbids in-class initialization of non-const static member `%D'",
8692               decl);
8693   else if (pedantic && !INTEGRAL_TYPE_P (type))
8694     cp_pedwarn ("ANSI C++ forbids initialization of member constant `%D' of non-integral type `%T'", decl, type);
8695
8696   return 0;
8697 }
8698
8699 /* Given the SIZE (i.e., number of elements) in an array, compute an
8700    appropriate index type for the array.  If non-NULL, NAME is the
8701    name of the thing being declared.  */
8702
8703 tree
8704 compute_array_index_type (name, size)
8705      tree name;
8706      tree size;
8707 {
8708   tree itype;
8709
8710   /* The size might be the result of a cast. */
8711   STRIP_TYPE_NOPS (size);
8712
8713   /* It might be a const variable or enumeration constant.  */
8714   if (TREE_READONLY_DECL_P (size))
8715     size = decl_constant_value (size);
8716
8717   /* If this involves a template parameter, it will be a constant at
8718      instantiation time, but we don't know what the value is yet.
8719      Even if no template parameters are involved, we may an expression
8720      that is not a constant; we don't even simplify `1 + 2' when
8721      processing a template.  */
8722   if (processing_template_decl)
8723     {
8724       /* Resolve a qualified reference to an enumerator or static
8725          const data member of ours.  */
8726       if (TREE_CODE (size) == SCOPE_REF
8727           && TREE_OPERAND (size, 0) == current_class_type)
8728         {
8729           tree t = lookup_field (current_class_type,
8730                                  TREE_OPERAND (size, 1), 0, 0);
8731           if (t)
8732             size = t;
8733         }
8734
8735       return build_index_type (build_min (MINUS_EXPR, sizetype,
8736                                           size, integer_one_node));
8737     }
8738
8739   /* The array bound must be an integer type.  */
8740   if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE
8741       && TREE_CODE (TREE_TYPE (size)) != ENUMERAL_TYPE
8742       && TREE_CODE (TREE_TYPE (size)) != BOOLEAN_TYPE)
8743     {
8744       cp_error ("size of array `%D' has non-integer type", name);
8745       size = integer_one_node;
8746     }
8747
8748   /* Normally, the array-bound will be a constant.  */
8749   if (TREE_CONSTANT (size))
8750     {
8751       /* Check to see if the array bound overflowed.  Make that an
8752          error, no matter how generous we're being.  */
8753       int old_flag_pedantic_errors = flag_pedantic_errors;
8754       int old_pedantic = pedantic;
8755       pedantic = flag_pedantic_errors = 1;
8756       constant_expression_warning (size);
8757       pedantic = old_pedantic;
8758       flag_pedantic_errors = old_flag_pedantic_errors;
8759
8760       /* An array must have a positive number of elements.  */
8761       if (INT_CST_LT (size, integer_zero_node))
8762         {
8763           cp_error ("size of array `%D' is negative", name);
8764           size = integer_one_node;
8765         }
8766       /* Except that an extension we allow zero-sized arrays.  We
8767          always allow them in system headers because glibc uses 
8768          them.  */
8769       else if (integer_zerop (size) && pedantic && !in_system_header)
8770         cp_pedwarn ("ANSI C++ forbids zero-size array `%D'", name);
8771     }
8772
8773   /* Compute the index of the largest element in the array.  It is
8774      one less than the number of elements in the array.  */
8775   itype
8776     = fold (build_binary_op (MINUS_EXPR,
8777                              cp_convert (ssizetype, size),
8778                              cp_convert (ssizetype,
8779                                          integer_one_node)));
8780   
8781   /* Check for variable-sized arrays.  We allow such things as an
8782      extension, even though they are not allowed in ANSI/ISO C++.  */
8783   if (!TREE_CONSTANT (itype))
8784     {
8785       if (pedantic)
8786         {
8787           if (name)
8788             cp_pedwarn ("ANSI C++ forbids variable-size array `%D'",
8789                         name);
8790           else
8791             cp_pedwarn ("ANSI C++ forbids variable-size array");
8792         }
8793
8794       /* Create a variable-sized array index type.  */
8795       itype = variable_size (itype);
8796     }
8797   /* Make sure that there was no overflow when creating to a signed
8798      index type.  (For example, on a 32-bit machine, an array with
8799      size 2^32 - 1 is too big.)  */
8800   else if (TREE_OVERFLOW (itype))
8801     {
8802       error ("overflow in array dimension");
8803       TREE_OVERFLOW (itype) = 0;
8804     }
8805   
8806   /* Create and return the appropriate index type.  */
8807   return build_index_type (itype);
8808 }
8809
8810 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
8811    indicated TYPE.  If non-NULL, NAME is the NAME of the declaration
8812    with this type.  */
8813
8814 static tree
8815 create_array_type_for_decl (name, type, size)
8816      tree name;
8817      tree type;
8818      tree size;
8819 {
8820   tree itype = NULL_TREE;
8821   const char* error_msg;
8822
8823   /* If things have already gone awry, bail now.  */
8824   if (type == error_mark_node || size == error_mark_node)
8825     return error_mark_node;
8826
8827   /* Assume that everything will go OK.  */
8828   error_msg = NULL;
8829
8830   /* There are some types which cannot be array elements.  */
8831   switch (TREE_CODE (type))
8832     {
8833     case VOID_TYPE:
8834       error_msg = "array of void";
8835       break;
8836
8837     case FUNCTION_TYPE:
8838       error_msg = "array of functions";
8839       break;
8840
8841     case REFERENCE_TYPE:
8842       error_msg = "array of references";
8843       break;
8844
8845     case OFFSET_TYPE:
8846       error_msg = "array of data members";
8847       break;
8848
8849     case METHOD_TYPE:
8850       error_msg = "array of function members";
8851       break;
8852
8853     default:
8854       break;
8855     }
8856
8857   /* If something went wrong, issue an error-message and return.  */
8858   if (error_msg)
8859     {
8860       if (name)
8861         cp_error ("declaration of `%D' as %s", name, error_msg);
8862       else
8863         cp_error ("creating %s", error_msg);
8864
8865       return error_mark_node;
8866     }
8867
8868   /* [dcl.array]
8869      
8870      The constant expressions that specify the bounds of the arrays
8871      can be omitted only for the first member of the sequence.  */
8872   if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
8873     {
8874       cp_error ("declaration of `%D' as multidimensional array",
8875                 name);
8876       cp_error ("must have bounds for all dimensions except the first");
8877
8878       return error_mark_node;
8879     }
8880
8881   /* Figure out the index type for the array.  */
8882   if (size)
8883     itype = compute_array_index_type (name, size);
8884
8885   return build_cplus_array_type (type, itype);
8886 }
8887
8888 /* Given declspecs and a declarator,
8889    determine the name and type of the object declared
8890    and construct a ..._DECL node for it.
8891    (In one case we can return a ..._TYPE node instead.
8892     For invalid input we sometimes return 0.)
8893
8894    DECLSPECS is a chain of tree_list nodes whose value fields
8895     are the storage classes and type specifiers.
8896
8897    DECL_CONTEXT says which syntactic context this declaration is in:
8898      NORMAL for most contexts.  Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
8899      FUNCDEF for a function definition.  Like NORMAL but a few different
8900       error messages in each case.  Return value may be zero meaning
8901       this definition is too screwy to try to parse.
8902      MEMFUNCDEF for a function definition.  Like FUNCDEF but prepares to
8903       handle member functions (which have FIELD context).
8904       Return value may be zero meaning this definition is too screwy to
8905       try to parse.
8906      PARM for a parameter declaration (either within a function prototype
8907       or before a function body).  Make a PARM_DECL, or return void_type_node.
8908      CATCHPARM for a parameter declaration before a catch clause.
8909      TYPENAME if for a typename (in a cast or sizeof).
8910       Don't make a DECL node; just return the ..._TYPE node.
8911      FIELD for a struct or union field; make a FIELD_DECL.
8912      BITFIELD for a field with specified width.
8913    INITIALIZED is 1 if the decl has an initializer.
8914
8915    ATTRLIST is a TREE_LIST node with prefix attributes in TREE_VALUE and
8916    normal attributes in TREE_PURPOSE, or NULL_TREE.
8917
8918    In the TYPENAME case, DECLARATOR is really an absolute declarator.
8919    It may also be so in the PARM case, for a prototype where the
8920    argument type is specified but not the name.
8921
8922    This function is where the complicated C meanings of `static'
8923    and `extern' are interpreted.
8924
8925    For C++, if there is any monkey business to do, the function which
8926    calls this one must do it, i.e., prepending instance variables,
8927    renaming overloaded function names, etc.
8928
8929    Note that for this C++, it is an error to define a method within a class
8930    which does not belong to that class.
8931
8932    Except in the case where SCOPE_REFs are implicitly known (such as
8933    methods within a class being redundantly qualified),
8934    declarations which involve SCOPE_REFs are returned as SCOPE_REFs
8935    (class_name::decl_name).  The caller must also deal with this.
8936
8937    If a constructor or destructor is seen, and the context is FIELD,
8938    then the type gains the attribute TREE_HAS_x.  If such a declaration
8939    is erroneous, NULL_TREE is returned.
8940
8941    QUALS is used only for FUNCDEF and MEMFUNCDEF cases.  For a member
8942    function, these are the qualifiers to give to the `this' pointer. We
8943    apply TYPE_QUAL_RESTRICT to the this ptr, not the object.
8944
8945    May return void_type_node if the declarator turned out to be a friend.
8946    See grokfield for details.  */
8947
8948 enum return_types { return_normal, return_ctor, return_dtor, return_conversion };
8949
8950 tree
8951 grokdeclarator (declarator, declspecs, decl_context, initialized, attrlist)
8952      tree declspecs;
8953      tree declarator;
8954      enum decl_context decl_context;
8955      int initialized;
8956      tree attrlist;
8957 {
8958   RID_BIT_TYPE specbits;
8959   int nclasses = 0;
8960   tree spec;
8961   tree type = NULL_TREE;
8962   int longlong = 0;
8963   int constp;
8964   int restrictp;
8965   int volatilep;
8966   int type_quals;
8967   int virtualp, explicitp, friendp, inlinep, staticp;
8968   int explicit_int = 0;
8969   int explicit_char = 0;
8970   int defaulted_int = 0;
8971   tree typedef_decl = NULL_TREE;
8972   const char *name;
8973   tree typedef_type = NULL_TREE;
8974   int funcdef_flag = 0;
8975   enum tree_code innermost_code = ERROR_MARK;
8976   int bitfield = 0;
8977 #if 0
8978   /* See the code below that used this.  */
8979   tree decl_machine_attr = NULL_TREE;
8980 #endif
8981   /* Set this to error_mark_node for FIELD_DECLs we could not handle properly.
8982      All FIELD_DECLs we build here have `init' put into their DECL_INITIAL.  */
8983   tree init = NULL_TREE;
8984
8985   /* Keep track of what sort of function is being processed
8986      so that we can warn about default return values, or explicit
8987      return values which do not match prescribed defaults.  */
8988   enum return_types return_type = return_normal;
8989
8990   tree dname = NULL_TREE;
8991   tree ctype = current_class_type;
8992   tree ctor_return_type = NULL_TREE;
8993   enum overload_flags flags = NO_SPECIAL;
8994   tree quals = NULL_TREE;
8995   tree raises = NULL_TREE;
8996   int template_count = 0;
8997   tree in_namespace = NULL_TREE;
8998   tree inner_attrs;
8999   int ignore_attrs;
9000
9001   RIDBIT_RESET_ALL (specbits);
9002   if (decl_context == FUNCDEF)
9003     funcdef_flag = 1, decl_context = NORMAL;
9004   else if (decl_context == MEMFUNCDEF)
9005     funcdef_flag = -1, decl_context = FIELD;
9006   else if (decl_context == BITFIELD)
9007     bitfield = 1, decl_context = FIELD;
9008
9009   /* Look inside a declarator for the name being declared
9010      and get it as a string, for an error message.  */
9011   {
9012     tree *next = &declarator;
9013     register tree decl;
9014     name = NULL;
9015
9016     while (next && *next)
9017       {
9018         decl = *next;
9019         switch (TREE_CODE (decl))
9020           {
9021           case TREE_LIST:
9022             /* For attributes.  */
9023             next = &TREE_VALUE (decl);
9024             break;
9025
9026           case COND_EXPR:
9027             ctype = NULL_TREE;
9028             next = &TREE_OPERAND (decl, 0);
9029             break;
9030
9031           case BIT_NOT_EXPR:    /* For C++ destructors!  */
9032             {
9033               tree name = TREE_OPERAND (decl, 0);
9034               tree rename = NULL_TREE;
9035
9036               my_friendly_assert (flags == NO_SPECIAL, 152);
9037               flags = DTOR_FLAG;
9038               return_type = return_dtor;
9039               if (TREE_CODE (name) == TYPE_DECL)
9040                 TREE_OPERAND (decl, 0) = name = constructor_name (name);
9041               my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 153);
9042               if (ctype == NULL_TREE)
9043                 {
9044                   if (current_class_type == NULL_TREE)
9045                     {
9046                       error ("destructors must be member functions");
9047                       flags = NO_SPECIAL;
9048                     }
9049                   else
9050                     {
9051                       tree t = constructor_name (current_class_name);
9052                       if (t != name)
9053                         rename = t;
9054                     }
9055                 }
9056               else
9057                 {
9058                   tree t = constructor_name (ctype);
9059                   if (t != name)
9060                     rename = t;
9061                 }
9062
9063               if (rename)
9064                 {
9065                   cp_error ("destructor `%T' must match class name `%T'",
9066                             name, rename);
9067                   TREE_OPERAND (decl, 0) = rename;
9068                 }
9069               next = &name;
9070             }
9071             break;
9072
9073           case ADDR_EXPR:       /* C++ reference declaration */
9074             /* Fall through. */
9075           case ARRAY_REF:
9076           case INDIRECT_REF:
9077             ctype = NULL_TREE;
9078             innermost_code = TREE_CODE (decl);
9079             next = &TREE_OPERAND (decl, 0);
9080             break;
9081
9082           case CALL_EXPR:
9083             if (parmlist_is_exprlist (CALL_DECLARATOR_PARMS (decl)))
9084               {
9085                 /* This is actually a variable declaration using
9086                    constructor syntax.  We need to call start_decl and
9087                    cp_finish_decl so we can get the variable
9088                    initialized...  */
9089
9090                 tree attributes, prefix_attributes;
9091
9092                 *next = TREE_OPERAND (decl, 0);
9093                 init = CALL_DECLARATOR_PARMS (decl);
9094
9095                 if (attrlist)
9096                   {
9097                     attributes = TREE_PURPOSE (attrlist);
9098                     prefix_attributes = TREE_VALUE (attrlist);
9099                   }
9100                 else
9101                   {
9102                     attributes = NULL_TREE;
9103                     prefix_attributes = NULL_TREE;
9104                   }
9105
9106                 decl = start_decl (declarator, declspecs, 1,
9107                                    attributes, prefix_attributes);
9108                 if (decl)
9109                   {
9110                     /* Look for __unused__ attribute */
9111                     if (TREE_USED (TREE_TYPE (decl)))
9112                       TREE_USED (decl) = 1;
9113                     finish_decl (decl, init, NULL_TREE);
9114                   }
9115                 else
9116                   cp_error ("invalid declarator");
9117                 return 0;
9118               }
9119             innermost_code = TREE_CODE (decl);
9120             if (decl_context == FIELD && ctype == NULL_TREE)
9121               ctype = current_class_type;
9122             if (ctype
9123                 && TREE_OPERAND (decl, 0)
9124                 && (TREE_CODE (TREE_OPERAND (decl, 0)) == TYPE_DECL
9125                     && ((DECL_NAME (TREE_OPERAND (decl, 0))
9126                          == constructor_name_full (ctype))
9127                         || (DECL_NAME (TREE_OPERAND (decl, 0))
9128                             == constructor_name (ctype)))))
9129               TREE_OPERAND (decl, 0) = constructor_name (ctype);
9130             next = &TREE_OPERAND (decl, 0);
9131             decl = *next;
9132             if (ctype != NULL_TREE
9133                 && decl != NULL_TREE && flags != DTOR_FLAG
9134                 && decl == constructor_name (ctype))
9135               {
9136                 return_type = return_ctor;
9137                 ctor_return_type = ctype;
9138               }
9139             ctype = NULL_TREE;
9140             break;
9141             
9142           case TEMPLATE_ID_EXPR:
9143               {
9144                 tree fns = TREE_OPERAND (decl, 0);
9145
9146                 if (TREE_CODE (fns) == LOOKUP_EXPR)
9147                   fns = TREE_OPERAND (fns, 0);
9148
9149                 dname = fns;
9150                 if (TREE_CODE (dname) == COMPONENT_REF)
9151                   dname = TREE_OPERAND (dname, 1);
9152                 if (TREE_CODE (dname) != IDENTIFIER_NODE)
9153                   {
9154                     my_friendly_assert (is_overloaded_fn (dname),
9155                                         19990331);
9156                     dname = DECL_NAME (get_first_fn (dname));
9157                   }
9158               }
9159           /* Fall through. */
9160
9161           case IDENTIFIER_NODE:
9162             if (TREE_CODE (decl) == IDENTIFIER_NODE)
9163               dname = decl;
9164
9165             next = 0;
9166
9167             if (is_rid (dname))
9168               {
9169                 cp_error ("declarator-id missing; using reserved word `%D'",
9170                           dname);
9171                 name = IDENTIFIER_POINTER (dname);
9172               }
9173             if (! IDENTIFIER_OPNAME_P (dname)
9174                 /* GNU/Linux headers use '__op'.  Arrgh.  */
9175                 || (IDENTIFIER_TYPENAME_P (dname) && ! TREE_TYPE (dname)))
9176               name = IDENTIFIER_POINTER (dname);
9177             else
9178               {
9179                 if (IDENTIFIER_TYPENAME_P (dname))
9180                   {
9181                     my_friendly_assert (flags == NO_SPECIAL, 154);
9182                     flags = TYPENAME_FLAG;
9183                     ctor_return_type = TREE_TYPE (dname);
9184                     return_type = return_conversion;
9185                   }
9186                 name = operator_name_string (dname);
9187               }
9188             break;
9189
9190             /* C++ extension */
9191           case SCOPE_REF:
9192             {
9193               /* Perform error checking, and decide on a ctype.  */
9194               tree cname = TREE_OPERAND (decl, 0);
9195               if (cname == NULL_TREE)
9196                 ctype = NULL_TREE;
9197               else if (TREE_CODE (cname) == NAMESPACE_DECL)
9198                 {
9199                   ctype = NULL_TREE;
9200                   in_namespace = TREE_OPERAND (decl, 0);
9201                   TREE_OPERAND (decl, 0) = NULL_TREE;
9202                 }
9203               else if (! is_aggr_type (cname, 1))
9204                 TREE_OPERAND (decl, 0) = NULL_TREE;
9205               /* Must test TREE_OPERAND (decl, 1), in case user gives
9206                  us `typedef (class::memfunc)(int); memfunc *memfuncptr;'  */
9207               else if (TREE_OPERAND (decl, 1)
9208                        && TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)
9209                 ctype = cname;
9210               else if (TREE_CODE (cname) == TEMPLATE_TYPE_PARM
9211                        || TREE_CODE (cname) == TEMPLATE_TEMPLATE_PARM)
9212                 {
9213                   cp_error ("`%T::%D' is not a valid declarator", cname,
9214                             TREE_OPERAND (decl, 1));
9215                   cp_error ("  perhaps you want `typename %T::%D' to make it a type",
9216                             cname, TREE_OPERAND (decl, 1));
9217                   return void_type_node;
9218                 }
9219               else if (ctype == NULL_TREE)
9220                 ctype = cname;
9221               else if (TREE_COMPLEXITY (decl) == current_class_depth)
9222                 TREE_OPERAND (decl, 0) = ctype;
9223               else
9224                 {
9225                   if (! UNIQUELY_DERIVED_FROM_P (cname, ctype))
9226                     {
9227                       cp_error ("type `%T' is not derived from type `%T'",
9228                                 cname, ctype);
9229                       TREE_OPERAND (decl, 0) = NULL_TREE;
9230                     }
9231                   else
9232                     ctype = cname;
9233                 }
9234
9235               if (ctype && TREE_CODE (TREE_OPERAND (decl, 1)) == TYPE_DECL
9236                   && ((DECL_NAME (TREE_OPERAND (decl, 1))
9237                        == constructor_name_full (ctype))
9238                       || (DECL_NAME (TREE_OPERAND (decl, 1))
9239                           == constructor_name (ctype))))
9240                 TREE_OPERAND (decl, 1) = constructor_name (ctype);
9241               next = &TREE_OPERAND (decl, 1);
9242               decl = *next;
9243               if (ctype)
9244                 {
9245                   if (TREE_CODE (decl) == IDENTIFIER_NODE
9246                       && constructor_name (ctype) == decl)
9247                     {
9248                       return_type = return_ctor;
9249                       ctor_return_type = ctype;
9250                     }
9251                   else if (TREE_CODE (decl) == BIT_NOT_EXPR
9252                            && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
9253                            && (constructor_name (ctype) == TREE_OPERAND (decl, 0)
9254                                || constructor_name_full (ctype) == TREE_OPERAND (decl, 0)))
9255                     {
9256                       return_type = return_dtor;
9257                       ctor_return_type = ctype;
9258                       flags = DTOR_FLAG;
9259                       TREE_OPERAND (decl, 0) = constructor_name (ctype);
9260                       next = &TREE_OPERAND (decl, 0);
9261                     }
9262                 }
9263             }
9264             break;
9265
9266           case ERROR_MARK:
9267             next = 0;
9268             break;
9269
9270           case TYPE_DECL:
9271             /* Parse error puts this typespec where
9272                a declarator should go.  */
9273             cp_error ("`%T' specified as declarator-id", DECL_NAME (decl));
9274             if (TREE_TYPE (decl) == current_class_type)
9275               cp_error ("  perhaps you want `%T' for a constructor",
9276                         current_class_name);
9277             dname = DECL_NAME (decl);
9278             name = IDENTIFIER_POINTER (dname);
9279
9280             /* Avoid giving two errors for this.  */
9281             IDENTIFIER_CLASS_VALUE (dname) = NULL_TREE;
9282
9283             declspecs = temp_tree_cons (NULL_TREE, integer_type_node,
9284                                         declspecs);
9285             *next = dname;
9286             next = 0;
9287             break;
9288
9289           default:
9290             cp_compiler_error ("`%D' as declarator", decl);
9291             return 0; /* We used to do a 155 abort here.  */
9292           }
9293       }
9294   }
9295
9296   /* A function definition's declarator must have the form of
9297      a function declarator.  */
9298
9299   if (funcdef_flag && innermost_code != CALL_EXPR)
9300     return 0;
9301
9302   if (((dname && IDENTIFIER_OPNAME_P (dname)) || flags == TYPENAME_FLAG)
9303       && innermost_code != CALL_EXPR
9304       && ! (ctype && declspecs == NULL_TREE))
9305     {
9306       cp_error ("declaration of `%D' as non-function", dname);
9307       return void_type_node;
9308     }
9309
9310   /* Anything declared one level down from the top level
9311      must be one of the parameters of a function
9312      (because the body is at least two levels down).  */
9313
9314   /* This heuristic cannot be applied to C++ nodes! Fixed, however,
9315      by not allowing C++ class definitions to specify their parameters
9316      with xdecls (must be spec.d in the parmlist).
9317
9318      Since we now wait to push a class scope until we are sure that
9319      we are in a legitimate method context, we must set oldcname
9320      explicitly (since current_class_name is not yet alive).
9321
9322      We also want to avoid calling this a PARM if it is in a namespace.  */
9323
9324   if (decl_context == NORMAL && !toplevel_bindings_p ())
9325     {
9326       struct binding_level *b = current_binding_level;
9327       current_binding_level = b->level_chain;
9328       if (current_binding_level != 0 && toplevel_bindings_p ())
9329         decl_context = PARM;
9330       current_binding_level = b;
9331     }
9332
9333   if (name == NULL)
9334     name = decl_context == PARM ? "parameter" : "type name";
9335   
9336   /* Look through the decl specs and record which ones appear.
9337      Some typespecs are defined as built-in typenames.
9338      Others, the ones that are modifiers of other types,
9339      are represented by bits in SPECBITS: set the bits for
9340      the modifiers that appear.  Storage class keywords are also in SPECBITS.
9341
9342      If there is a typedef name or a type, store the type in TYPE.
9343      This includes builtin typedefs such as `int'.
9344
9345      Set EXPLICIT_INT if the type is `int' or `char' and did not
9346      come from a user typedef.
9347
9348      Set LONGLONG if `long' is mentioned twice.
9349
9350      For C++, constructors and destructors have their own fast treatment.  */
9351
9352   for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
9353     {
9354       register int i;
9355       register tree id;
9356
9357       /* Certain parse errors slip through.  For example,
9358          `int class;' is not caught by the parser. Try
9359          weakly to recover here.  */
9360       if (TREE_CODE (spec) != TREE_LIST)
9361         return 0;
9362
9363       id = TREE_VALUE (spec);
9364
9365       if (TREE_CODE (id) == IDENTIFIER_NODE)
9366         {
9367           if (id == ridpointers[(int) RID_INT]
9368               || id == ridpointers[(int) RID_CHAR]
9369               || id == ridpointers[(int) RID_BOOL]
9370               || id == ridpointers[(int) RID_WCHAR])
9371             {
9372               if (type)
9373                 {
9374                   if (id == ridpointers[(int) RID_BOOL])
9375                     error ("`bool' is now a keyword");
9376                   else
9377                     cp_error ("extraneous `%T' ignored", id);
9378                 }
9379               else
9380                 {
9381                   if (id == ridpointers[(int) RID_INT])
9382                     explicit_int = 1;
9383                   else if (id == ridpointers[(int) RID_CHAR])
9384                     explicit_char = 1;
9385                   type = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (id));
9386                 }
9387               goto found;
9388             }
9389           /* C++ aggregate types.  */
9390           if (IDENTIFIER_HAS_TYPE_VALUE (id))
9391             {
9392               if (type)
9393                 cp_error ("multiple declarations `%T' and `%T'", type, id);
9394               else
9395                 type = IDENTIFIER_TYPE_VALUE (id);
9396               goto found;
9397             }
9398
9399           for (i = (int) RID_FIRST_MODIFIER; i <= (int) RID_LAST_MODIFIER; i++)
9400             {
9401               if (ridpointers[i] == id)
9402                 {
9403                   if (i == (int) RID_LONG && RIDBIT_SETP (i, specbits))
9404                     {
9405                       if (pedantic && ! in_system_header && warn_long_long)
9406                         pedwarn ("ANSI C++ does not support `long long'");
9407                       if (longlong)
9408                         error ("`long long long' is too long for GCC");
9409                       else
9410                         longlong = 1;
9411                     }
9412                   else if (RIDBIT_SETP (i, specbits))
9413                     pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
9414                   RIDBIT_SET (i, specbits);
9415                   goto found;
9416                 }
9417             }
9418         }
9419       /* C++ aggregate types.  */
9420       else if (TREE_CODE (id) == TYPE_DECL || TREE_CODE (id) == TEMPLATE_DECL)
9421         {
9422           if (type)
9423             cp_error ("multiple declarations `%T' and `%T'", type,
9424                       TREE_TYPE (id));
9425           else
9426             {
9427               type = TREE_TYPE (id);
9428               TREE_VALUE (spec) = type;
9429             }
9430           goto found;
9431         }
9432       if (type)
9433         error ("two or more data types in declaration of `%s'", name);
9434       else if (TREE_CODE (id) == IDENTIFIER_NODE)
9435         {
9436           register tree t = lookup_name (id, 1);
9437           if (!t || TREE_CODE (t) != TYPE_DECL)
9438             error ("`%s' fails to be a typedef or built in type",
9439                    IDENTIFIER_POINTER (id));
9440           else
9441             {
9442               type = TREE_TYPE (t);
9443 #if 0
9444               /* See the code below that used this.  */
9445               decl_machine_attr = DECL_MACHINE_ATTRIBUTES (id);
9446 #endif
9447               typedef_decl = t;
9448             }
9449         }
9450       else if (id != error_mark_node)
9451         /* Can't change CLASS nodes into RECORD nodes here!  */
9452         type = id;
9453
9454     found: ;
9455     }
9456
9457   typedef_type = type;
9458
9459   /* No type at all: default to `int', and set DEFAULTED_INT
9460      because it was not a user-defined typedef.  */
9461
9462   if (type == NULL_TREE
9463       && (RIDBIT_SETP (RID_SIGNED, specbits)
9464           || RIDBIT_SETP (RID_UNSIGNED, specbits)
9465           || RIDBIT_SETP (RID_LONG, specbits)
9466           || RIDBIT_SETP (RID_SHORT, specbits)))
9467     {
9468       /* These imply 'int'.  */
9469       type = integer_type_node;
9470       defaulted_int = 1;
9471     }
9472
9473   if (type == NULL_TREE)
9474     {
9475       explicit_int = -1;
9476       if (return_type == return_dtor)
9477         type = void_type_node;
9478       else if (return_type == return_ctor)
9479         type = build_pointer_type (ctor_return_type);
9480       else if (return_type == return_conversion)
9481         type = ctor_return_type;
9482       else
9483         {
9484           /* We handle `main' specially here, because 'main () { }' is so
9485              common.  With no options, it is allowed.  With -Wreturn-type,
9486              it is a warning.  It is only an error with -pedantic-errors.  */
9487           int is_main = (funcdef_flag
9488                          && MAIN_NAME_P (dname)
9489                          && ctype == NULL_TREE
9490                          && in_namespace == NULL_TREE
9491                          && current_namespace == global_namespace);
9492
9493           if (in_system_header || flag_ms_extensions)
9494             /* Allow it, sigh.  */;
9495           else if (pedantic || ! is_main)
9496             cp_pedwarn ("ANSI C++ forbids declaration of `%s' with no type",
9497                         name);
9498           else if (warn_return_type)
9499             cp_warning ("ANSI C++ forbids declaration of `%s' with no type",
9500                         name);
9501
9502           type = integer_type_node;
9503         }
9504     }
9505   else if (return_type == return_dtor)
9506     {
9507       error ("return type specification for destructor invalid");
9508       type = void_type_node;
9509     }
9510   else if (return_type == return_ctor)
9511     {
9512       error ("return type specification for constructor invalid");
9513       type = build_pointer_type (ctor_return_type);
9514     }
9515   else if (return_type == return_conversion)
9516     {
9517       if (!same_type_p (type, ctor_return_type))
9518         cp_error ("operator `%T' declared to return `%T'",
9519                   ctor_return_type, type);
9520       else
9521         cp_pedwarn ("return type specified for `operator %T'",
9522                     ctor_return_type);
9523
9524       type = ctor_return_type;
9525     }
9526
9527   ctype = NULL_TREE;
9528
9529   /* Now process the modifiers that were specified
9530      and check for invalid combinations.  */
9531
9532   /* Long double is a special combination.  */
9533
9534   if (RIDBIT_SETP (RID_LONG, specbits)
9535       && TYPE_MAIN_VARIANT (type) == double_type_node)
9536     {
9537       RIDBIT_RESET (RID_LONG, specbits);
9538       type = build_qualified_type (long_double_type_node, 
9539                                    CP_TYPE_QUALS (type));
9540     }
9541
9542   /* Check all other uses of type modifiers.  */
9543
9544   if (RIDBIT_SETP (RID_UNSIGNED, specbits)
9545       || RIDBIT_SETP (RID_SIGNED, specbits)
9546       || RIDBIT_SETP (RID_LONG, specbits)
9547       || RIDBIT_SETP (RID_SHORT, specbits))
9548     {
9549       int ok = 0;
9550
9551       if (TREE_CODE (type) == REAL_TYPE)
9552         error ("short, signed or unsigned invalid for `%s'", name);
9553       else if (TREE_CODE (type) != INTEGER_TYPE)
9554         error ("long, short, signed or unsigned invalid for `%s'", name);
9555       else if (RIDBIT_SETP (RID_LONG, specbits)
9556                && RIDBIT_SETP (RID_SHORT, specbits))
9557         error ("long and short specified together for `%s'", name);
9558       else if ((RIDBIT_SETP (RID_LONG, specbits)
9559                 || RIDBIT_SETP (RID_SHORT, specbits))
9560                && explicit_char)
9561         error ("long or short specified with char for `%s'", name);
9562       else if ((RIDBIT_SETP (RID_LONG, specbits)
9563                 || RIDBIT_SETP (RID_SHORT, specbits))
9564                && TREE_CODE (type) == REAL_TYPE)
9565         error ("long or short specified with floating type for `%s'", name);
9566       else if (RIDBIT_SETP (RID_SIGNED, specbits)
9567                && RIDBIT_SETP (RID_UNSIGNED, specbits))
9568         error ("signed and unsigned given together for `%s'", name);
9569       else
9570         {
9571           ok = 1;
9572           if (!explicit_int && !defaulted_int && !explicit_char && pedantic)
9573             {
9574               pedwarn ("long, short, signed or unsigned used invalidly for `%s'",
9575                        name);
9576               if (flag_pedantic_errors)
9577                 ok = 0;
9578             }
9579         }
9580
9581       /* Discard the type modifiers if they are invalid.  */
9582       if (! ok)
9583         {
9584           RIDBIT_RESET (RID_UNSIGNED, specbits);
9585           RIDBIT_RESET (RID_SIGNED, specbits);
9586           RIDBIT_RESET (RID_LONG, specbits);
9587           RIDBIT_RESET (RID_SHORT, specbits);
9588           longlong = 0;
9589         }
9590     }
9591
9592   if (RIDBIT_SETP (RID_COMPLEX, specbits)
9593       && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
9594     {
9595       error ("complex invalid for `%s'", name);
9596       RIDBIT_RESET (RID_COMPLEX, specbits);
9597     }
9598
9599   /* Decide whether an integer type is signed or not.
9600      Optionally treat bitfields as signed by default.  */
9601   if (RIDBIT_SETP (RID_UNSIGNED, specbits)
9602       /* [class.bit]
9603
9604          It is implementation-defined whether a plain (neither
9605          explicitly signed or unsigned) char, short, int, or long
9606          bit-field is signed or unsigned.
9607              
9608          Naturally, we extend this to long long as well.  Note that
9609          this does not include wchar_t.  */
9610       || (bitfield && !flag_signed_bitfields
9611           && RIDBIT_NOTSETP (RID_SIGNED, specbits)
9612           /* A typedef for plain `int' without `signed' can be
9613              controlled just like plain `int', but a typedef for
9614              `signed int' cannot be so controlled.  */
9615           && !(typedef_decl 
9616                && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
9617           && (TREE_CODE (type) == INTEGER_TYPE
9618               || TREE_CODE (type) == CHAR_TYPE)
9619           && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
9620     {
9621       if (longlong)
9622         type = long_long_unsigned_type_node;
9623       else if (RIDBIT_SETP (RID_LONG, specbits))
9624         type = long_unsigned_type_node;
9625       else if (RIDBIT_SETP (RID_SHORT, specbits))
9626         type = short_unsigned_type_node;
9627       else if (type == char_type_node)
9628         type = unsigned_char_type_node;
9629       else if (typedef_decl)
9630         type = unsigned_type (type);
9631       else
9632         type = unsigned_type_node;
9633     }
9634   else if (RIDBIT_SETP (RID_SIGNED, specbits)
9635            && type == char_type_node)
9636     type = signed_char_type_node;
9637   else if (longlong)
9638     type = long_long_integer_type_node;
9639   else if (RIDBIT_SETP (RID_LONG, specbits))
9640     type = long_integer_type_node;
9641   else if (RIDBIT_SETP (RID_SHORT, specbits))
9642     type = short_integer_type_node;
9643
9644   if (RIDBIT_SETP (RID_COMPLEX, specbits))
9645     {
9646       /* If we just have "complex", it is equivalent to
9647          "complex double", but if any modifiers at all are specified it is
9648          the complex form of TYPE.  E.g, "complex short" is
9649          "complex short int".  */
9650
9651       if (defaulted_int && ! longlong
9652           && ! (RIDBIT_SETP (RID_LONG, specbits)
9653                 || RIDBIT_SETP (RID_SHORT, specbits)
9654                 || RIDBIT_SETP (RID_SIGNED, specbits)
9655                 || RIDBIT_SETP (RID_UNSIGNED, specbits)))
9656         type = complex_double_type_node;
9657       else if (type == integer_type_node)
9658         type = complex_integer_type_node;
9659       else if (type == float_type_node)
9660         type = complex_float_type_node;
9661       else if (type == double_type_node)
9662         type = complex_double_type_node;
9663       else if (type == long_double_type_node)
9664         type = complex_long_double_type_node;
9665       else
9666         type = build_complex_type (type);
9667     }
9668
9669   if (return_type == return_conversion 
9670       && (RIDBIT_SETP (RID_CONST, specbits)
9671           || RIDBIT_SETP (RID_VOLATILE, specbits)
9672           || RIDBIT_SETP (RID_RESTRICT, specbits)))
9673     cp_error ("qualifiers are not allowed on declaration of `operator %T'",
9674               ctor_return_type);
9675
9676   /* Set CONSTP if this declaration is `const', whether by
9677      explicit specification or via a typedef.
9678      Likewise for VOLATILEP.  */
9679
9680   constp = !! RIDBIT_SETP (RID_CONST, specbits) + CP_TYPE_CONST_P (type);
9681   restrictp = 
9682     !! RIDBIT_SETP (RID_RESTRICT, specbits) + CP_TYPE_RESTRICT_P (type);
9683   volatilep = 
9684     !! RIDBIT_SETP (RID_VOLATILE, specbits) + CP_TYPE_VOLATILE_P (type);
9685   type_quals = ((constp ? TYPE_QUAL_CONST : 0)
9686                 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
9687                 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
9688   type = cp_build_qualified_type (type, type_quals);
9689   staticp = 0;
9690   inlinep = !! RIDBIT_SETP (RID_INLINE, specbits);
9691   virtualp = RIDBIT_SETP (RID_VIRTUAL, specbits);
9692   RIDBIT_RESET (RID_VIRTUAL, specbits);
9693   explicitp = RIDBIT_SETP (RID_EXPLICIT, specbits) != 0;
9694   RIDBIT_RESET (RID_EXPLICIT, specbits);
9695
9696   if (RIDBIT_SETP (RID_STATIC, specbits))
9697     staticp = 1 + (decl_context == FIELD);
9698
9699   if (virtualp && staticp == 2)
9700     {
9701       cp_error ("member `%D' cannot be declared both virtual and static",
9702                 dname);
9703       staticp = 0;
9704     }
9705   friendp = RIDBIT_SETP (RID_FRIEND, specbits);
9706   RIDBIT_RESET (RID_FRIEND, specbits);
9707
9708   /* Warn if two storage classes are given. Default to `auto'.  */
9709
9710   if (RIDBIT_ANY_SET (specbits))
9711     {
9712       if (RIDBIT_SETP (RID_STATIC, specbits)) nclasses++;
9713       if (RIDBIT_SETP (RID_EXTERN, specbits)) nclasses++;
9714       if (decl_context == PARM && nclasses > 0)
9715         error ("storage class specifiers invalid in parameter declarations");
9716       if (RIDBIT_SETP (RID_TYPEDEF, specbits))
9717         {
9718           if (decl_context == PARM)
9719             error ("typedef declaration invalid in parameter declaration");
9720           nclasses++;
9721         }
9722       if (RIDBIT_SETP (RID_AUTO, specbits)) nclasses++;
9723       if (RIDBIT_SETP (RID_REGISTER, specbits)) nclasses++;
9724     }
9725
9726   /* Give error if `virtual' is used outside of class declaration.  */
9727   if (virtualp
9728       && (current_class_name == NULL_TREE || decl_context != FIELD))
9729     {
9730       error ("virtual outside class declaration");
9731       virtualp = 0;
9732     }
9733
9734   /* Static anonymous unions are dealt with here.  */
9735   if (staticp && decl_context == TYPENAME
9736       && TREE_CODE (declspecs) == TREE_LIST
9737       && ANON_AGGR_TYPE_P (TREE_VALUE (declspecs)))
9738     decl_context = FIELD;
9739
9740   /* Warn about storage classes that are invalid for certain
9741      kinds of declarations (parameters, typenames, etc.).  */
9742
9743   if (nclasses > 1)
9744     error ("multiple storage classes in declaration of `%s'", name);
9745   else if (decl_context != NORMAL && nclasses > 0)
9746     {
9747       if ((decl_context == PARM || decl_context == CATCHPARM)
9748           && (RIDBIT_SETP (RID_REGISTER, specbits)
9749               || RIDBIT_SETP (RID_AUTO, specbits)))
9750         ;
9751       else if (RIDBIT_SETP (RID_TYPEDEF, specbits))
9752         ;
9753       else if (decl_context == FIELD
9754                /* C++ allows static class elements  */
9755                && RIDBIT_SETP (RID_STATIC, specbits))
9756         /* C++ also allows inlines and signed and unsigned elements,
9757            but in those cases we don't come in here.  */
9758         ;
9759       else
9760         {
9761           if (decl_context == FIELD)
9762             {
9763               tree tmp = NULL_TREE;
9764               register int op = 0;
9765
9766               if (declarator)
9767                 {
9768                   /* Avoid trying to get an operand off an identifier node.  */ 
9769                   if (TREE_CODE (declarator) == IDENTIFIER_NODE)
9770                     tmp = declarator;
9771                   else
9772                     tmp = TREE_OPERAND (declarator, 0);
9773                   op = IDENTIFIER_OPNAME_P (tmp);
9774                 }
9775               error ("storage class specified for %s `%s'",
9776                      op ? "member operator" : "field",
9777                      op ? operator_name_string (tmp) : name);
9778             }
9779           else
9780             {
9781               if (decl_context == PARM || decl_context == CATCHPARM)
9782                 error ("storage class specified for parameter `%s'", name);
9783               else
9784                 error ("storage class specified for typename");
9785             }
9786           RIDBIT_RESET (RID_REGISTER, specbits);
9787           RIDBIT_RESET (RID_AUTO, specbits);
9788           RIDBIT_RESET (RID_EXTERN, specbits);
9789         }
9790     }
9791   else if (RIDBIT_SETP (RID_EXTERN, specbits) && initialized && !funcdef_flag)
9792     {
9793       if (toplevel_bindings_p ())
9794         {
9795           /* It's common practice (and completely valid) to have a const
9796              be initialized and declared extern.  */
9797           if (!(type_quals & TYPE_QUAL_CONST))
9798             warning ("`%s' initialized and declared `extern'", name);
9799         }
9800       else
9801         error ("`%s' has both `extern' and initializer", name);
9802     }
9803   else if (RIDBIT_SETP (RID_EXTERN, specbits) && funcdef_flag
9804            && ! toplevel_bindings_p ())
9805     error ("nested function `%s' declared `extern'", name);
9806   else if (toplevel_bindings_p ())
9807     {
9808       if (RIDBIT_SETP (RID_AUTO, specbits))
9809         error ("top-level declaration of `%s' specifies `auto'", name);
9810     }
9811
9812   if (nclasses > 0 && friendp)
9813     error ("storage class specifiers invalid in friend function declarations");
9814
9815   /* Now figure out the structure of the declarator proper.
9816      Descend through it, creating more complex types, until we reach
9817      the declared identifier (or NULL_TREE, in an absolute declarator).  */
9818
9819   inner_attrs = NULL_TREE;
9820   ignore_attrs = 0;  
9821
9822   while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE
9823          && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
9824     {
9825       /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
9826          an INDIRECT_REF (for *...),
9827          a CALL_EXPR (for ...(...)),
9828          an identifier (for the name being declared)
9829          or a null pointer (for the place in an absolute declarator
9830          where the name was omitted).
9831          For the last two cases, we have just exited the loop.
9832
9833          For C++ it could also be
9834          a SCOPE_REF (for class :: ...).  In this case, we have converted
9835          sensible names to types, and those are the values we use to
9836          qualify the member name.
9837          an ADDR_EXPR (for &...),
9838          a BIT_NOT_EXPR (for destructors)
9839
9840          At this point, TYPE is the type of elements of an array,
9841          or for a function to return, or for a pointer to point to.
9842          After this sequence of ifs, TYPE is the type of the
9843          array or function or pointer, and DECLARATOR has had its
9844          outermost layer removed.  */
9845
9846       if (type == error_mark_node)
9847         {
9848           if (TREE_CODE (declarator) == SCOPE_REF)
9849             declarator = TREE_OPERAND (declarator, 1);
9850           else
9851             declarator = TREE_OPERAND (declarator, 0);
9852           continue;
9853         }
9854       if (quals != NULL_TREE
9855           && (declarator == NULL_TREE
9856               || TREE_CODE (declarator) != SCOPE_REF))
9857         {
9858           if (ctype == NULL_TREE && TREE_CODE (type) == METHOD_TYPE)
9859             ctype = TYPE_METHOD_BASETYPE (type);
9860           if (ctype != NULL_TREE)
9861             {
9862               tree dummy = build_decl (TYPE_DECL, NULL_TREE, type);
9863               grok_method_quals (ctype, dummy, quals);
9864               type = TREE_TYPE (dummy);
9865               ctype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type)));
9866               quals = NULL_TREE;
9867             }
9868         }
9869
9870       /* See the comment for the TREE_LIST case, below.  */
9871       if (ignore_attrs)
9872         ignore_attrs = 0;
9873       else if (inner_attrs)
9874         {
9875           decl_attributes (type, inner_attrs, NULL_TREE);
9876           inner_attrs = NULL_TREE;
9877         }
9878
9879       switch (TREE_CODE (declarator))
9880         {
9881         case TREE_LIST:
9882           {
9883             /* We encode a declarator with embedded attributes using
9884                a TREE_LIST.  The attributes apply to the declarator
9885                directly inside them, so we have to skip an iteration
9886                before applying them to the type.  If the declarator just
9887                inside is the declarator-id, we apply the attrs to the
9888                decl itself.  */
9889             inner_attrs = TREE_PURPOSE (declarator);
9890             ignore_attrs = 1;
9891             declarator = TREE_VALUE (declarator);
9892           }
9893           break;
9894
9895         case ARRAY_REF:
9896           {
9897             register tree size;
9898
9899             size = TREE_OPERAND (declarator, 1);
9900
9901             /* VC++ spells a zero-sized array with [].  */
9902             if (size == NULL_TREE && decl_context == FIELD && ! staticp
9903                 && ! RIDBIT_SETP (RID_TYPEDEF, specbits))
9904               size = integer_zero_node;
9905
9906             declarator = TREE_OPERAND (declarator, 0);
9907
9908             type = create_array_type_for_decl (dname, type, size);
9909             ctype = NULL_TREE;
9910           }
9911           break;
9912
9913         case CALL_EXPR:
9914           {
9915             tree arg_types;
9916             int funcdecl_p;
9917             tree inner_parms = CALL_DECLARATOR_PARMS (declarator);
9918             tree inner_decl = TREE_OPERAND (declarator, 0);
9919
9920             /* Declaring a function type.
9921                Make sure we have a valid type for the function to return.  */
9922
9923             /* We now know that the TYPE_QUALS don't apply to the
9924                decl, but to its return type.  */
9925             type_quals = TYPE_UNQUALIFIED;
9926
9927             /* Warn about some types functions can't return.  */
9928
9929             if (TREE_CODE (type) == FUNCTION_TYPE)
9930               {
9931                 error ("`%s' declared as function returning a function", name);
9932                 type = integer_type_node;
9933               }
9934             if (TREE_CODE (type) == ARRAY_TYPE)
9935               {
9936                 error ("`%s' declared as function returning an array", name);
9937                 type = integer_type_node;
9938               }
9939
9940             if (inner_decl && TREE_CODE (inner_decl) == SCOPE_REF)
9941               inner_decl = TREE_OPERAND (inner_decl, 1);
9942
9943             if (inner_decl && TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR) 
9944               inner_decl = dname;
9945
9946             /* Pick up type qualifiers which should be applied to `this'.  */
9947             quals = CALL_DECLARATOR_QUALS (declarator);
9948
9949             /* Pick up the exception specifications.  */
9950             raises = CALL_DECLARATOR_EXCEPTION_SPEC (declarator);
9951
9952             /* Say it's a definition only for the CALL_EXPR
9953                closest to the identifier.  */
9954             funcdecl_p
9955               = inner_decl 
9956               && (TREE_CODE (inner_decl) == IDENTIFIER_NODE
9957                   || TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR 
9958                   || TREE_CODE (inner_decl) == BIT_NOT_EXPR);
9959             
9960             if (ctype == NULL_TREE
9961                 && decl_context == FIELD
9962                 && funcdecl_p
9963                 && (friendp == 0 || dname == current_class_name))
9964               ctype = current_class_type;
9965
9966             if (ctype && return_type == return_conversion)
9967               TYPE_HAS_CONVERSION (ctype) = 1;
9968             if (ctype && constructor_name (ctype) == dname)
9969               {
9970                 /* We are within a class's scope. If our declarator name
9971                    is the same as the class name, and we are defining
9972                    a function, then it is a constructor/destructor, and
9973                    therefore returns a void type.  */
9974
9975                 if (flags == DTOR_FLAG)
9976                   {
9977                     /* ANSI C++ June 5 1992 WP 12.4.1.  A destructor may
9978                        not be declared const or volatile.  A destructor
9979                        may not be static.  */
9980                     if (staticp == 2)
9981                       error ("destructor cannot be static member function");
9982                     if (quals)
9983                       {
9984                         cp_error ("destructors may not be `%s'",
9985                                   IDENTIFIER_POINTER (TREE_VALUE (quals)));
9986                         quals = NULL_TREE;
9987                       }
9988                     if (decl_context == FIELD)
9989                       {
9990                         if (! member_function_or_else (ctype, current_class_type,
9991                                                        "destructor for alien class `%s' cannot be a member"))
9992                           return void_type_node;
9993                       }
9994                   }
9995                 else            /* It's a constructor.  */
9996                   {
9997                     if (explicitp == 1)
9998                       explicitp = 2;
9999                     /* ANSI C++ June 5 1992 WP 12.1.2.  A constructor may
10000                        not be declared const or volatile.  A constructor may
10001                        not be virtual.  A constructor may not be static.  */
10002                     if (staticp == 2)
10003                       error ("constructor cannot be static member function");
10004                     if (virtualp)
10005                       {
10006                         pedwarn ("constructors cannot be declared virtual");
10007                         virtualp = 0;
10008                       }
10009                     if (quals)
10010                       {
10011                         cp_error ("constructors may not be `%s'",
10012                                   IDENTIFIER_POINTER (TREE_VALUE (quals)));
10013                         quals = NULL_TREE;
10014                       }
10015                     {
10016                       RID_BIT_TYPE tmp_bits;
10017                       bcopy ((void*)&specbits, (void*)&tmp_bits, sizeof (RID_BIT_TYPE));
10018                       RIDBIT_RESET (RID_INLINE, tmp_bits);
10019                       RIDBIT_RESET (RID_STATIC, tmp_bits);
10020                       if (RIDBIT_ANY_SET (tmp_bits))
10021                         error ("return value type specifier for constructor ignored");
10022                     }
10023                     type = build_pointer_type (ctype);
10024                     if (decl_context == FIELD)
10025                       {
10026                         if (! member_function_or_else (ctype, current_class_type,
10027                                                        "constructor for alien class `%s' cannot be member"))
10028                           return void_type_node;
10029                         TYPE_HAS_CONSTRUCTOR (ctype) = 1;
10030                         if (return_type != return_ctor)
10031                           return NULL_TREE;
10032                       }
10033                   }
10034                 if (decl_context == FIELD)
10035                   staticp = 0;
10036               }
10037             else if (friendp)
10038               {
10039                 if (initialized)
10040                   error ("can't initialize friend function `%s'", name);
10041                 if (virtualp)
10042                   {
10043                     /* Cannot be both friend and virtual.  */
10044                     error ("virtual functions cannot be friends");
10045                     RIDBIT_RESET (RID_FRIEND, specbits);
10046                     friendp = 0;
10047                   }
10048                 if (decl_context == NORMAL)
10049                   error ("friend declaration not in class definition");
10050                 if (current_function_decl && funcdef_flag)
10051                   cp_error ("can't define friend function `%s' in a local class definition",
10052                             name);
10053               }
10054
10055             /* Construct the function type and go to the next
10056                inner layer of declarator.  */
10057
10058             declarator = TREE_OPERAND (declarator, 0);
10059
10060             /* FIXME: This is where default args should be fully
10061                processed.  */
10062
10063             arg_types = grokparms (inner_parms, funcdecl_p ? funcdef_flag : 0);
10064
10065             if (declarator && flags == DTOR_FLAG)
10066               {
10067                 /* A destructor declared in the body of a class will
10068                    be represented as a BIT_NOT_EXPR.  But, we just
10069                    want the underlying IDENTIFIER.  */
10070                 if (TREE_CODE (declarator) == BIT_NOT_EXPR)
10071                   declarator = TREE_OPERAND (declarator, 0);
10072                 
10073                 if (strict_prototype == 0 && arg_types == NULL_TREE)
10074                   arg_types = void_list_node;
10075                 else if (arg_types == NULL_TREE
10076                          || arg_types != void_list_node)
10077                   {
10078                     cp_error ("destructors may not have parameters");
10079                     arg_types = void_list_node;
10080                     last_function_parms = NULL_TREE;
10081                   }
10082               }
10083
10084             /* ANSI says that `const int foo ();'
10085                does not make the function foo const.  */
10086             type = build_function_type (type, arg_types);
10087
10088             {
10089               tree t;
10090               for (t = arg_types; t; t = TREE_CHAIN (t))
10091                 if (TREE_PURPOSE (t)
10092                     && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
10093                   {
10094                     add_defarg_fn (type);
10095                     break;
10096                   }
10097             }
10098           }
10099           break;
10100
10101         case ADDR_EXPR:
10102         case INDIRECT_REF:
10103           /* Filter out pointers-to-references and references-to-references.
10104              We can get these if a TYPE_DECL is used.  */
10105
10106           if (TREE_CODE (type) == REFERENCE_TYPE)
10107             {
10108               error ("cannot declare %s to references",
10109                      TREE_CODE (declarator) == ADDR_EXPR
10110                      ? "references" : "pointers");
10111               declarator = TREE_OPERAND (declarator, 0);
10112               continue;
10113             }
10114
10115           if (TREE_CODE (type) == OFFSET_TYPE
10116               && (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE
10117                   || TREE_CODE (TREE_TYPE (type)) == REFERENCE_TYPE))
10118             {
10119               cp_error ("cannot declare pointer to `%#T' member",
10120                         TREE_TYPE (type));
10121               type = TREE_TYPE (type);
10122             }
10123
10124           /* Merge any constancy or volatility into the target type
10125              for the pointer.  */
10126
10127           /* We now know that the TYPE_QUALS don't apply to the decl,
10128              but to the target of the pointer.  */
10129           type_quals = TYPE_UNQUALIFIED;
10130
10131           if (TREE_CODE (declarator) == ADDR_EXPR)
10132             {
10133               if (TREE_CODE (type) == VOID_TYPE)
10134                 error ("invalid type: `void &'");
10135               else
10136                 type = build_reference_type (type);
10137             }
10138           else if (TREE_CODE (type) == METHOD_TYPE)
10139             type = build_ptrmemfunc_type (build_pointer_type (type));
10140           else
10141             type = build_pointer_type (type);
10142
10143           /* Process a list of type modifier keywords (such as
10144              const or volatile) that were given inside the `*' or `&'.  */
10145
10146           if (TREE_TYPE (declarator))
10147             {
10148               register tree typemodlist;
10149               int erred = 0;
10150
10151               constp = 0;
10152               volatilep = 0;
10153               restrictp = 0;
10154               for (typemodlist = TREE_TYPE (declarator); typemodlist;
10155                    typemodlist = TREE_CHAIN (typemodlist))
10156                 {
10157                   tree qualifier = TREE_VALUE (typemodlist);
10158
10159                   if (qualifier == ridpointers[(int) RID_CONST])
10160                     constp++;
10161                   else if (qualifier == ridpointers[(int) RID_VOLATILE])
10162                     volatilep++;
10163                   else if (qualifier == ridpointers[(int) RID_RESTRICT])
10164                     restrictp++;
10165                   else if (!erred)
10166                     {
10167                       erred = 1;
10168                       error ("invalid type modifier within pointer declarator");
10169                     }
10170                 }
10171               if (constp > 1)
10172                 pedwarn ("duplicate `const'");
10173               if (volatilep > 1)
10174                 pedwarn ("duplicate `volatile'");
10175               if (restrictp > 1)
10176                 pedwarn ("duplicate `restrict'");
10177
10178               type_quals = ((constp ? TYPE_QUAL_CONST : 0)
10179                             | (restrictp ? TYPE_QUAL_RESTRICT : 0)
10180                             | (volatilep ? TYPE_QUAL_VOLATILE : 0));
10181               if (TREE_CODE (declarator) == ADDR_EXPR
10182                   && (constp || volatilep))
10183                 {
10184                   if (constp)
10185                     pedwarn ("discarding `const' applied to a reference");
10186                   if (volatilep)
10187                     pedwarn ("discarding `volatile' applied to a reference");
10188                   type_quals &= ~(TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
10189                 }
10190               type = cp_build_qualified_type (type, type_quals);
10191             }
10192           declarator = TREE_OPERAND (declarator, 0);
10193           ctype = NULL_TREE;
10194           break;
10195
10196         case SCOPE_REF:
10197           {
10198             /* We have converted type names to NULL_TREE if the
10199                name was bogus, or to a _TYPE node, if not.
10200
10201                The variable CTYPE holds the type we will ultimately
10202                resolve to.  The code here just needs to build
10203                up appropriate member types.  */
10204             tree sname = TREE_OPERAND (declarator, 1);
10205             tree t;
10206
10207             /* Destructors can have their visibilities changed as well.  */
10208             if (TREE_CODE (sname) == BIT_NOT_EXPR)
10209               sname = TREE_OPERAND (sname, 0);
10210
10211             if (TREE_COMPLEXITY (declarator) == 0)
10212               /* This needs to be here, in case we are called
10213                  multiple times.  */ ;
10214             else if (TREE_COMPLEXITY (declarator) == -1)
10215               /* Namespace member. */
10216               pop_decl_namespace ();
10217             else if (friendp && (TREE_COMPLEXITY (declarator) < 2))
10218               /* Don't fall out into global scope. Hides real bug? --eichin */ ;
10219             else if (! IS_AGGR_TYPE_CODE
10220                      (TREE_CODE (TREE_OPERAND (declarator, 0))))
10221               ;
10222             else if (TREE_COMPLEXITY (declarator) == current_class_depth)
10223               {
10224                 /* Resolve any TYPENAME_TYPEs from the decl-specifier-seq
10225                    that refer to ctype.  They couldn't be resolved earlier
10226                    because we hadn't pushed into the class yet.
10227                    Example: resolve 'B<T>::type' in
10228                    'B<typename B<T>::type> B<T>::f () { }'.  */
10229                 if (current_template_parms
10230                     && uses_template_parms (type)
10231                     && uses_template_parms (current_class_type))
10232                   {
10233                     tree args = current_template_args ();
10234                     type = tsubst (type, args, /*complain=*/1, NULL_TREE);
10235                   }
10236
10237                 /* This pop_nested_class corresponds to the
10238                    push_nested_class used to push into class scope for
10239                    parsing the argument list of a function decl, in
10240                    qualified_id.  */
10241                 pop_nested_class ();
10242                 TREE_COMPLEXITY (declarator) = current_class_depth;
10243               }
10244             else
10245               my_friendly_abort (16);
10246
10247             if (TREE_OPERAND (declarator, 0) == NULL_TREE)
10248               {
10249                 /* We had a reference to a global decl, or
10250                    perhaps we were given a non-aggregate typedef,
10251                    in which case we cleared this out, and should just
10252                    keep going as though it wasn't there.  */
10253                 declarator = sname;
10254                 continue;
10255               }
10256             ctype = TREE_OPERAND (declarator, 0);
10257
10258             t = ctype;
10259             while (t != NULL_TREE && CLASS_TYPE_P (t)) 
10260               {
10261                 if (CLASSTYPE_TEMPLATE_INFO (t) &&
10262                     !CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
10263                   template_count += 1;
10264                 t = TYPE_MAIN_DECL (t);
10265                 if (DECL_LANG_SPECIFIC (t))
10266                   t = DECL_CLASS_CONTEXT (t);
10267                 else
10268                   t = NULL_TREE;
10269               }
10270
10271             if (sname == NULL_TREE)
10272               goto done_scoping;
10273
10274             if (TREE_CODE (sname) == IDENTIFIER_NODE)
10275               {
10276                 /* This is the `standard' use of the scoping operator:
10277                    basetype :: member .  */
10278
10279                 if (ctype == current_class_type)
10280                   {
10281                     /* class A {
10282                          void A::f ();
10283                        };
10284
10285                        Is this ill-formed?  */
10286
10287                     if (pedantic)
10288                       cp_pedwarn ("extra qualification `%T::' on member `%s' ignored",
10289                                   ctype, name);
10290                   }
10291                 else if (TREE_CODE (type) == FUNCTION_TYPE)
10292                   {
10293                     if (current_class_type == NULL_TREE
10294                         || friendp)
10295                       type = build_cplus_method_type (ctype, TREE_TYPE (type),
10296                                                       TYPE_ARG_TYPES (type));
10297                     else
10298                       {
10299                         cp_error ("cannot declare member function `%T::%s' within `%T'",
10300                                   ctype, name, current_class_type);
10301                         return void_type_node;
10302                       }
10303                   }
10304                 else if (RIDBIT_SETP (RID_TYPEDEF, specbits)
10305                          || TYPE_SIZE (complete_type (ctype)) != NULL_TREE)
10306                   {
10307                     /* Have to move this code elsewhere in this function.
10308                        this code is used for i.e., typedef int A::M; M *pm;
10309
10310                        It is?  How? jason 10/2/94 */
10311
10312                     if (current_class_type)
10313                       {
10314                         cp_error ("cannot declare member `%T::%s' within `%T'",
10315                                   ctype, name, current_class_type);
10316                         return void_type_node;
10317                       }
10318                     type = build_offset_type (ctype, type);
10319                   }
10320                 else if (uses_template_parms (ctype))
10321                   {
10322                     if (TREE_CODE (type) == FUNCTION_TYPE)
10323                       type
10324                         = build_cplus_method_type (ctype, TREE_TYPE (type),
10325                                                    TYPE_ARG_TYPES (type));
10326                   }
10327                 else
10328                   {
10329                     cp_error ("structure `%T' not yet defined", ctype);
10330                     return error_mark_node;
10331                   }
10332
10333                 declarator = sname;
10334               }
10335             else if (TREE_CODE (sname) == SCOPE_REF)
10336               my_friendly_abort (17);
10337             else
10338               {
10339               done_scoping:
10340                 declarator = TREE_OPERAND (declarator, 1);
10341                 if (declarator && TREE_CODE (declarator) == CALL_EXPR)
10342                   /* In this case, we will deal with it later.  */
10343                   ;
10344                 else
10345                   {
10346                     if (TREE_CODE (type) == FUNCTION_TYPE)
10347                       type = build_cplus_method_type (ctype, TREE_TYPE (type),
10348                                                       TYPE_ARG_TYPES (type));
10349                     else
10350                       type = build_offset_type (ctype, type);
10351                   }
10352               }
10353           }
10354           break;
10355
10356         case BIT_NOT_EXPR:
10357           declarator = TREE_OPERAND (declarator, 0);
10358           break;
10359
10360         case RECORD_TYPE:
10361         case UNION_TYPE:
10362         case ENUMERAL_TYPE:
10363           declarator = NULL_TREE;
10364           break;
10365
10366         case ERROR_MARK:
10367           declarator = NULL_TREE;
10368           break;
10369
10370         default:
10371           my_friendly_abort (158);
10372         }
10373     }
10374
10375   /* See the comment for the TREE_LIST case, above.  */
10376   if (inner_attrs)
10377     {
10378       if (! ignore_attrs)
10379         decl_attributes (type, inner_attrs, NULL_TREE);
10380       else if (attrlist)
10381         TREE_VALUE (attrlist) = chainon (inner_attrs, TREE_VALUE (attrlist));
10382       else
10383         attrlist = build_decl_list (NULL_TREE, inner_attrs);
10384     }
10385
10386   /* Now TYPE has the actual type.  */
10387
10388   if (explicitp == 1 || (explicitp && friendp))
10389     {
10390       /* [dcl.fct.spec] The explicit specifier shall only be used in
10391          declarations of constructors within a class definition.  */
10392       error ("only declarations of constructors can be `explicit'");
10393       explicitp = 0;
10394     }
10395
10396   if (RIDBIT_SETP (RID_MUTABLE, specbits))
10397     {
10398       if (current_class_name == NULL_TREE || decl_context == PARM || friendp)
10399         {
10400           error ("non-member `%s' cannot be declared `mutable'", name);
10401           RIDBIT_RESET (RID_MUTABLE, specbits);
10402         }
10403       else if (decl_context == TYPENAME || RIDBIT_SETP (RID_TYPEDEF, specbits))
10404         {
10405           error ("non-object member `%s' cannot be declared `mutable'", name);
10406           RIDBIT_RESET (RID_MUTABLE, specbits);
10407         }
10408       else if (TREE_CODE (type) == FUNCTION_TYPE
10409                || TREE_CODE (type) == METHOD_TYPE)
10410         {
10411           error ("function `%s' cannot be declared `mutable'", name);
10412           RIDBIT_RESET (RID_MUTABLE, specbits);
10413         }
10414       else if (staticp)
10415         {
10416           error ("static `%s' cannot be declared `mutable'", name);
10417           RIDBIT_RESET (RID_MUTABLE, specbits);
10418         }
10419       else if (type_quals & TYPE_QUAL_CONST)
10420         {
10421           error ("const `%s' cannot be declared `mutable'", name);
10422           RIDBIT_RESET (RID_MUTABLE, specbits);
10423         }
10424     }
10425
10426   if (declarator == NULL_TREE
10427       || TREE_CODE (declarator) == IDENTIFIER_NODE
10428       || (TREE_CODE (declarator) == TEMPLATE_ID_EXPR
10429           && (TREE_CODE (type) == FUNCTION_TYPE
10430               || TREE_CODE (type) == METHOD_TYPE)))
10431     /* OK */;
10432   else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
10433     {
10434       cp_error ("template-id `%D' used as a declarator", declarator);
10435       declarator = dname;
10436     }
10437   else
10438     /* Unexpected declarator format.  */
10439     my_friendly_abort (990210);
10440
10441   /* If this is declaring a typedef name, return a TYPE_DECL.  */
10442
10443   if (RIDBIT_SETP (RID_TYPEDEF, specbits) && decl_context != TYPENAME)
10444     {
10445       tree decl;
10446
10447       /* Note that the grammar rejects storage classes
10448          in typenames, fields or parameters.  */
10449       if (current_lang_name == lang_name_java)
10450         TYPE_FOR_JAVA (type) = 1;
10451
10452       if (decl_context == FIELD)
10453         {
10454           if (declarator == constructor_name (current_class_type))
10455             cp_pedwarn ("ANSI C++ forbids nested type `%D' with same name as enclosing class",
10456                         declarator);
10457           decl = build_lang_decl (TYPE_DECL, declarator, type);
10458         }
10459       else
10460         {
10461           /* Make sure this typedef lives as long as its type,
10462              since it might be used as a template parameter. */
10463           if (processing_template_decl)
10464             decl = build_lang_decl (TYPE_DECL, declarator, type);
10465           else
10466             decl = build_decl (TYPE_DECL, declarator, type);
10467         }
10468
10469       /* If the user declares "typedef struct {...} foo" then the
10470          struct will have an anonymous name.  Fill that name in now.
10471          Nothing can refer to it, so nothing needs know about the name
10472          change.  */
10473       if (type != error_mark_node
10474           && TYPE_NAME (type)
10475           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10476           && ANON_AGGRNAME_P (TYPE_IDENTIFIER (type))
10477           && CP_TYPE_QUALS (type) == TYPE_UNQUALIFIED)
10478         {
10479           tree oldname = TYPE_NAME (type);
10480           tree t;
10481
10482           /* Replace the anonymous name with the real name everywhere.  */
10483           lookup_tag_reverse (type, declarator);
10484           for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
10485             if (TYPE_NAME (t) == oldname)
10486               TYPE_NAME (t) = decl;
10487
10488           if (TYPE_LANG_SPECIFIC (type))
10489             TYPE_WAS_ANONYMOUS (type) = 1;
10490
10491           /* If this is a typedef within a template class, the nested
10492              type is a (non-primary) template.  The name for the
10493              template needs updating as well.  */
10494           if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
10495             DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)) 
10496               = TYPE_IDENTIFIER (type);
10497
10498           /* XXX Temporarily set the scope. 
10499              When returning, start_decl expects it as NULL_TREE,
10500              and will then then set it using pushdecl. */
10501           my_friendly_assert (DECL_CONTEXT (decl) == NULL_TREE, 980404);
10502           if (current_class_type)
10503             DECL_CONTEXT (decl) = current_class_type;
10504           else
10505             DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
10506
10507           DECL_ASSEMBLER_NAME (decl) = DECL_NAME (decl);
10508           DECL_ASSEMBLER_NAME (decl)
10509             = get_identifier (build_overload_name (type, 1, 1));
10510           DECL_CONTEXT (decl) = NULL_TREE;
10511
10512           /* FIXME remangle member functions; member functions of a
10513              type with external linkage have external linkage.  */
10514         }
10515
10516       if (TREE_CODE (type) == OFFSET_TYPE || TREE_CODE (type) == METHOD_TYPE)
10517         {
10518           cp_error_at ("typedef name may not be class-qualified", decl);
10519           return NULL_TREE;
10520         }
10521       else if (quals)
10522         {
10523           if (ctype == NULL_TREE)
10524             {
10525               if (TREE_CODE (type) != METHOD_TYPE)
10526                 cp_error_at ("invalid type qualifier for non-method type", decl);
10527               else
10528                 ctype = TYPE_METHOD_BASETYPE (type);
10529             }
10530           if (ctype != NULL_TREE)
10531             grok_method_quals (ctype, decl, quals);
10532         }
10533
10534       if (RIDBIT_SETP (RID_SIGNED, specbits)
10535           || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
10536         C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
10537
10538       bad_specifiers (decl, "type", virtualp, quals != NULL_TREE,
10539                       inlinep, friendp, raises != NULL_TREE);
10540
10541       if (initialized)
10542         error ("typedef declaration includes an initializer");
10543
10544       return decl;
10545     }
10546
10547   /* Detect the case of an array type of unspecified size
10548      which came, as such, direct from a typedef name.
10549      We must copy the type, so that each identifier gets
10550      a distinct type, so that each identifier's size can be
10551      controlled separately by its own initializer.  */
10552
10553   if (type == typedef_type && TREE_CODE (type) == ARRAY_TYPE
10554       && TYPE_DOMAIN (type) == NULL_TREE)
10555     {
10556       type = build_cplus_array_type (TREE_TYPE (type), TYPE_DOMAIN (type));
10557     }
10558
10559   /* If this is a type name (such as, in a cast or sizeof),
10560      compute the type and return it now.  */
10561
10562   if (decl_context == TYPENAME)
10563     {
10564       /* Note that the grammar rejects storage classes
10565          in typenames, fields or parameters.  */
10566       if (type_quals != TYPE_UNQUALIFIED)
10567         type_quals = TYPE_UNQUALIFIED;
10568
10569       /* Special case: "friend class foo" looks like a TYPENAME context.  */
10570       if (friendp)
10571         {
10572           if (type_quals != TYPE_UNQUALIFIED)
10573             {
10574               cp_error ("type qualifiers specified for friend class declaration");
10575               type_quals = TYPE_UNQUALIFIED;
10576             }
10577           if (inlinep)
10578             {
10579               cp_error ("`inline' specified for friend class declaration");
10580               inlinep = 0;
10581             }
10582
10583           /* Only try to do this stuff if we didn't already give up.  */
10584           if (type != integer_type_node)
10585             {
10586               /* A friendly class?  */
10587               if (current_class_type)
10588                 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type));
10589               else
10590                 error ("trying to make class `%s' a friend of global scope",
10591                        TYPE_NAME_STRING (type));
10592               type = void_type_node;
10593             }
10594         }
10595       else if (quals)
10596         {
10597           tree dummy = build_decl (TYPE_DECL, declarator, type);
10598           if (ctype == NULL_TREE)
10599             {
10600               my_friendly_assert (TREE_CODE (type) == METHOD_TYPE, 159);
10601               ctype = TYPE_METHOD_BASETYPE (type);
10602             }
10603           grok_method_quals (ctype, dummy, quals);
10604           type = TREE_TYPE (dummy);
10605         }
10606
10607       return type;
10608     }
10609   else if (declarator == NULL_TREE && decl_context != PARM
10610            && decl_context != CATCHPARM
10611            && TREE_CODE (type) != UNION_TYPE
10612            && ! bitfield)
10613     {
10614       cp_error ("abstract declarator `%T' used as declaration", type);
10615       declarator = make_anon_name ();
10616     }
10617
10618   /* `void' at top level (not within pointer)
10619      is allowed only in typedefs or type names.
10620      We don't complain about parms either, but that is because
10621      a better error message can be made later.  */
10622
10623   if (TREE_CODE (type) == VOID_TYPE && decl_context != PARM)
10624     {
10625       if (! declarator)
10626         error ("unnamed variable or field declared void");
10627       else if (TREE_CODE (declarator) == IDENTIFIER_NODE)
10628         {
10629           if (IDENTIFIER_OPNAME_P (declarator))
10630             my_friendly_abort (356);
10631           else
10632             error ("variable or field `%s' declared void", name);
10633         }
10634       else
10635         error ("variable or field declared void");
10636       type = integer_type_node;
10637     }
10638
10639   /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
10640      or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE.  */
10641
10642   if (decl_context == PARM || decl_context == CATCHPARM)
10643     {
10644       if (ctype || in_namespace)
10645         error ("cannot use `::' in parameter declaration");
10646
10647       /* A parameter declared as an array of T is really a pointer to T.
10648          One declared as a function is really a pointer to a function.
10649          One declared as a member is really a pointer to member.  */
10650
10651       if (TREE_CODE (type) == ARRAY_TYPE)
10652         {
10653           /* Transfer const-ness of array into that of type pointed to.  */
10654           type = build_pointer_type (TREE_TYPE (type));
10655           type_quals = TYPE_UNQUALIFIED;
10656         }
10657       else if (TREE_CODE (type) == FUNCTION_TYPE)
10658         type = build_pointer_type (type);
10659       else if (TREE_CODE (type) == OFFSET_TYPE)
10660         type = build_pointer_type (type);
10661       else if (TREE_CODE (type) == VOID_TYPE && declarator)
10662         {
10663           error ("declaration of `%s' as void", name);
10664           return NULL_TREE;
10665         }
10666     }
10667   
10668   {
10669     register tree decl;
10670
10671     if (decl_context == PARM)
10672       {
10673         decl = build_decl (PARM_DECL, declarator, type);
10674
10675         bad_specifiers (decl, "parameter", virtualp, quals != NULL_TREE,
10676                         inlinep, friendp, raises != NULL_TREE);
10677
10678         /* Compute the type actually passed in the parmlist,
10679            for the case where there is no prototype.
10680            (For example, shorts and chars are passed as ints.)
10681            When there is a prototype, this is overridden later.  */
10682
10683         DECL_ARG_TYPE (decl) = type_promotes_to (type);
10684       }
10685     else if (decl_context == FIELD)
10686       {
10687         if (type == error_mark_node)
10688           {
10689             /* Happens when declaring arrays of sizes which
10690                are error_mark_node, for example.  */
10691             decl = NULL_TREE;
10692           }
10693         else if (in_namespace && !friendp)
10694           {
10695             /* Something like struct S { int N::j; };  */
10696             cp_error ("invalid use of `::'");
10697             decl = NULL_TREE;
10698           }
10699         else if (TREE_CODE (type) == FUNCTION_TYPE)
10700           {
10701             int publicp = 0;
10702             tree function_context;
10703
10704             /* We catch the others as conflicts with the builtin
10705                typedefs.  */
10706             if (friendp && declarator == ridpointers[(int) RID_SIGNED])
10707               {
10708                 cp_error ("function `%D' cannot be declared friend",
10709                           declarator);
10710                 friendp = 0;
10711               }
10712
10713             if (friendp == 0)
10714               {
10715                 if (ctype == NULL_TREE)
10716                   ctype = current_class_type;
10717
10718                 if (ctype == NULL_TREE)
10719                   {
10720                     cp_error ("can't make `%D' into a method -- not in a class",
10721                               declarator);
10722                     return void_type_node;
10723                   }
10724
10725                 /* ``A union may [ ... ] not [ have ] virtual functions.''
10726                    ARM 9.5 */
10727                 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
10728                   {
10729                     cp_error ("function `%D' declared virtual inside a union",
10730                               declarator);
10731                     return void_type_node;
10732                   }
10733
10734                 if (declarator == ansi_opname[(int) NEW_EXPR]
10735                     || declarator == ansi_opname[(int) VEC_NEW_EXPR]
10736                     || declarator == ansi_opname[(int) DELETE_EXPR]
10737                     || declarator == ansi_opname[(int) VEC_DELETE_EXPR])
10738                   {
10739                     if (virtualp)
10740                       {
10741                         cp_error ("`%D' cannot be declared virtual, since it is always static",
10742                                   declarator);
10743                         virtualp = 0;
10744                       }
10745                   }
10746                 else if (staticp < 2)
10747                   type = build_cplus_method_type (ctype, TREE_TYPE (type),
10748                                                   TYPE_ARG_TYPES (type));
10749               }
10750
10751             /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node.  */
10752             function_context = (ctype != NULL_TREE) ? 
10753               hack_decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE;
10754             publicp = (! friendp || ! staticp)
10755               && function_context == NULL_TREE;
10756             decl = grokfndecl (ctype, type, 
10757                                TREE_CODE (declarator) != TEMPLATE_ID_EXPR
10758                                ? declarator : dname,
10759                                declarator,
10760                                virtualp, flags, quals, raises,
10761                                friendp ? -1 : 0, friendp, publicp, inlinep,
10762                                funcdef_flag, template_count, in_namespace);
10763             if (decl == NULL_TREE)
10764               return decl;
10765 #if 0
10766             /* This clobbers the attrs stored in `decl' from `attrlist'.  */
10767             /* The decl and setting of decl_machine_attr is also turned off.  */
10768             decl = build_decl_attribute_variant (decl, decl_machine_attr);
10769 #endif
10770
10771             /* [class.conv.ctor]
10772
10773                A constructor declared without the function-specifier
10774                explicit that can be called with a single parameter
10775                specifies a conversion from the type of its first
10776                parameter to the type of its class.  Such a constructor
10777                is called a converting constructor.  */
10778             if (explicitp == 2)
10779               DECL_NONCONVERTING_P (decl) = 1;
10780             else if (DECL_CONSTRUCTOR_P (decl))
10781               {
10782                 /* The constructor can be called with exactly one
10783                    parameter if there is at least one parameter, and
10784                    any subsequent parameters have default arguments.
10785                    We don't look at the first parameter, which is
10786                    really just the `this' parameter for the new
10787                    object.  */
10788                 tree arg_types = 
10789                   TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)));
10790
10791                 /* Skip the `in_chrg' argument too, if present.  */
10792                 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (decl)))
10793                   arg_types = TREE_CHAIN (arg_types);
10794
10795                 if (arg_types == void_list_node
10796                     || (arg_types 
10797                         && TREE_CHAIN (arg_types) 
10798                         && TREE_CHAIN (arg_types) != void_list_node
10799                         && !TREE_PURPOSE (TREE_CHAIN (arg_types))))
10800                   DECL_NONCONVERTING_P (decl) = 1;
10801               }
10802           }
10803         else if (TREE_CODE (type) == METHOD_TYPE)
10804           {
10805             /* We only get here for friend declarations of
10806                members of other classes.  */
10807             /* All method decls are public, so tell grokfndecl to set
10808                TREE_PUBLIC, also.  */
10809             decl = grokfndecl (ctype, type, declarator, declarator,
10810                                virtualp, flags, quals, raises,
10811                                friendp ? -1 : 0, friendp, 1, 0, funcdef_flag,
10812                                template_count, in_namespace);
10813             if (decl == NULL_TREE)
10814               return NULL_TREE;
10815           }
10816         else if (!staticp && ! processing_template_decl
10817                  && TYPE_SIZE (complete_type (type)) == NULL_TREE
10818                  && (TREE_CODE (type) != ARRAY_TYPE || initialized == 0))
10819           {
10820             if (declarator)
10821               cp_error ("field `%D' has incomplete type", declarator);
10822             else
10823               cp_error ("name `%T' has incomplete type", type);
10824
10825             /* If we're instantiating a template, tell them which
10826                instantiation made the field's type be incomplete.  */
10827             if (current_class_type
10828                 && TYPE_NAME (current_class_type)
10829                 && IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (current_class_type))
10830                 && declspecs && TREE_VALUE (declspecs)
10831                 && TREE_TYPE (TREE_VALUE (declspecs)) == type)
10832               cp_error ("  in instantiation of template `%T'",
10833                         current_class_type);
10834
10835             type = error_mark_node;
10836             decl = NULL_TREE;
10837           }
10838         else
10839           {
10840             if (friendp)
10841               {
10842                 error ("`%s' is neither function nor method; cannot be declared friend",
10843                        IDENTIFIER_POINTER (declarator));
10844                 friendp = 0;
10845               }
10846             decl = NULL_TREE;
10847           }
10848
10849         if (friendp)
10850           {
10851             /* Friends are treated specially.  */
10852             if (ctype == current_class_type)
10853               warning ("member functions are implicitly friends of their class");
10854             else
10855               {
10856                 tree t = NULL_TREE;
10857                 if (decl && DECL_NAME (decl))
10858                   {
10859                     if (template_class_depth (current_class_type) == 0)
10860                       {
10861                         decl 
10862                           = check_explicit_specialization 
10863                           (declarator, decl,
10864                            template_count, 2 * (funcdef_flag != 0) + 4);
10865                         if (decl == error_mark_node)
10866                           return error_mark_node;
10867                       }
10868
10869                     t = do_friend (ctype, declarator, decl,
10870                                    last_function_parms, attrlist, flags, quals,
10871                                    funcdef_flag);
10872                   }
10873                 if (t && funcdef_flag)
10874                   return t;
10875                 
10876                 return void_type_node;
10877               }
10878           }
10879
10880         /* Structure field.  It may not be a function, except for C++ */
10881
10882         if (decl == NULL_TREE)
10883           {
10884             if (initialized)
10885               {
10886                 if (!staticp)
10887                   {
10888                     /* An attempt is being made to initialize a non-static
10889                        member.  But, from [class.mem]:
10890                        
10891                        4 A member-declarator can contain a
10892                        constant-initializer only if it declares a static
10893                        member (_class.static_) of integral or enumeration
10894                        type, see _class.static.data_.  
10895
10896                        This used to be relatively common practice, but
10897                        the rest of the compiler does not correctly
10898                        handle the initialization unless the member is
10899                        static so we make it static below.  */
10900                     cp_pedwarn ("ANSI C++ forbids initialization of member `%D'",
10901                                 declarator);
10902                     cp_pedwarn ("making `%D' static", declarator);
10903                     staticp = 1;
10904                   }
10905
10906                 if (uses_template_parms (type))
10907                   /* We'll check at instantiation time.  */
10908                   ;
10909                 else if (check_static_variable_definition (declarator,
10910                                                            type))
10911                   /* If we just return the declaration, crashes
10912                      will sometimes occur.  We therefore return
10913                      void_type_node, as if this was a friend
10914                      declaration, to cause callers to completely
10915                      ignore this declaration.  */
10916                   return void_type_node;
10917               }
10918
10919             /* 9.2p13 [class.mem] */
10920             if (declarator == constructor_name (current_class_type)
10921                 /* Divergence from the standard:  In extern "C", we
10922                    allow non-static data members here, because C does
10923                    and /usr/include/netinet/in.h uses that.  */
10924                 && (staticp || ! in_system_header))
10925               cp_pedwarn ("ANSI C++ forbids data member `%D' with same name as enclosing class",
10926                           declarator);
10927
10928             if (staticp)
10929               {
10930                 /* C++ allows static class members.  All other work
10931                    for this is done by grokfield.  */
10932                 decl = build_lang_decl (VAR_DECL, declarator, type);
10933                 TREE_STATIC (decl) = 1;
10934                 /* In class context, 'static' means public access.  */
10935                 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl) = 1;
10936               }
10937             else
10938               {
10939                 decl = build_lang_decl (FIELD_DECL, declarator, type);
10940                 if (RIDBIT_SETP (RID_MUTABLE, specbits))
10941                   {
10942                     DECL_MUTABLE_P (decl) = 1;
10943                     RIDBIT_RESET (RID_MUTABLE, specbits);
10944                   }
10945               }
10946
10947             bad_specifiers (decl, "field", virtualp, quals != NULL_TREE,
10948                             inlinep, friendp, raises != NULL_TREE);
10949           }
10950       }
10951     else if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
10952       {
10953         tree original_name;
10954         int publicp = 0;
10955
10956         if (! declarator)
10957           return NULL_TREE;
10958
10959         if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
10960           original_name = dname;
10961         else
10962           original_name = declarator;
10963
10964         if (RIDBIT_SETP (RID_AUTO, specbits))
10965           error ("storage class `auto' invalid for function `%s'", name);
10966         else if (RIDBIT_SETP (RID_REGISTER, specbits))
10967           error ("storage class `register' invalid for function `%s'", name);
10968
10969         /* Function declaration not at top level.
10970            Storage classes other than `extern' are not allowed
10971            and `extern' makes no difference.  */
10972         if (! toplevel_bindings_p ()
10973             && (RIDBIT_SETP (RID_STATIC, specbits)
10974                 || RIDBIT_SETP (RID_INLINE, specbits))
10975             && pedantic)
10976           {
10977             if (RIDBIT_SETP (RID_STATIC, specbits))
10978               pedwarn ("storage class `static' invalid for function `%s' declared out of global scope", name);
10979             else
10980               pedwarn ("storage class `inline' invalid for function `%s' declared out of global scope", name);
10981           }
10982         
10983         if (ctype == NULL_TREE)
10984           {
10985             if (virtualp)
10986               {
10987                 error ("virtual non-class function `%s'", name);
10988                 virtualp = 0;
10989               }
10990           }
10991         else if (TREE_CODE (type) == FUNCTION_TYPE && staticp < 2)
10992           type = build_cplus_method_type (ctype, TREE_TYPE (type),
10993                                           TYPE_ARG_TYPES (type));
10994
10995         /* Record presence of `static'.  */
10996         publicp = (ctype != NULL_TREE
10997                    || RIDBIT_SETP (RID_EXTERN, specbits)
10998                    || !RIDBIT_SETP (RID_STATIC, specbits));
10999
11000         decl = grokfndecl (ctype, type, original_name, declarator,
11001                            virtualp, flags, quals, raises,
11002                            1, friendp,
11003                            publicp, inlinep, funcdef_flag, 
11004                            template_count, in_namespace);
11005         if (decl == NULL_TREE)
11006           return NULL_TREE;
11007
11008         if (staticp == 1)
11009           {
11010             int illegal_static = 0;
11011
11012             /* Don't allow a static member function in a class, and forbid
11013                declaring main to be static.  */
11014             if (TREE_CODE (type) == METHOD_TYPE)
11015               {
11016                 cp_pedwarn ("cannot declare member function `%D' to have static linkage", decl);
11017                 illegal_static = 1;
11018               }
11019             else if (current_function_decl)
11020               {
11021                 /* FIXME need arm citation */
11022                 error ("cannot declare static function inside another function");
11023                 illegal_static = 1;
11024               }
11025
11026             if (illegal_static)
11027               {
11028                 staticp = 0;
11029                 RIDBIT_RESET (RID_STATIC, specbits);
11030               }
11031           }
11032       }
11033     else
11034       {
11035         /* It's a variable.  */
11036
11037         /* An uninitialized decl with `extern' is a reference.  */
11038         decl = grokvardecl (type, declarator, &specbits, 
11039                             initialized, 
11040                             (type_quals & TYPE_QUAL_CONST) != 0, 
11041                             in_namespace);
11042         bad_specifiers (decl, "variable", virtualp, quals != NULL_TREE,
11043                         inlinep, friendp, raises != NULL_TREE);
11044
11045         if (ctype)
11046           {
11047             DECL_CONTEXT (decl) = ctype;
11048             if (staticp == 1)
11049               {
11050                 cp_pedwarn ("static member `%D' re-declared as static", decl);
11051                 staticp = 0;
11052                 RIDBIT_RESET (RID_STATIC, specbits);
11053               }
11054             if (RIDBIT_SETP (RID_REGISTER, specbits) && TREE_STATIC (decl))
11055               {
11056                 cp_error ("static member `%D' declared `register'", decl);
11057                 RIDBIT_RESET (RID_REGISTER, specbits);
11058               }
11059             if (RIDBIT_SETP (RID_EXTERN, specbits) && pedantic)
11060               {
11061                 cp_pedwarn ("cannot explicitly declare member `%#D' to have extern linkage",
11062                             decl);
11063                 RIDBIT_RESET (RID_EXTERN, specbits);
11064               }
11065           }
11066       }
11067
11068     my_friendly_assert (!RIDBIT_SETP (RID_MUTABLE, specbits), 19990927);
11069
11070     /* Record `register' declaration for warnings on &
11071        and in case doing stupid register allocation.  */
11072
11073     if (RIDBIT_SETP (RID_REGISTER, specbits))
11074       DECL_REGISTER (decl) = 1;
11075
11076     if (RIDBIT_SETP (RID_EXTERN, specbits))
11077       DECL_THIS_EXTERN (decl) = 1;
11078
11079     if (RIDBIT_SETP (RID_STATIC, specbits))
11080       DECL_THIS_STATIC (decl) = 1;
11081
11082     /* Record constancy and volatility.  There's no need to do this
11083        when processing a template; we'll do this for the instantiated
11084        declaration based on the type of DECL.  */
11085     if (!processing_template_decl)
11086       c_apply_type_quals_to_decl (type_quals, decl);
11087
11088     return decl;
11089   }
11090 }
11091 \f
11092 /* Tell if a parmlist/exprlist looks like an exprlist or a parmlist.
11093    An empty exprlist is a parmlist.  An exprlist which
11094    contains only identifiers at the global level
11095    is a parmlist.  Otherwise, it is an exprlist.  */
11096
11097 int
11098 parmlist_is_exprlist (exprs)
11099      tree exprs;
11100 {
11101   if (exprs == NULL_TREE || TREE_PARMLIST (exprs))
11102     return 0;
11103
11104   if (toplevel_bindings_p ())
11105     {
11106       /* At the global level, if these are all identifiers,
11107          then it is a parmlist.  */
11108       while (exprs)
11109         {
11110           if (TREE_CODE (TREE_VALUE (exprs)) != IDENTIFIER_NODE)
11111             return 1;
11112           exprs = TREE_CHAIN (exprs);
11113         }
11114       return 0;
11115     }
11116   return 1;
11117 }
11118
11119 /* Subroutine of start_function.  Ensure that each of the parameter
11120    types (as listed in PARMS) is complete, as is required for a
11121    function definition.  */
11122
11123 static void
11124 require_complete_types_for_parms (parms)
11125      tree parms;
11126 {
11127   while (parms)
11128     {
11129       tree type = TREE_TYPE (parms);
11130       if (TYPE_SIZE (complete_type (type)) == NULL_TREE)
11131         {
11132           if (DECL_NAME (parms))
11133             error ("parameter `%s' has incomplete type",
11134                    IDENTIFIER_POINTER (DECL_NAME (parms)));
11135           else
11136             error ("parameter has incomplete type");
11137           TREE_TYPE (parms) = error_mark_node;
11138         }
11139       else
11140         layout_decl (parms, 0);
11141
11142       parms = TREE_CHAIN (parms);
11143     }
11144 }
11145
11146 /* Returns non-zero if T is a local variable.  */
11147
11148 int
11149 local_variable_p (t)
11150      tree t;
11151 {
11152   if ((TREE_CODE (t) == VAR_DECL 
11153        /* A VAR_DECL with a context that is a _TYPE is a static data
11154           member.  */
11155        && !TYPE_P (CP_DECL_CONTEXT (t))
11156        /* Any other non-local variable must be at namespace scope.  */
11157        && !DECL_NAMESPACE_SCOPE_P (t))
11158       || (TREE_CODE (t) == PARM_DECL))
11159     return 1;
11160
11161   return 0;
11162 }
11163
11164 /* Returns non-zero if T is an automatic local variable or a label.
11165    (These are the declarations that need to be remapped when the code
11166    containing them is duplicated.)  */
11167
11168 int
11169 nonstatic_local_decl_p (t)
11170      tree t;
11171 {
11172   return ((local_variable_p (t) && !TREE_STATIC (t))
11173           || TREE_CODE (t) == LABEL_DECL
11174           || TREE_CODE (t) == RESULT_DECL);
11175 }
11176
11177 /* Like local_variable_p, but suitable for use as a tree-walking
11178    function.  */
11179
11180 static tree
11181 local_variable_p_walkfn (tp, walk_subtrees, data)
11182      tree *tp;
11183      int *walk_subtrees ATTRIBUTE_UNUSED;
11184      void *data ATTRIBUTE_UNUSED;
11185 {
11186   return ((local_variable_p (*tp) && !DECL_ARTIFICIAL (*tp)) 
11187           ? *tp : NULL_TREE);
11188 }
11189
11190 /* Check that ARG, which is a default-argument expression for a
11191    parameter DECL, is legal.  Returns ARG, or ERROR_MARK_NODE, if
11192    something goes wrong.  DECL may also be a _TYPE node, rather than a
11193    DECL, if there is no DECL available.  */
11194
11195 tree
11196 check_default_argument (decl, arg)
11197      tree decl;
11198      tree arg;
11199 {
11200   tree var;
11201   tree decl_type;
11202
11203   if (TREE_CODE (arg) == DEFAULT_ARG)
11204     /* We get a DEFAULT_ARG when looking at an in-class declaration
11205        with a default argument.  Ignore the argument for now; we'll
11206        deal with it after the class is complete.  */
11207     return arg;
11208
11209   if (processing_template_decl || uses_template_parms (arg))
11210     /* We don't do anything checking until instantiation-time.  Note
11211        that there may be uninstantiated arguments even for an
11212        instantiated function, since default arguments are not
11213        instantiated until they are needed.  */
11214     return arg;
11215
11216   if (TYPE_P (decl))
11217     {
11218       decl_type = decl;
11219       decl = NULL_TREE;
11220     }
11221   else
11222     decl_type = TREE_TYPE (decl);
11223
11224   if (arg == error_mark_node 
11225       || decl == error_mark_node
11226       || TREE_TYPE (arg) == error_mark_node
11227       || decl_type == error_mark_node)
11228     /* Something already went wrong.  There's no need to check
11229        further.  */
11230     return error_mark_node;
11231
11232   /* [dcl.fct.default]
11233      
11234      A default argument expression is implicitly converted to the
11235      parameter type.  */
11236   if (!TREE_TYPE (arg)
11237       || !can_convert_arg (decl_type, TREE_TYPE (arg), arg))
11238     {
11239       if (decl)
11240         cp_error ("default argument for `%#D' has type `%T'", 
11241                   decl, TREE_TYPE (arg));
11242       else
11243         cp_error ("default argument for parameter of type `%T' has type `%T'",
11244                   decl_type, TREE_TYPE (arg));
11245
11246       return error_mark_node;
11247     }
11248
11249   /* [dcl.fct.default]
11250
11251      Local variables shall not be used in default argument
11252      expressions. 
11253
11254      The keyword `this' shall not be used in a default argument of a
11255      member function.  */
11256   var = walk_tree (&arg, local_variable_p_walkfn, NULL);
11257   if (var)
11258     {
11259       cp_error ("default argument `%E' uses local variable `%D'",
11260                 arg, var);
11261       return error_mark_node;
11262     }
11263
11264   /* All is well.  */
11265   return arg;
11266 }
11267
11268 /* Decode the list of parameter types for a function type.
11269    Given the list of things declared inside the parens,
11270    return a list of types.
11271
11272    The list we receive can have three kinds of elements:
11273    an IDENTIFIER_NODE for names given without types,
11274    a TREE_LIST node for arguments given as typespecs or names with typespecs,
11275    or void_type_node, to mark the end of an argument list
11276    when additional arguments are not permitted (... was not used).
11277
11278    FUNCDEF_FLAG is nonzero for a function definition, 0 for
11279    a mere declaration.  A nonempty identifier-list gets an error message
11280    when FUNCDEF_FLAG is zero.
11281    If FUNCDEF_FLAG is 1, then parameter types must be complete.
11282    If FUNCDEF_FLAG is -1, then parameter types may be incomplete.
11283
11284    If all elements of the input list contain types,
11285    we return a list of the types.
11286    If all elements contain no type (except perhaps a void_type_node
11287    at the end), we return a null list.
11288    If some have types and some do not, it is an error, and we
11289    return a null list.
11290
11291    Also set last_function_parms to either
11292    a list of names (IDENTIFIER_NODEs) or a chain of PARM_DECLs.
11293    A list of names is converted to a chain of PARM_DECLs
11294    by store_parm_decls so that ultimately it is always a chain of decls.
11295
11296    Note that in C++, parameters can take default values.  These default
11297    values are in the TREE_PURPOSE field of the TREE_LIST.  It is
11298    an error to specify default values which are followed by parameters
11299    that have no default values, or an ELLIPSES.  For simplicities sake,
11300    only parameters which are specified with their types can take on
11301    default values.  */
11302
11303 static tree
11304 grokparms (first_parm, funcdef_flag)
11305      tree first_parm;
11306      int funcdef_flag;
11307 {
11308   tree result = NULL_TREE;
11309   tree decls = NULL_TREE;
11310
11311   if (first_parm != NULL_TREE
11312       && TREE_CODE (TREE_VALUE (first_parm)) == IDENTIFIER_NODE)
11313     {
11314       if (! funcdef_flag)
11315         pedwarn ("parameter names (without types) in function declaration");
11316       last_function_parms = first_parm;
11317       return NULL_TREE;
11318     }
11319   else if (first_parm != NULL_TREE
11320            && TREE_CODE (TREE_VALUE (first_parm)) != TREE_LIST
11321            && TREE_CODE (TREE_VALUE (first_parm)) != VOID_TYPE)
11322     my_friendly_abort (145);
11323   else
11324     {
11325       /* Types were specified.  This is a list of declarators
11326          each represented as a TREE_LIST node.  */
11327       register tree parm, chain;
11328       int any_init = 0, any_error = 0;
11329
11330       if (first_parm != NULL_TREE)
11331         {
11332           tree last_result = NULL_TREE;
11333           tree last_decl = NULL_TREE;
11334
11335           for (parm = first_parm; parm != NULL_TREE; parm = chain)
11336             {
11337               tree type = NULL_TREE, list_node = parm;
11338               register tree decl = TREE_VALUE (parm);
11339               tree init = TREE_PURPOSE (parm);
11340
11341               chain = TREE_CHAIN (parm);
11342               /* @@ weak defense against parse errors.  */
11343               if (TREE_CODE (decl) != VOID_TYPE 
11344                   && TREE_CODE (decl) != TREE_LIST)
11345                 {
11346                   /* Give various messages as the need arises.  */
11347                   if (TREE_CODE (decl) == STRING_CST)
11348                     cp_error ("invalid string constant `%E'", decl);
11349                   else if (TREE_CODE (decl) == INTEGER_CST)
11350                     error ("invalid integer constant in parameter list, did you forget to give parameter name?");
11351                   continue;
11352                 }
11353
11354               if (TREE_CODE (decl) != VOID_TYPE)
11355                 {
11356                   decl = grokdeclarator (TREE_VALUE (decl),
11357                                          TREE_PURPOSE (decl),
11358                                          PARM, init != NULL_TREE,
11359                                          NULL_TREE);
11360                   if (! decl || TREE_TYPE (decl) == error_mark_node)
11361                     continue;
11362
11363                   /* Top-level qualifiers on the parameters are
11364                      ignored for function types.  */
11365                   type = TYPE_MAIN_VARIANT (TREE_TYPE (decl));
11366
11367                   if (TREE_CODE (type) == VOID_TYPE)
11368                     decl = void_type_node;
11369                   else if (TREE_CODE (type) == METHOD_TYPE)
11370                     {
11371                       if (DECL_NAME (decl))
11372                         /* Cannot use the decl here because
11373                            we don't have DECL_CONTEXT set up yet.  */
11374                         cp_error ("parameter `%D' invalidly declared method type",
11375                                   DECL_NAME (decl));
11376                       else
11377                         error ("parameter invalidly declared method type");
11378                       type = build_pointer_type (type);
11379                       TREE_TYPE (decl) = type;
11380                     }
11381                   else if (TREE_CODE (type) == OFFSET_TYPE)
11382                     {
11383                       if (DECL_NAME (decl))
11384                         cp_error ("parameter `%D' invalidly declared offset type",
11385                                   DECL_NAME (decl));
11386                       else
11387                         error ("parameter invalidly declared offset type");
11388                       type = build_pointer_type (type);
11389                       TREE_TYPE (decl) = type;
11390                     }
11391                   else if (abstract_virtuals_error (decl, type))
11392                     any_error = 1;  /* Seems like a good idea. */
11393                   else if (POINTER_TYPE_P (type))
11394                     {
11395                       tree t = type;
11396                       while (POINTER_TYPE_P (t)
11397                              || (TREE_CODE (t) == ARRAY_TYPE
11398                                  && TYPE_DOMAIN (t) != NULL_TREE))
11399                         t = TREE_TYPE (t);
11400                       if (TREE_CODE (t) == ARRAY_TYPE)
11401                         cp_error ("parameter type `%T' includes %s to array of unknown bound",
11402                                   type,
11403                                   TYPE_PTR_P (type) ? "pointer" : "reference");
11404                     }
11405                 }
11406
11407               if (TREE_CODE (decl) == VOID_TYPE)
11408                 {
11409                   if (result == NULL_TREE)
11410                     {
11411                       result = void_list_node;
11412                       last_result = result;
11413                     }
11414                   else
11415                     {
11416                       TREE_CHAIN (last_result) = void_list_node;
11417                       last_result = void_list_node;
11418                     }
11419                   if (chain
11420                       && (chain != void_list_node || TREE_CHAIN (chain)))
11421                     error ("`void' in parameter list must be entire list");
11422                   break;
11423                 }
11424
11425               /* Since there is a prototype, args are passed in their own types.  */
11426               DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
11427               if (PROMOTE_PROTOTYPES
11428                   && (TREE_CODE (type) == INTEGER_TYPE
11429                       || TREE_CODE (type) == ENUMERAL_TYPE)
11430                   && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
11431                 DECL_ARG_TYPE (decl) = integer_type_node;
11432               if (!any_error && init)
11433                 {
11434                   any_init++;
11435                   init = check_default_argument (decl, init);
11436                 }
11437               else
11438                 init = NULL_TREE;
11439
11440               if (decls == NULL_TREE)
11441                 {
11442                   decls = decl;
11443                   last_decl = decls;
11444                 }
11445               else
11446                 {
11447                   TREE_CHAIN (last_decl) = decl;
11448                   last_decl = decl;
11449                 }
11450               list_node = tree_cons (init, type, NULL_TREE);
11451               if (result == NULL_TREE)
11452                 {
11453                   result = list_node;
11454                   last_result = result;
11455                 }
11456               else
11457                 {
11458                   TREE_CHAIN (last_result) = list_node;
11459                   last_result = list_node;
11460                 }
11461             }
11462           if (last_result)
11463             TREE_CHAIN (last_result) = NULL_TREE;
11464           /* If there are no parameters, and the function does not end
11465              with `...', then last_decl will be NULL_TREE.  */
11466           if (last_decl != NULL_TREE)
11467             TREE_CHAIN (last_decl) = NULL_TREE;
11468         }
11469     }
11470
11471   last_function_parms = decls;
11472
11473   return result;
11474 }
11475
11476 /* Called from the parser to update an element of TYPE_ARG_TYPES for some
11477    FUNCTION_TYPE with the newly parsed version of its default argument, which
11478    was previously digested as text.  See snarf_defarg et al in lex.c.  */
11479
11480 void
11481 replace_defarg (arg, init)
11482      tree arg, init;
11483 {
11484   if (! processing_template_decl
11485       && ! can_convert_arg (TREE_VALUE (arg), TREE_TYPE (init), init))
11486     cp_pedwarn ("invalid type `%T' for default argument to `%T'",
11487                 TREE_TYPE (init), TREE_VALUE (arg));
11488   TREE_PURPOSE (arg) = init;
11489 }
11490 \f
11491 int
11492 copy_args_p (d)
11493      tree d;
11494 {
11495   tree t = FUNCTION_ARG_CHAIN (d);
11496   if (DECL_CONSTRUCTOR_P (d)
11497       && TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (d)))
11498     t = TREE_CHAIN (t);
11499   if (t && TREE_CODE (TREE_VALUE (t)) == REFERENCE_TYPE
11500       && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (t)))
11501           == DECL_CLASS_CONTEXT (d))
11502       && (TREE_CHAIN (t) == NULL_TREE
11503           || TREE_CHAIN (t) == void_list_node
11504           || TREE_PURPOSE (TREE_CHAIN (t))))
11505     return 1;
11506   return 0;
11507 }
11508
11509 /* These memoizing functions keep track of special properties which
11510    a class may have.  `grok_ctor_properties' notices whether a class
11511    has a constructor of the form X(X&), and also complains
11512    if the class has a constructor of the form X(X).
11513    `grok_op_properties' takes notice of the various forms of
11514    operator= which are defined, as well as what sorts of type conversion
11515    may apply.  Both functions take a FUNCTION_DECL as an argument.  */
11516
11517 int
11518 grok_ctor_properties (ctype, decl)
11519      tree ctype, decl;
11520 {
11521   tree parmtypes = FUNCTION_ARG_CHAIN (decl);
11522   tree parmtype = parmtypes ? TREE_VALUE (parmtypes) : void_type_node;
11523
11524   /* When a type has virtual baseclasses, a magical first int argument is
11525      added to any ctor so we can tell if the class has been initialized
11526      yet.  This could screw things up in this function, so we deliberately
11527      ignore the leading int if we're in that situation.  */
11528   if (TYPE_USES_VIRTUAL_BASECLASSES (ctype))
11529     {
11530       my_friendly_assert (parmtypes
11531                           && TREE_VALUE (parmtypes) == integer_type_node,
11532                           980529);
11533       parmtypes = TREE_CHAIN (parmtypes);
11534       parmtype = TREE_VALUE (parmtypes);
11535     }
11536
11537   /* [class.copy]
11538
11539      A non-template constructor for class X is a copy constructor if
11540      its first parameter is of type X&, const X&, volatile X& or const
11541      volatile X&, and either there are no other parameters or else all
11542      other parameters have default arguments.  */
11543   if (TREE_CODE (parmtype) == REFERENCE_TYPE
11544       && TYPE_MAIN_VARIANT (TREE_TYPE (parmtype)) == ctype
11545       && (TREE_CHAIN (parmtypes) == NULL_TREE
11546           || TREE_CHAIN (parmtypes) == void_list_node
11547           || TREE_PURPOSE (TREE_CHAIN (parmtypes)))
11548       && !(DECL_TEMPLATE_INSTANTIATION (decl)
11549            && is_member_template (DECL_TI_TEMPLATE (decl))))
11550     {
11551       TYPE_HAS_INIT_REF (ctype) = 1;
11552       if (CP_TYPE_CONST_P (TREE_TYPE (parmtype)))
11553         TYPE_HAS_CONST_INIT_REF (ctype) = 1;
11554     }
11555   /* [class.copy]
11556
11557      A declaration of a constructor for a class X is ill-formed if its
11558      first parameter is of type (optionally cv-qualified) X and either
11559      there are no other parameters or else all other parameters have
11560      default arguments.  
11561
11562      We *don't* complain about member template instantiations that
11563      have this form, though; they can occur as we try to decide what
11564      constructor to use during overload resolution.  Since overload
11565      resolution will never prefer such a constructor to the
11566      non-template copy constructor (which is either explicitly or
11567      implicitly defined), there's no need to worry about their
11568      existence.  Theoretically, they should never even be
11569      instantiated, but that's hard to forestall.  */
11570   else if (TYPE_MAIN_VARIANT (parmtype) == ctype
11571            && (TREE_CHAIN (parmtypes) == NULL_TREE
11572                || TREE_CHAIN (parmtypes) == void_list_node
11573                || TREE_PURPOSE (TREE_CHAIN (parmtypes)))
11574            && !(DECL_TEMPLATE_INSTANTIATION (decl)
11575                 && is_member_template (DECL_TI_TEMPLATE (decl))))
11576     {
11577       cp_error ("invalid constructor; you probably meant `%T (const %T&)'",
11578                 ctype, ctype);
11579       SET_IDENTIFIER_ERROR_LOCUS (DECL_NAME (decl), ctype);
11580       return 0;
11581     }
11582   else if (TREE_CODE (parmtype) == VOID_TYPE
11583            || TREE_PURPOSE (parmtypes) != NULL_TREE)
11584     TYPE_HAS_DEFAULT_CONSTRUCTOR (ctype) = 1;
11585
11586   return 1;
11587 }
11588
11589 /* An operator with this name can be either unary or binary.  */
11590
11591 static int
11592 ambi_op_p (name)
11593      tree name;
11594 {
11595   return (name == ansi_opname [(int) INDIRECT_REF]
11596           || name == ansi_opname [(int) ADDR_EXPR]
11597           || name == ansi_opname [(int) NEGATE_EXPR]
11598           || name == ansi_opname[(int) POSTINCREMENT_EXPR]
11599           || name == ansi_opname[(int) POSTDECREMENT_EXPR]
11600           || name == ansi_opname [(int) CONVERT_EXPR]);
11601 }
11602
11603 /* An operator with this name can only be unary.  */
11604
11605 static int
11606 unary_op_p (name)
11607      tree name;
11608 {
11609   return (name == ansi_opname [(int) TRUTH_NOT_EXPR]
11610           || name == ansi_opname [(int) BIT_NOT_EXPR]
11611           || name == ansi_opname [(int) COMPONENT_REF]
11612           || IDENTIFIER_TYPENAME_P (name));
11613 }
11614
11615 /* Do a little sanity-checking on how they declared their operator.  */
11616
11617 void
11618 grok_op_properties (decl, virtualp, friendp)
11619      tree decl;
11620      int virtualp, friendp;
11621 {
11622   tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
11623   int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
11624   tree name = DECL_NAME (decl);
11625
11626   if (current_class_type == NULL_TREE)
11627     friendp = 1;
11628
11629   if (! friendp)
11630     {
11631       /* [class.copy]
11632
11633          A user-declared copy assignment operator X::operator= is a
11634          non-static non-template member function of class X with
11635          exactly one parameter of type X, X&, const X&, volatile X& or
11636          const volatile X&.  */
11637       if (name == ansi_opname[(int) MODIFY_EXPR]
11638           && !(DECL_TEMPLATE_INSTANTIATION (decl)
11639                && is_member_template (DECL_TI_TEMPLATE (decl))))
11640         ;
11641       else if (name == ansi_opname[(int) CALL_EXPR])
11642         TYPE_OVERLOADS_CALL_EXPR (current_class_type) = 1;
11643       else if (name == ansi_opname[(int) ARRAY_REF])
11644         TYPE_OVERLOADS_ARRAY_REF (current_class_type) = 1;
11645       else if (name == ansi_opname[(int) COMPONENT_REF]
11646                || name == ansi_opname[(int) MEMBER_REF])
11647         TYPE_OVERLOADS_ARROW (current_class_type) = 1;
11648       else if (name == ansi_opname[(int) NEW_EXPR])
11649         TYPE_GETS_NEW (current_class_type) |= 1;
11650       else if (name == ansi_opname[(int) DELETE_EXPR])
11651         TYPE_GETS_DELETE (current_class_type) |= 1;
11652       else if (name == ansi_opname[(int) VEC_NEW_EXPR])
11653         TYPE_GETS_NEW (current_class_type) |= 2;
11654       else if (name == ansi_opname[(int) VEC_DELETE_EXPR])
11655         TYPE_GETS_DELETE (current_class_type) |= 2;
11656     }
11657
11658   if (name == ansi_opname[(int) NEW_EXPR]
11659       || name == ansi_opname[(int) VEC_NEW_EXPR])
11660     {
11661       /* When the compiler encounters the definition of A::operator new, it
11662          doesn't look at the class declaration to find out if it's static.  */
11663       if (methodp)
11664         revert_static_member_fn (&decl, NULL, NULL);
11665      
11666       /* Take care of function decl if we had syntax errors.  */
11667       if (argtypes == NULL_TREE)
11668         TREE_TYPE (decl)
11669           = build_function_type (ptr_type_node,
11670                                  hash_tree_chain (integer_type_node,
11671                                                   void_list_node));
11672       else
11673         TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl));
11674     }
11675   else if (name == ansi_opname[(int) DELETE_EXPR]
11676            || name == ansi_opname[(int) VEC_DELETE_EXPR])
11677     {
11678       if (methodp)
11679         revert_static_member_fn (&decl, NULL, NULL);
11680      
11681       if (argtypes == NULL_TREE)
11682         TREE_TYPE (decl)
11683           = build_function_type (void_type_node,
11684                                  hash_tree_chain (ptr_type_node,
11685                                                   void_list_node));
11686       else
11687         {
11688           TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl));
11689
11690           if (! friendp && name == ansi_opname[(int) VEC_DELETE_EXPR]
11691               && (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
11692                   != void_list_node))
11693             TYPE_VEC_DELETE_TAKES_SIZE (current_class_type) = 1;
11694         }
11695     }
11696   else
11697     {
11698       /* An operator function must either be a non-static member function
11699          or have at least one parameter of a class, a reference to a class,
11700          an enumeration, or a reference to an enumeration.  13.4.0.6 */
11701       if (! methodp || DECL_STATIC_FUNCTION_P (decl))
11702         {
11703           if (IDENTIFIER_TYPENAME_P (name)
11704               || name == ansi_opname[(int) CALL_EXPR]
11705               || name == ansi_opname[(int) MODIFY_EXPR]
11706               || name == ansi_opname[(int) COMPONENT_REF]
11707               || name == ansi_opname[(int) ARRAY_REF])
11708             cp_error ("`%D' must be a nonstatic member function", decl);
11709           else
11710             {
11711               tree p = argtypes;
11712
11713               if (DECL_STATIC_FUNCTION_P (decl))
11714                 cp_error ("`%D' must be either a non-static member function or a non-member function", decl);
11715
11716               if (p)
11717                 for (; TREE_CODE (TREE_VALUE (p)) != VOID_TYPE ; p = TREE_CHAIN (p))
11718                   {
11719                     tree arg = TREE_VALUE (p);
11720                     if (TREE_CODE (arg) == REFERENCE_TYPE)
11721                       arg = TREE_TYPE (arg);
11722
11723                     /* This lets bad template code slip through.  */
11724                     if (IS_AGGR_TYPE (arg)
11725                         || TREE_CODE (arg) == ENUMERAL_TYPE
11726                         || TREE_CODE (arg) == TEMPLATE_TYPE_PARM
11727                         || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
11728                       goto foundaggr;
11729                   }
11730               cp_error
11731                 ("`%D' must have an argument of class or enumerated type",
11732                  decl);
11733             foundaggr:
11734               ;
11735             }
11736         }
11737       
11738       if (name == ansi_opname[(int) CALL_EXPR])
11739         return;                 /* No restrictions on args. */
11740
11741       if (IDENTIFIER_TYPENAME_P (name) && ! DECL_TEMPLATE_INFO (decl))
11742         {
11743           tree t = TREE_TYPE (name);
11744           if (TREE_CODE (t) == VOID_TYPE)
11745             pedwarn ("void is not a valid type conversion operator");
11746           else if (! friendp)
11747             {
11748               int ref = (TREE_CODE (t) == REFERENCE_TYPE);
11749               const char *what = 0;
11750               if (ref)
11751                 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
11752
11753               if (t == current_class_type)
11754                 what = "the same type";
11755               /* Don't force t to be complete here.  */
11756               else if (IS_AGGR_TYPE (t)
11757                        && TYPE_SIZE (t)
11758                        && DERIVED_FROM_P (t, current_class_type))
11759                 what = "a base class";
11760
11761               if (what)
11762                 warning ("conversion to %s%s will never use a type conversion operator",
11763                          ref ? "a reference to " : "", what);
11764             }
11765         }
11766
11767       if (name == ansi_opname[(int) MODIFY_EXPR])
11768         {
11769           tree parmtype;
11770
11771           if (list_length (argtypes) != 3 && methodp)
11772             {
11773               cp_error ("`%D' must take exactly one argument", decl);
11774               return;
11775             }
11776           parmtype = TREE_VALUE (TREE_CHAIN (argtypes));
11777
11778           if (copy_assignment_arg_p (parmtype, virtualp)
11779               && ! friendp)
11780             {
11781               TYPE_HAS_ASSIGN_REF (current_class_type) = 1;
11782               if (TREE_CODE (parmtype) != REFERENCE_TYPE
11783                   || CP_TYPE_CONST_P (TREE_TYPE (parmtype)))
11784                 TYPE_HAS_CONST_ASSIGN_REF (current_class_type) = 1;
11785             }
11786         }
11787       else if (name == ansi_opname[(int) COND_EXPR])
11788         {
11789           /* 13.4.0.3 */
11790           cp_error ("ANSI C++ prohibits overloading operator ?:");
11791         }         
11792       else if (ambi_op_p (name))
11793         {
11794           if (list_length (argtypes) == 2)
11795             /* prefix */;
11796           else if (list_length (argtypes) == 3)
11797             {
11798               if ((name == ansi_opname[(int) POSTINCREMENT_EXPR]
11799                    || name == ansi_opname[(int) POSTDECREMENT_EXPR])
11800                   && ! processing_template_decl
11801                   && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node))
11802                 {
11803                   if (methodp)
11804                     cp_error ("postfix `%D' must take `int' as its argument",
11805                               decl);
11806                   else
11807                     cp_error
11808                       ("postfix `%D' must take `int' as its second argument",
11809                        decl);
11810                 }
11811             }
11812           else
11813             {
11814               if (methodp)
11815                 cp_error ("`%D' must take either zero or one argument", decl);
11816               else
11817                 cp_error ("`%D' must take either one or two arguments", decl);
11818             }
11819
11820           /* More Effective C++ rule 6.  */
11821           if (warn_ecpp
11822               && (name == ansi_opname[(int) POSTINCREMENT_EXPR]
11823                   || name == ansi_opname[(int) POSTDECREMENT_EXPR]))
11824             {
11825               tree arg = TREE_VALUE (argtypes);
11826               tree ret = TREE_TYPE (TREE_TYPE (decl));
11827               if (methodp || TREE_CODE (arg) == REFERENCE_TYPE)
11828                 arg = TREE_TYPE (arg);
11829               arg = TYPE_MAIN_VARIANT (arg);
11830               if (list_length (argtypes) == 2)
11831                 {
11832                   if (TREE_CODE (ret) != REFERENCE_TYPE
11833                       || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)),
11834                                        arg))
11835                     cp_warning ("prefix `%D' should return `%T'", decl,
11836                                 build_reference_type (arg));
11837                 }
11838               else
11839                 {
11840                   if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
11841                     cp_warning ("postfix `%D' should return `%T'", decl, arg);
11842                 }
11843             }
11844         }
11845       else if (unary_op_p (name))
11846         {
11847           if (list_length (argtypes) != 2)
11848             {
11849               if (methodp)
11850                 cp_error ("`%D' must take `void'", decl);
11851               else
11852                 cp_error ("`%D' must take exactly one argument", decl);
11853             }
11854         }
11855       else /* if (binary_op_p (name)) */
11856         {
11857           if (list_length (argtypes) != 3)
11858             {
11859               if (methodp)
11860                 cp_error ("`%D' must take exactly one argument", decl);
11861               else
11862                 cp_error ("`%D' must take exactly two arguments", decl);
11863             }
11864
11865           /* More Effective C++ rule 7.  */
11866           if (warn_ecpp
11867               && (name == ansi_opname [TRUTH_ANDIF_EXPR]
11868                   || name == ansi_opname [TRUTH_ORIF_EXPR]
11869                   || name == ansi_opname [COMPOUND_EXPR]))
11870             cp_warning ("user-defined `%D' always evaluates both arguments",
11871                         decl);
11872         }
11873
11874       /* Effective C++ rule 23.  */
11875       if (warn_ecpp
11876           && list_length (argtypes) == 3
11877           && (name == ansi_opname [PLUS_EXPR]
11878               || name == ansi_opname [MINUS_EXPR]
11879               || name == ansi_opname [TRUNC_DIV_EXPR]
11880               || name == ansi_opname [MULT_EXPR])
11881           && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE)
11882         cp_warning ("`%D' should return by value", decl);
11883
11884       /* 13.4.0.8 */
11885       if (argtypes)
11886         for (; argtypes != void_list_node ; argtypes = TREE_CHAIN (argtypes))
11887           if (TREE_PURPOSE (argtypes))
11888             {
11889               TREE_PURPOSE (argtypes) = NULL_TREE;
11890               if (name == ansi_opname[(int) POSTINCREMENT_EXPR]
11891                   || name == ansi_opname[(int) POSTDECREMENT_EXPR])
11892                 {
11893                   if (pedantic)
11894                     cp_pedwarn ("`%D' cannot have default arguments", decl);
11895                 }
11896               else
11897                 cp_error ("`%D' cannot have default arguments", decl);
11898             }
11899     }
11900 }
11901 \f
11902 static const char *
11903 tag_name (code)
11904      enum tag_types code;
11905 {
11906   switch (code)
11907     {
11908     case record_type:
11909       return "struct";
11910     case class_type:
11911       return "class";
11912     case union_type:
11913       return "union ";
11914     case enum_type:
11915       return "enum";
11916     default:
11917       my_friendly_abort (981122);
11918     }
11919 }
11920
11921 /* Get the struct, enum or union (CODE says which) with tag NAME.
11922    Define the tag as a forward-reference if it is not defined.
11923
11924    C++: If a class derivation is given, process it here, and report
11925    an error if multiple derivation declarations are not identical.
11926
11927    If this is a definition, come in through xref_tag and only look in
11928    the current frame for the name (since C++ allows new names in any
11929    scope.)  */
11930
11931 tree
11932 xref_tag (code_type_node, name, globalize)
11933      tree code_type_node;
11934      tree name;
11935      int globalize;
11936 {
11937   enum tag_types tag_code;
11938   enum tree_code code;
11939   register tree ref, t;
11940   struct binding_level *b = current_binding_level;
11941   int got_type = 0;
11942   tree attributes = NULL_TREE;
11943   tree context = NULL_TREE;
11944
11945   /* If we are called from the parser, code_type_node will sometimes be a
11946      TREE_LIST.  This indicates that the user wrote
11947      "class __attribute__ ((foo)) bar".  Extract the attributes so we can
11948      use them later.  */
11949   if (TREE_CODE (code_type_node) == TREE_LIST)
11950     {
11951       attributes = TREE_PURPOSE (code_type_node);
11952       code_type_node = TREE_VALUE (code_type_node);
11953     }
11954
11955   tag_code = (enum tag_types) TREE_INT_CST_LOW (code_type_node);
11956   switch (tag_code)
11957     {
11958     case record_type:
11959     case class_type:
11960       code = RECORD_TYPE;
11961       break;
11962     case union_type:
11963       code = UNION_TYPE;
11964       break;
11965     case enum_type:
11966       code = ENUMERAL_TYPE;
11967       break;
11968     default:
11969       my_friendly_abort (18);
11970     }
11971
11972   /* If a cross reference is requested, look up the type
11973      already defined for this tag and return it.  */
11974   if (TREE_CODE_CLASS (TREE_CODE (name)) == 't')
11975     {
11976       t = name;
11977       name = TYPE_IDENTIFIER (t);
11978       got_type = 1;
11979     }
11980   else
11981     t = IDENTIFIER_TYPE_VALUE (name);
11982
11983   if (t && TREE_CODE (t) != code && TREE_CODE (t) != TEMPLATE_TYPE_PARM
11984       && TREE_CODE (t) != TEMPLATE_TEMPLATE_PARM)
11985     t = NULL_TREE;
11986
11987   if (! globalize)
11988     {
11989       /* If we know we are defining this tag, only look it up in
11990          this scope and don't try to find it as a type.  */
11991       ref = lookup_tag (code, name, b, 1);
11992     }
11993   else
11994     {
11995       if (t)
11996         {
11997           /* [dcl.type.elab] If the identifier resolves to a
11998              typedef-name or a template type-parameter, the
11999              elaborated-type-specifier is ill-formed.  */
12000           if (t != TYPE_MAIN_VARIANT (t)
12001               || (CLASS_TYPE_P (t) && TYPE_WAS_ANONYMOUS (t)))
12002             cp_pedwarn ("using typedef-name `%D' after `%s'",
12003                         TYPE_NAME (t), tag_name (tag_code));
12004           else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
12005             cp_error ("using template type parameter `%T' after `%s'",
12006                       t, tag_name (tag_code));
12007
12008           ref = t;
12009         }
12010       else
12011         ref = lookup_tag (code, name, b, 0);
12012           
12013       if (! ref)
12014         {
12015           /* Try finding it as a type declaration.  If that wins,
12016              use it.  */ 
12017           ref = lookup_name (name, 1);
12018
12019           if (ref != NULL_TREE
12020               && processing_template_decl
12021               && DECL_CLASS_TEMPLATE_P (ref)
12022               && template_class_depth (current_class_type) == 0)
12023             /* Since GLOBALIZE is true, we're declaring a global
12024                template, so we want this type.  */
12025             ref = DECL_RESULT (ref);
12026
12027           if (ref && TREE_CODE (ref) == TYPE_DECL
12028               && TREE_CODE (TREE_TYPE (ref)) == code)
12029             ref = TREE_TYPE (ref);
12030           else
12031             ref = NULL_TREE;
12032         }
12033
12034       if (ref && current_class_type 
12035           && template_class_depth (current_class_type) 
12036           && PROCESSING_REAL_TEMPLATE_DECL_P ()) 
12037         {
12038           /* Since GLOBALIZE is non-zero, we are not looking at a
12039              definition of this tag.  Since, in addition, we are currently
12040              processing a (member) template declaration of a template
12041              class, we must be very careful; consider:
12042
12043                template <class X>
12044                struct S1
12045
12046                template <class U>
12047                struct S2
12048                { template <class V>
12049                friend struct S1; };
12050
12051              Here, the S2::S1 declaration should not be confused with the
12052              outer declaration.  In particular, the inner version should
12053              have a template parameter of level 2, not level 1.  This
12054              would be particularly important if the member declaration
12055              were instead:
12056
12057                template <class V = U> friend struct S1;
12058
12059              say, when we should tsubst into `U' when instantiating
12060              S2.  On the other hand, when presented with:
12061
12062                  template <class T>
12063                  struct S1 {
12064                    template <class U>
12065                    struct S2 {};
12066                    template <class U>
12067                    friend struct S2;
12068                  };
12069
12070               we must find the inner binding eventually.  We
12071               accomplish this by making sure that the new type we
12072               create to represent this declaration has the right
12073               TYPE_CONTEXT.  */
12074           context = TYPE_CONTEXT (ref);
12075           ref = NULL_TREE;
12076         }
12077     }
12078
12079   if (! ref)
12080     {
12081       /* If no such tag is yet defined, create a forward-reference node
12082          and record it as the "definition".
12083          When a real declaration of this type is found,
12084          the forward-reference will be altered into a real type.  */
12085       if (code == ENUMERAL_TYPE)
12086         {
12087           cp_error ("use of enum `%#D' without previous declaration", name);
12088
12089           ref = make_node (ENUMERAL_TYPE);
12090
12091           /* Give the type a default layout like unsigned int
12092              to avoid crashing if it does not get defined.  */
12093           TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
12094           TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
12095           TREE_UNSIGNED (ref) = 1;
12096           TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
12097           TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
12098           TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
12099
12100           /* Enable us to recognize when a type is created in class context.
12101              To do nested classes correctly, this should probably be cleared
12102              out when we leave this classes scope.  Currently this in only
12103              done in `start_enum'.  */
12104
12105           pushtag (name, ref, globalize);
12106         }
12107       else
12108         {
12109           struct binding_level *old_b = class_binding_level;
12110
12111           ref = make_lang_type (code);
12112           TYPE_CONTEXT (ref) = context;
12113
12114 #ifdef NONNESTED_CLASSES
12115           /* Class types don't nest the way enums do.  */
12116           class_binding_level = (struct binding_level *)0;
12117 #endif
12118           pushtag (name, ref, globalize);
12119           class_binding_level = old_b;
12120         }
12121     }
12122   else
12123     {
12124       /* If it no longer looks like a nested type, make sure it's
12125          in global scope.  
12126          If it is not an IDENTIFIER, this is not a declaration */
12127       if (b->namespace_p && !class_binding_level
12128           && TREE_CODE (name) == IDENTIFIER_NODE
12129           && IDENTIFIER_NAMESPACE_VALUE (name) == NULL_TREE)
12130         SET_IDENTIFIER_NAMESPACE_VALUE (name, TYPE_NAME (ref));
12131
12132       if (!globalize && processing_template_decl && IS_AGGR_TYPE (ref))
12133         redeclare_class_template (ref, current_template_parms);
12134     }
12135
12136   /* Until the type is defined, tentatively accept whatever
12137      structure tag the user hands us.  */
12138   if (TYPE_SIZE (ref) == NULL_TREE
12139       && ref != current_class_type
12140       /* Have to check this, in case we have contradictory tag info.  */
12141       && IS_AGGR_TYPE_CODE (TREE_CODE (ref)))
12142     {
12143       if (tag_code == class_type)
12144         CLASSTYPE_DECLARED_CLASS (ref) = 1;
12145       else if (tag_code == record_type)
12146         CLASSTYPE_DECLARED_CLASS (ref) = 0;
12147     }
12148
12149   TREE_TYPE (ref) = attributes;
12150
12151   return ref;
12152 }
12153
12154 tree
12155 xref_tag_from_type (old, id, globalize)
12156      tree old, id;
12157      int globalize;
12158 {
12159   tree code_type_node;
12160
12161   if (TREE_CODE (old) == RECORD_TYPE)
12162     code_type_node = (CLASSTYPE_DECLARED_CLASS (old)
12163                       ? class_type_node : record_type_node);
12164   else
12165     code_type_node = union_type_node;
12166
12167   if (id == NULL_TREE)
12168     id = TYPE_IDENTIFIER (old);
12169
12170   return xref_tag (code_type_node, id, globalize);
12171 }
12172
12173 /* REF is a type (named NAME), for which we have just seen some
12174    baseclasses.  BINFO is a list of those baseclasses; the
12175    TREE_PURPOSE is an access_* node, and the TREE_VALUE is the type of
12176    the base-class.  CODE_TYPE_NODE indicates whether REF is a class,
12177    struct, or union.  */
12178
12179 void
12180 xref_basetypes (code_type_node, name, ref, binfo)
12181      tree code_type_node;
12182      tree name, ref;
12183      tree binfo;
12184 {
12185   /* In the declaration `A : X, Y, ... Z' we mark all the types
12186      (A, X, Y, ..., Z) so we can check for duplicates.  */
12187   tree binfos;
12188   tree base;
12189
12190   int i, len;
12191   enum tag_types tag_code = (enum tag_types) TREE_INT_CST_LOW (code_type_node);
12192
12193   if (tag_code == union_type)
12194     {
12195       cp_error ("derived union `%T' invalid", ref);
12196       return;
12197     }
12198
12199   len = list_length (binfo);
12200
12201   /* First, make sure that any templates in base-classes are
12202      instantiated.  This ensures that if we call ourselves recursively
12203      we do not get confused about which classes are marked and which
12204      are not.  */
12205   for (base = binfo; base; base = TREE_CHAIN (base))
12206     complete_type (TREE_VALUE (base));
12207
12208   SET_CLASSTYPE_MARKED (ref);
12209   BINFO_BASETYPES (TYPE_BINFO (ref)) = binfos = make_tree_vec (len);
12210
12211   for (i = 0; binfo; binfo = TREE_CHAIN (binfo))
12212     {
12213       /* The base of a derived struct is public by default.  */
12214       int via_public
12215         = (TREE_PURPOSE (binfo) == access_public_node
12216            || TREE_PURPOSE (binfo) == access_public_virtual_node
12217            || (tag_code != class_type
12218                && (TREE_PURPOSE (binfo) == access_default_node
12219                    || TREE_PURPOSE (binfo) == access_default_virtual_node)));
12220       int via_protected
12221         = (TREE_PURPOSE (binfo) == access_protected_node
12222            || TREE_PURPOSE (binfo) == access_protected_virtual_node);
12223       int via_virtual
12224         = (TREE_PURPOSE (binfo) == access_private_virtual_node
12225            || TREE_PURPOSE (binfo) == access_protected_virtual_node
12226            || TREE_PURPOSE (binfo) == access_public_virtual_node
12227            || TREE_PURPOSE (binfo) == access_default_virtual_node);
12228       tree basetype = TREE_VALUE (binfo);
12229       tree base_binfo;
12230
12231       if (basetype && TREE_CODE (basetype) == TYPE_DECL)
12232         basetype = TREE_TYPE (basetype);
12233       if (!basetype
12234           || (TREE_CODE (basetype) != RECORD_TYPE
12235               && TREE_CODE (basetype) != TYPENAME_TYPE
12236               && TREE_CODE (basetype) != TEMPLATE_TYPE_PARM
12237               && TREE_CODE (basetype) != TEMPLATE_TEMPLATE_PARM))
12238         {
12239           cp_error ("base type `%T' fails to be a struct or class type",
12240                     TREE_VALUE (binfo));
12241           continue;
12242         }
12243
12244       GNU_xref_hier (name, basetype, via_public, via_virtual, 0);
12245
12246       /* This code replaces similar code in layout_basetypes.
12247          We put the complete_type first for implicit `typename'.  */
12248       if (TYPE_SIZE (basetype) == NULL_TREE
12249           && ! (current_template_parms && uses_template_parms (basetype)))
12250         {
12251           cp_error ("base class `%T' has incomplete type", basetype);
12252           continue;
12253         }
12254       else
12255         {
12256           if (CLASSTYPE_MARKED (basetype))
12257             {
12258               if (basetype == ref)
12259                 cp_error ("recursive type `%T' undefined", basetype);
12260               else
12261                 cp_error ("duplicate base type `%T' invalid", basetype);
12262               continue;
12263             }
12264
12265           if (TYPE_FOR_JAVA (basetype)
12266               && (current_lang_stack 
12267                   == &VARRAY_TREE (current_lang_base, 0)))
12268             TYPE_FOR_JAVA (ref) = 1;
12269
12270           /* Note that the BINFO records which describe individual
12271              inheritances are *not* shared in the lattice!  They
12272              cannot be shared because a given baseclass may be
12273              inherited with different `accessibility' by different
12274              derived classes.  (Each BINFO record describing an
12275              individual inheritance contains flags which say what
12276              the `accessibility' of that particular inheritance is.)  */
12277   
12278           base_binfo 
12279             = make_binfo (integer_zero_node, basetype,
12280                           CLASS_TYPE_P (basetype)
12281                           ? TYPE_BINFO_VTABLE (basetype) : NULL_TREE,
12282                           CLASS_TYPE_P (basetype)
12283                           ? TYPE_BINFO_VIRTUALS (basetype) : NULL_TREE);
12284  
12285           TREE_VEC_ELT (binfos, i) = base_binfo;
12286           TREE_VIA_PUBLIC (base_binfo) = via_public;
12287           TREE_VIA_PROTECTED (base_binfo) = via_protected;
12288           TREE_VIA_VIRTUAL (base_binfo) = via_virtual;
12289           BINFO_INHERITANCE_CHAIN (base_binfo) = TYPE_BINFO (ref);
12290
12291           /* We need to unshare the binfos now so that lookups during class
12292              definition work.  */
12293           unshare_base_binfos (base_binfo);
12294
12295           SET_CLASSTYPE_MARKED (basetype);
12296
12297           /* We are free to modify these bits because they are meaningless
12298              at top level, and BASETYPE is a top-level type.  */
12299           if (via_virtual || TYPE_USES_VIRTUAL_BASECLASSES (basetype))
12300             {
12301               TYPE_USES_VIRTUAL_BASECLASSES (ref) = 1;
12302               TYPE_USES_COMPLEX_INHERITANCE (ref) = 1;
12303             }
12304
12305           if (CLASS_TYPE_P (basetype))
12306             {
12307               TYPE_GETS_NEW (ref) |= TYPE_GETS_NEW (basetype);
12308               TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
12309             }
12310
12311           i += 1;
12312         }
12313     }
12314   if (i)
12315     TREE_VEC_LENGTH (binfos) = i;
12316   else
12317     BINFO_BASETYPES (TYPE_BINFO (ref)) = NULL_TREE;
12318
12319   if (i > 1)
12320     TYPE_USES_MULTIPLE_INHERITANCE (ref) = 1;
12321   else if (i == 1)
12322     {
12323       tree basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, 0));
12324       
12325       if (CLASS_TYPE_P (basetype))
12326         TYPE_USES_MULTIPLE_INHERITANCE (ref)
12327           = TYPE_USES_MULTIPLE_INHERITANCE (basetype);
12328     }
12329
12330   if (TYPE_USES_MULTIPLE_INHERITANCE (ref))
12331     TYPE_USES_COMPLEX_INHERITANCE (ref) = 1;
12332
12333   /* Unmark all the types.  */
12334   while (--i >= 0)
12335     CLEAR_CLASSTYPE_MARKED (BINFO_TYPE (TREE_VEC_ELT (binfos, i)));
12336   CLEAR_CLASSTYPE_MARKED (ref);
12337
12338   /* Now that we know all the base-classes, set up the list of virtual
12339      bases.  */
12340   CLASSTYPE_VBASECLASSES (ref) = get_vbase_types (ref);
12341 }
12342   
12343 \f
12344 /* Begin compiling the definition of an enumeration type.
12345    NAME is its name (or null if anonymous).
12346    Returns the type object, as yet incomplete.
12347    Also records info about it so that build_enumerator
12348    may be used to declare the individual values as they are read.  */
12349
12350 tree
12351 start_enum (name)
12352      tree name;
12353 {
12354   register tree enumtype = NULL_TREE;
12355   struct binding_level *b = current_binding_level;
12356
12357   /* If this is the real definition for a previous forward reference,
12358      fill in the contents in the same object that used to be the
12359      forward reference.  */
12360
12361   if (name != NULL_TREE)
12362     enumtype = lookup_tag (ENUMERAL_TYPE, name, b, 1);
12363
12364   if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE)
12365     {
12366       cp_error ("multiple definition of `%#T'", enumtype);
12367       cp_error_at ("previous definition here", enumtype);
12368       /* Clear out TYPE_VALUES, and start again.  */
12369       TYPE_VALUES (enumtype) = NULL_TREE;
12370     }
12371   else
12372     {
12373       enumtype = make_node (ENUMERAL_TYPE);
12374       pushtag (name, enumtype, 0);
12375     }
12376
12377   if (current_class_type)
12378     TREE_ADDRESSABLE (b->tags) = 1;
12379
12380   GNU_xref_decl (current_function_decl, enumtype);
12381   return enumtype;
12382 }
12383
12384 /* After processing and defining all the values of an enumeration type,
12385    install their decls in the enumeration type and finish it off.
12386    ENUMTYPE is the type object and VALUES a list of name-value pairs.
12387    Returns ENUMTYPE.  */
12388
12389 tree
12390 finish_enum (enumtype)
12391      tree enumtype;
12392 {
12393   register tree minnode = NULL_TREE, maxnode = NULL_TREE;
12394   /* Calculate the maximum value of any enumerator in this type.  */
12395
12396   tree values = TYPE_VALUES (enumtype);
12397   if (values)
12398     {
12399       tree pair;
12400
12401       for (pair = values; pair; pair = TREE_CHAIN (pair))
12402         {
12403           tree decl;
12404           tree value;
12405
12406           /* The TREE_VALUE is a CONST_DECL for this enumeration
12407              constant.  */
12408           decl = TREE_VALUE (pair);
12409
12410           /* [dcl.enum]
12411
12412              Following the closing brace of an enum-specifier, each
12413              enumerator has the type of its enumeration.  Prior to the
12414              closing brace, the type of each enumerator is the type of
12415              its initializing value.  */
12416           TREE_TYPE (decl) = enumtype;
12417
12418           /* The DECL_INITIAL will be NULL if we are processing a
12419              template declaration and this enumeration constant had no
12420              explicit initializer.  */
12421           value = DECL_INITIAL (decl);
12422           if (value && !processing_template_decl)
12423             {
12424               /* Set the TREE_TYPE for the VALUE as well.  That's so
12425                  that when we call decl_constant_value we get an
12426                  entity of the right type (but with the constant
12427                  value).  Since we shouldn't ever call
12428                  decl_constant_value on a template type, there's no
12429                  reason to do that when processing_template_decl.
12430                  And, if the expression is something like a
12431                  TEMPLATE_PARM_INDEX or a CAST_EXPR doing so will
12432                  wreak havoc on the intended type of the expression.  
12433
12434                  Of course, there's also no point in trying to compute
12435                  minimum or maximum values if we're in a template.  */
12436               TREE_TYPE (value) = enumtype;
12437
12438               if (!minnode)
12439                 minnode = maxnode = value;
12440               else if (tree_int_cst_lt (maxnode, value))
12441                 maxnode = value;
12442               else if (tree_int_cst_lt (value, minnode))
12443                 minnode = value;
12444             }
12445
12446           if (processing_template_decl) 
12447             /* If this is just a template, leave the CONST_DECL
12448                alone.  That way tsubst_copy will find CONST_DECLs for
12449                CONST_DECLs, and not INTEGER_CSTs.  */
12450             ;
12451           else
12452             /* In the list we're building up, we want the enumeration
12453                values, not the CONST_DECLs.  */
12454             TREE_VALUE (pair) = value;
12455         }
12456     }
12457   else
12458     maxnode = minnode = integer_zero_node;
12459
12460   TYPE_VALUES (enumtype) = nreverse (values);
12461
12462   if (processing_template_decl)
12463     {
12464       tree scope = current_scope ();
12465       if (scope && TREE_CODE (scope) == FUNCTION_DECL)
12466         add_tree (build_min (TAG_DEFN, enumtype));
12467     }
12468   else
12469     {
12470       int unsignedp = tree_int_cst_sgn (minnode) >= 0;
12471       int lowprec = min_precision (minnode, unsignedp);
12472       int highprec = min_precision (maxnode, unsignedp);
12473       int precision = MAX (lowprec, highprec);
12474       tree tem;
12475
12476       TYPE_SIZE (enumtype) = NULL_TREE;
12477
12478       /* Set TYPE_MIN_VALUE and TYPE_MAX_VALUE according to `precision'.  */
12479
12480       TYPE_PRECISION (enumtype) = precision;
12481       if (unsignedp)
12482         fixup_unsigned_type (enumtype);
12483       else
12484         fixup_signed_type (enumtype);
12485
12486       if (flag_short_enums || (precision > TYPE_PRECISION (integer_type_node)))
12487         /* Use the width of the narrowest normal C type which is wide
12488            enough.  */ 
12489         TYPE_PRECISION (enumtype) = TYPE_PRECISION (type_for_size
12490                                                     (precision, 1));
12491       else
12492         TYPE_PRECISION (enumtype) = TYPE_PRECISION (integer_type_node);
12493
12494       TYPE_SIZE (enumtype) = 0;
12495       layout_type (enumtype);
12496     
12497       /* Fix up all variant types of this enum type.  */
12498       for (tem = TYPE_MAIN_VARIANT (enumtype); tem;
12499            tem = TYPE_NEXT_VARIANT (tem))
12500         {
12501           TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
12502           TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
12503           TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
12504           TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
12505           TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
12506           TYPE_MODE (tem) = TYPE_MODE (enumtype);
12507           TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
12508           TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
12509           TREE_UNSIGNED (tem) = TREE_UNSIGNED (enumtype);
12510         }
12511
12512       /* Finish debugging output for this type.  */
12513       rest_of_type_compilation (enumtype, namespace_bindings_p ());
12514     }
12515
12516   return enumtype;
12517 }
12518
12519 /* Build and install a CONST_DECL for an enumeration constant of the
12520    enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
12521    Assignment of sequential values by default is handled here.  */
12522
12523 void
12524 build_enumerator (name, value, enumtype)
12525      tree name;
12526      tree value;
12527      tree enumtype;
12528 {
12529   tree decl;
12530   tree context;
12531   tree type;
12532   tree values;
12533
12534   /* Remove no-op casts from the value.  */
12535   if (value)
12536     STRIP_TYPE_NOPS (value);
12537
12538   if (! processing_template_decl)
12539     {
12540       /* Validate and default VALUE.  */
12541       if (value != NULL_TREE)
12542         {
12543           if (TREE_READONLY_DECL_P (value))
12544             value = decl_constant_value (value);
12545
12546           if (TREE_CODE (value) == INTEGER_CST)
12547             {
12548               value = default_conversion (value);
12549               constant_expression_warning (value);
12550             }
12551           else
12552             {
12553               cp_error ("enumerator value for `%D' not integer constant", name);
12554               value = NULL_TREE;
12555             }
12556         }
12557
12558       /* Default based on previous value.  */
12559       if (value == NULL_TREE && ! processing_template_decl)
12560         {
12561           tree prev_value;
12562
12563           if (TYPE_VALUES (enumtype))
12564             {
12565               /* The next value is the previous value ... */
12566               prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
12567               /* ... plus one.  */
12568               value = build_binary_op_nodefault (PLUS_EXPR,
12569                                                  prev_value,
12570                                                  integer_one_node,
12571                                                  PLUS_EXPR);
12572               
12573               if (tree_int_cst_lt (value, prev_value))
12574                 cp_error ("overflow in enumeration values at `%D'", name);
12575             }
12576           else
12577             value = integer_zero_node;
12578         }
12579
12580       /* Remove no-op casts from the value.  */
12581       if (value)
12582         STRIP_TYPE_NOPS (value);
12583 #if 0
12584       /* To fix MAX_VAL enum consts. (bkoz)  */
12585       TREE_TYPE (value) = integer_type_node;
12586 #endif
12587     }
12588
12589   /* We always have to copy here; not all INTEGER_CSTs are unshared.
12590      Even in other cases, we will later (in finish_enum) be setting
12591      the type of VALUE.  But, we don't need to make a copy if this
12592      VALUE is one of the enumeration constants for this same
12593      enumeration type.  */
12594   for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
12595     if (TREE_VALUE (values) == value)
12596       break;
12597   /* If we didn't break out of the loop, then we do need a copy.  */
12598   if (!values && value)
12599     value = copy_node (value);
12600
12601   /* C++ associates enums with global, function, or class declarations.  */
12602   context = current_scope ();
12603
12604   /* Build the actual enumeration constant.  Note that the enumeration
12605     constants have the type of their initializers until the
12606     enumeration is complete:
12607
12608       [ dcl.enum ]
12609
12610       Following the closing brace of an enum-specifier, each enumer-
12611       ator has the type of its enumeration.  Prior to the closing
12612       brace, the type of each enumerator is the type of its
12613       initializing value.
12614
12615     In finish_enum we will reset the type.  Of course, if we're
12616     processing a template, there may be no value.   */
12617   type = value ? TREE_TYPE (value) : NULL_TREE;
12618
12619   if (context && context == current_class_type)
12620     /* This enum declaration is local to the class.  We need the full
12621       lang_decl so that we can record DECL_CLASS_CONTEXT, for example.  */
12622     decl = build_lang_decl (CONST_DECL, name, type);
12623   else
12624     /* It's a global enum, or it's local to a function.  (Note local to
12625       a function could mean local to a class method.  */
12626     decl = build_decl (CONST_DECL, name, type);
12627
12628   DECL_CONTEXT (decl) = FROB_CONTEXT (context);
12629   DECL_INITIAL (decl) = value;
12630   TREE_READONLY (decl) = 1;
12631
12632   if (context && context == current_class_type)
12633     /* In something like `struct S { enum E { i = 7 }; };' we put `i'
12634       on the TYPE_FIELDS list for `S'.  (That's so that you can say
12635       things like `S::i' later.)  */
12636     finish_member_declaration (decl);
12637   else
12638     {
12639       pushdecl (decl);
12640       GNU_xref_decl (current_function_decl, decl);
12641     }
12642
12643   /* Add this enumeration constant to the list for this type.  */
12644   TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
12645 }
12646
12647 \f
12648 static int function_depth;
12649
12650 /* We're defining DECL.  Make sure that it's type is OK.  */
12651
12652 static void
12653 check_function_type (decl)
12654      tree decl;
12655 {
12656   tree fntype = TREE_TYPE (decl);
12657
12658   /* In a function definition, arg types must be complete.  */
12659   require_complete_types_for_parms (current_function_parms);
12660
12661   if (TYPE_SIZE (complete_type (TREE_TYPE (fntype))) == NULL_TREE)
12662     {
12663       cp_error ("return type `%#T' is incomplete", TREE_TYPE (fntype));
12664
12665       /* Make it return void instead, but don't change the
12666          type of the DECL_RESULT, in case we have a named return value.  */
12667       if (TREE_CODE (fntype) == METHOD_TYPE)
12668         {
12669           tree ctype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fntype)));
12670           TREE_TYPE (decl)
12671             = build_cplus_method_type (ctype,
12672                                        void_type_node,
12673                                        FUNCTION_ARG_CHAIN (decl));
12674         }
12675       else
12676         TREE_TYPE (decl)
12677           = build_function_type (void_type_node,
12678                                  TYPE_ARG_TYPES (TREE_TYPE (decl)));
12679       TREE_TYPE (decl) 
12680         = build_exception_variant (fntype,
12681                                    TYPE_RAISES_EXCEPTIONS (fntype));
12682     }
12683   else
12684     abstract_virtuals_error (decl, TREE_TYPE (fntype));
12685 }
12686
12687 /* Create the FUNCTION_DECL for a function definition.
12688    DECLSPECS and DECLARATOR are the parts of the declaration;
12689    they describe the function's name and the type it returns,
12690    but twisted together in a fashion that parallels the syntax of C.
12691
12692    FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
12693    DECLARATOR is really the DECL for the function we are about to
12694    process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
12695    indicating that the function is an inline defined in-class, and
12696    SF_EXPAND indicating that we should generate RTL for this
12697    function.  
12698    
12699    This function creates a binding context for the function body
12700    as well as setting up the FUNCTION_DECL in current_function_decl.
12701
12702    Returns 1 on success.  If the DECLARATOR is not suitable for a function
12703    (it defines a datum instead), we return 0, which tells
12704    yyparse to report a parse error.
12705
12706    For C++, we must first check whether that datum makes any sense.
12707    For example, "class A local_a(1,2);" means that variable local_a
12708    is an aggregate of type A, which should have a constructor
12709    applied to it with the argument list [1, 2].  */
12710
12711 int
12712 start_function (declspecs, declarator, attrs, flags)
12713      tree declspecs, declarator, attrs;
12714      int flags;
12715 {
12716   tree decl1;
12717   tree ctype = NULL_TREE;
12718   tree fntype;
12719   tree restype;
12720   extern int have_extern_spec;
12721   extern int used_extern_spec;
12722   int doing_friend = 0;
12723   struct binding_level *bl;
12724
12725   /* Sanity check.  */
12726   my_friendly_assert (TREE_CODE (TREE_VALUE (void_list_node)) == VOID_TYPE, 160);
12727   my_friendly_assert (TREE_CHAIN (void_list_node) == NULL_TREE, 161);
12728
12729   /* This should only be done once on the top most decl.  */
12730   if (have_extern_spec && !used_extern_spec)
12731     {
12732       declspecs = decl_tree_cons (NULL_TREE, get_identifier ("extern"), declspecs);
12733       used_extern_spec = 1;
12734     }
12735
12736   if (flags & SF_PRE_PARSED)
12737     {
12738       decl1 = declarator;
12739
12740       fntype = TREE_TYPE (decl1);
12741       if (TREE_CODE (fntype) == METHOD_TYPE)
12742         ctype = TYPE_METHOD_BASETYPE (fntype);
12743
12744       /* ANSI C++ June 5 1992 WP 11.4.5.  A friend function defined in a
12745          class is in the (lexical) scope of the class in which it is
12746          defined.  */
12747       if (!ctype && DECL_FRIEND_P (decl1))
12748         {
12749           ctype = DECL_CLASS_CONTEXT (decl1);
12750
12751           /* CTYPE could be null here if we're dealing with a template;
12752              for example, `inline friend float foo()' inside a template
12753              will have no CTYPE set.  */
12754           if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
12755             ctype = NULL_TREE;
12756           else
12757             doing_friend = 1;
12758         }
12759
12760       last_function_parms = DECL_ARGUMENTS (decl1);
12761       last_function_parm_tags = NULL_TREE;
12762     }
12763   else
12764     {
12765       decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, NULL_TREE);
12766       /* If the declarator is not suitable for a function definition,
12767          cause a syntax error.  */
12768       if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL) return 0;
12769
12770       fntype = TREE_TYPE (decl1);
12771
12772       restype = TREE_TYPE (fntype);
12773       if (CLASS_TYPE_P (restype) && !CLASSTYPE_GOT_SEMICOLON (restype))
12774         {
12775           cp_error ("semicolon missing after declaration of `%#T'", restype);
12776           shadow_tag (build_expr_list (NULL_TREE, restype));
12777           CLASSTYPE_GOT_SEMICOLON (restype) = 1;
12778           if (TREE_CODE (fntype) == FUNCTION_TYPE)
12779             fntype = build_function_type (integer_type_node,
12780                                           TYPE_ARG_TYPES (fntype));
12781           else
12782             fntype = build_cplus_method_type (build_type_variant (TYPE_METHOD_BASETYPE (fntype), TREE_READONLY (decl1), TREE_SIDE_EFFECTS (decl1)),
12783                                               integer_type_node,
12784                                               TYPE_ARG_TYPES (fntype));
12785           TREE_TYPE (decl1) = fntype;
12786         }
12787
12788       if (TREE_CODE (fntype) == METHOD_TYPE)
12789         ctype = TYPE_METHOD_BASETYPE (fntype);
12790       else if (DECL_MAIN_P (decl1))
12791         {
12792           /* If this doesn't return integer_type, complain.  */
12793           if (TREE_TYPE (TREE_TYPE (decl1)) != integer_type_node)
12794             {
12795               if (pedantic || warn_return_type)
12796                 pedwarn ("return type for `main' changed to `int'");
12797               TREE_TYPE (decl1) = fntype = default_function_type;
12798             }
12799         }
12800     }
12801   
12802   /* Sometimes we don't notice that a function is a static member, and
12803      build a METHOD_TYPE for it.  Fix that up now.  */
12804   if (ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
12805       && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE)
12806     {
12807       revert_static_member_fn (&decl1, NULL, NULL);
12808       last_function_parms = TREE_CHAIN (last_function_parms);
12809       ctype = NULL_TREE;
12810     }
12811
12812   /* Warn if function was previously implicitly declared
12813      (but not if we warned then).  */
12814   if (! warn_implicit
12815       && IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)) != NULL_TREE)
12816     cp_warning_at ("`%D' implicitly declared before its definition", IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)));
12817
12818   /* Set up current_class_type, and enter the scope of the class, if
12819      appropriate.  */
12820   if (ctype)
12821     push_nested_class (ctype, 1);
12822   else if (DECL_STATIC_FUNCTION_P (decl1))
12823     push_nested_class (DECL_CONTEXT (decl1), 2);
12824
12825   /* Now that we have entered the scope of the class, we must restore
12826      the bindings for any template parameters surrounding DECL1, if it
12827      is an inline member template.  (Order is important; consider the
12828      case where a template parameter has the same name as a field of
12829      the class.)  It is not until after this point that
12830      PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly.  */
12831   if (flags & SF_INCLASS_INLINE)
12832     maybe_begin_member_template_processing (decl1);
12833
12834   /* Effective C++ rule 15.  See also c_expand_return.  */
12835   if (warn_ecpp
12836       && DECL_NAME (decl1) == ansi_opname[(int) MODIFY_EXPR]
12837       && TREE_CODE (TREE_TYPE (fntype)) == VOID_TYPE)
12838     cp_warning ("`operator=' should return a reference to `*this'");
12839
12840   /* Make the init_value nonzero so pushdecl knows this is not tentative.
12841      error_mark_node is replaced below (in poplevel) with the BLOCK.  */
12842   if (!DECL_INITIAL (decl1))
12843     DECL_INITIAL (decl1) = error_mark_node;
12844
12845 #ifdef SET_DEFAULT_DECL_ATTRIBUTES
12846   SET_DEFAULT_DECL_ATTRIBUTES (decl1, attrs);
12847 #endif
12848   
12849   /* This function exists in static storage.
12850      (This does not mean `static' in the C sense!)  */
12851   TREE_STATIC (decl1) = 1;
12852
12853   /* We must call push_template_decl after current_class_type is set
12854      up.  (If we are processing inline definitions after exiting a
12855      class scope, current_class_type will be NULL_TREE until set above
12856      by push_nested_class.)  */
12857   if (processing_template_decl)
12858     decl1 = push_template_decl (decl1);
12859
12860   /* We are now in the scope of the function being defined.  */
12861   current_function_decl = decl1;
12862
12863   /* Save the parm names or decls from this function's declarator
12864      where store_parm_decls will find them.  */
12865   current_function_parms = last_function_parms;
12866   current_function_parm_tags = last_function_parm_tags;
12867
12868   /* Make sure the parameter and return types are reasonable.  When
12869      you declare a function, these types can be incomplete, but they
12870      must be complete when you define the function.  */
12871   if (! processing_template_decl)
12872     check_function_type (decl1);
12873
12874   /* Build the return declaration for the function.  */
12875   restype = TREE_TYPE (fntype);
12876   if (!processing_template_decl)
12877     {
12878       if (!DECL_RESULT (decl1))
12879         {
12880           DECL_RESULT (decl1)
12881             = build_decl (RESULT_DECL, 0, TYPE_MAIN_VARIANT (restype));
12882           c_apply_type_quals_to_decl (CP_TYPE_QUALS (restype), 
12883                                       DECL_RESULT (decl1)); 
12884         }
12885     }
12886   else
12887     /* Just use `void'.  Nobody will ever look at this anyhow.  */
12888     DECL_RESULT (decl1) = build_decl (RESULT_DECL, 0, void_type_node);
12889
12890   /* Initialize RTL machinery.  We cannot do this until
12891      CURRENT_FUNCTION_DECL and DECL_RESULT are set up.  We do this
12892      even when processing a template; this is how we get
12893      CURRENT_FUNCTION set up, and our per-function variables
12894      initialized.  */
12895   bl = current_binding_level;
12896   init_function_start (decl1, input_filename, lineno);
12897   current_binding_level = bl;
12898   expanding_p = (flags & SF_EXPAND) != 0;
12899
12900   /* Even though we're inside a function body, we still don't want to
12901      call expand_expr to calculate the size of a variable-sized array.
12902      We haven't necessarily assigned RTL to all variables yet, so it's
12903      not safe to try to expand expressions involving them.  */
12904   immediate_size_expand = 0;
12905   current_function->x_dont_save_pending_sizes_p = 1;
12906
12907   /* If we're building a statement-tree, start the tree now.  */
12908   if (processing_template_decl || !expanding_p)
12909     begin_stmt_tree (&DECL_SAVED_TREE (decl1));
12910
12911   /* Let the user know we're compiling this function.  */
12912   if (processing_template_decl || !building_stmt_tree ())
12913     announce_function (decl1);
12914
12915   /* Record the decl so that the function name is defined.
12916      If we already have a decl for this name, and it is a FUNCTION_DECL,
12917      use the old decl.  */
12918   if (!processing_template_decl && !(flags & SF_PRE_PARSED))
12919     {
12920       /* A specialization is not used to guide overload resolution.  */
12921       if ((flag_guiding_decls 
12922            || !DECL_TEMPLATE_SPECIALIZATION (decl1))
12923           && ! DECL_FUNCTION_MEMBER_P (decl1))
12924         decl1 = pushdecl (decl1);
12925       else
12926         {
12927           /* We need to set the DECL_CONTEXT. */
12928           if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
12929             DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
12930           /* And make sure we have enough default args.  */
12931           check_default_args (decl1);
12932         }
12933       DECL_MAIN_VARIANT (decl1) = decl1;
12934       fntype = TREE_TYPE (decl1);
12935     }
12936
12937   /* Reset these in case the call to pushdecl changed them.  */
12938   current_function_decl = decl1;
12939   current_function->decl = decl1;
12940
12941   /* Initialize the per-function data.  */
12942   if (!DECL_PENDING_INLINE_P (decl1) && DECL_SAVED_FUNCTION_DATA (decl1))
12943     {
12944       /* If we already parsed this function, and we're just expanding it
12945          now, restore saved state.  */
12946       struct binding_level *bl = current_binding_level;
12947       *cp_function_chain = *DECL_SAVED_FUNCTION_DATA (decl1);
12948       current_binding_level = bl;
12949
12950       /* This function is being processed in whole-function mode; we
12951          already did semantic analysis.  */
12952       current_function->x_whole_function_mode_p = 1;
12953
12954       /* If we decided that we didn't want to inline this function,
12955          make sure the back-end knows that.  */
12956       if (!current_function_cannot_inline)
12957         current_function_cannot_inline = cp_function_chain->cannot_inline;
12958
12959       /* We don't need the saved data anymore.  */
12960       free (DECL_SAVED_FUNCTION_DATA (decl1));
12961       DECL_SAVED_FUNCTION_DATA (decl1) = NULL;
12962     }
12963   else if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
12964     {
12965       /* We know that this was set up by `grokclassfn'.  We do not
12966          wait until `store_parm_decls', since evil parse errors may
12967          never get us to that point.  Here we keep the consistency
12968          between `current_class_type' and `current_class_ptr'.  */
12969       tree t = DECL_ARGUMENTS (decl1);
12970               
12971       my_friendly_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL, 
12972                           162);
12973       my_friendly_assert (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE,
12974                           19990811);
12975           
12976       cp_function_chain->x_current_class_ref 
12977         = build_indirect_ref (t, NULL_PTR);
12978       cp_function_chain->x_current_class_ptr = t;
12979
12980       /* Constructors and destructors need to know whether they're "in
12981          charge" of initializing virtual base classes.  */
12982       if (DECL_DESTRUCTOR_P (decl1))
12983         current_in_charge_parm = TREE_CHAIN (t);
12984       else if (DECL_CONSTRUCTOR_P (decl1)
12985                && TREE_CHAIN (t)
12986                && DECL_ARTIFICIAL (TREE_CHAIN (t))
12987                && (DECL_NAME (TREE_CHAIN (t))
12988                    == in_charge_identifier))
12989         current_in_charge_parm = TREE_CHAIN (t);
12990     }
12991
12992   if (DECL_INTERFACE_KNOWN (decl1))
12993     {
12994       tree ctx = hack_decl_function_context (decl1);
12995
12996       if (DECL_NOT_REALLY_EXTERN (decl1))
12997         DECL_EXTERNAL (decl1) = 0;
12998
12999       if (ctx != NULL_TREE && DECL_THIS_INLINE (ctx) 
13000           && TREE_PUBLIC (ctx))
13001         /* This is a function in a local class in an extern inline
13002            function.  */
13003         comdat_linkage (decl1);
13004     }
13005   /* If this function belongs to an interface, it is public.
13006      If it belongs to someone else's interface, it is also external.
13007      This only affects inlines and template instantiations.  */
13008   else if (interface_unknown == 0
13009            && (! DECL_TEMPLATE_INSTANTIATION (decl1)
13010                || flag_alt_external_templates))
13011     {
13012       if (DECL_THIS_INLINE (decl1) || DECL_TEMPLATE_INSTANTIATION (decl1)
13013           || processing_template_decl)
13014         {
13015           DECL_EXTERNAL (decl1)
13016             = (interface_only
13017                || (DECL_THIS_INLINE (decl1) && ! flag_implement_inlines
13018                    && !DECL_VINDEX (decl1)));
13019
13020           /* For WIN32 we also want to put these in linkonce sections.  */
13021           maybe_make_one_only (decl1);
13022         }
13023       else
13024         DECL_EXTERNAL (decl1) = 0;
13025       DECL_NOT_REALLY_EXTERN (decl1) = 0;
13026       DECL_INTERFACE_KNOWN (decl1) = 1;
13027     }
13028   else if (interface_unknown && interface_only
13029            && (! DECL_TEMPLATE_INSTANTIATION (decl1)
13030                || flag_alt_external_templates))
13031     {
13032       /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
13033          interface, we will have interface_only set but not
13034          interface_known.  In that case, we don't want to use the normal
13035          heuristics because someone will supply a #pragma implementation
13036          elsewhere, and deducing it here would produce a conflict.  */
13037       comdat_linkage (decl1);
13038       DECL_EXTERNAL (decl1) = 0;
13039       DECL_INTERFACE_KNOWN (decl1) = 1;
13040       DECL_DEFER_OUTPUT (decl1) = 1;
13041     }
13042   else
13043     {
13044       /* This is a definition, not a reference.
13045          So clear DECL_EXTERNAL.  */
13046       DECL_EXTERNAL (decl1) = 0;
13047
13048       if ((DECL_THIS_INLINE (decl1) || DECL_TEMPLATE_INSTANTIATION (decl1))
13049           && ! DECL_INTERFACE_KNOWN (decl1)
13050           /* Don't try to defer nested functions for now.  */
13051           && ! hack_decl_function_context (decl1))
13052         DECL_DEFER_OUTPUT (decl1) = 1;
13053       else
13054         DECL_INTERFACE_KNOWN (decl1) = 1;
13055     }
13056
13057   if (doing_semantic_analysis_p ())
13058     {
13059       pushlevel (0);
13060       current_binding_level->parm_flag = 1;
13061     }
13062
13063   if (attrs)
13064     cplus_decl_attributes (decl1, NULL_TREE, attrs);
13065   
13066   if (!building_stmt_tree ())
13067     {
13068       GNU_xref_function (decl1, current_function_parms);
13069       make_function_rtl (decl1);
13070     }
13071
13072   /* Promote the value to int before returning it.  */
13073   if (C_PROMOTING_INTEGER_TYPE_P (restype))
13074     restype = type_promotes_to (restype);
13075
13076   /* If this fcn was already referenced via a block-scope `extern' decl
13077      (or an implicit decl), propagate certain information about the usage.  */
13078   if (TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (decl1)))
13079     TREE_ADDRESSABLE (decl1) = 1;
13080
13081   if (DECL_RESULT (decl1) == NULL_TREE)
13082     {
13083       DECL_RESULT (decl1)
13084         = build_decl (RESULT_DECL, 0, TYPE_MAIN_VARIANT (restype));
13085       TREE_READONLY (DECL_RESULT (decl1)) = CP_TYPE_CONST_P (restype);
13086       TREE_THIS_VOLATILE (DECL_RESULT (decl1)) = CP_TYPE_VOLATILE_P (restype);
13087     }
13088
13089   ++function_depth;
13090
13091   if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl1))
13092       && DECL_LANGUAGE (decl1) == lang_cplusplus)
13093     {
13094       dtor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
13095       DECL_CONTEXT (dtor_label) = current_function_decl;
13096     }
13097   else if (DECL_CONSTRUCTOR_P (decl1))
13098     {
13099       ctor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
13100       DECL_CONTEXT (ctor_label) = current_function_decl;
13101     }
13102
13103   return 1;
13104 }
13105 \f
13106 /* Called after store_parm_decls for a function-try-block.  */
13107
13108 void
13109 expand_start_early_try_stmts ()
13110 {
13111   expand_start_try_stmts ();
13112 }
13113
13114 /* Store the parameter declarations into the current function declaration.
13115    This is called after parsing the parameter declarations, before
13116    digesting the body of the function.
13117
13118    Also install to binding contour return value identifier, if any.  */
13119
13120 void
13121 store_parm_decls ()
13122 {
13123   register tree fndecl = current_function_decl;
13124   register tree parm;
13125   int parms_have_cleanups = 0;
13126   tree cleanups = NULL_TREE;
13127
13128   /* This is a list of types declared among parms in a prototype.  */
13129   tree parmtags = current_function_parm_tags;
13130
13131   /* This is a chain of any other decls that came in among the parm
13132      declarations.  If a parm is declared with  enum {foo, bar} x;
13133      then CONST_DECLs for foo and bar are put here.  */
13134   tree nonparms = NULL_TREE;
13135
13136   /* Create a binding level for the parms.  */
13137   if (!building_stmt_tree ())
13138     expand_start_bindings (2);
13139
13140   if (current_function_parms)
13141     {
13142       /* This case is when the function was defined with an ANSI prototype.
13143          The parms already have decls, so we need not do anything here
13144          except record them as in effect
13145          and complain if any redundant old-style parm decls were written.  */
13146
13147       tree specparms = current_function_parms;
13148       tree next;
13149
13150       if (doing_semantic_analysis_p ())
13151         {
13152           /* Must clear this because it might contain TYPE_DECLs declared
13153              at class level.  */
13154           storedecls (NULL_TREE);
13155
13156           /* If we're doing semantic analysis, then we'll call pushdecl
13157              for each of these.  We must do them in reverse order so that
13158              they end in the correct forward order.  */
13159           specparms = nreverse (specparms);
13160         }
13161
13162       for (parm = specparms; parm; parm = next)
13163         {
13164           next = TREE_CHAIN (parm);
13165           if (TREE_CODE (parm) == PARM_DECL)
13166             {
13167               tree type = TREE_TYPE (parm);
13168
13169               if (doing_semantic_analysis_p ())
13170                 {
13171                   tree cleanup;
13172               
13173                   if (DECL_NAME (parm) == NULL_TREE
13174                       || TREE_CODE (parm) != VOID_TYPE)
13175                     pushdecl (parm);
13176                   else
13177                     cp_error ("parameter `%D' declared void", parm);
13178
13179                   cleanup = maybe_build_cleanup (parm);
13180                   
13181                   if (cleanup)
13182                     cleanups = tree_cons (parm, cleanup, cleanups);
13183                 }
13184               else if (type != error_mark_node
13185                        && TYPE_NEEDS_DESTRUCTOR (type))
13186                 parms_have_cleanups = 1;
13187             }
13188           else
13189             {
13190               /* If we find an enum constant or a type tag,
13191                  put it aside for the moment.  */
13192               TREE_CHAIN (parm) = NULL_TREE;
13193               nonparms = chainon (nonparms, parm);
13194             }
13195         }
13196
13197       if (doing_semantic_analysis_p ())
13198         {
13199           /* Get the decls in their original chain order
13200              and record in the function.  This is all and only the
13201              PARM_DECLs that were pushed into scope by the loop above.  */
13202           DECL_ARGUMENTS (fndecl) = getdecls ();
13203           storetags (chainon (parmtags, gettags ()));
13204         }
13205     }
13206   else
13207     DECL_ARGUMENTS (fndecl) = NULL_TREE;
13208
13209   /* Now store the final chain of decls for the arguments
13210      as the decl-chain of the current lexical scope.
13211      Put the enumerators in as well, at the front so that
13212      DECL_ARGUMENTS is not modified.  */
13213   if (doing_semantic_analysis_p ())
13214     storedecls (chainon (nonparms, DECL_ARGUMENTS (fndecl)));
13215
13216   /* Initialize the RTL code for the function.  */
13217   DECL_SAVED_INSNS (fndecl) = 0;
13218   if (! building_stmt_tree ())
13219     expand_function_start (fndecl, parms_have_cleanups);
13220
13221   current_function_parms_stored = 1;
13222
13223   /* If this function is `main', emit a call to `__main'
13224      to run global initializers, etc.  */
13225   if (DECL_MAIN_P (fndecl) && !building_stmt_tree ())
13226     expand_main_function ();
13227
13228   /* Now that we have initialized the parms, we can start their
13229      cleanups.  We cannot do this before, since expand_decl_cleanup
13230      should not be called before the parm can be used.  */
13231   while (cleanups)
13232     {
13233       finish_decl_cleanup (TREE_PURPOSE (cleanups), 
13234                            TREE_VALUE (cleanups));
13235       cleanups = TREE_CHAIN (cleanups);
13236     }
13237
13238   /* Create a binding contour which can be used to catch
13239      cleanup-generated temporaries.  Also, if the return value needs or
13240      has initialization, deal with that now.  */
13241   if (parms_have_cleanups)
13242     {
13243       pushlevel (0);
13244       if (!building_stmt_tree ())
13245         expand_start_bindings (2);
13246     }
13247
13248   /* Do the starting of the exception specifications, if we have any.  */
13249   if (flag_exceptions && !processing_template_decl 
13250       && building_stmt_tree () 
13251       && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl)))
13252     current_eh_spec_try_block = expand_start_eh_spec ();
13253 }
13254
13255 /* Bind a name and initialization to the return value of
13256    the current function.  */
13257
13258 void
13259 store_return_init (decl)
13260      tree decl;
13261 {
13262   /* If this named return value comes in a register, put it in a
13263      pseudo-register.  */
13264   if (DECL_REGISTER (decl))
13265     {
13266       original_result_rtx = DECL_RTL (decl);
13267       DECL_RTL (decl) = gen_reg_rtx (DECL_MODE (decl));
13268     }
13269 }
13270
13271 \f
13272 /* We have finished doing semantic analysis on DECL, but have not yet
13273    generated RTL for its body.  Save away our current state, so that
13274    when we want to generate RTL later we know what to do.  */
13275
13276 static void
13277 save_function_data (decl)
13278      tree decl;
13279 {
13280   struct language_function *f;
13281
13282   /* Save the language-specific per-function data so that we can
13283      get it back when we really expand this function.  */
13284   my_friendly_assert (!DECL_PENDING_INLINE_P (decl),
13285                       19990908);
13286       
13287   /* Make a copy.  */
13288   f = ((struct language_function *) 
13289        xmalloc (sizeof (struct language_function)));
13290   bcopy ((char *) cp_function_chain, (char *) f,
13291          sizeof (struct language_function));
13292   DECL_SAVED_FUNCTION_DATA (decl) = f;
13293
13294   /* Clear out the bits we don't need.  */
13295   f->x_base_init_list = NULL_TREE;
13296   f->x_member_init_list = NULL_TREE;
13297   f->x_stmt_tree.x_last_stmt = NULL_TREE;
13298   f->x_stmt_tree.x_last_expr_type = NULL_TREE;
13299   f->x_result_rtx = NULL_RTX;
13300   f->x_named_label_uses = NULL;
13301   f->bindings = NULL;
13302
13303   /* When we get back here again, we will be expanding.  */
13304   f->x_expanding_p = 1;
13305
13306   /* If we've already decided that we cannot inline this function, we
13307      must remember that fact when we actually go to expand the
13308      function.  */
13309   f->cannot_inline = current_function_cannot_inline;
13310 }
13311
13312 /* At the end of every constructor we generate to code to return
13313    `this'.  Do that now.  */
13314
13315 static void
13316 finish_constructor_body ()
13317 {
13318   /* Any return from a constructor will end up here.  */
13319   add_tree (build_min_nt (LABEL_STMT, ctor_label));
13320
13321   /* Clear CTOR_LABEL so that finish_return_stmt knows to really
13322      generate the return, rather than a goto to CTOR_LABEL.  */
13323   ctor_label = NULL_TREE;
13324   /* In check_return_expr we translate an empty return from a
13325      constructor to a return of `this'.  */
13326   finish_return_stmt (NULL_TREE);
13327   /* Mark the end of the constructor.  */
13328   add_tree (build_min_nt (CTOR_STMT));
13329 }
13330
13331 /* At the end of every destructor we generate code to restore virtual
13332    function tables to the values desired by base classes and to call
13333    to base class destructors.  Do that now.  */
13334
13335 static void
13336 finish_destructor_body ()
13337 {
13338   tree compound_stmt;
13339   tree in_charge;
13340   tree virtual_size;
13341   tree exprstmt;
13342
13343   /* Create a block to contain all the extra code.  */
13344   compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
13345
13346   /* Any return from a destructor will end up here.  */
13347   add_tree (build_min_nt (LABEL_STMT, dtor_label));
13348
13349   /* Generate the code to call destructor on base class.  If this
13350      destructor belongs to a class with virtual functions, then set
13351      the virtual function table pointer to represent the type of our
13352      base class.  */
13353
13354   /* This side-effect makes call to `build_delete' generate the code
13355      we have to have at the end of this destructor.  `build_delete'
13356      will set the flag again.  */
13357   TYPE_HAS_DESTRUCTOR (current_class_type) = 0;
13358
13359   /* These are two cases where we cannot delegate deletion.  */
13360   if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type)
13361       || TYPE_GETS_REG_DELETE (current_class_type))
13362     in_charge = integer_zero_node;
13363   else
13364     in_charge = current_in_charge_parm;
13365
13366   exprstmt = build_delete (current_class_type,
13367                            current_class_ref, 
13368                            in_charge,
13369                            LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR|LOOKUP_NORMAL, 
13370                            0);
13371
13372   if (exprstmt != error_mark_node
13373       && (TREE_CODE (exprstmt) != NOP_EXPR
13374           || TREE_OPERAND (exprstmt, 0) != integer_zero_node
13375           || TYPE_USES_VIRTUAL_BASECLASSES (current_class_type)))
13376     {
13377       if (exprstmt != void_zero_node)
13378         /* Don't call `expand_expr_stmt' if we're not going to do
13379            anything, since -Wall will give a diagnostic.  */
13380         finish_expr_stmt (exprstmt);
13381
13382       /* Run destructors for all virtual baseclasses.  */
13383       if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
13384         {
13385           tree vbases = nreverse (copy_list (CLASSTYPE_VBASECLASSES (current_class_type)));
13386           tree if_stmt = begin_if_stmt ();
13387           finish_if_stmt_cond (build (BIT_AND_EXPR, integer_type_node,
13388                                       current_in_charge_parm, 
13389                                       integer_two_node),
13390                                if_stmt);
13391
13392           while (vbases)
13393             {
13394               if (TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (vbases)))
13395                 {
13396                   tree vb = get_vbase
13397                     (BINFO_TYPE (vbases),
13398                      TYPE_BINFO (current_class_type));
13399                   finish_expr_stmt
13400                     (build_scoped_method_call
13401                      (current_class_ref, vb, dtor_identifier,
13402                       build_expr_list (NULL_TREE, integer_zero_node)));
13403                 }
13404               vbases = TREE_CHAIN (vbases);
13405             }
13406
13407           finish_then_clause (if_stmt);
13408           finish_if_stmt ();
13409         }
13410     }
13411   
13412   virtual_size = c_sizeof (current_class_type);
13413
13414   /* At the end, call delete if that's what's requested.  */
13415   
13416   /* FDIS sez: At the point of definition of a virtual destructor
13417      (including an implicit definition), non-placement operator delete
13418      shall be looked up in the scope of the destructor's class and if
13419      found shall be accessible and unambiguous.
13420      
13421      This is somewhat unclear, but I take it to mean that if the class
13422      only defines placement deletes we don't do anything here.  So we
13423      pass LOOKUP_SPECULATIVELY; delete_sanity will complain for us if
13424      they ever try to delete one of these.  */
13425   if (TYPE_GETS_REG_DELETE (current_class_type)
13426       || TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
13427     {
13428       tree if_stmt;
13429
13430       exprstmt = build_op_delete_call
13431         (DELETE_EXPR, current_class_ptr, virtual_size,
13432          LOOKUP_NORMAL | LOOKUP_SPECULATIVELY, NULL_TREE);
13433
13434       if_stmt = begin_if_stmt ();
13435       finish_if_stmt_cond (build (BIT_AND_EXPR, integer_type_node,
13436                                   current_in_charge_parm,
13437                                   integer_one_node),
13438                            if_stmt);
13439       finish_expr_stmt (exprstmt);
13440       finish_then_clause (if_stmt);
13441       finish_if_stmt ();
13442     }
13443
13444   /* Close the block we started above.  */
13445   finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
13446 }
13447
13448 /* Finish up a function declaration and compile that function
13449    all the way to assembler language output.  The free the storage
13450    for the function definition.
13451
13452    This is called after parsing the body of the function definition.
13453    LINENO is the current line number.
13454
13455    FLAGS is a bitwise or of the following values: 
13456      1 - CALL_POPLEVEL 
13457        An extra call to poplevel (and expand_end_bindings) must be
13458        made to take care of the binding contour for the base
13459        initializers.  This is only relevant for constructors.
13460      2 - INCLASS_INLINE
13461        We just finished processing the body of an in-class inline
13462        function definition.  (This processing will have taken place
13463        after the class definition is complete.)  */
13464
13465 tree
13466 finish_function (lineno, flags)
13467      int lineno;
13468      int flags;
13469 {
13470   register tree fndecl = current_function_decl;
13471   tree fntype, ctype = NULL_TREE;
13472   /* Label to use if this function is supposed to return a value.  */
13473   tree no_return_label = NULL_TREE;
13474   int call_poplevel = (flags & 1) != 0;
13475   int inclass_inline = (flags & 2) != 0;
13476   int expand_p;
13477   int nested;
13478
13479   /* When we get some parse errors, we can end up without a
13480      current_function_decl, so cope.  */
13481   if (fndecl == NULL_TREE)
13482     return error_mark_node;
13483
13484   nested = function_depth > 1;
13485   fntype = TREE_TYPE (fndecl);
13486
13487   /*  TREE_READONLY (fndecl) = 1;
13488       This caused &foo to be of type ptr-to-const-function
13489       which then got a warning when stored in a ptr-to-function variable.  */
13490
13491   /* This happens on strange parse errors.  */
13492   if (! current_function_parms_stored)
13493     {
13494       call_poplevel = 0;
13495       store_parm_decls ();
13496     }
13497
13498   if (building_stmt_tree ())
13499     {
13500       if (DECL_CONSTRUCTOR_P (fndecl))
13501         {
13502           finish_constructor_body ();
13503           if (call_poplevel)
13504             do_poplevel ();
13505         }
13506       else if (DECL_DESTRUCTOR_P (fndecl) && !processing_template_decl)
13507         finish_destructor_body ();
13508       else if (DECL_MAIN_P (fndecl))
13509         {
13510           /* Make it so that `main' always returns 0 by default.  */
13511 #ifdef VMS
13512           finish_return_stmt (integer_one_node);
13513 #else
13514           finish_return_stmt (integer_zero_node);
13515 #endif
13516         }
13517
13518       /* Finish dealing with exception specifiers.  */
13519       if (flag_exceptions && !processing_template_decl
13520           && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl)))
13521         expand_end_eh_spec (TYPE_RAISES_EXCEPTIONS 
13522                             (TREE_TYPE (current_function_decl)),
13523                             current_eh_spec_try_block);
13524     }
13525   else
13526     {
13527 #if 0
13528       if (write_symbols != NO_DEBUG /*&& TREE_CODE (fntype) != METHOD_TYPE*/)
13529         {
13530           /* Keep this code around in case we later want to control debug info
13531              based on whether a type is "used".  (jason 1999-11-11) */
13532
13533           tree ttype = target_type (fntype);
13534           tree parmdecl;
13535
13536           if (IS_AGGR_TYPE (ttype))
13537             /* Let debugger know it should output info for this type.  */
13538             note_debug_info_needed (ttype);
13539
13540           for (parmdecl = DECL_ARGUMENTS (fndecl); parmdecl; parmdecl = TREE_CHAIN (parmdecl))
13541             {
13542               ttype = target_type (TREE_TYPE (parmdecl));
13543               if (IS_AGGR_TYPE (ttype))
13544                 /* Let debugger know it should output info for this type.  */
13545                 note_debug_info_needed (ttype);
13546             }
13547         }
13548 #endif
13549
13550       /* Clean house because we will need to reorder insns here.  */
13551       do_pending_stack_adjust ();
13552
13553       if (dtor_label)
13554         ;
13555       else if (DECL_CONSTRUCTOR_P (fndecl))
13556         {
13557           if (call_poplevel)
13558             do_poplevel ();
13559         }
13560       else if (return_label != NULL_RTX
13561                && flag_this_is_variable <= 0
13562                && current_function_return_value == NULL_TREE
13563                && ! DECL_NAME (DECL_RESULT (current_function_decl)))
13564         no_return_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
13565
13566       if (flag_exceptions)
13567         expand_exception_blocks ();
13568
13569       /* If this function is supposed to return a value, ensure that
13570          we do not fall into the cleanups by mistake.  The end of our
13571          function will look like this:
13572          
13573          user code (may have return stmt somewhere)
13574          goto no_return_label
13575          cleanup_label:
13576          cleanups
13577          goto return_label
13578          no_return_label:
13579          NOTE_INSN_FUNCTION_END
13580          return_label:
13581          things for return
13582          
13583          If the user omits a return stmt in the USER CODE section, we
13584          will have a control path which reaches NOTE_INSN_FUNCTION_END.
13585          Otherwise, we won't.  */
13586       if (no_return_label)
13587         {
13588           DECL_CONTEXT (no_return_label) = fndecl;
13589           DECL_INITIAL (no_return_label) = error_mark_node;
13590           DECL_SOURCE_FILE (no_return_label) = input_filename;
13591           DECL_SOURCE_LINE (no_return_label) = lineno;
13592           expand_goto (no_return_label);
13593         }
13594
13595       if (cleanup_label)
13596         {
13597           /* Remove the binding contour which is used
13598              to catch cleanup-generated temporaries.  */
13599           expand_end_bindings (0, 0, 0);
13600           poplevel (0, 0, 0);
13601
13602           /* Emit label at beginning of cleanup code for parameters.  */
13603           emit_label (cleanup_label);
13604         }
13605
13606       /* Get return value into register if that's where it's supposed
13607          to be.  */
13608       if (original_result_rtx)
13609         fixup_result_decl (DECL_RESULT (fndecl), original_result_rtx);
13610
13611       /* Finish building code that will trigger warnings if users forget
13612          to make their functions return values.  */
13613       if (no_return_label || cleanup_label)
13614         emit_jump (return_label);
13615       if (no_return_label)
13616         {
13617           /* We don't need to call `expand_*_return' here because we
13618              don't need any cleanups here--this path of code is only
13619              for error checking purposes.  */
13620           expand_label (no_return_label);
13621         }
13622
13623       /* We hard-wired immediate_size_expand to zero in
13624          start_function.  Expand_function_end will decrement this
13625          variable.  So, we set the variable to one here, so that after
13626          the decrement it will remain zero.  */
13627       immediate_size_expand = 1;
13628
13629       /* Generate rtl for function exit.  */
13630       expand_function_end (input_filename, lineno, 1);
13631     }
13632
13633   /* We have to save this value here in case
13634      maybe_end_member_template_processing decides to pop all the
13635      template parameters.  */
13636   expand_p = !building_stmt_tree ();
13637   
13638   /* If we're saving up tree structure, tie off the function now.  */
13639   if (!expand_p)
13640     finish_stmt_tree (&DECL_SAVED_TREE (fndecl));
13641
13642   /* This must come after expand_function_end because cleanups might
13643      have declarations (from inline functions) that need to go into
13644      this function's blocks.  */
13645   if (doing_semantic_analysis_p ())
13646     {
13647       if (current_binding_level->parm_flag != 1)
13648         my_friendly_abort (122);
13649       poplevel (1, 0, 1);
13650     }
13651
13652   /* Remember that we were in class scope.  */
13653   if (current_class_name)
13654     ctype = current_class_type;
13655
13656   /* Must mark the RESULT_DECL as being in this function.  */
13657   DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
13658
13659   /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
13660      to the FUNCTION_DECL node itself.  */
13661   BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
13662
13663   /* Save away current state, if appropriate.  */
13664   if (!expanding_p && !processing_template_decl)
13665     save_function_data (fndecl);
13666
13667   /* If this function calls `setjmp' it cannot be inlined.  When
13668      `longjmp' is called it is not guaranteed to restore the value of
13669      local variables that have been modified since the call to
13670      `setjmp'.  So, if were to inline this function into some caller
13671      `c', then when we `longjmp', we might not restore all variables
13672      in `c'.  (It might seem, at first blush, that there's no way for
13673      this function to modify local variables in `c', but their
13674      addresses may have been stored somewhere accessible to this
13675      function.)  */
13676   if (!expanding_p && !processing_template_decl && calls_setjmp_p (fndecl))
13677     DECL_UNINLINABLE (fndecl) = 1;
13678
13679   if (expand_p)
13680     {
13681       int returns_null;
13682       int returns_value;
13683       int saved_flag_keep_inline_functions =
13684         flag_keep_inline_functions;
13685
13686       /* So we can tell if jump_optimize sets it to 1.  */
13687       can_reach_end = 0;
13688
13689       if (DECL_CONTEXT (fndecl) != NULL_TREE
13690           && hack_decl_function_context (fndecl))
13691         /* Trick rest_of_compilation into not deferring output of this
13692            function, even if it is inline, since the rtl_obstack for
13693            this function is the function_obstack of the enclosing
13694            function and will be deallocated when the enclosing
13695            function is gone.  See save_tree_status.  */
13696         flag_keep_inline_functions = 1;
13697
13698       /* Before we call rest_of_compilation (which will pop the
13699          CURRENT_FUNCTION), we must save these values.  */
13700       returns_null = current_function_returns_null;
13701       returns_value = current_function_returns_value;
13702
13703       /* If this is a nested function (like a template instantiation
13704          that we're compiling in the midst of compiling something
13705          else), push a new GC context.  That will keep local variables
13706          on the stack from being collected while we're doing the
13707          compilation of this function.  */
13708       if (function_depth > 1)
13709         ggc_push_context ();
13710
13711       /* Run the optimizers and output the assembler code for this
13712          function.  */
13713       if (DECL_ARTIFICIAL (fndecl))
13714         {
13715           /* Do we really *want* to inline this synthesized method?  */
13716
13717           int save_fif = flag_inline_functions;
13718           flag_inline_functions = 1;
13719
13720           /* Turn off DECL_INLINE for the moment so function_cannot_inline_p
13721              will check our size.  */
13722           DECL_INLINE (fndecl) = 0;
13723
13724           rest_of_compilation (fndecl);
13725           flag_inline_functions = save_fif;
13726         }
13727       else
13728         rest_of_compilation (fndecl);
13729
13730       /* Undo the call to ggc_push_context above.  */
13731       if (function_depth > 1)
13732         ggc_pop_context ();
13733
13734       flag_keep_inline_functions = saved_flag_keep_inline_functions;
13735
13736       if (DECL_SAVED_INSNS (fndecl) && ! TREE_ASM_WRITTEN (fndecl))
13737         {
13738           /* Set DECL_EXTERNAL so that assemble_external will be called as
13739              necessary.  We'll clear it again in finish_file.  */
13740           if (! DECL_EXTERNAL (fndecl))
13741             DECL_NOT_REALLY_EXTERN (fndecl) = 1;
13742           DECL_EXTERNAL (fndecl) = 1;
13743           mark_inline_for_output (fndecl);
13744         }
13745
13746 #if 0
13747       /* Keep this code around in case we later want to control debug info
13748          based on whether a type is "used".  (jason 1999-11-11) */
13749
13750       if (ctype && TREE_ASM_WRITTEN (fndecl))
13751         note_debug_info_needed (ctype);
13752 #endif
13753
13754       returns_null |= can_reach_end;
13755
13756       /* Since we don't normally go through c_expand_return for constructors,
13757          this normally gets the wrong value.
13758          Also, named return values have their return codes emitted after
13759          NOTE_INSN_FUNCTION_END, confusing jump.c.  */
13760       if (DECL_CONSTRUCTOR_P (fndecl)
13761           || DECL_NAME (DECL_RESULT (fndecl)) != NULL_TREE)
13762         returns_null = 0;
13763
13764       if (TREE_THIS_VOLATILE (fndecl) && returns_null)
13765         cp_warning ("`noreturn' function `%D' does return", fndecl);
13766       else if ((warn_return_type || pedantic)
13767                && returns_null
13768                && TREE_CODE (TREE_TYPE (fntype)) != VOID_TYPE)
13769         {
13770           /* If this function returns non-void and control can drop through,
13771              complain.  */
13772           cp_warning ("control reaches end of non-void function `%D'", fndecl);
13773         }
13774       /* With just -W, complain only if function returns both with
13775          and without a value.  */
13776       else if (extra_warnings && returns_value && returns_null)
13777         warning ("this function may return with or without a value");
13778     }
13779   else
13780     {
13781       /* Clear out memory we no longer need.  */
13782       free_after_parsing (current_function);
13783       /* Since we never call rest_of_compilation, we never clear
13784          CURRENT_FUNCTION.  Do so explicitly.  */
13785       free_after_compilation (current_function);
13786       current_function = NULL;
13787     }
13788
13789   /* If this is a in-class inline definition, we may have to pop the
13790      bindings for the template parameters that we added in
13791      maybe_begin_member_template_processing when start_function was
13792      called.  */
13793   if (inclass_inline)
13794     maybe_end_member_template_processing ();
13795
13796   /* Leave the scope of the class.  */
13797   if (ctype)
13798     pop_nested_class ();
13799
13800   --function_depth;
13801
13802   if (!DECL_SAVED_INSNS (fndecl) && !DECL_SAVED_FUNCTION_DATA (fndecl)
13803       && !(flag_inline_trees && DECL_INLINE (fndecl)))
13804     {
13805       tree t;
13806
13807       /* Stop pointing to the local nodes about to be freed.  */
13808       /* But DECL_INITIAL must remain nonzero so we know this
13809          was an actual function definition.  */
13810       DECL_INITIAL (fndecl) = error_mark_node;
13811       for (t = DECL_ARGUMENTS (fndecl); t; t = TREE_CHAIN (t))
13812         DECL_RTL (t) = DECL_INCOMING_RTL (t) = NULL_RTX;
13813     }
13814
13815   if (DECL_STATIC_CONSTRUCTOR (fndecl))
13816     static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
13817   if (DECL_STATIC_DESTRUCTOR (fndecl))
13818     static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
13819
13820   /* Clean up.  */
13821   if (! nested)
13822     {
13823       /* Let the error reporting routines know that we're outside a
13824          function.  For a nested function, this value is used in
13825          pop_cp_function_context and then reset via pop_function_context.  */
13826       current_function_decl = NULL_TREE;
13827       /* We don't really care about obstacks, but the middle-end
13828          sometimes cares on what obstck things are located.  */
13829       permanent_allocation (1);
13830     }
13831
13832   return fndecl;
13833 }
13834 \f
13835 /* Create the FUNCTION_DECL for a function definition.
13836    DECLSPECS and DECLARATOR are the parts of the declaration;
13837    they describe the return type and the name of the function,
13838    but twisted together in a fashion that parallels the syntax of C.
13839
13840    This function creates a binding context for the function body
13841    as well as setting up the FUNCTION_DECL in current_function_decl.
13842
13843    Returns a FUNCTION_DECL on success.
13844
13845    If the DECLARATOR is not suitable for a function (it defines a datum
13846    instead), we return 0, which tells yyparse to report a parse error.
13847
13848    May return void_type_node indicating that this method is actually
13849    a friend.  See grokfield for more details.
13850
13851    Came here with a `.pushlevel' .
13852
13853    DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
13854    CHANGES TO CODE IN `grokfield'.  */
13855
13856 tree
13857 start_method (declspecs, declarator, attrlist)
13858      tree declarator, declspecs, attrlist;
13859 {
13860   tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
13861                                 attrlist);
13862
13863   /* Something too ugly to handle.  */
13864   if (fndecl == NULL_TREE)
13865     return NULL_TREE;
13866
13867   /* Pass friends other than inline friend functions back.  */
13868   if (fndecl == void_type_node)
13869     return fndecl;
13870
13871   if (TREE_CODE (fndecl) != FUNCTION_DECL)
13872     /* Not a function, tell parser to report parse error.  */
13873     return NULL_TREE;
13874
13875   if (DECL_IN_AGGR_P (fndecl))
13876     {
13877       if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (fndecl)) != current_class_type)
13878         {
13879           if (DECL_CONTEXT (fndecl) 
13880               && TREE_CODE( DECL_CONTEXT (fndecl)) != NAMESPACE_DECL)
13881             cp_error ("`%D' is already defined in class %s", fndecl,
13882                              TYPE_NAME_STRING (DECL_CONTEXT (fndecl)));
13883         }
13884       return void_type_node;
13885     }
13886
13887   check_template_shadow (fndecl);
13888
13889   DECL_THIS_INLINE (fndecl) = 1;
13890
13891   if (flag_default_inline)
13892     DECL_INLINE (fndecl) = 1;
13893
13894   /* We process method specializations in finish_struct_1.  */
13895   if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
13896     fndecl = push_template_decl (fndecl);
13897
13898   /* We read in the parameters on the maybepermanent_obstack,
13899      but we won't be getting back to them until after we
13900      may have clobbered them.  So the call to preserve_data
13901      will keep them safe.  */
13902   preserve_data ();
13903
13904   if (! DECL_FRIEND_P (fndecl))
13905     {
13906       if (TREE_CHAIN (fndecl))
13907         {
13908           fndecl = copy_node (fndecl);
13909           TREE_CHAIN (fndecl) = NULL_TREE;
13910         }
13911
13912       if (DECL_CONSTRUCTOR_P (fndecl))
13913         {
13914           if (! grok_ctor_properties (current_class_type, fndecl))
13915             return void_type_node;
13916         }
13917       else if (IDENTIFIER_OPNAME_P (DECL_NAME (fndecl)))
13918         grok_op_properties (fndecl, DECL_VIRTUAL_P (fndecl), 0);
13919     }
13920
13921   cp_finish_decl (fndecl, NULL_TREE, NULL_TREE, 0);
13922
13923   /* Make a place for the parms */
13924   pushlevel (0);
13925   current_binding_level->parm_flag = 1;
13926   
13927   DECL_IN_AGGR_P (fndecl) = 1;
13928   return fndecl;
13929 }
13930
13931 /* Go through the motions of finishing a function definition.
13932    We don't compile this method until after the whole class has
13933    been processed.
13934
13935    FINISH_METHOD must return something that looks as though it
13936    came from GROKFIELD (since we are defining a method, after all).
13937
13938    This is called after parsing the body of the function definition.
13939    STMTS is the chain of statements that makes up the function body.
13940
13941    DECL is the ..._DECL that `start_method' provided.  */
13942
13943 tree
13944 finish_method (decl)
13945      tree decl;
13946 {
13947   register tree fndecl = decl;
13948   tree old_initial;
13949
13950   register tree link;
13951
13952   if (decl == void_type_node)
13953     return decl;
13954
13955   old_initial = DECL_INITIAL (fndecl);
13956
13957   /* Undo the level for the parms (from start_method).
13958      This is like poplevel, but it causes nothing to be
13959      saved.  Saving information here confuses symbol-table
13960      output routines.  Besides, this information will
13961      be correctly output when this method is actually
13962      compiled.  */
13963
13964   /* Clear out the meanings of the local variables of this level;
13965      also record in each decl which block it belongs to.  */
13966
13967   for (link = current_binding_level->names; link; link = TREE_CHAIN (link))
13968     {
13969       if (DECL_NAME (link) != NULL_TREE)
13970         pop_binding (DECL_NAME (link), link);
13971       my_friendly_assert (TREE_CODE (link) != FUNCTION_DECL, 163);
13972       DECL_CONTEXT (link) = NULL_TREE;
13973     }
13974
13975   GNU_xref_end_scope ((HOST_WIDE_INT) current_binding_level,
13976                       (HOST_WIDE_INT) current_binding_level->level_chain,
13977                       current_binding_level->parm_flag,
13978                       current_binding_level->keep);
13979
13980   poplevel (0, 0, 0);
13981
13982   DECL_INITIAL (fndecl) = old_initial;
13983
13984   /* We used to check if the context of FNDECL was different from
13985      current_class_type as another way to get inside here.  This didn't work
13986      for String.cc in libg++.  */
13987   if (DECL_FRIEND_P (fndecl))
13988     {
13989       CLASSTYPE_INLINE_FRIENDS (current_class_type)
13990         = tree_cons (NULL_TREE, fndecl, CLASSTYPE_INLINE_FRIENDS (current_class_type));
13991       decl = void_type_node;
13992     }
13993
13994   return decl;
13995 }
13996 \f
13997 /* Called when a new struct TYPE is defined.
13998    If this structure or union completes the type of any previous
13999    variable declaration, lay it out and output its rtl.  */
14000
14001 void
14002 hack_incomplete_structures (type)
14003      tree type;
14004 {
14005   tree *list;
14006
14007   if (current_binding_level->incomplete == NULL_TREE)
14008     return;
14009
14010   if (!type) /* Don't do this for class templates.  */
14011     return;
14012
14013   for (list = &current_binding_level->incomplete; *list; )
14014     {
14015       tree decl = TREE_VALUE (*list);
14016       if ((decl && TREE_TYPE (decl) == type)
14017           || (TREE_TYPE (decl)
14018               && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
14019               && TREE_TYPE (TREE_TYPE (decl)) == type))
14020         {
14021           int toplevel = toplevel_bindings_p ();
14022           if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
14023               && TREE_TYPE (TREE_TYPE (decl)) == type)
14024             layout_type (TREE_TYPE (decl));
14025           layout_decl (decl, 0);
14026           rest_of_decl_compilation (decl, NULL_PTR, toplevel, 0);
14027           if (! toplevel)
14028             {
14029               tree cleanup;
14030               expand_decl (decl);
14031               cleanup = maybe_build_cleanup (decl);
14032               expand_decl_init (decl);
14033               if (! expand_decl_cleanup (decl, cleanup))
14034                 cp_error ("parser lost in parsing declaration of `%D'",
14035                           decl);
14036             }
14037           *list = TREE_CHAIN (*list);
14038         }
14039       else
14040         list = &TREE_CHAIN (*list);
14041     }
14042 }
14043
14044 /* If DECL is of a type which needs a cleanup, build that cleanup here.
14045    See build_delete for information about AUTO_DELETE.
14046
14047    Don't build these on the momentary obstack; they must live
14048    the life of the binding contour.  */
14049
14050 static tree
14051 maybe_build_cleanup_1 (decl, auto_delete)
14052      tree decl, auto_delete;
14053 {
14054   tree type = TREE_TYPE (decl);
14055   if (type != error_mark_node && TYPE_NEEDS_DESTRUCTOR (type))
14056     {
14057       int flags = LOOKUP_NORMAL|LOOKUP_DESTRUCTOR;
14058       tree rval;
14059
14060       if (TREE_CODE (type) == ARRAY_TYPE)
14061         rval = decl;
14062       else
14063         {
14064           mark_addressable (decl);
14065           rval = build_unary_op (ADDR_EXPR, decl, 0);
14066         }
14067
14068       /* Optimize for space over speed here.  */
14069       if (! TYPE_USES_VIRTUAL_BASECLASSES (type)
14070           || flag_expensive_optimizations)
14071         flags |= LOOKUP_NONVIRTUAL;
14072
14073       rval = build_delete (TREE_TYPE (rval), rval, auto_delete, flags, 0);
14074
14075       if (TYPE_USES_VIRTUAL_BASECLASSES (type)
14076           && ! TYPE_HAS_DESTRUCTOR (type))
14077         rval = build_compound_expr (tree_cons (NULL_TREE, rval,
14078                                                build_expr_list (NULL_TREE, build_vbase_delete (type, decl))));
14079
14080       return rval;
14081     }
14082   return 0;
14083 }
14084
14085 /* If DECL is of a type which needs a cleanup, build that cleanup
14086    here.  The cleanup does free the storage with a call to delete.  */
14087
14088 tree
14089 maybe_build_cleanup_and_delete (decl)
14090      tree decl;
14091 {
14092   return maybe_build_cleanup_1 (decl, integer_three_node);
14093 }
14094
14095 /* If DECL is of a type which needs a cleanup, build that cleanup
14096    here.  The cleanup does not free the storage with a call a delete.  */
14097
14098 tree
14099 maybe_build_cleanup (decl)
14100      tree decl;
14101 {
14102   return maybe_build_cleanup_1 (decl, integer_two_node);
14103 }
14104 \f
14105 /* Expand a C++ expression at the statement level.
14106    This is needed to ferret out nodes which have UNKNOWN_TYPE.
14107    The C++ type checker should get all of these out when
14108    expressions are combined with other, type-providing, expressions,
14109    leaving only orphan expressions, such as:
14110
14111    &class::bar;         / / takes its address, but does nothing with it.  */
14112
14113 void
14114 cplus_expand_expr_stmt (exp)
14115      tree exp;
14116 {
14117   if (stmts_are_full_exprs_p)
14118     exp = convert_to_void (exp, "statement");
14119   
14120 #if 0
14121   /* We should do this eventually, but right now this causes regex.o from
14122      libg++ to miscompile, and tString to core dump.  */
14123   exp = build1 (CLEANUP_POINT_EXPR, TREE_TYPE (exp), exp);
14124 #endif
14125
14126   /* If we don't do this, we end up down inside expand_expr
14127      trying to do TYPE_MODE on the ERROR_MARK, and really
14128      go outside the bounds of the type.  */
14129   if (exp != error_mark_node)
14130     expand_expr_stmt (exp);
14131 }
14132
14133 /* When a stmt has been parsed, this function is called.  */
14134
14135 void
14136 finish_stmt ()
14137 {
14138   /* Always assume this statement was not an expression statement.  If
14139      it actually was an expression statement, its our callers
14140      responsibility to fix this up.  */
14141   last_expr_type = NULL_TREE;
14142 }
14143
14144 /* Change a static member function definition into a FUNCTION_TYPE, instead
14145    of the METHOD_TYPE that we create when it's originally parsed.
14146
14147    WARNING: DO NOT pass &TREE_TYPE (decl) to FN or &TYPE_ARG_TYPES
14148    (TREE_TYPE (decl)) to ARGTYPES, as doing so will corrupt the types of
14149    other decls.  Either pass the addresses of local variables or NULL.  */
14150
14151 void
14152 revert_static_member_fn (decl, fn, argtypes)
14153      tree *decl, *fn, *argtypes;
14154 {
14155   tree tmp;
14156   tree function = fn ? *fn : TREE_TYPE (*decl);
14157   tree args = argtypes ? *argtypes : TYPE_ARG_TYPES (function);
14158
14159   if (CP_TYPE_QUALS (TREE_TYPE (TREE_VALUE (args))) 
14160       != TYPE_UNQUALIFIED)
14161     cp_error ("static member function `%#D' declared with type qualifiers", 
14162               *decl);
14163
14164   args = TREE_CHAIN (args);
14165   tmp = build_function_type (TREE_TYPE (function), args);
14166   tmp = build_qualified_type (tmp, CP_TYPE_QUALS (function));
14167   tmp = build_exception_variant (tmp,
14168                                  TYPE_RAISES_EXCEPTIONS (function));
14169   TREE_TYPE (*decl) = tmp;
14170   if (DECL_ARGUMENTS (*decl))
14171     DECL_ARGUMENTS (*decl) = TREE_CHAIN (DECL_ARGUMENTS (*decl));
14172   DECL_STATIC_FUNCTION_P (*decl) = 1;
14173   if (fn)
14174     *fn = tmp;
14175   if (argtypes)
14176     *argtypes = args;
14177 }
14178
14179 /* Initialize the variables used during compilation of a C++ 
14180    function.  */ 
14181
14182 static void
14183 push_cp_function_context (f)
14184      struct function *f;
14185 {
14186   struct language_function *p 
14187     = ((struct language_function *) 
14188        xcalloc (1, sizeof (struct language_function)));
14189   f->language = p;
14190
14191   /* It takes an explicit call to expand_body to generate RTL for a
14192      function.  */
14193   expanding_p = 0;
14194
14195   /* Whenever we start a new function, we destroy temporaries in the
14196      usual way.  */
14197   stmts_are_full_exprs_p = 1;
14198 }
14199
14200 /* Free the language-specific parts of F, now that we've finished
14201    compiling the function.  */
14202
14203 static void
14204 pop_cp_function_context (f)
14205      struct function *f;
14206 {
14207   if (f->language)
14208     free (f->language);
14209   f->language = 0;
14210 }
14211
14212 /* Mark P for GC.  */
14213
14214 static void
14215 mark_lang_function (p)
14216      struct language_function *p;
14217 {
14218   if (!p)
14219     return;
14220
14221   ggc_mark_tree (p->x_named_labels);
14222   ggc_mark_tree (p->x_ctor_label);
14223   ggc_mark_tree (p->x_dtor_label);
14224   ggc_mark_tree (p->x_base_init_list);
14225   ggc_mark_tree (p->x_member_init_list);
14226   ggc_mark_tree (p->x_current_class_ptr);
14227   ggc_mark_tree (p->x_current_class_ref);
14228   ggc_mark_tree (p->x_eh_spec_try_block);
14229   ggc_mark_tree (p->x_scope_stmt_stack);
14230
14231   ggc_mark_rtx (p->x_result_rtx);
14232
14233   mark_stmt_tree (&p->x_stmt_tree);
14234   mark_binding_level (&p->bindings);
14235 }
14236
14237 /* Mark the language-specific data in F for GC.  */
14238
14239 static void
14240 mark_cp_function_context (f)
14241      struct function *f;
14242 {
14243   mark_lang_function (f->language);
14244 }
14245
14246 int
14247 in_function_p ()
14248 {
14249   return function_depth != 0;
14250 }
14251
14252
14253 void
14254 lang_mark_false_label_stack (l)
14255      struct label_node *l;
14256 {
14257   /* C++ doesn't use false_label_stack.  It better be NULL.  */
14258   my_friendly_assert (l == NULL, 19990904);
14259 }
14260
14261 void
14262 lang_mark_tree (t)
14263      tree t;
14264 {
14265   enum tree_code code = TREE_CODE (t);
14266   if (code == IDENTIFIER_NODE)
14267     {
14268       struct lang_identifier *li = (struct lang_identifier *) t;
14269       struct lang_id2 *li2 = li->x;
14270       ggc_mark_tree (li->namespace_bindings);
14271       ggc_mark_tree (li->bindings);
14272       ggc_mark_tree (li->class_value);
14273       ggc_mark_tree (li->class_template_info);
14274
14275       if (li2)
14276         {
14277           ggc_mark_tree (li2->label_value);
14278           ggc_mark_tree (li2->implicit_decl);
14279           ggc_mark_tree (li2->error_locus);
14280         }
14281     }
14282   else if (code == CPLUS_BINDING)
14283     {
14284       if (BINDING_HAS_LEVEL_P (t))
14285         mark_binding_level (&BINDING_LEVEL (t));
14286       else
14287         ggc_mark_tree (BINDING_SCOPE (t));
14288       ggc_mark_tree (BINDING_VALUE (t));
14289     }
14290   else if (code == OVERLOAD)
14291     ggc_mark_tree (OVL_FUNCTION (t));
14292   else if (code == TEMPLATE_PARM_INDEX)
14293     ggc_mark_tree (TEMPLATE_PARM_DECL (t));
14294   else if (TREE_CODE_CLASS (code) == 'd')
14295     {
14296       struct lang_decl *ld = DECL_LANG_SPECIFIC (t);
14297
14298       if (ld)
14299         {
14300           ggc_mark (ld);
14301           if (!DECL_GLOBAL_CTOR_P (t) && !DECL_GLOBAL_DTOR_P (t))
14302             ggc_mark_tree (ld->decl_flags.u2.access);
14303           ggc_mark_tree (ld->decl_flags.context);
14304           if (TREE_CODE (t) != NAMESPACE_DECL)
14305             ggc_mark_tree (ld->decl_flags.u.template_info);
14306           else
14307             mark_binding_level (&NAMESPACE_LEVEL (t));
14308           if (CAN_HAVE_FULL_LANG_DECL_P (t))
14309             {
14310               ggc_mark_tree (ld->main_decl_variant);
14311               ggc_mark_tree (ld->befriending_classes);
14312               ggc_mark_tree (ld->saved_tree);
14313               if (TREE_CODE (t) == TYPE_DECL)
14314                 ggc_mark_tree (ld->u.sorted_fields);
14315               else if (TREE_CODE (t) == FUNCTION_DECL
14316                        && !DECL_PENDING_INLINE_P (t))
14317                 mark_lang_function (DECL_SAVED_FUNCTION_DATA (t));
14318             }
14319         }
14320     }
14321   else if (TREE_CODE_CLASS (code) == 't')
14322     {
14323       struct lang_type *lt = TYPE_LANG_SPECIFIC (t);
14324
14325       if (lt && !(TREE_CODE (t) == POINTER_TYPE 
14326                   && TREE_CODE (TREE_TYPE (t)) == METHOD_TYPE))
14327         {
14328           ggc_mark (lt);
14329           ggc_mark_tree (lt->vfields);
14330           ggc_mark_tree (lt->vbases);
14331           ggc_mark_tree (lt->tags);
14332           ggc_mark_tree (lt->search_slot);
14333           ggc_mark_tree (lt->size);
14334           ggc_mark_tree (lt->abstract_virtuals);
14335           ggc_mark_tree (lt->friend_classes);
14336           ggc_mark_tree (lt->rtti);
14337           ggc_mark_tree (lt->methods);
14338           ggc_mark_tree (lt->template_info);
14339           ggc_mark_tree (lt->befriending_classes);
14340         }
14341       else if (lt)
14342         /* In the case of pointer-to-member function types, the
14343            TYPE_LANG_SPECIFIC is really just a tree.  */
14344         ggc_mark_tree ((tree) lt);
14345     }
14346 }
14347