OSDN Git Service

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