* ggc-page.c (struct page_entry): Remove save_num_free_objects.
[official-gcc.git] / gcc / c-decl.c
blobe64f0a7505800e955d9262839431916437d8f833
1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988, 92-98, 1999 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 /* Process declarations and symbol lookup for C front end.
23 Also constructs types; the standard scalar types at initialization,
24 and structure, union, array and enum types when they are declared. */
26 /* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
29 #include "config.h"
30 #include "system.h"
31 #include "tree.h"
32 #include "flags.h"
33 #include "function.h"
34 #include "output.h"
35 #include "c-tree.h"
36 #include "c-lex.h"
37 #include "toplev.h"
38 #include "defaults.h"
39 #include "ggc.h"
41 #if USE_CPPLIB
42 #include "cpplib.h"
43 extern cpp_reader parse_in;
44 #endif
46 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
47 enum decl_context
48 { NORMAL, /* Ordinary declaration */
49 FUNCDEF, /* Function definition */
50 PARM, /* Declaration of parm before function body */
51 FIELD, /* Declaration inside struct or union */
52 BITFIELD, /* Likewise but with specified width */
53 TYPENAME}; /* Typename (inside cast or sizeof) */
55 /* We let tm.h override the types used here, to handle trivial differences
56 such as the choice of unsigned int or long unsigned int for size_t.
57 When machines start needing nontrivial differences in the size type,
58 it would be best to do something here to figure out automatically
59 from other information what type to use. */
61 #ifndef SIZE_TYPE
62 #define SIZE_TYPE "long unsigned int"
63 #endif
65 #ifndef PTRDIFF_TYPE
66 #define PTRDIFF_TYPE "long int"
67 #endif
69 #ifndef WCHAR_TYPE
70 #define WCHAR_TYPE "int"
71 #endif
73 /* Do GC. */
74 int ggc_p = 1;
76 /* Nonzero if we have seen an invalid cross reference
77 to a struct, union, or enum, but not yet printed the message. */
79 tree pending_invalid_xref;
80 /* File and line to appear in the eventual error message. */
81 char *pending_invalid_xref_file;
82 int pending_invalid_xref_line;
84 /* While defining an enum type, this is 1 plus the last enumerator
85 constant value. Note that will do not have to save this or `enum_overflow'
86 around nested function definition since such a definition could only
87 occur in an enum value expression and we don't use these variables in
88 that case. */
90 static tree enum_next_value;
92 /* Nonzero means that there was overflow computing enum_next_value. */
94 static int enum_overflow;
96 /* Parsing a function declarator leaves a list of parameter names
97 or a chain or parameter decls here. */
99 static tree last_function_parms;
101 /* Parsing a function declarator leaves here a chain of structure
102 and enum types declared in the parmlist. */
104 static tree last_function_parm_tags;
106 /* After parsing the declarator that starts a function definition,
107 `start_function' puts here the list of parameter names or chain of decls.
108 `store_parm_decls' finds it here. */
110 static tree current_function_parms;
112 /* Similar, for last_function_parm_tags. */
113 static tree current_function_parm_tags;
115 /* Similar, for the file and line that the prototype came from if this is
116 an old-style definition. */
117 static char *current_function_prototype_file;
118 static int current_function_prototype_line;
120 /* A list (chain of TREE_LIST nodes) of all LABEL_DECLs in the function
121 that have names. Here so we can clear out their names' definitions
122 at the end of the function. */
124 static tree named_labels;
126 /* A list of LABEL_DECLs from outer contexts that are currently shadowed. */
128 static tree shadowed_labels;
130 /* Nonzero when store_parm_decls is called indicates a varargs function.
131 Value not meaningful after store_parm_decls. */
133 static int c_function_varargs;
135 /* The FUNCTION_DECL for the function currently being compiled,
136 or 0 if between functions. */
137 tree current_function_decl;
139 /* Set to 0 at beginning of a function definition, set to 1 if
140 a return statement that specifies a return value is seen. */
142 int current_function_returns_value;
144 /* Set to 0 at beginning of a function definition, set to 1 if
145 a return statement with no argument is seen. */
147 int current_function_returns_null;
149 /* Set to nonzero by `grokdeclarator' for a function
150 whose return type is defaulted, if warnings for this are desired. */
152 static int warn_about_return_type;
154 /* Nonzero when starting a function declared `extern inline'. */
156 static int current_extern_inline;
158 /* For each binding contour we allocate a binding_level structure
159 * which records the names defined in that contour.
160 * Contours include:
161 * 0) the global one
162 * 1) one for each function definition,
163 * where internal declarations of the parameters appear.
164 * 2) one for each compound statement,
165 * to record its declarations.
167 * The current meaning of a name can be found by searching the levels from
168 * the current one out to the global one.
171 /* Note that the information in the `names' component of the global contour
172 is duplicated in the IDENTIFIER_GLOBAL_VALUEs of all identifiers. */
174 struct binding_level
176 /* A chain of _DECL nodes for all variables, constants, functions,
177 and typedef types. These are in the reverse of the order supplied.
179 tree names;
181 /* A list of structure, union and enum definitions,
182 * for looking up tag names.
183 * It is a chain of TREE_LIST nodes, each of whose TREE_PURPOSE is a name,
184 * or NULL_TREE; and whose TREE_VALUE is a RECORD_TYPE, UNION_TYPE,
185 * or ENUMERAL_TYPE node.
187 tree tags;
189 /* For each level, a list of shadowed outer-level local definitions
190 to be restored when this level is popped.
191 Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
192 whose TREE_VALUE is its old definition (a kind of ..._DECL node). */
193 tree shadowed;
195 /* For each level (except not the global one),
196 a chain of BLOCK nodes for all the levels
197 that were entered and exited one level down. */
198 tree blocks;
200 /* The BLOCK node for this level, if one has been preallocated.
201 If 0, the BLOCK is allocated (if needed) when the level is popped. */
202 tree this_block;
204 /* The binding level which this one is contained in (inherits from). */
205 struct binding_level *level_chain;
207 /* Nonzero for the level that holds the parameters of a function. */
208 char parm_flag;
210 /* Nonzero if this level "doesn't exist" for tags. */
211 char tag_transparent;
213 /* Nonzero if sublevels of this level "don't exist" for tags.
214 This is set in the parm level of a function definition
215 while reading the function body, so that the outermost block
216 of the function body will be tag-transparent. */
217 char subblocks_tag_transparent;
219 /* Nonzero means make a BLOCK for this level regardless of all else. */
220 char keep;
222 /* Nonzero means make a BLOCK if this level has any subblocks. */
223 char keep_if_subblocks;
225 /* Number of decls in `names' that have incomplete
226 structure or union types. */
227 int n_incomplete;
229 /* A list of decls giving the (reversed) specified order of parms,
230 not including any forward-decls in the parmlist.
231 This is so we can put the parms in proper order for assign_parms. */
232 tree parm_order;
235 #define NULL_BINDING_LEVEL (struct binding_level *) NULL
237 /* The binding level currently in effect. */
239 static struct binding_level *current_binding_level;
241 /* A chain of binding_level structures awaiting reuse. */
243 static struct binding_level *free_binding_level;
245 /* The outermost binding level, for names of file scope.
246 This is created when the compiler is started and exists
247 through the entire run. */
249 static struct binding_level *global_binding_level;
251 /* Binding level structures are initialized by copying this one. */
253 static struct binding_level clear_binding_level
254 = {NULL, NULL, NULL, NULL, NULL, NULL_BINDING_LEVEL, 0, 0, 0, 0, 0, 0,
255 NULL};
257 /* Nonzero means unconditionally make a BLOCK for the next level pushed. */
259 static int keep_next_level_flag;
261 /* Nonzero means make a BLOCK for the next level pushed
262 if it has subblocks. */
264 static int keep_next_if_subblocks;
266 /* The chain of outer levels of label scopes.
267 This uses the same data structure used for binding levels,
268 but it works differently: each link in the chain records
269 saved values of named_labels and shadowed_labels for
270 a label binding level outside the current one. */
272 static struct binding_level *label_level_chain;
274 /* Functions called automatically at the beginning and end of execution. */
276 tree static_ctors, static_dtors;
278 /* Forward declarations. */
280 static struct binding_level * make_binding_level PROTO((void));
281 static void mark_binding_level PROTO((void *));
282 static void clear_limbo_values PROTO((tree));
283 static int duplicate_decls PROTO((tree, tree, int));
284 static int redeclaration_error_message PROTO((tree, tree));
285 static void storedecls PROTO((tree));
286 static void storetags PROTO((tree));
287 static tree lookup_tag PROTO((enum tree_code, tree,
288 struct binding_level *, int));
289 static tree lookup_tag_reverse PROTO((tree));
290 static tree grokdeclarator PROTO((tree, tree, enum decl_context,
291 int));
292 static tree grokparms PROTO((tree, int));
293 static void layout_array_type PROTO((tree));
295 /* C-specific option variables. */
297 /* Nonzero means allow type mismatches in conditional expressions;
298 just make their values `void'. */
300 int flag_cond_mismatch;
302 /* Nonzero means give `double' the same size as `float'. */
304 int flag_short_double;
306 /* Nonzero means give `wchar_t' the same size as `short'. */
308 int flag_short_wchar;
310 /* Nonzero means don't recognize the keyword `asm'. */
312 int flag_no_asm;
314 /* Nonzero means don't recognize any builtin functions. */
316 int flag_no_builtin;
318 /* Nonzero means don't recognize the non-ANSI builtin functions.
319 -ansi sets this. */
321 int flag_no_nonansi_builtin;
323 /* Nonzero means do some things the same way PCC does. */
325 int flag_traditional;
327 /* Nonzero means use the ISO C9x dialect of C. */
329 int flag_isoc9x = 0;
331 /* Nonzero means that we have builtin functions, and main is an int */
333 int flag_hosted = 1;
335 /* Nonzero means to allow single precision math even if we're generally
336 being traditional. */
337 int flag_allow_single_precision = 0;
339 /* Nonzero means to treat bitfields as signed unless they say `unsigned'. */
341 int flag_signed_bitfields = 1;
342 int explicit_flag_signed_bitfields = 0;
344 /* Nonzero means warn about use of implicit int. */
346 int warn_implicit_int;
348 /* Nonzero means warn about usage of long long when `-pedantic'. */
350 int warn_long_long = 1;
352 /* Nonzero means message about use of implicit function declarations;
353 1 means warning; 2 means error. */
355 int mesg_implicit_function_declaration;
357 /* Nonzero means give string constants the type `const char *'
358 to get extra warnings from them. These warnings will be too numerous
359 to be useful, except in thoroughly ANSIfied programs. */
361 int flag_const_strings;
363 /* Nonzero means warn about pointer casts that can drop a type qualifier
364 from the pointer target type. */
366 int warn_cast_qual;
368 /* Nonzero means warn when casting a function call to a type that does
369 not match the return type (e.g. (float)sqrt() or (anything*)malloc()
370 when there is no previous declaration of sqrt or malloc. */
372 int warn_bad_function_cast;
374 /* Warn about functions which might be candidates for attribute noreturn. */
376 int warn_missing_noreturn;
378 /* Warn about traditional constructs whose meanings changed in ANSI C. */
380 int warn_traditional;
382 /* Nonzero means warn about sizeof(function) or addition/subtraction
383 of function pointers. */
385 int warn_pointer_arith;
387 /* Nonzero means warn for non-prototype function decls
388 or non-prototyped defs without previous prototype. */
390 int warn_strict_prototypes;
392 /* Nonzero means warn for any global function def
393 without separate previous prototype decl. */
395 int warn_missing_prototypes;
397 /* Nonzero means warn for any global function def
398 without separate previous decl. */
400 int warn_missing_declarations;
402 /* Nonzero means warn about multiple (redundant) decls for the same single
403 variable or function. */
405 int warn_redundant_decls = 0;
407 /* Nonzero means warn about extern declarations of objects not at
408 file-scope level and about *all* declarations of functions (whether
409 extern or static) not at file-scope level. Note that we exclude
410 implicit function declarations. To get warnings about those, use
411 -Wimplicit. */
413 int warn_nested_externs = 0;
415 /* Warn about *printf or *scanf format/argument anomalies. */
417 int warn_format;
419 /* Warn about a subscript that has type char. */
421 int warn_char_subscripts = 0;
423 /* Warn if a type conversion is done that might have confusing results. */
425 int warn_conversion;
427 /* Warn if adding () is suggested. */
429 int warn_parentheses;
431 /* Warn if initializer is not completely bracketed. */
433 int warn_missing_braces;
435 /* Warn if main is suspicious. */
437 int warn_main;
439 /* Warn about #pragma directives that are not recognised. */
441 int warn_unknown_pragmas = 0; /* Tri state variable. */
443 /* Warn about comparison of signed and unsigned values.
444 If -1, neither -Wsign-compare nor -Wno-sign-compare has been specified. */
446 int warn_sign_compare = -1;
448 /* Warn about testing equality of floating point numbers. */
450 int warn_float_equal = 0;
452 /* Nonzero means warn about use of multicharacter literals. */
454 int warn_multichar = 1;
456 /* Nonzero means `$' can be in an identifier. */
458 #ifndef DOLLARS_IN_IDENTIFIERS
459 #define DOLLARS_IN_IDENTIFIERS 1
460 #endif
461 int dollars_in_ident = DOLLARS_IN_IDENTIFIERS;
463 /* Decode the string P as a language-specific option for C.
464 Return the number of strings consumed. Should not complain
465 if it does not recognise the option. */
468 c_decode_option (argc, argv)
469 int argc ATTRIBUTE_UNUSED;
470 char **argv;
472 int strings_processed;
473 char *p = argv[0];
474 #if USE_CPPLIB
475 strings_processed = cpp_handle_option (&parse_in, argc, argv);
476 #else
477 strings_processed = 0;
478 #endif /* ! USE_CPPLIB */
480 if (!strcmp (p, "-ftraditional") || !strcmp (p, "-traditional"))
482 flag_traditional = 1;
483 flag_writable_strings = 1;
485 else if (!strcmp (p, "-fallow-single-precision"))
486 flag_allow_single_precision = 1;
487 else if (!strcmp (p, "-fhosted") || !strcmp (p, "-fno-freestanding"))
489 flag_hosted = 1;
490 flag_no_builtin = 0;
492 else if (!strcmp (p, "-ffreestanding") || !strcmp (p, "-fno-hosted"))
494 flag_hosted = 0;
495 flag_no_builtin = 1;
496 /* warn_main will be 2 if set by -Wall, 1 if set by -Wmain */
497 if (warn_main == 2)
498 warn_main = 0;
500 else if (!strcmp (p, "-fnotraditional") || !strcmp (p, "-fno-traditional"))
502 flag_traditional = 0;
503 flag_writable_strings = 0;
505 else if (!strncmp (p, "-std=", 5))
507 /* Select the appropriate language standard. We currently
508 recognize:
509 -std=iso9899:1990 same as -ansi
510 -std=iso9899:199409 ISO C as modified in amend. 1
511 -std=iso9899:199x ISO C 9x
512 -std=c89 same as -std=iso9899:1990
513 -std=c9x same as -std=iso9899:199x
514 -std=gnu89 default, iso9899:1990 + gnu extensions
515 -std=gnu9x iso9899:199x + gnu extensions
517 const char *argstart = &p[5];
519 if (!strcmp (argstart, "iso9899:1990")
520 || !strcmp (argstart, "c89"))
522 iso_1990:
523 flag_traditional = 0;
524 flag_writable_strings = 0;
525 flag_no_asm = 1;
526 flag_no_nonansi_builtin = 1;
527 flag_isoc9x = 0;
529 else if (!strcmp (argstart, "iso9899:199409"))
531 /* ??? The changes since ISO C 1990 are not supported. */
532 goto iso_1990;
534 else if (!strcmp (argstart, "iso9899:199x")
535 || !strcmp (argstart, "c9x"))
537 flag_traditional = 0;
538 flag_writable_strings = 0;
539 flag_no_asm = 1;
540 flag_no_nonansi_builtin = 1;
541 flag_isoc9x = 1;
543 else if (!strcmp (argstart, "gnu89"))
545 flag_traditional = 0;
546 flag_writable_strings = 0;
547 flag_no_asm = 0;
548 flag_no_nonansi_builtin = 0;
549 flag_isoc9x = 0;
551 else if (!strcmp (argstart, "gnu9x"))
553 flag_traditional = 0;
554 flag_writable_strings = 0;
555 flag_no_asm = 0;
556 flag_no_nonansi_builtin = 0;
557 flag_isoc9x = 1;
559 else
560 error ("unknown C standard `%s'", argstart);
562 else if (!strcmp (p, "-fdollars-in-identifiers"))
563 dollars_in_ident = 1;
564 else if (!strcmp (p, "-fno-dollars-in-identifiers"))
565 dollars_in_ident = 0;
566 else if (!strcmp (p, "-fsigned-char"))
567 flag_signed_char = 1;
568 else if (!strcmp (p, "-funsigned-char"))
569 flag_signed_char = 0;
570 else if (!strcmp (p, "-fno-signed-char"))
571 flag_signed_char = 0;
572 else if (!strcmp (p, "-fno-unsigned-char"))
573 flag_signed_char = 1;
574 else if (!strcmp (p, "-fsigned-bitfields")
575 || !strcmp (p, "-fno-unsigned-bitfields"))
577 flag_signed_bitfields = 1;
578 explicit_flag_signed_bitfields = 1;
580 else if (!strcmp (p, "-funsigned-bitfields")
581 || !strcmp (p, "-fno-signed-bitfields"))
583 flag_signed_bitfields = 0;
584 explicit_flag_signed_bitfields = 1;
586 else if (!strcmp (p, "-fshort-enums"))
587 flag_short_enums = 1;
588 else if (!strcmp (p, "-fno-short-enums"))
589 flag_short_enums = 0;
590 else if (!strcmp (p, "-fshort-wchar"))
591 flag_short_wchar = 1;
592 else if (!strcmp (p, "-fno-short-wchar"))
593 flag_short_wchar = 0;
594 else if (!strcmp (p, "-fcond-mismatch"))
595 flag_cond_mismatch = 1;
596 else if (!strcmp (p, "-fno-cond-mismatch"))
597 flag_cond_mismatch = 0;
598 else if (!strcmp (p, "-fshort-double"))
599 flag_short_double = 1;
600 else if (!strcmp (p, "-fno-short-double"))
601 flag_short_double = 0;
602 else if (!strcmp (p, "-fasm"))
603 flag_no_asm = 0;
604 else if (!strcmp (p, "-fno-asm"))
605 flag_no_asm = 1;
606 else if (!strcmp (p, "-fbuiltin"))
607 flag_no_builtin = 0;
608 else if (!strcmp (p, "-fno-builtin"))
609 flag_no_builtin = 1;
610 else if (!strcmp (p, "-ansi"))
611 goto iso_1990;
612 else if (!strcmp (p, "-Werror-implicit-function-declaration"))
613 mesg_implicit_function_declaration = 2;
614 else if (!strcmp (p, "-Wimplicit-function-declaration"))
615 mesg_implicit_function_declaration = 1;
616 else if (!strcmp (p, "-Wno-implicit-function-declaration"))
617 mesg_implicit_function_declaration = 0;
618 else if (!strcmp (p, "-Wimplicit-int"))
619 warn_implicit_int = 1;
620 else if (!strcmp (p, "-Wno-implicit-int"))
621 warn_implicit_int = 0;
622 else if (!strcmp (p, "-Wimplicit"))
624 warn_implicit_int = 1;
625 if (mesg_implicit_function_declaration != 2)
626 mesg_implicit_function_declaration = 1;
628 else if (!strcmp (p, "-Wno-implicit"))
629 warn_implicit_int = 0, mesg_implicit_function_declaration = 0;
630 else if (!strcmp (p, "-Wlong-long"))
631 warn_long_long = 1;
632 else if (!strcmp (p, "-Wno-long-long"))
633 warn_long_long = 0;
634 else if (!strcmp (p, "-Wwrite-strings"))
635 flag_const_strings = 1;
636 else if (!strcmp (p, "-Wno-write-strings"))
637 flag_const_strings = 0;
638 else if (!strcmp (p, "-Wcast-qual"))
639 warn_cast_qual = 1;
640 else if (!strcmp (p, "-Wno-cast-qual"))
641 warn_cast_qual = 0;
642 else if (!strcmp (p, "-Wbad-function-cast"))
643 warn_bad_function_cast = 1;
644 else if (!strcmp (p, "-Wno-bad-function-cast"))
645 warn_bad_function_cast = 0;
646 else if (!strcmp (p, "-Wmissing-noreturn"))
647 warn_missing_noreturn = 1;
648 else if (!strcmp (p, "-Wno-missing-noreturn"))
649 warn_missing_noreturn = 0;
650 else if (!strcmp (p, "-Wpointer-arith"))
651 warn_pointer_arith = 1;
652 else if (!strcmp (p, "-Wno-pointer-arith"))
653 warn_pointer_arith = 0;
654 else if (!strcmp (p, "-Wstrict-prototypes"))
655 warn_strict_prototypes = 1;
656 else if (!strcmp (p, "-Wno-strict-prototypes"))
657 warn_strict_prototypes = 0;
658 else if (!strcmp (p, "-Wmissing-prototypes"))
659 warn_missing_prototypes = 1;
660 else if (!strcmp (p, "-Wno-missing-prototypes"))
661 warn_missing_prototypes = 0;
662 else if (!strcmp (p, "-Wmissing-declarations"))
663 warn_missing_declarations = 1;
664 else if (!strcmp (p, "-Wno-missing-declarations"))
665 warn_missing_declarations = 0;
666 else if (!strcmp (p, "-Wredundant-decls"))
667 warn_redundant_decls = 1;
668 else if (!strcmp (p, "-Wno-redundant-decls"))
669 warn_redundant_decls = 0;
670 else if (!strcmp (p, "-Wnested-externs"))
671 warn_nested_externs = 1;
672 else if (!strcmp (p, "-Wno-nested-externs"))
673 warn_nested_externs = 0;
674 else if (!strcmp (p, "-Wtraditional"))
675 warn_traditional = 1;
676 else if (!strcmp (p, "-Wno-traditional"))
677 warn_traditional = 0;
678 else if (!strcmp (p, "-Wformat"))
679 warn_format = 1;
680 else if (!strcmp (p, "-Wno-format"))
681 warn_format = 0;
682 else if (!strcmp (p, "-Wchar-subscripts"))
683 warn_char_subscripts = 1;
684 else if (!strcmp (p, "-Wno-char-subscripts"))
685 warn_char_subscripts = 0;
686 else if (!strcmp (p, "-Wconversion"))
687 warn_conversion = 1;
688 else if (!strcmp (p, "-Wno-conversion"))
689 warn_conversion = 0;
690 else if (!strcmp (p, "-Wparentheses"))
691 warn_parentheses = 1;
692 else if (!strcmp (p, "-Wno-parentheses"))
693 warn_parentheses = 0;
694 else if (!strcmp (p, "-Wreturn-type"))
695 warn_return_type = 1;
696 else if (!strcmp (p, "-Wno-return-type"))
697 warn_return_type = 0;
698 else if (!strcmp (p, "-Wcomment"))
699 ; /* cpp handles this one. */
700 else if (!strcmp (p, "-Wno-comment"))
701 ; /* cpp handles this one. */
702 else if (!strcmp (p, "-Wcomments"))
703 ; /* cpp handles this one. */
704 else if (!strcmp (p, "-Wno-comments"))
705 ; /* cpp handles this one. */
706 else if (!strcmp (p, "-Wtrigraphs"))
707 ; /* cpp handles this one. */
708 else if (!strcmp (p, "-Wno-trigraphs"))
709 ; /* cpp handles this one. */
710 else if (!strcmp (p, "-Wundef"))
711 ; /* cpp handles this one. */
712 else if (!strcmp (p, "-Wno-undef"))
713 ; /* cpp handles this one. */
714 else if (!strcmp (p, "-Wimport"))
715 ; /* cpp handles this one. */
716 else if (!strcmp (p, "-Wno-import"))
717 ; /* cpp handles this one. */
718 else if (!strcmp (p, "-Wmissing-braces"))
719 warn_missing_braces = 1;
720 else if (!strcmp (p, "-Wno-missing-braces"))
721 warn_missing_braces = 0;
722 else if (!strcmp (p, "-Wmain"))
723 warn_main = 1;
724 else if (!strcmp (p, "-Wno-main"))
725 warn_main = -1;
726 else if (!strcmp (p, "-Wsign-compare"))
727 warn_sign_compare = 1;
728 else if (!strcmp (p, "-Wno-sign-compare"))
729 warn_sign_compare = 0;
730 else if (!strcmp (p, "-Wfloat-equal"))
731 warn_float_equal = 1;
732 else if (!strcmp (p, "-Wno-float-equal"))
733 warn_float_equal = 0;
734 else if (!strcmp (p, "-Wmultichar"))
735 warn_multichar = 1;
736 else if (!strcmp (p, "-Wno-multichar"))
737 warn_multichar = 0;
738 else if (!strcmp (p, "-Wunknown-pragmas"))
739 /* Set to greater than 1, so that even unknown pragmas in system
740 headers will be warned about. */
741 warn_unknown_pragmas = 2;
742 else if (!strcmp (p, "-Wno-unknown-pragmas"))
743 warn_unknown_pragmas = 0;
744 else if (!strcmp (p, "-Wall"))
746 /* We save the value of warn_uninitialized, since if they put
747 -Wuninitialized on the command line, we need to generate a
748 warning about not using it without also specifying -O. */
749 if (warn_uninitialized != 1)
750 warn_uninitialized = 2;
751 warn_implicit_int = 1;
752 mesg_implicit_function_declaration = 1;
753 warn_return_type = 1;
754 warn_unused = 1;
755 warn_switch = 1;
756 warn_format = 1;
757 warn_char_subscripts = 1;
758 warn_parentheses = 1;
759 warn_missing_braces = 1;
760 /* We set this to 2 here, but 1 in -Wmain, so -ffreestanding can turn
761 it off only if it's not explicit. */
762 warn_main = 2;
763 /* Only warn about unknown pragmas that are not in system headers. */
764 warn_unknown_pragmas = 1;
766 else
767 return strings_processed;
769 return 1;
772 /* Hooks for print_node. */
774 void
775 print_lang_decl (file, node, indent)
776 FILE *file ATTRIBUTE_UNUSED;
777 tree node ATTRIBUTE_UNUSED;
778 int indent ATTRIBUTE_UNUSED;
782 void
783 print_lang_type (file, node, indent)
784 FILE *file ATTRIBUTE_UNUSED;
785 tree node ATTRIBUTE_UNUSED;
786 int indent ATTRIBUTE_UNUSED;
790 void
791 print_lang_identifier (file, node, indent)
792 FILE *file;
793 tree node;
794 int indent;
796 print_node (file, "global", IDENTIFIER_GLOBAL_VALUE (node), indent + 4);
797 print_node (file, "local", IDENTIFIER_LOCAL_VALUE (node), indent + 4);
798 print_node (file, "label", IDENTIFIER_LABEL_VALUE (node), indent + 4);
799 print_node (file, "implicit", IDENTIFIER_IMPLICIT_DECL (node), indent + 4);
800 print_node (file, "error locus", IDENTIFIER_ERROR_LOCUS (node), indent + 4);
801 print_node (file, "limbo value", IDENTIFIER_LIMBO_VALUE (node), indent + 4);
804 /* Hook called at end of compilation to assume 1 elt
805 for a top-level array decl that wasn't complete before. */
807 void
808 finish_incomplete_decl (decl)
809 tree decl;
811 if (TREE_CODE (decl) == VAR_DECL)
813 tree type = TREE_TYPE (decl);
814 if (type != error_mark_node
815 && TREE_CODE (type) == ARRAY_TYPE
816 && TYPE_DOMAIN (type) == 0)
818 if (! DECL_EXTERNAL (decl))
819 warning_with_decl (decl, "array `%s' assumed to have one element");
821 complete_array_type (type, NULL_TREE, 1);
823 layout_decl (decl, 0);
828 /* Create a new `struct binding_level'. */
830 static
831 struct binding_level *
832 make_binding_level ()
834 /* NOSTRICT */
835 return (struct binding_level *) xmalloc (sizeof (struct binding_level));
838 /* Nonzero if we are currently in the global binding level. */
841 global_bindings_p ()
843 return current_binding_level == global_binding_level;
846 void
847 keep_next_level ()
849 keep_next_level_flag = 1;
852 /* Nonzero if the current level needs to have a BLOCK made. */
855 kept_level_p ()
857 return ((current_binding_level->keep_if_subblocks
858 && current_binding_level->blocks != 0)
859 || current_binding_level->keep
860 || current_binding_level->names != 0
861 || (current_binding_level->tags != 0
862 && !current_binding_level->tag_transparent));
865 /* Identify this binding level as a level of parameters.
866 DEFINITION_FLAG is 1 for a definition, 0 for a declaration.
867 But it turns out there is no way to pass the right value for
868 DEFINITION_FLAG, so we ignore it. */
870 void
871 declare_parm_level (definition_flag)
872 int definition_flag ATTRIBUTE_UNUSED;
874 current_binding_level->parm_flag = 1;
877 /* Nonzero if currently making parm declarations. */
880 in_parm_level_p ()
882 return current_binding_level->parm_flag;
885 /* Enter a new binding level.
886 If TAG_TRANSPARENT is nonzero, do so only for the name space of variables,
887 not for that of tags. */
889 void
890 pushlevel (tag_transparent)
891 int tag_transparent;
893 register struct binding_level *newlevel = NULL_BINDING_LEVEL;
895 /* If this is the top level of a function,
896 just make sure that NAMED_LABELS is 0. */
898 if (current_binding_level == global_binding_level)
900 named_labels = 0;
903 /* Reuse or create a struct for this binding level. */
905 if (free_binding_level)
907 newlevel = free_binding_level;
908 free_binding_level = free_binding_level->level_chain;
910 else
912 newlevel = make_binding_level ();
915 /* Add this level to the front of the chain (stack) of levels that
916 are active. */
918 *newlevel = clear_binding_level;
919 newlevel->tag_transparent
920 = (tag_transparent
921 || (current_binding_level
922 ? current_binding_level->subblocks_tag_transparent
923 : 0));
924 newlevel->level_chain = current_binding_level;
925 current_binding_level = newlevel;
926 newlevel->keep = keep_next_level_flag;
927 keep_next_level_flag = 0;
928 newlevel->keep_if_subblocks = keep_next_if_subblocks;
929 keep_next_if_subblocks = 0;
932 /* Clear the limbo values of all identifiers defined in BLOCK or a subblock. */
934 static void
935 clear_limbo_values (block)
936 tree block;
938 tree tem;
940 for (tem = BLOCK_VARS (block); tem; tem = TREE_CHAIN (tem))
941 if (DECL_NAME (tem) != 0)
942 IDENTIFIER_LIMBO_VALUE (DECL_NAME (tem)) = 0;
944 for (tem = BLOCK_SUBBLOCKS (block); tem; tem = TREE_CHAIN (tem))
945 clear_limbo_values (tem);
948 /* Exit a binding level.
949 Pop the level off, and restore the state of the identifier-decl mappings
950 that were in effect when this level was entered.
952 If KEEP is nonzero, this level had explicit declarations, so
953 and create a "block" (a BLOCK node) for the level
954 to record its declarations and subblocks for symbol table output.
956 If FUNCTIONBODY is nonzero, this level is the body of a function,
957 so create a block as if KEEP were set and also clear out all
958 label names.
960 If REVERSE is nonzero, reverse the order of decls before putting
961 them into the BLOCK. */
963 tree
964 poplevel (keep, reverse, functionbody)
965 int keep;
966 int reverse;
967 int functionbody;
969 register tree link;
970 /* The chain of decls was accumulated in reverse order.
971 Put it into forward order, just for cleanliness. */
972 tree decls;
973 tree tags = current_binding_level->tags;
974 tree subblocks = current_binding_level->blocks;
975 tree block = 0;
976 tree decl;
977 int block_previously_created;
979 keep |= current_binding_level->keep;
981 /* This warning is turned off because it causes warnings for
982 declarations like `extern struct foo *x'. */
983 #if 0
984 /* Warn about incomplete structure types in this level. */
985 for (link = tags; link; link = TREE_CHAIN (link))
986 if (TYPE_SIZE (TREE_VALUE (link)) == 0)
988 tree type = TREE_VALUE (link);
989 tree type_name = TYPE_NAME (type);
990 char *id = IDENTIFIER_POINTER (TREE_CODE (type_name) == IDENTIFIER_NODE
991 ? type_name
992 : DECL_NAME (type_name));
993 switch (TREE_CODE (type))
995 case RECORD_TYPE:
996 error ("`struct %s' incomplete in scope ending here", id);
997 break;
998 case UNION_TYPE:
999 error ("`union %s' incomplete in scope ending here", id);
1000 break;
1001 case ENUMERAL_TYPE:
1002 error ("`enum %s' incomplete in scope ending here", id);
1003 break;
1006 #endif /* 0 */
1008 /* Get the decls in the order they were written.
1009 Usually current_binding_level->names is in reverse order.
1010 But parameter decls were previously put in forward order. */
1012 if (reverse)
1013 current_binding_level->names
1014 = decls = nreverse (current_binding_level->names);
1015 else
1016 decls = current_binding_level->names;
1018 /* Output any nested inline functions within this block
1019 if they weren't already output. */
1021 for (decl = decls; decl; decl = TREE_CHAIN (decl))
1022 if (TREE_CODE (decl) == FUNCTION_DECL
1023 && ! TREE_ASM_WRITTEN (decl)
1024 && DECL_INITIAL (decl) != 0
1025 && TREE_ADDRESSABLE (decl))
1027 /* If this decl was copied from a file-scope decl
1028 on account of a block-scope extern decl,
1029 propagate TREE_ADDRESSABLE to the file-scope decl.
1031 DECL_ABSTRACT_ORIGIN can be set to itself if warn_return_type is
1032 true, since then the decl goes through save_for_inline_copying. */
1033 if (DECL_ABSTRACT_ORIGIN (decl) != 0
1034 && DECL_ABSTRACT_ORIGIN (decl) != decl)
1035 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (decl)) = 1;
1036 else if (DECL_SAVED_INSNS (decl) != 0)
1038 push_function_context ();
1039 output_inline_function (decl);
1040 pop_function_context ();
1044 /* If there were any declarations or structure tags in that level,
1045 or if this level is a function body,
1046 create a BLOCK to record them for the life of this function. */
1048 block = 0;
1049 block_previously_created = (current_binding_level->this_block != 0);
1050 if (block_previously_created)
1051 block = current_binding_level->this_block;
1052 else if (keep || functionbody
1053 || (current_binding_level->keep_if_subblocks && subblocks != 0))
1054 block = make_node (BLOCK);
1055 if (block != 0)
1057 BLOCK_VARS (block) = decls;
1058 BLOCK_SUBBLOCKS (block) = subblocks;
1061 /* In each subblock, record that this is its superior. */
1063 for (link = subblocks; link; link = TREE_CHAIN (link))
1064 BLOCK_SUPERCONTEXT (link) = block;
1066 /* Clear out the meanings of the local variables of this level. */
1068 for (link = decls; link; link = TREE_CHAIN (link))
1070 if (DECL_NAME (link) != 0)
1072 /* If the ident. was used or addressed via a local extern decl,
1073 don't forget that fact. */
1074 if (DECL_EXTERNAL (link))
1076 if (TREE_USED (link))
1077 TREE_USED (DECL_NAME (link)) = 1;
1078 if (TREE_ADDRESSABLE (link))
1079 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (link)) = 1;
1081 IDENTIFIER_LOCAL_VALUE (DECL_NAME (link)) = 0;
1085 /* Restore all name-meanings of the outer levels
1086 that were shadowed by this level. */
1088 for (link = current_binding_level->shadowed; link; link = TREE_CHAIN (link))
1089 IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
1091 /* If the level being exited is the top level of a function,
1092 check over all the labels, and clear out the current
1093 (function local) meanings of their names. */
1095 if (functionbody)
1097 clear_limbo_values (block);
1099 /* If this is the top level block of a function,
1100 the vars are the function's parameters.
1101 Don't leave them in the BLOCK because they are
1102 found in the FUNCTION_DECL instead. */
1104 BLOCK_VARS (block) = 0;
1106 /* Clear out the definitions of all label names,
1107 since their scopes end here,
1108 and add them to BLOCK_VARS. */
1110 for (link = named_labels; link; link = TREE_CHAIN (link))
1112 register tree label = TREE_VALUE (link);
1114 if (DECL_INITIAL (label) == 0)
1116 error_with_decl (label, "label `%s' used but not defined");
1117 /* Avoid crashing later. */
1118 define_label (input_filename, lineno,
1119 DECL_NAME (label));
1121 else if (warn_unused && !TREE_USED (label))
1122 warning_with_decl (label, "label `%s' defined but not used");
1123 IDENTIFIER_LABEL_VALUE (DECL_NAME (label)) = 0;
1125 /* Put the labels into the "variables" of the
1126 top-level block, so debugger can see them. */
1127 TREE_CHAIN (label) = BLOCK_VARS (block);
1128 BLOCK_VARS (block) = label;
1132 /* Pop the current level, and free the structure for reuse. */
1135 register struct binding_level *level = current_binding_level;
1136 current_binding_level = current_binding_level->level_chain;
1138 level->level_chain = free_binding_level;
1139 free_binding_level = level;
1142 /* Dispose of the block that we just made inside some higher level. */
1143 if (functionbody)
1144 DECL_INITIAL (current_function_decl) = block;
1145 else if (block)
1147 if (!block_previously_created)
1148 current_binding_level->blocks
1149 = chainon (current_binding_level->blocks, block);
1151 /* If we did not make a block for the level just exited,
1152 any blocks made for inner levels
1153 (since they cannot be recorded as subblocks in that level)
1154 must be carried forward so they will later become subblocks
1155 of something else. */
1156 else if (subblocks)
1157 current_binding_level->blocks
1158 = chainon (current_binding_level->blocks, subblocks);
1160 /* Set the TYPE_CONTEXTs for all of the tagged types belonging to this
1161 binding contour so that they point to the appropriate construct, i.e.
1162 either to the current FUNCTION_DECL node, or else to the BLOCK node
1163 we just constructed.
1165 Note that for tagged types whose scope is just the formal parameter
1166 list for some function type specification, we can't properly set
1167 their TYPE_CONTEXTs here, because we don't have a pointer to the
1168 appropriate FUNCTION_TYPE node readily available to us. For those
1169 cases, the TYPE_CONTEXTs of the relevant tagged type nodes get set
1170 in `grokdeclarator' as soon as we have created the FUNCTION_TYPE
1171 node which will represent the "scope" for these "parameter list local"
1172 tagged types.
1175 if (functionbody)
1176 for (link = tags; link; link = TREE_CHAIN (link))
1177 TYPE_CONTEXT (TREE_VALUE (link)) = current_function_decl;
1178 else if (block)
1179 for (link = tags; link; link = TREE_CHAIN (link))
1180 TYPE_CONTEXT (TREE_VALUE (link)) = block;
1182 if (block)
1183 TREE_USED (block) = 1;
1184 return block;
1187 /* Delete the node BLOCK from the current binding level.
1188 This is used for the block inside a stmt expr ({...})
1189 so that the block can be reinserted where appropriate. */
1191 void
1192 delete_block (block)
1193 tree block;
1195 tree t;
1196 if (current_binding_level->blocks == block)
1197 current_binding_level->blocks = TREE_CHAIN (block);
1198 for (t = current_binding_level->blocks; t;)
1200 if (TREE_CHAIN (t) == block)
1201 TREE_CHAIN (t) = TREE_CHAIN (block);
1202 else
1203 t = TREE_CHAIN (t);
1205 TREE_CHAIN (block) = NULL;
1206 /* Clear TREE_USED which is always set by poplevel.
1207 The flag is set again if insert_block is called. */
1208 TREE_USED (block) = 0;
1211 /* Insert BLOCK at the end of the list of subblocks of the
1212 current binding level. This is used when a BIND_EXPR is expanded,
1213 to handle the BLOCK node inside the BIND_EXPR. */
1215 void
1216 insert_block (block)
1217 tree block;
1219 TREE_USED (block) = 1;
1220 current_binding_level->blocks
1221 = chainon (current_binding_level->blocks, block);
1224 /* Set the BLOCK node for the innermost scope
1225 (the one we are currently in). */
1227 void
1228 set_block (block)
1229 register tree block;
1231 current_binding_level->this_block = block;
1234 void
1235 push_label_level ()
1237 register struct binding_level *newlevel;
1239 /* Reuse or create a struct for this binding level. */
1241 if (free_binding_level)
1243 newlevel = free_binding_level;
1244 free_binding_level = free_binding_level->level_chain;
1246 else
1248 newlevel = make_binding_level ();
1251 /* Add this level to the front of the chain (stack) of label levels. */
1253 newlevel->level_chain = label_level_chain;
1254 label_level_chain = newlevel;
1256 newlevel->names = named_labels;
1257 newlevel->shadowed = shadowed_labels;
1258 named_labels = 0;
1259 shadowed_labels = 0;
1262 void
1263 pop_label_level ()
1265 register struct binding_level *level = label_level_chain;
1266 tree link, prev;
1268 /* Clear out the definitions of the declared labels in this level.
1269 Leave in the list any ordinary, non-declared labels. */
1270 for (link = named_labels, prev = 0; link;)
1272 if (C_DECLARED_LABEL_FLAG (TREE_VALUE (link)))
1274 if (DECL_SOURCE_LINE (TREE_VALUE (link)) == 0)
1276 error_with_decl (TREE_VALUE (link),
1277 "label `%s' used but not defined");
1278 /* Avoid crashing later. */
1279 define_label (input_filename, lineno,
1280 DECL_NAME (TREE_VALUE (link)));
1282 else if (warn_unused && !TREE_USED (TREE_VALUE (link)))
1283 warning_with_decl (TREE_VALUE (link),
1284 "label `%s' defined but not used");
1285 IDENTIFIER_LABEL_VALUE (DECL_NAME (TREE_VALUE (link))) = 0;
1287 /* Delete this element from the list. */
1288 link = TREE_CHAIN (link);
1289 if (prev)
1290 TREE_CHAIN (prev) = link;
1291 else
1292 named_labels = link;
1294 else
1296 prev = link;
1297 link = TREE_CHAIN (link);
1301 /* Bring back all the labels that were shadowed. */
1302 for (link = shadowed_labels; link; link = TREE_CHAIN (link))
1303 if (DECL_NAME (TREE_VALUE (link)) != 0)
1304 IDENTIFIER_LABEL_VALUE (DECL_NAME (TREE_VALUE (link)))
1305 = TREE_VALUE (link);
1307 named_labels = chainon (named_labels, level->names);
1308 shadowed_labels = level->shadowed;
1310 /* Pop the current level, and free the structure for reuse. */
1311 label_level_chain = label_level_chain->level_chain;
1312 level->level_chain = free_binding_level;
1313 free_binding_level = level;
1316 /* Push a definition or a declaration of struct, union or enum tag "name".
1317 "type" should be the type node.
1318 We assume that the tag "name" is not already defined.
1320 Note that the definition may really be just a forward reference.
1321 In that case, the TYPE_SIZE will be zero. */
1323 void
1324 pushtag (name, type)
1325 tree name, type;
1327 register struct binding_level *b;
1329 /* Find the proper binding level for this type tag. */
1331 for (b = current_binding_level; b->tag_transparent; b = b->level_chain)
1332 continue;
1334 if (name)
1336 /* Record the identifier as the type's name if it has none. */
1338 if (TYPE_NAME (type) == 0)
1339 TYPE_NAME (type) = name;
1342 if (b == global_binding_level)
1343 b->tags = perm_tree_cons (name, type, b->tags);
1344 else
1345 b->tags = saveable_tree_cons (name, type, b->tags);
1347 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1348 tagged type we just added to the current binding level. This fake
1349 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1350 to output a representation of a tagged type, and it also gives
1351 us a convenient place to record the "scope start" address for the
1352 tagged type. */
1354 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
1356 /* An approximation for now, so we can tell this is a function-scope tag.
1357 This will be updated in poplevel. */
1358 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
1361 /* Handle when a new declaration NEWDECL
1362 has the same name as an old one OLDDECL
1363 in the same binding contour.
1364 Prints an error message if appropriate.
1366 If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
1367 Otherwise, return 0.
1369 When DIFFERENT_BINDING_LEVEL is true, NEWDECL is an external declaration,
1370 and OLDDECL is in an outer binding level and should thus not be changed. */
1372 static int
1373 duplicate_decls (newdecl, olddecl, different_binding_level)
1374 register tree newdecl, olddecl;
1375 int different_binding_level;
1377 int types_match = comptypes (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
1378 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1379 && DECL_INITIAL (newdecl) != 0);
1380 tree oldtype = TREE_TYPE (olddecl);
1381 tree newtype = TREE_TYPE (newdecl);
1382 int errmsg = 0;
1384 if (TREE_CODE_CLASS (TREE_CODE (olddecl)) == 'd')
1385 DECL_MACHINE_ATTRIBUTES (newdecl)
1386 = merge_machine_decl_attributes (olddecl, newdecl);
1388 if (TREE_CODE (newtype) == ERROR_MARK
1389 || TREE_CODE (oldtype) == ERROR_MARK)
1390 types_match = 0;
1392 /* New decl is completely inconsistent with the old one =>
1393 tell caller to replace the old one.
1394 This is always an error except in the case of shadowing a builtin. */
1395 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1397 if (TREE_CODE (olddecl) == FUNCTION_DECL
1398 && (DECL_BUILT_IN (olddecl)
1399 || DECL_BUILT_IN_NONANSI (olddecl)))
1401 /* If you declare a built-in or predefined function name as static,
1402 the old definition is overridden,
1403 but optionally warn this was a bad choice of name. */
1404 if (!TREE_PUBLIC (newdecl))
1406 if (!warn_shadow)
1408 else if (DECL_BUILT_IN (olddecl))
1409 warning_with_decl (newdecl, "shadowing built-in function `%s'");
1410 else
1411 warning_with_decl (newdecl, "shadowing library function `%s'");
1413 /* Likewise, if the built-in is not ansi, then programs can
1414 override it even globally without an error. */
1415 else if (! DECL_BUILT_IN (olddecl))
1416 warning_with_decl (newdecl,
1417 "library function `%s' declared as non-function");
1419 else if (DECL_BUILT_IN_NONANSI (olddecl))
1420 warning_with_decl (newdecl,
1421 "built-in function `%s' declared as non-function");
1422 else
1423 warning_with_decl (newdecl,
1424 "built-in function `%s' declared as non-function");
1426 else
1428 error_with_decl (newdecl, "`%s' redeclared as different kind of symbol");
1429 error_with_decl (olddecl, "previous declaration of `%s'");
1432 return 0;
1435 /* For real parm decl following a forward decl,
1436 return 1 so old decl will be reused. */
1437 if (types_match && TREE_CODE (newdecl) == PARM_DECL
1438 && TREE_ASM_WRITTEN (olddecl) && ! TREE_ASM_WRITTEN (newdecl))
1439 return 1;
1441 /* The new declaration is the same kind of object as the old one.
1442 The declarations may partially match. Print warnings if they don't
1443 match enough. Ultimately, copy most of the information from the new
1444 decl to the old one, and keep using the old one. */
1446 if (flag_traditional && TREE_CODE (newdecl) == FUNCTION_DECL
1447 && IDENTIFIER_IMPLICIT_DECL (DECL_NAME (newdecl)) == olddecl
1448 && DECL_INITIAL (olddecl) == 0)
1449 /* If -traditional, avoid error for redeclaring fcn
1450 after implicit decl. */
1452 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1453 && DECL_BUILT_IN (olddecl))
1455 /* A function declaration for a built-in function. */
1456 if (!TREE_PUBLIC (newdecl))
1458 /* If you declare a built-in function name as static, the
1459 built-in definition is overridden,
1460 but optionally warn this was a bad choice of name. */
1461 if (warn_shadow)
1462 warning_with_decl (newdecl, "shadowing built-in function `%s'");
1463 /* Discard the old built-in function. */
1464 return 0;
1466 else if (!types_match)
1468 /* Accept the return type of the new declaration if same modes. */
1469 tree oldreturntype = TREE_TYPE (oldtype);
1470 tree newreturntype = TREE_TYPE (newtype);
1472 /* Make sure we put the new type in the same obstack as the old ones.
1473 If the old types are not both in the same obstack, use the
1474 permanent one. */
1475 if (TYPE_OBSTACK (oldtype) == TYPE_OBSTACK (newtype))
1476 push_obstacks (TYPE_OBSTACK (oldtype), TYPE_OBSTACK (oldtype));
1477 else
1479 push_obstacks_nochange ();
1480 end_temporary_allocation ();
1483 if (TYPE_MODE (oldreturntype) == TYPE_MODE (newreturntype))
1485 /* Function types may be shared, so we can't just modify
1486 the return type of olddecl's function type. */
1487 tree trytype
1488 = build_function_type (newreturntype,
1489 TYPE_ARG_TYPES (oldtype));
1491 types_match = comptypes (newtype, trytype);
1492 if (types_match)
1493 oldtype = trytype;
1495 /* Accept harmless mismatch in first argument type also.
1496 This is for ffs. */
1497 if (TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != 0
1498 && TYPE_ARG_TYPES (oldtype) != 0
1499 && TREE_VALUE (TYPE_ARG_TYPES (newtype)) != 0
1500 && TREE_VALUE (TYPE_ARG_TYPES (oldtype)) != 0
1501 && (TYPE_MODE (TREE_VALUE (TYPE_ARG_TYPES (newtype)))
1502 == TYPE_MODE (TREE_VALUE (TYPE_ARG_TYPES (oldtype)))))
1504 /* Function types may be shared, so we can't just modify
1505 the return type of olddecl's function type. */
1506 tree trytype
1507 = build_function_type (TREE_TYPE (oldtype),
1508 tree_cons (NULL_TREE,
1509 TREE_VALUE (TYPE_ARG_TYPES (newtype)),
1510 TREE_CHAIN (TYPE_ARG_TYPES (oldtype))));
1512 types_match = comptypes (newtype, trytype);
1513 if (types_match)
1514 oldtype = trytype;
1516 if (! different_binding_level)
1517 TREE_TYPE (olddecl) = oldtype;
1519 pop_obstacks ();
1521 if (!types_match)
1523 /* If types don't match for a built-in, throw away the built-in. */
1524 warning_with_decl (newdecl, "conflicting types for built-in function `%s'");
1525 return 0;
1528 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1529 && DECL_SOURCE_LINE (olddecl) == 0)
1531 /* A function declaration for a predeclared function
1532 that isn't actually built in. */
1533 if (!TREE_PUBLIC (newdecl))
1535 /* If you declare it as static, the
1536 default definition is overridden. */
1537 return 0;
1539 else if (!types_match)
1541 /* If the types don't match, preserve volatility indication.
1542 Later on, we will discard everything else about the
1543 default declaration. */
1544 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1547 /* Permit char *foo () to match void *foo (...) if not pedantic,
1548 if one of them came from a system header file. */
1549 else if (!types_match
1550 && TREE_CODE (olddecl) == FUNCTION_DECL
1551 && TREE_CODE (newdecl) == FUNCTION_DECL
1552 && TREE_CODE (TREE_TYPE (oldtype)) == POINTER_TYPE
1553 && TREE_CODE (TREE_TYPE (newtype)) == POINTER_TYPE
1554 && (DECL_IN_SYSTEM_HEADER (olddecl)
1555 || DECL_IN_SYSTEM_HEADER (newdecl))
1556 && ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (newtype))) == void_type_node
1557 && TYPE_ARG_TYPES (oldtype) == 0
1558 && self_promoting_args_p (TYPE_ARG_TYPES (newtype))
1559 && TREE_TYPE (TREE_TYPE (oldtype)) == char_type_node)
1561 (TREE_TYPE (TREE_TYPE (newtype)) == char_type_node
1562 && TYPE_ARG_TYPES (newtype) == 0
1563 && self_promoting_args_p (TYPE_ARG_TYPES (oldtype))
1564 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (oldtype))) == void_type_node)))
1566 if (pedantic)
1567 pedwarn_with_decl (newdecl, "conflicting types for `%s'");
1568 /* Make sure we keep void * as ret type, not char *. */
1569 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (oldtype))) == void_type_node)
1570 TREE_TYPE (newdecl) = newtype = oldtype;
1572 /* Set DECL_IN_SYSTEM_HEADER, so that if we see another declaration
1573 we will come back here again. */
1574 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1576 else if (!types_match
1577 /* Permit char *foo (int, ...); followed by char *foo ();
1578 if not pedantic. */
1579 && ! (TREE_CODE (olddecl) == FUNCTION_DECL
1580 && ! pedantic
1581 /* Return types must still match. */
1582 && comptypes (TREE_TYPE (oldtype),
1583 TREE_TYPE (newtype))
1584 && TYPE_ARG_TYPES (newtype) == 0))
1586 error_with_decl (newdecl, "conflicting types for `%s'");
1587 /* Check for function type mismatch
1588 involving an empty arglist vs a nonempty one. */
1589 if (TREE_CODE (olddecl) == FUNCTION_DECL
1590 && comptypes (TREE_TYPE (oldtype),
1591 TREE_TYPE (newtype))
1592 && ((TYPE_ARG_TYPES (oldtype) == 0
1593 && DECL_INITIAL (olddecl) == 0)
1595 (TYPE_ARG_TYPES (newtype) == 0
1596 && DECL_INITIAL (newdecl) == 0)))
1598 /* Classify the problem further. */
1599 register tree t = TYPE_ARG_TYPES (oldtype);
1600 if (t == 0)
1601 t = TYPE_ARG_TYPES (newtype);
1602 for (; t; t = TREE_CHAIN (t))
1604 register tree type = TREE_VALUE (t);
1606 if (TREE_CHAIN (t) == 0
1607 && TYPE_MAIN_VARIANT (type) != void_type_node)
1609 error ("A parameter list with an ellipsis can't match an empty parameter name list declaration.");
1610 break;
1613 if (simple_type_promotes_to (type) != NULL_TREE)
1615 error ("An argument type that has a default promotion can't match an empty parameter name list declaration.");
1616 break;
1620 error_with_decl (olddecl, "previous declaration of `%s'");
1622 else
1624 errmsg = redeclaration_error_message (newdecl, olddecl);
1625 if (errmsg)
1627 switch (errmsg)
1629 case 1:
1630 error_with_decl (newdecl, "redefinition of `%s'");
1631 break;
1632 case 2:
1633 error_with_decl (newdecl, "redeclaration of `%s'");
1634 break;
1635 case 3:
1636 error_with_decl (newdecl, "conflicting declarations of `%s'");
1637 break;
1638 default:
1639 abort ();
1642 error_with_decl (olddecl,
1643 ((DECL_INITIAL (olddecl)
1644 && current_binding_level == global_binding_level)
1645 ? "`%s' previously defined here"
1646 : "`%s' previously declared here"));
1648 else if (TREE_CODE (newdecl) == TYPE_DECL
1649 && (DECL_IN_SYSTEM_HEADER (olddecl)
1650 || DECL_IN_SYSTEM_HEADER (newdecl)))
1652 warning_with_decl (newdecl, "redefinition of `%s'");
1653 warning_with_decl
1654 (olddecl,
1655 ((DECL_INITIAL (olddecl)
1656 && current_binding_level == global_binding_level)
1657 ? "`%s' previously defined here"
1658 : "`%s' previously declared here"));
1660 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1661 && DECL_INITIAL (olddecl) != 0
1662 && TYPE_ARG_TYPES (oldtype) == 0
1663 && TYPE_ARG_TYPES (newtype) != 0
1664 && TYPE_ACTUAL_ARG_TYPES (oldtype) != 0)
1666 register tree type, parm;
1667 register int nargs;
1668 /* Prototype decl follows defn w/o prototype. */
1670 for (parm = TYPE_ACTUAL_ARG_TYPES (oldtype),
1671 type = TYPE_ARG_TYPES (newtype),
1672 nargs = 1;
1674 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type), nargs++)
1676 if (TYPE_MAIN_VARIANT (TREE_VALUE (parm)) == void_type_node
1677 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
1679 warning_with_decl (newdecl, "prototype for `%s' follows");
1680 warning_with_decl (olddecl, "non-prototype definition here");
1681 break;
1683 if (TYPE_MAIN_VARIANT (TREE_VALUE (parm)) == void_type_node
1684 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
1686 error_with_decl (newdecl, "prototype for `%s' follows and number of arguments doesn't match");
1687 error_with_decl (olddecl, "non-prototype definition here");
1688 errmsg = 1;
1689 break;
1691 /* Type for passing arg must be consistent
1692 with that declared for the arg. */
1693 if (! comptypes (TREE_VALUE (parm), TREE_VALUE (type))
1694 /* If -traditional, allow `unsigned int' instead of `int'
1695 in the prototype. */
1696 && (! (flag_traditional
1697 && TYPE_MAIN_VARIANT (TREE_VALUE (parm)) == integer_type_node
1698 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == unsigned_type_node)))
1700 error_with_decl (newdecl,
1701 "prototype for `%s' follows and argument %d doesn't match",
1702 nargs);
1703 error_with_decl (olddecl, "non-prototype definition here");
1704 errmsg = 1;
1705 break;
1709 /* Warn about mismatches in various flags. */
1710 else
1712 /* Warn if function is now inline
1713 but was previously declared not inline and has been called. */
1714 if (TREE_CODE (olddecl) == FUNCTION_DECL
1715 && ! DECL_INLINE (olddecl) && DECL_INLINE (newdecl)
1716 && TREE_USED (olddecl))
1717 warning_with_decl (newdecl,
1718 "`%s' declared inline after being called");
1719 if (TREE_CODE (olddecl) == FUNCTION_DECL
1720 && ! DECL_INLINE (olddecl) && DECL_INLINE (newdecl)
1721 && DECL_INITIAL (olddecl) != 0)
1722 warning_with_decl (newdecl,
1723 "`%s' declared inline after its definition");
1725 /* If pedantic, warn when static declaration follows a non-static
1726 declaration. Otherwise, do so only for functions. */
1727 if ((pedantic || TREE_CODE (olddecl) == FUNCTION_DECL)
1728 && TREE_PUBLIC (olddecl)
1729 && !TREE_PUBLIC (newdecl))
1730 warning_with_decl (newdecl, "static declaration for `%s' follows non-static");
1732 /* If warn_traditional, warn when a non-static function
1733 declaration follows a static one. */
1734 if (warn_traditional
1735 && TREE_CODE (olddecl) == FUNCTION_DECL
1736 && !TREE_PUBLIC (olddecl)
1737 && TREE_PUBLIC (newdecl))
1738 warning_with_decl (newdecl, "non-static declaration for `%s' follows static");
1740 /* Warn when const declaration follows a non-const
1741 declaration, but not for functions. */
1742 if (TREE_CODE (olddecl) != FUNCTION_DECL
1743 && !TREE_READONLY (olddecl)
1744 && TREE_READONLY (newdecl))
1745 warning_with_decl (newdecl, "const declaration for `%s' follows non-const");
1746 /* These bits are logically part of the type, for variables.
1747 But not for functions
1748 (where qualifiers are not valid ANSI anyway). */
1749 else if (pedantic && TREE_CODE (olddecl) != FUNCTION_DECL
1750 && (TREE_READONLY (newdecl) != TREE_READONLY (olddecl)
1751 || TREE_THIS_VOLATILE (newdecl) != TREE_THIS_VOLATILE (olddecl)))
1752 pedwarn_with_decl (newdecl, "type qualifiers for `%s' conflict with previous decl");
1756 /* Optionally warn about more than one declaration for the same name. */
1757 if (errmsg == 0 && warn_redundant_decls && DECL_SOURCE_LINE (olddecl) != 0
1758 /* Don't warn about a function declaration
1759 followed by a definition. */
1760 && !(TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl) != 0
1761 && DECL_INITIAL (olddecl) == 0)
1762 /* Don't warn about extern decl followed by (tentative) definition. */
1763 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl)))
1765 warning_with_decl (newdecl, "redundant redeclaration of `%s' in same scope");
1766 warning_with_decl (olddecl, "previous declaration of `%s'");
1769 /* Copy all the DECL_... slots specified in the new decl
1770 except for any that we copy here from the old type.
1772 Past this point, we don't change OLDTYPE and NEWTYPE
1773 even if we change the types of NEWDECL and OLDDECL. */
1775 if (types_match)
1777 /* When copying info to olddecl, we store into write_olddecl
1778 instead. This allows us to avoid modifying olddecl when
1779 different_binding_level is true. */
1780 tree write_olddecl = different_binding_level ? newdecl : olddecl;
1782 /* Make sure we put the new type in the same obstack as the old ones.
1783 If the old types are not both in the same obstack, use the permanent
1784 one. */
1785 if (TYPE_OBSTACK (oldtype) == TYPE_OBSTACK (newtype))
1786 push_obstacks (TYPE_OBSTACK (oldtype), TYPE_OBSTACK (oldtype));
1787 else
1789 push_obstacks_nochange ();
1790 end_temporary_allocation ();
1793 /* Merge the data types specified in the two decls. */
1794 if (TREE_CODE (newdecl) != FUNCTION_DECL || !DECL_BUILT_IN (olddecl))
1796 if (different_binding_level)
1797 TREE_TYPE (newdecl)
1798 = build_type_attribute_variant
1799 (newtype,
1800 merge_attributes (TYPE_ATTRIBUTES (newtype),
1801 TYPE_ATTRIBUTES (oldtype)));
1802 else
1803 TREE_TYPE (newdecl)
1804 = TREE_TYPE (olddecl)
1805 = common_type (newtype, oldtype);
1808 /* Lay the type out, unless already done. */
1809 if (oldtype != TREE_TYPE (newdecl))
1811 if (TREE_TYPE (newdecl) != error_mark_node)
1812 layout_type (TREE_TYPE (newdecl));
1813 if (TREE_CODE (newdecl) != FUNCTION_DECL
1814 && TREE_CODE (newdecl) != TYPE_DECL
1815 && TREE_CODE (newdecl) != CONST_DECL)
1816 layout_decl (newdecl, 0);
1818 else
1820 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1821 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1822 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1823 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1824 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1827 /* Keep the old rtl since we can safely use it. */
1828 DECL_RTL (newdecl) = DECL_RTL (olddecl);
1830 /* Merge the type qualifiers. */
1831 if (DECL_BUILT_IN_NONANSI (olddecl) && TREE_THIS_VOLATILE (olddecl)
1832 && !TREE_THIS_VOLATILE (newdecl))
1833 TREE_THIS_VOLATILE (write_olddecl) = 0;
1834 if (TREE_READONLY (newdecl))
1835 TREE_READONLY (write_olddecl) = 1;
1836 if (TREE_THIS_VOLATILE (newdecl))
1838 TREE_THIS_VOLATILE (write_olddecl) = 1;
1839 if (TREE_CODE (newdecl) == VAR_DECL)
1840 make_var_volatile (newdecl);
1843 /* Keep source location of definition rather than declaration. */
1844 /* When called with different_binding_level set, keep the old
1845 information so that meaningful diagnostics can be given. */
1846 if (DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0
1847 && ! different_binding_level)
1849 DECL_SOURCE_LINE (newdecl) = DECL_SOURCE_LINE (olddecl);
1850 DECL_SOURCE_FILE (newdecl) = DECL_SOURCE_FILE (olddecl);
1853 /* Merge the unused-warning information. */
1854 if (DECL_IN_SYSTEM_HEADER (olddecl))
1855 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1856 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1857 DECL_IN_SYSTEM_HEADER (write_olddecl) = 1;
1859 /* Merge the initialization information. */
1860 /* When called with different_binding_level set, don't copy over
1861 DECL_INITIAL, so that we don't accidentally change function
1862 declarations into function definitions. */
1863 if (DECL_INITIAL (newdecl) == 0 && ! different_binding_level)
1864 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1866 /* Merge the section attribute.
1867 We want to issue an error if the sections conflict but that must be
1868 done later in decl_attributes since we are called before attributes
1869 are assigned. */
1870 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1871 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1873 /* Copy the assembler name.
1874 Currently, it can only be defined in the prototype. */
1875 DECL_ASSEMBLER_NAME (newdecl) = DECL_ASSEMBLER_NAME (olddecl);
1877 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1879 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1880 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1882 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1883 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1884 DECL_NO_CHECK_MEMORY_USAGE (newdecl)
1885 |= DECL_NO_CHECK_MEMORY_USAGE (olddecl);
1888 pop_obstacks ();
1890 /* If cannot merge, then use the new type and qualifiers,
1891 and don't preserve the old rtl. */
1892 else if (! different_binding_level)
1894 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1895 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
1896 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
1897 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
1900 /* Merge the storage class information. */
1901 DECL_WEAK (newdecl) |= DECL_WEAK (olddecl);
1902 /* For functions, static overrides non-static. */
1903 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1905 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1906 /* This is since we don't automatically
1907 copy the attributes of NEWDECL into OLDDECL. */
1908 /* No need to worry about different_binding_level here because
1909 then TREE_PUBLIC (newdecl) was true. */
1910 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1911 /* If this clears `static', clear it in the identifier too. */
1912 if (! TREE_PUBLIC (olddecl))
1913 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1915 if (DECL_EXTERNAL (newdecl))
1917 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1918 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1919 /* An extern decl does not override previous storage class. */
1920 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1921 if (! DECL_EXTERNAL (newdecl))
1922 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1924 else
1926 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1927 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1930 /* If either decl says `inline', this fn is inline,
1931 unless its definition was passed already. */
1932 if (DECL_INLINE (newdecl) && DECL_INITIAL (olddecl) == 0)
1933 DECL_INLINE (olddecl) = 1;
1934 DECL_INLINE (newdecl) = DECL_INLINE (olddecl);
1936 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1938 if (DECL_BUILT_IN (olddecl))
1940 /* Get rid of any built-in function if new arg types don't match it
1941 or if we have a function definition. */
1942 if (! types_match || new_is_definition)
1944 if (! different_binding_level)
1946 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1947 DECL_BUILT_IN_CLASS (olddecl) = NOT_BUILT_IN;
1950 else
1952 /* If redeclaring a builtin function, and not a definition,
1953 it stays built in. */
1954 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1955 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1958 /* Also preserve various other info from the definition. */
1959 else if (! new_is_definition)
1960 DECL_FRAME_SIZE (newdecl) = DECL_FRAME_SIZE (olddecl);
1961 if (! new_is_definition)
1963 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1964 /* When called with different_binding_level set, don't copy over
1965 DECL_INITIAL, so that we don't accidentally change function
1966 declarations into function definitions. */
1967 if (! different_binding_level)
1968 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1969 DECL_SAVED_INSNS (newdecl) = DECL_SAVED_INSNS (olddecl);
1970 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1971 if (DECL_INLINE (newdecl))
1972 DECL_ABSTRACT_ORIGIN (newdecl) = DECL_ORIGIN (olddecl);
1975 if (different_binding_level)
1977 /* Don't output a duplicate symbol or debugging information for this
1978 declaration.
1980 Do not set TREE_ASM_WRITTEN for a FUNCTION_DECL since we may actually
1981 just have two declarations without a definition. VAR_DECLs may need
1982 the same treatment, I'm not sure. */
1983 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1984 DECL_IGNORED_P (newdecl) = 1;
1985 else
1986 TREE_ASM_WRITTEN (newdecl) = DECL_IGNORED_P (newdecl) = 1;
1987 return 0;
1990 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1991 But preserve OLDDECL's DECL_UID. */
1993 register unsigned olddecl_uid = DECL_UID (olddecl);
1995 bcopy ((char *) newdecl + sizeof (struct tree_common),
1996 (char *) olddecl + sizeof (struct tree_common),
1997 sizeof (struct tree_decl) - sizeof (struct tree_common));
1998 DECL_UID (olddecl) = olddecl_uid;
2001 /* NEWDECL contains the merged attribute lists.
2002 Update OLDDECL to be the same. */
2003 DECL_MACHINE_ATTRIBUTES (olddecl) = DECL_MACHINE_ATTRIBUTES (newdecl);
2005 return 1;
2008 /* Record a decl-node X as belonging to the current lexical scope.
2009 Check for errors (such as an incompatible declaration for the same
2010 name already seen in the same scope).
2012 Returns either X or an old decl for the same name.
2013 If an old decl is returned, it may have been smashed
2014 to agree with what X says. */
2016 tree
2017 pushdecl (x)
2018 tree x;
2020 register tree t;
2021 register tree name = DECL_NAME (x);
2022 register struct binding_level *b = current_binding_level;
2024 DECL_CONTEXT (x) = current_function_decl;
2025 /* A local extern declaration for a function doesn't constitute nesting.
2026 A local auto declaration does, since it's a forward decl
2027 for a nested function coming later. */
2028 if (TREE_CODE (x) == FUNCTION_DECL && DECL_INITIAL (x) == 0
2029 && DECL_EXTERNAL (x))
2030 DECL_CONTEXT (x) = 0;
2032 if (warn_nested_externs && DECL_EXTERNAL (x) && b != global_binding_level
2033 && x != IDENTIFIER_IMPLICIT_DECL (name)
2034 /* Don't print error messages for __FUNCTION__ and __PRETTY_FUNCTION__ */
2035 && !DECL_IN_SYSTEM_HEADER (x))
2036 warning ("nested extern declaration of `%s'", IDENTIFIER_POINTER (name));
2038 if (name)
2040 char *file;
2041 int line;
2042 int different_binding_level = 0;
2044 t = lookup_name_current_level (name);
2045 /* Don't type check externs here when -traditional. This is so that
2046 code with conflicting declarations inside blocks will get warnings
2047 not errors. X11 for instance depends on this. */
2048 if (! t && DECL_EXTERNAL (x) && TREE_PUBLIC (x) && ! flag_traditional)
2050 t = IDENTIFIER_GLOBAL_VALUE (name);
2051 /* Type decls at global scope don't conflict with externs declared
2052 inside lexical blocks. */
2053 if (t && TREE_CODE (t) == TYPE_DECL)
2054 t = 0;
2055 different_binding_level = 1;
2057 if (t != 0 && t == error_mark_node)
2058 /* error_mark_node is 0 for a while during initialization! */
2060 t = 0;
2061 error_with_decl (x, "`%s' used prior to declaration");
2064 if (t != 0)
2066 file = DECL_SOURCE_FILE (t);
2067 line = DECL_SOURCE_LINE (t);
2070 /* If this decl is `static' and an implicit decl was seen previously,
2071 warn. But don't complain if -traditional,
2072 since traditional compilers don't complain. */
2073 if (! flag_traditional && TREE_PUBLIC (name)
2074 /* Don't test for DECL_EXTERNAL, because grokdeclarator
2075 sets this for all functions. */
2076 && ! TREE_PUBLIC (x)
2077 && (TREE_CODE (x) == FUNCTION_DECL || b == global_binding_level)
2078 /* We used to warn also for explicit extern followed by static,
2079 but sometimes you need to do it that way. */
2080 && IDENTIFIER_IMPLICIT_DECL (name) != 0)
2082 pedwarn ("`%s' was declared implicitly `extern' and later `static'",
2083 IDENTIFIER_POINTER (name));
2084 pedwarn_with_file_and_line
2085 (DECL_SOURCE_FILE (IDENTIFIER_IMPLICIT_DECL (name)),
2086 DECL_SOURCE_LINE (IDENTIFIER_IMPLICIT_DECL (name)),
2087 "previous declaration of `%s'",
2088 IDENTIFIER_POINTER (name));
2089 TREE_THIS_VOLATILE (name) = 1;
2092 if (t != 0 && duplicate_decls (x, t, different_binding_level))
2094 if (TREE_CODE (t) == PARM_DECL)
2096 /* Don't allow more than one "real" duplicate
2097 of a forward parm decl. */
2098 TREE_ASM_WRITTEN (t) = TREE_ASM_WRITTEN (x);
2099 return t;
2101 return t;
2104 /* If we are processing a typedef statement, generate a whole new
2105 ..._TYPE node (which will be just an variant of the existing
2106 ..._TYPE node with identical properties) and then install the
2107 TYPE_DECL node generated to represent the typedef name as the
2108 TYPE_NAME of this brand new (duplicate) ..._TYPE node.
2110 The whole point here is to end up with a situation where each
2111 and every ..._TYPE node the compiler creates will be uniquely
2112 associated with AT MOST one node representing a typedef name.
2113 This way, even though the compiler substitutes corresponding
2114 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
2115 early on, later parts of the compiler can always do the reverse
2116 translation and get back the corresponding typedef name. For
2117 example, given:
2119 typedef struct S MY_TYPE;
2120 MY_TYPE object;
2122 Later parts of the compiler might only know that `object' was of
2123 type `struct S' if it were not for code just below. With this
2124 code however, later parts of the compiler see something like:
2126 struct S' == struct S
2127 typedef struct S' MY_TYPE;
2128 struct S' object;
2130 And they can then deduce (from the node for type struct S') that
2131 the original object declaration was:
2133 MY_TYPE object;
2135 Being able to do this is important for proper support of protoize,
2136 and also for generating precise symbolic debugging information
2137 which takes full account of the programmer's (typedef) vocabulary.
2139 Obviously, we don't want to generate a duplicate ..._TYPE node if
2140 the TYPE_DECL node that we are now processing really represents a
2141 standard built-in type.
2143 Since all standard types are effectively declared at line zero
2144 in the source file, we can easily check to see if we are working
2145 on a standard type by checking the current value of lineno. */
2147 if (TREE_CODE (x) == TYPE_DECL)
2149 if (DECL_SOURCE_LINE (x) == 0)
2151 if (TYPE_NAME (TREE_TYPE (x)) == 0)
2152 TYPE_NAME (TREE_TYPE (x)) = x;
2154 else if (TREE_TYPE (x) != error_mark_node
2155 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
2157 tree tt = TREE_TYPE (x);
2158 DECL_ORIGINAL_TYPE (x) = tt;
2159 tt = build_type_copy (tt);
2160 TYPE_NAME (tt) = x;
2161 TREE_TYPE (x) = tt;
2165 /* Multiple external decls of the same identifier ought to match.
2166 Check against both global declarations (when traditional) and out of
2167 scope (limbo) block level declarations.
2169 We get warnings about inline functions where they are defined.
2170 Avoid duplicate warnings where they are used. */
2171 if (TREE_PUBLIC (x) && ! DECL_INLINE (x))
2173 tree decl;
2175 if (flag_traditional && IDENTIFIER_GLOBAL_VALUE (name) != 0
2176 && (DECL_EXTERNAL (IDENTIFIER_GLOBAL_VALUE (name))
2177 || TREE_PUBLIC (IDENTIFIER_GLOBAL_VALUE (name))))
2178 decl = IDENTIFIER_GLOBAL_VALUE (name);
2179 else if (IDENTIFIER_LIMBO_VALUE (name) != 0)
2180 /* Decls in limbo are always extern, so no need to check that. */
2181 decl = IDENTIFIER_LIMBO_VALUE (name);
2182 else
2183 decl = 0;
2185 if (decl && ! comptypes (TREE_TYPE (x), TREE_TYPE (decl))
2186 /* If old decl is built-in, we already warned if we should. */
2187 && !DECL_BUILT_IN (decl))
2189 pedwarn_with_decl (x,
2190 "type mismatch with previous external decl");
2191 pedwarn_with_decl (decl, "previous external decl of `%s'");
2195 /* If a function has had an implicit declaration, and then is defined,
2196 make sure they are compatible. */
2198 if (IDENTIFIER_IMPLICIT_DECL (name) != 0
2199 && IDENTIFIER_GLOBAL_VALUE (name) == 0
2200 && TREE_CODE (x) == FUNCTION_DECL
2201 && ! comptypes (TREE_TYPE (x),
2202 TREE_TYPE (IDENTIFIER_IMPLICIT_DECL (name))))
2204 warning_with_decl (x, "type mismatch with previous implicit declaration");
2205 warning_with_decl (IDENTIFIER_IMPLICIT_DECL (name),
2206 "previous implicit declaration of `%s'");
2209 /* In PCC-compatibility mode, extern decls of vars with no current decl
2210 take effect at top level no matter where they are. */
2211 if (flag_traditional && DECL_EXTERNAL (x)
2212 && lookup_name (name) == 0)
2214 tree type = TREE_TYPE (x);
2216 /* But don't do this if the type contains temporary nodes. */
2217 while (type)
2219 if (type == error_mark_node)
2220 break;
2221 if (! TYPE_CONTEXT (type))
2223 warning_with_decl (x, "type of external `%s' is not global");
2224 /* By exiting the loop early, we leave TYPE nonzero,
2225 and thus prevent globalization of the decl. */
2226 break;
2228 else if (TREE_CODE (type) == FUNCTION_TYPE
2229 && TYPE_ARG_TYPES (type) != 0)
2230 /* The types might not be truly local,
2231 but the list of arg types certainly is temporary.
2232 Since prototypes are nontraditional,
2233 ok not to do the traditional thing. */
2234 break;
2235 type = TREE_TYPE (type);
2238 if (type == 0)
2239 b = global_binding_level;
2242 /* This name is new in its binding level.
2243 Install the new declaration and return it. */
2244 if (b == global_binding_level)
2246 /* Install a global value. */
2248 /* If the first global decl has external linkage,
2249 warn if we later see static one. */
2250 if (IDENTIFIER_GLOBAL_VALUE (name) == 0 && TREE_PUBLIC (x))
2251 TREE_PUBLIC (name) = 1;
2253 IDENTIFIER_GLOBAL_VALUE (name) = x;
2255 /* We no longer care about any previous block level declarations. */
2256 IDENTIFIER_LIMBO_VALUE (name) = 0;
2258 /* Don't forget if the function was used via an implicit decl. */
2259 if (IDENTIFIER_IMPLICIT_DECL (name)
2260 && TREE_USED (IDENTIFIER_IMPLICIT_DECL (name)))
2261 TREE_USED (x) = 1, TREE_USED (name) = 1;
2263 /* Don't forget if its address was taken in that way. */
2264 if (IDENTIFIER_IMPLICIT_DECL (name)
2265 && TREE_ADDRESSABLE (IDENTIFIER_IMPLICIT_DECL (name)))
2266 TREE_ADDRESSABLE (x) = 1;
2268 /* Warn about mismatches against previous implicit decl. */
2269 if (IDENTIFIER_IMPLICIT_DECL (name) != 0
2270 /* If this real decl matches the implicit, don't complain. */
2271 && ! (TREE_CODE (x) == FUNCTION_DECL
2272 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (x)))
2273 == integer_type_node)))
2274 pedwarn ("`%s' was previously implicitly declared to return `int'",
2275 IDENTIFIER_POINTER (name));
2277 /* If this decl is `static' and an `extern' was seen previously,
2278 that is erroneous. */
2279 if (TREE_PUBLIC (name)
2280 && ! TREE_PUBLIC (x) && ! DECL_EXTERNAL (x))
2282 /* Okay to redeclare an ANSI built-in as static. */
2283 if (t != 0 && DECL_BUILT_IN (t))
2285 /* Okay to declare a non-ANSI built-in as anything. */
2286 else if (t != 0 && DECL_BUILT_IN_NONANSI (t))
2288 /* Okay to have global type decl after an earlier extern
2289 declaration inside a lexical block. */
2290 else if (TREE_CODE (x) == TYPE_DECL)
2292 else if (IDENTIFIER_IMPLICIT_DECL (name))
2294 if (! TREE_THIS_VOLATILE (name))
2295 pedwarn ("`%s' was declared implicitly `extern' and later `static'",
2296 IDENTIFIER_POINTER (name));
2298 else
2299 pedwarn ("`%s' was declared `extern' and later `static'",
2300 IDENTIFIER_POINTER (name));
2303 else
2305 /* Here to install a non-global value. */
2306 tree oldlocal = IDENTIFIER_LOCAL_VALUE (name);
2307 tree oldglobal = IDENTIFIER_GLOBAL_VALUE (name);
2308 IDENTIFIER_LOCAL_VALUE (name) = x;
2310 /* If this is an extern function declaration, see if we
2311 have a global definition or declaration for the function. */
2312 if (oldlocal == 0
2313 && DECL_EXTERNAL (x) && !DECL_INLINE (x)
2314 && oldglobal != 0
2315 && TREE_CODE (x) == FUNCTION_DECL
2316 && TREE_CODE (oldglobal) == FUNCTION_DECL)
2318 /* We have one. Their types must agree. */
2319 if (! comptypes (TREE_TYPE (x),
2320 TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (name))))
2321 pedwarn_with_decl (x, "extern declaration of `%s' doesn't match global one");
2322 else
2324 /* Inner extern decl is inline if global one is.
2325 Copy enough to really inline it. */
2326 if (DECL_INLINE (oldglobal))
2328 DECL_INLINE (x) = DECL_INLINE (oldglobal);
2329 DECL_INITIAL (x) = (current_function_decl == oldglobal
2330 ? 0 : DECL_INITIAL (oldglobal));
2331 DECL_SAVED_INSNS (x) = DECL_SAVED_INSNS (oldglobal);
2332 DECL_FRAME_SIZE (x) = DECL_FRAME_SIZE (oldglobal);
2333 DECL_ARGUMENTS (x) = DECL_ARGUMENTS (oldglobal);
2334 DECL_RESULT (x) = DECL_RESULT (oldglobal);
2335 TREE_ASM_WRITTEN (x) = TREE_ASM_WRITTEN (oldglobal);
2336 DECL_ABSTRACT_ORIGIN (x) = DECL_ORIGIN (oldglobal);
2338 /* Inner extern decl is built-in if global one is. */
2339 if (DECL_BUILT_IN (oldglobal))
2341 DECL_BUILT_IN_CLASS (x) = DECL_BUILT_IN_CLASS (oldglobal);
2342 DECL_FUNCTION_CODE (x) = DECL_FUNCTION_CODE (oldglobal);
2344 /* Keep the arg types from a file-scope fcn defn. */
2345 if (TYPE_ARG_TYPES (TREE_TYPE (oldglobal)) != 0
2346 && DECL_INITIAL (oldglobal)
2347 && TYPE_ARG_TYPES (TREE_TYPE (x)) == 0)
2348 TREE_TYPE (x) = TREE_TYPE (oldglobal);
2352 #if 0 /* This case is probably sometimes the right thing to do. */
2353 /* If we have a local external declaration,
2354 then any file-scope declaration should not
2355 have been static. */
2356 if (oldlocal == 0 && oldglobal != 0
2357 && !TREE_PUBLIC (oldglobal)
2358 && DECL_EXTERNAL (x) && TREE_PUBLIC (x))
2359 warning ("`%s' locally external but globally static",
2360 IDENTIFIER_POINTER (name));
2361 #endif
2363 /* If we have a local external declaration,
2364 and no file-scope declaration has yet been seen,
2365 then if we later have a file-scope decl it must not be static. */
2366 if (oldlocal == 0
2367 && DECL_EXTERNAL (x)
2368 && TREE_PUBLIC (x))
2370 if (oldglobal == 0)
2371 TREE_PUBLIC (name) = 1;
2373 /* Save this decl, so that we can do type checking against
2374 other decls after it falls out of scope.
2376 Only save it once. This prevents temporary decls created in
2377 expand_inline_function from being used here, since this
2378 will have been set when the inline function was parsed.
2379 It also helps give slightly better warnings. */
2380 if (IDENTIFIER_LIMBO_VALUE (name) == 0)
2381 IDENTIFIER_LIMBO_VALUE (name) = x;
2384 /* Warn if shadowing an argument at the top level of the body. */
2385 if (oldlocal != 0 && !DECL_EXTERNAL (x)
2386 /* This warning doesn't apply to the parms of a nested fcn. */
2387 && ! current_binding_level->parm_flag
2388 /* Check that this is one level down from the parms. */
2389 && current_binding_level->level_chain->parm_flag
2390 /* Check that the decl being shadowed
2391 comes from the parm level, one level up. */
2392 && chain_member (oldlocal, current_binding_level->level_chain->names))
2394 if (TREE_CODE (oldlocal) == PARM_DECL)
2395 pedwarn ("declaration of `%s' shadows a parameter",
2396 IDENTIFIER_POINTER (name));
2397 else
2398 pedwarn ("declaration of `%s' shadows a symbol from the parameter list",
2399 IDENTIFIER_POINTER (name));
2402 /* Maybe warn if shadowing something else. */
2403 else if (warn_shadow && !DECL_EXTERNAL (x)
2404 /* No shadow warnings for internally generated vars. */
2405 && DECL_SOURCE_LINE (x) != 0
2406 /* No shadow warnings for vars made for inlining. */
2407 && ! DECL_FROM_INLINE (x))
2409 char *id = IDENTIFIER_POINTER (name);
2411 if (TREE_CODE (x) == PARM_DECL
2412 && current_binding_level->level_chain->parm_flag)
2413 /* Don't warn about the parm names in function declarator
2414 within a function declarator.
2415 It would be nice to avoid warning in any function
2416 declarator in a declaration, as opposed to a definition,
2417 but there is no way to tell it's not a definition. */
2419 else if (oldlocal != 0 && TREE_CODE (oldlocal) == PARM_DECL)
2420 warning ("declaration of `%s' shadows a parameter", id);
2421 else if (oldlocal != 0)
2422 warning ("declaration of `%s' shadows previous local", id);
2423 else if (IDENTIFIER_GLOBAL_VALUE (name) != 0
2424 && IDENTIFIER_GLOBAL_VALUE (name) != error_mark_node)
2425 warning ("declaration of `%s' shadows global declaration", id);
2428 /* If storing a local value, there may already be one (inherited).
2429 If so, record it for restoration when this binding level ends. */
2430 if (oldlocal != 0)
2431 b->shadowed = tree_cons (name, oldlocal, b->shadowed);
2434 /* Keep count of variables in this level with incomplete type. */
2435 if (TYPE_SIZE (TREE_TYPE (x)) == 0)
2436 ++b->n_incomplete;
2439 /* Put decls on list in reverse order.
2440 We will reverse them later if necessary. */
2441 TREE_CHAIN (x) = b->names;
2442 b->names = x;
2444 return x;
2447 /* Like pushdecl, only it places X in GLOBAL_BINDING_LEVEL, if appropriate. */
2449 tree
2450 pushdecl_top_level (x)
2451 tree x;
2453 register tree t;
2454 register struct binding_level *b = current_binding_level;
2456 current_binding_level = global_binding_level;
2457 t = pushdecl (x);
2458 current_binding_level = b;
2459 return t;
2462 /* Generate an implicit declaration for identifier FUNCTIONID
2463 as a function of type int (). Print a warning if appropriate. */
2465 tree
2466 implicitly_declare (functionid)
2467 tree functionid;
2469 register tree decl;
2470 int traditional_warning = 0;
2471 /* Only one "implicit declaration" warning per identifier. */
2472 int implicit_warning;
2474 /* Save the decl permanently so we can warn if definition follows. */
2475 push_obstacks_nochange ();
2476 end_temporary_allocation ();
2478 /* We used to reuse an old implicit decl here,
2479 but this loses with inline functions because it can clobber
2480 the saved decl chains. */
2481 /* if (IDENTIFIER_IMPLICIT_DECL (functionid) != 0)
2482 decl = IDENTIFIER_IMPLICIT_DECL (functionid);
2483 else */
2484 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2486 /* Warn of implicit decl following explicit local extern decl.
2487 This is probably a program designed for traditional C. */
2488 if (TREE_PUBLIC (functionid) && IDENTIFIER_GLOBAL_VALUE (functionid) == 0)
2489 traditional_warning = 1;
2491 /* Warn once of an implicit declaration. */
2492 implicit_warning = (IDENTIFIER_IMPLICIT_DECL (functionid) == 0);
2494 DECL_EXTERNAL (decl) = 1;
2495 TREE_PUBLIC (decl) = 1;
2497 /* Record that we have an implicit decl and this is it. */
2498 IDENTIFIER_IMPLICIT_DECL (functionid) = decl;
2500 /* ANSI standard says implicit declarations are in the innermost block.
2501 So we record the decl in the standard fashion.
2502 If flag_traditional is set, pushdecl does it top-level. */
2503 pushdecl (decl);
2505 /* This is a no-op in c-lang.c or something real in objc-actions.c. */
2506 maybe_objc_check_decl (decl);
2508 rest_of_decl_compilation (decl, NULL_PTR, 0, 0);
2510 if (mesg_implicit_function_declaration && implicit_warning)
2512 if (mesg_implicit_function_declaration == 2)
2513 error ("implicit declaration of function `%s'",
2514 IDENTIFIER_POINTER (functionid));
2515 else
2516 warning ("implicit declaration of function `%s'",
2517 IDENTIFIER_POINTER (functionid));
2519 else if (warn_traditional && traditional_warning)
2520 warning ("function `%s' was previously declared within a block",
2521 IDENTIFIER_POINTER (functionid));
2523 /* Write a record describing this implicit function declaration to the
2524 prototypes file (if requested). */
2526 gen_aux_info_record (decl, 0, 1, 0);
2528 pop_obstacks ();
2530 return decl;
2533 /* Return zero if the declaration NEWDECL is valid
2534 when the declaration OLDDECL (assumed to be for the same name)
2535 has already been seen.
2536 Otherwise return 1 if NEWDECL is a redefinition, 2 if it is a redeclaration,
2537 and 3 if it is a conflicting declaration. */
2539 static int
2540 redeclaration_error_message (newdecl, olddecl)
2541 tree newdecl, olddecl;
2543 if (TREE_CODE (newdecl) == TYPE_DECL)
2545 if (flag_traditional && TREE_TYPE (newdecl) == TREE_TYPE (olddecl))
2546 return 0;
2547 /* pushdecl creates distinct types for TYPE_DECLs by calling
2548 build_type_copy, so the above comparison generally fails. We do
2549 another test against the TYPE_MAIN_VARIANT of the olddecl, which
2550 is equivalent to what this code used to do before the build_type_copy
2551 call. The variant type distinction should not matter for traditional
2552 code, because it doesn't have type qualifiers. */
2553 if (flag_traditional
2554 && TYPE_MAIN_VARIANT (TREE_TYPE (olddecl)) == TREE_TYPE (newdecl))
2555 return 0;
2556 if (DECL_IN_SYSTEM_HEADER (olddecl) || DECL_IN_SYSTEM_HEADER (newdecl))
2557 return 0;
2558 return 1;
2560 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2562 /* Declarations of functions can insist on internal linkage
2563 but they can't be inconsistent with internal linkage,
2564 so there can be no error on that account.
2565 However defining the same name twice is no good. */
2566 if (DECL_INITIAL (olddecl) != 0 && DECL_INITIAL (newdecl) != 0
2567 /* However, defining once as extern inline and a second
2568 time in another way is ok. */
2569 && !(DECL_INLINE (olddecl) && DECL_EXTERNAL (olddecl)
2570 && !(DECL_INLINE (newdecl) && DECL_EXTERNAL (newdecl))))
2571 return 1;
2572 return 0;
2574 else if (current_binding_level == global_binding_level)
2576 /* Objects declared at top level: */
2577 /* If at least one is a reference, it's ok. */
2578 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
2579 return 0;
2580 /* Reject two definitions. */
2581 if (DECL_INITIAL (olddecl) != 0 && DECL_INITIAL (newdecl) != 0)
2582 return 1;
2583 /* Now we have two tentative defs, or one tentative and one real def. */
2584 /* Insist that the linkage match. */
2585 if (TREE_PUBLIC (olddecl) != TREE_PUBLIC (newdecl))
2586 return 3;
2587 return 0;
2589 else if (current_binding_level->parm_flag
2590 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2591 return 0;
2592 else
2594 /* Newdecl has block scope. If olddecl has block scope also, then
2595 reject two definitions, and reject a definition together with an
2596 external reference. Otherwise, it is OK, because newdecl must
2597 be an extern reference to olddecl. */
2598 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl))
2599 && DECL_CONTEXT (newdecl) == DECL_CONTEXT (olddecl))
2600 return 2;
2601 return 0;
2605 /* Get the LABEL_DECL corresponding to identifier ID as a label.
2606 Create one if none exists so far for the current function.
2607 This function is called for both label definitions and label references. */
2609 tree
2610 lookup_label (id)
2611 tree id;
2613 register tree decl = IDENTIFIER_LABEL_VALUE (id);
2615 if (current_function_decl == 0)
2617 error ("label %s referenced outside of any function",
2618 IDENTIFIER_POINTER (id));
2619 return 0;
2622 /* Use a label already defined or ref'd with this name. */
2623 if (decl != 0)
2625 /* But not if it is inherited and wasn't declared to be inheritable. */
2626 if (DECL_CONTEXT (decl) != current_function_decl
2627 && ! C_DECLARED_LABEL_FLAG (decl))
2628 return shadow_label (id);
2629 return decl;
2632 decl = build_decl (LABEL_DECL, id, void_type_node);
2634 /* Make sure every label has an rtx. */
2635 label_rtx (decl);
2637 /* A label not explicitly declared must be local to where it's ref'd. */
2638 DECL_CONTEXT (decl) = current_function_decl;
2640 DECL_MODE (decl) = VOIDmode;
2642 /* Say where one reference is to the label,
2643 for the sake of the error if it is not defined. */
2644 DECL_SOURCE_LINE (decl) = lineno;
2645 DECL_SOURCE_FILE (decl) = input_filename;
2647 IDENTIFIER_LABEL_VALUE (id) = decl;
2649 named_labels = tree_cons (NULL_TREE, decl, named_labels);
2651 return decl;
2654 /* Make a label named NAME in the current function,
2655 shadowing silently any that may be inherited from containing functions
2656 or containing scopes.
2658 Note that valid use, if the label being shadowed
2659 comes from another scope in the same function,
2660 requires calling declare_nonlocal_label right away. */
2662 tree
2663 shadow_label (name)
2664 tree name;
2666 register tree decl = IDENTIFIER_LABEL_VALUE (name);
2668 if (decl != 0)
2670 register tree dup;
2672 /* Check to make sure that the label hasn't already been declared
2673 at this label scope */
2674 for (dup = named_labels; dup; dup = TREE_CHAIN (dup))
2675 if (TREE_VALUE (dup) == decl)
2677 error ("duplicate label declaration `%s'",
2678 IDENTIFIER_POINTER (name));
2679 error_with_decl (TREE_VALUE (dup),
2680 "this is a previous declaration");
2681 /* Just use the previous declaration. */
2682 return lookup_label (name);
2685 shadowed_labels = tree_cons (NULL_TREE, decl, shadowed_labels);
2686 IDENTIFIER_LABEL_VALUE (name) = decl = 0;
2689 return lookup_label (name);
2692 /* Define a label, specifying the location in the source file.
2693 Return the LABEL_DECL node for the label, if the definition is valid.
2694 Otherwise return 0. */
2696 tree
2697 define_label (filename, line, name)
2698 char *filename;
2699 int line;
2700 tree name;
2702 tree decl = lookup_label (name);
2704 /* If label with this name is known from an outer context, shadow it. */
2705 if (decl != 0 && DECL_CONTEXT (decl) != current_function_decl)
2707 shadowed_labels = tree_cons (NULL_TREE, decl, shadowed_labels);
2708 IDENTIFIER_LABEL_VALUE (name) = 0;
2709 decl = lookup_label (name);
2712 if (DECL_INITIAL (decl) != 0)
2714 error ("duplicate label `%s'", IDENTIFIER_POINTER (name));
2715 return 0;
2717 else
2719 /* Mark label as having been defined. */
2720 DECL_INITIAL (decl) = error_mark_node;
2721 /* Say where in the source. */
2722 DECL_SOURCE_FILE (decl) = filename;
2723 DECL_SOURCE_LINE (decl) = line;
2724 return decl;
2728 /* Return the list of declarations of the current level.
2729 Note that this list is in reverse order unless/until
2730 you nreverse it; and when you do nreverse it, you must
2731 store the result back using `storedecls' or you will lose. */
2733 tree
2734 getdecls ()
2736 return current_binding_level->names;
2739 /* Return the list of type-tags (for structs, etc) of the current level. */
2741 tree
2742 gettags ()
2744 return current_binding_level->tags;
2747 /* Store the list of declarations of the current level.
2748 This is done for the parameter declarations of a function being defined,
2749 after they are modified in the light of any missing parameters. */
2751 static void
2752 storedecls (decls)
2753 tree decls;
2755 current_binding_level->names = decls;
2758 /* Similarly, store the list of tags of the current level. */
2760 static void
2761 storetags (tags)
2762 tree tags;
2764 current_binding_level->tags = tags;
2767 /* Given NAME, an IDENTIFIER_NODE,
2768 return the structure (or union or enum) definition for that name.
2769 Searches binding levels from BINDING_LEVEL up to the global level.
2770 If THISLEVEL_ONLY is nonzero, searches only the specified context
2771 (but skips any tag-transparent contexts to find one that is
2772 meaningful for tags).
2773 CODE says which kind of type the caller wants;
2774 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2775 If the wrong kind of type is found, an error is reported. */
2777 static tree
2778 lookup_tag (code, name, binding_level, thislevel_only)
2779 enum tree_code code;
2780 struct binding_level *binding_level;
2781 tree name;
2782 int thislevel_only;
2784 register struct binding_level *level;
2786 for (level = binding_level; level; level = level->level_chain)
2788 register tree tail;
2789 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
2791 if (TREE_PURPOSE (tail) == name)
2793 if (TREE_CODE (TREE_VALUE (tail)) != code)
2795 /* Definition isn't the kind we were looking for. */
2796 pending_invalid_xref = name;
2797 pending_invalid_xref_file = input_filename;
2798 pending_invalid_xref_line = lineno;
2800 return TREE_VALUE (tail);
2803 if (thislevel_only && ! level->tag_transparent)
2804 return NULL_TREE;
2806 return NULL_TREE;
2809 /* Print an error message now
2810 for a recent invalid struct, union or enum cross reference.
2811 We don't print them immediately because they are not invalid
2812 when used in the `struct foo;' construct for shadowing. */
2814 void
2815 pending_xref_error ()
2817 if (pending_invalid_xref != 0)
2818 error_with_file_and_line (pending_invalid_xref_file,
2819 pending_invalid_xref_line,
2820 "`%s' defined as wrong kind of tag",
2821 IDENTIFIER_POINTER (pending_invalid_xref));
2822 pending_invalid_xref = 0;
2825 /* Given a type, find the tag that was defined for it and return the tag name.
2826 Otherwise return 0. */
2828 static tree
2829 lookup_tag_reverse (type)
2830 tree type;
2832 register struct binding_level *level;
2834 for (level = current_binding_level; level; level = level->level_chain)
2836 register tree tail;
2837 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
2839 if (TREE_VALUE (tail) == type)
2840 return TREE_PURPOSE (tail);
2843 return NULL_TREE;
2846 /* Look up NAME in the current binding level and its superiors
2847 in the namespace of variables, functions and typedefs.
2848 Return a ..._DECL node of some kind representing its definition,
2849 or return 0 if it is undefined. */
2851 tree
2852 lookup_name (name)
2853 tree name;
2855 register tree val;
2856 if (current_binding_level != global_binding_level
2857 && IDENTIFIER_LOCAL_VALUE (name))
2858 val = IDENTIFIER_LOCAL_VALUE (name);
2859 else
2860 val = IDENTIFIER_GLOBAL_VALUE (name);
2861 return val;
2864 /* Similar to `lookup_name' but look only at current binding level. */
2866 tree
2867 lookup_name_current_level (name)
2868 tree name;
2870 register tree t;
2872 if (current_binding_level == global_binding_level)
2873 return IDENTIFIER_GLOBAL_VALUE (name);
2875 if (IDENTIFIER_LOCAL_VALUE (name) == 0)
2876 return 0;
2878 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
2879 if (DECL_NAME (t) == name)
2880 break;
2882 return t;
2885 /* Mark ARG for GC. */
2886 static void
2887 mark_binding_level (arg)
2888 void *arg;
2890 struct binding_level *level = *(struct binding_level **) arg;
2892 while (level)
2894 ggc_mark_tree (level->names);
2895 ggc_mark_tree (level->tags);
2896 ggc_mark_tree (level->shadowed);
2897 ggc_mark_tree (level->blocks);
2898 ggc_mark_tree (level->this_block);
2899 ggc_mark_tree (level->parm_order);
2900 level = level->level_chain;
2904 /* Create the predefined scalar types of C,
2905 and some nodes representing standard constants (0, 1, (void *) 0).
2906 Initialize the global binding level.
2907 Make definitions for built-in primitive functions. */
2909 void
2910 init_decl_processing ()
2912 register tree endlink;
2913 tree ptr_ftype_void, ptr_ftype_ptr;
2914 int wchar_type_size;
2915 tree array_domain_type;
2917 current_function_decl = NULL;
2918 named_labels = NULL;
2919 current_binding_level = NULL_BINDING_LEVEL;
2920 free_binding_level = NULL_BINDING_LEVEL;
2921 pushlevel (0); /* make the binding_level structure for global names */
2922 global_binding_level = current_binding_level;
2924 build_common_tree_nodes (flag_signed_char);
2926 /* Define `int' and `char' first so that dbx will output them first. */
2927 pushdecl (build_decl (TYPE_DECL, ridpointers[(int) RID_INT],
2928 integer_type_node));
2929 pushdecl (build_decl (TYPE_DECL, get_identifier ("char"),
2930 char_type_node));
2931 pushdecl (build_decl (TYPE_DECL, get_identifier ("long int"),
2932 long_integer_type_node));
2933 pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned int"),
2934 unsigned_type_node));
2935 pushdecl (build_decl (TYPE_DECL, get_identifier ("long unsigned int"),
2936 long_unsigned_type_node));
2937 pushdecl (build_decl (TYPE_DECL, get_identifier ("long long int"),
2938 long_long_integer_type_node));
2939 pushdecl (build_decl (TYPE_DECL, get_identifier ("long long unsigned int"),
2940 long_long_unsigned_type_node));
2941 pushdecl (build_decl (TYPE_DECL, get_identifier ("short int"),
2942 short_integer_type_node));
2943 pushdecl (build_decl (TYPE_DECL, get_identifier ("short unsigned int"),
2944 short_unsigned_type_node));
2945 pushdecl (build_decl (TYPE_DECL, get_identifier ("signed char"),
2946 signed_char_type_node));
2947 pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned char"),
2948 unsigned_char_type_node));
2949 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intQI_type_node));
2950 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intHI_type_node));
2951 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intSI_type_node));
2952 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intDI_type_node));
2953 #if HOST_BITS_PER_WIDE_INT >= 64
2954 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intTI_type_node));
2955 #endif
2956 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intQI_type_node));
2957 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intHI_type_node));
2958 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intSI_type_node));
2959 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intDI_type_node));
2960 #if HOST_BITS_PER_WIDE_INT >= 64
2961 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intTI_type_node));
2962 #endif
2964 /* `unsigned long' is the standard type for sizeof.
2965 Traditionally, use a signed type.
2966 Note that stddef.h uses `unsigned long',
2967 and this must agree, even if long and int are the same size. */
2968 set_sizetype
2969 (TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (get_identifier (SIZE_TYPE))));
2970 if (flag_traditional && TREE_UNSIGNED (sizetype))
2971 set_sizetype (signed_type (sizetype));
2973 /* Create the widest literal types. */
2974 widest_integer_literal_type_node = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
2975 widest_unsigned_literal_type_node = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
2976 pushdecl (build_decl (TYPE_DECL, NULL_TREE,
2977 widest_integer_literal_type_node));
2978 pushdecl (build_decl (TYPE_DECL, NULL_TREE,
2979 widest_unsigned_literal_type_node));
2981 build_common_tree_nodes_2 (flag_short_double);
2983 pushdecl (build_decl (TYPE_DECL, ridpointers[(int) RID_FLOAT],
2984 float_type_node));
2985 pushdecl (build_decl (TYPE_DECL, ridpointers[(int) RID_DOUBLE],
2986 double_type_node));
2987 pushdecl (build_decl (TYPE_DECL, get_identifier ("long double"),
2988 long_double_type_node));
2989 pushdecl (build_decl (TYPE_DECL, get_identifier ("complex int"),
2990 complex_integer_type_node));
2991 pushdecl (build_decl (TYPE_DECL, get_identifier ("complex float"),
2992 complex_float_type_node));
2993 pushdecl (build_decl (TYPE_DECL, get_identifier ("complex double"),
2994 complex_double_type_node));
2995 pushdecl (build_decl (TYPE_DECL, get_identifier ("complex long double"),
2996 complex_long_double_type_node));
2997 pushdecl (build_decl (TYPE_DECL,
2998 ridpointers[(int) RID_VOID], void_type_node));
3000 #ifdef MD_INIT_BUILTINS
3001 MD_INIT_BUILTINS;
3002 #endif
3004 wchar_type_node = get_identifier (flag_short_wchar
3005 ? "short unsigned int"
3006 : WCHAR_TYPE);
3007 wchar_type_node = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (wchar_type_node));
3008 wchar_type_size = TYPE_PRECISION (wchar_type_node);
3009 signed_wchar_type_node = signed_type (wchar_type_node);
3010 unsigned_wchar_type_node = unsigned_type (wchar_type_node);
3012 boolean_type_node = integer_type_node;
3013 boolean_true_node = integer_one_node;
3014 boolean_false_node = integer_zero_node;
3016 string_type_node = build_pointer_type (char_type_node);
3017 const_string_type_node
3018 = build_pointer_type (build_type_variant (char_type_node, 1, 0));
3020 /* Make a type to be the domain of a few array types
3021 whose domains don't really matter.
3022 200 is small enough that it always fits in size_t
3023 and large enough that it can hold most function names for the
3024 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
3025 array_domain_type = build_index_type (build_int_2 (200, 0));
3027 /* make a type for arrays of characters.
3028 With luck nothing will ever really depend on the length of this
3029 array type. */
3030 char_array_type_node
3031 = build_array_type (char_type_node, array_domain_type);
3032 /* Likewise for arrays of ints. */
3033 int_array_type_node
3034 = build_array_type (integer_type_node, array_domain_type);
3035 /* This is for wide string constants. */
3036 wchar_array_type_node
3037 = build_array_type (wchar_type_node, array_domain_type);
3039 void_list_node = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
3041 default_function_type
3042 = build_function_type (integer_type_node, NULL_TREE);
3043 ptrdiff_type_node
3044 = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (get_identifier (PTRDIFF_TYPE)));
3046 c_common_nodes_and_builtins (0, flag_no_builtin, flag_no_nonansi_builtin);
3048 endlink = void_list_node;
3049 ptr_ftype_void = build_function_type (ptr_type_node, endlink);
3050 ptr_ftype_ptr
3051 = build_function_type (ptr_type_node,
3052 tree_cons (NULL_TREE, ptr_type_node, endlink));
3054 builtin_function ("__builtin_aggregate_incoming_address",
3055 build_function_type (ptr_type_node, NULL_TREE),
3056 BUILT_IN_AGGREGATE_INCOMING_ADDRESS,
3057 BUILT_IN_NORMAL, NULL_PTR);
3059 /* Hooks for the DWARF 2 __throw routine. */
3060 builtin_function ("__builtin_unwind_init",
3061 build_function_type (void_type_node, endlink),
3062 BUILT_IN_UNWIND_INIT, BUILT_IN_NORMAL, NULL_PTR);
3063 builtin_function ("__builtin_dwarf_cfa", ptr_ftype_void,
3064 BUILT_IN_DWARF_CFA, BUILT_IN_NORMAL, NULL_PTR);
3065 builtin_function ("__builtin_dwarf_fp_regnum",
3066 build_function_type (unsigned_type_node, endlink),
3067 BUILT_IN_DWARF_FP_REGNUM, BUILT_IN_NORMAL, NULL_PTR);
3068 builtin_function ("__builtin_init_dwarf_reg_size_table", void_ftype_ptr,
3069 BUILT_IN_INIT_DWARF_REG_SIZES, BUILT_IN_NORMAL, NULL_PTR);
3070 builtin_function ("__builtin_frob_return_addr", ptr_ftype_ptr,
3071 BUILT_IN_FROB_RETURN_ADDR, BUILT_IN_NORMAL, NULL_PTR);
3072 builtin_function ("__builtin_extract_return_addr", ptr_ftype_ptr,
3073 BUILT_IN_EXTRACT_RETURN_ADDR, BUILT_IN_NORMAL, NULL_PTR);
3074 builtin_function
3075 ("__builtin_eh_return",
3076 build_function_type (void_type_node,
3077 tree_cons (NULL_TREE, ptr_type_node,
3078 tree_cons (NULL_TREE,
3079 type_for_mode (ptr_mode, 0),
3080 tree_cons (NULL_TREE,
3081 ptr_type_node,
3082 endlink)))),
3083 BUILT_IN_EH_RETURN, BUILT_IN_NORMAL, NULL_PTR);
3085 pedantic_lvalues = pedantic;
3087 /* Create the global bindings for __FUNCTION__ and __PRETTY_FUNCTION__. */
3088 declare_function_name ();
3090 start_identifier_warnings ();
3092 /* Prepare to check format strings against argument lists. */
3093 init_function_format_info ();
3095 init_iterators ();
3097 incomplete_decl_finalize_hook = finish_incomplete_decl;
3099 lang_get_alias_set = c_get_alias_set;
3101 /* Record our roots. */
3103 ggc_add_tree_root (c_global_trees, CTI_MAX);
3104 ggc_add_tree_root (&current_function_decl, 1);
3105 ggc_add_tree_root (&named_labels, 1);
3106 ggc_add_tree_root (&shadowed_labels, 1);
3107 ggc_add_root (&current_binding_level, 1, sizeof current_binding_level,
3108 mark_binding_level);
3109 ggc_add_root (&label_level_chain, 1, sizeof label_level_chain,
3110 mark_binding_level);
3111 ggc_add_tree_root (&static_ctors, 1);
3112 ggc_add_tree_root (&static_dtors, 1);
3115 /* Return a definition for a builtin function named NAME and whose data type
3116 is TYPE. TYPE should be a function type with argument types.
3117 FUNCTION_CODE tells later passes how to compile calls to this function.
3118 See tree.h for its possible values.
3120 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
3121 the name to be called if we can't opencode the function. */
3123 tree
3124 builtin_function (name, type, function_code, class, library_name)
3125 const char *name;
3126 tree type;
3127 int function_code;
3128 enum built_in_class class;
3129 const char *library_name;
3131 tree decl = build_decl (FUNCTION_DECL, get_identifier (name), type);
3132 DECL_EXTERNAL (decl) = 1;
3133 TREE_PUBLIC (decl) = 1;
3134 /* If -traditional, permit redefining a builtin function any way you like.
3135 (Though really, if the program redefines these functions,
3136 it probably won't work right unless compiled with -fno-builtin.) */
3137 if (flag_traditional && name[0] != '_')
3138 DECL_BUILT_IN_NONANSI (decl) = 1;
3139 if (library_name)
3140 DECL_ASSEMBLER_NAME (decl) = get_identifier (library_name);
3141 make_decl_rtl (decl, NULL_PTR, 1);
3142 pushdecl (decl);
3143 DECL_BUILT_IN_CLASS (decl) = class;
3144 DECL_FUNCTION_CODE (decl) = function_code;
3146 /* Warn if a function in the namespace for users
3147 is used without an occasion to consider it declared. */
3148 if (name[0] != '_' || name[1] != '_')
3149 C_DECL_ANTICIPATED (decl) = 1;
3151 return decl;
3154 /* Called when a declaration is seen that contains no names to declare.
3155 If its type is a reference to a structure, union or enum inherited
3156 from a containing scope, shadow that tag name for the current scope
3157 with a forward reference.
3158 If its type defines a new named structure or union
3159 or defines an enum, it is valid but we need not do anything here.
3160 Otherwise, it is an error. */
3162 void
3163 shadow_tag (declspecs)
3164 tree declspecs;
3166 shadow_tag_warned (declspecs, 0);
3169 void
3170 shadow_tag_warned (declspecs, warned)
3171 tree declspecs;
3172 int warned;
3173 /* 1 => we have done a pedwarn. 2 => we have done a warning, but
3174 no pedwarn. */
3176 int found_tag = 0;
3177 register tree link;
3178 tree specs, attrs;
3180 pending_invalid_xref = 0;
3182 /* Remove the attributes from declspecs, since they will confuse the
3183 following code. */
3184 split_specs_attrs (declspecs, &specs, &attrs);
3186 for (link = specs; link; link = TREE_CHAIN (link))
3188 register tree value = TREE_VALUE (link);
3189 register enum tree_code code = TREE_CODE (value);
3191 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
3192 /* Used to test also that TYPE_SIZE (value) != 0.
3193 That caused warning for `struct foo;' at top level in the file. */
3195 register tree name = lookup_tag_reverse (value);
3196 register tree t;
3198 found_tag++;
3200 if (name == 0)
3202 if (warned != 1 && code != ENUMERAL_TYPE)
3203 /* Empty unnamed enum OK */
3205 pedwarn ("unnamed struct/union that defines no instances");
3206 warned = 1;
3209 else
3211 t = lookup_tag (code, name, current_binding_level, 1);
3213 if (t == 0)
3215 t = make_node (code);
3216 pushtag (name, t);
3220 else
3222 if (!warned && ! in_system_header)
3224 warning ("useless keyword or type name in empty declaration");
3225 warned = 2;
3230 if (found_tag > 1)
3231 error ("two types specified in one empty declaration");
3233 if (warned != 1)
3235 if (found_tag == 0)
3236 pedwarn ("empty declaration");
3240 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
3242 tree
3243 groktypename (typename)
3244 tree typename;
3246 if (TREE_CODE (typename) != TREE_LIST)
3247 return typename;
3248 return grokdeclarator (TREE_VALUE (typename),
3249 TREE_PURPOSE (typename),
3250 TYPENAME, 0);
3253 /* Return a PARM_DECL node for a given pair of specs and declarator. */
3255 tree
3256 groktypename_in_parm_context (typename)
3257 tree typename;
3259 if (TREE_CODE (typename) != TREE_LIST)
3260 return typename;
3261 return grokdeclarator (TREE_VALUE (typename),
3262 TREE_PURPOSE (typename),
3263 PARM, 0);
3266 /* Decode a declarator in an ordinary declaration or data definition.
3267 This is called as soon as the type information and variable name
3268 have been parsed, before parsing the initializer if any.
3269 Here we create the ..._DECL node, fill in its type,
3270 and put it on the list of decls for the current context.
3271 The ..._DECL node is returned as the value.
3273 Exception: for arrays where the length is not specified,
3274 the type is left null, to be filled in by `finish_decl'.
3276 Function definitions do not come here; they go to start_function
3277 instead. However, external and forward declarations of functions
3278 do go through here. Structure field declarations are done by
3279 grokfield and not through here. */
3281 /* Set this to zero to debug not using the temporary obstack
3282 to parse initializers. */
3283 int debug_temp_inits = 1;
3285 tree
3286 start_decl (declarator, declspecs, initialized, attributes, prefix_attributes)
3287 tree declarator, declspecs;
3288 int initialized;
3289 tree attributes, prefix_attributes;
3291 register tree decl = grokdeclarator (declarator, declspecs,
3292 NORMAL, initialized);
3293 register tree tem;
3294 int init_written = initialized;
3296 /* The corresponding pop_obstacks is in finish_decl. */
3297 push_obstacks_nochange ();
3299 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
3300 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "main"))
3301 warning_with_decl (decl, "`%s' is usually a function");
3303 if (initialized)
3304 /* Is it valid for this decl to have an initializer at all?
3305 If not, set INITIALIZED to zero, which will indirectly
3306 tell `finish_decl' to ignore the initializer once it is parsed. */
3307 switch (TREE_CODE (decl))
3309 case TYPE_DECL:
3310 /* typedef foo = bar means give foo the same type as bar.
3311 We haven't parsed bar yet, so `finish_decl' will fix that up.
3312 Any other case of an initialization in a TYPE_DECL is an error. */
3313 if (pedantic || list_length (declspecs) > 1)
3315 error ("typedef `%s' is initialized",
3316 IDENTIFIER_POINTER (DECL_NAME (decl)));
3317 initialized = 0;
3319 break;
3321 case FUNCTION_DECL:
3322 error ("function `%s' is initialized like a variable",
3323 IDENTIFIER_POINTER (DECL_NAME (decl)));
3324 initialized = 0;
3325 break;
3327 case PARM_DECL:
3328 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
3329 error ("parameter `%s' is initialized",
3330 IDENTIFIER_POINTER (DECL_NAME (decl)));
3331 initialized = 0;
3332 break;
3334 default:
3335 /* Don't allow initializations for incomplete types
3336 except for arrays which might be completed by the initialization. */
3337 if (TYPE_SIZE (TREE_TYPE (decl)) != 0)
3339 /* A complete type is ok if size is fixed. */
3341 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
3342 || C_DECL_VARIABLE_SIZE (decl))
3344 error ("variable-sized object may not be initialized");
3345 initialized = 0;
3348 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
3350 error ("variable `%s' has initializer but incomplete type",
3351 IDENTIFIER_POINTER (DECL_NAME (decl)));
3352 initialized = 0;
3354 else if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))) == 0)
3356 error ("elements of array `%s' have incomplete type",
3357 IDENTIFIER_POINTER (DECL_NAME (decl)));
3358 initialized = 0;
3362 if (initialized)
3364 #if 0 /* Seems redundant with grokdeclarator. */
3365 if (current_binding_level != global_binding_level
3366 && DECL_EXTERNAL (decl)
3367 && TREE_CODE (decl) != FUNCTION_DECL)
3368 warning ("declaration of `%s' has `extern' and is initialized",
3369 IDENTIFIER_POINTER (DECL_NAME (decl)));
3370 #endif
3371 DECL_EXTERNAL (decl) = 0;
3372 if (current_binding_level == global_binding_level)
3373 TREE_STATIC (decl) = 1;
3375 /* Tell `pushdecl' this is an initialized decl
3376 even though we don't yet have the initializer expression.
3377 Also tell `finish_decl' it may store the real initializer. */
3378 DECL_INITIAL (decl) = error_mark_node;
3381 /* If this is a function declaration, write a record describing it to the
3382 prototypes file (if requested). */
3384 if (TREE_CODE (decl) == FUNCTION_DECL)
3385 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
3387 /* ANSI specifies that a tentative definition which is not merged with
3388 a non-tentative definition behaves exactly like a definition with an
3389 initializer equal to zero. (Section 3.7.2)
3390 -fno-common gives strict ANSI behavior. Usually you don't want it.
3391 This matters only for variables with external linkage. */
3392 if (! flag_no_common || ! TREE_PUBLIC (decl))
3393 DECL_COMMON (decl) = 1;
3395 #ifdef SET_DEFAULT_DECL_ATTRIBUTES
3396 SET_DEFAULT_DECL_ATTRIBUTES (decl, attributes);
3397 #endif
3399 /* Set attributes here so if duplicate decl, will have proper attributes. */
3400 decl_attributes (decl, attributes, prefix_attributes);
3402 /* Add this decl to the current binding level.
3403 TEM may equal DECL or it may be a previous decl of the same name. */
3404 tem = pushdecl (decl);
3406 /* For a local variable, define the RTL now. */
3407 if (current_binding_level != global_binding_level
3408 /* But not if this is a duplicate decl
3409 and we preserved the rtl from the previous one
3410 (which may or may not happen). */
3411 && DECL_RTL (tem) == 0)
3413 if (TYPE_SIZE (TREE_TYPE (tem)) != 0)
3414 expand_decl (tem);
3415 else if (TREE_CODE (TREE_TYPE (tem)) == ARRAY_TYPE
3416 && DECL_INITIAL (tem) != 0)
3417 expand_decl (tem);
3420 if (init_written)
3422 /* When parsing and digesting the initializer,
3423 use temporary storage. Do this even if we will ignore the value. */
3424 if (current_binding_level == global_binding_level && debug_temp_inits)
3425 temporary_allocation ();
3428 return tem;
3431 /* Finish processing of a declaration;
3432 install its initial value.
3433 If the length of an array type is not known before,
3434 it must be determined now, from the initial value, or it is an error. */
3436 void
3437 finish_decl (decl, init, asmspec_tree)
3438 tree decl, init;
3439 tree asmspec_tree;
3441 register tree type = TREE_TYPE (decl);
3442 int was_incomplete = (DECL_SIZE (decl) == 0);
3443 int temporary = allocation_temporary_p ();
3444 char *asmspec = 0;
3446 /* If a name was specified, get the string. */
3447 if (asmspec_tree)
3448 asmspec = TREE_STRING_POINTER (asmspec_tree);
3450 /* If `start_decl' didn't like having an initialization, ignore it now. */
3452 if (init != 0 && DECL_INITIAL (decl) == 0)
3453 init = 0;
3454 /* Don't crash if parm is initialized. */
3455 if (TREE_CODE (decl) == PARM_DECL)
3456 init = 0;
3458 if (ITERATOR_P (decl))
3460 if (init == 0)
3461 error_with_decl (decl, "iterator has no initial value");
3462 else
3463 init = save_expr (init);
3466 if (init)
3468 if (TREE_CODE (decl) != TYPE_DECL)
3469 store_init_value (decl, init);
3470 else
3472 /* typedef foo = bar; store the type of bar as the type of foo. */
3473 TREE_TYPE (decl) = TREE_TYPE (init);
3474 DECL_INITIAL (decl) = init = 0;
3478 /* Pop back to the obstack that is current for this binding level.
3479 This is because MAXINDEX, rtl, etc. to be made below
3480 must go in the permanent obstack. But don't discard the
3481 temporary data yet. */
3482 pop_obstacks ();
3483 #if 0 /* pop_obstacks was near the end; this is what was here. */
3484 if (current_binding_level == global_binding_level && temporary)
3485 end_temporary_allocation ();
3486 #endif
3488 /* Deduce size of array from initialization, if not already known */
3490 if (TREE_CODE (type) == ARRAY_TYPE
3491 && TYPE_DOMAIN (type) == 0
3492 && TREE_CODE (decl) != TYPE_DECL)
3494 int do_default
3495 = (TREE_STATIC (decl)
3496 /* Even if pedantic, an external linkage array
3497 may have incomplete type at first. */
3498 ? pedantic && !TREE_PUBLIC (decl)
3499 : !DECL_EXTERNAL (decl));
3500 int failure
3501 = complete_array_type (type, DECL_INITIAL (decl), do_default);
3503 /* Get the completed type made by complete_array_type. */
3504 type = TREE_TYPE (decl);
3506 if (failure == 1)
3507 error_with_decl (decl, "initializer fails to determine size of `%s'");
3509 if (failure == 2)
3511 if (do_default)
3512 error_with_decl (decl, "array size missing in `%s'");
3513 /* If a `static' var's size isn't known,
3514 make it extern as well as static, so it does not get
3515 allocated.
3516 If it is not `static', then do not mark extern;
3517 finish_incomplete_decl will give it a default size
3518 and it will get allocated. */
3519 else if (!pedantic && TREE_STATIC (decl) && ! TREE_PUBLIC (decl))
3520 DECL_EXTERNAL (decl) = 1;
3523 /* TYPE_MAX_VALUE is always one less than the number of elements
3524 in the array, because we start counting at zero. Therefore,
3525 warn only if the value is less than zero. */
3526 if (pedantic && TYPE_DOMAIN (type) != 0
3527 && tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < 0)
3528 error_with_decl (decl, "zero or negative size array `%s'");
3530 layout_decl (decl, 0);
3533 if (TREE_CODE (decl) == VAR_DECL)
3535 if (DECL_SIZE (decl) == 0
3536 && TYPE_SIZE (TREE_TYPE (decl)) != 0)
3537 layout_decl (decl, 0);
3539 if (DECL_SIZE (decl) == 0
3540 && (TREE_STATIC (decl)
3542 /* A static variable with an incomplete type
3543 is an error if it is initialized.
3544 Also if it is not file scope.
3545 Otherwise, let it through, but if it is not `extern'
3546 then it may cause an error message later. */
3547 /* A duplicate_decls call could have changed an extern
3548 declaration into a file scope one. This can be detected
3549 by TREE_ASM_WRITTEN being set. */
3550 (DECL_INITIAL (decl) != 0
3551 || (DECL_CONTEXT (decl) != 0 && ! TREE_ASM_WRITTEN (decl)))
3553 /* An automatic variable with an incomplete type
3554 is an error. */
3555 !DECL_EXTERNAL (decl)))
3557 error_with_decl (decl, "storage size of `%s' isn't known");
3558 TREE_TYPE (decl) = error_mark_node;
3561 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
3562 && DECL_SIZE (decl) != 0)
3564 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
3565 constant_expression_warning (DECL_SIZE (decl));
3566 else
3567 error_with_decl (decl, "storage size of `%s' isn't constant");
3570 if (TREE_USED (type))
3571 TREE_USED (decl) = 1;
3574 /* If this is a function and an assembler name is specified, it isn't
3575 builtin any more. Also reset DECL_RTL so we can give it its new
3576 name. */
3577 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
3579 DECL_BUILT_IN_CLASS (decl) = NOT_BUILT_IN;
3580 DECL_RTL (decl) = 0;
3581 DECL_ASSEMBLER_NAME (decl) = get_identifier (asmspec);
3584 /* Output the assembler code and/or RTL code for variables and functions,
3585 unless the type is an undefined structure or union.
3586 If not, it will get done when the type is completed. */
3588 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3590 if ((flag_traditional || TREE_PERMANENT (decl))
3591 && allocation_temporary_p ())
3593 push_obstacks_nochange ();
3594 end_temporary_allocation ();
3595 /* This is a no-op in c-lang.c or something real in objc-actions.c. */
3596 maybe_objc_check_decl (decl);
3597 rest_of_decl_compilation (decl, asmspec,
3598 (DECL_CONTEXT (decl) == 0
3599 || TREE_ASM_WRITTEN (decl)),
3601 pop_obstacks ();
3603 else
3605 /* This is a no-op in c-lang.c or something real in objc-actions.c. */
3606 maybe_objc_check_decl (decl);
3607 rest_of_decl_compilation (decl, asmspec,
3608 (DECL_CONTEXT (decl) == 0
3609 || TREE_ASM_WRITTEN (decl)),
3612 if (DECL_CONTEXT (decl) != 0)
3614 /* Recompute the RTL of a local array now
3615 if it used to be an incomplete type. */
3616 if (was_incomplete
3617 && ! TREE_STATIC (decl) && ! DECL_EXTERNAL (decl))
3619 /* If we used it already as memory, it must stay in memory. */
3620 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3621 /* If it's still incomplete now, no init will save it. */
3622 if (DECL_SIZE (decl) == 0)
3623 DECL_INITIAL (decl) = 0;
3624 expand_decl (decl);
3626 /* Compute and store the initial value. */
3627 if (TREE_CODE (decl) != FUNCTION_DECL)
3628 expand_decl_init (decl);
3632 if (TREE_CODE (decl) == TYPE_DECL)
3634 /* This is a no-op in c-lang.c or something real in objc-actions.c. */
3635 maybe_objc_check_decl (decl);
3636 rest_of_decl_compilation (decl, NULL_PTR, DECL_CONTEXT (decl) == 0,
3640 /* ??? After 2.3, test (init != 0) instead of TREE_CODE. */
3641 /* This test used to include TREE_PERMANENT, however, we have the same
3642 problem with initializers at the function level. Such initializers get
3643 saved until the end of the function on the momentary_obstack. */
3644 if (!(TREE_CODE (decl) == FUNCTION_DECL && DECL_INLINE (decl))
3645 && temporary
3646 /* DECL_INITIAL is not defined in PARM_DECLs, since it shares
3647 space with DECL_ARG_TYPE. */
3648 && TREE_CODE (decl) != PARM_DECL)
3650 /* We need to remember that this array HAD an initialization,
3651 but discard the actual temporary nodes,
3652 since we can't have a permanent node keep pointing to them. */
3653 /* We make an exception for inline functions, since it's
3654 normal for a local extern redeclaration of an inline function
3655 to have a copy of the top-level decl's DECL_INLINE. */
3656 if (DECL_INITIAL (decl) != 0 && DECL_INITIAL (decl) != error_mark_node)
3658 /* If this is a const variable, then preserve the
3659 initializer instead of discarding it so that we can optimize
3660 references to it. */
3661 /* This test used to include TREE_STATIC, but this won't be set
3662 for function level initializers. */
3663 if (TREE_READONLY (decl) || ITERATOR_P (decl))
3665 preserve_initializer ();
3666 /* Hack? Set the permanent bit for something that is permanent,
3667 but not on the permanent obstack, so as to convince
3668 output_constant_def to make its rtl on the permanent
3669 obstack. */
3670 TREE_PERMANENT (DECL_INITIAL (decl)) = 1;
3672 /* The initializer and DECL must have the same (or equivalent
3673 types), but if the initializer is a STRING_CST, its type
3674 might not be on the right obstack, so copy the type
3675 of DECL. */
3676 TREE_TYPE (DECL_INITIAL (decl)) = type;
3678 else
3679 DECL_INITIAL (decl) = error_mark_node;
3683 /* If requested, warn about definitions of large data objects. */
3685 if (warn_larger_than
3686 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL)
3687 && !DECL_EXTERNAL (decl))
3689 register tree decl_size = DECL_SIZE (decl);
3691 if (decl_size && TREE_CODE (decl_size) == INTEGER_CST)
3693 unsigned units = TREE_INT_CST_LOW(decl_size) / BITS_PER_UNIT;
3695 if (units > larger_than_size)
3696 warning_with_decl (decl, "size of `%s' is %u bytes", units);
3700 #if 0
3701 /* Resume permanent allocation, if not within a function. */
3702 /* The corresponding push_obstacks_nochange is in start_decl,
3703 and in push_parm_decl and in grokfield. */
3704 pop_obstacks ();
3705 #endif
3707 /* If we have gone back from temporary to permanent allocation,
3708 actually free the temporary space that we no longer need. */
3709 if (temporary && !allocation_temporary_p ())
3710 permanent_allocation (0);
3712 /* At the end of a declaration, throw away any variable type sizes
3713 of types defined inside that declaration. There is no use
3714 computing them in the following function definition. */
3715 if (current_binding_level == global_binding_level)
3716 get_pending_sizes ();
3719 /* If DECL has a cleanup, build and return that cleanup here.
3720 This is a callback called by expand_expr. */
3722 tree
3723 maybe_build_cleanup (decl)
3724 tree decl ATTRIBUTE_UNUSED;
3726 /* There are no cleanups in C. */
3727 return NULL_TREE;
3730 /* Given a parsed parameter declaration,
3731 decode it into a PARM_DECL and push that on the current binding level.
3732 Also, for the sake of forward parm decls,
3733 record the given order of parms in `parm_order'. */
3735 void
3736 push_parm_decl (parm)
3737 tree parm;
3739 tree decl;
3740 int old_immediate_size_expand = immediate_size_expand;
3741 /* Don't try computing parm sizes now -- wait till fn is called. */
3742 immediate_size_expand = 0;
3744 /* The corresponding pop_obstacks is in finish_decl. */
3745 push_obstacks_nochange ();
3747 decl = grokdeclarator (TREE_VALUE (TREE_PURPOSE (parm)),
3748 TREE_PURPOSE (TREE_PURPOSE (parm)), PARM, 0);
3749 decl_attributes (decl, TREE_VALUE (TREE_VALUE (parm)),
3750 TREE_PURPOSE (TREE_VALUE (parm)));
3752 #if 0
3753 if (DECL_NAME (decl))
3755 tree olddecl;
3756 olddecl = lookup_name (DECL_NAME (decl));
3757 if (pedantic && olddecl != 0 && TREE_CODE (olddecl) == TYPE_DECL)
3758 pedwarn_with_decl (decl, "ANSI C forbids parameter `%s' shadowing typedef");
3760 #endif
3762 decl = pushdecl (decl);
3764 immediate_size_expand = old_immediate_size_expand;
3766 current_binding_level->parm_order
3767 = tree_cons (NULL_TREE, decl, current_binding_level->parm_order);
3769 /* Add this decl to the current binding level. */
3770 finish_decl (decl, NULL_TREE, NULL_TREE);
3773 /* Clear the given order of parms in `parm_order'.
3774 Used at start of parm list,
3775 and also at semicolon terminating forward decls. */
3777 void
3778 clear_parm_order ()
3780 current_binding_level->parm_order = NULL_TREE;
3783 /* Make TYPE a complete type based on INITIAL_VALUE.
3784 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
3785 2 if there was no information (in which case assume 1 if DO_DEFAULT). */
3788 complete_array_type (type, initial_value, do_default)
3789 tree type;
3790 tree initial_value;
3791 int do_default;
3793 register tree maxindex = NULL_TREE;
3794 int value = 0;
3796 if (initial_value)
3798 /* Note MAXINDEX is really the maximum index,
3799 one less than the size. */
3800 if (TREE_CODE (initial_value) == STRING_CST)
3802 int eltsize
3803 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
3804 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
3805 / eltsize) - 1, 0);
3807 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
3809 tree elts = CONSTRUCTOR_ELTS (initial_value);
3810 maxindex = size_binop (MINUS_EXPR, integer_zero_node, size_one_node);
3811 for (; elts; elts = TREE_CHAIN (elts))
3813 if (TREE_PURPOSE (elts))
3814 maxindex = TREE_PURPOSE (elts);
3815 else
3816 maxindex = size_binop (PLUS_EXPR, maxindex, size_one_node);
3818 maxindex = copy_node (maxindex);
3820 else
3822 /* Make an error message unless that happened already. */
3823 if (initial_value != error_mark_node)
3824 value = 1;
3826 /* Prevent further error messages. */
3827 maxindex = build_int_2 (0, 0);
3831 if (!maxindex)
3833 if (do_default)
3834 maxindex = build_int_2 (0, 0);
3835 value = 2;
3838 if (maxindex)
3840 TYPE_DOMAIN (type) = build_index_type (maxindex);
3841 if (!TREE_TYPE (maxindex))
3842 TREE_TYPE (maxindex) = TYPE_DOMAIN (type);
3845 /* Lay out the type now that we can get the real answer. */
3847 layout_type (type);
3849 return value;
3852 /* Given declspecs and a declarator,
3853 determine the name and type of the object declared
3854 and construct a ..._DECL node for it.
3855 (In one case we can return a ..._TYPE node instead.
3856 For invalid input we sometimes return 0.)
3858 DECLSPECS is a chain of tree_list nodes whose value fields
3859 are the storage classes and type specifiers.
3861 DECL_CONTEXT says which syntactic context this declaration is in:
3862 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3863 FUNCDEF for a function definition. Like NORMAL but a few different
3864 error messages in each case. Return value may be zero meaning
3865 this definition is too screwy to try to parse.
3866 PARM for a parameter declaration (either within a function prototype
3867 or before a function body). Make a PARM_DECL, or return void_type_node.
3868 TYPENAME if for a typename (in a cast or sizeof).
3869 Don't make a DECL node; just return the ..._TYPE node.
3870 FIELD for a struct or union field; make a FIELD_DECL.
3871 BITFIELD for a field with specified width.
3872 INITIALIZED is 1 if the decl has an initializer.
3874 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3875 It may also be so in the PARM case, for a prototype where the
3876 argument type is specified but not the name.
3878 This function is where the complicated C meanings of `static'
3879 and `extern' are interpreted. */
3881 static tree
3882 grokdeclarator (declarator, declspecs, decl_context, initialized)
3883 tree declspecs;
3884 tree declarator;
3885 enum decl_context decl_context;
3886 int initialized;
3888 int specbits = 0;
3889 tree spec;
3890 tree type = NULL_TREE;
3891 int longlong = 0;
3892 int constp;
3893 int restrictp;
3894 int volatilep;
3895 int type_quals = TYPE_UNQUALIFIED;
3896 int inlinep;
3897 int explicit_int = 0;
3898 int explicit_char = 0;
3899 int defaulted_int = 0;
3900 tree typedef_decl = 0;
3901 const char *name;
3902 tree typedef_type = 0;
3903 int funcdef_flag = 0;
3904 enum tree_code innermost_code = ERROR_MARK;
3905 int bitfield = 0;
3906 int size_varies = 0;
3907 tree decl_machine_attr = NULL_TREE;
3909 if (decl_context == BITFIELD)
3910 bitfield = 1, decl_context = FIELD;
3912 if (decl_context == FUNCDEF)
3913 funcdef_flag = 1, decl_context = NORMAL;
3915 push_obstacks_nochange ();
3917 if (flag_traditional && allocation_temporary_p ())
3918 end_temporary_allocation ();
3920 /* Look inside a declarator for the name being declared
3921 and get it as a string, for an error message. */
3923 register tree decl = declarator;
3924 name = 0;
3926 while (decl)
3927 switch (TREE_CODE (decl))
3929 case ARRAY_REF:
3930 case INDIRECT_REF:
3931 case CALL_EXPR:
3932 innermost_code = TREE_CODE (decl);
3933 decl = TREE_OPERAND (decl, 0);
3934 break;
3936 case IDENTIFIER_NODE:
3937 name = IDENTIFIER_POINTER (decl);
3938 decl = 0;
3939 break;
3941 default:
3942 abort ();
3944 if (name == 0)
3945 name = "type name";
3948 /* A function definition's declarator must have the form of
3949 a function declarator. */
3951 if (funcdef_flag && innermost_code != CALL_EXPR)
3952 return 0;
3954 /* Anything declared one level down from the top level
3955 must be one of the parameters of a function
3956 (because the body is at least two levels down). */
3958 /* If this looks like a function definition, make it one,
3959 even if it occurs where parms are expected.
3960 Then store_parm_decls will reject it and not use it as a parm. */
3961 if (decl_context == NORMAL && !funcdef_flag
3962 && current_binding_level->parm_flag)
3963 decl_context = PARM;
3965 /* Look through the decl specs and record which ones appear.
3966 Some typespecs are defined as built-in typenames.
3967 Others, the ones that are modifiers of other types,
3968 are represented by bits in SPECBITS: set the bits for
3969 the modifiers that appear. Storage class keywords are also in SPECBITS.
3971 If there is a typedef name or a type, store the type in TYPE.
3972 This includes builtin typedefs such as `int'.
3974 Set EXPLICIT_INT or EXPLICIT_CHAR if the type is `int' or `char'
3975 and did not come from a user typedef.
3977 Set LONGLONG if `long' is mentioned twice. */
3979 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
3981 register int i;
3982 register tree id = TREE_VALUE (spec);
3984 if (id == ridpointers[(int) RID_INT])
3985 explicit_int = 1;
3986 if (id == ridpointers[(int) RID_CHAR])
3987 explicit_char = 1;
3989 if (TREE_CODE (id) == IDENTIFIER_NODE)
3990 for (i = (int) RID_FIRST_MODIFIER; i < (int) RID_MAX; i++)
3992 if (ridpointers[i] == id)
3994 if (i == (int) RID_LONG && specbits & (1<<i))
3996 if (longlong)
3997 error ("`long long long' is too long for GCC");
3998 else
4000 if (pedantic && ! in_system_header && warn_long_long)
4001 pedwarn ("ANSI C does not support `long long'");
4002 longlong = 1;
4005 else if (specbits & (1 << i))
4006 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
4007 specbits |= 1 << i;
4008 goto found;
4011 if (type)
4012 error ("two or more data types in declaration of `%s'", name);
4013 /* Actual typedefs come to us as TYPE_DECL nodes. */
4014 else if (TREE_CODE (id) == TYPE_DECL)
4016 type = TREE_TYPE (id);
4017 decl_machine_attr = DECL_MACHINE_ATTRIBUTES (id);
4018 typedef_decl = id;
4020 /* Built-in types come as identifiers. */
4021 else if (TREE_CODE (id) == IDENTIFIER_NODE)
4023 register tree t = lookup_name (id);
4024 if (TREE_TYPE (t) == error_mark_node)
4026 else if (!t || TREE_CODE (t) != TYPE_DECL)
4027 error ("`%s' fails to be a typedef or built in type",
4028 IDENTIFIER_POINTER (id));
4029 else
4031 type = TREE_TYPE (t);
4032 typedef_decl = t;
4035 else if (TREE_CODE (id) != ERROR_MARK)
4036 type = id;
4038 found: {}
4041 typedef_type = type;
4042 if (type)
4043 size_varies = C_TYPE_VARIABLE_SIZE (type);
4045 /* No type at all: default to `int', and set DEFAULTED_INT
4046 because it was not a user-defined typedef. */
4048 if (type == 0)
4050 if ((! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
4051 | (1 << (int) RID_SIGNED)
4052 | (1 << (int) RID_UNSIGNED))))
4053 /* Don't warn about typedef foo = bar. */
4054 && ! (specbits & (1 << (int) RID_TYPEDEF) && initialized)
4055 && ! (in_system_header && ! allocation_temporary_p ()))
4057 /* Issue a warning if this is an ISO C 9x program or if -Wreturn-type
4058 and this is a function, or if -Wimplicit; prefer the former
4059 warning since it is more explicit. */
4060 if ((warn_implicit_int || warn_return_type) && funcdef_flag)
4061 warn_about_return_type = 1;
4062 else if (warn_implicit_int || flag_isoc9x)
4063 warning ("type defaults to `int' in declaration of `%s'", name);
4066 defaulted_int = 1;
4067 type = integer_type_node;
4070 /* Now process the modifiers that were specified
4071 and check for invalid combinations. */
4073 /* Long double is a special combination. */
4075 if ((specbits & 1 << (int) RID_LONG) && ! longlong
4076 && TYPE_MAIN_VARIANT (type) == double_type_node)
4078 specbits &= ~ (1 << (int) RID_LONG);
4079 type = long_double_type_node;
4082 /* Check all other uses of type modifiers. */
4084 if (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
4085 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED)))
4087 int ok = 0;
4089 if ((specbits & 1 << (int) RID_LONG)
4090 && (specbits & 1 << (int) RID_SHORT))
4091 error ("both long and short specified for `%s'", name);
4092 else if (((specbits & 1 << (int) RID_LONG)
4093 || (specbits & 1 << (int) RID_SHORT))
4094 && explicit_char)
4095 error ("long or short specified with char for `%s'", name);
4096 else if (((specbits & 1 << (int) RID_LONG)
4097 || (specbits & 1 << (int) RID_SHORT))
4098 && TREE_CODE (type) == REAL_TYPE)
4100 static int already = 0;
4102 error ("long or short specified with floating type for `%s'", name);
4103 if (! already && ! pedantic)
4105 error ("the only valid combination is `long double'");
4106 already = 1;
4109 else if ((specbits & 1 << (int) RID_SIGNED)
4110 && (specbits & 1 << (int) RID_UNSIGNED))
4111 error ("both signed and unsigned specified for `%s'", name);
4112 else if (TREE_CODE (type) != INTEGER_TYPE)
4113 error ("long, short, signed or unsigned invalid for `%s'", name);
4114 else
4116 ok = 1;
4117 if (!explicit_int && !defaulted_int && !explicit_char && pedantic)
4119 pedwarn ("long, short, signed or unsigned used invalidly for `%s'",
4120 name);
4121 if (flag_pedantic_errors)
4122 ok = 0;
4126 /* Discard the type modifiers if they are invalid. */
4127 if (! ok)
4129 specbits &= ~((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
4130 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED));
4131 longlong = 0;
4135 if ((specbits & (1 << (int) RID_COMPLEX))
4136 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
4138 error ("complex invalid for `%s'", name);
4139 specbits &= ~ (1 << (int) RID_COMPLEX);
4142 /* Decide whether an integer type is signed or not.
4143 Optionally treat bitfields as signed by default. */
4144 if (specbits & 1 << (int) RID_UNSIGNED
4145 /* Traditionally, all bitfields are unsigned. */
4146 || (bitfield && flag_traditional
4147 && (! explicit_flag_signed_bitfields || !flag_signed_bitfields))
4148 || (bitfield && ! flag_signed_bitfields
4149 && (explicit_int || defaulted_int || explicit_char
4150 /* A typedef for plain `int' without `signed'
4151 can be controlled just like plain `int'. */
4152 || ! (typedef_decl != 0
4153 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
4154 && TREE_CODE (type) != ENUMERAL_TYPE
4155 && !(specbits & 1 << (int) RID_SIGNED)))
4157 if (longlong)
4158 type = long_long_unsigned_type_node;
4159 else if (specbits & 1 << (int) RID_LONG)
4160 type = long_unsigned_type_node;
4161 else if (specbits & 1 << (int) RID_SHORT)
4162 type = short_unsigned_type_node;
4163 else if (type == char_type_node)
4164 type = unsigned_char_type_node;
4165 else if (typedef_decl)
4166 type = unsigned_type (type);
4167 else
4168 type = unsigned_type_node;
4170 else if ((specbits & 1 << (int) RID_SIGNED)
4171 && type == char_type_node)
4172 type = signed_char_type_node;
4173 else if (longlong)
4174 type = long_long_integer_type_node;
4175 else if (specbits & 1 << (int) RID_LONG)
4176 type = long_integer_type_node;
4177 else if (specbits & 1 << (int) RID_SHORT)
4178 type = short_integer_type_node;
4180 if (specbits & 1 << (int) RID_COMPLEX)
4182 /* If we just have "complex", it is equivalent to
4183 "complex double", but if any modifiers at all are specified it is
4184 the complex form of TYPE. E.g, "complex short" is
4185 "complex short int". */
4187 if (defaulted_int && ! longlong
4188 && ! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
4189 | (1 << (int) RID_SIGNED)
4190 | (1 << (int) RID_UNSIGNED))))
4191 type = complex_double_type_node;
4192 else if (type == integer_type_node)
4193 type = complex_integer_type_node;
4194 else if (type == float_type_node)
4195 type = complex_float_type_node;
4196 else if (type == double_type_node)
4197 type = complex_double_type_node;
4198 else if (type == long_double_type_node)
4199 type = complex_long_double_type_node;
4200 else
4201 type = build_complex_type (type);
4204 /* Figure out the type qualifiers for the declaration. There are
4205 two ways a declaration can become qualified. One is something
4206 like `const int i' where the `const' is explicit. Another is
4207 something like `typedef const int CI; CI i' where the type of the
4208 declaration contains the `const'. */
4209 constp = !! (specbits & 1 << (int) RID_CONST) + TYPE_READONLY (type);
4210 restrictp = !! (specbits & 1 << (int) RID_RESTRICT) + TYPE_RESTRICT (type);
4211 volatilep = !! (specbits & 1 << (int) RID_VOLATILE) + TYPE_VOLATILE (type);
4212 inlinep = !! (specbits & (1 << (int) RID_INLINE));
4213 if (constp > 1)
4214 pedwarn ("duplicate `const'");
4215 if (restrictp > 1)
4216 pedwarn ("duplicate `restrict'");
4217 if (volatilep > 1)
4218 pedwarn ("duplicate `volatile'");
4219 if (! flag_gen_aux_info && (TYPE_QUALS (type)))
4220 type = TYPE_MAIN_VARIANT (type);
4221 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4222 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4223 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4225 /* Warn if two storage classes are given. Default to `auto'. */
4228 int nclasses = 0;
4230 if (specbits & 1 << (int) RID_AUTO) nclasses++;
4231 if (specbits & 1 << (int) RID_STATIC) nclasses++;
4232 if (specbits & 1 << (int) RID_EXTERN) nclasses++;
4233 if (specbits & 1 << (int) RID_REGISTER) nclasses++;
4234 if (specbits & 1 << (int) RID_TYPEDEF) nclasses++;
4235 if (specbits & 1 << (int) RID_ITERATOR) nclasses++;
4237 /* Warn about storage classes that are invalid for certain
4238 kinds of declarations (parameters, typenames, etc.). */
4240 if (nclasses > 1)
4241 error ("multiple storage classes in declaration of `%s'", name);
4242 else if (funcdef_flag
4243 && (specbits
4244 & ((1 << (int) RID_REGISTER)
4245 | (1 << (int) RID_AUTO)
4246 | (1 << (int) RID_TYPEDEF))))
4248 if (specbits & 1 << (int) RID_AUTO
4249 && (pedantic || current_binding_level == global_binding_level))
4250 pedwarn ("function definition declared `auto'");
4251 if (specbits & 1 << (int) RID_REGISTER)
4252 error ("function definition declared `register'");
4253 if (specbits & 1 << (int) RID_TYPEDEF)
4254 error ("function definition declared `typedef'");
4255 specbits &= ~ ((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
4256 | (1 << (int) RID_AUTO));
4258 else if (decl_context != NORMAL && nclasses > 0)
4260 if (decl_context == PARM && specbits & 1 << (int) RID_REGISTER)
4262 else
4264 error ((decl_context == FIELD
4265 ? "storage class specified for structure field `%s'"
4266 : (decl_context == PARM
4267 ? "storage class specified for parameter `%s'"
4268 : "storage class specified for typename")),
4269 name);
4270 specbits &= ~ ((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
4271 | (1 << (int) RID_AUTO) | (1 << (int) RID_STATIC)
4272 | (1 << (int) RID_EXTERN));
4275 else if (specbits & 1 << (int) RID_EXTERN && initialized && ! funcdef_flag)
4277 /* `extern' with initialization is invalid if not at top level. */
4278 if (current_binding_level == global_binding_level)
4279 warning ("`%s' initialized and declared `extern'", name);
4280 else
4281 error ("`%s' has both `extern' and initializer", name);
4283 else if (specbits & 1 << (int) RID_EXTERN && funcdef_flag
4284 && current_binding_level != global_binding_level)
4285 error ("nested function `%s' declared `extern'", name);
4286 else if (current_binding_level == global_binding_level
4287 && specbits & (1 << (int) RID_AUTO))
4288 error ("top-level declaration of `%s' specifies `auto'", name);
4289 else if ((specbits & 1 << (int) RID_ITERATOR)
4290 && TREE_CODE (declarator) != IDENTIFIER_NODE)
4292 error ("iterator `%s' has derived type", name);
4293 type = error_mark_node;
4295 else if ((specbits & 1 << (int) RID_ITERATOR)
4296 && TREE_CODE (type) != INTEGER_TYPE)
4298 error ("iterator `%s' has noninteger type", name);
4299 type = error_mark_node;
4303 /* Now figure out the structure of the declarator proper.
4304 Descend through it, creating more complex types, until we reach
4305 the declared identifier (or NULL_TREE, in an absolute declarator). */
4307 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE)
4309 if (type == error_mark_node)
4311 declarator = TREE_OPERAND (declarator, 0);
4312 continue;
4315 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
4316 an INDIRECT_REF (for *...),
4317 a CALL_EXPR (for ...(...)),
4318 an identifier (for the name being declared)
4319 or a null pointer (for the place in an absolute declarator
4320 where the name was omitted).
4321 For the last two cases, we have just exited the loop.
4323 At this point, TYPE is the type of elements of an array,
4324 or for a function to return, or for a pointer to point to.
4325 After this sequence of ifs, TYPE is the type of the
4326 array or function or pointer, and DECLARATOR has had its
4327 outermost layer removed. */
4329 if (TREE_CODE (declarator) == ARRAY_REF)
4331 register tree itype = NULL_TREE;
4332 register tree size = TREE_OPERAND (declarator, 1);
4333 /* An uninitialized decl with `extern' is a reference. */
4334 int extern_ref = !initialized && (specbits & (1 << (int) RID_EXTERN));
4335 /* The index is a signed object `sizetype' bits wide. */
4336 tree index_type = signed_type (sizetype);
4338 declarator = TREE_OPERAND (declarator, 0);
4340 /* Check for some types that there cannot be arrays of. */
4342 if (TYPE_MAIN_VARIANT (type) == void_type_node)
4344 error ("declaration of `%s' as array of voids", name);
4345 type = error_mark_node;
4348 if (TREE_CODE (type) == FUNCTION_TYPE)
4350 error ("declaration of `%s' as array of functions", name);
4351 type = error_mark_node;
4354 if (size == error_mark_node)
4355 type = error_mark_node;
4357 if (type == error_mark_node)
4358 continue;
4360 /* If this is a block level extern, it must live past the end
4361 of the function so that we can check it against other extern
4362 declarations (IDENTIFIER_LIMBO_VALUE). */
4363 if (extern_ref && allocation_temporary_p ())
4364 end_temporary_allocation ();
4366 /* If size was specified, set ITYPE to a range-type for that size.
4367 Otherwise, ITYPE remains null. finish_decl may figure it out
4368 from an initial value. */
4370 if (size)
4372 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4373 STRIP_TYPE_NOPS (size);
4375 if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE
4376 && TREE_CODE (TREE_TYPE (size)) != ENUMERAL_TYPE)
4378 error ("size of array `%s' has non-integer type", name);
4379 size = integer_one_node;
4382 if (pedantic && integer_zerop (size))
4383 pedwarn ("ANSI C forbids zero-size array `%s'", name);
4385 if (TREE_CODE (size) == INTEGER_CST)
4387 constant_expression_warning (size);
4388 if (tree_int_cst_sgn (size) < 0)
4390 error ("size of array `%s' is negative", name);
4391 size = integer_one_node;
4394 else
4396 /* Make sure the array size remains visibly nonconstant
4397 even if it is (eg) a const variable with known value. */
4398 size_varies = 1;
4400 if (pedantic)
4402 if (TREE_CONSTANT (size))
4403 pedwarn ("ANSI C forbids array `%s' whose size can't be evaluated", name);
4404 else
4405 pedwarn ("ANSI C forbids variable-size array `%s'", name);
4409 /* Convert size to index_type, so that if it is a variable
4410 the computations will be done in the proper mode. */
4411 itype = fold (build (MINUS_EXPR, index_type,
4412 convert (index_type, size),
4413 convert (index_type, size_one_node)));
4415 /* If that overflowed, the array is too big.
4416 ??? While a size of INT_MAX+1 technically shouldn't cause
4417 an overflow (because we subtract 1), the overflow is recorded
4418 during the conversion to index_type, before the subtraction.
4419 Handling this case seems like an unnecessary complication. */
4420 if (TREE_OVERFLOW (itype))
4422 error ("size of array `%s' is too large", name);
4423 type = error_mark_node;
4424 continue;
4427 if (size_varies)
4428 itype = variable_size (itype);
4429 itype = build_index_type (itype);
4432 #if 0 /* This had bad results for pointers to arrays, as in
4433 union incomplete (*foo)[4]; */
4434 /* Complain about arrays of incomplete types, except in typedefs. */
4436 if (TYPE_SIZE (type) == 0
4437 /* Avoid multiple warnings for nested array types. */
4438 && TREE_CODE (type) != ARRAY_TYPE
4439 && !(specbits & (1 << (int) RID_TYPEDEF))
4440 && !C_TYPE_BEING_DEFINED (type))
4441 warning ("array type has incomplete element type");
4442 #endif
4444 #if 0 /* We shouldn't have a function type here at all!
4445 Functions aren't allowed as array elements. */
4446 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4447 && (constp || volatilep))
4448 pedwarn ("ANSI C forbids const or volatile function types");
4449 #endif
4451 /* Build the array type itself, then merge any constancy or
4452 volatility into the target type. We must do it in this order
4453 to ensure that the TYPE_MAIN_VARIANT field of the array type
4454 is set correctly. */
4456 type = build_array_type (type, itype);
4457 if (type_quals)
4458 type = c_build_qualified_type (type, type_quals);
4460 #if 0 /* don't clear these; leave them set so that the array type
4461 or the variable is itself const or volatile. */
4462 type_quals = TYPE_UNQUALIFIED;
4463 #endif
4465 if (size_varies)
4466 C_TYPE_VARIABLE_SIZE (type) = 1;
4468 else if (TREE_CODE (declarator) == CALL_EXPR)
4470 int extern_ref = (!(specbits & (1 << (int) RID_AUTO))
4471 || current_binding_level == global_binding_level);
4472 tree arg_types;
4474 /* Declaring a function type.
4475 Make sure we have a valid type for the function to return. */
4476 if (type == error_mark_node)
4477 continue;
4479 size_varies = 0;
4481 /* Warn about some types functions can't return. */
4483 if (TREE_CODE (type) == FUNCTION_TYPE)
4485 error ("`%s' declared as function returning a function", name);
4486 type = integer_type_node;
4488 if (TREE_CODE (type) == ARRAY_TYPE)
4490 error ("`%s' declared as function returning an array", name);
4491 type = integer_type_node;
4494 #ifndef TRADITIONAL_RETURN_FLOAT
4495 /* Traditionally, declaring return type float means double. */
4497 if (flag_traditional && TYPE_MAIN_VARIANT (type) == float_type_node)
4498 type = double_type_node;
4499 #endif /* TRADITIONAL_RETURN_FLOAT */
4501 /* If this is a block level extern, it must live past the end
4502 of the function so that we can check it against other extern
4503 declarations (IDENTIFIER_LIMBO_VALUE). */
4504 if (extern_ref && allocation_temporary_p ())
4505 end_temporary_allocation ();
4507 /* Construct the function type and go to the next
4508 inner layer of declarator. */
4510 arg_types = grokparms (TREE_OPERAND (declarator, 1),
4511 funcdef_flag
4512 /* Say it's a definition
4513 only for the CALL_EXPR
4514 closest to the identifier. */
4515 && TREE_CODE (TREE_OPERAND (declarator, 0)) == IDENTIFIER_NODE);
4516 #if 0 /* This seems to be false. We turn off temporary allocation
4517 above in this function if -traditional.
4518 And this code caused inconsistent results with prototypes:
4519 callers would ignore them, and pass arguments wrong. */
4521 /* Omit the arg types if -traditional, since the arg types
4522 and the list links might not be permanent. */
4523 type = build_function_type (type,
4524 flag_traditional
4525 ? NULL_TREE : arg_types);
4526 #endif
4527 /* Type qualifiers before the return type of the function
4528 qualify the return type, not the function type. */
4529 if (type_quals)
4530 type = c_build_qualified_type (type, type_quals);
4531 type_quals = TYPE_UNQUALIFIED;
4533 type = build_function_type (type, arg_types);
4534 declarator = TREE_OPERAND (declarator, 0);
4536 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4537 the formal parameter list of this FUNCTION_TYPE to point to
4538 the FUNCTION_TYPE node itself. */
4541 register tree link;
4543 for (link = last_function_parm_tags;
4544 link;
4545 link = TREE_CHAIN (link))
4546 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4549 else if (TREE_CODE (declarator) == INDIRECT_REF)
4551 /* Merge any constancy or volatility into the target type
4552 for the pointer. */
4554 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4555 && type_quals)
4556 pedwarn ("ANSI C forbids qualified function types");
4557 if (type_quals)
4558 type = c_build_qualified_type (type, type_quals);
4559 type_quals = TYPE_UNQUALIFIED;
4560 size_varies = 0;
4562 type = build_pointer_type (type);
4564 /* Process a list of type modifier keywords
4565 (such as const or volatile) that were given inside the `*'. */
4567 if (TREE_TYPE (declarator))
4569 register tree typemodlist;
4570 int erred = 0;
4572 constp = 0;
4573 volatilep = 0;
4574 restrictp = 0;
4575 for (typemodlist = TREE_TYPE (declarator); typemodlist;
4576 typemodlist = TREE_CHAIN (typemodlist))
4578 tree qualifier = TREE_VALUE (typemodlist);
4580 if (qualifier == ridpointers[(int) RID_CONST])
4581 constp++;
4582 else if (qualifier == ridpointers[(int) RID_VOLATILE])
4583 volatilep++;
4584 else if (qualifier == ridpointers[(int) RID_RESTRICT])
4585 restrictp++;
4586 else if (!erred)
4588 erred = 1;
4589 error ("invalid type modifier within pointer declarator");
4592 if (constp > 1)
4593 pedwarn ("duplicate `const'");
4594 if (volatilep > 1)
4595 pedwarn ("duplicate `volatile'");
4596 if (restrictp > 1)
4597 pedwarn ("duplicate `restrict'");
4599 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4600 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4601 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4604 declarator = TREE_OPERAND (declarator, 0);
4606 else
4607 abort ();
4611 /* Now TYPE has the actual type. */
4613 /* Did array size calculations overflow? */
4615 if (TREE_CODE (type) == ARRAY_TYPE
4616 && TYPE_SIZE (type)
4617 && TREE_OVERFLOW (TYPE_SIZE (type)))
4618 error ("size of array `%s' is too large", name);
4620 /* If this is declaring a typedef name, return a TYPE_DECL. */
4622 if (specbits & (1 << (int) RID_TYPEDEF))
4624 tree decl;
4625 /* Note that the grammar rejects storage classes
4626 in typenames, fields or parameters */
4627 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4628 && type_quals)
4629 pedwarn ("ANSI C forbids qualified function types");
4630 if (type_quals)
4631 type = c_build_qualified_type (type, type_quals);
4632 decl = build_decl (TYPE_DECL, declarator, type);
4633 if ((specbits & (1 << (int) RID_SIGNED))
4634 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
4635 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4636 pop_obstacks ();
4637 return decl;
4640 /* Detect the case of an array type of unspecified size
4641 which came, as such, direct from a typedef name.
4642 We must copy the type, so that each identifier gets
4643 a distinct type, so that each identifier's size can be
4644 controlled separately by its own initializer. */
4646 if (type != 0 && typedef_type != 0
4647 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type)
4648 && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0)
4650 type = build_array_type (TREE_TYPE (type), 0);
4651 if (size_varies)
4652 C_TYPE_VARIABLE_SIZE (type) = 1;
4655 /* If this is a type name (such as, in a cast or sizeof),
4656 compute the type and return it now. */
4658 if (decl_context == TYPENAME)
4660 /* Note that the grammar rejects storage classes
4661 in typenames, fields or parameters */
4662 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4663 && type_quals)
4664 pedwarn ("ANSI C forbids const or volatile function types");
4665 if (type_quals)
4666 type = c_build_qualified_type (type, type_quals);
4667 pop_obstacks ();
4668 return type;
4671 /* Aside from typedefs and type names (handle above),
4672 `void' at top level (not within pointer)
4673 is allowed only in public variables.
4674 We don't complain about parms either, but that is because
4675 a better error message can be made later. */
4677 if (TYPE_MAIN_VARIANT (type) == void_type_node && decl_context != PARM
4678 && ! ((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4679 && ((specbits & (1 << (int) RID_EXTERN))
4680 || (current_binding_level == global_binding_level
4681 && !(specbits
4682 & ((1 << (int) RID_STATIC) | (1 << (int) RID_REGISTER)))))))
4684 error ("variable or field `%s' declared void", name);
4685 type = integer_type_node;
4688 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4689 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4692 register tree decl;
4694 if (decl_context == PARM)
4696 tree type_as_written = type;
4697 tree promoted_type;
4699 /* A parameter declared as an array of T is really a pointer to T.
4700 One declared as a function is really a pointer to a function. */
4702 if (TREE_CODE (type) == ARRAY_TYPE)
4704 /* Transfer const-ness of array into that of type pointed to. */
4705 type = TREE_TYPE (type);
4706 if (type_quals)
4707 type = c_build_qualified_type (type, type_quals);
4708 type = build_pointer_type (type);
4709 type_quals = TYPE_UNQUALIFIED;
4710 size_varies = 0;
4712 else if (TREE_CODE (type) == FUNCTION_TYPE)
4714 if (pedantic && type_quals)
4715 pedwarn ("ANSI C forbids qualified function types");
4716 if (type_quals)
4717 type = c_build_qualified_type (type, type_quals);
4718 type = build_pointer_type (type);
4719 type_quals = TYPE_UNQUALIFIED;
4722 decl = build_decl (PARM_DECL, declarator, type);
4723 if (size_varies)
4724 C_DECL_VARIABLE_SIZE (decl) = 1;
4726 /* Compute the type actually passed in the parmlist,
4727 for the case where there is no prototype.
4728 (For example, shorts and chars are passed as ints.)
4729 When there is a prototype, this is overridden later. */
4731 if (type == error_mark_node)
4732 promoted_type = type;
4733 else
4735 promoted_type = simple_type_promotes_to (type);
4736 if (! promoted_type)
4737 promoted_type = type;
4740 DECL_ARG_TYPE (decl) = promoted_type;
4741 DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
4743 else if (decl_context == FIELD)
4745 /* Structure field. It may not be a function. */
4747 if (TREE_CODE (type) == FUNCTION_TYPE)
4749 error ("field `%s' declared as a function", name);
4750 type = build_pointer_type (type);
4752 else if (TREE_CODE (type) != ERROR_MARK && TYPE_SIZE (type) == 0)
4754 error ("field `%s' has incomplete type", name);
4755 type = error_mark_node;
4757 /* Move type qualifiers down to element of an array. */
4758 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4760 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4761 type_quals),
4762 TYPE_DOMAIN (type));
4763 #if 0 /* Leave the field const or volatile as well. */
4764 type_quals = TYPE_UNQUALIFIED;
4765 #endif
4767 decl = build_decl (FIELD_DECL, declarator, type);
4768 if (size_varies)
4769 C_DECL_VARIABLE_SIZE (decl) = 1;
4771 else if (TREE_CODE (type) == FUNCTION_TYPE)
4773 /* Every function declaration is "external"
4774 except for those which are inside a function body
4775 in which `auto' is used.
4776 That is a case not specified by ANSI C,
4777 and we use it for forward declarations for nested functions. */
4778 int extern_ref = (!(specbits & (1 << (int) RID_AUTO))
4779 || current_binding_level == global_binding_level);
4781 if (specbits & (1 << (int) RID_AUTO)
4782 && (pedantic || current_binding_level == global_binding_level))
4783 pedwarn ("invalid storage class for function `%s'", name);
4784 if (specbits & (1 << (int) RID_REGISTER))
4785 error ("invalid storage class for function `%s'", name);
4786 /* Function declaration not at top level.
4787 Storage classes other than `extern' are not allowed
4788 and `extern' makes no difference. */
4789 if (current_binding_level != global_binding_level
4790 && (specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_INLINE)))
4791 && pedantic)
4792 pedwarn ("invalid storage class for function `%s'", name);
4794 /* If this is a block level extern, it must live past the end
4795 of the function so that we can check it against other
4796 extern declarations (IDENTIFIER_LIMBO_VALUE). */
4797 if (extern_ref && allocation_temporary_p ())
4798 end_temporary_allocation ();
4800 decl = build_decl (FUNCTION_DECL, declarator, type);
4801 decl = build_decl_attribute_variant (decl, decl_machine_attr);
4803 if (pedantic && type_quals && ! DECL_IN_SYSTEM_HEADER (decl))
4804 pedwarn ("ANSI C forbids qualified function types");
4806 if (pedantic
4807 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl))) == void_type_node
4808 && TYPE_QUALS (TREE_TYPE (TREE_TYPE (decl)))
4809 && ! DECL_IN_SYSTEM_HEADER (decl))
4810 pedwarn ("ANSI C forbids qualified void function return type");
4812 /* GNU C interprets a `volatile void' return type to indicate
4813 that the function does not return. */
4814 if ((type_quals & TYPE_QUAL_VOLATILE)
4815 && TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
4816 warning ("`noreturn' function returns non-void value");
4818 if (extern_ref)
4819 DECL_EXTERNAL (decl) = 1;
4820 /* Record absence of global scope for `static' or `auto'. */
4821 TREE_PUBLIC (decl)
4822 = !(specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_AUTO)));
4824 /* Record presence of `inline', if it is reasonable. */
4825 if (inlinep)
4827 if (! strcmp (IDENTIFIER_POINTER (declarator), "main"))
4828 warning ("cannot inline function `main'");
4829 else
4830 /* Assume that otherwise the function can be inlined. */
4831 DECL_INLINE (decl) = 1;
4833 if (specbits & (1 << (int) RID_EXTERN))
4834 current_extern_inline = 1;
4837 else
4839 /* It's a variable. */
4840 /* An uninitialized decl with `extern' is a reference. */
4841 int extern_ref = !initialized && (specbits & (1 << (int) RID_EXTERN));
4843 /* Move type qualifiers down to element of an array. */
4844 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4846 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4847 type_quals),
4848 TYPE_DOMAIN (type));
4849 #if 0 /* Leave the variable const or volatile as well. */
4850 type_quals = TYPE_UNQUALIFIED;
4851 #endif
4854 /* If this is a block level extern, it must live past the end
4855 of the function so that we can check it against other
4856 extern declarations (IDENTIFIER_LIMBO_VALUE). */
4857 if (extern_ref && allocation_temporary_p ())
4858 end_temporary_allocation ();
4860 decl = build_decl (VAR_DECL, declarator, type);
4861 if (size_varies)
4862 C_DECL_VARIABLE_SIZE (decl) = 1;
4864 if (inlinep)
4865 pedwarn_with_decl (decl, "variable `%s' declared `inline'");
4867 DECL_EXTERNAL (decl) = extern_ref;
4868 /* At top level, the presence of a `static' or `register' storage
4869 class specifier, or the absence of all storage class specifiers
4870 makes this declaration a definition (perhaps tentative). Also,
4871 the absence of both `static' and `register' makes it public. */
4872 if (current_binding_level == global_binding_level)
4874 TREE_PUBLIC (decl)
4875 = !(specbits
4876 & ((1 << (int) RID_STATIC) | (1 << (int) RID_REGISTER)));
4877 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
4879 /* Not at top level, only `static' makes a static definition. */
4880 else
4882 TREE_STATIC (decl) = (specbits & (1 << (int) RID_STATIC)) != 0;
4883 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
4886 if (specbits & 1 << (int) RID_ITERATOR)
4887 ITERATOR_P (decl) = 1;
4890 /* Record `register' declaration for warnings on &
4891 and in case doing stupid register allocation. */
4893 if (specbits & (1 << (int) RID_REGISTER))
4894 DECL_REGISTER (decl) = 1;
4896 /* Record constancy and volatility. */
4897 c_apply_type_quals_to_decl (type_quals, decl);
4899 /* If a type has volatile components, it should be stored in memory.
4900 Otherwise, the fact that those components are volatile
4901 will be ignored, and would even crash the compiler. */
4902 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
4903 mark_addressable (decl);
4905 pop_obstacks ();
4907 return decl;
4911 /* Decode the parameter-list info for a function type or function definition.
4912 The argument is the value returned by `get_parm_info' (or made in parse.y
4913 if there is an identifier list instead of a parameter decl list).
4914 These two functions are separate because when a function returns
4915 or receives functions then each is called multiple times but the order
4916 of calls is different. The last call to `grokparms' is always the one
4917 that contains the formal parameter names of a function definition.
4919 Store in `last_function_parms' a chain of the decls of parms.
4920 Also store in `last_function_parm_tags' a chain of the struct, union,
4921 and enum tags declared among the parms.
4923 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4925 FUNCDEF_FLAG is nonzero for a function definition, 0 for
4926 a mere declaration. A nonempty identifier-list gets an error message
4927 when FUNCDEF_FLAG is zero. */
4929 static tree
4930 grokparms (parms_info, funcdef_flag)
4931 tree parms_info;
4932 int funcdef_flag;
4934 tree first_parm = TREE_CHAIN (parms_info);
4936 last_function_parms = TREE_PURPOSE (parms_info);
4937 last_function_parm_tags = TREE_VALUE (parms_info);
4939 if (warn_strict_prototypes && first_parm == 0 && !funcdef_flag
4940 && !in_system_header)
4941 warning ("function declaration isn't a prototype");
4943 if (first_parm != 0
4944 && TREE_CODE (TREE_VALUE (first_parm)) == IDENTIFIER_NODE)
4946 if (! funcdef_flag)
4947 pedwarn ("parameter names (without types) in function declaration");
4949 last_function_parms = first_parm;
4950 return 0;
4952 else
4954 tree parm;
4955 tree typelt;
4956 /* We no longer test FUNCDEF_FLAG.
4957 If the arg types are incomplete in a declaration,
4958 they must include undefined tags.
4959 These tags can never be defined in the scope of the declaration,
4960 so the types can never be completed,
4961 and no call can be compiled successfully. */
4962 #if 0
4963 /* In a fcn definition, arg types must be complete. */
4964 if (funcdef_flag)
4965 #endif
4966 for (parm = last_function_parms, typelt = first_parm;
4967 parm;
4968 parm = TREE_CHAIN (parm))
4969 /* Skip over any enumeration constants declared here. */
4970 if (TREE_CODE (parm) == PARM_DECL)
4972 /* Barf if the parameter itself has an incomplete type. */
4973 tree type = TREE_VALUE (typelt);
4974 if (TYPE_SIZE (type) == 0)
4976 if (funcdef_flag && DECL_NAME (parm) != 0)
4977 error ("parameter `%s' has incomplete type",
4978 IDENTIFIER_POINTER (DECL_NAME (parm)));
4979 else
4980 warning ("parameter has incomplete type");
4981 if (funcdef_flag)
4983 TREE_VALUE (typelt) = error_mark_node;
4984 TREE_TYPE (parm) = error_mark_node;
4987 #if 0 /* This has been replaced by parm_tags_warning
4988 which uses a more accurate criterion for what to warn about. */
4989 else
4991 /* Now warn if is a pointer to an incomplete type. */
4992 while (TREE_CODE (type) == POINTER_TYPE
4993 || TREE_CODE (type) == REFERENCE_TYPE)
4994 type = TREE_TYPE (type);
4995 type = TYPE_MAIN_VARIANT (type);
4996 if (TYPE_SIZE (type) == 0)
4998 if (DECL_NAME (parm) != 0)
4999 warning ("parameter `%s' points to incomplete type",
5000 IDENTIFIER_POINTER (DECL_NAME (parm)));
5001 else
5002 warning ("parameter points to incomplete type");
5005 #endif
5006 typelt = TREE_CHAIN (typelt);
5009 /* Allocate the list of types the way we allocate a type. */
5010 if (first_parm && ! TREE_PERMANENT (first_parm))
5012 /* Construct a copy of the list of types
5013 on the saveable obstack. */
5014 tree result = NULL;
5015 for (typelt = first_parm; typelt; typelt = TREE_CHAIN (typelt))
5016 result = saveable_tree_cons (NULL_TREE, TREE_VALUE (typelt),
5017 result);
5018 return nreverse (result);
5020 else
5021 /* The list we have is permanent already. */
5022 return first_parm;
5027 /* Return a tree_list node with info on a parameter list just parsed.
5028 The TREE_PURPOSE is a chain of decls of those parms.
5029 The TREE_VALUE is a list of structure, union and enum tags defined.
5030 The TREE_CHAIN is a list of argument types to go in the FUNCTION_TYPE.
5031 This tree_list node is later fed to `grokparms'.
5033 VOID_AT_END nonzero means append `void' to the end of the type-list.
5034 Zero means the parmlist ended with an ellipsis so don't append `void'. */
5036 tree
5037 get_parm_info (void_at_end)
5038 int void_at_end;
5040 register tree decl, t;
5041 register tree types = 0;
5042 int erred = 0;
5043 tree tags = gettags ();
5044 tree parms = getdecls ();
5045 tree new_parms = 0;
5046 tree order = current_binding_level->parm_order;
5048 /* Just `void' (and no ellipsis) is special. There are really no parms. */
5049 if (void_at_end && parms != 0
5050 && TREE_CHAIN (parms) == 0
5051 && TYPE_MAIN_VARIANT (TREE_TYPE (parms)) == void_type_node
5052 && DECL_NAME (parms) == 0)
5054 parms = NULL_TREE;
5055 storedecls (NULL_TREE);
5056 return saveable_tree_cons (NULL_TREE, NULL_TREE,
5057 saveable_tree_cons (NULL_TREE, void_type_node, NULL_TREE));
5060 /* Extract enumerator values and other non-parms declared with the parms.
5061 Likewise any forward parm decls that didn't have real parm decls. */
5062 for (decl = parms; decl; )
5064 tree next = TREE_CHAIN (decl);
5066 if (TREE_CODE (decl) != PARM_DECL)
5068 TREE_CHAIN (decl) = new_parms;
5069 new_parms = decl;
5071 else if (TREE_ASM_WRITTEN (decl))
5073 error_with_decl (decl, "parameter `%s' has just a forward declaration");
5074 TREE_CHAIN (decl) = new_parms;
5075 new_parms = decl;
5077 decl = next;
5080 /* Put the parm decls back in the order they were in in the parm list. */
5081 for (t = order; t; t = TREE_CHAIN (t))
5083 if (TREE_CHAIN (t))
5084 TREE_CHAIN (TREE_VALUE (t)) = TREE_VALUE (TREE_CHAIN (t));
5085 else
5086 TREE_CHAIN (TREE_VALUE (t)) = 0;
5089 new_parms = chainon (order ? nreverse (TREE_VALUE (order)) : 0,
5090 new_parms);
5092 /* Store the parmlist in the binding level since the old one
5093 is no longer a valid list. (We have changed the chain pointers.) */
5094 storedecls (new_parms);
5096 for (decl = new_parms; decl; decl = TREE_CHAIN (decl))
5097 /* There may also be declarations for enumerators if an enumeration
5098 type is declared among the parms. Ignore them here. */
5099 if (TREE_CODE (decl) == PARM_DECL)
5101 /* Since there is a prototype,
5102 args are passed in their declared types. */
5103 tree type = TREE_TYPE (decl);
5104 DECL_ARG_TYPE (decl) = type;
5105 if (PROMOTE_PROTOTYPES
5106 && (TREE_CODE (type) == INTEGER_TYPE
5107 || TREE_CODE (type) == ENUMERAL_TYPE)
5108 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
5109 DECL_ARG_TYPE (decl) = integer_type_node;
5111 types = saveable_tree_cons (NULL_TREE, TREE_TYPE (decl), types);
5112 if (TYPE_MAIN_VARIANT (TREE_VALUE (types)) == void_type_node && ! erred
5113 && DECL_NAME (decl) == 0)
5115 error ("`void' in parameter list must be the entire list");
5116 erred = 1;
5120 if (void_at_end)
5121 return saveable_tree_cons (new_parms, tags,
5122 nreverse (saveable_tree_cons (NULL_TREE, void_type_node, types)));
5124 return saveable_tree_cons (new_parms, tags, nreverse (types));
5127 /* At end of parameter list, warn about any struct, union or enum tags
5128 defined within. Do so because these types cannot ever become complete. */
5130 void
5131 parmlist_tags_warning ()
5133 tree elt;
5134 static int already;
5136 for (elt = current_binding_level->tags; elt; elt = TREE_CHAIN (elt))
5138 enum tree_code code = TREE_CODE (TREE_VALUE (elt));
5139 /* An anonymous union parm type is meaningful as a GNU extension.
5140 So don't warn for that. */
5141 if (code == UNION_TYPE && TREE_PURPOSE (elt) == 0 && !pedantic)
5142 continue;
5143 if (TREE_PURPOSE (elt) != 0)
5144 warning ("`%s %s' declared inside parameter list",
5145 (code == RECORD_TYPE ? "struct"
5146 : code == UNION_TYPE ? "union"
5147 : "enum"),
5148 IDENTIFIER_POINTER (TREE_PURPOSE (elt)));
5149 else
5150 warning ("anonymous %s declared inside parameter list",
5151 (code == RECORD_TYPE ? "struct"
5152 : code == UNION_TYPE ? "union"
5153 : "enum"));
5155 if (! already)
5157 warning ("its scope is only this definition or declaration, which is probably not what you want.");
5158 already = 1;
5163 /* Get the struct, enum or union (CODE says which) with tag NAME.
5164 Define the tag as a forward-reference if it is not defined. */
5166 tree
5167 xref_tag (code, name)
5168 enum tree_code code;
5169 tree name;
5171 int temporary = allocation_temporary_p ();
5173 /* If a cross reference is requested, look up the type
5174 already defined for this tag and return it. */
5176 register tree ref = lookup_tag (code, name, current_binding_level, 0);
5177 /* Even if this is the wrong type of tag, return what we found.
5178 There will be an error message anyway, from pending_xref_error.
5179 If we create an empty xref just for an invalid use of the type,
5180 the main result is to create lots of superfluous error messages. */
5181 if (ref)
5182 return ref;
5184 push_obstacks_nochange ();
5186 if (current_binding_level == global_binding_level && temporary)
5187 end_temporary_allocation ();
5189 /* If no such tag is yet defined, create a forward-reference node
5190 and record it as the "definition".
5191 When a real declaration of this type is found,
5192 the forward-reference will be altered into a real type. */
5194 ref = make_node (code);
5195 if (code == ENUMERAL_TYPE)
5197 /* (In ANSI, Enums can be referred to only if already defined.) */
5198 if (pedantic)
5199 pedwarn ("ANSI C forbids forward references to `enum' types");
5200 /* Give the type a default layout like unsigned int
5201 to avoid crashing if it does not get defined. */
5202 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
5203 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
5204 TREE_UNSIGNED (ref) = 1;
5205 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
5206 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
5207 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
5210 pushtag (name, ref);
5212 pop_obstacks ();
5214 return ref;
5217 /* Make sure that the tag NAME is defined *in the current binding level*
5218 at least as a forward reference.
5219 CODE says which kind of tag NAME ought to be.
5221 We also do a push_obstacks_nochange
5222 whose matching pop is in finish_struct. */
5224 tree
5225 start_struct (code, name)
5226 enum tree_code code;
5227 tree name;
5229 /* If there is already a tag defined at this binding level
5230 (as a forward reference), just return it. */
5232 register tree ref = 0;
5234 push_obstacks_nochange ();
5235 if (current_binding_level == global_binding_level)
5236 end_temporary_allocation ();
5238 if (name != 0)
5239 ref = lookup_tag (code, name, current_binding_level, 1);
5240 if (ref && TREE_CODE (ref) == code)
5242 C_TYPE_BEING_DEFINED (ref) = 1;
5243 TYPE_PACKED (ref) = flag_pack_struct;
5244 if (TYPE_FIELDS (ref))
5245 error ((code == UNION_TYPE ? "redefinition of `union %s'"
5246 : "redefinition of `struct %s'"),
5247 IDENTIFIER_POINTER (name));
5249 return ref;
5252 /* Otherwise create a forward-reference just so the tag is in scope. */
5254 ref = make_node (code);
5255 pushtag (name, ref);
5256 C_TYPE_BEING_DEFINED (ref) = 1;
5257 TYPE_PACKED (ref) = flag_pack_struct;
5258 return ref;
5261 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
5262 of a structure component, returning a FIELD_DECL node.
5263 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node.
5265 This is done during the parsing of the struct declaration.
5266 The FIELD_DECL nodes are chained together and the lot of them
5267 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
5269 tree
5270 grokfield (filename, line, declarator, declspecs, width)
5271 const char *filename ATTRIBUTE_UNUSED;
5272 int line ATTRIBUTE_UNUSED;
5273 tree declarator, declspecs, width;
5275 tree value;
5277 /* The corresponding pop_obstacks is in finish_decl. */
5278 push_obstacks_nochange ();
5280 value = grokdeclarator (declarator, declspecs, width ? BITFIELD : FIELD, 0);
5282 finish_decl (value, NULL_TREE, NULL_TREE);
5283 DECL_INITIAL (value) = width;
5285 maybe_objc_check_decl (value);
5286 return value;
5289 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5290 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5291 ATTRIBUTES are attributes to be applied to the structure.
5293 We also do a pop_obstacks to match the push in start_struct. */
5295 tree
5296 finish_struct (t, fieldlist, attributes)
5297 tree t;
5298 tree fieldlist;
5299 tree attributes;
5301 register tree x;
5302 int old_momentary;
5303 int toplevel = global_binding_level == current_binding_level;
5305 /* If this type was previously laid out as a forward reference,
5306 make sure we lay it out again. */
5308 TYPE_SIZE (t) = 0;
5310 decl_attributes (t, attributes, NULL_TREE);
5312 /* Nameless union parm types are useful as GCC extension. */
5313 if (! (TREE_CODE (t) == UNION_TYPE && TYPE_NAME (t) == 0) && !pedantic)
5314 /* Otherwise, warn about any struct or union def. in parmlist. */
5315 if (in_parm_level_p ())
5317 if (pedantic)
5318 pedwarn ((TREE_CODE (t) == UNION_TYPE ? "union defined inside parms"
5319 : "structure defined inside parms"));
5320 else if (! flag_traditional)
5321 warning ((TREE_CODE (t) == UNION_TYPE ? "union defined inside parms"
5322 : "structure defined inside parms"));
5325 old_momentary = suspend_momentary ();
5327 if (pedantic)
5329 for (x = fieldlist; x; x = TREE_CHAIN (x))
5330 if (DECL_NAME (x) != 0)
5331 break;
5333 if (x == 0)
5334 pedwarn ((fieldlist
5335 ? "%s has no named members"
5336 : "%s has no members"),
5337 TREE_CODE (t) == UNION_TYPE ? "union" : "struct");
5340 /* Install struct as DECL_CONTEXT of each field decl.
5341 Also process specified field sizes.
5342 Set DECL_FIELD_SIZE to the specified size, or 0 if none specified.
5343 The specified size is found in the DECL_INITIAL.
5344 Store 0 there, except for ": 0" fields (so we can find them
5345 and delete them, below). */
5347 for (x = fieldlist; x; x = TREE_CHAIN (x))
5349 DECL_CONTEXT (x) = t;
5350 DECL_PACKED (x) |= TYPE_PACKED (t);
5351 DECL_FIELD_SIZE (x) = 0;
5353 /* If any field is const, the structure type is pseudo-const. */
5354 if (TREE_READONLY (x))
5355 C_TYPE_FIELDS_READONLY (t) = 1;
5356 else
5358 /* A field that is pseudo-const makes the structure likewise. */
5359 tree t1 = TREE_TYPE (x);
5360 while (TREE_CODE (t1) == ARRAY_TYPE)
5361 t1 = TREE_TYPE (t1);
5362 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5363 && C_TYPE_FIELDS_READONLY (t1))
5364 C_TYPE_FIELDS_READONLY (t) = 1;
5367 /* Any field that is volatile means variables of this type must be
5368 treated in some ways as volatile. */
5369 if (TREE_THIS_VOLATILE (x))
5370 C_TYPE_FIELDS_VOLATILE (t) = 1;
5372 /* Any field of nominal variable size implies structure is too. */
5373 if (C_DECL_VARIABLE_SIZE (x))
5374 C_TYPE_VARIABLE_SIZE (t) = 1;
5376 /* Detect invalid nested redefinition. */
5377 if (TREE_TYPE (x) == t)
5378 error ("nested redefinition of `%s'",
5379 IDENTIFIER_POINTER (TYPE_NAME (t)));
5381 /* Detect invalid bit-field size. */
5382 if (DECL_INITIAL (x))
5383 STRIP_NOPS (DECL_INITIAL (x));
5384 if (DECL_INITIAL (x))
5386 if (TREE_CODE (DECL_INITIAL (x)) == INTEGER_CST)
5387 constant_expression_warning (DECL_INITIAL (x));
5388 else
5390 error_with_decl (x, "bit-field `%s' width not an integer constant");
5391 DECL_INITIAL (x) = NULL;
5395 /* Detect invalid bit-field type. */
5396 if (DECL_INITIAL (x)
5397 && TREE_CODE (TREE_TYPE (x)) != INTEGER_TYPE
5398 && TREE_CODE (TREE_TYPE (x)) != ENUMERAL_TYPE)
5400 error_with_decl (x, "bit-field `%s' has invalid type");
5401 DECL_INITIAL (x) = NULL;
5403 if (DECL_INITIAL (x) && pedantic
5404 && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != integer_type_node
5405 && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != unsigned_type_node
5406 /* Accept an enum that's equivalent to int or unsigned int. */
5407 && !(TREE_CODE (TREE_TYPE (x)) == ENUMERAL_TYPE
5408 && (TYPE_PRECISION (TREE_TYPE (x))
5409 == TYPE_PRECISION (integer_type_node))))
5410 pedwarn_with_decl (x, "bit-field `%s' type invalid in ANSI C");
5412 /* Detect and ignore out of range field width. */
5413 if (DECL_INITIAL (x))
5415 unsigned HOST_WIDE_INT width = TREE_INT_CST_LOW (DECL_INITIAL (x));
5417 if (tree_int_cst_sgn (DECL_INITIAL (x)) < 0)
5419 DECL_INITIAL (x) = NULL;
5420 error_with_decl (x, "negative width in bit-field `%s'");
5422 else if (TREE_INT_CST_HIGH (DECL_INITIAL (x)) != 0
5423 || width > TYPE_PRECISION (TREE_TYPE (x)))
5425 DECL_INITIAL (x) = NULL;
5426 pedwarn_with_decl (x, "width of `%s' exceeds its type");
5428 else if (width == 0 && DECL_NAME (x) != 0)
5430 error_with_decl (x, "zero width for bit-field `%s'");
5431 DECL_INITIAL (x) = NULL;
5435 /* Process valid field width. */
5436 if (DECL_INITIAL (x))
5438 register int width = TREE_INT_CST_LOW (DECL_INITIAL (x));
5440 if (TREE_CODE (TREE_TYPE (x)) == ENUMERAL_TYPE
5441 && (width < min_precision (TYPE_MIN_VALUE (TREE_TYPE (x)),
5442 TREE_UNSIGNED (TREE_TYPE (x)))
5443 || width < min_precision (TYPE_MAX_VALUE (TREE_TYPE (x)),
5444 TREE_UNSIGNED (TREE_TYPE (x)))))
5445 warning_with_decl (x, "`%s' is narrower than values of its type");
5447 DECL_FIELD_SIZE (x) = width;
5448 DECL_BIT_FIELD (x) = DECL_C_BIT_FIELD (x) = 1;
5449 DECL_INITIAL (x) = NULL;
5451 if (width == 0)
5453 /* field size 0 => force desired amount of alignment. */
5454 #ifdef EMPTY_FIELD_BOUNDARY
5455 DECL_ALIGN (x) = MAX (DECL_ALIGN (x), EMPTY_FIELD_BOUNDARY);
5456 #endif
5457 #ifdef PCC_BITFIELD_TYPE_MATTERS
5458 if (PCC_BITFIELD_TYPE_MATTERS)
5459 DECL_ALIGN (x) = MAX (DECL_ALIGN (x),
5460 TYPE_ALIGN (TREE_TYPE (x)));
5461 #endif
5464 else if (TREE_TYPE (x) != error_mark_node)
5466 unsigned int min_align = (DECL_PACKED (x) ? BITS_PER_UNIT
5467 : TYPE_ALIGN (TREE_TYPE (x)));
5468 /* Non-bit-fields are aligned for their type, except packed
5469 fields which require only BITS_PER_UNIT alignment. */
5470 DECL_ALIGN (x) = MAX (DECL_ALIGN (x), min_align);
5474 /* Now DECL_INITIAL is null on all members. */
5476 /* Delete all duplicate fields from the fieldlist */
5477 for (x = fieldlist; x && TREE_CHAIN (x);)
5478 /* Anonymous fields aren't duplicates. */
5479 if (DECL_NAME (TREE_CHAIN (x)) == 0)
5480 x = TREE_CHAIN (x);
5481 else
5483 register tree y = fieldlist;
5485 while (1)
5487 if (DECL_NAME (y) == DECL_NAME (TREE_CHAIN (x)))
5488 break;
5489 if (y == x)
5490 break;
5491 y = TREE_CHAIN (y);
5493 if (DECL_NAME (y) == DECL_NAME (TREE_CHAIN (x)))
5495 error_with_decl (TREE_CHAIN (x), "duplicate member `%s'");
5496 TREE_CHAIN (x) = TREE_CHAIN (TREE_CHAIN (x));
5498 else x = TREE_CHAIN (x);
5501 /* Now we have the nearly final fieldlist. Record it,
5502 then lay out the structure or union (including the fields). */
5504 TYPE_FIELDS (t) = fieldlist;
5506 layout_type (t);
5508 /* Delete all zero-width bit-fields from the fieldlist */
5510 tree *fieldlistp = &fieldlist;
5511 while (*fieldlistp)
5512 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp))
5513 *fieldlistp = TREE_CHAIN (*fieldlistp);
5514 else
5515 fieldlistp = &TREE_CHAIN (*fieldlistp);
5518 /* Now we have the truly final field list.
5519 Store it in this type and in the variants. */
5521 TYPE_FIELDS (t) = fieldlist;
5523 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5525 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5526 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5527 TYPE_ALIGN (x) = TYPE_ALIGN (t);
5530 /* If this was supposed to be a transparent union, but we can't
5531 make it one, warn and turn off the flag. */
5532 if (TREE_CODE (t) == UNION_TYPE
5533 && TYPE_TRANSPARENT_UNION (t)
5534 && TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t)))
5536 TYPE_TRANSPARENT_UNION (t) = 0;
5537 warning ("union cannot be made transparent");
5540 /* If this structure or union completes the type of any previous
5541 variable declaration, lay it out and output its rtl. */
5543 if (current_binding_level->n_incomplete != 0)
5545 tree decl;
5546 for (decl = current_binding_level->names; decl; decl = TREE_CHAIN (decl))
5548 if (TREE_TYPE (decl) == t
5549 && TREE_CODE (decl) != TYPE_DECL)
5551 layout_decl (decl, 0);
5552 /* This is a no-op in c-lang.c or something real in objc-actions.c. */
5553 maybe_objc_check_decl (decl);
5554 rest_of_decl_compilation (decl, NULL_PTR, toplevel, 0);
5555 if (! toplevel)
5556 expand_decl (decl);
5557 --current_binding_level->n_incomplete;
5559 else if (TYPE_SIZE (TREE_TYPE (decl)) == 0
5560 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5562 tree element = TREE_TYPE (decl);
5563 while (TREE_CODE (element) == ARRAY_TYPE)
5564 element = TREE_TYPE (element);
5565 if (element == t)
5566 layout_array_type (TREE_TYPE (decl));
5571 resume_momentary (old_momentary);
5573 /* Finish debugging output for this type. */
5574 rest_of_type_compilation (t, toplevel);
5576 /* The matching push is in start_struct. */
5577 pop_obstacks ();
5579 return t;
5582 /* Lay out the type T, and its element type, and so on. */
5584 static void
5585 layout_array_type (t)
5586 tree t;
5588 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5589 layout_array_type (TREE_TYPE (t));
5590 layout_type (t);
5593 /* Begin compiling the definition of an enumeration type.
5594 NAME is its name (or null if anonymous).
5595 Returns the type object, as yet incomplete.
5596 Also records info about it so that build_enumerator
5597 may be used to declare the individual values as they are read. */
5599 tree
5600 start_enum (name)
5601 tree name;
5603 register tree enumtype = 0;
5605 /* If this is the real definition for a previous forward reference,
5606 fill in the contents in the same object that used to be the
5607 forward reference. */
5609 if (name != 0)
5610 enumtype = lookup_tag (ENUMERAL_TYPE, name, current_binding_level, 1);
5612 /* The corresponding pop_obstacks is in finish_enum. */
5613 push_obstacks_nochange ();
5614 /* If these symbols and types are global, make them permanent. */
5615 if (current_binding_level == global_binding_level)
5616 end_temporary_allocation ();
5618 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5620 enumtype = make_node (ENUMERAL_TYPE);
5621 pushtag (name, enumtype);
5624 C_TYPE_BEING_DEFINED (enumtype) = 1;
5626 if (TYPE_VALUES (enumtype) != 0)
5628 /* This enum is a named one that has been declared already. */
5629 error ("redeclaration of `enum %s'", IDENTIFIER_POINTER (name));
5631 /* Completely replace its old definition.
5632 The old enumerators remain defined, however. */
5633 TYPE_VALUES (enumtype) = 0;
5636 enum_next_value = integer_zero_node;
5637 enum_overflow = 0;
5639 if (flag_short_enums)
5640 TYPE_PACKED (enumtype) = 1;
5642 return enumtype;
5645 /* After processing and defining all the values of an enumeration type,
5646 install their decls in the enumeration type and finish it off.
5647 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5648 and ATTRIBUTES are the specified attributes.
5649 Returns ENUMTYPE. */
5651 tree
5652 finish_enum (enumtype, values, attributes)
5653 tree enumtype;
5654 tree values;
5655 tree attributes;
5657 register tree pair, tem;
5658 tree minnode = 0, maxnode = 0;
5659 int lowprec, highprec, precision;
5660 int toplevel = global_binding_level == current_binding_level;
5662 if (in_parm_level_p ())
5663 warning ("enum defined inside parms");
5665 decl_attributes (enumtype, attributes, NULL_TREE);
5667 /* Calculate the maximum value of any enumerator in this type. */
5669 if (values == error_mark_node)
5670 minnode = maxnode = integer_zero_node;
5671 else
5672 for (pair = values; pair; pair = TREE_CHAIN (pair))
5674 tree value = TREE_VALUE (pair);
5675 if (pair == values)
5676 minnode = maxnode = TREE_VALUE (pair);
5677 else
5679 if (tree_int_cst_lt (maxnode, value))
5680 maxnode = value;
5681 if (tree_int_cst_lt (value, minnode))
5682 minnode = value;
5686 TYPE_MIN_VALUE (enumtype) = minnode;
5687 TYPE_MAX_VALUE (enumtype) = maxnode;
5689 /* An enum can have some negative values; then it is signed. */
5690 TREE_UNSIGNED (enumtype) = tree_int_cst_sgn (minnode) >= 0;
5692 /* Determine the precision this type needs. */
5694 lowprec = min_precision (minnode, TREE_UNSIGNED (enumtype));
5695 highprec = min_precision (maxnode, TREE_UNSIGNED (enumtype));
5696 precision = MAX (lowprec, highprec);
5698 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5700 tree narrowest = type_for_size (precision, 1);
5701 if (narrowest == 0)
5703 warning ("enumeration values exceed range of largest integer");
5704 narrowest = long_long_integer_type_node;
5707 TYPE_PRECISION (enumtype) = TYPE_PRECISION (narrowest);
5709 else
5710 TYPE_PRECISION (enumtype) = TYPE_PRECISION (integer_type_node);
5712 TYPE_SIZE (enumtype) = 0;
5713 layout_type (enumtype);
5715 if (values != error_mark_node)
5717 /* Change the type of the enumerators to be the enum type.
5718 Formerly this was done only for enums that fit in an int,
5719 but the comment said it was done only for enums wider than int.
5720 It seems necessary to do this for wide enums,
5721 and best not to change what's done for ordinary narrower ones. */
5722 for (pair = values; pair; pair = TREE_CHAIN (pair))
5724 TREE_TYPE (TREE_PURPOSE (pair)) = enumtype;
5725 DECL_SIZE (TREE_PURPOSE (pair)) = TYPE_SIZE (enumtype);
5726 if (TREE_CODE (TREE_PURPOSE (pair)) != FUNCTION_DECL)
5727 DECL_ALIGN (TREE_PURPOSE (pair)) = TYPE_ALIGN (enumtype);
5730 /* Replace the decl nodes in VALUES with their names. */
5731 for (pair = values; pair; pair = TREE_CHAIN (pair))
5732 TREE_PURPOSE (pair) = DECL_NAME (TREE_PURPOSE (pair));
5734 TYPE_VALUES (enumtype) = values;
5737 /* Fix up all variant types of this enum type. */
5738 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5740 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5741 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5742 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5743 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5744 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5745 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5746 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5747 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5748 TREE_UNSIGNED (tem) = TREE_UNSIGNED (enumtype);
5751 /* Finish debugging output for this type. */
5752 rest_of_type_compilation (enumtype, toplevel);
5754 /* This matches a push in start_enum. */
5755 pop_obstacks ();
5757 return enumtype;
5760 /* Build and install a CONST_DECL for one value of the
5761 current enumeration type (one that was begun with start_enum).
5762 Return a tree-list containing the CONST_DECL and its value.
5763 Assignment of sequential values by default is handled here. */
5765 tree
5766 build_enumerator (name, value)
5767 tree name, value;
5769 register tree decl, type;
5771 /* Validate and default VALUE. */
5773 /* Remove no-op casts from the value. */
5774 if (value)
5775 STRIP_TYPE_NOPS (value);
5777 if (value != 0)
5779 if (TREE_CODE (value) == INTEGER_CST)
5781 value = default_conversion (value);
5782 constant_expression_warning (value);
5784 else
5786 error ("enumerator value for `%s' not integer constant",
5787 IDENTIFIER_POINTER (name));
5788 value = 0;
5792 /* Default based on previous value. */
5793 /* It should no longer be possible to have NON_LVALUE_EXPR
5794 in the default. */
5795 if (value == 0)
5797 value = enum_next_value;
5798 if (enum_overflow)
5799 error ("overflow in enumeration values");
5802 if (pedantic && ! int_fits_type_p (value, integer_type_node))
5804 pedwarn ("ANSI C restricts enumerator values to range of `int'");
5805 value = integer_zero_node;
5808 /* Set basis for default for next value. */
5809 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
5810 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5812 /* Now create a declaration for the enum value name. */
5814 type = TREE_TYPE (value);
5815 type = type_for_size (MAX (TYPE_PRECISION (type),
5816 TYPE_PRECISION (integer_type_node)),
5817 ((flag_traditional
5818 || TYPE_PRECISION (type) >= TYPE_PRECISION (integer_type_node))
5819 && TREE_UNSIGNED (type)));
5821 decl = build_decl (CONST_DECL, name, type);
5822 DECL_INITIAL (decl) = value;
5823 TREE_TYPE (value) = type;
5824 pushdecl (decl);
5826 return saveable_tree_cons (decl, value, NULL_TREE);
5829 /* Create the FUNCTION_DECL for a function definition.
5830 DECLSPECS, DECLARATOR, PREFIX_ATTRIBUTES and ATTRIBUTES are the parts of
5831 the declaration; they describe the function's name and the type it returns,
5832 but twisted together in a fashion that parallels the syntax of C.
5834 This function creates a binding context for the function body
5835 as well as setting up the FUNCTION_DECL in current_function_decl.
5837 Returns 1 on success. If the DECLARATOR is not suitable for a function
5838 (it defines a datum instead), we return 0, which tells
5839 yyparse to report a parse error.
5841 NESTED is nonzero for a function nested within another function. */
5844 start_function (declspecs, declarator, prefix_attributes, attributes, nested)
5845 tree declarator, declspecs, prefix_attributes, attributes;
5846 int nested;
5848 tree decl1, old_decl;
5849 tree restype;
5850 int old_immediate_size_expand = immediate_size_expand;
5852 current_function_returns_value = 0; /* Assume, until we see it does. */
5853 current_function_returns_null = 0;
5854 warn_about_return_type = 0;
5855 current_extern_inline = 0;
5856 c_function_varargs = 0;
5857 named_labels = 0;
5858 shadowed_labels = 0;
5860 /* Don't expand any sizes in the return type of the function. */
5861 immediate_size_expand = 0;
5863 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1);
5865 /* If the declarator is not suitable for a function definition,
5866 cause a syntax error. */
5867 if (decl1 == 0)
5869 immediate_size_expand = old_immediate_size_expand;
5870 return 0;
5873 decl_attributes (decl1, prefix_attributes, attributes);
5875 announce_function (decl1);
5877 if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl1))) == 0)
5879 error ("return-type is an incomplete type");
5880 /* Make it return void instead. */
5881 TREE_TYPE (decl1)
5882 = build_function_type (void_type_node,
5883 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
5886 if (warn_about_return_type)
5887 warning ("return-type defaults to `int'");
5889 /* Save the parm names or decls from this function's declarator
5890 where store_parm_decls will find them. */
5891 current_function_parms = last_function_parms;
5892 current_function_parm_tags = last_function_parm_tags;
5894 /* Make the init_value nonzero so pushdecl knows this is not tentative.
5895 error_mark_node is replaced below (in poplevel) with the BLOCK. */
5896 DECL_INITIAL (decl1) = error_mark_node;
5898 /* If this definition isn't a prototype and we had a prototype declaration
5899 before, copy the arg type info from that prototype.
5900 But not if what we had before was a builtin function. */
5901 old_decl = lookup_name_current_level (DECL_NAME (decl1));
5902 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
5903 && !DECL_BUILT_IN (old_decl)
5904 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5905 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (old_decl))))
5906 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
5908 TREE_TYPE (decl1) = TREE_TYPE (old_decl);
5909 current_function_prototype_file = DECL_SOURCE_FILE (old_decl);
5910 current_function_prototype_line = DECL_SOURCE_LINE (old_decl);
5913 /* If there is no explicit declaration, look for any out-of-scope implicit
5914 declarations. */
5915 if (old_decl == 0)
5916 old_decl = IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1));
5918 /* Optionally warn of old-fashioned def with no previous prototype. */
5919 if (warn_strict_prototypes
5920 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
5921 && !(old_decl != 0 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) != 0))
5922 warning ("function declaration isn't a prototype");
5923 /* Optionally warn of any global def with no previous prototype. */
5924 else if (warn_missing_prototypes
5925 && TREE_PUBLIC (decl1)
5926 && !(old_decl != 0 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) != 0)
5927 && strcmp ("main", IDENTIFIER_POINTER (DECL_NAME (decl1))))
5928 warning_with_decl (decl1, "no previous prototype for `%s'");
5929 /* Optionally warn of any def with no previous prototype
5930 if the function has already been used. */
5931 else if (warn_missing_prototypes
5932 && old_decl != 0 && TREE_USED (old_decl)
5933 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
5934 warning_with_decl (decl1,
5935 "`%s' was used with no prototype before its definition");
5936 /* Optionally warn of any global def with no previous declaration. */
5937 else if (warn_missing_declarations
5938 && TREE_PUBLIC (decl1)
5939 && old_decl == 0
5940 && strcmp ("main", IDENTIFIER_POINTER (DECL_NAME (decl1))))
5941 warning_with_decl (decl1, "no previous declaration for `%s'");
5942 /* Optionally warn of any def with no previous declaration
5943 if the function has already been used. */
5944 else if (warn_missing_declarations
5945 && old_decl != 0 && TREE_USED (old_decl)
5946 && old_decl == IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)))
5947 warning_with_decl (decl1,
5948 "`%s' was used with no declaration before its definition");
5950 /* This is a definition, not a reference.
5951 So normally clear DECL_EXTERNAL.
5952 However, `extern inline' acts like a declaration
5953 except for defining how to inline. So set DECL_EXTERNAL in that case. */
5954 DECL_EXTERNAL (decl1) = current_extern_inline;
5956 #ifdef SET_DEFAULT_DECL_ATTRIBUTES
5957 SET_DEFAULT_DECL_ATTRIBUTES (decl1, attributes);
5958 #endif
5960 /* This function exists in static storage.
5961 (This does not mean `static' in the C sense!) */
5962 TREE_STATIC (decl1) = 1;
5964 /* A nested function is not global. */
5965 if (current_function_decl != 0)
5966 TREE_PUBLIC (decl1) = 0;
5968 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
5969 if (warn_main > 0
5970 && strcmp ("main", IDENTIFIER_POINTER (DECL_NAME (decl1))) == 0)
5972 tree args;
5973 int argct = 0;
5975 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5976 != integer_type_node)
5977 pedwarn_with_decl (decl1, "return type of `%s' is not `int'");
5979 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
5980 args = TREE_CHAIN (args))
5982 tree type = args ? TREE_VALUE (args) : 0;
5984 if (type == void_type_node)
5985 break;
5987 ++argct;
5988 switch (argct)
5990 case 1:
5991 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
5992 pedwarn_with_decl (decl1,
5993 "first argument of `%s' should be `int'");
5994 break;
5996 case 2:
5997 if (TREE_CODE (type) != POINTER_TYPE
5998 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5999 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
6000 != char_type_node))
6001 pedwarn_with_decl (decl1,
6002 "second argument of `%s' should be `char **'");
6003 break;
6005 case 3:
6006 if (TREE_CODE (type) != POINTER_TYPE
6007 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
6008 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
6009 != char_type_node))
6010 pedwarn_with_decl (decl1,
6011 "third argument of `%s' should probably be `char **'");
6012 break;
6016 /* It is intentional that this message does not mention the third
6017 argument, which is warned for only pedantically, because it's
6018 blessed by mention in an appendix of the standard. */
6019 if (argct > 0 && (argct < 2 || argct > 3))
6020 pedwarn_with_decl (decl1, "`%s' takes only zero or two arguments");
6022 if (argct == 3 && pedantic)
6023 pedwarn_with_decl (decl1, "third argument of `%s' is deprecated");
6025 if (! TREE_PUBLIC (decl1))
6026 pedwarn_with_decl (decl1, "`%s' is normally a non-static function");
6029 /* Record the decl so that the function name is defined.
6030 If we already have a decl for this name, and it is a FUNCTION_DECL,
6031 use the old decl. */
6033 current_function_decl = pushdecl (decl1);
6035 pushlevel (0);
6036 declare_parm_level (1);
6037 current_binding_level->subblocks_tag_transparent = 1;
6039 make_function_rtl (current_function_decl);
6041 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
6042 /* Promote the value to int before returning it. */
6043 if (C_PROMOTING_INTEGER_TYPE_P (restype))
6045 /* It retains unsignedness if traditional
6046 or if not really getting wider. */
6047 if (TREE_UNSIGNED (restype)
6048 && (flag_traditional
6049 || (TYPE_PRECISION (restype)
6050 == TYPE_PRECISION (integer_type_node))))
6051 restype = unsigned_type_node;
6052 else
6053 restype = integer_type_node;
6055 DECL_RESULT (current_function_decl)
6056 = build_decl (RESULT_DECL, NULL_TREE, restype);
6058 if (!nested)
6059 /* Allocate further tree nodes temporarily during compilation
6060 of this function only. */
6061 temporary_allocation ();
6063 /* If this fcn was already referenced via a block-scope `extern' decl
6064 (or an implicit decl), propagate certain information about the usage. */
6065 if (TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (current_function_decl)))
6066 TREE_ADDRESSABLE (current_function_decl) = 1;
6068 immediate_size_expand = old_immediate_size_expand;
6070 return 1;
6073 /* Record that this function is going to be a varargs function.
6074 This is called before store_parm_decls, which is too early
6075 to call mark_varargs directly. */
6077 void
6078 c_mark_varargs ()
6080 c_function_varargs = 1;
6083 /* Store the parameter declarations into the current function declaration.
6084 This is called after parsing the parameter declarations, before
6085 digesting the body of the function.
6087 For an old-style definition, modify the function's type
6088 to specify at least the number of arguments. */
6090 void
6091 store_parm_decls ()
6093 register tree fndecl = current_function_decl;
6094 register tree parm;
6096 /* This is either a chain of PARM_DECLs (if a prototype was used)
6097 or a list of IDENTIFIER_NODEs (for an old-fashioned C definition). */
6098 tree specparms = current_function_parms;
6100 /* This is a list of types declared among parms in a prototype. */
6101 tree parmtags = current_function_parm_tags;
6103 /* This is a chain of PARM_DECLs from old-style parm declarations. */
6104 register tree parmdecls = getdecls ();
6106 /* This is a chain of any other decls that came in among the parm
6107 declarations. If a parm is declared with enum {foo, bar} x;
6108 then CONST_DECLs for foo and bar are put here. */
6109 tree nonparms = 0;
6111 /* Nonzero if this definition is written with a prototype. */
6112 int prototype = 0;
6114 if (specparms != 0 && TREE_CODE (specparms) != TREE_LIST)
6116 /* This case is when the function was defined with an ANSI prototype.
6117 The parms already have decls, so we need not do anything here
6118 except record them as in effect
6119 and complain if any redundant old-style parm decls were written. */
6121 register tree next;
6122 tree others = 0;
6124 prototype = 1;
6126 if (parmdecls != 0)
6128 tree decl, link;
6130 error_with_decl (fndecl,
6131 "parm types given both in parmlist and separately");
6132 /* Get rid of the erroneous decls; don't keep them on
6133 the list of parms, since they might not be PARM_DECLs. */
6134 for (decl = current_binding_level->names;
6135 decl; decl = TREE_CHAIN (decl))
6136 if (DECL_NAME (decl))
6137 IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) = 0;
6138 for (link = current_binding_level->shadowed;
6139 link; link = TREE_CHAIN (link))
6140 IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
6141 current_binding_level->names = 0;
6142 current_binding_level->shadowed = 0;
6145 specparms = nreverse (specparms);
6146 for (parm = specparms; parm; parm = next)
6148 next = TREE_CHAIN (parm);
6149 if (TREE_CODE (parm) == PARM_DECL)
6151 if (DECL_NAME (parm) == 0)
6152 error_with_decl (parm, "parameter name omitted");
6153 else if (TYPE_MAIN_VARIANT (TREE_TYPE (parm)) == void_type_node)
6155 error_with_decl (parm, "parameter `%s' declared void");
6156 /* Change the type to error_mark_node so this parameter
6157 will be ignored by assign_parms. */
6158 TREE_TYPE (parm) = error_mark_node;
6160 pushdecl (parm);
6162 else
6164 /* If we find an enum constant or a type tag,
6165 put it aside for the moment. */
6166 TREE_CHAIN (parm) = 0;
6167 others = chainon (others, parm);
6171 /* Get the decls in their original chain order
6172 and record in the function. */
6173 DECL_ARGUMENTS (fndecl) = getdecls ();
6175 #if 0
6176 /* If this function takes a variable number of arguments,
6177 add a phony parameter to the end of the parm list,
6178 to represent the position of the first unnamed argument. */
6179 if (TREE_VALUE (tree_last (TYPE_ARG_TYPES (TREE_TYPE (fndecl))))
6180 != void_type_node)
6182 tree dummy = build_decl (PARM_DECL, NULL_TREE, void_type_node);
6183 /* Let's hope the address of the unnamed parm
6184 won't depend on its type. */
6185 TREE_TYPE (dummy) = integer_type_node;
6186 DECL_ARG_TYPE (dummy) = integer_type_node;
6187 DECL_ARGUMENTS (fndecl)
6188 = chainon (DECL_ARGUMENTS (fndecl), dummy);
6190 #endif
6192 /* Now pushdecl the enum constants. */
6193 for (parm = others; parm; parm = next)
6195 next = TREE_CHAIN (parm);
6196 if (DECL_NAME (parm) == 0)
6198 else if (TYPE_MAIN_VARIANT (TREE_TYPE (parm)) == void_type_node)
6200 else if (TREE_CODE (parm) != PARM_DECL)
6201 pushdecl (parm);
6204 storetags (chainon (parmtags, gettags ()));
6206 else
6208 /* SPECPARMS is an identifier list--a chain of TREE_LIST nodes
6209 each with a parm name as the TREE_VALUE.
6211 PARMDECLS is a chain of declarations for parameters.
6212 Warning! It can also contain CONST_DECLs which are not parameters
6213 but are names of enumerators of any enum types
6214 declared among the parameters.
6216 First match each formal parameter name with its declaration.
6217 Associate decls with the names and store the decls
6218 into the TREE_PURPOSE slots. */
6220 for (parm = parmdecls; parm; parm = TREE_CHAIN (parm))
6221 DECL_RESULT (parm) = 0;
6223 for (parm = specparms; parm; parm = TREE_CHAIN (parm))
6225 register tree tail, found = NULL;
6227 if (TREE_VALUE (parm) == 0)
6229 error_with_decl (fndecl, "parameter name missing from parameter list");
6230 TREE_PURPOSE (parm) = 0;
6231 continue;
6234 /* See if any of the parmdecls specifies this parm by name.
6235 Ignore any enumerator decls. */
6236 for (tail = parmdecls; tail; tail = TREE_CHAIN (tail))
6237 if (DECL_NAME (tail) == TREE_VALUE (parm)
6238 && TREE_CODE (tail) == PARM_DECL)
6240 found = tail;
6241 break;
6244 /* If declaration already marked, we have a duplicate name.
6245 Complain, and don't use this decl twice. */
6246 if (found && DECL_RESULT (found) != 0)
6248 error_with_decl (found, "multiple parameters named `%s'");
6249 found = 0;
6252 /* If the declaration says "void", complain and ignore it. */
6253 if (found && TYPE_MAIN_VARIANT (TREE_TYPE (found)) == void_type_node)
6255 error_with_decl (found, "parameter `%s' declared void");
6256 TREE_TYPE (found) = integer_type_node;
6257 DECL_ARG_TYPE (found) = integer_type_node;
6258 layout_decl (found, 0);
6261 /* Traditionally, a parm declared float is actually a double. */
6262 if (found && flag_traditional
6263 && TYPE_MAIN_VARIANT (TREE_TYPE (found)) == float_type_node)
6265 TREE_TYPE (found) = double_type_node;
6266 DECL_ARG_TYPE (found) = double_type_node;
6267 layout_decl (found, 0);
6270 /* If no declaration found, default to int. */
6271 if (!found)
6273 found = build_decl (PARM_DECL, TREE_VALUE (parm),
6274 integer_type_node);
6275 DECL_ARG_TYPE (found) = TREE_TYPE (found);
6276 DECL_SOURCE_LINE (found) = DECL_SOURCE_LINE (fndecl);
6277 DECL_SOURCE_FILE (found) = DECL_SOURCE_FILE (fndecl);
6278 if (extra_warnings)
6279 warning_with_decl (found, "type of `%s' defaults to `int'");
6280 pushdecl (found);
6283 TREE_PURPOSE (parm) = found;
6285 /* Mark this decl as "already found" -- see test, above.
6286 It is safe to use DECL_RESULT for this
6287 since it is not used in PARM_DECLs or CONST_DECLs. */
6288 DECL_RESULT (found) = error_mark_node;
6291 /* Put anything which is on the parmdecls chain and which is
6292 not a PARM_DECL onto the list NONPARMS. (The types of
6293 non-parm things which might appear on the list include
6294 enumerators and NULL-named TYPE_DECL nodes.) Complain about
6295 any actual PARM_DECLs not matched with any names. */
6297 nonparms = 0;
6298 for (parm = parmdecls; parm; )
6300 tree next = TREE_CHAIN (parm);
6301 TREE_CHAIN (parm) = 0;
6303 if (TREE_CODE (parm) != PARM_DECL)
6304 nonparms = chainon (nonparms, parm);
6305 else
6307 /* Complain about args with incomplete types. */
6308 if (TYPE_SIZE (TREE_TYPE (parm)) == 0)
6310 error_with_decl (parm, "parameter `%s' has incomplete type");
6311 TREE_TYPE (parm) = error_mark_node;
6314 if (DECL_RESULT (parm) == 0)
6316 error_with_decl (parm,
6317 "declaration for parameter `%s' but no such parameter");
6318 /* Pretend the parameter was not missing.
6319 This gets us to a standard state and minimizes
6320 further error messages. */
6321 specparms
6322 = chainon (specparms,
6323 tree_cons (parm, NULL_TREE, NULL_TREE));
6327 parm = next;
6330 /* Chain the declarations together in the order of the list of names. */
6331 /* Store that chain in the function decl, replacing the list of names. */
6332 parm = specparms;
6333 DECL_ARGUMENTS (fndecl) = 0;
6335 register tree last;
6336 for (last = 0; parm; parm = TREE_CHAIN (parm))
6337 if (TREE_PURPOSE (parm))
6339 if (last == 0)
6340 DECL_ARGUMENTS (fndecl) = TREE_PURPOSE (parm);
6341 else
6342 TREE_CHAIN (last) = TREE_PURPOSE (parm);
6343 last = TREE_PURPOSE (parm);
6344 TREE_CHAIN (last) = 0;
6348 /* If there was a previous prototype,
6349 set the DECL_ARG_TYPE of each argument according to
6350 the type previously specified, and report any mismatches. */
6352 if (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
6354 register tree type;
6355 for (parm = DECL_ARGUMENTS (fndecl),
6356 type = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6357 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
6358 != void_type_node));
6359 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6361 if (parm == 0 || type == 0
6362 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6364 error ("number of arguments doesn't match prototype");
6365 error_with_file_and_line (current_function_prototype_file,
6366 current_function_prototype_line,
6367 "prototype declaration");
6368 break;
6370 /* Type for passing arg must be consistent
6371 with that declared for the arg. */
6372 if (! comptypes (DECL_ARG_TYPE (parm), TREE_VALUE (type)))
6374 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6375 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6377 /* Adjust argument to match prototype. E.g. a previous
6378 `int foo(float);' prototype causes
6379 `int foo(x) float x; {...}' to be treated like
6380 `int foo(float x) {...}'. This is particularly
6381 useful for argument types like uid_t. */
6382 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6384 if (PROMOTE_PROTOTYPES
6385 && (TREE_CODE (TREE_TYPE (parm)) == INTEGER_TYPE
6386 || TREE_CODE (TREE_TYPE (parm)) == ENUMERAL_TYPE)
6387 && TYPE_PRECISION (TREE_TYPE (parm))
6388 < TYPE_PRECISION (integer_type_node))
6389 DECL_ARG_TYPE (parm) = integer_type_node;
6391 if (pedantic)
6393 pedwarn ("promoted argument `%s' doesn't match prototype",
6394 IDENTIFIER_POINTER (DECL_NAME (parm)));
6395 warning_with_file_and_line
6396 (current_function_prototype_file,
6397 current_function_prototype_line,
6398 "prototype declaration");
6401 /* If -traditional, allow `int' argument to match
6402 `unsigned' prototype. */
6403 else if (! (flag_traditional
6404 && TYPE_MAIN_VARIANT (TREE_TYPE (parm)) == integer_type_node
6405 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == unsigned_type_node))
6407 error ("argument `%s' doesn't match prototype",
6408 IDENTIFIER_POINTER (DECL_NAME (parm)));
6409 error_with_file_and_line (current_function_prototype_file,
6410 current_function_prototype_line,
6411 "prototype declaration");
6415 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6418 /* Otherwise, create a prototype that would match. */
6420 else
6422 tree actual = 0, last = 0, type;
6424 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6426 type = perm_tree_cons (NULL_TREE, DECL_ARG_TYPE (parm),
6427 NULL_TREE);
6428 if (last)
6429 TREE_CHAIN (last) = type;
6430 else
6431 actual = type;
6432 last = type;
6434 type = perm_tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6435 if (last)
6436 TREE_CHAIN (last) = type;
6437 else
6438 actual = type;
6440 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6441 of the type of this function, but we need to avoid having this
6442 affect the types of other similarly-typed functions, so we must
6443 first force the generation of an identical (but separate) type
6444 node for the relevant function type. The new node we create
6445 will be a variant of the main variant of the original function
6446 type. */
6448 TREE_TYPE (fndecl) = build_type_copy (TREE_TYPE (fndecl));
6450 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6453 /* Now store the final chain of decls for the arguments
6454 as the decl-chain of the current lexical scope.
6455 Put the enumerators in as well, at the front so that
6456 DECL_ARGUMENTS is not modified. */
6458 storedecls (chainon (nonparms, DECL_ARGUMENTS (fndecl)));
6461 /* Make sure the binding level for the top of the function body
6462 gets a BLOCK if there are any in the function.
6463 Otherwise, the dbx output is wrong. */
6465 keep_next_if_subblocks = 1;
6467 /* ??? This might be an improvement,
6468 but needs to be thought about some more. */
6469 #if 0
6470 keep_next_level_flag = 1;
6471 #endif
6473 /* Write a record describing this function definition to the prototypes
6474 file (if requested). */
6476 gen_aux_info_record (fndecl, 1, 0, prototype);
6478 /* Initialize the RTL code for the function. */
6480 init_function_start (fndecl, input_filename, lineno);
6482 /* If this is a varargs function, inform function.c. */
6484 if (c_function_varargs)
6485 mark_varargs ();
6487 /* Declare __FUNCTION__ and __PRETTY_FUNCTION__ for this function. */
6489 declare_function_name ();
6491 /* Set up parameters and prepare for return, for the function. */
6493 expand_function_start (fndecl, 0);
6495 /* If this function is `main', emit a call to `__main'
6496 to run global initializers, etc. */
6497 if (DECL_NAME (fndecl)
6498 && strcmp (IDENTIFIER_POINTER (DECL_NAME (fndecl)), "main") == 0
6499 && DECL_CONTEXT (fndecl) == NULL_TREE)
6500 expand_main_function ();
6503 /* SPECPARMS is an identifier list--a chain of TREE_LIST nodes
6504 each with a parm name as the TREE_VALUE. A null pointer as TREE_VALUE
6505 stands for an ellipsis in the identifier list.
6507 PARMLIST is the data returned by get_parm_info for the
6508 parmlist that follows the semicolon.
6510 We return a value of the same sort that get_parm_info returns,
6511 except that it describes the combination of identifiers and parmlist. */
6513 tree
6514 combine_parm_decls (specparms, parmlist, void_at_end)
6515 tree specparms, parmlist;
6516 int void_at_end;
6518 register tree fndecl = current_function_decl;
6519 register tree parm;
6521 tree parmdecls = TREE_PURPOSE (parmlist);
6523 /* This is a chain of any other decls that came in among the parm
6524 declarations. They were separated already by get_parm_info,
6525 so we just need to keep them separate. */
6526 tree nonparms = TREE_VALUE (parmlist);
6528 tree types = 0;
6530 for (parm = parmdecls; parm; parm = TREE_CHAIN (parm))
6531 DECL_RESULT (parm) = 0;
6533 for (parm = specparms; parm; parm = TREE_CHAIN (parm))
6535 register tree tail, found = NULL;
6537 /* See if any of the parmdecls specifies this parm by name. */
6538 for (tail = parmdecls; tail; tail = TREE_CHAIN (tail))
6539 if (DECL_NAME (tail) == TREE_VALUE (parm))
6541 found = tail;
6542 break;
6545 /* If declaration already marked, we have a duplicate name.
6546 Complain, and don't use this decl twice. */
6547 if (found && DECL_RESULT (found) != 0)
6549 error_with_decl (found, "multiple parameters named `%s'");
6550 found = 0;
6553 /* If the declaration says "void", complain and ignore it. */
6554 if (found && TYPE_MAIN_VARIANT (TREE_TYPE (found)) == void_type_node)
6556 error_with_decl (found, "parameter `%s' declared void");
6557 TREE_TYPE (found) = integer_type_node;
6558 DECL_ARG_TYPE (found) = integer_type_node;
6559 layout_decl (found, 0);
6562 /* Traditionally, a parm declared float is actually a double. */
6563 if (found && flag_traditional
6564 && TYPE_MAIN_VARIANT (TREE_TYPE (found)) == float_type_node)
6566 TREE_TYPE (found) = double_type_node;
6567 DECL_ARG_TYPE (found) = double_type_node;
6568 layout_decl (found, 0);
6571 /* If no declaration found, default to int. */
6572 if (!found)
6574 found = build_decl (PARM_DECL, TREE_VALUE (parm),
6575 integer_type_node);
6576 DECL_ARG_TYPE (found) = TREE_TYPE (found);
6577 DECL_SOURCE_LINE (found) = DECL_SOURCE_LINE (fndecl);
6578 DECL_SOURCE_FILE (found) = DECL_SOURCE_FILE (fndecl);
6579 error_with_decl (found, "type of parameter `%s' is not declared");
6580 pushdecl (found);
6583 TREE_PURPOSE (parm) = found;
6585 /* Mark this decl as "already found" -- see test, above.
6586 It is safe to use DECL_RESULT for this
6587 since it is not used in PARM_DECLs or CONST_DECLs. */
6588 DECL_RESULT (found) = error_mark_node;
6591 /* Complain about any actual PARM_DECLs not matched with any names. */
6593 for (parm = parmdecls; parm; )
6595 tree next = TREE_CHAIN (parm);
6596 TREE_CHAIN (parm) = 0;
6598 /* Complain about args with incomplete types. */
6599 if (TYPE_SIZE (TREE_TYPE (parm)) == 0)
6601 error_with_decl (parm, "parameter `%s' has incomplete type");
6602 TREE_TYPE (parm) = error_mark_node;
6605 if (DECL_RESULT (parm) == 0)
6607 error_with_decl (parm,
6608 "declaration for parameter `%s' but no such parameter");
6609 /* Pretend the parameter was not missing.
6610 This gets us to a standard state and minimizes
6611 further error messages. */
6612 specparms
6613 = chainon (specparms,
6614 tree_cons (parm, NULL_TREE, NULL_TREE));
6617 parm = next;
6620 /* Chain the declarations together in the order of the list of names.
6621 At the same time, build up a list of their types, in reverse order. */
6623 parm = specparms;
6624 parmdecls = 0;
6626 register tree last;
6627 for (last = 0; parm; parm = TREE_CHAIN (parm))
6628 if (TREE_PURPOSE (parm))
6630 if (last == 0)
6631 parmdecls = TREE_PURPOSE (parm);
6632 else
6633 TREE_CHAIN (last) = TREE_PURPOSE (parm);
6634 last = TREE_PURPOSE (parm);
6635 TREE_CHAIN (last) = 0;
6637 types = saveable_tree_cons (NULL_TREE, TREE_TYPE (parm), types);
6641 if (void_at_end)
6642 return saveable_tree_cons (parmdecls, nonparms,
6643 nreverse (saveable_tree_cons (NULL_TREE,
6644 void_type_node,
6645 types)));
6647 return saveable_tree_cons (parmdecls, nonparms, nreverse (types));
6650 /* Finish up a function declaration and compile that function
6651 all the way to assembler language output. The free the storage
6652 for the function definition.
6654 This is called after parsing the body of the function definition.
6656 NESTED is nonzero if the function being finished is nested in another. */
6658 void
6659 finish_function (nested)
6660 int nested;
6662 register tree fndecl = current_function_decl;
6664 /* TREE_READONLY (fndecl) = 1;
6665 This caused &foo to be of type ptr-to-const-function
6666 which then got a warning when stored in a ptr-to-function variable. */
6668 poplevel (1, 0, 1);
6669 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6671 /* Must mark the RESULT_DECL as being in this function. */
6673 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6675 /* Obey `register' declarations if `setjmp' is called in this fn. */
6676 if (flag_traditional && current_function_calls_setjmp)
6678 setjmp_protect (DECL_INITIAL (fndecl));
6679 setjmp_protect_args ();
6682 if (! strcmp (IDENTIFIER_POINTER (DECL_NAME (fndecl)), "main"))
6684 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6685 != integer_type_node)
6687 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6688 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6689 if (! warn_main)
6690 pedwarn_with_decl (fndecl, "return type of `%s' is not `int'");
6692 else
6694 #ifdef DEFAULT_MAIN_RETURN
6695 /* Make it so that `main' always returns success by default. */
6696 DEFAULT_MAIN_RETURN;
6697 #endif
6701 /* Generate rtl for function exit. */
6702 expand_function_end (input_filename, lineno, 0);
6704 /* So we can tell if jump_optimize sets it to 1. */
6705 can_reach_end = 0;
6707 /* If this is a nested function, protect the local variables in the stack
6708 above us from being collected while we're compiling this function. */
6709 if (ggc_p && nested)
6710 ggc_push_context ();
6712 /* Run the optimizers and output the assembler code for this function. */
6713 rest_of_compilation (fndecl);
6715 /* Undo the GC context switch. */
6716 if (ggc_p && nested)
6717 ggc_pop_context ();
6719 current_function_returns_null |= can_reach_end;
6721 if (warn_missing_noreturn
6722 && !TREE_THIS_VOLATILE (fndecl)
6723 && !current_function_returns_null
6724 && !current_function_returns_value)
6725 warning ("function might be possible candidate for attribute `noreturn'");
6727 if (TREE_THIS_VOLATILE (fndecl) && current_function_returns_null)
6728 warning ("`noreturn' function does return");
6729 else if (warn_return_type && can_reach_end
6730 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl))) != void_type_node)
6731 /* If this function returns non-void and control can drop through,
6732 complain. */
6733 warning ("control reaches end of non-void function");
6734 /* With just -W, complain only if function returns both with
6735 and without a value. */
6736 else if (extra_warnings
6737 && current_function_returns_value && current_function_returns_null)
6738 warning ("this function may return with or without a value");
6740 /* If requested, warn about function definitions where the function will
6741 return a value (usually of some struct or union type) which itself will
6742 take up a lot of stack space. */
6744 if (warn_larger_than && !DECL_EXTERNAL (fndecl) && TREE_TYPE (fndecl))
6746 register tree ret_type = TREE_TYPE (TREE_TYPE (fndecl));
6748 if (ret_type)
6750 register tree ret_type_size = TYPE_SIZE (ret_type);
6752 if (TREE_CODE (ret_type_size) == INTEGER_CST)
6754 unsigned units
6755 = TREE_INT_CST_LOW (ret_type_size) / BITS_PER_UNIT;
6757 if (units > larger_than_size)
6758 warning_with_decl (fndecl,
6759 "size of return value of `%s' is %u bytes",
6760 units);
6765 /* Free all the tree nodes making up this function. */
6766 /* Switch back to allocating nodes permanently
6767 until we start another function. */
6768 if (! nested)
6769 permanent_allocation (1);
6771 if (DECL_SAVED_INSNS (fndecl) == 0 && ! nested)
6773 /* Stop pointing to the local nodes about to be freed. */
6774 /* But DECL_INITIAL must remain nonzero so we know this
6775 was an actual function definition. */
6776 /* For a nested function, this is done in pop_c_function_context. */
6777 /* If rest_of_compilation set this to 0, leave it 0. */
6778 if (DECL_INITIAL (fndecl) != 0)
6779 DECL_INITIAL (fndecl) = error_mark_node;
6780 DECL_ARGUMENTS (fndecl) = 0;
6783 if (DECL_STATIC_CONSTRUCTOR (fndecl))
6785 #ifndef ASM_OUTPUT_CONSTRUCTOR
6786 if (! flag_gnu_linker)
6787 static_ctors = perm_tree_cons (NULL_TREE, fndecl, static_ctors);
6788 else
6789 #endif
6790 assemble_constructor (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl)));
6793 if (DECL_STATIC_DESTRUCTOR (fndecl))
6795 #ifndef ASM_OUTPUT_DESTRUCTOR
6796 if (! flag_gnu_linker)
6797 static_dtors = perm_tree_cons (NULL_TREE, fndecl, static_dtors);
6798 else
6799 #endif
6800 assemble_destructor (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl)));
6803 if (! nested)
6805 /* Let the error reporting routines know that we're outside a
6806 function. For a nested function, this value is used in
6807 pop_c_function_context and then reset via pop_function_context. */
6808 current_function_decl = NULL;
6812 /* Save and restore the variables in this file and elsewhere
6813 that keep track of the progress of compilation of the current function.
6814 Used for nested functions. */
6816 struct language_function
6818 tree named_labels;
6819 tree shadowed_labels;
6820 int returns_value;
6821 int returns_null;
6822 int warn_about_return_type;
6823 int extern_inline;
6824 struct binding_level *binding_level;
6827 /* Save and reinitialize the variables
6828 used during compilation of a C function. */
6830 void
6831 push_c_function_context (f)
6832 struct function *f;
6834 struct language_function *p;
6835 p = (struct language_function *) xmalloc (sizeof (struct language_function));
6836 f->language = p;
6838 p->named_labels = named_labels;
6839 p->shadowed_labels = shadowed_labels;
6840 p->returns_value = current_function_returns_value;
6841 p->returns_null = current_function_returns_null;
6842 p->warn_about_return_type = warn_about_return_type;
6843 p->extern_inline = current_extern_inline;
6844 p->binding_level = current_binding_level;
6847 /* Restore the variables used during compilation of a C function. */
6849 void
6850 pop_c_function_context (f)
6851 struct function *f;
6853 struct language_function *p = f->language;
6854 tree link;
6856 /* Bring back all the labels that were shadowed. */
6857 for (link = shadowed_labels; link; link = TREE_CHAIN (link))
6858 if (DECL_NAME (TREE_VALUE (link)) != 0)
6859 IDENTIFIER_LABEL_VALUE (DECL_NAME (TREE_VALUE (link)))
6860 = TREE_VALUE (link);
6862 if (DECL_SAVED_INSNS (current_function_decl) == 0)
6864 /* Stop pointing to the local nodes about to be freed. */
6865 /* But DECL_INITIAL must remain nonzero so we know this
6866 was an actual function definition. */
6867 DECL_INITIAL (current_function_decl) = error_mark_node;
6868 DECL_ARGUMENTS (current_function_decl) = 0;
6871 named_labels = p->named_labels;
6872 shadowed_labels = p->shadowed_labels;
6873 current_function_returns_value = p->returns_value;
6874 current_function_returns_null = p->returns_null;
6875 warn_about_return_type = p->warn_about_return_type;
6876 current_extern_inline = p->extern_inline;
6877 current_binding_level = p->binding_level;
6879 free (p);
6880 f->language = 0;
6883 /* Mark the language specific parts of F for GC. */
6884 void
6885 mark_c_function_context (f)
6886 struct function *f;
6888 struct language_function *p = f->language;
6890 if (p == 0)
6891 return;
6893 ggc_mark_tree (p->shadowed_labels);
6894 ggc_mark_tree (p->named_labels);
6895 mark_binding_level (&p->binding_level);
6898 /* integrate_decl_tree calls this function, but since we don't use the
6899 DECL_LANG_SPECIFIC field, this is a no-op. */
6901 void
6902 copy_lang_decl (node)
6903 tree node ATTRIBUTE_UNUSED;
6907 /* Mark ARG for GC. */
6908 void
6909 lang_mark_false_label_stack (arg)
6910 struct label_node *arg;
6912 /* C doesn't use false_label_stack. It better be NULL. */
6913 if (arg != NULL)
6914 abort();
6917 /* Mark the language specific bits in T for GC. */
6918 void
6919 lang_mark_tree (t)
6920 tree t;
6922 if (TREE_CODE (t) == IDENTIFIER_NODE)
6924 struct lang_identifier *i = (struct lang_identifier *) t;
6925 ggc_mark_tree (i->global_value);
6926 ggc_mark_tree (i->local_value);
6927 ggc_mark_tree (i->label_value);
6928 ggc_mark_tree (i->implicit_decl);
6929 ggc_mark_tree (i->error_locus);
6930 ggc_mark_tree (i->limbo_value);
6932 else if (TYPE_P (t) && TYPE_LANG_SPECIFIC (t))
6933 ggc_mark (TYPE_LANG_SPECIFIC (t));