OSDN Git Service

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