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