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