OSDN Git Service

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