OSDN Git Service

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