OSDN Git Service

31th Cygnus<->FSF merge.
[pf3gnuchains/gcc-fork.git] / gcc / cp / decl.c
1 /* Process declarations and variables for C compiler.
2    Copyright (C) 1988, 1992, 1993, 1994 Free Software Foundation, Inc.
3    Hacked 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, 675 Mass Ave, Cambridge, MA 02139, USA.  */
20
21
22 /* Process declarations and symbol lookup for C front end.
23    Also constructs types; the standard scalar types at initialization,
24    and structure, union, array and enum types when they are declared.  */
25
26 /* ??? not all decl nodes are given the most useful possible
27    line numbers.  For example, the CONST_DECLs for enum values.  */
28
29 #include <stdio.h>
30 #include "config.h"
31 #include "tree.h"
32 #include "rtl.h"
33 #include "flags.h"
34 #include "cp-tree.h"
35 #include "decl.h"
36 #include "lex.h"
37 #include <sys/types.h>
38 #include <signal.h>
39 #include "obstack.h"
40
41 #define obstack_chunk_alloc xmalloc
42 #define obstack_chunk_free free
43
44 extern struct obstack permanent_obstack;
45
46 extern int current_class_depth;
47
48 /* Stack of places to restore the search obstack back to.  */
49    
50 /* Obstack used for remembering local class declarations (like
51    enums and static (const) members.  */
52 #include "stack.h"
53 static struct obstack decl_obstack;
54 static struct stack_level *decl_stack;
55
56 #ifndef CHAR_TYPE_SIZE
57 #define CHAR_TYPE_SIZE BITS_PER_UNIT
58 #endif
59
60 #ifndef SHORT_TYPE_SIZE
61 #define SHORT_TYPE_SIZE (BITS_PER_UNIT * MIN ((UNITS_PER_WORD + 1) / 2, 2))
62 #endif
63
64 #ifndef INT_TYPE_SIZE
65 #define INT_TYPE_SIZE BITS_PER_WORD
66 #endif
67
68 #ifndef LONG_TYPE_SIZE
69 #define LONG_TYPE_SIZE BITS_PER_WORD
70 #endif
71
72 #ifndef LONG_LONG_TYPE_SIZE
73 #define LONG_LONG_TYPE_SIZE (BITS_PER_WORD * 2)
74 #endif
75
76 #ifndef WCHAR_UNSIGNED
77 #define WCHAR_UNSIGNED 0
78 #endif
79
80 #ifndef FLOAT_TYPE_SIZE
81 #define FLOAT_TYPE_SIZE BITS_PER_WORD
82 #endif
83
84 #ifndef DOUBLE_TYPE_SIZE
85 #define DOUBLE_TYPE_SIZE (BITS_PER_WORD * 2)
86 #endif
87
88 #ifndef LONG_DOUBLE_TYPE_SIZE
89 #define LONG_DOUBLE_TYPE_SIZE (BITS_PER_WORD * 2)
90 #endif
91
92 /* We let tm.h override the types used here, to handle trivial differences
93    such as the choice of unsigned int or long unsigned int for size_t.
94    When machines start needing nontrivial differences in the size type,
95    it would be best to do something here to figure out automatically
96    from other information what type to use.  */
97
98 #ifndef SIZE_TYPE
99 #define SIZE_TYPE "long unsigned int"
100 #endif
101
102 #ifndef PTRDIFF_TYPE
103 #define PTRDIFF_TYPE "long int"
104 #endif
105
106 #ifndef WCHAR_TYPE
107 #define WCHAR_TYPE "int"
108 #endif
109
110 #define builtin_function(NAME, TYPE, CODE, LIBNAME) \
111   define_function (NAME, TYPE, CODE, (void (*)())pushdecl, LIBNAME)
112 #define auto_function(NAME, TYPE, CODE) \
113   do {                                  \
114     tree __name = NAME;         \
115     tree __type = TYPE;                 \
116     define_function (IDENTIFIER_POINTER (__name), __type, CODE, \
117                      (void (*)())push_overloaded_decl_1,        \
118                      IDENTIFIER_POINTER (build_decl_overload (__name, TYPE_ARG_TYPES (__type), 0)));\
119   } while (0)
120
121 static tree grokparms                           PROTO((tree, int));
122 static tree lookup_nested_type                  PROTO((tree, tree));
123 static char *redeclaration_error_message        PROTO((tree, tree));
124 static void grok_op_properties                  PROTO((tree, int, int));
125 static void deactivate_exception_cleanups       PROTO((void));
126
127 tree define_function            
128         PROTO((char *, tree, enum built_in_function, void (*)(), char *));
129
130 /* a node which has tree code ERROR_MARK, and whose type is itself.
131    All erroneous expressions are replaced with this node.  All functions
132    that accept nodes as arguments should avoid generating error messages
133    if this node is one of the arguments, since it is undesirable to get
134    multiple error messages from one error in the input.  */
135
136 tree error_mark_node;
137
138 /* Erroneous argument lists can use this *IFF* they do not modify it.  */
139 tree error_mark_list;
140
141 /* INTEGER_TYPE and REAL_TYPE nodes for the standard data types */
142
143 tree short_integer_type_node;
144 tree integer_type_node;
145 tree long_integer_type_node;
146 tree long_long_integer_type_node;
147
148 tree short_unsigned_type_node;
149 tree unsigned_type_node;
150 tree long_unsigned_type_node;
151 tree long_long_unsigned_type_node;
152
153 tree ptrdiff_type_node;
154
155 tree unsigned_char_type_node;
156 tree signed_char_type_node;
157 tree char_type_node;
158 tree wchar_type_node;
159 tree signed_wchar_type_node;
160 tree unsigned_wchar_type_node;
161
162 tree float_type_node;
163 tree double_type_node;
164 tree long_double_type_node;
165
166 tree intQI_type_node;
167 tree intHI_type_node;
168 tree intSI_type_node;
169 tree intDI_type_node;
170
171 tree unsigned_intQI_type_node;
172 tree unsigned_intHI_type_node;
173 tree unsigned_intSI_type_node;
174 tree unsigned_intDI_type_node;
175
176 /* a VOID_TYPE node, and the same, packaged in a TREE_LIST.  */
177
178 tree void_type_node, void_list_node;
179 tree void_zero_node;
180
181 /* Nodes for types `void *' and `const void *'.  */
182
183 tree ptr_type_node, const_ptr_type_node;
184
185 /* Nodes for types `char *' and `const char *'.  */
186
187 tree string_type_node, const_string_type_node;
188
189 /* Type `char[256]' or something like it.
190    Used when an array of char is needed and the size is irrelevant.  */
191
192 tree char_array_type_node;
193
194 /* Type `int[256]' or something like it.
195    Used when an array of int needed and the size is irrelevant.  */
196
197 tree int_array_type_node;
198
199 /* Type `wchar_t[256]' or something like it.
200    Used when a wide string literal is created.  */
201
202 tree wchar_array_type_node;
203
204 /* type `int ()' -- used for implicit declaration of functions.  */
205
206 tree default_function_type;
207
208 /* function types `double (double)' and `double (double, double)', etc.  */
209
210 tree double_ftype_double, double_ftype_double_double;
211 tree int_ftype_int, long_ftype_long;
212
213 /* Function type `void (void *, void *, int)' and similar ones.  */
214
215 tree void_ftype_ptr_ptr_int, int_ftype_ptr_ptr_int, void_ftype_ptr_int_int;
216
217 /* Function type `char *(char *, char *)' and similar ones */
218 tree string_ftype_ptr_ptr, int_ftype_string_string;
219
220 /* Function type `size_t (const char *)' */
221 tree sizet_ftype_string;
222
223 /* Function type `int (const void *, const void *, size_t)' */
224 tree int_ftype_cptr_cptr_sizet;
225
226 /* C++ extensions */
227 tree memptr_type, vtable_entry_type;
228 tree delta_type_node;
229 tree __t_desc_type_node, __i_desc_type_node, __m_desc_type_node;
230 tree __t_desc_array_type, __i_desc_array_type, __m_desc_array_type;
231 tree class_star_type_node;
232 tree class_type_node, record_type_node, union_type_node, enum_type_node;
233 tree exception_type_node, unknown_type_node;
234 tree opaque_type_node, signature_type_node;
235 tree sigtable_entry_type;
236 tree maybe_gc_cleanup;
237
238 /* Used for virtual function tables.  */
239 tree vtbl_mask;
240
241 /* Array type `vtable_entry_type[]' */
242 tree vtbl_type_node;
243
244 /* Static decls which do not have static initializers have no
245    initializers as far as GNU C is concerned.  EMPTY_INIT_NODE
246    is a static initializer which makes varasm code place the decl
247    in data rather than in bss space.  Such gymnastics are necessary
248    to avoid the problem that the linker will not include a library
249    file if all the library appears to contribute are bss variables.  */
250
251 tree empty_init_node;
252
253 /* In a destructor, the point at which all derived class destroying
254    has been done, just before any base class destroying will be done.  */
255
256 tree dtor_label;
257
258 /* In a constructor, the point at which we are ready to return
259    the pointer to the initialized object.  */
260
261 tree ctor_label;
262
263 /* A FUNCTION_DECL which can call `unhandled_exception'.
264    Not necessarily the one that the user will declare,
265    but sufficient to be called by routines that want to abort the program.  */
266
267 tree unhandled_exception_fndecl;
268
269 /* A FUNCTION_DECL which can call `abort'.  Not necessarily the
270    one that the user will declare, but sufficient to be called
271    by routines that want to abort the program.  */
272
273 tree abort_fndecl;
274
275 extern rtx cleanup_label, return_label;
276
277 /* If original DECL_RESULT of current function was a register,
278    but due to being an addressable named return value, would up
279    on the stack, this variable holds the named return value's
280    original location.  */
281 rtx original_result_rtx;
282
283 /* Sequence of insns which represents base initialization.  */
284 rtx base_init_insns;
285
286 /* C++: Keep these around to reduce calls to `get_identifier'.
287    Identifiers for `this' in member functions and the auto-delete
288    parameter for destructors.  */
289 tree this_identifier, in_charge_identifier;
290 /* Used in pointer to member functions, and in vtables. */
291 tree pfn_identifier, index_identifier, delta_identifier, delta2_identifier;
292 tree pfn_or_delta2_identifier;
293
294 /* A list (chain of TREE_LIST nodes) of named label uses.
295    The TREE_PURPOSE field is the list of variables defined
296    the the label's scope defined at the point of use.
297    The TREE_VALUE field is the LABEL_DECL used.
298    The TREE_TYPE field holds `current_binding_level' at the
299    point of the label's use.
300
301    Used only for jumps to as-yet undefined labels, since
302    jumps to defined labels can have their validity checked
303    by stmt.c.  */
304
305 static tree named_label_uses;
306
307 /* A list of objects which have constructors or destructors
308    which reside in the global scope.  The decl is stored in
309    the TREE_VALUE slot and the initializer is stored
310    in the TREE_PURPOSE slot.  */
311 tree static_aggregates;
312
313 /* A list of overloaded functions which we should forget ever
314    existed, such as functions declared in a function's scope,
315    once we leave that function's scope.  */
316 static tree overloads_to_forget;
317
318 /* -- end of C++ */
319
320 /* Two expressions that are constants with value zero.
321    The first is of type `int', the second of type `void *'.  */
322
323 tree integer_zero_node;
324 tree null_pointer_node;
325
326 /* A node for the integer constants 1, 2, and 3.  */
327
328 tree integer_one_node, integer_two_node, integer_three_node;
329
330 /* Nonzero if we have seen an invalid cross reference
331    to a struct, union, or enum, but not yet printed the message.  */
332
333 tree pending_invalid_xref;
334 /* File and line to appear in the eventual error message.  */
335 char *pending_invalid_xref_file;
336 int pending_invalid_xref_line;
337
338 /* While defining an enum type, this is 1 plus the last enumerator
339    constant value.  */
340
341 static tree enum_next_value;
342
343 /* Parsing a function declarator leaves a list of parameter names
344    or a chain or parameter decls here.  */
345
346 tree last_function_parms;
347
348 /* Parsing a function declarator leaves here a chain of structure
349    and enum types declared in the parmlist.  */
350
351 static tree last_function_parm_tags;
352
353 /* After parsing the declarator that starts a function definition,
354    `start_function' puts here the list of parameter names or chain of decls.
355    `store_parm_decls' finds it here.  */
356
357 static tree current_function_parms;
358
359 /* Similar, for last_function_parm_tags.  */
360 static tree current_function_parm_tags;
361
362 /* A list (chain of TREE_LIST nodes) of all LABEL_DECLs in the function
363    that have names.  Here so we can clear out their names' definitions
364    at the end of the function.  */
365
366 static tree named_labels;
367
368 /* A list of LABEL_DECLs from outer contexts that are currently shadowed.  */
369
370 static tree shadowed_labels;
371
372 #if 0 /* Not needed by C++ */
373 /* Nonzero when store_parm_decls is called indicates a varargs function.
374    Value not meaningful after store_parm_decls.  */
375
376 static int c_function_varargs;
377 #endif
378
379 /* The FUNCTION_DECL for the function currently being compiled,
380    or 0 if between functions.  */
381 tree current_function_decl;
382
383 /* Set to 0 at beginning of a function definition, set to 1 if
384    a return statement that specifies a return value is seen.  */
385
386 int current_function_returns_value;
387
388 /* Set to 0 at beginning of a function definition, set to 1 if
389    a return statement with no argument is seen.  */
390
391 int current_function_returns_null;
392
393 /* Set to 0 at beginning of a function definition, and whenever
394    a label (case or named) is defined.  Set to value of expression
395    returned from function when that value can be transformed into
396    a named return value.  */
397
398 tree current_function_return_value;
399
400 /* Set to nonzero by `grokdeclarator' for a function
401    whose return type is defaulted, if warnings for this are desired.  */
402
403 static int warn_about_return_type;
404
405 /* Nonzero when starting a function declared `extern inline'.  */
406
407 static int current_extern_inline;
408
409 /* Nonzero means give `double' the same size as `float'.  */
410
411 extern int flag_short_double;
412
413 /* Nonzero means don't recognize any builtin functions.  */
414
415 extern int flag_no_builtin;
416
417 /* Nonzero means do emit exported implementations of functions even if
418    they can be inlined.  */
419
420 extern int flag_implement_inlines;
421
422 /* Nonzero means handle things in ANSI, instead of GNU fashion.  This
423    flag should be tested for language behavior that's different between
424    ANSI and GNU, but not so horrible as to merit a PEDANTIC label.  */
425
426 extern int flag_ansi;
427
428 /* Nonzero if we want to support huge (> 2^(sizeof(short)*8-1) bytes)
429    objects. */
430 extern int flag_huge_objects;
431
432 /* Nonzero if we want to conserve space in the .o files.  We do this
433    by putting uninitialized data and runtime initialized data into
434    .common instead of .data at the expense of not flaging multiple
435    definitions.  */
436 extern int flag_conserve_space;
437
438 /* Pointers to the base and current top of the language name stack.  */
439
440 extern tree *current_lang_base, *current_lang_stack;
441 \f
442 /* C and C++ flags are in decl2.c.  */
443
444 /* Set to 0 at beginning of a constructor, set to 1
445    if that function does an allocation before referencing its
446    instance variable.  */
447 int current_function_assigns_this;
448 int current_function_just_assigned_this;
449
450 /* Set to 0 at beginning of a function.  Set non-zero when
451    store_parm_decls is called.  Don't call store_parm_decls
452    if this flag is non-zero!  */
453 int current_function_parms_stored;
454
455 /* Current end of entries in the gc obstack for stack pointer variables.  */
456
457 int current_function_obstack_index;
458
459 /* Flag saying whether we have used the obstack in this function or not.  */
460
461 int current_function_obstack_usage;
462
463 /* Flag used when debugging spew.c */
464
465 extern int spew_debug;
466
467 /* This is a copy of the class_shadowed list of the previous class binding
468    contour when at global scope.  It's used to reset IDENTIFIER_CLASS_VALUEs
469    when entering another class scope (i.e. a cache miss).  */
470 extern tree previous_class_values;
471
472 \f
473 /* Allocate a level of searching.  */
474 struct stack_level *
475 push_decl_level (stack, obstack)
476      struct stack_level *stack;
477      struct obstack *obstack;
478 {
479   struct stack_level tem;
480   tem.prev = stack;
481
482   return push_stack_level (obstack, (char *)&tem, sizeof (tem));
483 }
484 \f
485 /* For each binding contour we allocate a binding_level structure
486  * which records the names defined in that contour.
487  * Contours include:
488  *  0) the global one
489  *  1) one for each function definition,
490  *     where internal declarations of the parameters appear.
491  *  2) one for each compound statement,
492  *     to record its declarations.
493  *
494  * The current meaning of a name can be found by searching the levels from
495  * the current one out to the global one.
496  *
497  * Off to the side, may be the class_binding_level.  This exists
498  * only to catch class-local declarations.  It is otherwise
499  * nonexistent.
500  * 
501  * Also there may be binding levels that catch cleanups that
502  * must be run when exceptions occur.
503  */
504
505 /* Note that the information in the `names' component of the global contour
506    is duplicated in the IDENTIFIER_GLOBAL_VALUEs of all identifiers.  */
507
508 struct binding_level
509   {
510     /* A chain of _DECL nodes for all variables, constants, functions,
511      * and typedef types.  These are in the reverse of the order supplied.
512      */
513     tree names;
514
515     /* A list of structure, union and enum definitions,
516      * for looking up tag names.
517      * It is a chain of TREE_LIST nodes, each of whose TREE_PURPOSE is a name,
518      * or NULL_TREE; and whose TREE_VALUE is a RECORD_TYPE, UNION_TYPE,
519      * or ENUMERAL_TYPE node.
520      *
521      * C++: the TREE_VALUE nodes can be simple types for component_bindings.
522      *
523      */
524     tree tags;
525
526     /* For each level, a list of shadowed outer-level local definitions
527        to be restored when this level is popped.
528        Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
529        whose TREE_VALUE is its old definition (a kind of ..._DECL node).  */
530     tree shadowed;
531
532     /* Same, for IDENTIFIER_CLASS_VALUE.  */
533     tree class_shadowed;
534
535     /* Same, for IDENTIFIER_TYPE_VALUE.  */
536     tree type_shadowed;
537
538     /* For each level (except not the global one),
539        a chain of BLOCK nodes for all the levels
540        that were entered and exited one level down.  */
541     tree blocks;
542
543     /* The BLOCK node for this level, if one has been preallocated.
544        If 0, the BLOCK is allocated (if needed) when the level is popped.  */
545     tree this_block;
546
547     /* The binding level which this one is contained in (inherits from).  */
548     struct binding_level *level_chain;
549
550     /* Number of decls in `names' that have incomplete 
551        structure or union types.  */
552     unsigned short n_incomplete;
553
554     /* 1 for the level that holds the parameters of a function.
555        2 for the level that holds a class declaration.
556        3 for levels that hold parameter declarations.  */
557     unsigned parm_flag : 4;
558
559     /* 1 means make a BLOCK for this level regardless of all else.
560        2 for temporary binding contours created by the compiler.  */
561     unsigned keep : 3;
562
563     /* Nonzero if this level "doesn't exist" for tags.  */
564     unsigned tag_transparent : 1;
565
566     /* Nonzero if this level can safely have additional
567        cleanup-needing variables added to it.  */
568     unsigned more_cleanups_ok : 1;
569     unsigned have_cleanups : 1;
570
571     /* Nonzero if this level can safely have additional
572        exception-raising statements added to it.  */
573     unsigned more_exceptions_ok : 1;
574     unsigned have_exceptions : 1;
575
576     /* Nonzero if we should accept any name as an identifier in
577        this scope.  This happens in some template definitions.  */
578     unsigned accept_any : 1;
579
580     /* Nonzero if this level is for completing a template class definition
581        inside a binding level that temporarily binds the parameters.  This
582        means that definitions here should not be popped off when unwinding
583        this binding level.  (Not actually implemented this way,
584        unfortunately.)  */
585     unsigned pseudo_global : 1;
586
587     /* Two bits left for this word.  */
588
589 #if defined(DEBUG_CP_BINDING_LEVELS)
590     /* Binding depth at which this level began.  */
591     unsigned binding_depth;
592 #endif /* defined(DEBUG_CP_BINDING_LEVELS) */
593   };
594
595 #define NULL_BINDING_LEVEL ((struct binding_level *) NULL)
596   
597 /* The (non-class) binding level currently in effect.  */
598
599 static struct binding_level *current_binding_level;
600
601 /* The binding level of the current class, if any.  */
602
603 static struct binding_level *class_binding_level;
604
605 /* The current (class or non-class) binding level currently in effect.  */
606
607 #define inner_binding_level \
608   (class_binding_level ? class_binding_level : current_binding_level)
609
610 /* A chain of binding_level structures awaiting reuse.  */
611
612 static struct binding_level *free_binding_level;
613
614 /* The outermost binding level, for names of file scope.
615    This is created when the compiler is started and exists
616    through the entire run.  */
617
618 static struct binding_level *global_binding_level;
619
620 /* Binding level structures are initialized by copying this one.  */
621
622 static struct binding_level clear_binding_level;
623
624 /* Nonzero means unconditionally make a BLOCK for the next level pushed.  */
625
626 static int keep_next_level_flag;
627
628 #if defined(DEBUG_CP_BINDING_LEVELS)
629 static int binding_depth = 0;
630 static int is_class_level = 0;
631
632 static void
633 indent ()
634 {
635   register unsigned i;
636
637   for (i = 0; i < binding_depth*2; i++)
638     putc (' ', stderr);
639 }
640 #endif /* defined(DEBUG_CP_BINDING_LEVELS) */
641
642 static tree pushdecl_with_scope PROTO((tree, struct binding_level *));
643
644 static void
645 push_binding_level (newlevel, tag_transparent, keep)
646      struct binding_level *newlevel;
647      int tag_transparent, keep;
648 {
649   /* Add this level to the front of the chain (stack) of levels that
650      are active.  */
651   *newlevel = clear_binding_level;
652   if (class_binding_level)
653     {
654       newlevel->level_chain = class_binding_level;
655       class_binding_level = (struct binding_level *)0;
656     }
657   else
658     {
659       newlevel->level_chain = current_binding_level;
660     }
661   current_binding_level = newlevel;
662   newlevel->tag_transparent = tag_transparent;
663   newlevel->more_cleanups_ok = 1;
664   newlevel->more_exceptions_ok = 1;
665   newlevel->keep = keep;
666 #if defined(DEBUG_CP_BINDING_LEVELS)
667   newlevel->binding_depth = binding_depth;
668   indent ();
669   fprintf (stderr, "push %s level 0x%08x line %d\n",
670            (is_class_level) ? "class" : "block", newlevel, lineno);
671   is_class_level = 0;
672   binding_depth++;
673 #endif /* defined(DEBUG_CP_BINDING_LEVELS) */
674 }
675
676 static void
677 pop_binding_level ()
678 {
679   if (class_binding_level)
680     current_binding_level = class_binding_level;
681
682   if (global_binding_level)
683     {
684       /* cannot pop a level, if there are none left to pop. */
685       if (current_binding_level == global_binding_level)
686         my_friendly_abort (123);
687     }
688   /* Pop the current level, and free the structure for reuse.  */
689 #if defined(DEBUG_CP_BINDING_LEVELS)
690   binding_depth--;
691   indent ();
692   fprintf (stderr, "pop  %s level 0x%08x line %d\n",
693           (is_class_level) ? "class" : "block",
694           current_binding_level, lineno);
695   if (is_class_level != (current_binding_level == class_binding_level))
696 #if 0 /* XXX Don't abort when we're watching how things are being managed.  */
697     abort ();
698 #else
699   {
700     indent ();
701     fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
702   }
703 #endif
704   is_class_level = 0;
705 #endif /* defined(DEBUG_CP_BINDING_LEVELS) */
706   {
707     register struct binding_level *level = current_binding_level;
708     current_binding_level = current_binding_level->level_chain;
709     level->level_chain = free_binding_level;
710 #if 0 /* defined(DEBUG_CP_BINDING_LEVELS) */
711     if (level->binding_depth != binding_depth)
712       abort ();
713 #endif /* defined(DEBUG_CP_BINDING_LEVELS) */
714       free_binding_level = level;
715
716     class_binding_level = current_binding_level;
717     if (class_binding_level->parm_flag != 2)
718       class_binding_level = 0;
719     while (current_binding_level->parm_flag == 2)
720       current_binding_level = current_binding_level->level_chain;
721   }
722 }
723 \f
724 /* Nonzero if we are currently in the global binding level.  */
725
726 int
727 global_bindings_p ()
728 {
729   return current_binding_level == global_binding_level;
730 }
731
732 void
733 keep_next_level ()
734 {
735   keep_next_level_flag = 1;
736 }
737
738 /* Nonzero if the current level needs to have a BLOCK made.  */
739
740 int
741 kept_level_p ()
742 {
743   return (current_binding_level->blocks != NULL_TREE
744           || current_binding_level->keep
745           || current_binding_level->names != NULL_TREE
746           || (current_binding_level->tags != NULL_TREE
747               && !current_binding_level->tag_transparent));
748 }
749
750 /* Identify this binding level as a level of parameters.  */
751
752 void
753 declare_parm_level ()
754 {
755   current_binding_level->parm_flag = 1;
756 }
757
758 /* Identify this binding level as a level of a default exception handler.  */
759
760 void
761 declare_implicit_exception ()
762 {
763   current_binding_level->parm_flag = 3;
764 }
765
766 /* Nonzero if current binding contour contains expressions
767    that might raise exceptions.  */
768
769 int
770 have_exceptions_p ()
771 {
772   return current_binding_level->have_exceptions;
773 }
774
775 void
776 declare_uninstantiated_type_level ()
777 {
778   current_binding_level->accept_any = 1;
779 }
780
781 int
782 uninstantiated_type_level_p ()
783 {
784   return current_binding_level->accept_any;
785 }
786
787 void
788 declare_pseudo_global_level ()
789 {
790   current_binding_level->pseudo_global = 1;
791 }
792
793 int
794 pseudo_global_level_p ()
795 {
796   return current_binding_level->pseudo_global;
797 }
798
799 void
800 set_class_shadows (shadows)
801      tree shadows;
802 {
803   class_binding_level->class_shadowed = shadows;
804 }
805
806 /* Enter a new binding level.
807    If TAG_TRANSPARENT is nonzero, do so only for the name space of variables,
808    not for that of tags.  */
809
810 void
811 pushlevel (tag_transparent)
812      int tag_transparent;
813 {
814   register struct binding_level *newlevel = NULL_BINDING_LEVEL;
815
816   /* If this is the top level of a function,
817      just make sure that NAMED_LABELS is 0.
818      They should have been set to 0 at the end of the previous function.  */
819
820   if (current_binding_level == global_binding_level)
821     my_friendly_assert (named_labels == NULL_TREE, 134);
822
823   /* Reuse or create a struct for this binding level.  */
824
825 #if defined(DEBUG_CP_BINDING_LEVELS)
826   if (0)
827 #else /* !defined(DEBUG_CP_BINDING_LEVELS) */
828   if (free_binding_level)
829 #endif /* !defined(DEBUG_CP_BINDING_LEVELS) */
830     {
831       newlevel = free_binding_level;
832       free_binding_level = free_binding_level->level_chain;
833     }
834   else
835     {
836       /* Create a new `struct binding_level'.  */
837       newlevel = (struct binding_level *) xmalloc (sizeof (struct binding_level));
838     }
839   push_binding_level (newlevel, tag_transparent, keep_next_level_flag);
840   GNU_xref_start_scope ((HOST_WIDE_INT) newlevel);
841   keep_next_level_flag = 0;
842 }
843
844 void
845 pushlevel_temporary (tag_transparent)
846      int tag_transparent;
847 {
848   pushlevel (tag_transparent);
849   current_binding_level->keep = 2;
850   clear_last_expr ();
851
852   /* Note we don't call push_momentary() here.  Otherwise, it would cause
853      cleanups to be allocated on the momentary obstack, and they will be
854      overwritten by the next statement.  */
855
856   expand_start_bindings (0);
857 }
858
859 /* Exit a binding level.
860    Pop the level off, and restore the state of the identifier-decl mappings
861    that were in effect when this level was entered.
862
863    If KEEP == 1, this level had explicit declarations, so
864    and create a "block" (a BLOCK node) for the level
865    to record its declarations and subblocks for symbol table output.
866
867    If KEEP == 2, this level's subblocks go to the front,
868    not the back of the current binding level.  This happens,
869    for instance, when code for constructors and destructors
870    need to generate code at the end of a function which must
871    be moved up to the front of the function.
872
873    If FUNCTIONBODY is nonzero, this level is the body of a function,
874    so create a block as if KEEP were set and also clear out all
875    label names.
876
877    If REVERSE is nonzero, reverse the order of decls before putting
878    them into the BLOCK.  */
879
880 tree
881 poplevel (keep, reverse, functionbody)
882      int keep;
883      int reverse;
884      int functionbody;
885 {
886   register tree link;
887   /* The chain of decls was accumulated in reverse order.
888      Put it into forward order, just for cleanliness.  */
889   tree decls;
890   int tmp = functionbody;
891   int implicit_try_block = current_binding_level->parm_flag == 3;
892   int real_functionbody = current_binding_level->keep == 2
893     ? ((functionbody = 0), tmp) : functionbody;
894   tree tags = functionbody >= 0 ? current_binding_level->tags : 0;
895   tree subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
896   tree block = NULL_TREE;
897   tree decl;
898   int block_previously_created;
899
900   GNU_xref_end_scope ((HOST_WIDE_INT) current_binding_level,
901                       (HOST_WIDE_INT) current_binding_level->level_chain,
902                       current_binding_level->parm_flag,
903                       current_binding_level->keep,
904                       current_binding_level->tag_transparent);
905
906   if (current_binding_level->keep == 1)
907     keep = 1;
908
909   /* This warning is turned off because it causes warnings for
910      declarations like `extern struct foo *x'.  */
911 #if 0
912   /* Warn about incomplete structure types in this level.  */
913   for (link = tags; link; link = TREE_CHAIN (link))
914     if (TYPE_SIZE (TREE_VALUE (link)) == NULL_TREE)
915       {
916         tree type = TREE_VALUE (link);
917         char *errmsg;
918         switch (TREE_CODE (type))
919           {
920           case RECORD_TYPE:
921             errmsg = "`struct %s' incomplete in scope ending here";
922             break;
923           case UNION_TYPE:
924             errmsg = "`union %s' incomplete in scope ending here";
925             break;
926           case ENUMERAL_TYPE:
927             errmsg = "`enum %s' incomplete in scope ending here";
928             break;
929           }
930         if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
931           error (errmsg, IDENTIFIER_POINTER (TYPE_NAME (type)));
932         else
933           /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL.  */
934           error (errmsg, TYPE_NAME_STRING (type));
935       }
936 #endif /* 0 */
937
938   /* Get the decls in the order they were written.
939      Usually current_binding_level->names is in reverse order.
940      But parameter decls were previously put in forward order.  */
941
942   if (reverse)
943     current_binding_level->names
944       = decls = nreverse (current_binding_level->names);
945   else
946     decls = current_binding_level->names;
947
948   /* Output any nested inline functions within this block
949      if they weren't already output.  */
950
951   for (decl = decls; decl; decl = TREE_CHAIN (decl))
952     if (TREE_CODE (decl) == FUNCTION_DECL
953         && ! TREE_ASM_WRITTEN (decl)
954         && DECL_INITIAL (decl) != NULL_TREE
955         && TREE_ADDRESSABLE (decl))
956       {
957         /* If this decl was copied from a file-scope decl
958            on account of a block-scope extern decl,
959            propagate TREE_ADDRESSABLE to the file-scope decl.  */
960         if (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE)
961           TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (decl)) = 1;
962         else
963           {
964             push_function_context ();
965             output_inline_function (decl);
966             pop_function_context ();
967           }
968       }
969
970   /* If there were any declarations or structure tags in that level,
971      or if this level is a function body,
972      create a BLOCK to record them for the life of this function.  */
973
974   block = NULL_TREE;
975   block_previously_created = (current_binding_level->this_block != NULL_TREE);
976   if (block_previously_created)
977     block = current_binding_level->this_block;
978   else if (keep == 1 || functionbody)
979     block = make_node (BLOCK);
980   if (block != NULL_TREE)
981     {
982       BLOCK_VARS (block) = decls;
983       BLOCK_TYPE_TAGS (block) = tags;
984       BLOCK_SUBBLOCKS (block) = subblocks;
985       /* If we created the block earlier on, and we are just diddling it now,
986          then it already should have a proper BLOCK_END_NOTE value associated
987          with it, so avoid trashing that.  Otherwise, for a new block, install
988          a new BLOCK_END_NOTE value.  */
989       if (! block_previously_created)
990         remember_end_note (block);
991     }
992
993   /* In each subblock, record that this is its superior.  */
994
995   if (keep >= 0)
996     for (link = subblocks; link; link = TREE_CHAIN (link))
997       BLOCK_SUPERCONTEXT (link) = block;
998
999   /* Clear out the meanings of the local variables of this level.  */
1000
1001   for (link = decls; link; link = TREE_CHAIN (link))
1002     {
1003       if (DECL_NAME (link) != NULL_TREE)
1004         {
1005           /* If the ident. was used or addressed via a local extern decl,
1006              don't forget that fact.  */
1007           if (DECL_EXTERNAL (link))
1008             {
1009               if (TREE_USED (link))
1010                 TREE_USED (DECL_ASSEMBLER_NAME (link)) = 1;
1011               if (TREE_ADDRESSABLE (link))
1012                 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (link)) = 1;
1013             }
1014           IDENTIFIER_LOCAL_VALUE (DECL_NAME (link)) = NULL_TREE;
1015         }
1016     }
1017
1018   /* Restore all name-meanings of the outer levels
1019      that were shadowed by this level.  */
1020
1021   for (link = current_binding_level->shadowed; link; link = TREE_CHAIN (link))
1022     IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
1023   for (link = current_binding_level->class_shadowed;
1024        link; link = TREE_CHAIN (link))
1025     IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
1026   for (link = current_binding_level->type_shadowed;
1027        link; link = TREE_CHAIN (link))
1028     IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
1029
1030   /* If the level being exited is the top level of a function,
1031      check over all the labels.  */
1032
1033   if (functionbody)
1034     {
1035       /* If this is the top level block of a function,
1036          the vars are the function's parameters.
1037          Don't leave them in the BLOCK because they are
1038          found in the FUNCTION_DECL instead.  */
1039
1040       BLOCK_VARS (block) = 0;
1041
1042       /* Clear out the definitions of all label names,
1043          since their scopes end here.  */
1044
1045       for (link = named_labels; link; link = TREE_CHAIN (link))
1046         {
1047           register tree label = TREE_VALUE (link);
1048
1049           if (DECL_INITIAL (label) == NULL_TREE)
1050             {
1051               cp_error_at ("label `%D' used but not defined", label);
1052               /* Avoid crashing later.  */
1053               define_label (input_filename, 1, DECL_NAME (label));
1054             }
1055           else if (warn_unused && !TREE_USED (label))
1056             cp_warning_at ("label `%D' defined but not used", label);
1057           SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), NULL_TREE);
1058
1059           /* Put the labels into the "variables" of the
1060              top-level block, so debugger can see them.  */
1061           TREE_CHAIN (label) = BLOCK_VARS (block);
1062           BLOCK_VARS (block) = label;
1063         }
1064
1065       named_labels = NULL_TREE;
1066     }
1067
1068   /* Any uses of undefined labels now operate under constraints
1069      of next binding contour.  */
1070   {
1071     struct binding_level *level_chain;
1072     level_chain = current_binding_level->level_chain;
1073     if (level_chain)
1074       {
1075         tree labels;
1076         for (labels = named_label_uses; labels; labels = TREE_CHAIN (labels))
1077           if (TREE_TYPE (labels) == (tree)current_binding_level)
1078             {
1079               TREE_TYPE (labels) = (tree)level_chain;
1080               TREE_PURPOSE (labels) = level_chain->names;
1081             }
1082       }
1083   }
1084
1085   tmp = current_binding_level->keep;
1086
1087   pop_binding_level ();
1088   if (functionbody)
1089     DECL_INITIAL (current_function_decl) = block;
1090   else if (block)
1091     {
1092       if (!block_previously_created)
1093         current_binding_level->blocks
1094           = chainon (current_binding_level->blocks, block);
1095     }
1096   /* If we did not make a block for the level just exited,
1097      any blocks made for inner levels
1098      (since they cannot be recorded as subblocks in that level)
1099      must be carried forward so they will later become subblocks
1100      of something else.  */
1101   else if (subblocks)
1102     {
1103       if (keep == 2)
1104         current_binding_level->blocks
1105           = chainon (subblocks, current_binding_level->blocks);
1106       else
1107         current_binding_level->blocks
1108           = chainon (current_binding_level->blocks, subblocks);
1109     }
1110
1111   /* Take care of compiler's internal binding structures.  */
1112   if (tmp == 2 && !implicit_try_block)
1113     {
1114 #if 0
1115       /* We did not call push_momentary for this
1116          binding contour, so there is nothing to pop.  */
1117       pop_momentary ();
1118 #endif
1119       expand_end_bindings (getdecls (), keep, 1);
1120       /* Each and every BLOCK node created here in `poplevel' is important
1121          (e.g. for proper debugging information) so if we created one
1122          earlier, mark it as "used".  */
1123       if (block)
1124         TREE_USED (block) = 1;
1125       block = poplevel (keep, reverse, real_functionbody);
1126     }
1127
1128   /* Each and every BLOCK node created here in `poplevel' is important
1129      (e.g. for proper debugging information) so if we created one
1130      earlier, mark it as "used".  */
1131   if (block)
1132     TREE_USED (block) = 1;
1133   return block;
1134 }
1135
1136 /* Delete the node BLOCK from the current binding level.
1137    This is used for the block inside a stmt expr ({...})
1138    so that the block can be reinserted where appropriate.  */
1139
1140 void
1141 delete_block (block)
1142      tree block;
1143 {
1144   tree t;
1145   if (current_binding_level->blocks == block)
1146     current_binding_level->blocks = TREE_CHAIN (block);
1147   for (t = current_binding_level->blocks; t;)
1148     {
1149       if (TREE_CHAIN (t) == block)
1150         TREE_CHAIN (t) = TREE_CHAIN (block);
1151       else
1152         t = TREE_CHAIN (t);
1153     }
1154   TREE_CHAIN (block) = NULL_TREE;
1155   /* Clear TREE_USED which is always set by poplevel.
1156      The flag is set again if insert_block is called.  */
1157   TREE_USED (block) = 0;
1158 }
1159
1160 /* Insert BLOCK at the end of the list of subblocks of the
1161    current binding level.  This is used when a BIND_EXPR is expanded,
1162    to handle the BLOCK node inside the BIND_EXPR.  */
1163
1164 void
1165 insert_block (block)
1166      tree block;
1167 {
1168   TREE_USED (block) = 1;
1169   current_binding_level->blocks
1170     = chainon (current_binding_level->blocks, block);
1171 }
1172
1173 /* Add BLOCK to the current list of blocks for this binding contour.  */
1174 void
1175 add_block_current_level (block)
1176      tree block;
1177 {
1178   current_binding_level->blocks
1179     = chainon (current_binding_level->blocks, block);
1180 }
1181
1182 /* Set the BLOCK node for the innermost scope
1183    (the one we are currently in).  */
1184
1185 void
1186 set_block (block)
1187     register tree block;
1188 {
1189   current_binding_level->this_block = block;
1190 }
1191
1192 /* Do a pushlevel for class declarations.  */
1193 void
1194 pushlevel_class ()
1195 {
1196   register struct binding_level *newlevel;
1197
1198   /* Reuse or create a struct for this binding level.  */
1199 #if defined(DEBUG_CP_BINDING_LEVELS)
1200   if (0)
1201 #else /* !defined(DEBUG_CP_BINDING_LEVELS) */
1202   if (free_binding_level)
1203 #endif /* !defined(DEBUG_CP_BINDING_LEVELS) */
1204     {
1205       newlevel = free_binding_level;
1206       free_binding_level = free_binding_level->level_chain;
1207     }
1208   else
1209     {
1210       /* Create a new `struct binding_level'.  */
1211       newlevel = (struct binding_level *) xmalloc (sizeof (struct binding_level));
1212     }
1213
1214 #if defined(DEBUG_CP_BINDING_LEVELS)
1215   is_class_level = 1;
1216 #endif /* defined(DEBUG_CP_BINDING_LEVELS) */
1217
1218   push_binding_level (newlevel, 0, 0);
1219
1220   decl_stack = push_decl_level (decl_stack, &decl_obstack);
1221   class_binding_level = current_binding_level;
1222   class_binding_level->parm_flag = 2;
1223   /* We have just pushed into a new binding level.  Now, fake out the rest
1224      of the compiler.  Set the `current_binding_level' back to point to
1225      the most closely containing non-class binding level.  */
1226   do
1227     {
1228       current_binding_level = current_binding_level->level_chain;
1229     }
1230   while (current_binding_level->parm_flag == 2);
1231 }
1232
1233 /* ...and a poplevel for class declarations.  */
1234 tree
1235 poplevel_class ()
1236 {
1237   register struct binding_level *level = class_binding_level;
1238   tree block = NULL_TREE;
1239   tree shadowed;
1240
1241   my_friendly_assert (level != 0, 354);
1242   
1243   decl_stack = pop_stack_level (decl_stack);
1244   for (shadowed = level->shadowed; shadowed; shadowed = TREE_CHAIN (shadowed))
1245     IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (shadowed)) = TREE_VALUE (shadowed);
1246   /* If we're leaving a toplevel class, don't bother to do the setting
1247      of IDENTIFER_CLASS_VALUE to NULL_TREE, since first of all this slot
1248      shouldn't even be used when current_class_type isn't set, and second,
1249      if we don't touch it here, we're able to use the caching effect if the
1250      next time we're entering a class scope, it is the same class.  */
1251   if (current_class_depth != 1)
1252     for (shadowed = level->class_shadowed;
1253          shadowed;
1254          shadowed = TREE_CHAIN (shadowed))
1255       IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed)) = TREE_VALUE (shadowed);
1256   else
1257     /* Remember to save what IDENTIFIER's were bound in this scope so we
1258        can recover from cache misses.  */
1259     previous_class_values = class_binding_level->class_shadowed;
1260   for (shadowed = level->type_shadowed;
1261        shadowed;
1262        shadowed = TREE_CHAIN (shadowed))
1263     IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed)) = TREE_VALUE (shadowed);
1264
1265   GNU_xref_end_scope ((HOST_WIDE_INT) class_binding_level,
1266                       (HOST_WIDE_INT) class_binding_level->level_chain,
1267                       class_binding_level->parm_flag,
1268                       class_binding_level->keep,
1269                       class_binding_level->tag_transparent);
1270
1271   if (class_binding_level->parm_flag != 2)
1272     class_binding_level = (struct binding_level *)0;
1273
1274   /* Now, pop out of the the binding level which we created up in the
1275      `pushlevel_class' routine.  */
1276 #if defined(DEBUG_CP_BINDING_LEVELS)
1277   is_class_level = 1;
1278 #endif /* defined(DEBUG_CP_BINDING_LEVELS) */
1279
1280   pop_binding_level ();
1281
1282   return block;
1283 }
1284 \f
1285 /* For debugging.  */
1286 int no_print_functions = 0;
1287 int no_print_builtins = 0;
1288
1289 void
1290 print_binding_level (lvl)
1291      struct binding_level *lvl;
1292 {
1293   tree t;
1294   int i = 0, len;
1295   fprintf (stderr, " blocks=");
1296   fprintf (stderr, HOST_PTR_PRINTF, lvl->blocks);
1297   fprintf (stderr, " n_incomplete=%d parm_flag=%d keep=%d",
1298            lvl->n_incomplete, lvl->parm_flag, lvl->keep);
1299   if (lvl->tag_transparent)
1300     fprintf (stderr, " tag-transparent");
1301   if (lvl->more_cleanups_ok)
1302     fprintf (stderr, " more-cleanups-ok");
1303   if (lvl->have_cleanups)
1304     fprintf (stderr, " have-cleanups");
1305   if (lvl->more_exceptions_ok)
1306     fprintf (stderr, " more-exceptions-ok");
1307   if (lvl->have_exceptions)
1308     fprintf (stderr, " have-exceptions");
1309   fprintf (stderr, "\n");
1310   if (lvl->names)
1311     {
1312       fprintf (stderr, " names:\t");
1313       /* We can probably fit 3 names to a line?  */
1314       for (t = lvl->names; t; t = TREE_CHAIN (t))
1315         {
1316           if (no_print_functions && (TREE_CODE(t) == FUNCTION_DECL)) 
1317             continue;
1318           if (no_print_builtins
1319               && (TREE_CODE(t) == TYPE_DECL)
1320               && (!strcmp(DECL_SOURCE_FILE(t),"<built-in>")))
1321             continue;
1322
1323           /* Function decls tend to have longer names.  */
1324           if (TREE_CODE (t) == FUNCTION_DECL)
1325             len = 3;
1326           else
1327             len = 2;
1328           i += len;
1329           if (i > 6)
1330             {
1331               fprintf (stderr, "\n\t");
1332               i = len;
1333             }
1334           print_node_brief (stderr, "", t, 0);
1335           if (TREE_CODE (t) == ERROR_MARK)
1336             break;
1337         }
1338       if (i)
1339         fprintf (stderr, "\n");
1340     }
1341   if (lvl->tags)
1342     {
1343       fprintf (stderr, " tags:\t");
1344       i = 0;
1345       for (t = lvl->tags; t; t = TREE_CHAIN (t))
1346         {
1347           if (TREE_PURPOSE (t) == NULL_TREE)
1348             len = 3;
1349           else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t)))
1350             len = 2;
1351           else
1352             len = 4;
1353           i += len;
1354           if (i > 5)
1355             {
1356               fprintf (stderr, "\n\t");
1357               i = len;
1358             }
1359           if (TREE_PURPOSE (t) == NULL_TREE)
1360             {
1361               print_node_brief (stderr, "<unnamed-typedef", TREE_VALUE (t), 0);
1362               fprintf (stderr, ">");
1363             }
1364           else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t)))
1365             print_node_brief (stderr, "", TREE_VALUE (t), 0);
1366           else
1367             {
1368               print_node_brief (stderr, "<typedef", TREE_PURPOSE (t), 0);
1369               print_node_brief (stderr, "", TREE_VALUE (t), 0);
1370               fprintf (stderr, ">");
1371             }
1372         }
1373       if (i)
1374         fprintf (stderr, "\n");
1375     }
1376   if (lvl->shadowed)
1377     {
1378       fprintf (stderr, " shadowed:");
1379       for (t = lvl->shadowed; t; t = TREE_CHAIN (t))
1380         {
1381           fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1382         }
1383       fprintf (stderr, "\n");
1384     }
1385   if (lvl->class_shadowed)
1386     {
1387       fprintf (stderr, " class-shadowed:");
1388       for (t = lvl->class_shadowed; t; t = TREE_CHAIN (t))
1389         {
1390           fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1391         }
1392       fprintf (stderr, "\n");
1393     }
1394   if (lvl->type_shadowed)
1395     {
1396       fprintf (stderr, " type-shadowed:");
1397       for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
1398         {
1399 #if 0
1400           fprintf (stderr, "\n\t");
1401           print_node_brief (stderr, "<", TREE_PURPOSE (t), 0);
1402           if (TREE_VALUE (t))
1403             print_node_brief (stderr, " ", TREE_VALUE (t), 0);
1404           else
1405             fprintf (stderr, " (none)");
1406           fprintf (stderr, ">");
1407 #else
1408           fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1409 #endif
1410         }
1411       fprintf (stderr, "\n");
1412     }
1413 }
1414
1415 void
1416 print_other_binding_stack (stack)
1417      struct binding_level *stack;
1418 {
1419   struct binding_level *level;
1420   for (level = stack; level != global_binding_level; level = level->level_chain)
1421     {
1422       fprintf (stderr, "binding level ");
1423       fprintf (stderr, HOST_PTR_PRINTF, level);
1424       fprintf (stderr, "\n");
1425       print_binding_level (level);
1426     }
1427 }
1428
1429 void
1430 print_binding_stack ()
1431 {
1432   struct binding_level *b;
1433   fprintf (stderr, "current_binding_level=");
1434   fprintf (stderr, HOST_PTR_PRINTF, current_binding_level);
1435   fprintf (stderr, "\nclass_binding_level=");
1436   fprintf (stderr, HOST_PTR_PRINTF, class_binding_level);
1437   fprintf (stderr, "\nglobal_binding_level=");
1438   fprintf (stderr, HOST_PTR_PRINTF, global_binding_level);
1439   fprintf (stderr, "\n");
1440   if (class_binding_level)
1441     {
1442       for (b = class_binding_level; b; b = b->level_chain)
1443         if (b == current_binding_level)
1444           break;
1445       if (b)
1446         b = class_binding_level;
1447       else
1448         b = current_binding_level;
1449     }
1450   else
1451     b = current_binding_level;
1452   print_other_binding_stack (b);
1453   fprintf (stderr, "global:\n");
1454   print_binding_level (global_binding_level);
1455 }
1456 \f
1457 /* Subroutines for reverting temporarily to top-level for instantiation
1458    of templates and such.  We actually need to clear out the class- and
1459    local-value slots of all identifiers, so that only the global values
1460    are at all visible.  Simply setting current_binding_level to the global
1461    scope isn't enough, because more binding levels may be pushed.  */
1462 struct saved_scope {
1463   struct binding_level *old_binding_level;
1464   tree old_bindings;
1465   struct saved_scope *prev;
1466   tree class_name, class_type, class_decl, function_decl;
1467   struct binding_level *class_bindings;
1468   tree previous_class_type;
1469   tree *lang_base, *lang_stack, lang_name;
1470   int lang_stacksize;
1471   tree named_labels;
1472 };
1473 static struct saved_scope *current_saved_scope;
1474 extern tree prev_class_type;
1475
1476 void
1477 push_to_top_level ()
1478 {
1479   extern int current_lang_stacksize;
1480   struct saved_scope *s =
1481     (struct saved_scope *) xmalloc (sizeof (struct saved_scope));
1482   struct binding_level *b = current_binding_level;
1483   tree old_bindings = NULL_TREE;
1484
1485   /* Have to include global_binding_level, because class-level decls
1486      aren't listed anywhere useful.  */
1487   for (; b; b = b->level_chain)
1488     {
1489       tree t;
1490
1491       if (b == global_binding_level)
1492         continue;
1493       
1494       for (t = b->names; t; t = TREE_CHAIN (t))
1495         {
1496           tree binding, t1, t2 = t;
1497           tree id = DECL_ASSEMBLER_NAME (t2);
1498
1499           if (!id
1500               || (!IDENTIFIER_LOCAL_VALUE (id)
1501                   && !IDENTIFIER_CLASS_VALUE (id)))
1502             continue;
1503
1504           for (t1 = old_bindings; t1; t1 = TREE_CHAIN (t1))
1505             if (TREE_VEC_ELT (t1, 0) == id)
1506               goto skip_it;
1507             
1508           binding = make_tree_vec (4);
1509           if (id)
1510             {
1511               my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 135);
1512               TREE_VEC_ELT (binding, 0) = id;
1513               TREE_VEC_ELT (binding, 1) = IDENTIFIER_TYPE_VALUE (id);
1514               TREE_VEC_ELT (binding, 2) = IDENTIFIER_LOCAL_VALUE (id);
1515               TREE_VEC_ELT (binding, 3) = IDENTIFIER_CLASS_VALUE (id);
1516               IDENTIFIER_LOCAL_VALUE (id) = NULL_TREE;
1517               IDENTIFIER_CLASS_VALUE (id) = NULL_TREE;
1518             }
1519           TREE_CHAIN (binding) = old_bindings;
1520           old_bindings = binding;
1521         skip_it:
1522           ;
1523         }
1524       /* Unwind type-value slots back to top level.  */
1525       for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
1526         SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
1527     }
1528   /* Clear out class-level bindings cache.  */
1529   if (current_binding_level == global_binding_level
1530       && previous_class_type != NULL_TREE)
1531     {
1532       popclass (-1);
1533       previous_class_type = NULL_TREE;
1534     }
1535
1536   s->old_binding_level = current_binding_level;
1537   current_binding_level = global_binding_level;
1538
1539   s->class_name = current_class_name;
1540   s->class_type = current_class_type;
1541   s->class_decl = current_class_decl;
1542   s->function_decl = current_function_decl;
1543   s->class_bindings = class_binding_level;
1544   s->previous_class_type = previous_class_type;
1545   s->lang_stack = current_lang_stack;
1546   s->lang_base = current_lang_base;
1547   s->lang_stacksize = current_lang_stacksize;
1548   s->lang_name = current_lang_name;
1549   s->named_labels = named_labels;
1550   current_class_name = current_class_type = current_class_decl = NULL_TREE;
1551   current_function_decl = NULL_TREE;
1552   class_binding_level = (struct binding_level *)0;
1553   previous_class_type = NULL_TREE;
1554   current_lang_stacksize = 10;
1555   current_lang_stack = current_lang_base
1556     = (tree *) xmalloc (current_lang_stacksize * sizeof (tree));
1557   current_lang_name = lang_name_cplusplus;
1558   strict_prototype = strict_prototypes_lang_cplusplus;
1559   named_labels = NULL_TREE;
1560
1561   s->prev = current_saved_scope;
1562   s->old_bindings = old_bindings;
1563   current_saved_scope = s;
1564 }
1565
1566 void
1567 pop_from_top_level ()
1568 {
1569   extern int current_lang_stacksize;
1570   struct saved_scope *s = current_saved_scope;
1571   tree t;
1572
1573   if (previous_class_type)
1574     previous_class_type = NULL_TREE;
1575
1576   current_binding_level = s->old_binding_level;
1577   current_saved_scope = s->prev;
1578   for (t = s->old_bindings; t; t = TREE_CHAIN (t))
1579     {
1580       tree id = TREE_VEC_ELT (t, 0);
1581       if (id)
1582         {
1583           IDENTIFIER_TYPE_VALUE (id) = TREE_VEC_ELT (t, 1);
1584           IDENTIFIER_LOCAL_VALUE (id) = TREE_VEC_ELT (t, 2);
1585           IDENTIFIER_CLASS_VALUE (id) = TREE_VEC_ELT (t, 3);
1586         }
1587     }
1588   current_class_name = s->class_name;
1589   current_class_type = s->class_type;
1590   current_class_decl = s->class_decl;
1591   if (current_class_type)
1592     C_C_D = CLASSTYPE_INST_VAR (current_class_type);
1593   else
1594     C_C_D = NULL_TREE;
1595   current_function_decl = s->function_decl;
1596   class_binding_level = s->class_bindings;
1597   previous_class_type = s->previous_class_type;
1598   free (current_lang_base);
1599   current_lang_base = s->lang_base;
1600   current_lang_stack = s->lang_stack;
1601   current_lang_name = s->lang_name;
1602   current_lang_stacksize = s->lang_stacksize;
1603   if (current_lang_name == lang_name_cplusplus)
1604     strict_prototype = strict_prototypes_lang_cplusplus;
1605   else if (current_lang_name == lang_name_c)
1606     strict_prototype = strict_prototypes_lang_c;
1607   named_labels = s->named_labels;
1608
1609   free (s);
1610 }
1611 \f
1612 /* Push a definition of struct, union or enum tag "name".
1613    into binding_level "b".   "type" should be the type node, 
1614    We assume that the tag "name" is not already defined.
1615
1616    Note that the definition may really be just a forward reference.
1617    In that case, the TYPE_SIZE will be a NULL_TREE.
1618
1619    C++ gratuitously puts all these tags in the name space. */
1620
1621 /* When setting the IDENTIFIER_TYPE_VALUE field of an identifier ID,
1622    record the shadowed value for this binding contour.  TYPE is
1623    the type that ID maps to.  */
1624
1625 static void
1626 set_identifier_type_value_with_scope (id, type, b)
1627      tree id;
1628      tree type;
1629      struct binding_level *b;
1630 {
1631   if (b != global_binding_level)
1632     {
1633       tree old_type_value = IDENTIFIER_TYPE_VALUE (id);
1634       b->type_shadowed
1635         = tree_cons (id, old_type_value, b->type_shadowed);
1636     }
1637   SET_IDENTIFIER_TYPE_VALUE (id, type);
1638 }
1639
1640 /* As set_identifier_type_value_with_scope, but using inner_binding_level. */
1641
1642 void
1643 set_identifier_type_value (id, type)
1644      tree id;
1645      tree type;
1646 {
1647   set_identifier_type_value_with_scope (id, type, inner_binding_level);
1648 }
1649
1650 /* Subroutine "set_nested_typename" builds the nested-typename of
1651    the type decl in question.  (Argument CLASSNAME can actually be
1652    a function as well, if that's the smallest containing scope.)  */
1653
1654 static void
1655 set_nested_typename (decl, classname, name, type)
1656      tree decl, classname, name, type;
1657 {
1658   my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 136);
1659   if (classname != NULL_TREE)
1660     {
1661       char *buf;
1662       my_friendly_assert (TREE_CODE (classname) == IDENTIFIER_NODE, 137);
1663       my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 138);
1664       buf = (char *) alloca (4 + IDENTIFIER_LENGTH (classname)
1665                              + IDENTIFIER_LENGTH (name));
1666       sprintf (buf, "%s::%s", IDENTIFIER_POINTER (classname),
1667                IDENTIFIER_POINTER (name));
1668       DECL_NESTED_TYPENAME (decl) = get_identifier (buf);
1669
1670       /* This is a special usage of IDENTIFIER_TYPE_VALUE which have no
1671          correspondence in any binding_level.  This is ok since the
1672          DECL_NESTED_TYPENAME is just a convenience identifier whose
1673          IDENTIFIER_TYPE_VALUE will remain constant from now on.  */
1674       SET_IDENTIFIER_TYPE_VALUE (DECL_NESTED_TYPENAME (decl), type);
1675     }
1676   else
1677     DECL_NESTED_TYPENAME (decl) = name;
1678 }
1679
1680 /* Pop off extraneous binding levels left over due to syntax errors.  */
1681 void
1682 pop_everything ()
1683 {
1684 #ifdef DEBUG_CP_BINDING_LEVELS
1685   fprintf (stderr, "XXX entering pop_everything ()\n");
1686 #endif
1687   while (current_binding_level != global_binding_level
1688          && ! current_binding_level->pseudo_global)
1689     {
1690       if (class_binding_level)
1691         pop_nested_class (1);
1692       else
1693         poplevel (0, 0, 0);
1694     }
1695 #ifdef DEBUG_CP_BINDING_LEVELS
1696   fprintf (stderr, "XXX leaving pop_everything ()\n");
1697 #endif
1698 }
1699
1700 #if 0 /* not yet, should get fixed properly later */
1701 /* Create a TYPE_DECL node with the correct DECL_ASSEMBLER_NAME.
1702    Other routines shouldn't use build_decl directly; they'll produce
1703    incorrect results with `-g' unless they duplicate this code.
1704
1705    This is currently needed mainly for dbxout.c, but we can make
1706    use of it in method.c later as well.  */
1707 tree
1708 make_type_decl (name, type)
1709      tree name, type;
1710 {
1711   tree decl, id;
1712   decl = build_decl (TYPE_DECL, name, type);
1713   if (TYPE_NAME (type) == name)
1714     /* Class/union/enum definition, or a redundant typedef for same.  */
1715     {
1716       id = get_identifier (build_overload_name (type, 1, 1));
1717       DECL_ASSEMBLER_NAME (decl) = id;
1718     }
1719   else if (TYPE_NAME (type) != NULL_TREE)
1720     /* Explicit typedef, or implicit typedef for template expansion.  */
1721     DECL_ASSEMBLER_NAME (decl) = DECL_ASSEMBLER_NAME (TYPE_NAME (type));
1722   else
1723     {
1724       /* XXX: Typedef for unnamed struct; some other situations.
1725          TYPE_NAME is null; what's right here?  */
1726     }
1727   return decl;
1728 }
1729 #endif
1730
1731 /* Push a tag name NAME for struct/class/union/enum type TYPE.
1732    Normally put into into the inner-most non-tag-tranparent scope,
1733    but if GLOBALIZE is true, put it in the inner-most non-class scope.
1734    The latter is needed for implicit declarations. */
1735
1736 void
1737 pushtag (name, type, globalize)
1738      tree name, type;
1739      int globalize;
1740 {
1741   register struct binding_level *b;
1742   tree t_context = 0;
1743
1744   b = inner_binding_level;
1745   while (b->tag_transparent
1746          || (globalize && b->parm_flag == 2))
1747     b = b->level_chain;
1748
1749   if (b == global_binding_level)
1750     b->tags = perm_tree_cons (name, type, b->tags);
1751   else
1752     b->tags = saveable_tree_cons (name, type, b->tags);
1753
1754   if (name)
1755     {
1756       t_context = type ? TYPE_CONTEXT(type) : NULL_TREE;
1757       if (!t_context && !globalize)
1758         t_context = current_class_type;
1759
1760       /* Record the identifier as the type's name if it has none.  */
1761       if (TYPE_NAME (type) == NULL_TREE)
1762         TYPE_NAME (type) = name;
1763       
1764       /* Do C++ gratuitous typedefing.  */
1765       if (IDENTIFIER_TYPE_VALUE (name) != type
1766           && (TREE_CODE (type) != RECORD_TYPE
1767               || b->parm_flag != 2
1768               || !CLASSTYPE_DECLARED_EXCEPTION (type)))
1769         {
1770           register tree d;
1771           int newdecl = 0;
1772           
1773           if (b->parm_flag != 2
1774               || TYPE_SIZE (current_class_type) != NULL_TREE)
1775             {
1776               if (current_lang_name == lang_name_cplusplus)
1777                 d = lookup_nested_type (type, 
1778                         t_context ? TYPE_NAME (t_context) : NULL_TREE);
1779               else
1780                 d = NULL_TREE;
1781
1782               if (d == NULL_TREE)
1783                 {
1784                   newdecl = 1;
1785 #if 0 /* not yet, should get fixed properly later */
1786                   d = make_type_decl (name, type);
1787 #else
1788                   d = build_decl (TYPE_DECL, name, type);
1789 #endif
1790 #ifdef DWARF_DEBUGGING_INFO
1791                   if (write_symbols == DWARF_DEBUG)
1792                     {
1793                       /* Mark the TYPE_DECL node we created just above as an
1794                          gratuitous one.  We need to do this so that dwarfout.c
1795                          will understand that it is not supposed to output a
1796                          TAG_typedef DIE  for it. */
1797                       DECL_IGNORED_P (d) = 1;
1798                     }
1799 #endif /* DWARF_DEBUGGING_INFO */
1800                   set_identifier_type_value_with_scope (name, type, b);
1801                 }
1802               else
1803                 d = TYPE_NAME (d);
1804
1805               /* If it is anonymous, then we are called from pushdecl,
1806                  and we don't want to infinitely recurse.  Also, if the
1807                  name is already in scope, we don't want to push it
1808                  again--pushdecl is only for pushing new decls.  */
1809               if (! ANON_AGGRNAME_P (name)
1810                   && TYPE_NAME (type)
1811                   && (TREE_CODE (TYPE_NAME (type)) != TYPE_DECL
1812                       || lookup_name (name, 1) != TYPE_NAME (type)))
1813                 {
1814                   if (b->parm_flag == 2)
1815                     d = pushdecl_class_level (d);
1816                   else
1817                     d = pushdecl_with_scope (d, b);
1818                 }
1819             }
1820           else
1821             {
1822               /* Make nested declarations go into class-level scope.  */
1823               newdecl = 1;
1824               d = build_lang_field_decl (TYPE_DECL, name, type);
1825 #ifdef DWARF_DEBUGGING_INFO
1826               if (write_symbols == DWARF_DEBUG)
1827                 {
1828                   /* Mark the TYPE_DECL node we created just above as an
1829                      gratuitous one.  We need to do this so that dwarfout.c
1830                      will understand that it is not supposed to output a
1831                      TAG_typedef DIE  for it. */
1832                   DECL_IGNORED_P (d) = 1;
1833                 }
1834 #endif /* DWARF_DEBUGGING_INFO */
1835               /* Make sure we're in this type's scope when we push the
1836                  decl for a template, otherwise class_binding_level will
1837                  be NULL and we'll end up dying inside of
1838                  push_class_level_binding.  */
1839               if (TREE_CODE (type) == UNINSTANTIATED_P_TYPE)
1840                 pushclass (type, 0);
1841               d = pushdecl_class_level (d);
1842               if (TREE_CODE (type) == UNINSTANTIATED_P_TYPE)
1843                 popclass (0);
1844             }
1845           if (write_symbols != DWARF_DEBUG)
1846             {
1847               if (ANON_AGGRNAME_P (name))
1848                 DECL_IGNORED_P (d) = 1;
1849             }
1850           TYPE_NAME (type) = d;
1851
1852           if ((t_context == NULL_TREE
1853                && current_function_decl == NULL_TREE)
1854               || current_lang_name != lang_name_cplusplus)
1855             /* Non-nested class.  */
1856             DECL_NESTED_TYPENAME (d) = name;
1857           else if (current_function_decl != NULL_TREE)
1858             {
1859               /* Function-nested class.  */
1860               set_nested_typename (d, 
1861                 DECL_ASSEMBLER_NAME (current_function_decl), name, type);
1862               /* This builds the links for classes nested in fn scope.  */
1863               DECL_CONTEXT (d) = current_function_decl;
1864             }
1865 /*        else if (TYPE_SIZE (current_class_type) == NULL_TREE)
1866 */
1867           else if (t_context && TREE_CODE (t_context) == RECORD_TYPE)
1868             {
1869               /* Class-nested class.  */
1870               set_nested_typename (d, 
1871                 DECL_NESTED_TYPENAME (TYPE_NAME (t_context)), name, type);
1872               /* This builds the links for classes nested in type scope.  */
1873               DECL_CONTEXT (d) = t_context;
1874               DECL_CLASS_CONTEXT (d) = t_context;
1875             }
1876           TYPE_CONTEXT (type) = DECL_CONTEXT (d);
1877           if (newdecl)
1878             DECL_ASSEMBLER_NAME (d)
1879               = get_identifier (build_overload_name (type, 1, 1));
1880         }
1881       if (b->parm_flag == 2)
1882         {
1883           TREE_NONLOCAL_FLAG (type) = 1;
1884           if (TYPE_SIZE (current_class_type) == NULL_TREE)
1885             CLASSTYPE_TAGS (current_class_type) = b->tags;
1886         }
1887     }
1888
1889   if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL)
1890     /* Use the canonical TYPE_DECL for this node.  */
1891     TYPE_STUB_DECL (type) = TYPE_NAME (type);
1892   else
1893     {
1894       /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE
1895          will be the tagged type we just added to the current
1896          binding level.  This fake NULL-named TYPE_DECL node helps
1897          dwarfout.c to know when it needs to output a
1898          representation of a tagged type, and it also gives us a
1899          convenient place to record the "scope start" address for
1900          the tagged type.  */
1901
1902 #if 0 /* not yet, should get fixed properly later */
1903       tree d = make_type_decl (NULL_TREE, type);
1904 #else
1905       tree d = build_decl (TYPE_DECL, NULL_TREE, type);
1906 #endif
1907       TYPE_STUB_DECL (type) = pushdecl_with_scope (d, b);
1908     }
1909 }
1910
1911 /* Counter used to create anonymous type names.  */
1912 static int anon_cnt = 0;
1913
1914 /* Return an IDENTIFIER which can be used as a name for
1915    anonymous structs and unions.  */
1916 tree
1917 make_anon_name ()
1918 {
1919   char buf[32];
1920
1921   sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
1922   return get_identifier (buf);
1923 }
1924
1925 /* Clear the TREE_PURPOSE slot of tags which have anonymous typenames.
1926    This keeps dbxout from getting confused.  */
1927 void
1928 clear_anon_tags ()
1929 {
1930   register struct binding_level *b;
1931   register tree tags;
1932   static int last_cnt = 0;
1933
1934   /* Fast out if no new anon names were declared.  */
1935   if (last_cnt == anon_cnt)
1936     return;
1937
1938   b = current_binding_level;
1939   while (b->tag_transparent)
1940     b = b->level_chain;
1941   tags = b->tags;
1942   while (tags)
1943     {
1944       /* A NULL purpose means we have already processed all tags
1945          from here to the end of the list.  */
1946       if (TREE_PURPOSE (tags) == NULL_TREE)
1947         break;
1948       if (ANON_AGGRNAME_P (TREE_PURPOSE (tags)))
1949         TREE_PURPOSE (tags) = NULL_TREE;
1950       tags = TREE_CHAIN (tags);
1951     }
1952   last_cnt = anon_cnt;
1953 }
1954 \f
1955 /* Subroutine of duplicate_decls: return truthvalue of whether
1956    or not types of these decls match.
1957
1958    For C++, we must compare the parameter list so that `int' can match
1959    `int&' in a parameter position, but `int&' is not confused with
1960    `const int&'.  */
1961 static int
1962 decls_match (newdecl, olddecl)
1963      tree newdecl, olddecl;
1964 {
1965   int types_match;
1966
1967   if (TREE_CODE (newdecl) == FUNCTION_DECL && TREE_CODE (olddecl) == FUNCTION_DECL)
1968     {
1969       tree f1 = TREE_TYPE (newdecl);
1970       tree f2 = TREE_TYPE (olddecl);
1971       tree p1 = TYPE_ARG_TYPES (f1);
1972       tree p2 = TYPE_ARG_TYPES (f2);
1973
1974       /* When we parse a static member function definition,
1975          we put together a FUNCTION_DECL which thinks its type
1976          is METHOD_TYPE.  Change that to FUNCTION_TYPE, and
1977          proceed.  */
1978       if (TREE_CODE (f1) == METHOD_TYPE && DECL_STATIC_FUNCTION_P (olddecl))
1979         revert_static_member_fn (&f1, &newdecl, &p1);
1980       else if (TREE_CODE (f2) == METHOD_TYPE
1981                && DECL_STATIC_FUNCTION_P (newdecl))
1982         revert_static_member_fn (&f2, &olddecl, &p2);
1983
1984       /* Here we must take care of the case where new default
1985          parameters are specified.  Also, warn if an old
1986          declaration becomes ambiguous because default
1987          parameters may cause the two to be ambiguous.  */
1988       if (TREE_CODE (f1) != TREE_CODE (f2))
1989         {
1990           if (TREE_CODE (f1) == OFFSET_TYPE)
1991             cp_compiler_error ("`%D' redeclared as member function", newdecl);
1992           else
1993             cp_compiler_error ("`%D' redeclared as non-member function", newdecl);
1994           return 0;
1995         }
1996
1997       if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (f1)),
1998                      TYPE_MAIN_VARIANT (TREE_TYPE (f2)), 2))
1999         {
2000           if (DECL_LANGUAGE (olddecl) == lang_c
2001               && ! strict_prototypes_lang_c
2002               && p2 == NULL_TREE)
2003             types_match = self_promoting_args_p (p1);
2004           else
2005             types_match = compparms (p1, p2, 1);
2006         }
2007       else
2008         types_match = 0;
2009     }
2010   else if (TREE_CODE (newdecl) == TEMPLATE_DECL
2011            && TREE_CODE (olddecl) == TEMPLATE_DECL)
2012     {
2013         tree newargs = DECL_TEMPLATE_PARMS (newdecl);
2014         tree oldargs = DECL_TEMPLATE_PARMS (olddecl);
2015         int i, len = TREE_VEC_LENGTH (newargs);
2016
2017         if (TREE_VEC_LENGTH (oldargs) != len)
2018           return 0;
2019         
2020         for (i = 0; i < len; i++)
2021           {
2022             tree newarg = TREE_VEC_ELT (newargs, i);
2023             tree oldarg = TREE_VEC_ELT (oldargs, i);
2024             if (TREE_CODE (newarg) != TREE_CODE (oldarg))
2025               return 0;
2026             else if (TREE_CODE (newarg) == IDENTIFIER_NODE)
2027               /* continue */;
2028             else if (! comptypes (TREE_TYPE (newarg), TREE_TYPE (oldarg), 1))
2029               return 0;
2030           }
2031
2032         if (DECL_TEMPLATE_IS_CLASS (newdecl)
2033             != DECL_TEMPLATE_IS_CLASS (olddecl))
2034           types_match = 0;
2035         else if (DECL_TEMPLATE_IS_CLASS (newdecl))
2036           types_match = 1;
2037         else
2038           types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
2039                                      DECL_TEMPLATE_RESULT (newdecl));
2040     }
2041   else
2042     {
2043       if (TREE_TYPE (newdecl) == error_mark_node)
2044         types_match = TREE_TYPE (olddecl) == error_mark_node;
2045       else if (TREE_TYPE (olddecl) == NULL_TREE)
2046         types_match = TREE_TYPE (newdecl) == NULL_TREE;
2047       else
2048         types_match = comptypes (TREE_TYPE (newdecl), TREE_TYPE (olddecl), 1);
2049     }
2050
2051   return types_match;
2052 }
2053
2054 /* If NEWDECL is `static' and an `extern' was seen previously,
2055    warn about it.  (OLDDECL may be NULL_TREE; NAME contains
2056    information about previous usage as an `extern'.)
2057
2058    Note that this does not apply to the C++ case of declaring
2059    a variable `extern const' and then later `const'.
2060
2061    Don't complain if -traditional, since traditional compilers
2062    don't complain.
2063
2064    Don't complain about built-in functions, since they are beyond
2065    the user's control.  */
2066
2067 static void
2068 warn_extern_redeclared_static (newdecl, olddecl)
2069      tree newdecl, olddecl;
2070 {
2071   tree name;
2072
2073   static char *explicit_extern_static_warning
2074     = "`%D' was declared `extern' and later `static'";
2075   static char *implicit_extern_static_warning
2076     = "`%D' was declared implicitly `extern' and later `static'";
2077
2078   if (flag_traditional
2079       || TREE_CODE (newdecl) == TYPE_DECL
2080       || (! warn_extern_inline
2081           && DECL_INLINE (newdecl)))
2082     return;
2083
2084   name = DECL_ASSEMBLER_NAME (newdecl);
2085   if (TREE_PUBLIC (name) && ! TREE_PUBLIC (newdecl))
2086     {
2087       /* It's okay to redeclare an ANSI built-in function as static,
2088          or to declare a non-ANSI built-in function as anything.  */
2089       if (! (TREE_CODE (newdecl) == FUNCTION_DECL
2090              && olddecl != NULL_TREE
2091              && TREE_CODE (olddecl) == FUNCTION_DECL
2092              && (DECL_BUILT_IN (olddecl)
2093                  || DECL_BUILT_IN_NONANSI (olddecl))))
2094         {
2095           cp_warning (IDENTIFIER_IMPLICIT_DECL (name)
2096                       ? implicit_extern_static_warning
2097                       : explicit_extern_static_warning, newdecl);
2098           if (olddecl != NULL_TREE)
2099             cp_warning_at ("previous declaration of `%D'", olddecl);
2100         }
2101     }
2102 }
2103
2104 /* Handle when a new declaration NEWDECL has the same name as an old
2105    one OLDDECL in the same binding contour.  Prints an error message
2106    if appropriate.
2107
2108    If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
2109    Otherwise, return 0.  */
2110
2111 int
2112 duplicate_decls (newdecl, olddecl)
2113      register tree newdecl, olddecl;
2114 {
2115   extern struct obstack permanent_obstack;
2116   unsigned olddecl_uid = DECL_UID (olddecl);
2117   int olddecl_friend = 0, types_match = 0;
2118   int new_defines_function;
2119   tree previous_c_decl = NULL_TREE;
2120
2121   types_match = decls_match (newdecl, olddecl);
2122
2123   if (TREE_CODE (olddecl) != TREE_LIST)
2124     olddecl_friend = DECL_LANG_SPECIFIC (olddecl) && DECL_FRIEND_P (olddecl);
2125
2126   /* If either the type of the new decl or the type of the old decl is an
2127      error_mark_node, then that implies that we have already issued an
2128      error (earlier) for some bogus type specification, and in that case,
2129      it is rather pointless to harass the user with yet more error message
2130      about the same declaration, so well just pretent the types match here.  */
2131   if ((TREE_TYPE (newdecl)
2132        && TREE_CODE (TREE_TYPE (newdecl)) == ERROR_MARK)
2133       || (TREE_TYPE (olddecl)
2134           && TREE_CODE (TREE_TYPE (olddecl)) == ERROR_MARK))
2135     types_match = 1;
2136
2137   if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
2138     {
2139       if ((TREE_CODE (newdecl) == FUNCTION_DECL
2140            && TREE_CODE (olddecl) == TEMPLATE_DECL
2141            && ! DECL_TEMPLATE_IS_CLASS (olddecl))
2142           || (TREE_CODE (olddecl) == FUNCTION_DECL
2143               && TREE_CODE (newdecl) == TEMPLATE_DECL
2144               && ! DECL_TEMPLATE_IS_CLASS (newdecl)))
2145         return 0;
2146       
2147       cp_error ("`%#D' redeclared as different kind of symbol", newdecl);
2148       if (TREE_CODE (olddecl) == TREE_LIST)
2149         olddecl = TREE_VALUE (olddecl);
2150       cp_error_at ("previous declaration of `%#D'", olddecl);
2151
2152       /* New decl is completely inconsistent with the old one =>
2153          tell caller to replace the old one.  */
2154
2155       return 0;
2156     }
2157
2158   if (flag_traditional && TREE_CODE (newdecl) == FUNCTION_DECL
2159       && IDENTIFIER_IMPLICIT_DECL (DECL_ASSEMBLER_NAME (newdecl)) == olddecl)
2160     /* If -traditional, avoid error for redeclaring fcn
2161        after implicit decl.  */
2162     ;
2163   else if (TREE_CODE (olddecl) == FUNCTION_DECL
2164            && (DECL_BUILT_IN (olddecl)
2165                || DECL_BUILT_IN_NONANSI (olddecl))
2166            && DECL_ASSEMBLER_NAME (newdecl) == DECL_ASSEMBLER_NAME (olddecl))
2167     {
2168       /* If you declare a built-in or predefined function name as static,
2169          the old definition is overridden,
2170          but optionally warn this was a bad choice of name.  */
2171       if (! TREE_PUBLIC (newdecl))
2172         {
2173           if (warn_shadow)
2174             cp_warning ("shadowing %s function `%#D'",
2175                         DECL_BUILT_IN (olddecl) ? "built-in" : "library",
2176                         newdecl);
2177           /* Discard the old built-in function.  */
2178           return 0;
2179         }
2180       /* Likewise, if the built-in is not ansi, then programs can
2181          override it even globally without an error.  */
2182       else if (! DECL_BUILT_IN (olddecl))
2183         cp_warning ("library function `%#D' declared as non-function",
2184                     newdecl);
2185
2186       if (!types_match)
2187         {
2188           cp_warning ("declaration of `%#D'", newdecl);
2189           cp_warning ("conflicts with built-in declaration `%#D'",
2190                       olddecl);
2191           return 0;
2192         }
2193     }
2194   else if (!types_match)
2195     {
2196       if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2197         return 0;
2198       if (TREE_CODE (newdecl) == FUNCTION_DECL)
2199         {
2200           if (DECL_LANGUAGE (newdecl) == lang_c
2201               && DECL_LANGUAGE (olddecl) == lang_c)
2202             {
2203               cp_error ("declaration of C function `%#D' conflicts with",
2204                         newdecl);
2205               cp_error_at ("previous declaration `%#D' here", olddecl);
2206             }
2207           
2208           return 0;
2209         }
2210
2211       /* Already complained about this, so don't do so again.  */
2212       if (current_class_type == NULL_TREE
2213           || IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (newdecl)) != current_class_type)
2214         {
2215           /* Since we're doing this before finish_struct can set the
2216              line number on NEWDECL, we just do a regular error here.  */
2217           if (DECL_SOURCE_LINE (newdecl) == 0)
2218             cp_error ("conflicting types for `%#D'", newdecl);
2219           else
2220             cp_error_at ("conflicting types for `%#D'", newdecl);
2221           cp_error_at ("previous declaration as `%#D'", olddecl);
2222         }
2223     }
2224   else
2225     {
2226       char *errmsg = redeclaration_error_message (newdecl, olddecl);
2227       if (errmsg)
2228         {
2229           cp_error (errmsg, newdecl);
2230           if (DECL_NAME (olddecl) != NULL_TREE)
2231             cp_error_at ((DECL_INITIAL (olddecl)
2232                           && current_binding_level == global_binding_level)
2233                          ? "`%#D' previously defined here"
2234                          : "`%#D' previously declared here", olddecl);
2235         }
2236       else if (TREE_CODE (olddecl) == FUNCTION_DECL
2237                && DECL_INITIAL (olddecl) != NULL_TREE
2238                && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE
2239                && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE)
2240         {
2241           /* Prototype decl follows defn w/o prototype.  */
2242           cp_warning_at ("prototype for `%#D'", newdecl);
2243           cp_warning_at ("follows non-prototype definition here", olddecl);
2244         }
2245       else if (TREE_CODE (olddecl) == FUNCTION_DECL
2246                && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
2247         {
2248           /* extern "C" int foo ();
2249              int foo () { bar (); }
2250              is OK.  */
2251           if (current_lang_stack == current_lang_base)
2252             {
2253               DECL_LANGUAGE (newdecl) = DECL_LANGUAGE (olddecl);
2254               if (TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE
2255                   && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) == void_list_node)
2256                 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
2257             }
2258           else
2259             {
2260               cp_error_at ("previous declaration of `%#D' with %L linkage",
2261                            olddecl, DECL_LANGUAGE (olddecl));
2262               cp_error ("conflicts with new declaration with %L linkage",
2263                         DECL_LANGUAGE (newdecl));
2264             }
2265         }
2266
2267       /* These bits are logically part of the type.  */
2268       if (pedantic
2269           && (TREE_READONLY (newdecl) != TREE_READONLY (olddecl)
2270               || TREE_THIS_VOLATILE (newdecl) != TREE_THIS_VOLATILE (olddecl)))
2271         cp_error_at ("type qualifiers for `%D' conflict with previous decl", newdecl);
2272     }
2273
2274   /* If new decl is `static' and an `extern' was seen previously,
2275      warn about it.  */
2276   warn_extern_redeclared_static (newdecl, olddecl);
2277
2278   /* We have committed to returning 1 at this point. */
2279   if (TREE_CODE (newdecl) == FUNCTION_DECL)
2280     {
2281       /* Now that functions must hold information normally held
2282          by field decls, there is extra work to do so that
2283          declaration information does not get destroyed during
2284          definition.  */
2285       if (DECL_VINDEX (olddecl))
2286         DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
2287       if (DECL_CONTEXT (olddecl))
2288         DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2289       if (DECL_CLASS_CONTEXT (olddecl))
2290         DECL_CLASS_CONTEXT (newdecl) = DECL_CLASS_CONTEXT (olddecl);
2291       if (DECL_CHAIN (newdecl) == NULL_TREE)
2292         DECL_CHAIN (newdecl) = DECL_CHAIN (olddecl);
2293       if (DECL_NEXT_METHOD (newdecl) == NULL_TREE)
2294         DECL_NEXT_METHOD (newdecl) = DECL_NEXT_METHOD (olddecl);
2295       if (DECL_PENDING_INLINE_INFO (newdecl) == (struct pending_inline *)0)
2296         DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
2297     }
2298
2299   /* Deal with C++: must preserve virtual function table size.  */
2300   if (TREE_CODE (olddecl) == TYPE_DECL)
2301     {
2302       register tree newtype = TREE_TYPE (newdecl);
2303       register tree oldtype = TREE_TYPE (olddecl);
2304
2305       if (newtype != error_mark_node && oldtype != error_mark_node
2306           && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
2307         {
2308           CLASSTYPE_VSIZE (newtype) = CLASSTYPE_VSIZE (oldtype);
2309           CLASSTYPE_FRIEND_CLASSES (newtype)
2310             = CLASSTYPE_FRIEND_CLASSES (oldtype);
2311         }
2312       /* why assert here?  Just because debugging information is
2313          messed up? (mrs) */
2314       /* it happens on something like:
2315                 typedef struct Thing {
2316                         Thing();
2317                         int     x;
2318                 } Thing;
2319       */
2320 #if 0
2321       my_friendly_assert (DECL_IGNORED_P (olddecl) == DECL_IGNORED_P (newdecl), 139);
2322 #endif
2323     }
2324
2325   /* Special handling ensues if new decl is a function definition.  */
2326   new_defines_function = (TREE_CODE (newdecl) == FUNCTION_DECL
2327                           && DECL_INITIAL (newdecl) != NULL_TREE);
2328
2329   /* Optionally warn about more than one declaration for the same name,
2330      but don't warn about a function declaration followed by a definition.  */
2331   if (warn_redundant_decls
2332       && DECL_SOURCE_LINE (olddecl) != 0
2333       && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
2334       /* Don't warn about extern decl followed by (tentative) definition.  */
2335       && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl)))
2336     {
2337       cp_warning ("redundant redeclaration of `%D' in same scope", newdecl);
2338       cp_warning ("previous declaration of `%D'", olddecl);
2339     }
2340
2341   /* Copy all the DECL_... slots specified in the new decl
2342      except for any that we copy here from the old type.  */
2343
2344   if (types_match)
2345     {
2346       /* Automatically handles default parameters.  */
2347       tree oldtype = TREE_TYPE (olddecl);
2348       /* Merge the data types specified in the two decls.  */
2349       tree newtype = common_type (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
2350
2351       if (TREE_CODE (newdecl) == VAR_DECL)
2352         DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
2353       /* Do this after calling `common_type' so that default
2354          parameters don't confuse us.  */
2355       else if (TREE_CODE (newdecl) == FUNCTION_DECL
2356           && (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl))
2357               != TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl))))
2358         {
2359           tree ctype = NULL_TREE;
2360           ctype = DECL_CLASS_CONTEXT (newdecl);
2361           TREE_TYPE (newdecl) = build_exception_variant (ctype, newtype,
2362                                                          TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)));
2363           TREE_TYPE (olddecl) = build_exception_variant (ctype, newtype,
2364                                                          TYPE_RAISES_EXCEPTIONS (oldtype));
2365
2366           if (! compexcepttypes (TREE_TYPE (newdecl), TREE_TYPE(olddecl), 0))
2367             {
2368               cp_error ("declaration of `%D' raises different exceptions...", newdecl);
2369               cp_error_at ("...from previous declaration here", olddecl);
2370             }
2371         }
2372       TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
2373
2374       /* Lay the type out, unless already done.  */
2375       if (oldtype != TREE_TYPE (newdecl))
2376         {
2377           if (TREE_TYPE (newdecl) != error_mark_node)
2378             layout_type (TREE_TYPE (newdecl));
2379           if (TREE_CODE (newdecl) != FUNCTION_DECL
2380               && TREE_CODE (newdecl) != TYPE_DECL
2381               && TREE_CODE (newdecl) != CONST_DECL
2382               && TREE_CODE (newdecl) != TEMPLATE_DECL)
2383             layout_decl (newdecl, 0);
2384         }
2385       else
2386         {
2387           /* Since the type is OLDDECL's, make OLDDECL's size go with.  */
2388           DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
2389         }
2390
2391       /* Merge the type qualifiers.  */
2392       if (TREE_READONLY (newdecl))
2393         TREE_READONLY (olddecl) = 1;
2394       if (TREE_THIS_VOLATILE (newdecl))
2395         TREE_THIS_VOLATILE (olddecl) = 1;
2396
2397       /* Merge the initialization information.  */
2398       if (DECL_INITIAL (newdecl) == NULL_TREE
2399           && DECL_INITIAL (olddecl) != NULL_TREE)
2400         {
2401           DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2402           DECL_SOURCE_FILE (newdecl) = DECL_SOURCE_FILE (olddecl);
2403           DECL_SOURCE_LINE (newdecl) = DECL_SOURCE_LINE (olddecl);
2404         }
2405       /* Keep the old rtl since we can safely use it, unless it's the
2406          call to abort() used for abstract virtuals.  */
2407       if ((DECL_LANG_SPECIFIC (olddecl)
2408            && !DECL_ABSTRACT_VIRTUAL_P (olddecl))
2409           || DECL_RTL (olddecl) != DECL_RTL (abort_fndecl))
2410         DECL_RTL (newdecl) = DECL_RTL (olddecl);
2411     }
2412   /* If cannot merge, then use the new type and qualifiers,
2413      and don't preserve the old rtl.  */
2414   else
2415     {
2416       /* Clean out any memory we had of the old declaration.  */
2417       tree oldstatic = value_member (olddecl, static_aggregates);
2418       if (oldstatic)
2419         TREE_VALUE (oldstatic) = error_mark_node;
2420
2421       TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
2422       TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2423       TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2424       TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
2425     }
2426
2427   /* Merge the storage class information.  */
2428   if (DECL_EXTERNAL (newdecl))
2429     {
2430       TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
2431       DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
2432
2433       /* For functions, static overrides non-static.  */
2434       if (TREE_CODE (newdecl) == FUNCTION_DECL)
2435         {
2436           TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
2437           /* This is since we don't automatically
2438              copy the attributes of NEWDECL into OLDDECL.  */
2439           TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2440           /* If this clears `static', clear it in the identifier too.  */
2441           if (! TREE_PUBLIC (olddecl))
2442             TREE_PUBLIC (DECL_ASSEMBLER_NAME (olddecl)) = 0;
2443         }
2444       else
2445         TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2446     }
2447   else
2448     {
2449       TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
2450       /* A `const' which was not declared `extern' and is
2451          in static storage is invisible.  */
2452       if (TREE_CODE (newdecl) == VAR_DECL
2453           && TREE_READONLY (newdecl) && TREE_STATIC (newdecl)
2454           && ! DECL_THIS_EXTERN (newdecl))
2455         TREE_PUBLIC (newdecl) = 0;
2456       TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2457     }
2458
2459   /* If either decl says `inline', this fn is inline,
2460      unless its definition was passed already.  */
2461   if (DECL_INLINE (newdecl) && DECL_INITIAL (olddecl) == NULL_TREE)
2462     DECL_INLINE (olddecl) = 1;
2463   DECL_INLINE (newdecl) = DECL_INLINE (olddecl);
2464
2465   if (TREE_CODE (newdecl) == FUNCTION_DECL)
2466     {
2467       if (new_defines_function)
2468         /* If defining a function declared with other language
2469            linkage, use the previously declared language linkage.  */
2470         DECL_LANGUAGE (newdecl) = DECL_LANGUAGE (olddecl);
2471       else
2472         {
2473           /* If redeclaring a builtin function, and not a definition,
2474              it stays built in.  */
2475           if (DECL_BUILT_IN (olddecl))
2476             {
2477               DECL_BUILT_IN (newdecl) = 1;
2478               DECL_SET_FUNCTION_CODE (newdecl, DECL_FUNCTION_CODE (olddecl));
2479               /* If we're keeping the built-in definition, keep the rtl,
2480                  regardless of declaration matches.  */
2481               DECL_RTL (newdecl) = DECL_RTL (olddecl);
2482             }
2483           else
2484             DECL_FRAME_SIZE (newdecl) = DECL_FRAME_SIZE (olddecl);
2485
2486           DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2487           if ((DECL_SAVED_INSNS (newdecl) = DECL_SAVED_INSNS (olddecl)))
2488             /* Previously saved insns go together with
2489                the function's previous definition.  */
2490             DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2491           /* Don't clear out the arguments if we're redefining a function.  */
2492           if (DECL_ARGUMENTS (olddecl))
2493             DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
2494         }
2495     }
2496
2497   if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2498     {
2499       if (DECL_TEMPLATE_INFO (olddecl)->length)
2500         DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
2501       DECL_TEMPLATE_MEMBERS (newdecl) = DECL_TEMPLATE_MEMBERS (olddecl);
2502       DECL_TEMPLATE_INSTANTIATIONS (newdecl)
2503         = DECL_TEMPLATE_INSTANTIATIONS (olddecl);
2504     }
2505   
2506   /* Now preserve various other info from the definition.  */
2507   TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
2508   TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
2509   DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2510
2511   /* Don't really know how much of the language-specific
2512      values we should copy from old to new.  */
2513   if (DECL_LANG_SPECIFIC (olddecl))
2514     {
2515       DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
2516       DECL_ACCESS (newdecl) = DECL_ACCESS (olddecl);
2517     }
2518
2519   if (TREE_CODE (newdecl) == FUNCTION_DECL)
2520     {
2521       int function_size;
2522       struct lang_decl *ol = DECL_LANG_SPECIFIC (olddecl);
2523       struct lang_decl *nl = DECL_LANG_SPECIFIC (newdecl);
2524
2525       function_size = sizeof (struct tree_decl);
2526
2527       bcopy ((char *) newdecl + sizeof (struct tree_common),
2528              (char *) olddecl + sizeof (struct tree_common),
2529              function_size - sizeof (struct tree_common));
2530
2531       /* Can we safely free the storage used by newdecl?  */
2532
2533 #define ROUND(x) ((x + obstack_alignment_mask (&permanent_obstack)) \
2534                   & ~ obstack_alignment_mask (&permanent_obstack))
2535
2536       if ((char *)newdecl + ROUND (function_size)
2537           + ROUND (sizeof (struct lang_decl))
2538           == obstack_next_free (&permanent_obstack))
2539         {
2540           DECL_MAIN_VARIANT (newdecl) = olddecl;
2541           DECL_LANG_SPECIFIC (olddecl) = ol;
2542           bcopy ((char *)nl, (char *)ol, sizeof (struct lang_decl));
2543
2544           obstack_free (&permanent_obstack, newdecl);
2545         }
2546       else if (LANG_DECL_PERMANENT (ol))
2547         {
2548           if (DECL_MAIN_VARIANT (olddecl) == olddecl)
2549             {
2550               /* Save these lang_decls that would otherwise be lost.  */
2551               extern tree free_lang_decl_chain;
2552               tree free_lang_decl = (tree) ol;
2553               TREE_CHAIN (free_lang_decl) = free_lang_decl_chain;
2554               free_lang_decl_chain = free_lang_decl;
2555             }
2556           else
2557             {
2558               /* Storage leak.  */
2559             }
2560         }
2561     }
2562   else
2563     {
2564       bcopy ((char *) newdecl + sizeof (struct tree_common),
2565              (char *) olddecl + sizeof (struct tree_common),
2566              sizeof (struct tree_decl) - sizeof (struct tree_common)
2567              + tree_code_length [(int)TREE_CODE (newdecl)] * sizeof (char *));
2568     }
2569
2570   DECL_UID (olddecl) = olddecl_uid;
2571   if (olddecl_friend)
2572     DECL_FRIEND_P (olddecl) = 1;
2573
2574   return 1;
2575 }
2576
2577 /* Record a decl-node X as belonging to the current lexical scope.
2578    Check for errors (such as an incompatible declaration for the same
2579    name already seen in the same scope).
2580
2581    Returns either X or an old decl for the same name.
2582    If an old decl is returned, it may have been smashed
2583    to agree with what X says.  */
2584
2585 tree
2586 pushdecl (x)
2587      tree x;
2588 {
2589   register tree t;
2590 #if 0 /* not yet, should get fixed properly later */
2591   register tree name;
2592 #else
2593   register tree name = DECL_ASSEMBLER_NAME (x);
2594 #endif
2595   register struct binding_level *b = current_binding_level;
2596
2597 #if 0
2598   static int nglobals; int len;
2599
2600   len = list_length (global_binding_level->names);
2601   if (len < nglobals)
2602     my_friendly_abort (8);
2603   else if (len > nglobals)
2604     nglobals = len;
2605 #endif
2606
2607   /* Don't change DECL_CONTEXT of virtual methods.  */
2608   if (x != current_function_decl
2609       && (TREE_CODE (x) != FUNCTION_DECL
2610           || !DECL_VIRTUAL_P (x))
2611       && ! DECL_CONTEXT (x))
2612     DECL_CONTEXT (x) = current_function_decl;
2613   /* A local declaration for a function doesn't constitute nesting.  */
2614   if (TREE_CODE (x) == FUNCTION_DECL && DECL_INITIAL (x) == 0)
2615     DECL_CONTEXT (x) = 0;
2616
2617 #if 0 /* not yet, should get fixed properly later */
2618   /* For functions and class static data, we currently look up the encoded
2619      form of the name.  For types, we want the real name.  The former will
2620      probably be changed soon, according to MDT.  */
2621   if (TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
2622     name = DECL_ASSEMBLER_NAME (x);
2623   else
2624     name = DECL_NAME (x);
2625 #else
2626   /* Type are looked up using the DECL_NAME, as that is what the rest of the
2627      compiler wants to use. */
2628   if (TREE_CODE (x) == TYPE_DECL)
2629     name = DECL_NAME (x);
2630 #endif
2631
2632   if (name)
2633     {
2634       char *file;
2635       int line;
2636
2637       t = lookup_name_current_level (name);
2638       if (t == error_mark_node)
2639         {
2640           /* error_mark_node is 0 for a while during initialization!  */
2641           t = NULL_TREE;
2642           cp_error_at ("`%#D' used prior to declaration", x);
2643         }
2644
2645       else if (t != NULL_TREE)
2646         {
2647           if (TREE_CODE (t) == PARM_DECL)
2648             {
2649               if (DECL_CONTEXT (t) == NULL_TREE)
2650                 fatal ("parse errors have confused me too much");
2651             }
2652           file = DECL_SOURCE_FILE (t);
2653           line = DECL_SOURCE_LINE (t);
2654
2655           if (TREE_CODE (x) == FUNCTION_DECL && DECL_LANGUAGE (x) == lang_c
2656               && is_overloaded_fn (t))
2657             /* don't do anything just yet */;
2658           else if (TREE_CODE (t) != TREE_CODE (x))
2659             {
2660               if (TREE_CODE (t) == TYPE_DECL || TREE_CODE (x) == TYPE_DECL)
2661                 {
2662                   /* We do nothing special here, because C++ does such nasty
2663                      things with TYPE_DECLs.  Instead, just let the TYPE_DECL
2664                      get shadowed, and know that if we need to find a TYPE_DECL
2665                      for a given name, we can look in the IDENTIFIER_TYPE_VALUE
2666                      slot of the identifier.  */
2667                   ;
2668                 }
2669               else if (duplicate_decls (x, t))
2670                 return t;
2671             }
2672           else if (duplicate_decls (x, t))
2673             {
2674 #if 0
2675               /* This is turned off until I have time to do it right (bpk).  */
2676
2677               /* Also warn if they did a prototype with `static' on it, but
2678                  then later left the `static' off.  */
2679               if (! TREE_PUBLIC (name) && TREE_PUBLIC (x))
2680                 {
2681                   if (DECL_LANG_SPECIFIC (t) && DECL_FRIEND_P (t))
2682                     return t;
2683
2684                   if (extra_warnings)
2685                     {
2686                       cp_warning ("`static' missing from declaration of `%D'", t);
2687                       warning_with_file_and_line (file, line,
2688                                                   "previous declaration of `%s'",
2689                                                   decl_as_string (t, 0));
2690                     }
2691
2692                   /* Now fix things so it'll do what they expect.  */
2693                   if (current_function_decl)
2694                     TREE_PUBLIC (current_function_decl) = 0;
2695                 }
2696               /* Due to interference in memory reclamation (X may be
2697                  obstack-deallocated at this point), we must guard against
2698                  one really special case.  [jason: This should be handled
2699                  by start_function]  */
2700               if (current_function_decl == x)
2701                 current_function_decl = t;
2702 #endif
2703               
2704               return t;
2705             }
2706         }
2707
2708       if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_FUNCTION_MEMBER_P (x))
2709         {
2710           t = push_overloaded_decl (x, 1);
2711           if (t != x || DECL_LANGUAGE (x) == lang_c)
2712             return t;
2713         }
2714       else if (TREE_CODE (x) == TEMPLATE_DECL && ! DECL_TEMPLATE_IS_CLASS (x))
2715         return push_overloaded_decl (x, 0);
2716
2717       /* If declaring a type as a typedef, and the type has no known
2718          typedef name, install this TYPE_DECL as its typedef name.  */
2719       if (TREE_CODE (x) == TYPE_DECL)
2720         {
2721           tree type = TREE_TYPE (x);
2722           tree name = (type != error_mark_node) ? TYPE_NAME (type) : x;
2723
2724           if (name == NULL_TREE || TREE_CODE (name) != TYPE_DECL)
2725             {
2726               /* If these are different names, and we're at the global
2727                  binding level, make two equivalent definitions.  */
2728               name = x;
2729               if (global_bindings_p ())
2730                 TYPE_NAME (type) = x;
2731             }
2732           else
2733             {
2734               tree tname = DECL_NAME (name);
2735
2736               if (global_bindings_p () && ANON_AGGRNAME_P (tname))
2737                 {
2738                   /* do gratuitous C++ typedefing, and make sure that
2739                      we access this type either through TREE_TYPE field
2740                      or via the tags list.  */
2741                   TYPE_NAME (TREE_TYPE (x)) = x;
2742                   pushtag (tname, TREE_TYPE (x), 0);
2743                 }
2744             }
2745           my_friendly_assert (TREE_CODE (name) == TYPE_DECL, 140);
2746
2747           if (DECL_NAME (name) && !DECL_NESTED_TYPENAME (name))
2748             set_nested_typename (x, current_class_name,
2749                                  DECL_NAME (name), type);
2750
2751           if (type != error_mark_node
2752               && TYPE_NAME (type)
2753               && TYPE_IDENTIFIER (type))
2754             set_identifier_type_value_with_scope (DECL_NAME (x), type, b);
2755         }
2756
2757       /* Multiple external decls of the same identifier ought to match.
2758
2759          We get warnings about inline functions where they are defined.
2760          Avoid duplicate warnings where they are used.  */
2761       if (TREE_PUBLIC (x) && !DECL_INLINE (x))
2762         {
2763           tree decl;
2764
2765           if (IDENTIFIER_GLOBAL_VALUE (name) != NULL_TREE
2766               && (DECL_EXTERNAL (IDENTIFIER_GLOBAL_VALUE (name))
2767                   || TREE_PUBLIC (IDENTIFIER_GLOBAL_VALUE (name))))
2768             decl = IDENTIFIER_GLOBAL_VALUE (name);
2769           else
2770             decl = NULL_TREE;
2771
2772           if (decl && ! comptypes (TREE_TYPE (x), TREE_TYPE (decl), 1)
2773               /* If different sort of thing, we already gave an error.  */
2774               && TREE_CODE (decl) == TREE_CODE (x)
2775               /* If old decl is built-in, we already warned if we should.  */
2776               && !DECL_BUILT_IN (decl))
2777             {
2778               cp_pedwarn ("type mismatch with previous external decl", x);
2779               cp_pedwarn_at ("previous external decl of `%#D'", decl);
2780             }
2781         }
2782
2783       /* In PCC-compatibility mode, extern decls of vars with no current decl
2784          take effect at top level no matter where they are.  */
2785       if (flag_traditional && DECL_EXTERNAL (x)
2786           && lookup_name (name, 0) == NULL_TREE)
2787         b = global_binding_level;
2788
2789       /* This name is new in its binding level.
2790          Install the new declaration and return it.  */
2791       if (b == global_binding_level)
2792         {
2793           /* Install a global value.  */
2794
2795           /* Rule for VAR_DECLs, but not for other kinds of _DECLs:
2796              A `const' which was not declared `extern' is invisible.  */
2797           if (TREE_CODE (x) == VAR_DECL
2798               && TREE_READONLY (x) && ! DECL_THIS_EXTERN (x))
2799             TREE_PUBLIC (x) = 0;
2800
2801           /* If the first global decl has external linkage,
2802              warn if we later see static one.  */
2803           if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
2804             TREE_PUBLIC (name) = 1;
2805
2806           /* Don't install a TYPE_DECL if we already have another
2807              sort of _DECL with that name.  */
2808           if (TREE_CODE (x) != TYPE_DECL
2809               || t == NULL_TREE
2810               || TREE_CODE (t) == TYPE_DECL)
2811             IDENTIFIER_GLOBAL_VALUE (name) = x;
2812
2813           /* Don't forget if the function was used via an implicit decl.  */
2814           if (IDENTIFIER_IMPLICIT_DECL (name)
2815               && TREE_USED (IDENTIFIER_IMPLICIT_DECL (name)))
2816             TREE_USED (x) = 1;
2817
2818           /* Don't forget if its address was taken in that way.  */
2819           if (IDENTIFIER_IMPLICIT_DECL (name)
2820               && TREE_ADDRESSABLE (IDENTIFIER_IMPLICIT_DECL (name)))
2821             TREE_ADDRESSABLE (x) = 1;
2822
2823           /* Warn about mismatches against previous implicit decl.  */
2824           if (IDENTIFIER_IMPLICIT_DECL (name) != NULL_TREE
2825               /* If this real decl matches the implicit, don't complain.  */
2826               && ! (TREE_CODE (x) == FUNCTION_DECL
2827                     && TREE_TYPE (TREE_TYPE (x)) == integer_type_node))
2828             cp_warning
2829               ("`%D' was previously implicitly declared to return `int'", x);
2830
2831           /* If new decl is `static' and an `extern' was seen previously,
2832              warn about it.  */
2833           warn_extern_redeclared_static (x, t);
2834         }
2835       else
2836         {
2837           /* Here to install a non-global value.  */
2838           tree oldlocal = IDENTIFIER_LOCAL_VALUE (name);
2839           tree oldglobal = IDENTIFIER_GLOBAL_VALUE (name);
2840
2841           b->shadowed = tree_cons (name, oldlocal, b->shadowed);
2842           IDENTIFIER_LOCAL_VALUE (name) = x;
2843
2844           /* If this is a TYPE_DECL, push it into the type value slot.  */
2845           if (TREE_CODE (x) == TYPE_DECL)
2846             set_identifier_type_value_with_scope (name, TREE_TYPE (x), b);
2847
2848           /* If this is an extern function declaration, see if we
2849              have a global definition or declaration for the function.  */
2850           if (oldlocal == NULL_TREE
2851               && DECL_EXTERNAL (x) && !DECL_INLINE (x)
2852               && oldglobal != NULL_TREE
2853               && TREE_CODE (x) == FUNCTION_DECL
2854               && TREE_CODE (oldglobal) == FUNCTION_DECL)
2855             {
2856               /* We have one.  Their types must agree.  */
2857               if (! comptypes (TREE_TYPE (x), TREE_TYPE (oldglobal), 1))
2858                 {
2859                   cp_warning ("extern declaration of `%#D' doesn't match", x);
2860                   cp_warning_at ("global declaration `%#D'", oldglobal);
2861                 }
2862               else
2863                 {
2864                   /* Inner extern decl is inline if global one is.
2865                      Copy enough to really inline it.  */
2866                   if (DECL_INLINE (oldglobal))
2867                     {
2868                       DECL_INLINE (x) = DECL_INLINE (oldglobal);
2869                       DECL_INITIAL (x) = (current_function_decl == oldglobal
2870                                           ? NULL_TREE : DECL_INITIAL (oldglobal));
2871                       DECL_SAVED_INSNS (x) = DECL_SAVED_INSNS (oldglobal);
2872                       DECL_FRAME_SIZE (x) = DECL_FRAME_SIZE (oldglobal);
2873                       DECL_ARGUMENTS (x) = DECL_ARGUMENTS (oldglobal);
2874                       DECL_RESULT (x) = DECL_RESULT (oldglobal);
2875                       TREE_ASM_WRITTEN (x) = TREE_ASM_WRITTEN (oldglobal);
2876                       DECL_ABSTRACT_ORIGIN (x) = oldglobal;
2877                     }
2878                   /* Inner extern decl is built-in if global one is.  */
2879                   if (DECL_BUILT_IN (oldglobal))
2880                     {
2881                       DECL_BUILT_IN (x) = DECL_BUILT_IN (oldglobal);
2882                       DECL_SET_FUNCTION_CODE (x, DECL_FUNCTION_CODE (oldglobal));
2883                     }
2884                   /* Keep the arg types from a file-scope fcn defn.  */
2885                   if (TYPE_ARG_TYPES (TREE_TYPE (oldglobal)) != NULL_TREE
2886                       && DECL_INITIAL (oldglobal)
2887                       && TYPE_ARG_TYPES (TREE_TYPE (x)) == NULL_TREE)
2888                     TREE_TYPE (x) = TREE_TYPE (oldglobal);
2889                 }
2890             }
2891           /* If we have a local external declaration,
2892              and no file-scope declaration has yet been seen,
2893              then if we later have a file-scope decl it must not be static.  */
2894           if (oldlocal == NULL_TREE
2895               && oldglobal == NULL_TREE
2896               && DECL_EXTERNAL (x)
2897               && TREE_PUBLIC (x))
2898             {
2899               TREE_PUBLIC (name) = 1;
2900             }
2901
2902           if (DECL_FROM_INLINE (x))
2903             /* Inline decls shadow nothing.  */;
2904
2905           /* Warn if shadowing an argument at the top level of the body.  */
2906           else if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
2907               && TREE_CODE (oldlocal) == PARM_DECL
2908               && TREE_CODE (x) != PARM_DECL)
2909             {
2910               /* Go to where the parms should be and see if we
2911                  find them there.  */
2912               struct binding_level *b = current_binding_level->level_chain;
2913
2914               if (cleanup_label)
2915                 b = b->level_chain;
2916
2917               /* ARM $8.3 */
2918               if (b->parm_flag == 1)
2919                 cp_error ("declaration of `%#D' shadows a parameter", name);
2920             }
2921           /* Maybe warn if shadowing something else.  */
2922           else if (warn_shadow && !DECL_EXTERNAL (x)
2923                    /* No shadow warnings for internally generated vars.  */
2924                    && DECL_SOURCE_LINE (x) != 0
2925                    /* No shadow warnings for vars made for inlining.  */
2926                    && ! DECL_FROM_INLINE (x))
2927             {
2928               char *warnstring = NULL;
2929
2930               if (oldlocal != NULL_TREE && TREE_CODE (oldlocal) == PARM_DECL)
2931                 warnstring = "declaration of `%s' shadows a parameter";
2932               else if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE
2933                        && !TREE_STATIC (name))
2934                 warnstring = "declaration of `%s' shadows a member of `this'";
2935               else if (oldlocal != NULL_TREE)
2936                 warnstring = "declaration of `%s' shadows previous local";
2937               else if (oldglobal != NULL_TREE)
2938                 warnstring = "declaration of `%s' shadows global declaration";
2939
2940               if (warnstring)
2941                 warning (warnstring, IDENTIFIER_POINTER (name));
2942             }
2943
2944           /* If storing a local value, there may already be one (inherited).
2945              If so, record it for restoration when this binding level ends.  */
2946           if (oldlocal != NULL_TREE)
2947             b->shadowed = tree_cons (name, oldlocal, b->shadowed);
2948         }
2949
2950       /* Keep count of variables in this level with incomplete type.  */
2951       if (TREE_CODE (x) != TEMPLATE_DECL
2952           && TREE_CODE (x) != CPLUS_CATCH_DECL
2953           && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
2954           && PROMOTES_TO_AGGR_TYPE (TREE_TYPE (x), ARRAY_TYPE))
2955         {
2956           if (++b->n_incomplete == 0)
2957             error ("too many incomplete variables at this point");
2958         }
2959     }
2960
2961   if (TREE_CODE (x) == TYPE_DECL && name != NULL_TREE)
2962     {
2963       if (current_class_name)
2964         {
2965           if (!DECL_NESTED_TYPENAME (x))
2966             set_nested_typename (x, current_class_name, DECL_NAME (x),
2967                                  TREE_TYPE (x));
2968         }
2969     }
2970
2971   /* Put decls on list in reverse order.
2972      We will reverse them later if necessary.  */
2973   TREE_CHAIN (x) = b->names;
2974   b->names = x;
2975   if (! (b != global_binding_level || TREE_PERMANENT (x)))
2976     my_friendly_abort (124);
2977
2978   return x;
2979 }
2980
2981 /* Same as pushdecl, but define X in binding-level LEVEL. */
2982
2983 static tree
2984 pushdecl_with_scope (x, level)
2985      tree x;
2986      struct binding_level *level;
2987 {
2988   register struct binding_level *b = current_binding_level;
2989
2990   current_binding_level = level;
2991   x = pushdecl (x);
2992   current_binding_level = b;
2993   return x;
2994 }
2995
2996 /* Like pushdecl, only it places X in GLOBAL_BINDING_LEVEL,
2997    if appropriate.  */
2998 tree
2999 pushdecl_top_level (x)
3000      tree x;
3001 {
3002   register struct binding_level *b = inner_binding_level;
3003   register tree t = pushdecl_with_scope (x, global_binding_level);
3004
3005   /* Now, the type_shadowed stack may screw us.  Munge it so it does
3006      what we want.  */
3007   if (TREE_CODE (x) == TYPE_DECL)
3008     {
3009       tree name = DECL_NAME (x);
3010       tree newval;
3011       tree *ptr = (tree *)0;
3012       for (; b != global_binding_level; b = b->level_chain)
3013         {
3014           tree shadowed = b->type_shadowed;
3015           for (; shadowed; shadowed = TREE_CHAIN (shadowed))
3016             if (TREE_PURPOSE (shadowed) == name)
3017               {
3018                 ptr = &TREE_VALUE (shadowed);
3019                 /* Can't break out of the loop here because sometimes
3020                    a binding level will have duplicate bindings for
3021                    PT names.  It's gross, but I haven't time to fix it.  */
3022               }
3023         }
3024       newval = TREE_TYPE (x);
3025       if (ptr == (tree *)0)
3026         {
3027           /* @@ This shouldn't be needed.  My test case "zstring.cc" trips
3028              up here if this is changed to an assertion.  --KR  */
3029           SET_IDENTIFIER_TYPE_VALUE (name, newval);
3030         }
3031       else
3032         {
3033 #if 0
3034           /* Disabled this 11/10/92, since there are many cases which
3035              behave just fine when *ptr doesn't satisfy either of these.
3036              For example, nested classes declared as friends of their enclosing
3037              class will not meet this criteria.  (bpk) */
3038           my_friendly_assert (*ptr == NULL_TREE || *ptr == newval, 141);
3039 #endif
3040           *ptr = newval;
3041         }
3042     }
3043   return t;
3044 }
3045
3046 /* Like push_overloaded_decl, only it places X in GLOBAL_BINDING_LEVEL,
3047    if appropriate.  */
3048 void
3049 push_overloaded_decl_top_level (x, forget)
3050      tree x;
3051      int forget;
3052 {
3053   struct binding_level *b = current_binding_level;
3054
3055   current_binding_level = global_binding_level;
3056   push_overloaded_decl (x, forget);
3057   current_binding_level = b;
3058 }
3059
3060 /* Make the declaration of X appear in CLASS scope.  */
3061 tree
3062 pushdecl_class_level (x)
3063      tree x;
3064 {
3065   /* Don't use DECL_ASSEMBLER_NAME here!  Everything that looks in class
3066      scope looks for the pre-mangled name.  */
3067   register tree name = DECL_NAME (x);
3068
3069   if (name)
3070     {
3071       push_class_level_binding (name, x);
3072       if (TREE_CODE (x) == TYPE_DECL)
3073         {
3074           set_identifier_type_value (name, TREE_TYPE (x));
3075           if (!DECL_NESTED_TYPENAME (x))
3076             set_nested_typename (x, current_class_name, name, TREE_TYPE (x));
3077         }
3078     }
3079   return x;
3080 }
3081
3082 /* Make the declaration(s) of X appear in CLASS scope
3083    under the name NAME.  */
3084 void
3085 push_class_level_binding (name, x)
3086      tree name;
3087      tree x;
3088 {
3089   maybe_push_cache_obstack ();
3090   class_binding_level->class_shadowed
3091       = tree_cons (name, IDENTIFIER_CLASS_VALUE (name),
3092                    class_binding_level->class_shadowed);
3093   pop_obstacks ();
3094   IDENTIFIER_CLASS_VALUE (name) = x;
3095   obstack_ptr_grow (&decl_obstack, x);
3096 }
3097
3098 /* Tell caller how to interpret a TREE_LIST which contains
3099    chains of FUNCTION_DECLS.  */
3100 int
3101 overloaded_globals_p (list)
3102      tree list;
3103 {
3104   my_friendly_assert (TREE_CODE (list) == TREE_LIST, 142);
3105
3106   /* Don't commit caller to seeing them as globals.  */
3107   if (TREE_NONLOCAL_FLAG (list))
3108     return -1;
3109   /* Do commit caller to seeing them as globals.  */
3110   if (TREE_CODE (TREE_PURPOSE (list)) == IDENTIFIER_NODE)
3111     return 1;
3112   /* Do commit caller to not seeing them as globals.  */
3113   return 0;
3114 }
3115
3116 /* DECL is a FUNCTION_DECL which may have other definitions already in place.
3117    We get around this by making IDENTIFIER_GLOBAL_VALUE (DECL_NAME (DECL))
3118    point to a list of all the things that want to be referenced by that name.
3119    It is then up to the users of that name to decide what to do with that
3120    list.
3121
3122    DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its DECL_RESULT
3123    slot.  It is dealt with the same way.
3124
3125    The value returned may be a previous declaration if we guessed wrong
3126    about what language DECL should belong to (C or C++).  Otherwise,
3127    it's always DECL (and never something that's not a _DECL).  */
3128 tree
3129 push_overloaded_decl (decl, forgettable)
3130      tree decl;
3131      int forgettable;
3132 {
3133   tree orig_name = DECL_NAME (decl);
3134   tree glob = IDENTIFIER_GLOBAL_VALUE (orig_name);
3135
3136   if (forgettable
3137       && ! flag_traditional
3138       && (glob == NULL_TREE || TREE_PERMANENT (glob) == 1)
3139       && !global_bindings_p ()
3140       && !pseudo_global_level_p ())
3141     overloads_to_forget = tree_cons (orig_name, glob, overloads_to_forget);
3142
3143   if (glob)
3144     {
3145       /* We cache the value of builtin functions as ADDR_EXPRs
3146          in the name space.  Convert it to some kind of _DECL after
3147          remembering what to forget.  */
3148       if (TREE_CODE (glob) == ADDR_EXPR)
3149         glob = TREE_OPERAND (glob, 0);
3150
3151       else if (TREE_CODE (decl) == TEMPLATE_DECL)
3152         {
3153           tree tmp;
3154
3155           for (tmp = get_first_fn (glob); tmp; tmp = DECL_CHAIN (tmp))
3156             if (decl == tmp || duplicate_decls (decl, tmp))
3157               return decl;
3158         }
3159       else if (TREE_CODE (glob) == VAR_DECL)
3160         {
3161           cp_error_at ("previous non-function declaration `%#D'", glob);
3162           cp_error ("conflicts with function declaration `%#D'", decl);
3163           return error_mark_node;
3164         }
3165       else if (TREE_CODE (glob) == TYPE_DECL)
3166         {
3167           tree t = TREE_TYPE (glob);
3168           if (IS_AGGR_TYPE (t) && warn_shadow)
3169             cp_warning ("`%#D' hides constructor for `%#T'", decl, t);
3170         }
3171       else if (is_overloaded_fn (glob))
3172         {
3173           tree tmp;
3174           
3175           for (tmp = get_first_fn (glob); tmp; tmp = DECL_CHAIN (tmp))
3176             {
3177               if (decl == tmp || duplicate_decls (decl, tmp))
3178                 return tmp;
3179               if (compparms (TYPE_ARG_TYPES (TREE_TYPE (decl)),
3180                              TYPE_ARG_TYPES (TREE_TYPE (tmp)), 2))
3181                 {
3182                   cp_error ("new declaration `%#D'", decl);
3183                   cp_error_at ("ambiguates old declaration `%#D'", tmp);
3184                 }
3185             }
3186         }
3187     }
3188   if (glob || TREE_CODE (decl) == TEMPLATE_DECL)
3189     {
3190       if (glob && is_overloaded_fn (glob))
3191         DECL_CHAIN (decl) = get_first_fn (glob);
3192       else
3193         DECL_CHAIN (decl) = NULL_TREE;
3194       glob = tree_cons (orig_name, decl, NULL_TREE);
3195       TREE_TYPE (glob) = unknown_type_node;
3196     }
3197   else
3198     /* orig_name is not ambiguous.  */
3199     glob = decl;
3200   
3201   IDENTIFIER_GLOBAL_VALUE (orig_name) = glob;
3202   return decl;
3203 }
3204 \f
3205 /* Generate an implicit declaration for identifier FUNCTIONID
3206    as a function of type int ().  Print a warning if appropriate.  */
3207
3208 tree
3209 implicitly_declare (functionid)
3210      tree functionid;
3211 {
3212   register tree decl;
3213   int temp = allocation_temporary_p ();
3214
3215   push_obstacks_nochange ();
3216
3217   /* Save the decl permanently so we can warn if definition follows.
3218      In ANSI C, warn_implicit is usually false, so the saves little space.
3219      But in C++, it's usually true, hence the extra code.  */
3220   if (temp && (flag_traditional || !warn_implicit
3221                || current_binding_level == global_binding_level))
3222     end_temporary_allocation ();
3223
3224   /* We used to reuse an old implicit decl here,
3225      but this loses with inline functions because it can clobber
3226      the saved decl chains.  */
3227   decl = build_lang_decl (FUNCTION_DECL, functionid, default_function_type);
3228
3229   DECL_EXTERNAL (decl) = 1;
3230   TREE_PUBLIC (decl) = 1;
3231
3232   /* ANSI standard says implicit declarations are in the innermost block.
3233      So we record the decl in the standard fashion.
3234      If flag_traditional is set, pushdecl does it top-level.  */
3235   pushdecl (decl);
3236   rest_of_decl_compilation (decl, NULL_PTR, 0, 0);
3237
3238   if (warn_implicit
3239       /* Only one warning per identifier.  */
3240       && IDENTIFIER_IMPLICIT_DECL (functionid) == NULL_TREE)
3241     {
3242       cp_pedwarn ("implicit declaration of function `%#D'", decl);
3243     }
3244
3245   SET_IDENTIFIER_IMPLICIT_DECL (functionid, decl);
3246
3247   pop_obstacks ();
3248
3249   return decl;
3250 }
3251
3252 /* Return zero if the declaration NEWDECL is valid
3253    when the declaration OLDDECL (assumed to be for the same name)
3254    has already been seen.
3255    Otherwise return an error message format string with a %s
3256    where the identifier should go.  */
3257
3258 static char *
3259 redeclaration_error_message (newdecl, olddecl)
3260      tree newdecl, olddecl;
3261 {
3262   if (TREE_CODE (newdecl) == TYPE_DECL)
3263     {
3264       /* Because C++ can put things into name space for free,
3265          constructs like "typedef struct foo { ... } foo"
3266          would look like an erroneous redeclaration.  */
3267       if (comptypes (TREE_TYPE (newdecl), TREE_TYPE (olddecl), 0))
3268         return 0;
3269       else
3270         return "redefinition of `%#D'";
3271     }
3272   else if (TREE_CODE (newdecl) == FUNCTION_DECL)
3273     {
3274       /* If this is a pure function, its olddecl will actually be
3275          the original initialization to `0' (which we force to call
3276          abort()).  Don't complain about redefinition in this case.  */
3277       if (DECL_LANG_SPECIFIC (olddecl) && DECL_ABSTRACT_VIRTUAL_P (olddecl))
3278         return 0;
3279
3280       /* Declarations of functions can insist on internal linkage
3281          but they can't be inconsistent with internal linkage,
3282          so there can be no error on that account.
3283          However defining the same name twice is no good.  */
3284       if (DECL_INITIAL (olddecl) != NULL_TREE
3285           && DECL_INITIAL (newdecl) != NULL_TREE
3286           /* However, defining once as extern inline and a second
3287              time in another way is ok.  */
3288           && !(DECL_INLINE (olddecl) && DECL_EXTERNAL (olddecl)
3289                && !(DECL_INLINE (newdecl) && DECL_EXTERNAL (newdecl))))
3290         {
3291           if (DECL_NAME (olddecl) == NULL_TREE)
3292             return "`%#D' not declared in class";
3293           else
3294             return "redefinition of `%#D'";
3295         }
3296       return 0;
3297     }
3298   else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3299     {
3300       if (DECL_INITIAL (olddecl) && DECL_INITIAL (newdecl))
3301         return "redefinition of `%#D'";
3302       return 0;
3303     }
3304   else if (current_binding_level == global_binding_level)
3305     {
3306       /* Objects declared at top level:  */
3307       /* If at least one is a reference, it's ok.  */
3308       if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
3309         return 0;
3310       /* Now we have two tentative defs, or one tentative and one real def.  */
3311       /* Insist that the linkage match.  */
3312       if (TREE_PUBLIC (olddecl) != TREE_PUBLIC (newdecl))
3313         return "conflicting declarations of `%#D'";
3314       /* Reject two definitions.  */
3315       return "redefinition of `%#D'";
3316     }
3317   else
3318     {
3319       /* Objects declared with block scope:  */
3320       /* Reject two definitions, and reject a definition
3321          together with an external reference.  */
3322       if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
3323         return "redeclaration of `%#D'";
3324       return 0;
3325     }
3326 }
3327 \f
3328 /* Get the LABEL_DECL corresponding to identifier ID as a label.
3329    Create one if none exists so far for the current function.
3330    This function is called for both label definitions and label references.  */
3331
3332 tree
3333 lookup_label (id)
3334      tree id;
3335 {
3336   register tree decl = IDENTIFIER_LABEL_VALUE (id);
3337
3338   if (current_function_decl == NULL_TREE)
3339     {
3340       error ("label `%s' referenced outside of any function",
3341              IDENTIFIER_POINTER (id));
3342       return NULL_TREE;
3343     }
3344
3345   if ((decl == NULL_TREE
3346       || DECL_SOURCE_LINE (decl) == 0)
3347       && (named_label_uses == NULL_TREE
3348           || TREE_PURPOSE (named_label_uses) != current_binding_level->names
3349           || TREE_VALUE (named_label_uses) != decl))
3350     {
3351       named_label_uses
3352         = tree_cons (current_binding_level->names, decl, named_label_uses);
3353       TREE_TYPE (named_label_uses) = (tree)current_binding_level;
3354     }
3355
3356   /* Use a label already defined or ref'd with this name.  */
3357   if (decl != NULL_TREE)
3358     {
3359       /* But not if it is inherited and wasn't declared to be inheritable.  */
3360       if (DECL_CONTEXT (decl) != current_function_decl
3361           && ! C_DECLARED_LABEL_FLAG (decl))
3362         return shadow_label (id);
3363       return decl;
3364     }
3365
3366   decl = build_decl (LABEL_DECL, id, void_type_node);
3367
3368   /* A label not explicitly declared must be local to where it's ref'd.  */
3369   DECL_CONTEXT (decl) = current_function_decl;
3370
3371   DECL_MODE (decl) = VOIDmode;
3372
3373   /* Say where one reference is to the label,
3374      for the sake of the error if it is not defined.  */
3375   DECL_SOURCE_LINE (decl) = lineno;
3376   DECL_SOURCE_FILE (decl) = input_filename;
3377
3378   SET_IDENTIFIER_LABEL_VALUE (id, decl);
3379
3380   named_labels = tree_cons (NULL_TREE, decl, named_labels);
3381   TREE_VALUE (named_label_uses) = decl;
3382
3383   return decl;
3384 }
3385
3386 /* Make a label named NAME in the current function,
3387    shadowing silently any that may be inherited from containing functions
3388    or containing scopes.
3389
3390    Note that valid use, if the label being shadowed
3391    comes from another scope in the same function,
3392    requires calling declare_nonlocal_label right away.  */
3393
3394 tree
3395 shadow_label (name)
3396      tree name;
3397 {
3398   register tree decl = IDENTIFIER_LABEL_VALUE (name);
3399
3400   if (decl != NULL_TREE)
3401     {
3402       shadowed_labels = tree_cons (NULL_TREE, decl, shadowed_labels);
3403       SET_IDENTIFIER_LABEL_VALUE (name, NULL_TREE);
3404       SET_IDENTIFIER_LABEL_VALUE (decl, NULL_TREE);
3405     }
3406
3407   return lookup_label (name);
3408 }
3409
3410 /* Define a label, specifying the location in the source file.
3411    Return the LABEL_DECL node for the label, if the definition is valid.
3412    Otherwise return 0.  */
3413
3414 tree
3415 define_label (filename, line, name)
3416      char *filename;
3417      int line;
3418      tree name;
3419 {
3420   tree decl = lookup_label (name);
3421
3422   /* After labels, make any new cleanups go into their
3423      own new (temporary) binding contour.  */
3424   current_binding_level->more_cleanups_ok = 0;
3425
3426   /* If label with this name is known from an outer context, shadow it.  */
3427   if (decl != NULL_TREE && DECL_CONTEXT (decl) != current_function_decl)
3428     {
3429       shadowed_labels = tree_cons (NULL_TREE, decl, shadowed_labels);
3430       SET_IDENTIFIER_LABEL_VALUE (name, NULL_TREE);
3431       decl = lookup_label (name);
3432     }
3433
3434   if (DECL_INITIAL (decl) != NULL_TREE)
3435     {
3436       cp_error ("duplicate label `%D'", decl);
3437       return 0;
3438     }
3439   else
3440     {
3441       tree uses, prev;
3442
3443       /* Mark label as having been defined.  */
3444       DECL_INITIAL (decl) = error_mark_node;
3445       /* Say where in the source.  */
3446       DECL_SOURCE_FILE (decl) = filename;
3447       DECL_SOURCE_LINE (decl) = line;
3448
3449       for (prev = NULL_TREE, uses = named_label_uses;
3450            uses;
3451            prev = uses, uses = TREE_CHAIN (uses))
3452         if (TREE_VALUE (uses) == decl)
3453           {
3454             struct binding_level *b = current_binding_level;
3455             while (b)
3456               {
3457                 tree new_decls = b->names;
3458                 tree old_decls = ((tree)b == TREE_TYPE (uses)
3459                                   ? TREE_PURPOSE (uses) : NULL_TREE);
3460                 while (new_decls != old_decls)
3461                   {
3462                     if (TREE_CODE (new_decls) == VAR_DECL
3463                         /* Don't complain about crossing initialization
3464                            of internal entities.  They can't be accessed,
3465                            and they should be cleaned up
3466                            by the time we get to the label.  */
3467                         && DECL_SOURCE_LINE (new_decls) != 0
3468                         && ((DECL_INITIAL (new_decls) != NULL_TREE
3469                              && DECL_INITIAL (new_decls) != error_mark_node)
3470                             || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (new_decls))))
3471                       {
3472                         if (IDENTIFIER_ERROR_LOCUS (decl) == NULL_TREE)
3473                           cp_error ("invalid jump to label `%D'", decl);
3474                         SET_IDENTIFIER_ERROR_LOCUS (decl, current_function_decl);
3475                         cp_error ("crosses initialization of `%D'", new_decls);
3476                       }
3477                     new_decls = TREE_CHAIN (new_decls);
3478                   }
3479                 if ((tree)b == TREE_TYPE (uses))
3480                   break;
3481                 b = b->level_chain;
3482               }
3483
3484             if (prev)
3485               TREE_CHAIN (prev) = TREE_CHAIN (uses);
3486             else
3487               named_label_uses = TREE_CHAIN (uses);
3488           }
3489       current_function_return_value = NULL_TREE;
3490       return decl;
3491     }
3492 }
3493
3494 /* Same, but for CASE labels.  If DECL is NULL_TREE, it's the default.  */
3495 /* XXX Note decl is never actually used. (bpk) */
3496 void
3497 define_case_label (decl)
3498      tree decl;
3499 {
3500   tree cleanup = last_cleanup_this_contour ();
3501   if (cleanup)
3502     {
3503       static int explained = 0;
3504       cp_error_at ("destructor needed for `%#D'", TREE_PURPOSE (cleanup));
3505       error ("where case label appears here");
3506       if (!explained)
3507         {
3508           error ("(enclose actions of previous case statements requiring");
3509           error ("destructors in their own binding contours.)");
3510           explained = 1;
3511         }
3512     }
3513
3514   /* After labels, make any new cleanups go into their
3515      own new (temporary) binding contour.  */
3516
3517   current_binding_level->more_cleanups_ok = 0;
3518   current_function_return_value = NULL_TREE;
3519 }
3520 \f
3521 /* Return the list of declarations of the current level.
3522    Note that this list is in reverse order unless/until
3523    you nreverse it; and when you do nreverse it, you must
3524    store the result back using `storedecls' or you will lose.  */
3525
3526 tree
3527 getdecls ()
3528 {
3529   return current_binding_level->names;
3530 }
3531
3532 /* Return the list of type-tags (for structs, etc) of the current level.  */
3533
3534 tree
3535 gettags ()
3536 {
3537   return current_binding_level->tags;
3538 }
3539
3540 /* Store the list of declarations of the current level.
3541    This is done for the parameter declarations of a function being defined,
3542    after they are modified in the light of any missing parameters.  */
3543
3544 static void
3545 storedecls (decls)
3546      tree decls;
3547 {
3548   current_binding_level->names = decls;
3549 }
3550
3551 /* Similarly, store the list of tags of the current level.  */
3552
3553 static void
3554 storetags (tags)
3555      tree tags;
3556 {
3557   current_binding_level->tags = tags;
3558 }
3559 \f
3560 /* Given NAME, an IDENTIFIER_NODE,
3561    return the structure (or union or enum) definition for that name.
3562    Searches binding levels from BINDING_LEVEL up to the global level.
3563    If THISLEVEL_ONLY is nonzero, searches only the specified context
3564    (but skips any tag-transparent contexts to find one that is
3565    meaningful for tags).
3566    FORM says which kind of type the caller wants;
3567    it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
3568    If the wrong kind of type is found, and it's not a template, an error is
3569    reported.  */
3570
3571 static tree
3572 lookup_tag (form, name, binding_level, thislevel_only)
3573      enum tree_code form;
3574      struct binding_level *binding_level;
3575      tree name;
3576      int thislevel_only;
3577 {
3578   register struct binding_level *level;
3579
3580   for (level = binding_level; level; level = level->level_chain)
3581     {
3582       register tree tail;
3583       if (ANON_AGGRNAME_P (name))
3584         for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
3585           {
3586             /* There's no need for error checking here, because
3587                anon names are unique throughout the compilation.  */
3588             if (TYPE_IDENTIFIER (TREE_VALUE (tail)) == name)
3589               return TREE_VALUE (tail);
3590           }
3591       else
3592         for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
3593           {
3594             if (TREE_PURPOSE (tail) == name)
3595               {
3596                 enum tree_code code = TREE_CODE (TREE_VALUE (tail));
3597                 /* Should tighten this up; it'll probably permit
3598                    UNION_TYPE and a struct template, for example.  */
3599                 if (code != form
3600                     && !(form != ENUMERAL_TYPE
3601                          && (code == TEMPLATE_DECL
3602                              || code == UNINSTANTIATED_P_TYPE)))
3603                            
3604                   {
3605                     /* Definition isn't the kind we were looking for.  */
3606                     cp_error ("`%#D' redeclared as %C", TREE_VALUE (tail),
3607                               form);
3608                   }
3609                 return TREE_VALUE (tail);
3610               }
3611           }
3612       if (thislevel_only && ! level->tag_transparent)
3613         return NULL_TREE;
3614       if (current_class_type && level->level_chain == global_binding_level)
3615         {
3616           /* Try looking in this class's tags before heading into
3617              global binding level.  */
3618           tree context = current_class_type;
3619           while (context)
3620             {
3621               switch (TREE_CODE_CLASS (TREE_CODE (context)))
3622                 {
3623                 tree these_tags;
3624                 case 't':
3625                     these_tags = CLASSTYPE_TAGS (context);
3626                     if (ANON_AGGRNAME_P (name))
3627                       while (these_tags)
3628                         {
3629                           if (TYPE_IDENTIFIER (TREE_VALUE (these_tags))
3630                               == name)
3631                             return TREE_VALUE (tail);
3632                           these_tags = TREE_CHAIN (these_tags);
3633                         }
3634                     else
3635                       while (these_tags)
3636                         {
3637                           if (TREE_PURPOSE (these_tags) == name)
3638                             {
3639                               if (TREE_CODE (TREE_VALUE (these_tags)) != form)
3640                                 {
3641                                   cp_error ("`%#D' redeclared as %C in class scope",
3642                                             TREE_VALUE (tail), form);
3643                                 }
3644                               return TREE_VALUE (tail);
3645                             }
3646                           these_tags = TREE_CHAIN (these_tags);
3647                         }
3648                     /* If this type is not yet complete, then don't
3649                        look at its context.  */
3650                     if (TYPE_SIZE (context) == NULL_TREE)
3651                       goto no_context;
3652                     /* Go to next enclosing type, if any.  */
3653                     context = DECL_CONTEXT (TYPE_NAME (context));
3654                     break;
3655                 case 'd':
3656                     context = DECL_CONTEXT (context);
3657                     break;
3658                 default:
3659                     my_friendly_abort (10);
3660                 }
3661               continue;
3662               no_context:
3663               break;
3664             }
3665         }
3666     }
3667   return NULL_TREE;
3668 }
3669
3670 void
3671 set_current_level_tags_transparency (tags_transparent)
3672      int tags_transparent;
3673 {
3674   current_binding_level->tag_transparent = tags_transparent;
3675 }
3676
3677 /* Given a type, find the tag that was defined for it and return the tag name.
3678    Otherwise return 0.  However, the value can never be 0
3679    in the cases in which this is used.
3680
3681    C++: If NAME is non-zero, this is the new name to install.  This is
3682    done when replacing anonymous tags with real tag names.  */
3683
3684 static tree
3685 lookup_tag_reverse (type, name)
3686      tree type;
3687      tree name;
3688 {
3689   register struct binding_level *level;
3690
3691   for (level = current_binding_level; level; level = level->level_chain)
3692     {
3693       register tree tail;
3694       for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
3695         {
3696           if (TREE_VALUE (tail) == type)
3697             {
3698               if (name)
3699                 TREE_PURPOSE (tail) = name;
3700               return TREE_PURPOSE (tail);
3701             }
3702         }
3703     }
3704   return NULL_TREE;
3705 }
3706
3707 /* Given type TYPE which was not declared in C++ language context,
3708    attempt to find a name by which it is referred.  */
3709 tree
3710 typedecl_for_tag (tag)
3711      tree tag;
3712 {
3713   struct binding_level *b = current_binding_level;
3714
3715   if (TREE_CODE (TYPE_NAME (tag)) == TYPE_DECL)
3716     return TYPE_NAME (tag);
3717
3718   while (b)
3719     {
3720       tree decls = b->names;
3721       while (decls)
3722         {
3723           if (TREE_CODE (decls) == TYPE_DECL && TREE_TYPE (decls) == tag)
3724             break;
3725           decls = TREE_CHAIN (decls);
3726         }
3727       if (decls)
3728         return decls;
3729       b = b->level_chain;
3730     }
3731   return NULL_TREE;
3732 }
3733 \f
3734 /* Lookup TYPE in CONTEXT (a chain of nested types or a FUNCTION_DECL).
3735    Return the type value, or NULL_TREE if not found.  */
3736 static tree
3737 lookup_nested_type (type, context)
3738      tree type;
3739      tree context;
3740 {
3741   if (context == NULL_TREE)
3742     return NULL_TREE;
3743   while (context)
3744     {
3745       switch (TREE_CODE (context))
3746         {
3747         case TYPE_DECL:
3748           {
3749             tree ctype = TREE_TYPE (context);
3750             tree match = value_member (type, CLASSTYPE_TAGS (ctype));
3751             if (match)
3752               return TREE_VALUE (match);
3753             context = DECL_CONTEXT (context);
3754
3755             /* When we have a nested class whose member functions have
3756                local types (e.g., a set of enums), we'll arrive here
3757                with the DECL_CONTEXT as the actual RECORD_TYPE node for
3758                the enclosing class.  Instead, we want to make sure we
3759                come back in here with the TYPE_DECL, not the RECORD_TYPE.  */
3760             if (context && TREE_CODE (context) == RECORD_TYPE)
3761               context = TREE_CHAIN (context);
3762           }
3763           break;
3764         case FUNCTION_DECL:
3765           return TYPE_IDENTIFIER (type) ? lookup_name (TYPE_IDENTIFIER (type), 1) : NULL_TREE;
3766           break;
3767         default:
3768           my_friendly_abort (12);
3769         }
3770     }
3771   return NULL_TREE;
3772 }
3773
3774 /* Look up NAME in the current binding level and its superiors in the
3775    namespace of variables, functions and typedefs.  Return a ..._DECL
3776    node of some kind representing its definition if there is only one
3777    such declaration, or return a TREE_LIST with all the overloaded
3778    definitions if there are many, or return 0 if it is undefined.
3779
3780    If PREFER_TYPE is > 0, we prefer TYPE_DECLs.
3781    Otherwise we prefer non-TYPE_DECLs.  */
3782
3783 tree
3784 lookup_name (name, prefer_type)
3785      tree name;
3786      int prefer_type;
3787 {
3788   register tree val;
3789
3790   if (current_binding_level != global_binding_level
3791       && IDENTIFIER_LOCAL_VALUE (name))
3792     val = IDENTIFIER_LOCAL_VALUE (name);
3793   /* In C++ class fields are between local and global scope,
3794      just before the global scope.  */
3795   else if (current_class_type)
3796     {
3797       val = IDENTIFIER_CLASS_VALUE (name);
3798       if (val == NULL_TREE
3799           && TYPE_SIZE (current_class_type) == NULL_TREE
3800           && CLASSTYPE_LOCAL_TYPEDECLS (current_class_type))
3801         {
3802           /* Try to find values from base classes
3803              if we are presently defining a type.
3804              We are presently only interested in TYPE_DECLs.  */
3805           val = lookup_field (current_class_type, name, 0, prefer_type < 0);
3806           if (val == error_mark_node)
3807             return val;
3808           if (val && TREE_CODE (val) != TYPE_DECL)
3809             val = NULL_TREE;
3810         }
3811
3812       /* yylex() calls this with -2, since we should never start digging for
3813          the nested name at the point where we haven't even, for example,
3814          created the COMPONENT_REF or anything like that.  */
3815       if (val == NULL_TREE)
3816         val = lookup_nested_field (name, prefer_type != -2);
3817
3818       if (val == NULL_TREE)
3819         val = IDENTIFIER_GLOBAL_VALUE (name);
3820     }
3821   else
3822     val = IDENTIFIER_GLOBAL_VALUE (name);
3823
3824   if (val)
3825     {
3826       extern int looking_for_typename;
3827
3828       /* Arbitrate between finding a TYPE_DECL and finding
3829          other kinds of _DECLs.  */
3830       if (TREE_CODE (val) == TYPE_DECL || looking_for_typename < 0)
3831         return val;
3832
3833       if (IDENTIFIER_HAS_TYPE_VALUE (name))
3834         {
3835           register tree val_as_type = TYPE_NAME (IDENTIFIER_TYPE_VALUE (name));
3836
3837           if (val == val_as_type || prefer_type > 0
3838               || looking_for_typename > 0)
3839             return val_as_type;
3840
3841           return val;
3842         }
3843       if (TREE_TYPE (val) == error_mark_node)
3844         return error_mark_node;
3845     }
3846
3847   return val;
3848 }
3849
3850 /* Similar to `lookup_name' but look only at current binding level.  */
3851
3852 tree
3853 lookup_name_current_level (name)
3854      tree name;
3855 {
3856   register tree t = NULL_TREE;
3857
3858   if (current_binding_level == global_binding_level)
3859     {
3860       t = IDENTIFIER_GLOBAL_VALUE (name);
3861
3862       /* extern "C" function() */
3863       if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
3864         t = TREE_VALUE (t);
3865     }
3866   else if (IDENTIFIER_LOCAL_VALUE (name) != NULL_TREE)
3867     {
3868       for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
3869         if (DECL_NAME (t) == name)
3870           break;
3871     }
3872
3873   return t;
3874 }
3875 \f
3876 /* Arrange for the user to get a source line number, even when the
3877    compiler is going down in flames, so that she at least has a
3878    chance of working around problems in the compiler.  We used to
3879    call error(), but that let the segmentation fault continue
3880    through; now, it's much more passive by asking them to send the
3881    maintainers mail about the problem.  */
3882
3883 static void
3884 signal_catch (sig)
3885      int sig;
3886 {
3887   signal (SIGSEGV, SIG_DFL);
3888 #ifdef SIGIOT
3889   signal (SIGIOT, SIG_DFL);
3890 #endif
3891 #ifdef SIGILL
3892   signal (SIGILL, SIG_DFL);
3893 #endif
3894 #ifdef SIGABRT
3895   signal (SIGABRT, SIG_DFL);
3896 #endif
3897 #ifdef SIGBUS
3898   signal (SIGBUS, SIG_DFL);
3899 #endif
3900   my_friendly_abort (0);
3901 }
3902
3903 /* Array for holding types considered "built-in".  These types
3904    are output in the module in which `main' is defined.  */
3905 static tree *builtin_type_tdescs_arr;
3906 static int builtin_type_tdescs_len, builtin_type_tdescs_max;
3907
3908 /* Push the declarations of builtin types into the namespace.
3909    RID_INDEX, if < RID_MAX is the index of the builtin type
3910    in the array RID_POINTERS.  NAME is the name used when looking
3911    up the builtin type.  TYPE is the _TYPE node for the builtin type.  */
3912
3913 static void
3914 record_builtin_type (rid_index, name, type)
3915      enum rid rid_index;
3916      char *name;
3917      tree type;
3918 {
3919   tree rname = NULL_TREE, tname = NULL_TREE;
3920   tree tdecl;
3921
3922   if ((int) rid_index < (int) RID_MAX)
3923     rname = ridpointers[(int) rid_index];
3924   if (name)
3925     tname = get_identifier (name);
3926
3927   TYPE_BUILT_IN (type) = 1;
3928   
3929   if (tname)
3930     {
3931 #if 0 /* not yet, should get fixed properly later */
3932       tdecl = pushdecl (make_type_decl (tname, type));
3933 #else
3934       tdecl = pushdecl (build_decl (TYPE_DECL, tname, type));
3935 #endif
3936       set_identifier_type_value (tname, NULL_TREE);
3937       if ((int) rid_index < (int) RID_MAX)
3938         IDENTIFIER_GLOBAL_VALUE (tname) = tdecl;
3939     }
3940   if (rname != NULL_TREE)
3941     {
3942       if (tname != NULL_TREE)
3943         {
3944           set_identifier_type_value (rname, NULL_TREE);
3945           IDENTIFIER_GLOBAL_VALUE (rname) = tdecl;
3946         }
3947       else
3948         {
3949 #if 0 /* not yet, should get fixed properly later */
3950           tdecl = pushdecl (make_type_decl (rname, type));
3951 #else
3952           tdecl = pushdecl (build_decl (TYPE_DECL, rname, type));
3953 #endif
3954           set_identifier_type_value (rname, NULL_TREE);
3955         }
3956     }
3957
3958   if (flag_dossier)
3959     {
3960       if (builtin_type_tdescs_len+5 >= builtin_type_tdescs_max)
3961         {
3962           builtin_type_tdescs_max *= 2;
3963           builtin_type_tdescs_arr
3964             = (tree *)xrealloc (builtin_type_tdescs_arr,
3965                                 builtin_type_tdescs_max * sizeof (tree));
3966         }
3967       builtin_type_tdescs_arr[builtin_type_tdescs_len++] = type;
3968       if (TREE_CODE (type) != POINTER_TYPE)
3969         {
3970           builtin_type_tdescs_arr[builtin_type_tdescs_len++]
3971             = build_pointer_type (type);
3972           builtin_type_tdescs_arr[builtin_type_tdescs_len++]
3973             = build_type_variant (TYPE_POINTER_TO (type), 1, 0);
3974         }
3975       if (TREE_CODE (type) != VOID_TYPE)
3976         {
3977           builtin_type_tdescs_arr[builtin_type_tdescs_len++]
3978             = build_reference_type (type);
3979           builtin_type_tdescs_arr[builtin_type_tdescs_len++]
3980             = build_type_variant (TYPE_REFERENCE_TO (type), 1, 0);
3981         }
3982     }
3983 }
3984
3985 static void
3986 output_builtin_tdesc_entries ()
3987 {
3988   extern struct obstack permanent_obstack;
3989
3990   /* If there's more than one main in this file, don't crash.  */
3991   if (builtin_type_tdescs_arr == 0)
3992     return;
3993
3994   push_obstacks (&permanent_obstack, &permanent_obstack);
3995   while (builtin_type_tdescs_len > 0)
3996     {
3997       tree type = builtin_type_tdescs_arr[--builtin_type_tdescs_len];
3998       tree tdesc = build_t_desc (type, 0);
3999       TREE_ASM_WRITTEN (tdesc) = 0;
4000       build_t_desc (type, 2);
4001     }
4002   free (builtin_type_tdescs_arr);
4003   builtin_type_tdescs_arr = 0;
4004   pop_obstacks ();
4005 }
4006
4007 /* Push overloaded decl, in global scope, with one argument so it
4008    can be used as a callback from define_function.  */
4009 static void
4010 push_overloaded_decl_1 (x)
4011      tree x;
4012 {
4013   push_overloaded_decl (x, 0);
4014 }
4015
4016 /* Create the predefined scalar types of C,
4017    and some nodes representing standard constants (0, 1, (void *)0).
4018    Initialize the global binding level.
4019    Make definitions for built-in primitive functions.  */
4020
4021 void
4022 init_decl_processing ()
4023 {
4024   tree decl;
4025   register tree endlink, int_endlink, double_endlink, ptr_endlink;
4026   tree fields[20];
4027   /* Either char* or void*.  */
4028   tree traditional_ptr_type_node;
4029   /* Data type of memcpy.  */
4030   tree memcpy_ftype;
4031 #if 0 /* Not yet.  */
4032   /* Data type of strncpy.  */
4033   tree strncpy_ftype;
4034 #endif
4035   int wchar_type_size;
4036   tree temp;
4037   tree array_domain_type;
4038
4039   /* Have to make these distinct before we try using them.  */
4040   lang_name_cplusplus = get_identifier ("C++");
4041   lang_name_c = get_identifier ("C");
4042
4043   if (flag_ansi || pedantic)
4044     strict_prototypes_lang_c = strict_prototypes_lang_cplusplus;
4045
4046   /* Initially, C.  */
4047   current_lang_name = lang_name_c;
4048
4049   current_function_decl = NULL_TREE;
4050   named_labels = NULL_TREE;
4051   named_label_uses = NULL_TREE;
4052   current_binding_level = NULL_BINDING_LEVEL;
4053   free_binding_level = NULL_BINDING_LEVEL;
4054
4055   /* Because most segmentation signals can be traced back into user
4056      code, catch them and at least give the user a chance of working
4057      around compiler bugs. */
4058   signal (SIGSEGV, signal_catch);
4059
4060   /* We will also catch aborts in the back-end through signal_catch and
4061      give the user a chance to see where the error might be, and to defeat
4062      aborts in the back-end when there have been errors previously in their
4063      code. */
4064 #ifdef SIGIOT
4065   signal (SIGIOT, signal_catch);
4066 #endif
4067 #ifdef SIGILL
4068   signal (SIGILL, signal_catch);
4069 #endif
4070 #ifdef SIGABRT
4071   signal (SIGABRT, signal_catch);
4072 #endif
4073 #ifdef SIGBUS
4074   signal (SIGBUS, signal_catch);
4075 #endif
4076
4077   gcc_obstack_init (&decl_obstack);
4078   if (flag_dossier)
4079     {
4080       builtin_type_tdescs_max = 100;
4081       builtin_type_tdescs_arr = (tree *)xmalloc (100 * sizeof (tree));
4082     }
4083
4084   /* Must lay these out before anything else gets laid out.  */
4085   error_mark_node = make_node (ERROR_MARK);
4086   TREE_PERMANENT (error_mark_node) = 1;
4087   TREE_TYPE (error_mark_node) = error_mark_node;
4088   error_mark_list = build_tree_list (error_mark_node, error_mark_node);
4089   TREE_TYPE (error_mark_list) = error_mark_node;
4090
4091   pushlevel (0);        /* make the binding_level structure for global names.  */
4092   global_binding_level = current_binding_level;
4093
4094   this_identifier = get_identifier (THIS_NAME);
4095   in_charge_identifier = get_identifier (IN_CHARGE_NAME);
4096   pfn_identifier = get_identifier (VTABLE_PFN_NAME);
4097   index_identifier = get_identifier (VTABLE_INDEX_NAME);
4098   delta_identifier = get_identifier (VTABLE_DELTA_NAME);
4099   delta2_identifier = get_identifier (VTABLE_DELTA2_NAME);
4100   pfn_or_delta2_identifier = get_identifier ("__pfn_or_delta2");
4101
4102   /* Define `int' and `char' first so that dbx will output them first.  */
4103
4104   integer_type_node = make_signed_type (INT_TYPE_SIZE);
4105   record_builtin_type (RID_INT, NULL_PTR, integer_type_node);
4106
4107   /* Define `char', which is like either `signed char' or `unsigned char'
4108      but not the same as either.  */
4109
4110   char_type_node =
4111     (flag_signed_char
4112      ? make_signed_type (CHAR_TYPE_SIZE)
4113      : make_unsigned_type (CHAR_TYPE_SIZE));
4114   record_builtin_type (RID_CHAR, "char", char_type_node);
4115
4116   long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
4117   record_builtin_type (RID_LONG, "long int", long_integer_type_node);
4118
4119   unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
4120   record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
4121
4122   long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
4123   record_builtin_type (RID_MAX, "long unsigned int", long_unsigned_type_node);
4124   record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
4125
4126   long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
4127   record_builtin_type (RID_MAX, "long long int", long_long_integer_type_node);
4128
4129   long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
4130   record_builtin_type (RID_MAX, "long long unsigned int",
4131                        long_long_unsigned_type_node);
4132   record_builtin_type (RID_MAX, "long long unsigned",
4133                        long_long_unsigned_type_node);
4134
4135   /* `unsigned long' is the standard type for sizeof.
4136      Traditionally, use a signed type.
4137      Note that stddef.h uses `unsigned long',
4138      and this must agree, even of long and int are the same size.  */
4139   sizetype
4140     = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (get_identifier (SIZE_TYPE)));
4141   if (flag_traditional && TREE_UNSIGNED (sizetype))
4142     sizetype = signed_type (sizetype);
4143
4144   ptrdiff_type_node
4145     = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (get_identifier (PTRDIFF_TYPE)));
4146
4147   TREE_TYPE (TYPE_SIZE (integer_type_node)) = sizetype;
4148   TREE_TYPE (TYPE_SIZE (char_type_node)) = sizetype;
4149   TREE_TYPE (TYPE_SIZE (unsigned_type_node)) = sizetype;
4150   TREE_TYPE (TYPE_SIZE (long_unsigned_type_node)) = sizetype;
4151   TREE_TYPE (TYPE_SIZE (long_integer_type_node)) = sizetype;
4152   TREE_TYPE (TYPE_SIZE (long_long_integer_type_node)) = sizetype;
4153   TREE_TYPE (TYPE_SIZE (long_long_unsigned_type_node)) = sizetype;
4154
4155   short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
4156   record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
4157   short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
4158   record_builtin_type (RID_MAX, "short unsigned int", short_unsigned_type_node);
4159   record_builtin_type (RID_MAX, "unsigned short", short_unsigned_type_node);
4160
4161   /* Define both `signed char' and `unsigned char'.  */
4162   signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
4163   record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
4164   unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
4165   record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
4166
4167   /* These are types that type_for_size and type_for_mode use.  */
4168   intQI_type_node = make_signed_type (GET_MODE_BITSIZE (QImode));
4169   pushdecl (build_decl (TYPE_DECL, NULL_TREE, intQI_type_node));
4170   intHI_type_node = make_signed_type (GET_MODE_BITSIZE (HImode));
4171   pushdecl (build_decl (TYPE_DECL, NULL_TREE, intHI_type_node));
4172   intSI_type_node = make_signed_type (GET_MODE_BITSIZE (SImode));
4173   pushdecl (build_decl (TYPE_DECL, NULL_TREE, intSI_type_node));
4174   intDI_type_node = make_signed_type (GET_MODE_BITSIZE (DImode));
4175   pushdecl (build_decl (TYPE_DECL, NULL_TREE, intDI_type_node));
4176   unsigned_intQI_type_node = make_unsigned_type (GET_MODE_BITSIZE (QImode));
4177   pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intQI_type_node));
4178   unsigned_intHI_type_node = make_unsigned_type (GET_MODE_BITSIZE (HImode));
4179   pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intHI_type_node));
4180   unsigned_intSI_type_node = make_unsigned_type (GET_MODE_BITSIZE (SImode));
4181   pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intSI_type_node));
4182   unsigned_intDI_type_node = make_unsigned_type (GET_MODE_BITSIZE (DImode));
4183   pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intDI_type_node));
4184
4185   float_type_node = make_node (REAL_TYPE);
4186   TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
4187   record_builtin_type (RID_FLOAT, NULL_PTR, float_type_node);
4188   layout_type (float_type_node);
4189
4190   double_type_node = make_node (REAL_TYPE);
4191   if (flag_short_double)
4192     TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
4193   else
4194     TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
4195   record_builtin_type (RID_DOUBLE, NULL_PTR, double_type_node);
4196   layout_type (double_type_node);
4197
4198   long_double_type_node = make_node (REAL_TYPE);
4199   TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
4200   record_builtin_type (RID_MAX, "long double", long_double_type_node);
4201   layout_type (long_double_type_node);
4202
4203   integer_zero_node = build_int_2 (0, 0);
4204   TREE_TYPE (integer_zero_node) = integer_type_node;
4205   integer_one_node = build_int_2 (1, 0);
4206   TREE_TYPE (integer_one_node) = integer_type_node;
4207   integer_two_node = build_int_2 (2, 0);
4208   TREE_TYPE (integer_two_node) = integer_type_node;
4209   integer_three_node = build_int_2 (3, 0);
4210   TREE_TYPE (integer_three_node) = integer_type_node;
4211   empty_init_node = build_nt (CONSTRUCTOR, NULL_TREE, NULL_TREE);
4212
4213   /* These are needed by stor-layout.c.  */
4214   size_zero_node = size_int (0);
4215   size_one_node = size_int (1);
4216
4217   void_type_node = make_node (VOID_TYPE);
4218   record_builtin_type (RID_VOID, NULL_PTR, void_type_node);
4219   layout_type (void_type_node); /* Uses integer_zero_node.  */
4220   void_list_node = build_tree_list (NULL_TREE, void_type_node);
4221   TREE_PARMLIST (void_list_node) = 1;
4222
4223   null_pointer_node = build_int_2 (0, 0);
4224   TREE_TYPE (null_pointer_node) = build_pointer_type (void_type_node);
4225   layout_type (TREE_TYPE (null_pointer_node));
4226
4227   /* Used for expressions that do nothing, but are not errors.  */
4228   void_zero_node = build_int_2 (0, 0);
4229   TREE_TYPE (void_zero_node) = void_type_node;
4230
4231   string_type_node = build_pointer_type (char_type_node);
4232   const_string_type_node = build_pointer_type (build_type_variant (char_type_node, 1, 0));
4233   record_builtin_type (RID_MAX, NULL_PTR, string_type_node);
4234
4235   /* Make a type to be the domain of a few array types
4236      whose domains don't really matter.
4237      200 is small enough that it always fits in size_t
4238      and large enough that it can hold most function names for the
4239      initializations of __FUNCTION__ and __PRETTY_FUNCTION__.  */
4240   array_domain_type = build_index_type (build_int_2 (200, 0));
4241
4242   /* make a type for arrays of characters.
4243      With luck nothing will ever really depend on the length of this
4244      array type.  */
4245   char_array_type_node
4246     = build_array_type (char_type_node, array_domain_type);
4247   /* Likewise for arrays of ints.  */
4248   int_array_type_node
4249     = build_array_type (integer_type_node, array_domain_type);
4250
4251   /* This is just some anonymous class type.  Nobody should ever
4252      need to look inside this envelope.  */
4253   class_star_type_node = build_pointer_type (make_lang_type (RECORD_TYPE));
4254
4255   default_function_type
4256     = build_function_type (integer_type_node, NULL_TREE);
4257   build_pointer_type (default_function_type);
4258
4259   ptr_type_node = build_pointer_type (void_type_node);
4260   const_ptr_type_node = build_pointer_type (build_type_variant (void_type_node, 1, 0));
4261   record_builtin_type (RID_MAX, NULL_PTR, ptr_type_node);
4262   endlink = void_list_node;
4263   int_endlink = tree_cons (NULL_TREE, integer_type_node, endlink);
4264   double_endlink = tree_cons (NULL_TREE, double_type_node, endlink);
4265   ptr_endlink = tree_cons (NULL_TREE, ptr_type_node, endlink);
4266
4267   double_ftype_double
4268     = build_function_type (double_type_node, double_endlink);
4269
4270   double_ftype_double_double
4271     = build_function_type (double_type_node,
4272                            tree_cons (NULL_TREE, double_type_node, double_endlink));
4273
4274   int_ftype_int
4275     = build_function_type (integer_type_node, int_endlink);
4276
4277   long_ftype_long
4278     = build_function_type (long_integer_type_node,
4279                            tree_cons (NULL_TREE, long_integer_type_node, endlink));
4280
4281   void_ftype_ptr_ptr_int
4282     = build_function_type (void_type_node,
4283                            tree_cons (NULL_TREE, ptr_type_node,
4284                                       tree_cons (NULL_TREE, ptr_type_node,
4285                                                  int_endlink)));
4286
4287   int_ftype_cptr_cptr_sizet
4288     = build_function_type (integer_type_node,
4289                            tree_cons (NULL_TREE, const_ptr_type_node,
4290                                       tree_cons (NULL_TREE, const_ptr_type_node,
4291                                                  tree_cons (NULL_TREE,
4292                                                             sizetype,
4293                                                             endlink))));
4294
4295   void_ftype_ptr_int_int
4296     = build_function_type (void_type_node,
4297                            tree_cons (NULL_TREE, ptr_type_node,
4298                                       tree_cons (NULL_TREE, integer_type_node,
4299                                                  int_endlink)));
4300
4301   string_ftype_ptr_ptr          /* strcpy prototype */
4302     = build_function_type (string_type_node,
4303                            tree_cons (NULL_TREE, string_type_node,
4304                                       tree_cons (NULL_TREE,
4305                                                  const_string_type_node,
4306                                                  endlink)));
4307
4308 #if 0
4309   /* Not yet.  */
4310   strncpy_ftype                 /* strncpy prototype */
4311     = build_function_type (string_type_node,
4312                            tree_cons (NULL_TREE, string_type_node,
4313                                       tree_cons (NULL_TREE, const_string_type_node,
4314                                                  tree_cons (NULL_TREE,
4315                                                             sizetype,
4316                                                             endlink))));
4317 #endif
4318
4319   int_ftype_string_string       /* strcmp prototype */
4320     = build_function_type (integer_type_node,
4321                            tree_cons (NULL_TREE, const_string_type_node,
4322                                       tree_cons (NULL_TREE,
4323                                                  const_string_type_node,
4324                                                  endlink)));
4325
4326   sizet_ftype_string            /* strlen prototype */
4327     = build_function_type (sizetype,
4328                            tree_cons (NULL_TREE, const_string_type_node,
4329                                       endlink));
4330
4331   traditional_ptr_type_node
4332     = (flag_traditional ? string_type_node : ptr_type_node);
4333
4334   memcpy_ftype  /* memcpy prototype */
4335     = build_function_type (traditional_ptr_type_node,
4336                            tree_cons (NULL_TREE, ptr_type_node,
4337                                       tree_cons (NULL_TREE, const_ptr_type_node,
4338                                                  tree_cons (NULL_TREE,
4339                                                             sizetype,
4340                                                             endlink))));
4341
4342   if (flag_huge_objects)
4343     delta_type_node = long_integer_type_node;
4344   else
4345     delta_type_node = short_integer_type_node;
4346
4347   builtin_function ("__builtin_constant_p", int_ftype_int,
4348                     BUILT_IN_CONSTANT_P, NULL_PTR);
4349
4350   builtin_function ("__builtin_return_address",
4351                     build_function_type (ptr_type_node, 
4352                                          tree_cons (NULL_TREE,
4353                                                     unsigned_type_node,
4354                                                     endlink)),
4355                     BUILT_IN_RETURN_ADDRESS, NULL_PTR);
4356
4357   builtin_function ("__builtin_frame_address",
4358                     build_function_type (ptr_type_node, 
4359                                          tree_cons (NULL_TREE,
4360                                                     unsigned_type_node,
4361                                                     endlink)),
4362                     BUILT_IN_FRAME_ADDRESS, NULL_PTR);
4363
4364
4365   builtin_function ("__builtin_alloca",
4366                     build_function_type (ptr_type_node,
4367                                          tree_cons (NULL_TREE,
4368                                                     sizetype,
4369                                                     endlink)),
4370                     BUILT_IN_ALLOCA, "alloca");
4371 #if 0
4372   builtin_function ("alloca",
4373                     build_function_type (ptr_type_node,
4374                                          tree_cons (NULL_TREE,
4375                                                     sizetype,
4376                                                     endlink)),
4377                     BUILT_IN_ALLOCA, NULL_PTR);
4378 #endif
4379
4380   builtin_function ("__builtin_abs", int_ftype_int,
4381                     BUILT_IN_ABS, NULL_PTR);
4382   builtin_function ("__builtin_fabs", double_ftype_double,
4383                     BUILT_IN_FABS, NULL_PTR);
4384   builtin_function ("__builtin_labs", long_ftype_long,
4385                     BUILT_IN_LABS, NULL_PTR);
4386   builtin_function ("__builtin_ffs", int_ftype_int,
4387                     BUILT_IN_FFS, NULL_PTR);
4388   builtin_function ("__builtin_fsqrt", double_ftype_double,
4389                     BUILT_IN_FSQRT, NULL_PTR);
4390   builtin_function ("__builtin_sin", double_ftype_double, 
4391                     BUILT_IN_SIN, "sin");
4392   builtin_function ("__builtin_cos", double_ftype_double, 
4393                     BUILT_IN_COS, "cos");
4394   builtin_function ("__builtin_saveregs",
4395                     build_function_type (ptr_type_node, NULL_TREE),
4396                     BUILT_IN_SAVEREGS, NULL_PTR);
4397 /* EXPAND_BUILTIN_VARARGS is obsolete.  */
4398 #if 0
4399   builtin_function ("__builtin_varargs",
4400                     build_function_type (ptr_type_node,
4401                                          tree_cons (NULL_TREE,
4402                                                     integer_type_node,
4403                                                     endlink)),
4404                     BUILT_IN_VARARGS, NULL_PTR);
4405 #endif
4406   builtin_function ("__builtin_classify_type", default_function_type,
4407                     BUILT_IN_CLASSIFY_TYPE, NULL_PTR);
4408   builtin_function ("__builtin_next_arg",
4409                     build_function_type (ptr_type_node, NULL_TREE),
4410                     BUILT_IN_NEXT_ARG, NULL_PTR);
4411   builtin_function ("__builtin_args_info",
4412                     build_function_type (integer_type_node,
4413                                          tree_cons (NULL_TREE,
4414                                                     integer_type_node,
4415                                                     endlink)),
4416                     BUILT_IN_ARGS_INFO, NULL_PTR);
4417
4418   /* Untyped call and return.  */
4419   builtin_function ("__builtin_apply_args",
4420                     build_function_type (ptr_type_node, NULL_TREE),
4421                     BUILT_IN_APPLY_ARGS, NULL_PTR);
4422
4423   temp = tree_cons (NULL_TREE,
4424                     build_pointer_type (build_function_type (void_type_node,
4425                                                              NULL_TREE)),
4426                     tree_cons (NULL_TREE,
4427                                ptr_type_node,
4428                                tree_cons (NULL_TREE,
4429                                           sizetype,
4430                                           endlink)));
4431   builtin_function ("__builtin_apply",
4432                     build_function_type (ptr_type_node, temp),
4433                     BUILT_IN_APPLY, NULL_PTR);
4434   builtin_function ("__builtin_return",
4435                     build_function_type (void_type_node,
4436                                          tree_cons (NULL_TREE,
4437                                                     ptr_type_node,
4438                                                     endlink)),
4439                     BUILT_IN_RETURN, NULL_PTR);
4440
4441   /* Currently under experimentation.  */
4442   builtin_function ("__builtin_memcpy", memcpy_ftype,
4443                     BUILT_IN_MEMCPY, "memcpy");
4444   builtin_function ("__builtin_memcmp", int_ftype_cptr_cptr_sizet,
4445                     BUILT_IN_MEMCMP, "memcmp");
4446   builtin_function ("__builtin_strcmp", int_ftype_string_string,
4447                     BUILT_IN_STRCMP, "strcmp");
4448   builtin_function ("__builtin_strcpy", string_ftype_ptr_ptr,
4449                     BUILT_IN_STRCPY, "strcpy");
4450 #if 0
4451   /* Not yet.  */
4452   builtin_function ("__builtin_strncpy", strncpy_ftype,
4453                     BUILT_IN_STRNCPY, "strncpy");
4454 #endif
4455   builtin_function ("__builtin_strlen", sizet_ftype_string,
4456                     BUILT_IN_STRLEN, "strlen");
4457
4458   if (!flag_no_builtin)
4459     {
4460 #if 0 /* These do not work well with libg++.  */
4461       builtin_function ("abs", int_ftype_int, BUILT_IN_ABS, NULL_PTR);
4462       builtin_function ("fabs", double_ftype_double, BUILT_IN_FABS, NULL_PTR);
4463       builtin_function ("labs", long_ftype_long, BUILT_IN_LABS, NULL_PTR);
4464 #endif
4465       builtin_function ("memcpy", memcpy_ftype, BUILT_IN_MEMCPY, NULL_PTR);
4466       builtin_function ("memcmp", int_ftype_cptr_cptr_sizet, BUILT_IN_MEMCMP,
4467                         NULL_PTR);
4468       builtin_function ("strcmp", int_ftype_string_string, BUILT_IN_STRCMP, NULL_PTR);
4469       builtin_function ("strcpy", string_ftype_ptr_ptr, BUILT_IN_STRCPY, NULL_PTR);
4470 #if 0
4471       /* Not yet.  */
4472       builtin_function ("strncpy", strncpy_ftype, BUILT_IN_STRNCPY, NULL_PTR);
4473 #endif
4474       builtin_function ("strlen", sizet_ftype_string, BUILT_IN_STRLEN, NULL_PTR);
4475       builtin_function ("sin", double_ftype_double, BUILT_IN_SIN, NULL_PTR);
4476       builtin_function ("cos", double_ftype_double, BUILT_IN_COS, NULL_PTR);
4477     }
4478
4479 #if 0
4480   /* Support for these has not been written in either expand_builtin
4481      or build_function_call.  */
4482   builtin_function ("__builtin_div", default_ftype, BUILT_IN_DIV, 0);
4483   builtin_function ("__builtin_ldiv", default_ftype, BUILT_IN_LDIV, 0);
4484   builtin_function ("__builtin_ffloor", double_ftype_double, BUILT_IN_FFLOOR, 0);
4485   builtin_function ("__builtin_fceil", double_ftype_double, BUILT_IN_FCEIL, 0);
4486   builtin_function ("__builtin_fmod", double_ftype_double_double, BUILT_IN_FMOD, 0);
4487   builtin_function ("__builtin_frem", double_ftype_double_double, BUILT_IN_FREM, 0);
4488   builtin_function ("__builtin_memset", ptr_ftype_ptr_int_int, BUILT_IN_MEMSET, 0);
4489   builtin_function ("__builtin_getexp", double_ftype_double, BUILT_IN_GETEXP, 0);
4490   builtin_function ("__builtin_getman", double_ftype_double, BUILT_IN_GETMAN, 0);
4491 #endif
4492
4493   /* C++ extensions */
4494
4495   unknown_type_node = make_node (UNKNOWN_TYPE);
4496 #if 0 /* not yet, should get fixed properly later */
4497   pushdecl (make_type_decl (get_identifier ("unknown type"),
4498                        unknown_type_node));
4499 #else
4500   decl = pushdecl (build_decl (TYPE_DECL, get_identifier ("unknown type"),
4501                         unknown_type_node));
4502   /* Make sure the "unknown type" typedecl gets ignored for debug info.  */
4503   DECL_IGNORED_P (decl) = 1;
4504 #endif
4505   TYPE_SIZE (unknown_type_node) = TYPE_SIZE (void_type_node);
4506   TYPE_ALIGN (unknown_type_node) = 1;
4507   TYPE_MODE (unknown_type_node) = TYPE_MODE (void_type_node);
4508   /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node.  */
4509   TREE_TYPE (unknown_type_node) = unknown_type_node;
4510   /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same result.  */
4511   TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
4512   TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
4513
4514   /* This is for handling opaque types in signatures.  */
4515   opaque_type_node = copy_node (ptr_type_node);
4516   TYPE_MAIN_VARIANT (opaque_type_node) = opaque_type_node;
4517   record_builtin_type (RID_MAX, 0, opaque_type_node);
4518
4519   /* This is special for C++ so functions can be overloaded. */
4520   wchar_type_node
4521     = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (get_identifier (WCHAR_TYPE)));
4522   wchar_type_size = TYPE_PRECISION (wchar_type_node);
4523   signed_wchar_type_node = make_signed_type (wchar_type_size);
4524   unsigned_wchar_type_node = make_unsigned_type (wchar_type_size);
4525   wchar_type_node
4526     = TREE_UNSIGNED (wchar_type_node)
4527       ? unsigned_wchar_type_node
4528       : signed_wchar_type_node;
4529   record_builtin_type (RID_WCHAR, "__wchar_t", wchar_type_node);
4530
4531   /* This is for wide string constants.  */
4532   wchar_array_type_node
4533     = build_array_type (wchar_type_node, array_domain_type);
4534
4535   /* This is a hack that should go away when we deliver the
4536      real gc code.  */
4537   if (flag_gc)
4538     {
4539       builtin_function ("__gc_main", default_function_type, NOT_BUILT_IN, 0);
4540       pushdecl (lookup_name (get_identifier ("__gc_main"), 0));
4541     }
4542
4543   /* Simplify life by making a "memptr_type".  Give its
4544      fields names so that the debugger can use them.  */
4545
4546   memptr_type = make_lang_type (RECORD_TYPE);
4547   fields[0] = build_lang_field_decl (FIELD_DECL, delta_identifier, delta_type_node);
4548   fields[1] = build_lang_field_decl (FIELD_DECL, index_identifier, delta_type_node);
4549   fields[2] = build_lang_field_decl (FIELD_DECL, pfn_identifier, ptr_type_node);
4550   finish_builtin_type (memptr_type, VTBL_PTR_TYPE, fields, 2,
4551                        double_type_node);
4552
4553   /* Make this part of an invisible union.  */
4554   fields[3] = copy_node (fields[2]);
4555   TREE_TYPE (fields[3]) = delta_type_node;
4556   DECL_NAME (fields[3]) = delta2_identifier;
4557   DECL_MODE (fields[3]) = TYPE_MODE (delta_type_node);
4558   DECL_SIZE (fields[3]) = TYPE_SIZE (delta_type_node);
4559   TREE_UNSIGNED (fields[3]) = 0;
4560   TREE_CHAIN (fields[2]) = fields[3];
4561   memptr_type = build_type_variant (memptr_type, 1, 0);
4562   record_builtin_type (RID_MAX, VTBL_PTR_TYPE, memptr_type);
4563
4564   if (flag_vtable_thunks)
4565     vtable_entry_type = ptr_type_node;
4566   else
4567     vtable_entry_type = memptr_type;
4568
4569 #ifdef VTABLE_USES_MASK
4570   /* This is primarily for virtual function definition.  We
4571      declare an array of `void *', which can later be
4572      converted to the appropriate function pointer type.
4573      To do pointers to members, we need a mask which can
4574      distinguish an index value into a virtual function table
4575      from an address.  */
4576   vtbl_mask = build_int_2 (~((HOST_WIDE_INT) VINDEX_MAX - 1), -1);
4577 #endif
4578
4579   vtbl_type_node
4580     = build_array_type (vtable_entry_type, NULL_TREE);
4581   layout_type (vtbl_type_node);
4582   vtbl_type_node = build_type_variant (vtbl_type_node, 1, 0);
4583   record_builtin_type (RID_MAX, NULL_PTR, vtbl_type_node);
4584
4585   /* Simplify life by making a "sigtable_entry_type".  Give its
4586      fields names so that the debugger can use them.  */
4587
4588   if (flag_handle_signatures)
4589     {
4590       sigtable_entry_type = make_lang_type (RECORD_TYPE);
4591       fields[0] = build_lang_field_decl (FIELD_DECL, get_identifier (SIGTABLE_CODE_NAME), short_integer_type_node);
4592       fields[1] = build_lang_field_decl (FIELD_DECL, get_identifier (SIGTABLE_OFFSET_NAME), short_integer_type_node);
4593       fields[2] = build_lang_field_decl (FIELD_DECL, get_identifier (SIGTABLE_PFN_NAME), ptr_type_node);
4594       finish_builtin_type (sigtable_entry_type, SIGTABLE_PTR_TYPE, fields, 2,
4595                            double_type_node);
4596       sigtable_entry_type = build_type_variant (sigtable_entry_type, 1, 0);
4597       record_builtin_type (RID_MAX, SIGTABLE_PTR_TYPE, sigtable_entry_type);
4598     }
4599
4600   if (flag_dossier)
4601     {
4602       /* Must build __t_desc type.  Currently, type descriptors look like this:
4603
4604          struct __t_desc
4605          {
4606            const char *name;
4607            int size;
4608            int bits;
4609            struct __t_desc *points_to;
4610            int ivars_count, meths_count;
4611            struct __i_desc *ivars[];
4612            struct __m_desc *meths[];
4613            struct __t_desc *parents[];
4614            struct __t_desc *vbases[];
4615            int offsets[];
4616          };
4617
4618          ...as per Linton's paper.  */
4619
4620       __t_desc_type_node = make_lang_type (RECORD_TYPE);
4621       __i_desc_type_node = make_lang_type (RECORD_TYPE);
4622       __m_desc_type_node = make_lang_type (RECORD_TYPE);
4623       __t_desc_array_type = build_array_type (TYPE_POINTER_TO (__t_desc_type_node), NULL_TREE);
4624       __i_desc_array_type = build_array_type (TYPE_POINTER_TO (__i_desc_type_node), NULL_TREE);
4625       __m_desc_array_type = build_array_type (TYPE_POINTER_TO (__m_desc_type_node), NULL_TREE);
4626
4627       fields[0] = build_lang_field_decl (FIELD_DECL, get_identifier ("name"),
4628                                          string_type_node);
4629       fields[1] = build_lang_field_decl (FIELD_DECL, get_identifier ("size"),
4630                                          unsigned_type_node);
4631       fields[2] = build_lang_field_decl (FIELD_DECL, get_identifier ("bits"),
4632                                          unsigned_type_node);
4633       fields[3] = build_lang_field_decl (FIELD_DECL, get_identifier ("points_to"),
4634                                          TYPE_POINTER_TO (__t_desc_type_node));
4635       fields[4] = build_lang_field_decl (FIELD_DECL,
4636                                          get_identifier ("ivars_count"),
4637                                          integer_type_node);
4638       fields[5] = build_lang_field_decl (FIELD_DECL,
4639                                          get_identifier ("meths_count"),
4640                                          integer_type_node);
4641       fields[6] = build_lang_field_decl (FIELD_DECL, get_identifier ("ivars"),
4642                                          build_pointer_type (__i_desc_array_type));
4643       fields[7] = build_lang_field_decl (FIELD_DECL, get_identifier ("meths"),
4644                                          build_pointer_type (__m_desc_array_type));
4645       fields[8] = build_lang_field_decl (FIELD_DECL, get_identifier ("parents"),
4646                                          build_pointer_type (__t_desc_array_type));
4647       fields[9] = build_lang_field_decl (FIELD_DECL, get_identifier ("vbases"),
4648                                          build_pointer_type (__t_desc_array_type));
4649       fields[10] = build_lang_field_decl (FIELD_DECL, get_identifier ("offsets"),
4650                                          build_pointer_type (integer_type_node));
4651       finish_builtin_type (__t_desc_type_node, "__t_desc", fields, 10, integer_type_node);
4652
4653       /* ivar descriptors look like this:
4654
4655          struct __i_desc
4656          {
4657            const char *name;
4658            int offset;
4659            struct __t_desc *type;
4660          };
4661       */
4662
4663       fields[0] = build_lang_field_decl (FIELD_DECL, get_identifier ("name"),
4664                                          string_type_node);
4665       fields[1] = build_lang_field_decl (FIELD_DECL, get_identifier ("offset"),
4666                                          integer_type_node);
4667       fields[2] = build_lang_field_decl (FIELD_DECL, get_identifier ("type"),
4668                                          TYPE_POINTER_TO (__t_desc_type_node));
4669       finish_builtin_type (__i_desc_type_node, "__i_desc", fields, 2, integer_type_node);
4670
4671       /* method descriptors look like this:
4672
4673          struct __m_desc
4674          {
4675            const char *name;
4676            int vindex;
4677            struct __t_desc *vcontext;
4678            struct __t_desc *return_type;
4679            void (*address)();
4680            short parm_count;
4681            short required_parms;
4682            struct __t_desc *parm_types[];
4683          };
4684       */
4685
4686       fields[0] = build_lang_field_decl (FIELD_DECL, get_identifier ("name"),
4687                                          string_type_node);
4688       fields[1] = build_lang_field_decl (FIELD_DECL, get_identifier ("vindex"),
4689                                          integer_type_node);
4690       fields[2] = build_lang_field_decl (FIELD_DECL, get_identifier ("vcontext"),
4691                                          TYPE_POINTER_TO (__t_desc_type_node));
4692       fields[3] = build_lang_field_decl (FIELD_DECL, get_identifier ("return_type"),
4693                                          TYPE_POINTER_TO (__t_desc_type_node));
4694       fields[4] = build_lang_field_decl (FIELD_DECL, get_identifier ("address"),
4695                                          build_pointer_type (default_function_type));
4696       fields[5] = build_lang_field_decl (FIELD_DECL, get_identifier ("parm_count"),
4697                                          short_integer_type_node);
4698       fields[6] = build_lang_field_decl (FIELD_DECL, get_identifier ("required_parms"),
4699                                          short_integer_type_node);
4700       fields[7] = build_lang_field_decl (FIELD_DECL, get_identifier ("parm_types"),
4701                                          build_pointer_type (build_array_type (TYPE_POINTER_TO (__t_desc_type_node), NULL_TREE)));
4702       finish_builtin_type (__m_desc_type_node, "__m_desc", fields, 7, integer_type_node);
4703     }
4704
4705   /* Now, C++.  */
4706   current_lang_name = lang_name_cplusplus;
4707   if (flag_dossier)
4708     {
4709       int i = builtin_type_tdescs_len;
4710       while (i > 0)
4711         {
4712           tree tdesc = build_t_desc (builtin_type_tdescs_arr[--i], 0);
4713           TREE_ASM_WRITTEN (tdesc) = 1;
4714           TREE_PUBLIC (TREE_OPERAND (tdesc, 0)) = 1;
4715         }
4716     }
4717
4718   auto_function (ansi_opname[(int) NEW_EXPR],
4719                  build_function_type (ptr_type_node,
4720                                       tree_cons (NULL_TREE, sizetype,
4721                                                  void_list_node)),
4722                  NOT_BUILT_IN);
4723   auto_function (ansi_opname[(int) DELETE_EXPR],
4724                  build_function_type (void_type_node,
4725                                       tree_cons (NULL_TREE, ptr_type_node,
4726                                                  void_list_node)),
4727                  NOT_BUILT_IN);
4728
4729   abort_fndecl
4730     = define_function ("abort",
4731                        build_function_type (void_type_node, void_list_node),
4732                        NOT_BUILT_IN, 0, 0);
4733
4734   unhandled_exception_fndecl
4735     = define_function ("__unhandled_exception",
4736                        build_function_type (void_type_node, NULL_TREE),
4737                        NOT_BUILT_IN, 0, 0);
4738
4739   /* Perform other language dependent initializations.  */
4740   init_class_processing ();
4741   init_init_processing ();
4742   init_search_processing ();
4743
4744   if (flag_handle_exceptions)
4745     {
4746       if (flag_handle_exceptions == 2)
4747         /* Too much trouble to inline all the trys needed for this.  */
4748         flag_this_is_variable = 2;
4749       init_exception_processing ();
4750     }
4751   if (flag_gc)
4752     init_gc_processing ();
4753   if (flag_no_inline)
4754     {
4755       flag_inline_functions = 0;
4756 #if 0
4757       /* This causes uneccessary emission of inline functions.  */
4758       flag_default_inline = 0;
4759 #endif
4760     }
4761   if (flag_cadillac)
4762     init_cadillac ();
4763
4764   /* Create the global bindings for __FUNCTION__ and __PRETTY_FUNCTION__.  */
4765   declare_function_name ();
4766
4767   /* Prepare to check format strings against argument lists.  */
4768   init_function_format_info ();
4769 }
4770
4771 /* Make a definition for a builtin function named NAME and whose data type
4772    is TYPE.  TYPE should be a function type with argument types.
4773    FUNCTION_CODE tells later passes how to compile calls to this function.
4774    See tree.h for its possible values.
4775
4776    If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
4777    the name to be called if we can't opencode the function.  */
4778
4779 tree
4780 define_function (name, type, function_code, pfn, library_name)
4781      char *name;
4782      tree type;
4783      enum built_in_function function_code;
4784      void (*pfn)();
4785      char *library_name;
4786 {
4787   tree decl = build_lang_decl (FUNCTION_DECL, get_identifier (name), type);
4788   DECL_EXTERNAL (decl) = 1;
4789   TREE_PUBLIC (decl) = 1;
4790
4791   /* Since `pushdecl' relies on DECL_ASSEMBLER_NAME instead of DECL_NAME,
4792      we cannot change DECL_ASSEMBLER_NAME until we have installed this
4793      function in the namespace.  */
4794   if (pfn) (*pfn) (decl);
4795   if (library_name)
4796     DECL_ASSEMBLER_NAME (decl) = get_identifier (library_name);
4797   make_function_rtl (decl);
4798   if (function_code != NOT_BUILT_IN)
4799     {
4800       DECL_BUILT_IN (decl) = 1;
4801       DECL_SET_FUNCTION_CODE (decl, function_code);
4802     }
4803   return decl;
4804 }
4805 \f
4806 /* Called when a declaration is seen that contains no names to declare.
4807    If its type is a reference to a structure, union or enum inherited
4808    from a containing scope, shadow that tag name for the current scope
4809    with a forward reference.
4810    If its type defines a new named structure or union
4811    or defines an enum, it is valid but we need not do anything here.
4812    Otherwise, it is an error.
4813
4814    C++: may have to grok the declspecs to learn about static,
4815    complain for anonymous unions.  */
4816
4817 void
4818 shadow_tag (declspecs)
4819      tree declspecs;
4820 {
4821   int found_tag = 0;
4822   int warned = 0;
4823   int static_or_extern = 0;
4824   register tree link;
4825   register enum tree_code code, ok_code = ERROR_MARK;
4826   register tree t = NULL_TREE;
4827
4828   for (link = declspecs; link; link = TREE_CHAIN (link))
4829     {
4830       register tree value = TREE_VALUE (link);
4831
4832       code = TREE_CODE (value);
4833       if (IS_AGGR_TYPE_CODE (code) || code == ENUMERAL_TYPE)
4834         /* Used to test also that TYPE_SIZE (value) != 0.
4835            That caused warning for `struct foo;' at top level in the file.  */
4836         {
4837           register tree name = TYPE_NAME (value);
4838
4839           if (name == NULL_TREE)
4840             name = lookup_tag_reverse (value, NULL_TREE);
4841
4842           if (name && TREE_CODE (name) == TYPE_DECL)
4843             name = DECL_NAME (name);
4844
4845           t = lookup_tag (code, name, inner_binding_level, 1);
4846
4847           if (t == NULL_TREE)
4848             {
4849               push_obstacks (&permanent_obstack, &permanent_obstack);
4850               if (IS_AGGR_TYPE_CODE (code))
4851                 t = make_lang_type (code);
4852               else
4853                 t = make_node (code);
4854               pushtag (name, t, 0);
4855               pop_obstacks ();
4856               ok_code = code;
4857               break;
4858             }
4859           else if (name != NULL_TREE || code == ENUMERAL_TYPE)
4860             ok_code = code;
4861
4862           if (ok_code != ERROR_MARK)
4863             found_tag++;
4864           else
4865             {
4866               if (!warned)
4867                 pedwarn ("useless keyword or type name in declaration");
4868               warned = 1;
4869             }
4870         }
4871       else if (value == ridpointers[(int) RID_STATIC]
4872                || value == ridpointers[(int) RID_EXTERN])
4873         static_or_extern = 1;
4874     }
4875
4876   /* This is where the variables in an anonymous union are
4877      declared.  An anonymous union declaration looks like:
4878      union { ... } ;
4879      because there is no declarator after the union, the parser
4880      sends that declaration here.  */
4881   if (ok_code == UNION_TYPE
4882       && t != NULL_TREE
4883       && ((TREE_CODE (TYPE_NAME (t)) == IDENTIFIER_NODE
4884            && ANON_AGGRNAME_P (TYPE_NAME (t)))
4885           || (TREE_CODE (TYPE_NAME (t)) == TYPE_DECL
4886               && ANON_AGGRNAME_P (TYPE_IDENTIFIER (t)))))
4887     {
4888       /* ANSI C++ June 5 1992 WP 9.5.3.  Anonymous unions may not have
4889          function members.  */
4890       if (TYPE_FIELDS (t))
4891         {
4892           tree decl = grokdeclarator (NULL_TREE, declspecs, NORMAL, 0, NULL_TREE);
4893           finish_anon_union (decl);
4894         }
4895       else
4896         error ("anonymous union cannot have a function member");
4897     }
4898   else
4899     {
4900       /* Anonymous unions are objects, that's why we only check for
4901          static/extern specifiers in this branch.  */
4902       if (static_or_extern)
4903         error ("static/extern can only be specified for objects and functions");
4904
4905       if (ok_code == RECORD_TYPE
4906           && found_tag == 1
4907           && TYPE_LANG_SPECIFIC (t)
4908           && CLASSTYPE_DECLARED_EXCEPTION (t))
4909         {
4910           if (TYPE_SIZE (t))
4911             cp_error ("redeclaration of exception `%T'", t);
4912           else
4913             {
4914               tree ename, decl;
4915
4916               push_obstacks (&permanent_obstack, &permanent_obstack);
4917
4918               pushclass (t, 0);
4919               finish_exception (t, NULL_TREE);
4920
4921               ename = TYPE_NAME (t);
4922               if (TREE_CODE (ename) == TYPE_DECL)
4923                 ename = DECL_NAME (ename);
4924               decl = build_lang_field_decl (VAR_DECL, ename, t);
4925               finish_exception_decl (current_class_name, decl);
4926               end_exception_decls ();
4927
4928               pop_obstacks ();
4929             }
4930         }
4931       else if (!warned && found_tag > 1)
4932         warning ("multiple types in one declaration");
4933     }
4934 }
4935 \f
4936 /* Decode a "typename", such as "int **", returning a ..._TYPE node.  */
4937
4938 tree
4939 groktypename (typename)
4940      tree typename;
4941 {
4942   if (TREE_CODE (typename) != TREE_LIST)
4943     return typename;
4944   return grokdeclarator (TREE_VALUE (typename),
4945                          TREE_PURPOSE (typename),
4946                          TYPENAME, 0, NULL_TREE);
4947 }
4948
4949 /* Decode a declarator in an ordinary declaration or data definition.
4950    This is called as soon as the type information and variable name
4951    have been parsed, before parsing the initializer if any.
4952    Here we create the ..._DECL node, fill in its type,
4953    and put it on the list of decls for the current context.
4954    The ..._DECL node is returned as the value.
4955
4956    Exception: for arrays where the length is not specified,
4957    the type is left null, to be filled in by `finish_decl'.
4958
4959    Function definitions do not come here; they go to start_function
4960    instead.  However, external and forward declarations of functions
4961    do go through here.  Structure field declarations are done by
4962    grokfield and not through here.  */
4963
4964 /* Set this to zero to debug not using the temporary obstack
4965    to parse initializers.  */
4966 int debug_temp_inits = 1;
4967
4968 tree
4969 start_decl (declarator, declspecs, initialized, raises)
4970      tree declarator, declspecs;
4971      int initialized;
4972      tree raises;
4973 {
4974   register tree decl;
4975   register tree type, tem;
4976   tree context;
4977   extern int have_extern_spec;
4978   extern int used_extern_spec;
4979
4980   int init_written = initialized;
4981
4982   /* This should only be done once on the top most decl. */
4983   if (have_extern_spec && !used_extern_spec)
4984     {
4985       declspecs = decl_tree_cons (NULL_TREE, get_identifier ("extern"), declspecs);
4986       used_extern_spec = 1;
4987     }
4988
4989   decl = grokdeclarator (declarator, declspecs, NORMAL, initialized, raises);
4990   if (decl == NULL_TREE || decl == void_type_node)
4991     return NULL_TREE;
4992
4993   type = TREE_TYPE (decl);
4994
4995   /* Don't lose if destructors must be executed at file-level.  */
4996   if (TREE_STATIC (decl)
4997       && TYPE_NEEDS_DESTRUCTOR (type)
4998       && !TREE_PERMANENT (decl))
4999     {
5000       push_obstacks (&permanent_obstack, &permanent_obstack);
5001       decl = copy_node (decl);
5002       if (TREE_CODE (type) == ARRAY_TYPE)
5003         {
5004           tree itype = TYPE_DOMAIN (type);
5005           if (itype && ! TREE_PERMANENT (itype))
5006             {
5007               itype = build_index_type (copy_to_permanent (TYPE_MAX_VALUE (itype)));
5008               type = build_cplus_array_type (TREE_TYPE (type), itype);
5009               TREE_TYPE (decl) = type;
5010             }
5011         }
5012       pop_obstacks ();
5013     }
5014
5015   /* Interesting work for this is done in `finish_exception_decl'.  */
5016   if (TREE_CODE (type) == RECORD_TYPE
5017       && CLASSTYPE_DECLARED_EXCEPTION (type))
5018     return decl;
5019
5020   /* Corresponding pop_obstacks is done in `finish_decl'.  */
5021   push_obstacks_nochange ();
5022
5023   context
5024     = (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
5025       ? DECL_CLASS_CONTEXT (decl)
5026       : DECL_CONTEXT (decl);
5027
5028   if (processing_template_decl)
5029     {
5030       tree d;
5031       if (TREE_CODE (decl) == FUNCTION_DECL)
5032         {
5033           /* Declarator is a call_expr; extract arguments from it, since
5034              grokdeclarator didn't do it.  */
5035           tree args;
5036           args = copy_to_permanent (last_function_parms);
5037           if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
5038             {
5039               tree t = TREE_TYPE (decl);
5040               
5041               t = TYPE_METHOD_BASETYPE (t); /* type method belongs to */
5042               if (TREE_CODE (t) != UNINSTANTIATED_P_TYPE)
5043                 {
5044                   t = build_pointer_type (t); /* base type of `this' */
5045 #if 1
5046                   /* I suspect this is wrong. */
5047                   t = build_type_variant (t, flag_this_is_variable <= 0,
5048                                           0); /* type of `this' */
5049 #else
5050                   t = build_type_variant (t, 0, 0); /* type of `this' */
5051 #endif
5052                   t = build (PARM_DECL, t, this_identifier);
5053                   TREE_CHAIN (t) = args;
5054                   args = t;
5055                 }
5056             }
5057           DECL_ARGUMENTS (decl) = args;
5058         }
5059       d = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), TREE_TYPE (decl));
5060       if (interface_unknown && flag_external_templates && ! DECL_IN_SYSTEM_HEADER (decl))
5061         warn_if_unknown_interface ();
5062       TREE_PUBLIC (d) = TREE_PUBLIC (decl) = flag_external_templates && !interface_unknown;
5063       TREE_STATIC (d) = TREE_STATIC (decl);
5064       DECL_EXTERNAL (d) = (DECL_EXTERNAL (decl)
5065                            && !(context && !DECL_THIS_EXTERN (decl)));
5066       DECL_TEMPLATE_RESULT (d) = decl;
5067       decl = d;
5068     }
5069
5070   if (context && TYPE_SIZE (context) != NULL_TREE)
5071     {
5072       /* If it was not explicitly declared `extern',
5073          revoke any previous claims of DECL_EXTERNAL.  */
5074       if (DECL_THIS_EXTERN (decl) == 0)
5075         DECL_EXTERNAL (decl) = 0;
5076       if (DECL_LANG_SPECIFIC (decl))
5077         DECL_IN_AGGR_P (decl) = 0;
5078       pushclass (context, 2);
5079     }
5080
5081   /* If this type of object needs a cleanup, and control may
5082      jump past it, make a new binding level so that it is cleaned
5083      up only when it is initialized first.  */
5084   if (TYPE_NEEDS_DESTRUCTOR (type)
5085       && current_binding_level->more_cleanups_ok == 0)
5086     pushlevel_temporary (1);
5087
5088   if (initialized)
5089     /* Is it valid for this decl to have an initializer at all?
5090        If not, set INITIALIZED to zero, which will indirectly
5091        tell `finish_decl' to ignore the initializer once it is parsed.  */
5092     switch (TREE_CODE (decl))
5093       {
5094       case TYPE_DECL:
5095         /* typedef foo = bar  means give foo the same type as bar.
5096            We haven't parsed bar yet, so `finish_decl' will fix that up.
5097            Any other case of an initialization in a TYPE_DECL is an error.  */
5098         if (pedantic || list_length (declspecs) > 1)
5099           {
5100             cp_error ("typedef `%D' is initialized", decl);
5101             initialized = 0;
5102           }
5103         break;
5104
5105       case FUNCTION_DECL:
5106         cp_error ("function `%#D' is initialized like a variable", decl);
5107         initialized = 0;
5108         break;
5109
5110       default:
5111         /* Don't allow initializations for incomplete types except for
5112            arrays which might be completed by the initialization.  */
5113         if (TYPE_SIZE (type) != NULL_TREE)
5114           ;                     /* A complete type is ok.  */
5115         else if (TREE_CODE (type) != ARRAY_TYPE)
5116           {
5117             cp_error ("variable `%#D' has initializer but incomplete type",
5118                       decl);
5119             initialized = 0;
5120           }
5121         else if (TYPE_SIZE (TREE_TYPE (type)) == NULL_TREE)
5122           {
5123             cp_error ("elements of array `%#D' have incomplete type", decl);
5124             initialized = 0;
5125           }
5126       }
5127
5128   if (!initialized
5129       && TREE_CODE (decl) != TYPE_DECL
5130       && TREE_CODE (decl) != TEMPLATE_DECL
5131       && IS_AGGR_TYPE (type) && ! DECL_EXTERNAL (decl))
5132     {
5133       if (TYPE_SIZE (type) == NULL_TREE)
5134         {
5135           cp_error ("aggregate `%#D' has incomplete type and cannot be initialized",
5136                  decl);
5137           /* Change the type so that assemble_variable will give
5138              DECL an rtl we can live with: (mem (const_int 0)).  */
5139           TREE_TYPE (decl) = error_mark_node;
5140           type = error_mark_node;
5141         }
5142       else
5143         {
5144           /* If any base type in the hierarchy of TYPE needs a constructor,
5145              then we set initialized to 1.  This way any nodes which are
5146              created for the purposes of initializing this aggregate
5147              will live as long as it does.  This is necessary for global
5148              aggregates which do not have their initializers processed until
5149              the end of the file.  */
5150           initialized = TYPE_NEEDS_CONSTRUCTING (type);
5151         }
5152     }
5153
5154   if (initialized)
5155     {
5156       if (current_binding_level != global_binding_level
5157           && DECL_EXTERNAL (decl))
5158         cp_warning ("declaration of `%#D' has `extern' and is initialized",
5159                     decl);
5160       DECL_EXTERNAL (decl) = 0;
5161       if (current_binding_level == global_binding_level)
5162         TREE_STATIC (decl) = 1;
5163
5164       /* Tell `pushdecl' this is an initialized decl
5165          even though we don't yet have the initializer expression.
5166          Also tell `finish_decl' it may store the real initializer.  */
5167       DECL_INITIAL (decl) = error_mark_node;
5168     }
5169
5170   /* Add this decl to the current binding level, but not if it
5171      comes from another scope, e.g. a static member variable.
5172      TEM may equal DECL or it may be a previous decl of the same name.  */
5173   if ((TREE_CODE (decl) != PARM_DECL && DECL_CONTEXT (decl) != NULL_TREE)
5174       || (TREE_CODE (decl) == TEMPLATE_DECL && !global_bindings_p ())
5175       || TREE_CODE (type) == LANG_TYPE)
5176     tem = decl;
5177   else
5178     tem = pushdecl (decl);
5179              
5180   /* Tell the back-end to use or not use .common as appropriate.  */
5181   DECL_COMMON (tem) = flag_conserve_space;
5182
5183 #if 0
5184   /* We don't do this yet for GNU C++.  */
5185   /* For a local variable, define the RTL now.  */
5186   if (current_binding_level != global_binding_level
5187       /* But not if this is a duplicate decl
5188          and we preserved the rtl from the previous one
5189          (which may or may not happen).  */
5190       && DECL_RTL (tem) == NULL_RTX)
5191     {
5192       if (TYPE_SIZE (TREE_TYPE (tem)) != NULL_TREE)
5193         expand_decl (tem);
5194       else if (TREE_CODE (TREE_TYPE (tem)) == ARRAY_TYPE
5195                && DECL_INITIAL (tem) != NULL_TREE)
5196         expand_decl (tem);
5197     }
5198 #endif
5199
5200   if (TREE_CODE (decl) == TEMPLATE_DECL)
5201     {
5202       tree result = DECL_TEMPLATE_RESULT (decl);
5203       if (DECL_CONTEXT (result) != NULL_TREE)
5204         {
5205           tree type;
5206           type = DECL_CONTEXT (result);
5207
5208           if (TREE_CODE (type) != UNINSTANTIATED_P_TYPE)
5209             {
5210               cp_error ("declaration of `%D' in non-template type `%T'",
5211                         decl, type);
5212               return NULL_TREE;
5213             }
5214
5215           if (/* TREE_CODE (result) == VAR_DECL */ 1)
5216             {
5217 #if 0
5218               tree tmpl = UPT_TEMPLATE (type);
5219               
5220               fprintf (stderr, "%s:%d: adding ", __FILE__, __LINE__);
5221               print_node_brief (stderr, "", DECL_NAME (tem), 0);
5222               fprintf (stderr, " to class %s\n",
5223                        IDENTIFIER_POINTER (DECL_NAME (tmpl)));
5224               DECL_TEMPLATE_MEMBERS (tmpl)
5225                 = perm_tree_cons (DECL_NAME (tem), tem,
5226                                   DECL_TEMPLATE_MEMBERS (tmpl));
5227 #endif
5228               return tem;
5229             }
5230           my_friendly_abort (13);
5231         }
5232       else if (TREE_CODE (result) == FUNCTION_DECL)
5233         /*tem = push_overloaded_decl (tem, 0)*/;
5234       else if (TREE_CODE (result) == VAR_DECL
5235                || TREE_CODE (result) == TYPE_DECL)
5236         {
5237           cp_error ("invalid template `%#D'", result);
5238           return NULL_TREE;
5239         }
5240       else
5241         my_friendly_abort (14);
5242     }
5243
5244   if (init_written
5245       && ! (TREE_CODE (tem) == PARM_DECL
5246             || (TREE_READONLY (tem)
5247                 && (TREE_CODE (tem) == VAR_DECL
5248                     || TREE_CODE (tem) == FIELD_DECL))))
5249     {
5250       /* When parsing and digesting the initializer,
5251          use temporary storage.  Do this even if we will ignore the value.  */
5252       if (current_binding_level == global_binding_level && debug_temp_inits)
5253         {
5254           if (TYPE_NEEDS_CONSTRUCTING (type) || TREE_CODE (type) == REFERENCE_TYPE)
5255             /* In this case, the initializer must lay down in permanent
5256                storage, since it will be saved until `finish_file' is run.   */
5257             ;
5258           else
5259             temporary_allocation ();
5260         }
5261     }
5262
5263   if (flag_cadillac)
5264     cadillac_start_decl (tem);
5265
5266   return tem;
5267 }
5268
5269 #if 0                           /* unused */
5270 static void
5271 make_temporary_for_reference (decl, ctor_call, init, cleanupp)
5272      tree decl, ctor_call, init;
5273      tree *cleanupp;
5274 {
5275   tree type = TREE_TYPE (decl);
5276   tree target_type = TREE_TYPE (type);
5277   tree tmp, tmp_addr;
5278
5279   if (ctor_call)
5280     {
5281       tmp_addr = TREE_VALUE (TREE_OPERAND (ctor_call, 1));
5282       if (TREE_CODE (tmp_addr) == NOP_EXPR)
5283         tmp_addr = TREE_OPERAND (tmp_addr, 0);
5284       my_friendly_assert (TREE_CODE (tmp_addr) == ADDR_EXPR, 146);
5285       tmp = TREE_OPERAND (tmp_addr, 0);
5286     }
5287   else
5288     {
5289       tmp = get_temp_name (target_type,
5290                            current_binding_level == global_binding_level);
5291       tmp_addr = build_unary_op (ADDR_EXPR, tmp, 0);
5292     }
5293
5294   TREE_TYPE (tmp_addr) = build_pointer_type (target_type);
5295   DECL_INITIAL (decl) = convert (TYPE_POINTER_TO (target_type), tmp_addr);
5296   TREE_TYPE (DECL_INITIAL (decl)) = type;
5297   if (TYPE_NEEDS_CONSTRUCTING (target_type))
5298     {
5299       if (current_binding_level == global_binding_level)
5300         {
5301           /* lay this variable out now.  Otherwise `output_addressed_constants'
5302              gets confused by its initializer.  */
5303           make_decl_rtl (tmp, NULL_PTR, 1);
5304           static_aggregates = perm_tree_cons (init, tmp, static_aggregates);
5305         }
5306       else
5307         {
5308           if (ctor_call != NULL_TREE)
5309             init = ctor_call;
5310           else
5311             init = build_method_call (tmp, constructor_name_full (target_type),
5312                                       build_tree_list (NULL_TREE, init),
5313                                       NULL_TREE, LOOKUP_NORMAL);
5314           DECL_INITIAL (decl) = build (COMPOUND_EXPR, type, init,
5315                                        DECL_INITIAL (decl));
5316           *cleanupp = maybe_build_cleanup (tmp);
5317         }
5318     }
5319   else
5320     {
5321       DECL_INITIAL (tmp) = init;
5322       TREE_STATIC (tmp) = current_binding_level == global_binding_level;
5323       finish_decl (tmp, init, 0, 0);
5324     }
5325   if (TREE_STATIC (tmp))
5326     preserve_initializer ();
5327 }
5328 #endif
5329
5330 /* Handle initialization of references.
5331    These three arguments from from `finish_decl', and have the
5332    same meaning here that they do there.  */
5333 /* quotes on semantics can be found in ARM 8.4.3. */
5334 static void
5335 grok_reference_init (decl, type, init, cleanupp)
5336      tree decl, type, init;
5337      tree *cleanupp;
5338 {
5339   char *errstr = NULL;
5340   int is_reference;
5341   tree tmp;
5342   tree this_ptr_type, actual_init = NULL_TREE;
5343
5344   if (init == NULL_TREE)
5345     {
5346       if (DECL_LANG_SPECIFIC (decl) == 0
5347           || DECL_IN_AGGR_P (decl) == 0)
5348         {
5349           cp_error ("`%D' declared as reference but not initialized", decl);
5350           if (TREE_CODE (decl) == VAR_DECL)
5351             SET_DECL_REFERENCE_SLOT (decl, error_mark_node);
5352         }
5353       return;
5354     }
5355
5356   if (init == error_mark_node)
5357     return;
5358
5359   if (TREE_CODE (type) == REFERENCE_TYPE
5360       && TREE_CODE (init) == CONSTRUCTOR)
5361     {
5362       cp_error ("ANSI C++ forbids use of initializer list to initialize reference `%D'", decl);
5363       return;
5364     }
5365
5366   if (TREE_CODE (init) == TREE_LIST)
5367     init = build_compound_expr (init);
5368   is_reference = TREE_CODE (TREE_TYPE (init)) == REFERENCE_TYPE;
5369   tmp = is_reference ? convert_from_reference (init) : init;
5370
5371   if (TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE
5372       && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
5373     {
5374       /* Note: default conversion is only called in very
5375          special cases.  */
5376       init = default_conversion (init);
5377     }
5378
5379   /* Can we just enreference this lvalue?  */
5380   if ((is_reference || lvalue_p (init)
5381        || (actual_init = unary_complex_lvalue (ADDR_EXPR, init)))
5382       && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
5383                     TYPE_MAIN_VARIANT (TREE_TYPE (tmp)), 0))
5384     {
5385       /* This section implements ANSI C++ June 5 1992 WP 8.4.3.5. */
5386
5387       /* A reference to a volatile T cannot be initialized with
5388          a const T, and vice-versa.  */
5389       if (TYPE_VOLATILE (TREE_TYPE (type)) && TREE_READONLY (init))
5390         errstr = "cannot initialize a reference to a volatile `%T' with a const `%T'";
5391       else if (TYPE_READONLY (TREE_TYPE (type)) && TREE_THIS_VOLATILE (init))
5392         errstr = "cannot initialize a reference to a const `%T' with a volatile `%T'";
5393       /* A reference to a plain T can be initialized only with a plain T. */
5394       else if (!TYPE_VOLATILE (TREE_TYPE (type))
5395                && !TYPE_READONLY (TREE_TYPE (type)))
5396         {
5397           if (TREE_READONLY (init))
5398             errstr = "cannot initialize a reference to `%T' with a const `%T'";
5399           else if (TREE_THIS_VOLATILE (init))
5400             errstr = "cannot initialize a reference to `%T' with a volatile `%T'";
5401         }
5402       if (errstr)
5403         {
5404           cp_error (errstr, TREE_TYPE (type), TREE_TYPE (tmp));
5405           goto fail;
5406         }
5407     }
5408   /* OK, can we generate a reference then?  */
5409   else if ((actual_init = convert_to_reference
5410             (decl, type, init, 0, 0, "initialization", 0,
5411              LOOKUP_SPECULATIVELY|LOOKUP_NORMAL)))
5412     {
5413       if (actual_init == error_mark_node)
5414         goto fail;
5415
5416       init = actual_init;
5417       is_reference = 1;
5418     }
5419   /* OK, try going through a temporary.  */
5420   else if ((actual_init = convert_to_reference
5421             (error_mark_node, type, init, 0, 0, "initialization",
5422              0, LOOKUP_NORMAL)))
5423     {
5424       if (actual_init == error_mark_node)
5425         goto fail;
5426       
5427       init = actual_init;
5428       is_reference = 1;
5429
5430       if (TREE_CODE (init) == WITH_CLEANUP_EXPR)
5431         {
5432           /* Associate the cleanup with the reference so that we
5433              don't get burned by "aggressive" cleanup policy.  */
5434           *cleanupp = TREE_OPERAND (init, 2);
5435           TREE_OPERAND (init, 2) = error_mark_node;
5436         }
5437     }
5438   else
5439     {
5440       cp_error ("cannot initialize `%T' from `%T'", type, TREE_TYPE (init));
5441       goto fail;
5442     }
5443
5444   /* In the case of initialization, it is permissible
5445      to assign one reference to another.  */
5446   this_ptr_type = build_pointer_type (TREE_TYPE (type));
5447
5448   if (is_reference)
5449     {
5450       if (TREE_SIDE_EFFECTS (init))
5451         DECL_INITIAL (decl) = save_expr (init);
5452       else
5453         DECL_INITIAL (decl) = init;
5454     }
5455   else if (lvalue_p (init))
5456     {
5457       tmp = build_unary_op (ADDR_EXPR, init, 0);
5458       if (TREE_CODE (tmp) == ADDR_EXPR
5459           && TREE_CODE (TREE_OPERAND (tmp, 0)) == WITH_CLEANUP_EXPR)
5460         {
5461           if (*cleanupp) my_friendly_abort (1);
5462           *cleanupp = TREE_OPERAND (TREE_OPERAND (tmp, 0), 2);
5463           TREE_OPERAND (TREE_OPERAND (tmp, 0), 2) = error_mark_node;
5464         }
5465       if (IS_AGGR_TYPE (TREE_TYPE (this_ptr_type)))
5466         DECL_INITIAL (decl) = convert_pointer_to (TREE_TYPE (this_ptr_type), tmp);
5467       else
5468         DECL_INITIAL (decl) = convert (this_ptr_type, tmp);
5469
5470       DECL_INITIAL (decl) = save_expr (DECL_INITIAL (decl));
5471       if (DECL_INITIAL (decl) == current_class_decl)
5472         DECL_INITIAL (decl) = copy_node (current_class_decl);
5473       TREE_TYPE (DECL_INITIAL (decl)) = type;
5474     }
5475   /* If actual_init is set here, it is set from the first check above.  */
5476   else if (actual_init)
5477     {
5478       /* The initializer for this decl goes into its
5479          DECL_REFERENCE_SLOT.  Make sure that we can handle
5480          multiple evaluations without ill effect.  */
5481       if (TREE_CODE (actual_init) == ADDR_EXPR
5482           && TREE_CODE (TREE_OPERAND (actual_init, 0)) == TARGET_EXPR)
5483         actual_init = save_expr (actual_init);
5484       DECL_INITIAL (decl) = convert_pointer_to (TREE_TYPE (this_ptr_type), actual_init);
5485       DECL_INITIAL (decl) = save_expr (DECL_INITIAL (decl));
5486       TREE_TYPE (DECL_INITIAL (decl)) = type;
5487     }
5488   else
5489     my_friendly_abort (1);
5490
5491   /* ?? Can this be optimized in some cases to
5492      hand back the DECL_INITIAL slot??  */
5493   if (TYPE_SIZE (TREE_TYPE (type)))
5494     {
5495       init = convert_from_reference (decl);
5496       if (TREE_PERMANENT (decl))
5497         init = copy_to_permanent (init);
5498       SET_DECL_REFERENCE_SLOT (decl, init);
5499     }
5500
5501   if (TREE_STATIC (decl) && ! TREE_CONSTANT (DECL_INITIAL (decl)))
5502     {
5503       expand_static_init (decl, DECL_INITIAL (decl));
5504       DECL_INITIAL (decl) = NULL_TREE;
5505     }
5506   return;
5507
5508  fail:
5509   if (TREE_CODE (decl) == VAR_DECL)
5510     SET_DECL_REFERENCE_SLOT (decl, error_mark_node);
5511   return;
5512 }
5513
5514 /* Finish processing of a declaration;
5515    install its line number and initial value.
5516    If the length of an array type is not known before,
5517    it must be determined now, from the initial value, or it is an error.
5518
5519    Call `pop_obstacks' iff NEED_POP is nonzero.
5520
5521    For C++, `finish_decl' must be fairly evasive:  it must keep initializers
5522    for aggregates that have constructors alive on the permanent obstack,
5523    so that the global initializing functions can be written at the end.
5524
5525    INIT0 holds the value of an initializer that should be allowed to escape
5526    the normal rules.
5527
5528    For functions that take default parameters, DECL points to its
5529    "maximal" instantiation.  `finish_decl' must then also declared its
5530    subsequently lower and lower forms of instantiation, checking for
5531    ambiguity as it goes.  This can be sped up later.  */
5532
5533 void
5534 finish_decl (decl, init, asmspec_tree, need_pop)
5535      tree decl, init;
5536      tree asmspec_tree;
5537      int need_pop;
5538 {
5539   register tree type;
5540   tree cleanup = NULL_TREE, ttype;
5541   int was_incomplete;
5542   int temporary = allocation_temporary_p ();
5543   char *asmspec = NULL;
5544   int was_readonly = 0;
5545
5546   /* If this is 0, then we did not change obstacks.  */
5547   if (! decl)
5548     {
5549       if (init)
5550         error ("assignment (not initialization) in declaration");
5551       return;
5552     }
5553
5554   if (asmspec_tree)
5555     {
5556       asmspec = TREE_STRING_POINTER (asmspec_tree);
5557       /* Zero out old RTL, since we will rewrite it.  */
5558       DECL_RTL (decl) = NULL_RTX;
5559     }
5560
5561   /* If the type of the thing we are declaring either has
5562      a constructor, or has a virtual function table pointer,
5563      AND its initialization was accepted by `start_decl',
5564      then we stayed on the permanent obstack through the
5565      declaration, otherwise, changed obstacks as GCC would.  */
5566
5567   type = TREE_TYPE (decl);
5568
5569   was_incomplete = (DECL_SIZE (decl) == NULL_TREE);
5570
5571   /* Take care of TYPE_DECLs up front.  */
5572   if (TREE_CODE (decl) == TYPE_DECL)
5573     {
5574       if (init && DECL_INITIAL (decl))
5575         {
5576           /* typedef foo = bar; store the type of bar as the type of foo.  */
5577           TREE_TYPE (decl) = type = TREE_TYPE (init);
5578           DECL_INITIAL (decl) = init = NULL_TREE;
5579         }
5580       if (IS_AGGR_TYPE (type) && DECL_NAME (decl))
5581         {
5582           if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
5583             cp_warning ("shadowing previous type declaration of `%#D'", decl);
5584           set_identifier_type_value (DECL_NAME (decl), type);
5585           CLASSTYPE_GOT_SEMICOLON (type) = 1;
5586         }
5587       GNU_xref_decl (current_function_decl, decl);
5588       rest_of_decl_compilation (decl, NULL_PTR,
5589                                 DECL_CONTEXT (decl) == NULL_TREE, 0);
5590       goto finish_end;
5591     }
5592   if (type != error_mark_node && IS_AGGR_TYPE (type)
5593       && CLASSTYPE_DECLARED_EXCEPTION (type))
5594     {
5595       finish_exception_decl (NULL_TREE, decl);
5596       CLASSTYPE_GOT_SEMICOLON (type) = 1;
5597       goto finish_end;
5598     }
5599   if (TREE_CODE (decl) != FUNCTION_DECL)
5600     {
5601       ttype = target_type (type);
5602 #if 0 /* WTF?  -KR
5603          Leave this out until we can figure out why it was
5604          needed/desirable in the first place.  Then put a comment
5605          here explaining why.  Or just delete the code if no ill
5606          effects arise.  */
5607       if (TYPE_NAME (ttype)
5608           && TREE_CODE (TYPE_NAME (ttype)) == TYPE_DECL
5609           && ANON_AGGRNAME_P (TYPE_IDENTIFIER (ttype)))
5610         {
5611           tree old_id = TYPE_IDENTIFIER (ttype);
5612           char *newname = (char *)alloca (IDENTIFIER_LENGTH (old_id) + 2);
5613           /* Need to preserve template data for UPT nodes.  */
5614           tree old_template = IDENTIFIER_TEMPLATE (old_id);
5615           newname[0] = '_';
5616           bcopy (IDENTIFIER_POINTER (old_id), newname + 1,
5617                  IDENTIFIER_LENGTH (old_id) + 1);
5618           old_id = get_identifier (newname);
5619           lookup_tag_reverse (ttype, old_id);
5620           TYPE_IDENTIFIER (ttype) = old_id;
5621           IDENTIFIER_TEMPLATE (old_id) = old_template;
5622         }
5623 #endif
5624     }
5625
5626   if (! DECL_EXTERNAL (decl) && TREE_READONLY (decl)
5627       && TYPE_NEEDS_CONSTRUCTING (type))
5628     {
5629
5630       /* Currently, GNU C++ puts constants in text space, making them
5631          impossible to initialize.  In the future, one would hope for
5632          an operating system which understood the difference between
5633          initialization and the running of a program.  */
5634       was_readonly = 1;
5635       TREE_READONLY (decl) = 0;
5636     }
5637
5638   if (TREE_CODE (decl) == FIELD_DECL)
5639     {
5640       if (init && init != error_mark_node)
5641         my_friendly_assert (TREE_PERMANENT (init), 147);
5642
5643       if (asmspec)
5644         {
5645           /* This must override the asm specifier which was placed
5646              by grokclassfn.  Lay this out fresh.
5647              
5648              @@ Should emit an error if this redefines an asm-specified
5649              @@ name, or if we have already used the function's name.  */
5650           DECL_RTL (TREE_TYPE (decl)) = NULL_RTX;
5651           DECL_ASSEMBLER_NAME (decl) = get_identifier (asmspec);
5652           make_decl_rtl (decl, asmspec, 0);
5653         }
5654     }
5655   /* If `start_decl' didn't like having an initialization, ignore it now.  */
5656   else if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
5657     init = NULL_TREE;
5658   else if (DECL_EXTERNAL (decl))
5659     ;
5660   else if (TREE_CODE (type) == REFERENCE_TYPE
5661            || (TYPE_LANG_SPECIFIC (type) && IS_SIGNATURE_REFERENCE (type)))
5662     {
5663       grok_reference_init (decl, type, init, &cleanup);
5664       init = NULL_TREE;
5665     }
5666
5667   GNU_xref_decl (current_function_decl, decl);
5668
5669   if (TREE_CODE (decl) == FIELD_DECL || DECL_EXTERNAL (decl))
5670     ;
5671   else if (TREE_CODE (decl) == CONST_DECL)
5672     {
5673       my_friendly_assert (TREE_CODE (decl) != REFERENCE_TYPE, 148);
5674
5675       DECL_INITIAL (decl) = init;
5676
5677       /* This will keep us from needing to worry about our obstacks.  */
5678       my_friendly_assert (init != NULL_TREE, 149);
5679       init = NULL_TREE;
5680     }
5681   else if (init)
5682     {
5683       if (TYPE_HAS_CONSTRUCTOR (type) || TYPE_NEEDS_CONSTRUCTING (type))
5684         {
5685           if (TREE_CODE (type) == ARRAY_TYPE)
5686             init = digest_init (type, init, (tree *) 0);
5687           else if (TREE_CODE (init) == CONSTRUCTOR
5688                    && CONSTRUCTOR_ELTS (init) != NULL_TREE)
5689             {
5690               if (TYPE_NEEDS_CONSTRUCTING (type))
5691                 {
5692                   cp_error ("`%D' must be initialized by constructor, not by `{...}'", decl);
5693                   init = error_mark_node;
5694                 }
5695               else
5696                 goto dont_use_constructor;
5697             }
5698 #if 0
5699           /* fix this in `build_functional_cast' instead.
5700              Here's the trigger code:
5701
5702                 struct ostream
5703                 {
5704                   ostream ();
5705                   ostream (int, char *);
5706                   ostream (char *);
5707                   operator char *();
5708                   ostream (void *);
5709                   operator void *();
5710                   operator << (int);
5711                 };
5712                 int buf_size = 1024;
5713                 static char buf[buf_size];
5714                 const char *debug(int i) {
5715                   char *b = &buf[0];
5716                   ostream o = ostream(buf_size, b);
5717                   o << i;
5718                   return buf;
5719                 }
5720                 */
5721
5722           else if (TREE_CODE (init) == TARGET_EXPR
5723                    && TREE_CODE (TREE_OPERAND (init, 1) == NEW_EXPR))
5724             {
5725               /* User wrote something like `foo x = foo (args)'  */
5726               my_friendly_assert (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL, 150);
5727               my_friendly_assert (DECL_NAME (TREE_OPERAND (init, 0)) == NULL_TREE, 151);
5728
5729               /* User wrote exactly `foo x = foo (args)'  */
5730               if (TYPE_MAIN_VARIANT (type) == TREE_TYPE (init))
5731                 {
5732                   init = build (CALL_EXPR, TREE_TYPE (init),
5733                                 TREE_OPERAND (TREE_OPERAND (init, 1), 0),
5734                                 TREE_OPERAND (TREE_OPERAND (init, 1), 1), 0);
5735                   TREE_SIDE_EFFECTS (init) = 1;
5736                 }
5737             }
5738 #endif
5739
5740           /* We must hide the initializer so that expand_decl
5741              won't try to do something it does not understand.  */
5742           if (current_binding_level == global_binding_level)
5743             {
5744               tree value;
5745               if (flag_conserve_space)
5746                 /* If we say -fconserve-space, we want this to save
5747                    space, at the expense of wrong semantics. */
5748                 /* Should this be a NULL_TREE? */
5749                 value = error_mark_node;
5750               else
5751                 /* If we say -fno-conserve-space, we want this to
5752                    produce errors about redefs, to do this we make it
5753                    go in the data space */
5754                 value = digest_init (type, empty_init_node, (tree *) 0);
5755               DECL_INITIAL (decl) = value;
5756             }
5757           else
5758             DECL_INITIAL (decl) = error_mark_node;
5759         }
5760       else
5761         {
5762         dont_use_constructor:
5763           if (TREE_CODE (init) != TREE_VEC)
5764             init = store_init_value (decl, init);
5765
5766           if (init)
5767             /* Don't let anyone try to initialize this variable
5768                until we are ready to do so.  */
5769             DECL_INITIAL (decl) = error_mark_node;
5770         }
5771     }
5772   else if (TREE_CODE_CLASS (TREE_CODE (type)) == 't'
5773            && (IS_AGGR_TYPE (type) || TYPE_NEEDS_CONSTRUCTING (type)))
5774     {
5775       tree ctype = type;
5776       while (TREE_CODE (ctype) == ARRAY_TYPE)
5777         ctype = TREE_TYPE (ctype);
5778       if (! TYPE_NEEDS_CONSTRUCTING (ctype))
5779         {
5780           if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (ctype))
5781             cp_error ("structure `%D' with uninitialized const members", decl);
5782           if (CLASSTYPE_REF_FIELDS_NEED_INIT (ctype))
5783             cp_error ("structure `%D' with uninitialized reference members", decl);
5784         }
5785
5786       if (TREE_CODE (decl) == VAR_DECL
5787           && !DECL_INITIAL (decl)
5788           && !TYPE_NEEDS_CONSTRUCTING (type)
5789           && (TYPE_READONLY (type) || TREE_READONLY (decl)))
5790         cp_error ("uninitialized const `%D'", decl);
5791
5792       /* Initialize variables in need of static initialization
5793          with `empty_init_node' to keep assemble_variable from putting them
5794          in the wrong program space.  (Common storage is okay for non-public
5795          uninitialized data; the linker can't match it with storage from other
5796          files, and we may save some disk space.)  */
5797       if (flag_pic == 0
5798           && TREE_STATIC (decl)
5799           && TREE_PUBLIC (decl)
5800           && ! DECL_EXTERNAL (decl)
5801           && TREE_CODE (decl) == VAR_DECL
5802           && TYPE_NEEDS_CONSTRUCTING (type)
5803           && (DECL_INITIAL (decl) == NULL_TREE
5804               || DECL_INITIAL (decl) == error_mark_node)
5805           /* If we say -fconserve-space, we want this to save space,
5806              at the expense of wrong semantics. */
5807           && ! flag_conserve_space)
5808         {
5809           tree value = digest_init (type, empty_init_node, (tree *) 0);
5810           DECL_INITIAL (decl) = value;
5811         }
5812     }
5813   else if (TREE_CODE (decl) == VAR_DECL
5814            && TREE_CODE (type) != REFERENCE_TYPE
5815            && (TYPE_READONLY (type) || TREE_READONLY (decl)))
5816     {
5817       /* ``Unless explicitly declared extern, a const object does not have
5818          external linkage and must be initialized. ($8.4; $12.1)'' ARM 7.1.6
5819          However, if it's `const int foo = 1; const int foo;', don't complain
5820          about the second decl, since it does have an initializer before.
5821          We deliberately don't complain about arrays, because they're
5822          supposed to be initialized by a constructor.  */
5823       if (! DECL_INITIAL (decl)
5824           && TREE_CODE (type) != ARRAY_TYPE
5825           && (!pedantic || !current_class_type))
5826         cp_error ("uninitialized const `%#D'", decl);
5827     }
5828
5829   /* For top-level declaration, the initial value was read in
5830      the temporary obstack.  MAXINDEX, rtl, etc. to be made below
5831      must go in the permanent obstack; but don't discard the
5832      temporary data yet.  */
5833
5834   if (current_binding_level == global_binding_level && temporary)
5835     end_temporary_allocation ();
5836
5837   /* Deduce size of array from initialization, if not already known.  */
5838
5839   if (TREE_CODE (type) == ARRAY_TYPE
5840       && TYPE_DOMAIN (type) == NULL_TREE
5841       && TREE_CODE (decl) != TYPE_DECL)
5842     {
5843       int do_default
5844         = (TREE_STATIC (decl)
5845            /* Even if pedantic, an external linkage array
5846               may have incomplete type at first.  */
5847            ? pedantic && ! DECL_EXTERNAL (decl)
5848            : !DECL_EXTERNAL (decl));
5849       tree initializer = init ? init : DECL_INITIAL (decl);
5850       int failure = complete_array_type (type, initializer, do_default);
5851
5852       if (failure == 1)
5853         cp_error ("initializer fails to determine size of `%D'", decl);
5854
5855       if (failure == 2)
5856         {
5857           if (do_default)
5858             cp_error ("array size missing in `%D'", decl);
5859           /* If a `static' var's size isn't known, make it extern as
5860              well as static, so it does not get allocated.  If it's not
5861              `static', then don't mark it extern; finish_incomplete_decl
5862              will give it a default size and it will get allocated.  */
5863           else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
5864             DECL_EXTERNAL (decl) = 1;
5865         }
5866
5867       if (pedantic && TYPE_DOMAIN (type) != NULL_TREE
5868           && tree_int_cst_lt (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
5869                               integer_zero_node))
5870         cp_error ("zero-size array `%D'", decl);
5871
5872       layout_decl (decl, 0);
5873     }
5874
5875   if (TREE_CODE (decl) == VAR_DECL)
5876     {
5877       if (DECL_SIZE (decl) == NULL_TREE
5878           && TYPE_SIZE (TREE_TYPE (decl)) != NULL_TREE)
5879         layout_decl (decl, 0);
5880
5881       if (TREE_STATIC (decl) && DECL_SIZE (decl) == NULL_TREE)
5882         {
5883           /* A static variable with an incomplete type:
5884              that is an error if it is initialized.
5885              Otherwise, let it through, but if it is not `extern'
5886              then it may cause an error message later.  */
5887           if (DECL_INITIAL (decl) != NULL_TREE)
5888             cp_error ("storage size of `%D' isn't known", decl);
5889           init = NULL_TREE;
5890         }
5891       else if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
5892         {
5893           /* An automatic variable with an incomplete type: that is an error.
5894              Don't talk about array types here, since we took care of that
5895              message in grokdeclarator.  */
5896           cp_error ("storage size of `%D' isn't known", decl);
5897           TREE_TYPE (decl) = error_mark_node;
5898         }
5899       else if (!DECL_EXTERNAL (decl) && IS_AGGR_TYPE (ttype))
5900         /* Let debugger know it should output info for this type.  */
5901         note_debug_info_needed (ttype);
5902
5903       if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
5904           && DECL_SIZE (decl) != NULL_TREE
5905           && ! TREE_CONSTANT (DECL_SIZE (decl)))
5906         {
5907           if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
5908             constant_expression_warning (DECL_SIZE (decl));
5909           else
5910             cp_error ("storage size of `%D' isn't constant", decl);
5911         }
5912
5913       if (!DECL_EXTERNAL (decl) && TYPE_NEEDS_DESTRUCTOR (type))
5914         {
5915           int yes = suspend_momentary ();
5916
5917           /* If INIT comes from a functional cast, use the cleanup
5918              we built for that.  Otherwise, make our own cleanup.  */
5919           if (init && TREE_CODE (init) == WITH_CLEANUP_EXPR
5920               && comptypes (TREE_TYPE (decl), TREE_TYPE (init), 1))
5921             {
5922               cleanup = TREE_OPERAND (init, 2);
5923               init = TREE_OPERAND (init, 0);
5924               current_binding_level->have_cleanups = 1;
5925               current_binding_level->more_exceptions_ok = 0;
5926             }
5927           else
5928             cleanup = maybe_build_cleanup (decl);
5929           resume_momentary (yes);
5930         }
5931     }
5932   /* PARM_DECLs get cleanups, too.  */
5933   else if (TREE_CODE (decl) == PARM_DECL && TYPE_NEEDS_DESTRUCTOR (type))
5934     {
5935       if (temporary)
5936         end_temporary_allocation ();
5937       cleanup = maybe_build_cleanup (decl);
5938       if (temporary)
5939         resume_temporary_allocation ();
5940     }
5941
5942   /* Output the assembler code and/or RTL code for variables and functions,
5943      unless the type is an undefined structure or union.
5944      If not, it will get done when the type is completed.  */
5945
5946   if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL
5947       || TREE_CODE (decl) == RESULT_DECL)
5948     {
5949       /* ??? FIXME: What about nested classes?  */
5950       int toplev = (current_binding_level == global_binding_level
5951                     || pseudo_global_level_p ());
5952       int was_temp
5953         = ((flag_traditional
5954             || (TREE_STATIC (decl) && TYPE_NEEDS_DESTRUCTOR (type)))
5955            && allocation_temporary_p ());
5956
5957       if (was_temp)
5958         end_temporary_allocation ();
5959
5960       /* If we are in need of a cleanup, get out of any implicit
5961          handlers that have been established so far.  */
5962       if (cleanup && current_binding_level->parm_flag == 3)
5963         {
5964           pop_implicit_try_blocks (decl);
5965           current_binding_level->more_exceptions_ok = 0;
5966         }
5967
5968       if (TREE_CODE (decl) == VAR_DECL
5969           && current_binding_level != global_binding_level
5970           && ! TREE_STATIC (decl)
5971           && type_needs_gc_entry (type))
5972         DECL_GC_OFFSET (decl) = size_int (++current_function_obstack_index);
5973
5974       if (TREE_CODE (decl) == VAR_DECL && DECL_VIRTUAL_P (decl))
5975         make_decl_rtl (decl, NULL_PTR, toplev);
5976       else if (TREE_CODE (decl) == VAR_DECL
5977                && TREE_READONLY (decl)
5978                && DECL_INITIAL (decl) != NULL_TREE
5979                && DECL_INITIAL (decl) != error_mark_node
5980                && DECL_INITIAL (decl) != empty_init_node)
5981         {
5982           DECL_INITIAL (decl) = save_expr (DECL_INITIAL (decl));
5983
5984           if (asmspec)
5985             DECL_ASSEMBLER_NAME (decl) = get_identifier (asmspec);
5986
5987           if (! toplev
5988               && TREE_STATIC (decl)
5989               && ! TREE_SIDE_EFFECTS (decl)
5990               && ! TREE_PUBLIC (decl)
5991               && ! DECL_EXTERNAL (decl)
5992               && ! TYPE_NEEDS_DESTRUCTOR (type)
5993               && DECL_MODE (decl) != BLKmode)
5994             {
5995               /* If this variable is really a constant, then fill its DECL_RTL
5996                  slot with something which won't take up storage.
5997                  If something later should take its address, we can always give
5998                  it legitimate RTL at that time.  */
5999               DECL_RTL (decl) = gen_reg_rtx (DECL_MODE (decl));
6000               store_expr (DECL_INITIAL (decl), DECL_RTL (decl), 0);
6001               TREE_ASM_WRITTEN (decl) = 1;
6002             }
6003           else if (toplev)
6004             {
6005               /* Keep GCC from complaining that this variable
6006                  is defined but never used.  */
6007               TREE_USED (decl) = 1;
6008               /* If this is a static const, change its apparent linkage
6009                  if it belongs to a #pragma interface.  */
6010               if (TREE_STATIC (decl) && !interface_unknown)
6011                 {
6012                   TREE_PUBLIC (decl) = 1;
6013                   DECL_EXTERNAL (decl) = interface_only;
6014                 }
6015               make_decl_rtl (decl, asmspec, toplev);
6016             }
6017           else
6018             rest_of_decl_compilation (decl, asmspec, toplev, 0);
6019         }
6020       else if (TREE_CODE (decl) == VAR_DECL
6021                && DECL_LANG_SPECIFIC (decl)
6022                && DECL_IN_AGGR_P (decl))
6023         {
6024           if (TREE_STATIC (decl))
6025             {
6026               if (init == NULL_TREE
6027 #ifdef DEFAULT_STATIC_DEFS
6028                   /* If this code is dead, then users must
6029                      explicitly declare static member variables
6030                      outside the class def'n as well.  */
6031                   && TYPE_NEEDS_CONSTRUCTING (type)
6032 #endif
6033                   )
6034                 {
6035                   DECL_EXTERNAL (decl) = 1;
6036                   make_decl_rtl (decl, asmspec, 1);
6037                 }
6038               else
6039                 rest_of_decl_compilation (decl, asmspec, toplev, 0);
6040             }
6041           else
6042             /* Just a constant field.  Should not need any rtl.  */
6043             goto finish_end0;
6044         }
6045       else
6046         rest_of_decl_compilation (decl, asmspec, toplev, 0);
6047
6048       if (was_temp)
6049         resume_temporary_allocation ();
6050
6051       if (type != error_mark_node
6052           && TYPE_LANG_SPECIFIC (type)
6053           && CLASSTYPE_ABSTRACT_VIRTUALS (type))
6054         abstract_virtuals_error (decl, type);
6055       else if ((TREE_CODE (type) == FUNCTION_TYPE
6056                 || TREE_CODE (type) == METHOD_TYPE)
6057                && TYPE_LANG_SPECIFIC (TREE_TYPE (type))
6058                && CLASSTYPE_ABSTRACT_VIRTUALS (TREE_TYPE (type)))
6059         abstract_virtuals_error (decl, TREE_TYPE (type));
6060
6061       if (TYPE_LANG_SPECIFIC (type) && IS_SIGNATURE (type))
6062         signature_error (decl, type);
6063       else if ((TREE_CODE (type) == FUNCTION_TYPE
6064                 || TREE_CODE (type) == METHOD_TYPE)
6065                && TYPE_LANG_SPECIFIC (TREE_TYPE (type))
6066                && IS_SIGNATURE (TREE_TYPE (type)))
6067         signature_error (decl, TREE_TYPE (type));
6068
6069       if (TREE_CODE (decl) == FUNCTION_DECL)
6070         {
6071 #if 0
6072           /* C++: Handle overloaded functions with default parameters.  */
6073           if (DECL_OVERLOADED (decl))
6074             {
6075               tree parmtypes = TYPE_ARG_TYPES (type);
6076               tree prev = NULL_TREE;
6077               tree original_name = DECL_NAME (decl);
6078               struct lang_decl *tmp_lang_decl = DECL_LANG_SPECIFIC (decl);
6079               /* All variants will share an uncollectible lang_decl.  */
6080               copy_decl_lang_specific (decl);
6081
6082               while (parmtypes && parmtypes != void_list_node)
6083                 {
6084                   /* The default value for the parameter in parmtypes is
6085                      stored in the TREE_PURPOSE of the TREE_LIST.  */ 
6086                   if (TREE_PURPOSE (parmtypes))
6087                     {
6088                       tree fnname, fndecl;
6089                       tree *argp;
6090
6091                       argp = prev ? & TREE_CHAIN (prev)
6092                         : & TYPE_ARG_TYPES (type);
6093
6094                       *argp = NULL_TREE;
6095                       fnname = build_decl_overload (original_name, TYPE_ARG_TYPES (type), 0);
6096                       *argp = parmtypes;
6097                       fndecl = build_decl (FUNCTION_DECL, fnname, type);
6098                       DECL_EXTERNAL (fndecl) = DECL_EXTERNAL (decl);
6099                       TREE_PUBLIC (fndecl) = TREE_PUBLIC (decl);
6100                       DECL_INLINE (fndecl) = DECL_INLINE (decl);
6101                       /* Keep G++ from thinking this function is unused.
6102                          It is only used to speed up search in name space.  */
6103                       TREE_USED (fndecl) = 1;
6104                       TREE_ASM_WRITTEN (fndecl) = 1;
6105                       DECL_INITIAL (fndecl) = NULL_TREE;
6106                       DECL_LANG_SPECIFIC (fndecl) = DECL_LANG_SPECIFIC (decl);
6107                       fndecl = pushdecl (fndecl);
6108                       DECL_INITIAL (fndecl) = error_mark_node;
6109                       DECL_RTL (fndecl) = DECL_RTL (decl);
6110                     }
6111                   prev = parmtypes;
6112                   parmtypes = TREE_CHAIN (parmtypes);
6113                 }
6114               DECL_LANG_SPECIFIC (decl) = tmp_lang_decl;
6115             }
6116 #endif
6117         }
6118       else if (DECL_EXTERNAL (decl))
6119         ;
6120       else if (TREE_STATIC (decl) && type != error_mark_node)
6121         {
6122           /* Cleanups for static variables are handled by `finish_file'.  */
6123           if (TYPE_NEEDS_CONSTRUCTING (type) || init != NULL_TREE)
6124             expand_static_init (decl, init);
6125           else if (TYPE_NEEDS_DESTRUCTOR (type))
6126             static_aggregates = perm_tree_cons (NULL_TREE, decl,
6127                                                 static_aggregates);
6128
6129           /* Make entry in appropriate vector.  */
6130           if (flag_gc && type_needs_gc_entry (type))
6131             build_static_gc_entry (decl, type);
6132         }
6133       else if (! toplev)
6134         {
6135           /* This is a declared decl which must live until the
6136              end of the binding contour.  It may need a cleanup.  */
6137
6138           /* Recompute the RTL of a local array now
6139              if it used to be an incomplete type.  */
6140           if (was_incomplete && ! TREE_STATIC (decl))
6141             {
6142               /* If we used it already as memory, it must stay in memory.  */
6143               TREE_ADDRESSABLE (decl) = TREE_USED (decl);
6144               /* If it's still incomplete now, no init will save it.  */
6145               if (DECL_SIZE (decl) == NULL_TREE)
6146                 DECL_INITIAL (decl) = NULL_TREE;
6147               expand_decl (decl);
6148             }
6149           else if (! TREE_ASM_WRITTEN (decl)
6150                    && (TYPE_SIZE (type) != NULL_TREE
6151                        || TREE_CODE (type) == ARRAY_TYPE))
6152             {
6153               /* Do this here, because we did not expand this decl's
6154                  rtl in start_decl.  */
6155               if (DECL_RTL (decl) == NULL_RTX)
6156                 expand_decl (decl);
6157               else if (cleanup)
6158                 {
6159                   expand_decl_cleanup (NULL_TREE, cleanup);
6160                   /* Cleanup used up here.  */
6161                   cleanup = NULL_TREE;
6162                 }
6163             }
6164
6165           if (DECL_SIZE (decl) && type != error_mark_node)
6166             {
6167               /* Compute and store the initial value.  */
6168               expand_decl_init (decl);
6169
6170               if (init || TYPE_NEEDS_CONSTRUCTING (type))
6171                 {
6172                   emit_line_note (DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
6173                   expand_aggr_init (decl, init, 0);
6174                 }
6175
6176               /* Set this to 0 so we can tell whether an aggregate
6177                  which was initialized was ever used.  */
6178               if (TYPE_NEEDS_CONSTRUCTING (type))
6179                 TREE_USED (decl) = 0;
6180
6181               /* Store the cleanup, if there was one.  */
6182               if (cleanup)
6183                 {
6184                   if (! expand_decl_cleanup (decl, cleanup))
6185                     cp_error ("parser lost in parsing declaration of `%D'", decl);
6186                 }
6187             }
6188         }
6189     finish_end0:
6190
6191       /* Undo call to `pushclass' that was done in `start_decl'
6192          due to initialization of qualified member variable.
6193          I.e., Foo::x = 10;  */
6194       {
6195         tree context = DECL_CONTEXT (decl);
6196         if (context
6197             && TREE_CODE_CLASS (TREE_CODE (context)) == 't'
6198             && (TREE_CODE (decl) == VAR_DECL
6199                 /* We also have a pushclass done that we need to undo here
6200                    if we're at top level and declare a method.  */
6201                 || (TREE_CODE (decl) == FUNCTION_DECL
6202                     /* If size hasn't been set, we're still defining it,
6203                        and therefore inside the class body; don't pop
6204                        the binding level..  */
6205                     && TYPE_SIZE (context) != NULL_TREE
6206                     /* The binding level gets popped elsewhere for a
6207                        friend declaration inside another class.  */
6208                     /*
6209                     && TYPE_IDENTIFIER (context) == current_class_name
6210                     */
6211                     && context == current_class_type
6212                     )))
6213           popclass (1);
6214       }
6215     }
6216
6217  finish_end:
6218
6219   if (need_pop)
6220     {
6221       /* Resume permanent allocation, if not within a function.  */
6222       /* The corresponding push_obstacks_nochange is in start_decl,
6223          start_method, groktypename, and in grokfield.  */
6224       pop_obstacks ();
6225     }
6226
6227   if (was_readonly)
6228     TREE_READONLY (decl) = 1;
6229
6230   if (flag_cadillac)
6231     cadillac_finish_decl (decl);
6232 }
6233
6234 void
6235 expand_static_init (decl, init)
6236      tree decl;
6237      tree init;
6238 {
6239   tree oldstatic = value_member (decl, static_aggregates);
6240   if (oldstatic)
6241     {
6242       if (TREE_PURPOSE (oldstatic) && init != NULL_TREE)
6243         cp_error ("multiple initializations given for `%D'", decl);
6244     }
6245   else if (current_binding_level != global_binding_level)
6246     {
6247       /* Emit code to perform this initialization but once.  */
6248       tree temp;
6249
6250       /* Remember this information until end of file. */
6251       push_obstacks (&permanent_obstack, &permanent_obstack);
6252
6253       /* Emit code to perform this initialization but once.  */
6254       temp = get_temp_name (integer_type_node, 1);
6255       rest_of_decl_compilation (temp, NULL_PTR, 0, 0);
6256       expand_start_cond (build_binary_op (EQ_EXPR, temp,
6257                                           integer_zero_node, 1), 0);
6258       expand_assignment (temp, integer_one_node, 0, 0);
6259       if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
6260         {
6261           expand_aggr_init (decl, init, 0);
6262           do_pending_stack_adjust ();
6263         }
6264       else
6265         expand_assignment (decl, init, 0, 0);
6266       expand_end_cond ();
6267       if (TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (decl)))
6268         {
6269           static_aggregates = perm_tree_cons (temp, decl, static_aggregates);
6270           TREE_STATIC (static_aggregates) = 1;
6271         }
6272
6273       /* Resume old (possibly temporary) allocation. */
6274       pop_obstacks ();
6275     }
6276   else
6277     {
6278       /* This code takes into account memory allocation
6279          policy of `start_decl'.  Namely, if TYPE_NEEDS_CONSTRUCTING
6280          does not hold for this object, then we must make permanent
6281          the storage currently in the temporary obstack.  */
6282       if (! TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
6283         preserve_initializer ();
6284       static_aggregates = perm_tree_cons (init, decl, static_aggregates);
6285     }
6286 }
6287 \f
6288 /* Make TYPE a complete type based on INITIAL_VALUE.
6289    Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
6290    2 if there was no information (in which case assume 1 if DO_DEFAULT).  */
6291
6292 int
6293 complete_array_type (type, initial_value, do_default)
6294      tree type, initial_value;
6295      int do_default;
6296 {
6297   register tree maxindex = NULL_TREE;
6298   int value = 0;
6299
6300   if (initial_value)
6301     {
6302       /* Note MAXINDEX  is really the maximum index,
6303          one less than the size.  */
6304       if (TREE_CODE (initial_value) == STRING_CST)
6305         maxindex = build_int_2 (TREE_STRING_LENGTH (initial_value) - 1, 0);
6306       else if (TREE_CODE (initial_value) == CONSTRUCTOR)
6307         {
6308           register int nelts
6309             = list_length (CONSTRUCTOR_ELTS (initial_value));
6310           maxindex = build_int_2 (nelts - 1, - (nelts == 0));
6311         }
6312       else
6313         {
6314           /* Make an error message unless that happened already.  */
6315           if (initial_value != error_mark_node)
6316             value = 1;
6317
6318           /* Prevent further error messages.  */
6319           maxindex = build_int_2 (0, 0);
6320         }
6321     }
6322
6323   if (!maxindex)
6324     {
6325       if (do_default)
6326         maxindex = build_int_2 (0, 0);
6327       value = 2;
6328     }
6329
6330   if (maxindex)
6331     {
6332       tree itype;
6333
6334       TYPE_DOMAIN (type) = build_index_type (maxindex);
6335       if (!TREE_TYPE (maxindex))
6336         TREE_TYPE (maxindex) = TYPE_DOMAIN (type);
6337       if (initial_value)
6338         itype = TREE_TYPE (initial_value);
6339       else
6340         itype = NULL;
6341       if (itype && !TYPE_DOMAIN (itype))
6342         TYPE_DOMAIN (itype) = TYPE_DOMAIN (type);
6343     }
6344
6345   /* Lay out the type now that we can get the real answer.  */
6346
6347   layout_type (type);
6348
6349   return value;
6350 }
6351 \f
6352 /* Return zero if something is declared to be a member of type
6353    CTYPE when in the context of CUR_TYPE.  STRING is the error
6354    message to print in that case.  Otherwise, quietly return 1.  */
6355 static int
6356 member_function_or_else (ctype, cur_type, string)
6357      tree ctype, cur_type;
6358      char *string;
6359 {
6360   if (ctype && ctype != cur_type)
6361     {
6362       error (string, TYPE_NAME_STRING (ctype));
6363       return 0;
6364     }
6365   return 1;
6366 }
6367 \f
6368 /* Subroutine of `grokdeclarator'.  */
6369
6370 /* Generate errors possibly applicable for a given set of specifiers.
6371    This is for ARM $7.1.2.  */
6372 static void
6373 bad_specifiers (object, type, virtualp, quals, inlinep, friendp, raises)
6374      tree object;
6375      char *type;
6376      int virtualp, quals, friendp, raises, inlinep;
6377 {
6378   if (virtualp)
6379     cp_error ("`%D' declared as a `virtual' %s", object, type);
6380   if (inlinep)
6381     cp_error ("`%D' declared as an `inline' %s", object, type);
6382   if (quals)
6383     cp_error ("`const' and `volatile' function specifiers on `%D' invalid in %s declaration", object, type);
6384   if (friendp)
6385     cp_error_at ("invalid friend declaration", object);
6386   if (raises)
6387     cp_error_at ("invalid raises declaration", object);
6388 }
6389
6390 /* CTYPE is class type, or null if non-class.
6391    TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
6392    or METHOD_TYPE.
6393    DECLARATOR is the function's name.
6394    VIRTUALP is truthvalue of whether the function is virtual or not.
6395    FLAGS are to be passed through to `grokclassfn'.
6396    QUALS are qualifiers indicating whether the function is `const'
6397    or `volatile'.
6398    RAISES is a list of exceptions that this function can raise.
6399    CHECK is 1 if we must find this method in CTYPE, 0 if we should
6400    not look, and -1 if we should not call `grokclassfn' at all.  */
6401 static tree
6402 grokfndecl (ctype, type, declarator, virtualp, flags, quals, raises, check, publicp)
6403      tree ctype, type;
6404      tree declarator;
6405      int virtualp;
6406      enum overload_flags flags;
6407      tree quals, raises;
6408      int check, publicp;
6409 {
6410   tree cname, decl;
6411   int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
6412
6413   if (ctype)
6414     cname = TREE_CODE (TYPE_NAME (ctype)) == TYPE_DECL
6415       ? TYPE_IDENTIFIER (ctype) : TYPE_NAME (ctype);
6416   else
6417     cname = NULL_TREE;
6418
6419   if (raises)
6420     {
6421       type = build_exception_variant (ctype, type, raises);
6422       raises = TYPE_RAISES_EXCEPTIONS (type);
6423     }
6424   decl = build_lang_decl (FUNCTION_DECL, declarator, type);
6425   /* propagate volatile out from type to decl */
6426   if (TYPE_VOLATILE (type))
6427       TREE_THIS_VOLATILE (decl) = 1;
6428
6429   /* Should probably propagate const out from type to decl I bet (mrs).  */
6430   if (staticp)
6431     {
6432       DECL_STATIC_FUNCTION_P (decl) = 1;
6433       DECL_CONTEXT (decl) = ctype;
6434       DECL_CLASS_CONTEXT (decl) = ctype;
6435     }
6436
6437   if (publicp)
6438     TREE_PUBLIC (decl) = 1;
6439
6440   DECL_EXTERNAL (decl) = 1;
6441   if (quals != NULL_TREE && TREE_CODE (type) == FUNCTION_TYPE)
6442     {
6443       cp_error ("%smember function `%D' cannot have `%T' method qualifier",
6444                 (ctype ? "static " : "non-"), decl, TREE_VALUE (quals));
6445       quals = NULL_TREE;
6446     }
6447
6448   if (IDENTIFIER_OPNAME_P (DECL_NAME (decl)))
6449     grok_op_properties (decl, virtualp, check < 0);
6450
6451   /* Caller will do the rest of this.  */
6452   if (check < 0)
6453     return decl;
6454
6455   if (flags == NO_SPECIAL && ctype && constructor_name (cname) == declarator)
6456     {
6457       tree tmp;
6458       /* Just handle constructors here.  We could do this
6459          inside the following if stmt, but I think
6460          that the code is more legible by breaking this
6461          case out.  See comments below for what each of
6462          the following calls is supposed to do.  */
6463       DECL_CONSTRUCTOR_P (decl) = 1;
6464
6465       grokclassfn (ctype, declarator, decl, flags, quals);
6466       if (check)
6467         check_classfn (ctype, declarator, decl);
6468       grok_ctor_properties (ctype, decl);
6469       if (check == 0)
6470         {
6471           /* FIXME: this should only need to look at IDENTIFIER_GLOBAL_VALUE.  */
6472           tmp = lookup_name (DECL_ASSEMBLER_NAME (decl), 0);
6473           if (tmp == NULL_TREE)
6474             IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (decl)) = decl;
6475           else if (TREE_CODE (tmp) != TREE_CODE (decl))
6476             cp_error ("inconsistent declarations for `%D'", decl);
6477           else
6478             {
6479               duplicate_decls (decl, tmp);
6480               decl = tmp;
6481               /* avoid creating circularities.  */
6482               DECL_CHAIN (decl) = NULL_TREE;
6483             }
6484           make_decl_rtl (decl, NULL_PTR, 1);
6485         }
6486     }
6487   else
6488     {
6489       tree tmp;
6490
6491       /* Function gets the ugly name, field gets the nice one.
6492          This call may change the type of the function (because
6493          of default parameters)!  */
6494       if (ctype != NULL_TREE)
6495         grokclassfn (ctype, cname, decl, flags, quals);
6496
6497       if (ctype != NULL_TREE && check)
6498         check_classfn (ctype, cname, decl);
6499
6500       if (ctype == NULL_TREE || check)
6501         return decl;
6502
6503       /* Now install the declaration of this function so that
6504          others may find it (esp. its DECL_FRIENDLIST).
6505          Pretend we are at top level, we will get true
6506          reference later, perhaps.
6507
6508          FIXME: This should only need to look at IDENTIFIER_GLOBAL_VALUE.  */
6509       tmp = lookup_name (DECL_ASSEMBLER_NAME (decl), 0);
6510       if (tmp == NULL_TREE)
6511         IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (decl)) = decl;
6512       else if (TREE_CODE (tmp) != TREE_CODE (decl))
6513         cp_error ("inconsistent declarations for `%D'", decl);
6514       else
6515         {
6516           duplicate_decls (decl, tmp);
6517           decl = tmp;
6518           /* avoid creating circularities.  */
6519           DECL_CHAIN (decl) = NULL_TREE;
6520         }
6521       make_decl_rtl (decl, NULL_PTR, 1);
6522
6523       /* If this declaration supersedes the declaration of
6524          a method declared virtual in the base class, then
6525          mark this field as being virtual as well.  */
6526       {
6527         tree binfos = BINFO_BASETYPES (TYPE_BINFO (ctype));
6528         int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
6529
6530         for (i = 0; i < n_baselinks; i++)
6531           {
6532             tree base_binfo = TREE_VEC_ELT (binfos, i);
6533             if (TYPE_VIRTUAL_P (BINFO_TYPE (base_binfo)) || flag_all_virtual == 1)
6534               {
6535                 tmp = get_first_matching_virtual (base_binfo, decl,
6536                                                   flags == DTOR_FLAG);
6537                 if (tmp)
6538                   {
6539                     /* If this function overrides some virtual in some base
6540                        class, then the function itself is also necessarily
6541                        virtual, even if the user didn't explicitly say so.  */
6542                     DECL_VIRTUAL_P (decl) = 1;
6543
6544                     /* The TMP we really want is the one from the deepest
6545                        baseclass on this path, taking care not to
6546                        duplicate if we have already found it (via another
6547                        path to its virtual baseclass.  */
6548                     if (staticp)
6549                       {
6550                         cp_error ("method `%D' may not be declared static", decl);
6551                         cp_error_at ("(since `%D' declared virtual in base class.)", tmp);
6552                         break;
6553                       }
6554                     virtualp = 1;
6555
6556 #if 0
6557                     /* Disable this as we want the most recent fndecl, not the most
6558                        base fndecl. */
6559                     if ((TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (base_binfo))
6560                          || TYPE_USES_MULTIPLE_INHERITANCE (ctype))
6561                         && BINFO_TYPE (base_binfo) != DECL_CONTEXT (tmp))
6562                       tmp = get_first_matching_virtual (TYPE_BINFO (DECL_CONTEXT (tmp)),
6563                                                         decl, flags == DTOR_FLAG);
6564 #endif
6565                     if (value_member (tmp, DECL_VINDEX (decl)) == NULL_TREE)
6566                       {
6567                         /* The argument types may have changed... */
6568                         tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
6569                         tree base_variant = TREE_TYPE (TREE_VALUE (argtypes));
6570
6571                         argtypes = commonparms (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (tmp))),
6572                                                 TREE_CHAIN (argtypes));
6573                         /* But the return type has not.  */
6574                         type = build_cplus_method_type (base_variant, TREE_TYPE (type), argtypes);
6575                         if (raises)
6576                           {
6577                             type = build_exception_variant (ctype, type, raises);
6578                             raises = TYPE_RAISES_EXCEPTIONS (type);
6579                           }
6580                         TREE_TYPE (decl) = type;
6581                         DECL_VINDEX (decl)
6582                           = tree_cons (NULL_TREE, tmp, DECL_VINDEX (decl));
6583                       }
6584                   }
6585               }
6586           }
6587       }
6588       if (virtualp)
6589         {
6590           if (DECL_VINDEX (decl) == NULL_TREE)
6591             DECL_VINDEX (decl) = error_mark_node;
6592           IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
6593           if (ctype && CLASSTYPE_VTABLE_NEEDS_WRITING (ctype)
6594               /* If this function is derived from a template, don't
6595                  make it public.  This shouldn't be here, but there's
6596                  no good way to override the interface pragmas for one
6597                  function or class only.  Bletch.  */
6598               && IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (ctype)) == NULL_TREE
6599               && (write_virtuals == 2
6600                   || (write_virtuals == 3
6601                       && CLASSTYPE_INTERFACE_KNOWN (ctype))))
6602             TREE_PUBLIC (decl) = 1;
6603         }
6604     }
6605   return decl;
6606 }
6607
6608 static tree
6609 grokvardecl (type, declarator, specbits, initialized)
6610      tree type;
6611      tree declarator;
6612      RID_BIT_TYPE specbits;
6613      int initialized;
6614 {
6615   tree decl;
6616
6617   if (TREE_CODE (type) == OFFSET_TYPE)
6618     {
6619       /* If you declare a static member so that it
6620          can be initialized, the code will reach here.  */
6621       tree field = lookup_field (TYPE_OFFSET_BASETYPE (type),
6622                                  declarator, 0, 0);
6623       if (field == NULL_TREE || TREE_CODE (field) != VAR_DECL)
6624         {
6625           tree basetype = TYPE_OFFSET_BASETYPE (type);
6626           error ("`%s' is not a static member of class `%s'",
6627                  IDENTIFIER_POINTER (declarator),
6628                  TYPE_NAME_STRING (basetype));
6629           type = TREE_TYPE (type);
6630           decl = build_lang_field_decl (VAR_DECL, declarator, type);
6631           DECL_CONTEXT (decl) = basetype;
6632           DECL_CLASS_CONTEXT (decl) = basetype;
6633         }
6634       else
6635         {
6636           tree f_type = TREE_TYPE (field);
6637           tree o_type = TREE_TYPE (type);
6638
6639           if (TYPE_SIZE (f_type) == NULL_TREE)
6640             {
6641               if (TREE_CODE (f_type) != TREE_CODE (o_type)
6642                   || (TREE_CODE (f_type) == ARRAY_TYPE
6643                       && TREE_TYPE (f_type) != TREE_TYPE (o_type)))
6644                 error ("redeclaration of type for `%s'",
6645                        IDENTIFIER_POINTER (declarator));
6646               else if (TYPE_SIZE (o_type) != NULL_TREE)
6647                 TREE_TYPE (field) = type;
6648             }
6649           else if (f_type != o_type)
6650             error ("redeclaration of type for `%s'",
6651                    IDENTIFIER_POINTER (declarator));
6652           decl = field;
6653           if (initialized && DECL_INITIAL (decl)
6654               /* Complain about multiply-initialized
6655                  member variables, but don't be faked
6656                  out if initializer is faked up from `empty_init_node'.  */
6657               && (TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR
6658                   || CONSTRUCTOR_ELTS (DECL_INITIAL (decl)) != NULL_TREE))
6659             error_with_aggr_type (DECL_CONTEXT (decl),
6660                                   "multiple initializations of static member `%s::%s'",
6661                                   IDENTIFIER_POINTER (DECL_NAME (decl)));
6662         }
6663     }
6664   else
6665     decl = build_decl (VAR_DECL, declarator, type);
6666
6667   if (RIDBIT_SETP (RID_EXTERN, specbits))
6668     {
6669       DECL_THIS_EXTERN (decl) = 1;
6670       DECL_EXTERNAL (decl) = !initialized;
6671     }
6672
6673   /* In class context, static means one per class,
6674      public access, and static storage.  */
6675   if (DECL_FIELD_CONTEXT (decl) != NULL_TREE
6676       && IS_AGGR_TYPE (DECL_FIELD_CONTEXT (decl)))
6677     {
6678       TREE_PUBLIC (decl) = 1;
6679       TREE_STATIC (decl) = 1;
6680       DECL_EXTERNAL (decl) = !initialized;
6681     }
6682   /* At top level, either `static' or no s.c. makes a definition
6683      (perhaps tentative), and absence of `static' makes it public.  */
6684   else if (current_binding_level == global_binding_level)
6685     {
6686       TREE_PUBLIC (decl) = RIDBIT_NOTSETP (RID_STATIC, specbits);
6687       TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
6688     }
6689   /* Not at top level, only `static' makes a static definition.  */
6690   else
6691     {
6692       TREE_STATIC (decl) = !! RIDBIT_SETP (RID_STATIC, specbits);
6693       TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
6694     }
6695   return decl;
6696 }
6697
6698 /* Create a canonical pointer to member function type. */
6699
6700 tree
6701 build_ptrmemfunc_type (type)
6702      tree type;
6703 {
6704   tree fields[4];
6705   tree t;
6706   tree u;
6707
6708   /* If a canonical type already exists for this type, use it.  We use
6709      this method instead of type_hash_canon, because it only does a
6710      simple equality check on the list of field members.  */
6711
6712   if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type)))
6713     return t;
6714
6715   push_obstacks (TYPE_OBSTACK (type), TYPE_OBSTACK (type));
6716
6717   u = make_lang_type (UNION_TYPE);
6718   fields[0] = build_lang_field_decl (FIELD_DECL, pfn_identifier, type);
6719   fields[1] = build_lang_field_decl (FIELD_DECL, delta2_identifier, delta_type_node);
6720   finish_builtin_type (u, "__ptrmemfunc_type", fields, 1, ptr_type_node);
6721   TYPE_NAME (u) = NULL_TREE;
6722
6723   t = make_lang_type (RECORD_TYPE);
6724
6725   /* Let the front-end know this is a pointer to member function. */
6726   TYPE_PTRMEMFUNC_FLAG(t) = 1;
6727
6728   fields[0] = build_lang_field_decl (FIELD_DECL, delta_identifier, delta_type_node);
6729   fields[1] = build_lang_field_decl (FIELD_DECL, index_identifier, delta_type_node);
6730   fields[2] = build_lang_field_decl (FIELD_DECL, pfn_or_delta2_identifier, u);
6731   finish_builtin_type (t, "__ptrmemfunc_type", fields, 2, ptr_type_node);
6732
6733   pop_obstacks ();
6734
6735   /* Zap out the name so that the back-end will give us the debugging
6736      information for this anonymous RECORD_TYPE.  */
6737   TYPE_NAME (t) = NULL_TREE;
6738
6739   TYPE_SET_PTRMEMFUNC_TYPE (type, t);
6740
6741   /* Seems to be wanted. */
6742   CLASSTYPE_GOT_SEMICOLON (t) = 1;
6743   return t;
6744 }
6745
6746 /* Given declspecs and a declarator,
6747    determine the name and type of the object declared
6748    and construct a ..._DECL node for it.
6749    (In one case we can return a ..._TYPE node instead.
6750     For invalid input we sometimes return 0.)
6751
6752    DECLSPECS is a chain of tree_list nodes whose value fields
6753     are the storage classes and type specifiers.
6754
6755    DECL_CONTEXT says which syntactic context this declaration is in:
6756      NORMAL for most contexts.  Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
6757      FUNCDEF for a function definition.  Like NORMAL but a few different
6758       error messages in each case.  Return value may be zero meaning
6759       this definition is too screwy to try to parse.
6760      MEMFUNCDEF for a function definition.  Like FUNCDEF but prepares to
6761       handle member functions (which have FIELD context).
6762       Return value may be zero meaning this definition is too screwy to
6763       try to parse.
6764      PARM for a parameter declaration (either within a function prototype
6765       or before a function body).  Make a PARM_DECL, or return void_type_node.
6766      TYPENAME if for a typename (in a cast or sizeof).
6767       Don't make a DECL node; just return the ..._TYPE node.
6768      FIELD for a struct or union field; make a FIELD_DECL.
6769      BITFIELD for a field with specified width.
6770    INITIALIZED is 1 if the decl has an initializer.
6771
6772    In the TYPENAME case, DECLARATOR is really an absolute declarator.
6773    It may also be so in the PARM case, for a prototype where the
6774    argument type is specified but not the name.
6775
6776    This function is where the complicated C meanings of `static'
6777    and `extern' are interpreted.
6778
6779    For C++, if there is any monkey business to do, the function which
6780    calls this one must do it, i.e., prepending instance variables,
6781    renaming overloaded function names, etc.
6782
6783    Note that for this C++, it is an error to define a method within a class
6784    which does not belong to that class.
6785
6786    Except in the case where SCOPE_REFs are implicitly known (such as
6787    methods within a class being redundantly qualified),
6788    declarations which involve SCOPE_REFs are returned as SCOPE_REFs
6789    (class_name::decl_name).  The caller must also deal with this.
6790
6791    If a constructor or destructor is seen, and the context is FIELD,
6792    then the type gains the attribute TREE_HAS_x.  If such a declaration
6793    is erroneous, NULL_TREE is returned.
6794
6795    QUALS is used only for FUNCDEF and MEMFUNCDEF cases.  For a member
6796    function, these are the qualifiers to give to the `this' pointer.
6797
6798    May return void_type_node if the declarator turned out to be a friend.
6799    See grokfield for details.  */
6800
6801 enum return_types { return_normal, return_ctor, return_dtor, return_conversion };
6802
6803 tree
6804 grokdeclarator (declarator, declspecs, decl_context, initialized, raises)
6805      tree declspecs;
6806      tree declarator;
6807      enum decl_context decl_context;
6808      int initialized;
6809      tree raises;
6810 {
6811   RID_BIT_TYPE specbits;
6812   int nclasses = 0;
6813   tree spec;
6814   tree type = NULL_TREE;
6815   int longlong = 0;
6816   int constp;
6817   int volatilep;
6818   int virtualp, friendp, inlinep, staticp;
6819   int explicit_int = 0;
6820   int explicit_char = 0;
6821   int opaque_typedef = 0;
6822   tree typedef_decl = NULL_TREE;
6823   char *name;
6824   tree typedef_type = NULL_TREE;
6825   int funcdef_flag = 0;
6826   enum tree_code innermost_code = ERROR_MARK;
6827   int bitfield = 0;
6828   int size_varies = 0;
6829   /* Set this to error_mark_node for FIELD_DECLs we could not handle properly.
6830      All FIELD_DECLs we build here have `init' put into their DECL_INITIAL.  */
6831   tree init = NULL_TREE;
6832
6833   /* Keep track of what sort of function is being processed
6834      so that we can warn about default return values, or explicit
6835      return values which do not match prescribed defaults.  */
6836   enum return_types return_type = return_normal;
6837
6838   tree dname = NULL_TREE;
6839   tree ctype = current_class_type;
6840   tree ctor_return_type = NULL_TREE;
6841   enum overload_flags flags = NO_SPECIAL;
6842   int seen_scope_ref = 0;
6843   tree quals = NULL_TREE;
6844
6845   RIDBIT_RESET_ALL (specbits);
6846   if (decl_context == FUNCDEF)
6847     funcdef_flag = 1, decl_context = NORMAL;
6848   else if (decl_context == MEMFUNCDEF)
6849     funcdef_flag = -1, decl_context = FIELD;
6850   else if (decl_context == BITFIELD)
6851     bitfield = 1, decl_context = FIELD;
6852
6853   if (flag_traditional && allocation_temporary_p ())
6854     end_temporary_allocation ();
6855
6856   /* Look inside a declarator for the name being declared
6857      and get it as a string, for an error message.  */
6858   {
6859     tree last = NULL_TREE;
6860     register tree decl = declarator;
6861     name = NULL;
6862
6863     while (decl)
6864       switch (TREE_CODE (decl))
6865         {
6866         case COND_EXPR:
6867           ctype = NULL_TREE;
6868           decl = TREE_OPERAND (decl, 0);
6869           break;
6870
6871         case BIT_NOT_EXPR:      /* for C++ destructors!  */
6872           {
6873             tree name = TREE_OPERAND (decl, 0);
6874             tree rename = NULL_TREE;
6875
6876             my_friendly_assert (flags == NO_SPECIAL, 152);
6877             flags = DTOR_FLAG;
6878             return_type = return_dtor;
6879             my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 153);
6880             if (ctype == NULL_TREE)
6881               {
6882                 if (current_class_type == NULL_TREE)
6883                   {
6884                     error ("destructors must be member functions");
6885                     flags = NO_SPECIAL;
6886                   }
6887                 else
6888                   {
6889                     tree t = constructor_name (current_class_name);
6890                     if (t != name)
6891                       rename = t;
6892                   }
6893               }
6894             else
6895               {
6896                 tree t = constructor_name (ctype);
6897                 if (t != name)
6898                   rename = t;
6899               }
6900
6901             if (rename)
6902               {
6903                 error ("destructor `%s' must match class name `%s'",
6904                        IDENTIFIER_POINTER (name),
6905                        IDENTIFIER_POINTER (rename));
6906                 TREE_OPERAND (decl, 0) = rename;
6907               }
6908             decl = name;
6909           }
6910           break;
6911
6912         case ADDR_EXPR:         /* C++ reference declaration */
6913           /* fall through */
6914         case ARRAY_REF:
6915         case INDIRECT_REF:
6916           ctype = NULL_TREE;
6917           innermost_code = TREE_CODE (decl);
6918           last = decl;
6919           decl = TREE_OPERAND (decl, 0);
6920           break;
6921
6922         case CALL_EXPR:
6923           if (parmlist_is_exprlist (TREE_OPERAND (decl, 1)))
6924             {
6925               /* This is actually a variable declaration using constructor
6926                  syntax.  We need to call start_decl and finish_decl so we
6927                  can get the variable initialized...  */
6928
6929               if (last)
6930                 /* We need to insinuate ourselves into the declarator in place
6931                    of the CALL_EXPR.  */
6932                 TREE_OPERAND (last, 0) = TREE_OPERAND (decl, 0);
6933               else
6934                 declarator = TREE_OPERAND (decl, 0);
6935
6936               init = TREE_OPERAND (decl, 1);
6937
6938               decl = start_decl (declarator, declspecs, 1, NULL_TREE);
6939               finish_decl (decl, init, NULL_TREE, 1);
6940               return 0;
6941             }
6942           innermost_code = TREE_CODE (decl);
6943           decl = TREE_OPERAND (decl, 0);
6944           if (decl_context == FIELD && ctype == NULL_TREE)
6945             ctype = current_class_type;
6946           if (ctype != NULL_TREE
6947               && decl != NULL_TREE && flags != DTOR_FLAG
6948               && decl == constructor_name (ctype))
6949             {
6950               return_type = return_ctor;
6951               ctor_return_type = ctype;
6952             }
6953           ctype = NULL_TREE;
6954           break;
6955
6956         case IDENTIFIER_NODE:
6957           dname = decl;
6958           decl = NULL_TREE;
6959
6960           if (IDENTIFIER_TYPENAME_P (dname))
6961             {
6962               my_friendly_assert (flags == NO_SPECIAL, 154);
6963               flags = TYPENAME_FLAG;
6964               ctor_return_type = TREE_TYPE (dname);
6965               return_type = return_conversion;
6966             }
6967
6968           if (IDENTIFIER_OPNAME_P (dname))
6969             name = operator_name_string (dname);
6970           else
6971             name = IDENTIFIER_POINTER (dname);
6972           break;
6973
6974         case RECORD_TYPE:
6975         case UNION_TYPE:
6976         case ENUMERAL_TYPE:
6977           /* Parse error puts this typespec where
6978              a declarator should go.  */
6979           error ("declarator name missing");
6980           dname = TYPE_NAME (decl);
6981           if (dname && TREE_CODE (dname) == TYPE_DECL)
6982             dname = DECL_NAME (dname);
6983           name = dname ? IDENTIFIER_POINTER (dname) : "<nameless>";
6984           declspecs = temp_tree_cons (NULL_TREE, decl, declspecs);
6985           decl = NULL_TREE;
6986           break;
6987
6988           /* C++ extension */
6989         case SCOPE_REF:
6990 /*
6991           if (seen_scope_ref == 1)
6992             error ("multiple `::' terms in declarator invalid");
6993 */
6994           seen_scope_ref += 1;
6995           {
6996             /* Perform error checking, and convert class names to types.
6997                We may call grokdeclarator multiple times for the same
6998                tree structure, so only do the conversion once.  In this
6999                case, we have exactly what we want for `ctype'.  */
7000             tree cname = TREE_OPERAND (decl, 0);
7001             if (cname == NULL_TREE)
7002               ctype = NULL_TREE;
7003             /* Can't use IS_AGGR_TYPE because CNAME might not be a type.  */
7004             else if (IS_AGGR_TYPE_CODE (TREE_CODE (cname))
7005                      || TREE_CODE (cname) == UNINSTANTIATED_P_TYPE)
7006               ctype = cname;
7007             else if (! is_aggr_typedef (cname, 1))
7008               {
7009                 TREE_OPERAND (decl, 0) = NULL_TREE;
7010               }
7011             /* Must test TREE_OPERAND (decl, 1), in case user gives
7012                us `typedef (class::memfunc)(int); memfunc *memfuncptr;'  */
7013             else if (TREE_OPERAND (decl, 1)
7014                      && TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)
7015               {
7016                 TREE_OPERAND (decl, 0) = IDENTIFIER_TYPE_VALUE (cname);
7017               }
7018             else if (ctype == NULL_TREE)
7019               {
7020                 ctype = IDENTIFIER_TYPE_VALUE (cname);
7021                 TREE_OPERAND (decl, 0) = ctype;
7022               }
7023             else if (TREE_COMPLEXITY (decl) == current_class_depth)
7024               TREE_OPERAND (decl, 0) = ctype;
7025             else
7026               {
7027                 if (! UNIQUELY_DERIVED_FROM_P (IDENTIFIER_TYPE_VALUE (cname),
7028                                                ctype))
7029                   {
7030                     cp_error ("type `%T' is not derived from type `%T'",
7031                               IDENTIFIER_TYPE_VALUE (cname), ctype);
7032                     TREE_OPERAND (decl, 0) = NULL_TREE;
7033                   }
7034                 else
7035                   {
7036                     ctype = IDENTIFIER_TYPE_VALUE (cname);
7037                     TREE_OPERAND (decl, 0) = ctype;
7038                   }
7039               }
7040
7041             decl = TREE_OPERAND (decl, 1);
7042             if (ctype)
7043               {
7044                 if (TREE_CODE (decl) == IDENTIFIER_NODE
7045                     && constructor_name (ctype) == decl)
7046                   {
7047                     return_type = return_ctor;
7048                     ctor_return_type = ctype;
7049                   }
7050                 else if (TREE_CODE (decl) == BIT_NOT_EXPR
7051                          && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
7052                          && constructor_name (ctype) == TREE_OPERAND (decl, 0))
7053                   {
7054                     return_type = return_dtor;
7055                     ctor_return_type = ctype;
7056                     flags = DTOR_FLAG;
7057                     decl = TREE_OPERAND (decl, 0);
7058                   }
7059               }
7060           }
7061           break;
7062
7063         case ERROR_MARK:
7064           decl = NULL_TREE;
7065           break;
7066
7067         default:
7068           return 0; /* We used to do a 155 abort here.  */
7069         }
7070     if (name == NULL)
7071       name = "type name";
7072   }
7073
7074   /* A function definition's declarator must have the form of
7075      a function declarator.  */
7076
7077   if (funcdef_flag && innermost_code != CALL_EXPR)
7078     return 0;
7079
7080   /* Anything declared one level down from the top level
7081      must be one of the parameters of a function
7082      (because the body is at least two levels down).  */
7083
7084   /* This heuristic cannot be applied to C++ nodes! Fixed, however,
7085      by not allowing C++ class definitions to specify their parameters
7086      with xdecls (must be spec.d in the parmlist).
7087
7088      Since we now wait to push a class scope until we are sure that
7089      we are in a legitimate method context, we must set oldcname
7090      explicitly (since current_class_name is not yet alive).  */
7091
7092   if (decl_context == NORMAL
7093       && current_binding_level->level_chain == global_binding_level)
7094     decl_context = PARM;
7095
7096   /* Look through the decl specs and record which ones appear.
7097      Some typespecs are defined as built-in typenames.
7098      Others, the ones that are modifiers of other types,
7099      are represented by bits in SPECBITS: set the bits for
7100      the modifiers that appear.  Storage class keywords are also in SPECBITS.
7101
7102      If there is a typedef name or a type, store the type in TYPE.
7103      This includes builtin typedefs such as `int'.
7104
7105      Set EXPLICIT_INT if the type is `int' or `char' and did not
7106      come from a user typedef.
7107
7108      Set LONGLONG if `long' is mentioned twice.
7109
7110      For C++, constructors and destructors have their own fast treatment.  */
7111
7112   for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
7113     {
7114       register int i;
7115       register tree id;
7116
7117       /* Certain parse errors slip through.  For example,
7118          `int class;' is not caught by the parser. Try
7119          weakly to recover here.  */
7120       if (TREE_CODE (spec) != TREE_LIST)
7121         return 0;
7122
7123       id = TREE_VALUE (spec);
7124
7125       if (TREE_CODE (id) == IDENTIFIER_NODE)
7126         {
7127           if (id == ridpointers[(int) RID_INT])
7128             {
7129               if (type)
7130                 error ("extraneous `int' ignored");
7131               else
7132                 {
7133                   explicit_int = 1;
7134                   type = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (id));
7135                 }
7136               goto found;
7137             }
7138           if (id == ridpointers[(int) RID_CHAR])
7139             {
7140               if (type)
7141                 error ("extraneous `char' ignored");
7142               else
7143                 {
7144                   explicit_char = 1;
7145                   type = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (id));
7146                 }
7147               goto found;
7148             }
7149           if (id == ridpointers[(int) RID_WCHAR])
7150             {
7151               if (type)
7152                 error ("extraneous `__wchar_t' ignored");
7153               else
7154                 {
7155                   type = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (id));
7156                 }
7157               goto found;
7158             }
7159           /* C++ aggregate types. */
7160           if (IDENTIFIER_HAS_TYPE_VALUE (id))
7161             {
7162               if (type)
7163                 cp_error ("multiple declarations `%T' and `%T'", type, id);
7164               else
7165                 type = IDENTIFIER_TYPE_VALUE (id);
7166               goto found;
7167             }
7168
7169           for (i = (int) RID_FIRST_MODIFIER; i < (int) RID_MAX; i++)
7170             {
7171               if (ridpointers[i] == id)
7172                 {
7173                   if (i == (int) RID_LONG && RIDBIT_SETP (i, specbits))
7174                     {
7175 #if 0
7176                       if (pedantic)
7177                         pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
7178                       else
7179 #endif
7180                         if (longlong)
7181                         error ("`long long long' is too long for GCC");
7182                       else
7183                         longlong = 1;
7184                     }
7185                   else if (RIDBIT_SETP (i, specbits))
7186                     warning ("duplicate `%s'", IDENTIFIER_POINTER (id));
7187                   RIDBIT_SET (i, specbits);
7188                   goto found;
7189                 }
7190             }
7191         }
7192       if (type)
7193         error ("two or more data types in declaration of `%s'", name);
7194       else if (TREE_CODE (id) == IDENTIFIER_NODE)
7195         {
7196           register tree t = lookup_name (id, 1);
7197           if (!t || TREE_CODE (t) != TYPE_DECL)
7198             error ("`%s' fails to be a typedef or built in type",
7199                    IDENTIFIER_POINTER (id));
7200           else
7201             {
7202               type = TREE_TYPE (t);
7203               typedef_decl = t;
7204             }
7205         }
7206       else if (TREE_CODE (id) != ERROR_MARK)
7207         /* Can't change CLASS nodes into RECORD nodes here!  */
7208         type = id;
7209
7210     found: ;
7211     }
7212
7213   typedef_type = type;
7214
7215   /* No type at all: default to `int', and set EXPLICIT_INT
7216      because it was not a user-defined typedef.
7217      Except when we have a `typedef' inside a signature, in
7218      which case the type defaults to `unknown type' and is
7219      instantiated when assigning to a signature pointer or ref.  */
7220
7221   if (type == NULL_TREE)
7222     {
7223       explicit_int = -1;
7224       if (return_type == return_dtor)
7225         type = void_type_node;
7226       else if (return_type == return_ctor)
7227         type = TYPE_POINTER_TO (ctor_return_type);
7228       else if (return_type == return_conversion)
7229         type = ctor_return_type;
7230       else if (current_class_type
7231                && IS_SIGNATURE (current_class_type)
7232                && (RIDBIT_SETP (RID_TYPEDEF, specbits)
7233                    || SIGNATURE_GROKKING_TYPEDEF (current_class_type))
7234                && (decl_context == FIELD || decl_context == NORMAL))
7235         {
7236           explicit_int = 0;
7237           opaque_typedef = 1;
7238           type = copy_node (opaque_type_node);
7239         }
7240       else
7241         {
7242           if (funcdef_flag && warn_return_type
7243               && return_type == return_normal
7244               && ! (RIDBIT_SETP (RID_SIGNED, specbits)
7245                     || RIDBIT_SETP (RID_UNSIGNED, specbits)
7246                     || RIDBIT_SETP (RID_LONG, specbits)
7247                     || RIDBIT_SETP (RID_SHORT, specbits)))
7248             warn_about_return_type = 1;
7249           /* Save warning until we know what is really going on.  */
7250           type = integer_type_node;
7251         }
7252     }
7253   else if (return_type == return_dtor)
7254     {
7255       error ("return type specification for destructor invalid");
7256       type = void_type_node;
7257     }
7258   else if (return_type == return_ctor)
7259     {
7260       error ("return type specification for constructor invalid");
7261       type = TYPE_POINTER_TO (ctor_return_type);
7262     }
7263   else if (return_type == return_conversion)
7264     {
7265       if (comp_target_types (type, ctor_return_type, 1) == 0)
7266         cp_error ("operator `%T' declared to return `%T'",
7267                   ctor_return_type, type);
7268       else
7269         cp_pedwarn ("return type specified for `operator %T'",
7270                     ctor_return_type);
7271
7272       type = ctor_return_type;
7273     }
7274
7275   ctype = NULL_TREE;
7276
7277   /* Now process the modifiers that were specified
7278      and check for invalid combinations.  */
7279
7280   /* Long double is a special combination.  */
7281
7282   if (RIDBIT_SETP (RID_LONG, specbits)
7283       && TYPE_MAIN_VARIANT (type) == double_type_node)
7284     {
7285       RIDBIT_RESET (RID_LONG, specbits);
7286       type = build_type_variant (long_double_type_node, TYPE_READONLY (type),
7287                                  TYPE_VOLATILE (type));
7288     }
7289
7290   /* Check all other uses of type modifiers.  */
7291
7292   if (RIDBIT_SETP (RID_UNSIGNED, specbits)
7293       || RIDBIT_SETP (RID_SIGNED, specbits)
7294       || RIDBIT_SETP (RID_LONG, specbits)
7295       || RIDBIT_SETP (RID_SHORT, specbits))
7296     {
7297       int ok = 0;
7298
7299       if (TREE_CODE (type) == REAL_TYPE)
7300         error ("short, signed or unsigned invalid for `%s'", name);
7301       else if (TREE_CODE (type) != INTEGER_TYPE || type == wchar_type_node)
7302         error ("long, short, signed or unsigned invalid for `%s'", name);
7303       else if (RIDBIT_SETP (RID_LONG, specbits)
7304                && RIDBIT_SETP (RID_SHORT, specbits))
7305         error ("long and short specified together for `%s'", name);
7306       else if ((RIDBIT_SETP (RID_LONG, specbits)
7307                 || RIDBIT_SETP (RID_SHORT, specbits))
7308                && explicit_char)
7309         error ("long or short specified with char for `%s'", name);
7310       else if ((RIDBIT_SETP (RID_LONG, specbits)
7311                 || RIDBIT_SETP (RID_SHORT, specbits))
7312                && TREE_CODE (type) == REAL_TYPE)
7313         error ("long or short specified with floating type for `%s'", name);
7314       else if (RIDBIT_SETP (RID_SIGNED, specbits)
7315                && RIDBIT_SETP (RID_UNSIGNED, specbits))
7316         error ("signed and unsigned given together for `%s'", name);
7317       else
7318         {
7319           ok = 1;
7320           if (!explicit_int && !explicit_char && pedantic)
7321             {
7322               pedwarn ("long, short, signed or unsigned used invalidly for `%s'",
7323                        name);
7324               if (flag_pedantic_errors)
7325                 ok = 0;
7326             }
7327         }
7328
7329       /* Discard the type modifiers if they are invalid.  */
7330       if (! ok)
7331         {
7332           RIDBIT_RESET (RID_UNSIGNED, specbits);
7333           RIDBIT_RESET (RID_SIGNED, specbits);
7334           RIDBIT_RESET (RID_LONG, specbits);
7335           RIDBIT_RESET (RID_SHORT, specbits);
7336           longlong = 0;
7337         }
7338     }
7339
7340   /* Decide whether an integer type is signed or not.
7341      Optionally treat bitfields as signed by default.  */
7342   if (RIDBIT_SETP (RID_UNSIGNED, specbits)
7343       /* Traditionally, all bitfields are unsigned.  */
7344       || (bitfield && flag_traditional)
7345       || (bitfield && ! flag_signed_bitfields
7346           && (explicit_int || explicit_char
7347               /* A typedef for plain `int' without `signed'
7348                  can be controlled just like plain `int'.  */
7349               || ! (typedef_decl != NULL_TREE
7350                     && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
7351           && TREE_CODE (type) != ENUMERAL_TYPE
7352           && RIDBIT_NOTSETP (RID_SIGNED, specbits)))
7353     {
7354       if (longlong)
7355         type = long_long_unsigned_type_node;
7356       else if (RIDBIT_SETP (RID_LONG, specbits))
7357         type = long_unsigned_type_node;
7358       else if (RIDBIT_SETP (RID_SHORT, specbits))
7359         type = short_unsigned_type_node;
7360       else if (type == char_type_node)
7361         type = unsigned_char_type_node;
7362       else if (typedef_decl)
7363         type = unsigned_type (type);
7364       else
7365         type = unsigned_type_node;
7366     }
7367   else if (RIDBIT_SETP (RID_SIGNED, specbits)
7368            && type == char_type_node)
7369     type = signed_char_type_node;
7370   else if (longlong)
7371     type = long_long_integer_type_node;
7372   else if (RIDBIT_SETP (RID_LONG, specbits))
7373     type = long_integer_type_node;
7374   else if (RIDBIT_SETP (RID_SHORT, specbits))
7375     type = short_integer_type_node;
7376
7377   /* Set CONSTP if this declaration is `const', whether by
7378      explicit specification or via a typedef.
7379      Likewise for VOLATILEP.  */
7380
7381   constp = !! RIDBIT_SETP (RID_CONST, specbits) + TYPE_READONLY (type);
7382   volatilep = !! RIDBIT_SETP (RID_VOLATILE, specbits) + TYPE_VOLATILE (type);
7383   staticp = 0;
7384   inlinep = !! RIDBIT_SETP (RID_INLINE, specbits);
7385   if (constp > 1)
7386     warning ("duplicate `const'");
7387   if (volatilep > 1)
7388     warning ("duplicate `volatile'");
7389   virtualp = RIDBIT_SETP (RID_VIRTUAL, specbits);
7390
7391   /* operators new and delete are implicitly static.  */
7392   if (RIDBIT_SETP (RID_STATIC, specbits))
7393     staticp = 1 + (decl_context == FIELD);
7394
7395   if (virtualp && staticp == 2)
7396     {
7397       cp_error ("member `%D' cannot be declared both virtual and static",
7398                 dname);
7399       staticp = 0;
7400     }
7401   friendp = RIDBIT_SETP (RID_FRIEND, specbits);
7402   RIDBIT_RESET (RID_VIRTUAL, specbits);
7403   RIDBIT_RESET (RID_FRIEND, specbits);
7404
7405   if (RIDBIT_SETP (RID_MUTABLE, specbits))
7406     {
7407       if (decl_context == PARM)
7408         {
7409           error ("non-member `%s' cannot be declared mutable", name);
7410           RIDBIT_RESET (RID_MUTABLE, specbits);
7411         }
7412       else if (friendp || decl_context == TYPENAME)
7413         {
7414           error ("non-object member `%s' cannot be declared mutable", name);
7415           RIDBIT_RESET (RID_MUTABLE, specbits);
7416         }
7417       else if (staticp)
7418         {
7419           error ("static `%s' cannot be declared mutable", name);
7420           RIDBIT_RESET (RID_MUTABLE, specbits);
7421         }
7422 #if 0
7423       if (RIDBIT_SETP (RID_TYPEDEF, specbits))
7424         {
7425           error ("non-object member `%s' cannot be declared mutable", name);
7426           RIDBIT_RESET (RID_MUTABLE, specbits);
7427         }
7428       /* Because local typedefs are parsed twice, we don't want this
7429          message here. */
7430       else if (decl_context != FIELD)
7431         {
7432           error ("non-member `%s' cannot be declared mutable", name);
7433           RIDBIT_RESET (RID_MUTABLE, specbits);
7434         }
7435 #endif
7436     }
7437
7438   /* Warn if two storage classes are given. Default to `auto'.  */
7439
7440   if (RIDBIT_ANY_SET (specbits))
7441     {
7442       if (RIDBIT_SETP (RID_STATIC, specbits)) nclasses++;
7443       if (RIDBIT_SETP (RID_EXTERN, specbits)) nclasses++;
7444       if (decl_context == PARM && nclasses > 0)
7445         error ("storage class specifiers invalid in parameter declarations");
7446       if (RIDBIT_SETP (RID_TYPEDEF, specbits))
7447         {
7448           if (decl_context == PARM)
7449             error ("typedef declaration invalid in parameter declaration");
7450           nclasses++;
7451         }
7452       if (RIDBIT_SETP (RID_AUTO, specbits)) nclasses++;
7453       if (RIDBIT_SETP (RID_REGISTER, specbits)) nclasses++;
7454     }
7455
7456   /* Give error if `virtual' is used outside of class declaration.  */
7457   if (virtualp && current_class_name == NULL_TREE)
7458     {
7459       error ("virtual outside class declaration");
7460       virtualp = 0;
7461     }
7462   if (current_class_name == NULL_TREE && RIDBIT_SETP (RID_MUTABLE, specbits))
7463     {
7464       error ("only members can be declared mutable");
7465       RIDBIT_RESET (RID_MUTABLE, specbits);
7466     }
7467
7468   /* Static anonymous unions are dealt with here.  */
7469   if (staticp && decl_context == TYPENAME
7470       && TREE_CODE (declspecs) == TREE_LIST
7471       && TREE_CODE (TREE_VALUE (declspecs)) == UNION_TYPE
7472       && ANON_AGGRNAME_P (TYPE_IDENTIFIER (TREE_VALUE (declspecs))))
7473     decl_context = FIELD;
7474
7475   /* Give error if `const,' `volatile,' `inline,' `friend,' or `virtual'
7476      is used in a signature member function declaration.  */
7477   if (decl_context == FIELD
7478       && IS_SIGNATURE (current_class_type)
7479       && RIDBIT_NOTSETP(RID_TYPEDEF, specbits)
7480       && !SIGNATURE_GROKKING_TYPEDEF (current_class_type))
7481     {
7482       if (constp)
7483         {
7484           error ("`const' specified for signature member function `%s'", name);
7485           constp = 0;
7486         }
7487       if (volatilep)
7488         {
7489           error ("`volatile' specified for signature member function `%s'", name);
7490           volatilep = 0;
7491         }
7492       if (inlinep)
7493         {
7494           error ("`inline' specified for signature member function `%s'", name);
7495           /* Later, we'll make signature member functions inline.  */
7496           inlinep = 0;
7497         }
7498       if (friendp)
7499         {
7500           error ("`friend' declaration in signature definition");
7501           friendp = 0;
7502         }
7503       if (virtualp)
7504         {
7505           error ("`virtual' specified for signature member function `%s'", name);
7506           /* Later, we'll make signature member functions virtual.  */
7507           virtualp = 0;
7508         }
7509     }
7510
7511   /* Warn about storage classes that are invalid for certain
7512      kinds of declarations (parameters, typenames, etc.).  */
7513
7514   if (nclasses > 1)
7515     error ("multiple storage classes in declaration of `%s'", name);
7516   else if (decl_context != NORMAL && nclasses > 0)
7517     {
7518       if (decl_context == PARM
7519           && (RIDBIT_SETP (RID_REGISTER, specbits)
7520               || RIDBIT_SETP (RID_AUTO, specbits)))
7521         ;
7522       else if (decl_context == FIELD
7523                && RIDBIT_SETP (RID_TYPEDEF, specbits))
7524         {
7525           /* Processing a typedef declaration nested within a class type
7526              definition.  */
7527           register tree scanner;
7528           register tree previous_declspec;
7529           tree loc_typedecl;
7530   
7531           if (initialized)
7532             error ("typedef declaration includes an initializer");
7533   
7534           /* To process a class-local typedef declaration, we descend down
7535              the chain of declspecs looking for the `typedef' spec.  When
7536              we find it, we replace it with `static', and then recursively
7537              call `grokdeclarator' with the original declarator and with
7538              the newly adjusted declspecs.  This call should return a
7539              FIELD_DECL node with the TREE_TYPE (and other parts) set
7540              appropriately.  We can then just change the TREE_CODE on that
7541              from FIELD_DECL to TYPE_DECL and we're done.  */
7542
7543           for (previous_declspec = NULL_TREE, scanner = declspecs;
7544                scanner;
7545                previous_declspec = scanner, scanner = TREE_CHAIN (scanner))
7546             {
7547               if (TREE_VALUE (scanner) == ridpointers[(int) RID_TYPEDEF])
7548                 break;
7549             }
7550
7551           if (scanner == IDENTIFIER_AS_LIST (ridpointers [(int) RID_TYPEDEF]))
7552             {
7553               if (previous_declspec)
7554                 TREE_CHAIN (previous_declspec)
7555                   = IDENTIFIER_AS_LIST (ridpointers [(int) RID_STATIC]);
7556               else
7557                 declspecs
7558                   = IDENTIFIER_AS_LIST (ridpointers [(int) RID_STATIC]);
7559             }
7560           else
7561             TREE_VALUE (scanner) = ridpointers[(int) RID_STATIC];
7562
7563           /* In the recursive call to grokdeclarator we need to know
7564              whether we are working on a signature-local typedef.  */
7565           if (IS_SIGNATURE (current_class_type))
7566             SIGNATURE_GROKKING_TYPEDEF (current_class_type) = 1;
7567   
7568           loc_typedecl =
7569             grokdeclarator (declarator, declspecs, FIELD, 0, NULL_TREE);
7570   
7571           if (loc_typedecl != error_mark_node)
7572             {
7573               register int i = sizeof (struct lang_decl_flags) / sizeof (int);
7574               register int *pi;
7575   
7576               TREE_SET_CODE (loc_typedecl, TYPE_DECL);
7577   
7578               pi = (int *) permalloc (sizeof (struct lang_decl_flags));
7579               while (i > 0)
7580                 pi[--i] = 0;
7581               DECL_LANG_SPECIFIC (loc_typedecl) = (struct lang_decl *) pi;
7582             }
7583   
7584           if (IS_SIGNATURE (current_class_type))
7585             {
7586               SIGNATURE_GROKKING_TYPEDEF (current_class_type) = 0;
7587               if (loc_typedecl != error_mark_node && opaque_typedef)
7588                 SIGNATURE_HAS_OPAQUE_TYPEDECLS (current_class_type) = 1;
7589             }
7590
7591           return loc_typedecl;
7592         }
7593       else if (decl_context == FIELD
7594                && (! IS_SIGNATURE (current_class_type))
7595                /* C++ allows static class elements  */
7596                && RIDBIT_SETP (RID_STATIC, specbits))
7597         /* C++ also allows inlines and signed and unsigned elements,
7598            but in those cases we don't come in here.  */
7599         ;
7600       else
7601         {
7602           if (decl_context == FIELD)
7603             {
7604               tree tmp = TREE_OPERAND (declarator, 0);
7605               register int op = IDENTIFIER_OPNAME_P (tmp);
7606               error ("storage class specified for %s `%s'",
7607                      IS_SIGNATURE (current_class_type)
7608                      ? (op
7609                         ? "signature member operator"
7610                         : "signature member function")
7611                      : (op ? "member operator" : "structure field"),
7612                      op ? operator_name_string (tmp) : name);
7613             }
7614           else
7615             error ((decl_context == PARM
7616                     ? "storage class specified for parameter `%s'"
7617                     : "storage class specified for typename"), name);
7618           RIDBIT_RESET (RID_REGISTER, specbits);
7619           RIDBIT_RESET (RID_AUTO, specbits);
7620           RIDBIT_RESET (RID_EXTERN, specbits);
7621
7622           if (decl_context == FIELD && IS_SIGNATURE (current_class_type))
7623             {
7624               RIDBIT_RESET (RID_STATIC, specbits);
7625               staticp = 0;
7626             }
7627         }
7628     }
7629   else if (RIDBIT_SETP (RID_EXTERN, specbits) && initialized && !funcdef_flag)
7630     {
7631       if (current_binding_level == global_binding_level)
7632         {
7633           /* It's common practice (and completely legal) to have a const
7634              be initialized and declared extern.  */
7635           if (! constp)
7636             warning ("`%s' initialized and declared `extern'", name);
7637         }
7638       else
7639         error ("`%s' has both `extern' and initializer", name);
7640     }
7641   else if (RIDBIT_SETP (RID_EXTERN, specbits) && funcdef_flag
7642            && current_binding_level != global_binding_level)
7643     error ("nested function `%s' declared `extern'", name);
7644   else if (current_binding_level == global_binding_level)
7645     {
7646       if (RIDBIT_SETP (RID_AUTO, specbits))
7647         error ("top-level declaration of `%s' specifies `auto'", name);
7648 #if 0
7649       if (RIDBIT_SETP (RID_REGISTER, specbits))
7650         error ("top-level declaration of `%s' specifies `register'", name);
7651 #endif
7652 #if 0
7653       /* I'm not sure under what circumstances we should turn
7654          on the extern bit, and under what circumstances we should
7655          warn if other bits are turned on.  */
7656       if (decl_context == NORMAL
7657           && RIDBIT_NOSETP (RID_EXTERN, specbits)
7658           && ! root_lang_context_p ())
7659         {
7660           RIDBIT_SET (RID_EXTERN, specbits);
7661         }
7662 #endif
7663     }
7664
7665   /* Now figure out the structure of the declarator proper.
7666      Descend through it, creating more complex types, until we reach
7667      the declared identifier (or NULL_TREE, in an absolute declarator).  */
7668
7669   while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE)
7670     {
7671       /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
7672          an INDIRECT_REF (for *...),
7673          a CALL_EXPR (for ...(...)),
7674          an identifier (for the name being declared)
7675          or a null pointer (for the place in an absolute declarator
7676          where the name was omitted).
7677          For the last two cases, we have just exited the loop.
7678
7679          For C++ it could also be
7680          a SCOPE_REF (for class :: ...).  In this case, we have converted
7681          sensible names to types, and those are the values we use to
7682          qualify the member name.
7683          an ADDR_EXPR (for &...),
7684          a BIT_NOT_EXPR (for destructors)
7685
7686          At this point, TYPE is the type of elements of an array,
7687          or for a function to return, or for a pointer to point to.
7688          After this sequence of ifs, TYPE is the type of the
7689          array or function or pointer, and DECLARATOR has had its
7690          outermost layer removed.  */
7691
7692       if (TREE_CODE (type) == ERROR_MARK)
7693         {
7694           if (TREE_CODE (declarator) == SCOPE_REF)
7695             declarator = TREE_OPERAND (declarator, 1);
7696           else
7697             declarator = TREE_OPERAND (declarator, 0);
7698           continue;
7699         }
7700       if (quals != NULL_TREE
7701           && (declarator == NULL_TREE
7702               || TREE_CODE (declarator) != SCOPE_REF))
7703         {
7704           if (ctype == NULL_TREE && TREE_CODE (type) == METHOD_TYPE)
7705             ctype = TYPE_METHOD_BASETYPE (type);
7706           if (ctype != NULL_TREE)
7707             {
7708 #if 0 /* not yet, should get fixed properly later */
7709               tree dummy = make_type_decl (NULL_TREE, type);
7710 #else
7711               tree dummy = build_decl (TYPE_DECL, NULL_TREE, type);
7712 #endif
7713               ctype = grok_method_quals (ctype, dummy, quals);
7714               type = TREE_TYPE (dummy);
7715               quals = NULL_TREE;
7716             }
7717         }
7718       switch (TREE_CODE (declarator))
7719         {
7720         case ARRAY_REF:
7721           {
7722             register tree itype = NULL_TREE;
7723             register tree size = TREE_OPERAND (declarator, 1);
7724
7725             declarator = TREE_OPERAND (declarator, 0);
7726
7727             /* Check for some types that there cannot be arrays of.  */
7728
7729             if (TYPE_MAIN_VARIANT (type) == void_type_node)
7730               {
7731                 cp_error ("declaration of `%D' as array of voids", dname);
7732                 type = error_mark_node;
7733               }
7734
7735             if (TREE_CODE (type) == FUNCTION_TYPE)
7736               {
7737                 cp_error ("declaration of `%D' as array of functions", dname);
7738                 type = error_mark_node;
7739               }
7740
7741             /* ARM $8.4.3: Since you can't have a pointer to a reference,
7742                you can't have arrays of references.  If we allowed them,
7743                then we'd be saying x[i] is legal for an array x, but
7744                then you'd have to ask: what does `*(x + i)' mean?  */
7745             if (TREE_CODE (type) == REFERENCE_TYPE)
7746               {
7747                 if (decl_context == TYPENAME)
7748                   cp_error ("cannot make arrays of references");
7749                 else
7750                   cp_error ("declaration of `%D' as array of references",
7751                             dname);
7752                 type = error_mark_node;
7753               }
7754
7755             if (TREE_CODE (type) == OFFSET_TYPE)
7756               {
7757                   cp_error ("declaration of `%D' as array of data members",
7758                             dname);
7759                 type = error_mark_node;
7760               }
7761
7762             if (TREE_CODE (type) == METHOD_TYPE)
7763               {
7764                 cp_error ("declaration of `%D' as array of function members",
7765                           dname);
7766                 type = error_mark_node;
7767               }
7768
7769             if (size == error_mark_node)
7770               type = error_mark_node;
7771
7772             if (type == error_mark_node)
7773               continue;
7774
7775             if (size)
7776               {
7777                 /* Must suspend_momentary here because the index
7778                    type may need to live until the end of the function.
7779                    For example, it is used in the declaration of a
7780                    variable which requires destructing at the end of
7781                    the function; then build_vec_delete will need this
7782                    value.  */
7783                 int yes = suspend_momentary ();
7784                 /* might be a cast */
7785                 if (TREE_CODE (size) == NOP_EXPR
7786                     && TREE_TYPE (size) == TREE_TYPE (TREE_OPERAND (size, 0)))
7787                   size = TREE_OPERAND (size, 0);
7788
7789                 /* If this is a template parameter, it'll be constant, but
7790                    we don't know what the value is yet.  */
7791                 if (TREE_CODE (size) == TEMPLATE_CONST_PARM)
7792                   goto dont_grok_size;
7793
7794                 if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE
7795                     && TREE_CODE (TREE_TYPE (size)) != ENUMERAL_TYPE)
7796                   {
7797                     cp_error ("size of array `%D' has non-integer type",
7798                               dname);
7799                     size = integer_one_node;
7800                   }
7801                 if (TREE_READONLY_DECL_P (size))
7802                   size = decl_constant_value (size);
7803                 if (pedantic && integer_zerop (size))
7804                   cp_pedwarn ("ANSI C++ forbids zero-size array `%D'", dname);
7805                 if (TREE_CONSTANT (size))
7806                   {
7807                     constant_expression_warning (size);
7808                     if (INT_CST_LT (size, integer_zero_node))
7809                       {
7810                         cp_error ("size of array `%D' is negative", dname);
7811                         size = integer_one_node;
7812                       }
7813                     itype = build_index_type (size_binop (MINUS_EXPR, size,
7814                                                           integer_one_node));
7815                   }
7816                 else
7817                   {
7818                     if (pedantic)
7819                       cp_pedwarn ("ANSI C++ forbids variable-size array `%D'", dname);
7820                   dont_grok_size:
7821                     itype =
7822                       build_binary_op (MINUS_EXPR, size, integer_one_node, 1);
7823                     /* Make sure the array size remains visibly nonconstant
7824                        even if it is (eg) a const variable with known value.  */
7825                     size_varies = 1;
7826                     itype = variable_size (itype);
7827                     itype = build_index_type (itype);
7828                   }
7829                 resume_momentary (yes);
7830               }
7831
7832           /* Build the array type itself, then merge any constancy or
7833              volatility into the target type.  We must do it in this order
7834              to ensure that the TYPE_MAIN_VARIANT field of the array type
7835              is set correctly.  */
7836
7837             type = build_cplus_array_type (type, itype);
7838             if (constp || volatilep)
7839               /* Should this be c_build_type_variant? -jason */
7840               type = build_type_variant (type, constp, volatilep);
7841
7842             ctype = NULL_TREE;
7843           }
7844           break;
7845
7846         case CALL_EXPR:
7847           {
7848             tree arg_types;
7849
7850             /* Declaring a function type.
7851                Make sure we have a valid type for the function to return.  */
7852 #if 0
7853             /* Is this an error?  Should they be merged into TYPE here?  */
7854             if (pedantic && (constp || volatilep))
7855               pedwarn ("function declared to return const or volatile result");
7856 #else
7857             /* Merge any constancy or volatility into the target type
7858                for the pointer.  */
7859
7860             if (constp || volatilep)
7861               {
7862                 type = build_type_variant (type, constp, volatilep);
7863                 if (IS_AGGR_TYPE (type))
7864                   build_pointer_type (type);
7865                 constp = 0;
7866                 volatilep = 0;
7867               }
7868 #endif
7869
7870             /* Warn about some types functions can't return.  */
7871
7872             if (TREE_CODE (type) == FUNCTION_TYPE)
7873               {
7874                 error ("`%s' declared as function returning a function", name);
7875                 type = integer_type_node;
7876               }
7877             if (TREE_CODE (type) == ARRAY_TYPE)
7878               {
7879                 error ("`%s' declared as function returning an array", name);
7880                 type = integer_type_node;
7881               }
7882
7883             if (ctype == NULL_TREE
7884                 && decl_context == FIELD
7885                 && (friendp == 0 || dname == current_class_name))
7886               ctype = current_class_type;
7887
7888             if (ctype && return_type == return_conversion)
7889               TYPE_HAS_CONVERSION (ctype) = 1;
7890             if (ctype && constructor_name (ctype) == dname)
7891               {
7892                 /* We are within a class's scope. If our declarator name
7893                    is the same as the class name, and we are defining
7894                    a function, then it is a constructor/destructor, and
7895                    therefore returns a void type.  */
7896
7897                 if (flags == DTOR_FLAG)
7898                   {
7899                     /* ANSI C++ June 5 1992 WP 12.4.1.  A destructor may
7900                        not be declared const or volatile.  A destructor
7901                        may not be static.  */
7902                     if (staticp == 2)
7903                       error ("destructor cannot be static member function");
7904                     if (TYPE_READONLY (type))
7905                       {
7906                         error ("destructors cannot be declared `const'");
7907                         return void_type_node;
7908                       }
7909                     if (TYPE_VOLATILE (type))
7910                       {
7911                         error ("destructors cannot be declared `volatile'");
7912                         return void_type_node;
7913                       }
7914                     if (decl_context == FIELD)
7915                       {
7916                         if (! member_function_or_else (ctype, current_class_type,
7917                                                        "destructor for alien class `%s' cannot be a member"))
7918                           return void_type_node;
7919                       }
7920                   }
7921                 else            /* it's a constructor. */
7922                   {
7923                     /* ANSI C++ June 5 1992 WP 12.1.2.  A constructor may
7924                        not be declared const or volatile.  A constructor may
7925                        not be virtual.  A constructor may not be static.  */
7926                     if (staticp == 2)
7927                       error ("constructor cannot be static member function");
7928                     if (virtualp)
7929                       {
7930                         pedwarn ("constructors cannot be declared virtual");
7931                         virtualp = 0;
7932                       }
7933                     if (TYPE_READONLY (type))
7934                       {
7935                         error ("constructors cannot be declared `const'");
7936                         return void_type_node;
7937                       }
7938                     if (TYPE_VOLATILE (type))
7939                       {
7940                         error ("constructors cannot be declared `volatile'");
7941                         return void_type_node;
7942                       }
7943                     {
7944                       RID_BIT_TYPE tmp_bits;
7945                       bcopy ((void*)&specbits, (void*)&tmp_bits, sizeof(RID_BIT_TYPE));
7946                       RIDBIT_RESET (RID_INLINE, tmp_bits);
7947                       RIDBIT_RESET (RID_STATIC, tmp_bits);
7948                       if (RIDBIT_ANY_SET (tmp_bits))
7949                         error ("return value type specifier for constructor ignored");
7950                     }
7951                     type = TYPE_POINTER_TO (ctype);
7952                     if (decl_context == FIELD &&
7953                         IS_SIGNATURE (current_class_type))
7954                       {
7955                         error ("constructor not allowed in signature");
7956                         return void_type_node;
7957                       }                   
7958                     else if (decl_context == FIELD)
7959                       {
7960                         if (! member_function_or_else (ctype, current_class_type,
7961                                                        "constructor for alien class `%s' cannot be member"))
7962                           return void_type_node;
7963                         TYPE_HAS_CONSTRUCTOR (ctype) = 1;
7964                         if (return_type != return_ctor)
7965                           return NULL_TREE;
7966                       }
7967                   }
7968                 if (decl_context == FIELD)
7969                   staticp = 0;
7970               }
7971             else if (friendp && virtualp)
7972               {
7973                 /* Cannot be both friend and virtual.  */
7974                 error ("virtual functions cannot be friends");
7975                 RIDBIT_RESET (RID_FRIEND, specbits);
7976                 friendp = 0;
7977               }
7978
7979             if (decl_context == NORMAL && friendp)
7980               error ("friend declaration not in class definition");
7981
7982             /* Pick up type qualifiers which should be applied to `this'.  */
7983             quals = TREE_OPERAND (declarator, 2);
7984
7985             /* Traditionally, declaring return type float means double.  */
7986
7987             if (flag_traditional
7988                 && TYPE_MAIN_VARIANT (type) == float_type_node)
7989               {
7990                 type = build_type_variant (double_type_node,
7991                                            TYPE_READONLY (type),
7992                                            TYPE_VOLATILE (type));
7993               }
7994
7995             /* Construct the function type and go to the next
7996                inner layer of declarator.  */
7997
7998             {
7999               int funcdef_p;
8000               tree inner_parms = TREE_OPERAND (declarator, 1);
8001               tree inner_decl = TREE_OPERAND (declarator, 0);
8002
8003               declarator = TREE_OPERAND (declarator, 0);
8004
8005               if (inner_decl && TREE_CODE (inner_decl) == SCOPE_REF)
8006                 inner_decl = TREE_OPERAND (inner_decl, 1);
8007
8008               /* Say it's a definition only for the CALL_EXPR
8009                  closest to the identifier.  */
8010               funcdef_p =
8011                 (inner_decl && TREE_CODE (inner_decl) == IDENTIFIER_NODE)
8012                   ? funcdef_flag : 0;
8013
8014               /* FIXME: This is where default args should be fully
8015                  processed.  */
8016
8017               arg_types = grokparms (inner_parms, funcdef_p);
8018             }
8019
8020             if (declarator)
8021               {
8022                 /* Get past destructors, etc.
8023                    We know we have one because FLAGS will be non-zero.
8024
8025                    Complain about improper parameter lists here.  */
8026                 if (TREE_CODE (declarator) == BIT_NOT_EXPR)
8027                   {
8028                     declarator = TREE_OPERAND (declarator, 0);
8029
8030                     if (strict_prototype == 0 && arg_types == NULL_TREE)
8031                       arg_types = void_list_node;
8032                     else if (arg_types == NULL_TREE
8033                              || arg_types != void_list_node)
8034                       {
8035                         error ("destructors cannot be specified with parameters");
8036                         arg_types = void_list_node;
8037                       }
8038                   }
8039               }
8040
8041             /* ANSI seems to say that `const int foo ();'
8042                does not make the function foo const.  */
8043             type = build_function_type (type,
8044                                         flag_traditional ? 0 : arg_types);
8045           }
8046           break;
8047
8048         case ADDR_EXPR:
8049         case INDIRECT_REF:
8050           /* Filter out pointers-to-references and references-to-references.
8051              We can get these if a TYPE_DECL is used.  */
8052
8053           if (TREE_CODE (type) == REFERENCE_TYPE)
8054             {
8055               error ("cannot declare %s to references",
8056                      TREE_CODE (declarator) == ADDR_EXPR
8057                      ? "references" : "pointers");
8058               declarator = TREE_OPERAND (declarator, 0);
8059               continue;
8060             }
8061
8062           /* Merge any constancy or volatility into the target type
8063              for the pointer.  */
8064
8065           if (constp || volatilep)
8066             {
8067               /* A const or volatile signature pointer/reference is
8068                  pointing to a const or volatile object, i.e., the
8069                  `optr' is const or volatile, respectively, not the
8070                  signature pointer/reference itself.  */
8071               if (! IS_SIGNATURE (type))
8072                 {
8073                   type = build_type_variant (type, constp, volatilep);
8074                   if (IS_AGGR_TYPE (type))
8075                     build_pointer_type (type);
8076                   constp = 0;
8077                   volatilep = 0;
8078                 }
8079             }
8080
8081           if (IS_SIGNATURE (type))
8082             {
8083               if (TREE_CODE (declarator) == ADDR_EXPR)
8084                 {
8085                   if (CLASSTYPE_METHOD_VEC (type) == NULL_TREE)
8086                     warning ("empty signature `%s' used in signature reference declaration",
8087                              TYPE_NAME_STRING(type));
8088 #if 0
8089                   type = build_signature_reference_type (type,
8090                                                          constp, volatilep);
8091 #else
8092                   sorry ("signature reference");
8093                   return NULL_TREE;
8094 #endif
8095                 }
8096               else
8097                 {
8098                   if (CLASSTYPE_METHOD_VEC (type) == NULL_TREE)
8099                     warning ("empty signature `%s' used in signature pointer declaration",
8100                              TYPE_NAME_STRING(type));
8101                   type = build_signature_pointer_type (type,
8102                                                        constp, volatilep);
8103                 }
8104               constp = 0;
8105               volatilep = 0;
8106             }
8107           else if (TREE_CODE (declarator) == ADDR_EXPR)
8108             {
8109               if (TREE_CODE (type) == FUNCTION_TYPE)
8110                 {
8111                   error ("cannot declare references to functions; use pointer to function instead");
8112                   type = build_pointer_type (type);
8113                 }
8114               else
8115                 {
8116                   if (TYPE_MAIN_VARIANT (type) == void_type_node)
8117                     error ("invalid type: `void &'");
8118                   else
8119                     type = build_reference_type (type);
8120                 }
8121             }
8122           else if (TREE_CODE (type) == METHOD_TYPE)
8123             {
8124               type = build_ptrmemfunc_type (build_pointer_type (type));
8125             }
8126           else
8127             type = build_pointer_type (type);
8128
8129           /* Process a list of type modifier keywords (such as
8130              const or volatile) that were given inside the `*' or `&'.  */
8131
8132           if (TREE_TYPE (declarator))
8133             {
8134               register tree typemodlist;
8135               int erred = 0;
8136               for (typemodlist = TREE_TYPE (declarator); typemodlist;
8137                    typemodlist = TREE_CHAIN (typemodlist))
8138                 {
8139                   if (TREE_VALUE (typemodlist) == ridpointers[(int) RID_CONST])
8140                     constp++;
8141                   else if (TREE_VALUE (typemodlist) == ridpointers[(int) RID_VOLATILE])
8142                     volatilep++;
8143                   else if (!erred)
8144                     {
8145                       erred = 1;
8146                       error ("invalid type modifier within %s declarator",
8147                              TREE_CODE (declarator) == ADDR_EXPR
8148                              ? "reference" : "pointer");
8149                     }
8150                 }
8151               if (constp > 1)
8152                 warning ("duplicate `const'");
8153               if (volatilep > 1)
8154                 warning ("duplicate `volatile'");
8155             }
8156           declarator = TREE_OPERAND (declarator, 0);
8157           ctype = NULL_TREE;
8158           break;
8159
8160         case SCOPE_REF:
8161           {
8162             /* We have converted type names to NULL_TREE if the
8163                name was bogus, or to a _TYPE node, if not.
8164
8165                The variable CTYPE holds the type we will ultimately
8166                resolve to.  The code here just needs to build
8167                up appropriate member types.  */
8168             tree sname = TREE_OPERAND (declarator, 1);
8169             /* Destructors can have their visibilities changed as well.  */
8170             if (TREE_CODE (sname) == BIT_NOT_EXPR)
8171               sname = TREE_OPERAND (sname, 0);
8172
8173             if (TREE_COMPLEXITY (declarator) == 0)
8174               /* This needs to be here, in case we are called
8175                  multiple times.  */ ;
8176             else if (friendp && (TREE_COMPLEXITY (declarator) < 2))
8177               /* don't fall out into global scope. Hides real bug? --eichin */ ;
8178             else if (TREE_COMPLEXITY (declarator) == current_class_depth)
8179               {
8180                 /* I'm not really sure what pushclass calls this popclass
8181                    corresponds to.  One is in build_push_scope and that has
8182                    been changed to a push_nested_class call, that's why I
8183                    try to use pop_nested_class here instead.
8184                    -niklas@appli.se */
8185                 pop_nested_class (1);
8186                 TREE_COMPLEXITY (declarator) = current_class_depth;
8187               }
8188             else
8189               my_friendly_abort (16);
8190
8191             if (TREE_OPERAND (declarator, 0) == NULL_TREE)
8192               {
8193                 /* We had a reference to a global decl, or
8194                    perhaps we were given a non-aggregate typedef,
8195                    in which case we cleared this out, and should just
8196                    keep going as though it wasn't there.  */
8197                 declarator = sname;
8198                 continue;
8199               }
8200             ctype = TREE_OPERAND (declarator, 0);
8201
8202             if (sname == NULL_TREE)
8203               goto done_scoping;
8204
8205             if (TREE_CODE (sname) == IDENTIFIER_NODE)
8206               {
8207                 /* This is the `standard' use of the scoping operator:
8208                    basetype :: member .  */
8209
8210                 if (TREE_CODE (type) == FUNCTION_TYPE)
8211                   {
8212                     if (current_class_type == NULL_TREE
8213                         || TYPE_MAIN_VARIANT (ctype) == current_class_type
8214                         || friendp)
8215                       type = build_cplus_method_type (build_type_variant (ctype, constp, volatilep),
8216                                                       TREE_TYPE (type), TYPE_ARG_TYPES (type));
8217                     else
8218                       {
8219                         error ("cannot declare member function `%s::%s' within this class",
8220                                TYPE_NAME_STRING (ctype), name);
8221                         return void_type_node;
8222                       }
8223                   }
8224                 else if (TYPE_MAIN_VARIANT (ctype) == current_class_type)
8225                   {
8226                     if (extra_warnings)
8227                       warning ("extra qualification `%s' on member `%s' ignored",
8228                                TYPE_NAME_STRING (ctype), name);
8229                     type = build_offset_type (ctype, type);
8230                   }
8231                 else if (TYPE_SIZE (ctype) != NULL_TREE
8232                          || (RIDBIT_SETP (RID_TYPEDEF, specbits)))
8233                   {
8234                     tree t;
8235                     /* have to move this code elsewhere in this function.
8236                        this code is used for i.e., typedef int A::M; M *pm; */
8237
8238                     if (explicit_int == -1 && decl_context == FIELD
8239                         && funcdef_flag == 0)
8240                       {
8241                         /* The code in here should only be used to build
8242                            stuff that will be grokked as access decls.  */
8243                         t = lookup_field (ctype, sname, 0, 0);
8244                         if (t)
8245                           {
8246                             t = build_lang_field_decl (FIELD_DECL, build_nt (SCOPE_REF, ctype, t), type);
8247                             DECL_INITIAL (t) = init;
8248                             return t;
8249                           }
8250                         /* No such field, try member functions.  */
8251                         t = lookup_fnfields (TYPE_BINFO (ctype), sname, 0);
8252                         if (t)
8253                           {
8254                             if (flags == DTOR_FLAG)
8255                               t = TREE_VALUE (t);
8256                             else if (CLASSTYPE_METHOD_VEC (ctype)
8257                                      && TREE_VALUE (t) == TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (ctype), 0))
8258                               {
8259                                 /* Don't include destructor with constructors.  */
8260                                 t = DECL_CHAIN (TREE_VALUE (t));
8261                                 if (t == NULL_TREE)
8262                                   error ("class `%s' does not have any constructors", IDENTIFIER_POINTER (sname));
8263                                 t = build_tree_list (NULL_TREE, t);
8264                               }
8265                             t = build_lang_field_decl (FIELD_DECL, build_nt (SCOPE_REF, ctype, t), type);
8266                             DECL_INITIAL (t) = init;
8267                             return t;
8268                           }
8269
8270                         cp_error
8271                           ("field `%D' is not a member of structure `%T'",
8272                            sname, ctype);
8273                       }
8274
8275                     if (current_class_type)
8276                       {
8277                         if (TYPE_MAIN_VARIANT (ctype) != current_class_type)
8278                           {
8279                             cp_error ("cannot declare member `%T::%s' within `%T'",
8280                                    ctype, name, current_class_type);
8281                             return void_type_node;
8282                           }
8283                         else if (extra_warnings)
8284                           cp_warning ("extra qualification `%T' on member `%s' ignored",
8285                                    ctype, name);
8286                       }
8287                     type = build_offset_type (ctype, type);
8288                   }
8289                 else if (uses_template_parms (ctype))
8290                   {
8291                     enum tree_code c;
8292                     if (TREE_CODE (type) == FUNCTION_TYPE)
8293                       {
8294                         type = build_cplus_method_type (build_type_variant (ctype, constp, volatilep),
8295                                                         TREE_TYPE (type),
8296                                                         TYPE_ARG_TYPES (type));
8297                         c = FUNCTION_DECL;
8298                       }
8299                   }
8300                 else
8301                   {
8302                     cp_error ("structure `%T' not yet defined", ctype);
8303                     return error_mark_node;
8304                   }
8305
8306                 declarator = sname;
8307               }
8308             else if (TREE_CODE (sname) == SCOPE_REF)
8309               my_friendly_abort (17);
8310             else
8311               {
8312               done_scoping:
8313                 declarator = TREE_OPERAND (declarator, 1);
8314                 if (declarator && TREE_CODE (declarator) == CALL_EXPR)
8315                   /* In this case, we will deal with it later.  */
8316                   ;
8317                 else
8318                   {
8319                     if (TREE_CODE (type) == FUNCTION_TYPE)
8320                       type = build_cplus_method_type (build_type_variant (ctype, constp, volatilep), TREE_TYPE (type), TYPE_ARG_TYPES (type));
8321                     else
8322                       type = build_offset_type (ctype, type);
8323                   }
8324               }
8325           }
8326           break;
8327
8328         case BIT_NOT_EXPR:
8329           declarator = TREE_OPERAND (declarator, 0);
8330           break;
8331
8332         case RECORD_TYPE:
8333         case UNION_TYPE:
8334         case ENUMERAL_TYPE:
8335           declarator = NULL_TREE;
8336           break;
8337
8338         case ERROR_MARK:
8339           declarator = NULL_TREE;
8340           break;
8341
8342         default:
8343           my_friendly_abort (158);
8344         }
8345     }
8346
8347   /* Now TYPE has the actual type.  */
8348
8349   /* If this is declaring a typedef name, return a TYPE_DECL.  */
8350
8351   if (RIDBIT_SETP (RID_TYPEDEF, specbits))
8352     {
8353       tree decl;
8354
8355       /* Note that the grammar rejects storage classes
8356          in typenames, fields or parameters.  */
8357       if (constp || volatilep)
8358         type = build_type_variant (type, constp, volatilep);
8359
8360       /* If the user declares "struct {...} foo" then `foo' will have
8361          an anonymous name.  Fill that name in now.  Nothing can
8362          refer to it, so nothing needs know about the name change.
8363          The TYPE_NAME field was filled in by build_struct_xref.  */
8364       if (type != error_mark_node
8365           && TYPE_NAME (type)
8366           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8367           && ANON_AGGRNAME_P (TYPE_IDENTIFIER (type)))
8368         {
8369           /* replace the anonymous name with the real name everywhere.  */
8370           lookup_tag_reverse (type, declarator);
8371           TYPE_IDENTIFIER (type) = declarator;
8372
8373           if (TYPE_LANG_SPECIFIC (type))
8374             TYPE_WAS_ANONYMOUS (type) = 1;
8375
8376           {
8377             tree d = TYPE_NAME (type), c = DECL_CONTEXT (d);
8378
8379             if (!c)
8380               set_nested_typename (d, 0, declarator, type);
8381             else if (TREE_CODE (c) == FUNCTION_DECL)
8382               set_nested_typename (d, DECL_ASSEMBLER_NAME (c),
8383                                    declarator, type);
8384             else
8385               set_nested_typename (d, TYPE_NESTED_NAME (c), declarator, type);
8386           }
8387         }
8388
8389 #if 0 /* not yet, should get fixed properly later */
8390       decl = make_type_decl (declarator, type);
8391 #else
8392       decl = build_decl (TYPE_DECL, declarator, type);
8393 #endif
8394       if (TREE_CODE (type) == OFFSET_TYPE || TREE_CODE (type) == METHOD_TYPE)
8395         {
8396           cp_error_at ("typedef name may not be class-qualified", decl);
8397           TREE_TYPE (decl) = error_mark_node;
8398         }
8399       else if (quals)
8400         {
8401           if (ctype == NULL_TREE)
8402             {
8403               if (TREE_CODE (type) != METHOD_TYPE)
8404                 cp_error_at ("invalid type qualifier for non-method type", decl);
8405               else
8406                 ctype = TYPE_METHOD_BASETYPE (type);
8407             }
8408           if (ctype != NULL_TREE)
8409             grok_method_quals (ctype, decl, quals);
8410         }
8411
8412       if (RIDBIT_SETP (RID_SIGNED, specbits)
8413           || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
8414         C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
8415
8416       if (RIDBIT_SETP (RID_MUTABLE, specbits))
8417         {
8418           error ("non-object member `%s' cannot be declared mutable", name);
8419         }
8420
8421       return decl;
8422     }
8423
8424   /* Detect the case of an array type of unspecified size
8425      which came, as such, direct from a typedef name.
8426      We must copy the type, so that each identifier gets
8427      a distinct type, so that each identifier's size can be
8428      controlled separately by its own initializer.  */
8429
8430   if (type == typedef_type && TREE_CODE (type) == ARRAY_TYPE
8431       && TYPE_DOMAIN (type) == NULL_TREE)
8432     {
8433       type = build_cplus_array_type (TREE_TYPE (type), TYPE_DOMAIN (type));
8434     }
8435
8436   /* If this is a type name (such as, in a cast or sizeof),
8437      compute the type and return it now.  */
8438
8439   if (decl_context == TYPENAME)
8440     {
8441       /* Note that the grammar rejects storage classes
8442          in typenames, fields or parameters.  */
8443       if (constp || volatilep)
8444         if (IS_SIGNATURE (type))
8445           error ("`const' or `volatile' specified with signature type");
8446         else  
8447           type = build_type_variant (type, constp, volatilep);
8448
8449       /* Special case: "friend class foo" looks like a TYPENAME context.  */
8450       if (friendp)
8451         {
8452           /* A friendly class?  */
8453           if (current_class_type)
8454             make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type));
8455           else
8456             error("trying to make class `%s' a friend of global scope",
8457                   TYPE_NAME_STRING (type));
8458           type = void_type_node;
8459         }
8460       else if (quals)
8461         {
8462 #if 0 /* not yet, should get fixed properly later */
8463           tree dummy = make_type_decl (declarator, type);
8464 #else
8465           tree dummy = build_decl (TYPE_DECL, declarator, type);
8466 #endif
8467           if (ctype == NULL_TREE)
8468             {
8469               my_friendly_assert (TREE_CODE (type) == METHOD_TYPE, 159);
8470               ctype = TYPE_METHOD_BASETYPE (type);
8471             }
8472           grok_method_quals (ctype, dummy, quals);
8473           type = TREE_TYPE (dummy);
8474         }
8475
8476       return type;
8477     }
8478   else if (declarator == NULL_TREE && decl_context != PARM
8479            && TREE_CODE (type) != UNION_TYPE
8480            && ! bitfield)
8481     {
8482       cp_error ("abstract declarator `%T' used as declaration", type);
8483       declarator = make_anon_name ();
8484     }
8485
8486   /* `void' at top level (not within pointer)
8487      is allowed only in typedefs or type names.
8488      We don't complain about parms either, but that is because
8489      a better error message can be made later.  */
8490
8491   if (TYPE_MAIN_VARIANT (type) == void_type_node && decl_context != PARM)
8492     {
8493       if (TREE_CODE (declarator) == IDENTIFIER_NODE)
8494         {
8495           if (IDENTIFIER_OPNAME_P (declarator))
8496 #if 0                           /* How could this happen? */
8497             error ("operator `%s' declared void",
8498                    operator_name_string (declarator));
8499 #else
8500             my_friendly_abort (356);
8501 #endif
8502           else
8503             error ("variable or field `%s' declared void", name);
8504         }
8505       else
8506         error ("variable or field declared void");
8507       type = integer_type_node;
8508     }
8509
8510   /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
8511      or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE.  */
8512
8513   {
8514     register tree decl;
8515
8516     if (decl_context == PARM)
8517       {
8518         tree parmtype = type;
8519
8520         if (ctype)
8521           error ("cannot use `::' in parameter declaration");
8522
8523         /* A parameter declared as an array of T is really a pointer to T.
8524            One declared as a function is really a pointer to a function.
8525            One declared as a member is really a pointer to member.
8526
8527            Don't be misled by references.  */
8528
8529         if (TREE_CODE (type) == REFERENCE_TYPE)
8530           type = TREE_TYPE (type);
8531
8532         if (TREE_CODE (type) == ARRAY_TYPE)
8533           {
8534             if (parmtype == type)
8535               {
8536                 /* Transfer const-ness of array into that of type
8537                    pointed to.  */
8538                 type = build_pointer_type
8539                   (build_type_variant (TREE_TYPE (type), constp, volatilep));
8540                 volatilep = constp = 0;
8541               }
8542             else
8543               type = build_pointer_type (TREE_TYPE (type));
8544           }
8545         else if (TREE_CODE (type) == FUNCTION_TYPE)
8546           type = build_pointer_type (type);
8547         else if (TREE_CODE (type) == OFFSET_TYPE)
8548           type = build_pointer_type (type);
8549
8550         if (TREE_CODE (parmtype) == REFERENCE_TYPE)
8551           {
8552             /* Transfer const-ness of reference into that of type pointed to.  */
8553             type = build_type_variant (build_reference_type (type), constp, volatilep);
8554             constp = volatilep = 0;
8555           }
8556
8557         decl = build_decl (PARM_DECL, declarator, type);
8558
8559         bad_specifiers (decl, "parameter", virtualp, quals != NULL_TREE,
8560                         inlinep, friendp, raises != NULL_TREE);
8561         if (current_class_type
8562             && IS_SIGNATURE (current_class_type))
8563           {
8564             if (inlinep)
8565               error ("parameter of signature member function declared `inline'");
8566             if (RIDBIT_SETP (RID_AUTO, specbits))
8567               error ("parameter of signature member function declared `auto'");
8568             if (RIDBIT_SETP (RID_REGISTER, specbits))
8569               error ("parameter of signature member function declared `register'");
8570           }
8571
8572         /* Compute the type actually passed in the parmlist,
8573            for the case where there is no prototype.
8574            (For example, shorts and chars are passed as ints.)
8575            When there is a prototype, this is overridden later.  */
8576
8577         DECL_ARG_TYPE (decl) = type;
8578         if (TYPE_MAIN_VARIANT (type) == float_type_node)
8579           DECL_ARG_TYPE (decl) = build_type_variant (double_type_node,
8580                                                      TYPE_READONLY (type),
8581                                                      TYPE_VOLATILE (type));
8582         else if (C_PROMOTING_INTEGER_TYPE_P (type))
8583           {
8584             tree argtype;
8585
8586             /* Retain unsignedness if traditional or if not really
8587                getting wider.  */
8588             if (TREE_UNSIGNED (type)
8589                 && (flag_traditional
8590                     || TYPE_PRECISION (type)
8591                         == TYPE_PRECISION (integer_type_node)))
8592               argtype = unsigned_type_node;
8593             else
8594               argtype = integer_type_node;
8595             DECL_ARG_TYPE (decl) = build_type_variant (argtype,
8596                                                        TYPE_READONLY (type),
8597                                                        TYPE_VOLATILE (type));
8598           }
8599       }
8600     else if (decl_context == FIELD)
8601       {
8602         if (type == error_mark_node)
8603           {
8604             /* Happens when declaring arrays of sizes which
8605                are error_mark_node, for example.  */
8606             decl = NULL_TREE;
8607           }
8608         else if (TREE_CODE (type) == FUNCTION_TYPE)
8609           {
8610             int publicp = 0;
8611
8612             if (friendp == 0)
8613               {
8614                 if (ctype == NULL_TREE)
8615                   ctype = current_class_type;
8616
8617                 if (ctype == NULL_TREE)
8618                   {
8619                     cp_error ("can't make `%D' into a method -- not in a class",
8620                               declarator);
8621                     return void_type_node;
8622                   }
8623
8624                 /* ``A union may [ ... ] not [ have ] virtual functions.''
8625                    ARM 9.5 */
8626                 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
8627                   {
8628                     cp_error ("function `%D' declared virtual inside a union",
8629                               declarator);
8630                     return void_type_node;
8631                   }
8632
8633                 if (declarator == ansi_opname[(int) NEW_EXPR]
8634                     || declarator == ansi_opname[(int) DELETE_EXPR])
8635                   {
8636                     if (virtualp)
8637                       {
8638                         cp_error ("`%D' cannot be declared virtual, since it is always static",
8639                                   declarator);
8640                         virtualp = 0;
8641                       }
8642                   }
8643                 else if (staticp < 2)
8644                   type = build_cplus_method_type (build_type_variant (ctype, constp, volatilep),
8645                                                   TREE_TYPE (type), TYPE_ARG_TYPES (type));
8646               }
8647
8648             /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node.  */
8649             publicp = (RIDBIT_SETP (RID_EXTERN, specbits)
8650                        || (ctype != NULL_TREE && funcdef_flag >= 0)
8651                        || (friendp
8652                            && ! funcdef_flag
8653                            && RIDBIT_NOTSETP (RID_STATIC, specbits)
8654                            && RIDBIT_NOTSETP (RID_INLINE, specbits)));
8655             decl = grokfndecl (ctype, type, declarator,
8656                                virtualp, flags, quals,
8657                                raises, friendp ? -1 : 0, publicp);
8658             DECL_INLINE (decl) = inlinep;
8659           }
8660         else if (TREE_CODE (type) == METHOD_TYPE)
8661           {
8662             /* All method decls are public, so tell grokfndecl to set
8663                TREE_PUBLIC, also.  */
8664             decl = grokfndecl (ctype, type, declarator,
8665                                virtualp, flags, quals,
8666                                raises, friendp ? -1 : 0, 1);
8667             DECL_INLINE (decl) = inlinep;
8668           }
8669         else if (TREE_CODE (type) == RECORD_TYPE
8670                  && CLASSTYPE_DECLARED_EXCEPTION (type))
8671           {
8672             /* Handle a class-local exception declaration.  */
8673             decl = build_lang_field_decl (VAR_DECL, declarator, type);
8674             if (ctype == NULL_TREE)
8675               ctype = current_class_type;
8676             finish_exception_decl (TREE_CODE (TYPE_NAME (ctype)) == TYPE_DECL
8677                                    ? TYPE_IDENTIFIER (ctype) : TYPE_NAME (ctype), decl);
8678             return void_type_node;
8679           }
8680         else if (TYPE_SIZE (type) == NULL_TREE && !staticp
8681                  && (TREE_CODE (type) != ARRAY_TYPE || initialized == 0))
8682           {
8683             error ("field `%s' has incomplete type",
8684                    IDENTIFIER_POINTER (declarator));
8685
8686             /* If we're instantiating a template, tell them which
8687                instantiation made the field's type be incomplete.  */
8688             if (current_class_type
8689                 && TYPE_NAME (current_class_type)
8690                 && IDENTIFIER_TEMPLATE (DECL_NAME (TYPE_NAME (current_class_type)))
8691                 && declspecs && TREE_VALUE (declspecs)
8692                 && TREE_TYPE (TREE_VALUE (declspecs)) == type)
8693               error ("  in instantiation of template `%s'",
8694                      TYPE_NAME_STRING (current_class_type));
8695                 
8696             type = error_mark_node;
8697             decl = NULL_TREE;
8698           }
8699         else
8700           {
8701             if (friendp)
8702               {
8703                 error ("`%s' is neither function nor method; cannot be declared friend",
8704                        IDENTIFIER_POINTER (declarator));
8705                 friendp = 0;
8706               }
8707             decl = NULL_TREE;
8708           }
8709
8710         if (friendp)
8711           {
8712             /* Friends are treated specially.  */
8713             if (ctype == current_class_type)
8714               warning ("member functions are implicitly friends of their class");
8715             else
8716               {
8717                 tree t = NULL_TREE;
8718                 if (decl && DECL_NAME (decl))
8719                   t = do_friend (ctype, declarator, decl,
8720                                  last_function_parms, flags, quals);
8721                 if (t && funcdef_flag)
8722                   return t;
8723                 
8724                 return void_type_node;
8725               }
8726           }
8727
8728         /* Structure field.  It may not be a function, except for C++ */
8729
8730         if (decl == NULL_TREE)
8731           {
8732             /* ANSI C++ June 5 1992 WP 9.2.2 and 9.4.2.  A member-declarator
8733                cannot have an initializer, and a static member declaration must
8734                be defined elsewhere.  */
8735             if (initialized)
8736               {
8737                 if (staticp)
8738                   error ("static member `%s' must be defined separately from its declaration",
8739                           IDENTIFIER_POINTER (declarator));
8740                 /* Note that initialization of const members is prohibited
8741                    by the draft ANSI standard, though it appears to be in
8742                    common practice.  12.6.2: The argument list is used to
8743                    initialize the named nonstatic member....  This (or an
8744                    aggregate) is the only way to initialize nonstatic const
8745                    and reference members.  */
8746                 else
8747                   pedwarn ("ANSI C++ forbids initialization of %s `%s'",
8748                            constp ? "const member" : "member",
8749                            IDENTIFIER_POINTER (declarator));
8750               }
8751
8752             if (staticp || (constp && initialized))
8753               {
8754                 /* C++ allows static class members.
8755                    All other work for this is done by grokfield.
8756                    This VAR_DECL is built by build_lang_field_decl.
8757                    All other VAR_DECLs are built by build_decl.  */
8758                 decl = build_lang_field_decl (VAR_DECL, declarator, type);
8759                 if (staticp || TREE_CODE (type) == ARRAY_TYPE)
8760                   TREE_STATIC (decl) = 1;
8761                 /* In class context, static means public access.  */
8762                 TREE_PUBLIC (decl) = 1;
8763                 DECL_EXTERNAL (decl) = !initialized;
8764               }
8765             else
8766               {
8767                 decl = build_lang_field_decl (FIELD_DECL, declarator, type);
8768                 if (RIDBIT_SETP (RID_MUTABLE, specbits))
8769                   {
8770                     DECL_MUTABLE_P (decl) = 1;
8771                     RIDBIT_RESET (RID_MUTABLE, specbits);
8772                   }
8773               }
8774
8775             bad_specifiers (decl, "field", virtualp, quals != NULL_TREE,
8776                             inlinep, friendp, raises != NULL_TREE);
8777           }
8778       }
8779     else if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
8780       {
8781         tree original_name = declarator;
8782         int publicp = 0;
8783
8784         if (! declarator)
8785           return NULL_TREE;
8786
8787         if (RIDBIT_SETP (RID_AUTO, specbits))
8788           error ("storage class `auto' invalid for function `%s'", name);
8789         else if (RIDBIT_SETP (RID_REGISTER, specbits))
8790           error ("storage class `register' invalid for function `%s'", name);
8791
8792         /* Function declaration not at top level.
8793            Storage classes other than `extern' are not allowed
8794            and `extern' makes no difference.  */
8795         if (current_binding_level != global_binding_level
8796             && ! processing_template_decl
8797             && (RIDBIT_SETP (RID_STATIC, specbits)
8798                 || RIDBIT_SETP (RID_INLINE, specbits))
8799             && pedantic)
8800           {
8801             if (RIDBIT_SETP (RID_STATIC, specbits))
8802               pedwarn ("storage class `static' invalid for function `%s' declared out of global scope", name);
8803             else
8804               pedwarn ("storage class `inline' invalid for function `%s' declared out of global scope", name);
8805           }
8806         
8807         if (ctype == NULL_TREE)
8808           {
8809             if (virtualp)
8810               {
8811                 error ("virtual non-class function `%s'", name);
8812                 virtualp = 0;
8813               }
8814
8815             if (current_lang_name == lang_name_cplusplus
8816                 && ! (IDENTIFIER_LENGTH (original_name) == 4
8817                       && IDENTIFIER_POINTER (original_name)[0] == 'm'
8818                       && strcmp (IDENTIFIER_POINTER (original_name), "main") == 0)
8819                 && ! (IDENTIFIER_LENGTH (original_name) > 10
8820                       && IDENTIFIER_POINTER (original_name)[0] == '_'
8821                       && IDENTIFIER_POINTER (original_name)[1] == '_'
8822                       && strncmp (IDENTIFIER_POINTER (original_name)+2, "builtin_", 8) == 0))
8823               /* Plain overloading: will not be grok'd by grokclassfn.  */
8824               declarator = build_decl_overload (dname, TYPE_ARG_TYPES (type), 0);
8825           }
8826         else if (TREE_CODE (type) == FUNCTION_TYPE && staticp < 2)
8827           type = build_cplus_method_type (build_type_variant (ctype, constp, volatilep),
8828                                           TREE_TYPE (type), TYPE_ARG_TYPES (type));
8829
8830         /* Record presence of `static'.  In C++, `inline' is like `static'.
8831            Methods of classes should be public, unless we're dropping them
8832            into some other file, so we don't clear TREE_PUBLIC for them.  */
8833         publicp
8834           = ((ctype
8835               && CLASSTYPE_INTERFACE_KNOWN (ctype))
8836              || !(RIDBIT_SETP (RID_STATIC, specbits)
8837                   || RIDBIT_SETP (RID_INLINE, specbits)));
8838
8839         decl = grokfndecl (ctype, type, original_name,
8840                            virtualp, flags, quals,
8841                            raises,
8842                            processing_template_decl ? 0 : friendp ? 2 : 1,
8843                            publicp);
8844
8845         if (ctype == NULL_TREE && DECL_LANGUAGE (decl) != lang_c)
8846           DECL_ASSEMBLER_NAME (decl) = declarator;
8847
8848         if (staticp == 1)
8849           {
8850             int illegal_static = 0;
8851
8852             /* Don't allow a static member function in a class, and forbid
8853                declaring main to be static.  */
8854             if (TREE_CODE (type) == METHOD_TYPE)
8855               {
8856                 cp_error_at ("cannot declare member function `%D' to have static linkage", decl);
8857                 illegal_static = 1;
8858               }
8859             else if (! ctype
8860                      && IDENTIFIER_LENGTH (original_name) == 4
8861                      && IDENTIFIER_POINTER (original_name)[0] == 'm'
8862                      && ! strcmp (IDENTIFIER_POINTER (original_name), "main"))
8863               {
8864                 error ("cannot declare function `main' to have static linkage");
8865                 illegal_static = 1;
8866               }
8867             else if (current_function_decl)
8868               {
8869                 /* FIXME need arm citation */
8870                 error ("cannot declare static function inside another function");
8871                 illegal_static = 1;
8872               }
8873
8874             if (illegal_static)
8875               {
8876                 staticp = 0;
8877                 RIDBIT_RESET (RID_STATIC, specbits);
8878               }
8879           }
8880
8881         /* Record presence of `inline', if it is reasonable.  */
8882         if (inlinep)
8883           {
8884             tree last = tree_last (TYPE_ARG_TYPES (type));
8885
8886             if (! ctype
8887                 && ! strcmp (IDENTIFIER_POINTER (original_name), "main"))
8888               error ("cannot inline function `main'");
8889             else if (last && last != void_list_node)
8890               cp_warning ("cannot inline function `%D' which takes `...'", original_name);
8891             else
8892               /* Assume that otherwise the function can be inlined.  */
8893               DECL_INLINE (decl) = 1;
8894
8895             if (RIDBIT_SETP (RID_EXTERN, specbits))
8896               {
8897                 current_extern_inline = 1;
8898                 if (flag_ansi || pedantic || flag_pedantic_errors)
8899                   pedwarn ("ANSI C++ does not permit `extern inline'");
8900               }
8901           }
8902       }
8903     else
8904       {
8905         /* It's a variable.  */
8906
8907         /* An uninitialized decl with `extern' is a reference.  */
8908         decl = grokvardecl (type, declarator, specbits, initialized);
8909         bad_specifiers (decl, "variable", virtualp, quals != NULL_TREE,
8910                         inlinep, friendp, raises != NULL_TREE);
8911
8912         if (ctype)
8913           {
8914             if (staticp == 1)
8915               {
8916                 cp_error ("static member `%D' re-declared as static",
8917                           decl);
8918                 staticp = 0;
8919                 RIDBIT_RESET (RID_STATIC, specbits);
8920               }
8921             if (RIDBIT_SETP (RID_EXTERN, specbits))
8922               {
8923                 cp_error ("cannot explicitly declare member `%#D' to have extern linkage",
8924                           decl);
8925                 RIDBIT_RESET (RID_EXTERN, specbits);
8926               }
8927           }
8928       }
8929
8930     if (RIDBIT_SETP (RID_MUTABLE, specbits))
8931       {
8932         error ("`%s' cannot be declared mutable", name);
8933       }
8934
8935     /* Record `register' declaration for warnings on &
8936        and in case doing stupid register allocation.  */
8937
8938     if (RIDBIT_SETP (RID_REGISTER, specbits))
8939       DECL_REGISTER (decl) = 1;
8940
8941     if (RIDBIT_SETP (RID_EXTERN, specbits))
8942       DECL_THIS_EXTERN (decl) = 1;
8943
8944     /* Record constancy and volatility.  */
8945
8946     if (constp)
8947       TREE_READONLY (decl) = TREE_CODE (type) != REFERENCE_TYPE;
8948     if (volatilep)
8949       {
8950         TREE_SIDE_EFFECTS (decl) = 1;
8951         TREE_THIS_VOLATILE (decl) = 1;
8952       }
8953
8954     return decl;
8955   }
8956 }
8957 \f
8958 /* Tell if a parmlist/exprlist looks like an exprlist or a parmlist.
8959    An empty exprlist is a parmlist.  An exprlist which
8960    contains only identifiers at the global level
8961    is a parmlist.  Otherwise, it is an exprlist.  */
8962 int
8963 parmlist_is_exprlist (exprs)
8964      tree exprs;
8965 {
8966   if (exprs == NULL_TREE || TREE_PARMLIST (exprs))
8967     return 0;
8968
8969   if (current_binding_level == global_binding_level)
8970     {
8971       /* At the global level, if these are all identifiers,
8972          then it is a parmlist.  */
8973       while (exprs)
8974         {
8975           if (TREE_CODE (TREE_VALUE (exprs)) != IDENTIFIER_NODE)
8976             return 1;
8977           exprs = TREE_CHAIN (exprs);
8978         }
8979       return 0;
8980     }
8981   return 1;
8982 }
8983
8984 /* Subroutine of `grokparms'.  In a fcn definition, arg types must
8985    be complete.
8986
8987    C++: also subroutine of `start_function'.  */
8988 static void
8989 require_complete_types_for_parms (parms)
8990      tree parms;
8991 {
8992   while (parms)
8993     {
8994       tree type = TREE_TYPE (parms);
8995       if (TYPE_SIZE (type) == NULL_TREE)
8996         {
8997           if (DECL_NAME (parms))
8998             error ("parameter `%s' has incomplete type",
8999                    IDENTIFIER_POINTER (DECL_NAME (parms)));
9000           else
9001             error ("parameter has incomplete type");
9002           TREE_TYPE (parms) = error_mark_node;
9003         }
9004 #if 0
9005       /* If the arg types are incomplete in a declaration,
9006          they must include undefined tags.
9007          These tags can never be defined in the scope of the declaration,
9008          so the types can never be completed,
9009          and no call can be compiled successfully.  */
9010       /* This is not the right behavior for C++, but not having
9011          it is also probably wrong.  */
9012       else
9013         {
9014           /* Now warn if is a pointer to an incomplete type.  */
9015           while (TREE_CODE (type) == POINTER_TYPE
9016                  || TREE_CODE (type) == REFERENCE_TYPE)
9017             type = TREE_TYPE (type);
9018           type = TYPE_MAIN_VARIANT (type);
9019           if (TYPE_SIZE (type) == NULL_TREE)
9020             {
9021               if (DECL_NAME (parm) != NULL_TREE)
9022                 warning ("parameter `%s' points to incomplete type",
9023                          IDENTIFIER_POINTER (DECL_NAME (parm)));
9024               else
9025                 warning ("parameter points to incomplete type");
9026             }
9027         }
9028 #endif
9029       parms = TREE_CHAIN (parms);
9030     }
9031 }
9032
9033 /* Decode the list of parameter types for a function type.
9034    Given the list of things declared inside the parens,
9035    return a list of types.
9036
9037    The list we receive can have three kinds of elements:
9038    an IDENTIFIER_NODE for names given without types,
9039    a TREE_LIST node for arguments given as typespecs or names with typespecs,
9040    or void_type_node, to mark the end of an argument list
9041    when additional arguments are not permitted (... was not used).
9042
9043    FUNCDEF_FLAG is nonzero for a function definition, 0 for
9044    a mere declaration.  A nonempty identifier-list gets an error message
9045    when FUNCDEF_FLAG is zero.
9046    If FUNCDEF_FLAG is 1, then parameter types must be complete.
9047    If FUNCDEF_FLAG is -1, then parameter types may be incomplete.
9048
9049    If all elements of the input list contain types,
9050    we return a list of the types.
9051    If all elements contain no type (except perhaps a void_type_node
9052    at the end), we return a null list.
9053    If some have types and some do not, it is an error, and we
9054    return a null list.
9055
9056    Also set last_function_parms to either
9057    a list of names (IDENTIFIER_NODEs) or a chain of PARM_DECLs.
9058    A list of names is converted to a chain of PARM_DECLs
9059    by store_parm_decls so that ultimately it is always a chain of decls.
9060
9061    Note that in C++, parameters can take default values.  These default
9062    values are in the TREE_PURPOSE field of the TREE_LIST.  It is
9063    an error to specify default values which are followed by parameters
9064    that have no default values, or an ELLIPSES.  For simplicities sake,
9065    only parameters which are specified with their types can take on
9066    default values.  */
9067
9068 static tree
9069 grokparms (first_parm, funcdef_flag)
9070      tree first_parm;
9071      int funcdef_flag;
9072 {
9073   tree result = NULL_TREE;
9074   tree decls = NULL_TREE;
9075
9076   if (first_parm != NULL_TREE
9077       && TREE_CODE (TREE_VALUE (first_parm)) == IDENTIFIER_NODE)
9078     {
9079       if (! funcdef_flag)
9080         pedwarn ("parameter names (without types) in function declaration");
9081       last_function_parms = first_parm;
9082       return NULL_TREE;
9083     }
9084   else if (first_parm != NULL_TREE
9085            && TREE_CODE (TREE_VALUE (first_parm)) != TREE_LIST
9086            && TREE_VALUE (first_parm) != void_type_node)
9087     my_friendly_abort (145);
9088   else
9089     {
9090       /* Types were specified.  This is a list of declarators
9091          each represented as a TREE_LIST node.  */
9092       register tree parm, chain;
9093       int any_init = 0, any_error = 0, saw_void = 0;
9094
9095       if (first_parm != NULL_TREE)
9096         {
9097           tree last_result = NULL_TREE;
9098           tree last_decl = NULL_TREE;
9099
9100           for (parm = first_parm; parm != NULL_TREE; parm = chain)
9101             {
9102               tree type, list_node = parm;
9103               register tree decl = TREE_VALUE (parm);
9104               tree init = TREE_PURPOSE (parm);
9105
9106               chain = TREE_CHAIN (parm);
9107               /* @@ weak defense against parse errors.  */
9108               if (decl != void_type_node && TREE_CODE (decl) != TREE_LIST)
9109                 {
9110                   /* Give various messages as the need arises.  */
9111                   if (TREE_CODE (decl) == STRING_CST)
9112                     error ("invalid string constant `%s'",
9113                            TREE_STRING_POINTER (decl));
9114                   else if (TREE_CODE (decl) == INTEGER_CST)
9115                     error ("invalid integer constant in parameter list, did you forget to give parameter name?");
9116                   continue;
9117                 }
9118
9119               if (decl != void_type_node)
9120                 {
9121                   /* @@ May need to fetch out a `raises' here.  */
9122                   decl = grokdeclarator (TREE_VALUE (decl),
9123                                          TREE_PURPOSE (decl),
9124                                          PARM, init != NULL_TREE, NULL_TREE);
9125                   if (! decl)
9126                     continue;
9127                   type = TREE_TYPE (decl);
9128                   if (TYPE_MAIN_VARIANT (type) == void_type_node)
9129                     decl = void_type_node;
9130                   else if (TREE_CODE (type) == METHOD_TYPE)
9131                     {
9132                       if (DECL_NAME (decl))
9133                         /* Cannot use `error_with_decl' here because
9134                            we don't have DECL_CONTEXT set up yet.  */
9135                         error ("parameter `%s' invalidly declared method type",
9136                                IDENTIFIER_POINTER (DECL_NAME (decl)));
9137                       else
9138                         error ("parameter invalidly declared method type");
9139                       type = build_pointer_type (type);
9140                       TREE_TYPE (decl) = type;
9141                     }
9142                   else if (TREE_CODE (type) == OFFSET_TYPE)
9143                     {
9144                       if (DECL_NAME (decl))
9145                         error ("parameter `%s' invalidly declared offset type",
9146                                IDENTIFIER_POINTER (DECL_NAME (decl)));
9147                       else
9148                         error ("parameter invalidly declared offset type");
9149                       type = build_pointer_type (type);
9150                       TREE_TYPE (decl) = type;
9151                     }
9152                   else if (TREE_CODE (type) == RECORD_TYPE
9153                            && TYPE_LANG_SPECIFIC (type)
9154                            && CLASSTYPE_ABSTRACT_VIRTUALS (type))
9155                     {
9156                       abstract_virtuals_error (decl, type);
9157                       any_error = 1;  /* seems like a good idea */
9158                     }
9159                   else if (TREE_CODE (type) == RECORD_TYPE
9160                            && TYPE_LANG_SPECIFIC (type)
9161                            && IS_SIGNATURE (type))
9162                     {
9163                       signature_error (decl, type);
9164                       any_error = 1;  /* seems like a good idea */
9165                     }
9166                 }
9167
9168               if (decl == void_type_node)
9169                 {
9170                   if (result == NULL_TREE)
9171                     {
9172                       result = void_list_node;
9173                       last_result = result;
9174                     }
9175                   else
9176                     {
9177                       TREE_CHAIN (last_result) = void_list_node;
9178                       last_result = void_list_node;
9179                     }
9180                   saw_void = 1;
9181                   if (chain
9182                       && (chain != void_list_node || TREE_CHAIN (chain)))
9183                     error ("`void' in parameter list must be entire list");
9184                   break;
9185                 }
9186
9187               /* Since there is a prototype, args are passed in their own types.  */
9188               DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
9189 #ifdef PROMOTE_PROTOTYPES
9190               if ((TREE_CODE (type) == INTEGER_TYPE
9191                    || TREE_CODE (type) == ENUMERAL_TYPE)
9192                   && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
9193                 DECL_ARG_TYPE (decl) = integer_type_node;
9194 #endif
9195               if (!any_error)
9196                 {
9197                   if (init)
9198                     {
9199                       any_init++;
9200                       if (TREE_CODE (init) == SAVE_EXPR)
9201                         PARM_DECL_EXPR (init) = 1;
9202                       else if (TREE_CODE (init) == VAR_DECL)
9203                         {
9204                           if (IDENTIFIER_LOCAL_VALUE (DECL_NAME (init)))
9205                             {
9206                               /* ``Local variables may not be used in default
9207                                  argument expressions.'' dpANSI C++ 8.2.6 */
9208                               /* If extern int i; within a function is not
9209                                  considered a local variable, then this code is
9210                                  wrong. */
9211                               cp_error ("local variable `%D' may not be used as a default argument", init);
9212                               any_error = 1;
9213                             }
9214                           else if (TREE_READONLY_DECL_P (init))
9215                             init = decl_constant_value (init);
9216                         }
9217                       else
9218                         init = require_instantiated_type (type, init, integer_zero_node);
9219                     }
9220                   else if (any_init)
9221                     {
9222                       error ("all trailing parameters must have default arguments");
9223                       any_error = 1;
9224                     }
9225                 }
9226               else
9227                 init = NULL_TREE;
9228
9229               if (decls == NULL_TREE)
9230                 {
9231                   decls = decl;
9232                   last_decl = decls;
9233                 }
9234               else
9235                 {
9236                   TREE_CHAIN (last_decl) = decl;
9237                   last_decl = decl;
9238                 }
9239               if (TREE_PERMANENT (list_node))
9240                 {
9241                   TREE_PURPOSE (list_node) = init;
9242                   TREE_VALUE (list_node) = type;
9243                   TREE_CHAIN (list_node) = NULL_TREE;
9244                 }
9245               else
9246                 list_node = saveable_tree_cons (init, type, NULL_TREE);
9247               if (result == NULL_TREE)
9248                 {
9249                   result = list_node;
9250                   last_result = result;
9251                 }
9252               else
9253                 {
9254                   TREE_CHAIN (last_result) = list_node;
9255                   last_result = list_node;
9256                 }
9257             }
9258           if (last_result)
9259             TREE_CHAIN (last_result) = NULL_TREE;
9260           /* If there are no parameters, and the function does not end
9261              with `...', then last_decl will be NULL_TREE.  */
9262           if (last_decl != NULL_TREE)
9263             TREE_CHAIN (last_decl) = NULL_TREE;
9264         }
9265     }
9266
9267   last_function_parms = decls;
9268
9269   /* In a fcn definition, arg types must be complete.  */
9270   if (funcdef_flag > 0)
9271     require_complete_types_for_parms (last_function_parms);
9272
9273   return result;
9274 }
9275 \f
9276 /* These memoizing functions keep track of special properties which
9277    a class may have.  `grok_ctor_properties' notices whether a class
9278    has a constructor of the form X(X&), and also complains
9279    if the class has a constructor of the form X(X).
9280    `grok_op_properties' takes notice of the various forms of
9281    operator= which are defined, as well as what sorts of type conversion
9282    may apply.  Both functions take a FUNCTION_DECL as an argument.  */
9283 void
9284 grok_ctor_properties (ctype, decl)
9285      tree ctype, decl;
9286 {
9287   tree parmtypes = FUNCTION_ARG_CHAIN (decl);
9288   tree parmtype = parmtypes ? TREE_VALUE (parmtypes) : void_type_node;
9289
9290   /* When a type has virtual baseclasses, a magical first int argument is
9291      added to any ctor so we can tell if the class has been initialized
9292      yet.  This could screw things up in this function, so we deliberately
9293      ignore the leading int if we're in that situation.  */
9294   if (parmtypes
9295       && TREE_VALUE (parmtypes) == integer_type_node
9296       && TYPE_USES_VIRTUAL_BASECLASSES (ctype))
9297     {
9298       parmtypes = TREE_CHAIN (parmtypes);
9299       parmtype = TREE_VALUE (parmtypes);
9300     }
9301
9302   if (TREE_CODE (parmtype) == REFERENCE_TYPE
9303       && TYPE_MAIN_VARIANT (TREE_TYPE (parmtype)) == ctype)
9304     {
9305       if (TREE_CHAIN (parmtypes) == NULL_TREE
9306           || TREE_CHAIN (parmtypes) == void_list_node
9307           || TREE_PURPOSE (TREE_CHAIN (parmtypes)))
9308         {
9309           TYPE_HAS_INIT_REF (ctype) = 1;
9310           if (TYPE_READONLY (TREE_TYPE (parmtype)))
9311             TYPE_HAS_CONST_INIT_REF (ctype) = 1;
9312         }
9313       else
9314         TYPE_GETS_INIT_AGGR (ctype) = 1;
9315     }
9316   else if (TYPE_MAIN_VARIANT (parmtype) == ctype)
9317     {
9318       if (TREE_CHAIN (parmtypes) != NULL_TREE
9319           && TREE_CHAIN (parmtypes) == void_list_node)
9320         cp_error ("invalid constructor; you probably meant `%T (%T&)'",
9321                   ctype, ctype);
9322       SET_IDENTIFIER_ERROR_LOCUS (DECL_NAME (decl), ctype);
9323       TYPE_GETS_INIT_AGGR (ctype) = 1;
9324     }
9325   else if (TREE_CODE (parmtype) == VOID_TYPE
9326            || TREE_PURPOSE (parmtypes) != NULL_TREE)
9327     TYPE_HAS_DEFAULT_CONSTRUCTOR (ctype) = 1;
9328 }
9329
9330 /* An operator with this name can be either unary or binary.  */
9331 int ambi_op_p (name)
9332      tree name;
9333 {
9334   return (name == ansi_opname [(int) INDIRECT_REF]
9335           || name == ansi_opname [(int) ADDR_EXPR]
9336           || name == ansi_opname [(int) NEGATE_EXPR]
9337           || name == ansi_opname[(int) POSTINCREMENT_EXPR]
9338           || name == ansi_opname[(int) POSTDECREMENT_EXPR]
9339           || name == ansi_opname [(int) CONVERT_EXPR]);
9340 }
9341
9342 /* An operator with this name can only be unary.  */
9343 int unary_op_p (name)
9344      tree name;
9345 {
9346   return (name == ansi_opname [(int) TRUTH_NOT_EXPR]
9347           || name == ansi_opname [(int) BIT_NOT_EXPR]
9348           || name == ansi_opname [(int) COMPONENT_REF]
9349           || OPERATOR_TYPENAME_P (name));
9350 }
9351
9352 /* Do a little sanity-checking on how they declared their operator.  */
9353 static void
9354 grok_op_properties (decl, virtualp, friendp)
9355      tree decl;
9356      int virtualp, friendp;
9357 {
9358   tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9359   int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
9360   tree name = DECL_NAME (decl);
9361   tree t;
9362
9363   if (! friendp)
9364     for (t = current_class_type; t; t = TYPE_NEXT_VARIANT (t))
9365       {
9366         if (name == ansi_opname[(int) MODIFY_EXPR])
9367           TYPE_HAS_ASSIGNMENT (t) = 1;
9368         else if (name == ansi_opname[(int) CALL_EXPR])
9369           TYPE_OVERLOADS_CALL_EXPR (t) = 1;
9370         else if (name == ansi_opname[(int) ARRAY_REF])
9371           TYPE_OVERLOADS_ARRAY_REF (t) = 1;
9372         else if (name == ansi_opname[(int) COMPONENT_REF]
9373                  || name == ansi_opname[(int) MEMBER_REF])
9374           TYPE_OVERLOADS_ARROW (t) = 1;
9375         else if (name == ansi_opname[(int) NEW_EXPR])
9376           {
9377             if (TREE_CHAIN (argtypes) == void_list_node)
9378               TREE_GETS_NEW (t) = 1;
9379             else
9380               TREE_GETS_PLACED_NEW (t) = 1;
9381           }
9382         else if (name == ansi_opname[(int) DELETE_EXPR])
9383           TREE_GETS_DELETE (t) = 1;
9384 #if 0
9385         else if (name == ansi_opname[(int) VEC_NEW_EXPR])
9386           TREE_GETS_NEW (t) = 1;
9387         else if (name == ansi_opname[(int) VEC_DELETE_EXPR])
9388           TREE_GETS_DELETE (t) = 1;
9389 #endif
9390       }
9391
9392   if (name == ansi_opname[(int) NEW_EXPR])
9393     {
9394 #if 0
9395       /* When the compiler encounters the definition of A::operator new, it
9396          doesn't look at the class declaration to find out if it's static.  */
9397       my_friendly_assert (!methodp, 355);
9398 #endif
9399      
9400       /* Take care of function decl if we had syntax errors.  */
9401       if (argtypes == NULL_TREE)
9402         TREE_TYPE (decl) =
9403           build_function_type (ptr_type_node,
9404                                hash_tree_chain (integer_type_node,
9405                                                 void_list_node));
9406       else
9407         decl = coerce_new_type (TREE_TYPE (decl));
9408     }
9409 #if 0
9410   else if (name == ansi_opname[(int) VEC_NEW_EXPR])
9411     {
9412     }
9413 #endif
9414   else if (name == ansi_opname[(int) DELETE_EXPR])
9415     {
9416 #if 0
9417       my_friendly_assert (!methodp, 355);
9418 #endif
9419      
9420       if (argtypes == NULL_TREE)
9421         TREE_TYPE (decl) =
9422           build_function_type (void_type_node,
9423                                hash_tree_chain (ptr_type_node,
9424                                                 void_list_node));
9425       else
9426         decl = coerce_delete_type (TREE_TYPE (decl));
9427     }
9428 #if 0
9429   else if (name == ansi_opname[(int) VEC_DELETE_EXPR])
9430     {
9431     }
9432 #endif
9433   else
9434     {
9435       /* An operator function must either be a non-static member function
9436          or have at least one parameter of a class, a reference to a class,
9437          an enumeration, or a reference to an enumeration.  13.4.0.6 */
9438       if (! methodp)
9439         {
9440           if (OPERATOR_TYPENAME_P (name)
9441               || name == ansi_opname[(int) CALL_EXPR]
9442               || name == ansi_opname[(int) MODIFY_EXPR]
9443               || name == ansi_opname[(int) COMPONENT_REF]
9444               || name == ansi_opname[(int) ARRAY_REF])
9445             cp_error ("`%D' must be a nonstatic member function", decl);
9446           else
9447             {
9448               tree p = argtypes;
9449
9450               if (p)
9451                 for (; TREE_VALUE (p) != void_type_node ; p = TREE_CHAIN (p))
9452                   {
9453                     tree arg = TREE_VALUE (p);
9454                     if (TREE_CODE (arg) == REFERENCE_TYPE)
9455                       arg = TREE_TYPE (arg);
9456
9457                     /* This lets bad template code slip through.  */
9458                     if (IS_AGGR_TYPE (arg)
9459                         || TREE_CODE (arg) == ENUMERAL_TYPE
9460                         || TREE_CODE (arg) == TEMPLATE_TYPE_PARM)
9461                       goto foundaggr;
9462                   }
9463               cp_error
9464                 ("`%D' must have an argument of class or enumerated type",
9465                  decl);
9466             foundaggr:
9467               ;
9468             }
9469         }
9470       
9471       if (name == ansi_opname[(int) CALL_EXPR]
9472           || name == ansi_opname[(int) METHOD_CALL_EXPR])
9473         return;                 /* no restrictions on args */
9474
9475       if (name == ansi_opname[(int) MODIFY_EXPR])
9476         {
9477           tree parmtype;
9478
9479           if (list_length (argtypes) != 3 && methodp)
9480             {
9481               cp_error ("`%D' must take exactly one argument", decl);
9482               return;
9483             }
9484           parmtype = TREE_VALUE (TREE_CHAIN (argtypes));
9485
9486           if (TREE_CODE (parmtype) == REFERENCE_TYPE
9487               && TYPE_MAIN_VARIANT (TREE_TYPE (parmtype))
9488               == current_class_type)
9489             {
9490               TYPE_HAS_ASSIGN_REF (current_class_type) = 1;
9491               if (TYPE_READONLY (TREE_TYPE (parmtype)))
9492                 TYPE_HAS_CONST_ASSIGN_REF (current_class_type) = 1;
9493             }
9494         }
9495       else if (name == ansi_opname[(int) COND_EXPR])
9496         {
9497           /* 13.4.0.3 */
9498           pedwarn ("ANSI C++ prohibits overloading operator ?:");
9499           if (list_length (argtypes) != 4)
9500             cp_error ("`%D' must take exactly three arguments", decl);
9501         }         
9502       else if (ambi_op_p (name))
9503         {
9504           if (list_length (argtypes) == 2)
9505             /* prefix */;
9506           else if (list_length (argtypes) == 3)
9507             {
9508               if ((name == ansi_opname[(int) POSTINCREMENT_EXPR]
9509                    || name == ansi_opname[(int) POSTDECREMENT_EXPR])
9510                   && TREE_VALUE (TREE_CHAIN (argtypes)) != integer_type_node)
9511                 {
9512                   if (methodp)
9513                     cp_error ("postfix `%D' must take `int' as its argument",
9514                               decl);
9515                   else
9516                     cp_error
9517                       ("postfix `%D' must take `int' as its second argument",
9518                        decl);
9519                 }
9520             }
9521           else
9522             {
9523               if (methodp)
9524                 cp_error ("`%D' must take either zero or one argument", decl);
9525               else
9526                 cp_error ("`%D' must take either one or two arguments", decl);
9527             }
9528         }
9529       else if (unary_op_p (name))
9530         {
9531           if (list_length (argtypes) != 2)
9532             {
9533               if (methodp)
9534                 cp_error ("`%D' must take `void'", decl);
9535               else
9536                 cp_error ("`%D' must take exactly one argument", decl);
9537             }
9538         }
9539       else /* if (binary_op_p (name)) */
9540         {
9541           if (list_length (argtypes) != 3)
9542             {
9543               if (methodp)
9544                 cp_error ("`%D' must take exactly one argument", decl);
9545               else
9546                 cp_error ("`%D' must take exactly two arguments", decl);
9547             }
9548         }
9549
9550       /* 13.4.0.8 */
9551       if (argtypes)
9552         for (; argtypes != void_list_node ; argtypes = TREE_CHAIN (argtypes))
9553           if (TREE_PURPOSE (argtypes))
9554             {
9555               TREE_PURPOSE (argtypes) = NULL_TREE;
9556               if (name == ansi_opname[(int) POSTINCREMENT_EXPR]
9557                   || name == ansi_opname[(int) POSTDECREMENT_EXPR])
9558                 {
9559                   if (pedantic)
9560                     cp_pedwarn ("`%D' cannot have default arguments", decl);
9561                 }
9562               else
9563                 cp_error ("`%D' cannot have default arguments", decl);
9564             }
9565     }
9566 }
9567 \f
9568 /* Get the struct, enum or union (CODE says which) with tag NAME.
9569    Define the tag as a forward-reference if it is not defined.
9570
9571    C++: If a class derivation is given, process it here, and report
9572    an error if multiple derivation declarations are not identical.
9573
9574    If this is a definition, come in through xref_tag and only look in
9575    the current frame for the name (since C++ allows new names in any
9576    scope.)  */
9577
9578 /* avoid rewriting all callers of xref_tag */
9579 static int xref_next_defn = 0;
9580
9581 tree
9582 xref_defn_tag (code_type_node, name, binfo)
9583      tree code_type_node;
9584      tree name, binfo;
9585 {
9586   tree rv, ncp;
9587   xref_next_defn = 1;
9588
9589   if (class_binding_level)
9590     {
9591       tree n1;
9592       char *buf;
9593       /* we need to build a new IDENTIFIER_NODE for name which nukes
9594        * the pieces... */
9595 /*
9596       n1 = IDENTIFIER_LOCAL_VALUE (current_class_name);
9597       if (n1)
9598         n1 = DECL_NAME (n1);
9599       else
9600         n1 = current_class_name;
9601 */
9602       n1 = TYPE_NAME(current_class_type);
9603       if (n1)
9604         n1 = DECL_NESTED_TYPENAME(n1);
9605       else
9606         n1 = current_class_name;
9607       
9608       buf = (char *) alloca (4 + IDENTIFIER_LENGTH (n1)
9609                              + IDENTIFIER_LENGTH (name));
9610       
9611       sprintf (buf, "%s::%s", IDENTIFIER_POINTER (n1),
9612                IDENTIFIER_POINTER (name));
9613       ncp = get_identifier (buf);
9614 #ifdef SPEW_DEBUG
9615       if (spew_debug)
9616         printf("*** %s ***\n", IDENTIFIER_POINTER (ncp));
9617 #endif
9618 #if 0
9619       IDENTIFIER_LOCAL_VALUE (name) =
9620         build_lang_decl (TYPE_DECL, ncp, NULL_TREE);
9621 #endif
9622       rv = xref_tag (code_type_node, name, binfo, 0);
9623       if (! ANON_AGGRNAME_P (name))
9624       {
9625         register tree type_decl = build_lang_decl (TYPE_DECL, ncp, rv);
9626 #ifdef DWARF_DEBUGGING_INFO
9627         /* Mark the TYPE_DECL node created just above as a gratuitous one
9628            so that dwarfout.c will know not to generate a TAG_typedef DIE
9629            for it.  */
9630         if (write_symbols == DWARF_DEBUG)
9631           DECL_IGNORED_P (type_decl) = 1;
9632 #endif /* DWARF_DEBUGGING_INFO */
9633         pushdecl_top_level (type_decl);
9634       }
9635     }
9636   else
9637     {
9638       rv = xref_tag (code_type_node, name, binfo, 0);
9639     }
9640   xref_next_defn = 0;
9641   return rv;
9642 }
9643
9644 tree
9645 xref_tag (code_type_node, name, binfo, globalize)
9646      tree code_type_node;
9647      tree name, binfo;
9648      int globalize;
9649 {
9650   enum tag_types tag_code;
9651   enum tree_code code;
9652   int temp = 0;
9653   int i, len;
9654   register tree ref, t;
9655   struct binding_level *b = inner_binding_level;
9656
9657   tag_code = (enum tag_types) TREE_INT_CST_LOW (code_type_node);
9658   switch (tag_code)
9659     {
9660     case record_type:
9661     case class_type:
9662     case exception_type:
9663     case signature_type:
9664       code = RECORD_TYPE;
9665       len = list_length (binfo);
9666       break;
9667     case union_type:
9668       code = UNION_TYPE;
9669       if (binfo)
9670         {
9671           cp_error ("derived union `%T' invalid", name);
9672           binfo = NULL_TREE;
9673         }
9674       len = 0;
9675       break;
9676     case enum_type:
9677       code = ENUMERAL_TYPE;
9678       break;
9679     default:
9680       my_friendly_abort (18);
9681     }
9682
9683   /* If a cross reference is requested, look up the type
9684      already defined for this tag and return it.  */
9685   if ((t = IDENTIFIER_TYPE_VALUE(name)))
9686     {
9687        if (TREE_CODE(t) != code) t = NULL_TREE;
9688     }
9689   if (xref_next_defn)
9690     {
9691       /* If we know we are defining this tag, only look it up in this scope
9692        * and don't try to find it as a type. */
9693       xref_next_defn = 0;
9694       if (t && TYPE_CONTEXT(t) && strstr(IDENTIFIER_POINTER(name), "::"))
9695         ref = t;
9696       else
9697         ref = lookup_tag (code, name, b, 1);
9698     }
9699   else
9700     {
9701       if (t)
9702         ref = t;
9703       else
9704         ref = lookup_tag (code, name, b, 0);
9705
9706       if (! ref)
9707         {
9708           /* Try finding it as a type declaration.  If that wins, use it.  */
9709           ref = lookup_name (name, 1);
9710           if (ref && TREE_CODE (ref) == TYPE_DECL
9711               && TREE_CODE (TREE_TYPE (ref)) == code)
9712             ref = TREE_TYPE (ref);
9713           else
9714             ref = NULL_TREE;
9715         }
9716     }
9717
9718   push_obstacks_nochange ();
9719
9720   if (! ref)
9721     {
9722       /* If no such tag is yet defined, create a forward-reference node
9723          and record it as the "definition".
9724          When a real declaration of this type is found,
9725          the forward-reference will be altered into a real type.  */
9726
9727       /* In C++, since these migrate into the global scope, we must
9728          build them on the permanent obstack.  */
9729
9730       temp = allocation_temporary_p ();
9731       if (temp)
9732         end_temporary_allocation ();
9733
9734       if (code == ENUMERAL_TYPE)
9735         {
9736           ref = make_node (ENUMERAL_TYPE);
9737
9738           /* Give the type a default layout like unsigned int
9739              to avoid crashing if it does not get defined.  */
9740           TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
9741           TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
9742           TREE_UNSIGNED (ref) = 1;
9743           TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
9744           TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
9745           TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
9746
9747           /* Enable us to recognize when a type is created in class context.
9748              To do nested classes correctly, this should probably be cleared
9749              out when we leave this classes scope.  Currently this in only
9750              done in `start_enum'.  */
9751
9752           pushtag (name, ref, globalize);
9753           if (flag_cadillac)
9754             cadillac_start_enum (ref);
9755         }
9756       else if (tag_code == exception_type)
9757         {
9758           ref = make_lang_type (code);
9759           /* Enable us to recognize when an exception type is created in
9760              class context.  To do nested classes correctly, this should
9761              probably be cleared out when we leave this class's scope.  */
9762           CLASSTYPE_DECLARED_EXCEPTION (ref) = 1;
9763           pushtag (name, ref, globalize);
9764           if (flag_cadillac)
9765             cadillac_start_struct (ref);
9766         }
9767       else
9768         {
9769           extern tree pending_vtables;
9770           struct binding_level *old_b = class_binding_level;
9771           int needs_writing;
9772
9773           ref = make_lang_type (code);
9774
9775           /* A signature type will contain the fields of the signature
9776              table.  Therefore, it's not only an interface.  */
9777           if (tag_code == signature_type)
9778             {
9779               SET_SIGNATURE (ref);
9780               CLASSTYPE_INTERFACE_ONLY (ref) = 0;
9781               CLASSTYPE_INTERFACE_UNKNOWN (ref) = 0;
9782             }
9783
9784           /* Record how to set the access of this class's
9785              virtual functions.  If write_virtuals == 2 or 3, then
9786              inline virtuals are ``extern inline''.  */
9787           switch (write_virtuals)
9788             {
9789             case 0:
9790             case 1:
9791               needs_writing = 1;
9792               break;
9793             case 2:
9794               needs_writing = !! value_member (name, pending_vtables);
9795               break;
9796             case 3:
9797               needs_writing = ! CLASSTYPE_INTERFACE_ONLY (ref)
9798                 && CLASSTYPE_INTERFACE_KNOWN (ref);
9799               break;
9800             default:
9801               needs_writing = 0;
9802             }
9803
9804           /* Signatures don't have a vtable.  As long as we don't have default
9805              implementations, they behave as if `write_virtuals' were 3.  */
9806           if (tag_code == signature_type)
9807             CLASSTYPE_VTABLE_NEEDS_WRITING (ref) = 0;
9808           else
9809             CLASSTYPE_VTABLE_NEEDS_WRITING (ref) = needs_writing;
9810
9811 #ifdef NONNESTED_CLASSES
9812           /* Class types don't nest the way enums do.  */
9813           class_binding_level = (struct binding_level *)0;
9814 #endif
9815           pushtag (name, ref, globalize);
9816           class_binding_level = old_b;
9817
9818           if (flag_cadillac)
9819             cadillac_start_struct (ref);
9820         }
9821     }
9822   else
9823     {
9824       if (IS_AGGR_TYPE_CODE (code))
9825         {
9826           if (IS_AGGR_TYPE (ref)
9827               && ((tag_code == exception_type)
9828                   != (CLASSTYPE_DECLARED_EXCEPTION (ref) == 1)))
9829             {
9830               cp_error ("type `%T' is both exception and aggregate type", ref);
9831               CLASSTYPE_DECLARED_EXCEPTION (ref) = (tag_code == exception_type);
9832             }
9833         }
9834
9835       /* If it no longer looks like a nested type, make sure it's
9836          in global scope.  */
9837       if (b == global_binding_level && !class_binding_level
9838           && IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE)
9839         IDENTIFIER_GLOBAL_VALUE (name) = TYPE_NAME (ref);
9840
9841       if (binfo)
9842         {
9843           tree tt1 = binfo;
9844           tree tt2 = TYPE_BINFO_BASETYPES (ref);
9845
9846           if (TYPE_BINFO_BASETYPES (ref))
9847             for (i = 0; tt1; i++, tt1 = TREE_CHAIN (tt1))
9848               if (TREE_VALUE (tt1) != TYPE_IDENTIFIER (BINFO_TYPE (TREE_VEC_ELT (tt2, i))))
9849                 {
9850                   cp_error ("redeclaration of derivation chain of type `%#T'",
9851                             ref);
9852                   break;
9853                 }
9854
9855           if (tt1 == NULL_TREE)
9856             /* The user told us something we already knew.  */
9857             goto just_return;
9858
9859           /* In C++, since these migrate into the global scope, we must
9860              build them on the permanent obstack.  */
9861           end_temporary_allocation ();
9862         }
9863     }
9864
9865   if (binfo)
9866     {
9867       /* In the declaration `A : X, Y, ... Z' we mark all the types
9868          (A, X, Y, ..., Z) so we can check for duplicates.  */
9869       tree binfos;
9870
9871       SET_CLASSTYPE_MARKED (ref);
9872       BINFO_BASETYPES (TYPE_BINFO (ref)) = binfos = make_tree_vec (len);
9873
9874       for (i = 0; binfo; binfo = TREE_CHAIN (binfo))
9875         {
9876           /* The base of a derived struct is public by default.  */
9877           int via_public
9878             = (TREE_PURPOSE (binfo) == (tree)access_public
9879                || TREE_PURPOSE (binfo) == (tree)access_public_virtual
9880                || (tag_code != class_type
9881                    && (TREE_PURPOSE (binfo) == (tree)access_default
9882                        || TREE_PURPOSE (binfo) == (tree)access_default_virtual)));
9883           int via_protected = TREE_PURPOSE (binfo) == (tree)access_protected;
9884           int via_virtual
9885             = (TREE_PURPOSE (binfo) == (tree)access_private_virtual
9886                || TREE_PURPOSE (binfo) == (tree)access_public_virtual
9887                || TREE_PURPOSE (binfo) == (tree)access_default_virtual);
9888           tree basetype = TREE_TYPE (TREE_VALUE (binfo));
9889           tree base_binfo;
9890
9891           GNU_xref_hier (IDENTIFIER_POINTER (name),
9892                          IDENTIFIER_POINTER (TREE_VALUE (binfo)),
9893                          via_public, via_virtual, 0);
9894
9895           if (basetype && TREE_CODE (basetype) == TYPE_DECL)
9896             basetype = TREE_TYPE (basetype);
9897           if (!basetype || TREE_CODE (basetype) != RECORD_TYPE)
9898             {
9899               error ("base type `%s' fails to be a struct or class type",
9900                      IDENTIFIER_POINTER (TREE_VALUE (binfo)));
9901               continue;
9902             }
9903 #if 1
9904           /* This code replaces similar code in layout_basetypes.  */
9905           else if (TYPE_SIZE (basetype) == NULL_TREE)
9906             {
9907               cp_error ("base class `%T' has incomplete type", basetype);
9908               continue;
9909             }
9910 #endif
9911           else
9912             {
9913               if (CLASSTYPE_MARKED (basetype))
9914                 {
9915                   if (basetype == ref)
9916                     cp_error ("recursive type `%T' undefined", basetype);
9917                   else
9918                     cp_error ("duplicate base type `%T' invalid", basetype);
9919                   continue;
9920                 }
9921
9922               /* Note that the BINFO records which describe individual
9923                  inheritances are *not* shared in the lattice!  They
9924                  cannot be shared because a given baseclass may be
9925                  inherited with different `accessibility' by different
9926                  derived classes.  (Each BINFO record describing an
9927                  individual inheritance contains flags which say what
9928                  the `accessibility' of that particular inheritance is.)  */
9929   
9930               base_binfo = make_binfo (integer_zero_node, basetype,
9931                                   TYPE_BINFO_VTABLE (basetype),
9932                                   TYPE_BINFO_VIRTUALS (basetype), NULL_TREE);
9933  
9934               TREE_VEC_ELT (binfos, i) = base_binfo;
9935               TREE_VIA_PUBLIC (base_binfo) = via_public;
9936               TREE_VIA_PROTECTED (base_binfo) = via_protected;
9937               TREE_VIA_VIRTUAL (base_binfo) = via_virtual;
9938
9939               SET_CLASSTYPE_MARKED (basetype);
9940 #if 0
9941 /* XYZZY TEST VIRTUAL BASECLASSES */
9942 if (CLASSTYPE_N_BASECLASSES (basetype) == NULL_TREE
9943     && TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
9944     && via_virtual == 0)
9945   {
9946     warning ("making type `%s' a virtual baseclass",
9947              TYPE_NAME_STRING (basetype));
9948     via_virtual = 1;
9949   }
9950 #endif
9951               /* We are free to modify these bits because they are meaningless
9952                  at top level, and BASETYPE is a top-level type.  */
9953               if (via_virtual || TYPE_USES_VIRTUAL_BASECLASSES (basetype))
9954                 {
9955                   TYPE_USES_VIRTUAL_BASECLASSES (ref) = 1;
9956                   TYPE_USES_COMPLEX_INHERITANCE (ref) = 1;
9957                 }
9958
9959               TYPE_OVERLOADS_METHOD_CALL_EXPR (ref) |= TYPE_OVERLOADS_METHOD_CALL_EXPR (basetype);
9960               TREE_GETS_NEW (ref) |= TREE_GETS_NEW (basetype);
9961               TREE_GETS_PLACED_NEW (ref) |= TREE_GETS_PLACED_NEW (basetype);
9962               TREE_GETS_DELETE (ref) |= TREE_GETS_DELETE (basetype);
9963               CLASSTYPE_LOCAL_TYPEDECLS (ref) |= CLASSTYPE_LOCAL_TYPEDECLS (basetype);
9964               i += 1;
9965             }
9966         }
9967       if (i)
9968         TREE_VEC_LENGTH (binfos) = i;
9969       else
9970         BINFO_BASETYPES (TYPE_BINFO (ref)) = NULL_TREE;
9971
9972       if (i > 1)
9973         TYPE_USES_MULTIPLE_INHERITANCE (ref) = 1;
9974       else if (i == 1)
9975         TYPE_USES_MULTIPLE_INHERITANCE (ref)
9976           = TYPE_USES_MULTIPLE_INHERITANCE (BINFO_TYPE (TREE_VEC_ELT (binfos, 0)));
9977       if (TYPE_USES_MULTIPLE_INHERITANCE (ref))
9978         TYPE_USES_COMPLEX_INHERITANCE (ref) = 1;
9979
9980       /* Unmark all the types.  */
9981       while (--i >= 0)
9982         CLEAR_CLASSTYPE_MARKED (BINFO_TYPE (TREE_VEC_ELT (binfos, i)));
9983       CLEAR_CLASSTYPE_MARKED (ref);
9984     }
9985
9986  just_return:
9987
9988   /* Until the type is defined, tentatively accept whatever
9989      structure tag the user hands us.  */
9990   if (TYPE_SIZE (ref) == NULL_TREE
9991       && ref != current_class_type
9992       /* Have to check this, in case we have contradictory tag info.  */
9993       && IS_AGGR_TYPE_CODE (TREE_CODE (ref)))
9994     {
9995       if (tag_code == class_type)
9996         CLASSTYPE_DECLARED_CLASS (ref) = 1;
9997       else if (tag_code == record_type || tag_code == signature_type)
9998         CLASSTYPE_DECLARED_CLASS (ref) = 0;
9999     }
10000
10001   pop_obstacks ();
10002
10003   return ref;
10004 }
10005 \f
10006 static tree current_local_enum = NULL_TREE;
10007
10008 /* Begin compiling the definition of an enumeration type.
10009    NAME is its name (or null if anonymous).
10010    Returns the type object, as yet incomplete.
10011    Also records info about it so that build_enumerator
10012    may be used to declare the individual values as they are read.  */
10013
10014 tree
10015 start_enum (name)
10016      tree name;
10017 {
10018   register tree enumtype = NULL_TREE;
10019   struct binding_level *b = inner_binding_level;
10020
10021   /* If this is the real definition for a previous forward reference,
10022      fill in the contents in the same object that used to be the
10023      forward reference.  */
10024
10025   if (name != NULL_TREE)
10026     enumtype = lookup_tag (ENUMERAL_TYPE, name, b, 1);
10027
10028   if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE)
10029     cp_error ("multiple definition of enum `%T'", enumtype);
10030   else
10031     {
10032       enumtype = make_node (ENUMERAL_TYPE);
10033       pushtag (name, enumtype, 0);
10034     }
10035
10036   if (current_class_type)
10037     TREE_ADDRESSABLE (b->tags) = 1;
10038   current_local_enum = NULL_TREE;
10039
10040   if (TYPE_VALUES (enumtype) != NULL_TREE)
10041     /* Completely replace its old definition.
10042        The old enumerators remain defined, however.  */
10043     TYPE_VALUES (enumtype) = NULL_TREE;
10044
10045   /* Initially, set up this enum as like `int'
10046      so that we can create the enumerators' declarations and values.
10047      Later on, the precision of the type may be changed and
10048      it may be laid out again.  */
10049
10050   TYPE_PRECISION (enumtype) = TYPE_PRECISION (integer_type_node);
10051   TYPE_SIZE (enumtype) = NULL_TREE;
10052   fixup_unsigned_type (enumtype);
10053
10054   /* We copy this value because enumerated type constants
10055      are really of the type of the enumerator, not integer_type_node.  */
10056   enum_next_value = copy_node (integer_zero_node);
10057
10058   GNU_xref_decl (current_function_decl, enumtype);
10059   return enumtype;
10060 }
10061
10062 /* After processing and defining all the values of an enumeration type,
10063    install their decls in the enumeration type and finish it off.
10064    ENUMTYPE is the type object and VALUES a list of name-value pairs.
10065    Returns ENUMTYPE.  */
10066
10067 tree
10068 finish_enum (enumtype, values)
10069      register tree enumtype, values;
10070 {
10071   register tree pair, tem;
10072   register HOST_WIDE_INT maxvalue = 0;
10073   register HOST_WIDE_INT minvalue = 0;
10074   register HOST_WIDE_INT i;
10075
10076   TYPE_VALUES (enumtype) = values;
10077
10078   /* Calculate the maximum value of any enumerator in this type.  */
10079
10080   if (values)
10081     {
10082       /* Speed up the main loop by performing some precalculations */
10083
10084       HOST_WIDE_INT value = TREE_INT_CST_LOW (TREE_VALUE (values));
10085       TREE_TYPE (TREE_VALUE (values)) = enumtype;
10086       minvalue = maxvalue = value;
10087       
10088       for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
10089         {
10090           value = TREE_INT_CST_LOW (TREE_VALUE (pair));
10091           if (value > maxvalue)
10092             maxvalue = value;
10093           else if (value < minvalue)
10094             minvalue = value;
10095           TREE_TYPE (TREE_VALUE (pair)) = enumtype;
10096         }
10097     }
10098
10099   if (flag_short_enums)
10100     {
10101       /* Determine the precision this type needs, lay it out, and define it.  */
10102
10103       for (i = maxvalue; i; i >>= 1)
10104         TYPE_PRECISION (enumtype)++;
10105
10106       if (!TYPE_PRECISION (enumtype))
10107         TYPE_PRECISION (enumtype) = 1;
10108
10109       /* Cancel the laying out previously done for the enum type,
10110          so that fixup_unsigned_type will do it over.  */
10111       TYPE_SIZE (enumtype) = NULL_TREE;
10112
10113       fixup_unsigned_type (enumtype);
10114     }
10115
10116   TREE_INT_CST_LOW (TYPE_MAX_VALUE (enumtype)) = maxvalue;
10117
10118   /* An enum can have some negative values; then it is signed.  */
10119   if (minvalue < 0)
10120     {
10121       TREE_INT_CST_LOW (TYPE_MIN_VALUE (enumtype)) = minvalue;
10122       TREE_INT_CST_HIGH (TYPE_MIN_VALUE (enumtype)) = -1;
10123       TREE_UNSIGNED (enumtype) = 0;
10124     }
10125   if (flag_cadillac)
10126     cadillac_finish_enum (enumtype);
10127
10128   /* Fix up all variant types of this enum type.  */
10129   for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
10130     {
10131       TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
10132       TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
10133       TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
10134       TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
10135       TYPE_MODE (tem) = TYPE_MODE (enumtype);
10136       TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
10137       TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
10138       TREE_UNSIGNED (tem) = TREE_UNSIGNED (enumtype);
10139     }
10140
10141   /* Finish debugging output for this type.  */
10142 #if 0
10143   /* @@ Do we ever generate generate ENUMERAL_TYPE nodes for which debugging
10144      information should *not* be generated?  I think not.  */
10145   if (! DECL_IGNORED_P (TYPE_NAME (enumtype)))
10146 #endif
10147     rest_of_type_compilation (enumtype, global_bindings_p ());
10148
10149   return enumtype;
10150 }
10151
10152 /* Build and install a CONST_DECL for one value of the
10153    current enumeration type (one that was begun with start_enum).
10154    Return a tree-list containing the name and its value.
10155    Assignment of sequential values by default is handled here.  */
10156
10157 tree
10158 build_enumerator (name, value)
10159      tree name, value;
10160 {
10161   tree decl, result;
10162   /* Change this to zero if we find VALUE is not shareable.  */
10163   int shareable = 1;
10164
10165   /* Remove no-op casts from the value.  */
10166   if (value)
10167     STRIP_TYPE_NOPS (value);
10168
10169   /* Validate and default VALUE.  */
10170   if (value != NULL_TREE)
10171     {
10172       if (TREE_READONLY_DECL_P (value))
10173         {
10174           value = decl_constant_value (value);
10175           shareable = 0;
10176         }
10177
10178       if (TREE_CODE (value) == INTEGER_CST)
10179         {
10180           value = default_conversion (value);
10181           constant_expression_warning (value);
10182         }
10183       else
10184         {
10185           error ("enumerator value for `%s' not integer constant",
10186                  IDENTIFIER_POINTER (name));
10187           value = NULL_TREE;
10188         }
10189     }
10190
10191   /* The order of things is reversed here so that we
10192      can check for possible sharing of enum values,
10193      to keep that from happening.  */
10194   /* Default based on previous value.  */
10195   if (value == NULL_TREE)
10196     value = enum_next_value;
10197
10198   /* Remove no-op casts from the value.  */
10199   if (value)
10200     STRIP_TYPE_NOPS (value);
10201
10202   /* Make up for hacks in lex.c.  */
10203   if (value == integer_zero_node)
10204     value = build_int_2 (0, 0);
10205   else if (value == integer_one_node)
10206     value = build_int_2 (1, 0);
10207   else if (TREE_CODE (value) == INTEGER_CST
10208            && (shareable == 0
10209                || TREE_CODE (TREE_TYPE (value)) == ENUMERAL_TYPE))
10210     {
10211       value = copy_node (value);
10212       TREE_TYPE (value) = integer_type_node;
10213     }
10214
10215   result = saveable_tree_cons (name, value, NULL_TREE);
10216
10217   /* C++ associates enums with global, function, or class declarations.  */
10218
10219   /* There are a number of cases we need to be aware of here:
10220                                 current_class_type      current_function_decl
10221      * global enums             NULL                    NULL
10222      * fn-local enum            NULL                    SET
10223      * class-local enum         SET                     NULL
10224      * class->fn->enum          SET                     SET
10225      * fn->class->enum          SET                     SET
10226
10227      Those last two make life interesting.  If it's a fn-local enum which is
10228      itself inside a class, we need the enum to go into the fn's decls (our
10229      second case below).  But if it's a class-local enum and the class itself
10230      is inside a function, we need that enum to go into the decls for the
10231      class.  To achieve this last goal, we must see if, when both
10232      current_class_decl and current_function_decl are set, the class was
10233      declared inside that function.  If so, we know to put the enum into
10234      the class's scope.  */
10235      
10236   if ((current_class_type && ! current_function_decl)
10237       || (current_class_type && current_function_decl
10238           && TYPE_CONTEXT (current_class_type) == current_function_decl))
10239     {
10240       /* This enum declaration is local to the class, so we must put
10241          it in that class's list of decls.  */
10242       decl = build_lang_field_decl (CONST_DECL, name, integer_type_node);
10243       DECL_INITIAL (decl) = value;
10244       TREE_READONLY (decl) = 1;
10245       pushdecl_class_level (decl);
10246       TREE_CHAIN (decl) = current_local_enum;
10247       current_local_enum = decl;
10248     }
10249   else
10250     {
10251       /* It's a global enum, or it's local to a function.  (Note local to
10252          a function could mean local to a class method.  */
10253       decl = build_decl (CONST_DECL, name, integer_type_node);
10254       DECL_INITIAL (decl) = value;
10255
10256       pushdecl (decl);
10257       GNU_xref_decl (current_function_decl, decl);
10258     }
10259
10260   /* Set basis for default for next value.  */
10261   enum_next_value = build_binary_op_nodefault (PLUS_EXPR, value,
10262                                                integer_one_node, PLUS_EXPR);
10263   if (enum_next_value == integer_one_node)
10264     enum_next_value = copy_node (enum_next_value);
10265
10266   return result;
10267 }
10268
10269 tree
10270 grok_enum_decls (type, decl)
10271      tree type, decl;
10272 {
10273   tree d = current_local_enum;
10274   
10275   if (d == NULL_TREE)
10276     return decl;
10277   
10278   while (1)
10279     {
10280       TREE_TYPE (d) = type;
10281       if (TREE_CHAIN (d) == NULL_TREE)
10282         {
10283           TREE_CHAIN (d) = decl;
10284           break;
10285         }
10286       d = TREE_CHAIN (d);
10287     }
10288
10289   decl = current_local_enum;
10290   current_local_enum = NULL_TREE;
10291   
10292   return decl;
10293 }
10294 \f
10295 /* Create the FUNCTION_DECL for a function definition.
10296    DECLSPECS and DECLARATOR are the parts of the declaration;
10297    they describe the function's name and the type it returns,
10298    but twisted together in a fashion that parallels the syntax of C.
10299
10300    This function creates a binding context for the function body
10301    as well as setting up the FUNCTION_DECL in current_function_decl.
10302
10303    Returns 1 on success.  If the DECLARATOR is not suitable for a function
10304    (it defines a datum instead), we return 0, which tells
10305    yyparse to report a parse error.
10306
10307    For C++, we must first check whether that datum makes any sense.
10308    For example, "class A local_a(1,2);" means that variable local_a
10309    is an aggregate of type A, which should have a constructor
10310    applied to it with the argument list [1, 2].
10311
10312    @@ There is currently no way to retrieve the storage
10313    @@ allocated to FUNCTION (or all of its parms) if we return
10314    @@ something we had previously.  */
10315
10316 int
10317 start_function (declspecs, declarator, raises, pre_parsed_p)
10318      tree declarator, declspecs, raises;
10319      int pre_parsed_p;
10320 {
10321   extern tree EHS_decl;
10322   tree decl1, olddecl;
10323   tree ctype = NULL_TREE;
10324   tree fntype;
10325   tree restype;
10326   extern int have_extern_spec;
10327   extern int used_extern_spec;
10328   int doing_friend = 0;
10329
10330   if (flag_handle_exceptions && EHS_decl == NULL_TREE)
10331     init_exception_processing_1 ();
10332
10333   /* Sanity check.  */
10334   my_friendly_assert (TREE_VALUE (void_list_node) == void_type_node, 160);
10335   my_friendly_assert (TREE_CHAIN (void_list_node) == NULL_TREE, 161);
10336
10337   /* Assume, until we see it does. */
10338   current_function_returns_value = 0;
10339   current_function_returns_null = 0;
10340   warn_about_return_type = 0;
10341   current_extern_inline = 0;
10342   current_function_assigns_this = 0;
10343   current_function_just_assigned_this = 0;
10344   current_function_parms_stored = 0;
10345   original_result_rtx = NULL_RTX;
10346   current_function_obstack_index = 0;
10347   current_function_obstack_usage = 0;
10348
10349   clear_temp_name ();
10350
10351   /* This should only be done once on the top most decl. */
10352   if (have_extern_spec && !used_extern_spec)
10353     {
10354       declspecs = decl_tree_cons (NULL_TREE, get_identifier ("extern"), declspecs);
10355       used_extern_spec = 1;
10356     }
10357
10358   if (pre_parsed_p)
10359     {
10360       decl1 = declarator;
10361
10362       if (! DECL_ARGUMENTS (decl1)
10363           && !DECL_STATIC_FUNCTION_P (decl1)
10364           && DECL_CONTEXT (decl1)
10365           && DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl1)))
10366           && IDENTIFIER_TEMPLATE (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl1)))))
10367         {
10368           cp_error ("redeclaration of `%#D'", decl1);
10369           if (IDENTIFIER_CLASS_VALUE (DECL_NAME (decl1)))
10370             cp_error_at ("previous declaration here", IDENTIFIER_CLASS_VALUE (DECL_NAME (decl1)));
10371           else if (IDENTIFIER_GLOBAL_VALUE (DECL_NAME (decl1)))
10372             cp_error_at ("previous declaration here", IDENTIFIER_GLOBAL_VALUE (DECL_NAME (decl1)));
10373         }
10374
10375       last_function_parms = DECL_ARGUMENTS (decl1);
10376       last_function_parm_tags = NULL_TREE;
10377       fntype = TREE_TYPE (decl1);
10378       if (TREE_CODE (fntype) == METHOD_TYPE)
10379         ctype = TYPE_METHOD_BASETYPE (fntype);
10380
10381       /* ANSI C++ June 5 1992 WP 11.4.5.  A friend function defined in a
10382          class is in the (lexical) scope of the class in which it is
10383          defined.  */
10384       if (!ctype && DECL_FRIEND_P (decl1))
10385         {
10386           ctype = DECL_CLASS_CONTEXT (decl1);
10387
10388           /* CTYPE could be null here if we're dealing with a template;
10389              for example, `inline friend float foo()' inside a template
10390              will have no CTYPE set.  */
10391           if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
10392             ctype = NULL_TREE;
10393           else
10394             doing_friend = 1;
10395         }
10396
10397       if ( !(DECL_VINDEX (decl1)
10398              && write_virtuals >= 2
10399              && CLASSTYPE_VTABLE_NEEDS_WRITING (ctype)))
10400         current_extern_inline = DECL_THIS_EXTERN (decl1) && DECL_INLINE (decl1);
10401
10402       raises = TYPE_RAISES_EXCEPTIONS (fntype);
10403
10404       /* In a fcn definition, arg types must be complete.  */
10405       require_complete_types_for_parms (last_function_parms);
10406     }
10407   else
10408     {
10409       decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, raises);
10410       /* If the declarator is not suitable for a function definition,
10411          cause a syntax error.  */
10412       if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL) return 0;
10413
10414       fntype = TREE_TYPE (decl1);
10415
10416       restype = TREE_TYPE (fntype);
10417       if (IS_AGGR_TYPE (restype) && ! TYPE_PTRMEMFUNC_P (restype)
10418           && ! CLASSTYPE_GOT_SEMICOLON (restype))
10419         {
10420           cp_error ("semicolon missing after declaration of `%#T'", restype);
10421           shadow_tag (build_tree_list (NULL_TREE, restype));
10422           CLASSTYPE_GOT_SEMICOLON (restype) = 1;
10423           if (TREE_CODE (fntype) == FUNCTION_TYPE)
10424             fntype = build_function_type (integer_type_node,
10425                                           TYPE_ARG_TYPES (fntype));
10426           else
10427             fntype = build_cplus_method_type (build_type_variant (TYPE_METHOD_BASETYPE (fntype), TREE_READONLY (decl1), TREE_SIDE_EFFECTS (decl1)),
10428                                               integer_type_node,
10429                                               TYPE_ARG_TYPES (fntype));
10430           TREE_TYPE (decl1) = fntype;
10431         }
10432
10433       if (TREE_CODE (fntype) == METHOD_TYPE)
10434         ctype = TYPE_METHOD_BASETYPE (fntype);
10435       else if (IDENTIFIER_LENGTH (DECL_NAME (decl1)) == 4
10436                && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (decl1)), "main")
10437                && DECL_CONTEXT (decl1) == NULL_TREE)
10438         {
10439           /* If this doesn't return integer_type, complain.  */
10440           if (TREE_TYPE (TREE_TYPE (decl1)) != integer_type_node)
10441             {
10442               warning ("return type for `main' changed to integer type");
10443               TREE_TYPE (decl1) = fntype = default_function_type;
10444             }
10445           warn_about_return_type = 0;
10446         }
10447     }
10448
10449   /* Warn if function was previously implicitly declared
10450      (but not if we warned then).  */
10451   if (! warn_implicit
10452       && IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)) != NULL_TREE)
10453     cp_warning_at ("`%D' implicitly declared before its definition", IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)));
10454
10455   current_function_decl = decl1;
10456
10457   if (flag_cadillac)
10458     cadillac_start_function (decl1);
10459   else
10460     announce_function (decl1);
10461
10462   if (TYPE_SIZE (TREE_TYPE (fntype)) == NULL_TREE)
10463     {
10464       if (IS_AGGR_TYPE (TREE_TYPE (fntype)))
10465         error_with_aggr_type (TREE_TYPE (fntype),
10466                               "return-type `%s' is an incomplete type");
10467       else
10468         error ("return-type is an incomplete type");
10469
10470       /* Make it return void instead, but don't change the
10471          type of the DECL_RESULT, in case we have a named return value.  */
10472       if (ctype)
10473         TREE_TYPE (decl1)
10474           = build_cplus_method_type (build_type_variant (ctype,
10475                                                          TREE_READONLY (decl1),
10476                                                          TREE_SIDE_EFFECTS (decl1)),
10477                                      void_type_node,
10478                                      FUNCTION_ARG_CHAIN (decl1));
10479       else
10480         TREE_TYPE (decl1)
10481           = build_function_type (void_type_node,
10482                                  TYPE_ARG_TYPES (TREE_TYPE (decl1)));
10483       DECL_RESULT (decl1) = build_decl (RESULT_DECL, 0, TREE_TYPE (fntype));
10484     }
10485
10486   if (warn_about_return_type)
10487     warning ("return-type defaults to `int'");
10488
10489   /* Make the init_value nonzero so pushdecl knows this is not tentative.
10490      error_mark_node is replaced below (in poplevel) with the BLOCK.  */
10491   DECL_INITIAL (decl1) = error_mark_node;
10492
10493   /* Didn't get anything from C.  */
10494   olddecl = NULL_TREE;
10495
10496   /* This function exists in static storage.
10497      (This does not mean `static' in the C sense!)  */
10498   TREE_STATIC (decl1) = 1;
10499
10500   /* If this function belongs to an interface, it is public.
10501      If it belongs to someone else's interface, it is also external.
10502      It doesn't matter whether it's inline or not.  */
10503   if (interface_unknown == 0)
10504     {
10505       TREE_PUBLIC (decl1) = 1;
10506       DECL_EXTERNAL (decl1) = (interface_only
10507                                || (DECL_INLINE (decl1)
10508                                    && ! flag_implement_inlines));
10509     }
10510   else
10511     /* This is a definition, not a reference.
10512        So normally clear DECL_EXTERNAL.
10513        However, `extern inline' acts like a declaration except for
10514        defining how to inline.  So set DECL_EXTERNAL in that case.  */
10515     DECL_EXTERNAL (decl1) = current_extern_inline;
10516
10517   /* Record the decl so that the function name is defined.
10518      If we already have a decl for this name, and it is a FUNCTION_DECL,
10519      use the old decl.  */
10520
10521   if (pre_parsed_p == 0)
10522     {
10523       current_function_decl = decl1 = pushdecl (decl1);
10524       DECL_MAIN_VARIANT (decl1) = decl1;
10525       fntype = TREE_TYPE (decl1);
10526     }
10527   else
10528     current_function_decl = decl1;
10529
10530   if (ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1))
10531     {
10532       if (TREE_CODE (fntype) == METHOD_TYPE)
10533         TREE_TYPE (decl1) = fntype
10534           = build_function_type (TREE_TYPE (fntype),
10535                                  TREE_CHAIN (TYPE_ARG_TYPES (fntype)));
10536       last_function_parms = TREE_CHAIN (last_function_parms);
10537       DECL_ARGUMENTS (decl1) = last_function_parms;
10538       ctype = NULL_TREE;
10539     }
10540   restype = TREE_TYPE (fntype);
10541
10542   pushlevel (0);
10543   current_binding_level->parm_flag = 1;
10544
10545   /* Save the parm names or decls from this function's declarator
10546      where store_parm_decls will find them.  */
10547   current_function_parms = last_function_parms;
10548   current_function_parm_tags = last_function_parm_tags;
10549
10550   GNU_xref_function (decl1, current_function_parms);
10551
10552   make_function_rtl (decl1);
10553
10554   if (ctype)
10555     {
10556       push_nested_class (ctype, 1);
10557
10558       /* If we're compiling a friend function, neither of the variables
10559          current_class_decl nor current_class_type will have values.  */
10560       if (! doing_friend)
10561         {
10562           /* We know that this was set up by `grokclassfn'.
10563              We do not wait until `store_parm_decls', since evil
10564              parse errors may never get us to that point.  Here
10565              we keep the consistency between `current_class_type'
10566              and `current_class_decl'.  */
10567           current_class_decl = last_function_parms;
10568           my_friendly_assert (current_class_decl != NULL_TREE
10569                   && TREE_CODE (current_class_decl) == PARM_DECL, 162);
10570           if (TREE_CODE (TREE_TYPE (current_class_decl)) == POINTER_TYPE)
10571             {
10572               tree variant = TREE_TYPE (TREE_TYPE (current_class_decl));
10573               if (CLASSTYPE_INST_VAR (ctype) == NULL_TREE)
10574                 {
10575                   /* Can't call build_indirect_ref here, because it has special
10576                      logic to return C_C_D given this argument.  */
10577                   C_C_D = build1 (INDIRECT_REF, current_class_type, current_class_decl);
10578                   CLASSTYPE_INST_VAR (ctype) = C_C_D;
10579                 }
10580               else
10581                 {
10582                   C_C_D = CLASSTYPE_INST_VAR (ctype);
10583                   /* `current_class_decl' is different for every
10584                      function we compile.  */
10585                   TREE_OPERAND (C_C_D, 0) = current_class_decl;
10586                 }
10587               TREE_READONLY (C_C_D) = TYPE_READONLY (variant);
10588               TREE_SIDE_EFFECTS (C_C_D) = TYPE_VOLATILE (variant);
10589               TREE_THIS_VOLATILE (C_C_D) = TYPE_VOLATILE (variant);
10590             }
10591           else
10592             C_C_D = current_class_decl;
10593         }
10594     }
10595   else
10596     {
10597       if (DECL_STATIC_FUNCTION_P (decl1))
10598         push_nested_class (DECL_CONTEXT (decl1), 2);
10599       else
10600         push_memoized_context (0, 1);
10601     }
10602
10603   /* Allocate further tree nodes temporarily during compilation
10604      of this function only.  Tiemann moved up here from bottom of fn.  */
10605   temporary_allocation ();
10606
10607   /* Promote the value to int before returning it.  */
10608   if (C_PROMOTING_INTEGER_TYPE_P (restype))
10609     {
10610       /* It retains unsignedness if traditional or if it isn't
10611          really getting wider.  */
10612       if (TREE_UNSIGNED (restype)
10613           && (flag_traditional
10614               || TYPE_PRECISION (restype)
10615                    == TYPE_PRECISION (integer_type_node)))
10616         restype = unsigned_type_node;
10617       else
10618         restype = integer_type_node;
10619     }
10620   if (DECL_RESULT (decl1) == NULL_TREE)
10621     DECL_RESULT (decl1) = build_decl (RESULT_DECL, 0, restype);
10622
10623   if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl1)))
10624     {
10625       dtor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
10626       ctor_label = NULL_TREE;
10627     }
10628   else
10629     {
10630       dtor_label = NULL_TREE;
10631       if (DECL_CONSTRUCTOR_P (decl1))
10632         ctor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
10633     }
10634
10635   /* If this fcn was already referenced via a block-scope `extern' decl
10636      (or an implicit decl), propagate certain information about the usage.  */
10637   if (TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (decl1)))
10638     TREE_ADDRESSABLE (decl1) = 1;
10639
10640   return 1;
10641 }
10642 \f
10643 /* Store the parameter declarations into the current function declaration.
10644    This is called after parsing the parameter declarations, before
10645    digesting the body of the function.
10646
10647    Also install to binding contour return value identifier, if any.  */
10648
10649 void
10650 store_parm_decls ()
10651 {
10652   register tree fndecl = current_function_decl;
10653   register tree parm;
10654   int parms_have_cleanups = 0;
10655   tree eh_decl;
10656
10657   /* This is either a chain of PARM_DECLs (when a prototype is used).  */
10658   tree specparms = current_function_parms;
10659
10660   /* This is a list of types declared among parms in a prototype.  */
10661   tree parmtags = current_function_parm_tags;
10662
10663   /* This is a chain of any other decls that came in among the parm
10664      declarations.  If a parm is declared with  enum {foo, bar} x;
10665      then CONST_DECLs for foo and bar are put here.  */
10666   tree nonparms = NULL_TREE;
10667
10668   if (current_binding_level == global_binding_level)
10669     fatal ("parse errors have confused me too much");
10670
10671   /* Initialize RTL machinery.  */
10672   init_function_start (fndecl, input_filename, lineno);
10673
10674   /* Declare __FUNCTION__ and __PRETTY_FUNCTION__ for this function.  */
10675   declare_function_name ();
10676
10677   /* Create a binding level for the parms.  */
10678   expand_start_bindings (0);
10679
10680   /* Prepare to catch raises, if appropriate.  */
10681   if (flag_handle_exceptions)
10682     {
10683       /* Get this cleanup to be run last, since it
10684          is a call to `longjmp'.  */
10685       setup_exception_throw_decl ();
10686       eh_decl = current_binding_level->names;
10687       current_binding_level->names = TREE_CHAIN (current_binding_level->names);
10688     }
10689   if (flag_handle_exceptions)
10690     expand_start_try (integer_one_node, 0, 1);
10691
10692   if (specparms != NULL_TREE)
10693     {
10694       /* This case is when the function was defined with an ANSI prototype.
10695          The parms already have decls, so we need not do anything here
10696          except record them as in effect
10697          and complain if any redundant old-style parm decls were written.  */
10698
10699       register tree next;
10700
10701       /* Must clear this because it might contain TYPE_DECLs declared
10702          at class level.  */
10703       storedecls (NULL_TREE);
10704       for (parm = nreverse (specparms); parm; parm = next)
10705         {
10706           next = TREE_CHAIN (parm);
10707           if (TREE_CODE (parm) == PARM_DECL)
10708             {
10709               tree cleanup = maybe_build_cleanup (parm);
10710               if (DECL_NAME (parm) == NULL_TREE)
10711                 {
10712 #if 0
10713                   cp_error_at ("parameter name omitted", parm);
10714 #else
10715                   /* for C++, this is not an error.  */
10716                   pushdecl (parm);
10717 #endif
10718                 }
10719               else if (TYPE_MAIN_VARIANT (TREE_TYPE (parm)) == void_type_node)
10720                 cp_error ("parameter `%D' declared void", parm);
10721               else
10722                 {
10723                   /* Now fill in DECL_REFERENCE_SLOT for any of the parm decls.
10724                      A parameter is assumed not to have any side effects.
10725                      If this should change for any reason, then this
10726                      will have to wrap the bashed reference type in a save_expr.
10727                      
10728                      Also, if the parameter type is declared to be an X
10729                      and there is an X(X&) constructor, we cannot lay it
10730                      into the stack (any more), so we make this parameter
10731                      look like it is really of reference type.  Functions
10732                      which pass parameters to this function will know to
10733                      create a temporary in their frame, and pass a reference
10734                      to that.  */
10735
10736                   if (TREE_CODE (TREE_TYPE (parm)) == REFERENCE_TYPE
10737                       && TYPE_SIZE (TREE_TYPE (TREE_TYPE (parm))))
10738                     SET_DECL_REFERENCE_SLOT (parm, convert_from_reference (parm));
10739
10740                   pushdecl (parm);
10741                 }
10742               if (cleanup)
10743                 {
10744                   expand_decl (parm);
10745                   expand_decl_cleanup (parm, cleanup);
10746                   parms_have_cleanups = 1;
10747                 }
10748             }
10749           else
10750             {
10751               /* If we find an enum constant or a type tag,
10752                  put it aside for the moment.  */
10753               TREE_CHAIN (parm) = NULL_TREE;
10754               nonparms = chainon (nonparms, parm);
10755             }
10756         }
10757
10758       /* Get the decls in their original chain order
10759          and record in the function.  This is all and only the
10760          PARM_DECLs that were pushed into scope by the loop above.  */
10761       DECL_ARGUMENTS (fndecl) = getdecls ();
10762
10763       storetags (chainon (parmtags, gettags ()));
10764     }
10765   else
10766     DECL_ARGUMENTS (fndecl) = NULL_TREE;
10767
10768   /* Now store the final chain of decls for the arguments
10769      as the decl-chain of the current lexical scope.
10770      Put the enumerators in as well, at the front so that
10771      DECL_ARGUMENTS is not modified.  */
10772
10773   storedecls (chainon (nonparms, DECL_ARGUMENTS (fndecl)));
10774
10775   /* Initialize the RTL code for the function.  */
10776   DECL_SAVED_INSNS (fndecl) = NULL_RTX;
10777   expand_function_start (fndecl, parms_have_cleanups);
10778
10779   if (flag_handle_exceptions)
10780     {
10781       /* Make the throw decl visible at this level, just
10782          not in the way of the parameters.  */
10783       pushdecl (eh_decl);
10784       expand_decl_init (eh_decl);
10785     }
10786
10787   /* Create a binding contour which can be used to catch
10788      cleanup-generated temporaries.  Also, if the return value needs or
10789      has initialization, deal with that now.  */
10790   if (parms_have_cleanups)
10791     {
10792       pushlevel (0);
10793       expand_start_bindings (0);
10794     }
10795
10796   current_function_parms_stored = 1;
10797
10798   if (flag_gc)
10799     {
10800       maybe_gc_cleanup = build_tree_list (NULL_TREE, error_mark_node);
10801       expand_decl_cleanup (NULL_TREE, maybe_gc_cleanup);
10802     }
10803
10804   /* If this function is `main', emit a call to `__main'
10805      to run global initializers, etc.  */
10806   if (DECL_NAME (fndecl)
10807       && IDENTIFIER_LENGTH (DECL_NAME (fndecl)) == 4
10808       && strcmp (IDENTIFIER_POINTER (DECL_NAME (fndecl)), "main") == 0
10809       && DECL_CONTEXT (fndecl) == NULL_TREE)
10810     {
10811       expand_main_function ();
10812
10813       if (flag_gc)
10814         expand_expr (build_function_call (lookup_name (get_identifier ("__gc_main"), 0), NULL_TREE),
10815                      0, VOIDmode, 0);
10816
10817       if (flag_dossier)
10818         output_builtin_tdesc_entries ();
10819     }
10820 }
10821
10822 /* Bind a name and initialization to the return value of
10823    the current function.  */
10824 void
10825 store_return_init (return_id, init)
10826      tree return_id, init;
10827 {
10828   tree decl = DECL_RESULT (current_function_decl);
10829
10830   if (pedantic)
10831     /* Give this error as many times as there are occurrences,
10832        so that users can use Emacs compilation buffers to find
10833        and fix all such places.  */
10834     pedwarn ("ANSI C++ does not permit named return values");
10835
10836   if (return_id != NULL_TREE)
10837     {
10838       if (DECL_NAME (decl) == NULL_TREE)
10839         {
10840           DECL_NAME (decl) = return_id;
10841           DECL_ASSEMBLER_NAME (decl) = return_id;
10842         }
10843       else
10844         error ("return identifier `%s' already in place",
10845                IDENTIFIER_POINTER (DECL_NAME (decl)));
10846     }
10847
10848   /* Can't let this happen for constructors.  */
10849   if (DECL_CONSTRUCTOR_P (current_function_decl))
10850     {
10851       error ("can't redefine default return value for constructors");
10852       return;
10853     }
10854
10855   /* If we have a named return value, put that in our scope as well.  */
10856   if (DECL_NAME (decl) != NULL_TREE)
10857     {
10858       /* If this named return value comes in a register,
10859          put it in a pseudo-register.  */
10860       if (DECL_REGISTER (decl))
10861         {
10862           original_result_rtx = DECL_RTL (decl);
10863           DECL_RTL (decl) = gen_reg_rtx (DECL_MODE (decl));
10864         }
10865
10866       /* Let `finish_decl' know that this initializer is ok.  */
10867       DECL_INITIAL (decl) = init;
10868       pushdecl (decl);
10869       finish_decl (decl, init, 0, 0);
10870     }
10871 }
10872
10873 #if 0
10874 /* Generate code for default X() constructor.  */
10875 static void
10876 build_default_constructor (fndecl)
10877      tree fndecl;
10878 {
10879   int i = CLASSTYPE_N_BASECLASSES (current_class_type);
10880   tree parm = TREE_CHAIN (DECL_ARGUMENTS (fndecl));
10881   tree fields = TYPE_FIELDS (current_class_type);
10882   tree binfos = TYPE_BINFO_BASETYPES (current_class_type);
10883
10884   if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
10885     parm = TREE_CHAIN (parm);
10886   parm = DECL_REFERENCE_SLOT (parm);
10887
10888   while (--i >= 0)
10889     {
10890       tree basetype = TREE_VEC_ELT (binfos, i);
10891       if (TYPE_HAS_INIT_REF (basetype))
10892         {
10893           tree name = TYPE_NAME (basetype);
10894           if (TREE_CODE (name) == TYPE_DECL)
10895             name = DECL_NAME (name);
10896           current_base_init_list = tree_cons (name, parm, current_base_init_list);
10897         }
10898     }
10899   for (; fields; fields = TREE_CHAIN (fields))
10900     {
10901       tree name, init;
10902       if (TREE_STATIC (fields))
10903         continue;
10904       if (TREE_CODE (fields) != FIELD_DECL)
10905         continue;
10906       if (DECL_NAME (fields))
10907         {
10908           if (VFIELD_NAME_P (DECL_NAME (fields)))
10909             continue;
10910           if (VBASE_NAME_P (DECL_NAME (fields)))
10911             continue;
10912
10913           /* True for duplicate members.  */
10914           if (IDENTIFIER_CLASS_VALUE (DECL_NAME (fields)) != fields)
10915             continue;
10916         }
10917
10918       init = build (COMPONENT_REF, TREE_TYPE (fields), parm, fields);
10919       init = build_tree_list (NULL_TREE, init);
10920
10921       current_member_init_list
10922         = tree_cons (DECL_NAME (fields), init, current_member_init_list);
10923     }
10924 }
10925 #endif
10926 \f
10927 /* Finish up a function declaration and compile that function
10928    all the way to assembler language output.  The free the storage
10929    for the function definition.
10930
10931    This is called after parsing the body of the function definition.
10932    LINENO is the current line number.
10933
10934    C++: CALL_POPLEVEL is non-zero if an extra call to poplevel
10935    (and expand_end_bindings) must be made to take care of the binding
10936    contour for the base initializers.  This is only relevant for
10937    constructors.  */
10938
10939 void
10940 finish_function (lineno, call_poplevel)
10941      int lineno;
10942      int call_poplevel;
10943 {
10944   register tree fndecl = current_function_decl;
10945   tree fntype, ctype = NULL_TREE;
10946   rtx head, last_parm_insn, mark;
10947   extern int sets_exception_throw_decl;
10948   /* Label to use if this function is supposed to return a value.  */
10949   tree no_return_label = NULL_TREE;
10950   tree decls = NULL_TREE;
10951
10952   /* When we get some parse errors, we can end up without a
10953      current_function_decl, so cope.  */
10954   if (fndecl == NULL_TREE)
10955     return;
10956
10957   fntype = TREE_TYPE (fndecl);
10958
10959 /*  TREE_READONLY (fndecl) = 1;
10960     This caused &foo to be of type ptr-to-const-function
10961     which then got a warning when stored in a ptr-to-function variable.  */
10962
10963   /* This happens on strange parse errors.  */
10964   if (! current_function_parms_stored)
10965     {
10966       call_poplevel = 0;
10967       store_parm_decls ();
10968     }
10969
10970   if (write_symbols != NO_DEBUG && TREE_CODE (fntype) != METHOD_TYPE)
10971     {
10972       tree ttype = target_type (fntype);
10973       tree parmdecl;
10974
10975       if (IS_AGGR_TYPE (ttype))
10976         /* Let debugger know it should output info for this type.  */
10977         note_debug_info_needed (ttype);
10978
10979       for (parmdecl = DECL_ARGUMENTS (fndecl); parmdecl; parmdecl = TREE_CHAIN (parmdecl))
10980         {
10981           ttype = target_type (TREE_TYPE (parmdecl));
10982           if (IS_AGGR_TYPE (ttype))
10983             /* Let debugger know it should output info for this type.  */
10984             note_debug_info_needed (ttype);
10985         }
10986     }
10987
10988   /* Clean house because we will need to reorder insns here.  */
10989   do_pending_stack_adjust ();
10990
10991   if (dtor_label)
10992     {
10993       tree binfo = TYPE_BINFO (current_class_type);
10994       tree cond = integer_one_node;
10995       tree exprstmt, vfields;
10996       tree in_charge_node = lookup_name (in_charge_identifier, 0);
10997       tree virtual_size;
10998       int ok_to_optimize_dtor = 0;
10999
11000       if (current_function_assigns_this)
11001         cond = build (NE_EXPR, integer_type_node,
11002                       current_class_decl, integer_zero_node);
11003       else
11004         {
11005           int n_baseclasses = CLASSTYPE_N_BASECLASSES (current_class_type);
11006
11007           /* If this destructor is empty, then we don't need to check
11008              whether `this' is NULL in some cases.  */
11009           mark = get_last_insn ();
11010           last_parm_insn = get_first_nonparm_insn ();
11011
11012           if ((flag_this_is_variable & 1) == 0)
11013             ok_to_optimize_dtor = 1;
11014           else if (mark == last_parm_insn)
11015             ok_to_optimize_dtor
11016               = (n_baseclasses == 0
11017                  || (n_baseclasses == 1
11018                      && TYPE_HAS_DESTRUCTOR (TYPE_BINFO_BASETYPE (current_class_type, 0))));
11019         }
11020
11021       /* These initializations might go inline.  Protect
11022          the binding level of the parms.  */
11023       pushlevel (0);
11024       expand_start_bindings (0);
11025
11026       if (current_function_assigns_this)
11027         {
11028           current_function_assigns_this = 0;
11029           current_function_just_assigned_this = 0;
11030         }
11031
11032       /* Generate the code to call destructor on base class.
11033          If this destructor belongs to a class with virtual
11034          functions, then set the virtual function table
11035          pointer to represent the type of our base class.  */
11036
11037       /* This side-effect makes call to `build_delete' generate the
11038          code we have to have at the end of this destructor.  */
11039       TYPE_HAS_DESTRUCTOR (current_class_type) = 0;
11040
11041       /* These are two cases where we cannot delegate deletion.  */
11042       if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type)
11043           || TREE_GETS_DELETE (current_class_type))
11044         exprstmt = build_delete (current_class_type, C_C_D, integer_zero_node,
11045                                  LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
11046       else
11047         exprstmt = build_delete (current_class_type, C_C_D, in_charge_node,
11048                                  LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
11049
11050       /* If we did not assign to this, then `this' is non-zero at
11051          the end of a destructor.  As a special optimization, don't
11052          emit test if this is an empty destructor.  If it does nothing,
11053          it does nothing.  If it calls a base destructor, the base
11054          destructor will perform the test.  */
11055
11056       if (exprstmt != error_mark_node
11057           && (TREE_CODE (exprstmt) != NOP_EXPR
11058               || TREE_OPERAND (exprstmt, 0) != integer_zero_node
11059               || TYPE_USES_VIRTUAL_BASECLASSES (current_class_type)))
11060         {
11061           expand_label (dtor_label);
11062           if (cond != integer_one_node)
11063             expand_start_cond (cond, 0);
11064           if (exprstmt != void_zero_node)
11065             /* Don't call `expand_expr_stmt' if we're not going to do
11066                anything, since -Wall will give a diagnostic.  */
11067             expand_expr_stmt (exprstmt);
11068
11069           /* Run destructor on all virtual baseclasses.  */
11070           if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
11071             {
11072               tree vbases = nreverse (copy_list (CLASSTYPE_VBASECLASSES (current_class_type)));
11073               expand_start_cond (build (BIT_AND_EXPR, integer_type_node,
11074                                         in_charge_node, integer_two_node), 0);
11075               while (vbases)
11076                 {
11077                   if (TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (vbases)))
11078                     {
11079                       tree ptr = convert_pointer_to_vbase (vbases, current_class_decl);
11080                       expand_expr_stmt (build_delete (TYPE_POINTER_TO (BINFO_TYPE (vbases)),
11081                                                       ptr, integer_zero_node,
11082                                                       LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR|LOOKUP_HAS_IN_CHARGE, 0));
11083                     }
11084                   vbases = TREE_CHAIN (vbases);
11085                 }
11086               expand_end_cond ();
11087             }
11088
11089           do_pending_stack_adjust ();
11090           if (cond != integer_one_node)
11091             expand_end_cond ();
11092         }
11093
11094       TYPE_HAS_DESTRUCTOR (current_class_type) = 1;
11095
11096       virtual_size = c_sizeof (current_class_type);
11097
11098       /* At the end, call delete if that's what's requested.  */
11099       if (TREE_GETS_DELETE (current_class_type))
11100         /* This NOP_EXPR means we are in a static call context.  */
11101         exprstmt =
11102           build_method_call
11103             (build_indirect_ref
11104              (build1 (NOP_EXPR, TYPE_POINTER_TO (current_class_type),
11105                       error_mark_node),
11106               NULL_PTR),
11107              ansi_opname[(int) DELETE_EXPR],
11108              tree_cons (NULL_TREE, current_class_decl,
11109                         build_tree_list (NULL_TREE, virtual_size)),
11110              NULL_TREE, LOOKUP_NORMAL);
11111       else if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
11112         exprstmt = build_x_delete (ptr_type_node, current_class_decl, 0,
11113                                    virtual_size);
11114       else
11115         exprstmt = NULL_TREE;
11116
11117       if (exprstmt)
11118         {
11119           cond = build (BIT_AND_EXPR, integer_type_node,
11120                         in_charge_node, integer_one_node);
11121           expand_start_cond (cond, 0);
11122           expand_expr_stmt (exprstmt);
11123           expand_end_cond ();
11124         }
11125
11126       /* End of destructor.  */
11127       expand_end_bindings (NULL_TREE, getdecls() != NULL_TREE, 0);
11128       poplevel (2, 0, 0); /* XXX change to 1 */
11129
11130       /* Back to the top of destructor.  */
11131       /* Dont execute destructor code if `this' is NULL.  */
11132       mark = get_last_insn ();
11133       last_parm_insn = get_first_nonparm_insn ();
11134       if (last_parm_insn == NULL_RTX)
11135         last_parm_insn = mark;
11136       else
11137         last_parm_insn = previous_insn (last_parm_insn);
11138
11139       /* Make all virtual function table pointers in non-virtual base
11140          classes point to CURRENT_CLASS_TYPE's virtual function
11141          tables.  */
11142       init_vtbl_ptrs (binfo, 1, 0);
11143       if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
11144         expand_vbase_vtables_init (binfo, binfo, C_C_D, current_class_decl, 0);
11145       if (! ok_to_optimize_dtor)
11146         {
11147           cond = build_binary_op (NE_EXPR,
11148                                   current_class_decl, integer_zero_node, 1);
11149           expand_start_cond (cond, 0);
11150         }
11151       if (mark != get_last_insn ())
11152         reorder_insns (next_insn (mark), get_last_insn (), last_parm_insn);
11153       if (! ok_to_optimize_dtor)
11154         expand_end_cond ();
11155     }
11156   else if (current_function_assigns_this)
11157     {
11158       /* Does not need to call emit_base_init, because
11159          that is done (if needed) just after assignment to this
11160          is seen.  */
11161
11162       if (DECL_CONSTRUCTOR_P (current_function_decl))
11163         {
11164           expand_label (ctor_label);
11165           ctor_label = NULL_TREE;
11166
11167           if (call_poplevel)
11168             {
11169               decls = getdecls ();
11170               if (flag_handle_exceptions == 2)
11171                 deactivate_exception_cleanups ();
11172               expand_end_bindings (decls, decls != NULL_TREE, 0);
11173               poplevel (decls != NULL_TREE, 0, 0);
11174             }
11175           c_expand_return (current_class_decl);
11176         }
11177       else if (TYPE_MAIN_VARIANT (TREE_TYPE (
11178                         DECL_RESULT (current_function_decl))) != void_type_node
11179                && return_label != NULL_RTX)
11180         no_return_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
11181
11182       current_function_assigns_this = 0;
11183       current_function_just_assigned_this = 0;
11184       base_init_insns = NULL_RTX;
11185     }
11186   else if (DECL_CONSTRUCTOR_P (fndecl))
11187     {
11188       tree allocated_this;
11189       tree cond, thenclause;
11190       /* Allow constructor for a type to get a new instance of the object
11191          using `build_new'.  */
11192       tree abstract_virtuals = CLASSTYPE_ABSTRACT_VIRTUALS (current_class_type);
11193       CLASSTYPE_ABSTRACT_VIRTUALS (current_class_type) = NULL_TREE;
11194
11195       DECL_RETURNS_FIRST_ARG (fndecl) = 1;
11196
11197       if (flag_this_is_variable > 0)
11198         {
11199           cond = build_binary_op (EQ_EXPR,
11200                                   current_class_decl, integer_zero_node, 1);
11201           thenclause = build_modify_expr (current_class_decl, NOP_EXPR,
11202                                           build_new (NULL_TREE, current_class_type, void_type_node, 0));
11203           if (flag_handle_exceptions == 2)
11204             {
11205               tree cleanup, cleanup_deallocate;
11206               tree virtual_size;
11207
11208               /* This is the size of the virtual object pointed to by
11209                  allocated_this.  In this case, it is simple.  */
11210               virtual_size = c_sizeof (current_class_type);
11211
11212               allocated_this = build_decl (VAR_DECL, NULL_TREE, ptr_type_node);
11213               DECL_REGISTER (allocated_this) = 1;
11214               DECL_INITIAL (allocated_this) = error_mark_node;
11215               expand_decl (allocated_this);
11216               expand_decl_init (allocated_this);
11217               /* How we cleanup `this' if an exception was raised before
11218                  we are ready to bail out.  */
11219               cleanup = TREE_GETS_DELETE (current_class_type)
11220                 ? build_opfncall (DELETE_EXPR, LOOKUP_NORMAL, allocated_this, virtual_size, NULL_TREE)
11221                   /* The size of allocated_this is wrong, and hence the
11222                      second argument to operator delete will be wrong. */
11223                   : build_delete (TREE_TYPE (allocated_this), allocated_this,
11224                                   integer_three_node,
11225                                   LOOKUP_NORMAL|LOOKUP_HAS_IN_CHARGE, 1);
11226               cleanup_deallocate
11227                 = build_modify_expr (current_class_decl, NOP_EXPR, integer_zero_node);
11228               cleanup = tree_cons (NULL_TREE, cleanup,
11229                                    build_tree_list (NULL_TREE, cleanup_deallocate));
11230
11231               expand_decl_cleanup (allocated_this,
11232                                    build (COND_EXPR, integer_type_node,
11233                                           build (NE_EXPR, integer_type_node,
11234                                                  allocated_this, integer_zero_node),
11235                                           build_compound_expr (cleanup),
11236                                           integer_zero_node));
11237             }
11238         }
11239
11240       CLASSTYPE_ABSTRACT_VIRTUALS (current_class_type) = abstract_virtuals;
11241
11242       /* must keep the first insn safe.  */
11243       head = get_insns ();
11244
11245       /* this note will come up to the top with us.  */
11246       mark = get_last_insn ();
11247
11248       if (flag_this_is_variable > 0)
11249         {
11250           expand_start_cond (cond, 0);
11251           expand_expr_stmt (thenclause);
11252           if (flag_handle_exceptions == 2)
11253             expand_assignment (allocated_this, current_class_decl, 0, 0);
11254           expand_end_cond ();
11255         }
11256
11257 #if 0
11258       if (DECL_NAME (fndecl) == NULL_TREE
11259           && TREE_CHAIN (DECL_ARGUMENTS (fndecl)) != NULL_TREE)
11260         build_default_constructor (fndecl);
11261 #endif
11262
11263       /* Emit insns from `emit_base_init' which sets up virtual
11264          function table pointer(s).  */
11265       emit_insns (base_init_insns);
11266       base_init_insns = NULL_RTX;
11267
11268       /* This is where the body of the constructor begins.
11269          If there were no insns in this function body, then the
11270          last_parm_insn is also the last insn.
11271
11272          If optimization is enabled, last_parm_insn may move, so
11273          we don't hold on to it (across emit_base_init).  */
11274       last_parm_insn = get_first_nonparm_insn ();
11275       if (last_parm_insn == NULL_RTX)
11276         last_parm_insn = mark;
11277       else
11278         last_parm_insn = previous_insn (last_parm_insn);
11279
11280       if (mark != get_last_insn ())
11281         reorder_insns (next_insn (mark), get_last_insn (), last_parm_insn);
11282
11283       /* This is where the body of the constructor ends.  */
11284       expand_label (ctor_label);
11285       ctor_label = NULL_TREE;
11286       if (flag_handle_exceptions == 2)
11287         {
11288           expand_assignment (allocated_this, integer_zero_node, 0, 0);
11289           if (call_poplevel)
11290             deactivate_exception_cleanups ();
11291         }
11292
11293       pop_implicit_try_blocks (NULL_TREE);
11294
11295       if (call_poplevel)
11296         {
11297           expand_end_bindings (decls = getdecls (), decls != NULL_TREE, 0);
11298           poplevel (decls != NULL_TREE, 1, 0);
11299         }
11300
11301       c_expand_return (current_class_decl);
11302
11303       current_function_assigns_this = 0;
11304       current_function_just_assigned_this = 0;
11305     }
11306   else if (IDENTIFIER_LENGTH (DECL_NAME (fndecl)) == 4
11307            && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (fndecl)), "main")
11308            && DECL_CONTEXT (fndecl) == NULL_TREE)
11309     {
11310       /* Make it so that `main' always returns 0 by default.  */
11311 #ifdef VMS
11312       c_expand_return (integer_one_node);
11313 #else
11314       c_expand_return (integer_zero_node);
11315 #endif
11316     }
11317   else if (return_label != NULL_RTX
11318            && current_function_return_value == NULL_TREE
11319            && ! DECL_NAME (DECL_RESULT (current_function_decl)))
11320     no_return_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
11321
11322   if (flag_gc)
11323     expand_gc_prologue_and_epilogue ();
11324
11325   /* That's the end of the vtable decl's life.  Need to mark it such
11326      if doing stupid register allocation.
11327
11328      Note that current_vtable_decl is really an INDIRECT_REF
11329      on top of a VAR_DECL here.  */
11330   if (obey_regdecls && current_vtable_decl)
11331     use_variable (DECL_RTL (TREE_OPERAND (current_vtable_decl, 0)));
11332
11333   /* If this function is supposed to return a value, ensure that
11334      we do not fall into the cleanups by mistake.  The end of our
11335      function will look like this:
11336
11337         user code (may have return stmt somewhere)
11338         goto no_return_label
11339         cleanup_label:
11340         cleanups
11341         goto return_label
11342         no_return_label:
11343         NOTE_INSN_FUNCTION_END
11344         return_label:
11345         things for return
11346
11347      If the user omits a return stmt in the USER CODE section, we
11348      will have a control path which reaches NOTE_INSN_FUNCTION_END.
11349      Otherwise, we won't.  */
11350   if (no_return_label)
11351     {
11352       DECL_CONTEXT (no_return_label) = fndecl;
11353       DECL_INITIAL (no_return_label) = error_mark_node;
11354       DECL_SOURCE_FILE (no_return_label) = input_filename;
11355       DECL_SOURCE_LINE (no_return_label) = lineno;
11356       expand_goto (no_return_label);
11357     }
11358
11359   if (cleanup_label)
11360     {
11361       /* remove the binding contour which is used
11362          to catch cleanup-generated temporaries.  */
11363       expand_end_bindings (0, 0, 0);
11364       poplevel (0, 0, 0);
11365     }
11366
11367   if (cleanup_label)
11368     /* Emit label at beginning of cleanup code for parameters.  */
11369     emit_label (cleanup_label);
11370
11371 #if 1
11372   /* Cheap hack to get better code from GNU C++.  Remove when cse is fixed.  */
11373   if (exception_throw_decl && sets_exception_throw_decl == 0)
11374     expand_assignment (exception_throw_decl, integer_zero_node, 0, 0);
11375 #endif
11376
11377   if (flag_handle_exceptions)
11378     {
11379       expand_end_try ();
11380       expand_start_except (0, 0);
11381       expand_end_except ();
11382     }
11383
11384   /* Get return value into register if that's where it's supposed to be.  */
11385   if (original_result_rtx)
11386     fixup_result_decl (DECL_RESULT (fndecl), original_result_rtx);
11387
11388   /* Finish building code that will trigger warnings if users forget
11389      to make their functions return values.  */
11390   if (no_return_label || cleanup_label)
11391     emit_jump (return_label);
11392   if (no_return_label)
11393     {
11394       /* We don't need to call `expand_*_return' here because we
11395          don't need any cleanups here--this path of code is only
11396          for error checking purposes.  */
11397       expand_label (no_return_label);
11398     }
11399
11400   /* reset scope for C++: if we were in the scope of a class,
11401      then when we finish this function, we are not longer so.
11402      This cannot be done until we know for sure that no more
11403      class members will ever be referenced in this function
11404      (i.e., calls to destructors).  */
11405   if (current_class_name)
11406     {
11407       ctype = current_class_type;
11408       pop_nested_class (1);
11409     }
11410   else
11411     pop_memoized_context (1);
11412
11413   /* Forget about all overloaded functions defined in
11414      this scope which go away.  */
11415   while (overloads_to_forget)
11416     {
11417       IDENTIFIER_GLOBAL_VALUE (TREE_PURPOSE (overloads_to_forget))
11418         = TREE_VALUE (overloads_to_forget);
11419       overloads_to_forget = TREE_CHAIN (overloads_to_forget);
11420     }
11421
11422   /* Generate rtl for function exit.  */
11423   expand_function_end (input_filename, lineno, 1);
11424
11425   /* This must come after expand_function_end because cleanups might
11426      have declarations (from inline functions) that need to go into
11427      this function's blocks.  */
11428   if (current_binding_level->parm_flag != 1)
11429     my_friendly_abort (122);
11430   poplevel (1, 0, 1);
11431
11432   /* Must mark the RESULT_DECL as being in this function.  */
11433   DECL_CONTEXT (DECL_RESULT (fndecl)) = DECL_INITIAL (fndecl);
11434
11435   /* Obey `register' declarations if `setjmp' is called in this fn.  */
11436   if (flag_traditional && current_function_calls_setjmp)
11437     setjmp_protect (DECL_INITIAL (fndecl));
11438
11439   /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
11440      to the FUNCTION_DECL node itself.  */
11441   BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
11442
11443   /* So we can tell if jump_optimize sets it to 1.  */
11444   can_reach_end = 0;
11445
11446   /* ??? Compensate for Sun brain damage in dealing with data segments
11447      of PIC code.  */
11448   if (flag_pic
11449       && (DECL_CONSTRUCTOR_P (fndecl)
11450           || DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
11451       && CLASSTYPE_NEEDS_VIRTUAL_REINIT (TYPE_METHOD_BASETYPE (fntype)))
11452     DECL_INLINE (fndecl) = 0;
11453
11454   if (DECL_EXTERNAL (fndecl)
11455       /* This function is just along for the ride.  If we can make
11456          it inline, that's great.  Otherwise, just punt it.  */
11457       && (DECL_INLINE (fndecl) == 0
11458           || flag_no_inline
11459           || function_cannot_inline_p (fndecl)))
11460     {
11461       extern int rtl_dump_and_exit;
11462       int old_rtl_dump_and_exit = rtl_dump_and_exit;
11463       int inline_spec = DECL_INLINE (fndecl);
11464
11465       /* This throws away the code for FNDECL.  */
11466       rtl_dump_and_exit = 1;
11467       /* This throws away the memory of the code for FNDECL.  */
11468       if (flag_no_inline)
11469         DECL_INLINE (fndecl) = 0;
11470       rest_of_compilation (fndecl);
11471       rtl_dump_and_exit = old_rtl_dump_and_exit;
11472       DECL_INLINE (fndecl) = inline_spec;
11473     }
11474   else
11475     {
11476       /* Run the optimizers and output the assembler code for this
11477          function.  */
11478       rest_of_compilation (fndecl);
11479     }
11480
11481   if (DECL_INLINE (fndecl)
11482       && !TREE_ASM_WRITTEN (fndecl) && DECL_FUNCTION_MEMBER_P (fndecl))
11483     {
11484       mark_inline_for_output (fndecl);
11485     }
11486
11487   if (ctype && TREE_ASM_WRITTEN (fndecl))
11488     note_debug_info_needed (ctype);
11489
11490   current_function_returns_null |= can_reach_end;
11491
11492   /* Since we don't normally go through c_expand_return for constructors,
11493      this normally gets the wrong value.
11494      Also, named return values have their return codes emitted after
11495      NOTE_INSN_FUNCTION_END, confusing jump.c.  */
11496   if (DECL_CONSTRUCTOR_P (fndecl)
11497       || DECL_NAME (DECL_RESULT (fndecl)) != NULL_TREE)
11498     current_function_returns_null = 0;
11499
11500   if (TREE_THIS_VOLATILE (fndecl) && current_function_returns_null)
11501     cp_warning ("`noreturn' function `%D' does return", fndecl);
11502   else if ((warn_return_type || pedantic)
11503            && current_function_returns_null
11504            && TYPE_MAIN_VARIANT (TREE_TYPE (fntype)) != void_type_node)
11505     {
11506       /* If this function returns non-void and control can drop through,
11507          complain.  */
11508       cp_pedwarn ("control reaches end of non-void function `%D'", fndecl);
11509     }
11510   /* With just -W, complain only if function returns both with
11511      and without a value.  */
11512   else if (extra_warnings
11513            && current_function_returns_value && current_function_returns_null)
11514     warning ("this function may return with or without a value");
11515
11516   /* Free all the tree nodes making up this function.  */
11517   /* Switch back to allocating nodes permanently
11518      until we start another function.  */
11519   permanent_allocation (1);
11520
11521   if (flag_cadillac)
11522     cadillac_finish_function (fndecl);
11523
11524   if (DECL_SAVED_INSNS (fndecl) == NULL_RTX)
11525     {
11526       /* Stop pointing to the local nodes about to be freed.  */
11527       /* But DECL_INITIAL must remain nonzero so we know this
11528          was an actual function definition.  */
11529       DECL_INITIAL (fndecl) = error_mark_node;
11530       if (! DECL_CONSTRUCTOR_P (fndecl)
11531           || !TYPE_USES_VIRTUAL_BASECLASSES (TYPE_METHOD_BASETYPE (fntype)))
11532         DECL_ARGUMENTS (fndecl) = NULL_TREE;
11533     }
11534
11535   /* Let the error reporting routines know that we're outside a function.  */
11536   current_function_decl = NULL_TREE;
11537   named_label_uses = NULL_TREE;
11538 }
11539 \f
11540 /* Create the FUNCTION_DECL for a function definition.
11541    LINE1 is the line number that the definition absolutely begins on.
11542    LINE2 is the line number that the name of the function appears on.
11543    DECLSPECS and DECLARATOR are the parts of the declaration;
11544    they describe the return type and the name of the function,
11545    but twisted together in a fashion that parallels the syntax of C.
11546
11547    This function creates a binding context for the function body
11548    as well as setting up the FUNCTION_DECL in current_function_decl.
11549
11550    Returns a FUNCTION_DECL on success.
11551
11552    If the DECLARATOR is not suitable for a function (it defines a datum
11553    instead), we return 0, which tells yyparse to report a parse error.
11554
11555    May return void_type_node indicating that this method is actually
11556    a friend.  See grokfield for more details.
11557
11558    Came here with a `.pushlevel' .
11559
11560    DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
11561    CHANGES TO CODE IN `grokfield'.  */
11562 tree
11563 start_method (declspecs, declarator, raises)
11564      tree declarator, declspecs, raises;
11565 {
11566   tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0, raises);
11567
11568   /* Something too ugly to handle.  */
11569   if (fndecl == NULL_TREE)
11570     return NULL_TREE;
11571
11572   /* Pass friends other than inline friend functions back.  */
11573   if (TYPE_MAIN_VARIANT (fndecl) == void_type_node)
11574     return fndecl;
11575
11576   if (TREE_CODE (fndecl) != FUNCTION_DECL)
11577     /* Not a function, tell parser to report parse error.  */
11578     return NULL_TREE;
11579
11580   if (IS_SIGNATURE (current_class_type))
11581     {
11582       IS_DEFAULT_IMPLEMENTATION (fndecl) = 1;
11583       /* In case we need this info later.  */
11584       HAS_DEFAULT_IMPLEMENTATION (current_class_type) = 1;
11585     }
11586
11587   if (DECL_IN_AGGR_P (fndecl))
11588     {
11589       if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (fndecl)) != current_class_type)
11590         {
11591           if (DECL_CONTEXT (fndecl))
11592             cp_error ("`%D' is already defined in class %s", fndecl,
11593                              TYPE_NAME_STRING (DECL_CONTEXT (fndecl)));
11594         }
11595       return void_type_node;
11596     }
11597
11598   if (flag_default_inline)
11599     DECL_INLINE (fndecl) = 1;
11600
11601   /* We read in the parameters on the maybepermanent_obstack,
11602      but we won't be getting back to them until after we
11603      may have clobbered them.  So the call to preserve_data
11604      will keep them safe.  */
11605   preserve_data ();
11606
11607   if (! DECL_FRIEND_P (fndecl))
11608     {
11609       if (DECL_CHAIN (fndecl) != NULL_TREE)
11610         {
11611           /* Need a fresh node here so that we don't get circularity
11612              when we link these together.  If FNDECL was a friend, then
11613              `pushdecl' does the right thing, which is nothing wrt its
11614              current value of DECL_CHAIN.  */
11615           fndecl = copy_node (fndecl);
11616         }
11617       if (TREE_CHAIN (fndecl))
11618         {
11619           fndecl = copy_node (fndecl);
11620           TREE_CHAIN (fndecl) = NULL_TREE;
11621         }
11622
11623       if (DECL_CONSTRUCTOR_P (fndecl))
11624         grok_ctor_properties (current_class_type, fndecl);
11625       else if (IDENTIFIER_OPNAME_P (DECL_NAME (fndecl)))
11626         grok_op_properties (fndecl, DECL_VIRTUAL_P (fndecl), 0);
11627     }
11628
11629   finish_decl (fndecl, NULL_TREE, NULL_TREE, 0);
11630
11631   /* Make a place for the parms */
11632   pushlevel (0);
11633   current_binding_level->parm_flag = 1;
11634   
11635   DECL_IN_AGGR_P (fndecl) = 1;
11636   return fndecl;
11637 }
11638
11639 /* Go through the motions of finishing a function definition.
11640    We don't compile this method until after the whole class has
11641    been processed.
11642
11643    FINISH_METHOD must return something that looks as though it
11644    came from GROKFIELD (since we are defining a method, after all).
11645
11646    This is called after parsing the body of the function definition.
11647    STMTS is the chain of statements that makes up the function body.
11648
11649    DECL is the ..._DECL that `start_method' provided.  */
11650
11651 tree
11652 finish_method (decl)
11653      tree decl;
11654 {
11655   register tree fndecl = decl;
11656   tree old_initial;
11657
11658   register tree link;
11659
11660   if (TYPE_MAIN_VARIANT (decl) == void_type_node)
11661     return decl;
11662
11663   old_initial = DECL_INITIAL (fndecl);
11664
11665   /* Undo the level for the parms (from start_method).
11666      This is like poplevel, but it causes nothing to be
11667      saved.  Saving information here confuses symbol-table
11668      output routines.  Besides, this information will
11669      be correctly output when this method is actually
11670      compiled.  */
11671
11672   /* Clear out the meanings of the local variables of this level;
11673      also record in each decl which block it belongs to.  */
11674
11675   for (link = current_binding_level->names; link; link = TREE_CHAIN (link))
11676     {
11677       if (DECL_NAME (link) != NULL_TREE)
11678         IDENTIFIER_LOCAL_VALUE (DECL_NAME (link)) = 0;
11679       my_friendly_assert (TREE_CODE (link) != FUNCTION_DECL, 163);
11680       DECL_CONTEXT (link) = NULL_TREE;
11681     }
11682
11683   /* Restore all name-meanings of the outer levels
11684      that were shadowed by this level.  */
11685
11686   for (link = current_binding_level->shadowed; link; link = TREE_CHAIN (link))
11687       IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
11688   for (link = current_binding_level->class_shadowed;
11689        link; link = TREE_CHAIN (link))
11690     IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
11691   for (link = current_binding_level->type_shadowed;
11692        link; link = TREE_CHAIN (link))
11693     IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
11694
11695   GNU_xref_end_scope ((HOST_WIDE_INT) current_binding_level,
11696                       (HOST_WIDE_INT) current_binding_level->level_chain,
11697                       current_binding_level->parm_flag,
11698                       current_binding_level->keep,
11699                       current_binding_level->tag_transparent);
11700
11701   poplevel (0, 0, 0);
11702
11703   DECL_INITIAL (fndecl) = old_initial;
11704
11705   /* We used to check if the context of FNDECL was different from
11706      current_class_type as another way to get inside here.  This didn't work
11707      for String.cc in libg++.  */
11708   if (DECL_FRIEND_P (fndecl))
11709     {
11710       CLASSTYPE_INLINE_FRIENDS (current_class_type)
11711         = tree_cons (NULL_TREE, fndecl, CLASSTYPE_INLINE_FRIENDS (current_class_type));
11712       decl = void_type_node;
11713     }
11714
11715   return decl;
11716 }
11717 \f
11718 /* Called when a new struct TYPE is defined.
11719    If this structure or union completes the type of any previous
11720    variable declaration, lay it out and output its rtl.  */
11721
11722 void
11723 hack_incomplete_structures (type)
11724      tree type;
11725 {
11726   tree decl;
11727
11728   if (current_binding_level->n_incomplete == 0)
11729     return;
11730
11731   if (!type) /* Don't do this for class templates.  */
11732     return;
11733
11734   for (decl = current_binding_level->names; decl; decl = TREE_CHAIN (decl))
11735     if (TREE_TYPE (decl) == type
11736         || (TREE_TYPE (decl)
11737             && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
11738             && TREE_TYPE (TREE_TYPE (decl)) == type))
11739       {
11740         if (TREE_CODE (decl) == TYPE_DECL)
11741           layout_type (TREE_TYPE (decl));
11742         else
11743           {
11744             int toplevel = global_binding_level == current_binding_level;
11745             if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
11746                 && TREE_TYPE (TREE_TYPE (decl)) == type)
11747               layout_type (TREE_TYPE (decl));
11748             layout_decl (decl, 0);
11749             rest_of_decl_compilation (decl, NULL_PTR, toplevel, 0);
11750             if (! toplevel)
11751               {
11752                 expand_decl (decl);
11753                 expand_decl_cleanup (decl, maybe_build_cleanup (decl));
11754                 expand_decl_init (decl);
11755               }
11756           }
11757         my_friendly_assert (current_binding_level->n_incomplete > 0, 164);
11758         --current_binding_level->n_incomplete;
11759       }
11760 }
11761
11762 /* Nonzero if presently building a cleanup.  Needed because
11763    SAVE_EXPRs are not the right things to use inside of cleanups.
11764    They are only ever evaluated once, where the cleanup
11765    might be evaluated several times.  In this case, a later evaluation
11766    of the cleanup might fill in the SAVE_EXPR_RTL, and it will
11767    not be valid for an earlier cleanup.  */
11768
11769 int building_cleanup;
11770
11771 /* If DECL is of a type which needs a cleanup, build that cleanup here.
11772    We don't build cleanups if just going for syntax checking, since
11773    fixup_cleanups does not know how to not handle them.
11774
11775    Don't build these on the momentary obstack; they must live
11776    the life of the binding contour.  */
11777 tree
11778 maybe_build_cleanup (decl)
11779      tree decl;
11780 {
11781   tree type = TREE_TYPE (decl);
11782   if (TYPE_NEEDS_DESTRUCTOR (type))
11783     {
11784       int temp = 0, flags = LOOKUP_NORMAL|LOOKUP_DESTRUCTOR;
11785       tree rval;
11786       int old_building_cleanup = building_cleanup;
11787       building_cleanup = 1;
11788
11789       if (TREE_CODE (decl) != PARM_DECL)
11790         temp = suspend_momentary ();
11791
11792       if (TREE_CODE (type) == ARRAY_TYPE)
11793         rval = decl;
11794       else
11795         {
11796           mark_addressable (decl);
11797           rval = build_unary_op (ADDR_EXPR, decl, 0);
11798         }
11799
11800       /* Optimize for space over speed here.  */
11801       if (! TYPE_USES_VIRTUAL_BASECLASSES (type)
11802           || flag_expensive_optimizations)
11803         flags |= LOOKUP_NONVIRTUAL;
11804
11805       /* Use TYPE_MAIN_VARIANT so we don't get a warning about
11806          calling delete on a `const' variable.  */
11807       if (TYPE_READONLY (TREE_TYPE (TREE_TYPE (rval))))
11808         rval = build1 (NOP_EXPR, TYPE_POINTER_TO (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (rval)))), rval);
11809
11810       rval = build_delete (TREE_TYPE (rval), rval, integer_two_node, flags, 0);
11811
11812       if (TYPE_USES_VIRTUAL_BASECLASSES (type)
11813           && ! TYPE_HAS_DESTRUCTOR (type))
11814         rval = build_compound_expr (tree_cons (NULL_TREE, rval,
11815                                                build_tree_list (NULL_TREE, build_vbase_delete (type, decl))));
11816
11817       current_binding_level->have_cleanups = 1;
11818       current_binding_level->more_exceptions_ok = 0;
11819
11820       if (TREE_CODE (decl) != PARM_DECL)
11821         resume_momentary (temp);
11822
11823       building_cleanup = old_building_cleanup;
11824
11825       return rval;
11826     }
11827   return 0;
11828 }
11829 \f
11830 /* Expand a C++ expression at the statement level.
11831    This is needed to ferret out nodes which have UNKNOWN_TYPE.
11832    The C++ type checker should get all of these out when
11833    expressions are combined with other, type-providing, expressions,
11834    leaving only orphan expressions, such as:
11835
11836    &class::bar;         / / takes its address, but does nothing with it.
11837
11838    */
11839 void
11840 cplus_expand_expr_stmt (exp)
11841      tree exp;
11842 {
11843   if (TREE_TYPE (exp) == unknown_type_node)
11844     {
11845       if (TREE_CODE (exp) == ADDR_EXPR || TREE_CODE (exp) == TREE_LIST)
11846         error ("address of overloaded function with no contextual type information");
11847       else if (TREE_CODE (exp) == COMPONENT_REF)
11848         warning ("useless reference to a member function name, did you forget the ()?");
11849     }
11850   else
11851     {
11852       int remove_implicit_immediately = 0;
11853
11854       if (TREE_CODE (exp) == FUNCTION_DECL)
11855         {
11856           cp_warning ("reference, not call, to function `%D'", exp);
11857           warning ("at this point in file");
11858         }
11859       if (TREE_RAISES (exp))
11860         {
11861           my_friendly_assert (flag_handle_exceptions, 165);
11862           if (flag_handle_exceptions == 2)
11863             {
11864               if (! current_binding_level->more_exceptions_ok)
11865                 {
11866                   extern struct nesting *nesting_stack, *block_stack;
11867
11868                   remove_implicit_immediately
11869                     = (nesting_stack != block_stack);
11870                   cplus_expand_start_try (1);
11871                 }
11872               current_binding_level->have_exceptions = 1;
11873             }
11874         }
11875
11876       expand_expr_stmt (break_out_cleanups (exp));
11877
11878       if (remove_implicit_immediately)
11879         pop_implicit_try_blocks (NULL_TREE);
11880     }
11881
11882   /* Clean up any pending cleanups.  This happens when a function call
11883      returns a cleanup-needing value that nobody uses.  */
11884   expand_cleanups_to (NULL_TREE);
11885 }
11886
11887 /* When a stmt has been parsed, this function is called.
11888
11889    Currently, this function only does something within a
11890    constructor's scope: if a stmt has just assigned to this,
11891    and we are in a derived class, we call `emit_base_init'.  */
11892
11893 void
11894 finish_stmt ()
11895 {
11896   extern struct nesting *cond_stack, *loop_stack, *case_stack;
11897
11898   
11899   if (current_function_assigns_this
11900       || ! current_function_just_assigned_this)
11901     return;
11902   if (DECL_CONSTRUCTOR_P (current_function_decl))
11903     {
11904       /* Constructors must wait until we are out of control
11905          zones before calling base constructors.  */
11906       if (cond_stack || loop_stack || case_stack)
11907         return;
11908       emit_insns (base_init_insns);
11909       check_base_init (current_class_type);
11910     }
11911   current_function_assigns_this = 1;
11912
11913   if (flag_cadillac)
11914     cadillac_finish_stmt ();
11915 }
11916
11917 void
11918 pop_implicit_try_blocks (decl)
11919      tree decl;
11920 {
11921   if (decl)
11922     {
11923       my_friendly_assert (current_binding_level->parm_flag == 3, 166);
11924       current_binding_level->names = TREE_CHAIN (decl);
11925     }
11926
11927   while (current_binding_level->parm_flag == 3)
11928     {
11929       tree name = get_identifier ("(compiler error)");
11930       tree orig_ex_type = current_exception_type;
11931       tree orig_ex_decl = current_exception_decl;
11932       tree orig_ex_obj = current_exception_object;
11933       tree decl = cplus_expand_end_try (2);
11934
11935       /* @@ It would be nice to make all these point
11936          to exactly the same handler.  */
11937       /* Start hidden EXCEPT.  */
11938       cplus_expand_start_except (name, decl);
11939       /* reraise ALL.  */
11940       cplus_expand_reraise (NULL_TREE);
11941       current_exception_type = orig_ex_type;
11942       current_exception_decl = orig_ex_decl;
11943       current_exception_object = orig_ex_obj;
11944       /* This will reraise for us.  */
11945       cplus_expand_end_except (error_mark_node);
11946     }
11947
11948   if (decl)
11949     {
11950       TREE_CHAIN (decl) = current_binding_level->names;
11951       current_binding_level->names = decl;
11952     }
11953 }
11954
11955 /* Push a cleanup onto the current binding contour that will cause
11956    ADDR to be cleaned up, in the case that an exception propagates
11957    through its binding contour.  */
11958
11959 void
11960 push_exception_cleanup (addr)
11961      tree addr;
11962 {
11963   tree decl = build_decl (VAR_DECL, get_identifier (EXCEPTION_CLEANUP_NAME), ptr_type_node);
11964   tree cleanup;
11965
11966   decl = pushdecl (decl);
11967   DECL_REGISTER (decl) = 1;
11968   store_init_value (decl, addr);
11969   expand_decl (decl);
11970   expand_decl_init (decl);
11971
11972   cleanup = build (COND_EXPR, integer_type_node,
11973                    build (NE_EXPR, integer_type_node,
11974                           decl, integer_zero_node),
11975                    build_delete (TREE_TYPE (addr), decl,
11976                                  lookup_name (in_charge_identifier, 0),
11977                                  LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0),
11978                    integer_zero_node);
11979   expand_decl_cleanup (decl, cleanup);
11980 }
11981
11982 /* For each binding contour, emit code that deactivates the
11983    exception cleanups.  All other cleanups are left as they were.  */
11984
11985 static void
11986 deactivate_exception_cleanups ()
11987 {
11988   struct binding_level *b = current_binding_level;
11989   tree xyzzy = get_identifier (EXCEPTION_CLEANUP_NAME);
11990   while (b != class_binding_level)
11991     {
11992       if (b->parm_flag == 3)
11993         {
11994           tree decls = b->names;
11995           while (decls)
11996             {
11997               if (DECL_NAME (decls) == xyzzy)
11998                 expand_assignment (decls, integer_zero_node, 0, 0);
11999               decls = TREE_CHAIN (decls);
12000             }
12001         }
12002       b = b->level_chain;
12003     }
12004 }
12005
12006 /* Change a static member function definition into a FUNCTION_TYPE, instead
12007    of the METHOD_TYPE that we create when it's originally parsed.  */
12008 void
12009 revert_static_member_fn (fn, decl, argtypes)
12010      tree *fn, *decl, *argtypes;
12011 {
12012   tree tmp, function = *fn;
12013
12014   *argtypes = TREE_CHAIN (*argtypes);
12015   tmp = build_function_type (TREE_TYPE (function), *argtypes);
12016   tmp = build_type_variant (tmp, TYPE_READONLY (function),
12017                             TYPE_VOLATILE (function));
12018   tmp = build_exception_variant (TYPE_METHOD_BASETYPE (function), tmp,
12019                                  TYPE_RAISES_EXCEPTIONS (function));
12020   TREE_TYPE (*decl) = tmp;
12021   *fn = tmp;
12022   DECL_STATIC_FUNCTION_P (*decl) = 1;
12023 }