OSDN Git Service

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