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