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