OSDN Git Service

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