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