OSDN Git Service

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