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