OSDN Git Service

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