OSDN Git Service

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