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