OSDN Git Service

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