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