OSDN Git Service

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