OSDN Git Service

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