OSDN Git Service

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