OSDN Git Service

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