OSDN Git Service

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