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