OSDN Git Service

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