OSDN Git Service

18ad11901f97962da9b9b42e50121cd51ed607ca
[pf3gnuchains/gcc-fork.git] / gcc / c-decl.c
1 /* Process declarations and variables for C compiler.
2    Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3    2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
4    Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
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 "input.h"
33 #include "tm.h"
34 #include "intl.h"
35 #include "tree.h"
36 #include "tree-inline.h"
37 #include "rtl.h"
38 #include "flags.h"
39 #include "function.h"
40 #include "output.h"
41 #include "expr.h"
42 #include "c-tree.h"
43 #include "toplev.h"
44 #include "ggc.h"
45 #include "tm_p.h"
46 #include "cpplib.h"
47 #include "target.h"
48 #include "debug.h"
49 #include "opts.h"
50 #include "timevar.h"
51 #include "c-common.h"
52 #include "c-pragma.h"
53 #include "langhooks.h"
54 #include "tree-mudflap.h"
55 #include "tree-gimple.h"
56 #include "diagnostic.h"
57 #include "tree-dump.h"
58 #include "cgraph.h"
59 #include "hashtab.h"
60 #include "libfuncs.h"
61 #include "except.h"
62 #include "langhooks-def.h"
63 #include "pointer-set.h"
64
65 /* In grokdeclarator, distinguish syntactic contexts of declarators.  */
66 enum decl_context
67 { NORMAL,                       /* Ordinary declaration */
68   FUNCDEF,                      /* Function definition */
69   PARM,                         /* Declaration of parm before function body */
70   FIELD,                        /* Declaration inside struct or union */
71   TYPENAME};                    /* Typename (inside cast or sizeof)  */
72
73 /* States indicating how grokdeclarator() should handle declspecs marked
74    with __attribute__((deprecated)).  An object declared as
75    __attribute__((deprecated)) suppresses warnings of uses of other
76    deprecated items.  */
77
78 enum deprecated_states {
79   DEPRECATED_NORMAL,
80   DEPRECATED_SUPPRESS
81 };
82
83 \f
84 /* Nonzero if we have seen an invalid cross reference
85    to a struct, union, or enum, but not yet printed the message.  */
86 tree pending_invalid_xref;
87
88 /* File and line to appear in the eventual error message.  */
89 location_t pending_invalid_xref_location;
90
91 /* True means we've initialized exception handling.  */
92 bool c_eh_initialized_p;
93
94 /* The file and line that the prototype came from if this is an
95    old-style definition; used for diagnostics in
96    store_parm_decls_oldstyle.  */
97
98 static location_t current_function_prototype_locus;
99
100 /* Whether this prototype was built-in.  */
101
102 static bool current_function_prototype_built_in;
103
104 /* The argument type information of this prototype.  */
105
106 static tree current_function_prototype_arg_types;
107
108 /* The argument information structure for the function currently being
109    defined.  */
110
111 static struct c_arg_info *current_function_arg_info;
112
113 /* The obstack on which parser and related data structures, which are
114    not live beyond their top-level declaration or definition, are
115    allocated.  */
116 struct obstack parser_obstack;
117
118 /* The current statement tree.  */
119
120 static GTY(()) struct stmt_tree_s c_stmt_tree;
121
122 /* State saving variables.  */
123 tree c_break_label;
124 tree c_cont_label;
125
126 /* Linked list of TRANSLATION_UNIT_DECLS for the translation units
127    included in this invocation.  Note that the current translation
128    unit is not included in this list.  */
129
130 static GTY(()) tree all_translation_units;
131
132 /* A list of decls to be made automatically visible in each file scope.  */
133 static GTY(()) tree visible_builtins;
134
135 /* Set to 0 at beginning of a function definition, set to 1 if
136    a return statement that specifies a return value is seen.  */
137
138 int current_function_returns_value;
139
140 /* Set to 0 at beginning of a function definition, set to 1 if
141    a return statement with no argument is seen.  */
142
143 int current_function_returns_null;
144
145 /* Set to 0 at beginning of a function definition, set to 1 if
146    a call to a noreturn function is seen.  */
147
148 int current_function_returns_abnormally;
149
150 /* Set to nonzero by `grokdeclarator' for a function
151    whose return type is defaulted, if warnings for this are desired.  */
152
153 static int warn_about_return_type;
154
155 /* Nonzero when the current toplevel function contains a declaration
156    of a nested function which is never defined.  */
157
158 static bool undef_nested_function;
159
160 /* True means global_bindings_p should return false even if the scope stack
161    says we are in file scope.  */
162 bool c_override_global_bindings_to_false;
163
164 \f
165 /* Each c_binding structure describes one binding of an identifier to
166    a decl.  All the decls in a scope - irrespective of namespace - are
167    chained together by the ->prev field, which (as the name implies)
168    runs in reverse order.  All the decls in a given namespace bound to
169    a given identifier are chained by the ->shadowed field, which runs
170    from inner to outer scopes.
171
172    The ->decl field usually points to a DECL node, but there are two
173    exceptions.  In the namespace of type tags, the bound entity is a
174    RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node.  If an undeclared
175    identifier is encountered, it is bound to error_mark_node to
176    suppress further errors about that identifier in the current
177    function.
178
179    The ->type field stores the type of the declaration in this scope;
180    if NULL, the type is the type of the ->decl field.  This is only of
181    relevance for objects with external or internal linkage which may
182    be redeclared in inner scopes, forming composite types that only
183    persist for the duration of those scopes.  In the external scope,
184    this stores the composite of all the types declared for this
185    object, visible or not.  The ->inner_comp field (used only at file
186    scope) stores whether an incomplete array type at file scope was
187    completed at an inner scope to an array size other than 1.
188
189    The depth field is copied from the scope structure that holds this
190    decl.  It is used to preserve the proper ordering of the ->shadowed
191    field (see bind()) and also for a handful of special-case checks.
192    Finally, the invisible bit is true for a decl which should be
193    ignored for purposes of normal name lookup, and the nested bit is
194    true for a decl that's been bound a second time in an inner scope;
195    in all such cases, the binding in the outer scope will have its
196    invisible bit true.  */
197
198 struct c_binding GTY((chain_next ("%h.prev")))
199 {
200   tree decl;                    /* the decl bound */
201   tree type;                    /* the type in this scope */
202   tree id;                      /* the identifier it's bound to */
203   struct c_binding *prev;       /* the previous decl in this scope */
204   struct c_binding *shadowed;   /* the innermost decl shadowed by this one */
205   unsigned int depth : 28;      /* depth of this scope */
206   BOOL_BITFIELD invisible : 1;  /* normal lookup should ignore this binding */
207   BOOL_BITFIELD nested : 1;     /* do not set DECL_CONTEXT when popping */
208   BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
209   /* one free bit */
210 };
211 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
212 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
213 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
214 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
215
216 #define I_SYMBOL_BINDING(node) \
217   (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->symbol_binding)
218 #define I_SYMBOL_DECL(node) \
219  (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
220
221 #define I_TAG_BINDING(node) \
222   (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->tag_binding)
223 #define I_TAG_DECL(node) \
224  (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
225
226 #define I_LABEL_BINDING(node) \
227   (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->label_binding)
228 #define I_LABEL_DECL(node) \
229  (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
230
231 /* Each C symbol points to three linked lists of c_binding structures.
232    These describe the values of the identifier in the three different
233    namespaces defined by the language.  */
234
235 struct lang_identifier GTY(())
236 {
237   struct c_common_identifier common_id;
238   struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
239   struct c_binding *tag_binding;    /* struct/union/enum tags */
240   struct c_binding *label_binding;  /* labels */
241 };
242
243 /* Validate c-lang.c's assumptions.  */
244 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
245 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
246
247 /* The resulting tree type.  */
248
249 union lang_tree_node
250   GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
251        chain_next ("TREE_CODE (&%h.generic) == INTEGER_TYPE ? (union lang_tree_node *) TYPE_NEXT_VARIANT (&%h.generic) : ((union lang_tree_node *) GENERIC_NEXT (&%h.generic))")))
252 {
253   union tree_node GTY ((tag ("0"),
254                         desc ("tree_node_structure (&%h)")))
255     generic;
256   struct lang_identifier GTY ((tag ("1"))) identifier;
257 };
258
259 /* Each c_scope structure describes the complete contents of one
260    scope.  Four scopes are distinguished specially: the innermost or
261    current scope, the innermost function scope, the file scope (always
262    the second to outermost) and the outermost or external scope.
263
264    Most declarations are recorded in the current scope.
265
266    All normal label declarations are recorded in the innermost
267    function scope, as are bindings of undeclared identifiers to
268    error_mark_node.  (GCC permits nested functions as an extension,
269    hence the 'innermost' qualifier.)  Explicitly declared labels
270    (using the __label__ extension) appear in the current scope.
271
272    Being in the file scope (current_scope == file_scope) causes
273    special behavior in several places below.  Also, under some
274    conditions the Objective-C front end records declarations in the
275    file scope even though that isn't the current scope.
276
277    All declarations with external linkage are recorded in the external
278    scope, even if they aren't visible there; this models the fact that
279    such declarations are visible to the entire program, and (with a
280    bit of cleverness, see pushdecl) allows diagnosis of some violations
281    of C99 6.2.2p7 and 6.2.7p2:
282
283      If, within the same translation unit, the same identifier appears
284      with both internal and external linkage, the behavior is
285      undefined.
286
287      All declarations that refer to the same object or function shall
288      have compatible type; otherwise, the behavior is undefined.
289
290    Initially only the built-in declarations, which describe compiler
291    intrinsic functions plus a subset of the standard library, are in
292    this scope.
293
294    The order of the blocks list matters, and it is frequently appended
295    to.  To avoid having to walk all the way to the end of the list on
296    each insertion, or reverse the list later, we maintain a pointer to
297    the last list entry.  (FIXME: It should be feasible to use a reversed
298    list here.)
299
300    The bindings list is strictly in reverse order of declarations;
301    pop_scope relies on this.  */
302
303
304 struct c_scope GTY((chain_next ("%h.outer")))
305 {
306   /* The scope containing this one.  */
307   struct c_scope *outer;
308
309   /* The next outermost function scope.  */
310   struct c_scope *outer_function;
311
312   /* All bindings in this scope.  */
313   struct c_binding *bindings;
314
315   /* For each scope (except the global one), a chain of BLOCK nodes
316      for all the scopes that were entered and exited one level down.  */
317   tree blocks;
318   tree blocks_last;
319
320   /* The depth of this scope.  Used to keep the ->shadowed chain of
321      bindings sorted innermost to outermost.  */
322   unsigned int depth : 28;
323
324   /* True if we are currently filling this scope with parameter
325      declarations.  */
326   BOOL_BITFIELD parm_flag : 1;
327
328   /* True if we saw [*] in this scope.  Used to give an error messages
329      if these appears in a function definition.  */
330   BOOL_BITFIELD had_vla_unspec : 1;
331
332   /* True if we already complained about forward parameter decls
333      in this scope.  This prevents double warnings on
334      foo (int a; int b; ...)  */
335   BOOL_BITFIELD warned_forward_parm_decls : 1;
336
337   /* True if this is the outermost block scope of a function body.
338      This scope contains the parameters, the local variables declared
339      in the outermost block, and all the labels (except those in
340      nested functions, or declared at block scope with __label__).  */
341   BOOL_BITFIELD function_body : 1;
342
343   /* True means make a BLOCK for this scope no matter what.  */
344   BOOL_BITFIELD keep : 1;
345 };
346
347 /* The scope currently in effect.  */
348
349 static GTY(()) struct c_scope *current_scope;
350
351 /* The innermost function scope.  Ordinary (not explicitly declared)
352    labels, bindings to error_mark_node, and the lazily-created
353    bindings of __func__ and its friends get this scope.  */
354
355 static GTY(()) struct c_scope *current_function_scope;
356
357 /* The C file scope.  This is reset for each input translation unit.  */
358
359 static GTY(()) struct c_scope *file_scope;
360
361 /* The outermost scope.  This is used for all declarations with
362    external linkage, and only these, hence the name.  */
363
364 static GTY(()) struct c_scope *external_scope;
365
366 /* A chain of c_scope structures awaiting reuse.  */
367
368 static GTY((deletable)) struct c_scope *scope_freelist;
369
370 /* A chain of c_binding structures awaiting reuse.  */
371
372 static GTY((deletable)) struct c_binding *binding_freelist;
373
374 /* Append VAR to LIST in scope SCOPE.  */
375 #define SCOPE_LIST_APPEND(scope, list, decl) do {       \
376   struct c_scope *s_ = (scope);                         \
377   tree d_ = (decl);                                     \
378   if (s_->list##_last)                                  \
379     BLOCK_CHAIN (s_->list##_last) = d_;                 \
380   else                                                  \
381     s_->list = d_;                                      \
382   s_->list##_last = d_;                                 \
383 } while (0)
384
385 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE.  */
386 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do {        \
387   struct c_scope *t_ = (tscope);                                \
388   struct c_scope *f_ = (fscope);                                \
389   if (t_->to##_last)                                            \
390     BLOCK_CHAIN (t_->to##_last) = f_->from;                     \
391   else                                                          \
392     t_->to = f_->from;                                          \
393   t_->to##_last = f_->from##_last;                              \
394 } while (0)
395
396 /* True means unconditionally make a BLOCK for the next scope pushed.  */
397
398 static bool keep_next_level_flag;
399
400 /* True means the next call to push_scope will be the outermost scope
401    of a function body, so do not push a new scope, merely cease
402    expecting parameter decls.  */
403
404 static bool next_is_function_body;
405
406 /* Forward declarations.  */
407 static tree lookup_name_in_scope (tree, struct c_scope *);
408 static tree c_make_fname_decl (tree, int);
409 static tree grokdeclarator (const struct c_declarator *,
410                             struct c_declspecs *,
411                             enum decl_context, bool, tree *, tree *,
412                             enum deprecated_states);
413 static tree grokparms (struct c_arg_info *, bool);
414 static void layout_array_type (tree);
415 \f
416 /* T is a statement.  Add it to the statement-tree.  This is the
417    C/ObjC version--C++ has a slightly different version of this
418    function.  */
419
420 tree
421 add_stmt (tree t)
422 {
423   enum tree_code code = TREE_CODE (t);
424
425   if (CAN_HAVE_LOCATION_P (t) && code != LABEL_EXPR)
426     {
427       if (!EXPR_HAS_LOCATION (t))
428         SET_EXPR_LOCATION (t, input_location);
429     }
430
431   if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
432     STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
433
434   /* Add T to the statement-tree.  Non-side-effect statements need to be
435      recorded during statement expressions.  */
436   append_to_statement_list_force (t, &cur_stmt_list);
437
438   return t;
439 }
440 \f
441
442 void
443 c_print_identifier (FILE *file, tree node, int indent)
444 {
445   print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
446   print_node (file, "tag", I_TAG_DECL (node), indent + 4);
447   print_node (file, "label", I_LABEL_DECL (node), indent + 4);
448   if (C_IS_RESERVED_WORD (node))
449     {
450       tree rid = ridpointers[C_RID_CODE (node)];
451       indent_to (file, indent + 4);
452       fprintf (file, "rid %p \"%s\"",
453                (void *) rid, IDENTIFIER_POINTER (rid));
454     }
455 }
456
457 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
458    which may be any of several kinds of DECL or TYPE or error_mark_node,
459    in the scope SCOPE.  */
460 static void
461 bind (tree name, tree decl, struct c_scope *scope, bool invisible, bool nested)
462 {
463   struct c_binding *b, **here;
464
465   if (binding_freelist)
466     {
467       b = binding_freelist;
468       binding_freelist = b->prev;
469     }
470   else
471     b = GGC_NEW (struct c_binding);
472
473   b->shadowed = 0;
474   b->decl = decl;
475   b->id = name;
476   b->depth = scope->depth;
477   b->invisible = invisible;
478   b->nested = nested;
479   b->inner_comp = 0;
480
481   b->type = 0;
482
483   b->prev = scope->bindings;
484   scope->bindings = b;
485
486   if (!name)
487     return;
488
489   switch (TREE_CODE (decl))
490     {
491     case LABEL_DECL:     here = &I_LABEL_BINDING (name);   break;
492     case ENUMERAL_TYPE:
493     case UNION_TYPE:
494     case RECORD_TYPE:    here = &I_TAG_BINDING (name);     break;
495     case VAR_DECL:
496     case FUNCTION_DECL:
497     case TYPE_DECL:
498     case CONST_DECL:
499     case PARM_DECL:
500     case ERROR_MARK:     here = &I_SYMBOL_BINDING (name);  break;
501
502     default:
503       gcc_unreachable ();
504     }
505
506   /* Locate the appropriate place in the chain of shadowed decls
507      to insert this binding.  Normally, scope == current_scope and
508      this does nothing.  */
509   while (*here && (*here)->depth > scope->depth)
510     here = &(*here)->shadowed;
511
512   b->shadowed = *here;
513   *here = b;
514 }
515
516 /* Clear the binding structure B, stick it on the binding_freelist,
517    and return the former value of b->prev.  This is used by pop_scope
518    and get_parm_info to iterate destructively over all the bindings
519    from a given scope.  */
520 static struct c_binding *
521 free_binding_and_advance (struct c_binding *b)
522 {
523   struct c_binding *prev = b->prev;
524
525   memset (b, 0, sizeof (struct c_binding));
526   b->prev = binding_freelist;
527   binding_freelist = b;
528
529   return prev;
530 }
531
532 \f
533 /* Hook called at end of compilation to assume 1 elt
534    for a file-scope tentative array defn that wasn't complete before.  */
535
536 void
537 c_finish_incomplete_decl (tree decl)
538 {
539   if (TREE_CODE (decl) == VAR_DECL)
540     {
541       tree type = TREE_TYPE (decl);
542       if (type != error_mark_node
543           && TREE_CODE (type) == ARRAY_TYPE
544           && !DECL_EXTERNAL (decl)
545           && TYPE_DOMAIN (type) == 0)
546         {
547           warning (0, "array %q+D assumed to have one element", decl);
548
549           complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
550
551           layout_decl (decl, 0);
552         }
553     }
554 }
555 \f
556 /* The Objective-C front-end often needs to determine the current scope.  */
557
558 void *
559 objc_get_current_scope (void)
560 {
561   return current_scope;
562 }
563
564 /* The following function is used only by Objective-C.  It needs to live here
565    because it accesses the innards of c_scope.  */
566
567 void
568 objc_mark_locals_volatile (void *enclosing_blk)
569 {
570   struct c_scope *scope;
571   struct c_binding *b;
572
573   for (scope = current_scope;
574        scope && scope != enclosing_blk;
575        scope = scope->outer)
576     {
577       for (b = scope->bindings; b; b = b->prev)
578         objc_volatilize_decl (b->decl);
579
580       /* Do not climb up past the current function.  */
581       if (scope->function_body)
582         break;
583     }
584 }
585
586 /* Nonzero if we are currently in file scope.  */
587
588 int
589 global_bindings_p (void)
590 {
591   return current_scope == file_scope && !c_override_global_bindings_to_false;
592 }
593
594 void
595 keep_next_level (void)
596 {
597   keep_next_level_flag = true;
598 }
599
600 /* Identify this scope as currently being filled with parameters.  */
601
602 void
603 declare_parm_level (void)
604 {
605   current_scope->parm_flag = true;
606 }
607
608 void
609 push_scope (void)
610 {
611   if (next_is_function_body)
612     {
613       /* This is the transition from the parameters to the top level
614          of the function body.  These are the same scope
615          (C99 6.2.1p4,6) so we do not push another scope structure.
616          next_is_function_body is set only by store_parm_decls, which
617          in turn is called when and only when we are about to
618          encounter the opening curly brace for the function body.
619
620          The outermost block of a function always gets a BLOCK node,
621          because the debugging output routines expect that each
622          function has at least one BLOCK.  */
623       current_scope->parm_flag         = false;
624       current_scope->function_body     = true;
625       current_scope->keep              = true;
626       current_scope->outer_function    = current_function_scope;
627       current_function_scope           = current_scope;
628
629       keep_next_level_flag = false;
630       next_is_function_body = false;
631     }
632   else
633     {
634       struct c_scope *scope;
635       if (scope_freelist)
636         {
637           scope = scope_freelist;
638           scope_freelist = scope->outer;
639         }
640       else
641         scope = GGC_CNEW (struct c_scope);
642
643       scope->keep          = keep_next_level_flag;
644       scope->outer         = current_scope;
645       scope->depth         = current_scope ? (current_scope->depth + 1) : 0;
646
647       /* Check for scope depth overflow.  Unlikely (2^28 == 268,435,456) but
648          possible.  */
649       if (current_scope && scope->depth == 0)
650         {
651           scope->depth--;
652           sorry ("GCC supports only %u nested scopes", scope->depth);
653         }
654
655       current_scope        = scope;
656       keep_next_level_flag = false;
657     }
658 }
659
660 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT.  */
661
662 static void
663 set_type_context (tree type, tree context)
664 {
665   for (type = TYPE_MAIN_VARIANT (type); type;
666        type = TYPE_NEXT_VARIANT (type))
667     TYPE_CONTEXT (type) = context;
668 }
669
670 /* Exit a scope.  Restore the state of the identifier-decl mappings
671    that were in effect when this scope was entered.  Return a BLOCK
672    node containing all the DECLs in this scope that are of interest
673    to debug info generation.  */
674
675 tree
676 pop_scope (void)
677 {
678   struct c_scope *scope = current_scope;
679   tree block, context, p;
680   struct c_binding *b;
681
682   bool functionbody = scope->function_body;
683   bool keep = functionbody || scope->keep || scope->bindings;
684
685   c_end_vm_scope (scope->depth);
686
687   /* If appropriate, create a BLOCK to record the decls for the life
688      of this function.  */
689   block = 0;
690   if (keep)
691     {
692       block = make_node (BLOCK);
693       BLOCK_SUBBLOCKS (block) = scope->blocks;
694       TREE_USED (block) = 1;
695
696       /* In each subblock, record that this is its superior.  */
697       for (p = scope->blocks; p; p = BLOCK_CHAIN (p))
698         BLOCK_SUPERCONTEXT (p) = block;
699
700       BLOCK_VARS (block) = 0;
701     }
702
703   /* The TYPE_CONTEXTs for all of the tagged types belonging to this
704      scope must be set so that they point to the appropriate
705      construct, i.e.  either to the current FUNCTION_DECL node, or
706      else to the BLOCK node we just constructed.
707
708      Note that for tagged types whose scope is just the formal
709      parameter list for some function type specification, we can't
710      properly set their TYPE_CONTEXTs here, because we don't have a
711      pointer to the appropriate FUNCTION_TYPE node readily available
712      to us.  For those cases, the TYPE_CONTEXTs of the relevant tagged
713      type nodes get set in `grokdeclarator' as soon as we have created
714      the FUNCTION_TYPE node which will represent the "scope" for these
715      "parameter list local" tagged types.  */
716   if (scope->function_body)
717     context = current_function_decl;
718   else if (scope == file_scope)
719     {
720       tree file_decl = build_decl (TRANSLATION_UNIT_DECL, 0, 0);
721       TREE_CHAIN (file_decl) = all_translation_units;
722       all_translation_units = file_decl;
723       context = file_decl;
724     }
725   else
726     context = block;
727
728   /* Clear all bindings in this scope.  */
729   for (b = scope->bindings; b; b = free_binding_and_advance (b))
730     {
731       p = b->decl;
732       switch (TREE_CODE (p))
733         {
734         case LABEL_DECL:
735           /* Warnings for unused labels, errors for undefined labels.  */
736           if (TREE_USED (p) && !DECL_INITIAL (p))
737             {
738               error ("label %q+D used but not defined", p);
739               DECL_INITIAL (p) = error_mark_node;
740             }
741           else 
742             warn_for_unused_label (p);
743
744           /* Labels go in BLOCK_VARS.  */
745           TREE_CHAIN (p) = BLOCK_VARS (block);
746           BLOCK_VARS (block) = p;
747           gcc_assert (I_LABEL_BINDING (b->id) == b);
748           I_LABEL_BINDING (b->id) = b->shadowed;
749           break;
750
751         case ENUMERAL_TYPE:
752         case UNION_TYPE:
753         case RECORD_TYPE:
754           set_type_context (p, context);
755
756           /* Types may not have tag-names, in which case the type
757              appears in the bindings list with b->id NULL.  */
758           if (b->id)
759             {
760               gcc_assert (I_TAG_BINDING (b->id) == b);
761               I_TAG_BINDING (b->id) = b->shadowed;
762             }
763           break;
764
765         case FUNCTION_DECL:
766           /* Propagate TREE_ADDRESSABLE from nested functions to their
767              containing functions.  */
768           if (!TREE_ASM_WRITTEN (p)
769               && DECL_INITIAL (p) != 0
770               && TREE_ADDRESSABLE (p)
771               && DECL_ABSTRACT_ORIGIN (p) != 0
772               && DECL_ABSTRACT_ORIGIN (p) != p)
773             TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
774           if (!DECL_EXTERNAL (p)
775               && !DECL_INITIAL (p)
776               && scope != file_scope
777               && scope != external_scope)
778             {
779               error ("nested function %q+D declared but never defined", p);
780               undef_nested_function = true;
781             }
782           /* C99 6.7.4p6: "a function with external linkage... declared
783              with an inline function specifier ... shall also be defined in the
784              same translation unit."  */
785           else if (DECL_DECLARED_INLINE_P (p)
786                    && TREE_PUBLIC (p)
787                    && !DECL_INITIAL (p)
788                    && !flag_gnu89_inline)
789             pedwarn ("inline function %q+D declared but never defined", p);
790
791           goto common_symbol;
792
793         case VAR_DECL:
794           /* Warnings for unused variables.  */
795           if (!TREE_USED (p)
796               && !TREE_NO_WARNING (p)
797               && !DECL_IN_SYSTEM_HEADER (p)
798               && DECL_NAME (p)
799               && !DECL_ARTIFICIAL (p)
800               && scope != file_scope
801               && scope != external_scope)
802             warning (OPT_Wunused_variable, "unused variable %q+D", p);
803
804           if (b->inner_comp)
805             {
806               error ("type of array %q+D completed incompatibly with"
807                      " implicit initialization", p);
808             }
809
810           /* Fall through.  */
811         case TYPE_DECL:
812         case CONST_DECL:
813         common_symbol:
814           /* All of these go in BLOCK_VARS, but only if this is the
815              binding in the home scope.  */
816           if (!b->nested)
817             {
818               TREE_CHAIN (p) = BLOCK_VARS (block);
819               BLOCK_VARS (block) = p;
820             }
821           /* If this is the file scope, and we are processing more
822              than one translation unit in this compilation, set
823              DECL_CONTEXT of each decl to the TRANSLATION_UNIT_DECL.
824              This makes same_translation_unit_p work, and causes
825              static declarations to be given disambiguating suffixes.  */
826           if (scope == file_scope && num_in_fnames > 1)
827             {
828               DECL_CONTEXT (p) = context;
829               if (TREE_CODE (p) == TYPE_DECL)
830                 set_type_context (TREE_TYPE (p), context);
831             }
832
833           /* Fall through.  */
834           /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
835              already been put there by store_parm_decls.  Unused-
836              parameter warnings are handled by function.c.
837              error_mark_node obviously does not go in BLOCK_VARS and
838              does not get unused-variable warnings.  */
839         case PARM_DECL:
840         case ERROR_MARK:
841           /* It is possible for a decl not to have a name.  We get
842              here with b->id NULL in this case.  */
843           if (b->id)
844             {
845               gcc_assert (I_SYMBOL_BINDING (b->id) == b);
846               I_SYMBOL_BINDING (b->id) = b->shadowed;
847               if (b->shadowed && b->shadowed->type)
848                 TREE_TYPE (b->shadowed->decl) = b->shadowed->type;
849             }
850           break;
851
852         default:
853           gcc_unreachable ();
854         }
855     }
856
857
858   /* Dispose of the block that we just made inside some higher level.  */
859   if ((scope->function_body || scope == file_scope) && context)
860     {
861       DECL_INITIAL (context) = block;
862       BLOCK_SUPERCONTEXT (block) = context;
863     }
864   else if (scope->outer)
865     {
866       if (block)
867         SCOPE_LIST_APPEND (scope->outer, blocks, block);
868       /* If we did not make a block for the scope just exited, any
869          blocks made for inner scopes must be carried forward so they
870          will later become subblocks of something else.  */
871       else if (scope->blocks)
872         SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
873     }
874
875   /* Pop the current scope, and free the structure for reuse.  */
876   current_scope = scope->outer;
877   if (scope->function_body)
878     current_function_scope = scope->outer_function;
879
880   memset (scope, 0, sizeof (struct c_scope));
881   scope->outer = scope_freelist;
882   scope_freelist = scope;
883
884   return block;
885 }
886
887 void
888 push_file_scope (void)
889 {
890   tree decl;
891
892   if (file_scope)
893     return;
894
895   push_scope ();
896   file_scope = current_scope;
897
898   start_fname_decls ();
899
900   for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl))
901     bind (DECL_NAME (decl), decl, file_scope,
902           /*invisible=*/false, /*nested=*/true);
903 }
904
905 void
906 pop_file_scope (void)
907 {
908   /* In case there were missing closebraces, get us back to the global
909      binding level.  */
910   while (current_scope != file_scope)
911     pop_scope ();
912
913   /* __FUNCTION__ is defined at file scope ("").  This
914      call may not be necessary as my tests indicate it
915      still works without it.  */
916   finish_fname_decls ();
917
918   /* This is the point to write out a PCH if we're doing that.
919      In that case we do not want to do anything else.  */
920   if (pch_file)
921     {
922       c_common_write_pch ();
923       return;
924     }
925
926   /* Pop off the file scope and close this translation unit.  */
927   pop_scope ();
928   file_scope = 0;
929
930   maybe_apply_pending_pragma_weaks ();
931   cgraph_finalize_compilation_unit ();
932 }
933
934 \f
935 /* Push a definition or a declaration of struct, union or enum tag "name".
936    "type" should be the type node.
937    We assume that the tag "name" is not already defined.
938
939    Note that the definition may really be just a forward reference.
940    In that case, the TYPE_SIZE will be zero.  */
941
942 static void
943 pushtag (tree name, tree type)
944 {
945   /* Record the identifier as the type's name if it has none.  */
946   if (name && !TYPE_NAME (type))
947     TYPE_NAME (type) = name;
948   bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false);
949
950   /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
951      tagged type we just added to the current scope.  This fake
952      NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
953      to output a representation of a tagged type, and it also gives
954      us a convenient place to record the "scope start" address for the
955      tagged type.  */
956
957   TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
958
959   /* An approximation for now, so we can tell this is a function-scope tag.
960      This will be updated in pop_scope.  */
961   TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
962 }
963 \f
964 /* Subroutine of compare_decls.  Allow harmless mismatches in return
965    and argument types provided that the type modes match.  This function
966    return a unified type given a suitable match, and 0 otherwise.  */
967
968 static tree
969 match_builtin_function_types (tree newtype, tree oldtype)
970 {
971   tree newrettype, oldrettype;
972   tree newargs, oldargs;
973   tree trytype, tryargs;
974
975   /* Accept the return type of the new declaration if same modes.  */
976   oldrettype = TREE_TYPE (oldtype);
977   newrettype = TREE_TYPE (newtype);
978
979   if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
980     return 0;
981
982   oldargs = TYPE_ARG_TYPES (oldtype);
983   newargs = TYPE_ARG_TYPES (newtype);
984   tryargs = newargs;
985
986   while (oldargs || newargs)
987     {
988       if (!oldargs
989           || !newargs
990           || !TREE_VALUE (oldargs)
991           || !TREE_VALUE (newargs)
992           || TYPE_MODE (TREE_VALUE (oldargs))
993              != TYPE_MODE (TREE_VALUE (newargs)))
994         return 0;
995
996       oldargs = TREE_CHAIN (oldargs);
997       newargs = TREE_CHAIN (newargs);
998     }
999
1000   trytype = build_function_type (newrettype, tryargs);
1001   return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
1002 }
1003
1004 /* Subroutine of diagnose_mismatched_decls.  Check for function type
1005    mismatch involving an empty arglist vs a nonempty one and give clearer
1006    diagnostics.  */
1007 static void
1008 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1009                            tree newtype, tree oldtype)
1010 {
1011   tree t;
1012
1013   if (TREE_CODE (olddecl) != FUNCTION_DECL
1014       || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1015       || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
1016            ||
1017            (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
1018     return;
1019
1020   t = TYPE_ARG_TYPES (oldtype);
1021   if (t == 0)
1022     t = TYPE_ARG_TYPES (newtype);
1023   for (; t; t = TREE_CHAIN (t))
1024     {
1025       tree type = TREE_VALUE (t);
1026
1027       if (TREE_CHAIN (t) == 0
1028           && TYPE_MAIN_VARIANT (type) != void_type_node)
1029         {
1030           inform ("a parameter list with an ellipsis can%'t match "
1031                   "an empty parameter name list declaration");
1032           break;
1033         }
1034
1035       if (c_type_promotes_to (type) != type)
1036         {
1037           inform ("an argument type that has a default promotion can%'t match "
1038                   "an empty parameter name list declaration");
1039           break;
1040         }
1041     }
1042 }
1043
1044 /* Another subroutine of diagnose_mismatched_decls.  OLDDECL is an
1045    old-style function definition, NEWDECL is a prototype declaration.
1046    Diagnose inconsistencies in the argument list.  Returns TRUE if
1047    the prototype is compatible, FALSE if not.  */
1048 static bool
1049 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1050 {
1051   tree newargs, oldargs;
1052   int i;
1053
1054 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1055
1056   oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1057   newargs = TYPE_ARG_TYPES (newtype);
1058   i = 1;
1059
1060   for (;;)
1061     {
1062       tree oldargtype = TREE_VALUE (oldargs);
1063       tree newargtype = TREE_VALUE (newargs);
1064
1065       if (oldargtype == error_mark_node || newargtype == error_mark_node)
1066         return false;
1067
1068       oldargtype = TYPE_MAIN_VARIANT (oldargtype);
1069       newargtype = TYPE_MAIN_VARIANT (newargtype);
1070
1071       if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1072         break;
1073
1074       /* Reaching the end of just one list means the two decls don't
1075          agree on the number of arguments.  */
1076       if (END_OF_ARGLIST (oldargtype))
1077         {
1078           error ("prototype for %q+D declares more arguments "
1079                  "than previous old-style definition", newdecl);
1080           return false;
1081         }
1082       else if (END_OF_ARGLIST (newargtype))
1083         {
1084           error ("prototype for %q+D declares fewer arguments "
1085                  "than previous old-style definition", newdecl);
1086           return false;
1087         }
1088
1089       /* Type for passing arg must be consistent with that declared
1090          for the arg.  */
1091       else if (!comptypes (oldargtype, newargtype))
1092         {
1093           error ("prototype for %q+D declares argument %d"
1094                  " with incompatible type",
1095                  newdecl, i);
1096           return false;
1097         }
1098
1099       oldargs = TREE_CHAIN (oldargs);
1100       newargs = TREE_CHAIN (newargs);
1101       i++;
1102     }
1103
1104   /* If we get here, no errors were found, but do issue a warning
1105      for this poor-style construct.  */
1106   warning (0, "prototype for %q+D follows non-prototype definition",
1107            newdecl);
1108   return true;
1109 #undef END_OF_ARGLIST
1110 }
1111
1112 /* Subroutine of diagnose_mismatched_decls.  Report the location of DECL,
1113    first in a pair of mismatched declarations, using the diagnostic
1114    function DIAG.  */
1115 static void
1116 locate_old_decl (tree decl, void (*diag)(const char *, ...) ATTRIBUTE_GCC_CDIAG(1,2))
1117 {
1118   if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1119     ;
1120   else if (DECL_INITIAL (decl))
1121     diag (G_("previous definition of %q+D was here"), decl);
1122   else if (C_DECL_IMPLICIT (decl))
1123     diag (G_("previous implicit declaration of %q+D was here"), decl);
1124   else
1125     diag (G_("previous declaration of %q+D was here"), decl);
1126 }
1127
1128 /* Subroutine of duplicate_decls.  Compare NEWDECL to OLDDECL.
1129    Returns true if the caller should proceed to merge the two, false
1130    if OLDDECL should simply be discarded.  As a side effect, issues
1131    all necessary diagnostics for invalid or poor-style combinations.
1132    If it returns true, writes the types of NEWDECL and OLDDECL to
1133    *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1134    TREE_TYPE (NEWDECL, OLDDECL) respectively.  */
1135
1136 static bool
1137 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1138                            tree *newtypep, tree *oldtypep)
1139 {
1140   tree newtype, oldtype;
1141   bool pedwarned = false;
1142   bool warned = false;
1143   bool retval = true;
1144
1145 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL)  \
1146                                   && DECL_EXTERNAL (DECL))
1147
1148   /* If we have error_mark_node for either decl or type, just discard
1149      the previous decl - we're in an error cascade already.  */
1150   if (olddecl == error_mark_node || newdecl == error_mark_node)
1151     return false;
1152   *oldtypep = oldtype = TREE_TYPE (olddecl);
1153   *newtypep = newtype = TREE_TYPE (newdecl);
1154   if (oldtype == error_mark_node || newtype == error_mark_node)
1155     return false;
1156
1157   /* Two different categories of symbol altogether.  This is an error
1158      unless OLDDECL is a builtin.  OLDDECL will be discarded in any case.  */
1159   if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1160     {
1161       if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1162             && DECL_BUILT_IN (olddecl)
1163             && !C_DECL_DECLARED_BUILTIN (olddecl)))
1164         {
1165           error ("%q+D redeclared as different kind of symbol", newdecl);
1166           locate_old_decl (olddecl, error);
1167         }
1168       else if (TREE_PUBLIC (newdecl))
1169         warning (0, "built-in function %q+D declared as non-function",
1170                  newdecl);
1171       else
1172         warning (OPT_Wshadow, "declaration of %q+D shadows "
1173                  "a built-in function", newdecl);
1174       return false;
1175     }
1176
1177   /* Enumerators have no linkage, so may only be declared once in a
1178      given scope.  */
1179   if (TREE_CODE (olddecl) == CONST_DECL)
1180     {
1181       error ("redeclaration of enumerator %q+D", newdecl);
1182       locate_old_decl (olddecl, error);
1183       return false;
1184     }
1185
1186   if (!comptypes (oldtype, newtype))
1187     {
1188       if (TREE_CODE (olddecl) == FUNCTION_DECL
1189           && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1190         {
1191           /* Accept harmless mismatch in function types.
1192              This is for the ffs and fprintf builtins.  */
1193           tree trytype = match_builtin_function_types (newtype, oldtype);
1194
1195           if (trytype && comptypes (newtype, trytype))
1196             *oldtypep = oldtype = trytype;
1197           else
1198             {
1199               /* If types don't match for a built-in, throw away the
1200                  built-in.  No point in calling locate_old_decl here, it
1201                  won't print anything.  */
1202               warning (0, "conflicting types for built-in function %q+D",
1203                        newdecl);
1204               return false;
1205             }
1206         }
1207       else if (TREE_CODE (olddecl) == FUNCTION_DECL
1208                && DECL_IS_BUILTIN (olddecl))
1209         {
1210           /* A conflicting function declaration for a predeclared
1211              function that isn't actually built in.  Objective C uses
1212              these.  The new declaration silently overrides everything
1213              but the volatility (i.e. noreturn) indication.  See also
1214              below.  FIXME: Make Objective C use normal builtins.  */
1215           TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1216           return false;
1217         }
1218       /* Permit void foo (...) to match int foo (...) if the latter is
1219          the definition and implicit int was used.  See
1220          c-torture/compile/920625-2.c.  */
1221       else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1222                && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1223                && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1224                && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1225         {
1226           pedwarn ("conflicting types for %q+D", newdecl);
1227           /* Make sure we keep void as the return type.  */
1228           TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1229           C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1230           pedwarned = true;
1231         }
1232       /* Permit void foo (...) to match an earlier call to foo (...) with
1233          no declared type (thus, implicitly int).  */
1234       else if (TREE_CODE (newdecl) == FUNCTION_DECL
1235                && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1236                && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1237                && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1238         {
1239           pedwarn ("conflicting types for %q+D", newdecl);
1240           /* Make sure we keep void as the return type.  */
1241           TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1242           pedwarned = true;
1243         }
1244       else
1245         {
1246           if (TYPE_QUALS (newtype) != TYPE_QUALS (oldtype))
1247             error ("conflicting type qualifiers for %q+D", newdecl);
1248           else
1249             error ("conflicting types for %q+D", newdecl);
1250           diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1251           locate_old_decl (olddecl, error);
1252           return false;
1253         }
1254     }
1255
1256   /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1257      but silently ignore the redeclaration if either is in a system
1258      header.  (Conflicting redeclarations were handled above.)  */
1259   if (TREE_CODE (newdecl) == TYPE_DECL)
1260     {
1261       if (DECL_IN_SYSTEM_HEADER (newdecl) || DECL_IN_SYSTEM_HEADER (olddecl))
1262         return true;  /* Allow OLDDECL to continue in use.  */
1263
1264       error ("redefinition of typedef %q+D", newdecl);
1265       locate_old_decl (olddecl, error);
1266       return false;
1267     }
1268
1269   /* Function declarations can either be 'static' or 'extern' (no
1270      qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1271      can never conflict with each other on account of linkage
1272      (6.2.2p4).  Multiple definitions are not allowed (6.9p3,5) but
1273      gnu89 mode permits two definitions if one is 'extern inline' and
1274      one is not.  The non- extern-inline definition supersedes the
1275      extern-inline definition.  */
1276
1277   else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1278     {
1279       /* If you declare a built-in function name as static, or
1280          define the built-in with an old-style definition (so we
1281          can't validate the argument list) the built-in definition is
1282          overridden, but optionally warn this was a bad choice of name.  */
1283       if (DECL_BUILT_IN (olddecl)
1284           && !C_DECL_DECLARED_BUILTIN (olddecl)
1285           && (!TREE_PUBLIC (newdecl)
1286               || (DECL_INITIAL (newdecl)
1287                   && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1288         {
1289           warning (OPT_Wshadow, "declaration of %q+D shadows "
1290                    "a built-in function", newdecl);
1291           /* Discard the old built-in function.  */
1292           return false;
1293         }
1294
1295       if (DECL_INITIAL (newdecl))
1296         {
1297           if (DECL_INITIAL (olddecl))
1298             {
1299               /* If both decls are in the same TU and the new declaration
1300                  isn't overriding an extern inline reject the new decl.
1301                  In c99, no overriding is allowed in the same translation
1302                  unit.  */
1303               if ((!DECL_EXTERN_INLINE (olddecl)
1304                    || DECL_EXTERN_INLINE (newdecl)
1305                    || (!flag_gnu89_inline
1306                        && (!DECL_DECLARED_INLINE_P (olddecl)
1307                            || !lookup_attribute ("gnu_inline",
1308                                                  DECL_ATTRIBUTES (olddecl)))
1309                        && (!DECL_DECLARED_INLINE_P (newdecl)
1310                            || !lookup_attribute ("gnu_inline",
1311                                                  DECL_ATTRIBUTES (newdecl))))
1312                   )
1313                   && same_translation_unit_p (newdecl, olddecl))
1314                 {
1315                   error ("redefinition of %q+D", newdecl);
1316                   locate_old_decl (olddecl, error);
1317                   return false;
1318                 }
1319             }
1320         }
1321       /* If we have a prototype after an old-style function definition,
1322          the argument types must be checked specially.  */
1323       else if (DECL_INITIAL (olddecl)
1324                && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1325                && TYPE_ACTUAL_ARG_TYPES (oldtype)
1326                && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1327         {
1328           locate_old_decl (olddecl, error);
1329           return false;
1330         }
1331       /* A non-static declaration (even an "extern") followed by a
1332          static declaration is undefined behavior per C99 6.2.2p3-5,7.
1333          The same is true for a static forward declaration at block
1334          scope followed by a non-static declaration/definition at file
1335          scope.  Static followed by non-static at the same scope is
1336          not undefined behavior, and is the most convenient way to get
1337          some effects (see e.g.  what unwind-dw2-fde-glibc.c does to
1338          the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1339          we do diagnose it if -Wtraditional.  */
1340       if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1341         {
1342           /* Two exceptions to the rule.  If olddecl is an extern
1343              inline, or a predeclared function that isn't actually
1344              built in, newdecl silently overrides olddecl.  The latter
1345              occur only in Objective C; see also above.  (FIXME: Make
1346              Objective C use normal builtins.)  */
1347           if (!DECL_IS_BUILTIN (olddecl)
1348               && !DECL_EXTERN_INLINE (olddecl))
1349             {
1350               error ("static declaration of %q+D follows "
1351                      "non-static declaration", newdecl);
1352               locate_old_decl (olddecl, error);
1353             }
1354           return false;
1355         }
1356       else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
1357         {
1358           if (DECL_CONTEXT (olddecl))
1359             {
1360               error ("non-static declaration of %q+D follows "
1361                      "static declaration", newdecl);
1362               locate_old_decl (olddecl, error);
1363               return false;
1364             }
1365           else if (warn_traditional)
1366             {
1367               warning (OPT_Wtraditional, "non-static declaration of %q+D "
1368                        "follows static declaration", newdecl);
1369               warned = true;
1370             }
1371         }
1372
1373       /* Make sure gnu_inline attribute is either not present, or
1374          present on all inline decls.  */
1375       if (DECL_DECLARED_INLINE_P (olddecl)
1376           && DECL_DECLARED_INLINE_P (newdecl))
1377         {
1378           bool newa = lookup_attribute ("gnu_inline",
1379                                         DECL_ATTRIBUTES (newdecl)) != NULL;
1380           bool olda = lookup_attribute ("gnu_inline",
1381                                         DECL_ATTRIBUTES (olddecl)) != NULL;
1382           if (newa != olda)
1383             {
1384               error ("%<gnu_inline%> attribute present on %q+D",
1385                      newa ? newdecl : olddecl);
1386               error ("%Jbut not here", newa ? olddecl : newdecl);
1387             }
1388         }
1389     }
1390   else if (TREE_CODE (newdecl) == VAR_DECL)
1391     {
1392       /* Only variables can be thread-local, and all declarations must
1393          agree on this property.  */
1394       if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
1395         {
1396           /* Nothing to check.  Since OLDDECL is marked threadprivate
1397              and NEWDECL does not have a thread-local attribute, we
1398              will merge the threadprivate attribute into NEWDECL.  */
1399           ;
1400         }
1401       else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
1402         {
1403           if (DECL_THREAD_LOCAL_P (newdecl))
1404             error ("thread-local declaration of %q+D follows "
1405                    "non-thread-local declaration", newdecl);
1406           else
1407             error ("non-thread-local declaration of %q+D follows "
1408                    "thread-local declaration", newdecl);
1409
1410           locate_old_decl (olddecl, error);
1411           return false;
1412         }
1413
1414       /* Multiple initialized definitions are not allowed (6.9p3,5).  */
1415       if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1416         {
1417           error ("redefinition of %q+D", newdecl);
1418           locate_old_decl (olddecl, error);
1419           return false;
1420         }
1421
1422       /* Objects declared at file scope: if the first declaration had
1423          external linkage (even if it was an external reference) the
1424          second must have external linkage as well, or the behavior is
1425          undefined.  If the first declaration had internal linkage, then
1426          the second must too, or else be an external reference (in which
1427          case the composite declaration still has internal linkage).
1428          As for function declarations, we warn about the static-then-
1429          extern case only for -Wtraditional.  See generally 6.2.2p3-5,7.  */
1430       if (DECL_FILE_SCOPE_P (newdecl)
1431           && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1432         {
1433           if (DECL_EXTERNAL (newdecl))
1434             {
1435               if (!DECL_FILE_SCOPE_P (olddecl))
1436                 {
1437                   error ("extern declaration of %q+D follows "
1438                          "declaration with no linkage", newdecl);
1439                   locate_old_decl (olddecl, error);
1440                   return false;
1441                 }
1442               else if (warn_traditional)
1443                 {
1444                   warning (OPT_Wtraditional, "non-static declaration of %q+D "
1445                            "follows static declaration", newdecl);
1446                   warned = true;
1447                 }
1448             }
1449           else
1450             {
1451               if (TREE_PUBLIC (newdecl))
1452                 error ("non-static declaration of %q+D follows "
1453                        "static declaration", newdecl);
1454               else
1455                 error ("static declaration of %q+D follows "
1456                        "non-static declaration", newdecl);
1457
1458               locate_old_decl (olddecl, error);
1459               return false;
1460             }
1461         }
1462       /* Two objects with the same name declared at the same block
1463          scope must both be external references (6.7p3).  */
1464       else if (!DECL_FILE_SCOPE_P (newdecl))
1465         {
1466           if (DECL_EXTERNAL (newdecl))
1467             {
1468               /* Extern with initializer at block scope, which will
1469                  already have received an error.  */
1470             }
1471           else if (DECL_EXTERNAL (olddecl))
1472             {
1473               error ("declaration of %q+D with no linkage follows "
1474                      "extern declaration", newdecl);
1475               locate_old_decl (olddecl, error);
1476             }
1477           else
1478             {
1479               error ("redeclaration of %q+D with no linkage", newdecl);
1480               locate_old_decl (olddecl, error);
1481             }
1482
1483           return false;
1484         }
1485     }
1486
1487   /* warnings */
1488   /* All decls must agree on a visibility.  */
1489   if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
1490       && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
1491       && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1492     {
1493       warning (0, "redeclaration of %q+D with different visibility "
1494                "(old visibility preserved)", newdecl);
1495       warned = true;
1496     }
1497
1498   if (TREE_CODE (newdecl) == FUNCTION_DECL)
1499     {
1500       /* Diagnose inline __attribute__ ((noinline)) which is silly.  */
1501       if (DECL_DECLARED_INLINE_P (newdecl)
1502           && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1503         {
1504           warning (OPT_Wattributes, "inline declaration of %qD follows "
1505                    "declaration with attribute noinline", newdecl);
1506           warned = true;
1507         }
1508       else if (DECL_DECLARED_INLINE_P (olddecl)
1509                && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1510         {
1511           warning (OPT_Wattributes, "declaration of %q+D with attribute "
1512                    "noinline follows inline declaration ", newdecl);
1513           warned = true;
1514         }
1515
1516       /* Inline declaration after use or definition.
1517          ??? Should we still warn about this now we have unit-at-a-time
1518          mode and can get it right?
1519          Definitely don't complain if the decls are in different translation
1520          units.
1521          C99 permits this, so don't warn in that case.  (The function
1522          may not be inlined everywhere in function-at-a-time mode, but
1523          we still shouldn't warn.)  */
1524       if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl)
1525           && same_translation_unit_p (olddecl, newdecl)
1526           && flag_gnu89_inline)
1527         {
1528           if (TREE_USED (olddecl))
1529             {
1530               warning (0, "%q+D declared inline after being called", olddecl);
1531               warned = true;
1532             }
1533           else if (DECL_INITIAL (olddecl))
1534             {
1535               warning (0, "%q+D declared inline after its definition", olddecl);
1536               warned = true;
1537             }
1538         }
1539     }
1540   else /* PARM_DECL, VAR_DECL */
1541     {
1542       /* Redeclaration of a parameter is a constraint violation (this is
1543          not explicitly stated, but follows from C99 6.7p3 [no more than
1544          one declaration of the same identifier with no linkage in the
1545          same scope, except type tags] and 6.2.2p6 [parameters have no
1546          linkage]).  We must check for a forward parameter declaration,
1547          indicated by TREE_ASM_WRITTEN on the old declaration - this is
1548          an extension, the mandatory diagnostic for which is handled by
1549          mark_forward_parm_decls.  */
1550
1551       if (TREE_CODE (newdecl) == PARM_DECL
1552           && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1553         {
1554           error ("redefinition of parameter %q+D", newdecl);
1555           locate_old_decl (olddecl, error);
1556           return false;
1557         }
1558     }
1559
1560   /* Optional warning for completely redundant decls.  */
1561   if (!warned && !pedwarned
1562       && warn_redundant_decls
1563       /* Don't warn about a function declaration followed by a
1564          definition.  */
1565       && !(TREE_CODE (newdecl) == FUNCTION_DECL
1566            && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1567       /* Don't warn about redundant redeclarations of builtins.  */
1568       && !(TREE_CODE (newdecl) == FUNCTION_DECL
1569            && !DECL_BUILT_IN (newdecl)
1570            && DECL_BUILT_IN (olddecl)
1571            && !C_DECL_DECLARED_BUILTIN (olddecl))
1572       /* Don't warn about an extern followed by a definition.  */
1573       && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1574       /* Don't warn about forward parameter decls.  */
1575       && !(TREE_CODE (newdecl) == PARM_DECL
1576            && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1577       /* Don't warn about a variable definition following a declaration.  */
1578       && !(TREE_CODE (newdecl) == VAR_DECL
1579            && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
1580     {
1581       warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
1582                newdecl);
1583       warned = true;
1584     }
1585
1586   /* Report location of previous decl/defn in a consistent manner.  */
1587   if (warned || pedwarned)
1588     locate_old_decl (olddecl, pedwarned ? pedwarn : warning0);
1589
1590 #undef DECL_EXTERN_INLINE
1591
1592   return retval;
1593 }
1594
1595 /* Subroutine of duplicate_decls.  NEWDECL has been found to be
1596    consistent with OLDDECL, but carries new information.  Merge the
1597    new information into OLDDECL.  This function issues no
1598    diagnostics.  */
1599
1600 static void
1601 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1602 {
1603   bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1604                             && DECL_INITIAL (newdecl) != 0);
1605   bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
1606                            && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != 0);
1607   bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
1608                            && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) != 0);
1609   bool extern_changed = false;
1610
1611   /* For real parm decl following a forward decl, rechain the old decl
1612      in its new location and clear TREE_ASM_WRITTEN (it's not a
1613      forward decl anymore).  */
1614   if (TREE_CODE (newdecl) == PARM_DECL
1615       && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1616     {
1617       struct c_binding *b, **here;
1618
1619       for (here = &current_scope->bindings; *here; here = &(*here)->prev)
1620         if ((*here)->decl == olddecl)
1621           goto found;
1622       gcc_unreachable ();
1623
1624     found:
1625       b = *here;
1626       *here = b->prev;
1627       b->prev = current_scope->bindings;
1628       current_scope->bindings = b;
1629
1630       TREE_ASM_WRITTEN (olddecl) = 0;
1631     }
1632
1633   DECL_ATTRIBUTES (newdecl)
1634     = targetm.merge_decl_attributes (olddecl, newdecl);
1635
1636   /* Merge the data types specified in the two decls.  */
1637   TREE_TYPE (newdecl)
1638     = TREE_TYPE (olddecl)
1639     = composite_type (newtype, oldtype);
1640
1641   /* Lay the type out, unless already done.  */
1642   if (!comptypes (oldtype, TREE_TYPE (newdecl)))
1643     {
1644       if (TREE_TYPE (newdecl) != error_mark_node)
1645         layout_type (TREE_TYPE (newdecl));
1646       if (TREE_CODE (newdecl) != FUNCTION_DECL
1647           && TREE_CODE (newdecl) != TYPE_DECL
1648           && TREE_CODE (newdecl) != CONST_DECL)
1649         layout_decl (newdecl, 0);
1650     }
1651   else
1652     {
1653       /* Since the type is OLDDECL's, make OLDDECL's size go with.  */
1654       DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1655       DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1656       DECL_MODE (newdecl) = DECL_MODE (olddecl);
1657       if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1658         {
1659           DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1660           DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
1661         }
1662     }
1663
1664   /* Keep the old rtl since we can safely use it.  */
1665   if (HAS_RTL_P (olddecl))
1666     COPY_DECL_RTL (olddecl, newdecl);
1667
1668   /* Merge the type qualifiers.  */
1669   if (TREE_READONLY (newdecl))
1670     TREE_READONLY (olddecl) = 1;
1671
1672   if (TREE_THIS_VOLATILE (newdecl))
1673     TREE_THIS_VOLATILE (olddecl) = 1;
1674
1675   /* Merge deprecatedness.  */
1676   if (TREE_DEPRECATED (newdecl))
1677     TREE_DEPRECATED (olddecl) = 1;
1678
1679   /* Keep source location of definition rather than declaration and of
1680      prototype rather than non-prototype unless that prototype is
1681      built-in.  */
1682   if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1683       || (old_is_prototype && !new_is_prototype
1684           && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
1685     DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1686
1687   /* Merge the initialization information.  */
1688    if (DECL_INITIAL (newdecl) == 0)
1689     DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1690
1691   /* Merge the threadprivate attribute.  */
1692   if (TREE_CODE (olddecl) == VAR_DECL && C_DECL_THREADPRIVATE_P (olddecl))
1693     {
1694       DECL_TLS_MODEL (newdecl) = DECL_TLS_MODEL (olddecl);
1695       C_DECL_THREADPRIVATE_P (newdecl) = 1;
1696     }
1697
1698   if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
1699     {
1700       /* Merge the unused-warning information.  */
1701       if (DECL_IN_SYSTEM_HEADER (olddecl))
1702         DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1703       else if (DECL_IN_SYSTEM_HEADER (newdecl))
1704         DECL_IN_SYSTEM_HEADER (olddecl) = 1;
1705
1706       /* Merge the section attribute.
1707          We want to issue an error if the sections conflict but that
1708          must be done later in decl_attributes since we are called
1709          before attributes are assigned.  */
1710       if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1711         DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1712
1713       /* Copy the assembler name.
1714          Currently, it can only be defined in the prototype.  */
1715       COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1716
1717       /* Use visibility of whichever declaration had it specified */
1718       if (DECL_VISIBILITY_SPECIFIED (olddecl))
1719         {
1720           DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1721           DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
1722         }
1723
1724       if (TREE_CODE (newdecl) == FUNCTION_DECL)
1725         {
1726           DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1727           DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1728           DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1729           DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1730             |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1731           TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1732           DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1733           DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
1734           TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1735           DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
1736           DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
1737         }
1738
1739       /* Merge the storage class information.  */
1740       merge_weak (newdecl, olddecl);
1741
1742       /* For functions, static overrides non-static.  */
1743       if (TREE_CODE (newdecl) == FUNCTION_DECL)
1744         {
1745           TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1746           /* This is since we don't automatically
1747              copy the attributes of NEWDECL into OLDDECL.  */
1748           TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1749           /* If this clears `static', clear it in the identifier too.  */
1750           if (!TREE_PUBLIC (olddecl))
1751             TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1752         }
1753     }
1754
1755   /* In c99, 'extern' declaration before (or after) 'inline' means this
1756      function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
1757      is present.  */
1758   if (TREE_CODE (newdecl) == FUNCTION_DECL
1759       && !flag_gnu89_inline
1760       && (DECL_DECLARED_INLINE_P (newdecl)
1761           || DECL_DECLARED_INLINE_P (olddecl))
1762       && (!DECL_DECLARED_INLINE_P (newdecl)
1763           || !DECL_DECLARED_INLINE_P (olddecl)
1764           || !DECL_EXTERNAL (olddecl))
1765       && DECL_EXTERNAL (newdecl)
1766       && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))
1767       && !current_function_decl)
1768     DECL_EXTERNAL (newdecl) = 0;
1769
1770   if (DECL_EXTERNAL (newdecl))
1771     {
1772       TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1773       DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1774
1775       /* An extern decl does not override previous storage class.  */
1776       TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1777       if (!DECL_EXTERNAL (newdecl))
1778         {
1779           DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1780           DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1781         }
1782     }
1783   else
1784     {
1785       TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1786       TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1787     }
1788
1789   if (TREE_CODE (newdecl) == FUNCTION_DECL)
1790     {
1791       /* If we're redefining a function previously defined as extern
1792          inline, make sure we emit debug info for the inline before we
1793          throw it away, in case it was inlined into a function that
1794          hasn't been written out yet.  */
1795       if (new_is_definition && DECL_INITIAL (olddecl))
1796         {
1797           if (TREE_USED (olddecl)
1798               /* In unit-at-a-time mode we never inline re-defined extern
1799                  inline functions.  */
1800               && !flag_unit_at_a_time
1801               && cgraph_function_possibly_inlined_p (olddecl))
1802             (*debug_hooks->outlining_inline_function) (olddecl);
1803
1804           /* The new defn must not be inline.  */
1805           DECL_INLINE (newdecl) = 0;
1806           DECL_UNINLINABLE (newdecl) = 1;
1807         }
1808       else
1809         {
1810           /* If either decl says `inline', this fn is inline, unless
1811              its definition was passed already.  */
1812           if (DECL_DECLARED_INLINE_P (newdecl)
1813               || DECL_DECLARED_INLINE_P (olddecl))
1814             DECL_DECLARED_INLINE_P (newdecl) = 1;
1815
1816           DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1817             = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1818
1819           DECL_DISREGARD_INLINE_LIMITS (newdecl)
1820             = DECL_DISREGARD_INLINE_LIMITS (olddecl)
1821             = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
1822                || DECL_DISREGARD_INLINE_LIMITS (olddecl));
1823         }
1824
1825       if (DECL_BUILT_IN (olddecl))
1826         {
1827           /* If redeclaring a builtin function, it stays built in.
1828              But it gets tagged as having been declared.  */
1829           DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1830           DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1831           C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1832           if (new_is_prototype)
1833             C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
1834           else
1835             C_DECL_BUILTIN_PROTOTYPE (newdecl)
1836               = C_DECL_BUILTIN_PROTOTYPE (olddecl);
1837         }
1838
1839       /* Also preserve various other info from the definition.  */
1840       if (!new_is_definition)
1841         {
1842           DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1843           DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1844           DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1845           DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1846           DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1847
1848           /* Set DECL_INLINE on the declaration if we've got a body
1849              from which to instantiate.  */
1850           if (DECL_INLINE (olddecl) && !DECL_UNINLINABLE (newdecl))
1851             {
1852               DECL_INLINE (newdecl) = 1;
1853               DECL_ABSTRACT_ORIGIN (newdecl)
1854                 = DECL_ABSTRACT_ORIGIN (olddecl);
1855             }
1856         }
1857       else
1858         {
1859           /* If a previous declaration said inline, mark the
1860              definition as inlinable.  */
1861           if (DECL_DECLARED_INLINE_P (newdecl)
1862               && !DECL_UNINLINABLE (newdecl))
1863             DECL_INLINE (newdecl) = 1;
1864         }
1865     }
1866
1867    extern_changed = DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl);
1868
1869   /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1870      But preserve OLDDECL's DECL_UID and DECL_CONTEXT.  */
1871   {
1872     unsigned olddecl_uid = DECL_UID (olddecl);
1873     tree olddecl_context = DECL_CONTEXT (olddecl);
1874
1875     memcpy ((char *) olddecl + sizeof (struct tree_common),
1876             (char *) newdecl + sizeof (struct tree_common),
1877             sizeof (struct tree_decl_common) - sizeof (struct tree_common));
1878     switch (TREE_CODE (olddecl))
1879       {
1880       case FIELD_DECL:
1881       case VAR_DECL:
1882       case PARM_DECL:
1883       case LABEL_DECL:
1884       case RESULT_DECL:
1885       case CONST_DECL:
1886       case TYPE_DECL:
1887       case FUNCTION_DECL:
1888         memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
1889                 (char *) newdecl + sizeof (struct tree_decl_common),
1890                 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
1891         break;
1892
1893       default:
1894
1895         memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
1896                 (char *) newdecl + sizeof (struct tree_decl_common),
1897                 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
1898       }
1899     DECL_UID (olddecl) = olddecl_uid;
1900     DECL_CONTEXT (olddecl) = olddecl_context;
1901   }
1902
1903   /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1904      so that encode_section_info has a chance to look at the new decl
1905      flags and attributes.  */
1906   if (DECL_RTL_SET_P (olddecl)
1907       && (TREE_CODE (olddecl) == FUNCTION_DECL
1908           || (TREE_CODE (olddecl) == VAR_DECL
1909               && TREE_STATIC (olddecl))))
1910     make_decl_rtl (olddecl);
1911
1912   /* If we changed a function from DECL_EXTERNAL to !DECL_EXTERNAL,
1913      and the definition is coming from the old version, cgraph needs
1914      to be called again.  */
1915   if (extern_changed && !new_is_definition
1916       && TREE_CODE (olddecl) == FUNCTION_DECL && DECL_INITIAL (olddecl))
1917     cgraph_mark_if_needed (olddecl);
1918 }
1919
1920 /* Handle when a new declaration NEWDECL has the same name as an old
1921    one OLDDECL in the same binding contour.  Prints an error message
1922    if appropriate.
1923
1924    If safely possible, alter OLDDECL to look like NEWDECL, and return
1925    true.  Otherwise, return false.  */
1926
1927 static bool
1928 duplicate_decls (tree newdecl, tree olddecl)
1929 {
1930   tree newtype = NULL, oldtype = NULL;
1931
1932   if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
1933     {
1934       /* Avoid `unused variable' and other warnings for OLDDECL.  */
1935       TREE_NO_WARNING (olddecl) = 1;
1936       return false;
1937     }
1938
1939   merge_decls (newdecl, olddecl, newtype, oldtype);
1940   return true;
1941 }
1942
1943 \f
1944 /* Check whether decl-node NEW_DECL shadows an existing declaration.  */
1945 static void
1946 warn_if_shadowing (tree new_decl)
1947 {
1948   struct c_binding *b;
1949
1950   /* Shadow warnings wanted?  */
1951   if (!warn_shadow
1952       /* No shadow warnings for internally generated vars.  */
1953       || DECL_IS_BUILTIN (new_decl)
1954       /* No shadow warnings for vars made for inlining.  */
1955       || DECL_FROM_INLINE (new_decl))
1956     return;
1957
1958   /* Is anything being shadowed?  Invisible decls do not count.  */
1959   for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
1960     if (b->decl && b->decl != new_decl && !b->invisible)
1961       {
1962         tree old_decl = b->decl;
1963
1964         if (old_decl == error_mark_node)
1965           {
1966             warning (OPT_Wshadow, "declaration of %q+D shadows previous "
1967                      "non-variable", new_decl);
1968             break;
1969           }
1970         else if (TREE_CODE (old_decl) == PARM_DECL)
1971           warning (OPT_Wshadow, "declaration of %q+D shadows a parameter",
1972                    new_decl);
1973         else if (DECL_FILE_SCOPE_P (old_decl))
1974           warning (OPT_Wshadow, "declaration of %q+D shadows a global "
1975                    "declaration", new_decl);
1976         else if (TREE_CODE (old_decl) == FUNCTION_DECL
1977                  && DECL_BUILT_IN (old_decl))
1978           {
1979             warning (OPT_Wshadow, "declaration of %q+D shadows "
1980                      "a built-in function", new_decl);
1981             break;
1982           }
1983         else
1984           warning (OPT_Wshadow, "declaration of %q+D shadows a previous local",
1985                    new_decl);
1986
1987         warning (OPT_Wshadow, "%Jshadowed declaration is here", old_decl);
1988
1989         break;
1990       }
1991 }
1992
1993
1994 /* Subroutine of pushdecl.
1995
1996    X is a TYPE_DECL for a typedef statement.  Create a brand new
1997    ..._TYPE node (which will be just a variant of the existing
1998    ..._TYPE node with identical properties) and then install X
1999    as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
2000
2001    The whole point here is to end up with a situation where each
2002    and every ..._TYPE node the compiler creates will be uniquely
2003    associated with AT MOST one node representing a typedef name.
2004    This way, even though the compiler substitutes corresponding
2005    ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
2006    early on, later parts of the compiler can always do the reverse
2007    translation and get back the corresponding typedef name.  For
2008    example, given:
2009
2010         typedef struct S MY_TYPE;
2011         MY_TYPE object;
2012
2013    Later parts of the compiler might only know that `object' was of
2014    type `struct S' if it were not for code just below.  With this
2015    code however, later parts of the compiler see something like:
2016
2017         struct S' == struct S
2018         typedef struct S' MY_TYPE;
2019         struct S' object;
2020
2021     And they can then deduce (from the node for type struct S') that
2022     the original object declaration was:
2023
2024                 MY_TYPE object;
2025
2026     Being able to do this is important for proper support of protoize,
2027     and also for generating precise symbolic debugging information
2028     which takes full account of the programmer's (typedef) vocabulary.
2029
2030     Obviously, we don't want to generate a duplicate ..._TYPE node if
2031     the TYPE_DECL node that we are now processing really represents a
2032     standard built-in type.  */
2033
2034 static void
2035 clone_underlying_type (tree x)
2036 {
2037   if (DECL_IS_BUILTIN (x))
2038     {
2039       if (TYPE_NAME (TREE_TYPE (x)) == 0)
2040         TYPE_NAME (TREE_TYPE (x)) = x;
2041     }
2042   else if (TREE_TYPE (x) != error_mark_node
2043            && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
2044     {
2045       tree tt = TREE_TYPE (x);
2046       DECL_ORIGINAL_TYPE (x) = tt;
2047       tt = build_variant_type_copy (tt);
2048       TYPE_NAME (tt) = x;
2049       TREE_USED (tt) = TREE_USED (x);
2050       TREE_TYPE (x) = tt;
2051     }
2052 }
2053
2054 /* Record a decl-node X as belonging to the current lexical scope.
2055    Check for errors (such as an incompatible declaration for the same
2056    name already seen in the same scope).
2057
2058    Returns either X or an old decl for the same name.
2059    If an old decl is returned, it may have been smashed
2060    to agree with what X says.  */
2061
2062 tree
2063 pushdecl (tree x)
2064 {
2065   tree name = DECL_NAME (x);
2066   struct c_scope *scope = current_scope;
2067   struct c_binding *b;
2068   bool nested = false;
2069
2070   /* Must set DECL_CONTEXT for everything not at file scope or
2071      DECL_FILE_SCOPE_P won't work.  Local externs don't count
2072      unless they have initializers (which generate code).  */
2073   if (current_function_decl
2074       && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
2075           || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2076     DECL_CONTEXT (x) = current_function_decl;
2077
2078   /* If this is of variably modified type, prevent jumping into its
2079      scope.  */
2080   if ((TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == TYPE_DECL)
2081       && variably_modified_type_p (TREE_TYPE (x), NULL_TREE))
2082     c_begin_vm_scope (scope->depth);
2083
2084   /* Anonymous decls are just inserted in the scope.  */
2085   if (!name)
2086     {
2087       bind (name, x, scope, /*invisible=*/false, /*nested=*/false);
2088       return x;
2089     }
2090
2091   /* First, see if there is another declaration with the same name in
2092      the current scope.  If there is, duplicate_decls may do all the
2093      work for us.  If duplicate_decls returns false, that indicates
2094      two incompatible decls in the same scope; we are to silently
2095      replace the old one (duplicate_decls has issued all appropriate
2096      diagnostics).  In particular, we should not consider possible
2097      duplicates in the external scope, or shadowing.  */
2098   b = I_SYMBOL_BINDING (name);
2099   if (b && B_IN_SCOPE (b, scope))
2100     {
2101       struct c_binding *b_ext, *b_use;
2102       tree type = TREE_TYPE (x);
2103       tree visdecl = b->decl;
2104       tree vistype = TREE_TYPE (visdecl);
2105       if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2106           && COMPLETE_TYPE_P (TREE_TYPE (x)))
2107         b->inner_comp = false;
2108       b_use = b;
2109       b_ext = b;
2110       /* If this is an external linkage declaration, we should check
2111          for compatibility with the type in the external scope before
2112          setting the type at this scope based on the visible
2113          information only.  */
2114       if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2115         {
2116           while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2117             b_ext = b_ext->shadowed;
2118           if (b_ext)
2119             {
2120               b_use = b_ext;
2121               if (b_use->type)
2122                 TREE_TYPE (b_use->decl) = b_use->type;
2123             }
2124         }
2125       if (duplicate_decls (x, b_use->decl))
2126         {
2127           if (b_use != b)
2128             {
2129               /* Save the updated type in the external scope and
2130                  restore the proper type for this scope.  */
2131               tree thistype;
2132               if (comptypes (vistype, type))
2133                 thistype = composite_type (vistype, type);
2134               else
2135                 thistype = TREE_TYPE (b_use->decl);
2136               b_use->type = TREE_TYPE (b_use->decl);
2137               if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2138                   && DECL_BUILT_IN (b_use->decl))
2139                 thistype
2140                   = build_type_attribute_variant (thistype,
2141                                                   TYPE_ATTRIBUTES
2142                                                   (b_use->type));
2143               TREE_TYPE (b_use->decl) = thistype;
2144             }
2145           return b_use->decl;
2146         }
2147       else
2148         goto skip_external_and_shadow_checks;
2149     }
2150
2151   /* All declarations with external linkage, and all external
2152      references, go in the external scope, no matter what scope is
2153      current.  However, the binding in that scope is ignored for
2154      purposes of normal name lookup.  A separate binding structure is
2155      created in the requested scope; this governs the normal
2156      visibility of the symbol.
2157
2158      The binding in the externals scope is used exclusively for
2159      detecting duplicate declarations of the same object, no matter
2160      what scope they are in; this is what we do here.  (C99 6.2.7p2:
2161      All declarations that refer to the same object or function shall
2162      have compatible type; otherwise, the behavior is undefined.)  */
2163   if (DECL_EXTERNAL (x) || scope == file_scope)
2164     {
2165       tree type = TREE_TYPE (x);
2166       tree vistype = 0;
2167       tree visdecl = 0;
2168       bool type_saved = false;
2169       if (b && !B_IN_EXTERNAL_SCOPE (b)
2170           && (TREE_CODE (b->decl) == FUNCTION_DECL
2171               || TREE_CODE (b->decl) == VAR_DECL)
2172           && DECL_FILE_SCOPE_P (b->decl))
2173         {
2174           visdecl = b->decl;
2175           vistype = TREE_TYPE (visdecl);
2176         }
2177       if (scope != file_scope
2178           && !DECL_IN_SYSTEM_HEADER (x))
2179         warning (OPT_Wnested_externs, "nested extern declaration of %qD", x);
2180
2181       while (b && !B_IN_EXTERNAL_SCOPE (b))
2182         {
2183           /* If this decl might be modified, save its type.  This is
2184              done here rather than when the decl is first bound
2185              because the type may change after first binding, through
2186              being completed or through attributes being added.  If we
2187              encounter multiple such decls, only the first should have
2188              its type saved; the others will already have had their
2189              proper types saved and the types will not have changed as
2190              their scopes will not have been re-entered.  */
2191           if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2192             {
2193               b->type = TREE_TYPE (b->decl);
2194               type_saved = true;
2195             }
2196           if (B_IN_FILE_SCOPE (b)
2197               && TREE_CODE (b->decl) == VAR_DECL
2198               && TREE_STATIC (b->decl)
2199               && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2200               && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2201               && TREE_CODE (type) == ARRAY_TYPE
2202               && TYPE_DOMAIN (type)
2203               && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2204               && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2205             {
2206               /* Array type completed in inner scope, which should be
2207                  diagnosed if the completion does not have size 1 and
2208                  it does not get completed in the file scope.  */
2209               b->inner_comp = true;
2210             }
2211           b = b->shadowed;
2212         }
2213
2214       /* If a matching external declaration has been found, set its
2215          type to the composite of all the types of that declaration.
2216          After the consistency checks, it will be reset to the
2217          composite of the visible types only.  */
2218       if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2219           && b->type)
2220         TREE_TYPE (b->decl) = b->type;
2221
2222       /* The point of the same_translation_unit_p check here is,
2223          we want to detect a duplicate decl for a construct like
2224          foo() { extern bar(); } ... static bar();  but not if
2225          they are in different translation units.  In any case,
2226          the static does not go in the externals scope.  */
2227       if (b
2228           && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2229           && duplicate_decls (x, b->decl))
2230         {
2231           tree thistype;
2232           if (vistype)
2233             {
2234               if (comptypes (vistype, type))
2235                 thistype = composite_type (vistype, type);
2236               else
2237                 thistype = TREE_TYPE (b->decl);
2238             }
2239           else
2240             thistype = type;
2241           b->type = TREE_TYPE (b->decl);
2242           if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2243             thistype
2244               = build_type_attribute_variant (thistype,
2245                                               TYPE_ATTRIBUTES (b->type));
2246           TREE_TYPE (b->decl) = thistype;
2247           bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true);
2248           return b->decl;
2249         }
2250       else if (TREE_PUBLIC (x))
2251         {
2252           if (visdecl && !b && duplicate_decls (x, visdecl))
2253             {
2254               /* An external declaration at block scope referring to a
2255                  visible entity with internal linkage.  The composite
2256                  type will already be correct for this scope, so we
2257                  just need to fall through to make the declaration in
2258                  this scope.  */
2259               nested = true;
2260               x = visdecl;
2261             }
2262           else
2263             {
2264               bind (name, x, external_scope, /*invisible=*/true,
2265                     /*nested=*/false);
2266               nested = true;
2267             }
2268         }
2269     }
2270
2271   if (TREE_CODE (x) != PARM_DECL)
2272     warn_if_shadowing (x);
2273
2274  skip_external_and_shadow_checks:
2275   if (TREE_CODE (x) == TYPE_DECL)
2276     clone_underlying_type (x);
2277
2278   bind (name, x, scope, /*invisible=*/false, nested);
2279
2280   /* If x's type is incomplete because it's based on a
2281      structure or union which has not yet been fully declared,
2282      attach it to that structure or union type, so we can go
2283      back and complete the variable declaration later, if the
2284      structure or union gets fully declared.
2285
2286      If the input is erroneous, we can have error_mark in the type
2287      slot (e.g. "f(void a, ...)") - that doesn't count as an
2288      incomplete type.  */
2289   if (TREE_TYPE (x) != error_mark_node
2290       && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2291     {
2292       tree element = TREE_TYPE (x);
2293
2294       while (TREE_CODE (element) == ARRAY_TYPE)
2295         element = TREE_TYPE (element);
2296       element = TYPE_MAIN_VARIANT (element);
2297
2298       if ((TREE_CODE (element) == RECORD_TYPE
2299            || TREE_CODE (element) == UNION_TYPE)
2300           && (TREE_CODE (x) != TYPE_DECL
2301               || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
2302           && !COMPLETE_TYPE_P (element))
2303         C_TYPE_INCOMPLETE_VARS (element)
2304           = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
2305     }
2306   return x;
2307 }
2308
2309 /* Record X as belonging to file scope.
2310    This is used only internally by the Objective-C front end,
2311    and is limited to its needs.  duplicate_decls is not called;
2312    if there is any preexisting decl for this identifier, it is an ICE.  */
2313
2314 tree
2315 pushdecl_top_level (tree x)
2316 {
2317   tree name;
2318   bool nested = false;
2319   gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
2320
2321   name = DECL_NAME (x);
2322
2323  gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
2324
2325   if (TREE_PUBLIC (x))
2326     {
2327       bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false);
2328       nested = true;
2329     }
2330   if (file_scope)
2331     bind (name, x, file_scope, /*invisible=*/false, nested);
2332
2333   return x;
2334 }
2335 \f
2336 static void
2337 implicit_decl_warning (tree id, tree olddecl)
2338 {
2339   if (warn_implicit_function_declaration)
2340     {
2341       if (flag_isoc99)
2342         pedwarn (G_("implicit declaration of function %qE"), id);
2343       else 
2344         warning (OPT_Wimplicit_function_declaration, 
2345                  G_("implicit declaration of function %qE"), id);
2346       if (olddecl)
2347         locate_old_decl (olddecl, inform);
2348     }
2349 }
2350
2351 /* Generate an implicit declaration for identifier FUNCTIONID as a
2352    function of type int ().  */
2353
2354 tree
2355 implicitly_declare (tree functionid)
2356 {
2357   struct c_binding *b;
2358   tree decl = 0;
2359   tree asmspec_tree;
2360
2361   for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
2362     {
2363       if (B_IN_SCOPE (b, external_scope))
2364         {
2365           decl = b->decl;
2366           break;
2367         }
2368     }
2369
2370   if (decl)
2371     {
2372       if (decl == error_mark_node)
2373         return decl;
2374
2375       /* FIXME: Objective-C has weird not-really-builtin functions
2376          which are supposed to be visible automatically.  They wind up
2377          in the external scope because they're pushed before the file
2378          scope gets created.  Catch this here and rebind them into the
2379          file scope.  */
2380       if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2381         {
2382           bind (functionid, decl, file_scope,
2383                 /*invisible=*/false, /*nested=*/true);
2384           return decl;
2385         }
2386       else
2387         {
2388           tree newtype = default_function_type;
2389           if (b->type)
2390             TREE_TYPE (decl) = b->type;
2391           /* Implicit declaration of a function already declared
2392              (somehow) in a different scope, or as a built-in.
2393              If this is the first time this has happened, warn;
2394              then recycle the old declaration but with the new type.  */
2395           if (!C_DECL_IMPLICIT (decl))
2396             {
2397               implicit_decl_warning (functionid, decl);
2398               C_DECL_IMPLICIT (decl) = 1;
2399             }
2400           if (DECL_BUILT_IN (decl))
2401             {
2402               newtype = build_type_attribute_variant (newtype,
2403                                                       TYPE_ATTRIBUTES
2404                                                       (TREE_TYPE (decl)));
2405               if (!comptypes (newtype, TREE_TYPE (decl)))
2406                 {
2407                   warning (0, "incompatible implicit declaration of built-in"
2408                            " function %qD", decl);
2409                   newtype = TREE_TYPE (decl);
2410                 }
2411             }
2412           else
2413             {
2414               if (!comptypes (newtype, TREE_TYPE (decl)))
2415                 {
2416                   error ("incompatible implicit declaration of function %qD",
2417                          decl);
2418                   locate_old_decl (decl, error);
2419                 }
2420             }
2421           b->type = TREE_TYPE (decl);
2422           TREE_TYPE (decl) = newtype;
2423           bind (functionid, decl, current_scope,
2424                 /*invisible=*/false, /*nested=*/true);
2425           return decl;
2426         }
2427     }
2428
2429   /* Not seen before.  */
2430   decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2431   DECL_EXTERNAL (decl) = 1;
2432   TREE_PUBLIC (decl) = 1;
2433   C_DECL_IMPLICIT (decl) = 1;
2434   implicit_decl_warning (functionid, 0);
2435   asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
2436   if (asmspec_tree)
2437     set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
2438
2439   /* C89 says implicit declarations are in the innermost block.
2440      So we record the decl in the standard fashion.  */
2441   decl = pushdecl (decl);
2442
2443   /* No need to call objc_check_decl here - it's a function type.  */
2444   rest_of_decl_compilation (decl, 0, 0);
2445
2446   /* Write a record describing this implicit function declaration
2447      to the prototypes file (if requested).  */
2448   gen_aux_info_record (decl, 0, 1, 0);
2449
2450   /* Possibly apply some default attributes to this implicit declaration.  */
2451   decl_attributes (&decl, NULL_TREE, 0);
2452
2453   return decl;
2454 }
2455
2456 /* Issue an error message for a reference to an undeclared variable
2457    ID, including a reference to a builtin outside of function-call
2458    context.  Establish a binding of the identifier to error_mark_node
2459    in an appropriate scope, which will suppress further errors for the
2460    same identifier.  The error message should be given location LOC.  */
2461 void
2462 undeclared_variable (tree id, location_t loc)
2463 {
2464   static bool already = false;
2465   struct c_scope *scope;
2466
2467   if (current_function_decl == 0)
2468     {
2469       error ("%H%qE undeclared here (not in a function)", &loc, id);
2470       scope = current_scope;
2471     }
2472   else
2473     {
2474       error ("%H%qE undeclared (first use in this function)", &loc, id);
2475
2476       if (!already)
2477         {
2478           error ("%H(Each undeclared identifier is reported only once", &loc);
2479           error ("%Hfor each function it appears in.)", &loc);
2480           already = true;
2481         }
2482
2483       /* If we are parsing old-style parameter decls, current_function_decl
2484          will be nonnull but current_function_scope will be null.  */
2485       scope = current_function_scope ? current_function_scope : current_scope;
2486     }
2487   bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false);
2488 }
2489 \f
2490 /* Subroutine of lookup_label, declare_label, define_label: construct a
2491    LABEL_DECL with all the proper frills.  */
2492
2493 static tree
2494 make_label (tree name, location_t location)
2495 {
2496   tree label = build_decl (LABEL_DECL, name, void_type_node);
2497
2498   DECL_CONTEXT (label) = current_function_decl;
2499   DECL_MODE (label) = VOIDmode;
2500   DECL_SOURCE_LOCATION (label) = location;
2501
2502   return label;
2503 }
2504
2505 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2506    Create one if none exists so far for the current function.
2507    This is called when a label is used in a goto expression or
2508    has its address taken.  */
2509
2510 tree
2511 lookup_label (tree name)
2512 {
2513   tree label;
2514
2515   if (current_function_decl == 0)
2516     {
2517       error ("label %qE referenced outside of any function", name);
2518       return 0;
2519     }
2520
2521   /* Use a label already defined or ref'd with this name, but not if
2522      it is inherited from a containing function and wasn't declared
2523      using __label__.  */
2524   label = I_LABEL_DECL (name);
2525   if (label && (DECL_CONTEXT (label) == current_function_decl
2526                 || C_DECLARED_LABEL_FLAG (label)))
2527     {
2528       /* If the label has only been declared, update its apparent
2529          location to point here, for better diagnostics if it
2530          turns out not to have been defined.  */
2531       if (!TREE_USED (label))
2532         DECL_SOURCE_LOCATION (label) = input_location;
2533       return label;
2534     }
2535
2536   /* No label binding for that identifier; make one.  */
2537   label = make_label (name, input_location);
2538
2539   /* Ordinary labels go in the current function scope.  */
2540   bind (name, label, current_function_scope,
2541         /*invisible=*/false, /*nested=*/false);
2542   return label;
2543 }
2544
2545 /* Make a label named NAME in the current function, shadowing silently
2546    any that may be inherited from containing functions or containing
2547    scopes.  This is called for __label__ declarations.  */
2548
2549 tree
2550 declare_label (tree name)
2551 {
2552   struct c_binding *b = I_LABEL_BINDING (name);
2553   tree label;
2554
2555   /* Check to make sure that the label hasn't already been declared
2556      at this scope */
2557   if (b && B_IN_CURRENT_SCOPE (b))
2558     {
2559       error ("duplicate label declaration %qE", name);
2560       locate_old_decl (b->decl, error);
2561
2562       /* Just use the previous declaration.  */
2563       return b->decl;
2564     }
2565
2566   label = make_label (name, input_location);
2567   C_DECLARED_LABEL_FLAG (label) = 1;
2568
2569   /* Declared labels go in the current scope.  */
2570   bind (name, label, current_scope,
2571         /*invisible=*/false, /*nested=*/false);
2572   return label;
2573 }
2574
2575 /* Define a label, specifying the location in the source file.
2576    Return the LABEL_DECL node for the label, if the definition is valid.
2577    Otherwise return 0.  */
2578
2579 tree
2580 define_label (location_t location, tree name)
2581 {
2582   /* Find any preexisting label with this name.  It is an error
2583      if that label has already been defined in this function, or
2584      if there is a containing function with a declared label with
2585      the same name.  */
2586   tree label = I_LABEL_DECL (name);
2587   struct c_label_list *nlist_se, *nlist_vm;
2588
2589   if (label
2590       && ((DECL_CONTEXT (label) == current_function_decl
2591            && DECL_INITIAL (label) != 0)
2592           || (DECL_CONTEXT (label) != current_function_decl
2593               && C_DECLARED_LABEL_FLAG (label))))
2594     {
2595       error ("%Hduplicate label %qD", &location, label);
2596       locate_old_decl (label, error);
2597       return 0;
2598     }
2599   else if (label && DECL_CONTEXT (label) == current_function_decl)
2600     {
2601       /* The label has been used or declared already in this function,
2602          but not defined.  Update its location to point to this
2603          definition.  */
2604       if (C_DECL_UNDEFINABLE_STMT_EXPR (label))
2605         error ("%Jjump into statement expression", label);
2606       if (C_DECL_UNDEFINABLE_VM (label))
2607         error ("%Jjump into scope of identifier with variably modified type",
2608                label);
2609       DECL_SOURCE_LOCATION (label) = location;
2610     }
2611   else
2612     {
2613       /* No label binding for that identifier; make one.  */
2614       label = make_label (name, location);
2615
2616       /* Ordinary labels go in the current function scope.  */
2617       bind (name, label, current_function_scope,
2618             /*invisible=*/false, /*nested=*/false);
2619     }
2620
2621   if (!in_system_header && lookup_name (name))
2622     warning (OPT_Wtraditional, "%Htraditional C lacks a separate namespace "
2623              "for labels, identifier %qE conflicts", &location, name);
2624
2625   nlist_se = XOBNEW (&parser_obstack, struct c_label_list);
2626   nlist_se->next = label_context_stack_se->labels_def;
2627   nlist_se->label = label;
2628   label_context_stack_se->labels_def = nlist_se;
2629
2630   nlist_vm = XOBNEW (&parser_obstack, struct c_label_list);
2631   nlist_vm->next = label_context_stack_vm->labels_def;
2632   nlist_vm->label = label;
2633   label_context_stack_vm->labels_def = nlist_vm;
2634
2635   /* Mark label as having been defined.  */
2636   DECL_INITIAL (label) = error_mark_node;
2637   return label;
2638 }
2639 \f
2640 /* Given NAME, an IDENTIFIER_NODE,
2641    return the structure (or union or enum) definition for that name.
2642    If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2643    CODE says which kind of type the caller wants;
2644    it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2645    If the wrong kind of type is found, an error is reported.  */
2646
2647 static tree
2648 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2649 {
2650   struct c_binding *b = I_TAG_BINDING (name);
2651   int thislevel = 0;
2652
2653   if (!b || !b->decl)
2654     return 0;
2655
2656   /* We only care about whether it's in this level if
2657      thislevel_only was set or it might be a type clash.  */
2658   if (thislevel_only || TREE_CODE (b->decl) != code)
2659     {
2660       /* For our purposes, a tag in the external scope is the same as
2661          a tag in the file scope.  (Primarily relevant to Objective-C
2662          and its builtin structure tags, which get pushed before the
2663          file scope is created.)  */
2664       if (B_IN_CURRENT_SCOPE (b)
2665           || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
2666         thislevel = 1;
2667     }
2668
2669   if (thislevel_only && !thislevel)
2670     return 0;
2671
2672   if (TREE_CODE (b->decl) != code)
2673     {
2674       /* Definition isn't the kind we were looking for.  */
2675       pending_invalid_xref = name;
2676       pending_invalid_xref_location = input_location;
2677
2678       /* If in the same binding level as a declaration as a tag
2679          of a different type, this must not be allowed to
2680          shadow that tag, so give the error immediately.
2681          (For example, "struct foo; union foo;" is invalid.)  */
2682       if (thislevel)
2683         pending_xref_error ();
2684     }
2685   return b->decl;
2686 }
2687
2688 /* Print an error message now
2689    for a recent invalid struct, union or enum cross reference.
2690    We don't print them immediately because they are not invalid
2691    when used in the `struct foo;' construct for shadowing.  */
2692
2693 void
2694 pending_xref_error (void)
2695 {
2696   if (pending_invalid_xref != 0)
2697     error ("%H%qE defined as wrong kind of tag",
2698            &pending_invalid_xref_location, pending_invalid_xref);
2699   pending_invalid_xref = 0;
2700 }
2701
2702 \f
2703 /* Look up NAME in the current scope and its superiors
2704    in the namespace of variables, functions and typedefs.
2705    Return a ..._DECL node of some kind representing its definition,
2706    or return 0 if it is undefined.  */
2707
2708 tree
2709 lookup_name (tree name)
2710 {
2711   struct c_binding *b = I_SYMBOL_BINDING (name);
2712   if (b && !b->invisible)
2713     return b->decl;
2714   return 0;
2715 }
2716
2717 /* Similar to `lookup_name' but look only at the indicated scope.  */
2718
2719 static tree
2720 lookup_name_in_scope (tree name, struct c_scope *scope)
2721 {
2722   struct c_binding *b;
2723
2724   for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2725     if (B_IN_SCOPE (b, scope))
2726       return b->decl;
2727   return 0;
2728 }
2729 \f
2730 /* Create the predefined scalar types of C,
2731    and some nodes representing standard constants (0, 1, (void *) 0).
2732    Initialize the global scope.
2733    Make definitions for built-in primitive functions.  */
2734
2735 void
2736 c_init_decl_processing (void)
2737 {
2738   location_t save_loc = input_location;
2739
2740   /* Initialize reserved words for parser.  */
2741   c_parse_init ();
2742
2743   current_function_decl = 0;
2744
2745   gcc_obstack_init (&parser_obstack);
2746
2747   /* Make the externals scope.  */
2748   push_scope ();
2749   external_scope = current_scope;
2750
2751   /* Declarations from c_common_nodes_and_builtins must not be associated
2752      with this input file, lest we get differences between using and not
2753      using preprocessed headers.  */
2754   input_location = BUILTINS_LOCATION;
2755
2756   build_common_tree_nodes (flag_signed_char, false);
2757
2758   c_common_nodes_and_builtins ();
2759
2760   /* In C, comparisons and TRUTH_* expressions have type int.  */
2761   truthvalue_type_node = integer_type_node;
2762   truthvalue_true_node = integer_one_node;
2763   truthvalue_false_node = integer_zero_node;
2764
2765   /* Even in C99, which has a real boolean type.  */
2766   pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2767                         boolean_type_node));
2768
2769   input_location = save_loc;
2770
2771   pedantic_lvalues = true;
2772
2773   make_fname_decl = c_make_fname_decl;
2774   start_fname_decls ();
2775 }
2776
2777 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2778    decl, NAME is the initialization string and TYPE_DEP indicates whether
2779    NAME depended on the type of the function.  As we don't yet implement
2780    delayed emission of static data, we mark the decl as emitted
2781    so it is not placed in the output.  Anything using it must therefore pull
2782    out the STRING_CST initializer directly.  FIXME.  */
2783
2784 static tree
2785 c_make_fname_decl (tree id, int type_dep)
2786 {
2787   const char *name = fname_as_string (type_dep);
2788   tree decl, type, init;
2789   size_t length = strlen (name);
2790
2791   type = build_array_type (char_type_node,
2792                            build_index_type (size_int (length)));
2793   type = c_build_qualified_type (type, TYPE_QUAL_CONST);
2794
2795   decl = build_decl (VAR_DECL, id, type);
2796
2797   TREE_STATIC (decl) = 1;
2798   TREE_READONLY (decl) = 1;
2799   DECL_ARTIFICIAL (decl) = 1;
2800
2801   init = build_string (length + 1, name);
2802   free (CONST_CAST (char *, name));
2803   TREE_TYPE (init) = type;
2804   DECL_INITIAL (decl) = init;
2805
2806   TREE_USED (decl) = 1;
2807
2808   if (current_function_decl
2809       /* For invalid programs like this:
2810         
2811          void foo()
2812          const char* p = __FUNCTION__;
2813         
2814          the __FUNCTION__ is believed to appear in K&R style function
2815          parameter declarator.  In that case we still don't have
2816          function_scope.  */
2817       && (!errorcount || current_function_scope))
2818     {
2819       DECL_CONTEXT (decl) = current_function_decl;
2820       bind (id, decl, current_function_scope,
2821             /*invisible=*/false, /*nested=*/false);
2822     }
2823
2824   finish_decl (decl, init, NULL_TREE);
2825
2826   return decl;
2827 }
2828
2829 tree
2830 c_builtin_function (tree decl)
2831 {
2832   tree type = TREE_TYPE (decl);
2833   tree   id = DECL_NAME (decl);
2834
2835   const char *name = IDENTIFIER_POINTER (id);
2836   C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0);
2837
2838   /* Should never be called on a symbol with a preexisting meaning.  */
2839   gcc_assert (!I_SYMBOL_BINDING (id));
2840
2841   bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false);
2842
2843   /* Builtins in the implementation namespace are made visible without
2844      needing to be explicitly declared.  See push_file_scope.  */
2845   if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2846     {
2847       TREE_CHAIN (decl) = visible_builtins;
2848       visible_builtins = decl;
2849     }
2850
2851   return decl;
2852 }
2853 \f
2854 /* Called when a declaration is seen that contains no names to declare.
2855    If its type is a reference to a structure, union or enum inherited
2856    from a containing scope, shadow that tag name for the current scope
2857    with a forward reference.
2858    If its type defines a new named structure or union
2859    or defines an enum, it is valid but we need not do anything here.
2860    Otherwise, it is an error.  */
2861
2862 void
2863 shadow_tag (const struct c_declspecs *declspecs)
2864 {
2865   shadow_tag_warned (declspecs, 0);
2866 }
2867
2868 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
2869    but no pedwarn.  */
2870 void
2871 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
2872 {
2873   bool found_tag = false;
2874
2875   if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
2876     {
2877       tree value = declspecs->type;
2878       enum tree_code code = TREE_CODE (value);
2879
2880       if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2881         /* Used to test also that TYPE_SIZE (value) != 0.
2882            That caused warning for `struct foo;' at top level in the file.  */
2883         {
2884           tree name = TYPE_NAME (value);
2885           tree t;
2886
2887           found_tag = true;
2888
2889           if (name == 0)
2890             {
2891               if (warned != 1 && code != ENUMERAL_TYPE)
2892                 /* Empty unnamed enum OK */
2893                 {
2894                   pedwarn ("unnamed struct/union that defines no instances");
2895                   warned = 1;
2896                 }
2897             }
2898           else if (!declspecs->tag_defined_p
2899                    && declspecs->storage_class != csc_none)
2900             {
2901               if (warned != 1)
2902                 pedwarn ("empty declaration with storage class specifier "
2903                          "does not redeclare tag");
2904               warned = 1;
2905               pending_xref_error ();
2906             }
2907           else if (!declspecs->tag_defined_p
2908                    && (declspecs->const_p
2909                        || declspecs->volatile_p
2910                        || declspecs->restrict_p))
2911             {
2912               if (warned != 1)
2913                 pedwarn ("empty declaration with type qualifier "
2914                          "does not redeclare tag");
2915               warned = 1;
2916               pending_xref_error ();
2917             }
2918           else
2919             {
2920               pending_invalid_xref = 0;
2921               t = lookup_tag (code, name, 1);
2922
2923               if (t == 0)
2924                 {
2925                   t = make_node (code);
2926                   pushtag (name, t);
2927                 }
2928             }
2929         }
2930       else
2931         {
2932           if (warned != 1 && !in_system_header)
2933             {
2934               pedwarn ("useless type name in empty declaration");
2935               warned = 1;
2936             }
2937         }
2938     }
2939   else if (warned != 1 && !in_system_header && declspecs->typedef_p)
2940     {
2941       pedwarn ("useless type name in empty declaration");
2942       warned = 1;
2943     }
2944
2945   pending_invalid_xref = 0;
2946
2947   if (declspecs->inline_p)
2948     {
2949       error ("%<inline%> in empty declaration");
2950       warned = 1;
2951     }
2952
2953   if (current_scope == file_scope && declspecs->storage_class == csc_auto)
2954     {
2955       error ("%<auto%> in file-scope empty declaration");
2956       warned = 1;
2957     }
2958
2959   if (current_scope == file_scope && declspecs->storage_class == csc_register)
2960     {
2961       error ("%<register%> in file-scope empty declaration");
2962       warned = 1;
2963     }
2964
2965   if (!warned && !in_system_header && declspecs->storage_class != csc_none)
2966     {
2967       warning (0, "useless storage class specifier in empty declaration");
2968       warned = 2;
2969     }
2970
2971   if (!warned && !in_system_header && declspecs->thread_p)
2972     {
2973       warning (0, "useless %<__thread%> in empty declaration");
2974       warned = 2;
2975     }
2976
2977   if (!warned && !in_system_header && (declspecs->const_p
2978                                        || declspecs->volatile_p
2979                                        || declspecs->restrict_p))
2980     {
2981       warning (0, "useless type qualifier in empty declaration");
2982       warned = 2;
2983     }
2984
2985   if (warned != 1)
2986     {
2987       if (!found_tag)
2988         pedwarn ("empty declaration");
2989     }
2990 }
2991 \f
2992
2993 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
2994    bits.  SPECS represents declaration specifiers that the grammar
2995    only permits to contain type qualifiers and attributes.  */
2996
2997 int
2998 quals_from_declspecs (const struct c_declspecs *specs)
2999 {
3000   int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
3001                | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
3002                | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0));
3003   gcc_assert (!specs->type
3004               && !specs->decl_attr
3005               && specs->typespec_word == cts_none
3006               && specs->storage_class == csc_none
3007               && !specs->typedef_p
3008               && !specs->explicit_signed_p
3009               && !specs->deprecated_p
3010               && !specs->long_p
3011               && !specs->long_long_p
3012               && !specs->short_p
3013               && !specs->signed_p
3014               && !specs->unsigned_p
3015               && !specs->complex_p
3016               && !specs->inline_p
3017               && !specs->thread_p);
3018   return quals;
3019 }
3020
3021 /* Construct an array declarator.  EXPR is the expression inside [],
3022    or NULL_TREE.  QUALS are the type qualifiers inside the [] (to be
3023    applied to the pointer to which a parameter array is converted).
3024    STATIC_P is true if "static" is inside the [], false otherwise.
3025    VLA_UNSPEC_P is true if the array is [*], a VLA of unspecified
3026    length which is nevertheless a complete type, false otherwise.  The
3027    field for the contained declarator is left to be filled in by
3028    set_array_declarator_inner.  */
3029
3030 struct c_declarator *
3031 build_array_declarator (tree expr, struct c_declspecs *quals, bool static_p,
3032                         bool vla_unspec_p)
3033 {
3034   struct c_declarator *declarator = XOBNEW (&parser_obstack,
3035                                             struct c_declarator);
3036   declarator->kind = cdk_array;
3037   declarator->declarator = 0;
3038   declarator->u.array.dimen = expr;
3039   if (quals)
3040     {
3041       declarator->u.array.attrs = quals->attrs;
3042       declarator->u.array.quals = quals_from_declspecs (quals);
3043     }
3044   else
3045     {
3046       declarator->u.array.attrs = NULL_TREE;
3047       declarator->u.array.quals = 0;
3048     }
3049   declarator->u.array.static_p = static_p;
3050   declarator->u.array.vla_unspec_p = vla_unspec_p;
3051   if (pedantic && !flag_isoc99)
3052     {
3053       if (static_p || quals != NULL)
3054         pedwarn ("ISO C90 does not support %<static%> or type "
3055                  "qualifiers in parameter array declarators");
3056       if (vla_unspec_p)
3057         pedwarn ("ISO C90 does not support %<[*]%> array declarators");
3058     }
3059   if (vla_unspec_p)
3060     {
3061       if (!current_scope->parm_flag)
3062         {
3063           /* C99 6.7.5.2p4 */
3064           error ("%<[*]%> not allowed in other than function prototype scope");
3065           declarator->u.array.vla_unspec_p = false;
3066           return NULL;
3067         }
3068       current_scope->had_vla_unspec = true;
3069     }
3070   return declarator;
3071 }
3072
3073 /* Set the contained declarator of an array declarator.  DECL is the
3074    declarator, as constructed by build_array_declarator; INNER is what
3075    appears on the left of the [].  */
3076
3077 struct c_declarator *
3078 set_array_declarator_inner (struct c_declarator *decl,
3079                             struct c_declarator *inner)
3080 {
3081   decl->declarator = inner;
3082   return decl;
3083 }
3084
3085 /* INIT is a constructor that forms DECL's initializer.  If the final
3086    element initializes a flexible array field, add the size of that
3087    initializer to DECL's size.  */
3088
3089 static void
3090 add_flexible_array_elts_to_size (tree decl, tree init)
3091 {
3092   tree elt, type;
3093
3094   if (VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (init)))
3095     return;
3096
3097   elt = VEC_last (constructor_elt, CONSTRUCTOR_ELTS (init))->value;
3098   type = TREE_TYPE (elt);
3099   if (TREE_CODE (type) == ARRAY_TYPE
3100       && TYPE_SIZE (type) == NULL_TREE
3101       && TYPE_DOMAIN (type) != NULL_TREE
3102       && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
3103     {
3104       complete_array_type (&type, elt, false);
3105       DECL_SIZE (decl)
3106         = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
3107       DECL_SIZE_UNIT (decl)
3108         = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
3109     }
3110 }
3111 \f
3112 /* Decode a "typename", such as "int **", returning a ..._TYPE node.  */
3113
3114 tree
3115 groktypename (struct c_type_name *type_name)
3116 {
3117   tree type;
3118   tree attrs = type_name->specs->attrs;
3119
3120   type_name->specs->attrs = NULL_TREE;
3121
3122   type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
3123                          false, NULL, &attrs, DEPRECATED_NORMAL);
3124
3125   /* Apply attributes.  */
3126   decl_attributes (&type, attrs, 0);
3127
3128   return type;
3129 }
3130
3131 /* Decode a declarator in an ordinary declaration or data definition.
3132    This is called as soon as the type information and variable name
3133    have been parsed, before parsing the initializer if any.
3134    Here we create the ..._DECL node, fill in its type,
3135    and put it on the list of decls for the current context.
3136    The ..._DECL node is returned as the value.
3137
3138    Exception: for arrays where the length is not specified,
3139    the type is left null, to be filled in by `finish_decl'.
3140
3141    Function definitions do not come here; they go to start_function
3142    instead.  However, external and forward declarations of functions
3143    do go through here.  Structure field declarations are done by
3144    grokfield and not through here.  */
3145
3146 tree
3147 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
3148             bool initialized, tree attributes)
3149 {
3150   tree decl;
3151   tree tem;
3152   enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
3153
3154   /* An object declared as __attribute__((deprecated)) suppresses
3155      warnings of uses of other deprecated items.  */
3156   if (lookup_attribute ("deprecated", attributes))
3157     deprecated_state = DEPRECATED_SUPPRESS;
3158
3159   decl = grokdeclarator (declarator, declspecs,
3160                          NORMAL, initialized, NULL, &attributes,
3161                          deprecated_state);
3162   if (!decl)
3163     return 0;
3164
3165   if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
3166       && MAIN_NAME_P (DECL_NAME (decl)))
3167     warning (OPT_Wmain, "%q+D is usually a function", decl);
3168
3169   if (initialized)
3170     /* Is it valid for this decl to have an initializer at all?
3171        If not, set INITIALIZED to zero, which will indirectly
3172        tell 'finish_decl' to ignore the initializer once it is parsed.  */
3173     switch (TREE_CODE (decl))
3174       {
3175       case TYPE_DECL:
3176         error ("typedef %qD is initialized (use __typeof__ instead)", decl);
3177         initialized = 0;
3178         break;
3179
3180       case FUNCTION_DECL:
3181         error ("function %qD is initialized like a variable", decl);
3182         initialized = 0;
3183         break;
3184
3185       case PARM_DECL:
3186         /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE.  */
3187         error ("parameter %qD is initialized", decl);
3188         initialized = 0;
3189         break;
3190
3191       default:
3192         /* Don't allow initializations for incomplete types except for
3193            arrays which might be completed by the initialization.  */
3194
3195         /* This can happen if the array size is an undefined macro.
3196            We already gave a warning, so we don't need another one.  */
3197         if (TREE_TYPE (decl) == error_mark_node)
3198           initialized = 0;
3199         else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
3200           {
3201             /* A complete type is ok if size is fixed.  */
3202
3203             if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
3204                 || C_DECL_VARIABLE_SIZE (decl))
3205               {
3206                 error ("variable-sized object may not be initialized");
3207                 initialized = 0;
3208               }
3209           }
3210         else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
3211           {
3212             error ("variable %qD has initializer but incomplete type", decl);
3213             initialized = 0;
3214           }
3215         else if (C_DECL_VARIABLE_SIZE (decl))
3216           {
3217             /* Although C99 is unclear about whether incomplete arrays
3218                of VLAs themselves count as VLAs, it does not make
3219                sense to permit them to be initialized given that
3220                ordinary VLAs may not be initialized.  */
3221             error ("variable-sized object may not be initialized");
3222             initialized = 0;
3223           }
3224       }
3225
3226   if (initialized)
3227     {
3228       if (current_scope == file_scope)
3229         TREE_STATIC (decl) = 1;
3230
3231       /* Tell 'pushdecl' this is an initialized decl
3232          even though we don't yet have the initializer expression.
3233          Also tell 'finish_decl' it may store the real initializer.  */
3234       DECL_INITIAL (decl) = error_mark_node;
3235     }
3236
3237   /* If this is a function declaration, write a record describing it to the
3238      prototypes file (if requested).  */
3239
3240   if (TREE_CODE (decl) == FUNCTION_DECL)
3241     gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
3242
3243   /* ANSI specifies that a tentative definition which is not merged with
3244      a non-tentative definition behaves exactly like a definition with an
3245      initializer equal to zero.  (Section 3.7.2)
3246
3247      -fno-common gives strict ANSI behavior, though this tends to break
3248      a large body of code that grew up without this rule.
3249
3250      Thread-local variables are never common, since there's no entrenched
3251      body of code to break, and it allows more efficient variable references
3252      in the presence of dynamic linking.  */
3253
3254   if (TREE_CODE (decl) == VAR_DECL
3255       && !initialized
3256       && TREE_PUBLIC (decl)
3257       && !DECL_THREAD_LOCAL_P (decl)
3258       && !flag_no_common)
3259     DECL_COMMON (decl) = 1;
3260
3261   /* Set attributes here so if duplicate decl, will have proper attributes.  */
3262   decl_attributes (&decl, attributes, 0);
3263
3264   /* Handle gnu_inline attribute.  */
3265   if (declspecs->inline_p
3266       && !flag_gnu89_inline
3267       && TREE_CODE (decl) == FUNCTION_DECL
3268       && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
3269           || current_function_decl))
3270     {
3271       if (declspecs->storage_class == csc_auto && current_scope != file_scope)
3272         ;
3273       else if (declspecs->storage_class != csc_static)
3274         DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
3275     }
3276
3277   if (TREE_CODE (decl) == FUNCTION_DECL
3278       && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
3279     {
3280       struct c_declarator *ce = declarator;
3281
3282       if (ce->kind == cdk_pointer)
3283         ce = declarator->declarator;
3284       if (ce->kind == cdk_function)
3285         {
3286           tree args = ce->u.arg_info->parms;
3287           for (; args; args = TREE_CHAIN (args))
3288             {
3289               tree type = TREE_TYPE (args);
3290               if (type && INTEGRAL_TYPE_P (type)
3291                   && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
3292                 DECL_ARG_TYPE (args) = integer_type_node;
3293             }
3294         }
3295     }
3296
3297   if (TREE_CODE (decl) == FUNCTION_DECL
3298       && DECL_DECLARED_INLINE_P (decl)
3299       && DECL_UNINLINABLE (decl)
3300       && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
3301     warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
3302              decl);
3303
3304   /* C99 6.7.4p3: An inline definition of a function with external
3305      linkage shall not contain a definition of a modifiable object
3306      with static storage duration...  */
3307   if (TREE_CODE (decl) == VAR_DECL
3308       && current_scope != file_scope
3309       && TREE_STATIC (decl)
3310       && !TREE_READONLY (decl)
3311       && DECL_DECLARED_INLINE_P (current_function_decl)
3312       && DECL_EXTERNAL (current_function_decl))
3313     pedwarn ("%q+D is static but declared in inline function %qD "
3314              "which is not static", decl, current_function_decl);
3315
3316   /* Add this decl to the current scope.
3317      TEM may equal DECL or it may be a previous decl of the same name.  */
3318   tem = pushdecl (decl);
3319
3320   if (initialized && DECL_EXTERNAL (tem))
3321     {
3322       DECL_EXTERNAL (tem) = 0;
3323       TREE_STATIC (tem) = 1;
3324     }
3325
3326   return tem;
3327 }
3328
3329 /* Initialize EH if not initialized yet and exceptions are enabled.  */
3330
3331 void
3332 c_maybe_initialize_eh (void)
3333 {
3334   if (!flag_exceptions || c_eh_initialized_p)
3335     return;
3336
3337   c_eh_initialized_p = true;
3338   eh_personality_libfunc
3339     = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3340                         ? "__gcc_personality_sj0"
3341                         : "__gcc_personality_v0");
3342   default_init_unwind_resume_libfunc ();
3343   using_eh_for_cleanups ();
3344 }
3345
3346 /* Finish processing of a declaration;
3347    install its initial value.
3348    If the length of an array type is not known before,
3349    it must be determined now, from the initial value, or it is an error.  */
3350
3351 void
3352 finish_decl (tree decl, tree init, tree asmspec_tree)
3353 {
3354   tree type;
3355   int was_incomplete = (DECL_SIZE (decl) == 0);
3356   const char *asmspec = 0;
3357
3358   /* If a name was specified, get the string.  */
3359   if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
3360       && DECL_FILE_SCOPE_P (decl))
3361     asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
3362   if (asmspec_tree)
3363     asmspec = TREE_STRING_POINTER (asmspec_tree);
3364
3365   /* If `start_decl' didn't like having an initialization, ignore it now.  */
3366   if (init != 0 && DECL_INITIAL (decl) == 0)
3367     init = 0;
3368
3369   /* Don't crash if parm is initialized.  */
3370   if (TREE_CODE (decl) == PARM_DECL)
3371     init = 0;
3372
3373   if (init)
3374     store_init_value (decl, init);
3375
3376   if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
3377                             || TREE_CODE (decl) == FUNCTION_DECL
3378                             || TREE_CODE (decl) == FIELD_DECL))
3379     objc_check_decl (decl);
3380
3381   type = TREE_TYPE (decl);
3382
3383   /* Deduce size of array from initialization, if not already known.  */
3384   if (TREE_CODE (type) == ARRAY_TYPE
3385       && TYPE_DOMAIN (type) == 0
3386       && TREE_CODE (decl) != TYPE_DECL)
3387     {
3388       bool do_default
3389         = (TREE_STATIC (decl)
3390            /* Even if pedantic, an external linkage array
3391               may have incomplete type at first.  */
3392            ? pedantic && !TREE_PUBLIC (decl)
3393            : !DECL_EXTERNAL (decl));
3394       int failure
3395         = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
3396                                do_default);
3397
3398       /* Get the completed type made by complete_array_type.  */
3399       type = TREE_TYPE (decl);
3400
3401       switch (failure)
3402         {
3403         case 1:
3404           error ("initializer fails to determine size of %q+D", decl);
3405           break;
3406
3407         case 2:
3408           if (do_default)
3409             error ("array size missing in %q+D", decl);
3410           /* If a `static' var's size isn't known,
3411              make it extern as well as static, so it does not get
3412              allocated.
3413              If it is not `static', then do not mark extern;
3414              finish_incomplete_decl will give it a default size
3415              and it will get allocated.  */
3416           else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
3417             DECL_EXTERNAL (decl) = 1;
3418           break;
3419
3420         case 3:
3421           error ("zero or negative size array %q+D", decl);
3422           break;
3423
3424         case 0:
3425           /* For global variables, update the copy of the type that
3426              exists in the binding.  */
3427           if (TREE_PUBLIC (decl))
3428             {
3429               struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
3430               while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
3431                 b_ext = b_ext->shadowed;
3432               if (b_ext)
3433                 {
3434                   if (b_ext->type)
3435                     b_ext->type = composite_type (b_ext->type, type);
3436                   else
3437                     b_ext->type = type;
3438                 }
3439             }
3440           break;
3441
3442         default:
3443           gcc_unreachable ();
3444         }
3445
3446       if (DECL_INITIAL (decl))
3447         TREE_TYPE (DECL_INITIAL (decl)) = type;
3448
3449       layout_decl (decl, 0);
3450     }
3451
3452   if (TREE_CODE (decl) == VAR_DECL)
3453     {
3454       if (init && TREE_CODE (init) == CONSTRUCTOR)
3455         add_flexible_array_elts_to_size (decl, init);
3456
3457       if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
3458           && COMPLETE_TYPE_P (TREE_TYPE (decl)))
3459         layout_decl (decl, 0);
3460
3461       if (DECL_SIZE (decl) == 0
3462           /* Don't give an error if we already gave one earlier.  */
3463           && TREE_TYPE (decl) != error_mark_node
3464           && (TREE_STATIC (decl)
3465               /* A static variable with an incomplete type
3466                  is an error if it is initialized.
3467                  Also if it is not file scope.
3468                  Otherwise, let it through, but if it is not `extern'
3469                  then it may cause an error message later.  */
3470               ? (DECL_INITIAL (decl) != 0
3471                  || !DECL_FILE_SCOPE_P (decl))
3472               /* An automatic variable with an incomplete type
3473                  is an error.  */
3474               : !DECL_EXTERNAL (decl)))
3475          {
3476            error ("storage size of %q+D isn%'t known", decl);
3477            TREE_TYPE (decl) = error_mark_node;
3478          }
3479
3480       if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
3481           && DECL_SIZE (decl) != 0)
3482         {
3483           if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
3484             constant_expression_warning (DECL_SIZE (decl));
3485           else
3486             {
3487               error ("storage size of %q+D isn%'t constant", decl);
3488               TREE_TYPE (decl) = error_mark_node;
3489             }
3490         }
3491
3492       if (TREE_USED (type))
3493         TREE_USED (decl) = 1;
3494     }
3495
3496   /* If this is a function and an assembler name is specified, reset DECL_RTL
3497      so we can give it its new name.  Also, update built_in_decls if it
3498      was a normal built-in.  */
3499   if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
3500     {
3501       if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
3502         set_builtin_user_assembler_name (decl, asmspec);
3503       set_user_assembler_name (decl, asmspec);
3504     }
3505
3506   /* If #pragma weak was used, mark the decl weak now.  */
3507   maybe_apply_pragma_weak (decl);
3508
3509   /* Output the assembler code and/or RTL code for variables and functions,
3510      unless the type is an undefined structure or union.
3511      If not, it will get done when the type is completed.  */
3512
3513   if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3514     {
3515       /* Determine the ELF visibility.  */
3516       if (TREE_PUBLIC (decl))
3517         c_determine_visibility (decl);
3518
3519       /* This is a no-op in c-lang.c or something real in objc-act.c.  */
3520       if (c_dialect_objc ())
3521         objc_check_decl (decl);
3522
3523       if (asmspec)
3524         {
3525           /* If this is not a static variable, issue a warning.
3526              It doesn't make any sense to give an ASMSPEC for an
3527              ordinary, non-register local variable.  Historically,
3528              GCC has accepted -- but ignored -- the ASMSPEC in
3529              this case.  */
3530           if (!DECL_FILE_SCOPE_P (decl)
3531               && TREE_CODE (decl) == VAR_DECL
3532               && !C_DECL_REGISTER (decl)
3533               && !TREE_STATIC (decl))
3534             warning (0, "ignoring asm-specifier for non-static local "
3535                      "variable %q+D", decl);
3536           else
3537             set_user_assembler_name (decl, asmspec);
3538         }
3539
3540       if (DECL_FILE_SCOPE_P (decl))
3541         {
3542           if (DECL_INITIAL (decl) == NULL_TREE
3543               || DECL_INITIAL (decl) == error_mark_node)
3544             /* Don't output anything
3545                when a tentative file-scope definition is seen.
3546                But at end of compilation, do output code for them.  */
3547             DECL_DEFER_OUTPUT (decl) = 1;
3548           rest_of_decl_compilation (decl, true, 0);
3549         }
3550       else
3551         {
3552           /* In conjunction with an ASMSPEC, the `register'
3553              keyword indicates that we should place the variable
3554              in a particular register.  */
3555           if (asmspec && C_DECL_REGISTER (decl))
3556             {
3557               DECL_HARD_REGISTER (decl) = 1;
3558               /* This cannot be done for a structure with volatile
3559                  fields, on which DECL_REGISTER will have been
3560                  reset.  */
3561               if (!DECL_REGISTER (decl))
3562                 error ("cannot put object with volatile field into register");
3563             }
3564
3565           if (TREE_CODE (decl) != FUNCTION_DECL)
3566             {
3567               /* If we're building a variable sized type, and we might be
3568                  reachable other than via the top of the current binding
3569                  level, then create a new BIND_EXPR so that we deallocate
3570                  the object at the right time.  */
3571               /* Note that DECL_SIZE can be null due to errors.  */
3572               if (DECL_SIZE (decl)
3573                   && !TREE_CONSTANT (DECL_SIZE (decl))
3574                   && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
3575                 {
3576                   tree bind;
3577                   bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
3578                   TREE_SIDE_EFFECTS (bind) = 1;
3579                   add_stmt (bind);
3580                   BIND_EXPR_BODY (bind) = push_stmt_list ();
3581                 }
3582               add_stmt (build_stmt (DECL_EXPR, decl));
3583             }
3584         }
3585
3586
3587       if (!DECL_FILE_SCOPE_P (decl))
3588         {
3589           /* Recompute the RTL of a local array now
3590              if it used to be an incomplete type.  */
3591           if (was_incomplete
3592               && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
3593             {
3594               /* If we used it already as memory, it must stay in memory.  */
3595               TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3596               /* If it's still incomplete now, no init will save it.  */
3597               if (DECL_SIZE (decl) == 0)
3598                 DECL_INITIAL (decl) = 0;
3599             }
3600         }
3601     }
3602
3603   /* If this was marked 'used', be sure it will be output.  */
3604   if (!flag_unit_at_a_time && lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
3605     mark_decl_referenced (decl);
3606
3607   if (TREE_CODE (decl) == TYPE_DECL)
3608     {
3609       if (!DECL_FILE_SCOPE_P (decl)
3610           && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3611         add_stmt (build_stmt (DECL_EXPR, decl));
3612
3613       rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
3614     }
3615
3616   /* At the end of a declaration, throw away any variable type sizes
3617      of types defined inside that declaration.  There is no use
3618      computing them in the following function definition.  */
3619   if (current_scope == file_scope)
3620     get_pending_sizes ();
3621
3622   /* Install a cleanup (aka destructor) if one was given.  */
3623   if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
3624     {
3625       tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
3626       if (attr)
3627         {
3628           tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
3629           tree cleanup_decl = lookup_name (cleanup_id);
3630           tree cleanup;
3631
3632           /* Build "cleanup(&decl)" for the destructor.  */
3633           cleanup = build_unary_op (ADDR_EXPR, decl, 0);
3634           cleanup = build_tree_list (NULL_TREE, cleanup);
3635           cleanup = build_function_call (cleanup_decl, cleanup);
3636
3637           /* Don't warn about decl unused; the cleanup uses it.  */
3638           TREE_USED (decl) = 1;
3639           TREE_USED (cleanup_decl) = 1;
3640
3641           /* Initialize EH, if we've been told to do so.  */
3642           c_maybe_initialize_eh ();
3643
3644           push_cleanup (decl, cleanup, false);
3645         }
3646     }
3647 }
3648
3649 /* Given a parsed parameter declaration, decode it into a PARM_DECL.  */
3650
3651 tree
3652 grokparm (const struct c_parm *parm)
3653 {
3654   tree attrs = parm->attrs;
3655   tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
3656                               NULL, &attrs, DEPRECATED_NORMAL);
3657
3658   decl_attributes (&decl, attrs, 0);
3659
3660   return decl;
3661 }
3662
3663 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3664    and push that on the current scope.  */
3665
3666 void
3667 push_parm_decl (const struct c_parm *parm)
3668 {
3669   tree attrs = parm->attrs;
3670   tree decl;
3671
3672   decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
3673                          &attrs, DEPRECATED_NORMAL);
3674   decl_attributes (&decl, attrs, 0);
3675
3676   decl = pushdecl (decl);
3677
3678   finish_decl (decl, NULL_TREE, NULL_TREE);
3679 }
3680
3681 /* Mark all the parameter declarations to date as forward decls.
3682    Also diagnose use of this extension.  */
3683
3684 void
3685 mark_forward_parm_decls (void)
3686 {
3687   struct c_binding *b;
3688
3689   if (pedantic && !current_scope->warned_forward_parm_decls)
3690     {
3691       pedwarn ("ISO C forbids forward parameter declarations");
3692       current_scope->warned_forward_parm_decls = true;
3693     }
3694
3695   for (b = current_scope->bindings; b; b = b->prev)
3696     if (TREE_CODE (b->decl) == PARM_DECL)
3697       TREE_ASM_WRITTEN (b->decl) = 1;
3698 }
3699 \f
3700 /* Build a COMPOUND_LITERAL_EXPR.  TYPE is the type given in the compound
3701    literal, which may be an incomplete array type completed by the
3702    initializer; INIT is a CONSTRUCTOR that initializes the compound
3703    literal.  */
3704
3705 tree
3706 build_compound_literal (tree type, tree init)
3707 {
3708   /* We do not use start_decl here because we have a type, not a declarator;
3709      and do not use finish_decl because the decl should be stored inside
3710      the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR.  */
3711   tree decl;
3712   tree complit;
3713   tree stmt;
3714
3715   if (type == error_mark_node)
3716     return error_mark_node;
3717
3718   decl = build_decl (VAR_DECL, NULL_TREE, type);
3719   DECL_EXTERNAL (decl) = 0;
3720   TREE_PUBLIC (decl) = 0;
3721   TREE_STATIC (decl) = (current_scope == file_scope);
3722   DECL_CONTEXT (decl) = current_function_decl;
3723   TREE_USED (decl) = 1;
3724   TREE_TYPE (decl) = type;
3725   TREE_READONLY (decl) = TYPE_READONLY (type);
3726   store_init_value (decl, init);
3727
3728   if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3729     {
3730       int failure = complete_array_type (&TREE_TYPE (decl),
3731                                          DECL_INITIAL (decl), true);
3732       gcc_assert (!failure);
3733
3734       type = TREE_TYPE (decl);
3735       TREE_TYPE (DECL_INITIAL (decl)) = type;
3736     }
3737
3738   if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3739     return error_mark_node;
3740
3741   stmt = build_stmt (DECL_EXPR, decl);
3742   complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
3743   TREE_SIDE_EFFECTS (complit) = 1;
3744
3745   layout_decl (decl, 0);
3746
3747   if (TREE_STATIC (decl))
3748     {
3749       /* This decl needs a name for the assembler output.  */
3750       set_compound_literal_name (decl);
3751       DECL_DEFER_OUTPUT (decl) = 1;
3752       DECL_COMDAT (decl) = 1;
3753       DECL_ARTIFICIAL (decl) = 1;
3754       DECL_IGNORED_P (decl) = 1;
3755       pushdecl (decl);
3756       rest_of_decl_compilation (decl, 1, 0);
3757     }
3758
3759   return complit;
3760 }
3761 \f
3762 /* Determine whether TYPE is a structure with a flexible array member,
3763    or a union containing such a structure (possibly recursively).  */
3764
3765 static bool
3766 flexible_array_type_p (tree type)
3767 {
3768   tree x;
3769   switch (TREE_CODE (type))
3770     {
3771     case RECORD_TYPE:
3772       x = TYPE_FIELDS (type);
3773       if (x == NULL_TREE)
3774         return false;
3775       while (TREE_CHAIN (x) != NULL_TREE)
3776         x = TREE_CHAIN (x);
3777       if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3778           && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3779           && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3780           && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3781         return true;
3782       return false;
3783     case UNION_TYPE:
3784       for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3785         {
3786           if (flexible_array_type_p (TREE_TYPE (x)))
3787             return true;
3788         }
3789       return false;
3790     default:
3791     return false;
3792   }
3793 }
3794 \f
3795 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3796    replacing with appropriate values if they are invalid.  */
3797 static void
3798 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3799 {
3800   tree type_mv;
3801   unsigned int max_width;
3802   unsigned HOST_WIDE_INT w;
3803   const char *name = orig_name ? orig_name: _("<anonymous>");
3804
3805   /* Detect and ignore out of range field width and process valid
3806      field widths.  */
3807   if (!INTEGRAL_TYPE_P (TREE_TYPE (*width))
3808       || TREE_CODE (*width) != INTEGER_CST)
3809     {
3810       error ("bit-field %qs width not an integer constant", name);
3811       *width = integer_one_node;
3812     }
3813   else
3814     {
3815       constant_expression_warning (*width);
3816       if (tree_int_cst_sgn (*width) < 0)
3817         {
3818           error ("negative width in bit-field %qs", name);
3819           *width = integer_one_node;
3820         }
3821       else if (integer_zerop (*width) && orig_name)
3822         {
3823           error ("zero width for bit-field %qs", name);
3824           *width = integer_one_node;
3825         }
3826     }
3827
3828   /* Detect invalid bit-field type.  */
3829   if (TREE_CODE (*type) != INTEGER_TYPE
3830       && TREE_CODE (*type) != BOOLEAN_TYPE
3831       && TREE_CODE (*type) != ENUMERAL_TYPE)
3832     {
3833       error ("bit-field %qs has invalid type", name);
3834       *type = unsigned_type_node;
3835     }
3836
3837   type_mv = TYPE_MAIN_VARIANT (*type);
3838   if (pedantic
3839       && !in_system_header
3840       && type_mv != integer_type_node
3841       && type_mv != unsigned_type_node
3842       && type_mv != boolean_type_node)
3843     pedwarn ("type of bit-field %qs is a GCC extension", name);
3844
3845   max_width = TYPE_PRECISION (*type);
3846
3847   if (0 < compare_tree_int (*width, max_width))
3848     {
3849       error ("width of %qs exceeds its type", name);
3850       w = max_width;
3851       *width = build_int_cst (NULL_TREE, w);
3852     }
3853   else
3854     w = tree_low_cst (*width, 1);
3855
3856   if (TREE_CODE (*type) == ENUMERAL_TYPE)
3857     {
3858       struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
3859       if (!lt
3860           || w < min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
3861           || w < min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
3862         warning (0, "%qs is narrower than values of its type", name);
3863     }
3864 }
3865
3866 \f
3867
3868 /* Print warning about variable length array if necessary.  */
3869
3870 static void
3871 warn_variable_length_array (const char *name, tree size)
3872 {
3873   int ped = !flag_isoc99 && pedantic && warn_vla != 0;
3874   int const_size = TREE_CONSTANT (size);
3875
3876   if (ped)
3877     {
3878       if (const_size)
3879         {
3880           if (name)
3881             pedwarn ("ISO C90 forbids array %qs whose size "
3882                      "can%'t be evaluated",
3883                      name);
3884           else
3885             pedwarn ("ISO C90 forbids array whose size "
3886                      "can%'t be evaluated");
3887         }
3888       else
3889         {
3890           if (name) 
3891             pedwarn ("ISO C90 forbids variable length array %qs",
3892                      name);
3893           else
3894             pedwarn ("ISO C90 forbids variable length array");
3895         }
3896     }
3897   else if (warn_vla > 0)
3898     {
3899       if (const_size)
3900         {
3901           if (name)
3902             warning (OPT_Wvla,
3903                      "the size of array %qs can"
3904                      "%'t be evaluated", name);
3905           else
3906             warning (OPT_Wvla,
3907                      "the size of array can %'t be evaluated");
3908         }
3909       else
3910         {
3911           if (name)
3912             warning (OPT_Wvla,
3913                      "variable length array %qs is used",
3914                      name);
3915           else
3916             warning (OPT_Wvla,
3917                      "variable length array is used");
3918         }
3919     }
3920 }
3921
3922 /* Given declspecs and a declarator,
3923    determine the name and type of the object declared
3924    and construct a ..._DECL node for it.
3925    (In one case we can return a ..._TYPE node instead.
3926     For invalid input we sometimes return 0.)
3927
3928    DECLSPECS is a c_declspecs structure for the declaration specifiers.
3929
3930    DECL_CONTEXT says which syntactic context this declaration is in:
3931      NORMAL for most contexts.  Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3932      FUNCDEF for a function definition.  Like NORMAL but a few different
3933       error messages in each case.  Return value may be zero meaning
3934       this definition is too screwy to try to parse.
3935      PARM for a parameter declaration (either within a function prototype
3936       or before a function body).  Make a PARM_DECL, or return void_type_node.
3937      TYPENAME if for a typename (in a cast or sizeof).
3938       Don't make a DECL node; just return the ..._TYPE node.
3939      FIELD for a struct or union field; make a FIELD_DECL.
3940    INITIALIZED is true if the decl has an initializer.
3941    WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3942    representing the width of the bit-field.
3943    DECL_ATTRS points to the list of attributes that should be added to this
3944      decl.  Any nested attributes that belong on the decl itself will be
3945      added to this list.
3946    DEPRECATED_STATE is a deprecated_states value indicating whether
3947    deprecation warnings should be suppressed.
3948
3949    In the TYPENAME case, DECLARATOR is really an absolute declarator.
3950    It may also be so in the PARM case, for a prototype where the
3951    argument type is specified but not the name.
3952
3953    This function is where the complicated C meanings of `static'
3954    and `extern' are interpreted.  */
3955
3956 static tree
3957 grokdeclarator (const struct c_declarator *declarator,
3958                 struct c_declspecs *declspecs,
3959                 enum decl_context decl_context, bool initialized, tree *width,
3960                 tree *decl_attrs, enum deprecated_states deprecated_state)
3961 {
3962   tree type = declspecs->type;
3963   bool threadp = declspecs->thread_p;
3964   enum c_storage_class storage_class = declspecs->storage_class;
3965   int constp;
3966   int restrictp;
3967   int volatilep;
3968   int type_quals = TYPE_UNQUALIFIED;
3969   const char *name, *orig_name;
3970   bool funcdef_flag = false;
3971   bool funcdef_syntax = false;
3972   int size_varies = 0;
3973   tree decl_attr = declspecs->decl_attr;
3974   int array_ptr_quals = TYPE_UNQUALIFIED;
3975   tree array_ptr_attrs = NULL_TREE;
3976   int array_parm_static = 0;
3977   bool array_parm_vla_unspec_p = false;
3978   tree returned_attrs = NULL_TREE;
3979   bool bitfield = width != NULL;
3980   tree element_type;
3981   struct c_arg_info *arg_info = 0;
3982
3983   if (decl_context == FUNCDEF)
3984     funcdef_flag = true, decl_context = NORMAL;
3985
3986   /* Look inside a declarator for the name being declared
3987      and get it as a string, for an error message.  */
3988   {
3989     const struct c_declarator *decl = declarator;
3990     name = 0;
3991
3992     while (decl)
3993       switch (decl->kind)
3994         {
3995         case cdk_function:
3996         case cdk_array:
3997         case cdk_pointer:
3998           funcdef_syntax = (decl->kind == cdk_function);
3999           decl = decl->declarator;
4000           break;
4001
4002         case cdk_attrs:
4003           decl = decl->declarator;
4004           break;
4005
4006         case cdk_id:
4007           if (decl->u.id)
4008             name = IDENTIFIER_POINTER (decl->u.id);
4009           decl = 0;
4010           break;
4011
4012         default:
4013           gcc_unreachable ();
4014         }
4015     orig_name = name;
4016     if (name == 0)
4017       name = "type name";
4018   }
4019
4020   /* A function definition's declarator must have the form of
4021      a function declarator.  */
4022
4023   if (funcdef_flag && !funcdef_syntax)
4024     return 0;
4025
4026   /* If this looks like a function definition, make it one,
4027      even if it occurs where parms are expected.
4028      Then store_parm_decls will reject it and not use it as a parm.  */
4029   if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
4030     decl_context = PARM;
4031
4032   if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
4033     warn_deprecated_use (declspecs->type);
4034
4035   if ((decl_context == NORMAL || decl_context == FIELD)
4036       && current_scope == file_scope
4037       && variably_modified_type_p (type, NULL_TREE))
4038     {
4039       error ("variably modified %qs at file scope", name);
4040       type = integer_type_node;
4041     }
4042
4043   size_varies = C_TYPE_VARIABLE_SIZE (type);
4044
4045   /* Diagnose defaulting to "int".  */
4046
4047   if (declspecs->default_int_p && !in_system_header)
4048     {
4049       /* Issue a warning if this is an ISO C 99 program or if
4050          -Wreturn-type and this is a function, or if -Wimplicit;
4051          prefer the former warning since it is more explicit.  */
4052       if ((warn_implicit_int || warn_return_type || flag_isoc99)
4053           && funcdef_flag)
4054         warn_about_return_type = 1;
4055       else if (warn_implicit_int || flag_isoc99)
4056         pedwarn_c99 ("type defaults to %<int%> in declaration of %qs", name);
4057     }
4058
4059   /* Adjust the type if a bit-field is being declared,
4060      -funsigned-bitfields applied and the type is not explicitly
4061      "signed".  */
4062   if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
4063       && TREE_CODE (type) == INTEGER_TYPE)
4064     type = c_common_unsigned_type (type);
4065
4066   /* Figure out the type qualifiers for the declaration.  There are
4067      two ways a declaration can become qualified.  One is something
4068      like `const int i' where the `const' is explicit.  Another is
4069      something like `typedef const int CI; CI i' where the type of the
4070      declaration contains the `const'.  A third possibility is that
4071      there is a type qualifier on the element type of a typedefed
4072      array type, in which case we should extract that qualifier so
4073      that c_apply_type_quals_to_decl receives the full list of
4074      qualifiers to work with (C90 is not entirely clear about whether
4075      duplicate qualifiers should be diagnosed in this case, but it
4076      seems most appropriate to do so).  */
4077   element_type = strip_array_types (type);
4078   constp = declspecs->const_p + TYPE_READONLY (element_type);
4079   restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
4080   volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
4081   if (pedantic && !flag_isoc99)
4082     {
4083       if (constp > 1)
4084         pedwarn ("duplicate %<const%>");
4085       if (restrictp > 1)
4086         pedwarn ("duplicate %<restrict%>");
4087       if (volatilep > 1)
4088         pedwarn ("duplicate %<volatile%>");
4089     }
4090   if (!flag_gen_aux_info && (TYPE_QUALS (element_type)))
4091     type = TYPE_MAIN_VARIANT (type);
4092   type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4093                 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4094                 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4095
4096   /* Warn about storage classes that are invalid for certain
4097      kinds of declarations (parameters, typenames, etc.).  */
4098
4099   if (funcdef_flag
4100       && (threadp
4101           || storage_class == csc_auto
4102           || storage_class == csc_register
4103           || storage_class == csc_typedef))
4104     {
4105       if (storage_class == csc_auto
4106           && (pedantic || current_scope == file_scope))
4107         pedwarn ("function definition declared %<auto%>");
4108       if (storage_class == csc_register)