OSDN Git Service

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