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