OSDN Git Service

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