OSDN Git Service

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