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