OSDN Git Service

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