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