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