OSDN Git Service

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