]> gcc.gnu.org Git - gcc.git/blame - gcc/c-decl.c
(execute): Don't wait for just any N subprocesses...
[gcc.git] / gcc / c-decl.c
CommitLineData
51e29401 1/* Process declarations and variables for C compiler.
11433f42 2 Copyright (C) 1988, 1992, 1993, 1994 Free Software Foundation, Inc.
51e29401
RS
3
4This file is part of GNU CC.
5
6GNU CC is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU CC is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU CC; see the file COPYING. If not, write to
18the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20
21/* Process declarations and symbol lookup for C front end.
22 Also constructs types; the standard scalar types at initialization,
23 and structure, union, array and enum types when they are declared. */
24
25/* ??? not all decl nodes are given the most useful possible
26 line numbers. For example, the CONST_DECLs for enum values. */
27
28#include "config.h"
29#include "tree.h"
30#include "flags.h"
e14417fa 31#include "output.h"
51e29401
RS
32#include "c-tree.h"
33#include "c-lex.h"
34#include <stdio.h>
35
36/* In grokdeclarator, distinguish syntactic contexts of declarators. */
37enum decl_context
38{ NORMAL, /* Ordinary declaration */
39 FUNCDEF, /* Function definition */
40 PARM, /* Declaration of parm before function body */
41 FIELD, /* Declaration inside struct or union */
42 BITFIELD, /* Likewise but with specified width */
43 TYPENAME}; /* Typename (inside cast or sizeof) */
44
51e29401
RS
45#ifndef CHAR_TYPE_SIZE
46#define CHAR_TYPE_SIZE BITS_PER_UNIT
47#endif
48
49#ifndef SHORT_TYPE_SIZE
50#define SHORT_TYPE_SIZE (BITS_PER_UNIT * MIN ((UNITS_PER_WORD + 1) / 2, 2))
51#endif
52
53#ifndef INT_TYPE_SIZE
54#define INT_TYPE_SIZE BITS_PER_WORD
55#endif
56
57#ifndef LONG_TYPE_SIZE
58#define LONG_TYPE_SIZE BITS_PER_WORD
59#endif
60
61#ifndef LONG_LONG_TYPE_SIZE
62#define LONG_LONG_TYPE_SIZE (BITS_PER_WORD * 2)
63#endif
64
65#ifndef WCHAR_UNSIGNED
66#define WCHAR_UNSIGNED 0
67#endif
68
69#ifndef FLOAT_TYPE_SIZE
70#define FLOAT_TYPE_SIZE BITS_PER_WORD
71#endif
72
73#ifndef DOUBLE_TYPE_SIZE
74#define DOUBLE_TYPE_SIZE (BITS_PER_WORD * 2)
75#endif
76
77#ifndef LONG_DOUBLE_TYPE_SIZE
78#define LONG_DOUBLE_TYPE_SIZE (BITS_PER_WORD * 2)
79#endif
80
81/* We let tm.h override the types used here, to handle trivial differences
82 such as the choice of unsigned int or long unsigned int for size_t.
83 When machines start needing nontrivial differences in the size type,
84 it would be best to do something here to figure out automatically
85 from other information what type to use. */
86
87#ifndef SIZE_TYPE
88#define SIZE_TYPE "long unsigned int"
89#endif
90
91#ifndef PTRDIFF_TYPE
92#define PTRDIFF_TYPE "long int"
93#endif
94
95#ifndef WCHAR_TYPE
96#define WCHAR_TYPE "int"
97#endif
98\f
99/* a node which has tree code ERROR_MARK, and whose type is itself.
100 All erroneous expressions are replaced with this node. All functions
101 that accept nodes as arguments should avoid generating error messages
102 if this node is one of the arguments, since it is undesirable to get
103 multiple error messages from one error in the input. */
104
105tree error_mark_node;
106
107/* INTEGER_TYPE and REAL_TYPE nodes for the standard data types */
108
109tree short_integer_type_node;
110tree integer_type_node;
111tree long_integer_type_node;
112tree long_long_integer_type_node;
113
114tree short_unsigned_type_node;
115tree unsigned_type_node;
116tree long_unsigned_type_node;
117tree long_long_unsigned_type_node;
118
119tree ptrdiff_type_node;
120
121tree unsigned_char_type_node;
122tree signed_char_type_node;
123tree char_type_node;
124tree wchar_type_node;
125tree signed_wchar_type_node;
126tree unsigned_wchar_type_node;
127
128tree float_type_node;
129tree double_type_node;
130tree long_double_type_node;
131
5ab10c42
RS
132tree complex_integer_type_node;
133tree complex_float_type_node;
134tree complex_double_type_node;
135tree complex_long_double_type_node;
136
ac4f24e7
RS
137tree intQI_type_node;
138tree intHI_type_node;
139tree intSI_type_node;
140tree intDI_type_node;
141
142tree unsigned_intQI_type_node;
143tree unsigned_intHI_type_node;
144tree unsigned_intSI_type_node;
145tree unsigned_intDI_type_node;
146
51e29401
RS
147/* a VOID_TYPE node. */
148
149tree void_type_node;
150
151/* Nodes for types `void *' and `const void *'. */
152
153tree ptr_type_node, const_ptr_type_node;
154
155/* Nodes for types `char *' and `const char *'. */
156
157tree string_type_node, const_string_type_node;
158
fba9adc6 159/* Type `char[SOMENUMBER]'.
51e29401
RS
160 Used when an array of char is needed and the size is irrelevant. */
161
162tree char_array_type_node;
163
fba9adc6 164/* Type `int[SOMENUMBER]' or something like it.
51e29401
RS
165 Used when an array of int needed and the size is irrelevant. */
166
167tree int_array_type_node;
168
fba9adc6 169/* Type `wchar_t[SOMENUMBER]' or something like it.
51e29401
RS
170 Used when a wide string literal is created. */
171
172tree wchar_array_type_node;
173
174/* type `int ()' -- used for implicit declaration of functions. */
175
176tree default_function_type;
177
178/* function types `double (double)' and `double (double, double)', etc. */
179
180tree double_ftype_double, double_ftype_double_double;
181tree int_ftype_int, long_ftype_long;
732149f9
RK
182tree float_ftype_float;
183tree ldouble_ftype_ldouble;
51e29401
RS
184
185/* Function type `void (void *, void *, int)' and similar ones */
186
187tree void_ftype_ptr_ptr_int, int_ftype_ptr_ptr_int, void_ftype_ptr_int_int;
188
189/* Function type `char *(char *, char *)' and similar ones */
190tree string_ftype_ptr_ptr, int_ftype_string_string;
191
51e29401
RS
192/* Function type `int (const void *, const void *, size_t)' */
193tree int_ftype_cptr_cptr_sizet;
194
195/* Two expressions that are constants with value zero.
196 The first is of type `int', the second of type `void *'. */
197
198tree integer_zero_node;
199tree null_pointer_node;
200
201/* A node for the integer constant 1. */
202
203tree integer_one_node;
204
205/* Nonzero if we have seen an invalid cross reference
206 to a struct, union, or enum, but not yet printed the message. */
207
208tree pending_invalid_xref;
209/* File and line to appear in the eventual error message. */
210char *pending_invalid_xref_file;
211int pending_invalid_xref_line;
212
213/* While defining an enum type, this is 1 plus the last enumerator
18192b41
RK
214 constant value. Note that will do not have to save this or `enum_overflow'
215 around nested function definition since such a definition could only
216 occur in an enum value expression and we don't use these variables in
217 that case. */
51e29401
RS
218
219static tree enum_next_value;
220
93e3ba4f
RS
221/* Nonzero means that there was overflow computing enum_next_value. */
222
223static int enum_overflow;
224
51e29401
RS
225/* Parsing a function declarator leaves a list of parameter names
226 or a chain or parameter decls here. */
227
228static tree last_function_parms;
229
230/* Parsing a function declarator leaves here a chain of structure
231 and enum types declared in the parmlist. */
232
233static tree last_function_parm_tags;
234
235/* After parsing the declarator that starts a function definition,
236 `start_function' puts here the list of parameter names or chain of decls.
237 `store_parm_decls' finds it here. */
238
239static tree current_function_parms;
240
241/* Similar, for last_function_parm_tags. */
242static tree current_function_parm_tags;
243
50a9145c
JW
244/* Similar, for the file and line that the prototype came from if this is
245 an old-style definition. */
246static char *current_function_prototype_file;
247static int current_function_prototype_line;
248
51e29401
RS
249/* A list (chain of TREE_LIST nodes) of all LABEL_DECLs in the function
250 that have names. Here so we can clear out their names' definitions
251 at the end of the function. */
252
c9c30903 253static tree named_labels;
51e29401
RS
254
255/* A list of LABEL_DECLs from outer contexts that are currently shadowed. */
256
257static tree shadowed_labels;
258
259/* Nonzero when store_parm_decls is called indicates a varargs function.
260 Value not meaningful after store_parm_decls. */
261
262static int c_function_varargs;
263
264/* The FUNCTION_DECL for the function currently being compiled,
265 or 0 if between functions. */
266tree current_function_decl;
267
268/* Set to 0 at beginning of a function definition, set to 1 if
269 a return statement that specifies a return value is seen. */
270
271int current_function_returns_value;
272
273/* Set to 0 at beginning of a function definition, set to 1 if
274 a return statement with no argument is seen. */
275
276int current_function_returns_null;
277
278/* Set to nonzero by `grokdeclarator' for a function
279 whose return type is defaulted, if warnings for this are desired. */
280
281static int warn_about_return_type;
282
929f3671 283/* Nonzero when starting a function declared `extern inline'. */
51e29401
RS
284
285static int current_extern_inline;
286\f
287/* For each binding contour we allocate a binding_level structure
288 * which records the names defined in that contour.
289 * Contours include:
290 * 0) the global one
291 * 1) one for each function definition,
292 * where internal declarations of the parameters appear.
293 * 2) one for each compound statement,
294 * to record its declarations.
295 *
296 * The current meaning of a name can be found by searching the levels from
297 * the current one out to the global one.
298 */
299
300/* Note that the information in the `names' component of the global contour
301 is duplicated in the IDENTIFIER_GLOBAL_VALUEs of all identifiers. */
302
303struct binding_level
304 {
305 /* A chain of _DECL nodes for all variables, constants, functions,
306 and typedef types. These are in the reverse of the order supplied.
307 */
308 tree names;
309
310 /* A list of structure, union and enum definitions,
311 * for looking up tag names.
312 * It is a chain of TREE_LIST nodes, each of whose TREE_PURPOSE is a name,
313 * or NULL_TREE; and whose TREE_VALUE is a RECORD_TYPE, UNION_TYPE,
314 * or ENUMERAL_TYPE node.
315 */
316 tree tags;
317
318 /* For each level, a list of shadowed outer-level local definitions
319 to be restored when this level is popped.
320 Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
321 whose TREE_VALUE is its old definition (a kind of ..._DECL node). */
322 tree shadowed;
323
324 /* For each level (except not the global one),
325 a chain of BLOCK nodes for all the levels
326 that were entered and exited one level down. */
327 tree blocks;
328
3bf40d18
RS
329 /* The BLOCK node for this level, if one has been preallocated.
330 If 0, the BLOCK is allocated (if needed) when the level is popped. */
331 tree this_block;
332
51e29401
RS
333 /* The binding level which this one is contained in (inherits from). */
334 struct binding_level *level_chain;
335
336 /* Nonzero for the level that holds the parameters of a function. */
51e29401
RS
337 char parm_flag;
338
339 /* Nonzero if this level "doesn't exist" for tags. */
340 char tag_transparent;
341
342 /* Nonzero if sublevels of this level "don't exist" for tags.
343 This is set in the parm level of a function definition
344 while reading the function body, so that the outermost block
345 of the function body will be tag-transparent. */
346 char subblocks_tag_transparent;
347
348 /* Nonzero means make a BLOCK for this level regardless of all else. */
349 char keep;
350
351 /* Nonzero means make a BLOCK if this level has any subblocks. */
352 char keep_if_subblocks;
353
354 /* Number of decls in `names' that have incomplete
355 structure or union types. */
356 int n_incomplete;
357
358 /* A list of decls giving the (reversed) specified order of parms,
359 not including any forward-decls in the parmlist.
360 This is so we can put the parms in proper order for assign_parms. */
361 tree parm_order;
362 };
363
364#define NULL_BINDING_LEVEL (struct binding_level *) NULL
365
366/* The binding level currently in effect. */
367
368static struct binding_level *current_binding_level;
369
370/* A chain of binding_level structures awaiting reuse. */
371
372static struct binding_level *free_binding_level;
373
374/* The outermost binding level, for names of file scope.
375 This is created when the compiler is started and exists
376 through the entire run. */
377
378static struct binding_level *global_binding_level;
379
380/* Binding level structures are initialized by copying this one. */
381
382static struct binding_level clear_binding_level
014aa47e
RK
383 = {NULL, NULL, NULL, NULL, NULL, NULL_BINDING_LEVEL, 0, 0, 0, 0, 0, 0,
384 NULL};
51e29401
RS
385
386/* Nonzero means unconditionally make a BLOCK for the next level pushed. */
387
388static int keep_next_level_flag;
389
390/* Nonzero means make a BLOCK for the next level pushed
391 if it has subblocks. */
392
393static int keep_next_if_subblocks;
394
395/* The chain of outer levels of label scopes.
396 This uses the same data structure used for binding levels,
397 but it works differently: each link in the chain records
398 saved values of named_labels and shadowed_labels for
399 a label binding level outside the current one. */
400
401static struct binding_level *label_level_chain;
402
403/* Forward declarations. */
404
405static tree grokparms (), grokdeclarator ();
406tree pushdecl ();
929f3671 407tree builtin_function ();
9282f2f9 408void shadow_tag_warned ();
51e29401
RS
409
410static tree lookup_tag ();
411static tree lookup_tag_reverse ();
f062da04 412tree lookup_name_current_level ();
51e29401
RS
413static char *redeclaration_error_message ();
414static void layout_array_type ();
415\f
416/* C-specific option variables. */
417
418/* Nonzero means allow type mismatches in conditional expressions;
419 just make their values `void'. */
420
421int flag_cond_mismatch;
422
423/* Nonzero means give `double' the same size as `float'. */
424
425int flag_short_double;
426
427/* Nonzero means don't recognize the keyword `asm'. */
428
429int flag_no_asm;
430
fc3ffe83 431/* Nonzero means don't recognize any builtin functions. */
51e29401
RS
432
433int flag_no_builtin;
434
fc3ffe83
RK
435/* Nonzero means don't recognize the non-ANSI builtin functions.
436 -ansi sets this. */
437
438int flag_no_nonansi_builtin;
439
51e29401
RS
440/* Nonzero means do some things the same way PCC does. */
441
442int flag_traditional;
443
55b4742b
RS
444/* Nonzero means to allow single precision math even if we're generally
445 being traditional. */
446int flag_allow_single_precision = 0;
447
51e29401
RS
448/* Nonzero means to treat bitfields as signed unless they say `unsigned'. */
449
450int flag_signed_bitfields = 1;
7a0347ff 451int explicit_flag_signed_bitfields = 0;
51e29401
RS
452
453/* Nonzero means handle `#ident' directives. 0 means ignore them. */
454
455int flag_no_ident = 0;
456
457/* Nonzero means warn about implicit declarations. */
458
459int warn_implicit;
460
461/* Nonzero means give string constants the type `const char *'
462 to get extra warnings from them. These warnings will be too numerous
463 to be useful, except in thoroughly ANSIfied programs. */
464
465int warn_write_strings;
466
467/* Nonzero means warn about pointer casts that can drop a type qualifier
468 from the pointer target type. */
469
470int warn_cast_qual;
471
5c8902bb
RK
472/* Nonzero means warn when casting a function call to a type that does
473 not match the return type (e.g. (float)sqrt() or (anything*)malloc()
474 when there is no previous declaration of sqrt or malloc. */
475
476int warn_bad_function_cast;
477
51e29401
RS
478/* Warn about traditional constructs whose meanings changed in ANSI C. */
479
480int warn_traditional;
481
482/* Nonzero means warn about sizeof(function) or addition/subtraction
483 of function pointers. */
484
485int warn_pointer_arith;
486
487/* Nonzero means warn for non-prototype function decls
488 or non-prototyped defs without previous prototype. */
489
490int warn_strict_prototypes;
491
492/* Nonzero means warn for any global function def
493 without separate previous prototype decl. */
494
495int warn_missing_prototypes;
496
1474fe46
RK
497/* Nonzero means warn for any global function def
498 without separate previous decl. */
499
500int warn_missing_declarations;
501
51e29401
RS
502/* Nonzero means warn about multiple (redundant) decls for the same single
503 variable or function. */
504
505int warn_redundant_decls = 0;
506
507/* Nonzero means warn about extern declarations of objects not at
508 file-scope level and about *all* declarations of functions (whether
509 extern or static) not at file-scope level. Note that we exclude
510 implicit function declarations. To get warnings about those, use
511 -Wimplicit. */
512
513int warn_nested_externs = 0;
514
515/* Warn about *printf or *scanf format/argument anomalies. */
516
517int warn_format;
518
519/* Warn about a subscript that has type char. */
520
521int warn_char_subscripts = 0;
522
523/* Warn if a type conversion is done that might have confusing results. */
524
525int warn_conversion;
526
527/* Warn if adding () is suggested. */
528
929f3671 529int warn_parentheses;
51e29401 530
36e6fa69
RS
531/* Warn if initializer is not completely bracketed. */
532
533int warn_missing_braces;
534
51e29401
RS
535/* Nonzero means `$' can be in an identifier.
536 See cccp.c for reasons why this breaks some obscure ANSI C programs. */
537
538#ifndef DOLLARS_IN_IDENTIFIERS
539#define DOLLARS_IN_IDENTIFIERS 1
540#endif
541int dollars_in_ident = DOLLARS_IN_IDENTIFIERS > 1;
542
51e29401
RS
543/* Decode the string P as a language-specific option for C.
544 Return 1 if it is recognized (and handle it);
545 return 0 if not recognized. */
546
547int
548c_decode_option (p)
549 char *p;
550{
551 if (!strcmp (p, "-ftraditional") || !strcmp (p, "-traditional"))
552 {
553 flag_traditional = 1;
554 flag_writable_strings = 1;
555#if DOLLARS_IN_IDENTIFIERS > 0
556 dollars_in_ident = 1;
557#endif
558 }
55b4742b
RS
559 else if (!strcmp (p, "-fallow-single-precision"))
560 flag_allow_single_precision = 1;
4ecc65ac
RS
561 else if (!strcmp (p, "-fnotraditional") || !strcmp (p, "-fno-traditional"))
562 {
563 flag_traditional = 0;
564 flag_writable_strings = 0;
565 dollars_in_ident = DOLLARS_IN_IDENTIFIERS > 1;
566 }
a419ff54
RS
567 else if (!strcmp (p, "-fdollars-in-identifiers"))
568 {
569#if DOLLARS_IN_IDENTIFIERS > 0
570 dollars_in_ident = 1;
571#endif
572 }
014aa47e 573 else if (!strcmp (p, "-fno-dollars-in-identifiers"))
a419ff54 574 dollars_in_ident = 0;
51e29401
RS
575 else if (!strcmp (p, "-fsigned-char"))
576 flag_signed_char = 1;
577 else if (!strcmp (p, "-funsigned-char"))
578 flag_signed_char = 0;
579 else if (!strcmp (p, "-fno-signed-char"))
580 flag_signed_char = 0;
581 else if (!strcmp (p, "-fno-unsigned-char"))
582 flag_signed_char = 1;
7a0347ff
RS
583 else if (!strcmp (p, "-fsigned-bitfields")
584 || !strcmp (p, "-fno-unsigned-bitfields"))
585 {
586 flag_signed_bitfields = 1;
587 explicit_flag_signed_bitfields = 1;
588 }
589 else if (!strcmp (p, "-funsigned-bitfields")
590 || !strcmp (p, "-fno-signed-bitfields"))
591 {
592 flag_signed_bitfields = 0;
593 explicit_flag_signed_bitfields = 1;
594 }
51e29401
RS
595 else if (!strcmp (p, "-fshort-enums"))
596 flag_short_enums = 1;
597 else if (!strcmp (p, "-fno-short-enums"))
598 flag_short_enums = 0;
599 else if (!strcmp (p, "-fcond-mismatch"))
600 flag_cond_mismatch = 1;
601 else if (!strcmp (p, "-fno-cond-mismatch"))
602 flag_cond_mismatch = 0;
603 else if (!strcmp (p, "-fshort-double"))
604 flag_short_double = 1;
605 else if (!strcmp (p, "-fno-short-double"))
606 flag_short_double = 0;
607 else if (!strcmp (p, "-fasm"))
608 flag_no_asm = 0;
609 else if (!strcmp (p, "-fno-asm"))
610 flag_no_asm = 1;
611 else if (!strcmp (p, "-fbuiltin"))
612 flag_no_builtin = 0;
613 else if (!strcmp (p, "-fno-builtin"))
614 flag_no_builtin = 1;
615 else if (!strcmp (p, "-fno-ident"))
616 flag_no_ident = 1;
617 else if (!strcmp (p, "-fident"))
618 flag_no_ident = 0;
619 else if (!strcmp (p, "-ansi"))
fc3ffe83 620 flag_no_asm = 1, flag_no_nonansi_builtin = 1, dollars_in_ident = 0;
51e29401
RS
621 else if (!strcmp (p, "-Wimplicit"))
622 warn_implicit = 1;
623 else if (!strcmp (p, "-Wno-implicit"))
624 warn_implicit = 0;
625 else if (!strcmp (p, "-Wwrite-strings"))
626 warn_write_strings = 1;
627 else if (!strcmp (p, "-Wno-write-strings"))
628 warn_write_strings = 0;
629 else if (!strcmp (p, "-Wcast-qual"))
630 warn_cast_qual = 1;
631 else if (!strcmp (p, "-Wno-cast-qual"))
632 warn_cast_qual = 0;
5c8902bb
RK
633 else if (!strcmp (p, "-Wbad-function-cast"))
634 warn_bad_function_cast = 1;
635 else if (!strcmp (p, "-Wno-bad-function-cast"))
636 warn_bad_function_cast = 0;
51e29401
RS
637 else if (!strcmp (p, "-Wpointer-arith"))
638 warn_pointer_arith = 1;
639 else if (!strcmp (p, "-Wno-pointer-arith"))
640 warn_pointer_arith = 0;
641 else if (!strcmp (p, "-Wstrict-prototypes"))
642 warn_strict_prototypes = 1;
643 else if (!strcmp (p, "-Wno-strict-prototypes"))
644 warn_strict_prototypes = 0;
645 else if (!strcmp (p, "-Wmissing-prototypes"))
646 warn_missing_prototypes = 1;
647 else if (!strcmp (p, "-Wno-missing-prototypes"))
648 warn_missing_prototypes = 0;
1474fe46
RK
649 else if (!strcmp (p, "-Wmissing-declarations"))
650 warn_missing_declarations = 1;
651 else if (!strcmp (p, "-Wno-missing-declarations"))
652 warn_missing_declarations = 0;
51e29401
RS
653 else if (!strcmp (p, "-Wredundant-decls"))
654 warn_redundant_decls = 1;
ec2343c4 655 else if (!strcmp (p, "-Wno-redundant-decls"))
51e29401
RS
656 warn_redundant_decls = 0;
657 else if (!strcmp (p, "-Wnested-externs"))
658 warn_nested_externs = 1;
659 else if (!strcmp (p, "-Wno-nested-externs"))
660 warn_nested_externs = 0;
661 else if (!strcmp (p, "-Wtraditional"))
662 warn_traditional = 1;
663 else if (!strcmp (p, "-Wno-traditional"))
664 warn_traditional = 0;
665 else if (!strcmp (p, "-Wformat"))
666 warn_format = 1;
667 else if (!strcmp (p, "-Wno-format"))
668 warn_format = 0;
669 else if (!strcmp (p, "-Wchar-subscripts"))
670 warn_char_subscripts = 1;
671 else if (!strcmp (p, "-Wno-char-subscripts"))
672 warn_char_subscripts = 0;
673 else if (!strcmp (p, "-Wconversion"))
674 warn_conversion = 1;
675 else if (!strcmp (p, "-Wno-conversion"))
676 warn_conversion = 0;
677 else if (!strcmp (p, "-Wparentheses"))
678 warn_parentheses = 1;
679 else if (!strcmp (p, "-Wno-parentheses"))
680 warn_parentheses = 0;
57916f61
RS
681 else if (!strcmp (p, "-Wreturn-type"))
682 warn_return_type = 1;
683 else if (!strcmp (p, "-Wno-return-type"))
684 warn_return_type = 0;
51e29401
RS
685 else if (!strcmp (p, "-Wcomment"))
686 ; /* cpp handles this one. */
687 else if (!strcmp (p, "-Wno-comment"))
688 ; /* cpp handles this one. */
689 else if (!strcmp (p, "-Wcomments"))
690 ; /* cpp handles this one. */
691 else if (!strcmp (p, "-Wno-comments"))
692 ; /* cpp handles this one. */
693 else if (!strcmp (p, "-Wtrigraphs"))
694 ; /* cpp handles this one. */
695 else if (!strcmp (p, "-Wno-trigraphs"))
696 ; /* cpp handles this one. */
fc3ffe83
RK
697 else if (!strcmp (p, "-Wimport"))
698 ; /* cpp handles this one. */
699 else if (!strcmp (p, "-Wno-import"))
700 ; /* cpp handles this one. */
36e6fa69
RS
701 else if (!strcmp (p, "-Wmissing-braces"))
702 warn_missing_braces = 1;
703 else if (!strcmp (p, "-Wno-missing-braces"))
704 warn_missing_braces = 0;
51e29401
RS
705 else if (!strcmp (p, "-Wall"))
706 {
707 extra_warnings = 1;
a2468e45
BK
708 /* We save the value of warn_uninitialized, since if they put
709 -Wuninitialized on the command line, we need to generate a
710 warning about not using it without also specifying -O. */
711 if (warn_uninitialized != 1)
712 warn_uninitialized = 2;
51e29401
RS
713 warn_implicit = 1;
714 warn_return_type = 1;
715 warn_unused = 1;
716 warn_switch = 1;
717 warn_format = 1;
718 warn_char_subscripts = 1;
929f3671 719 warn_parentheses = 1;
36e6fa69 720 warn_missing_braces = 1;
51e29401
RS
721 }
722 else
723 return 0;
724
725 return 1;
726}
727
728/* Hooks for print_node. */
729
730void
27d433a2
BC
731print_lang_decl (file, node, indent)
732 FILE *file;
733 tree node;
734 int indent;
51e29401
RS
735{
736}
737
738void
27d433a2
BC
739print_lang_type (file, node, indent)
740 FILE *file;
741 tree node;
742 int indent;
51e29401
RS
743{
744}
745
746void
747print_lang_identifier (file, node, indent)
748 FILE *file;
749 tree node;
750 int indent;
751{
752 print_node (file, "global", IDENTIFIER_GLOBAL_VALUE (node), indent + 4);
753 print_node (file, "local", IDENTIFIER_LOCAL_VALUE (node), indent + 4);
754 print_node (file, "label", IDENTIFIER_LABEL_VALUE (node), indent + 4);
755 print_node (file, "implicit", IDENTIFIER_IMPLICIT_DECL (node), indent + 4);
756 print_node (file, "error locus", IDENTIFIER_ERROR_LOCUS (node), indent + 4);
fd0b8fce 757 print_node (file, "limbo value", IDENTIFIER_LIMBO_VALUE (node), indent + 4);
51e29401
RS
758}
759\f
b4892310
RS
760/* Hook called at end of compilation to assume 1 elt
761 for a top-level array decl that wasn't complete before. */
762
763void
764finish_incomplete_decl (decl)
765 tree decl;
766{
767 if (TREE_CODE (decl) == VAR_DECL && TREE_TYPE (decl) != error_mark_node)
768 {
769 tree type = TREE_TYPE (decl);
770 if (TREE_CODE (type) == ARRAY_TYPE
771 && TYPE_DOMAIN (type) == 0
772 && TREE_CODE (decl) != TYPE_DECL)
773 {
774 complete_array_type (type, NULL_TREE, 1);
775
776 layout_decl (decl, 0);
777 }
778 }
779}
780\f
51e29401
RS
781/* Create a new `struct binding_level'. */
782
783static
784struct binding_level *
785make_binding_level ()
786{
787 /* NOSTRICT */
788 return (struct binding_level *) xmalloc (sizeof (struct binding_level));
789}
790
791/* Nonzero if we are currently in the global binding level. */
792
793int
794global_bindings_p ()
795{
796 return current_binding_level == global_binding_level;
797}
798
799void
800keep_next_level ()
801{
802 keep_next_level_flag = 1;
803}
804
805/* Nonzero if the current level needs to have a BLOCK made. */
806
807int
808kept_level_p ()
809{
810 return ((current_binding_level->keep_if_subblocks
811 && current_binding_level->blocks != 0)
812 || current_binding_level->keep
813 || current_binding_level->names != 0
814 || (current_binding_level->tags != 0
815 && !current_binding_level->tag_transparent));
816}
817
818/* Identify this binding level as a level of parameters.
89d7540d
RS
819 DEFINITION_FLAG is 1 for a definition, 0 for a declaration.
820 But it turns out there is no way to pass the right value for
821 DEFINITION_FLAG, so we ignore it. */
51e29401
RS
822
823void
824declare_parm_level (definition_flag)
825 int definition_flag;
826{
89d7540d 827 current_binding_level->parm_flag = 1;
51e29401
RS
828}
829
830/* Nonzero if currently making parm declarations. */
831
832int
833in_parm_level_p ()
834{
835 return current_binding_level->parm_flag;
836}
837
838/* Enter a new binding level.
839 If TAG_TRANSPARENT is nonzero, do so only for the name space of variables,
840 not for that of tags. */
841
842void
843pushlevel (tag_transparent)
844 int tag_transparent;
845{
846 register struct binding_level *newlevel = NULL_BINDING_LEVEL;
847
848 /* If this is the top level of a function,
849 just make sure that NAMED_LABELS is 0. */
850
851 if (current_binding_level == global_binding_level)
852 {
853 named_labels = 0;
854 }
855
856 /* Reuse or create a struct for this binding level. */
857
858 if (free_binding_level)
859 {
860 newlevel = free_binding_level;
861 free_binding_level = free_binding_level->level_chain;
862 }
863 else
864 {
865 newlevel = make_binding_level ();
866 }
867
868 /* Add this level to the front of the chain (stack) of levels that
869 are active. */
870
871 *newlevel = clear_binding_level;
872 newlevel->tag_transparent
873 = (tag_transparent
874 || (current_binding_level
875 ? current_binding_level->subblocks_tag_transparent
876 : 0));
877 newlevel->level_chain = current_binding_level;
878 current_binding_level = newlevel;
879 newlevel->keep = keep_next_level_flag;
880 keep_next_level_flag = 0;
881 newlevel->keep_if_subblocks = keep_next_if_subblocks;
882 keep_next_if_subblocks = 0;
883}
884
885/* Exit a binding level.
886 Pop the level off, and restore the state of the identifier-decl mappings
887 that were in effect when this level was entered.
888
889 If KEEP is nonzero, this level had explicit declarations, so
890 and create a "block" (a BLOCK node) for the level
891 to record its declarations and subblocks for symbol table output.
892
893 If FUNCTIONBODY is nonzero, this level is the body of a function,
894 so create a block as if KEEP were set and also clear out all
895 label names.
896
897 If REVERSE is nonzero, reverse the order of decls before putting
898 them into the BLOCK. */
899
900tree
901poplevel (keep, reverse, functionbody)
902 int keep;
903 int reverse;
904 int functionbody;
905{
906 register tree link;
907 /* The chain of decls was accumulated in reverse order.
908 Put it into forward order, just for cleanliness. */
909 tree decls;
910 tree tags = current_binding_level->tags;
911 tree subblocks = current_binding_level->blocks;
912 tree block = 0;
913 tree decl;
968e5643 914 int block_previously_created;
51e29401
RS
915
916 keep |= current_binding_level->keep;
917
918 /* This warning is turned off because it causes warnings for
919 declarations like `extern struct foo *x'. */
920#if 0
921 /* Warn about incomplete structure types in this level. */
922 for (link = tags; link; link = TREE_CHAIN (link))
923 if (TYPE_SIZE (TREE_VALUE (link)) == 0)
924 {
925 tree type = TREE_VALUE (link);
926 char *errmsg;
927 switch (TREE_CODE (type))
928 {
929 case RECORD_TYPE:
930 errmsg = "`struct %s' incomplete in scope ending here";
931 break;
932 case UNION_TYPE:
933 errmsg = "`union %s' incomplete in scope ending here";
934 break;
935 case ENUMERAL_TYPE:
936 errmsg = "`enum %s' incomplete in scope ending here";
937 break;
938 }
939 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
940 error (errmsg, IDENTIFIER_POINTER (TYPE_NAME (type)));
941 else
942 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
943 error (errmsg, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
944 }
945#endif /* 0 */
946
947 /* Get the decls in the order they were written.
948 Usually current_binding_level->names is in reverse order.
949 But parameter decls were previously put in forward order. */
950
951 if (reverse)
952 current_binding_level->names
953 = decls = nreverse (current_binding_level->names);
954 else
955 decls = current_binding_level->names;
956
957 /* Output any nested inline functions within this block
958 if they weren't already output. */
959
960 for (decl = decls; decl; decl = TREE_CHAIN (decl))
961 if (TREE_CODE (decl) == FUNCTION_DECL
962 && ! TREE_ASM_WRITTEN (decl)
963 && DECL_INITIAL (decl) != 0
964 && TREE_ADDRESSABLE (decl))
42dfa47f
RS
965 {
966 /* If this decl was copied from a file-scope decl
967 on account of a block-scope extern decl,
968 propagate TREE_ADDRESSABLE to the file-scope decl. */
969 if (DECL_ABSTRACT_ORIGIN (decl) != 0)
970 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (decl)) = 1;
971 else
47df0262
RK
972 {
973 push_function_context ();
974 output_inline_function (decl);
975 pop_function_context ();
976 }
42dfa47f 977 }
51e29401
RS
978
979 /* If there were any declarations or structure tags in that level,
980 or if this level is a function body,
981 create a BLOCK to record them for the life of this function. */
982
3bf40d18 983 block = 0;
968e5643
RS
984 block_previously_created = (current_binding_level->this_block != 0);
985 if (block_previously_created)
3bf40d18
RS
986 block = current_binding_level->this_block;
987 else if (keep || functionbody
988 || (current_binding_level->keep_if_subblocks && subblocks != 0))
989 block = make_node (BLOCK);
990 if (block != 0)
991 {
992 BLOCK_VARS (block) = decls;
993 BLOCK_TYPE_TAGS (block) = tags;
994 BLOCK_SUBBLOCKS (block) = subblocks;
4ecc65ac 995 remember_end_note (block);
3bf40d18 996 }
51e29401
RS
997
998 /* In each subblock, record that this is its superior. */
999
1000 for (link = subblocks; link; link = TREE_CHAIN (link))
1001 BLOCK_SUPERCONTEXT (link) = block;
1002
1003 /* Clear out the meanings of the local variables of this level. */
1004
1005 for (link = decls; link; link = TREE_CHAIN (link))
1006 {
1007 if (DECL_NAME (link) != 0)
1008 {
1009 /* If the ident. was used or addressed via a local extern decl,
1010 don't forget that fact. */
1394aabd 1011 if (DECL_EXTERNAL (link))
51e29401
RS
1012 {
1013 if (TREE_USED (link))
1014 TREE_USED (DECL_NAME (link)) = 1;
1015 if (TREE_ADDRESSABLE (link))
1016 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (link)) = 1;
1017 }
1018 IDENTIFIER_LOCAL_VALUE (DECL_NAME (link)) = 0;
1019 }
1020 }
1021
1022 /* Restore all name-meanings of the outer levels
1023 that were shadowed by this level. */
1024
1025 for (link = current_binding_level->shadowed; link; link = TREE_CHAIN (link))
1026 IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
1027
1028 /* If the level being exited is the top level of a function,
1029 check over all the labels, and clear out the current
1030 (function local) meanings of their names. */
1031
1032 if (functionbody)
1033 {
1034 /* If this is the top level block of a function,
1035 the vars are the function's parameters.
1036 Don't leave them in the BLOCK because they are
1037 found in the FUNCTION_DECL instead. */
1038
1039 BLOCK_VARS (block) = 0;
1040
1041 /* Clear out the definitions of all label names,
1042 since their scopes end here,
1043 and add them to BLOCK_VARS. */
1044
1045 for (link = named_labels; link; link = TREE_CHAIN (link))
1046 {
1047 register tree label = TREE_VALUE (link);
1048
1049 if (DECL_INITIAL (label) == 0)
1050 {
1051 error_with_decl (label, "label `%s' used but not defined");
1052 /* Avoid crashing later. */
1053 define_label (input_filename, lineno,
1054 DECL_NAME (label));
1055 }
1056 else if (warn_unused && !TREE_USED (label))
1057 warning_with_decl (label, "label `%s' defined but not used");
1058 IDENTIFIER_LABEL_VALUE (DECL_NAME (label)) = 0;
1059
1060 /* Put the labels into the "variables" of the
1061 top-level block, so debugger can see them. */
1062 TREE_CHAIN (label) = BLOCK_VARS (block);
1063 BLOCK_VARS (block) = label;
1064 }
1065 }
1066
1067 /* Pop the current level, and free the structure for reuse. */
1068
1069 {
1070 register struct binding_level *level = current_binding_level;
1071 current_binding_level = current_binding_level->level_chain;
1072
1073 level->level_chain = free_binding_level;
1074 free_binding_level = level;
1075 }
1076
1077 /* Dispose of the block that we just made inside some higher level. */
1078 if (functionbody)
1079 DECL_INITIAL (current_function_decl) = block;
1080 else if (block)
968e5643
RS
1081 {
1082 if (!block_previously_created)
1083 current_binding_level->blocks
1084 = chainon (current_binding_level->blocks, block);
1085 }
51e29401
RS
1086 /* If we did not make a block for the level just exited,
1087 any blocks made for inner levels
1088 (since they cannot be recorded as subblocks in that level)
1089 must be carried forward so they will later become subblocks
1090 of something else. */
1091 else if (subblocks)
1092 current_binding_level->blocks
1093 = chainon (current_binding_level->blocks, subblocks);
1094
1095 /* Set the TYPE_CONTEXTs for all of the tagged types belonging to this
1096 binding contour so that they point to the appropriate construct, i.e.
1097 either to the current FUNCTION_DECL node, or else to the BLOCK node
1098 we just constructed.
1099
1100 Note that for tagged types whose scope is just the formal parameter
1101 list for some function type specification, we can't properly set
1102 their TYPE_CONTEXTs here, because we don't have a pointer to the
1103 appropriate FUNCTION_TYPE node readily available to us. For those
1104 cases, the TYPE_CONTEXTs of the relevant tagged type nodes get set
1105 in `grokdeclarator' as soon as we have created the FUNCTION_TYPE
1106 node which will represent the "scope" for these "parameter list local"
1107 tagged types.
1108 */
1109
1110 if (functionbody)
1111 for (link = tags; link; link = TREE_CHAIN (link))
1112 TYPE_CONTEXT (TREE_VALUE (link)) = current_function_decl;
1113 else if (block)
1114 for (link = tags; link; link = TREE_CHAIN (link))
1115 TYPE_CONTEXT (TREE_VALUE (link)) = block;
1116
1117 if (block)
1118 TREE_USED (block) = 1;
1119 return block;
1120}
3bf40d18
RS
1121
1122/* Delete the node BLOCK from the current binding level.
1123 This is used for the block inside a stmt expr ({...})
1124 so that the block can be reinserted where appropriate. */
1125
1126void
1127delete_block (block)
1128 tree block;
1129{
1130 tree t;
1131 if (current_binding_level->blocks == block)
1132 current_binding_level->blocks = TREE_CHAIN (block);
1133 for (t = current_binding_level->blocks; t;)
1134 {
1135 if (TREE_CHAIN (t) == block)
1136 TREE_CHAIN (t) = TREE_CHAIN (block);
1137 else
1138 t = TREE_CHAIN (t);
1139 }
1140 TREE_CHAIN (block) = NULL;
1141 /* Clear TREE_USED which is always set by poplevel.
1142 The flag is set again if insert_block is called. */
1143 TREE_USED (block) = 0;
1144}
1145
1146/* Insert BLOCK at the end of the list of subblocks of the
1147 current binding level. This is used when a BIND_EXPR is expanded,
6f65afb0 1148 to handle the BLOCK node inside the BIND_EXPR. */
3bf40d18
RS
1149
1150void
1151insert_block (block)
1152 tree block;
1153{
1154 TREE_USED (block) = 1;
1155 current_binding_level->blocks
1156 = chainon (current_binding_level->blocks, block);
1157}
1158
968e5643 1159/* Set the BLOCK node for the innermost scope
3bf40d18
RS
1160 (the one we are currently in). */
1161
968e5643
RS
1162void
1163set_block (block)
1164 register tree block;
3bf40d18 1165{
968e5643 1166 current_binding_level->this_block = block;
3bf40d18 1167}
51e29401
RS
1168\f
1169void
1170push_label_level ()
1171{
1172 register struct binding_level *newlevel;
1173
1174 /* Reuse or create a struct for this binding level. */
1175
1176 if (free_binding_level)
1177 {
1178 newlevel = free_binding_level;
1179 free_binding_level = free_binding_level->level_chain;
1180 }
1181 else
1182 {
1183 newlevel = make_binding_level ();
1184 }
1185
1186 /* Add this level to the front of the chain (stack) of label levels. */
1187
1188 newlevel->level_chain = label_level_chain;
1189 label_level_chain = newlevel;
1190
1191 newlevel->names = named_labels;
1192 newlevel->shadowed = shadowed_labels;
1193 named_labels = 0;
1194 shadowed_labels = 0;
1195}
1196
1197void
1198pop_label_level ()
1199{
1200 register struct binding_level *level = label_level_chain;
1201 tree link, prev;
1202
1203 /* Clear out the definitions of the declared labels in this level.
1204 Leave in the list any ordinary, non-declared labels. */
1205 for (link = named_labels, prev = 0; link;)
1206 {
1207 if (C_DECLARED_LABEL_FLAG (TREE_VALUE (link)))
1208 {
1209 if (DECL_SOURCE_LINE (TREE_VALUE (link)) == 0)
1210 {
6b2a374b
RK
1211 error_with_decl (TREE_VALUE (link),
1212 "label `%s' used but not defined");
51e29401
RS
1213 /* Avoid crashing later. */
1214 define_label (input_filename, lineno,
1215 DECL_NAME (TREE_VALUE (link)));
1216 }
1217 else if (warn_unused && !TREE_USED (TREE_VALUE (link)))
1218 warning_with_decl (TREE_VALUE (link),
1219 "label `%s' defined but not used");
1220 IDENTIFIER_LABEL_VALUE (DECL_NAME (TREE_VALUE (link))) = 0;
1221
1222 /* Delete this element from the list. */
1223 link = TREE_CHAIN (link);
1224 if (prev)
1225 TREE_CHAIN (prev) = link;
1226 else
1227 named_labels = link;
1228 }
1229 else
1230 {
1231 prev = link;
1232 link = TREE_CHAIN (link);
1233 }
1234 }
1235
1236 /* Bring back all the labels that were shadowed. */
1237 for (link = shadowed_labels; link; link = TREE_CHAIN (link))
1238 if (DECL_NAME (TREE_VALUE (link)) != 0)
1239 IDENTIFIER_LABEL_VALUE (DECL_NAME (TREE_VALUE (link)))
1240 = TREE_VALUE (link);
1241
1242 named_labels = chainon (named_labels, level->names);
1243 shadowed_labels = level->shadowed;
1244
1245 /* Pop the current level, and free the structure for reuse. */
1246 label_level_chain = label_level_chain->level_chain;
1247 level->level_chain = free_binding_level;
1248 free_binding_level = level;
1249}
1250\f
1251/* Push a definition or a declaration of struct, union or enum tag "name".
1252 "type" should be the type node.
1253 We assume that the tag "name" is not already defined.
1254
1255 Note that the definition may really be just a forward reference.
1256 In that case, the TYPE_SIZE will be zero. */
1257
1258void
1259pushtag (name, type)
1260 tree name, type;
1261{
1262 register struct binding_level *b;
1263
1264 /* Find the proper binding level for this type tag. */
1265
1266 for (b = current_binding_level; b->tag_transparent; b = b->level_chain)
1267 continue;
1268
1269 if (name)
1270 {
1271 /* Record the identifier as the type's name if it has none. */
1272
1273 if (TYPE_NAME (type) == 0)
1274 TYPE_NAME (type) = name;
51e29401
RS
1275 }
1276
c138f328
RS
1277 if (b == global_binding_level)
1278 b->tags = perm_tree_cons (name, type, b->tags);
1279 else
1280 b->tags = saveable_tree_cons (name, type, b->tags);
1281
51e29401
RS
1282 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1283 tagged type we just added to the current binding level. This fake
1284 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
858a47b1 1285 to output a representation of a tagged type, and it also gives
51e29401
RS
1286 us a convenient place to record the "scope start" address for the
1287 tagged type. */
1288
8d9bfdc5 1289 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
51e29401
RS
1290}
1291\f
1292/* Handle when a new declaration NEWDECL
1293 has the same name as an old one OLDDECL
1294 in the same binding contour.
1295 Prints an error message if appropriate.
1296
1297 If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
1298 Otherwise, return 0. */
1299
1300static int
1301duplicate_decls (newdecl, olddecl)
1302 register tree newdecl, olddecl;
1303{
1304 int types_match = comptypes (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
1305 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1306 && DECL_INITIAL (newdecl) != 0);
664b4b1e
RS
1307 tree oldtype = TREE_TYPE (olddecl);
1308 tree newtype = TREE_TYPE (newdecl);
8e5e53da 1309 char *errmsg = 0;
51e29401 1310
664b4b1e
RS
1311 if (TREE_CODE (newtype) == ERROR_MARK
1312 || TREE_CODE (oldtype) == ERROR_MARK)
51e29401
RS
1313 types_match = 0;
1314
1315 /* New decl is completely inconsistent with the old one =>
1316 tell caller to replace the old one.
1317 This is always an error except in the case of shadowing a builtin. */
1318 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1319 {
1320 if (TREE_CODE (olddecl) == FUNCTION_DECL
70efc776
RS
1321 && (DECL_BUILT_IN (olddecl)
1322 || DECL_BUILT_IN_NONANSI (olddecl)))
51e29401 1323 {
70efc776
RS
1324 /* If you declare a built-in or predefined function name as static,
1325 the old definition is overridden,
51e29401
RS
1326 but optionally warn this was a bad choice of name. */
1327 if (!TREE_PUBLIC (newdecl))
1328 {
70efc776
RS
1329 if (!warn_shadow)
1330 ;
1331 else if (DECL_BUILT_IN (olddecl))
51e29401 1332 warning_with_decl (newdecl, "shadowing built-in function `%s'");
70efc776
RS
1333 else
1334 warning_with_decl (newdecl, "shadowing library function `%s'");
51e29401
RS
1335 }
1336 /* Likewise, if the built-in is not ansi, then programs can
929f3671 1337 override it even globally without an error. */
70efc776
RS
1338 else if (! DECL_BUILT_IN (olddecl))
1339 warning_with_decl (newdecl,
1340 "library function `%s' declared as non-function");
1341
51e29401
RS
1342 else if (DECL_BUILT_IN_NONANSI (olddecl))
1343 warning_with_decl (newdecl,
1344 "built-in function `%s' declared as non-function");
51e29401
RS
1345 else
1346 warning_with_decl (newdecl,
70efc776 1347 "built-in function `%s' declared as non-function");
51e29401
RS
1348 }
1349 else
1350 {
1351 error_with_decl (newdecl, "`%s' redeclared as different kind of symbol");
1352 error_with_decl (olddecl, "previous declaration of `%s'");
1353 }
1354
1355 return 0;
1356 }
1357
1358 /* For real parm decl following a forward decl,
1359 return 1 so old decl will be reused. */
1360 if (types_match && TREE_CODE (newdecl) == PARM_DECL
1361 && TREE_ASM_WRITTEN (olddecl) && ! TREE_ASM_WRITTEN (newdecl))
1362 return 1;
1363
1364 /* The new declaration is the same kind of object as the old one.
1365 The declarations may partially match. Print warnings if they don't
1366 match enough. Ultimately, copy most of the information from the new
1367 decl to the old one, and keep using the old one. */
1368
1369 if (flag_traditional && TREE_CODE (newdecl) == FUNCTION_DECL
1370 && IDENTIFIER_IMPLICIT_DECL (DECL_NAME (newdecl)) == olddecl
1371 && DECL_INITIAL (olddecl) == 0)
1372 /* If -traditional, avoid error for redeclaring fcn
1373 after implicit decl. */
1374 ;
1375 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1376 && DECL_BUILT_IN (olddecl))
1377 {
e841f997 1378 /* A function declaration for a built-in function. */
51e29401
RS
1379 if (!TREE_PUBLIC (newdecl))
1380 {
1381 /* If you declare a built-in function name as static, the
1382 built-in definition is overridden,
1383 but optionally warn this was a bad choice of name. */
1384 if (warn_shadow)
1385 warning_with_decl (newdecl, "shadowing built-in function `%s'");
1386 /* Discard the old built-in function. */
1387 return 0;
1388 }
1389 else if (!types_match)
4c41bbfa
RS
1390 {
1391 /* Accept the return type of the new declaration if same modes. */
1392 tree oldreturntype = TREE_TYPE (TREE_TYPE (olddecl));
1393 tree newreturntype = TREE_TYPE (TREE_TYPE (newdecl));
4fc7cf69
JW
1394
1395 /* Make sure we put the new type in the same obstack as the old ones.
1396 If the old types are not both in the same obstack, use the
1397 permanent one. */
1398 if (TYPE_OBSTACK (oldtype) == TYPE_OBSTACK (newtype))
1399 push_obstacks (TYPE_OBSTACK (oldtype), TYPE_OBSTACK (oldtype));
1400 else
1401 {
1402 push_obstacks_nochange ();
1403 end_temporary_allocation ();
1404 }
1405
4c41bbfa
RS
1406 if (TYPE_MODE (oldreturntype) == TYPE_MODE (newreturntype))
1407 {
32436219
RS
1408 /* Function types may be shared, so we can't just modify
1409 the return type of olddecl's function type. */
1410 tree newtype
1411 = build_function_type (newreturntype,
1412 TYPE_ARG_TYPES (TREE_TYPE (olddecl)));
1413
1f7586c1
RS
1414 types_match = comptypes (TREE_TYPE (newdecl), newtype);
1415 if (types_match)
1416 TREE_TYPE (olddecl) = newtype;
1417 }
1418 /* Accept harmless mismatch in first argument type also.
1419 This is for ffs. */
1420 if (TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != 0
1421 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) != 0
1422 && TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (newdecl))) != 0
1423 && TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (olddecl))) != 0
1424 && (TYPE_MODE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (newdecl))))
1425 ==
1426 TYPE_MODE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (olddecl))))))
1427 {
1428 /* Function types may be shared, so we can't just modify
1429 the return type of olddecl's function type. */
1430 tree newtype
1431 = build_function_type (TREE_TYPE (TREE_TYPE (olddecl)),
1432 tree_cons (NULL_TREE,
1433 TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (newdecl))),
1434 TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (olddecl)))));
1435
32436219
RS
1436 types_match = comptypes (TREE_TYPE (newdecl), newtype);
1437 if (types_match)
1438 TREE_TYPE (olddecl) = newtype;
4c41bbfa 1439 }
4fc7cf69
JW
1440
1441 pop_obstacks ();
4c41bbfa
RS
1442 }
1443 if (!types_match)
52b6a22f
RS
1444 {
1445 /* If types don't match for a built-in, throw away the built-in. */
1446 warning_with_decl (newdecl, "conflicting types for built-in function `%s'");
1447 return 0;
1448 }
51e29401
RS
1449 }
1450 else if (TREE_CODE (olddecl) == FUNCTION_DECL
e841f997 1451 && DECL_SOURCE_LINE (olddecl) == 0)
51e29401 1452 {
e841f997
RS
1453 /* A function declaration for a predeclared function
1454 that isn't actually built in. */
51e29401
RS
1455 if (!TREE_PUBLIC (newdecl))
1456 {
858a47b1 1457 /* If you declare it as static, the
e841f997 1458 default definition is overridden. */
51e29401
RS
1459 return 0;
1460 }
1461 else if (!types_match)
e841f997 1462 {
4d06f145
RS
1463 /* If the types don't match, preserve volatility indication.
1464 Later on, we will discard everything else about the
1465 default declaration. */
e841f997 1466 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
e841f997 1467 }
51e29401 1468 }
592252ad
RS
1469 /* Permit char *foo () to match void *foo (...) if not pedantic,
1470 if one of them came from a system header file. */
a4219ac7
RS
1471 else if (!types_match
1472 && TREE_CODE (olddecl) == FUNCTION_DECL
1473 && TREE_CODE (newdecl) == FUNCTION_DECL
664b4b1e
RS
1474 && TREE_CODE (TREE_TYPE (oldtype)) == POINTER_TYPE
1475 && TREE_CODE (TREE_TYPE (newtype)) == POINTER_TYPE
592252ad
RS
1476 && (DECL_IN_SYSTEM_HEADER (olddecl)
1477 || DECL_IN_SYSTEM_HEADER (newdecl))
5fe86b8b 1478 && ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (newtype))) == void_type_node
664b4b1e
RS
1479 && TYPE_ARG_TYPES (oldtype) == 0
1480 && self_promoting_args_p (TYPE_ARG_TYPES (newtype))
1481 && TREE_TYPE (TREE_TYPE (oldtype)) == char_type_node)
a4219ac7 1482 ||
664b4b1e
RS
1483 (TREE_TYPE (TREE_TYPE (newtype)) == char_type_node
1484 && TYPE_ARG_TYPES (newtype) == 0
1485 && self_promoting_args_p (TYPE_ARG_TYPES (oldtype))
5fe86b8b 1486 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (oldtype))) == void_type_node)))
a4219ac7
RS
1487 {
1488 if (pedantic)
1489 pedwarn_with_decl (newdecl, "conflicting types for `%s'");
664b4b1e 1490 /* Make sure we keep void * as ret type, not char *. */
5fe86b8b 1491 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (oldtype))) == void_type_node)
664b4b1e 1492 TREE_TYPE (newdecl) = newtype = oldtype;
1d00bef8
JW
1493
1494 /* Set DECL_IN_SYSTEM_HEADER, so that if we see another declaration
1495 we will come back here again. */
1496 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
a4219ac7 1497 }
51e29401
RS
1498 else if (!types_match
1499 /* Permit char *foo (int, ...); followed by char *foo ();
1500 if not pedantic. */
1501 && ! (TREE_CODE (olddecl) == FUNCTION_DECL
1502 && ! pedantic
1503 /* Return types must still match. */
664b4b1e
RS
1504 && comptypes (TREE_TYPE (oldtype),
1505 TREE_TYPE (newtype))
1506 && TYPE_ARG_TYPES (newtype) == 0))
51e29401
RS
1507 {
1508 error_with_decl (newdecl, "conflicting types for `%s'");
1509 /* Check for function type mismatch
1510 involving an empty arglist vs a nonempty one. */
1511 if (TREE_CODE (olddecl) == FUNCTION_DECL
664b4b1e
RS
1512 && comptypes (TREE_TYPE (oldtype),
1513 TREE_TYPE (newtype))
1514 && ((TYPE_ARG_TYPES (oldtype) == 0
51e29401
RS
1515 && DECL_INITIAL (olddecl) == 0)
1516 ||
664b4b1e 1517 (TYPE_ARG_TYPES (newtype) == 0
51e29401
RS
1518 && DECL_INITIAL (newdecl) == 0)))
1519 {
1520 /* Classify the problem further. */
664b4b1e 1521 register tree t = TYPE_ARG_TYPES (oldtype);
51e29401 1522 if (t == 0)
664b4b1e 1523 t = TYPE_ARG_TYPES (newtype);
51e29401
RS
1524 for (; t; t = TREE_CHAIN (t))
1525 {
1526 register tree type = TREE_VALUE (t);
1527
5fe86b8b
RS
1528 if (TREE_CHAIN (t) == 0
1529 && TYPE_MAIN_VARIANT (type) != void_type_node)
51e29401
RS
1530 {
1531 error ("A parameter list with an ellipsis can't match");
1532 error ("an empty parameter name list declaration.");
1533 break;
1534 }
1535
90d56da8 1536 if (TYPE_MAIN_VARIANT (type) == float_type_node
24bc4c7f 1537 || C_PROMOTING_INTEGER_TYPE_P (type))
51e29401
RS
1538 {
1539 error ("An argument type that has a default promotion");
1540 error ("can't match an empty parameter name list declaration.");
1541 break;
1542 }
1543 }
1544 }
1545 error_with_decl (olddecl, "previous declaration of `%s'");
1546 }
1547 else
1548 {
8e5e53da 1549 errmsg = redeclaration_error_message (newdecl, olddecl);
51e29401
RS
1550 if (errmsg)
1551 {
1552 error_with_decl (newdecl, errmsg);
1553 error_with_decl (olddecl,
d847e6a7
RS
1554 ((DECL_INITIAL (olddecl)
1555 && current_binding_level == global_binding_level)
1556 ? "`%s' previously defined here"
1557 : "`%s' previously declared here"));
51e29401
RS
1558 }
1559 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1560 && DECL_INITIAL (olddecl) != 0
664b4b1e
RS
1561 && TYPE_ARG_TYPES (oldtype) == 0
1562 && TYPE_ARG_TYPES (newtype) != 0)
51e29401
RS
1563 {
1564 register tree type, parm;
1565 register int nargs;
1566 /* Prototype decl follows defn w/o prototype. */
1567
664b4b1e
RS
1568 for (parm = TYPE_ACTUAL_ARG_TYPES (oldtype),
1569 type = TYPE_ARG_TYPES (newtype),
51e29401 1570 nargs = 1;
5fe86b8b
RS
1571 (TYPE_MAIN_VARIANT (TREE_VALUE (parm)) != void_type_node
1572 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node);
51e29401
RS
1573 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type), nargs++)
1574 {
5fe86b8b
RS
1575 if (TYPE_MAIN_VARIANT (TREE_VALUE (parm)) == void_type_node
1576 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
51e29401
RS
1577 {
1578 errmsg = "prototype for `%s' follows and number of arguments";
1579 break;
1580 }
1581 /* Type for passing arg must be consistent
1582 with that declared for the arg. */
1583 if (! comptypes (TREE_VALUE (parm), TREE_VALUE (type))
1584 /* If -traditional, allow `unsigned int' instead of `int'
1585 in the prototype. */
1586 && (! (flag_traditional
90d56da8
RS
1587 && TYPE_MAIN_VARIANT (TREE_VALUE (parm)) == integer_type_node
1588 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == unsigned_type_node)))
51e29401
RS
1589 {
1590 errmsg = "prototype for `%s' follows and argument %d";
1591 break;
1592 }
1593 }
1594 if (errmsg)
1595 {
1596 error_with_decl (newdecl, errmsg, nargs);
1597 error_with_decl (olddecl,
1598 "doesn't match non-prototype definition here");
1599 }
1600 else
1601 {
1602 warning_with_decl (newdecl, "prototype for `%s' follows");
1603 warning_with_decl (olddecl, "non-prototype definition here");
1604 }
1605 }
8eebb258 1606 /* Warn about mismatches in various flags. */
51e29401
RS
1607 else
1608 {
8eebb258
RS
1609 /* Warn if function is now inline
1610 but was previously declared not inline and has been called. */
51e29401 1611 if (TREE_CODE (olddecl) == FUNCTION_DECL
1394aabd 1612 && ! DECL_INLINE (olddecl) && DECL_INLINE (newdecl)
51e29401
RS
1613 && TREE_USED (olddecl))
1614 warning_with_decl (newdecl,
1615 "`%s' declared inline after being called");
1616 if (TREE_CODE (olddecl) == FUNCTION_DECL
1394aabd 1617 && ! DECL_INLINE (olddecl) && DECL_INLINE (newdecl)
960a2eb1 1618 && DECL_INITIAL (olddecl) != 0)
51e29401 1619 warning_with_decl (newdecl,
960a2eb1 1620 "`%s' declared inline after its definition");
a1a77352
JW
1621
1622 /* If pedantic, warn when static declaration follows a non-static
1623 declaration. Otherwise, do so only for functions. */
1624 if ((pedantic || TREE_CODE (olddecl) == FUNCTION_DECL)
51e29401
RS
1625 && TREE_PUBLIC (olddecl)
1626 && !TREE_PUBLIC (newdecl))
1627 warning_with_decl (newdecl, "static declaration for `%s' follows non-static");
1628
2af6a433
RK
1629 /* Warn when const declaration follows a non-const
1630 declaration, but not for functions. */
1631 if (TREE_CODE (olddecl) != FUNCTION_DECL
1632 && !TREE_READONLY (olddecl)
1633 && TREE_READONLY (newdecl))
1634 warning_with_decl (newdecl, "const declaration for `%s' follows non-const");
8eebb258
RS
1635 /* These bits are logically part of the type, for variables.
1636 But not for functions
1637 (where qualifiers are not valid ANSI anyway). */
2af6a433 1638 else if (pedantic && TREE_CODE (olddecl) != FUNCTION_DECL
51e29401
RS
1639 && (TREE_READONLY (newdecl) != TREE_READONLY (olddecl)
1640 || TREE_THIS_VOLATILE (newdecl) != TREE_THIS_VOLATILE (olddecl)))
1641 pedwarn_with_decl (newdecl, "type qualifiers for `%s' conflict with previous decl");
1642 }
1643 }
1644
27f427f8 1645 /* Optionally warn about more than one declaration for the same name. */
5ce574f2 1646 if (errmsg == 0 && warn_redundant_decls && DECL_SOURCE_LINE (olddecl) != 0
27f427f8
RS
1647 /* Dont warn about a function declaration
1648 followed by a definition. */
1649 && !(TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl) != 0
ec820a12
RS
1650 && DECL_INITIAL (olddecl) == 0)
1651 /* Don't warn about extern decl followed by (tentative) definition. */
1652 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl)))
27f427f8
RS
1653 {
1654 warning_with_decl (newdecl, "redundant redeclaration of `%s' in same scope");
1655 warning_with_decl (olddecl, "previous declaration of `%s'");
1656 }
1657
51e29401 1658 /* Copy all the DECL_... slots specified in the new decl
664b4b1e
RS
1659 except for any that we copy here from the old type.
1660
1661 Past this point, we don't change OLDTYPE and NEWTYPE
1662 even if we change the types of NEWDECL and OLDDECL. */
51e29401
RS
1663
1664 if (types_match)
1665 {
46b1c393
RK
1666 /* Make sure we put the new type in the same obstack as the old ones.
1667 If the old types are not both in the same obstack, use the permanent
1668 one. */
1669 if (TYPE_OBSTACK (oldtype) == TYPE_OBSTACK (newtype))
1670 push_obstacks (TYPE_OBSTACK (oldtype), TYPE_OBSTACK (oldtype));
1671 else
1672 {
1673 push_obstacks_nochange ();
1674 end_temporary_allocation ();
1675 }
1676
51e29401
RS
1677 /* Merge the data types specified in the two decls. */
1678 if (TREE_CODE (newdecl) != FUNCTION_DECL || !DECL_BUILT_IN (olddecl))
1679 TREE_TYPE (newdecl)
1680 = TREE_TYPE (olddecl)
664b4b1e 1681 = common_type (newtype, oldtype);
51e29401
RS
1682
1683 /* Lay the type out, unless already done. */
1684 if (oldtype != TREE_TYPE (newdecl))
1685 {
1686 if (TREE_TYPE (newdecl) != error_mark_node)
1687 layout_type (TREE_TYPE (newdecl));
1688 if (TREE_CODE (newdecl) != FUNCTION_DECL
1689 && TREE_CODE (newdecl) != TYPE_DECL
1690 && TREE_CODE (newdecl) != CONST_DECL)
1691 layout_decl (newdecl, 0);
1692 }
1693 else
1694 {
1695 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1696 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
ec2343c4
MM
1697 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1698 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1699 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
51e29401
RS
1700 }
1701
fc542d3c
RS
1702 /* Keep the old rtl since we can safely use it. */
1703 DECL_RTL (newdecl) = DECL_RTL (olddecl);
1704
51e29401
RS
1705 /* Merge the type qualifiers. */
1706 if (DECL_BUILT_IN_NONANSI (olddecl) && TREE_THIS_VOLATILE (olddecl)
1707 && !TREE_THIS_VOLATILE (newdecl))
1708 TREE_THIS_VOLATILE (olddecl) = 0;
1709 if (TREE_READONLY (newdecl))
1710 TREE_READONLY (olddecl) = 1;
1711 if (TREE_THIS_VOLATILE (newdecl))
e2f6a3cf
RS
1712 {
1713 TREE_THIS_VOLATILE (olddecl) = 1;
1714 if (TREE_CODE (newdecl) == VAR_DECL)
1715 make_var_volatile (newdecl);
1716 }
51e29401 1717
e3c56d1b
RK
1718 /* Keep source location of definition rather than declaration.
1719 Likewise, keep decl at outer scope. */
1720 if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1721 || (DECL_CONTEXT (newdecl) != 0 && DECL_CONTEXT (olddecl) == 0))
51e29401
RS
1722 {
1723 DECL_SOURCE_LINE (newdecl) = DECL_SOURCE_LINE (olddecl);
1724 DECL_SOURCE_FILE (newdecl) = DECL_SOURCE_FILE (olddecl);
e3c56d1b
RK
1725
1726 if (DECL_CONTEXT (olddecl) == 0)
1727 DECL_CONTEXT (newdecl) = 0;
51e29401
RS
1728 }
1729
e2f6a3cf
RS
1730 /* Merge the unused-warning information. */
1731 if (DECL_IN_SYSTEM_HEADER (olddecl))
1732 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1733 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1734 DECL_IN_SYSTEM_HEADER (olddecl) = 1;
1735
51e29401
RS
1736 /* Merge the initialization information. */
1737 if (DECL_INITIAL (newdecl) == 0)
1738 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1e50b042
DE
1739
1740 /* Merge the section attribute.
1741 We want to issue an error if the sections conflict but that must be
1742 done later in decl_attributes since we are called before attributes
1743 are assigned. */
1744 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1745 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
46b1c393
RK
1746
1747 pop_obstacks ();
51e29401
RS
1748 }
1749 /* If cannot merge, then use the new type and qualifiers,
1750 and don't preserve the old rtl. */
1751 else
1752 {
1753 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1754 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
1755 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
1756 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
1757 }
1758
1759 /* Merge the storage class information. */
1760 /* For functions, static overrides non-static. */
1761 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1762 {
1763 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1764 /* This is since we don't automatically
1765 copy the attributes of NEWDECL into OLDDECL. */
1766 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1767 /* If this clears `static', clear it in the identifier too. */
1768 if (! TREE_PUBLIC (olddecl))
1769 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1770 }
1394aabd 1771 if (DECL_EXTERNAL (newdecl))
51e29401
RS
1772 {
1773 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1394aabd 1774 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
51e29401
RS
1775 /* An extern decl does not override previous storage class. */
1776 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1777 }
1778 else
1779 {
1780 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
51e29401
RS
1781 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1782 }
850cba29 1783
51e29401
RS
1784 /* If either decl says `inline', this fn is inline,
1785 unless its definition was passed already. */
1394aabd
RS
1786 if (DECL_INLINE (newdecl) && DECL_INITIAL (olddecl) == 0)
1787 DECL_INLINE (olddecl) = 1;
850cba29 1788 DECL_INLINE (newdecl) = DECL_INLINE (olddecl);
51e29401
RS
1789
1790 /* Get rid of any built-in function if new arg types don't match it
1791 or if we have a function definition. */
1792 if (TREE_CODE (newdecl) == FUNCTION_DECL
1793 && DECL_BUILT_IN (olddecl)
1794 && (!types_match || new_is_definition))
1795 {
1796 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1797 DECL_BUILT_IN (olddecl) = 0;
1798 }
1799
1800 /* If redeclaring a builtin function, and not a definition,
1801 it stays built in.
1802 Also preserve various other info from the definition. */
1803 if (TREE_CODE (newdecl) == FUNCTION_DECL && !new_is_definition)
1804 {
1805 if (DECL_BUILT_IN (olddecl))
1806 {
1807 DECL_BUILT_IN (newdecl) = 1;
678566a5 1808 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
51e29401
RS
1809 }
1810 else
1811 DECL_FRAME_SIZE (newdecl) = DECL_FRAME_SIZE (olddecl);
1812
1813 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1814 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1815 DECL_SAVED_INSNS (newdecl) = DECL_SAVED_INSNS (olddecl);
1816 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1817 }
1818
850cba29
RS
1819 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1820 But preserve OLDdECL's DECL_UID. */
1821 {
1822 register unsigned olddecl_uid = DECL_UID (olddecl);
1823
1824 bcopy ((char *) newdecl + sizeof (struct tree_common),
1825 (char *) olddecl + sizeof (struct tree_common),
1826 sizeof (struct tree_decl) - sizeof (struct tree_common));
1827 DECL_UID (olddecl) = olddecl_uid;
1828 }
51e29401
RS
1829
1830 return 1;
1831}
1832
1833/* Record a decl-node X as belonging to the current lexical scope.
1834 Check for errors (such as an incompatible declaration for the same
1835 name already seen in the same scope).
1836
1837 Returns either X or an old decl for the same name.
1838 If an old decl is returned, it may have been smashed
1839 to agree with what X says. */
1840
1841tree
1842pushdecl (x)
1843 tree x;
1844{
1845 register tree t;
1846 register tree name = DECL_NAME (x);
1847 register struct binding_level *b = current_binding_level;
1848
1849 DECL_CONTEXT (x) = current_function_decl;
89d7540d
RS
1850 /* A local extern declaration for a function doesn't constitute nesting.
1851 A local auto declaration does, since it's a forward decl
1852 for a nested function coming later. */
1853 if (TREE_CODE (x) == FUNCTION_DECL && DECL_INITIAL (x) == 0
1854 && DECL_EXTERNAL (x))
51e29401
RS
1855 DECL_CONTEXT (x) = 0;
1856
1394aabd 1857 if (warn_nested_externs && DECL_EXTERNAL (x) && b != global_binding_level
6e026f48
MM
1858 && x != IDENTIFIER_IMPLICIT_DECL (name)
1859 /* Don't print error messages for __FUNCTION__ and __PRETTY_FUNCTION__ */
1860 && !DECL_IN_SYSTEM_HEADER (x))
51e29401
RS
1861 warning ("nested extern declaration of `%s'", IDENTIFIER_POINTER (name));
1862
1863 if (name)
1864 {
1865 char *file;
1866 int line;
1867
93f623fa
JW
1868 /* Don't type check externs here when -traditional. This is so that
1869 code with conflicting declarations inside blocks will get warnings
1870 not errors. X11 for instance depends on this. */
1871 if (DECL_EXTERNAL (x) && TREE_PUBLIC (x) && ! flag_traditional)
e32fe224 1872 t = lookup_name_current_level_global (name);
7dcf01c2
JW
1873 else
1874 t = lookup_name_current_level (name);
51e29401
RS
1875 if (t != 0 && t == error_mark_node)
1876 /* error_mark_node is 0 for a while during initialization! */
1877 {
1878 t = 0;
1879 error_with_decl (x, "`%s' used prior to declaration");
1880 }
1881
1882 if (t != 0)
1883 {
1884 file = DECL_SOURCE_FILE (t);
1885 line = DECL_SOURCE_LINE (t);
1886 }
1887
1888 if (t != 0 && duplicate_decls (x, t))
1889 {
1890 if (TREE_CODE (t) == PARM_DECL)
1891 {
1892 /* Don't allow more than one "real" duplicate
1893 of a forward parm decl. */
1894 TREE_ASM_WRITTEN (t) = TREE_ASM_WRITTEN (x);
1895 return t;
1896 }
1897 /* If this decl is `static' and an implicit decl was seen previously,
1898 warn. But don't complain if -traditional,
1899 since traditional compilers don't complain. */
1900 if (!flag_traditional && TREE_PUBLIC (name)
1394aabd 1901 && ! TREE_PUBLIC (x) && ! DECL_EXTERNAL (x)
51e29401
RS
1902 /* We used to warn also for explicit extern followed by static,
1903 but sometimes you need to do it that way. */
1904 && IDENTIFIER_IMPLICIT_DECL (name) != 0)
1905 {
1906 pedwarn ("`%s' was declared implicitly `extern' and later `static'",
1907 IDENTIFIER_POINTER (name));
1908 pedwarn_with_file_and_line (file, line,
1909 "previous declaration of `%s'",
1910 IDENTIFIER_POINTER (name));
1911 }
27f427f8 1912
e32fe224
JW
1913 /* If this is a global decl, and there exists a conflicting local
1914 decl in a parent block, then we can't return as yet, because we
1915 need to register this decl in the current binding block. */
1916 if (! DECL_EXTERNAL (x) || ! TREE_PUBLIC (x)
1917 || lookup_name (name) == t)
1918 return t;
51e29401
RS
1919 }
1920
1ce634c3
RS
1921 /* If we are processing a typedef statement, generate a whole new
1922 ..._TYPE node (which will be just an variant of the existing
1923 ..._TYPE node with identical properties) and then install the
1924 TYPE_DECL node generated to represent the typedef name as the
1925 TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1926
1927 The whole point here is to end up with a situation where each
1928 and every ..._TYPE node the compiler creates will be uniquely
1929 associated with AT MOST one node representing a typedef name.
1930 This way, even though the compiler substitutes corresponding
1931 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1932 early on, later parts of the compiler can always do the reverse
1933 translation and get back the corresponding typedef name. For
1934 example, given:
1935
1936 typedef struct S MY_TYPE;
1937 MY_TYPE object;
1938
1939 Later parts of the compiler might only know that `object' was of
1940 type `struct S' if if were not for code just below. With this
1941 code however, later parts of the compiler see something like:
1942
1943 struct S' == struct S
1944 typedef struct S' MY_TYPE;
1945 struct S' object;
1946
1947 And they can then deduce (from the node for type struct S') that
1948 the original object declaration was:
1949
1950 MY_TYPE object;
1951
1952 Being able to do this is important for proper support of protoize,
1953 and also for generating precise symbolic debugging information
1954 which takes full account of the programmer's (typedef) vocabulary.
1955
1956 Obviously, we don't want to generate a duplicate ..._TYPE node if
1957 the TYPE_DECL node that we are now processing really represents a
1958 standard built-in type.
1959
51e29401
RS
1960 Since all standard types are effectively declared at line zero
1961 in the source file, we can easily check to see if we are working
1962 on a standard type by checking the current value of lineno. */
1963
1964 if (TREE_CODE (x) == TYPE_DECL)
1965 {
1600ec67 1966 if (DECL_SOURCE_LINE (x) == 0)
51e29401
RS
1967 {
1968 if (TYPE_NAME (TREE_TYPE (x)) == 0)
1969 TYPE_NAME (TREE_TYPE (x)) = x;
1970 }
32831d4b 1971 else if (TREE_TYPE (x) != error_mark_node)
51e29401
RS
1972 {
1973 tree tt = TREE_TYPE (x);
1974
1ce634c3 1975 tt = build_type_copy (tt);
51e29401
RS
1976 TYPE_NAME (tt) = x;
1977 TREE_TYPE (x) = tt;
1978 }
1979 }
1980
fd0b8fce 1981 /* Multiple external decls of the same identifier ought to match.
93f623fa
JW
1982 Check against both global declarations (when traditional) and out of
1983 scope (limbo) block level declarations.
51e29401 1984
fd0b8fce
JW
1985 We get warnings about inline functions where they are defined.
1986 Avoid duplicate warnings where they are used. */
93f623fa 1987 if (TREE_PUBLIC (x) && ! DECL_INLINE (x))
51e29401 1988 {
93f623fa
JW
1989 tree decl;
1990
1991 if (flag_traditional && IDENTIFIER_GLOBAL_VALUE (name) != 0
1992 && (DECL_EXTERNAL (IDENTIFIER_GLOBAL_VALUE (name))
1993 || TREE_PUBLIC (IDENTIFIER_GLOBAL_VALUE (name))))
1994 decl = IDENTIFIER_GLOBAL_VALUE (name);
1995 else if (IDENTIFIER_LIMBO_VALUE (name) != 0)
1996 /* Decls in limbo are always extern, so no need to check that. */
1997 decl = IDENTIFIER_LIMBO_VALUE (name);
1998 else
1999 decl = 0;
fd0b8fce 2000
93f623fa 2001 if (decl && ! comptypes (TREE_TYPE (x), TREE_TYPE (decl))
58811315
RS
2002 /* If old decl is built-in, we already warned if we should. */
2003 && !DECL_BUILT_IN (decl))
51e29401
RS
2004 {
2005 pedwarn_with_decl (x,
2006 "type mismatch with previous external decl");
fd0b8fce 2007 pedwarn_with_decl (decl, "previous external decl of `%s'");
51e29401
RS
2008 }
2009 }
2010
2011 /* If a function has had an implicit declaration, and then is defined,
2012 make sure they are compatible. */
2013
2014 if (IDENTIFIER_IMPLICIT_DECL (name) != 0
2015 && IDENTIFIER_GLOBAL_VALUE (name) == 0
2016 && TREE_CODE (x) == FUNCTION_DECL
2017 && ! comptypes (TREE_TYPE (x),
2018 TREE_TYPE (IDENTIFIER_IMPLICIT_DECL (name))))
2019 {
2020 warning_with_decl (x, "type mismatch with previous implicit declaration");
929f3671
RS
2021 warning_with_decl (IDENTIFIER_IMPLICIT_DECL (name),
2022 "previous implicit declaration of `%s'");
51e29401
RS
2023 }
2024
2025 /* In PCC-compatibility mode, extern decls of vars with no current decl
2026 take effect at top level no matter where they are. */
1394aabd 2027 if (flag_traditional && DECL_EXTERNAL (x)
51e29401
RS
2028 && lookup_name (name) == 0)
2029 {
2030 tree type = TREE_TYPE (x);
2031
2032 /* But don't do this if the type contains temporary nodes. */
2033 while (type)
2034 {
2035 if (type == error_mark_node)
2036 break;
2037 if (! TREE_PERMANENT (type))
2038 {
2039 warning_with_decl (x, "type of external `%s' is not global");
2040 /* By exiting the loop early, we leave TYPE nonzero,
2041 and thus prevent globalization of the decl. */
2042 break;
2043 }
2044 else if (TREE_CODE (type) == FUNCTION_TYPE
2045 && TYPE_ARG_TYPES (type) != 0)
2046 /* The types might not be truly local,
2047 but the list of arg types certainly is temporary.
2048 Since prototypes are nontraditional,
2049 ok not to do the traditional thing. */
2050 break;
2051 type = TREE_TYPE (type);
2052 }
2053
2054 if (type == 0)
2055 b = global_binding_level;
2056 }
2057
2058 /* This name is new in its binding level.
2059 Install the new declaration and return it. */
2060 if (b == global_binding_level)
2061 {
2062 /* Install a global value. */
2063
2064 /* If the first global decl has external linkage,
2065 warn if we later see static one. */
2066 if (IDENTIFIER_GLOBAL_VALUE (name) == 0 && TREE_PUBLIC (x))
2067 TREE_PUBLIC (name) = 1;
2068
2069 IDENTIFIER_GLOBAL_VALUE (name) = x;
2070
fd0b8fce
JW
2071 /* We no longer care about any previous block level declarations. */
2072 IDENTIFIER_LIMBO_VALUE (name) = 0;
2073
51e29401
RS
2074 /* Don't forget if the function was used via an implicit decl. */
2075 if (IDENTIFIER_IMPLICIT_DECL (name)
2076 && TREE_USED (IDENTIFIER_IMPLICIT_DECL (name)))
2077 TREE_USED (x) = 1, TREE_USED (name) = 1;
2078
2079 /* Don't forget if its address was taken in that way. */
2080 if (IDENTIFIER_IMPLICIT_DECL (name)
2081 && TREE_ADDRESSABLE (IDENTIFIER_IMPLICIT_DECL (name)))
2082 TREE_ADDRESSABLE (x) = 1;
2083
2084 /* Warn about mismatches against previous implicit decl. */
2085 if (IDENTIFIER_IMPLICIT_DECL (name) != 0
2086 /* If this real decl matches the implicit, don't complain. */
2087 && ! (TREE_CODE (x) == FUNCTION_DECL
90d56da8
RS
2088 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (x)))
2089 == integer_type_node)))
51e29401
RS
2090 pedwarn ("`%s' was previously implicitly declared to return `int'",
2091 IDENTIFIER_POINTER (name));
2092
2093 /* If this decl is `static' and an `extern' was seen previously,
2094 that is erroneous. */
2095 if (TREE_PUBLIC (name)
1394aabd 2096 && ! TREE_PUBLIC (x) && ! DECL_EXTERNAL (x))
51e29401 2097 {
b0e919de
RS
2098 /* Okay to redeclare an ANSI built-in as static. */
2099 if (t != 0 && DECL_BUILT_IN (t))
929f3671
RS
2100 ;
2101 /* Okay to declare a non-ANSI built-in as anything. */
2102 else if (t != 0 && DECL_BUILT_IN_NONANSI (t))
2103 ;
2104 else if (IDENTIFIER_IMPLICIT_DECL (name))
51e29401
RS
2105 pedwarn ("`%s' was declared implicitly `extern' and later `static'",
2106 IDENTIFIER_POINTER (name));
2107 else
2108 pedwarn ("`%s' was declared `extern' and later `static'",
2109 IDENTIFIER_POINTER (name));
2110 }
2111 }
2112 else
2113 {
2114 /* Here to install a non-global value. */
2115 tree oldlocal = IDENTIFIER_LOCAL_VALUE (name);
2116 tree oldglobal = IDENTIFIER_GLOBAL_VALUE (name);
2117 IDENTIFIER_LOCAL_VALUE (name) = x;
2118
2119 /* If this is an extern function declaration, see if we
fc542d3c 2120 have a global definition or declaration for the function. */
51e29401 2121 if (oldlocal == 0
1394aabd 2122 && DECL_EXTERNAL (x) && !DECL_INLINE (x)
51e29401
RS
2123 && oldglobal != 0
2124 && TREE_CODE (x) == FUNCTION_DECL
2125 && TREE_CODE (oldglobal) == FUNCTION_DECL)
2126 {
2127 /* We have one. Their types must agree. */
2128 if (! comptypes (TREE_TYPE (x),
2129 TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (name))))
fc542d3c
RS
2130 pedwarn_with_decl (x, "extern declaration of `%s' doesn't match global one");
2131 else
2132 {
2133 /* Inner extern decl is inline if global one is.
2134 Copy enough to really inline it. */
2135 if (DECL_INLINE (oldglobal))
2136 {
2137 DECL_INLINE (x) = DECL_INLINE (oldglobal);
4135e766
RS
2138 DECL_INITIAL (x) = (current_function_decl == oldglobal
2139 ? 0 : DECL_INITIAL (oldglobal));
fc542d3c 2140 DECL_SAVED_INSNS (x) = DECL_SAVED_INSNS (oldglobal);
a2469305 2141 DECL_FRAME_SIZE (x) = DECL_FRAME_SIZE (oldglobal);
fc542d3c 2142 DECL_ARGUMENTS (x) = DECL_ARGUMENTS (oldglobal);
42dfa47f
RS
2143 DECL_RESULT (x) = DECL_RESULT (oldglobal);
2144 TREE_ASM_WRITTEN (x) = TREE_ASM_WRITTEN (oldglobal);
2145 DECL_ABSTRACT_ORIGIN (x) = oldglobal;
fc542d3c
RS
2146 }
2147 /* Inner extern decl is built-in if global one is. */
2148 if (DECL_BUILT_IN (oldglobal))
2149 {
2150 DECL_BUILT_IN (x) = DECL_BUILT_IN (oldglobal);
678566a5 2151 DECL_FUNCTION_CODE (x) = DECL_FUNCTION_CODE (oldglobal);
fc542d3c
RS
2152 }
2153 /* Keep the arg types from a file-scope fcn defn. */
2154 if (TYPE_ARG_TYPES (TREE_TYPE (oldglobal)) != 0
2155 && DECL_INITIAL (oldglobal)
2156 && TYPE_ARG_TYPES (TREE_TYPE (x)) == 0)
2157 TREE_TYPE (x) = TREE_TYPE (oldglobal);
2158 }
51e29401
RS
2159 }
2160
2161#if 0 /* This case is probably sometimes the right thing to do. */
2162 /* If we have a local external declaration,
2163 then any file-scope declaration should not
2164 have been static. */
2165 if (oldlocal == 0 && oldglobal != 0
2166 && !TREE_PUBLIC (oldglobal)
1394aabd 2167 && DECL_EXTERNAL (x) && TREE_PUBLIC (x))
51e29401
RS
2168 warning ("`%s' locally external but globally static",
2169 IDENTIFIER_POINTER (name));
2170#endif
2171
2172 /* If we have a local external declaration,
2173 and no file-scope declaration has yet been seen,
2174 then if we later have a file-scope decl it must not be static. */
2175 if (oldlocal == 0
2176 && oldglobal == 0
1394aabd 2177 && DECL_EXTERNAL (x)
51e29401
RS
2178 && TREE_PUBLIC (x))
2179 {
2180 TREE_PUBLIC (name) = 1;
fd0b8fce
JW
2181
2182 /* Save this decl, so that we can do type checking against
2183 other decls after it falls out of scope.
2184
2185 Only save it once. This prevents temporary decls created in
2186 expand_inline_function from being used here, since this
2187 will have been set when the inline function was parsed.
2188 It also helps give slightly better warnings. */
2189 if (IDENTIFIER_LIMBO_VALUE (name) == 0)
2190 IDENTIFIER_LIMBO_VALUE (name) = x;
51e29401
RS
2191 }
2192
2193 /* Warn if shadowing an argument at the top level of the body. */
1394aabd 2194 if (oldlocal != 0 && !DECL_EXTERNAL (x)
51e29401
RS
2195 /* This warning doesn't apply to the parms of a nested fcn. */
2196 && ! current_binding_level->parm_flag
2197 /* Check that this is one level down from the parms. */
2198 && current_binding_level->level_chain->parm_flag
2199 /* Check that the decl being shadowed
2200 comes from the parm level, one level up. */
2201 && chain_member (oldlocal, current_binding_level->level_chain->names))
2202 {
2203 if (TREE_CODE (oldlocal) == PARM_DECL)
2204 pedwarn ("declaration of `%s' shadows a parameter",
2205 IDENTIFIER_POINTER (name));
2206 else
2207 pedwarn ("declaration of `%s' shadows a symbol from the parameter list",
2208 IDENTIFIER_POINTER (name));
2209 }
2210
2211 /* Maybe warn if shadowing something else. */
1394aabd 2212 else if (warn_shadow && !DECL_EXTERNAL (x)
2bae939e 2213 /* No shadow warnings for internally generated vars. */
7a0347ff 2214 && DECL_SOURCE_LINE (x) != 0
51e29401 2215 /* No shadow warnings for vars made for inlining. */
b032c74c 2216 && ! DECL_FROM_INLINE (x))
51e29401
RS
2217 {
2218 char *warnstring = 0;
2219
2220 if (TREE_CODE (x) == PARM_DECL
89d7540d
RS
2221 && current_binding_level->level_chain->parm_flag)
2222 /* Don't warn about the parm names in function declarator
2223 within a function declarator.
2224 It would be nice to avoid warning in any function
2225 declarator in a declaration, as opposed to a definition,
2226 but there is no way to tell it's not a definition. */
51e29401
RS
2227 ;
2228 else if (oldlocal != 0 && TREE_CODE (oldlocal) == PARM_DECL)
2229 warnstring = "declaration of `%s' shadows a parameter";
2230 else if (oldlocal != 0)
2231 warnstring = "declaration of `%s' shadows previous local";
2232 else if (IDENTIFIER_GLOBAL_VALUE (name) != 0
2233 && IDENTIFIER_GLOBAL_VALUE (name) != error_mark_node)
2234 warnstring = "declaration of `%s' shadows global declaration";
2235
2236 if (warnstring)
2237 warning (warnstring, IDENTIFIER_POINTER (name));
2238 }
2239
2240 /* If storing a local value, there may already be one (inherited).
2241 If so, record it for restoration when this binding level ends. */
2242 if (oldlocal != 0)
2243 b->shadowed = tree_cons (name, oldlocal, b->shadowed);
2244 }
2245
2246 /* Keep count of variables in this level with incomplete type. */
2247 if (TYPE_SIZE (TREE_TYPE (x)) == 0)
2248 ++b->n_incomplete;
2249 }
2250
2251 /* Put decls on list in reverse order.
2252 We will reverse them later if necessary. */
2253 TREE_CHAIN (x) = b->names;
2254 b->names = x;
2255
2256 return x;
2257}
2258
2259/* Like pushdecl, only it places X in GLOBAL_BINDING_LEVEL, if appropriate. */
2260
2261tree
2262pushdecl_top_level (x)
2263 tree x;
2264{
2265 register tree t;
2266 register struct binding_level *b = current_binding_level;
2267
2268 current_binding_level = global_binding_level;
2269 t = pushdecl (x);
2270 current_binding_level = b;
2271 return t;
2272}
2273\f
2274/* Generate an implicit declaration for identifier FUNCTIONID
2275 as a function of type int (). Print a warning if appropriate. */
2276
2277tree
2278implicitly_declare (functionid)
2279 tree functionid;
2280{
2281 register tree decl;
2282 int traditional_warning = 0;
2283 /* Only one "implicit declaration" warning per identifier. */
2284 int implicit_warning;
2285
2286 /* Save the decl permanently so we can warn if definition follows. */
2287 push_obstacks_nochange ();
2288 end_temporary_allocation ();
2289
2290 /* We used to reuse an old implicit decl here,
2291 but this loses with inline functions because it can clobber
2292 the saved decl chains. */
2293/* if (IDENTIFIER_IMPLICIT_DECL (functionid) != 0)
2294 decl = IDENTIFIER_IMPLICIT_DECL (functionid);
2295 else */
2296 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2297
2298 /* Warn of implicit decl following explicit local extern decl.
2299 This is probably a program designed for traditional C. */
2300 if (TREE_PUBLIC (functionid) && IDENTIFIER_GLOBAL_VALUE (functionid) == 0)
2301 traditional_warning = 1;
2302
2303 /* Warn once of an implicit declaration. */
2304 implicit_warning = (IDENTIFIER_IMPLICIT_DECL (functionid) == 0);
2305
1394aabd 2306 DECL_EXTERNAL (decl) = 1;
51e29401
RS
2307 TREE_PUBLIC (decl) = 1;
2308
2309 /* Record that we have an implicit decl and this is it. */
2310 IDENTIFIER_IMPLICIT_DECL (functionid) = decl;
2311
2312 /* ANSI standard says implicit declarations are in the innermost block.
2313 So we record the decl in the standard fashion.
2314 If flag_traditional is set, pushdecl does it top-level. */
2315 pushdecl (decl);
2316
2317 /* This is a no-op in c-lang.c or something real in objc-actions.c. */
2318 maybe_objc_check_decl (decl);
2319
8d9bfdc5 2320 rest_of_decl_compilation (decl, NULL_PTR, 0, 0);
51e29401
RS
2321
2322 if (warn_implicit && implicit_warning)
2323 warning ("implicit declaration of function `%s'",
2324 IDENTIFIER_POINTER (functionid));
2325 else if (warn_traditional && traditional_warning)
2326 warning ("function `%s' was previously declared within a block",
2327 IDENTIFIER_POINTER (functionid));
2328
2329 /* Write a record describing this implicit function declaration to the
2330 prototypes file (if requested). */
2331
2332 gen_aux_info_record (decl, 0, 1, 0);
2333
2334 pop_obstacks ();
2335
2336 return decl;
2337}
2338
2339/* Return zero if the declaration NEWDECL is valid
2340 when the declaration OLDDECL (assumed to be for the same name)
2341 has already been seen.
2342 Otherwise return an error message format string with a %s
2343 where the identifier should go. */
2344
2345static char *
2346redeclaration_error_message (newdecl, olddecl)
2347 tree newdecl, olddecl;
2348{
2349 if (TREE_CODE (newdecl) == TYPE_DECL)
2350 {
2351 if (flag_traditional && TREE_TYPE (newdecl) == TREE_TYPE (olddecl))
2352 return 0;
2353 return "redefinition of `%s'";
2354 }
2355 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2356 {
2357 /* Declarations of functions can insist on internal linkage
2358 but they can't be inconsistent with internal linkage,
2359 so there can be no error on that account.
2360 However defining the same name twice is no good. */
2361 if (DECL_INITIAL (olddecl) != 0 && DECL_INITIAL (newdecl) != 0
2362 /* However, defining once as extern inline and a second
2363 time in another way is ok. */
1394aabd
RS
2364 && !(DECL_INLINE (olddecl) && DECL_EXTERNAL (olddecl)
2365 && !(DECL_INLINE (newdecl) && DECL_EXTERNAL (newdecl))))
51e29401
RS
2366 return "redefinition of `%s'";
2367 return 0;
2368 }
2369 else if (current_binding_level == global_binding_level)
2370 {
2371 /* Objects declared at top level: */
2372 /* If at least one is a reference, it's ok. */
1394aabd 2373 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
51e29401
RS
2374 return 0;
2375 /* Reject two definitions. */
2376 if (DECL_INITIAL (olddecl) != 0 && DECL_INITIAL (newdecl) != 0)
2377 return "redefinition of `%s'";
2378 /* Now we have two tentative defs, or one tentative and one real def. */
2379 /* Insist that the linkage match. */
2380 if (TREE_PUBLIC (olddecl) != TREE_PUBLIC (newdecl))
2381 return "conflicting declarations of `%s'";
2382 return 0;
2383 }
2384 else if (current_binding_level->parm_flag
2385 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2386 return 0;
2387 else
2388 {
7dcf01c2
JW
2389 /* Newdecl has block scope. If olddecl has block scope also, then
2390 reject two definitions, and reject a definition together with an
2391 external reference. Otherwise, it is OK, because newdecl must
2392 be an extern reference to olddecl. */
2393 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl))
2394 && DECL_CONTEXT (newdecl) == DECL_CONTEXT (olddecl))
51e29401
RS
2395 return "redeclaration of `%s'";
2396 return 0;
2397 }
2398}
2399\f
2400/* Get the LABEL_DECL corresponding to identifier ID as a label.
2401 Create one if none exists so far for the current function.
2402 This function is called for both label definitions and label references. */
2403
2404tree
2405lookup_label (id)
2406 tree id;
2407{
2408 register tree decl = IDENTIFIER_LABEL_VALUE (id);
2409
7d9795e5
RS
2410 if (current_function_decl == 0)
2411 {
2412 error ("label %s referenced outside of any function",
2413 IDENTIFIER_POINTER (id));
2414 return 0;
2415 }
2416
51e29401
RS
2417 /* Use a label already defined or ref'd with this name. */
2418 if (decl != 0)
2419 {
2420 /* But not if it is inherited and wasn't declared to be inheritable. */
2421 if (DECL_CONTEXT (decl) != current_function_decl
2422 && ! C_DECLARED_LABEL_FLAG (decl))
2423 return shadow_label (id);
2424 return decl;
2425 }
2426
2427 decl = build_decl (LABEL_DECL, id, void_type_node);
2428
8fcd361e
RS
2429 /* Make sure every label has an rtx. */
2430 label_rtx (decl);
2431
51e29401
RS
2432 /* A label not explicitly declared must be local to where it's ref'd. */
2433 DECL_CONTEXT (decl) = current_function_decl;
2434
2435 DECL_MODE (decl) = VOIDmode;
2436
2437 /* Say where one reference is to the label,
2438 for the sake of the error if it is not defined. */
2439 DECL_SOURCE_LINE (decl) = lineno;
2440 DECL_SOURCE_FILE (decl) = input_filename;
2441
2442 IDENTIFIER_LABEL_VALUE (id) = decl;
2443
2444 named_labels = tree_cons (NULL_TREE, decl, named_labels);
2445
2446 return decl;
2447}
2448
2449/* Make a label named NAME in the current function,
2450 shadowing silently any that may be inherited from containing functions
2451 or containing scopes.
2452
2453 Note that valid use, if the label being shadowed
2454 comes from another scope in the same function,
2455 requires calling declare_nonlocal_label right away. */
2456
2457tree
2458shadow_label (name)
2459 tree name;
2460{
2461 register tree decl = IDENTIFIER_LABEL_VALUE (name);
2462
2463 if (decl != 0)
2464 {
a784882b
RE
2465 register tree dup;
2466
2467 /* Check to make sure that the label hasn't already been declared
2468 at this label scope */
2469 for (dup = named_labels; dup; dup = TREE_CHAIN (dup))
2470 if (TREE_VALUE (dup) == decl)
2471 {
2472 error ("duplicate label declaration `%s'",
2473 IDENTIFIER_POINTER (name));
2474 error_with_decl (TREE_VALUE (dup),
2475 "this is a previous declaration");
2476 /* Just use the previous declaration. */
2477 return lookup_label (name);
2478 }
2479
51e29401
RS
2480 shadowed_labels = tree_cons (NULL_TREE, decl, shadowed_labels);
2481 IDENTIFIER_LABEL_VALUE (name) = decl = 0;
2482 }
2483
2484 return lookup_label (name);
2485}
2486
2487/* Define a label, specifying the location in the source file.
2488 Return the LABEL_DECL node for the label, if the definition is valid.
2489 Otherwise return 0. */
2490
2491tree
2492define_label (filename, line, name)
2493 char *filename;
2494 int line;
2495 tree name;
2496{
2497 tree decl = lookup_label (name);
2498
2499 /* If label with this name is known from an outer context, shadow it. */
2500 if (decl != 0 && DECL_CONTEXT (decl) != current_function_decl)
2501 {
2502 shadowed_labels = tree_cons (NULL_TREE, decl, shadowed_labels);
2503 IDENTIFIER_LABEL_VALUE (name) = 0;
2504 decl = lookup_label (name);
2505 }
2506
2507 if (DECL_INITIAL (decl) != 0)
2508 {
db2e669b 2509 error ("duplicate label `%s'", IDENTIFIER_POINTER (name));
51e29401
RS
2510 return 0;
2511 }
2512 else
2513 {
2514 /* Mark label as having been defined. */
2515 DECL_INITIAL (decl) = error_mark_node;
2516 /* Say where in the source. */
2517 DECL_SOURCE_FILE (decl) = filename;
2518 DECL_SOURCE_LINE (decl) = line;
2519 return decl;
2520 }
2521}
2522\f
2523/* Return the list of declarations of the current level.
2524 Note that this list is in reverse order unless/until
2525 you nreverse it; and when you do nreverse it, you must
2526 store the result back using `storedecls' or you will lose. */
2527
2528tree
2529getdecls ()
2530{
2531 return current_binding_level->names;
2532}
2533
2534/* Return the list of type-tags (for structs, etc) of the current level. */
2535
2536tree
2537gettags ()
2538{
2539 return current_binding_level->tags;
2540}
2541
2542/* Store the list of declarations of the current level.
2543 This is done for the parameter declarations of a function being defined,
2544 after they are modified in the light of any missing parameters. */
2545
2546static void
2547storedecls (decls)
2548 tree decls;
2549{
2550 current_binding_level->names = decls;
2551}
2552
2553/* Similarly, store the list of tags of the current level. */
2554
2555static void
2556storetags (tags)
2557 tree tags;
2558{
2559 current_binding_level->tags = tags;
2560}
2561\f
2562/* Given NAME, an IDENTIFIER_NODE,
2563 return the structure (or union or enum) definition for that name.
2564 Searches binding levels from BINDING_LEVEL up to the global level.
2565 If THISLEVEL_ONLY is nonzero, searches only the specified context
2566 (but skips any tag-transparent contexts to find one that is
2567 meaningful for tags).
2568 CODE says which kind of type the caller wants;
2569 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2570 If the wrong kind of type is found, an error is reported. */
2571
2572static tree
2573lookup_tag (code, name, binding_level, thislevel_only)
2574 enum tree_code code;
2575 struct binding_level *binding_level;
2576 tree name;
2577 int thislevel_only;
2578{
2579 register struct binding_level *level;
2580
2581 for (level = binding_level; level; level = level->level_chain)
2582 {
2583 register tree tail;
2584 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
2585 {
2586 if (TREE_PURPOSE (tail) == name)
2587 {
2588 if (TREE_CODE (TREE_VALUE (tail)) != code)
2589 {
2590 /* Definition isn't the kind we were looking for. */
2591 pending_invalid_xref = name;
2592 pending_invalid_xref_file = input_filename;
2593 pending_invalid_xref_line = lineno;
2594 }
2595 return TREE_VALUE (tail);
2596 }
2597 }
2598 if (thislevel_only && ! level->tag_transparent)
2599 return NULL_TREE;
2600 }
2601 return NULL_TREE;
2602}
2603
2604/* Print an error message now
2605 for a recent invalid struct, union or enum cross reference.
2606 We don't print them immediately because they are not invalid
2607 when used in the `struct foo;' construct for shadowing. */
2608
2609void
2610pending_xref_error ()
2611{
2612 if (pending_invalid_xref != 0)
2613 error_with_file_and_line (pending_invalid_xref_file,
2614 pending_invalid_xref_line,
2615 "`%s' defined as wrong kind of tag",
2616 IDENTIFIER_POINTER (pending_invalid_xref));
2617 pending_invalid_xref = 0;
2618}
2619
2620/* Given a type, find the tag that was defined for it and return the tag name.
2621 Otherwise return 0. */
2622
2623static tree
2624lookup_tag_reverse (type)
2625 tree type;
2626{
2627 register struct binding_level *level;
2628
2629 for (level = current_binding_level; level; level = level->level_chain)
2630 {
2631 register tree tail;
2632 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
2633 {
2634 if (TREE_VALUE (tail) == type)
2635 return TREE_PURPOSE (tail);
2636 }
2637 }
2638 return NULL_TREE;
2639}
2640\f
2641/* Look up NAME in the current binding level and its superiors
2642 in the namespace of variables, functions and typedefs.
2643 Return a ..._DECL node of some kind representing its definition,
2644 or return 0 if it is undefined. */
2645
2646tree
2647lookup_name (name)
2648 tree name;
2649{
2650 register tree val;
2651 if (current_binding_level != global_binding_level
2652 && IDENTIFIER_LOCAL_VALUE (name))
2653 val = IDENTIFIER_LOCAL_VALUE (name);
2654 else
2655 val = IDENTIFIER_GLOBAL_VALUE (name);
2656 return val;
2657}
2658
2659/* Similar to `lookup_name' but look only at current binding level. */
2660
f062da04 2661tree
51e29401
RS
2662lookup_name_current_level (name)
2663 tree name;
2664{
2665 register tree t;
2666
2667 if (current_binding_level == global_binding_level)
2668 return IDENTIFIER_GLOBAL_VALUE (name);
2669
2670 if (IDENTIFIER_LOCAL_VALUE (name) == 0)
2671 return 0;
2672
2673 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
2674 if (DECL_NAME (t) == name)
2675 break;
2676
2677 return t;
2678}
e32fe224
JW
2679
2680/* Similar to `lookup_name_current_level' but also look at the global binding
2681 level. */
2682
2683tree
2684lookup_name_current_level_global (name)
2685 tree name;
2686{
2687 register tree t = 0;
2688
2689 if (current_binding_level == global_binding_level)
2690 return IDENTIFIER_GLOBAL_VALUE (name);
2691
2692 if (IDENTIFIER_LOCAL_VALUE (name) != 0)
2693 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
2694 if (DECL_NAME (t) == name)
2695 break;
2696
2697 if (t == 0)
2698 t = IDENTIFIER_GLOBAL_VALUE (name);
2699
2700 return t;
2701}
51e29401
RS
2702\f
2703/* Create the predefined scalar types of C,
2704 and some nodes representing standard constants (0, 1, (void *)0).
2705 Initialize the global binding level.
2706 Make definitions for built-in primitive functions. */
2707
2708void
2709init_decl_processing ()
2710{
2711 register tree endlink;
2712 /* Either char* or void*. */
2713 tree traditional_ptr_type_node;
f0a45d37
RS
2714 /* Data types of memcpy and strlen. */
2715 tree memcpy_ftype, strlen_ftype;
9c4614c3 2716 tree void_ftype_any;
51e29401
RS
2717 int wchar_type_size;
2718 tree temp;
fba9adc6 2719 tree array_domain_type;
51e29401
RS
2720
2721 current_function_decl = NULL;
2722 named_labels = NULL;
2723 current_binding_level = NULL_BINDING_LEVEL;
2724 free_binding_level = NULL_BINDING_LEVEL;
2725 pushlevel (0); /* make the binding_level structure for global names */
2726 global_binding_level = current_binding_level;
2727
2728 /* Define `int' and `char' first so that dbx will output them first. */
2729
2730 integer_type_node = make_signed_type (INT_TYPE_SIZE);
2731 pushdecl (build_decl (TYPE_DECL, ridpointers[(int) RID_INT],
2732 integer_type_node));
2733
2734 /* Define `char', which is like either `signed char' or `unsigned char'
2735 but not the same as either. */
2736
7a0347ff
RS
2737 char_type_node
2738 = (flag_signed_char
2739 ? make_signed_type (CHAR_TYPE_SIZE)
2740 : make_unsigned_type (CHAR_TYPE_SIZE));
51e29401
RS
2741 pushdecl (build_decl (TYPE_DECL, get_identifier ("char"),
2742 char_type_node));
2743
2744 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
2745 pushdecl (build_decl (TYPE_DECL, get_identifier ("long int"),
2746 long_integer_type_node));
2747
2748 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
2749 pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned int"),
2750 unsigned_type_node));
2751
2752 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
2753 pushdecl (build_decl (TYPE_DECL, get_identifier ("long unsigned int"),
2754 long_unsigned_type_node));
2755
6706083e
DE
2756 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
2757 pushdecl (build_decl (TYPE_DECL, get_identifier ("long long int"),
2758 long_long_integer_type_node));
2759
2760 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
2761 pushdecl (build_decl (TYPE_DECL, get_identifier ("long long unsigned int"),
2762 long_long_unsigned_type_node));
2763
51e29401
RS
2764 /* `unsigned long' is the standard type for sizeof.
2765 Traditionally, use a signed type.
2766 Note that stddef.h uses `unsigned long',
2767 and this must agree, even of long and int are the same size. */
a34731a6
RK
2768 sizetype
2769 = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (get_identifier (SIZE_TYPE)));
2770 if (flag_traditional && TREE_UNSIGNED (sizetype))
2771 sizetype = signed_type (sizetype);
51e29401
RS
2772
2773 ptrdiff_type_node
2774 = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (get_identifier (PTRDIFF_TYPE)));
2775
2776 TREE_TYPE (TYPE_SIZE (integer_type_node)) = sizetype;
2777 TREE_TYPE (TYPE_SIZE (char_type_node)) = sizetype;
2778 TREE_TYPE (TYPE_SIZE (unsigned_type_node)) = sizetype;
2779 TREE_TYPE (TYPE_SIZE (long_unsigned_type_node)) = sizetype;
2780 TREE_TYPE (TYPE_SIZE (long_integer_type_node)) = sizetype;
6706083e
DE
2781 TREE_TYPE (TYPE_SIZE (long_long_integer_type_node)) = sizetype;
2782 TREE_TYPE (TYPE_SIZE (long_long_unsigned_type_node)) = sizetype;
51e29401
RS
2783
2784 error_mark_node = make_node (ERROR_MARK);
2785 TREE_TYPE (error_mark_node) = error_mark_node;
2786
2787 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
2788 pushdecl (build_decl (TYPE_DECL, get_identifier ("short int"),
2789 short_integer_type_node));
2790
51e29401
RS
2791 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
2792 pushdecl (build_decl (TYPE_DECL, get_identifier ("short unsigned int"),
2793 short_unsigned_type_node));
2794
51e29401
RS
2795 /* Define both `signed char' and `unsigned char'. */
2796 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
2797 pushdecl (build_decl (TYPE_DECL, get_identifier ("signed char"),
2798 signed_char_type_node));
2799
2800 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
2801 pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned char"),
2802 unsigned_char_type_node));
2803
fa7d8b92 2804 intQI_type_node = make_signed_type (GET_MODE_BITSIZE (QImode));
ac4f24e7
RS
2805 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intQI_type_node));
2806
fa7d8b92 2807 intHI_type_node = make_signed_type (GET_MODE_BITSIZE (HImode));
ac4f24e7
RS
2808 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intHI_type_node));
2809
fa7d8b92 2810 intSI_type_node = make_signed_type (GET_MODE_BITSIZE (SImode));
ac4f24e7
RS
2811 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intSI_type_node));
2812
fa7d8b92 2813 intDI_type_node = make_signed_type (GET_MODE_BITSIZE (DImode));
ac4f24e7
RS
2814 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intDI_type_node));
2815
fa7d8b92 2816 unsigned_intQI_type_node = make_unsigned_type (GET_MODE_BITSIZE (QImode));
ac4f24e7
RS
2817 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intQI_type_node));
2818
fa7d8b92 2819 unsigned_intHI_type_node = make_unsigned_type (GET_MODE_BITSIZE (HImode));
ac4f24e7
RS
2820 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intHI_type_node));
2821
fa7d8b92 2822 unsigned_intSI_type_node = make_unsigned_type (GET_MODE_BITSIZE (SImode));
ac4f24e7
RS
2823 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intSI_type_node));
2824
fa7d8b92 2825 unsigned_intDI_type_node = make_unsigned_type (GET_MODE_BITSIZE (DImode));
ac4f24e7
RS
2826 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intDI_type_node));
2827
51e29401
RS
2828 float_type_node = make_node (REAL_TYPE);
2829 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
2830 pushdecl (build_decl (TYPE_DECL, ridpointers[(int) RID_FLOAT],
2831 float_type_node));
2832 layout_type (float_type_node);
2833
2834 double_type_node = make_node (REAL_TYPE);
2835 if (flag_short_double)
2836 TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
2837 else
2838 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
2839 pushdecl (build_decl (TYPE_DECL, ridpointers[(int) RID_DOUBLE],
2840 double_type_node));
2841 layout_type (double_type_node);
2842
2843 long_double_type_node = make_node (REAL_TYPE);
2844 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
2845 pushdecl (build_decl (TYPE_DECL, get_identifier ("long double"),
2846 long_double_type_node));
2847 layout_type (long_double_type_node);
2848
5ab10c42
RS
2849 complex_integer_type_node = make_node (COMPLEX_TYPE);
2850 pushdecl (build_decl (TYPE_DECL, get_identifier ("complex int"),
2851 complex_integer_type_node));
2852 TREE_TYPE (complex_integer_type_node) = integer_type_node;
2853 layout_type (complex_integer_type_node);
2854
2855 complex_float_type_node = make_node (COMPLEX_TYPE);
2856 pushdecl (build_decl (TYPE_DECL, get_identifier ("complex float"),
2857 complex_float_type_node));
2858 TREE_TYPE (complex_float_type_node) = float_type_node;
2859 layout_type (complex_float_type_node);
2860
2861 complex_double_type_node = make_node (COMPLEX_TYPE);
2862 pushdecl (build_decl (TYPE_DECL, get_identifier ("complex double"),
2863 complex_double_type_node));
2864 TREE_TYPE (complex_double_type_node) = double_type_node;
2865 layout_type (complex_double_type_node);
2866
2867 complex_long_double_type_node = make_node (COMPLEX_TYPE);
2868 pushdecl (build_decl (TYPE_DECL, get_identifier ("complex long double"),
2869 complex_long_double_type_node));
2870 TREE_TYPE (complex_long_double_type_node) = long_double_type_node;
2871 layout_type (complex_long_double_type_node);
2872
51e29401
RS
2873 wchar_type_node
2874 = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (get_identifier (WCHAR_TYPE)));
2875 wchar_type_size = TYPE_PRECISION (wchar_type_node);
7e4849da
RS
2876 signed_wchar_type_node = signed_type (wchar_type_node);
2877 unsigned_wchar_type_node = unsigned_type (wchar_type_node);
51e29401
RS
2878
2879 integer_zero_node = build_int_2 (0, 0);
2880 TREE_TYPE (integer_zero_node) = integer_type_node;
2881 integer_one_node = build_int_2 (1, 0);
2882 TREE_TYPE (integer_one_node) = integer_type_node;
2883
2884 size_zero_node = build_int_2 (0, 0);
2885 TREE_TYPE (size_zero_node) = sizetype;
2886 size_one_node = build_int_2 (1, 0);
2887 TREE_TYPE (size_one_node) = sizetype;
2888
2889 void_type_node = make_node (VOID_TYPE);
2890 pushdecl (build_decl (TYPE_DECL,
2891 ridpointers[(int) RID_VOID], void_type_node));
2892 layout_type (void_type_node); /* Uses integer_zero_node */
2893 /* We are not going to have real types in C with less than byte alignment,
2894 so we might as well not have any types that claim to have it. */
2895 TYPE_ALIGN (void_type_node) = BITS_PER_UNIT;
2896
2897 null_pointer_node = build_int_2 (0, 0);
2898 TREE_TYPE (null_pointer_node) = build_pointer_type (void_type_node);
2899 layout_type (TREE_TYPE (null_pointer_node));
2900
2901 string_type_node = build_pointer_type (char_type_node);
2902 const_string_type_node
2903 = build_pointer_type (build_type_variant (char_type_node, 1, 0));
2904
fba9adc6 2905 /* Make a type to be the domain of a few array types
c81fe25d
RK
2906 whose domains don't really matter.
2907 200 is small enough that it always fits in size_t
2908 and large enough that it can hold most function names for the
2909 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
2910 array_domain_type = build_index_type (build_int_2 (200, 0));
fba9adc6
RS
2911
2912 /* make a type for arrays of characters.
51e29401
RS
2913 With luck nothing will ever really depend on the length of this
2914 array type. */
2915 char_array_type_node
fba9adc6 2916 = build_array_type (char_type_node, array_domain_type);
51e29401
RS
2917 /* Likewise for arrays of ints. */
2918 int_array_type_node
fba9adc6 2919 = build_array_type (integer_type_node, array_domain_type);
51e29401
RS
2920 /* This is for wide string constants. */
2921 wchar_array_type_node
fba9adc6 2922 = build_array_type (wchar_type_node, array_domain_type);
51e29401
RS
2923
2924 default_function_type
2925 = build_function_type (integer_type_node, NULL_TREE);
2926
2927 ptr_type_node = build_pointer_type (void_type_node);
2928 const_ptr_type_node
2929 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
2930
2931 endlink = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
2932
9c4614c3 2933 void_ftype_any
8d9bfdc5 2934 = build_function_type (void_type_node, NULL_TREE);
9c4614c3 2935
732149f9
RK
2936 float_ftype_float
2937 = build_function_type (float_type_node,
2938 tree_cons (NULL_TREE, float_type_node, endlink));
2939
51e29401
RS
2940 double_ftype_double
2941 = build_function_type (double_type_node,
2942 tree_cons (NULL_TREE, double_type_node, endlink));
2943
732149f9
RK
2944 ldouble_ftype_ldouble
2945 = build_function_type (long_double_type_node,
2946 tree_cons (NULL_TREE, long_double_type_node,
2947 endlink));
2948
51e29401
RS
2949 double_ftype_double_double
2950 = build_function_type (double_type_node,
2951 tree_cons (NULL_TREE, double_type_node,
2952 tree_cons (NULL_TREE,
2953 double_type_node, endlink)));
2954
2955 int_ftype_int
2956 = build_function_type (integer_type_node,
2957 tree_cons (NULL_TREE, integer_type_node, endlink));
2958
2959 long_ftype_long
2960 = build_function_type (long_integer_type_node,
2961 tree_cons (NULL_TREE,
2962 long_integer_type_node, endlink));
2963
2964 void_ftype_ptr_ptr_int
2965 = build_function_type (void_type_node,
2966 tree_cons (NULL_TREE, ptr_type_node,
2967 tree_cons (NULL_TREE, ptr_type_node,
2968 tree_cons (NULL_TREE,
2969 integer_type_node,
2970 endlink))));
2971
2972 int_ftype_cptr_cptr_sizet
2973 = build_function_type (integer_type_node,
2974 tree_cons (NULL_TREE, const_ptr_type_node,
2975 tree_cons (NULL_TREE, const_ptr_type_node,
2976 tree_cons (NULL_TREE,
2977 sizetype,
2978 endlink))));
2979
2980 void_ftype_ptr_int_int
2981 = build_function_type (void_type_node,
2982 tree_cons (NULL_TREE, ptr_type_node,
2983 tree_cons (NULL_TREE, integer_type_node,
2984 tree_cons (NULL_TREE,
2985 integer_type_node,
2986 endlink))));
2987
2988 string_ftype_ptr_ptr /* strcpy prototype */
2989 = build_function_type (string_type_node,
2990 tree_cons (NULL_TREE, string_type_node,
2991 tree_cons (NULL_TREE,
2992 const_string_type_node,
2993 endlink)));
2994
2995 int_ftype_string_string /* strcmp prototype */
2996 = build_function_type (integer_type_node,
2997 tree_cons (NULL_TREE, const_string_type_node,
2998 tree_cons (NULL_TREE,
2999 const_string_type_node,
3000 endlink)));
3001
f0a45d37
RS
3002 strlen_ftype /* strlen prototype */
3003 = build_function_type (flag_traditional ? integer_type_node : sizetype,
51e29401
RS
3004 tree_cons (NULL_TREE, const_string_type_node,
3005 endlink));
3006
3007 traditional_ptr_type_node
3008 = (flag_traditional ? string_type_node : ptr_type_node);
3009
3010 memcpy_ftype /* memcpy prototype */
3011 = build_function_type (traditional_ptr_type_node,
3012 tree_cons (NULL_TREE, ptr_type_node,
3013 tree_cons (NULL_TREE, const_ptr_type_node,
3014 tree_cons (NULL_TREE,
3015 sizetype,
3016 endlink))));
3017
5fe5c210 3018 builtin_function ("__builtin_constant_p", int_ftype_int,
8d9bfdc5 3019 BUILT_IN_CONSTANT_P, NULL_PTR);
51e29401
RS
3020
3021 builtin_function ("__builtin_return_address",
530fb43c 3022 build_function_type (ptr_type_node,
51e29401
RS
3023 tree_cons (NULL_TREE,
3024 unsigned_type_node,
3025 endlink)),
8d9bfdc5 3026 BUILT_IN_RETURN_ADDRESS, NULL_PTR);
51e29401
RS
3027
3028 builtin_function ("__builtin_frame_address",
530fb43c 3029 build_function_type (ptr_type_node,
51e29401
RS
3030 tree_cons (NULL_TREE,
3031 unsigned_type_node,
3032 endlink)),
8d9bfdc5 3033 BUILT_IN_FRAME_ADDRESS, NULL_PTR);
51e29401
RS
3034
3035 builtin_function ("__builtin_alloca",
3036 build_function_type (ptr_type_node,
3037 tree_cons (NULL_TREE,
3038 sizetype,
3039 endlink)),
3040 BUILT_IN_ALLOCA, "alloca");
1f53ee05
RS
3041 builtin_function ("__builtin_ffs", int_ftype_int, BUILT_IN_FFS, NULL_PTR);
3042 /* Define alloca, ffs as builtins.
3043 Declare _exit just to mark it as volatile. */
fc3ffe83 3044 if (! flag_no_builtin && !flag_no_nonansi_builtin)
51e29401 3045 {
51e29401
RS
3046 temp = builtin_function ("alloca",
3047 build_function_type (ptr_type_node,
3048 tree_cons (NULL_TREE,
3049 sizetype,
3050 endlink)),
8d9bfdc5 3051 BUILT_IN_ALLOCA, NULL_PTR);
51e29401
RS
3052 /* Suppress error if redefined as a non-function. */
3053 DECL_BUILT_IN_NONANSI (temp) = 1;
1f53ee05
RS
3054 temp = builtin_function ("ffs", int_ftype_int, BUILT_IN_FFS, NULL_PTR);
3055 /* Suppress error if redefined as a non-function. */
3056 DECL_BUILT_IN_NONANSI (temp) = 1;
8d9bfdc5
RK
3057 temp = builtin_function ("_exit", void_ftype_any, NOT_BUILT_IN,
3058 NULL_PTR);
51e29401
RS
3059 TREE_THIS_VOLATILE (temp) = 1;
3060 TREE_SIDE_EFFECTS (temp) = 1;
3061 /* Suppress error if redefined as a non-function. */
3062 DECL_BUILT_IN_NONANSI (temp) = 1;
3063 }
3064
8d9bfdc5 3065 builtin_function ("__builtin_abs", int_ftype_int, BUILT_IN_ABS, NULL_PTR);
732149f9
RK
3066 builtin_function ("__builtin_fabsf", float_ftype_float, BUILT_IN_FABS,
3067 NULL_PTR);
8d9bfdc5
RK
3068 builtin_function ("__builtin_fabs", double_ftype_double, BUILT_IN_FABS,
3069 NULL_PTR);
732149f9
RK
3070 builtin_function ("__builtin_fabsl", ldouble_ftype_ldouble, BUILT_IN_FABS,
3071 NULL_PTR);
8d9bfdc5
RK
3072 builtin_function ("__builtin_labs", long_ftype_long, BUILT_IN_LABS,
3073 NULL_PTR);
530fb43c
RK
3074 builtin_function ("__builtin_saveregs",
3075 build_function_type (ptr_type_node, NULL_TREE),
8d9bfdc5 3076 BUILT_IN_SAVEREGS, NULL_PTR);
51e29401
RS
3077/* EXPAND_BUILTIN_VARARGS is obsolete. */
3078#if 0
3079 builtin_function ("__builtin_varargs",
3080 build_function_type (ptr_type_node,
3081 tree_cons (NULL_TREE,
3082 integer_type_node,
3083 endlink)),
8d9bfdc5 3084 BUILT_IN_VARARGS, NULL_PTR);
51e29401
RS
3085#endif
3086 builtin_function ("__builtin_classify_type", default_function_type,
8d9bfdc5 3087 BUILT_IN_CLASSIFY_TYPE, NULL_PTR);
51e29401 3088 builtin_function ("__builtin_next_arg",
8d0aed47 3089 build_function_type (ptr_type_node, NULL_TREE),
8d9bfdc5 3090 BUILT_IN_NEXT_ARG, NULL_PTR);
51e29401
RS
3091 builtin_function ("__builtin_args_info",
3092 build_function_type (integer_type_node,
3093 tree_cons (NULL_TREE,
3094 integer_type_node,
3095 endlink)),
8d9bfdc5 3096 BUILT_IN_ARGS_INFO, NULL_PTR);
51e29401 3097
efb99317
TW
3098 /* Untyped call and return. */
3099 builtin_function ("__builtin_apply_args",
3100 build_function_type (ptr_type_node, NULL_TREE),
3101 BUILT_IN_APPLY_ARGS, NULL_PTR);
3102
3103 temp = tree_cons (NULL_TREE,
3104 build_pointer_type (build_function_type (void_type_node,
3105 NULL_TREE)),
3106 tree_cons (NULL_TREE,
3107 ptr_type_node,
3108 tree_cons (NULL_TREE,
e09d75bd 3109 sizetype,
efb99317
TW
3110 endlink)));
3111 builtin_function ("__builtin_apply",
3112 build_function_type (ptr_type_node, temp),
3113 BUILT_IN_APPLY, NULL_PTR);
3114 builtin_function ("__builtin_return",
3115 build_function_type (void_type_node,
3116 tree_cons (NULL_TREE,
3117 ptr_type_node,
3118 endlink)),
3119 BUILT_IN_RETURN, NULL_PTR);
3120
51e29401
RS
3121 /* Currently under experimentation. */
3122 builtin_function ("__builtin_memcpy", memcpy_ftype,
3123 BUILT_IN_MEMCPY, "memcpy");
3124 builtin_function ("__builtin_memcmp", int_ftype_cptr_cptr_sizet,
3125 BUILT_IN_MEMCMP, "memcmp");
3126 builtin_function ("__builtin_strcmp", int_ftype_string_string,
3127 BUILT_IN_STRCMP, "strcmp");
3128 builtin_function ("__builtin_strcpy", string_ftype_ptr_ptr,
3129 BUILT_IN_STRCPY, "strcpy");
f0a45d37 3130 builtin_function ("__builtin_strlen", strlen_ftype,
51e29401 3131 BUILT_IN_STRLEN, "strlen");
732149f9
RK
3132 builtin_function ("__builtin_sqrtf", float_ftype_float,
3133 BUILT_IN_FSQRT, "sqrtf");
929f3671
RS
3134 builtin_function ("__builtin_fsqrt", double_ftype_double,
3135 BUILT_IN_FSQRT, "sqrt");
732149f9
RK
3136 builtin_function ("__builtin_sqrtl", ldouble_ftype_ldouble,
3137 BUILT_IN_FSQRT, "sqrtl");
3138 builtin_function ("__builtin_sinf", float_ftype_float,
3139 BUILT_IN_SIN, "sinf");
96a1b3af
JVA
3140 builtin_function ("__builtin_sin", double_ftype_double,
3141 BUILT_IN_SIN, "sin");
732149f9
RK
3142 builtin_function ("__builtin_sinl", ldouble_ftype_ldouble,
3143 BUILT_IN_SIN, "sinl");
3144 builtin_function ("__builtin_cosf", float_ftype_float,
3145 BUILT_IN_COS, "cosf");
96a1b3af
JVA
3146 builtin_function ("__builtin_cos", double_ftype_double,
3147 BUILT_IN_COS, "cos");
732149f9
RK
3148 builtin_function ("__builtin_cosl", ldouble_ftype_ldouble,
3149 BUILT_IN_COS, "cosl");
96a1b3af 3150
51e29401
RS
3151 /* In an ANSI C program, it is okay to supply built-in meanings
3152 for these functions, since applications cannot validly use them
3153 with any other meaning.
9e3c9e1b
RS
3154 However, honor the -fno-builtin option. */
3155 if (!flag_no_builtin)
51e29401 3156 {
8d9bfdc5 3157 builtin_function ("abs", int_ftype_int, BUILT_IN_ABS, NULL_PTR);
732149f9 3158 builtin_function ("fabsf", float_ftype_float, BUILT_IN_FABS, NULL_PTR);
8d9bfdc5 3159 builtin_function ("fabs", double_ftype_double, BUILT_IN_FABS, NULL_PTR);
732149f9
RK
3160 builtin_function ("fabsl", ldouble_ftype_ldouble, BUILT_IN_FABS,
3161 NULL_PTR);
8d9bfdc5
RK
3162 builtin_function ("labs", long_ftype_long, BUILT_IN_LABS, NULL_PTR);
3163 builtin_function ("memcpy", memcpy_ftype, BUILT_IN_MEMCPY, NULL_PTR);
3164 builtin_function ("memcmp", int_ftype_cptr_cptr_sizet, BUILT_IN_MEMCMP,
3165 NULL_PTR);
3166 builtin_function ("strcmp", int_ftype_string_string, BUILT_IN_STRCMP,
3167 NULL_PTR);
3168 builtin_function ("strcpy", string_ftype_ptr_ptr, BUILT_IN_STRCPY,
3169 NULL_PTR);
3170 builtin_function ("strlen", strlen_ftype, BUILT_IN_STRLEN, NULL_PTR);
732149f9 3171 builtin_function ("sqrtf", float_ftype_float, BUILT_IN_FSQRT, NULL_PTR);
8d9bfdc5 3172 builtin_function ("sqrt", double_ftype_double, BUILT_IN_FSQRT, NULL_PTR);
732149f9
RK
3173 builtin_function ("sqrtl", ldouble_ftype_ldouble, BUILT_IN_FSQRT,
3174 NULL_PTR);
3175 builtin_function ("sinf", float_ftype_float, BUILT_IN_SIN, NULL_PTR);
96a1b3af 3176 builtin_function ("sin", double_ftype_double, BUILT_IN_SIN, NULL_PTR);
732149f9
RK
3177 builtin_function ("sinl", ldouble_ftype_ldouble, BUILT_IN_SIN, NULL_PTR);
3178 builtin_function ("cosf", float_ftype_float, BUILT_IN_COS, NULL_PTR);
96a1b3af 3179 builtin_function ("cos", double_ftype_double, BUILT_IN_COS, NULL_PTR);
732149f9 3180 builtin_function ("cosl", ldouble_ftype_ldouble, BUILT_IN_COS, NULL_PTR);
9e3c9e1b
RS
3181
3182 /* Declare these functions volatile
3183 to avoid spurious "control drops through" warnings. */
3184 /* Don't specify the argument types, to avoid errors
3185 from certain code which isn't valid in ANSI but which exists. */
8d9bfdc5
RK
3186 temp = builtin_function ("abort", void_ftype_any, NOT_BUILT_IN,
3187 NULL_PTR);
9e3c9e1b
RS
3188 TREE_THIS_VOLATILE (temp) = 1;
3189 TREE_SIDE_EFFECTS (temp) = 1;
8d9bfdc5 3190 temp = builtin_function ("exit", void_ftype_any, NOT_BUILT_IN, NULL_PTR);
9e3c9e1b
RS
3191 TREE_THIS_VOLATILE (temp) = 1;
3192 TREE_SIDE_EFFECTS (temp) = 1;
51e29401
RS
3193 }
3194
3195#if 0
3196 /* Support for these has not been written in either expand_builtin
3197 or build_function_call. */
8d9bfdc5
RK
3198 builtin_function ("__builtin_div", default_ftype, BUILT_IN_DIV, NULL_PTR);
3199 builtin_function ("__builtin_ldiv", default_ftype, BUILT_IN_LDIV, NULL_PTR);
3200 builtin_function ("__builtin_ffloor", double_ftype_double, BUILT_IN_FFLOOR,
3201 NULL_PTR);
3202 builtin_function ("__builtin_fceil", double_ftype_double, BUILT_IN_FCEIL,
3203 NULL_PTR);
3204 builtin_function ("__builtin_fmod", double_ftype_double_double,
3205 BUILT_IN_FMOD, NULL_PTR);
3206 builtin_function ("__builtin_frem", double_ftype_double_double,
3207 BUILT_IN_FREM, NULL_PTR);
3208 builtin_function ("__builtin_memset", ptr_ftype_ptr_int_int,
3209 BUILT_IN_MEMSET, NULL_PTR);
3210 builtin_function ("__builtin_getexp", double_ftype_double, BUILT_IN_GETEXP,
3211 NULL_PTR);
3212 builtin_function ("__builtin_getman", double_ftype_double, BUILT_IN_GETMAN,
3213 NULL_PTR);
51e29401
RS
3214#endif
3215
b032c74c 3216 /* Create the global bindings for __FUNCTION__ and __PRETTY_FUNCTION__. */
7da551a2 3217 declare_function_name ();
64309441 3218
51e29401
RS
3219 start_identifier_warnings ();
3220
561d994f
RK
3221 /* Prepare to check format strings against argument lists. */
3222 init_function_format_info ();
75621238
RS
3223
3224 init_iterators ();
b4892310
RS
3225
3226 incomplete_decl_finalize_hook = finish_incomplete_decl;
51e29401
RS
3227}
3228
3229/* Return a definition for a builtin function named NAME and whose data type
3230 is TYPE. TYPE should be a function type with argument types.
3231 FUNCTION_CODE tells later passes how to compile calls to this function.
3232 See tree.h for its possible values.
3233
3234 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
3235 the name to be called if we can't opencode the function. */
3236
929f3671 3237tree
51e29401
RS
3238builtin_function (name, type, function_code, library_name)
3239 char *name;
3240 tree type;
3241 enum built_in_function function_code;
3242 char *library_name;
3243{
3244 tree decl = build_decl (FUNCTION_DECL, get_identifier (name), type);
1394aabd 3245 DECL_EXTERNAL (decl) = 1;
51e29401 3246 TREE_PUBLIC (decl) = 1;
9a509bfe
RS
3247 /* If -traditional, permit redefining a builtin function any way you like.
3248 (Though really, if the program redefines these functions,
3249 it probably won't work right unless compiled with -fno-builtin.) */
3250 if (flag_traditional && name[0] != '_')
3251 DECL_BUILT_IN_NONANSI (decl) = 1;
51e29401
RS
3252 if (library_name)
3253 DECL_ASSEMBLER_NAME (decl) = get_identifier (library_name);
8d9bfdc5 3254 make_decl_rtl (decl, NULL_PTR, 1);
51e29401
RS
3255 pushdecl (decl);
3256 if (function_code != NOT_BUILT_IN)
3257 {
3258 DECL_BUILT_IN (decl) = 1;
678566a5 3259 DECL_FUNCTION_CODE (decl) = function_code;
51e29401 3260 }
6b19af32
RS
3261 /* Warn if a function in the namespace for users
3262 is used without an occasion to consider it declared. */
3263 if (name[0] != '_' || name[1] != '_')
3264 C_DECL_ANTICIPATED (decl) = 1;
51e29401
RS
3265
3266 return decl;
3267}
3268\f
3269/* Called when a declaration is seen that contains no names to declare.
3270 If its type is a reference to a structure, union or enum inherited
3271 from a containing scope, shadow that tag name for the current scope
3272 with a forward reference.
3273 If its type defines a new named structure or union
3274 or defines an enum, it is valid but we need not do anything here.
3275 Otherwise, it is an error. */
3276
3277void
3278shadow_tag (declspecs)
3279 tree declspecs;
9282f2f9
RS
3280{
3281 shadow_tag_warned (declspecs, 0);
3282}
3283
3284void
3285shadow_tag_warned (declspecs, warned)
3286 tree declspecs;
3287 int warned;
773edaef
RK
3288 /* 1 => we have done a pedwarn. 2 => we have done a warning, but
3289 no pedwarn. */
51e29401
RS
3290{
3291 int found_tag = 0;
51e29401
RS
3292 register tree link;
3293
3294 pending_invalid_xref = 0;
3295
3296 for (link = declspecs; link; link = TREE_CHAIN (link))
3297 {
3298 register tree value = TREE_VALUE (link);
3299 register enum tree_code code = TREE_CODE (value);
3300
3301 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
3302 /* Used to test also that TYPE_SIZE (value) != 0.
3303 That caused warning for `struct foo;' at top level in the file. */
3304 {
3305 register tree name = lookup_tag_reverse (value);
3306 register tree t;
3307
3308 found_tag++;
3309
3310 if (name == 0)
3311 {
773edaef
RK
3312 if (warned != 1 && code != ENUMERAL_TYPE)
3313 /* Empty unnamed enum OK */
51e29401
RS
3314 {
3315 pedwarn ("unnamed struct/union that defines no instances");
3316 warned = 1;
3317 }
3318 }
3319 else
3320 {
3321 t = lookup_tag (code, name, current_binding_level, 1);
3322
3323 if (t == 0)
3324 {
3325 t = make_node (code);
3326 pushtag (name, t);
3327 }
3328 }
3329 }
3330 else
3331 {
3332 if (!warned)
773edaef
RK
3333 {
3334 warning ("useless keyword or type name in empty declaration");
3335 warned = 2;
3336 }
51e29401
RS
3337 }
3338 }
3339
773edaef
RK
3340 if (found_tag > 1)
3341 error ("two types specified in one empty declaration");
3342
3343 if (warned != 1)
51e29401 3344 {
51e29401
RS
3345 if (found_tag == 0)
3346 pedwarn ("empty declaration");
3347 }
3348}
3349\f
3350/* Decode a "typename", such as "int **", returning a ..._TYPE node. */
3351
3352tree
3353groktypename (typename)
3354 tree typename;
3355{
3356 if (TREE_CODE (typename) != TREE_LIST)
3357 return typename;
3358 return grokdeclarator (TREE_VALUE (typename),
3359 TREE_PURPOSE (typename),
3360 TYPENAME, 0);
3361}
3362
3363/* Return a PARM_DECL node for a given pair of specs and declarator. */
3364
3365tree
3366groktypename_in_parm_context (typename)
3367 tree typename;
3368{
3369 if (TREE_CODE (typename) != TREE_LIST)
3370 return typename;
3371 return grokdeclarator (TREE_VALUE (typename),
3372 TREE_PURPOSE (typename),
3373 PARM, 0);
3374}
3375
3376/* Decode a declarator in an ordinary declaration or data definition.
3377 This is called as soon as the type information and variable name
3378 have been parsed, before parsing the initializer if any.
3379 Here we create the ..._DECL node, fill in its type,
3380 and put it on the list of decls for the current context.
3381 The ..._DECL node is returned as the value.
3382
3383 Exception: for arrays where the length is not specified,
3384 the type is left null, to be filled in by `finish_decl'.
3385
3386 Function definitions do not come here; they go to start_function
3387 instead. However, external and forward declarations of functions
3388 do go through here. Structure field declarations are done by
3389 grokfield and not through here. */
3390
3391/* Set this to zero to debug not using the temporary obstack
3392 to parse initializers. */
3393int debug_temp_inits = 1;
3394
3395tree
3396start_decl (declarator, declspecs, initialized)
eaa81144 3397 tree declarator, declspecs;
51e29401
RS
3398 int initialized;
3399{
3400 register tree decl = grokdeclarator (declarator, declspecs,
3401 NORMAL, initialized);
3402 register tree tem;
3403 int init_written = initialized;
3404
3405 /* The corresponding pop_obstacks is in finish_decl. */
3406 push_obstacks_nochange ();
3407
3408 if (initialized)
3409 /* Is it valid for this decl to have an initializer at all?
3410 If not, set INITIALIZED to zero, which will indirectly
3411 tell `finish_decl' to ignore the initializer once it is parsed. */
3412 switch (TREE_CODE (decl))
3413 {
3414 case TYPE_DECL:
3415 /* typedef foo = bar means give foo the same type as bar.
3416 We haven't parsed bar yet, so `finish_decl' will fix that up.
3417 Any other case of an initialization in a TYPE_DECL is an error. */
3418 if (pedantic || list_length (declspecs) > 1)
3419 {
3420 error ("typedef `%s' is initialized",
3421 IDENTIFIER_POINTER (DECL_NAME (decl)));
3422 initialized = 0;
3423 }
3424 break;
3425
3426 case FUNCTION_DECL:
3427 error ("function `%s' is initialized like a variable",
3428 IDENTIFIER_POINTER (DECL_NAME (decl)));
3429 initialized = 0;
3430 break;
3431
3432 case PARM_DECL:
3433 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
3434 error ("parameter `%s' is initialized",
3435 IDENTIFIER_POINTER (DECL_NAME (decl)));
3436 initialized = 0;
3437 break;
3438
3439 default:
3440 /* Don't allow initializations for incomplete types
3441 except for arrays which might be completed by the initialization. */
3442 if (TYPE_SIZE (TREE_TYPE (decl)) != 0)
3443 {
3444 /* A complete type is ok if size is fixed. */
3445
3446 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
3447 || C_DECL_VARIABLE_SIZE (decl))
3448 {
3449 error ("variable-sized object may not be initialized");
3450 initialized = 0;
3451 }
3452 }
3453 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
3454 {
3455 error ("variable `%s' has initializer but incomplete type",
3456 IDENTIFIER_POINTER (DECL_NAME (decl)));
3457 initialized = 0;
3458 }
3459 else if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))) == 0)
3460 {
3461 error ("elements of array `%s' have incomplete type",
3462 IDENTIFIER_POINTER (DECL_NAME (decl)));
3463 initialized = 0;
3464 }
3465 }
3466
3467 if (initialized)
3468 {
3469#if 0 /* Seems redundant with grokdeclarator. */
3470 if (current_binding_level != global_binding_level
1394aabd 3471 && DECL_EXTERNAL (decl)
51e29401
RS
3472 && TREE_CODE (decl) != FUNCTION_DECL)
3473 warning ("declaration of `%s' has `extern' and is initialized",
3474 IDENTIFIER_POINTER (DECL_NAME (decl)));
3475#endif
1394aabd 3476 DECL_EXTERNAL (decl) = 0;
51e29401
RS
3477 if (current_binding_level == global_binding_level)
3478 TREE_STATIC (decl) = 1;
3479
3480 /* Tell `pushdecl' this is an initialized decl
3481 even though we don't yet have the initializer expression.
3482 Also tell `finish_decl' it may store the real initializer. */
3483 DECL_INITIAL (decl) = error_mark_node;
3484 }
3485
3486 /* If this is a function declaration, write a record describing it to the
3487 prototypes file (if requested). */
3488
3489 if (TREE_CODE (decl) == FUNCTION_DECL)
3490 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
3491
3492 /* Add this decl to the current binding level.
3493 TEM may equal DECL or it may be a previous decl of the same name. */
3494 tem = pushdecl (decl);
3495
2c0d84d6
MS
3496 /* For C and Obective-C, we by default put things in .common when
3497 possible. */
3498 DECL_COMMON (tem) = 1;
3499
51e29401
RS
3500 /* For a local variable, define the RTL now. */
3501 if (current_binding_level != global_binding_level
3502 /* But not if this is a duplicate decl
3503 and we preserved the rtl from the previous one
3504 (which may or may not happen). */
3505 && DECL_RTL (tem) == 0)
3506 {
3507 if (TYPE_SIZE (TREE_TYPE (tem)) != 0)
3508 expand_decl (tem);
3509 else if (TREE_CODE (TREE_TYPE (tem)) == ARRAY_TYPE
3510 && DECL_INITIAL (tem) != 0)
3511 expand_decl (tem);
3512 }
3513
3514 if (init_written)
3515 {
3516 /* When parsing and digesting the initializer,
3517 use temporary storage. Do this even if we will ignore the value. */
3518 if (current_binding_level == global_binding_level && debug_temp_inits)
3519 temporary_allocation ();
3520 }
3521
3522 return tem;
3523}
3524
3525/* Finish processing of a declaration;
3526 install its initial value.
3527 If the length of an array type is not known before,
3528 it must be determined now, from the initial value, or it is an error. */
3529
3530void
3531finish_decl (decl, init, asmspec_tree)
3532 tree decl, init;
3533 tree asmspec_tree;
3534{
3535 register tree type = TREE_TYPE (decl);
3536 int was_incomplete = (DECL_SIZE (decl) == 0);
3537 int temporary = allocation_temporary_p ();
3538 char *asmspec = 0;
3539
72f5a12b 3540 /* If a name was specified, get the string. */
51e29401 3541 if (asmspec_tree)
72f5a12b 3542 asmspec = TREE_STRING_POINTER (asmspec_tree);
51e29401
RS
3543
3544 /* If `start_decl' didn't like having an initialization, ignore it now. */
3545
3546 if (init != 0 && DECL_INITIAL (decl) == 0)
3547 init = 0;
3548 /* Don't crash if parm is initialized. */
3549 if (TREE_CODE (decl) == PARM_DECL)
3550 init = 0;
3551
519d591f
RS
3552 if (ITERATOR_P (decl))
3553 {
3554 if (init == 0)
3555 error_with_decl (decl, "iterator has no initial value");
3556 else
3557 init = save_expr (init);
3558 }
3559
51e29401
RS
3560 if (init)
3561 {
3562 if (TREE_CODE (decl) != TYPE_DECL)
3563 store_init_value (decl, init);
3564 else
3565 {
3566 /* typedef foo = bar; store the type of bar as the type of foo. */
3567 TREE_TYPE (decl) = TREE_TYPE (init);
3568 DECL_INITIAL (decl) = init = 0;
3569 }
3570 }
3571
7a0347ff
RS
3572 /* Pop back to the obstack that is current for this binding level.
3573 This is because MAXINDEX, rtl, etc. to be made below
3574 must go in the permanent obstack. But don't discard the
51e29401 3575 temporary data yet. */
7a0347ff
RS
3576 pop_obstacks ();
3577#if 0 /* pop_obstacks was near the end; this is what was here. */
51e29401
RS
3578 if (current_binding_level == global_binding_level && temporary)
3579 end_temporary_allocation ();
7a0347ff 3580#endif
51e29401
RS
3581
3582 /* Deduce size of array from initialization, if not already known */
3583
3584 if (TREE_CODE (type) == ARRAY_TYPE
3585 && TYPE_DOMAIN (type) == 0
3586 && TREE_CODE (decl) != TYPE_DECL)
3587 {
3588 int do_default
3589 = (TREE_STATIC (decl)
3590 /* Even if pedantic, an external linkage array
3591 may have incomplete type at first. */
3592 ? pedantic && !TREE_PUBLIC (decl)
1394aabd 3593 : !DECL_EXTERNAL (decl));
51e29401
RS
3594 int failure
3595 = complete_array_type (type, DECL_INITIAL (decl), do_default);
3596
3597 /* Get the completed type made by complete_array_type. */
3598 type = TREE_TYPE (decl);
3599
3600 if (failure == 1)
3601 error_with_decl (decl, "initializer fails to determine size of `%s'");
3602
3603 if (failure == 2)
3604 {
3605 if (do_default)
3606 error_with_decl (decl, "array size missing in `%s'");
b4892310
RS
3607 /* If a `static' var's size isn't known,
3608 make it extern as well as static, so it does not get
3609 allocated.
3610 If it is not `static', then do not mark extern;
3611 finish_incomplete_decl will give it a default size
3612 and it will get allocated. */
3613 else if (!pedantic && TREE_STATIC (decl) && ! TREE_PUBLIC (decl))
1394aabd 3614 DECL_EXTERNAL (decl) = 1;
51e29401
RS
3615 }
3616
7e44eda6
JW
3617 /* TYPE_MAX_VALUE is always one less than the number of elements
3618 in the array, because we start counting at zero. Therefore,
3619 warn only if the value is less than zero. */
51e29401 3620 if (pedantic && TYPE_DOMAIN (type) != 0
7e44eda6 3621 && tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < 0)
6aa10371 3622 error_with_decl (decl, "zero or negative size array `%s'");
51e29401
RS
3623
3624 layout_decl (decl, 0);
3625 }
3626
3627 if (TREE_CODE (decl) == VAR_DECL)
3628 {
d575f110
RS
3629 if (DECL_SIZE (decl) == 0
3630 && TYPE_SIZE (TREE_TYPE (decl)) != 0)
3631 layout_decl (decl, 0);
3632
a7f64d52
RS
3633 if (DECL_SIZE (decl) == 0
3634 && (TREE_STATIC (decl)
3635 ?
3636 /* A static variable with an incomplete type
f3b4fb6e 3637 is an error if it is initialized.
70efc776 3638 Also if it is not file scope.
a7f64d52
RS
3639 Otherwise, let it through, but if it is not `extern'
3640 then it may cause an error message later. */
15317f89
JW
3641 /* We must use DECL_CONTEXT instead of current_binding_level,
3642 because a duplicate_decls call could have changed the binding
3643 level of this decl. */
3644 (DECL_INITIAL (decl) != 0 || DECL_CONTEXT (decl) != 0)
a7f64d52
RS
3645 :
3646 /* An automatic variable with an incomplete type
3647 is an error. */
70038ec9 3648 !DECL_EXTERNAL (decl)))
51e29401 3649 {
51e29401
RS
3650 error_with_decl (decl, "storage size of `%s' isn't known");
3651 TREE_TYPE (decl) = error_mark_node;
3652 }
3653
1394aabd 3654 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
90374cc2 3655 && DECL_SIZE (decl) != 0)
e681c5a1
RS
3656 {
3657 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
3658 constant_expression_warning (DECL_SIZE (decl));
3659 else
3660 error_with_decl (decl, "storage size of `%s' isn't constant");
3661 }
51e29401
RS
3662 }
3663
3c4afaa5 3664 /* If this is a function and an assembler name is specified, it isn't
72f5a12b
RK
3665 builtin any more. Also reset DECL_RTL so we can give it its new
3666 name. */
3c4afaa5 3667 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
72f5a12b
RK
3668 {
3669 DECL_BUILT_IN (decl) = 0;
3670 DECL_RTL (decl) = 0;
3671 }
3c4afaa5 3672
51e29401
RS
3673 /* Output the assembler code and/or RTL code for variables and functions,
3674 unless the type is an undefined structure or union.
3675 If not, it will get done when the type is completed. */
3676
3677 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3678 {
a98b1078
RK
3679 if ((flag_traditional || TREE_PERMANENT (decl))
3680 && allocation_temporary_p ())
51e29401
RS
3681 {
3682 push_obstacks_nochange ();
3683 end_temporary_allocation ();
3684 /* This is a no-op in c-lang.c or something real in objc-actions.c. */
3685 maybe_objc_check_decl (decl);
15317f89 3686 rest_of_decl_compilation (decl, asmspec, DECL_CONTEXT (decl) == 0,
51e29401
RS
3687 0);
3688 pop_obstacks ();
3689 }
3690 else
3691 {
3692 /* This is a no-op in c-lang.c or something real in objc-actions.c. */
3693 maybe_objc_check_decl (decl);
15317f89 3694 rest_of_decl_compilation (decl, asmspec, DECL_CONTEXT (decl) == 0,
51e29401
RS
3695 0);
3696 }
15317f89 3697 if (DECL_CONTEXT (decl) != 0)
51e29401
RS
3698 {
3699 /* Recompute the RTL of a local array now
3700 if it used to be an incomplete type. */
3701 if (was_incomplete
1394aabd 3702 && ! TREE_STATIC (decl) && ! DECL_EXTERNAL (decl))
51e29401
RS
3703 {
3704 /* If we used it already as memory, it must stay in memory. */
3705 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3706 /* If it's still incomplete now, no init will save it. */
3707 if (DECL_SIZE (decl) == 0)
3708 DECL_INITIAL (decl) = 0;
3709 expand_decl (decl);
3710 }
3711 /* Compute and store the initial value. */
42dfa47f
RS
3712 if (TREE_CODE (decl) != FUNCTION_DECL)
3713 expand_decl_init (decl);
51e29401
RS
3714 }
3715 }
3716
3717 if (TREE_CODE (decl) == TYPE_DECL)
3718 {
3719 /* This is a no-op in c-lang.c or something real in objc-actions.c. */
3720 maybe_objc_check_decl (decl);
15317f89 3721 rest_of_decl_compilation (decl, NULL_PTR, DECL_CONTEXT (decl) == 0,
51e29401
RS
3722 0);
3723 }
3724
14f3e886 3725 /* ??? After 2.3, test (init != 0) instead of TREE_CODE. */
3e755d23
JW
3726 /* This test used to include TREE_PERMANENT, however, we have the same
3727 problem with initializers at the function level. Such initializers get
3728 saved until the end of the function on the momentary_obstack. */
14f3e886 3729 if (!(TREE_CODE (decl) == FUNCTION_DECL && DECL_INLINE (decl))
3e755d23 3730 && temporary
eda115dd
RS
3731 /* DECL_INITIAL is not defined in PARM_DECLs, since it shares
3732 space with DECL_ARG_TYPE. */
3733 && TREE_CODE (decl) != PARM_DECL)
51e29401
RS
3734 {
3735 /* We need to remember that this array HAD an initialization,
3736 but discard the actual temporary nodes,
3737 since we can't have a permanent node keep pointing to them. */
14f3e886
RS
3738 /* We make an exception for inline functions, since it's
3739 normal for a local extern redeclaration of an inline function
3740 to have a copy of the top-level decl's DECL_INLINE. */
db57d413 3741 if (DECL_INITIAL (decl) != 0 && DECL_INITIAL (decl) != error_mark_node)
549a367a 3742 {
3e755d23 3743 /* If this is a const variable, then preserve the
549a367a
JW
3744 initializer instead of discarding it so that we can optimize
3745 references to it. */
3e755d23
JW
3746 /* This test used to include TREE_STATIC, but this won't be set
3747 for function level initializers. */
3861b613 3748 if (TREE_READONLY (decl) || ITERATOR_P (decl))
b4d4e33d
RS
3749 {
3750 preserve_initializer ();
3751 /* Hack? Set the permanent bit for something that is permanent,
3752 but not on the permenent obstack, so as to convince
3753 output_constant_def to make its rtl on the permanent
3754 obstack. */
3755 TREE_PERMANENT (DECL_INITIAL (decl)) = 1;
7d49f92a
RK
3756
3757 /* The initializer and DECL must have the same (or equivalent
3758 types), but if the initializer is a STRING_CST, its type
3759 might not be on the right obstack, so copy the type
3760 of DECL. */
3761 TREE_TYPE (DECL_INITIAL (decl)) = type;
b4d4e33d 3762 }
549a367a
JW
3763 else
3764 DECL_INITIAL (decl) = error_mark_node;
3765 }
51e29401
RS
3766 }
3767
739d15ab
RK
3768 /* If requested, warn about definitions of large data objects. */
3769
3770 if (warn_larger_than
3771 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL)
3772 && !DECL_EXTERNAL (decl))
3773 {
3774 register tree decl_size = DECL_SIZE (decl);
3775
3776 if (decl_size && TREE_CODE (decl_size) == INTEGER_CST)
3777 {
3778 unsigned units = TREE_INT_CST_LOW(decl_size) / BITS_PER_UNIT;
3779
3780 if (units > larger_than_size)
3781 warning_with_decl (decl, "size of `%s' is %u bytes", units);
3782 }
3783 }
3784
7a0347ff 3785#if 0
51e29401
RS
3786 /* Resume permanent allocation, if not within a function. */
3787 /* The corresponding push_obstacks_nochange is in start_decl,
3788 and in push_parm_decl and in grokfield. */
3789 pop_obstacks ();
7a0347ff
RS
3790#endif
3791
3792 /* If we have gone back from temporary to permanent allocation,
3793 actually free the temporary space that we no longer need. */
3794 if (temporary && !allocation_temporary_p ())
3e755d23 3795 permanent_allocation (0);
51e29401
RS
3796
3797 /* At the end of a declaration, throw away any variable type sizes
3798 of types defined inside that declaration. There is no use
3799 computing them in the following function definition. */
3800 if (current_binding_level == global_binding_level)
3801 get_pending_sizes ();
3802}
3803
3804/* If DECL has a cleanup, build and return that cleanup here.
3805 This is a callback called by expand_expr. */
3806
3807tree
3808maybe_build_cleanup (decl)
3809 tree decl;
3810{
3811 /* There are no cleanups in C. */
3812 return NULL_TREE;
3813}
3814
3815/* Given a parsed parameter declaration,
3816 decode it into a PARM_DECL and push that on the current binding level.
3817 Also, for the sake of forward parm decls,
3818 record the given order of parms in `parm_order'. */
3819
3820void
3821push_parm_decl (parm)
3822 tree parm;
3823{
6cc902a1 3824 tree decl;
929f3671
RS
3825 int old_immediate_size_expand = immediate_size_expand;
3826 /* Don't try computing parm sizes now -- wait till fn is called. */
3827 immediate_size_expand = 0;
51e29401
RS
3828
3829 /* The corresponding pop_obstacks is in finish_decl. */
3830 push_obstacks_nochange ();
3831
3832 decl = grokdeclarator (TREE_VALUE (parm), TREE_PURPOSE (parm), PARM, 0);
6a5ed5bf
RK
3833
3834#if 0
93e3ba4f
RS
3835 if (DECL_NAME (decl))
3836 {
6cc902a1 3837 tree olddecl;
93e3ba4f
RS
3838 olddecl = lookup_name (DECL_NAME (decl));
3839 if (pedantic && olddecl != 0 && TREE_CODE (olddecl) == TYPE_DECL)
3840 pedwarn_with_decl (decl, "ANSI C forbids parameter `%s' shadowing typedef");
3841 }
6a5ed5bf
RK
3842#endif
3843
51e29401
RS
3844 decl = pushdecl (decl);
3845
929f3671
RS
3846 immediate_size_expand = old_immediate_size_expand;
3847
51e29401
RS
3848 current_binding_level->parm_order
3849 = tree_cons (NULL_TREE, decl, current_binding_level->parm_order);
3850
3851 /* Add this decl to the current binding level. */
3852 finish_decl (decl, NULL_TREE, NULL_TREE);
3853}
3854
3855/* Clear the given order of parms in `parm_order'.
3856 Used at start of parm list,
3857 and also at semicolon terminating forward decls. */
3858
3859void
3860clear_parm_order ()
3861{
3862 current_binding_level->parm_order = NULL_TREE;
3863}
3864\f
3865/* Make TYPE a complete type based on INITIAL_VALUE.
929f3671 3866 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
51e29401
RS
3867 2 if there was no information (in which case assume 1 if DO_DEFAULT). */
3868
3869int
3870complete_array_type (type, initial_value, do_default)
3871 tree type;
3872 tree initial_value;
3873 int do_default;
3874{
3875 register tree maxindex = NULL_TREE;
3876 int value = 0;
3877
3878 if (initial_value)
3879 {
3880 /* Note MAXINDEX is really the maximum index,
3881 one less than the size. */
3882 if (TREE_CODE (initial_value) == STRING_CST)
3883 {
3884 int eltsize
3885 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
20bf3fac
RS
3886 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
3887 / eltsize) - 1, 0);
51e29401
RS
3888 }
3889 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
3890 {
ecd4cee0 3891 tree elts = CONSTRUCTOR_ELTS (initial_value);
20bf3fac 3892 maxindex = size_binop (MINUS_EXPR, integer_zero_node, size_one_node);
ecd4cee0
RS
3893 for (; elts; elts = TREE_CHAIN (elts))
3894 {
3895 if (TREE_PURPOSE (elts))
3896 maxindex = TREE_PURPOSE (elts);
3897 else
3898 maxindex = size_binop (PLUS_EXPR, maxindex, size_one_node);
3899 }
3900 maxindex = copy_node (maxindex);
51e29401
RS
3901 }
3902 else
3903 {
3904 /* Make an error message unless that happened already. */
3905 if (initial_value != error_mark_node)
3906 value = 1;
3907
3908 /* Prevent further error messages. */
b4892310 3909 maxindex = build_int_2 (0, 0);
51e29401
RS
3910 }
3911 }
3912
3913 if (!maxindex)
3914 {
3915 if (do_default)
b4892310 3916 maxindex = build_int_2 (0, 0);
51e29401
RS
3917 value = 2;
3918 }
3919
3920 if (maxindex)
3921 {
3922 TYPE_DOMAIN (type) = build_index_type (maxindex);
3923 if (!TREE_TYPE (maxindex))
3924 TREE_TYPE (maxindex) = TYPE_DOMAIN (type);
f4e59008
RS
3925#if 0 /* I took out this change
3926 together with the change in build_array_type. --rms */
e1fe03e9
RS
3927 change_main_variant (type,
3928 build_array_type (TREE_TYPE (type),
3929 TYPE_DOMAIN (type)));
f4e59008 3930#endif
51e29401
RS
3931 }
3932
3933 /* Lay out the type now that we can get the real answer. */
3934
3935 layout_type (type);
3936
3937 return value;
3938}
3939\f
3940/* Given declspecs and a declarator,
3941 determine the name and type of the object declared
3942 and construct a ..._DECL node for it.
3943 (In one case we can return a ..._TYPE node instead.
3944 For invalid input we sometimes return 0.)
3945
3946 DECLSPECS is a chain of tree_list nodes whose value fields
3947 are the storage classes and type specifiers.
3948
3949 DECL_CONTEXT says which syntactic context this declaration is in:
3950 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3951 FUNCDEF for a function definition. Like NORMAL but a few different
3952 error messages in each case. Return value may be zero meaning
3953 this definition is too screwy to try to parse.
3954 PARM for a parameter declaration (either within a function prototype
3955 or before a function body). Make a PARM_DECL, or return void_type_node.
3956 TYPENAME if for a typename (in a cast or sizeof).
3957 Don't make a DECL node; just return the ..._TYPE node.
3958 FIELD for a struct or union field; make a FIELD_DECL.
3959 BITFIELD for a field with specified width.
3960 INITIALIZED is 1 if the decl has an initializer.
3961
3962 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3963 It may also be so in the PARM case, for a prototype where the
3964 argument type is specified but not the name.
3965
3966 This function is where the complicated C meanings of `static'
929f3671 3967 and `extern' are interpreted. */
51e29401
RS
3968
3969static tree
3970grokdeclarator (declarator, declspecs, decl_context, initialized)
3971 tree declspecs;
3972 tree declarator;
3973 enum decl_context decl_context;
3974 int initialized;
3975{
3976 int specbits = 0;
3977 tree spec;
3978 tree type = NULL_TREE;
3979 int longlong = 0;
3980 int constp;
3981 int volatilep;
3982 int inlinep;
3983 int explicit_int = 0;
3984 int explicit_char = 0;
5ab10c42 3985 int defaulted_int = 0;
51e29401
RS
3986 tree typedef_decl = 0;
3987 char *name;
3988 tree typedef_type = 0;
3989 int funcdef_flag = 0;
3990 enum tree_code innermost_code = ERROR_MARK;
3991 int bitfield = 0;
929f3671 3992 int size_varies = 0;
51e29401
RS
3993
3994 if (decl_context == BITFIELD)
3995 bitfield = 1, decl_context = FIELD;
3996
3997 if (decl_context == FUNCDEF)
3998 funcdef_flag = 1, decl_context = NORMAL;
3999
4000 push_obstacks_nochange ();
4001
4002 if (flag_traditional && allocation_temporary_p ())
4003 end_temporary_allocation ();
4004
4005 /* Look inside a declarator for the name being declared
4006 and get it as a string, for an error message. */
4007 {
4008 register tree decl = declarator;
4009 name = 0;
4010
4011 while (decl)
4012 switch (TREE_CODE (decl))
4013 {
4014 case ARRAY_REF:
4015 case INDIRECT_REF:
4016 case CALL_EXPR:
4017 innermost_code = TREE_CODE (decl);
4018 decl = TREE_OPERAND (decl, 0);
4019 break;
4020
4021 case IDENTIFIER_NODE:
4022 name = IDENTIFIER_POINTER (decl);
4023 decl = 0;
4024 break;
4025
4026 default:
4027 abort ();
4028 }
4029 if (name == 0)
4030 name = "type name";
4031 }
4032
4033 /* A function definition's declarator must have the form of
4034 a function declarator. */
4035
4036 if (funcdef_flag && innermost_code != CALL_EXPR)
4037 return 0;
4038
4039 /* Anything declared one level down from the top level
4040 must be one of the parameters of a function
4041 (because the body is at least two levels down). */
4042
4043 /* If this looks like a function definition, make it one,
4044 even if it occurs where parms are expected.
4045 Then store_parm_decls will reject it and not use it as a parm. */
4046 if (decl_context == NORMAL && !funcdef_flag
4047 && current_binding_level->level_chain == global_binding_level)
4048 decl_context = PARM;
4049
4050 /* Look through the decl specs and record which ones appear.
4051 Some typespecs are defined as built-in typenames.
4052 Others, the ones that are modifiers of other types,
4053 are represented by bits in SPECBITS: set the bits for
4054 the modifiers that appear. Storage class keywords are also in SPECBITS.
4055
4056 If there is a typedef name or a type, store the type in TYPE.
4057 This includes builtin typedefs such as `int'.
4058
4059 Set EXPLICIT_INT or EXPLICIT_CHAR if the type is `int' or `char'
4060 and did not come from a user typedef.
4061
4062 Set LONGLONG if `long' is mentioned twice. */
4063
4064 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
4065 {
4066 register int i;
4067 register tree id = TREE_VALUE (spec);
4068
4069 if (id == ridpointers[(int) RID_INT])
4070 explicit_int = 1;
4071 if (id == ridpointers[(int) RID_CHAR])
4072 explicit_char = 1;
4073
4074 if (TREE_CODE (id) == IDENTIFIER_NODE)
4075 for (i = (int) RID_FIRST_MODIFIER; i < (int) RID_MAX; i++)
4076 {
4077 if (ridpointers[i] == id)
4078 {
4079 if (i == (int) RID_LONG && specbits & (1<<i))
4080 {
89d7540d 4081 if (longlong)
47429a02 4082 error ("`long long long' is too long for GCC");
51e29401 4083 else
89d7540d 4084 {
5db1a3c4 4085 if (pedantic && ! in_system_header)
89d7540d
RS
4086 pedwarn ("ANSI C does not support `long long'");
4087 longlong = 1;
4088 }
51e29401
RS
4089 }
4090 else if (specbits & (1 << i))
93e3ba4f 4091 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
51e29401
RS
4092 specbits |= 1 << i;
4093 goto found;
4094 }
4095 }
4096 if (type)
4097 error ("two or more data types in declaration of `%s'", name);
4098 /* Actual typedefs come to us as TYPE_DECL nodes. */
4099 else if (TREE_CODE (id) == TYPE_DECL)
4100 {
4101 type = TREE_TYPE (id);
4102 typedef_decl = id;
4103 }
4104 /* Built-in types come as identifiers. */
4105 else if (TREE_CODE (id) == IDENTIFIER_NODE)
4106 {
4107 register tree t = lookup_name (id);
4108 if (TREE_TYPE (t) == error_mark_node)
4109 ;
4110 else if (!t || TREE_CODE (t) != TYPE_DECL)
4111 error ("`%s' fails to be a typedef or built in type",
4112 IDENTIFIER_POINTER (id));
4113 else
4114 {
4115 type = TREE_TYPE (t);
4116 typedef_decl = t;
4117 }
4118 }
4119 else if (TREE_CODE (id) != ERROR_MARK)
4120 type = id;
4121
4122 found: {}
4123 }
4124
4125 typedef_type = type;
4126 if (type)
929f3671 4127 size_varies = C_TYPE_VARIABLE_SIZE (type);
51e29401 4128
5ab10c42 4129 /* No type at all: default to `int', and set DEFAULTED_INT
51e29401
RS
4130 because it was not a user-defined typedef. */
4131
4132 if (type == 0)
4133 {
4134 if (funcdef_flag && warn_return_type
4135 && ! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
4136 | (1 << (int) RID_SIGNED) | (1 << (int) RID_UNSIGNED))))
4137 warn_about_return_type = 1;
5ab10c42 4138 defaulted_int = 1;
51e29401
RS
4139 type = integer_type_node;
4140 }
4141
4142 /* Now process the modifiers that were specified
4143 and check for invalid combinations. */
4144
4145 /* Long double is a special combination. */
4146
90d56da8
RS
4147 if ((specbits & 1 << (int) RID_LONG)
4148 && TYPE_MAIN_VARIANT (type) == double_type_node)
51e29401
RS
4149 {
4150 specbits &= ~ (1 << (int) RID_LONG);
4151 type = long_double_type_node;
4152 }
4153
4154 /* Check all other uses of type modifiers. */
4155
4156 if (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
4157 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED)))
4158 {
4159 int ok = 0;
4160
4161 if (TREE_CODE (type) != INTEGER_TYPE)
4162 error ("long, short, signed or unsigned invalid for `%s'", name);
4163 else if ((specbits & 1 << (int) RID_LONG)
4164 && (specbits & 1 << (int) RID_SHORT))
4165 error ("long and short specified together for `%s'", name);
4166 else if (((specbits & 1 << (int) RID_LONG)
4167 || (specbits & 1 << (int) RID_SHORT))
4168 && explicit_char)
4169 error ("long or short specified with char for `%s'", name);
4170 else if (((specbits & 1 << (int) RID_LONG)
4171 || (specbits & 1 << (int) RID_SHORT))
4172 && TREE_CODE (type) == REAL_TYPE)
4173 error ("long or short specified with floating type for `%s'", name);
4174 else if ((specbits & 1 << (int) RID_SIGNED)
4175 && (specbits & 1 << (int) RID_UNSIGNED))
4176 error ("signed and unsigned given together for `%s'", name);
4177 else
4178 {
4179 ok = 1;
5ab10c42 4180 if (!explicit_int && !defaulted_int && !explicit_char && pedantic)
51e29401
RS
4181 {
4182 pedwarn ("long, short, signed or unsigned used invalidly for `%s'",
4183 name);
4184 if (flag_pedantic_errors)
4185 ok = 0;
4186 }
4187 }
4188
4189 /* Discard the type modifiers if they are invalid. */
4190 if (! ok)
4191 {
4192 specbits &= ~((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
4193 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED));
4194 longlong = 0;
4195 }
4196 }
4197
c470260b
RK
4198 if ((specbits & (1 << (int) RID_COMPLEX))
4199 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
4200 {
4201 error ("complex invalid for `%s'", name);
4202 specbits &= ~ (1 << (int) RID_COMPLEX);
4203 }
4204
51e29401
RS
4205 /* Decide whether an integer type is signed or not.
4206 Optionally treat bitfields as signed by default. */
4207 if (specbits & 1 << (int) RID_UNSIGNED
4208 /* Traditionally, all bitfields are unsigned. */
7a0347ff
RS
4209 || (bitfield && flag_traditional
4210 && (! explicit_flag_signed_bitfields || !flag_signed_bitfields))
51e29401 4211 || (bitfield && ! flag_signed_bitfields
5ab10c42 4212 && (explicit_int || defaulted_int || explicit_char
51e29401
RS
4213 /* A typedef for plain `int' without `signed'
4214 can be controlled just like plain `int'. */
4215 || ! (typedef_decl != 0
4216 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
4217 && TREE_CODE (type) != ENUMERAL_TYPE
4218 && !(specbits & 1 << (int) RID_SIGNED)))
4219 {
4220 if (longlong)
4221 type = long_long_unsigned_type_node;
4222 else if (specbits & 1 << (int) RID_LONG)
4223 type = long_unsigned_type_node;
4224 else if (specbits & 1 << (int) RID_SHORT)
4225 type = short_unsigned_type_node;
4226 else if (type == char_type_node)
4227 type = unsigned_char_type_node;
4228 else if (typedef_decl)
4229 type = unsigned_type (type);
4230 else
4231 type = unsigned_type_node;
4232 }
4233 else if ((specbits & 1 << (int) RID_SIGNED)
4234 && type == char_type_node)
4235 type = signed_char_type_node;
4236 else if (longlong)
4237 type = long_long_integer_type_node;
4238 else if (specbits & 1 << (int) RID_LONG)
4239 type = long_integer_type_node;
4240 else if (specbits & 1 << (int) RID_SHORT)
4241 type = short_integer_type_node;
c470260b
RK
4242
4243 if (specbits & 1 << (int) RID_COMPLEX)
5ab10c42 4244 {
c470260b
RK
4245 /* If we just have "complex", it is equivalent to
4246 "complex double", but if any modifiers at all are specified it is
4247 the complex form of TYPE. E.g, "complex short" is
4248 "complex short int". */
4249
4250 if (defaulted_int && ! longlong
4251 && ! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
4252 | (1 << (int) RID_SIGNED)
4253 | (1 << (int) RID_UNSIGNED))))
5ab10c42
RS
4254 type = complex_double_type_node;
4255 else if (type == integer_type_node)
4256 type = complex_integer_type_node;
4257 else if (type == float_type_node)
4258 type = complex_float_type_node;
4259 else if (type == double_type_node)
4260 type = complex_double_type_node;
4261 else if (type == long_double_type_node)
4262 type = complex_long_double_type_node;
4263 else
c470260b 4264 type = build_complex_type (type);
5ab10c42 4265 }
51e29401
RS
4266
4267 /* Set CONSTP if this declaration is `const', whether by
4268 explicit specification or via a typedef.
4269 Likewise for VOLATILEP. */
4270
4271 constp = !! (specbits & 1 << (int) RID_CONST) + TYPE_READONLY (type);
4272 volatilep = !! (specbits & 1 << (int) RID_VOLATILE) + TYPE_VOLATILE (type);
4273 inlinep = !! (specbits & (1 << (int) RID_INLINE));
4274 if (constp > 1)
93e3ba4f 4275 pedwarn ("duplicate `const'");
51e29401 4276 if (volatilep > 1)
93e3ba4f 4277 pedwarn ("duplicate `volatile'");
51e29401
RS
4278 if (! flag_gen_aux_info && (TYPE_READONLY (type) || TYPE_VOLATILE (type)))
4279 type = TYPE_MAIN_VARIANT (type);
4280
4281 /* Warn if two storage classes are given. Default to `auto'. */
4282
4283 {
4284 int nclasses = 0;
4285
4286 if (specbits & 1 << (int) RID_AUTO) nclasses++;
4287 if (specbits & 1 << (int) RID_STATIC) nclasses++;
4288 if (specbits & 1 << (int) RID_EXTERN) nclasses++;
4289 if (specbits & 1 << (int) RID_REGISTER) nclasses++;
4290 if (specbits & 1 << (int) RID_TYPEDEF) nclasses++;
519d591f 4291 if (specbits & 1 << (int) RID_ITERATOR) nclasses++;
51e29401
RS
4292
4293 /* Warn about storage classes that are invalid for certain
4294 kinds of declarations (parameters, typenames, etc.). */
4295
4296 if (nclasses > 1)
4297 error ("multiple storage classes in declaration of `%s'", name);
4298 else if (funcdef_flag
4299 && (specbits
4300 & ((1 << (int) RID_REGISTER)
4301 | (1 << (int) RID_AUTO)
4302 | (1 << (int) RID_TYPEDEF))))
4303 {
4304 if (specbits & 1 << (int) RID_AUTO
4305 && (pedantic || current_binding_level == global_binding_level))
4306 pedwarn ("function definition declared `auto'");
4307 if (specbits & 1 << (int) RID_REGISTER)
4308 error ("function definition declared `register'");
4309 if (specbits & 1 << (int) RID_TYPEDEF)
4310 error ("function definition declared `typedef'");
4311 specbits &= ~ ((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
4312 | (1 << (int) RID_AUTO));
4313 }
4314 else if (decl_context != NORMAL && nclasses > 0)
4315 {
4316 if (decl_context == PARM && specbits & 1 << (int) RID_REGISTER)
4317 ;
4318 else
4319 {
4320 error ((decl_context == FIELD
4321 ? "storage class specified for structure field `%s'"
4322 : (decl_context == PARM
4323 ? "storage class specified for parameter `%s'"
4324 : "storage class specified for typename")),
4325 name);
4326 specbits &= ~ ((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
4327 | (1 << (int) RID_AUTO) | (1 << (int) RID_STATIC)
4328 | (1 << (int) RID_EXTERN));
4329 }
4330 }
4331 else if (specbits & 1 << (int) RID_EXTERN && initialized && ! funcdef_flag)
4332 {
4333 /* `extern' with initialization is invalid if not at top level. */
4334 if (current_binding_level == global_binding_level)
4335 warning ("`%s' initialized and declared `extern'", name);
4336 else
4337 error ("`%s' has both `extern' and initializer", name);
4338 }
4339 else if (specbits & 1 << (int) RID_EXTERN && funcdef_flag
4340 && current_binding_level != global_binding_level)
4341 error ("nested function `%s' declared `extern'", name);
4342 else if (current_binding_level == global_binding_level
4343 && specbits & (1 << (int) RID_AUTO))
4344 error ("top-level declaration of `%s' specifies `auto'", name);
519d591f
RS
4345 else if ((specbits & 1 << (int) RID_ITERATOR)
4346 && TREE_CODE (declarator) != IDENTIFIER_NODE)
4347 {
4348 error ("iterator `%s' has derived type", name);
4349 type = error_mark_node;
4350 }
4351 else if ((specbits & 1 << (int) RID_ITERATOR)
4352 && TREE_CODE (type) != INTEGER_TYPE)
4353 {
4354 error ("iterator `%s' has noninteger type", name);
4355 type = error_mark_node;
4356 }
51e29401
RS
4357 }
4358
4359 /* Now figure out the structure of the declarator proper.
4360 Descend through it, creating more complex types, until we reach
4361 the declared identifier (or NULL_TREE, in an absolute declarator). */
4362
4363 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE)
4364 {
4365 if (type == error_mark_node)
4366 {
4367 declarator = TREE_OPERAND (declarator, 0);
4368 continue;
4369 }
4370
4371 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
4372 an INDIRECT_REF (for *...),
4373 a CALL_EXPR (for ...(...)),
4374 an identifier (for the name being declared)
4375 or a null pointer (for the place in an absolute declarator
4376 where the name was omitted).
4377 For the last two cases, we have just exited the loop.
4378
4379 At this point, TYPE is the type of elements of an array,
4380 or for a function to return, or for a pointer to point to.
4381 After this sequence of ifs, TYPE is the type of the
4382 array or function or pointer, and DECLARATOR has had its
4383 outermost layer removed. */
4384
4385 if (TREE_CODE (declarator) == ARRAY_REF)
4386 {
4387 register tree itype = NULL_TREE;
4388 register tree size = TREE_OPERAND (declarator, 1);
e0e2f469
JW
4389 /* An uninitialized decl with `extern' is a reference. */
4390 int extern_ref = !initialized && (specbits & (1 << (int) RID_EXTERN));
1ff1a2d2
RK
4391 /* The index is a signed object `sizetype' bits wide. */
4392 tree index_type = signed_type (sizetype);
51e29401
RS
4393
4394 declarator = TREE_OPERAND (declarator, 0);
4395
4396 /* Check for some types that there cannot be arrays of. */
4397
5fe86b8b 4398 if (TYPE_MAIN_VARIANT (type) == void_type_node)
51e29401
RS
4399 {
4400 error ("declaration of `%s' as array of voids", name);
4401 type = error_mark_node;
4402 }
4403
4404 if (TREE_CODE (type) == FUNCTION_TYPE)
4405 {
4406 error ("declaration of `%s' as array of functions", name);
4407 type = error_mark_node;
4408 }
4409
4410 if (size == error_mark_node)
4411 type = error_mark_node;
4412
4413 if (type == error_mark_node)
4414 continue;
4415
e0e2f469
JW
4416 /* If this is a block level extern, it must live past the end
4417 of the function so that we can check it against other extern
4418 declarations (IDENTIFIER_LIMBO_VALUE). */
4419 if (extern_ref && allocation_temporary_p ())
4420 end_temporary_allocation ();
4421
51e29401
RS
4422 /* If size was specified, set ITYPE to a range-type for that size.
4423 Otherwise, ITYPE remains null. finish_decl may figure it out
4424 from an initial value. */
4425
4426 if (size)
4427 {
4428 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
874a7be1 4429 STRIP_TYPE_NOPS (size);
51e29401
RS
4430
4431 if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE
4432 && TREE_CODE (TREE_TYPE (size)) != ENUMERAL_TYPE)
4433 {
4434 error ("size of array `%s' has non-integer type", name);
4435 size = integer_one_node;
4436 }
0a43d680 4437
51e29401
RS
4438 if (pedantic && integer_zerop (size))
4439 pedwarn ("ANSI C forbids zero-size array `%s'", name);
0a43d680 4440
51e29401
RS
4441 if (TREE_CODE (size) == INTEGER_CST)
4442 {
90374cc2 4443 constant_expression_warning (size);
0a43d680 4444 if (tree_int_cst_sgn (size) < 0)
51e29401
RS
4445 {
4446 error ("size of array `%s' is negative", name);
4447 size = integer_one_node;
4448 }
51e29401
RS
4449 }
4450 else
4451 {
0a43d680
RK
4452 /* Make sure the array size remains visibly nonconstant
4453 even if it is (eg) a const variable with known value. */
4454 size_varies = 1;
4455
51e29401 4456 if (pedantic)
1a6603da
RS
4457 {
4458 if (TREE_CONSTANT (size))
4459 pedwarn ("ANSI C forbids array `%s' whose size can't be evaluated", name);
4460 else
4461 pedwarn ("ANSI C forbids variable-size array `%s'", name);
4462 }
51e29401 4463 }
0a43d680 4464
1ff1a2d2 4465 /* Convert size to index_type, so that if it is a variable
0a43d680 4466 the computations will be done in the proper mode. */
1ff1a2d2
RK
4467 itype = fold (build (MINUS_EXPR, index_type,
4468 convert (index_type, size),
4469 convert (index_type, size_one_node)));
0a43d680
RK
4470
4471 if (size_varies)
4472 itype = variable_size (itype);
4473 itype = build_index_type (itype);
51e29401
RS
4474 }
4475
4476#if 0 /* This had bad results for pointers to arrays, as in
4477 union incomplete (*foo)[4]; */
4478 /* Complain about arrays of incomplete types, except in typedefs. */
4479
4480 if (TYPE_SIZE (type) == 0
4481 /* Avoid multiple warnings for nested array types. */
4482 && TREE_CODE (type) != ARRAY_TYPE
4483 && !(specbits & (1 << (int) RID_TYPEDEF))
4484 && !C_TYPE_BEING_DEFINED (type))
4485 warning ("array type has incomplete element type");
4486#endif
4487
51e29401
RS
4488#if 0 /* We shouldn't have a function type here at all!
4489 Functions aren't allowed as array elements. */
4490 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4491 && (constp || volatilep))
4492 pedwarn ("ANSI C forbids const or volatile function types");
4493#endif
50e65854
JW
4494
4495 /* Build the array type itself, then merge any constancy or
4496 volatility into the target type. We must do it in this order
4497 to ensure that the TYPE_MAIN_VARIANT field of the array type
4498 is set correctly. */
4499
4500 type = build_array_type (type, itype);
51e29401
RS
4501 if (constp || volatilep)
4502 type = c_build_type_variant (type, constp, volatilep);
4503
4504#if 0 /* don't clear these; leave them set so that the array type
4505 or the variable is itself const or volatile. */
4506 constp = 0;
4507 volatilep = 0;
4508#endif
4509
929f3671 4510 if (size_varies)
51e29401
RS
4511 C_TYPE_VARIABLE_SIZE (type) = 1;
4512 }
4513 else if (TREE_CODE (declarator) == CALL_EXPR)
4514 {
fd0b8fce
JW
4515 int extern_ref = (!(specbits & (1 << (int) RID_AUTO))
4516 || current_binding_level == global_binding_level);
51e29401
RS
4517 tree arg_types;
4518
4519 /* Declaring a function type.
4520 Make sure we have a valid type for the function to return. */
4521 if (type == error_mark_node)
4522 continue;
4523
929f3671 4524 size_varies = 0;
51e29401
RS
4525
4526 /* Warn about some types functions can't return. */
4527
4528 if (TREE_CODE (type) == FUNCTION_TYPE)
4529 {
4530 error ("`%s' declared as function returning a function", name);
4531 type = integer_type_node;
4532 }
4533 if (TREE_CODE (type) == ARRAY_TYPE)
4534 {
4535 error ("`%s' declared as function returning an array", name);
4536 type = integer_type_node;
4537 }
4538
4539#ifndef TRADITIONAL_RETURN_FLOAT
4540 /* Traditionally, declaring return type float means double. */
4541
90d56da8 4542 if (flag_traditional && TYPE_MAIN_VARIANT (type) == float_type_node)
51e29401
RS
4543 type = double_type_node;
4544#endif /* TRADITIONAL_RETURN_FLOAT */
4545
fd0b8fce
JW
4546 /* If this is a block level extern, it must live past the end
4547 of the function so that we can check it against other extern
4548 declarations (IDENTIFIER_LIMBO_VALUE). */
4549 if (extern_ref && allocation_temporary_p ())
4550 end_temporary_allocation ();
4551
51e29401
RS
4552 /* Construct the function type and go to the next
4553 inner layer of declarator. */
4554
4555 arg_types = grokparms (TREE_OPERAND (declarator, 1),
4556 funcdef_flag
4557 /* Say it's a definition
4558 only for the CALL_EXPR
4559 closest to the identifier. */
4560 && TREE_CODE (TREE_OPERAND (declarator, 0)) == IDENTIFIER_NODE);
4561#if 0 /* This seems to be false. We turn off temporary allocation
4562 above in this function if -traditional.
4563 And this code caused inconsistent results with prototypes:
4564 callers would ignore them, and pass arguments wrong. */
4565
4566 /* Omit the arg types if -traditional, since the arg types
4567 and the list links might not be permanent. */
8d9bfdc5
RK
4568 type = build_function_type (type,
4569 flag_traditional
4570 ? NULL_TREE : arg_types);
51e29401 4571#endif
61df2ee2
RS
4572 /* ANSI seems to say that `const int foo ();'
4573 does not make the function foo const. */
4574 if (constp || volatilep)
4575 type = c_build_type_variant (type, constp, volatilep);
4576 constp = 0;
4577 volatilep = 0;
4578
51e29401
RS
4579 type = build_function_type (type, arg_types);
4580 declarator = TREE_OPERAND (declarator, 0);
4581
4582 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4583 the formal parameter list of this FUNCTION_TYPE to point to
4584 the FUNCTION_TYPE node itself. */
4585
4586 {
4587 register tree link;
4588
4589 for (link = current_function_parm_tags;
4590 link;
4591 link = TREE_CHAIN (link))
4592 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4593 }
4594 }
4595 else if (TREE_CODE (declarator) == INDIRECT_REF)
4596 {
4597 /* Merge any constancy or volatility into the target type
4598 for the pointer. */
4599
4600 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4601 && (constp || volatilep))
4602 pedwarn ("ANSI C forbids const or volatile function types");
4603 if (constp || volatilep)
4604 type = c_build_type_variant (type, constp, volatilep);
4605 constp = 0;
4606 volatilep = 0;
929f3671 4607 size_varies = 0;
51e29401
RS
4608
4609 type = build_pointer_type (type);
4610
4611 /* Process a list of type modifier keywords
4612 (such as const or volatile) that were given inside the `*'. */
4613
4614 if (TREE_TYPE (declarator))
4615 {
4616 register tree typemodlist;
4617 int erred = 0;
4618 for (typemodlist = TREE_TYPE (declarator); typemodlist;
4619 typemodlist = TREE_CHAIN (typemodlist))
4620 {
4621 if (TREE_VALUE (typemodlist) == ridpointers[(int) RID_CONST])
4622 constp++;
4623 else if (TREE_VALUE (typemodlist) == ridpointers[(int) RID_VOLATILE])
4624 volatilep++;
4625 else if (!erred)
4626 {
4627 erred = 1;
4628 error ("invalid type modifier within pointer declarator");
4629 }
4630 }
4631 if (constp > 1)
47429a02 4632 pedwarn ("duplicate `const'");
51e29401 4633 if (volatilep > 1)
47429a02 4634 pedwarn ("duplicate `volatile'");
51e29401
RS
4635 }
4636
4637 declarator = TREE_OPERAND (declarator, 0);
4638 }
4639 else
4640 abort ();
4641
4642 }
4643
4644 /* Now TYPE has the actual type. */
4645
4646 /* If this is declaring a typedef name, return a TYPE_DECL. */
4647
4648 if (specbits & (1 << (int) RID_TYPEDEF))
4649 {
4650 tree decl;
4651 /* Note that the grammar rejects storage classes
4652 in typenames, fields or parameters */
4653 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4654 && (constp || volatilep))
4655 pedwarn ("ANSI C forbids const or volatile function types");
4656 if (constp || volatilep)
4657 type = c_build_type_variant (type, constp, volatilep);
4658 pop_obstacks ();
4659 decl = build_decl (TYPE_DECL, declarator, type);
4660 if ((specbits & (1 << (int) RID_SIGNED))
4661 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
4662 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4663 return decl;
4664 }
4665
4666 /* Detect the case of an array type of unspecified size
4667 which came, as such, direct from a typedef name.
4668 We must copy the type, so that each identifier gets
4669 a distinct type, so that each identifier's size can be
4670 controlled separately by its own initializer. */
4671
4672 if (type != 0 && typedef_type != 0
4673 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type)
4674 && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0)
4675 {
4676 type = build_array_type (TREE_TYPE (type), 0);
929f3671 4677 if (size_varies)
51e29401
RS
4678 C_TYPE_VARIABLE_SIZE (type) = 1;
4679 }
4680
4681 /* If this is a type name (such as, in a cast or sizeof),
4682 compute the type and return it now. */
4683
4684 if (decl_context == TYPENAME)
4685 {
4686 /* Note that the grammar rejects storage classes
4687 in typenames, fields or parameters */
4688 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4689 && (constp || volatilep))
4690 pedwarn ("ANSI C forbids const or volatile function types");
4691 if (constp || volatilep)
4692 type = c_build_type_variant (type, constp, volatilep);
4693 pop_obstacks ();
4694 return type;
4695 }
4696
61df2ee2
RS
4697 /* Aside from typedefs and type names (handle above),
4698 `void' at top level (not within pointer)
4699 is allowed only in public variables.
51e29401
RS
4700 We don't complain about parms either, but that is because
4701 a better error message can be made later. */
4702
61df2ee2
RS
4703 if (TYPE_MAIN_VARIANT (type) == void_type_node && decl_context != PARM
4704 && ! ((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4705 && ((specbits & (1 << (int) RID_EXTERN))
4706 || (current_binding_level == global_binding_level
4707 && !(specbits
4708 & ((1 << (int) RID_STATIC) | (1 << (int) RID_REGISTER)))))))
51e29401
RS
4709 {
4710 error ("variable or field `%s' declared void",
4711 IDENTIFIER_POINTER (declarator));
4712 type = integer_type_node;
4713 }
4714
4715 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4716 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4717
4718 {
4719 register tree decl;
4720
4721 if (decl_context == PARM)
4722 {
4723 tree type_as_written = type;
90d56da8 4724 tree main_type;
51e29401
RS
4725
4726 /* A parameter declared as an array of T is really a pointer to T.
4727 One declared as a function is really a pointer to a function. */
4728
4729 if (TREE_CODE (type) == ARRAY_TYPE)
4730 {
4731 /* Transfer const-ness of array into that of type pointed to. */
eaf2e788
RS
4732 type = TREE_TYPE (type);
4733 if (constp || volatilep)
4734 type = c_build_type_variant (type, constp, volatilep);
4735 type = build_pointer_type (type);
51e29401 4736 volatilep = constp = 0;
929f3671 4737 size_varies = 0;
51e29401
RS
4738 }
4739 else if (TREE_CODE (type) == FUNCTION_TYPE)
4740 {
4741 if (pedantic && (constp || volatilep))
4742 pedwarn ("ANSI C forbids const or volatile function types");
eaf2e788
RS
4743 if (constp || volatilep)
4744 type = c_build_type_variant (type, constp, volatilep);
4745 type = build_pointer_type (type);
51e29401
RS
4746 volatilep = constp = 0;
4747 }
4748
51e29401 4749 decl = build_decl (PARM_DECL, declarator, type);
929f3671 4750 if (size_varies)
51e29401
RS
4751 C_DECL_VARIABLE_SIZE (decl) = 1;
4752
4753 /* Compute the type actually passed in the parmlist,
4754 for the case where there is no prototype.
4755 (For example, shorts and chars are passed as ints.)
4756 When there is a prototype, this is overridden later. */
4757
4758 DECL_ARG_TYPE (decl) = type;
f537a5c5
RS
4759 main_type = (type == error_mark_node
4760 ? error_mark_node
4761 : TYPE_MAIN_VARIANT (type));
90d56da8 4762 if (main_type == float_type_node)
51e29401 4763 DECL_ARG_TYPE (decl) = double_type_node;
13d39dbc 4764 /* Don't use TYPE_PRECISION to decide whether to promote,
8eebb258
RS
4765 because we should convert short if it's the same size as int,
4766 but we should not convert long if it's the same size as int. */
f537a5c5
RS
4767 else if (TREE_CODE (main_type) != ERROR_MARK
4768 && C_PROMOTING_INTEGER_TYPE_P (main_type))
8eebb258
RS
4769 {
4770 if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)
4771 && TREE_UNSIGNED (type))
4772 DECL_ARG_TYPE (decl) = unsigned_type_node;
4773 else
4774 DECL_ARG_TYPE (decl) = integer_type_node;
4775 }
51e29401
RS
4776
4777 DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
4778 }
4779 else if (decl_context == FIELD)
4780 {
4781 /* Structure field. It may not be a function. */
4782
4783 if (TREE_CODE (type) == FUNCTION_TYPE)
4784 {
4785 error ("field `%s' declared as a function",
4786 IDENTIFIER_POINTER (declarator));
4787 type = build_pointer_type (type);
4788 }
4789 else if (TREE_CODE (type) != ERROR_MARK && TYPE_SIZE (type) == 0)
4790 {
4791 error ("field `%s' has incomplete type",
4792 IDENTIFIER_POINTER (declarator));
4793 type = error_mark_node;
4794 }
4795 /* Move type qualifiers down to element of an array. */
4796 if (TREE_CODE (type) == ARRAY_TYPE && (constp || volatilep))
4797 {
4798 type = build_array_type (c_build_type_variant (TREE_TYPE (type),
4799 constp, volatilep),
4800 TYPE_DOMAIN (type));
4801#if 0 /* Leave the field const or volatile as well. */
4802 constp = volatilep = 0;
4803#endif
4804 }
4805 decl = build_decl (FIELD_DECL, declarator, type);
929f3671 4806 if (size_varies)
51e29401
RS
4807 C_DECL_VARIABLE_SIZE (decl) = 1;
4808 }
4809 else if (TREE_CODE (type) == FUNCTION_TYPE)
4810 {
fd0b8fce
JW
4811 /* Every function declaration is "external"
4812 except for those which are inside a function body
4813 in which `auto' is used.
4814 That is a case not specified by ANSI C,
4815 and we use it for forward declarations for nested functions. */
4816 int extern_ref = (!(specbits & (1 << (int) RID_AUTO))
4817 || current_binding_level == global_binding_level);
4818
51e29401
RS
4819 if (specbits & (1 << (int) RID_AUTO)
4820 && (pedantic || current_binding_level == global_binding_level))
4821 pedwarn ("invalid storage class for function `%s'",
4822 IDENTIFIER_POINTER (declarator));
4823 if (specbits & (1 << (int) RID_REGISTER))
4824 error ("invalid storage class for function `%s'",
4825 IDENTIFIER_POINTER (declarator));
4826 /* Function declaration not at top level.
4827 Storage classes other than `extern' are not allowed
4828 and `extern' makes no difference. */
4829 if (current_binding_level != global_binding_level
4830 && (specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_INLINE)))
4831 && pedantic)
4832 pedwarn ("invalid storage class for function `%s'",
4833 IDENTIFIER_POINTER (declarator));
fd0b8fce
JW
4834
4835 /* If this is a block level extern, it must live past the end
4836 of the function so that we can check it against other
4837 extern declarations (IDENTIFIER_LIMBO_VALUE). */
4838 if (extern_ref && allocation_temporary_p ())
4839 end_temporary_allocation ();
4840
51e29401
RS
4841 decl = build_decl (FUNCTION_DECL, declarator, type);
4842
7a0347ff
RS
4843 if (pedantic && (constp || volatilep)
4844 && ! DECL_IN_SYSTEM_HEADER (decl))
51e29401
RS
4845 pedwarn ("ANSI C forbids const or volatile functions");
4846
61df2ee2
RS
4847 if (volatilep
4848 && TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
11433f42 4849 warning ("`noreturn' function returns non-void value");
61df2ee2 4850
fd0b8fce 4851 if (extern_ref)
1394aabd 4852 DECL_EXTERNAL (decl) = 1;
51e29401
RS
4853 /* Record absence of global scope for `static' or `auto'. */
4854 TREE_PUBLIC (decl)
4855 = !(specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_AUTO)));
4856 /* Record presence of `inline', if it is reasonable. */
4857 if (inlinep)
4858 {
4859 tree last = tree_last (TYPE_ARG_TYPES (type));
4860
4861 if (! strcmp (IDENTIFIER_POINTER (declarator), "main"))
4862 warning ("cannot inline function `main'");
5fe86b8b
RS
4863 else if (last && (TYPE_MAIN_VARIANT (TREE_VALUE (last))
4864 != void_type_node))
51e29401
RS
4865 warning ("inline declaration ignored for function with `...'");
4866 else
4867 /* Assume that otherwise the function can be inlined. */
1394aabd 4868 DECL_INLINE (decl) = 1;
51e29401
RS
4869
4870 if (specbits & (1 << (int) RID_EXTERN))
4871 current_extern_inline = 1;
4872 }
4873 }
4874 else
4875 {
4876 /* It's a variable. */
fd0b8fce
JW
4877 /* An uninitialized decl with `extern' is a reference. */
4878 int extern_ref = !initialized && (specbits & (1 << (int) RID_EXTERN));
51e29401
RS
4879
4880 /* Move type qualifiers down to element of an array. */
4881 if (TREE_CODE (type) == ARRAY_TYPE && (constp || volatilep))
4882 {
4883 type = build_array_type (c_build_type_variant (TREE_TYPE (type),
4884 constp, volatilep),
4885 TYPE_DOMAIN (type));
4886#if 0 /* Leave the variable const or volatile as well. */
4887 constp = volatilep = 0;
4888#endif
4889 }
4890
fd0b8fce
JW
4891 /* If this is a block level extern, it must live past the end
4892 of the function so that we can check it against other
4893 extern declarations (IDENTIFIER_LIMBO_VALUE). */
4894 if (extern_ref && allocation_temporary_p ())
4895 end_temporary_allocation ();
4896
51e29401 4897 decl = build_decl (VAR_DECL, declarator, type);
929f3671 4898 if (size_varies)
51e29401
RS
4899 C_DECL_VARIABLE_SIZE (decl) = 1;
4900
4901 if (inlinep)
4902 pedwarn_with_decl (decl, "variable `%s' declared `inline'");
4903
fd0b8fce 4904 DECL_EXTERNAL (decl) = extern_ref;
ee534ebf
RS
4905 /* At top level, the presence of a `static' or `register' storage
4906 class specifier, or the absence of all storage class specifiers
4907 makes this declaration a definition (perhaps tentative). Also,
4908 the absence of both `static' and `register' makes it public. */
51e29401
RS
4909 if (current_binding_level == global_binding_level)
4910 {
ee534ebf
RS
4911 TREE_PUBLIC (decl)
4912 = !(specbits
4913 & ((1 << (int) RID_STATIC) | (1 << (int) RID_REGISTER)));
1394aabd 4914 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
51e29401
RS
4915 }
4916 /* Not at top level, only `static' makes a static definition. */
4917 else
4918 {
4919 TREE_STATIC (decl) = (specbits & (1 << (int) RID_STATIC)) != 0;
1394aabd 4920 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
51e29401 4921 }
519d591f
RS
4922
4923 if (specbits & 1 << (int) RID_ITERATOR)
4924 ITERATOR_P (decl) = 1;
51e29401
RS
4925 }
4926
4927 /* Record `register' declaration for warnings on &
4928 and in case doing stupid register allocation. */
4929
4930 if (specbits & (1 << (int) RID_REGISTER))
1394aabd 4931 DECL_REGISTER (decl) = 1;
51e29401
RS
4932
4933 /* Record constancy and volatility. */
4934
4935 if (constp)
4936 TREE_READONLY (decl) = 1;
4937 if (volatilep)
4938 {
4939 TREE_SIDE_EFFECTS (decl) = 1;
4940 TREE_THIS_VOLATILE (decl) = 1;
4941 }
4942 /* If a type has volatile components, it should be stored in memory.
4943 Otherwise, the fact that those components are volatile
4944 will be ignored, and would even crash the compiler. */
4945 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
4946 mark_addressable (decl);
4947
4948 pop_obstacks ();
4949
4950 return decl;
4951 }
4952}
4953\f
51e29401
RS
4954/* Decode the parameter-list info for a function type or function definition.
4955 The argument is the value returned by `get_parm_info' (or made in parse.y
4956 if there is an identifier list instead of a parameter decl list).
4957 These two functions are separate because when a function returns
4958 or receives functions then each is called multiple times but the order
4959 of calls is different. The last call to `grokparms' is always the one
4960 that contains the formal parameter names of a function definition.
4961
4962 Store in `last_function_parms' a chain of the decls of parms.
4963 Also store in `last_function_parm_tags' a chain of the struct, union,
4964 and enum tags declared among the parms.
4965
4966 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4967
4968 FUNCDEF_FLAG is nonzero for a function definition, 0 for
4969 a mere declaration. A nonempty identifier-list gets an error message
4970 when FUNCDEF_FLAG is zero. */
4971
4972static tree
4973grokparms (parms_info, funcdef_flag)
4974 tree parms_info;
4975 int funcdef_flag;
4976{
4977 tree first_parm = TREE_CHAIN (parms_info);
4978
4979 last_function_parms = TREE_PURPOSE (parms_info);
4980 last_function_parm_tags = TREE_VALUE (parms_info);
4981
27f427f8
RS
4982 if (warn_strict_prototypes && first_parm == 0 && !funcdef_flag
4983 && !in_system_header)
51e29401
RS
4984 warning ("function declaration isn't a prototype");
4985
4986 if (first_parm != 0
4987 && TREE_CODE (TREE_VALUE (first_parm)) == IDENTIFIER_NODE)
4988 {
4989 if (! funcdef_flag)
4990 pedwarn ("parameter names (without types) in function declaration");
4991
4992 last_function_parms = first_parm;
4993 return 0;
4994 }
4995 else
4996 {
4997 tree parm;
4998 tree typelt;
4999 /* We no longer test FUNCDEF_FLAG.
5000 If the arg types are incomplete in a declaration,
5001 they must include undefined tags.
5002 These tags can never be defined in the scope of the declaration,
5003 so the types can never be completed,
5004 and no call can be compiled successfully. */
5005#if 0
5006 /* In a fcn definition, arg types must be complete. */
5007 if (funcdef_flag)
5008#endif
5009 for (parm = last_function_parms, typelt = first_parm;
5010 parm;
5011 parm = TREE_CHAIN (parm))
5012 /* Skip over any enumeration constants declared here. */
5013 if (TREE_CODE (parm) == PARM_DECL)
5014 {
5015 /* Barf if the parameter itself has an incomplete type. */
5016 tree type = TREE_VALUE (typelt);
5017 if (TYPE_SIZE (type) == 0)
5018 {
5019 if (funcdef_flag && DECL_NAME (parm) != 0)
5020 error ("parameter `%s' has incomplete type",
5021 IDENTIFIER_POINTER (DECL_NAME (parm)));
5022 else
5023 warning ("parameter has incomplete type");
5024 if (funcdef_flag)
5025 {
5026 TREE_VALUE (typelt) = error_mark_node;
5027 TREE_TYPE (parm) = error_mark_node;
5028 }
5029 }
5030#if 0 /* This has been replaced by parm_tags_warning
5031 which uses a more accurate criterion for what to warn about. */
5032 else
5033 {
5034 /* Now warn if is a pointer to an incomplete type. */
5035 while (TREE_CODE (type) == POINTER_TYPE
5036 || TREE_CODE (type) == REFERENCE_TYPE)
5037 type = TREE_TYPE (type);
5038 type = TYPE_MAIN_VARIANT (type);
5039 if (TYPE_SIZE (type) == 0)
5040 {
5041 if (DECL_NAME (parm) != 0)
5042 warning ("parameter `%s' points to incomplete type",
5043 IDENTIFIER_POINTER (DECL_NAME (parm)));
5044 else
5045 warning ("parameter points to incomplete type");
5046 }
5047 }
5048#endif
5049 typelt = TREE_CHAIN (typelt);
5050 }
5051
023de292 5052 /* Allocate the list of types the way we allocate a type. */
c47851dd 5053 if (first_parm && ! TREE_PERMANENT (first_parm))
023de292
RS
5054 {
5055 /* Construct a copy of the list of types
5056 on the saveable obstack. */
5057 tree result = NULL;
5058 for (typelt = first_parm; typelt; typelt = TREE_CHAIN (typelt))
5059 result = saveable_tree_cons (NULL_TREE, TREE_VALUE (typelt),
5060 result);
5061 return nreverse (result);
5062 }
5063 else
5064 /* The list we have is permanent already. */
5065 return first_parm;
51e29401
RS
5066 }
5067}
5068
5069
5070/* Return a tree_list node with info on a parameter list just parsed.
5071 The TREE_PURPOSE is a chain of decls of those parms.
5072 The TREE_VALUE is a list of structure, union and enum tags defined.
5073 The TREE_CHAIN is a list of argument types to go in the FUNCTION_TYPE.
5074 This tree_list node is later fed to `grokparms'.
5075
5076 VOID_AT_END nonzero means append `void' to the end of the type-list.
5077 Zero means the parmlist ended with an ellipsis so don't append `void'. */
5078
5079tree
5080get_parm_info (void_at_end)
5081 int void_at_end;
5082{
5083 register tree decl, t;
5084 register tree types = 0;
5085 int erred = 0;
5086 tree tags = gettags ();
5087 tree parms = getdecls ();
5088 tree new_parms = 0;
5089 tree order = current_binding_level->parm_order;
5090
5091 /* Just `void' (and no ellipsis) is special. There are really no parms. */
5092 if (void_at_end && parms != 0
5093 && TREE_CHAIN (parms) == 0
5fe86b8b 5094 && TYPE_MAIN_VARIANT (TREE_TYPE (parms)) == void_type_node
51e29401
RS
5095 && DECL_NAME (parms) == 0)
5096 {
5097 parms = NULL_TREE;
5098 storedecls (NULL_TREE);
5099 return saveable_tree_cons (NULL_TREE, NULL_TREE,
5100 saveable_tree_cons (NULL_TREE, void_type_node, NULL_TREE));
5101 }
5102
fc3ffe83
RK
5103 /* Extract enumerator values and other non-parms declared with the parms.
5104 Likewise any forward parm decls that didn't have real parm decls. */
51e29401
RS
5105 for (decl = parms; decl; )
5106 {
5107 tree next = TREE_CHAIN (decl);
5108
e38e5ba8 5109 if (TREE_CODE (decl) != PARM_DECL)
fc3ffe83 5110 {
fc3ffe83
RK
5111 TREE_CHAIN (decl) = new_parms;
5112 new_parms = decl;
5113 }
e38e5ba8 5114 else if (TREE_ASM_WRITTEN (decl))
51e29401 5115 {
e38e5ba8 5116 error_with_decl (decl, "parameter `%s' has just a forward declaration");
51e29401
RS
5117 TREE_CHAIN (decl) = new_parms;
5118 new_parms = decl;
5119 }
5120 decl = next;
5121 }
5122
5123 /* Put the parm decls back in the order they were in in the parm list. */
5124 for (t = order; t; t = TREE_CHAIN (t))
5125 {
5126 if (TREE_CHAIN (t))
5127 TREE_CHAIN (TREE_VALUE (t)) = TREE_VALUE (TREE_CHAIN (t));
5128 else
5129 TREE_CHAIN (TREE_VALUE (t)) = 0;
5130 }
5131
5132 new_parms = chainon (order ? nreverse (TREE_VALUE (order)) : 0,
5133 new_parms);
5134
5135 /* Store the parmlist in the binding level since the old one
5136 is no longer a valid list. (We have changed the chain pointers.) */
5137 storedecls (new_parms);
5138
5139 for (decl = new_parms; decl; decl = TREE_CHAIN (decl))
5140 /* There may also be declarations for enumerators if an enumeration
5141 type is declared among the parms. Ignore them here. */
5142 if (TREE_CODE (decl) == PARM_DECL)
5143 {
5144 /* Since there is a prototype,
5145 args are passed in their declared types. */
5146 tree type = TREE_TYPE (decl);
5147 DECL_ARG_TYPE (decl) = type;
5148#ifdef PROMOTE_PROTOTYPES
fd48df3e
RK
5149 if ((TREE_CODE (type) == INTEGER_TYPE
5150 || TREE_CODE (type) == ENUMERAL_TYPE)
51e29401
RS
5151 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
5152 DECL_ARG_TYPE (decl) = integer_type_node;
5153#endif
5154
5155 types = saveable_tree_cons (NULL_TREE, TREE_TYPE (decl), types);
5fe86b8b 5156 if (TYPE_MAIN_VARIANT (TREE_VALUE (types)) == void_type_node && ! erred
51e29401
RS
5157 && DECL_NAME (decl) == 0)
5158 {
5159 error ("`void' in parameter list must be the entire list");
5160 erred = 1;
5161 }
5162 }
5163
5164 if (void_at_end)
5165 return saveable_tree_cons (new_parms, tags,
5166 nreverse (saveable_tree_cons (NULL_TREE, void_type_node, types)));
5167
5168 return saveable_tree_cons (new_parms, tags, nreverse (types));
5169}
5170
5171/* At end of parameter list, warn about any struct, union or enum tags
5172 defined within. Do so because these types cannot ever become complete. */
5173
5174void
5175parmlist_tags_warning ()
5176{
5177 tree elt;
5178 static int already;
5179
5180 for (elt = current_binding_level->tags; elt; elt = TREE_CHAIN (elt))
5181 {
5182 enum tree_code code = TREE_CODE (TREE_VALUE (elt));
27301b30
RS
5183 /* An anonymous union parm type is meaningful as a GNU extension.
5184 So don't warn for that. */
5185 if (code == UNION_TYPE && !pedantic)
5186 continue;
c138f328
RS
5187 if (TREE_PURPOSE (elt) != 0)
5188 warning ("`%s %s' declared inside parameter list",
5189 (code == RECORD_TYPE ? "struct"
5190 : code == UNION_TYPE ? "union"
5191 : "enum"),
5192 IDENTIFIER_POINTER (TREE_PURPOSE (elt)));
5193 else
5194 warning ("anonymous %s declared inside parameter list",
5195 (code == RECORD_TYPE ? "struct"
5196 : code == UNION_TYPE ? "union"
5197 : "enum"));
5198
51e29401
RS
5199 if (! already)
5200 {
5201 warning ("its scope is only this definition or declaration,");
5202 warning ("which is probably not what you want.");
5203 already = 1;
5204 }
5205 }
5206}
5207\f
5208/* Get the struct, enum or union (CODE says which) with tag NAME.
5209 Define the tag as a forward-reference if it is not defined. */
5210
5211tree
5212xref_tag (code, name)
5213 enum tree_code code;
5214 tree name;
5215{
5216 int temporary = allocation_temporary_p ();
5217
5218 /* If a cross reference is requested, look up the type
5219 already defined for this tag and return it. */
5220
5221 register tree ref = lookup_tag (code, name, current_binding_level, 0);
5222 /* Even if this is the wrong type of tag, return what we found.
5223 There will be an error message anyway, from pending_xref_error.
5224 If we create an empty xref just for an invalid use of the type,
929f3671 5225 the main result is to create lots of superfluous error messages. */
51e29401
RS
5226 if (ref)
5227 return ref;
5228
5229 push_obstacks_nochange ();
5230
5231 if (current_binding_level == global_binding_level && temporary)
5232 end_temporary_allocation ();
5233
5234 /* If no such tag is yet defined, create a forward-reference node
5235 and record it as the "definition".
5236 When a real declaration of this type is found,
5237 the forward-reference will be altered into a real type. */
5238
5239 ref = make_node (code);
5240 if (code == ENUMERAL_TYPE)
5241 {
5242 /* (In ANSI, Enums can be referred to only if already defined.) */
5243 if (pedantic)
5244 pedwarn ("ANSI C forbids forward references to `enum' types");
5245 /* Give the type a default layout like unsigned int
5246 to avoid crashing if it does not get defined. */
5247 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
5248 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
5249 TREE_UNSIGNED (ref) = 1;
5250 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
5251 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
5252 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
5253 }
5254
5255 pushtag (name, ref);
5256
5257 pop_obstacks ();
5258
5259 return ref;
5260}
5261\f
5262/* Make sure that the tag NAME is defined *in the current binding level*
5263 at least as a forward reference.
7a0347ff
RS
5264 CODE says which kind of tag NAME ought to be.
5265
5266 We also do a push_obstacks_nochange
5267 whose matching pop is in finish_struct. */
51e29401
RS
5268
5269tree
5270start_struct (code, name)
5271 enum tree_code code;
5272 tree name;
5273{
5274 /* If there is already a tag defined at this binding level
5275 (as a forward reference), just return it. */
5276
5277 register tree ref = 0;
5278
7a0347ff
RS
5279 push_obstacks_nochange ();
5280 if (current_binding_level == global_binding_level)
5281 end_temporary_allocation ();
5282
51e29401
RS
5283 if (name != 0)
5284 ref = lookup_tag (code, name, current_binding_level, 1);
5285 if (ref && TREE_CODE (ref) == code)
5286 {
5287 C_TYPE_BEING_DEFINED (ref) = 1;
5288 if (TYPE_FIELDS (ref))
5289 error ((code == UNION_TYPE ? "redefinition of `union %s'"
5290 : "redefinition of `struct %s'"),
5291 IDENTIFIER_POINTER (name));
5292
5293 return ref;
5294 }
5295
5296 /* Otherwise create a forward-reference just so the tag is in scope. */
5297
5298 ref = make_node (code);
5299 pushtag (name, ref);
5300 C_TYPE_BEING_DEFINED (ref) = 1;
5301 return ref;
5302}
5303
5304/* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
5305 of a structure component, returning a FIELD_DECL node.
5306 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node.
5307
5308 This is done during the parsing of the struct declaration.
5309 The FIELD_DECL nodes are chained together and the lot of them
5310 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
5311
5312tree
5313grokfield (filename, line, declarator, declspecs, width)
5314 char *filename;
5315 int line;
5316 tree declarator, declspecs, width;
5317{
5318 tree value;
5319
5320 /* The corresponding pop_obstacks is in finish_decl. */
5321 push_obstacks_nochange ();
5322
5323 value = grokdeclarator (declarator, declspecs, width ? BITFIELD : FIELD, 0);
5324
8d9bfdc5 5325 finish_decl (value, NULL_TREE, NULL_TREE);
51e29401
RS
5326 DECL_INITIAL (value) = width;
5327
abe31bf8 5328 maybe_objc_check_decl (value);
51e29401
RS
5329 return value;
5330}
5331\f
5332/* Function to help qsort sort FIELD_DECLs by name order. */
5333
5334static int
5335field_decl_cmp (x, y)
5336 tree *x, *y;
5337{
5338 return (long)DECL_NAME (*x) - (long)DECL_NAME (*y);
5339}
5340
5341/* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
7a0347ff
RS
5342 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5343
5344 We also do a pop_obstacks to match the push in start_struct. */
51e29401
RS
5345
5346tree
5347finish_struct (t, fieldlist)
5348 register tree t, fieldlist;
5349{
5350 register tree x;
5351 int old_momentary;
5352 int toplevel = global_binding_level == current_binding_level;
5353
5354 /* If this type was previously laid out as a forward reference,
5355 make sure we lay it out again. */
5356
5357 TYPE_SIZE (t) = 0;
5358
5359 /* Nameless union parm types are useful as GCC extension. */
5360 if (! (TREE_CODE (t) == UNION_TYPE && TYPE_NAME (t) == 0) && !pedantic)
5361 /* Otherwise, warn about any struct or union def. in parmlist. */
5362 if (in_parm_level_p ())
5363 {
5364 if (pedantic)
5365 pedwarn ((TREE_CODE (t) == UNION_TYPE ? "union defined inside parms"
5366 : "structure defined inside parms"));
f3c2c111 5367 else if (! flag_traditional)
51e29401
RS
5368 warning ((TREE_CODE (t) == UNION_TYPE ? "union defined inside parms"
5369 : "structure defined inside parms"));
5370 }
5371
5372 old_momentary = suspend_momentary ();
5373
5374 if (fieldlist == 0 && pedantic)
5375 pedwarn ((TREE_CODE (t) == UNION_TYPE ? "union has no members"
5376 : "structure has no members"));
5377
5378 /* Install struct as DECL_CONTEXT of each field decl.
5379 Also process specified field sizes.
77f934bb 5380 Set DECL_FIELD_SIZE to the specified size, or 0 if none specified.
51e29401
RS
5381 The specified size is found in the DECL_INITIAL.
5382 Store 0 there, except for ": 0" fields (so we can find them
5383 and delete them, below). */
5384
5385 for (x = fieldlist; x; x = TREE_CHAIN (x))
5386 {
5387 DECL_CONTEXT (x) = t;
77f934bb 5388 DECL_FIELD_SIZE (x) = 0;
51e29401
RS
5389
5390 /* If any field is const, the structure type is pseudo-const. */
5391 if (TREE_READONLY (x))
5392 C_TYPE_FIELDS_READONLY (t) = 1;
5393 else
5394 {
5395 /* A field that is pseudo-const makes the structure likewise. */
5396 tree t1 = TREE_TYPE (x);
5397 while (TREE_CODE (t1) == ARRAY_TYPE)
5398 t1 = TREE_TYPE (t1);
5399 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5400 && C_TYPE_FIELDS_READONLY (t1))
5401 C_TYPE_FIELDS_READONLY (t) = 1;
5402 }
5403
5404 /* Any field that is volatile means variables of this type must be
5405 treated in some ways as volatile. */
5406 if (TREE_THIS_VOLATILE (x))
5407 C_TYPE_FIELDS_VOLATILE (t) = 1;
5408
5409 /* Any field of nominal variable size implies structure is too. */
5410 if (C_DECL_VARIABLE_SIZE (x))
5411 C_TYPE_VARIABLE_SIZE (t) = 1;
5412
8d7bbe5f
RS
5413 /* Detect invalid nested redefinition. */
5414 if (TREE_TYPE (x) == t)
5415 error ("nested redefinition of `%s'",
5416 IDENTIFIER_POINTER (TYPE_NAME (t)));
5417
51e29401 5418 /* Detect invalid bit-field size. */
07c5ab55
RS
5419 if (DECL_INITIAL (x))
5420 STRIP_NOPS (DECL_INITIAL (x));
90374cc2 5421 if (DECL_INITIAL (x))
e681c5a1
RS
5422 {
5423 if (TREE_CODE (DECL_INITIAL (x)) == INTEGER_CST)
5424 constant_expression_warning (DECL_INITIAL (x));
5425 else
5426 {
5427 error_with_decl (x, "bit-field `%s' width not an integer constant");
5428 DECL_INITIAL (x) = NULL;
5429 }
5430 }
51e29401
RS
5431
5432 /* Detect invalid bit-field type. */
5433 if (DECL_INITIAL (x)
5434 && TREE_CODE (TREE_TYPE (x)) != INTEGER_TYPE
5435 && TREE_CODE (TREE_TYPE (x)) != ENUMERAL_TYPE)
5436 {
5437 error_with_decl (x, "bit-field `%s' has invalid type");
5438 DECL_INITIAL (x) = NULL;
5439 }
5440 if (DECL_INITIAL (x) && pedantic
5441 && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != integer_type_node
61df2ee2
RS
5442 && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != unsigned_type_node
5443 /* Accept an enum that's equivalent to int or unsigned int. */
5444 && !(TREE_CODE (TREE_TYPE (x)) == ENUMERAL_TYPE
5445 && (TYPE_PRECISION (TREE_TYPE (x))
5446 == TYPE_PRECISION (integer_type_node))))
51e29401
RS
5447 pedwarn_with_decl (x, "bit-field `%s' type invalid in ANSI C");
5448
5449 /* Detect and ignore out of range field width. */
5450 if (DECL_INITIAL (x))
5451 {
2d724389 5452 unsigned HOST_WIDE_INT width = TREE_INT_CST_LOW (DECL_INITIAL (x));
51e29401 5453
6aa10371 5454 if (tree_int_cst_sgn (DECL_INITIAL (x)) < 0)
51e29401
RS
5455 {
5456 DECL_INITIAL (x) = NULL;
5457 error_with_decl (x, "negative width in bit-field `%s'");
5458 }
2d724389
RS
5459 else if (TREE_INT_CST_HIGH (DECL_INITIAL (x)) != 0
5460 || width > TYPE_PRECISION (TREE_TYPE (x)))
51e29401 5461 {
51e29401 5462 DECL_INITIAL (x) = NULL;
2d724389 5463 pedwarn_with_decl (x, "width of `%s' exceeds its type");
51e29401 5464 }
2d724389 5465 else if (width == 0 && DECL_NAME (x) != 0)
51e29401 5466 {
2d724389 5467 error_with_decl (x, "zero width for bit-field `%s'");
51e29401 5468 DECL_INITIAL (x) = NULL;
51e29401
RS
5469 }
5470 }
5471
5472 /* Process valid field width. */
5473 if (DECL_INITIAL (x))
5474 {
5475 register int width = TREE_INT_CST_LOW (DECL_INITIAL (x));
5476
77f934bb 5477 DECL_FIELD_SIZE (x) = width;
51e29401
RS
5478 DECL_BIT_FIELD (x) = 1;
5479 DECL_INITIAL (x) = NULL;
5480
5481 if (width == 0)
5482 {
5483 /* field size 0 => force desired amount of alignment. */
5484#ifdef EMPTY_FIELD_BOUNDARY
5485 DECL_ALIGN (x) = MAX (DECL_ALIGN (x), EMPTY_FIELD_BOUNDARY);
5486#endif
5487#ifdef PCC_BITFIELD_TYPE_MATTERS
5488 DECL_ALIGN (x) = MAX (DECL_ALIGN (x),
5489 TYPE_ALIGN (TREE_TYPE (x)));
5490#endif
5491 }
5492 }
34322499 5493 else if (TREE_TYPE (x) != error_mark_node)
ec2343c4
MM
5494 {
5495 int min_align = (DECL_PACKED (x) ? BITS_PER_UNIT
5496 : TYPE_ALIGN (TREE_TYPE (x)));
5497 /* Non-bit-fields are aligned for their type, except packed
5498 fields which require only BITS_PER_UNIT alignment. */
5499 DECL_ALIGN (x) = MAX (DECL_ALIGN (x), min_align);
5500 }
51e29401
RS
5501 }
5502
5503 /* Now DECL_INITIAL is null on all members. */
5504
5505 /* Delete all duplicate fields from the fieldlist */
5506 for (x = fieldlist; x && TREE_CHAIN (x);)
5507 /* Anonymous fields aren't duplicates. */
5508 if (DECL_NAME (TREE_CHAIN (x)) == 0)
5509 x = TREE_CHAIN (x);
5510 else
5511 {
5512 register tree y = fieldlist;
5513
5514 while (1)
5515 {
5516 if (DECL_NAME (y) == DECL_NAME (TREE_CHAIN (x)))
5517 break;
5518 if (y == x)
5519 break;
5520 y = TREE_CHAIN (y);
5521 }
5522 if (DECL_NAME (y) == DECL_NAME (TREE_CHAIN (x)))
5523 {
5524 error_with_decl (TREE_CHAIN (x), "duplicate member `%s'");
5525 TREE_CHAIN (x) = TREE_CHAIN (TREE_CHAIN (x));
5526 }
5527 else x = TREE_CHAIN (x);
5528 }
5529
5530 /* Now we have the nearly final fieldlist. Record it,
5531 then lay out the structure or union (including the fields). */
5532
5533 TYPE_FIELDS (t) = fieldlist;
5534
5535 layout_type (t);
5536
5537 /* Delete all zero-width bit-fields from the front of the fieldlist */
5538 while (fieldlist
5539 && DECL_INITIAL (fieldlist))
5540 fieldlist = TREE_CHAIN (fieldlist);
5541 /* Delete all such members from the rest of the fieldlist */
5542 for (x = fieldlist; x;)
5543 {
5544 if (TREE_CHAIN (x) && DECL_INITIAL (TREE_CHAIN (x)))
5545 TREE_CHAIN (x) = TREE_CHAIN (TREE_CHAIN (x));
5546 else x = TREE_CHAIN (x);
5547 }
5548
5549 /* Now we have the truly final field list.
5550 Store it in this type and in the variants. */
5551
5552 TYPE_FIELDS (t) = fieldlist;
5553
5554 /* If there are lots of fields, sort so we can look through them fast.
5555 We arbitrarily consider 16 or more elts to be "a lot". */
5556 {
5557 int len = 0;
5558
5559 for (x = fieldlist; x; x = TREE_CHAIN (x))
5560 {
5561 if (len > 15)
5562 break;
5563 len += 1;
5564 }
5565 if (len > 15)
5566 {
5567 tree *field_array;
5568 char *space;
5569
5570 len += list_length (x);
5571 /* Use the same allocation policy here that make_node uses, to
5572 ensure that this lives as long as the rest of the struct decl.
5573 All decls in an inline function need to be saved. */
5574 if (allocation_temporary_p ())
5575 space = savealloc (sizeof (struct lang_type) + len * sizeof (tree));
5576 else
5577 space = oballoc (sizeof (struct lang_type) + len * sizeof (tree));
5578
5579 TYPE_LANG_SPECIFIC (t) = (struct lang_type *) space;
5580 TYPE_LANG_SPECIFIC (t)->len = len;
5581
5582 field_array = &TYPE_LANG_SPECIFIC (t)->elts[0];
5583 len = 0;
5584 for (x = fieldlist; x; x = TREE_CHAIN (x))
5585 field_array[len++] = x;
5586
5587 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5588 }
5589 }
5590
5591 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5592 {
5593 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5594 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5595 TYPE_ALIGN (x) = TYPE_ALIGN (t);
5596 }
5597
5598 /* Promote each bit-field's type to int if it is narrower than that. */
5599 for (x = fieldlist; x; x = TREE_CHAIN (x))
5600 if (DECL_BIT_FIELD (x)
06038f12
RS
5601 && (C_PROMOTING_INTEGER_TYPE_P (TREE_TYPE (x))
5602 || DECL_FIELD_SIZE (x) < TYPE_PRECISION (integer_type_node)))
5603 {
5604 tree type = TREE_TYPE (x);
5605
5606 /* Preserve unsignedness if traditional
5607 or if not really getting any wider. */
5608 if (TREE_UNSIGNED (type)
5609 && (flag_traditional
5610 ||
5611 (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)
5612 &&
5613 DECL_FIELD_SIZE (x) == TYPE_PRECISION (integer_type_node))))
5614 TREE_TYPE (x) = unsigned_type_node;
5615 else
5616 TREE_TYPE (x) = integer_type_node;
5617 }
51e29401
RS
5618
5619 /* If this structure or union completes the type of any previous
5620 variable declaration, lay it out and output its rtl. */
5621
5622 if (current_binding_level->n_incomplete != 0)
5623 {
5624 tree decl;
5625 for (decl = current_binding_level->names; decl; decl = TREE_CHAIN (decl))
5626 {
5627 if (TREE_TYPE (decl) == t
5628 && TREE_CODE (decl) != TYPE_DECL)
5629 {
5630 layout_decl (decl, 0);
5631 /* This is a no-op in c-lang.c or something real in objc-actions.c. */
5632 maybe_objc_check_decl (decl);
8d9bfdc5 5633 rest_of_decl_compilation (decl, NULL_PTR, toplevel, 0);
51e29401
RS
5634 if (! toplevel)
5635 expand_decl (decl);
5636 --current_binding_level->n_incomplete;
5637 }
5638 else if (TYPE_SIZE (TREE_TYPE (decl)) == 0
5639 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5640 {
5641 tree element = TREE_TYPE (decl);
5642 while (TREE_CODE (element) == ARRAY_TYPE)
5643 element = TREE_TYPE (element);
5644 if (element == t)
5645 layout_array_type (TREE_TYPE (decl));
5646 }
5647 }
5648 }
5649
5650 resume_momentary (old_momentary);
5651
5652 /* Finish debugging output for this type. */
5653 rest_of_type_compilation (t, toplevel);
5654
7a0347ff
RS
5655 /* The matching push is in start_struct. */
5656 pop_obstacks ();
5657
51e29401
RS
5658 return t;
5659}
5660
5661/* Lay out the type T, and its element type, and so on. */
5662
5663static void
5664layout_array_type (t)
5665 tree t;
5666{
5667 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5668 layout_array_type (TREE_TYPE (t));
5669 layout_type (t);
5670}
5671\f
5672/* Begin compiling the definition of an enumeration type.
5673 NAME is its name (or null if anonymous).
5674 Returns the type object, as yet incomplete.
5675 Also records info about it so that build_enumerator
5676 may be used to declare the individual values as they are read. */
5677
5678tree
5679start_enum (name)
5680 tree name;
5681{
5682 register tree enumtype = 0;
5683
5684 /* If this is the real definition for a previous forward reference,
5685 fill in the contents in the same object that used to be the
5686 forward reference. */
5687
5688 if (name != 0)
5689 enumtype = lookup_tag (ENUMERAL_TYPE, name, current_binding_level, 1);
5690
7a0347ff
RS
5691 /* The corresponding pop_obstacks is in finish_enum. */
5692 push_obstacks_nochange ();
5693 /* If these symbols and types are global, make them permanent. */
5694 if (current_binding_level == global_binding_level)
5695 end_temporary_allocation ();
5696
51e29401
RS
5697 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5698 {
5699 enumtype = make_node (ENUMERAL_TYPE);
5700 pushtag (name, enumtype);
5701 }
5702
5703 C_TYPE_BEING_DEFINED (enumtype) = 1;
5704
5705 if (TYPE_VALUES (enumtype) != 0)
5706 {
5707 /* This enum is a named one that has been declared already. */
5708 error ("redeclaration of `enum %s'", IDENTIFIER_POINTER (name));
5709
5710 /* Completely replace its old definition.
5711 The old enumerators remain defined, however. */
5712 TYPE_VALUES (enumtype) = 0;
5713 }
5714
5715 enum_next_value = integer_zero_node;
93e3ba4f 5716 enum_overflow = 0;
51e29401
RS
5717
5718 return enumtype;
5719}
5720
5721/* After processing and defining all the values of an enumeration type,
5722 install their decls in the enumeration type and finish it off.
5723 ENUMTYPE is the type object and VALUES a list of decl-value pairs.
5724 Returns ENUMTYPE. */
5725
5726tree
5727finish_enum (enumtype, values)
5728 register tree enumtype, values;
5729{
fbe23ee7 5730 register tree pair, tem;
51e29401 5731 tree minnode = 0, maxnode = 0;
de953b38 5732 int lowprec, highprec, precision;
51e29401
RS
5733 int toplevel = global_binding_level == current_binding_level;
5734
5735 if (in_parm_level_p ())
5736 warning ("enum defined inside parms");
5737
5738 /* Calculate the maximum value of any enumerator in this type. */
5739
59116212
RK
5740 if (values == error_mark_node)
5741 minnode = maxnode = integer_zero_node;
5742 else
5743 for (pair = values; pair; pair = TREE_CHAIN (pair))
5744 {
5745 tree value = TREE_VALUE (pair);
5746 if (pair == values)
5747 minnode = maxnode = TREE_VALUE (pair);
5748 else
5749 {
5750 if (tree_int_cst_lt (maxnode, value))
5751 maxnode = value;
5752 if (tree_int_cst_lt (value, minnode))
5753 minnode = value;
5754 }
5755 }
51e29401
RS
5756
5757 TYPE_MIN_VALUE (enumtype) = minnode;
5758 TYPE_MAX_VALUE (enumtype) = maxnode;
5759
de953b38
RK
5760 /* An enum can have some negative values; then it is signed. */
5761 TREE_UNSIGNED (enumtype) = tree_int_cst_sgn (minnode) >= 0;
51e29401 5762
de953b38 5763 /* Determine the precision this type needs. */
51e29401 5764
de953b38
RK
5765 lowprec = min_precision (minnode, TREE_UNSIGNED (enumtype));
5766 highprec = min_precision (maxnode, TREE_UNSIGNED (enumtype));
5767 precision = MAX (lowprec, highprec);
51e29401
RS
5768
5769 if (flag_short_enums || precision > TYPE_PRECISION (integer_type_node))
5770 /* Use the width of the narrowest normal C type which is wide enough. */
5771 TYPE_PRECISION (enumtype) = TYPE_PRECISION (type_for_size (precision, 1));
5772 else
5773 TYPE_PRECISION (enumtype) = TYPE_PRECISION (integer_type_node);
5774
5775 TYPE_SIZE (enumtype) = 0;
5776 layout_type (enumtype);
5777
59116212 5778 if (values != error_mark_node)
75b46437 5779 {
59116212
RK
5780 /* Change the type of the enumerators to be the enum type.
5781 Formerly this was done only for enums that fit in an int,
5782 but the comment said it was done only for enums wider than int.
5783 It seems necessary to do this for wide enums,
5784 and best not to change what's done for ordinary narrower ones. */
5785 for (pair = values; pair; pair = TREE_CHAIN (pair))
5786 {
5787 TREE_TYPE (TREE_PURPOSE (pair)) = enumtype;
5788 DECL_SIZE (TREE_PURPOSE (pair)) = TYPE_SIZE (enumtype);
5789 if (TREE_CODE (TREE_PURPOSE (pair)) != FUNCTION_DECL)
5790 DECL_ALIGN (TREE_PURPOSE (pair)) = TYPE_ALIGN (enumtype);
5791 }
51e29401 5792
59116212
RK
5793 /* Replace the decl nodes in VALUES with their names. */
5794 for (pair = values; pair; pair = TREE_CHAIN (pair))
5795 TREE_PURPOSE (pair) = DECL_NAME (TREE_PURPOSE (pair));
51e29401 5796
59116212
RK
5797 TYPE_VALUES (enumtype) = values;
5798 }
51e29401 5799
fbe23ee7
RS
5800 /* Fix up all variant types of this enum type. */
5801 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5802 {
5803 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5804 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5805 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5806 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5807 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5808 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5809 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5810 TREE_UNSIGNED (tem) = TREE_UNSIGNED (enumtype);
5811 }
5812
51e29401
RS
5813 /* Finish debugging output for this type. */
5814 rest_of_type_compilation (enumtype, toplevel);
5815
7a0347ff
RS
5816 /* This matches a push in start_enum. */
5817 pop_obstacks ();
5818
51e29401
RS
5819 return enumtype;
5820}
5821
5822/* Build and install a CONST_DECL for one value of the
5823 current enumeration type (one that was begun with start_enum).
5824 Return a tree-list containing the CONST_DECL and its value.
5825 Assignment of sequential values by default is handled here. */
5826
5827tree
5828build_enumerator (name, value)
5829 tree name, value;
5830{
75b46437 5831 register tree decl, type;
51e29401
RS
5832
5833 /* Validate and default VALUE. */
5834
5835 /* Remove no-op casts from the value. */
cd7a1451 5836 if (value)
874a7be1 5837 STRIP_TYPE_NOPS (value);
51e29401 5838
90374cc2 5839 if (value != 0)
e681c5a1
RS
5840 {
5841 if (TREE_CODE (value) == INTEGER_CST)
25a1019f
RK
5842 {
5843 value = default_conversion (value);
5844 constant_expression_warning (value);
5845 }
e681c5a1
RS
5846 else
5847 {
5848 error ("enumerator value for `%s' not integer constant",
5849 IDENTIFIER_POINTER (name));
5850 value = 0;
5851 }
5852 }
51e29401
RS
5853
5854 /* Default based on previous value. */
5855 /* It should no longer be possible to have NON_LVALUE_EXPR
5856 in the default. */
5857 if (value == 0)
93e3ba4f
RS
5858 {
5859 value = enum_next_value;
5860 if (enum_overflow)
5861 error ("overflow in enumeration values");
5862 }
51e29401
RS
5863
5864 if (pedantic && ! int_fits_type_p (value, integer_type_node))
5865 {
5866 pedwarn ("ANSI C restricts enumerator values to range of `int'");
5867 value = integer_zero_node;
5868 }
5869
5870 /* Set basis for default for next value. */
5871 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
93e3ba4f 5872 enum_overflow = tree_int_cst_lt (enum_next_value, value);
51e29401
RS
5873
5874 /* Now create a declaration for the enum value name. */
5875
75b46437
RS
5876 type = TREE_TYPE (value);
5877 type = type_for_size (MAX (TYPE_PRECISION (type),
5878 TYPE_PRECISION (integer_type_node)),
5879 ((flag_traditional
5880 || TYPE_PRECISION (type) >= TYPE_PRECISION (integer_type_node))
5881 && TREE_UNSIGNED (type)));
5882
5883 decl = build_decl (CONST_DECL, name, type);
51e29401 5884 DECL_INITIAL (decl) = value;
75b46437 5885 TREE_TYPE (value) = type;
51e29401
RS
5886 pushdecl (decl);
5887
8d9bfdc5 5888 return saveable_tree_cons (decl, value, NULL_TREE);
51e29401
RS
5889}
5890\f
5891/* Create the FUNCTION_DECL for a function definition.
5892 DECLSPECS and DECLARATOR are the parts of the declaration;
5893 they describe the function's name and the type it returns,
5894 but twisted together in a fashion that parallels the syntax of C.
5895
5896 This function creates a binding context for the function body
5897 as well as setting up the FUNCTION_DECL in current_function_decl.
5898
5899 Returns 1 on success. If the DECLARATOR is not suitable for a function
5900 (it defines a datum instead), we return 0, which tells
5901 yyparse to report a parse error.
5902
5903 NESTED is nonzero for a function nested within another function. */
5904
5905int
5906start_function (declspecs, declarator, nested)
5907 tree declarator, declspecs;
5908 int nested;
5909{
5910 tree decl1, old_decl;
5911 tree restype;
5415b705 5912 int old_immediate_size_expand = immediate_size_expand;
51e29401
RS
5913
5914 current_function_returns_value = 0; /* Assume, until we see it does. */
5915 current_function_returns_null = 0;
5916 warn_about_return_type = 0;
5917 current_extern_inline = 0;
5918 c_function_varargs = 0;
5919 named_labels = 0;
5920 shadowed_labels = 0;
5921
5415b705
RK
5922 /* Don't expand any sizes in the return type of the function. */
5923 immediate_size_expand = 0;
5924
51e29401
RS
5925 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1);
5926
5927 /* If the declarator is not suitable for a function definition,
5928 cause a syntax error. */
5929 if (decl1 == 0)
5930 return 0;
5931
5932 announce_function (decl1);
5933
5934 if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl1))) == 0)
5935 {
5936 error ("return-type is an incomplete type");
5937 /* Make it return void instead. */
5938 TREE_TYPE (decl1)
5939 = build_function_type (void_type_node,
5940 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
5941 }
5942
5943 if (warn_about_return_type)
5944 warning ("return-type defaults to `int'");
5945
5946 /* Save the parm names or decls from this function's declarator
5947 where store_parm_decls will find them. */
5948 current_function_parms = last_function_parms;
5949 current_function_parm_tags = last_function_parm_tags;
5950
5951 /* Make the init_value nonzero so pushdecl knows this is not tentative.
5952 error_mark_node is replaced below (in poplevel) with the BLOCK. */
5953 DECL_INITIAL (decl1) = error_mark_node;
5954
5955 /* If this definition isn't a prototype and we had a prototype declaration
5956 before, copy the arg type info from that prototype.
5957 But not if what we had before was a builtin function. */
5958 old_decl = lookup_name_current_level (DECL_NAME (decl1));
5959 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
5960 && !DECL_BUILT_IN (old_decl)
fa7d8b92
RK
5961 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5962 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (old_decl))))
51e29401 5963 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
50a9145c
JW
5964 {
5965 TREE_TYPE (decl1) = TREE_TYPE (old_decl);
5966 current_function_prototype_file = DECL_SOURCE_FILE (old_decl);
5967 current_function_prototype_line = DECL_SOURCE_LINE (old_decl);
5968 }
51e29401 5969
6fc7c517
JW
5970 /* If there is no explicit declaration, look for any out-of-scope implicit
5971 declarations. */
5972 if (old_decl == 0)
5973 old_decl = IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1));
5974
51e29401
RS
5975 /* Optionally warn of old-fashioned def with no previous prototype. */
5976 if (warn_strict_prototypes
5977 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
5978 && !(old_decl != 0 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) != 0))
5979 warning ("function declaration isn't a prototype");
5980 /* Optionally warn of any global def with no previous prototype. */
5981 else if (warn_missing_prototypes
5982 && TREE_PUBLIC (decl1)
39ab948e
RS
5983 && !(old_decl != 0 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) != 0)
5984 && strcmp ("main", IDENTIFIER_POINTER (DECL_NAME (decl1))))
51e29401
RS
5985 warning_with_decl (decl1, "no previous prototype for `%s'");
5986 /* Optionally warn of any def with no previous prototype
5987 if the function has already been used. */
5988 else if (warn_missing_prototypes
5989 && old_decl != 0 && TREE_USED (old_decl)
6fc7c517 5990 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
1474fe46
RK
5991 warning_with_decl (decl1,
5992 "`%s' was used with no prototype before its definition");
5993 /* Optionally warn of any global def with no previous declaration. */
5994 else if (warn_missing_declarations
5995 && TREE_PUBLIC (decl1)
5996 && old_decl == 0
5997 && strcmp ("main", IDENTIFIER_POINTER (DECL_NAME (decl1))))
5998 warning_with_decl (decl1, "no previous declaration for `%s'");
5999 /* Optionally warn of any def with no previous declaration
6000 if the function has already been used. */
6001 else if (warn_missing_declarations
6fc7c517
JW
6002 && old_decl != 0 && TREE_USED (old_decl)
6003 && old_decl == IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)))
1474fe46
RK
6004 warning_with_decl (decl1,
6005 "`%s' was used with no declaration before its definition");
51e29401
RS
6006
6007 /* This is a definition, not a reference.
1394aabd 6008 So normally clear DECL_EXTERNAL.
51e29401 6009 However, `extern inline' acts like a declaration
1394aabd
RS
6010 except for defining how to inline. So set DECL_EXTERNAL in that case. */
6011 DECL_EXTERNAL (decl1) = current_extern_inline;
51e29401
RS
6012
6013 /* This function exists in static storage.
6014 (This does not mean `static' in the C sense!) */
6015 TREE_STATIC (decl1) = 1;
6016
6017 /* A nested function is not global. */
6018 if (current_function_decl != 0)
6019 TREE_PUBLIC (decl1) = 0;
6020
6021 /* Record the decl so that the function name is defined.
6022 If we already have a decl for this name, and it is a FUNCTION_DECL,
6023 use the old decl. */
6024
6025 current_function_decl = pushdecl (decl1);
6026
6027 pushlevel (0);
6028 declare_parm_level (1);
6029 current_binding_level->subblocks_tag_transparent = 1;
6030
6031 make_function_rtl (current_function_decl);
6032
6033 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
6034 /* Promote the value to int before returning it. */
24bc4c7f 6035 if (C_PROMOTING_INTEGER_TYPE_P (restype))
42dfa47f
RS
6036 {
6037 /* It retains unsignedness if traditional
6038 or if not really getting wider. */
6039 if (TREE_UNSIGNED (restype)
6040 && (flag_traditional
6041 || (TYPE_PRECISION (restype)
6042 == TYPE_PRECISION (integer_type_node))))
6043 restype = unsigned_type_node;
6044 else
6045 restype = integer_type_node;
6046 }
8d9bfdc5
RK
6047 DECL_RESULT (current_function_decl)
6048 = build_decl (RESULT_DECL, NULL_TREE, restype);
51e29401
RS
6049
6050 if (!nested)
6051 /* Allocate further tree nodes temporarily during compilation
6052 of this function only. */
6053 temporary_allocation ();
6054
6055 /* If this fcn was already referenced via a block-scope `extern' decl
6056 (or an implicit decl), propagate certain information about the usage. */
6057 if (TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (current_function_decl)))
6058 TREE_ADDRESSABLE (current_function_decl) = 1;
6059
5415b705
RK
6060 immediate_size_expand = old_immediate_size_expand;
6061
51e29401
RS
6062 return 1;
6063}
6064
6065/* Record that this function is going to be a varargs function.
6066 This is called before store_parm_decls, which is too early
6067 to call mark_varargs directly. */
6068
6069void
6070c_mark_varargs ()
6071{
6072 c_function_varargs = 1;
6073}
6074\f
6075/* Store the parameter declarations into the current function declaration.
6076 This is called after parsing the parameter declarations, before
6077 digesting the body of the function.
6078
6079 For an old-style definition, modify the function's type
6080 to specify at least the number of arguments. */
6081
6082void
6083store_parm_decls ()
6084{
6085 register tree fndecl = current_function_decl;
6086 register tree parm;
6087
6088 /* This is either a chain of PARM_DECLs (if a prototype was used)
6089 or a list of IDENTIFIER_NODEs (for an old-fashioned C definition). */
6090 tree specparms = current_function_parms;
6091
6092 /* This is a list of types declared among parms in a prototype. */
6093 tree parmtags = current_function_parm_tags;
6094
6095 /* This is a chain of PARM_DECLs from old-style parm declarations. */
6096 register tree parmdecls = getdecls ();
6097
6098 /* This is a chain of any other decls that came in among the parm
6099 declarations. If a parm is declared with enum {foo, bar} x;
6100 then CONST_DECLs for foo and bar are put here. */
6101 tree nonparms = 0;
6102
6103 /* Nonzero if this definition is written with a prototype. */
6104 int prototype = 0;
6105
6106 if (specparms != 0 && TREE_CODE (specparms) != TREE_LIST)
6107 {
6108 /* This case is when the function was defined with an ANSI prototype.
6109 The parms already have decls, so we need not do anything here
6110 except record them as in effect
6111 and complain if any redundant old-style parm decls were written. */
6112
6113 register tree next;
6114 tree others = 0;
6115
6116 prototype = 1;
6117
6118 if (parmdecls != 0)
7a0347ff
RS
6119 {
6120 tree decl, link;
6121
6122 error_with_decl (fndecl,
6123 "parm types given both in parmlist and separately");
6124 /* Get rid of the erroneous decls; don't keep them on
6125 the list of parms, since they might not be PARM_DECLs. */
6126 for (decl = current_binding_level->names;
6127 decl; decl = TREE_CHAIN (decl))
6128 if (DECL_NAME (decl))
6129 IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) = 0;
6130 for (link = current_binding_level->shadowed;
6131 link; link = TREE_CHAIN (link))
6132 IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
6133 current_binding_level->names = 0;
6134 current_binding_level->shadowed = 0;
6135 }
51e29401
RS
6136
6137 specparms = nreverse (specparms);
6138 for (parm = specparms; parm; parm = next)
6139 {
6140 next = TREE_CHAIN (parm);
6141 if (TREE_CODE (parm) == PARM_DECL)
6142 {
6143 if (DECL_NAME (parm) == 0)
6144 error_with_decl (parm, "parameter name omitted");
5fe86b8b 6145 else if (TYPE_MAIN_VARIANT (TREE_TYPE (parm)) == void_type_node)
a4faa7cc
JW
6146 {
6147 error_with_decl (parm, "parameter `%s' declared void");
6148 /* Change the type to error_mark_node so this parameter
6149 will be ignored by assign_parms. */
6150 TREE_TYPE (parm) = error_mark_node;
6151 }
51e29401
RS
6152 pushdecl (parm);
6153 }
6154 else
6155 {
6156 /* If we find an enum constant or a type tag,
6157 put it aside for the moment. */
6158 TREE_CHAIN (parm) = 0;
6159 others = chainon (others, parm);
6160 }
6161 }
6162
6163 /* Get the decls in their original chain order
6164 and record in the function. */
6165 DECL_ARGUMENTS (fndecl) = getdecls ();
6166
6167#if 0
6168 /* If this function takes a variable number of arguments,
6169 add a phony parameter to the end of the parm list,
6170 to represent the position of the first unnamed argument. */
6171 if (TREE_VALUE (tree_last (TYPE_ARG_TYPES (TREE_TYPE (fndecl))))
6172 != void_type_node)
6173 {
6174 tree dummy = build_decl (PARM_DECL, NULL_TREE, void_type_node);
6175 /* Let's hope the address of the unnamed parm
6176 won't depend on its type. */
6177 TREE_TYPE (dummy) = integer_type_node;
6178 DECL_ARG_TYPE (dummy) = integer_type_node;
6179 DECL_ARGUMENTS (fndecl)
6180 = chainon (DECL_ARGUMENTS (fndecl), dummy);
6181 }
6182#endif
6183
6184 /* Now pushdecl the enum constants. */
6185 for (parm = others; parm; parm = next)
6186 {
6187 next = TREE_CHAIN (parm);
6188 if (DECL_NAME (parm) == 0)
6189 ;
5fe86b8b 6190 else if (TYPE_MAIN_VARIANT (TREE_TYPE (parm)) == void_type_node)
51e29401
RS
6191 ;
6192 else if (TREE_CODE (parm) != PARM_DECL)
6193 pushdecl (parm);
6194 }
6195
6196 storetags (chainon (parmtags, gettags ()));
6197 }
6198 else
6199 {
6200 /* SPECPARMS is an identifier list--a chain of TREE_LIST nodes
6201 each with a parm name as the TREE_VALUE.
6202
6203 PARMDECLS is a chain of declarations for parameters.
6204 Warning! It can also contain CONST_DECLs which are not parameters
6205 but are names of enumerators of any enum types
6206 declared among the parameters.
6207
6208 First match each formal parameter name with its declaration.
6209 Associate decls with the names and store the decls
6210 into the TREE_PURPOSE slots. */
6211
6212 for (parm = parmdecls; parm; parm = TREE_CHAIN (parm))
6213 DECL_RESULT (parm) = 0;
6214
6215 for (parm = specparms; parm; parm = TREE_CHAIN (parm))
6216 {
6217 register tree tail, found = NULL;
6218
6219 if (TREE_VALUE (parm) == 0)
6220 {
6221 error_with_decl (fndecl, "parameter name missing from parameter list");
6222 TREE_PURPOSE (parm) = 0;
6223 continue;
6224 }
6225
6226 /* See if any of the parmdecls specifies this parm by name.
6227 Ignore any enumerator decls. */
6228 for (tail = parmdecls; tail; tail = TREE_CHAIN (tail))
6229 if (DECL_NAME (tail) == TREE_VALUE (parm)
6230 && TREE_CODE (tail) == PARM_DECL)
6231 {
6232 found = tail;
6233 break;
6234 }
6235
6236 /* If declaration already marked, we have a duplicate name.
6237 Complain, and don't use this decl twice. */
6238 if (found && DECL_RESULT (found) != 0)
6239 {
6240 error_with_decl (found, "multiple parameters named `%s'");
6241 found = 0;
6242 }
6243
6244 /* If the declaration says "void", complain and ignore it. */
5fe86b8b 6245 if (found && TYPE_MAIN_VARIANT (TREE_TYPE (found)) == void_type_node)
51e29401
RS
6246 {
6247 error_with_decl (found, "parameter `%s' declared void");
6248 TREE_TYPE (found) = integer_type_node;
6249 DECL_ARG_TYPE (found) = integer_type_node;
6250 layout_decl (found, 0);
6251 }
6252
6253 /* Traditionally, a parm declared float is actually a double. */
6254 if (found && flag_traditional
90d56da8 6255 && TYPE_MAIN_VARIANT (TREE_TYPE (found)) == float_type_node)
6d142a10
RS
6256 {
6257 TREE_TYPE (found) = double_type_node;
6258 DECL_ARG_TYPE (found) = double_type_node;
6259 layout_decl (found, 0);
6260 }
51e29401
RS
6261
6262 /* If no declaration found, default to int. */
6263 if (!found)
6264 {
6265 found = build_decl (PARM_DECL, TREE_VALUE (parm),
6266 integer_type_node);
6267 DECL_ARG_TYPE (found) = TREE_TYPE (found);
6268 DECL_SOURCE_LINE (found) = DECL_SOURCE_LINE (fndecl);
6269 DECL_SOURCE_FILE (found) = DECL_SOURCE_FILE (fndecl);
6270 if (extra_warnings)
6271 warning_with_decl (found, "type of `%s' defaults to `int'");
6272 pushdecl (found);
6273 }
6274
6275 TREE_PURPOSE (parm) = found;
6276
6277 /* Mark this decl as "already found" -- see test, above.
6278 It is safe to use DECL_RESULT for this
6279 since it is not used in PARM_DECLs or CONST_DECLs. */
6280 DECL_RESULT (found) = error_mark_node;
6281 }
6282
6283 /* Put anything which is on the parmdecls chain and which is
6284 not a PARM_DECL onto the list NONPARMS. (The types of
6285 non-parm things which might appear on the list include
6286 enumerators and NULL-named TYPE_DECL nodes.) Complain about
6287 any actual PARM_DECLs not matched with any names. */
6288
6289 nonparms = 0;
6290 for (parm = parmdecls; parm; )
6291 {
6292 tree next = TREE_CHAIN (parm);
6293 TREE_CHAIN (parm) = 0;
6294
6295 if (TREE_CODE (parm) != PARM_DECL)
6296 nonparms = chainon (nonparms, parm);
6297 else
6298 {
6299 /* Complain about args with incomplete types. */
6300 if (TYPE_SIZE (TREE_TYPE (parm)) == 0)
6301 {
6302 error_with_decl (parm, "parameter `%s' has incomplete type");
6303 TREE_TYPE (parm) = error_mark_node;
6304 }
6305
6306 if (DECL_RESULT (parm) == 0)
6307 {
6308 error_with_decl (parm,
6309 "declaration for parameter `%s' but no such parameter");
6310 /* Pretend the parameter was not missing.
6311 This gets us to a standard state and minimizes
6312 further error messages. */
6313 specparms
6314 = chainon (specparms,
6315 tree_cons (parm, NULL_TREE, NULL_TREE));
6316 }
6317 }
6318
6319 parm = next;
6320 }
6321
6322 /* Chain the declarations together in the order of the list of names. */
6323 /* Store that chain in the function decl, replacing the list of names. */
6324 parm = specparms;
6325 DECL_ARGUMENTS (fndecl) = 0;
6326 {
6327 register tree last;
6328 for (last = 0; parm; parm = TREE_CHAIN (parm))
6329 if (TREE_PURPOSE (parm))
6330 {
6331 if (last == 0)
6332 DECL_ARGUMENTS (fndecl) = TREE_PURPOSE (parm);
6333 else
6334 TREE_CHAIN (last) = TREE_PURPOSE (parm);
6335 last = TREE_PURPOSE (parm);
6336 TREE_CHAIN (last) = 0;
6337 }
6338 }
6339
6340 /* If there was a previous prototype,
6341 set the DECL_ARG_TYPE of each argument according to
6342 the type previously specified, and report any mismatches. */
6343
6344 if (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
6345 {
6346 register tree type;
6347 for (parm = DECL_ARGUMENTS (fndecl),
6348 type = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
5fe86b8b
RS
6349 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
6350 != void_type_node));
51e29401
RS
6351 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6352 {
6353 if (parm == 0 || type == 0
5fe86b8b 6354 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
51e29401
RS
6355 {
6356 error ("number of arguments doesn't match prototype");
50a9145c
JW
6357 error_with_file_and_line (current_function_prototype_file,
6358 current_function_prototype_line,
6359 "prototype declaration");
51e29401
RS
6360 break;
6361 }
6362 /* Type for passing arg must be consistent
6363 with that declared for the arg. */
ec2343c4 6364 if (! comptypes (DECL_ARG_TYPE (parm), TREE_VALUE (type)))
51e29401 6365 {
b3fc0c98
RS
6366 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6367 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
51e29401 6368 {
ec2343c4
MM
6369 /* Adjust argument to match prototype. E.g. a previous
6370 `int foo(float);' prototype causes
6371 `int foo(x) float x; {...}' to be treated like
6372 `int foo(float x) {...}'. This is particularly
6373 useful for argument types like uid_t. */
6374 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6375#ifdef PROMOTE_PROTOTYPES
fd48df3e
RK
6376 if ((TREE_CODE (TREE_TYPE (parm)) == INTEGER_TYPE
6377 || TREE_CODE (TREE_TYPE (parm)) == ENUMERAL_TYPE)
ec2343c4
MM
6378 && TYPE_PRECISION (TREE_TYPE (parm))
6379 < TYPE_PRECISION (integer_type_node))
6380 DECL_ARG_TYPE (parm) = integer_type_node;
6381#endif
6382 if (pedantic)
50a9145c 6383 {
42f00318 6384 pedwarn ("promoted argument `%s' doesn't match prototype",
50a9145c
JW
6385 IDENTIFIER_POINTER (DECL_NAME (parm)));
6386 warning_with_file_and_line
6387 (current_function_prototype_file,
6388 current_function_prototype_line,
6389 "prototype declaration");
6390 }
51e29401 6391 }
ec2343c4
MM
6392 /* If -traditional, allow `int' argument to match
6393 `unsigned' prototype. */
6394 else if (! (flag_traditional
90d56da8
RS
6395 && TYPE_MAIN_VARIANT (TREE_TYPE (parm)) == integer_type_node
6396 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == unsigned_type_node))
50a9145c
JW
6397 {
6398 error ("argument `%s' doesn't match prototype",
6399 IDENTIFIER_POINTER (DECL_NAME (parm)));
6400 error_with_file_and_line (current_function_prototype_file,
6401 current_function_prototype_line,
6402 "prototype declaration");
6403 }
51e29401
RS
6404 }
6405 }
6406 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6407 }
6408
6409 /* Otherwise, create a prototype that would match. */
6410
6411 else
6412 {
b8e605ab 6413 tree actual = 0, last = 0, type;
51e29401
RS
6414
6415 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6416 {
8d9bfdc5
RK
6417 type = perm_tree_cons (NULL_TREE, DECL_ARG_TYPE (parm),
6418 NULL_TREE);
51e29401
RS
6419 if (last)
6420 TREE_CHAIN (last) = type;
6421 else
6422 actual = type;
6423 last = type;
6424 }
8d9bfdc5 6425 type = perm_tree_cons (NULL_TREE, void_type_node, NULL_TREE);
51e29401
RS
6426 if (last)
6427 TREE_CHAIN (last) = type;
6428 else
6429 actual = type;
6430
c138f328
RS
6431 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6432 of the type of this function, but we need to avoid having this
6433 affect the types of other similarly-typed functions, so we must
6434 first force the generation of an identical (but separate) type
6435 node for the relevant function type. The new node we create
6436 will be a variant of the main variant of the original function
6437 type. */
6438
929f3671 6439 TREE_TYPE (fndecl) = build_type_copy (TREE_TYPE (fndecl));
c138f328 6440
51e29401
RS
6441 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6442 }
6443
6444 /* Now store the final chain of decls for the arguments
6445 as the decl-chain of the current lexical scope.
6446 Put the enumerators in as well, at the front so that
6447 DECL_ARGUMENTS is not modified. */
6448
6449 storedecls (chainon (nonparms, DECL_ARGUMENTS (fndecl)));
6450 }
6451
6452 /* Make sure the binding level for the top of the function body
6453 gets a BLOCK if there are any in the function.
6454 Otherwise, the dbx output is wrong. */
6455
6456 keep_next_if_subblocks = 1;
6457
6458 /* ??? This might be an improvement,
6459 but needs to be thought about some more. */
6460#if 0
6461 keep_next_level_flag = 1;
6462#endif
6463
6464 /* Write a record describing this function definition to the prototypes
6465 file (if requested). */
6466
6467 gen_aux_info_record (fndecl, 1, 0, prototype);
6468
6469 /* Initialize the RTL code for the function. */
6470
6471 init_function_start (fndecl, input_filename, lineno);
6472
6473 /* If this is a varargs function, inform function.c. */
6474
6475 if (c_function_varargs)
6476 mark_varargs ();
6477
b032c74c 6478 /* Declare __FUNCTION__ and __PRETTY_FUNCTION__ for this function. */
7da551a2
RS
6479
6480 declare_function_name ();
93e3ba4f 6481
51e29401
RS
6482 /* Set up parameters and prepare for return, for the function. */
6483
6484 expand_function_start (fndecl, 0);
6485
6486 /* If this function is `main', emit a call to `__main'
6487 to run global initializers, etc. */
6488 if (DECL_NAME (fndecl)
6489 && strcmp (IDENTIFIER_POINTER (DECL_NAME (fndecl)), "main") == 0
6490 && DECL_CONTEXT (fndecl) == NULL_TREE)
6491 expand_main_function ();
6492}
6493\f
6494/* SPECPARMS is an identifier list--a chain of TREE_LIST nodes
6495 each with a parm name as the TREE_VALUE. A null pointer as TREE_VALUE
6496 stands for an ellipsis in the identifier list.
6497
6498 PARMLIST is the data returned by get_parm_info for the
6499 parmlist that follows the semicolon.
6500
6501 We return a value of the same sort that get_parm_info returns,
6502 except that it describes the combination of identifiers and parmlist. */
6503
6504tree
6505combine_parm_decls (specparms, parmlist, void_at_end)
6506 tree specparms, parmlist;
6507 int void_at_end;
6508{
6509 register tree fndecl = current_function_decl;
6510 register tree parm;
6511
6512 tree parmdecls = TREE_PURPOSE (parmlist);
6513
6514 /* This is a chain of any other decls that came in among the parm
6515 declarations. They were separated already by get_parm_info,
6516 so we just need to keep them separate. */
6517 tree nonparms = TREE_VALUE (parmlist);
6518
6519 tree types = 0;
6520
6521 for (parm = parmdecls; parm; parm = TREE_CHAIN (parm))
6522 DECL_RESULT (parm) = 0;
6523
6524 for (parm = specparms; parm; parm = TREE_CHAIN (parm))
6525 {
6526 register tree tail, found = NULL;
6527
6528 /* See if any of the parmdecls specifies this parm by name. */
6529 for (tail = parmdecls; tail; tail = TREE_CHAIN (tail))
6530 if (DECL_NAME (tail) == TREE_VALUE (parm))
6531 {
6532 found = tail;
6533 break;
6534 }
6535
6536 /* If declaration already marked, we have a duplicate name.
6537 Complain, and don't use this decl twice. */
6538 if (found && DECL_RESULT (found) != 0)
6539 {
6540 error_with_decl (found, "multiple parameters named `%s'");
6541 found = 0;
6542 }
6543
6544 /* If the declaration says "void", complain and ignore it. */
5fe86b8b 6545 if (found && TYPE_MAIN_VARIANT (TREE_TYPE (found)) == void_type_node)
51e29401
RS
6546 {
6547 error_with_decl (found, "parameter `%s' declared void");
6548 TREE_TYPE (found) = integer_type_node;
6549 DECL_ARG_TYPE (found) = integer_type_node;
6550 layout_decl (found, 0);
6551 }
6552
6553 /* Traditionally, a parm declared float is actually a double. */
6554 if (found && flag_traditional
90d56da8 6555 && TYPE_MAIN_VARIANT (TREE_TYPE (found)) == float_type_node)
6d142a10
RS
6556 {
6557 TREE_TYPE (found) = double_type_node;
6558 DECL_ARG_TYPE (found) = double_type_node;
6559 layout_decl (found, 0);
6560 }
51e29401
RS
6561
6562 /* If no declaration found, default to int. */
6563 if (!found)
6564 {
6565 found = build_decl (PARM_DECL, TREE_VALUE (parm),
6566 integer_type_node);
6567 DECL_ARG_TYPE (found) = TREE_TYPE (found);
6568 DECL_SOURCE_LINE (found) = DECL_SOURCE_LINE (fndecl);
6569 DECL_SOURCE_FILE (found) = DECL_SOURCE_FILE (fndecl);
6b2a374b 6570 error_with_decl (found, "type of parameter `%s' is not declared");
51e29401
RS
6571 pushdecl (found);
6572 }
6573
6574 TREE_PURPOSE (parm) = found;
6575
6576 /* Mark this decl as "already found" -- see test, above.
6577 It is safe to use DECL_RESULT for this
6578 since it is not used in PARM_DECLs or CONST_DECLs. */
6579 DECL_RESULT (found) = error_mark_node;
6580 }
6581
6582 /* Complain about any actual PARM_DECLs not matched with any names. */
6583
6584 for (parm = parmdecls; parm; )
6585 {
6586 tree next = TREE_CHAIN (parm);
6587 TREE_CHAIN (parm) = 0;
6588
6589 /* Complain about args with incomplete types. */
6590 if (TYPE_SIZE (TREE_TYPE (parm)) == 0)
6591 {
6592 error_with_decl (parm, "parameter `%s' has incomplete type");
6593 TREE_TYPE (parm) = error_mark_node;
6594 }
6595
6596 if (DECL_RESULT (parm) == 0)
6597 {
6598 error_with_decl (parm,
6599 "declaration for parameter `%s' but no such parameter");
6600 /* Pretend the parameter was not missing.
6601 This gets us to a standard state and minimizes
6602 further error messages. */
6603 specparms
6604 = chainon (specparms,
6605 tree_cons (parm, NULL_TREE, NULL_TREE));
6606 }
6607
6608 parm = next;
6609 }
6610
6611 /* Chain the declarations together in the order of the list of names.
6612 At the same time, build up a list of their types, in reverse order. */
6613
6614 parm = specparms;
6615 parmdecls = 0;
6616 {
6617 register tree last;
6618 for (last = 0; parm; parm = TREE_CHAIN (parm))
6619 if (TREE_PURPOSE (parm))
6620 {
6621 if (last == 0)
6622 parmdecls = TREE_PURPOSE (parm);
6623 else
6624 TREE_CHAIN (last) = TREE_PURPOSE (parm);
6625 last = TREE_PURPOSE (parm);
6626 TREE_CHAIN (last) = 0;
6627
6628 types = saveable_tree_cons (NULL_TREE, TREE_TYPE (parm), types);
6629 }
6630 }
6631
6632 if (void_at_end)
6633 return saveable_tree_cons (parmdecls, nonparms,
6634 nreverse (saveable_tree_cons (NULL_TREE, void_type_node, types)));
6635
6636 return saveable_tree_cons (parmdecls, nonparms, nreverse (types));
6637}
6638\f
6639/* Finish up a function declaration and compile that function
6640 all the way to assembler language output. The free the storage
6641 for the function definition.
6642
6643 This is called after parsing the body of the function definition.
6644
6645 NESTED is nonzero if the function being finished is nested in another. */
6646
6647void
6648finish_function (nested)
6649 int nested;
6650{
6651 register tree fndecl = current_function_decl;
6652
6653/* TREE_READONLY (fndecl) = 1;
6654 This caused &foo to be of type ptr-to-const-function
6655 which then got a warning when stored in a ptr-to-function variable. */
6656
6657 poplevel (1, 0, 1);
960a2eb1 6658 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
51e29401
RS
6659
6660 /* Must mark the RESULT_DECL as being in this function. */
6661
6662 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6663
6664 /* Obey `register' declarations if `setjmp' is called in this fn. */
6665 if (flag_traditional && current_function_calls_setjmp)
6666 {
6667 setjmp_protect (DECL_INITIAL (fndecl));
6668 setjmp_protect_args ();
6669 }
6670
6671#ifdef DEFAULT_MAIN_RETURN
6672 if (! strcmp (IDENTIFIER_POINTER (DECL_NAME (fndecl)), "main"))
6673 {
90d56da8
RS
6674 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6675 != integer_type_node)
8e077183
RS
6676 warning_with_decl (fndecl, "return type of `%s' is not `int'");
6677 else
6678 {
6679 /* Make it so that `main' always returns success by default. */
6680 DEFAULT_MAIN_RETURN;
6681 }
51e29401
RS
6682 }
6683#endif
6684
6685 /* Generate rtl for function exit. */
0e5eedfe 6686 expand_function_end (input_filename, lineno, 0);
51e29401
RS
6687
6688 /* So we can tell if jump_optimize sets it to 1. */
6689 can_reach_end = 0;
6690
6691 /* Run the optimizers and output the assembler code for this function. */
6692 rest_of_compilation (fndecl);
6693
6694 current_function_returns_null |= can_reach_end;
6695
6696 if (TREE_THIS_VOLATILE (fndecl) && current_function_returns_null)
11433f42 6697 warning ("`noreturn' function does return");
42dfa47f 6698 else if (warn_return_type && can_reach_end
51e29401
RS
6699 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl))) != void_type_node)
6700 /* If this function returns non-void and control can drop through,
6701 complain. */
6702 warning ("control reaches end of non-void function");
6703 /* With just -W, complain only if function returns both with
6704 and without a value. */
6705 else if (extra_warnings
6706 && current_function_returns_value && current_function_returns_null)
6707 warning ("this function may return with or without a value");
6708
739d15ab
RK
6709 /* If requested, warn about function definitions where the function will
6710 return a value (usually of some struct or union type) which itself will
6711 take up a lot of stack space. */
6712
6713 if (warn_larger_than && !DECL_EXTERNAL (fndecl) && TREE_TYPE (fndecl))
6714 {
6715 register tree ret_type = TREE_TYPE (TREE_TYPE (fndecl));
6716
6717 if (ret_type)
6718 {
6719 register tree ret_type_size = TYPE_SIZE (ret_type);
6720
6721 if (TREE_CODE (ret_type_size) == INTEGER_CST)
6722 {
6723 unsigned units
6724 = TREE_INT_CST_LOW (ret_type_size) / BITS_PER_UNIT;
6725
6726 if (units > larger_than_size)
6727 warning_with_decl (fndecl,
6728 "size of return value of `%s' is %u bytes",
6729 units);
6730 }
6731 }
6732 }
6733
51e29401
RS
6734 /* Free all the tree nodes making up this function. */
6735 /* Switch back to allocating nodes permanently
6736 until we start another function. */
6737 if (! nested)
3e755d23 6738 permanent_allocation (1);
51e29401
RS
6739
6740 if (DECL_SAVED_INSNS (fndecl) == 0 && ! nested)
6741 {
6742 /* Stop pointing to the local nodes about to be freed. */
6743 /* But DECL_INITIAL must remain nonzero so we know this
6744 was an actual function definition. */
6745 /* For a nested function, this is done in pop_c_function_context. */
708e813b
RS
6746 /* If rest_of_compilation set this to 0, leave it 0. */
6747 if (DECL_INITIAL (fndecl) != 0)
6748 DECL_INITIAL (fndecl) = error_mark_node;
51e29401
RS
6749 DECL_ARGUMENTS (fndecl) = 0;
6750 }
6751
6752 if (! nested)
6753 {
6754 /* Let the error reporting routines know that we're outside a
6755 function. For a nested function, this value is used in
6756 pop_c_function_context and then reset via pop_function_context. */
6757 current_function_decl = NULL;
6758 }
6759}
6760\f
6761/* Save and restore the variables in this file and elsewhere
6762 that keep track of the progress of compilation of the current function.
6763 Used for nested functions. */
6764
6765struct c_function
6766{
6767 struct c_function *next;
51e29401
RS
6768 tree named_labels;
6769 tree shadowed_labels;
6770 int returns_value;
6771 int returns_null;
6772 int warn_about_return_type;
6773 int extern_inline;
6774 struct binding_level *binding_level;
6775};
6776
6777struct c_function *c_function_chain;
6778
6779/* Save and reinitialize the variables
6780 used during compilation of a C function. */
6781
6782void
6783push_c_function_context ()
6784{
6785 struct c_function *p
6786 = (struct c_function *) xmalloc (sizeof (struct c_function));
6787
ec2343c4
MM
6788 if (pedantic)
6789 pedwarn ("ANSI C forbids nested functions");
6790
51e29401
RS
6791 push_function_context ();
6792
6793 p->next = c_function_chain;
6794 c_function_chain = p;
6795
51e29401
RS
6796 p->named_labels = named_labels;
6797 p->shadowed_labels = shadowed_labels;
6798 p->returns_value = current_function_returns_value;
6799 p->returns_null = current_function_returns_null;
6800 p->warn_about_return_type = warn_about_return_type;
6801 p->extern_inline = current_extern_inline;
6802 p->binding_level = current_binding_level;
6803}
6804
6805/* Restore the variables used during compilation of a C function. */
6806
6807void
6808pop_c_function_context ()
6809{
6810 struct c_function *p = c_function_chain;
6811 tree link;
6812
6813 /* Bring back all the labels that were shadowed. */
6814 for (link = shadowed_labels; link; link = TREE_CHAIN (link))
6815 if (DECL_NAME (TREE_VALUE (link)) != 0)
6816 IDENTIFIER_LABEL_VALUE (DECL_NAME (TREE_VALUE (link)))
6817 = TREE_VALUE (link);
6818
6819 if (DECL_SAVED_INSNS (current_function_decl) == 0)
6820 {
6821 /* Stop pointing to the local nodes about to be freed. */
6822 /* But DECL_INITIAL must remain nonzero so we know this
6823 was an actual function definition. */
6824 DECL_INITIAL (current_function_decl) = error_mark_node;
6825 DECL_ARGUMENTS (current_function_decl) = 0;
6826 }
6827
6828 pop_function_context ();
6829
6830 c_function_chain = p->next;
6831
51e29401
RS
6832 named_labels = p->named_labels;
6833 shadowed_labels = p->shadowed_labels;
6834 current_function_returns_value = p->returns_value;
6835 current_function_returns_null = p->returns_null;
6836 warn_about_return_type = p->warn_about_return_type;
6837 current_extern_inline = p->extern_inline;
6838 current_binding_level = p->binding_level;
6839
6840 free (p);
6841}
37105beb
JM
6842
6843/* integrate_decl_tree calls this function, but since we don't use the
6844 DECL_LANG_SPECIFIC field, this is a no-op. */
6845
6846void
6847copy_lang_decl (node)
6848 tree node;
6849{
6850}
This page took 1.15806 seconds and 5 git commands to generate.