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