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