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