OSDN Git Service

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