OSDN Git Service

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