OSDN Git Service

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