OSDN Git Service

2005-06-28 Paul Brook <paul@codesourcery.com>
[pf3gnuchains/gcc-fork.git] / gcc / c-decl.c
1 /* Process declarations and variables for C compiler.
2    Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3    2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA.  */
21
22 /* Process declarations and symbol lookup for C front end.
23    Also constructs types; the standard scalar types at initialization,
24    and structure, union, array and enum types when they are declared.  */
25
26 /* ??? not all decl nodes are given the most useful possible
27    line numbers.  For example, the CONST_DECLs for enum values.  */
28
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "input.h"
33 #include "tm.h"
34 #include "intl.h"
35 #include "tree.h"
36 #include "tree-inline.h"
37 #include "rtl.h"
38 #include "flags.h"
39 #include "function.h"
40 #include "output.h"
41 #include "expr.h"
42 #include "c-tree.h"
43 #include "toplev.h"
44 #include "ggc.h"
45 #include "tm_p.h"
46 #include "cpplib.h"
47 #include "target.h"
48 #include "debug.h"
49 #include "opts.h"
50 #include "timevar.h"
51 #include "c-common.h"
52 #include "c-pragma.h"
53 #include "langhooks.h"
54 #include "tree-mudflap.h"
55 #include "tree-gimple.h"
56 #include "diagnostic.h"
57 #include "tree-dump.h"
58 #include "cgraph.h"
59 #include "hashtab.h"
60 #include "libfuncs.h"
61 #include "except.h"
62 #include "langhooks-def.h"
63
64 /* In grokdeclarator, distinguish syntactic contexts of declarators.  */
65 enum decl_context
66 { NORMAL,                       /* Ordinary declaration */
67   FUNCDEF,                      /* Function definition */
68   PARM,                         /* Declaration of parm before function body */
69   FIELD,                        /* Declaration inside struct or union */
70   TYPENAME};                    /* Typename (inside cast or sizeof)  */
71
72 \f
73 /* Nonzero if we have seen an invalid cross reference
74    to a struct, union, or enum, but not yet printed the message.  */
75 tree pending_invalid_xref;
76
77 /* File and line to appear in the eventual error message.  */
78 location_t pending_invalid_xref_location;
79
80 /* True means we've initialized exception handling.  */
81 bool c_eh_initialized_p;
82
83 /* While defining an enum type, this is 1 plus the last enumerator
84    constant value.  Note that will do not have to save this or `enum_overflow'
85    around nested function definition since such a definition could only
86    occur in an enum value expression and we don't use these variables in
87    that case.  */
88
89 static tree enum_next_value;
90
91 /* Nonzero means that there was overflow computing enum_next_value.  */
92
93 static int enum_overflow;
94
95 /* The file and line that the prototype came from if this is an
96    old-style definition; used for diagnostics in
97    store_parm_decls_oldstyle.  */
98
99 static location_t current_function_prototype_locus;
100
101 /* Whether this prototype was built-in.  */
102
103 static bool current_function_prototype_built_in;
104
105 /* The argument type information of this prototype.  */
106
107 static tree current_function_prototype_arg_types;
108
109 /* The argument information structure for the function currently being
110    defined.  */
111
112 static struct c_arg_info *current_function_arg_info;
113
114 /* The obstack on which parser and related data structures, which are
115    not live beyond their top-level declaration or definition, are
116    allocated.  */
117 struct obstack parser_obstack;
118
119 /* The current statement tree.  */
120
121 static GTY(()) struct stmt_tree_s c_stmt_tree;
122
123 /* State saving variables.  */
124 tree c_break_label;
125 tree c_cont_label;
126
127 /* Linked list of TRANSLATION_UNIT_DECLS for the translation units
128    included in this invocation.  Note that the current translation
129    unit is not included in this list.  */
130
131 static GTY(()) tree all_translation_units;
132
133 /* A list of decls to be made automatically visible in each file scope.  */
134 static GTY(()) tree visible_builtins;
135
136 /* Set to 0 at beginning of a function definition, set to 1 if
137    a return statement that specifies a return value is seen.  */
138
139 int current_function_returns_value;
140
141 /* Set to 0 at beginning of a function definition, set to 1 if
142    a return statement with no argument is seen.  */
143
144 int current_function_returns_null;
145
146 /* Set to 0 at beginning of a function definition, set to 1 if
147    a call to a noreturn function is seen.  */
148
149 int current_function_returns_abnormally;
150
151 /* Set to nonzero by `grokdeclarator' for a function
152    whose return type is defaulted, if warnings for this are desired.  */
153
154 static int warn_about_return_type;
155
156 /* Nonzero when starting a function declared `extern inline'.  */
157
158 static int current_extern_inline;
159
160 /* Nonzero when the current toplevel function contains a declaration
161    of a nested function which is never defined.  */
162
163 static bool undef_nested_function;
164
165 /* True means global_bindings_p should return false even if the scope stack
166    says we are in file scope.  */
167 bool c_override_global_bindings_to_false;
168
169 \f
170 /* Each c_binding structure describes one binding of an identifier to
171    a decl.  All the decls in a scope - irrespective of namespace - are
172    chained together by the ->prev field, which (as the name implies)
173    runs in reverse order.  All the decls in a given namespace bound to
174    a given identifier are chained by the ->shadowed field, which runs
175    from inner to outer scopes.
176
177    The ->decl field usually points to a DECL node, but there are two
178    exceptions.  In the namespace of type tags, the bound entity is a
179    RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node.  If an undeclared
180    identifier is encountered, it is bound to error_mark_node to
181    suppress further errors about that identifier in the current
182    function.
183
184    The ->type field stores the type of the declaration in this scope;
185    if NULL, the type is the type of the ->decl field.  This is only of
186    relevance for objects with external or internal linkage which may
187    be redeclared in inner scopes, forming composite types that only
188    persist for the duration of those scopes.  In the external scope,
189    this stores the composite of all the types declared for this
190    object, visible or not.  The ->inner_comp field (used only at file
191    scope) stores whether an incomplete array type at file scope was
192    completed at an inner scope to an array size other than 1.
193
194    The depth field is copied from the scope structure that holds this
195    decl.  It is used to preserve the proper ordering of the ->shadowed
196    field (see bind()) and also for a handful of special-case checks.
197    Finally, the invisible bit is true for a decl which should be
198    ignored for purposes of normal name lookup, and the nested bit is
199    true for a decl that's been bound a second time in an inner scope;
200    in all such cases, the binding in the outer scope will have its
201    invisible bit true.  */
202
203 struct c_binding GTY((chain_next ("%h.prev")))
204 {
205   tree decl;                    /* the decl bound */
206   tree type;                    /* the type in this scope */
207   tree id;                      /* the identifier it's bound to */
208   struct c_binding *prev;       /* the previous decl in this scope */
209   struct c_binding *shadowed;   /* the innermost decl shadowed by this one */
210   unsigned int depth : 28;      /* depth of this scope */
211   BOOL_BITFIELD invisible : 1;  /* normal lookup should ignore this binding */
212   BOOL_BITFIELD nested : 1;     /* do not set DECL_CONTEXT when popping */
213   BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
214   /* one free bit */
215 };
216 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
217 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
218 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
219 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
220
221 #define I_SYMBOL_BINDING(node) \
222   (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->symbol_binding)
223 #define I_SYMBOL_DECL(node) \
224  (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
225
226 #define I_TAG_BINDING(node) \
227   (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->tag_binding)
228 #define I_TAG_DECL(node) \
229  (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
230
231 #define I_LABEL_BINDING(node) \
232   (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->label_binding)
233 #define I_LABEL_DECL(node) \
234  (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
235
236 /* Each C symbol points to three linked lists of c_binding structures.
237    These describe the values of the identifier in the three different
238    namespaces defined by the language.  */
239
240 struct lang_identifier GTY(())
241 {
242   struct c_common_identifier common_id;
243   struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
244   struct c_binding *tag_binding;    /* struct/union/enum tags */
245   struct c_binding *label_binding;  /* labels */
246 };
247
248 /* Validate c-lang.c's assumptions.  */
249 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
250 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
251
252 /* The resulting tree type.  */
253
254 union lang_tree_node
255   GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
256        chain_next ("TREE_CODE (&%h.generic) == INTEGER_TYPE ? (union lang_tree_node *) TYPE_NEXT_VARIANT (&%h.generic) : (union lang_tree_node *) TREE_CHAIN (&%h.generic)")))
257 {
258   union tree_node GTY ((tag ("0"),
259                         desc ("tree_node_structure (&%h)")))
260     generic;
261   struct lang_identifier GTY ((tag ("1"))) identifier;
262 };
263
264 /* Each c_scope structure describes the complete contents of one
265    scope.  Four scopes are distinguished specially: the innermost or
266    current scope, the innermost function scope, the file scope (always
267    the second to outermost) and the outermost or external scope.
268
269    Most declarations are recorded in the current scope.
270
271    All normal label declarations are recorded in the innermost
272    function scope, as are bindings of undeclared identifiers to
273    error_mark_node.  (GCC permits nested functions as an extension,
274    hence the 'innermost' qualifier.)  Explicitly declared labels
275    (using the __label__ extension) appear in the current scope.
276
277    Being in the file scope (current_scope == file_scope) causes
278    special behavior in several places below.  Also, under some
279    conditions the Objective-C front end records declarations in the
280    file scope even though that isn't the current scope.
281
282    All declarations with external linkage are recorded in the external
283    scope, even if they aren't visible there; this models the fact that
284    such declarations are visible to the entire program, and (with a
285    bit of cleverness, see pushdecl) allows diagnosis of some violations
286    of C99 6.2.2p7 and 6.2.7p2:
287
288      If, within the same translation unit, the same identifier appears
289      with both internal and external linkage, the behavior is
290      undefined.
291
292      All declarations that refer to the same object or function shall
293      have compatible type; otherwise, the behavior is undefined.
294
295    Initially only the built-in declarations, which describe compiler
296    intrinsic functions plus a subset of the standard library, are in
297    this scope.
298
299    The order of the blocks list matters, and it is frequently appended
300    to.  To avoid having to walk all the way to the end of the list on
301    each insertion, or reverse the list later, we maintain a pointer to
302    the last list entry.  (FIXME: It should be feasible to use a reversed
303    list here.)
304
305    The bindings list is strictly in reverse order of declarations;
306    pop_scope relies on this.  */
307
308
309 struct c_scope GTY((chain_next ("%h.outer")))
310 {
311   /* The scope containing this one.  */
312   struct c_scope *outer;
313
314   /* The next outermost function scope.  */
315   struct c_scope *outer_function;
316
317   /* All bindings in this scope.  */
318   struct c_binding *bindings;
319
320   /* For each scope (except the global one), a chain of BLOCK nodes
321      for all the scopes that were entered and exited one level down.  */
322   tree blocks;
323   tree blocks_last;
324
325   /* The depth of this scope.  Used to keep the ->shadowed chain of
326      bindings sorted innermost to outermost.  */
327   unsigned int depth : 28;
328
329   /* True if we are currently filling this scope with parameter
330      declarations.  */
331   BOOL_BITFIELD parm_flag : 1;
332
333   /* True if we already complained about forward parameter decls
334      in this scope.  This prevents double warnings on
335      foo (int a; int b; ...)  */
336   BOOL_BITFIELD warned_forward_parm_decls : 1;
337
338   /* True if this is the outermost block scope of a function body.
339      This scope contains the parameters, the local variables declared
340      in the outermost block, and all the labels (except those in
341      nested functions, or declared at block scope with __label__).  */
342   BOOL_BITFIELD function_body : 1;
343
344   /* True means make a BLOCK for this scope no matter what.  */
345   BOOL_BITFIELD keep : 1;
346 };
347
348 /* The scope currently in effect.  */
349
350 static GTY(()) struct c_scope *current_scope;
351
352 /* The innermost function scope.  Ordinary (not explicitly declared)
353    labels, bindings to error_mark_node, and the lazily-created
354    bindings of __func__ and its friends get this scope.  */
355
356 static GTY(()) struct c_scope *current_function_scope;
357
358 /* The C file scope.  This is reset for each input translation unit.  */
359
360 static GTY(()) struct c_scope *file_scope;
361
362 /* The outermost scope.  This is used for all declarations with
363    external linkage, and only these, hence the name.  */
364
365 static GTY(()) struct c_scope *external_scope;
366
367 /* A chain of c_scope structures awaiting reuse.  */
368
369 static GTY((deletable)) struct c_scope *scope_freelist;
370
371 /* A chain of c_binding structures awaiting reuse.  */
372
373 static GTY((deletable)) struct c_binding *binding_freelist;
374
375 /* Append VAR to LIST in scope SCOPE.  */
376 #define SCOPE_LIST_APPEND(scope, list, decl) do {       \
377   struct c_scope *s_ = (scope);                         \
378   tree d_ = (decl);                                     \
379   if (s_->list##_last)                                  \
380     TREE_CHAIN (s_->list##_last) = d_;                  \
381   else                                                  \
382     s_->list = d_;                                      \
383   s_->list##_last = d_;                                 \
384 } while (0)
385
386 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE.  */
387 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do {        \
388   struct c_scope *t_ = (tscope);                                \
389   struct c_scope *f_ = (fscope);                                \
390   if (t_->to##_last)                                            \
391     TREE_CHAIN (t_->to##_last) = f_->from;                      \
392   else                                                          \
393     t_->to = f_->from;                                          \
394   t_->to##_last = f_->from##_last;                              \
395 } while (0)
396
397 /* True means unconditionally make a BLOCK for the next scope pushed.  */
398
399 static bool keep_next_level_flag;
400
401 /* True means the next call to push_scope will be the outermost scope
402    of a function body, so do not push a new scope, merely cease
403    expecting parameter decls.  */
404
405 static bool next_is_function_body;
406
407 /* Functions called automatically at the beginning and end of execution.  */
408
409 static GTY(()) tree static_ctors;
410 static GTY(()) tree static_dtors;
411
412 /* Forward declarations.  */
413 static tree lookup_name_in_scope (tree, struct c_scope *);
414 static tree c_make_fname_decl (tree, int);
415 static tree grokdeclarator (const struct c_declarator *,
416                             struct c_declspecs *,
417                             enum decl_context, bool, tree *);
418 static tree grokparms (struct c_arg_info *, bool);
419 static void layout_array_type (tree);
420 \f
421 /* T is a statement.  Add it to the statement-tree.  This is the
422    C/ObjC version--C++ has a slightly different version of this
423    function.  */
424
425 tree
426 add_stmt (tree t)
427 {
428   enum tree_code code = TREE_CODE (t);
429
430   if (EXPR_P (t) && code != LABEL_EXPR)
431     {
432       if (!EXPR_HAS_LOCATION (t))
433         SET_EXPR_LOCATION (t, input_location);
434     }
435
436   if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
437     STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
438
439   /* Add T to the statement-tree.  Non-side-effect statements need to be
440      recorded during statement expressions.  */
441   append_to_statement_list_force (t, &cur_stmt_list);
442
443   return t;
444 }
445 \f
446 /* States indicating how grokdeclarator() should handle declspecs marked
447    with __attribute__((deprecated)).  An object declared as
448    __attribute__((deprecated)) suppresses warnings of uses of other
449    deprecated items.  */
450
451 enum deprecated_states {
452   DEPRECATED_NORMAL,
453   DEPRECATED_SUPPRESS
454 };
455
456 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
457
458 void
459 c_print_identifier (FILE *file, tree node, int indent)
460 {
461   print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
462   print_node (file, "tag", I_TAG_DECL (node), indent + 4);
463   print_node (file, "label", I_LABEL_DECL (node), indent + 4);
464   if (C_IS_RESERVED_WORD (node))
465     {
466       tree rid = ridpointers[C_RID_CODE (node)];
467       indent_to (file, indent + 4);
468       fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
469                (void *) rid, IDENTIFIER_POINTER (rid));
470     }
471 }
472
473 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
474    which may be any of several kinds of DECL or TYPE or error_mark_node,
475    in the scope SCOPE.  */
476 static void
477 bind (tree name, tree decl, struct c_scope *scope, bool invisible, bool nested)
478 {
479   struct c_binding *b, **here;
480
481   if (binding_freelist)
482     {
483       b = binding_freelist;
484       binding_freelist = b->prev;
485     }
486   else
487     b = GGC_NEW (struct c_binding);
488
489   b->shadowed = 0;
490   b->decl = decl;
491   b->id = name;
492   b->depth = scope->depth;
493   b->invisible = invisible;
494   b->nested = nested;
495   b->inner_comp = 0;
496
497   b->type = 0;
498
499   b->prev = scope->bindings;
500   scope->bindings = b;
501
502   if (!name)
503     return;
504
505   switch (TREE_CODE (decl))
506     {
507     case LABEL_DECL:     here = &I_LABEL_BINDING (name);   break;
508     case ENUMERAL_TYPE:
509     case UNION_TYPE:
510     case RECORD_TYPE:    here = &I_TAG_BINDING (name);     break;
511     case VAR_DECL:
512     case FUNCTION_DECL:
513     case TYPE_DECL:
514     case CONST_DECL:
515     case PARM_DECL:
516     case ERROR_MARK:     here = &I_SYMBOL_BINDING (name);  break;
517
518     default:
519       gcc_unreachable ();
520     }
521
522   /* Locate the appropriate place in the chain of shadowed decls
523      to insert this binding.  Normally, scope == current_scope and
524      this does nothing.  */
525   while (*here && (*here)->depth > scope->depth)
526     here = &(*here)->shadowed;
527
528   b->shadowed = *here;
529   *here = b;
530 }
531
532 /* Clear the binding structure B, stick it on the binding_freelist,
533    and return the former value of b->prev.  This is used by pop_scope
534    and get_parm_info to iterate destructively over all the bindings
535    from a given scope.  */
536 static struct c_binding *
537 free_binding_and_advance (struct c_binding *b)
538 {
539   struct c_binding *prev = b->prev;
540
541   memset (b, 0, sizeof (struct c_binding));
542   b->prev = binding_freelist;
543   binding_freelist = b;
544
545   return prev;
546 }
547
548 \f
549 /* Hook called at end of compilation to assume 1 elt
550    for a file-scope tentative array defn that wasn't complete before.  */
551
552 void
553 c_finish_incomplete_decl (tree decl)
554 {
555   if (TREE_CODE (decl) == VAR_DECL)
556     {
557       tree type = TREE_TYPE (decl);
558       if (type != error_mark_node
559           && TREE_CODE (type) == ARRAY_TYPE
560           && !DECL_EXTERNAL (decl)
561           && TYPE_DOMAIN (type) == 0)
562         {
563           warning (0, "%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               default_init_unwind_resume_libfunc ();
3541               using_eh_for_cleanups ();
3542             }
3543
3544           push_cleanup (decl, cleanup, false);
3545         }
3546     }
3547 }
3548
3549 /* Given a parsed parameter declaration, decode it into a PARM_DECL.  */
3550
3551 tree
3552 grokparm (const struct c_parm *parm)
3553 {
3554   tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
3555                               NULL);
3556
3557   decl_attributes (&decl, parm->attrs, 0);
3558
3559   return decl;
3560 }
3561
3562 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3563    and push that on the current scope.  */
3564
3565 void
3566 push_parm_decl (const struct c_parm *parm)
3567 {
3568   tree decl;
3569
3570   decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL);
3571   decl_attributes (&decl, parm->attrs, 0);
3572
3573   decl = pushdecl (decl);
3574
3575   finish_decl (decl, NULL_TREE, NULL_TREE);
3576 }
3577
3578 /* Mark all the parameter declarations to date as forward decls.
3579    Also diagnose use of this extension.  */
3580
3581 void
3582 mark_forward_parm_decls (void)
3583 {
3584   struct c_binding *b;
3585
3586   if (pedantic && !current_scope->warned_forward_parm_decls)
3587     {
3588       pedwarn ("ISO C forbids forward parameter declarations");
3589       current_scope->warned_forward_parm_decls = true;
3590     }
3591
3592   for (b = current_scope->bindings; b; b = b->prev)
3593     if (TREE_CODE (b->decl) == PARM_DECL)
3594       TREE_ASM_WRITTEN (b->decl) = 1;
3595 }
3596 \f
3597 static GTY(()) int compound_literal_number;
3598
3599 /* Build a COMPOUND_LITERAL_EXPR.  TYPE is the type given in the compound
3600    literal, which may be an incomplete array type completed by the
3601    initializer; INIT is a CONSTRUCTOR that initializes the compound
3602    literal.  */
3603
3604 tree
3605 build_compound_literal (tree type, tree init)
3606 {
3607   /* We do not use start_decl here because we have a type, not a declarator;
3608      and do not use finish_decl because the decl should be stored inside
3609      the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR.  */
3610   tree decl;
3611   tree complit;
3612   tree stmt;
3613
3614   if (type == error_mark_node)
3615     return error_mark_node;
3616
3617   decl = build_decl (VAR_DECL, NULL_TREE, type);
3618   DECL_EXTERNAL (decl) = 0;
3619   TREE_PUBLIC (decl) = 0;
3620   TREE_STATIC (decl) = (current_scope == file_scope);
3621   DECL_CONTEXT (decl) = current_function_decl;
3622   TREE_USED (decl) = 1;
3623   TREE_TYPE (decl) = type;
3624   TREE_READONLY (decl) = TYPE_READONLY (type);
3625   store_init_value (decl, init);
3626
3627   if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3628     {
3629       int failure = complete_array_type (&TREE_TYPE (decl),
3630                                          DECL_INITIAL (decl), true);
3631       gcc_assert (!failure);
3632
3633       type = TREE_TYPE (decl);
3634       TREE_TYPE (DECL_INITIAL (decl)) = type;
3635     }
3636
3637   if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3638     return error_mark_node;
3639
3640   stmt = build_stmt (DECL_EXPR, decl);
3641   complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
3642   TREE_SIDE_EFFECTS (complit) = 1;
3643
3644   layout_decl (decl, 0);
3645
3646   if (TREE_STATIC (decl))
3647     {
3648       /* This decl needs a name for the assembler output.  We also need
3649          a unique suffix to be added to the name.  */
3650       char *name;
3651
3652       ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
3653                                compound_literal_number);
3654       compound_literal_number++;
3655       DECL_NAME (decl) = get_identifier (name);
3656       DECL_DEFER_OUTPUT (decl) = 1;
3657       DECL_COMDAT (decl) = 1;
3658       DECL_ARTIFICIAL (decl) = 1;
3659       DECL_IGNORED_P (decl) = 1;
3660       pushdecl (decl);
3661       rest_of_decl_compilation (decl, 1, 0);
3662     }
3663
3664   return complit;
3665 }
3666 \f
3667 /* Determine whether TYPE is a structure with a flexible array member,
3668    or a union containing such a structure (possibly recursively).  */
3669
3670 static bool
3671 flexible_array_type_p (tree type)
3672 {
3673   tree x;
3674   switch (TREE_CODE (type))
3675     {
3676     case RECORD_TYPE:
3677       x = TYPE_FIELDS (type);
3678       if (x == NULL_TREE)
3679         return false;
3680       while (TREE_CHAIN (x) != NULL_TREE)
3681         x = TREE_CHAIN (x);
3682       if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3683           && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3684           && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3685           && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3686         return true;
3687       return false;
3688     case UNION_TYPE:
3689       for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3690         {
3691           if (flexible_array_type_p (TREE_TYPE (x)))
3692             return true;
3693         }
3694       return false;
3695     default:
3696     return false;
3697   }
3698 }
3699 \f
3700 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3701    replacing with appropriate values if they are invalid.  */
3702 static void
3703 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3704 {
3705   tree type_mv;
3706   unsigned int max_width;
3707   unsigned HOST_WIDE_INT w;
3708   const char *name = orig_name ? orig_name: _("<anonymous>");
3709
3710   /* Detect and ignore out of range field width and process valid
3711      field widths.  */
3712   if (!INTEGRAL_TYPE_P (TREE_TYPE (*width))
3713       || TREE_CODE (*width) != INTEGER_CST)
3714     {
3715       error ("bit-field %qs width not an integer constant", name);
3716       *width = integer_one_node;
3717     }
3718   else
3719     {
3720       constant_expression_warning (*width);
3721       if (tree_int_cst_sgn (*width) < 0)
3722         {
3723           error ("negative width in bit-field %qs", name);
3724           *width = integer_one_node;
3725         }
3726       else if (integer_zerop (*width) && orig_name)
3727         {
3728           error ("zero width for bit-field %qs", name);
3729           *width = integer_one_node;
3730         }
3731     }
3732
3733   /* Detect invalid bit-field type.  */
3734   if (TREE_CODE (*type) != INTEGER_TYPE
3735       && TREE_CODE (*type) != BOOLEAN_TYPE
3736       && TREE_CODE (*type) != ENUMERAL_TYPE)
3737     {
3738       error ("bit-field %qs has invalid type", name);
3739       *type = unsigned_type_node;
3740     }
3741
3742   type_mv = TYPE_MAIN_VARIANT (*type);
3743   if (pedantic
3744       && type_mv != integer_type_node
3745       && type_mv != unsigned_type_node
3746       && type_mv != boolean_type_node)
3747     pedwarn ("type of bit-field %qs is a GCC extension", name);
3748
3749   if (type_mv == boolean_type_node)
3750     max_width = CHAR_TYPE_SIZE;
3751   else
3752     max_width = TYPE_PRECISION (*type);
3753
3754   if (0 < compare_tree_int (*width, max_width))
3755     {
3756       error ("width of %qs exceeds its type", name);
3757       w = max_width;
3758       *width = build_int_cst (NULL_TREE, w);
3759     }
3760   else
3761     w = tree_low_cst (*width, 1);
3762
3763   if (TREE_CODE (*type) == ENUMERAL_TYPE)
3764     {
3765       struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
3766       if (!lt
3767           || w < min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
3768           || w < min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
3769         warning (0, "%qs is narrower than values of its type", name);
3770     }
3771 }
3772 \f
3773 /* Given declspecs and a declarator,
3774    determine the name and type of the object declared
3775    and construct a ..._DECL node for it.
3776    (In one case we can return a ..._TYPE node instead.
3777     For invalid input we sometimes return 0.)
3778
3779    DECLSPECS is a c_declspecs structure for the declaration specifiers.
3780
3781    DECL_CONTEXT says which syntactic context this declaration is in:
3782      NORMAL for most contexts.  Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3783      FUNCDEF for a function definition.  Like NORMAL but a few different
3784       error messages in each case.  Return value may be zero meaning
3785       this definition is too screwy to try to parse.
3786      PARM for a parameter declaration (either within a function prototype
3787       or before a function body).  Make a PARM_DECL, or return void_type_node.
3788      TYPENAME if for a typename (in a cast or sizeof).
3789       Don't make a DECL node; just return the ..._TYPE node.
3790      FIELD for a struct or union field; make a FIELD_DECL.
3791    INITIALIZED is true if the decl has an initializer.
3792    WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3793    representing the width of the bit-field.
3794
3795    In the TYPENAME case, DECLARATOR is really an absolute declarator.
3796    It may also be so in the PARM case, for a prototype where the
3797    argument type is specified but not the name.
3798
3799    This function is where the complicated C meanings of `static'
3800    and `extern' are interpreted.  */
3801
3802 static tree
3803 grokdeclarator (const struct c_declarator *declarator,
3804                 struct c_declspecs *declspecs,
3805                 enum decl_context decl_context, bool initialized, tree *width)
3806 {
3807   tree type = declspecs->type;
3808   bool threadp = declspecs->thread_p;
3809   enum c_storage_class storage_class = declspecs->storage_class;
3810   int constp;
3811   int restrictp;
3812   int volatilep;
3813   int type_quals = TYPE_UNQUALIFIED;
3814   const char *name, *orig_name;
3815   tree typedef_type = 0;
3816   int funcdef_flag = 0;
3817   bool funcdef_syntax = false;
3818   int size_varies = 0;
3819   tree decl_attr = declspecs->decl_attr;
3820   int array_ptr_quals = TYPE_UNQUALIFIED;
3821   tree array_ptr_attrs = NULL_TREE;
3822   int array_parm_static = 0;
3823   tree returned_attrs = NULL_TREE;
3824   bool bitfield = width != NULL;
3825   tree element_type;
3826   struct c_arg_info *arg_info = 0;
3827
3828   if (decl_context == FUNCDEF)
3829     funcdef_flag = 1, decl_context = NORMAL;
3830
3831   /* Look inside a declarator for the name being declared
3832      and get it as a string, for an error message.  */
3833   {
3834     const struct c_declarator *decl = declarator;
3835     name = 0;
3836
3837     while (decl)
3838       switch (decl->kind)
3839         {
3840         case cdk_function:
3841         case cdk_array:
3842         case cdk_pointer:
3843           funcdef_syntax = (decl->kind == cdk_function);
3844           decl = decl->declarator;
3845           break;
3846
3847         case cdk_attrs:
3848           decl = decl->declarator;
3849           break;
3850
3851         case cdk_id:
3852           if (decl->u.id)
3853             name = IDENTIFIER_POINTER (decl->u.id);
3854           decl = 0;
3855           break;
3856
3857         default:
3858           gcc_unreachable ();
3859         }
3860     orig_name = name;
3861     if (name == 0)
3862       name = "type name";
3863   }
3864
3865   /* A function definition's declarator must have the form of
3866      a function declarator.  */
3867
3868   if (funcdef_flag && !funcdef_syntax)
3869     return 0;
3870
3871   /* If this looks like a function definition, make it one,
3872      even if it occurs where parms are expected.
3873      Then store_parm_decls will reject it and not use it as a parm.  */
3874   if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
3875     decl_context = PARM;
3876
3877   if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
3878     warn_deprecated_use (declspecs->type);
3879
3880   typedef_type = type;
3881   size_varies = C_TYPE_VARIABLE_SIZE (type);
3882
3883   /* Diagnose defaulting to "int".  */
3884
3885   if (declspecs->default_int_p && !in_system_header)
3886     {
3887       /* Issue a warning if this is an ISO C 99 program or if
3888          -Wreturn-type and this is a function, or if -Wimplicit;
3889          prefer the former warning since it is more explicit.  */
3890       if ((warn_implicit_int || warn_return_type || flag_isoc99)
3891           && funcdef_flag)
3892         warn_about_return_type = 1;
3893       else if (warn_implicit_int || flag_isoc99)
3894         pedwarn_c99 ("type defaults to %<int%> in declaration of %qs", name);
3895     }
3896
3897   /* Adjust the type if a bit-field is being declared,
3898      -funsigned-bitfields applied and the type is not explicitly
3899      "signed".  */
3900   if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
3901       && TREE_CODE (type) == INTEGER_TYPE)
3902     type = c_common_unsigned_type (type);
3903
3904   /* Figure out the type qualifiers for the declaration.  There are
3905      two ways a declaration can become qualified.  One is something
3906      like `const int i' where the `const' is explicit.  Another is
3907      something like `typedef const int CI; CI i' where the type of the
3908      declaration contains the `const'.  A third possibility is that
3909      there is a type qualifier on the element type of a typedefed
3910      array type, in which case we should extract that qualifier so
3911      that c_apply_type_quals_to_decls receives the full list of
3912      qualifiers to work with (C90 is not entirely clear about whether
3913      duplicate qualifiers should be diagnosed in this case, but it
3914      seems most appropriate to do so).  */
3915   element_type = strip_array_types (type);
3916   constp = declspecs->const_p + TYPE_READONLY (element_type);
3917   restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
3918   volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
3919   if (pedantic && !flag_isoc99)
3920     {
3921       if (constp > 1)
3922         pedwarn ("duplicate %<const%>");
3923       if (restrictp > 1)
3924         pedwarn ("duplicate %<restrict%>");
3925       if (volatilep > 1)
3926         pedwarn ("duplicate %<volatile%>");
3927     }
3928   if (!flag_gen_aux_info && (TYPE_QUALS (element_type)))
3929     type = TYPE_MAIN_VARIANT (type);
3930   type_quals = ((constp ? TYPE_QUAL_CONST : 0)
3931                 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
3932                 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
3933
3934   /* Warn about storage classes that are invalid for certain
3935      kinds of declarations (parameters, typenames, etc.).  */
3936
3937   if (funcdef_flag
3938       && (threadp
3939           || storage_class == csc_auto
3940           || storage_class == csc_register
3941           || storage_class == csc_typedef))
3942     {
3943       if (storage_class == csc_auto
3944           && (pedantic || current_scope == file_scope))
3945         pedwarn ("function definition declared %<auto%>");
3946       if (storage_class == csc_register)
3947         error ("function definition declared %<register%>");
3948       if (storage_class == csc_typedef)
3949         error ("function definition declared %<typedef%>");
3950       if (threadp)
3951         error ("function definition declared %<__thread%>");
3952       threadp = false;
3953       if (storage_class == csc_auto
3954           || storage_class == csc_register
3955           || storage_class == csc_typedef)
3956         storage_class = csc_none;
3957     }
3958   else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
3959     {
3960       if (decl_context == PARM && storage_class == csc_register)
3961         ;
3962       else
3963         {
3964           switch (decl_context)
3965             {
3966             case FIELD:
3967               error ("storage class specified for structure field %qs",
3968                      name);
3969               break;
3970             case PARM:
3971               error ("storage class specified for parameter %qs", name);
3972               break;
3973             default:
3974               error ("storage class specified for typename");
3975               break;
3976             }
3977           storage_class = csc_none;
3978           threadp = false;
3979         }
3980     }
3981   else if (storage_class == csc_extern
3982            && initialized
3983            && !funcdef_flag)
3984     {
3985       /* 'extern' with initialization is invalid if not at file scope.  */
3986       if (current_scope == file_scope)
3987         warning (0, "%qs initialized and declared %<extern%>", name);
3988       else
3989         error ("%qs has both %<extern%> and initializer", name);
3990     }
3991   else if (current_scope == file_scope)
3992     {
3993       if (storage_class == csc_auto)
3994         error ("file-scope declaration of %qs specifies %<auto%>", name);
3995       if (pedantic && storage_class == csc_register)
3996         pedwarn ("file-scope declaration of %qs specifies %<register%>", name);
3997     }
3998   else
3999     {
4000       if (storage_class == csc_extern && funcdef_flag)
4001         error ("nested function %qs declared %<extern%>", name);
4002       else if (threadp && storage_class == csc_none)
4003         {
4004           error ("function-scope %qs implicitly auto and declared "
4005                  "%<__thread%>",
4006                  name);
4007           threadp = false;
4008         }
4009     }
4010
4011   /* Now figure out the structure of the declarator proper.
4012      Descend through it, creating more complex types, until we reach
4013      the declared identifier (or NULL_TREE, in an absolute declarator).
4014      At each stage we maintain an unqualified version of the type
4015      together with any qualifiers that should be applied to it with
4016      c_build_qualified_type; this way, array types including
4017      multidimensional array types are first built up in unqualified
4018      form and then the qualified form is created with
4019      TYPE_MAIN_VARIANT pointing to the unqualified form.  */
4020
4021   while (declarator && declarator->kind != cdk_id)
4022     {
4023       if (type == error_mark_node)
4024         {
4025           declarator = declarator->declarator;
4026           continue;
4027         }
4028
4029       /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
4030          a cdk_pointer (for *...),
4031          a cdk_function (for ...(...)),
4032          a cdk_attrs (for nested attributes),
4033          or a cdk_id (for the name being declared
4034          or the place in an absolute declarator
4035          where the name was omitted).
4036          For the last case, we have just exited the loop.
4037
4038          At this point, TYPE is the type of elements of an array,
4039          or for a function to return, or for a pointer to point to.
4040          After this sequence of ifs, TYPE is the type of the
4041          array or function or pointer, and DECLARATOR has had its
4042          outermost layer removed.  */
4043
4044       if (array_ptr_quals != TYPE_UNQUALIFIED
4045           || array_ptr_attrs != NULL_TREE
4046           || array_parm_static)
4047         {
4048           /* Only the innermost declarator (making a parameter be of
4049              array type which is converted to pointer type)
4050              may have static or type qualifiers.  */
4051           error ("static or type qualifiers in non-parameter array declarator");
4052           array_ptr_quals = TYPE_UNQUALIFIED;
4053           array_ptr_attrs = NULL_TREE;
4054           array_parm_static = 0;
4055         }
4056
4057       switch (declarator->kind)
4058         {
4059         case cdk_attrs:
4060           {
4061             /* A declarator with embedded attributes.  */
4062             tree attrs = declarator->u.attrs;
4063             const struct c_declarator *inner_decl;
4064             int attr_flags = 0;
4065             declarator = declarator->declarator;
4066             inner_decl = declarator;
4067             while (inner_decl->kind == cdk_attrs)
4068               inner_decl = inner_decl->declarator;
4069             if (inner_decl->kind == cdk_id)
4070               attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
4071             else if (inner_decl->kind == cdk_function)
4072               attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
4073             else if (inner_decl->kind == cdk_array)
4074               attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
4075             returned_attrs = decl_attributes (&type,
4076                                               chainon (returned_attrs, attrs),
4077                                               attr_flags);
4078             break;
4079           }
4080         case cdk_array:
4081           {
4082             tree itype = NULL_TREE;
4083             tree size = declarator->u.array.dimen;
4084             /* The index is a signed object `sizetype' bits wide.  */
4085             tree index_type = c_common_signed_type (sizetype);
4086
4087             array_ptr_quals = declarator->u.array.quals;
4088             array_ptr_attrs = declarator->u.array.attrs;
4089             array_parm_static = declarator->u.array.static_p;
4090             
4091             declarator = declarator->declarator;
4092
4093             /* Check for some types that there cannot be arrays of.  */
4094             
4095             if (VOID_TYPE_P (type))
4096               {
4097                 error ("declaration of %qs as array of voids", name);
4098                 type = error_mark_node;
4099               }
4100             
4101             if (TREE_CODE (type) == FUNCTION_TYPE)
4102               {
4103                 error ("declaration of %qs as array of functions", name);
4104                 type = error_mark_node;
4105               }
4106             
4107             if (pedantic && !in_system_header && flexible_array_type_p (type))
4108               pedwarn ("invalid use of structure with flexible array member");
4109             
4110             if (size == error_mark_node)
4111               type = error_mark_node;
4112             
4113             if (type == error_mark_node)
4114               continue;
4115
4116             /* If size was specified, set ITYPE to a range-type for
4117                that size.  Otherwise, ITYPE remains null.  finish_decl
4118                may figure it out from an initial value.  */
4119
4120             if (size)
4121               {
4122                 /* Strip NON_LVALUE_EXPRs since we aren't using as an
4123                    lvalue.  */
4124                 STRIP_TYPE_NOPS (size);
4125                 
4126                 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
4127                   {
4128                     error ("size of array %qs has non-integer type", name);
4129                     size = integer_one_node;
4130                   }
4131                 
4132                 if (pedantic && integer_zerop (size))
4133                   pedwarn ("ISO C forbids zero-size array %qs", name);
4134                 
4135                 if (TREE_CODE (size) == INTEGER_CST)
4136                   {
4137                     constant_expression_warning (size);
4138                     if (tree_int_cst_sgn (size) < 0)
4139                       {
4140                         error ("size of array %qs is negative", name);
4141                         size = integer_one_node;
4142                       }
4143                   }
4144                 else
4145                   {
4146                     /* Make sure the array size remains visibly
4147                        nonconstant even if it is (eg) a const variable
4148                        with known value.  */
4149                     size_varies = 1;
4150                     
4151                     if (!flag_isoc99 && pedantic)
4152                       {
4153                         if (TREE_CONSTANT (size))
4154                           pedwarn ("ISO C90 forbids array %qs whose size "
4155                                    "can%'t be evaluated",
4156                                    name);
4157                         else
4158                           pedwarn ("ISO C90 forbids variable-size array %qs",
4159                                    name);
4160                       }
4161                   }
4162
4163                 if (integer_zerop (size))
4164                   {
4165                     /*  A zero-length array cannot be represented with
4166                         an unsigned index type, which is what we'll
4167                         get with build_index_type.  Create an
4168                         open-ended range instead.  */
4169                     itype = build_range_type (sizetype, size, NULL_TREE);
4170                   }
4171                 else
4172                   {
4173                     /* Arrange for the SAVE_EXPR on the inside of the
4174                        MINUS_EXPR, which allows the -1 to get folded
4175                        with the +1 that happens when building TYPE_SIZE.  */
4176                     if (size_varies)
4177                       size = variable_size (size);
4178
4179                     /* Compute the maximum valid index, that is, size
4180                        - 1.  Do the calculation in index_type, so that
4181                        if it is a variable the computations will be
4182                        done in the proper mode.  */
4183                     itype = fold_build2 (MINUS_EXPR, index_type,
4184                                          convert (index_type, size),
4185                                          convert (index_type,
4186                                                   size_one_node));
4187
4188                     /* If that overflowed, the array is too big.  ??? 
4189                        While a size of INT_MAX+1 technically shouldn't
4190                        cause an overflow (because we subtract 1), the
4191                        overflow is recorded during the conversion to
4192                        index_type, before the subtraction.  Handling
4193                        this case seems like an unnecessary
4194                        complication.  */
4195                     if (TREE_CODE (itype) == INTEGER_CST
4196                         && TREE_OVERFLOW (itype))
4197                       {
4198                         error ("size of array %qs is too large", name);
4199                         type = error_mark_node;
4200                         continue;
4201                       }
4202                     
4203                     itype = build_index_type (itype);
4204                   }
4205               }
4206             else if (decl_context == FIELD)
4207               {
4208                 if (pedantic && !flag_isoc99 && !in_system_header)
4209                   pedwarn ("ISO C90 does not support flexible array members");
4210
4211                 /* ISO C99 Flexible array members are effectively
4212                    identical to GCC's zero-length array extension.  */
4213                 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4214               }
4215
4216              /* Complain about arrays of incomplete types.  */
4217             if (!COMPLETE_TYPE_P (type))
4218               {
4219                 error ("array type has incomplete element type");
4220                 type = error_mark_node;
4221               }
4222             else
4223               type = build_array_type (type, itype);
4224
4225             if (size_varies)
4226               C_TYPE_VARIABLE_SIZE (type) = 1;
4227
4228             /* The GCC extension for zero-length arrays differs from
4229                ISO flexible array members in that sizeof yields
4230                zero.  */
4231             if (size && integer_zerop (size))
4232               {
4233                 TYPE_SIZE (type) = bitsize_zero_node;
4234                 TYPE_SIZE_UNIT (type) = size_zero_node;
4235               }
4236
4237             if (decl_context != PARM
4238                 && (array_ptr_quals != TYPE_UNQUALIFIED
4239                     || array_ptr_attrs != NULL_TREE
4240                     || array_parm_static))
4241               {
4242                 error ("static or type qualifiers in non-parameter array declarator");
4243                 array_ptr_quals = TYPE_UNQUALIFIED;
4244                 array_ptr_attrs = NULL_TREE;
4245                 array_parm_static = 0;
4246               }
4247             break;
4248           }
4249         case cdk_function:
4250           {
4251             /* Say it's a definition only for the declarator closest
4252                to the identifier, apart possibly from some
4253                attributes.  */
4254             bool really_funcdef = false;
4255             tree arg_types;
4256             if (funcdef_flag)
4257               {
4258                 const struct c_declarator *t = declarator->declarator;
4259                 while (t->kind == cdk_attrs)
4260                   t = t->declarator;
4261                 really_funcdef = (t->kind == cdk_id);
4262               }
4263
4264             /* Declaring a function type.  Make sure we have a valid
4265                type for the function to return.  */
4266             if (type == error_mark_node)
4267               continue;
4268             
4269             size_varies = 0;
4270
4271             /* Warn about some types functions can't return.  */
4272             if (TREE_CODE (type) == FUNCTION_TYPE)
4273               {
4274                 error ("%qs declared as function returning a function", name);
4275                 type = integer_type_node;
4276               }
4277             if (TREE_CODE (type) == ARRAY_TYPE)
4278               {
4279                 error ("%qs declared as function returning an array", name);
4280                 type = integer_type_node;
4281               }
4282
4283             /* Construct the function type and go to the next
4284                inner layer of declarator.  */
4285             arg_info = declarator->u.arg_info;
4286             arg_types = grokparms (arg_info, really_funcdef);
4287
4288             /* Type qualifiers before the return type of the function
4289                qualify the return type, not the function type.  */
4290             if (type_quals)
4291               {
4292                 /* Type qualifiers on a function return type are
4293                    normally permitted by the standard but have no
4294                    effect, so give a warning at -Wreturn-type.
4295                    Qualifiers on a void return type are banned on
4296                    function definitions in ISO C; GCC used to used
4297                    them for noreturn functions.  */
4298                 if (VOID_TYPE_P (type) && really_funcdef)
4299                   pedwarn ("function definition has qualified void return type");
4300                 else
4301                   warning (OPT_Wreturn_type,
4302                            "type qualifiers ignored on function return type");
4303                 
4304                 type = c_build_qualified_type (type, type_quals);
4305               }
4306             type_quals = TYPE_UNQUALIFIED;
4307             
4308             type = build_function_type (type, arg_types);
4309             declarator = declarator->declarator;
4310             
4311             /* Set the TYPE_CONTEXTs for each tagged type which is local to
4312                the formal parameter list of this FUNCTION_TYPE to point to
4313                the FUNCTION_TYPE node itself.  */
4314             {
4315               tree link;
4316               
4317               for (link = arg_info->tags;
4318                    link;
4319                    link = TREE_CHAIN (link))
4320                 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4321             }
4322             break;
4323           }
4324         case cdk_pointer:
4325           {
4326             /* Merge any constancy or volatility into the target type
4327                for the pointer.  */
4328
4329             if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4330                 && type_quals)
4331               pedwarn ("ISO C forbids qualified function types");
4332             if (type_quals)
4333               type = c_build_qualified_type (type, type_quals);
4334             size_varies = 0;
4335
4336             type = build_pointer_type (type);
4337             
4338             /* Process type qualifiers (such as const or volatile)
4339                that were given inside the `*'.  */
4340             type_quals = declarator->u.pointer_quals;
4341
4342             declarator = declarator->declarator;
4343             break;
4344           }
4345         default:
4346           gcc_unreachable ();
4347         }
4348     }
4349
4350   /* Now TYPE has the actual type, apart from any qualifiers in
4351      TYPE_QUALS.  */
4352
4353   /* Check the type and width of a bit-field.  */
4354   if (bitfield)
4355     check_bitfield_type_and_width (&type, width, orig_name);
4356
4357   /* Did array size calculations overflow?  */
4358
4359   if (TREE_CODE (type) == ARRAY_TYPE
4360       && COMPLETE_TYPE_P (type)
4361       && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
4362       && TREE_OVERFLOW (TYPE_SIZE (type)))
4363     {
4364       error ("size of array %qs is too large", name);
4365       /* If we proceed with the array type as it is, we'll eventually
4366          crash in tree_low_cst().  */
4367       type = error_mark_node;
4368     }
4369
4370   /* If this is declaring a typedef name, return a TYPE_DECL.  */
4371
4372   if (storage_class == csc_typedef)
4373     {
4374       tree decl;
4375       if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4376           && type_quals)
4377         pedwarn ("ISO C forbids qualified function types");
4378       if (type_quals)
4379         type = c_build_qualified_type (type, type_quals);
4380       decl = build_decl (TYPE_DECL, declarator->u.id, type);
4381       if (declspecs->explicit_signed_p)
4382         C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4383       decl_attributes (&decl, returned_attrs, 0);
4384       if (declspecs->inline_p)
4385         pedwarn ("%Jtypedef %qD declared %<inline%>", decl, decl);
4386       return decl;
4387     }
4388
4389   /* Detect the case of an array type of unspecified size
4390      which came, as such, direct from a typedef name.
4391      We must copy the type, so that each identifier gets
4392      a distinct type, so that each identifier's size can be
4393      controlled separately by its own initializer.  */
4394
4395   if (type != 0 && typedef_type != 0
4396       && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0
4397       && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
4398     {
4399       type = build_array_type (TREE_TYPE (type), 0);
4400       if (size_varies)
4401         C_TYPE_VARIABLE_SIZE (type) = 1;
4402     }
4403
4404   /* If this is a type name (such as, in a cast or sizeof),
4405      compute the type and return it now.  */
4406
4407   if (decl_context == TYPENAME)
4408     {
4409       /* Note that the grammar rejects storage classes in typenames
4410          and fields.  */
4411       gcc_assert (storage_class == csc_none && !threadp
4412                   && !declspecs->inline_p);
4413       if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4414           && type_quals)
4415         pedwarn ("ISO C forbids const or volatile function types");
4416       if (type_quals)
4417         type = c_build_qualified_type (type, type_quals);
4418       decl_attributes (&type, returned_attrs, 0);
4419       return type;
4420     }
4421
4422   /* Aside from typedefs and type names (handle above),
4423      `void' at top level (not within pointer)
4424      is allowed only in public variables.
4425      We don't complain about parms either, but that is because
4426      a better error message can be made later.  */
4427
4428   if (VOID_TYPE_P (type) && decl_context != PARM
4429       && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4430             && (storage_class == csc_extern
4431                 || (current_scope == file_scope
4432                     && !(storage_class == csc_static
4433                          || storage_class == csc_register)))))
4434     {
4435       error ("variable or field %qs declared void", name);
4436       type = integer_type_node;
4437     }
4438
4439   /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4440      or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE.  */
4441
4442   {
4443     tree decl;
4444
4445     if (decl_context == PARM)
4446       {
4447         tree type_as_written;
4448         tree promoted_type;
4449
4450         /* A parameter declared as an array of T is really a pointer to T.
4451            One declared as a function is really a pointer to a function.  */
4452
4453         if (TREE_CODE (type) == ARRAY_TYPE)
4454           {
4455             /* Transfer const-ness of array into that of type pointed to.  */
4456             type = TREE_TYPE (type);
4457             if (type_quals)
4458               type = c_build_qualified_type (type, type_quals);
4459             type = build_pointer_type (type);
4460             type_quals = array_ptr_quals;
4461
4462             /* We don't yet implement attributes in this context.  */
4463             if (array_ptr_attrs != NULL_TREE)
4464               warning (OPT_Wattributes,
4465                        "attributes in parameter array declarator ignored");
4466
4467             size_varies = 0;
4468           }
4469         else if (TREE_CODE (type) == FUNCTION_TYPE)
4470           {
4471             if (pedantic && type_quals)
4472               pedwarn ("ISO C forbids qualified function types");
4473             if (type_quals)
4474               type = c_build_qualified_type (type, type_quals);
4475             type = build_pointer_type (type);
4476             type_quals = TYPE_UNQUALIFIED;
4477           }
4478         else if (type_quals)
4479           type = c_build_qualified_type (type, type_quals);
4480
4481         type_as_written = type;
4482
4483         decl = build_decl (PARM_DECL, declarator->u.id, type);
4484         if (size_varies)
4485           C_DECL_VARIABLE_SIZE (decl) = 1;
4486
4487         /* Compute the type actually passed in the parmlist,
4488            for the case where there is no prototype.
4489            (For example, shorts and chars are passed as ints.)
4490            When there is a prototype, this is overridden later.  */
4491
4492         if (type == error_mark_node)
4493           promoted_type = type;
4494         else
4495           promoted_type = c_type_promotes_to (type);
4496
4497         DECL_ARG_TYPE (decl) = promoted_type;
4498         DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
4499         if (declspecs->inline_p)
4500           pedwarn ("%Jparameter %qD declared %<inline%>", decl, decl);
4501       }
4502     else if (decl_context == FIELD)
4503       {
4504         /* Note that the grammar rejects storage classes in typenames
4505            and fields.  */
4506         gcc_assert (storage_class == csc_none && !threadp
4507                     && !declspecs->inline_p);
4508
4509         /* Structure field.  It may not be a function.  */
4510
4511         if (TREE_CODE (type) == FUNCTION_TYPE)
4512           {
4513             error ("field %qs declared as a function", name);
4514             type = build_pointer_type (type);
4515           }
4516         else if (TREE_CODE (type) != ERROR_MARK
4517                  && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4518           {
4519             error ("field %qs has incomplete type", name);
4520             type = error_mark_node;
4521           }
4522         type = c_build_qualified_type (type, type_quals);
4523         decl = build_decl (FIELD_DECL, declarator->u.id, type);
4524         DECL_NONADDRESSABLE_P (decl) = bitfield;
4525
4526         if (size_varies)
4527           C_DECL_VARIABLE_SIZE (decl) = 1;
4528       }
4529     else if (TREE_CODE (type) == FUNCTION_TYPE)
4530       {
4531         if (storage_class == csc_register || threadp)
4532           {
4533             error ("invalid storage class for function %qs", name);
4534            }
4535         else if (current_scope != file_scope)
4536           {
4537             /* Function declaration not at file scope.  Storage
4538                classes other than `extern' are not allowed, C99
4539                6.7.1p5, and `extern' makes no difference.  However,
4540                GCC allows 'auto', perhaps with 'inline', to support
4541                nested functions.  */
4542             if (storage_class == csc_auto)
4543               {
4544                 if (pedantic)
4545                   pedwarn ("invalid storage class for function %qs", name);
4546               }
4547             else if (storage_class == csc_static)
4548               {
4549                 error ("invalid storage class for function %qs", name);
4550                 if (funcdef_flag)
4551                   storage_class = declspecs->storage_class = csc_none;
4552                 else
4553                   return 0;
4554               }
4555           }
4556
4557         decl = build_decl (FUNCTION_DECL, declarator->u.id, type);
4558         decl = build_decl_attribute_variant (decl, decl_attr);
4559
4560         DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
4561
4562         if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
4563           pedwarn ("ISO C forbids qualified function types");
4564
4565         /* GNU C interprets a volatile-qualified function type to indicate
4566            that the function does not return.  */
4567         if ((type_quals & TYPE_QUAL_VOLATILE)
4568             && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4569           warning (0, "%<noreturn%> function returns non-void value");
4570
4571         /* Every function declaration is an external reference
4572            (DECL_EXTERNAL) except for those which are not at file
4573            scope and are explicitly declared "auto".  This is
4574            forbidden by standard C (C99 6.7.1p5) and is interpreted by
4575            GCC to signify a forward declaration of a nested function.  */
4576         if (storage_class == csc_auto && current_scope != file_scope)
4577           DECL_EXTERNAL (decl) = 0;
4578         else
4579           DECL_EXTERNAL (decl) = 1;
4580
4581         /* Record absence of global scope for `static' or `auto'.  */
4582         TREE_PUBLIC (decl)
4583           = !(storage_class == csc_static || storage_class == csc_auto);
4584
4585         /* For a function definition, record the argument information
4586            block where store_parm_decls will look for it.  */
4587         if (funcdef_flag)
4588           current_function_arg_info = arg_info;
4589
4590         if (declspecs->default_int_p)
4591           C_FUNCTION_IMPLICIT_INT (decl) = 1;
4592
4593         /* Record presence of `inline', if it is reasonable.  */
4594         if (flag_hosted && MAIN_NAME_P (declarator->u.id))
4595           {
4596             if (declspecs->inline_p)
4597               pedwarn ("cannot inline function %<main%>");
4598           }
4599         else if (declspecs->inline_p)
4600           {
4601             /* Record that the function is declared `inline'.  */
4602             DECL_DECLARED_INLINE_P (decl) = 1;
4603
4604             /* Do not mark bare declarations as DECL_INLINE.  Doing so
4605                in the presence of multiple declarations can result in
4606                the abstract origin pointing between the declarations,
4607                which will confuse dwarf2out.  */
4608             if (initialized)
4609               {
4610                 DECL_INLINE (decl) = 1;
4611                 if (storage_class == csc_extern)
4612                   current_extern_inline = 1;
4613               }
4614           }
4615         /* If -finline-functions, assume it can be inlined.  This does
4616            two things: let the function be deferred until it is actually
4617            needed, and let dwarf2 know that the function is inlinable.  */
4618         else if (flag_inline_trees == 2 && initialized)
4619           DECL_INLINE (decl) = 1;
4620       }
4621     else
4622       {
4623         /* It's a variable.  */
4624         /* An uninitialized decl with `extern' is a reference.  */
4625         int extern_ref = !initialized && storage_class == csc_extern;
4626
4627         type = c_build_qualified_type (type, type_quals);
4628
4629         /* C99 6.2.2p7: It is invalid (compile-time undefined
4630            behavior) to create an 'extern' declaration for a
4631            variable if there is a global declaration that is
4632            'static' and the global declaration is not visible.
4633            (If the static declaration _is_ currently visible,
4634            the 'extern' declaration is taken to refer to that decl.) */
4635         if (extern_ref && current_scope != file_scope)
4636           {
4637             tree global_decl  = identifier_global_value (declarator->u.id);
4638             tree visible_decl = lookup_name (declarator->u.id);
4639
4640             if (global_decl
4641                 && global_decl != visible_decl
4642                 && TREE_CODE (global_decl) == VAR_DECL
4643                 && !TREE_PUBLIC (global_decl))
4644               error ("variable previously declared %<static%> redeclared "
4645                      "%<extern%>");
4646           }
4647
4648         decl = build_decl (VAR_DECL, declarator->u.id, type);
4649         DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4650         if (size_varies)
4651           C_DECL_VARIABLE_SIZE (decl) = 1;
4652
4653         if (declspecs->inline_p)
4654           pedwarn ("%Jvariable %qD declared %<inline%>", decl, decl);
4655
4656         /* At file scope, an initialized extern declaration may follow
4657            a static declaration.  In that case, DECL_EXTERNAL will be
4658            reset later in start_decl.  */
4659         DECL_EXTERNAL (decl) = (storage_class == csc_extern);
4660
4661         /* At file scope, the presence of a `static' or `register' storage
4662            class specifier, or the absence of all storage class specifiers
4663            makes this declaration a definition (perhaps tentative).  Also,
4664            the absence of both `static' and `register' makes it public.  */
4665         if (current_scope == file_scope)
4666           {
4667             TREE_PUBLIC (decl) = !(storage_class == csc_static
4668                                    || storage_class == csc_register);
4669             TREE_STATIC (decl) = !extern_ref;
4670           }
4671         /* Not at file scope, only `static' makes a static definition.  */
4672         else
4673           {
4674             TREE_STATIC (decl) = (storage_class == csc_static);
4675             TREE_PUBLIC (decl) = extern_ref;
4676           }
4677
4678         if (threadp)
4679           {
4680             if (targetm.have_tls)
4681               DECL_THREAD_LOCAL (decl) = 1;
4682             else
4683               /* A mere warning is sure to result in improper semantics
4684                  at runtime.  Don't bother to allow this to compile.  */
4685               error ("thread-local storage not supported for this target");
4686           }
4687       }
4688
4689     /* Record `register' declaration for warnings on &
4690        and in case doing stupid register allocation.  */
4691
4692     if (storage_class == csc_register)
4693       {
4694         C_DECL_REGISTER (decl) = 1;
4695         DECL_REGISTER (decl) = 1;
4696       }
4697
4698     /* Record constancy and volatility.  */
4699     c_apply_type_quals_to_decl (type_quals, decl);
4700
4701     /* If a type has volatile components, it should be stored in memory.
4702        Otherwise, the fact that those components are volatile
4703        will be ignored, and would even crash the compiler.  */
4704     if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
4705       {
4706         /* It is not an error for a structure with volatile fields to
4707            be declared register, but reset DECL_REGISTER since it
4708            cannot actually go in a register.  */
4709         int was_reg = C_DECL_REGISTER (decl);
4710         C_DECL_REGISTER (decl) = 0;
4711         DECL_REGISTER (decl) = 0;
4712         c_mark_addressable (decl);
4713         C_DECL_REGISTER (decl) = was_reg;
4714       }
4715
4716   /* This is the earliest point at which we might know the assembler
4717      name of a variable.  Thus, if it's known before this, die horribly.  */
4718     gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
4719
4720     decl_attributes (&decl, returned_attrs, 0);
4721
4722     return decl;
4723   }
4724 }
4725 \f
4726 /* Decode the parameter-list info for a function type or function definition.
4727    The argument is the value returned by `get_parm_info' (or made in parse.y
4728    if there is an identifier list instead of a parameter decl list).
4729    These two functions are separate because when a function returns
4730    or receives functions then each is called multiple times but the order
4731    of calls is different.  The last call to `grokparms' is always the one
4732    that contains the formal parameter names of a function definition.
4733
4734    Return a list of arg types to use in the FUNCTION_TYPE for this function.
4735
4736    FUNCDEF_FLAG is true for a function definition, false for
4737    a mere declaration.  A nonempty identifier-list gets an error message
4738    when FUNCDEF_FLAG is false.  */
4739
4740 static tree
4741 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
4742 {
4743   tree arg_types = arg_info->types;
4744
4745   if (arg_types == 0 && !funcdef_flag && !in_system_header)
4746     warning (OPT_Wstrict_prototypes,
4747              "function declaration isn%'t a prototype");
4748
4749   if (arg_types == error_mark_node)
4750     return 0;  /* don't set TYPE_ARG_TYPES in this case */
4751
4752   else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
4753     {
4754       if (!funcdef_flag)
4755         pedwarn ("parameter names (without types) in function declaration");
4756
4757       arg_info->parms = arg_info->types;
4758       arg_info->types = 0;
4759       return 0;
4760     }
4761   else
4762     {
4763       tree parm, type, typelt;
4764       unsigned int parmno;
4765
4766       /* If there is a parameter of incomplete type in a definition,
4767          this is an error.  In a declaration this is valid, and a
4768          struct or union type may be completed later, before any calls
4769          or definition of the function.  In the case where the tag was
4770          first declared within the parameter list, a warning has
4771          already been given.  If a parameter has void type, then
4772          however the function cannot be defined or called, so
4773          warn.  */
4774
4775       for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
4776            parm;
4777            parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
4778         {
4779           type = TREE_VALUE (typelt);
4780           if (type == error_mark_node)
4781             continue;
4782
4783           if (!COMPLETE_TYPE_P (type))
4784             {
4785               if (funcdef_flag)
4786                 {
4787                   if (DECL_NAME (parm))
4788                     error ("%Jparameter %u (%qD) has incomplete type",
4789                            parm, parmno, parm);
4790                   else
4791                     error ("%Jparameter %u has incomplete type",
4792                            parm, parmno);
4793
4794                   TREE_VALUE (typelt) = error_mark_node;
4795                   TREE_TYPE (parm) = error_mark_node;
4796                 }
4797               else if (VOID_TYPE_P (type))
4798                 {
4799                   if (DECL_NAME (parm))
4800                     warning (0, "%Jparameter %u (%qD) has void type",
4801                              parm, parmno, parm);
4802                   else
4803                     warning (0, "%Jparameter %u has void type",
4804                              parm, parmno);
4805                 }
4806             }
4807         }
4808       return arg_types;
4809     }
4810 }
4811
4812 /* Take apart the current scope and return a c_arg_info structure with
4813    info on a parameter list just parsed.
4814
4815    This structure is later fed to 'grokparms' and 'store_parm_decls'.
4816
4817    ELLIPSIS being true means the argument list ended in '...' so don't
4818    append a sentinel (void_list_node) to the end of the type-list.  */
4819
4820 struct c_arg_info *
4821 get_parm_info (bool ellipsis)
4822 {
4823   struct c_binding *b = current_scope->bindings;
4824   struct c_arg_info *arg_info = XOBNEW (&parser_obstack,
4825                                         struct c_arg_info);
4826   tree parms    = 0;
4827   tree tags     = 0;
4828   tree types    = 0;
4829   tree others   = 0;
4830
4831   static bool explained_incomplete_types = false;
4832   bool gave_void_only_once_err = false;
4833
4834   arg_info->parms = 0;
4835   arg_info->tags = 0;
4836   arg_info->types = 0;
4837   arg_info->others = 0;
4838
4839   /* The bindings in this scope must not get put into a block.
4840      We will take care of deleting the binding nodes.  */
4841   current_scope->bindings = 0;
4842
4843   /* This function is only called if there was *something* on the
4844      parameter list.  */
4845   gcc_assert (b);
4846
4847   /* A parameter list consisting solely of 'void' indicates that the
4848      function takes no arguments.  But if the 'void' is qualified
4849      (by 'const' or 'volatile'), or has a storage class specifier
4850      ('register'), then the behavior is undefined; issue an error.
4851      Typedefs for 'void' are OK (see DR#157).  */
4852   if (b->prev == 0                          /* one binding */
4853       && TREE_CODE (b->decl) == PARM_DECL   /* which is a parameter */
4854       && !DECL_NAME (b->decl)               /* anonymous */
4855       && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
4856     {
4857       if (TREE_THIS_VOLATILE (b->decl)
4858           || TREE_READONLY (b->decl)
4859           || C_DECL_REGISTER (b->decl))
4860         error ("%<void%> as only parameter may not be qualified");
4861
4862       /* There cannot be an ellipsis.  */
4863       if (ellipsis)
4864         error ("%<void%> must be the only parameter");
4865
4866       arg_info->types = void_list_node;
4867       return arg_info;
4868     }
4869
4870   if (!ellipsis)
4871     types = void_list_node;
4872
4873   /* Break up the bindings list into parms, tags, types, and others;
4874      apply sanity checks; purge the name-to-decl bindings.  */
4875   while (b)
4876     {
4877       tree decl = b->decl;
4878       tree type = TREE_TYPE (decl);
4879       const char *keyword;
4880
4881       switch (TREE_CODE (decl))
4882         {
4883         case PARM_DECL:
4884           if (b->id)
4885             {
4886               gcc_assert (I_SYMBOL_BINDING (b->id) == b);
4887               I_SYMBOL_BINDING (b->id) = b->shadowed;
4888             }
4889
4890           /* Check for forward decls that never got their actual decl.  */
4891           if (TREE_ASM_WRITTEN (decl))
4892             error ("%Jparameter %qD has just a forward declaration",
4893                    decl, decl);
4894           /* Check for (..., void, ...) and issue an error.  */
4895           else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
4896             {
4897               if (!gave_void_only_once_err)
4898                 {
4899                   error ("%<void%> must be the only parameter");
4900                   gave_void_only_once_err = true;
4901                 }
4902             }
4903           else
4904             {
4905               /* Valid parameter, add it to the list.  */
4906               TREE_CHAIN (decl) = parms;
4907               parms = decl;
4908
4909               /* Since there is a prototype, args are passed in their
4910                  declared types.  The back end may override this later.  */
4911               DECL_ARG_TYPE (decl) = type;
4912               types = tree_cons (0, type, types);
4913             }
4914           break;
4915
4916         case ENUMERAL_TYPE: keyword = "enum"; goto tag;
4917         case UNION_TYPE:    keyword = "union"; goto tag;
4918         case RECORD_TYPE:   keyword = "struct"; goto tag;
4919         tag:
4920           /* Types may not have tag-names, in which case the type
4921              appears in the bindings list with b->id NULL.  */
4922           if (b->id)
4923             {
4924               gcc_assert (I_TAG_BINDING (b->id) == b);
4925               I_TAG_BINDING (b->id) = b->shadowed;
4926             }
4927
4928           /* Warn about any struct, union or enum tags defined in a
4929              parameter list.  The scope of such types is limited to
4930              the parameter list, which is rarely if ever desirable
4931              (it's impossible to call such a function with type-
4932              correct arguments).  An anonymous union parm type is
4933              meaningful as a GNU extension, so don't warn for that.  */
4934           if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
4935             {
4936               if (b->id)
4937                 /* The %s will be one of 'struct', 'union', or 'enum'.  */
4938                 warning (0, "%<%s %E%> declared inside parameter list",
4939                          keyword, b->id);
4940               else
4941                 /* The %s will be one of 'struct', 'union', or 'enum'.  */
4942                 warning (0, "anonymous %s declared inside parameter list",
4943                          keyword);
4944
4945               if (!explained_incomplete_types)
4946                 {
4947                   warning (0, "its scope is only this definition or declaration,"
4948                            " which is probably not what you want");
4949                   explained_incomplete_types = true;
4950                 }
4951             }
4952
4953           tags = tree_cons (b->id, decl, tags);
4954           break;
4955
4956         case CONST_DECL:
4957         case TYPE_DECL:
4958         case FUNCTION_DECL:
4959           /* CONST_DECLs appear here when we have an embedded enum,
4960              and TYPE_DECLs appear here when we have an embedded struct
4961              or union.  No warnings for this - we already warned about the
4962              type itself.  FUNCTION_DECLs appear when there is an implicit
4963              function declaration in the parameter list.  */
4964
4965           TREE_CHAIN (decl) = others;
4966           others = decl;
4967           /* fall through */
4968
4969         case ERROR_MARK:
4970           /* error_mark_node appears here when we have an undeclared
4971              variable.  Just throw it away.  */
4972           if (b->id)
4973             {
4974               gcc_assert (I_SYMBOL_BINDING (b->id) == b);
4975               I_SYMBOL_BINDING (b->id) = b->shadowed;
4976             }
4977           break;
4978
4979           /* Other things that might be encountered.  */
4980         case LABEL_DECL:
4981         case VAR_DECL:
4982         default:
4983           gcc_unreachable ();
4984         }
4985
4986       b = free_binding_and_advance (b);
4987     }
4988
4989   arg_info->parms = parms;
4990   arg_info->tags = tags;
4991   arg_info->types = types;
4992   arg_info->others = others;
4993   return arg_info;
4994 }
4995 \f
4996 /* Get the struct, enum or union (CODE says which) with tag NAME.
4997    Define the tag as a forward-reference if it is not defined.
4998    Return a c_typespec structure for the type specifier.  */
4999
5000 struct c_typespec
5001 parser_xref_tag (enum tree_code code, tree name)
5002 {
5003   struct c_typespec ret;
5004   /* If a cross reference is requested, look up the type
5005      already defined for this tag and return it.  */
5006
5007   tree ref = lookup_tag (code, name, 0);
5008   /* If this is the right type of tag, return what we found.
5009      (This reference will be shadowed by shadow_tag later if appropriate.)
5010      If this is the wrong type of tag, do not return it.  If it was the
5011      wrong type in the same scope, we will have had an error
5012      message already; if in a different scope and declaring
5013      a name, pending_xref_error will give an error message; but if in a
5014      different scope and not declaring a name, this tag should
5015      shadow the previous declaration of a different type of tag, and
5016      this would not work properly if we return the reference found.
5017      (For example, with "struct foo" in an outer scope, "union foo;"
5018      must shadow that tag with a new one of union type.)  */
5019   ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
5020   if (ref && TREE_CODE (ref) == code)
5021     {
5022       ret.spec = ref;
5023       return ret;
5024     }
5025
5026   /* If no such tag is yet defined, create a forward-reference node
5027      and record it as the "definition".
5028      When a real declaration of this type is found,
5029      the forward-reference will be altered into a real type.  */
5030
5031   ref = make_node (code);
5032   if (code == ENUMERAL_TYPE)
5033     {
5034       /* Give the type a default layout like unsigned int
5035          to avoid crashing if it does not get defined.  */
5036       TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
5037       TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
5038       TYPE_USER_ALIGN (ref) = 0;
5039       TYPE_UNSIGNED (ref) = 1;
5040       TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
5041       TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
5042       TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
5043     }
5044
5045   pushtag (name, ref);
5046
5047   ret.spec = ref;
5048   return ret;
5049 }
5050
5051 /* Get the struct, enum or union (CODE says which) with tag NAME.
5052    Define the tag as a forward-reference if it is not defined.
5053    Return a tree for the type.  */
5054
5055 tree
5056 xref_tag (enum tree_code code, tree name)
5057 {
5058   return parser_xref_tag (code, name).spec;
5059 }
5060 \f
5061 /* Make sure that the tag NAME is defined *in the current scope*
5062    at least as a forward reference.
5063    CODE says which kind of tag NAME ought to be.  */
5064
5065 tree
5066 start_struct (enum tree_code code, tree name)
5067 {
5068   /* If there is already a tag defined at this scope
5069      (as a forward reference), just return it.  */
5070
5071   tree ref = 0;
5072
5073   if (name != 0)
5074     ref = lookup_tag (code, name, 1);
5075   if (ref && TREE_CODE (ref) == code)
5076     {
5077       if (TYPE_SIZE (ref))
5078         {
5079           if (code == UNION_TYPE)
5080             error ("redefinition of %<union %E%>", name);
5081           else
5082             error ("redefinition of %<struct %E%>", name);
5083         }
5084       else if (C_TYPE_BEING_DEFINED (ref))
5085         {
5086           if (code == UNION_TYPE)
5087             error ("nested redefinition of %<union %E%>", name);
5088           else
5089             error ("nested redefinition of %<struct %E%>", name);
5090         }
5091     }
5092   else
5093     {
5094       /* Otherwise create a forward-reference just so the tag is in scope.  */
5095
5096       ref = make_node (code);
5097       pushtag (name, ref);
5098     }
5099
5100   C_TYPE_BEING_DEFINED (ref) = 1;
5101   TYPE_PACKED (ref) = flag_pack_struct;
5102   return ref;
5103 }
5104
5105 /* Process the specs, declarator and width (NULL if omitted)
5106    of a structure component, returning a FIELD_DECL node.
5107    WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
5108
5109    This is done during the parsing of the struct declaration.
5110    The FIELD_DECL nodes are chained together and the lot of them
5111    are ultimately passed to `build_struct' to make the RECORD_TYPE node.  */
5112
5113 tree
5114 grokfield (struct c_declarator *declarator, struct c_declspecs *declspecs,
5115            tree width)
5116 {
5117   tree value;
5118
5119   if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
5120       && width == NULL_TREE)
5121     {
5122       /* This is an unnamed decl.
5123
5124          If we have something of the form "union { list } ;" then this
5125          is the anonymous union extension.  Similarly for struct.
5126
5127          If this is something of the form "struct foo;", then
5128            If MS extensions are enabled, this is handled as an
5129              anonymous struct.
5130            Otherwise this is a forward declaration of a structure tag.
5131
5132          If this is something of the form "foo;" and foo is a TYPE_DECL, then
5133            If MS extensions are enabled and foo names a structure, then
5134              again this is an anonymous struct.
5135            Otherwise this is an error.
5136
5137          Oh what a horrid tangled web we weave.  I wonder if MS consciously
5138          took this from Plan 9 or if it was an accident of implementation
5139          that took root before someone noticed the bug...  */
5140
5141       tree type = declspecs->type;
5142       bool type_ok = (TREE_CODE (type) == RECORD_TYPE
5143                       || TREE_CODE (type) == UNION_TYPE);
5144       bool ok = false;
5145
5146       if (type_ok
5147           && (flag_ms_extensions || !declspecs->typedef_p))
5148         {
5149           if (flag_ms_extensions)
5150             ok = true;
5151           else if (flag_iso)
5152             ok = false;
5153           else if (TYPE_NAME (type) == NULL)
5154             ok = true;
5155           else
5156             ok = false;
5157         }
5158       if (!ok)
5159         {
5160           pedwarn ("declaration does not declare anything");
5161           return NULL_TREE;
5162         }
5163       if (pedantic)
5164         pedwarn ("ISO C doesn%'t support unnamed structs/unions");
5165     }
5166
5167   value = grokdeclarator (declarator, declspecs, FIELD, false,
5168                           width ? &width : NULL);
5169
5170   finish_decl (value, NULL_TREE, NULL_TREE);
5171   DECL_INITIAL (value) = width;
5172
5173   return value;
5174 }
5175 \f
5176 /* Generate an error for any duplicate field names in FIELDLIST.  Munge
5177    the list such that this does not present a problem later.  */
5178
5179 static void
5180 detect_field_duplicates (tree fieldlist)
5181 {
5182   tree x, y;
5183   int timeout = 10;
5184
5185   /* First, see if there are more than "a few" fields.
5186      This is trivially true if there are zero or one fields.  */
5187   if (!fieldlist)
5188     return;
5189   x = TREE_CHAIN (fieldlist);
5190   if (!x)
5191     return;
5192   do {
5193     timeout--;
5194     x = TREE_CHAIN (x);
5195   } while (timeout > 0 && x);
5196
5197   /* If there were "few" fields, avoid the overhead of allocating
5198      a hash table.  Instead just do the nested traversal thing.  */
5199   if (timeout > 0)
5200     {
5201       for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
5202         if (DECL_NAME (x))
5203           {
5204             for (y = fieldlist; y != x; y = TREE_CHAIN (y))
5205               if (DECL_NAME (y) == DECL_NAME (x))
5206                 {
5207                   error ("%Jduplicate member %qD", x, x);
5208                   DECL_NAME (x) = NULL_TREE;
5209                 }
5210           }
5211     }
5212   else
5213     {
5214       htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
5215       void **slot;
5216
5217       for (x = fieldlist; x ; x = TREE_CHAIN (x))
5218         if ((y = DECL_NAME (x)) != 0)
5219           {
5220             slot = htab_find_slot (htab, y, INSERT);
5221             if (*slot)
5222               {
5223                 error ("%Jduplicate member %qD", x, x);
5224                 DECL_NAME (x) = NULL_TREE;
5225               }
5226             *slot = y;
5227           }
5228
5229       htab_delete (htab);
5230     }
5231 }
5232
5233 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5234    FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5235    ATTRIBUTES are attributes to be applied to the structure.  */
5236
5237 tree
5238 finish_struct (tree t, tree fieldlist, tree attributes)
5239 {
5240   tree x;
5241   bool toplevel = file_scope == current_scope;
5242   int saw_named_field;
5243
5244   /* If this type was previously laid out as a forward reference,
5245      make sure we lay it out again.  */
5246
5247   TYPE_SIZE (t) = 0;
5248
5249   decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5250
5251   if (pedantic)
5252     {
5253       for (x = fieldlist; x; x = TREE_CHAIN (x))
5254         if (DECL_NAME (x) != 0)
5255           break;
5256
5257       if (x == 0)
5258         {
5259           if (TREE_CODE (t) == UNION_TYPE)
5260             {
5261               if (fieldlist)
5262                 pedwarn ("union has no named members");
5263               else
5264                 pedwarn ("union has no members");
5265             }
5266           else
5267             {
5268               if (fieldlist)
5269                 pedwarn ("struct has no named members");
5270               else
5271                 pedwarn ("struct has no members");
5272             }
5273         }
5274     }
5275
5276   /* Install struct as DECL_CONTEXT of each field decl.
5277      Also process specified field sizes, found in the DECL_INITIAL,
5278      storing 0 there after the type has been changed to precision equal
5279      to its width, rather than the precision of the specified standard
5280      type.  (Correct layout requires the original type to have been preserved
5281      until now.)  */
5282
5283   saw_named_field = 0;
5284   for (x = fieldlist; x; x = TREE_CHAIN (x))
5285     {
5286       DECL_CONTEXT (x) = t;
5287       DECL_PACKED (x) |= TYPE_PACKED (t);
5288
5289       /* If any field is const, the structure type is pseudo-const.  */
5290       if (TREE_READONLY (x))
5291         C_TYPE_FIELDS_READONLY (t) = 1;
5292       else
5293         {
5294           /* A field that is pseudo-const makes the structure likewise.  */
5295           tree t1 = TREE_TYPE (x);
5296           while (TREE_CODE (t1) == ARRAY_TYPE)
5297             t1 = TREE_TYPE (t1);
5298           if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5299               && C_TYPE_FIELDS_READONLY (t1))
5300             C_TYPE_FIELDS_READONLY (t) = 1;
5301         }
5302
5303       /* Any field that is volatile means variables of this type must be
5304          treated in some ways as volatile.  */
5305       if (TREE_THIS_VOLATILE (x))
5306         C_TYPE_FIELDS_VOLATILE (t) = 1;
5307
5308       /* Any field of nominal variable size implies structure is too.  */
5309       if (C_DECL_VARIABLE_SIZE (x))
5310         C_TYPE_VARIABLE_SIZE (t) = 1;
5311
5312       if (DECL_INITIAL (x))
5313         {
5314           unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5315           DECL_SIZE (x) = bitsize_int (width);
5316           DECL_BIT_FIELD (x) = 1;
5317           SET_DECL_C_BIT_FIELD (x);
5318         }
5319
5320       /* Detect flexible array member in an invalid context.  */
5321       if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5322           && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5323           && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5324           && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5325         {
5326           if (TREE_CODE (t) == UNION_TYPE)
5327             {
5328               error ("%Jflexible array member in union", x);
5329               TREE_TYPE (x) = error_mark_node;
5330             }
5331           else if (TREE_CHAIN (x) != NULL_TREE)
5332             {
5333               error ("%Jflexible array member not at end of struct", x);
5334               TREE_TYPE (x) = error_mark_node;
5335             }
5336           else if (!saw_named_field)
5337             {
5338               error ("%Jflexible array member in otherwise empty struct", x);
5339               TREE_TYPE (x) = error_mark_node;
5340             }
5341         }
5342
5343       if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE
5344           && flexible_array_type_p (TREE_TYPE (x)))
5345         pedwarn ("%Jinvalid use of structure with flexible array member", x);
5346
5347       if (DECL_NAME (x))
5348         saw_named_field = 1;
5349     }
5350
5351   detect_field_duplicates (fieldlist);
5352
5353   /* Now we have the nearly final fieldlist.  Record it,
5354      then lay out the structure or union (including the fields).  */
5355
5356   TYPE_FIELDS (t) = fieldlist;
5357
5358   layout_type (t);
5359
5360   /* Give bit-fields their proper types.  */
5361   {
5362     tree *fieldlistp = &fieldlist;
5363     while (*fieldlistp)
5364       if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
5365           && TREE_TYPE (*fieldlistp) != error_mark_node)
5366         {
5367           unsigned HOST_WIDE_INT width
5368             = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
5369           tree type = TREE_TYPE (*fieldlistp);
5370           if (width != TYPE_PRECISION (type))
5371             {
5372               TREE_TYPE (*fieldlistp)
5373                 = build_nonstandard_integer_type (width, TYPE_UNSIGNED (type));
5374               DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
5375             }
5376           DECL_INITIAL (*fieldlistp) = 0;
5377         }
5378       else
5379         fieldlistp = &TREE_CHAIN (*fieldlistp);
5380   }
5381
5382   /* Now we have the truly final field list.
5383      Store it in this type and in the variants.  */
5384
5385   TYPE_FIELDS (t) = fieldlist;
5386
5387   /* If there are lots of fields, sort so we can look through them fast.
5388      We arbitrarily consider 16 or more elts to be "a lot".  */
5389
5390   {
5391     int len = 0;
5392
5393     for (x = fieldlist; x; x = TREE_CHAIN (x))
5394       {
5395         if (len > 15 || DECL_NAME (x) == NULL)
5396           break;
5397         len += 1;
5398       }
5399
5400     if (len > 15)
5401       {
5402         tree *field_array;
5403         struct lang_type *space;
5404         struct sorted_fields_type *space2;
5405
5406         len += list_length (x);
5407
5408         /* Use the same allocation policy here that make_node uses, to
5409           ensure that this lives as long as the rest of the struct decl.
5410           All decls in an inline function need to be saved.  */
5411
5412         space = GGC_CNEW (struct lang_type);
5413         space2 = GGC_NEWVAR (struct sorted_fields_type,
5414                              sizeof (struct sorted_fields_type) + len * sizeof (tree));
5415
5416         len = 0;
5417         space->s = space2;
5418         field_array = &space2->elts[0];
5419         for (x = fieldlist; x; x = TREE_CHAIN (x))
5420           {
5421             field_array[len++] = x;
5422
5423             /* If there is anonymous struct or union, break out of the loop.  */
5424             if (DECL_NAME (x) == NULL)
5425               break;
5426           }
5427         /* Found no anonymous struct/union.  Add the TYPE_LANG_SPECIFIC.  */
5428         if (x == NULL)
5429           {
5430             TYPE_LANG_SPECIFIC (t) = space;
5431             TYPE_LANG_SPECIFIC (t)->s->len = len;
5432             field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5433             qsort (field_array, len, sizeof (tree), field_decl_cmp);
5434           }
5435       }
5436   }
5437
5438   for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5439     {
5440       TYPE_FIELDS (x) = TYPE_FIELDS (t);
5441       TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5442       TYPE_ALIGN (x) = TYPE_ALIGN (t);
5443       TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t);
5444     }
5445
5446   /* If this was supposed to be a transparent union, but we can't
5447      make it one, warn and turn off the flag.  */
5448   if (TREE_CODE (t) == UNION_TYPE
5449       && TYPE_TRANSPARENT_UNION (t)
5450       && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
5451     {
5452       TYPE_TRANSPARENT_UNION (t) = 0;
5453       warning (0, "union cannot be made transparent");
5454     }
5455
5456   /* If this structure or union completes the type of any previous
5457      variable declaration, lay it out and output its rtl.  */
5458   for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5459        x;
5460        x = TREE_CHAIN (x))
5461     {
5462       tree decl = TREE_VALUE (x);
5463       if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5464         layout_array_type (TREE_TYPE (decl));
5465       if (TREE_CODE (decl) != TYPE_DECL)
5466         {
5467           layout_decl (decl, 0);
5468           if (c_dialect_objc ())
5469             objc_check_decl (decl);
5470           rest_of_decl_compilation (decl, toplevel, 0);
5471           if (!toplevel)
5472             expand_decl (decl);
5473         }
5474     }
5475   C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5476
5477   /* Finish debugging output for this type.  */
5478   rest_of_type_compilation (t, toplevel);
5479
5480   /* If we're inside a function proper, i.e. not file-scope and not still
5481      parsing parameters, then arrange for the size of a variable sized type
5482      to be bound now.  */
5483   if (cur_stmt_list && variably_modified_type_p (t, NULL))
5484     add_stmt (build_stmt (DECL_EXPR, build_decl (TYPE_DECL, NULL, t)));
5485
5486   return t;
5487 }
5488
5489 /* Lay out the type T, and its element type, and so on.  */
5490
5491 static void
5492 layout_array_type (tree t)
5493 {
5494   if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5495     layout_array_type (TREE_TYPE (t));
5496   layout_type (t);
5497 }
5498 \f
5499 /* Begin compiling the definition of an enumeration type.
5500    NAME is its name (or null if anonymous).
5501    Returns the type object, as yet incomplete.
5502    Also records info about it so that build_enumerator
5503    may be used to declare the individual values as they are read.  */
5504
5505 tree
5506 start_enum (tree name)
5507 {
5508   tree enumtype = 0;
5509
5510   /* If this is the real definition for a previous forward reference,
5511      fill in the contents in the same object that used to be the
5512      forward reference.  */
5513
5514   if (name != 0)
5515     enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5516
5517   if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5518     {
5519       enumtype = make_node (ENUMERAL_TYPE);
5520       pushtag (name, enumtype);
5521     }
5522
5523   if (C_TYPE_BEING_DEFINED (enumtype))
5524     error ("nested redefinition of %<enum %E%>", name);
5525
5526   C_TYPE_BEING_DEFINED (enumtype) = 1;
5527
5528   if (TYPE_VALUES (enumtype) != 0)
5529     {
5530       /* This enum is a named one that has been declared already.  */
5531       error ("redeclaration of %<enum %E%>", name);
5532
5533       /* Completely replace its old definition.
5534          The old enumerators remain defined, however.  */
5535       TYPE_VALUES (enumtype) = 0;
5536     }
5537
5538   enum_next_value = integer_zero_node;
5539   enum_overflow = 0;
5540
5541   if (flag_short_enums)
5542     TYPE_PACKED (enumtype) = 1;
5543
5544   return enumtype;
5545 }
5546
5547 /* After processing and defining all the values of an enumeration type,
5548    install their decls in the enumeration type and finish it off.
5549    ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5550    and ATTRIBUTES are the specified attributes.
5551    Returns ENUMTYPE.  */
5552
5553 tree
5554 finish_enum (tree enumtype, tree values, tree attributes)
5555 {
5556   tree pair, tem;
5557   tree minnode = 0, maxnode = 0;
5558   int precision, unsign;
5559   bool toplevel = (file_scope == current_scope);
5560   struct lang_type *lt;
5561
5562   decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5563
5564   /* Calculate the maximum value of any enumerator in this type.  */
5565
5566   if (values == error_mark_node)
5567     minnode = maxnode = integer_zero_node;
5568   else
5569     {
5570       minnode = maxnode = TREE_VALUE (values);
5571       for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5572         {
5573           tree value = TREE_VALUE (pair);
5574           if (tree_int_cst_lt (maxnode, value))
5575             maxnode = value;
5576           if (tree_int_cst_lt (value, minnode))
5577             minnode = value;
5578         }
5579     }
5580
5581   /* Construct the final type of this enumeration.  It is the same
5582      as one of the integral types - the narrowest one that fits, except
5583      that normally we only go as narrow as int - and signed iff any of
5584      the values are negative.  */
5585   unsign = (tree_int_cst_sgn (minnode) >= 0);
5586   precision = MAX (min_precision (minnode, unsign),
5587                    min_precision (maxnode, unsign));
5588
5589   if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5590     {
5591       tem = c_common_type_for_size (precision, unsign);
5592       if (tem == NULL)
5593         {
5594           warning (0, "enumeration values exceed range of largest integer");
5595           tem = long_long_integer_type_node;
5596         }
5597     }
5598   else
5599     tem = unsign ? unsigned_type_node : integer_type_node;
5600
5601   TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
5602   TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
5603   TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
5604   TYPE_SIZE (enumtype) = 0;
5605
5606   /* If the precision of the type was specific with an attribute and it
5607      was too small, give an error.  Otherwise, use it.  */
5608   if (TYPE_PRECISION (enumtype))
5609     {
5610       if (precision > TYPE_PRECISION (enumtype))
5611         error ("specified mode too small for enumeral values");
5612     }
5613   else
5614     TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
5615
5616   layout_type (enumtype);
5617
5618   if (values != error_mark_node)
5619     {
5620       /* Change the type of the enumerators to be the enum type.  We
5621          need to do this irrespective of the size of the enum, for
5622          proper type checking.  Replace the DECL_INITIALs of the
5623          enumerators, and the value slots of the list, with copies
5624          that have the enum type; they cannot be modified in place
5625          because they may be shared (e.g.  integer_zero_node) Finally,
5626          change the purpose slots to point to the names of the decls.  */
5627       for (pair = values; pair; pair = TREE_CHAIN (pair))
5628         {
5629           tree enu = TREE_PURPOSE (pair);
5630           tree ini = DECL_INITIAL (enu);
5631
5632           TREE_TYPE (enu) = enumtype;
5633
5634           /* The ISO C Standard mandates enumerators to have type int,
5635              even though the underlying type of an enum type is
5636              unspecified.  Here we convert any enumerators that fit in
5637              an int to type int, to avoid promotions to unsigned types
5638              when comparing integers with enumerators that fit in the
5639              int range.  When -pedantic is given, build_enumerator()
5640              would have already taken care of those that don't fit.  */
5641           if (int_fits_type_p (ini, integer_type_node))
5642             tem = integer_type_node;
5643           else
5644             tem = enumtype;
5645           ini = convert (tem, ini);
5646
5647           DECL_INITIAL (enu) = ini;
5648           TREE_PURPOSE (pair) = DECL_NAME (enu);
5649           TREE_VALUE (pair) = ini;
5650         }
5651
5652       TYPE_VALUES (enumtype) = values;
5653     }
5654
5655   /* Record the min/max values so that we can warn about bit-field
5656      enumerations that are too small for the values.  */
5657   lt = GGC_CNEW (struct lang_type);
5658   lt->enum_min = minnode;
5659   lt->enum_max = maxnode;
5660   TYPE_LANG_SPECIFIC (enumtype) = lt;
5661
5662   /* Fix up all variant types of this enum type.  */
5663   for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5664     {
5665       if (tem == enumtype)
5666         continue;
5667       TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5668       TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5669       TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5670       TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5671       TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5672       TYPE_MODE (tem) = TYPE_MODE (enumtype);
5673       TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5674       TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5675       TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5676       TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
5677       TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
5678     }
5679
5680   /* Finish debugging output for this type.  */
5681   rest_of_type_compilation (enumtype, toplevel);
5682
5683   return enumtype;
5684 }
5685
5686 /* Build and install a CONST_DECL for one value of the
5687    current enumeration type (one that was begun with start_enum).
5688    Return a tree-list containing the CONST_DECL and its value.
5689    Assignment of sequential values by default is handled here.  */
5690
5691 tree
5692 build_enumerator (tree name, tree value)
5693 {
5694   tree decl, type;
5695
5696   /* Validate and default VALUE.  */
5697
5698   if (value != 0)
5699     {
5700       /* Don't issue more errors for error_mark_node (i.e. an
5701          undeclared identifier) - just ignore the value expression.  */
5702       if (value == error_mark_node)
5703         value = 0;
5704       else if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
5705                || TREE_CODE (value) != INTEGER_CST)
5706         {
5707           error ("enumerator value for %qE is not an integer constant", name);
5708           value = 0;
5709         }
5710       else
5711         {
5712           value = default_conversion (value);
5713           constant_expression_warning (value);
5714         }
5715     }
5716
5717   /* Default based on previous value.  */
5718   /* It should no longer be possible to have NON_LVALUE_EXPR
5719      in the default.  */
5720   if (value == 0)
5721     {
5722       value = enum_next_value;
5723       if (enum_overflow)
5724         error ("overflow in enumeration values");
5725     }
5726
5727   if (pedantic && !int_fits_type_p (value, integer_type_node))
5728     {
5729       pedwarn ("ISO C restricts enumerator values to range of %<int%>");
5730       /* XXX This causes -pedantic to change the meaning of the program.
5731          Remove?  -zw 2004-03-15  */
5732       value = convert (integer_type_node, value);
5733     }
5734
5735   /* Set basis for default for next value.  */
5736   enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
5737   enum_overflow = tree_int_cst_lt (enum_next_value, value);
5738
5739   /* Now create a declaration for the enum value name.  */
5740
5741   type = TREE_TYPE (value);
5742   type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5743                                       TYPE_PRECISION (integer_type_node)),
5744                                  (TYPE_PRECISION (type)
5745                                   >= TYPE_PRECISION (integer_type_node)
5746                                   && TYPE_UNSIGNED (type)));
5747
5748   decl = build_decl (CONST_DECL, name, type);
5749   DECL_INITIAL (decl) = convert (type, value);
5750   pushdecl (decl);
5751
5752   return tree_cons (decl, value, NULL_TREE);
5753 }
5754
5755 \f
5756 /* Create the FUNCTION_DECL for a function definition.
5757    DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5758    the declaration; they describe the function's name and the type it returns,
5759    but twisted together in a fashion that parallels the syntax of C.
5760
5761    This function creates a binding context for the function body
5762    as well as setting up the FUNCTION_DECL in current_function_decl.
5763
5764    Returns 1 on success.  If the DECLARATOR is not suitable for a function
5765    (it defines a datum instead), we return 0, which tells
5766    yyparse to report a parse error.  */
5767
5768 int
5769 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
5770                 tree attributes)
5771 {
5772   tree decl1, old_decl;
5773   tree restype, resdecl;
5774   struct c_label_context_se *nstack_se;
5775   struct c_label_context_vm *nstack_vm;
5776
5777   current_function_returns_value = 0;  /* Assume, until we see it does.  */
5778   current_function_returns_null = 0;
5779   current_function_returns_abnormally = 0;
5780   warn_about_return_type = 0;
5781   current_extern_inline = 0;
5782   c_switch_stack = NULL;
5783
5784   nstack_se = XOBNEW (&parser_obstack, struct c_label_context_se);
5785   nstack_se->labels_def = NULL;
5786   nstack_se->labels_used = NULL;
5787   nstack_se->next = label_context_stack_se;
5788   label_context_stack_se = nstack_se;
5789
5790   nstack_vm = XOBNEW (&parser_obstack, struct c_label_context_vm);
5791   nstack_vm->labels_def = NULL;
5792   nstack_vm->labels_used = NULL;
5793   nstack_vm->scope = 0;
5794   nstack_vm->next = label_context_stack_vm;
5795   label_context_stack_vm = nstack_vm;
5796
5797   /* Indicate no valid break/continue context by setting these variables
5798      to some non-null, non-label value.  We'll notice and emit the proper
5799      error message in c_finish_bc_stmt.  */
5800   c_break_label = c_cont_label = size_zero_node;
5801
5802   decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL);
5803
5804   /* If the declarator is not suitable for a function definition,
5805      cause a syntax error.  */
5806   if (decl1 == 0)
5807     {
5808       label_context_stack_se = label_context_stack_se->next;
5809       label_context_stack_vm = label_context_stack_vm->next;
5810       return 0;
5811     }
5812
5813   decl_attributes (&decl1, attributes, 0);
5814
5815   if (DECL_DECLARED_INLINE_P (decl1)
5816       && DECL_UNINLINABLE (decl1)
5817       && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
5818     warning (OPT_Wattributes, "%Jinline function %qD given attribute noinline",
5819              decl1, decl1);
5820
5821   announce_function (decl1);
5822
5823   if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
5824     {
5825       error ("return type is an incomplete type");
5826       /* Make it return void instead.  */
5827       TREE_TYPE (decl1)
5828         = build_function_type (void_type_node,
5829                                TYPE_ARG_TYPES (TREE_TYPE (decl1)));
5830     }
5831
5832   if (warn_about_return_type)
5833     pedwarn_c99 ("return type defaults to %<int%>");
5834
5835   /* Make the init_value nonzero so pushdecl knows this is not tentative.
5836      error_mark_node is replaced below (in pop_scope) with the BLOCK.  */
5837   DECL_INITIAL (decl1) = error_mark_node;
5838
5839   /* If this definition isn't a prototype and we had a prototype declaration
5840      before, copy the arg type info from that prototype.  */
5841   old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
5842   current_function_prototype_locus = UNKNOWN_LOCATION;
5843   current_function_prototype_built_in = false;
5844   current_function_prototype_arg_types = NULL_TREE;
5845   if (TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
5846     {
5847       if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
5848           && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
5849                         TREE_TYPE (TREE_TYPE (old_decl))))
5850         {
5851           TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
5852                                               TREE_TYPE (decl1));
5853           current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
5854           current_function_prototype_built_in
5855             = C_DECL_BUILTIN_PROTOTYPE (old_decl);
5856           current_function_prototype_arg_types
5857             = TYPE_ARG_TYPES (TREE_TYPE (decl1));
5858         }
5859       if (TREE_PUBLIC (decl1))
5860         {
5861           /* If there is an external prototype declaration of this
5862              function, record its location but do not copy information
5863              to this decl.  This may be an invisible declaration
5864              (built-in or in a scope which has finished) or simply
5865              have more refined argument types than any declaration
5866              found above.  */
5867           struct c_binding *b;
5868           for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
5869             if (B_IN_SCOPE (b, external_scope))
5870               break;
5871           if (b)
5872             {
5873               tree ext_decl, ext_type;
5874               ext_decl = b->decl;
5875               ext_type = b->type ? b->type : TREE_TYPE (ext_decl);
5876               if (TREE_CODE (ext_type) == FUNCTION_TYPE
5877                   && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
5878                                 TREE_TYPE (ext_type)))
5879                 {
5880                   current_function_prototype_locus
5881                     = DECL_SOURCE_LOCATION (ext_decl);
5882                   current_function_prototype_built_in
5883                     = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
5884                   current_function_prototype_arg_types
5885                     = TYPE_ARG_TYPES (ext_type);
5886                 }
5887             }
5888         }
5889     }
5890
5891   /* Optionally warn of old-fashioned def with no previous prototype.  */
5892   if (warn_strict_prototypes
5893       && old_decl != error_mark_node
5894       && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
5895       && C_DECL_ISNT_PROTOTYPE (old_decl))
5896     warning (OPT_Wstrict_prototypes,
5897              "function declaration isn%'t a prototype");
5898   /* Optionally warn of any global def with no previous prototype.  */
5899   else if (warn_missing_prototypes
5900            && old_decl != error_mark_node
5901            && TREE_PUBLIC (decl1)
5902            && !MAIN_NAME_P (DECL_NAME (decl1))
5903            && C_DECL_ISNT_PROTOTYPE (old_decl))
5904     warning (OPT_Wmissing_prototypes, "%Jno previous prototype for %qD",
5905              decl1, decl1);
5906   /* Optionally warn of any def with no previous prototype
5907      if the function has already been used.  */
5908   else if (warn_missing_prototypes
5909            && old_decl != 0
5910            && old_decl != error_mark_node
5911            && TREE_USED (old_decl)
5912            && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
5913     warning (OPT_Wmissing_prototypes,
5914              "%J%qD was used with no prototype before its definition",
5915              decl1, decl1);
5916   /* Optionally warn of any global def with no previous declaration.  */
5917   else if (warn_missing_declarations
5918            && TREE_PUBLIC (decl1)
5919            && old_decl == 0
5920            && !MAIN_NAME_P (DECL_NAME (decl1)))
5921     warning (OPT_Wmissing_declarations, "%Jno previous declaration for %qD",
5922              decl1, decl1);
5923   /* Optionally warn of any def with no previous declaration
5924      if the function has already been used.  */
5925   else if (warn_missing_declarations
5926            && old_decl != 0
5927            && old_decl != error_mark_node
5928            && TREE_USED (old_decl)
5929            && C_DECL_IMPLICIT (old_decl))
5930     warning (OPT_Wmissing_declarations,
5931              "%J%qD was used with no declaration before its definition",
5932              decl1, decl1);
5933
5934   /* This is a definition, not a reference.
5935      So normally clear DECL_EXTERNAL.
5936      However, `extern inline' acts like a declaration
5937      except for defining how to inline.  So set DECL_EXTERNAL in that case.  */
5938   DECL_EXTERNAL (decl1) = current_extern_inline;
5939
5940   /* This function exists in static storage.
5941      (This does not mean `static' in the C sense!)  */
5942   TREE_STATIC (decl1) = 1;
5943
5944   /* A nested function is not global.  */
5945   if (current_function_decl != 0)
5946     TREE_PUBLIC (decl1) = 0;
5947
5948   /* This is the earliest point at which we might know the assembler
5949      name of the function.  Thus, if it's set before this, die horribly.  */
5950   gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
5951
5952   /* If #pragma weak was used, mark the decl weak now.  */
5953   if (current_scope == file_scope)
5954     maybe_apply_pragma_weak (decl1);
5955
5956   /* Warn for unlikely, improbable, or stupid declarations of `main'.  */
5957   if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
5958     {
5959       tree args;
5960       int argct = 0;
5961
5962       if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5963           != integer_type_node)
5964         pedwarn ("%Jreturn type of %qD is not %<int%>", decl1, decl1);
5965
5966       for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
5967            args = TREE_CHAIN (args))
5968         {
5969           tree type = args ? TREE_VALUE (args) : 0;
5970
5971           if (type == void_type_node)
5972             break;
5973
5974           ++argct;
5975           switch (argct)
5976             {
5977             case 1:
5978               if (TYPE_MAIN_VARIANT (type) != integer_type_node)
5979                 pedwarn ("%Jfirst argument of %qD should be %<int%>",
5980                          decl1, decl1);
5981               break;
5982
5983             case 2:
5984               if (TREE_CODE (type) != POINTER_TYPE
5985                   || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5986                   || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5987                       != char_type_node))
5988                 pedwarn ("%Jsecond argument of %qD should be %<char **%>",
5989                          decl1, decl1);
5990               break;
5991
5992             case 3:
5993               if (TREE_CODE (type) != POINTER_TYPE
5994                   || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5995                   || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5996                       != char_type_node))
5997                 pedwarn ("%Jthird argument of %qD should probably be "
5998                          "%<char **%>", decl1, decl1);
5999               break;
6000             }
6001         }
6002
6003       /* It is intentional that this message does not mention the third
6004          argument because it's only mentioned in an appendix of the
6005          standard.  */
6006       if (argct > 0 && (argct < 2 || argct > 3))
6007         pedwarn ("%J%qD takes only zero or two arguments", decl1, decl1);
6008
6009       if (!TREE_PUBLIC (decl1))
6010         pedwarn ("%J%qD is normally a non-static function", decl1, decl1);
6011     }
6012
6013   /* Record the decl so that the function name is defined.
6014      If we already have a decl for this name, and it is a FUNCTION_DECL,
6015      use the old decl.  */
6016
6017   current_function_decl = pushdecl (decl1);
6018
6019   push_scope ();
6020   declare_parm_level ();
6021
6022   restype = TREE_TYPE (TREE_TYPE (current_function_decl));
6023   /* Promote the value to int before returning it.  */
6024   if (c_promoting_integer_type_p (restype))
6025     {
6026       /* It retains unsignedness if not really getting wider.  */
6027       if (TYPE_UNSIGNED (restype)
6028           && (TYPE_PRECISION (restype)
6029                   == TYPE_PRECISION (integer_type_node)))
6030         restype = unsigned_type_node;
6031       else
6032         restype = integer_type_node;
6033     }
6034
6035   resdecl = build_decl (RESULT_DECL, NULL_TREE, restype);
6036   DECL_ARTIFICIAL (resdecl) = 1;
6037   DECL_IGNORED_P (resdecl) = 1;
6038   DECL_RESULT (current_function_decl) = resdecl;
6039
6040   start_fname_decls ();
6041
6042   return 1;
6043 }
6044 \f
6045 /* Subroutine of store_parm_decls which handles new-style function
6046    definitions (prototype format). The parms already have decls, so we
6047    need only record them as in effect and complain if any redundant
6048    old-style parm decls were written.  */
6049 static void
6050 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
6051 {
6052   tree decl;
6053
6054   if (current_scope->bindings)
6055     {
6056       error ("%Jold-style parameter declarations in prototyped "
6057              "function definition", fndecl);
6058
6059       /* Get rid of the old-style declarations.  */
6060       pop_scope ();
6061       push_scope ();
6062     }
6063   /* Don't issue this warning for nested functions, and don't issue this
6064      warning if we got here because ARG_INFO_TYPES was error_mark_node
6065      (this happens when a function definition has just an ellipsis in
6066      its parameter list).  */
6067   else if (!in_system_header && !current_function_scope
6068            && arg_info->types != error_mark_node)
6069     warning (OPT_Wtraditional,
6070              "%Jtraditional C rejects ISO C style function definitions",
6071              fndecl);
6072
6073   /* Now make all the parameter declarations visible in the function body.
6074      We can bypass most of the grunt work of pushdecl.  */
6075   for (decl = arg_info->parms; decl; decl = TREE_CHAIN (decl))
6076     {
6077       DECL_CONTEXT (decl) = current_function_decl;
6078       if (DECL_NAME (decl))
6079         bind (DECL_NAME (decl), decl, current_scope,
6080               /*invisible=*/false, /*nested=*/false);
6081       else
6082         error ("%Jparameter name omitted", decl);
6083     }
6084
6085   /* Record the parameter list in the function declaration.  */
6086   DECL_ARGUMENTS (fndecl) = arg_info->parms;
6087
6088   /* Now make all the ancillary declarations visible, likewise.  */
6089   for (decl = arg_info->others; decl; decl = TREE_CHAIN (decl))
6090     {
6091       DECL_CONTEXT (decl) = current_function_decl;
6092       if (DECL_NAME (decl))
6093         bind (DECL_NAME (decl), decl, current_scope,
6094               /*invisible=*/false, /*nested=*/false);
6095     }
6096
6097   /* And all the tag declarations.  */
6098   for (decl = arg_info->tags; decl; decl = TREE_CHAIN (decl))
6099     if (TREE_PURPOSE (decl))
6100       bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope,
6101             /*invisible=*/false, /*nested=*/false);
6102 }
6103
6104 /* Subroutine of store_parm_decls which handles old-style function
6105    definitions (separate parameter list and declarations).  */
6106
6107 static void
6108 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
6109 {
6110   struct c_binding *b;
6111   tree parm, decl, last;
6112   tree parmids = arg_info->parms;
6113
6114   /* We use DECL_WEAK as a flag to show which parameters have been
6115      seen already, since it is not used on PARM_DECL.  */
6116 #ifdef ENABLE_CHECKING
6117   for (b = current_scope->bindings; b; b = b->prev)
6118     gcc_assert (TREE_CODE (b->decl) != PARM_DECL || !DECL_WEAK (b->decl));
6119 #endif
6120
6121   if (!in_system_header)
6122     warning (OPT_Wold_style_definition, "%Jold-style function definition",
6123              fndecl);
6124
6125   /* Match each formal parameter name with its declaration.  Save each
6126      decl in the appropriate TREE_PURPOSE slot of the parmids chain.  */
6127   for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6128     {
6129       if (TREE_VALUE (parm) == 0)
6130         {
6131           error ("%Jparameter name missing from parameter list", fndecl);
6132           TREE_PURPOSE (parm) = 0;
6133           continue;
6134         }
6135
6136       b = I_SYMBOL_BINDING (TREE_VALUE (parm));
6137       if (b && B_IN_CURRENT_SCOPE (b))
6138         {
6139           decl = b->decl;
6140           /* If we got something other than a PARM_DECL it is an error.  */
6141           if (TREE_CODE (decl) != PARM_DECL)
6142             error ("%J%qD declared as a non-parameter", decl, decl);
6143           /* If the declaration is already marked, we have a duplicate
6144              name.  Complain and ignore the duplicate.  */
6145           else if (DECL_WEAK (decl))
6146             {
6147               error ("%Jmultiple parameters named %qD", decl, decl);
6148               TREE_PURPOSE (parm) = 0;
6149               continue;
6150             }
6151           /* If the declaration says "void", complain and turn it into
6152              an int.  */
6153           else if (VOID_TYPE_P (TREE_TYPE (decl)))
6154             {
6155               error ("%Jparameter %qD declared with void type", decl, decl);
6156               TREE_TYPE (decl) = integer_type_node;
6157               DECL_ARG_TYPE (decl) = integer_type_node;
6158               layout_decl (decl, 0);
6159             }
6160         }
6161       /* If no declaration found, default to int.  */
6162       else
6163         {
6164           decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
6165           DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
6166           DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
6167           pushdecl (decl);
6168
6169           if (flag_isoc99)
6170             pedwarn ("%Jtype of %qD defaults to %<int%>", decl, decl);
6171           else if (extra_warnings)
6172             warning (OPT_Wextra, "%Jtype of %qD defaults to %<int%>",
6173                      decl, decl);
6174         }
6175
6176       TREE_PURPOSE (parm) = decl;
6177       DECL_WEAK (decl) = 1;
6178     }
6179
6180   /* Now examine the parms chain for incomplete declarations
6181      and declarations with no corresponding names.  */
6182
6183   for (b = current_scope->bindings; b; b = b->prev)
6184     {
6185       parm = b->decl;
6186       if (TREE_CODE (parm) != PARM_DECL)
6187         continue;
6188
6189       if (TREE_TYPE (parm) != error_mark_node
6190           && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
6191         {
6192           error ("%Jparameter %qD has incomplete type", parm, parm);
6193           TREE_TYPE (parm) = error_mark_node;
6194         }
6195
6196       if (!DECL_WEAK (parm))
6197         {
6198           error ("%Jdeclaration for parameter %qD but no such parameter",
6199                  parm, parm);
6200
6201           /* Pretend the parameter was not missing.
6202              This gets us to a standard state and minimizes
6203              further error messages.  */
6204           parmids = chainon (parmids, tree_cons (parm, 0, 0));
6205         }
6206     }
6207
6208   /* Chain the declarations together in the order of the list of
6209      names.  Store that chain in the function decl, replacing the
6210      list of names.  Update the current scope to match.  */
6211   DECL_ARGUMENTS (fndecl) = 0;
6212
6213   for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6214     if (TREE_PURPOSE (parm))
6215       break;
6216   if (parm && TREE_PURPOSE (parm))
6217     {
6218       last = TREE_PURPOSE (parm);
6219       DECL_ARGUMENTS (fndecl) = last;
6220       DECL_WEAK (last) = 0;
6221
6222       for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
6223         if (TREE_PURPOSE (parm))
6224           {
6225             TREE_CHAIN (last) = TREE_PURPOSE (parm);
6226             last = TREE_PURPOSE (parm);
6227             DECL_WEAK (last) = 0;
6228           }
6229       TREE_CHAIN (last) = 0;
6230     }
6231
6232   /* If there was a previous prototype,
6233      set the DECL_ARG_TYPE of each argument according to
6234      the type previously specified, and report any mismatches.  */
6235
6236   if (current_function_prototype_arg_types)
6237     {
6238       tree type;
6239       for (parm = DECL_ARGUMENTS (fndecl),
6240              type = current_function_prototype_arg_types;
6241            parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
6242                              != void_type_node));
6243            parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6244         {
6245           if (parm == 0 || type == 0
6246               || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6247             {
6248               if (current_function_prototype_built_in)
6249                 warning (0, "number of arguments doesn%'t match "
6250                          "built-in prototype");
6251               else
6252                 {
6253                   error ("number of arguments doesn%'t match prototype");
6254                   error ("%Hprototype declaration",
6255                          &current_function_prototype_locus);
6256                 }
6257               break;
6258             }
6259           /* Type for passing arg must be consistent with that
6260              declared for the arg.  ISO C says we take the unqualified
6261              type for parameters declared with qualified type.  */
6262           if (!comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6263                           TYPE_MAIN_VARIANT (TREE_VALUE (type))))
6264             {
6265               if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6266                   == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6267                 {
6268                   /* Adjust argument to match prototype.  E.g. a previous
6269                      `int foo(float);' prototype causes
6270                      `int foo(x) float x; {...}' to be treated like
6271                      `int foo(float x) {...}'.  This is particularly
6272                      useful for argument types like uid_t.  */
6273                   DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6274
6275                   if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
6276                       && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6277                       && TYPE_PRECISION (TREE_TYPE (parm))
6278                       < TYPE_PRECISION (integer_type_node))
6279                     DECL_ARG_TYPE (parm) = integer_type_node;
6280
6281                   if (pedantic)
6282                     {
6283                       /* ??? Is it possible to get here with a
6284                          built-in prototype or will it always have
6285                          been diagnosed as conflicting with an
6286                          old-style definition and discarded?  */
6287                       if (current_function_prototype_built_in)
6288                         warning (0, "promoted argument %qD "
6289                                  "doesn%'t match built-in prototype", parm);
6290                       else
6291                         {
6292                           pedwarn ("promoted argument %qD "
6293                                    "doesn%'t match prototype", parm);
6294                           pedwarn ("%Hprototype declaration",
6295                                    &current_function_prototype_locus);
6296                         }
6297                     }
6298                 }
6299               else
6300                 {
6301                   if (current_function_prototype_built_in)
6302                     warning (0, "argument %qD doesn%'t match "
6303                              "built-in prototype", parm);
6304                   else
6305                     {
6306                       error ("argument %qD doesn%'t match prototype", parm);
6307                       error ("%Hprototype declaration",
6308                              &current_function_prototype_locus);
6309                     }
6310                 }
6311             }
6312         }
6313       TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6314     }
6315
6316   /* Otherwise, create a prototype that would match.  */
6317
6318   else
6319     {
6320       tree actual = 0, last = 0, type;
6321
6322       for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6323         {
6324           type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6325           if (last)
6326             TREE_CHAIN (last) = type;
6327           else
6328             actual = type;
6329           last = type;
6330         }
6331       type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6332       if (last)
6333         TREE_CHAIN (last) = type;
6334       else
6335         actual = type;
6336
6337       /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6338          of the type of this function, but we need to avoid having this
6339          affect the types of other similarly-typed functions, so we must
6340          first force the generation of an identical (but separate) type
6341          node for the relevant function type.  The new node we create
6342          will be a variant of the main variant of the original function
6343          type.  */
6344
6345       TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
6346
6347       TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6348     }
6349 }
6350
6351 /* Store parameter declarations passed in ARG_INFO into the current
6352    function declaration.  */
6353
6354 void
6355 store_parm_decls_from (struct c_arg_info *arg_info)
6356 {
6357   current_function_arg_info = arg_info;
6358   store_parm_decls ();
6359 }
6360
6361 /* Store the parameter declarations into the current function declaration.
6362    This is called after parsing the parameter declarations, before
6363    digesting the body of the function.
6364
6365    For an old-style definition, construct a prototype out of the old-style
6366    parameter declarations and inject it into the function's type.  */
6367
6368 void
6369 store_parm_decls (void)
6370 {
6371   tree fndecl = current_function_decl;
6372   bool proto;
6373
6374   /* The argument information block for FNDECL.  */
6375   struct c_arg_info *arg_info = current_function_arg_info;
6376   current_function_arg_info = 0;
6377
6378   /* True if this definition is written with a prototype.  Note:
6379      despite C99 6.7.5.3p14, we can *not* treat an empty argument
6380      list in a function definition as equivalent to (void) -- an
6381      empty argument list specifies the function has no parameters,
6382      but only (void) sets up a prototype for future calls.  */
6383   proto = arg_info->types != 0;
6384
6385   if (proto)
6386     store_parm_decls_newstyle (fndecl, arg_info);
6387   else
6388     store_parm_decls_oldstyle (fndecl, arg_info);
6389
6390   /* The next call to push_scope will be a function body.  */
6391
6392   next_is_function_body = true;
6393
6394   /* Write a record describing this function definition to the prototypes
6395      file (if requested).  */
6396
6397   gen_aux_info_record (fndecl, 1, 0, proto);
6398
6399   /* Initialize the RTL code for the function.  */
6400   allocate_struct_function (fndecl);
6401
6402   /* Begin the statement tree for this function.  */
6403   DECL_SAVED_TREE (fndecl) = push_stmt_list ();
6404
6405   /* ??? Insert the contents of the pending sizes list into the function
6406      to be evaluated.  The only reason left to have this is
6407         void foo(int n, int array[n++])
6408      because we throw away the array type in favor of a pointer type, and
6409      thus won't naturally see the SAVE_EXPR containing the increment.  All
6410      other pending sizes would be handled by gimplify_parameters.  */
6411   {
6412     tree t;
6413     for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t))
6414       add_stmt (TREE_VALUE (t));
6415   }
6416
6417   /* Even though we're inside a function body, we still don't want to
6418      call expand_expr to calculate the size of a variable-sized array.
6419      We haven't necessarily assigned RTL to all variables yet, so it's
6420      not safe to try to expand expressions involving them.  */
6421   cfun->x_dont_save_pending_sizes_p = 1;
6422 }
6423 \f
6424 /* Handle attribute((warn_unused_result)) on FNDECL and all its nested
6425    functions.  */
6426
6427 static void
6428 c_warn_unused_result_recursively (tree fndecl)
6429 {
6430   struct cgraph_node *cgn;
6431
6432   /* Handle attribute((warn_unused_result)).  Relies on gimple input.  */
6433   c_warn_unused_result (&DECL_SAVED_TREE (fndecl));
6434
6435   /* Finalize all nested functions now.  */
6436   cgn = cgraph_node (fndecl);
6437   for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
6438     c_warn_unused_result_recursively (cgn->decl);
6439 }
6440
6441 /* Finish up a function declaration and compile that function
6442    all the way to assembler language output.  The free the storage
6443    for the function definition.
6444
6445    This is called after parsing the body of the function definition.  */
6446
6447 void
6448 finish_function (void)
6449 {
6450   tree fndecl = current_function_decl;
6451
6452   label_context_stack_se = label_context_stack_se->next;
6453   label_context_stack_vm = label_context_stack_vm->next;
6454
6455   if (TREE_CODE (fndecl) == FUNCTION_DECL
6456       && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6457     {
6458       tree args = DECL_ARGUMENTS (fndecl);
6459       for (; args; args = TREE_CHAIN (args))
6460         {
6461           tree type = TREE_TYPE (args);
6462           if (INTEGRAL_TYPE_P (type)
6463               && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6464             DECL_ARG_TYPE (args) = integer_type_node;
6465         }
6466     }
6467
6468   if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6469     BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6470
6471   /* Must mark the RESULT_DECL as being in this function.  */
6472
6473   if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6474     DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6475
6476   if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6477     {
6478       if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6479           != integer_type_node)
6480         {
6481           /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6482              If warn_main is -1 (-Wno-main) we don't want to be warned.  */
6483           if (!warn_main)
6484             pedwarn ("%Jreturn type of %qD is not %<int%>", fndecl, fndecl);
6485         }
6486       else
6487         {
6488           if (flag_isoc99)
6489             {
6490               tree stmt = c_finish_return (integer_zero_node);
6491 #ifdef USE_MAPPED_LOCATION
6492               /* Hack.  We don't want the middle-end to warn that this return
6493                  is unreachable, so we mark its location as special.  Using
6494                  UNKNOWN_LOCATION has the problem that it gets clobbered in
6495                  annotate_one_with_locus.  A cleaner solution might be to
6496                  ensure ! should_carry_locus_p (stmt), but that needs a flag.
6497               */
6498               SET_EXPR_LOCATION (stmt, BUILTINS_LOCATION);
6499 #else
6500               /* Hack.  We don't want the middle-end to warn that this
6501                  return is unreachable, so put the statement on the
6502                  special line 0.  */
6503               annotate_with_file_line (stmt, input_filename, 0);
6504 #endif
6505             }
6506         }
6507     }
6508
6509   /* Tie off the statement tree for this function.  */
6510   DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
6511
6512   finish_fname_decls ();
6513
6514   /* Complain if there's just no return statement.  */
6515   if (warn_return_type
6516       && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6517       && !current_function_returns_value && !current_function_returns_null
6518       /* Don't complain if we are no-return.  */
6519       && !current_function_returns_abnormally
6520       /* Don't warn for main().  */
6521       && !MAIN_NAME_P (DECL_NAME (fndecl))
6522       /* Or if they didn't actually specify a return type.  */
6523       && !C_FUNCTION_IMPLICIT_INT (fndecl)
6524       /* Normally, with -Wreturn-type, flow will complain.  Unless we're an
6525          inline function, as we might never be compiled separately.  */
6526       && DECL_INLINE (fndecl))
6527     {
6528       warning (OPT_Wreturn_type,
6529                "no return statement in function returning non-void");
6530       TREE_NO_WARNING (fndecl) = 1;
6531     }
6532
6533   /* With just -Wextra, complain only if function returns both with
6534      and without a value.  */
6535   if (extra_warnings
6536       && current_function_returns_value
6537       && current_function_returns_null)
6538     warning (OPT_Wextra, "this function may return with or without a value");
6539
6540   /* Store the end of the function, so that we get good line number
6541      info for the epilogue.  */
6542   cfun->function_end_locus = input_location;
6543
6544   /* If we don't have ctors/dtors sections, and this is a static
6545      constructor or destructor, it must be recorded now.  */
6546   if (DECL_STATIC_CONSTRUCTOR (fndecl)
6547       && !targetm.have_ctors_dtors)
6548     static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6549   if (DECL_STATIC_DESTRUCTOR (fndecl)
6550       && !targetm.have_ctors_dtors)
6551     static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6552
6553   /* Finalize the ELF visibility for the function.  */
6554   c_determine_visibility (fndecl);
6555
6556   /* Genericize before inlining.  Delay genericizing nested functions
6557      until their parent function is genericized.  Since finalizing
6558      requires GENERIC, delay that as well.  */
6559
6560   if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
6561       && !undef_nested_function)
6562     {
6563       if (!decl_function_context (fndecl))
6564         {
6565           c_genericize (fndecl);
6566           c_warn_unused_result_recursively (fndecl);
6567
6568           /* ??? Objc emits functions after finalizing the compilation unit.
6569              This should be cleaned up later and this conditional removed.  */
6570           if (cgraph_global_info_ready)
6571             {
6572               c_expand_body (fndecl);
6573               return;
6574             }
6575
6576           cgraph_finalize_function (fndecl, false);
6577         }
6578       else
6579         {
6580           /* Register this function with cgraph just far enough to get it
6581             added to our parent's nested function list.  Handy, since the
6582             C front end doesn't have such a list.  */
6583           (void) cgraph_node (fndecl);
6584         }
6585     }
6586
6587   if (!decl_function_context (fndecl))
6588     undef_nested_function = false;
6589
6590   /* We're leaving the context of this function, so zap cfun.
6591      It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
6592      tree_rest_of_compilation.  */
6593   cfun = NULL;
6594   current_function_decl = NULL;
6595 }
6596
6597 /* Generate the RTL for the body of FNDECL.  */
6598
6599 void
6600 c_expand_body (tree fndecl)
6601 {
6602
6603   if (!DECL_INITIAL (fndecl)
6604       || DECL_INITIAL (fndecl) == error_mark_node)
6605     return;
6606
6607   tree_rest_of_compilation (fndecl);
6608
6609   if (DECL_STATIC_CONSTRUCTOR (fndecl)
6610       && targetm.have_ctors_dtors)
6611     targetm.asm_out.constructor (XEXP (DECL_RTL (fndecl), 0),
6612                                  DEFAULT_INIT_PRIORITY);
6613   if (DECL_STATIC_DESTRUCTOR (fndecl)
6614       && targetm.have_ctors_dtors)
6615     targetm.asm_out.destructor (XEXP (DECL_RTL (fndecl), 0),
6616                                 DEFAULT_INIT_PRIORITY);
6617 }
6618 \f
6619 /* Check the declarations given in a for-loop for satisfying the C99
6620    constraints.  */
6621 void
6622 check_for_loop_decls (void)
6623 {
6624   struct c_binding *b;
6625
6626   if (!flag_isoc99)
6627     {
6628       /* If we get here, declarations have been used in a for loop without
6629          the C99 for loop scope.  This doesn't make much sense, so don't
6630          allow it.  */
6631       error ("%<for%> loop initial declaration used outside C99 mode");
6632       return;
6633     }
6634   /* C99 subclause 6.8.5 paragraph 3:
6635
6636        [#3]  The  declaration  part  of  a for statement shall only
6637        declare identifiers for objects having storage class auto or
6638        register.
6639
6640      It isn't clear whether, in this sentence, "identifiers" binds to
6641      "shall only declare" or to "objects" - that is, whether all identifiers
6642      declared must be identifiers for objects, or whether the restriction
6643      only applies to those that are.  (A question on this in comp.std.c
6644      in November 2000 received no answer.)  We implement the strictest
6645      interpretation, to avoid creating an extension which later causes
6646      problems.  */
6647
6648   for (b = current_scope->bindings; b; b = b->prev)
6649     {
6650       tree id = b->id;
6651       tree decl = b->decl;
6652
6653       if (!id)
6654         continue;
6655
6656       switch (TREE_CODE (decl))
6657         {
6658         case VAR_DECL:
6659           if (TREE_STATIC (decl))
6660             error ("%Jdeclaration of static variable %qD in %<for%> loop "
6661                    "initial declaration", decl, decl);
6662           else if (DECL_EXTERNAL (decl))
6663             error ("%Jdeclaration of %<extern%> variable %qD in %<for%> loop "
6664                    "initial declaration", decl, decl);
6665           break;
6666
6667         case RECORD_TYPE:
6668           error ("%<struct %E%> declared in %<for%> loop initial declaration",
6669                  id);
6670           break;
6671         case UNION_TYPE:
6672           error ("%<union %E%> declared in %<for%> loop initial declaration",
6673                  id);
6674           break;
6675         case ENUMERAL_TYPE:
6676           error ("%<enum %E%> declared in %<for%> loop initial declaration",
6677                  id);
6678           break;
6679         default:
6680           error ("%Jdeclaration of non-variable %qD in %<for%> loop "
6681                  "initial declaration", decl, decl);
6682         }
6683     }
6684 }
6685 \f
6686 /* Save and reinitialize the variables
6687    used during compilation of a C function.  */
6688
6689 void
6690 c_push_function_context (struct function *f)
6691 {
6692   struct language_function *p;
6693   p = GGC_NEW (struct language_function);
6694   f->language = p;
6695
6696   p->base.x_stmt_tree = c_stmt_tree;
6697   p->x_break_label = c_break_label;
6698   p->x_cont_label = c_cont_label;
6699   p->x_switch_stack = c_switch_stack;
6700   p->arg_info = current_function_arg_info;
6701   p->returns_value = current_function_returns_value;
6702   p->returns_null = current_function_returns_null;
6703   p->returns_abnormally = current_function_returns_abnormally;
6704   p->warn_about_return_type = warn_about_return_type;
6705   p->extern_inline = current_extern_inline;
6706 }
6707
6708 /* Restore the variables used during compilation of a C function.  */
6709
6710 void
6711 c_pop_function_context (struct function *f)
6712 {
6713   struct language_function *p = f->language;
6714
6715   if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
6716       && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6717     {
6718       /* Stop pointing to the local nodes about to be freed.  */
6719       /* But DECL_INITIAL must remain nonzero so we know this
6720          was an actual function definition.  */
6721       DECL_INITIAL (current_function_decl) = error_mark_node;
6722       DECL_ARGUMENTS (current_function_decl) = 0;
6723     }
6724
6725   c_stmt_tree = p->base.x_stmt_tree;
6726   c_break_label = p->x_break_label;
6727   c_cont_label = p->x_cont_label;
6728   c_switch_stack = p->x_switch_stack;
6729   current_function_arg_info = p->arg_info;
6730   current_function_returns_value = p->returns_value;
6731   current_function_returns_null = p->returns_null;
6732   current_function_returns_abnormally = p->returns_abnormally;
6733   warn_about_return_type = p->warn_about_return_type;
6734   current_extern_inline = p->extern_inline;
6735
6736   f->language = NULL;
6737 }
6738
6739 /* Copy the DECL_LANG_SPECIFIC data associated with DECL.  */
6740
6741 void
6742 c_dup_lang_specific_decl (tree decl)
6743 {
6744   struct lang_decl *ld;
6745
6746   if (!DECL_LANG_SPECIFIC (decl))
6747     return;
6748
6749   ld = GGC_NEW (struct lang_decl);
6750   memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6751   DECL_LANG_SPECIFIC (decl) = ld;
6752 }
6753
6754 /* The functions below are required for functionality of doing
6755    function at once processing in the C front end. Currently these
6756    functions are not called from anywhere in the C front end, but as
6757    these changes continue, that will change.  */
6758
6759 /* Returns the stmt_tree (if any) to which statements are currently
6760    being added.  If there is no active statement-tree, NULL is
6761    returned.  */
6762
6763 stmt_tree
6764 current_stmt_tree (void)
6765 {
6766   return &c_stmt_tree;
6767 }
6768
6769 /* Nonzero if TYPE is an anonymous union or struct type.  Always 0 in
6770    C.  */
6771
6772 int
6773 anon_aggr_type_p (tree ARG_UNUSED (node))
6774 {
6775   return 0;
6776 }
6777
6778 /* Return the global value of T as a symbol.  */
6779
6780 tree
6781 identifier_global_value (tree t)
6782 {
6783   struct c_binding *b;
6784
6785   for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
6786     if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
6787       return b->decl;
6788
6789   return 0;
6790 }
6791
6792 /* Record a builtin type for C.  If NAME is non-NULL, it is the name used;
6793    otherwise the name is found in ridpointers from RID_INDEX.  */
6794
6795 void
6796 record_builtin_type (enum rid rid_index, const char *name, tree type)
6797 {
6798   tree id, decl;
6799   if (name == 0)
6800     id = ridpointers[(int) rid_index];
6801   else
6802     id = get_identifier (name);
6803   decl = build_decl (TYPE_DECL, id, type);
6804   pushdecl (decl);
6805   if (debug_hooks->type_decl)
6806     debug_hooks->type_decl (decl, false);
6807 }
6808
6809 /* Build the void_list_node (void_type_node having been created).  */
6810 tree
6811 build_void_list_node (void)
6812 {
6813   tree t = build_tree_list (NULL_TREE, void_type_node);
6814   return t;
6815 }
6816
6817 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR.  */
6818
6819 struct c_parm *
6820 build_c_parm (struct c_declspecs *specs, tree attrs,
6821               struct c_declarator *declarator)
6822 {
6823   struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
6824   ret->specs = specs;
6825   ret->attrs = attrs;
6826   ret->declarator = declarator;
6827   return ret;
6828 }
6829
6830 /* Return a declarator with nested attributes.  TARGET is the inner
6831    declarator to which these attributes apply.  ATTRS are the
6832    attributes.  */
6833
6834 struct c_declarator *
6835 build_attrs_declarator (tree attrs, struct c_declarator *target)
6836 {
6837   struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6838   ret->kind = cdk_attrs;
6839   ret->declarator = target;
6840   ret->u.attrs = attrs;
6841   return ret;
6842 }
6843
6844 /* Return a declarator for a function with arguments specified by ARGS
6845    and return type specified by TARGET.  */
6846
6847 struct c_declarator *
6848 build_function_declarator (struct c_arg_info *args,
6849                            struct c_declarator *target)
6850 {
6851   struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6852   ret->kind = cdk_function;
6853   ret->declarator = target;
6854   ret->u.arg_info = args;
6855   return ret;
6856 }
6857
6858 /* Return a declarator for the identifier IDENT (which may be
6859    NULL_TREE for an abstract declarator).  */
6860
6861 struct c_declarator *
6862 build_id_declarator (tree ident)
6863 {
6864   struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6865   ret->kind = cdk_id;
6866   ret->declarator = 0;
6867   ret->u.id = ident;
6868   /* Default value - may get reset to a more precise location. */
6869   ret->id_loc = input_location;
6870   return ret;
6871 }
6872
6873 /* Return something to represent absolute declarators containing a *.
6874    TARGET is the absolute declarator that the * contains.
6875    TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
6876    to apply to the pointer type.  */
6877
6878 struct c_declarator *
6879 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
6880                          struct c_declarator *target)
6881 {
6882   tree attrs;
6883   int quals = 0;
6884   struct c_declarator *itarget = target;
6885   struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6886   if (type_quals_attrs)
6887     {
6888       attrs = type_quals_attrs->attrs;
6889       quals = quals_from_declspecs (type_quals_attrs);
6890       if (attrs != NULL_TREE)
6891         itarget = build_attrs_declarator (attrs, target);
6892     }
6893   ret->kind = cdk_pointer;
6894   ret->declarator = itarget;
6895   ret->u.pointer_quals = quals;
6896   return ret;
6897 }
6898
6899 /* Return a pointer to a structure for an empty list of declaration
6900    specifiers.  */
6901
6902 struct c_declspecs *
6903 build_null_declspecs (void)
6904 {
6905   struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
6906   ret->type = 0;
6907   ret->decl_attr = 0;
6908   ret->attrs = 0;
6909   ret->typespec_word = cts_none;
6910   ret->storage_class = csc_none;
6911   ret->declspecs_seen_p = false;
6912   ret->type_seen_p = false;
6913   ret->non_sc_seen_p = false;
6914   ret->typedef_p = false;
6915   ret->tag_defined_p = false;
6916   ret->explicit_signed_p = false;
6917   ret->deprecated_p = false;
6918   ret->default_int_p = false;
6919   ret->long_p = false;
6920   ret->long_long_p = false;
6921   ret->short_p = false;
6922   ret->signed_p = false;
6923   ret->unsigned_p = false;
6924   ret->complex_p = false;
6925   ret->inline_p = false;
6926   ret->thread_p = false;
6927   ret->const_p = false;
6928   ret->volatile_p = false;
6929   ret->restrict_p = false;
6930   return ret;
6931 }
6932
6933 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
6934    returning SPECS.  */
6935
6936 struct c_declspecs *
6937 declspecs_add_qual (struct c_declspecs *specs, tree qual)
6938 {
6939   enum rid i;
6940   bool dupe = false;
6941   specs->non_sc_seen_p = true;
6942   specs->declspecs_seen_p = true;
6943   gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
6944               && C_IS_RESERVED_WORD (qual));
6945   i = C_RID_CODE (qual);
6946   switch (i)
6947     {
6948     case RID_CONST:
6949       dupe = specs->const_p;
6950       specs->const_p = true;
6951       break;
6952     case RID_VOLATILE:
6953       dupe = specs->volatile_p;
6954       specs->volatile_p = true;
6955       break;
6956     case RID_RESTRICT:
6957       dupe = specs->restrict_p;
6958       specs->restrict_p = true;
6959       break;
6960     default:
6961       gcc_unreachable ();
6962     }
6963   if (dupe && pedantic && !flag_isoc99)
6964     pedwarn ("duplicate %qE", qual);
6965   return specs;
6966 }
6967
6968 /* Add the type specifier TYPE to the declaration specifiers SPECS,
6969    returning SPECS.  */
6970
6971 struct c_declspecs *
6972 declspecs_add_type (struct c_declspecs *specs, struct c_typespec spec)
6973 {
6974   tree type = spec.spec;
6975   specs->non_sc_seen_p = true;
6976   specs->declspecs_seen_p = true;
6977   specs->type_seen_p = true;
6978   if (TREE_DEPRECATED (type))
6979     specs->deprecated_p = true;
6980
6981   /* Handle type specifier keywords.  */
6982   if (TREE_CODE (type) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (type))
6983     {
6984       enum rid i = C_RID_CODE (type);
6985       if (specs->type)
6986         {
6987           error ("two or more data types in declaration specifiers");
6988           return specs;
6989         }
6990       if ((int) i <= (int) RID_LAST_MODIFIER)
6991         {
6992           /* "long", "short", "signed", "unsigned" or "_Complex".  */
6993           bool dupe = false;
6994           switch (i)
6995             {
6996             case RID_LONG:
6997               if (specs->long_long_p)
6998                 {
6999                   error ("%<long long long%> is too long for GCC");
7000                   break;
7001                 }
7002               if (specs->long_p)
7003                 {
7004                   if (specs->typespec_word == cts_double)
7005                     {
7006                       error ("both %<long long%> and %<double%> in "
7007                              "declaration specifiers");
7008                       break;
7009                     }
7010                   if (pedantic && !flag_isoc99 && !in_system_header
7011                       && warn_long_long)
7012                     pedwarn ("ISO C90 does not support %<long long%>");
7013                   specs->long_long_p = 1;
7014                   break;
7015                 }
7016               if (specs->short_p)
7017                 error ("both %<long%> and %<short%> in "
7018                        "declaration specifiers");
7019               else if (specs->typespec_word == cts_void)
7020                 error ("both %<long%> and %<void%> in "
7021                        "declaration specifiers");
7022               else if (specs->typespec_word == cts_bool)
7023                 error ("both %<long%> and %<_Bool%> in "
7024                        "declaration specifiers");
7025               else if (specs->typespec_word == cts_char)
7026                 error ("both %<long%> and %<char%> in "
7027                        "declaration specifiers");
7028               else if (specs->typespec_word == cts_float)
7029                 error ("both %<long%> and %<float%> in "
7030                        "declaration specifiers");
7031               else
7032                 specs->long_p = true;
7033               break;
7034             case RID_SHORT:
7035               dupe = specs->short_p;
7036               if (specs->long_p)
7037                 error ("both %<long%> and %<short%> in "
7038                        "declaration specifiers");
7039               else if (specs->typespec_word == cts_void)
7040                 error ("both %<short%> and %<void%> in "
7041                        "declaration specifiers");
7042               else if (specs->typespec_word == cts_bool)
7043                 error ("both %<short%> and %<_Bool%> in "
7044                        "declaration specifiers");
7045               else if (specs->typespec_word == cts_char)
7046                 error ("both %<short%> and %<char%> in "
7047                        "declaration specifiers");
7048               else if (specs->typespec_word == cts_float)
7049                 error ("both %<short%> and %<float%> in "
7050                        "declaration specifiers");
7051               else if (specs->typespec_word == cts_double)
7052                 error ("both %<short%> and %<double%> in "
7053                        "declaration specifiers");
7054               else
7055                 specs->short_p = true;
7056               break;
7057             case RID_SIGNED:
7058               dupe = specs->signed_p;
7059               if (specs->unsigned_p)
7060                 error ("both %<signed%> and %<unsigned%> in "
7061                        "declaration specifiers");
7062               else if (specs->typespec_word == cts_void)
7063                 error ("both %<signed%> and %<void%> in "
7064                        "declaration specifiers");
7065               else if (specs->typespec_word == cts_bool)
7066                 error ("both %<signed%> and %<_Bool%> in "
7067                        "declaration specifiers");
7068               else if (specs->typespec_word == cts_float)
7069                 error ("both %<signed%> and %<float%> in "
7070                        "declaration specifiers");
7071               else if (specs->typespec_word == cts_double)
7072                 error ("both %<signed%> and %<double%> in "
7073                        "declaration specifiers");
7074               else
7075                 specs->signed_p = true;
7076               break;
7077             case RID_UNSIGNED:
7078               dupe = specs->unsigned_p;
7079               if (specs->signed_p)
7080                 error ("both %<signed%> and %<unsigned%> in "
7081                        "declaration specifiers");
7082               else if (specs->typespec_word == cts_void)
7083                 error ("both %<unsigned%> and %<void%> in "
7084                        "declaration specifiers");
7085               else if (specs->typespec_word == cts_bool)
7086                 error ("both %<unsigned%> and %<_Bool%> in "
7087                        "declaration specifiers");
7088               else if (specs->typespec_word == cts_float)
7089                 error ("both %<unsigned%> and %<float%> in "
7090                        "declaration specifiers");
7091               else if (specs->typespec_word == cts_double)
7092                 error ("both %<unsigned%> and %<double%> in "
7093                        "declaration specifiers");
7094               else
7095                 specs->unsigned_p = true;
7096               break;
7097             case RID_COMPLEX:
7098               dupe = specs->complex_p;
7099               if (pedantic && !flag_isoc99 && !in_system_header)
7100                 pedwarn ("ISO C90 does not support complex types");
7101               if (specs->typespec_word == cts_void)
7102                 error ("both %<complex%> and %<void%> in "
7103                        "declaration specifiers");
7104               else if (specs->typespec_word == cts_bool)
7105                 error ("both %<complex%> and %<_Bool%> in "
7106                        "declaration specifiers");
7107               else
7108                 specs->complex_p = true;
7109               break;
7110             default:
7111               gcc_unreachable ();
7112             }
7113
7114           if (dupe)
7115             error ("duplicate %qE", type);
7116
7117           return specs;
7118         }
7119       else
7120         {
7121           /* "void", "_Bool", "char", "int", "float" or "double".  */
7122           if (specs->typespec_word != cts_none)
7123             {
7124               error ("two or more data types in declaration specifiers");
7125               return specs;
7126             }
7127           switch (i)
7128             {
7129             case RID_VOID:
7130               if (specs->long_p)
7131                 error ("both %<long%> and %<void%> in "
7132                        "declaration specifiers");
7133               else if (specs->short_p)
7134                 error ("both %<short%> and %<void%> in "
7135                        "declaration specifiers");
7136               else if (specs->signed_p)
7137                 error ("both %<signed%> and %<void%> in "
7138                        "declaration specifiers");
7139               else if (specs->unsigned_p)
7140                 error ("both %<unsigned%> and %<void%> in "
7141                        "declaration specifiers");
7142               else if (specs->complex_p)
7143                 error ("both %<complex%> and %<void%> in "
7144                        "declaration specifiers");
7145               else
7146                 specs->typespec_word = cts_void;
7147               return specs;
7148             case RID_BOOL:
7149               if (specs->long_p)
7150                 error ("both %<long%> and %<_Bool%> in "
7151                        "declaration specifiers");
7152               else if (specs->short_p)
7153                 error ("both %<short%> and %<_Bool%> in "
7154                        "declaration specifiers");
7155               else if (specs->signed_p)
7156                 error ("both %<signed%> and %<_Bool%> in "
7157                        "declaration specifiers");
7158               else if (specs->unsigned_p)
7159                 error ("both %<unsigned%> and %<_Bool%> in "
7160                        "declaration specifiers");
7161               else if (specs->complex_p)
7162                 error ("both %<complex%> and %<_Bool%> in "
7163                        "declaration specifiers");
7164               else
7165                 specs->typespec_word = cts_bool;
7166               return specs;
7167             case RID_CHAR:
7168               if (specs->long_p)
7169                 error ("both %<long%> and %<char%> in "
7170                        "declaration specifiers");
7171               else if (specs->short_p)
7172                 error ("both %<short%> and %<char%> in "
7173                        "declaration specifiers");
7174               else
7175                 specs->typespec_word = cts_char;
7176               return specs;
7177             case RID_INT:
7178               specs->typespec_word = cts_int;
7179               return specs;
7180             case RID_FLOAT:
7181               if (specs->long_p)
7182                 error ("both %<long%> and %<float%> in "
7183                        "declaration specifiers");
7184               else if (specs->short_p)
7185                 error ("both %<short%> and %<float%> in "
7186                        "declaration specifiers");
7187               else if (specs->signed_p)
7188                 error ("both %<signed%> and %<float%> in "
7189                        "declaration specifiers");
7190               else if (specs->unsigned_p)
7191                 error ("both %<unsigned%> and %<float%> in "
7192                        "declaration specifiers");
7193               else
7194                 specs->typespec_word = cts_float;
7195               return specs;
7196             case RID_DOUBLE:
7197               if (specs->long_long_p)
7198                 error ("both %<long long%> and %<double%> in "
7199                        "declaration specifiers");
7200               else if (specs->short_p)
7201                 error ("both %<short%> and %<double%> in "
7202                        "declaration specifiers");
7203               else if (specs->signed_p)
7204                 error ("both %<signed%> and %<double%> in "
7205                        "declaration specifiers");
7206               else if (specs->unsigned_p)
7207                 error ("both %<unsigned%> and %<double%> in "
7208                        "declaration specifiers");
7209               else
7210                 specs->typespec_word = cts_double;
7211               return specs;
7212             default:
7213               /* ObjC reserved word "id", handled below.  */
7214               break;
7215             }
7216         }
7217     }
7218
7219   /* Now we have a typedef (a TYPE_DECL node), an identifier (some
7220      form of ObjC type, cases such as "int" and "long" being handled
7221      above), a TYPE (struct, union, enum and typeof specifiers) or an
7222      ERROR_MARK.  In none of these cases may there have previously
7223      been any type specifiers.  */
7224   if (specs->type || specs->typespec_word != cts_none
7225       || specs->long_p || specs->short_p || specs->signed_p
7226       || specs->unsigned_p || specs->complex_p)
7227     error ("two or more data types in declaration specifiers");
7228   else if (TREE_CODE (type) == TYPE_DECL)
7229     {
7230       if (TREE_TYPE (type) == error_mark_node)
7231         ; /* Allow the type to default to int to avoid cascading errors.  */
7232       else
7233         {
7234           specs->type = TREE_TYPE (type);
7235           specs->decl_attr = DECL_ATTRIBUTES (type);
7236           specs->typedef_p = true;
7237           specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
7238         }
7239     }
7240   else if (TREE_CODE (type) == IDENTIFIER_NODE)
7241     {
7242       tree t = lookup_name (type);
7243       if (!t || TREE_CODE (t) != TYPE_DECL)
7244         error ("%qE fails to be a typedef or built in type", type);
7245       else if (TREE_TYPE (t) == error_mark_node)
7246         ;
7247       else
7248         specs->type = TREE_TYPE (t);
7249     }
7250   else if (TREE_CODE (type) != ERROR_MARK)
7251     {
7252       if (spec.kind == ctsk_tagdef || spec.kind == ctsk_tagfirstref)
7253         specs->tag_defined_p = true;
7254       if (spec.kind == ctsk_typeof)
7255         specs->typedef_p = true;
7256       specs->type = type;
7257     }
7258
7259   return specs;
7260 }
7261
7262 /* Add the storage class specifier or function specifier SCSPEC to the
7263    declaration specifiers SPECS, returning SPECS.  */
7264
7265 struct c_declspecs *
7266 declspecs_add_scspec (struct c_declspecs *specs, tree scspec)
7267 {
7268   enum rid i;
7269   enum c_storage_class n = csc_none;
7270   bool dupe = false;
7271   specs->declspecs_seen_p = true;
7272   gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
7273               && C_IS_RESERVED_WORD (scspec));
7274   i = C_RID_CODE (scspec);
7275   if (extra_warnings && specs->non_sc_seen_p)
7276     warning (OPT_Wextra, "%qE is not at beginning of declaration", scspec);
7277   switch (i)
7278     {
7279     case RID_INLINE:
7280       /* C99 permits duplicate inline.  Although of doubtful utility,
7281          it seems simplest to permit it in gnu89 mode as well, as
7282          there is also little utility in maintaining this as a
7283          difference between gnu89 and C99 inline.  */
7284       dupe = false;
7285       specs->inline_p = true;
7286       break;
7287     case RID_THREAD:
7288       dupe = specs->thread_p;
7289       if (specs->storage_class == csc_auto)
7290         error ("%<__thread%> used with %<auto%>");
7291       else if (specs->storage_class == csc_register)
7292         error ("%<__thread%> used with %<register%>");
7293       else if (specs->storage_class == csc_typedef)
7294         error ("%<__thread%> used with %<typedef%>");
7295       else
7296         specs->thread_p = true;
7297       break;
7298     case RID_AUTO:
7299       n = csc_auto;
7300       break;
7301     case RID_EXTERN:
7302       n = csc_extern;
7303       /* Diagnose "__thread extern".  */
7304       if (specs->thread_p)
7305         error ("%<__thread%> before %<extern%>");
7306       break;
7307     case RID_REGISTER:
7308       n = csc_register;
7309       break;
7310     case RID_STATIC:
7311       n = csc_static;
7312       /* Diagnose "__thread static".  */
7313       if (specs->thread_p)
7314         error ("%<__thread%> before %<static%>");
7315       break;
7316     case RID_TYPEDEF:
7317       n = csc_typedef;
7318       break;
7319     default:
7320       gcc_unreachable ();
7321     }
7322   if (n != csc_none && n == specs->storage_class)
7323     dupe = true;
7324   if (dupe)
7325     error ("duplicate %qE", scspec);
7326   if (n != csc_none)
7327     {
7328       if (specs->storage_class != csc_none && n != specs->storage_class)
7329         {
7330           error ("multiple storage classes in declaration specifiers");
7331         }
7332       else
7333         {
7334           specs->storage_class = n;
7335           if (n != csc_extern && n != csc_static && specs->thread_p)
7336             {
7337               error ("%<__thread%> used with %qE", scspec);
7338               specs->thread_p = false;
7339             }
7340         }
7341     }
7342   return specs;
7343 }
7344
7345 /* Add the attributes ATTRS to the declaration specifiers SPECS,
7346    returning SPECS.  */
7347
7348 struct c_declspecs *
7349 declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
7350 {
7351   specs->attrs = chainon (attrs, specs->attrs);
7352   specs->declspecs_seen_p = true;
7353   return specs;
7354 }
7355
7356 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
7357    specifiers with any other type specifier to determine the resulting
7358    type.  This is where ISO C checks on complex types are made, since
7359    "_Complex long" is a prefix of the valid ISO C type "_Complex long
7360    double".  */
7361
7362 struct c_declspecs *
7363 finish_declspecs (struct c_declspecs *specs)
7364 {
7365   /* If a type was specified as a whole, we have no modifiers and are
7366      done.  */
7367   if (specs->type != NULL_TREE)
7368     {
7369       gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
7370                   && !specs->signed_p && !specs->unsigned_p
7371                   && !specs->complex_p);
7372       return specs;
7373     }
7374
7375   /* If none of "void", "_Bool", "char", "int", "float" or "double"
7376      has been specified, treat it as "int" unless "_Complex" is
7377      present and there are no other specifiers.  If we just have
7378      "_Complex", it is equivalent to "_Complex double", but e.g.
7379      "_Complex short" is equivalent to "_Complex short int".  */
7380   if (specs->typespec_word == cts_none)
7381     {
7382       if (specs->long_p || specs->short_p
7383           || specs->signed_p || specs->unsigned_p)
7384         {
7385           specs->typespec_word = cts_int;
7386         }
7387       else if (specs->complex_p)
7388         {
7389           specs->typespec_word = cts_double;
7390           if (pedantic)
7391             pedwarn ("ISO C does not support plain %<complex%> meaning "
7392                      "%<double complex%>");
7393         }
7394       else
7395         {
7396           specs->typespec_word = cts_int;
7397           specs->default_int_p = true;
7398           /* We don't diagnose this here because grokdeclarator will
7399              give more specific diagnostics according to whether it is
7400              a function definition.  */
7401         }
7402     }
7403
7404   /* If "signed" was specified, record this to distinguish "int" and
7405      "signed int" in the case of a bit-field with
7406      -funsigned-bitfields.  */
7407   specs->explicit_signed_p = specs->signed_p;
7408
7409   /* Now compute the actual type.  */
7410   switch (specs->typespec_word)
7411     {
7412     case cts_void:
7413       gcc_assert (!specs->long_p && !specs->short_p
7414                   && !specs->signed_p && !specs->unsigned_p
7415                   && !specs->complex_p);
7416       specs->type = void_type_node;
7417       break;
7418     case cts_bool:
7419       gcc_assert (!specs->long_p && !specs->short_p
7420                   && !specs->signed_p && !specs->unsigned_p
7421                   && !specs->complex_p);
7422       specs->type = boolean_type_node;
7423       break;
7424     case cts_char:
7425       gcc_assert (!specs->long_p && !specs->short_p);
7426       gcc_assert (!(specs->signed_p && specs->unsigned_p));
7427       if (specs->signed_p)
7428         specs->type = signed_char_type_node;
7429       else if (specs->unsigned_p)
7430         specs->type = unsigned_char_type_node;
7431       else
7432         specs->type = char_type_node;
7433       if (specs->complex_p)
7434         {
7435           if (pedantic)
7436             pedwarn ("ISO C does not support complex integer types");
7437           specs->type = build_complex_type (specs->type);
7438         }
7439       break;
7440     case cts_int:
7441       gcc_assert (!(specs->long_p && specs->short_p));
7442       gcc_assert (!(specs->signed_p && specs->unsigned_p));
7443       if (specs->long_long_p)
7444         specs->type = (specs->unsigned_p
7445                        ? long_long_unsigned_type_node
7446                        : long_long_integer_type_node);
7447       else if (specs->long_p)
7448         specs->type = (specs->unsigned_p
7449                        ? long_unsigned_type_node
7450                        : long_integer_type_node);
7451       else if (specs->short_p)
7452         specs->type = (specs->unsigned_p
7453                        ? short_unsigned_type_node
7454                        : short_integer_type_node);
7455       else
7456         specs->type = (specs->unsigned_p
7457                        ? unsigned_type_node
7458                        : integer_type_node);
7459       if (specs->complex_p)
7460         {
7461           if (pedantic)
7462             pedwarn ("ISO C does not support complex integer types");
7463           specs->type = build_complex_type (specs->type);
7464         }
7465       break;
7466     case cts_float:
7467       gcc_assert (!specs->long_p && !specs->short_p
7468                   && !specs->signed_p && !specs->unsigned_p);
7469       specs->type = (specs->complex_p
7470                      ? complex_float_type_node
7471                      : float_type_node);
7472       break;
7473     case cts_double:
7474       gcc_assert (!specs->long_long_p && !specs->short_p
7475                   && !specs->signed_p && !specs->unsigned_p);
7476       if (specs->long_p)
7477         {
7478           specs->type = (specs->complex_p
7479                          ? complex_long_double_type_node
7480                          : long_double_type_node);
7481         }
7482       else
7483         {
7484           specs->type = (specs->complex_p
7485                          ? complex_double_type_node
7486                          : double_type_node);
7487         }
7488       break;
7489     default:
7490       gcc_unreachable ();
7491     }
7492
7493   return specs;
7494 }
7495
7496 /* Synthesize a function which calls all the global ctors or global
7497    dtors in this file.  This is only used for targets which do not
7498    support .ctors/.dtors sections.  FIXME: Migrate into cgraph.  */
7499 static void
7500 build_cdtor (int method_type, tree cdtors)
7501 {
7502   tree body = 0;
7503
7504   if (!cdtors)
7505     return;
7506
7507   for (; cdtors; cdtors = TREE_CHAIN (cdtors))
7508     append_to_statement_list (build_function_call (TREE_VALUE (cdtors), 0),
7509                               &body);
7510
7511   cgraph_build_static_cdtor (method_type, body, DEFAULT_INIT_PRIORITY);
7512 }
7513
7514 /* Perform final processing on one file scope's declarations (or the
7515    external scope's declarations), GLOBALS.  */
7516 static void
7517 c_write_global_declarations_1 (tree globals)
7518 {
7519   size_t len = list_length (globals);
7520   tree *vec = XNEWVEC (tree, len);
7521   size_t i;
7522   tree decl;
7523
7524   /* Process the decls in the order they were written.  */
7525   for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
7526     {
7527       vec[i] = decl;
7528       /* Check for used but undefined static functions using the C
7529          standard's definition of "used", and set TREE_NO_WARNING so
7530          that check_global_declarations doesn't repeat the check.  */
7531       if (TREE_CODE (decl) == FUNCTION_DECL
7532           && DECL_INITIAL (decl) == 0
7533           && DECL_EXTERNAL (decl)
7534           && !TREE_PUBLIC (decl)
7535           && C_DECL_USED (decl))
7536         {
7537           pedwarn ("%J%qF used but never defined", decl, decl);
7538           TREE_NO_WARNING (decl) = 1;
7539         }
7540     }
7541
7542   wrapup_global_declarations (vec, len);
7543   check_global_declarations (vec, len);
7544
7545   free (vec);
7546 }
7547
7548 void
7549 c_write_global_declarations (void)
7550 {
7551   tree ext_block, t;
7552
7553   /* We don't want to do this if generating a PCH.  */
7554   if (pch_file)
7555     return;
7556
7557   /* Don't waste time on further processing if -fsyntax-only or we've
7558      encountered errors.  */
7559   if (flag_syntax_only || errorcount || sorrycount || cpp_errors (parse_in))
7560     return;
7561
7562   /* Close the external scope.  */
7563   ext_block = pop_scope ();
7564   external_scope = 0;
7565   gcc_assert (!current_scope);
7566
7567   /* Process all file scopes in this compilation, and the external_scope,
7568      through wrapup_global_declarations and check_global_declarations.  */
7569   for (t = all_translation_units; t; t = TREE_CHAIN (t))
7570     c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
7571   c_write_global_declarations_1 (BLOCK_VARS (ext_block));
7572
7573   /* Generate functions to call static constructors and destructors
7574      for targets that do not support .ctors/.dtors sections.  These
7575      functions have magic names which are detected by collect2.  */
7576   build_cdtor ('I', static_ctors); static_ctors = 0;
7577   build_cdtor ('D', static_dtors); static_dtors = 0;
7578
7579   /* We're done parsing; proceed to optimize and emit assembly.
7580      FIXME: shouldn't be the front end's responsibility to call this.  */
7581   cgraph_optimize ();
7582 }
7583
7584 #include "gt-c-decl.h"