OSDN Git Service

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