OSDN Git Service

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