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