OSDN Git Service

* gcc/ChangeLog: Fix ChangeLog.
[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, 2005, 2006, 2007, 2008, 2009
4    Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
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 "input.h"
33 #include "tm.h"
34 #include "intl.h"
35 #include "tree.h"
36 #include "tree-inline.h"
37 #include "rtl.h"
38 #include "flags.h"
39 #include "function.h"
40 #include "output.h"
41 #include "expr.h"
42 #include "c-tree.h"
43 #include "toplev.h"
44 #include "ggc.h"
45 #include "tm_p.h"
46 #include "cpplib.h"
47 #include "target.h"
48 #include "debug.h"
49 #include "opts.h"
50 #include "timevar.h"
51 #include "c-common.h"
52 #include "c-pragma.h"
53 #include "langhooks.h"
54 #include "tree-mudflap.h"
55 #include "gimple.h"
56 #include "tree-iterator.h"
57 #include "diagnostic.h"
58 #include "tree-dump.h"
59 #include "cgraph.h"
60 #include "hashtab.h"
61 #include "libfuncs.h"
62 #include "except.h"
63 #include "langhooks-def.h"
64 #include "pointer-set.h"
65 #include "gimple.h"
66
67 /* In grokdeclarator, distinguish syntactic contexts of declarators.  */
68 enum decl_context
69 { NORMAL,                       /* Ordinary declaration */
70   FUNCDEF,                      /* Function definition */
71   PARM,                         /* Declaration of parm before function body */
72   FIELD,                        /* Declaration inside struct or union */
73   TYPENAME};                    /* Typename (inside cast or sizeof)  */
74
75 /* States indicating how grokdeclarator() should handle declspecs marked
76    with __attribute__((deprecated)).  An object declared as
77    __attribute__((deprecated)) suppresses warnings of uses of other
78    deprecated items.  */
79
80 enum deprecated_states {
81   DEPRECATED_NORMAL,
82   DEPRECATED_SUPPRESS
83 };
84
85 \f
86 /* Nonzero if we have seen an invalid cross reference
87    to a struct, union, or enum, but not yet printed the message.  */
88 tree pending_invalid_xref;
89
90 /* File and line to appear in the eventual error message.  */
91 location_t pending_invalid_xref_location;
92
93 /* True means we've initialized exception handling.  */
94 bool c_eh_initialized_p;
95
96 /* The file and line that the prototype came from if this is an
97    old-style definition; used for diagnostics in
98    store_parm_decls_oldstyle.  */
99
100 static location_t current_function_prototype_locus;
101
102 /* Whether this prototype was built-in.  */
103
104 static bool current_function_prototype_built_in;
105
106 /* The argument type information of this prototype.  */
107
108 static tree current_function_prototype_arg_types;
109
110 /* The argument information structure for the function currently being
111    defined.  */
112
113 static struct c_arg_info *current_function_arg_info;
114
115 /* The obstack on which parser and related data structures, which are
116    not live beyond their top-level declaration or definition, are
117    allocated.  */
118 struct obstack parser_obstack;
119
120 /* The current statement tree.  */
121
122 static GTY(()) struct stmt_tree_s c_stmt_tree;
123
124 /* State saving variables.  */
125 tree c_break_label;
126 tree c_cont_label;
127
128 /* Linked list of TRANSLATION_UNIT_DECLS for the translation units
129    included in this invocation.  Note that the current translation
130    unit is not included in this list.  */
131
132 static GTY(()) tree all_translation_units;
133
134 /* A list of decls to be made automatically visible in each file scope.  */
135 static GTY(()) tree visible_builtins;
136
137 /* Set to 0 at beginning of a function definition, set to 1 if
138    a return statement that specifies a return value is seen.  */
139
140 int current_function_returns_value;
141
142 /* Set to 0 at beginning of a function definition, set to 1 if
143    a return statement with no argument is seen.  */
144
145 int current_function_returns_null;
146
147 /* Set to 0 at beginning of a function definition, set to 1 if
148    a call to a noreturn function is seen.  */
149
150 int current_function_returns_abnormally;
151
152 /* Set to nonzero by `grokdeclarator' for a function
153    whose return type is defaulted, if warnings for this are desired.  */
154
155 static int warn_about_return_type;
156
157 /* Nonzero when the current toplevel function contains a declaration
158    of a nested function which is never defined.  */
159
160 static bool undef_nested_function;
161
162 /* True means global_bindings_p should return false even if the scope stack
163    says we are in file scope.  */
164 bool c_override_global_bindings_to_false;
165
166 \f
167 /* Each c_binding structure describes one binding of an identifier to
168    a decl.  All the decls in a scope - irrespective of namespace - are
169    chained together by the ->prev field, which (as the name implies)
170    runs in reverse order.  All the decls in a given namespace bound to
171    a given identifier are chained by the ->shadowed field, which runs
172    from inner to outer scopes.
173
174    The ->decl field usually points to a DECL node, but there are two
175    exceptions.  In the namespace of type tags, the bound entity is a
176    RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node.  If an undeclared
177    identifier is encountered, it is bound to error_mark_node to
178    suppress further errors about that identifier in the current
179    function.
180
181    The ->type field stores the type of the declaration in this scope;
182    if NULL, the type is the type of the ->decl field.  This is only of
183    relevance for objects with external or internal linkage which may
184    be redeclared in inner scopes, forming composite types that only
185    persist for the duration of those scopes.  In the external scope,
186    this stores the composite of all the types declared for this
187    object, visible or not.  The ->inner_comp field (used only at file
188    scope) stores whether an incomplete array type at file scope was
189    completed at an inner scope to an array size other than 1.
190
191    The depth field is copied from the scope structure that holds this
192    decl.  It is used to preserve the proper ordering of the ->shadowed
193    field (see bind()) and also for a handful of special-case checks.
194    Finally, the invisible bit is true for a decl which should be
195    ignored for purposes of normal name lookup, and the nested bit is
196    true for a decl that's been bound a second time in an inner scope;
197    in all such cases, the binding in the outer scope will have its
198    invisible bit true.  */
199
200 struct c_binding GTY((chain_next ("%h.prev")))
201 {
202   tree decl;                    /* the decl bound */
203   tree type;                    /* the type in this scope */
204   tree id;                      /* the identifier it's bound to */
205   struct c_binding *prev;       /* the previous decl in this scope */
206   struct c_binding *shadowed;   /* the innermost decl shadowed by this one */
207   unsigned int depth : 28;      /* depth of this scope */
208   BOOL_BITFIELD invisible : 1;  /* normal lookup should ignore this binding */
209   BOOL_BITFIELD nested : 1;     /* do not set DECL_CONTEXT when popping */
210   BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
211   /* one free bit */
212   location_t locus;             /* location for nested bindings */
213 };
214 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
215 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
216 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
217 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
218
219 #define I_SYMBOL_BINDING(node) \
220   (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->symbol_binding)
221 #define I_SYMBOL_DECL(node) \
222  (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
223
224 #define I_TAG_BINDING(node) \
225   (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->tag_binding)
226 #define I_TAG_DECL(node) \
227  (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
228
229 #define I_LABEL_BINDING(node) \
230   (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->label_binding)
231 #define I_LABEL_DECL(node) \
232  (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
233
234 /* Each C symbol points to three linked lists of c_binding structures.
235    These describe the values of the identifier in the three different
236    namespaces defined by the language.  */
237
238 struct lang_identifier GTY(())
239 {
240   struct c_common_identifier common_id;
241   struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
242   struct c_binding *tag_binding;    /* struct/union/enum tags */
243   struct c_binding *label_binding;  /* labels */
244 };
245
246 /* Validate c-lang.c's assumptions.  */
247 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
248 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
249
250 /* The resulting tree type.  */
251
252 union lang_tree_node
253   GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
254        chain_next ("TREE_CODE (&%h.generic) == INTEGER_TYPE ? (union lang_tree_node *) TYPE_NEXT_VARIANT (&%h.generic) : ((union lang_tree_node *) TREE_CHAIN (&%h.generic))")))
255 {
256   union tree_node GTY ((tag ("0"),
257                         desc ("tree_node_structure (&%h)")))
258     generic;
259   struct lang_identifier GTY ((tag ("1"))) identifier;
260 };
261
262 /* Each c_scope structure describes the complete contents of one
263    scope.  Four scopes are distinguished specially: the innermost or
264    current scope, the innermost function scope, the file scope (always
265    the second to outermost) and the outermost or external scope.
266
267    Most declarations are recorded in the current scope.
268
269    All normal label declarations are recorded in the innermost
270    function scope, as are bindings of undeclared identifiers to
271    error_mark_node.  (GCC permits nested functions as an extension,
272    hence the 'innermost' qualifier.)  Explicitly declared labels
273    (using the __label__ extension) appear in the current scope.
274
275    Being in the file scope (current_scope == file_scope) causes
276    special behavior in several places below.  Also, under some
277    conditions the Objective-C front end records declarations in the
278    file scope even though that isn't the current scope.
279
280    All declarations with external linkage are recorded in the external
281    scope, even if they aren't visible there; this models the fact that
282    such declarations are visible to the entire program, and (with a
283    bit of cleverness, see pushdecl) allows diagnosis of some violations
284    of C99 6.2.2p7 and 6.2.7p2:
285
286      If, within the same translation unit, the same identifier appears
287      with both internal and external linkage, the behavior is
288      undefined.
289
290      All declarations that refer to the same object or function shall
291      have compatible type; otherwise, the behavior is undefined.
292
293    Initially only the built-in declarations, which describe compiler
294    intrinsic functions plus a subset of the standard library, are in
295    this scope.
296
297    The order of the blocks list matters, and it is frequently appended
298    to.  To avoid having to walk all the way to the end of the list on
299    each insertion, or reverse the list later, we maintain a pointer to
300    the last list entry.  (FIXME: It should be feasible to use a reversed
301    list here.)
302
303    The bindings list is strictly in reverse order of declarations;
304    pop_scope relies on this.  */
305
306
307 struct c_scope GTY((chain_next ("%h.outer")))
308 {
309   /* The scope containing this one.  */
310   struct c_scope *outer;
311
312   /* The next outermost function scope.  */
313   struct c_scope *outer_function;
314
315   /* All bindings in this scope.  */
316   struct c_binding *bindings;
317
318   /* For each scope (except the global one), a chain of BLOCK nodes
319      for all the scopes that were entered and exited one level down.  */
320   tree blocks;
321   tree blocks_last;
322
323   /* The depth of this scope.  Used to keep the ->shadowed chain of
324      bindings sorted innermost to outermost.  */
325   unsigned int depth : 28;
326
327   /* True if we are currently filling this scope with parameter
328      declarations.  */
329   BOOL_BITFIELD parm_flag : 1;
330
331   /* True if we saw [*] in this scope.  Used to give an error messages
332      if these appears in a function definition.  */
333   BOOL_BITFIELD had_vla_unspec : 1;
334
335   /* True if we already complained about forward parameter decls
336      in this scope.  This prevents double warnings on
337      foo (int a; int b; ...)  */
338   BOOL_BITFIELD warned_forward_parm_decls : 1;
339
340   /* True if this is the outermost block scope of a function body.
341      This scope contains the parameters, the local variables declared
342      in the outermost block, and all the labels (except those in
343      nested functions, or declared at block scope with __label__).  */
344   BOOL_BITFIELD function_body : 1;
345
346   /* True means make a BLOCK for this scope no matter what.  */
347   BOOL_BITFIELD keep : 1;
348 };
349
350 /* The scope currently in effect.  */
351
352 static GTY(()) struct c_scope *current_scope;
353
354 /* The innermost function scope.  Ordinary (not explicitly declared)
355    labels, bindings to error_mark_node, and the lazily-created
356    bindings of __func__ and its friends get this scope.  */
357
358 static GTY(()) struct c_scope *current_function_scope;
359
360 /* The C file scope.  This is reset for each input translation unit.  */
361
362 static GTY(()) struct c_scope *file_scope;
363
364 /* The outermost scope.  This is used for all declarations with
365    external linkage, and only these, hence the name.  */
366
367 static GTY(()) struct c_scope *external_scope;
368
369 /* A chain of c_scope structures awaiting reuse.  */
370
371 static GTY((deletable)) struct c_scope *scope_freelist;
372
373 /* A chain of c_binding structures awaiting reuse.  */
374
375 static GTY((deletable)) struct c_binding *binding_freelist;
376
377 /* Append VAR to LIST in scope SCOPE.  */
378 #define SCOPE_LIST_APPEND(scope, list, decl) do {       \
379   struct c_scope *s_ = (scope);                         \
380   tree d_ = (decl);                                     \
381   if (s_->list##_last)                                  \
382     BLOCK_CHAIN (s_->list##_last) = d_;                 \
383   else                                                  \
384     s_->list = d_;                                      \
385   s_->list##_last = d_;                                 \
386 } while (0)
387
388 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE.  */
389 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do {        \
390   struct c_scope *t_ = (tscope);                                \
391   struct c_scope *f_ = (fscope);                                \
392   if (t_->to##_last)                                            \
393     BLOCK_CHAIN (t_->to##_last) = f_->from;                     \
394   else                                                          \
395     t_->to = f_->from;                                          \
396   t_->to##_last = f_->from##_last;                              \
397 } while (0)
398
399 /* True means unconditionally make a BLOCK for the next scope pushed.  */
400
401 static bool keep_next_level_flag;
402
403 /* True means the next call to push_scope will be the outermost scope
404    of a function body, so do not push a new scope, merely cease
405    expecting parameter decls.  */
406
407 static bool next_is_function_body;
408
409 /* Forward declarations.  */
410 static tree lookup_name_in_scope (tree, struct c_scope *);
411 static tree c_make_fname_decl (tree, int);
412 static tree grokdeclarator (const struct c_declarator *,
413                             struct c_declspecs *,
414                             enum decl_context, bool, tree *, tree *, tree *,
415                             bool *, enum deprecated_states);
416 static tree grokparms (struct c_arg_info *, bool);
417 static void layout_array_type (tree);
418 \f
419 /* T is a statement.  Add it to the statement-tree.  This is the
420    C/ObjC version--C++ has a slightly different version of this
421    function.  */
422
423 tree
424 add_stmt (tree t)
425 {
426   enum tree_code code = TREE_CODE (t);
427
428   if (CAN_HAVE_LOCATION_P (t) && code != LABEL_EXPR)
429     {
430       if (!EXPR_HAS_LOCATION (t))
431         SET_EXPR_LOCATION (t, input_location);
432     }
433
434   if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
435     STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
436
437   /* Add T to the statement-tree.  Non-side-effect statements need to be
438      recorded during statement expressions.  */
439   append_to_statement_list_force (t, &cur_stmt_list);
440
441   return t;
442 }
443 \f
444
445 void
446 c_print_identifier (FILE *file, tree node, int indent)
447 {
448   print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
449   print_node (file, "tag", I_TAG_DECL (node), indent + 4);
450   print_node (file, "label", I_LABEL_DECL (node), indent + 4);
451   if (C_IS_RESERVED_WORD (node) && C_RID_CODE (node) != RID_CXX_COMPAT_WARN)
452     {
453       tree rid = ridpointers[C_RID_CODE (node)];
454       indent_to (file, indent + 4);
455       fprintf (file, "rid %p \"%s\"",
456                (void *) rid, IDENTIFIER_POINTER (rid));
457     }
458 }
459
460 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
461    which may be any of several kinds of DECL or TYPE or error_mark_node,
462    in the scope SCOPE.  */
463 static void
464 bind (tree name, tree decl, struct c_scope *scope, bool invisible,
465       bool nested, location_t locus)
466 {
467   struct c_binding *b, **here;
468
469   if (binding_freelist)
470     {
471       b = binding_freelist;
472       binding_freelist = b->prev;
473     }
474   else
475     b = GGC_NEW (struct c_binding);
476
477   b->shadowed = 0;
478   b->decl = decl;
479   b->id = name;
480   b->depth = scope->depth;
481   b->invisible = invisible;
482   b->nested = nested;
483   b->inner_comp = 0;
484   b->locus = locus;
485
486   b->type = 0;
487
488   b->prev = scope->bindings;
489   scope->bindings = b;
490
491   if (!name)
492     return;
493
494   switch (TREE_CODE (decl))
495     {
496     case LABEL_DECL:     here = &I_LABEL_BINDING (name);   break;
497     case ENUMERAL_TYPE:
498     case UNION_TYPE:
499     case RECORD_TYPE:    here = &I_TAG_BINDING (name);     break;
500     case VAR_DECL:
501     case FUNCTION_DECL:
502     case TYPE_DECL:
503     case CONST_DECL:
504     case PARM_DECL:
505     case ERROR_MARK:     here = &I_SYMBOL_BINDING (name);  break;
506
507     default:
508       gcc_unreachable ();
509     }
510
511   /* Locate the appropriate place in the chain of shadowed decls
512      to insert this binding.  Normally, scope == current_scope and
513      this does nothing.  */
514   while (*here && (*here)->depth > scope->depth)
515     here = &(*here)->shadowed;
516
517   b->shadowed = *here;
518   *here = b;
519 }
520
521 /* Clear the binding structure B, stick it on the binding_freelist,
522    and return the former value of b->prev.  This is used by pop_scope
523    and get_parm_info to iterate destructively over all the bindings
524    from a given scope.  */
525 static struct c_binding *
526 free_binding_and_advance (struct c_binding *b)
527 {
528   struct c_binding *prev = b->prev;
529
530   memset (b, 0, sizeof (struct c_binding));
531   b->prev = binding_freelist;
532   binding_freelist = b;
533
534   return prev;
535 }
536
537 \f
538 /* Hook called at end of compilation to assume 1 elt
539    for a file-scope tentative array defn that wasn't complete before.  */
540
541 void
542 c_finish_incomplete_decl (tree decl)
543 {
544   if (TREE_CODE (decl) == VAR_DECL)
545     {
546       tree type = TREE_TYPE (decl);
547       if (type != error_mark_node
548           && TREE_CODE (type) == ARRAY_TYPE
549           && !DECL_EXTERNAL (decl)
550           && TYPE_DOMAIN (type) == 0)
551         {
552           warning (0, "array %q+D assumed to have one element", decl);
553
554           complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
555
556           layout_decl (decl, 0);
557         }
558     }
559 }
560 \f
561 /* The Objective-C front-end often needs to determine the current scope.  */
562
563 void *
564 objc_get_current_scope (void)
565 {
566   return current_scope;
567 }
568
569 /* The following function is used only by Objective-C.  It needs to live here
570    because it accesses the innards of c_scope.  */
571
572 void
573 objc_mark_locals_volatile (void *enclosing_blk)
574 {
575   struct c_scope *scope;
576   struct c_binding *b;
577
578   for (scope = current_scope;
579        scope && scope != enclosing_blk;
580        scope = scope->outer)
581     {
582       for (b = scope->bindings; b; b = b->prev)
583         objc_volatilize_decl (b->decl);
584
585       /* Do not climb up past the current function.  */
586       if (scope->function_body)
587         break;
588     }
589 }
590
591 /* Nonzero if we are currently in file scope.  */
592
593 int
594 global_bindings_p (void)
595 {
596   return current_scope == file_scope && !c_override_global_bindings_to_false;
597 }
598
599 void
600 keep_next_level (void)
601 {
602   keep_next_level_flag = true;
603 }
604
605 /* Identify this scope as currently being filled with parameters.  */
606
607 void
608 declare_parm_level (void)
609 {
610   current_scope->parm_flag = true;
611 }
612
613 void
614 push_scope (void)
615 {
616   if (next_is_function_body)
617     {
618       /* This is the transition from the parameters to the top level
619          of the function body.  These are the same scope
620          (C99 6.2.1p4,6) so we do not push another scope structure.
621          next_is_function_body is set only by store_parm_decls, which
622          in turn is called when and only when we are about to
623          encounter the opening curly brace for the function body.
624
625          The outermost block of a function always gets a BLOCK node,
626          because the debugging output routines expect that each
627          function has at least one BLOCK.  */
628       current_scope->parm_flag         = false;
629       current_scope->function_body     = true;
630       current_scope->keep              = true;
631       current_scope->outer_function    = current_function_scope;
632       current_function_scope           = current_scope;
633
634       keep_next_level_flag = false;
635       next_is_function_body = false;
636     }
637   else
638     {
639       struct c_scope *scope;
640       if (scope_freelist)
641         {
642           scope = scope_freelist;
643           scope_freelist = scope->outer;
644         }
645       else
646         scope = GGC_CNEW (struct c_scope);
647
648       scope->keep          = keep_next_level_flag;
649       scope->outer         = current_scope;
650       scope->depth         = current_scope ? (current_scope->depth + 1) : 0;
651
652       /* Check for scope depth overflow.  Unlikely (2^28 == 268,435,456) but
653          possible.  */
654       if (current_scope && scope->depth == 0)
655         {
656           scope->depth--;
657           sorry ("GCC supports only %u nested scopes", scope->depth);
658         }
659
660       current_scope        = scope;
661       keep_next_level_flag = false;
662     }
663 }
664
665 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT.  */
666
667 static void
668 set_type_context (tree type, tree context)
669 {
670   for (type = TYPE_MAIN_VARIANT (type); type;
671        type = TYPE_NEXT_VARIANT (type))
672     TYPE_CONTEXT (type) = context;
673 }
674
675 /* Exit a scope.  Restore the state of the identifier-decl mappings
676    that were in effect when this scope was entered.  Return a BLOCK
677    node containing all the DECLs in this scope that are of interest
678    to debug info generation.  */
679
680 tree
681 pop_scope (void)
682 {
683   struct c_scope *scope = current_scope;
684   tree block, context, p;
685   struct c_binding *b;
686
687   bool functionbody = scope->function_body;
688   bool keep = functionbody || scope->keep || scope->bindings;
689
690   c_end_vm_scope (scope->depth);
691
692   /* If appropriate, create a BLOCK to record the decls for the life
693      of this function.  */
694   block = 0;
695   if (keep)
696     {
697       block = make_node (BLOCK);
698       BLOCK_SUBBLOCKS (block) = scope->blocks;
699       TREE_USED (block) = 1;
700
701       /* In each subblock, record that this is its superior.  */
702       for (p = scope->blocks; p; p = BLOCK_CHAIN (p))
703         BLOCK_SUPERCONTEXT (p) = block;
704
705       BLOCK_VARS (block) = 0;
706     }
707
708   /* The TYPE_CONTEXTs for all of the tagged types belonging to this
709      scope must be set so that they point to the appropriate
710      construct, i.e.  either to the current FUNCTION_DECL node, or
711      else to the BLOCK node we just constructed.
712
713      Note that for tagged types whose scope is just the formal
714      parameter list for some function type specification, we can't
715      properly set their TYPE_CONTEXTs here, because we don't have a
716      pointer to the appropriate FUNCTION_TYPE node readily available
717      to us.  For those cases, the TYPE_CONTEXTs of the relevant tagged
718      type nodes get set in `grokdeclarator' as soon as we have created
719      the FUNCTION_TYPE node which will represent the "scope" for these
720      "parameter list local" tagged types.  */
721   if (scope->function_body)
722     context = current_function_decl;
723   else if (scope == file_scope)
724     {
725       tree file_decl = build_decl (TRANSLATION_UNIT_DECL, 0, 0);
726       TREE_CHAIN (file_decl) = all_translation_units;
727       all_translation_units = file_decl;
728       context = file_decl;
729     }
730   else
731     context = block;
732
733   /* Clear all bindings in this scope.  */
734   for (b = scope->bindings; b; b = free_binding_and_advance (b))
735     {
736       p = b->decl;
737       switch (TREE_CODE (p))
738         {
739         case LABEL_DECL:
740           /* Warnings for unused labels, errors for undefined labels.  */
741           if (TREE_USED (p) && !DECL_INITIAL (p))
742             {
743               error ("label %q+D used but not defined", p);
744               DECL_INITIAL (p) = error_mark_node;
745             }
746           else 
747             warn_for_unused_label (p);
748
749           /* Labels go in BLOCK_VARS.  */
750           TREE_CHAIN (p) = BLOCK_VARS (block);
751           BLOCK_VARS (block) = p;
752           gcc_assert (I_LABEL_BINDING (b->id) == b);
753           I_LABEL_BINDING (b->id) = b->shadowed;
754           break;
755
756         case ENUMERAL_TYPE:
757         case UNION_TYPE:
758         case RECORD_TYPE:
759           set_type_context (p, context);
760
761           /* Types may not have tag-names, in which case the type
762              appears in the bindings list with b->id NULL.  */
763           if (b->id)
764             {
765               gcc_assert (I_TAG_BINDING (b->id) == b);
766               I_TAG_BINDING (b->id) = b->shadowed;
767             }
768           break;
769
770         case FUNCTION_DECL:
771           /* Propagate TREE_ADDRESSABLE from nested functions to their
772              containing functions.  */
773           if (!TREE_ASM_WRITTEN (p)
774               && DECL_INITIAL (p) != 0
775               && TREE_ADDRESSABLE (p)
776               && DECL_ABSTRACT_ORIGIN (p) != 0
777               && DECL_ABSTRACT_ORIGIN (p) != p)
778             TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
779           if (!DECL_EXTERNAL (p)
780               && !DECL_INITIAL (p)
781               && scope != file_scope
782               && scope != external_scope)
783             {
784               error ("nested function %q+D declared but never defined", p);
785               undef_nested_function = true;
786             }
787           else if (DECL_DECLARED_INLINE_P (p)
788                    && TREE_PUBLIC (p)
789                    && !DECL_INITIAL (p))
790             {
791               /* C99 6.7.4p6: "a function with external linkage... declared
792                  with an inline function specifier ... shall also be defined
793                  in the same translation unit."  */
794               if (!flag_gnu89_inline)
795                 pedwarn (input_location, 0,
796                          "inline function %q+D declared but never defined", p);
797               DECL_EXTERNAL (p) = 1;
798             }
799
800           goto common_symbol;
801
802         case VAR_DECL:
803           /* Warnings for unused variables.  */
804           if (!TREE_USED (p)
805               && !TREE_NO_WARNING (p)
806               && !DECL_IN_SYSTEM_HEADER (p)
807               && DECL_NAME (p)
808               && !DECL_ARTIFICIAL (p)
809               && scope != file_scope
810               && scope != external_scope)
811             warning (OPT_Wunused_variable, "unused variable %q+D", p);
812
813           if (b->inner_comp)
814             {
815               error ("type of array %q+D completed incompatibly with"
816                      " implicit initialization", p);
817             }
818
819           /* Fall through.  */
820         case TYPE_DECL:
821         case CONST_DECL:
822         common_symbol:
823           /* All of these go in BLOCK_VARS, but only if this is the
824              binding in the home scope.  */
825           if (!b->nested)
826             {
827               TREE_CHAIN (p) = BLOCK_VARS (block);
828               BLOCK_VARS (block) = p;
829             }
830           else if (VAR_OR_FUNCTION_DECL_P (p))
831             {
832               /* For block local externs add a special
833                  DECL_EXTERNAL decl for debug info generation.  */
834               tree extp = copy_node (p);
835
836               DECL_EXTERNAL (extp) = 1;
837               TREE_STATIC (extp) = 0;
838               TREE_PUBLIC (extp) = 1;
839               DECL_INITIAL (extp) = NULL_TREE;
840               DECL_LANG_SPECIFIC (extp) = NULL;
841               DECL_CONTEXT (extp) = current_function_decl;
842               if (TREE_CODE (p) == FUNCTION_DECL)
843                 {
844                   DECL_RESULT (extp) = NULL_TREE;
845                   DECL_SAVED_TREE (extp) = NULL_TREE;
846                   DECL_STRUCT_FUNCTION (extp) = NULL;
847                 }
848               if (b->locus != UNKNOWN_LOCATION)
849                 DECL_SOURCE_LOCATION (extp) = b->locus;
850               TREE_CHAIN (extp) = BLOCK_VARS (block);
851               BLOCK_VARS (block) = extp;
852             }
853           /* If this is the file scope, and we are processing more
854              than one translation unit in this compilation, set
855              DECL_CONTEXT of each decl to the TRANSLATION_UNIT_DECL.
856              This makes same_translation_unit_p work, and causes
857              static declarations to be given disambiguating suffixes.  */
858           if (scope == file_scope && num_in_fnames > 1)
859             {
860               DECL_CONTEXT (p) = context;
861               if (TREE_CODE (p) == TYPE_DECL)
862                 set_type_context (TREE_TYPE (p), context);
863             }
864
865           /* Fall through.  */
866           /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
867              already been put there by store_parm_decls.  Unused-
868              parameter warnings are handled by function.c.
869              error_mark_node obviously does not go in BLOCK_VARS and
870              does not get unused-variable warnings.  */
871         case PARM_DECL:
872         case ERROR_MARK:
873           /* It is possible for a decl not to have a name.  We get
874              here with b->id NULL in this case.  */
875           if (b->id)
876             {
877               gcc_assert (I_SYMBOL_BINDING (b->id) == b);
878               I_SYMBOL_BINDING (b->id) = b->shadowed;
879               if (b->shadowed && b->shadowed->type)
880                 TREE_TYPE (b->shadowed->decl) = b->shadowed->type;
881             }
882           break;
883
884         default:
885           gcc_unreachable ();
886         }
887     }
888
889
890   /* Dispose of the block that we just made inside some higher level.  */
891   if ((scope->function_body || scope == file_scope) && context)
892     {
893       DECL_INITIAL (context) = block;
894       BLOCK_SUPERCONTEXT (block) = context;
895     }
896   else if (scope->outer)
897     {
898       if (block)
899         SCOPE_LIST_APPEND (scope->outer, blocks, block);
900       /* If we did not make a block for the scope just exited, any
901          blocks made for inner scopes must be carried forward so they
902          will later become subblocks of something else.  */
903       else if (scope->blocks)
904         SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
905     }
906
907   /* Pop the current scope, and free the structure for reuse.  */
908   current_scope = scope->outer;
909   if (scope->function_body)
910     current_function_scope = scope->outer_function;
911
912   memset (scope, 0, sizeof (struct c_scope));
913   scope->outer = scope_freelist;
914   scope_freelist = scope;
915
916   return block;
917 }
918
919 void
920 push_file_scope (void)
921 {
922   tree decl;
923
924   if (file_scope)
925     return;
926
927   push_scope ();
928   file_scope = current_scope;
929
930   start_fname_decls ();
931
932   for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl))
933     bind (DECL_NAME (decl), decl, file_scope,
934           /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl));
935 }
936
937 void
938 pop_file_scope (void)
939 {
940   /* In case there were missing closebraces, get us back to the global
941      binding level.  */
942   while (current_scope != file_scope)
943     pop_scope ();
944
945   /* __FUNCTION__ is defined at file scope ("").  This
946      call may not be necessary as my tests indicate it
947      still works without it.  */
948   finish_fname_decls ();
949
950   /* This is the point to write out a PCH if we're doing that.
951      In that case we do not want to do anything else.  */
952   if (pch_file)
953     {
954       c_common_write_pch ();
955       return;
956     }
957
958   /* Pop off the file scope and close this translation unit.  */
959   pop_scope ();
960   file_scope = 0;
961
962   maybe_apply_pending_pragma_weaks ();
963   cgraph_finalize_compilation_unit ();
964 }
965
966 \f
967 /* Push a definition or a declaration of struct, union or enum tag "name".
968    "type" should be the type node.
969    We assume that the tag "name" is not already defined.
970
971    Note that the definition may really be just a forward reference.
972    In that case, the TYPE_SIZE will be zero.  */
973
974 static void
975 pushtag (tree name, tree type)
976 {
977   /* Record the identifier as the type's name if it has none.  */
978   if (name && !TYPE_NAME (type))
979     TYPE_NAME (type) = name;
980   bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false,
981         UNKNOWN_LOCATION);
982
983   /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
984      tagged type we just added to the current scope.  This fake
985      NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
986      to output a representation of a tagged type, and it also gives
987      us a convenient place to record the "scope start" address for the
988      tagged type.  */
989
990   TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
991
992   /* An approximation for now, so we can tell this is a function-scope tag.
993      This will be updated in pop_scope.  */
994   TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
995 }
996 \f
997 /* Subroutine of compare_decls.  Allow harmless mismatches in return
998    and argument types provided that the type modes match.  This function
999    return a unified type given a suitable match, and 0 otherwise.  */
1000
1001 static tree
1002 match_builtin_function_types (tree newtype, tree oldtype)
1003 {
1004   tree newrettype, oldrettype;
1005   tree newargs, oldargs;
1006   tree trytype, tryargs;
1007
1008   /* Accept the return type of the new declaration if same modes.  */
1009   oldrettype = TREE_TYPE (oldtype);
1010   newrettype = TREE_TYPE (newtype);
1011
1012   if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
1013     return 0;
1014
1015   oldargs = TYPE_ARG_TYPES (oldtype);
1016   newargs = TYPE_ARG_TYPES (newtype);
1017   tryargs = newargs;
1018
1019   while (oldargs || newargs)
1020     {
1021       if (!oldargs
1022           || !newargs
1023           || !TREE_VALUE (oldargs)
1024           || !TREE_VALUE (newargs)
1025           || TYPE_MODE (TREE_VALUE (oldargs))
1026              != TYPE_MODE (TREE_VALUE (newargs)))
1027         return 0;
1028
1029       oldargs = TREE_CHAIN (oldargs);
1030       newargs = TREE_CHAIN (newargs);
1031     }
1032
1033   trytype = build_function_type (newrettype, tryargs);
1034   return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
1035 }
1036
1037 /* Subroutine of diagnose_mismatched_decls.  Check for function type
1038    mismatch involving an empty arglist vs a nonempty one and give clearer
1039    diagnostics.  */
1040 static void
1041 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1042                            tree newtype, tree oldtype)
1043 {
1044   tree t;
1045
1046   if (TREE_CODE (olddecl) != FUNCTION_DECL
1047       || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1048       || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
1049            ||
1050            (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
1051     return;
1052
1053   t = TYPE_ARG_TYPES (oldtype);
1054   if (t == 0)
1055     t = TYPE_ARG_TYPES (newtype);
1056   for (; t; t = TREE_CHAIN (t))
1057     {
1058       tree type = TREE_VALUE (t);
1059
1060       if (TREE_CHAIN (t) == 0
1061           && TYPE_MAIN_VARIANT (type) != void_type_node)
1062         {
1063           inform (input_location, "a parameter list with an ellipsis can%'t match "
1064                   "an empty parameter name list declaration");
1065           break;
1066         }
1067
1068       if (c_type_promotes_to (type) != type)
1069         {
1070           inform (input_location, "an argument type that has a default promotion can%'t match "
1071                   "an empty parameter name list declaration");
1072           break;
1073         }
1074     }
1075 }
1076
1077 /* Another subroutine of diagnose_mismatched_decls.  OLDDECL is an
1078    old-style function definition, NEWDECL is a prototype declaration.
1079    Diagnose inconsistencies in the argument list.  Returns TRUE if
1080    the prototype is compatible, FALSE if not.  */
1081 static bool
1082 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1083 {
1084   tree newargs, oldargs;
1085   int i;
1086
1087 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1088
1089   oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1090   newargs = TYPE_ARG_TYPES (newtype);
1091   i = 1;
1092
1093   for (;;)
1094     {
1095       tree oldargtype = TREE_VALUE (oldargs);
1096       tree newargtype = TREE_VALUE (newargs);
1097
1098       if (oldargtype == error_mark_node || newargtype == error_mark_node)
1099         return false;
1100
1101       oldargtype = TYPE_MAIN_VARIANT (oldargtype);
1102       newargtype = TYPE_MAIN_VARIANT (newargtype);
1103
1104       if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1105         break;
1106
1107       /* Reaching the end of just one list means the two decls don't
1108          agree on the number of arguments.  */
1109       if (END_OF_ARGLIST (oldargtype))
1110         {
1111           error ("prototype for %q+D declares more arguments "
1112                  "than previous old-style definition", newdecl);
1113           return false;
1114         }
1115       else if (END_OF_ARGLIST (newargtype))
1116         {
1117           error ("prototype for %q+D declares fewer arguments "
1118                  "than previous old-style definition", newdecl);
1119           return false;
1120         }
1121
1122       /* Type for passing arg must be consistent with that declared
1123          for the arg.  */
1124       else if (!comptypes (oldargtype, newargtype))
1125         {
1126           error ("prototype for %q+D declares argument %d"
1127                  " with incompatible type",
1128                  newdecl, i);
1129           return false;
1130         }
1131
1132       oldargs = TREE_CHAIN (oldargs);
1133       newargs = TREE_CHAIN (newargs);
1134       i++;
1135     }
1136
1137   /* If we get here, no errors were found, but do issue a warning
1138      for this poor-style construct.  */
1139   warning (0, "prototype for %q+D follows non-prototype definition",
1140            newdecl);
1141   return true;
1142 #undef END_OF_ARGLIST
1143 }
1144
1145 /* Subroutine of diagnose_mismatched_decls.  Report the location of DECL,
1146    first in a pair of mismatched declarations, using the diagnostic
1147    function DIAG.  */
1148 static void
1149 locate_old_decl (tree decl)
1150 {
1151   if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1152     ;
1153   else if (DECL_INITIAL (decl))
1154     inform (input_location, "previous definition of %q+D was here", decl);
1155   else if (C_DECL_IMPLICIT (decl))
1156     inform (input_location, "previous implicit declaration of %q+D was here", decl);
1157   else
1158     inform (input_location, "previous declaration of %q+D was here", decl);
1159 }
1160
1161 /* Subroutine of duplicate_decls.  Compare NEWDECL to OLDDECL.
1162    Returns true if the caller should proceed to merge the two, false
1163    if OLDDECL should simply be discarded.  As a side effect, issues
1164    all necessary diagnostics for invalid or poor-style combinations.
1165    If it returns true, writes the types of NEWDECL and OLDDECL to
1166    *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1167    TREE_TYPE (NEWDECL, OLDDECL) respectively.  */
1168
1169 static bool
1170 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1171                            tree *newtypep, tree *oldtypep)
1172 {
1173   tree newtype, oldtype;
1174   bool pedwarned = false;
1175   bool warned = false;
1176   bool retval = true;
1177
1178 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL)  \
1179                                   && DECL_EXTERNAL (DECL))
1180
1181   /* If we have error_mark_node for either decl or type, just discard
1182      the previous decl - we're in an error cascade already.  */
1183   if (olddecl == error_mark_node || newdecl == error_mark_node)
1184     return false;
1185   *oldtypep = oldtype = TREE_TYPE (olddecl);
1186   *newtypep = newtype = TREE_TYPE (newdecl);
1187   if (oldtype == error_mark_node || newtype == error_mark_node)
1188     return false;
1189
1190   /* Two different categories of symbol altogether.  This is an error
1191      unless OLDDECL is a builtin.  OLDDECL will be discarded in any case.  */
1192   if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1193     {
1194       if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1195             && DECL_BUILT_IN (olddecl)
1196             && !C_DECL_DECLARED_BUILTIN (olddecl)))
1197         {
1198           error ("%q+D redeclared as different kind of symbol", newdecl);
1199           locate_old_decl (olddecl);
1200         }
1201       else if (TREE_PUBLIC (newdecl))
1202         warning (0, "built-in function %q+D declared as non-function",
1203                  newdecl);
1204       else
1205         warning (OPT_Wshadow, "declaration of %q+D shadows "
1206                  "a built-in function", newdecl);
1207       return false;
1208     }
1209
1210   /* Enumerators have no linkage, so may only be declared once in a
1211      given scope.  */
1212   if (TREE_CODE (olddecl) == CONST_DECL)
1213     {
1214       error ("redeclaration of enumerator %q+D", newdecl);
1215       locate_old_decl (olddecl);
1216       return false;
1217     }
1218
1219   if (!comptypes (oldtype, newtype))
1220     {
1221       if (TREE_CODE (olddecl) == FUNCTION_DECL
1222           && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1223         {
1224           /* Accept harmless mismatch in function types.
1225              This is for the ffs and fprintf builtins.  */
1226           tree trytype = match_builtin_function_types (newtype, oldtype);
1227
1228           if (trytype && comptypes (newtype, trytype))
1229             *oldtypep = oldtype = trytype;
1230           else
1231             {
1232               /* If types don't match for a built-in, throw away the
1233                  built-in.  No point in calling locate_old_decl here, it
1234                  won't print anything.  */
1235               warning (0, "conflicting types for built-in function %q+D",
1236                        newdecl);
1237               return false;
1238             }
1239         }
1240       else if (TREE_CODE (olddecl) == FUNCTION_DECL
1241                && DECL_IS_BUILTIN (olddecl))
1242         {
1243           /* A conflicting function declaration for a predeclared
1244              function that isn't actually built in.  Objective C uses
1245              these.  The new declaration silently overrides everything
1246              but the volatility (i.e. noreturn) indication.  See also
1247              below.  FIXME: Make Objective C use normal builtins.  */
1248           TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1249           return false;
1250         }
1251       /* Permit void foo (...) to match int foo (...) if the latter is
1252          the definition and implicit int was used.  See
1253          c-torture/compile/920625-2.c.  */
1254       else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1255                && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1256                && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1257                && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1258         {
1259           pedwarned = pedwarn (input_location, 0,
1260                                "conflicting types for %q+D", newdecl);
1261           /* Make sure we keep void as the return type.  */
1262           TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1263           C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1264         }
1265       /* Permit void foo (...) to match an earlier call to foo (...) with
1266          no declared type (thus, implicitly int).  */
1267       else if (TREE_CODE (newdecl) == FUNCTION_DECL
1268                && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1269                && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1270                && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1271         {
1272           pedwarned = pedwarn (input_location, 0,
1273                                "conflicting types for %q+D", newdecl);
1274           /* Make sure we keep void as the return type.  */
1275           TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1276         }
1277       else
1278         {
1279           if (TYPE_QUALS (newtype) != TYPE_QUALS (oldtype))
1280             error ("conflicting type qualifiers for %q+D", newdecl);
1281           else
1282             error ("conflicting types for %q+D", newdecl);
1283           diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1284           locate_old_decl (olddecl);
1285           return false;
1286         }
1287     }
1288
1289   /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1290      but silently ignore the redeclaration if either is in a system
1291      header.  (Conflicting redeclarations were handled above.)  */
1292   if (TREE_CODE (newdecl) == TYPE_DECL)
1293     {
1294       if (DECL_IN_SYSTEM_HEADER (newdecl)
1295           || DECL_IN_SYSTEM_HEADER (olddecl)
1296           || TREE_NO_WARNING (newdecl)
1297           || TREE_NO_WARNING (olddecl))
1298         return true;  /* Allow OLDDECL to continue in use.  */
1299
1300       error ("redefinition of typedef %q+D", newdecl);
1301       locate_old_decl (olddecl);
1302       return false;
1303     }
1304
1305   /* Function declarations can either be 'static' or 'extern' (no
1306      qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1307      can never conflict with each other on account of linkage
1308      (6.2.2p4).  Multiple definitions are not allowed (6.9p3,5) but
1309      gnu89 mode permits two definitions if one is 'extern inline' and
1310      one is not.  The non- extern-inline definition supersedes the
1311      extern-inline definition.  */
1312
1313   else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1314     {
1315       /* If you declare a built-in function name as static, or
1316          define the built-in with an old-style definition (so we
1317          can't validate the argument list) the built-in definition is
1318          overridden, but optionally warn this was a bad choice of name.  */
1319       if (DECL_BUILT_IN (olddecl)
1320           && !C_DECL_DECLARED_BUILTIN (olddecl)
1321           && (!TREE_PUBLIC (newdecl)
1322               || (DECL_INITIAL (newdecl)
1323                   && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1324         {
1325           warning (OPT_Wshadow, "declaration of %q+D shadows "
1326                    "a built-in function", newdecl);
1327           /* Discard the old built-in function.  */
1328           return false;
1329         }
1330
1331       if (DECL_INITIAL (newdecl))
1332         {
1333           if (DECL_INITIAL (olddecl))
1334             {
1335               /* If both decls are in the same TU and the new declaration
1336                  isn't overriding an extern inline reject the new decl.
1337                  In c99, no overriding is allowed in the same translation
1338                  unit.  */
1339               if ((!DECL_EXTERN_INLINE (olddecl)
1340                    || DECL_EXTERN_INLINE (newdecl)
1341                    || (!flag_gnu89_inline
1342                        && (!DECL_DECLARED_INLINE_P (olddecl)
1343                            || !lookup_attribute ("gnu_inline",
1344                                                  DECL_ATTRIBUTES (olddecl)))
1345                        && (!DECL_DECLARED_INLINE_P (newdecl)
1346                            || !lookup_attribute ("gnu_inline",
1347                                                  DECL_ATTRIBUTES (newdecl))))
1348                   )
1349                   && same_translation_unit_p (newdecl, olddecl))
1350                 {
1351                   error ("redefinition of %q+D", newdecl);
1352                   locate_old_decl (olddecl);
1353                   return false;
1354                 }
1355             }
1356         }
1357       /* If we have a prototype after an old-style function definition,
1358          the argument types must be checked specially.  */
1359       else if (DECL_INITIAL (olddecl)
1360                && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1361                && TYPE_ACTUAL_ARG_TYPES (oldtype)
1362                && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1363         {
1364           locate_old_decl (olddecl);
1365           return false;
1366         }
1367       /* A non-static declaration (even an "extern") followed by a
1368          static declaration is undefined behavior per C99 6.2.2p3-5,7.
1369          The same is true for a static forward declaration at block
1370          scope followed by a non-static declaration/definition at file
1371          scope.  Static followed by non-static at the same scope is
1372          not undefined behavior, and is the most convenient way to get
1373          some effects (see e.g.  what unwind-dw2-fde-glibc.c does to
1374          the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1375          we do diagnose it if -Wtraditional.  */
1376       if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1377         {
1378           /* Two exceptions to the rule.  If olddecl is an extern
1379              inline, or a predeclared function that isn't actually
1380              built in, newdecl silently overrides olddecl.  The latter
1381              occur only in Objective C; see also above.  (FIXME: Make
1382              Objective C use normal builtins.)  */
1383           if (!DECL_IS_BUILTIN (olddecl)
1384               && !DECL_EXTERN_INLINE (olddecl))
1385             {
1386               error ("static declaration of %q+D follows "
1387                      "non-static declaration", newdecl);
1388               locate_old_decl (olddecl);
1389             }
1390           return false;
1391         }
1392       else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
1393         {
1394           if (DECL_CONTEXT (olddecl))
1395             {
1396               error ("non-static declaration of %q+D follows "
1397                      "static declaration", newdecl);
1398               locate_old_decl (olddecl);
1399               return false;
1400             }
1401           else if (warn_traditional)
1402             {
1403               warned |= warning (OPT_Wtraditional, 
1404                                  "non-static declaration of %q+D "
1405                                  "follows static declaration", newdecl);
1406             }
1407         }
1408
1409       /* Make sure gnu_inline attribute is either not present, or
1410          present on all inline decls.  */
1411       if (DECL_DECLARED_INLINE_P (olddecl)
1412           && DECL_DECLARED_INLINE_P (newdecl))
1413         {
1414           bool newa = lookup_attribute ("gnu_inline",
1415                                         DECL_ATTRIBUTES (newdecl)) != NULL;
1416           bool olda = lookup_attribute ("gnu_inline",
1417                                         DECL_ATTRIBUTES (olddecl)) != NULL;
1418           if (newa != olda)
1419             {
1420               error ("%<gnu_inline%> attribute present on %q+D",
1421                      newa ? newdecl : olddecl);
1422               error ("%Jbut not here", newa ? olddecl : newdecl);
1423             }
1424         }
1425     }
1426   else if (TREE_CODE (newdecl) == VAR_DECL)
1427     {
1428       /* Only variables can be thread-local, and all declarations must
1429          agree on this property.  */
1430       if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
1431         {
1432           /* Nothing to check.  Since OLDDECL is marked threadprivate
1433              and NEWDECL does not have a thread-local attribute, we
1434              will merge the threadprivate attribute into NEWDECL.  */
1435           ;
1436         }
1437       else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
1438         {
1439           if (DECL_THREAD_LOCAL_P (newdecl))
1440             error ("thread-local declaration of %q+D follows "
1441                    "non-thread-local declaration", newdecl);
1442           else
1443             error ("non-thread-local declaration of %q+D follows "
1444                    "thread-local declaration", newdecl);
1445
1446           locate_old_decl (olddecl);
1447           return false;
1448         }
1449
1450       /* Multiple initialized definitions are not allowed (6.9p3,5).  */
1451       if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1452         {
1453           error ("redefinition of %q+D", newdecl);
1454           locate_old_decl (olddecl);
1455           return false;
1456         }
1457
1458       /* Objects declared at file scope: if the first declaration had
1459          external linkage (even if it was an external reference) the
1460          second must have external linkage as well, or the behavior is
1461          undefined.  If the first declaration had internal linkage, then
1462          the second must too, or else be an external reference (in which
1463          case the composite declaration still has internal linkage).
1464          As for function declarations, we warn about the static-then-
1465          extern case only for -Wtraditional.  See generally 6.2.2p3-5,7.  */
1466       if (DECL_FILE_SCOPE_P (newdecl)
1467           && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1468         {
1469           if (DECL_EXTERNAL (newdecl))
1470             {
1471               if (!DECL_FILE_SCOPE_P (olddecl))
1472                 {
1473                   error ("extern declaration of %q+D follows "
1474                          "declaration with no linkage", newdecl);
1475                   locate_old_decl (olddecl);
1476                   return false;
1477                 }
1478               else if (warn_traditional)
1479                 {
1480                   warned |= warning (OPT_Wtraditional, 
1481                                      "non-static declaration of %q+D "
1482                                      "follows static declaration", newdecl);
1483                 }
1484             }
1485           else
1486             {
1487               if (TREE_PUBLIC (newdecl))
1488                 error ("non-static declaration of %q+D follows "
1489                        "static declaration", newdecl);
1490               else
1491                 error ("static declaration of %q+D follows "
1492                        "non-static declaration", newdecl);
1493
1494               locate_old_decl (olddecl);
1495               return false;
1496             }
1497         }
1498       /* Two objects with the same name declared at the same block
1499          scope must both be external references (6.7p3).  */
1500       else if (!DECL_FILE_SCOPE_P (newdecl))
1501         {
1502           if (DECL_EXTERNAL (newdecl))
1503             {
1504               /* Extern with initializer at block scope, which will
1505                  already have received an error.  */
1506             }
1507           else if (DECL_EXTERNAL (olddecl))
1508             {
1509               error ("declaration of %q+D with no linkage follows "
1510                      "extern declaration", newdecl);
1511               locate_old_decl (olddecl);
1512             }
1513           else
1514             {
1515               error ("redeclaration of %q+D with no linkage", newdecl);
1516               locate_old_decl (olddecl);
1517             }
1518
1519           return false;
1520         }
1521     }
1522
1523   /* warnings */
1524   /* All decls must agree on a visibility.  */
1525   if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
1526       && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
1527       && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1528     {
1529       warned |= warning (0, "redeclaration of %q+D with different visibility "
1530                          "(old visibility preserved)", newdecl);
1531     }
1532
1533   if (TREE_CODE (newdecl) == FUNCTION_DECL)
1534     {
1535       /* Diagnose inline __attribute__ ((noinline)) which is silly.  */
1536       if (DECL_DECLARED_INLINE_P (newdecl)
1537           && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1538         {
1539           warned |= warning (OPT_Wattributes, 
1540                              "inline declaration of %qD follows "
1541                              "declaration with attribute noinline", newdecl);
1542         }
1543       else if (DECL_DECLARED_INLINE_P (olddecl)
1544                && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1545         {
1546           warned |= warning (OPT_Wattributes, 
1547                              "declaration of %q+D with attribute "
1548                              "noinline follows inline declaration ", newdecl);
1549         }
1550     }
1551   else /* PARM_DECL, VAR_DECL */
1552     {
1553       /* Redeclaration of a parameter is a constraint violation (this is
1554          not explicitly stated, but follows from C99 6.7p3 [no more than
1555          one declaration of the same identifier with no linkage in the
1556          same scope, except type tags] and 6.2.2p6 [parameters have no
1557          linkage]).  We must check for a forward parameter declaration,
1558          indicated by TREE_ASM_WRITTEN on the old declaration - this is
1559          an extension, the mandatory diagnostic for which is handled by
1560          mark_forward_parm_decls.  */
1561
1562       if (TREE_CODE (newdecl) == PARM_DECL
1563           && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1564         {
1565           error ("redefinition of parameter %q+D", newdecl);
1566           locate_old_decl (olddecl);
1567           return false;
1568         }
1569     }
1570
1571   /* Optional warning for completely redundant decls.  */
1572   if (!warned && !pedwarned
1573       && warn_redundant_decls
1574       /* Don't warn about a function declaration followed by a
1575          definition.  */
1576       && !(TREE_CODE (newdecl) == FUNCTION_DECL
1577            && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1578       /* Don't warn about redundant redeclarations of builtins.  */
1579       && !(TREE_CODE (newdecl) == FUNCTION_DECL
1580            && !DECL_BUILT_IN (newdecl)
1581            && DECL_BUILT_IN (olddecl)
1582            && !C_DECL_DECLARED_BUILTIN (olddecl))
1583       /* Don't warn about an extern followed by a definition.  */
1584       && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1585       /* Don't warn about forward parameter decls.  */
1586       && !(TREE_CODE (newdecl) == PARM_DECL
1587            && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1588       /* Don't warn about a variable definition following a declaration.  */
1589       && !(TREE_CODE (newdecl) == VAR_DECL
1590            && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
1591     {
1592       warned = warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
1593                         newdecl);
1594     }
1595
1596   /* Report location of previous decl/defn.  */
1597   if (warned || pedwarned)
1598     locate_old_decl (olddecl);
1599
1600 #undef DECL_EXTERN_INLINE
1601
1602   return retval;
1603 }
1604
1605 /* Subroutine of duplicate_decls.  NEWDECL has been found to be
1606    consistent with OLDDECL, but carries new information.  Merge the
1607    new information into OLDDECL.  This function issues no
1608    diagnostics.  */
1609
1610 static void
1611 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1612 {
1613   bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1614                             && DECL_INITIAL (newdecl) != 0);
1615   bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
1616                            && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != 0);
1617   bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
1618                            && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) != 0);
1619   bool extern_changed = false;
1620
1621   /* For real parm decl following a forward decl, rechain the old decl
1622      in its new location and clear TREE_ASM_WRITTEN (it's not a
1623      forward decl anymore).  */
1624   if (TREE_CODE (newdecl) == PARM_DECL
1625       && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1626     {
1627       struct c_binding *b, **here;
1628
1629       for (here = &current_scope->bindings; *here; here = &(*here)->prev)
1630         if ((*here)->decl == olddecl)
1631           goto found;
1632       gcc_unreachable ();
1633
1634     found:
1635       b = *here;
1636       *here = b->prev;
1637       b->prev = current_scope->bindings;
1638       current_scope->bindings = b;
1639
1640       TREE_ASM_WRITTEN (olddecl) = 0;
1641     }
1642
1643   DECL_ATTRIBUTES (newdecl)
1644     = targetm.merge_decl_attributes (olddecl, newdecl);
1645
1646   /* Merge the data types specified in the two decls.  */
1647   TREE_TYPE (newdecl)
1648     = TREE_TYPE (olddecl)
1649     = composite_type (newtype, oldtype);
1650
1651   /* Lay the type out, unless already done.  */
1652   if (!comptypes (oldtype, TREE_TYPE (newdecl)))
1653     {
1654       if (TREE_TYPE (newdecl) != error_mark_node)
1655         layout_type (TREE_TYPE (newdecl));
1656       if (TREE_CODE (newdecl) != FUNCTION_DECL
1657           && TREE_CODE (newdecl) != TYPE_DECL
1658           && TREE_CODE (newdecl) != CONST_DECL)
1659         layout_decl (newdecl, 0);
1660     }
1661   else
1662     {
1663       /* Since the type is OLDDECL's, make OLDDECL's size go with.  */
1664       DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1665       DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1666       DECL_MODE (newdecl) = DECL_MODE (olddecl);
1667       if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1668         {
1669           DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1670           DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
1671         }
1672     }
1673
1674   /* Keep the old rtl since we can safely use it.  */
1675   if (HAS_RTL_P (olddecl))
1676     COPY_DECL_RTL (olddecl, newdecl);
1677
1678   /* Merge the type qualifiers.  */
1679   if (TREE_READONLY (newdecl))
1680     TREE_READONLY (olddecl) = 1;
1681
1682   if (TREE_THIS_VOLATILE (newdecl))
1683     TREE_THIS_VOLATILE (olddecl) = 1;
1684
1685   /* Merge deprecatedness.  */
1686   if (TREE_DEPRECATED (newdecl))
1687     TREE_DEPRECATED (olddecl) = 1;
1688
1689   /* If a decl is in a system header and the other isn't, keep the one on the
1690      system header. Otherwise, keep source location of definition rather than
1691      declaration and of prototype rather than non-prototype unless that
1692      prototype is built-in.  */
1693   if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
1694       && DECL_IN_SYSTEM_HEADER (olddecl)
1695       && !DECL_IN_SYSTEM_HEADER (newdecl) )
1696     DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1697   else if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
1698            && DECL_IN_SYSTEM_HEADER (newdecl)
1699            && !DECL_IN_SYSTEM_HEADER (olddecl))
1700     DECL_SOURCE_LOCATION (olddecl) = DECL_SOURCE_LOCATION (newdecl);
1701   else if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1702            || (old_is_prototype && !new_is_prototype
1703                && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
1704     DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1705
1706   /* Merge the initialization information.  */
1707    if (DECL_INITIAL (newdecl) == 0)
1708     DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1709
1710   /* Merge the threadprivate attribute.  */
1711   if (TREE_CODE (olddecl) == VAR_DECL && C_DECL_THREADPRIVATE_P (olddecl))
1712     {
1713       DECL_TLS_MODEL (newdecl) = DECL_TLS_MODEL (olddecl);
1714       C_DECL_THREADPRIVATE_P (newdecl) = 1;
1715     }
1716
1717   if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
1718     {
1719       /* Merge the section attribute.
1720          We want to issue an error if the sections conflict but that
1721          must be done later in decl_attributes since we are called
1722          before attributes are assigned.  */
1723       if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1724         DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1725
1726       /* Copy the assembler name.
1727          Currently, it can only be defined in the prototype.  */
1728       COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1729
1730       /* Use visibility of whichever declaration had it specified */
1731       if (DECL_VISIBILITY_SPECIFIED (olddecl))
1732         {
1733           DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1734           DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
1735         }
1736
1737       if (TREE_CODE (newdecl) == FUNCTION_DECL)
1738         {
1739           DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1740           DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1741           DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1742           DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1743             |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1744           TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1745           DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1746           DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
1747           TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1748           DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
1749           DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
1750         }
1751
1752       /* Merge the storage class information.  */
1753       merge_weak (newdecl, olddecl);
1754
1755       /* For functions, static overrides non-static.  */
1756       if (TREE_CODE (newdecl) == FUNCTION_DECL)
1757         {
1758           TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1759           /* This is since we don't automatically
1760              copy the attributes of NEWDECL into OLDDECL.  */
1761           TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1762           /* If this clears `static', clear it in the identifier too.  */
1763           if (!TREE_PUBLIC (olddecl))
1764             TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1765         }
1766     }
1767
1768   /* In c99, 'extern' declaration before (or after) 'inline' means this
1769      function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
1770      is present.  */
1771   if (TREE_CODE (newdecl) == FUNCTION_DECL
1772       && !flag_gnu89_inline
1773       && (DECL_DECLARED_INLINE_P (newdecl)
1774           || DECL_DECLARED_INLINE_P (olddecl))
1775       && (!DECL_DECLARED_INLINE_P (newdecl)
1776           || !DECL_DECLARED_INLINE_P (olddecl)
1777           || !DECL_EXTERNAL (olddecl))
1778       && DECL_EXTERNAL (newdecl)
1779       && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))
1780       && !current_function_decl)
1781     DECL_EXTERNAL (newdecl) = 0;
1782
1783   if (DECL_EXTERNAL (newdecl))
1784     {
1785       TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1786       DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1787
1788       /* An extern decl does not override previous storage class.  */
1789       TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1790       if (!DECL_EXTERNAL (newdecl))
1791         {
1792           DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1793           DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1794         }
1795     }
1796   else
1797     {
1798       TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1799       TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1800     }
1801
1802   if (TREE_CODE (newdecl) == FUNCTION_DECL)
1803     {
1804       /* If we're redefining a function previously defined as extern
1805          inline, make sure we emit debug info for the inline before we
1806          throw it away, in case it was inlined into a function that
1807          hasn't been written out yet.  */
1808       if (new_is_definition && DECL_INITIAL (olddecl))
1809         /* The new defn must not be inline.  */
1810         DECL_UNINLINABLE (newdecl) = 1;
1811       else
1812         {
1813           /* If either decl says `inline', this fn is inline, unless
1814              its definition was passed already.  */
1815           if (DECL_DECLARED_INLINE_P (newdecl)
1816               || DECL_DECLARED_INLINE_P (olddecl))
1817             DECL_DECLARED_INLINE_P (newdecl) = 1;
1818
1819           DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1820             = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1821
1822           DECL_DISREGARD_INLINE_LIMITS (newdecl)
1823             = DECL_DISREGARD_INLINE_LIMITS (olddecl)
1824             = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
1825                || DECL_DISREGARD_INLINE_LIMITS (olddecl));
1826         }
1827
1828       if (DECL_BUILT_IN (olddecl))
1829         {
1830           /* If redeclaring a builtin function, it stays built in.
1831              But it gets tagged as having been declared.  */
1832           DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1833           DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1834           C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1835           if (new_is_prototype)
1836             C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
1837           else
1838             C_DECL_BUILTIN_PROTOTYPE (newdecl)
1839               = C_DECL_BUILTIN_PROTOTYPE (olddecl);
1840         }
1841
1842       /* Preserve function specific target and optimization options */
1843       if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
1844           && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
1845         DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
1846           = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
1847
1848       if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
1849           && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
1850         DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
1851           = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
1852
1853       /* Also preserve various other info from the definition.  */
1854       if (!new_is_definition)
1855         {
1856           tree t;
1857           DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1858           DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1859           DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1860           DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1861           gimple_set_body (newdecl, gimple_body (olddecl));
1862           DECL_ARGUMENTS (newdecl) = copy_list (DECL_ARGUMENTS (olddecl));
1863           for (t = DECL_ARGUMENTS (newdecl); t ; t = TREE_CHAIN (t))
1864             DECL_CONTEXT (t) = newdecl;
1865
1866           /* See if we've got a function to instantiate from.  */
1867           if (DECL_SAVED_TREE (olddecl))
1868             DECL_ABSTRACT_ORIGIN (newdecl)
1869               = DECL_ABSTRACT_ORIGIN (olddecl);
1870         }
1871     }
1872
1873    extern_changed = DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl);
1874
1875    /* Merge the USED information.  */
1876    if (TREE_USED (olddecl))
1877      TREE_USED (newdecl) = 1;
1878    else if (TREE_USED (newdecl))
1879      TREE_USED (olddecl) = 1;
1880
1881   /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1882      But preserve OLDDECL's DECL_UID and DECL_CONTEXT.  */
1883   {
1884     unsigned olddecl_uid = DECL_UID (olddecl);
1885     tree olddecl_context = DECL_CONTEXT (olddecl);
1886     tree olddecl_arguments = NULL;
1887     if (TREE_CODE (olddecl) == FUNCTION_DECL)
1888       olddecl_arguments = DECL_ARGUMENTS (olddecl);
1889
1890     memcpy ((char *) olddecl + sizeof (struct tree_common),
1891             (char *) newdecl + sizeof (struct tree_common),
1892             sizeof (struct tree_decl_common) - sizeof (struct tree_common));
1893     switch (TREE_CODE (olddecl))
1894       {
1895       case FUNCTION_DECL:
1896         gimple_set_body (olddecl, gimple_body (newdecl));
1897         /* fall through */
1898
1899       case FIELD_DECL:
1900       case VAR_DECL:
1901       case PARM_DECL:
1902       case LABEL_DECL:
1903       case RESULT_DECL:
1904       case CONST_DECL:
1905       case TYPE_DECL:
1906         memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
1907                 (char *) newdecl + sizeof (struct tree_decl_common),
1908                 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
1909         break;
1910
1911       default:
1912
1913         memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
1914                 (char *) newdecl + sizeof (struct tree_decl_common),
1915                 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
1916       }
1917     DECL_UID (olddecl) = olddecl_uid;
1918     DECL_CONTEXT (olddecl) = olddecl_context;
1919     if (TREE_CODE (olddecl) == FUNCTION_DECL)
1920       DECL_ARGUMENTS (olddecl) = olddecl_arguments;
1921   }
1922
1923   /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1924      so that encode_section_info has a chance to look at the new decl
1925      flags and attributes.  */
1926   if (DECL_RTL_SET_P (olddecl)
1927       && (TREE_CODE (olddecl) == FUNCTION_DECL
1928           || (TREE_CODE (olddecl) == VAR_DECL
1929               && TREE_STATIC (olddecl))))
1930     make_decl_rtl (olddecl);
1931
1932   /* If we changed a function from DECL_EXTERNAL to !DECL_EXTERNAL,
1933      and the definition is coming from the old version, cgraph needs
1934      to be called again.  */
1935   if (extern_changed && !new_is_definition
1936       && TREE_CODE (olddecl) == FUNCTION_DECL && DECL_INITIAL (olddecl))
1937     cgraph_mark_if_needed (olddecl);
1938 }
1939
1940 /* Handle when a new declaration NEWDECL has the same name as an old
1941    one OLDDECL in the same binding contour.  Prints an error message
1942    if appropriate.
1943
1944    If safely possible, alter OLDDECL to look like NEWDECL, and return
1945    true.  Otherwise, return false.  */
1946
1947 static bool
1948 duplicate_decls (tree newdecl, tree olddecl)
1949 {
1950   tree newtype = NULL, oldtype = NULL;
1951
1952   if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
1953     {
1954       /* Avoid `unused variable' and other warnings for OLDDECL.  */
1955       TREE_NO_WARNING (olddecl) = 1;
1956       return false;
1957     }
1958
1959   merge_decls (newdecl, olddecl, newtype, oldtype);
1960   return true;
1961 }
1962
1963 \f
1964 /* Check whether decl-node NEW_DECL shadows an existing declaration.  */
1965 static void
1966 warn_if_shadowing (tree new_decl)
1967 {
1968   struct c_binding *b;
1969
1970   /* Shadow warnings wanted?  */
1971   if (!warn_shadow
1972       /* No shadow warnings for internally generated vars.  */
1973       || DECL_IS_BUILTIN (new_decl)
1974       /* No shadow warnings for vars made for inlining.  */
1975       || DECL_FROM_INLINE (new_decl))
1976     return;
1977
1978   /* Is anything being shadowed?  Invisible decls do not count.  */
1979   for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
1980     if (b->decl && b->decl != new_decl && !b->invisible)
1981       {
1982         tree old_decl = b->decl;
1983
1984         if (old_decl == error_mark_node)
1985           {
1986             warning (OPT_Wshadow, "declaration of %q+D shadows previous "
1987                      "non-variable", new_decl);
1988             break;
1989           }
1990         else if (TREE_CODE (old_decl) == PARM_DECL)
1991           warning (OPT_Wshadow, "declaration of %q+D shadows a parameter",
1992                    new_decl);
1993         else if (DECL_FILE_SCOPE_P (old_decl))
1994           warning (OPT_Wshadow, "declaration of %q+D shadows a global "
1995                    "declaration", new_decl);
1996         else if (TREE_CODE (old_decl) == FUNCTION_DECL
1997                  && DECL_BUILT_IN (old_decl))
1998           {
1999             warning (OPT_Wshadow, "declaration of %q+D shadows "
2000                      "a built-in function", new_decl);
2001             break;
2002           }
2003         else
2004           warning (OPT_Wshadow, "declaration of %q+D shadows a previous local",
2005                    new_decl);
2006
2007         warning (OPT_Wshadow, "%Jshadowed declaration is here", old_decl);
2008
2009         break;
2010       }
2011 }
2012
2013 /* Record a decl-node X as belonging to the current lexical scope.
2014    Check for errors (such as an incompatible declaration for the same
2015    name already seen in the same scope).
2016
2017    Returns either X or an old decl for the same name.
2018    If an old decl is returned, it may have been smashed
2019    to agree with what X says.  */
2020
2021 tree
2022 pushdecl (tree x)
2023 {
2024   tree name = DECL_NAME (x);
2025   struct c_scope *scope = current_scope;
2026   struct c_binding *b;
2027   bool nested = false;
2028   location_t locus = DECL_SOURCE_LOCATION (x);
2029
2030   /* Must set DECL_CONTEXT for everything not at file scope or
2031      DECL_FILE_SCOPE_P won't work.  Local externs don't count
2032      unless they have initializers (which generate code).  */
2033   if (current_function_decl
2034       && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
2035           || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2036     DECL_CONTEXT (x) = current_function_decl;
2037
2038   /* If this is of variably modified type, prevent jumping into its
2039      scope.  */
2040   if ((TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == TYPE_DECL)
2041       && variably_modified_type_p (TREE_TYPE (x), NULL_TREE))
2042     c_begin_vm_scope (scope->depth);
2043
2044   /* Anonymous decls are just inserted in the scope.  */
2045   if (!name)
2046     {
2047       bind (name, x, scope, /*invisible=*/false, /*nested=*/false,
2048             locus);
2049       return x;
2050     }
2051
2052   /* First, see if there is another declaration with the same name in
2053      the current scope.  If there is, duplicate_decls may do all the
2054      work for us.  If duplicate_decls returns false, that indicates
2055      two incompatible decls in the same scope; we are to silently
2056      replace the old one (duplicate_decls has issued all appropriate
2057      diagnostics).  In particular, we should not consider possible
2058      duplicates in the external scope, or shadowing.  */
2059   b = I_SYMBOL_BINDING (name);
2060   if (b && B_IN_SCOPE (b, scope))
2061     {
2062       struct c_binding *b_ext, *b_use;
2063       tree type = TREE_TYPE (x);
2064       tree visdecl = b->decl;
2065       tree vistype = TREE_TYPE (visdecl);
2066       if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2067           && COMPLETE_TYPE_P (TREE_TYPE (x)))
2068         b->inner_comp = false;
2069       b_use = b;
2070       b_ext = b;
2071       /* If this is an external linkage declaration, we should check
2072          for compatibility with the type in the external scope before
2073          setting the type at this scope based on the visible
2074          information only.  */
2075       if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2076         {
2077           while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2078             b_ext = b_ext->shadowed;
2079           if (b_ext)
2080             {
2081               b_use = b_ext;
2082               if (b_use->type)
2083                 TREE_TYPE (b_use->decl) = b_use->type;
2084             }
2085         }
2086       if (duplicate_decls (x, b_use->decl))
2087         {
2088           if (b_use != b)
2089             {
2090               /* Save the updated type in the external scope and
2091                  restore the proper type for this scope.  */
2092               tree thistype;
2093               if (comptypes (vistype, type))
2094                 thistype = composite_type (vistype, type);
2095               else
2096                 thistype = TREE_TYPE (b_use->decl);
2097               b_use->type = TREE_TYPE (b_use->decl);
2098               if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2099                   && DECL_BUILT_IN (b_use->decl))
2100                 thistype
2101                   = build_type_attribute_variant (thistype,
2102                                                   TYPE_ATTRIBUTES
2103                                                   (b_use->type));
2104               TREE_TYPE (b_use->decl) = thistype;
2105             }
2106           return b_use->decl;
2107         }
2108       else
2109         goto skip_external_and_shadow_checks;
2110     }
2111
2112   /* All declarations with external linkage, and all external
2113      references, go in the external scope, no matter what scope is
2114      current.  However, the binding in that scope is ignored for
2115      purposes of normal name lookup.  A separate binding structure is
2116      created in the requested scope; this governs the normal
2117      visibility of the symbol.
2118
2119      The binding in the externals scope is used exclusively for
2120      detecting duplicate declarations of the same object, no matter
2121      what scope they are in; this is what we do here.  (C99 6.2.7p2:
2122      All declarations that refer to the same object or function shall
2123      have compatible type; otherwise, the behavior is undefined.)  */
2124   if (DECL_EXTERNAL (x) || scope == file_scope)
2125     {
2126       tree type = TREE_TYPE (x);
2127       tree vistype = 0;
2128       tree visdecl = 0;
2129       bool type_saved = false;
2130       if (b && !B_IN_EXTERNAL_SCOPE (b)
2131           && (TREE_CODE (b->decl) == FUNCTION_DECL
2132               || TREE_CODE (b->decl) == VAR_DECL)
2133           && DECL_FILE_SCOPE_P (b->decl))
2134         {
2135           visdecl = b->decl;
2136           vistype = TREE_TYPE (visdecl);
2137         }
2138       if (scope != file_scope
2139           && !DECL_IN_SYSTEM_HEADER (x))
2140         warning (OPT_Wnested_externs, "nested extern declaration of %qD", x);
2141
2142       while (b && !B_IN_EXTERNAL_SCOPE (b))
2143         {
2144           /* If this decl might be modified, save its type.  This is
2145              done here rather than when the decl is first bound
2146              because the type may change after first binding, through
2147              being completed or through attributes being added.  If we
2148              encounter multiple such decls, only the first should have
2149              its type saved; the others will already have had their
2150              proper types saved and the types will not have changed as
2151              their scopes will not have been re-entered.  */
2152           if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2153             {
2154               b->type = TREE_TYPE (b->decl);
2155               type_saved = true;
2156             }
2157           if (B_IN_FILE_SCOPE (b)
2158               && TREE_CODE (b->decl) == VAR_DECL
2159               && TREE_STATIC (b->decl)
2160               && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2161               && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2162               && TREE_CODE (type) == ARRAY_TYPE
2163               && TYPE_DOMAIN (type)
2164               && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2165               && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2166             {
2167               /* Array type completed in inner scope, which should be
2168                  diagnosed if the completion does not have size 1 and
2169                  it does not get completed in the file scope.  */
2170               b->inner_comp = true;
2171             }
2172           b = b->shadowed;
2173         }
2174
2175       /* If a matching external declaration has been found, set its
2176          type to the composite of all the types of that declaration.
2177          After the consistency checks, it will be reset to the
2178          composite of the visible types only.  */
2179       if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2180           && b->type)
2181         TREE_TYPE (b->decl) = b->type;
2182
2183       /* The point of the same_translation_unit_p check here is,
2184          we want to detect a duplicate decl for a construct like
2185          foo() { extern bar(); } ... static bar();  but not if
2186          they are in different translation units.  In any case,
2187          the static does not go in the externals scope.  */
2188       if (b
2189           && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2190           && duplicate_decls (x, b->decl))
2191         {
2192           tree thistype;
2193           if (vistype)
2194             {
2195               if (comptypes (vistype, type))
2196                 thistype = composite_type (vistype, type);
2197               else
2198                 thistype = TREE_TYPE (b->decl);
2199             }
2200           else
2201             thistype = type;
2202           b->type = TREE_TYPE (b->decl);
2203           if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2204             thistype
2205               = build_type_attribute_variant (thistype,
2206                                               TYPE_ATTRIBUTES (b->type));
2207           TREE_TYPE (b->decl) = thistype;
2208           bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true,
2209                 locus);
2210           return b->decl;
2211         }
2212       else if (TREE_PUBLIC (x))
2213         {
2214           if (visdecl && !b && duplicate_decls (x, visdecl))
2215             {
2216               /* An external declaration at block scope referring to a
2217                  visible entity with internal linkage.  The composite
2218                  type will already be correct for this scope, so we
2219                  just need to fall through to make the declaration in
2220                  this scope.  */
2221               nested = true;
2222               x = visdecl;
2223             }
2224           else
2225             {
2226               bind (name, x, external_scope, /*invisible=*/true,
2227                     /*nested=*/false, locus);
2228               nested = true;
2229             }
2230         }
2231     }
2232
2233   if (TREE_CODE (x) != PARM_DECL)
2234     warn_if_shadowing (x);
2235
2236  skip_external_and_shadow_checks:
2237   if (TREE_CODE (x) == TYPE_DECL)
2238     set_underlying_type (x);
2239
2240   bind (name, x, scope, /*invisible=*/false, nested, locus);
2241
2242   /* If x's type is incomplete because it's based on a
2243      structure or union which has not yet been fully declared,
2244      attach it to that structure or union type, so we can go
2245      back and complete the variable declaration later, if the
2246      structure or union gets fully declared.
2247
2248      If the input is erroneous, we can have error_mark in the type
2249      slot (e.g. "f(void a, ...)") - that doesn't count as an
2250      incomplete type.  */
2251   if (TREE_TYPE (x) != error_mark_node
2252       && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2253     {
2254       tree element = TREE_TYPE (x);
2255
2256       while (TREE_CODE (element) == ARRAY_TYPE)
2257         element = TREE_TYPE (element);
2258       element = TYPE_MAIN_VARIANT (element);
2259
2260       if ((TREE_CODE (element) == RECORD_TYPE
2261            || TREE_CODE (element) == UNION_TYPE)
2262           && (TREE_CODE (x) != TYPE_DECL
2263               || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
2264           && !COMPLETE_TYPE_P (element))
2265         C_TYPE_INCOMPLETE_VARS (element)
2266           = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
2267     }
2268   return x;
2269 }
2270
2271 /* Record X as belonging to file scope.
2272    This is used only internally by the Objective-C front end,
2273    and is limited to its needs.  duplicate_decls is not called;
2274    if there is any preexisting decl for this identifier, it is an ICE.  */
2275
2276 tree
2277 pushdecl_top_level (tree x)
2278 {
2279   tree name;
2280   bool nested = false;
2281   gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
2282
2283   name = DECL_NAME (x);
2284
2285  gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
2286
2287   if (TREE_PUBLIC (x))
2288     {
2289       bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false,
2290             UNKNOWN_LOCATION);
2291       nested = true;
2292     }
2293   if (file_scope)
2294     bind (name, x, file_scope, /*invisible=*/false, nested, UNKNOWN_LOCATION);
2295
2296   return x;
2297 }
2298 \f
2299 static void
2300 implicit_decl_warning (tree id, tree olddecl)
2301 {
2302   if (warn_implicit_function_declaration)
2303     {
2304       bool warned;
2305
2306       if (flag_isoc99)
2307         warned = pedwarn (input_location, OPT_Wimplicit_function_declaration,
2308                           "implicit declaration of function %qE", id);
2309       else 
2310         warned = warning (OPT_Wimplicit_function_declaration, 
2311                           G_("implicit declaration of function %qE"), id);
2312       if (olddecl && warned)
2313         locate_old_decl (olddecl);
2314     }
2315 }
2316
2317 /* Generate an implicit declaration for identifier FUNCTIONID as a
2318    function of type int ().  */
2319
2320 tree
2321 implicitly_declare (tree functionid)
2322 {
2323   struct c_binding *b;
2324   tree decl = 0;
2325   tree asmspec_tree;
2326
2327   for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
2328     {
2329       if (B_IN_SCOPE (b, external_scope))
2330         {
2331           decl = b->decl;
2332           break;
2333         }
2334     }
2335
2336   if (decl)
2337     {
2338       if (decl == error_mark_node)
2339         return decl;
2340
2341       /* FIXME: Objective-C has weird not-really-builtin functions
2342          which are supposed to be visible automatically.  They wind up
2343          in the external scope because they're pushed before the file
2344          scope gets created.  Catch this here and rebind them into the
2345          file scope.  */
2346       if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2347         {
2348           bind (functionid, decl, file_scope,
2349                 /*invisible=*/false, /*nested=*/true,
2350                 DECL_SOURCE_LOCATION (decl));
2351           return decl;
2352         }
2353       else
2354         {
2355           tree newtype = default_function_type;
2356           if (b->type)
2357             TREE_TYPE (decl) = b->type;
2358           /* Implicit declaration of a function already declared
2359              (somehow) in a different scope, or as a built-in.
2360              If this is the first time this has happened, warn;
2361              then recycle the old declaration but with the new type.  */
2362           if (!C_DECL_IMPLICIT (decl))
2363             {
2364               implicit_decl_warning (functionid, decl);
2365               C_DECL_IMPLICIT (decl) = 1;
2366             }
2367           if (DECL_BUILT_IN (decl))
2368             {
2369               newtype = build_type_attribute_variant (newtype,
2370                                                       TYPE_ATTRIBUTES
2371                                                       (TREE_TYPE (decl)));
2372               if (!comptypes (newtype, TREE_TYPE (decl)))
2373                 {
2374                   warning (0, "incompatible implicit declaration of built-in"
2375                            " function %qD", decl);
2376                   newtype = TREE_TYPE (decl);
2377                 }
2378             }
2379           else
2380             {
2381               if (!comptypes (newtype, TREE_TYPE (decl)))
2382                 {
2383                   error ("incompatible implicit declaration of function %qD",
2384                          decl);
2385                   locate_old_decl (decl);
2386                 }
2387             }
2388           b->type = TREE_TYPE (decl);
2389           TREE_TYPE (decl) = newtype;
2390           bind (functionid, decl, current_scope,
2391                 /*invisible=*/false, /*nested=*/true,
2392                 DECL_SOURCE_LOCATION (decl));
2393           return decl;
2394         }
2395     }
2396
2397   /* Not seen before.  */
2398   decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2399   DECL_EXTERNAL (decl) = 1;
2400   TREE_PUBLIC (decl) = 1;
2401   C_DECL_IMPLICIT (decl) = 1;
2402   implicit_decl_warning (functionid, 0);
2403   asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
2404   if (asmspec_tree)
2405     set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
2406
2407   /* C89 says implicit declarations are in the innermost block.
2408      So we record the decl in the standard fashion.  */
2409   decl = pushdecl (decl);
2410
2411   /* No need to call objc_check_decl here - it's a function type.  */
2412   rest_of_decl_compilation (decl, 0, 0);
2413
2414   /* Write a record describing this implicit function declaration
2415      to the prototypes file (if requested).  */
2416   gen_aux_info_record (decl, 0, 1, 0);
2417
2418   /* Possibly apply some default attributes to this implicit declaration.  */
2419   decl_attributes (&decl, NULL_TREE, 0);
2420
2421   return decl;
2422 }
2423
2424 /* Issue an error message for a reference to an undeclared variable
2425    ID, including a reference to a builtin outside of function-call
2426    context.  Establish a binding of the identifier to error_mark_node
2427    in an appropriate scope, which will suppress further errors for the
2428    same identifier.  The error message should be given location LOC.  */
2429 void
2430 undeclared_variable (tree id, location_t loc)
2431 {
2432   static bool already = false;
2433   struct c_scope *scope;
2434
2435   if (current_function_decl == 0)
2436     {
2437       error ("%H%qE undeclared here (not in a function)", &loc, id);
2438       scope = current_scope;
2439     }
2440   else
2441     {
2442       error ("%H%qE undeclared (first use in this function)", &loc, id);
2443
2444       if (!already)
2445         {
2446           error ("%H(Each undeclared identifier is reported only once", &loc);
2447           error ("%Hfor each function it appears in.)", &loc);
2448           already = true;
2449         }
2450
2451       /* If we are parsing old-style parameter decls, current_function_decl
2452          will be nonnull but current_function_scope will be null.  */
2453       scope = current_function_scope ? current_function_scope : current_scope;
2454     }
2455   bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
2456         UNKNOWN_LOCATION);
2457 }
2458 \f
2459 /* Subroutine of lookup_label, declare_label, define_label: construct a
2460    LABEL_DECL with all the proper frills.  */
2461
2462 static tree
2463 make_label (tree name, location_t location)
2464 {
2465   tree label = build_decl (LABEL_DECL, name, void_type_node);
2466
2467   DECL_CONTEXT (label) = current_function_decl;
2468   DECL_MODE (label) = VOIDmode;
2469   DECL_SOURCE_LOCATION (label) = location;
2470
2471   return label;
2472 }
2473
2474 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2475    Create one if none exists so far for the current function.
2476    This is called when a label is used in a goto expression or
2477    has its address taken.  */
2478
2479 tree
2480 lookup_label (tree name)
2481 {
2482   tree label;
2483
2484   if (current_function_decl == 0)
2485     {
2486       error ("label %qE referenced outside of any function", name);
2487       return 0;
2488     }
2489
2490   /* Use a label already defined or ref'd with this name, but not if
2491      it is inherited from a containing function and wasn't declared
2492      using __label__.  */
2493   label = I_LABEL_DECL (name);
2494   if (label && (DECL_CONTEXT (label) == current_function_decl
2495                 || C_DECLARED_LABEL_FLAG (label)))
2496     {
2497       /* If the label has only been declared, update its apparent
2498          location to point here, for better diagnostics if it
2499          turns out not to have been defined.  */
2500       if (!TREE_USED (label))
2501         DECL_SOURCE_LOCATION (label) = input_location;
2502       return label;
2503     }
2504
2505   /* No label binding for that identifier; make one.  */
2506   label = make_label (name, input_location);
2507
2508   /* Ordinary labels go in the current function scope.  */
2509   bind (name, label, current_function_scope,
2510         /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
2511   return label;
2512 }
2513
2514 /* Make a label named NAME in the current function, shadowing silently
2515    any that may be inherited from containing functions or containing
2516    scopes.  This is called for __label__ declarations.  */
2517
2518 tree
2519 declare_label (tree name)
2520 {
2521   struct c_binding *b = I_LABEL_BINDING (name);
2522   tree label;
2523
2524   /* Check to make sure that the label hasn't already been declared
2525      at this scope */
2526   if (b && B_IN_CURRENT_SCOPE (b))
2527     {
2528       error ("duplicate label declaration %qE", name);
2529       locate_old_decl (b->decl);
2530
2531       /* Just use the previous declaration.  */
2532       return b->decl;
2533     }
2534
2535   label = make_label (name, input_location);
2536   C_DECLARED_LABEL_FLAG (label) = 1;
2537
2538   /* Declared labels go in the current scope.  */
2539   bind (name, label, current_scope,
2540         /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
2541   return label;
2542 }
2543
2544 /* Define a label, specifying the location in the source file.
2545    Return the LABEL_DECL node for the label, if the definition is valid.
2546    Otherwise return 0.  */
2547
2548 tree
2549 define_label (location_t location, tree name)
2550 {
2551   /* Find any preexisting label with this name.  It is an error
2552      if that label has already been defined in this function, or
2553      if there is a containing function with a declared label with
2554      the same name.  */
2555   tree label = I_LABEL_DECL (name);
2556   struct c_label_list *nlist_se, *nlist_vm;
2557
2558   if (label
2559       && ((DECL_CONTEXT (label) == current_function_decl
2560            && DECL_INITIAL (label) != 0)
2561           || (DECL_CONTEXT (label) != current_function_decl
2562               && C_DECLARED_LABEL_FLAG (label))))
2563     {
2564       error ("%Hduplicate label %qD", &location, label);
2565       locate_old_decl (label);
2566       return 0;
2567     }
2568   else if (label && DECL_CONTEXT (label) == current_function_decl)
2569     {
2570       /* The label has been used or declared already in this function,
2571          but not defined.  Update its location to point to this
2572          definition.  */
2573       if (C_DECL_UNDEFINABLE_STMT_EXPR (label))
2574         error ("%Jjump into statement expression", label);
2575       if (C_DECL_UNDEFINABLE_VM (label))
2576         error ("%Jjump into scope of identifier with variably modified type",
2577                label);
2578       DECL_SOURCE_LOCATION (label) = location;
2579     }
2580   else
2581     {
2582       /* No label binding for that identifier; make one.  */
2583       label = make_label (name, location);
2584
2585       /* Ordinary labels go in the current function scope.  */
2586       bind (name, label, current_function_scope,
2587             /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
2588     }
2589
2590   if (!in_system_header && lookup_name (name))
2591     warning (OPT_Wtraditional, "%Htraditional C lacks a separate namespace "
2592              "for labels, identifier %qE conflicts", &location, name);
2593
2594   nlist_se = XOBNEW (&parser_obstack, struct c_label_list);
2595   nlist_se->next = label_context_stack_se->labels_def;
2596   nlist_se->label = label;
2597   label_context_stack_se->labels_def = nlist_se;
2598
2599   nlist_vm = XOBNEW (&parser_obstack, struct c_label_list);
2600   nlist_vm->next = label_context_stack_vm->labels_def;
2601   nlist_vm->label = label;
2602   label_context_stack_vm->labels_def = nlist_vm;
2603
2604   /* Mark label as having been defined.  */
2605   DECL_INITIAL (label) = error_mark_node;
2606   return label;
2607 }
2608 \f
2609 /* Given NAME, an IDENTIFIER_NODE,
2610    return the structure (or union or enum) definition for that name.
2611    If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2612    CODE says which kind of type the caller wants;
2613    it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2614    If the wrong kind of type is found, an error is reported.  */
2615
2616 static tree
2617 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2618 {
2619   struct c_binding *b = I_TAG_BINDING (name);
2620   int thislevel = 0;
2621
2622   if (!b || !b->decl)
2623     return 0;
2624
2625   /* We only care about whether it's in this level if
2626      thislevel_only was set or it might be a type clash.  */
2627   if (thislevel_only || TREE_CODE (b->decl) != code)
2628     {
2629       /* For our purposes, a tag in the external scope is the same as
2630          a tag in the file scope.  (Primarily relevant to Objective-C
2631          and its builtin structure tags, which get pushed before the
2632          file scope is created.)  */
2633       if (B_IN_CURRENT_SCOPE (b)
2634           || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
2635         thislevel = 1;
2636     }
2637
2638   if (thislevel_only && !thislevel)
2639     return 0;
2640
2641   if (TREE_CODE (b->decl) != code)
2642     {
2643       /* Definition isn't the kind we were looking for.  */
2644       pending_invalid_xref = name;
2645       pending_invalid_xref_location = input_location;
2646
2647       /* If in the same binding level as a declaration as a tag
2648          of a different type, this must not be allowed to
2649          shadow that tag, so give the error immediately.
2650          (For example, "struct foo; union foo;" is invalid.)  */
2651       if (thislevel)
2652         pending_xref_error ();
2653     }
2654   return b->decl;
2655 }
2656
2657 /* Print an error message now
2658    for a recent invalid struct, union or enum cross reference.
2659    We don't print them immediately because they are not invalid
2660    when used in the `struct foo;' construct for shadowing.  */
2661
2662 void
2663 pending_xref_error (void)
2664 {
2665   if (pending_invalid_xref != 0)
2666     error ("%H%qE defined as wrong kind of tag",
2667            &pending_invalid_xref_location, pending_invalid_xref);
2668   pending_invalid_xref = 0;
2669 }
2670
2671 \f
2672 /* Look up NAME in the current scope and its superiors
2673    in the namespace of variables, functions and typedefs.
2674    Return a ..._DECL node of some kind representing its definition,
2675    or return 0 if it is undefined.  */
2676
2677 tree
2678 lookup_name (tree name)
2679 {
2680   struct c_binding *b = I_SYMBOL_BINDING (name);
2681   if (b && !b->invisible)
2682     return b->decl;
2683   return 0;
2684 }
2685
2686 /* Similar to `lookup_name' but look only at the indicated scope.  */
2687
2688 static tree
2689 lookup_name_in_scope (tree name, struct c_scope *scope)
2690 {
2691   struct c_binding *b;
2692
2693   for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2694     if (B_IN_SCOPE (b, scope))
2695       return b->decl;
2696   return 0;
2697 }
2698 \f
2699 /* Create the predefined scalar types of C,
2700    and some nodes representing standard constants (0, 1, (void *) 0).
2701    Initialize the global scope.
2702    Make definitions for built-in primitive functions.  */
2703
2704 void
2705 c_init_decl_processing (void)
2706 {
2707   location_t save_loc = input_location;
2708
2709   /* Initialize reserved words for parser.  */
2710   c_parse_init ();
2711
2712   current_function_decl = 0;
2713
2714   gcc_obstack_init (&parser_obstack);
2715
2716   /* Make the externals scope.  */
2717   push_scope ();
2718   external_scope = current_scope;
2719
2720   /* Declarations from c_common_nodes_and_builtins must not be associated
2721      with this input file, lest we get differences between using and not
2722      using preprocessed headers.  */
2723   input_location = BUILTINS_LOCATION;
2724
2725   build_common_tree_nodes (flag_signed_char, false);
2726
2727   c_common_nodes_and_builtins ();
2728
2729   /* In C, comparisons and TRUTH_* expressions have type int.  */
2730   truthvalue_type_node = integer_type_node;
2731   truthvalue_true_node = integer_one_node;
2732   truthvalue_false_node = integer_zero_node;
2733
2734   /* Even in C99, which has a real boolean type.  */
2735   pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2736                         boolean_type_node));
2737
2738   input_location = save_loc;
2739
2740   pedantic_lvalues = true;
2741
2742   make_fname_decl = c_make_fname_decl;
2743   start_fname_decls ();
2744 }
2745
2746 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2747    decl, NAME is the initialization string and TYPE_DEP indicates whether
2748    NAME depended on the type of the function.  As we don't yet implement
2749    delayed emission of static data, we mark the decl as emitted
2750    so it is not placed in the output.  Anything using it must therefore pull
2751    out the STRING_CST initializer directly.  FIXME.  */
2752
2753 static tree
2754 c_make_fname_decl (tree id, int type_dep)
2755 {
2756   const char *name = fname_as_string (type_dep);
2757   tree decl, type, init;
2758   size_t length = strlen (name);
2759
2760   type = build_array_type (char_type_node,
2761                            build_index_type (size_int (length)));
2762   type = c_build_qualified_type (type, TYPE_QUAL_CONST);
2763
2764   decl = build_decl (VAR_DECL, id, type);
2765
2766   TREE_STATIC (decl) = 1;
2767   TREE_READONLY (decl) = 1;
2768   DECL_ARTIFICIAL (decl) = 1;
2769
2770   init = build_string (length + 1, name);
2771   free (CONST_CAST (char *, name));
2772   TREE_TYPE (init) = type;
2773   DECL_INITIAL (decl) = init;
2774
2775   TREE_USED (decl) = 1;
2776
2777   if (current_function_decl
2778       /* For invalid programs like this:
2779         
2780          void foo()
2781          const char* p = __FUNCTION__;
2782         
2783          the __FUNCTION__ is believed to appear in K&R style function
2784          parameter declarator.  In that case we still don't have
2785          function_scope.  */
2786       && (!errorcount || current_function_scope))
2787     {
2788       DECL_CONTEXT (decl) = current_function_decl;
2789       bind (id, decl, current_function_scope,
2790             /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
2791     }
2792
2793   finish_decl (decl, init, NULL_TREE);
2794
2795   return decl;
2796 }
2797
2798 tree
2799 c_builtin_function (tree decl)
2800 {
2801   tree type = TREE_TYPE (decl);
2802   tree   id = DECL_NAME (decl);
2803
2804   const char *name = IDENTIFIER_POINTER (id);
2805   C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0);
2806
2807   /* Should never be called on a symbol with a preexisting meaning.  */
2808   gcc_assert (!I_SYMBOL_BINDING (id));
2809
2810   bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
2811         UNKNOWN_LOCATION);
2812
2813   /* Builtins in the implementation namespace are made visible without
2814      needing to be explicitly declared.  See push_file_scope.  */
2815   if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2816     {
2817       TREE_CHAIN (decl) = visible_builtins;
2818       visible_builtins = decl;
2819     }
2820
2821   return decl;
2822 }
2823
2824 tree
2825 c_builtin_function_ext_scope (tree decl)
2826 {
2827   tree type = TREE_TYPE (decl);
2828   tree   id = DECL_NAME (decl);
2829
2830   const char *name = IDENTIFIER_POINTER (id);
2831   C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0);
2832
2833   /* Should never be called on a symbol with a preexisting meaning.  */
2834   gcc_assert (!I_SYMBOL_BINDING (id));
2835
2836   bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
2837         UNKNOWN_LOCATION);
2838
2839   /* Builtins in the implementation namespace are made visible without
2840      needing to be explicitly declared.  See push_file_scope.  */
2841   if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2842     {
2843       TREE_CHAIN (decl) = visible_builtins;
2844       visible_builtins = decl;
2845     }
2846
2847   return decl;
2848 }
2849 \f
2850 /* Called when a declaration is seen that contains no names to declare.
2851    If its type is a reference to a structure, union or enum inherited
2852    from a containing scope, shadow that tag name for the current scope
2853    with a forward reference.
2854    If its type defines a new named structure or union
2855    or defines an enum, it is valid but we need not do anything here.
2856    Otherwise, it is an error.  */
2857
2858 void
2859 shadow_tag (const struct c_declspecs *declspecs)
2860 {
2861   shadow_tag_warned (declspecs, 0);
2862 }
2863
2864 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
2865    but no pedwarn.  */
2866 void
2867 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
2868 {
2869   bool found_tag = false;
2870
2871   if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
2872     {
2873       tree value = declspecs->type;
2874       enum tree_code code = TREE_CODE (value);
2875
2876       if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2877         /* Used to test also that TYPE_SIZE (value) != 0.
2878            That caused warning for `struct foo;' at top level in the file.  */
2879         {
2880           tree name = TYPE_NAME (value);
2881           tree t;
2882
2883           found_tag = true;
2884
2885           if (name == 0)
2886             {
2887               if (warned != 1 && code != ENUMERAL_TYPE)
2888                 /* Empty unnamed enum OK */
2889                 {
2890                   pedwarn (input_location, 0,
2891                            "unnamed struct/union that defines no instances");
2892                   warned = 1;
2893                 }
2894             }
2895           else if (!declspecs->tag_defined_p
2896                    && declspecs->storage_class != csc_none)
2897             {
2898               if (warned != 1)
2899                 pedwarn (input_location, 0,
2900                          "empty declaration with storage class specifier "
2901                          "does not redeclare tag");
2902               warned = 1;
2903               pending_xref_error ();
2904             }
2905           else if (!declspecs->tag_defined_p
2906                    && (declspecs->const_p
2907                        || declspecs->volatile_p
2908                        || declspecs->restrict_p))
2909             {
2910               if (warned != 1)
2911                 pedwarn (input_location, 0,
2912                          "empty declaration with type qualifier "
2913                           "does not redeclare tag");
2914               warned = 1;
2915               pending_xref_error ();
2916             }
2917           else
2918             {
2919               pending_invalid_xref = 0;
2920               t = lookup_tag (code, name, 1);
2921
2922               if (t == 0)
2923                 {
2924                   t = make_node (code);
2925                   pushtag (name, t);
2926                 }
2927             }
2928         }
2929       else
2930         {
2931           if (warned != 1 && !in_system_header)
2932             {
2933               pedwarn (input_location, 0,
2934                        "useless type name in empty declaration");
2935               warned = 1;
2936             }
2937         }
2938     }
2939   else if (warned != 1 && !in_system_header && declspecs->typedef_p)
2940     {
2941       pedwarn (input_location, 0, "useless type name in empty declaration");
2942       warned = 1;
2943     }
2944
2945   pending_invalid_xref = 0;
2946
2947   if (declspecs->inline_p)
2948     {
2949       error ("%<inline%> in empty declaration");
2950       warned = 1;
2951     }
2952
2953   if (current_scope == file_scope && declspecs->storage_class == csc_auto)
2954     {
2955       error ("%<auto%> in file-scope empty declaration");
2956       warned = 1;
2957     }
2958
2959   if (current_scope == file_scope && declspecs->storage_class == csc_register)
2960     {
2961       error ("%<register%> in file-scope empty declaration");
2962       warned = 1;
2963     }
2964
2965   if (!warned && !in_system_header && declspecs->storage_class != csc_none)
2966     {
2967       warning (0, "useless storage class specifier in empty declaration");
2968       warned = 2;
2969     }
2970
2971   if (!warned && !in_system_header && declspecs->thread_p)
2972     {
2973       warning (0, "useless %<__thread%> in empty declaration");
2974       warned = 2;
2975     }
2976
2977   if (!warned && !in_system_header && (declspecs->const_p
2978                                        || declspecs->volatile_p
2979                                        || declspecs->restrict_p))
2980     {
2981       warning (0, "useless type qualifier in empty declaration");
2982       warned = 2;
2983     }
2984
2985   if (warned != 1)
2986     {
2987       if (!found_tag)
2988         pedwarn (input_location, 0, "empty declaration");
2989     }
2990 }
2991 \f
2992
2993 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
2994    bits.  SPECS represents declaration specifiers that the grammar
2995    only permits to contain type qualifiers and attributes.  */
2996
2997 int
2998 quals_from_declspecs (const struct c_declspecs *specs)
2999 {
3000   int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
3001                | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
3002                | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0));
3003   gcc_assert (!specs->type
3004               && !specs->decl_attr
3005               && specs->typespec_word == cts_none
3006               && specs->storage_class == csc_none
3007               && !specs->typedef_p
3008               && !specs->explicit_signed_p
3009               && !specs->deprecated_p
3010               && !specs->long_p
3011               && !specs->long_long_p
3012               && !specs->short_p
3013               && !specs->signed_p
3014               && !specs->unsigned_p
3015               && !specs->complex_p
3016               && !specs->inline_p
3017               && !specs->thread_p);
3018   return quals;
3019 }
3020
3021 /* Construct an array declarator.  EXPR is the expression inside [],
3022    or NULL_TREE.  QUALS are the type qualifiers inside the [] (to be
3023    applied to the pointer to which a parameter array is converted).
3024    STATIC_P is true if "static" is inside the [], false otherwise.
3025    VLA_UNSPEC_P is true if the array is [*], a VLA of unspecified
3026    length which is nevertheless a complete type, false otherwise.  The
3027    field for the contained declarator is left to be filled in by
3028    set_array_declarator_inner.  */
3029
3030 struct c_declarator *
3031 build_array_declarator (tree expr, struct c_declspecs *quals, bool static_p,
3032                         bool vla_unspec_p)
3033 {
3034   struct c_declarator *declarator = XOBNEW (&parser_obstack,
3035                                             struct c_declarator);
3036   declarator->kind = cdk_array;
3037   declarator->declarator = 0;
3038   declarator->u.array.dimen = expr;
3039   if (quals)
3040     {
3041       declarator->u.array.attrs = quals->attrs;
3042       declarator->u.array.quals = quals_from_declspecs (quals);
3043     }
3044   else
3045     {
3046       declarator->u.array.attrs = NULL_TREE;
3047       declarator->u.array.quals = 0;
3048     }
3049   declarator->u.array.static_p = static_p;
3050   declarator->u.array.vla_unspec_p = vla_unspec_p;
3051   if (!flag_isoc99)
3052     {
3053       if (static_p || quals != NULL)
3054         pedwarn (input_location, OPT_pedantic,
3055                  "ISO C90 does not support %<static%> or type "
3056                  "qualifiers in parameter array declarators");
3057       if (vla_unspec_p)
3058         pedwarn (input_location, OPT_pedantic,
3059                  "ISO C90 does not support %<[*]%> array declarators");
3060     }
3061   if (vla_unspec_p)
3062     {
3063       if (!current_scope->parm_flag)
3064         {
3065           /* C99 6.7.5.2p4 */
3066           error ("%<[*]%> not allowed in other than function prototype scope");
3067           declarator->u.array.vla_unspec_p = false;
3068           return NULL;
3069         }
3070       current_scope->had_vla_unspec = true;
3071     }
3072   return declarator;
3073 }
3074
3075 /* Set the contained declarator of an array declarator.  DECL is the
3076    declarator, as constructed by build_array_declarator; INNER is what
3077    appears on the left of the [].  */
3078
3079 struct c_declarator *
3080 set_array_declarator_inner (struct c_declarator *decl,
3081                             struct c_declarator *inner)
3082 {
3083   decl->declarator = inner;
3084   return decl;
3085 }
3086
3087 /* INIT is a constructor that forms DECL's initializer.  If the final
3088    element initializes a flexible array field, add the size of that
3089    initializer to DECL's size.  */
3090
3091 static void
3092 add_flexible_array_elts_to_size (tree decl, tree init)
3093 {
3094   tree elt, type;
3095
3096   if (VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (init)))
3097     return;
3098
3099   elt = VEC_last (constructor_elt, CONSTRUCTOR_ELTS (init))->value;
3100   type = TREE_TYPE (elt);
3101   if (TREE_CODE (type) == ARRAY_TYPE
3102       && TYPE_SIZE (type) == NULL_TREE
3103       && TYPE_DOMAIN (type) != NULL_TREE
3104       && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
3105     {
3106       complete_array_type (&type, elt, false);
3107       DECL_SIZE (decl)
3108         = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
3109       DECL_SIZE_UNIT (decl)
3110         = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
3111     }
3112 }
3113 \f
3114 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
3115    Set *EXPR, if EXPR not NULL, to any expression to be evaluated
3116    before the type name, and set *EXPR_CONST_OPERANDS, if
3117    EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
3118    appear in a constant expression.  */
3119
3120 tree
3121 groktypename (struct c_type_name *type_name, tree *expr,
3122               bool *expr_const_operands)
3123 {
3124   tree type;
3125   tree attrs = type_name->specs->attrs;
3126
3127   type_name->specs->attrs = NULL_TREE;
3128
3129   type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
3130                          false, NULL, &attrs, expr, expr_const_operands,
3131                          DEPRECATED_NORMAL);
3132
3133   /* Apply attributes.  */
3134   decl_attributes (&type, attrs, 0);
3135
3136   return type;
3137 }
3138
3139 /* Decode a declarator in an ordinary declaration or data definition.
3140    This is called as soon as the type information and variable name
3141    have been parsed, before parsing the initializer if any.
3142    Here we create the ..._DECL node, fill in its type,
3143    and put it on the list of decls for the current context.
3144    The ..._DECL node is returned as the value.
3145
3146    Exception: for arrays where the length is not specified,
3147    the type is left null, to be filled in by `finish_decl'.
3148
3149    Function definitions do not come here; they go to start_function
3150    instead.  However, external and forward declarations of functions
3151    do go through here.  Structure field declarations are done by
3152    grokfield and not through here.  */
3153
3154 tree
3155 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
3156             bool initialized, tree attributes)
3157 {
3158   tree decl;
3159   tree tem;
3160   tree expr = NULL_TREE;
3161   enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
3162
3163   /* An object declared as __attribute__((deprecated)) suppresses
3164      warnings of uses of other deprecated items.  */
3165   if (lookup_attribute ("deprecated", attributes))
3166     deprecated_state = DEPRECATED_SUPPRESS;
3167
3168   decl = grokdeclarator (declarator, declspecs,
3169                          NORMAL, initialized, NULL, &attributes, &expr, NULL,
3170                          deprecated_state);
3171   if (!decl)
3172     return 0;
3173
3174   if (expr)
3175     add_stmt (expr);
3176
3177   if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl)))
3178     warning (OPT_Wmain, "%q+D is usually a function", decl);
3179
3180   if (initialized)
3181     /* Is it valid for this decl to have an initializer at all?
3182        If not, set INITIALIZED to zero, which will indirectly
3183        tell 'finish_decl' to ignore the initializer once it is parsed.  */
3184     switch (TREE_CODE (decl))
3185       {
3186       case TYPE_DECL:
3187         error ("typedef %qD is initialized (use __typeof__ instead)", decl);
3188         initialized = 0;
3189         break;
3190
3191       case FUNCTION_DECL:
3192         error ("function %qD is initialized like a variable", decl);
3193         initialized = 0;
3194         break;
3195
3196       case PARM_DECL:
3197         /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE.  */
3198         error ("parameter %qD is initialized", decl);
3199         initialized = 0;
3200         break;
3201
3202       default:
3203         /* Don't allow initializations for incomplete types except for
3204            arrays which might be completed by the initialization.  */
3205
3206         /* This can happen if the array size is an undefined macro.
3207            We already gave a warning, so we don't need another one.  */
3208         if (TREE_TYPE (decl) == error_mark_node)
3209           initialized = 0;
3210         else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
3211           {
3212             /* A complete type is ok if size is fixed.  */
3213
3214             if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
3215                 || C_DECL_VARIABLE_SIZE (decl))
3216               {
3217                 error ("variable-sized object may not be initialized");
3218                 initialized = 0;
3219               }
3220           }
3221         else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
3222           {
3223             error ("variable %qD has initializer but incomplete type", decl);
3224             initialized = 0;
3225           }
3226         else if (C_DECL_VARIABLE_SIZE (decl))
3227           {
3228             /* Although C99 is unclear about whether incomplete arrays
3229                of VLAs themselves count as VLAs, it does not make
3230                sense to permit them to be initialized given that
3231                ordinary VLAs may not be initialized.  */
3232             error ("variable-sized object may not be initialized");
3233             initialized = 0;
3234           }
3235       }
3236
3237   if (initialized)
3238     {
3239       if (current_scope == file_scope)
3240         TREE_STATIC (decl) = 1;
3241
3242       /* Tell 'pushdecl' this is an initialized decl
3243          even though we don't yet have the initializer expression.
3244          Also tell 'finish_decl' it may store the real initializer.  */
3245       DECL_INITIAL (decl) = error_mark_node;
3246     }
3247
3248   /* If this is a function declaration, write a record describing it to the
3249      prototypes file (if requested).  */
3250
3251   if (TREE_CODE (decl) == FUNCTION_DECL)
3252     gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
3253
3254   /* ANSI specifies that a tentative definition which is not merged with
3255      a non-tentative definition behaves exactly like a definition with an
3256      initializer equal to zero.  (Section 3.7.2)
3257
3258      -fno-common gives strict ANSI behavior, though this tends to break
3259      a large body of code that grew up without this rule.
3260
3261      Thread-local variables are never common, since there's no entrenched
3262      body of code to break, and it allows more efficient variable references
3263      in the presence of dynamic linking.  */
3264
3265   if (TREE_CODE (decl) == VAR_DECL
3266       && !initialized
3267       && TREE_PUBLIC (decl)
3268       && !DECL_THREAD_LOCAL_P (decl)
3269       && !flag_no_common)
3270     DECL_COMMON (decl) = 1;
3271
3272   /* Set attributes here so if duplicate decl, will have proper attributes.  */
3273   decl_attributes (&decl, attributes, 0);
3274
3275   /* Handle gnu_inline attribute.  */
3276   if (declspecs->inline_p
3277       && !flag_gnu89_inline
3278       && TREE_CODE (decl) == FUNCTION_DECL
3279       && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
3280           || current_function_decl))
3281     {
3282       if (declspecs->storage_class == csc_auto && current_scope != file_scope)
3283         ;
3284       else if (declspecs->storage_class != csc_static)
3285         DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
3286     }
3287
3288   if (TREE_CODE (decl) == FUNCTION_DECL
3289       && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
3290     {
3291       struct c_declarator *ce = declarator;
3292
3293       if (ce->kind == cdk_pointer)
3294         ce = declarator->declarator;
3295       if (ce->kind == cdk_function)
3296         {
3297           tree args = ce->u.arg_info->parms;
3298           for (; args; args = TREE_CHAIN (args))
3299             {
3300               tree type = TREE_TYPE (args);
3301               if (type && INTEGRAL_TYPE_P (type)
3302                   && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
3303                 DECL_ARG_TYPE (args) = integer_type_node;
3304             }
3305         }
3306     }
3307
3308   if (TREE_CODE (decl) == FUNCTION_DECL
3309       && DECL_DECLARED_INLINE_P (decl)
3310       && DECL_UNINLINABLE (decl)
3311       && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
3312     warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
3313              decl);
3314
3315   /* C99 6.7.4p3: An inline definition of a function with external
3316      linkage shall not contain a definition of a modifiable object
3317      with static storage duration...  */
3318   if (TREE_CODE (decl) == VAR_DECL
3319       && current_scope != file_scope
3320       && TREE_STATIC (decl)
3321       && !TREE_READONLY (decl)
3322       && DECL_DECLARED_INLINE_P (current_function_decl)
3323       && DECL_EXTERNAL (current_function_decl))
3324     pedwarn (input_location, 0,
3325              "%q+D is static but declared in inline function %qD "
3326              "which is not static", decl, current_function_decl);
3327
3328   /* Add this decl to the current scope.
3329      TEM may equal DECL or it may be a previous decl of the same name.  */
3330   tem = pushdecl (decl);
3331
3332   if (initialized && DECL_EXTERNAL (tem))
3333     {
3334       DECL_EXTERNAL (tem) = 0;
3335       TREE_STATIC (tem) = 1;
3336     }
3337
3338   return tem;
3339 }
3340
3341 /* Initialize EH if not initialized yet and exceptions are enabled.  */
3342
3343 void
3344 c_maybe_initialize_eh (void)
3345 {
3346   if (!flag_exceptions || c_eh_initialized_p)
3347     return;
3348
3349   c_eh_initialized_p = true;
3350   eh_personality_libfunc
3351     = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3352                         ? "__gcc_personality_sj0"
3353                         : "__gcc_personality_v0");
3354   default_init_unwind_resume_libfunc ();
3355   using_eh_for_cleanups ();
3356 }
3357
3358 /* Finish processing of a declaration;
3359    install its initial value.
3360    If the length of an array type is not known before,
3361    it must be determined now, from the initial value, or it is an error.  */
3362
3363 void
3364 finish_decl (tree decl, tree init, tree asmspec_tree)
3365 {
3366   tree type;
3367   int was_incomplete = (DECL_SIZE (decl) == 0);
3368   const char *asmspec = 0;
3369
3370   /* If a name was specified, get the string.  */
3371   if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
3372       && DECL_FILE_SCOPE_P (decl))
3373     asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
3374   if (asmspec_tree)
3375     asmspec = TREE_STRING_POINTER (asmspec_tree);
3376
3377   /* If `start_decl' didn't like having an initialization, ignore it now.  */
3378   if (init != 0 && DECL_INITIAL (decl) == 0)
3379     init = 0;
3380
3381   /* Don't crash if parm is initialized.  */
3382   if (TREE_CODE (decl) == PARM_DECL)
3383     init = 0;
3384
3385   if (init)
3386     store_init_value (decl, init);
3387
3388   if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
3389                             || TREE_CODE (decl) == FUNCTION_DECL
3390                             || TREE_CODE (decl) == FIELD_DECL))
3391     objc_check_decl (decl);
3392
3393   type = TREE_TYPE (decl);
3394
3395   /* Deduce size of array from initialization, if not already known.  */
3396   if (TREE_CODE (type) == ARRAY_TYPE
3397       && TYPE_DOMAIN (type) == 0
3398       && TREE_CODE (decl) != TYPE_DECL)
3399     {
3400       bool do_default
3401         = (TREE_STATIC (decl)
3402            /* Even if pedantic, an external linkage array
3403               may have incomplete type at first.  */
3404            ? pedantic && !TREE_PUBLIC (decl)
3405            : !DECL_EXTERNAL (decl));
3406       int failure
3407         = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
3408                                do_default);
3409
3410       /* Get the completed type made by complete_array_type.  */
3411       type = TREE_TYPE (decl);
3412
3413       switch (failure)
3414         {
3415         case 1:
3416           error ("initializer fails to determine size of %q+D", decl);
3417           break;
3418
3419         case 2:
3420           if (do_default)
3421             error ("array size missing in %q+D", decl);
3422           /* If a `static' var's size isn't known,
3423              make it extern as well as static, so it does not get
3424              allocated.
3425              If it is not `static', then do not mark extern;
3426              finish_incomplete_decl will give it a default size
3427              and it will get allocated.  */
3428           else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
3429             DECL_EXTERNAL (decl) = 1;
3430           break;
3431
3432         case 3:
3433           error ("zero or negative size array %q+D", decl);
3434           break;
3435
3436         case 0:
3437           /* For global variables, update the copy of the type that
3438              exists in the binding.  */
3439           if (TREE_PUBLIC (decl))
3440             {
3441               struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
3442               while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
3443                 b_ext = b_ext->shadowed;
3444               if (b_ext)
3445                 {
3446                   if (b_ext->type)
3447                     b_ext->type = composite_type (b_ext->type, type);
3448                   else
3449                     b_ext->type = type;
3450                 }
3451             }
3452           break;
3453
3454         default:
3455           gcc_unreachable ();
3456         }
3457
3458       if (DECL_INITIAL (decl))
3459         TREE_TYPE (DECL_INITIAL (decl)) = type;
3460
3461       layout_decl (decl, 0);
3462     }
3463
3464   if (TREE_CODE (decl) == VAR_DECL)
3465     {
3466       if (init && TREE_CODE (init) == CONSTRUCTOR)
3467         add_flexible_array_elts_to_size (decl, init);
3468
3469       if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
3470           && COMPLETE_TYPE_P (TREE_TYPE (decl)))
3471         layout_decl (decl, 0);
3472
3473       if (DECL_SIZE (decl) == 0
3474           /* Don't give an error if we already gave one earlier.  */
3475           && TREE_TYPE (decl) != error_mark_node
3476           && (TREE_STATIC (decl)
3477               /* A static variable with an incomplete type
3478                  is an error if it is initialized.
3479                  Also if it is not file scope.
3480                  Otherwise, let it through, but if it is not `extern'
3481                  then it may cause an error message later.  */
3482               ? (DECL_INITIAL (decl) != 0
3483                  || !DECL_FILE_SCOPE_P (decl))
3484               /* An automatic variable with an incomplete type
3485                  is an error.  */
3486               : !DECL_EXTERNAL (decl)))
3487          {
3488            error ("storage size of %q+D isn%'t known", decl);
3489            TREE_TYPE (decl) = error_mark_node;
3490          }
3491
3492       if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
3493           && DECL_SIZE (decl) != 0)
3494         {
3495           if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
3496             constant_expression_warning (DECL_SIZE (decl));
3497           else
3498             {
3499               error ("storage size of %q+D isn%'t constant", decl);
3500               TREE_TYPE (decl) = error_mark_node;
3501             }
3502         }
3503
3504       if (TREE_USED (type))
3505         TREE_USED (decl) = 1;
3506     }
3507
3508   /* If this is a function and an assembler name is specified, reset DECL_RTL
3509      so we can give it its new name.  Also, update built_in_decls if it
3510      was a normal built-in.  */
3511   if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
3512     {
3513       if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
3514         set_builtin_user_assembler_name (decl, asmspec);
3515       set_user_assembler_name (decl, asmspec);
3516     }
3517
3518   /* If #pragma weak was used, mark the decl weak now.  */
3519   maybe_apply_pragma_weak (decl);
3520
3521   /* Output the assembler code and/or RTL code for variables and functions,
3522      unless the type is an undefined structure or union.
3523      If not, it will get done when the type is completed.  */
3524
3525   if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3526     {
3527       /* Determine the ELF visibility.  */
3528       if (TREE_PUBLIC (decl))
3529         c_determine_visibility (decl);
3530
3531       /* This is a no-op in c-lang.c or something real in objc-act.c.  */
3532       if (c_dialect_objc ())
3533         objc_check_decl (decl);
3534
3535       if (asmspec)
3536         {
3537           /* If this is not a static variable, issue a warning.
3538              It doesn't make any sense to give an ASMSPEC for an
3539              ordinary, non-register local variable.  Historically,
3540              GCC has accepted -- but ignored -- the ASMSPEC in
3541              this case.  */
3542           if (!DECL_FILE_SCOPE_P (decl)
3543               && TREE_CODE (decl) == VAR_DECL
3544               && !C_DECL_REGISTER (decl)
3545               && !TREE_STATIC (decl))
3546             warning (0, "ignoring asm-specifier for non-static local "
3547                      "variable %q+D", decl);
3548           else
3549             set_user_assembler_name (decl, asmspec);
3550         }
3551
3552       if (DECL_FILE_SCOPE_P (decl))
3553         {
3554           if (DECL_INITIAL (decl) == NULL_TREE
3555               || DECL_INITIAL (decl) == error_mark_node)
3556             /* Don't output anything
3557                when a tentative file-scope definition is seen.
3558                But at end of compilation, do output code for them.  */
3559             DECL_DEFER_OUTPUT (decl) = 1;
3560           rest_of_decl_compilation (decl, true, 0);
3561         }
3562       else
3563         {
3564           /* In conjunction with an ASMSPEC, the `register'
3565              keyword indicates that we should place the variable
3566              in a particular register.  */
3567           if (asmspec && C_DECL_REGISTER (decl))
3568             {
3569               DECL_HARD_REGISTER (decl) = 1;
3570               /* This cannot be done for a structure with volatile
3571                  fields, on which DECL_REGISTER will have been
3572                  reset.  */
3573               if (!DECL_REGISTER (decl))
3574                 error ("cannot put object with volatile field into register");
3575             }
3576
3577           if (TREE_CODE (decl) != FUNCTION_DECL)
3578             {
3579               /* If we're building a variable sized type, and we might be
3580                  reachable other than via the top of the current binding
3581                  level, then create a new BIND_EXPR so that we deallocate
3582                  the object at the right time.  */
3583               /* Note that DECL_SIZE can be null due to errors.  */
3584               if (DECL_SIZE (decl)
3585                   && !TREE_CONSTANT (DECL_SIZE (decl))
3586                   && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
3587                 {
3588                   tree bind;
3589                   bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
3590                   TREE_SIDE_EFFECTS (bind) = 1;
3591                   add_stmt (bind);
3592                   BIND_EXPR_BODY (bind) = push_stmt_list ();
3593                 }
3594               add_stmt (build_stmt (DECL_EXPR, decl));
3595             }
3596         }
3597
3598
3599       if (!DECL_FILE_SCOPE_P (decl))
3600         {
3601           /* Recompute the RTL of a local array now
3602              if it used to be an incomplete type.  */
3603           if (was_incomplete
3604               && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
3605             {
3606               /* If we used it already as memory, it must stay in memory.  */
3607               TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3608               /* If it's still incomplete now, no init will save it.  */
3609               if (DECL_SIZE (decl) == 0)
3610                 DECL_INITIAL (decl) = 0;
3611             }
3612         }
3613     }
3614
3615   if (TREE_CODE (decl) == TYPE_DECL)
3616     {
3617       if (!DECL_FILE_SCOPE_P (decl)
3618           && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3619         add_stmt (build_stmt (DECL_EXPR, decl));
3620
3621       rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
3622     }
3623
3624   /* At the end of a declaration, throw away any variable type sizes
3625      of types defined inside that declaration.  There is no use
3626      computing them in the following function definition.  */
3627   if (current_scope == file_scope)
3628     get_pending_sizes ();
3629
3630   /* Install a cleanup (aka destructor) if one was given.  */
3631   if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
3632     {
3633       tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
3634       if (attr)
3635         {
3636           tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
3637           tree cleanup_decl = lookup_name (cleanup_id);
3638           tree cleanup;
3639
3640           /* Build "cleanup(&decl)" for the destructor.  */
3641           cleanup = build_unary_op (input_location, ADDR_EXPR, decl, 0);
3642           cleanup = build_tree_list (NULL_TREE, cleanup);
3643           cleanup = build_function_call (cleanup_decl, cleanup);
3644
3645           /* Don't warn about decl unused; the cleanup uses it.  */
3646           TREE_USED (decl) = 1;
3647           TREE_USED (cleanup_decl) = 1;
3648
3649           /* Initialize EH, if we've been told to do so.  */
3650           c_maybe_initialize_eh ();
3651
3652           push_cleanup (decl, cleanup, false);
3653         }
3654     }
3655 }
3656
3657 /* Given a parsed parameter declaration, decode it into a PARM_DECL.  */
3658
3659 tree
3660 grokparm (const struct c_parm *parm)
3661 {
3662   tree attrs = parm->attrs;
3663   tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
3664                               NULL, &attrs, NULL, NULL, DEPRECATED_NORMAL);
3665
3666   decl_attributes (&decl, attrs, 0);
3667
3668   return decl;
3669 }
3670
3671 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3672    and push that on the current scope.  */
3673
3674 void
3675 push_parm_decl (const struct c_parm *parm)
3676 {
3677   tree attrs = parm->attrs;
3678   tree decl;
3679
3680   decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
3681                          &attrs, NULL, NULL, DEPRECATED_NORMAL);
3682   decl_attributes (&decl, attrs, 0);
3683
3684   decl = pushdecl (decl);
3685
3686   finish_decl (decl, NULL_TREE, NULL_TREE);
3687 }
3688
3689 /* Mark all the parameter declarations to date as forward decls.
3690    Also diagnose use of this extension.  */
3691
3692 void
3693 mark_forward_parm_decls (void)
3694 {
3695   struct c_binding *b;
3696
3697   if (pedantic && !current_scope->warned_forward_parm_decls)
3698     {
3699       pedwarn (input_location, OPT_pedantic,
3700                "ISO C forbids forward parameter declarations");
3701       current_scope->warned_forward_parm_decls = true;
3702     }
3703
3704   for (b = current_scope->bindings; b; b = b->prev)
3705     if (TREE_CODE (b->decl) == PARM_DECL)
3706       TREE_ASM_WRITTEN (b->decl) = 1;
3707 }
3708 \f
3709 /* Build a COMPOUND_LITERAL_EXPR.  TYPE is the type given in the compound
3710    literal, which may be an incomplete array type completed by the
3711    initializer; INIT is a CONSTRUCTOR that initializes the compound
3712    literal.  NON_CONST is true if the initializers contain something
3713    that cannot occur in a constant expression.  */
3714
3715 tree
3716 build_compound_literal (tree type, tree init, bool non_const)
3717 {
3718   /* We do not use start_decl here because we have a type, not a declarator;
3719      and do not use finish_decl because the decl should be stored inside
3720      the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR.  */
3721   tree decl;
3722   tree complit;
3723   tree stmt;
3724
3725   if (type == error_mark_node)
3726     return error_mark_node;
3727
3728   decl = build_decl (VAR_DECL, NULL_TREE, type);
3729   DECL_EXTERNAL (decl) = 0;
3730   TREE_PUBLIC (decl) = 0;
3731   TREE_STATIC (decl) = (current_scope == file_scope);
3732   DECL_CONTEXT (decl) = current_function_decl;
3733   TREE_USED (decl) = 1;
3734   TREE_TYPE (decl) = type;
3735   TREE_READONLY (decl) = TYPE_READONLY (type);
3736   store_init_value (decl, init);
3737
3738   if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3739     {
3740       int failure = complete_array_type (&TREE_TYPE (decl),
3741                                          DECL_INITIAL (decl), true);
3742       gcc_assert (!failure);
3743
3744       type = TREE_TYPE (decl);
3745       TREE_TYPE (DECL_INITIAL (decl)) = type;
3746     }
3747
3748   if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3749     return error_mark_node;
3750
3751   stmt = build_stmt (DECL_EXPR, decl);
3752   complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
3753   TREE_SIDE_EFFECTS (complit) = 1;
3754
3755   layout_decl (decl, 0);
3756
3757   if (TREE_STATIC (decl))
3758     {
3759       /* This decl needs a name for the assembler output.  */
3760       set_compound_literal_name (decl);
3761       DECL_DEFER_OUTPUT (decl) = 1;
3762       DECL_COMDAT (decl) = 1;
3763       DECL_ARTIFICIAL (decl) = 1;
3764       DECL_IGNORED_P (decl) = 1;
3765       pushdecl (decl);
3766       rest_of_decl_compilation (decl, 1, 0);
3767     }
3768
3769   if (non_const)
3770     {
3771       complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
3772       C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
3773     }
3774
3775   return complit;
3776 }
3777 \f
3778 /* Determine whether TYPE is a structure with a flexible array member,
3779    or a union containing such a structure (possibly recursively).  */
3780
3781 static bool
3782 flexible_array_type_p (tree type)
3783 {
3784   tree x;
3785   switch (TREE_CODE (type))
3786     {
3787     case RECORD_TYPE:
3788       x = TYPE_FIELDS (type);
3789       if (x == NULL_TREE)
3790         return false;
3791       while (TREE_CHAIN (x) != NULL_TREE)
3792         x = TREE_CHAIN (x);
3793       if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3794           && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3795           && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3796           && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3797         return true;
3798       return false;
3799     case UNION_TYPE:
3800       for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3801         {
3802           if (flexible_array_type_p (TREE_TYPE (x)))
3803             return true;
3804         }
3805       return false;
3806     default:
3807     return false;
3808   }
3809 }
3810 \f
3811 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3812    replacing with appropriate values if they are invalid.  */
3813 static void
3814 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3815 {
3816   tree type_mv;
3817   unsigned int max_width;
3818   unsigned HOST_WIDE_INT w;
3819   const char *name = orig_name ? orig_name: _("<anonymous>");
3820
3821   /* Detect and ignore out of range field width and process valid
3822      field widths.  */
3823   if (!INTEGRAL_TYPE_P (TREE_TYPE (*width))
3824       || TREE_CODE (*width) != INTEGER_CST)
3825     {
3826       error ("bit-field %qs width not an integer constant", name);
3827       *width = integer_one_node;
3828     }
3829   else
3830     {
3831       constant_expression_warning (*width);
3832       if (tree_int_cst_sgn (*width) < 0)
3833         {
3834           error ("negative width in bit-field %qs", name);
3835           *width = integer_one_node;
3836         }
3837       else if (integer_zerop (*width) && orig_name)
3838         {
3839           error ("zero width for bit-field %qs", name);
3840           *width = integer_one_node;
3841         }
3842     }
3843
3844   /* Detect invalid bit-field type.  */
3845   if (TREE_CODE (*type) != INTEGER_TYPE
3846       && TREE_CODE (*type) != BOOLEAN_TYPE
3847       && TREE_CODE (*type) != ENUMERAL_TYPE)
3848     {
3849       error ("bit-field %qs has invalid type", name);
3850       *type = unsigned_type_node;
3851     }
3852
3853   type_mv = TYPE_MAIN_VARIANT (*type);
3854   if (!in_system_header
3855       && type_mv != integer_type_node
3856       && type_mv != unsigned_type_node
3857       && type_mv != boolean_type_node)
3858     pedwarn (input_location, OPT_pedantic,
3859              "type of bit-field %qs is a GCC extension", name);
3860
3861   max_width = TYPE_PRECISION (*type);
3862
3863   if (0 < compare_tree_int (*width, max_width))
3864     {
3865       error ("width of %qs exceeds its type", name);
3866       w = max_width;
3867       *width = build_int_cst (NULL_TREE, w);
3868     }
3869   else
3870     w = tree_low_cst (*width, 1);
3871
3872   if (TREE_CODE (*type) == ENUMERAL_TYPE)
3873     {
3874       struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
3875       if (!lt
3876           || w < tree_int_cst_min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
3877           || w < tree_int_cst_min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
3878         warning (0, "%qs is narrower than values of its type", name);
3879     }
3880 }
3881
3882 \f
3883
3884 /* Print warning about variable length array if necessary.  */
3885
3886 static void
3887 warn_variable_length_array (const char *name, tree size)
3888 {
3889   int const_size = TREE_CONSTANT (size);
3890
3891   if (!flag_isoc99 && pedantic && warn_vla != 0)
3892     {
3893       if (const_size)
3894         {
3895           if (name)
3896             pedwarn (input_location, OPT_Wvla, "ISO C90 forbids array %qs whose size "
3897                      "can%'t be evaluated",
3898                      name);
3899           else
3900             pedwarn (input_location, OPT_Wvla, "ISO C90 forbids array whose size "
3901                      "can%'t be evaluated");
3902         }
3903       else
3904         {
3905           if (name) 
3906             pedwarn (input_location, OPT_Wvla, "ISO C90 forbids variable length array %qs",
3907                      name);
3908           else
3909             pedwarn (input_location, OPT_Wvla, "ISO C90 forbids variable length array");
3910         }
3911     }
3912   else if (warn_vla > 0)
3913     {
3914       if (const_size)
3915         {
3916           if (name)
3917             warning (OPT_Wvla,
3918                      "the size of array %qs can"
3919                      "%'t be evaluated", name);
3920           else
3921             warning (OPT_Wvla,
3922                      "the size of array can %'t be evaluated");
3923         }
3924       else
3925         {
3926           if (name)
3927             warning (OPT_Wvla,
3928                      "variable length array %qs is used",
3929                      name);
3930           else
3931             warning (OPT_Wvla,
3932                      "variable length array is used");
3933         }
3934     }
3935 }
3936
3937 /* Given declspecs and a declarator,
3938    determine the name and type of the object declared
3939    and construct a ..._DECL node for it.
3940    (In one case we can return a ..._TYPE node instead.
3941     For invalid input we sometimes return 0.)
3942
3943    DECLSPECS is a c_declspecs structure for the declaration specifiers.
3944
3945    DECL_CONTEXT says which syntactic context this declaration is in:
3946      NORMAL for most contexts.  Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3947      FUNCDEF for a function definition.  Like NORMAL but a few different
3948       error messages in each case.  Return value may be zero meaning
3949       this definition is too screwy to try to parse.
3950      PARM for a parameter declaration (either within a function prototype
3951       or before a function body).  Make a PARM_DECL, or return void_type_node.
3952      TYPENAME if for a typename (in a cast or sizeof).
3953       Don't make a DECL node; just return the ..._TYPE node.
3954      FIELD for a struct or union field; make a FIELD_DECL.
3955    INITIALIZED is true if the decl has an initializer.
3956    WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3957    representing the width of the bit-field.
3958    DECL_ATTRS points to the list of attributes that should be added to this
3959      decl.  Any nested attributes that belong on the decl itself will be
3960      added to this list.
3961    If EXPR is not NULL, any expressions that need to be evaluated as
3962      part of evaluating variably modified types will be stored in *EXPR.
3963    If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
3964      set to indicate whether operands in *EXPR can be used in constant
3965      expressions.
3966    DEPRECATED_STATE is a deprecated_states value indicating whether
3967    deprecation warnings should be suppressed.
3968
3969    In the TYPENAME case, DECLARATOR is really an absolute declarator.
3970    It may also be so in the PARM case, for a prototype where the
3971    argument type is specified but not the name.
3972
3973    This function is where the complicated C meanings of `static'
3974    and `extern' are interpreted.  */
3975
3976 static tree
3977 grokdeclarator (const struct c_declarator *declarator,
3978                 struct c_declspecs *declspecs,
3979                 enum decl_context decl_context, bool initialized, tree *width,
3980                 tree *decl_attrs, tree *expr, bool *expr_const_operands,
3981                 enum deprecated_states deprecated_state)
3982 {
3983   tree type = declspecs->type;
3984   bool threadp = declspecs->thread_p;
3985   enum c_storage_class storage_class = declspecs->storage_class;
3986   int constp;
3987   int restrictp;
3988   int volatilep;
3989   int type_quals = TYPE_UNQUALIFIED;
3990   const char *name, *orig_name;
3991   bool funcdef_flag = false;
3992   bool funcdef_syntax = false;
3993   int size_varies = 0;
3994   tree decl_attr = declspecs->decl_attr;
3995   int array_ptr_quals = TYPE_UNQUALIFIED;
3996   tree array_ptr_attrs = NULL_TREE;
3997   int array_parm_static = 0;
3998   bool array_parm_vla_unspec_p = false;
3999   tree returned_attrs = NULL_TREE;
4000   bool bitfield = width != NULL;
4001   tree element_type;
4002   struct c_arg_info *arg_info = 0;
4003   tree expr_dummy;
4004   bool expr_const_operands_dummy;
4005
4006   if (expr == NULL)
4007     expr = &expr_dummy;
4008   if (expr_const_operands == NULL)
4009     expr_const_operands = &expr_const_operands_dummy;
4010
4011   *expr = declspecs->expr;
4012   *expr_const_operands = declspecs->expr_const_operands;
4013
4014   if (decl_context == FUNCDEF)
4015     funcdef_flag = true, decl_context = NORMAL;
4016
4017   /* Look inside a declarator for the name being declared
4018      and get it as a string, for an error message.  */
4019   {
4020     const struct c_declarator *decl = declarator;
4021     name = 0;
4022
4023     while (decl)
4024       switch (decl->kind)
4025         {
4026         case cdk_function:
4027         case cdk_array:
4028         case cdk_pointer:
4029           funcdef_syntax = (decl->kind == cdk_function);
4030           decl = decl->declarator;
4031           break;
4032
4033         case cdk_attrs:
4034           decl = decl->declarator;
4035           break;
4036
4037         case cdk_id:
4038           if (decl->u.id)
4039             name = IDENTIFIER_POINTER (decl->u.id);
4040           decl = 0;
4041           break;
4042
4043         default:
4044           gcc_unreachable ();
4045         }
4046     orig_name = name;
4047     if (name == 0)
4048       name = "type name";
4049   }
4050
4051   /* A function definition's declarator must have the form of
4052      a function declarator.  */
4053
4054   if (funcdef_flag && !funcdef_syntax)
4055     return 0;
4056
4057   /* If this looks like a function definition, make it one,
4058      even if it occurs where parms are expected.
4059      Then store_parm_decls will reject it and not use it as a parm.  */
4060   if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
4061     decl_context = PARM;
4062
4063   if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
4064     warn_deprecated_use (declspecs->type);
4065
4066   if ((decl_context == NORMAL || decl_context == FIELD)
4067       && current_scope == file_scope
4068       && variably_modified_type_p (type, NULL_TREE))
4069     {
4070       error ("variably modified %qs at file scope", name);
4071       type = integer_type_node;
4072     }
4073
4074   size_varies = C_TYPE_VARIABLE_SIZE (type);
4075
4076   /* Diagnose defaulting to "int".  */
4077
4078   if (declspecs->default_int_p && !in_system_header)
4079     {
4080       /* Issue a warning if this is an ISO C 99 program or if
4081          -Wreturn-type and this is a function, or if -Wimplicit;
4082          prefer the former warning since it is more explicit.  */
4083       if ((warn_implicit_int || warn_return_type || flag_isoc99)
4084           && funcdef_flag)
4085         warn_about_return_type = 1;
4086       else 
4087         pedwarn_c99 (input_location, flag_isoc99 ? 0 : OPT_Wimplicit_int, 
4088                      "type defaults to %<int%> in declaration of %qs", name);
4089     }
4090
4091   /* Adjust the type if a bit-field is being declared,
4092      -funsigned-bitfields applied and the type is not explicitly
4093      "signed".  */
4094   if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
4095       && TREE_CODE (type) == INTEGER_TYPE)
4096     type = unsigned_type_for (type);
4097
4098   /* Figure out the type qualifiers for the declaration.  There are
4099      two ways a declaration can become qualified.  One is something
4100      like `const int i' where the `const' is explicit.  Another is
4101      something like `typedef const int CI; CI i' where the type of the
4102      declaration contains the `const'.  A third possibility is that
4103      there is a type qualifier on the element type of a typedefed
4104      array type, in which case we should extract that qualifier so
4105      that c_apply_type_quals_to_decl receives the full list of
4106      qualifiers to work with (C90 is not entirely clear about whether
4107      duplicate qualifiers should be diagnosed in this case, but it
4108      seems most appropriate to do so).  */
4109   element_type = strip_array_types (type);
4110   constp = declspecs->const_p + TYPE_READONLY (element_type);
4111   restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
4112   volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
4113   if (pedantic && !flag_isoc99)
4114     {
4115       if (constp > 1)
4116         pedwarn (input_location, OPT_pedantic, "duplicate %<const%>");
4117       if (restrictp > 1)
4118         pedwarn (input_location, OPT_pedantic, "duplicate %<restrict%>");
4119       if (volatilep > 1)
4120         pedwarn (input_location, OPT_pedantic, "duplicate %<volatile%>");
4121     }
4122   if (!flag_gen_aux_info && (TYPE_QUALS (element_type)))
4123     type = TYPE_MAIN_VARIANT (type);
4124   type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4125                 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4126                 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4127
4128   /* Warn about storage classes that are invalid for certain
4129      kinds of declarations (parameters, typenames, etc.).  */
4130
4131   if (funcdef_flag
4132       && (threadp
4133           || storage_class == csc_auto
4134           || storage_class == csc_register
4135           || storage_class == csc_typedef))
4136     {
4137       if (storage_class == csc_auto)
4138         pedwarn (input_location, 
4139                  (current_scope == file_scope) ? 0 : OPT_pedantic, 
4140                  "function definition declared %<auto%>");
4141       if (storage_class == csc_register)
4142         error ("function definition declared %<register%>");
4143       if (storage_class == csc_typedef)
4144         error ("function definition declared %<typedef%>");
4145       if (threadp)
4146         error ("function definition declared %<__thread%>");
4147       threadp = false;
4148       if (storage_class == csc_auto
4149           || storage_class == csc_register
4150           || storage_class == csc_typedef)
4151         storage_class = csc_none;
4152     }
4153   else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
4154     {
4155       if (decl_context == PARM && storage_class == csc_register)
4156         ;
4157       else
4158         {
4159           switch (decl_context)
4160             {
4161             case FIELD:
4162               error ("storage class specified for structure field %qs",
4163                      name);
4164               break;
4165             case PARM:
4166               error ("storage class specified for parameter %qs", name);
4167               break;
4168             default:
4169               error ("storage class specified for typename");
4170               break;
4171             }
4172           storage_class = csc_none;
4173           threadp = false;
4174         }
4175     }
4176   else if (storage_class == csc_extern
4177            && initialized
4178            && !funcdef_flag)
4179     {
4180       /* 'extern' with initialization is invalid if not at file scope.  */
4181        if (current_scope == file_scope)
4182          {
4183            /* It is fine to have 'extern const' when compiling at C
4184               and C++ intersection.  */
4185            if (!(warn_cxx_compat && constp))
4186              warning (0, "%qs initialized and declared %<extern%>", name);
4187          }
4188       else
4189         error ("%qs has both %<extern%> and initializer", name);
4190     }
4191   else if (current_scope == file_scope)
4192     {
4193       if (storage_class == csc_auto)
4194         error ("file-scope declaration of %qs specifies %<auto%>", name);
4195       if (pedantic && storage_class == csc_register)
4196         pedwarn (input_location, OPT_pedantic,
4197                  "file-scope declaration of %qs specifies %<register%>", name);
4198     }
4199   else
4200     {
4201       if (storage_class == csc_extern && funcdef_flag)
4202         error ("nested function %qs declared %<extern%>", name);
4203       else if (threadp && storage_class == csc_none)
4204         {
4205           error ("function-scope %qs implicitly auto and declared "
4206                  "%<__thread%>",
4207                  name);
4208           threadp = false;
4209         }
4210     }
4211
4212   /* Now figure out the structure of the declarator proper.
4213      Descend through it, creating more complex types, until we reach
4214      the declared identifier (or NULL_TREE, in an absolute declarator).
4215      At each stage we maintain an unqualified version of the type
4216      together with any qualifiers that should be applied to it with
4217      c_build_qualified_type; this way, array types including
4218      multidimensional array types are first built up in unqualified
4219      form and then the qualified form is created with
4220      TYPE_MAIN_VARIANT pointing to the unqualified form.  */
4221
4222   while (declarator && declarator->kind != cdk_id)
4223     {
4224       if (type == error_mark_node)
4225         {
4226           declarator = declarator->declarator;
4227           continue;
4228         }
4229
4230       /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
4231          a cdk_pointer (for *...),
4232          a cdk_function (for ...(...)),
4233          a cdk_attrs (for nested attributes),
4234          or a cdk_id (for the name being declared
4235          or the place in an absolute declarator
4236          where the name was omitted).
4237          For the last case, we have just exited the loop.
4238
4239          At this point, TYPE is the type of elements of an array,
4240          or for a function to return, or for a pointer to point to.
4241          After this sequence of ifs, TYPE is the type of the
4242          array or function or pointer, and DECLARATOR has had its
4243          outermost layer removed.  */
4244
4245       if (array_ptr_quals != TYPE_UNQUALIFIED
4246           || array_ptr_attrs != NULL_TREE
4247           || array_parm_static)
4248         {
4249           /* Only the innermost declarator (making a parameter be of
4250              array type which is converted to pointer type)
4251              may have static or type qualifiers.  */
4252           error ("static or type qualifiers in non-parameter array declarator");
4253           array_ptr_quals = TYPE_UNQUALIFIED;
4254           array_ptr_attrs = NULL_TREE;
4255           array_parm_static = 0;
4256         }
4257
4258       switch (declarator->kind)
4259         {
4260         case cdk_attrs:
4261           {
4262             /* A declarator with embedded attributes.  */
4263             tree attrs = declarator->u.attrs;
4264             const struct c_declarator *inner_decl;
4265             int attr_flags = 0;
4266             declarator = declarator->declarator;
4267             inner_decl = declarator;
4268             while (inner_decl->kind == cdk_attrs)
4269               inner_decl = inner_decl->declarator;
4270             if (inner_decl->kind == cdk_id)
4271               attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
4272             else if (inner_decl->kind == cdk_function)
4273               attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
4274             else if (inner_decl->kind == cdk_array)
4275               attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
4276             returned_attrs = decl_attributes (&type,
4277                                               chainon (returned_attrs, attrs),
4278                                               attr_flags);
4279             break;
4280           }
4281         case cdk_array:
4282           {
4283             tree itype = NULL_TREE;
4284             tree size = declarator->u.array.dimen;
4285             /* The index is a signed object `sizetype' bits wide.  */
4286             tree index_type = c_common_signed_type (sizetype);
4287
4288             array_ptr_quals = declarator->u.array.quals;
4289             array_ptr_attrs = declarator->u.array.attrs;
4290             array_parm_static = declarator->u.array.static_p;
4291             array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
4292
4293             declarator = declarator->declarator;
4294
4295             /* Check for some types that there cannot be arrays of.  */
4296
4297             if (VOID_TYPE_P (type))
4298               {
4299                 error ("declaration of %qs as array of voids", name);
4300                 type = error_mark_node;
4301               }
4302
4303             if (TREE_CODE (type) == FUNCTION_TYPE)
4304               {
4305                 error ("declaration of %qs as array of functions", name);
4306                 type = error_mark_node;
4307               }
4308
4309             if (pedantic && !in_system_header && flexible_array_type_p (type))
4310               pedwarn (input_location, OPT_pedantic,
4311                        "invalid use of structure with flexible array member");
4312
4313             if (size == error_mark_node)
4314               type = error_mark_node;
4315
4316             if (type == error_mark_node)
4317               continue;
4318
4319             /* If size was specified, set ITYPE to a range-type for
4320                that size.  Otherwise, ITYPE remains null.  finish_decl
4321                may figure it out from an initial value.  */
4322
4323             if (size)
4324               {
4325                 bool size_maybe_const = true;
4326                 bool size_int_const = (TREE_CODE (size) == INTEGER_CST
4327                                        && !TREE_OVERFLOW (size));
4328                 bool this_size_varies = false;
4329
4330                 /* Strip NON_LVALUE_EXPRs since we aren't using as an
4331                    lvalue.  */
4332                 STRIP_TYPE_NOPS (size);
4333
4334                 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
4335                   {
4336                     error ("size of array %qs has non-integer type", name);
4337                     size = integer_one_node;
4338                   }
4339
4340                 size = c_fully_fold (size, false, &size_maybe_const);
4341
4342                 if (pedantic && size_maybe_const && integer_zerop (size))
4343                   pedwarn (input_location, OPT_pedantic,
4344                            "ISO C forbids zero-size array %qs", name);
4345
4346                 if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
4347                   {
4348                     constant_expression_warning (size);
4349                     if (tree_int_cst_sgn (size) < 0)
4350                       {
4351                         error ("size of array %qs is negative", name);
4352                         size = integer_one_node;
4353                       }
4354                     /* Handle a size folded to an integer constant but
4355                        not an integer constant expression.  */
4356                     if (!size_int_const)
4357                       {
4358                         /* If this is a file scope declaration of an
4359                            ordinary identifier, this is invalid code;
4360                            diagnosing it here and not subsequently
4361                            treating the type as variable-length avoids
4362                            more confusing diagnostics later.  */
4363                         if ((decl_context == NORMAL || decl_context == FIELD)
4364                             && current_scope == file_scope)
4365                           pedwarn (input_location, 0,
4366                                    "variably modified %qs at file scope", name);
4367                         else
4368                           this_size_varies = size_varies = 1;
4369                         warn_variable_length_array (orig_name, size);
4370                       }
4371                   }
4372                 else if ((decl_context == NORMAL || decl_context == FIELD)
4373                          && current_scope == file_scope)
4374                   {
4375                     error ("variably modified %qs at file scope", name);
4376                     size = integer_one_node;
4377                   }
4378                 else
4379                   {
4380                     /* Make sure the array size remains visibly
4381                        nonconstant even if it is (eg) a const variable
4382                        with known value.  */
4383                     this_size_varies = size_varies = 1;
4384                     warn_variable_length_array (orig_name, size);
4385                   }
4386
4387                 if (integer_zerop (size) && !this_size_varies)
4388                   {
4389                     /* A zero-length array cannot be represented with
4390                        an unsigned index type, which is what we'll
4391                        get with build_index_type.  Create an
4392                        open-ended range instead.  */
4393                     itype = build_range_type (sizetype, size, NULL_TREE);
4394                   }
4395                 else
4396                   {
4397                     /* Arrange for the SAVE_EXPR on the inside of the
4398                        MINUS_EXPR, which allows the -1 to get folded
4399                        with the +1 that happens when building TYPE_SIZE.  */
4400                     if (size_varies)
4401                       size = variable_size (size);
4402                     if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
4403                       size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
4404                                      integer_zero_node, size);
4405
4406                     /* Compute the maximum valid index, that is, size
4407                        - 1.  Do the calculation in index_type, so that
4408                        if it is a variable the computations will be
4409                        done in the proper mode.  */
4410                     itype = fold_build2 (MINUS_EXPR, index_type,
4411                                          convert (index_type, size),
4412                                          convert (index_type,
4413                                                   size_one_node));
4414
4415                     /* If that overflowed, the array is too big.  ???
4416                        While a size of INT_MAX+1 technically shouldn't
4417                        cause an overflow (because we subtract 1), the
4418                        overflow is recorded during the conversion to
4419                        index_type, before the subtraction.  Handling
4420                        this case seems like an unnecessary
4421                        complication.  */
4422                     if (TREE_CODE (itype) == INTEGER_CST
4423                         && TREE_OVERFLOW (itype))
4424                       {
4425                         error ("size of array %qs is too large", name);
4426                         type = error_mark_node;
4427                         continue;
4428                       }
4429
4430                     itype = build_index_type (itype);
4431                   }
4432                 if (this_size_varies)
4433                   {
4434                     if (*expr)
4435                       *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
4436                                       *expr, size);
4437                     else
4438                       *expr = size;
4439                     *expr_const_operands &= size_maybe_const;
4440                   }
4441               }
4442             else if (decl_context == FIELD)
4443               {
4444                 bool flexible_array_member = false;
4445                 if (array_parm_vla_unspec_p)
4446                   /* Field names can in fact have function prototype
4447                      scope so [*] is disallowed here through making
4448                      the field variably modified, not through being
4449                      something other than a declaration with function
4450                      prototype scope.  */
4451                   size_varies = 1;
4452                 else
4453                   {
4454                     const struct c_declarator *t = declarator;
4455                     while (t->kind == cdk_attrs)
4456                       t = t->declarator;
4457                     flexible_array_member = (t->kind == cdk_id);
4458                   }
4459                 if (flexible_array_member
4460                     && pedantic && !flag_isoc99 && !in_system_header)
4461                   pedwarn (input_location, OPT_pedantic,
4462                            "ISO C90 does not support flexible array members");
4463
4464                 /* ISO C99 Flexible array members are effectively
4465                    identical to GCC's zero-length array extension.  */
4466                 if (flexible_array_member || array_parm_vla_unspec_p)
4467                   itype = build_range_type (sizetype, size_zero_node,
4468                                             NULL_TREE);
4469               }
4470             else if (decl_context == PARM)
4471               {
4472                 if (array_parm_vla_unspec_p)
4473                   {
4474                     itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4475                     size_varies = 1;
4476                   }
4477               }
4478             else if (decl_context == TYPENAME)
4479               {
4480                 if (array_parm_vla_unspec_p)
4481                   {
4482                     /* C99 6.7.5.2p4 */
4483                     warning (0, "%<[*]%> not in a declaration");
4484                     /* We use this to avoid messing up with incomplete
4485                        array types of the same type, that would
4486                        otherwise be modified below.  */
4487                     itype = build_range_type (sizetype, size_zero_node,
4488                                               NULL_TREE);
4489                     size_varies = 1;
4490                   }
4491               }
4492
4493              /* Complain about arrays of incomplete types.  */
4494             if (!COMPLETE_TYPE_P (type))
4495               {
4496                 error ("array type has incomplete element type");
4497                 type = error_mark_node;
4498               }
4499             else
4500             /* When itype is NULL, a shared incomplete array type is
4501                returned for all array of a given type.  Elsewhere we
4502                make sure we don't complete that type before copying
4503                it, but here we want to make sure we don't ever
4504                modify the shared type, so we gcc_assert (itype)
4505                below.  */
4506               type = build_array_type (type, itype);
4507
4508             if (type != error_mark_node)
4509               {
4510                 if (size_varies)
4511                   {
4512                     /* It is ok to modify type here even if itype is
4513                        NULL: if size_varies, we're in a
4514                        multi-dimensional array and the inner type has
4515                        variable size, so the enclosing shared array type
4516                        must too.  */
4517                     if (size && TREE_CODE (size) == INTEGER_CST)
4518                       type
4519                         = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
4520                     C_TYPE_VARIABLE_SIZE (type) = 1;
4521                   }
4522
4523                 /* The GCC extension for zero-length arrays differs from
4524                    ISO flexible array members in that sizeof yields
4525                    zero.  */
4526                 if (size && integer_zerop (size))
4527                   {
4528                     gcc_assert (itype);
4529                     TYPE_SIZE (type) = bitsize_zero_node;
4530                     TYPE_SIZE_UNIT (type) = size_zero_node;
4531                   }
4532                 if (array_parm_vla_unspec_p)
4533                   {
4534                     gcc_assert (itype);
4535                     /* The type is complete.  C99 6.7.5.2p4  */
4536                     TYPE_SIZE (type) = bitsize_zero_node;
4537                     TYPE_SIZE_UNIT (type) = size_zero_node;
4538                   }
4539               }
4540
4541             if (decl_context != PARM
4542                 && (array_ptr_quals != TYPE_UNQUALIFIED
4543                     || array_ptr_attrs != NULL_TREE
4544                     || array_parm_static))
4545               {
4546                 error ("static or type qualifiers in non-parameter array declarator");
4547                 array_ptr_quals = TYPE_UNQUALIFIED;
4548                 array_ptr_attrs = NULL_TREE;
4549                 array_parm_static = 0;
4550               }
4551             break;
4552           }
4553         case cdk_function:
4554           {
4555             /* Say it's a definition only for the declarator closest
4556                to the identifier, apart possibly from some
4557                attributes.  */
4558             bool really_funcdef = false;
4559             tree arg_types;
4560             if (funcdef_flag)
4561               {
4562                 const struct c_declarator *t = declarator->declarator;
4563                 while (t->kind == cdk_attrs)
4564                   t = t->declarator;
4565                 really_funcdef = (t->kind == cdk_id);
4566               }
4567
4568             /* Declaring a function type.  Make sure we have a valid
4569                type for the function to return.  */
4570             if (type == error_mark_node)
4571               continue;
4572
4573             size_varies = 0;
4574
4575             /* Warn about some types functions can't return.  */
4576             if (TREE_CODE (type) == FUNCTION_TYPE)
4577               {
4578                 error ("%qs declared as function returning a function", name);
4579                 type = integer_type_node;
4580               }
4581             if (TREE_CODE (type) == ARRAY_TYPE)
4582               {
4583                 error ("%qs declared as function returning an array", name);
4584                 type = integer_type_node;
4585               }
4586
4587             /* Construct the function type and go to the next
4588                inner layer of declarator.  */
4589             arg_info = declarator->u.arg_info;
4590             arg_types = grokparms (arg_info, really_funcdef);
4591             if (really_funcdef)
4592               put_pending_sizes (arg_info->pending_sizes);
4593
4594             /* Type qualifiers before the return type of the function
4595                qualify the return type, not the function type.  */
4596             if (type_quals)
4597               {
4598                 /* Type qualifiers on a function return type are
4599                    normally permitted by the standard but have no
4600                    effect, so give a warning at -Wreturn-type.
4601                    Qualifiers on a void return type are banned on
4602                    function definitions in ISO C; GCC used to used
4603                    them for noreturn functions.  */
4604                 if (VOID_TYPE_P (type) && really_funcdef)
4605                   pedwarn (input_location, 0,
4606                            "function definition has qualified void return type");
4607                 else
4608                   warning (OPT_Wignored_qualifiers,
4609                            "type qualifiers ignored on function return type");
4610
4611                 type = c_build_qualified_type (type, type_quals);
4612               }
4613             type_quals = TYPE_UNQUALIFIED;
4614
4615             type = build_function_type (type, arg_types);
4616             declarator = declarator->declarator;
4617
4618             /* Set the TYPE_CONTEXTs for each tagged type which is local to
4619                the formal parameter list of this FUNCTION_TYPE to point to
4620                the FUNCTION_TYPE node itself.  */
4621             {
4622               tree link;
4623
4624               for (link = arg_info->tags;
4625                    link;
4626                    link = TREE_CHAIN (link))
4627                 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4628             }
4629             break;
4630           }
4631         case cdk_pointer:
4632           {
4633             /* Merge any constancy or volatility into the target type
4634                for the pointer.  */
4635
4636             if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4637                 && type_quals)
4638               pedwarn (input_location, OPT_pedantic,
4639                        "ISO C forbids qualified function types");
4640             if (type_quals)
4641               type = c_build_qualified_type (type, type_quals);
4642             size_varies = 0;
4643
4644             /* When the pointed-to type involves components of variable size,
4645                care must be taken to ensure that the size evaluation code is
4646                emitted early enough to dominate all the possible later uses
4647                and late enough for the variables on which it depends to have
4648                been assigned.
4649
4650                This is expected to happen automatically when the pointed-to
4651                type has a name/declaration of it's own, but special attention
4652                is required if the type is anonymous.
4653
4654                We handle the NORMAL and FIELD contexts here by attaching an
4655                artificial TYPE_DECL to such pointed-to type.  This forces the
4656                sizes evaluation at a safe point and ensures it is not deferred
4657                until e.g. within a deeper conditional context.
4658
4659                We expect nothing to be needed here for PARM or TYPENAME.
4660                Pushing a TYPE_DECL at this point for TYPENAME would actually
4661                be incorrect, as we might be in the middle of an expression
4662                with side effects on the pointed-to type size "arguments" prior
4663                to the pointer declaration point and the fake TYPE_DECL in the
4664                enclosing context would force the size evaluation prior to the
4665                side effects.  */
4666
4667             if (!TYPE_NAME (type)
4668                 && (decl_context == NORMAL || decl_context == FIELD)
4669                 && variably_modified_type_p (type, NULL_TREE))
4670               {
4671                 tree decl = build_decl (TYPE_DECL, NULL_TREE, type);
4672                 DECL_ARTIFICIAL (decl) = 1;
4673                 pushdecl (decl);
4674                 finish_decl (decl, NULL_TREE, NULL_TREE);
4675                 TYPE_NAME (type) = decl;
4676               }
4677
4678             type = build_pointer_type (type);
4679
4680             /* Process type qualifiers (such as const or volatile)
4681                that were given inside the `*'.  */
4682             type_quals = declarator->u.pointer_quals;
4683
4684             declarator = declarator->declarator;
4685             break;
4686           }
4687         default:
4688           gcc_unreachable ();
4689         }
4690     }
4691   *decl_attrs = chainon (returned_attrs, *decl_attrs);
4692
4693   /* Now TYPE has the actual type, apart from any qualifiers in
4694      TYPE_QUALS.  */
4695
4696   /* Check the type and width of a bit-field.  */
4697   if (bitfield)
4698     check_bitfield_type_and_width (&type, width, orig_name);
4699
4700   /* Did array size calculations overflow?  */
4701
4702   if (TREE_CODE (type) == ARRAY_TYPE
4703       && COMPLETE_TYPE_P (type)
4704       && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
4705       && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
4706     {
4707       error ("size of array %qs is too large", name);
4708       /* If we proceed with the array type as it is, we'll eventually
4709          crash in tree_low_cst().  */
4710       type = error_mark_node;
4711     }
4712
4713   /* If this is declaring a typedef name, return a TYPE_DECL.  */
4714
4715   if (storage_class == csc_typedef)
4716     {
4717       tree decl;
4718       if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4719           && type_quals)
4720         pedwarn (input_location, OPT_pedantic,
4721                  "ISO C forbids qualified function types");
4722       if (type_quals)
4723         type = c_build_qualified_type (type, type_quals);
4724       decl = build_decl (TYPE_DECL, declarator->u.id, type);
4725       DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4726       if (declspecs->explicit_signed_p)
4727         C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4728       if (declspecs->inline_p)
4729         pedwarn (input_location, 0,"typedef %q+D declared %<inline%>", decl);
4730       return decl;
4731     }
4732
4733   /* If this is a type name (such as, in a cast or sizeof),
4734      compute the type and return it now.  */
4735
4736   if (decl_context == TYPENAME)
4737     {
4738       /* Note that the grammar rejects storage classes in typenames
4739          and fields.  */
4740       gcc_assert (storage_class == csc_none && !threadp
4741                   && !declspecs->inline_p);
4742       if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4743           && type_quals)
4744         pedwarn (input_location, OPT_pedantic,
4745                  "ISO C forbids const or volatile function types");
4746       if (type_quals)
4747         type = c_build_qualified_type (type, type_quals);
4748       return type;
4749     }
4750
4751   if (pedantic && decl_context == FIELD
4752       && variably_modified_type_p (type, NULL_TREE))
4753     {
4754       /* C99 6.7.2.1p8 */
4755       pedwarn (input_location, OPT_pedantic, 
4756                "a member of a structure or union cannot have a variably modified type");
4757     }
4758
4759   /* Aside from typedefs and type names (handle above),
4760      `void' at top level (not within pointer)
4761      is allowed only in public variables.
4762      We don't complain about parms either, but that is because
4763      a better error message can be made later.  */
4764
4765   if (VOID_TYPE_P (type) && decl_context != PARM
4766       && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4767             && (storage_class == csc_extern
4768                 || (current_scope == file_scope
4769                     && !(storage_class == csc_static
4770                          || storage_class == csc_register)))))
4771     {
4772       error ("variable or field %qs declared void", name);
4773       type = integer_type_node;
4774     }
4775
4776   /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4777      or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE.  */
4778
4779   {
4780     tree decl;
4781
4782     if (decl_context == PARM)
4783       {
4784         tree promoted_type;
4785
4786         /* A parameter declared as an array of T is really a pointer to T.
4787            One declared as a function is really a pointer to a function.  */
4788
4789         if (TREE_CODE (type) == ARRAY_TYPE)
4790           {
4791             /* Transfer const-ness of array into that of type pointed to.  */
4792             type = TREE_TYPE (type);
4793             if (type_quals)
4794               type = c_build_qualified_type (type, type_quals);
4795             type = build_pointer_type (type);
4796             type_quals = array_ptr_quals;
4797             if (type_quals)
4798               type = c_build_qualified_type (type, type_quals);
4799
4800             /* We don't yet implement attributes in this context.  */
4801             if (array_ptr_attrs != NULL_TREE)
4802               warning (OPT_Wattributes,
4803                        "attributes in parameter array declarator ignored");
4804
4805             size_varies = 0;
4806           }
4807         else if (TREE_CODE (type) == FUNCTION_TYPE)
4808           {
4809             if (type_quals)
4810               pedwarn (input_location, OPT_pedantic,
4811                        "ISO C forbids qualified function types");
4812             if (type_quals)
4813               type = c_build_qualified_type (type, type_quals);
4814             type = build_pointer_type (type);
4815             type_quals = TYPE_UNQUALIFIED;
4816           }
4817         else if (type_quals)
4818           type = c_build_qualified_type (type, type_quals);
4819
4820         decl = build_decl (PARM_DECL, declarator->u.id, type);
4821         DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4822         if (size_varies)
4823           C_DECL_VARIABLE_SIZE (decl) = 1;
4824
4825         /* Compute the type actually passed in the parmlist,
4826            for the case where there is no prototype.
4827            (For example, shorts and chars are passed as ints.)
4828            When there is a prototype, this is overridden later.  */
4829
4830         if (type == error_mark_node)
4831           promoted_type = type;
4832         else
4833           promoted_type = c_type_promotes_to (type);
4834
4835         DECL_ARG_TYPE (decl) = promoted_type;
4836         if (declspecs->inline_p)
4837           pedwarn (input_location, 0, "parameter %q+D declared %<inline%>", decl);
4838       }
4839     else if (decl_context == FIELD)
4840       {
4841         /* Note that the grammar rejects storage classes in typenames
4842            and fields.  */
4843         gcc_assert (storage_class == csc_none && !threadp
4844                     && !declspecs->inline_p);
4845
4846         /* Structure field.  It may not be a function.  */
4847
4848         if (TREE_CODE (type) == FUNCTION_TYPE)
4849           {
4850             error ("field %qs declared as a function", name);
4851             type = build_pointer_type (type);
4852           }
4853         else if (TREE_CODE (type) != ERROR_MARK
4854                  && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4855           {
4856             error ("field %qs has incomplete type", name);
4857             type = error_mark_node;
4858           }
4859         type = c_build_qualified_type (type, type_quals);
4860         decl = build_decl (FIELD_DECL, declarator->u.id, type);
4861         DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4862         DECL_NONADDRESSABLE_P (decl) = bitfield;
4863         if (bitfield && !declarator->u.id)
4864           TREE_NO_WARNING (decl) = 1;
4865
4866         if (size_varies)
4867           C_DECL_VARIABLE_SIZE (decl) = 1;
4868       }
4869     else if (TREE_CODE (type) == FUNCTION_TYPE)
4870       {
4871         if (storage_class == csc_register || threadp)
4872           {
4873             error ("invalid storage class for function %qs", name);
4874            }
4875         else if (current_scope != file_scope)
4876           {
4877             /* Function declaration not at file scope.  Storage
4878                classes other than `extern' are not allowed, C99
4879                6.7.1p5, and `extern' makes no difference.  However,
4880                GCC allows 'auto', perhaps with 'inline', to support
4881                nested functions.  */
4882             if (storage_class == csc_auto)
4883                 pedwarn (input_location, OPT_pedantic, "invalid storage class for function %qs", name);
4884             else if (storage_class == csc_static)
4885               {
4886                 error ("invalid storage class for function %qs", name);
4887                 if (funcdef_flag)
4888                   storage_class = declspecs->storage_class = csc_none;
4889                 else
4890                   return 0;
4891               }
4892           }
4893
4894         decl = build_decl (FUNCTION_DECL, declarator->u.id, type);
4895         DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4896         decl = build_decl_attribute_variant (decl, decl_attr);
4897
4898         if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
4899           pedwarn (input_location, OPT_pedantic,
4900                    "ISO C forbids qualified function types");
4901
4902         /* GNU C interprets a volatile-qualified function type to indicate
4903            that the function does not return.  */
4904         if ((type_quals & TYPE_QUAL_VOLATILE)
4905             && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4906           warning (0, "%<noreturn%> function returns non-void value");
4907
4908         /* Every function declaration is an external reference
4909            (DECL_EXTERNAL) except for those which are not at file
4910            scope and are explicitly declared "auto".  This is
4911            forbidden by standard C (C99 6.7.1p5) and is interpreted by
4912            GCC to signify a forward declaration of a nested function.  */
4913         if (storage_class == csc_auto && current_scope != file_scope)
4914           DECL_EXTERNAL (decl) = 0;
4915         /* In C99, a function which is declared 'inline' with 'extern'
4916            is not an external reference (which is confusing).  It
4917            means that the later definition of the function must be output
4918            in this file, C99 6.7.4p6.  In GNU C89, a function declared
4919            'extern inline' is an external reference.  */
4920         else if (declspecs->inline_p && storage_class != csc_static)
4921           DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
4922                                   == flag_gnu89_inline);
4923         else
4924           DECL_EXTERNAL (decl) = !initialized;
4925
4926         /* Record absence of global scope for `static' or `auto'.  */
4927         TREE_PUBLIC (decl)
4928           = !(storage_class == csc_static || storage_class == csc_auto);
4929
4930         /* For a function definition, record the argument information
4931            block where store_parm_decls will look for it.  */
4932         if (funcdef_flag)
4933           current_function_arg_info = arg_info;
4934
4935         if (declspecs->default_int_p)
4936           C_FUNCTION_IMPLICIT_INT (decl) = 1;
4937
4938         /* Record presence of `inline', if it is reasonable.  */
4939         if (flag_hosted && MAIN_NAME_P (declarator->u.id))
4940           {
4941             if (declspecs->inline_p)
4942               pedwarn (input_location, 0, "cannot inline function %<main%>");
4943           }
4944         else if (declspecs->inline_p)
4945           /* Record that the function is declared `inline'.  */
4946           DECL_DECLARED_INLINE_P (decl) = 1;
4947       }
4948     else
4949       {
4950         /* It's a variable.  */
4951         /* An uninitialized decl with `extern' is a reference.  */
4952         int extern_ref = !initialized && storage_class == csc_extern;
4953
4954         type = c_build_qualified_type (type, type_quals);
4955
4956         /* C99 6.2.2p7: It is invalid (compile-time undefined
4957            behavior) to create an 'extern' declaration for a
4958            variable if there is a global declaration that is
4959            'static' and the global declaration is not visible.
4960            (If the static declaration _is_ currently visible,
4961            the 'extern' declaration is taken to refer to that decl.) */
4962         if (extern_ref && current_scope != file_scope)
4963           {
4964             tree global_decl  = identifier_global_value (declarator->u.id);
4965             tree visible_decl = lookup_name (declarator->u.id);
4966
4967             if (global_decl
4968                 && global_decl != visible_decl
4969                 && TREE_CODE (global_decl) == VAR_DECL
4970                 && !TREE_PUBLIC (global_decl))
4971               error ("variable previously declared %<static%> redeclared "
4972                      "%<extern%>");
4973           }
4974
4975         decl = build_decl (VAR_DECL, declarator->u.id, type);
4976         DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4977         if (size_varies)
4978           C_DECL_VARIABLE_SIZE (decl) = 1;
4979
4980         if (declspecs->inline_p)
4981           pedwarn (input_location, 0, "variable %q+D declared %<inline%>", decl);
4982
4983         /* At file scope, an initialized extern declaration may follow
4984            a static declaration.  In that case, DECL_EXTERNAL will be
4985            reset later in start_decl.  */
4986         DECL_EXTERNAL (decl) = (storage_class == csc_extern);
4987
4988         /* At file scope, the presence of a `static' or `register' storage
4989            class specifier, or the absence of all storage class specifiers
4990            makes this declaration a definition (perhaps tentative).  Also,
4991            the absence of `static' makes it public.  */
4992         if (current_scope == file_scope)
4993           {
4994             TREE_PUBLIC (decl) = storage_class != csc_static;
4995             TREE_STATIC (decl) = !extern_ref;
4996           }
4997         /* Not at file scope, only `static' makes a static definition.  */
4998         else
4999           {
5000             TREE_STATIC (decl) = (storage_class == csc_static);
5001             TREE_PUBLIC (decl) = extern_ref;
5002           }
5003
5004         if (threadp)
5005           DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
5006       }
5007
5008     if (storage_class == csc_extern
5009         && variably_modified_type_p (type, NULL_TREE))
5010       {
5011         /* C99 6.7.5.2p2 */
5012         error ("object with variably modified type must have no linkage");
5013       }
5014
5015     /* Record `register' declaration for warnings on &
5016        and in case doing stupid register allocation.  */
5017
5018     if (storage_class == csc_register)
5019       {
5020         C_DECL_REGISTER (decl) = 1;
5021         DECL_REGISTER (decl) = 1;
5022       }
5023
5024     /* Record constancy and volatility.  */
5025     c_apply_type_quals_to_decl (type_quals, decl);
5026
5027     /* If a type has volatile components, it should be stored in memory.
5028        Otherwise, the fact that those components are volatile
5029        will be ignored, and would even crash the compiler.
5030        Of course, this only makes sense on  VAR,PARM, and RESULT decl's.   */
5031     if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
5032         && (TREE_CODE (decl) == VAR_DECL ||  TREE_CODE (decl) == PARM_DECL
5033           || TREE_CODE (decl) == RESULT_DECL))
5034       {
5035         /* It is not an error for a structure with volatile fields to
5036            be declared register, but reset DECL_REGISTER since it
5037            cannot actually go in a register.  */
5038         int was_reg = C_DECL_REGISTER (decl);
5039         C_DECL_REGISTER (decl) = 0;
5040         DECL_REGISTER (decl) = 0;
5041         c_mark_addressable (decl);
5042         C_DECL_REGISTER (decl) = was_reg;
5043       }
5044
5045   /* This is the earliest point at which we might know the assembler
5046      name of a variable.  Thus, if it's known before this, die horribly.  */
5047     gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
5048
5049     return decl;
5050   }
5051 }
5052 \f
5053 /* Decode the parameter-list info for a function type or function definition.
5054    The argument is the value returned by `get_parm_info' (or made in c-parse.c
5055    if there is an identifier list instead of a parameter decl list).
5056    These two functions are separate because when a function returns
5057    or receives functions then each is called multiple times but the order
5058    of calls is different.  The last call to `grokparms' is always the one
5059    that contains the formal parameter names of a function definition.
5060
5061    Return a list of arg types to use in the FUNCTION_TYPE for this function.
5062
5063    FUNCDEF_FLAG is true for a function definition, false for
5064    a mere declaration.  A nonempty identifier-list gets an error message
5065    when FUNCDEF_FLAG is false.  */
5066
5067 static tree
5068 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
5069 {
5070   tree arg_types = arg_info->types;
5071
5072   if (funcdef_flag && arg_info->had_vla_unspec)
5073     {
5074       /* A function definition isn't function prototype scope C99 6.2.1p4.  */
5075       /* C99 6.7.5.2p4 */
5076       error ("%<[*]%> not allowed in other than function prototype scope");
5077     }
5078
5079   if (arg_types == 0 && !funcdef_flag && !in_system_header)
5080     warning (OPT_Wstrict_prototypes,
5081              "function declaration isn%'t a prototype");
5082
5083   if (arg_types == error_mark_node)
5084     return 0;  /* don't set TYPE_ARG_TYPES in this case */
5085
5086   else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
5087     {
5088       if (!funcdef_flag)
5089         pedwarn (input_location, 0, "parameter names (without types) in function declaration");
5090
5091       arg_info->parms = arg_info->types;
5092       arg_info->types = 0;
5093       return 0;
5094     }
5095   else
5096     {
5097       tree parm, type, typelt;
5098       unsigned int parmno;
5099
5100       /* If there is a parameter of incomplete type in a definition,
5101          this is an error.  In a declaration this is valid, and a
5102          struct or union type may be completed later, before any calls
5103          or definition of the function.  In the case where the tag was
5104          first declared within the parameter list, a warning has
5105          already been given.  If a parameter has void type, then
5106          however the function cannot be defined or called, so
5107          warn.  */
5108
5109       for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
5110            parm;
5111            parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
5112         {
5113           type = TREE_VALUE (typelt);
5114           if (type == error_mark_node)
5115             continue;
5116
5117           if (!COMPLETE_TYPE_P (type))
5118             {
5119               if (funcdef_flag)
5120                 {
5121                   if (DECL_NAME (parm))
5122                     error ("parameter %u (%q+D) has incomplete type",
5123                            parmno, parm);
5124                   else
5125                     error ("%Jparameter %u has incomplete type",
5126                            parm, parmno);
5127
5128                   TREE_VALUE (typelt) = error_mark_node;
5129                   TREE_TYPE (parm) = error_mark_node;
5130                 }
5131               else if (VOID_TYPE_P (type))
5132                 {
5133                   if (DECL_NAME (parm))
5134                     warning (0, "parameter %u (%q+D) has void type",
5135                              parmno, parm);
5136                   else
5137                     warning (0, "%Jparameter %u has void type",
5138                              parm, parmno);
5139                 }
5140             }
5141
5142           if (DECL_NAME (parm) && TREE_USED (parm))
5143             warn_if_shadowing (parm);
5144         }
5145       return arg_types;
5146     }
5147 }
5148
5149 /* Take apart the current scope and return a c_arg_info structure with
5150    info on a parameter list just parsed.
5151
5152    This structure is later fed to 'grokparms' and 'store_parm_decls'.
5153
5154    ELLIPSIS being true means the argument list ended in '...' so don't
5155    append a sentinel (void_list_node) to the end of the type-list.  */
5156
5157 struct c_arg_info *
5158 get_parm_info (bool ellipsis)
5159 {
5160   struct c_binding *b = current_scope->bindings;
5161   struct c_arg_info *arg_info = XOBNEW (&parser_obstack,
5162                                         struct c_arg_info);
5163   tree parms    = 0;
5164   tree tags     = 0;
5165   tree types    = 0;
5166   tree others   = 0;
5167
5168   static bool explained_incomplete_types = false;
5169   bool gave_void_only_once_err = false;
5170
5171   arg_info->parms = 0;
5172   arg_info->tags = 0;
5173   arg_info->types = 0;
5174   arg_info->others = 0;
5175   arg_info->pending_sizes = 0;
5176   arg_info->had_vla_unspec = current_scope->had_vla_unspec;
5177
5178   /* The bindings in this scope must not get put into a block.
5179      We will take care of deleting the binding nodes.  */
5180   current_scope->bindings = 0;
5181
5182   /* This function is only called if there was *something* on the
5183      parameter list.  */
5184   gcc_assert (b);
5185
5186   /* A parameter list consisting solely of 'void' indicates that the
5187      function takes no arguments.  But if the 'void' is qualified
5188      (by 'const' or 'volatile'), or has a storage class specifier
5189      ('register'), then the behavior is undefined; issue an error.
5190      Typedefs for 'void' are OK (see DR#157).  */
5191   if (b->prev == 0                          /* one binding */
5192       && TREE_CODE (b->decl) == PARM_DECL   /* which is a parameter */
5193       && !DECL_NAME (b->decl)               /* anonymous */
5194       && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
5195     {
5196       if (TREE_THIS_VOLATILE (b->decl)
5197           || TREE_READONLY (b->decl)
5198           || C_DECL_REGISTER (b->decl))
5199         error ("%<void%> as only parameter may not be qualified");
5200
5201       /* There cannot be an ellipsis.  */
5202       if (ellipsis)
5203         error ("%<void%> must be the only parameter");
5204
5205       arg_info->types = void_list_node;
5206       return arg_info;
5207     }
5208
5209   if (!ellipsis)
5210     types = void_list_node;
5211
5212   /* Break up the bindings list into parms, tags, types, and others;
5213      apply sanity checks; purge the name-to-decl bindings.  */
5214   while (b)
5215     {
5216       tree decl = b->decl;
5217       tree type = TREE_TYPE (decl);
5218       const char *keyword;
5219
5220       switch (TREE_CODE (decl))
5221         {
5222         case PARM_DECL:
5223           if (b->id)
5224             {
5225               gcc_assert (I_SYMBOL_BINDING (b->id) == b);
5226               I_SYMBOL_BINDING (b->id) = b->shadowed;
5227             }
5228
5229           /* Check for forward decls that never got their actual decl.  */
5230           if (TREE_ASM_WRITTEN (decl))
5231             error ("parameter %q+D has just a forward declaration", decl);
5232           /* Check for (..., void, ...) and issue an error.  */
5233           else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
5234             {
5235               if (!gave_void_only_once_err)
5236                 {
5237                   error ("%<void%> must be the only parameter");
5238                   gave_void_only_once_err = true;
5239                 }
5240             }
5241           else
5242             {
5243               /* Valid parameter, add it to the list.  */
5244               TREE_CHAIN (decl) = parms;
5245               parms = decl;
5246
5247               /* Since there is a prototype, args are passed in their
5248                  declared types.  The back end may override this later.  */
5249               DECL_ARG_TYPE (decl) = type;
5250               types = tree_cons (0, type, types);
5251             }
5252           break;
5253
5254         case ENUMERAL_TYPE: keyword = "enum"; goto tag;
5255         case UNION_TYPE:    keyword = "union"; goto tag;
5256         case RECORD_TYPE:   keyword = "struct"; goto tag;
5257         tag:
5258           /* Types may not have tag-names, in which case the type
5259              appears in the bindings list with b->id NULL.  */
5260           if (b->id)
5261             {
5262               gcc_assert (I_TAG_BINDING (b->id) == b);
5263               I_TAG_BINDING (b->id) = b->shadowed;
5264             }
5265
5266           /* Warn about any struct, union or enum tags defined in a
5267              parameter list.  The scope of such types is limited to
5268              the parameter list, which is rarely if ever desirable
5269              (it's impossible to call such a function with type-
5270              correct arguments).  An anonymous union parm type is
5271              meaningful as a GNU extension, so don't warn for that.  */
5272           if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
5273             {
5274               if (b->id)
5275                 /* The %s will be one of 'struct', 'union', or 'enum'.  */
5276                 warning (0, "%<%s %E%> declared inside parameter list",
5277                          keyword, b->id);
5278               else
5279                 /* The %s will be one of 'struct', 'union', or 'enum'.  */
5280                 warning (0, "anonymous %s declared inside parameter list",
5281                          keyword);
5282
5283               if (!explained_incomplete_types)
5284                 {
5285                   warning (0, "its scope is only this definition or declaration,"
5286                            " which is probably not what you want");
5287                   explained_incomplete_types = true;
5288                 }
5289             }
5290
5291           tags = tree_cons (b->id, decl, tags);
5292           break;
5293
5294         case CONST_DECL:
5295         case TYPE_DECL:
5296         case FUNCTION_DECL:
5297           /* CONST_DECLs appear here when we have an embedded enum,
5298              and TYPE_DECLs appear here when we have an embedded struct
5299              or union.  No warnings for this - we already warned about the
5300              type itself.  FUNCTION_DECLs appear when there is an implicit
5301              function declaration in the parameter list.  */
5302
5303           TREE_CHAIN (decl) = others;
5304           others = decl;
5305           /* fall through */
5306
5307         case ERROR_MARK:
5308           /* error_mark_node appears here when we have an undeclared
5309              variable.  Just throw it away.  */
5310           if (b->id)
5311             {
5312               gcc_assert (I_SYMBOL_BINDING (b->id) == b);
5313               I_SYMBOL_BINDING (b->id) = b->shadowed;
5314             }
5315           break;
5316
5317           /* Other things that might be encountered.  */
5318         case LABEL_DECL:
5319         case VAR_DECL:
5320         default:
5321           gcc_unreachable ();
5322         }
5323
5324       b = free_binding_and_advance (b);
5325     }
5326
5327   arg_info->parms = parms;
5328   arg_info->tags = tags;
5329   arg_info->types = types;
5330   arg_info->others = others;
5331   arg_info->pending_sizes = get_pending_sizes ();
5332   return arg_info;
5333 }
5334 \f
5335 /* Get the struct, enum or union (CODE says which) with tag NAME.
5336    Define the tag as a forward-reference if it is not defined.
5337    Return a c_typespec structure for the type specifier.  */
5338
5339 struct c_typespec
5340 parser_xref_tag (enum tree_code code, tree name)
5341 {
5342   struct c_typespec ret;
5343   tree ref;
5344
5345   ret.expr = NULL_TREE;
5346   ret.expr_const_operands = true;
5347
5348   /* If a cross reference is requested, look up the type
5349      already defined for this tag and return it.  */
5350
5351   ref = lookup_tag (code, name, 0);
5352   /* If this is the right type of tag, return what we found.
5353      (This reference will be shadowed by shadow_tag later if appropriate.)
5354      If this is the wrong type of tag, do not return it.  If it was the
5355      wrong type in the same scope, we will have had an error
5356      message already; if in a different scope and declaring
5357      a name, pending_xref_error will give an error message; but if in a
5358      different scope and not declaring a name, this tag should
5359      shadow the previous declaration of a different type of tag, and
5360      this would not work properly if we return the reference found.
5361      (For example, with "struct foo" in an outer scope, "union foo;"
5362      must shadow that tag with a new one of union type.)  */
5363   ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
5364   if (ref && TREE_CODE (ref) == code)
5365     {
5366       ret.spec = ref;
5367       return ret;
5368     }
5369
5370   /* If no such tag is yet defined, create a forward-reference node
5371      and record it as the "definition".
5372      When a real declaration of this type is found,
5373      the forward-reference will be altered into a real type.  */
5374
5375   ref = make_node (code);
5376   if (code == ENUMERAL_TYPE)
5377     {
5378       /* Give the type a default layout like unsigned int
5379          to avoid crashing if it does not get defined.  */
5380       SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
5381       TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
5382       TYPE_USER_ALIGN (ref) = 0;
5383       TYPE_UNSIGNED (ref) = 1;
5384       TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
5385       TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
5386       TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
5387     }
5388
5389   pushtag (name, ref);
5390
5391   ret.spec = ref;
5392   return ret;
5393 }
5394
5395 /* Get the struct, enum or union (CODE says which) with tag NAME.
5396    Define the tag as a forward-reference if it is not defined.
5397    Return a tree for the type.  */
5398
5399 tree
5400 xref_tag (enum tree_code code, tree name)
5401 {
5402   return parser_xref_tag (code, name).spec;
5403 }
5404 \f
5405 /* Make sure that the tag NAME is defined *in the current scope*
5406    at least as a forward reference.
5407    CODE says which kind of tag NAME ought to be.  */
5408
5409 tree
5410 start_struct (enum tree_code code, tree name)
5411 {
5412   /* If there is already a tag defined at this scope
5413      (as a forward reference), just return it.  */
5414
5415   tree ref = 0;
5416
5417   if (name != 0)
5418     ref = lookup_tag (code, name, 1);
5419   if (ref && TREE_CODE (ref) == code)
5420     {
5421       if (TYPE_SIZE (ref))
5422         {
5423           if (code == UNION_TYPE)
5424             error ("redefinition of %<union %E%>", name);
5425           else
5426             error ("redefinition of %<struct %E%>", name);
5427           /* Don't create structures using a name already in use.  */
5428           ref = NULL_TREE;
5429         }
5430       else if (C_TYPE_BEING_DEFINED (ref))
5431         {
5432           if (code == UNION_TYPE)
5433             error ("nested redefinition of %<union %E%>", name);
5434           else
5435             error ("nested redefinition of %<struct %E%>", name);
5436           /* Don't create structures that contain themselves.  */
5437           ref = NULL_TREE;
5438         }
5439     }
5440
5441   /* Otherwise create a forward-reference just so the tag is in scope.  */
5442
5443   if (ref == NULL_TREE || TREE_CODE (ref) != code)
5444     {
5445       ref = make_node (code);
5446       pushtag (name, ref);
5447     }
5448
5449   C_TYPE_BEING_DEFINED (ref) = 1;
5450   TYPE_PACKED (ref) = flag_pack_struct;
5451   return ref;
5452 }
5453
5454 /* Process the specs, declarator and width (NULL if omitted)
5455    of a structure component, returning a FIELD_DECL node.
5456    WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
5457    DECL_ATTRS is as for grokdeclarator.
5458
5459    LOC is the location of the structure component.
5460
5461    This is done during the parsing of the struct declaration.
5462    The FIELD_DECL nodes are chained together and the lot of them
5463    are ultimately passed to `build_struct' to make the RECORD_TYPE node.  */
5464
5465 tree
5466 grokfield (location_t loc,
5467            struct c_declarator *declarator, struct c_declspecs *declspecs,
5468            tree width, tree *decl_attrs)
5469 {
5470   tree value;
5471
5472   if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
5473       && width == NULL_TREE)
5474     {
5475       /* This is an unnamed decl.
5476
5477          If we have something of the form "union { list } ;" then this
5478          is the anonymous union extension.  Similarly for struct.
5479
5480          If this is something of the form "struct foo;", then
5481            If MS extensions are enabled, this is handled as an
5482              anonymous struct.
5483            Otherwise this is a forward declaration of a structure tag.
5484
5485          If this is something of the form "foo;" and foo is a TYPE_DECL, then
5486            If MS extensions are enabled and foo names a structure, then
5487              again this is an anonymous struct.
5488            Otherwise this is an error.
5489
5490          Oh what a horrid tangled web we weave.  I wonder if MS consciously
5491          took this from Plan 9 or if it was an accident of implementation
5492          that took root before someone noticed the bug...  */
5493
5494       tree type = declspecs->type;
5495       bool type_ok = (TREE_CODE (type) == RECORD_TYPE
5496                       || TREE_CODE (type) == UNION_TYPE);
5497       bool ok = false;
5498
5499       if (type_ok
5500           && (flag_ms_extensions || !declspecs->typedef_p))
5501         {
5502           if (flag_ms_extensions)
5503             ok = true;
5504           else if (flag_iso)
5505             ok = false;
5506           else if (TYPE_NAME (type) == NULL)
5507             ok = true;
5508           else
5509             ok = false;
5510         }
5511       if (!ok)
5512         {
5513           pedwarn (loc, 0, "declaration does not declare anything");
5514           return NULL_TREE;
5515         }
5516       pedwarn (loc, OPT_pedantic, "ISO C doesn%'t support unnamed structs/unions");
5517     }
5518
5519   value = grokdeclarator (declarator, declspecs, FIELD, false,
5520                           width ? &width : NULL, decl_attrs, NULL, NULL,
5521                           DEPRECATED_NORMAL);
5522
5523   finish_decl (value, NULL_TREE, NULL_TREE);
5524   DECL_INITIAL (value) = width;
5525
5526   return value;
5527 }
5528 \f
5529 /* Generate an error for any duplicate field names in FIELDLIST.  Munge
5530    the list such that this does not present a problem later.  */
5531
5532 static void
5533 detect_field_duplicates (tree fieldlist)
5534 {
5535   tree x, y;
5536   int timeout = 10;
5537
5538   /* First, see if there are more than "a few" fields.
5539      This is trivially true if there are zero or one fields.  */
5540   if (!fieldlist)
5541     return;
5542   x = TREE_CHAIN (fieldlist);
5543   if (!x)
5544     return;
5545   do {
5546     timeout--;
5547     x = TREE_CHAIN (x);
5548   } while (timeout > 0 && x);
5549
5550   /* If there were "few" fields, avoid the overhead of allocating
5551      a hash table.  Instead just do the nested traversal thing.  */
5552   if (timeout > 0)
5553     {
5554       for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
5555         if (DECL_NAME (x))
5556           {
5557             for (y = fieldlist; y != x; y = TREE_CHAIN (y))
5558               if (DECL_NAME (y) == DECL_NAME (x))
5559                 {
5560                   error ("duplicate member %q+D", x);
5561                   DECL_NAME (x) = NULL_TREE;
5562                 }
5563           }
5564     }
5565   else
5566     {
5567       htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
5568       void **slot;
5569
5570       for (x = fieldlist; x ; x = TREE_CHAIN (x))
5571         if ((y = DECL_NAME (x)) != 0)
5572           {
5573             slot = htab_find_slot (htab, y, INSERT);
5574             if (*slot)
5575               {
5576                 error ("duplicate member %q+D", x);
5577                 DECL_NAME (x) = NULL_TREE;
5578               }
5579             *slot = y;
5580           }
5581
5582       htab_delete (htab);
5583     }
5584 }
5585
5586 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5587    FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5588    ATTRIBUTES are attributes to be applied to the structure.  */
5589
5590 tree
5591 finish_struct (tree t, tree fieldlist, tree attributes)
5592 {
5593   tree x;
5594   bool toplevel = file_scope == current_scope;
5595   int saw_named_field;
5596
5597   /* If this type was previously laid out as a forward reference,
5598      make sure we lay it out again.  */
5599
5600   TYPE_SIZE (t) = 0;
5601
5602   decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5603
5604   if (pedantic)
5605     {
5606       for (x = fieldlist; x; x = TREE_CHAIN (x))
5607         if (DECL_NAME (x) != 0)
5608           break;
5609
5610       if (x == 0)
5611         {
5612           if (TREE_CODE (t) == UNION_TYPE)
5613             {
5614               if (fieldlist)
5615                 pedwarn (input_location, OPT_pedantic, "union has no named members");
5616               else
5617                 pedwarn (input_location, OPT_pedantic, "union has no members");
5618             }
5619           else
5620             {
5621               if (fieldlist)
5622                 pedwarn (input_location, OPT_pedantic, "struct has no named members");
5623               else
5624                 pedwarn (input_location, OPT_pedantic, "struct has no members");
5625             }
5626         }
5627     }
5628
5629   /* Install struct as DECL_CONTEXT of each field decl.
5630      Also process specified field sizes, found in the DECL_INITIAL,
5631      storing 0 there after the type has been changed to precision equal
5632      to its width, rather than the precision of the specified standard
5633      type.  (Correct layout requires the original type to have been preserved
5634      until now.)  */
5635
5636   saw_named_field = 0;
5637   for (x = fieldlist; x; x = TREE_CHAIN (x))
5638     {
5639       if (TREE_TYPE (x) == error_mark_node)
5640         continue;
5641
5642       DECL_CONTEXT (x) = t;
5643
5644       /* If any field is const, the structure type is pseudo-const.  */
5645       if (TREE_READONLY (x))
5646         C_TYPE_FIELDS_READONLY (t) = 1;
5647       else
5648         {
5649           /* A field that is pseudo-const makes the structure likewise.  */
5650           tree t1 = TREE_TYPE (x);
5651           while (TREE_CODE (t1) == ARRAY_TYPE)
5652             t1 = TREE_TYPE (t1);
5653           if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5654               && C_TYPE_FIELDS_READONLY (t1))
5655             C_TYPE_FIELDS_READONLY (t) = 1;
5656         }
5657
5658       /* Any field that is volatile means variables of this type must be
5659          treated in some ways as volatile.  */
5660       if (TREE_THIS_VOLATILE (x))
5661         C_TYPE_FIELDS_VOLATILE (t) = 1;
5662
5663       /* Any field of nominal variable size implies structure is too.  */
5664       if (C_DECL_VARIABLE_SIZE (x))
5665         C_TYPE_VARIABLE_SIZE (t) = 1;
5666
5667       if (DECL_INITIAL (x))
5668         {
5669           unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5670           DECL_SIZE (x) = bitsize_int (width);
5671           DECL_BIT_FIELD (x) = 1;
5672           SET_DECL_C_BIT_FIELD (x);
5673         }
5674
5675       if (TYPE_PACKED (t)
5676           && (DECL_BIT_FIELD (x)
5677               || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
5678         DECL_PACKED (x) = 1;
5679
5680       /* Detect flexible array member in an invalid context.  */
5681       if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5682           && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5683           && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5684           && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5685         {
5686           if (TREE_CODE (t) == UNION_TYPE)
5687             {
5688               error ("%Jflexible array member in union", x);
5689               TREE_TYPE (x) = error_mark_node;
5690             }
5691           else if (TREE_CHAIN (x) != NULL_TREE)
5692             {
5693               error ("%Jflexible array member not at end of struct", x);
5694               TREE_TYPE (x) = error_mark_node;
5695             }
5696           else if (!saw_named_field)
5697             {
5698               error ("%Jflexible array member in otherwise empty struct", x);
5699               TREE_TYPE (x) = error_mark_node;
5700             }
5701         }
5702
5703       if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE
5704           && flexible_array_type_p (TREE_TYPE (x)))
5705         pedwarn (input_location, OPT_pedantic, 
5706                  "%Jinvalid use of structure with flexible array member", x);
5707
5708       if (DECL_NAME (x))
5709         saw_named_field = 1;
5710     }
5711
5712   detect_field_duplicates (fieldlist);
5713
5714   /* Now we have the nearly final fieldlist.  Record it,
5715      then lay out the structure or union (including the fields).  */
5716
5717   TYPE_FIELDS (t) = fieldlist;
5718
5719   layout_type (t);
5720
5721   /* Give bit-fields their proper types.  */
5722   {
5723     tree *fieldlistp = &fieldlist;
5724     while (*fieldlistp)
5725       if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
5726           && TREE_TYPE (*fieldlistp) != error_mark_node)
5727         {
5728           unsigned HOST_WIDE_INT width
5729             = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
5730           tree type = TREE_TYPE (*fieldlistp);
5731           if (width != TYPE_PRECISION (type))
5732             {
5733               TREE_TYPE (*fieldlistp)
5734                 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
5735               DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
5736             }
5737           DECL_INITIAL (*fieldlistp) = 0;
5738         }
5739       else
5740         fieldlistp = &TREE_CHAIN (*fieldlistp);
5741   }
5742
5743   /* Now we have the truly final field list.
5744      Store it in this type and in the variants.  */
5745
5746   TYPE_FIELDS (t) = fieldlist;
5747
5748   /* If there are lots of fields, sort so we can look through them fast.
5749      We arbitrarily consider 16 or more elts to be "a lot".  */
5750
5751   {
5752     int len = 0;
5753
5754     for (x = fieldlist; x; x = TREE_CHAIN (x))
5755       {
5756         if (len > 15 || DECL_NAME (x) == NULL)
5757           break;
5758         len += 1;
5759       }
5760
5761     if (len > 15)
5762       {
5763         tree *field_array;
5764         struct lang_type *space;
5765         struct sorted_fields_type *space2;
5766
5767         len += list_length (x);
5768
5769         /* Use the same allocation policy here that make_node uses, to
5770           ensure that this lives as long as the rest of the struct decl.
5771           All decls in an inline function need to be saved.  */
5772
5773         space = GGC_CNEW (struct lang_type);
5774         space2 = GGC_NEWVAR (struct sorted_fields_type,
5775                              sizeof (struct sorted_fields_type) + len * sizeof (tree));
5776
5777         len = 0;
5778         space->s = space2;
5779         field_array = &space2->elts[0];
5780         for (x = fieldlist; x; x = TREE_CHAIN (x))
5781           {
5782             field_array[len++] = x;
5783
5784             /* If there is anonymous struct or union, break out of the loop.  */
5785             if (DECL_NAME (x) == NULL)
5786               break;
5787           }
5788         /* Found no anonymous struct/union.  Add the TYPE_LANG_SPECIFIC.  */
5789         if (x == NULL)
5790           {
5791             TYPE_LANG_SPECIFIC (t) = space;
5792             TYPE_LANG_SPECIFIC (t)->s->len = len;
5793             field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5794             qsort (field_array, len, sizeof (tree), field_decl_cmp);
5795           }
5796       }
5797   }
5798
5799   for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5800     {
5801       TYPE_FIELDS (x) = TYPE_FIELDS (t);
5802       TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5803       C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
5804       C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
5805       C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
5806     }
5807
5808   /* If this was supposed to be a transparent union, but we can't
5809      make it one, warn and turn off the flag.  */
5810   if (TREE_CODE (t) == UNION_TYPE
5811       && TYPE_TRANSPARENT_UNION (t)
5812       && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
5813     {
5814       TYPE_TRANSPARENT_UNION (t) = 0;
5815       warning (0, "union cannot be made transparent");
5816     }
5817
5818   /* If this structure or union completes the type of any previous
5819      variable declaration, lay it out and output its rtl.  */
5820   for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5821        x;
5822        x = TREE_CHAIN (x))
5823     {
5824       tree decl = TREE_VALUE (x);
5825       if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5826         layout_array_type (TREE_TYPE (decl));
5827       if (TREE_CODE (decl) != TYPE_DECL)
5828         {
5829           layout_decl (decl, 0);
5830           if (c_dialect_objc ())
5831             objc_check_decl (decl);
5832           rest_of_decl_compilation (decl, toplevel, 0);
5833           if (!toplevel)
5834             expand_decl (decl);
5835         }
5836     }
5837   C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5838
5839   /* Finish debugging output for this type.  */
5840   rest_of_type_compilation (t, toplevel);
5841
5842   /* If we're inside a function proper, i.e. not file-scope and not still
5843      parsing parameters, then arrange for the size of a variable sized type
5844      to be bound now.  */
5845   if (cur_stmt_list && variably_modified_type_p (t, NULL_TREE))
5846     add_stmt (build_stmt (DECL_EXPR, build_decl (TYPE_DECL, NULL, t)));
5847
5848   return t;
5849 }
5850
5851 /* Lay out the type T, and its element type, and so on.  */
5852
5853 static void
5854 layout_array_type (tree t)
5855 {
5856   if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5857     layout_array_type (TREE_TYPE (t));
5858   layout_type (t);
5859 }
5860 \f
5861 /* Begin compiling the definition of an enumeration type.
5862    NAME is its name (or null if anonymous).
5863    Returns the type object, as yet incomplete.
5864    Also records info about it so that build_enumerator
5865    may be used to declare the individual values as they are read.  */
5866
5867 tree
5868 start_enum (struct c_enum_contents *the_enum, tree name)
5869 {
5870   tree enumtype = 0;
5871
5872   /* If this is the real definition for a previous forward reference,
5873      fill in the contents in the same object that used to be the
5874      forward reference.  */
5875
5876   if (name != 0)
5877     enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5878
5879   if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5880     {
5881       enumtype = make_node (ENUMERAL_TYPE);
5882       pushtag (name, enumtype);
5883     }
5884
5885   if (C_TYPE_BEING_DEFINED (enumtype))
5886     error ("nested redefinition of %<enum %E%>", name);
5887
5888   C_TYPE_BEING_DEFINED (enumtype) = 1;
5889
5890   if (TYPE_VALUES (enumtype) != 0)
5891     {
5892       /* This enum is a named one that has been declared already.  */
5893       error ("redeclaration of %<enum %E%>", name);
5894
5895       /* Completely replace its old definition.
5896          The old enumerators remain defined, however.  */
5897       TYPE_VALUES (enumtype) = 0;
5898     }
5899
5900   the_enum->enum_next_value = integer_zero_node;
5901   the_enum->enum_overflow = 0;
5902
5903   if (flag_short_enums)
5904     TYPE_PACKED (enumtype) = 1;
5905
5906   return enumtype;
5907 }
5908
5909 /* After processing and defining all the values of an enumeration type,
5910    install their decls in the enumeration type and finish it off.
5911    ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5912    and ATTRIBUTES are the specified attributes.
5913    Returns ENUMTYPE.  */
5914
5915 tree
5916 finish_enum (tree enumtype, tree values, tree attributes)
5917 {
5918   tree pair, tem;
5919   tree minnode = 0, maxnode = 0;
5920   int precision, unsign;
5921   bool toplevel = (file_scope == current_scope);
5922   struct lang_type *lt;
5923
5924   decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5925
5926   /* Calculate the maximum value of any enumerator in this type.  */
5927
5928   if (values == error_mark_node)
5929     minnode = maxnode = integer_zero_node;
5930   else
5931     {
5932       minnode = maxnode = TREE_VALUE (values);
5933       for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5934         {
5935           tree value = TREE_VALUE (pair);
5936           if (tree_int_cst_lt (maxnode, value))
5937             maxnode = value;
5938           if (tree_int_cst_lt (value, minnode))
5939             minnode = value;
5940         }
5941     }
5942
5943   /* Construct the final type of this enumeration.  It is the same
5944      as one of the integral types - the narrowest one that fits, except
5945      that normally we only go as narrow as int - and signed iff any of
5946      the values are negative.  */
5947   unsign = (tree_int_cst_sgn (minnode) >= 0);
5948   precision = MAX (tree_int_cst_min_precision (minnode, unsign),
5949                    tree_int_cst_min_precision (maxnode, unsign));
5950
5951   if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5952     {
5953       tem = c_common_type_for_size (precision, unsign);
5954       if (tem == NULL)
5955         {
5956           warning (0, "enumeration values exceed range of largest integer");
5957           tem = long_long_integer_type_node;
5958         }
5959     }
5960   else
5961     tem = unsign ? unsigned_type_node : integer_type_node;
5962
5963   TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
5964   TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
5965   TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
5966   TYPE_SIZE (enumtype) = 0;
5967
5968   /* If the precision of the type was specific with an attribute and it
5969      was too small, give an error.  Otherwise, use it.  */
5970   if (TYPE_PRECISION (enumtype))
5971     {
5972       if (precision > TYPE_PRECISION (enumtype))
5973         error ("specified mode too small for enumeral values");
5974     }
5975   else
5976     TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
5977
5978   layout_type (enumtype);
5979
5980   if (values != error_mark_node)
5981     {
5982       /* Change the type of the enumerators to be the enum type.  We
5983          need to do this irrespective of the size of the enum, for
5984          proper type checking.  Replace the DECL_INITIALs of the
5985          enumerators, and the value slots of the list, with copies
5986          that have the enum type; they cannot be modified in place
5987          because they may be shared (e.g.  integer_zero_node) Finally,
5988          change the purpose slots to point to the names of the decls.  */
5989       for (pair = values; pair; pair = TREE_CHAIN (pair))
5990         {
5991           tree enu = TREE_PURPOSE (pair);
5992           tree ini = DECL_INITIAL (enu);
5993
5994           TREE_TYPE (enu) = enumtype;
5995
5996           /* The ISO C Standard mandates enumerators to have type int,
5997              even though the underlying type of an enum type is
5998              unspecified.  However, GCC allows enumerators of any
5999              integer type as an extensions.  build_enumerator()
6000              converts any enumerators that fit in an int to type int,
6001              to avoid promotions to unsigned types when comparing
6002              integers with enumerators that fit in the int range.
6003              When -pedantic is given, build_enumerator() would have
6004              already warned about those that don't fit. Here we
6005              convert the rest to the enumerator type. */
6006           if (TREE_TYPE (ini) != integer_type_node)
6007             ini = convert (enumtype, ini);
6008
6009           DECL_INITIAL (enu) = ini;
6010           TREE_PURPOSE (pair) = DECL_NAME (enu);
6011           TREE_VALUE (pair) = ini;
6012         }
6013
6014       TYPE_VALUES (enumtype) = values;
6015     }
6016
6017   /* Record the min/max values so that we can warn about bit-field
6018      enumerations that are too small for the values.  */
6019   lt = GGC_CNEW (struct lang_type);
6020   lt->enum_min = minnode;
6021   lt->enum_max = maxnode;
6022   TYPE_LANG_SPECIFIC (enumtype) = lt;
6023
6024   /* Fix up all variant types of this enum type.  */
6025   for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
6026     {
6027       if (tem == enumtype)
6028         continue;
6029       TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
6030       TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
6031       TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
6032       TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
6033       TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
6034       SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
6035       TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
6036       TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
6037       TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
6038       TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
6039       TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
6040     }
6041
6042   /* Finish debugging output for this type.  */
6043   rest_of_type_compilation (enumtype, toplevel);
6044
6045   return enumtype;
6046 }
6047
6048 /* Build and install a CONST_DECL for one value of the
6049    current enumeration type (one that was begun with start_enum).
6050    Return a tree-list containing the CONST_DECL and its value.
6051    Assignment of sequential values by default is handled here.  */
6052
6053 tree
6054 build_enumerator (struct c_enum_contents *the_enum, tree name, tree value,
6055                   location_t value_loc)
6056 {
6057   tree decl, type;
6058
6059   /* Validate and default VALUE.  */
6060
6061   if (value != 0)
6062     {
6063       /* Don't issue more errors for error_mark_node (i.e. an
6064          undeclared identifier) - just ignore the value expression.  */
6065       if (value == error_mark_node)
6066         value = 0;
6067       else if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
6068                || TREE_CODE (value) != INTEGER_CST)
6069         {
6070           error ("enumerator value for %qE is not an integer constant", name);
6071           value = 0;
6072         }
6073       else
6074         {
6075           value = default_conversion (value);
6076           constant_expression_warning (value);
6077         }
6078     }
6079
6080   /* Default based on previous value.  */
6081   /* It should no longer be possible to have NON_LVALUE_EXPR
6082      in the default.  */
6083   if (value == 0)
6084     {
6085       value = the_enum->enum_next_value;
6086       if (the_enum->enum_overflow)
6087         error ("overflow in enumeration values");
6088     }
6089   /* Even though the underlying type of an enum is unspecified, the
6090      type of enumeration constants is explicitly defined as int
6091      (6.4.4.3/2 in the C99 Standard).  GCC allows any integer type as
6092      an extension.  */
6093   else if (!int_fits_type_p (value, integer_type_node))
6094     pedwarn (value_loc, OPT_pedantic, 
6095              "ISO C restricts enumerator values to range of %<int%>");
6096
6097   /* The ISO C Standard mandates enumerators to have type int, even
6098      though the underlying type of an enum type is unspecified.
6099      However, GCC allows enumerators of any integer type as an
6100      extensions.  Here we convert any enumerators that fit in an int
6101      to type int, to avoid promotions to unsigned types when comparing
6102      integers with enumerators that fit in the int range.  When
6103      -pedantic is given, we would have already warned about those that
6104      don't fit. We have to do this here rather than in finish_enum
6105      because this value may be used to define more enumerators.  */
6106   if (int_fits_type_p (value, integer_type_node))
6107     value = convert (integer_type_node, value);
6108
6109   /* Set basis for default for next value.  */
6110   the_enum->enum_next_value
6111     = build_binary_op
6112          (EXPR_HAS_LOCATION (value) ? EXPR_LOCATION (value) : input_location,
6113          PLUS_EXPR, value, integer_one_node, 0);
6114   the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
6115
6116   /* Now create a declaration for the enum value name.  */
6117
6118   type = TREE_TYPE (value);
6119   type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
6120                                       TYPE_PRECISION (integer_type_node)),
6121                                  (TYPE_PRECISION (type)
6122                                   >= TYPE_PRECISION (integer_type_node)
6123                                   && TYPE_UNSIGNED (type)));
6124
6125   decl = build_decl (CONST_DECL, name, type);
6126   DECL_INITIAL (decl) = convert (type, value);
6127   pushdecl (decl);
6128
6129   return tree_cons (decl, value, NULL_TREE);
6130 }
6131
6132 \f
6133 /* Create the FUNCTION_DECL for a function definition.
6134    DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
6135    the declaration; they describe the function's name and the type it returns,
6136    but twisted together in a fashion that parallels the syntax of C.
6137
6138    This function creates a binding context for the function body
6139    as well as setting up the FUNCTION_DECL in current_function_decl.
6140
6141    Returns 1 on success.  If the DECLARATOR is not suitable for a function
6142    (it defines a datum instead), we return 0, which tells
6143    yyparse to report a parse error.  */
6144
6145 int
6146 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
6147                 tree attributes)
6148 {
6149   tree decl1, old_decl;
6150   tree restype, resdecl;
6151   struct c_label_context_se *nstack_se;
6152   struct c_label_context_vm *nstack_vm;
6153
6154   current_function_returns_value = 0;  /* Assume, until we see it does.  */
6155   current_function_returns_null = 0;
6156   current_function_returns_abnormally = 0;
6157   warn_about_return_type = 0;
6158   c_switch_stack = NULL;
6159
6160   nstack_se = XOBNEW (&parser_obstack, struct c_label_context_se);
6161   nstack_se->labels_def = NULL;
6162   nstack_se->labels_used = NULL;
6163   nstack_se->next = label_context_stack_se;
6164   label_context_stack_se = nstack_se;
6165
6166   nstack_vm = XOBNEW (&parser_obstack, struct c_label_context_vm);
6167   nstack_vm->labels_def = NULL;
6168   nstack_vm->labels_used = NULL;
6169   nstack_vm->scope = 0;
6170   nstack_vm->next = label_context_stack_vm;
6171   label_context_stack_vm = nstack_vm;
6172
6173   /* Indicate no valid break/continue context by setting these variables
6174      to some non-null, non-label value.  We'll notice and emit the proper
6175      error message in c_finish_bc_stmt.  */
6176   c_break_label = c_cont_label = size_zero_node;
6177
6178   decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
6179                           &attributes, NULL, NULL, DEPRECATED_NORMAL);
6180
6181   /* If the declarator is not suitable for a function definition,
6182      cause a syntax error.  */
6183   if (decl1 == 0)
6184     {
6185       label_context_stack_se = label_context_stack_se->next;
6186       label_context_stack_vm = label_context_stack_vm->next;
6187       return 0;
6188     }
6189
6190   decl_attributes (&decl1, attributes, 0);
6191
6192   if (DECL_DECLARED_INLINE_P (decl1)
6193       && DECL_UNINLINABLE (decl1)
6194       && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
6195     warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
6196              decl1);
6197
6198   /* Handle gnu_inline attribute.  */
6199   if (declspecs->inline_p
6200       && !flag_gnu89_inline
6201       && TREE_CODE (decl1) == FUNCTION_DECL
6202       && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
6203           || current_function_decl))
6204     {
6205       if (declspecs->storage_class != csc_static)
6206         DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
6207     }
6208
6209   announce_function (decl1);
6210
6211   if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
6212     {
6213       error ("return type is an incomplete type");
6214       /* Make it return void instead.  */
6215       TREE_TYPE (decl1)
6216         = build_function_type (void_type_node,
6217                                TYPE_ARG_TYPES (TREE_TYPE (decl1)));
6218     }
6219
6220   if (warn_about_return_type)
6221     pedwarn_c99 (input_location, flag_isoc99 ? 0 
6222                  : (warn_return_type ? OPT_Wreturn_type : OPT_Wimplicit_int),
6223                  "return type defaults to %<int%>");
6224
6225   /* Make the init_value nonzero so pushdecl knows this is not tentative.
6226      error_mark_node is replaced below (in pop_scope) with the BLOCK.  */
6227   DECL_INITIAL (decl1) = error_mark_node;
6228
6229   /* If this definition isn't a prototype and we had a prototype declaration
6230      before, copy the arg type info from that prototype.  */
6231   old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
6232   if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
6233     old_decl = 0;
6234   current_function_prototype_locus = UNKNOWN_LOCATION;
6235   current_function_prototype_built_in = false;
6236   current_function_prototype_arg_types = NULL_TREE;
6237   if (TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
6238     {
6239       if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
6240           && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
6241                         TREE_TYPE (TREE_TYPE (old_decl))))
6242         {
6243           TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
6244                                               TREE_TYPE (decl1));
6245           current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
6246           current_function_prototype_built_in
6247             = C_DECL_BUILTIN_PROTOTYPE (old_decl);
6248           current_function_prototype_arg_types
6249             = TYPE_ARG_TYPES (TREE_TYPE (decl1));
6250         }
6251       if (TREE_PUBLIC (decl1))
6252         {
6253           /* If there is an external prototype declaration of this
6254              function, record its location but do not copy information
6255              to this decl.  This may be an invisible declaration
6256              (built-in or in a scope which has finished) or simply
6257              have more refined argument types than any declaration
6258              found above.  */
6259           struct c_binding *b;
6260           for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
6261             if (B_IN_SCOPE (b, external_scope))
6262               break;
6263           if (b)
6264             {
6265               tree ext_decl, ext_type;
6266               ext_decl = b->decl;
6267               ext_type = b->type ? b->type : TREE_TYPE (ext_decl);
6268               if (TREE_CODE (ext_type) == FUNCTION_TYPE
6269                   && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
6270                                 TREE_TYPE (ext_type)))
6271                 {
6272                   current_function_prototype_locus
6273                     = DECL_SOURCE_LOCATION (ext_decl);
6274                   current_function_prototype_built_in
6275                     = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
6276                   current_function_prototype_arg_types
6277                     = TYPE_ARG_TYPES (ext_type);
6278                 }
6279             }
6280         }
6281     }
6282
6283   /* Optionally warn of old-fashioned def with no previous prototype.  */
6284   if (warn_strict_prototypes
6285       && old_decl != error_mark_node
6286       && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
6287       && C_DECL_ISNT_PROTOTYPE (old_decl))
6288     warning (OPT_Wstrict_prototypes,
6289              "function declaration isn%'t a prototype");
6290   /* Optionally warn of any global def with no previous prototype.  */
6291   else if (warn_missing_prototypes
6292            && old_decl != error_mark_node
6293            && TREE_PUBLIC (decl1)
6294            && !MAIN_NAME_P (DECL_NAME (decl1))
6295            && C_DECL_ISNT_PROTOTYPE (old_decl))
6296     warning (OPT_Wmissing_prototypes, "no previous prototype for %q+D", decl1);
6297   /* Optionally warn of any def with no previous prototype
6298      if the function has already been used.  */
6299   else if (warn_missing_prototypes
6300            && old_decl != 0
6301            && old_decl != error_mark_node
6302            && TREE_USED (old_decl)
6303            && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
6304     warning (OPT_Wmissing_prototypes,
6305              "%q+D was used with no prototype before its definition", decl1);
6306   /* Optionally warn of any global def with no previous declaration.  */
6307   else if (warn_missing_declarations
6308            && TREE_PUBLIC (decl1)
6309            && old_decl == 0
6310            && !MAIN_NAME_P (DECL_NAME (decl1)))
6311     warning (OPT_Wmissing_declarations, "no previous declaration for %q+D",
6312              decl1);
6313   /* Optionally warn of any def with no previous declaration
6314      if the function has already been used.  */
6315   else if (warn_missing_declarations
6316            && old_decl != 0
6317            && old_decl != error_mark_node
6318            && TREE_USED (old_decl)
6319            && C_DECL_IMPLICIT (old_decl))
6320     warning (OPT_Wmissing_declarations,
6321              "%q+D was used with no declaration before its definition", decl1);
6322
6323   /* This function exists in static storage.
6324      (This does not mean `static' in the C sense!)  */
6325   TREE_STATIC (decl1) = 1;
6326
6327   /* A nested function is not global.  */
6328   if (current_function_decl != 0)
6329     TREE_PUBLIC (decl1) = 0;
6330
6331   /* This is the earliest point at which we might know the assembler
6332      name of the function.  Thus, if it's set before this, die horribly.  */
6333   gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
6334
6335   /* If #pragma weak was used, mark the decl weak now.  */
6336   if (current_scope == file_scope)
6337     maybe_apply_pragma_weak (decl1);
6338
6339   /* Warn for unlikely, improbable, or stupid declarations of `main'.  */
6340   if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
6341     {
6342       if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
6343           != integer_type_node)
6344         pedwarn (input_location, OPT_Wmain, "return type of %q+D is not %<int%>", decl1);
6345
6346       check_main_parameter_types (decl1);
6347
6348       if (!TREE_PUBLIC (decl1))
6349         pedwarn (input_location, OPT_Wmain, "%q+D is normally a non-static function", decl1);
6350     }
6351
6352   /* Record the decl so that the function name is defined.
6353      If we already have a decl for this name, and it is a FUNCTION_DECL,
6354      use the old decl.  */
6355
6356   current_function_decl = pushdecl (decl1);
6357
6358   push_scope ();
6359   declare_parm_level ();
6360
6361   restype = TREE_TYPE (TREE_TYPE (current_function_decl));
6362   resdecl = build_decl (RESULT_DECL, NULL_TREE, restype);
6363   DECL_ARTIFICIAL (resdecl) = 1;
6364   DECL_IGNORED_P (resdecl) = 1;
6365   DECL_RESULT (current_function_decl) = resdecl;
6366
6367   start_fname_decls ();
6368
6369   return 1;
6370 }
6371 \f
6372 /* Subroutine of store_parm_decls which handles new-style function
6373    definitions (prototype format). The parms already have decls, so we
6374    need only record them as in effect and complain if any redundant
6375    old-style parm decls were written.  */
6376 static void
6377 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
6378 {
6379   tree decl;
6380
6381   if (current_scope->bindings)
6382     {
6383       error ("%Jold-style parameter declarations in prototyped "
6384              "function definition", fndecl);
6385
6386       /* Get rid of the old-style declarations.  */
6387       pop_scope ();
6388       push_scope ();
6389     }
6390   /* Don't issue this warning for nested functions, and don't issue this
6391      warning if we got here because ARG_INFO_TYPES was error_mark_node
6392      (this happens when a function definition has just an ellipsis in
6393      its parameter list).  */
6394   else if (!in_system_header && !current_function_scope
6395            && arg_info->types != error_mark_node)
6396     warning (OPT_Wtraditional,
6397              "%Jtraditional C rejects ISO C style function definitions",
6398              fndecl);
6399
6400   /* Now make all the parameter declarations visible in the function body.
6401      We can bypass most of the grunt work of pushdecl.  */
6402   for (decl = arg_info->parms; decl; decl = TREE_CHAIN (decl))
6403     {
6404       DECL_CONTEXT (decl) = current_function_decl;
6405       if (DECL_NAME (decl))
6406         {
6407           bind (DECL_NAME (decl), decl, current_scope,
6408                 /*invisible=*/false, /*nested=*/false,
6409                 UNKNOWN_LOCATION);
6410           if (!TREE_USED (decl))
6411             warn_if_shadowing (decl);
6412         }
6413       else
6414         error ("%Jparameter name omitted", decl);
6415     }
6416
6417   /* Record the parameter list in the function declaration.  */
6418   DECL_ARGUMENTS (fndecl) = arg_info->parms;
6419
6420   /* Now make all the ancillary declarations visible, likewise.  */
6421   for (decl = arg_info->others; decl; decl = TREE_CHAIN (decl))
6422     {
6423       DECL_CONTEXT (decl) = current_function_decl;
6424       if (DECL_NAME (decl))
6425         bind (DECL_NAME (decl), decl, current_scope,
6426               /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
6427     }
6428
6429   /* And all the tag declarations.  */
6430   for (decl = arg_info->tags; decl; decl = TREE_CHAIN (decl))
6431     if (TREE_PURPOSE (decl))
6432       bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope,
6433             /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
6434 }
6435
6436 /* Subroutine of store_parm_decls which handles old-style function
6437    definitions (separate parameter list and declarations).  */
6438
6439 static void
6440 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
6441 {
6442   struct c_binding *b;
6443   tree parm, decl, last;
6444   tree parmids = arg_info->parms;
6445   struct pointer_set_t *seen_args = pointer_set_create ();
6446
6447   if (!in_system_header)
6448     warning (OPT_Wold_style_definition, "%Jold-style function definition",
6449              fndecl);
6450
6451   /* Match each formal parameter name with its declaration.  Save each
6452      decl in the appropriate TREE_PURPOSE slot of the parmids chain.  */
6453   for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6454     {
6455       if (TREE_VALUE (parm) == 0)
6456         {
6457           error ("%Jparameter name missing from parameter list", fndecl);
6458           TREE_PURPOSE (parm) = 0;
6459           continue;
6460         }
6461
6462       b = I_SYMBOL_BINDING (TREE_VALUE (parm));
6463       if (b && B_IN_CURRENT_SCOPE (b))
6464         {
6465           decl = b->decl;
6466           /* If we got something other than a PARM_DECL it is an error.  */
6467           if (TREE_CODE (decl) != PARM_DECL)
6468             error ("%q+D declared as a non-parameter", decl);
6469           /* If the declaration is already marked, we have a duplicate
6470              name.  Complain and ignore the duplicate.  */
6471           else if (pointer_set_contains (seen_args, decl))
6472             {
6473               error ("multiple parameters named %q+D", decl);
6474               TREE_PURPOSE (parm) = 0;
6475               continue;
6476             }
6477           /* If the declaration says "void", complain and turn it into
6478              an int.  */
6479           else if (VOID_TYPE_P (TREE_TYPE (decl)))
6480             {
6481               error ("parameter %q+D declared with void type", decl);
6482               TREE_TYPE (decl) = integer_type_node;
6483               DECL_ARG_TYPE (decl) = integer_type_node;
6484               layout_decl (decl, 0);
6485             }
6486           warn_if_shadowing (decl);
6487         }
6488       /* If no declaration found, default to int.  */
6489       else
6490         {
6491           decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
6492           DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
6493           DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
6494           pushdecl (decl);
6495           warn_if_shadowing (decl);
6496
6497           if (flag_isoc99)
6498             pedwarn (input_location, 0, "type of %q+D defaults to %<int%>", decl);
6499           else 
6500             warning (OPT_Wmissing_parameter_type, "type of %q+D defaults to %<int%>", decl);
6501         }
6502
6503       TREE_PURPOSE (parm) = decl;
6504       pointer_set_insert (seen_args, decl);
6505     }
6506
6507   /* Now examine the parms chain for incomplete declarations
6508      and declarations with no corresponding names.  */
6509
6510   for (b = current_scope->bindings; b; b = b->prev)
6511     {
6512       parm = b->decl;
6513       if (TREE_CODE (parm) != PARM_DECL)
6514         continue;
6515
6516       if (TREE_TYPE (parm) != error_mark_node
6517           && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
6518         {
6519           error ("parameter %q+D has incomplete type", parm);
6520           TREE_TYPE (parm) = error_mark_node;
6521         }
6522
6523       if (!pointer_set_contains (seen_args, parm))
6524         {
6525           error ("declaration for parameter %q+D but no such parameter", parm);
6526
6527           /* Pretend the parameter was not missing.
6528              This gets us to a standard state and minimizes
6529              further error messages.  */
6530           parmids = chainon (parmids, tree_cons (parm, 0, 0));
6531         }
6532     }
6533
6534   /* Chain the declarations together in the order of the list of
6535      names.  Store that chain in the function decl, replacing the
6536      list of names.  Update the current scope to match.  */
6537   DECL_ARGUMENTS (fndecl) = 0;
6538
6539   for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6540     if (TREE_PURPOSE (parm))
6541       break;
6542   if (parm && TREE_PURPOSE (parm))
6543     {
6544       last = TREE_PURPOSE (parm);
6545       DECL_ARGUMENTS (fndecl) = last;
6546
6547       for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
6548         if (TREE_PURPOSE (parm))
6549           {
6550             TREE_CHAIN (last) = TREE_PURPOSE (parm);
6551             last = TREE_PURPOSE (parm);
6552           }
6553       TREE_CHAIN (last) = 0;
6554     }
6555
6556   pointer_set_destroy (seen_args);
6557
6558   /* If there was a previous prototype,
6559      set the DECL_ARG_TYPE of each argument according to
6560      the type previously specified, and report any mismatches.  */
6561
6562   if (current_function_prototype_arg_types)
6563     {
6564       tree type;
6565       for (parm = DECL_ARGUMENTS (fndecl),
6566              type = current_function_prototype_arg_types;
6567            parm || (type && TREE_VALUE (type) != error_mark_node
6568                    && (TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node));
6569            parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6570         {
6571           if (parm == 0 || type == 0
6572               || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6573             {
6574               if (current_function_prototype_built_in)
6575                 warning (0, "number of arguments doesn%'t match "
6576                          "built-in prototype");
6577               else
6578                 {
6579                   error ("number of arguments doesn%'t match prototype");
6580                   error ("%Hprototype declaration",
6581                          &current_function_prototype_locus);
6582                 }
6583               break;
6584             }
6585           /* Type for passing arg must be consistent with that
6586              declared for the arg.  ISO C says we take the unqualified
6587              type for parameters declared with qualified type.  */
6588           if (TREE_TYPE (parm) != error_mark_node
6589               && TREE_TYPE (type) != error_mark_node
6590               && !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6591                              TYPE_MAIN_VARIANT (TREE_VALUE (type))))
6592             {
6593               if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6594                   == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6595                 {
6596                   /* Adjust argument to match prototype.  E.g. a previous
6597                      `int foo(float);' prototype causes
6598                      `int foo(x) float x; {...}' to be treated like
6599                      `int foo(float x) {...}'.  This is particularly
6600                      useful for argument types like uid_t.  */
6601                   DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6602
6603                   if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
6604                       && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6605                       && TYPE_PRECISION (TREE_TYPE (parm))
6606                       < TYPE_PRECISION (integer_type_node))
6607                     DECL_ARG_TYPE (parm) = integer_type_node;
6608
6609                   /* ??? Is it possible to get here with a
6610                      built-in prototype or will it always have
6611                      been diagnosed as conflicting with an
6612                      old-style definition and discarded?  */
6613                   if (current_function_prototype_built_in)
6614                     warning (OPT_pedantic, "promoted argument %qD "
6615                              "doesn%'t match built-in prototype", parm);
6616                   else
6617                     {
6618                       pedwarn (input_location, OPT_pedantic, "promoted argument %qD "
6619                                "doesn%'t match prototype", parm);
6620                       pedwarn (current_function_prototype_locus, OPT_pedantic,
6621                                "prototype declaration");
6622                     }
6623                 }
6624               else
6625                 {
6626                   if (current_function_prototype_built_in)
6627                     warning (0, "argument %qD doesn%'t match "
6628                              "built-in prototype", parm);
6629                   else
6630                     {
6631                       error ("argument %qD doesn%'t match prototype", parm);
6632                       error ("%Hprototype declaration",
6633                              &current_function_prototype_locus);
6634                     }
6635                 }
6636             }
6637         }
6638       TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6639     }
6640
6641   /* Otherwise, create a prototype that would match.  */
6642
6643   else
6644     {
6645       tree actual = 0, last = 0, type;
6646
6647       for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6648         {
6649           type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6650           if (last)
6651             TREE_CHAIN (last) = type;
6652           else
6653             actual = type;
6654           last = type;
6655         }
6656       type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6657       if (last)
6658         TREE_CHAIN (last) = type;
6659       else
6660         actual = type;
6661
6662       /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6663          of the type of this function, but we need to avoid having this
6664          affect the types of other similarly-typed functions, so we must
6665          first force the generation of an identical (but separate) type
6666          node for the relevant function type.  The new node we create
6667          will be a variant of the main variant of the original function
6668          type.  */
6669
6670       TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
6671
6672       TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6673     }
6674 }
6675
6676 /* Store parameter declarations passed in ARG_INFO into the current
6677    function declaration.  */
6678
6679 void
6680 store_parm_decls_from (struct c_arg_info *arg_info)
6681 {
6682   current_function_arg_info = arg_info;
6683   store_parm_decls ();
6684 }
6685
6686 /* Store the parameter declarations into the current function declaration.
6687    This is called after parsing the parameter declarations, before
6688    digesting the body of the function.
6689
6690    For an old-style definition, construct a prototype out of the old-style
6691    parameter declarations and inject it into the function's type.  */
6692
6693 void
6694 store_parm_decls (void)
6695 {
6696   tree fndecl = current_function_decl;
6697   bool proto;
6698
6699   /* The argument information block for FNDECL.  */
6700   struct c_arg_info *arg_info = current_function_arg_info;
6701   current_function_arg_info = 0;
6702
6703   /* True if this definition is written with a prototype.  Note:
6704      despite C99 6.7.5.3p14, we can *not* treat an empty argument
6705      list in a function definition as equivalent to (void) -- an
6706      empty argument list specifies the function has no parameters,
6707      but only (void) sets up a prototype for future calls.  */
6708   proto = arg_info->types != 0;
6709
6710   if (proto)
6711     store_parm_decls_newstyle (fndecl, arg_info);
6712   else
6713     store_parm_decls_oldstyle (fndecl, arg_info);
6714
6715   /* The next call to push_scope will be a function body.  */
6716
6717   next_is_function_body = true;
6718
6719   /* Write a record describing this function definition to the prototypes
6720      file (if requested).  */
6721
6722   gen_aux_info_record (fndecl, 1, 0, proto);
6723
6724   /* Initialize the RTL code for the function.  */
6725   allocate_struct_function (fndecl, false);
6726
6727   /* Begin the statement tree for this function.  */
6728   DECL_SAVED_TREE (fndecl) = push_stmt_list ();
6729
6730   /* ??? Insert the contents of the pending sizes list into the function
6731      to be evaluated.  The only reason left to have this is
6732         void foo(int n, int array[n++])
6733      because we throw away the array type in favor of a pointer type, and
6734      thus won't naturally see the SAVE_EXPR containing the increment.  All
6735      other pending sizes would be handled by gimplify_parameters.  */
6736   {
6737     tree t;
6738     for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t))
6739       add_stmt (TREE_VALUE (t));
6740   }
6741
6742   /* Even though we're inside a function body, we still don't want to
6743      call expand_expr to calculate the size of a variable-sized array.
6744      We haven't necessarily assigned RTL to all variables yet, so it's
6745      not safe to try to expand expressions involving them.  */
6746   cfun->dont_save_pending_sizes_p = 1;
6747 }
6748 \f
6749 /* Emit diagnostics that require gimple input for detection.  Operate on
6750    FNDECL and all its nested functions.  */
6751
6752 static void
6753 c_gimple_diagnostics_recursively (tree fndecl)
6754 {
6755   struct cgraph_node *cgn;
6756   gimple_seq body = gimple_body (fndecl);
6757
6758   /* Handle attribute((warn_unused_result)).  Relies on gimple input.  */
6759   c_warn_unused_result (body);
6760
6761   /* Notice when OpenMP structured block constraints are violated.  */
6762   if (flag_openmp)
6763     diagnose_omp_structured_block_errors (fndecl);
6764
6765   /* Finalize all nested functions now.  */
6766   cgn = cgraph_node (fndecl);
6767   for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
6768     c_gimple_diagnostics_recursively (cgn->decl);
6769 }
6770
6771 /* Finish up a function declaration and compile that function
6772    all the way to assembler language output.  The free the storage
6773    for the function definition.
6774
6775    This is called after parsing the body of the function definition.  */
6776
6777 void
6778 finish_function (void)
6779 {
6780   tree fndecl = current_function_decl;
6781
6782   label_context_stack_se = label_context_stack_se->next;
6783   label_context_stack_vm = label_context_stack_vm->next;
6784
6785   if (TREE_CODE (fndecl) == FUNCTION_DECL
6786       && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6787     {
6788       tree args = DECL_ARGUMENTS (fndecl);
6789       for (; args; args = TREE_CHAIN (args))
6790         {
6791           tree type = TREE_TYPE (args);
6792           if (INTEGRAL_TYPE_P (type)
6793               && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6794             DECL_ARG_TYPE (args) = integer_type_node;
6795         }
6796     }
6797
6798   if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6799     BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6800
6801   /* Must mark the RESULT_DECL as being in this function.  */
6802
6803   if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6804     DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6805
6806   if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted
6807       && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6808       == integer_type_node && flag_isoc99)
6809     {
6810       tree stmt = c_finish_return (integer_zero_node);
6811       /* Hack.  We don't want the middle-end to warn that this return
6812          is unreachable, so we mark its location as special.  Using
6813          UNKNOWN_LOCATION has the problem that it gets clobbered in
6814          annotate_one_with_locus.  A cleaner solution might be to
6815          ensure ! should_carry_locus_p (stmt), but that needs a flag.
6816       */
6817       SET_EXPR_LOCATION (stmt, BUILTINS_LOCATION);
6818     }
6819
6820   /* Tie off the statement tree for this function.  */
6821   DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
6822
6823   finish_fname_decls ();
6824
6825   /* Complain if there's just no return statement.  */
6826   if (warn_return_type
6827       && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6828       && !current_function_returns_value && !current_function_returns_null
6829       /* Don't complain if we are no-return.  */
6830       && !current_function_returns_abnormally
6831       /* Don't warn for main().  */
6832       && !MAIN_NAME_P (DECL_NAME (fndecl))
6833       /* Or if they didn't actually specify a return type.  */
6834       && !C_FUNCTION_IMPLICIT_INT (fndecl)
6835       /* Normally, with -Wreturn-type, flow will complain, but we might
6836          optimize out static functions.  */
6837       && !TREE_PUBLIC (fndecl))
6838     {
6839       warning (OPT_Wreturn_type,
6840                "no return statement in function returning non-void");
6841       TREE_NO_WARNING (fndecl) = 1;
6842     }
6843
6844   /* Store the end of the function, so that we get good line number
6845      info for the epilogue.  */
6846   cfun->function_end_locus = input_location;
6847
6848   /* Finalize the ELF visibility for the function.  */
6849   c_determine_visibility (fndecl);
6850
6851   /* For GNU C extern inline functions disregard inline limits.  */
6852   if (DECL_EXTERNAL (fndecl) 
6853       && DECL_DECLARED_INLINE_P (fndecl))
6854     DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
6855
6856   /* Genericize before inlining.  Delay genericizing nested functions
6857      until their parent function is genericized.  Since finalizing
6858      requires GENERIC, delay that as well.  */
6859
6860   if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
6861       && !undef_nested_function)
6862     {
6863       if (!decl_function_context (fndecl))
6864         {
6865           c_genericize (fndecl);
6866           c_gimple_diagnostics_recursively (fndecl);
6867
6868           /* ??? Objc emits functions after finalizing the compilation unit.
6869              This should be cleaned up later and this conditional removed.  */
6870           if (cgraph_global_info_ready)
6871             {
6872               cgraph_add_new_function (fndecl, false);
6873               return;
6874             }
6875
6876           cgraph_finalize_function (fndecl, false);
6877         }
6878       else
6879         {
6880           /* Register this function with cgraph just far enough to get it
6881             added to our parent's nested function list.  Handy, since the
6882             C front end doesn't have such a list.  */
6883           (void) cgraph_node (fndecl);
6884         }
6885     }
6886
6887   if (!decl_function_context (fndecl))
6888     undef_nested_function = false;
6889
6890   /* We're leaving the context of this function, so zap cfun.
6891      It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
6892      tree_rest_of_compilation.  */
6893   set_cfun (NULL);
6894   current_function_decl = NULL;
6895 }
6896 \f
6897 /* Check the declarations given in a for-loop for satisfying the C99
6898    constraints.  If exactly one such decl is found, return it.  */
6899
6900 tree
6901 check_for_loop_decls (void)
6902 {
6903   struct c_binding *b;
6904   tree one_decl = NULL_TREE;
6905   int n_decls = 0;
6906
6907   if (!flag_isoc99)
6908     {
6909       static bool hint = true;
6910       /* If we get here, declarations have been used in a for loop without
6911          the C99 for loop scope.  This doesn't make much sense, so don't
6912          allow it.  */
6913       error ("%<for%> loop initial declarations are only allowed in C99 mode");
6914       if (hint)
6915         {
6916           inform (input_location, 
6917                   "use option -std=c99 or -std=gnu99 to compile your code");
6918           hint = false;
6919         }
6920       return NULL_TREE;
6921     }
6922   /* C99 subclause 6.8.5 paragraph 3:
6923
6924        [#3]  The  declaration  part  of  a for statement shall only
6925        declare identifiers for objects having storage class auto or
6926        register.
6927
6928      It isn't clear whether, in this sentence, "identifiers" binds to
6929      "shall only declare" or to "objects" - that is, whether all identifiers
6930      declared must be identifiers for objects, or whether the restriction
6931      only applies to those that are.  (A question on this in comp.std.c
6932      in November 2000 received no answer.)  We implement the strictest
6933      interpretation, to avoid creating an extension which later causes
6934      problems.  */
6935
6936   for (b = current_scope->bindings; b; b = b->prev)
6937     {
6938       tree id = b->id;
6939       tree decl = b->decl;
6940
6941       if (!id)
6942         continue;
6943
6944       switch (TREE_CODE (decl))
6945         {
6946         case VAR_DECL:
6947           if (TREE_STATIC (decl))
6948             error ("declaration of static variable %q+D in %<for%> loop "
6949                    "initial declaration", decl);
6950           else if (DECL_EXTERNAL (decl))
6951             error ("declaration of %<extern%> variable %q+D in %<for%> loop "
6952                    "initial declaration", decl);
6953           break;
6954
6955         case RECORD_TYPE:
6956           error ("%<struct %E%> declared in %<for%> loop initial declaration",
6957                  id);
6958           break;
6959         case UNION_TYPE:
6960           error ("%<union %E%> declared in %<for%> loop initial declaration",
6961                  id);
6962           break;
6963         case ENUMERAL_TYPE:
6964           error ("%<enum %E%> declared in %<for%> loop initial declaration",
6965                  id);
6966           break;
6967         default:
6968           error ("declaration of non-variable %q+D in %<for%> loop "
6969                  "initial declaration", decl);
6970         }
6971
6972       n_decls++;
6973       one_decl = decl;
6974     }
6975
6976   return n_decls == 1 ? one_decl : NULL_TREE;
6977 }
6978 \f
6979 /* Save and reinitialize the variables
6980    used during compilation of a C function.  */
6981
6982 void
6983 c_push_function_context (void)
6984 {
6985   struct language_function *p;
6986   p = GGC_NEW (struct language_function);
6987   cfun->language = p;
6988
6989   p->base.x_stmt_tree = c_stmt_tree;
6990   p->x_break_label = c_break_label;
6991   p->x_cont_label = c_cont_label;
6992   p->x_switch_stack = c_switch_stack;
6993   p->arg_info = current_function_arg_info;
6994   p->returns_value = current_function_returns_value;
6995   p->returns_null = current_function_returns_null;
6996   p->returns_abnormally = current_function_returns_abnormally;
6997   p->warn_about_return_type = warn_about_return_type;
6998
6999   push_function_context ();
7000 }
7001
7002 /* Restore the variables used during compilation of a C function.  */
7003
7004 void
7005 c_pop_function_context (void)
7006 {
7007   struct language_function *p;
7008
7009   pop_function_context ();
7010   p = cfun->language;
7011   cfun->language = NULL;
7012
7013   if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
7014       && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
7015     {
7016       /* Stop pointing to the local nodes about to be freed.  */
7017       /* But DECL_INITIAL must remain nonzero so we know this
7018          was an actual function definition.  */
7019       DECL_INITIAL (current_function_decl) = error_mark_node;
7020       DECL_ARGUMENTS (current_function_decl) = 0;
7021     }
7022
7023   c_stmt_tree = p->base.x_stmt_tree;
7024   c_break_label = p->x_break_label;
7025   c_cont_label = p->x_cont_label;
7026   c_switch_stack = p->x_switch_stack;
7027   current_function_arg_info = p->arg_info;
7028   current_function_returns_value = p->returns_value;
7029   current_function_returns_null = p->returns_null;
7030   current_function_returns_abnormally = p->returns_abnormally;
7031   warn_about_return_type = p->warn_about_return_type;
7032 }
7033
7034 /* Copy the DECL_LANG_SPECIFIC data associated with DECL.  */
7035
7036 void
7037 c_dup_lang_specific_decl (tree decl)
7038 {
7039   struct lang_decl *ld;
7040
7041   if (!DECL_LANG_SPECIFIC (decl))
7042     return;
7043
7044   ld = GGC_NEW (struct lang_decl);
7045   memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
7046   DECL_LANG_SPECIFIC (decl) = ld;
7047 }
7048
7049 /* The functions below are required for functionality of doing
7050    function at once processing in the C front end. Currently these
7051    functions are not called from anywhere in the C front end, but as
7052    these changes continue, that will change.  */
7053
7054 /* Returns the stmt_tree (if any) to which statements are currently
7055    being added.  If there is no active statement-tree, NULL is
7056    returned.  */
7057
7058 stmt_tree
7059 current_stmt_tree (void)
7060 {
7061   return &c_stmt_tree;
7062 }
7063
7064 /* Return the global value of T as a symbol.  */
7065
7066 tree
7067 identifier_global_value (tree t)
7068 {
7069   struct c_binding *b;
7070
7071   for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
7072     if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
7073       return b->decl;
7074
7075   return 0;
7076 }
7077
7078 /* Record a builtin type for C.  If NAME is non-NULL, it is the name used;
7079    otherwise the name is found in ridpointers from RID_INDEX.  */
7080
7081 void
7082 record_builtin_type (enum rid rid_index, const char *name, tree type)
7083 {
7084   tree id, decl;
7085   if (name == 0)
7086     id = ridpointers[(int) rid_index];
7087   else
7088     id = get_identifier (name);
7089   decl = build_decl (TYPE_DECL, id, type);
7090   pushdecl (decl);
7091   if (debug_hooks->type_decl)
7092     debug_hooks->type_decl (decl, false);
7093 }
7094
7095 /* Build the void_list_node (void_type_node having been created).  */
7096 tree
7097 build_void_list_node (void)
7098 {
7099   tree t = build_tree_list (NULL_TREE, void_type_node);
7100   return t;
7101 }
7102
7103 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR.  */
7104
7105 struct c_parm *
7106 build_c_parm (struct c_declspecs *specs, tree attrs,
7107               struct c_declarator *declarator)
7108 {
7109   struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
7110   ret->specs = specs;
7111   ret->attrs = attrs;
7112   ret->declarator = declarator;
7113   return ret;
7114 }
7115
7116 /* Return a declarator with nested attributes.  TARGET is the inner
7117    declarator to which these attributes apply.  ATTRS are the
7118    attributes.  */
7119
7120 struct c_declarator *
7121 build_attrs_declarator (tree attrs, struct c_declarator *target)
7122 {
7123   struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7124   ret->kind = cdk_attrs;
7125   ret->declarator = target;
7126   ret->u.attrs = attrs;
7127   return ret;
7128 }
7129
7130 /* Return a declarator for a function with arguments specified by ARGS
7131    and return type specified by TARGET.  */
7132
7133 struct c_declarator *
7134 build_function_declarator (struct c_arg_info *args,
7135                            struct c_declarator *target)
7136 {
7137   struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7138   ret->kind = cdk_function;
7139   ret->declarator = target;
7140   ret->u.arg_info = args;
7141   return ret;
7142 }
7143
7144 /* Return a declarator for the identifier IDENT (which may be
7145    NULL_TREE for an abstract declarator).  */
7146
7147 struct c_declarator *
7148 build_id_declarator (tree ident)
7149 {
7150   struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7151   ret->kind = cdk_id;
7152   ret->declarator = 0;
7153   ret->u.id = ident;
7154   /* Default value - may get reset to a more precise location. */
7155   ret->id_loc = input_location;
7156   return ret;
7157 }
7158
7159 /* Return something to represent absolute declarators containing a *.
7160    TARGET is the absolute declarator that the * contains.
7161    TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
7162    to apply to the pointer type.  */
7163
7164 struct c_declarator *
7165 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
7166                          struct c_declarator *target)
7167 {
7168   tree attrs;
7169   int quals = 0;
7170   struct c_declarator *itarget = target;
7171   struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7172   if (type_quals_attrs)
7173     {
7174       attrs = type_quals_attrs->attrs;
7175       quals = quals_from_declspecs (type_quals_attrs);
7176       if (attrs != NULL_TREE)
7177         itarget = build_attrs_declarator (attrs, target);
7178     }
7179   ret->kind = cdk_pointer;
7180   ret->declarator = itarget;
7181   ret->u.pointer_quals = quals;
7182   return ret;
7183 }
7184
7185 /* Return a pointer to a structure for an empty list of declaration
7186    specifiers.  */
7187
7188 struct c_declspecs *
7189 build_null_declspecs (void)
7190 {
7191   struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
7192   ret->type = 0;
7193   ret->expr = 0;
7194   ret->decl_attr = 0;
7195   ret->attrs = 0;
7196   ret->typespec_word = cts_none;
7197   ret->storage_class = csc_none;
7198   ret->expr_const_operands = true;
7199   ret->declspecs_seen_p = false;
7200   ret->type_seen_p = false;
7201   ret->non_sc_seen_p = false;
7202   ret->typedef_p = false;
7203   ret->tag_defined_p = false;
7204   ret->explicit_signed_p = false;
7205   ret->deprecated_p = false;
7206   ret->default_int_p = false;
7207   ret->long_p = false;
7208   ret->long_long_p = false;
7209   ret->short_p = false;
7210   ret->signed_p = false;
7211   ret->unsigned_p = false;
7212   ret->complex_p = false;
7213   ret->inline_p = false;
7214   ret->thread_p = false;
7215   ret->const_p = false;
7216   ret->volatile_p = false;
7217   ret->restrict_p = false;
7218   ret->saturating_p = false;
7219   return ret;
7220 }
7221
7222 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
7223    returning SPECS.  */
7224
7225 struct c_declspecs *
7226 declspecs_add_qual (struct c_declspecs *specs, tree qual)
7227 {
7228   enum rid i;
7229   bool dupe = false;
7230   specs->non_sc_seen_p = true;
7231   specs->declspecs_seen_p = true;
7232   gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
7233               && C_IS_RESERVED_WORD (qual));
7234   i = C_RID_CODE (qual);
7235   switch (i)
7236     {
7237     case RID_CONST:
7238       dupe = specs->const_p;
7239       specs->const_p = true;
7240       break;
7241     case RID_VOLATILE:
7242       dupe = specs->volatile_p;
7243       specs->volatile_p = true;
7244       break;
7245     case RID_RESTRICT:
7246       dupe = specs->restrict_p;
7247       specs->restrict_p = true;
7248       break;
7249     default:
7250       gcc_unreachable ();
7251     }
7252   if (dupe && !flag_isoc99)
7253     pedwarn (input_location, OPT_pedantic, "duplicate %qE", qual);
7254   return specs;
7255 }
7256
7257 /* Add the type specifier TYPE to the declaration specifiers SPECS,
7258    returning SPECS.  */
7259
7260 struct c_declspecs *
7261 declspecs_add_type (struct c_declspecs *specs, struct c_typespec spec)
7262 {
7263   tree type = spec.spec;
7264   specs->non_sc_seen_p = true;
7265   specs->declspecs_seen_p = true;
7266   specs->type_seen_p = true;
7267   if (TREE_DEPRECATED (type))
7268     specs->deprecated_p = true;
7269
7270   /* Handle type specifier keywords.  */
7271   if (TREE_CODE (type) == IDENTIFIER_NODE
7272       && C_IS_RESERVED_WORD (type)
7273       && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
7274     {
7275       enum rid i = C_RID_CODE (type);
7276       if (specs->type)
7277         {
7278           error ("two or more data types in declaration specifiers");
7279           return specs;
7280         }
7281       if ((int) i <= (int) RID_LAST_MODIFIER)
7282         {
7283           /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat".  */
7284           bool dupe = false;
7285           switch (i)
7286             {
7287             case RID_LONG:
7288               if (specs->long_long_p)
7289                 {
7290                   error ("%<long long long%> is too long for GCC");
7291                   break;
7292                 }
7293               if (specs->long_p)
7294                 {
7295                   if (specs->typespec_word == cts_double)
7296                     {
7297                       error ("both %<long long%> and %<double%> in "
7298                              "declaration specifiers");
7299                       break;
7300                     }
7301                   if (pedantic && !flag_isoc99 && !in_system_header)
7302                     pedwarn (input_location, OPT_Wlong_long, "ISO C90 does not support %<long long%>");
7303                   specs->long_long_p = 1;
7304                   break;
7305                 }
7306               if (specs->short_p)
7307                 error ("both %<long%> and %<short%> in "
7308                        "declaration specifiers");
7309               else if (specs->typespec_word == cts_void)
7310                 error ("both %<long%> and %<void%> in "
7311                        "declaration specifiers");
7312               else if (specs->typespec_word == cts_bool)
7313                 error ("both %<long%> and %<_Bool%> in "
7314                        "declaration specifiers");
7315               else if (specs->typespec_word == cts_char)
7316                 error ("both %<long%> and %<char%> in "
7317                        "declaration specifiers");
7318               else if (specs->typespec_word == cts_float)
7319                 error ("both %<long%> and %<float%> in "
7320                        "declaration specifiers");
7321               else if (specs->typespec_word == cts_dfloat32)
7322                 error ("both %<long%> and %<_Decimal32%> in "
7323                        "declaration specifiers");
7324               else if (specs->typespec_word == cts_dfloat64)
7325                 error ("both %<long%> and %<_Decimal64%> in "
7326                        "declaration specifiers");
7327               else if (specs->typespec_word == cts_dfloat128)
7328                 error ("both %<long%> and %<_Decimal128%> in "
7329                        "declaration specifiers");
7330               else
7331                 specs->long_p = true;
7332               break;
7333             case RID_SHORT:
7334               dupe = specs->short_p;
7335               if (specs->long_p)
7336                 error ("both %<long%> and %<short%> in "
7337                        "declaration specifiers");
7338               else if (specs->typespec_word == cts_void)
7339                 error ("both %<short%> and %<void%> in "
7340                        "declaration specifiers");
7341               else if (specs->typespec_word == cts_bool)
7342                 error ("both %<short%> and %<_Bool%> in "
7343                        "declaration specifiers");
7344               else if (specs->typespec_word == cts_char)
7345                 error ("both %<short%> and %<char%> in "
7346                        "declaration specifiers");
7347               else if (specs->typespec_word == cts_float)
7348                 error ("both %<short%> and %<float%> in "
7349                        "declaration specifiers");
7350               else if (specs->typespec_word == cts_double)
7351                 error ("both %<short%> and %<double%> in "
7352                        "declaration specifiers");
7353               else if (specs->typespec_word == cts_dfloat32)
7354                 error ("both %<short%> and %<_Decimal32%> in "
7355                        "declaration specifiers");
7356               else if (specs->typespec_word == cts_dfloat64)
7357                 error ("both %<short%> and %<_Decimal64%> in "
7358                                         "declaration specifiers");
7359               else if (specs->typespec_word == cts_dfloat128)
7360                 error ("both %<short%> and %<_Decimal128%> in "
7361                        "declaration specifiers");
7362               else
7363                 specs->short_p = true;
7364               break;
7365             case RID_SIGNED:
7366               dupe = specs->signed_p;
7367               if (specs->unsigned_p)
7368                 error ("both %<signed%> and %<unsigned%> in "
7369                        "declaration specifiers");
7370               else if (specs->typespec_word == cts_void)
7371                 error ("both %<signed%> and %<void%> in "
7372                        "declaration specifiers");
7373               else if (specs->typespec_word == cts_bool)
7374                 error ("both %<signed%> and %<_Bool%> in "
7375                        "declaration specifiers");
7376               else if (specs->typespec_word == cts_float)
7377                 error ("both %<signed%> and %<float%> in "
7378                        "declaration specifiers");
7379               else if (specs->typespec_word == cts_double)
7380                 error ("both %<signed%> and %<double%> in "
7381                        "declaration specifiers");
7382               else if (specs->typespec_word == cts_dfloat32)
7383                 error ("both %<signed%> and %<_Decimal32%> in "
7384                        "declaration specifiers");
7385               else if (specs->typespec_word == cts_dfloat64)
7386                 error ("both %<signed%> and %<_Decimal64%> in "
7387                        "declaration specifiers");
7388               else if (specs->typespec_word == cts_dfloat128)
7389                 error ("both %<signed%> and %<_Decimal128%> in "
7390                        "declaration specifiers");
7391               else
7392                 specs->signed_p = true;
7393               break;
7394             case RID_UNSIGNED:
7395               dupe = specs->unsigned_p;
7396               if (specs->signed_p)
7397                 error ("both %<signed%> and %<unsigned%> in "
7398                        "declaration specifiers");
7399               else if (specs->typespec_word == cts_void)
7400                 error ("both %<unsigned%> and %<void%> in "
7401                        "declaration specifiers");
7402               else if (specs->typespec_word == cts_bool)
7403                 error ("both %<unsigned%> and %<_Bool%> in "
7404                        "declaration specifiers");
7405               else if (specs->typespec_word == cts_float)
7406                 error ("both %<unsigned%> and %<float%> in "
7407                        "declaration specifiers");
7408               else if (specs->typespec_word == cts_double)
7409                 error ("both %<unsigned%> and %<double%> in "
7410                        "declaration specifiers");
7411               else if (specs->typespec_word == cts_dfloat32)
7412                 error ("both %<unsigned%> and %<_Decimal32%> in "
7413                        "declaration specifiers");
7414               else if (specs->typespec_word == cts_dfloat64)
7415                 error ("both %<unsigned%> and %<_Decimal64%> in "
7416                        "declaration specifiers");
7417               else if (specs->typespec_word == cts_dfloat128)
7418                 error ("both %<unsigned%> and %<_Decimal128%> in "
7419                        "declaration specifiers");
7420               else
7421                 specs->unsigned_p = true;
7422               break;
7423             case RID_COMPLEX:
7424               dupe = specs->complex_p;
7425               if (!flag_isoc99 && !in_system_header)
7426                 pedwarn (input_location, OPT_pedantic, "ISO C90 does not support complex types");
7427               if (specs->typespec_word == cts_void)
7428                 error ("both %<complex%> and %<void%> in "
7429                        "declaration specifiers");
7430               else if (specs->typespec_word == cts_bool)
7431                 error ("both %<complex%> and %<_Bool%> in "
7432                        "declaration specifiers");
7433               else if (specs->typespec_word == cts_dfloat32)
7434                 error ("both %<complex%> and %<_Decimal32%> in "
7435                        "declaration specifiers");
7436               else if (specs->typespec_word == cts_dfloat64)
7437                 error ("both %<complex%> and %<_Decimal64%> in "
7438                        "declaration specifiers");
7439               else if (specs->typespec_word == cts_dfloat128)
7440                 error ("both %<complex%> and %<_Decimal128%> in "
7441                        "declaration specifiers");
7442               else if (specs->typespec_word == cts_fract)
7443                 error ("both %<complex%> and %<_Fract%> in "
7444                        "declaration specifiers");
7445               else if (specs->typespec_word == cts_accum)
7446                 error ("both %<complex%> and %<_Accum%> in "
7447                        "declaration specifiers");
7448               else if (specs->saturating_p)
7449                 error ("both %<complex%> and %<_Sat%> in "
7450                        "declaration specifiers");
7451               else
7452                 specs->complex_p = true;
7453               break;
7454             case RID_SAT:
7455               dupe = specs->saturating_p;
7456               pedwarn (input_location, OPT_pedantic, "ISO C does not support saturating types");
7457               if (specs->typespec_word == cts_void)
7458                 error ("both %<_Sat%> and %<void%> in "
7459                        "declaration specifiers");
7460               else if (specs->typespec_word == cts_bool)
7461                 error ("both %<_Sat%> and %<_Bool%> in "
7462                        "declaration specifiers");
7463               else if (specs->typespec_word == cts_char)
7464                 error ("both %<_Sat%> and %<char%> in "
7465                        "declaration specifiers");
7466               else if (specs->typespec_word == cts_int)
7467                 error ("both %<_Sat%> and %<int%> in "
7468                        "declaration specifiers");
7469               else if (specs->typespec_word == cts_float)
7470                 error ("both %<_Sat%> and %<float%> in "
7471                        "declaration specifiers");
7472               else if (specs->typespec_word == cts_double)
7473                 error ("both %<_Sat%> and %<double%> in "
7474                        "declaration specifiers");
7475               else if (specs->typespec_word == cts_dfloat32)
7476                 error ("both %<_Sat%> and %<_Decimal32%> in "
7477                        "declaration specifiers");
7478               else if (specs->typespec_word == cts_dfloat64)
7479                 error ("both %<_Sat%> and %<_Decimal64%> in "
7480                        "declaration specifiers");
7481               else if (specs->typespec_word == cts_dfloat128)
7482                 error ("both %<_Sat%> and %<_Decimal128%> in "
7483                        "declaration specifiers");
7484               else if (specs->complex_p)
7485                 error ("both %<_Sat%> and %<complex%> in "
7486                        "declaration specifiers");
7487               else
7488                 specs->saturating_p = true;
7489               break;
7490             default:
7491               gcc_unreachable ();
7492             }
7493
7494           if (dupe)
7495             error ("duplicate %qE", type);
7496
7497           return specs;
7498         }
7499       else
7500         {
7501           /* "void", "_Bool", "char", "int", "float", "double", "_Decimal32",
7502              "_Decimal64", "_Decimal128", "_Fract" or "_Accum".  */
7503           if (specs->typespec_word != cts_none)
7504             {
7505               error ("two or more data types in declaration specifiers");
7506               return specs;
7507             }
7508           switch (i)
7509             {
7510             case RID_VOID:
7511               if (specs->long_p)
7512                 error ("both %<long%> and %<void%> in "
7513                        "declaration specifiers");
7514               else if (specs->short_p)
7515                 error ("both %<short%> and %<void%> in "
7516                        "declaration specifiers");
7517               else if (specs->signed_p)
7518                 error ("both %<signed%> and %<void%> in "
7519                        "declaration specifiers");
7520               else if (specs->unsigned_p)
7521                 error ("both %<unsigned%> and %<void%> in "
7522                        "declaration specifiers");
7523               else if (specs->complex_p)
7524                 error ("both %<complex%> and %<void%> in "
7525                        "declaration specifiers");
7526               else if (specs->saturating_p)
7527                 error ("both %<_Sat%> and %<void%> in "
7528                        "declaration specifiers");
7529               else
7530                 specs->typespec_word = cts_void;
7531               return specs;
7532             case RID_BOOL:
7533               if (specs->long_p)
7534                 error ("both %<long%> and %<_Bool%> in "
7535                        "declaration specifiers");
7536               else if (specs->short_p)
7537                 error ("both %<short%> and %<_Bool%> in "
7538                        "declaration specifiers");
7539               else if (specs->signed_p)
7540                 error ("both %<signed%> and %<_Bool%> in "
7541                        "declaration specifiers");
7542               else if (specs->unsigned_p)
7543                 error ("both %<unsigned%> and %<_Bool%> in "
7544                        "declaration specifiers");
7545               else if (specs->complex_p)
7546                 error ("both %<complex%> and %<_Bool%> in "
7547                        "declaration specifiers");
7548               else if (specs->saturating_p)
7549                 error ("both %<_Sat%> and %<_Bool%> in "
7550                        "declaration specifiers");
7551               else
7552                 specs->typespec_word = cts_bool;
7553               return specs;
7554             case RID_CHAR:
7555               if (specs->long_p)
7556                 error ("both %<long%> and %<char%> in "
7557                        "declaration specifiers");
7558               else if (specs->short_p)
7559                 error ("both %<short%> and %<char%> in "
7560                        "declaration specifiers");
7561               else if (specs->saturating_p)
7562                 error ("both %<_Sat%> and %<char%> in "
7563                        "declaration specifiers");
7564               else
7565                 specs->typespec_word = cts_char;
7566               return specs;
7567             case RID_INT:
7568               if (specs->saturating_p)
7569                 error ("both %<_Sat%> and %<int%> in "
7570                        "declaration specifiers");
7571               else
7572                 specs->typespec_word = cts_int;
7573               return specs;
7574             case RID_FLOAT:
7575               if (specs->long_p)
7576                 error ("both %<long%> and %<float%> in "
7577                        "declaration specifiers");
7578               else if (specs->short_p)
7579                 error ("both %<short%> and %<float%> in "
7580                        "declaration specifiers");
7581               else if (specs->signed_p)
7582                 error ("both %<signed%> and %<float%> in "
7583                        "declaration specifiers");
7584               else if (specs->unsigned_p)
7585                 error ("both %<unsigned%> and %<float%> in "
7586                        "declaration specifiers");
7587               else if (specs->saturating_p)
7588                 error ("both %<_Sat%> and %<float%> in "
7589                        "declaration specifiers");
7590               else
7591                 specs->typespec_word = cts_float;
7592               return specs;
7593             case RID_DOUBLE:
7594               if (specs->long_long_p)
7595                 error ("both %<long long%> and %<double%> in "
7596                        "declaration specifiers");
7597               else if (specs->short_p)
7598                 error ("both %<short%> and %<double%> in "
7599                        "declaration specifiers");
7600               else if (specs->signed_p)
7601                 error ("both %<signed%> and %<double%> in "
7602                        "declaration specifiers");
7603               else if (specs->unsigned_p)
7604                 error ("both %<unsigned%> and %<double%> in "
7605                        "declaration specifiers");
7606               else if (specs->saturating_p)
7607                 error ("both %<_Sat%> and %<double%> in "
7608                        "declaration specifiers");
7609               else
7610                 specs->typespec_word = cts_double;
7611               return specs;
7612             case RID_DFLOAT32:
7613             case RID_DFLOAT64:
7614             case RID_DFLOAT128:
7615               { 
7616                 const char *str;
7617                 if (i == RID_DFLOAT32)
7618                   str = "_Decimal32";
7619                 else if (i == RID_DFLOAT64)
7620                   str = "_Decimal64";
7621                 else
7622                   str = "_Decimal128";
7623                 if (specs->long_long_p)
7624                   error ("both %<long long%> and %<%s%> in "
7625                          "declaration specifiers", str);
7626                 if (specs->long_p)
7627                   error ("both %<long%> and %<%s%> in "
7628                          "declaration specifiers", str);
7629                 else if (specs->short_p)
7630                   error ("both %<short%> and %<%s%> in "
7631                          "declaration specifiers", str);
7632                 else if (specs->signed_p)
7633                   error ("both %<signed%> and %<%s%> in "
7634                          "declaration specifiers", str);
7635                 else if (specs->unsigned_p)
7636                   error ("both %<unsigned%> and %<%s%> in "
7637                          "declaration specifiers", str);
7638                 else if (specs->complex_p)
7639                   error ("both %<complex%> and %<%s%> in "
7640                          "declaration specifiers", str);
7641                 else if (specs->saturating_p)
7642                   error ("both %<_Sat%> and %<%s%> in "
7643                          "declaration specifiers", str);
7644                 else if (i == RID_DFLOAT32)
7645                   specs->typespec_word = cts_dfloat32;
7646                 else if (i == RID_DFLOAT64)
7647                   specs->typespec_word = cts_dfloat64;
7648                 else
7649                   specs->typespec_word = cts_dfloat128;
7650               }
7651               if (!targetm.decimal_float_supported_p ())
7652                 error ("decimal floating point not supported for this target");
7653               pedwarn (input_location, OPT_pedantic, 
7654                        "ISO C does not support decimal floating point");
7655               return specs;
7656             case RID_FRACT:
7657             case RID_ACCUM:
7658               {
7659                 const char *str;
7660                 if (i == RID_FRACT)
7661                   str = "_Fract";
7662                 else
7663                   str = "_Accum";
7664                 if (specs->complex_p)
7665                   error ("both %<complex%> and %<%s%> in "
7666                          "declaration specifiers", str);
7667                 else if (i == RID_FRACT)
7668                     specs->typespec_word = cts_fract;
7669                 else
7670                     specs->typespec_word = cts_accum;
7671               }
7672               if (!targetm.fixed_point_supported_p ())
7673                 error ("fixed-point types not supported for this target");
7674               pedwarn (input_location, OPT_pedantic, 
7675                        "ISO C does not support fixed-point types");
7676               return specs;
7677             default:
7678               /* ObjC reserved word "id", handled below.  */
7679               break;
7680             }
7681         }
7682     }
7683
7684   /* Now we have a typedef (a TYPE_DECL node), an identifier (some
7685      form of ObjC type, cases such as "int" and "long" being handled
7686      above), a TYPE (struct, union, enum and typeof specifiers) or an
7687      ERROR_MARK.  In none of these cases may there have previously
7688      been any type specifiers.  */
7689   if (specs->type || specs->typespec_word != cts_none
7690       || specs->long_p || specs->short_p || specs->signed_p
7691       || specs->unsigned_p || specs->complex_p)
7692     error ("two or more data types in declaration specifiers");
7693   else if (TREE_CODE (type) == TYPE_DECL)
7694     {
7695       if (TREE_TYPE (type) == error_mark_node)
7696         ; /* Allow the type to default to int to avoid cascading errors.  */
7697       else
7698         {
7699           specs->type = TREE_TYPE (type);
7700           specs->decl_attr = DECL_ATTRIBUTES (type);
7701           specs->typedef_p = true;
7702           specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
7703         }
7704     }
7705   else if (TREE_CODE (type) == IDENTIFIER_NODE)
7706     {
7707       tree t = lookup_name (type);
7708       if (!t || TREE_CODE (t) != TYPE_DECL)
7709         error ("%qE fails to be a typedef or built in type", type);
7710       else if (TREE_TYPE (t) == error_mark_node)
7711         ;
7712       else
7713         specs->type = TREE_TYPE (t);
7714     }
7715   else if (TREE_CODE (type) != ERROR_MARK)
7716     {
7717       if (spec.kind == ctsk_tagdef || spec.kind == ctsk_tagfirstref)
7718         specs->tag_defined_p = true;
7719       if (spec.kind == ctsk_typeof)
7720         {
7721           specs->typedef_p = true;
7722           if (spec.expr)
7723             {
7724               if (specs->expr)
7725                 specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
7726                                       specs->expr, spec.expr);
7727               else
7728                 specs->expr = spec.expr;
7729               specs->expr_const_operands &= spec.expr_const_operands;
7730             }
7731         }
7732       specs->type = type;
7733     }
7734
7735   return specs;
7736 }
7737
7738 /* Add the storage class specifier or function specifier SCSPEC to the
7739    declaration specifiers SPECS, returning SPECS.  */
7740
7741 struct c_declspecs *
7742 declspecs_add_scspec (struct c_declspecs *specs, tree scspec)
7743 {
7744   enum rid i;
7745   enum c_storage_class n = csc_none;
7746   bool dupe = false;
7747   specs->declspecs_seen_p = true;
7748   gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
7749               && C_IS_RESERVED_WORD (scspec));
7750   i = C_RID_CODE (scspec);
7751   if (specs->non_sc_seen_p)
7752     warning (OPT_Wold_style_declaration, 
7753              "%qE is not at beginning of declaration", scspec);
7754   switch (i)
7755     {
7756     case RID_INLINE:
7757       /* C99 permits duplicate inline.  Although of doubtful utility,
7758          it seems simplest to permit it in gnu89 mode as well, as
7759          there is also little utility in maintaining this as a
7760          difference between gnu89 and C99 inline.  */
7761       dupe = false;
7762       specs->inline_p = true;
7763       break;
7764     case RID_THREAD:
7765       dupe = specs->thread_p;
7766       if (specs->storage_class == csc_auto)
7767         error ("%<__thread%> used with %<auto%>");
7768       else if (specs->storage_class == csc_register)
7769         error ("%<__thread%> used with %<register%>");
7770       else if (specs->storage_class == csc_typedef)
7771         error ("%<__thread%> used with %<typedef%>");
7772       else
7773         specs->thread_p = true;
7774       break;
7775     case RID_AUTO:
7776       n = csc_auto;
7777       break;
7778     case RID_EXTERN:
7779       n = csc_extern;
7780       /* Diagnose "__thread extern".  */
7781       if (specs->thread_p)
7782         error ("%<__thread%> before %<extern%>");
7783       break;
7784     case RID_REGISTER:
7785       n = csc_register;
7786       break;
7787     case RID_STATIC:
7788       n = csc_static;
7789       /* Diagnose "__thread static".  */
7790       if (specs->thread_p)
7791         error ("%<__thread%> before %<static%>");
7792       break;
7793     case RID_TYPEDEF:
7794       n = csc_typedef;
7795       break;
7796     default:
7797       gcc_unreachable ();
7798     }
7799   if (n != csc_none && n == specs->storage_class)
7800     dupe = true;
7801   if (dupe)
7802     error ("duplicate %qE", scspec);
7803   if (n != csc_none)
7804     {
7805       if (specs->storage_class != csc_none && n != specs->storage_class)
7806         {
7807           error ("multiple storage classes in declaration specifiers");
7808         }
7809       else
7810         {
7811           specs->storage_class = n;
7812           if (n != csc_extern && n != csc_static && specs->thread_p)
7813             {
7814               error ("%<__thread%> used with %qE", scspec);
7815               specs->thread_p = false;
7816             }
7817         }
7818     }
7819   return specs;
7820 }
7821
7822 /* Add the attributes ATTRS to the declaration specifiers SPECS,
7823    returning SPECS.  */
7824
7825 struct c_declspecs *
7826 declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
7827 {
7828   specs->attrs = chainon (attrs, specs->attrs);
7829   specs->declspecs_seen_p = true;
7830   return specs;
7831 }
7832
7833 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
7834    specifiers with any other type specifier to determine the resulting
7835    type.  This is where ISO C checks on complex types are made, since
7836    "_Complex long" is a prefix of the valid ISO C type "_Complex long
7837    double".  */
7838
7839 struct c_declspecs *
7840 finish_declspecs (struct c_declspecs *specs)
7841 {
7842   /* If a type was specified as a whole, we have no modifiers and are
7843      done.  */
7844   if (specs->type != NULL_TREE)
7845     {
7846       gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
7847                   && !specs->signed_p && !specs->unsigned_p
7848                   && !specs->complex_p);
7849       return specs;
7850     }
7851
7852   /* If none of "void", "_Bool", "char", "int", "float" or "double"
7853      has been specified, treat it as "int" unless "_Complex" is
7854      present and there are no other specifiers.  If we just have
7855      "_Complex", it is equivalent to "_Complex double", but e.g.
7856      "_Complex short" is equivalent to "_Complex short int".  */
7857   if (specs->typespec_word == cts_none)
7858     {
7859       if (specs->saturating_p)
7860         {
7861           error ("%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
7862           if (!targetm.fixed_point_supported_p ())
7863             error ("fixed-point types not supported for this target");
7864           specs->typespec_word = cts_fract;
7865         }
7866       else if (specs->long_p || specs->short_p
7867                || specs->signed_p || specs->unsigned_p)
7868         {
7869           specs->typespec_word = cts_int;
7870         }
7871       else if (specs->complex_p)
7872         {
7873           specs->typespec_word = cts_double;
7874           pedwarn (input_location, OPT_pedantic, 
7875                    "ISO C does not support plain %<complex%> meaning "
7876                    "%<double complex%>");
7877         }
7878       else
7879         {
7880           specs->typespec_word = cts_int;
7881           specs->default_int_p = true;
7882           /* We don't diagnose this here because grokdeclarator will
7883              give more specific diagnostics according to whether it is
7884              a function definition.  */
7885         }
7886     }
7887
7888   /* If "signed" was specified, record this to distinguish "int" and
7889      "signed int" in the case of a bit-field with
7890      -funsigned-bitfields.  */
7891   specs->explicit_signed_p = specs->signed_p;
7892
7893   /* Now compute the actual type.  */
7894   switch (specs->typespec_word)
7895     {
7896     case cts_void:
7897       gcc_assert (!specs->long_p && !specs->short_p
7898                   && !specs->signed_p && !specs->unsigned_p
7899                   && !specs->complex_p);
7900       specs->type = void_type_node;
7901       break;
7902     case cts_bool:
7903       gcc_assert (!specs->long_p && !specs->short_p
7904                   && !specs->signed_p && !specs->unsigned_p
7905                   && !specs->complex_p);
7906       specs->type = boolean_type_node;
7907       break;
7908     case cts_char:
7909       gcc_assert (!specs->long_p && !specs->short_p);
7910       gcc_assert (!(specs->signed_p && specs->unsigned_p));
7911       if (specs->signed_p)
7912         specs->type = signed_char_type_node;
7913       else if (specs->unsigned_p)
7914         specs->type = unsigned_char_type_node;
7915       else
7916         specs->type = char_type_node;
7917       if (specs->complex_p)
7918         {
7919           pedwarn (input_location, OPT_pedantic, 
7920                    "ISO C does not support complex integer types");
7921           specs->type = build_complex_type (specs->type);
7922         }
7923       break;
7924     case cts_int:
7925       gcc_assert (!(specs->long_p && specs->short_p));
7926       gcc_assert (!(specs->signed_p && specs->unsigned_p));
7927       if (specs->long_long_p)
7928         specs->type = (specs->unsigned_p
7929                        ? long_long_unsigned_type_node
7930                        : long_long_integer_type_node);
7931       else if (specs->long_p)
7932         specs->type = (specs->unsigned_p
7933                        ? long_unsigned_type_node
7934                        : long_integer_type_node);
7935       else if (specs->short_p)
7936         specs->type = (specs->unsigned_p
7937                        ? short_unsigned_type_node
7938                        : short_integer_type_node);
7939       else
7940         specs->type = (specs->unsigned_p
7941                        ? unsigned_type_node
7942                        : integer_type_node);
7943       if (specs->complex_p)
7944         {
7945           pedwarn (input_location, OPT_pedantic, 
7946                    "ISO C does not support complex integer types");
7947           specs->type = build_complex_type (specs->type);
7948         }
7949       break;
7950     case cts_float:
7951       gcc_assert (!specs->long_p && !specs->short_p
7952                   && !specs->signed_p && !specs->unsigned_p);
7953       specs->type = (specs->complex_p
7954                      ? complex_float_type_node
7955                      : float_type_node);
7956       break;
7957     case cts_double:
7958       gcc_assert (!specs->long_long_p && !specs->short_p
7959                   && !specs->signed_p && !specs->unsigned_p);
7960       if (specs->long_p)
7961         {
7962           specs->type = (specs->complex_p
7963                          ? complex_long_double_type_node
7964                          : long_double_type_node);
7965         }
7966       else
7967         {
7968           specs->type = (specs->complex_p
7969                          ? complex_double_type_node
7970                          : double_type_node);
7971         }
7972       break;
7973     case cts_dfloat32:
7974     case cts_dfloat64:
7975     case cts_dfloat128:
7976       gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
7977                   && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
7978       if (specs->typespec_word == cts_dfloat32)
7979         specs->type = dfloat32_type_node;
7980       else if (specs->typespec_word == cts_dfloat64)
7981         specs->type = dfloat64_type_node;
7982       else
7983         specs->type = dfloat128_type_node;
7984       break;
7985     case cts_fract:
7986       gcc_assert (!specs->complex_p);
7987       if (!targetm.fixed_point_supported_p ())
7988         specs->type = integer_type_node;
7989       else if (specs->saturating_p)
7990         {
7991           if (specs->long_long_p)
7992             specs->type = specs->unsigned_p
7993                           ? sat_unsigned_long_long_fract_type_node
7994                           : sat_long_long_fract_type_node;
7995           else if (specs->long_p)
7996             specs->type = specs->unsigned_p
7997                           ? sat_unsigned_long_fract_type_node
7998                           : sat_long_fract_type_node;
7999           else if (specs->short_p)
8000             specs->type = specs->unsigned_p
8001                           ? sat_unsigned_short_fract_type_node
8002                           : sat_short_fract_type_node;
8003           else
8004             specs->type = specs->unsigned_p
8005                           ? sat_unsigned_fract_type_node
8006                           : sat_fract_type_node;
8007         }
8008       else
8009         {
8010           if (specs->long_long_p)
8011             specs->type = specs->unsigned_p
8012                           ? unsigned_long_long_fract_type_node
8013                           : long_long_fract_type_node;
8014           else if (specs->long_p)
8015             specs->type = specs->unsigned_p
8016                           ? unsigned_long_fract_type_node
8017                           : long_fract_type_node;
8018           else if (specs->short_p)
8019             specs->type = specs->unsigned_p
8020                           ? unsigned_short_fract_type_node
8021                           : short_fract_type_node;
8022           else
8023             specs->type = specs->unsigned_p
8024                           ? unsigned_fract_type_node
8025                           : fract_type_node;
8026         }
8027       break;
8028     case cts_accum:
8029       gcc_assert (!specs->complex_p);
8030       if (!targetm.fixed_point_supported_p ())
8031         specs->type = integer_type_node;
8032       else if (specs->saturating_p)
8033         {
8034           if (specs->long_long_p)
8035             specs->type = specs->unsigned_p
8036                           ? sat_unsigned_long_long_accum_type_node
8037                           : sat_long_long_accum_type_node;
8038           else if (specs->long_p)
8039             specs->type = specs->unsigned_p
8040                           ? sat_unsigned_long_accum_type_node
8041                           : sat_long_accum_type_node;
8042           else if (specs->short_p)
8043             specs->type = specs->unsigned_p
8044                           ? sat_unsigned_short_accum_type_node
8045                           : sat_short_accum_type_node;
8046           else
8047             specs->type = specs->unsigned_p
8048                           ? sat_unsigned_accum_type_node
8049                           : sat_accum_type_node;
8050         }
8051       else
8052         {
8053           if (specs->long_long_p)
8054             specs->type = specs->unsigned_p
8055                           ? unsigned_long_long_accum_type_node
8056                           : long_long_accum_type_node;
8057           else if (specs->long_p)
8058             specs->type = specs->unsigned_p
8059                           ? unsigned_long_accum_type_node
8060                           : long_accum_type_node;
8061           else if (specs->short_p)
8062             specs->type = specs->unsigned_p
8063                           ? unsigned_short_accum_type_node
8064                           : short_accum_type_node;
8065           else
8066             specs->type = specs->unsigned_p
8067                           ? unsigned_accum_type_node
8068                           : accum_type_node;
8069         }
8070       break;
8071     default:
8072       gcc_unreachable ();
8073     }
8074
8075   return specs;
8076 }
8077
8078 /* A subroutine of c_write_global_declarations.  Perform final processing
8079    on one file scope's declarations (or the external scope's declarations),
8080    GLOBALS.  */
8081
8082 static void
8083 c_write_global_declarations_1 (tree globals)
8084 {
8085   tree decl;
8086   bool reconsider;
8087
8088   /* Process the decls in the order they were written.  */
8089   for (decl = globals; decl; decl = TREE_CHAIN (decl))
8090     {
8091       /* Check for used but undefined static functions using the C
8092          standard's definition of "used", and set TREE_NO_WARNING so
8093          that check_global_declarations doesn't repeat the check.  */
8094       if (TREE_CODE (decl) == FUNCTION_DECL
8095           && DECL_INITIAL (decl) == 0
8096           && DECL_EXTERNAL (decl)
8097           && !TREE_PUBLIC (decl)
8098           && C_DECL_USED (decl))
8099         {
8100           pedwarn (input_location, 0, "%q+F used but never defined", decl);
8101           TREE_NO_WARNING (decl) = 1;
8102         }
8103
8104       wrapup_global_declaration_1 (decl);
8105     }
8106
8107   do
8108     {
8109       reconsider = false;
8110       for (decl = globals; decl; decl = TREE_CHAIN (decl))
8111         reconsider |= wrapup_global_declaration_2 (decl);
8112     }
8113   while (reconsider);
8114
8115   for (decl = globals; decl; decl = TREE_CHAIN (decl))
8116     check_global_declaration_1 (decl);
8117 }
8118
8119 /* A subroutine of c_write_global_declarations Emit debug information for each
8120    of the declarations in GLOBALS.  */
8121
8122 static void
8123 c_write_global_declarations_2 (tree globals)
8124 {
8125   tree decl;
8126
8127   for (decl = globals; decl ; decl = TREE_CHAIN (decl))
8128     debug_hooks->global_decl (decl);
8129 }
8130
8131 /* Preserve the external declarations scope across a garbage collect.  */
8132 static GTY(()) tree ext_block;
8133
8134 void
8135 c_write_global_declarations (void)
8136 {
8137   tree t;
8138
8139   /* We don't want to do this if generating a PCH.  */
8140   if (pch_file)
8141     return;
8142
8143   /* Don't waste time on further processing if -fsyntax-only or we've
8144      encountered errors.  */
8145   if (flag_syntax_only || errorcount || sorrycount)
8146     return;
8147
8148   /* Close the external scope.  */
8149   ext_block = pop_scope ();
8150   external_scope = 0;
8151   gcc_assert (!current_scope);
8152
8153   if (ext_block)
8154     {
8155       tree tmp = BLOCK_VARS (ext_block);
8156       int flags;
8157       FILE * stream = dump_begin (TDI_tu, &flags);
8158       if (stream && tmp)
8159         {
8160           dump_node (tmp, flags & ~TDF_SLIM, stream);
8161           dump_end (TDI_tu, stream);
8162         }
8163     }
8164
8165   /* Process all file scopes in this compilation, and the external_scope,
8166      through wrapup_global_declarations and check_global_declarations.  */
8167   for (t = all_translation_units; t; t = TREE_CHAIN (t))
8168     c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
8169   c_write_global_declarations_1 (BLOCK_VARS (ext_block));
8170
8171   /* We're done parsing; proceed to optimize and emit assembly.
8172      FIXME: shouldn't be the front end's responsibility to call this.  */
8173   cgraph_optimize ();
8174
8175   /* After cgraph has had a chance to emit everything that's going to
8176      be emitted, output debug information for globals.  */
8177   if (errorcount == 0 && sorrycount == 0)
8178     {
8179       timevar_push (TV_SYMOUT);
8180       for (t = all_translation_units; t; t = TREE_CHAIN (t))
8181         c_write_global_declarations_2 (BLOCK_VARS (DECL_INITIAL (t)));
8182       c_write_global_declarations_2 (BLOCK_VARS (ext_block));
8183       timevar_pop (TV_SYMOUT);
8184     }
8185
8186   ext_block = NULL;
8187 }
8188
8189 #include "gt-c-decl.h"