OSDN Git Service

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