OSDN Git Service

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