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