OSDN Git Service

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