OSDN Git Service

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