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