OSDN Git Service

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