OSDN Git Service

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