2002-04-02 David S. Miller <davem@redhat.com>
[official-gcc.git] / gcc / c-decl.c
blobacb71e589c13c499d387229c579078a3f52e30ce
1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 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 "intl.h"
32 #include "tree.h"
33 #include "tree-inline.h"
34 #include "rtl.h"
35 #include "flags.h"
36 #include "function.h"
37 #include "output.h"
38 #include "expr.h"
39 #include "c-tree.h"
40 #include "c-lex.h"
41 #include "toplev.h"
42 #include "ggc.h"
43 #include "tm_p.h"
44 #include "cpplib.h"
45 #include "target.h"
46 #include "debug.h"
47 #include "timevar.h"
48 #include "c-common.h"
49 #include "c-pragma.h"
51 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
52 enum decl_context
53 { NORMAL, /* Ordinary declaration */
54 FUNCDEF, /* Function definition */
55 PARM, /* Declaration of parm before function body */
56 FIELD, /* Declaration inside struct or union */
57 BITFIELD, /* Likewise but with specified width */
58 TYPENAME}; /* Typename (inside cast or sizeof) */
61 /* Nonzero if we have seen an invalid cross reference
62 to a struct, union, or enum, but not yet printed the message. */
64 tree pending_invalid_xref;
65 /* File and line to appear in the eventual error message. */
66 const char *pending_invalid_xref_file;
67 int pending_invalid_xref_line;
69 /* While defining an enum type, this is 1 plus the last enumerator
70 constant value. Note that will do not have to save this or `enum_overflow'
71 around nested function definition since such a definition could only
72 occur in an enum value expression and we don't use these variables in
73 that case. */
75 static tree enum_next_value;
77 /* Nonzero means that there was overflow computing enum_next_value. */
79 static int enum_overflow;
81 /* Parsing a function declarator leaves a list of parameter names
82 or a chain or parameter decls here. */
84 static tree last_function_parms;
86 /* Parsing a function declarator leaves here a chain of structure
87 and enum types declared in the parmlist. */
89 static tree last_function_parm_tags;
91 /* After parsing the declarator that starts a function definition,
92 `start_function' puts here the list of parameter names or chain of decls.
93 `store_parm_decls' finds it here. */
95 static tree current_function_parms;
97 /* Similar, for last_function_parm_tags. */
98 static tree current_function_parm_tags;
100 /* Similar, for the file and line that the prototype came from if this is
101 an old-style definition. */
102 static const char *current_function_prototype_file;
103 static int current_function_prototype_line;
105 /* The current statement tree. */
107 static struct stmt_tree_s c_stmt_tree;
109 /* The current scope statement stack. */
111 static tree c_scope_stmt_stack;
113 /* A list (chain of TREE_LIST nodes) of all LABEL_DECLs in the function
114 that have names. Here so we can clear out their names' definitions
115 at the end of the function. */
117 static tree named_labels;
119 /* A list of LABEL_DECLs from outer contexts that are currently shadowed. */
121 static tree shadowed_labels;
123 /* Nonzero when store_parm_decls is called indicates a varargs function.
124 Value not meaningful after store_parm_decls. */
126 static int c_function_varargs;
128 /* Set to 0 at beginning of a function definition, set to 1 if
129 a return statement that specifies a return value is seen. */
131 int current_function_returns_value;
133 /* Set to 0 at beginning of a function definition, set to 1 if
134 a return statement with no argument is seen. */
136 int current_function_returns_null;
138 /* Set to 0 at beginning of a function definition, set to 1 if
139 a call to a noreturn function is seen. */
141 int current_function_returns_abnormally;
143 /* Set to nonzero by `grokdeclarator' for a function
144 whose return type is defaulted, if warnings for this are desired. */
146 static int warn_about_return_type;
148 /* Nonzero when starting a function declared `extern inline'. */
150 static int current_extern_inline;
152 /* For each binding contour we allocate a binding_level structure
153 * which records the names defined in that contour.
154 * Contours include:
155 * 0) the global one
156 * 1) one for each function definition,
157 * where internal declarations of the parameters appear.
158 * 2) one for each compound statement,
159 * to record its declarations.
161 * The current meaning of a name can be found by searching the levels from
162 * the current one out to the global one.
165 /* Note that the information in the `names' component of the global contour
166 is duplicated in the IDENTIFIER_GLOBAL_VALUEs of all identifiers. */
168 struct binding_level
170 /* A chain of _DECL nodes for all variables, constants, functions,
171 and typedef types. These are in the reverse of the order supplied.
173 tree names;
175 /* A list of structure, union and enum definitions,
176 * for looking up tag names.
177 * It is a chain of TREE_LIST nodes, each of whose TREE_PURPOSE is a name,
178 * or NULL_TREE; and whose TREE_VALUE is a RECORD_TYPE, UNION_TYPE,
179 * or ENUMERAL_TYPE node.
181 tree tags;
183 /* For each level, a list of shadowed outer-level local definitions
184 to be restored when this level is popped.
185 Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
186 whose TREE_VALUE is its old definition (a kind of ..._DECL node). */
187 tree shadowed;
189 /* For each level (except not the global one),
190 a chain of BLOCK nodes for all the levels
191 that were entered and exited one level down. */
192 tree blocks;
194 /* The BLOCK node for this level, if one has been preallocated.
195 If 0, the BLOCK is allocated (if needed) when the level is popped. */
196 tree this_block;
198 /* The binding level which this one is contained in (inherits from). */
199 struct binding_level *level_chain;
201 /* Nonzero for the level that holds the parameters of a function. */
202 char parm_flag;
204 /* Nonzero if this level "doesn't exist" for tags. */
205 char tag_transparent;
207 /* Nonzero if sublevels of this level "don't exist" for tags.
208 This is set in the parm level of a function definition
209 while reading the function body, so that the outermost block
210 of the function body will be tag-transparent. */
211 char subblocks_tag_transparent;
213 /* Nonzero means make a BLOCK for this level regardless of all else. */
214 char keep;
216 /* Nonzero means make a BLOCK if this level has any subblocks. */
217 char keep_if_subblocks;
219 /* Number of decls in `names' that have incomplete
220 structure or union types. */
221 int n_incomplete;
223 /* A list of decls giving the (reversed) specified order of parms,
224 not including any forward-decls in the parmlist.
225 This is so we can put the parms in proper order for assign_parms. */
226 tree parm_order;
229 #define NULL_BINDING_LEVEL (struct binding_level *) NULL
231 /* The binding level currently in effect. */
233 static struct binding_level *current_binding_level;
235 /* A chain of binding_level structures awaiting reuse. */
237 static struct binding_level *free_binding_level;
239 /* The outermost binding level, for names of file scope.
240 This is created when the compiler is started and exists
241 through the entire run. */
243 static struct binding_level *global_binding_level;
245 /* Binding level structures are initialized by copying this one. */
247 static struct binding_level clear_binding_level
248 = {NULL, NULL, NULL, NULL, NULL, NULL_BINDING_LEVEL, 0, 0, 0, 0, 0, 0,
249 NULL};
251 /* Nonzero means unconditionally make a BLOCK for the next level pushed. */
253 static int keep_next_level_flag;
255 /* Nonzero means make a BLOCK for the next level pushed
256 if it has subblocks. */
258 static int keep_next_if_subblocks;
260 /* The chain of outer levels of label scopes.
261 This uses the same data structure used for binding levels,
262 but it works differently: each link in the chain records
263 saved values of named_labels and shadowed_labels for
264 a label binding level outside the current one. */
266 static struct binding_level *label_level_chain;
268 /* Functions called automatically at the beginning and end of execution. */
270 tree static_ctors, static_dtors;
272 /* Forward declarations. */
274 static struct binding_level * make_binding_level PARAMS ((void));
275 static void mark_binding_level PARAMS ((void *));
276 static void clear_limbo_values PARAMS ((tree));
277 static int duplicate_decls PARAMS ((tree, tree, int));
278 static int redeclaration_error_message PARAMS ((tree, tree));
279 static void storedecls PARAMS ((tree));
280 static void storetags PARAMS ((tree));
281 static tree lookup_tag PARAMS ((enum tree_code, tree,
282 struct binding_level *, int));
283 static tree lookup_tag_reverse PARAMS ((tree));
284 static tree grokdeclarator PARAMS ((tree, tree, enum decl_context,
285 int));
286 static tree grokparms PARAMS ((tree, int));
287 static void layout_array_type PARAMS ((tree));
288 static tree c_make_fname_decl PARAMS ((tree, int));
289 static void c_expand_body PARAMS ((tree, int, int));
290 static void warn_if_shadowing PARAMS ((tree, tree));
292 /* C-specific option variables. */
294 /* Nonzero means allow type mismatches in conditional expressions;
295 just make their values `void'. */
297 int flag_cond_mismatch;
299 /* Nonzero means don't recognize the keyword `asm'. */
301 int flag_no_asm;
303 /* Nonzero means enable C89 Amendment 1 features. */
305 int flag_isoc94 = 0;
307 /* Nonzero means use the ISO C99 dialect of C. */
309 int flag_isoc99 = 0;
311 /* Nonzero means that we have builtin functions, and main is an int */
313 int flag_hosted = 1;
315 /* Nonzero means add default format_arg attributes for functions not
316 in ISO C. */
318 int flag_noniso_default_format_attributes = 1;
320 /* Nonzero means to treat bitfields as signed unless they say `unsigned'. */
322 int flag_signed_bitfields = 1;
323 int explicit_flag_signed_bitfields = 0;
325 /* Nonzero means warn about use of implicit int. */
327 int warn_implicit_int;
329 /* Nonzero means warn about usage of long long when `-pedantic'. */
331 int warn_long_long = 1;
333 /* Nonzero means message about use of implicit function declarations;
334 1 means warning; 2 means error. */
336 int mesg_implicit_function_declaration = -1;
338 /* Nonzero means give string constants the type `const char *'
339 to get extra warnings from them. These warnings will be too numerous
340 to be useful, except in thoroughly ANSIfied programs. */
342 int flag_const_strings;
344 /* Nonzero means warn about pointer casts that can drop a type qualifier
345 from the pointer target type. */
347 int warn_cast_qual;
349 /* Nonzero means warn when casting a function call to a type that does
350 not match the return type (e.g. (float)sqrt() or (anything*)malloc()
351 when there is no previous declaration of sqrt or malloc. */
353 int warn_bad_function_cast;
355 /* Warn about functions which might be candidates for format attributes. */
357 int warn_missing_format_attribute;
359 /* Warn about traditional constructs whose meanings changed in ANSI C. */
361 int warn_traditional;
363 /* Nonzero means warn about sizeof(function) or addition/subtraction
364 of function pointers. */
366 int warn_pointer_arith;
368 /* Nonzero means warn for non-prototype function decls
369 or non-prototyped defs without previous prototype. */
371 int warn_strict_prototypes;
373 /* Nonzero means warn for any global function def
374 without separate previous prototype decl. */
376 int warn_missing_prototypes;
378 /* Nonzero means warn for any global function def
379 without separate previous decl. */
381 int warn_missing_declarations;
383 /* Nonzero means warn about multiple (redundant) decls for the same single
384 variable or function. */
386 int warn_redundant_decls = 0;
388 /* Nonzero means warn about extern declarations of objects not at
389 file-scope level and about *all* declarations of functions (whether
390 extern or static) not at file-scope level. Note that we exclude
391 implicit function declarations. To get warnings about those, use
392 -Wimplicit. */
394 int warn_nested_externs = 0;
396 /* Warn about a subscript that has type char. */
398 int warn_char_subscripts = 0;
400 /* Warn if a type conversion is done that might have confusing results. */
402 int warn_conversion;
404 /* Warn if adding () is suggested. */
406 int warn_parentheses;
408 /* Warn if initializer is not completely bracketed. */
410 int warn_missing_braces;
412 /* Warn if main is suspicious. */
414 int warn_main;
416 /* Warn about #pragma directives that are not recognised. */
418 int warn_unknown_pragmas = 0; /* Tri state variable. */
420 /* Warn about comparison of signed and unsigned values.
421 If -1, neither -Wsign-compare nor -Wno-sign-compare has been specified. */
423 int warn_sign_compare = -1;
425 /* Warn about testing equality of floating point numbers. */
427 int warn_float_equal = 0;
429 /* Nonzero means warn about use of multicharacter literals. */
431 int warn_multichar = 1;
433 /* Nonzero means `$' can be in an identifier. */
435 #ifndef DOLLARS_IN_IDENTIFIERS
436 #define DOLLARS_IN_IDENTIFIERS 1
437 #endif
438 int dollars_in_ident = DOLLARS_IN_IDENTIFIERS;
440 /* States indicating how grokdeclarator() should handle declspecs marked
441 with __attribute__((deprecated)). An object declared as
442 __attribute__((deprecated)) suppresses warnings of uses of other
443 deprecated items. */
445 enum deprecated_states {
446 DEPRECATED_NORMAL,
447 DEPRECATED_SUPPRESS
450 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
452 /* Decode the string P as a language-specific option for C.
453 Return the number of strings consumed. Should not complain
454 if it does not recognise the option. */
457 c_decode_option (argc, argv)
458 int argc ATTRIBUTE_UNUSED;
459 char **argv;
461 int strings_processed;
462 char *p = argv[0];
464 static const struct {
465 /* The name of the option. */
466 const char *option;
467 /* If non-NULL, a flag variable to set to 0 or 1. If NULL,
468 this means that cpp handles this option. */
469 int *flag;
470 } warn_options[] = {
471 /* This list is in alphabetical order. Keep it like that. */
472 { "bad-function-cast", &warn_bad_function_cast },
473 { "cast-qual", &warn_cast_qual },
474 { "char-subscripts", &warn_char_subscripts },
475 { "comment", NULL },
476 { "comments", NULL },
477 { "conversion", &warn_conversion },
478 { "div-by-zero", &warn_div_by_zero },
479 { "float-equal", &warn_float_equal },
480 { "format-extra-args", &warn_format_extra_args },
481 { "format-nonliteral", &warn_format_nonliteral },
482 { "format-security", &warn_format_security },
483 { "format-y2k", &warn_format_y2k },
484 { "implicit-function-declaration", &mesg_implicit_function_declaration },
485 { "implicit-int", &warn_implicit_int },
486 { "import", NULL },
487 { "long-long", &warn_long_long },
488 { "main", &warn_main },
489 { "missing-braces", &warn_missing_braces },
490 { "missing-declarations", &warn_missing_declarations },
491 { "missing-format-attribute", &warn_missing_format_attribute },
492 { "missing-prototypes", &warn_missing_prototypes },
493 { "multichar", &warn_multichar },
494 { "nested-externs", &warn_nested_externs },
495 { "parentheses", &warn_parentheses },
496 { "pointer-arith", &warn_pointer_arith },
497 { "redundant-decls", &warn_redundant_decls },
498 { "return-type", &warn_return_type },
499 { "sequence-point", &warn_sequence_point },
500 { "sign-compare", &warn_sign_compare },
501 { "strict-prototypes", &warn_strict_prototypes },
502 { "traditional", &warn_traditional },
503 { "trigraphs", NULL },
504 { "undef", NULL },
505 { "write-strings", &flag_const_strings }
508 strings_processed = cpp_handle_option (parse_in, argc, argv, 0);
510 if (!strcmp (p, "-fhosted") || !strcmp (p, "-fno-freestanding"))
512 flag_hosted = 1;
513 flag_no_builtin = 0;
515 else if (!strcmp (p, "-ffreestanding") || !strcmp (p, "-fno-hosted"))
517 flag_hosted = 0;
518 flag_no_builtin = 1;
519 /* warn_main will be 2 if set by -Wall, 1 if set by -Wmain */
520 if (warn_main == 2)
521 warn_main = 0;
523 else if (!strncmp (p, "-std=", 5))
525 /* Select the appropriate language standard. We currently
526 recognize:
527 -std=iso9899:1990 same as -ansi
528 -std=iso9899:199409 ISO C as modified in amend. 1
529 -std=iso9899:1999 ISO C 99
530 -std=c89 same as -std=iso9899:1990
531 -std=c99 same as -std=iso9899:1999
532 -std=gnu89 default, iso9899:1990 + gnu extensions
533 -std=gnu99 iso9899:1999 + gnu extensions
535 const char *const argstart = &p[5];
537 if (!strcmp (argstart, "iso9899:1990")
538 || !strcmp (argstart, "c89"))
540 iso_1990:
541 flag_isoc94 = 0;
542 iso_1994:
543 flag_writable_strings = 0;
544 flag_no_asm = 1;
545 flag_no_nonansi_builtin = 1;
546 flag_noniso_default_format_attributes = 0;
547 flag_isoc99 = 0;
549 else if (!strcmp (argstart, "iso9899:199409"))
551 flag_isoc94 = 1;
552 goto iso_1994;
554 else if (!strcmp (argstart, "iso9899:199x")
555 || !strcmp (argstart, "iso9899:1999")
556 || !strcmp (argstart, "c9x")
557 || !strcmp (argstart, "c99"))
559 flag_writable_strings = 0;
560 flag_no_asm = 1;
561 flag_no_nonansi_builtin = 1;
562 flag_noniso_default_format_attributes = 0;
563 flag_isoc99 = 1;
564 flag_isoc94 = 1;
566 else if (!strcmp (argstart, "gnu89"))
568 flag_writable_strings = 0;
569 flag_no_asm = 0;
570 flag_no_nonansi_builtin = 0;
571 flag_noniso_default_format_attributes = 1;
572 flag_isoc99 = 0;
573 flag_isoc94 = 0;
575 else if (!strcmp (argstart, "gnu9x") || !strcmp (argstart, "gnu99"))
577 flag_writable_strings = 0;
578 flag_no_asm = 0;
579 flag_no_nonansi_builtin = 0;
580 flag_noniso_default_format_attributes = 1;
581 flag_isoc99 = 1;
582 flag_isoc94 = 1;
584 else if (!strcmp (argstart, "c++98"))
585 ; /* Handled by cpplib. */
586 else
587 error ("unknown C standard `%s'", argstart);
589 else if (!strcmp (p, "-fdollars-in-identifiers"))
590 dollars_in_ident = 1;
591 else if (!strcmp (p, "-fno-dollars-in-identifiers"))
592 dollars_in_ident = 0;
593 else if (!strcmp (p, "-fsigned-char"))
594 flag_signed_char = 1;
595 else if (!strcmp (p, "-funsigned-char"))
596 flag_signed_char = 0;
597 else if (!strcmp (p, "-fno-signed-char"))
598 flag_signed_char = 0;
599 else if (!strcmp (p, "-fno-unsigned-char"))
600 flag_signed_char = 1;
601 else if (!strcmp (p, "-fsigned-bitfields")
602 || !strcmp (p, "-fno-unsigned-bitfields"))
604 flag_signed_bitfields = 1;
605 explicit_flag_signed_bitfields = 1;
607 else if (!strcmp (p, "-funsigned-bitfields")
608 || !strcmp (p, "-fno-signed-bitfields"))
610 flag_signed_bitfields = 0;
611 explicit_flag_signed_bitfields = 1;
613 else if (!strcmp (p, "-fshort-enums"))
614 flag_short_enums = 1;
615 else if (!strcmp (p, "-fno-short-enums"))
616 flag_short_enums = 0;
617 else if (!strcmp (p, "-fshort-wchar"))
618 flag_short_wchar = 1;
619 else if (!strcmp (p, "-fno-short-wchar"))
620 flag_short_wchar = 0;
621 else if (!strcmp (p, "-fcond-mismatch"))
622 flag_cond_mismatch = 1;
623 else if (!strcmp (p, "-fno-cond-mismatch"))
624 flag_cond_mismatch = 0;
625 else if (!strcmp (p, "-fshort-double"))
626 flag_short_double = 1;
627 else if (!strcmp (p, "-fno-short-double"))
628 flag_short_double = 0;
629 else if (!strcmp (p, "-fasm"))
630 flag_no_asm = 0;
631 else if (!strcmp (p, "-fno-asm"))
632 flag_no_asm = 1;
633 else if (!strcmp (p, "-fbuiltin"))
634 flag_no_builtin = 0;
635 else if (!strcmp (p, "-fno-builtin"))
636 flag_no_builtin = 1;
637 else if (!strncmp (p, "-fno-builtin-", strlen ("-fno-builtin-")))
638 disable_builtin_function (p + strlen ("-fno-builtin-"));
639 else if (p[0] == '-' && p[1] == 'f' && dump_switch_p (p + 2))
641 else if (!strcmp (p, "-ansi"))
642 goto iso_1990;
643 else if (!strcmp (p, "-Werror-implicit-function-declaration"))
644 mesg_implicit_function_declaration = 2;
645 else if (!strncmp (p, "-Wformat=", 9))
646 set_Wformat (atoi (p + 9));
647 else if (!strcmp (p, "-Wformat"))
648 set_Wformat (1);
649 else if (!strcmp (p, "-Wno-format"))
650 set_Wformat (0);
651 else if (!strcmp (p, "-Wimplicit"))
653 warn_implicit_int = 1;
654 if (mesg_implicit_function_declaration != 2)
655 mesg_implicit_function_declaration = 1;
657 else if (!strcmp (p, "-Wno-implicit"))
658 warn_implicit_int = 0, mesg_implicit_function_declaration = 0;
659 else if (!strcmp (p, "-Wno-main"))
660 warn_main = -1;
661 else if (!strcmp (p, "-Wunknown-pragmas"))
662 /* Set to greater than 1, so that even unknown pragmas in system
663 headers will be warned about. */
664 warn_unknown_pragmas = 2;
665 else if (!strcmp (p, "-Wno-unknown-pragmas"))
666 warn_unknown_pragmas = 0;
667 else if (!strcmp (p, "-Wall"))
669 /* We save the value of warn_uninitialized, since if they put
670 -Wuninitialized on the command line, we need to generate a
671 warning about not using it without also specifying -O. */
672 if (warn_uninitialized != 1)
673 warn_uninitialized = 2;
674 warn_implicit_int = 1;
675 mesg_implicit_function_declaration = 1;
676 warn_return_type = 1;
677 set_Wunused (1);
678 warn_switch = 1;
679 set_Wformat (1);
680 warn_char_subscripts = 1;
681 warn_parentheses = 1;
682 warn_sequence_point = 1;
683 warn_missing_braces = 1;
684 /* We set this to 2 here, but 1 in -Wmain, so -ffreestanding can turn
685 it off only if it's not explicit. */
686 warn_main = 2;
687 /* Only warn about unknown pragmas that are not in system headers. */
688 warn_unknown_pragmas = 1;
690 else if (!strcmp (p, "-E"))
691 flag_preprocess_only = 1;
692 else
694 size_t i;
695 for (i = 0; i < sizeof (warn_options) / sizeof (warn_options[0]); i++)
696 if (strncmp (p, "-W", 2) == 0
697 && warn_options[i].flag
698 && (strcmp (p+2, warn_options[i].option) == 0
699 || (strncmp (p+2, "no-", 3) == 0
700 && strcmp (p+5, warn_options[i].option) == 0)))
702 *(warn_options[i].flag) = strncmp (p+2, "no-", 3) != 0;
703 return 1;
705 return strings_processed;
708 return 1;
711 void
712 c_print_identifier (file, node, indent)
713 FILE *file;
714 tree node;
715 int indent;
717 print_node (file, "global", IDENTIFIER_GLOBAL_VALUE (node), indent + 4);
718 print_node (file, "local", IDENTIFIER_LOCAL_VALUE (node), indent + 4);
719 print_node (file, "label", IDENTIFIER_LABEL_VALUE (node), indent + 4);
720 print_node (file, "implicit", IDENTIFIER_IMPLICIT_DECL (node), indent + 4);
721 print_node (file, "error locus", IDENTIFIER_ERROR_LOCUS (node), indent + 4);
722 print_node (file, "limbo value", IDENTIFIER_LIMBO_VALUE (node), indent + 4);
723 if (C_IS_RESERVED_WORD (node))
725 tree rid = ridpointers[C_RID_CODE (node)];
726 indent_to (file, indent + 4);
727 fprintf (file, "rid ");
728 fprintf (file, HOST_PTR_PRINTF, (void *)rid);
729 fprintf (file, " \"%s\"", IDENTIFIER_POINTER (rid));
733 /* Hook called at end of compilation to assume 1 elt
734 for a top-level tentative array defn that wasn't complete before. */
736 void
737 c_finish_incomplete_decl (decl)
738 tree decl;
740 if (TREE_CODE (decl) == VAR_DECL)
742 tree type = TREE_TYPE (decl);
743 if (type != error_mark_node
744 && TREE_CODE (type) == ARRAY_TYPE
745 && ! DECL_EXTERNAL (decl)
746 && TYPE_DOMAIN (type) == 0)
748 warning_with_decl (decl, "array `%s' assumed to have one element");
750 complete_array_type (type, NULL_TREE, 1);
752 layout_decl (decl, 0);
757 /* Create a new `struct binding_level'. */
759 static struct binding_level *
760 make_binding_level ()
762 /* NOSTRICT */
763 return (struct binding_level *) xmalloc (sizeof (struct binding_level));
766 /* Nonzero if we are currently in the global binding level. */
769 global_bindings_p ()
771 return current_binding_level == global_binding_level;
774 void
775 keep_next_level ()
777 keep_next_level_flag = 1;
780 /* Nonzero if the current level needs to have a BLOCK made. */
783 kept_level_p ()
785 return ((current_binding_level->keep_if_subblocks
786 && current_binding_level->blocks != 0)
787 || current_binding_level->keep
788 || current_binding_level->names != 0
789 || (current_binding_level->tags != 0
790 && !current_binding_level->tag_transparent));
793 /* Identify this binding level as a level of parameters.
794 DEFINITION_FLAG is 1 for a definition, 0 for a declaration.
795 But it turns out there is no way to pass the right value for
796 DEFINITION_FLAG, so we ignore it. */
798 void
799 declare_parm_level (definition_flag)
800 int definition_flag ATTRIBUTE_UNUSED;
802 current_binding_level->parm_flag = 1;
805 /* Nonzero if currently making parm declarations. */
808 in_parm_level_p ()
810 return current_binding_level->parm_flag;
813 /* Enter a new binding level.
814 If TAG_TRANSPARENT is nonzero, do so only for the name space of variables,
815 not for that of tags. */
817 void
818 pushlevel (tag_transparent)
819 int tag_transparent;
821 struct binding_level *newlevel = NULL_BINDING_LEVEL;
823 /* If this is the top level of a function,
824 just make sure that NAMED_LABELS is 0. */
826 if (current_binding_level == global_binding_level)
828 named_labels = 0;
831 /* Reuse or create a struct for this binding level. */
833 if (free_binding_level)
835 newlevel = free_binding_level;
836 free_binding_level = free_binding_level->level_chain;
838 else
840 newlevel = make_binding_level ();
843 /* Add this level to the front of the chain (stack) of levels that
844 are active. */
846 *newlevel = clear_binding_level;
847 newlevel->tag_transparent
848 = (tag_transparent
849 || (current_binding_level
850 ? current_binding_level->subblocks_tag_transparent
851 : 0));
852 newlevel->level_chain = current_binding_level;
853 current_binding_level = newlevel;
854 newlevel->keep = keep_next_level_flag;
855 keep_next_level_flag = 0;
856 newlevel->keep_if_subblocks = keep_next_if_subblocks;
857 keep_next_if_subblocks = 0;
860 /* Clear the limbo values of all identifiers defined in BLOCK or a subblock. */
862 static void
863 clear_limbo_values (block)
864 tree block;
866 tree tem;
868 for (tem = BLOCK_VARS (block); tem; tem = TREE_CHAIN (tem))
869 if (DECL_NAME (tem) != 0)
870 IDENTIFIER_LIMBO_VALUE (DECL_NAME (tem)) = 0;
872 for (tem = BLOCK_SUBBLOCKS (block); tem; tem = TREE_CHAIN (tem))
873 clear_limbo_values (tem);
876 /* Exit a binding level.
877 Pop the level off, and restore the state of the identifier-decl mappings
878 that were in effect when this level was entered.
880 If KEEP is nonzero, this level had explicit declarations, so
881 and create a "block" (a BLOCK node) for the level
882 to record its declarations and subblocks for symbol table output.
884 If FUNCTIONBODY is nonzero, this level is the body of a function,
885 so create a block as if KEEP were set and also clear out all
886 label names.
888 If REVERSE is nonzero, reverse the order of decls before putting
889 them into the BLOCK. */
891 tree
892 poplevel (keep, reverse, functionbody)
893 int keep;
894 int reverse;
895 int functionbody;
897 tree link;
898 /* The chain of decls was accumulated in reverse order.
899 Put it into forward order, just for cleanliness. */
900 tree decls;
901 tree tags = current_binding_level->tags;
902 tree subblocks = current_binding_level->blocks;
903 tree block = 0;
904 tree decl;
905 int block_previously_created;
907 keep |= current_binding_level->keep;
909 /* This warning is turned off because it causes warnings for
910 declarations like `extern struct foo *x'. */
911 #if 0
912 /* Warn about incomplete structure types in this level. */
913 for (link = tags; link; link = TREE_CHAIN (link))
914 if (!COMPLETE_TYPE_P (TREE_VALUE (link)))
916 tree type = TREE_VALUE (link);
917 tree type_name = TYPE_NAME (type);
918 char *id = IDENTIFIER_POINTER (TREE_CODE (type_name) == IDENTIFIER_NODE
919 ? type_name
920 : DECL_NAME (type_name));
921 switch (TREE_CODE (type))
923 case RECORD_TYPE:
924 error ("`struct %s' incomplete in scope ending here", id);
925 break;
926 case UNION_TYPE:
927 error ("`union %s' incomplete in scope ending here", id);
928 break;
929 case ENUMERAL_TYPE:
930 error ("`enum %s' incomplete in scope ending here", id);
931 break;
934 #endif /* 0 */
936 /* Get the decls in the order they were written.
937 Usually current_binding_level->names is in reverse order.
938 But parameter decls were previously put in forward order. */
940 if (reverse)
941 current_binding_level->names
942 = decls = nreverse (current_binding_level->names);
943 else
944 decls = current_binding_level->names;
946 /* Output any nested inline functions within this block
947 if they weren't already output. */
949 for (decl = decls; decl; decl = TREE_CHAIN (decl))
950 if (TREE_CODE (decl) == FUNCTION_DECL
951 && ! TREE_ASM_WRITTEN (decl)
952 && DECL_INITIAL (decl) != 0
953 && TREE_ADDRESSABLE (decl))
955 /* If this decl was copied from a file-scope decl
956 on account of a block-scope extern decl,
957 propagate TREE_ADDRESSABLE to the file-scope decl.
959 DECL_ABSTRACT_ORIGIN can be set to itself if warn_return_type is
960 true, since then the decl goes through save_for_inline_copying. */
961 if (DECL_ABSTRACT_ORIGIN (decl) != 0
962 && DECL_ABSTRACT_ORIGIN (decl) != decl)
963 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (decl)) = 1;
966 /* We used to warn about unused variables in expand_end_bindings,
967 i.e. while generating RTL. But in function-at-a-time mode we may
968 choose to never expand a function at all (e.g. auto inlining), so
969 we do this explicitly now. */
970 warn_about_unused_variables (getdecls ());
972 /* If there were any declarations or structure tags in that level,
973 or if this level is a function body,
974 create a BLOCK to record them for the life of this function. */
976 block = 0;
977 block_previously_created = (current_binding_level->this_block != 0);
978 if (block_previously_created)
979 block = current_binding_level->this_block;
980 else if (keep || functionbody
981 || (current_binding_level->keep_if_subblocks && subblocks != 0))
982 block = make_node (BLOCK);
983 if (block != 0)
985 BLOCK_VARS (block) = decls;
986 BLOCK_SUBBLOCKS (block) = subblocks;
989 /* In each subblock, record that this is its superior. */
991 for (link = subblocks; link; link = TREE_CHAIN (link))
992 BLOCK_SUPERCONTEXT (link) = block;
994 /* Clear out the meanings of the local variables of this level. */
996 for (link = decls; link; link = TREE_CHAIN (link))
998 if (DECL_NAME (link) != 0)
1000 /* If the ident. was used or addressed via a local extern decl,
1001 don't forget that fact. */
1002 if (DECL_EXTERNAL (link))
1004 if (TREE_USED (link))
1005 TREE_USED (DECL_NAME (link)) = 1;
1006 if (TREE_ADDRESSABLE (link))
1007 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (link)) = 1;
1009 IDENTIFIER_LOCAL_VALUE (DECL_NAME (link)) = 0;
1013 /* Restore all name-meanings of the outer levels
1014 that were shadowed by this level. */
1016 for (link = current_binding_level->shadowed; link; link = TREE_CHAIN (link))
1017 IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
1019 /* If the level being exited is the top level of a function,
1020 check over all the labels, and clear out the current
1021 (function local) meanings of their names. */
1023 if (functionbody)
1025 clear_limbo_values (block);
1027 /* If this is the top level block of a function,
1028 the vars are the function's parameters.
1029 Don't leave them in the BLOCK because they are
1030 found in the FUNCTION_DECL instead. */
1032 BLOCK_VARS (block) = 0;
1034 /* Clear out the definitions of all label names,
1035 since their scopes end here,
1036 and add them to BLOCK_VARS. */
1038 for (link = named_labels; link; link = TREE_CHAIN (link))
1040 tree label = TREE_VALUE (link);
1042 if (DECL_INITIAL (label) == 0)
1044 error_with_decl (label, "label `%s' used but not defined");
1045 /* Avoid crashing later. */
1046 define_label (input_filename, lineno,
1047 DECL_NAME (label));
1049 else if (warn_unused_label && !TREE_USED (label))
1050 warning_with_decl (label, "label `%s' defined but not used");
1051 IDENTIFIER_LABEL_VALUE (DECL_NAME (label)) = 0;
1053 /* Put the labels into the "variables" of the
1054 top-level block, so debugger can see them. */
1055 TREE_CHAIN (label) = BLOCK_VARS (block);
1056 BLOCK_VARS (block) = label;
1060 /* Pop the current level, and free the structure for reuse. */
1063 struct binding_level *level = current_binding_level;
1064 current_binding_level = current_binding_level->level_chain;
1066 level->level_chain = free_binding_level;
1067 free_binding_level = level;
1070 /* Dispose of the block that we just made inside some higher level. */
1071 if (functionbody)
1072 DECL_INITIAL (current_function_decl) = block;
1073 else if (block)
1075 if (!block_previously_created)
1076 current_binding_level->blocks
1077 = chainon (current_binding_level->blocks, block);
1079 /* If we did not make a block for the level just exited,
1080 any blocks made for inner levels
1081 (since they cannot be recorded as subblocks in that level)
1082 must be carried forward so they will later become subblocks
1083 of something else. */
1084 else if (subblocks)
1085 current_binding_level->blocks
1086 = chainon (current_binding_level->blocks, subblocks);
1088 /* Set the TYPE_CONTEXTs for all of the tagged types belonging to this
1089 binding contour so that they point to the appropriate construct, i.e.
1090 either to the current FUNCTION_DECL node, or else to the BLOCK node
1091 we just constructed.
1093 Note that for tagged types whose scope is just the formal parameter
1094 list for some function type specification, we can't properly set
1095 their TYPE_CONTEXTs here, because we don't have a pointer to the
1096 appropriate FUNCTION_TYPE node readily available to us. For those
1097 cases, the TYPE_CONTEXTs of the relevant tagged type nodes get set
1098 in `grokdeclarator' as soon as we have created the FUNCTION_TYPE
1099 node which will represent the "scope" for these "parameter list local"
1100 tagged types. */
1102 if (functionbody)
1103 for (link = tags; link; link = TREE_CHAIN (link))
1104 TYPE_CONTEXT (TREE_VALUE (link)) = current_function_decl;
1105 else if (block)
1106 for (link = tags; link; link = TREE_CHAIN (link))
1107 TYPE_CONTEXT (TREE_VALUE (link)) = block;
1109 if (block)
1110 TREE_USED (block) = 1;
1112 return block;
1115 /* Insert BLOCK at the end of the list of subblocks of the
1116 current binding level. This is used when a BIND_EXPR is expanded,
1117 to handle the BLOCK node inside the BIND_EXPR. */
1119 void
1120 insert_block (block)
1121 tree block;
1123 TREE_USED (block) = 1;
1124 current_binding_level->blocks
1125 = chainon (current_binding_level->blocks, block);
1128 /* Set the BLOCK node for the innermost scope
1129 (the one we are currently in). */
1131 void
1132 set_block (block)
1133 tree block;
1135 current_binding_level->this_block = block;
1136 current_binding_level->names = chainon (current_binding_level->names,
1137 BLOCK_VARS (block));
1138 current_binding_level->blocks = chainon (current_binding_level->blocks,
1139 BLOCK_SUBBLOCKS (block));
1142 void
1143 push_label_level ()
1145 struct binding_level *newlevel;
1147 /* Reuse or create a struct for this binding level. */
1149 if (free_binding_level)
1151 newlevel = free_binding_level;
1152 free_binding_level = free_binding_level->level_chain;
1154 else
1156 newlevel = make_binding_level ();
1159 /* Add this level to the front of the chain (stack) of label levels. */
1161 newlevel->level_chain = label_level_chain;
1162 label_level_chain = newlevel;
1164 newlevel->names = named_labels;
1165 newlevel->shadowed = shadowed_labels;
1166 named_labels = 0;
1167 shadowed_labels = 0;
1170 void
1171 pop_label_level ()
1173 struct binding_level *level = label_level_chain;
1174 tree link, prev;
1176 /* Clear out the definitions of the declared labels in this level.
1177 Leave in the list any ordinary, non-declared labels. */
1178 for (link = named_labels, prev = 0; link;)
1180 if (C_DECLARED_LABEL_FLAG (TREE_VALUE (link)))
1182 if (DECL_SOURCE_LINE (TREE_VALUE (link)) == 0)
1184 error_with_decl (TREE_VALUE (link),
1185 "label `%s' used but not defined");
1186 /* Avoid crashing later. */
1187 define_label (input_filename, lineno,
1188 DECL_NAME (TREE_VALUE (link)));
1190 else if (warn_unused_label && !TREE_USED (TREE_VALUE (link)))
1191 warning_with_decl (TREE_VALUE (link),
1192 "label `%s' defined but not used");
1193 IDENTIFIER_LABEL_VALUE (DECL_NAME (TREE_VALUE (link))) = 0;
1195 /* Delete this element from the list. */
1196 link = TREE_CHAIN (link);
1197 if (prev)
1198 TREE_CHAIN (prev) = link;
1199 else
1200 named_labels = link;
1202 else
1204 prev = link;
1205 link = TREE_CHAIN (link);
1209 /* Bring back all the labels that were shadowed. */
1210 for (link = shadowed_labels; link; link = TREE_CHAIN (link))
1211 if (DECL_NAME (TREE_VALUE (link)) != 0)
1212 IDENTIFIER_LABEL_VALUE (DECL_NAME (TREE_VALUE (link)))
1213 = TREE_VALUE (link);
1215 named_labels = chainon (named_labels, level->names);
1216 shadowed_labels = level->shadowed;
1218 /* Pop the current level, and free the structure for reuse. */
1219 label_level_chain = label_level_chain->level_chain;
1220 level->level_chain = free_binding_level;
1221 free_binding_level = level;
1224 /* Push a definition or a declaration of struct, union or enum tag "name".
1225 "type" should be the type node.
1226 We assume that the tag "name" is not already defined.
1228 Note that the definition may really be just a forward reference.
1229 In that case, the TYPE_SIZE will be zero. */
1231 void
1232 pushtag (name, type)
1233 tree name, type;
1235 struct binding_level *b;
1237 /* Find the proper binding level for this type tag. */
1239 for (b = current_binding_level; b->tag_transparent; b = b->level_chain)
1240 continue;
1242 if (name)
1244 /* Record the identifier as the type's name if it has none. */
1246 if (TYPE_NAME (type) == 0)
1247 TYPE_NAME (type) = name;
1250 b->tags = tree_cons (name, type, b->tags);
1252 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1253 tagged type we just added to the current binding level. This fake
1254 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1255 to output a representation of a tagged type, and it also gives
1256 us a convenient place to record the "scope start" address for the
1257 tagged type. */
1259 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
1261 /* An approximation for now, so we can tell this is a function-scope tag.
1262 This will be updated in poplevel. */
1263 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
1266 /* Handle when a new declaration NEWDECL
1267 has the same name as an old one OLDDECL
1268 in the same binding contour.
1269 Prints an error message if appropriate.
1271 If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
1272 Otherwise, return 0.
1274 When DIFFERENT_BINDING_LEVEL is true, NEWDECL is an external declaration,
1275 and OLDDECL is in an outer binding level and should thus not be changed. */
1277 static int
1278 duplicate_decls (newdecl, olddecl, different_binding_level)
1279 tree newdecl, olddecl;
1280 int different_binding_level;
1282 int types_match = comptypes (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
1283 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1284 && DECL_INITIAL (newdecl) != 0);
1285 tree oldtype = TREE_TYPE (olddecl);
1286 tree newtype = TREE_TYPE (newdecl);
1287 int errmsg = 0;
1289 if (DECL_P (olddecl))
1291 if (TREE_CODE (newdecl) == FUNCTION_DECL
1292 && TREE_CODE (olddecl) == FUNCTION_DECL
1293 && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
1295 if (DECL_DECLARED_INLINE_P (newdecl)
1296 && DECL_UNINLINABLE (newdecl)
1297 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1298 /* Already warned elsewhere. */;
1299 else if (DECL_DECLARED_INLINE_P (olddecl)
1300 && DECL_UNINLINABLE (olddecl)
1301 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1302 /* Already warned. */;
1303 else if (DECL_DECLARED_INLINE_P (newdecl)
1304 && ! DECL_DECLARED_INLINE_P (olddecl)
1305 && DECL_UNINLINABLE (olddecl)
1306 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1308 warning_with_decl (newdecl,
1309 "function `%s' redeclared as inline");
1310 warning_with_decl (olddecl,
1311 "previous declaration of function `%s' with attribute noinline");
1313 else if (DECL_DECLARED_INLINE_P (olddecl)
1314 && DECL_UNINLINABLE (newdecl)
1315 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1317 warning_with_decl (newdecl,
1318 "function `%s' redeclared with attribute noinline");
1319 warning_with_decl (olddecl,
1320 "previous declaration of function `%s' was inline");
1324 DECL_ATTRIBUTES (newdecl)
1325 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
1328 if (TREE_CODE (newtype) == ERROR_MARK
1329 || TREE_CODE (oldtype) == ERROR_MARK)
1330 types_match = 0;
1332 /* New decl is completely inconsistent with the old one =>
1333 tell caller to replace the old one.
1334 This is always an error except in the case of shadowing a builtin. */
1335 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1337 if (TREE_CODE (olddecl) == FUNCTION_DECL
1338 && (DECL_BUILT_IN (olddecl)
1339 || DECL_BUILT_IN_NONANSI (olddecl)))
1341 /* If you declare a built-in or predefined function name as static,
1342 the old definition is overridden,
1343 but optionally warn this was a bad choice of name. */
1344 if (!TREE_PUBLIC (newdecl))
1346 if (!warn_shadow)
1348 else if (DECL_BUILT_IN (olddecl))
1349 warning_with_decl (newdecl, "shadowing built-in function `%s'");
1350 else
1351 warning_with_decl (newdecl, "shadowing library function `%s'");
1353 /* Likewise, if the built-in is not ansi, then programs can
1354 override it even globally without an error. */
1355 else if (! DECL_BUILT_IN (olddecl))
1356 warning_with_decl (newdecl,
1357 "library function `%s' declared as non-function");
1359 else if (DECL_BUILT_IN_NONANSI (olddecl))
1360 warning_with_decl (newdecl,
1361 "built-in function `%s' declared as non-function");
1362 else
1363 warning_with_decl (newdecl,
1364 "built-in function `%s' declared as non-function");
1366 else
1368 error_with_decl (newdecl, "`%s' redeclared as different kind of symbol");
1369 error_with_decl (olddecl, "previous declaration of `%s'");
1372 return 0;
1375 /* For real parm decl following a forward decl,
1376 return 1 so old decl will be reused. */
1377 if (types_match && TREE_CODE (newdecl) == PARM_DECL
1378 && TREE_ASM_WRITTEN (olddecl) && ! TREE_ASM_WRITTEN (newdecl))
1379 return 1;
1381 /* The new declaration is the same kind of object as the old one.
1382 The declarations may partially match. Print warnings if they don't
1383 match enough. Ultimately, copy most of the information from the new
1384 decl to the old one, and keep using the old one. */
1386 if (TREE_CODE (olddecl) == FUNCTION_DECL && DECL_BUILT_IN (olddecl))
1388 /* A function declaration for a built-in function. */
1389 if (!TREE_PUBLIC (newdecl))
1391 /* If you declare a built-in function name as static, the
1392 built-in definition is overridden,
1393 but optionally warn this was a bad choice of name. */
1394 if (warn_shadow)
1395 warning_with_decl (newdecl, "shadowing built-in function `%s'");
1396 /* Discard the old built-in function. */
1397 return 0;
1399 else if (!types_match)
1401 /* Accept the return type of the new declaration if same modes. */
1402 tree oldreturntype = TREE_TYPE (oldtype);
1403 tree newreturntype = TREE_TYPE (newtype);
1405 if (TYPE_MODE (oldreturntype) == TYPE_MODE (newreturntype))
1407 /* Function types may be shared, so we can't just modify
1408 the return type of olddecl's function type. */
1409 tree trytype
1410 = build_function_type (newreturntype,
1411 TYPE_ARG_TYPES (oldtype));
1412 trytype = build_type_attribute_variant (trytype,
1413 TYPE_ATTRIBUTES (oldtype));
1415 types_match = comptypes (newtype, trytype);
1416 if (types_match)
1417 oldtype = trytype;
1419 /* Accept harmless mismatch in first argument type also.
1420 This is for the ffs and fprintf builtins. */
1421 if (TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != 0
1422 && TYPE_ARG_TYPES (oldtype) != 0
1423 && TREE_VALUE (TYPE_ARG_TYPES (newtype)) != 0
1424 && TREE_VALUE (TYPE_ARG_TYPES (oldtype)) != 0
1425 && (TYPE_MODE (TREE_VALUE (TYPE_ARG_TYPES (newtype)))
1426 == TYPE_MODE (TREE_VALUE (TYPE_ARG_TYPES (oldtype)))))
1428 /* Function types may be shared, so we can't just modify
1429 the return type of olddecl's function type. */
1430 tree trytype
1431 = build_function_type (TREE_TYPE (oldtype),
1432 tree_cons (NULL_TREE,
1433 TREE_VALUE (TYPE_ARG_TYPES (newtype)),
1434 TREE_CHAIN (TYPE_ARG_TYPES (oldtype))));
1435 trytype = build_type_attribute_variant (trytype,
1436 TYPE_ATTRIBUTES (oldtype));
1438 types_match = comptypes (newtype, trytype);
1439 if (types_match)
1440 oldtype = trytype;
1442 if (! different_binding_level)
1443 TREE_TYPE (olddecl) = oldtype;
1445 else if (TYPE_ARG_TYPES (oldtype) == NULL
1446 && TYPE_ARG_TYPES (newtype) != NULL)
1448 /* For bcmp, bzero, fputs the builtin type has arguments not
1449 specified. Use the ones from the prototype so that type checking
1450 is done for them. */
1451 tree trytype
1452 = build_function_type (TREE_TYPE (oldtype),
1453 TYPE_ARG_TYPES (newtype));
1454 trytype = build_type_attribute_variant (trytype,
1455 TYPE_ATTRIBUTES (oldtype));
1457 oldtype = trytype;
1458 if (! different_binding_level)
1459 TREE_TYPE (olddecl) = oldtype;
1461 if (!types_match)
1463 /* If types don't match for a built-in, throw away the built-in. */
1464 warning_with_decl (newdecl, "conflicting types for built-in function `%s'");
1465 return 0;
1468 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1469 && DECL_SOURCE_LINE (olddecl) == 0)
1471 /* A function declaration for a predeclared function
1472 that isn't actually built in. */
1473 if (!TREE_PUBLIC (newdecl))
1475 /* If you declare it as static, the
1476 default definition is overridden. */
1477 return 0;
1479 else if (!types_match)
1481 /* If the types don't match, preserve volatility indication.
1482 Later on, we will discard everything else about the
1483 default declaration. */
1484 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1487 /* Permit char *foo () to match void *foo (...) if not pedantic,
1488 if one of them came from a system header file. */
1489 else if (!types_match
1490 && TREE_CODE (olddecl) == FUNCTION_DECL
1491 && TREE_CODE (newdecl) == FUNCTION_DECL
1492 && TREE_CODE (TREE_TYPE (oldtype)) == POINTER_TYPE
1493 && TREE_CODE (TREE_TYPE (newtype)) == POINTER_TYPE
1494 && (DECL_IN_SYSTEM_HEADER (olddecl)
1495 || DECL_IN_SYSTEM_HEADER (newdecl))
1496 && ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (newtype))) == void_type_node
1497 && TYPE_ARG_TYPES (oldtype) == 0
1498 && self_promoting_args_p (TYPE_ARG_TYPES (newtype))
1499 && TREE_TYPE (TREE_TYPE (oldtype)) == char_type_node)
1501 (TREE_TYPE (TREE_TYPE (newtype)) == char_type_node
1502 && TYPE_ARG_TYPES (newtype) == 0
1503 && self_promoting_args_p (TYPE_ARG_TYPES (oldtype))
1504 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (oldtype))) == void_type_node)))
1506 if (pedantic)
1507 pedwarn_with_decl (newdecl, "conflicting types for `%s'");
1508 /* Make sure we keep void * as ret type, not char *. */
1509 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (oldtype))) == void_type_node)
1510 TREE_TYPE (newdecl) = newtype = oldtype;
1512 /* Set DECL_IN_SYSTEM_HEADER, so that if we see another declaration
1513 we will come back here again. */
1514 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1516 else if (!types_match
1517 /* Permit char *foo (int, ...); followed by char *foo ();
1518 if not pedantic. */
1519 && ! (TREE_CODE (olddecl) == FUNCTION_DECL
1520 && ! pedantic
1521 /* Return types must still match. */
1522 && comptypes (TREE_TYPE (oldtype),
1523 TREE_TYPE (newtype))
1524 && TYPE_ARG_TYPES (newtype) == 0))
1526 error_with_decl (newdecl, "conflicting types for `%s'");
1527 /* Check for function type mismatch
1528 involving an empty arglist vs a nonempty one. */
1529 if (TREE_CODE (olddecl) == FUNCTION_DECL
1530 && comptypes (TREE_TYPE (oldtype),
1531 TREE_TYPE (newtype))
1532 && ((TYPE_ARG_TYPES (oldtype) == 0
1533 && DECL_INITIAL (olddecl) == 0)
1535 (TYPE_ARG_TYPES (newtype) == 0
1536 && DECL_INITIAL (newdecl) == 0)))
1538 /* Classify the problem further. */
1539 tree t = TYPE_ARG_TYPES (oldtype);
1540 if (t == 0)
1541 t = TYPE_ARG_TYPES (newtype);
1542 for (; t; t = TREE_CHAIN (t))
1544 tree type = TREE_VALUE (t);
1546 if (TREE_CHAIN (t) == 0
1547 && TYPE_MAIN_VARIANT (type) != void_type_node)
1549 error ("a parameter list with an ellipsis can't match an empty parameter name list declaration");
1550 break;
1553 if (simple_type_promotes_to (type) != NULL_TREE)
1555 error ("an argument type that has a default promotion can't match an empty parameter name list declaration");
1556 break;
1560 error_with_decl (olddecl, "previous declaration of `%s'");
1562 else
1564 errmsg = redeclaration_error_message (newdecl, olddecl);
1565 if (errmsg)
1567 switch (errmsg)
1569 case 1:
1570 error_with_decl (newdecl, "redefinition of `%s'");
1571 break;
1572 case 2:
1573 error_with_decl (newdecl, "redeclaration of `%s'");
1574 break;
1575 case 3:
1576 error_with_decl (newdecl, "conflicting declarations of `%s'");
1577 break;
1578 default:
1579 abort ();
1582 error_with_decl (olddecl,
1583 ((DECL_INITIAL (olddecl)
1584 && current_binding_level == global_binding_level)
1585 ? "`%s' previously defined here"
1586 : "`%s' previously declared here"));
1587 return 0;
1589 else if (TREE_CODE (newdecl) == TYPE_DECL
1590 && (DECL_IN_SYSTEM_HEADER (olddecl)
1591 || DECL_IN_SYSTEM_HEADER (newdecl)))
1593 warning_with_decl (newdecl, "redefinition of `%s'");
1594 warning_with_decl
1595 (olddecl,
1596 ((DECL_INITIAL (olddecl)
1597 && current_binding_level == global_binding_level)
1598 ? "`%s' previously defined here"
1599 : "`%s' previously declared here"));
1601 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1602 && DECL_INITIAL (olddecl) != 0
1603 && TYPE_ARG_TYPES (oldtype) == 0
1604 && TYPE_ARG_TYPES (newtype) != 0
1605 && TYPE_ACTUAL_ARG_TYPES (oldtype) != 0)
1607 tree type, parm;
1608 int nargs;
1609 /* Prototype decl follows defn w/o prototype. */
1611 for (parm = TYPE_ACTUAL_ARG_TYPES (oldtype),
1612 type = TYPE_ARG_TYPES (newtype),
1613 nargs = 1;
1615 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type), nargs++)
1617 if (TYPE_MAIN_VARIANT (TREE_VALUE (parm)) == void_type_node
1618 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
1620 warning_with_decl (newdecl, "prototype for `%s' follows");
1621 warning_with_decl (olddecl, "non-prototype definition here");
1622 break;
1624 if (TYPE_MAIN_VARIANT (TREE_VALUE (parm)) == void_type_node
1625 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
1627 error_with_decl (newdecl,
1628 "prototype for `%s' follows and number of arguments doesn't match");
1629 error_with_decl (olddecl, "non-prototype definition here");
1630 errmsg = 1;
1631 break;
1633 /* Type for passing arg must be consistent
1634 with that declared for the arg. */
1635 if (! comptypes (TREE_VALUE (parm), TREE_VALUE (type)))
1637 error_with_decl (newdecl,
1638 "prototype for `%s' follows and argument %d doesn't match",
1639 nargs);
1640 error_with_decl (olddecl, "non-prototype definition here");
1641 errmsg = 1;
1642 break;
1646 /* Warn about mismatches in various flags. */
1647 else
1649 /* Warn if function is now inline
1650 but was previously declared not inline and has been called. */
1651 if (TREE_CODE (olddecl) == FUNCTION_DECL
1652 && ! DECL_DECLARED_INLINE_P (olddecl)
1653 && DECL_DECLARED_INLINE_P (newdecl)
1654 && TREE_USED (olddecl))
1655 warning_with_decl (newdecl,
1656 "`%s' declared inline after being called");
1657 if (TREE_CODE (olddecl) == FUNCTION_DECL
1658 && ! DECL_DECLARED_INLINE_P (olddecl)
1659 && DECL_DECLARED_INLINE_P (newdecl)
1660 && DECL_INITIAL (olddecl) != 0)
1661 warning_with_decl (newdecl,
1662 "`%s' declared inline after its definition");
1664 /* If pedantic, warn when static declaration follows a non-static
1665 declaration. Otherwise, do so only for functions. */
1666 if ((pedantic || TREE_CODE (olddecl) == FUNCTION_DECL)
1667 && TREE_PUBLIC (olddecl)
1668 && !TREE_PUBLIC (newdecl))
1669 warning_with_decl (newdecl, "static declaration for `%s' follows non-static");
1671 /* If warn_traditional, warn when a non-static function
1672 declaration follows a static one. */
1673 if (warn_traditional && !in_system_header
1674 && TREE_CODE (olddecl) == FUNCTION_DECL
1675 && !TREE_PUBLIC (olddecl)
1676 && TREE_PUBLIC (newdecl))
1677 warning_with_decl (newdecl, "non-static declaration for `%s' follows static");
1679 /* Warn when const declaration follows a non-const
1680 declaration, but not for functions. */
1681 if (TREE_CODE (olddecl) != FUNCTION_DECL
1682 && !TREE_READONLY (olddecl)
1683 && TREE_READONLY (newdecl))
1684 warning_with_decl (newdecl, "const declaration for `%s' follows non-const");
1685 /* These bits are logically part of the type, for variables.
1686 But not for functions
1687 (where qualifiers are not valid ANSI anyway). */
1688 else if (pedantic && TREE_CODE (olddecl) != FUNCTION_DECL
1689 && (TREE_READONLY (newdecl) != TREE_READONLY (olddecl)
1690 || TREE_THIS_VOLATILE (newdecl) != TREE_THIS_VOLATILE (olddecl)))
1691 pedwarn_with_decl (newdecl, "type qualifiers for `%s' conflict with previous decl");
1695 /* Optionally warn about more than one declaration for the same name. */
1696 if (errmsg == 0 && warn_redundant_decls && DECL_SOURCE_LINE (olddecl) != 0
1697 /* Don't warn about a function declaration
1698 followed by a definition. */
1699 && !(TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl) != 0
1700 && DECL_INITIAL (olddecl) == 0)
1701 /* Don't warn about extern decl followed by (tentative) definition. */
1702 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl)))
1704 warning_with_decl (newdecl, "redundant redeclaration of `%s' in same scope");
1705 warning_with_decl (olddecl, "previous declaration of `%s'");
1708 /* Copy all the DECL_... slots specified in the new decl
1709 except for any that we copy here from the old type.
1711 Past this point, we don't change OLDTYPE and NEWTYPE
1712 even if we change the types of NEWDECL and OLDDECL. */
1714 if (types_match)
1716 /* When copying info to olddecl, we store into write_olddecl
1717 instead. This allows us to avoid modifying olddecl when
1718 different_binding_level is true. */
1719 tree write_olddecl = different_binding_level ? newdecl : olddecl;
1721 /* Merge the data types specified in the two decls. */
1722 if (TREE_CODE (newdecl) != FUNCTION_DECL || !DECL_BUILT_IN (olddecl))
1724 if (different_binding_level)
1726 if (TYPE_ARG_TYPES (oldtype) != 0
1727 && TYPE_ARG_TYPES (newtype) == 0)
1728 TREE_TYPE (newdecl) = common_type (newtype, oldtype);
1729 else
1730 TREE_TYPE (newdecl)
1731 = build_type_attribute_variant
1732 (newtype,
1733 merge_attributes (TYPE_ATTRIBUTES (newtype),
1734 TYPE_ATTRIBUTES (oldtype)));
1736 else
1737 TREE_TYPE (newdecl)
1738 = TREE_TYPE (olddecl)
1739 = common_type (newtype, oldtype);
1742 /* Lay the type out, unless already done. */
1743 if (oldtype != TREE_TYPE (newdecl))
1745 if (TREE_TYPE (newdecl) != error_mark_node)
1746 layout_type (TREE_TYPE (newdecl));
1747 if (TREE_CODE (newdecl) != FUNCTION_DECL
1748 && TREE_CODE (newdecl) != TYPE_DECL
1749 && TREE_CODE (newdecl) != CONST_DECL)
1750 layout_decl (newdecl, 0);
1752 else
1754 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1755 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1756 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1757 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1758 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1759 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1761 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1762 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1766 /* Keep the old rtl since we can safely use it. */
1767 COPY_DECL_RTL (olddecl, newdecl);
1769 /* Merge the type qualifiers. */
1770 if (TREE_CODE (olddecl) == FUNCTION_DECL
1771 && DECL_BUILT_IN_NONANSI (olddecl) && TREE_THIS_VOLATILE (olddecl)
1772 && ! TREE_THIS_VOLATILE (newdecl))
1773 TREE_THIS_VOLATILE (write_olddecl) = 0;
1775 if (TREE_READONLY (newdecl))
1776 TREE_READONLY (write_olddecl) = 1;
1778 if (TREE_THIS_VOLATILE (newdecl))
1780 TREE_THIS_VOLATILE (write_olddecl) = 1;
1781 if (TREE_CODE (newdecl) == VAR_DECL
1782 /* If an automatic variable is re-declared in the same
1783 function scope, but the old declaration was not
1784 volatile, make_var_volatile() would crash because the
1785 variable would have been assigned to a pseudo, not a
1786 MEM. Since this duplicate declaration is invalid
1787 anyway, we just skip the call. */
1788 && errmsg == 0)
1789 make_var_volatile (newdecl);
1792 /* Keep source location of definition rather than declaration. */
1793 /* When called with different_binding_level set, keep the old
1794 information so that meaningful diagnostics can be given. */
1795 if (DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0
1796 && ! different_binding_level)
1798 DECL_SOURCE_LINE (newdecl) = DECL_SOURCE_LINE (olddecl);
1799 DECL_SOURCE_FILE (newdecl) = DECL_SOURCE_FILE (olddecl);
1802 /* Merge the unused-warning information. */
1803 if (DECL_IN_SYSTEM_HEADER (olddecl))
1804 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1805 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1806 DECL_IN_SYSTEM_HEADER (write_olddecl) = 1;
1808 /* Merge the initialization information. */
1809 /* When called with different_binding_level set, don't copy over
1810 DECL_INITIAL, so that we don't accidentally change function
1811 declarations into function definitions. */
1812 if (DECL_INITIAL (newdecl) == 0 && ! different_binding_level)
1813 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1815 /* Merge the section attribute.
1816 We want to issue an error if the sections conflict but that must be
1817 done later in decl_attributes since we are called before attributes
1818 are assigned. */
1819 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1820 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1822 /* Copy the assembler name.
1823 Currently, it can only be defined in the prototype. */
1824 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1826 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1828 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1829 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1830 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1831 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1832 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1835 /* If cannot merge, then use the new type and qualifiers,
1836 and don't preserve the old rtl. */
1837 else if (! different_binding_level)
1839 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1840 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
1841 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
1842 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
1845 /* Merge the storage class information. */
1846 DECL_WEAK (newdecl) |= DECL_WEAK (olddecl);
1847 /* For functions, static overrides non-static. */
1848 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1850 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1851 /* This is since we don't automatically
1852 copy the attributes of NEWDECL into OLDDECL. */
1853 /* No need to worry about different_binding_level here because
1854 then TREE_PUBLIC (newdecl) was true. */
1855 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1856 /* If this clears `static', clear it in the identifier too. */
1857 if (! TREE_PUBLIC (olddecl))
1858 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1860 if (DECL_EXTERNAL (newdecl))
1862 if (! different_binding_level)
1864 /* Don't mess with these flags on local externs; they remain
1865 external even if there's a declaration at file scope which
1866 isn't. */
1867 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1868 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1870 /* An extern decl does not override previous storage class. */
1871 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1872 if (! DECL_EXTERNAL (newdecl))
1873 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1875 else
1877 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1878 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1881 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1883 /* If we're redefining a function previously defined as extern
1884 inline, make sure we emit debug info for the inline before we
1885 throw it away, in case it was inlined into a function that hasn't
1886 been written out yet. */
1887 if (new_is_definition && DECL_INITIAL (olddecl) && TREE_USED (olddecl))
1889 (*debug_hooks->outlining_inline_function) (olddecl);
1891 /* The new defn must not be inline. */
1892 DECL_INLINE (newdecl) = 0;
1893 DECL_UNINLINABLE (newdecl) = 1;
1895 else
1897 /* If either decl says `inline', this fn is inline,
1898 unless its definition was passed already. */
1899 if (DECL_DECLARED_INLINE_P (newdecl)
1900 || DECL_DECLARED_INLINE_P (olddecl))
1901 DECL_DECLARED_INLINE_P (newdecl) = 1;
1903 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1904 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1907 if (DECL_BUILT_IN (olddecl))
1909 /* Get rid of any built-in function if new arg types don't match it
1910 or if we have a function definition. */
1911 if (! types_match || new_is_definition)
1913 if (! different_binding_level)
1915 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1916 DECL_BUILT_IN_CLASS (olddecl) = NOT_BUILT_IN;
1919 else
1921 /* If redeclaring a builtin function, and not a definition,
1922 it stays built in. */
1923 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1924 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1928 /* Also preserve various other info from the definition. */
1929 if (! new_is_definition)
1931 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1932 /* When called with different_binding_level set, don't copy over
1933 DECL_INITIAL, so that we don't accidentally change function
1934 declarations into function definitions. */
1935 if (! different_binding_level)
1936 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1937 DECL_SAVED_INSNS (newdecl) = DECL_SAVED_INSNS (olddecl);
1938 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1939 DECL_NUM_STMTS (newdecl) = DECL_NUM_STMTS (olddecl);
1940 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1942 /* Set DECL_INLINE on the declaration if we've got a body
1943 from which to instantiate. */
1944 if (DECL_INLINE (olddecl) && ! DECL_UNINLINABLE (newdecl))
1946 DECL_INLINE (newdecl) = 1;
1947 DECL_ABSTRACT_ORIGIN (newdecl)
1948 = (different_binding_level
1949 ? DECL_ORIGIN (olddecl)
1950 : DECL_ABSTRACT_ORIGIN (olddecl));
1953 else
1955 /* If a previous declaration said inline, mark the
1956 definition as inlinable. */
1957 if (DECL_DECLARED_INLINE_P (newdecl)
1958 && ! DECL_UNINLINABLE (newdecl))
1959 DECL_INLINE (newdecl) = 1;
1962 if (different_binding_level)
1963 return 0;
1965 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1966 But preserve OLDDECL's DECL_UID. */
1968 unsigned olddecl_uid = DECL_UID (olddecl);
1970 memcpy ((char *) olddecl + sizeof (struct tree_common),
1971 (char *) newdecl + sizeof (struct tree_common),
1972 sizeof (struct tree_decl) - sizeof (struct tree_common));
1973 DECL_UID (olddecl) = olddecl_uid;
1976 /* NEWDECL contains the merged attribute lists.
1977 Update OLDDECL to be the same. */
1978 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
1980 return 1;
1983 /* Check whether decl-node X shadows an existing declaration.
1984 OLDLOCAL is the old IDENTIFIER_LOCAL_VALUE of the DECL_NAME of X,
1985 which might be a NULL_TREE. */
1986 static void
1987 warn_if_shadowing (x, oldlocal)
1988 tree x, oldlocal;
1990 tree name;
1992 if (DECL_EXTERNAL (x))
1993 return;
1995 name = DECL_NAME (x);
1997 /* Warn if shadowing an argument at the top level of the body. */
1998 if (oldlocal != 0
1999 /* This warning doesn't apply to the parms of a nested fcn. */
2000 && ! current_binding_level->parm_flag
2001 /* Check that this is one level down from the parms. */
2002 && current_binding_level->level_chain->parm_flag
2003 /* Check that the decl being shadowed
2004 comes from the parm level, one level up. */
2005 && chain_member (oldlocal, current_binding_level->level_chain->names))
2007 if (TREE_CODE (oldlocal) == PARM_DECL)
2008 pedwarn ("declaration of `%s' shadows a parameter",
2009 IDENTIFIER_POINTER (name));
2010 else
2011 pedwarn ("declaration of `%s' shadows a symbol from the parameter list",
2012 IDENTIFIER_POINTER (name));
2014 /* Maybe warn if shadowing something else. */
2015 else if (warn_shadow
2016 /* No shadow warnings for internally generated vars. */
2017 && DECL_SOURCE_LINE (x) != 0
2018 /* No shadow warnings for vars made for inlining. */
2019 && ! DECL_FROM_INLINE (x))
2021 if (TREE_CODE (x) == PARM_DECL
2022 && current_binding_level->level_chain->parm_flag)
2023 /* Don't warn about the parm names in function declarator
2024 within a function declarator.
2025 It would be nice to avoid warning in any function
2026 declarator in a declaration, as opposed to a definition,
2027 but there is no way to tell it's not a definition. */
2029 else if (oldlocal)
2031 if (TREE_CODE (oldlocal) == PARM_DECL)
2032 shadow_warning ("a parameter", name, oldlocal);
2033 else
2034 shadow_warning ("a previous local", name, oldlocal);
2036 else if (IDENTIFIER_GLOBAL_VALUE (name) != 0
2037 && IDENTIFIER_GLOBAL_VALUE (name) != error_mark_node)
2038 shadow_warning ("a global declaration", name,
2039 IDENTIFIER_GLOBAL_VALUE (name));
2043 /* Record a decl-node X as belonging to the current lexical scope.
2044 Check for errors (such as an incompatible declaration for the same
2045 name already seen in the same scope).
2047 Returns either X or an old decl for the same name.
2048 If an old decl is returned, it may have been smashed
2049 to agree with what X says. */
2051 tree
2052 pushdecl (x)
2053 tree x;
2055 tree t;
2056 tree name = DECL_NAME (x);
2057 struct binding_level *b = current_binding_level;
2059 /* Functions need the lang_decl data. */
2060 if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_LANG_SPECIFIC (x))
2061 DECL_LANG_SPECIFIC (x) = (struct lang_decl *)
2062 ggc_alloc_cleared (sizeof (struct lang_decl));
2064 DECL_CONTEXT (x) = current_function_decl;
2065 /* A local extern declaration for a function doesn't constitute nesting.
2066 A local auto declaration does, since it's a forward decl
2067 for a nested function coming later. */
2068 if ((TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
2069 && DECL_INITIAL (x) == 0 && DECL_EXTERNAL (x))
2070 DECL_CONTEXT (x) = 0;
2072 if (name)
2074 int different_binding_level = 0;
2076 if (warn_nested_externs
2077 && DECL_EXTERNAL (x)
2078 && b != global_binding_level
2079 && x != IDENTIFIER_IMPLICIT_DECL (name)
2080 /* No error messages for __FUNCTION__ and __PRETTY_FUNCTION__. */
2081 && !DECL_IN_SYSTEM_HEADER (x))
2082 warning ("nested extern declaration of `%s'",
2083 IDENTIFIER_POINTER (name));
2085 t = lookup_name_current_level (name);
2086 if (! t && DECL_EXTERNAL (x) && TREE_PUBLIC (x))
2088 t = IDENTIFIER_GLOBAL_VALUE (name);
2089 /* Type decls at global scope don't conflict with externs declared
2090 inside lexical blocks. */
2091 if (! t || TREE_CODE (t) == TYPE_DECL)
2092 /* If there's no visible global declaration, try for an
2093 invisible one. */
2094 t = IDENTIFIER_LIMBO_VALUE (name);
2095 different_binding_level = 1;
2097 if (t != 0 && t == error_mark_node)
2098 /* error_mark_node is 0 for a while during initialization! */
2100 t = 0;
2101 error_with_decl (x, "`%s' used prior to declaration");
2104 /* If this decl is `static' and an implicit decl was seen previously,
2105 warn. */
2106 if (TREE_PUBLIC (name)
2107 /* Don't test for DECL_EXTERNAL, because grokdeclarator
2108 sets this for all functions. */
2109 && ! TREE_PUBLIC (x)
2110 && (TREE_CODE (x) == FUNCTION_DECL || b == global_binding_level)
2111 /* We used to warn also for explicit extern followed by static,
2112 but sometimes you need to do it that way. */
2113 && IDENTIFIER_IMPLICIT_DECL (name) != 0)
2115 pedwarn ("`%s' was declared implicitly `extern' and later `static'",
2116 IDENTIFIER_POINTER (name));
2117 pedwarn_with_file_and_line
2118 (DECL_SOURCE_FILE (IDENTIFIER_IMPLICIT_DECL (name)),
2119 DECL_SOURCE_LINE (IDENTIFIER_IMPLICIT_DECL (name)),
2120 "previous declaration of `%s'",
2121 IDENTIFIER_POINTER (name));
2122 TREE_THIS_VOLATILE (name) = 1;
2125 if (t != 0 && duplicate_decls (x, t, different_binding_level))
2127 if (TREE_CODE (t) == PARM_DECL)
2129 /* Don't allow more than one "real" duplicate
2130 of a forward parm decl. */
2131 TREE_ASM_WRITTEN (t) = TREE_ASM_WRITTEN (x);
2132 return t;
2134 return t;
2137 /* If we are processing a typedef statement, generate a whole new
2138 ..._TYPE node (which will be just an variant of the existing
2139 ..._TYPE node with identical properties) and then install the
2140 TYPE_DECL node generated to represent the typedef name as the
2141 TYPE_NAME of this brand new (duplicate) ..._TYPE node.
2143 The whole point here is to end up with a situation where each
2144 and every ..._TYPE node the compiler creates will be uniquely
2145 associated with AT MOST one node representing a typedef name.
2146 This way, even though the compiler substitutes corresponding
2147 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
2148 early on, later parts of the compiler can always do the reverse
2149 translation and get back the corresponding typedef name. For
2150 example, given:
2152 typedef struct S MY_TYPE;
2153 MY_TYPE object;
2155 Later parts of the compiler might only know that `object' was of
2156 type `struct S' if it were not for code just below. With this
2157 code however, later parts of the compiler see something like:
2159 struct S' == struct S
2160 typedef struct S' MY_TYPE;
2161 struct S' object;
2163 And they can then deduce (from the node for type struct S') that
2164 the original object declaration was:
2166 MY_TYPE object;
2168 Being able to do this is important for proper support of protoize,
2169 and also for generating precise symbolic debugging information
2170 which takes full account of the programmer's (typedef) vocabulary.
2172 Obviously, we don't want to generate a duplicate ..._TYPE node if
2173 the TYPE_DECL node that we are now processing really represents a
2174 standard built-in type.
2176 Since all standard types are effectively declared at line zero
2177 in the source file, we can easily check to see if we are working
2178 on a standard type by checking the current value of lineno. */
2180 if (TREE_CODE (x) == TYPE_DECL)
2182 if (DECL_SOURCE_LINE (x) == 0)
2184 if (TYPE_NAME (TREE_TYPE (x)) == 0)
2185 TYPE_NAME (TREE_TYPE (x)) = x;
2187 else if (TREE_TYPE (x) != error_mark_node
2188 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
2190 tree tt = TREE_TYPE (x);
2191 DECL_ORIGINAL_TYPE (x) = tt;
2192 tt = build_type_copy (tt);
2193 TYPE_NAME (tt) = x;
2194 TREE_USED (tt) = TREE_USED (x);
2195 TREE_TYPE (x) = tt;
2199 /* Multiple external decls of the same identifier ought to match.
2200 We get warnings about inline functions where they are defined.
2201 Avoid duplicate warnings where they are used. */
2202 if (TREE_PUBLIC (x)
2203 && ! (TREE_CODE (x) == FUNCTION_DECL && DECL_INLINE (x)))
2205 tree decl;
2207 if (IDENTIFIER_LIMBO_VALUE (name) != 0)
2208 /* Decls in limbo are always extern, so no need to check that. */
2209 decl = IDENTIFIER_LIMBO_VALUE (name);
2210 else
2211 decl = 0;
2213 if (decl && ! comptypes (TREE_TYPE (x), TREE_TYPE (decl))
2214 /* If old decl is built-in, we already warned if we should. */
2215 && !DECL_BUILT_IN (decl))
2217 pedwarn_with_decl (x,
2218 "type mismatch with previous external decl");
2219 pedwarn_with_decl (decl, "previous external decl of `%s'");
2223 /* If a function has had an implicit declaration, and then is defined,
2224 make sure they are compatible. */
2226 if (IDENTIFIER_IMPLICIT_DECL (name) != 0
2227 && IDENTIFIER_GLOBAL_VALUE (name) == 0
2228 && TREE_CODE (x) == FUNCTION_DECL
2229 && ! comptypes (TREE_TYPE (x),
2230 TREE_TYPE (IDENTIFIER_IMPLICIT_DECL (name))))
2232 warning_with_decl (x, "type mismatch with previous implicit declaration");
2233 warning_with_decl (IDENTIFIER_IMPLICIT_DECL (name),
2234 "previous implicit declaration of `%s'");
2237 /* This name is new in its binding level.
2238 Install the new declaration and return it. */
2239 if (b == global_binding_level)
2241 /* Install a global value. */
2243 /* If the first global decl has external linkage,
2244 warn if we later see static one. */
2245 if (IDENTIFIER_GLOBAL_VALUE (name) == 0 && TREE_PUBLIC (x))
2246 TREE_PUBLIC (name) = 1;
2248 IDENTIFIER_GLOBAL_VALUE (name) = x;
2250 /* We no longer care about any previous block level declarations. */
2251 IDENTIFIER_LIMBO_VALUE (name) = 0;
2253 /* Don't forget if the function was used via an implicit decl. */
2254 if (IDENTIFIER_IMPLICIT_DECL (name)
2255 && TREE_USED (IDENTIFIER_IMPLICIT_DECL (name)))
2256 TREE_USED (x) = 1, TREE_USED (name) = 1;
2258 /* Don't forget if its address was taken in that way. */
2259 if (IDENTIFIER_IMPLICIT_DECL (name)
2260 && TREE_ADDRESSABLE (IDENTIFIER_IMPLICIT_DECL (name)))
2261 TREE_ADDRESSABLE (x) = 1;
2263 /* Warn about mismatches against previous implicit decl. */
2264 if (IDENTIFIER_IMPLICIT_DECL (name) != 0
2265 /* If this real decl matches the implicit, don't complain. */
2266 && ! (TREE_CODE (x) == FUNCTION_DECL
2267 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (x)))
2268 == integer_type_node)))
2269 pedwarn ("`%s' was previously implicitly declared to return `int'",
2270 IDENTIFIER_POINTER (name));
2272 /* If this decl is `static' and an `extern' was seen previously,
2273 that is erroneous. */
2274 if (TREE_PUBLIC (name)
2275 && ! TREE_PUBLIC (x) && ! DECL_EXTERNAL (x))
2277 /* Okay to redeclare an ANSI built-in as static. */
2278 if (t != 0 && DECL_BUILT_IN (t))
2280 /* Okay to declare a non-ANSI built-in as anything. */
2281 else if (t != 0 && DECL_BUILT_IN_NONANSI (t))
2283 /* Okay to have global type decl after an earlier extern
2284 declaration inside a lexical block. */
2285 else if (TREE_CODE (x) == TYPE_DECL)
2287 else if (IDENTIFIER_IMPLICIT_DECL (name))
2289 if (! TREE_THIS_VOLATILE (name))
2290 pedwarn ("`%s' was declared implicitly `extern' and later `static'",
2291 IDENTIFIER_POINTER (name));
2293 else
2294 pedwarn ("`%s' was declared `extern' and later `static'",
2295 IDENTIFIER_POINTER (name));
2298 else
2300 /* Here to install a non-global value. */
2301 tree oldlocal = IDENTIFIER_LOCAL_VALUE (name);
2302 tree oldglobal = IDENTIFIER_GLOBAL_VALUE (name);
2304 IDENTIFIER_LOCAL_VALUE (name) = x;
2306 /* If this is an extern function declaration, see if we
2307 have a global definition or declaration for the function. */
2308 if (oldlocal == 0
2309 && oldglobal != 0
2310 && TREE_CODE (x) == FUNCTION_DECL
2311 && TREE_CODE (oldglobal) == FUNCTION_DECL
2312 && DECL_EXTERNAL (x)
2313 && ! DECL_DECLARED_INLINE_P (x))
2315 /* We have one. Their types must agree. */
2316 if (! comptypes (TREE_TYPE (x),
2317 TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (name))))
2318 pedwarn_with_decl (x, "extern declaration of `%s' doesn't match global one");
2319 else
2321 /* Inner extern decl is inline if global one is.
2322 Copy enough to really inline it. */
2323 if (DECL_DECLARED_INLINE_P (oldglobal))
2325 DECL_DECLARED_INLINE_P (x)
2326 = DECL_DECLARED_INLINE_P (oldglobal);
2327 DECL_INLINE (x) = DECL_INLINE (oldglobal);
2328 DECL_INITIAL (x) = (current_function_decl == oldglobal
2329 ? 0 : DECL_INITIAL (oldglobal));
2330 DECL_SAVED_INSNS (x) = DECL_SAVED_INSNS (oldglobal);
2331 DECL_NUM_STMTS (x) = DECL_NUM_STMTS (oldglobal);
2332 DECL_ARGUMENTS (x) = DECL_ARGUMENTS (oldglobal);
2333 DECL_RESULT (x) = DECL_RESULT (oldglobal);
2334 TREE_ASM_WRITTEN (x) = TREE_ASM_WRITTEN (oldglobal);
2335 DECL_ABSTRACT_ORIGIN (x)
2336 = DECL_ABSTRACT_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
2353 /* This case is probably sometimes the right thing to do. */
2354 /* If we have a local external declaration,
2355 then any file-scope declaration should not
2356 have been static. */
2357 if (oldlocal == 0 && oldglobal != 0
2358 && !TREE_PUBLIC (oldglobal)
2359 && DECL_EXTERNAL (x) && TREE_PUBLIC (x))
2360 warning ("`%s' locally external but globally static",
2361 IDENTIFIER_POINTER (name));
2362 #endif
2364 /* If we have a local external declaration,
2365 and no file-scope declaration has yet been seen,
2366 then if we later have a file-scope decl it must not be static. */
2367 if (oldlocal == 0
2368 && DECL_EXTERNAL (x)
2369 && TREE_PUBLIC (x))
2371 if (oldglobal == 0)
2372 TREE_PUBLIC (name) = 1;
2374 /* Save this decl, so that we can do type checking against
2375 other decls after it falls out of scope.
2377 Only save it once. This prevents temporary decls created in
2378 expand_inline_function from being used here, since this
2379 will have been set when the inline function was parsed.
2380 It also helps give slightly better warnings. */
2381 if (IDENTIFIER_LIMBO_VALUE (name) == 0)
2382 IDENTIFIER_LIMBO_VALUE (name) = x;
2385 warn_if_shadowing (x, oldlocal);
2387 /* If storing a local value, there may already be one (inherited).
2388 If so, record it for restoration when this binding level ends. */
2389 if (oldlocal != 0)
2390 b->shadowed = tree_cons (name, oldlocal, b->shadowed);
2393 /* Keep count of variables in this level with incomplete type.
2394 If the input is erroneous, we can have error_mark in the type
2395 slot (e.g. "f(void a, ...)") - that doesn't count as an
2396 incomplete type. */
2397 if (TREE_TYPE (x) != error_mark_node
2398 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2400 tree element = TREE_TYPE (x);
2402 while (TREE_CODE (element) == ARRAY_TYPE)
2403 element = TREE_TYPE (element);
2404 if (TREE_CODE (element) == RECORD_TYPE
2405 || TREE_CODE (element) == UNION_TYPE)
2406 ++b->n_incomplete;
2410 /* Put decls on list in reverse order.
2411 We will reverse them later if necessary. */
2412 TREE_CHAIN (x) = b->names;
2413 b->names = x;
2415 return x;
2418 /* Like pushdecl, only it places X in GLOBAL_BINDING_LEVEL, if appropriate. */
2420 tree
2421 pushdecl_top_level (x)
2422 tree x;
2424 tree t;
2425 struct binding_level *b = current_binding_level;
2427 current_binding_level = global_binding_level;
2428 t = pushdecl (x);
2429 current_binding_level = b;
2430 return t;
2433 /* Generate an implicit declaration for identifier FUNCTIONID
2434 as a function of type int (). Print a warning if appropriate. */
2436 tree
2437 implicitly_declare (functionid)
2438 tree functionid;
2440 tree decl;
2441 int traditional_warning = 0;
2442 /* Only one "implicit declaration" warning per identifier. */
2443 int implicit_warning;
2445 /* We used to reuse an old implicit decl here,
2446 but this loses with inline functions because it can clobber
2447 the saved decl chains. */
2448 #if 0
2449 if (IDENTIFIER_IMPLICIT_DECL (functionid) != 0)
2450 decl = IDENTIFIER_IMPLICIT_DECL (functionid);
2451 else
2452 #endif
2453 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2455 /* Warn of implicit decl following explicit local extern decl.
2456 This is probably a program designed for traditional C. */
2457 if (TREE_PUBLIC (functionid) && IDENTIFIER_GLOBAL_VALUE (functionid) == 0)
2458 traditional_warning = 1;
2460 /* Warn once of an implicit declaration. */
2461 implicit_warning = (IDENTIFIER_IMPLICIT_DECL (functionid) == 0);
2463 DECL_EXTERNAL (decl) = 1;
2464 TREE_PUBLIC (decl) = 1;
2466 /* Record that we have an implicit decl and this is it. */
2467 IDENTIFIER_IMPLICIT_DECL (functionid) = decl;
2469 /* ANSI standard says implicit declarations are in the innermost block.
2470 So we record the decl in the standard fashion. */
2471 pushdecl (decl);
2473 /* This is a no-op in c-lang.c or something real in objc-actions.c. */
2474 maybe_objc_check_decl (decl);
2476 rest_of_decl_compilation (decl, NULL, 0, 0);
2478 if (implicit_warning)
2479 implicit_decl_warning (functionid);
2480 else if (warn_traditional && traditional_warning)
2481 warning ("function `%s' was previously declared within a block",
2482 IDENTIFIER_POINTER (functionid));
2484 /* Write a record describing this implicit function declaration to the
2485 prototypes file (if requested). */
2487 gen_aux_info_record (decl, 0, 1, 0);
2489 /* Possibly apply some default attributes to this implicit declaration. */
2490 decl_attributes (&decl, NULL_TREE, 0);
2492 return decl;
2495 void
2496 implicit_decl_warning (id)
2497 tree id;
2499 const char *name = IDENTIFIER_POINTER (id);
2500 if (mesg_implicit_function_declaration == 2)
2501 error ("implicit declaration of function `%s'", name);
2502 else if (mesg_implicit_function_declaration == 1)
2503 warning ("implicit declaration of function `%s'", name);
2506 /* Return zero if the declaration NEWDECL is valid
2507 when the declaration OLDDECL (assumed to be for the same name)
2508 has already been seen.
2509 Otherwise return 1 if NEWDECL is a redefinition, 2 if it is a redeclaration,
2510 and 3 if it is a conflicting declaration. */
2512 static int
2513 redeclaration_error_message (newdecl, olddecl)
2514 tree newdecl, olddecl;
2516 if (TREE_CODE (newdecl) == TYPE_DECL)
2518 /* Do not complain about type redeclarations where at least one
2519 declaration was in a system header. */
2520 if (DECL_IN_SYSTEM_HEADER (olddecl) || DECL_IN_SYSTEM_HEADER (newdecl))
2521 return 0;
2522 return 1;
2524 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2526 /* Declarations of functions can insist on internal linkage
2527 but they can't be inconsistent with internal linkage,
2528 so there can be no error on that account.
2529 However defining the same name twice is no good. */
2530 if (DECL_INITIAL (olddecl) != 0 && DECL_INITIAL (newdecl) != 0
2531 /* However, defining once as extern inline and a second
2532 time in another way is ok. */
2533 && ! (DECL_DECLARED_INLINE_P (olddecl) && DECL_EXTERNAL (olddecl)
2534 && ! (DECL_DECLARED_INLINE_P (newdecl)
2535 && DECL_EXTERNAL (newdecl))))
2536 return 1;
2537 return 0;
2539 else if (DECL_CONTEXT (newdecl) == NULL_TREE)
2541 /* Objects declared at top level: */
2542 /* If at least one is a reference, it's ok. */
2543 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
2544 return 0;
2545 /* Reject two definitions. */
2546 if (DECL_INITIAL (olddecl) != 0 && DECL_INITIAL (newdecl) != 0)
2547 return 1;
2548 /* Now we have two tentative defs, or one tentative and one real def. */
2549 /* Insist that the linkage match. */
2550 if (TREE_PUBLIC (olddecl) != TREE_PUBLIC (newdecl))
2551 return 3;
2552 return 0;
2554 else if (current_binding_level->parm_flag
2555 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2556 return 0;
2557 else
2559 /* Newdecl has block scope. If olddecl has block scope also, then
2560 reject two definitions, and reject a definition together with an
2561 external reference. Otherwise, it is OK, because newdecl must
2562 be an extern reference to olddecl. */
2563 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl))
2564 && DECL_CONTEXT (newdecl) == DECL_CONTEXT (olddecl))
2565 return 2;
2566 return 0;
2570 /* Get the LABEL_DECL corresponding to identifier ID as a label.
2571 Create one if none exists so far for the current function.
2572 This function is called for both label definitions and label references. */
2574 tree
2575 lookup_label (id)
2576 tree id;
2578 tree decl = IDENTIFIER_LABEL_VALUE (id);
2580 if (current_function_decl == 0)
2582 error ("label %s referenced outside of any function",
2583 IDENTIFIER_POINTER (id));
2584 return 0;
2587 /* Use a label already defined or ref'd with this name. */
2588 if (decl != 0)
2590 /* But not if it is inherited and wasn't declared to be inheritable. */
2591 if (DECL_CONTEXT (decl) != current_function_decl
2592 && ! C_DECLARED_LABEL_FLAG (decl))
2593 return shadow_label (id);
2594 return decl;
2597 decl = build_decl (LABEL_DECL, id, void_type_node);
2599 /* A label not explicitly declared must be local to where it's ref'd. */
2600 DECL_CONTEXT (decl) = current_function_decl;
2602 DECL_MODE (decl) = VOIDmode;
2604 /* Say where one reference is to the label,
2605 for the sake of the error if it is not defined. */
2606 DECL_SOURCE_LINE (decl) = lineno;
2607 DECL_SOURCE_FILE (decl) = input_filename;
2609 IDENTIFIER_LABEL_VALUE (id) = decl;
2611 named_labels = tree_cons (NULL_TREE, decl, named_labels);
2613 return decl;
2616 /* Make a label named NAME in the current function,
2617 shadowing silently any that may be inherited from containing functions
2618 or containing scopes.
2620 Note that valid use, if the label being shadowed
2621 comes from another scope in the same function,
2622 requires calling declare_nonlocal_label right away. */
2624 tree
2625 shadow_label (name)
2626 tree name;
2628 tree decl = IDENTIFIER_LABEL_VALUE (name);
2630 if (decl != 0)
2632 tree dup;
2634 /* Check to make sure that the label hasn't already been declared
2635 at this label scope */
2636 for (dup = named_labels; dup; dup = TREE_CHAIN (dup))
2637 if (TREE_VALUE (dup) == decl)
2639 error ("duplicate label declaration `%s'",
2640 IDENTIFIER_POINTER (name));
2641 error_with_decl (TREE_VALUE (dup),
2642 "this is a previous declaration");
2643 /* Just use the previous declaration. */
2644 return lookup_label (name);
2647 shadowed_labels = tree_cons (NULL_TREE, decl, shadowed_labels);
2648 IDENTIFIER_LABEL_VALUE (name) = decl = 0;
2651 return lookup_label (name);
2654 /* Define a label, specifying the location in the source file.
2655 Return the LABEL_DECL node for the label, if the definition is valid.
2656 Otherwise return 0. */
2658 tree
2659 define_label (filename, line, name)
2660 const char *filename;
2661 int line;
2662 tree name;
2664 tree decl = lookup_label (name);
2666 /* If label with this name is known from an outer context, shadow it. */
2667 if (decl != 0 && DECL_CONTEXT (decl) != current_function_decl)
2669 shadowed_labels = tree_cons (NULL_TREE, decl, shadowed_labels);
2670 IDENTIFIER_LABEL_VALUE (name) = 0;
2671 decl = lookup_label (name);
2674 if (warn_traditional && !in_system_header && lookup_name (name))
2675 warning_with_file_and_line (filename, line,
2676 "traditional C lacks a separate namespace for labels, identifier `%s' conflicts",
2677 IDENTIFIER_POINTER (name));
2679 if (DECL_INITIAL (decl) != 0)
2681 error_with_file_and_line (filename, line, "duplicate label `%s'",
2682 IDENTIFIER_POINTER (name));
2683 return 0;
2685 else
2687 /* Mark label as having been defined. */
2688 DECL_INITIAL (decl) = error_mark_node;
2689 /* Say where in the source. */
2690 DECL_SOURCE_FILE (decl) = filename;
2691 DECL_SOURCE_LINE (decl) = line;
2692 return decl;
2696 /* Return the list of declarations of the current level.
2697 Note that this list is in reverse order unless/until
2698 you nreverse it; and when you do nreverse it, you must
2699 store the result back using `storedecls' or you will lose. */
2701 tree
2702 getdecls ()
2704 return current_binding_level->names;
2707 /* Return the list of type-tags (for structs, etc) of the current level. */
2709 tree
2710 gettags ()
2712 return current_binding_level->tags;
2715 /* Store the list of declarations of the current level.
2716 This is done for the parameter declarations of a function being defined,
2717 after they are modified in the light of any missing parameters. */
2719 static void
2720 storedecls (decls)
2721 tree decls;
2723 current_binding_level->names = decls;
2726 /* Similarly, store the list of tags of the current level. */
2728 static void
2729 storetags (tags)
2730 tree tags;
2732 current_binding_level->tags = tags;
2735 /* Given NAME, an IDENTIFIER_NODE,
2736 return the structure (or union or enum) definition for that name.
2737 Searches binding levels from BINDING_LEVEL up to the global level.
2738 If THISLEVEL_ONLY is nonzero, searches only the specified context
2739 (but skips any tag-transparent contexts to find one that is
2740 meaningful for tags).
2741 CODE says which kind of type the caller wants;
2742 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2743 If the wrong kind of type is found, an error is reported. */
2745 static tree
2746 lookup_tag (code, name, binding_level, thislevel_only)
2747 enum tree_code code;
2748 struct binding_level *binding_level;
2749 tree name;
2750 int thislevel_only;
2752 struct binding_level *level;
2753 int thislevel = 1;
2755 for (level = binding_level; level; level = level->level_chain)
2757 tree tail;
2758 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
2760 if (TREE_PURPOSE (tail) == name)
2762 if (TREE_CODE (TREE_VALUE (tail)) != code)
2764 /* Definition isn't the kind we were looking for. */
2765 pending_invalid_xref = name;
2766 pending_invalid_xref_file = input_filename;
2767 pending_invalid_xref_line = lineno;
2768 /* If in the same binding level as a declaration as a tag
2769 of a different type, this must not be allowed to
2770 shadow that tag, so give the error immediately.
2771 (For example, "struct foo; union foo;" is invalid.) */
2772 if (thislevel)
2773 pending_xref_error ();
2775 return TREE_VALUE (tail);
2778 if (! level->tag_transparent)
2780 if (thislevel_only)
2781 return NULL_TREE;
2782 thislevel = 0;
2785 return NULL_TREE;
2788 /* Print an error message now
2789 for a recent invalid struct, union or enum cross reference.
2790 We don't print them immediately because they are not invalid
2791 when used in the `struct foo;' construct for shadowing. */
2793 void
2794 pending_xref_error ()
2796 if (pending_invalid_xref != 0)
2797 error_with_file_and_line (pending_invalid_xref_file,
2798 pending_invalid_xref_line,
2799 "`%s' defined as wrong kind of tag",
2800 IDENTIFIER_POINTER (pending_invalid_xref));
2801 pending_invalid_xref = 0;
2804 /* Given a type, find the tag that was defined for it and return the tag name.
2805 Otherwise return 0. */
2807 static tree
2808 lookup_tag_reverse (type)
2809 tree type;
2811 struct binding_level *level;
2813 for (level = current_binding_level; level; level = level->level_chain)
2815 tree tail;
2816 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
2818 if (TREE_VALUE (tail) == type)
2819 return TREE_PURPOSE (tail);
2822 return NULL_TREE;
2825 /* Look up NAME in the current binding level and its superiors
2826 in the namespace of variables, functions and typedefs.
2827 Return a ..._DECL node of some kind representing its definition,
2828 or return 0 if it is undefined. */
2830 tree
2831 lookup_name (name)
2832 tree name;
2834 tree val;
2836 if (current_binding_level != global_binding_level
2837 && IDENTIFIER_LOCAL_VALUE (name))
2838 val = IDENTIFIER_LOCAL_VALUE (name);
2839 else
2840 val = IDENTIFIER_GLOBAL_VALUE (name);
2841 return val;
2844 /* Similar to `lookup_name' but look only at current binding level. */
2846 tree
2847 lookup_name_current_level (name)
2848 tree name;
2850 tree t;
2852 if (current_binding_level == global_binding_level)
2853 return IDENTIFIER_GLOBAL_VALUE (name);
2855 if (IDENTIFIER_LOCAL_VALUE (name) == 0)
2856 return 0;
2858 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
2859 if (DECL_NAME (t) == name)
2860 break;
2862 return t;
2865 /* Mark ARG for GC. */
2867 static void
2868 mark_binding_level (arg)
2869 void *arg;
2871 struct binding_level *level = *(struct binding_level **) arg;
2873 for (; level != 0; level = level->level_chain)
2875 ggc_mark_tree (level->names);
2876 ggc_mark_tree (level->tags);
2877 ggc_mark_tree (level->shadowed);
2878 ggc_mark_tree (level->blocks);
2879 ggc_mark_tree (level->this_block);
2880 ggc_mark_tree (level->parm_order);
2884 /* Create the predefined scalar types of C,
2885 and some nodes representing standard constants (0, 1, (void *) 0).
2886 Initialize the global binding level.
2887 Make definitions for built-in primitive functions. */
2889 void
2890 c_init_decl_processing ()
2892 tree endlink;
2893 tree ptr_ftype_void, ptr_ftype_ptr;
2895 /* Adds some ggc roots, and reserved words for c-parse.in. */
2896 c_parse_init ();
2898 current_function_decl = NULL;
2899 named_labels = NULL;
2900 current_binding_level = NULL_BINDING_LEVEL;
2901 free_binding_level = NULL_BINDING_LEVEL;
2903 /* Make the binding_level structure for global names. */
2904 pushlevel (0);
2905 global_binding_level = current_binding_level;
2907 build_common_tree_nodes (flag_signed_char);
2909 c_common_nodes_and_builtins ();
2911 boolean_type_node = integer_type_node;
2912 boolean_true_node = integer_one_node;
2913 boolean_false_node = integer_zero_node;
2915 /* With GCC, C99's _Bool is always of size 1. */
2916 c_bool_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
2917 TREE_SET_CODE (c_bool_type_node, BOOLEAN_TYPE);
2918 TYPE_MAX_VALUE (c_bool_type_node) = build_int_2 (1, 0);
2919 TREE_TYPE (TYPE_MAX_VALUE (c_bool_type_node)) = c_bool_type_node;
2920 TYPE_PRECISION (c_bool_type_node) = 1;
2921 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2922 c_bool_type_node));
2923 c_bool_false_node = build_int_2 (0, 0);
2924 TREE_TYPE (c_bool_false_node) = c_bool_type_node;
2925 c_bool_true_node = build_int_2 (1, 0);
2926 TREE_TYPE (c_bool_true_node) = c_bool_type_node;
2928 endlink = void_list_node;
2929 ptr_ftype_void = build_function_type (ptr_type_node, endlink);
2930 ptr_ftype_ptr
2931 = build_function_type (ptr_type_node,
2932 tree_cons (NULL_TREE, ptr_type_node, endlink));
2934 /* Types which are common to the fortran compiler and libf2c. When
2935 changing these, you also need to be concerned with f/com.h. */
2937 if (TYPE_PRECISION (float_type_node)
2938 == TYPE_PRECISION (long_integer_type_node))
2940 g77_integer_type_node = long_integer_type_node;
2941 g77_uinteger_type_node = long_unsigned_type_node;
2943 else if (TYPE_PRECISION (float_type_node)
2944 == TYPE_PRECISION (integer_type_node))
2946 g77_integer_type_node = integer_type_node;
2947 g77_uinteger_type_node = unsigned_type_node;
2949 else
2950 g77_integer_type_node = g77_uinteger_type_node = NULL_TREE;
2952 if (g77_integer_type_node != NULL_TREE)
2954 pushdecl (build_decl (TYPE_DECL, get_identifier ("__g77_integer"),
2955 g77_integer_type_node));
2956 pushdecl (build_decl (TYPE_DECL, get_identifier ("__g77_uinteger"),
2957 g77_uinteger_type_node));
2960 if (TYPE_PRECISION (float_type_node) * 2
2961 == TYPE_PRECISION (long_integer_type_node))
2963 g77_longint_type_node = long_integer_type_node;
2964 g77_ulongint_type_node = long_unsigned_type_node;
2966 else if (TYPE_PRECISION (float_type_node) * 2
2967 == TYPE_PRECISION (long_long_integer_type_node))
2969 g77_longint_type_node = long_long_integer_type_node;
2970 g77_ulongint_type_node = long_long_unsigned_type_node;
2972 else
2973 g77_longint_type_node = g77_ulongint_type_node = NULL_TREE;
2975 if (g77_longint_type_node != NULL_TREE)
2977 pushdecl (build_decl (TYPE_DECL, get_identifier ("__g77_longint"),
2978 g77_longint_type_node));
2979 pushdecl (build_decl (TYPE_DECL, get_identifier ("__g77_ulongint"),
2980 g77_ulongint_type_node));
2983 pedantic_lvalues = pedantic;
2985 make_fname_decl = c_make_fname_decl;
2986 start_fname_decls ();
2988 /* Record our roots. */
2990 ggc_add_tree_root (c_global_trees, CTI_MAX);
2991 ggc_add_root (&c_stmt_tree, 1, sizeof c_stmt_tree, mark_stmt_tree);
2992 ggc_add_tree_root (&c_scope_stmt_stack, 1);
2993 ggc_add_tree_root (&named_labels, 1);
2994 ggc_add_tree_root (&shadowed_labels, 1);
2995 ggc_add_root (&current_binding_level, 1, sizeof current_binding_level,
2996 mark_binding_level);
2997 ggc_add_root (&label_level_chain, 1, sizeof label_level_chain,
2998 mark_binding_level);
2999 ggc_add_tree_root (&static_ctors, 1);
3000 ggc_add_tree_root (&static_dtors, 1);
3003 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
3004 decl, NAME is the initialization string and TYPE_DEP indicates whether
3005 NAME depended on the type of the function. As we don't yet implement
3006 delayed emission of static data, we mark the decl as emitted
3007 so it is not placed in the output. Anything using it must therefore pull
3008 out the STRING_CST initializer directly. This does mean that these names
3009 are string merging candidates, which is wrong for C99's __func__. FIXME. */
3011 static tree
3012 c_make_fname_decl (id, type_dep)
3013 tree id;
3014 int type_dep;
3016 const char *name = fname_as_string (type_dep);
3017 tree decl, type, init;
3018 size_t length = strlen (name);
3020 type = build_array_type
3021 (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
3022 build_index_type (size_int (length)));
3024 decl = build_decl (VAR_DECL, id, type);
3025 /* We don't push the decl, so have to set its context here. */
3026 DECL_CONTEXT (decl) = current_function_decl;
3028 TREE_STATIC (decl) = 1;
3029 TREE_READONLY (decl) = 1;
3030 DECL_ARTIFICIAL (decl) = 1;
3032 init = build_string (length + 1, name);
3033 TREE_TYPE (init) = type;
3034 DECL_INITIAL (decl) = init;
3036 TREE_USED (decl) = 1;
3038 finish_decl (decl, init, NULL_TREE);
3040 return decl;
3043 /* Return a definition for a builtin function named NAME and whose data type
3044 is TYPE. TYPE should be a function type with argument types.
3045 FUNCTION_CODE tells later passes how to compile calls to this function.
3046 See tree.h for its possible values.
3048 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
3049 the name to be called if we can't opencode the function. */
3051 tree
3052 builtin_function (name, type, function_code, class, library_name)
3053 const char *name;
3054 tree type;
3055 int function_code;
3056 enum built_in_class class;
3057 const char *library_name;
3059 tree decl = build_decl (FUNCTION_DECL, get_identifier (name), type);
3060 DECL_EXTERNAL (decl) = 1;
3061 TREE_PUBLIC (decl) = 1;
3062 if (library_name)
3063 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
3064 make_decl_rtl (decl, NULL);
3065 pushdecl (decl);
3066 DECL_BUILT_IN_CLASS (decl) = class;
3067 DECL_FUNCTION_CODE (decl) = function_code;
3069 /* The return builtins leave the current function. */
3070 if (function_code == BUILT_IN_RETURN || function_code == BUILT_IN_EH_RETURN)
3071 TREE_THIS_VOLATILE (decl) = 1;
3073 /* Warn if a function in the namespace for users
3074 is used without an occasion to consider it declared. */
3075 if (name[0] != '_' || name[1] != '_')
3076 C_DECL_ANTICIPATED (decl) = 1;
3078 /* Possibly apply some default attributes to this built-in function. */
3079 decl_attributes (&decl, NULL_TREE, 0);
3081 return decl;
3084 /* Apply default attributes to a function, if a system function with default
3085 attributes. */
3087 void
3088 c_insert_default_attributes (decl)
3089 tree decl;
3091 if (!TREE_PUBLIC (decl))
3092 return;
3093 c_common_insert_default_attributes (decl);
3096 /* Called when a declaration is seen that contains no names to declare.
3097 If its type is a reference to a structure, union or enum inherited
3098 from a containing scope, shadow that tag name for the current scope
3099 with a forward reference.
3100 If its type defines a new named structure or union
3101 or defines an enum, it is valid but we need not do anything here.
3102 Otherwise, it is an error. */
3104 void
3105 shadow_tag (declspecs)
3106 tree declspecs;
3108 shadow_tag_warned (declspecs, 0);
3111 void
3112 shadow_tag_warned (declspecs, warned)
3113 tree declspecs;
3114 int warned;
3115 /* 1 => we have done a pedwarn. 2 => we have done a warning, but
3116 no pedwarn. */
3118 int found_tag = 0;
3119 tree link;
3120 tree specs, attrs;
3122 pending_invalid_xref = 0;
3124 /* Remove the attributes from declspecs, since they will confuse the
3125 following code. */
3126 split_specs_attrs (declspecs, &specs, &attrs);
3128 for (link = specs; link; link = TREE_CHAIN (link))
3130 tree value = TREE_VALUE (link);
3131 enum tree_code code = TREE_CODE (value);
3133 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
3134 /* Used to test also that TYPE_SIZE (value) != 0.
3135 That caused warning for `struct foo;' at top level in the file. */
3137 tree name = lookup_tag_reverse (value);
3138 tree t;
3140 found_tag++;
3142 if (name == 0)
3144 if (warned != 1 && code != ENUMERAL_TYPE)
3145 /* Empty unnamed enum OK */
3147 pedwarn ("unnamed struct/union that defines no instances");
3148 warned = 1;
3151 else
3153 t = lookup_tag (code, name, current_binding_level, 1);
3155 if (t == 0)
3157 t = make_node (code);
3158 pushtag (name, t);
3162 else
3164 if (!warned && ! in_system_header)
3166 warning ("useless keyword or type name in empty declaration");
3167 warned = 2;
3172 if (found_tag > 1)
3173 error ("two types specified in one empty declaration");
3175 if (warned != 1)
3177 if (found_tag == 0)
3178 pedwarn ("empty declaration");
3182 /* Construct an array declarator. EXPR is the expression inside [], or
3183 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
3184 to the pointer to which a parameter array is converted). STATIC_P is
3185 non-zero if "static" is inside the [], zero otherwise. VLA_UNSPEC_P
3186 is non-zero is the array is [*], a VLA of unspecified length which is
3187 nevertheless a complete type (not currently implemented by GCC),
3188 zero otherwise. The declarator is constructed as an ARRAY_REF
3189 (to be decoded by grokdeclarator), whose operand 0 is what's on the
3190 left of the [] (filled by in set_array_declarator_type) and operand 1
3191 is the expression inside; whose TREE_TYPE is the type qualifiers and
3192 which has TREE_STATIC set if "static" is used. */
3194 tree
3195 build_array_declarator (expr, quals, static_p, vla_unspec_p)
3196 tree expr;
3197 tree quals;
3198 int static_p;
3199 int vla_unspec_p;
3201 tree decl;
3202 decl = build_nt (ARRAY_REF, NULL_TREE, expr);
3203 TREE_TYPE (decl) = quals;
3204 TREE_STATIC (decl) = (static_p ? 1 : 0);
3205 if (pedantic && !flag_isoc99)
3207 if (static_p || quals != NULL_TREE)
3208 pedwarn ("ISO C89 does not support `static' or type qualifiers in parameter array declarators");
3209 if (vla_unspec_p)
3210 pedwarn ("ISO C89 does not support `[*]' array declarators");
3212 if (vla_unspec_p)
3213 warning ("GCC does not yet properly implement `[*]' array declarators");
3214 return decl;
3217 /* Set the type of an array declarator. DECL is the declarator, as
3218 constructed by build_array_declarator; TYPE is what appears on the left
3219 of the [] and goes in operand 0. ABSTRACT_P is non-zero if it is an
3220 abstract declarator, zero otherwise; this is used to reject static and
3221 type qualifiers in abstract declarators, where they are not in the
3222 C99 grammar. */
3224 tree
3225 set_array_declarator_type (decl, type, abstract_p)
3226 tree decl;
3227 tree type;
3228 int abstract_p;
3230 TREE_OPERAND (decl, 0) = type;
3231 if (abstract_p && (TREE_TYPE (decl) != NULL_TREE || TREE_STATIC (decl)))
3232 error ("static or type qualifiers in abstract declarator");
3233 return decl;
3236 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
3238 tree
3239 groktypename (typename)
3240 tree typename;
3242 tree specs, attrs;
3244 if (TREE_CODE (typename) != TREE_LIST)
3245 return typename;
3247 split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
3249 typename = grokdeclarator (TREE_VALUE (typename), specs, TYPENAME, 0);
3251 /* Apply attributes. */
3252 decl_attributes (&typename, attrs, 0);
3254 return typename;
3257 /* Return a PARM_DECL node for a given pair of specs and declarator. */
3259 tree
3260 groktypename_in_parm_context (typename)
3261 tree typename;
3263 if (TREE_CODE (typename) != TREE_LIST)
3264 return typename;
3265 return grokdeclarator (TREE_VALUE (typename),
3266 TREE_PURPOSE (typename),
3267 PARM, 0);
3270 /* Decode a declarator in an ordinary declaration or data definition.
3271 This is called as soon as the type information and variable name
3272 have been parsed, before parsing the initializer if any.
3273 Here we create the ..._DECL node, fill in its type,
3274 and put it on the list of decls for the current context.
3275 The ..._DECL node is returned as the value.
3277 Exception: for arrays where the length is not specified,
3278 the type is left null, to be filled in by `finish_decl'.
3280 Function definitions do not come here; they go to start_function
3281 instead. However, external and forward declarations of functions
3282 do go through here. Structure field declarations are done by
3283 grokfield and not through here. */
3285 tree
3286 start_decl (declarator, declspecs, initialized, attributes)
3287 tree declarator, declspecs;
3288 int initialized;
3289 tree attributes;
3291 tree decl;
3292 tree tem;
3294 /* An object declared as __attribute__((deprecated)) suppresses
3295 warnings of uses of other deprecated items. */
3296 if (lookup_attribute ("deprecated", attributes))
3297 deprecated_state = DEPRECATED_SUPPRESS;
3299 decl = grokdeclarator (declarator, declspecs,
3300 NORMAL, initialized);
3302 deprecated_state = DEPRECATED_NORMAL;
3304 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
3305 && MAIN_NAME_P (DECL_NAME (decl)))
3306 warning_with_decl (decl, "`%s' is usually a function");
3308 if (initialized)
3309 /* Is it valid for this decl to have an initializer at all?
3310 If not, set INITIALIZED to zero, which will indirectly
3311 tell `finish_decl' to ignore the initializer once it is parsed. */
3312 switch (TREE_CODE (decl))
3314 case TYPE_DECL:
3315 /* typedef foo = bar means give foo the same type as bar.
3316 We haven't parsed bar yet, so `finish_decl' will fix that up.
3317 Any other case of an initialization in a TYPE_DECL is an error. */
3318 if (pedantic || list_length (declspecs) > 1)
3320 error ("typedef `%s' is initialized",
3321 IDENTIFIER_POINTER (DECL_NAME (decl)));
3322 initialized = 0;
3324 break;
3326 case FUNCTION_DECL:
3327 error ("function `%s' is initialized like a variable",
3328 IDENTIFIER_POINTER (DECL_NAME (decl)));
3329 initialized = 0;
3330 break;
3332 case PARM_DECL:
3333 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
3334 error ("parameter `%s' is initialized",
3335 IDENTIFIER_POINTER (DECL_NAME (decl)));
3336 initialized = 0;
3337 break;
3339 default:
3340 /* Don't allow initializations for incomplete types
3341 except for arrays which might be completed by the initialization. */
3343 /* This can happen if the array size is an undefined macro. We already
3344 gave a warning, so we don't need another one. */
3345 if (TREE_TYPE (decl) == error_mark_node)
3346 initialized = 0;
3347 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
3349 /* A complete type is ok if size is fixed. */
3351 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
3352 || C_DECL_VARIABLE_SIZE (decl))
3354 error ("variable-sized object may not be initialized");
3355 initialized = 0;
3358 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
3360 error ("variable `%s' has initializer but incomplete type",
3361 IDENTIFIER_POINTER (DECL_NAME (decl)));
3362 initialized = 0;
3364 else if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
3366 error ("elements of array `%s' have incomplete type",
3367 IDENTIFIER_POINTER (DECL_NAME (decl)));
3368 initialized = 0;
3372 if (initialized)
3374 #if 0
3375 /* Seems redundant with grokdeclarator. */
3376 if (current_binding_level != global_binding_level
3377 && DECL_EXTERNAL (decl)
3378 && TREE_CODE (decl) != FUNCTION_DECL)
3379 warning ("declaration of `%s' has `extern' and is initialized",
3380 IDENTIFIER_POINTER (DECL_NAME (decl)));
3381 #endif
3382 DECL_EXTERNAL (decl) = 0;
3383 if (current_binding_level == global_binding_level)
3384 TREE_STATIC (decl) = 1;
3386 /* Tell `pushdecl' this is an initialized decl
3387 even though we don't yet have the initializer expression.
3388 Also tell `finish_decl' it may store the real initializer. */
3389 DECL_INITIAL (decl) = error_mark_node;
3392 /* If this is a function declaration, write a record describing it to the
3393 prototypes file (if requested). */
3395 if (TREE_CODE (decl) == FUNCTION_DECL)
3396 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
3398 /* ANSI specifies that a tentative definition which is not merged with
3399 a non-tentative definition behaves exactly like a definition with an
3400 initializer equal to zero. (Section 3.7.2)
3401 -fno-common gives strict ANSI behavior. Usually you don't want it.
3402 This matters only for variables with external linkage. */
3403 if (!initialized && (! flag_no_common || ! TREE_PUBLIC (decl)))
3404 DECL_COMMON (decl) = 1;
3406 /* Set attributes here so if duplicate decl, will have proper attributes. */
3407 decl_attributes (&decl, attributes, 0);
3409 /* If #pragma weak was used, mark the decl weak now. */
3410 if (current_binding_level == global_binding_level)
3411 maybe_apply_pragma_weak (decl);
3413 if (TREE_CODE (decl) == FUNCTION_DECL
3414 && DECL_DECLARED_INLINE_P (decl)
3415 && DECL_UNINLINABLE (decl)
3416 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
3417 warning_with_decl (decl,
3418 "inline function `%s' given attribute noinline");
3420 /* Add this decl to the current binding level.
3421 TEM may equal DECL or it may be a previous decl of the same name. */
3422 tem = pushdecl (decl);
3424 /* For a local variable, define the RTL now. */
3425 if (current_binding_level != global_binding_level
3426 /* But not if this is a duplicate decl
3427 and we preserved the rtl from the previous one
3428 (which may or may not happen). */
3429 && !DECL_RTL_SET_P (tem)
3430 && !DECL_CONTEXT (tem))
3432 if (TREE_TYPE (tem) != error_mark_node
3433 && COMPLETE_TYPE_P (TREE_TYPE (tem)))
3434 expand_decl (tem);
3435 else if (TREE_CODE (TREE_TYPE (tem)) == ARRAY_TYPE
3436 && DECL_INITIAL (tem) != 0)
3437 expand_decl (tem);
3440 return tem;
3443 /* Finish processing of a declaration;
3444 install its initial value.
3445 If the length of an array type is not known before,
3446 it must be determined now, from the initial value, or it is an error. */
3448 void
3449 finish_decl (decl, init, asmspec_tree)
3450 tree decl, init;
3451 tree asmspec_tree;
3453 tree type = TREE_TYPE (decl);
3454 int was_incomplete = (DECL_SIZE (decl) == 0);
3455 const char *asmspec = 0;
3457 /* If a name was specified, get the string. */
3458 if (current_binding_level == global_binding_level)
3459 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
3460 if (asmspec_tree)
3461 asmspec = TREE_STRING_POINTER (asmspec_tree);
3463 /* If `start_decl' didn't like having an initialization, ignore it now. */
3464 if (init != 0 && DECL_INITIAL (decl) == 0)
3465 init = 0;
3467 /* Don't crash if parm is initialized. */
3468 if (TREE_CODE (decl) == PARM_DECL)
3469 init = 0;
3471 if (init)
3473 if (TREE_CODE (decl) != TYPE_DECL)
3474 store_init_value (decl, init);
3475 else
3477 /* typedef foo = bar; store the type of bar as the type of foo. */
3478 TREE_TYPE (decl) = TREE_TYPE (init);
3479 DECL_INITIAL (decl) = init = 0;
3483 /* Deduce size of array from initialization, if not already known */
3484 if (TREE_CODE (type) == ARRAY_TYPE
3485 && TYPE_DOMAIN (type) == 0
3486 && TREE_CODE (decl) != TYPE_DECL)
3488 int do_default
3489 = (TREE_STATIC (decl)
3490 /* Even if pedantic, an external linkage array
3491 may have incomplete type at first. */
3492 ? pedantic && !TREE_PUBLIC (decl)
3493 : !DECL_EXTERNAL (decl));
3494 int failure
3495 = complete_array_type (type, DECL_INITIAL (decl), do_default);
3497 /* Get the completed type made by complete_array_type. */
3498 type = TREE_TYPE (decl);
3500 if (failure == 1)
3501 error_with_decl (decl, "initializer fails to determine size of `%s'");
3503 else if (failure == 2)
3505 if (do_default)
3506 error_with_decl (decl, "array size missing in `%s'");
3507 /* If a `static' var's size isn't known,
3508 make it extern as well as static, so it does not get
3509 allocated.
3510 If it is not `static', then do not mark extern;
3511 finish_incomplete_decl will give it a default size
3512 and it will get allocated. */
3513 else if (!pedantic && TREE_STATIC (decl) && ! TREE_PUBLIC (decl))
3514 DECL_EXTERNAL (decl) = 1;
3517 /* TYPE_MAX_VALUE is always one less than the number of elements
3518 in the array, because we start counting at zero. Therefore,
3519 warn only if the value is less than zero. */
3520 else if (pedantic && TYPE_DOMAIN (type) != 0
3521 && tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < 0)
3522 error_with_decl (decl, "zero or negative size array `%s'");
3524 layout_decl (decl, 0);
3527 if (TREE_CODE (decl) == VAR_DECL)
3529 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
3530 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
3531 layout_decl (decl, 0);
3533 if (DECL_SIZE (decl) == 0
3534 /* Don't give an error if we already gave one earlier. */
3535 && TREE_TYPE (decl) != error_mark_node
3536 && (TREE_STATIC (decl)
3538 /* A static variable with an incomplete type
3539 is an error if it is initialized.
3540 Also if it is not file scope.
3541 Otherwise, let it through, but if it is not `extern'
3542 then it may cause an error message later. */
3543 (DECL_INITIAL (decl) != 0
3544 || DECL_CONTEXT (decl) != 0)
3546 /* An automatic variable with an incomplete type
3547 is an error. */
3548 !DECL_EXTERNAL (decl)))
3550 error_with_decl (decl, "storage size of `%s' isn't known");
3551 TREE_TYPE (decl) = error_mark_node;
3554 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
3555 && DECL_SIZE (decl) != 0)
3557 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
3558 constant_expression_warning (DECL_SIZE (decl));
3559 else
3560 error_with_decl (decl, "storage size of `%s' isn't constant");
3563 if (TREE_USED (type))
3564 TREE_USED (decl) = 1;
3567 /* If this is a function and an assembler name is specified, it isn't
3568 builtin any more. Also reset DECL_RTL so we can give it its new
3569 name. */
3570 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
3572 DECL_BUILT_IN_CLASS (decl) = NOT_BUILT_IN;
3573 SET_DECL_RTL (decl, NULL_RTX);
3574 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
3577 /* Output the assembler code and/or RTL code for variables and functions,
3578 unless the type is an undefined structure or union.
3579 If not, it will get done when the type is completed. */
3581 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3583 /* This is a no-op in c-lang.c or something real in objc-actions.c. */
3584 maybe_objc_check_decl (decl);
3586 if (!DECL_CONTEXT (decl))
3588 if (DECL_INITIAL (decl) == NULL_TREE
3589 || DECL_INITIAL (decl) == error_mark_node)
3590 /* Don't output anything
3591 when a tentative file-scope definition is seen.
3592 But at end of compilation, do output code for them. */
3593 DECL_DEFER_OUTPUT (decl) = 1;
3594 rest_of_decl_compilation (decl, asmspec,
3595 (DECL_CONTEXT (decl) == 0
3596 || TREE_ASM_WRITTEN (decl)), 0);
3598 else
3600 /* This is a local variable. If there is an ASMSPEC, the
3601 user has requested that we handle it specially. */
3602 if (asmspec)
3604 /* In conjunction with an ASMSPEC, the `register'
3605 keyword indicates that we should place the variable
3606 in a particular register. */
3607 if (DECL_REGISTER (decl))
3608 DECL_C_HARD_REGISTER (decl) = 1;
3610 /* If this is not a static variable, issue a warning.
3611 It doesn't make any sense to give an ASMSPEC for an
3612 ordinary, non-register local variable. Historically,
3613 GCC has accepted -- but ignored -- the ASMSPEC in
3614 this case. */
3615 if (TREE_CODE (decl) == VAR_DECL
3616 && !DECL_REGISTER (decl)
3617 && !TREE_STATIC (decl))
3618 warning_with_decl (decl,
3619 "ignoring asm-specifier for non-static local variable `%s'");
3620 else
3621 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
3624 if (TREE_CODE (decl) != FUNCTION_DECL)
3625 add_decl_stmt (decl);
3628 if (DECL_CONTEXT (decl) != 0)
3630 /* Recompute the RTL of a local array now
3631 if it used to be an incomplete type. */
3632 if (was_incomplete
3633 && ! TREE_STATIC (decl) && ! DECL_EXTERNAL (decl))
3635 /* If we used it already as memory, it must stay in memory. */
3636 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3637 /* If it's still incomplete now, no init will save it. */
3638 if (DECL_SIZE (decl) == 0)
3639 DECL_INITIAL (decl) = 0;
3644 if (TREE_CODE (decl) == TYPE_DECL)
3646 /* This is a no-op in c-lang.c or something real in objc-actions.c. */
3647 maybe_objc_check_decl (decl);
3648 rest_of_decl_compilation (decl, NULL, DECL_CONTEXT (decl) == 0, 0);
3651 /* At the end of a declaration, throw away any variable type sizes
3652 of types defined inside that declaration. There is no use
3653 computing them in the following function definition. */
3654 if (current_binding_level == global_binding_level)
3655 get_pending_sizes ();
3658 /* Given a parsed parameter declaration,
3659 decode it into a PARM_DECL and push that on the current binding level.
3660 Also, for the sake of forward parm decls,
3661 record the given order of parms in `parm_order'. */
3663 void
3664 push_parm_decl (parm)
3665 tree parm;
3667 tree decl;
3668 int old_immediate_size_expand = immediate_size_expand;
3669 /* Don't try computing parm sizes now -- wait till fn is called. */
3670 immediate_size_expand = 0;
3672 decl = grokdeclarator (TREE_VALUE (TREE_PURPOSE (parm)),
3673 TREE_PURPOSE (TREE_PURPOSE (parm)), PARM, 0);
3674 decl_attributes (&decl, TREE_VALUE (parm), 0);
3676 #if 0
3677 if (DECL_NAME (decl))
3679 tree olddecl;
3680 olddecl = lookup_name (DECL_NAME (decl));
3681 if (pedantic && olddecl != 0 && TREE_CODE (olddecl) == TYPE_DECL)
3682 pedwarn_with_decl (decl,
3683 "ISO C forbids parameter `%s' shadowing typedef");
3685 #endif
3687 decl = pushdecl (decl);
3689 immediate_size_expand = old_immediate_size_expand;
3691 current_binding_level->parm_order
3692 = tree_cons (NULL_TREE, decl, current_binding_level->parm_order);
3694 /* Add this decl to the current binding level. */
3695 finish_decl (decl, NULL_TREE, NULL_TREE);
3698 /* Clear the given order of parms in `parm_order'.
3699 Used at start of parm list,
3700 and also at semicolon terminating forward decls. */
3702 void
3703 clear_parm_order ()
3705 current_binding_level->parm_order = NULL_TREE;
3708 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3709 literal, which may be an incomplete array type completed by the
3710 initializer; INIT is a CONSTRUCTOR that initializes the compound
3711 literal. */
3713 tree
3714 build_compound_literal (type, init)
3715 tree type;
3716 tree init;
3718 /* We do not use start_decl here because we have a type, not a declarator;
3719 and do not use finish_decl because the decl should be stored inside
3720 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_STMT. */
3721 tree decl = build_decl (VAR_DECL, NULL_TREE, type);
3722 tree complit;
3723 tree stmt;
3724 DECL_EXTERNAL (decl) = 0;
3725 TREE_PUBLIC (decl) = 0;
3726 TREE_STATIC (decl) = (current_binding_level == global_binding_level);
3727 DECL_CONTEXT (decl) = current_function_decl;
3728 TREE_USED (decl) = 1;
3729 TREE_TYPE (decl) = type;
3730 store_init_value (decl, init);
3732 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3734 int failure = complete_array_type (type, DECL_INITIAL (decl), 1);
3735 if (failure)
3736 abort ();
3739 type = TREE_TYPE (decl);
3740 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3741 return error_mark_node;
3743 stmt = build_stmt (DECL_STMT, decl);
3744 complit = build1 (COMPOUND_LITERAL_EXPR, TREE_TYPE (decl), stmt);
3745 TREE_SIDE_EFFECTS (complit) = 1;
3747 layout_decl (decl, 0);
3749 if (TREE_STATIC (decl))
3751 /* This decl needs a name for the assembler output. We also need
3752 a unique suffix to be added to the name, for which DECL_CONTEXT
3753 must be set. */
3754 DECL_NAME (decl) = get_identifier ("__compound_literal");
3755 DECL_CONTEXT (decl) = complit;
3756 rest_of_decl_compilation (decl, NULL, 1, 0);
3757 DECL_CONTEXT (decl) = NULL_TREE;
3760 return complit;
3763 /* Make TYPE a complete type based on INITIAL_VALUE.
3764 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
3765 2 if there was no information (in which case assume 1 if DO_DEFAULT). */
3768 complete_array_type (type, initial_value, do_default)
3769 tree type;
3770 tree initial_value;
3771 int do_default;
3773 tree maxindex = NULL_TREE;
3774 int value = 0;
3776 if (initial_value)
3778 /* Note MAXINDEX is really the maximum index,
3779 one less than the size. */
3780 if (TREE_CODE (initial_value) == STRING_CST)
3782 int eltsize
3783 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
3784 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
3785 / eltsize) - 1, 0);
3787 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
3789 tree elts = CONSTRUCTOR_ELTS (initial_value);
3790 maxindex = build_int_2 (-1, -1);
3791 for (; elts; elts = TREE_CHAIN (elts))
3793 if (TREE_PURPOSE (elts))
3794 maxindex = TREE_PURPOSE (elts);
3795 else
3796 maxindex = fold (build (PLUS_EXPR, integer_type_node,
3797 maxindex, integer_one_node));
3799 maxindex = copy_node (maxindex);
3801 else
3803 /* Make an error message unless that happened already. */
3804 if (initial_value != error_mark_node)
3805 value = 1;
3807 /* Prevent further error messages. */
3808 maxindex = build_int_2 (0, 0);
3812 if (!maxindex)
3814 if (do_default)
3815 maxindex = build_int_2 (0, 0);
3816 value = 2;
3819 if (maxindex)
3821 TYPE_DOMAIN (type) = build_index_type (maxindex);
3822 if (!TREE_TYPE (maxindex))
3823 TREE_TYPE (maxindex) = TYPE_DOMAIN (type);
3826 /* Lay out the type now that we can get the real answer. */
3828 layout_type (type);
3830 return value;
3833 /* Given declspecs and a declarator,
3834 determine the name and type of the object declared
3835 and construct a ..._DECL node for it.
3836 (In one case we can return a ..._TYPE node instead.
3837 For invalid input we sometimes return 0.)
3839 DECLSPECS is a chain of tree_list nodes whose value fields
3840 are the storage classes and type specifiers.
3842 DECL_CONTEXT says which syntactic context this declaration is in:
3843 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3844 FUNCDEF for a function definition. Like NORMAL but a few different
3845 error messages in each case. Return value may be zero meaning
3846 this definition is too screwy to try to parse.
3847 PARM for a parameter declaration (either within a function prototype
3848 or before a function body). Make a PARM_DECL, or return void_type_node.
3849 TYPENAME if for a typename (in a cast or sizeof).
3850 Don't make a DECL node; just return the ..._TYPE node.
3851 FIELD for a struct or union field; make a FIELD_DECL.
3852 BITFIELD for a field with specified width.
3853 INITIALIZED is 1 if the decl has an initializer.
3855 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3856 It may also be so in the PARM case, for a prototype where the
3857 argument type is specified but not the name.
3859 This function is where the complicated C meanings of `static'
3860 and `extern' are interpreted. */
3862 static tree
3863 grokdeclarator (declarator, declspecs, decl_context, initialized)
3864 tree declspecs;
3865 tree declarator;
3866 enum decl_context decl_context;
3867 int initialized;
3869 int specbits = 0;
3870 tree spec;
3871 tree type = NULL_TREE;
3872 int longlong = 0;
3873 int constp;
3874 int restrictp;
3875 int volatilep;
3876 int type_quals = TYPE_UNQUALIFIED;
3877 int inlinep;
3878 int explicit_int = 0;
3879 int explicit_char = 0;
3880 int defaulted_int = 0;
3881 tree typedef_decl = 0;
3882 const char *name;
3883 tree typedef_type = 0;
3884 int funcdef_flag = 0;
3885 enum tree_code innermost_code = ERROR_MARK;
3886 int bitfield = 0;
3887 int size_varies = 0;
3888 tree decl_attr = NULL_TREE;
3889 tree array_ptr_quals = NULL_TREE;
3890 int array_parm_static = 0;
3891 tree returned_attrs = NULL_TREE;
3893 if (decl_context == BITFIELD)
3894 bitfield = 1, decl_context = FIELD;
3896 if (decl_context == FUNCDEF)
3897 funcdef_flag = 1, decl_context = NORMAL;
3899 /* Look inside a declarator for the name being declared
3900 and get it as a string, for an error message. */
3902 tree decl = declarator;
3903 name = 0;
3905 while (decl)
3906 switch (TREE_CODE (decl))
3908 case ARRAY_REF:
3909 case INDIRECT_REF:
3910 case CALL_EXPR:
3911 innermost_code = TREE_CODE (decl);
3912 decl = TREE_OPERAND (decl, 0);
3913 break;
3915 case TREE_LIST:
3916 decl = TREE_VALUE (decl);
3917 break;
3919 case IDENTIFIER_NODE:
3920 name = IDENTIFIER_POINTER (decl);
3921 decl = 0;
3922 break;
3924 default:
3925 abort ();
3927 if (name == 0)
3928 name = "type name";
3931 /* A function definition's declarator must have the form of
3932 a function declarator. */
3934 if (funcdef_flag && innermost_code != CALL_EXPR)
3935 return 0;
3937 /* Anything declared one level down from the top level
3938 must be one of the parameters of a function
3939 (because the body is at least two levels down). */
3941 /* If this looks like a function definition, make it one,
3942 even if it occurs where parms are expected.
3943 Then store_parm_decls will reject it and not use it as a parm. */
3944 if (decl_context == NORMAL && !funcdef_flag
3945 && current_binding_level->parm_flag)
3946 decl_context = PARM;
3948 /* Look through the decl specs and record which ones appear.
3949 Some typespecs are defined as built-in typenames.
3950 Others, the ones that are modifiers of other types,
3951 are represented by bits in SPECBITS: set the bits for
3952 the modifiers that appear. Storage class keywords are also in SPECBITS.
3954 If there is a typedef name or a type, store the type in TYPE.
3955 This includes builtin typedefs such as `int'.
3957 Set EXPLICIT_INT or EXPLICIT_CHAR if the type is `int' or `char'
3958 and did not come from a user typedef.
3960 Set LONGLONG if `long' is mentioned twice. */
3962 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
3964 tree id = TREE_VALUE (spec);
3966 /* If the entire declaration is itself tagged as deprecated then
3967 suppress reports of deprecated items. */
3968 if (id && TREE_DEPRECATED (id))
3970 if (deprecated_state != DEPRECATED_SUPPRESS)
3971 warn_deprecated_use (id);
3974 if (id == ridpointers[(int) RID_INT])
3975 explicit_int = 1;
3976 if (id == ridpointers[(int) RID_CHAR])
3977 explicit_char = 1;
3979 if (TREE_CODE (id) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (id))
3981 enum rid i = C_RID_CODE (id);
3982 if ((int) i <= (int) RID_LAST_MODIFIER)
3984 if (i == RID_LONG && (specbits & (1 << (int) i)))
3986 if (longlong)
3987 error ("`long long long' is too long for GCC");
3988 else
3990 if (pedantic && !flag_isoc99 && ! in_system_header
3991 && warn_long_long)
3992 pedwarn ("ISO C89 does not support `long long'");
3993 longlong = 1;
3996 else if (specbits & (1 << (int) i))
3997 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
3998 specbits |= 1 << (int) i;
3999 goto found;
4002 if (type)
4003 error ("two or more data types in declaration of `%s'", name);
4004 /* Actual typedefs come to us as TYPE_DECL nodes. */
4005 else if (TREE_CODE (id) == TYPE_DECL)
4007 if (TREE_TYPE (id) == error_mark_node)
4008 ; /* Allow the type to default to int to avoid cascading errors. */
4009 else
4011 type = TREE_TYPE (id);
4012 decl_attr = DECL_ATTRIBUTES (id);
4013 typedef_decl = id;
4016 /* Built-in types come as identifiers. */
4017 else if (TREE_CODE (id) == IDENTIFIER_NODE)
4019 tree t = lookup_name (id);
4020 if (TREE_TYPE (t) == error_mark_node)
4022 else if (!t || TREE_CODE (t) != TYPE_DECL)
4023 error ("`%s' fails to be a typedef or built in type",
4024 IDENTIFIER_POINTER (id));
4025 else
4027 type = TREE_TYPE (t);
4028 typedef_decl = t;
4031 else if (TREE_CODE (id) != ERROR_MARK)
4032 type = id;
4034 found:
4038 typedef_type = type;
4039 if (type)
4040 size_varies = C_TYPE_VARIABLE_SIZE (type);
4042 /* No type at all: default to `int', and set DEFAULTED_INT
4043 because it was not a user-defined typedef. */
4045 if (type == 0)
4047 if ((! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
4048 | (1 << (int) RID_SIGNED)
4049 | (1 << (int) RID_UNSIGNED)
4050 | (1 << (int) RID_COMPLEX))))
4051 /* Don't warn about typedef foo = bar. */
4052 && ! (specbits & (1 << (int) RID_TYPEDEF) && initialized)
4053 && ! in_system_header)
4055 /* Issue a warning if this is an ISO C 99 program or if -Wreturn-type
4056 and this is a function, or if -Wimplicit; prefer the former
4057 warning since it is more explicit. */
4058 if ((warn_implicit_int || warn_return_type || flag_isoc99)
4059 && funcdef_flag)
4060 warn_about_return_type = 1;
4061 else if (warn_implicit_int || flag_isoc99)
4062 pedwarn_c99 ("type defaults to `int' in declaration of `%s'",
4063 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 || (bitfield && ! flag_signed_bitfields
4146 && (explicit_int || defaulted_int || explicit_char
4147 /* A typedef for plain `int' without `signed'
4148 can be controlled just like plain `int'. */
4149 || ! (typedef_decl != 0
4150 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
4151 && TREE_CODE (type) != ENUMERAL_TYPE
4152 && !(specbits & 1 << (int) RID_SIGNED)))
4154 if (longlong)
4155 type = long_long_unsigned_type_node;
4156 else if (specbits & 1 << (int) RID_LONG)
4157 type = long_unsigned_type_node;
4158 else if (specbits & 1 << (int) RID_SHORT)
4159 type = short_unsigned_type_node;
4160 else if (type == char_type_node)
4161 type = unsigned_char_type_node;
4162 else if (typedef_decl)
4163 type = c_common_unsigned_type (type);
4164 else
4165 type = unsigned_type_node;
4167 else if ((specbits & 1 << (int) RID_SIGNED)
4168 && type == char_type_node)
4169 type = signed_char_type_node;
4170 else if (longlong)
4171 type = long_long_integer_type_node;
4172 else if (specbits & 1 << (int) RID_LONG)
4173 type = long_integer_type_node;
4174 else if (specbits & 1 << (int) RID_SHORT)
4175 type = short_integer_type_node;
4177 if (specbits & 1 << (int) RID_COMPLEX)
4179 if (pedantic && !flag_isoc99)
4180 pedwarn ("ISO C89 does not support complex types");
4181 /* If we just have "complex", it is equivalent to
4182 "complex double", but if any modifiers at all are specified it is
4183 the complex form of TYPE. E.g, "complex short" is
4184 "complex short int". */
4186 if (defaulted_int && ! longlong
4187 && ! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
4188 | (1 << (int) RID_SIGNED)
4189 | (1 << (int) RID_UNSIGNED))))
4191 if (pedantic)
4192 pedwarn ("ISO C does not support plain `complex' meaning `double complex'");
4193 type = complex_double_type_node;
4195 else if (type == integer_type_node)
4197 if (pedantic)
4198 pedwarn ("ISO C does not support complex integer types");
4199 type = complex_integer_type_node;
4201 else if (type == float_type_node)
4202 type = complex_float_type_node;
4203 else if (type == double_type_node)
4204 type = complex_double_type_node;
4205 else if (type == long_double_type_node)
4206 type = complex_long_double_type_node;
4207 else
4209 if (pedantic)
4210 pedwarn ("ISO C does not support complex integer types");
4211 type = build_complex_type (type);
4215 /* Figure out the type qualifiers for the declaration. There are
4216 two ways a declaration can become qualified. One is something
4217 like `const int i' where the `const' is explicit. Another is
4218 something like `typedef const int CI; CI i' where the type of the
4219 declaration contains the `const'. */
4220 constp = !! (specbits & 1 << (int) RID_CONST) + TYPE_READONLY (type);
4221 restrictp = !! (specbits & 1 << (int) RID_RESTRICT) + TYPE_RESTRICT (type);
4222 volatilep = !! (specbits & 1 << (int) RID_VOLATILE) + TYPE_VOLATILE (type);
4223 inlinep = !! (specbits & (1 << (int) RID_INLINE));
4224 if (constp > 1 && ! flag_isoc99)
4225 pedwarn ("duplicate `const'");
4226 if (restrictp > 1 && ! flag_isoc99)
4227 pedwarn ("duplicate `restrict'");
4228 if (volatilep > 1 && ! flag_isoc99)
4229 pedwarn ("duplicate `volatile'");
4230 if (! flag_gen_aux_info && (TYPE_QUALS (type)))
4231 type = TYPE_MAIN_VARIANT (type);
4232 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4233 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4234 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4236 /* Warn if two storage classes are given. Default to `auto'. */
4239 int nclasses = 0;
4241 if (specbits & 1 << (int) RID_AUTO) nclasses++;
4242 if (specbits & 1 << (int) RID_STATIC) nclasses++;
4243 if (specbits & 1 << (int) RID_EXTERN) nclasses++;
4244 if (specbits & 1 << (int) RID_REGISTER) nclasses++;
4245 if (specbits & 1 << (int) RID_TYPEDEF) nclasses++;
4247 /* Warn about storage classes that are invalid for certain
4248 kinds of declarations (parameters, typenames, etc.). */
4250 if (nclasses > 1)
4251 error ("multiple storage classes in declaration of `%s'", name);
4252 else if (funcdef_flag
4253 && (specbits
4254 & ((1 << (int) RID_REGISTER)
4255 | (1 << (int) RID_AUTO)
4256 | (1 << (int) RID_TYPEDEF))))
4258 if (specbits & 1 << (int) RID_AUTO
4259 && (pedantic || current_binding_level == global_binding_level))
4260 pedwarn ("function definition declared `auto'");
4261 if (specbits & 1 << (int) RID_REGISTER)
4262 error ("function definition declared `register'");
4263 if (specbits & 1 << (int) RID_TYPEDEF)
4264 error ("function definition declared `typedef'");
4265 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
4266 | (1 << (int) RID_AUTO));
4268 else if (decl_context != NORMAL && nclasses > 0)
4270 if (decl_context == PARM && specbits & 1 << (int) RID_REGISTER)
4272 else
4274 switch (decl_context)
4276 case FIELD:
4277 error ("storage class specified for structure field `%s'",
4278 name);
4279 break;
4280 case PARM:
4281 error ("storage class specified for parameter `%s'", name);
4282 break;
4283 default:
4284 error ("storage class specified for typename");
4285 break;
4287 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
4288 | (1 << (int) RID_AUTO) | (1 << (int) RID_STATIC)
4289 | (1 << (int) RID_EXTERN));
4292 else if (specbits & 1 << (int) RID_EXTERN && initialized && ! funcdef_flag)
4294 /* `extern' with initialization is invalid if not at top level. */
4295 if (current_binding_level == global_binding_level)
4296 warning ("`%s' initialized and declared `extern'", name);
4297 else
4298 error ("`%s' has both `extern' and initializer", name);
4300 else if (specbits & 1 << (int) RID_EXTERN && funcdef_flag
4301 && current_binding_level != global_binding_level)
4302 error ("nested function `%s' declared `extern'", name);
4303 else if (current_binding_level == global_binding_level
4304 && specbits & (1 << (int) RID_AUTO))
4305 error ("top-level declaration of `%s' specifies `auto'", name);
4308 /* Now figure out the structure of the declarator proper.
4309 Descend through it, creating more complex types, until we reach
4310 the declared identifier (or NULL_TREE, in an absolute declarator). */
4312 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE)
4314 if (type == error_mark_node)
4316 declarator = TREE_OPERAND (declarator, 0);
4317 continue;
4320 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
4321 an INDIRECT_REF (for *...),
4322 a CALL_EXPR (for ...(...)),
4323 a TREE_LIST (for nested attributes),
4324 an identifier (for the name being declared)
4325 or a null pointer (for the place in an absolute declarator
4326 where the name was omitted).
4327 For the last two cases, we have just exited the loop.
4329 At this point, TYPE is the type of elements of an array,
4330 or for a function to return, or for a pointer to point to.
4331 After this sequence of ifs, TYPE is the type of the
4332 array or function or pointer, and DECLARATOR has had its
4333 outermost layer removed. */
4335 if (array_ptr_quals != NULL_TREE || array_parm_static)
4337 /* Only the innermost declarator (making a parameter be of
4338 array type which is converted to pointer type)
4339 may have static or type qualifiers. */
4340 error ("static or type qualifiers in non-parameter array declarator");
4341 array_ptr_quals = NULL_TREE;
4342 array_parm_static = 0;
4345 if (TREE_CODE (declarator) == TREE_LIST)
4347 /* We encode a declarator with embedded attributes using
4348 a TREE_LIST. */
4349 tree attrs = TREE_PURPOSE (declarator);
4350 tree inner_decl;
4351 int attr_flags = 0;
4352 declarator = TREE_VALUE (declarator);
4353 inner_decl = declarator;
4354 while (inner_decl != NULL_TREE
4355 && TREE_CODE (inner_decl) == TREE_LIST)
4356 inner_decl = TREE_VALUE (inner_decl);
4357 if (inner_decl == NULL_TREE
4358 || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
4359 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
4360 else if (TREE_CODE (inner_decl) == CALL_EXPR)
4361 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
4362 else if (TREE_CODE (inner_decl) == ARRAY_REF)
4363 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
4364 returned_attrs = decl_attributes (&type,
4365 chainon (returned_attrs, attrs),
4366 attr_flags);
4368 else if (TREE_CODE (declarator) == ARRAY_REF)
4370 tree itype = NULL_TREE;
4371 tree size = TREE_OPERAND (declarator, 1);
4372 /* The index is a signed object `sizetype' bits wide. */
4373 tree index_type = c_common_signed_type (sizetype);
4375 array_ptr_quals = TREE_TYPE (declarator);
4376 array_parm_static = TREE_STATIC (declarator);
4378 declarator = TREE_OPERAND (declarator, 0);
4380 /* Check for some types that there cannot be arrays of. */
4382 if (VOID_TYPE_P (type))
4384 error ("declaration of `%s' as array of voids", name);
4385 type = error_mark_node;
4388 if (TREE_CODE (type) == FUNCTION_TYPE)
4390 error ("declaration of `%s' as array of functions", name);
4391 type = error_mark_node;
4394 if (size == error_mark_node)
4395 type = error_mark_node;
4397 if (type == error_mark_node)
4398 continue;
4400 /* If size was specified, set ITYPE to a range-type for that size.
4401 Otherwise, ITYPE remains null. finish_decl may figure it out
4402 from an initial value. */
4404 if (size)
4406 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4407 STRIP_TYPE_NOPS (size);
4409 if (! INTEGRAL_TYPE_P (TREE_TYPE (size)))
4411 error ("size of array `%s' has non-integer type", name);
4412 size = integer_one_node;
4415 if (pedantic && integer_zerop (size))
4416 pedwarn ("ISO C forbids zero-size array `%s'", name);
4418 if (TREE_CODE (size) == INTEGER_CST)
4420 constant_expression_warning (size);
4421 if (tree_int_cst_sgn (size) < 0)
4423 error ("size of array `%s' is negative", name);
4424 size = integer_one_node;
4427 else
4429 /* Make sure the array size remains visibly nonconstant
4430 even if it is (eg) a const variable with known value. */
4431 size_varies = 1;
4433 if (pedantic)
4435 if (TREE_CONSTANT (size))
4436 pedwarn ("ISO C89 forbids array `%s' whose size can't be evaluated",
4437 name);
4438 else
4439 pedwarn ("ISO C89 forbids variable-size array `%s'",
4440 name);
4444 if (integer_zerop (size))
4446 /* A zero-length array cannot be represented with an
4447 unsigned index type, which is what we'll get with
4448 build_index_type. Create an open-ended range instead. */
4449 itype = build_range_type (sizetype, size, NULL_TREE);
4451 else
4453 /* Compute the maximum valid index, that is, size - 1.
4454 Do the calculation in index_type, so that if it is
4455 a variable the computations will be done in the
4456 proper mode. */
4457 itype = fold (build (MINUS_EXPR, index_type,
4458 convert (index_type, size),
4459 convert (index_type, size_one_node)));
4461 /* If that overflowed, the array is too big.
4462 ??? While a size of INT_MAX+1 technically shouldn't
4463 cause an overflow (because we subtract 1), the overflow
4464 is recorded during the conversion to index_type, before
4465 the subtraction. Handling this case seems like an
4466 unnecessary complication. */
4467 if (TREE_OVERFLOW (itype))
4469 error ("size of array `%s' is too large", name);
4470 type = error_mark_node;
4471 continue;
4474 if (size_varies)
4475 itype = variable_size (itype);
4476 itype = build_index_type (itype);
4479 else if (decl_context == FIELD)
4481 /* ??? Need to check somewhere that this is a structure
4482 and not a union, that this field is last, and that
4483 this structure has at least one other named member. */
4485 if (pedantic && !flag_isoc99 && !in_system_header)
4486 pedwarn ("ISO C89 does not support flexible array members");
4488 /* ISO C99 Flexible array members are effectively identical
4489 to GCC's zero-length array extension. */
4490 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4493 /* If pedantic, complain about arrays of incomplete types. */
4495 if (pedantic && !COMPLETE_TYPE_P (type))
4496 pedwarn ("array type has incomplete element type");
4498 #if 0
4499 /* We shouldn't have a function type here at all!
4500 Functions aren't allowed as array elements. */
4501 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4502 && (constp || volatilep))
4503 pedwarn ("ISO C forbids const or volatile function types");
4504 #endif
4506 /* Build the array type itself, then merge any constancy or
4507 volatility into the target type. We must do it in this order
4508 to ensure that the TYPE_MAIN_VARIANT field of the array type
4509 is set correctly. */
4511 type = build_array_type (type, itype);
4512 if (type_quals)
4513 type = c_build_qualified_type (type, type_quals);
4515 if (size_varies)
4516 C_TYPE_VARIABLE_SIZE (type) = 1;
4518 /* The GCC extension for zero-length arrays differs from
4519 ISO flexible array members in that sizeof yields zero. */
4520 if (size && integer_zerop (size))
4522 layout_type (type);
4523 TYPE_SIZE (type) = bitsize_zero_node;
4524 TYPE_SIZE_UNIT (type) = size_zero_node;
4526 if (decl_context != PARM
4527 && (array_ptr_quals != NULL_TREE || array_parm_static))
4529 error ("static or type qualifiers in non-parameter array declarator");
4530 array_ptr_quals = NULL_TREE;
4531 array_parm_static = 0;
4534 else if (TREE_CODE (declarator) == CALL_EXPR)
4536 tree arg_types;
4538 /* Declaring a function type.
4539 Make sure we have a valid type for the function to return. */
4540 if (type == error_mark_node)
4541 continue;
4543 size_varies = 0;
4545 /* Warn about some types functions can't return. */
4547 if (TREE_CODE (type) == FUNCTION_TYPE)
4549 error ("`%s' declared as function returning a function", name);
4550 type = integer_type_node;
4552 if (TREE_CODE (type) == ARRAY_TYPE)
4554 error ("`%s' declared as function returning an array", name);
4555 type = integer_type_node;
4558 /* Construct the function type and go to the next
4559 inner layer of declarator. */
4561 arg_types = grokparms (TREE_OPERAND (declarator, 1),
4562 funcdef_flag
4563 /* Say it's a definition
4564 only for the CALL_EXPR
4565 closest to the identifier. */
4566 && TREE_CODE (TREE_OPERAND (declarator, 0)) == IDENTIFIER_NODE);
4567 /* Type qualifiers before the return type of the function
4568 qualify the return type, not the function type. */
4569 if (type_quals)
4571 /* Type qualifiers on a function return type are normally
4572 permitted by the standard but have no effect, so give a
4573 warning at -W. Qualifiers on a void return type have
4574 meaning as a GNU extension, and are banned on function
4575 definitions in ISO C. FIXME: strictly we shouldn't
4576 pedwarn for qualified void return types except on function
4577 definitions, but not doing so could lead to the undesirable
4578 state of a "volatile void" function return type not being
4579 warned about, and a use of the function being compiled
4580 with GNU semantics, with no diagnostics under -pedantic. */
4581 if (VOID_TYPE_P (type) && pedantic && !in_system_header)
4582 pedwarn ("ISO C forbids qualified void function return type");
4583 else if (extra_warnings
4584 && !(VOID_TYPE_P (type)
4585 && type_quals == TYPE_QUAL_VOLATILE))
4586 warning ("type qualifiers ignored on function return type");
4588 type = c_build_qualified_type (type, type_quals);
4590 type_quals = TYPE_UNQUALIFIED;
4592 type = build_function_type (type, arg_types);
4593 declarator = TREE_OPERAND (declarator, 0);
4595 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4596 the formal parameter list of this FUNCTION_TYPE to point to
4597 the FUNCTION_TYPE node itself. */
4600 tree link;
4602 for (link = last_function_parm_tags;
4603 link;
4604 link = TREE_CHAIN (link))
4605 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4608 else if (TREE_CODE (declarator) == INDIRECT_REF)
4610 /* Merge any constancy or volatility into the target type
4611 for the pointer. */
4613 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4614 && type_quals)
4615 pedwarn ("ISO C forbids qualified function types");
4616 if (type_quals)
4617 type = c_build_qualified_type (type, type_quals);
4618 type_quals = TYPE_UNQUALIFIED;
4619 size_varies = 0;
4621 type = build_pointer_type (type);
4623 /* Process a list of type modifier keywords
4624 (such as const or volatile) that were given inside the `*'. */
4626 if (TREE_TYPE (declarator))
4628 tree typemodlist;
4629 int erred = 0;
4631 constp = 0;
4632 volatilep = 0;
4633 restrictp = 0;
4634 for (typemodlist = TREE_TYPE (declarator); typemodlist;
4635 typemodlist = TREE_CHAIN (typemodlist))
4637 tree qualifier = TREE_VALUE (typemodlist);
4639 if (C_IS_RESERVED_WORD (qualifier))
4641 if (C_RID_CODE (qualifier) == RID_CONST)
4642 constp++;
4643 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4644 volatilep++;
4645 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4646 restrictp++;
4647 else
4648 erred++;
4650 else
4651 erred++;
4654 if (erred)
4655 error ("invalid type modifier within pointer declarator");
4656 if (constp > 1 && ! flag_isoc99)
4657 pedwarn ("duplicate `const'");
4658 if (volatilep > 1 && ! flag_isoc99)
4659 pedwarn ("duplicate `volatile'");
4660 if (restrictp > 1 && ! flag_isoc99)
4661 pedwarn ("duplicate `restrict'");
4663 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4664 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4665 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4668 declarator = TREE_OPERAND (declarator, 0);
4670 else
4671 abort ();
4675 /* Now TYPE has the actual type. */
4677 /* Did array size calculations overflow? */
4679 if (TREE_CODE (type) == ARRAY_TYPE
4680 && COMPLETE_TYPE_P (type)
4681 && TREE_OVERFLOW (TYPE_SIZE (type)))
4683 error ("size of array `%s' is too large", name);
4684 /* If we proceed with the array type as it is, we'll eventually
4685 crash in tree_low_cst(). */
4686 type = error_mark_node;
4689 /* If this is declaring a typedef name, return a TYPE_DECL. */
4691 if (specbits & (1 << (int) RID_TYPEDEF))
4693 tree decl;
4694 /* Note that the grammar rejects storage classes
4695 in typenames, fields or parameters */
4696 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4697 && type_quals)
4698 pedwarn ("ISO C forbids qualified function types");
4699 if (type_quals)
4700 type = c_build_qualified_type (type, type_quals);
4701 decl = build_decl (TYPE_DECL, declarator, type);
4702 if ((specbits & (1 << (int) RID_SIGNED))
4703 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
4704 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4705 decl_attributes (&decl, returned_attrs, 0);
4706 return decl;
4709 /* Detect the case of an array type of unspecified size
4710 which came, as such, direct from a typedef name.
4711 We must copy the type, so that each identifier gets
4712 a distinct type, so that each identifier's size can be
4713 controlled separately by its own initializer. */
4715 if (type != 0 && typedef_type != 0
4716 && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0
4717 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
4719 type = build_array_type (TREE_TYPE (type), 0);
4720 if (size_varies)
4721 C_TYPE_VARIABLE_SIZE (type) = 1;
4724 /* If this is a type name (such as, in a cast or sizeof),
4725 compute the type and return it now. */
4727 if (decl_context == TYPENAME)
4729 /* Note that the grammar rejects storage classes
4730 in typenames, fields or parameters */
4731 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4732 && type_quals)
4733 pedwarn ("ISO C forbids const or volatile function types");
4734 if (type_quals)
4735 type = c_build_qualified_type (type, type_quals);
4736 decl_attributes (&type, returned_attrs, 0);
4737 return type;
4740 /* Aside from typedefs and type names (handle above),
4741 `void' at top level (not within pointer)
4742 is allowed only in public variables.
4743 We don't complain about parms either, but that is because
4744 a better error message can be made later. */
4746 if (VOID_TYPE_P (type) && decl_context != PARM
4747 && ! ((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4748 && ((specbits & (1 << (int) RID_EXTERN))
4749 || (current_binding_level == global_binding_level
4750 && !(specbits
4751 & ((1 << (int) RID_STATIC) | (1 << (int) RID_REGISTER)))))))
4753 error ("variable or field `%s' declared void", name);
4754 type = integer_type_node;
4757 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4758 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4761 tree decl;
4763 if (decl_context == PARM)
4765 tree type_as_written;
4766 tree promoted_type;
4768 /* A parameter declared as an array of T is really a pointer to T.
4769 One declared as a function is really a pointer to a function. */
4771 if (TREE_CODE (type) == ARRAY_TYPE)
4773 /* Transfer const-ness of array into that of type pointed to. */
4774 type = TREE_TYPE (type);
4775 if (type_quals)
4776 type = c_build_qualified_type (type, type_quals);
4777 type = build_pointer_type (type);
4778 type_quals = TYPE_UNQUALIFIED;
4779 if (array_ptr_quals)
4781 tree new_ptr_quals, new_ptr_attrs;
4782 int erred = 0;
4783 split_specs_attrs (array_ptr_quals, &new_ptr_quals, &new_ptr_attrs);
4784 /* We don't yet implement attributes in this context. */
4785 if (new_ptr_attrs != NULL_TREE)
4786 warning ("attributes in parameter array declarator ignored");
4788 constp = 0;
4789 volatilep = 0;
4790 restrictp = 0;
4791 for (; new_ptr_quals; new_ptr_quals = TREE_CHAIN (new_ptr_quals))
4793 tree qualifier = TREE_VALUE (new_ptr_quals);
4795 if (C_IS_RESERVED_WORD (qualifier))
4797 if (C_RID_CODE (qualifier) == RID_CONST)
4798 constp++;
4799 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4800 volatilep++;
4801 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4802 restrictp++;
4803 else
4804 erred++;
4806 else
4807 erred++;
4810 if (erred)
4811 error ("invalid type modifier within array declarator");
4813 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4814 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4815 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4817 size_varies = 0;
4819 else if (TREE_CODE (type) == FUNCTION_TYPE)
4821 if (pedantic && type_quals)
4822 pedwarn ("ISO C forbids qualified function types");
4823 if (type_quals)
4824 type = c_build_qualified_type (type, type_quals);
4825 type = build_pointer_type (type);
4826 type_quals = TYPE_UNQUALIFIED;
4828 else if (type_quals)
4829 type = c_build_qualified_type (type, type_quals);
4831 type_as_written = type;
4833 decl = build_decl (PARM_DECL, declarator, type);
4834 if (size_varies)
4835 C_DECL_VARIABLE_SIZE (decl) = 1;
4837 /* Compute the type actually passed in the parmlist,
4838 for the case where there is no prototype.
4839 (For example, shorts and chars are passed as ints.)
4840 When there is a prototype, this is overridden later. */
4842 if (type == error_mark_node)
4843 promoted_type = type;
4844 else
4846 promoted_type = simple_type_promotes_to (type);
4847 if (! promoted_type)
4848 promoted_type = type;
4851 DECL_ARG_TYPE (decl) = promoted_type;
4852 DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
4854 else if (decl_context == FIELD)
4856 /* Structure field. It may not be a function. */
4858 if (TREE_CODE (type) == FUNCTION_TYPE)
4860 error ("field `%s' declared as a function", name);
4861 type = build_pointer_type (type);
4863 else if (TREE_CODE (type) != ERROR_MARK
4864 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4866 error ("field `%s' has incomplete type", name);
4867 type = error_mark_node;
4869 /* Move type qualifiers down to element of an array. */
4870 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4872 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4873 type_quals),
4874 TYPE_DOMAIN (type));
4875 #if 0
4876 /* Leave the field const or volatile as well. */
4877 type_quals = TYPE_UNQUALIFIED;
4878 #endif
4880 decl = build_decl (FIELD_DECL, declarator, type);
4881 DECL_NONADDRESSABLE_P (decl) = bitfield;
4883 if (size_varies)
4884 C_DECL_VARIABLE_SIZE (decl) = 1;
4886 else if (TREE_CODE (type) == FUNCTION_TYPE)
4888 /* Every function declaration is "external"
4889 except for those which are inside a function body
4890 in which `auto' is used.
4891 That is a case not specified by ANSI C,
4892 and we use it for forward declarations for nested functions. */
4893 int extern_ref = (!(specbits & (1 << (int) RID_AUTO))
4894 || current_binding_level == global_binding_level);
4896 if (specbits & (1 << (int) RID_AUTO)
4897 && (pedantic || current_binding_level == global_binding_level))
4898 pedwarn ("invalid storage class for function `%s'", name);
4899 if (specbits & (1 << (int) RID_REGISTER))
4900 error ("invalid storage class for function `%s'", name);
4901 /* Function declaration not at top level.
4902 Storage classes other than `extern' are not allowed
4903 and `extern' makes no difference. */
4904 if (current_binding_level != global_binding_level
4905 && (specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_INLINE)))
4906 && pedantic)
4907 pedwarn ("invalid storage class for function `%s'", name);
4909 decl = build_decl (FUNCTION_DECL, declarator, type);
4910 decl = build_decl_attribute_variant (decl, decl_attr);
4912 DECL_LANG_SPECIFIC (decl) = (struct lang_decl *)
4913 ggc_alloc_cleared (sizeof (struct lang_decl));
4915 if (pedantic && type_quals && ! DECL_IN_SYSTEM_HEADER (decl))
4916 pedwarn ("ISO C forbids qualified function types");
4918 /* GNU C interprets a `volatile void' return type to indicate
4919 that the function does not return. */
4920 if ((type_quals & TYPE_QUAL_VOLATILE)
4921 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4922 warning ("`noreturn' function returns non-void value");
4924 if (extern_ref)
4925 DECL_EXTERNAL (decl) = 1;
4926 /* Record absence of global scope for `static' or `auto'. */
4927 TREE_PUBLIC (decl)
4928 = !(specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_AUTO)));
4930 if (defaulted_int)
4931 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4933 /* Record presence of `inline', if it is reasonable. */
4934 if (MAIN_NAME_P (declarator))
4936 if (inlinep)
4937 warning ("cannot inline function `main'");
4939 else if (inlinep)
4941 /* Assume that otherwise the function can be inlined. */
4942 DECL_DECLARED_INLINE_P (decl) = 1;
4944 /* Do not mark bare declarations as DECL_INLINE. Doing so
4945 in the presence of multiple declarations can result in
4946 the abstract origin pointing between the declarations,
4947 which will confuse dwarf2out. */
4948 if (initialized)
4950 DECL_INLINE (decl) = 1;
4951 if (specbits & (1 << (int) RID_EXTERN))
4952 current_extern_inline = 1;
4955 /* If -finline-functions, assume it can be inlined. This does
4956 two things: let the function be deferred until it is actually
4957 needed, and let dwarf2 know that the function is inlinable. */
4958 else if (flag_inline_trees == 2 && initialized)
4960 DECL_INLINE (decl) = 1;
4961 DECL_DECLARED_INLINE_P (decl) = 0;
4964 else
4966 /* It's a variable. */
4967 /* An uninitialized decl with `extern' is a reference. */
4968 int extern_ref = !initialized && (specbits & (1 << (int) RID_EXTERN));
4970 /* Move type qualifiers down to element of an array. */
4971 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4973 int saved_align = TYPE_ALIGN(type);
4974 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4975 type_quals),
4976 TYPE_DOMAIN (type));
4977 TYPE_ALIGN (type) = saved_align;
4978 #if 0 /* Leave the variable const or volatile as well. */
4979 type_quals = TYPE_UNQUALIFIED;
4980 #endif
4982 else if (type_quals)
4983 type = c_build_qualified_type (type, type_quals);
4985 decl = build_decl (VAR_DECL, declarator, type);
4986 if (size_varies)
4987 C_DECL_VARIABLE_SIZE (decl) = 1;
4989 if (inlinep)
4990 pedwarn_with_decl (decl, "variable `%s' declared `inline'");
4992 DECL_EXTERNAL (decl) = extern_ref;
4993 /* At top level, the presence of a `static' or `register' storage
4994 class specifier, or the absence of all storage class specifiers
4995 makes this declaration a definition (perhaps tentative). Also,
4996 the absence of both `static' and `register' makes it public. */
4997 if (current_binding_level == global_binding_level)
4999 TREE_PUBLIC (decl)
5000 = !(specbits
5001 & ((1 << (int) RID_STATIC) | (1 << (int) RID_REGISTER)));
5002 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
5004 /* Not at top level, only `static' makes a static definition. */
5005 else
5007 TREE_STATIC (decl) = (specbits & (1 << (int) RID_STATIC)) != 0;
5008 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
5012 /* Record `register' declaration for warnings on &
5013 and in case doing stupid register allocation. */
5015 if (specbits & (1 << (int) RID_REGISTER))
5016 DECL_REGISTER (decl) = 1;
5018 /* Record constancy and volatility. */
5019 c_apply_type_quals_to_decl (type_quals, decl);
5021 /* If a type has volatile components, it should be stored in memory.
5022 Otherwise, the fact that those components are volatile
5023 will be ignored, and would even crash the compiler. */
5024 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
5025 c_mark_addressable (decl);
5027 decl_attributes (&decl, returned_attrs, 0);
5029 return decl;
5033 /* Decode the parameter-list info for a function type or function definition.
5034 The argument is the value returned by `get_parm_info' (or made in parse.y
5035 if there is an identifier list instead of a parameter decl list).
5036 These two functions are separate because when a function returns
5037 or receives functions then each is called multiple times but the order
5038 of calls is different. The last call to `grokparms' is always the one
5039 that contains the formal parameter names of a function definition.
5041 Store in `last_function_parms' a chain of the decls of parms.
5042 Also store in `last_function_parm_tags' a chain of the struct, union,
5043 and enum tags declared among the parms.
5045 Return a list of arg types to use in the FUNCTION_TYPE for this function.
5047 FUNCDEF_FLAG is nonzero for a function definition, 0 for
5048 a mere declaration. A nonempty identifier-list gets an error message
5049 when FUNCDEF_FLAG is zero. */
5051 static tree
5052 grokparms (parms_info, funcdef_flag)
5053 tree parms_info;
5054 int funcdef_flag;
5056 tree first_parm = TREE_CHAIN (parms_info);
5058 last_function_parms = TREE_PURPOSE (parms_info);
5059 last_function_parm_tags = TREE_VALUE (parms_info);
5061 if (warn_strict_prototypes && first_parm == 0 && !funcdef_flag
5062 && !in_system_header)
5063 warning ("function declaration isn't a prototype");
5065 if (first_parm != 0
5066 && TREE_CODE (TREE_VALUE (first_parm)) == IDENTIFIER_NODE)
5068 if (! funcdef_flag)
5069 pedwarn ("parameter names (without types) in function declaration");
5071 last_function_parms = first_parm;
5072 return 0;
5074 else
5076 tree parm;
5077 tree typelt;
5078 /* We no longer test FUNCDEF_FLAG.
5079 If the arg types are incomplete in a declaration,
5080 they must include undefined tags.
5081 These tags can never be defined in the scope of the declaration,
5082 so the types can never be completed,
5083 and no call can be compiled successfully. */
5084 #if 0
5085 /* In a fcn definition, arg types must be complete. */
5086 if (funcdef_flag)
5087 #endif
5088 for (parm = last_function_parms, typelt = first_parm;
5089 parm;
5090 parm = TREE_CHAIN (parm))
5091 /* Skip over any enumeration constants declared here. */
5092 if (TREE_CODE (parm) == PARM_DECL)
5094 /* Barf if the parameter itself has an incomplete type. */
5095 tree type = TREE_VALUE (typelt);
5096 if (type == error_mark_node)
5097 continue;
5098 if (!COMPLETE_TYPE_P (type))
5100 if (funcdef_flag && DECL_NAME (parm) != 0)
5101 error ("parameter `%s' has incomplete type",
5102 IDENTIFIER_POINTER (DECL_NAME (parm)));
5103 else
5104 warning ("parameter has incomplete type");
5105 if (funcdef_flag)
5107 TREE_VALUE (typelt) = error_mark_node;
5108 TREE_TYPE (parm) = error_mark_node;
5111 #if 0
5112 /* This has been replaced by parm_tags_warning, which
5113 uses a more accurate criterion for what to warn
5114 about. */
5115 else
5117 /* Now warn if is a pointer to an incomplete type. */
5118 while (TREE_CODE (type) == POINTER_TYPE
5119 || TREE_CODE (type) == REFERENCE_TYPE)
5120 type = TREE_TYPE (type);
5121 type = TYPE_MAIN_VARIANT (type);
5122 if (!COMPLETE_TYPE_P (type))
5124 if (DECL_NAME (parm) != 0)
5125 warning ("parameter `%s' points to incomplete type",
5126 IDENTIFIER_POINTER (DECL_NAME (parm)));
5127 else
5128 warning ("parameter points to incomplete type");
5131 #endif
5132 typelt = TREE_CHAIN (typelt);
5135 return first_parm;
5139 /* Return a tree_list node with info on a parameter list just parsed.
5140 The TREE_PURPOSE is a chain of decls of those parms.
5141 The TREE_VALUE is a list of structure, union and enum tags defined.
5142 The TREE_CHAIN is a list of argument types to go in the FUNCTION_TYPE.
5143 This tree_list node is later fed to `grokparms'.
5145 VOID_AT_END nonzero means append `void' to the end of the type-list.
5146 Zero means the parmlist ended with an ellipsis so don't append `void'. */
5148 tree
5149 get_parm_info (void_at_end)
5150 int void_at_end;
5152 tree decl, t;
5153 tree types = 0;
5154 int erred = 0;
5155 tree tags = gettags ();
5156 tree parms = getdecls ();
5157 tree new_parms = 0;
5158 tree order = current_binding_level->parm_order;
5160 /* Just `void' (and no ellipsis) is special. There are really no parms.
5161 But if the `void' is qualified (by `const' or `volatile') or has a
5162 storage class specifier (`register'), then the behavior is undefined;
5163 by not counting it as the special case of `void' we will cause an
5164 error later. Typedefs for `void' are OK (see DR#157). */
5165 if (void_at_end && parms != 0
5166 && TREE_CHAIN (parms) == 0
5167 && VOID_TYPE_P (TREE_TYPE (parms))
5168 && ! TREE_THIS_VOLATILE (parms)
5169 && ! TREE_READONLY (parms)
5170 && ! DECL_REGISTER (parms)
5171 && DECL_NAME (parms) == 0)
5173 parms = NULL_TREE;
5174 storedecls (NULL_TREE);
5175 return tree_cons (NULL_TREE, NULL_TREE,
5176 tree_cons (NULL_TREE, void_type_node, NULL_TREE));
5179 /* Extract enumerator values and other non-parms declared with the parms.
5180 Likewise any forward parm decls that didn't have real parm decls. */
5181 for (decl = parms; decl;)
5183 tree next = TREE_CHAIN (decl);
5185 if (TREE_CODE (decl) != PARM_DECL)
5187 TREE_CHAIN (decl) = new_parms;
5188 new_parms = decl;
5190 else if (TREE_ASM_WRITTEN (decl))
5192 error_with_decl (decl,
5193 "parameter `%s' has just a forward declaration");
5194 TREE_CHAIN (decl) = new_parms;
5195 new_parms = decl;
5197 decl = next;
5200 /* Put the parm decls back in the order they were in in the parm list. */
5201 for (t = order; t; t = TREE_CHAIN (t))
5203 if (TREE_CHAIN (t))
5204 TREE_CHAIN (TREE_VALUE (t)) = TREE_VALUE (TREE_CHAIN (t));
5205 else
5206 TREE_CHAIN (TREE_VALUE (t)) = 0;
5209 new_parms = chainon (order ? nreverse (TREE_VALUE (order)) : 0,
5210 new_parms);
5212 /* Store the parmlist in the binding level since the old one
5213 is no longer a valid list. (We have changed the chain pointers.) */
5214 storedecls (new_parms);
5216 for (decl = new_parms; decl; decl = TREE_CHAIN (decl))
5217 /* There may also be declarations for enumerators if an enumeration
5218 type is declared among the parms. Ignore them here. */
5219 if (TREE_CODE (decl) == PARM_DECL)
5221 /* Since there is a prototype,
5222 args are passed in their declared types. */
5223 tree type = TREE_TYPE (decl);
5224 DECL_ARG_TYPE (decl) = type;
5225 if (PROMOTE_PROTOTYPES
5226 && INTEGRAL_TYPE_P (type)
5227 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
5228 DECL_ARG_TYPE (decl) = integer_type_node;
5230 types = tree_cons (NULL_TREE, TREE_TYPE (decl), types);
5231 if (VOID_TYPE_P (TREE_VALUE (types)) && ! erred
5232 && DECL_NAME (decl) == 0)
5234 error ("`void' in parameter list must be the entire list");
5235 erred = 1;
5239 if (void_at_end)
5240 return tree_cons (new_parms, tags,
5241 nreverse (tree_cons (NULL_TREE, void_type_node, types)));
5243 return tree_cons (new_parms, tags, nreverse (types));
5246 /* At end of parameter list, warn about any struct, union or enum tags
5247 defined within. Do so because these types cannot ever become complete. */
5249 void
5250 parmlist_tags_warning ()
5252 tree elt;
5253 static int already;
5255 for (elt = current_binding_level->tags; elt; elt = TREE_CHAIN (elt))
5257 enum tree_code code = TREE_CODE (TREE_VALUE (elt));
5258 /* An anonymous union parm type is meaningful as a GNU extension.
5259 So don't warn for that. */
5260 if (code == UNION_TYPE && TREE_PURPOSE (elt) == 0 && !pedantic)
5261 continue;
5262 if (TREE_PURPOSE (elt) != 0)
5264 if (code == RECORD_TYPE)
5265 warning ("`struct %s' declared inside parameter list",
5266 IDENTIFIER_POINTER (TREE_PURPOSE (elt)));
5267 else if (code == UNION_TYPE)
5268 warning ("`union %s' declared inside parameter list",
5269 IDENTIFIER_POINTER (TREE_PURPOSE (elt)));
5270 else
5271 warning ("`enum %s' declared inside parameter list",
5272 IDENTIFIER_POINTER (TREE_PURPOSE (elt)));
5274 else
5276 /* For translation these need to be separate warnings */
5277 if (code == RECORD_TYPE)
5278 warning ("anonymous struct declared inside parameter list");
5279 else if (code == UNION_TYPE)
5280 warning ("anonymous union declared inside parameter list");
5281 else
5282 warning ("anonymous enum declared inside parameter list");
5284 if (! already)
5286 warning ("its scope is only this definition or declaration, which is probably not what you want");
5287 already = 1;
5292 /* Get the struct, enum or union (CODE says which) with tag NAME.
5293 Define the tag as a forward-reference if it is not defined. */
5295 tree
5296 xref_tag (code, name)
5297 enum tree_code code;
5298 tree name;
5300 /* If a cross reference is requested, look up the type
5301 already defined for this tag and return it. */
5303 tree ref = lookup_tag (code, name, current_binding_level, 0);
5304 /* If this is the right type of tag, return what we found.
5305 (This reference will be shadowed by shadow_tag later if appropriate.)
5306 If this is the wrong type of tag, do not return it. If it was the
5307 wrong type in the same binding level, we will have had an error
5308 message already; if in a different binding level and declaring
5309 a name, pending_xref_error will give an error message; but if in a
5310 different binding level and not declaring a name, this tag should
5311 shadow the previous declaration of a different type of tag, and
5312 this would not work properly if we return the reference found.
5313 (For example, with "struct foo" in an outer scope, "union foo;"
5314 must shadow that tag with a new one of union type.) */
5315 if (ref && TREE_CODE (ref) == code)
5316 return ref;
5318 /* If no such tag is yet defined, create a forward-reference node
5319 and record it as the "definition".
5320 When a real declaration of this type is found,
5321 the forward-reference will be altered into a real type. */
5323 ref = make_node (code);
5324 if (code == ENUMERAL_TYPE)
5326 /* Give the type a default layout like unsigned int
5327 to avoid crashing if it does not get defined. */
5328 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
5329 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
5330 TYPE_USER_ALIGN (ref) = 0;
5331 TREE_UNSIGNED (ref) = 1;
5332 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
5333 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
5334 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
5337 pushtag (name, ref);
5339 return ref;
5342 /* Make sure that the tag NAME is defined *in the current binding level*
5343 at least as a forward reference.
5344 CODE says which kind of tag NAME ought to be. */
5346 tree
5347 start_struct (code, name)
5348 enum tree_code code;
5349 tree name;
5351 /* If there is already a tag defined at this binding level
5352 (as a forward reference), just return it. */
5354 tree ref = 0;
5356 if (name != 0)
5357 ref = lookup_tag (code, name, current_binding_level, 1);
5358 if (ref && TREE_CODE (ref) == code)
5360 C_TYPE_BEING_DEFINED (ref) = 1;
5361 TYPE_PACKED (ref) = flag_pack_struct;
5362 if (TYPE_FIELDS (ref))
5364 if (code == UNION_TYPE)
5365 error ("redefinition of `union %s'",
5366 IDENTIFIER_POINTER (name));
5367 else
5368 error ("redefinition of `struct %s'",
5369 IDENTIFIER_POINTER (name));
5372 return ref;
5375 /* Otherwise create a forward-reference just so the tag is in scope. */
5377 ref = make_node (code);
5378 pushtag (name, ref);
5379 C_TYPE_BEING_DEFINED (ref) = 1;
5380 TYPE_PACKED (ref) = flag_pack_struct;
5381 return ref;
5384 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
5385 of a structure component, returning a FIELD_DECL node.
5386 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node.
5388 This is done during the parsing of the struct declaration.
5389 The FIELD_DECL nodes are chained together and the lot of them
5390 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
5392 tree
5393 grokfield (filename, line, declarator, declspecs, width)
5394 const char *filename ATTRIBUTE_UNUSED;
5395 int line ATTRIBUTE_UNUSED;
5396 tree declarator, declspecs, width;
5398 tree value;
5400 if (declarator == NULL_TREE && width == NULL_TREE)
5402 /* This is an unnamed decl. We only support unnamed
5403 structs/unions, so check for other things and refuse them. */
5404 if (TREE_CODE (TREE_VALUE (declspecs)) != RECORD_TYPE
5405 && TREE_CODE (TREE_VALUE (declspecs)) != UNION_TYPE)
5407 error ("unnamed fields of type other than struct or union are not allowed");
5408 return NULL_TREE;
5412 value = grokdeclarator (declarator, declspecs, width ? BITFIELD : FIELD, 0);
5414 finish_decl (value, NULL_TREE, NULL_TREE);
5415 DECL_INITIAL (value) = width;
5417 maybe_objc_check_decl (value);
5418 return value;
5421 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5422 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5423 ATTRIBUTES are attributes to be applied to the structure. */
5425 tree
5426 finish_struct (t, fieldlist, attributes)
5427 tree t;
5428 tree fieldlist;
5429 tree attributes;
5431 tree x;
5432 int toplevel = global_binding_level == current_binding_level;
5433 int saw_named_field;
5435 /* If this type was previously laid out as a forward reference,
5436 make sure we lay it out again. */
5438 TYPE_SIZE (t) = 0;
5440 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5442 /* Nameless union parm types are useful as GCC extension. */
5443 if (! (TREE_CODE (t) == UNION_TYPE && TYPE_NAME (t) == 0) && !pedantic)
5444 /* Otherwise, warn about any struct or union def. in parmlist. */
5445 if (in_parm_level_p ())
5447 if (pedantic)
5448 pedwarn ("%s defined inside parms",
5449 TREE_CODE (t) == UNION_TYPE ? _("union") : _("structure"));
5450 else
5451 warning ("%s defined inside parms",
5452 TREE_CODE (t) == UNION_TYPE ? _("union") : _("structure"));
5455 if (pedantic)
5457 for (x = fieldlist; x; x = TREE_CHAIN (x))
5458 if (DECL_NAME (x) != 0)
5459 break;
5461 if (x == 0)
5462 pedwarn ("%s has no %s",
5463 TREE_CODE (t) == UNION_TYPE ? _("union") : _("struct"),
5464 fieldlist ? _("named members") : _("members"));
5467 /* Install struct as DECL_CONTEXT of each field decl.
5468 Also process specified field sizes,m which is found in the DECL_INITIAL.
5469 Store 0 there, except for ": 0" fields (so we can find them
5470 and delete them, below). */
5472 saw_named_field = 0;
5473 for (x = fieldlist; x; x = TREE_CHAIN (x))
5475 DECL_CONTEXT (x) = t;
5476 DECL_PACKED (x) |= TYPE_PACKED (t);
5478 /* If any field is const, the structure type is pseudo-const. */
5479 if (TREE_READONLY (x))
5480 C_TYPE_FIELDS_READONLY (t) = 1;
5481 else
5483 /* A field that is pseudo-const makes the structure likewise. */
5484 tree t1 = TREE_TYPE (x);
5485 while (TREE_CODE (t1) == ARRAY_TYPE)
5486 t1 = TREE_TYPE (t1);
5487 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5488 && C_TYPE_FIELDS_READONLY (t1))
5489 C_TYPE_FIELDS_READONLY (t) = 1;
5492 /* Any field that is volatile means variables of this type must be
5493 treated in some ways as volatile. */
5494 if (TREE_THIS_VOLATILE (x))
5495 C_TYPE_FIELDS_VOLATILE (t) = 1;
5497 /* Any field of nominal variable size implies structure is too. */
5498 if (C_DECL_VARIABLE_SIZE (x))
5499 C_TYPE_VARIABLE_SIZE (t) = 1;
5501 /* Detect invalid nested redefinition. */
5502 if (TREE_TYPE (x) == t)
5503 error ("nested redefinition of `%s'",
5504 IDENTIFIER_POINTER (TYPE_NAME (t)));
5506 /* Detect invalid bit-field size. */
5507 if (DECL_INITIAL (x))
5508 STRIP_NOPS (DECL_INITIAL (x));
5509 if (DECL_INITIAL (x))
5511 if (TREE_CODE (DECL_INITIAL (x)) == INTEGER_CST)
5512 constant_expression_warning (DECL_INITIAL (x));
5513 else
5515 error_with_decl (x,
5516 "bit-field `%s' width not an integer constant");
5517 DECL_INITIAL (x) = NULL;
5521 /* Detect invalid bit-field type. */
5522 if (DECL_INITIAL (x)
5523 && TREE_CODE (TREE_TYPE (x)) != INTEGER_TYPE
5524 && TREE_CODE (TREE_TYPE (x)) != BOOLEAN_TYPE
5525 && TREE_CODE (TREE_TYPE (x)) != ENUMERAL_TYPE)
5527 error_with_decl (x, "bit-field `%s' has invalid type");
5528 DECL_INITIAL (x) = NULL;
5531 if (DECL_INITIAL (x) && pedantic
5532 && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != integer_type_node
5533 && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != unsigned_type_node
5534 && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != c_bool_type_node
5535 /* Accept an enum that's equivalent to int or unsigned int. */
5536 && !(TREE_CODE (TREE_TYPE (x)) == ENUMERAL_TYPE
5537 && (TYPE_PRECISION (TREE_TYPE (x))
5538 == TYPE_PRECISION (integer_type_node))))
5539 pedwarn_with_decl (x, "bit-field `%s' type invalid in ISO C");
5541 /* Detect and ignore out of range field width and process valid
5542 field widths. */
5543 if (DECL_INITIAL (x))
5545 int max_width
5546 = (TYPE_MAIN_VARIANT (TREE_TYPE (x)) == c_bool_type_node
5547 ? CHAR_TYPE_SIZE : TYPE_PRECISION (TREE_TYPE (x)));
5549 if (tree_int_cst_sgn (DECL_INITIAL (x)) < 0)
5550 error_with_decl (x, "negative width in bit-field `%s'");
5551 else if (0 < compare_tree_int (DECL_INITIAL (x), max_width))
5552 pedwarn_with_decl (x, "width of `%s' exceeds its type");
5553 else if (integer_zerop (DECL_INITIAL (x)) && DECL_NAME (x) != 0)
5554 error_with_decl (x, "zero width for bit-field `%s'");
5555 else
5557 /* The test above has assured us that TREE_INT_CST_HIGH is 0. */
5558 unsigned HOST_WIDE_INT width
5559 = tree_low_cst (DECL_INITIAL (x), 1);
5561 if (TREE_CODE (TREE_TYPE (x)) == ENUMERAL_TYPE
5562 && (width < min_precision (TYPE_MIN_VALUE (TREE_TYPE (x)),
5563 TREE_UNSIGNED (TREE_TYPE (x)))
5564 || (width
5565 < min_precision (TYPE_MAX_VALUE (TREE_TYPE (x)),
5566 TREE_UNSIGNED (TREE_TYPE (x))))))
5567 warning_with_decl (x,
5568 "`%s' is narrower than values of its type");
5570 DECL_SIZE (x) = bitsize_int (width);
5571 DECL_BIT_FIELD (x) = 1;
5572 SET_DECL_C_BIT_FIELD (x);
5574 if (width == 0
5575 && ! (* targetm.ms_bitfield_layout_p) (t))
5577 /* field size 0 => force desired amount of alignment. */
5578 #ifdef EMPTY_FIELD_BOUNDARY
5579 DECL_ALIGN (x) = MAX (DECL_ALIGN (x), EMPTY_FIELD_BOUNDARY);
5580 #endif
5581 #ifdef PCC_BITFIELD_TYPE_MATTERS
5582 if (PCC_BITFIELD_TYPE_MATTERS)
5584 DECL_ALIGN (x) = MAX (DECL_ALIGN (x),
5585 TYPE_ALIGN (TREE_TYPE (x)));
5586 DECL_USER_ALIGN (x) |= TYPE_USER_ALIGN (TREE_TYPE (x));
5588 #endif
5593 else if (TREE_TYPE (x) != error_mark_node)
5595 unsigned int min_align = (DECL_PACKED (x) ? BITS_PER_UNIT
5596 : TYPE_ALIGN (TREE_TYPE (x)));
5598 /* Non-bit-fields are aligned for their type, except packed
5599 fields which require only BITS_PER_UNIT alignment. */
5600 DECL_ALIGN (x) = MAX (DECL_ALIGN (x), min_align);
5601 if (! DECL_PACKED (x))
5602 DECL_USER_ALIGN (x) |= TYPE_USER_ALIGN (TREE_TYPE (x));
5605 DECL_INITIAL (x) = 0;
5607 /* Detect flexible array member in an invalid context. */
5608 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5609 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5610 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5611 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5613 if (TREE_CODE (t) == UNION_TYPE)
5614 error_with_decl (x, "flexible array member in union");
5615 else if (TREE_CHAIN (x) != NULL_TREE)
5616 error_with_decl (x, "flexible array member not at end of struct");
5617 else if (! saw_named_field)
5618 error_with_decl (x, "flexible array member in otherwise empty struct");
5620 if (DECL_NAME (x))
5621 saw_named_field = 1;
5624 /* Delete all duplicate fields from the fieldlist */
5625 for (x = fieldlist; x && TREE_CHAIN (x);)
5626 /* Anonymous fields aren't duplicates. */
5627 if (DECL_NAME (TREE_CHAIN (x)) == 0)
5628 x = TREE_CHAIN (x);
5629 else
5631 tree y = fieldlist;
5633 while (1)
5635 if (DECL_NAME (y) == DECL_NAME (TREE_CHAIN (x)))
5636 break;
5637 if (y == x)
5638 break;
5639 y = TREE_CHAIN (y);
5641 if (DECL_NAME (y) == DECL_NAME (TREE_CHAIN (x)))
5643 error_with_decl (TREE_CHAIN (x), "duplicate member `%s'");
5644 TREE_CHAIN (x) = TREE_CHAIN (TREE_CHAIN (x));
5646 else
5647 x = TREE_CHAIN (x);
5650 /* Now we have the nearly final fieldlist. Record it,
5651 then lay out the structure or union (including the fields). */
5653 TYPE_FIELDS (t) = fieldlist;
5655 layout_type (t);
5657 /* Delete all zero-width bit-fields from the fieldlist */
5659 tree *fieldlistp = &fieldlist;
5660 while (*fieldlistp)
5661 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp))
5662 *fieldlistp = TREE_CHAIN (*fieldlistp);
5663 else
5664 fieldlistp = &TREE_CHAIN (*fieldlistp);
5667 /* Now we have the truly final field list.
5668 Store it in this type and in the variants. */
5670 TYPE_FIELDS (t) = fieldlist;
5672 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5674 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5675 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5676 TYPE_ALIGN (x) = TYPE_ALIGN (t);
5677 TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t);
5680 /* If this was supposed to be a transparent union, but we can't
5681 make it one, warn and turn off the flag. */
5682 if (TREE_CODE (t) == UNION_TYPE
5683 && TYPE_TRANSPARENT_UNION (t)
5684 && TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t)))
5686 TYPE_TRANSPARENT_UNION (t) = 0;
5687 warning ("union cannot be made transparent");
5690 /* If this structure or union completes the type of any previous
5691 variable declaration, lay it out and output its rtl. */
5693 if (current_binding_level->n_incomplete != 0)
5695 tree decl;
5696 for (decl = current_binding_level->names; decl; decl = TREE_CHAIN (decl))
5698 if (TYPE_MAIN_VARIANT (TREE_TYPE (decl)) == TYPE_MAIN_VARIANT (t)
5699 && TREE_CODE (decl) != TYPE_DECL)
5701 layout_decl (decl, 0);
5702 /* This is a no-op in c-lang.c or something real in objc-actions.c. */
5703 maybe_objc_check_decl (decl);
5704 rest_of_decl_compilation (decl, NULL, toplevel, 0);
5705 if (! toplevel)
5706 expand_decl (decl);
5707 if (--current_binding_level->n_incomplete == 0)
5708 break;
5710 else if (!COMPLETE_TYPE_P (TREE_TYPE (decl))
5711 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5713 tree element = TREE_TYPE (decl);
5714 while (TREE_CODE (element) == ARRAY_TYPE)
5715 element = TREE_TYPE (element);
5716 if (element == t)
5718 layout_array_type (TREE_TYPE (decl));
5719 if (TREE_CODE (decl) != TYPE_DECL)
5721 layout_decl (decl, 0);
5722 maybe_objc_check_decl (decl);
5723 rest_of_decl_compilation (decl, NULL, toplevel, 0);
5724 if (! toplevel)
5725 expand_decl (decl);
5727 if (--current_binding_level->n_incomplete == 0)
5728 break;
5734 /* Finish debugging output for this type. */
5735 rest_of_type_compilation (t, toplevel);
5737 return t;
5740 /* Lay out the type T, and its element type, and so on. */
5742 static void
5743 layout_array_type (t)
5744 tree t;
5746 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5747 layout_array_type (TREE_TYPE (t));
5748 layout_type (t);
5751 /* Begin compiling the definition of an enumeration type.
5752 NAME is its name (or null if anonymous).
5753 Returns the type object, as yet incomplete.
5754 Also records info about it so that build_enumerator
5755 may be used to declare the individual values as they are read. */
5757 tree
5758 start_enum (name)
5759 tree name;
5761 tree enumtype = 0;
5763 /* If this is the real definition for a previous forward reference,
5764 fill in the contents in the same object that used to be the
5765 forward reference. */
5767 if (name != 0)
5768 enumtype = lookup_tag (ENUMERAL_TYPE, name, current_binding_level, 1);
5770 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5772 enumtype = make_node (ENUMERAL_TYPE);
5773 pushtag (name, enumtype);
5776 C_TYPE_BEING_DEFINED (enumtype) = 1;
5778 if (TYPE_VALUES (enumtype) != 0)
5780 /* This enum is a named one that has been declared already. */
5781 error ("redeclaration of `enum %s'", IDENTIFIER_POINTER (name));
5783 /* Completely replace its old definition.
5784 The old enumerators remain defined, however. */
5785 TYPE_VALUES (enumtype) = 0;
5788 enum_next_value = integer_zero_node;
5789 enum_overflow = 0;
5791 if (flag_short_enums)
5792 TYPE_PACKED (enumtype) = 1;
5794 return enumtype;
5797 /* After processing and defining all the values of an enumeration type,
5798 install their decls in the enumeration type and finish it off.
5799 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5800 and ATTRIBUTES are the specified attributes.
5801 Returns ENUMTYPE. */
5803 tree
5804 finish_enum (enumtype, values, attributes)
5805 tree enumtype;
5806 tree values;
5807 tree attributes;
5809 tree pair, tem;
5810 tree minnode = 0, maxnode = 0, enum_value_type;
5811 int precision, unsign;
5812 int toplevel = (global_binding_level == current_binding_level);
5814 if (in_parm_level_p ())
5815 warning ("enum defined inside parms");
5817 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5819 /* Calculate the maximum value of any enumerator in this type. */
5821 if (values == error_mark_node)
5822 minnode = maxnode = integer_zero_node;
5823 else
5825 minnode = maxnode = TREE_VALUE (values);
5826 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5828 tree value = TREE_VALUE (pair);
5829 if (tree_int_cst_lt (maxnode, value))
5830 maxnode = value;
5831 if (tree_int_cst_lt (value, minnode))
5832 minnode = value;
5836 /* Construct the final type of this enumeration. It is the same
5837 as one of the integral types - the narrowest one that fits, except
5838 that normally we only go as narrow as int - and signed iff any of
5839 the values are negative. */
5840 unsign = (tree_int_cst_sgn (minnode) >= 0);
5841 precision = MAX (min_precision (minnode, unsign),
5842 min_precision (maxnode, unsign));
5843 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5845 tree narrowest = c_common_type_for_size (precision, unsign);
5846 if (narrowest == 0)
5848 warning ("enumeration values exceed range of largest integer");
5849 narrowest = long_long_integer_type_node;
5852 precision = TYPE_PRECISION (narrowest);
5854 else
5855 precision = TYPE_PRECISION (integer_type_node);
5857 if (precision == TYPE_PRECISION (integer_type_node))
5858 enum_value_type = c_common_type_for_size (precision, 0);
5859 else
5860 enum_value_type = enumtype;
5862 TYPE_MIN_VALUE (enumtype) = minnode;
5863 TYPE_MAX_VALUE (enumtype) = maxnode;
5864 TYPE_PRECISION (enumtype) = precision;
5865 TREE_UNSIGNED (enumtype) = unsign;
5866 TYPE_SIZE (enumtype) = 0;
5867 layout_type (enumtype);
5869 if (values != error_mark_node)
5871 /* Change the type of the enumerators to be the enum type. We
5872 need to do this irrespective of the size of the enum, for
5873 proper type checking. Replace the DECL_INITIALs of the
5874 enumerators, and the value slots of the list, with copies
5875 that have the enum type; they cannot be modified in place
5876 because they may be shared (e.g. integer_zero_node) Finally,
5877 change the purpose slots to point to the names of the decls. */
5878 for (pair = values; pair; pair = TREE_CHAIN (pair))
5880 tree enu = TREE_PURPOSE (pair);
5882 TREE_TYPE (enu) = enumtype;
5883 DECL_SIZE (enu) = TYPE_SIZE (enumtype);
5884 DECL_SIZE_UNIT (enu) = TYPE_SIZE_UNIT (enumtype);
5885 DECL_ALIGN (enu) = TYPE_ALIGN (enumtype);
5886 DECL_USER_ALIGN (enu) = TYPE_USER_ALIGN (enumtype);
5887 DECL_MODE (enu) = TYPE_MODE (enumtype);
5889 /* The ISO C Standard mandates enumerators to have type int,
5890 even though the underlying type of an enum type is
5891 unspecified. Here we convert any enumerators that fit in
5892 an int to type int, to avoid promotions to unsigned types
5893 when comparing integers with enumerators that fit in the
5894 int range. When -pedantic is given, build_enumerator()
5895 would have already taken care of those that don't fit. */
5896 if (int_fits_type_p (DECL_INITIAL (enu), enum_value_type))
5897 DECL_INITIAL (enu) = convert (enum_value_type, DECL_INITIAL (enu));
5898 else
5899 DECL_INITIAL (enu) = convert (enumtype, DECL_INITIAL (enu));
5901 TREE_PURPOSE (pair) = DECL_NAME (enu);
5902 TREE_VALUE (pair) = DECL_INITIAL (enu);
5905 TYPE_VALUES (enumtype) = values;
5908 /* Fix up all variant types of this enum type. */
5909 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5911 if (tem == enumtype)
5912 continue;
5913 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5914 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5915 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5916 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5917 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5918 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5919 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5920 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5921 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5922 TREE_UNSIGNED (tem) = TREE_UNSIGNED (enumtype);
5925 /* Finish debugging output for this type. */
5926 rest_of_type_compilation (enumtype, toplevel);
5928 return enumtype;
5931 /* Build and install a CONST_DECL for one value of the
5932 current enumeration type (one that was begun with start_enum).
5933 Return a tree-list containing the CONST_DECL and its value.
5934 Assignment of sequential values by default is handled here. */
5936 tree
5937 build_enumerator (name, value)
5938 tree name, value;
5940 tree decl, type;
5942 /* Validate and default VALUE. */
5944 /* Remove no-op casts from the value. */
5945 if (value)
5946 STRIP_TYPE_NOPS (value);
5948 if (value != 0)
5950 if (TREE_CODE (value) == INTEGER_CST)
5952 value = default_conversion (value);
5953 constant_expression_warning (value);
5955 else
5957 error ("enumerator value for `%s' not integer constant",
5958 IDENTIFIER_POINTER (name));
5959 value = 0;
5963 /* Default based on previous value. */
5964 /* It should no longer be possible to have NON_LVALUE_EXPR
5965 in the default. */
5966 if (value == 0)
5968 value = enum_next_value;
5969 if (enum_overflow)
5970 error ("overflow in enumeration values");
5973 if (pedantic && ! int_fits_type_p (value, integer_type_node))
5975 pedwarn ("ISO C restricts enumerator values to range of `int'");
5976 value = convert (integer_type_node, value);
5979 /* Set basis for default for next value. */
5980 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
5981 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5983 /* Now create a declaration for the enum value name. */
5985 type = TREE_TYPE (value);
5986 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5987 TYPE_PRECISION (integer_type_node)),
5988 (TYPE_PRECISION (type)
5989 >= TYPE_PRECISION (integer_type_node)
5990 && TREE_UNSIGNED (type)));
5992 decl = build_decl (CONST_DECL, name, type);
5993 DECL_INITIAL (decl) = convert (type, value);
5994 pushdecl (decl);
5996 return tree_cons (decl, value, NULL_TREE);
6000 /* Create the FUNCTION_DECL for a function definition.
6001 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
6002 the declaration; they describe the function's name and the type it returns,
6003 but twisted together in a fashion that parallels the syntax of C.
6005 This function creates a binding context for the function body
6006 as well as setting up the FUNCTION_DECL in current_function_decl.
6008 Returns 1 on success. If the DECLARATOR is not suitable for a function
6009 (it defines a datum instead), we return 0, which tells
6010 yyparse to report a parse error. */
6013 start_function (declspecs, declarator, attributes)
6014 tree declarator, declspecs, attributes;
6016 tree decl1, old_decl;
6017 tree restype;
6018 int old_immediate_size_expand = immediate_size_expand;
6020 current_function_returns_value = 0; /* Assume, until we see it does. */
6021 current_function_returns_null = 0;
6022 current_function_returns_abnormally = 0;
6023 warn_about_return_type = 0;
6024 current_extern_inline = 0;
6025 c_function_varargs = 0;
6026 named_labels = 0;
6027 shadowed_labels = 0;
6029 /* Don't expand any sizes in the return type of the function. */
6030 immediate_size_expand = 0;
6032 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1);
6034 /* If the declarator is not suitable for a function definition,
6035 cause a syntax error. */
6036 if (decl1 == 0)
6038 immediate_size_expand = old_immediate_size_expand;
6039 return 0;
6042 decl_attributes (&decl1, attributes, 0);
6044 /* If #pragma weak was used, mark the decl weak now. */
6045 if (current_binding_level == global_binding_level)
6046 maybe_apply_pragma_weak (decl1);
6048 if (DECL_DECLARED_INLINE_P (decl1)
6049 && DECL_UNINLINABLE (decl1)
6050 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
6051 warning_with_decl (decl1,
6052 "inline function `%s' given attribute noinline");
6054 announce_function (decl1);
6056 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
6058 error ("return type is an incomplete type");
6059 /* Make it return void instead. */
6060 TREE_TYPE (decl1)
6061 = build_function_type (void_type_node,
6062 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
6065 if (warn_about_return_type)
6066 pedwarn_c99 ("return type defaults to `int'");
6068 /* Save the parm names or decls from this function's declarator
6069 where store_parm_decls will find them. */
6070 current_function_parms = last_function_parms;
6071 current_function_parm_tags = last_function_parm_tags;
6073 /* Make the init_value nonzero so pushdecl knows this is not tentative.
6074 error_mark_node is replaced below (in poplevel) with the BLOCK. */
6075 DECL_INITIAL (decl1) = error_mark_node;
6077 /* If this definition isn't a prototype and we had a prototype declaration
6078 before, copy the arg type info from that prototype.
6079 But not if what we had before was a builtin function. */
6080 old_decl = lookup_name_current_level (DECL_NAME (decl1));
6081 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
6082 && !DECL_BUILT_IN (old_decl)
6083 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
6084 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (old_decl))))
6085 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
6087 TREE_TYPE (decl1) = TREE_TYPE (old_decl);
6088 current_function_prototype_file = DECL_SOURCE_FILE (old_decl);
6089 current_function_prototype_line = DECL_SOURCE_LINE (old_decl);
6092 /* If there is no explicit declaration, look for any out-of-scope implicit
6093 declarations. */
6094 if (old_decl == 0)
6095 old_decl = IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1));
6097 /* Optionally warn of old-fashioned def with no previous prototype. */
6098 if (warn_strict_prototypes
6099 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
6100 && !(old_decl != 0
6101 && (TYPE_ARG_TYPES (TREE_TYPE (old_decl)) != 0
6102 || (DECL_BUILT_IN (old_decl)
6103 && ! C_DECL_ANTICIPATED (old_decl)))))
6104 warning ("function declaration isn't a prototype");
6105 /* Optionally warn of any global def with no previous prototype. */
6106 else if (warn_missing_prototypes
6107 && TREE_PUBLIC (decl1)
6108 && !(old_decl != 0
6109 && (TYPE_ARG_TYPES (TREE_TYPE (old_decl)) != 0
6110 || (DECL_BUILT_IN (old_decl)
6111 && ! C_DECL_ANTICIPATED (old_decl))))
6112 && ! MAIN_NAME_P (DECL_NAME (decl1)))
6113 warning_with_decl (decl1, "no previous prototype for `%s'");
6114 /* Optionally warn of any def with no previous prototype
6115 if the function has already been used. */
6116 else if (warn_missing_prototypes
6117 && old_decl != 0 && TREE_USED (old_decl)
6118 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
6119 warning_with_decl (decl1,
6120 "`%s' was used with no prototype before its definition");
6121 /* Optionally warn of any global def with no previous declaration. */
6122 else if (warn_missing_declarations
6123 && TREE_PUBLIC (decl1)
6124 && old_decl == 0
6125 && ! MAIN_NAME_P (DECL_NAME (decl1)))
6126 warning_with_decl (decl1, "no previous declaration for `%s'");
6127 /* Optionally warn of any def with no previous declaration
6128 if the function has already been used. */
6129 else if (warn_missing_declarations
6130 && old_decl != 0 && TREE_USED (old_decl)
6131 && old_decl == IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)))
6132 warning_with_decl (decl1,
6133 "`%s' was used with no declaration before its definition");
6135 /* This is a definition, not a reference.
6136 So normally clear DECL_EXTERNAL.
6137 However, `extern inline' acts like a declaration
6138 except for defining how to inline. So set DECL_EXTERNAL in that case. */
6139 DECL_EXTERNAL (decl1) = current_extern_inline;
6141 /* This function exists in static storage.
6142 (This does not mean `static' in the C sense!) */
6143 TREE_STATIC (decl1) = 1;
6145 /* A nested function is not global. */
6146 if (current_function_decl != 0)
6147 TREE_PUBLIC (decl1) = 0;
6149 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
6150 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
6152 tree args;
6153 int argct = 0;
6155 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
6156 != integer_type_node)
6157 pedwarn_with_decl (decl1, "return type of `%s' is not `int'");
6159 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
6160 args = TREE_CHAIN (args))
6162 tree type = args ? TREE_VALUE (args) : 0;
6164 if (type == void_type_node)
6165 break;
6167 ++argct;
6168 switch (argct)
6170 case 1:
6171 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
6172 pedwarn_with_decl (decl1,
6173 "first argument of `%s' should be `int'");
6174 break;
6176 case 2:
6177 if (TREE_CODE (type) != POINTER_TYPE
6178 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
6179 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
6180 != char_type_node))
6181 pedwarn_with_decl (decl1,
6182 "second argument of `%s' should be `char **'");
6183 break;
6185 case 3:
6186 if (TREE_CODE (type) != POINTER_TYPE
6187 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
6188 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
6189 != char_type_node))
6190 pedwarn_with_decl (decl1,
6191 "third argument of `%s' should probably be `char **'");
6192 break;
6196 /* It is intentional that this message does not mention the third
6197 argument because it's only mentioned in an appendix of the
6198 standard. */
6199 if (argct > 0 && (argct < 2 || argct > 3))
6200 pedwarn_with_decl (decl1, "`%s' takes only zero or two arguments");
6202 if (! TREE_PUBLIC (decl1))
6203 pedwarn_with_decl (decl1, "`%s' is normally a non-static function");
6206 /* Record the decl so that the function name is defined.
6207 If we already have a decl for this name, and it is a FUNCTION_DECL,
6208 use the old decl. */
6210 current_function_decl = pushdecl (decl1);
6212 pushlevel (0);
6213 declare_parm_level (1);
6214 current_binding_level->subblocks_tag_transparent = 1;
6216 make_decl_rtl (current_function_decl, NULL);
6218 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
6219 /* Promote the value to int before returning it. */
6220 if (c_promoting_integer_type_p (restype))
6222 /* It retains unsignedness if not really getting wider. */
6223 if (TREE_UNSIGNED (restype)
6224 && (TYPE_PRECISION (restype)
6225 == TYPE_PRECISION (integer_type_node)))
6226 restype = unsigned_type_node;
6227 else
6228 restype = integer_type_node;
6230 DECL_RESULT (current_function_decl)
6231 = build_decl (RESULT_DECL, NULL_TREE, restype);
6233 /* If this fcn was already referenced via a block-scope `extern' decl
6234 (or an implicit decl), propagate certain information about the usage. */
6235 if (TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (current_function_decl)))
6236 TREE_ADDRESSABLE (current_function_decl) = 1;
6238 immediate_size_expand = old_immediate_size_expand;
6240 start_fname_decls ();
6242 return 1;
6245 /* Record that this function is going to be a varargs function.
6246 This is called before store_parm_decls, which is too early
6247 to call mark_varargs directly. */
6249 void
6250 c_mark_varargs ()
6252 c_function_varargs = 1;
6255 /* Store the parameter declarations into the current function declaration.
6256 This is called after parsing the parameter declarations, before
6257 digesting the body of the function.
6259 For an old-style definition, modify the function's type
6260 to specify at least the number of arguments. */
6262 void
6263 store_parm_decls ()
6265 tree fndecl = current_function_decl;
6266 tree parm;
6268 /* This is either a chain of PARM_DECLs (if a prototype was used)
6269 or a list of IDENTIFIER_NODEs (for an old-fashioned C definition). */
6270 tree specparms = current_function_parms;
6272 /* This is a list of types declared among parms in a prototype. */
6273 tree parmtags = current_function_parm_tags;
6275 /* This is a chain of PARM_DECLs from old-style parm declarations. */
6276 tree parmdecls = getdecls ();
6278 /* This is a chain of any other decls that came in among the parm
6279 declarations. If a parm is declared with enum {foo, bar} x;
6280 then CONST_DECLs for foo and bar are put here. */
6281 tree nonparms = 0;
6283 /* The function containing FNDECL, if any. */
6284 tree context = decl_function_context (fndecl);
6286 /* Nonzero if this definition is written with a prototype. */
6287 int prototype = 0;
6289 int saved_warn_shadow = warn_shadow;
6291 /* Don't re-emit shadow warnings. */
6292 warn_shadow = 0;
6294 if (specparms != 0 && TREE_CODE (specparms) != TREE_LIST)
6296 /* This case is when the function was defined with an ANSI prototype.
6297 The parms already have decls, so we need not do anything here
6298 except record them as in effect
6299 and complain if any redundant old-style parm decls were written. */
6301 tree next;
6302 tree others = 0;
6304 prototype = 1;
6306 if (parmdecls != 0)
6308 tree decl, link;
6310 error_with_decl (fndecl,
6311 "parm types given both in parmlist and separately");
6312 /* Get rid of the erroneous decls; don't keep them on
6313 the list of parms, since they might not be PARM_DECLs. */
6314 for (decl = current_binding_level->names;
6315 decl; decl = TREE_CHAIN (decl))
6316 if (DECL_NAME (decl))
6317 IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) = 0;
6318 for (link = current_binding_level->shadowed;
6319 link; link = TREE_CHAIN (link))
6320 IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
6321 current_binding_level->names = 0;
6322 current_binding_level->shadowed = 0;
6325 specparms = nreverse (specparms);
6326 for (parm = specparms; parm; parm = next)
6328 next = TREE_CHAIN (parm);
6329 if (TREE_CODE (parm) == PARM_DECL)
6331 if (DECL_NAME (parm) == 0)
6332 error_with_decl (parm, "parameter name omitted");
6333 else if (TREE_CODE (TREE_TYPE (parm)) != ERROR_MARK
6334 && VOID_TYPE_P (TREE_TYPE (parm)))
6336 error_with_decl (parm, "parameter `%s' declared void");
6337 /* Change the type to error_mark_node so this parameter
6338 will be ignored by assign_parms. */
6339 TREE_TYPE (parm) = error_mark_node;
6341 pushdecl (parm);
6343 else
6345 /* If we find an enum constant or a type tag,
6346 put it aside for the moment. */
6347 TREE_CHAIN (parm) = 0;
6348 others = chainon (others, parm);
6352 /* Get the decls in their original chain order
6353 and record in the function. */
6354 DECL_ARGUMENTS (fndecl) = getdecls ();
6356 #if 0
6357 /* If this function takes a variable number of arguments,
6358 add a phony parameter to the end of the parm list,
6359 to represent the position of the first unnamed argument. */
6360 if (TREE_VALUE (tree_last (TYPE_ARG_TYPES (TREE_TYPE (fndecl))))
6361 != void_type_node)
6363 tree dummy = build_decl (PARM_DECL, NULL_TREE, void_type_node);
6364 /* Let's hope the address of the unnamed parm
6365 won't depend on its type. */
6366 TREE_TYPE (dummy) = integer_type_node;
6367 DECL_ARG_TYPE (dummy) = integer_type_node;
6368 DECL_ARGUMENTS (fndecl) = chainon (DECL_ARGUMENTS (fndecl), dummy);
6370 #endif
6372 /* Now pushdecl the enum constants. */
6373 for (parm = others; parm; parm = next)
6375 next = TREE_CHAIN (parm);
6376 if (DECL_NAME (parm) == 0)
6378 else if (TYPE_MAIN_VARIANT (TREE_TYPE (parm)) == void_type_node)
6380 else if (TREE_CODE (parm) != PARM_DECL)
6381 pushdecl (parm);
6384 storetags (chainon (parmtags, gettags ()));
6386 else
6388 /* SPECPARMS is an identifier list--a chain of TREE_LIST nodes
6389 each with a parm name as the TREE_VALUE.
6391 PARMDECLS is a chain of declarations for parameters.
6392 Warning! It can also contain CONST_DECLs which are not parameters
6393 but are names of enumerators of any enum types
6394 declared among the parameters.
6396 First match each formal parameter name with its declaration.
6397 Associate decls with the names and store the decls
6398 into the TREE_PURPOSE slots. */
6400 /* We use DECL_WEAK as a flag to show which parameters have been
6401 seen already since it is not used on PARM_DECL or CONST_DECL. */
6402 for (parm = parmdecls; parm; parm = TREE_CHAIN (parm))
6403 DECL_WEAK (parm) = 0;
6405 for (parm = specparms; parm; parm = TREE_CHAIN (parm))
6407 tree tail, found = NULL;
6409 if (TREE_VALUE (parm) == 0)
6411 error_with_decl (fndecl,
6412 "parameter name missing from parameter list");
6413 TREE_PURPOSE (parm) = 0;
6414 continue;
6417 /* See if any of the parmdecls specifies this parm by name.
6418 Ignore any enumerator decls. */
6419 for (tail = parmdecls; tail; tail = TREE_CHAIN (tail))
6420 if (DECL_NAME (tail) == TREE_VALUE (parm)
6421 && TREE_CODE (tail) == PARM_DECL)
6423 found = tail;
6424 break;
6427 /* If declaration already marked, we have a duplicate name.
6428 Complain, and don't use this decl twice. */
6429 if (found && DECL_WEAK (found))
6431 error_with_decl (found, "multiple parameters named `%s'");
6432 found = 0;
6435 /* If the declaration says "void", complain and ignore it. */
6436 if (found && VOID_TYPE_P (TREE_TYPE (found)))
6438 error_with_decl (found, "parameter `%s' declared void");
6439 TREE_TYPE (found) = integer_type_node;
6440 DECL_ARG_TYPE (found) = integer_type_node;
6441 layout_decl (found, 0);
6444 /* If no declaration found, default to int. */
6445 if (!found)
6447 found = build_decl (PARM_DECL, TREE_VALUE (parm),
6448 integer_type_node);
6449 DECL_ARG_TYPE (found) = TREE_TYPE (found);
6450 DECL_SOURCE_LINE (found) = DECL_SOURCE_LINE (fndecl);
6451 DECL_SOURCE_FILE (found) = DECL_SOURCE_FILE (fndecl);
6452 if (flag_isoc99)
6453 pedwarn_with_decl (found, "type of `%s' defaults to `int'");
6454 else if (extra_warnings)
6455 warning_with_decl (found, "type of `%s' defaults to `int'");
6456 pushdecl (found);
6459 TREE_PURPOSE (parm) = found;
6461 /* Mark this decl as "already found". */
6462 DECL_WEAK (found) = 1;
6465 /* Put anything which is on the parmdecls chain and which is
6466 not a PARM_DECL onto the list NONPARMS. (The types of
6467 non-parm things which might appear on the list include
6468 enumerators and NULL-named TYPE_DECL nodes.) Complain about
6469 any actual PARM_DECLs not matched with any names. */
6471 nonparms = 0;
6472 for (parm = parmdecls; parm;)
6474 tree next = TREE_CHAIN (parm);
6475 TREE_CHAIN (parm) = 0;
6477 if (TREE_CODE (parm) != PARM_DECL)
6478 nonparms = chainon (nonparms, parm);
6479 else
6481 /* Complain about args with incomplete types. */
6482 if (!COMPLETE_TYPE_P (TREE_TYPE (parm)))
6484 error_with_decl (parm, "parameter `%s' has incomplete type");
6485 TREE_TYPE (parm) = error_mark_node;
6488 if (! DECL_WEAK (parm))
6490 error_with_decl (parm,
6491 "declaration for parameter `%s' but no such parameter");
6492 /* Pretend the parameter was not missing.
6493 This gets us to a standard state and minimizes
6494 further error messages. */
6495 specparms
6496 = chainon (specparms,
6497 tree_cons (parm, NULL_TREE, NULL_TREE));
6501 parm = next;
6504 /* Chain the declarations together in the order of the list of
6505 names. Store that chain in the function decl, replacing the
6506 list of names. */
6507 parm = specparms;
6508 DECL_ARGUMENTS (fndecl) = 0;
6510 tree last;
6511 for (last = 0; parm; parm = TREE_CHAIN (parm))
6512 if (TREE_PURPOSE (parm))
6514 if (last == 0)
6515 DECL_ARGUMENTS (fndecl) = TREE_PURPOSE (parm);
6516 else
6517 TREE_CHAIN (last) = TREE_PURPOSE (parm);
6518 last = TREE_PURPOSE (parm);
6519 TREE_CHAIN (last) = 0;
6523 /* If there was a previous prototype,
6524 set the DECL_ARG_TYPE of each argument according to
6525 the type previously specified, and report any mismatches. */
6527 if (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
6529 tree type;
6530 for (parm = DECL_ARGUMENTS (fndecl),
6531 type = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6532 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
6533 != void_type_node));
6534 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6536 if (parm == 0 || type == 0
6537 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6539 error ("number of arguments doesn't match prototype");
6540 error_with_file_and_line (current_function_prototype_file,
6541 current_function_prototype_line,
6542 "prototype declaration");
6543 break;
6545 /* Type for passing arg must be consistent with that
6546 declared for the arg. ISO C says we take the unqualified
6547 type for parameters declared with qualified type. */
6548 if (! comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6549 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
6551 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6552 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6554 /* Adjust argument to match prototype. E.g. a previous
6555 `int foo(float);' prototype causes
6556 `int foo(x) float x; {...}' to be treated like
6557 `int foo(float x) {...}'. This is particularly
6558 useful for argument types like uid_t. */
6559 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6561 if (PROMOTE_PROTOTYPES
6562 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6563 && TYPE_PRECISION (TREE_TYPE (parm))
6564 < TYPE_PRECISION (integer_type_node))
6565 DECL_ARG_TYPE (parm) = integer_type_node;
6567 if (pedantic)
6569 pedwarn ("promoted argument `%s' doesn't match prototype",
6570 IDENTIFIER_POINTER (DECL_NAME (parm)));
6571 warning_with_file_and_line
6572 (current_function_prototype_file,
6573 current_function_prototype_line,
6574 "prototype declaration");
6577 else
6579 error ("argument `%s' doesn't match prototype",
6580 IDENTIFIER_POINTER (DECL_NAME (parm)));
6581 error_with_file_and_line (current_function_prototype_file,
6582 current_function_prototype_line,
6583 "prototype declaration");
6587 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6590 /* Otherwise, create a prototype that would match. */
6592 else
6594 tree actual = 0, last = 0, type;
6596 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6598 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6599 if (last)
6600 TREE_CHAIN (last) = type;
6601 else
6602 actual = type;
6603 last = type;
6605 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6606 if (last)
6607 TREE_CHAIN (last) = type;
6608 else
6609 actual = type;
6611 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6612 of the type of this function, but we need to avoid having this
6613 affect the types of other similarly-typed functions, so we must
6614 first force the generation of an identical (but separate) type
6615 node for the relevant function type. The new node we create
6616 will be a variant of the main variant of the original function
6617 type. */
6619 TREE_TYPE (fndecl) = build_type_copy (TREE_TYPE (fndecl));
6621 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6624 /* Now store the final chain of decls for the arguments
6625 as the decl-chain of the current lexical scope.
6626 Put the enumerators in as well, at the front so that
6627 DECL_ARGUMENTS is not modified. */
6629 storedecls (chainon (nonparms, DECL_ARGUMENTS (fndecl)));
6632 /* Make sure the binding level for the top of the function body
6633 gets a BLOCK if there are any in the function.
6634 Otherwise, the dbx output is wrong. */
6636 keep_next_if_subblocks = 1;
6638 /* ??? This might be an improvement,
6639 but needs to be thought about some more. */
6640 #if 0
6641 keep_next_level_flag = 1;
6642 #endif
6644 /* Write a record describing this function definition to the prototypes
6645 file (if requested). */
6647 gen_aux_info_record (fndecl, 1, 0, prototype);
6649 /* Initialize the RTL code for the function. */
6650 init_function_start (fndecl, input_filename, lineno);
6652 /* Begin the statement tree for this function. */
6653 begin_stmt_tree (&DECL_SAVED_TREE (current_function_decl));
6655 /* If this is a nested function, save away the sizes of any
6656 variable-size types so that we can expand them when generating
6657 RTL. */
6658 if (context)
6660 tree t;
6662 DECL_LANG_SPECIFIC (fndecl)->pending_sizes
6663 = nreverse (get_pending_sizes ());
6664 for (t = DECL_LANG_SPECIFIC (fndecl)->pending_sizes;
6666 t = TREE_CHAIN (t))
6667 SAVE_EXPR_CONTEXT (TREE_VALUE (t)) = context;
6670 /* This function is being processed in whole-function mode. */
6671 cfun->x_whole_function_mode_p = 1;
6673 /* Even though we're inside a function body, we still don't want to
6674 call expand_expr to calculate the size of a variable-sized array.
6675 We haven't necessarily assigned RTL to all variables yet, so it's
6676 not safe to try to expand expressions involving them. */
6677 immediate_size_expand = 0;
6678 cfun->x_dont_save_pending_sizes_p = 1;
6680 warn_shadow = saved_warn_shadow;
6683 /* Finish up a function declaration and compile that function
6684 all the way to assembler language output. The free the storage
6685 for the function definition.
6687 This is called after parsing the body of the function definition.
6689 NESTED is nonzero if the function being finished is nested in another.
6690 CAN_DEFER_P is nonzero if the function may be deferred. */
6692 void
6693 finish_function (nested, can_defer_p)
6694 int nested;
6695 int can_defer_p;
6697 tree fndecl = current_function_decl;
6699 #if 0
6700 /* This caused &foo to be of type ptr-to-const-function which then
6701 got a warning when stored in a ptr-to-function variable. */
6702 TREE_READONLY (fndecl) = 1;
6703 #endif
6705 poplevel (1, 0, 1);
6706 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6708 /* Must mark the RESULT_DECL as being in this function. */
6710 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6712 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6714 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6715 != integer_type_node)
6717 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6718 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6719 if (! warn_main)
6720 pedwarn_with_decl (fndecl, "return type of `%s' is not `int'");
6722 else
6724 #ifdef DEFAULT_MAIN_RETURN
6725 /* Make it so that `main' always returns success by default. */
6726 DEFAULT_MAIN_RETURN;
6727 #else
6728 if (flag_isoc99)
6729 c_expand_return (integer_zero_node);
6730 #endif
6734 finish_fname_decls ();
6736 /* Tie off the statement tree for this function. */
6737 finish_stmt_tree (&DECL_SAVED_TREE (fndecl));
6739 /* Complain if there's just no return statement. */
6740 if (warn_return_type
6741 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6742 && !current_function_returns_value && !current_function_returns_null
6743 /* Don't complain if we abort. */
6744 && !current_function_returns_abnormally
6745 /* Don't warn for main(). */
6746 && !MAIN_NAME_P (DECL_NAME (fndecl))
6747 /* Or if they didn't actually specify a return type. */
6748 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6749 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6750 inline function, as we might never be compiled separately. */
6751 && DECL_INLINE (fndecl))
6752 warning ("no return statement in function returning non-void");
6754 /* Clear out memory we no longer need. */
6755 free_after_parsing (cfun);
6756 /* Since we never call rest_of_compilation, we never clear
6757 CFUN. Do so explicitly. */
6758 free_after_compilation (cfun);
6759 cfun = NULL;
6761 if (! nested)
6763 /* Generate RTL for the body of this function. */
6764 c_expand_body (fndecl, nested, can_defer_p);
6766 /* Let the error reporting routines know that we're outside a
6767 function. For a nested function, this value is used in
6768 pop_c_function_context and then reset via pop_function_context. */
6769 current_function_decl = NULL;
6773 /* Generate the RTL for a deferred function FNDECL. */
6775 void
6776 c_expand_deferred_function (fndecl)
6777 tree fndecl;
6779 /* DECL_INLINE or DECL_RESULT might got cleared after the inline
6780 function was deferred, e.g. in duplicate_decls. */
6781 if (DECL_INLINE (fndecl) && DECL_RESULT (fndecl))
6783 c_expand_body (fndecl, 0, 0);
6784 current_function_decl = NULL;
6788 /* Generate the RTL for the body of FNDECL. If NESTED_P is non-zero,
6789 then we are already in the process of generating RTL for another
6790 function. If can_defer_p is zero, we won't attempt to defer the
6791 generation of RTL. */
6793 static void
6794 c_expand_body (fndecl, nested_p, can_defer_p)
6795 tree fndecl;
6796 int nested_p, can_defer_p;
6798 int uninlinable = 1;
6800 /* There's no reason to do any of the work here if we're only doing
6801 semantic analysis; this code just generates RTL. */
6802 if (flag_syntax_only)
6803 return;
6805 if (flag_inline_trees)
6807 /* First, cache whether the current function is inlinable. Some
6808 predicates depend on cfun and current_function_decl to
6809 function completely. */
6810 timevar_push (TV_INTEGRATION);
6811 uninlinable = ! tree_inlinable_function_p (fndecl);
6813 if (! uninlinable && can_defer_p
6814 /* Save function tree for inlining. Should return 0 if the
6815 language does not support function deferring or the
6816 function could not be deferred. */
6817 && defer_fn (fndecl))
6819 /* Let the back-end know that this function exists. */
6820 (*debug_hooks->deferred_inline_function) (fndecl);
6821 timevar_pop (TV_INTEGRATION);
6822 return;
6825 /* Then, inline any functions called in it. */
6826 optimize_inline_calls (fndecl);
6827 timevar_pop (TV_INTEGRATION);
6830 timevar_push (TV_EXPAND);
6832 if (nested_p)
6834 /* Make sure that we will evaluate variable-sized types involved
6835 in our function's type. */
6836 expand_pending_sizes (DECL_LANG_SPECIFIC (fndecl)->pending_sizes);
6837 /* Squirrel away our current state. */
6838 push_function_context ();
6841 /* Initialize the RTL code for the function. */
6842 current_function_decl = fndecl;
6843 input_filename = DECL_SOURCE_FILE (fndecl);
6844 init_function_start (fndecl, input_filename, DECL_SOURCE_LINE (fndecl));
6846 /* This function is being processed in whole-function mode. */
6847 cfun->x_whole_function_mode_p = 1;
6849 /* Even though we're inside a function body, we still don't want to
6850 call expand_expr to calculate the size of a variable-sized array.
6851 We haven't necessarily assigned RTL to all variables yet, so it's
6852 not safe to try to expand expressions involving them. */
6853 immediate_size_expand = 0;
6854 cfun->x_dont_save_pending_sizes_p = 1;
6856 /* If this is a varargs function, inform function.c. */
6857 if (c_function_varargs)
6858 mark_varargs ();
6860 /* Set up parameters and prepare for return, for the function. */
6861 expand_function_start (fndecl, 0);
6863 /* If this function is `main', emit a call to `__main'
6864 to run global initializers, etc. */
6865 if (DECL_NAME (fndecl)
6866 && MAIN_NAME_P (DECL_NAME (fndecl))
6867 && DECL_CONTEXT (fndecl) == NULL_TREE)
6868 expand_main_function ();
6870 /* Generate the RTL for this function. */
6871 expand_stmt (DECL_SAVED_TREE (fndecl));
6872 if (uninlinable)
6874 /* Allow the body of the function to be garbage collected. */
6875 DECL_SAVED_TREE (fndecl) = NULL_TREE;
6878 /* We hard-wired immediate_size_expand to zero above.
6879 expand_function_end will decrement this variable. So, we set the
6880 variable to one here, so that after the decrement it will remain
6881 zero. */
6882 immediate_size_expand = 1;
6884 /* Allow language dialects to perform special processing. */
6885 if (lang_expand_function_end)
6886 (*lang_expand_function_end) ();
6888 /* Generate rtl for function exit. */
6889 expand_function_end (input_filename, lineno, 0);
6891 /* If this is a nested function, protect the local variables in the stack
6892 above us from being collected while we're compiling this function. */
6893 if (nested_p)
6894 ggc_push_context ();
6896 /* Run the optimizers and output the assembler code for this function. */
6897 rest_of_compilation (fndecl);
6899 /* Undo the GC context switch. */
6900 if (nested_p)
6901 ggc_pop_context ();
6903 /* With just -W, complain only if function returns both with
6904 and without a value. */
6905 if (extra_warnings
6906 && current_function_returns_value
6907 && current_function_returns_null)
6908 warning ("this function may return with or without a value");
6910 /* If requested, warn about function definitions where the function will
6911 return a value (usually of some struct or union type) which itself will
6912 take up a lot of stack space. */
6914 if (warn_larger_than && !DECL_EXTERNAL (fndecl) && TREE_TYPE (fndecl))
6916 tree ret_type = TREE_TYPE (TREE_TYPE (fndecl));
6918 if (ret_type && TYPE_SIZE_UNIT (ret_type)
6919 && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
6920 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type),
6921 larger_than_size))
6923 unsigned int size_as_int
6924 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
6926 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
6927 warning_with_decl (fndecl,
6928 "size of return value of `%s' is %u bytes",
6929 size_as_int);
6930 else
6931 warning_with_decl (fndecl,
6932 "size of return value of `%s' is larger than %d bytes",
6933 larger_than_size);
6937 if (DECL_SAVED_INSNS (fndecl) == 0 && ! nested_p
6938 && ! flag_inline_trees)
6940 /* Stop pointing to the local nodes about to be freed.
6941 But DECL_INITIAL must remain nonzero so we know this
6942 was an actual function definition.
6943 For a nested function, this is done in pop_c_function_context.
6944 If rest_of_compilation set this to 0, leave it 0. */
6945 if (DECL_INITIAL (fndecl) != 0)
6946 DECL_INITIAL (fndecl) = error_mark_node;
6948 DECL_ARGUMENTS (fndecl) = 0;
6951 if (DECL_STATIC_CONSTRUCTOR (fndecl))
6953 if (targetm.have_ctors_dtors)
6954 (* targetm.asm_out.constructor) (XEXP (DECL_RTL (fndecl), 0),
6955 DEFAULT_INIT_PRIORITY);
6956 else
6957 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6960 if (DECL_STATIC_DESTRUCTOR (fndecl))
6962 if (targetm.have_ctors_dtors)
6963 (* targetm.asm_out.destructor) (XEXP (DECL_RTL (fndecl), 0),
6964 DEFAULT_INIT_PRIORITY);
6965 else
6966 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6969 if (nested_p)
6970 /* Return to the enclosing function. */
6971 pop_function_context ();
6972 timevar_pop (TV_EXPAND);
6975 /* Check the declarations given in a for-loop for satisfying the C99
6976 constraints. */
6977 void
6978 check_for_loop_decls ()
6980 tree t;
6982 if (!flag_isoc99)
6984 /* If we get here, declarations have been used in a for loop without
6985 the C99 for loop scope. This doesn't make much sense, so don't
6986 allow it. */
6987 error ("`for' loop initial declaration used outside C99 mode");
6988 return;
6990 /* C99 subclause 6.8.5 paragraph 3:
6992 [#3] The declaration part of a for statement shall only
6993 declare identifiers for objects having storage class auto or
6994 register.
6996 It isn't clear whether, in this sentence, "identifiers" binds to
6997 "shall only declare" or to "objects" - that is, whether all identifiers
6998 declared must be identifiers for objects, or whether the restriction
6999 only applies to those that are. (A question on this in comp.std.c
7000 in November 2000 received no answer.) We implement the strictest
7001 interpretation, to avoid creating an extension which later causes
7002 problems. */
7004 for (t = gettags (); t; t = TREE_CHAIN (t))
7006 if (TREE_PURPOSE (t) != 0)
7008 enum tree_code code = TREE_CODE (TREE_VALUE (t));
7010 if (code == RECORD_TYPE)
7011 error ("`struct %s' declared in `for' loop initial declaration",
7012 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
7013 else if (code == UNION_TYPE)
7014 error ("`union %s' declared in `for' loop initial declaration",
7015 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
7016 else
7017 error ("`enum %s' declared in `for' loop initial declaration",
7018 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
7022 for (t = getdecls (); t; t = TREE_CHAIN (t))
7024 if (TREE_CODE (t) != VAR_DECL && DECL_NAME (t))
7025 error_with_decl (t, "declaration of non-variable `%s' in `for' loop initial declaration");
7026 else if (TREE_STATIC (t))
7027 error_with_decl (t, "declaration of static variable `%s' in `for' loop initial declaration");
7028 else if (DECL_EXTERNAL (t))
7029 error_with_decl (t, "declaration of `extern' variable `%s' in `for' loop initial declaration");
7033 /* Save and restore the variables in this file and elsewhere
7034 that keep track of the progress of compilation of the current function.
7035 Used for nested functions. */
7037 struct c_language_function
7039 struct language_function base;
7040 tree named_labels;
7041 tree shadowed_labels;
7042 int returns_value;
7043 int returns_null;
7044 int returns_abnormally;
7045 int warn_about_return_type;
7046 int extern_inline;
7047 struct binding_level *binding_level;
7050 /* Save and reinitialize the variables
7051 used during compilation of a C function. */
7053 void
7054 push_c_function_context (f)
7055 struct function *f;
7057 struct c_language_function *p;
7058 p = ((struct c_language_function *)
7059 xmalloc (sizeof (struct c_language_function)));
7060 f->language = (struct language_function *) p;
7062 p->base.x_stmt_tree = c_stmt_tree;
7063 p->base.x_scope_stmt_stack = c_scope_stmt_stack;
7064 p->named_labels = named_labels;
7065 p->shadowed_labels = shadowed_labels;
7066 p->returns_value = current_function_returns_value;
7067 p->returns_null = current_function_returns_null;
7068 p->returns_abnormally = current_function_returns_abnormally;
7069 p->warn_about_return_type = warn_about_return_type;
7070 p->extern_inline = current_extern_inline;
7071 p->binding_level = current_binding_level;
7074 /* Restore the variables used during compilation of a C function. */
7076 void
7077 pop_c_function_context (f)
7078 struct function *f;
7080 struct c_language_function *p
7081 = (struct c_language_function *) f->language;
7082 tree link;
7084 /* Bring back all the labels that were shadowed. */
7085 for (link = shadowed_labels; link; link = TREE_CHAIN (link))
7086 if (DECL_NAME (TREE_VALUE (link)) != 0)
7087 IDENTIFIER_LABEL_VALUE (DECL_NAME (TREE_VALUE (link)))
7088 = TREE_VALUE (link);
7090 if (DECL_SAVED_INSNS (current_function_decl) == 0
7091 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
7093 /* Stop pointing to the local nodes about to be freed. */
7094 /* But DECL_INITIAL must remain nonzero so we know this
7095 was an actual function definition. */
7096 DECL_INITIAL (current_function_decl) = error_mark_node;
7097 DECL_ARGUMENTS (current_function_decl) = 0;
7100 c_stmt_tree = p->base.x_stmt_tree;
7101 c_scope_stmt_stack = p->base.x_scope_stmt_stack;
7102 named_labels = p->named_labels;
7103 shadowed_labels = p->shadowed_labels;
7104 current_function_returns_value = p->returns_value;
7105 current_function_returns_null = p->returns_null;
7106 current_function_returns_abnormally = p->returns_abnormally;
7107 warn_about_return_type = p->warn_about_return_type;
7108 current_extern_inline = p->extern_inline;
7109 current_binding_level = p->binding_level;
7111 free (p);
7112 f->language = 0;
7115 /* Mark the language specific parts of F for GC. */
7117 void
7118 mark_c_function_context (f)
7119 struct function *f;
7121 struct c_language_function *p
7122 = (struct c_language_function *) f->language;
7124 if (p == 0)
7125 return;
7127 mark_c_language_function (&p->base);
7128 ggc_mark_tree (p->shadowed_labels);
7129 ggc_mark_tree (p->named_labels);
7130 mark_binding_level (&p->binding_level);
7133 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
7135 void
7136 c_dup_lang_specific_decl (decl)
7137 tree decl;
7139 struct lang_decl *ld;
7141 if (!DECL_LANG_SPECIFIC (decl))
7142 return;
7144 ld = (struct lang_decl *) ggc_alloc (sizeof (struct lang_decl));
7145 memcpy ((char *) ld, (char *) DECL_LANG_SPECIFIC (decl),
7146 sizeof (struct lang_decl));
7147 DECL_LANG_SPECIFIC (decl) = ld;
7150 /* Mark the language specific bits in T for GC. */
7152 void
7153 c_mark_tree (t)
7154 tree t;
7156 if (TREE_CODE (t) == IDENTIFIER_NODE)
7158 struct lang_identifier *i = (struct lang_identifier *) t;
7159 ggc_mark_tree (i->global_value);
7160 ggc_mark_tree (i->local_value);
7161 ggc_mark_tree (i->label_value);
7162 ggc_mark_tree (i->implicit_decl);
7163 ggc_mark_tree (i->error_locus);
7164 ggc_mark_tree (i->limbo_value);
7166 else if (TYPE_P (t) && TYPE_LANG_SPECIFIC (t))
7167 ggc_mark (TYPE_LANG_SPECIFIC (t));
7168 else if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
7170 ggc_mark (DECL_LANG_SPECIFIC (t));
7171 c_mark_lang_decl (&DECL_LANG_SPECIFIC (t)->base);
7172 ggc_mark_tree (DECL_LANG_SPECIFIC (t)->pending_sizes);
7176 /* The functions below are required for functionality of doing
7177 function at once processing in the C front end. Currently these
7178 functions are not called from anywhere in the C front end, but as
7179 these changes continue, that will change. */
7181 /* Returns non-zero if the current statement is a full expression,
7182 i.e. temporaries created during that statement should be destroyed
7183 at the end of the statement. */
7186 stmts_are_full_exprs_p ()
7188 return 0;
7191 /* Returns the stmt_tree (if any) to which statements are currently
7192 being added. If there is no active statement-tree, NULL is
7193 returned. */
7195 stmt_tree
7196 current_stmt_tree ()
7198 return &c_stmt_tree;
7201 /* Returns the stack of SCOPE_STMTs for the current function. */
7203 tree *
7204 current_scope_stmt_stack ()
7206 return &c_scope_stmt_stack;
7209 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
7210 C. */
7213 anon_aggr_type_p (node)
7214 tree node ATTRIBUTE_UNUSED;
7216 return 0;
7219 /* Dummy function in place of callback used by C++. */
7221 void
7222 extract_interface_info ()
7226 /* Return a new COMPOUND_STMT, after adding it to the current
7227 statement tree. */
7229 tree
7230 c_begin_compound_stmt ()
7232 tree stmt;
7234 /* Create the COMPOUND_STMT. */
7235 stmt = add_stmt (build_stmt (COMPOUND_STMT, NULL_TREE));
7237 return stmt;
7240 /* Expand T (a DECL_STMT) if it declares an entity not handled by the
7241 common code. */
7243 void
7244 c_expand_decl_stmt (t)
7245 tree t;
7247 tree decl = DECL_STMT_DECL (t);
7249 /* Expand nested functions. */
7250 if (TREE_CODE (decl) == FUNCTION_DECL
7251 && DECL_CONTEXT (decl) == current_function_decl
7252 && DECL_SAVED_TREE (decl))
7253 c_expand_body (decl, /*nested_p=*/1, /*can_defer_p=*/0);
7256 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
7257 the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */
7259 tree
7260 identifier_global_value (t)
7261 tree t;
7263 return IDENTIFIER_GLOBAL_VALUE (t);
7266 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
7267 otherwise the name is found in ridpointers from RID_INDEX. */
7269 void
7270 record_builtin_type (rid_index, name, type)
7271 enum rid rid_index;
7272 const char *name;
7273 tree type;
7275 tree id;
7276 if (name == 0)
7277 id = ridpointers[(int) rid_index];
7278 else
7279 id = get_identifier (name);
7280 pushdecl (build_decl (TYPE_DECL, id, type));
7283 /* Build the void_list_node (void_type_node having been created). */
7284 tree
7285 build_void_list_node ()
7287 tree t = build_tree_list (NULL_TREE, void_type_node);
7288 return t;