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