OSDN Git Service

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