]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - contrib/gcc/c-decl.c
MFC r368207,368607:
[FreeBSD/stable/10.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 [].  ABSTRACT_P is true if it is an
3154    abstract declarator, false otherwise; this is used to reject static
3155    and type qualifiers in abstract declarators, where they are not in
3156    the C99 grammar (subject to possible change in DR#289).  */
3157
3158 struct c_declarator *
3159 set_array_declarator_inner (struct c_declarator *decl,
3160                             struct c_declarator *inner, bool abstract_p)
3161 {
3162   decl->declarator = inner;
3163   if (abstract_p && (decl->u.array.quals != TYPE_UNQUALIFIED
3164                      || decl->u.array.attrs != NULL_TREE
3165                      || decl->u.array.static_p))
3166     error ("static or type qualifiers in abstract declarator");
3167   return decl;
3168 }
3169
3170 /* INIT is a constructor that forms DECL's initializer.  If the final
3171    element initializes a flexible array field, add the size of that
3172    initializer to DECL's size.  */
3173
3174 static void
3175 add_flexible_array_elts_to_size (tree decl, tree init)
3176 {
3177   tree elt, type;
3178
3179   if (VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (init)))
3180     return;
3181
3182   elt = VEC_last (constructor_elt, CONSTRUCTOR_ELTS (init))->value;
3183   type = TREE_TYPE (elt);
3184   if (TREE_CODE (type) == ARRAY_TYPE
3185       && TYPE_SIZE (type) == NULL_TREE
3186       && TYPE_DOMAIN (type) != NULL_TREE
3187       && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
3188     {
3189       complete_array_type (&type, elt, false);
3190       DECL_SIZE (decl)
3191         = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
3192       DECL_SIZE_UNIT (decl)
3193         = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
3194     }
3195 }
3196
3197 /* APPLE LOCAL begin blocks 6339747 */
3198 /* Decode a block literal type, such as "int **", returning a ...FUNCTION_DECL node.  */
3199
3200 tree
3201 grokblockdecl (struct c_declspecs *specs, struct c_declarator *declarator)
3202 {
3203   tree decl;
3204   tree attrs = specs->attrs;
3205
3206   specs->attrs = NULL_TREE;
3207
3208   decl = grokdeclarator (declarator, specs, BLOCKDEF,
3209                           false, NULL);
3210
3211   /* Apply attributes.  */
3212   decl_attributes (&decl, attrs, 0);
3213
3214   return decl;
3215 }
3216 /* APPLE LOCAL end blocks 6339747 */
3217
3218 /* Decode a "typename", such as "int **", returning a ..._TYPE node.  */
3219
3220 tree
3221 groktypename (struct c_type_name *type_name)
3222 {
3223   tree type;
3224   tree attrs = type_name->specs->attrs;
3225
3226   type_name->specs->attrs = NULL_TREE;
3227
3228   type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
3229                          false, NULL);
3230
3231   /* Apply attributes.  */
3232   decl_attributes (&type, attrs, 0);
3233
3234   return type;
3235 }
3236
3237 /* Decode a declarator in an ordinary declaration or data definition.
3238    This is called as soon as the type information and variable name
3239    have been parsed, before parsing the initializer if any.
3240    Here we create the ..._DECL node, fill in its type,
3241    and put it on the list of decls for the current context.
3242    The ..._DECL node is returned as the value.
3243
3244    Exception: for arrays where the length is not specified,
3245    the type is left null, to be filled in by `finish_decl'.
3246
3247    Function definitions do not come here; they go to start_function
3248    instead.  However, external and forward declarations of functions
3249    do go through here.  Structure field declarations are done by
3250    grokfield and not through here.  */
3251
3252 tree
3253 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
3254             bool initialized, tree attributes)
3255 {
3256   tree decl;
3257   tree tem;
3258
3259   /* An object declared as __attribute__((deprecated)) suppresses
3260      warnings of uses of other deprecated items.  */
3261   /* APPLE LOCAL begin "unavailable" attribute (radar 2809697) */
3262   /* An object declared as __attribute__((unavailable)) suppresses
3263      any reports of being declared with unavailable or deprecated
3264      items.  An object declared as __attribute__((deprecated))
3265      suppresses warnings of uses of other deprecated items.  */
3266 #ifdef A_LESS_INEFFICENT_WAY /* which I really don't want to do!  */
3267   if (lookup_attribute ("deprecated", attributes))
3268     deprecated_state = DEPRECATED_SUPPRESS;
3269   else if (lookup_attribute ("unavailable", attributes))
3270     deprecated_state = DEPRECATED_UNAVAILABLE_SUPPRESS;
3271 #else /* a more efficient way doing what lookup_attribute would do */
3272   tree a;
3273
3274   for (a = attributes; a; a = TREE_CHAIN (a))
3275     {
3276       tree name = TREE_PURPOSE (a);
3277       if (TREE_CODE (name) == IDENTIFIER_NODE)
3278         if (is_attribute_p ("deprecated", name))
3279           {
3280             deprecated_state = DEPRECATED_SUPPRESS;
3281             break;
3282           }
3283         if (is_attribute_p ("unavailable", name))
3284           {
3285             deprecated_state = DEPRECATED_UNAVAILABLE_SUPPRESS;
3286             break;
3287           }
3288     }
3289 #endif
3290   /* APPLE LOCAL end "unavailable" attribute (radar 2809697) */
3291
3292   decl = grokdeclarator (declarator, declspecs,
3293                          NORMAL, initialized, NULL);
3294   if (!decl)
3295     return 0;
3296
3297   deprecated_state = DEPRECATED_NORMAL;
3298
3299   if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
3300       && MAIN_NAME_P (DECL_NAME (decl)))
3301     warning (OPT_Wmain, "%q+D is usually a function", decl);
3302
3303   if (initialized)
3304     /* Is it valid for this decl to have an initializer at all?
3305        If not, set INITIALIZED to zero, which will indirectly
3306        tell 'finish_decl' to ignore the initializer once it is parsed.  */
3307     switch (TREE_CODE (decl))
3308       {
3309       case TYPE_DECL:
3310         error ("typedef %qD is initialized (use __typeof__ instead)", decl);
3311         initialized = 0;
3312         break;
3313
3314       case FUNCTION_DECL:
3315         error ("function %qD is initialized like a variable", decl);
3316         initialized = 0;
3317         break;
3318
3319       case PARM_DECL:
3320         /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE.  */
3321         error ("parameter %qD is initialized", decl);
3322         initialized = 0;
3323         break;
3324
3325       default:
3326         /* Don't allow initializations for incomplete types except for
3327            arrays which might be completed by the initialization.  */
3328
3329         /* This can happen if the array size is an undefined macro.
3330            We already gave a warning, so we don't need another one.  */
3331         if (TREE_TYPE (decl) == error_mark_node)
3332           initialized = 0;
3333         else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
3334           {
3335             /* A complete type is ok if size is fixed.  */
3336
3337             if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
3338                 || C_DECL_VARIABLE_SIZE (decl))
3339               {
3340                 error ("variable-sized object may not be initialized");
3341                 initialized = 0;
3342               }
3343           }
3344         else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
3345           {
3346             error ("variable %qD has initializer but incomplete type", decl);
3347             initialized = 0;
3348           }
3349         else if (C_DECL_VARIABLE_SIZE (decl))
3350           {
3351             /* Although C99 is unclear about whether incomplete arrays
3352                of VLAs themselves count as VLAs, it does not make
3353                sense to permit them to be initialized given that
3354                ordinary VLAs may not be initialized.  */
3355             error ("variable-sized object may not be initialized");
3356             initialized = 0;
3357           }
3358       }
3359
3360   if (initialized)
3361     {
3362       if (current_scope == file_scope)
3363         TREE_STATIC (decl) = 1;
3364
3365       /* Tell 'pushdecl' this is an initialized decl
3366          even though we don't yet have the initializer expression.
3367          Also tell 'finish_decl' it may store the real initializer.  */
3368       DECL_INITIAL (decl) = error_mark_node;
3369     }
3370
3371   /* If this is a function declaration, write a record describing it to the
3372      prototypes file (if requested).  */
3373
3374   if (TREE_CODE (decl) == FUNCTION_DECL)
3375     gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
3376
3377   /* ANSI specifies that a tentative definition which is not merged with
3378      a non-tentative definition behaves exactly like a definition with an
3379      initializer equal to zero.  (Section 3.7.2)
3380
3381      -fno-common gives strict ANSI behavior, though this tends to break
3382      a large body of code that grew up without this rule.
3383
3384      Thread-local variables are never common, since there's no entrenched
3385      body of code to break, and it allows more efficient variable references
3386      in the presence of dynamic linking.  */
3387
3388   if (TREE_CODE (decl) == VAR_DECL
3389       && !initialized
3390       && TREE_PUBLIC (decl)
3391       && !DECL_THREAD_LOCAL_P (decl)
3392       && !flag_no_common)
3393     DECL_COMMON (decl) = 1;
3394
3395   /* Set attributes here so if duplicate decl, will have proper attributes.  */
3396   decl_attributes (&decl, attributes, 0);
3397
3398   /* Handle gnu_inline attribute.  */
3399   if (declspecs->inline_p
3400       && !flag_gnu89_inline
3401       && TREE_CODE (decl) == FUNCTION_DECL
3402       && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl)))
3403     {
3404       if (declspecs->storage_class == csc_auto && current_scope != file_scope)
3405         ;
3406       else if (declspecs->storage_class != csc_static)
3407         DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
3408     }
3409
3410   if (TREE_CODE (decl) == FUNCTION_DECL
3411       && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
3412     {
3413       struct c_declarator *ce = declarator;
3414
3415       if (ce->kind == cdk_pointer)
3416         ce = declarator->declarator;
3417       if (ce->kind == cdk_function)
3418         {
3419           tree args = ce->u.arg_info->parms;
3420           for (; args; args = TREE_CHAIN (args))
3421             {
3422               tree type = TREE_TYPE (args);
3423               if (type && INTEGRAL_TYPE_P (type)
3424                   && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
3425                 DECL_ARG_TYPE (args) = integer_type_node;
3426             }
3427         }
3428     }
3429
3430   if (TREE_CODE (decl) == FUNCTION_DECL
3431       && DECL_DECLARED_INLINE_P (decl)
3432       && DECL_UNINLINABLE (decl)
3433       && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
3434     warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
3435              decl);
3436
3437   /* C99 6.7.4p3: An inline definition of a function with external
3438      linkage shall not contain a definition of a modifiable object
3439      with static storage duration...  */
3440   if (TREE_CODE (decl) == VAR_DECL
3441       && current_scope != file_scope
3442       && TREE_STATIC (decl)
3443       && !TREE_READONLY (decl)
3444       && DECL_DECLARED_INLINE_P (current_function_decl)
3445       && DECL_EXTERNAL (current_function_decl))
3446     pedwarn ("%q+D is static but declared in inline function %qD "
3447              "which is not static", decl, current_function_decl);
3448
3449   /* Add this decl to the current scope.
3450      TEM may equal DECL or it may be a previous decl of the same name.  */
3451   tem = pushdecl (decl);
3452
3453   if (initialized && DECL_EXTERNAL (tem))
3454     {
3455       DECL_EXTERNAL (tem) = 0;
3456       TREE_STATIC (tem) = 1;
3457     }
3458
3459   return tem;
3460 }
3461
3462 /* Initialize EH if not initialized yet and exceptions are enabled.  */
3463
3464 void
3465 c_maybe_initialize_eh (void)
3466 {
3467   if (!flag_exceptions || c_eh_initialized_p)
3468     return;
3469
3470   c_eh_initialized_p = true;
3471   eh_personality_libfunc
3472     = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3473                         ? "__gcc_personality_sj0"
3474                         : "__gcc_personality_v0");
3475   default_init_unwind_resume_libfunc ();
3476   using_eh_for_cleanups ();
3477 }
3478
3479 /* APPLE LOCAL begin radar 5932809 - copyable byref blocks (C++ cr) */
3480 #define BLOCK_ALIGN_MAX 18
3481 static tree block_byref_id_object_copy[BLOCK_BYREF_CURRENT_MAX*(BLOCK_ALIGN_MAX+1)];
3482 static tree block_byref_id_object_dispose[BLOCK_BYREF_CURRENT_MAX*(BLOCK_ALIGN_MAX+1)];
3483
3484 /**
3485  This routine builds:
3486
3487  void __Block_byref_id_object_copy(struct Block_byref_id_object *dst,
3488                                    struct Block_byref_id_object *src) {
3489    _Block_object_assign (&_dest->object, _src->object, BLOCK_FIELD_IS_OBJECT[|BLOCK_FIELD_IS_WEAK]) // objects
3490    _Block_object_assign(&_dest->object, _src->object, BLOCK_FIELD_IS_BLOCK[|BLOCK_FIELD_IS_WEAK])  // blocks
3491  }  */
3492 static void
3493 synth_block_byref_id_object_copy_func (int flag, int kind)
3494 {
3495   tree stmt, fnbody;
3496   tree dst_arg, src_arg;
3497   tree dst_obj, src_obj;
3498   struct c_arg_info * arg_info;
3499   tree call_exp;
3500   gcc_assert (block_byref_id_object_copy[kind]);
3501   /* Set up: (void* _dest, void*_src) parameters. */
3502   dst_arg = build_decl (PARM_DECL, get_identifier ("_dst"),
3503                          ptr_type_node);
3504   TREE_USED (dst_arg) = 1;
3505   DECL_ARG_TYPE (dst_arg) = ptr_type_node;
3506   src_arg = build_decl (PARM_DECL, get_identifier ("_src"),
3507                          ptr_type_node);
3508   TREE_USED (src_arg) = 1;
3509   DECL_ARG_TYPE (src_arg) = ptr_type_node;
3510   arg_info = xcalloc (1, sizeof (struct c_arg_info));
3511   TREE_CHAIN (dst_arg) = src_arg;
3512   arg_info->parms = dst_arg;
3513   arg_info->types = tree_cons (NULL_TREE, ptr_type_node,
3514                                 tree_cons (NULL_TREE,
3515                                            ptr_type_node,
3516                                            NULL_TREE));
3517   /* function header synthesis. */
3518   push_function_context ();
3519   start_block_helper_function (block_byref_id_object_copy[kind]);
3520   store_parm_decls_from (arg_info);
3521
3522   /* Body of the function. */
3523   stmt = c_begin_compound_stmt (true);
3524   /* Build dst->object */
3525   dst_obj = build_indirect_object_id_exp (dst_arg);
3526
3527
3528   /* src_obj is: _src->object. */
3529   src_obj = build_indirect_object_id_exp (src_arg);
3530
3531   /* APPLE LOCAL begin radar 6180456 */
3532   /* _Block_object_assign (&_dest->object, _src->object, BLOCK_FIELD_IS_OBJECT) or :
3533      _Block_object_assign (&_dest->object, _src->object, BLOCK_FIELD_IS_BLOCK) */
3534   /* APPLE LOCAL begin radar 6573923 */
3535   /* Also add the new flag when calling _Block_object_dispose
3536      from byref dispose helper. */
3537   flag |= BLOCK_BYREF_CALLER;
3538  /* APPLE LOCAL end radar 6573923 */
3539   call_exp = build_block_object_assign_call_exp (build_fold_addr_expr (dst_obj), src_obj, flag);
3540   add_stmt (call_exp);
3541   /* APPLE LOCAL end radar 6180456 */
3542
3543   fnbody = c_end_compound_stmt (stmt, true);
3544   add_stmt (fnbody);
3545   finish_function ();
3546   pop_function_context ();
3547   free (arg_info);
3548 }
3549
3550 /**
3551   This routine builds:
3552
3553   void __Block_byref_id_object_dispose(struct Block_byref_id_object *_src) {
3554     _Block_object_dispose(_src->object, BLOCK_FIELD_IS_OBJECT[|BLOCK_FIELD_IS_WEAK]) // object
3555     _Block_object_dispose(_src->object, BLOCK_FIELD_IS_BLOCK[|BLOCK_FIELD_IS_WEAK]) // block
3556   }  */
3557 static void
3558 synth_block_byref_id_object_dispose_func (int flag, int kind)
3559 {
3560   tree stmt, fnbody;
3561   tree src_arg, src_obj, rel_exp;
3562   struct c_arg_info * arg_info;
3563   gcc_assert (block_byref_id_object_dispose[kind]);
3564   /* Set up: (void *_src) parameter. */
3565   src_arg = build_decl (PARM_DECL, get_identifier ("_src"),
3566                          ptr_type_node);
3567   TREE_USED (src_arg) = 1;
3568   DECL_ARG_TYPE (src_arg) = ptr_type_node;
3569   arg_info = xcalloc (1, sizeof (struct c_arg_info));
3570   arg_info->parms = src_arg;
3571   arg_info->types = tree_cons (NULL_TREE, ptr_type_node,
3572                                 NULL_TREE);
3573   /* function header synthesis. */
3574   push_function_context ();
3575   start_block_helper_function (block_byref_id_object_dispose[kind]);
3576   store_parm_decls_from (arg_info);
3577
3578   /* Body of the function. */
3579   stmt = c_begin_compound_stmt (true);
3580   src_obj = build_indirect_object_id_exp (src_arg);
3581
3582   /* APPLE LOCAL begin radar 6180456 */
3583   /* _Block_object_dispose(_src->object, BLOCK_FIELD_IS_OBJECT) : or
3584      _Block_object_dispose(_src->object, BLOCK_FIELD_IS_BLOCK) */
3585   /* APPLE LOCAL begin radar 6573923 */
3586   /* Also add the new flag when calling _Block_object_dispose
3587      from byref dispose helper. */
3588   flag |= BLOCK_BYREF_CALLER;
3589   /* APPLE LOCAL end radar 6573923 */
3590   rel_exp = build_block_object_dispose_call_exp (src_obj, flag);
3591   /* APPLE LOCAL end radar 6180456 */
3592   add_stmt (rel_exp);
3593
3594   fnbody = c_end_compound_stmt (stmt, true);
3595   add_stmt (fnbody);
3596   finish_function ();
3597   pop_function_context ();
3598   free (arg_info);
3599 }
3600
3601 /* new_block_byref_decl - This routine changes a 'typex x' declared variable into:
3602
3603   struct __Block_byref_x {
3604     // APPLE LOCAL radar 6244520
3605     void *__isa;                        // NULL for everything except __weak pointers
3606     struct Block_byref_x *__forwarding;
3607     int32_t __flags;
3608     int32_t __size;
3609     void *__ByrefKeepFuncPtr;    // Only if variable is __block ObjC object
3610     void *__ByrefDestroyFuncPtr; // Only if variable is __block ObjC object
3611     typex x;
3612   } x;
3613 */
3614
3615 static tree
3616 new_block_byref_decl (tree decl)
3617 {
3618   static int unique_count;
3619   /* APPLE LOCAL radar 5847976 */
3620   int save_flag_objc_gc;
3621   tree Block_byref_type;
3622   tree field_decl_chain, field_decl;
3623   const char *prefix = "__Block_byref_";
3624   char *string = alloca (strlen (IDENTIFIER_POINTER (DECL_NAME (decl))) +
3625                           strlen (prefix) + 8 /* to hold the count */);
3626
3627   sprintf (string, "%s%d_%s", prefix, ++unique_count,
3628             IDENTIFIER_POINTER (DECL_NAME (decl)));
3629
3630   push_to_top_level ();
3631   Block_byref_type = start_struct (RECORD_TYPE, get_identifier (string));
3632
3633   /* APPLE LOCAL begin radar 6244520 */
3634   /* void *__isa; */
3635   field_decl = build_decl (FIELD_DECL, get_identifier ("__isa"), ptr_type_node);
3636   field_decl_chain = field_decl;
3637   /* APPLE LOCAL end radar 6244520 */
3638
3639   /* struct Block_byref_x *__forwarding; */
3640   field_decl = build_decl (FIELD_DECL, get_identifier ("__forwarding"),
3641                             build_pointer_type (Block_byref_type));
3642   /* APPLE LOCAL radar 6244520 */
3643   chainon (field_decl_chain, field_decl);
3644
3645   /* int32_t __flags; */
3646   field_decl = build_decl (FIELD_DECL, get_identifier ("__flags"),
3647                             unsigned_type_node);
3648   chainon (field_decl_chain, field_decl);
3649
3650   /* int32_t __size; */
3651   field_decl = build_decl (FIELD_DECL, get_identifier ("__size"),
3652                             unsigned_type_node);
3653   chainon (field_decl_chain, field_decl);
3654
3655   if (COPYABLE_BYREF_LOCAL_NONPOD (decl))
3656     {
3657       /* void *__ByrefKeepFuncPtr; */
3658       field_decl = build_decl (FIELD_DECL, get_identifier ("__ByrefKeepFuncPtr"),
3659                                ptr_type_node);
3660       chainon (field_decl_chain, field_decl);
3661
3662       /* void *__ByrefDestroyFuncPtr; */
3663       field_decl = build_decl (FIELD_DECL, get_identifier ("__ByrefDestroyFuncPtr"),
3664                                ptr_type_node);
3665       chainon (field_decl_chain, field_decl);
3666   }
3667
3668   /* typex x; */
3669   field_decl = build_decl (FIELD_DECL, DECL_NAME (decl), TREE_TYPE (decl));
3670   chainon (field_decl_chain, field_decl);
3671
3672   pop_from_top_level ();
3673   /* APPLE LOCAL begin radar 5847976 */
3674   /* Hack so we don't issue warning on a field_decl having __weak attribute */
3675   save_flag_objc_gc = flag_objc_gc;
3676   flag_objc_gc = 0;
3677   finish_struct (Block_byref_type, field_decl_chain, NULL_TREE);
3678   flag_objc_gc = save_flag_objc_gc;
3679   /* APPLE LOCAL end radar 5847976 */
3680
3681   TREE_TYPE (decl) = Block_byref_type;
3682   /* Force layout_decl to recompute these fields. */
3683   DECL_SIZE (decl) = DECL_SIZE_UNIT (decl) = 0;
3684   layout_decl (decl, 0);
3685   return decl;
3686 }
3687
3688 /* init_byref_decl - This routine builds the initializer for the __Block_byref_x
3689    type in the form of:
3690    { NULL, &x, 0, sizeof(struct __Block_byref_x), initializer-expr};
3691
3692    or:
3693    { NULL, &x, 0, sizeof(struct __Block_byref_x)};
3694    when INIT is NULL_TREE
3695
3696    For __block ObjC objects, it also adds "byref_keep" and "byref_destroy"
3697    Funtion pointers. So the most general initializers would be:
3698
3699    { NULL, &x, 0, sizeof(struct __Block_byref_x), &byref_keep, &byref_destroy,
3700      &initializer-expr};
3701 */
3702 static tree
3703 /* APPLE LOCAL radar 5847976 */
3704 init_byref_decl (tree decl, tree init, int flag)
3705 {
3706   tree initlist;
3707   tree block_byref_type = TREE_TYPE (decl);
3708   int size = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (block_byref_type));
3709   unsigned flags = 0;
3710   tree fields;
3711
3712   if (COPYABLE_BYREF_LOCAL_NONPOD (decl))
3713     flags = BLOCK_HAS_COPY_DISPOSE;
3714
3715   fields = TYPE_FIELDS (block_byref_type);
3716   /* APPLE LOCAL begin radar 6244520 */
3717   /* APPLE LOCAL begin radar 5847976 */
3718   initlist = tree_cons (fields, fold_convert (ptr_type_node, ((flag & BLOCK_FIELD_IS_WEAK) != 0) ? integer_one_node 
3719                                                                                 : integer_zero_node), 0);
3720   /* APPLE LOCAL end radar 5847976 */
3721   fields = TREE_CHAIN (fields);
3722
3723   initlist = tree_cons (fields,
3724                          build_unary_op (ADDR_EXPR, decl, 0), initlist);
3725    /* APPLE LOCAL end radar 6244520 */
3726   fields = TREE_CHAIN (fields);
3727
3728   initlist = tree_cons (fields, build_int_cst (TREE_TYPE (fields), flags),
3729                          initlist);
3730   fields = TREE_CHAIN (fields);
3731   initlist = tree_cons (fields, build_int_cst (TREE_TYPE (fields), size),
3732                          initlist);
3733   fields = TREE_CHAIN (fields);
3734
3735   if (COPYABLE_BYREF_LOCAL_NONPOD (decl))
3736     {
3737       char name[64];
3738       int align = exact_log2 ((DECL_ALIGN (decl)+TYPE_ALIGN (ptr_type_node)-1) / TYPE_ALIGN (ptr_type_node));
3739       int kind;
3740       if (align == -1 || align > BLOCK_ALIGN_MAX) {
3741         error ("invalid alignment for __block variable");
3742         kind = 0;
3743       } else
3744         kind = align*BLOCK_BYREF_CURRENT_MAX + flag;
3745       /* Add &__Block_byref_id_object_copy, &__Block_byref_id_object_dispose
3746          initializers. */
3747       if (!block_byref_id_object_copy[kind])
3748         {
3749           /* Build a void __Block_byref_id_object_copy(void*, void*) type. */
3750           tree func_type =
3751             build_function_type (void_type_node,
3752                                  tree_cons (NULL_TREE, ptr_type_node,
3753                                             tree_cons (NULL_TREE, ptr_type_node,
3754                                                        void_list_node)));
3755           sprintf (name, "__Block_byref_id_object_copy%d", kind);
3756           block_byref_id_object_copy[kind] = build_helper_func_decl (get_identifier (name),
3757                                                                      func_type);
3758           /* Synthesize function definition. */
3759           synth_block_byref_id_object_copy_func (flag, kind);
3760         }
3761       initlist = tree_cons (fields,
3762                             build_fold_addr_expr (block_byref_id_object_copy[kind]),
3763                             initlist);
3764       fields = TREE_CHAIN (fields);
3765
3766       if (!block_byref_id_object_dispose[kind])
3767         {
3768           /* Synthesize void __Block_byref_id_object_dispose (void*) and
3769              build &__Block_byref_id_object_dispose. */
3770           tree func_type =
3771             build_function_type (void_type_node,
3772                                  tree_cons (NULL_TREE, ptr_type_node, void_list_node));
3773           sprintf (name, "__Block_byref_id_object_dispose%d", kind);
3774           block_byref_id_object_dispose[kind] = build_helper_func_decl (get_identifier (name),
3775                                                                         func_type);
3776           /* Synthesize function definition. */
3777           synth_block_byref_id_object_dispose_func (flag, kind);
3778         }
3779       initlist = tree_cons (fields,
3780                             build_fold_addr_expr (block_byref_id_object_dispose[kind]),
3781                             initlist);
3782       fields = TREE_CHAIN (fields);
3783     }
3784
3785   if (init)
3786     {
3787       init = do_digest_init (TREE_TYPE (fields), init);
3788       initlist = tree_cons (fields, init, initlist);
3789     }
3790   init =  build_constructor_from_list (block_byref_type, nreverse (initlist));
3791   return init;
3792 }
3793 /* APPLE LOCAL end radar 5932809 - copyable byref blocks (C++ cr) */
3794
3795 /* Finish processing of a declaration;
3796    install its initial value.
3797    If the length of an array type is not known before,
3798    it must be determined now, from the initial value, or it is an error.  */
3799
3800 void
3801 finish_decl (tree decl, tree init, tree asmspec_tree)
3802 {
3803   tree type;
3804   int was_incomplete = (DECL_SIZE (decl) == 0);
3805   const char *asmspec = 0;
3806
3807   /* If a name was specified, get the string.  */
3808   if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
3809       && DECL_FILE_SCOPE_P (decl))
3810     asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
3811   if (asmspec_tree)
3812     asmspec = TREE_STRING_POINTER (asmspec_tree);
3813
3814   /* If `start_decl' didn't like having an initialization, ignore it now.  */
3815   if (init != 0 && DECL_INITIAL (decl) == 0)
3816     init = 0;
3817
3818   /* Don't crash if parm is initialized.  */
3819   if (TREE_CODE (decl) == PARM_DECL)
3820     init = 0;
3821   /* APPLE LOCAL begin radar 5932809 - copyable byref blocks (C++ cq) */
3822   /* We build a new type for each local variable declared as __block
3823      and initialize it to a list of initializers. */
3824   else if (TREE_CODE (decl) == VAR_DECL && COPYABLE_BYREF_LOCAL_VAR (decl))
3825     {
3826       if (DECL_EXTERNAL (decl) || TREE_STATIC (decl))
3827         {
3828           error ("__block attribute on %q+D not allowed, only allowed on local variables", decl);
3829           COPYABLE_BYREF_LOCAL_VAR (decl) = 0;
3830           COPYABLE_BYREF_LOCAL_NONPOD (decl) = 0;
3831         }
3832       else
3833         {
3834            int flag = 0;
3835            if (objc_is_gcable_type (TREE_TYPE (decl)) == -1)
3836             flag = BLOCK_FIELD_IS_WEAK;
3837           if (block_requires_copying (decl))
3838             {
3839               if (TREE_CODE (TREE_TYPE (decl)) == BLOCK_POINTER_TYPE)
3840                 flag |= BLOCK_FIELD_IS_BLOCK;
3841               else 
3842                 flag |= BLOCK_FIELD_IS_OBJECT;
3843             }
3844           decl = new_block_byref_decl (decl);
3845           /* APPLE LOCAL begin radar 6289031 */
3846 #if 0
3847           if (! flag_objc_gc_only)
3848 #endif
3849             {
3850                push_cleanup (decl, build_block_byref_release_exp (decl), false);
3851             }
3852           /* APPLE LOCAL end radar 6289031 */
3853            /* APPLE LOCAL begin radar 5847976 */
3854           COPYABLE_WEAK_BLOCK (decl) = ((flag & BLOCK_FIELD_IS_WEAK) != 0);
3855           init = init_byref_decl (decl, init, flag);
3856            /* APPLE LOCAL end radar 5847976 */
3857         }
3858     }
3859   /* APPLE LOCAL end radar 5932809 - copyable byref blocks (C++ cq) */
3860
3861   if (init)
3862     store_init_value (decl, init);
3863
3864   if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
3865                             || TREE_CODE (decl) == FUNCTION_DECL
3866                             || TREE_CODE (decl) == FIELD_DECL))
3867     objc_check_decl (decl);
3868
3869   type = TREE_TYPE (decl);
3870
3871   /* Deduce size of array from initialization, if not already known.  */
3872   if (TREE_CODE (type) == ARRAY_TYPE
3873       && TYPE_DOMAIN (type) == 0
3874       && TREE_CODE (decl) != TYPE_DECL)
3875     {
3876       bool do_default
3877         = (TREE_STATIC (decl)
3878            /* Even if pedantic, an external linkage array
3879               may have incomplete type at first.  */
3880            ? pedantic && !TREE_PUBLIC (decl)
3881            : !DECL_EXTERNAL (decl));
3882       int failure
3883         = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
3884                                do_default);
3885
3886       /* Get the completed type made by complete_array_type.  */
3887       type = TREE_TYPE (decl);
3888
3889       switch (failure)
3890         {
3891         case 1:
3892           error ("initializer fails to determine size of %q+D", decl);
3893           break;
3894
3895         case 2:
3896           if (do_default)
3897             error ("array size missing in %q+D", decl);
3898           /* If a `static' var's size isn't known,
3899              make it extern as well as static, so it does not get
3900              allocated.
3901              If it is not `static', then do not mark extern;
3902              finish_incomplete_decl will give it a default size
3903              and it will get allocated.  */
3904           else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
3905             DECL_EXTERNAL (decl) = 1;
3906           break;
3907
3908         case 3:
3909           error ("zero or negative size array %q+D", decl);
3910           break;
3911
3912         case 0:
3913           /* For global variables, update the copy of the type that
3914              exists in the binding.  */
3915           if (TREE_PUBLIC (decl))
3916             {
3917               struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
3918               while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
3919                 b_ext = b_ext->shadowed;
3920               if (b_ext)
3921                 {
3922                   if (b_ext->type)
3923                     b_ext->type = composite_type (b_ext->type, type);
3924                   else
3925                     b_ext->type = type;
3926                 }
3927             }
3928           break;
3929
3930         default:
3931           gcc_unreachable ();
3932         }
3933
3934       if (DECL_INITIAL (decl))
3935         TREE_TYPE (DECL_INITIAL (decl)) = type;
3936
3937       layout_decl (decl, 0);
3938     }
3939
3940   if (TREE_CODE (decl) == VAR_DECL)
3941     {
3942       if (init && TREE_CODE (init) == CONSTRUCTOR)
3943         add_flexible_array_elts_to_size (decl, init);
3944
3945       if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
3946           && COMPLETE_TYPE_P (TREE_TYPE (decl)))
3947         layout_decl (decl, 0);
3948
3949       if (DECL_SIZE (decl) == 0
3950           /* Don't give an error if we already gave one earlier.  */
3951           && TREE_TYPE (decl) != error_mark_node
3952           && (TREE_STATIC (decl)
3953               /* A static variable with an incomplete type
3954                  is an error if it is initialized.
3955                  Also if it is not file scope.
3956                  Otherwise, let it through, but if it is not `extern'
3957                  then it may cause an error message later.  */
3958               ? (DECL_INITIAL (decl) != 0
3959                  || !DECL_FILE_SCOPE_P (decl))
3960               /* An automatic variable with an incomplete type
3961                  is an error.  */
3962               : !DECL_EXTERNAL (decl)))
3963          {
3964            error ("storage size of %q+D isn%'t known", decl);
3965            TREE_TYPE (decl) = error_mark_node;
3966          }
3967
3968       if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
3969           && DECL_SIZE (decl) != 0)
3970         {
3971           if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
3972             constant_expression_warning (DECL_SIZE (decl));
3973           else
3974             error ("storage size of %q+D isn%'t constant", decl);
3975         }
3976
3977       if (TREE_USED (type))
3978         TREE_USED (decl) = 1;
3979     }
3980
3981   /* If this is a function and an assembler name is specified, reset DECL_RTL
3982      so we can give it its new name.  Also, update built_in_decls if it
3983      was a normal built-in.  */
3984   if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
3985     {
3986       if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
3987         set_builtin_user_assembler_name (decl, asmspec);
3988       set_user_assembler_name (decl, asmspec);
3989     }
3990
3991   /* If #pragma weak was used, mark the decl weak now.  */
3992   maybe_apply_pragma_weak (decl);
3993
3994   /* Output the assembler code and/or RTL code for variables and functions,
3995      unless the type is an undefined structure or union.
3996      If not, it will get done when the type is completed.  */
3997
3998   if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3999     {
4000       /* Determine the ELF visibility.  */
4001       if (TREE_PUBLIC (decl))
4002         c_determine_visibility (decl);
4003
4004       /* This is a no-op in c-lang.c or something real in objc-act.c.  */
4005       if (c_dialect_objc ())
4006         objc_check_decl (decl);
4007
4008       if (asmspec)
4009         {
4010           /* If this is not a static variable, issue a warning.
4011              It doesn't make any sense to give an ASMSPEC for an
4012              ordinary, non-register local variable.  Historically,
4013              GCC has accepted -- but ignored -- the ASMSPEC in
4014              this case.  */
4015           if (!DECL_FILE_SCOPE_P (decl)
4016               && TREE_CODE (decl) == VAR_DECL
4017               && !C_DECL_REGISTER (decl)
4018               && !TREE_STATIC (decl))
4019             warning (0, "ignoring asm-specifier for non-static local "
4020                      "variable %q+D", decl);
4021           else
4022             set_user_assembler_name (decl, asmspec);
4023         }
4024
4025       if (DECL_FILE_SCOPE_P (decl))
4026         {
4027           if (DECL_INITIAL (decl) == NULL_TREE
4028               || DECL_INITIAL (decl) == error_mark_node)
4029             /* Don't output anything
4030                when a tentative file-scope definition is seen.
4031                But at end of compilation, do output code for them.  */
4032             DECL_DEFER_OUTPUT (decl) = 1;
4033           rest_of_decl_compilation (decl, true, 0);
4034         }
4035       else
4036         {
4037           /* In conjunction with an ASMSPEC, the `register'
4038              keyword indicates that we should place the variable
4039              in a particular register.  */
4040           if (asmspec && C_DECL_REGISTER (decl))
4041             {
4042               DECL_HARD_REGISTER (decl) = 1;
4043               /* This cannot be done for a structure with volatile
4044                  fields, on which DECL_REGISTER will have been
4045                  reset.  */
4046               if (!DECL_REGISTER (decl))
4047                 error ("cannot put object with volatile field into register");
4048             }
4049
4050           if (TREE_CODE (decl) != FUNCTION_DECL)
4051             {
4052               /* If we're building a variable sized type, and we might be
4053                  reachable other than via the top of the current binding
4054                  level, then create a new BIND_EXPR so that we deallocate
4055                  the object at the right time.  */
4056               /* Note that DECL_SIZE can be null due to errors.  */
4057               if (DECL_SIZE (decl)
4058                   && !TREE_CONSTANT (DECL_SIZE (decl))
4059                   && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
4060                 {
4061                   tree bind;
4062                   bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
4063                   TREE_SIDE_EFFECTS (bind) = 1;
4064                   add_stmt (bind);
4065                   BIND_EXPR_BODY (bind) = push_stmt_list ();
4066                 }
4067               add_stmt (build_stmt (DECL_EXPR, decl));
4068             }
4069         }
4070
4071
4072       if (!DECL_FILE_SCOPE_P (decl))
4073         {
4074           /* Recompute the RTL of a local array now
4075              if it used to be an incomplete type.  */
4076           if (was_incomplete
4077               && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
4078             {
4079               /* If we used it already as memory, it must stay in memory.  */
4080               TREE_ADDRESSABLE (decl) = TREE_USED (decl);
4081               /* If it's still incomplete now, no init will save it.  */
4082               if (DECL_SIZE (decl) == 0)
4083                 DECL_INITIAL (decl) = 0;
4084             }
4085         }
4086     }
4087
4088   /* If this was marked 'used', be sure it will be output.  */
4089   if (!flag_unit_at_a_time && lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
4090     mark_decl_referenced (decl);
4091
4092   if (TREE_CODE (decl) == TYPE_DECL)
4093     {
4094       if (!DECL_FILE_SCOPE_P (decl)
4095           && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
4096         add_stmt (build_stmt (DECL_EXPR, decl));
4097
4098       rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
4099     }
4100
4101   /* At the end of a declaration, throw away any variable type sizes
4102      of types defined inside that declaration.  There is no use
4103      computing them in the following function definition.  */
4104   if (current_scope == file_scope)
4105     get_pending_sizes ();
4106
4107   /* Install a cleanup (aka destructor) if one was given.  */
4108   if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
4109     {
4110       tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
4111       if (attr)
4112         {
4113           tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
4114           tree cleanup_decl = lookup_name (cleanup_id);
4115           tree cleanup;
4116
4117           /* Build "cleanup(&decl)" for the destructor.  */
4118           cleanup = build_unary_op (ADDR_EXPR, decl, 0);
4119           cleanup = build_tree_list (NULL_TREE, cleanup);
4120           cleanup = build_function_call (cleanup_decl, cleanup);
4121
4122           /* Don't warn about decl unused; the cleanup uses it.  */
4123           TREE_USED (decl) = 1;
4124           TREE_USED (cleanup_decl) = 1;
4125
4126           /* Initialize EH, if we've been told to do so.  */
4127           c_maybe_initialize_eh ();
4128
4129           push_cleanup (decl, cleanup, false);
4130         }
4131     }
4132 }
4133
4134 /* Given a parsed parameter declaration, decode it into a PARM_DECL.  */
4135
4136 tree
4137 grokparm (const struct c_parm *parm)
4138 {
4139   tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
4140                               NULL);
4141
4142   decl_attributes (&decl, parm->attrs, 0);
4143
4144   return decl;
4145 }
4146
4147 /* Given a parsed parameter declaration, decode it into a PARM_DECL
4148    and push that on the current scope.  */
4149
4150 void
4151 push_parm_decl (const struct c_parm *parm)
4152 {
4153   tree decl;
4154
4155   decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL);
4156   decl_attributes (&decl, parm->attrs, 0);
4157
4158   decl = pushdecl (decl);
4159
4160   finish_decl (decl, NULL_TREE, NULL_TREE);
4161 }
4162
4163 /* Mark all the parameter declarations to date as forward decls.
4164    Also diagnose use of this extension.  */
4165
4166 void
4167 mark_forward_parm_decls (void)
4168 {
4169   struct c_binding *b;
4170
4171   if (pedantic && !current_scope->warned_forward_parm_decls)
4172     {
4173       pedwarn ("ISO C forbids forward parameter declarations");
4174       current_scope->warned_forward_parm_decls = true;
4175     }
4176
4177   for (b = current_scope->bindings; b; b = b->prev)
4178     if (TREE_CODE (b->decl) == PARM_DECL)
4179       TREE_ASM_WRITTEN (b->decl) = 1;
4180 }
4181 \f
4182 /* Build a COMPOUND_LITERAL_EXPR.  TYPE is the type given in the compound
4183    literal, which may be an incomplete array type completed by the
4184    initializer; INIT is a CONSTRUCTOR that initializes the compound
4185    literal.  */
4186
4187 tree
4188 build_compound_literal (tree type, tree init)
4189 {
4190   /* We do not use start_decl here because we have a type, not a declarator;
4191      and do not use finish_decl because the decl should be stored inside
4192      the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR.  */
4193   tree decl;
4194   tree complit;
4195   tree stmt;
4196
4197   if (type == error_mark_node)
4198     return error_mark_node;
4199
4200   decl = build_decl (VAR_DECL, NULL_TREE, type);
4201   DECL_EXTERNAL (decl) = 0;
4202   TREE_PUBLIC (decl) = 0;
4203   TREE_STATIC (decl) = (current_scope == file_scope);
4204   DECL_CONTEXT (decl) = current_function_decl;
4205   TREE_USED (decl) = 1;
4206   TREE_TYPE (decl) = type;
4207   TREE_READONLY (decl) = TYPE_READONLY (type);
4208   store_init_value (decl, init);
4209
4210   if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
4211     {
4212       int failure = complete_array_type (&TREE_TYPE (decl),
4213                                          DECL_INITIAL (decl), true);
4214       gcc_assert (!failure);
4215
4216       type = TREE_TYPE (decl);
4217       TREE_TYPE (DECL_INITIAL (decl)) = type;
4218     }
4219
4220   if (type == error_mark_node || !COMPLETE_TYPE_P (type))
4221     return error_mark_node;
4222
4223   stmt = build_stmt (DECL_EXPR, decl);
4224   complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
4225   TREE_SIDE_EFFECTS (complit) = 1;
4226
4227   layout_decl (decl, 0);
4228
4229   if (TREE_STATIC (decl))
4230     {
4231       /* This decl needs a name for the assembler output.  */
4232       set_compound_literal_name (decl);
4233       DECL_DEFER_OUTPUT (decl) = 1;
4234       DECL_COMDAT (decl) = 1;
4235       DECL_ARTIFICIAL (decl) = 1;
4236       DECL_IGNORED_P (decl) = 1;
4237       pushdecl (decl);
4238       rest_of_decl_compilation (decl, 1, 0);
4239     }
4240
4241   return complit;
4242 }
4243 \f
4244 /* Determine whether TYPE is a structure with a flexible array member,
4245    or a union containing such a structure (possibly recursively).  */
4246
4247 static bool
4248 flexible_array_type_p (tree type)
4249 {
4250   tree x;
4251   switch (TREE_CODE (type))
4252     {
4253     case RECORD_TYPE:
4254       x = TYPE_FIELDS (type);
4255       if (x == NULL_TREE)
4256         return false;
4257       while (TREE_CHAIN (x) != NULL_TREE)
4258         x = TREE_CHAIN (x);
4259       if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
4260           && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
4261           && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
4262           && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
4263         return true;
4264       return false;
4265     case UNION_TYPE:
4266       for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
4267         {
4268           if (flexible_array_type_p (TREE_TYPE (x)))
4269             return true;
4270         }
4271       return false;
4272     default:
4273     return false;
4274   }
4275 }
4276 \f
4277 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
4278    replacing with appropriate values if they are invalid.  */
4279 static void
4280 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
4281 {
4282   tree type_mv;
4283   unsigned int max_width;
4284   unsigned HOST_WIDE_INT w;
4285   const char *name = orig_name ? orig_name: _("<anonymous>");
4286
4287   /* Detect and ignore out of range field width and process valid
4288      field widths.  */
4289   if (!INTEGRAL_TYPE_P (TREE_TYPE (*width))
4290       || TREE_CODE (*width) != INTEGER_CST)
4291     {
4292       error ("bit-field %qs width not an integer constant", name);
4293       *width = integer_one_node;
4294     }
4295   else
4296     {
4297       constant_expression_warning (*width);
4298       if (tree_int_cst_sgn (*width) < 0)
4299         {
4300           error ("negative width in bit-field %qs", name);
4301           *width = integer_one_node;
4302         }
4303       else if (integer_zerop (*width) && orig_name)
4304         {
4305           error ("zero width for bit-field %qs", name);
4306           *width = integer_one_node;
4307         }
4308     }
4309
4310   /* Detect invalid bit-field type.  */
4311   if (TREE_CODE (*type) != INTEGER_TYPE
4312       && TREE_CODE (*type) != BOOLEAN_TYPE
4313       && TREE_CODE (*type) != ENUMERAL_TYPE)
4314     {
4315       error ("bit-field %qs has invalid type", name);
4316       *type = unsigned_type_node;
4317     }
4318
4319   type_mv = TYPE_MAIN_VARIANT (*type);
4320   if (pedantic
4321       && !in_system_header
4322       && type_mv != integer_type_node
4323       && type_mv != unsigned_type_node
4324       && type_mv != boolean_type_node)
4325     pedwarn ("type of bit-field %qs is a GCC extension", name);
4326
4327   if (type_mv == boolean_type_node)
4328     max_width = CHAR_TYPE_SIZE;
4329   else
4330     max_width = TYPE_PRECISION (*type);
4331
4332   if (0 < compare_tree_int (*width, max_width))
4333     {
4334       error ("width of %qs exceeds its type", name);
4335       w = max_width;
4336       *width = build_int_cst (NULL_TREE, w);
4337     }
4338   else
4339     w = tree_low_cst (*width, 1);
4340
4341   if (TREE_CODE (*type) == ENUMERAL_TYPE)
4342     {
4343       struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
4344       if (!lt
4345           || w < min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
4346           || w < min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
4347         warning (0, "%qs is narrower than values of its type", name);
4348     }
4349 }
4350
4351 \f
4352
4353 /* Print warning about variable length array if necessary.  */
4354
4355 static void
4356 warn_variable_length_array (const char *name, tree size)
4357 {
4358   int ped = !flag_isoc99 && pedantic && warn_vla != 0;
4359   int const_size = TREE_CONSTANT (size);
4360
4361   if (ped)
4362     {
4363       if (const_size)
4364         {
4365           if (name)
4366             pedwarn ("ISO C90 forbids array %qs whose size "
4367                      "can%'t be evaluated",
4368                      name);
4369           else
4370             pedwarn ("ISO C90 forbids array whose size "
4371                      "can%'t be evaluated");
4372         }
4373       else
4374         {
4375           if (name) 
4376             pedwarn ("ISO C90 forbids variable length array %qs",
4377                      name);
4378           else
4379             pedwarn ("ISO C90 forbids variable length array");
4380         }
4381     }
4382   else if (warn_vla > 0)
4383     {
4384       if (const_size)
4385         {
4386           if (name)
4387             warning (OPT_Wvla,
4388                      "the size of array %qs can"
4389                      "%'t be evaluated", name);
4390           else
4391             warning (OPT_Wvla,
4392                      "the size of array can %'t be evaluated");
4393         }
4394       else
4395         {
4396           if (name)
4397             warning (OPT_Wvla,
4398                      "variable length array %qs is used",
4399                      name);
4400           else
4401             warning (OPT_Wvla,
4402                      "variable length array is used");
4403         }
4404     }
4405 }
4406
4407 /* Given declspecs and a declarator,
4408    determine the name and type of the object declared
4409    and construct a ..._DECL node for it.
4410    (In one case we can return a ..._TYPE node instead.
4411     For invalid input we sometimes return 0.)
4412
4413    DECLSPECS is a c_declspecs structure for the declaration specifiers.
4414
4415    DECL_CONTEXT says which syntactic context this declaration is in:
4416      NORMAL for most contexts.  Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
4417      FUNCDEF for a function definition.  Like NORMAL but a few different
4418       error messages in each case.  Return value may be zero meaning
4419       this definition is too screwy to try to parse.
4420      PARM for a parameter declaration (either within a function prototype
4421       or before a function body).  Make a PARM_DECL, or return void_type_node.
4422      TYPENAME if for a typename (in a cast or sizeof).
4423       Don't make a DECL node; just return the ..._TYPE node.
4424      FIELD for a struct or union field; make a FIELD_DECL.
4425    INITIALIZED is true if the decl has an initializer.
4426    WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
4427    representing the width of the bit-field.
4428
4429    In the TYPENAME case, DECLARATOR is really an absolute declarator.
4430    It may also be so in the PARM case, for a prototype where the
4431    argument type is specified but not the name.
4432
4433    This function is where the complicated C meanings of `static'
4434    and `extern' are interpreted.  */
4435
4436 static tree
4437 grokdeclarator (const struct c_declarator *declarator,
4438                 struct c_declspecs *declspecs,
4439                 enum decl_context decl_context, bool initialized, tree *width)
4440 {
4441   tree type = declspecs->type;
4442   bool threadp = declspecs->thread_p;
4443   enum c_storage_class storage_class = declspecs->storage_class;
4444   int constp;
4445   int restrictp;
4446   int volatilep;
4447   int type_quals = TYPE_UNQUALIFIED;
4448   const char *name, *orig_name;
4449   tree typedef_type = 0;
4450   bool funcdef_flag = false;
4451   bool funcdef_syntax = false;
4452   int size_varies = 0;
4453   tree decl_attr = declspecs->decl_attr;
4454   int array_ptr_quals = TYPE_UNQUALIFIED;
4455   tree array_ptr_attrs = NULL_TREE;
4456   int array_parm_static = 0;
4457   bool array_parm_vla_unspec_p = false;
4458   tree returned_attrs = NULL_TREE;
4459   bool bitfield = width != NULL;
4460   tree element_type;
4461   struct c_arg_info *arg_info = 0;
4462
4463   if (decl_context == FUNCDEF)
4464     funcdef_flag = true, decl_context = NORMAL;
4465
4466   /* Look inside a declarator for the name being declared
4467      and get it as a string, for an error message.  */
4468   {
4469     const struct c_declarator *decl = declarator;
4470     name = 0;
4471
4472     while (decl)
4473       switch (decl->kind)
4474         {
4475         case cdk_function:
4476         case cdk_array:
4477         case cdk_pointer:
4478          /* APPLE LOCAL radar 5732232 - blocks */
4479         case cdk_block_pointer:
4480           funcdef_syntax = (decl->kind == cdk_function);
4481           decl = decl->declarator;
4482           break;
4483
4484         case cdk_attrs:
4485           decl = decl->declarator;
4486           break;
4487
4488         case cdk_id:
4489           if (decl->u.id)
4490             name = IDENTIFIER_POINTER (decl->u.id);
4491           decl = 0;
4492           break;
4493
4494         default:
4495           gcc_unreachable ();
4496         }
4497     orig_name = name;
4498     if (name == 0)
4499       name = "type name";
4500   }
4501
4502   /* A function definition's declarator must have the form of
4503      a function declarator.  */
4504
4505   if (funcdef_flag && !funcdef_syntax)
4506     return 0;
4507
4508   /* If this looks like a function definition, make it one,
4509      even if it occurs where parms are expected.
4510      Then store_parm_decls will reject it and not use it as a parm.  */
4511   /* APPLE LOCAL begin "unavailable" attribute (radar 2809697) */
4512   if (declspecs->unavailable_p)
4513     error_unavailable_use (declspecs->type);
4514   else
4515   /* APPLE LOCAL end "unavailable" attribute (radar 2809697) */
4516   if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
4517     decl_context = PARM;
4518
4519   if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
4520     warn_deprecated_use (declspecs->type);
4521
4522   if ((decl_context == NORMAL || decl_context == FIELD)
4523       && current_scope == file_scope
4524       && variably_modified_type_p (type, NULL_TREE))
4525     {
4526       error ("variably modified %qs at file scope", name);
4527       type = integer_type_node;
4528     }
4529
4530   typedef_type = type;
4531   size_varies = C_TYPE_VARIABLE_SIZE (type);
4532
4533   /* Diagnose defaulting to "int".  */
4534
4535   if (declspecs->default_int_p && !in_system_header)
4536     {
4537       /* Issue a warning if this is an ISO C 99 program or if
4538          -Wreturn-type and this is a function, or if -Wimplicit;
4539          prefer the former warning since it is more explicit.  */
4540       if ((warn_implicit_int || warn_return_type || flag_isoc99)
4541           && funcdef_flag)
4542         warn_about_return_type = 1;
4543       else if (warn_implicit_int || flag_isoc99)
4544         pedwarn_c99 ("type defaults to %<int%> in declaration of %qs", name);
4545     }
4546
4547   /* Adjust the type if a bit-field is being declared,
4548      -funsigned-bitfields applied and the type is not explicitly
4549      "signed".  */
4550   if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
4551       && TREE_CODE (type) == INTEGER_TYPE)
4552     type = c_common_unsigned_type (type);
4553
4554   /* Figure out the type qualifiers for the declaration.  There are
4555      two ways a declaration can become qualified.  One is something
4556      like `const int i' where the `const' is explicit.  Another is
4557      something like `typedef const int CI; CI i' where the type of the
4558      declaration contains the `const'.  A third possibility is that
4559      there is a type qualifier on the element type of a typedefed
4560      array type, in which case we should extract that qualifier so
4561      that c_apply_type_quals_to_decls receives the full list of
4562      qualifiers to work with (C90 is not entirely clear about whether
4563      duplicate qualifiers should be diagnosed in this case, but it
4564      seems most appropriate to do so).  */
4565   element_type = strip_array_types (type);
4566   constp = declspecs->const_p + TYPE_READONLY (element_type);
4567   restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
4568   volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
4569   if (pedantic && !flag_isoc99)
4570     {
4571       if (constp > 1)
4572         pedwarn ("duplicate %<const%>");
4573       if (restrictp > 1)
4574         pedwarn ("duplicate %<restrict%>");
4575       if (volatilep > 1)
4576         pedwarn ("duplicate %<volatile%>");
4577     }
4578   if (!flag_gen_aux_info && (TYPE_QUALS (element_type)))
4579     type = TYPE_MAIN_VARIANT (type);
4580   type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4581                 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4582                 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4583
4584   /* Warn about storage classes that are invalid for certain
4585      kinds of declarations (parameters, typenames, etc.).  */
4586
4587   if (funcdef_flag
4588       && (threadp
4589           || storage_class == csc_auto
4590           || storage_class == csc_register
4591           || storage_class == csc_typedef))
4592     {
4593       if (storage_class == csc_auto
4594           && (pedantic || current_scope == file_scope))
4595         pedwarn ("function definition declared %<auto%>");
4596       if (storage_class == csc_register)
4597         error ("function definition declared %<register%>");
4598       if (storage_class == csc_typedef)
4599         error ("function definition declared %<typedef%>");
4600       if (threadp)
4601         error ("function definition declared %<__thread%>");
4602       threadp = false;
4603       if (storage_class == csc_auto
4604           || storage_class == csc_register
4605           || storage_class == csc_typedef)
4606         storage_class = csc_none;
4607     }
4608   else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
4609     {
4610       if (decl_context == PARM && storage_class == csc_register)
4611         ;
4612       else
4613         {
4614           switch (decl_context)
4615             {
4616             case FIELD:
4617               error ("storage class specified for structure field %qs",
4618                      name);
4619               break;
4620             case PARM:
4621               error ("storage class specified for parameter %qs", name);
4622               break;
4623             default:
4624               error ("storage class specified for typename");
4625               break;
4626             }
4627           storage_class = csc_none;
4628           threadp = false;
4629         }
4630     }
4631   else if (storage_class == csc_extern
4632            && initialized
4633            && !funcdef_flag)
4634     {
4635       /* 'extern' with initialization is invalid if not at file scope.  */
4636        if (current_scope == file_scope)
4637          {
4638            /* It is fine to have 'extern const' when compiling at C
4639               and C++ intersection.  */
4640            if (!(warn_cxx_compat && constp))
4641              warning (0, "%qs initialized and declared %<extern%>", name);
4642          }
4643       else
4644         error ("%qs has both %<extern%> and initializer", name);
4645     }
4646   else if (current_scope == file_scope)
4647     {
4648       if (storage_class == csc_auto)
4649         error ("file-scope declaration of %qs specifies %<auto%>", name);
4650       if (pedantic && storage_class == csc_register)
4651         pedwarn ("file-scope declaration of %qs specifies %<register%>", name);
4652     }
4653   else
4654     {
4655       if (storage_class == csc_extern && funcdef_flag)
4656         error ("nested function %qs declared %<extern%>", name);
4657       else if (threadp && storage_class == csc_none)
4658         {
4659           error ("function-scope %qs implicitly auto and declared "
4660                  "%<__thread%>",
4661                  name);
4662           threadp = false;
4663         }
4664     }
4665
4666   /* Now figure out the structure of the declarator proper.
4667      Descend through it, creating more complex types, until we reach
4668      the declared identifier (or NULL_TREE, in an absolute declarator).
4669      At each stage we maintain an unqualified version of the type
4670      together with any qualifiers that should be applied to it with
4671      c_build_qualified_type; this way, array types including
4672      multidimensional array types are first built up in unqualified
4673      form and then the qualified form is created with
4674      TYPE_MAIN_VARIANT pointing to the unqualified form.  */
4675
4676   while (declarator && declarator->kind != cdk_id)
4677     {
4678       if (type == error_mark_node)
4679         {
4680           declarator = declarator->declarator;
4681           continue;
4682         }
4683
4684       /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
4685          a cdk_pointer (for *...),
4686          a cdk_function (for ...(...)),
4687          a cdk_attrs (for nested attributes),
4688          or a cdk_id (for the name being declared
4689          or the place in an absolute declarator
4690          where the name was omitted).
4691          For the last case, we have just exited the loop.
4692
4693          At this point, TYPE is the type of elements of an array,
4694          or for a function to return, or for a pointer to point to.
4695          After this sequence of ifs, TYPE is the type of the
4696          array or function or pointer, and DECLARATOR has had its
4697          outermost layer removed.  */
4698
4699       if (array_ptr_quals != TYPE_UNQUALIFIED
4700           || array_ptr_attrs != NULL_TREE
4701           || array_parm_static)
4702         {
4703           /* Only the innermost declarator (making a parameter be of
4704              array type which is converted to pointer type)
4705              may have static or type qualifiers.  */
4706           error ("static or type qualifiers in non-parameter array declarator");
4707           array_ptr_quals = TYPE_UNQUALIFIED;
4708           array_ptr_attrs = NULL_TREE;
4709           array_parm_static = 0;
4710         }
4711
4712       switch (declarator->kind)
4713         {
4714         case cdk_attrs:
4715           {
4716             /* A declarator with embedded attributes.  */
4717             tree attrs = declarator->u.attrs;
4718             const struct c_declarator *inner_decl;
4719             int attr_flags = 0;
4720             declarator = declarator->declarator;
4721             inner_decl = declarator;
4722             while (inner_decl->kind == cdk_attrs)
4723               inner_decl = inner_decl->declarator;
4724             if (inner_decl->kind == cdk_id)
4725               attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
4726             else if (inner_decl->kind == cdk_function)
4727               attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
4728             else if (inner_decl->kind == cdk_array)
4729               attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
4730             returned_attrs = decl_attributes (&type,
4731                                               chainon (returned_attrs, attrs),
4732                                               attr_flags);
4733             break;
4734           }
4735         case cdk_array:
4736           {
4737             tree itype = NULL_TREE;
4738             tree size = declarator->u.array.dimen;
4739             /* The index is a signed object `sizetype' bits wide.  */
4740             tree index_type = c_common_signed_type (sizetype);
4741
4742             array_ptr_quals = declarator->u.array.quals;
4743             array_ptr_attrs = declarator->u.array.attrs;
4744             array_parm_static = declarator->u.array.static_p;
4745             array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
4746
4747             declarator = declarator->declarator;
4748
4749             /* Check for some types that there cannot be arrays of.  */
4750
4751             if (VOID_TYPE_P (type))
4752               {
4753                 error ("declaration of %qs as array of voids", name);
4754                 type = error_mark_node;
4755               }
4756
4757             if (TREE_CODE (type) == FUNCTION_TYPE)
4758               {
4759                 error ("declaration of %qs as array of functions", name);
4760                 type = error_mark_node;
4761               }
4762
4763             if (pedantic && !in_system_header && flexible_array_type_p (type))
4764               pedwarn ("invalid use of structure with flexible array member");
4765
4766             if (size == error_mark_node)
4767               type = error_mark_node;
4768
4769             if (type == error_mark_node)
4770               continue;
4771
4772             /* If size was specified, set ITYPE to a range-type for
4773                that size.  Otherwise, ITYPE remains null.  finish_decl
4774                may figure it out from an initial value.  */
4775
4776             if (size)
4777               {
4778                 /* Strip NON_LVALUE_EXPRs since we aren't using as an
4779                    lvalue.  */
4780                 STRIP_TYPE_NOPS (size);
4781
4782                 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
4783                   {
4784                     error ("size of array %qs has non-integer type", name);
4785                     size = integer_one_node;
4786                   }
4787
4788                 if (pedantic && integer_zerop (size))
4789                   pedwarn ("ISO C forbids zero-size array %qs", name);
4790
4791                 if (TREE_CODE (size) == INTEGER_CST)
4792                   {
4793                     constant_expression_warning (size);
4794                     if (tree_int_cst_sgn (size) < 0)
4795                       {
4796                         error ("size of array %qs is negative", name);
4797                         size = integer_one_node;
4798                       }
4799                   }
4800                 else if ((decl_context == NORMAL || decl_context == FIELD)
4801                          && current_scope == file_scope)
4802                   {
4803                     error ("variably modified %qs at file scope", name);
4804                     size = integer_one_node;
4805                   }
4806                 else
4807                   {
4808                     /* Make sure the array size remains visibly
4809                        nonconstant even if it is (eg) a const variable
4810                        with known value.  */
4811                     size_varies = 1;
4812                     warn_variable_length_array (orig_name, size);
4813                     if (warn_variable_decl)
4814                       warning (0, "variable-sized array %qs", name);
4815                   }
4816
4817                 if (integer_zerop (size))
4818                   {
4819                     /* A zero-length array cannot be represented with
4820                        an unsigned index type, which is what we'll
4821                        get with build_index_type.  Create an
4822                        open-ended range instead.  */
4823                     itype = build_range_type (sizetype, size, NULL_TREE);
4824                   }
4825                 else
4826                   {
4827                     /* Arrange for the SAVE_EXPR on the inside of the
4828                        MINUS_EXPR, which allows the -1 to get folded
4829                        with the +1 that happens when building TYPE_SIZE.  */
4830                     if (size_varies)
4831                       size = variable_size (size);
4832
4833                     /* Compute the maximum valid index, that is, size
4834                        - 1.  Do the calculation in index_type, so that
4835                        if it is a variable the computations will be
4836                        done in the proper mode.  */
4837                     itype = fold_build2 (MINUS_EXPR, index_type,
4838                                          convert (index_type, size),
4839                                          convert (index_type,
4840                                                   size_one_node));
4841
4842                     /* If that overflowed, the array is too big.  ???
4843                        While a size of INT_MAX+1 technically shouldn't
4844                        cause an overflow (because we subtract 1), the
4845                        overflow is recorded during the conversion to
4846                        index_type, before the subtraction.  Handling
4847                        this case seems like an unnecessary
4848                        complication.  */
4849                     if (TREE_CODE (itype) == INTEGER_CST
4850                         && TREE_OVERFLOW (itype))
4851                       {
4852                         error ("size of array %qs is too large", name);
4853                         type = error_mark_node;
4854                         continue;
4855                       }
4856
4857                     itype = build_index_type (itype);
4858                   }
4859               }
4860             else if (decl_context == FIELD)
4861               {
4862                 if (pedantic && !flag_isoc99 && !in_system_header)
4863                   pedwarn ("ISO C90 does not support flexible array members");
4864
4865                 /* ISO C99 Flexible array members are effectively
4866                    identical to GCC's zero-length array extension.  */
4867                 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4868               }
4869             else if (decl_context == PARM)
4870               {
4871                 if (array_parm_vla_unspec_p)
4872                   {
4873                     if (! orig_name)
4874                       {
4875                         /* C99 6.7.5.2p4 */
4876                         error ("%<[*]%> not allowed in other than a declaration");
4877                       }
4878
4879                     itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4880                     size_varies = 1;
4881                   }
4882               }
4883             else if (decl_context == TYPENAME)
4884               {
4885                 if (array_parm_vla_unspec_p)
4886                   {
4887                     /* The error is printed elsewhere.  We use this to
4888                        avoid messing up with incomplete array types of
4889                        the same type, that would otherwise be modified
4890                        below.  */
4891                     itype = build_range_type (sizetype, size_zero_node,
4892                                               NULL_TREE);
4893                   }
4894               }
4895
4896              /* Complain about arrays of incomplete types.  */
4897             if (!COMPLETE_TYPE_P (type))
4898               {
4899                 error ("array type has incomplete element type");
4900                 type = error_mark_node;
4901               }
4902             else
4903             /* When itype is NULL, a shared incomplete array type is
4904                returned for all array of a given type.  Elsewhere we
4905                make sure we don't complete that type before copying
4906                it, but here we want to make sure we don't ever
4907                modify the shared type, so we gcc_assert (itype)
4908                below.  */
4909               type = build_array_type (type, itype);
4910
4911             if (type != error_mark_node)
4912               {
4913                 if (size_varies)
4914                   {
4915                     /* It is ok to modify type here even if itype is
4916                        NULL: if size_varies, we're in a
4917                        multi-dimensional array and the inner type has
4918                        variable size, so the enclosing shared array type
4919                        must too.  */
4920                     if (size && TREE_CODE (size) == INTEGER_CST)
4921                       type
4922                         = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
4923                     C_TYPE_VARIABLE_SIZE (type) = 1;
4924                   }
4925
4926                 /* The GCC extension for zero-length arrays differs from
4927                    ISO flexible array members in that sizeof yields
4928                    zero.  */
4929                 if (size && integer_zerop (size))
4930                   {
4931                     gcc_assert (itype);
4932                     TYPE_SIZE (type) = bitsize_zero_node;
4933                     TYPE_SIZE_UNIT (type) = size_zero_node;
4934                   }
4935                 if (array_parm_vla_unspec_p)
4936                   {
4937                     gcc_assert (itype);
4938                     /* The type is complete.  C99 6.7.5.2p4  */
4939                     TYPE_SIZE (type) = bitsize_zero_node;
4940                     TYPE_SIZE_UNIT (type) = size_zero_node;
4941                   }
4942               }
4943
4944             if (decl_context != PARM
4945                 && (array_ptr_quals != TYPE_UNQUALIFIED
4946                     || array_ptr_attrs != NULL_TREE
4947                     || array_parm_static))
4948               {
4949                 error ("static or type qualifiers in non-parameter array declarator");
4950                 array_ptr_quals = TYPE_UNQUALIFIED;
4951                 array_ptr_attrs = NULL_TREE;
4952                 array_parm_static = 0;
4953               }
4954             break;
4955           }
4956         case cdk_function:
4957           {
4958             /* Say it's a definition only for the declarator closest
4959                to the identifier, apart possibly from some
4960                attributes.  */
4961             bool really_funcdef = false;
4962             tree arg_types;
4963             if (funcdef_flag)
4964               {
4965                 const struct c_declarator *t = declarator->declarator;
4966                 while (t->kind == cdk_attrs)
4967                   t = t->declarator;
4968                 really_funcdef = (t->kind == cdk_id);
4969               }
4970
4971             /* Declaring a function type.  Make sure we have a valid
4972                type for the function to return.  */
4973             if (type == error_mark_node)
4974               continue;
4975
4976             size_varies = 0;
4977
4978             /* Warn about some types functions can't return.  */
4979             if (TREE_CODE (type) == FUNCTION_TYPE)
4980               {
4981                 error ("%qs declared as function returning a function", name);
4982                 type = integer_type_node;
4983               }
4984             if (TREE_CODE (type) == ARRAY_TYPE)
4985               {
4986                 error ("%qs declared as function returning an array", name);
4987                 type = integer_type_node;
4988               }
4989
4990             /* Construct the function type and go to the next
4991                inner layer of declarator.  */
4992             arg_info = declarator->u.arg_info;
4993             arg_types = grokparms (arg_info, really_funcdef);
4994             if (really_funcdef)
4995               put_pending_sizes (arg_info->pending_sizes);
4996
4997             /* Type qualifiers before the return type of the function
4998                qualify the return type, not the function type.  */
4999             if (type_quals)
5000               {
5001                 /* Type qualifiers on a function return type are
5002                    normally permitted by the standard but have no
5003                    effect, so give a warning at -Wreturn-type.
5004                    Qualifiers on a void return type are banned on
5005                    function definitions in ISO C; GCC used to used
5006                    them for noreturn functions.  */
5007                 if (VOID_TYPE_P (type) && really_funcdef)
5008                   pedwarn ("function definition has qualified void return type");
5009                 else
5010                   warning (OPT_Wreturn_type,
5011                            "type qualifiers ignored on function return type");
5012
5013                 type = c_build_qualified_type (type, type_quals);
5014               }
5015             type_quals = TYPE_UNQUALIFIED;
5016
5017             type = build_function_type (type, arg_types);
5018             declarator = declarator->declarator;
5019
5020             /* Set the TYPE_CONTEXTs for each tagged type which is local to
5021                the formal parameter list of this FUNCTION_TYPE to point to
5022                the FUNCTION_TYPE node itself.  */
5023             {
5024               tree link;
5025
5026               for (link = arg_info->tags;
5027                    link;
5028                    link = TREE_CHAIN (link))
5029                 TYPE_CONTEXT (TREE_VALUE (link)) = type;
5030             }
5031             break;
5032           }
5033         case cdk_pointer:
5034           {
5035             /* Merge any constancy or volatility into the target type
5036                for the pointer.  */
5037
5038             if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
5039                 && type_quals)
5040               pedwarn ("ISO C forbids qualified function types");
5041             if (type_quals)
5042               type = c_build_qualified_type (type, type_quals);
5043             size_varies = 0;
5044
5045             /* When the pointed-to type involves components of variable size,
5046                care must be taken to ensure that the size evaluation code is
5047                emitted early enough to dominate all the possible later uses
5048                and late enough for the variables on which it depends to have
5049                been assigned.
5050
5051                This is expected to happen automatically when the pointed-to
5052                type has a name/declaration of it's own, but special attention
5053                is required if the type is anonymous.
5054
5055                We handle the NORMAL and FIELD contexts here by attaching an
5056                artificial TYPE_DECL to such pointed-to type.  This forces the
5057                sizes evaluation at a safe point and ensures it is not deferred
5058                until e.g. within a deeper conditional context.
5059
5060                We expect nothing to be needed here for PARM or TYPENAME.
5061                Pushing a TYPE_DECL at this point for TYPENAME would actually
5062                be incorrect, as we might be in the middle of an expression
5063                with side effects on the pointed-to type size "arguments" prior
5064                to the pointer declaration point and the fake TYPE_DECL in the
5065                enclosing context would force the size evaluation prior to the
5066                side effects.  */
5067
5068             if (!TYPE_NAME (type)
5069                 && (decl_context == NORMAL || decl_context == FIELD)
5070                 && variably_modified_type_p (type, NULL_TREE))
5071               {
5072                 tree decl = build_decl (TYPE_DECL, NULL_TREE, type);
5073                 DECL_ARTIFICIAL (decl) = 1;
5074                 pushdecl (decl);
5075                 finish_decl (decl, NULL_TREE, NULL_TREE);
5076                 TYPE_NAME (type) = decl;
5077               }
5078
5079             type = build_pointer_type (type);
5080
5081             /* Process type qualifiers (such as const or volatile)
5082                that were given inside the `*'.  */
5083             type_quals = declarator->u.pointer_quals;
5084
5085             declarator = declarator->declarator;
5086             break;
5087           }
5088
5089
5090          /* APPLE LOCAL begin radar 5732232 - blocks (C++ cj) */
5091         case cdk_block_pointer:
5092           {
5093             if (TREE_CODE (type) != FUNCTION_TYPE)
5094               {
5095                 error ("block pointer to non-function type is invalid");
5096                 type = error_mark_node;
5097               }
5098             else
5099               {
5100                 type = build_block_pointer_type (type);
5101                 /* APPLE LOCAL begin radar 5814025 (C++ cj) */
5102                 /* Process type qualifiers (such as const or volatile)
5103                    that were given inside the `^'.  */
5104                 type_quals = declarator->u.pointer_quals;
5105                 /* APPLE LOCAL end radar 5814025 (C++ cj) */
5106                 declarator = declarator->declarator;
5107               }
5108             break;
5109           }
5110           /* APPLE LOCAL end radar 5732232 - blocks (C++ cj) */
5111
5112         default:
5113           gcc_unreachable ();
5114         }
5115     }
5116
5117   /* Now TYPE has the actual type, apart from any qualifiers in
5118      TYPE_QUALS.  */
5119
5120   /* Check the type and width of a bit-field.  */
5121   if (bitfield)
5122     check_bitfield_type_and_width (&type, width, orig_name);
5123
5124   /* Did array size calculations overflow?  */
5125
5126   if (TREE_CODE (type) == ARRAY_TYPE
5127       && COMPLETE_TYPE_P (type)
5128       && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
5129       && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
5130     {
5131       error ("size of array %qs is too large", name);
5132       /* If we proceed with the array type as it is, we'll eventually
5133          crash in tree_low_cst().  */
5134       type = error_mark_node;
5135     }
5136
5137   /* APPLE LOCAL begin blocks 6339747 */
5138   if (decl_context == BLOCKDEF)
5139     {
5140       tree decl;
5141       
5142       if (type == error_mark_node)
5143         return error_mark_node;
5144       
5145       if (TREE_CODE (type) != FUNCTION_TYPE)
5146       {
5147         tree arg_types;
5148         
5149         if (TREE_CODE (type) == ARRAY_TYPE)
5150         {
5151                 error ("block declared as returning an array");
5152                 return error_mark_node;
5153         }
5154
5155         arg_info = XOBNEW (&parser_obstack, struct c_arg_info);
5156         arg_info->parms = 0;
5157         arg_info->tags = 0;
5158         arg_info->types = 0;
5159         arg_info->others = 0;
5160         arg_info->pending_sizes = 0;
5161         arg_info->had_vla_unspec = 0;
5162         arg_types = grokparms (arg_info, false);
5163         type_quals = TYPE_UNQUALIFIED;
5164         type = build_function_type (type, arg_types);
5165       }
5166       decl = build_decl (FUNCTION_DECL, NULL_TREE, type);
5167       DECL_ARGUMENTS (decl) = arg_info ? arg_info->parms : NULL_TREE;
5168       return decl;
5169     }
5170   /* APPLE LOCAL end blocks 6339747 */
5171   
5172   /* If this is declaring a typedef name, return a TYPE_DECL.  */
5173
5174   if (storage_class == csc_typedef)
5175     {
5176       tree decl;
5177       if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
5178           && type_quals)
5179         pedwarn ("ISO C forbids qualified function types");
5180       if (type_quals)
5181         type = c_build_qualified_type (type, type_quals);
5182       decl = build_decl (TYPE_DECL, declarator->u.id, type);
5183       if (declspecs->explicit_signed_p)
5184         C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
5185       decl_attributes (&decl, returned_attrs, 0);
5186       if (declspecs->inline_p)
5187         pedwarn ("typedef %q+D declared %<inline%>", decl);
5188       return decl;
5189     }
5190
5191   /* If this is a type name (such as, in a cast or sizeof),
5192      compute the type and return it now.  */
5193
5194   if (decl_context == TYPENAME)
5195     {
5196       /* Note that the grammar rejects storage classes in typenames
5197          and fields.  */
5198       gcc_assert (storage_class == csc_none && !threadp
5199                   && !declspecs->inline_p);
5200       if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
5201           && type_quals)
5202         pedwarn ("ISO C forbids const or volatile function types");
5203       if (type_quals)
5204         type = c_build_qualified_type (type, type_quals);
5205       decl_attributes (&type, returned_attrs, 0);
5206       return type;
5207     }
5208
5209   if (pedantic && decl_context == FIELD
5210       && variably_modified_type_p (type, NULL_TREE))
5211     {
5212       /* C99 6.7.2.1p8 */
5213       pedwarn ("a member of a structure or union cannot have a variably modified type");
5214     }
5215
5216   /* Aside from typedefs and type names (handle above),
5217      `void' at top level (not within pointer)
5218      is allowed only in public variables.
5219      We don't complain about parms either, but that is because
5220      a better error message can be made later.  */
5221
5222   if (VOID_TYPE_P (type) && decl_context != PARM
5223       && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
5224             && (storage_class == csc_extern
5225                 || (current_scope == file_scope
5226                     && !(storage_class == csc_static
5227                          || storage_class == csc_register)))))
5228     {
5229       error ("variable or field %qs declared void", name);
5230       type = integer_type_node;
5231     }
5232
5233   /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
5234      or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE.  */
5235
5236   {
5237     tree decl;
5238
5239     if (decl_context == PARM)
5240       {
5241         tree type_as_written;
5242         tree promoted_type;
5243
5244         /* A parameter declared as an array of T is really a pointer to T.
5245            One declared as a function is really a pointer to a function.  */
5246
5247         if (TREE_CODE (type) == ARRAY_TYPE)
5248           {
5249             /* Transfer const-ness of array into that of type pointed to.  */
5250             type = TREE_TYPE (type);
5251             if (type_quals)
5252               type = c_build_qualified_type (type, type_quals);
5253             type = build_pointer_type (type);
5254             type_quals = array_ptr_quals;
5255
5256             /* We don't yet implement attributes in this context.  */
5257             if (array_ptr_attrs != NULL_TREE)
5258               warning (OPT_Wattributes,
5259                        "attributes in parameter array declarator ignored");
5260
5261             size_varies = 0;
5262           }
5263         else if (TREE_CODE (type) == FUNCTION_TYPE)
5264           {
5265             if (pedantic && type_quals)
5266               pedwarn ("ISO C forbids qualified function types");
5267             if (type_quals)
5268               type = c_build_qualified_type (type, type_quals);
5269             type = build_pointer_type (type);
5270             type_quals = TYPE_UNQUALIFIED;
5271           }
5272         else if (type_quals)
5273           type = c_build_qualified_type (type, type_quals);
5274
5275         type_as_written = type;
5276
5277         decl = build_decl (PARM_DECL, declarator->u.id, type);
5278         if (size_varies)
5279           C_DECL_VARIABLE_SIZE (decl) = 1;
5280
5281         /* Compute the type actually passed in the parmlist,
5282            for the case where there is no prototype.
5283            (For example, shorts and chars are passed as ints.)
5284            When there is a prototype, this is overridden later.  */
5285
5286         if (type == error_mark_node)
5287           promoted_type = type;
5288         else
5289           promoted_type = c_type_promotes_to (type);
5290
5291         DECL_ARG_TYPE (decl) = promoted_type;
5292         if (declspecs->inline_p)
5293           pedwarn ("parameter %q+D declared %<inline%>", decl);
5294       }
5295     else if (decl_context == FIELD)
5296       {
5297         /* Note that the grammar rejects storage classes in typenames
5298            and fields.  */
5299         gcc_assert (storage_class == csc_none && !threadp
5300                     && !declspecs->inline_p);
5301
5302         /* Structure field.  It may not be a function.  */
5303
5304         if (TREE_CODE (type) == FUNCTION_TYPE)
5305           {
5306             error ("field %qs declared as a function", name);
5307             type = build_pointer_type (type);
5308           }
5309         else if (TREE_CODE (type) != ERROR_MARK
5310                  && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
5311           {
5312             error ("field %qs has incomplete type", name);
5313             type = error_mark_node;
5314           }
5315         type = c_build_qualified_type (type, type_quals);
5316         decl = build_decl (FIELD_DECL, declarator->u.id, type);
5317         DECL_NONADDRESSABLE_P (decl) = bitfield;
5318
5319         if (size_varies)
5320           C_DECL_VARIABLE_SIZE (decl) = 1;
5321       }
5322     else if (TREE_CODE (type) == FUNCTION_TYPE)
5323       {
5324         if (storage_class == csc_register || threadp)
5325           {
5326             error ("invalid storage class for function %qs", name);
5327            }
5328         else if (current_scope != file_scope)
5329           {
5330             /* Function declaration not at file scope.  Storage
5331                classes other than `extern' are not allowed, C99
5332                6.7.1p5, and `extern' makes no difference.  However,
5333                GCC allows 'auto', perhaps with 'inline', to support
5334                nested functions.  */
5335             if (storage_class == csc_auto)
5336               {
5337                 if (pedantic)
5338                   pedwarn ("invalid storage class for function %qs", name);
5339               }
5340             else if (storage_class == csc_static)
5341               {
5342                 error ("invalid storage class for function %qs", name);
5343                 if (funcdef_flag)
5344                   storage_class = declspecs->storage_class = csc_none;
5345                 else
5346                   return 0;
5347               }
5348           }
5349
5350         decl = build_decl (FUNCTION_DECL, declarator->u.id, type);
5351         decl = build_decl_attribute_variant (decl, decl_attr);
5352
5353         DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
5354
5355         if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
5356           pedwarn ("ISO C forbids qualified function types");
5357
5358         /* GNU C interprets a volatile-qualified function type to indicate
5359            that the function does not return.  */
5360         if ((type_quals & TYPE_QUAL_VOLATILE)
5361             && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
5362           warning (0, "%<noreturn%> function returns non-void value");
5363
5364         /* Every function declaration is an external reference
5365            (DECL_EXTERNAL) except for those which are not at file
5366            scope and are explicitly declared "auto".  This is
5367            forbidden by standard C (C99 6.7.1p5) and is interpreted by
5368            GCC to signify a forward declaration of a nested function.  */
5369         if (storage_class == csc_auto && current_scope != file_scope)
5370           DECL_EXTERNAL (decl) = 0;
5371         /* In C99, a function which is declared 'inline' with 'extern'
5372            is not an external reference (which is confusing).  It
5373            means that the later definition of the function must be output
5374            in this file, C99 6.7.4p6.  In GNU C89, a function declared
5375            'extern inline' is an external reference.  */
5376         else if (declspecs->inline_p && storage_class != csc_static)
5377           DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
5378                                   == flag_gnu89_inline);
5379         else
5380           DECL_EXTERNAL (decl) = !initialized;
5381
5382         /* Record absence of global scope for `static' or `auto'.  */
5383         TREE_PUBLIC (decl)
5384           = !(storage_class == csc_static || storage_class == csc_auto);
5385
5386         /* For a function definition, record the argument information
5387            block where store_parm_decls will look for it.  */
5388         if (funcdef_flag)
5389           current_function_arg_info = arg_info;
5390
5391         if (declspecs->default_int_p)
5392           C_FUNCTION_IMPLICIT_INT (decl) = 1;
5393
5394         /* Record presence of `inline', if it is reasonable.  */
5395         if (flag_hosted && MAIN_NAME_P (declarator->u.id))
5396           {
5397             if (declspecs->inline_p)
5398               pedwarn ("cannot inline function %<main%>");
5399           }
5400         else if (declspecs->inline_p)
5401           {
5402             /* Record that the function is declared `inline'.  */
5403             DECL_DECLARED_INLINE_P (decl) = 1;
5404
5405             /* Do not mark bare declarations as DECL_INLINE.  Doing so
5406                in the presence of multiple declarations can result in
5407                the abstract origin pointing between the declarations,
5408                which will confuse dwarf2out.  */
5409             if (initialized)
5410               DECL_INLINE (decl) = 1;
5411           }
5412         /* If -finline-functions, assume it can be inlined.  This does
5413            two things: let the function be deferred until it is actually
5414            needed, and let dwarf2 know that the function is inlinable.  */
5415         else if (flag_inline_trees == 2 && initialized)
5416           DECL_INLINE (decl) = 1;
5417       }
5418     else
5419       {
5420         /* It's a variable.  */
5421         /* An uninitialized decl with `extern' is a reference.  */
5422         int extern_ref = !initialized && storage_class == csc_extern;
5423
5424         type = c_build_qualified_type (type, type_quals);
5425
5426         /* C99 6.2.2p7: It is invalid (compile-time undefined
5427            behavior) to create an 'extern' declaration for a
5428            variable if there is a global declaration that is
5429            'static' and the global declaration is not visible.
5430            (If the static declaration _is_ currently visible,
5431            the 'extern' declaration is taken to refer to that decl.) */
5432         if (extern_ref && current_scope != file_scope)
5433           {
5434             tree global_decl  = identifier_global_value (declarator->u.id);
5435             tree visible_decl = lookup_name (declarator->u.id);
5436
5437             if (global_decl
5438                 && global_decl != visible_decl
5439                 && TREE_CODE (global_decl) == VAR_DECL
5440                 && !TREE_PUBLIC (global_decl))
5441               error ("variable previously declared %<static%> redeclared "
5442                      "%<extern%>");
5443           }
5444
5445         decl = build_decl (VAR_DECL, declarator->u.id, type);
5446         DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
5447         if (size_varies)
5448           C_DECL_VARIABLE_SIZE (decl) = 1;
5449
5450         if (declspecs->inline_p)
5451           pedwarn ("variable %q+D declared %<inline%>", decl);
5452
5453         /* At file scope, an initialized extern declaration may follow
5454            a static declaration.  In that case, DECL_EXTERNAL will be
5455            reset later in start_decl.  */
5456         DECL_EXTERNAL (decl) = (storage_class == csc_extern);
5457
5458         /* At file scope, the presence of a `static' or `register' storage
5459            class specifier, or the absence of all storage class specifiers
5460            makes this declaration a definition (perhaps tentative).  Also,
5461            the absence of `static' makes it public.  */
5462         if (current_scope == file_scope)
5463           {
5464             TREE_PUBLIC (decl) = storage_class != csc_static;
5465             TREE_STATIC (decl) = !extern_ref;
5466           }
5467         /* Not at file scope, only `static' makes a static definition.  */
5468         else
5469           {
5470             TREE_STATIC (decl) = (storage_class == csc_static);
5471             TREE_PUBLIC (decl) = extern_ref;
5472           }
5473
5474         if (threadp)
5475           {
5476             if (targetm.have_tls)
5477               DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
5478             else
5479               /* A mere warning is sure to result in improper semantics
5480                  at runtime.  Don't bother to allow this to compile.  */
5481               error ("thread-local storage not supported for this target");
5482           }
5483       }
5484
5485     if (storage_class == csc_extern
5486         && variably_modified_type_p (type, NULL_TREE))
5487       {
5488         /* C99 6.7.5.2p2 */
5489         error ("object with variably modified type must have no linkage");
5490       }
5491
5492     /* Record `register' declaration for warnings on &
5493        and in case doing stupid register allocation.  */
5494
5495     if (storage_class == csc_register)
5496       {
5497         C_DECL_REGISTER (decl) = 1;
5498         DECL_REGISTER (decl) = 1;
5499       }
5500
5501     /* Record constancy and volatility.  */
5502     c_apply_type_quals_to_decl (type_quals, decl);
5503
5504     /* If a type has volatile components, it should be stored in memory.
5505        Otherwise, the fact that those components are volatile
5506        will be ignored, and would even crash the compiler.
5507        Of course, this only makes sense on  VAR,PARM, and RESULT decl's.   */
5508     if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
5509         && (TREE_CODE (decl) == VAR_DECL ||  TREE_CODE (decl) == PARM_DECL
5510           || TREE_CODE (decl) == RESULT_DECL))
5511       {
5512         /* It is not an error for a structure with volatile fields to
5513            be declared register, but reset DECL_REGISTER since it
5514            cannot actually go in a register.  */
5515         int was_reg = C_DECL_REGISTER (decl);
5516         C_DECL_REGISTER (decl) = 0;
5517         DECL_REGISTER (decl) = 0;
5518         c_mark_addressable (decl);
5519         C_DECL_REGISTER (decl) = was_reg;
5520       }
5521
5522   /* This is the earliest point at which we might know the assembler
5523      name of a variable.  Thus, if it's known before this, die horribly.  */
5524     gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
5525
5526     decl_attributes (&decl, returned_attrs, 0);
5527
5528     return decl;
5529   }
5530 }
5531 \f
5532 /* Decode the parameter-list info for a function type or function definition.
5533    The argument is the value returned by `get_parm_info' (or made in c-parse.c
5534    if there is an identifier list instead of a parameter decl list).
5535    These two functions are separate because when a function returns
5536    or receives functions then each is called multiple times but the order
5537    of calls is different.  The last call to `grokparms' is always the one
5538    that contains the formal parameter names of a function definition.
5539
5540    Return a list of arg types to use in the FUNCTION_TYPE for this function.
5541
5542    FUNCDEF_FLAG is true for a function definition, false for
5543    a mere declaration.  A nonempty identifier-list gets an error message
5544    when FUNCDEF_FLAG is false.  */
5545
5546 static tree
5547 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
5548 {
5549   tree arg_types = arg_info->types;
5550
5551   if (funcdef_flag && arg_info->had_vla_unspec)
5552     {
5553       /* A function definition isn't function prototype scope C99 6.2.1p4.  */
5554       /* C99 6.7.5.2p4 */
5555       error ("%<[*]%> not allowed in other than function prototype scope");
5556     }
5557
5558   if (arg_types == 0 && !funcdef_flag && !in_system_header)
5559     warning (OPT_Wstrict_prototypes,
5560              "function declaration isn%'t a prototype");
5561
5562   if (arg_types == error_mark_node)
5563     return 0;  /* don't set TYPE_ARG_TYPES in this case */
5564
5565   else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
5566     {
5567       if (!funcdef_flag)
5568         pedwarn ("parameter names (without types) in function declaration");
5569
5570       arg_info->parms = arg_info->types;
5571       arg_info->types = 0;
5572       return 0;
5573     }
5574   else
5575     {
5576       tree parm, type, typelt;
5577       unsigned int parmno;
5578
5579       /* If there is a parameter of incomplete type in a definition,
5580          this is an error.  In a declaration this is valid, and a
5581          struct or union type may be completed later, before any calls
5582          or definition of the function.  In the case where the tag was
5583          first declared within the parameter list, a warning has
5584          already been given.  If a parameter has void type, then
5585          however the function cannot be defined or called, so
5586          warn.  */
5587
5588       for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
5589            parm;
5590            parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
5591         {
5592           type = TREE_VALUE (typelt);
5593           if (type == error_mark_node)
5594             continue;
5595
5596           if (!COMPLETE_TYPE_P (type))
5597             {
5598               if (funcdef_flag)
5599                 {
5600                   if (DECL_NAME (parm))
5601                     error ("parameter %u (%q+D) has incomplete type",
5602                            parmno, parm);
5603                   else
5604                     error ("%Jparameter %u has incomplete type",
5605                            parm, parmno);
5606
5607                   TREE_VALUE (typelt) = error_mark_node;
5608                   TREE_TYPE (parm) = error_mark_node;
5609                 }
5610               else if (VOID_TYPE_P (type))
5611                 {
5612                   if (DECL_NAME (parm))
5613                     warning (0, "parameter %u (%q+D) has void type",
5614                              parmno, parm);
5615                   else
5616                     warning (0, "%Jparameter %u has void type",
5617                              parm, parmno);
5618                 }
5619             }
5620
5621           if (DECL_NAME (parm) && TREE_USED (parm))
5622             warn_if_shadowing (parm);
5623         }
5624       return arg_types;
5625     }
5626 }
5627
5628 /* Take apart the current scope and return a c_arg_info structure with
5629    info on a parameter list just parsed.
5630
5631    This structure is later fed to 'grokparms' and 'store_parm_decls'.
5632
5633    ELLIPSIS being true means the argument list ended in '...' so don't
5634    append a sentinel (void_list_node) to the end of the type-list.  */
5635
5636 struct c_arg_info *
5637 get_parm_info (bool ellipsis)
5638 {
5639   struct c_binding *b = current_scope->bindings;
5640   struct c_arg_info *arg_info = XOBNEW (&parser_obstack,
5641                                         struct c_arg_info);
5642   tree parms    = 0;
5643   tree tags     = 0;
5644   tree types    = 0;
5645   tree others   = 0;
5646
5647   static bool explained_incomplete_types = false;
5648   bool gave_void_only_once_err = false;
5649
5650   arg_info->parms = 0;
5651   arg_info->tags = 0;
5652   arg_info->types = 0;
5653   arg_info->others = 0;
5654   arg_info->pending_sizes = 0;
5655   arg_info->had_vla_unspec = current_scope->had_vla_unspec;
5656
5657   /* The bindings in this scope must not get put into a block.
5658      We will take care of deleting the binding nodes.  */
5659   current_scope->bindings = 0;
5660
5661   /* This function is only called if there was *something* on the
5662      parameter list.  */
5663   gcc_assert (b);
5664
5665   /* A parameter list consisting solely of 'void' indicates that the
5666      function takes no arguments.  But if the 'void' is qualified
5667      (by 'const' or 'volatile'), or has a storage class specifier
5668      ('register'), then the behavior is undefined; issue an error.
5669      Typedefs for 'void' are OK (see DR#157).  */
5670   if (b->prev == 0                          /* one binding */
5671       && TREE_CODE (b->decl) == PARM_DECL   /* which is a parameter */
5672       && !DECL_NAME (b->decl)               /* anonymous */
5673       && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
5674     {
5675       if (TREE_THIS_VOLATILE (b->decl)
5676           || TREE_READONLY (b->decl)
5677           || C_DECL_REGISTER (b->decl))
5678         error ("%<void%> as only parameter may not be qualified");
5679
5680       /* There cannot be an ellipsis.  */
5681       if (ellipsis)
5682         error ("%<void%> must be the only parameter");
5683
5684       arg_info->types = void_list_node;
5685       return arg_info;
5686     }
5687
5688   if (!ellipsis)
5689     types = void_list_node;
5690
5691   /* Break up the bindings list into parms, tags, types, and others;
5692      apply sanity checks; purge the name-to-decl bindings.  */
5693   while (b)
5694     {
5695       tree decl = b->decl;
5696       tree type = TREE_TYPE (decl);
5697       const char *keyword;
5698
5699       switch (TREE_CODE (decl))
5700         {
5701         case PARM_DECL:
5702           if (b->id)
5703             {
5704               gcc_assert (I_SYMBOL_BINDING (b->id) == b);
5705               I_SYMBOL_BINDING (b->id) = b->shadowed;
5706             }
5707
5708           /* Check for forward decls that never got their actual decl.  */
5709           if (TREE_ASM_WRITTEN (decl))
5710             error ("parameter %q+D has just a forward declaration", decl);
5711           /* Check for (..., void, ...) and issue an error.  */
5712           else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
5713             {
5714               if (!gave_void_only_once_err)
5715                 {
5716                   error ("%<void%> must be the only parameter");
5717                   gave_void_only_once_err = true;
5718                 }
5719             }
5720           else
5721             {
5722               /* Valid parameter, add it to the list.  */
5723               TREE_CHAIN (decl) = parms;
5724               parms = decl;
5725
5726               /* Since there is a prototype, args are passed in their
5727                  declared types.  The back end may override this later.  */
5728               DECL_ARG_TYPE (decl) = type;
5729               types = tree_cons (0, type, types);
5730             }
5731           break;
5732
5733         case ENUMERAL_TYPE: keyword = "enum"; goto tag;
5734         case UNION_TYPE:    keyword = "union"; goto tag;
5735         case RECORD_TYPE:   keyword = "struct"; goto tag;
5736         tag:
5737           /* Types may not have tag-names, in which case the type
5738              appears in the bindings list with b->id NULL.  */
5739           if (b->id)
5740             {
5741               gcc_assert (I_TAG_BINDING (b->id) == b);
5742               I_TAG_BINDING (b->id) = b->shadowed;
5743             }
5744
5745           /* Warn about any struct, union or enum tags defined in a
5746              parameter list.  The scope of such types is limited to
5747              the parameter list, which is rarely if ever desirable
5748              (it's impossible to call such a function with type-
5749              correct arguments).  An anonymous union parm type is
5750              meaningful as a GNU extension, so don't warn for that.  */
5751           if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
5752             {
5753               if (b->id)
5754                 /* The %s will be one of 'struct', 'union', or 'enum'.  */
5755                 warning (0, "%<%s %E%> declared inside parameter list",
5756                          keyword, b->id);
5757               else
5758                 /* The %s will be one of 'struct', 'union', or 'enum'.  */
5759                 warning (0, "anonymous %s declared inside parameter list",
5760                          keyword);
5761
5762               if (!explained_incomplete_types)
5763                 {
5764                   warning (0, "its scope is only this definition or declaration,"
5765                            " which is probably not what you want");
5766                   explained_incomplete_types = true;
5767                 }
5768             }
5769
5770           tags = tree_cons (b->id, decl, tags);
5771           break;
5772
5773         case CONST_DECL:
5774         case TYPE_DECL:
5775         case FUNCTION_DECL:
5776           /* CONST_DECLs appear here when we have an embedded enum,
5777              and TYPE_DECLs appear here when we have an embedded struct
5778              or union.  No warnings for this - we already warned about the
5779              type itself.  FUNCTION_DECLs appear when there is an implicit
5780              function declaration in the parameter list.  */
5781
5782           TREE_CHAIN (decl) = others;
5783           others = decl;
5784           /* fall through */
5785
5786         case ERROR_MARK:
5787           /* error_mark_node appears here when we have an undeclared
5788              variable.  Just throw it away.  */
5789           if (b->id)
5790             {
5791               gcc_assert (I_SYMBOL_BINDING (b->id) == b);
5792               I_SYMBOL_BINDING (b->id) = b->shadowed;
5793             }
5794           break;
5795
5796           /* Other things that might be encountered.  */
5797         case LABEL_DECL:
5798         case VAR_DECL:
5799         default:
5800           gcc_unreachable ();
5801         }
5802
5803       b = free_binding_and_advance (b);
5804     }
5805
5806   arg_info->parms = parms;
5807   arg_info->tags = tags;
5808   arg_info->types = types;
5809   arg_info->others = others;
5810   arg_info->pending_sizes = get_pending_sizes ();
5811   return arg_info;
5812 }
5813 \f
5814 /* Get the struct, enum or union (CODE says which) with tag NAME.
5815    Define the tag as a forward-reference if it is not defined.
5816    Return a c_typespec structure for the type specifier.  */
5817
5818 struct c_typespec
5819 parser_xref_tag (enum tree_code code, tree name)
5820 {
5821   struct c_typespec ret;
5822   /* If a cross reference is requested, look up the type
5823      already defined for this tag and return it.  */
5824
5825   tree ref = lookup_tag (code, name, 0);
5826   /* If this is the right type of tag, return what we found.
5827      (This reference will be shadowed by shadow_tag later if appropriate.)
5828      If this is the wrong type of tag, do not return it.  If it was the
5829      wrong type in the same scope, we will have had an error
5830      message already; if in a different scope and declaring
5831      a name, pending_xref_error will give an error message; but if in a
5832      different scope and not declaring a name, this tag should
5833      shadow the previous declaration of a different type of tag, and
5834      this would not work properly if we return the reference found.
5835      (For example, with "struct foo" in an outer scope, "union foo;"
5836      must shadow that tag with a new one of union type.)  */
5837   ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
5838   if (ref && TREE_CODE (ref) == code)
5839     {
5840       ret.spec = ref;
5841       return ret;
5842     }
5843
5844   /* If no such tag is yet defined, create a forward-reference node
5845      and record it as the "definition".
5846      When a real declaration of this type is found,
5847      the forward-reference will be altered into a real type.  */
5848
5849   ref = make_node (code);
5850   if (code == ENUMERAL_TYPE)
5851     {
5852       /* Give the type a default layout like unsigned int
5853          to avoid crashing if it does not get defined.  */
5854       TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
5855       TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
5856       TYPE_USER_ALIGN (ref) = 0;
5857       TYPE_UNSIGNED (ref) = 1;
5858       TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
5859       TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
5860       TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
5861     }
5862
5863   pushtag (name, ref);
5864
5865   ret.spec = ref;
5866   return ret;
5867 }
5868
5869 /* Get the struct, enum or union (CODE says which) with tag NAME.
5870    Define the tag as a forward-reference if it is not defined.
5871    Return a tree for the type.  */
5872
5873 tree
5874 xref_tag (enum tree_code code, tree name)
5875 {
5876   return parser_xref_tag (code, name).spec;
5877 }
5878 \f
5879 /* Make sure that the tag NAME is defined *in the current scope*
5880    at least as a forward reference.
5881    CODE says which kind of tag NAME ought to be.  */
5882
5883 tree
5884 start_struct (enum tree_code code, tree name)
5885 {
5886   /* If there is already a tag defined at this scope
5887      (as a forward reference), just return it.  */
5888
5889   tree ref = 0;
5890
5891   if (name != 0)
5892     ref = lookup_tag (code, name, 1);
5893   if (ref && TREE_CODE (ref) == code)
5894     {
5895       if (TYPE_SIZE (ref))
5896         {
5897           if (code == UNION_TYPE)
5898             error ("redefinition of %<union %E%>", name);
5899           else
5900             error ("redefinition of %<struct %E%>", name);
5901         }
5902       else if (C_TYPE_BEING_DEFINED (ref))
5903         {
5904           if (code == UNION_TYPE)
5905             error ("nested redefinition of %<union %E%>", name);
5906           else
5907             error ("nested redefinition of %<struct %E%>", name);
5908           /* Don't create structures that contain themselves.  */
5909           ref = NULL_TREE;
5910         }
5911     }
5912
5913   /* Otherwise create a forward-reference just so the tag is in scope.  */
5914
5915   if (ref == NULL_TREE || TREE_CODE (ref) != code)
5916     {
5917       ref = make_node (code);
5918       pushtag (name, ref);
5919     }
5920
5921   C_TYPE_BEING_DEFINED (ref) = 1;
5922   TYPE_PACKED (ref) = flag_pack_struct;
5923   return ref;
5924 }
5925
5926 /* Process the specs, declarator and width (NULL if omitted)
5927    of a structure component, returning a FIELD_DECL node.
5928    WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
5929
5930    This is done during the parsing of the struct declaration.
5931    The FIELD_DECL nodes are chained together and the lot of them
5932    are ultimately passed to `build_struct' to make the RECORD_TYPE node.  */
5933
5934 tree
5935 grokfield (struct c_declarator *declarator, struct c_declspecs *declspecs,
5936            tree width)
5937 {
5938   tree value;
5939
5940   if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
5941       && width == NULL_TREE)
5942     {
5943       /* This is an unnamed decl.
5944
5945          If we have something of the form "union { list } ;" then this
5946          is the anonymous union extension.  Similarly for struct.
5947
5948          If this is something of the form "struct foo;", then
5949            If MS extensions are enabled, this is handled as an
5950              anonymous struct.
5951            Otherwise this is a forward declaration of a structure tag.
5952
5953          If this is something of the form "foo;" and foo is a TYPE_DECL, then
5954            If MS extensions are enabled and foo names a structure, then
5955              again this is an anonymous struct.
5956            Otherwise this is an error.
5957
5958          Oh what a horrid tangled web we weave.  I wonder if MS consciously
5959          took this from Plan 9 or if it was an accident of implementation
5960          that took root before someone noticed the bug...  */
5961
5962       tree type = declspecs->type;
5963       bool type_ok = (TREE_CODE (type) == RECORD_TYPE
5964                       || TREE_CODE (type) == UNION_TYPE);
5965       bool ok = false;
5966
5967       if (type_ok
5968           && (flag_ms_extensions || !declspecs->typedef_p))
5969         {
5970           if (flag_ms_extensions)
5971             ok = true;
5972           else if (flag_iso)
5973             ok = false;
5974           else if (TYPE_NAME (type) == NULL)
5975             ok = true;
5976           else
5977             ok = false;
5978         }
5979       if (!ok)
5980         {
5981           pedwarn ("declaration does not declare anything");
5982           return NULL_TREE;
5983         }
5984       if (pedantic)
5985         pedwarn ("ISO C doesn%'t support unnamed structs/unions");
5986     }
5987
5988   value = grokdeclarator (declarator, declspecs, FIELD, false,
5989                           width ? &width : NULL);
5990
5991   finish_decl (value, NULL_TREE, NULL_TREE);
5992   DECL_INITIAL (value) = width;
5993
5994   return value;
5995 }
5996 \f
5997 /* Generate an error for any duplicate field names in FIELDLIST.  Munge
5998    the list such that this does not present a problem later.  */
5999
6000 static void
6001 detect_field_duplicates (tree fieldlist)
6002 {
6003   tree x, y;
6004   int timeout = 10;
6005
6006   /* First, see if there are more than "a few" fields.
6007      This is trivially true if there are zero or one fields.  */
6008   if (!fieldlist)
6009     return;
6010   x = TREE_CHAIN (fieldlist);
6011   if (!x)
6012     return;
6013   do {
6014     timeout--;
6015     x = TREE_CHAIN (x);
6016   } while (timeout > 0 && x);
6017
6018   /* If there were "few" fields, avoid the overhead of allocating
6019      a hash table.  Instead just do the nested traversal thing.  */
6020   if (timeout > 0)
6021     {
6022       for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
6023         if (DECL_NAME (x))
6024           {
6025             for (y = fieldlist; y != x; y = TREE_CHAIN (y))
6026               if (DECL_NAME (y) == DECL_NAME (x))
6027                 {
6028                   error ("duplicate member %q+D", x);
6029                   DECL_NAME (x) = NULL_TREE;
6030                 }
6031           }
6032     }
6033   else
6034     {
6035       htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
6036       void **slot;
6037
6038       for (x = fieldlist; x ; x = TREE_CHAIN (x))
6039         if ((y = DECL_NAME (x)) != 0)
6040           {
6041             slot = htab_find_slot (htab, y, INSERT);
6042             if (*slot)
6043               {
6044                 error ("duplicate member %q+D", x);
6045                 DECL_NAME (x) = NULL_TREE;
6046               }
6047             *slot = y;
6048           }
6049
6050       htab_delete (htab);
6051     }
6052 }
6053
6054 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
6055    FIELDLIST is a chain of FIELD_DECL nodes for the fields.
6056    ATTRIBUTES are attributes to be applied to the structure.  */
6057
6058 tree
6059 finish_struct (tree t, tree fieldlist, tree attributes)
6060 {
6061   tree x;
6062   bool toplevel = file_scope == current_scope;
6063   int saw_named_field;
6064
6065   /* If this type was previously laid out as a forward reference,
6066      make sure we lay it out again.  */
6067
6068   TYPE_SIZE (t) = 0;
6069
6070   decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
6071
6072   if (pedantic)
6073     {
6074       for (x = fieldlist; x; x = TREE_CHAIN (x))
6075         if (DECL_NAME (x) != 0)
6076           break;
6077
6078       if (x == 0)
6079         {
6080           if (TREE_CODE (t) == UNION_TYPE)
6081             {
6082               if (fieldlist)
6083                 pedwarn ("union has no named members");
6084               else
6085                 pedwarn ("union has no members");
6086             }
6087           else
6088             {
6089               if (fieldlist)
6090                 pedwarn ("struct has no named members");
6091               else
6092                 pedwarn ("struct has no members");
6093             }
6094         }
6095     }
6096
6097   /* Install struct as DECL_CONTEXT of each field decl.
6098      Also process specified field sizes, found in the DECL_INITIAL,
6099      storing 0 there after the type has been changed to precision equal
6100      to its width, rather than the precision of the specified standard
6101      type.  (Correct layout requires the original type to have been preserved
6102      until now.)  */
6103
6104   saw_named_field = 0;
6105   for (x = fieldlist; x; x = TREE_CHAIN (x))
6106     {
6107       if (TREE_TYPE (x) == error_mark_node)
6108         continue;
6109
6110       DECL_CONTEXT (x) = t;
6111
6112       if (TYPE_PACKED (t) && TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
6113         DECL_PACKED (x) = 1;
6114
6115       /* If any field is const, the structure type is pseudo-const.  */
6116       if (TREE_READONLY (x))
6117         C_TYPE_FIELDS_READONLY (t) = 1;
6118       else
6119         {
6120           /* A field that is pseudo-const makes the structure likewise.  */
6121           tree t1 = TREE_TYPE (x);
6122           while (TREE_CODE (t1) == ARRAY_TYPE)
6123             t1 = TREE_TYPE (t1);
6124           if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
6125               && C_TYPE_FIELDS_READONLY (t1))
6126             C_TYPE_FIELDS_READONLY (t) = 1;
6127         }
6128
6129       /* Any field that is volatile means variables of this type must be
6130          treated in some ways as volatile.  */
6131       if (TREE_THIS_VOLATILE (x))
6132         C_TYPE_FIELDS_VOLATILE (t) = 1;
6133
6134       /* Any field of nominal variable size implies structure is too.  */
6135       if (C_DECL_VARIABLE_SIZE (x))
6136         C_TYPE_VARIABLE_SIZE (t) = 1;
6137
6138       if (DECL_INITIAL (x))
6139         {
6140           unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
6141           DECL_SIZE (x) = bitsize_int (width);
6142           DECL_BIT_FIELD (x) = 1;
6143           SET_DECL_C_BIT_FIELD (x);
6144         }
6145
6146       /* Detect flexible array member in an invalid context.  */
6147       if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
6148           && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
6149           && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
6150           && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
6151         {
6152           if (TREE_CODE (t) == UNION_TYPE)
6153             {
6154               error ("%Jflexible array member in union", x);
6155               TREE_TYPE (x) = error_mark_node;
6156             }
6157           else if (TREE_CHAIN (x) != NULL_TREE)
6158             {
6159               error ("%Jflexible array member not at end of struct", x);
6160               TREE_TYPE (x) = error_mark_node;
6161             }
6162           else if (!saw_named_field)
6163             {
6164               error ("%Jflexible array member in otherwise empty struct", x);
6165               TREE_TYPE (x) = error_mark_node;
6166             }
6167         }
6168
6169       if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE
6170           && flexible_array_type_p (TREE_TYPE (x)))
6171         pedwarn ("%Jinvalid use of structure with flexible array member", x);
6172
6173       if (DECL_NAME (x))
6174         saw_named_field = 1;
6175     }
6176
6177   detect_field_duplicates (fieldlist);
6178
6179   /* Now we have the nearly final fieldlist.  Record it,
6180      then lay out the structure or union (including the fields).  */
6181
6182   TYPE_FIELDS (t) = fieldlist;
6183
6184   layout_type (t);
6185
6186   /* Give bit-fields their proper types.  */
6187   {
6188     tree *fieldlistp = &fieldlist;
6189     while (*fieldlistp)
6190       if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
6191           && TREE_TYPE (*fieldlistp) != error_mark_node)
6192         {
6193           unsigned HOST_WIDE_INT width
6194             = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
6195           tree type = TREE_TYPE (*fieldlistp);
6196           if (width != TYPE_PRECISION (type))
6197             {
6198               TREE_TYPE (*fieldlistp)
6199                 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
6200               DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
6201             }
6202           DECL_INITIAL (*fieldlistp) = 0;
6203         }
6204       else
6205         fieldlistp = &TREE_CHAIN (*fieldlistp);
6206   }
6207
6208   /* Now we have the truly final field list.
6209      Store it in this type and in the variants.  */
6210
6211   TYPE_FIELDS (t) = fieldlist;
6212
6213   /* If there are lots of fields, sort so we can look through them fast.
6214      We arbitrarily consider 16 or more elts to be "a lot".  */
6215
6216   {
6217     int len = 0;
6218
6219     for (x = fieldlist; x; x = TREE_CHAIN (x))
6220       {
6221         if (len > 15 || DECL_NAME (x) == NULL)
6222           break;
6223         len += 1;
6224       }
6225
6226     if (len > 15)
6227       {
6228         tree *field_array;
6229         struct lang_type *space;
6230         struct sorted_fields_type *space2;
6231
6232         len += list_length (x);
6233
6234         /* Use the same allocation policy here that make_node uses, to
6235           ensure that this lives as long as the rest of the struct decl.
6236           All decls in an inline function need to be saved.  */
6237
6238         space = GGC_CNEW (struct lang_type);
6239         space2 = GGC_NEWVAR (struct sorted_fields_type,
6240                              sizeof (struct sorted_fields_type) + len * sizeof (tree));
6241
6242         len = 0;
6243         space->s = space2;
6244         field_array = &space2->elts[0];
6245         for (x = fieldlist; x; x = TREE_CHAIN (x))
6246           {
6247             field_array[len++] = x;
6248
6249             /* If there is anonymous struct or union, break out of the loop.  */
6250             if (DECL_NAME (x) == NULL)
6251               break;
6252           }
6253         /* Found no anonymous struct/union.  Add the TYPE_LANG_SPECIFIC.  */
6254         if (x == NULL)
6255           {
6256             TYPE_LANG_SPECIFIC (t) = space;
6257             TYPE_LANG_SPECIFIC (t)->s->len = len;
6258             field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
6259             qsort (field_array, len, sizeof (tree), field_decl_cmp);
6260           }
6261       }
6262   }
6263
6264   for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
6265     {
6266       TYPE_FIELDS (x) = TYPE_FIELDS (t);
6267       TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
6268       C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
6269       C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
6270       C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
6271     }
6272
6273   /* If this was supposed to be a transparent union, but we can't
6274      make it one, warn and turn off the flag.  */
6275   if (TREE_CODE (t) == UNION_TYPE
6276       && TYPE_TRANSPARENT_UNION (t)
6277       && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
6278     {
6279       TYPE_TRANSPARENT_UNION (t) = 0;
6280       warning (0, "union cannot be made transparent");
6281     }
6282
6283   /* If this structure or union completes the type of any previous
6284      variable declaration, lay it out and output its rtl.  */
6285   for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
6286        x;
6287        x = TREE_CHAIN (x))
6288     {
6289       tree decl = TREE_VALUE (x);
6290       if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
6291         layout_array_type (TREE_TYPE (decl));
6292       if (TREE_CODE (decl) != TYPE_DECL)
6293         {
6294           layout_decl (decl, 0);
6295           if (c_dialect_objc ())
6296             objc_check_decl (decl);
6297           rest_of_decl_compilation (decl, toplevel, 0);
6298           if (!toplevel)
6299             expand_decl (decl);
6300         }
6301     }
6302   C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
6303
6304   /* Finish debugging output for this type.  */
6305   rest_of_type_compilation (t, toplevel);
6306
6307   /* If we're inside a function proper, i.e. not file-scope and not still
6308      parsing parameters, then arrange for the size of a variable sized type
6309      to be bound now.  */
6310   if (cur_stmt_list && variably_modified_type_p (t, NULL_TREE))
6311     add_stmt (build_stmt (DECL_EXPR, build_decl (TYPE_DECL, NULL, t)));
6312
6313   return t;
6314 }
6315
6316 /* Lay out the type T, and its element type, and so on.  */
6317
6318 static void
6319 layout_array_type (tree t)
6320 {
6321   if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
6322     layout_array_type (TREE_TYPE (t));
6323   layout_type (t);
6324 }
6325 \f
6326 /* Begin compiling the definition of an enumeration type.
6327    NAME is its name (or null if anonymous).
6328    Returns the type object, as yet incomplete.
6329    Also records info about it so that build_enumerator
6330    may be used to declare the individual values as they are read.  */
6331
6332 tree
6333 start_enum (tree name)
6334 {
6335   tree enumtype = 0;
6336
6337   /* If this is the real definition for a previous forward reference,
6338      fill in the contents in the same object that used to be the
6339      forward reference.  */
6340
6341   if (name != 0)
6342     enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
6343
6344   if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
6345     {
6346       enumtype = make_node (ENUMERAL_TYPE);
6347       pushtag (name, enumtype);
6348     }
6349
6350   if (C_TYPE_BEING_DEFINED (enumtype))
6351     error ("nested redefinition of %<enum %E%>", name);
6352
6353   C_TYPE_BEING_DEFINED (enumtype) = 1;
6354
6355   if (TYPE_VALUES (enumtype) != 0)
6356     {
6357       /* This enum is a named one that has been declared already.  */
6358       error ("redeclaration of %<enum %E%>", name);
6359
6360       /* Completely replace its old definition.
6361          The old enumerators remain defined, however.  */
6362       TYPE_VALUES (enumtype) = 0;
6363     }
6364
6365   enum_next_value = integer_zero_node;
6366   enum_overflow = 0;
6367
6368   if (flag_short_enums)
6369     TYPE_PACKED (enumtype) = 1;
6370
6371   return enumtype;
6372 }
6373
6374 /* After processing and defining all the values of an enumeration type,
6375    install their decls in the enumeration type and finish it off.
6376    ENUMTYPE is the type object, VALUES a list of decl-value pairs,
6377    and ATTRIBUTES are the specified attributes.
6378    Returns ENUMTYPE.  */
6379
6380 tree
6381 finish_enum (tree enumtype, tree values, tree attributes)
6382 {
6383   tree pair, tem;
6384   tree minnode = 0, maxnode = 0;
6385   int precision, unsign;
6386   bool toplevel = (file_scope == current_scope);
6387   struct lang_type *lt;
6388
6389   decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
6390
6391   /* Calculate the maximum value of any enumerator in this type.  */
6392
6393   if (values == error_mark_node)
6394     minnode = maxnode = integer_zero_node;
6395   else
6396     {
6397       minnode = maxnode = TREE_VALUE (values);
6398       for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
6399         {
6400           tree value = TREE_VALUE (pair);
6401           if (tree_int_cst_lt (maxnode, value))
6402             maxnode = value;
6403           if (tree_int_cst_lt (value, minnode))
6404             minnode = value;
6405         }
6406     }
6407
6408   /* Construct the final type of this enumeration.  It is the same
6409      as one of the integral types - the narrowest one that fits, except
6410      that normally we only go as narrow as int - and signed iff any of
6411      the values are negative.  */
6412   unsign = (tree_int_cst_sgn (minnode) >= 0);
6413   precision = MAX (min_precision (minnode, unsign),
6414                    min_precision (maxnode, unsign));
6415
6416   if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
6417     {
6418       tem = c_common_type_for_size (precision, unsign);
6419       if (tem == NULL)
6420         {
6421           warning (0, "enumeration values exceed range of largest integer");
6422           tem = long_long_integer_type_node;
6423         }
6424     }
6425   else
6426     tem = unsign ? unsigned_type_node : integer_type_node;
6427
6428   TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
6429   TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
6430   TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
6431   TYPE_SIZE (enumtype) = 0;
6432
6433   /* If the precision of the type was specific with an attribute and it
6434      was too small, give an error.  Otherwise, use it.  */
6435   if (TYPE_PRECISION (enumtype))
6436     {
6437       if (precision > TYPE_PRECISION (enumtype))
6438         error ("specified mode too small for enumeral values");
6439     }
6440   else
6441     TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
6442
6443   layout_type (enumtype);
6444
6445   if (values != error_mark_node)
6446     {
6447       /* Change the type of the enumerators to be the enum type.  We
6448          need to do this irrespective of the size of the enum, for
6449          proper type checking.  Replace the DECL_INITIALs of the
6450          enumerators, and the value slots of the list, with copies
6451          that have the enum type; they cannot be modified in place
6452          because they may be shared (e.g.  integer_zero_node) Finally,
6453          change the purpose slots to point to the names of the decls.  */
6454       for (pair = values; pair; pair = TREE_CHAIN (pair))
6455         {
6456           tree enu = TREE_PURPOSE (pair);
6457           tree ini = DECL_INITIAL (enu);
6458
6459           TREE_TYPE (enu) = enumtype;
6460
6461           /* The ISO C Standard mandates enumerators to have type int,
6462              even though the underlying type of an enum type is
6463              unspecified.  Here we convert any enumerators that fit in
6464              an int to type int, to avoid promotions to unsigned types
6465              when comparing integers with enumerators that fit in the
6466              int range.  When -pedantic is given, build_enumerator()
6467              would have already taken care of those that don't fit.  */
6468           if (int_fits_type_p (ini, integer_type_node))
6469             tem = integer_type_node;
6470           else
6471             tem = enumtype;
6472           ini = convert (tem, ini);
6473
6474           DECL_INITIAL (enu) = ini;
6475           TREE_PURPOSE (pair) = DECL_NAME (enu);
6476           TREE_VALUE (pair) = ini;
6477         }
6478
6479       TYPE_VALUES (enumtype) = values;
6480     }
6481
6482   /* Record the min/max values so that we can warn about bit-field
6483      enumerations that are too small for the values.  */
6484   lt = GGC_CNEW (struct lang_type);
6485   lt->enum_min = minnode;
6486   lt->enum_max = maxnode;
6487   TYPE_LANG_SPECIFIC (enumtype) = lt;
6488
6489   /* Fix up all variant types of this enum type.  */
6490   for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
6491     {
6492       if (tem == enumtype)
6493         continue;
6494       TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
6495       TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
6496       TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
6497       TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
6498       TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
6499       TYPE_MODE (tem) = TYPE_MODE (enumtype);
6500       TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
6501       TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
6502       TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
6503       TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
6504       TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
6505     }
6506
6507   /* Finish debugging output for this type.  */
6508   rest_of_type_compilation (enumtype, toplevel);
6509
6510   return enumtype;
6511 }
6512
6513 /* Build and install a CONST_DECL for one value of the
6514    current enumeration type (one that was begun with start_enum).
6515    Return a tree-list containing the CONST_DECL and its value.
6516    Assignment of sequential values by default is handled here.  */
6517
6518 tree
6519 build_enumerator (tree name, tree value)
6520 {
6521   tree decl, type;
6522
6523   /* Validate and default VALUE.  */
6524
6525   if (value != 0)
6526     {
6527       /* Don't issue more errors for error_mark_node (i.e. an
6528          undeclared identifier) - just ignore the value expression.  */
6529       if (value == error_mark_node)
6530         value = 0;
6531       else if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
6532                || TREE_CODE (value) != INTEGER_CST)
6533         {
6534           error ("enumerator value for %qE is not an integer constant", name);
6535           value = 0;
6536         }
6537       else
6538         {
6539           value = default_conversion (value);
6540           constant_expression_warning (value);
6541         }
6542     }
6543
6544   /* Default based on previous value.  */
6545   /* It should no longer be possible to have NON_LVALUE_EXPR
6546      in the default.  */
6547   if (value == 0)
6548     {
6549       value = enum_next_value;
6550       if (enum_overflow)
6551         error ("overflow in enumeration values");
6552     }
6553
6554   if (pedantic && !int_fits_type_p (value, integer_type_node))
6555     {
6556       pedwarn ("ISO C restricts enumerator values to range of %<int%>");
6557       /* XXX This causes -pedantic to change the meaning of the program.
6558          Remove?  -zw 2004-03-15  */
6559       value = convert (integer_type_node, value);
6560     }
6561
6562   /* Set basis for default for next value.  */
6563   enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
6564   enum_overflow = tree_int_cst_lt (enum_next_value, value);
6565
6566   /* Now create a declaration for the enum value name.  */
6567
6568   type = TREE_TYPE (value);
6569   type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
6570                                       TYPE_PRECISION (integer_type_node)),
6571                                  (TYPE_PRECISION (type)
6572                                   >= TYPE_PRECISION (integer_type_node)
6573                                   && TYPE_UNSIGNED (type)));
6574
6575   decl = build_decl (CONST_DECL, name, type);
6576   DECL_INITIAL (decl) = convert (type, value);
6577   pushdecl (decl);
6578
6579   return tree_cons (decl, value, NULL_TREE);
6580 }
6581
6582 \f
6583 /* Create the FUNCTION_DECL for a function definition.
6584    DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
6585    the declaration; they describe the function's name and the type it returns,
6586    but twisted together in a fashion that parallels the syntax of C.
6587
6588    This function creates a binding context for the function body
6589    as well as setting up the FUNCTION_DECL in current_function_decl.
6590
6591    Returns 1 on success.  If the DECLARATOR is not suitable for a function
6592    (it defines a datum instead), we return 0, which tells
6593    yyparse to report a parse error.  */
6594
6595 int
6596 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
6597                 tree attributes)
6598 {
6599   tree decl1, old_decl;
6600   tree restype, resdecl;
6601   struct c_label_context_se *nstack_se;
6602   struct c_label_context_vm *nstack_vm;
6603
6604   current_function_returns_value = 0;  /* Assume, until we see it does.  */
6605   current_function_returns_null = 0;
6606   current_function_returns_abnormally = 0;
6607   warn_about_return_type = 0;
6608   c_switch_stack = NULL;
6609
6610   nstack_se = XOBNEW (&parser_obstack, struct c_label_context_se);
6611   nstack_se->labels_def = NULL;
6612   nstack_se->labels_used = NULL;
6613   nstack_se->next = label_context_stack_se;
6614   label_context_stack_se = nstack_se;
6615
6616   nstack_vm = XOBNEW (&parser_obstack, struct c_label_context_vm);
6617   nstack_vm->labels_def = NULL;
6618   nstack_vm->labels_used = NULL;
6619   nstack_vm->scope = 0;
6620   nstack_vm->next = label_context_stack_vm;
6621   label_context_stack_vm = nstack_vm;
6622
6623   /* Indicate no valid break/continue context by setting these variables
6624      to some non-null, non-label value.  We'll notice and emit the proper
6625      error message in c_finish_bc_stmt.  */
6626   c_break_label = c_cont_label = size_zero_node;
6627
6628   decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL);
6629
6630   /* If the declarator is not suitable for a function definition,
6631      cause a syntax error.  */
6632   if (decl1 == 0)
6633     {
6634       label_context_stack_se = label_context_stack_se->next;
6635       label_context_stack_vm = label_context_stack_vm->next;
6636       return 0;
6637     }
6638
6639   decl_attributes (&decl1, attributes, 0);
6640
6641   if (DECL_DECLARED_INLINE_P (decl1)
6642       && DECL_UNINLINABLE (decl1)
6643       && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
6644     warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
6645              decl1);
6646
6647   /* Handle gnu_inline attribute.  */
6648   if (declspecs->inline_p
6649       && !flag_gnu89_inline
6650       && TREE_CODE (decl1) == FUNCTION_DECL
6651       && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1)))
6652     {
6653       if (declspecs->storage_class != csc_static)
6654         DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
6655     }
6656
6657   announce_function (decl1);
6658
6659   if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
6660     {
6661       error ("return type is an incomplete type");
6662       /* Make it return void instead.  */
6663       TREE_TYPE (decl1)
6664         = build_function_type (void_type_node,
6665                                TYPE_ARG_TYPES (TREE_TYPE (decl1)));
6666     }
6667
6668   if (warn_about_return_type)
6669     pedwarn_c99 ("return type defaults to %<int%>");
6670
6671   /* Make the init_value nonzero so pushdecl knows this is not tentative.
6672      error_mark_node is replaced below (in pop_scope) with the BLOCK.  */
6673   DECL_INITIAL (decl1) = error_mark_node;
6674
6675   /* If this definition isn't a prototype and we had a prototype declaration
6676      before, copy the arg type info from that prototype.  */
6677   old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
6678   if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
6679     old_decl = 0;
6680   current_function_prototype_locus = UNKNOWN_LOCATION;
6681   current_function_prototype_built_in = false;
6682   current_function_prototype_arg_types = NULL_TREE;
6683   if (TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
6684     {
6685       if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
6686           && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
6687                         TREE_TYPE (TREE_TYPE (old_decl))))
6688         {
6689           TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
6690                                               TREE_TYPE (decl1));
6691           current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
6692           current_function_prototype_built_in
6693             = C_DECL_BUILTIN_PROTOTYPE (old_decl);
6694           current_function_prototype_arg_types
6695             = TYPE_ARG_TYPES (TREE_TYPE (decl1));
6696         }
6697       if (TREE_PUBLIC (decl1))
6698         {
6699           /* If there is an external prototype declaration of this
6700              function, record its location but do not copy information
6701              to this decl.  This may be an invisible declaration
6702              (built-in or in a scope which has finished) or simply
6703              have more refined argument types than any declaration
6704              found above.  */
6705           struct c_binding *b;
6706           for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
6707             if (B_IN_SCOPE (b, external_scope))
6708               break;
6709           if (b)
6710             {
6711               tree ext_decl, ext_type;
6712               ext_decl = b->decl;
6713               ext_type = b->type ? b->type : TREE_TYPE (ext_decl);
6714               if (TREE_CODE (ext_type) == FUNCTION_TYPE
6715                   && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
6716                                 TREE_TYPE (ext_type)))
6717                 {
6718                   current_function_prototype_locus
6719                     = DECL_SOURCE_LOCATION (ext_decl);
6720                   current_function_prototype_built_in
6721                     = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
6722                   current_function_prototype_arg_types
6723                     = TYPE_ARG_TYPES (ext_type);
6724                 }
6725             }
6726         }
6727     }
6728
6729   /* Optionally warn of old-fashioned def with no previous prototype.  */
6730   if (warn_strict_prototypes
6731       && old_decl != error_mark_node
6732       && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
6733       && C_DECL_ISNT_PROTOTYPE (old_decl))
6734     warning (OPT_Wstrict_prototypes,
6735              "function declaration isn%'t a prototype");
6736   /* Optionally warn of any global def with no previous prototype.  */
6737   else if (warn_missing_prototypes
6738            && old_decl != error_mark_node
6739            && TREE_PUBLIC (decl1)
6740            && !MAIN_NAME_P (DECL_NAME (decl1))
6741            && C_DECL_ISNT_PROTOTYPE (old_decl))
6742     warning (OPT_Wmissing_prototypes, "no previous prototype for %q+D", decl1);
6743   /* Optionally warn of any def with no previous prototype
6744      if the function has already been used.  */
6745   else if (warn_missing_prototypes
6746            && old_decl != 0
6747            && old_decl != error_mark_node
6748            && TREE_USED (old_decl)
6749            && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
6750     warning (OPT_Wmissing_prototypes,
6751              "%q+D was used with no prototype before its definition", decl1);
6752   /* Optionally warn of any global def with no previous declaration.  */
6753   else if (warn_missing_declarations
6754            && TREE_PUBLIC (decl1)
6755            && old_decl == 0
6756            && !MAIN_NAME_P (DECL_NAME (decl1)))
6757     warning (OPT_Wmissing_declarations, "no previous declaration for %q+D",
6758              decl1);
6759   /* Optionally warn of any def with no previous declaration
6760      if the function has already been used.  */
6761   else if (warn_missing_declarations
6762            && old_decl != 0
6763            && old_decl != error_mark_node
6764            && TREE_USED (old_decl)
6765            && C_DECL_IMPLICIT (old_decl))
6766     warning (OPT_Wmissing_declarations,
6767              "%q+D was used with no declaration before its definition", decl1);
6768
6769   /* This function exists in static storage.
6770      (This does not mean `static' in the C sense!)  */
6771   TREE_STATIC (decl1) = 1;
6772
6773   /* A nested function is not global.  */
6774   if (current_function_decl != 0)
6775     TREE_PUBLIC (decl1) = 0;
6776
6777   /* This is the earliest point at which we might know the assembler
6778      name of the function.  Thus, if it's set before this, die horribly.  */
6779   gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
6780
6781   /* If #pragma weak was used, mark the decl weak now.  */
6782   if (current_scope == file_scope)
6783     maybe_apply_pragma_weak (decl1);
6784
6785   /* Warn for unlikely, improbable, or stupid declarations of `main'.  */
6786   if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
6787     {
6788       tree args;
6789       int argct = 0;
6790
6791       if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
6792           != integer_type_node)
6793         pedwarn ("return type of %q+D is not %<int%>", decl1);
6794
6795       for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
6796            args = TREE_CHAIN (args))
6797         {
6798           tree type = args ? TREE_VALUE (args) : 0;
6799
6800           if (type == void_type_node)
6801             break;
6802
6803           ++argct;
6804           switch (argct)
6805             {
6806             case 1:
6807               if (TYPE_MAIN_VARIANT (type) != integer_type_node)
6808                 pedwarn ("first argument of %q+D should be %<int%>", decl1);
6809               break;
6810
6811             case 2:
6812               if (TREE_CODE (type) != POINTER_TYPE
6813                   || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
6814                   || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
6815                       != char_type_node))
6816                 pedwarn ("second argument of %q+D should be %<char **%>",
6817                          decl1);
6818               break;
6819
6820             case 3:
6821               if (TREE_CODE (type) != POINTER_TYPE
6822                   || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
6823                   || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
6824                       != char_type_node))
6825                 pedwarn ("third argument of %q+D should probably be "
6826                          "%<char **%>", decl1);
6827               break;
6828             }
6829         }
6830
6831       /* It is intentional that this message does not mention the third
6832          argument because it's only mentioned in an appendix of the
6833          standard.  */
6834       if (argct > 0 && (argct < 2 || argct > 3))
6835         pedwarn ("%q+D takes only zero or two arguments", decl1);
6836
6837       if (!TREE_PUBLIC (decl1))
6838         pedwarn ("%q+D is normally a non-static function", decl1);
6839     }
6840
6841   /* Record the decl so that the function name is defined.
6842      If we already have a decl for this name, and it is a FUNCTION_DECL,
6843      use the old decl.  */
6844
6845   current_function_decl = pushdecl (decl1);
6846
6847   push_scope ();
6848   declare_parm_level ();
6849
6850   restype = TREE_TYPE (TREE_TYPE (current_function_decl));
6851   /* Promote the value to int before returning it.  */
6852   if (c_promoting_integer_type_p (restype))
6853     {
6854       /* It retains unsignedness if not really getting wider.  */
6855       if (TYPE_UNSIGNED (restype)
6856           && (TYPE_PRECISION (restype)
6857                   == TYPE_PRECISION (integer_type_node)))
6858         restype = unsigned_type_node;
6859       else
6860         restype = integer_type_node;
6861     }
6862
6863   resdecl = build_decl (RESULT_DECL, NULL_TREE, restype);
6864   DECL_ARTIFICIAL (resdecl) = 1;
6865   DECL_IGNORED_P (resdecl) = 1;
6866   DECL_RESULT (current_function_decl) = resdecl;
6867
6868   start_fname_decls ();
6869
6870   return 1;
6871 }
6872 \f
6873 /* Subroutine of store_parm_decls which handles new-style function
6874    definitions (prototype format). The parms already have decls, so we
6875    need only record them as in effect and complain if any redundant
6876    old-style parm decls were written.  */
6877 static void
6878 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
6879 {
6880   tree decl;
6881
6882   if (current_scope->bindings)
6883     {
6884       error ("%Jold-style parameter declarations in prototyped "
6885              "function definition", fndecl);
6886
6887       /* Get rid of the old-style declarations.  */
6888       pop_scope ();
6889       push_scope ();
6890     }
6891   /* Don't issue this warning for nested functions, and don't issue this
6892      warning if we got here because ARG_INFO_TYPES was error_mark_node
6893      (this happens when a function definition has just an ellipsis in
6894      its parameter list).  */
6895   else if (!in_system_header && !current_function_scope
6896            && arg_info->types != error_mark_node)
6897     warning (OPT_Wtraditional,
6898              "%Jtraditional C rejects ISO C style function definitions",
6899              fndecl);
6900
6901   /* Now make all the parameter declarations visible in the function body.
6902      We can bypass most of the grunt work of pushdecl.  */
6903   for (decl = arg_info->parms; decl; decl = TREE_CHAIN (decl))
6904     {
6905       DECL_CONTEXT (decl) = current_function_decl;
6906       if (DECL_NAME (decl))
6907         {
6908           bind (DECL_NAME (decl), decl, current_scope,
6909                 /*invisible=*/false, /*nested=*/false);
6910           if (!TREE_USED (decl))
6911             warn_if_shadowing (decl);
6912         }
6913       else
6914         error ("%Jparameter name omitted", decl);
6915     }
6916
6917   /* Record the parameter list in the function declaration.  */
6918   DECL_ARGUMENTS (fndecl) = arg_info->parms;
6919
6920   /* Now make all the ancillary declarations visible, likewise.  */
6921   for (decl = arg_info->others; decl; decl = TREE_CHAIN (decl))
6922     {
6923       DECL_CONTEXT (decl) = current_function_decl;
6924       if (DECL_NAME (decl))
6925         bind (DECL_NAME (decl), decl, current_scope,
6926               /*invisible=*/false, /*nested=*/false);
6927     }
6928
6929   /* And all the tag declarations.  */
6930   for (decl = arg_info->tags; decl; decl = TREE_CHAIN (decl))
6931     if (TREE_PURPOSE (decl))
6932       bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope,
6933             /*invisible=*/false, /*nested=*/false);
6934 }
6935
6936 /* Subroutine of store_parm_decls which handles old-style function
6937    definitions (separate parameter list and declarations).  */
6938
6939 static void
6940 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
6941 {
6942   struct c_binding *b;
6943   tree parm, decl, last;
6944   tree parmids = arg_info->parms;
6945   struct pointer_set_t *seen_args = pointer_set_create ();
6946
6947   if (!in_system_header)
6948     warning (OPT_Wold_style_definition, "%Jold-style function definition",
6949              fndecl);
6950
6951   /* Match each formal parameter name with its declaration.  Save each
6952      decl in the appropriate TREE_PURPOSE slot of the parmids chain.  */
6953   for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6954     {
6955       if (TREE_VALUE (parm) == 0)
6956         {
6957           error ("%Jparameter name missing from parameter list", fndecl);
6958           TREE_PURPOSE (parm) = 0;
6959           continue;
6960         }
6961
6962       b = I_SYMBOL_BINDING (TREE_VALUE (parm));
6963       if (b && B_IN_CURRENT_SCOPE (b))
6964         {
6965           decl = b->decl;
6966           /* If we got something other than a PARM_DECL it is an error.  */
6967           if (TREE_CODE (decl) != PARM_DECL)
6968             error ("%q+D declared as a non-parameter", decl);
6969           /* If the declaration is already marked, we have a duplicate
6970              name.  Complain and ignore the duplicate.  */
6971           else if (pointer_set_contains (seen_args, decl))
6972             {
6973               error ("multiple parameters named %q+D", decl);
6974               TREE_PURPOSE (parm) = 0;
6975               continue;
6976             }
6977           /* If the declaration says "void", complain and turn it into
6978              an int.  */
6979           else if (VOID_TYPE_P (TREE_TYPE (decl)))
6980             {
6981               error ("parameter %q+D declared with void type", decl);
6982               TREE_TYPE (decl) = integer_type_node;
6983               DECL_ARG_TYPE (decl) = integer_type_node;
6984               layout_decl (decl, 0);
6985             }
6986           warn_if_shadowing (decl);
6987         }
6988       /* If no declaration found, default to int.  */
6989       else
6990         {
6991           decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
6992           DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
6993           DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
6994           pushdecl (decl);
6995           warn_if_shadowing (decl);
6996
6997           if (flag_isoc99)
6998             pedwarn ("type of %q+D defaults to %<int%>", decl);
6999           else if (extra_warnings)
7000             warning (OPT_Wextra, "type of %q+D defaults to %<int%>", decl);
7001         }
7002
7003       TREE_PURPOSE (parm) = decl;
7004       pointer_set_insert (seen_args, decl);
7005     }
7006
7007   /* Now examine the parms chain for incomplete declarations
7008      and declarations with no corresponding names.  */
7009
7010   for (b = current_scope->bindings; b; b = b->prev)
7011     {
7012       parm = b->decl;
7013       if (TREE_CODE (parm) != PARM_DECL)
7014         continue;
7015
7016       if (TREE_TYPE (parm) != error_mark_node
7017           && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
7018         {
7019           error ("parameter %q+D has incomplete type", parm);
7020           TREE_TYPE (parm) = error_mark_node;
7021         }
7022
7023       if (!pointer_set_contains (seen_args, parm))
7024         {
7025           error ("declaration for parameter %q+D but no such parameter", parm);
7026
7027           /* Pretend the parameter was not missing.
7028              This gets us to a standard state and minimizes
7029              further error messages.  */
7030           parmids = chainon (parmids, tree_cons (parm, 0, 0));
7031         }
7032     }
7033
7034   /* Chain the declarations together in the order of the list of
7035      names.  Store that chain in the function decl, replacing the
7036      list of names.  Update the current scope to match.  */
7037   DECL_ARGUMENTS (fndecl) = 0;
7038
7039   for (parm = parmids; parm; parm = TREE_CHAIN (parm))
7040     if (TREE_PURPOSE (parm))
7041       break;
7042   if (parm && TREE_PURPOSE (parm))
7043     {
7044       last = TREE_PURPOSE (parm);
7045       DECL_ARGUMENTS (fndecl) = last;
7046
7047       for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
7048         if (TREE_PURPOSE (parm))
7049           {
7050             TREE_CHAIN (last) = TREE_PURPOSE (parm);
7051             last = TREE_PURPOSE (parm);
7052           }
7053       TREE_CHAIN (last) = 0;
7054     }
7055
7056   pointer_set_destroy (seen_args);
7057
7058   /* If there was a previous prototype,
7059      set the DECL_ARG_TYPE of each argument according to
7060      the type previously specified, and report any mismatches.  */
7061
7062   if (current_function_prototype_arg_types)
7063     {
7064       tree type;
7065       for (parm = DECL_ARGUMENTS (fndecl),
7066              type = current_function_prototype_arg_types;
7067            parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
7068                              != void_type_node));
7069            parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
7070         {
7071           if (parm == 0 || type == 0
7072               || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
7073             {
7074               if (current_function_prototype_built_in)
7075                 warning (0, "number of arguments doesn%'t match "
7076                          "built-in prototype");
7077               else
7078                 {
7079                   error ("number of arguments doesn%'t match prototype");
7080                   error ("%Hprototype declaration",
7081                          &current_function_prototype_locus);
7082                 }
7083               break;
7084             }
7085           /* Type for passing arg must be consistent with that
7086              declared for the arg.  ISO C says we take the unqualified
7087              type for parameters declared with qualified type.  */
7088           if (!comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
7089                           TYPE_MAIN_VARIANT (TREE_VALUE (type))))
7090             {
7091               if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
7092                   == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
7093                 {
7094                   /* Adjust argument to match prototype.  E.g. a previous
7095                      `int foo(float);' prototype causes
7096                      `int foo(x) float x; {...}' to be treated like
7097                      `int foo(float x) {...}'.  This is particularly
7098                      useful for argument types like uid_t.  */
7099                   DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
7100
7101                   if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
7102                       && INTEGRAL_TYPE_P (TREE_TYPE (parm))
7103                       && TYPE_PRECISION (TREE_TYPE (parm))
7104                       < TYPE_PRECISION (integer_type_node))
7105                     DECL_ARG_TYPE (parm) = integer_type_node;
7106
7107                   if (pedantic)
7108                     {
7109                       /* ??? Is it possible to get here with a
7110                          built-in prototype or will it always have
7111                          been diagnosed as conflicting with an
7112                          old-style definition and discarded?  */
7113                       if (current_function_prototype_built_in)
7114                         warning (0, "promoted argument %qD "
7115                                  "doesn%'t match built-in prototype", parm);
7116                       else
7117                         {
7118                           pedwarn ("promoted argument %qD "
7119                                    "doesn%'t match prototype", parm);
7120                           pedwarn ("%Hprototype declaration",
7121                                    &current_function_prototype_locus);
7122                         }
7123                     }
7124                 }
7125               else
7126                 {
7127                   if (current_function_prototype_built_in)
7128                     warning (0, "argument %qD doesn%'t match "
7129                              "built-in prototype", parm);
7130                   else
7131                     {
7132                       error ("argument %qD doesn%'t match prototype", parm);
7133                       error ("%Hprototype declaration",
7134                              &current_function_prototype_locus);
7135                     }
7136                 }
7137             }
7138         }
7139       TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
7140     }
7141
7142   /* Otherwise, create a prototype that would match.  */
7143
7144   else
7145     {
7146       tree actual = 0, last = 0, type;
7147
7148       for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
7149         {
7150           type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
7151           if (last)
7152             TREE_CHAIN (last) = type;
7153           else
7154             actual = type;
7155           last = type;
7156         }
7157       type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
7158       if (last)
7159         TREE_CHAIN (last) = type;
7160       else
7161         actual = type;
7162
7163       /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
7164          of the type of this function, but we need to avoid having this
7165          affect the types of other similarly-typed functions, so we must
7166          first force the generation of an identical (but separate) type
7167          node for the relevant function type.  The new node we create
7168          will be a variant of the main variant of the original function
7169          type.  */
7170
7171       TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
7172
7173       TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
7174     }
7175 }
7176
7177 /* Store parameter declarations passed in ARG_INFO into the current
7178    function declaration.  */
7179
7180 void
7181 store_parm_decls_from (struct c_arg_info *arg_info)
7182 {
7183   current_function_arg_info = arg_info;
7184   store_parm_decls ();
7185 }
7186
7187 /* Store the parameter declarations into the current function declaration.
7188    This is called after parsing the parameter declarations, before
7189    digesting the body of the function.
7190
7191    For an old-style definition, construct a prototype out of the old-style
7192    parameter declarations and inject it into the function's type.  */
7193
7194 void
7195 store_parm_decls (void)
7196 {
7197   tree fndecl = current_function_decl;
7198   bool proto;
7199
7200   /* The argument information block for FNDECL.  */
7201   struct c_arg_info *arg_info = current_function_arg_info;
7202   current_function_arg_info = 0;
7203
7204   /* True if this definition is written with a prototype.  Note:
7205      despite C99 6.7.5.3p14, we can *not* treat an empty argument
7206      list in a function definition as equivalent to (void) -- an
7207      empty argument list specifies the function has no parameters,
7208      but only (void) sets up a prototype for future calls.  */
7209   proto = arg_info->types != 0;
7210
7211   if (proto)
7212     store_parm_decls_newstyle (fndecl, arg_info);
7213   else
7214     store_parm_decls_oldstyle (fndecl, arg_info);
7215
7216   /* The next call to push_scope will be a function body.  */
7217
7218   next_is_function_body = true;
7219
7220   /* Write a record describing this function definition to the prototypes
7221      file (if requested).  */
7222
7223   gen_aux_info_record (fndecl, 1, 0, proto);
7224
7225   /* Initialize the RTL code for the function.  */
7226   allocate_struct_function (fndecl);
7227
7228   /* Begin the statement tree for this function.  */
7229   DECL_SAVED_TREE (fndecl) = push_stmt_list ();
7230
7231   /* ??? Insert the contents of the pending sizes list into the function
7232      to be evaluated.  The only reason left to have this is
7233         void foo(int n, int array[n++])
7234      because we throw away the array type in favor of a pointer type, and
7235      thus won't naturally see the SAVE_EXPR containing the increment.  All
7236      other pending sizes would be handled by gimplify_parameters.  */
7237   {
7238     tree t;
7239     for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t))
7240       add_stmt (TREE_VALUE (t));
7241   }
7242
7243   /* Even though we're inside a function body, we still don't want to
7244      call expand_expr to calculate the size of a variable-sized array.
7245      We haven't necessarily assigned RTL to all variables yet, so it's
7246      not safe to try to expand expressions involving them.  */
7247   cfun->x_dont_save_pending_sizes_p = 1;
7248 }
7249 \f
7250 /* Emit diagnostics that require gimple input for detection.  Operate on
7251    FNDECL and all its nested functions.  */
7252
7253 static void
7254 c_gimple_diagnostics_recursively (tree fndecl)
7255 {
7256   struct cgraph_node *cgn;
7257
7258   /* Handle attribute((warn_unused_result)).  Relies on gimple input.  */
7259   c_warn_unused_result (&DECL_SAVED_TREE (fndecl));
7260
7261   /* Notice when OpenMP structured block constraints are violated.  */
7262   if (flag_openmp)
7263     diagnose_omp_structured_block_errors (fndecl);
7264
7265   /* Finalize all nested functions now.  */
7266   cgn = cgraph_node (fndecl);
7267   for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
7268     c_gimple_diagnostics_recursively (cgn->decl);
7269 }
7270
7271 /* Finish up a function declaration and compile that function
7272    all the way to assembler language output.  The free the storage
7273    for the function definition.
7274
7275    This is called after parsing the body of the function definition.  */
7276
7277 void
7278 finish_function (void)
7279 {
7280   tree fndecl = current_function_decl;
7281
7282   label_context_stack_se = label_context_stack_se->next;
7283   label_context_stack_vm = label_context_stack_vm->next;
7284
7285   if (TREE_CODE (fndecl) == FUNCTION_DECL
7286       && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
7287     {
7288       tree args = DECL_ARGUMENTS (fndecl);
7289       for (; args; args = TREE_CHAIN (args))
7290         {
7291           tree type = TREE_TYPE (args);
7292           if (INTEGRAL_TYPE_P (type)
7293               && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
7294             DECL_ARG_TYPE (args) = integer_type_node;
7295         }
7296     }
7297
7298   if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
7299     BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
7300
7301   /* Must mark the RESULT_DECL as being in this function.  */
7302
7303   if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
7304     DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
7305
7306   if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
7307     {
7308       if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
7309           != integer_type_node)
7310         {
7311           /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
7312              If warn_main is -1 (-Wno-main) we don't want to be warned.  */
7313           if (!warn_main)
7314             pedwarn ("return type of %q+D is not %<int%>", fndecl);
7315         }
7316       else
7317         {
7318           if (flag_isoc99)
7319             {
7320               tree stmt = c_finish_return (integer_zero_node);
7321 #ifdef USE_MAPPED_LOCATION
7322               /* Hack.  We don't want the middle-end to warn that this return
7323                  is unreachable, so we mark its location as special.  Using
7324                  UNKNOWN_LOCATION has the problem that it gets clobbered in
7325                  annotate_one_with_locus.  A cleaner solution might be to
7326                  ensure ! should_carry_locus_p (stmt), but that needs a flag.
7327               */
7328               SET_EXPR_LOCATION (stmt, BUILTINS_LOCATION);
7329 #else
7330               /* Hack.  We don't want the middle-end to warn that this
7331                  return is unreachable, so put the statement on the
7332                  special line 0.  */
7333               annotate_with_file_line (stmt, input_filename, 0);
7334 #endif
7335             }
7336         }
7337     }
7338
7339   /* Tie off the statement tree for this function.  */
7340   DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
7341
7342   finish_fname_decls ();
7343
7344   /* Complain if there's just no return statement.  */
7345   if (warn_return_type
7346       && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
7347       && !current_function_returns_value && !current_function_returns_null
7348       /* Don't complain if we are no-return.  */
7349       && !current_function_returns_abnormally
7350       /* Don't warn for main().  */
7351       && !MAIN_NAME_P (DECL_NAME (fndecl))
7352       /* Or if they didn't actually specify a return type.  */
7353       && !C_FUNCTION_IMPLICIT_INT (fndecl)
7354       /* Normally, with -Wreturn-type, flow will complain.  Unless we're an
7355          inline function, as we might never be compiled separately.  */
7356       && DECL_INLINE (fndecl))
7357     {
7358       warning (OPT_Wreturn_type,
7359                "no return statement in function returning non-void");
7360       TREE_NO_WARNING (fndecl) = 1;
7361     }
7362
7363   /* With just -Wextra, complain only if function returns both with
7364      and without a value.  */
7365   if (extra_warnings
7366       && current_function_returns_value
7367       && current_function_returns_null)
7368     warning (OPT_Wextra, "this function may return with or without a value");
7369
7370   /* Store the end of the function, so that we get good line number
7371      info for the epilogue.  */
7372   cfun->function_end_locus = input_location;
7373
7374   /* If we don't have ctors/dtors sections, and this is a static
7375      constructor or destructor, it must be recorded now.  */
7376   if (DECL_STATIC_CONSTRUCTOR (fndecl)
7377       && !targetm.have_ctors_dtors)
7378     static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
7379   if (DECL_STATIC_DESTRUCTOR (fndecl)
7380       && !targetm.have_ctors_dtors)
7381     static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
7382
7383   /* Finalize the ELF visibility for the function.  */
7384   c_determine_visibility (fndecl);
7385
7386   /* Genericize before inlining.  Delay genericizing nested functions
7387      until their parent function is genericized.  Since finalizing
7388      requires GENERIC, delay that as well.  */
7389
7390   if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
7391       && !undef_nested_function)
7392     {
7393       if (!decl_function_context (fndecl))
7394         {
7395           c_genericize (fndecl);
7396           c_gimple_diagnostics_recursively (fndecl);
7397
7398           /* ??? Objc emits functions after finalizing the compilation unit.
7399              This should be cleaned up later and this conditional removed.  */
7400           if (cgraph_global_info_ready)
7401             {
7402               c_expand_body (fndecl);
7403               return;
7404             }
7405
7406           cgraph_finalize_function (fndecl, false);
7407         }
7408       else
7409         {
7410           /* Register this function with cgraph just far enough to get it
7411             added to our parent's nested function list.  Handy, since the
7412             C front end doesn't have such a list.  */
7413           (void) cgraph_node (fndecl);
7414         }
7415     }
7416
7417   if (!decl_function_context (fndecl))
7418     undef_nested_function = false;
7419
7420   /* We're leaving the context of this function, so zap cfun.
7421      It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
7422      tree_rest_of_compilation.  */
7423   cfun = NULL;
7424   current_function_decl = NULL;
7425 }
7426
7427 /* Generate the RTL for the body of FNDECL.  */
7428
7429 void
7430 c_expand_body (tree fndecl)
7431 {
7432
7433   if (!DECL_INITIAL (fndecl)
7434       || DECL_INITIAL (fndecl) == error_mark_node)
7435     return;
7436
7437   tree_rest_of_compilation (fndecl);
7438
7439   if (DECL_STATIC_CONSTRUCTOR (fndecl)
7440       && targetm.have_ctors_dtors)
7441     targetm.asm_out.constructor (XEXP (DECL_RTL (fndecl), 0),
7442                                  DEFAULT_INIT_PRIORITY);
7443   if (DECL_STATIC_DESTRUCTOR (fndecl)
7444       && targetm.have_ctors_dtors)
7445     targetm.asm_out.destructor (XEXP (DECL_RTL (fndecl), 0),
7446                                 DEFAULT_INIT_PRIORITY);
7447 }
7448 \f
7449 /* Check the declarations given in a for-loop for satisfying the C99
7450    constraints.  If exactly one such decl is found, return it.  */
7451
7452 tree
7453 check_for_loop_decls (void)
7454 {
7455   struct c_binding *b;
7456   tree one_decl = NULL_TREE;
7457   int n_decls = 0;
7458
7459
7460   if (!flag_isoc99)
7461     {
7462       /* If we get here, declarations have been used in a for loop without
7463          the C99 for loop scope.  This doesn't make much sense, so don't
7464          allow it.  */
7465       error ("%<for%> loop initial declaration used outside C99 mode");
7466       return NULL_TREE;
7467     }
7468   /* C99 subclause 6.8.5 paragraph 3:
7469
7470        [#3]  The  declaration  part  of  a for statement shall only
7471        declare identifiers for objects having storage class auto or
7472        register.
7473
7474      It isn't clear whether, in this sentence, "identifiers" binds to
7475      "shall only declare" or to "objects" - that is, whether all identifiers
7476      declared must be identifiers for objects, or whether the restriction
7477      only applies to those that are.  (A question on this in comp.std.c
7478      in November 2000 received no answer.)  We implement the strictest
7479      interpretation, to avoid creating an extension which later causes
7480      problems.  */
7481
7482   for (b = current_scope->bindings; b; b = b->prev)
7483     {
7484       tree id = b->id;
7485       tree decl = b->decl;
7486
7487       if (!id)
7488         continue;
7489
7490       switch (TREE_CODE (decl))
7491         {
7492         case VAR_DECL:
7493           if (TREE_STATIC (decl))
7494             error ("declaration of static variable %q+D in %<for%> loop "
7495                    "initial declaration", decl);
7496           else if (DECL_EXTERNAL (decl))
7497             error ("declaration of %<extern%> variable %q+D in %<for%> loop "
7498                    "initial declaration", decl);
7499           break;
7500
7501         case RECORD_TYPE:
7502           error ("%<struct %E%> declared in %<for%> loop initial declaration",
7503                  id);
7504           break;
7505         case UNION_TYPE:
7506           error ("%<union %E%> declared in %<for%> loop initial declaration",
7507                  id);
7508           break;
7509         case ENUMERAL_TYPE:
7510           error ("%<enum %E%> declared in %<for%> loop initial declaration",
7511                  id);
7512           break;
7513         /* APPLE LOCAL begin radar 6268817 */
7514          case FUNCTION_DECL:
7515         /* Block helper function can be declared in the statement block
7516          for the for-loop declarations. */
7517         if (BLOCK_SYNTHESIZED_FUNC (decl))
7518                 break;
7519         /* APPLE LOCAL end radar 6268817 */
7520         default:
7521           error ("declaration of non-variable %q+D in %<for%> loop "
7522                  "initial declaration", decl);
7523         }
7524
7525       n_decls++;
7526       one_decl = decl;
7527     }
7528
7529   return n_decls == 1 ? one_decl : NULL_TREE;
7530 }
7531 \f
7532 /* Save and reinitialize the variables
7533    used during compilation of a C function.  */
7534
7535 void
7536 c_push_function_context (struct function *f)
7537 {
7538   struct language_function *p;
7539   p = GGC_NEW (struct language_function);
7540   f->language = p;
7541
7542   p->base.x_stmt_tree = c_stmt_tree;
7543   p->x_break_label = c_break_label;
7544   p->x_cont_label = c_cont_label;
7545   p->x_switch_stack = c_switch_stack;
7546   p->arg_info = current_function_arg_info;
7547   p->returns_value = current_function_returns_value;
7548   p->returns_null = current_function_returns_null;
7549   p->returns_abnormally = current_function_returns_abnormally;
7550   p->warn_about_return_type = warn_about_return_type;
7551 }
7552
7553 /* Restore the variables used during compilation of a C function.  */
7554
7555 void
7556 c_pop_function_context (struct function *f)
7557 {
7558   struct language_function *p = f->language;
7559
7560   /* APPLE LOCAL begin blocks 6040305 */
7561   if (current_function_decl
7562       && DECL_STRUCT_FUNCTION (current_function_decl) == 0
7563       /* APPLE LOCAL end blocks 6040305 */
7564       && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
7565     {
7566       /* Stop pointing to the local nodes about to be freed.  */
7567       /* But DECL_INITIAL must remain nonzero so we know this
7568          was an actual function definition.  */
7569       DECL_INITIAL (current_function_decl) = error_mark_node;
7570       DECL_ARGUMENTS (current_function_decl) = 0;
7571     }
7572
7573   c_stmt_tree = p->base.x_stmt_tree;
7574   c_break_label = p->x_break_label;
7575   c_cont_label = p->x_cont_label;
7576   c_switch_stack = p->x_switch_stack;
7577   current_function_arg_info = p->arg_info;
7578   current_function_returns_value = p->returns_value;
7579   current_function_returns_null = p->returns_null;
7580   current_function_returns_abnormally = p->returns_abnormally;
7581   warn_about_return_type = p->warn_about_return_type;
7582
7583   f->language = NULL;
7584 }
7585
7586 /* Copy the DECL_LANG_SPECIFIC data associated with DECL.  */
7587
7588 void
7589 c_dup_lang_specific_decl (tree decl)
7590 {
7591   struct lang_decl *ld;
7592
7593   if (!DECL_LANG_SPECIFIC (decl))
7594     return;
7595
7596   ld = GGC_NEW (struct lang_decl);
7597   memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
7598   DECL_LANG_SPECIFIC (decl) = ld;
7599 }
7600
7601 /* The functions below are required for functionality of doing
7602    function at once processing in the C front end. Currently these
7603    functions are not called from anywhere in the C front end, but as
7604    these changes continue, that will change.  */
7605
7606 /* Returns the stmt_tree (if any) to which statements are currently
7607    being added.  If there is no active statement-tree, NULL is
7608    returned.  */
7609
7610 stmt_tree
7611 current_stmt_tree (void)
7612 {
7613   return &c_stmt_tree;
7614 }
7615
7616 /* Nonzero if TYPE is an anonymous union or struct type.  Always 0 in
7617    C.  */
7618
7619 int
7620 anon_aggr_type_p (tree ARG_UNUSED (node))
7621 {
7622   return 0;
7623 }
7624
7625 /* Return the global value of T as a symbol.  */
7626
7627 tree
7628 identifier_global_value (tree t)
7629 {
7630   struct c_binding *b;
7631
7632   for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
7633     if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
7634       return b->decl;
7635
7636   return 0;
7637 }
7638
7639 /* Record a builtin type for C.  If NAME is non-NULL, it is the name used;
7640    otherwise the name is found in ridpointers from RID_INDEX.  */
7641
7642 void
7643 record_builtin_type (enum rid rid_index, const char *name, tree type)
7644 {
7645   tree id, decl;
7646   if (name == 0)
7647     id = ridpointers[(int) rid_index];
7648   else
7649     id = get_identifier (name);
7650   decl = build_decl (TYPE_DECL, id, type);
7651   pushdecl (decl);
7652   if (debug_hooks->type_decl)
7653     debug_hooks->type_decl (decl, false);
7654 }
7655
7656 /* Build the void_list_node (void_type_node having been created).  */
7657 tree
7658 build_void_list_node (void)
7659 {
7660   tree t = build_tree_list (NULL_TREE, void_type_node);
7661   return t;
7662 }
7663
7664 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR.  */
7665
7666 struct c_parm *
7667 build_c_parm (struct c_declspecs *specs, tree attrs,
7668               struct c_declarator *declarator)
7669 {
7670   struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
7671   ret->specs = specs;
7672   ret->attrs = attrs;
7673   ret->declarator = declarator;
7674   return ret;
7675 }
7676
7677 /* Return a declarator with nested attributes.  TARGET is the inner
7678    declarator to which these attributes apply.  ATTRS are the
7679    attributes.  */
7680
7681 struct c_declarator *
7682 build_attrs_declarator (tree attrs, struct c_declarator *target)
7683 {
7684   struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7685   ret->kind = cdk_attrs;
7686   ret->declarator = target;
7687   ret->u.attrs = attrs;
7688   return ret;
7689 }
7690
7691 /* Return a declarator for a function with arguments specified by ARGS
7692    and return type specified by TARGET.  */
7693
7694 struct c_declarator *
7695 build_function_declarator (struct c_arg_info *args,
7696                            struct c_declarator *target)
7697 {
7698   struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7699   ret->kind = cdk_function;
7700   ret->declarator = target;
7701   ret->u.arg_info = args;
7702   return ret;
7703 }
7704
7705 /* Return a declarator for the identifier IDENT (which may be
7706    NULL_TREE for an abstract declarator).  */
7707
7708 struct c_declarator *
7709 build_id_declarator (tree ident)
7710 {
7711   struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7712   ret->kind = cdk_id;
7713   ret->declarator = 0;
7714   ret->u.id = ident;
7715   /* Default value - may get reset to a more precise location. */
7716   ret->id_loc = input_location;
7717   return ret;
7718 }
7719
7720 /* Return something to represent absolute declarators containing a *.
7721    TARGET is the absolute declarator that the * contains.
7722    TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
7723    to apply to the pointer type.  */
7724
7725 struct c_declarator *
7726 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
7727                          struct c_declarator *target)
7728 {
7729   tree attrs;
7730   int quals = 0;
7731   struct c_declarator *itarget = target;
7732   struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7733   if (type_quals_attrs)
7734     {
7735       attrs = type_quals_attrs->attrs;
7736       quals = quals_from_declspecs (type_quals_attrs);
7737       if (attrs != NULL_TREE)
7738         itarget = build_attrs_declarator (attrs, target);
7739     }
7740   ret->kind = cdk_pointer;
7741   ret->declarator = itarget;
7742   ret->u.pointer_quals = quals;
7743   return ret;
7744 }
7745
7746 /* APPLE LOCAL begin radar 5932809 - copyable byref blocks (C++ ch) */
7747 /* build_byref_local_var_access - converts EXPR to:
7748    EXPR.__forwarding-><decl-name>.
7749 */
7750 tree
7751 build_byref_local_var_access (tree expr, tree decl_name)
7752 {
7753   tree exp = build_component_ref (expr, get_identifier ("__forwarding"));
7754   exp = build_indirect_ref (exp, "unary *");
7755   exp = build_component_ref (exp, decl_name);
7756   return exp;
7757 }
7758 /* APPLE LOCAL end radar 5932809 - copyable byref blocks (C++ ch) */
7759 /* APPLE LOCAL begin radar 5732232 - blocks (C++ ch) */
7760 /**
7761   build_block_byref_decl - This routine inserts a variable declared as a
7762   'byref' variable using the |...| syntax in helper function's outer-most scope.
7763 */
7764 tree
7765 build_block_byref_decl (tree name, tree decl, tree exp)
7766 {
7767   struct c_scope *scope = current_scope;
7768   tree ptr_type, byref_decl;
7769   /* APPLE LOCAL begin radar 6225809 */
7770   if (cur_block->prev_block_info) {
7771     /* Traverse enclosing blocks. Insert a __block variable in
7772      each enclosing block which has no declaration of this
7773      variable. This is to ensure that the current (inner) block
7774      gets the __block version of the variable; */
7775     struct block_sema_info *cb = cur_block->prev_block_info;
7776     while (cb) {
7777       struct c_binding *b = I_SYMBOL_BINDING (name);
7778       /* Find the first declaration not in current block. */
7779       while (b && b->decl
7780               && (TREE_CODE (b->decl) == VAR_DECL
7781                   || TREE_CODE (b->decl) == PARM_DECL)
7782               && b->depth >= cur_block->the_scope->depth)
7783          b = b->shadowed;
7784       
7785       /* Is the next declaration not in enclosing block? */
7786       if (b && b->decl
7787            && (TREE_CODE (b->decl) == VAR_DECL
7788                || TREE_CODE (b->decl) == PARM_DECL)
7789            && b->depth < cb->the_scope->depth) {
7790          /* No declaration of variable seen in the block. Must insert one. */
7791          struct c_scope *save_scope = current_scope;
7792          struct block_sema_info *save_current_block = cur_block;
7793          current_scope = cb->the_scope;
7794          cur_block = cb;
7795          decl = build_block_byref_decl (name, decl, exp);
7796          cur_block = save_current_block;
7797          current_scope = save_scope;
7798       }
7799       cb = cb->prev_block_info;
7800     }
7801   }
7802   /* APPLE LOCAL end radar 6225809 */
7803   
7804   /* If it is already a byref declaration, do not add the pointer type
7805      because such declarations already have the pointer type
7806      added. This happens when we have two nested byref declarations in
7807      nested blocks. */
7808   ptr_type = (TREE_CODE (decl) == VAR_DECL && BLOCK_DECL_BYREF (decl))
7809                ? TREE_TYPE (decl) : build_pointer_type (TREE_TYPE (decl));
7810   byref_decl = build_decl (VAR_DECL, name, ptr_type);
7811   /* APPLE LOCAL begin radars 6144664 & 6145471  */
7812   DECL_SOURCE_LOCATION (byref_decl) = DECL_SOURCE_LOCATION 
7813                                                 (cur_block->helper_func_decl);
7814   /* APPLE LOCAL end radars 6144664 & 6145471  */
7815   BLOCK_DECL_BYREF (byref_decl) = 1;
7816
7817   /* APPLE LOCAL begin radar 5932809 - copyable byref blocks (C++ ch) */
7818   if (TREE_CODE (decl) == VAR_DECL && COPYABLE_BYREF_LOCAL_VAR (decl))
7819     {
7820       COPYABLE_BYREF_LOCAL_VAR (byref_decl) = 1;
7821       COPYABLE_BYREF_LOCAL_NONPOD (byref_decl) = COPYABLE_BYREF_LOCAL_NONPOD (decl);
7822       /* APPLE LOCAL radar 5847976 */
7823       COPYABLE_WEAK_BLOCK (byref_decl) = COPYABLE_WEAK_BLOCK (decl);
7824     }
7825   /* APPLE LOCAL end radar 5932809 - copyable byref blocks (C++ ch) */
7826
7827   /* In the presence of nested "{" 
7828      move up the scope chain until reaching the main function body's scope.
7829   */
7830   while (scope && !scope->function_body)
7831     scope = scope->outer;
7832   /* Current scope must be that of the main function body. */
7833   gcc_assert (scope && scope->function_body);
7834   bind (name, byref_decl,
7835          scope, /*invisible=*/false, /*nested=*/false);
7836   cur_block->block_byref_decl_list =
7837     tree_cons (NULL_TREE, byref_decl, cur_block->block_byref_decl_list);
7838   /* APPLE LOCAL radar 5847213 - building block_original_byref_decl_list list removed. */
7839   /* APPLE LOCAL begin radar 6289031 */
7840 #if 0
7841   if (! flag_objc_gc_only)
7842 #endif
7843     push_cleanup (byref_decl, build_block_byref_release_exp (byref_decl), false);
7844   /* APPLE LOCAL end radar 6289031 */
7845
7846   return byref_decl;
7847 }
7848
7849 /**
7850   build_block_ref_decl - This routine inserts a copied-in variable (a variable
7851   referenced in the block but whose scope is outside the block) in helper
7852   function's outer-most scope. It also sets its type to 'const' as such
7853   variables are read-only.
7854 */
7855 tree
7856 build_block_ref_decl (tree name, tree decl)
7857 {
7858   struct c_scope *scope = current_scope;
7859   tree ref_decl;
7860   /* APPLE LOCAL radar 6212722 */
7861   tree type, exp;
7862   /* APPLE LOCAL begin radar 5932809 - copyable byref blocks (C++ ch) */
7863   /* 'decl' was previously declared as __block. Simply, copy the value
7864      embedded in the above variable. */
7865   if (TREE_CODE (decl) == VAR_DECL && COPYABLE_BYREF_LOCAL_VAR (decl))
7866     decl = build_byref_local_var_access (decl, DECL_NAME (decl));
7867   else {
7868     /* APPLE LOCAL begin radar 5988451 (C++ ch) */
7869     if (cur_block->prev_block_info) {
7870       /* Traverse enclosing blocks. Insert a copied-in variable in
7871           each enclosing block which has no declaration of this
7872           variable. This is to ensure that the current (inner) block
7873           has the 'frozen' value of the copied-in variable; which means
7874           the value of the copied in variable is at the point of the
7875           block declaration and *not* when the inner block is
7876           invoked.  */
7877       struct block_sema_info *cb = cur_block->prev_block_info;
7878       while (cb) {
7879          struct c_binding *b = I_SYMBOL_BINDING (name);
7880          /* Find the first declaration not in current block. */
7881          while (b && b->decl
7882                 && (TREE_CODE (b->decl) == VAR_DECL
7883                     || TREE_CODE (b->decl) == PARM_DECL)
7884                 && b->depth >= cur_block->the_scope->depth)
7885            b = b->shadowed;
7886
7887          /* Is the next declaration not in enclosing block? */
7888          if (b && b->decl
7889              && (TREE_CODE (b->decl) == VAR_DECL
7890                  || TREE_CODE (b->decl) == PARM_DECL)
7891              && b->depth < cb->the_scope->depth) {
7892            /* No declaration of variable seen in the block. Must insert one,
7893               so it 'freezes' the variable in this block. */
7894            struct c_scope *save_scope = current_scope;
7895            struct block_sema_info *save_current_block = cur_block;
7896            current_scope = cb->the_scope;
7897            cur_block = cb;
7898            decl = build_block_ref_decl (name, decl);
7899            cur_block = save_current_block;
7900            current_scope = save_scope;
7901          }
7902          cb = cb->prev_block_info;
7903       }
7904     }
7905     /* APPLE LOCAL end radar 5988451 (C++ ch) */
7906   }
7907   /* APPLE LOCAL end radar 5932809 - copyable byref blocks (C++ ch) */
7908   /* APPLE LOCAL begin radar 6212722 */
7909   exp = decl;
7910   type = TREE_TYPE (exp);
7911   if (TREE_CODE (type) == ARRAY_TYPE) {
7912     exp = array_to_pointer_conversion (decl);
7913     type = TREE_TYPE (exp);
7914   }
7915   else if (TREE_CODE (type) == FUNCTION_TYPE) {
7916     exp = function_to_pointer_conversion (exp);
7917     type = TREE_TYPE (exp);
7918   }
7919   ref_decl = build_decl (VAR_DECL, name,
7920                           build_qualified_type (type, TYPE_QUAL_CONST));
7921   /* APPLE LOCAL end radar 6212722 */
7922   /* APPLE LOCAL begin radars 6144664 & 6145471  */
7923   DECL_SOURCE_LOCATION (ref_decl) = DECL_SOURCE_LOCATION 
7924                                                 (cur_block->helper_func_decl);
7925   /* APPLE LOCAL end radars 6144664 & 6145471  */
7926   DECL_INITIAL (ref_decl) = error_mark_node;
7927   /* APPLE LOCAL radar 5805175 - blocks (C++ ch) */
7928   c_apply_type_quals_to_decl (TYPE_QUAL_CONST, ref_decl);
7929   BLOCK_DECL_COPIED (ref_decl) = 1;
7930
7931   /* Find the scope for function body (outer-most scope) and insert
7932      this variable in that scope. This is to avoid duplicate
7933      declaration of the save variable. */
7934   while (scope && !scope->function_body)
7935     scope = scope->outer;
7936   /* We are enterring the copied-in variable in helper function's
7937      outer scope; that of its main body. */
7938   gcc_assert (scope);
7939   bind (name, ref_decl,
7940          scope, /*invisible=*/false, /*nested=*/false);
7941   cur_block->block_ref_decl_list =
7942     tree_cons (NULL_TREE, ref_decl, cur_block->block_ref_decl_list);
7943   cur_block->block_original_ref_decl_list =
7944     /* APPLE LOCAL radar 6212722 */
7945     tree_cons (NULL_TREE, exp, cur_block->block_original_ref_decl_list);
7946   return ref_decl;
7947 }
7948
7949 /* APPLE LOCAL begin radar 5847213 - radar 6329245 */
7950 static GTY (())  tree descriptor_ptr_type;
7951 static GTY (())  tree descriptor_ptr_type_with_copydispose;
7952 /** build_block_descriptor_type - This routine builds following internal type:
7953  struct __block_descriptor {
7954  unsigned long int reserved;     // NULL
7955  unsigned long int Size;  // sizeof(struct Block_literal_1)
7956  
7957  // optional helper functions
7958  void *CopyFuncPtr; // When BLOCK_HAS_COPY_DISPOSE is set (withCopyDispose true)
7959  void *DestroyFuncPtr; // When BLOCK_HAS_COPY_DISPOSE is set (withCopyDispose true)
7960 } *descriptor_ptr_type;
7961
7962 Objects of this type will always be static. This is one main component of abi change.
7963 */
7964 tree
7965 build_block_descriptor_type (bool withCopyDispose)
7966 {
7967   tree field_decl_chain, field_decl;
7968   tree main_type;
7969   
7970   if (withCopyDispose && descriptor_ptr_type_with_copydispose)
7971     return descriptor_ptr_type_with_copydispose;
7972   if (!withCopyDispose && descriptor_ptr_type)
7973     return descriptor_ptr_type;
7974   
7975   main_type = 
7976     withCopyDispose ? 
7977       start_struct (RECORD_TYPE, get_identifier ("__block_descriptor_withcopydispose"))
7978       : start_struct (RECORD_TYPE, get_identifier ("__block_descriptor"));
7979   
7980   /* unsigned long int reserved; */
7981   field_decl = build_decl (FIELD_DECL, get_identifier ("reserved"), long_unsigned_type_node);
7982   field_decl_chain = field_decl;
7983   
7984   /* unsigned long int Size; */
7985   field_decl = build_decl (FIELD_DECL, get_identifier ("Size"), long_unsigned_type_node);
7986   chainon (field_decl_chain, field_decl);
7987   
7988   if (withCopyDispose)
7989   {
7990     /* void *CopyFuncPtr; */
7991     field_decl = build_decl (FIELD_DECL, get_identifier ("CopyFuncPtr"), ptr_type_node);
7992     chainon (field_decl_chain, field_decl);
7993     /* void *DestroyFuncPtr; */
7994     field_decl = build_decl (FIELD_DECL, get_identifier ("DestroyFuncPtr"), ptr_type_node);
7995     chainon (field_decl_chain, field_decl);
7996   }
7997   
7998    /* Mark this struct as being a block struct rather than a 'normal'
7999       struct.  */
8000   TYPE_BLOCK_IMPL_STRUCT (main_type) = 1;
8001   finish_struct (main_type, field_decl_chain, NULL_TREE);
8002
8003   main_type = build_pointer_type (main_type);
8004   if (withCopyDispose)
8005     descriptor_ptr_type_with_copydispose = main_type;
8006   else
8007     descriptor_ptr_type = main_type;
8008   return main_type;
8009 }
8010 /* APPLE LOCAL end radar 5847213 - radar 6329245 */
8011
8012 /* APPLE LOCAL begin radar 5814025 (C++ ch) */
8013 struct c_declarator *
8014 make_block_pointer_declarator (struct c_declspecs *type_quals_attrs,
8015                                   struct c_declarator *target)
8016 {
8017   int quals = 0;
8018   struct c_declarator *itarget = target;
8019   struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
8020
8021   if (type_quals_attrs)
8022   {
8023     tree attrs = type_quals_attrs->attrs;
8024     quals = quals_from_declspecs (type_quals_attrs);
8025     if (attrs != NULL_TREE)
8026       itarget = build_attrs_declarator (attrs, target);
8027   }
8028   ret->kind = cdk_block_pointer;
8029   /* APPLE LOCAL radar 5882266 (C++ ch) */
8030   ret->declarator = itarget;
8031   ret->u.pointer_quals = quals;
8032   return ret;
8033 }
8034 /* APPLE LOCAL end radar 5814025 (C++ ch) */
8035
8036 tree
8037 begin_block (void)
8038 {
8039   struct block_sema_info *csi;
8040 #if 0
8041   push_scope ();
8042 #endif
8043   csi = (struct block_sema_info*)xcalloc (1, sizeof (struct block_sema_info));
8044   csi->prev_block_info = cur_block;
8045   cur_block = csi;
8046   return NULL_TREE;
8047 }
8048
8049 struct block_sema_info *
8050 finish_block (tree block __attribute__ ((__unused__)))
8051 {
8052   struct block_sema_info *csi = cur_block;
8053   cur_block = cur_block->prev_block_info;
8054 #if 0
8055   pop_scope ();
8056 #endif
8057   return csi;
8058 }
8059
8060 bool
8061 in_imm_block (void)
8062 {
8063   /* APPLE LOCAL radar 5988451 (C++ ch) */
8064   return (cur_block && cur_block->the_scope == current_scope);
8065 }
8066
8067 /* This routine returns 'true' if 'name' has a declaration inside the
8068    current block, 'false' otherwise.  If 'name' has no declaration in
8069    the current block, it returns in DECL the user declaration for
8070    'name' found in the enclosing scope.  Note that if it is declared
8071    in current declaration, it can be either a user declaration or a
8072    byref/copied-in declaration added in current block's scope by the
8073    compiler.  */
8074 bool
8075 lookup_name_in_block (tree name, tree *decl)
8076 {
8077   if (cur_block)
8078     {
8079       struct c_binding *b = I_SYMBOL_BINDING (name);
8080       if (b->depth >= cur_block->the_scope->depth)
8081         return true;
8082
8083       /* Check for common case of block nested inside a non-block. */
8084       if (!cur_block->prev_block_info)
8085         return false;
8086       /* Check for less common case of nested blocks. */
8087       /* Declaration not in current block. Find the first user
8088          declaration of 'name' in outer scope. */
8089       /* APPLE LOCAL begin radar 5988451 (C++ ch) */
8090       /* Check for variables only, as we may have parameters, such as
8091          'self' */
8092       /* Note that if a copied-in variable (BLOCK_DECL_COPIED) in the
8093           enclosing block is found, it must be returned as this is
8094           where the variable in current (nested block) will have to get
8095           its value. */
8096       while (b && b->decl
8097              && (TREE_CODE (b->decl) == VAR_DECL)
8098               && BLOCK_DECL_BYREF (b->decl))
8099          b = b->shadowed;
8100         /* APPLE LOCAL end radar 5988451 (C++ ch) */
8101       if (b && b->decl)
8102         *decl = b->decl;
8103     }
8104   return false;
8105 }
8106
8107 static struct c_scope *save_current_scope;
8108 static tree save_current_function_decl;
8109 void
8110 push_to_top_level (void)
8111 {
8112   save_current_scope = current_scope;
8113   save_current_function_decl = current_function_decl;
8114   current_scope = file_scope;
8115   current_function_decl = NULL_TREE;
8116 }
8117
8118 void
8119 pop_from_top_level (void)
8120 {
8121   current_scope = save_current_scope;
8122   current_function_decl = save_current_function_decl;
8123 }
8124
8125 /**
8126   build_helper_func_decl - This routine builds a FUNCTION_DECL for
8127   a block helper function.
8128 */
8129 tree
8130 build_helper_func_decl (tree ident, tree type)
8131 {
8132   tree func_decl = build_decl (FUNCTION_DECL, ident, type);
8133   DECL_EXTERNAL (func_decl) = 0;
8134   TREE_PUBLIC (func_decl) = 0;
8135   TREE_USED (func_decl) = 1;
8136   TREE_NOTHROW (func_decl) = 0;
8137   /* APPLE LOCAL radar 6172148 */
8138   BLOCK_SYNTHESIZED_FUNC (func_decl) = 1;
8139   return func_decl;
8140 }
8141
8142 /**
8143  start_block_helper_function - This is a light-weight version of start_function().
8144  It has removed all the fuss in the start_function().
8145  */
8146 void
8147 start_block_helper_function (tree decl1)
8148 {
8149   struct c_label_context_se *nstack_se;
8150   struct c_label_context_vm *nstack_vm;
8151   tree restype, resdecl;
8152
8153   current_function_returns_value = 0;  /* Assume, until we see it does.  */
8154   current_function_returns_null = 0;
8155   current_function_returns_abnormally = 0;
8156   warn_about_return_type = 0;
8157   c_switch_stack = NULL;
8158
8159   nstack_se = XOBNEW (&parser_obstack, struct c_label_context_se);
8160   nstack_se->labels_def = NULL;
8161   nstack_se->labels_used = NULL;
8162   nstack_se->next = label_context_stack_se;
8163   label_context_stack_se = nstack_se;
8164
8165   nstack_vm = XOBNEW (&parser_obstack, struct c_label_context_vm);
8166   nstack_vm->labels_def = NULL;
8167   nstack_vm->labels_used = NULL;
8168   nstack_vm->scope = 0;
8169   nstack_vm->next = label_context_stack_vm;
8170   label_context_stack_vm = nstack_vm;
8171
8172   /* Indicate no valid break/continue context by setting these variables
8173    to some non-null, non-label value.  We'll notice and emit the proper
8174    error message in c_finish_bc_stmt.  */
8175   c_break_label = c_cont_label = size_zero_node;
8176
8177   announce_function (decl1);
8178
8179   /* Make the init_value nonzero so pushdecl knows this is not tentative.
8180    error_mark_node is replaced below (in pop_scope) with the BLOCK.  */
8181   DECL_INITIAL (decl1) = error_mark_node;
8182
8183   current_function_prototype_locus = UNKNOWN_LOCATION;
8184   current_function_prototype_built_in = false;
8185   current_function_prototype_arg_types = NULL_TREE;
8186
8187   /* This function exists in static storage.
8188    (This does not mean `static' in the C sense!)  */
8189   TREE_STATIC (decl1) = 1;
8190   /* A helper function is not global */
8191   TREE_PUBLIC (decl1) = 0;
8192
8193   /* This is the earliest point at which we might know the assembler
8194    name of the function.  Thus, if it's set before this, die horribly.  */
8195   gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
8196   current_function_decl = pushdecl (decl1);
8197
8198   push_scope ();
8199   declare_parm_level ();
8200
8201   restype = TREE_TYPE (TREE_TYPE (current_function_decl));
8202   resdecl = build_decl (RESULT_DECL, NULL_TREE, restype);
8203   DECL_ARTIFICIAL (resdecl) = 1;
8204   DECL_IGNORED_P (resdecl) = 1;
8205   DECL_RESULT (current_function_decl) = resdecl;
8206
8207   start_fname_decls ();
8208 }
8209
8210 /**
8211  declare_block_prologue_local_vars - utility routine to do the actual
8212  declaration and initialization for each referecned block variable.
8213 */
8214 static void
8215 declare_block_prologue_local_vars (tree self_parm, tree component,
8216                                    tree stmt)
8217 {
8218   tree decl, block_component;
8219   tree_stmt_iterator i;
8220   tree decl_stmt;
8221
8222   decl = component;
8223   block_component = build_component_ref (build_indirect_ref (self_parm, "->"),
8224                                          DECL_NAME (component));
8225   gcc_assert (block_component);
8226   DECL_EXTERNAL (decl) = 0;
8227   TREE_STATIC (decl) = 0;
8228   TREE_USED (decl) = 1;
8229   DECL_CONTEXT (decl) = current_function_decl;
8230   DECL_ARTIFICIAL (decl) = 1;
8231   DECL_INITIAL (decl) = block_component;
8232   /* Prepend a DECL_EXPR statement to the statement list. */
8233   i = tsi_start (stmt);
8234   decl_stmt = build_stmt (DECL_EXPR, decl);
8235   /* APPLE LOCAL Radar 5811961, Fix location of block prologue vars (C++ ch) */
8236   SET_EXPR_LOCATION (decl_stmt, DECL_SOURCE_LOCATION (decl));
8237   /* APPLE LOCAL begin radar 6163705, Blocks prologues  */
8238   /* Give the prologue statements a line number of one before the beginning of
8239      the function, to make them easily identifiable later.  */
8240   EXPR_LINENO (decl_stmt) =  DECL_SOURCE_LINE (decl) - 1;
8241   /* APPLE LOCAL end radar 6163705, Blocks prologues  */
8242   tsi_link_before (&i, decl_stmt, TSI_SAME_STMT);
8243 }
8244
8245 /**
8246  block_build_prologue
8247  - This routine builds the declarations for the
8248  variables referenced in the block; as in:
8249  int *y = .block_descriptor->y;
8250  int x = .block_descriptor->x;
8251
8252  The decl_expr declaration for each initialization is enterred at the
8253  beginning of the helper function's statement-list which is passed
8254  in block_impl->block_body.
8255 */
8256 void
8257 block_build_prologue (struct block_sema_info *block_impl)
8258 {
8259   tree chain;
8260   /* APPLE LOCAL radar 6404979 */
8261   tree self_parm = lookup_name (get_identifier (".block_descriptor"));
8262   gcc_assert (self_parm);
8263
8264   for (chain = block_impl->block_ref_decl_list; chain;
8265         chain = TREE_CHAIN (chain))
8266     declare_block_prologue_local_vars (self_parm, TREE_VALUE (chain),
8267                                        block_impl->block_body);
8268
8269   for (chain = block_impl->block_byref_decl_list; chain;
8270         chain = TREE_CHAIN (chain))
8271     declare_block_prologue_local_vars (self_parm, TREE_VALUE (chain),
8272                                        block_impl->block_body);
8273 }
8274 /* APPLE LOCAL end radar 5732232 - blocks (C++ ch) */
8275
8276 /* Return a pointer to a structure for an empty list of declaration
8277    specifiers.  */
8278
8279 struct c_declspecs *
8280 build_null_declspecs (void)
8281 {
8282   struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
8283   ret->type = 0;
8284   ret->decl_attr = 0;
8285   ret->attrs = 0;
8286   ret->typespec_word = cts_none;
8287   ret->storage_class = csc_none;
8288   ret->declspecs_seen_p = false;
8289   ret->type_seen_p = false;
8290   ret->non_sc_seen_p = false;
8291   ret->typedef_p = false;
8292   ret->tag_defined_p = false;
8293   ret->explicit_signed_p = false;
8294   ret->deprecated_p = false;
8295   /* APPLE LOCAL "unavailable" attribute (radar 2809697) */
8296   ret->unavailable_p = false;
8297   ret->default_int_p = false;
8298   ret->long_p = false;
8299   ret->long_long_p = false;
8300   ret->short_p = false;
8301   ret->signed_p = false;
8302   ret->unsigned_p = false;
8303   ret->complex_p = false;
8304   ret->inline_p = false;
8305   ret->thread_p = false;
8306   ret->const_p = false;
8307   ret->volatile_p = false;
8308   ret->restrict_p = false;
8309   return ret;
8310 }
8311
8312 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
8313    returning SPECS.  */
8314
8315 struct c_declspecs *
8316 declspecs_add_qual (struct c_declspecs *specs, tree qual)
8317 {
8318   enum rid i;
8319   bool dupe = false;
8320   specs->non_sc_seen_p = true;
8321   specs->declspecs_seen_p = true;
8322   gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
8323               && C_IS_RESERVED_WORD (qual));
8324   i = C_RID_CODE (qual);
8325   switch (i)
8326     {
8327     case RID_CONST:
8328       dupe = specs->const_p;
8329       specs->const_p = true;
8330       break;
8331     case RID_VOLATILE:
8332       dupe = specs->volatile_p;
8333       specs->volatile_p = true;
8334       break;
8335     case RID_RESTRICT:
8336       dupe = specs->restrict_p;
8337       specs->restrict_p = true;
8338       break;
8339     default:
8340       gcc_unreachable ();
8341     }
8342   if (dupe && pedantic && !flag_isoc99)
8343     pedwarn ("duplicate %qE", qual);
8344   return specs;
8345 }
8346
8347 /* Add the type specifier TYPE to the declaration specifiers SPECS,
8348    returning SPECS.  */
8349
8350 struct c_declspecs *
8351 declspecs_add_type (struct c_declspecs *specs, struct c_typespec spec)
8352 {
8353   tree type = spec.spec;
8354   specs->non_sc_seen_p = true;
8355   specs->declspecs_seen_p = true;
8356   specs->type_seen_p = true;
8357   if (TREE_DEPRECATED (type))
8358     specs->deprecated_p = true;
8359
8360   /* APPLE LOCAL begin "unavailable" attribute (radar 2809697) */
8361   if (TREE_UNAVAILABLE (type))
8362     specs->unavailable_p = true;
8363   /* APPLE LOCAL end "unavailable" attribute (radar 2809697) */
8364
8365   /* Handle type specifier keywords.  */
8366   if (TREE_CODE (type) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (type))
8367     {
8368       enum rid i = C_RID_CODE (type);
8369       if (specs->type)
8370         {
8371           error ("two or more data types in declaration specifiers");
8372           return specs;
8373         }
8374       if ((int) i <= (int) RID_LAST_MODIFIER)
8375         {
8376           /* "long", "short", "signed", "unsigned" or "_Complex".  */
8377           bool dupe = false;
8378           switch (i)
8379             {
8380             case RID_LONG:
8381               if (specs->long_long_p)
8382                 {
8383                   error ("%<long long long%> is too long for GCC");
8384                   break;
8385                 }
8386               if (specs->long_p)
8387                 {
8388                   if (specs->typespec_word == cts_double)
8389                     {
8390                       error ("both %<long long%> and %<double%> in "
8391                              "declaration specifiers");
8392                       break;
8393                     }
8394                   if (pedantic && !flag_isoc99 && !in_system_header
8395                       && warn_long_long)
8396                     pedwarn ("ISO C90 does not support %<long long%>");
8397                   specs->long_long_p = 1;
8398                   break;
8399                 }
8400               if (specs->short_p)
8401                 error ("both %<long%> and %<short%> in "
8402                        "declaration specifiers");
8403               else if (specs->typespec_word == cts_void)
8404                 error ("both %<long%> and %<void%> in "
8405                        "declaration specifiers");
8406               else if (specs->typespec_word == cts_bool)
8407                 error ("both %<long%> and %<_Bool%> in "
8408                        "declaration specifiers");
8409               else if (specs->typespec_word == cts_char)
8410                 error ("both %<long%> and %<char%> in "
8411                        "declaration specifiers");
8412               else if (specs->typespec_word == cts_float)
8413                 error ("both %<long%> and %<float%> in "
8414                        "declaration specifiers");
8415               else if (specs->typespec_word == cts_dfloat32)
8416                 error ("both %<long%> and %<_Decimal32%> in "
8417                        "declaration specifiers");
8418               else if (specs->typespec_word == cts_dfloat64)
8419                 error ("both %<long%> and %<_Decimal64%> in "
8420                        "declaration specifiers");
8421               else if (specs->typespec_word == cts_dfloat128)
8422                 error ("both %<long%> and %<_Decimal128%> in "
8423                        "declaration specifiers");
8424               else
8425                 specs->long_p = true;
8426               break;
8427             case RID_SHORT:
8428               dupe = specs->short_p;
8429               if (specs->long_p)
8430                 error ("both %<long%> and %<short%> in "
8431                        "declaration specifiers");
8432               else if (specs->typespec_word == cts_void)
8433                 error ("both %<short%> and %<void%> in "
8434                        "declaration specifiers");
8435               else if (specs->typespec_word == cts_bool)
8436                 error ("both %<short%> and %<_Bool%> in "
8437                        "declaration specifiers");
8438               else if (specs->typespec_word == cts_char)
8439                 error ("both %<short%> and %<char%> in "
8440                        "declaration specifiers");
8441               else if (specs->typespec_word == cts_float)
8442                 error ("both %<short%> and %<float%> in "
8443                        "declaration specifiers");
8444               else if (specs->typespec_word == cts_double)
8445                 error ("both %<short%> and %<double%> in "
8446                        "declaration specifiers");
8447               else if (specs->typespec_word == cts_dfloat32)
8448                 error ("both %<short%> and %<_Decimal32%> in "
8449                        "declaration specifiers");
8450               else if (specs->typespec_word == cts_dfloat64)
8451                 error ("both %<short%> and %<_Decimal64%> in "
8452                                         "declaration specifiers");
8453               else if (specs->typespec_word == cts_dfloat128)
8454                 error ("both %<short%> and %<_Decimal128%> in "
8455                        "declaration specifiers");
8456               else
8457                 specs->short_p = true;
8458               break;
8459             case RID_SIGNED:
8460               dupe = specs->signed_p;
8461               if (specs->unsigned_p)
8462                 error ("both %<signed%> and %<unsigned%> in "
8463                        "declaration specifiers");
8464               else if (specs->typespec_word == cts_void)
8465                 error ("both %<signed%> and %<void%> in "
8466                        "declaration specifiers");
8467               else if (specs->typespec_word == cts_bool)
8468                 error ("both %<signed%> and %<_Bool%> in "
8469                        "declaration specifiers");
8470               else if (specs->typespec_word == cts_float)
8471                 error ("both %<signed%> and %<float%> in "
8472                        "declaration specifiers");
8473               else if (specs->typespec_word == cts_double)
8474                 error ("both %<signed%> and %<double%> in "
8475                        "declaration specifiers");
8476               else if (specs->typespec_word == cts_dfloat32)
8477                 error ("both %<signed%> and %<_Decimal32%> in "
8478                        "declaration specifiers");
8479               else if (specs->typespec_word == cts_dfloat64)
8480                 error ("both %<signed%> and %<_Decimal64%> in "
8481                        "declaration specifiers");
8482               else if (specs->typespec_word == cts_dfloat128)
8483                 error ("both %<signed%> and %<_Decimal128%> in "
8484                        "declaration specifiers");
8485               else
8486                 specs->signed_p = true;
8487               break;
8488             case RID_UNSIGNED:
8489               dupe = specs->unsigned_p;
8490               if (specs->signed_p)
8491                 error ("both %<signed%> and %<unsigned%> in "
8492                        "declaration specifiers");
8493               else if (specs->typespec_word == cts_void)
8494                 error ("both %<unsigned%> and %<void%> in "
8495                        "declaration specifiers");
8496               else if (specs->typespec_word == cts_bool)
8497                 error ("both %<unsigned%> and %<_Bool%> in "
8498                        "declaration specifiers");
8499               else if (specs->typespec_word == cts_float)
8500                 error ("both %<unsigned%> and %<float%> in "
8501                        "declaration specifiers");
8502               else if (specs->typespec_word == cts_double)
8503                 error ("both %<unsigned%> and %<double%> in "
8504                        "declaration specifiers");
8505               else if (specs->typespec_word == cts_dfloat32)
8506                 error ("both %<unsigned%> and %<_Decimal32%> in "
8507                        "declaration specifiers");
8508               else if (specs->typespec_word == cts_dfloat64)
8509                 error ("both %<unsigned%> and %<_Decimal64%> in "
8510                        "declaration specifiers");
8511               else if (specs->typespec_word == cts_dfloat128)
8512                 error ("both %<unsigned%> and %<_Decimal128%> in "
8513                        "declaration specifiers");
8514               else
8515                 specs->unsigned_p = true;
8516               break;
8517             case RID_COMPLEX:
8518               dupe = specs->complex_p;
8519               if (pedantic && !flag_isoc99 && !in_system_header)
8520                 pedwarn ("ISO C90 does not support complex types");
8521               if (specs->typespec_word == cts_void)
8522                 error ("both %<complex%> and %<void%> in "
8523                        "declaration specifiers");
8524               else if (specs->typespec_word == cts_bool)
8525                 error ("both %<complex%> and %<_Bool%> in "
8526                        "declaration specifiers");
8527               else if (specs->typespec_word == cts_dfloat32)
8528                 error ("both %<complex%> and %<_Decimal32%> in "
8529                        "declaration specifiers");
8530               else if (specs->typespec_word == cts_dfloat64)
8531                 error ("both %<complex%> and %<_Decimal64%> in "
8532                        "declaration specifiers");
8533               else if (specs->typespec_word == cts_dfloat128)
8534                 error ("both %<complex%> and %<_Decimal128%> in "
8535                        "declaration specifiers");
8536               else
8537                 specs->complex_p = true;
8538               break;
8539             default:
8540               gcc_unreachable ();
8541             }
8542
8543           if (dupe)
8544             error ("duplicate %qE", type);
8545
8546           return specs;
8547         }
8548       else
8549         {
8550           /* "void", "_Bool", "char", "int", "float" or "double".  */
8551           if (specs->typespec_word != cts_none)
8552             {
8553               error ("two or more data types in declaration specifiers");
8554               return specs;
8555             }
8556           switch (i)
8557             {
8558             case RID_VOID:
8559               if (specs->long_p)
8560                 error ("both %<long%> and %<void%> in "
8561                        "declaration specifiers");
8562               else if (specs->short_p)
8563                 error ("both %<short%> and %<void%> in "
8564                        "declaration specifiers");
8565               else if (specs->signed_p)
8566                 error ("both %<signed%> and %<void%> in "
8567                        "declaration specifiers");
8568               else if (specs->unsigned_p)
8569                 error ("both %<unsigned%> and %<void%> in "
8570                        "declaration specifiers");
8571               else if (specs->complex_p)
8572                 error ("both %<complex%> and %<void%> in "
8573                        "declaration specifiers");
8574               else
8575                 specs->typespec_word = cts_void;
8576               return specs;
8577             case RID_BOOL:
8578               if (specs->long_p)
8579                 error ("both %<long%> and %<_Bool%> in "
8580                        "declaration specifiers");
8581               else if (specs->short_p)
8582                 error ("both %<short%> and %<_Bool%> in "
8583                        "declaration specifiers");
8584               else if (specs->signed_p)
8585                 error ("both %<signed%> and %<_Bool%> in "
8586                        "declaration specifiers");
8587               else if (specs->unsigned_p)
8588                 error ("both %<unsigned%> and %<_Bool%> in "
8589                        "declaration specifiers");
8590               else if (specs->complex_p)
8591                 error ("both %<complex%> and %<_Bool%> in "
8592                        "declaration specifiers");
8593               else
8594                 specs->typespec_word = cts_bool;
8595               return specs;
8596             case RID_CHAR:
8597               if (specs->long_p)
8598                 error ("both %<long%> and %<char%> in "
8599                        "declaration specifiers");
8600               else if (specs->short_p)
8601                 error ("both %<short%> and %<char%> in "
8602                        "declaration specifiers");
8603               else
8604                 specs->typespec_word = cts_char;
8605               return specs;
8606             case RID_INT:
8607               specs->typespec_word = cts_int;
8608               return specs;
8609             case RID_FLOAT:
8610               if (specs->long_p)
8611                 error ("both %<long%> and %<float%> in "
8612                        "declaration specifiers");
8613               else if (specs->short_p)
8614                 error ("both %<short%> and %<float%> in "
8615                        "declaration specifiers");
8616               else if (specs->signed_p)
8617                 error ("both %<signed%> and %<float%> in "
8618                        "declaration specifiers");
8619               else if (specs->unsigned_p)
8620                 error ("both %<unsigned%> and %<float%> in "
8621                        "declaration specifiers");
8622               else
8623                 specs->typespec_word = cts_float;
8624               return specs;
8625             case RID_DOUBLE:
8626               if (specs->long_long_p)
8627                 error ("both %<long long%> and %<double%> in "
8628                        "declaration specifiers");
8629               else if (specs->short_p)
8630                 error ("both %<short%> and %<double%> in "
8631                        "declaration specifiers");
8632               else if (specs->signed_p)
8633                 error ("both %<signed%> and %<double%> in "
8634                        "declaration specifiers");
8635               else if (specs->unsigned_p)
8636                 error ("both %<unsigned%> and %<double%> in "
8637                        "declaration specifiers");
8638               else
8639                 specs->typespec_word = cts_double;
8640               return specs;
8641             case RID_DFLOAT32:
8642             case RID_DFLOAT64:
8643             case RID_DFLOAT128:
8644               { 
8645                 const char *str;
8646                 if (i == RID_DFLOAT32)
8647                   str = "_Decimal32";
8648                 else if (i == RID_DFLOAT64)
8649                   str = "_Decimal64";
8650                 else
8651                   str = "_Decimal128";
8652                 if (specs->long_long_p)
8653                   error ("both %<long long%> and %<%s%> in "
8654                          "declaration specifiers", str);
8655                 if (specs->long_p)
8656                   error ("both %<long%> and %<%s%> in "
8657                          "declaration specifiers", str);
8658                 else if (specs->short_p)
8659                   error ("both %<short%> and %<%s%> in "
8660                          "declaration specifiers", str);
8661                 else if (specs->signed_p)
8662                   error ("both %<signed%> and %<%s%> in "
8663                          "declaration specifiers", str);
8664                 else if (specs->unsigned_p)
8665                   error ("both %<unsigned%> and %<%s%> in "
8666                          "declaration specifiers", str);
8667                 else if (specs->complex_p)
8668                   error ("both %<complex%> and %<%s%> in "
8669                          "declaration specifiers", str);
8670                 else if (i == RID_DFLOAT32)
8671                   specs->typespec_word = cts_dfloat32;
8672                 else if (i == RID_DFLOAT64)
8673                   specs->typespec_word = cts_dfloat64;
8674                 else
8675                   specs->typespec_word = cts_dfloat128;
8676               }
8677               if (!targetm.decimal_float_supported_p ())
8678                 error ("decimal floating point not supported for this target");
8679               if (pedantic)
8680                 pedwarn ("ISO C does not support decimal floating point");
8681               return specs;
8682             default:
8683               /* ObjC reserved word "id", handled below.  */
8684               break;
8685             }
8686         }
8687     }
8688
8689   /* Now we have a typedef (a TYPE_DECL node), an identifier (some
8690      form of ObjC type, cases such as "int" and "long" being handled
8691      above), a TYPE (struct, union, enum and typeof specifiers) or an
8692      ERROR_MARK.  In none of these cases may there have previously
8693      been any type specifiers.  */
8694   if (specs->type || specs->typespec_word != cts_none
8695       || specs->long_p || specs->short_p || specs->signed_p
8696       || specs->unsigned_p || specs->complex_p)
8697     error ("two or more data types in declaration specifiers");
8698   else if (TREE_CODE (type) == TYPE_DECL)
8699     {
8700       if (TREE_TYPE (type) == error_mark_node)
8701         ; /* Allow the type to default to int to avoid cascading errors.  */
8702       else
8703         {
8704           specs->type = TREE_TYPE (type);
8705           specs->decl_attr = DECL_ATTRIBUTES (type);
8706           specs->typedef_p = true;
8707           specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
8708         }
8709     }
8710   else if (TREE_CODE (type) == IDENTIFIER_NODE)
8711     {
8712       tree t = lookup_name (type);
8713       if (!t || TREE_CODE (t) != TYPE_DECL)
8714         error ("%qE fails to be a typedef or built in type", type);
8715       else if (TREE_TYPE (t) == error_mark_node)
8716         ;
8717       else
8718         specs->type = TREE_TYPE (t);
8719     }
8720   else if (TREE_CODE (type) != ERROR_MARK)
8721     {
8722       if (spec.kind == ctsk_tagdef || spec.kind == ctsk_tagfirstref)
8723         specs->tag_defined_p = true;
8724       if (spec.kind == ctsk_typeof)
8725         specs->typedef_p = true;
8726       specs->type = type;
8727     }
8728
8729   return specs;
8730 }
8731
8732 /* Add the storage class specifier or function specifier SCSPEC to the
8733    declaration specifiers SPECS, returning SPECS.  */
8734
8735 struct c_declspecs *
8736 declspecs_add_scspec (struct c_declspecs *specs, tree scspec)
8737 {
8738   enum rid i;
8739   enum c_storage_class n = csc_none;
8740   bool dupe = false;
8741   specs->declspecs_seen_p = true;
8742   gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
8743               && C_IS_RESERVED_WORD (scspec));
8744   i = C_RID_CODE (scspec);
8745   if (extra_warnings && specs->non_sc_seen_p)
8746     warning (OPT_Wextra, "%qE is not at beginning of declaration", scspec);
8747   switch (i)
8748     {
8749     case RID_INLINE:
8750       /* C99 permits duplicate inline.  Although of doubtful utility,
8751          it seems simplest to permit it in gnu89 mode as well, as
8752          there is also little utility in maintaining this as a
8753          difference between gnu89 and C99 inline.  */
8754       dupe = false;
8755       specs->inline_p = true;
8756       break;
8757     case RID_THREAD:
8758       dupe = specs->thread_p;
8759       if (specs->storage_class == csc_auto)
8760         error ("%<__thread%> used with %<auto%>");
8761       else if (specs->storage_class == csc_register)
8762         error ("%<__thread%> used with %<register%>");
8763       else if (specs->storage_class == csc_typedef)
8764         error ("%<__thread%> used with %<typedef%>");
8765       else
8766         specs->thread_p = true;
8767       break;
8768     case RID_AUTO:
8769       n = csc_auto;
8770       break;
8771     case RID_EXTERN:
8772       n = csc_extern;
8773       /* Diagnose "__thread extern".  */
8774       if (specs->thread_p)
8775         error ("%<__thread%> before %<extern%>");
8776       break;
8777     case RID_REGISTER:
8778       n = csc_register;
8779       break;
8780     case RID_STATIC:
8781       n = csc_static;
8782       /* Diagnose "__thread static".  */
8783       if (specs->thread_p)
8784         error ("%<__thread%> before %<static%>");
8785       break;
8786     case RID_TYPEDEF:
8787       n = csc_typedef;
8788       break;
8789     default:
8790       gcc_unreachable ();
8791     }
8792   if (n != csc_none && n == specs->storage_class)
8793     dupe = true;
8794   if (dupe)
8795     error ("duplicate %qE", scspec);
8796   if (n != csc_none)
8797     {
8798       if (specs->storage_class != csc_none && n != specs->storage_class)
8799         {
8800           error ("multiple storage classes in declaration specifiers");
8801         }
8802       else
8803         {
8804           specs->storage_class = n;
8805           if (n != csc_extern && n != csc_static && specs->thread_p)
8806             {
8807               error ("%<__thread%> used with %qE", scspec);
8808               specs->thread_p = false;
8809             }
8810         }
8811     }
8812   return specs;
8813 }
8814
8815 /* Add the attributes ATTRS to the declaration specifiers SPECS,
8816    returning SPECS.  */
8817
8818 struct c_declspecs *
8819 declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
8820 {
8821   specs->attrs = chainon (attrs, specs->attrs);
8822   specs->declspecs_seen_p = true;
8823   return specs;
8824 }
8825
8826 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
8827    specifiers with any other type specifier to determine the resulting
8828    type.  This is where ISO C checks on complex types are made, since
8829    "_Complex long" is a prefix of the valid ISO C type "_Complex long
8830    double".  */
8831
8832 struct c_declspecs *
8833 finish_declspecs (struct c_declspecs *specs)
8834 {
8835   /* If a type was specified as a whole, we have no modifiers and are
8836      done.  */
8837   if (specs->type != NULL_TREE)
8838     {
8839       gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
8840                   && !specs->signed_p && !specs->unsigned_p
8841                   && !specs->complex_p);
8842       return specs;
8843     }
8844
8845   /* If none of "void", "_Bool", "char", "int", "float" or "double"
8846      has been specified, treat it as "int" unless "_Complex" is
8847      present and there are no other specifiers.  If we just have
8848      "_Complex", it is equivalent to "_Complex double", but e.g.
8849      "_Complex short" is equivalent to "_Complex short int".  */
8850   if (specs->typespec_word == cts_none)
8851     {
8852       if (specs->long_p || specs->short_p
8853           || specs->signed_p || specs->unsigned_p)
8854         {
8855           specs->typespec_word = cts_int;
8856         }
8857       else if (specs->complex_p)
8858         {
8859           specs->typespec_word = cts_double;
8860           if (pedantic)
8861             pedwarn ("ISO C does not support plain %<complex%> meaning "
8862                      "%<double complex%>");
8863         }
8864       else
8865         {
8866           specs->typespec_word = cts_int;
8867           specs->default_int_p = true;
8868           /* We don't diagnose this here because grokdeclarator will
8869              give more specific diagnostics according to whether it is
8870              a function definition.  */
8871         }
8872     }
8873
8874   /* If "signed" was specified, record this to distinguish "int" and
8875      "signed int" in the case of a bit-field with
8876      -funsigned-bitfields.  */
8877   specs->explicit_signed_p = specs->signed_p;
8878
8879   /* Now compute the actual type.  */
8880   switch (specs->typespec_word)
8881     {
8882     case cts_void:
8883       gcc_assert (!specs->long_p && !specs->short_p
8884                   && !specs->signed_p && !specs->unsigned_p
8885                   && !specs->complex_p);
8886       specs->type = void_type_node;
8887       break;
8888     case cts_bool:
8889       gcc_assert (!specs->long_p && !specs->short_p
8890                   && !specs->signed_p && !specs->unsigned_p
8891                   && !specs->complex_p);
8892       specs->type = boolean_type_node;
8893       break;
8894     case cts_char:
8895       gcc_assert (!specs->long_p && !specs->short_p);
8896       gcc_assert (!(specs->signed_p && specs->unsigned_p));
8897       if (specs->signed_p)
8898         specs->type = signed_char_type_node;
8899       else if (specs->unsigned_p)
8900         specs->type = unsigned_char_type_node;
8901       else
8902         specs->type = char_type_node;
8903       if (specs->complex_p)
8904         {
8905           if (pedantic)
8906             pedwarn ("ISO C does not support complex integer types");
8907           specs->type = build_complex_type (specs->type);
8908         }
8909       break;
8910     case cts_int:
8911       gcc_assert (!(specs->long_p && specs->short_p));
8912       gcc_assert (!(specs->signed_p && specs->unsigned_p));
8913       if (specs->long_long_p)
8914         specs->type = (specs->unsigned_p
8915                        ? long_long_unsigned_type_node
8916                        : long_long_integer_type_node);
8917       else if (specs->long_p)
8918         specs->type = (specs->unsigned_p
8919                        ? long_unsigned_type_node
8920                        : long_integer_type_node);
8921       else if (specs->short_p)
8922         specs->type = (specs->unsigned_p
8923                        ? short_unsigned_type_node
8924                        : short_integer_type_node);
8925       else
8926         specs->type = (specs->unsigned_p
8927                        ? unsigned_type_node
8928                        : integer_type_node);
8929       if (specs->complex_p)
8930         {
8931           if (pedantic)
8932             pedwarn ("ISO C does not support complex integer types");
8933           specs->type = build_complex_type (specs->type);
8934         }
8935       break;
8936     case cts_float:
8937       gcc_assert (!specs->long_p && !specs->short_p
8938                   && !specs->signed_p && !specs->unsigned_p);
8939       specs->type = (specs->complex_p
8940                      ? complex_float_type_node
8941                      : float_type_node);
8942       break;
8943     case cts_double:
8944       gcc_assert (!specs->long_long_p && !specs->short_p
8945                   && !specs->signed_p && !specs->unsigned_p);
8946       if (specs->long_p)
8947         {
8948           specs->type = (specs->complex_p
8949                          ? complex_long_double_type_node
8950                          : long_double_type_node);
8951         }
8952       else
8953         {
8954           specs->type = (specs->complex_p
8955                          ? complex_double_type_node
8956                          : double_type_node);
8957         }
8958       break;
8959     case cts_dfloat32:
8960     case cts_dfloat64:
8961     case cts_dfloat128:
8962       gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
8963                   && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
8964       if (specs->typespec_word == cts_dfloat32)
8965         specs->type = dfloat32_type_node;
8966       else if (specs->typespec_word == cts_dfloat64)
8967         specs->type = dfloat64_type_node;
8968       else
8969         specs->type = dfloat128_type_node;
8970       break;
8971     default:
8972       gcc_unreachable ();
8973     }
8974
8975   return specs;
8976 }
8977
8978 /* Synthesize a function which calls all the global ctors or global
8979    dtors in this file.  This is only used for targets which do not
8980    support .ctors/.dtors sections.  FIXME: Migrate into cgraph.  */
8981 static void
8982 build_cdtor (int method_type, tree cdtors)
8983 {
8984   tree body = 0;
8985
8986   if (!cdtors)
8987     return;
8988
8989   for (; cdtors; cdtors = TREE_CHAIN (cdtors))
8990     append_to_statement_list (build_function_call (TREE_VALUE (cdtors), 0),
8991                               &body);
8992
8993   cgraph_build_static_cdtor (method_type, body, DEFAULT_INIT_PRIORITY);
8994 }
8995
8996 /* A subroutine of c_write_global_declarations.  Perform final processing
8997    on one file scope's declarations (or the external scope's declarations),
8998    GLOBALS.  */
8999
9000 static void
9001 c_write_global_declarations_1 (tree globals)
9002 {
9003   tree decl;
9004   bool reconsider;
9005
9006   /* Process the decls in the order they were written.  */
9007   for (decl = globals; decl; decl = TREE_CHAIN (decl))
9008     {
9009       /* Check for used but undefined static functions using the C
9010          standard's definition of "used", and set TREE_NO_WARNING so
9011          that check_global_declarations doesn't repeat the check.  */
9012       if (TREE_CODE (decl) == FUNCTION_DECL
9013           && DECL_INITIAL (decl) == 0
9014           && DECL_EXTERNAL (decl)
9015           && !TREE_PUBLIC (decl)
9016           && C_DECL_USED (decl))
9017         {
9018           pedwarn ("%q+F used but never defined", decl);
9019           TREE_NO_WARNING (decl) = 1;
9020         }
9021
9022       wrapup_global_declaration_1 (decl);
9023     }
9024
9025   do
9026     {
9027       reconsider = false;
9028       for (decl = globals; decl; decl = TREE_CHAIN (decl))
9029         reconsider |= wrapup_global_declaration_2 (decl);
9030     }
9031   while (reconsider);
9032
9033   for (decl = globals; decl; decl = TREE_CHAIN (decl))
9034     check_global_declaration_1 (decl);
9035 }
9036
9037 /* A subroutine of c_write_global_declarations Emit debug information for each
9038    of the declarations in GLOBALS.  */
9039
9040 static void
9041 c_write_global_declarations_2 (tree globals)
9042 {
9043   tree decl;
9044
9045   for (decl = globals; decl ; decl = TREE_CHAIN (decl))
9046     debug_hooks->global_decl (decl);
9047 }
9048
9049 /* Preserve the external declarations scope across a garbage collect.  */
9050 static GTY(()) tree ext_block;
9051
9052 void
9053 c_write_global_declarations (void)
9054 {
9055   tree t;
9056
9057   /* We don't want to do this if generating a PCH.  */
9058   if (pch_file)
9059     return;
9060
9061   /* Don't waste time on further processing if -fsyntax-only or we've
9062      encountered errors.  */
9063   if (flag_syntax_only || errorcount || sorrycount || cpp_errors (parse_in))
9064     return;
9065
9066   /* Close the external scope.  */
9067   ext_block = pop_scope ();
9068   external_scope = 0;
9069   gcc_assert (!current_scope);
9070
9071   if (ext_block)
9072     {
9073       tree tmp = BLOCK_VARS (ext_block);
9074       int flags;
9075       FILE * stream = dump_begin (TDI_tu, &flags);
9076       if (stream && tmp)
9077         {
9078           dump_node (tmp, flags & ~TDF_SLIM, stream);
9079           dump_end (TDI_tu, stream);
9080         }
9081     }
9082
9083   /* Process all file scopes in this compilation, and the external_scope,
9084      through wrapup_global_declarations and check_global_declarations.  */
9085   for (t = all_translation_units; t; t = TREE_CHAIN (t))
9086     c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
9087   c_write_global_declarations_1 (BLOCK_VARS (ext_block));
9088
9089   /* Generate functions to call static constructors and destructors
9090      for targets that do not support .ctors/.dtors sections.  These
9091      functions have magic names which are detected by collect2.  */
9092   build_cdtor ('I', static_ctors); static_ctors = 0;
9093   build_cdtor ('D', static_dtors); static_dtors = 0;
9094
9095   /* We're done parsing; proceed to optimize and emit assembly.
9096      FIXME: shouldn't be the front end's responsibility to call this.  */
9097   cgraph_optimize ();
9098
9099   /* After cgraph has had a chance to emit everything that's going to
9100      be emitted, output debug information for globals.  */
9101   if (errorcount == 0 && sorrycount == 0)
9102     {
9103       timevar_push (TV_SYMOUT);
9104       for (t = all_translation_units; t; t = TREE_CHAIN (t))
9105         c_write_global_declarations_2 (BLOCK_VARS (DECL_INITIAL (t)));
9106       c_write_global_declarations_2 (BLOCK_VARS (ext_block));
9107       timevar_pop (TV_SYMOUT);
9108     }
9109
9110   ext_block = NULL;
9111 }
9112
9113 /* APPLE LOCAL begin radar 5741070  */
9114
9115 /* Given an IDENTIFIER tree for a class interface, find (if possible) and
9116    return the record type for the class interface.  */
9117
9118 tree
9119 c_return_interface_record_type (tree typename)
9120 {
9121   enum tree_code_class class;
9122   enum tree_code code;
9123   tree retval = NULL;
9124
9125   if (typename == NULL)
9126     return retval;
9127
9128   code = TREE_CODE (typename);
9129   class = TREE_CODE_CLASS (code);
9130
9131   if (code != IDENTIFIER_NODE
9132       || class != tcc_exceptional)
9133     return retval;
9134
9135   retval = I_TAG_DECL (typename);
9136
9137   if (TREE_CODE (retval) != RECORD_TYPE)
9138     retval = NULL;
9139
9140   return retval;
9141 }
9142 /* APPLE LOCAL end radar 5741070  */
9143
9144 #include "gt-c-decl.h"