OSDN Git Service

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