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