OSDN Git Service

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