OSDN Git Service

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