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