* reload1.c (fixup_abnormal_edges): Remove unused variable.
[official-gcc.git] / gcc / c-decl.c
blobbce03a9b91a8d272375ccfa95709afc02234f9d1
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 "toplev.h"
41 #include "ggc.h"
42 #include "tm_p.h"
43 #include "cpplib.h"
44 #include "target.h"
45 #include "debug.h"
46 #include "timevar.h"
47 #include "c-common.h"
48 #include "c-pragma.h"
50 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
51 enum decl_context
52 { NORMAL, /* Ordinary declaration */
53 FUNCDEF, /* Function definition */
54 PARM, /* Declaration of parm before function body */
55 FIELD, /* Declaration inside struct or union */
56 BITFIELD, /* Likewise but with specified width */
57 TYPENAME}; /* Typename (inside cast or sizeof) */
60 /* Nonzero if we have seen an invalid cross reference
61 to a struct, union, or enum, but not yet printed the message. */
63 tree pending_invalid_xref;
64 /* File and line to appear in the eventual error message. */
65 const char *pending_invalid_xref_file;
66 int pending_invalid_xref_line;
68 /* While defining an enum type, this is 1 plus the last enumerator
69 constant value. Note that will do not have to save this or `enum_overflow'
70 around nested function definition since such a definition could only
71 occur in an enum value expression and we don't use these variables in
72 that case. */
74 static tree enum_next_value;
76 /* Nonzero means that there was overflow computing enum_next_value. */
78 static int enum_overflow;
80 /* Parsing a function declarator leaves a list of parameter names
81 or a chain or parameter decls here. */
83 static tree last_function_parms;
85 /* Parsing a function declarator leaves here a chain of structure
86 and enum types declared in the parmlist. */
88 static tree last_function_parm_tags;
90 /* After parsing the declarator that starts a function definition,
91 `start_function' puts here the list of parameter names or chain of decls.
92 `store_parm_decls' finds it here. */
94 static tree current_function_parms;
96 /* Similar, for last_function_parm_tags. */
97 static tree current_function_parm_tags;
99 /* Similar, for the file and line that the prototype came from if this is
100 an old-style definition. */
101 static const char *current_function_prototype_file;
102 static int current_function_prototype_line;
104 /* The current statement tree. */
106 static GTY(()) struct stmt_tree_s c_stmt_tree;
108 /* The current scope statement stack. */
110 static GTY(()) tree c_scope_stmt_stack;
112 /* A list (chain of TREE_LIST nodes) of all LABEL_DECLs in the function
113 that have names. Here so we can clear out their names' definitions
114 at the end of the function. */
116 static GTY(()) tree named_labels;
118 /* A list of LABEL_DECLs from outer contexts that are currently shadowed. */
120 static GTY(()) tree shadowed_labels;
122 /* Set to 0 at beginning of a function definition, set to 1 if
123 a return statement that specifies a return value is seen. */
125 int current_function_returns_value;
127 /* Set to 0 at beginning of a function definition, set to 1 if
128 a return statement with no argument is seen. */
130 int current_function_returns_null;
132 /* Set to 0 at beginning of a function definition, set to 1 if
133 a call to a noreturn function is seen. */
135 int current_function_returns_abnormally;
137 /* Set to nonzero by `grokdeclarator' for a function
138 whose return type is defaulted, if warnings for this are desired. */
140 static int warn_about_return_type;
142 /* Nonzero when starting a function declared `extern inline'. */
144 static int current_extern_inline;
146 /* For each binding contour we allocate a binding_level structure
147 * which records the names defined in that contour.
148 * Contours include:
149 * 0) the global one
150 * 1) one for each function definition,
151 * where internal declarations of the parameters appear.
152 * 2) one for each compound statement,
153 * to record its declarations.
155 * The current meaning of a name can be found by searching the levels from
156 * the current one out to the global one.
159 /* Note that the information in the `names' component of the global contour
160 is duplicated in the IDENTIFIER_GLOBAL_VALUEs of all identifiers. */
162 struct binding_level GTY(())
164 /* A chain of _DECL nodes for all variables, constants, functions,
165 and typedef types. These are in the reverse of the order supplied.
167 tree names;
169 /* A list of structure, union and enum definitions,
170 * for looking up tag names.
171 * It is a chain of TREE_LIST nodes, each of whose TREE_PURPOSE is a name,
172 * or NULL_TREE; and whose TREE_VALUE is a RECORD_TYPE, UNION_TYPE,
173 * or ENUMERAL_TYPE node.
175 tree tags;
177 /* For each level, a list of shadowed outer-level local definitions
178 to be restored when this level is popped.
179 Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
180 whose TREE_VALUE is its old definition (a kind of ..._DECL node). */
181 tree shadowed;
183 /* For each level (except not the global one),
184 a chain of BLOCK nodes for all the levels
185 that were entered and exited one level down. */
186 tree blocks;
188 /* The BLOCK node for this level, if one has been preallocated.
189 If 0, the BLOCK is allocated (if needed) when the level is popped. */
190 tree this_block;
192 /* The binding level which this one is contained in (inherits from). */
193 struct binding_level *level_chain;
195 /* Nonzero for the level that holds the parameters of a function. */
196 char parm_flag;
198 /* Nonzero if this level "doesn't exist" for tags. */
199 char tag_transparent;
201 /* Nonzero if sublevels of this level "don't exist" for tags.
202 This is set in the parm level of a function definition
203 while reading the function body, so that the outermost block
204 of the function body will be tag-transparent. */
205 char subblocks_tag_transparent;
207 /* Nonzero means make a BLOCK for this level regardless of all else. */
208 char keep;
210 /* Nonzero means make a BLOCK if this level has any subblocks. */
211 char keep_if_subblocks;
213 /* List of decls in `names' that have incomplete structure or
214 union types. */
215 tree incomplete_list;
217 /* A list of decls giving the (reversed) specified order of parms,
218 not including any forward-decls in the parmlist.
219 This is so we can put the parms in proper order for assign_parms. */
220 tree parm_order;
223 #define NULL_BINDING_LEVEL (struct binding_level *) NULL
225 /* The binding level currently in effect. */
227 static GTY(()) struct binding_level *current_binding_level;
229 /* A chain of binding_level structures awaiting reuse. */
231 static GTY((deletable (""))) struct binding_level *free_binding_level;
233 /* The outermost binding level, for names of file scope.
234 This is created when the compiler is started and exists
235 through the entire run. */
237 static GTY(()) struct binding_level *global_binding_level;
239 /* Binding level structures are initialized by copying this one. */
241 static struct binding_level clear_binding_level
242 = {NULL, NULL, NULL, NULL, NULL, NULL_BINDING_LEVEL, 0, 0, 0, 0, 0, NULL,
243 NULL};
245 /* Nonzero means unconditionally make a BLOCK for the next level pushed. */
247 static int keep_next_level_flag;
249 /* Nonzero means make a BLOCK for the next level pushed
250 if it has subblocks. */
252 static int keep_next_if_subblocks;
254 /* The chain of outer levels of label scopes.
255 This uses the same data structure used for binding levels,
256 but it works differently: each link in the chain records
257 saved values of named_labels and shadowed_labels for
258 a label binding level outside the current one. */
260 static GTY(()) struct binding_level *label_level_chain;
262 /* Functions called automatically at the beginning and end of execution. */
264 tree static_ctors, static_dtors;
266 /* Forward declarations. */
268 static struct binding_level * make_binding_level PARAMS ((void));
269 static void pop_binding_level PARAMS ((struct binding_level **));
270 static void clear_limbo_values PARAMS ((tree));
271 static int duplicate_decls PARAMS ((tree, tree, int));
272 static int redeclaration_error_message PARAMS ((tree, tree));
273 static void storedecls PARAMS ((tree));
274 static void storetags PARAMS ((tree));
275 static tree lookup_tag PARAMS ((enum tree_code, tree,
276 struct binding_level *, int));
277 static tree lookup_tag_reverse PARAMS ((tree));
278 static tree grokdeclarator PARAMS ((tree, tree, enum decl_context,
279 int));
280 static tree grokparms PARAMS ((tree, int));
281 static void layout_array_type PARAMS ((tree));
282 static tree c_make_fname_decl PARAMS ((tree, int));
283 static void c_expand_body PARAMS ((tree, int, int));
284 static void warn_if_shadowing PARAMS ((tree, tree));
286 /* C-specific option variables. */
288 /* Nonzero means allow type mismatches in conditional expressions;
289 just make their values `void'. */
291 int flag_cond_mismatch;
293 /* Nonzero means don't recognize the keyword `asm'. */
295 int flag_no_asm;
297 /* Nonzero means enable C89 Amendment 1 features. */
299 int flag_isoc94 = 0;
301 /* Nonzero means use the ISO C99 dialect of C. */
303 int flag_isoc99 = 0;
305 /* Nonzero means that we have builtin functions, and main is an int */
307 int flag_hosted = 1;
309 /* Nonzero means add default format_arg attributes for functions not
310 in ISO C. */
312 int flag_noniso_default_format_attributes = 1;
314 /* Nonzero means to treat bitfields as signed unless they say `unsigned'. */
316 int flag_signed_bitfields = 1;
317 int explicit_flag_signed_bitfields = 0;
319 /* Nonzero means warn about use of implicit int. */
321 int warn_implicit_int;
323 /* Nonzero means warn about usage of long long when `-pedantic'. */
325 int warn_long_long = 1;
327 /* Nonzero means message about use of implicit function declarations;
328 1 means warning; 2 means error. */
330 int mesg_implicit_function_declaration = -1;
332 /* Nonzero means give string constants the type `const char *'
333 to get extra warnings from them. These warnings will be too numerous
334 to be useful, except in thoroughly ANSIfied programs. */
336 int flag_const_strings;
338 /* Nonzero means warn about pointer casts that can drop a type qualifier
339 from the pointer target type. */
341 int warn_cast_qual;
343 /* Nonzero means warn when casting a function call to a type that does
344 not match the return type (e.g. (float)sqrt() or (anything*)malloc()
345 when there is no previous declaration of sqrt or malloc. */
347 int warn_bad_function_cast;
349 /* Warn about functions which might be candidates for format attributes. */
351 int warn_missing_format_attribute;
353 /* Warn about traditional constructs whose meanings changed in ANSI C. */
355 int warn_traditional;
357 /* Nonzero means warn about sizeof(function) or addition/subtraction
358 of function pointers. */
360 int warn_pointer_arith;
362 /* Nonzero means warn for non-prototype function decls
363 or non-prototyped defs without previous prototype. */
365 int warn_strict_prototypes;
367 /* Nonzero means warn for any global function def
368 without separate previous prototype decl. */
370 int warn_missing_prototypes;
372 /* Nonzero means warn for any global function def
373 without separate previous decl. */
375 int warn_missing_declarations;
377 /* Nonzero means warn about multiple (redundant) decls for the same single
378 variable or function. */
380 int warn_redundant_decls = 0;
382 /* Nonzero means warn about extern declarations of objects not at
383 file-scope level and about *all* declarations of functions (whether
384 extern or static) not at file-scope level. Note that we exclude
385 implicit function declarations. To get warnings about those, use
386 -Wimplicit. */
388 int warn_nested_externs = 0;
390 /* Warn about a subscript that has type char. */
392 int warn_char_subscripts = 0;
394 /* Warn if a type conversion is done that might have confusing results. */
396 int warn_conversion;
398 /* Warn if adding () is suggested. */
400 int warn_parentheses;
402 /* Warn if initializer is not completely bracketed. */
404 int warn_missing_braces;
406 /* Warn if main is suspicious. */
408 int warn_main;
410 /* Warn about #pragma directives that are not recognised. */
412 int warn_unknown_pragmas = 0; /* Tri state variable. */
414 /* Warn about comparison of signed and unsigned values.
415 If -1, neither -Wsign-compare nor -Wno-sign-compare has been specified. */
417 int warn_sign_compare = -1;
419 /* Warn about testing equality of floating point numbers. */
421 int warn_float_equal = 0;
423 /* Nonzero means `$' can be in an identifier. */
425 #ifndef DOLLARS_IN_IDENTIFIERS
426 #define DOLLARS_IN_IDENTIFIERS 1
427 #endif
428 int dollars_in_ident = DOLLARS_IN_IDENTIFIERS;
430 /* States indicating how grokdeclarator() should handle declspecs marked
431 with __attribute__((deprecated)). An object declared as
432 __attribute__((deprecated)) suppresses warnings of uses of other
433 deprecated items. */
435 enum deprecated_states {
436 DEPRECATED_NORMAL,
437 DEPRECATED_SUPPRESS
440 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
442 /* Decode the string P as a language-specific option for C.
443 Return the number of strings consumed. Should not complain
444 if it does not recognise the option. */
447 c_decode_option (argc, argv)
448 int argc ATTRIBUTE_UNUSED;
449 char **argv;
451 int strings_processed;
452 char *p = argv[0];
454 static const struct {
455 /* The name of the option. */
456 const char *option;
457 /* If non-NULL, a flag variable to set to 0 or 1. If NULL,
458 this means that cpp handles this option. */
459 int *flag;
460 } warn_options[] = {
461 /* This list is in alphabetical order. Keep it like that. */
462 { "bad-function-cast", &warn_bad_function_cast },
463 { "cast-qual", &warn_cast_qual },
464 { "char-subscripts", &warn_char_subscripts },
465 { "comment", NULL },
466 { "comments", NULL },
467 { "conversion", &warn_conversion },
468 { "div-by-zero", &warn_div_by_zero },
469 { "float-equal", &warn_float_equal },
470 { "format-extra-args", &warn_format_extra_args },
471 { "format-zero-length", &warn_format_zero_length },
472 { "format-nonliteral", &warn_format_nonliteral },
473 { "format-security", &warn_format_security },
474 { "format-y2k", &warn_format_y2k },
475 { "implicit-function-declaration", &mesg_implicit_function_declaration },
476 { "implicit-int", &warn_implicit_int },
477 { "import", NULL },
478 { "long-long", &warn_long_long },
479 { "main", &warn_main },
480 { "missing-braces", &warn_missing_braces },
481 { "missing-declarations", &warn_missing_declarations },
482 { "missing-format-attribute", &warn_missing_format_attribute },
483 { "missing-prototypes", &warn_missing_prototypes },
484 { "multichar", &warn_multichar },
485 { "nested-externs", &warn_nested_externs },
486 { "nonnull", &warn_nonnull },
487 { "parentheses", &warn_parentheses },
488 { "pointer-arith", &warn_pointer_arith },
489 { "redundant-decls", &warn_redundant_decls },
490 { "return-type", &warn_return_type },
491 { "sequence-point", &warn_sequence_point },
492 { "sign-compare", &warn_sign_compare },
493 { "strict-prototypes", &warn_strict_prototypes },
494 { "traditional", &warn_traditional },
495 { "trigraphs", NULL },
496 { "undef", NULL },
497 { "write-strings", &flag_const_strings }
500 strings_processed = cpp_handle_option (parse_in, argc, argv);
502 if (!strcmp (p, "-fhosted") || !strcmp (p, "-fno-freestanding"))
504 flag_hosted = 1;
505 flag_no_builtin = 0;
507 else if (!strcmp (p, "-ffreestanding") || !strcmp (p, "-fno-hosted"))
509 flag_hosted = 0;
510 flag_no_builtin = 1;
511 /* warn_main will be 2 if set by -Wall, 1 if set by -Wmain */
512 if (warn_main == 2)
513 warn_main = 0;
515 else if (!strncmp (p, "-std=", 5))
517 /* Select the appropriate language standard. We currently
518 recognize:
519 -std=iso9899:1990 same as -ansi
520 -std=iso9899:199409 ISO C as modified in amend. 1
521 -std=iso9899:1999 ISO C 99
522 -std=c89 same as -std=iso9899:1990
523 -std=c99 same as -std=iso9899:1999
524 -std=gnu89 default, iso9899:1990 + gnu extensions
525 -std=gnu99 iso9899:1999 + gnu extensions
527 const char *const argstart = &p[5];
529 if (!strcmp (argstart, "iso9899:1990")
530 || !strcmp (argstart, "c89"))
532 iso_1990:
533 flag_isoc94 = 0;
534 iso_1994:
535 flag_writable_strings = 0;
536 flag_no_asm = 1;
537 flag_no_nonansi_builtin = 1;
538 flag_noniso_default_format_attributes = 0;
539 flag_isoc99 = 0;
540 flag_iso = 1;
542 else if (!strcmp (argstart, "iso9899:199409"))
544 flag_isoc94 = 1;
545 goto iso_1994;
547 else if (!strcmp (argstart, "iso9899:199x")
548 || !strcmp (argstart, "iso9899:1999")
549 || !strcmp (argstart, "c9x")
550 || !strcmp (argstart, "c99"))
552 flag_writable_strings = 0;
553 flag_no_asm = 1;
554 flag_no_nonansi_builtin = 1;
555 flag_noniso_default_format_attributes = 0;
556 flag_isoc99 = 1;
557 flag_isoc94 = 1;
558 flag_iso = 1;
560 else if (!strcmp (argstart, "gnu89"))
562 flag_writable_strings = 0;
563 flag_no_asm = 0;
564 flag_no_nonansi_builtin = 0;
565 flag_noniso_default_format_attributes = 1;
566 flag_isoc99 = 0;
567 flag_isoc94 = 0;
569 else if (!strcmp (argstart, "gnu9x") || !strcmp (argstart, "gnu99"))
571 flag_writable_strings = 0;
572 flag_no_asm = 0;
573 flag_no_nonansi_builtin = 0;
574 flag_noniso_default_format_attributes = 1;
575 flag_isoc99 = 1;
576 flag_isoc94 = 1;
578 else if (!strcmp (argstart, "c++98"))
579 ; /* Handled by cpplib. */
580 else
581 error ("unknown C standard `%s'", argstart);
583 else if (!strcmp (p, "-fdollars-in-identifiers"))
584 dollars_in_ident = 1;
585 else if (!strcmp (p, "-fno-dollars-in-identifiers"))
586 dollars_in_ident = 0;
587 else if (!strcmp (p, "-fsigned-char"))
588 flag_signed_char = 1;
589 else if (!strcmp (p, "-funsigned-char"))
590 flag_signed_char = 0;
591 else if (!strcmp (p, "-fno-signed-char"))
592 flag_signed_char = 0;
593 else if (!strcmp (p, "-fno-unsigned-char"))
594 flag_signed_char = 1;
595 else if (!strcmp (p, "-fsigned-bitfields")
596 || !strcmp (p, "-fno-unsigned-bitfields"))
598 flag_signed_bitfields = 1;
599 explicit_flag_signed_bitfields = 1;
601 else if (!strcmp (p, "-funsigned-bitfields")
602 || !strcmp (p, "-fno-signed-bitfields"))
604 flag_signed_bitfields = 0;
605 explicit_flag_signed_bitfields = 1;
607 else if (!strcmp (p, "-fshort-enums"))
608 flag_short_enums = 1;
609 else if (!strcmp (p, "-fno-short-enums"))
610 flag_short_enums = 0;
611 else if (!strcmp (p, "-fshort-wchar"))
612 flag_short_wchar = 1;
613 else if (!strcmp (p, "-fno-short-wchar"))
614 flag_short_wchar = 0;
615 else if (!strcmp (p, "-fcond-mismatch"))
616 flag_cond_mismatch = 1;
617 else if (!strcmp (p, "-fno-cond-mismatch"))
618 flag_cond_mismatch = 0;
619 else if (!strcmp (p, "-fshort-double"))
620 flag_short_double = 1;
621 else if (!strcmp (p, "-fno-short-double"))
622 flag_short_double = 0;
623 else if (!strcmp (p, "-fasm"))
624 flag_no_asm = 0;
625 else if (!strcmp (p, "-fno-asm"))
626 flag_no_asm = 1;
627 else if (!strcmp (p, "-fms-extensions"))
628 flag_ms_extensions = 1;
629 else if (!strcmp (p, "-fno-ms-extensions"))
630 flag_ms_extensions = 0;
631 else if (!strcmp (p, "-fbuiltin"))
632 flag_no_builtin = 0;
633 else if (!strcmp (p, "-fno-builtin"))
634 flag_no_builtin = 1;
635 else if (!strncmp (p, "-fno-builtin-", strlen ("-fno-builtin-")))
636 disable_builtin_function (p + strlen ("-fno-builtin-"));
637 else if (p[0] == '-' && p[1] == 'f' && dump_switch_p (p + 2))
639 else if (!strcmp (p, "-ansi"))
640 goto iso_1990;
641 else if (!strcmp (p, "-undef"))
642 flag_undef = 1;
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 < ARRAY_SIZE (warn_options); 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 /* Reuse or create a struct for this binding level. */
759 static struct binding_level *
760 make_binding_level ()
762 if (free_binding_level)
764 struct binding_level *result = free_binding_level;
765 free_binding_level = result->level_chain;
766 return result;
768 else
769 return (struct binding_level *) ggc_alloc (sizeof (struct binding_level));
772 /* Remove a binding level from a list and add it to the level chain. */
774 static void
775 pop_binding_level (lp)
776 struct binding_level **lp;
778 struct binding_level *l = *lp;
779 *lp = l->level_chain;
781 memset (l, 0, sizeof (struct binding_level));
782 l->level_chain = free_binding_level;
783 free_binding_level = l;
786 /* Nonzero if we are currently in the global binding level. */
789 global_bindings_p ()
791 return current_binding_level == global_binding_level;
794 void
795 keep_next_level ()
797 keep_next_level_flag = 1;
800 /* Nonzero if the current level needs to have a BLOCK made. */
803 kept_level_p ()
805 return ((current_binding_level->keep_if_subblocks
806 && current_binding_level->blocks != 0)
807 || current_binding_level->keep
808 || current_binding_level->names != 0
809 || (current_binding_level->tags != 0
810 && !current_binding_level->tag_transparent));
813 /* Identify this binding level as a level of parameters.
814 DEFINITION_FLAG is 1 for a definition, 0 for a declaration.
815 But it turns out there is no way to pass the right value for
816 DEFINITION_FLAG, so we ignore it. */
818 void
819 declare_parm_level (definition_flag)
820 int definition_flag ATTRIBUTE_UNUSED;
822 current_binding_level->parm_flag = 1;
825 /* Nonzero if currently making parm declarations. */
828 in_parm_level_p ()
830 return current_binding_level->parm_flag;
833 /* Enter a new binding level.
834 If TAG_TRANSPARENT is nonzero, do so only for the name space of variables,
835 not for that of tags. */
837 void
838 pushlevel (tag_transparent)
839 int tag_transparent;
841 struct binding_level *newlevel = NULL_BINDING_LEVEL;
843 /* If this is the top level of a function,
844 just make sure that NAMED_LABELS is 0. */
846 if (current_binding_level == global_binding_level)
848 named_labels = 0;
851 newlevel = make_binding_level ();
853 /* Add this level to the front of the chain (stack) of levels that
854 are active. */
856 *newlevel = clear_binding_level;
857 newlevel->tag_transparent
858 = (tag_transparent
859 || (current_binding_level
860 ? current_binding_level->subblocks_tag_transparent
861 : 0));
862 newlevel->level_chain = current_binding_level;
863 current_binding_level = newlevel;
864 newlevel->keep = keep_next_level_flag;
865 keep_next_level_flag = 0;
866 newlevel->keep_if_subblocks = keep_next_if_subblocks;
867 keep_next_if_subblocks = 0;
870 /* Clear the limbo values of all identifiers defined in BLOCK or a subblock. */
872 static void
873 clear_limbo_values (block)
874 tree block;
876 tree tem;
878 for (tem = BLOCK_VARS (block); tem; tem = TREE_CHAIN (tem))
879 if (DECL_NAME (tem) != 0)
880 IDENTIFIER_LIMBO_VALUE (DECL_NAME (tem)) = 0;
882 for (tem = BLOCK_SUBBLOCKS (block); tem; tem = TREE_CHAIN (tem))
883 clear_limbo_values (tem);
886 /* Exit a binding level.
887 Pop the level off, and restore the state of the identifier-decl mappings
888 that were in effect when this level was entered.
890 If KEEP is nonzero, this level had explicit declarations, so
891 and create a "block" (a BLOCK node) for the level
892 to record its declarations and subblocks for symbol table output.
894 If FUNCTIONBODY is nonzero, this level is the body of a function,
895 so create a block as if KEEP were set and also clear out all
896 label names.
898 If REVERSE is nonzero, reverse the order of decls before putting
899 them into the BLOCK. */
901 tree
902 poplevel (keep, reverse, functionbody)
903 int keep;
904 int reverse;
905 int functionbody;
907 tree link;
908 /* The chain of decls was accumulated in reverse order.
909 Put it into forward order, just for cleanliness. */
910 tree decls;
911 tree tags = current_binding_level->tags;
912 tree subblocks = current_binding_level->blocks;
913 tree block = 0;
914 tree decl;
915 int block_previously_created;
917 keep |= current_binding_level->keep;
919 /* This warning is turned off because it causes warnings for
920 declarations like `extern struct foo *x'. */
921 #if 0
922 /* Warn about incomplete structure types in this level. */
923 for (link = tags; link; link = TREE_CHAIN (link))
924 if (!COMPLETE_TYPE_P (TREE_VALUE (link)))
926 tree type = TREE_VALUE (link);
927 tree type_name = TYPE_NAME (type);
928 char *id = IDENTIFIER_POINTER (TREE_CODE (type_name) == IDENTIFIER_NODE
929 ? type_name
930 : DECL_NAME (type_name));
931 switch (TREE_CODE (type))
933 case RECORD_TYPE:
934 error ("`struct %s' incomplete in scope ending here", id);
935 break;
936 case UNION_TYPE:
937 error ("`union %s' incomplete in scope ending here", id);
938 break;
939 case ENUMERAL_TYPE:
940 error ("`enum %s' incomplete in scope ending here", id);
941 break;
944 #endif /* 0 */
946 /* Get the decls in the order they were written.
947 Usually current_binding_level->names is in reverse order.
948 But parameter decls were previously put in forward order. */
950 if (reverse)
951 current_binding_level->names
952 = decls = nreverse (current_binding_level->names);
953 else
954 decls = current_binding_level->names;
956 /* Output any nested inline functions within this block
957 if they weren't already output. */
959 for (decl = decls; decl; decl = TREE_CHAIN (decl))
960 if (TREE_CODE (decl) == FUNCTION_DECL
961 && ! TREE_ASM_WRITTEN (decl)
962 && DECL_INITIAL (decl) != 0
963 && TREE_ADDRESSABLE (decl))
965 /* If this decl was copied from a file-scope decl
966 on account of a block-scope extern decl,
967 propagate TREE_ADDRESSABLE to the file-scope decl.
969 DECL_ABSTRACT_ORIGIN can be set to itself if warn_return_type is
970 true, since then the decl goes through save_for_inline_copying. */
971 if (DECL_ABSTRACT_ORIGIN (decl) != 0
972 && DECL_ABSTRACT_ORIGIN (decl) != decl)
973 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (decl)) = 1;
976 /* We used to warn about unused variables in expand_end_bindings,
977 i.e. while generating RTL. But in function-at-a-time mode we may
978 choose to never expand a function at all (e.g. auto inlining), so
979 we do this explicitly now. */
980 warn_about_unused_variables (getdecls ());
982 /* If there were any declarations or structure tags in that level,
983 or if this level is a function body,
984 create a BLOCK to record them for the life of this function. */
986 block = 0;
987 block_previously_created = (current_binding_level->this_block != 0);
988 if (block_previously_created)
989 block = current_binding_level->this_block;
990 else if (keep || functionbody
991 || (current_binding_level->keep_if_subblocks && subblocks != 0))
992 block = make_node (BLOCK);
993 if (block != 0)
995 BLOCK_VARS (block) = decls;
996 BLOCK_SUBBLOCKS (block) = subblocks;
999 /* In each subblock, record that this is its superior. */
1001 for (link = subblocks; link; link = TREE_CHAIN (link))
1002 BLOCK_SUPERCONTEXT (link) = block;
1004 /* Clear out the meanings of the local variables of this level. */
1006 for (link = decls; link; link = TREE_CHAIN (link))
1008 if (DECL_NAME (link) != 0)
1010 /* If the ident. was used or addressed via a local extern decl,
1011 don't forget that fact. */
1012 if (DECL_EXTERNAL (link))
1014 if (TREE_USED (link))
1015 TREE_USED (DECL_NAME (link)) = 1;
1016 if (TREE_ADDRESSABLE (link))
1017 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (link)) = 1;
1019 IDENTIFIER_LOCAL_VALUE (DECL_NAME (link)) = 0;
1023 /* Restore all name-meanings of the outer levels
1024 that were shadowed by this level. */
1026 for (link = current_binding_level->shadowed; link; link = TREE_CHAIN (link))
1027 IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
1029 /* If the level being exited is the top level of a function,
1030 check over all the labels, and clear out the current
1031 (function local) meanings of their names. */
1033 if (functionbody)
1035 clear_limbo_values (block);
1037 /* If this is the top level block of a function,
1038 the vars are the function's parameters.
1039 Don't leave them in the BLOCK because they are
1040 found in the FUNCTION_DECL instead. */
1042 BLOCK_VARS (block) = 0;
1044 /* Clear out the definitions of all label names,
1045 since their scopes end here,
1046 and add them to BLOCK_VARS. */
1048 for (link = named_labels; link; link = TREE_CHAIN (link))
1050 tree label = TREE_VALUE (link);
1052 if (DECL_INITIAL (label) == 0)
1054 error_with_decl (label, "label `%s' used but not defined");
1055 /* Avoid crashing later. */
1056 define_label (input_filename, lineno,
1057 DECL_NAME (label));
1059 else if (warn_unused_label && !TREE_USED (label))
1060 warning_with_decl (label, "label `%s' defined but not used");
1061 IDENTIFIER_LABEL_VALUE (DECL_NAME (label)) = 0;
1063 /* Put the labels into the "variables" of the
1064 top-level block, so debugger can see them. */
1065 TREE_CHAIN (label) = BLOCK_VARS (block);
1066 BLOCK_VARS (block) = label;
1070 /* Pop the current level, and free the structure for reuse. */
1072 pop_binding_level (&current_binding_level);
1074 /* Dispose of the block that we just made inside some higher level. */
1075 if (functionbody)
1076 DECL_INITIAL (current_function_decl) = block;
1077 else if (block)
1079 if (!block_previously_created)
1080 current_binding_level->blocks
1081 = chainon (current_binding_level->blocks, block);
1083 /* If we did not make a block for the level just exited,
1084 any blocks made for inner levels
1085 (since they cannot be recorded as subblocks in that level)
1086 must be carried forward so they will later become subblocks
1087 of something else. */
1088 else if (subblocks)
1089 current_binding_level->blocks
1090 = chainon (current_binding_level->blocks, subblocks);
1092 /* Set the TYPE_CONTEXTs for all of the tagged types belonging to this
1093 binding contour so that they point to the appropriate construct, i.e.
1094 either to the current FUNCTION_DECL node, or else to the BLOCK node
1095 we just constructed.
1097 Note that for tagged types whose scope is just the formal parameter
1098 list for some function type specification, we can't properly set
1099 their TYPE_CONTEXTs here, because we don't have a pointer to the
1100 appropriate FUNCTION_TYPE node readily available to us. For those
1101 cases, the TYPE_CONTEXTs of the relevant tagged type nodes get set
1102 in `grokdeclarator' as soon as we have created the FUNCTION_TYPE
1103 node which will represent the "scope" for these "parameter list local"
1104 tagged types. */
1106 if (functionbody)
1107 for (link = tags; link; link = TREE_CHAIN (link))
1108 TYPE_CONTEXT (TREE_VALUE (link)) = current_function_decl;
1109 else if (block)
1110 for (link = tags; link; link = TREE_CHAIN (link))
1111 TYPE_CONTEXT (TREE_VALUE (link)) = block;
1113 if (block)
1114 TREE_USED (block) = 1;
1116 return block;
1119 /* Insert BLOCK at the end of the list of subblocks of the
1120 current binding level. This is used when a BIND_EXPR is expanded,
1121 to handle the BLOCK node inside the BIND_EXPR. */
1123 void
1124 insert_block (block)
1125 tree block;
1127 TREE_USED (block) = 1;
1128 current_binding_level->blocks
1129 = chainon (current_binding_level->blocks, block);
1132 /* Set the BLOCK node for the innermost scope
1133 (the one we are currently in). */
1135 void
1136 set_block (block)
1137 tree block;
1139 current_binding_level->this_block = block;
1140 current_binding_level->names = chainon (current_binding_level->names,
1141 BLOCK_VARS (block));
1142 current_binding_level->blocks = chainon (current_binding_level->blocks,
1143 BLOCK_SUBBLOCKS (block));
1146 void
1147 push_label_level ()
1149 struct binding_level *newlevel;
1151 newlevel = make_binding_level ();
1153 /* Add this level to the front of the chain (stack) of label levels. */
1155 newlevel->level_chain = label_level_chain;
1156 label_level_chain = newlevel;
1158 newlevel->names = named_labels;
1159 newlevel->shadowed = shadowed_labels;
1160 named_labels = 0;
1161 shadowed_labels = 0;
1164 void
1165 pop_label_level ()
1167 struct binding_level *level = label_level_chain;
1168 tree link, prev;
1170 /* Clear out the definitions of the declared labels in this level.
1171 Leave in the list any ordinary, non-declared labels. */
1172 for (link = named_labels, prev = 0; link;)
1174 if (C_DECLARED_LABEL_FLAG (TREE_VALUE (link)))
1176 if (DECL_SOURCE_LINE (TREE_VALUE (link)) == 0)
1178 error_with_decl (TREE_VALUE (link),
1179 "label `%s' used but not defined");
1180 /* Avoid crashing later. */
1181 define_label (input_filename, lineno,
1182 DECL_NAME (TREE_VALUE (link)));
1184 else if (warn_unused_label && !TREE_USED (TREE_VALUE (link)))
1185 warning_with_decl (TREE_VALUE (link),
1186 "label `%s' defined but not used");
1187 IDENTIFIER_LABEL_VALUE (DECL_NAME (TREE_VALUE (link))) = 0;
1189 /* Delete this element from the list. */
1190 link = TREE_CHAIN (link);
1191 if (prev)
1192 TREE_CHAIN (prev) = link;
1193 else
1194 named_labels = link;
1196 else
1198 prev = link;
1199 link = TREE_CHAIN (link);
1203 /* Bring back all the labels that were shadowed. */
1204 for (link = shadowed_labels; link; link = TREE_CHAIN (link))
1205 if (DECL_NAME (TREE_VALUE (link)) != 0)
1206 IDENTIFIER_LABEL_VALUE (DECL_NAME (TREE_VALUE (link)))
1207 = TREE_VALUE (link);
1209 named_labels = chainon (named_labels, level->names);
1210 shadowed_labels = level->shadowed;
1212 /* Pop the current level, and free the structure for reuse. */
1213 pop_binding_level (&label_level_chain);
1216 /* Push a definition or a declaration of struct, union or enum tag "name".
1217 "type" should be the type node.
1218 We assume that the tag "name" is not already defined.
1220 Note that the definition may really be just a forward reference.
1221 In that case, the TYPE_SIZE will be zero. */
1223 void
1224 pushtag (name, type)
1225 tree name, type;
1227 struct binding_level *b;
1229 /* Find the proper binding level for this type tag. */
1231 for (b = current_binding_level; b->tag_transparent; b = b->level_chain)
1232 continue;
1234 if (name)
1236 /* Record the identifier as the type's name if it has none. */
1238 if (TYPE_NAME (type) == 0)
1239 TYPE_NAME (type) = name;
1242 b->tags = tree_cons (name, type, b->tags);
1244 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1245 tagged type we just added to the current binding level. This fake
1246 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1247 to output a representation of a tagged type, and it also gives
1248 us a convenient place to record the "scope start" address for the
1249 tagged type. */
1251 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
1253 /* An approximation for now, so we can tell this is a function-scope tag.
1254 This will be updated in poplevel. */
1255 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
1258 /* Handle when a new declaration NEWDECL
1259 has the same name as an old one OLDDECL
1260 in the same binding contour.
1261 Prints an error message if appropriate.
1263 If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
1264 Otherwise, return 0.
1266 When DIFFERENT_BINDING_LEVEL is true, NEWDECL is an external declaration,
1267 and OLDDECL is in an outer binding level and should thus not be changed. */
1269 static int
1270 duplicate_decls (newdecl, olddecl, different_binding_level)
1271 tree newdecl, olddecl;
1272 int different_binding_level;
1274 int types_match = comptypes (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
1275 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1276 && DECL_INITIAL (newdecl) != 0);
1277 tree oldtype = TREE_TYPE (olddecl);
1278 tree newtype = TREE_TYPE (newdecl);
1279 int errmsg = 0;
1281 if (DECL_P (olddecl))
1283 if (TREE_CODE (newdecl) == FUNCTION_DECL
1284 && TREE_CODE (olddecl) == FUNCTION_DECL
1285 && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
1287 if (DECL_DECLARED_INLINE_P (newdecl)
1288 && DECL_UNINLINABLE (newdecl)
1289 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1290 /* Already warned elsewhere. */;
1291 else if (DECL_DECLARED_INLINE_P (olddecl)
1292 && DECL_UNINLINABLE (olddecl)
1293 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1294 /* Already warned. */;
1295 else if (DECL_DECLARED_INLINE_P (newdecl)
1296 && ! DECL_DECLARED_INLINE_P (olddecl)
1297 && DECL_UNINLINABLE (olddecl)
1298 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1300 warning_with_decl (newdecl,
1301 "function `%s' redeclared as inline");
1302 warning_with_decl (olddecl,
1303 "previous declaration of function `%s' with attribute noinline");
1305 else if (DECL_DECLARED_INLINE_P (olddecl)
1306 && DECL_UNINLINABLE (newdecl)
1307 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1309 warning_with_decl (newdecl,
1310 "function `%s' redeclared with attribute noinline");
1311 warning_with_decl (olddecl,
1312 "previous declaration of function `%s' was inline");
1316 DECL_ATTRIBUTES (newdecl)
1317 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
1320 if (TREE_CODE (newtype) == ERROR_MARK
1321 || TREE_CODE (oldtype) == ERROR_MARK)
1322 types_match = 0;
1324 /* New decl is completely inconsistent with the old one =>
1325 tell caller to replace the old one.
1326 This is always an error except in the case of shadowing a builtin. */
1327 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1329 if (TREE_CODE (olddecl) == FUNCTION_DECL
1330 && (DECL_BUILT_IN (olddecl)
1331 || DECL_BUILT_IN_NONANSI (olddecl)))
1333 /* If you declare a built-in or predefined function name as static,
1334 the old definition is overridden,
1335 but optionally warn this was a bad choice of name. */
1336 if (!TREE_PUBLIC (newdecl))
1338 if (!warn_shadow)
1340 else if (DECL_BUILT_IN (olddecl))
1341 warning_with_decl (newdecl, "shadowing built-in function `%s'");
1342 else
1343 warning_with_decl (newdecl, "shadowing library function `%s'");
1345 /* Likewise, if the built-in is not ansi, then programs can
1346 override it even globally without an error. */
1347 else if (! DECL_BUILT_IN (olddecl))
1348 warning_with_decl (newdecl,
1349 "library function `%s' declared as non-function");
1351 else if (DECL_BUILT_IN_NONANSI (olddecl))
1352 warning_with_decl (newdecl,
1353 "built-in function `%s' declared as non-function");
1354 else
1355 warning_with_decl (newdecl,
1356 "built-in function `%s' declared as non-function");
1358 else
1360 error_with_decl (newdecl, "`%s' redeclared as different kind of symbol");
1361 error_with_decl (olddecl, "previous declaration of `%s'");
1364 return 0;
1367 /* For real parm decl following a forward decl,
1368 return 1 so old decl will be reused. */
1369 if (types_match && TREE_CODE (newdecl) == PARM_DECL
1370 && TREE_ASM_WRITTEN (olddecl) && ! TREE_ASM_WRITTEN (newdecl))
1371 return 1;
1373 /* The new declaration is the same kind of object as the old one.
1374 The declarations may partially match. Print warnings if they don't
1375 match enough. Ultimately, copy most of the information from the new
1376 decl to the old one, and keep using the old one. */
1378 if (TREE_CODE (olddecl) == FUNCTION_DECL && DECL_BUILT_IN (olddecl))
1380 /* A function declaration for a built-in function. */
1381 if (!TREE_PUBLIC (newdecl))
1383 /* If you declare a built-in function name as static, the
1384 built-in definition is overridden,
1385 but optionally warn this was a bad choice of name. */
1386 if (warn_shadow)
1387 warning_with_decl (newdecl, "shadowing built-in function `%s'");
1388 /* Discard the old built-in function. */
1389 return 0;
1391 else if (!types_match)
1393 /* Accept the return type of the new declaration if same modes. */
1394 tree oldreturntype = TREE_TYPE (oldtype);
1395 tree newreturntype = TREE_TYPE (newtype);
1397 if (TYPE_MODE (oldreturntype) == TYPE_MODE (newreturntype))
1399 /* Function types may be shared, so we can't just modify
1400 the return type of olddecl's function type. */
1401 tree trytype
1402 = build_function_type (newreturntype,
1403 TYPE_ARG_TYPES (oldtype));
1404 trytype = build_type_attribute_variant (trytype,
1405 TYPE_ATTRIBUTES (oldtype));
1407 types_match = comptypes (newtype, trytype);
1408 if (types_match)
1409 oldtype = trytype;
1411 /* Accept harmless mismatch in first argument type also.
1412 This is for the ffs and fprintf builtins. */
1413 if (TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != 0
1414 && TYPE_ARG_TYPES (oldtype) != 0
1415 && TREE_VALUE (TYPE_ARG_TYPES (newtype)) != 0
1416 && TREE_VALUE (TYPE_ARG_TYPES (oldtype)) != 0
1417 && (TYPE_MODE (TREE_VALUE (TYPE_ARG_TYPES (newtype)))
1418 == TYPE_MODE (TREE_VALUE (TYPE_ARG_TYPES (oldtype)))))
1420 /* Function types may be shared, so we can't just modify
1421 the return type of olddecl's function type. */
1422 tree trytype
1423 = build_function_type (TREE_TYPE (oldtype),
1424 tree_cons (NULL_TREE,
1425 TREE_VALUE (TYPE_ARG_TYPES (newtype)),
1426 TREE_CHAIN (TYPE_ARG_TYPES (oldtype))));
1427 trytype = build_type_attribute_variant (trytype,
1428 TYPE_ATTRIBUTES (oldtype));
1430 types_match = comptypes (newtype, trytype);
1431 if (types_match)
1432 oldtype = trytype;
1434 if (! different_binding_level)
1435 TREE_TYPE (olddecl) = oldtype;
1437 else if (TYPE_ARG_TYPES (oldtype) == NULL
1438 && TYPE_ARG_TYPES (newtype) != NULL)
1440 /* For bcmp, bzero, fputs the builtin type has arguments not
1441 specified. Use the ones from the prototype so that type checking
1442 is done for them. */
1443 tree trytype
1444 = build_function_type (TREE_TYPE (oldtype),
1445 TYPE_ARG_TYPES (newtype));
1446 trytype = build_type_attribute_variant (trytype,
1447 TYPE_ATTRIBUTES (oldtype));
1449 oldtype = trytype;
1450 if (! different_binding_level)
1451 TREE_TYPE (olddecl) = oldtype;
1453 if (!types_match)
1455 /* If types don't match for a built-in, throw away the built-in. */
1456 warning_with_decl (newdecl, "conflicting types for built-in function `%s'");
1457 return 0;
1460 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1461 && DECL_SOURCE_LINE (olddecl) == 0)
1463 /* A function declaration for a predeclared function
1464 that isn't actually built in. */
1465 if (!TREE_PUBLIC (newdecl))
1467 /* If you declare it as static, the
1468 default definition is overridden. */
1469 return 0;
1471 else if (!types_match)
1473 /* If the types don't match, preserve volatility indication.
1474 Later on, we will discard everything else about the
1475 default declaration. */
1476 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1479 /* Permit char *foo () to match void *foo (...) if not pedantic,
1480 if one of them came from a system header file. */
1481 else if (!types_match
1482 && TREE_CODE (olddecl) == FUNCTION_DECL
1483 && TREE_CODE (newdecl) == FUNCTION_DECL
1484 && TREE_CODE (TREE_TYPE (oldtype)) == POINTER_TYPE
1485 && TREE_CODE (TREE_TYPE (newtype)) == POINTER_TYPE
1486 && (DECL_IN_SYSTEM_HEADER (olddecl)
1487 || DECL_IN_SYSTEM_HEADER (newdecl))
1488 && ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (newtype))) == void_type_node
1489 && TYPE_ARG_TYPES (oldtype) == 0
1490 && self_promoting_args_p (TYPE_ARG_TYPES (newtype))
1491 && TREE_TYPE (TREE_TYPE (oldtype)) == char_type_node)
1493 (TREE_TYPE (TREE_TYPE (newtype)) == char_type_node
1494 && TYPE_ARG_TYPES (newtype) == 0
1495 && self_promoting_args_p (TYPE_ARG_TYPES (oldtype))
1496 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (oldtype))) == void_type_node)))
1498 if (pedantic)
1499 pedwarn_with_decl (newdecl, "conflicting types for `%s'");
1500 /* Make sure we keep void * as ret type, not char *. */
1501 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (oldtype))) == void_type_node)
1502 TREE_TYPE (newdecl) = newtype = oldtype;
1504 /* Set DECL_IN_SYSTEM_HEADER, so that if we see another declaration
1505 we will come back here again. */
1506 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1508 else if (!types_match
1509 /* Permit char *foo (int, ...); followed by char *foo ();
1510 if not pedantic. */
1511 && ! (TREE_CODE (olddecl) == FUNCTION_DECL
1512 && ! pedantic
1513 /* Return types must still match. */
1514 && comptypes (TREE_TYPE (oldtype),
1515 TREE_TYPE (newtype))
1516 && TYPE_ARG_TYPES (newtype) == 0))
1518 error_with_decl (newdecl, "conflicting types for `%s'");
1519 /* Check for function type mismatch
1520 involving an empty arglist vs a nonempty one. */
1521 if (TREE_CODE (olddecl) == FUNCTION_DECL
1522 && comptypes (TREE_TYPE (oldtype),
1523 TREE_TYPE (newtype))
1524 && ((TYPE_ARG_TYPES (oldtype) == 0
1525 && DECL_INITIAL (olddecl) == 0)
1527 (TYPE_ARG_TYPES (newtype) == 0
1528 && DECL_INITIAL (newdecl) == 0)))
1530 /* Classify the problem further. */
1531 tree t = TYPE_ARG_TYPES (oldtype);
1532 if (t == 0)
1533 t = TYPE_ARG_TYPES (newtype);
1534 for (; t; t = TREE_CHAIN (t))
1536 tree type = TREE_VALUE (t);
1538 if (TREE_CHAIN (t) == 0
1539 && TYPE_MAIN_VARIANT (type) != void_type_node)
1541 error ("a parameter list with an ellipsis can't match an empty parameter name list declaration");
1542 break;
1545 if (c_type_promotes_to (type) != type)
1547 error ("an argument type that has a default promotion can't match an empty parameter name list declaration");
1548 break;
1552 error_with_decl (olddecl, "previous declaration of `%s'");
1554 else
1556 errmsg = redeclaration_error_message (newdecl, olddecl);
1557 if (errmsg)
1559 switch (errmsg)
1561 case 1:
1562 error_with_decl (newdecl, "redefinition of `%s'");
1563 break;
1564 case 2:
1565 error_with_decl (newdecl, "redeclaration of `%s'");
1566 break;
1567 case 3:
1568 error_with_decl (newdecl, "conflicting declarations of `%s'");
1569 break;
1570 default:
1571 abort ();
1574 error_with_decl (olddecl,
1575 ((DECL_INITIAL (olddecl)
1576 && current_binding_level == global_binding_level)
1577 ? "`%s' previously defined here"
1578 : "`%s' previously declared here"));
1579 return 0;
1581 else if (TREE_CODE (newdecl) == TYPE_DECL
1582 && (DECL_IN_SYSTEM_HEADER (olddecl)
1583 || DECL_IN_SYSTEM_HEADER (newdecl)))
1585 warning_with_decl (newdecl, "redefinition of `%s'");
1586 warning_with_decl
1587 (olddecl,
1588 ((DECL_INITIAL (olddecl)
1589 && current_binding_level == global_binding_level)
1590 ? "`%s' previously defined here"
1591 : "`%s' previously declared here"));
1593 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1594 && DECL_INITIAL (olddecl) != 0
1595 && TYPE_ARG_TYPES (oldtype) == 0
1596 && TYPE_ARG_TYPES (newtype) != 0
1597 && TYPE_ACTUAL_ARG_TYPES (oldtype) != 0)
1599 tree type, parm;
1600 int nargs;
1601 /* Prototype decl follows defn w/o prototype. */
1603 for (parm = TYPE_ACTUAL_ARG_TYPES (oldtype),
1604 type = TYPE_ARG_TYPES (newtype),
1605 nargs = 1;
1607 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type), nargs++)
1609 if (TYPE_MAIN_VARIANT (TREE_VALUE (parm)) == void_type_node
1610 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
1612 warning_with_decl (newdecl, "prototype for `%s' follows");
1613 warning_with_decl (olddecl, "non-prototype definition here");
1614 break;
1616 if (TYPE_MAIN_VARIANT (TREE_VALUE (parm)) == void_type_node
1617 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
1619 error_with_decl (newdecl,
1620 "prototype for `%s' follows and number of arguments doesn't match");
1621 error_with_decl (olddecl, "non-prototype definition here");
1622 errmsg = 1;
1623 break;
1625 /* Type for passing arg must be consistent
1626 with that declared for the arg. */
1627 if (! comptypes (TREE_VALUE (parm), TREE_VALUE (type)))
1629 error_with_decl (newdecl,
1630 "prototype for `%s' follows and argument %d doesn't match",
1631 nargs);
1632 error_with_decl (olddecl, "non-prototype definition here");
1633 errmsg = 1;
1634 break;
1638 /* Warn about mismatches in various flags. */
1639 else
1641 /* Warn if function is now inline
1642 but was previously declared not inline and has been called. */
1643 if (TREE_CODE (olddecl) == FUNCTION_DECL
1644 && ! DECL_DECLARED_INLINE_P (olddecl)
1645 && DECL_DECLARED_INLINE_P (newdecl)
1646 && TREE_USED (olddecl))
1647 warning_with_decl (newdecl,
1648 "`%s' declared inline after being called");
1649 if (TREE_CODE (olddecl) == FUNCTION_DECL
1650 && ! DECL_DECLARED_INLINE_P (olddecl)
1651 && DECL_DECLARED_INLINE_P (newdecl)
1652 && DECL_INITIAL (olddecl) != 0)
1653 warning_with_decl (newdecl,
1654 "`%s' declared inline after its definition");
1656 /* If pedantic, warn when static declaration follows a non-static
1657 declaration. Otherwise, do so only for functions. */
1658 if ((pedantic || TREE_CODE (olddecl) == FUNCTION_DECL)
1659 && TREE_PUBLIC (olddecl)
1660 && !TREE_PUBLIC (newdecl))
1661 warning_with_decl (newdecl, "static declaration for `%s' follows non-static");
1663 /* If warn_traditional, warn when a non-static function
1664 declaration follows a static one. */
1665 if (warn_traditional && !in_system_header
1666 && TREE_CODE (olddecl) == FUNCTION_DECL
1667 && !TREE_PUBLIC (olddecl)
1668 && TREE_PUBLIC (newdecl))
1669 warning_with_decl (newdecl, "non-static declaration for `%s' follows static");
1671 /* Warn when const declaration follows a non-const
1672 declaration, but not for functions. */
1673 if (TREE_CODE (olddecl) != FUNCTION_DECL
1674 && !TREE_READONLY (olddecl)
1675 && TREE_READONLY (newdecl))
1676 warning_with_decl (newdecl, "const declaration for `%s' follows non-const");
1677 /* These bits are logically part of the type, for variables.
1678 But not for functions
1679 (where qualifiers are not valid ANSI anyway). */
1680 else if (pedantic && TREE_CODE (olddecl) != FUNCTION_DECL
1681 && (TREE_READONLY (newdecl) != TREE_READONLY (olddecl)
1682 || TREE_THIS_VOLATILE (newdecl) != TREE_THIS_VOLATILE (olddecl)))
1683 pedwarn_with_decl (newdecl, "type qualifiers for `%s' conflict with previous decl");
1687 /* Optionally warn about more than one declaration for the same name. */
1688 if (errmsg == 0 && warn_redundant_decls && DECL_SOURCE_LINE (olddecl) != 0
1689 /* Don't warn about a function declaration
1690 followed by a definition. */
1691 && !(TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl) != 0
1692 && DECL_INITIAL (olddecl) == 0)
1693 /* Don't warn about extern decl followed by (tentative) definition. */
1694 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl)))
1696 warning_with_decl (newdecl, "redundant redeclaration of `%s' in same scope");
1697 warning_with_decl (olddecl, "previous declaration of `%s'");
1700 /* Copy all the DECL_... slots specified in the new decl
1701 except for any that we copy here from the old type.
1703 Past this point, we don't change OLDTYPE and NEWTYPE
1704 even if we change the types of NEWDECL and OLDDECL. */
1706 if (types_match)
1708 /* When copying info to olddecl, we store into write_olddecl
1709 instead. This allows us to avoid modifying olddecl when
1710 different_binding_level is true. */
1711 tree write_olddecl = different_binding_level ? newdecl : olddecl;
1713 /* Merge the data types specified in the two decls. */
1714 if (TREE_CODE (newdecl) != FUNCTION_DECL || !DECL_BUILT_IN (olddecl))
1716 if (different_binding_level)
1718 if (TYPE_ARG_TYPES (oldtype) != 0
1719 && TYPE_ARG_TYPES (newtype) == 0)
1720 TREE_TYPE (newdecl) = common_type (newtype, oldtype);
1721 else
1722 TREE_TYPE (newdecl)
1723 = build_type_attribute_variant
1724 (newtype,
1725 merge_attributes (TYPE_ATTRIBUTES (newtype),
1726 TYPE_ATTRIBUTES (oldtype)));
1728 else
1729 TREE_TYPE (newdecl)
1730 = TREE_TYPE (olddecl)
1731 = common_type (newtype, oldtype);
1734 /* Lay the type out, unless already done. */
1735 if (oldtype != TREE_TYPE (newdecl))
1737 if (TREE_TYPE (newdecl) != error_mark_node)
1738 layout_type (TREE_TYPE (newdecl));
1739 if (TREE_CODE (newdecl) != FUNCTION_DECL
1740 && TREE_CODE (newdecl) != TYPE_DECL
1741 && TREE_CODE (newdecl) != CONST_DECL)
1742 layout_decl (newdecl, 0);
1744 else
1746 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1747 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1748 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1749 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1750 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1751 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1753 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1754 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1758 /* Keep the old rtl since we can safely use it. */
1759 COPY_DECL_RTL (olddecl, newdecl);
1761 /* Merge the type qualifiers. */
1762 if (TREE_READONLY (newdecl))
1763 TREE_READONLY (write_olddecl) = 1;
1765 if (TREE_THIS_VOLATILE (newdecl))
1767 TREE_THIS_VOLATILE (write_olddecl) = 1;
1768 if (TREE_CODE (newdecl) == VAR_DECL
1769 /* If an automatic variable is re-declared in the same
1770 function scope, but the old declaration was not
1771 volatile, make_var_volatile() would crash because the
1772 variable would have been assigned to a pseudo, not a
1773 MEM. Since this duplicate declaration is invalid
1774 anyway, we just skip the call. */
1775 && errmsg == 0)
1776 make_var_volatile (newdecl);
1779 /* Keep source location of definition rather than declaration. */
1780 /* When called with different_binding_level set, keep the old
1781 information so that meaningful diagnostics can be given. */
1782 if (DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0
1783 && ! different_binding_level)
1785 DECL_SOURCE_LINE (newdecl) = DECL_SOURCE_LINE (olddecl);
1786 DECL_SOURCE_FILE (newdecl) = DECL_SOURCE_FILE (olddecl);
1789 /* Merge the unused-warning information. */
1790 if (DECL_IN_SYSTEM_HEADER (olddecl))
1791 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1792 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1793 DECL_IN_SYSTEM_HEADER (write_olddecl) = 1;
1795 /* Merge the initialization information. */
1796 /* When called with different_binding_level set, don't copy over
1797 DECL_INITIAL, so that we don't accidentally change function
1798 declarations into function definitions. */
1799 if (DECL_INITIAL (newdecl) == 0 && ! different_binding_level)
1800 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1802 /* Merge the section attribute.
1803 We want to issue an error if the sections conflict but that must be
1804 done later in decl_attributes since we are called before attributes
1805 are assigned. */
1806 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1807 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1809 /* Copy the assembler name.
1810 Currently, it can only be defined in the prototype. */
1811 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1813 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1815 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1816 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1817 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1818 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1819 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1822 /* If cannot merge, then use the new type and qualifiers,
1823 and don't preserve the old rtl. */
1824 else if (! different_binding_level)
1826 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1827 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
1828 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
1829 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
1832 /* Merge the storage class information. */
1833 merge_weak (newdecl, olddecl);
1835 /* For functions, static overrides non-static. */
1836 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1838 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1839 /* This is since we don't automatically
1840 copy the attributes of NEWDECL into OLDDECL. */
1841 /* No need to worry about different_binding_level here because
1842 then TREE_PUBLIC (newdecl) was true. */
1843 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1844 /* If this clears `static', clear it in the identifier too. */
1845 if (! TREE_PUBLIC (olddecl))
1846 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1848 if (DECL_EXTERNAL (newdecl))
1850 if (! different_binding_level)
1852 /* Don't mess with these flags on local externs; they remain
1853 external even if there's a declaration at file scope which
1854 isn't. */
1855 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1856 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1858 /* An extern decl does not override previous storage class. */
1859 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1860 if (! DECL_EXTERNAL (newdecl))
1861 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1863 else
1865 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1866 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1869 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1871 /* If we're redefining a function previously defined as extern
1872 inline, make sure we emit debug info for the inline before we
1873 throw it away, in case it was inlined into a function that hasn't
1874 been written out yet. */
1875 if (new_is_definition && DECL_INITIAL (olddecl) && TREE_USED (olddecl))
1877 (*debug_hooks->outlining_inline_function) (olddecl);
1879 /* The new defn must not be inline. */
1880 DECL_INLINE (newdecl) = 0;
1881 DECL_UNINLINABLE (newdecl) = 1;
1883 else
1885 /* If either decl says `inline', this fn is inline,
1886 unless its definition was passed already. */
1887 if (DECL_DECLARED_INLINE_P (newdecl)
1888 || DECL_DECLARED_INLINE_P (olddecl))
1889 DECL_DECLARED_INLINE_P (newdecl) = 1;
1891 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1892 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1895 if (DECL_BUILT_IN (olddecl))
1897 /* Get rid of any built-in function if new arg types don't match it
1898 or if we have a function definition. */
1899 if (! types_match || new_is_definition)
1901 if (! different_binding_level)
1903 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1904 DECL_BUILT_IN_CLASS (olddecl) = NOT_BUILT_IN;
1907 else
1909 /* If redeclaring a builtin function, and not a definition,
1910 it stays built in. */
1911 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1912 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1916 /* Also preserve various other info from the definition. */
1917 if (! new_is_definition)
1919 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1920 /* When called with different_binding_level set, don't copy over
1921 DECL_INITIAL, so that we don't accidentally change function
1922 declarations into function definitions. */
1923 if (! different_binding_level)
1924 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1925 DECL_SAVED_INSNS (newdecl) = DECL_SAVED_INSNS (olddecl);
1926 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1927 DECL_NUM_STMTS (newdecl) = DECL_NUM_STMTS (olddecl);
1928 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1930 /* Set DECL_INLINE on the declaration if we've got a body
1931 from which to instantiate. */
1932 if (DECL_INLINE (olddecl) && ! DECL_UNINLINABLE (newdecl))
1934 DECL_INLINE (newdecl) = 1;
1935 DECL_ABSTRACT_ORIGIN (newdecl)
1936 = (different_binding_level
1937 ? DECL_ORIGIN (olddecl)
1938 : DECL_ABSTRACT_ORIGIN (olddecl));
1941 else
1943 /* If a previous declaration said inline, mark the
1944 definition as inlinable. */
1945 if (DECL_DECLARED_INLINE_P (newdecl)
1946 && ! DECL_UNINLINABLE (newdecl))
1947 DECL_INLINE (newdecl) = 1;
1950 if (different_binding_level)
1951 return 0;
1953 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1954 But preserve OLDDECL's DECL_UID. */
1956 unsigned olddecl_uid = DECL_UID (olddecl);
1958 memcpy ((char *) olddecl + sizeof (struct tree_common),
1959 (char *) newdecl + sizeof (struct tree_common),
1960 sizeof (struct tree_decl) - sizeof (struct tree_common));
1961 DECL_UID (olddecl) = olddecl_uid;
1964 /* NEWDECL contains the merged attribute lists.
1965 Update OLDDECL to be the same. */
1966 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
1968 return 1;
1971 /* Check whether decl-node X shadows an existing declaration.
1972 OLDLOCAL is the old IDENTIFIER_LOCAL_VALUE of the DECL_NAME of X,
1973 which might be a NULL_TREE. */
1974 static void
1975 warn_if_shadowing (x, oldlocal)
1976 tree x, oldlocal;
1978 tree name;
1980 if (DECL_EXTERNAL (x))
1981 return;
1983 name = DECL_NAME (x);
1985 /* Warn if shadowing an argument at the top level of the body. */
1986 if (oldlocal != 0
1987 /* This warning doesn't apply to the parms of a nested fcn. */
1988 && ! current_binding_level->parm_flag
1989 /* Check that this is one level down from the parms. */
1990 && current_binding_level->level_chain->parm_flag
1991 /* Check that the decl being shadowed
1992 comes from the parm level, one level up. */
1993 && chain_member (oldlocal, current_binding_level->level_chain->names))
1995 if (TREE_CODE (oldlocal) == PARM_DECL)
1996 pedwarn ("declaration of `%s' shadows a parameter",
1997 IDENTIFIER_POINTER (name));
1998 else
1999 pedwarn ("declaration of `%s' shadows a symbol from the parameter list",
2000 IDENTIFIER_POINTER (name));
2002 /* Maybe warn if shadowing something else. */
2003 else if (warn_shadow
2004 /* No shadow warnings for internally generated vars. */
2005 && DECL_SOURCE_LINE (x) != 0
2006 /* No shadow warnings for vars made for inlining. */
2007 && ! DECL_FROM_INLINE (x))
2009 if (TREE_CODE (x) == PARM_DECL
2010 && current_binding_level->level_chain->parm_flag)
2011 /* Don't warn about the parm names in function declarator
2012 within a function declarator.
2013 It would be nice to avoid warning in any function
2014 declarator in a declaration, as opposed to a definition,
2015 but there is no way to tell it's not a definition. */
2017 else if (oldlocal)
2019 if (TREE_CODE (oldlocal) == PARM_DECL)
2020 shadow_warning ("a parameter", name, oldlocal);
2021 else
2022 shadow_warning ("a previous local", name, oldlocal);
2024 else if (IDENTIFIER_GLOBAL_VALUE (name) != 0
2025 && IDENTIFIER_GLOBAL_VALUE (name) != error_mark_node)
2026 shadow_warning ("a global declaration", name,
2027 IDENTIFIER_GLOBAL_VALUE (name));
2031 /* Record a decl-node X as belonging to the current lexical scope.
2032 Check for errors (such as an incompatible declaration for the same
2033 name already seen in the same scope).
2035 Returns either X or an old decl for the same name.
2036 If an old decl is returned, it may have been smashed
2037 to agree with what X says. */
2039 tree
2040 pushdecl (x)
2041 tree x;
2043 tree t;
2044 tree name = DECL_NAME (x);
2045 struct binding_level *b = current_binding_level;
2047 /* Functions need the lang_decl data. */
2048 if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_LANG_SPECIFIC (x))
2049 DECL_LANG_SPECIFIC (x) = (struct lang_decl *)
2050 ggc_alloc_cleared (sizeof (struct lang_decl));
2052 DECL_CONTEXT (x) = current_function_decl;
2053 /* A local extern declaration for a function doesn't constitute nesting.
2054 A local auto declaration does, since it's a forward decl
2055 for a nested function coming later. */
2056 if ((TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
2057 && DECL_INITIAL (x) == 0 && DECL_EXTERNAL (x))
2058 DECL_CONTEXT (x) = 0;
2060 if (name)
2062 int different_binding_level = 0;
2064 if (warn_nested_externs
2065 && DECL_EXTERNAL (x)
2066 && b != global_binding_level
2067 && x != IDENTIFIER_IMPLICIT_DECL (name)
2068 /* No error messages for __FUNCTION__ and __PRETTY_FUNCTION__. */
2069 && !DECL_IN_SYSTEM_HEADER (x))
2070 warning ("nested extern declaration of `%s'",
2071 IDENTIFIER_POINTER (name));
2073 t = lookup_name_current_level (name);
2074 if (! t && DECL_EXTERNAL (x) && TREE_PUBLIC (x))
2076 t = IDENTIFIER_GLOBAL_VALUE (name);
2077 /* Type decls at global scope don't conflict with externs declared
2078 inside lexical blocks. */
2079 if (! t || TREE_CODE (t) == TYPE_DECL)
2080 /* If there's no visible global declaration, try for an
2081 invisible one. */
2082 t = IDENTIFIER_LIMBO_VALUE (name);
2083 different_binding_level = 1;
2085 if (t != 0 && t == error_mark_node)
2086 /* error_mark_node is 0 for a while during initialization! */
2088 t = 0;
2089 error_with_decl (x, "`%s' used prior to declaration");
2092 /* If this decl is `static' and an implicit decl was seen previously,
2093 warn. */
2094 if (TREE_PUBLIC (name)
2095 /* Don't test for DECL_EXTERNAL, because grokdeclarator
2096 sets this for all functions. */
2097 && ! TREE_PUBLIC (x)
2098 && (TREE_CODE (x) == FUNCTION_DECL || b == global_binding_level)
2099 /* We used to warn also for explicit extern followed by static,
2100 but sometimes you need to do it that way. */
2101 && IDENTIFIER_IMPLICIT_DECL (name) != 0)
2103 pedwarn ("`%s' was declared implicitly `extern' and later `static'",
2104 IDENTIFIER_POINTER (name));
2105 pedwarn_with_file_and_line
2106 (DECL_SOURCE_FILE (IDENTIFIER_IMPLICIT_DECL (name)),
2107 DECL_SOURCE_LINE (IDENTIFIER_IMPLICIT_DECL (name)),
2108 "previous declaration of `%s'",
2109 IDENTIFIER_POINTER (name));
2110 TREE_THIS_VOLATILE (name) = 1;
2113 if (t != 0 && duplicate_decls (x, t, different_binding_level))
2115 if (TREE_CODE (t) == PARM_DECL)
2117 /* Don't allow more than one "real" duplicate
2118 of a forward parm decl. */
2119 TREE_ASM_WRITTEN (t) = TREE_ASM_WRITTEN (x);
2120 return t;
2122 return t;
2125 /* If we are processing a typedef statement, generate a whole new
2126 ..._TYPE node (which will be just an variant of the existing
2127 ..._TYPE node with identical properties) and then install the
2128 TYPE_DECL node generated to represent the typedef name as the
2129 TYPE_NAME of this brand new (duplicate) ..._TYPE node.
2131 The whole point here is to end up with a situation where each
2132 and every ..._TYPE node the compiler creates will be uniquely
2133 associated with AT MOST one node representing a typedef name.
2134 This way, even though the compiler substitutes corresponding
2135 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
2136 early on, later parts of the compiler can always do the reverse
2137 translation and get back the corresponding typedef name. For
2138 example, given:
2140 typedef struct S MY_TYPE;
2141 MY_TYPE object;
2143 Later parts of the compiler might only know that `object' was of
2144 type `struct S' if it were not for code just below. With this
2145 code however, later parts of the compiler see something like:
2147 struct S' == struct S
2148 typedef struct S' MY_TYPE;
2149 struct S' object;
2151 And they can then deduce (from the node for type struct S') that
2152 the original object declaration was:
2154 MY_TYPE object;
2156 Being able to do this is important for proper support of protoize,
2157 and also for generating precise symbolic debugging information
2158 which takes full account of the programmer's (typedef) vocabulary.
2160 Obviously, we don't want to generate a duplicate ..._TYPE node if
2161 the TYPE_DECL node that we are now processing really represents a
2162 standard built-in type.
2164 Since all standard types are effectively declared at line zero
2165 in the source file, we can easily check to see if we are working
2166 on a standard type by checking the current value of lineno. */
2168 if (TREE_CODE (x) == TYPE_DECL)
2170 if (DECL_SOURCE_LINE (x) == 0)
2172 if (TYPE_NAME (TREE_TYPE (x)) == 0)
2173 TYPE_NAME (TREE_TYPE (x)) = x;
2175 else if (TREE_TYPE (x) != error_mark_node
2176 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
2178 tree tt = TREE_TYPE (x);
2179 DECL_ORIGINAL_TYPE (x) = tt;
2180 tt = build_type_copy (tt);
2181 TYPE_NAME (tt) = x;
2182 TREE_USED (tt) = TREE_USED (x);
2183 TREE_TYPE (x) = tt;
2187 /* Multiple external decls of the same identifier ought to match.
2188 We get warnings about inline functions where they are defined.
2189 Avoid duplicate warnings where they are used. */
2190 if (TREE_PUBLIC (x)
2191 && ! (TREE_CODE (x) == FUNCTION_DECL && DECL_INLINE (x)))
2193 tree decl;
2195 if (IDENTIFIER_LIMBO_VALUE (name) != 0)
2196 /* Decls in limbo are always extern, so no need to check that. */
2197 decl = IDENTIFIER_LIMBO_VALUE (name);
2198 else
2199 decl = 0;
2201 if (decl && ! comptypes (TREE_TYPE (x), TREE_TYPE (decl))
2202 /* If old decl is built-in, we already warned if we should. */
2203 && !DECL_BUILT_IN (decl))
2205 pedwarn_with_decl (x,
2206 "type mismatch with previous external decl");
2207 pedwarn_with_decl (decl, "previous external decl of `%s'");
2211 /* If a function has had an implicit declaration, and then is defined,
2212 make sure they are compatible. */
2214 if (IDENTIFIER_IMPLICIT_DECL (name) != 0
2215 && IDENTIFIER_GLOBAL_VALUE (name) == 0
2216 && TREE_CODE (x) == FUNCTION_DECL
2217 && ! comptypes (TREE_TYPE (x),
2218 TREE_TYPE (IDENTIFIER_IMPLICIT_DECL (name))))
2220 warning_with_decl (x, "type mismatch with previous implicit declaration");
2221 warning_with_decl (IDENTIFIER_IMPLICIT_DECL (name),
2222 "previous implicit declaration of `%s'");
2225 /* This name is new in its binding level.
2226 Install the new declaration and return it. */
2227 if (b == global_binding_level)
2229 /* Install a global value. */
2231 /* If the first global decl has external linkage,
2232 warn if we later see static one. */
2233 if (IDENTIFIER_GLOBAL_VALUE (name) == 0 && TREE_PUBLIC (x))
2234 TREE_PUBLIC (name) = 1;
2236 IDENTIFIER_GLOBAL_VALUE (name) = x;
2238 /* We no longer care about any previous block level declarations. */
2239 IDENTIFIER_LIMBO_VALUE (name) = 0;
2241 /* Don't forget if the function was used via an implicit decl. */
2242 if (IDENTIFIER_IMPLICIT_DECL (name)
2243 && TREE_USED (IDENTIFIER_IMPLICIT_DECL (name)))
2244 TREE_USED (x) = 1, TREE_USED (name) = 1;
2246 /* Don't forget if its address was taken in that way. */
2247 if (IDENTIFIER_IMPLICIT_DECL (name)
2248 && TREE_ADDRESSABLE (IDENTIFIER_IMPLICIT_DECL (name)))
2249 TREE_ADDRESSABLE (x) = 1;
2251 /* Warn about mismatches against previous implicit decl. */
2252 if (IDENTIFIER_IMPLICIT_DECL (name) != 0
2253 /* If this real decl matches the implicit, don't complain. */
2254 && ! (TREE_CODE (x) == FUNCTION_DECL
2255 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (x)))
2256 == integer_type_node)))
2257 pedwarn ("`%s' was previously implicitly declared to return `int'",
2258 IDENTIFIER_POINTER (name));
2260 /* If this decl is `static' and an `extern' was seen previously,
2261 that is erroneous. */
2262 if (TREE_PUBLIC (name)
2263 && ! TREE_PUBLIC (x) && ! DECL_EXTERNAL (x))
2265 /* Okay to redeclare an ANSI built-in as static. */
2266 if (t != 0 && DECL_BUILT_IN (t))
2268 /* Okay to declare a non-ANSI built-in as anything. */
2269 else if (t != 0 && DECL_BUILT_IN_NONANSI (t))
2271 /* Okay to have global type decl after an earlier extern
2272 declaration inside a lexical block. */
2273 else if (TREE_CODE (x) == TYPE_DECL)
2275 else if (IDENTIFIER_IMPLICIT_DECL (name))
2277 if (! TREE_THIS_VOLATILE (name))
2278 pedwarn ("`%s' was declared implicitly `extern' and later `static'",
2279 IDENTIFIER_POINTER (name));
2281 else
2282 pedwarn ("`%s' was declared `extern' and later `static'",
2283 IDENTIFIER_POINTER (name));
2286 else
2288 /* Here to install a non-global value. */
2289 tree oldlocal = IDENTIFIER_LOCAL_VALUE (name);
2290 tree oldglobal = IDENTIFIER_GLOBAL_VALUE (name);
2292 IDENTIFIER_LOCAL_VALUE (name) = x;
2294 /* If this is an extern function declaration, see if we
2295 have a global definition or declaration for the function. */
2296 if (oldlocal == 0
2297 && oldglobal != 0
2298 && TREE_CODE (x) == FUNCTION_DECL
2299 && TREE_CODE (oldglobal) == FUNCTION_DECL
2300 && DECL_EXTERNAL (x)
2301 && ! DECL_DECLARED_INLINE_P (x))
2303 /* We have one. Their types must agree. */
2304 if (! comptypes (TREE_TYPE (x),
2305 TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (name))))
2306 pedwarn_with_decl (x, "extern declaration of `%s' doesn't match global one");
2307 else
2309 /* Inner extern decl is inline if global one is.
2310 Copy enough to really inline it. */
2311 if (DECL_DECLARED_INLINE_P (oldglobal))
2313 DECL_DECLARED_INLINE_P (x)
2314 = DECL_DECLARED_INLINE_P (oldglobal);
2315 DECL_INLINE (x) = DECL_INLINE (oldglobal);
2316 DECL_INITIAL (x) = (current_function_decl == oldglobal
2317 ? 0 : DECL_INITIAL (oldglobal));
2318 DECL_SAVED_INSNS (x) = DECL_SAVED_INSNS (oldglobal);
2319 DECL_NUM_STMTS (x) = DECL_NUM_STMTS (oldglobal);
2320 DECL_ARGUMENTS (x) = DECL_ARGUMENTS (oldglobal);
2321 DECL_RESULT (x) = DECL_RESULT (oldglobal);
2322 TREE_ASM_WRITTEN (x) = TREE_ASM_WRITTEN (oldglobal);
2323 DECL_ABSTRACT_ORIGIN (x)
2324 = DECL_ABSTRACT_ORIGIN (oldglobal);
2326 /* Inner extern decl is built-in if global one is. */
2327 if (DECL_BUILT_IN (oldglobal))
2329 DECL_BUILT_IN_CLASS (x) = DECL_BUILT_IN_CLASS (oldglobal);
2330 DECL_FUNCTION_CODE (x) = DECL_FUNCTION_CODE (oldglobal);
2332 /* Keep the arg types from a file-scope fcn defn. */
2333 if (TYPE_ARG_TYPES (TREE_TYPE (oldglobal)) != 0
2334 && DECL_INITIAL (oldglobal)
2335 && TYPE_ARG_TYPES (TREE_TYPE (x)) == 0)
2336 TREE_TYPE (x) = TREE_TYPE (oldglobal);
2340 #if 0
2341 /* This case is probably sometimes the right thing to do. */
2342 /* If we have a local external declaration,
2343 then any file-scope declaration should not
2344 have been static. */
2345 if (oldlocal == 0 && oldglobal != 0
2346 && !TREE_PUBLIC (oldglobal)
2347 && DECL_EXTERNAL (x) && TREE_PUBLIC (x))
2348 warning ("`%s' locally external but globally static",
2349 IDENTIFIER_POINTER (name));
2350 #endif
2352 /* If we have a local external declaration,
2353 and no file-scope declaration has yet been seen,
2354 then if we later have a file-scope decl it must not be static. */
2355 if (oldlocal == 0
2356 && DECL_EXTERNAL (x)
2357 && TREE_PUBLIC (x))
2359 if (oldglobal == 0)
2360 TREE_PUBLIC (name) = 1;
2362 /* Save this decl, so that we can do type checking against
2363 other decls after it falls out of scope.
2365 Only save it once. This prevents temporary decls created in
2366 expand_inline_function from being used here, since this
2367 will have been set when the inline function was parsed.
2368 It also helps give slightly better warnings. */
2369 if (IDENTIFIER_LIMBO_VALUE (name) == 0)
2370 IDENTIFIER_LIMBO_VALUE (name) = x;
2373 warn_if_shadowing (x, oldlocal);
2375 /* If storing a local value, there may already be one (inherited).
2376 If so, record it for restoration when this binding level ends. */
2377 if (oldlocal != 0)
2378 b->shadowed = tree_cons (name, oldlocal, b->shadowed);
2381 /* Keep list of variables in this level with incomplete type.
2382 If the input is erroneous, we can have error_mark in the type
2383 slot (e.g. "f(void a, ...)") - that doesn't count as an
2384 incomplete type. */
2385 if (TREE_TYPE (x) != error_mark_node
2386 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2388 tree element = TREE_TYPE (x);
2390 while (TREE_CODE (element) == ARRAY_TYPE)
2391 element = TREE_TYPE (element);
2392 if (TREE_CODE (element) == RECORD_TYPE
2393 || TREE_CODE (element) == UNION_TYPE)
2394 b->incomplete_list = tree_cons (NULL_TREE, x, b->incomplete_list);
2398 /* Put decls on list in reverse order.
2399 We will reverse them later if necessary. */
2400 TREE_CHAIN (x) = b->names;
2401 b->names = x;
2403 return x;
2406 /* Like pushdecl, only it places X in GLOBAL_BINDING_LEVEL, if appropriate. */
2408 tree
2409 pushdecl_top_level (x)
2410 tree x;
2412 tree t;
2413 struct binding_level *b = current_binding_level;
2415 current_binding_level = global_binding_level;
2416 t = pushdecl (x);
2417 current_binding_level = b;
2418 return t;
2421 /* Generate an implicit declaration for identifier FUNCTIONID
2422 as a function of type int (). Print a warning if appropriate. */
2424 tree
2425 implicitly_declare (functionid)
2426 tree functionid;
2428 tree decl;
2429 int traditional_warning = 0;
2430 /* Only one "implicit declaration" warning per identifier. */
2431 int implicit_warning;
2433 /* We used to reuse an old implicit decl here,
2434 but this loses with inline functions because it can clobber
2435 the saved decl chains. */
2436 #if 0
2437 if (IDENTIFIER_IMPLICIT_DECL (functionid) != 0)
2438 decl = IDENTIFIER_IMPLICIT_DECL (functionid);
2439 else
2440 #endif
2441 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2443 /* Warn of implicit decl following explicit local extern decl.
2444 This is probably a program designed for traditional C. */
2445 if (TREE_PUBLIC (functionid) && IDENTIFIER_GLOBAL_VALUE (functionid) == 0)
2446 traditional_warning = 1;
2448 /* Warn once of an implicit declaration. */
2449 implicit_warning = (IDENTIFIER_IMPLICIT_DECL (functionid) == 0);
2451 DECL_EXTERNAL (decl) = 1;
2452 TREE_PUBLIC (decl) = 1;
2454 /* Record that we have an implicit decl and this is it. */
2455 IDENTIFIER_IMPLICIT_DECL (functionid) = decl;
2457 /* ANSI standard says implicit declarations are in the innermost block.
2458 So we record the decl in the standard fashion. */
2459 pushdecl (decl);
2461 /* This is a no-op in c-lang.c or something real in objc-actions.c. */
2462 maybe_objc_check_decl (decl);
2464 rest_of_decl_compilation (decl, NULL, 0, 0);
2466 if (implicit_warning)
2467 implicit_decl_warning (functionid);
2468 else if (warn_traditional && traditional_warning)
2469 warning ("function `%s' was previously declared within a block",
2470 IDENTIFIER_POINTER (functionid));
2472 /* Write a record describing this implicit function declaration to the
2473 prototypes file (if requested). */
2475 gen_aux_info_record (decl, 0, 1, 0);
2477 /* Possibly apply some default attributes to this implicit declaration. */
2478 decl_attributes (&decl, NULL_TREE, 0);
2480 return decl;
2483 void
2484 implicit_decl_warning (id)
2485 tree id;
2487 const char *name = IDENTIFIER_POINTER (id);
2488 if (mesg_implicit_function_declaration == 2)
2489 error ("implicit declaration of function `%s'", name);
2490 else if (mesg_implicit_function_declaration == 1)
2491 warning ("implicit declaration of function `%s'", name);
2494 /* Return zero if the declaration NEWDECL is valid
2495 when the declaration OLDDECL (assumed to be for the same name)
2496 has already been seen.
2497 Otherwise return 1 if NEWDECL is a redefinition, 2 if it is a redeclaration,
2498 and 3 if it is a conflicting declaration. */
2500 static int
2501 redeclaration_error_message (newdecl, olddecl)
2502 tree newdecl, olddecl;
2504 if (TREE_CODE (newdecl) == TYPE_DECL)
2506 /* Do not complain about type redeclarations where at least one
2507 declaration was in a system header. */
2508 if (DECL_IN_SYSTEM_HEADER (olddecl) || DECL_IN_SYSTEM_HEADER (newdecl))
2509 return 0;
2510 return 1;
2512 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2514 /* Declarations of functions can insist on internal linkage
2515 but they can't be inconsistent with internal linkage,
2516 so there can be no error on that account.
2517 However defining the same name twice is no good. */
2518 if (DECL_INITIAL (olddecl) != 0 && DECL_INITIAL (newdecl) != 0
2519 /* However, defining once as extern inline and a second
2520 time in another way is ok. */
2521 && ! (DECL_DECLARED_INLINE_P (olddecl) && DECL_EXTERNAL (olddecl)
2522 && ! (DECL_DECLARED_INLINE_P (newdecl)
2523 && DECL_EXTERNAL (newdecl))))
2524 return 1;
2525 return 0;
2527 else if (DECL_CONTEXT (newdecl) == NULL_TREE)
2529 /* Objects declared at top level: */
2530 /* If at least one is a reference, it's ok. */
2531 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
2532 return 0;
2533 /* Reject two definitions. */
2534 if (DECL_INITIAL (olddecl) != 0 && DECL_INITIAL (newdecl) != 0)
2535 return 1;
2536 /* Now we have two tentative defs, or one tentative and one real def. */
2537 /* Insist that the linkage match. */
2538 if (TREE_PUBLIC (olddecl) != TREE_PUBLIC (newdecl))
2539 return 3;
2540 return 0;
2542 else if (current_binding_level->parm_flag
2543 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2544 return 0;
2545 else
2547 /* Newdecl has block scope. If olddecl has block scope also, then
2548 reject two definitions, and reject a definition together with an
2549 external reference. Otherwise, it is OK, because newdecl must
2550 be an extern reference to olddecl. */
2551 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl))
2552 && DECL_CONTEXT (newdecl) == DECL_CONTEXT (olddecl))
2553 return 2;
2554 return 0;
2558 /* Get the LABEL_DECL corresponding to identifier ID as a label.
2559 Create one if none exists so far for the current function.
2560 This function is called for both label definitions and label references. */
2562 tree
2563 lookup_label (id)
2564 tree id;
2566 tree decl = IDENTIFIER_LABEL_VALUE (id);
2568 if (current_function_decl == 0)
2570 error ("label %s referenced outside of any function",
2571 IDENTIFIER_POINTER (id));
2572 return 0;
2575 /* Use a label already defined or ref'd with this name. */
2576 if (decl != 0)
2578 /* But not if it is inherited and wasn't declared to be inheritable. */
2579 if (DECL_CONTEXT (decl) != current_function_decl
2580 && ! C_DECLARED_LABEL_FLAG (decl))
2581 return shadow_label (id);
2582 return decl;
2585 decl = build_decl (LABEL_DECL, id, void_type_node);
2587 /* A label not explicitly declared must be local to where it's ref'd. */
2588 DECL_CONTEXT (decl) = current_function_decl;
2590 DECL_MODE (decl) = VOIDmode;
2592 /* Say where one reference is to the label,
2593 for the sake of the error if it is not defined. */
2594 DECL_SOURCE_LINE (decl) = lineno;
2595 DECL_SOURCE_FILE (decl) = input_filename;
2597 IDENTIFIER_LABEL_VALUE (id) = decl;
2599 named_labels = tree_cons (NULL_TREE, decl, named_labels);
2601 return decl;
2604 /* Make a label named NAME in the current function,
2605 shadowing silently any that may be inherited from containing functions
2606 or containing scopes.
2608 Note that valid use, if the label being shadowed
2609 comes from another scope in the same function,
2610 requires calling declare_nonlocal_label right away. */
2612 tree
2613 shadow_label (name)
2614 tree name;
2616 tree decl = IDENTIFIER_LABEL_VALUE (name);
2618 if (decl != 0)
2620 tree dup;
2622 /* Check to make sure that the label hasn't already been declared
2623 at this label scope */
2624 for (dup = named_labels; dup; dup = TREE_CHAIN (dup))
2625 if (TREE_VALUE (dup) == decl)
2627 error ("duplicate label declaration `%s'",
2628 IDENTIFIER_POINTER (name));
2629 error_with_decl (TREE_VALUE (dup),
2630 "this is a previous declaration");
2631 /* Just use the previous declaration. */
2632 return lookup_label (name);
2635 shadowed_labels = tree_cons (NULL_TREE, decl, shadowed_labels);
2636 IDENTIFIER_LABEL_VALUE (name) = decl = 0;
2639 return lookup_label (name);
2642 /* Define a label, specifying the location in the source file.
2643 Return the LABEL_DECL node for the label, if the definition is valid.
2644 Otherwise return 0. */
2646 tree
2647 define_label (filename, line, name)
2648 const char *filename;
2649 int line;
2650 tree name;
2652 tree decl = lookup_label (name);
2654 /* If label with this name is known from an outer context, shadow it. */
2655 if (decl != 0 && DECL_CONTEXT (decl) != current_function_decl)
2657 shadowed_labels = tree_cons (NULL_TREE, decl, shadowed_labels);
2658 IDENTIFIER_LABEL_VALUE (name) = 0;
2659 decl = lookup_label (name);
2662 if (warn_traditional && !in_system_header && lookup_name (name))
2663 warning_with_file_and_line (filename, line,
2664 "traditional C lacks a separate namespace for labels, identifier `%s' conflicts",
2665 IDENTIFIER_POINTER (name));
2667 if (DECL_INITIAL (decl) != 0)
2669 error_with_file_and_line (filename, line, "duplicate label `%s'",
2670 IDENTIFIER_POINTER (name));
2671 return 0;
2673 else
2675 /* Mark label as having been defined. */
2676 DECL_INITIAL (decl) = error_mark_node;
2677 /* Say where in the source. */
2678 DECL_SOURCE_FILE (decl) = filename;
2679 DECL_SOURCE_LINE (decl) = line;
2680 return decl;
2684 /* Return the list of declarations of the current level.
2685 Note that this list is in reverse order unless/until
2686 you nreverse it; and when you do nreverse it, you must
2687 store the result back using `storedecls' or you will lose. */
2689 tree
2690 getdecls ()
2692 return current_binding_level->names;
2695 /* Return the list of type-tags (for structs, etc) of the current level. */
2697 tree
2698 gettags ()
2700 return current_binding_level->tags;
2703 /* Store the list of declarations of the current level.
2704 This is done for the parameter declarations of a function being defined,
2705 after they are modified in the light of any missing parameters. */
2707 static void
2708 storedecls (decls)
2709 tree decls;
2711 current_binding_level->names = decls;
2714 /* Similarly, store the list of tags of the current level. */
2716 static void
2717 storetags (tags)
2718 tree tags;
2720 current_binding_level->tags = tags;
2723 /* Given NAME, an IDENTIFIER_NODE,
2724 return the structure (or union or enum) definition for that name.
2725 Searches binding levels from BINDING_LEVEL up to the global level.
2726 If THISLEVEL_ONLY is nonzero, searches only the specified context
2727 (but skips any tag-transparent contexts to find one that is
2728 meaningful for tags).
2729 CODE says which kind of type the caller wants;
2730 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2731 If the wrong kind of type is found, an error is reported. */
2733 static tree
2734 lookup_tag (code, name, binding_level, thislevel_only)
2735 enum tree_code code;
2736 struct binding_level *binding_level;
2737 tree name;
2738 int thislevel_only;
2740 struct binding_level *level;
2741 int thislevel = 1;
2743 for (level = binding_level; level; level = level->level_chain)
2745 tree tail;
2746 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
2748 if (TREE_PURPOSE (tail) == name)
2750 if (TREE_CODE (TREE_VALUE (tail)) != code)
2752 /* Definition isn't the kind we were looking for. */
2753 pending_invalid_xref = name;
2754 pending_invalid_xref_file = input_filename;
2755 pending_invalid_xref_line = lineno;
2756 /* If in the same binding level as a declaration as a tag
2757 of a different type, this must not be allowed to
2758 shadow that tag, so give the error immediately.
2759 (For example, "struct foo; union foo;" is invalid.) */
2760 if (thislevel)
2761 pending_xref_error ();
2763 return TREE_VALUE (tail);
2766 if (! level->tag_transparent)
2768 if (thislevel_only)
2769 return NULL_TREE;
2770 thislevel = 0;
2773 return NULL_TREE;
2776 /* Print an error message now
2777 for a recent invalid struct, union or enum cross reference.
2778 We don't print them immediately because they are not invalid
2779 when used in the `struct foo;' construct for shadowing. */
2781 void
2782 pending_xref_error ()
2784 if (pending_invalid_xref != 0)
2785 error_with_file_and_line (pending_invalid_xref_file,
2786 pending_invalid_xref_line,
2787 "`%s' defined as wrong kind of tag",
2788 IDENTIFIER_POINTER (pending_invalid_xref));
2789 pending_invalid_xref = 0;
2792 /* Given a type, find the tag that was defined for it and return the tag name.
2793 Otherwise return 0. */
2795 static tree
2796 lookup_tag_reverse (type)
2797 tree type;
2799 struct binding_level *level;
2801 for (level = current_binding_level; level; level = level->level_chain)
2803 tree tail;
2804 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
2806 if (TREE_VALUE (tail) == type)
2807 return TREE_PURPOSE (tail);
2810 return NULL_TREE;
2813 /* Look up NAME in the current binding level and its superiors
2814 in the namespace of variables, functions and typedefs.
2815 Return a ..._DECL node of some kind representing its definition,
2816 or return 0 if it is undefined. */
2818 tree
2819 lookup_name (name)
2820 tree name;
2822 tree val;
2824 if (current_binding_level != global_binding_level
2825 && IDENTIFIER_LOCAL_VALUE (name))
2826 val = IDENTIFIER_LOCAL_VALUE (name);
2827 else
2828 val = IDENTIFIER_GLOBAL_VALUE (name);
2829 return val;
2832 /* Similar to `lookup_name' but look only at current binding level. */
2834 tree
2835 lookup_name_current_level (name)
2836 tree name;
2838 tree t;
2840 if (current_binding_level == global_binding_level)
2841 return IDENTIFIER_GLOBAL_VALUE (name);
2843 if (IDENTIFIER_LOCAL_VALUE (name) == 0)
2844 return 0;
2846 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
2847 if (DECL_NAME (t) == name)
2848 break;
2850 return t;
2853 /* Create the predefined scalar types of C,
2854 and some nodes representing standard constants (0, 1, (void *) 0).
2855 Initialize the global binding level.
2856 Make definitions for built-in primitive functions. */
2858 void
2859 c_init_decl_processing ()
2861 tree endlink;
2862 tree ptr_ftype_void, ptr_ftype_ptr;
2864 /* Adds some ggc roots, and reserved words for c-parse.in. */
2865 c_parse_init ();
2867 current_function_decl = NULL;
2868 named_labels = NULL;
2869 current_binding_level = NULL_BINDING_LEVEL;
2870 free_binding_level = NULL_BINDING_LEVEL;
2872 /* Make the binding_level structure for global names. */
2873 pushlevel (0);
2874 global_binding_level = current_binding_level;
2876 build_common_tree_nodes (flag_signed_char);
2878 c_common_nodes_and_builtins ();
2880 boolean_type_node = integer_type_node;
2881 boolean_true_node = integer_one_node;
2882 boolean_false_node = integer_zero_node;
2884 c_bool_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
2885 TREE_SET_CODE (c_bool_type_node, BOOLEAN_TYPE);
2886 TYPE_MAX_VALUE (c_bool_type_node) = build_int_2 (1, 0);
2887 TREE_TYPE (TYPE_MAX_VALUE (c_bool_type_node)) = c_bool_type_node;
2888 TYPE_PRECISION (c_bool_type_node) = 1;
2889 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2890 c_bool_type_node));
2891 c_bool_false_node = build_int_2 (0, 0);
2892 TREE_TYPE (c_bool_false_node) = c_bool_type_node;
2893 c_bool_true_node = build_int_2 (1, 0);
2894 TREE_TYPE (c_bool_true_node) = c_bool_type_node;
2896 endlink = void_list_node;
2897 ptr_ftype_void = build_function_type (ptr_type_node, endlink);
2898 ptr_ftype_ptr
2899 = build_function_type (ptr_type_node,
2900 tree_cons (NULL_TREE, ptr_type_node, endlink));
2902 pedantic_lvalues = pedantic;
2904 make_fname_decl = c_make_fname_decl;
2905 start_fname_decls ();
2908 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2909 decl, NAME is the initialization string and TYPE_DEP indicates whether
2910 NAME depended on the type of the function. As we don't yet implement
2911 delayed emission of static data, we mark the decl as emitted
2912 so it is not placed in the output. Anything using it must therefore pull
2913 out the STRING_CST initializer directly. This does mean that these names
2914 are string merging candidates, which is wrong for C99's __func__. FIXME. */
2916 static tree
2917 c_make_fname_decl (id, type_dep)
2918 tree id;
2919 int type_dep;
2921 const char *name = fname_as_string (type_dep);
2922 tree decl, type, init;
2923 size_t length = strlen (name);
2925 type = build_array_type
2926 (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
2927 build_index_type (size_int (length)));
2929 decl = build_decl (VAR_DECL, id, type);
2930 /* We don't push the decl, so have to set its context here. */
2931 DECL_CONTEXT (decl) = current_function_decl;
2933 TREE_STATIC (decl) = 1;
2934 TREE_READONLY (decl) = 1;
2935 DECL_ARTIFICIAL (decl) = 1;
2937 init = build_string (length + 1, name);
2938 TREE_TYPE (init) = type;
2939 DECL_INITIAL (decl) = init;
2941 TREE_USED (decl) = 1;
2943 finish_decl (decl, init, NULL_TREE);
2945 return decl;
2948 /* Return a definition for a builtin function named NAME and whose data type
2949 is TYPE. TYPE should be a function type with argument types.
2950 FUNCTION_CODE tells later passes how to compile calls to this function.
2951 See tree.h for its possible values.
2953 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2954 the name to be called if we can't opencode the function. If
2955 ATTRS is nonzero, use that for the function's attribute list. */
2957 tree
2958 builtin_function (name, type, function_code, class, library_name, attrs)
2959 const char *name;
2960 tree type;
2961 int function_code;
2962 enum built_in_class class;
2963 const char *library_name;
2964 tree attrs;
2966 tree decl = build_decl (FUNCTION_DECL, get_identifier (name), type);
2967 DECL_EXTERNAL (decl) = 1;
2968 TREE_PUBLIC (decl) = 1;
2969 if (library_name)
2970 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2971 make_decl_rtl (decl, NULL);
2972 pushdecl (decl);
2973 DECL_BUILT_IN_CLASS (decl) = class;
2974 DECL_FUNCTION_CODE (decl) = function_code;
2976 /* The return builtins leave the current function. */
2977 if (function_code == BUILT_IN_RETURN || function_code == BUILT_IN_EH_RETURN)
2978 TREE_THIS_VOLATILE (decl) = 1;
2980 /* Warn if a function in the namespace for users
2981 is used without an occasion to consider it declared. */
2982 if (name[0] != '_' || name[1] != '_')
2983 C_DECL_ANTICIPATED (decl) = 1;
2985 /* Possibly apply some default attributes to this built-in function. */
2986 if (attrs)
2987 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2988 else
2989 decl_attributes (&decl, NULL_TREE, 0);
2991 return decl;
2994 /* Apply default attributes to a function, if a system function with default
2995 attributes. */
2997 void
2998 c_insert_default_attributes (decl)
2999 tree decl;
3001 if (!TREE_PUBLIC (decl))
3002 return;
3003 c_common_insert_default_attributes (decl);
3006 /* Called when a declaration is seen that contains no names to declare.
3007 If its type is a reference to a structure, union or enum inherited
3008 from a containing scope, shadow that tag name for the current scope
3009 with a forward reference.
3010 If its type defines a new named structure or union
3011 or defines an enum, it is valid but we need not do anything here.
3012 Otherwise, it is an error. */
3014 void
3015 shadow_tag (declspecs)
3016 tree declspecs;
3018 shadow_tag_warned (declspecs, 0);
3021 void
3022 shadow_tag_warned (declspecs, warned)
3023 tree declspecs;
3024 int warned;
3025 /* 1 => we have done a pedwarn. 2 => we have done a warning, but
3026 no pedwarn. */
3028 int found_tag = 0;
3029 tree link;
3030 tree specs, attrs;
3032 pending_invalid_xref = 0;
3034 /* Remove the attributes from declspecs, since they will confuse the
3035 following code. */
3036 split_specs_attrs (declspecs, &specs, &attrs);
3038 for (link = specs; link; link = TREE_CHAIN (link))
3040 tree value = TREE_VALUE (link);
3041 enum tree_code code = TREE_CODE (value);
3043 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
3044 /* Used to test also that TYPE_SIZE (value) != 0.
3045 That caused warning for `struct foo;' at top level in the file. */
3047 tree name = lookup_tag_reverse (value);
3048 tree t;
3050 found_tag++;
3052 if (name == 0)
3054 if (warned != 1 && code != ENUMERAL_TYPE)
3055 /* Empty unnamed enum OK */
3057 pedwarn ("unnamed struct/union that defines no instances");
3058 warned = 1;
3061 else
3063 t = lookup_tag (code, name, current_binding_level, 1);
3065 if (t == 0)
3067 t = make_node (code);
3068 pushtag (name, t);
3072 else
3074 if (!warned && ! in_system_header)
3076 warning ("useless keyword or type name in empty declaration");
3077 warned = 2;
3082 if (found_tag > 1)
3083 error ("two types specified in one empty declaration");
3085 if (warned != 1)
3087 if (found_tag == 0)
3088 pedwarn ("empty declaration");
3092 /* Construct an array declarator. EXPR is the expression inside [], or
3093 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
3094 to the pointer to which a parameter array is converted). STATIC_P is
3095 non-zero if "static" is inside the [], zero otherwise. VLA_UNSPEC_P
3096 is non-zero is the array is [*], a VLA of unspecified length which is
3097 nevertheless a complete type (not currently implemented by GCC),
3098 zero otherwise. The declarator is constructed as an ARRAY_REF
3099 (to be decoded by grokdeclarator), whose operand 0 is what's on the
3100 left of the [] (filled by in set_array_declarator_type) and operand 1
3101 is the expression inside; whose TREE_TYPE is the type qualifiers and
3102 which has TREE_STATIC set if "static" is used. */
3104 tree
3105 build_array_declarator (expr, quals, static_p, vla_unspec_p)
3106 tree expr;
3107 tree quals;
3108 int static_p;
3109 int vla_unspec_p;
3111 tree decl;
3112 decl = build_nt (ARRAY_REF, NULL_TREE, expr);
3113 TREE_TYPE (decl) = quals;
3114 TREE_STATIC (decl) = (static_p ? 1 : 0);
3115 if (pedantic && !flag_isoc99)
3117 if (static_p || quals != NULL_TREE)
3118 pedwarn ("ISO C89 does not support `static' or type qualifiers in parameter array declarators");
3119 if (vla_unspec_p)
3120 pedwarn ("ISO C89 does not support `[*]' array declarators");
3122 if (vla_unspec_p)
3123 warning ("GCC does not yet properly implement `[*]' array declarators");
3124 return decl;
3127 /* Set the type of an array declarator. DECL is the declarator, as
3128 constructed by build_array_declarator; TYPE is what appears on the left
3129 of the [] and goes in operand 0. ABSTRACT_P is non-zero if it is an
3130 abstract declarator, zero otherwise; this is used to reject static and
3131 type qualifiers in abstract declarators, where they are not in the
3132 C99 grammar. */
3134 tree
3135 set_array_declarator_type (decl, type, abstract_p)
3136 tree decl;
3137 tree type;
3138 int abstract_p;
3140 TREE_OPERAND (decl, 0) = type;
3141 if (abstract_p && (TREE_TYPE (decl) != NULL_TREE || TREE_STATIC (decl)))
3142 error ("static or type qualifiers in abstract declarator");
3143 return decl;
3146 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
3148 tree
3149 groktypename (typename)
3150 tree typename;
3152 tree specs, attrs;
3154 if (TREE_CODE (typename) != TREE_LIST)
3155 return typename;
3157 split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
3159 typename = grokdeclarator (TREE_VALUE (typename), specs, TYPENAME, 0);
3161 /* Apply attributes. */
3162 decl_attributes (&typename, attrs, 0);
3164 return typename;
3167 /* Return a PARM_DECL node for a given pair of specs and declarator. */
3169 tree
3170 groktypename_in_parm_context (typename)
3171 tree typename;
3173 if (TREE_CODE (typename) != TREE_LIST)
3174 return typename;
3175 return grokdeclarator (TREE_VALUE (typename),
3176 TREE_PURPOSE (typename),
3177 PARM, 0);
3180 /* Decode a declarator in an ordinary declaration or data definition.
3181 This is called as soon as the type information and variable name
3182 have been parsed, before parsing the initializer if any.
3183 Here we create the ..._DECL node, fill in its type,
3184 and put it on the list of decls for the current context.
3185 The ..._DECL node is returned as the value.
3187 Exception: for arrays where the length is not specified,
3188 the type is left null, to be filled in by `finish_decl'.
3190 Function definitions do not come here; they go to start_function
3191 instead. However, external and forward declarations of functions
3192 do go through here. Structure field declarations are done by
3193 grokfield and not through here. */
3195 tree
3196 start_decl (declarator, declspecs, initialized, attributes)
3197 tree declarator, declspecs;
3198 int initialized;
3199 tree attributes;
3201 tree decl;
3202 tree tem;
3204 /* An object declared as __attribute__((deprecated)) suppresses
3205 warnings of uses of other deprecated items. */
3206 if (lookup_attribute ("deprecated", attributes))
3207 deprecated_state = DEPRECATED_SUPPRESS;
3209 decl = grokdeclarator (declarator, declspecs,
3210 NORMAL, initialized);
3212 deprecated_state = DEPRECATED_NORMAL;
3214 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
3215 && MAIN_NAME_P (DECL_NAME (decl)))
3216 warning_with_decl (decl, "`%s' is usually a function");
3218 if (initialized)
3219 /* Is it valid for this decl to have an initializer at all?
3220 If not, set INITIALIZED to zero, which will indirectly
3221 tell `finish_decl' to ignore the initializer once it is parsed. */
3222 switch (TREE_CODE (decl))
3224 case TYPE_DECL:
3225 /* typedef foo = bar means give foo the same type as bar.
3226 We haven't parsed bar yet, so `finish_decl' will fix that up.
3227 Any other case of an initialization in a TYPE_DECL is an error. */
3228 if (pedantic || list_length (declspecs) > 1)
3230 error ("typedef `%s' is initialized",
3231 IDENTIFIER_POINTER (DECL_NAME (decl)));
3232 initialized = 0;
3234 break;
3236 case FUNCTION_DECL:
3237 error ("function `%s' is initialized like a variable",
3238 IDENTIFIER_POINTER (DECL_NAME (decl)));
3239 initialized = 0;
3240 break;
3242 case PARM_DECL:
3243 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
3244 error ("parameter `%s' is initialized",
3245 IDENTIFIER_POINTER (DECL_NAME (decl)));
3246 initialized = 0;
3247 break;
3249 default:
3250 /* Don't allow initializations for incomplete types
3251 except for arrays which might be completed by the initialization. */
3253 /* This can happen if the array size is an undefined macro. We already
3254 gave a warning, so we don't need another one. */
3255 if (TREE_TYPE (decl) == error_mark_node)
3256 initialized = 0;
3257 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
3259 /* A complete type is ok if size is fixed. */
3261 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
3262 || C_DECL_VARIABLE_SIZE (decl))
3264 error ("variable-sized object may not be initialized");
3265 initialized = 0;
3268 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
3270 error ("variable `%s' has initializer but incomplete type",
3271 IDENTIFIER_POINTER (DECL_NAME (decl)));
3272 initialized = 0;
3274 else if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
3276 error ("elements of array `%s' have incomplete type",
3277 IDENTIFIER_POINTER (DECL_NAME (decl)));
3278 initialized = 0;
3282 if (initialized)
3284 #if 0
3285 /* Seems redundant with grokdeclarator. */
3286 if (current_binding_level != global_binding_level
3287 && DECL_EXTERNAL (decl)
3288 && TREE_CODE (decl) != FUNCTION_DECL)
3289 warning ("declaration of `%s' has `extern' and is initialized",
3290 IDENTIFIER_POINTER (DECL_NAME (decl)));
3291 #endif
3292 DECL_EXTERNAL (decl) = 0;
3293 if (current_binding_level == global_binding_level)
3294 TREE_STATIC (decl) = 1;
3296 /* Tell `pushdecl' this is an initialized decl
3297 even though we don't yet have the initializer expression.
3298 Also tell `finish_decl' it may store the real initializer. */
3299 DECL_INITIAL (decl) = error_mark_node;
3302 /* If this is a function declaration, write a record describing it to the
3303 prototypes file (if requested). */
3305 if (TREE_CODE (decl) == FUNCTION_DECL)
3306 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
3308 /* ANSI specifies that a tentative definition which is not merged with
3309 a non-tentative definition behaves exactly like a definition with an
3310 initializer equal to zero. (Section 3.7.2)
3312 -fno-common gives strict ANSI behavior, though this tends to break
3313 a large body of code that grew up without this rule.
3315 Thread-local variables are never common, since there's no entrenched
3316 body of code to break, and it allows more efficient variable references
3317 in the presense of dynamic linking. */
3319 if (TREE_CODE (decl) == VAR_DECL
3320 && !initialized
3321 && TREE_PUBLIC (decl)
3322 && !DECL_THREAD_LOCAL (decl)
3323 && !flag_no_common)
3324 DECL_COMMON (decl) = 1;
3326 /* Set attributes here so if duplicate decl, will have proper attributes. */
3327 decl_attributes (&decl, attributes, 0);
3329 /* If #pragma weak was used, mark the decl weak now. */
3330 if (current_binding_level == global_binding_level)
3331 maybe_apply_pragma_weak (decl);
3333 if (TREE_CODE (decl) == FUNCTION_DECL
3334 && DECL_DECLARED_INLINE_P (decl)
3335 && DECL_UNINLINABLE (decl)
3336 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
3337 warning_with_decl (decl,
3338 "inline function `%s' given attribute noinline");
3340 /* Add this decl to the current binding level.
3341 TEM may equal DECL or it may be a previous decl of the same name. */
3342 tem = pushdecl (decl);
3344 /* For a local variable, define the RTL now. */
3345 if (current_binding_level != global_binding_level
3346 /* But not if this is a duplicate decl
3347 and we preserved the rtl from the previous one
3348 (which may or may not happen). */
3349 && !DECL_RTL_SET_P (tem)
3350 && !DECL_CONTEXT (tem))
3352 if (TREE_TYPE (tem) != error_mark_node
3353 && COMPLETE_TYPE_P (TREE_TYPE (tem)))
3354 expand_decl (tem);
3355 else if (TREE_CODE (TREE_TYPE (tem)) == ARRAY_TYPE
3356 && DECL_INITIAL (tem) != 0)
3357 expand_decl (tem);
3360 return tem;
3363 /* Finish processing of a declaration;
3364 install its initial value.
3365 If the length of an array type is not known before,
3366 it must be determined now, from the initial value, or it is an error. */
3368 void
3369 finish_decl (decl, init, asmspec_tree)
3370 tree decl, init;
3371 tree asmspec_tree;
3373 tree type = TREE_TYPE (decl);
3374 int was_incomplete = (DECL_SIZE (decl) == 0);
3375 const char *asmspec = 0;
3377 /* If a name was specified, get the string. */
3378 if (current_binding_level == global_binding_level)
3379 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
3380 if (asmspec_tree)
3381 asmspec = TREE_STRING_POINTER (asmspec_tree);
3383 /* If `start_decl' didn't like having an initialization, ignore it now. */
3384 if (init != 0 && DECL_INITIAL (decl) == 0)
3385 init = 0;
3387 /* Don't crash if parm is initialized. */
3388 if (TREE_CODE (decl) == PARM_DECL)
3389 init = 0;
3391 if (init)
3393 if (TREE_CODE (decl) != TYPE_DECL)
3394 store_init_value (decl, init);
3395 else
3397 /* typedef foo = bar; store the type of bar as the type of foo. */
3398 TREE_TYPE (decl) = TREE_TYPE (init);
3399 DECL_INITIAL (decl) = init = 0;
3403 /* Deduce size of array from initialization, if not already known */
3404 if (TREE_CODE (type) == ARRAY_TYPE
3405 && TYPE_DOMAIN (type) == 0
3406 && TREE_CODE (decl) != TYPE_DECL)
3408 int do_default
3409 = (TREE_STATIC (decl)
3410 /* Even if pedantic, an external linkage array
3411 may have incomplete type at first. */
3412 ? pedantic && !TREE_PUBLIC (decl)
3413 : !DECL_EXTERNAL (decl));
3414 int failure
3415 = complete_array_type (type, DECL_INITIAL (decl), do_default);
3417 /* Get the completed type made by complete_array_type. */
3418 type = TREE_TYPE (decl);
3420 if (failure == 1)
3421 error_with_decl (decl, "initializer fails to determine size of `%s'");
3423 else if (failure == 2)
3425 if (do_default)
3426 error_with_decl (decl, "array size missing in `%s'");
3427 /* If a `static' var's size isn't known,
3428 make it extern as well as static, so it does not get
3429 allocated.
3430 If it is not `static', then do not mark extern;
3431 finish_incomplete_decl will give it a default size
3432 and it will get allocated. */
3433 else if (!pedantic && TREE_STATIC (decl) && ! TREE_PUBLIC (decl))
3434 DECL_EXTERNAL (decl) = 1;
3437 /* TYPE_MAX_VALUE is always one less than the number of elements
3438 in the array, because we start counting at zero. Therefore,
3439 warn only if the value is less than zero. */
3440 else if (pedantic && TYPE_DOMAIN (type) != 0
3441 && tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < 0)
3442 error_with_decl (decl, "zero or negative size array `%s'");
3444 layout_decl (decl, 0);
3447 if (TREE_CODE (decl) == VAR_DECL)
3449 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
3450 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
3451 layout_decl (decl, 0);
3453 if (DECL_SIZE (decl) == 0
3454 /* Don't give an error if we already gave one earlier. */
3455 && TREE_TYPE (decl) != error_mark_node
3456 && (TREE_STATIC (decl)
3458 /* A static variable with an incomplete type
3459 is an error if it is initialized.
3460 Also if it is not file scope.
3461 Otherwise, let it through, but if it is not `extern'
3462 then it may cause an error message later. */
3463 (DECL_INITIAL (decl) != 0
3464 || DECL_CONTEXT (decl) != 0)
3466 /* An automatic variable with an incomplete type
3467 is an error. */
3468 !DECL_EXTERNAL (decl)))
3470 error_with_decl (decl, "storage size of `%s' isn't known");
3471 TREE_TYPE (decl) = error_mark_node;
3474 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
3475 && DECL_SIZE (decl) != 0)
3477 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
3478 constant_expression_warning (DECL_SIZE (decl));
3479 else
3480 error_with_decl (decl, "storage size of `%s' isn't constant");
3483 if (TREE_USED (type))
3484 TREE_USED (decl) = 1;
3487 /* If this is a function and an assembler name is specified, it isn't
3488 builtin any more. Also reset DECL_RTL so we can give it its new
3489 name. */
3490 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
3492 DECL_BUILT_IN_CLASS (decl) = NOT_BUILT_IN;
3493 SET_DECL_RTL (decl, NULL_RTX);
3494 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
3497 /* Output the assembler code and/or RTL code for variables and functions,
3498 unless the type is an undefined structure or union.
3499 If not, it will get done when the type is completed. */
3501 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3503 /* This is a no-op in c-lang.c or something real in objc-actions.c. */
3504 maybe_objc_check_decl (decl);
3506 if (!DECL_CONTEXT (decl))
3508 if (DECL_INITIAL (decl) == NULL_TREE
3509 || DECL_INITIAL (decl) == error_mark_node)
3510 /* Don't output anything
3511 when a tentative file-scope definition is seen.
3512 But at end of compilation, do output code for them. */
3513 DECL_DEFER_OUTPUT (decl) = 1;
3514 rest_of_decl_compilation (decl, asmspec,
3515 (DECL_CONTEXT (decl) == 0
3516 || TREE_ASM_WRITTEN (decl)), 0);
3518 else
3520 /* This is a local variable. If there is an ASMSPEC, the
3521 user has requested that we handle it specially. */
3522 if (asmspec)
3524 /* In conjunction with an ASMSPEC, the `register'
3525 keyword indicates that we should place the variable
3526 in a particular register. */
3527 if (DECL_REGISTER (decl))
3528 DECL_C_HARD_REGISTER (decl) = 1;
3530 /* If this is not a static variable, issue a warning.
3531 It doesn't make any sense to give an ASMSPEC for an
3532 ordinary, non-register local variable. Historically,
3533 GCC has accepted -- but ignored -- the ASMSPEC in
3534 this case. */
3535 if (TREE_CODE (decl) == VAR_DECL
3536 && !DECL_REGISTER (decl)
3537 && !TREE_STATIC (decl))
3538 warning_with_decl (decl,
3539 "ignoring asm-specifier for non-static local variable `%s'");
3540 else
3541 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
3544 if (TREE_CODE (decl) != FUNCTION_DECL)
3545 add_decl_stmt (decl);
3548 if (DECL_CONTEXT (decl) != 0)
3550 /* Recompute the RTL of a local array now
3551 if it used to be an incomplete type. */
3552 if (was_incomplete
3553 && ! TREE_STATIC (decl) && ! DECL_EXTERNAL (decl))
3555 /* If we used it already as memory, it must stay in memory. */
3556 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3557 /* If it's still incomplete now, no init will save it. */
3558 if (DECL_SIZE (decl) == 0)
3559 DECL_INITIAL (decl) = 0;
3564 if (TREE_CODE (decl) == TYPE_DECL)
3566 /* This is a no-op in c-lang.c or something real in objc-actions.c. */
3567 maybe_objc_check_decl (decl);
3568 rest_of_decl_compilation (decl, NULL, DECL_CONTEXT (decl) == 0, 0);
3571 /* At the end of a declaration, throw away any variable type sizes
3572 of types defined inside that declaration. There is no use
3573 computing them in the following function definition. */
3574 if (current_binding_level == global_binding_level)
3575 get_pending_sizes ();
3578 /* Given a parsed parameter declaration,
3579 decode it into a PARM_DECL and push that on the current binding level.
3580 Also, for the sake of forward parm decls,
3581 record the given order of parms in `parm_order'. */
3583 void
3584 push_parm_decl (parm)
3585 tree parm;
3587 tree decl;
3588 int old_immediate_size_expand = immediate_size_expand;
3589 /* Don't try computing parm sizes now -- wait till fn is called. */
3590 immediate_size_expand = 0;
3592 decl = grokdeclarator (TREE_VALUE (TREE_PURPOSE (parm)),
3593 TREE_PURPOSE (TREE_PURPOSE (parm)), PARM, 0);
3594 decl_attributes (&decl, TREE_VALUE (parm), 0);
3596 #if 0
3597 if (DECL_NAME (decl))
3599 tree olddecl;
3600 olddecl = lookup_name (DECL_NAME (decl));
3601 if (pedantic && olddecl != 0 && TREE_CODE (olddecl) == TYPE_DECL)
3602 pedwarn_with_decl (decl,
3603 "ISO C forbids parameter `%s' shadowing typedef");
3605 #endif
3607 decl = pushdecl (decl);
3609 immediate_size_expand = old_immediate_size_expand;
3611 current_binding_level->parm_order
3612 = tree_cons (NULL_TREE, decl, current_binding_level->parm_order);
3614 /* Add this decl to the current binding level. */
3615 finish_decl (decl, NULL_TREE, NULL_TREE);
3618 /* Clear the given order of parms in `parm_order'.
3619 Used at start of parm list,
3620 and also at semicolon terminating forward decls. */
3622 void
3623 clear_parm_order ()
3625 current_binding_level->parm_order = NULL_TREE;
3628 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3629 literal, which may be an incomplete array type completed by the
3630 initializer; INIT is a CONSTRUCTOR that initializes the compound
3631 literal. */
3633 tree
3634 build_compound_literal (type, init)
3635 tree type;
3636 tree init;
3638 /* We do not use start_decl here because we have a type, not a declarator;
3639 and do not use finish_decl because the decl should be stored inside
3640 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_STMT. */
3641 tree decl = build_decl (VAR_DECL, NULL_TREE, type);
3642 tree complit;
3643 tree stmt;
3644 DECL_EXTERNAL (decl) = 0;
3645 TREE_PUBLIC (decl) = 0;
3646 TREE_STATIC (decl) = (current_binding_level == global_binding_level);
3647 DECL_CONTEXT (decl) = current_function_decl;
3648 TREE_USED (decl) = 1;
3649 TREE_TYPE (decl) = type;
3650 store_init_value (decl, init);
3652 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3654 int failure = complete_array_type (type, DECL_INITIAL (decl), 1);
3655 if (failure)
3656 abort ();
3659 type = TREE_TYPE (decl);
3660 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3661 return error_mark_node;
3663 stmt = build_stmt (DECL_STMT, decl);
3664 complit = build1 (COMPOUND_LITERAL_EXPR, TREE_TYPE (decl), stmt);
3665 TREE_SIDE_EFFECTS (complit) = 1;
3667 layout_decl (decl, 0);
3669 if (TREE_STATIC (decl))
3671 /* This decl needs a name for the assembler output. We also need
3672 a unique suffix to be added to the name, for which DECL_CONTEXT
3673 must be set. */
3674 DECL_NAME (decl) = get_identifier ("__compound_literal");
3675 DECL_CONTEXT (decl) = complit;
3676 rest_of_decl_compilation (decl, NULL, 1, 0);
3677 DECL_CONTEXT (decl) = NULL_TREE;
3680 return complit;
3683 /* Make TYPE a complete type based on INITIAL_VALUE.
3684 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
3685 2 if there was no information (in which case assume 1 if DO_DEFAULT). */
3688 complete_array_type (type, initial_value, do_default)
3689 tree type;
3690 tree initial_value;
3691 int do_default;
3693 tree maxindex = NULL_TREE;
3694 int value = 0;
3696 if (initial_value)
3698 /* Note MAXINDEX is really the maximum index,
3699 one less than the size. */
3700 if (TREE_CODE (initial_value) == STRING_CST)
3702 int eltsize
3703 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
3704 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
3705 / eltsize) - 1, 0);
3707 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
3709 tree elts = CONSTRUCTOR_ELTS (initial_value);
3710 maxindex = build_int_2 (-1, -1);
3711 for (; elts; elts = TREE_CHAIN (elts))
3713 if (TREE_PURPOSE (elts))
3714 maxindex = TREE_PURPOSE (elts);
3715 else
3716 maxindex = fold (build (PLUS_EXPR, integer_type_node,
3717 maxindex, integer_one_node));
3719 maxindex = copy_node (maxindex);
3721 else
3723 /* Make an error message unless that happened already. */
3724 if (initial_value != error_mark_node)
3725 value = 1;
3727 /* Prevent further error messages. */
3728 maxindex = build_int_2 (0, 0);
3732 if (!maxindex)
3734 if (do_default)
3735 maxindex = build_int_2 (0, 0);
3736 value = 2;
3739 if (maxindex)
3741 TYPE_DOMAIN (type) = build_index_type (maxindex);
3742 if (!TREE_TYPE (maxindex))
3743 TREE_TYPE (maxindex) = TYPE_DOMAIN (type);
3746 /* Lay out the type now that we can get the real answer. */
3748 layout_type (type);
3750 return value;
3753 /* Given declspecs and a declarator,
3754 determine the name and type of the object declared
3755 and construct a ..._DECL node for it.
3756 (In one case we can return a ..._TYPE node instead.
3757 For invalid input we sometimes return 0.)
3759 DECLSPECS is a chain of tree_list nodes whose value fields
3760 are the storage classes and type specifiers.
3762 DECL_CONTEXT says which syntactic context this declaration is in:
3763 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3764 FUNCDEF for a function definition. Like NORMAL but a few different
3765 error messages in each case. Return value may be zero meaning
3766 this definition is too screwy to try to parse.
3767 PARM for a parameter declaration (either within a function prototype
3768 or before a function body). Make a PARM_DECL, or return void_type_node.
3769 TYPENAME if for a typename (in a cast or sizeof).
3770 Don't make a DECL node; just return the ..._TYPE node.
3771 FIELD for a struct or union field; make a FIELD_DECL.
3772 BITFIELD for a field with specified width.
3773 INITIALIZED is 1 if the decl has an initializer.
3775 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3776 It may also be so in the PARM case, for a prototype where the
3777 argument type is specified but not the name.
3779 This function is where the complicated C meanings of `static'
3780 and `extern' are interpreted. */
3782 static tree
3783 grokdeclarator (declarator, declspecs, decl_context, initialized)
3784 tree declspecs;
3785 tree declarator;
3786 enum decl_context decl_context;
3787 int initialized;
3789 int specbits = 0;
3790 tree spec;
3791 tree type = NULL_TREE;
3792 int longlong = 0;
3793 int constp;
3794 int restrictp;
3795 int volatilep;
3796 int type_quals = TYPE_UNQUALIFIED;
3797 int inlinep;
3798 int explicit_int = 0;
3799 int explicit_char = 0;
3800 int defaulted_int = 0;
3801 tree typedef_decl = 0;
3802 const char *name;
3803 tree typedef_type = 0;
3804 int funcdef_flag = 0;
3805 enum tree_code innermost_code = ERROR_MARK;
3806 int bitfield = 0;
3807 int size_varies = 0;
3808 tree decl_attr = NULL_TREE;
3809 tree array_ptr_quals = NULL_TREE;
3810 int array_parm_static = 0;
3811 tree returned_attrs = NULL_TREE;
3813 if (decl_context == BITFIELD)
3814 bitfield = 1, decl_context = FIELD;
3816 if (decl_context == FUNCDEF)
3817 funcdef_flag = 1, decl_context = NORMAL;
3819 /* Look inside a declarator for the name being declared
3820 and get it as a string, for an error message. */
3822 tree decl = declarator;
3823 name = 0;
3825 while (decl)
3826 switch (TREE_CODE (decl))
3828 case ARRAY_REF:
3829 case INDIRECT_REF:
3830 case CALL_EXPR:
3831 innermost_code = TREE_CODE (decl);
3832 decl = TREE_OPERAND (decl, 0);
3833 break;
3835 case TREE_LIST:
3836 decl = TREE_VALUE (decl);
3837 break;
3839 case IDENTIFIER_NODE:
3840 name = IDENTIFIER_POINTER (decl);
3841 decl = 0;
3842 break;
3844 default:
3845 abort ();
3847 if (name == 0)
3848 name = "type name";
3851 /* A function definition's declarator must have the form of
3852 a function declarator. */
3854 if (funcdef_flag && innermost_code != CALL_EXPR)
3855 return 0;
3857 /* Anything declared one level down from the top level
3858 must be one of the parameters of a function
3859 (because the body is at least two levels down). */
3861 /* If this looks like a function definition, make it one,
3862 even if it occurs where parms are expected.
3863 Then store_parm_decls will reject it and not use it as a parm. */
3864 if (decl_context == NORMAL && !funcdef_flag
3865 && current_binding_level->parm_flag)
3866 decl_context = PARM;
3868 /* Look through the decl specs and record which ones appear.
3869 Some typespecs are defined as built-in typenames.
3870 Others, the ones that are modifiers of other types,
3871 are represented by bits in SPECBITS: set the bits for
3872 the modifiers that appear. Storage class keywords are also in SPECBITS.
3874 If there is a typedef name or a type, store the type in TYPE.
3875 This includes builtin typedefs such as `int'.
3877 Set EXPLICIT_INT or EXPLICIT_CHAR if the type is `int' or `char'
3878 and did not come from a user typedef.
3880 Set LONGLONG if `long' is mentioned twice. */
3882 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
3884 tree id = TREE_VALUE (spec);
3886 /* If the entire declaration is itself tagged as deprecated then
3887 suppress reports of deprecated items. */
3888 if (id && TREE_DEPRECATED (id))
3890 if (deprecated_state != DEPRECATED_SUPPRESS)
3891 warn_deprecated_use (id);
3894 if (id == ridpointers[(int) RID_INT])
3895 explicit_int = 1;
3896 if (id == ridpointers[(int) RID_CHAR])
3897 explicit_char = 1;
3899 if (TREE_CODE (id) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (id))
3901 enum rid i = C_RID_CODE (id);
3902 if ((int) i <= (int) RID_LAST_MODIFIER)
3904 if (i == RID_LONG && (specbits & (1 << (int) RID_LONG)))
3906 if (longlong)
3907 error ("`long long long' is too long for GCC");
3908 else
3910 if (pedantic && !flag_isoc99 && ! in_system_header
3911 && warn_long_long)
3912 pedwarn ("ISO C89 does not support `long long'");
3913 longlong = 1;
3916 else if (specbits & (1 << (int) i))
3917 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
3919 /* Diagnose "__thread extern". Recall that this list
3920 is in the reverse order seen in the text. */
3921 if (i == RID_THREAD
3922 && (specbits & (1 << (int) RID_EXTERN
3923 | 1 << (int) RID_STATIC)))
3925 if (specbits & 1 << (int) RID_EXTERN)
3926 error ("`__thread' before `extern'");
3927 else
3928 error ("`__thread' before `static'");
3931 specbits |= 1 << (int) i;
3932 goto found;
3935 if (type)
3936 error ("two or more data types in declaration of `%s'", name);
3937 /* Actual typedefs come to us as TYPE_DECL nodes. */
3938 else if (TREE_CODE (id) == TYPE_DECL)
3940 if (TREE_TYPE (id) == error_mark_node)
3941 ; /* Allow the type to default to int to avoid cascading errors. */
3942 else
3944 type = TREE_TYPE (id);
3945 decl_attr = DECL_ATTRIBUTES (id);
3946 typedef_decl = id;
3949 /* Built-in types come as identifiers. */
3950 else if (TREE_CODE (id) == IDENTIFIER_NODE)
3952 tree t = lookup_name (id);
3953 if (TREE_TYPE (t) == error_mark_node)
3955 else if (!t || TREE_CODE (t) != TYPE_DECL)
3956 error ("`%s' fails to be a typedef or built in type",
3957 IDENTIFIER_POINTER (id));
3958 else
3960 type = TREE_TYPE (t);
3961 typedef_decl = t;
3964 else if (TREE_CODE (id) != ERROR_MARK)
3965 type = id;
3967 found:
3971 typedef_type = type;
3972 if (type)
3973 size_varies = C_TYPE_VARIABLE_SIZE (type);
3975 /* No type at all: default to `int', and set DEFAULTED_INT
3976 because it was not a user-defined typedef. */
3978 if (type == 0)
3980 if ((! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3981 | (1 << (int) RID_SIGNED)
3982 | (1 << (int) RID_UNSIGNED)
3983 | (1 << (int) RID_COMPLEX))))
3984 /* Don't warn about typedef foo = bar. */
3985 && ! (specbits & (1 << (int) RID_TYPEDEF) && initialized)
3986 && ! in_system_header)
3988 /* Issue a warning if this is an ISO C 99 program or if -Wreturn-type
3989 and this is a function, or if -Wimplicit; prefer the former
3990 warning since it is more explicit. */
3991 if ((warn_implicit_int || warn_return_type || flag_isoc99)
3992 && funcdef_flag)
3993 warn_about_return_type = 1;
3994 else if (warn_implicit_int || flag_isoc99)
3995 pedwarn_c99 ("type defaults to `int' in declaration of `%s'",
3996 name);
3999 defaulted_int = 1;
4000 type = integer_type_node;
4003 /* Now process the modifiers that were specified
4004 and check for invalid combinations. */
4006 /* Long double is a special combination. */
4008 if ((specbits & 1 << (int) RID_LONG) && ! longlong
4009 && TYPE_MAIN_VARIANT (type) == double_type_node)
4011 specbits &= ~(1 << (int) RID_LONG);
4012 type = long_double_type_node;
4015 /* Check all other uses of type modifiers. */
4017 if (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
4018 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED)))
4020 int ok = 0;
4022 if ((specbits & 1 << (int) RID_LONG)
4023 && (specbits & 1 << (int) RID_SHORT))
4024 error ("both long and short specified for `%s'", name);
4025 else if (((specbits & 1 << (int) RID_LONG)
4026 || (specbits & 1 << (int) RID_SHORT))
4027 && explicit_char)
4028 error ("long or short specified with char for `%s'", name);
4029 else if (((specbits & 1 << (int) RID_LONG)
4030 || (specbits & 1 << (int) RID_SHORT))
4031 && TREE_CODE (type) == REAL_TYPE)
4033 static int already = 0;
4035 error ("long or short specified with floating type for `%s'", name);
4036 if (! already && ! pedantic)
4038 error ("the only valid combination is `long double'");
4039 already = 1;
4042 else if ((specbits & 1 << (int) RID_SIGNED)
4043 && (specbits & 1 << (int) RID_UNSIGNED))
4044 error ("both signed and unsigned specified for `%s'", name);
4045 else if (TREE_CODE (type) != INTEGER_TYPE)
4046 error ("long, short, signed or unsigned invalid for `%s'", name);
4047 else
4049 ok = 1;
4050 if (!explicit_int && !defaulted_int && !explicit_char && pedantic)
4052 pedwarn ("long, short, signed or unsigned used invalidly for `%s'",
4053 name);
4054 if (flag_pedantic_errors)
4055 ok = 0;
4059 /* Discard the type modifiers if they are invalid. */
4060 if (! ok)
4062 specbits &= ~((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
4063 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED));
4064 longlong = 0;
4068 if ((specbits & (1 << (int) RID_COMPLEX))
4069 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
4071 error ("complex invalid for `%s'", name);
4072 specbits &= ~(1 << (int) RID_COMPLEX);
4075 /* Decide whether an integer type is signed or not.
4076 Optionally treat bitfields as signed by default. */
4077 if (specbits & 1 << (int) RID_UNSIGNED
4078 || (bitfield && ! flag_signed_bitfields
4079 && (explicit_int || defaulted_int || explicit_char
4080 /* A typedef for plain `int' without `signed'
4081 can be controlled just like plain `int'. */
4082 || ! (typedef_decl != 0
4083 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
4084 && TREE_CODE (type) != ENUMERAL_TYPE
4085 && !(specbits & 1 << (int) RID_SIGNED)))
4087 if (longlong)
4088 type = long_long_unsigned_type_node;
4089 else if (specbits & 1 << (int) RID_LONG)
4090 type = long_unsigned_type_node;
4091 else if (specbits & 1 << (int) RID_SHORT)
4092 type = short_unsigned_type_node;
4093 else if (type == char_type_node)
4094 type = unsigned_char_type_node;
4095 else if (typedef_decl)
4096 type = c_common_unsigned_type (type);
4097 else
4098 type = unsigned_type_node;
4100 else if ((specbits & 1 << (int) RID_SIGNED)
4101 && type == char_type_node)
4102 type = signed_char_type_node;
4103 else if (longlong)
4104 type = long_long_integer_type_node;
4105 else if (specbits & 1 << (int) RID_LONG)
4106 type = long_integer_type_node;
4107 else if (specbits & 1 << (int) RID_SHORT)
4108 type = short_integer_type_node;
4110 if (specbits & 1 << (int) RID_COMPLEX)
4112 if (pedantic && !flag_isoc99)
4113 pedwarn ("ISO C89 does not support complex types");
4114 /* If we just have "complex", it is equivalent to
4115 "complex double", but if any modifiers at all are specified it is
4116 the complex form of TYPE. E.g, "complex short" is
4117 "complex short int". */
4119 if (defaulted_int && ! longlong
4120 && ! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
4121 | (1 << (int) RID_SIGNED)
4122 | (1 << (int) RID_UNSIGNED))))
4124 if (pedantic)
4125 pedwarn ("ISO C does not support plain `complex' meaning `double complex'");
4126 type = complex_double_type_node;
4128 else if (type == integer_type_node)
4130 if (pedantic)
4131 pedwarn ("ISO C does not support complex integer types");
4132 type = complex_integer_type_node;
4134 else if (type == float_type_node)
4135 type = complex_float_type_node;
4136 else if (type == double_type_node)
4137 type = complex_double_type_node;
4138 else if (type == long_double_type_node)
4139 type = complex_long_double_type_node;
4140 else
4142 if (pedantic)
4143 pedwarn ("ISO C does not support complex integer types");
4144 type = build_complex_type (type);
4148 /* Figure out the type qualifiers for the declaration. There are
4149 two ways a declaration can become qualified. One is something
4150 like `const int i' where the `const' is explicit. Another is
4151 something like `typedef const int CI; CI i' where the type of the
4152 declaration contains the `const'. */
4153 constp = !! (specbits & 1 << (int) RID_CONST) + TYPE_READONLY (type);
4154 restrictp = !! (specbits & 1 << (int) RID_RESTRICT) + TYPE_RESTRICT (type);
4155 volatilep = !! (specbits & 1 << (int) RID_VOLATILE) + TYPE_VOLATILE (type);
4156 inlinep = !! (specbits & (1 << (int) RID_INLINE));
4157 if (constp > 1 && ! flag_isoc99)
4158 pedwarn ("duplicate `const'");
4159 if (restrictp > 1 && ! flag_isoc99)
4160 pedwarn ("duplicate `restrict'");
4161 if (volatilep > 1 && ! flag_isoc99)
4162 pedwarn ("duplicate `volatile'");
4163 if (! flag_gen_aux_info && (TYPE_QUALS (type)))
4164 type = TYPE_MAIN_VARIANT (type);
4165 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4166 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4167 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4169 /* Warn if two storage classes are given. Default to `auto'. */
4172 int nclasses = 0;
4174 if (specbits & 1 << (int) RID_AUTO) nclasses++;
4175 if (specbits & 1 << (int) RID_STATIC) nclasses++;
4176 if (specbits & 1 << (int) RID_EXTERN) nclasses++;
4177 if (specbits & 1 << (int) RID_REGISTER) nclasses++;
4178 if (specbits & 1 << (int) RID_TYPEDEF) nclasses++;
4180 /* "static __thread" and "extern __thread" are allowed. */
4181 if ((specbits & (1 << (int) RID_THREAD
4182 | 1 << (int) RID_STATIC
4183 | 1 << (int) RID_EXTERN)) == (1 << (int) RID_THREAD))
4184 nclasses++;
4186 /* Warn about storage classes that are invalid for certain
4187 kinds of declarations (parameters, typenames, etc.). */
4189 if (nclasses > 1)
4190 error ("multiple storage classes in declaration of `%s'", name);
4191 else if (funcdef_flag
4192 && (specbits
4193 & ((1 << (int) RID_REGISTER)
4194 | (1 << (int) RID_AUTO)
4195 | (1 << (int) RID_TYPEDEF)
4196 | (1 << (int) RID_THREAD))))
4198 if (specbits & 1 << (int) RID_AUTO
4199 && (pedantic || current_binding_level == global_binding_level))
4200 pedwarn ("function definition declared `auto'");
4201 if (specbits & 1 << (int) RID_REGISTER)
4202 error ("function definition declared `register'");
4203 if (specbits & 1 << (int) RID_TYPEDEF)
4204 error ("function definition declared `typedef'");
4205 if (specbits & 1 << (int) RID_THREAD)
4206 error ("function definition declared `__thread'");
4207 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
4208 | (1 << (int) RID_AUTO) | (1 << (int) RID_THREAD));
4210 else if (decl_context != NORMAL && nclasses > 0)
4212 if (decl_context == PARM && specbits & 1 << (int) RID_REGISTER)
4214 else
4216 switch (decl_context)
4218 case FIELD:
4219 error ("storage class specified for structure field `%s'",
4220 name);
4221 break;
4222 case PARM:
4223 error ("storage class specified for parameter `%s'", name);
4224 break;
4225 default:
4226 error ("storage class specified for typename");
4227 break;
4229 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
4230 | (1 << (int) RID_AUTO) | (1 << (int) RID_STATIC)
4231 | (1 << (int) RID_EXTERN) | (1 << (int) RID_THREAD));
4234 else if (specbits & 1 << (int) RID_EXTERN && initialized && ! funcdef_flag)
4236 /* `extern' with initialization is invalid if not at top level. */
4237 if (current_binding_level == global_binding_level)
4238 warning ("`%s' initialized and declared `extern'", name);
4239 else
4240 error ("`%s' has both `extern' and initializer", name);
4242 else if (current_binding_level == global_binding_level)
4244 if (specbits & 1 << (int) RID_AUTO)
4245 error ("top-level declaration of `%s' specifies `auto'", name);
4247 else
4249 if (specbits & 1 << (int) RID_EXTERN && funcdef_flag)
4250 error ("nested function `%s' declared `extern'", name);
4251 else if ((specbits & (1 << (int) RID_THREAD
4252 | 1 << (int) RID_EXTERN
4253 | 1 << (int) RID_STATIC))
4254 == (1 << (int) RID_THREAD))
4256 error ("function-scope `%s' implicitly auto and declared `__thread'",
4257 name);
4258 specbits &= ~(1 << (int) RID_THREAD);
4263 /* Now figure out the structure of the declarator proper.
4264 Descend through it, creating more complex types, until we reach
4265 the declared identifier (or NULL_TREE, in an absolute declarator). */
4267 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE)
4269 if (type == error_mark_node)
4271 declarator = TREE_OPERAND (declarator, 0);
4272 continue;
4275 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
4276 an INDIRECT_REF (for *...),
4277 a CALL_EXPR (for ...(...)),
4278 a TREE_LIST (for nested attributes),
4279 an identifier (for the name being declared)
4280 or a null pointer (for the place in an absolute declarator
4281 where the name was omitted).
4282 For the last two cases, we have just exited the loop.
4284 At this point, TYPE is the type of elements of an array,
4285 or for a function to return, or for a pointer to point to.
4286 After this sequence of ifs, TYPE is the type of the
4287 array or function or pointer, and DECLARATOR has had its
4288 outermost layer removed. */
4290 if (array_ptr_quals != NULL_TREE || array_parm_static)
4292 /* Only the innermost declarator (making a parameter be of
4293 array type which is converted to pointer type)
4294 may have static or type qualifiers. */
4295 error ("static or type qualifiers in non-parameter array declarator");
4296 array_ptr_quals = NULL_TREE;
4297 array_parm_static = 0;
4300 if (TREE_CODE (declarator) == TREE_LIST)
4302 /* We encode a declarator with embedded attributes using
4303 a TREE_LIST. */
4304 tree attrs = TREE_PURPOSE (declarator);
4305 tree inner_decl;
4306 int attr_flags = 0;
4307 declarator = TREE_VALUE (declarator);
4308 inner_decl = declarator;
4309 while (inner_decl != NULL_TREE
4310 && TREE_CODE (inner_decl) == TREE_LIST)
4311 inner_decl = TREE_VALUE (inner_decl);
4312 if (inner_decl == NULL_TREE
4313 || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
4314 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
4315 else if (TREE_CODE (inner_decl) == CALL_EXPR)
4316 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
4317 else if (TREE_CODE (inner_decl) == ARRAY_REF)
4318 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
4319 returned_attrs = decl_attributes (&type,
4320 chainon (returned_attrs, attrs),
4321 attr_flags);
4323 else if (TREE_CODE (declarator) == ARRAY_REF)
4325 tree itype = NULL_TREE;
4326 tree size = TREE_OPERAND (declarator, 1);
4327 /* The index is a signed object `sizetype' bits wide. */
4328 tree index_type = c_common_signed_type (sizetype);
4330 array_ptr_quals = TREE_TYPE (declarator);
4331 array_parm_static = TREE_STATIC (declarator);
4333 declarator = TREE_OPERAND (declarator, 0);
4335 /* Check for some types that there cannot be arrays of. */
4337 if (VOID_TYPE_P (type))
4339 error ("declaration of `%s' as array of voids", name);
4340 type = error_mark_node;
4343 if (TREE_CODE (type) == FUNCTION_TYPE)
4345 error ("declaration of `%s' as array of functions", name);
4346 type = error_mark_node;
4349 if (size == error_mark_node)
4350 type = error_mark_node;
4352 if (type == error_mark_node)
4353 continue;
4355 /* If size was specified, set ITYPE to a range-type for that size.
4356 Otherwise, ITYPE remains null. finish_decl may figure it out
4357 from an initial value. */
4359 if (size)
4361 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4362 STRIP_TYPE_NOPS (size);
4364 if (! INTEGRAL_TYPE_P (TREE_TYPE (size)))
4366 error ("size of array `%s' has non-integer type", name);
4367 size = integer_one_node;
4370 if (pedantic && integer_zerop (size))
4371 pedwarn ("ISO C forbids zero-size array `%s'", name);
4373 if (TREE_CODE (size) == INTEGER_CST)
4375 constant_expression_warning (size);
4376 if (tree_int_cst_sgn (size) < 0)
4378 error ("size of array `%s' is negative", name);
4379 size = integer_one_node;
4382 else
4384 /* Make sure the array size remains visibly nonconstant
4385 even if it is (eg) a const variable with known value. */
4386 size_varies = 1;
4388 if (!flag_isoc99 && pedantic)
4390 if (TREE_CONSTANT (size))
4391 pedwarn ("ISO C89 forbids array `%s' whose size can't be evaluated",
4392 name);
4393 else
4394 pedwarn ("ISO C89 forbids variable-size array `%s'",
4395 name);
4399 if (integer_zerop (size))
4401 /* A zero-length array cannot be represented with an
4402 unsigned index type, which is what we'll get with
4403 build_index_type. Create an open-ended range instead. */
4404 itype = build_range_type (sizetype, size, NULL_TREE);
4406 else
4408 /* Compute the maximum valid index, that is, size - 1.
4409 Do the calculation in index_type, so that if it is
4410 a variable the computations will be done in the
4411 proper mode. */
4412 itype = fold (build (MINUS_EXPR, index_type,
4413 convert (index_type, size),
4414 convert (index_type, size_one_node)));
4416 /* If that overflowed, the array is too big.
4417 ??? While a size of INT_MAX+1 technically shouldn't
4418 cause an overflow (because we subtract 1), the overflow
4419 is recorded during the conversion to index_type, before
4420 the subtraction. Handling this case seems like an
4421 unnecessary complication. */
4422 if (TREE_OVERFLOW (itype))
4424 error ("size of array `%s' is too large", name);
4425 type = error_mark_node;
4426 continue;
4429 if (size_varies)
4430 itype = variable_size (itype);
4431 itype = build_index_type (itype);
4434 else if (decl_context == FIELD)
4436 if (pedantic && !flag_isoc99 && !in_system_header)
4437 pedwarn ("ISO C89 does not support flexible array members");
4439 /* ISO C99 Flexible array members are effectively identical
4440 to GCC's zero-length array extension. */
4441 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4444 /* If pedantic, complain about arrays of incomplete types. */
4446 if (pedantic && !COMPLETE_TYPE_P (type))
4447 pedwarn ("array type has incomplete element type");
4449 #if 0
4450 /* We shouldn't have a function type here at all!
4451 Functions aren't allowed as array elements. */
4452 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4453 && (constp || volatilep))
4454 pedwarn ("ISO C forbids const or volatile function types");
4455 #endif
4457 /* Build the array type itself, then merge any constancy or
4458 volatility into the target type. We must do it in this order
4459 to ensure that the TYPE_MAIN_VARIANT field of the array type
4460 is set correctly. */
4462 type = build_array_type (type, itype);
4463 if (type_quals)
4464 type = c_build_qualified_type (type, type_quals);
4466 if (size_varies)
4467 C_TYPE_VARIABLE_SIZE (type) = 1;
4469 /* The GCC extension for zero-length arrays differs from
4470 ISO flexible array members in that sizeof yields zero. */
4471 if (size && integer_zerop (size))
4473 layout_type (type);
4474 TYPE_SIZE (type) = bitsize_zero_node;
4475 TYPE_SIZE_UNIT (type) = size_zero_node;
4477 if (decl_context != PARM
4478 && (array_ptr_quals != NULL_TREE || array_parm_static))
4480 error ("static or type qualifiers in non-parameter array declarator");
4481 array_ptr_quals = NULL_TREE;
4482 array_parm_static = 0;
4485 else if (TREE_CODE (declarator) == CALL_EXPR)
4487 tree arg_types;
4489 /* Declaring a function type.
4490 Make sure we have a valid type for the function to return. */
4491 if (type == error_mark_node)
4492 continue;
4494 size_varies = 0;
4496 /* Warn about some types functions can't return. */
4498 if (TREE_CODE (type) == FUNCTION_TYPE)
4500 error ("`%s' declared as function returning a function", name);
4501 type = integer_type_node;
4503 if (TREE_CODE (type) == ARRAY_TYPE)
4505 error ("`%s' declared as function returning an array", name);
4506 type = integer_type_node;
4509 /* Construct the function type and go to the next
4510 inner layer of declarator. */
4512 arg_types = grokparms (TREE_OPERAND (declarator, 1),
4513 funcdef_flag
4514 /* Say it's a definition
4515 only for the CALL_EXPR
4516 closest to the identifier. */
4517 && TREE_CODE (TREE_OPERAND (declarator, 0)) == IDENTIFIER_NODE);
4518 /* Type qualifiers before the return type of the function
4519 qualify the return type, not the function type. */
4520 if (type_quals)
4522 /* Type qualifiers on a function return type are normally
4523 permitted by the standard but have no effect, so give a
4524 warning at -W. Qualifiers on a void return type have
4525 meaning as a GNU extension, and are banned on function
4526 definitions in ISO C. FIXME: strictly we shouldn't
4527 pedwarn for qualified void return types except on function
4528 definitions, but not doing so could lead to the undesirable
4529 state of a "volatile void" function return type not being
4530 warned about, and a use of the function being compiled
4531 with GNU semantics, with no diagnostics under -pedantic. */
4532 if (VOID_TYPE_P (type) && pedantic && !in_system_header)
4533 pedwarn ("ISO C forbids qualified void function return type");
4534 else if (extra_warnings
4535 && !(VOID_TYPE_P (type)
4536 && type_quals == TYPE_QUAL_VOLATILE))
4537 warning ("type qualifiers ignored on function return type");
4539 type = c_build_qualified_type (type, type_quals);
4541 type_quals = TYPE_UNQUALIFIED;
4543 type = build_function_type (type, arg_types);
4544 declarator = TREE_OPERAND (declarator, 0);
4546 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4547 the formal parameter list of this FUNCTION_TYPE to point to
4548 the FUNCTION_TYPE node itself. */
4551 tree link;
4553 for (link = last_function_parm_tags;
4554 link;
4555 link = TREE_CHAIN (link))
4556 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4559 else if (TREE_CODE (declarator) == INDIRECT_REF)
4561 /* Merge any constancy or volatility into the target type
4562 for the pointer. */
4564 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4565 && type_quals)
4566 pedwarn ("ISO C forbids qualified function types");
4567 if (type_quals)
4568 type = c_build_qualified_type (type, type_quals);
4569 type_quals = TYPE_UNQUALIFIED;
4570 size_varies = 0;
4572 type = build_pointer_type (type);
4574 /* Process a list of type modifier keywords
4575 (such as const or volatile) that were given inside the `*'. */
4577 if (TREE_TYPE (declarator))
4579 tree typemodlist;
4580 int erred = 0;
4582 constp = 0;
4583 volatilep = 0;
4584 restrictp = 0;
4585 for (typemodlist = TREE_TYPE (declarator); typemodlist;
4586 typemodlist = TREE_CHAIN (typemodlist))
4588 tree qualifier = TREE_VALUE (typemodlist);
4590 if (C_IS_RESERVED_WORD (qualifier))
4592 if (C_RID_CODE (qualifier) == RID_CONST)
4593 constp++;
4594 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4595 volatilep++;
4596 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4597 restrictp++;
4598 else
4599 erred++;
4601 else
4602 erred++;
4605 if (erred)
4606 error ("invalid type modifier within pointer declarator");
4607 if (constp > 1 && ! flag_isoc99)
4608 pedwarn ("duplicate `const'");
4609 if (volatilep > 1 && ! flag_isoc99)
4610 pedwarn ("duplicate `volatile'");
4611 if (restrictp > 1 && ! flag_isoc99)
4612 pedwarn ("duplicate `restrict'");
4614 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4615 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4616 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4619 declarator = TREE_OPERAND (declarator, 0);
4621 else
4622 abort ();
4626 /* Now TYPE has the actual type. */
4628 /* Did array size calculations overflow? */
4630 if (TREE_CODE (type) == ARRAY_TYPE
4631 && COMPLETE_TYPE_P (type)
4632 && TREE_OVERFLOW (TYPE_SIZE (type)))
4634 error ("size of array `%s' is too large", name);
4635 /* If we proceed with the array type as it is, we'll eventually
4636 crash in tree_low_cst(). */
4637 type = error_mark_node;
4640 /* If this is declaring a typedef name, return a TYPE_DECL. */
4642 if (specbits & (1 << (int) RID_TYPEDEF))
4644 tree decl;
4645 /* Note that the grammar rejects storage classes
4646 in typenames, fields or parameters */
4647 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4648 && type_quals)
4649 pedwarn ("ISO C forbids qualified function types");
4650 if (type_quals)
4651 type = c_build_qualified_type (type, type_quals);
4652 decl = build_decl (TYPE_DECL, declarator, type);
4653 if ((specbits & (1 << (int) RID_SIGNED))
4654 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
4655 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4656 decl_attributes (&decl, returned_attrs, 0);
4657 return decl;
4660 /* Detect the case of an array type of unspecified size
4661 which came, as such, direct from a typedef name.
4662 We must copy the type, so that each identifier gets
4663 a distinct type, so that each identifier's size can be
4664 controlled separately by its own initializer. */
4666 if (type != 0 && typedef_type != 0
4667 && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0
4668 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
4670 type = build_array_type (TREE_TYPE (type), 0);
4671 if (size_varies)
4672 C_TYPE_VARIABLE_SIZE (type) = 1;
4675 /* If this is a type name (such as, in a cast or sizeof),
4676 compute the type and return it now. */
4678 if (decl_context == TYPENAME)
4680 /* Note that the grammar rejects storage classes
4681 in typenames, fields or parameters */
4682 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4683 && type_quals)
4684 pedwarn ("ISO C forbids const or volatile function types");
4685 if (type_quals)
4686 type = c_build_qualified_type (type, type_quals);
4687 decl_attributes (&type, returned_attrs, 0);
4688 return type;
4691 /* Aside from typedefs and type names (handle above),
4692 `void' at top level (not within pointer)
4693 is allowed only in public variables.
4694 We don't complain about parms either, but that is because
4695 a better error message can be made later. */
4697 if (VOID_TYPE_P (type) && decl_context != PARM
4698 && ! ((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4699 && ((specbits & (1 << (int) RID_EXTERN))
4700 || (current_binding_level == global_binding_level
4701 && !(specbits
4702 & ((1 << (int) RID_STATIC) | (1 << (int) RID_REGISTER)))))))
4704 error ("variable or field `%s' declared void", name);
4705 type = integer_type_node;
4708 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4709 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4712 tree decl;
4714 if (decl_context == PARM)
4716 tree type_as_written;
4717 tree promoted_type;
4719 /* A parameter declared as an array of T is really a pointer to T.
4720 One declared as a function is really a pointer to a function. */
4722 if (TREE_CODE (type) == ARRAY_TYPE)
4724 /* Transfer const-ness of array into that of type pointed to. */
4725 type = TREE_TYPE (type);
4726 if (type_quals)
4727 type = c_build_qualified_type (type, type_quals);
4728 type = build_pointer_type (type);
4729 type_quals = TYPE_UNQUALIFIED;
4730 if (array_ptr_quals)
4732 tree new_ptr_quals, new_ptr_attrs;
4733 int erred = 0;
4734 split_specs_attrs (array_ptr_quals, &new_ptr_quals, &new_ptr_attrs);
4735 /* We don't yet implement attributes in this context. */
4736 if (new_ptr_attrs != NULL_TREE)
4737 warning ("attributes in parameter array declarator ignored");
4739 constp = 0;
4740 volatilep = 0;
4741 restrictp = 0;
4742 for (; new_ptr_quals; new_ptr_quals = TREE_CHAIN (new_ptr_quals))
4744 tree qualifier = TREE_VALUE (new_ptr_quals);
4746 if (C_IS_RESERVED_WORD (qualifier))
4748 if (C_RID_CODE (qualifier) == RID_CONST)
4749 constp++;
4750 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4751 volatilep++;
4752 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4753 restrictp++;
4754 else
4755 erred++;
4757 else
4758 erred++;
4761 if (erred)
4762 error ("invalid type modifier within array declarator");
4764 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4765 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4766 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4768 size_varies = 0;
4770 else if (TREE_CODE (type) == FUNCTION_TYPE)
4772 if (pedantic && type_quals)
4773 pedwarn ("ISO C forbids qualified function types");
4774 if (type_quals)
4775 type = c_build_qualified_type (type, type_quals);
4776 type = build_pointer_type (type);
4777 type_quals = TYPE_UNQUALIFIED;
4779 else if (type_quals)
4780 type = c_build_qualified_type (type, type_quals);
4782 type_as_written = type;
4784 decl = build_decl (PARM_DECL, declarator, type);
4785 if (size_varies)
4786 C_DECL_VARIABLE_SIZE (decl) = 1;
4788 /* Compute the type actually passed in the parmlist,
4789 for the case where there is no prototype.
4790 (For example, shorts and chars are passed as ints.)
4791 When there is a prototype, this is overridden later. */
4793 if (type == error_mark_node)
4794 promoted_type = type;
4795 else
4796 promoted_type = c_type_promotes_to (type);
4798 DECL_ARG_TYPE (decl) = promoted_type;
4799 DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
4801 else if (decl_context == FIELD)
4803 /* Structure field. It may not be a function. */
4805 if (TREE_CODE (type) == FUNCTION_TYPE)
4807 error ("field `%s' declared as a function", name);
4808 type = build_pointer_type (type);
4810 else if (TREE_CODE (type) != ERROR_MARK
4811 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4813 error ("field `%s' has incomplete type", name);
4814 type = error_mark_node;
4816 /* Move type qualifiers down to element of an array. */
4817 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4819 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4820 type_quals),
4821 TYPE_DOMAIN (type));
4822 #if 0
4823 /* Leave the field const or volatile as well. */
4824 type_quals = TYPE_UNQUALIFIED;
4825 #endif
4827 decl = build_decl (FIELD_DECL, declarator, type);
4828 DECL_NONADDRESSABLE_P (decl) = bitfield;
4830 if (size_varies)
4831 C_DECL_VARIABLE_SIZE (decl) = 1;
4833 else if (TREE_CODE (type) == FUNCTION_TYPE)
4835 /* Every function declaration is "external"
4836 except for those which are inside a function body
4837 in which `auto' is used.
4838 That is a case not specified by ANSI C,
4839 and we use it for forward declarations for nested functions. */
4840 int extern_ref = (!(specbits & (1 << (int) RID_AUTO))
4841 || current_binding_level == global_binding_level);
4843 if (specbits & (1 << (int) RID_AUTO)
4844 && (pedantic || current_binding_level == global_binding_level))
4845 pedwarn ("invalid storage class for function `%s'", name);
4846 if (specbits & (1 << (int) RID_REGISTER))
4847 error ("invalid storage class for function `%s'", name);
4848 if (specbits & (1 << (int) RID_THREAD))
4849 error ("invalid storage class for function `%s'", name);
4850 /* Function declaration not at top level.
4851 Storage classes other than `extern' are not allowed
4852 and `extern' makes no difference. */
4853 if (current_binding_level != global_binding_level
4854 && (specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_INLINE)))
4855 && pedantic)
4856 pedwarn ("invalid storage class for function `%s'", name);
4858 decl = build_decl (FUNCTION_DECL, declarator, type);
4859 decl = build_decl_attribute_variant (decl, decl_attr);
4861 DECL_LANG_SPECIFIC (decl) = (struct lang_decl *)
4862 ggc_alloc_cleared (sizeof (struct lang_decl));
4864 if (pedantic && type_quals && ! DECL_IN_SYSTEM_HEADER (decl))
4865 pedwarn ("ISO C forbids qualified function types");
4867 /* GNU C interprets a `volatile void' return type to indicate
4868 that the function does not return. */
4869 if ((type_quals & TYPE_QUAL_VOLATILE)
4870 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4871 warning ("`noreturn' function returns non-void value");
4873 if (extern_ref)
4874 DECL_EXTERNAL (decl) = 1;
4875 /* Record absence of global scope for `static' or `auto'. */
4876 TREE_PUBLIC (decl)
4877 = !(specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_AUTO)));
4879 if (defaulted_int)
4880 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4882 /* Record presence of `inline', if it is reasonable. */
4883 if (MAIN_NAME_P (declarator))
4885 if (inlinep)
4886 warning ("cannot inline function `main'");
4888 else if (inlinep)
4890 /* Assume that otherwise the function can be inlined. */
4891 DECL_DECLARED_INLINE_P (decl) = 1;
4893 /* Do not mark bare declarations as DECL_INLINE. Doing so
4894 in the presence of multiple declarations can result in
4895 the abstract origin pointing between the declarations,
4896 which will confuse dwarf2out. */
4897 if (initialized)
4899 DECL_INLINE (decl) = 1;
4900 if (specbits & (1 << (int) RID_EXTERN))
4901 current_extern_inline = 1;
4904 /* If -finline-functions, assume it can be inlined. This does
4905 two things: let the function be deferred until it is actually
4906 needed, and let dwarf2 know that the function is inlinable. */
4907 else if (flag_inline_trees == 2 && initialized)
4909 DECL_INLINE (decl) = 1;
4910 DECL_DECLARED_INLINE_P (decl) = 0;
4913 else
4915 /* It's a variable. */
4916 /* An uninitialized decl with `extern' is a reference. */
4917 int extern_ref = !initialized && (specbits & (1 << (int) RID_EXTERN));
4919 /* Move type qualifiers down to element of an array. */
4920 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4922 int saved_align = TYPE_ALIGN(type);
4923 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4924 type_quals),
4925 TYPE_DOMAIN (type));
4926 TYPE_ALIGN (type) = saved_align;
4927 #if 0 /* Leave the variable const or volatile as well. */
4928 type_quals = TYPE_UNQUALIFIED;
4929 #endif
4931 else if (type_quals)
4932 type = c_build_qualified_type (type, type_quals);
4934 decl = build_decl (VAR_DECL, declarator, type);
4935 if (size_varies)
4936 C_DECL_VARIABLE_SIZE (decl) = 1;
4938 if (inlinep)
4939 pedwarn_with_decl (decl, "variable `%s' declared `inline'");
4941 DECL_EXTERNAL (decl) = extern_ref;
4943 /* At top level, the presence of a `static' or `register' storage
4944 class specifier, or the absence of all storage class specifiers
4945 makes this declaration a definition (perhaps tentative). Also,
4946 the absence of both `static' and `register' makes it public. */
4947 if (current_binding_level == global_binding_level)
4949 TREE_PUBLIC (decl) = !(specbits & ((1 << (int) RID_STATIC)
4950 | (1 << (int) RID_REGISTER)));
4951 TREE_STATIC (decl) = !extern_ref;
4953 /* Not at top level, only `static' makes a static definition. */
4954 else
4956 TREE_STATIC (decl) = (specbits & (1 << (int) RID_STATIC)) != 0;
4957 TREE_PUBLIC (decl) = extern_ref;
4960 if (specbits & 1 << (int) RID_THREAD)
4962 if (targetm.have_tls)
4963 DECL_THREAD_LOCAL (decl) = 1;
4964 else
4965 /* A mere warning is sure to result in improper semantics
4966 at runtime. Don't bother to allow this to compile. */
4967 error ("thread-local storage not supported for this target");
4971 /* Record `register' declaration for warnings on &
4972 and in case doing stupid register allocation. */
4974 if (specbits & (1 << (int) RID_REGISTER))
4975 DECL_REGISTER (decl) = 1;
4977 /* Record constancy and volatility. */
4978 c_apply_type_quals_to_decl (type_quals, decl);
4980 /* If a type has volatile components, it should be stored in memory.
4981 Otherwise, the fact that those components are volatile
4982 will be ignored, and would even crash the compiler. */
4983 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
4984 c_mark_addressable (decl);
4986 decl_attributes (&decl, returned_attrs, 0);
4988 return decl;
4992 /* Decode the parameter-list info for a function type or function definition.
4993 The argument is the value returned by `get_parm_info' (or made in parse.y
4994 if there is an identifier list instead of a parameter decl list).
4995 These two functions are separate because when a function returns
4996 or receives functions then each is called multiple times but the order
4997 of calls is different. The last call to `grokparms' is always the one
4998 that contains the formal parameter names of a function definition.
5000 Store in `last_function_parms' a chain of the decls of parms.
5001 Also store in `last_function_parm_tags' a chain of the struct, union,
5002 and enum tags declared among the parms.
5004 Return a list of arg types to use in the FUNCTION_TYPE for this function.
5006 FUNCDEF_FLAG is nonzero for a function definition, 0 for
5007 a mere declaration. A nonempty identifier-list gets an error message
5008 when FUNCDEF_FLAG is zero. */
5010 static tree
5011 grokparms (parms_info, funcdef_flag)
5012 tree parms_info;
5013 int funcdef_flag;
5015 tree first_parm = TREE_CHAIN (parms_info);
5017 last_function_parms = TREE_PURPOSE (parms_info);
5018 last_function_parm_tags = TREE_VALUE (parms_info);
5020 if (warn_strict_prototypes && first_parm == 0 && !funcdef_flag
5021 && !in_system_header)
5022 warning ("function declaration isn't a prototype");
5024 if (first_parm != 0
5025 && TREE_CODE (TREE_VALUE (first_parm)) == IDENTIFIER_NODE)
5027 if (! funcdef_flag)
5028 pedwarn ("parameter names (without types) in function declaration");
5030 last_function_parms = first_parm;
5031 return 0;
5033 else
5035 tree parm;
5036 tree typelt;
5037 /* We no longer test FUNCDEF_FLAG.
5038 If the arg types are incomplete in a declaration,
5039 they must include undefined tags.
5040 These tags can never be defined in the scope of the declaration,
5041 so the types can never be completed,
5042 and no call can be compiled successfully. */
5043 #if 0
5044 /* In a fcn definition, arg types must be complete. */
5045 if (funcdef_flag)
5046 #endif
5047 for (parm = last_function_parms, typelt = first_parm;
5048 parm;
5049 parm = TREE_CHAIN (parm))
5050 /* Skip over any enumeration constants declared here. */
5051 if (TREE_CODE (parm) == PARM_DECL)
5053 /* Barf if the parameter itself has an incomplete type. */
5054 tree type = TREE_VALUE (typelt);
5055 if (type == error_mark_node)
5056 continue;
5057 if (!COMPLETE_TYPE_P (type))
5059 if (funcdef_flag && DECL_NAME (parm) != 0)
5060 error ("parameter `%s' has incomplete type",
5061 IDENTIFIER_POINTER (DECL_NAME (parm)));
5062 else
5063 warning ("parameter has incomplete type");
5064 if (funcdef_flag)
5066 TREE_VALUE (typelt) = error_mark_node;
5067 TREE_TYPE (parm) = error_mark_node;
5070 #if 0
5071 /* This has been replaced by parm_tags_warning, which
5072 uses a more accurate criterion for what to warn
5073 about. */
5074 else
5076 /* Now warn if is a pointer to an incomplete type. */
5077 while (TREE_CODE (type) == POINTER_TYPE
5078 || TREE_CODE (type) == REFERENCE_TYPE)
5079 type = TREE_TYPE (type);
5080 type = TYPE_MAIN_VARIANT (type);
5081 if (!COMPLETE_TYPE_P (type))
5083 if (DECL_NAME (parm) != 0)
5084 warning ("parameter `%s' points to incomplete type",
5085 IDENTIFIER_POINTER (DECL_NAME (parm)));
5086 else
5087 warning ("parameter points to incomplete type");
5090 #endif
5091 typelt = TREE_CHAIN (typelt);
5094 return first_parm;
5098 /* Return a tree_list node with info on a parameter list just parsed.
5099 The TREE_PURPOSE is a chain of decls of those parms.
5100 The TREE_VALUE is a list of structure, union and enum tags defined.
5101 The TREE_CHAIN is a list of argument types to go in the FUNCTION_TYPE.
5102 This tree_list node is later fed to `grokparms'.
5104 VOID_AT_END nonzero means append `void' to the end of the type-list.
5105 Zero means the parmlist ended with an ellipsis so don't append `void'. */
5107 tree
5108 get_parm_info (void_at_end)
5109 int void_at_end;
5111 tree decl, t;
5112 tree types = 0;
5113 int erred = 0;
5114 tree tags = gettags ();
5115 tree parms = getdecls ();
5116 tree new_parms = 0;
5117 tree order = current_binding_level->parm_order;
5119 /* Just `void' (and no ellipsis) is special. There are really no parms.
5120 But if the `void' is qualified (by `const' or `volatile') or has a
5121 storage class specifier (`register'), then the behavior is undefined;
5122 by not counting it as the special case of `void' we will cause an
5123 error later. Typedefs for `void' are OK (see DR#157). */
5124 if (void_at_end && parms != 0
5125 && TREE_CHAIN (parms) == 0
5126 && VOID_TYPE_P (TREE_TYPE (parms))
5127 && ! TREE_THIS_VOLATILE (parms)
5128 && ! TREE_READONLY (parms)
5129 && ! DECL_REGISTER (parms)
5130 && DECL_NAME (parms) == 0)
5132 parms = NULL_TREE;
5133 storedecls (NULL_TREE);
5134 return tree_cons (NULL_TREE, NULL_TREE,
5135 tree_cons (NULL_TREE, void_type_node, NULL_TREE));
5138 /* Extract enumerator values and other non-parms declared with the parms.
5139 Likewise any forward parm decls that didn't have real parm decls. */
5140 for (decl = parms; decl;)
5142 tree next = TREE_CHAIN (decl);
5144 if (TREE_CODE (decl) != PARM_DECL)
5146 TREE_CHAIN (decl) = new_parms;
5147 new_parms = decl;
5149 else if (TREE_ASM_WRITTEN (decl))
5151 error_with_decl (decl,
5152 "parameter `%s' has just a forward declaration");
5153 TREE_CHAIN (decl) = new_parms;
5154 new_parms = decl;
5156 decl = next;
5159 /* Put the parm decls back in the order they were in in the parm list. */
5160 for (t = order; t; t = TREE_CHAIN (t))
5162 if (TREE_CHAIN (t))
5163 TREE_CHAIN (TREE_VALUE (t)) = TREE_VALUE (TREE_CHAIN (t));
5164 else
5165 TREE_CHAIN (TREE_VALUE (t)) = 0;
5168 new_parms = chainon (order ? nreverse (TREE_VALUE (order)) : 0,
5169 new_parms);
5171 /* Store the parmlist in the binding level since the old one
5172 is no longer a valid list. (We have changed the chain pointers.) */
5173 storedecls (new_parms);
5175 for (decl = new_parms; decl; decl = TREE_CHAIN (decl))
5176 /* There may also be declarations for enumerators if an enumeration
5177 type is declared among the parms. Ignore them here. */
5178 if (TREE_CODE (decl) == PARM_DECL)
5180 /* Since there is a prototype,
5181 args are passed in their declared types. */
5182 tree type = TREE_TYPE (decl);
5183 DECL_ARG_TYPE (decl) = type;
5184 if (PROMOTE_PROTOTYPES
5185 && INTEGRAL_TYPE_P (type)
5186 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
5187 DECL_ARG_TYPE (decl) = integer_type_node;
5189 types = tree_cons (NULL_TREE, TREE_TYPE (decl), types);
5190 if (VOID_TYPE_P (TREE_VALUE (types)) && ! erred
5191 && DECL_NAME (decl) == 0)
5193 error ("`void' in parameter list must be the entire list");
5194 erred = 1;
5198 if (void_at_end)
5199 return tree_cons (new_parms, tags,
5200 nreverse (tree_cons (NULL_TREE, void_type_node, types)));
5202 return tree_cons (new_parms, tags, nreverse (types));
5205 /* At end of parameter list, warn about any struct, union or enum tags
5206 defined within. Do so because these types cannot ever become complete. */
5208 void
5209 parmlist_tags_warning ()
5211 tree elt;
5212 static int already;
5214 for (elt = current_binding_level->tags; elt; elt = TREE_CHAIN (elt))
5216 enum tree_code code = TREE_CODE (TREE_VALUE (elt));
5217 /* An anonymous union parm type is meaningful as a GNU extension.
5218 So don't warn for that. */
5219 if (code == UNION_TYPE && TREE_PURPOSE (elt) == 0 && !pedantic)
5220 continue;
5221 if (TREE_PURPOSE (elt) != 0)
5223 if (code == RECORD_TYPE)
5224 warning ("`struct %s' declared inside parameter list",
5225 IDENTIFIER_POINTER (TREE_PURPOSE (elt)));
5226 else if (code == UNION_TYPE)
5227 warning ("`union %s' declared inside parameter list",
5228 IDENTIFIER_POINTER (TREE_PURPOSE (elt)));
5229 else
5230 warning ("`enum %s' declared inside parameter list",
5231 IDENTIFIER_POINTER (TREE_PURPOSE (elt)));
5233 else
5235 /* For translation these need to be separate warnings */
5236 if (code == RECORD_TYPE)
5237 warning ("anonymous struct declared inside parameter list");
5238 else if (code == UNION_TYPE)
5239 warning ("anonymous union declared inside parameter list");
5240 else
5241 warning ("anonymous enum declared inside parameter list");
5243 if (! already)
5245 warning ("its scope is only this definition or declaration, which is probably not what you want");
5246 already = 1;
5251 /* Get the struct, enum or union (CODE says which) with tag NAME.
5252 Define the tag as a forward-reference if it is not defined. */
5254 tree
5255 xref_tag (code, name)
5256 enum tree_code code;
5257 tree name;
5259 /* If a cross reference is requested, look up the type
5260 already defined for this tag and return it. */
5262 tree ref = lookup_tag (code, name, current_binding_level, 0);
5263 /* If this is the right type of tag, return what we found.
5264 (This reference will be shadowed by shadow_tag later if appropriate.)
5265 If this is the wrong type of tag, do not return it. If it was the
5266 wrong type in the same binding level, we will have had an error
5267 message already; if in a different binding level and declaring
5268 a name, pending_xref_error will give an error message; but if in a
5269 different binding level and not declaring a name, this tag should
5270 shadow the previous declaration of a different type of tag, and
5271 this would not work properly if we return the reference found.
5272 (For example, with "struct foo" in an outer scope, "union foo;"
5273 must shadow that tag with a new one of union type.) */
5274 if (ref && TREE_CODE (ref) == code)
5275 return ref;
5277 /* If no such tag is yet defined, create a forward-reference node
5278 and record it as the "definition".
5279 When a real declaration of this type is found,
5280 the forward-reference will be altered into a real type. */
5282 ref = make_node (code);
5283 if (code == ENUMERAL_TYPE)
5285 /* Give the type a default layout like unsigned int
5286 to avoid crashing if it does not get defined. */
5287 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
5288 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
5289 TYPE_USER_ALIGN (ref) = 0;
5290 TREE_UNSIGNED (ref) = 1;
5291 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
5292 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
5293 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
5296 pushtag (name, ref);
5298 return ref;
5301 /* Make sure that the tag NAME is defined *in the current binding level*
5302 at least as a forward reference.
5303 CODE says which kind of tag NAME ought to be. */
5305 tree
5306 start_struct (code, name)
5307 enum tree_code code;
5308 tree name;
5310 /* If there is already a tag defined at this binding level
5311 (as a forward reference), just return it. */
5313 tree ref = 0;
5315 if (name != 0)
5316 ref = lookup_tag (code, name, current_binding_level, 1);
5317 if (ref && TREE_CODE (ref) == code)
5319 C_TYPE_BEING_DEFINED (ref) = 1;
5320 TYPE_PACKED (ref) = flag_pack_struct;
5321 if (TYPE_FIELDS (ref))
5323 if (code == UNION_TYPE)
5324 error ("redefinition of `union %s'",
5325 IDENTIFIER_POINTER (name));
5326 else
5327 error ("redefinition of `struct %s'",
5328 IDENTIFIER_POINTER (name));
5331 return ref;
5334 /* Otherwise create a forward-reference just so the tag is in scope. */
5336 ref = make_node (code);
5337 pushtag (name, ref);
5338 C_TYPE_BEING_DEFINED (ref) = 1;
5339 TYPE_PACKED (ref) = flag_pack_struct;
5340 return ref;
5343 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
5344 of a structure component, returning a FIELD_DECL node.
5345 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node.
5347 This is done during the parsing of the struct declaration.
5348 The FIELD_DECL nodes are chained together and the lot of them
5349 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
5351 tree
5352 grokfield (filename, line, declarator, declspecs, width)
5353 const char *filename ATTRIBUTE_UNUSED;
5354 int line ATTRIBUTE_UNUSED;
5355 tree declarator, declspecs, width;
5357 tree value;
5359 if (declarator == NULL_TREE && width == NULL_TREE)
5361 /* This is an unnamed decl.
5363 If we have something of the form "union { list } ;" then this
5364 is the anonymous union extension. Similarly for struct.
5366 If this is something of the form "struct foo;", then
5367 If MS extensions are enabled, this is handled as an
5368 anonymous struct.
5369 Otherwise this is a forward declaration of a structure tag.
5371 If this is something of the form "foo;" and foo is a TYPE_DECL, then
5372 If MS extensions are enabled and foo names a structure, then
5373 again this is an anonymous struct.
5374 Otherwise this is an error.
5376 Oh what a horrid tangled web we weave. I wonder if MS consiously
5377 took this from Plan 9 or if it was an accident of implementation
5378 that took root before someone noticed the bug... */
5380 tree type = TREE_VALUE (declspecs);
5382 if (flag_ms_extensions && TREE_CODE (type) == TYPE_DECL)
5383 type = TREE_TYPE (type);
5384 if (TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE)
5386 if (flag_ms_extensions)
5387 ; /* ok */
5388 else if (flag_iso)
5389 goto warn_unnamed_field;
5390 else if (TYPE_NAME (type) == NULL)
5391 ; /* ok */
5392 else
5393 goto warn_unnamed_field;
5395 else
5397 warn_unnamed_field:
5398 warning ("declaration does not declare anything");
5399 return NULL_TREE;
5403 value = grokdeclarator (declarator, declspecs, width ? BITFIELD : FIELD, 0);
5405 finish_decl (value, NULL_TREE, NULL_TREE);
5406 DECL_INITIAL (value) = width;
5408 maybe_objc_check_decl (value);
5409 return value;
5412 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5413 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5414 ATTRIBUTES are attributes to be applied to the structure. */
5416 tree
5417 finish_struct (t, fieldlist, attributes)
5418 tree t;
5419 tree fieldlist;
5420 tree attributes;
5422 tree x;
5423 int toplevel = global_binding_level == current_binding_level;
5424 int saw_named_field;
5426 /* If this type was previously laid out as a forward reference,
5427 make sure we lay it out again. */
5429 TYPE_SIZE (t) = 0;
5431 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5433 /* Nameless union parm types are useful as GCC extension. */
5434 if (! (TREE_CODE (t) == UNION_TYPE && TYPE_NAME (t) == 0) && !pedantic)
5435 /* Otherwise, warn about any struct or union def. in parmlist. */
5436 if (in_parm_level_p ())
5438 if (pedantic)
5439 pedwarn ("%s defined inside parms",
5440 TREE_CODE (t) == UNION_TYPE ? _("union") : _("structure"));
5441 else
5442 warning ("%s defined inside parms",
5443 TREE_CODE (t) == UNION_TYPE ? _("union") : _("structure"));
5446 if (pedantic)
5448 for (x = fieldlist; x; x = TREE_CHAIN (x))
5449 if (DECL_NAME (x) != 0)
5450 break;
5452 if (x == 0)
5453 pedwarn ("%s has no %s",
5454 TREE_CODE (t) == UNION_TYPE ? _("union") : _("struct"),
5455 fieldlist ? _("named members") : _("members"));
5458 /* Install struct as DECL_CONTEXT of each field decl.
5459 Also process specified field sizes,m which is found in the DECL_INITIAL.
5460 Store 0 there, except for ": 0" fields (so we can find them
5461 and delete them, below). */
5463 saw_named_field = 0;
5464 for (x = fieldlist; x; x = TREE_CHAIN (x))
5466 DECL_CONTEXT (x) = t;
5467 DECL_PACKED (x) |= TYPE_PACKED (t);
5469 /* If any field is const, the structure type is pseudo-const. */
5470 if (TREE_READONLY (x))
5471 C_TYPE_FIELDS_READONLY (t) = 1;
5472 else
5474 /* A field that is pseudo-const makes the structure likewise. */
5475 tree t1 = TREE_TYPE (x);
5476 while (TREE_CODE (t1) == ARRAY_TYPE)
5477 t1 = TREE_TYPE (t1);
5478 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5479 && C_TYPE_FIELDS_READONLY (t1))
5480 C_TYPE_FIELDS_READONLY (t) = 1;
5483 /* Any field that is volatile means variables of this type must be
5484 treated in some ways as volatile. */
5485 if (TREE_THIS_VOLATILE (x))
5486 C_TYPE_FIELDS_VOLATILE (t) = 1;
5488 /* Any field of nominal variable size implies structure is too. */
5489 if (C_DECL_VARIABLE_SIZE (x))
5490 C_TYPE_VARIABLE_SIZE (t) = 1;
5492 /* Detect invalid nested redefinition. */
5493 if (TREE_TYPE (x) == t)
5494 error ("nested redefinition of `%s'",
5495 IDENTIFIER_POINTER (TYPE_NAME (t)));
5497 /* Detect invalid bit-field size. */
5498 if (DECL_INITIAL (x))
5499 STRIP_NOPS (DECL_INITIAL (x));
5500 if (DECL_INITIAL (x))
5502 if (TREE_CODE (DECL_INITIAL (x)) == INTEGER_CST)
5503 constant_expression_warning (DECL_INITIAL (x));
5504 else
5506 error_with_decl (x,
5507 "bit-field `%s' width not an integer constant");
5508 DECL_INITIAL (x) = NULL;
5512 /* Detect invalid bit-field type. */
5513 if (DECL_INITIAL (x)
5514 && TREE_CODE (TREE_TYPE (x)) != INTEGER_TYPE
5515 && TREE_CODE (TREE_TYPE (x)) != BOOLEAN_TYPE
5516 && TREE_CODE (TREE_TYPE (x)) != ENUMERAL_TYPE)
5518 error_with_decl (x, "bit-field `%s' has invalid type");
5519 DECL_INITIAL (x) = NULL;
5522 if (DECL_INITIAL (x) && pedantic
5523 && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != integer_type_node
5524 && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != unsigned_type_node
5525 && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != c_bool_type_node
5526 /* Accept an enum that's equivalent to int or unsigned int. */
5527 && !(TREE_CODE (TREE_TYPE (x)) == ENUMERAL_TYPE
5528 && (TYPE_PRECISION (TREE_TYPE (x))
5529 == TYPE_PRECISION (integer_type_node))))
5530 pedwarn_with_decl (x, "bit-field `%s' type invalid in ISO C");
5532 /* Detect and ignore out of range field width and process valid
5533 field widths. */
5534 if (DECL_INITIAL (x))
5536 int max_width
5537 = (TYPE_MAIN_VARIANT (TREE_TYPE (x)) == c_bool_type_node
5538 ? CHAR_TYPE_SIZE : TYPE_PRECISION (TREE_TYPE (x)));
5540 if (tree_int_cst_sgn (DECL_INITIAL (x)) < 0)
5541 error_with_decl (x, "negative width in bit-field `%s'");
5542 else if (0 < compare_tree_int (DECL_INITIAL (x), max_width))
5543 pedwarn_with_decl (x, "width of `%s' exceeds its type");
5544 else if (integer_zerop (DECL_INITIAL (x)) && DECL_NAME (x) != 0)
5545 error_with_decl (x, "zero width for bit-field `%s'");
5546 else
5548 /* The test above has assured us that TREE_INT_CST_HIGH is 0. */
5549 unsigned HOST_WIDE_INT width
5550 = tree_low_cst (DECL_INITIAL (x), 1);
5552 if (TREE_CODE (TREE_TYPE (x)) == ENUMERAL_TYPE
5553 && (width < min_precision (TYPE_MIN_VALUE (TREE_TYPE (x)),
5554 TREE_UNSIGNED (TREE_TYPE (x)))
5555 || (width
5556 < min_precision (TYPE_MAX_VALUE (TREE_TYPE (x)),
5557 TREE_UNSIGNED (TREE_TYPE (x))))))
5558 warning_with_decl (x,
5559 "`%s' is narrower than values of its type");
5561 DECL_SIZE (x) = bitsize_int (width);
5562 DECL_BIT_FIELD (x) = 1;
5563 SET_DECL_C_BIT_FIELD (x);
5565 if (width == 0
5566 && ! (* targetm.ms_bitfield_layout_p) (t))
5568 /* field size 0 => force desired amount of alignment. */
5569 #ifdef EMPTY_FIELD_BOUNDARY
5570 DECL_ALIGN (x) = MAX (DECL_ALIGN (x), EMPTY_FIELD_BOUNDARY);
5571 #endif
5572 #ifdef PCC_BITFIELD_TYPE_MATTERS
5573 if (PCC_BITFIELD_TYPE_MATTERS)
5575 DECL_ALIGN (x) = MAX (DECL_ALIGN (x),
5576 TYPE_ALIGN (TREE_TYPE (x)));
5577 DECL_USER_ALIGN (x) |= TYPE_USER_ALIGN (TREE_TYPE (x));
5579 #endif
5584 else if (TREE_TYPE (x) != error_mark_node)
5586 unsigned int min_align = (DECL_PACKED (x) ? BITS_PER_UNIT
5587 : TYPE_ALIGN (TREE_TYPE (x)));
5589 /* Non-bit-fields are aligned for their type, except packed
5590 fields which require only BITS_PER_UNIT alignment. */
5591 DECL_ALIGN (x) = MAX (DECL_ALIGN (x), min_align);
5592 if (! DECL_PACKED (x))
5593 DECL_USER_ALIGN (x) |= TYPE_USER_ALIGN (TREE_TYPE (x));
5596 DECL_INITIAL (x) = 0;
5598 /* Detect flexible array member in an invalid context. */
5599 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5600 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5601 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5602 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5604 if (TREE_CODE (t) == UNION_TYPE)
5605 error_with_decl (x, "flexible array member in union");
5606 else if (TREE_CHAIN (x) != NULL_TREE)
5607 error_with_decl (x, "flexible array member not at end of struct");
5608 else if (! saw_named_field)
5609 error_with_decl (x, "flexible array member in otherwise empty struct");
5611 if (DECL_NAME (x))
5612 saw_named_field = 1;
5615 /* Delete all duplicate fields from the fieldlist */
5616 for (x = fieldlist; x && TREE_CHAIN (x);)
5617 /* Anonymous fields aren't duplicates. */
5618 if (DECL_NAME (TREE_CHAIN (x)) == 0)
5619 x = TREE_CHAIN (x);
5620 else
5622 tree y = fieldlist;
5624 while (1)
5626 if (DECL_NAME (y) == DECL_NAME (TREE_CHAIN (x)))
5627 break;
5628 if (y == x)
5629 break;
5630 y = TREE_CHAIN (y);
5632 if (DECL_NAME (y) == DECL_NAME (TREE_CHAIN (x)))
5634 error_with_decl (TREE_CHAIN (x), "duplicate member `%s'");
5635 TREE_CHAIN (x) = TREE_CHAIN (TREE_CHAIN (x));
5637 else
5638 x = TREE_CHAIN (x);
5641 /* Now we have the nearly final fieldlist. Record it,
5642 then lay out the structure or union (including the fields). */
5644 TYPE_FIELDS (t) = fieldlist;
5646 layout_type (t);
5648 /* Delete all zero-width bit-fields from the fieldlist */
5650 tree *fieldlistp = &fieldlist;
5651 while (*fieldlistp)
5652 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp))
5653 *fieldlistp = TREE_CHAIN (*fieldlistp);
5654 else
5655 fieldlistp = &TREE_CHAIN (*fieldlistp);
5658 /* Now we have the truly final field list.
5659 Store it in this type and in the variants. */
5661 TYPE_FIELDS (t) = fieldlist;
5663 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5665 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5666 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5667 TYPE_ALIGN (x) = TYPE_ALIGN (t);
5668 TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t);
5671 /* If this was supposed to be a transparent union, but we can't
5672 make it one, warn and turn off the flag. */
5673 if (TREE_CODE (t) == UNION_TYPE
5674 && TYPE_TRANSPARENT_UNION (t)
5675 && TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t)))
5677 TYPE_TRANSPARENT_UNION (t) = 0;
5678 warning ("union cannot be made transparent");
5681 /* If this structure or union completes the type of any previous
5682 variable declaration, lay it out and output its rtl. */
5684 if (current_binding_level->incomplete_list != NULL_TREE)
5686 tree prev = NULL_TREE;
5688 for (x = current_binding_level->incomplete_list; x; x = TREE_CHAIN (x))
5690 tree decl = TREE_VALUE (x);
5692 if (TYPE_MAIN_VARIANT (TREE_TYPE (decl)) == TYPE_MAIN_VARIANT (t)
5693 && TREE_CODE (decl) != TYPE_DECL)
5695 layout_decl (decl, 0);
5696 /* This is a no-op in c-lang.c or something real in objc-actions.c. */
5697 maybe_objc_check_decl (decl);
5698 rest_of_decl_compilation (decl, NULL, toplevel, 0);
5699 if (! toplevel)
5700 expand_decl (decl);
5701 /* Unlink X from the incomplete list. */
5702 if (prev)
5703 TREE_CHAIN (prev) = TREE_CHAIN (x);
5704 else
5705 current_binding_level->incomplete_list = TREE_CHAIN (x);
5707 else if (!COMPLETE_TYPE_P (TREE_TYPE (decl))
5708 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5710 tree element = TREE_TYPE (decl);
5711 while (TREE_CODE (element) == ARRAY_TYPE)
5712 element = TREE_TYPE (element);
5713 if (element == t)
5715 layout_array_type (TREE_TYPE (decl));
5716 if (TREE_CODE (decl) != TYPE_DECL)
5718 layout_decl (decl, 0);
5719 maybe_objc_check_decl (decl);
5720 rest_of_decl_compilation (decl, NULL, toplevel, 0);
5721 if (! toplevel)
5722 expand_decl (decl);
5724 /* Unlink X from the incomplete list. */
5725 if (prev)
5726 TREE_CHAIN (prev) = TREE_CHAIN (x);
5727 else
5728 current_binding_level->incomplete_list = TREE_CHAIN (x);
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 named_labels = 0;
6026 shadowed_labels = 0;
6028 /* Don't expand any sizes in the return type of the function. */
6029 immediate_size_expand = 0;
6031 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1);
6033 /* If the declarator is not suitable for a function definition,
6034 cause a syntax error. */
6035 if (decl1 == 0)
6037 immediate_size_expand = old_immediate_size_expand;
6038 return 0;
6041 decl_attributes (&decl1, attributes, 0);
6043 /* If #pragma weak was used, mark the decl weak now. */
6044 if (current_binding_level == global_binding_level)
6045 maybe_apply_pragma_weak (decl1);
6047 if (DECL_DECLARED_INLINE_P (decl1)
6048 && DECL_UNINLINABLE (decl1)
6049 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
6050 warning_with_decl (decl1,
6051 "inline function `%s' given attribute noinline");
6053 announce_function (decl1);
6055 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
6057 error ("return type is an incomplete type");
6058 /* Make it return void instead. */
6059 TREE_TYPE (decl1)
6060 = build_function_type (void_type_node,
6061 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
6064 if (warn_about_return_type)
6065 pedwarn_c99 ("return type defaults to `int'");
6067 /* Save the parm names or decls from this function's declarator
6068 where store_parm_decls will find them. */
6069 current_function_parms = last_function_parms;
6070 current_function_parm_tags = last_function_parm_tags;
6072 /* Make the init_value nonzero so pushdecl knows this is not tentative.
6073 error_mark_node is replaced below (in poplevel) with the BLOCK. */
6074 DECL_INITIAL (decl1) = error_mark_node;
6076 /* If this definition isn't a prototype and we had a prototype declaration
6077 before, copy the arg type info from that prototype.
6078 But not if what we had before was a builtin function. */
6079 old_decl = lookup_name_current_level (DECL_NAME (decl1));
6080 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
6081 && !DECL_BUILT_IN (old_decl)
6082 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
6083 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (old_decl))))
6084 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
6086 TREE_TYPE (decl1) = TREE_TYPE (old_decl);
6087 current_function_prototype_file = DECL_SOURCE_FILE (old_decl);
6088 current_function_prototype_line = DECL_SOURCE_LINE (old_decl);
6091 /* If there is no explicit declaration, look for any out-of-scope implicit
6092 declarations. */
6093 if (old_decl == 0)
6094 old_decl = IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1));
6096 /* Optionally warn of old-fashioned def with no previous prototype. */
6097 if (warn_strict_prototypes
6098 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
6099 && !(old_decl != 0
6100 && (TYPE_ARG_TYPES (TREE_TYPE (old_decl)) != 0
6101 || (DECL_BUILT_IN (old_decl)
6102 && ! C_DECL_ANTICIPATED (old_decl)))))
6103 warning ("function declaration isn't a prototype");
6104 /* Optionally warn of any global def with no previous prototype. */
6105 else if (warn_missing_prototypes
6106 && TREE_PUBLIC (decl1)
6107 && !(old_decl != 0
6108 && (TYPE_ARG_TYPES (TREE_TYPE (old_decl)) != 0
6109 || (DECL_BUILT_IN (old_decl)
6110 && ! C_DECL_ANTICIPATED (old_decl))))
6111 && ! MAIN_NAME_P (DECL_NAME (decl1)))
6112 warning_with_decl (decl1, "no previous prototype for `%s'");
6113 /* Optionally warn of any def with no previous prototype
6114 if the function has already been used. */
6115 else if (warn_missing_prototypes
6116 && old_decl != 0 && TREE_USED (old_decl)
6117 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
6118 warning_with_decl (decl1,
6119 "`%s' was used with no prototype before its definition");
6120 /* Optionally warn of any global def with no previous declaration. */
6121 else if (warn_missing_declarations
6122 && TREE_PUBLIC (decl1)
6123 && old_decl == 0
6124 && ! MAIN_NAME_P (DECL_NAME (decl1)))
6125 warning_with_decl (decl1, "no previous declaration for `%s'");
6126 /* Optionally warn of any def with no previous declaration
6127 if the function has already been used. */
6128 else if (warn_missing_declarations
6129 && old_decl != 0 && TREE_USED (old_decl)
6130 && old_decl == IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)))
6131 warning_with_decl (decl1,
6132 "`%s' was used with no declaration before its definition");
6134 /* This is a definition, not a reference.
6135 So normally clear DECL_EXTERNAL.
6136 However, `extern inline' acts like a declaration
6137 except for defining how to inline. So set DECL_EXTERNAL in that case. */
6138 DECL_EXTERNAL (decl1) = current_extern_inline;
6140 /* This function exists in static storage.
6141 (This does not mean `static' in the C sense!) */
6142 TREE_STATIC (decl1) = 1;
6144 /* A nested function is not global. */
6145 if (current_function_decl != 0)
6146 TREE_PUBLIC (decl1) = 0;
6148 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
6149 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
6151 tree args;
6152 int argct = 0;
6154 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
6155 != integer_type_node)
6156 pedwarn_with_decl (decl1, "return type of `%s' is not `int'");
6158 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
6159 args = TREE_CHAIN (args))
6161 tree type = args ? TREE_VALUE (args) : 0;
6163 if (type == void_type_node)
6164 break;
6166 ++argct;
6167 switch (argct)
6169 case 1:
6170 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
6171 pedwarn_with_decl (decl1,
6172 "first argument of `%s' should be `int'");
6173 break;
6175 case 2:
6176 if (TREE_CODE (type) != POINTER_TYPE
6177 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
6178 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
6179 != char_type_node))
6180 pedwarn_with_decl (decl1,
6181 "second argument of `%s' should be `char **'");
6182 break;
6184 case 3:
6185 if (TREE_CODE (type) != POINTER_TYPE
6186 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
6187 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
6188 != char_type_node))
6189 pedwarn_with_decl (decl1,
6190 "third argument of `%s' should probably be `char **'");
6191 break;
6195 /* It is intentional that this message does not mention the third
6196 argument because it's only mentioned in an appendix of the
6197 standard. */
6198 if (argct > 0 && (argct < 2 || argct > 3))
6199 pedwarn_with_decl (decl1, "`%s' takes only zero or two arguments");
6201 if (! TREE_PUBLIC (decl1))
6202 pedwarn_with_decl (decl1, "`%s' is normally a non-static function");
6205 /* Record the decl so that the function name is defined.
6206 If we already have a decl for this name, and it is a FUNCTION_DECL,
6207 use the old decl. */
6209 current_function_decl = pushdecl (decl1);
6211 pushlevel (0);
6212 declare_parm_level (1);
6213 current_binding_level->subblocks_tag_transparent = 1;
6215 make_decl_rtl (current_function_decl, NULL);
6217 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
6218 /* Promote the value to int before returning it. */
6219 if (c_promoting_integer_type_p (restype))
6221 /* It retains unsignedness if not really getting wider. */
6222 if (TREE_UNSIGNED (restype)
6223 && (TYPE_PRECISION (restype)
6224 == TYPE_PRECISION (integer_type_node)))
6225 restype = unsigned_type_node;
6226 else
6227 restype = integer_type_node;
6229 DECL_RESULT (current_function_decl)
6230 = build_decl (RESULT_DECL, NULL_TREE, restype);
6232 /* If this fcn was already referenced via a block-scope `extern' decl
6233 (or an implicit decl), propagate certain information about the usage. */
6234 if (TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (current_function_decl)))
6235 TREE_ADDRESSABLE (current_function_decl) = 1;
6237 immediate_size_expand = old_immediate_size_expand;
6239 start_fname_decls ();
6241 return 1;
6244 /* Store the parameter declarations into the current function declaration.
6245 This is called after parsing the parameter declarations, before
6246 digesting the body of the function.
6248 For an old-style definition, modify the function's type
6249 to specify at least the number of arguments. */
6251 void
6252 store_parm_decls ()
6254 tree fndecl = current_function_decl;
6255 tree parm;
6257 /* This is either a chain of PARM_DECLs (if a prototype was used)
6258 or a list of IDENTIFIER_NODEs (for an old-fashioned C definition). */
6259 tree specparms = current_function_parms;
6261 /* This is a list of types declared among parms in a prototype. */
6262 tree parmtags = current_function_parm_tags;
6264 /* This is a chain of PARM_DECLs from old-style parm declarations. */
6265 tree parmdecls = getdecls ();
6267 /* This is a chain of any other decls that came in among the parm
6268 declarations. If a parm is declared with enum {foo, bar} x;
6269 then CONST_DECLs for foo and bar are put here. */
6270 tree nonparms = 0;
6272 /* The function containing FNDECL, if any. */
6273 tree context = decl_function_context (fndecl);
6275 /* Nonzero if this definition is written with a prototype. */
6276 int prototype = 0;
6278 int saved_warn_shadow = warn_shadow;
6280 /* Don't re-emit shadow warnings. */
6281 warn_shadow = 0;
6283 if (specparms != 0 && TREE_CODE (specparms) != TREE_LIST)
6285 /* This case is when the function was defined with an ANSI prototype.
6286 The parms already have decls, so we need not do anything here
6287 except record them as in effect
6288 and complain if any redundant old-style parm decls were written. */
6290 tree next;
6291 tree others = 0;
6293 prototype = 1;
6295 if (parmdecls != 0)
6297 tree decl, link;
6299 error_with_decl (fndecl,
6300 "parm types given both in parmlist and separately");
6301 /* Get rid of the erroneous decls; don't keep them on
6302 the list of parms, since they might not be PARM_DECLs. */
6303 for (decl = current_binding_level->names;
6304 decl; decl = TREE_CHAIN (decl))
6305 if (DECL_NAME (decl))
6306 IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) = 0;
6307 for (link = current_binding_level->shadowed;
6308 link; link = TREE_CHAIN (link))
6309 IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
6310 current_binding_level->names = 0;
6311 current_binding_level->shadowed = 0;
6314 specparms = nreverse (specparms);
6315 for (parm = specparms; parm; parm = next)
6317 next = TREE_CHAIN (parm);
6318 if (TREE_CODE (parm) == PARM_DECL)
6320 if (DECL_NAME (parm) == 0)
6321 error_with_decl (parm, "parameter name omitted");
6322 else if (TREE_CODE (TREE_TYPE (parm)) != ERROR_MARK
6323 && VOID_TYPE_P (TREE_TYPE (parm)))
6325 error_with_decl (parm, "parameter `%s' declared void");
6326 /* Change the type to error_mark_node so this parameter
6327 will be ignored by assign_parms. */
6328 TREE_TYPE (parm) = error_mark_node;
6330 pushdecl (parm);
6332 else
6334 /* If we find an enum constant or a type tag,
6335 put it aside for the moment. */
6336 TREE_CHAIN (parm) = 0;
6337 others = chainon (others, parm);
6341 /* Get the decls in their original chain order
6342 and record in the function. */
6343 DECL_ARGUMENTS (fndecl) = getdecls ();
6345 #if 0
6346 /* If this function takes a variable number of arguments,
6347 add a phony parameter to the end of the parm list,
6348 to represent the position of the first unnamed argument. */
6349 if (TREE_VALUE (tree_last (TYPE_ARG_TYPES (TREE_TYPE (fndecl))))
6350 != void_type_node)
6352 tree dummy = build_decl (PARM_DECL, NULL_TREE, void_type_node);
6353 /* Let's hope the address of the unnamed parm
6354 won't depend on its type. */
6355 TREE_TYPE (dummy) = integer_type_node;
6356 DECL_ARG_TYPE (dummy) = integer_type_node;
6357 DECL_ARGUMENTS (fndecl) = chainon (DECL_ARGUMENTS (fndecl), dummy);
6359 #endif
6361 /* Now pushdecl the enum constants. */
6362 for (parm = others; parm; parm = next)
6364 next = TREE_CHAIN (parm);
6365 if (DECL_NAME (parm) == 0)
6367 else if (TYPE_MAIN_VARIANT (TREE_TYPE (parm)) == void_type_node)
6369 else if (TREE_CODE (parm) != PARM_DECL)
6370 pushdecl (parm);
6373 storetags (chainon (parmtags, gettags ()));
6375 else
6377 /* SPECPARMS is an identifier list--a chain of TREE_LIST nodes
6378 each with a parm name as the TREE_VALUE.
6380 PARMDECLS is a chain of declarations for parameters.
6381 Warning! It can also contain CONST_DECLs which are not parameters
6382 but are names of enumerators of any enum types
6383 declared among the parameters.
6385 First match each formal parameter name with its declaration.
6386 Associate decls with the names and store the decls
6387 into the TREE_PURPOSE slots. */
6389 /* We use DECL_WEAK as a flag to show which parameters have been
6390 seen already since it is not used on PARM_DECL or CONST_DECL. */
6391 for (parm = parmdecls; parm; parm = TREE_CHAIN (parm))
6392 DECL_WEAK (parm) = 0;
6394 for (parm = specparms; parm; parm = TREE_CHAIN (parm))
6396 tree tail, found = NULL;
6398 if (TREE_VALUE (parm) == 0)
6400 error_with_decl (fndecl,
6401 "parameter name missing from parameter list");
6402 TREE_PURPOSE (parm) = 0;
6403 continue;
6406 /* See if any of the parmdecls specifies this parm by name.
6407 Ignore any enumerator decls. */
6408 for (tail = parmdecls; tail; tail = TREE_CHAIN (tail))
6409 if (DECL_NAME (tail) == TREE_VALUE (parm)
6410 && TREE_CODE (tail) == PARM_DECL)
6412 found = tail;
6413 break;
6416 /* If declaration already marked, we have a duplicate name.
6417 Complain, and don't use this decl twice. */
6418 if (found && DECL_WEAK (found))
6420 error_with_decl (found, "multiple parameters named `%s'");
6421 found = 0;
6424 /* If the declaration says "void", complain and ignore it. */
6425 if (found && VOID_TYPE_P (TREE_TYPE (found)))
6427 error_with_decl (found, "parameter `%s' declared void");
6428 TREE_TYPE (found) = integer_type_node;
6429 DECL_ARG_TYPE (found) = integer_type_node;
6430 layout_decl (found, 0);
6433 /* If no declaration found, default to int. */
6434 if (!found)
6436 found = build_decl (PARM_DECL, TREE_VALUE (parm),
6437 integer_type_node);
6438 DECL_ARG_TYPE (found) = TREE_TYPE (found);
6439 DECL_SOURCE_LINE (found) = DECL_SOURCE_LINE (fndecl);
6440 DECL_SOURCE_FILE (found) = DECL_SOURCE_FILE (fndecl);
6441 if (flag_isoc99)
6442 pedwarn_with_decl (found, "type of `%s' defaults to `int'");
6443 else if (extra_warnings)
6444 warning_with_decl (found, "type of `%s' defaults to `int'");
6445 pushdecl (found);
6448 TREE_PURPOSE (parm) = found;
6450 /* Mark this decl as "already found". */
6451 DECL_WEAK (found) = 1;
6454 /* Put anything which is on the parmdecls chain and which is
6455 not a PARM_DECL onto the list NONPARMS. (The types of
6456 non-parm things which might appear on the list include
6457 enumerators and NULL-named TYPE_DECL nodes.) Complain about
6458 any actual PARM_DECLs not matched with any names. */
6460 nonparms = 0;
6461 for (parm = parmdecls; parm;)
6463 tree next = TREE_CHAIN (parm);
6464 TREE_CHAIN (parm) = 0;
6466 if (TREE_CODE (parm) != PARM_DECL)
6467 nonparms = chainon (nonparms, parm);
6468 else
6470 /* Complain about args with incomplete types. */
6471 if (!COMPLETE_TYPE_P (TREE_TYPE (parm)))
6473 error_with_decl (parm, "parameter `%s' has incomplete type");
6474 TREE_TYPE (parm) = error_mark_node;
6477 if (! DECL_WEAK (parm))
6479 error_with_decl (parm,
6480 "declaration for parameter `%s' but no such parameter");
6481 /* Pretend the parameter was not missing.
6482 This gets us to a standard state and minimizes
6483 further error messages. */
6484 specparms
6485 = chainon (specparms,
6486 tree_cons (parm, NULL_TREE, NULL_TREE));
6490 parm = next;
6493 /* Chain the declarations together in the order of the list of
6494 names. Store that chain in the function decl, replacing the
6495 list of names. */
6496 parm = specparms;
6497 DECL_ARGUMENTS (fndecl) = 0;
6499 tree last;
6500 for (last = 0; parm; parm = TREE_CHAIN (parm))
6501 if (TREE_PURPOSE (parm))
6503 if (last == 0)
6504 DECL_ARGUMENTS (fndecl) = TREE_PURPOSE (parm);
6505 else
6506 TREE_CHAIN (last) = TREE_PURPOSE (parm);
6507 last = TREE_PURPOSE (parm);
6508 TREE_CHAIN (last) = 0;
6512 /* If there was a previous prototype,
6513 set the DECL_ARG_TYPE of each argument according to
6514 the type previously specified, and report any mismatches. */
6516 if (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
6518 tree type;
6519 for (parm = DECL_ARGUMENTS (fndecl),
6520 type = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6521 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
6522 != void_type_node));
6523 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6525 if (parm == 0 || type == 0
6526 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6528 error ("number of arguments doesn't match prototype");
6529 error_with_file_and_line (current_function_prototype_file,
6530 current_function_prototype_line,
6531 "prototype declaration");
6532 break;
6534 /* Type for passing arg must be consistent with that
6535 declared for the arg. ISO C says we take the unqualified
6536 type for parameters declared with qualified type. */
6537 if (! comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6538 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
6540 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6541 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6543 /* Adjust argument to match prototype. E.g. a previous
6544 `int foo(float);' prototype causes
6545 `int foo(x) float x; {...}' to be treated like
6546 `int foo(float x) {...}'. This is particularly
6547 useful for argument types like uid_t. */
6548 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6550 if (PROMOTE_PROTOTYPES
6551 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6552 && TYPE_PRECISION (TREE_TYPE (parm))
6553 < TYPE_PRECISION (integer_type_node))
6554 DECL_ARG_TYPE (parm) = integer_type_node;
6556 if (pedantic)
6558 pedwarn ("promoted argument `%s' doesn't match prototype",
6559 IDENTIFIER_POINTER (DECL_NAME (parm)));
6560 warning_with_file_and_line
6561 (current_function_prototype_file,
6562 current_function_prototype_line,
6563 "prototype declaration");
6566 else
6568 error ("argument `%s' doesn't match prototype",
6569 IDENTIFIER_POINTER (DECL_NAME (parm)));
6570 error_with_file_and_line (current_function_prototype_file,
6571 current_function_prototype_line,
6572 "prototype declaration");
6576 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6579 /* Otherwise, create a prototype that would match. */
6581 else
6583 tree actual = 0, last = 0, type;
6585 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6587 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6588 if (last)
6589 TREE_CHAIN (last) = type;
6590 else
6591 actual = type;
6592 last = type;
6594 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6595 if (last)
6596 TREE_CHAIN (last) = type;
6597 else
6598 actual = type;
6600 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6601 of the type of this function, but we need to avoid having this
6602 affect the types of other similarly-typed functions, so we must
6603 first force the generation of an identical (but separate) type
6604 node for the relevant function type. The new node we create
6605 will be a variant of the main variant of the original function
6606 type. */
6608 TREE_TYPE (fndecl) = build_type_copy (TREE_TYPE (fndecl));
6610 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6613 /* Now store the final chain of decls for the arguments
6614 as the decl-chain of the current lexical scope.
6615 Put the enumerators in as well, at the front so that
6616 DECL_ARGUMENTS is not modified. */
6618 storedecls (chainon (nonparms, DECL_ARGUMENTS (fndecl)));
6621 /* Make sure the binding level for the top of the function body
6622 gets a BLOCK if there are any in the function.
6623 Otherwise, the dbx output is wrong. */
6625 keep_next_if_subblocks = 1;
6627 /* ??? This might be an improvement,
6628 but needs to be thought about some more. */
6629 #if 0
6630 keep_next_level_flag = 1;
6631 #endif
6633 /* Write a record describing this function definition to the prototypes
6634 file (if requested). */
6636 gen_aux_info_record (fndecl, 1, 0, prototype);
6638 /* Initialize the RTL code for the function. */
6639 init_function_start (fndecl, input_filename, lineno);
6641 /* Begin the statement tree for this function. */
6642 begin_stmt_tree (&DECL_SAVED_TREE (current_function_decl));
6644 /* If this is a nested function, save away the sizes of any
6645 variable-size types so that we can expand them when generating
6646 RTL. */
6647 if (context)
6649 tree t;
6651 DECL_LANG_SPECIFIC (fndecl)->pending_sizes
6652 = nreverse (get_pending_sizes ());
6653 for (t = DECL_LANG_SPECIFIC (fndecl)->pending_sizes;
6655 t = TREE_CHAIN (t))
6656 SAVE_EXPR_CONTEXT (TREE_VALUE (t)) = context;
6659 /* This function is being processed in whole-function mode. */
6660 cfun->x_whole_function_mode_p = 1;
6662 /* Even though we're inside a function body, we still don't want to
6663 call expand_expr to calculate the size of a variable-sized array.
6664 We haven't necessarily assigned RTL to all variables yet, so it's
6665 not safe to try to expand expressions involving them. */
6666 immediate_size_expand = 0;
6667 cfun->x_dont_save_pending_sizes_p = 1;
6669 warn_shadow = saved_warn_shadow;
6672 /* Finish up a function declaration and compile that function
6673 all the way to assembler language output. The free the storage
6674 for the function definition.
6676 This is called after parsing the body of the function definition.
6678 NESTED is nonzero if the function being finished is nested in another.
6679 CAN_DEFER_P is nonzero if the function may be deferred. */
6681 void
6682 finish_function (nested, can_defer_p)
6683 int nested;
6684 int can_defer_p;
6686 tree fndecl = current_function_decl;
6688 #if 0
6689 /* This caused &foo to be of type ptr-to-const-function which then
6690 got a warning when stored in a ptr-to-function variable. */
6691 TREE_READONLY (fndecl) = 1;
6692 #endif
6694 poplevel (1, 0, 1);
6695 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6697 /* Must mark the RESULT_DECL as being in this function. */
6699 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6701 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6703 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6704 != integer_type_node)
6706 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6707 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6708 if (! warn_main)
6709 pedwarn_with_decl (fndecl, "return type of `%s' is not `int'");
6711 else
6713 #ifdef DEFAULT_MAIN_RETURN
6714 /* Make it so that `main' always returns success by default. */
6715 DEFAULT_MAIN_RETURN;
6716 #else
6717 if (flag_isoc99)
6718 c_expand_return (integer_zero_node);
6719 #endif
6723 finish_fname_decls ();
6725 /* Tie off the statement tree for this function. */
6726 finish_stmt_tree (&DECL_SAVED_TREE (fndecl));
6728 /* Complain if there's just no return statement. */
6729 if (warn_return_type
6730 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6731 && !current_function_returns_value && !current_function_returns_null
6732 /* Don't complain if we abort. */
6733 && !current_function_returns_abnormally
6734 /* Don't warn for main(). */
6735 && !MAIN_NAME_P (DECL_NAME (fndecl))
6736 /* Or if they didn't actually specify a return type. */
6737 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6738 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6739 inline function, as we might never be compiled separately. */
6740 && DECL_INLINE (fndecl))
6741 warning ("no return statement in function returning non-void");
6743 /* Clear out memory we no longer need. */
6744 free_after_parsing (cfun);
6745 /* Since we never call rest_of_compilation, we never clear
6746 CFUN. Do so explicitly. */
6747 free_after_compilation (cfun);
6748 cfun = NULL;
6750 if (! nested)
6752 /* Generate RTL for the body of this function. */
6753 c_expand_body (fndecl, nested, can_defer_p);
6755 /* Let the error reporting routines know that we're outside a
6756 function. For a nested function, this value is used in
6757 c_pop_function_context and then reset via pop_function_context. */
6758 current_function_decl = NULL;
6762 /* Generate the RTL for a deferred function FNDECL. */
6764 void
6765 c_expand_deferred_function (fndecl)
6766 tree fndecl;
6768 /* DECL_INLINE or DECL_RESULT might got cleared after the inline
6769 function was deferred, e.g. in duplicate_decls. */
6770 if (DECL_INLINE (fndecl) && DECL_RESULT (fndecl))
6772 c_expand_body (fndecl, 0, 0);
6773 current_function_decl = NULL;
6777 /* Generate the RTL for the body of FNDECL. If NESTED_P is non-zero,
6778 then we are already in the process of generating RTL for another
6779 function. If can_defer_p is zero, we won't attempt to defer the
6780 generation of RTL. */
6782 static void
6783 c_expand_body (fndecl, nested_p, can_defer_p)
6784 tree fndecl;
6785 int nested_p, can_defer_p;
6787 int uninlinable = 1;
6789 /* There's no reason to do any of the work here if we're only doing
6790 semantic analysis; this code just generates RTL. */
6791 if (flag_syntax_only)
6792 return;
6794 if (flag_inline_trees)
6796 /* First, cache whether the current function is inlinable. Some
6797 predicates depend on cfun and current_function_decl to
6798 function completely. */
6799 timevar_push (TV_INTEGRATION);
6800 uninlinable = ! tree_inlinable_function_p (fndecl);
6802 if (! uninlinable && can_defer_p
6803 /* Save function tree for inlining. Should return 0 if the
6804 language does not support function deferring or the
6805 function could not be deferred. */
6806 && defer_fn (fndecl))
6808 /* Let the back-end know that this function exists. */
6809 (*debug_hooks->deferred_inline_function) (fndecl);
6810 timevar_pop (TV_INTEGRATION);
6811 return;
6814 /* Then, inline any functions called in it. */
6815 optimize_inline_calls (fndecl);
6816 timevar_pop (TV_INTEGRATION);
6819 timevar_push (TV_EXPAND);
6821 if (nested_p)
6823 /* Make sure that we will evaluate variable-sized types involved
6824 in our function's type. */
6825 expand_pending_sizes (DECL_LANG_SPECIFIC (fndecl)->pending_sizes);
6826 /* Squirrel away our current state. */
6827 push_function_context ();
6830 /* Initialize the RTL code for the function. */
6831 current_function_decl = fndecl;
6832 input_filename = DECL_SOURCE_FILE (fndecl);
6833 init_function_start (fndecl, input_filename, DECL_SOURCE_LINE (fndecl));
6835 /* This function is being processed in whole-function mode. */
6836 cfun->x_whole_function_mode_p = 1;
6838 /* Even though we're inside a function body, we still don't want to
6839 call expand_expr to calculate the size of a variable-sized array.
6840 We haven't necessarily assigned RTL to all variables yet, so it's
6841 not safe to try to expand expressions involving them. */
6842 immediate_size_expand = 0;
6843 cfun->x_dont_save_pending_sizes_p = 1;
6845 /* Set up parameters and prepare for return, for the function. */
6846 expand_function_start (fndecl, 0);
6848 /* If this function is `main', emit a call to `__main'
6849 to run global initializers, etc. */
6850 if (DECL_NAME (fndecl)
6851 && MAIN_NAME_P (DECL_NAME (fndecl))
6852 && DECL_CONTEXT (fndecl) == NULL_TREE)
6853 expand_main_function ();
6855 /* Generate the RTL for this function. */
6856 expand_stmt (DECL_SAVED_TREE (fndecl));
6857 if (uninlinable)
6859 /* Allow the body of the function to be garbage collected. */
6860 DECL_SAVED_TREE (fndecl) = NULL_TREE;
6863 /* We hard-wired immediate_size_expand to zero above.
6864 expand_function_end will decrement this variable. So, we set the
6865 variable to one here, so that after the decrement it will remain
6866 zero. */
6867 immediate_size_expand = 1;
6869 /* Allow language dialects to perform special processing. */
6870 if (lang_expand_function_end)
6871 (*lang_expand_function_end) ();
6873 /* Generate rtl for function exit. */
6874 expand_function_end (input_filename, lineno, 0);
6876 /* If this is a nested function, protect the local variables in the stack
6877 above us from being collected while we're compiling this function. */
6878 if (nested_p)
6879 ggc_push_context ();
6881 /* Run the optimizers and output the assembler code for this function. */
6882 rest_of_compilation (fndecl);
6884 /* Undo the GC context switch. */
6885 if (nested_p)
6886 ggc_pop_context ();
6888 /* With just -W, complain only if function returns both with
6889 and without a value. */
6890 if (extra_warnings
6891 && current_function_returns_value
6892 && current_function_returns_null)
6893 warning ("this function may return with or without a value");
6895 /* If requested, warn about function definitions where the function will
6896 return a value (usually of some struct or union type) which itself will
6897 take up a lot of stack space. */
6899 if (warn_larger_than && !DECL_EXTERNAL (fndecl) && TREE_TYPE (fndecl))
6901 tree ret_type = TREE_TYPE (TREE_TYPE (fndecl));
6903 if (ret_type && TYPE_SIZE_UNIT (ret_type)
6904 && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
6905 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type),
6906 larger_than_size))
6908 unsigned int size_as_int
6909 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
6911 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
6912 warning_with_decl (fndecl,
6913 "size of return value of `%s' is %u bytes",
6914 size_as_int);
6915 else
6916 warning_with_decl (fndecl,
6917 "size of return value of `%s' is larger than %d bytes",
6918 larger_than_size);
6922 if (DECL_SAVED_INSNS (fndecl) == 0 && ! nested_p
6923 && ! flag_inline_trees)
6925 /* Stop pointing to the local nodes about to be freed.
6926 But DECL_INITIAL must remain nonzero so we know this
6927 was an actual function definition.
6928 For a nested function, this is done in c_pop_function_context.
6929 If rest_of_compilation set this to 0, leave it 0. */
6930 if (DECL_INITIAL (fndecl) != 0)
6931 DECL_INITIAL (fndecl) = error_mark_node;
6933 DECL_ARGUMENTS (fndecl) = 0;
6936 if (DECL_STATIC_CONSTRUCTOR (fndecl))
6938 if (targetm.have_ctors_dtors)
6939 (* targetm.asm_out.constructor) (XEXP (DECL_RTL (fndecl), 0),
6940 DEFAULT_INIT_PRIORITY);
6941 else
6942 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6945 if (DECL_STATIC_DESTRUCTOR (fndecl))
6947 if (targetm.have_ctors_dtors)
6948 (* targetm.asm_out.destructor) (XEXP (DECL_RTL (fndecl), 0),
6949 DEFAULT_INIT_PRIORITY);
6950 else
6951 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6954 if (nested_p)
6955 /* Return to the enclosing function. */
6956 pop_function_context ();
6957 timevar_pop (TV_EXPAND);
6960 /* Check the declarations given in a for-loop for satisfying the C99
6961 constraints. */
6962 void
6963 check_for_loop_decls ()
6965 tree t;
6967 if (!flag_isoc99)
6969 /* If we get here, declarations have been used in a for loop without
6970 the C99 for loop scope. This doesn't make much sense, so don't
6971 allow it. */
6972 error ("`for' loop initial declaration used outside C99 mode");
6973 return;
6975 /* C99 subclause 6.8.5 paragraph 3:
6977 [#3] The declaration part of a for statement shall only
6978 declare identifiers for objects having storage class auto or
6979 register.
6981 It isn't clear whether, in this sentence, "identifiers" binds to
6982 "shall only declare" or to "objects" - that is, whether all identifiers
6983 declared must be identifiers for objects, or whether the restriction
6984 only applies to those that are. (A question on this in comp.std.c
6985 in November 2000 received no answer.) We implement the strictest
6986 interpretation, to avoid creating an extension which later causes
6987 problems. */
6989 for (t = gettags (); t; t = TREE_CHAIN (t))
6991 if (TREE_PURPOSE (t) != 0)
6993 enum tree_code code = TREE_CODE (TREE_VALUE (t));
6995 if (code == RECORD_TYPE)
6996 error ("`struct %s' declared in `for' loop initial declaration",
6997 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6998 else if (code == UNION_TYPE)
6999 error ("`union %s' declared in `for' loop initial declaration",
7000 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
7001 else
7002 error ("`enum %s' declared in `for' loop initial declaration",
7003 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
7007 for (t = getdecls (); t; t = TREE_CHAIN (t))
7009 if (TREE_CODE (t) != VAR_DECL && DECL_NAME (t))
7010 error_with_decl (t, "declaration of non-variable `%s' in `for' loop initial declaration");
7011 else if (TREE_STATIC (t))
7012 error_with_decl (t, "declaration of static variable `%s' in `for' loop initial declaration");
7013 else if (DECL_EXTERNAL (t))
7014 error_with_decl (t, "declaration of `extern' variable `%s' in `for' loop initial declaration");
7018 /* Save and restore the variables in this file and elsewhere
7019 that keep track of the progress of compilation of the current function.
7020 Used for nested functions. */
7022 struct language_function GTY(())
7024 struct c_language_function base;
7025 tree named_labels;
7026 tree shadowed_labels;
7027 int returns_value;
7028 int returns_null;
7029 int returns_abnormally;
7030 int warn_about_return_type;
7031 int extern_inline;
7032 struct binding_level *binding_level;
7035 /* Save and reinitialize the variables
7036 used during compilation of a C function. */
7038 void
7039 c_push_function_context (f)
7040 struct function *f;
7042 struct language_function *p;
7043 p = ((struct language_function *)
7044 ggc_alloc (sizeof (struct language_function)));
7045 f->language = p;
7047 p->base.x_stmt_tree = c_stmt_tree;
7048 p->base.x_scope_stmt_stack = c_scope_stmt_stack;
7049 p->named_labels = named_labels;
7050 p->shadowed_labels = shadowed_labels;
7051 p->returns_value = current_function_returns_value;
7052 p->returns_null = current_function_returns_null;
7053 p->returns_abnormally = current_function_returns_abnormally;
7054 p->warn_about_return_type = warn_about_return_type;
7055 p->extern_inline = current_extern_inline;
7056 p->binding_level = current_binding_level;
7059 /* Restore the variables used during compilation of a C function. */
7061 void
7062 c_pop_function_context (f)
7063 struct function *f;
7065 struct language_function *p = f->language;
7066 tree link;
7068 /* Bring back all the labels that were shadowed. */
7069 for (link = shadowed_labels; link; link = TREE_CHAIN (link))
7070 if (DECL_NAME (TREE_VALUE (link)) != 0)
7071 IDENTIFIER_LABEL_VALUE (DECL_NAME (TREE_VALUE (link)))
7072 = TREE_VALUE (link);
7074 if (DECL_SAVED_INSNS (current_function_decl) == 0
7075 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
7077 /* Stop pointing to the local nodes about to be freed. */
7078 /* But DECL_INITIAL must remain nonzero so we know this
7079 was an actual function definition. */
7080 DECL_INITIAL (current_function_decl) = error_mark_node;
7081 DECL_ARGUMENTS (current_function_decl) = 0;
7084 c_stmt_tree = p->base.x_stmt_tree;
7085 c_scope_stmt_stack = p->base.x_scope_stmt_stack;
7086 named_labels = p->named_labels;
7087 shadowed_labels = p->shadowed_labels;
7088 current_function_returns_value = p->returns_value;
7089 current_function_returns_null = p->returns_null;
7090 current_function_returns_abnormally = p->returns_abnormally;
7091 warn_about_return_type = p->warn_about_return_type;
7092 current_extern_inline = p->extern_inline;
7093 current_binding_level = p->binding_level;
7095 f->language = NULL;
7098 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
7100 void
7101 c_dup_lang_specific_decl (decl)
7102 tree decl;
7104 struct lang_decl *ld;
7106 if (!DECL_LANG_SPECIFIC (decl))
7107 return;
7109 ld = (struct lang_decl *) ggc_alloc (sizeof (struct lang_decl));
7110 memcpy ((char *) ld, (char *) DECL_LANG_SPECIFIC (decl),
7111 sizeof (struct lang_decl));
7112 DECL_LANG_SPECIFIC (decl) = ld;
7115 /* The functions below are required for functionality of doing
7116 function at once processing in the C front end. Currently these
7117 functions are not called from anywhere in the C front end, but as
7118 these changes continue, that will change. */
7120 /* Returns non-zero if the current statement is a full expression,
7121 i.e. temporaries created during that statement should be destroyed
7122 at the end of the statement. */
7125 stmts_are_full_exprs_p ()
7127 return 0;
7130 /* Returns the stmt_tree (if any) to which statements are currently
7131 being added. If there is no active statement-tree, NULL is
7132 returned. */
7134 stmt_tree
7135 current_stmt_tree ()
7137 return &c_stmt_tree;
7140 /* Returns the stack of SCOPE_STMTs for the current function. */
7142 tree *
7143 current_scope_stmt_stack ()
7145 return &c_scope_stmt_stack;
7148 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
7149 C. */
7152 anon_aggr_type_p (node)
7153 tree node ATTRIBUTE_UNUSED;
7155 return 0;
7158 /* Dummy function in place of callback used by C++. */
7160 void
7161 extract_interface_info ()
7165 /* Return a new COMPOUND_STMT, after adding it to the current
7166 statement tree. */
7168 tree
7169 c_begin_compound_stmt ()
7171 tree stmt;
7173 /* Create the COMPOUND_STMT. */
7174 stmt = add_stmt (build_stmt (COMPOUND_STMT, NULL_TREE));
7176 return stmt;
7179 /* Expand T (a DECL_STMT) if it declares an entity not handled by the
7180 common code. */
7182 void
7183 c_expand_decl_stmt (t)
7184 tree t;
7186 tree decl = DECL_STMT_DECL (t);
7188 /* Expand nested functions. */
7189 if (TREE_CODE (decl) == FUNCTION_DECL
7190 && DECL_CONTEXT (decl) == current_function_decl
7191 && DECL_SAVED_TREE (decl))
7192 c_expand_body (decl, /*nested_p=*/1, /*can_defer_p=*/0);
7195 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
7196 the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */
7198 tree
7199 identifier_global_value (t)
7200 tree t;
7202 return IDENTIFIER_GLOBAL_VALUE (t);
7205 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
7206 otherwise the name is found in ridpointers from RID_INDEX. */
7208 void
7209 record_builtin_type (rid_index, name, type)
7210 enum rid rid_index;
7211 const char *name;
7212 tree type;
7214 tree id;
7215 if (name == 0)
7216 id = ridpointers[(int) rid_index];
7217 else
7218 id = get_identifier (name);
7219 pushdecl (build_decl (TYPE_DECL, id, type));
7222 /* Build the void_list_node (void_type_node having been created). */
7223 tree
7224 build_void_list_node ()
7226 tree t = build_tree_list (NULL_TREE, void_type_node);
7227 return t;
7230 /* Return something to represent absolute declarators containing a *.
7231 TARGET is the absolute declarator that the * contains.
7232 TYPE_QUALS_ATTRS is a list of modifiers such as const or volatile
7233 to apply to the pointer type, represented as identifiers, possible mixed
7234 with attributes.
7236 We return an INDIRECT_REF whose "contents" are TARGET (inside a TREE_LIST,
7237 if attributes are present) and whose type is the modifier list. */
7239 tree
7240 make_pointer_declarator (type_quals_attrs, target)
7241 tree type_quals_attrs, target;
7243 tree quals, attrs;
7244 tree itarget = target;
7245 split_specs_attrs (type_quals_attrs, &quals, &attrs);
7246 if (attrs != NULL_TREE)
7247 itarget = tree_cons (attrs, target, NULL_TREE);
7248 return build1 (INDIRECT_REF, quals, itarget);
7251 #include "gt-c-decl.h"