OSDN Git Service

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