c-common.c (unsigned_conversion_warning): Move warning control from if() to warning...
[platform/upstream/gcc.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
1158         warning (OPT_Wshadow, "%Jdeclaration of %qD shadows "
1159                  "a built-in function", 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           warning (OPT_Wshadow, "%Jdeclaration of %qD shadows "
1274                    "a built-in function", newdecl, newdecl);
1275           /* Discard the old built-in function.  */
1276           return false;
1277         }
1278
1279       if (DECL_INITIAL (newdecl))
1280         {
1281           if (DECL_INITIAL (olddecl))
1282             {
1283               /* If both decls have extern inline and are in the same TU,
1284                  reject the new decl.  */
1285               if (DECL_DECLARED_INLINE_P (olddecl)
1286                   && DECL_EXTERNAL (olddecl)
1287                   && DECL_DECLARED_INLINE_P (newdecl)
1288                   && DECL_EXTERNAL (newdecl)
1289                   && same_translation_unit_p (newdecl, olddecl))
1290                 {
1291                   error ("%Jredefinition of %qD", newdecl, newdecl);
1292                   locate_old_decl (olddecl, error);
1293                   return false;
1294                 }
1295               /* If both decls have not extern inline, reject the new decl.  */
1296               if (!DECL_DECLARED_INLINE_P (olddecl)
1297                   && !DECL_EXTERNAL (olddecl)
1298                   && !DECL_DECLARED_INLINE_P (newdecl)
1299                   && !DECL_EXTERNAL (newdecl))
1300                 {
1301                   error ("%Jredefinition of %qD", newdecl, newdecl);
1302                   locate_old_decl (olddecl, error);
1303                   return false;
1304                 }
1305               /* If the new decl is declared as extern inline, error if they are
1306                  in the same TU, otherwise retain the old decl.  */
1307               if (!DECL_DECLARED_INLINE_P (olddecl)
1308                   && !DECL_EXTERNAL (olddecl)
1309                   && DECL_DECLARED_INLINE_P (newdecl)
1310                   && DECL_EXTERNAL (newdecl))
1311                 {
1312                   if (same_translation_unit_p (newdecl, olddecl))
1313                     {
1314                       error ("%Jredefinition of %qD", newdecl, newdecl);
1315                       locate_old_decl (olddecl, error);
1316                       return false;
1317                     }
1318                   else
1319                     retval = false;
1320                 }
1321            }
1322         }
1323       /* If we have a prototype after an old-style function definition,
1324          the argument types must be checked specially.  */
1325       else if (DECL_INITIAL (olddecl)
1326                && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1327                && TYPE_ACTUAL_ARG_TYPES (oldtype)
1328                && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1329         {
1330           locate_old_decl (olddecl, error);
1331           return false;
1332         }
1333       /* A non-static declaration (even an "extern") followed by a
1334          static declaration is undefined behavior per C99 6.2.2p3-5,7.
1335          The same is true for a static forward declaration at block
1336          scope followed by a non-static declaration/definition at file
1337          scope.  Static followed by non-static at the same scope is
1338          not undefined behavior, and is the most convenient way to get
1339          some effects (see e.g.  what unwind-dw2-fde-glibc.c does to
1340          the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1341          we do diagnose it if -Wtraditional.  */
1342       if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1343         {
1344           /* Two exceptions to the rule.  If olddecl is an extern
1345              inline, or a predeclared function that isn't actually
1346              built in, newdecl silently overrides olddecl.  The latter
1347              occur only in Objective C; see also above.  (FIXME: Make
1348              Objective C use normal builtins.)  */
1349           if (!DECL_IS_BUILTIN (olddecl)
1350               && !(DECL_EXTERNAL (olddecl)
1351                    && DECL_DECLARED_INLINE_P (olddecl)))
1352             {
1353               error ("%Jstatic declaration of %qD follows "
1354                      "non-static declaration", newdecl, newdecl);
1355               locate_old_decl (olddecl, error);
1356             }
1357           return false;
1358         }
1359       else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
1360         {
1361           if (DECL_CONTEXT (olddecl))
1362             {
1363               error ("%Jnon-static declaration of %qD follows "
1364                      "static declaration", newdecl, newdecl);
1365               locate_old_decl (olddecl, error);
1366               return false;
1367             }
1368           else if (warn_traditional)
1369             {
1370               warning (0, "%Jnon-static declaration of %qD follows "
1371                        "static declaration", newdecl, newdecl);
1372               warned = true;
1373             }
1374         }
1375     }
1376   else if (TREE_CODE (newdecl) == VAR_DECL)
1377     {
1378       /* Only variables can be thread-local, and all declarations must
1379          agree on this property.  */
1380       if (DECL_THREAD_LOCAL (newdecl) != DECL_THREAD_LOCAL (olddecl))
1381         {
1382           if (DECL_THREAD_LOCAL (newdecl))
1383             error ("%Jthread-local declaration of %qD follows "
1384                    "non-thread-local declaration", newdecl, newdecl);
1385           else
1386             error ("%Jnon-thread-local declaration of %qD follows "
1387                    "thread-local declaration", newdecl, newdecl);
1388
1389           locate_old_decl (olddecl, error);
1390           return false;
1391         }
1392
1393       /* Multiple initialized definitions are not allowed (6.9p3,5).  */
1394       if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1395         {
1396           error ("%Jredefinition of %qD", newdecl, newdecl);
1397           locate_old_decl (olddecl, error);
1398           return false;
1399         }
1400
1401       /* Objects declared at file scope: if the first declaration had
1402          external linkage (even if it was an external reference) the
1403          second must have external linkage as well, or the behavior is
1404          undefined.  If the first declaration had internal linkage, then
1405          the second must too, or else be an external reference (in which
1406          case the composite declaration still has internal linkage).
1407          As for function declarations, we warn about the static-then-
1408          extern case only for -Wtraditional.  See generally 6.2.2p3-5,7.  */
1409       if (DECL_FILE_SCOPE_P (newdecl)
1410           && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1411         {
1412           if (DECL_EXTERNAL (newdecl))
1413             {
1414               if (!DECL_FILE_SCOPE_P (olddecl))
1415                 {
1416                   error ("%Jextern declaration of %qD follows "
1417                          "declaration with no linkage", newdecl, newdecl);
1418                   locate_old_decl (olddecl, error);
1419                   return false;
1420                 }
1421               else if (warn_traditional)
1422                 {
1423                   warning (0, "%Jnon-static declaration of %qD follows "
1424                            "static declaration", newdecl, newdecl);
1425                   warned = true;
1426                 }
1427             }
1428           else
1429             {
1430               if (TREE_PUBLIC (newdecl))
1431                 error ("%Jnon-static declaration of %qD follows "
1432                        "static declaration", newdecl, newdecl);
1433               else
1434                 error ("%Jstatic declaration of %qD follows "
1435                        "non-static declaration", newdecl, newdecl);
1436
1437               locate_old_decl (olddecl, error);
1438               return false;
1439             }
1440         }
1441       /* Two objects with the same name declared at the same block
1442          scope must both be external references (6.7p3).  */
1443       else if (!DECL_FILE_SCOPE_P (newdecl))
1444         {
1445           if (DECL_EXTERNAL (newdecl))
1446             {
1447               /* Extern with initializer at block scope, which will
1448                  already have received an error.  */
1449             }
1450           else if (DECL_EXTERNAL (olddecl))
1451             {
1452               error ("%Jdeclaration of %qD with no linkage follows "
1453                      "extern declaration", newdecl, newdecl);
1454               locate_old_decl (olddecl, error);
1455             }
1456           else
1457             {
1458               error ("%Jredeclaration of %qD with no linkage",
1459                      newdecl, newdecl);
1460               locate_old_decl (olddecl, error);
1461             }
1462
1463           return false;
1464         }
1465     }
1466
1467   /* warnings */
1468   /* All decls must agree on a visibility.  */
1469   if (DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
1470       && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1471     {
1472       warning (0, "%Jredeclaration of %qD with different visibility "
1473                "(old visibility preserved)", newdecl, newdecl);
1474       warned = true;
1475     }
1476
1477   if (TREE_CODE (newdecl) == FUNCTION_DECL)
1478     {
1479       /* Diagnose inline __attribute__ ((noinline)) which is silly.  */
1480       if (DECL_DECLARED_INLINE_P (newdecl)
1481           && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1482         {
1483           warning (0, "%Jinline declaration of %qD follows "
1484                    "declaration with attribute noinline", newdecl, newdecl);
1485           warned = true;
1486         }
1487       else if (DECL_DECLARED_INLINE_P (olddecl)
1488                && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1489         {
1490           warning (0, "%Jdeclaration of %qD with attribute noinline follows "
1491                    "inline declaration ", newdecl, newdecl);
1492           warned = true;
1493         }
1494
1495       /* Inline declaration after use or definition.
1496          ??? Should we still warn about this now we have unit-at-a-time
1497          mode and can get it right?
1498          Definitely don't complain if the decls are in different translation
1499          units.  */
1500       if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl)
1501           && same_translation_unit_p (olddecl, newdecl))
1502         {
1503           if (TREE_USED (olddecl))
1504             {
1505               warning (0, "%J%qD declared inline after being called",
1506                        olddecl, olddecl);
1507               warned = true;
1508             }
1509           else if (DECL_INITIAL (olddecl))
1510             {
1511               warning (0, "%J%qD declared inline after its definition",
1512                        olddecl, olddecl);
1513               warned = true;
1514             }
1515         }
1516     }
1517   else /* PARM_DECL, VAR_DECL */
1518     {
1519       /* Redeclaration of a parameter is a constraint violation (this is
1520          not explicitly stated, but follows from C99 6.7p3 [no more than
1521          one declaration of the same identifier with no linkage in the
1522          same scope, except type tags] and 6.2.2p6 [parameters have no
1523          linkage]).  We must check for a forward parameter declaration,
1524          indicated by TREE_ASM_WRITTEN on the old declaration - this is
1525          an extension, the mandatory diagnostic for which is handled by
1526          mark_forward_parm_decls.  */
1527
1528       if (TREE_CODE (newdecl) == PARM_DECL
1529           && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1530         {
1531           error ("%Jredefinition of parameter %qD", newdecl, newdecl);
1532           locate_old_decl (olddecl, error);
1533           return false;
1534         }
1535     }
1536
1537   /* Optional warning for completely redundant decls.  */
1538   if (!warned && !pedwarned
1539       && warn_redundant_decls
1540       /* Don't warn about a function declaration followed by a
1541          definition.  */
1542       && !(TREE_CODE (newdecl) == FUNCTION_DECL
1543            && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1544       /* Don't warn about redundant redeclarations of builtins.  */
1545       && !(TREE_CODE (newdecl) == FUNCTION_DECL
1546            && !DECL_BUILT_IN (newdecl)
1547            && DECL_BUILT_IN (olddecl)
1548            && !C_DECL_DECLARED_BUILTIN (olddecl))
1549       /* Don't warn about an extern followed by a definition.  */
1550       && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1551       /* Don't warn about forward parameter decls.  */
1552       && !(TREE_CODE (newdecl) == PARM_DECL
1553            && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl)))
1554     {
1555       warning (0, "%Jredundant redeclaration of %qD", newdecl, newdecl);
1556       warned = true;
1557     }
1558
1559   /* Report location of previous decl/defn in a consistent manner.  */
1560   if (warned || pedwarned)
1561     locate_old_decl (olddecl, pedwarned ? pedwarn : warning0);
1562
1563   return retval;
1564 }
1565
1566 /* Subroutine of duplicate_decls.  NEWDECL has been found to be
1567    consistent with OLDDECL, but carries new information.  Merge the
1568    new information into OLDDECL.  This function issues no
1569    diagnostics.  */
1570
1571 static void
1572 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1573 {
1574   int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1575                            && DECL_INITIAL (newdecl) != 0);
1576   int new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
1577                           && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != 0);
1578   int old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
1579                           && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) != 0);
1580
1581   /* For real parm decl following a forward decl, rechain the old decl
1582      in its new location and clear TREE_ASM_WRITTEN (it's not a
1583      forward decl anymore).  */
1584   if (TREE_CODE (newdecl) == PARM_DECL
1585       && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1586     {
1587       struct c_binding *b, **here;
1588
1589       for (here = &current_scope->bindings; *here; here = &(*here)->prev)
1590         if ((*here)->decl == olddecl)
1591           goto found;
1592       gcc_unreachable ();
1593
1594     found:
1595       b = *here;
1596       *here = b->prev;
1597       b->prev = current_scope->bindings;
1598       current_scope->bindings = b;
1599
1600       TREE_ASM_WRITTEN (olddecl) = 0;
1601     }
1602
1603   DECL_ATTRIBUTES (newdecl)
1604     = targetm.merge_decl_attributes (olddecl, newdecl);
1605
1606   /* Merge the data types specified in the two decls.  */
1607   TREE_TYPE (newdecl)
1608     = TREE_TYPE (olddecl)
1609     = composite_type (newtype, oldtype);
1610
1611   /* Lay the type out, unless already done.  */
1612   if (!comptypes (oldtype, TREE_TYPE (newdecl)))
1613     {
1614       if (TREE_TYPE (newdecl) != error_mark_node)
1615         layout_type (TREE_TYPE (newdecl));
1616       if (TREE_CODE (newdecl) != FUNCTION_DECL
1617           && TREE_CODE (newdecl) != TYPE_DECL
1618           && TREE_CODE (newdecl) != CONST_DECL)
1619         layout_decl (newdecl, 0);
1620     }
1621   else
1622     {
1623       /* Since the type is OLDDECL's, make OLDDECL's size go with.  */
1624       DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1625       DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1626       DECL_MODE (newdecl) = DECL_MODE (olddecl);
1627       if (TREE_CODE (olddecl) != FUNCTION_DECL)
1628         if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1629           {
1630             DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1631             DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1632           }
1633     }
1634
1635   /* Keep the old rtl since we can safely use it.  */
1636   COPY_DECL_RTL (olddecl, newdecl);
1637
1638   /* Merge the type qualifiers.  */
1639   if (TREE_READONLY (newdecl))
1640     TREE_READONLY (olddecl) = 1;
1641
1642   if (TREE_THIS_VOLATILE (newdecl))
1643     {
1644       TREE_THIS_VOLATILE (olddecl) = 1;
1645       if (TREE_CODE (newdecl) == VAR_DECL)
1646         make_var_volatile (newdecl);
1647     }
1648
1649   /* Merge deprecatedness.  */
1650   if (TREE_DEPRECATED (newdecl))
1651     TREE_DEPRECATED (olddecl) = 1;
1652
1653   /* Keep source location of definition rather than declaration and of
1654      prototype rather than non-prototype unless that prototype is
1655      built-in.  */
1656   if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1657       || (old_is_prototype && !new_is_prototype
1658           && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
1659     DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1660
1661   /* Merge the unused-warning information.  */
1662   if (DECL_IN_SYSTEM_HEADER (olddecl))
1663     DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1664   else if (DECL_IN_SYSTEM_HEADER (newdecl))
1665     DECL_IN_SYSTEM_HEADER (olddecl) = 1;
1666
1667   /* Merge the initialization information.  */
1668    if (DECL_INITIAL (newdecl) == 0)
1669     DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1670
1671   /* Merge the section attribute.
1672      We want to issue an error if the sections conflict but that must be
1673      done later in decl_attributes since we are called before attributes
1674      are assigned.  */
1675   if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1676     DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1677
1678   /* Copy the assembler name.
1679      Currently, it can only be defined in the prototype.  */
1680   COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1681
1682   /* Use visibility of whichever declaration had it specified */
1683   if (DECL_VISIBILITY_SPECIFIED (olddecl))
1684     {
1685       DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1686       DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
1687     }
1688
1689   if (TREE_CODE (newdecl) == FUNCTION_DECL)
1690     {
1691       DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1692       DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1693       DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1694       DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1695         |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1696       TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1697       TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1698       DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1699       DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1700       DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
1701     }
1702
1703   /* Merge the storage class information.  */
1704   merge_weak (newdecl, olddecl);
1705
1706   /* For functions, static overrides non-static.  */
1707   if (TREE_CODE (newdecl) == FUNCTION_DECL)
1708     {
1709       TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1710       /* This is since we don't automatically
1711          copy the attributes of NEWDECL into OLDDECL.  */
1712       TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1713       /* If this clears `static', clear it in the identifier too.  */
1714       if (!TREE_PUBLIC (olddecl))
1715         TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1716     }
1717   if (DECL_EXTERNAL (newdecl))
1718     {
1719       TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1720       DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1721
1722       /* An extern decl does not override previous storage class.  */
1723       TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1724       if (!DECL_EXTERNAL (newdecl))
1725         {
1726           DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1727           DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1728         }
1729     }
1730   else
1731     {
1732       TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1733       TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1734     }
1735
1736   if (TREE_CODE (newdecl) == FUNCTION_DECL)
1737     {
1738       /* If we're redefining a function previously defined as extern
1739          inline, make sure we emit debug info for the inline before we
1740          throw it away, in case it was inlined into a function that hasn't
1741          been written out yet.  */
1742       if (new_is_definition && DECL_INITIAL (olddecl))
1743         {
1744           if (TREE_USED (olddecl)
1745               /* In unit-at-a-time mode we never inline re-defined extern
1746                  inline functions.  */
1747               && !flag_unit_at_a_time
1748               && cgraph_function_possibly_inlined_p (olddecl))
1749             (*debug_hooks->outlining_inline_function) (olddecl);
1750
1751           /* The new defn must not be inline.  */
1752           DECL_INLINE (newdecl) = 0;
1753           DECL_UNINLINABLE (newdecl) = 1;
1754         }
1755       else
1756         {
1757           /* If either decl says `inline', this fn is inline,
1758              unless its definition was passed already.  */
1759           if (DECL_DECLARED_INLINE_P (newdecl)
1760               || DECL_DECLARED_INLINE_P (olddecl))
1761             DECL_DECLARED_INLINE_P (newdecl) = 1;
1762
1763           DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1764             = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1765         }
1766
1767       if (DECL_BUILT_IN (olddecl))
1768         {
1769           /* If redeclaring a builtin function, it stays built in.
1770              But it gets tagged as having been declared.  */
1771           DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1772           DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1773           C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1774           if (new_is_prototype)
1775             C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
1776           else
1777             C_DECL_BUILTIN_PROTOTYPE (newdecl)
1778               = C_DECL_BUILTIN_PROTOTYPE (olddecl);
1779         }
1780
1781       /* Also preserve various other info from the definition.  */
1782       if (!new_is_definition)
1783         {
1784           DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1785           DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1786           DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1787           DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1788           DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1789
1790           /* Set DECL_INLINE on the declaration if we've got a body
1791              from which to instantiate.  */
1792           if (DECL_INLINE (olddecl) && !DECL_UNINLINABLE (newdecl))
1793             {
1794               DECL_INLINE (newdecl) = 1;
1795               DECL_ABSTRACT_ORIGIN (newdecl)
1796                 = DECL_ABSTRACT_ORIGIN (olddecl);
1797             }
1798         }
1799       else
1800         {
1801           /* If a previous declaration said inline, mark the
1802              definition as inlinable.  */
1803           if (DECL_DECLARED_INLINE_P (newdecl)
1804               && !DECL_UNINLINABLE (newdecl))
1805             DECL_INLINE (newdecl) = 1;
1806         }
1807     }
1808
1809   /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1810      But preserve OLDDECL's DECL_UID and DECL_CONTEXT.  */
1811   {
1812     unsigned olddecl_uid = DECL_UID (olddecl);
1813     tree olddecl_context = DECL_CONTEXT (olddecl);
1814
1815     memcpy ((char *) olddecl + sizeof (struct tree_common),
1816             (char *) newdecl + sizeof (struct tree_common),
1817             sizeof (struct tree_decl) - sizeof (struct tree_common));
1818     DECL_UID (olddecl) = olddecl_uid;
1819     DECL_CONTEXT (olddecl) = olddecl_context;
1820   }
1821
1822   /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1823      so that encode_section_info has a chance to look at the new decl
1824      flags and attributes.  */
1825   if (DECL_RTL_SET_P (olddecl)
1826       && (TREE_CODE (olddecl) == FUNCTION_DECL
1827           || (TREE_CODE (olddecl) == VAR_DECL
1828               && TREE_STATIC (olddecl))))
1829     make_decl_rtl (olddecl);
1830 }
1831
1832 /* Handle when a new declaration NEWDECL has the same name as an old
1833    one OLDDECL in the same binding contour.  Prints an error message
1834    if appropriate.
1835
1836    If safely possible, alter OLDDECL to look like NEWDECL, and return
1837    true.  Otherwise, return false.  */
1838
1839 static bool
1840 duplicate_decls (tree newdecl, tree olddecl)
1841 {
1842   tree newtype = NULL, oldtype = NULL;
1843
1844   if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
1845     return false;
1846
1847   merge_decls (newdecl, olddecl, newtype, oldtype);
1848   return true;
1849 }
1850
1851 \f
1852 /* Check whether decl-node NEW_DECL shadows an existing declaration.  */
1853 static void
1854 warn_if_shadowing (tree new_decl)
1855 {
1856   struct c_binding *b;
1857
1858   /* Shadow warnings wanted?  */
1859   if (!warn_shadow
1860       /* No shadow warnings for internally generated vars.  */
1861       || DECL_IS_BUILTIN (new_decl)
1862       /* No shadow warnings for vars made for inlining.  */
1863       || DECL_FROM_INLINE (new_decl)
1864       /* Don't warn about the parm names in function declarator
1865          within a function declarator.  It would be nice to avoid
1866          warning in any function declarator in a declaration, as
1867          opposed to a definition, but there is no way to tell
1868          it's not a definition at this point.  */
1869       || (TREE_CODE (new_decl) == PARM_DECL && current_scope->outer->parm_flag))
1870     return;
1871
1872   /* Is anything being shadowed?  Invisible decls do not count.  */
1873   for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
1874     if (b->decl && b->decl != new_decl && !b->invisible)
1875       {
1876         tree old_decl = b->decl;
1877
1878         if (old_decl == error_mark_node)
1879           {
1880             warning (0, "%Jdeclaration of %qD shadows previous non-variable",
1881                      new_decl, new_decl);
1882             break;
1883           }
1884         else if (TREE_CODE (old_decl) == PARM_DECL)
1885           warning (0, "%Jdeclaration of %qD shadows a parameter",
1886                    new_decl, new_decl);
1887         else if (DECL_FILE_SCOPE_P (old_decl))
1888           warning (0, "%Jdeclaration of %qD shadows a global declaration",
1889                    new_decl, new_decl);
1890         else if (TREE_CODE (old_decl) == FUNCTION_DECL
1891                  && DECL_BUILT_IN (old_decl))
1892           {
1893             warning (0, "%Jdeclaration of %qD shadows a built-in function",
1894                      new_decl, new_decl);
1895             break;
1896           }
1897         else
1898           warning (0, "%Jdeclaration of %qD shadows a previous local",
1899                    new_decl, new_decl);
1900
1901         warning (0, "%Jshadowed declaration is here", old_decl);
1902
1903         break;
1904       }
1905 }
1906
1907
1908 /* Subroutine of pushdecl.
1909
1910    X is a TYPE_DECL for a typedef statement.  Create a brand new
1911    ..._TYPE node (which will be just a variant of the existing
1912    ..._TYPE node with identical properties) and then install X
1913    as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1914
1915    The whole point here is to end up with a situation where each
1916    and every ..._TYPE node the compiler creates will be uniquely
1917    associated with AT MOST one node representing a typedef name.
1918    This way, even though the compiler substitutes corresponding
1919    ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1920    early on, later parts of the compiler can always do the reverse
1921    translation and get back the corresponding typedef name.  For
1922    example, given:
1923
1924         typedef struct S MY_TYPE;
1925         MY_TYPE object;
1926
1927    Later parts of the compiler might only know that `object' was of
1928    type `struct S' if it were not for code just below.  With this
1929    code however, later parts of the compiler see something like:
1930
1931         struct S' == struct S
1932         typedef struct S' MY_TYPE;
1933         struct S' object;
1934
1935     And they can then deduce (from the node for type struct S') that
1936     the original object declaration was:
1937
1938                 MY_TYPE object;
1939
1940     Being able to do this is important for proper support of protoize,
1941     and also for generating precise symbolic debugging information
1942     which takes full account of the programmer's (typedef) vocabulary.
1943
1944     Obviously, we don't want to generate a duplicate ..._TYPE node if
1945     the TYPE_DECL node that we are now processing really represents a
1946     standard built-in type.
1947
1948     Since all standard types are effectively declared at line zero
1949     in the source file, we can easily check to see if we are working
1950     on a standard type by checking the current value of lineno.  */
1951
1952 static void
1953 clone_underlying_type (tree x)
1954 {
1955   if (DECL_IS_BUILTIN (x))
1956     {
1957       if (TYPE_NAME (TREE_TYPE (x)) == 0)
1958         TYPE_NAME (TREE_TYPE (x)) = x;
1959     }
1960   else if (TREE_TYPE (x) != error_mark_node
1961            && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
1962     {
1963       tree tt = TREE_TYPE (x);
1964       DECL_ORIGINAL_TYPE (x) = tt;
1965       tt = build_variant_type_copy (tt);
1966       TYPE_NAME (tt) = x;
1967       TREE_USED (tt) = TREE_USED (x);
1968       TREE_TYPE (x) = tt;
1969     }
1970 }
1971
1972 /* Record a decl-node X as belonging to the current lexical scope.
1973    Check for errors (such as an incompatible declaration for the same
1974    name already seen in the same scope).
1975
1976    Returns either X or an old decl for the same name.
1977    If an old decl is returned, it may have been smashed
1978    to agree with what X says.  */
1979
1980 tree
1981 pushdecl (tree x)
1982 {
1983   tree name = DECL_NAME (x);
1984   struct c_scope *scope = current_scope;
1985   struct c_binding *b;
1986   bool nested = false;
1987
1988   /* Functions need the lang_decl data.  */
1989   if (TREE_CODE (x) == FUNCTION_DECL && !DECL_LANG_SPECIFIC (x))
1990     DECL_LANG_SPECIFIC (x) = GGC_CNEW (struct lang_decl);
1991
1992   /* Must set DECL_CONTEXT for everything not at file scope or
1993      DECL_FILE_SCOPE_P won't work.  Local externs don't count
1994      unless they have initializers (which generate code).  */
1995   if (current_function_decl
1996       && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
1997           || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
1998     DECL_CONTEXT (x) = current_function_decl;
1999
2000   /* If this is of variably modified type, prevent jumping into its
2001      scope.  */
2002   if ((TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == TYPE_DECL)
2003       && variably_modified_type_p (TREE_TYPE (x), NULL_TREE))
2004     c_begin_vm_scope (scope->depth);
2005
2006   /* Anonymous decls are just inserted in the scope.  */
2007   if (!name)
2008     {
2009       bind (name, x, scope, /*invisible=*/false, /*nested=*/false);
2010       return x;
2011     }
2012
2013   /* First, see if there is another declaration with the same name in
2014      the current scope.  If there is, duplicate_decls may do all the
2015      work for us.  If duplicate_decls returns false, that indicates
2016      two incompatible decls in the same scope; we are to silently
2017      replace the old one (duplicate_decls has issued all appropriate
2018      diagnostics).  In particular, we should not consider possible
2019      duplicates in the external scope, or shadowing.  */
2020   b = I_SYMBOL_BINDING (name);
2021   if (b && B_IN_SCOPE (b, scope))
2022     {
2023       struct c_binding *b_ext, *b_use;
2024       tree type = TREE_TYPE (x);
2025       tree visdecl = b->decl;
2026       tree vistype = TREE_TYPE (visdecl);
2027       if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2028           && COMPLETE_TYPE_P (TREE_TYPE (x)))
2029         b->inner_comp = false;
2030       b_use = b;
2031       b_ext = b;
2032       /* If this is an external linkage declaration, we should check
2033          for compatibility with the type in the external scope before
2034          setting the type at this scope based on the visible
2035          information only.  */
2036       if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2037         {
2038           while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2039             b_ext = b_ext->shadowed;
2040           if (b_ext)
2041             {
2042               b_use = b_ext;
2043               if (b_use->type)
2044                 TREE_TYPE (b_use->decl) = b_use->type;
2045             }
2046         }
2047       if (duplicate_decls (x, b_use->decl))
2048         {
2049           if (b_use != b)
2050             {
2051               /* Save the updated type in the external scope and
2052                  restore the proper type for this scope.  */
2053               tree thistype;
2054               if (comptypes (vistype, type))
2055                 thistype = composite_type (vistype, type);
2056               else
2057                 thistype = TREE_TYPE (b_use->decl);
2058               b_use->type = TREE_TYPE (b_use->decl);
2059               if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2060                   && DECL_BUILT_IN (b_use->decl))
2061                 thistype
2062                   = build_type_attribute_variant (thistype,
2063                                                   TYPE_ATTRIBUTES
2064                                                   (b_use->type));
2065               TREE_TYPE (b_use->decl) = thistype;
2066             }
2067           return b_use->decl;
2068         }
2069       else
2070         goto skip_external_and_shadow_checks;
2071     }
2072
2073   /* All declarations with external linkage, and all external
2074      references, go in the external scope, no matter what scope is
2075      current.  However, the binding in that scope is ignored for
2076      purposes of normal name lookup.  A separate binding structure is
2077      created in the requested scope; this governs the normal
2078      visibility of the symbol.
2079
2080      The binding in the externals scope is used exclusively for
2081      detecting duplicate declarations of the same object, no matter
2082      what scope they are in; this is what we do here.  (C99 6.2.7p2:
2083      All declarations that refer to the same object or function shall
2084      have compatible type; otherwise, the behavior is undefined.)  */
2085   if (DECL_EXTERNAL (x) || scope == file_scope)
2086     {
2087       tree type = TREE_TYPE (x);
2088       tree vistype = 0;
2089       tree visdecl = 0;
2090       bool type_saved = false;
2091       if (b && !B_IN_EXTERNAL_SCOPE (b)
2092           && (TREE_CODE (b->decl) == FUNCTION_DECL
2093               || TREE_CODE (b->decl) == VAR_DECL)
2094           && DECL_FILE_SCOPE_P (b->decl))
2095         {
2096           visdecl = b->decl;
2097           vistype = TREE_TYPE (visdecl);
2098         }
2099       if (warn_nested_externs
2100           && scope != file_scope
2101           && !DECL_IN_SYSTEM_HEADER (x))
2102         warning (0, "nested extern declaration of %qD", x);
2103
2104       while (b && !B_IN_EXTERNAL_SCOPE (b))
2105         {
2106           /* If this decl might be modified, save its type.  This is
2107              done here rather than when the decl is first bound
2108              because the type may change after first binding, through
2109              being completed or through attributes being added.  If we
2110              encounter multiple such decls, only the first should have
2111              its type saved; the others will already have had their
2112              proper types saved and the types will not have changed as
2113              their scopes will not have been re-entered.  */
2114           if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2115             {
2116               b->type = TREE_TYPE (b->decl);
2117               type_saved = true;
2118             }
2119           if (B_IN_FILE_SCOPE (b)
2120               && TREE_CODE (b->decl) == VAR_DECL
2121               && TREE_STATIC (b->decl)
2122               && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2123               && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2124               && TREE_CODE (type) == ARRAY_TYPE
2125               && TYPE_DOMAIN (type)
2126               && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2127               && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2128             {
2129               /* Array type completed in inner scope, which should be
2130                  diagnosed if the completion does not have size 1 and
2131                  it does not get completed in the file scope.  */
2132               b->inner_comp = true;
2133             }
2134           b = b->shadowed;
2135         }
2136
2137       /* If a matching external declaration has been found, set its
2138          type to the composite of all the types of that declaration.
2139          After the consistency checks, it will be reset to the
2140          composite of the visible types only.  */
2141       if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2142           && b->type)
2143         TREE_TYPE (b->decl) = b->type;
2144
2145       /* The point of the same_translation_unit_p check here is,
2146          we want to detect a duplicate decl for a construct like
2147          foo() { extern bar(); } ... static bar();  but not if
2148          they are in different translation units.  In any case,
2149          the static does not go in the externals scope.  */
2150       if (b
2151           && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2152           && duplicate_decls (x, b->decl))
2153         {
2154           tree thistype;
2155           if (vistype)
2156             {
2157               if (comptypes (vistype, type))
2158                 thistype = composite_type (vistype, type);
2159               else
2160                 thistype = TREE_TYPE (b->decl);
2161             }
2162           else
2163             thistype = type;
2164           b->type = TREE_TYPE (b->decl);
2165           if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2166             thistype
2167               = build_type_attribute_variant (thistype,
2168                                               TYPE_ATTRIBUTES (b->type));
2169           TREE_TYPE (b->decl) = thistype;
2170           bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true);
2171           return b->decl;
2172         }
2173       else if (TREE_PUBLIC (x))
2174         {
2175           if (visdecl && !b && duplicate_decls (x, visdecl))
2176             {
2177               /* An external declaration at block scope referring to a
2178                  visible entity with internal linkage.  The composite
2179                  type will already be correct for this scope, so we
2180                  just need to fall through to make the declaration in
2181                  this scope.  */
2182               nested = true;
2183               x = visdecl;
2184             }
2185           else
2186             {
2187               bind (name, x, external_scope, /*invisible=*/true,
2188                     /*nested=*/false);
2189               nested = true;
2190             }
2191         }
2192     }
2193
2194   warn_if_shadowing (x);
2195
2196  skip_external_and_shadow_checks:
2197   if (TREE_CODE (x) == TYPE_DECL)
2198     clone_underlying_type (x);
2199
2200   bind (name, x, scope, /*invisible=*/false, nested);
2201
2202   /* If x's type is incomplete because it's based on a
2203      structure or union which has not yet been fully declared,
2204      attach it to that structure or union type, so we can go
2205      back and complete the variable declaration later, if the
2206      structure or union gets fully declared.
2207
2208      If the input is erroneous, we can have error_mark in the type
2209      slot (e.g. "f(void a, ...)") - that doesn't count as an
2210      incomplete type.  */
2211   if (TREE_TYPE (x) != error_mark_node
2212       && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2213     {
2214       tree element = TREE_TYPE (x);
2215
2216       while (TREE_CODE (element) == ARRAY_TYPE)
2217         element = TREE_TYPE (element);
2218       element = TYPE_MAIN_VARIANT (element);
2219
2220       if ((TREE_CODE (element) == RECORD_TYPE
2221            || TREE_CODE (element) == UNION_TYPE)
2222           && (TREE_CODE (x) != TYPE_DECL
2223               || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
2224           && !COMPLETE_TYPE_P (element))
2225         C_TYPE_INCOMPLETE_VARS (element)
2226           = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
2227     }
2228   return x;
2229 }
2230
2231 /* Record X as belonging to file scope.
2232    This is used only internally by the Objective-C front end,
2233    and is limited to its needs.  duplicate_decls is not called;
2234    if there is any preexisting decl for this identifier, it is an ICE.  */
2235
2236 tree
2237 pushdecl_top_level (tree x)
2238 {
2239   tree name;
2240   bool nested = false;
2241   gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
2242
2243   name = DECL_NAME (x);
2244
2245  gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
2246
2247   if (TREE_PUBLIC (x))
2248     {
2249       bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false);
2250       nested = true;
2251     }
2252   if (file_scope)
2253     bind (name, x, file_scope, /*invisible=*/false, nested);
2254
2255   return x;
2256 }
2257 \f
2258 static void
2259 implicit_decl_warning (tree id, tree olddecl)
2260 {
2261   void (*diag) (const char *, ...);
2262   switch (mesg_implicit_function_declaration)
2263     {
2264     case 0: return;
2265     case 1: diag = warning0; break;
2266     case 2: diag = error;   break;
2267     default: gcc_unreachable ();
2268     }
2269
2270   diag (N_("implicit declaration of function %qE"), id);
2271   if (olddecl)
2272     locate_old_decl (olddecl, diag);
2273 }
2274
2275 /* Generate an implicit declaration for identifier FUNCTIONID as a
2276    function of type int ().  */
2277
2278 tree
2279 implicitly_declare (tree functionid)
2280 {
2281   struct c_binding *b;
2282   tree decl = 0;
2283   tree asmspec_tree;
2284
2285   for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
2286     {
2287       if (B_IN_SCOPE (b, external_scope))
2288         {
2289           decl = b->decl;
2290           break;
2291         }
2292     }
2293
2294   if (decl)
2295     {
2296       if (decl == error_mark_node)
2297         return decl;
2298
2299       /* FIXME: Objective-C has weird not-really-builtin functions
2300          which are supposed to be visible automatically.  They wind up
2301          in the external scope because they're pushed before the file
2302          scope gets created.  Catch this here and rebind them into the
2303          file scope.  */
2304       if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2305         {
2306           bind (functionid, decl, file_scope,
2307                 /*invisible=*/false, /*nested=*/true);
2308           return decl;
2309         }
2310       else
2311         {
2312           tree newtype = default_function_type;
2313           if (b->type)
2314             TREE_TYPE (decl) = b->type;
2315           /* Implicit declaration of a function already declared
2316              (somehow) in a different scope, or as a built-in.
2317              If this is the first time this has happened, warn;
2318              then recycle the old declaration but with the new type.  */
2319           if (!C_DECL_IMPLICIT (decl))
2320             {
2321               implicit_decl_warning (functionid, decl);
2322               C_DECL_IMPLICIT (decl) = 1;
2323             }
2324           if (DECL_BUILT_IN (decl))
2325             {
2326               newtype = build_type_attribute_variant (newtype,
2327                                                       TYPE_ATTRIBUTES
2328                                                       (TREE_TYPE (decl)));
2329               if (!comptypes (newtype, TREE_TYPE (decl)))
2330                 {
2331                   warning (0, "incompatible implicit declaration of built-in"
2332                            " function %qD", decl);
2333                   newtype = TREE_TYPE (decl);
2334                 }
2335             }
2336           else
2337             {
2338               if (!comptypes (newtype, TREE_TYPE (decl)))
2339                 {
2340                   error ("incompatible implicit declaration of function %qD",
2341                          decl);
2342                   locate_old_decl (decl, error);
2343                 }
2344             }
2345           b->type = TREE_TYPE (decl);
2346           TREE_TYPE (decl) = newtype;
2347           bind (functionid, decl, current_scope,
2348                 /*invisible=*/false, /*nested=*/true);
2349           return decl;
2350         }
2351     }
2352
2353   /* Not seen before.  */
2354   decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2355   DECL_EXTERNAL (decl) = 1;
2356   TREE_PUBLIC (decl) = 1;
2357   C_DECL_IMPLICIT (decl) = 1;
2358   implicit_decl_warning (functionid, 0);
2359   asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
2360   if (asmspec_tree)
2361     set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
2362
2363   /* C89 says implicit declarations are in the innermost block.
2364      So we record the decl in the standard fashion.  */
2365   decl = pushdecl (decl);
2366
2367   /* No need to call objc_check_decl here - it's a function type.  */
2368   rest_of_decl_compilation (decl, 0, 0);
2369
2370   /* Write a record describing this implicit function declaration
2371      to the prototypes file (if requested).  */
2372   gen_aux_info_record (decl, 0, 1, 0);
2373
2374   /* Possibly apply some default attributes to this implicit declaration.  */
2375   decl_attributes (&decl, NULL_TREE, 0);
2376
2377   return decl;
2378 }
2379
2380 /* Issue an error message for a reference to an undeclared variable
2381    ID, including a reference to a builtin outside of function-call
2382    context.  Establish a binding of the identifier to error_mark_node
2383    in an appropriate scope, which will suppress further errors for the
2384    same identifier.  The error message should be given location LOC.  */
2385 void
2386 undeclared_variable (tree id, location_t loc)
2387 {
2388   static bool already = false;
2389   struct c_scope *scope;
2390
2391   if (current_function_decl == 0)
2392     {
2393       error ("%H%qE undeclared here (not in a function)", &loc, id);
2394       scope = current_scope;
2395     }
2396   else
2397     {
2398       error ("%H%qE undeclared (first use in this function)", &loc, id);
2399
2400       if (!already)
2401         {
2402           error ("%H(Each undeclared identifier is reported only once", &loc);
2403           error ("%Hfor each function it appears in.)", &loc);
2404           already = true;
2405         }
2406
2407       /* If we are parsing old-style parameter decls, current_function_decl
2408          will be nonnull but current_function_scope will be null.  */
2409       scope = current_function_scope ? current_function_scope : current_scope;
2410     }
2411   bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false);
2412 }
2413 \f
2414 /* Subroutine of lookup_label, declare_label, define_label: construct a
2415    LABEL_DECL with all the proper frills.  */
2416
2417 static tree
2418 make_label (tree name, location_t location)
2419 {
2420   tree label = build_decl (LABEL_DECL, name, void_type_node);
2421
2422   DECL_CONTEXT (label) = current_function_decl;
2423   DECL_MODE (label) = VOIDmode;
2424   DECL_SOURCE_LOCATION (label) = location;
2425
2426   return label;
2427 }
2428
2429 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2430    Create one if none exists so far for the current function.
2431    This is called when a label is used in a goto expression or
2432    has its address taken.  */
2433
2434 tree
2435 lookup_label (tree name)
2436 {
2437   tree label;
2438
2439   if (current_function_decl == 0)
2440     {
2441       error ("label %qE referenced outside of any function", name);
2442       return 0;
2443     }
2444
2445   /* Use a label already defined or ref'd with this name, but not if
2446      it is inherited from a containing function and wasn't declared
2447      using __label__.  */
2448   label = I_LABEL_DECL (name);
2449   if (label && (DECL_CONTEXT (label) == current_function_decl
2450                 || C_DECLARED_LABEL_FLAG (label)))
2451     {
2452       /* If the label has only been declared, update its apparent
2453          location to point here, for better diagnostics if it
2454          turns out not to have been defined.  */
2455       if (!TREE_USED (label))
2456         DECL_SOURCE_LOCATION (label) = input_location;
2457       return label;
2458     }
2459
2460   /* No label binding for that identifier; make one.  */
2461   label = make_label (name, input_location);
2462
2463   /* Ordinary labels go in the current function scope.  */
2464   bind (name, label, current_function_scope,
2465         /*invisible=*/false, /*nested=*/false);
2466   return label;
2467 }
2468
2469 /* Make a label named NAME in the current function, shadowing silently
2470    any that may be inherited from containing functions or containing
2471    scopes.  This is called for __label__ declarations.  */
2472
2473 tree
2474 declare_label (tree name)
2475 {
2476   struct c_binding *b = I_LABEL_BINDING (name);
2477   tree label;
2478
2479   /* Check to make sure that the label hasn't already been declared
2480      at this scope */
2481   if (b && B_IN_CURRENT_SCOPE (b))
2482     {
2483       error ("duplicate label declaration %qE", name);
2484       locate_old_decl (b->decl, error);
2485
2486       /* Just use the previous declaration.  */
2487       return b->decl;
2488     }
2489
2490   label = make_label (name, input_location);
2491   C_DECLARED_LABEL_FLAG (label) = 1;
2492
2493   /* Declared labels go in the current scope.  */
2494   bind (name, label, current_scope,
2495         /*invisible=*/false, /*nested=*/false);
2496   return label;
2497 }
2498
2499 /* Define a label, specifying the location in the source file.
2500    Return the LABEL_DECL node for the label, if the definition is valid.
2501    Otherwise return 0.  */
2502
2503 tree
2504 define_label (location_t location, tree name)
2505 {
2506   /* Find any preexisting label with this name.  It is an error
2507      if that label has already been defined in this function, or
2508      if there is a containing function with a declared label with
2509      the same name.  */
2510   tree label = I_LABEL_DECL (name);
2511   struct c_label_list *nlist_se, *nlist_vm;
2512
2513   if (label
2514       && ((DECL_CONTEXT (label) == current_function_decl
2515            && DECL_INITIAL (label) != 0)
2516           || (DECL_CONTEXT (label) != current_function_decl
2517               && C_DECLARED_LABEL_FLAG (label))))
2518     {
2519       error ("%Hduplicate label %qD", &location, label);
2520       locate_old_decl (label, error);
2521       return 0;
2522     }
2523   else if (label && DECL_CONTEXT (label) == current_function_decl)
2524     {
2525       /* The label has been used or declared already in this function,
2526          but not defined.  Update its location to point to this
2527          definition.  */
2528       if (C_DECL_UNDEFINABLE_STMT_EXPR (label))
2529         error ("%Jjump into statement expression", label);
2530       if (C_DECL_UNDEFINABLE_VM (label))
2531         error ("%Jjump into scope of identifier with variably modified type",
2532                label);
2533       DECL_SOURCE_LOCATION (label) = location;
2534     }
2535   else
2536     {
2537       /* No label binding for that identifier; make one.  */
2538       label = make_label (name, location);
2539
2540       /* Ordinary labels go in the current function scope.  */
2541       bind (name, label, current_function_scope,
2542             /*invisible=*/false, /*nested=*/false);
2543     }
2544
2545   if (!in_system_header && lookup_name (name))
2546     warning (OPT_Wtraditional, "%Htraditional C lacks a separate namespace "
2547              "for labels, identifier %qE conflicts", &location, name);
2548
2549   nlist_se = XOBNEW (&parser_obstack, struct c_label_list);
2550   nlist_se->next = label_context_stack_se->labels_def;
2551   nlist_se->label = label;
2552   label_context_stack_se->labels_def = nlist_se;
2553
2554   nlist_vm = XOBNEW (&parser_obstack, struct c_label_list);
2555   nlist_vm->next = label_context_stack_vm->labels_def;
2556   nlist_vm->label = label;
2557   label_context_stack_vm->labels_def = nlist_vm;
2558
2559   /* Mark label as having been defined.  */
2560   DECL_INITIAL (label) = error_mark_node;
2561   return label;
2562 }
2563 \f
2564 /* Given NAME, an IDENTIFIER_NODE,
2565    return the structure (or union or enum) definition for that name.
2566    If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2567    CODE says which kind of type the caller wants;
2568    it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2569    If the wrong kind of type is found, an error is reported.  */
2570
2571 static tree
2572 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2573 {
2574   struct c_binding *b = I_TAG_BINDING (name);
2575   int thislevel = 0;
2576
2577   if (!b || !b->decl)
2578     return 0;
2579
2580   /* We only care about whether it's in this level if
2581      thislevel_only was set or it might be a type clash.  */
2582   if (thislevel_only || TREE_CODE (b->decl) != code)
2583     {
2584       /* For our purposes, a tag in the external scope is the same as
2585          a tag in the file scope.  (Primarily relevant to Objective-C
2586          and its builtin structure tags, which get pushed before the
2587          file scope is created.)  */
2588       if (B_IN_CURRENT_SCOPE (b)
2589           || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
2590         thislevel = 1;
2591     }
2592
2593   if (thislevel_only && !thislevel)
2594     return 0;
2595
2596   if (TREE_CODE (b->decl) != code)
2597     {
2598       /* Definition isn't the kind we were looking for.  */
2599       pending_invalid_xref = name;
2600       pending_invalid_xref_location = input_location;
2601
2602       /* If in the same binding level as a declaration as a tag
2603          of a different type, this must not be allowed to
2604          shadow that tag, so give the error immediately.
2605          (For example, "struct foo; union foo;" is invalid.)  */
2606       if (thislevel)
2607         pending_xref_error ();
2608     }
2609   return b->decl;
2610 }
2611
2612 /* Print an error message now
2613    for a recent invalid struct, union or enum cross reference.
2614    We don't print them immediately because they are not invalid
2615    when used in the `struct foo;' construct for shadowing.  */
2616
2617 void
2618 pending_xref_error (void)
2619 {
2620   if (pending_invalid_xref != 0)
2621     error ("%H%qE defined as wrong kind of tag",
2622            &pending_invalid_xref_location, pending_invalid_xref);
2623   pending_invalid_xref = 0;
2624 }
2625
2626 \f
2627 /* Look up NAME in the current scope and its superiors
2628    in the namespace of variables, functions and typedefs.
2629    Return a ..._DECL node of some kind representing its definition,
2630    or return 0 if it is undefined.  */
2631
2632 tree
2633 lookup_name (tree name)
2634 {
2635   struct c_binding *b = I_SYMBOL_BINDING (name);
2636   if (b && !b->invisible)
2637     return b->decl;
2638   return 0;
2639 }
2640
2641 /* Similar to `lookup_name' but look only at the indicated scope.  */
2642
2643 static tree
2644 lookup_name_in_scope (tree name, struct c_scope *scope)
2645 {
2646   struct c_binding *b;
2647
2648   for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2649     if (B_IN_SCOPE (b, scope))
2650       return b->decl;
2651   return 0;
2652 }
2653 \f
2654 /* Create the predefined scalar types of C,
2655    and some nodes representing standard constants (0, 1, (void *) 0).
2656    Initialize the global scope.
2657    Make definitions for built-in primitive functions.  */
2658
2659 void
2660 c_init_decl_processing (void)
2661 {
2662   location_t save_loc = input_location;
2663
2664   /* Initialize reserved words for parser.  */
2665   c_parse_init ();
2666
2667   current_function_decl = 0;
2668
2669   gcc_obstack_init (&parser_obstack);
2670
2671   /* Make the externals scope.  */
2672   push_scope ();
2673   external_scope = current_scope;
2674
2675   /* Declarations from c_common_nodes_and_builtins must not be associated
2676      with this input file, lest we get differences between using and not
2677      using preprocessed headers.  */
2678 #ifdef USE_MAPPED_LOCATION
2679   input_location = BUILTINS_LOCATION;
2680 #else
2681   input_location.file = "<built-in>";
2682   input_location.line = 0;
2683 #endif
2684
2685   build_common_tree_nodes (flag_signed_char, false);
2686
2687   c_common_nodes_and_builtins ();
2688
2689   /* In C, comparisons and TRUTH_* expressions have type int.  */
2690   truthvalue_type_node = integer_type_node;
2691   truthvalue_true_node = integer_one_node;
2692   truthvalue_false_node = integer_zero_node;
2693
2694   /* Even in C99, which has a real boolean type.  */
2695   pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2696                         boolean_type_node));
2697
2698   input_location = save_loc;
2699
2700   pedantic_lvalues = true;
2701
2702   make_fname_decl = c_make_fname_decl;
2703   start_fname_decls ();
2704 }
2705
2706 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2707    decl, NAME is the initialization string and TYPE_DEP indicates whether
2708    NAME depended on the type of the function.  As we don't yet implement
2709    delayed emission of static data, we mark the decl as emitted
2710    so it is not placed in the output.  Anything using it must therefore pull
2711    out the STRING_CST initializer directly.  FIXME.  */
2712
2713 static tree
2714 c_make_fname_decl (tree id, int type_dep)
2715 {
2716   const char *name = fname_as_string (type_dep);
2717   tree decl, type, init;
2718   size_t length = strlen (name);
2719
2720   type = build_array_type (char_type_node,
2721                            build_index_type (size_int (length)));
2722   type = c_build_qualified_type (type, TYPE_QUAL_CONST);
2723
2724   decl = build_decl (VAR_DECL, id, type);
2725
2726   TREE_STATIC (decl) = 1;
2727   TREE_READONLY (decl) = 1;
2728   DECL_ARTIFICIAL (decl) = 1;
2729
2730   init = build_string (length + 1, name);
2731   free ((char *) name);
2732   TREE_TYPE (init) = type;
2733   DECL_INITIAL (decl) = init;
2734
2735   TREE_USED (decl) = 1;
2736
2737   if (current_function_decl)
2738     {
2739       DECL_CONTEXT (decl) = current_function_decl;
2740       bind (id, decl, current_function_scope,
2741             /*invisible=*/false, /*nested=*/false);
2742     }
2743
2744   finish_decl (decl, init, NULL_TREE);
2745
2746   return decl;
2747 }
2748
2749 /* Return a definition for a builtin function named NAME and whose data type
2750    is TYPE.  TYPE should be a function type with argument types.
2751    FUNCTION_CODE tells later passes how to compile calls to this function.
2752    See tree.h for its possible values.
2753
2754    If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2755    the name to be called if we can't opencode the function.  If
2756    ATTRS is nonzero, use that for the function's attribute list.  */
2757
2758 tree
2759 builtin_function (const char *name, tree type, int function_code,
2760                   enum built_in_class cl, const char *library_name,
2761                   tree attrs)
2762 {
2763   tree id = get_identifier (name);
2764   tree decl = build_decl (FUNCTION_DECL, id, type);
2765   TREE_PUBLIC (decl) = 1;
2766   DECL_EXTERNAL (decl) = 1;
2767   DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
2768   DECL_BUILT_IN_CLASS (decl) = cl;
2769   DECL_FUNCTION_CODE (decl) = function_code;
2770   C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0);
2771   if (library_name)
2772     SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2773
2774   /* Should never be called on a symbol with a preexisting meaning.  */
2775   gcc_assert (!I_SYMBOL_BINDING (id));
2776
2777   bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false);
2778
2779   /* Builtins in the implementation namespace are made visible without
2780      needing to be explicitly declared.  See push_file_scope.  */
2781   if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2782     {
2783       TREE_CHAIN (decl) = visible_builtins;
2784       visible_builtins = decl;
2785     }
2786
2787   /* Possibly apply some default attributes to this built-in function.  */
2788   if (attrs)
2789     decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2790   else
2791     decl_attributes (&decl, NULL_TREE, 0);
2792
2793   return decl;
2794 }
2795 \f
2796 /* Called when a declaration is seen that contains no names to declare.
2797    If its type is a reference to a structure, union or enum inherited
2798    from a containing scope, shadow that tag name for the current scope
2799    with a forward reference.
2800    If its type defines a new named structure or union
2801    or defines an enum, it is valid but we need not do anything here.
2802    Otherwise, it is an error.  */
2803
2804 void
2805 shadow_tag (const struct c_declspecs *declspecs)
2806 {
2807   shadow_tag_warned (declspecs, 0);
2808 }
2809
2810 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
2811    but no pedwarn.  */
2812 void
2813 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
2814 {
2815   bool found_tag = false;
2816
2817   if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
2818     {
2819       tree value = declspecs->type;
2820       enum tree_code code = TREE_CODE (value);
2821
2822       if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2823         /* Used to test also that TYPE_SIZE (value) != 0.
2824            That caused warning for `struct foo;' at top level in the file.  */
2825         {
2826           tree name = TYPE_NAME (value);
2827           tree t;
2828
2829           found_tag = true;
2830
2831           if (name == 0)
2832             {
2833               if (warned != 1 && code != ENUMERAL_TYPE)
2834                 /* Empty unnamed enum OK */
2835                 {
2836                   pedwarn ("unnamed struct/union that defines no instances");
2837                   warned = 1;
2838                 }
2839             }
2840           else if (!declspecs->tag_defined_p
2841                    && declspecs->storage_class != csc_none)
2842             {
2843               if (warned != 1)
2844                 pedwarn ("empty declaration with storage class specifier "
2845                          "does not redeclare tag");
2846               warned = 1;
2847               pending_xref_error ();
2848             }
2849           else if (!declspecs->tag_defined_p
2850                    && (declspecs->const_p
2851                        || declspecs->volatile_p
2852                        || declspecs->restrict_p))
2853             {
2854               if (warned != 1)
2855                 pedwarn ("empty declaration with type qualifier "
2856                          "does not redeclare tag");
2857               warned = 1;
2858               pending_xref_error ();
2859             }
2860           else
2861             {
2862               pending_invalid_xref = 0;
2863               t = lookup_tag (code, name, 1);
2864
2865               if (t == 0)
2866                 {
2867                   t = make_node (code);
2868                   pushtag (name, t);
2869                 }
2870             }
2871         }
2872       else
2873         {
2874           if (warned != 1 && !in_system_header)
2875             {
2876               pedwarn ("useless type name in empty declaration");
2877               warned = 1;
2878             }
2879         }
2880     }
2881   else if (warned != 1 && !in_system_header && declspecs->typedef_p)
2882     {
2883       pedwarn ("useless type name in empty declaration");
2884       warned = 1;
2885     }
2886
2887   pending_invalid_xref = 0;
2888
2889   if (declspecs->inline_p)
2890     {
2891       error ("%<inline%> in empty declaration");
2892       warned = 1;
2893     }
2894
2895   if (current_scope == file_scope && declspecs->storage_class == csc_auto)
2896     {
2897       error ("%<auto%> in file-scope empty declaration");
2898       warned = 1;
2899     }
2900
2901   if (current_scope == file_scope && declspecs->storage_class == csc_register)
2902     {
2903       error ("%<register%> in file-scope empty declaration");
2904       warned = 1;
2905     }
2906
2907   if (!warned && !in_system_header && declspecs->storage_class != csc_none)
2908     {
2909       warning (0, "useless storage class specifier in empty declaration");
2910       warned = 2;
2911     }
2912
2913   if (!warned && !in_system_header && declspecs->thread_p)
2914     {
2915       warning (0, "useless %<__thread%> in empty declaration");
2916       warned = 2;
2917     }
2918
2919   if (!warned && !in_system_header && (declspecs->const_p
2920                                        || declspecs->volatile_p
2921                                        || declspecs->restrict_p))
2922     {
2923       warning (0, "useless type qualifier in empty declaration");
2924       warned = 2;
2925     }
2926
2927   if (warned != 1)
2928     {
2929       if (!found_tag)
2930         pedwarn ("empty declaration");
2931     }
2932 }
2933 \f
2934
2935 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
2936    bits.  SPECS represents declaration specifiers that the grammar
2937    only permits to contain type qualifiers and attributes.  */
2938
2939 int
2940 quals_from_declspecs (const struct c_declspecs *specs)
2941 {
2942   int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
2943                | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
2944                | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0));
2945   gcc_assert (!specs->type
2946               && !specs->decl_attr
2947               && specs->typespec_word == cts_none
2948               && specs->storage_class == csc_none
2949               && !specs->typedef_p
2950               && !specs->explicit_signed_p
2951               && !specs->deprecated_p
2952               && !specs->long_p
2953               && !specs->long_long_p
2954               && !specs->short_p
2955               && !specs->signed_p
2956               && !specs->unsigned_p
2957               && !specs->complex_p
2958               && !specs->inline_p
2959               && !specs->thread_p);
2960   return quals;
2961 }
2962
2963 /* Construct an array declarator.  EXPR is the expression inside [], or
2964    NULL_TREE.  QUALS are the type qualifiers inside the [] (to be applied
2965    to the pointer to which a parameter array is converted).  STATIC_P is
2966    true if "static" is inside the [], false otherwise.  VLA_UNSPEC_P
2967    is true if the array is [*], a VLA of unspecified length which is
2968    nevertheless a complete type (not currently implemented by GCC),
2969    false otherwise.  The field for the contained declarator is left to be
2970    filled in by set_array_declarator_inner.  */
2971
2972 struct c_declarator *
2973 build_array_declarator (tree expr, struct c_declspecs *quals, bool static_p,
2974                         bool vla_unspec_p)
2975 {
2976   struct c_declarator *declarator = XOBNEW (&parser_obstack,
2977                                             struct c_declarator);
2978   declarator->kind = cdk_array;
2979   declarator->declarator = 0;
2980   declarator->u.array.dimen = expr;
2981   if (quals)
2982     {
2983       declarator->u.array.attrs = quals->attrs;
2984       declarator->u.array.quals = quals_from_declspecs (quals);
2985     }
2986   else
2987     {
2988       declarator->u.array.attrs = NULL_TREE;
2989       declarator->u.array.quals = 0;
2990     }
2991   declarator->u.array.static_p = static_p;
2992   declarator->u.array.vla_unspec_p = vla_unspec_p;
2993   if (pedantic && !flag_isoc99)
2994     {
2995       if (static_p || quals != NULL)
2996         pedwarn ("ISO C90 does not support %<static%> or type "
2997                  "qualifiers in parameter array declarators");
2998       if (vla_unspec_p)
2999         pedwarn ("ISO C90 does not support %<[*]%> array declarators");
3000     }
3001   if (vla_unspec_p)
3002     warning (0, "GCC does not yet properly implement %<[*]%> array declarators");
3003   return declarator;
3004 }
3005
3006 /* Set the contained declarator of an array declarator.  DECL is the
3007    declarator, as constructed by build_array_declarator; INNER is what
3008    appears on the left of the [].  ABSTRACT_P is true if it is an
3009    abstract declarator, false otherwise; this is used to reject static
3010    and type qualifiers in abstract declarators, where they are not in
3011    the C99 grammar (subject to possible change in DR#289).  */
3012
3013 struct c_declarator *
3014 set_array_declarator_inner (struct c_declarator *decl,
3015                             struct c_declarator *inner, bool abstract_p)
3016 {
3017   decl->declarator = inner;
3018   if (abstract_p && (decl->u.array.quals != TYPE_UNQUALIFIED
3019                      || decl->u.array.attrs != NULL_TREE
3020                      || decl->u.array.static_p))
3021     error ("static or type qualifiers in abstract declarator");
3022   return decl;
3023 }
3024 \f
3025 /* Decode a "typename", such as "int **", returning a ..._TYPE node.  */
3026
3027 tree
3028 groktypename (struct c_type_name *type_name)
3029 {
3030   tree type;
3031   tree attrs = type_name->specs->attrs;
3032
3033   type_name->specs->attrs = NULL_TREE;
3034
3035   type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
3036                          false, NULL);
3037
3038   /* Apply attributes.  */
3039   decl_attributes (&type, attrs, 0);
3040
3041   return type;
3042 }
3043
3044 /* Decode a declarator in an ordinary declaration or data definition.
3045    This is called as soon as the type information and variable name
3046    have been parsed, before parsing the initializer if any.
3047    Here we create the ..._DECL node, fill in its type,
3048    and put it on the list of decls for the current context.
3049    The ..._DECL node is returned as the value.
3050
3051    Exception: for arrays where the length is not specified,
3052    the type is left null, to be filled in by `finish_decl'.
3053
3054    Function definitions do not come here; they go to start_function
3055    instead.  However, external and forward declarations of functions
3056    do go through here.  Structure field declarations are done by
3057    grokfield and not through here.  */
3058
3059 tree
3060 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
3061             bool initialized, tree attributes)
3062 {
3063   tree decl;
3064   tree tem;
3065
3066   /* An object declared as __attribute__((deprecated)) suppresses
3067      warnings of uses of other deprecated items.  */
3068   if (lookup_attribute ("deprecated", attributes))
3069     deprecated_state = DEPRECATED_SUPPRESS;
3070
3071   decl = grokdeclarator (declarator, declspecs,
3072                          NORMAL, initialized, NULL);
3073   if (!decl)
3074     return 0;
3075
3076   deprecated_state = DEPRECATED_NORMAL;
3077
3078   if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
3079       && MAIN_NAME_P (DECL_NAME (decl)))
3080     warning (0, "%J%qD is usually a function", decl, decl);
3081
3082   if (initialized)
3083     /* Is it valid for this decl to have an initializer at all?
3084        If not, set INITIALIZED to zero, which will indirectly
3085        tell 'finish_decl' to ignore the initializer once it is parsed.  */
3086     switch (TREE_CODE (decl))
3087       {
3088       case TYPE_DECL:
3089         error ("typedef %qD is initialized (use __typeof__ instead)", decl);
3090         initialized = 0;
3091         break;
3092
3093       case FUNCTION_DECL:
3094         error ("function %qD is initialized like a variable", decl);
3095         initialized = 0;
3096         break;
3097
3098       case PARM_DECL:
3099         /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE.  */
3100         error ("parameter %qD is initialized", decl);
3101         initialized = 0;
3102         break;
3103
3104       default:
3105         /* Don't allow initializations for incomplete types except for
3106            arrays which might be completed by the initialization.  */
3107
3108         /* This can happen if the array size is an undefined macro.
3109            We already gave a warning, so we don't need another one.  */
3110         if (TREE_TYPE (decl) == error_mark_node)
3111           initialized = 0;
3112         else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
3113           {
3114             /* A complete type is ok if size is fixed.  */
3115
3116             if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
3117                 || C_DECL_VARIABLE_SIZE (decl))
3118               {
3119                 error ("variable-sized object may not be initialized");
3120                 initialized = 0;
3121               }
3122           }
3123         else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
3124           {
3125             error ("variable %qD has initializer but incomplete type", decl);
3126             initialized = 0;
3127           }
3128         else if (C_DECL_VARIABLE_SIZE (decl))
3129           {
3130             /* Although C99 is unclear about whether incomplete arrays
3131                of VLAs themselves count as VLAs, it does not make
3132                sense to permit them to be initialized given that
3133                ordinary VLAs may not be initialized.  */
3134             error ("variable-sized object may not be initialized");
3135             initialized = 0;
3136           }
3137       }
3138
3139   if (initialized)
3140     {
3141       if (current_scope == file_scope)
3142         TREE_STATIC (decl) = 1;
3143
3144       /* Tell 'pushdecl' this is an initialized decl
3145          even though we don't yet have the initializer expression.
3146          Also tell 'finish_decl' it may store the real initializer.  */
3147       DECL_INITIAL (decl) = error_mark_node;
3148     }
3149
3150   /* If this is a function declaration, write a record describing it to the
3151      prototypes file (if requested).  */
3152
3153   if (TREE_CODE (decl) == FUNCTION_DECL)
3154     gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
3155
3156   /* ANSI specifies that a tentative definition which is not merged with
3157      a non-tentative definition behaves exactly like a definition with an
3158      initializer equal to zero.  (Section 3.7.2)
3159
3160      -fno-common gives strict ANSI behavior, though this tends to break
3161      a large body of code that grew up without this rule.
3162
3163      Thread-local variables are never common, since there's no entrenched
3164      body of code to break, and it allows more efficient variable references
3165      in the presence of dynamic linking.  */
3166
3167   if (TREE_CODE (decl) == VAR_DECL
3168       && !initialized
3169       && TREE_PUBLIC (decl)
3170       && !DECL_THREAD_LOCAL (decl)
3171       && !flag_no_common)
3172     DECL_COMMON (decl) = 1;
3173
3174   /* Set attributes here so if duplicate decl, will have proper attributes.  */
3175   decl_attributes (&decl, attributes, 0);
3176
3177   if (TREE_CODE (decl) == FUNCTION_DECL
3178       && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
3179     {
3180       struct c_declarator *ce = declarator;
3181
3182       if (ce->kind == cdk_pointer)
3183         ce = declarator->declarator;
3184       if (ce->kind == cdk_function)
3185         {
3186           tree args = ce->u.arg_info->parms;
3187           for (; args; args = TREE_CHAIN (args))
3188             {
3189               tree type = TREE_TYPE (args);
3190               if (type && INTEGRAL_TYPE_P (type)
3191                   && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
3192                 DECL_ARG_TYPE (args) = integer_type_node;
3193             }
3194         }
3195     }
3196
3197   if (TREE_CODE (decl) == FUNCTION_DECL
3198       && DECL_DECLARED_INLINE_P (decl)
3199       && DECL_UNINLINABLE (decl)
3200       && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
3201     warning (0, "%Jinline function %qD given attribute noinline", decl, decl);
3202
3203   /* Add this decl to the current scope.
3204      TEM may equal DECL or it may be a previous decl of the same name.  */
3205   tem = pushdecl (decl);
3206
3207   if (initialized && DECL_EXTERNAL (tem))
3208     {
3209       DECL_EXTERNAL (tem) = 0;
3210       TREE_STATIC (tem) = 1;
3211     }
3212
3213   return tem;
3214 }
3215
3216 /* Finish processing of a declaration;
3217    install its initial value.
3218    If the length of an array type is not known before,
3219    it must be determined now, from the initial value, or it is an error.  */
3220
3221 void
3222 finish_decl (tree decl, tree init, tree asmspec_tree)
3223 {
3224   tree type = TREE_TYPE (decl);
3225   int was_incomplete = (DECL_SIZE (decl) == 0);
3226   const char *asmspec = 0;
3227
3228   /* If a name was specified, get the string.  */
3229   if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
3230       && DECL_FILE_SCOPE_P (decl))
3231     asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
3232   if (asmspec_tree)
3233     asmspec = TREE_STRING_POINTER (asmspec_tree);
3234
3235   /* If `start_decl' didn't like having an initialization, ignore it now.  */
3236   if (init != 0 && DECL_INITIAL (decl) == 0)
3237     init = 0;
3238
3239   /* Don't crash if parm is initialized.  */
3240   if (TREE_CODE (decl) == PARM_DECL)
3241     init = 0;
3242
3243   if (init)
3244     store_init_value (decl, init);
3245
3246   if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
3247                             || TREE_CODE (decl) == FUNCTION_DECL
3248                             || TREE_CODE (decl) == FIELD_DECL))
3249     objc_check_decl (decl);
3250
3251   /* Deduce size of array from initialization, if not already known.  */
3252   if (TREE_CODE (type) == ARRAY_TYPE
3253       && TYPE_DOMAIN (type) == 0
3254       && TREE_CODE (decl) != TYPE_DECL)
3255     {
3256       bool do_default
3257         = (TREE_STATIC (decl)
3258            /* Even if pedantic, an external linkage array
3259               may have incomplete type at first.  */
3260            ? pedantic && !TREE_PUBLIC (decl)
3261            : !DECL_EXTERNAL (decl));
3262       int failure
3263         = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
3264                                do_default);
3265
3266       /* Get the completed type made by complete_array_type.  */
3267       type = TREE_TYPE (decl);
3268
3269       switch (failure)
3270         {
3271         case 1:
3272           error ("%Jinitializer fails to determine size of %qD", decl, decl);
3273           break;
3274
3275         case 2:
3276           if (do_default)
3277             error ("%Jarray size missing in %qD", decl, decl);
3278           /* If a `static' var's size isn't known,
3279              make it extern as well as static, so it does not get
3280              allocated.
3281              If it is not `static', then do not mark extern;
3282              finish_incomplete_decl will give it a default size
3283              and it will get allocated.  */
3284           else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
3285             DECL_EXTERNAL (decl) = 1;
3286           break;
3287
3288         case 3:
3289           error ("%Jzero or negative size array %qD", decl, decl);
3290           break;
3291
3292         case 0:
3293           /* For global variables, update the copy of the type that
3294              exists in the binding.  */
3295           if (TREE_PUBLIC (decl))
3296             {
3297               struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
3298               while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
3299                 b_ext = b_ext->shadowed;
3300               if (b_ext)
3301                 {
3302                   if (b_ext->type)
3303                     b_ext->type = composite_type (b_ext->type, type);
3304                   else
3305                     b_ext->type = type;
3306                 }
3307             }
3308           break;
3309
3310         default:
3311           gcc_unreachable ();
3312         }
3313
3314       if (DECL_INITIAL (decl))
3315         TREE_TYPE (DECL_INITIAL (decl)) = type;
3316
3317       layout_decl (decl, 0);
3318     }
3319
3320   if (TREE_CODE (decl) == VAR_DECL)
3321     {
3322       if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
3323           && COMPLETE_TYPE_P (TREE_TYPE (decl)))
3324         layout_decl (decl, 0);
3325
3326       if (DECL_SIZE (decl) == 0
3327           /* Don't give an error if we already gave one earlier.  */
3328           && TREE_TYPE (decl) != error_mark_node
3329           && (TREE_STATIC (decl)
3330               /* A static variable with an incomplete type
3331                  is an error if it is initialized.
3332                  Also if it is not file scope.
3333                  Otherwise, let it through, but if it is not `extern'
3334                  then it may cause an error message later.  */
3335               ? (DECL_INITIAL (decl) != 0
3336                  || !DECL_FILE_SCOPE_P (decl))
3337               /* An automatic variable with an incomplete type
3338                  is an error.  */
3339               : !DECL_EXTERNAL (decl)))
3340          {
3341            error ("%Jstorage size of %qD isn%'t known", decl, decl);
3342            TREE_TYPE (decl) = error_mark_node;
3343          }
3344
3345       if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
3346           && DECL_SIZE (decl) != 0)
3347         {
3348           if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
3349             constant_expression_warning (DECL_SIZE (decl));
3350           else
3351             error ("%Jstorage size of %qD isn%'t constant", decl, decl);
3352         }
3353
3354       if (TREE_USED (type))
3355         TREE_USED (decl) = 1;
3356     }
3357
3358   /* If this is a function and an assembler name is specified, reset DECL_RTL
3359      so we can give it its new name.  Also, update built_in_decls if it
3360      was a normal built-in.  */
3361   if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
3362     {
3363       if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
3364         set_builtin_user_assembler_name (decl, asmspec);
3365       set_user_assembler_name (decl, asmspec);
3366     }
3367
3368   /* If #pragma weak was used, mark the decl weak now.  */
3369   maybe_apply_pragma_weak (decl);
3370
3371   /* If this is a variable definition, determine its ELF visibility.  */
3372   if (TREE_CODE (decl) == VAR_DECL 
3373       && TREE_STATIC (decl) 
3374       && !DECL_EXTERNAL (decl))
3375     c_determine_visibility (decl);
3376
3377   /* Output the assembler code and/or RTL code for variables and functions,
3378      unless the type is an undefined structure or union.
3379      If not, it will get done when the type is completed.  */
3380
3381   if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3382     {
3383       /* This is a no-op in c-lang.c or something real in objc-act.c.  */
3384       if (c_dialect_objc ())
3385         objc_check_decl (decl);
3386
3387       if (asmspec) 
3388         {
3389           /* If this is not a static variable, issue a warning.
3390              It doesn't make any sense to give an ASMSPEC for an
3391              ordinary, non-register local variable.  Historically,
3392              GCC has accepted -- but ignored -- the ASMSPEC in
3393              this case.  */
3394           if (!DECL_FILE_SCOPE_P (decl)
3395               && TREE_CODE (decl) == VAR_DECL
3396               && !C_DECL_REGISTER (decl)
3397               && !TREE_STATIC (decl))
3398             warning (0, "%Jignoring asm-specifier for non-static local "
3399                      "variable %qD", decl, decl);
3400           else if (C_DECL_REGISTER (decl))
3401             change_decl_assembler_name (decl, get_identifier (asmspec));
3402           else
3403             set_user_assembler_name (decl, asmspec);
3404         }
3405       
3406       if (DECL_FILE_SCOPE_P (decl))
3407         {
3408           if (DECL_INITIAL (decl) == NULL_TREE
3409               || DECL_INITIAL (decl) == error_mark_node)
3410             /* Don't output anything
3411                when a tentative file-scope definition is seen.
3412                But at end of compilation, do output code for them.  */
3413             DECL_DEFER_OUTPUT (decl) = 1;
3414           rest_of_decl_compilation (decl, true, 0);
3415         }
3416       else
3417         {
3418           /* In conjunction with an ASMSPEC, the `register'
3419              keyword indicates that we should place the variable
3420              in a particular register.  */
3421           if (asmspec && C_DECL_REGISTER (decl))
3422             {
3423               DECL_HARD_REGISTER (decl) = 1;
3424               /* This cannot be done for a structure with volatile
3425                  fields, on which DECL_REGISTER will have been
3426                  reset.  */
3427               if (!DECL_REGISTER (decl))
3428                 error ("cannot put object with volatile field into register");
3429             }
3430
3431           if (TREE_CODE (decl) != FUNCTION_DECL)
3432             {
3433               /* If we're building a variable sized type, and we might be
3434                  reachable other than via the top of the current binding
3435                  level, then create a new BIND_EXPR so that we deallocate
3436                  the object at the right time.  */
3437               /* Note that DECL_SIZE can be null due to errors.  */
3438               if (DECL_SIZE (decl)
3439                   && !TREE_CONSTANT (DECL_SIZE (decl))
3440                   && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
3441                 {
3442                   tree bind;
3443                   bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
3444                   TREE_SIDE_EFFECTS (bind) = 1;
3445                   add_stmt (bind);
3446                   BIND_EXPR_BODY (bind) = push_stmt_list ();
3447                 }
3448               add_stmt (build_stmt (DECL_EXPR, decl));
3449             }
3450         }
3451   
3452
3453       if (!DECL_FILE_SCOPE_P (decl))
3454         {
3455           /* Recompute the RTL of a local array now
3456              if it used to be an incomplete type.  */
3457           if (was_incomplete
3458               && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
3459             {
3460               /* If we used it already as memory, it must stay in memory.  */
3461               TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3462               /* If it's still incomplete now, no init will save it.  */
3463               if (DECL_SIZE (decl) == 0)
3464                 DECL_INITIAL (decl) = 0;
3465             }
3466         }
3467     }
3468
3469   /* If this was marked 'used', be sure it will be output.  */
3470   if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
3471     mark_decl_referenced (decl);
3472
3473   if (TREE_CODE (decl) == TYPE_DECL)
3474     {
3475       if (!DECL_FILE_SCOPE_P (decl)
3476           && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3477         add_stmt (build_stmt (DECL_EXPR, decl));
3478
3479       rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
3480     }
3481
3482   /* At the end of a declaration, throw away any variable type sizes
3483      of types defined inside that declaration.  There is no use
3484      computing them in the following function definition.  */
3485   if (current_scope == file_scope)
3486     get_pending_sizes ();
3487
3488   /* Install a cleanup (aka destructor) if one was given.  */
3489   if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
3490     {
3491       tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
3492       if (attr)
3493         {
3494           tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
3495           tree cleanup_decl = lookup_name (cleanup_id);
3496           tree cleanup;
3497
3498           /* Build "cleanup(&decl)" for the destructor.  */
3499           cleanup = build_unary_op (ADDR_EXPR, decl, 0);
3500           cleanup = build_tree_list (NULL_TREE, cleanup);
3501           cleanup = build_function_call (cleanup_decl, cleanup);
3502
3503           /* Don't warn about decl unused; the cleanup uses it.  */
3504           TREE_USED (decl) = 1;
3505           TREE_USED (cleanup_decl) = 1;
3506
3507           /* Initialize EH, if we've been told to do so.  */
3508           if (flag_exceptions && !c_eh_initialized_p)
3509             {
3510               c_eh_initialized_p = true;
3511               eh_personality_libfunc
3512                 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3513                                     ? "__gcc_personality_sj0"
3514                                     : "__gcc_personality_v0");
3515               using_eh_for_cleanups ();
3516             }
3517
3518           push_cleanup (decl, cleanup, false);
3519         }
3520     }
3521 }
3522
3523 /* Given a parsed parameter declaration, decode it into a PARM_DECL.  */
3524
3525 tree
3526 grokparm (const struct c_parm *parm)
3527 {
3528   tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
3529                               NULL);
3530
3531   decl_attributes (&decl, parm->attrs, 0);
3532
3533   return decl;
3534 }
3535
3536 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3537    and push that on the current scope.  */
3538
3539 void
3540 push_parm_decl (const struct c_parm *parm)
3541 {
3542   tree decl;
3543
3544   decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL);
3545   decl_attributes (&decl, parm->attrs, 0);
3546
3547   decl = pushdecl (decl);
3548
3549   finish_decl (decl, NULL_TREE, NULL_TREE);
3550 }
3551
3552 /* Mark all the parameter declarations to date as forward decls.
3553    Also diagnose use of this extension.  */
3554
3555 void
3556 mark_forward_parm_decls (void)
3557 {
3558   struct c_binding *b;
3559
3560   if (pedantic && !current_scope->warned_forward_parm_decls)
3561     {
3562       pedwarn ("ISO C forbids forward parameter declarations");
3563       current_scope->warned_forward_parm_decls = true;
3564     }
3565
3566   for (b = current_scope->bindings; b; b = b->prev)
3567     if (TREE_CODE (b->decl) == PARM_DECL)
3568       TREE_ASM_WRITTEN (b->decl) = 1;
3569 }
3570 \f
3571 static GTY(()) int compound_literal_number;
3572
3573 /* Build a COMPOUND_LITERAL_EXPR.  TYPE is the type given in the compound
3574    literal, which may be an incomplete array type completed by the
3575    initializer; INIT is a CONSTRUCTOR that initializes the compound
3576    literal.  */
3577
3578 tree
3579 build_compound_literal (tree type, tree init)
3580 {
3581   /* We do not use start_decl here because we have a type, not a declarator;
3582      and do not use finish_decl because the decl should be stored inside
3583      the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR.  */
3584   tree decl;
3585   tree complit;
3586   tree stmt;
3587
3588   if (type == error_mark_node)
3589     return error_mark_node;
3590
3591   decl = build_decl (VAR_DECL, NULL_TREE, type);
3592   DECL_EXTERNAL (decl) = 0;
3593   TREE_PUBLIC (decl) = 0;
3594   TREE_STATIC (decl) = (current_scope == file_scope);
3595   DECL_CONTEXT (decl) = current_function_decl;
3596   TREE_USED (decl) = 1;
3597   TREE_TYPE (decl) = type;
3598   TREE_READONLY (decl) = TYPE_READONLY (type);
3599   store_init_value (decl, init);
3600
3601   if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3602     {
3603       int failure = complete_array_type (&TREE_TYPE (decl),
3604                                          DECL_INITIAL (decl), true);
3605       gcc_assert (!failure);
3606
3607       type = TREE_TYPE (decl);
3608       TREE_TYPE (DECL_INITIAL (decl)) = type;
3609     }
3610
3611   if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3612     return error_mark_node;
3613
3614   stmt = build_stmt (DECL_EXPR, decl);
3615   complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
3616   TREE_SIDE_EFFECTS (complit) = 1;
3617
3618   layout_decl (decl, 0);
3619
3620   if (TREE_STATIC (decl))
3621     {
3622       /* This decl needs a name for the assembler output.  We also need
3623          a unique suffix to be added to the name.  */
3624       char *name;
3625
3626       ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
3627                                compound_literal_number);
3628       compound_literal_number++;
3629       DECL_NAME (decl) = get_identifier (name);
3630       DECL_DEFER_OUTPUT (decl) = 1;
3631       DECL_COMDAT (decl) = 1;
3632       DECL_ARTIFICIAL (decl) = 1;
3633       DECL_IGNORED_P (decl) = 1;
3634       pushdecl (decl);
3635       rest_of_decl_compilation (decl, 1, 0);
3636     }
3637
3638   return complit;
3639 }
3640 \f
3641 /* Determine whether TYPE is a structure with a flexible array member,
3642    or a union containing such a structure (possibly recursively).  */
3643
3644 static bool
3645 flexible_array_type_p (tree type)
3646 {
3647   tree x;
3648   switch (TREE_CODE (type))
3649     {
3650     case RECORD_TYPE:
3651       x = TYPE_FIELDS (type);
3652       if (x == NULL_TREE)
3653         return false;
3654       while (TREE_CHAIN (x) != NULL_TREE)
3655         x = TREE_CHAIN (x);
3656       if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3657           && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3658           && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3659           && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3660         return true;
3661       return false;
3662     case UNION_TYPE:
3663       for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3664         {
3665           if (flexible_array_type_p (TREE_TYPE (x)))
3666             return true;
3667         }
3668       return false;
3669     default:
3670     return false;
3671   }
3672 }
3673 \f
3674 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3675    replacing with appropriate values if they are invalid.  */
3676 static void
3677 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3678 {
3679   tree type_mv;
3680   unsigned int max_width;
3681   unsigned HOST_WIDE_INT w;
3682   const char *name = orig_name ? orig_name: _("<anonymous>");
3683
3684   /* Detect and ignore out of range field width and process valid
3685      field widths.  */
3686   if (!INTEGRAL_TYPE_P (TREE_TYPE (*width))
3687       || TREE_CODE (*width) != INTEGER_CST)
3688     {
3689       error ("bit-field %qs width not an integer constant", name);
3690       *width = integer_one_node;
3691     }
3692   else
3693     {
3694       constant_expression_warning (*width);
3695       if (tree_int_cst_sgn (*width) < 0)
3696         {
3697           error ("negative width in bit-field %qs", name);
3698           *width = integer_one_node;
3699         }
3700       else if (integer_zerop (*width) && orig_name)
3701         {
3702           error ("zero width for bit-field %qs", name);
3703           *width = integer_one_node;
3704         }
3705     }
3706
3707   /* Detect invalid bit-field type.  */
3708   if (TREE_CODE (*type) != INTEGER_TYPE
3709       && TREE_CODE (*type) != BOOLEAN_TYPE
3710       && TREE_CODE (*type) != ENUMERAL_TYPE)
3711     {
3712       error ("bit-field %qs has invalid type", name);
3713       *type = unsigned_type_node;
3714     }
3715
3716   type_mv = TYPE_MAIN_VARIANT (*type);
3717   if (pedantic
3718       && type_mv != integer_type_node
3719       && type_mv != unsigned_type_node
3720       && type_mv != boolean_type_node)
3721     pedwarn ("type of bit-field %qs is a GCC extension", name);
3722
3723   if (type_mv == boolean_type_node)
3724     max_width = CHAR_TYPE_SIZE;
3725   else
3726     max_width = TYPE_PRECISION (*type);
3727
3728   if (0 < compare_tree_int (*width, max_width))
3729     {
3730       error ("width of %qs exceeds its type", name);
3731       w = max_width;
3732       *width = build_int_cst (NULL_TREE, w);
3733     }
3734   else
3735     w = tree_low_cst (*width, 1);
3736
3737   if (TREE_CODE (*type) == ENUMERAL_TYPE)
3738     {
3739       struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
3740       if (!lt
3741           || w < min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
3742           || w < min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
3743         warning (0, "%qs is narrower than values of its type", name);
3744     }
3745 }
3746 \f
3747 /* Given declspecs and a declarator,
3748    determine the name and type of the object declared
3749    and construct a ..._DECL node for it.
3750    (In one case we can return a ..._TYPE node instead.
3751     For invalid input we sometimes return 0.)
3752
3753    DECLSPECS is a c_declspecs structure for the declaration specifiers.
3754
3755    DECL_CONTEXT says which syntactic context this declaration is in:
3756      NORMAL for most contexts.  Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3757      FUNCDEF for a function definition.  Like NORMAL but a few different
3758       error messages in each case.  Return value may be zero meaning
3759       this definition is too screwy to try to parse.
3760      PARM for a parameter declaration (either within a function prototype
3761       or before a function body).  Make a PARM_DECL, or return void_type_node.
3762      TYPENAME if for a typename (in a cast or sizeof).
3763       Don't make a DECL node; just return the ..._TYPE node.
3764      FIELD for a struct or union field; make a FIELD_DECL.
3765    INITIALIZED is true if the decl has an initializer.
3766    WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3767    representing the width of the bit-field.
3768
3769    In the TYPENAME case, DECLARATOR is really an absolute declarator.
3770    It may also be so in the PARM case, for a prototype where the
3771    argument type is specified but not the name.
3772
3773    This function is where the complicated C meanings of `static'
3774    and `extern' are interpreted.  */
3775
3776 static tree
3777 grokdeclarator (const struct c_declarator *declarator,
3778                 struct c_declspecs *declspecs,
3779                 enum decl_context decl_context, bool initialized, tree *width)
3780 {
3781   tree type = declspecs->type;
3782   bool threadp = declspecs->thread_p;
3783   enum c_storage_class storage_class = declspecs->storage_class;
3784   int constp;
3785   int restrictp;
3786   int volatilep;
3787   int type_quals = TYPE_UNQUALIFIED;
3788   const char *name, *orig_name;
3789   tree typedef_type = 0;
3790   int funcdef_flag = 0;
3791   bool funcdef_syntax = false;
3792   int size_varies = 0;
3793   tree decl_attr = declspecs->decl_attr;
3794   int array_ptr_quals = TYPE_UNQUALIFIED;
3795   tree array_ptr_attrs = NULL_TREE;
3796   int array_parm_static = 0;
3797   tree returned_attrs = NULL_TREE;
3798   bool bitfield = width != NULL;
3799   tree element_type;
3800   struct c_arg_info *arg_info = 0;
3801
3802   if (decl_context == FUNCDEF)
3803     funcdef_flag = 1, decl_context = NORMAL;
3804
3805   /* Look inside a declarator for the name being declared
3806      and get it as a string, for an error message.  */
3807   {
3808     const struct c_declarator *decl = declarator;
3809     name = 0;
3810
3811     while (decl)
3812       switch (decl->kind)
3813         {
3814         case cdk_function:
3815         case cdk_array:
3816         case cdk_pointer:
3817           funcdef_syntax = (decl->kind == cdk_function);
3818           decl = decl->declarator;
3819           break;
3820
3821         case cdk_attrs:
3822           decl = decl->declarator;
3823           break;
3824
3825         case cdk_id:
3826           if (decl->u.id)
3827             name = IDENTIFIER_POINTER (decl->u.id);
3828           decl = 0;
3829           break;
3830
3831         default:
3832           gcc_unreachable ();
3833         }
3834     orig_name = name;
3835     if (name == 0)
3836       name = "type name";
3837   }
3838
3839   /* A function definition's declarator must have the form of
3840      a function declarator.  */
3841
3842   if (funcdef_flag && !funcdef_syntax)
3843     return 0;
3844
3845   /* If this looks like a function definition, make it one,
3846      even if it occurs where parms are expected.
3847      Then store_parm_decls will reject it and not use it as a parm.  */
3848   if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
3849     decl_context = PARM;
3850
3851   if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
3852     warn_deprecated_use (declspecs->type);
3853
3854   typedef_type = type;
3855   size_varies = C_TYPE_VARIABLE_SIZE (type);
3856
3857   /* Diagnose defaulting to "int".  */
3858
3859   if (declspecs->default_int_p && !in_system_header)
3860     {
3861       /* Issue a warning if this is an ISO C 99 program or if
3862          -Wreturn-type and this is a function, or if -Wimplicit;
3863          prefer the former warning since it is more explicit.  */
3864       if ((warn_implicit_int || warn_return_type || flag_isoc99)
3865           && funcdef_flag)
3866         warn_about_return_type = 1;
3867       else if (warn_implicit_int || flag_isoc99)
3868         pedwarn_c99 ("type defaults to %<int%> in declaration of %qs", name);
3869     }
3870
3871   /* Adjust the type if a bit-field is being declared,
3872      -funsigned-bitfields applied and the type is not explicitly
3873      "signed".  */
3874   if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
3875       && TREE_CODE (type) == INTEGER_TYPE)
3876     type = c_common_unsigned_type (type);
3877
3878   /* Figure out the type qualifiers for the declaration.  There are
3879      two ways a declaration can become qualified.  One is something
3880      like `const int i' where the `const' is explicit.  Another is
3881      something like `typedef const int CI; CI i' where the type of the
3882      declaration contains the `const'.  A third possibility is that
3883      there is a type qualifier on the element type of a typedefed
3884      array type, in which case we should extract that qualifier so
3885      that c_apply_type_quals_to_decls receives the full list of
3886      qualifiers to work with (C90 is not entirely clear about whether
3887      duplicate qualifiers should be diagnosed in this case, but it
3888      seems most appropriate to do so).  */
3889   element_type = strip_array_types (type);
3890   constp = declspecs->const_p + TYPE_READONLY (element_type);
3891   restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
3892   volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
3893   if (pedantic && !flag_isoc99)
3894     {
3895       if (constp > 1)
3896         pedwarn ("duplicate %<const%>");
3897       if (restrictp > 1)
3898         pedwarn ("duplicate %<restrict%>");
3899       if (volatilep > 1)
3900         pedwarn ("duplicate %<volatile%>");
3901     }
3902   if (!flag_gen_aux_info && (TYPE_QUALS (element_type)))
3903     type = TYPE_MAIN_VARIANT (type);
3904   type_quals = ((constp ? TYPE_QUAL_CONST : 0)
3905                 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
3906                 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
3907
3908   /* Warn about storage classes that are invalid for certain
3909      kinds of declarations (parameters, typenames, etc.).  */
3910
3911   if (funcdef_flag
3912       && (threadp
3913           || storage_class == csc_auto
3914           || storage_class == csc_register
3915           || storage_class == csc_typedef))
3916     {
3917       if (storage_class == csc_auto
3918           && (pedantic || current_scope == file_scope))
3919         pedwarn ("function definition declared %<auto%>");
3920       if (storage_class == csc_register)
3921         error ("function definition declared %<register%>");
3922       if (storage_class == csc_typedef)
3923         error ("function definition declared %<typedef%>");
3924       if (threadp)
3925         error ("function definition declared %<__thread%>");
3926       threadp = false;
3927       if (storage_class == csc_auto
3928           || storage_class == csc_register
3929           || storage_class == csc_typedef)
3930         storage_class = csc_none;
3931     }
3932   else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
3933     {
3934       if (decl_context == PARM && storage_class == csc_register)
3935         ;
3936       else
3937         {
3938           switch (decl_context)
3939             {
3940             case FIELD:
3941               error ("storage class specified for structure field %qs",
3942                      name);
3943               break;
3944             case PARM:
3945               error ("storage class specified for parameter %qs", name);
3946               break;
3947             default:
3948               error ("storage class specified for typename");
3949               break;
3950             }
3951           storage_class = csc_none;
3952           threadp = false;
3953         }
3954     }
3955   else if (storage_class == csc_extern
3956            && initialized
3957            && !funcdef_flag)
3958     {
3959       /* 'extern' with initialization is invalid if not at file scope.  */
3960       if (current_scope == file_scope)
3961         warning (0, "%qs initialized and declared %<extern%>", name);
3962       else
3963         error ("%qs has both %<extern%> and initializer", name);
3964     }
3965   else if (current_scope == file_scope)
3966     {
3967       if (storage_class == csc_auto)
3968         error ("file-scope declaration of %qs specifies %<auto%>", name);
3969       if (pedantic && storage_class == csc_register)
3970         pedwarn ("file-scope declaration of %qs specifies %<register%>", name);
3971     }
3972   else
3973     {
3974       if (storage_class == csc_extern && funcdef_flag)
3975         error ("nested function %qs declared %<extern%>", name);
3976       else if (threadp && storage_class == csc_none)
3977         {
3978           error ("function-scope %qs implicitly auto and declared "
3979                  "%<__thread%>",
3980                  name);
3981           threadp = false;
3982         }
3983     }
3984
3985   /* Now figure out the structure of the declarator proper.
3986      Descend through it, creating more complex types, until we reach
3987      the declared identifier (or NULL_TREE, in an absolute declarator).
3988      At each stage we maintain an unqualified version of the type
3989      together with any qualifiers that should be applied to it with
3990      c_build_qualified_type; this way, array types including
3991      multidimensional array types are first built up in unqualified
3992      form and then the qualified form is created with
3993      TYPE_MAIN_VARIANT pointing to the unqualified form.  */
3994
3995   while (declarator && declarator->kind != cdk_id)
3996     {
3997       if (type == error_mark_node)
3998         {
3999           declarator = declarator->declarator;
4000           continue;
4001         }
4002
4003       /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
4004          a cdk_pointer (for *...),
4005          a cdk_function (for ...(...)),
4006          a cdk_attrs (for nested attributes),
4007          or a cdk_id (for the name being declared
4008          or the place in an absolute declarator
4009          where the name was omitted).
4010          For the last case, we have just exited the loop.
4011
4012          At this point, TYPE is the type of elements of an array,
4013          or for a function to return, or for a pointer to point to.
4014          After this sequence of ifs, TYPE is the type of the
4015          array or function or pointer, and DECLARATOR has had its
4016          outermost layer removed.  */
4017
4018       if (array_ptr_quals != TYPE_UNQUALIFIED
4019           || array_ptr_attrs != NULL_TREE
4020           || array_parm_static)
4021         {
4022           /* Only the innermost declarator (making a parameter be of
4023              array type which is converted to pointer type)
4024              may have static or type qualifiers.  */
4025           error ("static or type qualifiers in non-parameter array declarator");
4026           array_ptr_quals = TYPE_UNQUALIFIED;
4027           array_ptr_attrs = NULL_TREE;
4028           array_parm_static = 0;
4029         }
4030
4031       switch (declarator->kind)
4032         {
4033         case cdk_attrs:
4034           {
4035             /* A declarator with embedded attributes.  */
4036             tree attrs = declarator->u.attrs;
4037             const struct c_declarator *inner_decl;
4038             int attr_flags = 0;
4039             declarator = declarator->declarator;
4040             inner_decl = declarator;
4041             while (inner_decl->kind == cdk_attrs)
4042               inner_decl = inner_decl->declarator;
4043             if (inner_decl->kind == cdk_id)
4044               attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
4045             else if (inner_decl->kind == cdk_function)
4046               attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
4047             else if (inner_decl->kind == cdk_array)
4048               attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
4049             returned_attrs = decl_attributes (&type,
4050                                               chainon (returned_attrs, attrs),
4051                                               attr_flags);
4052             break;
4053           }
4054         case cdk_array:
4055           {
4056             tree itype = NULL_TREE;
4057             tree size = declarator->u.array.dimen;
4058             /* The index is a signed object `sizetype' bits wide.  */
4059             tree index_type = c_common_signed_type (sizetype);
4060
4061             array_ptr_quals = declarator->u.array.quals;
4062             array_ptr_attrs = declarator->u.array.attrs;
4063             array_parm_static = declarator->u.array.static_p;
4064             
4065             declarator = declarator->declarator;
4066
4067             /* Check for some types that there cannot be arrays of.  */
4068             
4069             if (VOID_TYPE_P (type))
4070               {
4071                 error ("declaration of %qs as array of voids", name);
4072                 type = error_mark_node;
4073               }
4074             
4075             if (TREE_CODE (type) == FUNCTION_TYPE)
4076               {
4077                 error ("declaration of %qs as array of functions", name);
4078                 type = error_mark_node;
4079               }
4080             
4081             if (pedantic && !in_system_header && flexible_array_type_p (type))
4082               pedwarn ("invalid use of structure with flexible array member");
4083             
4084             if (size == error_mark_node)
4085               type = error_mark_node;
4086             
4087             if (type == error_mark_node)
4088               continue;
4089
4090             /* If size was specified, set ITYPE to a range-type for
4091                that size.  Otherwise, ITYPE remains null.  finish_decl
4092                may figure it out from an initial value.  */
4093
4094             if (size)
4095               {
4096                 /* Strip NON_LVALUE_EXPRs since we aren't using as an
4097                    lvalue.  */
4098                 STRIP_TYPE_NOPS (size);
4099                 
4100                 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
4101                   {
4102                     error ("size of array %qs has non-integer type", name);
4103                     size = integer_one_node;
4104                   }
4105                 
4106                 if (pedantic && integer_zerop (size))
4107                   pedwarn ("ISO C forbids zero-size array %qs", name);
4108                 
4109                 if (TREE_CODE (size) == INTEGER_CST)
4110                   {
4111                     constant_expression_warning (size);
4112                     if (tree_int_cst_sgn (size) < 0)
4113                       {
4114                         error ("size of array %qs is negative", name);
4115                         size = integer_one_node;
4116                       }
4117                   }
4118                 else
4119                   {
4120                     /* Make sure the array size remains visibly
4121                        nonconstant even if it is (eg) a const variable
4122                        with known value.  */
4123                     size_varies = 1;
4124                     
4125                     if (!flag_isoc99 && pedantic)
4126                       {
4127                         if (TREE_CONSTANT (size))
4128                           pedwarn ("ISO C90 forbids array %qs whose size "
4129                                    "can%'t be evaluated",
4130                                    name);
4131                         else
4132                           pedwarn ("ISO C90 forbids variable-size array %qs",
4133                                    name);
4134                       }
4135                   }
4136
4137                 if (integer_zerop (size))
4138                   {
4139                     /*  A zero-length array cannot be represented with
4140                         an unsigned index type, which is what we'll
4141                         get with build_index_type.  Create an
4142                         open-ended range instead.  */
4143                     itype = build_range_type (sizetype, size, NULL_TREE);
4144                   }
4145                 else
4146                   {
4147                     /* Arrange for the SAVE_EXPR on the inside of the
4148                        MINUS_EXPR, which allows the -1 to get folded
4149                        with the +1 that happens when building TYPE_SIZE.  */
4150                     if (size_varies)
4151                       size = variable_size (size);
4152
4153                     /* Compute the maximum valid index, that is, size
4154                        - 1.  Do the calculation in index_type, so that
4155                        if it is a variable the computations will be
4156                        done in the proper mode.  */
4157                     itype = fold (build2 (MINUS_EXPR, index_type,
4158                                           convert (index_type, size),
4159                                           convert (index_type,
4160                                                    size_one_node)));
4161
4162                     /* If that overflowed, the array is too big.  ??? 
4163                        While a size of INT_MAX+1 technically shouldn't
4164                        cause an overflow (because we subtract 1), the
4165                        overflow is recorded during the conversion to
4166                        index_type, before the subtraction.  Handling
4167                        this case seems like an unnecessary
4168                        complication.  */
4169                     if (TREE_OVERFLOW (itype))
4170                       {
4171                         error ("size of array %qs is too large", name);
4172                         type = error_mark_node;
4173                         continue;
4174                       }
4175                     
4176                     itype = build_index_type (itype);
4177                   }
4178               }
4179             else if (decl_context == FIELD)
4180               {
4181                 if (pedantic && !flag_isoc99 && !in_system_header)
4182                   pedwarn ("ISO C90 does not support flexible array members");
4183
4184                 /* ISO C99 Flexible array members are effectively
4185                    identical to GCC's zero-length array extension.  */
4186                 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4187               }
4188
4189              /* Complain about arrays of incomplete types.  */
4190             if (!COMPLETE_TYPE_P (type))
4191               {
4192                 error ("array type has incomplete element type");
4193                 type = error_mark_node;
4194               }
4195             else
4196               type = build_array_type (type, itype);
4197
4198             if (size_varies)
4199               C_TYPE_VARIABLE_SIZE (type) = 1;
4200
4201             /* The GCC extension for zero-length arrays differs from
4202                ISO flexible array members in that sizeof yields
4203                zero.  */
4204             if (size && integer_zerop (size))
4205               {
4206                 TYPE_SIZE (type) = bitsize_zero_node;
4207                 TYPE_SIZE_UNIT (type) = size_zero_node;
4208               }
4209
4210             if (decl_context != PARM
4211                 && (array_ptr_quals != TYPE_UNQUALIFIED
4212                     || array_ptr_attrs != NULL_TREE
4213                     || array_parm_static))
4214               {
4215                 error ("static or type qualifiers in non-parameter array declarator");
4216                 array_ptr_quals = TYPE_UNQUALIFIED;
4217                 array_ptr_attrs = NULL_TREE;
4218                 array_parm_static = 0;
4219               }
4220             break;
4221           }
4222         case cdk_function:
4223           {
4224             /* Say it's a definition only for the declarator closest
4225                to the identifier, apart possibly from some
4226                attributes.  */
4227             bool really_funcdef = false;
4228             tree arg_types;
4229             if (funcdef_flag)
4230               {
4231                 const struct c_declarator *t = declarator->declarator;
4232                 while (t->kind == cdk_attrs)
4233                   t = t->declarator;
4234                 really_funcdef = (t->kind == cdk_id);
4235               }
4236
4237             /* Declaring a function type.  Make sure we have a valid
4238                type for the function to return.  */
4239             if (type == error_mark_node)
4240               continue;
4241             
4242             size_varies = 0;
4243
4244             /* Warn about some types functions can't return.  */
4245             if (TREE_CODE (type) == FUNCTION_TYPE)
4246               {
4247                 error ("%qs declared as function returning a function", name);
4248                 type = integer_type_node;
4249               }
4250             if (TREE_CODE (type) == ARRAY_TYPE)
4251               {
4252                 error ("%qs declared as function returning an array", name);
4253                 type = integer_type_node;
4254               }
4255
4256             /* Construct the function type and go to the next
4257                inner layer of declarator.  */
4258             arg_info = declarator->u.arg_info;
4259             arg_types = grokparms (arg_info, really_funcdef);
4260
4261             /* Type qualifiers before the return type of the function
4262                qualify the return type, not the function type.  */
4263             if (type_quals)
4264               {
4265                 /* Type qualifiers on a function return type are
4266                    normally permitted by the standard but have no
4267                    effect, so give a warning at -Wreturn-type.
4268                    Qualifiers on a void return type are banned on
4269                    function definitions in ISO C; GCC used to used
4270                    them for noreturn functions.  */
4271                 if (VOID_TYPE_P (type) && really_funcdef)
4272                   pedwarn ("function definition has qualified void return type");
4273                 else
4274                   warning (OPT_Wreturn_type,
4275                            "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",
6083              fndecl);
6084
6085   /* Match each formal parameter name with its declaration.  Save each
6086      decl in the appropriate TREE_PURPOSE slot of the parmids chain.  */
6087   for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6088     {
6089       if (TREE_VALUE (parm) == 0)
6090         {
6091           error ("%Jparameter name missing from parameter list", fndecl);
6092           TREE_PURPOSE (parm) = 0;
6093           continue;
6094         }
6095
6096       b = I_SYMBOL_BINDING (TREE_VALUE (parm));
6097       if (b && B_IN_CURRENT_SCOPE (b))
6098         {
6099           decl = b->decl;
6100           /* If we got something other than a PARM_DECL it is an error.  */
6101           if (TREE_CODE (decl) != PARM_DECL)
6102             error ("%J%qD declared as a non-parameter", decl, decl);
6103           /* If the declaration is already marked, we have a duplicate
6104              name.  Complain and ignore the duplicate.  */
6105           else if (DECL_WEAK (decl))
6106             {
6107               error ("%Jmultiple parameters named %qD", decl, decl);
6108               TREE_PURPOSE (parm) = 0;
6109               continue;
6110             }
6111           /* If the declaration says "void", complain and turn it into
6112              an int.  */
6113           else if (VOID_TYPE_P (TREE_TYPE (decl)))
6114             {
6115               error ("%Jparameter %qD declared with void type", decl, decl);
6116               TREE_TYPE (decl) = integer_type_node;
6117               DECL_ARG_TYPE (decl) = integer_type_node;
6118               layout_decl (decl, 0);
6119             }
6120         }
6121       /* If no declaration found, default to int.  */
6122       else
6123         {
6124           decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
6125           DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
6126           DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
6127           pushdecl (decl);
6128
6129           if (flag_isoc99)
6130             pedwarn ("%Jtype of %qD defaults to %<int%>", decl, decl);
6131           else if (extra_warnings)
6132             warning (0, "%Jtype of %qD defaults to %<int%>", decl, decl);
6133         }
6134
6135       TREE_PURPOSE (parm) = decl;
6136       DECL_WEAK (decl) = 1;
6137     }
6138
6139   /* Now examine the parms chain for incomplete declarations
6140      and declarations with no corresponding names.  */
6141
6142   for (b = current_scope->bindings; b; b = b->prev)
6143     {
6144       parm = b->decl;
6145       if (TREE_CODE (parm) != PARM_DECL)
6146         continue;
6147
6148       if (TREE_TYPE (parm) != error_mark_node
6149           && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
6150         {
6151           error ("%Jparameter %qD has incomplete type", parm, parm);
6152           TREE_TYPE (parm) = error_mark_node;
6153         }
6154
6155       if (!DECL_WEAK (parm))
6156         {
6157           error ("%Jdeclaration for parameter %qD but no such parameter",
6158                  parm, parm);
6159
6160           /* Pretend the parameter was not missing.
6161              This gets us to a standard state and minimizes
6162              further error messages.  */
6163           parmids = chainon (parmids, tree_cons (parm, 0, 0));
6164         }
6165     }
6166
6167   /* Chain the declarations together in the order of the list of
6168      names.  Store that chain in the function decl, replacing the
6169      list of names.  Update the current scope to match.  */
6170   DECL_ARGUMENTS (fndecl) = 0;
6171
6172   for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6173     if (TREE_PURPOSE (parm))
6174       break;
6175   if (parm && TREE_PURPOSE (parm))
6176     {
6177       last = TREE_PURPOSE (parm);
6178       DECL_ARGUMENTS (fndecl) = last;
6179       DECL_WEAK (last) = 0;
6180
6181       for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
6182         if (TREE_PURPOSE (parm))
6183           {
6184             TREE_CHAIN (last) = TREE_PURPOSE (parm);
6185             last = TREE_PURPOSE (parm);
6186             DECL_WEAK (last) = 0;
6187           }
6188       TREE_CHAIN (last) = 0;
6189     }
6190
6191   /* If there was a previous prototype,
6192      set the DECL_ARG_TYPE of each argument according to
6193      the type previously specified, and report any mismatches.  */
6194
6195   if (current_function_prototype_arg_types)
6196     {
6197       tree type;
6198       for (parm = DECL_ARGUMENTS (fndecl),
6199              type = current_function_prototype_arg_types;
6200            parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
6201                              != void_type_node));
6202            parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6203         {
6204           if (parm == 0 || type == 0
6205               || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6206             {
6207               if (current_function_prototype_built_in)
6208                 warning (0, "number of arguments doesn%'t match "
6209                          "built-in prototype");
6210               else
6211                 {
6212                   error ("number of arguments doesn%'t match prototype");
6213                   error ("%Hprototype declaration",
6214                          &current_function_prototype_locus);
6215                 }
6216               break;
6217             }
6218           /* Type for passing arg must be consistent with that
6219              declared for the arg.  ISO C says we take the unqualified
6220              type for parameters declared with qualified type.  */
6221           if (!comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6222                           TYPE_MAIN_VARIANT (TREE_VALUE (type))))
6223             {
6224               if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6225                   == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6226                 {
6227                   /* Adjust argument to match prototype.  E.g. a previous
6228                      `int foo(float);' prototype causes
6229                      `int foo(x) float x; {...}' to be treated like
6230                      `int foo(float x) {...}'.  This is particularly
6231                      useful for argument types like uid_t.  */
6232                   DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6233
6234                   if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
6235                       && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6236                       && TYPE_PRECISION (TREE_TYPE (parm))
6237                       < TYPE_PRECISION (integer_type_node))
6238                     DECL_ARG_TYPE (parm) = integer_type_node;
6239
6240                   if (pedantic)
6241                     {
6242                       /* ??? Is it possible to get here with a
6243                          built-in prototype or will it always have
6244                          been diagnosed as conflicting with an
6245                          old-style definition and discarded?  */
6246                       if (current_function_prototype_built_in)
6247                         warning (0, "promoted argument %qD "
6248                                  "doesn%'t match built-in prototype", parm);
6249                       else
6250                         {
6251                           pedwarn ("promoted argument %qD "
6252                                    "doesn%'t match prototype", parm);
6253                           pedwarn ("%Hprototype declaration",
6254                                    &current_function_prototype_locus);
6255                         }
6256                     }
6257                 }
6258               else
6259                 {
6260                   if (current_function_prototype_built_in)
6261                     warning (0, "argument %qD doesn%'t match "
6262                              "built-in prototype", parm);
6263                   else
6264                     {
6265                       error ("argument %qD doesn%'t match prototype", parm);
6266                       error ("%Hprototype declaration",
6267                              &current_function_prototype_locus);
6268                     }
6269                 }
6270             }
6271         }
6272       TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6273     }
6274
6275   /* Otherwise, create a prototype that would match.  */
6276
6277   else
6278     {
6279       tree actual = 0, last = 0, type;
6280
6281       for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6282         {
6283           type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6284           if (last)
6285             TREE_CHAIN (last) = type;
6286           else
6287             actual = type;
6288           last = type;
6289         }
6290       type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6291       if (last)
6292         TREE_CHAIN (last) = type;
6293       else
6294         actual = type;
6295
6296       /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6297          of the type of this function, but we need to avoid having this
6298          affect the types of other similarly-typed functions, so we must
6299          first force the generation of an identical (but separate) type
6300          node for the relevant function type.  The new node we create
6301          will be a variant of the main variant of the original function
6302          type.  */
6303
6304       TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
6305
6306       TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6307     }
6308 }
6309
6310 /* Store parameter declarations passed in ARG_INFO into the current
6311    function declaration.  */
6312
6313 void
6314 store_parm_decls_from (struct c_arg_info *arg_info)
6315 {
6316   current_function_arg_info = arg_info;
6317   store_parm_decls ();
6318 }
6319
6320 /* Store the parameter declarations into the current function declaration.
6321    This is called after parsing the parameter declarations, before
6322    digesting the body of the function.
6323
6324    For an old-style definition, construct a prototype out of the old-style
6325    parameter declarations and inject it into the function's type.  */
6326
6327 void
6328 store_parm_decls (void)
6329 {
6330   tree fndecl = current_function_decl;
6331   bool proto;
6332
6333   /* The argument information block for FNDECL.  */
6334   struct c_arg_info *arg_info = current_function_arg_info;
6335   current_function_arg_info = 0;
6336
6337   /* True if this definition is written with a prototype.  Note:
6338      despite C99 6.7.5.3p14, we can *not* treat an empty argument
6339      list in a function definition as equivalent to (void) -- an
6340      empty argument list specifies the function has no parameters,
6341      but only (void) sets up a prototype for future calls.  */
6342   proto = arg_info->types != 0;
6343
6344   if (proto)
6345     store_parm_decls_newstyle (fndecl, arg_info);
6346   else
6347     store_parm_decls_oldstyle (fndecl, arg_info);
6348
6349   /* The next call to push_scope will be a function body.  */
6350
6351   next_is_function_body = true;
6352
6353   /* Write a record describing this function definition to the prototypes
6354      file (if requested).  */
6355
6356   gen_aux_info_record (fndecl, 1, 0, proto);
6357
6358   /* Initialize the RTL code for the function.  */
6359   allocate_struct_function (fndecl);
6360
6361   /* Begin the statement tree for this function.  */
6362   DECL_SAVED_TREE (fndecl) = push_stmt_list ();
6363
6364   /* ??? Insert the contents of the pending sizes list into the function
6365      to be evaluated.  The only reason left to have this is
6366         void foo(int n, int array[n++])
6367      because we throw away the array type in favor of a pointer type, and
6368      thus won't naturally see the SAVE_EXPR containing the increment.  All
6369      other pending sizes would be handled by gimplify_parameters.  */
6370   {
6371     tree t;
6372     for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t))
6373       add_stmt (TREE_VALUE (t));
6374   }
6375
6376   /* Even though we're inside a function body, we still don't want to
6377      call expand_expr to calculate the size of a variable-sized array.
6378      We haven't necessarily assigned RTL to all variables yet, so it's
6379      not safe to try to expand expressions involving them.  */
6380   cfun->x_dont_save_pending_sizes_p = 1;
6381 }
6382 \f
6383 /* Handle attribute((warn_unused_result)) on FNDECL and all its nested
6384    functions.  */
6385
6386 static void
6387 c_warn_unused_result_recursively (tree fndecl)
6388 {
6389   struct cgraph_node *cgn;
6390
6391   /* Handle attribute((warn_unused_result)).  Relies on gimple input.  */
6392   c_warn_unused_result (&DECL_SAVED_TREE (fndecl));
6393
6394   /* Finalize all nested functions now.  */
6395   cgn = cgraph_node (fndecl);
6396   for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
6397     c_warn_unused_result_recursively (cgn->decl);
6398 }
6399
6400 /* Finish up a function declaration and compile that function
6401    all the way to assembler language output.  The free the storage
6402    for the function definition.
6403
6404    This is called after parsing the body of the function definition.  */
6405
6406 void
6407 finish_function (void)
6408 {
6409   tree fndecl = current_function_decl;
6410
6411   label_context_stack_se = label_context_stack_se->next;
6412   label_context_stack_vm = label_context_stack_vm->next;
6413
6414   if (TREE_CODE (fndecl) == FUNCTION_DECL
6415       && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6416     {
6417       tree args = DECL_ARGUMENTS (fndecl);
6418       for (; args; args = TREE_CHAIN (args))
6419         {
6420           tree type = TREE_TYPE (args);
6421           if (INTEGRAL_TYPE_P (type)
6422               && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6423             DECL_ARG_TYPE (args) = integer_type_node;
6424         }
6425     }
6426
6427   if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6428     BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6429
6430   /* Must mark the RESULT_DECL as being in this function.  */
6431
6432   if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6433     DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6434
6435   if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6436     {
6437       if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6438           != integer_type_node)
6439         {
6440           /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6441              If warn_main is -1 (-Wno-main) we don't want to be warned.  */
6442           if (!warn_main)
6443             pedwarn ("%Jreturn type of %qD is not %<int%>", fndecl, fndecl);
6444         }
6445       else
6446         {
6447           if (flag_isoc99)
6448             {
6449               tree stmt = c_finish_return (integer_zero_node);
6450 #ifdef USE_MAPPED_LOCATION
6451               /* Hack.  We don't want the middle-end to warn that this return
6452                  is unreachable, so we mark its location as special.  Using
6453                  UNKNOWN_LOCATION has the problem that it gets clobbered in
6454                  annotate_one_with_locus.  A cleaner solution might be to
6455                  ensure ! should_carry_locus_p (stmt), but that needs a flag.
6456               */
6457               SET_EXPR_LOCATION (stmt, BUILTINS_LOCATION);
6458 #else
6459               /* Hack.  We don't want the middle-end to warn that this
6460                  return is unreachable, so put the statement on the
6461                  special line 0.  */
6462               annotate_with_file_line (stmt, input_filename, 0);
6463 #endif
6464             }
6465         }
6466     }
6467
6468   /* Tie off the statement tree for this function.  */
6469   DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
6470
6471   finish_fname_decls ();
6472
6473   /* Complain if there's just no return statement.  */
6474   if (warn_return_type
6475       && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6476       && !current_function_returns_value && !current_function_returns_null
6477       /* Don't complain if we are no-return.  */
6478       && !current_function_returns_abnormally
6479       /* Don't warn for main().  */
6480       && !MAIN_NAME_P (DECL_NAME (fndecl))
6481       /* Or if they didn't actually specify a return type.  */
6482       && !C_FUNCTION_IMPLICIT_INT (fndecl)
6483       /* Normally, with -Wreturn-type, flow will complain.  Unless we're an
6484          inline function, as we might never be compiled separately.  */
6485       && DECL_INLINE (fndecl))
6486     {
6487       warning (0, "no return statement in function returning non-void");
6488       TREE_NO_WARNING (fndecl) = 1;
6489     }
6490
6491   /* With just -Wextra, complain only if function returns both with
6492      and without a value.  */
6493   if (extra_warnings
6494       && current_function_returns_value
6495       && current_function_returns_null)
6496     warning (0, "this function may return with or without a value");
6497
6498   /* Store the end of the function, so that we get good line number
6499      info for the epilogue.  */
6500   cfun->function_end_locus = input_location;
6501
6502   /* If we don't have ctors/dtors sections, and this is a static
6503      constructor or destructor, it must be recorded now.  */
6504   if (DECL_STATIC_CONSTRUCTOR (fndecl)
6505       && !targetm.have_ctors_dtors)
6506     static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6507   if (DECL_STATIC_DESTRUCTOR (fndecl)
6508       && !targetm.have_ctors_dtors)
6509     static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6510
6511   /* Finalize the ELF visibility for the function.  */
6512   c_determine_visibility (fndecl);
6513
6514   /* Genericize before inlining.  Delay genericizing nested functions
6515      until their parent function is genericized.  Since finalizing
6516      requires GENERIC, delay that as well.  */
6517
6518   if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
6519       && !undef_nested_function)
6520     {
6521       if (!decl_function_context (fndecl))
6522         {
6523           c_genericize (fndecl);
6524           c_warn_unused_result_recursively (fndecl);
6525
6526           /* ??? Objc emits functions after finalizing the compilation unit.
6527              This should be cleaned up later and this conditional removed.  */
6528           if (cgraph_global_info_ready)
6529             {
6530               c_expand_body (fndecl);
6531               return;
6532             }
6533
6534           cgraph_finalize_function (fndecl, false);
6535         }
6536       else
6537         {
6538           /* Register this function with cgraph just far enough to get it
6539             added to our parent's nested function list.  Handy, since the
6540             C front end doesn't have such a list.  */
6541           (void) cgraph_node (fndecl);
6542         }
6543     }
6544
6545   if (!decl_function_context (fndecl))
6546     undef_nested_function = false;
6547
6548   /* We're leaving the context of this function, so zap cfun.
6549      It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
6550      tree_rest_of_compilation.  */
6551   cfun = NULL;
6552   current_function_decl = NULL;
6553 }
6554
6555 /* Generate the RTL for the body of FNDECL.  */
6556
6557 void
6558 c_expand_body (tree fndecl)
6559 {
6560
6561   if (!DECL_INITIAL (fndecl)
6562       || DECL_INITIAL (fndecl) == error_mark_node)
6563     return;
6564
6565   tree_rest_of_compilation (fndecl);
6566
6567   if (DECL_STATIC_CONSTRUCTOR (fndecl)
6568       && targetm.have_ctors_dtors)
6569     targetm.asm_out.constructor (XEXP (DECL_RTL (fndecl), 0),
6570                                  DEFAULT_INIT_PRIORITY);
6571   if (DECL_STATIC_DESTRUCTOR (fndecl)
6572       && targetm.have_ctors_dtors)
6573     targetm.asm_out.destructor (XEXP (DECL_RTL (fndecl), 0),
6574                                 DEFAULT_INIT_PRIORITY);
6575 }
6576 \f
6577 /* Check the declarations given in a for-loop for satisfying the C99
6578    constraints.  */
6579 void
6580 check_for_loop_decls (void)
6581 {
6582   struct c_binding *b;
6583
6584   if (!flag_isoc99)
6585     {
6586       /* If we get here, declarations have been used in a for loop without
6587          the C99 for loop scope.  This doesn't make much sense, so don't
6588          allow it.  */
6589       error ("%<for%> loop initial declaration used outside C99 mode");
6590       return;
6591     }
6592   /* C99 subclause 6.8.5 paragraph 3:
6593
6594        [#3]  The  declaration  part  of  a for statement shall only
6595        declare identifiers for objects having storage class auto or
6596        register.
6597
6598      It isn't clear whether, in this sentence, "identifiers" binds to
6599      "shall only declare" or to "objects" - that is, whether all identifiers
6600      declared must be identifiers for objects, or whether the restriction
6601      only applies to those that are.  (A question on this in comp.std.c
6602      in November 2000 received no answer.)  We implement the strictest
6603      interpretation, to avoid creating an extension which later causes
6604      problems.  */
6605
6606   for (b = current_scope->bindings; b; b = b->prev)
6607     {
6608       tree id = b->id;
6609       tree decl = b->decl;
6610
6611       if (!id)
6612         continue;
6613
6614       switch (TREE_CODE (decl))
6615         {
6616         case VAR_DECL:
6617           if (TREE_STATIC (decl))
6618             error ("%Jdeclaration of static variable %qD in %<for%> loop "
6619                    "initial declaration", decl, decl);
6620           else if (DECL_EXTERNAL (decl))
6621             error ("%Jdeclaration of %<extern%> variable %qD in %<for%> loop "
6622                    "initial declaration", decl, decl);
6623           break;
6624
6625         case RECORD_TYPE:
6626           error ("%<struct %E%> declared in %<for%> loop initial declaration",
6627                  id);
6628           break;
6629         case UNION_TYPE:
6630           error ("%<union %E%> declared in %<for%> loop initial declaration",
6631                  id);
6632           break;
6633         case ENUMERAL_TYPE:
6634           error ("%<enum %E%> declared in %<for%> loop initial declaration",
6635                  id);
6636           break;
6637         default:
6638           error ("%Jdeclaration of non-variable %qD in %<for%> loop "
6639                  "initial declaration", decl, decl);
6640         }
6641     }
6642 }
6643 \f
6644 /* Save and reinitialize the variables
6645    used during compilation of a C function.  */
6646
6647 void
6648 c_push_function_context (struct function *f)
6649 {
6650   struct language_function *p;
6651   p = GGC_NEW (struct language_function);
6652   f->language = p;
6653
6654   p->base.x_stmt_tree = c_stmt_tree;
6655   p->x_break_label = c_break_label;
6656   p->x_cont_label = c_cont_label;
6657   p->x_switch_stack = c_switch_stack;
6658   p->arg_info = current_function_arg_info;
6659   p->returns_value = current_function_returns_value;
6660   p->returns_null = current_function_returns_null;
6661   p->returns_abnormally = current_function_returns_abnormally;
6662   p->warn_about_return_type = warn_about_return_type;
6663   p->extern_inline = current_extern_inline;
6664 }
6665
6666 /* Restore the variables used during compilation of a C function.  */
6667
6668 void
6669 c_pop_function_context (struct function *f)
6670 {
6671   struct language_function *p = f->language;
6672
6673   if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
6674       && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6675     {
6676       /* Stop pointing to the local nodes about to be freed.  */
6677       /* But DECL_INITIAL must remain nonzero so we know this
6678          was an actual function definition.  */
6679       DECL_INITIAL (current_function_decl) = error_mark_node;
6680       DECL_ARGUMENTS (current_function_decl) = 0;
6681     }
6682
6683   c_stmt_tree = p->base.x_stmt_tree;
6684   c_break_label = p->x_break_label;
6685   c_cont_label = p->x_cont_label;
6686   c_switch_stack = p->x_switch_stack;
6687   current_function_arg_info = p->arg_info;
6688   current_function_returns_value = p->returns_value;
6689   current_function_returns_null = p->returns_null;
6690   current_function_returns_abnormally = p->returns_abnormally;
6691   warn_about_return_type = p->warn_about_return_type;
6692   current_extern_inline = p->extern_inline;
6693
6694   f->language = NULL;
6695 }
6696
6697 /* Copy the DECL_LANG_SPECIFIC data associated with DECL.  */
6698
6699 void
6700 c_dup_lang_specific_decl (tree decl)
6701 {
6702   struct lang_decl *ld;
6703
6704   if (!DECL_LANG_SPECIFIC (decl))
6705     return;
6706
6707   ld = GGC_NEW (struct lang_decl);
6708   memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6709   DECL_LANG_SPECIFIC (decl) = ld;
6710 }
6711
6712 /* The functions below are required for functionality of doing
6713    function at once processing in the C front end. Currently these
6714    functions are not called from anywhere in the C front end, but as
6715    these changes continue, that will change.  */
6716
6717 /* Returns nonzero if the current statement is a full expression,
6718    i.e. temporaries created during that statement should be destroyed
6719    at the end of the statement.  */
6720
6721 int
6722 stmts_are_full_exprs_p (void)
6723 {
6724   return 0;
6725 }
6726
6727 /* Returns the stmt_tree (if any) to which statements are currently
6728    being added.  If there is no active statement-tree, NULL is
6729    returned.  */
6730
6731 stmt_tree
6732 current_stmt_tree (void)
6733 {
6734   return &c_stmt_tree;
6735 }
6736
6737 /* Nonzero if TYPE is an anonymous union or struct type.  Always 0 in
6738    C.  */
6739
6740 int
6741 anon_aggr_type_p (tree ARG_UNUSED (node))
6742 {
6743   return 0;
6744 }
6745
6746 /* Return the global value of T as a symbol.  */
6747
6748 tree
6749 identifier_global_value (tree t)
6750 {
6751   struct c_binding *b;
6752
6753   for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
6754     if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
6755       return b->decl;
6756
6757   return 0;
6758 }
6759
6760 /* Record a builtin type for C.  If NAME is non-NULL, it is the name used;
6761    otherwise the name is found in ridpointers from RID_INDEX.  */
6762
6763 void
6764 record_builtin_type (enum rid rid_index, const char *name, tree type)
6765 {
6766   tree id, decl;
6767   if (name == 0)
6768     id = ridpointers[(int) rid_index];
6769   else
6770     id = get_identifier (name);
6771   decl = build_decl (TYPE_DECL, id, type);
6772   pushdecl (decl);
6773   if (debug_hooks->type_decl)
6774     debug_hooks->type_decl (decl, false);
6775 }
6776
6777 /* Build the void_list_node (void_type_node having been created).  */
6778 tree
6779 build_void_list_node (void)
6780 {
6781   tree t = build_tree_list (NULL_TREE, void_type_node);
6782   return t;
6783 }
6784
6785 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR.  */
6786
6787 struct c_parm *
6788 build_c_parm (struct c_declspecs *specs, tree attrs,
6789               struct c_declarator *declarator)
6790 {
6791   struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
6792   ret->specs = specs;
6793   ret->attrs = attrs;
6794   ret->declarator = declarator;
6795   return ret;
6796 }
6797
6798 /* Return a declarator with nested attributes.  TARGET is the inner
6799    declarator to which these attributes apply.  ATTRS are the
6800    attributes.  */
6801
6802 struct c_declarator *
6803 build_attrs_declarator (tree attrs, struct c_declarator *target)
6804 {
6805   struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6806   ret->kind = cdk_attrs;
6807   ret->declarator = target;
6808   ret->u.attrs = attrs;
6809   return ret;
6810 }
6811
6812 /* Return a declarator for a function with arguments specified by ARGS
6813    and return type specified by TARGET.  */
6814
6815 struct c_declarator *
6816 build_function_declarator (struct c_arg_info *args,
6817                            struct c_declarator *target)
6818 {
6819   struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6820   ret->kind = cdk_function;
6821   ret->declarator = target;
6822   ret->u.arg_info = args;
6823   return ret;
6824 }
6825
6826 /* Return a declarator for the identifier IDENT (which may be
6827    NULL_TREE for an abstract declarator).  */
6828
6829 struct c_declarator *
6830 build_id_declarator (tree ident)
6831 {
6832   struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6833   ret->kind = cdk_id;
6834   ret->declarator = 0;
6835   ret->u.id = ident;
6836   /* Default value - may get reset to a more precise location. */
6837   ret->id_loc = input_location;
6838   return ret;
6839 }
6840
6841 /* Return something to represent absolute declarators containing a *.
6842    TARGET is the absolute declarator that the * contains.
6843    TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
6844    to apply to the pointer type.  */
6845
6846 struct c_declarator *
6847 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
6848                          struct c_declarator *target)
6849 {
6850   tree attrs;
6851   int quals = 0;
6852   struct c_declarator *itarget = target;
6853   struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6854   if (type_quals_attrs)
6855     {
6856       attrs = type_quals_attrs->attrs;
6857       quals = quals_from_declspecs (type_quals_attrs);
6858       if (attrs != NULL_TREE)
6859         itarget = build_attrs_declarator (attrs, target);
6860     }
6861   ret->kind = cdk_pointer;
6862   ret->declarator = itarget;
6863   ret->u.pointer_quals = quals;
6864   return ret;
6865 }
6866
6867 /* Return a pointer to a structure for an empty list of declaration
6868    specifiers.  */
6869
6870 struct c_declspecs *
6871 build_null_declspecs (void)
6872 {
6873   struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
6874   ret->type = 0;
6875   ret->decl_attr = 0;
6876   ret->attrs = 0;
6877   ret->typespec_word = cts_none;
6878   ret->storage_class = csc_none;
6879   ret->declspecs_seen_p = false;
6880   ret->type_seen_p = false;
6881   ret->non_sc_seen_p = false;
6882   ret->typedef_p = false;
6883   ret->tag_defined_p = false;
6884   ret->explicit_signed_p = false;
6885   ret->deprecated_p = false;
6886   ret->default_int_p = false;
6887   ret->long_p = false;
6888   ret->long_long_p = false;
6889   ret->short_p = false;
6890   ret->signed_p = false;
6891   ret->unsigned_p = false;
6892   ret->complex_p = false;
6893   ret->inline_p = false;
6894   ret->thread_p = false;
6895   ret->const_p = false;
6896   ret->volatile_p = false;
6897   ret->restrict_p = false;
6898   return ret;
6899 }
6900
6901 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
6902    returning SPECS.  */
6903
6904 struct c_declspecs *
6905 declspecs_add_qual (struct c_declspecs *specs, tree qual)
6906 {
6907   enum rid i;
6908   bool dupe = false;
6909   specs->non_sc_seen_p = true;
6910   specs->declspecs_seen_p = true;
6911   gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
6912               && C_IS_RESERVED_WORD (qual));
6913   i = C_RID_CODE (qual);
6914   switch (i)
6915     {
6916     case RID_CONST:
6917       dupe = specs->const_p;
6918       specs->const_p = true;
6919       break;
6920     case RID_VOLATILE:
6921       dupe = specs->volatile_p;
6922       specs->volatile_p = true;
6923       break;
6924     case RID_RESTRICT:
6925       dupe = specs->restrict_p;
6926       specs->restrict_p = true;
6927       break;
6928     default:
6929       gcc_unreachable ();
6930     }
6931   if (dupe && pedantic && !flag_isoc99)
6932     pedwarn ("duplicate %qE", qual);
6933   return specs;
6934 }
6935
6936 /* Add the type specifier TYPE to the declaration specifiers SPECS,
6937    returning SPECS.  */
6938
6939 struct c_declspecs *
6940 declspecs_add_type (struct c_declspecs *specs, struct c_typespec spec)
6941 {
6942   tree type = spec.spec;
6943   specs->non_sc_seen_p = true;
6944   specs->declspecs_seen_p = true;
6945   specs->type_seen_p = true;
6946   if (TREE_DEPRECATED (type))
6947     specs->deprecated_p = true;
6948
6949   /* Handle type specifier keywords.  */
6950   if (TREE_CODE (type) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (type))
6951     {
6952       enum rid i = C_RID_CODE (type);
6953       if (specs->type)
6954         {
6955           error ("two or more data types in declaration specifiers");
6956           return specs;
6957         }
6958       if ((int) i <= (int) RID_LAST_MODIFIER)
6959         {
6960           /* "long", "short", "signed", "unsigned" or "_Complex".  */
6961           bool dupe = false;
6962           switch (i)
6963             {
6964             case RID_LONG:
6965               if (specs->long_long_p)
6966                 {
6967                   error ("%<long long long%> is too long for GCC");
6968                   break;
6969                 }
6970               if (specs->long_p)
6971                 {
6972                   if (specs->typespec_word == cts_double)
6973                     {
6974                       error ("both %<long long%> and %<double%> in "
6975                              "declaration specifiers");
6976                       break;
6977                     }
6978                   if (pedantic && !flag_isoc99 && !in_system_header
6979                       && warn_long_long)
6980                     pedwarn ("ISO C90 does not support %<long long%>");
6981                   specs->long_long_p = 1;
6982                   break;
6983                 }
6984               if (specs->short_p)
6985                 error ("both %<long%> and %<short%> in "
6986                        "declaration specifiers");
6987               else if (specs->typespec_word == cts_void)
6988                 error ("both %<long%> and %<void%> in "
6989                        "declaration specifiers");
6990               else if (specs->typespec_word == cts_bool)
6991                 error ("both %<long%> and %<_Bool%> in "
6992                        "declaration specifiers");
6993               else if (specs->typespec_word == cts_char)
6994                 error ("both %<long%> and %<char%> in "
6995                        "declaration specifiers");
6996               else if (specs->typespec_word == cts_float)
6997                 error ("both %<long%> and %<float%> in "
6998                        "declaration specifiers");
6999               else
7000                 specs->long_p = true;
7001               break;
7002             case RID_SHORT:
7003               dupe = specs->short_p;
7004               if (specs->long_p)
7005                 error ("both %<long%> and %<short%> in "
7006                        "declaration specifiers");
7007               else if (specs->typespec_word == cts_void)
7008                 error ("both %<short%> and %<void%> in "
7009                        "declaration specifiers");
7010               else if (specs->typespec_word == cts_bool)
7011                 error ("both %<short%> and %<_Bool%> in "
7012                        "declaration specifiers");
7013               else if (specs->typespec_word == cts_char)
7014                 error ("both %<short%> and %<char%> in "
7015                        "declaration specifiers");
7016               else if (specs->typespec_word == cts_float)
7017                 error ("both %<short%> and %<float%> in "
7018                        "declaration specifiers");
7019               else if (specs->typespec_word == cts_double)
7020                 error ("both %<short%> and %<double%> in "
7021                        "declaration specifiers");
7022               else
7023                 specs->short_p = true;
7024               break;
7025             case RID_SIGNED:
7026               dupe = specs->signed_p;
7027               if (specs->unsigned_p)
7028                 error ("both %<signed%> and %<unsigned%> in "
7029                        "declaration specifiers");
7030               else if (specs->typespec_word == cts_void)
7031                 error ("both %<signed%> and %<void%> in "
7032                        "declaration specifiers");
7033               else if (specs->typespec_word == cts_bool)
7034                 error ("both %<signed%> and %<_Bool%> in "
7035                        "declaration specifiers");
7036               else if (specs->typespec_word == cts_float)
7037                 error ("both %<signed%> and %<float%> in "
7038                        "declaration specifiers");
7039               else if (specs->typespec_word == cts_double)
7040                 error ("both %<signed%> and %<double%> in "
7041                        "declaration specifiers");
7042               else
7043                 specs->signed_p = true;
7044               break;
7045             case RID_UNSIGNED:
7046               dupe = specs->unsigned_p;
7047               if (specs->signed_p)
7048                 error ("both %<signed%> and %<unsigned%> in "
7049                        "declaration specifiers");
7050               else if (specs->typespec_word == cts_void)
7051                 error ("both %<unsigned%> and %<void%> in "
7052                        "declaration specifiers");
7053               else if (specs->typespec_word == cts_bool)
7054                 error ("both %<unsigned%> and %<_Bool%> in "
7055                        "declaration specifiers");
7056               else if (specs->typespec_word == cts_float)
7057                 error ("both %<unsigned%> and %<float%> in "
7058                        "declaration specifiers");
7059               else if (specs->typespec_word == cts_double)
7060                 error ("both %<unsigned%> and %<double%> in "
7061                        "declaration specifiers");
7062               else
7063                 specs->unsigned_p = true;
7064               break;
7065             case RID_COMPLEX:
7066               dupe = specs->complex_p;
7067               if (pedantic && !flag_isoc99 && !in_system_header)
7068                 pedwarn ("ISO C90 does not support complex types");
7069               if (specs->typespec_word == cts_void)
7070                 error ("both %<complex%> and %<void%> in "
7071                        "declaration specifiers");
7072               else if (specs->typespec_word == cts_bool)
7073                 error ("both %<complex%> and %<_Bool%> in "
7074                        "declaration specifiers");
7075               else
7076                 specs->complex_p = true;
7077               break;
7078             default:
7079               gcc_unreachable ();
7080             }
7081
7082           if (dupe)
7083             error ("duplicate %qE", type);
7084
7085           return specs;
7086         }
7087       else
7088         {
7089           /* "void", "_Bool", "char", "int", "float" or "double".  */
7090           if (specs->typespec_word != cts_none)
7091             {
7092               error ("two or more data types in declaration specifiers");
7093               return specs;
7094             }
7095           switch (i)
7096             {
7097             case RID_VOID:
7098               if (specs->long_p)
7099                 error ("both %<long%> and %<void%> in "
7100                        "declaration specifiers");
7101               else if (specs->short_p)
7102                 error ("both %<short%> and %<void%> in "
7103                        "declaration specifiers");
7104               else if (specs->signed_p)
7105                 error ("both %<signed%> and %<void%> in "
7106                        "declaration specifiers");
7107               else if (specs->unsigned_p)
7108                 error ("both %<unsigned%> and %<void%> in "
7109                        "declaration specifiers");
7110               else if (specs->complex_p)
7111                 error ("both %<complex%> and %<void%> in "
7112                        "declaration specifiers");
7113               else
7114                 specs->typespec_word = cts_void;
7115               return specs;
7116             case RID_BOOL:
7117               if (specs->long_p)
7118                 error ("both %<long%> and %<_Bool%> in "
7119                        "declaration specifiers");
7120               else if (specs->short_p)
7121                 error ("both %<short%> and %<_Bool%> in "
7122                        "declaration specifiers");
7123               else if (specs->signed_p)
7124                 error ("both %<signed%> and %<_Bool%> in "
7125                        "declaration specifiers");
7126               else if (specs->unsigned_p)
7127                 error ("both %<unsigned%> and %<_Bool%> in "
7128                        "declaration specifiers");
7129               else if (specs->complex_p)
7130                 error ("both %<complex%> and %<_Bool%> in "
7131                        "declaration specifiers");
7132               else
7133                 specs->typespec_word = cts_bool;
7134               return specs;
7135             case RID_CHAR:
7136               if (specs->long_p)
7137                 error ("both %<long%> and %<char%> in "
7138                        "declaration specifiers");
7139               else if (specs->short_p)
7140                 error ("both %<short%> and %<char%> in "
7141                        "declaration specifiers");
7142               else
7143                 specs->typespec_word = cts_char;
7144               return specs;
7145             case RID_INT:
7146               specs->typespec_word = cts_int;
7147               return specs;
7148             case RID_FLOAT:
7149               if (specs->long_p)
7150                 error ("both %<long%> and %<float%> in "
7151                        "declaration specifiers");
7152               else if (specs->short_p)
7153                 error ("both %<short%> and %<float%> in "
7154                        "declaration specifiers");
7155               else if (specs->signed_p)
7156                 error ("both %<signed%> and %<float%> in "
7157                        "declaration specifiers");
7158               else if (specs->unsigned_p)
7159                 error ("both %<unsigned%> and %<float%> in "
7160                        "declaration specifiers");
7161               else
7162                 specs->typespec_word = cts_float;
7163               return specs;
7164             case RID_DOUBLE:
7165               if (specs->long_long_p)
7166                 error ("both %<long long%> and %<double%> in "
7167                        "declaration specifiers");
7168               else if (specs->short_p)
7169                 error ("both %<short%> and %<double%> in "
7170                        "declaration specifiers");
7171               else if (specs->signed_p)
7172                 error ("both %<signed%> and %<double%> in "
7173                        "declaration specifiers");
7174               else if (specs->unsigned_p)
7175                 error ("both %<unsigned%> and %<double%> in "
7176                        "declaration specifiers");
7177               else
7178                 specs->typespec_word = cts_double;
7179               return specs;
7180             default:
7181               /* ObjC reserved word "id", handled below.  */
7182               break;
7183             }
7184         }
7185     }
7186
7187   /* Now we have a typedef (a TYPE_DECL node), an identifier (some
7188      form of ObjC type, cases such as "int" and "long" being handled
7189      above), a TYPE (struct, union, enum and typeof specifiers) or an
7190      ERROR_MARK.  In none of these cases may there have previously
7191      been any type specifiers.  */
7192   if (specs->type || specs->typespec_word != cts_none
7193       || specs->long_p || specs->short_p || specs->signed_p
7194       || specs->unsigned_p || specs->complex_p)
7195     error ("two or more data types in declaration specifiers");
7196   else if (TREE_CODE (type) == TYPE_DECL)
7197     {
7198       if (TREE_TYPE (type) == error_mark_node)
7199         ; /* Allow the type to default to int to avoid cascading errors.  */
7200       else
7201         {
7202           specs->type = TREE_TYPE (type);
7203           specs->decl_attr = DECL_ATTRIBUTES (type);
7204           specs->typedef_p = true;
7205           specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
7206         }
7207     }
7208   else if (TREE_CODE (type) == IDENTIFIER_NODE)
7209     {
7210       tree t = lookup_name (type);
7211       if (!t || TREE_CODE (t) != TYPE_DECL)
7212         error ("%qE fails to be a typedef or built in type", type);
7213       else if (TREE_TYPE (t) == error_mark_node)
7214         ;
7215       else
7216         specs->type = TREE_TYPE (t);
7217     }
7218   else if (TREE_CODE (type) != ERROR_MARK)
7219     {
7220       if (spec.kind == ctsk_tagdef || spec.kind == ctsk_tagfirstref)
7221         specs->tag_defined_p = true;
7222       if (spec.kind == ctsk_typeof)
7223         specs->typedef_p = true;
7224       specs->type = type;
7225     }
7226
7227   return specs;
7228 }
7229
7230 /* Add the storage class specifier or function specifier SCSPEC to the
7231    declaration specifiers SPECS, returning SPECS.  */
7232
7233 struct c_declspecs *
7234 declspecs_add_scspec (struct c_declspecs *specs, tree scspec)
7235 {
7236   enum rid i;
7237   enum c_storage_class n = csc_none;
7238   bool dupe = false;
7239   specs->declspecs_seen_p = true;
7240   gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
7241               && C_IS_RESERVED_WORD (scspec));
7242   i = C_RID_CODE (scspec);
7243   if (extra_warnings && specs->non_sc_seen_p)
7244     warning (0, "%qE is not at beginning of declaration", scspec);
7245   switch (i)
7246     {
7247     case RID_INLINE:
7248       /* C99 permits duplicate inline.  Although of doubtful utility,
7249          it seems simplest to permit it in gnu89 mode as well, as
7250          there is also little utility in maintaining this as a
7251          difference between gnu89 and C99 inline.  */
7252       dupe = false;
7253       specs->inline_p = true;
7254       break;
7255     case RID_THREAD:
7256       dupe = specs->thread_p;
7257       if (specs->storage_class == csc_auto)
7258         error ("%<__thread%> used with %<auto%>");
7259       else if (specs->storage_class == csc_register)
7260         error ("%<__thread%> used with %<register%>");
7261       else if (specs->storage_class == csc_typedef)
7262         error ("%<__thread%> used with %<typedef%>");
7263       else
7264         specs->thread_p = true;
7265       break;
7266     case RID_AUTO:
7267       n = csc_auto;
7268       break;
7269     case RID_EXTERN:
7270       n = csc_extern;
7271       /* Diagnose "__thread extern".  */
7272       if (specs->thread_p)
7273         error ("%<__thread%> before %<extern%>");
7274       break;
7275     case RID_REGISTER:
7276       n = csc_register;
7277       break;
7278     case RID_STATIC:
7279       n = csc_static;
7280       /* Diagnose "__thread static".  */
7281       if (specs->thread_p)
7282         error ("%<__thread%> before %<static%>");
7283       break;
7284     case RID_TYPEDEF:
7285       n = csc_typedef;
7286       break;
7287     default:
7288       gcc_unreachable ();
7289     }
7290   if (n != csc_none && n == specs->storage_class)
7291     dupe = true;
7292   if (dupe)
7293     error ("duplicate %qE", scspec);
7294   if (n != csc_none)
7295     {
7296       if (specs->storage_class != csc_none && n != specs->storage_class)
7297         {
7298           error ("multiple storage classes in declaration specifiers");
7299         }
7300       else
7301         {
7302           specs->storage_class = n;
7303           if (n != csc_extern && n != csc_static && specs->thread_p)
7304             {
7305               error ("%<__thread%> used with %qE", scspec);
7306               specs->thread_p = false;
7307             }
7308         }
7309     }
7310   return specs;
7311 }
7312
7313 /* Add the attributes ATTRS to the declaration specifiers SPECS,
7314    returning SPECS.  */
7315
7316 struct c_declspecs *
7317 declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
7318 {
7319   specs->attrs = chainon (attrs, specs->attrs);
7320   specs->declspecs_seen_p = true;
7321   return specs;
7322 }
7323
7324 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
7325    specifiers with any other type specifier to determine the resulting
7326    type.  This is where ISO C checks on complex types are made, since
7327    "_Complex long" is a prefix of the valid ISO C type "_Complex long
7328    double".  */
7329
7330 struct c_declspecs *
7331 finish_declspecs (struct c_declspecs *specs)
7332 {
7333   /* If a type was specified as a whole, we have no modifiers and are
7334      done.  */
7335   if (specs->type != NULL_TREE)
7336     {
7337       gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
7338                   && !specs->signed_p && !specs->unsigned_p
7339                   && !specs->complex_p);
7340       return specs;
7341     }
7342
7343   /* If none of "void", "_Bool", "char", "int", "float" or "double"
7344      has been specified, treat it as "int" unless "_Complex" is
7345      present and there are no other specifiers.  If we just have
7346      "_Complex", it is equivalent to "_Complex double", but e.g.
7347      "_Complex short" is equivalent to "_Complex short int".  */
7348   if (specs->typespec_word == cts_none)
7349     {
7350       if (specs->long_p || specs->short_p
7351           || specs->signed_p || specs->unsigned_p)
7352         {
7353           specs->typespec_word = cts_int;
7354         }
7355       else if (specs->complex_p)
7356         {
7357           specs->typespec_word = cts_double;
7358           if (pedantic)
7359             pedwarn ("ISO C does not support plain %<complex%> meaning "
7360                      "%<double complex%>");
7361         }
7362       else
7363         {
7364           specs->typespec_word = cts_int;
7365           specs->default_int_p = true;
7366           /* We don't diagnose this here because grokdeclarator will
7367              give more specific diagnostics according to whether it is
7368              a function definition.  */
7369         }
7370     }
7371
7372   /* If "signed" was specified, record this to distinguish "int" and
7373      "signed int" in the case of a bit-field with
7374      -funsigned-bitfields.  */
7375   specs->explicit_signed_p = specs->signed_p;
7376
7377   /* Now compute the actual type.  */
7378   switch (specs->typespec_word)
7379     {
7380     case cts_void:
7381       gcc_assert (!specs->long_p && !specs->short_p
7382                   && !specs->signed_p && !specs->unsigned_p
7383                   && !specs->complex_p);
7384       specs->type = void_type_node;
7385       break;
7386     case cts_bool:
7387       gcc_assert (!specs->long_p && !specs->short_p
7388                   && !specs->signed_p && !specs->unsigned_p
7389                   && !specs->complex_p);
7390       specs->type = boolean_type_node;
7391       break;
7392     case cts_char:
7393       gcc_assert (!specs->long_p && !specs->short_p);
7394       gcc_assert (!(specs->signed_p && specs->unsigned_p));
7395       if (specs->signed_p)
7396         specs->type = signed_char_type_node;
7397       else if (specs->unsigned_p)
7398         specs->type = unsigned_char_type_node;
7399       else
7400         specs->type = char_type_node;
7401       if (specs->complex_p)
7402         {
7403           if (pedantic)
7404             pedwarn ("ISO C does not support complex integer types");
7405           specs->type = build_complex_type (specs->type);
7406         }
7407       break;
7408     case cts_int:
7409       gcc_assert (!(specs->long_p && specs->short_p));
7410       gcc_assert (!(specs->signed_p && specs->unsigned_p));
7411       if (specs->long_long_p)
7412         specs->type = (specs->unsigned_p
7413                        ? long_long_unsigned_type_node
7414                        : long_long_integer_type_node);
7415       else if (specs->long_p)
7416         specs->type = (specs->unsigned_p
7417                        ? long_unsigned_type_node
7418                        : long_integer_type_node);
7419       else if (specs->short_p)
7420         specs->type = (specs->unsigned_p
7421                        ? short_unsigned_type_node
7422                        : short_integer_type_node);
7423       else
7424         specs->type = (specs->unsigned_p
7425                        ? unsigned_type_node
7426                        : integer_type_node);
7427       if (specs->complex_p)
7428         {
7429           if (pedantic)
7430             pedwarn ("ISO C does not support complex integer types");
7431           specs->type = build_complex_type (specs->type);
7432         }
7433       break;
7434     case cts_float:
7435       gcc_assert (!specs->long_p && !specs->short_p
7436                   && !specs->signed_p && !specs->unsigned_p);
7437       specs->type = (specs->complex_p
7438                      ? complex_float_type_node
7439                      : float_type_node);
7440       break;
7441     case cts_double:
7442       gcc_assert (!specs->long_long_p && !specs->short_p
7443                   && !specs->signed_p && !specs->unsigned_p);
7444       if (specs->long_p)
7445         {
7446           specs->type = (specs->complex_p
7447                          ? complex_long_double_type_node
7448                          : long_double_type_node);
7449         }
7450       else
7451         {
7452           specs->type = (specs->complex_p
7453                          ? complex_double_type_node
7454                          : double_type_node);
7455         }
7456       break;
7457     default:
7458       gcc_unreachable ();
7459     }
7460
7461   return specs;
7462 }
7463
7464 /* Synthesize a function which calls all the global ctors or global
7465    dtors in this file.  This is only used for targets which do not
7466    support .ctors/.dtors sections.  FIXME: Migrate into cgraph.  */
7467 static void
7468 build_cdtor (int method_type, tree cdtors)
7469 {
7470   tree body = 0;
7471
7472   if (!cdtors)
7473     return;
7474
7475   for (; cdtors; cdtors = TREE_CHAIN (cdtors))
7476     append_to_statement_list (build_function_call (TREE_VALUE (cdtors), 0),
7477                               &body);
7478
7479   cgraph_build_static_cdtor (method_type, body, DEFAULT_INIT_PRIORITY);
7480 }
7481
7482 /* Perform final processing on one file scope's declarations (or the
7483    external scope's declarations), GLOBALS.  */
7484 static void
7485 c_write_global_declarations_1 (tree globals)
7486 {
7487   size_t len = list_length (globals);
7488   tree *vec = XNEWVEC (tree, len);
7489   size_t i;
7490   tree decl;
7491
7492   /* Process the decls in the order they were written.  */
7493   for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
7494     {
7495       vec[i] = decl;
7496       /* Check for used but undefined static functions using the C
7497          standard's definition of "used", and set TREE_NO_WARNING so
7498          that check_global_declarations doesn't repeat the check.  */
7499       if (TREE_CODE (decl) == FUNCTION_DECL
7500           && DECL_INITIAL (decl) == 0
7501           && DECL_EXTERNAL (decl)
7502           && !TREE_PUBLIC (decl)
7503           && C_DECL_USED (decl))
7504         {
7505           pedwarn ("%J%qF used but never defined", decl, decl);
7506           TREE_NO_WARNING (decl) = 1;
7507         }
7508     }
7509
7510   wrapup_global_declarations (vec, len);
7511   check_global_declarations (vec, len);
7512
7513   free (vec);
7514 }
7515
7516 void
7517 c_write_global_declarations (void)
7518 {
7519   tree ext_block, t;
7520
7521   /* We don't want to do this if generating a PCH.  */
7522   if (pch_file)
7523     return;
7524
7525   /* Don't waste time on further processing if -fsyntax-only or we've
7526      encountered errors.  */
7527   if (flag_syntax_only || errorcount || sorrycount || cpp_errors (parse_in))
7528     return;
7529
7530   /* Close the external scope.  */
7531   ext_block = pop_scope ();
7532   external_scope = 0;
7533   gcc_assert (!current_scope);
7534
7535   /* Process all file scopes in this compilation, and the external_scope,
7536      through wrapup_global_declarations and check_global_declarations.  */
7537   for (t = all_translation_units; t; t = TREE_CHAIN (t))
7538     c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
7539   c_write_global_declarations_1 (BLOCK_VARS (ext_block));
7540
7541   /* Generate functions to call static constructors and destructors
7542      for targets that do not support .ctors/.dtors sections.  These
7543      functions have magic names which are detected by collect2.  */
7544   build_cdtor ('I', static_ctors); static_ctors = 0;
7545   build_cdtor ('D', static_dtors); static_dtors = 0;
7546
7547   /* We're done parsing; proceed to optimize and emit assembly.
7548      FIXME: shouldn't be the front end's responsibility to call this.  */
7549   cgraph_optimize ();
7550 }
7551
7552 #include "gt-c-decl.h"