OSDN Git Service

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