]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/c-decl.c
re PR c/4784 (Anonymous structs issues)
[thirdparty/gcc.git] / gcc / c-decl.c
1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 /* Process declarations and symbol lookup for C front end.
23 Also constructs types; the standard scalar types at initialization,
24 and structure, union, array and enum types when they are declared. */
25
26 /* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
28
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "input.h"
33 #include "tm.h"
34 #include "intl.h"
35 #include "tree.h"
36 #include "tree-inline.h"
37 #include "rtl.h"
38 #include "flags.h"
39 #include "function.h"
40 #include "output.h"
41 #include "expr.h"
42 #include "c-tree.h"
43 #include "toplev.h"
44 #include "ggc.h"
45 #include "tm_p.h"
46 #include "cpplib.h"
47 #include "target.h"
48 #include "debug.h"
49 #include "opts.h"
50 #include "timevar.h"
51 #include "c-common.h"
52 #include "c-pragma.h"
53 #include "c-lang.h"
54 #include "langhooks.h"
55 #include "tree-mudflap.h"
56 #include "gimple.h"
57 #include "tree-iterator.h"
58 #include "diagnostic.h"
59 #include "tree-dump.h"
60 #include "cgraph.h"
61 #include "hashtab.h"
62 #include "libfuncs.h"
63 #include "except.h"
64 #include "langhooks-def.h"
65 #include "pointer-set.h"
66 #include "gimple.h"
67 #include "plugin.h"
68
69 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
70 enum decl_context
71 { NORMAL, /* Ordinary declaration */
72 FUNCDEF, /* Function definition */
73 PARM, /* Declaration of parm before function body */
74 FIELD, /* Declaration inside struct or union */
75 TYPENAME}; /* Typename (inside cast or sizeof) */
76
77 /* States indicating how grokdeclarator() should handle declspecs marked
78 with __attribute__((deprecated)). An object declared as
79 __attribute__((deprecated)) suppresses warnings of uses of other
80 deprecated items. */
81
82 enum deprecated_states {
83 DEPRECATED_NORMAL,
84 DEPRECATED_SUPPRESS
85 };
86
87 \f
88 /* Nonzero if we have seen an invalid cross reference
89 to a struct, union, or enum, but not yet printed the message. */
90 tree pending_invalid_xref;
91
92 /* File and line to appear in the eventual error message. */
93 location_t pending_invalid_xref_location;
94
95 /* The file and line that the prototype came from if this is an
96 old-style definition; used for diagnostics in
97 store_parm_decls_oldstyle. */
98
99 static location_t current_function_prototype_locus;
100
101 /* Whether this prototype was built-in. */
102
103 static bool current_function_prototype_built_in;
104
105 /* The argument type information of this prototype. */
106
107 static tree current_function_prototype_arg_types;
108
109 /* The argument information structure for the function currently being
110 defined. */
111
112 static struct c_arg_info *current_function_arg_info;
113
114 /* The obstack on which parser and related data structures, which are
115 not live beyond their top-level declaration or definition, are
116 allocated. */
117 struct obstack parser_obstack;
118
119 /* The current statement tree. */
120
121 static GTY(()) struct stmt_tree_s c_stmt_tree;
122
123 /* State saving variables. */
124 tree c_break_label;
125 tree c_cont_label;
126
127 /* Linked list of TRANSLATION_UNIT_DECLS for the translation units
128 included in this invocation. Note that the current translation
129 unit is not included in this list. */
130
131 static GTY(()) tree all_translation_units;
132
133 /* A list of decls to be made automatically visible in each file scope. */
134 static GTY(()) tree visible_builtins;
135
136 /* Set to 0 at beginning of a function definition, set to 1 if
137 a return statement that specifies a return value is seen. */
138
139 int current_function_returns_value;
140
141 /* Set to 0 at beginning of a function definition, set to 1 if
142 a return statement with no argument is seen. */
143
144 int current_function_returns_null;
145
146 /* Set to 0 at beginning of a function definition, set to 1 if
147 a call to a noreturn function is seen. */
148
149 int current_function_returns_abnormally;
150
151 /* Set to nonzero by `grokdeclarator' for a function
152 whose return type is defaulted, if warnings for this are desired. */
153
154 static int warn_about_return_type;
155
156 /* Nonzero when the current toplevel function contains a declaration
157 of a nested function which is never defined. */
158
159 static bool undef_nested_function;
160
161 /* True means global_bindings_p should return false even if the scope stack
162 says we are in file scope. */
163 bool c_override_global_bindings_to_false;
164
165 \f
166 /* Each c_binding structure describes one binding of an identifier to
167 a decl. All the decls in a scope - irrespective of namespace - are
168 chained together by the ->prev field, which (as the name implies)
169 runs in reverse order. All the decls in a given namespace bound to
170 a given identifier are chained by the ->shadowed field, which runs
171 from inner to outer scopes.
172
173 The ->decl field usually points to a DECL node, but there are two
174 exceptions. In the namespace of type tags, the bound entity is a
175 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
176 identifier is encountered, it is bound to error_mark_node to
177 suppress further errors about that identifier in the current
178 function.
179
180 The ->u.type field stores the type of the declaration in this scope;
181 if NULL, the type is the type of the ->decl field. This is only of
182 relevance for objects with external or internal linkage which may
183 be redeclared in inner scopes, forming composite types that only
184 persist for the duration of those scopes. In the external scope,
185 this stores the composite of all the types declared for this
186 object, visible or not. The ->inner_comp field (used only at file
187 scope) stores whether an incomplete array type at file scope was
188 completed at an inner scope to an array size other than 1.
189
190 The ->u.label field is used for labels. It points to a structure
191 which stores additional information used for warnings.
192
193 The depth field is copied from the scope structure that holds this
194 decl. It is used to preserve the proper ordering of the ->shadowed
195 field (see bind()) and also for a handful of special-case checks.
196 Finally, the invisible bit is true for a decl which should be
197 ignored for purposes of normal name lookup, and the nested bit is
198 true for a decl that's been bound a second time in an inner scope;
199 in all such cases, the binding in the outer scope will have its
200 invisible bit true. */
201
202 struct GTY((chain_next ("%h.prev"))) c_binding {
203 union GTY(()) { /* first so GTY desc can use decl */
204 tree GTY((tag ("0"))) type; /* the type in this scope */
205 struct c_label_vars * GTY((tag ("1"))) label; /* for warnings */
206 } GTY((desc ("TREE_CODE (%0.decl) == LABEL_DECL"))) u;
207 tree decl; /* the decl bound */
208 tree id; /* the identifier it's bound to */
209 struct c_binding *prev; /* the previous decl in this scope */
210 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
211 unsigned int depth : 28; /* depth of this scope */
212 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
213 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
214 BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
215 BOOL_BITFIELD in_struct : 1; /* currently defined as struct field */
216 location_t locus; /* location for nested bindings */
217 };
218 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
219 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
220 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
221 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
222
223 #define I_SYMBOL_BINDING(node) \
224 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->symbol_binding)
225 #define I_SYMBOL_DECL(node) \
226 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
227
228 #define I_TAG_BINDING(node) \
229 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->tag_binding)
230 #define I_TAG_DECL(node) \
231 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
232
233 #define I_LABEL_BINDING(node) \
234 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->label_binding)
235 #define I_LABEL_DECL(node) \
236 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
237
238 /* Each C symbol points to three linked lists of c_binding structures.
239 These describe the values of the identifier in the three different
240 namespaces defined by the language. */
241
242 struct GTY(()) lang_identifier {
243 struct c_common_identifier common_id;
244 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
245 struct c_binding *tag_binding; /* struct/union/enum tags */
246 struct c_binding *label_binding; /* labels */
247 };
248
249 /* Validate c-lang.c's assumptions. */
250 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
251 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
252
253 /* The resulting tree type. */
254
255 union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
256 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))"))) lang_tree_node
257 {
258 union tree_node GTY ((tag ("0"),
259 desc ("tree_node_structure (&%h)")))
260 generic;
261 struct lang_identifier GTY ((tag ("1"))) identifier;
262 };
263
264 /* Track bindings and other things that matter for goto warnings. For
265 efficiency, we do not gather all the decls at the point of
266 definition. Instead, we point into the bindings structure. As
267 scopes are popped, we update these structures and gather the decls
268 that matter at that time. */
269
270 struct GTY(()) c_spot_bindings {
271 /* The currently open scope which holds bindings defined when the
272 label was defined or the goto statement was found. */
273 struct c_scope *scope;
274 /* The bindings in the scope field which were defined at the point
275 of the label or goto. This lets us look at older or newer
276 bindings in the scope, as appropriate. */
277 struct c_binding *bindings_in_scope;
278 /* The number of statement expressions that have started since this
279 label or goto statement was defined. This is zero if we are at
280 the same statement expression level. It is positive if we are in
281 a statement expression started since this spot. It is negative
282 if this spot was in a statement expression and we have left
283 it. */
284 int stmt_exprs;
285 /* Whether we started in a statement expression but are no longer in
286 it. This is set to true if stmt_exprs ever goes negative. */
287 bool left_stmt_expr;
288 };
289
290 /* This structure is used to keep track of bindings seen when a goto
291 statement is defined. This is only used if we see the goto
292 statement before we see the label. */
293
294 struct GTY(()) c_goto_bindings {
295 /* The location of the goto statement. */
296 location_t loc;
297 /* The bindings of the goto statement. */
298 struct c_spot_bindings goto_bindings;
299 };
300
301 typedef struct c_goto_bindings *c_goto_bindings_p;
302 DEF_VEC_P(c_goto_bindings_p);
303 DEF_VEC_ALLOC_P(c_goto_bindings_p,gc);
304
305 /* The additional information we keep track of for a label binding.
306 These fields are updated as scopes are popped. */
307
308 struct GTY(()) c_label_vars {
309 /* The shadowed c_label_vars, when one label shadows another (which
310 can only happen using a __label__ declaration). */
311 struct c_label_vars *shadowed;
312 /* The bindings when the label was defined. */
313 struct c_spot_bindings label_bindings;
314 /* A list of decls that we care about: decls about which we should
315 warn if a goto branches to this label from later in the function.
316 Decls are added to this list as scopes are popped. We only add
317 the decls that matter. */
318 VEC(tree,gc) *decls_in_scope;
319 /* A list of goto statements to this label. This is only used for
320 goto statements seen before the label was defined, so that we can
321 issue appropriate warnings for them. */
322 VEC(c_goto_bindings_p,gc) *gotos;
323 };
324
325 /* Each c_scope structure describes the complete contents of one
326 scope. Four scopes are distinguished specially: the innermost or
327 current scope, the innermost function scope, the file scope (always
328 the second to outermost) and the outermost or external scope.
329
330 Most declarations are recorded in the current scope.
331
332 All normal label declarations are recorded in the innermost
333 function scope, as are bindings of undeclared identifiers to
334 error_mark_node. (GCC permits nested functions as an extension,
335 hence the 'innermost' qualifier.) Explicitly declared labels
336 (using the __label__ extension) appear in the current scope.
337
338 Being in the file scope (current_scope == file_scope) causes
339 special behavior in several places below. Also, under some
340 conditions the Objective-C front end records declarations in the
341 file scope even though that isn't the current scope.
342
343 All declarations with external linkage are recorded in the external
344 scope, even if they aren't visible there; this models the fact that
345 such declarations are visible to the entire program, and (with a
346 bit of cleverness, see pushdecl) allows diagnosis of some violations
347 of C99 6.2.2p7 and 6.2.7p2:
348
349 If, within the same translation unit, the same identifier appears
350 with both internal and external linkage, the behavior is
351 undefined.
352
353 All declarations that refer to the same object or function shall
354 have compatible type; otherwise, the behavior is undefined.
355
356 Initially only the built-in declarations, which describe compiler
357 intrinsic functions plus a subset of the standard library, are in
358 this scope.
359
360 The order of the blocks list matters, and it is frequently appended
361 to. To avoid having to walk all the way to the end of the list on
362 each insertion, or reverse the list later, we maintain a pointer to
363 the last list entry. (FIXME: It should be feasible to use a reversed
364 list here.)
365
366 The bindings list is strictly in reverse order of declarations;
367 pop_scope relies on this. */
368
369
370 struct GTY((chain_next ("%h.outer"))) c_scope {
371 /* The scope containing this one. */
372 struct c_scope *outer;
373
374 /* The next outermost function scope. */
375 struct c_scope *outer_function;
376
377 /* All bindings in this scope. */
378 struct c_binding *bindings;
379
380 /* For each scope (except the global one), a chain of BLOCK nodes
381 for all the scopes that were entered and exited one level down. */
382 tree blocks;
383 tree blocks_last;
384
385 /* The depth of this scope. Used to keep the ->shadowed chain of
386 bindings sorted innermost to outermost. */
387 unsigned int depth : 28;
388
389 /* True if we are currently filling this scope with parameter
390 declarations. */
391 BOOL_BITFIELD parm_flag : 1;
392
393 /* True if we saw [*] in this scope. Used to give an error messages
394 if these appears in a function definition. */
395 BOOL_BITFIELD had_vla_unspec : 1;
396
397 /* True if we already complained about forward parameter decls
398 in this scope. This prevents double warnings on
399 foo (int a; int b; ...) */
400 BOOL_BITFIELD warned_forward_parm_decls : 1;
401
402 /* True if this is the outermost block scope of a function body.
403 This scope contains the parameters, the local variables declared
404 in the outermost block, and all the labels (except those in
405 nested functions, or declared at block scope with __label__). */
406 BOOL_BITFIELD function_body : 1;
407
408 /* True means make a BLOCK for this scope no matter what. */
409 BOOL_BITFIELD keep : 1;
410
411 /* True means that an unsuffixed float constant is _Decimal64. */
412 BOOL_BITFIELD float_const_decimal64 : 1;
413
414 /* True if this scope has any label bindings. This is used to speed
415 up searching for labels when popping scopes, particularly since
416 labels are normally only found at function scope. */
417 BOOL_BITFIELD has_label_bindings : 1;
418 };
419
420 /* The scope currently in effect. */
421
422 static GTY(()) struct c_scope *current_scope;
423
424 /* The innermost function scope. Ordinary (not explicitly declared)
425 labels, bindings to error_mark_node, and the lazily-created
426 bindings of __func__ and its friends get this scope. */
427
428 static GTY(()) struct c_scope *current_function_scope;
429
430 /* The C file scope. This is reset for each input translation unit. */
431
432 static GTY(()) struct c_scope *file_scope;
433
434 /* The outermost scope. This is used for all declarations with
435 external linkage, and only these, hence the name. */
436
437 static GTY(()) struct c_scope *external_scope;
438
439 /* A chain of c_scope structures awaiting reuse. */
440
441 static GTY((deletable)) struct c_scope *scope_freelist;
442
443 /* A chain of c_binding structures awaiting reuse. */
444
445 static GTY((deletable)) struct c_binding *binding_freelist;
446
447 /* Append VAR to LIST in scope SCOPE. */
448 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
449 struct c_scope *s_ = (scope); \
450 tree d_ = (decl); \
451 if (s_->list##_last) \
452 BLOCK_CHAIN (s_->list##_last) = d_; \
453 else \
454 s_->list = d_; \
455 s_->list##_last = d_; \
456 } while (0)
457
458 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
459 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
460 struct c_scope *t_ = (tscope); \
461 struct c_scope *f_ = (fscope); \
462 if (t_->to##_last) \
463 BLOCK_CHAIN (t_->to##_last) = f_->from; \
464 else \
465 t_->to = f_->from; \
466 t_->to##_last = f_->from##_last; \
467 } while (0)
468
469 /* A c_inline_static structure stores details of a static identifier
470 referenced in a definition of a function that may be an inline
471 definition if no subsequent declaration of that function uses
472 "extern" or does not use "inline". */
473
474 struct GTY((chain_next ("%h.next"))) c_inline_static {
475 /* The location for a diagnostic. */
476 location_t location;
477
478 /* The function that may be an inline definition. */
479 tree function;
480
481 /* The object or function referenced. */
482 tree static_decl;
483
484 /* What sort of reference this is. */
485 enum c_inline_static_type type;
486
487 /* The next such structure or NULL. */
488 struct c_inline_static *next;
489 };
490
491 /* List of static identifiers used or referenced in functions that may
492 be inline definitions. */
493 static GTY(()) struct c_inline_static *c_inline_statics;
494
495 /* True means unconditionally make a BLOCK for the next scope pushed. */
496
497 static bool keep_next_level_flag;
498
499 /* True means the next call to push_scope will be the outermost scope
500 of a function body, so do not push a new scope, merely cease
501 expecting parameter decls. */
502
503 static bool next_is_function_body;
504
505 /* A VEC of pointers to c_binding structures. */
506
507 typedef struct c_binding *c_binding_ptr;
508 DEF_VEC_P(c_binding_ptr);
509 DEF_VEC_ALLOC_P(c_binding_ptr,heap);
510
511 /* Information that we keep for a struct or union while it is being
512 parsed. */
513
514 struct c_struct_parse_info
515 {
516 /* If warn_cxx_compat, a list of types defined within this
517 struct. */
518 VEC(tree,heap) *struct_types;
519 /* If warn_cxx_compat, a list of field names which have bindings,
520 and which are defined in this struct, but which are not defined
521 in any enclosing struct. This is used to clear the in_struct
522 field of the c_bindings structure. */
523 VEC(c_binding_ptr,heap) *fields;
524 /* If warn_cxx_compat, a list of typedef names used when defining
525 fields in this struct. */
526 VEC(tree,heap) *typedefs_seen;
527 };
528
529 /* Information for the struct or union currently being parsed, or
530 NULL if not parsing a struct or union. */
531 static struct c_struct_parse_info *struct_parse_info;
532
533 /* Forward declarations. */
534 static tree lookup_name_in_scope (tree, struct c_scope *);
535 static tree c_make_fname_decl (location_t, tree, int);
536 static tree grokdeclarator (const struct c_declarator *,
537 struct c_declspecs *,
538 enum decl_context, bool, tree *, tree *, tree *,
539 bool *, enum deprecated_states);
540 static tree grokparms (struct c_arg_info *, bool);
541 static void layout_array_type (tree);
542 \f
543 /* T is a statement. Add it to the statement-tree. This is the
544 C/ObjC version--C++ has a slightly different version of this
545 function. */
546
547 tree
548 add_stmt (tree t)
549 {
550 enum tree_code code = TREE_CODE (t);
551
552 if (CAN_HAVE_LOCATION_P (t) && code != LABEL_EXPR)
553 {
554 if (!EXPR_HAS_LOCATION (t))
555 SET_EXPR_LOCATION (t, input_location);
556 }
557
558 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
559 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
560
561 /* Add T to the statement-tree. Non-side-effect statements need to be
562 recorded during statement expressions. */
563 append_to_statement_list_force (t, &cur_stmt_list);
564
565 return t;
566 }
567 \f
568
569 void
570 c_print_identifier (FILE *file, tree node, int indent)
571 {
572 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
573 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
574 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
575 if (C_IS_RESERVED_WORD (node) && C_RID_CODE (node) != RID_CXX_COMPAT_WARN)
576 {
577 tree rid = ridpointers[C_RID_CODE (node)];
578 indent_to (file, indent + 4);
579 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
580 (void *) rid, IDENTIFIER_POINTER (rid));
581 }
582 }
583
584 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
585 which may be any of several kinds of DECL or TYPE or error_mark_node,
586 in the scope SCOPE. */
587 static void
588 bind (tree name, tree decl, struct c_scope *scope, bool invisible,
589 bool nested, location_t locus)
590 {
591 struct c_binding *b, **here;
592
593 if (binding_freelist)
594 {
595 b = binding_freelist;
596 binding_freelist = b->prev;
597 }
598 else
599 b = GGC_NEW (struct c_binding);
600
601 b->shadowed = 0;
602 b->decl = decl;
603 b->id = name;
604 b->depth = scope->depth;
605 b->invisible = invisible;
606 b->nested = nested;
607 b->inner_comp = 0;
608 b->in_struct = 0;
609 b->locus = locus;
610
611 b->u.type = NULL;
612
613 b->prev = scope->bindings;
614 scope->bindings = b;
615
616 if (!name)
617 return;
618
619 switch (TREE_CODE (decl))
620 {
621 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
622 case ENUMERAL_TYPE:
623 case UNION_TYPE:
624 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
625 case VAR_DECL:
626 case FUNCTION_DECL:
627 case TYPE_DECL:
628 case CONST_DECL:
629 case PARM_DECL:
630 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
631
632 default:
633 gcc_unreachable ();
634 }
635
636 /* Locate the appropriate place in the chain of shadowed decls
637 to insert this binding. Normally, scope == current_scope and
638 this does nothing. */
639 while (*here && (*here)->depth > scope->depth)
640 here = &(*here)->shadowed;
641
642 b->shadowed = *here;
643 *here = b;
644 }
645
646 /* Clear the binding structure B, stick it on the binding_freelist,
647 and return the former value of b->prev. This is used by pop_scope
648 and get_parm_info to iterate destructively over all the bindings
649 from a given scope. */
650 static struct c_binding *
651 free_binding_and_advance (struct c_binding *b)
652 {
653 struct c_binding *prev = b->prev;
654
655 memset (b, 0, sizeof (struct c_binding));
656 b->prev = binding_freelist;
657 binding_freelist = b;
658
659 return prev;
660 }
661
662 /* Bind a label. Like bind, but skip fields which aren't used for
663 labels, and add the LABEL_VARS value. */
664 static void
665 bind_label (tree name, tree label, struct c_scope *scope,
666 struct c_label_vars *label_vars)
667 {
668 struct c_binding *b;
669
670 bind (name, label, scope, /*invisible=*/false, /*nested=*/false,
671 UNKNOWN_LOCATION);
672
673 scope->has_label_bindings = true;
674
675 b = scope->bindings;
676 gcc_assert (b->decl == label);
677 label_vars->shadowed = b->u.label;
678 b->u.label = label_vars;
679 }
680 \f
681 /* Hook called at end of compilation to assume 1 elt
682 for a file-scope tentative array defn that wasn't complete before. */
683
684 void
685 c_finish_incomplete_decl (tree decl)
686 {
687 if (TREE_CODE (decl) == VAR_DECL)
688 {
689 tree type = TREE_TYPE (decl);
690 if (type != error_mark_node
691 && TREE_CODE (type) == ARRAY_TYPE
692 && !DECL_EXTERNAL (decl)
693 && TYPE_DOMAIN (type) == 0)
694 {
695 warning_at (DECL_SOURCE_LOCATION (decl),
696 0, "array %q+D assumed to have one element", decl);
697
698 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
699
700 layout_decl (decl, 0);
701 }
702 }
703 }
704 \f
705 /* Record that inline function FUNC contains a reference (location
706 LOC) to static DECL (file-scope or function-local according to
707 TYPE). */
708
709 void
710 record_inline_static (location_t loc, tree func, tree decl,
711 enum c_inline_static_type type)
712 {
713 struct c_inline_static *csi = GGC_NEW (struct c_inline_static);
714 csi->location = loc;
715 csi->function = func;
716 csi->static_decl = decl;
717 csi->type = type;
718 csi->next = c_inline_statics;
719 c_inline_statics = csi;
720 }
721
722 /* Check for references to static declarations in inline functions at
723 the end of the translation unit and diagnose them if the functions
724 are still inline definitions. */
725
726 static void
727 check_inline_statics (void)
728 {
729 struct c_inline_static *csi;
730 for (csi = c_inline_statics; csi; csi = csi->next)
731 {
732 if (DECL_EXTERNAL (csi->function))
733 switch (csi->type)
734 {
735 case csi_internal:
736 pedwarn (csi->location, 0,
737 "%qD is static but used in inline function %qD "
738 "which is not static", csi->static_decl, csi->function);
739 break;
740 case csi_modifiable:
741 pedwarn (csi->location, 0,
742 "%q+D is static but declared in inline function %qD "
743 "which is not static", csi->static_decl, csi->function);
744 break;
745 default:
746 gcc_unreachable ();
747 }
748 }
749 c_inline_statics = NULL;
750 }
751 \f
752 /* Fill in a c_spot_bindings structure. If DEFINING is true, set it
753 for the current state, otherwise set it to uninitialized. */
754
755 static void
756 set_spot_bindings (struct c_spot_bindings *p, bool defining)
757 {
758 if (defining)
759 {
760 p->scope = current_scope;
761 p->bindings_in_scope = current_scope->bindings;
762 }
763 else
764 {
765 p->scope = NULL;
766 p->bindings_in_scope = NULL;
767 }
768 p->stmt_exprs = 0;
769 p->left_stmt_expr = false;
770 }
771
772 /* Return true if we will want to say something if a goto statement
773 crosses DECL. */
774
775 static bool
776 decl_jump_unsafe (tree decl)
777 {
778 if (decl == error_mark_node || TREE_TYPE (decl) == error_mark_node)
779 return false;
780
781 /* Always warn about crossing variably modified types. */
782 if ((TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == TYPE_DECL)
783 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
784 return true;
785
786 /* Otherwise, only warn if -Wgoto-misses-init and this is an
787 initialized automatic decl. */
788 if (warn_jump_misses_init
789 && TREE_CODE (decl) == VAR_DECL
790 && !TREE_STATIC (decl)
791 && DECL_INITIAL (decl) != NULL_TREE)
792 return true;
793
794 return false;
795 }
796
797 /* Update spot bindings P as we pop out of SCOPE. Return true if we
798 should push decls for a label. */
799
800 static bool
801 update_spot_bindings (struct c_scope *scope, struct c_spot_bindings *p)
802 {
803 if (p->scope != scope)
804 {
805 /* This label or goto is defined in some other scope, or it is a
806 label which is not yet defined. There is nothing to
807 update. */
808 return false;
809 }
810
811 /* Adjust the spot bindings to refer to the bindings already defined
812 in the enclosing scope. */
813 p->scope = scope->outer;
814 p->bindings_in_scope = p->scope->bindings;
815
816 return true;
817 }
818 \f
819 /* The Objective-C front-end often needs to determine the current scope. */
820
821 void *
822 objc_get_current_scope (void)
823 {
824 return current_scope;
825 }
826
827 /* The following function is used only by Objective-C. It needs to live here
828 because it accesses the innards of c_scope. */
829
830 void
831 objc_mark_locals_volatile (void *enclosing_blk)
832 {
833 struct c_scope *scope;
834 struct c_binding *b;
835
836 for (scope = current_scope;
837 scope && scope != enclosing_blk;
838 scope = scope->outer)
839 {
840 for (b = scope->bindings; b; b = b->prev)
841 objc_volatilize_decl (b->decl);
842
843 /* Do not climb up past the current function. */
844 if (scope->function_body)
845 break;
846 }
847 }
848
849 /* Nonzero if we are currently in file scope. */
850
851 int
852 global_bindings_p (void)
853 {
854 return (current_scope == file_scope && !c_override_global_bindings_to_false
855 ? -1
856 : 0);
857 }
858
859 void
860 keep_next_level (void)
861 {
862 keep_next_level_flag = true;
863 }
864
865 /* Set the flag for the FLOAT_CONST_DECIMAL64 pragma being ON. */
866
867 void
868 set_float_const_decimal64 (void)
869 {
870 current_scope->float_const_decimal64 = true;
871 }
872
873 /* Clear the flag for the FLOAT_CONST_DECIMAL64 pragma. */
874
875 void
876 clear_float_const_decimal64 (void)
877 {
878 current_scope->float_const_decimal64 = false;
879 }
880
881 /* Return nonzero if an unsuffixed float constant is _Decimal64. */
882
883 bool
884 float_const_decimal64_p (void)
885 {
886 return current_scope->float_const_decimal64;
887 }
888
889 /* Identify this scope as currently being filled with parameters. */
890
891 void
892 declare_parm_level (void)
893 {
894 current_scope->parm_flag = true;
895 }
896
897 void
898 push_scope (void)
899 {
900 if (next_is_function_body)
901 {
902 /* This is the transition from the parameters to the top level
903 of the function body. These are the same scope
904 (C99 6.2.1p4,6) so we do not push another scope structure.
905 next_is_function_body is set only by store_parm_decls, which
906 in turn is called when and only when we are about to
907 encounter the opening curly brace for the function body.
908
909 The outermost block of a function always gets a BLOCK node,
910 because the debugging output routines expect that each
911 function has at least one BLOCK. */
912 current_scope->parm_flag = false;
913 current_scope->function_body = true;
914 current_scope->keep = true;
915 current_scope->outer_function = current_function_scope;
916 current_function_scope = current_scope;
917
918 keep_next_level_flag = false;
919 next_is_function_body = false;
920
921 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
922 if (current_scope->outer)
923 current_scope->float_const_decimal64
924 = current_scope->outer->float_const_decimal64;
925 else
926 current_scope->float_const_decimal64 = false;
927 }
928 else
929 {
930 struct c_scope *scope;
931 if (scope_freelist)
932 {
933 scope = scope_freelist;
934 scope_freelist = scope->outer;
935 }
936 else
937 scope = GGC_CNEW (struct c_scope);
938
939 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
940 if (current_scope)
941 scope->float_const_decimal64 = current_scope->float_const_decimal64;
942 else
943 scope->float_const_decimal64 = false;
944
945 scope->keep = keep_next_level_flag;
946 scope->outer = current_scope;
947 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
948
949 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
950 possible. */
951 if (current_scope && scope->depth == 0)
952 {
953 scope->depth--;
954 sorry ("GCC supports only %u nested scopes", scope->depth);
955 }
956
957 current_scope = scope;
958 keep_next_level_flag = false;
959 }
960 }
961
962 /* This is called when we are leaving SCOPE. For each label defined
963 in SCOPE, add any appropriate decls to its decls_in_scope fields.
964 These are the decls whose initialization will be skipped by a goto
965 later in the function. */
966
967 static void
968 update_label_decls (struct c_scope *scope)
969 {
970 struct c_scope *s;
971
972 s = scope;
973 while (s != NULL)
974 {
975 if (s->has_label_bindings)
976 {
977 struct c_binding *b;
978
979 for (b = s->bindings; b != NULL; b = b->prev)
980 {
981 struct c_label_vars *label_vars;
982 struct c_binding *b1;
983 unsigned int ix;
984 struct c_goto_bindings *g;
985
986 if (TREE_CODE (b->decl) != LABEL_DECL)
987 continue;
988 label_vars = b->u.label;
989
990 b1 = label_vars->label_bindings.bindings_in_scope;
991 if (update_spot_bindings (scope, &label_vars->label_bindings))
992 {
993 /* This label is defined in this scope. */
994 for (; b1 != NULL; b1 = b1->prev)
995 {
996 /* A goto from later in the function to this
997 label will never see the initialization of
998 B1, if any. Save it to issue a warning if
999 needed. */
1000 if (decl_jump_unsafe (b1->decl))
1001 VEC_safe_push (tree, gc, label_vars->decls_in_scope,
1002 b1->decl);
1003 }
1004 }
1005
1006 /* Update the bindings of any goto statements associated
1007 with this label. */
1008 for (ix = 0;
1009 VEC_iterate (c_goto_bindings_p, label_vars->gotos, ix, g);
1010 ++ix)
1011 update_spot_bindings (scope, &g->goto_bindings);
1012 }
1013 }
1014
1015 /* Don't search beyond the current function. */
1016 if (s == current_function_scope)
1017 break;
1018
1019 s = s->outer;
1020 }
1021 }
1022
1023 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
1024
1025 static void
1026 set_type_context (tree type, tree context)
1027 {
1028 for (type = TYPE_MAIN_VARIANT (type); type;
1029 type = TYPE_NEXT_VARIANT (type))
1030 TYPE_CONTEXT (type) = context;
1031 }
1032
1033 /* Exit a scope. Restore the state of the identifier-decl mappings
1034 that were in effect when this scope was entered. Return a BLOCK
1035 node containing all the DECLs in this scope that are of interest
1036 to debug info generation. */
1037
1038 tree
1039 pop_scope (void)
1040 {
1041 struct c_scope *scope = current_scope;
1042 tree block, context, p;
1043 struct c_binding *b;
1044
1045 bool functionbody = scope->function_body;
1046 bool keep = functionbody || scope->keep || scope->bindings;
1047
1048 update_label_decls (scope);
1049
1050 /* If appropriate, create a BLOCK to record the decls for the life
1051 of this function. */
1052 block = 0;
1053 if (keep)
1054 {
1055 block = make_node (BLOCK);
1056 BLOCK_SUBBLOCKS (block) = scope->blocks;
1057 TREE_USED (block) = 1;
1058
1059 /* In each subblock, record that this is its superior. */
1060 for (p = scope->blocks; p; p = BLOCK_CHAIN (p))
1061 BLOCK_SUPERCONTEXT (p) = block;
1062
1063 BLOCK_VARS (block) = 0;
1064 }
1065
1066 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
1067 scope must be set so that they point to the appropriate
1068 construct, i.e. either to the current FUNCTION_DECL node, or
1069 else to the BLOCK node we just constructed.
1070
1071 Note that for tagged types whose scope is just the formal
1072 parameter list for some function type specification, we can't
1073 properly set their TYPE_CONTEXTs here, because we don't have a
1074 pointer to the appropriate FUNCTION_TYPE node readily available
1075 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
1076 type nodes get set in `grokdeclarator' as soon as we have created
1077 the FUNCTION_TYPE node which will represent the "scope" for these
1078 "parameter list local" tagged types. */
1079 if (scope->function_body)
1080 context = current_function_decl;
1081 else if (scope == file_scope)
1082 {
1083 tree file_decl = build_decl (UNKNOWN_LOCATION,
1084 TRANSLATION_UNIT_DECL, 0, 0);
1085 TREE_CHAIN (file_decl) = all_translation_units;
1086 all_translation_units = file_decl;
1087 context = file_decl;
1088 }
1089 else
1090 context = block;
1091
1092 /* Clear all bindings in this scope. */
1093 for (b = scope->bindings; b; b = free_binding_and_advance (b))
1094 {
1095 p = b->decl;
1096 switch (TREE_CODE (p))
1097 {
1098 case LABEL_DECL:
1099 /* Warnings for unused labels, errors for undefined labels. */
1100 if (TREE_USED (p) && !DECL_INITIAL (p))
1101 {
1102 error ("label %q+D used but not defined", p);
1103 DECL_INITIAL (p) = error_mark_node;
1104 }
1105 else
1106 warn_for_unused_label (p);
1107
1108 /* Labels go in BLOCK_VARS. */
1109 TREE_CHAIN (p) = BLOCK_VARS (block);
1110 BLOCK_VARS (block) = p;
1111 gcc_assert (I_LABEL_BINDING (b->id) == b);
1112 I_LABEL_BINDING (b->id) = b->shadowed;
1113
1114 /* Also pop back to the shadowed label_vars. */
1115 release_tree_vector (b->u.label->decls_in_scope);
1116 b->u.label = b->u.label->shadowed;
1117 break;
1118
1119 case ENUMERAL_TYPE:
1120 case UNION_TYPE:
1121 case RECORD_TYPE:
1122 set_type_context (p, context);
1123
1124 /* Types may not have tag-names, in which case the type
1125 appears in the bindings list with b->id NULL. */
1126 if (b->id)
1127 {
1128 gcc_assert (I_TAG_BINDING (b->id) == b);
1129 I_TAG_BINDING (b->id) = b->shadowed;
1130 }
1131 break;
1132
1133 case FUNCTION_DECL:
1134 /* Propagate TREE_ADDRESSABLE from nested functions to their
1135 containing functions. */
1136 if (!TREE_ASM_WRITTEN (p)
1137 && DECL_INITIAL (p) != 0
1138 && TREE_ADDRESSABLE (p)
1139 && DECL_ABSTRACT_ORIGIN (p) != 0
1140 && DECL_ABSTRACT_ORIGIN (p) != p)
1141 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
1142 if (!DECL_EXTERNAL (p)
1143 && !DECL_INITIAL (p)
1144 && scope != file_scope
1145 && scope != external_scope)
1146 {
1147 error ("nested function %q+D declared but never defined", p);
1148 undef_nested_function = true;
1149 }
1150 else if (DECL_DECLARED_INLINE_P (p)
1151 && TREE_PUBLIC (p)
1152 && !DECL_INITIAL (p))
1153 {
1154 /* C99 6.7.4p6: "a function with external linkage... declared
1155 with an inline function specifier ... shall also be defined
1156 in the same translation unit." */
1157 if (!flag_gnu89_inline)
1158 pedwarn (input_location, 0,
1159 "inline function %q+D declared but never defined", p);
1160 DECL_EXTERNAL (p) = 1;
1161 }
1162
1163 goto common_symbol;
1164
1165 case VAR_DECL:
1166 /* Warnings for unused variables. */
1167 if ((!TREE_USED (p) || !DECL_READ_P (p))
1168 && !TREE_NO_WARNING (p)
1169 && !DECL_IN_SYSTEM_HEADER (p)
1170 && DECL_NAME (p)
1171 && !DECL_ARTIFICIAL (p)
1172 && scope != file_scope
1173 && scope != external_scope)
1174 {
1175 if (!TREE_USED (p))
1176 warning (OPT_Wunused_variable, "unused variable %q+D", p);
1177 else if (DECL_CONTEXT (p) == current_function_decl)
1178 warning_at (DECL_SOURCE_LOCATION (p),
1179 OPT_Wunused_but_set_variable,
1180 "variable %qD set but not used", p);
1181 }
1182
1183 if (b->inner_comp)
1184 {
1185 error ("type of array %q+D completed incompatibly with"
1186 " implicit initialization", p);
1187 }
1188
1189 /* Fall through. */
1190 case TYPE_DECL:
1191 case CONST_DECL:
1192 common_symbol:
1193 /* All of these go in BLOCK_VARS, but only if this is the
1194 binding in the home scope. */
1195 if (!b->nested)
1196 {
1197 TREE_CHAIN (p) = BLOCK_VARS (block);
1198 BLOCK_VARS (block) = p;
1199 }
1200 else if (VAR_OR_FUNCTION_DECL_P (p))
1201 {
1202 /* For block local externs add a special
1203 DECL_EXTERNAL decl for debug info generation. */
1204 tree extp = copy_node (p);
1205
1206 DECL_EXTERNAL (extp) = 1;
1207 TREE_STATIC (extp) = 0;
1208 TREE_PUBLIC (extp) = 1;
1209 DECL_INITIAL (extp) = NULL_TREE;
1210 DECL_LANG_SPECIFIC (extp) = NULL;
1211 DECL_CONTEXT (extp) = current_function_decl;
1212 if (TREE_CODE (p) == FUNCTION_DECL)
1213 {
1214 DECL_RESULT (extp) = NULL_TREE;
1215 DECL_SAVED_TREE (extp) = NULL_TREE;
1216 DECL_STRUCT_FUNCTION (extp) = NULL;
1217 }
1218 if (b->locus != UNKNOWN_LOCATION)
1219 DECL_SOURCE_LOCATION (extp) = b->locus;
1220 TREE_CHAIN (extp) = BLOCK_VARS (block);
1221 BLOCK_VARS (block) = extp;
1222 }
1223 /* If this is the file scope, and we are processing more
1224 than one translation unit in this compilation, set
1225 DECL_CONTEXT of each decl to the TRANSLATION_UNIT_DECL.
1226 This makes same_translation_unit_p work, and causes
1227 static declarations to be given disambiguating suffixes. */
1228 if (scope == file_scope && num_in_fnames > 1)
1229 {
1230 DECL_CONTEXT (p) = context;
1231 if (TREE_CODE (p) == TYPE_DECL)
1232 set_type_context (TREE_TYPE (p), context);
1233 }
1234
1235 /* Fall through. */
1236 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
1237 already been put there by store_parm_decls. Unused-
1238 parameter warnings are handled by function.c.
1239 error_mark_node obviously does not go in BLOCK_VARS and
1240 does not get unused-variable warnings. */
1241 case PARM_DECL:
1242 case ERROR_MARK:
1243 /* It is possible for a decl not to have a name. We get
1244 here with b->id NULL in this case. */
1245 if (b->id)
1246 {
1247 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
1248 I_SYMBOL_BINDING (b->id) = b->shadowed;
1249 if (b->shadowed && b->shadowed->u.type)
1250 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
1251 }
1252 break;
1253
1254 default:
1255 gcc_unreachable ();
1256 }
1257 }
1258
1259
1260 /* Dispose of the block that we just made inside some higher level. */
1261 if ((scope->function_body || scope == file_scope) && context)
1262 {
1263 DECL_INITIAL (context) = block;
1264 BLOCK_SUPERCONTEXT (block) = context;
1265 }
1266 else if (scope->outer)
1267 {
1268 if (block)
1269 SCOPE_LIST_APPEND (scope->outer, blocks, block);
1270 /* If we did not make a block for the scope just exited, any
1271 blocks made for inner scopes must be carried forward so they
1272 will later become subblocks of something else. */
1273 else if (scope->blocks)
1274 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
1275 }
1276
1277 /* Pop the current scope, and free the structure for reuse. */
1278 current_scope = scope->outer;
1279 if (scope->function_body)
1280 current_function_scope = scope->outer_function;
1281
1282 memset (scope, 0, sizeof (struct c_scope));
1283 scope->outer = scope_freelist;
1284 scope_freelist = scope;
1285
1286 return block;
1287 }
1288
1289 void
1290 push_file_scope (void)
1291 {
1292 tree decl;
1293
1294 if (file_scope)
1295 return;
1296
1297 push_scope ();
1298 file_scope = current_scope;
1299
1300 start_fname_decls ();
1301
1302 for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl))
1303 bind (DECL_NAME (decl), decl, file_scope,
1304 /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl));
1305 }
1306
1307 void
1308 pop_file_scope (void)
1309 {
1310 /* In case there were missing closebraces, get us back to the global
1311 binding level. */
1312 while (current_scope != file_scope)
1313 pop_scope ();
1314
1315 /* __FUNCTION__ is defined at file scope (""). This
1316 call may not be necessary as my tests indicate it
1317 still works without it. */
1318 finish_fname_decls ();
1319
1320 check_inline_statics ();
1321
1322 /* This is the point to write out a PCH if we're doing that.
1323 In that case we do not want to do anything else. */
1324 if (pch_file)
1325 {
1326 c_common_write_pch ();
1327 return;
1328 }
1329
1330 /* Pop off the file scope and close this translation unit. */
1331 pop_scope ();
1332 file_scope = 0;
1333
1334 maybe_apply_pending_pragma_weaks ();
1335 }
1336 \f
1337 /* Adjust the bindings for the start of a statement expression. */
1338
1339 void
1340 c_bindings_start_stmt_expr (struct c_spot_bindings* switch_bindings)
1341 {
1342 struct c_scope *scope;
1343
1344 for (scope = current_scope; scope != NULL; scope = scope->outer)
1345 {
1346 struct c_binding *b;
1347
1348 if (!scope->has_label_bindings)
1349 continue;
1350
1351 for (b = scope->bindings; b != NULL; b = b->prev)
1352 {
1353 struct c_label_vars *label_vars;
1354 unsigned int ix;
1355 struct c_goto_bindings *g;
1356
1357 if (TREE_CODE (b->decl) != LABEL_DECL)
1358 continue;
1359 label_vars = b->u.label;
1360 ++label_vars->label_bindings.stmt_exprs;
1361 for (ix = 0;
1362 VEC_iterate (c_goto_bindings_p, label_vars->gotos, ix, g);
1363 ++ix)
1364 ++g->goto_bindings.stmt_exprs;
1365 }
1366 }
1367
1368 if (switch_bindings != NULL)
1369 ++switch_bindings->stmt_exprs;
1370 }
1371
1372 /* Adjust the bindings for the end of a statement expression. */
1373
1374 void
1375 c_bindings_end_stmt_expr (struct c_spot_bindings *switch_bindings)
1376 {
1377 struct c_scope *scope;
1378
1379 for (scope = current_scope; scope != NULL; scope = scope->outer)
1380 {
1381 struct c_binding *b;
1382
1383 if (!scope->has_label_bindings)
1384 continue;
1385
1386 for (b = scope->bindings; b != NULL; b = b->prev)
1387 {
1388 struct c_label_vars *label_vars;
1389 unsigned int ix;
1390 struct c_goto_bindings *g;
1391
1392 if (TREE_CODE (b->decl) != LABEL_DECL)
1393 continue;
1394 label_vars = b->u.label;
1395 --label_vars->label_bindings.stmt_exprs;
1396 if (label_vars->label_bindings.stmt_exprs < 0)
1397 {
1398 label_vars->label_bindings.left_stmt_expr = true;
1399 label_vars->label_bindings.stmt_exprs = 0;
1400 }
1401 for (ix = 0;
1402 VEC_iterate (c_goto_bindings_p, label_vars->gotos, ix, g);
1403 ++ix)
1404 {
1405 --g->goto_bindings.stmt_exprs;
1406 if (g->goto_bindings.stmt_exprs < 0)
1407 {
1408 g->goto_bindings.left_stmt_expr = true;
1409 g->goto_bindings.stmt_exprs = 0;
1410 }
1411 }
1412 }
1413 }
1414
1415 if (switch_bindings != NULL)
1416 {
1417 --switch_bindings->stmt_exprs;
1418 gcc_assert (switch_bindings->stmt_exprs >= 0);
1419 }
1420 }
1421 \f
1422 /* Push a definition or a declaration of struct, union or enum tag "name".
1423 "type" should be the type node.
1424 We assume that the tag "name" is not already defined, and has a location
1425 of LOC.
1426
1427 Note that the definition may really be just a forward reference.
1428 In that case, the TYPE_SIZE will be zero. */
1429
1430 static void
1431 pushtag (location_t loc, tree name, tree type)
1432 {
1433 /* Record the identifier as the type's name if it has none. */
1434 if (name && !TYPE_NAME (type))
1435 TYPE_NAME (type) = name;
1436 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false, loc);
1437
1438 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1439 tagged type we just added to the current scope. This fake
1440 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1441 to output a representation of a tagged type, and it also gives
1442 us a convenient place to record the "scope start" address for the
1443 tagged type. */
1444
1445 TYPE_STUB_DECL (type) = pushdecl (build_decl (loc,
1446 TYPE_DECL, NULL_TREE, type));
1447
1448 /* An approximation for now, so we can tell this is a function-scope tag.
1449 This will be updated in pop_scope. */
1450 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
1451
1452 if (warn_cxx_compat && name != NULL_TREE)
1453 {
1454 struct c_binding *b = I_SYMBOL_BINDING (name);
1455
1456 if (b != NULL
1457 && b->decl != NULL_TREE
1458 && TREE_CODE (b->decl) == TYPE_DECL
1459 && (B_IN_CURRENT_SCOPE (b)
1460 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
1461 && (TYPE_MAIN_VARIANT (TREE_TYPE (b->decl))
1462 != TYPE_MAIN_VARIANT (type)))
1463 {
1464 warning_at (loc, OPT_Wc___compat,
1465 ("using %qD as both a typedef and a tag is "
1466 "invalid in C++"),
1467 b->decl);
1468 if (b->locus != UNKNOWN_LOCATION)
1469 inform (b->locus, "originally defined here");
1470 }
1471 }
1472 }
1473 \f
1474 /* Subroutine of compare_decls. Allow harmless mismatches in return
1475 and argument types provided that the type modes match. This function
1476 return a unified type given a suitable match, and 0 otherwise. */
1477
1478 static tree
1479 match_builtin_function_types (tree newtype, tree oldtype)
1480 {
1481 tree newrettype, oldrettype;
1482 tree newargs, oldargs;
1483 tree trytype, tryargs;
1484
1485 /* Accept the return type of the new declaration if same modes. */
1486 oldrettype = TREE_TYPE (oldtype);
1487 newrettype = TREE_TYPE (newtype);
1488
1489 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
1490 return 0;
1491
1492 oldargs = TYPE_ARG_TYPES (oldtype);
1493 newargs = TYPE_ARG_TYPES (newtype);
1494 tryargs = newargs;
1495
1496 while (oldargs || newargs)
1497 {
1498 if (!oldargs
1499 || !newargs
1500 || !TREE_VALUE (oldargs)
1501 || !TREE_VALUE (newargs)
1502 || TYPE_MODE (TREE_VALUE (oldargs))
1503 != TYPE_MODE (TREE_VALUE (newargs)))
1504 return 0;
1505
1506 oldargs = TREE_CHAIN (oldargs);
1507 newargs = TREE_CHAIN (newargs);
1508 }
1509
1510 trytype = build_function_type (newrettype, tryargs);
1511 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
1512 }
1513
1514 /* Subroutine of diagnose_mismatched_decls. Check for function type
1515 mismatch involving an empty arglist vs a nonempty one and give clearer
1516 diagnostics. */
1517 static void
1518 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1519 tree newtype, tree oldtype)
1520 {
1521 tree t;
1522
1523 if (TREE_CODE (olddecl) != FUNCTION_DECL
1524 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1525 || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
1526 ||
1527 (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
1528 return;
1529
1530 t = TYPE_ARG_TYPES (oldtype);
1531 if (t == 0)
1532 t = TYPE_ARG_TYPES (newtype);
1533 for (; t; t = TREE_CHAIN (t))
1534 {
1535 tree type = TREE_VALUE (t);
1536
1537 if (TREE_CHAIN (t) == 0
1538 && TYPE_MAIN_VARIANT (type) != void_type_node)
1539 {
1540 inform (input_location, "a parameter list with an ellipsis can%'t match "
1541 "an empty parameter name list declaration");
1542 break;
1543 }
1544
1545 if (c_type_promotes_to (type) != type)
1546 {
1547 inform (input_location, "an argument type that has a default promotion can%'t match "
1548 "an empty parameter name list declaration");
1549 break;
1550 }
1551 }
1552 }
1553
1554 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1555 old-style function definition, NEWDECL is a prototype declaration.
1556 Diagnose inconsistencies in the argument list. Returns TRUE if
1557 the prototype is compatible, FALSE if not. */
1558 static bool
1559 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1560 {
1561 tree newargs, oldargs;
1562 int i;
1563
1564 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1565
1566 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1567 newargs = TYPE_ARG_TYPES (newtype);
1568 i = 1;
1569
1570 for (;;)
1571 {
1572 tree oldargtype = TREE_VALUE (oldargs);
1573 tree newargtype = TREE_VALUE (newargs);
1574
1575 if (oldargtype == error_mark_node || newargtype == error_mark_node)
1576 return false;
1577
1578 oldargtype = TYPE_MAIN_VARIANT (oldargtype);
1579 newargtype = TYPE_MAIN_VARIANT (newargtype);
1580
1581 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1582 break;
1583
1584 /* Reaching the end of just one list means the two decls don't
1585 agree on the number of arguments. */
1586 if (END_OF_ARGLIST (oldargtype))
1587 {
1588 error ("prototype for %q+D declares more arguments "
1589 "than previous old-style definition", newdecl);
1590 return false;
1591 }
1592 else if (END_OF_ARGLIST (newargtype))
1593 {
1594 error ("prototype for %q+D declares fewer arguments "
1595 "than previous old-style definition", newdecl);
1596 return false;
1597 }
1598
1599 /* Type for passing arg must be consistent with that declared
1600 for the arg. */
1601 else if (!comptypes (oldargtype, newargtype))
1602 {
1603 error ("prototype for %q+D declares argument %d"
1604 " with incompatible type",
1605 newdecl, i);
1606 return false;
1607 }
1608
1609 oldargs = TREE_CHAIN (oldargs);
1610 newargs = TREE_CHAIN (newargs);
1611 i++;
1612 }
1613
1614 /* If we get here, no errors were found, but do issue a warning
1615 for this poor-style construct. */
1616 warning (0, "prototype for %q+D follows non-prototype definition",
1617 newdecl);
1618 return true;
1619 #undef END_OF_ARGLIST
1620 }
1621
1622 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1623 first in a pair of mismatched declarations, using the diagnostic
1624 function DIAG. */
1625 static void
1626 locate_old_decl (tree decl)
1627 {
1628 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1629 ;
1630 else if (DECL_INITIAL (decl))
1631 inform (input_location, "previous definition of %q+D was here", decl);
1632 else if (C_DECL_IMPLICIT (decl))
1633 inform (input_location, "previous implicit declaration of %q+D was here", decl);
1634 else
1635 inform (input_location, "previous declaration of %q+D was here", decl);
1636 }
1637
1638 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1639 Returns true if the caller should proceed to merge the two, false
1640 if OLDDECL should simply be discarded. As a side effect, issues
1641 all necessary diagnostics for invalid or poor-style combinations.
1642 If it returns true, writes the types of NEWDECL and OLDDECL to
1643 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1644 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1645
1646 static bool
1647 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1648 tree *newtypep, tree *oldtypep)
1649 {
1650 tree newtype, oldtype;
1651 bool pedwarned = false;
1652 bool warned = false;
1653 bool retval = true;
1654
1655 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
1656 && DECL_EXTERNAL (DECL))
1657
1658 /* If we have error_mark_node for either decl or type, just discard
1659 the previous decl - we're in an error cascade already. */
1660 if (olddecl == error_mark_node || newdecl == error_mark_node)
1661 return false;
1662 *oldtypep = oldtype = TREE_TYPE (olddecl);
1663 *newtypep = newtype = TREE_TYPE (newdecl);
1664 if (oldtype == error_mark_node || newtype == error_mark_node)
1665 return false;
1666
1667 /* Two different categories of symbol altogether. This is an error
1668 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1669 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1670 {
1671 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1672 && DECL_BUILT_IN (olddecl)
1673 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1674 {
1675 error ("%q+D redeclared as different kind of symbol", newdecl);
1676 locate_old_decl (olddecl);
1677 }
1678 else if (TREE_PUBLIC (newdecl))
1679 warning (0, "built-in function %q+D declared as non-function",
1680 newdecl);
1681 else
1682 warning (OPT_Wshadow, "declaration of %q+D shadows "
1683 "a built-in function", newdecl);
1684 return false;
1685 }
1686
1687 /* Enumerators have no linkage, so may only be declared once in a
1688 given scope. */
1689 if (TREE_CODE (olddecl) == CONST_DECL)
1690 {
1691 error ("redeclaration of enumerator %q+D", newdecl);
1692 locate_old_decl (olddecl);
1693 return false;
1694 }
1695
1696 if (!comptypes (oldtype, newtype))
1697 {
1698 if (TREE_CODE (olddecl) == FUNCTION_DECL
1699 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1700 {
1701 /* Accept harmless mismatch in function types.
1702 This is for the ffs and fprintf builtins. */
1703 tree trytype = match_builtin_function_types (newtype, oldtype);
1704
1705 if (trytype && comptypes (newtype, trytype))
1706 *oldtypep = oldtype = trytype;
1707 else
1708 {
1709 /* If types don't match for a built-in, throw away the
1710 built-in. No point in calling locate_old_decl here, it
1711 won't print anything. */
1712 warning (0, "conflicting types for built-in function %q+D",
1713 newdecl);
1714 return false;
1715 }
1716 }
1717 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1718 && DECL_IS_BUILTIN (olddecl))
1719 {
1720 /* A conflicting function declaration for a predeclared
1721 function that isn't actually built in. Objective C uses
1722 these. The new declaration silently overrides everything
1723 but the volatility (i.e. noreturn) indication. See also
1724 below. FIXME: Make Objective C use normal builtins. */
1725 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1726 return false;
1727 }
1728 /* Permit void foo (...) to match int foo (...) if the latter is
1729 the definition and implicit int was used. See
1730 c-torture/compile/920625-2.c. */
1731 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1732 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1733 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1734 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1735 {
1736 pedwarned = pedwarn (input_location, 0,
1737 "conflicting types for %q+D", newdecl);
1738 /* Make sure we keep void as the return type. */
1739 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1740 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1741 }
1742 /* Permit void foo (...) to match an earlier call to foo (...) with
1743 no declared type (thus, implicitly int). */
1744 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1745 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1746 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1747 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1748 {
1749 pedwarned = pedwarn (input_location, 0,
1750 "conflicting types for %q+D", newdecl);
1751 /* Make sure we keep void as the return type. */
1752 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1753 }
1754 else
1755 {
1756 int new_quals = TYPE_QUALS (newtype);
1757 int old_quals = TYPE_QUALS (oldtype);
1758
1759 if (new_quals != old_quals)
1760 {
1761 addr_space_t new_addr = DECODE_QUAL_ADDR_SPACE (new_quals);
1762 addr_space_t old_addr = DECODE_QUAL_ADDR_SPACE (old_quals);
1763 if (new_addr != old_addr)
1764 {
1765 if (ADDR_SPACE_GENERIC_P (new_addr))
1766 error ("conflicting named address spaces (generic vs %s) "
1767 "for %q+D",
1768 c_addr_space_name (old_addr), newdecl);
1769 else if (ADDR_SPACE_GENERIC_P (old_addr))
1770 error ("conflicting named address spaces (%s vs generic) "
1771 "for %q+D",
1772 c_addr_space_name (new_addr), newdecl);
1773 else
1774 error ("conflicting named address spaces (%s vs %s) "
1775 "for %q+D",
1776 c_addr_space_name (new_addr),
1777 c_addr_space_name (old_addr),
1778 newdecl);
1779 }
1780
1781 if (CLEAR_QUAL_ADDR_SPACE (new_quals)
1782 != CLEAR_QUAL_ADDR_SPACE (old_quals))
1783 error ("conflicting type qualifiers for %q+D", newdecl);
1784 }
1785 else
1786 error ("conflicting types for %q+D", newdecl);
1787 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1788 locate_old_decl (olddecl);
1789 return false;
1790 }
1791 }
1792
1793 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1794 but silently ignore the redeclaration if either is in a system
1795 header. (Conflicting redeclarations were handled above.) */
1796 if (TREE_CODE (newdecl) == TYPE_DECL)
1797 {
1798 if (DECL_IN_SYSTEM_HEADER (newdecl)
1799 || DECL_IN_SYSTEM_HEADER (olddecl)
1800 || TREE_NO_WARNING (newdecl)
1801 || TREE_NO_WARNING (olddecl))
1802 return true; /* Allow OLDDECL to continue in use. */
1803
1804 error ("redefinition of typedef %q+D", newdecl);
1805 locate_old_decl (olddecl);
1806 return false;
1807 }
1808
1809 /* Function declarations can either be 'static' or 'extern' (no
1810 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1811 can never conflict with each other on account of linkage
1812 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
1813 gnu89 mode permits two definitions if one is 'extern inline' and
1814 one is not. The non- extern-inline definition supersedes the
1815 extern-inline definition. */
1816
1817 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1818 {
1819 /* If you declare a built-in function name as static, or
1820 define the built-in with an old-style definition (so we
1821 can't validate the argument list) the built-in definition is
1822 overridden, but optionally warn this was a bad choice of name. */
1823 if (DECL_BUILT_IN (olddecl)
1824 && !C_DECL_DECLARED_BUILTIN (olddecl)
1825 && (!TREE_PUBLIC (newdecl)
1826 || (DECL_INITIAL (newdecl)
1827 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1828 {
1829 warning (OPT_Wshadow, "declaration of %q+D shadows "
1830 "a built-in function", newdecl);
1831 /* Discard the old built-in function. */
1832 return false;
1833 }
1834
1835 if (DECL_INITIAL (newdecl))
1836 {
1837 if (DECL_INITIAL (olddecl))
1838 {
1839 /* If both decls are in the same TU and the new declaration
1840 isn't overriding an extern inline reject the new decl.
1841 In c99, no overriding is allowed in the same translation
1842 unit. */
1843 if ((!DECL_EXTERN_INLINE (olddecl)
1844 || DECL_EXTERN_INLINE (newdecl)
1845 || (!flag_gnu89_inline
1846 && (!DECL_DECLARED_INLINE_P (olddecl)
1847 || !lookup_attribute ("gnu_inline",
1848 DECL_ATTRIBUTES (olddecl)))
1849 && (!DECL_DECLARED_INLINE_P (newdecl)
1850 || !lookup_attribute ("gnu_inline",
1851 DECL_ATTRIBUTES (newdecl))))
1852 )
1853 && same_translation_unit_p (newdecl, olddecl))
1854 {
1855 error ("redefinition of %q+D", newdecl);
1856 locate_old_decl (olddecl);
1857 return false;
1858 }
1859 }
1860 }
1861 /* If we have a prototype after an old-style function definition,
1862 the argument types must be checked specially. */
1863 else if (DECL_INITIAL (olddecl)
1864 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1865 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1866 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1867 {
1868 locate_old_decl (olddecl);
1869 return false;
1870 }
1871 /* A non-static declaration (even an "extern") followed by a
1872 static declaration is undefined behavior per C99 6.2.2p3-5,7.
1873 The same is true for a static forward declaration at block
1874 scope followed by a non-static declaration/definition at file
1875 scope. Static followed by non-static at the same scope is
1876 not undefined behavior, and is the most convenient way to get
1877 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
1878 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1879 we do diagnose it if -Wtraditional. */
1880 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1881 {
1882 /* Two exceptions to the rule. If olddecl is an extern
1883 inline, or a predeclared function that isn't actually
1884 built in, newdecl silently overrides olddecl. The latter
1885 occur only in Objective C; see also above. (FIXME: Make
1886 Objective C use normal builtins.) */
1887 if (!DECL_IS_BUILTIN (olddecl)
1888 && !DECL_EXTERN_INLINE (olddecl))
1889 {
1890 error ("static declaration of %q+D follows "
1891 "non-static declaration", newdecl);
1892 locate_old_decl (olddecl);
1893 }
1894 return false;
1895 }
1896 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
1897 {
1898 if (DECL_CONTEXT (olddecl))
1899 {
1900 error ("non-static declaration of %q+D follows "
1901 "static declaration", newdecl);
1902 locate_old_decl (olddecl);
1903 return false;
1904 }
1905 else if (warn_traditional)
1906 {
1907 warned |= warning (OPT_Wtraditional,
1908 "non-static declaration of %q+D "
1909 "follows static declaration", newdecl);
1910 }
1911 }
1912
1913 /* Make sure gnu_inline attribute is either not present, or
1914 present on all inline decls. */
1915 if (DECL_DECLARED_INLINE_P (olddecl)
1916 && DECL_DECLARED_INLINE_P (newdecl))
1917 {
1918 bool newa = lookup_attribute ("gnu_inline",
1919 DECL_ATTRIBUTES (newdecl)) != NULL;
1920 bool olda = lookup_attribute ("gnu_inline",
1921 DECL_ATTRIBUTES (olddecl)) != NULL;
1922 if (newa != olda)
1923 {
1924 error_at (input_location, "%<gnu_inline%> attribute present on %q+D",
1925 newa ? newdecl : olddecl);
1926 error_at (DECL_SOURCE_LOCATION (newa ? olddecl : newdecl),
1927 "but not here");
1928 }
1929 }
1930 }
1931 else if (TREE_CODE (newdecl) == VAR_DECL)
1932 {
1933 /* Only variables can be thread-local, and all declarations must
1934 agree on this property. */
1935 if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
1936 {
1937 /* Nothing to check. Since OLDDECL is marked threadprivate
1938 and NEWDECL does not have a thread-local attribute, we
1939 will merge the threadprivate attribute into NEWDECL. */
1940 ;
1941 }
1942 else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
1943 {
1944 if (DECL_THREAD_LOCAL_P (newdecl))
1945 error ("thread-local declaration of %q+D follows "
1946 "non-thread-local declaration", newdecl);
1947 else
1948 error ("non-thread-local declaration of %q+D follows "
1949 "thread-local declaration", newdecl);
1950
1951 locate_old_decl (olddecl);
1952 return false;
1953 }
1954
1955 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1956 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1957 {
1958 error ("redefinition of %q+D", newdecl);
1959 locate_old_decl (olddecl);
1960 return false;
1961 }
1962
1963 /* Objects declared at file scope: if the first declaration had
1964 external linkage (even if it was an external reference) the
1965 second must have external linkage as well, or the behavior is
1966 undefined. If the first declaration had internal linkage, then
1967 the second must too, or else be an external reference (in which
1968 case the composite declaration still has internal linkage).
1969 As for function declarations, we warn about the static-then-
1970 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
1971 if (DECL_FILE_SCOPE_P (newdecl)
1972 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1973 {
1974 if (DECL_EXTERNAL (newdecl))
1975 {
1976 if (!DECL_FILE_SCOPE_P (olddecl))
1977 {
1978 error ("extern declaration of %q+D follows "
1979 "declaration with no linkage", newdecl);
1980 locate_old_decl (olddecl);
1981 return false;
1982 }
1983 else if (warn_traditional)
1984 {
1985 warned |= warning (OPT_Wtraditional,
1986 "non-static declaration of %q+D "
1987 "follows static declaration", newdecl);
1988 }
1989 }
1990 else
1991 {
1992 if (TREE_PUBLIC (newdecl))
1993 error ("non-static declaration of %q+D follows "
1994 "static declaration", newdecl);
1995 else
1996 error ("static declaration of %q+D follows "
1997 "non-static declaration", newdecl);
1998
1999 locate_old_decl (olddecl);
2000 return false;
2001 }
2002 }
2003 /* Two objects with the same name declared at the same block
2004 scope must both be external references (6.7p3). */
2005 else if (!DECL_FILE_SCOPE_P (newdecl))
2006 {
2007 if (DECL_EXTERNAL (newdecl))
2008 {
2009 /* Extern with initializer at block scope, which will
2010 already have received an error. */
2011 }
2012 else if (DECL_EXTERNAL (olddecl))
2013 {
2014 error ("declaration of %q+D with no linkage follows "
2015 "extern declaration", newdecl);
2016 locate_old_decl (olddecl);
2017 }
2018 else
2019 {
2020 error ("redeclaration of %q+D with no linkage", newdecl);
2021 locate_old_decl (olddecl);
2022 }
2023
2024 return false;
2025 }
2026
2027 /* C++ does not permit a decl to appear multiple times at file
2028 scope. */
2029 if (warn_cxx_compat
2030 && DECL_FILE_SCOPE_P (newdecl)
2031 && !DECL_EXTERNAL (newdecl)
2032 && !DECL_EXTERNAL (olddecl))
2033 warned |= warning_at (DECL_SOURCE_LOCATION (newdecl),
2034 OPT_Wc___compat,
2035 ("duplicate declaration of %qD is "
2036 "invalid in C++"),
2037 newdecl);
2038 }
2039
2040 /* warnings */
2041 /* All decls must agree on a visibility. */
2042 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
2043 && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
2044 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2045 {
2046 warned |= warning (0, "redeclaration of %q+D with different visibility "
2047 "(old visibility preserved)", newdecl);
2048 }
2049
2050 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2051 {
2052 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
2053 if (DECL_DECLARED_INLINE_P (newdecl)
2054 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2055 {
2056 warned |= warning (OPT_Wattributes,
2057 "inline declaration of %qD follows "
2058 "declaration with attribute noinline", newdecl);
2059 }
2060 else if (DECL_DECLARED_INLINE_P (olddecl)
2061 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
2062 {
2063 warned |= warning (OPT_Wattributes,
2064 "declaration of %q+D with attribute "
2065 "noinline follows inline declaration ", newdecl);
2066 }
2067 }
2068 else /* PARM_DECL, VAR_DECL */
2069 {
2070 /* Redeclaration of a parameter is a constraint violation (this is
2071 not explicitly stated, but follows from C99 6.7p3 [no more than
2072 one declaration of the same identifier with no linkage in the
2073 same scope, except type tags] and 6.2.2p6 [parameters have no
2074 linkage]). We must check for a forward parameter declaration,
2075 indicated by TREE_ASM_WRITTEN on the old declaration - this is
2076 an extension, the mandatory diagnostic for which is handled by
2077 mark_forward_parm_decls. */
2078
2079 if (TREE_CODE (newdecl) == PARM_DECL
2080 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
2081 {
2082 error ("redefinition of parameter %q+D", newdecl);
2083 locate_old_decl (olddecl);
2084 return false;
2085 }
2086 }
2087
2088 /* Optional warning for completely redundant decls. */
2089 if (!warned && !pedwarned
2090 && warn_redundant_decls
2091 /* Don't warn about a function declaration followed by a
2092 definition. */
2093 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2094 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
2095 /* Don't warn about redundant redeclarations of builtins. */
2096 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2097 && !DECL_BUILT_IN (newdecl)
2098 && DECL_BUILT_IN (olddecl)
2099 && !C_DECL_DECLARED_BUILTIN (olddecl))
2100 /* Don't warn about an extern followed by a definition. */
2101 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
2102 /* Don't warn about forward parameter decls. */
2103 && !(TREE_CODE (newdecl) == PARM_DECL
2104 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2105 /* Don't warn about a variable definition following a declaration. */
2106 && !(TREE_CODE (newdecl) == VAR_DECL
2107 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
2108 {
2109 warned = warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
2110 newdecl);
2111 }
2112
2113 /* Report location of previous decl/defn. */
2114 if (warned || pedwarned)
2115 locate_old_decl (olddecl);
2116
2117 #undef DECL_EXTERN_INLINE
2118
2119 return retval;
2120 }
2121
2122 /* Subroutine of duplicate_decls. NEWDECL has been found to be
2123 consistent with OLDDECL, but carries new information. Merge the
2124 new information into OLDDECL. This function issues no
2125 diagnostics. */
2126
2127 static void
2128 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
2129 {
2130 bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
2131 && DECL_INITIAL (newdecl) != 0);
2132 bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
2133 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != 0);
2134 bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
2135 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) != 0);
2136 bool extern_changed = false;
2137
2138 /* For real parm decl following a forward decl, rechain the old decl
2139 in its new location and clear TREE_ASM_WRITTEN (it's not a
2140 forward decl anymore). */
2141 if (TREE_CODE (newdecl) == PARM_DECL
2142 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2143 {
2144 struct c_binding *b, **here;
2145
2146 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
2147 if ((*here)->decl == olddecl)
2148 goto found;
2149 gcc_unreachable ();
2150
2151 found:
2152 b = *here;
2153 *here = b->prev;
2154 b->prev = current_scope->bindings;
2155 current_scope->bindings = b;
2156
2157 TREE_ASM_WRITTEN (olddecl) = 0;
2158 }
2159
2160 DECL_ATTRIBUTES (newdecl)
2161 = targetm.merge_decl_attributes (olddecl, newdecl);
2162
2163 /* Merge the data types specified in the two decls. */
2164 TREE_TYPE (newdecl)
2165 = TREE_TYPE (olddecl)
2166 = composite_type (newtype, oldtype);
2167
2168 /* Lay the type out, unless already done. */
2169 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
2170 {
2171 if (TREE_TYPE (newdecl) != error_mark_node)
2172 layout_type (TREE_TYPE (newdecl));
2173 if (TREE_CODE (newdecl) != FUNCTION_DECL
2174 && TREE_CODE (newdecl) != TYPE_DECL
2175 && TREE_CODE (newdecl) != CONST_DECL)
2176 layout_decl (newdecl, 0);
2177 }
2178 else
2179 {
2180 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
2181 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
2182 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
2183 DECL_MODE (newdecl) = DECL_MODE (olddecl);
2184 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2185 {
2186 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
2187 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2188 }
2189 }
2190
2191 /* Keep the old rtl since we can safely use it. */
2192 if (HAS_RTL_P (olddecl))
2193 COPY_DECL_RTL (olddecl, newdecl);
2194
2195 /* Merge the type qualifiers. */
2196 if (TREE_READONLY (newdecl))
2197 TREE_READONLY (olddecl) = 1;
2198
2199 if (TREE_THIS_VOLATILE (newdecl))
2200 TREE_THIS_VOLATILE (olddecl) = 1;
2201
2202 /* Merge deprecatedness. */
2203 if (TREE_DEPRECATED (newdecl))
2204 TREE_DEPRECATED (olddecl) = 1;
2205
2206 /* If a decl is in a system header and the other isn't, keep the one on the
2207 system header. Otherwise, keep source location of definition rather than
2208 declaration and of prototype rather than non-prototype unless that
2209 prototype is built-in. */
2210 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2211 && DECL_IN_SYSTEM_HEADER (olddecl)
2212 && !DECL_IN_SYSTEM_HEADER (newdecl) )
2213 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2214 else if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2215 && DECL_IN_SYSTEM_HEADER (newdecl)
2216 && !DECL_IN_SYSTEM_HEADER (olddecl))
2217 DECL_SOURCE_LOCATION (olddecl) = DECL_SOURCE_LOCATION (newdecl);
2218 else if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
2219 || (old_is_prototype && !new_is_prototype
2220 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
2221 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2222
2223 /* Merge the initialization information. */
2224 if (DECL_INITIAL (newdecl) == 0)
2225 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2226
2227 /* Merge the threadprivate attribute. */
2228 if (TREE_CODE (olddecl) == VAR_DECL && C_DECL_THREADPRIVATE_P (olddecl))
2229 {
2230 DECL_TLS_MODEL (newdecl) = DECL_TLS_MODEL (olddecl);
2231 C_DECL_THREADPRIVATE_P (newdecl) = 1;
2232 }
2233
2234 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
2235 {
2236 /* Merge the section attribute.
2237 We want to issue an error if the sections conflict but that
2238 must be done later in decl_attributes since we are called
2239 before attributes are assigned. */
2240 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
2241 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
2242
2243 /* Copy the assembler name.
2244 Currently, it can only be defined in the prototype. */
2245 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2246
2247 /* Use visibility of whichever declaration had it specified */
2248 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2249 {
2250 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2251 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2252 }
2253
2254 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2255 {
2256 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
2257 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2258 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2259 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2260 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2261 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2262 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2263 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
2264 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2265 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2266 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
2267 }
2268
2269 /* Merge the storage class information. */
2270 merge_weak (newdecl, olddecl);
2271
2272 /* For functions, static overrides non-static. */
2273 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2274 {
2275 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
2276 /* This is since we don't automatically
2277 copy the attributes of NEWDECL into OLDDECL. */
2278 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2279 /* If this clears `static', clear it in the identifier too. */
2280 if (!TREE_PUBLIC (olddecl))
2281 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
2282 }
2283 }
2284
2285 /* In c99, 'extern' declaration before (or after) 'inline' means this
2286 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
2287 is present. */
2288 if (TREE_CODE (newdecl) == FUNCTION_DECL
2289 && !flag_gnu89_inline
2290 && (DECL_DECLARED_INLINE_P (newdecl)
2291 || DECL_DECLARED_INLINE_P (olddecl))
2292 && (!DECL_DECLARED_INLINE_P (newdecl)
2293 || !DECL_DECLARED_INLINE_P (olddecl)
2294 || !DECL_EXTERNAL (olddecl))
2295 && DECL_EXTERNAL (newdecl)
2296 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))
2297 && !current_function_decl)
2298 DECL_EXTERNAL (newdecl) = 0;
2299
2300 if (DECL_EXTERNAL (newdecl))
2301 {
2302 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
2303 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
2304
2305 /* An extern decl does not override previous storage class. */
2306 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2307 if (!DECL_EXTERNAL (newdecl))
2308 {
2309 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2310 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2311 }
2312 }
2313 else
2314 {
2315 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
2316 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2317 }
2318
2319 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2320 {
2321 /* If we're redefining a function previously defined as extern
2322 inline, make sure we emit debug info for the inline before we
2323 throw it away, in case it was inlined into a function that
2324 hasn't been written out yet. */
2325 if (new_is_definition && DECL_INITIAL (olddecl))
2326 /* The new defn must not be inline. */
2327 DECL_UNINLINABLE (newdecl) = 1;
2328 else
2329 {
2330 /* If either decl says `inline', this fn is inline, unless
2331 its definition was passed already. */
2332 if (DECL_DECLARED_INLINE_P (newdecl)
2333 || DECL_DECLARED_INLINE_P (olddecl))
2334 DECL_DECLARED_INLINE_P (newdecl) = 1;
2335
2336 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2337 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2338
2339 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2340 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2341 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2342 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2343 }
2344
2345 if (DECL_BUILT_IN (olddecl))
2346 {
2347 /* If redeclaring a builtin function, it stays built in.
2348 But it gets tagged as having been declared. */
2349 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
2350 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
2351 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
2352 if (new_is_prototype)
2353 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
2354 else
2355 C_DECL_BUILTIN_PROTOTYPE (newdecl)
2356 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
2357 }
2358
2359 /* Preserve function specific target and optimization options */
2360 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2361 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2362 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2363 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2364
2365 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2366 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2367 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2368 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2369
2370 /* Also preserve various other info from the definition. */
2371 if (!new_is_definition)
2372 {
2373 tree t;
2374 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2375 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2376 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2377 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2378 gimple_set_body (newdecl, gimple_body (olddecl));
2379 DECL_ARGUMENTS (newdecl) = copy_list (DECL_ARGUMENTS (olddecl));
2380 for (t = DECL_ARGUMENTS (newdecl); t ; t = TREE_CHAIN (t))
2381 DECL_CONTEXT (t) = newdecl;
2382
2383 /* See if we've got a function to instantiate from. */
2384 if (DECL_SAVED_TREE (olddecl))
2385 DECL_ABSTRACT_ORIGIN (newdecl)
2386 = DECL_ABSTRACT_ORIGIN (olddecl);
2387 }
2388 }
2389
2390 extern_changed = DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl);
2391
2392 /* Merge the USED information. */
2393 if (TREE_USED (olddecl))
2394 TREE_USED (newdecl) = 1;
2395 else if (TREE_USED (newdecl))
2396 TREE_USED (olddecl) = 1;
2397 if (TREE_CODE (olddecl) == VAR_DECL || TREE_CODE (olddecl) == PARM_DECL)
2398 DECL_READ_P (newdecl) |= DECL_READ_P (olddecl);
2399 if (DECL_PRESERVE_P (olddecl))
2400 DECL_PRESERVE_P (newdecl) = 1;
2401 else if (DECL_PRESERVE_P (newdecl))
2402 DECL_PRESERVE_P (olddecl) = 1;
2403
2404 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
2405 But preserve OLDDECL's DECL_UID, DECL_CONTEXT and
2406 DECL_ARGUMENTS (if appropriate). */
2407 {
2408 unsigned olddecl_uid = DECL_UID (olddecl);
2409 tree olddecl_context = DECL_CONTEXT (olddecl);
2410 tree olddecl_arguments = NULL;
2411 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2412 olddecl_arguments = DECL_ARGUMENTS (olddecl);
2413
2414 memcpy ((char *) olddecl + sizeof (struct tree_common),
2415 (char *) newdecl + sizeof (struct tree_common),
2416 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2417 switch (TREE_CODE (olddecl))
2418 {
2419 case FUNCTION_DECL:
2420 gimple_set_body (olddecl, gimple_body (newdecl));
2421 /* fall through */
2422
2423 case FIELD_DECL:
2424 case VAR_DECL:
2425 case PARM_DECL:
2426 case LABEL_DECL:
2427 case RESULT_DECL:
2428 case CONST_DECL:
2429 case TYPE_DECL:
2430 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2431 (char *) newdecl + sizeof (struct tree_decl_common),
2432 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2433 break;
2434
2435 default:
2436
2437 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2438 (char *) newdecl + sizeof (struct tree_decl_common),
2439 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
2440 }
2441 DECL_UID (olddecl) = olddecl_uid;
2442 DECL_CONTEXT (olddecl) = olddecl_context;
2443 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2444 DECL_ARGUMENTS (olddecl) = olddecl_arguments;
2445 }
2446
2447 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2448 so that encode_section_info has a chance to look at the new decl
2449 flags and attributes. */
2450 if (DECL_RTL_SET_P (olddecl)
2451 && (TREE_CODE (olddecl) == FUNCTION_DECL
2452 || (TREE_CODE (olddecl) == VAR_DECL
2453 && TREE_STATIC (olddecl))))
2454 make_decl_rtl (olddecl);
2455
2456 /* If we changed a function from DECL_EXTERNAL to !DECL_EXTERNAL,
2457 and the definition is coming from the old version, cgraph needs
2458 to be called again. */
2459 if (extern_changed && !new_is_definition
2460 && TREE_CODE (olddecl) == FUNCTION_DECL && DECL_INITIAL (olddecl))
2461 cgraph_mark_if_needed (olddecl);
2462 }
2463
2464 /* Handle when a new declaration NEWDECL has the same name as an old
2465 one OLDDECL in the same binding contour. Prints an error message
2466 if appropriate.
2467
2468 If safely possible, alter OLDDECL to look like NEWDECL, and return
2469 true. Otherwise, return false. */
2470
2471 static bool
2472 duplicate_decls (tree newdecl, tree olddecl)
2473 {
2474 tree newtype = NULL, oldtype = NULL;
2475
2476 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
2477 {
2478 /* Avoid `unused variable' and other warnings for OLDDECL. */
2479 TREE_NO_WARNING (olddecl) = 1;
2480 return false;
2481 }
2482
2483 merge_decls (newdecl, olddecl, newtype, oldtype);
2484 return true;
2485 }
2486
2487 \f
2488 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
2489 static void
2490 warn_if_shadowing (tree new_decl)
2491 {
2492 struct c_binding *b;
2493
2494 /* Shadow warnings wanted? */
2495 if (!warn_shadow
2496 /* No shadow warnings for internally generated vars. */
2497 || DECL_IS_BUILTIN (new_decl)
2498 /* No shadow warnings for vars made for inlining. */
2499 || DECL_FROM_INLINE (new_decl))
2500 return;
2501
2502 /* Is anything being shadowed? Invisible decls do not count. */
2503 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
2504 if (b->decl && b->decl != new_decl && !b->invisible)
2505 {
2506 tree old_decl = b->decl;
2507
2508 if (old_decl == error_mark_node)
2509 {
2510 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
2511 "non-variable", new_decl);
2512 break;
2513 }
2514 else if (TREE_CODE (old_decl) == PARM_DECL)
2515 warning (OPT_Wshadow, "declaration of %q+D shadows a parameter",
2516 new_decl);
2517 else if (DECL_FILE_SCOPE_P (old_decl))
2518 warning (OPT_Wshadow, "declaration of %q+D shadows a global "
2519 "declaration", new_decl);
2520 else if (TREE_CODE (old_decl) == FUNCTION_DECL
2521 && DECL_BUILT_IN (old_decl))
2522 {
2523 warning (OPT_Wshadow, "declaration of %q+D shadows "
2524 "a built-in function", new_decl);
2525 break;
2526 }
2527 else
2528 warning (OPT_Wshadow, "declaration of %q+D shadows a previous local",
2529 new_decl);
2530
2531 warning_at (DECL_SOURCE_LOCATION (old_decl), OPT_Wshadow,
2532 "shadowed declaration is here");
2533
2534 break;
2535 }
2536 }
2537
2538 /* Record a decl-node X as belonging to the current lexical scope.
2539 Check for errors (such as an incompatible declaration for the same
2540 name already seen in the same scope).
2541
2542 Returns either X or an old decl for the same name.
2543 If an old decl is returned, it may have been smashed
2544 to agree with what X says. */
2545
2546 tree
2547 pushdecl (tree x)
2548 {
2549 tree name = DECL_NAME (x);
2550 struct c_scope *scope = current_scope;
2551 struct c_binding *b;
2552 bool nested = false;
2553 location_t locus = DECL_SOURCE_LOCATION (x);
2554
2555 /* Must set DECL_CONTEXT for everything not at file scope or
2556 DECL_FILE_SCOPE_P won't work. Local externs don't count
2557 unless they have initializers (which generate code). */
2558 if (current_function_decl
2559 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
2560 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2561 DECL_CONTEXT (x) = current_function_decl;
2562
2563 /* Anonymous decls are just inserted in the scope. */
2564 if (!name)
2565 {
2566 bind (name, x, scope, /*invisible=*/false, /*nested=*/false,
2567 locus);
2568 return x;
2569 }
2570
2571 /* First, see if there is another declaration with the same name in
2572 the current scope. If there is, duplicate_decls may do all the
2573 work for us. If duplicate_decls returns false, that indicates
2574 two incompatible decls in the same scope; we are to silently
2575 replace the old one (duplicate_decls has issued all appropriate
2576 diagnostics). In particular, we should not consider possible
2577 duplicates in the external scope, or shadowing. */
2578 b = I_SYMBOL_BINDING (name);
2579 if (b && B_IN_SCOPE (b, scope))
2580 {
2581 struct c_binding *b_ext, *b_use;
2582 tree type = TREE_TYPE (x);
2583 tree visdecl = b->decl;
2584 tree vistype = TREE_TYPE (visdecl);
2585 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2586 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2587 b->inner_comp = false;
2588 b_use = b;
2589 b_ext = b;
2590 /* If this is an external linkage declaration, we should check
2591 for compatibility with the type in the external scope before
2592 setting the type at this scope based on the visible
2593 information only. */
2594 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2595 {
2596 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2597 b_ext = b_ext->shadowed;
2598 if (b_ext)
2599 {
2600 b_use = b_ext;
2601 if (b_use->u.type)
2602 TREE_TYPE (b_use->decl) = b_use->u.type;
2603 }
2604 }
2605 if (duplicate_decls (x, b_use->decl))
2606 {
2607 if (b_use != b)
2608 {
2609 /* Save the updated type in the external scope and
2610 restore the proper type for this scope. */
2611 tree thistype;
2612 if (comptypes (vistype, type))
2613 thistype = composite_type (vistype, type);
2614 else
2615 thistype = TREE_TYPE (b_use->decl);
2616 b_use->u.type = TREE_TYPE (b_use->decl);
2617 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2618 && DECL_BUILT_IN (b_use->decl))
2619 thistype
2620 = build_type_attribute_variant (thistype,
2621 TYPE_ATTRIBUTES
2622 (b_use->u.type));
2623 TREE_TYPE (b_use->decl) = thistype;
2624 }
2625 return b_use->decl;
2626 }
2627 else
2628 goto skip_external_and_shadow_checks;
2629 }
2630
2631 /* All declarations with external linkage, and all external
2632 references, go in the external scope, no matter what scope is
2633 current. However, the binding in that scope is ignored for
2634 purposes of normal name lookup. A separate binding structure is
2635 created in the requested scope; this governs the normal
2636 visibility of the symbol.
2637
2638 The binding in the externals scope is used exclusively for
2639 detecting duplicate declarations of the same object, no matter
2640 what scope they are in; this is what we do here. (C99 6.2.7p2:
2641 All declarations that refer to the same object or function shall
2642 have compatible type; otherwise, the behavior is undefined.) */
2643 if (DECL_EXTERNAL (x) || scope == file_scope)
2644 {
2645 tree type = TREE_TYPE (x);
2646 tree vistype = 0;
2647 tree visdecl = 0;
2648 bool type_saved = false;
2649 if (b && !B_IN_EXTERNAL_SCOPE (b)
2650 && (TREE_CODE (b->decl) == FUNCTION_DECL
2651 || TREE_CODE (b->decl) == VAR_DECL)
2652 && DECL_FILE_SCOPE_P (b->decl))
2653 {
2654 visdecl = b->decl;
2655 vistype = TREE_TYPE (visdecl);
2656 }
2657 if (scope != file_scope
2658 && !DECL_IN_SYSTEM_HEADER (x))
2659 warning (OPT_Wnested_externs, "nested extern declaration of %qD", x);
2660
2661 while (b && !B_IN_EXTERNAL_SCOPE (b))
2662 {
2663 /* If this decl might be modified, save its type. This is
2664 done here rather than when the decl is first bound
2665 because the type may change after first binding, through
2666 being completed or through attributes being added. If we
2667 encounter multiple such decls, only the first should have
2668 its type saved; the others will already have had their
2669 proper types saved and the types will not have changed as
2670 their scopes will not have been re-entered. */
2671 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2672 {
2673 b->u.type = TREE_TYPE (b->decl);
2674 type_saved = true;
2675 }
2676 if (B_IN_FILE_SCOPE (b)
2677 && TREE_CODE (b->decl) == VAR_DECL
2678 && TREE_STATIC (b->decl)
2679 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2680 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2681 && TREE_CODE (type) == ARRAY_TYPE
2682 && TYPE_DOMAIN (type)
2683 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2684 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2685 {
2686 /* Array type completed in inner scope, which should be
2687 diagnosed if the completion does not have size 1 and
2688 it does not get completed in the file scope. */
2689 b->inner_comp = true;
2690 }
2691 b = b->shadowed;
2692 }
2693
2694 /* If a matching external declaration has been found, set its
2695 type to the composite of all the types of that declaration.
2696 After the consistency checks, it will be reset to the
2697 composite of the visible types only. */
2698 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2699 && b->u.type)
2700 TREE_TYPE (b->decl) = b->u.type;
2701
2702 /* The point of the same_translation_unit_p check here is,
2703 we want to detect a duplicate decl for a construct like
2704 foo() { extern bar(); } ... static bar(); but not if
2705 they are in different translation units. In any case,
2706 the static does not go in the externals scope. */
2707 if (b
2708 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2709 && duplicate_decls (x, b->decl))
2710 {
2711 tree thistype;
2712 if (vistype)
2713 {
2714 if (comptypes (vistype, type))
2715 thistype = composite_type (vistype, type);
2716 else
2717 thistype = TREE_TYPE (b->decl);
2718 }
2719 else
2720 thistype = type;
2721 b->u.type = TREE_TYPE (b->decl);
2722 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2723 thistype
2724 = build_type_attribute_variant (thistype,
2725 TYPE_ATTRIBUTES (b->u.type));
2726 TREE_TYPE (b->decl) = thistype;
2727 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true,
2728 locus);
2729 return b->decl;
2730 }
2731 else if (TREE_PUBLIC (x))
2732 {
2733 if (visdecl && !b && duplicate_decls (x, visdecl))
2734 {
2735 /* An external declaration at block scope referring to a
2736 visible entity with internal linkage. The composite
2737 type will already be correct for this scope, so we
2738 just need to fall through to make the declaration in
2739 this scope. */
2740 nested = true;
2741 x = visdecl;
2742 }
2743 else
2744 {
2745 bind (name, x, external_scope, /*invisible=*/true,
2746 /*nested=*/false, locus);
2747 nested = true;
2748 }
2749 }
2750 }
2751
2752 if (TREE_CODE (x) != PARM_DECL)
2753 warn_if_shadowing (x);
2754
2755 skip_external_and_shadow_checks:
2756 if (TREE_CODE (x) == TYPE_DECL)
2757 set_underlying_type (x);
2758
2759 bind (name, x, scope, /*invisible=*/false, nested, locus);
2760
2761 /* If x's type is incomplete because it's based on a
2762 structure or union which has not yet been fully declared,
2763 attach it to that structure or union type, so we can go
2764 back and complete the variable declaration later, if the
2765 structure or union gets fully declared.
2766
2767 If the input is erroneous, we can have error_mark in the type
2768 slot (e.g. "f(void a, ...)") - that doesn't count as an
2769 incomplete type. */
2770 if (TREE_TYPE (x) != error_mark_node
2771 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2772 {
2773 tree element = TREE_TYPE (x);
2774
2775 while (TREE_CODE (element) == ARRAY_TYPE)
2776 element = TREE_TYPE (element);
2777 element = TYPE_MAIN_VARIANT (element);
2778
2779 if ((TREE_CODE (element) == RECORD_TYPE
2780 || TREE_CODE (element) == UNION_TYPE)
2781 && (TREE_CODE (x) != TYPE_DECL
2782 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
2783 && !COMPLETE_TYPE_P (element))
2784 C_TYPE_INCOMPLETE_VARS (element)
2785 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
2786 }
2787 return x;
2788 }
2789
2790 /* Record X as belonging to file scope.
2791 This is used only internally by the Objective-C front end,
2792 and is limited to its needs. duplicate_decls is not called;
2793 if there is any preexisting decl for this identifier, it is an ICE. */
2794
2795 tree
2796 pushdecl_top_level (tree x)
2797 {
2798 tree name;
2799 bool nested = false;
2800 gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
2801
2802 name = DECL_NAME (x);
2803
2804 gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
2805
2806 if (TREE_PUBLIC (x))
2807 {
2808 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false,
2809 UNKNOWN_LOCATION);
2810 nested = true;
2811 }
2812 if (file_scope)
2813 bind (name, x, file_scope, /*invisible=*/false, nested, UNKNOWN_LOCATION);
2814
2815 return x;
2816 }
2817 \f
2818 static void
2819 implicit_decl_warning (tree id, tree olddecl)
2820 {
2821 if (warn_implicit_function_declaration)
2822 {
2823 bool warned;
2824
2825 if (flag_isoc99)
2826 warned = pedwarn (input_location, OPT_Wimplicit_function_declaration,
2827 "implicit declaration of function %qE", id);
2828 else
2829 warned = warning (OPT_Wimplicit_function_declaration,
2830 G_("implicit declaration of function %qE"), id);
2831 if (olddecl && warned)
2832 locate_old_decl (olddecl);
2833 }
2834 }
2835
2836 /* Generate an implicit declaration for identifier FUNCTIONID at LOC as a
2837 function of type int (). */
2838
2839 tree
2840 implicitly_declare (location_t loc, tree functionid)
2841 {
2842 struct c_binding *b;
2843 tree decl = 0;
2844 tree asmspec_tree;
2845
2846 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
2847 {
2848 if (B_IN_SCOPE (b, external_scope))
2849 {
2850 decl = b->decl;
2851 break;
2852 }
2853 }
2854
2855 if (decl)
2856 {
2857 if (decl == error_mark_node)
2858 return decl;
2859
2860 /* FIXME: Objective-C has weird not-really-builtin functions
2861 which are supposed to be visible automatically. They wind up
2862 in the external scope because they're pushed before the file
2863 scope gets created. Catch this here and rebind them into the
2864 file scope. */
2865 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2866 {
2867 bind (functionid, decl, file_scope,
2868 /*invisible=*/false, /*nested=*/true,
2869 DECL_SOURCE_LOCATION (decl));
2870 return decl;
2871 }
2872 else
2873 {
2874 tree newtype = default_function_type;
2875 if (b->u.type)
2876 TREE_TYPE (decl) = b->u.type;
2877 /* Implicit declaration of a function already declared
2878 (somehow) in a different scope, or as a built-in.
2879 If this is the first time this has happened, warn;
2880 then recycle the old declaration but with the new type. */
2881 if (!C_DECL_IMPLICIT (decl))
2882 {
2883 implicit_decl_warning (functionid, decl);
2884 C_DECL_IMPLICIT (decl) = 1;
2885 }
2886 if (DECL_BUILT_IN (decl))
2887 {
2888 newtype = build_type_attribute_variant (newtype,
2889 TYPE_ATTRIBUTES
2890 (TREE_TYPE (decl)));
2891 if (!comptypes (newtype, TREE_TYPE (decl)))
2892 {
2893 warning_at (loc, 0, "incompatible implicit declaration of "
2894 "built-in function %qD", decl);
2895 newtype = TREE_TYPE (decl);
2896 }
2897 }
2898 else
2899 {
2900 if (!comptypes (newtype, TREE_TYPE (decl)))
2901 {
2902 error_at (loc, "incompatible implicit declaration of function %qD", decl);
2903 locate_old_decl (decl);
2904 }
2905 }
2906 b->u.type = TREE_TYPE (decl);
2907 TREE_TYPE (decl) = newtype;
2908 bind (functionid, decl, current_scope,
2909 /*invisible=*/false, /*nested=*/true,
2910 DECL_SOURCE_LOCATION (decl));
2911 return decl;
2912 }
2913 }
2914
2915 /* Not seen before. */
2916 decl = build_decl (loc, FUNCTION_DECL, functionid, default_function_type);
2917 DECL_EXTERNAL (decl) = 1;
2918 TREE_PUBLIC (decl) = 1;
2919 C_DECL_IMPLICIT (decl) = 1;
2920 implicit_decl_warning (functionid, 0);
2921 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
2922 if (asmspec_tree)
2923 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
2924
2925 /* C89 says implicit declarations are in the innermost block.
2926 So we record the decl in the standard fashion. */
2927 decl = pushdecl (decl);
2928
2929 /* No need to call objc_check_decl here - it's a function type. */
2930 rest_of_decl_compilation (decl, 0, 0);
2931
2932 /* Write a record describing this implicit function declaration
2933 to the prototypes file (if requested). */
2934 gen_aux_info_record (decl, 0, 1, 0);
2935
2936 /* Possibly apply some default attributes to this implicit declaration. */
2937 decl_attributes (&decl, NULL_TREE, 0);
2938
2939 return decl;
2940 }
2941
2942 /* Issue an error message for a reference to an undeclared variable
2943 ID, including a reference to a builtin outside of function-call
2944 context. Establish a binding of the identifier to error_mark_node
2945 in an appropriate scope, which will suppress further errors for the
2946 same identifier. The error message should be given location LOC. */
2947 void
2948 undeclared_variable (location_t loc, tree id)
2949 {
2950 static bool already = false;
2951 struct c_scope *scope;
2952
2953 if (current_function_decl == 0)
2954 {
2955 error_at (loc, "%qE undeclared here (not in a function)", id);
2956 scope = current_scope;
2957 }
2958 else
2959 {
2960 error_at (loc, "%qE undeclared (first use in this function)", id);
2961 if (!already)
2962 {
2963 inform (loc, "each undeclared identifier is reported only"
2964 " once for each function it appears in");
2965 already = true;
2966 }
2967
2968 /* If we are parsing old-style parameter decls, current_function_decl
2969 will be nonnull but current_function_scope will be null. */
2970 scope = current_function_scope ? current_function_scope : current_scope;
2971 }
2972 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
2973 UNKNOWN_LOCATION);
2974 }
2975 \f
2976 /* Subroutine of lookup_label, declare_label, define_label: construct a
2977 LABEL_DECL with all the proper frills. Also create a struct
2978 c_label_vars initialized for the current scope. */
2979
2980 static tree
2981 make_label (location_t location, tree name, bool defining,
2982 struct c_label_vars **p_label_vars)
2983 {
2984 tree label = build_decl (location, LABEL_DECL, name, void_type_node);
2985 struct c_label_vars *label_vars;
2986
2987 DECL_CONTEXT (label) = current_function_decl;
2988 DECL_MODE (label) = VOIDmode;
2989
2990 label_vars = GGC_NEW (struct c_label_vars);
2991 label_vars->shadowed = NULL;
2992 set_spot_bindings (&label_vars->label_bindings, defining);
2993 label_vars->decls_in_scope = make_tree_vector ();
2994 label_vars->gotos = VEC_alloc (c_goto_bindings_p, gc, 0);
2995 *p_label_vars = label_vars;
2996
2997 return label;
2998 }
2999
3000 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
3001 Create one if none exists so far for the current function.
3002 This is called when a label is used in a goto expression or
3003 has its address taken. */
3004
3005 tree
3006 lookup_label (tree name)
3007 {
3008 tree label;
3009 struct c_label_vars *label_vars;
3010
3011 if (current_function_decl == 0)
3012 {
3013 error ("label %qE referenced outside of any function", name);
3014 return 0;
3015 }
3016
3017 /* Use a label already defined or ref'd with this name, but not if
3018 it is inherited from a containing function and wasn't declared
3019 using __label__. */
3020 label = I_LABEL_DECL (name);
3021 if (label && (DECL_CONTEXT (label) == current_function_decl
3022 || C_DECLARED_LABEL_FLAG (label)))
3023 {
3024 /* If the label has only been declared, update its apparent
3025 location to point here, for better diagnostics if it
3026 turns out not to have been defined. */
3027 if (DECL_INITIAL (label) == NULL_TREE)
3028 DECL_SOURCE_LOCATION (label) = input_location;
3029 return label;
3030 }
3031
3032 /* No label binding for that identifier; make one. */
3033 label = make_label (input_location, name, false, &label_vars);
3034
3035 /* Ordinary labels go in the current function scope. */
3036 bind_label (name, label, current_function_scope, label_vars);
3037
3038 return label;
3039 }
3040
3041 /* Issue a warning about DECL for a goto statement at GOTO_LOC going
3042 to LABEL. */
3043
3044 static void
3045 warn_about_goto (location_t goto_loc, tree label, tree decl)
3046 {
3047 if (variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3048 error_at (goto_loc,
3049 "jump into scope of identifier with variably modified type");
3050 else
3051 warning_at (goto_loc, OPT_Wjump_misses_init,
3052 "jump skips variable initialization");
3053 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3054 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3055 }
3056
3057 /* Look up a label because of a goto statement. This is like
3058 lookup_label, but also issues any appropriate warnings. */
3059
3060 tree
3061 lookup_label_for_goto (location_t loc, tree name)
3062 {
3063 tree label;
3064 struct c_label_vars *label_vars;
3065 unsigned int ix;
3066 tree decl;
3067
3068 label = lookup_label (name);
3069 if (label == NULL_TREE)
3070 return NULL_TREE;
3071
3072 /* If we are jumping to a different function, we can't issue any
3073 useful warnings. */
3074 if (DECL_CONTEXT (label) != current_function_decl)
3075 {
3076 gcc_assert (C_DECLARED_LABEL_FLAG (label));
3077 return label;
3078 }
3079
3080 label_vars = I_LABEL_BINDING (name)->u.label;
3081
3082 /* If the label has not yet been defined, then push this goto on a
3083 list for possible later warnings. */
3084 if (label_vars->label_bindings.scope == NULL)
3085 {
3086 struct c_goto_bindings *g;
3087
3088 g = GGC_NEW (struct c_goto_bindings);
3089 g->loc = loc;
3090 set_spot_bindings (&g->goto_bindings, true);
3091 VEC_safe_push (c_goto_bindings_p, gc, label_vars->gotos, g);
3092 return label;
3093 }
3094
3095 /* If there are any decls in label_vars->decls_in_scope, then this
3096 goto has missed the declaration of the decl. This happens for a
3097 case like
3098 int i = 1;
3099 lab:
3100 ...
3101 goto lab;
3102 Issue a warning or error. */
3103 for (ix = 0; VEC_iterate (tree, label_vars->decls_in_scope, ix, decl); ++ix)
3104 warn_about_goto (loc, label, decl);
3105
3106 if (label_vars->label_bindings.left_stmt_expr)
3107 {
3108 error_at (loc, "jump into statement expression");
3109 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3110 }
3111
3112 return label;
3113 }
3114
3115 /* Make a label named NAME in the current function, shadowing silently
3116 any that may be inherited from containing functions or containing
3117 scopes. This is called for __label__ declarations. */
3118
3119 tree
3120 declare_label (tree name)
3121 {
3122 struct c_binding *b = I_LABEL_BINDING (name);
3123 tree label;
3124 struct c_label_vars *label_vars;
3125
3126 /* Check to make sure that the label hasn't already been declared
3127 at this scope */
3128 if (b && B_IN_CURRENT_SCOPE (b))
3129 {
3130 error ("duplicate label declaration %qE", name);
3131 locate_old_decl (b->decl);
3132
3133 /* Just use the previous declaration. */
3134 return b->decl;
3135 }
3136
3137 label = make_label (input_location, name, false, &label_vars);
3138 C_DECLARED_LABEL_FLAG (label) = 1;
3139
3140 /* Declared labels go in the current scope. */
3141 bind_label (name, label, current_scope, label_vars);
3142
3143 return label;
3144 }
3145
3146 /* When we define a label, issue any appropriate warnings if there are
3147 any gotos earlier in the function which jump to this label. */
3148
3149 static void
3150 check_earlier_gotos (tree label, struct c_label_vars* label_vars)
3151 {
3152 unsigned int ix;
3153 struct c_goto_bindings *g;
3154
3155 for (ix = 0;
3156 VEC_iterate (c_goto_bindings_p, label_vars->gotos, ix, g);
3157 ++ix)
3158 {
3159 struct c_binding *b;
3160 struct c_scope *scope;
3161
3162 /* We have a goto to this label. The goto is going forward. In
3163 g->scope, the goto is going to skip any binding which was
3164 defined after g->bindings_in_scope. */
3165 for (b = g->goto_bindings.scope->bindings;
3166 b != g->goto_bindings.bindings_in_scope;
3167 b = b->prev)
3168 {
3169 if (decl_jump_unsafe (b->decl))
3170 warn_about_goto (g->loc, label, b->decl);
3171 }
3172
3173 /* We also need to warn about decls defined in any scopes
3174 between the scope of the label and the scope of the goto. */
3175 for (scope = label_vars->label_bindings.scope;
3176 scope != g->goto_bindings.scope;
3177 scope = scope->outer)
3178 {
3179 gcc_assert (scope != NULL);
3180 if (scope == label_vars->label_bindings.scope)
3181 b = label_vars->label_bindings.bindings_in_scope;
3182 else
3183 b = scope->bindings;
3184 for (; b != NULL; b = b->prev)
3185 {
3186 if (decl_jump_unsafe (b->decl))
3187 warn_about_goto (g->loc, label, b->decl);
3188 }
3189 }
3190
3191 if (g->goto_bindings.stmt_exprs > 0)
3192 {
3193 error_at (g->loc, "jump into statement expression");
3194 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here",
3195 label);
3196 }
3197 }
3198
3199 /* Now that the label is defined, we will issue warnings about
3200 subsequent gotos to this label when we see them. */
3201 VEC_truncate (c_goto_bindings_p, label_vars->gotos, 0);
3202 label_vars->gotos = NULL;
3203 }
3204
3205 /* Define a label, specifying the location in the source file.
3206 Return the LABEL_DECL node for the label, if the definition is valid.
3207 Otherwise return 0. */
3208
3209 tree
3210 define_label (location_t location, tree name)
3211 {
3212 /* Find any preexisting label with this name. It is an error
3213 if that label has already been defined in this function, or
3214 if there is a containing function with a declared label with
3215 the same name. */
3216 tree label = I_LABEL_DECL (name);
3217
3218 if (label
3219 && ((DECL_CONTEXT (label) == current_function_decl
3220 && DECL_INITIAL (label) != 0)
3221 || (DECL_CONTEXT (label) != current_function_decl
3222 && C_DECLARED_LABEL_FLAG (label))))
3223 {
3224 error_at (location, "duplicate label %qD", label);
3225 locate_old_decl (label);
3226 return 0;
3227 }
3228 else if (label && DECL_CONTEXT (label) == current_function_decl)
3229 {
3230 struct c_label_vars *label_vars = I_LABEL_BINDING (name)->u.label;
3231
3232 /* The label has been used or declared already in this function,
3233 but not defined. Update its location to point to this
3234 definition. */
3235 DECL_SOURCE_LOCATION (label) = location;
3236 set_spot_bindings (&label_vars->label_bindings, true);
3237
3238 /* Issue warnings as required about any goto statements from
3239 earlier in the function. */
3240 check_earlier_gotos (label, label_vars);
3241 }
3242 else
3243 {
3244 struct c_label_vars *label_vars;
3245
3246 /* No label binding for that identifier; make one. */
3247 label = make_label (location, name, true, &label_vars);
3248
3249 /* Ordinary labels go in the current function scope. */
3250 bind_label (name, label, current_function_scope, label_vars);
3251 }
3252
3253 if (!in_system_header && lookup_name (name))
3254 warning_at (location, OPT_Wtraditional,
3255 "traditional C lacks a separate namespace "
3256 "for labels, identifier %qE conflicts", name);
3257
3258 /* Mark label as having been defined. */
3259 DECL_INITIAL (label) = error_mark_node;
3260 return label;
3261 }
3262 \f
3263 /* Get the bindings for a new switch statement. This is used to issue
3264 warnings as appropriate for jumps from the switch to case or
3265 default labels. */
3266
3267 struct c_spot_bindings *
3268 c_get_switch_bindings (void)
3269 {
3270 struct c_spot_bindings *switch_bindings;
3271
3272 switch_bindings = XNEW (struct c_spot_bindings);
3273 set_spot_bindings (switch_bindings, true);
3274 return switch_bindings;
3275 }
3276
3277 void
3278 c_release_switch_bindings (struct c_spot_bindings *bindings)
3279 {
3280 gcc_assert (bindings->stmt_exprs == 0 && !bindings->left_stmt_expr);
3281 XDELETE (bindings);
3282 }
3283
3284 /* This is called at the point of a case or default label to issue
3285 warnings about decls as needed. It returns true if it found an
3286 error, not just a warning. */
3287
3288 bool
3289 c_check_switch_jump_warnings (struct c_spot_bindings *switch_bindings,
3290 location_t switch_loc, location_t case_loc)
3291 {
3292 bool saw_error;
3293 struct c_scope *scope;
3294
3295 saw_error = false;
3296 for (scope = current_scope;
3297 scope != switch_bindings->scope;
3298 scope = scope->outer)
3299 {
3300 struct c_binding *b;
3301
3302 gcc_assert (scope != NULL);
3303 for (b = scope->bindings; b != NULL; b = b->prev)
3304 {
3305 if (decl_jump_unsafe (b->decl))
3306 {
3307 if (variably_modified_type_p (TREE_TYPE (b->decl), NULL_TREE))
3308 {
3309 saw_error = true;
3310 error_at (case_loc,
3311 ("switch jumps into scope of identifier with "
3312 "variably modified type"));
3313 }
3314 else
3315 warning_at (case_loc, OPT_Wjump_misses_init,
3316 "switch jumps over variable initialization");
3317 inform (switch_loc, "switch starts here");
3318 inform (DECL_SOURCE_LOCATION (b->decl), "%qD declared here",
3319 b->decl);
3320 }
3321 }
3322 }
3323
3324 if (switch_bindings->stmt_exprs > 0)
3325 {
3326 saw_error = true;
3327 error_at (case_loc, "switch jumps into statement expression");
3328 inform (switch_loc, "switch starts here");
3329 }
3330
3331 return saw_error;
3332 }
3333 \f
3334 /* Given NAME, an IDENTIFIER_NODE,
3335 return the structure (or union or enum) definition for that name.
3336 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
3337 CODE says which kind of type the caller wants;
3338 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
3339 If PLOC is not NULL and this returns non-null, it sets *PLOC to the
3340 location where the tag was defined.
3341 If the wrong kind of type is found, an error is reported. */
3342
3343 static tree
3344 lookup_tag (enum tree_code code, tree name, int thislevel_only,
3345 location_t *ploc)
3346 {
3347 struct c_binding *b = I_TAG_BINDING (name);
3348 int thislevel = 0;
3349
3350 if (!b || !b->decl)
3351 return 0;
3352
3353 /* We only care about whether it's in this level if
3354 thislevel_only was set or it might be a type clash. */
3355 if (thislevel_only || TREE_CODE (b->decl) != code)
3356 {
3357 /* For our purposes, a tag in the external scope is the same as
3358 a tag in the file scope. (Primarily relevant to Objective-C
3359 and its builtin structure tags, which get pushed before the
3360 file scope is created.) */
3361 if (B_IN_CURRENT_SCOPE (b)
3362 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
3363 thislevel = 1;
3364 }
3365
3366 if (thislevel_only && !thislevel)
3367 return 0;
3368
3369 if (TREE_CODE (b->decl) != code)
3370 {
3371 /* Definition isn't the kind we were looking for. */
3372 pending_invalid_xref = name;
3373 pending_invalid_xref_location = input_location;
3374
3375 /* If in the same binding level as a declaration as a tag
3376 of a different type, this must not be allowed to
3377 shadow that tag, so give the error immediately.
3378 (For example, "struct foo; union foo;" is invalid.) */
3379 if (thislevel)
3380 pending_xref_error ();
3381 }
3382
3383 if (ploc != NULL)
3384 *ploc = b->locus;
3385
3386 return b->decl;
3387 }
3388
3389 /* Print an error message now
3390 for a recent invalid struct, union or enum cross reference.
3391 We don't print them immediately because they are not invalid
3392 when used in the `struct foo;' construct for shadowing. */
3393
3394 void
3395 pending_xref_error (void)
3396 {
3397 if (pending_invalid_xref != 0)
3398 error_at (pending_invalid_xref_location, "%qE defined as wrong kind of tag",
3399 pending_invalid_xref);
3400 pending_invalid_xref = 0;
3401 }
3402
3403 \f
3404 /* Look up NAME in the current scope and its superiors
3405 in the namespace of variables, functions and typedefs.
3406 Return a ..._DECL node of some kind representing its definition,
3407 or return 0 if it is undefined. */
3408
3409 tree
3410 lookup_name (tree name)
3411 {
3412 struct c_binding *b = I_SYMBOL_BINDING (name);
3413 if (b && !b->invisible)
3414 return b->decl;
3415 return 0;
3416 }
3417
3418 /* Similar to `lookup_name' but look only at the indicated scope. */
3419
3420 static tree
3421 lookup_name_in_scope (tree name, struct c_scope *scope)
3422 {
3423 struct c_binding *b;
3424
3425 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
3426 if (B_IN_SCOPE (b, scope))
3427 return b->decl;
3428 return 0;
3429 }
3430 \f
3431 /* Create the predefined scalar types of C,
3432 and some nodes representing standard constants (0, 1, (void *) 0).
3433 Initialize the global scope.
3434 Make definitions for built-in primitive functions. */
3435
3436 void
3437 c_init_decl_processing (void)
3438 {
3439 location_t save_loc = input_location;
3440
3441 /* Initialize reserved words for parser. */
3442 c_parse_init ();
3443
3444 current_function_decl = 0;
3445
3446 gcc_obstack_init (&parser_obstack);
3447
3448 /* Make the externals scope. */
3449 push_scope ();
3450 external_scope = current_scope;
3451
3452 /* Declarations from c_common_nodes_and_builtins must not be associated
3453 with this input file, lest we get differences between using and not
3454 using preprocessed headers. */
3455 input_location = BUILTINS_LOCATION;
3456
3457 build_common_tree_nodes (flag_signed_char);
3458
3459 c_common_nodes_and_builtins ();
3460
3461 /* In C, comparisons and TRUTH_* expressions have type int. */
3462 truthvalue_type_node = integer_type_node;
3463 truthvalue_true_node = integer_one_node;
3464 truthvalue_false_node = integer_zero_node;
3465
3466 /* Even in C99, which has a real boolean type. */
3467 pushdecl (build_decl (UNKNOWN_LOCATION, TYPE_DECL, get_identifier ("_Bool"),
3468 boolean_type_node));
3469
3470 input_location = save_loc;
3471
3472 pedantic_lvalues = true;
3473
3474 make_fname_decl = c_make_fname_decl;
3475 start_fname_decls ();
3476 }
3477
3478 /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
3479 give the decl, NAME is the initialization string and TYPE_DEP
3480 indicates whether NAME depended on the type of the function. As we
3481 don't yet implement delayed emission of static data, we mark the
3482 decl as emitted so it is not placed in the output. Anything using
3483 it must therefore pull out the STRING_CST initializer directly.
3484 FIXME. */
3485
3486 static tree
3487 c_make_fname_decl (location_t loc, tree id, int type_dep)
3488 {
3489 const char *name = fname_as_string (type_dep);
3490 tree decl, type, init;
3491 size_t length = strlen (name);
3492
3493 type = build_array_type (char_type_node,
3494 build_index_type (size_int (length)));
3495 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
3496
3497 decl = build_decl (loc, VAR_DECL, id, type);
3498
3499 TREE_STATIC (decl) = 1;
3500 TREE_READONLY (decl) = 1;
3501 DECL_ARTIFICIAL (decl) = 1;
3502
3503 init = build_string (length + 1, name);
3504 free (CONST_CAST (char *, name));
3505 TREE_TYPE (init) = type;
3506 DECL_INITIAL (decl) = init;
3507
3508 TREE_USED (decl) = 1;
3509
3510 if (current_function_decl
3511 /* For invalid programs like this:
3512
3513 void foo()
3514 const char* p = __FUNCTION__;
3515
3516 the __FUNCTION__ is believed to appear in K&R style function
3517 parameter declarator. In that case we still don't have
3518 function_scope. */
3519 && (!errorcount || current_function_scope))
3520 {
3521 DECL_CONTEXT (decl) = current_function_decl;
3522 bind (id, decl, current_function_scope,
3523 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
3524 }
3525
3526 finish_decl (decl, loc, init, NULL_TREE, NULL_TREE);
3527
3528 return decl;
3529 }
3530
3531 tree
3532 c_builtin_function (tree decl)
3533 {
3534 tree type = TREE_TYPE (decl);
3535 tree id = DECL_NAME (decl);
3536
3537 const char *name = IDENTIFIER_POINTER (id);
3538 C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0);
3539
3540 /* Should never be called on a symbol with a preexisting meaning. */
3541 gcc_assert (!I_SYMBOL_BINDING (id));
3542
3543 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
3544 UNKNOWN_LOCATION);
3545
3546 /* Builtins in the implementation namespace are made visible without
3547 needing to be explicitly declared. See push_file_scope. */
3548 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
3549 {
3550 TREE_CHAIN (decl) = visible_builtins;
3551 visible_builtins = decl;
3552 }
3553
3554 return decl;
3555 }
3556
3557 tree
3558 c_builtin_function_ext_scope (tree decl)
3559 {
3560 tree type = TREE_TYPE (decl);
3561 tree id = DECL_NAME (decl);
3562
3563 const char *name = IDENTIFIER_POINTER (id);
3564 C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0);
3565
3566 /* Should never be called on a symbol with a preexisting meaning. */
3567 gcc_assert (!I_SYMBOL_BINDING (id));
3568
3569 bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
3570 UNKNOWN_LOCATION);
3571
3572 /* Builtins in the implementation namespace are made visible without
3573 needing to be explicitly declared. See push_file_scope. */
3574 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
3575 {
3576 TREE_CHAIN (decl) = visible_builtins;
3577 visible_builtins = decl;
3578 }
3579
3580 return decl;
3581 }
3582 \f
3583 /* Called when a declaration is seen that contains no names to declare.
3584 If its type is a reference to a structure, union or enum inherited
3585 from a containing scope, shadow that tag name for the current scope
3586 with a forward reference.
3587 If its type defines a new named structure or union
3588 or defines an enum, it is valid but we need not do anything here.
3589 Otherwise, it is an error. */
3590
3591 void
3592 shadow_tag (const struct c_declspecs *declspecs)
3593 {
3594 shadow_tag_warned (declspecs, 0);
3595 }
3596
3597 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
3598 but no pedwarn. */
3599 void
3600 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
3601 {
3602 bool found_tag = false;
3603
3604 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
3605 {
3606 tree value = declspecs->type;
3607 enum tree_code code = TREE_CODE (value);
3608
3609 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
3610 /* Used to test also that TYPE_SIZE (value) != 0.
3611 That caused warning for `struct foo;' at top level in the file. */
3612 {
3613 tree name = TYPE_NAME (value);
3614 tree t;
3615
3616 found_tag = true;
3617
3618 if (declspecs->restrict_p)
3619 {
3620 error ("invalid use of %<restrict%>");
3621 warned = 1;
3622 }
3623
3624 if (name == 0)
3625 {
3626 if (warned != 1 && code != ENUMERAL_TYPE)
3627 /* Empty unnamed enum OK */
3628 {
3629 pedwarn (input_location, 0,
3630 "unnamed struct/union that defines no instances");
3631 warned = 1;
3632 }
3633 }
3634 else if (!declspecs->tag_defined_p
3635 && declspecs->storage_class != csc_none)
3636 {
3637 if (warned != 1)
3638 pedwarn (input_location, 0,
3639 "empty declaration with storage class specifier "
3640 "does not redeclare tag");
3641 warned = 1;
3642 pending_xref_error ();
3643 }
3644 else if (!declspecs->tag_defined_p
3645 && (declspecs->const_p
3646 || declspecs->volatile_p
3647 || declspecs->restrict_p
3648 || declspecs->address_space))
3649 {
3650 if (warned != 1)
3651 pedwarn (input_location, 0,
3652 "empty declaration with type qualifier "
3653 "does not redeclare tag");
3654 warned = 1;
3655 pending_xref_error ();
3656 }
3657 else
3658 {
3659 pending_invalid_xref = 0;
3660 t = lookup_tag (code, name, 1, NULL);
3661
3662 if (t == 0)
3663 {
3664 t = make_node (code);
3665 pushtag (input_location, name, t);
3666 }
3667 }
3668 }
3669 else
3670 {
3671 if (warned != 1 && !in_system_header)
3672 {
3673 pedwarn (input_location, 0,
3674 "useless type name in empty declaration");
3675 warned = 1;
3676 }
3677 }
3678 }
3679 else if (warned != 1 && !in_system_header && declspecs->typedef_p)
3680 {
3681 pedwarn (input_location, 0, "useless type name in empty declaration");
3682 warned = 1;
3683 }
3684
3685 pending_invalid_xref = 0;
3686
3687 if (declspecs->inline_p)
3688 {
3689 error ("%<inline%> in empty declaration");
3690 warned = 1;
3691 }
3692
3693 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
3694 {
3695 error ("%<auto%> in file-scope empty declaration");
3696 warned = 1;
3697 }
3698
3699 if (current_scope == file_scope && declspecs->storage_class == csc_register)
3700 {
3701 error ("%<register%> in file-scope empty declaration");
3702 warned = 1;
3703 }
3704
3705 if (!warned && !in_system_header && declspecs->storage_class != csc_none)
3706 {
3707 warning (0, "useless storage class specifier in empty declaration");
3708 warned = 2;
3709 }
3710
3711 if (!warned && !in_system_header && declspecs->thread_p)
3712 {
3713 warning (0, "useless %<__thread%> in empty declaration");
3714 warned = 2;
3715 }
3716
3717 if (!warned && !in_system_header && (declspecs->const_p
3718 || declspecs->volatile_p
3719 || declspecs->restrict_p
3720 || declspecs->address_space))
3721 {
3722 warning (0, "useless type qualifier in empty declaration");
3723 warned = 2;
3724 }
3725
3726 if (warned != 1)
3727 {
3728 if (!found_tag)
3729 pedwarn (input_location, 0, "empty declaration");
3730 }
3731 }
3732 \f
3733
3734 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
3735 bits. SPECS represents declaration specifiers that the grammar
3736 only permits to contain type qualifiers and attributes. */
3737
3738 int
3739 quals_from_declspecs (const struct c_declspecs *specs)
3740 {
3741 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
3742 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
3743 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)
3744 | (ENCODE_QUAL_ADDR_SPACE (specs->address_space)));
3745 gcc_assert (!specs->type
3746 && !specs->decl_attr
3747 && specs->typespec_word == cts_none
3748 && specs->storage_class == csc_none
3749 && !specs->typedef_p
3750 && !specs->explicit_signed_p
3751 && !specs->deprecated_p
3752 && !specs->long_p
3753 && !specs->long_long_p
3754 && !specs->short_p
3755 && !specs->signed_p
3756 && !specs->unsigned_p
3757 && !specs->complex_p
3758 && !specs->inline_p
3759 && !specs->thread_p);
3760 return quals;
3761 }
3762
3763 /* Construct an array declarator. LOC is the location of the
3764 beginning of the array (usually the opening brace). EXPR is the
3765 expression inside [], or NULL_TREE. QUALS are the type qualifiers
3766 inside the [] (to be applied to the pointer to which a parameter
3767 array is converted). STATIC_P is true if "static" is inside the
3768 [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
3769 VLA of unspecified length which is nevertheless a complete type,
3770 false otherwise. The field for the contained declarator is left to
3771 be filled in by set_array_declarator_inner. */
3772
3773 struct c_declarator *
3774 build_array_declarator (location_t loc,
3775 tree expr, struct c_declspecs *quals, bool static_p,
3776 bool vla_unspec_p)
3777 {
3778 struct c_declarator *declarator = XOBNEW (&parser_obstack,
3779 struct c_declarator);
3780 declarator->id_loc = loc;
3781 declarator->kind = cdk_array;
3782 declarator->declarator = 0;
3783 declarator->u.array.dimen = expr;
3784 if (quals)
3785 {
3786 declarator->u.array.attrs = quals->attrs;
3787 declarator->u.array.quals = quals_from_declspecs (quals);
3788 }
3789 else
3790 {
3791 declarator->u.array.attrs = NULL_TREE;
3792 declarator->u.array.quals = 0;
3793 }
3794 declarator->u.array.static_p = static_p;
3795 declarator->u.array.vla_unspec_p = vla_unspec_p;
3796 if (!flag_isoc99)
3797 {
3798 if (static_p || quals != NULL)
3799 pedwarn (loc, OPT_pedantic,
3800 "ISO C90 does not support %<static%> or type "
3801 "qualifiers in parameter array declarators");
3802 if (vla_unspec_p)
3803 pedwarn (loc, OPT_pedantic,
3804 "ISO C90 does not support %<[*]%> array declarators");
3805 }
3806 if (vla_unspec_p)
3807 {
3808 if (!current_scope->parm_flag)
3809 {
3810 /* C99 6.7.5.2p4 */
3811 error_at (loc, "%<[*]%> not allowed in other than "
3812 "function prototype scope");
3813 declarator->u.array.vla_unspec_p = false;
3814 return NULL;
3815 }
3816 current_scope->had_vla_unspec = true;
3817 }
3818 return declarator;
3819 }
3820
3821 /* Set the contained declarator of an array declarator. DECL is the
3822 declarator, as constructed by build_array_declarator; INNER is what
3823 appears on the left of the []. */
3824
3825 struct c_declarator *
3826 set_array_declarator_inner (struct c_declarator *decl,
3827 struct c_declarator *inner)
3828 {
3829 decl->declarator = inner;
3830 return decl;
3831 }
3832
3833 /* INIT is a constructor that forms DECL's initializer. If the final
3834 element initializes a flexible array field, add the size of that
3835 initializer to DECL's size. */
3836
3837 static void
3838 add_flexible_array_elts_to_size (tree decl, tree init)
3839 {
3840 tree elt, type;
3841
3842 if (VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (init)))
3843 return;
3844
3845 elt = VEC_last (constructor_elt, CONSTRUCTOR_ELTS (init))->value;
3846 type = TREE_TYPE (elt);
3847 if (TREE_CODE (type) == ARRAY_TYPE
3848 && TYPE_SIZE (type) == NULL_TREE
3849 && TYPE_DOMAIN (type) != NULL_TREE
3850 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
3851 {
3852 complete_array_type (&type, elt, false);
3853 DECL_SIZE (decl)
3854 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
3855 DECL_SIZE_UNIT (decl)
3856 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
3857 }
3858 }
3859 \f
3860 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
3861 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
3862 before the type name, and set *EXPR_CONST_OPERANDS, if
3863 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
3864 appear in a constant expression. */
3865
3866 tree
3867 groktypename (struct c_type_name *type_name, tree *expr,
3868 bool *expr_const_operands)
3869 {
3870 tree type;
3871 tree attrs = type_name->specs->attrs;
3872
3873 type_name->specs->attrs = NULL_TREE;
3874
3875 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
3876 false, NULL, &attrs, expr, expr_const_operands,
3877 DEPRECATED_NORMAL);
3878
3879 /* Apply attributes. */
3880 decl_attributes (&type, attrs, 0);
3881
3882 return type;
3883 }
3884
3885 /* Decode a declarator in an ordinary declaration or data definition.
3886 This is called as soon as the type information and variable name
3887 have been parsed, before parsing the initializer if any.
3888 Here we create the ..._DECL node, fill in its type,
3889 and put it on the list of decls for the current context.
3890 The ..._DECL node is returned as the value.
3891
3892 Exception: for arrays where the length is not specified,
3893 the type is left null, to be filled in by `finish_decl'.
3894
3895 Function definitions do not come here; they go to start_function
3896 instead. However, external and forward declarations of functions
3897 do go through here. Structure field declarations are done by
3898 grokfield and not through here. */
3899
3900 tree
3901 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
3902 bool initialized, tree attributes)
3903 {
3904 tree decl;
3905 tree tem;
3906 tree expr = NULL_TREE;
3907 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
3908
3909 /* An object declared as __attribute__((deprecated)) suppresses
3910 warnings of uses of other deprecated items. */
3911 if (lookup_attribute ("deprecated", attributes))
3912 deprecated_state = DEPRECATED_SUPPRESS;
3913
3914 decl = grokdeclarator (declarator, declspecs,
3915 NORMAL, initialized, NULL, &attributes, &expr, NULL,
3916 deprecated_state);
3917 if (!decl)
3918 return 0;
3919
3920 if (expr)
3921 add_stmt (expr);
3922
3923 if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl)))
3924 warning (OPT_Wmain, "%q+D is usually a function", decl);
3925
3926 if (initialized)
3927 /* Is it valid for this decl to have an initializer at all?
3928 If not, set INITIALIZED to zero, which will indirectly
3929 tell 'finish_decl' to ignore the initializer once it is parsed. */
3930 switch (TREE_CODE (decl))
3931 {
3932 case TYPE_DECL:
3933 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
3934 initialized = 0;
3935 break;
3936
3937 case FUNCTION_DECL:
3938 error ("function %qD is initialized like a variable", decl);
3939 initialized = 0;
3940 break;
3941
3942 case PARM_DECL:
3943 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
3944 error ("parameter %qD is initialized", decl);
3945 initialized = 0;
3946 break;
3947
3948 default:
3949 /* Don't allow initializations for incomplete types except for
3950 arrays which might be completed by the initialization. */
3951
3952 /* This can happen if the array size is an undefined macro.
3953 We already gave a warning, so we don't need another one. */
3954 if (TREE_TYPE (decl) == error_mark_node)
3955 initialized = 0;
3956 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
3957 {
3958 /* A complete type is ok if size is fixed. */
3959
3960 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
3961 || C_DECL_VARIABLE_SIZE (decl))
3962 {
3963 error ("variable-sized object may not be initialized");
3964 initialized = 0;
3965 }
3966 }
3967 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
3968 {
3969 error ("variable %qD has initializer but incomplete type", decl);
3970 initialized = 0;
3971 }
3972 else if (C_DECL_VARIABLE_SIZE (decl))
3973 {
3974 /* Although C99 is unclear about whether incomplete arrays
3975 of VLAs themselves count as VLAs, it does not make
3976 sense to permit them to be initialized given that
3977 ordinary VLAs may not be initialized. */
3978 error ("variable-sized object may not be initialized");
3979 initialized = 0;
3980 }
3981 }
3982
3983 if (initialized)
3984 {
3985 if (current_scope == file_scope)
3986 TREE_STATIC (decl) = 1;
3987
3988 /* Tell 'pushdecl' this is an initialized decl
3989 even though we don't yet have the initializer expression.
3990 Also tell 'finish_decl' it may store the real initializer. */
3991 DECL_INITIAL (decl) = error_mark_node;
3992 }
3993
3994 /* If this is a function declaration, write a record describing it to the
3995 prototypes file (if requested). */
3996
3997 if (TREE_CODE (decl) == FUNCTION_DECL)
3998 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
3999
4000 /* ANSI specifies that a tentative definition which is not merged with
4001 a non-tentative definition behaves exactly like a definition with an
4002 initializer equal to zero. (Section 3.7.2)
4003
4004 -fno-common gives strict ANSI behavior, though this tends to break
4005 a large body of code that grew up without this rule.
4006
4007 Thread-local variables are never common, since there's no entrenched
4008 body of code to break, and it allows more efficient variable references
4009 in the presence of dynamic linking. */
4010
4011 if (TREE_CODE (decl) == VAR_DECL
4012 && !initialized
4013 && TREE_PUBLIC (decl)
4014 && !DECL_THREAD_LOCAL_P (decl)
4015 && !flag_no_common)
4016 DECL_COMMON (decl) = 1;
4017
4018 /* Set attributes here so if duplicate decl, will have proper attributes. */
4019 decl_attributes (&decl, attributes, 0);
4020
4021 /* Handle gnu_inline attribute. */
4022 if (declspecs->inline_p
4023 && !flag_gnu89_inline
4024 && TREE_CODE (decl) == FUNCTION_DECL
4025 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
4026 || current_function_decl))
4027 {
4028 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
4029 ;
4030 else if (declspecs->storage_class != csc_static)
4031 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
4032 }
4033
4034 if (TREE_CODE (decl) == FUNCTION_DECL
4035 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
4036 {
4037 struct c_declarator *ce = declarator;
4038
4039 if (ce->kind == cdk_pointer)
4040 ce = declarator->declarator;
4041 if (ce->kind == cdk_function)
4042 {
4043 tree args = ce->u.arg_info->parms;
4044 for (; args; args = TREE_CHAIN (args))
4045 {
4046 tree type = TREE_TYPE (args);
4047 if (type && INTEGRAL_TYPE_P (type)
4048 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4049 DECL_ARG_TYPE (args) = integer_type_node;
4050 }
4051 }
4052 }
4053
4054 if (TREE_CODE (decl) == FUNCTION_DECL
4055 && DECL_DECLARED_INLINE_P (decl)
4056 && DECL_UNINLINABLE (decl)
4057 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
4058 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
4059 decl);
4060
4061 /* C99 6.7.4p3: An inline definition of a function with external
4062 linkage shall not contain a definition of a modifiable object
4063 with static storage duration... */
4064 if (TREE_CODE (decl) == VAR_DECL
4065 && current_scope != file_scope
4066 && TREE_STATIC (decl)
4067 && !TREE_READONLY (decl)
4068 && DECL_DECLARED_INLINE_P (current_function_decl)
4069 && DECL_EXTERNAL (current_function_decl))
4070 record_inline_static (input_location, current_function_decl,
4071 decl, csi_modifiable);
4072
4073 /* Add this decl to the current scope.
4074 TEM may equal DECL or it may be a previous decl of the same name. */
4075 tem = pushdecl (decl);
4076
4077 if (initialized && DECL_EXTERNAL (tem))
4078 {
4079 DECL_EXTERNAL (tem) = 0;
4080 TREE_STATIC (tem) = 1;
4081 }
4082
4083 return tem;
4084 }
4085
4086 /* Finish processing of a declaration;
4087 install its initial value.
4088 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
4089 If the length of an array type is not known before,
4090 it must be determined now, from the initial value, or it is an error.
4091
4092 INIT_LOC is the location of the initial value. */
4093
4094 void
4095 finish_decl (tree decl, location_t init_loc, tree init,
4096 tree origtype, tree asmspec_tree)
4097 {
4098 tree type;
4099 bool was_incomplete = (DECL_SIZE (decl) == 0);
4100 const char *asmspec = 0;
4101
4102 /* If a name was specified, get the string. */
4103 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
4104 && DECL_FILE_SCOPE_P (decl))
4105 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
4106 if (asmspec_tree)
4107 asmspec = TREE_STRING_POINTER (asmspec_tree);
4108
4109 if (TREE_CODE (decl) == VAR_DECL
4110 && TREE_STATIC (decl)
4111 && global_bindings_p ())
4112 /* So decl is a global variable. Record the types it uses
4113 so that we can decide later to emit debug info for them. */
4114 record_types_used_by_current_var_decl (decl);
4115
4116 /* If `start_decl' didn't like having an initialization, ignore it now. */
4117 if (init != 0 && DECL_INITIAL (decl) == 0)
4118 init = 0;
4119
4120 /* Don't crash if parm is initialized. */
4121 if (TREE_CODE (decl) == PARM_DECL)
4122 init = 0;
4123
4124 if (init)
4125 store_init_value (init_loc, decl, init, origtype);
4126
4127 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
4128 || TREE_CODE (decl) == FUNCTION_DECL
4129 || TREE_CODE (decl) == FIELD_DECL))
4130 objc_check_decl (decl);
4131
4132 type = TREE_TYPE (decl);
4133
4134 /* Deduce size of array from initialization, if not already known. */
4135 if (TREE_CODE (type) == ARRAY_TYPE
4136 && TYPE_DOMAIN (type) == 0
4137 && TREE_CODE (decl) != TYPE_DECL)
4138 {
4139 bool do_default
4140 = (TREE_STATIC (decl)
4141 /* Even if pedantic, an external linkage array
4142 may have incomplete type at first. */
4143 ? pedantic && !TREE_PUBLIC (decl)
4144 : !DECL_EXTERNAL (decl));
4145 int failure
4146 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
4147 do_default);
4148
4149 /* Get the completed type made by complete_array_type. */
4150 type = TREE_TYPE (decl);
4151
4152 switch (failure)
4153 {
4154 case 1:
4155 error ("initializer fails to determine size of %q+D", decl);
4156 break;
4157
4158 case 2:
4159 if (do_default)
4160 error ("array size missing in %q+D", decl);
4161 /* If a `static' var's size isn't known,
4162 make it extern as well as static, so it does not get
4163 allocated.
4164 If it is not `static', then do not mark extern;
4165 finish_incomplete_decl will give it a default size
4166 and it will get allocated. */
4167 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
4168 DECL_EXTERNAL (decl) = 1;
4169 break;
4170
4171 case 3:
4172 error ("zero or negative size array %q+D", decl);
4173 break;
4174
4175 case 0:
4176 /* For global variables, update the copy of the type that
4177 exists in the binding. */
4178 if (TREE_PUBLIC (decl))
4179 {
4180 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
4181 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
4182 b_ext = b_ext->shadowed;
4183 if (b_ext)
4184 {
4185 if (b_ext->u.type)
4186 b_ext->u.type = composite_type (b_ext->u.type, type);
4187 else
4188 b_ext->u.type = type;
4189 }
4190 }
4191 break;
4192
4193 default:
4194 gcc_unreachable ();
4195 }
4196
4197 if (DECL_INITIAL (decl))
4198 TREE_TYPE (DECL_INITIAL (decl)) = type;
4199
4200 layout_decl (decl, 0);
4201 }
4202
4203 if (TREE_CODE (decl) == VAR_DECL)
4204 {
4205 if (init && TREE_CODE (init) == CONSTRUCTOR)
4206 add_flexible_array_elts_to_size (decl, init);
4207
4208 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
4209 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
4210 layout_decl (decl, 0);
4211
4212 if (DECL_SIZE (decl) == 0
4213 /* Don't give an error if we already gave one earlier. */
4214 && TREE_TYPE (decl) != error_mark_node
4215 && (TREE_STATIC (decl)
4216 /* A static variable with an incomplete type
4217 is an error if it is initialized.
4218 Also if it is not file scope.
4219 Otherwise, let it through, but if it is not `extern'
4220 then it may cause an error message later. */
4221 ? (DECL_INITIAL (decl) != 0
4222 || !DECL_FILE_SCOPE_P (decl))
4223 /* An automatic variable with an incomplete type
4224 is an error. */
4225 : !DECL_EXTERNAL (decl)))
4226 {
4227 error ("storage size of %q+D isn%'t known", decl);
4228 TREE_TYPE (decl) = error_mark_node;
4229 }
4230
4231 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
4232 && DECL_SIZE (decl) != 0)
4233 {
4234 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
4235 constant_expression_warning (DECL_SIZE (decl));
4236 else
4237 {
4238 error ("storage size of %q+D isn%'t constant", decl);
4239 TREE_TYPE (decl) = error_mark_node;
4240 }
4241 }
4242
4243 if (TREE_USED (type))
4244 {
4245 TREE_USED (decl) = 1;
4246 DECL_READ_P (decl) = 1;
4247 }
4248 }
4249
4250 /* If this is a function and an assembler name is specified, reset DECL_RTL
4251 so we can give it its new name. Also, update built_in_decls if it
4252 was a normal built-in. */
4253 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
4254 {
4255 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
4256 set_builtin_user_assembler_name (decl, asmspec);
4257 set_user_assembler_name (decl, asmspec);
4258 }
4259
4260 /* If #pragma weak was used, mark the decl weak now. */
4261 maybe_apply_pragma_weak (decl);
4262
4263 /* Output the assembler code and/or RTL code for variables and functions,
4264 unless the type is an undefined structure or union.
4265 If not, it will get done when the type is completed. */
4266
4267 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
4268 {
4269 /* Determine the ELF visibility. */
4270 if (TREE_PUBLIC (decl))
4271 c_determine_visibility (decl);
4272
4273 /* This is a no-op in c-lang.c or something real in objc-act.c. */
4274 if (c_dialect_objc ())
4275 objc_check_decl (decl);
4276
4277 if (asmspec)
4278 {
4279 /* If this is not a static variable, issue a warning.
4280 It doesn't make any sense to give an ASMSPEC for an
4281 ordinary, non-register local variable. Historically,
4282 GCC has accepted -- but ignored -- the ASMSPEC in
4283 this case. */
4284 if (!DECL_FILE_SCOPE_P (decl)
4285 && TREE_CODE (decl) == VAR_DECL
4286 && !C_DECL_REGISTER (decl)
4287 && !TREE_STATIC (decl))
4288 warning (0, "ignoring asm-specifier for non-static local "
4289 "variable %q+D", decl);
4290 else
4291 set_user_assembler_name (decl, asmspec);
4292 }
4293
4294 if (DECL_FILE_SCOPE_P (decl))
4295 {
4296 if (DECL_INITIAL (decl) == NULL_TREE
4297 || DECL_INITIAL (decl) == error_mark_node)
4298 /* Don't output anything
4299 when a tentative file-scope definition is seen.
4300 But at end of compilation, do output code for them. */
4301 DECL_DEFER_OUTPUT (decl) = 1;
4302 rest_of_decl_compilation (decl, true, 0);
4303 }
4304 else
4305 {
4306 /* In conjunction with an ASMSPEC, the `register'
4307 keyword indicates that we should place the variable
4308 in a particular register. */
4309 if (asmspec && C_DECL_REGISTER (decl))
4310 {
4311 DECL_HARD_REGISTER (decl) = 1;
4312 /* This cannot be done for a structure with volatile
4313 fields, on which DECL_REGISTER will have been
4314 reset. */
4315 if (!DECL_REGISTER (decl))
4316 error ("cannot put object with volatile field into register");
4317 }
4318
4319 if (TREE_CODE (decl) != FUNCTION_DECL)
4320 {
4321 /* If we're building a variable sized type, and we might be
4322 reachable other than via the top of the current binding
4323 level, then create a new BIND_EXPR so that we deallocate
4324 the object at the right time. */
4325 /* Note that DECL_SIZE can be null due to errors. */
4326 if (DECL_SIZE (decl)
4327 && !TREE_CONSTANT (DECL_SIZE (decl))
4328 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
4329 {
4330 tree bind;
4331 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
4332 TREE_SIDE_EFFECTS (bind) = 1;
4333 add_stmt (bind);
4334 BIND_EXPR_BODY (bind) = push_stmt_list ();
4335 }
4336 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl),
4337 DECL_EXPR, decl));
4338 }
4339 }
4340
4341
4342 if (!DECL_FILE_SCOPE_P (decl))
4343 {
4344 /* Recompute the RTL of a local array now
4345 if it used to be an incomplete type. */
4346 if (was_incomplete
4347 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
4348 {
4349 /* If we used it already as memory, it must stay in memory. */
4350 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
4351 /* If it's still incomplete now, no init will save it. */
4352 if (DECL_SIZE (decl) == 0)
4353 DECL_INITIAL (decl) = 0;
4354 }
4355 }
4356 }
4357
4358 if (TREE_CODE (decl) == TYPE_DECL)
4359 {
4360 if (!DECL_FILE_SCOPE_P (decl)
4361 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
4362 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
4363
4364 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
4365 }
4366
4367 /* At the end of a declaration, throw away any variable type sizes
4368 of types defined inside that declaration. There is no use
4369 computing them in the following function definition. */
4370 if (current_scope == file_scope)
4371 get_pending_sizes ();
4372
4373 /* Install a cleanup (aka destructor) if one was given. */
4374 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
4375 {
4376 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
4377 if (attr)
4378 {
4379 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
4380 tree cleanup_decl = lookup_name (cleanup_id);
4381 tree cleanup;
4382 VEC(tree,gc) *vec;
4383
4384 /* Build "cleanup(&decl)" for the destructor. */
4385 cleanup = build_unary_op (input_location, ADDR_EXPR, decl, 0);
4386 vec = VEC_alloc (tree, gc, 1);
4387 VEC_quick_push (tree, vec, cleanup);
4388 cleanup = build_function_call_vec (DECL_SOURCE_LOCATION (decl),
4389 cleanup_decl, vec, NULL);
4390 VEC_free (tree, gc, vec);
4391
4392 /* Don't warn about decl unused; the cleanup uses it. */
4393 TREE_USED (decl) = 1;
4394 TREE_USED (cleanup_decl) = 1;
4395 DECL_READ_P (decl) = 1;
4396
4397 push_cleanup (decl, cleanup, false);
4398 }
4399 }
4400
4401 if (warn_cxx_compat
4402 && TREE_CODE (decl) == VAR_DECL
4403 && TREE_READONLY (decl)
4404 && !DECL_EXTERNAL (decl)
4405 && DECL_INITIAL (decl) == NULL_TREE)
4406 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
4407 "uninitialized const %qD is invalid in C++", decl);
4408 }
4409
4410 /* Given a parsed parameter declaration, decode it into a PARM_DECL. */
4411
4412 tree
4413 grokparm (const struct c_parm *parm)
4414 {
4415 tree attrs = parm->attrs;
4416 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
4417 NULL, &attrs, NULL, NULL, DEPRECATED_NORMAL);
4418
4419 decl_attributes (&decl, attrs, 0);
4420
4421 return decl;
4422 }
4423
4424 /* Given a parsed parameter declaration, decode it into a PARM_DECL
4425 and push that on the current scope. */
4426
4427 void
4428 push_parm_decl (const struct c_parm *parm)
4429 {
4430 tree attrs = parm->attrs;
4431 tree decl;
4432
4433 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
4434 &attrs, NULL, NULL, DEPRECATED_NORMAL);
4435 decl_attributes (&decl, attrs, 0);
4436
4437 decl = pushdecl (decl);
4438
4439 finish_decl (decl, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
4440 }
4441
4442 /* Mark all the parameter declarations to date as forward decls.
4443 Also diagnose use of this extension. */
4444
4445 void
4446 mark_forward_parm_decls (void)
4447 {
4448 struct c_binding *b;
4449
4450 if (pedantic && !current_scope->warned_forward_parm_decls)
4451 {
4452 pedwarn (input_location, OPT_pedantic,
4453 "ISO C forbids forward parameter declarations");
4454 current_scope->warned_forward_parm_decls = true;
4455 }
4456
4457 for (b = current_scope->bindings; b; b = b->prev)
4458 if (TREE_CODE (b->decl) == PARM_DECL)
4459 TREE_ASM_WRITTEN (b->decl) = 1;
4460 }
4461 \f
4462 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
4463 literal, which may be an incomplete array type completed by the
4464 initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
4465 literal. NON_CONST is true if the initializers contain something
4466 that cannot occur in a constant expression. */
4467
4468 tree
4469 build_compound_literal (location_t loc, tree type, tree init, bool non_const)
4470 {
4471 /* We do not use start_decl here because we have a type, not a declarator;
4472 and do not use finish_decl because the decl should be stored inside
4473 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
4474 tree decl;
4475 tree complit;
4476 tree stmt;
4477
4478 if (type == error_mark_node
4479 || init == error_mark_node)
4480 return error_mark_node;
4481
4482 decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
4483 DECL_EXTERNAL (decl) = 0;
4484 TREE_PUBLIC (decl) = 0;
4485 TREE_STATIC (decl) = (current_scope == file_scope);
4486 DECL_CONTEXT (decl) = current_function_decl;
4487 TREE_USED (decl) = 1;
4488 DECL_READ_P (decl) = 1;
4489 TREE_TYPE (decl) = type;
4490 TREE_READONLY (decl) = TYPE_READONLY (type);
4491 store_init_value (loc, decl, init, NULL_TREE);
4492
4493 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
4494 {
4495 int failure = complete_array_type (&TREE_TYPE (decl),
4496 DECL_INITIAL (decl), true);
4497 gcc_assert (!failure);
4498
4499 type = TREE_TYPE (decl);
4500 TREE_TYPE (DECL_INITIAL (decl)) = type;
4501 }
4502
4503 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
4504 return error_mark_node;
4505
4506 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
4507 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
4508 TREE_SIDE_EFFECTS (complit) = 1;
4509
4510 layout_decl (decl, 0);
4511
4512 if (TREE_STATIC (decl))
4513 {
4514 /* This decl needs a name for the assembler output. */
4515 set_compound_literal_name (decl);
4516 DECL_DEFER_OUTPUT (decl) = 1;
4517 DECL_COMDAT (decl) = 1;
4518 DECL_ARTIFICIAL (decl) = 1;
4519 DECL_IGNORED_P (decl) = 1;
4520 pushdecl (decl);
4521 rest_of_decl_compilation (decl, 1, 0);
4522 }
4523
4524 if (non_const)
4525 {
4526 complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
4527 C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
4528 }
4529
4530 return complit;
4531 }
4532
4533 /* Check the type of a compound literal. Here we just check that it
4534 is valid for C++. */
4535
4536 void
4537 check_compound_literal_type (location_t loc, struct c_type_name *type_name)
4538 {
4539 if (warn_cxx_compat && type_name->specs->tag_defined_p)
4540 warning_at (loc, OPT_Wc___compat,
4541 "defining a type in a compound literal is invalid in C++");
4542 }
4543 \f
4544 /* Determine whether TYPE is a structure with a flexible array member,
4545 or a union containing such a structure (possibly recursively). */
4546
4547 static bool
4548 flexible_array_type_p (tree type)
4549 {
4550 tree x;
4551 switch (TREE_CODE (type))
4552 {
4553 case RECORD_TYPE:
4554 x = TYPE_FIELDS (type);
4555 if (x == NULL_TREE)
4556 return false;
4557 while (TREE_CHAIN (x) != NULL_TREE)
4558 x = TREE_CHAIN (x);
4559 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
4560 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
4561 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
4562 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
4563 return true;
4564 return false;
4565 case UNION_TYPE:
4566 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
4567 {
4568 if (flexible_array_type_p (TREE_TYPE (x)))
4569 return true;
4570 }
4571 return false;
4572 default:
4573 return false;
4574 }
4575 }
4576 \f
4577 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
4578 replacing with appropriate values if they are invalid. */
4579 static void
4580 check_bitfield_type_and_width (tree *type, tree *width, tree orig_name)
4581 {
4582 tree type_mv;
4583 unsigned int max_width;
4584 unsigned HOST_WIDE_INT w;
4585 const char *name = (orig_name
4586 ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
4587 : _("<anonymous>"));
4588
4589 /* Detect and ignore out of range field width and process valid
4590 field widths. */
4591 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)))
4592 {
4593 error ("bit-field %qs width not an integer constant", name);
4594 *width = integer_one_node;
4595 }
4596 else
4597 {
4598 if (TREE_CODE (*width) != INTEGER_CST)
4599 {
4600 *width = c_fully_fold (*width, false, NULL);
4601 if (TREE_CODE (*width) == INTEGER_CST)
4602 pedwarn (input_location, OPT_pedantic,
4603 "bit-field %qs width not an integer constant expression",
4604 name);
4605 }
4606 if (TREE_CODE (*width) != INTEGER_CST)
4607 {
4608 error ("bit-field %qs width not an integer constant", name);
4609 *width = integer_one_node;
4610 }
4611 constant_expression_warning (*width);
4612 if (tree_int_cst_sgn (*width) < 0)
4613 {
4614 error ("negative width in bit-field %qs", name);
4615 *width = integer_one_node;
4616 }
4617 else if (integer_zerop (*width) && orig_name)
4618 {
4619 error ("zero width for bit-field %qs", name);
4620 *width = integer_one_node;
4621 }
4622 }
4623
4624 /* Detect invalid bit-field type. */
4625 if (TREE_CODE (*type) != INTEGER_TYPE
4626 && TREE_CODE (*type) != BOOLEAN_TYPE
4627 && TREE_CODE (*type) != ENUMERAL_TYPE)
4628 {
4629 error ("bit-field %qs has invalid type", name);
4630 *type = unsigned_type_node;
4631 }
4632
4633 type_mv = TYPE_MAIN_VARIANT (*type);
4634 if (!in_system_header
4635 && type_mv != integer_type_node
4636 && type_mv != unsigned_type_node
4637 && type_mv != boolean_type_node)
4638 pedwarn (input_location, OPT_pedantic,
4639 "type of bit-field %qs is a GCC extension", name);
4640
4641 max_width = TYPE_PRECISION (*type);
4642
4643 if (0 < compare_tree_int (*width, max_width))
4644 {
4645 error ("width of %qs exceeds its type", name);
4646 w = max_width;
4647 *width = build_int_cst (NULL_TREE, w);
4648 }
4649 else
4650 w = tree_low_cst (*width, 1);
4651
4652 if (TREE_CODE (*type) == ENUMERAL_TYPE)
4653 {
4654 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
4655 if (!lt
4656 || w < tree_int_cst_min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
4657 || w < tree_int_cst_min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
4658 warning (0, "%qs is narrower than values of its type", name);
4659 }
4660 }
4661
4662 \f
4663
4664 /* Print warning about variable length array if necessary. */
4665
4666 static void
4667 warn_variable_length_array (tree name, tree size)
4668 {
4669 int const_size = TREE_CONSTANT (size);
4670
4671 if (!flag_isoc99 && pedantic && warn_vla != 0)
4672 {
4673 if (const_size)
4674 {
4675 if (name)
4676 pedwarn (input_location, OPT_Wvla,
4677 "ISO C90 forbids array %qE whose size "
4678 "can%'t be evaluated",
4679 name);
4680 else
4681 pedwarn (input_location, OPT_Wvla, "ISO C90 forbids array whose size "
4682 "can%'t be evaluated");
4683 }
4684 else
4685 {
4686 if (name)
4687 pedwarn (input_location, OPT_Wvla,
4688 "ISO C90 forbids variable length array %qE",
4689 name);
4690 else
4691 pedwarn (input_location, OPT_Wvla, "ISO C90 forbids variable length array");
4692 }
4693 }
4694 else if (warn_vla > 0)
4695 {
4696 if (const_size)
4697 {
4698 if (name)
4699 warning (OPT_Wvla,
4700 "the size of array %qE can"
4701 "%'t be evaluated", name);
4702 else
4703 warning (OPT_Wvla,
4704 "the size of array can %'t be evaluated");
4705 }
4706 else
4707 {
4708 if (name)
4709 warning (OPT_Wvla,
4710 "variable length array %qE is used",
4711 name);
4712 else
4713 warning (OPT_Wvla,
4714 "variable length array is used");
4715 }
4716 }
4717 }
4718
4719 /* Given a size SIZE that may not be a constant, return a SAVE_EXPR to
4720 serve as the actual size-expression for a type or decl. This is
4721 like variable_size in stor-layout.c, but we make global_bindings_p
4722 return negative to avoid calls to that function from outside the
4723 front end resulting in errors at file scope, then call this version
4724 instead from front-end code. */
4725
4726 static tree
4727 c_variable_size (tree size)
4728 {
4729 tree save;
4730
4731 if (TREE_CONSTANT (size))
4732 return size;
4733
4734 size = save_expr (size);
4735
4736 save = skip_simple_arithmetic (size);
4737
4738 if (cfun && cfun->dont_save_pending_sizes_p)
4739 return size;
4740
4741 if (!global_bindings_p ())
4742 put_pending_size (save);
4743
4744 return size;
4745 }
4746
4747 /* Given declspecs and a declarator,
4748 determine the name and type of the object declared
4749 and construct a ..._DECL node for it.
4750 (In one case we can return a ..._TYPE node instead.
4751 For invalid input we sometimes return 0.)
4752
4753 DECLSPECS is a c_declspecs structure for the declaration specifiers.
4754
4755 DECL_CONTEXT says which syntactic context this declaration is in:
4756 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
4757 FUNCDEF for a function definition. Like NORMAL but a few different
4758 error messages in each case. Return value may be zero meaning
4759 this definition is too screwy to try to parse.
4760 PARM for a parameter declaration (either within a function prototype
4761 or before a function body). Make a PARM_DECL, or return void_type_node.
4762 TYPENAME if for a typename (in a cast or sizeof).
4763 Don't make a DECL node; just return the ..._TYPE node.
4764 FIELD for a struct or union field; make a FIELD_DECL.
4765 INITIALIZED is true if the decl has an initializer.
4766 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
4767 representing the width of the bit-field.
4768 DECL_ATTRS points to the list of attributes that should be added to this
4769 decl. Any nested attributes that belong on the decl itself will be
4770 added to this list.
4771 If EXPR is not NULL, any expressions that need to be evaluated as
4772 part of evaluating variably modified types will be stored in *EXPR.
4773 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
4774 set to indicate whether operands in *EXPR can be used in constant
4775 expressions.
4776 DEPRECATED_STATE is a deprecated_states value indicating whether
4777 deprecation warnings should be suppressed.
4778
4779 In the TYPENAME case, DECLARATOR is really an absolute declarator.
4780 It may also be so in the PARM case, for a prototype where the
4781 argument type is specified but not the name.
4782
4783 This function is where the complicated C meanings of `static'
4784 and `extern' are interpreted. */
4785
4786 static tree
4787 grokdeclarator (const struct c_declarator *declarator,
4788 struct c_declspecs *declspecs,
4789 enum decl_context decl_context, bool initialized, tree *width,
4790 tree *decl_attrs, tree *expr, bool *expr_const_operands,
4791 enum deprecated_states deprecated_state)
4792 {
4793 tree type = declspecs->type;
4794 bool threadp = declspecs->thread_p;
4795 enum c_storage_class storage_class = declspecs->storage_class;
4796 int constp;
4797 int restrictp;
4798 int volatilep;
4799 int type_quals = TYPE_UNQUALIFIED;
4800 tree name = NULL_TREE;
4801 bool funcdef_flag = false;
4802 bool funcdef_syntax = false;
4803 bool size_varies = false;
4804 tree decl_attr = declspecs->decl_attr;
4805 int array_ptr_quals = TYPE_UNQUALIFIED;
4806 tree array_ptr_attrs = NULL_TREE;
4807 int array_parm_static = 0;
4808 bool array_parm_vla_unspec_p = false;
4809 tree returned_attrs = NULL_TREE;
4810 bool bitfield = width != NULL;
4811 tree element_type;
4812 struct c_arg_info *arg_info = 0;
4813 addr_space_t as1, as2, address_space;
4814 location_t loc = UNKNOWN_LOCATION;
4815 const char *errmsg;
4816 tree expr_dummy;
4817 bool expr_const_operands_dummy;
4818
4819 if (expr == NULL)
4820 expr = &expr_dummy;
4821 if (expr_const_operands == NULL)
4822 expr_const_operands = &expr_const_operands_dummy;
4823
4824 *expr = declspecs->expr;
4825 *expr_const_operands = declspecs->expr_const_operands;
4826
4827 if (decl_context == FUNCDEF)
4828 funcdef_flag = true, decl_context = NORMAL;
4829
4830 /* Look inside a declarator for the name being declared
4831 and get it as an IDENTIFIER_NODE, for an error message. */
4832 {
4833 const struct c_declarator *decl = declarator;
4834
4835 while (decl)
4836 switch (decl->kind)
4837 {
4838 case cdk_array:
4839 loc = decl->id_loc;
4840 /* FALL THRU. */
4841
4842 case cdk_function:
4843 case cdk_pointer:
4844 funcdef_syntax = (decl->kind == cdk_function);
4845 decl = decl->declarator;
4846 break;
4847
4848 case cdk_attrs:
4849 decl = decl->declarator;
4850 break;
4851
4852 case cdk_id:
4853 loc = decl->id_loc;
4854 if (decl->u.id)
4855 name = decl->u.id;
4856 decl = 0;
4857 break;
4858
4859 default:
4860 gcc_unreachable ();
4861 }
4862 if (name == 0)
4863 {
4864 gcc_assert (decl_context == PARM
4865 || decl_context == TYPENAME
4866 || (decl_context == FIELD
4867 && declarator->kind == cdk_id));
4868 gcc_assert (!initialized);
4869 }
4870 }
4871
4872 /* A function definition's declarator must have the form of
4873 a function declarator. */
4874
4875 if (funcdef_flag && !funcdef_syntax)
4876 return 0;
4877
4878 /* If this looks like a function definition, make it one,
4879 even if it occurs where parms are expected.
4880 Then store_parm_decls will reject it and not use it as a parm. */
4881 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
4882 decl_context = PARM;
4883
4884 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
4885 warn_deprecated_use (declspecs->type, declspecs->decl_attr);
4886
4887 if ((decl_context == NORMAL || decl_context == FIELD)
4888 && current_scope == file_scope
4889 && variably_modified_type_p (type, NULL_TREE))
4890 {
4891 if (name)
4892 error_at (loc, "variably modified %qE at file scope", name);
4893 else
4894 error_at (loc, "variably modified field at file scope");
4895 type = integer_type_node;
4896 }
4897
4898 size_varies = C_TYPE_VARIABLE_SIZE (type) != 0;
4899
4900 /* Diagnose defaulting to "int". */
4901
4902 if (declspecs->default_int_p && !in_system_header)
4903 {
4904 /* Issue a warning if this is an ISO C 99 program or if
4905 -Wreturn-type and this is a function, or if -Wimplicit;
4906 prefer the former warning since it is more explicit. */
4907 if ((warn_implicit_int || warn_return_type || flag_isoc99)
4908 && funcdef_flag)
4909 warn_about_return_type = 1;
4910 else
4911 {
4912 if (name)
4913 pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wimplicit_int,
4914 "type defaults to %<int%> in declaration of %qE",
4915 name);
4916 else
4917 pedwarn_c99 (input_location, flag_isoc99 ? 0 : OPT_Wimplicit_int,
4918 "type defaults to %<int%> in type name");
4919 }
4920 }
4921
4922 /* Adjust the type if a bit-field is being declared,
4923 -funsigned-bitfields applied and the type is not explicitly
4924 "signed". */
4925 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
4926 && TREE_CODE (type) == INTEGER_TYPE)
4927 type = unsigned_type_for (type);
4928
4929 /* Figure out the type qualifiers for the declaration. There are
4930 two ways a declaration can become qualified. One is something
4931 like `const int i' where the `const' is explicit. Another is
4932 something like `typedef const int CI; CI i' where the type of the
4933 declaration contains the `const'. A third possibility is that
4934 there is a type qualifier on the element type of a typedefed
4935 array type, in which case we should extract that qualifier so
4936 that c_apply_type_quals_to_decl receives the full list of
4937 qualifiers to work with (C90 is not entirely clear about whether
4938 duplicate qualifiers should be diagnosed in this case, but it
4939 seems most appropriate to do so). */
4940 element_type = strip_array_types (type);
4941 constp = declspecs->const_p + TYPE_READONLY (element_type);
4942 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
4943 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
4944 as1 = declspecs->address_space;
4945 as2 = TYPE_ADDR_SPACE (element_type);
4946 address_space = ADDR_SPACE_GENERIC_P (as1)? as2 : as1;
4947
4948 if (pedantic && !flag_isoc99)
4949 {
4950 if (constp > 1)
4951 pedwarn (loc, OPT_pedantic, "duplicate %<const%>");
4952 if (restrictp > 1)
4953 pedwarn (loc, OPT_pedantic, "duplicate %<restrict%>");
4954 if (volatilep > 1)
4955 pedwarn (loc, OPT_pedantic, "duplicate %<volatile%>");
4956 }
4957
4958 if (!ADDR_SPACE_GENERIC_P (as1) && !ADDR_SPACE_GENERIC_P (as2) && as1 != as2)
4959 error_at (loc, "conflicting named address spaces (%s vs %s)",
4960 c_addr_space_name (as1), c_addr_space_name (as2));
4961
4962 if (!flag_gen_aux_info && (TYPE_QUALS (element_type)))
4963 type = TYPE_MAIN_VARIANT (type);
4964 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4965 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4966 | (volatilep ? TYPE_QUAL_VOLATILE : 0)
4967 | ENCODE_QUAL_ADDR_SPACE (address_space));
4968
4969 /* Warn about storage classes that are invalid for certain
4970 kinds of declarations (parameters, typenames, etc.). */
4971
4972 if (funcdef_flag
4973 && (threadp
4974 || storage_class == csc_auto
4975 || storage_class == csc_register
4976 || storage_class == csc_typedef))
4977 {
4978 if (storage_class == csc_auto)
4979 pedwarn (loc,
4980 (current_scope == file_scope) ? 0 : OPT_pedantic,
4981 "function definition declared %<auto%>");
4982 if (storage_class == csc_register)
4983 error_at (loc, "function definition declared %<register%>");
4984 if (storage_class == csc_typedef)
4985 error_at (loc, "function definition declared %<typedef%>");
4986 if (threadp)
4987 error_at (loc, "function definition declared %<__thread%>");
4988 threadp = false;
4989 if (storage_class == csc_auto
4990 || storage_class == csc_register
4991 || storage_class == csc_typedef)
4992 storage_class = csc_none;
4993 }
4994 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
4995 {
4996 if (decl_context == PARM && storage_class == csc_register)
4997 ;
4998 else
4999 {
5000 switch (decl_context)
5001 {
5002 case FIELD:
5003 if (name)
5004 error_at (loc, "storage class specified for structure "
5005 "field %qE", name);
5006 else
5007 error_at (loc, "storage class specified for structure field");
5008 break;
5009 case PARM:
5010 if (name)
5011 error_at (loc, "storage class specified for parameter %qE",
5012 name);
5013 else
5014 error_at (loc, "storage class specified for unnamed parameter");
5015 break;
5016 default:
5017 error_at (loc, "storage class specified for typename");
5018 break;
5019 }
5020 storage_class = csc_none;
5021 threadp = false;
5022 }
5023 }
5024 else if (storage_class == csc_extern
5025 && initialized
5026 && !funcdef_flag)
5027 {
5028 /* 'extern' with initialization is invalid if not at file scope. */
5029 if (current_scope == file_scope)
5030 {
5031 /* It is fine to have 'extern const' when compiling at C
5032 and C++ intersection. */
5033 if (!(warn_cxx_compat && constp))
5034 warning_at (loc, 0, "%qE initialized and declared %<extern%>",
5035 name);
5036 }
5037 else
5038 error_at (loc, "%qE has both %<extern%> and initializer", name);
5039 }
5040 else if (current_scope == file_scope)
5041 {
5042 if (storage_class == csc_auto)
5043 error_at (loc, "file-scope declaration of %qE specifies %<auto%>",
5044 name);
5045 if (pedantic && storage_class == csc_register)
5046 pedwarn (input_location, OPT_pedantic,
5047 "file-scope declaration of %qE specifies %<register%>", name);
5048 }
5049 else
5050 {
5051 if (storage_class == csc_extern && funcdef_flag)
5052 error_at (loc, "nested function %qE declared %<extern%>", name);
5053 else if (threadp && storage_class == csc_none)
5054 {
5055 error_at (loc, "function-scope %qE implicitly auto and declared "
5056 "%<__thread%>",
5057 name);
5058 threadp = false;
5059 }
5060 }
5061
5062 /* Now figure out the structure of the declarator proper.
5063 Descend through it, creating more complex types, until we reach
5064 the declared identifier (or NULL_TREE, in an absolute declarator).
5065 At each stage we maintain an unqualified version of the type
5066 together with any qualifiers that should be applied to it with
5067 c_build_qualified_type; this way, array types including
5068 multidimensional array types are first built up in unqualified
5069 form and then the qualified form is created with
5070 TYPE_MAIN_VARIANT pointing to the unqualified form. */
5071
5072 while (declarator && declarator->kind != cdk_id)
5073 {
5074 if (type == error_mark_node)
5075 {
5076 declarator = declarator->declarator;
5077 continue;
5078 }
5079
5080 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
5081 a cdk_pointer (for *...),
5082 a cdk_function (for ...(...)),
5083 a cdk_attrs (for nested attributes),
5084 or a cdk_id (for the name being declared
5085 or the place in an absolute declarator
5086 where the name was omitted).
5087 For the last case, we have just exited the loop.
5088
5089 At this point, TYPE is the type of elements of an array,
5090 or for a function to return, or for a pointer to point to.
5091 After this sequence of ifs, TYPE is the type of the
5092 array or function or pointer, and DECLARATOR has had its
5093 outermost layer removed. */
5094
5095 if (array_ptr_quals != TYPE_UNQUALIFIED
5096 || array_ptr_attrs != NULL_TREE
5097 || array_parm_static)
5098 {
5099 /* Only the innermost declarator (making a parameter be of
5100 array type which is converted to pointer type)
5101 may have static or type qualifiers. */
5102 error_at (loc, "static or type qualifiers in non-parameter array declarator");
5103 array_ptr_quals = TYPE_UNQUALIFIED;
5104 array_ptr_attrs = NULL_TREE;
5105 array_parm_static = 0;
5106 }
5107
5108 switch (declarator->kind)
5109 {
5110 case cdk_attrs:
5111 {
5112 /* A declarator with embedded attributes. */
5113 tree attrs = declarator->u.attrs;
5114 const struct c_declarator *inner_decl;
5115 int attr_flags = 0;
5116 declarator = declarator->declarator;
5117 inner_decl = declarator;
5118 while (inner_decl->kind == cdk_attrs)
5119 inner_decl = inner_decl->declarator;
5120 if (inner_decl->kind == cdk_id)
5121 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
5122 else if (inner_decl->kind == cdk_function)
5123 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
5124 else if (inner_decl->kind == cdk_array)
5125 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
5126 returned_attrs = decl_attributes (&type,
5127 chainon (returned_attrs, attrs),
5128 attr_flags);
5129 break;
5130 }
5131 case cdk_array:
5132 {
5133 tree itype = NULL_TREE;
5134 tree size = declarator->u.array.dimen;
5135 /* The index is a signed object `sizetype' bits wide. */
5136 tree index_type = c_common_signed_type (sizetype);
5137
5138 array_ptr_quals = declarator->u.array.quals;
5139 array_ptr_attrs = declarator->u.array.attrs;
5140 array_parm_static = declarator->u.array.static_p;
5141 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
5142
5143 declarator = declarator->declarator;
5144
5145 /* Check for some types that there cannot be arrays of. */
5146
5147 if (VOID_TYPE_P (type))
5148 {
5149 if (name)
5150 error_at (loc, "declaration of %qE as array of voids", name);
5151 else
5152 error_at (loc, "declaration of type name as array of voids");
5153 type = error_mark_node;
5154 }
5155
5156 if (TREE_CODE (type) == FUNCTION_TYPE)
5157 {
5158 if (name)
5159 error_at (loc, "declaration of %qE as array of functions",
5160 name);
5161 else
5162 error_at (loc, "declaration of type name as array of "
5163 "functions");
5164 type = error_mark_node;
5165 }
5166
5167 if (pedantic && !in_system_header && flexible_array_type_p (type))
5168 pedwarn (loc, OPT_pedantic,
5169 "invalid use of structure with flexible array member");
5170
5171 if (size == error_mark_node)
5172 type = error_mark_node;
5173
5174 if (type == error_mark_node)
5175 continue;
5176
5177 /* If size was specified, set ITYPE to a range-type for
5178 that size. Otherwise, ITYPE remains null. finish_decl
5179 may figure it out from an initial value. */
5180
5181 if (size)
5182 {
5183 bool size_maybe_const = true;
5184 bool size_int_const = (TREE_CODE (size) == INTEGER_CST
5185 && !TREE_OVERFLOW (size));
5186 bool this_size_varies = false;
5187
5188 /* Strip NON_LVALUE_EXPRs since we aren't using as an
5189 lvalue. */
5190 STRIP_TYPE_NOPS (size);
5191
5192 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
5193 {
5194 if (name)
5195 error_at (loc, "size of array %qE has non-integer type",
5196 name);
5197 else
5198 error_at (loc,
5199 "size of unnamed array has non-integer type");
5200 size = integer_one_node;
5201 }
5202
5203 size = c_fully_fold (size, false, &size_maybe_const);
5204
5205 if (pedantic && size_maybe_const && integer_zerop (size))
5206 {
5207 if (name)
5208 pedwarn (loc, OPT_pedantic,
5209 "ISO C forbids zero-size array %qE", name);
5210 else
5211 pedwarn (loc, OPT_pedantic,
5212 "ISO C forbids zero-size array");
5213 }
5214
5215 if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
5216 {
5217 constant_expression_warning (size);
5218 if (tree_int_cst_sgn (size) < 0)
5219 {
5220 if (name)
5221 error_at (loc, "size of array %qE is negative", name);
5222 else
5223 error_at (loc, "size of unnamed array is negative");
5224 size = integer_one_node;
5225 }
5226 /* Handle a size folded to an integer constant but
5227 not an integer constant expression. */
5228 if (!size_int_const)
5229 {
5230 /* If this is a file scope declaration of an
5231 ordinary identifier, this is invalid code;
5232 diagnosing it here and not subsequently
5233 treating the type as variable-length avoids
5234 more confusing diagnostics later. */
5235 if ((decl_context == NORMAL || decl_context == FIELD)
5236 && current_scope == file_scope)
5237 pedwarn (input_location, 0,
5238 "variably modified %qE at file scope",
5239 name);
5240 else
5241 this_size_varies = size_varies = true;
5242 warn_variable_length_array (name, size);
5243 }
5244 }
5245 else if ((decl_context == NORMAL || decl_context == FIELD)
5246 && current_scope == file_scope)
5247 {
5248 error_at (loc, "variably modified %qE at file scope", name);
5249 size = integer_one_node;
5250 }
5251 else
5252 {
5253 /* Make sure the array size remains visibly
5254 nonconstant even if it is (eg) a const variable
5255 with known value. */
5256 this_size_varies = size_varies = true;
5257 warn_variable_length_array (name, size);
5258 }
5259
5260 if (integer_zerop (size) && !this_size_varies)
5261 {
5262 /* A zero-length array cannot be represented with
5263 an unsigned index type, which is what we'll
5264 get with build_index_type. Create an
5265 open-ended range instead. */
5266 itype = build_range_type (sizetype, size, NULL_TREE);
5267 }
5268 else
5269 {
5270 /* Arrange for the SAVE_EXPR on the inside of the
5271 MINUS_EXPR, which allows the -1 to get folded
5272 with the +1 that happens when building TYPE_SIZE. */
5273 if (size_varies)
5274 size = c_variable_size (size);
5275 if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
5276 size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
5277 integer_zero_node, size);
5278
5279 /* Compute the maximum valid index, that is, size
5280 - 1. Do the calculation in index_type, so that
5281 if it is a variable the computations will be
5282 done in the proper mode. */
5283 itype = fold_build2_loc (loc, MINUS_EXPR, index_type,
5284 convert (index_type, size),
5285 convert (index_type,
5286 size_one_node));
5287
5288 /* If that overflowed, the array is too big. ???
5289 While a size of INT_MAX+1 technically shouldn't
5290 cause an overflow (because we subtract 1), the
5291 overflow is recorded during the conversion to
5292 index_type, before the subtraction. Handling
5293 this case seems like an unnecessary
5294 complication. */
5295 if (TREE_CODE (itype) == INTEGER_CST
5296 && TREE_OVERFLOW (itype))
5297 {
5298 if (name)
5299 error_at (loc, "size of array %qE is too large",
5300 name);
5301 else
5302 error_at (loc, "size of unnamed array is too large");
5303 type = error_mark_node;
5304 continue;
5305 }
5306
5307 itype = build_index_type (itype);
5308 }
5309 if (this_size_varies)
5310 {
5311 if (*expr)
5312 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
5313 *expr, size);
5314 else
5315 *expr = size;
5316 *expr_const_operands &= size_maybe_const;
5317 }
5318 }
5319 else if (decl_context == FIELD)
5320 {
5321 bool flexible_array_member = false;
5322 if (array_parm_vla_unspec_p)
5323 /* Field names can in fact have function prototype
5324 scope so [*] is disallowed here through making
5325 the field variably modified, not through being
5326 something other than a declaration with function
5327 prototype scope. */
5328 size_varies = true;
5329 else
5330 {
5331 const struct c_declarator *t = declarator;
5332 while (t->kind == cdk_attrs)
5333 t = t->declarator;
5334 flexible_array_member = (t->kind == cdk_id);
5335 }
5336 if (flexible_array_member
5337 && pedantic && !flag_isoc99 && !in_system_header)
5338 pedwarn (loc, OPT_pedantic,
5339 "ISO C90 does not support flexible array members");
5340
5341 /* ISO C99 Flexible array members are effectively
5342 identical to GCC's zero-length array extension. */
5343 if (flexible_array_member || array_parm_vla_unspec_p)
5344 itype = build_range_type (sizetype, size_zero_node,
5345 NULL_TREE);
5346 }
5347 else if (decl_context == PARM)
5348 {
5349 if (array_parm_vla_unspec_p)
5350 {
5351 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
5352 size_varies = true;
5353 }
5354 }
5355 else if (decl_context == TYPENAME)
5356 {
5357 if (array_parm_vla_unspec_p)
5358 {
5359 /* C99 6.7.5.2p4 */
5360 warning (0, "%<[*]%> not in a declaration");
5361 /* We use this to avoid messing up with incomplete
5362 array types of the same type, that would
5363 otherwise be modified below. */
5364 itype = build_range_type (sizetype, size_zero_node,
5365 NULL_TREE);
5366 size_varies = true;
5367 }
5368 }
5369
5370 /* Complain about arrays of incomplete types. */
5371 if (!COMPLETE_TYPE_P (type))
5372 {
5373 error_at (loc, "array type has incomplete element type");
5374 type = error_mark_node;
5375 }
5376 else
5377 /* When itype is NULL, a shared incomplete array type is
5378 returned for all array of a given type. Elsewhere we
5379 make sure we don't complete that type before copying
5380 it, but here we want to make sure we don't ever
5381 modify the shared type, so we gcc_assert (itype)
5382 below. */
5383 {
5384 addr_space_t as = DECODE_QUAL_ADDR_SPACE (type_quals);
5385 if (!ADDR_SPACE_GENERIC_P (as) && as != TYPE_ADDR_SPACE (type))
5386 type = build_qualified_type (type,
5387 ENCODE_QUAL_ADDR_SPACE (as));
5388
5389 type = build_array_type (type, itype);
5390 }
5391
5392 if (type != error_mark_node)
5393 {
5394 if (size_varies)
5395 {
5396 /* It is ok to modify type here even if itype is
5397 NULL: if size_varies, we're in a
5398 multi-dimensional array and the inner type has
5399 variable size, so the enclosing shared array type
5400 must too. */
5401 if (size && TREE_CODE (size) == INTEGER_CST)
5402 type
5403 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5404 C_TYPE_VARIABLE_SIZE (type) = 1;
5405 }
5406
5407 /* The GCC extension for zero-length arrays differs from
5408 ISO flexible array members in that sizeof yields
5409 zero. */
5410 if (size && integer_zerop (size))
5411 {
5412 gcc_assert (itype);
5413 TYPE_SIZE (type) = bitsize_zero_node;
5414 TYPE_SIZE_UNIT (type) = size_zero_node;
5415 SET_TYPE_STRUCTURAL_EQUALITY (type);
5416 }
5417 if (array_parm_vla_unspec_p)
5418 {
5419 gcc_assert (itype);
5420 /* The type is complete. C99 6.7.5.2p4 */
5421 TYPE_SIZE (type) = bitsize_zero_node;
5422 TYPE_SIZE_UNIT (type) = size_zero_node;
5423 SET_TYPE_STRUCTURAL_EQUALITY (type);
5424 }
5425 }
5426
5427 if (decl_context != PARM
5428 && (array_ptr_quals != TYPE_UNQUALIFIED
5429 || array_ptr_attrs != NULL_TREE
5430 || array_parm_static))
5431 {
5432 error_at (loc, "static or type qualifiers in non-parameter array declarator");
5433 array_ptr_quals = TYPE_UNQUALIFIED;
5434 array_ptr_attrs = NULL_TREE;
5435 array_parm_static = 0;
5436 }
5437 break;
5438 }
5439 case cdk_function:
5440 {
5441 /* Say it's a definition only for the declarator closest
5442 to the identifier, apart possibly from some
5443 attributes. */
5444 bool really_funcdef = false;
5445 tree arg_types;
5446 if (funcdef_flag)
5447 {
5448 const struct c_declarator *t = declarator->declarator;
5449 while (t->kind == cdk_attrs)
5450 t = t->declarator;
5451 really_funcdef = (t->kind == cdk_id);
5452 }
5453
5454 /* Declaring a function type. Make sure we have a valid
5455 type for the function to return. */
5456 if (type == error_mark_node)
5457 continue;
5458
5459 size_varies = false;
5460
5461 /* Warn about some types functions can't return. */
5462 if (TREE_CODE (type) == FUNCTION_TYPE)
5463 {
5464 if (name)
5465 error_at (loc, "%qE declared as function returning a "
5466 "function", name);
5467 else
5468 error_at (loc, "type name declared as function "
5469 "returning a function");
5470 type = integer_type_node;
5471 }
5472 if (TREE_CODE (type) == ARRAY_TYPE)
5473 {
5474 if (name)
5475 error_at (loc, "%qE declared as function returning an array",
5476 name);
5477 else
5478 error_at (loc, "type name declared as function returning "
5479 "an array");
5480 type = integer_type_node;
5481 }
5482 errmsg = targetm.invalid_return_type (type);
5483 if (errmsg)
5484 {
5485 error (errmsg);
5486 type = integer_type_node;
5487 }
5488
5489 /* Construct the function type and go to the next
5490 inner layer of declarator. */
5491 arg_info = declarator->u.arg_info;
5492 arg_types = grokparms (arg_info, really_funcdef);
5493 if (really_funcdef)
5494 put_pending_sizes (arg_info->pending_sizes);
5495
5496 /* Type qualifiers before the return type of the function
5497 qualify the return type, not the function type. */
5498 if (type_quals)
5499 {
5500 /* Type qualifiers on a function return type are
5501 normally permitted by the standard but have no
5502 effect, so give a warning at -Wreturn-type.
5503 Qualifiers on a void return type are banned on
5504 function definitions in ISO C; GCC used to used
5505 them for noreturn functions. */
5506 if (VOID_TYPE_P (type) && really_funcdef)
5507 pedwarn (loc, 0,
5508 "function definition has qualified void return type");
5509 else
5510 warning_at (loc, OPT_Wignored_qualifiers,
5511 "type qualifiers ignored on function return type");
5512
5513 type = c_build_qualified_type (type, type_quals);
5514 }
5515 type_quals = TYPE_UNQUALIFIED;
5516
5517 type = build_function_type (type, arg_types);
5518 declarator = declarator->declarator;
5519
5520 /* Set the TYPE_CONTEXTs for each tagged type which is local to
5521 the formal parameter list of this FUNCTION_TYPE to point to
5522 the FUNCTION_TYPE node itself. */
5523 {
5524 tree link;
5525
5526 for (link = arg_info->tags;
5527 link;
5528 link = TREE_CHAIN (link))
5529 TYPE_CONTEXT (TREE_VALUE (link)) = type;
5530 }
5531 break;
5532 }
5533 case cdk_pointer:
5534 {
5535 /* Merge any constancy or volatility into the target type
5536 for the pointer. */
5537
5538 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
5539 && type_quals)
5540 pedwarn (loc, OPT_pedantic,
5541 "ISO C forbids qualified function types");
5542 if (type_quals)
5543 type = c_build_qualified_type (type, type_quals);
5544 size_varies = false;
5545
5546 /* When the pointed-to type involves components of variable size,
5547 care must be taken to ensure that the size evaluation code is
5548 emitted early enough to dominate all the possible later uses
5549 and late enough for the variables on which it depends to have
5550 been assigned.
5551
5552 This is expected to happen automatically when the pointed-to
5553 type has a name/declaration of it's own, but special attention
5554 is required if the type is anonymous.
5555
5556 We handle the NORMAL and FIELD contexts here by attaching an
5557 artificial TYPE_DECL to such pointed-to type. This forces the
5558 sizes evaluation at a safe point and ensures it is not deferred
5559 until e.g. within a deeper conditional context.
5560
5561 We expect nothing to be needed here for PARM or TYPENAME.
5562 Pushing a TYPE_DECL at this point for TYPENAME would actually
5563 be incorrect, as we might be in the middle of an expression
5564 with side effects on the pointed-to type size "arguments" prior
5565 to the pointer declaration point and the fake TYPE_DECL in the
5566 enclosing context would force the size evaluation prior to the
5567 side effects. */
5568
5569 if (!TYPE_NAME (type)
5570 && (decl_context == NORMAL || decl_context == FIELD)
5571 && variably_modified_type_p (type, NULL_TREE))
5572 {
5573 tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
5574 DECL_ARTIFICIAL (decl) = 1;
5575 pushdecl (decl);
5576 finish_decl (decl, loc, NULL_TREE, NULL_TREE, NULL_TREE);
5577 TYPE_NAME (type) = decl;
5578 }
5579
5580 type = build_pointer_type (type);
5581
5582 /* Process type qualifiers (such as const or volatile)
5583 that were given inside the `*'. */
5584 type_quals = declarator->u.pointer_quals;
5585
5586 declarator = declarator->declarator;
5587 break;
5588 }
5589 default:
5590 gcc_unreachable ();
5591 }
5592 }
5593 *decl_attrs = chainon (returned_attrs, *decl_attrs);
5594
5595 /* Now TYPE has the actual type, apart from any qualifiers in
5596 TYPE_QUALS. */
5597
5598 /* Warn about address space used for things other than static memory or
5599 pointers. */
5600 address_space = DECODE_QUAL_ADDR_SPACE (type_quals);
5601 if (!ADDR_SPACE_GENERIC_P (address_space))
5602 {
5603 if (decl_context == NORMAL)
5604 {
5605 switch (storage_class)
5606 {
5607 case csc_auto:
5608 error ("%qs combined with %<auto%> qualifier for %qE",
5609 c_addr_space_name (address_space), name);
5610 break;
5611 case csc_register:
5612 error ("%qs combined with %<register%> qualifier for %qE",
5613 c_addr_space_name (address_space), name);
5614 break;
5615 case csc_none:
5616 if (current_function_scope)
5617 {
5618 error ("%qs specified for auto variable %qE",
5619 c_addr_space_name (address_space), name);
5620 break;
5621 }
5622 break;
5623 case csc_static:
5624 case csc_extern:
5625 case csc_typedef:
5626 break;
5627 default:
5628 gcc_unreachable ();
5629 }
5630 }
5631 else if (decl_context == PARM && TREE_CODE (type) != ARRAY_TYPE)
5632 {
5633 if (name)
5634 error ("%qs specified for parameter %qE",
5635 c_addr_space_name (address_space), name);
5636 else
5637 error ("%qs specified for unnamed parameter",
5638 c_addr_space_name (address_space));
5639 }
5640 else if (decl_context == FIELD)
5641 {
5642 if (name)
5643 error ("%qs specified for structure field %qE",
5644 c_addr_space_name (address_space), name);
5645 else
5646 error ("%qs specified for structure field",
5647 c_addr_space_name (address_space));
5648 }
5649 }
5650
5651 /* Check the type and width of a bit-field. */
5652 if (bitfield)
5653 check_bitfield_type_and_width (&type, width, name);
5654
5655 /* Did array size calculations overflow? */
5656
5657 if (TREE_CODE (type) == ARRAY_TYPE
5658 && COMPLETE_TYPE_P (type)
5659 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
5660 && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
5661 {
5662 if (name)
5663 error_at (loc, "size of array %qE is too large", name);
5664 else
5665 error_at (loc, "size of unnamed array is too large");
5666 /* If we proceed with the array type as it is, we'll eventually
5667 crash in tree_low_cst(). */
5668 type = error_mark_node;
5669 }
5670
5671 /* If this is declaring a typedef name, return a TYPE_DECL. */
5672
5673 if (storage_class == csc_typedef)
5674 {
5675 tree decl;
5676 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
5677 && type_quals)
5678 pedwarn (loc, OPT_pedantic,
5679 "ISO C forbids qualified function types");
5680 if (type_quals)
5681 type = c_build_qualified_type (type, type_quals);
5682 decl = build_decl (declarator->id_loc,
5683 TYPE_DECL, declarator->u.id, type);
5684 if (declspecs->explicit_signed_p)
5685 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
5686 if (declspecs->inline_p)
5687 pedwarn (loc, 0,"typedef %q+D declared %<inline%>", decl);
5688
5689 if (warn_cxx_compat && declarator->u.id != NULL_TREE)
5690 {
5691 struct c_binding *b = I_TAG_BINDING (declarator->u.id);
5692
5693 if (b != NULL
5694 && b->decl != NULL_TREE
5695 && (B_IN_CURRENT_SCOPE (b)
5696 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
5697 && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type))
5698 {
5699 warning_at (declarator->id_loc, OPT_Wc___compat,
5700 ("using %qD as both a typedef and a tag is "
5701 "invalid in C++"),
5702 decl);
5703 if (b->locus != UNKNOWN_LOCATION)
5704 inform (b->locus, "originally defined here");
5705 }
5706 }
5707
5708 return decl;
5709 }
5710
5711 /* If this is a type name (such as, in a cast or sizeof),
5712 compute the type and return it now. */
5713
5714 if (decl_context == TYPENAME)
5715 {
5716 /* Note that the grammar rejects storage classes in typenames
5717 and fields. */
5718 gcc_assert (storage_class == csc_none && !threadp
5719 && !declspecs->inline_p);
5720 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
5721 && type_quals)
5722 pedwarn (loc, OPT_pedantic,
5723 "ISO C forbids const or volatile function types");
5724 if (type_quals)
5725 type = c_build_qualified_type (type, type_quals);
5726 return type;
5727 }
5728
5729 if (pedantic && decl_context == FIELD
5730 && variably_modified_type_p (type, NULL_TREE))
5731 {
5732 /* C99 6.7.2.1p8 */
5733 pedwarn (loc, OPT_pedantic, "a member of a structure or union cannot "
5734 "have a variably modified type");
5735 }
5736
5737 /* Aside from typedefs and type names (handle above),
5738 `void' at top level (not within pointer)
5739 is allowed only in public variables.
5740 We don't complain about parms either, but that is because
5741 a better error message can be made later. */
5742
5743 if (VOID_TYPE_P (type) && decl_context != PARM
5744 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
5745 && (storage_class == csc_extern
5746 || (current_scope == file_scope
5747 && !(storage_class == csc_static
5748 || storage_class == csc_register)))))
5749 {
5750 error_at (loc, "variable or field %qE declared void", name);
5751 type = integer_type_node;
5752 }
5753
5754 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
5755 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
5756
5757 {
5758 tree decl;
5759
5760 if (decl_context == PARM)
5761 {
5762 tree promoted_type;
5763
5764 /* A parameter declared as an array of T is really a pointer to T.
5765 One declared as a function is really a pointer to a function. */
5766
5767 if (TREE_CODE (type) == ARRAY_TYPE)
5768 {
5769 /* Transfer const-ness of array into that of type pointed to. */
5770 type = TREE_TYPE (type);
5771 if (type_quals)
5772 type = c_build_qualified_type (type, type_quals);
5773 type = build_pointer_type (type);
5774 type_quals = array_ptr_quals;
5775 if (type_quals)
5776 type = c_build_qualified_type (type, type_quals);
5777
5778 /* We don't yet implement attributes in this context. */
5779 if (array_ptr_attrs != NULL_TREE)
5780 warning_at (loc, OPT_Wattributes,
5781 "attributes in parameter array declarator ignored");
5782
5783 size_varies = false;
5784 }
5785 else if (TREE_CODE (type) == FUNCTION_TYPE)
5786 {
5787 if (type_quals)
5788 pedwarn (loc, OPT_pedantic,
5789 "ISO C forbids qualified function types");
5790 if (type_quals)
5791 type = c_build_qualified_type (type, type_quals);
5792 type = build_pointer_type (type);
5793 type_quals = TYPE_UNQUALIFIED;
5794 }
5795 else if (type_quals)
5796 type = c_build_qualified_type (type, type_quals);
5797
5798 decl = build_decl (declarator->id_loc,
5799 PARM_DECL, declarator->u.id, type);
5800 if (size_varies)
5801 C_DECL_VARIABLE_SIZE (decl) = 1;
5802
5803 /* Compute the type actually passed in the parmlist,
5804 for the case where there is no prototype.
5805 (For example, shorts and chars are passed as ints.)
5806 When there is a prototype, this is overridden later. */
5807
5808 if (type == error_mark_node)
5809 promoted_type = type;
5810 else
5811 promoted_type = c_type_promotes_to (type);
5812
5813 DECL_ARG_TYPE (decl) = promoted_type;
5814 if (declspecs->inline_p)
5815 pedwarn (loc, 0, "parameter %q+D declared %<inline%>", decl);
5816 }
5817 else if (decl_context == FIELD)
5818 {
5819 /* Note that the grammar rejects storage classes in typenames
5820 and fields. */
5821 gcc_assert (storage_class == csc_none && !threadp
5822 && !declspecs->inline_p);
5823
5824 /* Structure field. It may not be a function. */
5825
5826 if (TREE_CODE (type) == FUNCTION_TYPE)
5827 {
5828 error_at (loc, "field %qE declared as a function", name);
5829 type = build_pointer_type (type);
5830 }
5831 else if (TREE_CODE (type) != ERROR_MARK
5832 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
5833 {
5834 if (name)
5835 error_at (loc, "field %qE has incomplete type", name);
5836 else
5837 error_at (loc, "unnamed field has incomplete type");
5838 type = error_mark_node;
5839 }
5840 type = c_build_qualified_type (type, type_quals);
5841 decl = build_decl (declarator->id_loc,
5842 FIELD_DECL, declarator->u.id, type);
5843 DECL_NONADDRESSABLE_P (decl) = bitfield;
5844 if (bitfield && !declarator->u.id)
5845 TREE_NO_WARNING (decl) = 1;
5846
5847 if (size_varies)
5848 C_DECL_VARIABLE_SIZE (decl) = 1;
5849 }
5850 else if (TREE_CODE (type) == FUNCTION_TYPE)
5851 {
5852 if (storage_class == csc_register || threadp)
5853 {
5854 error_at (loc, "invalid storage class for function %qE", name);
5855 }
5856 else if (current_scope != file_scope)
5857 {
5858 /* Function declaration not at file scope. Storage
5859 classes other than `extern' are not allowed, C99
5860 6.7.1p5, and `extern' makes no difference. However,
5861 GCC allows 'auto', perhaps with 'inline', to support
5862 nested functions. */
5863 if (storage_class == csc_auto)
5864 pedwarn (loc, OPT_pedantic,
5865 "invalid storage class for function %qE", name);
5866 else if (storage_class == csc_static)
5867 {
5868 error_at (loc, "invalid storage class for function %qE", name);
5869 if (funcdef_flag)
5870 storage_class = declspecs->storage_class = csc_none;
5871 else
5872 return 0;
5873 }
5874 }
5875
5876 decl = build_decl (declarator->id_loc,
5877 FUNCTION_DECL, declarator->u.id, type);
5878 decl = build_decl_attribute_variant (decl, decl_attr);
5879
5880 if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
5881 pedwarn (loc, OPT_pedantic,
5882 "ISO C forbids qualified function types");
5883
5884 /* GNU C interprets a volatile-qualified function type to indicate
5885 that the function does not return. */
5886 if ((type_quals & TYPE_QUAL_VOLATILE)
5887 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
5888 warning_at (loc, 0, "%<noreturn%> function returns non-void value");
5889
5890 /* Every function declaration is an external reference
5891 (DECL_EXTERNAL) except for those which are not at file
5892 scope and are explicitly declared "auto". This is
5893 forbidden by standard C (C99 6.7.1p5) and is interpreted by
5894 GCC to signify a forward declaration of a nested function. */
5895 if (storage_class == csc_auto && current_scope != file_scope)
5896 DECL_EXTERNAL (decl) = 0;
5897 /* In C99, a function which is declared 'inline' with 'extern'
5898 is not an external reference (which is confusing). It
5899 means that the later definition of the function must be output
5900 in this file, C99 6.7.4p6. In GNU C89, a function declared
5901 'extern inline' is an external reference. */
5902 else if (declspecs->inline_p && storage_class != csc_static)
5903 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
5904 == flag_gnu89_inline);
5905 else
5906 DECL_EXTERNAL (decl) = !initialized;
5907
5908 /* Record absence of global scope for `static' or `auto'. */
5909 TREE_PUBLIC (decl)
5910 = !(storage_class == csc_static || storage_class == csc_auto);
5911
5912 /* For a function definition, record the argument information
5913 block where store_parm_decls will look for it. */
5914 if (funcdef_flag)
5915 current_function_arg_info = arg_info;
5916
5917 if (declspecs->default_int_p)
5918 C_FUNCTION_IMPLICIT_INT (decl) = 1;
5919
5920 /* Record presence of `inline', if it is reasonable. */
5921 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
5922 {
5923 if (declspecs->inline_p)
5924 pedwarn (loc, 0, "cannot inline function %<main%>");
5925 }
5926 else if (declspecs->inline_p)
5927 /* Record that the function is declared `inline'. */
5928 DECL_DECLARED_INLINE_P (decl) = 1;
5929 }
5930 else
5931 {
5932 /* It's a variable. */
5933 /* An uninitialized decl with `extern' is a reference. */
5934 int extern_ref = !initialized && storage_class == csc_extern;
5935
5936 type = c_build_qualified_type (type, type_quals);
5937
5938 /* C99 6.2.2p7: It is invalid (compile-time undefined
5939 behavior) to create an 'extern' declaration for a
5940 variable if there is a global declaration that is
5941 'static' and the global declaration is not visible.
5942 (If the static declaration _is_ currently visible,
5943 the 'extern' declaration is taken to refer to that decl.) */
5944 if (extern_ref && current_scope != file_scope)
5945 {
5946 tree global_decl = identifier_global_value (declarator->u.id);
5947 tree visible_decl = lookup_name (declarator->u.id);
5948
5949 if (global_decl
5950 && global_decl != visible_decl
5951 && TREE_CODE (global_decl) == VAR_DECL
5952 && !TREE_PUBLIC (global_decl))
5953 error_at (loc, "variable previously declared %<static%> "
5954 "redeclared %<extern%>");
5955 }
5956
5957 decl = build_decl (declarator->id_loc,
5958 VAR_DECL, declarator->u.id, type);
5959 if (size_varies)
5960 C_DECL_VARIABLE_SIZE (decl) = 1;
5961
5962 if (declspecs->inline_p)
5963 pedwarn (loc, 0, "variable %q+D declared %<inline%>", decl);
5964
5965 /* At file scope, an initialized extern declaration may follow
5966 a static declaration. In that case, DECL_EXTERNAL will be
5967 reset later in start_decl. */
5968 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
5969
5970 /* At file scope, the presence of a `static' or `register' storage
5971 class specifier, or the absence of all storage class specifiers
5972 makes this declaration a definition (perhaps tentative). Also,
5973 the absence of `static' makes it public. */
5974 if (current_scope == file_scope)
5975 {
5976 TREE_PUBLIC (decl) = storage_class != csc_static;
5977 TREE_STATIC (decl) = !extern_ref;
5978 }
5979 /* Not at file scope, only `static' makes a static definition. */
5980 else
5981 {
5982 TREE_STATIC (decl) = (storage_class == csc_static);
5983 TREE_PUBLIC (decl) = extern_ref;
5984 }
5985
5986 if (threadp)
5987 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
5988 }
5989
5990 if ((storage_class == csc_extern
5991 || (storage_class == csc_none
5992 && TREE_CODE (type) == FUNCTION_TYPE
5993 && !funcdef_flag))
5994 && variably_modified_type_p (type, NULL_TREE))
5995 {
5996 /* C99 6.7.5.2p2 */
5997 if (TREE_CODE (type) == FUNCTION_TYPE)
5998 error_at (loc, "non-nested function with variably modified type");
5999 else
6000 error_at (loc, "object with variably modified type must have "
6001 "no linkage");
6002 }
6003
6004 /* Record `register' declaration for warnings on &
6005 and in case doing stupid register allocation. */
6006
6007 if (storage_class == csc_register)
6008 {
6009 C_DECL_REGISTER (decl) = 1;
6010 DECL_REGISTER (decl) = 1;
6011 }
6012
6013 /* Record constancy and volatility. */
6014 c_apply_type_quals_to_decl (type_quals, decl);
6015
6016 /* If a type has volatile components, it should be stored in memory.
6017 Otherwise, the fact that those components are volatile
6018 will be ignored, and would even crash the compiler.
6019 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
6020 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
6021 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
6022 || TREE_CODE (decl) == RESULT_DECL))
6023 {
6024 /* It is not an error for a structure with volatile fields to
6025 be declared register, but reset DECL_REGISTER since it
6026 cannot actually go in a register. */
6027 int was_reg = C_DECL_REGISTER (decl);
6028 C_DECL_REGISTER (decl) = 0;
6029 DECL_REGISTER (decl) = 0;
6030 c_mark_addressable (decl);
6031 C_DECL_REGISTER (decl) = was_reg;
6032 }
6033
6034 /* This is the earliest point at which we might know the assembler
6035 name of a variable. Thus, if it's known before this, die horribly. */
6036 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
6037
6038 if (warn_cxx_compat
6039 && TREE_CODE (decl) == VAR_DECL
6040 && TREE_PUBLIC (decl)
6041 && TREE_STATIC (decl)
6042 && (TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
6043 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
6044 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
6045 && TYPE_NAME (TREE_TYPE (decl)) == NULL_TREE)
6046 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
6047 ("non-local variable %qD with anonymous type is "
6048 "questionable in C++"),
6049 decl);
6050
6051 return decl;
6052 }
6053 }
6054 \f
6055 /* Decode the parameter-list info for a function type or function definition.
6056 The argument is the value returned by `get_parm_info' (or made in c-parse.c
6057 if there is an identifier list instead of a parameter decl list).
6058 These two functions are separate because when a function returns
6059 or receives functions then each is called multiple times but the order
6060 of calls is different. The last call to `grokparms' is always the one
6061 that contains the formal parameter names of a function definition.
6062
6063 Return a list of arg types to use in the FUNCTION_TYPE for this function.
6064
6065 FUNCDEF_FLAG is true for a function definition, false for
6066 a mere declaration. A nonempty identifier-list gets an error message
6067 when FUNCDEF_FLAG is false. */
6068
6069 static tree
6070 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
6071 {
6072 tree arg_types = arg_info->types;
6073
6074 if (funcdef_flag && arg_info->had_vla_unspec)
6075 {
6076 /* A function definition isn't function prototype scope C99 6.2.1p4. */
6077 /* C99 6.7.5.2p4 */
6078 error ("%<[*]%> not allowed in other than function prototype scope");
6079 }
6080
6081 if (arg_types == 0 && !funcdef_flag && !in_system_header)
6082 warning (OPT_Wstrict_prototypes,
6083 "function declaration isn%'t a prototype");
6084
6085 if (arg_types == error_mark_node)
6086 return 0; /* don't set TYPE_ARG_TYPES in this case */
6087
6088 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
6089 {
6090 if (!funcdef_flag)
6091 pedwarn (input_location, 0, "parameter names (without types) in function declaration");
6092
6093 arg_info->parms = arg_info->types;
6094 arg_info->types = 0;
6095 return 0;
6096 }
6097 else
6098 {
6099 tree parm, type, typelt;
6100 unsigned int parmno;
6101 const char *errmsg;
6102
6103 /* If there is a parameter of incomplete type in a definition,
6104 this is an error. In a declaration this is valid, and a
6105 struct or union type may be completed later, before any calls
6106 or definition of the function. In the case where the tag was
6107 first declared within the parameter list, a warning has
6108 already been given. If a parameter has void type, then
6109 however the function cannot be defined or called, so
6110 warn. */
6111
6112 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
6113 parm;
6114 parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
6115 {
6116 type = TREE_VALUE (typelt);
6117 if (type == error_mark_node)
6118 continue;
6119
6120 if (!COMPLETE_TYPE_P (type))
6121 {
6122 if (funcdef_flag)
6123 {
6124 if (DECL_NAME (parm))
6125 error_at (input_location,
6126 "parameter %u (%q+D) has incomplete type",
6127 parmno, parm);
6128 else
6129 error_at (DECL_SOURCE_LOCATION (parm),
6130 "parameter %u has incomplete type",
6131 parmno);
6132
6133 TREE_VALUE (typelt) = error_mark_node;
6134 TREE_TYPE (parm) = error_mark_node;
6135 arg_types = NULL_TREE;
6136 }
6137 else if (VOID_TYPE_P (type))
6138 {
6139 if (DECL_NAME (parm))
6140 warning_at (input_location, 0,
6141 "parameter %u (%q+D) has void type",
6142 parmno, parm);
6143 else
6144 warning_at (DECL_SOURCE_LOCATION (parm), 0,
6145 "parameter %u has void type",
6146 parmno);
6147 }
6148 }
6149
6150 errmsg = targetm.invalid_parameter_type (type);
6151 if (errmsg)
6152 {
6153 error (errmsg);
6154 TREE_VALUE (typelt) = error_mark_node;
6155 TREE_TYPE (parm) = error_mark_node;
6156 arg_types = NULL_TREE;
6157 }
6158
6159 if (DECL_NAME (parm) && TREE_USED (parm))
6160 warn_if_shadowing (parm);
6161 }
6162 return arg_types;
6163 }
6164 }
6165
6166 /* Take apart the current scope and return a c_arg_info structure with
6167 info on a parameter list just parsed.
6168
6169 This structure is later fed to 'grokparms' and 'store_parm_decls'.
6170
6171 ELLIPSIS being true means the argument list ended in '...' so don't
6172 append a sentinel (void_list_node) to the end of the type-list. */
6173
6174 struct c_arg_info *
6175 get_parm_info (bool ellipsis)
6176 {
6177 struct c_binding *b = current_scope->bindings;
6178 struct c_arg_info *arg_info = XOBNEW (&parser_obstack,
6179 struct c_arg_info);
6180 tree parms = 0;
6181 tree tags = 0;
6182 tree types = 0;
6183 tree others = 0;
6184
6185 static bool explained_incomplete_types = false;
6186 bool gave_void_only_once_err = false;
6187
6188 arg_info->parms = 0;
6189 arg_info->tags = 0;
6190 arg_info->types = 0;
6191 arg_info->others = 0;
6192 arg_info->pending_sizes = 0;
6193 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
6194
6195 /* The bindings in this scope must not get put into a block.
6196 We will take care of deleting the binding nodes. */
6197 current_scope->bindings = 0;
6198
6199 /* This function is only called if there was *something* on the
6200 parameter list. */
6201 gcc_assert (b);
6202
6203 /* A parameter list consisting solely of 'void' indicates that the
6204 function takes no arguments. But if the 'void' is qualified
6205 (by 'const' or 'volatile'), or has a storage class specifier
6206 ('register'), then the behavior is undefined; issue an error.
6207 Typedefs for 'void' are OK (see DR#157). */
6208 if (b->prev == 0 /* one binding */
6209 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
6210 && !DECL_NAME (b->decl) /* anonymous */
6211 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
6212 {
6213 if (TREE_THIS_VOLATILE (b->decl)
6214 || TREE_READONLY (b->decl)
6215 || C_DECL_REGISTER (b->decl))
6216 error ("%<void%> as only parameter may not be qualified");
6217
6218 /* There cannot be an ellipsis. */
6219 if (ellipsis)
6220 error ("%<void%> must be the only parameter");
6221
6222 arg_info->types = void_list_node;
6223 return arg_info;
6224 }
6225
6226 if (!ellipsis)
6227 types = void_list_node;
6228
6229 /* Break up the bindings list into parms, tags, types, and others;
6230 apply sanity checks; purge the name-to-decl bindings. */
6231 while (b)
6232 {
6233 tree decl = b->decl;
6234 tree type = TREE_TYPE (decl);
6235 const char *keyword;
6236
6237 switch (TREE_CODE (decl))
6238 {
6239 case PARM_DECL:
6240 if (b->id)
6241 {
6242 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
6243 I_SYMBOL_BINDING (b->id) = b->shadowed;
6244 }
6245
6246 /* Check for forward decls that never got their actual decl. */
6247 if (TREE_ASM_WRITTEN (decl))
6248 error ("parameter %q+D has just a forward declaration", decl);
6249 /* Check for (..., void, ...) and issue an error. */
6250 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
6251 {
6252 if (!gave_void_only_once_err)
6253 {
6254 error ("%<void%> must be the only parameter");
6255 gave_void_only_once_err = true;
6256 }
6257 }
6258 else
6259 {
6260 /* Valid parameter, add it to the list. */
6261 TREE_CHAIN (decl) = parms;
6262 parms = decl;
6263
6264 /* Since there is a prototype, args are passed in their
6265 declared types. The back end may override this later. */
6266 DECL_ARG_TYPE (decl) = type;
6267 types = tree_cons (0, type, types);
6268 }
6269 break;
6270
6271 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
6272 case UNION_TYPE: keyword = "union"; goto tag;
6273 case RECORD_TYPE: keyword = "struct"; goto tag;
6274 tag:
6275 /* Types may not have tag-names, in which case the type
6276 appears in the bindings list with b->id NULL. */
6277 if (b->id)
6278 {
6279 gcc_assert (I_TAG_BINDING (b->id) == b);
6280 I_TAG_BINDING (b->id) = b->shadowed;
6281 }
6282
6283 /* Warn about any struct, union or enum tags defined in a
6284 parameter list. The scope of such types is limited to
6285 the parameter list, which is rarely if ever desirable
6286 (it's impossible to call such a function with type-
6287 correct arguments). An anonymous union parm type is
6288 meaningful as a GNU extension, so don't warn for that. */
6289 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
6290 {
6291 if (b->id)
6292 /* The %s will be one of 'struct', 'union', or 'enum'. */
6293 warning (0, "%<%s %E%> declared inside parameter list",
6294 keyword, b->id);
6295 else
6296 /* The %s will be one of 'struct', 'union', or 'enum'. */
6297 warning (0, "anonymous %s declared inside parameter list",
6298 keyword);
6299
6300 if (!explained_incomplete_types)
6301 {
6302 warning (0, "its scope is only this definition or declaration,"
6303 " which is probably not what you want");
6304 explained_incomplete_types = true;
6305 }
6306 }
6307
6308 tags = tree_cons (b->id, decl, tags);
6309 break;
6310
6311 case CONST_DECL:
6312 case TYPE_DECL:
6313 case FUNCTION_DECL:
6314 /* CONST_DECLs appear here when we have an embedded enum,
6315 and TYPE_DECLs appear here when we have an embedded struct
6316 or union. No warnings for this - we already warned about the
6317 type itself. FUNCTION_DECLs appear when there is an implicit
6318 function declaration in the parameter list. */
6319
6320 /* When we reinsert this decl in the function body, we need
6321 to reconstruct whether it was marked as nested. */
6322 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
6323 ? b->nested
6324 : !b->nested);
6325 TREE_CHAIN (decl) = others;
6326 others = decl;
6327 /* fall through */
6328
6329 case ERROR_MARK:
6330 /* error_mark_node appears here when we have an undeclared
6331 variable. Just throw it away. */
6332 if (b->id)
6333 {
6334 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
6335 I_SYMBOL_BINDING (b->id) = b->shadowed;
6336 }
6337 break;
6338
6339 /* Other things that might be encountered. */
6340 case LABEL_DECL:
6341 case VAR_DECL:
6342 default:
6343 gcc_unreachable ();
6344 }
6345
6346 b = free_binding_and_advance (b);
6347 }
6348
6349 arg_info->parms = parms;
6350 arg_info->tags = tags;
6351 arg_info->types = types;
6352 arg_info->others = others;
6353 arg_info->pending_sizes = get_pending_sizes ();
6354 return arg_info;
6355 }
6356 \f
6357 /* Get the struct, enum or union (CODE says which) with tag NAME.
6358 Define the tag as a forward-reference with location LOC if it is
6359 not defined. Return a c_typespec structure for the type
6360 specifier. */
6361
6362 struct c_typespec
6363 parser_xref_tag (location_t loc, enum tree_code code, tree name)
6364 {
6365 struct c_typespec ret;
6366 tree ref;
6367 location_t refloc;
6368
6369 ret.expr = NULL_TREE;
6370 ret.expr_const_operands = true;
6371
6372 /* If a cross reference is requested, look up the type
6373 already defined for this tag and return it. */
6374
6375 ref = lookup_tag (code, name, 0, &refloc);
6376 /* If this is the right type of tag, return what we found.
6377 (This reference will be shadowed by shadow_tag later if appropriate.)
6378 If this is the wrong type of tag, do not return it. If it was the
6379 wrong type in the same scope, we will have had an error
6380 message already; if in a different scope and declaring
6381 a name, pending_xref_error will give an error message; but if in a
6382 different scope and not declaring a name, this tag should
6383 shadow the previous declaration of a different type of tag, and
6384 this would not work properly if we return the reference found.
6385 (For example, with "struct foo" in an outer scope, "union foo;"
6386 must shadow that tag with a new one of union type.) */
6387 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
6388 if (ref && TREE_CODE (ref) == code)
6389 {
6390 if (C_TYPE_DEFINED_IN_STRUCT (ref)
6391 && loc != UNKNOWN_LOCATION
6392 && warn_cxx_compat)
6393 {
6394 switch (code)
6395 {
6396 case ENUMERAL_TYPE:
6397 warning_at (loc, OPT_Wc___compat,
6398 ("enum type defined in struct or union "
6399 "is not visible in C++"));
6400 inform (refloc, "enum type defined here");
6401 break;
6402 case RECORD_TYPE:
6403 warning_at (loc, OPT_Wc___compat,
6404 ("struct defined in struct or union "
6405 "is not visible in C++"));
6406 inform (refloc, "struct defined here");
6407 break;
6408 case UNION_TYPE:
6409 warning_at (loc, OPT_Wc___compat,
6410 ("union defined in struct or union "
6411 "is not visible in C++"));
6412 inform (refloc, "union defined here");
6413 break;
6414 default:
6415 gcc_unreachable();
6416 }
6417 }
6418
6419 ret.spec = ref;
6420 return ret;
6421 }
6422
6423 /* If no such tag is yet defined, create a forward-reference node
6424 and record it as the "definition".
6425 When a real declaration of this type is found,
6426 the forward-reference will be altered into a real type. */
6427
6428 ref = make_node (code);
6429 if (code == ENUMERAL_TYPE)
6430 {
6431 /* Give the type a default layout like unsigned int
6432 to avoid crashing if it does not get defined. */
6433 SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
6434 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
6435 TYPE_USER_ALIGN (ref) = 0;
6436 TYPE_UNSIGNED (ref) = 1;
6437 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
6438 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
6439 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
6440 }
6441
6442 pushtag (loc, name, ref);
6443
6444 ret.spec = ref;
6445 return ret;
6446 }
6447
6448 /* Get the struct, enum or union (CODE says which) with tag NAME.
6449 Define the tag as a forward-reference if it is not defined.
6450 Return a tree for the type. */
6451
6452 tree
6453 xref_tag (enum tree_code code, tree name)
6454 {
6455 return parser_xref_tag (input_location, code, name).spec;
6456 }
6457 \f
6458 /* Make sure that the tag NAME is defined *in the current scope*
6459 at least as a forward reference.
6460 LOC is the location of the struct's definition.
6461 CODE says which kind of tag NAME ought to be.
6462
6463 This stores the current value of the file static STRUCT_PARSE_INFO
6464 in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
6465 new c_struct_parse_info structure. The old value of
6466 STRUCT_PARSE_INFO is restored in finish_struct. */
6467
6468 tree
6469 start_struct (location_t loc, enum tree_code code, tree name,
6470 struct c_struct_parse_info **enclosing_struct_parse_info)
6471 {
6472 /* If there is already a tag defined at this scope
6473 (as a forward reference), just return it. */
6474
6475 tree ref = NULL_TREE;
6476 location_t refloc = UNKNOWN_LOCATION;
6477
6478 if (name != NULL_TREE)
6479 ref = lookup_tag (code, name, 1, &refloc);
6480 if (ref && TREE_CODE (ref) == code)
6481 {
6482 if (TYPE_SIZE (ref))
6483 {
6484 if (code == UNION_TYPE)
6485 error_at (loc, "redefinition of %<union %E%>", name);
6486 else
6487 error_at (loc, "redefinition of %<struct %E%>", name);
6488 if (refloc != UNKNOWN_LOCATION)
6489 inform (refloc, "originally defined here");
6490 /* Don't create structures using a name already in use. */
6491 ref = NULL_TREE;
6492 }
6493 else if (C_TYPE_BEING_DEFINED (ref))
6494 {
6495 if (code == UNION_TYPE)
6496 error_at (loc, "nested redefinition of %<union %E%>", name);
6497 else
6498 error_at (loc, "nested redefinition of %<struct %E%>", name);
6499 /* Don't bother to report "originally defined here" for a
6500 nested redefinition; the original definition should be
6501 obvious. */
6502 /* Don't create structures that contain themselves. */
6503 ref = NULL_TREE;
6504 }
6505 }
6506
6507 /* Otherwise create a forward-reference just so the tag is in scope. */
6508
6509 if (ref == NULL_TREE || TREE_CODE (ref) != code)
6510 {
6511 ref = make_node (code);
6512 pushtag (loc, name, ref);
6513 }
6514
6515 C_TYPE_BEING_DEFINED (ref) = 1;
6516 TYPE_PACKED (ref) = flag_pack_struct;
6517
6518 *enclosing_struct_parse_info = struct_parse_info;
6519 struct_parse_info = XNEW (struct c_struct_parse_info);
6520 struct_parse_info->struct_types = VEC_alloc (tree, heap, 0);
6521 struct_parse_info->fields = VEC_alloc (c_binding_ptr, heap, 0);
6522 struct_parse_info->typedefs_seen = VEC_alloc (tree, heap, 0);
6523
6524 /* FIXME: This will issue a warning for a use of a type defined
6525 within a statement expr used within sizeof, et. al. This is not
6526 terribly serious as C++ doesn't permit statement exprs within
6527 sizeof anyhow. */
6528 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
6529 warning_at (loc, OPT_Wc___compat,
6530 "defining type in %qs expression is invalid in C++",
6531 (in_sizeof
6532 ? "sizeof"
6533 : (in_typeof ? "typeof" : "alignof")));
6534
6535 return ref;
6536 }
6537
6538 /* Process the specs, declarator and width (NULL if omitted)
6539 of a structure component, returning a FIELD_DECL node.
6540 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
6541 DECL_ATTRS is as for grokdeclarator.
6542
6543 LOC is the location of the structure component.
6544
6545 This is done during the parsing of the struct declaration.
6546 The FIELD_DECL nodes are chained together and the lot of them
6547 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
6548
6549 tree
6550 grokfield (location_t loc,
6551 struct c_declarator *declarator, struct c_declspecs *declspecs,
6552 tree width, tree *decl_attrs)
6553 {
6554 tree value;
6555
6556 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
6557 && width == NULL_TREE)
6558 {
6559 /* This is an unnamed decl.
6560
6561 If we have something of the form "union { list } ;" then this
6562 is the anonymous union extension. Similarly for struct.
6563
6564 If this is something of the form "struct foo;", then
6565 If MS extensions are enabled, this is handled as an
6566 anonymous struct.
6567 Otherwise this is a forward declaration of a structure tag.
6568
6569 If this is something of the form "foo;" and foo is a TYPE_DECL, then
6570 If MS extensions are enabled and foo names a structure, then
6571 again this is an anonymous struct.
6572 Otherwise this is an error.
6573
6574 Oh what a horrid tangled web we weave. I wonder if MS consciously
6575 took this from Plan 9 or if it was an accident of implementation
6576 that took root before someone noticed the bug... */
6577
6578 tree type = declspecs->type;
6579 bool type_ok = (TREE_CODE (type) == RECORD_TYPE
6580 || TREE_CODE (type) == UNION_TYPE);
6581 bool ok = false;
6582
6583 if (type_ok
6584 && (flag_ms_extensions || !declspecs->typedef_p))
6585 {
6586 if (flag_ms_extensions)
6587 ok = true;
6588 else if (flag_iso)
6589 ok = false;
6590 else if (TYPE_NAME (type) == NULL)
6591 ok = true;
6592 else
6593 ok = false;
6594 }
6595 if (!ok)
6596 {
6597 pedwarn (loc, 0, "declaration does not declare anything");
6598 return NULL_TREE;
6599 }
6600 pedwarn (loc, OPT_pedantic, "ISO C doesn%'t support unnamed structs/unions");
6601 }
6602
6603 value = grokdeclarator (declarator, declspecs, FIELD, false,
6604 width ? &width : NULL, decl_attrs, NULL, NULL,
6605 DEPRECATED_NORMAL);
6606
6607 finish_decl (value, loc, NULL_TREE, NULL_TREE, NULL_TREE);
6608 DECL_INITIAL (value) = width;
6609
6610 if (warn_cxx_compat && DECL_NAME (value) != NULL_TREE)
6611 {
6612 /* If we currently have a binding for this field, set the
6613 in_struct field in the binding, so that we warn about lookups
6614 which find it. */
6615 struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (value));
6616 if (b != NULL)
6617 {
6618 /* If the in_struct field is not yet set, push it on a list
6619 to be cleared when this struct is finished. */
6620 if (!b->in_struct)
6621 {
6622 VEC_safe_push (c_binding_ptr, heap,
6623 struct_parse_info->fields, b);
6624 b->in_struct = 1;
6625 }
6626 }
6627 }
6628
6629 return value;
6630 }
6631 \f
6632 /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
6633 to HTAB, giving errors for any duplicates. */
6634
6635 static void
6636 detect_field_duplicates_hash (tree fieldlist, htab_t htab)
6637 {
6638 tree x, y;
6639 void **slot;
6640
6641 for (x = fieldlist; x ; x = TREE_CHAIN (x))
6642 if ((y = DECL_NAME (x)) != 0)
6643 {
6644 slot = htab_find_slot (htab, y, INSERT);
6645 if (*slot)
6646 {
6647 error ("duplicate member %q+D", x);
6648 DECL_NAME (x) = NULL_TREE;
6649 }
6650 *slot = y;
6651 }
6652 else if (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
6653 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE)
6654 detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x)), htab);
6655 }
6656
6657 /* Generate an error for any duplicate field names in FIELDLIST. Munge
6658 the list such that this does not present a problem later. */
6659
6660 static void
6661 detect_field_duplicates (tree fieldlist)
6662 {
6663 tree x, y;
6664 int timeout = 10;
6665
6666 /* First, see if there are more than "a few" fields.
6667 This is trivially true if there are zero or one fields. */
6668 if (!fieldlist)
6669 return;
6670 x = TREE_CHAIN (fieldlist);
6671 if (!x)
6672 return;
6673 do {
6674 timeout--;
6675 if (DECL_NAME (x) == NULL_TREE
6676 && (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
6677 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE))
6678 timeout = 0;
6679 x = TREE_CHAIN (x);
6680 } while (timeout > 0 && x);
6681
6682 /* If there were "few" fields and no anonymous structures or unions,
6683 avoid the overhead of allocating a hash table. Instead just do
6684 the nested traversal thing. */
6685 if (timeout > 0)
6686 {
6687 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
6688 if (DECL_NAME (x))
6689 {
6690 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
6691 if (DECL_NAME (y) == DECL_NAME (x))
6692 {
6693 error ("duplicate member %q+D", x);
6694 DECL_NAME (x) = NULL_TREE;
6695 }
6696 }
6697 }
6698 else
6699 {
6700 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
6701
6702 detect_field_duplicates_hash (fieldlist, htab);
6703 htab_delete (htab);
6704 }
6705 }
6706
6707 /* Finish up struct info used by -Wc++-compat. */
6708
6709 static void
6710 warn_cxx_compat_finish_struct (tree fieldlist)
6711 {
6712 unsigned int ix;
6713 tree x;
6714 struct c_binding *b;
6715
6716 /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
6717 the current struct. We do this now at the end of the struct
6718 because the flag is used to issue visibility warnings, and we
6719 only want to issue those warnings if the type is referenced
6720 outside of the struct declaration. */
6721 for (ix = 0; VEC_iterate (tree, struct_parse_info->struct_types, ix, x); ++ix)
6722 C_TYPE_DEFINED_IN_STRUCT (x) = 1;
6723
6724 /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
6725 typedefs used when declaring fields in this struct. If the name
6726 of any of the fields is also a typedef name then the struct would
6727 not parse in C++, because the C++ lookup rules say that the
6728 typedef name would be looked up in the context of the struct, and
6729 would thus be the field rather than the typedef. */
6730 if (!VEC_empty (tree, struct_parse_info->typedefs_seen)
6731 && fieldlist != NULL_TREE)
6732 {
6733 /* Use a pointer_set using the name of the typedef. We can use
6734 a pointer_set because identifiers are interned. */
6735 struct pointer_set_t *tset = pointer_set_create ();
6736
6737 for (ix = 0;
6738 VEC_iterate (tree, struct_parse_info->typedefs_seen, ix, x);
6739 ++ix)
6740 pointer_set_insert (tset, DECL_NAME (x));
6741
6742 for (x = fieldlist; x != NULL_TREE; x = TREE_CHAIN (x))
6743 {
6744 if (pointer_set_contains (tset, DECL_NAME (x)))
6745 {
6746 warning_at (DECL_SOURCE_LOCATION (x), OPT_Wc___compat,
6747 ("using %qD as both field and typedef name is "
6748 "invalid in C++"),
6749 x);
6750 /* FIXME: It would be nice to report the location where
6751 the typedef name is used. */
6752 }
6753 }
6754
6755 pointer_set_destroy (tset);
6756 }
6757
6758 /* For each field which has a binding and which was not defined in
6759 an enclosing struct, clear the in_struct field. */
6760 for (ix = 0;
6761 VEC_iterate (c_binding_ptr, struct_parse_info->fields, ix, b);
6762 ++ix)
6763 b->in_struct = 0;
6764 }
6765
6766 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
6767 LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
6768 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
6769 ATTRIBUTES are attributes to be applied to the structure.
6770
6771 ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
6772 the struct was started. */
6773
6774 tree
6775 finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
6776 struct c_struct_parse_info *enclosing_struct_parse_info)
6777 {
6778 tree x;
6779 bool toplevel = file_scope == current_scope;
6780 int saw_named_field;
6781
6782 /* If this type was previously laid out as a forward reference,
6783 make sure we lay it out again. */
6784
6785 TYPE_SIZE (t) = 0;
6786
6787 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
6788
6789 if (pedantic)
6790 {
6791 for (x = fieldlist; x; x = TREE_CHAIN (x))
6792 if (DECL_NAME (x) != 0)
6793 break;
6794
6795 if (x == 0)
6796 {
6797 if (TREE_CODE (t) == UNION_TYPE)
6798 {
6799 if (fieldlist)
6800 pedwarn (loc, OPT_pedantic, "union has no named members");
6801 else
6802 pedwarn (loc, OPT_pedantic, "union has no members");
6803 }
6804 else
6805 {
6806 if (fieldlist)
6807 pedwarn (loc, OPT_pedantic, "struct has no named members");
6808 else
6809 pedwarn (loc, OPT_pedantic, "struct has no members");
6810 }
6811 }
6812 }
6813
6814 /* Install struct as DECL_CONTEXT of each field decl.
6815 Also process specified field sizes, found in the DECL_INITIAL,
6816 storing 0 there after the type has been changed to precision equal
6817 to its width, rather than the precision of the specified standard
6818 type. (Correct layout requires the original type to have been preserved
6819 until now.) */
6820
6821 saw_named_field = 0;
6822 for (x = fieldlist; x; x = TREE_CHAIN (x))
6823 {
6824 if (TREE_TYPE (x) == error_mark_node)
6825 continue;
6826
6827 DECL_CONTEXT (x) = t;
6828
6829 /* If any field is const, the structure type is pseudo-const. */
6830 if (TREE_READONLY (x))
6831 C_TYPE_FIELDS_READONLY (t) = 1;
6832 else
6833 {
6834 /* A field that is pseudo-const makes the structure likewise. */
6835 tree t1 = TREE_TYPE (x);
6836 while (TREE_CODE (t1) == ARRAY_TYPE)
6837 t1 = TREE_TYPE (t1);
6838 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
6839 && C_TYPE_FIELDS_READONLY (t1))
6840 C_TYPE_FIELDS_READONLY (t) = 1;
6841 }
6842
6843 /* Any field that is volatile means variables of this type must be
6844 treated in some ways as volatile. */
6845 if (TREE_THIS_VOLATILE (x))
6846 C_TYPE_FIELDS_VOLATILE (t) = 1;
6847
6848 /* Any field of nominal variable size implies structure is too. */
6849 if (C_DECL_VARIABLE_SIZE (x))
6850 C_TYPE_VARIABLE_SIZE (t) = 1;
6851
6852 if (DECL_INITIAL (x))
6853 {
6854 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
6855 DECL_SIZE (x) = bitsize_int (width);
6856 DECL_BIT_FIELD (x) = 1;
6857 SET_DECL_C_BIT_FIELD (x);
6858 }
6859
6860 if (TYPE_PACKED (t)
6861 && (DECL_BIT_FIELD (x)
6862 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
6863 DECL_PACKED (x) = 1;
6864
6865 /* Detect flexible array member in an invalid context. */
6866 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
6867 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
6868 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
6869 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
6870 {
6871 if (TREE_CODE (t) == UNION_TYPE)
6872 {
6873 error_at (DECL_SOURCE_LOCATION (x),
6874 "flexible array member in union");
6875 TREE_TYPE (x) = error_mark_node;
6876 }
6877 else if (TREE_CHAIN (x) != NULL_TREE)
6878 {
6879 error_at (DECL_SOURCE_LOCATION (x),
6880 "flexible array member not at end of struct");
6881 TREE_TYPE (x) = error_mark_node;
6882 }
6883 else if (!saw_named_field)
6884 {
6885 error_at (DECL_SOURCE_LOCATION (x),
6886 "flexible array member in otherwise empty struct");
6887 TREE_TYPE (x) = error_mark_node;
6888 }
6889 }
6890
6891 if (pedantic && TREE_CODE (t) == RECORD_TYPE
6892 && flexible_array_type_p (TREE_TYPE (x)))
6893 pedwarn (DECL_SOURCE_LOCATION (x), OPT_pedantic,
6894 "invalid use of structure with flexible array member");
6895
6896 if (DECL_NAME (x))
6897 saw_named_field = 1;
6898 }
6899
6900 detect_field_duplicates (fieldlist);
6901
6902 /* Now we have the nearly final fieldlist. Record it,
6903 then lay out the structure or union (including the fields). */
6904
6905 TYPE_FIELDS (t) = fieldlist;
6906
6907 layout_type (t);
6908
6909 /* Give bit-fields their proper types. */
6910 {
6911 tree *fieldlistp = &fieldlist;
6912 while (*fieldlistp)
6913 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
6914 && TREE_TYPE (*fieldlistp) != error_mark_node)
6915 {
6916 unsigned HOST_WIDE_INT width
6917 = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
6918 tree type = TREE_TYPE (*fieldlistp);
6919 if (width != TYPE_PRECISION (type))
6920 {
6921 TREE_TYPE (*fieldlistp)
6922 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
6923 DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
6924 }
6925 DECL_INITIAL (*fieldlistp) = 0;
6926 }
6927 else
6928 fieldlistp = &TREE_CHAIN (*fieldlistp);
6929 }
6930
6931 /* Now we have the truly final field list.
6932 Store it in this type and in the variants. */
6933
6934 TYPE_FIELDS (t) = fieldlist;
6935
6936 /* If there are lots of fields, sort so we can look through them fast.
6937 We arbitrarily consider 16 or more elts to be "a lot". */
6938
6939 {
6940 int len = 0;
6941
6942 for (x = fieldlist; x; x = TREE_CHAIN (x))
6943 {
6944 if (len > 15 || DECL_NAME (x) == NULL)
6945 break;
6946 len += 1;
6947 }
6948
6949 if (len > 15)
6950 {
6951 tree *field_array;
6952 struct lang_type *space;
6953 struct sorted_fields_type *space2;
6954
6955 len += list_length (x);
6956
6957 /* Use the same allocation policy here that make_node uses, to
6958 ensure that this lives as long as the rest of the struct decl.
6959 All decls in an inline function need to be saved. */
6960
6961 space = GGC_CNEW (struct lang_type);
6962 space2 = GGC_NEWVAR (struct sorted_fields_type,
6963 sizeof (struct sorted_fields_type) + len * sizeof (tree));
6964
6965 len = 0;
6966 space->s = space2;
6967 field_array = &space2->elts[0];
6968 for (x = fieldlist; x; x = TREE_CHAIN (x))
6969 {
6970 field_array[len++] = x;
6971
6972 /* If there is anonymous struct or union, break out of the loop. */
6973 if (DECL_NAME (x) == NULL)
6974 break;
6975 }
6976 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
6977 if (x == NULL)
6978 {
6979 TYPE_LANG_SPECIFIC (t) = space;
6980 TYPE_LANG_SPECIFIC (t)->s->len = len;
6981 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
6982 qsort (field_array, len, sizeof (tree), field_decl_cmp);
6983 }
6984 }
6985 }
6986
6987 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
6988 {
6989 TYPE_FIELDS (x) = TYPE_FIELDS (t);
6990 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
6991 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
6992 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
6993 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
6994 }
6995
6996 /* If this was supposed to be a transparent union, but we can't
6997 make it one, warn and turn off the flag. */
6998 if (TREE_CODE (t) == UNION_TYPE
6999 && TYPE_TRANSPARENT_AGGR (t)
7000 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
7001 {
7002 TYPE_TRANSPARENT_AGGR (t) = 0;
7003 warning_at (loc, 0, "union cannot be made transparent");
7004 }
7005
7006 /* If this structure or union completes the type of any previous
7007 variable declaration, lay it out and output its rtl. */
7008 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
7009 x;
7010 x = TREE_CHAIN (x))
7011 {
7012 tree decl = TREE_VALUE (x);
7013 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
7014 layout_array_type (TREE_TYPE (decl));
7015 if (TREE_CODE (decl) != TYPE_DECL)
7016 {
7017 layout_decl (decl, 0);
7018 if (c_dialect_objc ())
7019 objc_check_decl (decl);
7020 rest_of_decl_compilation (decl, toplevel, 0);
7021 if (!toplevel)
7022 expand_decl (decl);
7023 }
7024 }
7025 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
7026
7027 /* Update type location to the one of the definition, instead of e.g.
7028 a forward declaration. */
7029 if (TYPE_STUB_DECL (t))
7030 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) = loc;
7031
7032 /* Finish debugging output for this type. */
7033 rest_of_type_compilation (t, toplevel);
7034
7035 /* If we're inside a function proper, i.e. not file-scope and not still
7036 parsing parameters, then arrange for the size of a variable sized type
7037 to be bound now. */
7038 if (cur_stmt_list && variably_modified_type_p (t, NULL_TREE))
7039 add_stmt (build_stmt (loc,
7040 DECL_EXPR, build_decl (loc, TYPE_DECL, NULL, t)));
7041
7042 if (warn_cxx_compat)
7043 warn_cxx_compat_finish_struct (fieldlist);
7044
7045 VEC_free (tree, heap, struct_parse_info->struct_types);
7046 VEC_free (c_binding_ptr, heap, struct_parse_info->fields);
7047 VEC_free (tree, heap, struct_parse_info->typedefs_seen);
7048 XDELETE (struct_parse_info);
7049
7050 struct_parse_info = enclosing_struct_parse_info;
7051
7052 /* If this struct is defined inside a struct, add it to
7053 struct_types. */
7054 if (warn_cxx_compat
7055 && struct_parse_info != NULL
7056 && !in_sizeof && !in_typeof && !in_alignof)
7057 VEC_safe_push (tree, heap, struct_parse_info->struct_types, t);
7058
7059 return t;
7060 }
7061
7062 /* Lay out the type T, and its element type, and so on. */
7063
7064 static void
7065 layout_array_type (tree t)
7066 {
7067 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
7068 layout_array_type (TREE_TYPE (t));
7069 layout_type (t);
7070 }
7071 \f
7072 /* Begin compiling the definition of an enumeration type.
7073 NAME is its name (or null if anonymous).
7074 LOC is the enum's location.
7075 Returns the type object, as yet incomplete.
7076 Also records info about it so that build_enumerator
7077 may be used to declare the individual values as they are read. */
7078
7079 tree
7080 start_enum (location_t loc, struct c_enum_contents *the_enum, tree name)
7081 {
7082 tree enumtype = NULL_TREE;
7083 location_t enumloc = UNKNOWN_LOCATION;
7084
7085 /* If this is the real definition for a previous forward reference,
7086 fill in the contents in the same object that used to be the
7087 forward reference. */
7088
7089 if (name != NULL_TREE)
7090 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1, &enumloc);
7091
7092 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
7093 {
7094 enumtype = make_node (ENUMERAL_TYPE);
7095 pushtag (loc, name, enumtype);
7096 }
7097
7098 if (C_TYPE_BEING_DEFINED (enumtype))
7099 error_at (loc, "nested redefinition of %<enum %E%>", name);
7100
7101 C_TYPE_BEING_DEFINED (enumtype) = 1;
7102
7103 if (TYPE_VALUES (enumtype) != 0)
7104 {
7105 /* This enum is a named one that has been declared already. */
7106 error_at (loc, "redeclaration of %<enum %E%>", name);
7107 if (enumloc != UNKNOWN_LOCATION)
7108 inform (enumloc, "originally defined here");
7109
7110 /* Completely replace its old definition.
7111 The old enumerators remain defined, however. */
7112 TYPE_VALUES (enumtype) = 0;
7113 }
7114
7115 the_enum->enum_next_value = integer_zero_node;
7116 the_enum->enum_overflow = 0;
7117
7118 if (flag_short_enums)
7119 TYPE_PACKED (enumtype) = 1;
7120
7121 /* FIXME: This will issue a warning for a use of a type defined
7122 within sizeof in a statement expr. This is not terribly serious
7123 as C++ doesn't permit statement exprs within sizeof anyhow. */
7124 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
7125 warning_at (loc, OPT_Wc___compat,
7126 "defining type in %qs expression is invalid in C++",
7127 (in_sizeof
7128 ? "sizeof"
7129 : (in_typeof ? "typeof" : "alignof")));
7130
7131 return enumtype;
7132 }
7133
7134 /* After processing and defining all the values of an enumeration type,
7135 install their decls in the enumeration type and finish it off.
7136 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
7137 and ATTRIBUTES are the specified attributes.
7138 Returns ENUMTYPE. */
7139
7140 tree
7141 finish_enum (tree enumtype, tree values, tree attributes)
7142 {
7143 tree pair, tem;
7144 tree minnode = 0, maxnode = 0;
7145 int precision, unsign;
7146 bool toplevel = (file_scope == current_scope);
7147 struct lang_type *lt;
7148
7149 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7150
7151 /* Calculate the maximum value of any enumerator in this type. */
7152
7153 if (values == error_mark_node)
7154 minnode = maxnode = integer_zero_node;
7155 else
7156 {
7157 minnode = maxnode = TREE_VALUE (values);
7158 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
7159 {
7160 tree value = TREE_VALUE (pair);
7161 if (tree_int_cst_lt (maxnode, value))
7162 maxnode = value;
7163 if (tree_int_cst_lt (value, minnode))
7164 minnode = value;
7165 }
7166 }
7167
7168 /* Construct the final type of this enumeration. It is the same
7169 as one of the integral types - the narrowest one that fits, except
7170 that normally we only go as narrow as int - and signed iff any of
7171 the values are negative. */
7172 unsign = (tree_int_cst_sgn (minnode) >= 0);
7173 precision = MAX (tree_int_cst_min_precision (minnode, unsign),
7174 tree_int_cst_min_precision (maxnode, unsign));
7175
7176 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
7177 {
7178 tem = c_common_type_for_size (precision, unsign);
7179 if (tem == NULL)
7180 {
7181 warning (0, "enumeration values exceed range of largest integer");
7182 tem = long_long_integer_type_node;
7183 }
7184 }
7185 else
7186 tem = unsign ? unsigned_type_node : integer_type_node;
7187
7188 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
7189 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
7190 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
7191 TYPE_SIZE (enumtype) = 0;
7192
7193 /* If the precision of the type was specific with an attribute and it
7194 was too small, give an error. Otherwise, use it. */
7195 if (TYPE_PRECISION (enumtype))
7196 {
7197 if (precision > TYPE_PRECISION (enumtype))
7198 error ("specified mode too small for enumeral values");
7199 }
7200 else
7201 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
7202
7203 layout_type (enumtype);
7204
7205 if (values != error_mark_node)
7206 {
7207 /* Change the type of the enumerators to be the enum type. We
7208 need to do this irrespective of the size of the enum, for
7209 proper type checking. Replace the DECL_INITIALs of the
7210 enumerators, and the value slots of the list, with copies
7211 that have the enum type; they cannot be modified in place
7212 because they may be shared (e.g. integer_zero_node) Finally,
7213 change the purpose slots to point to the names of the decls. */
7214 for (pair = values; pair; pair = TREE_CHAIN (pair))
7215 {
7216 tree enu = TREE_PURPOSE (pair);
7217 tree ini = DECL_INITIAL (enu);
7218
7219 TREE_TYPE (enu) = enumtype;
7220
7221 /* The ISO C Standard mandates enumerators to have type int,
7222 even though the underlying type of an enum type is
7223 unspecified. However, GCC allows enumerators of any
7224 integer type as an extensions. build_enumerator()
7225 converts any enumerators that fit in an int to type int,
7226 to avoid promotions to unsigned types when comparing
7227 integers with enumerators that fit in the int range.
7228 When -pedantic is given, build_enumerator() would have
7229 already warned about those that don't fit. Here we
7230 convert the rest to the enumerator type. */
7231 if (TREE_TYPE (ini) != integer_type_node)
7232 ini = convert (enumtype, ini);
7233
7234 DECL_INITIAL (enu) = ini;
7235 TREE_PURPOSE (pair) = DECL_NAME (enu);
7236 TREE_VALUE (pair) = ini;
7237 }
7238
7239 TYPE_VALUES (enumtype) = values;
7240 }
7241
7242 /* Record the min/max values so that we can warn about bit-field
7243 enumerations that are too small for the values. */
7244 lt = GGC_CNEW (struct lang_type);
7245 lt->enum_min = minnode;
7246 lt->enum_max = maxnode;
7247 TYPE_LANG_SPECIFIC (enumtype) = lt;
7248
7249 /* Fix up all variant types of this enum type. */
7250 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
7251 {
7252 if (tem == enumtype)
7253 continue;
7254 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
7255 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
7256 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
7257 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
7258 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
7259 SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
7260 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
7261 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
7262 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
7263 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
7264 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
7265 }
7266
7267 /* Finish debugging output for this type. */
7268 rest_of_type_compilation (enumtype, toplevel);
7269
7270 /* If this enum is defined inside a struct, add it to
7271 struct_types. */
7272 if (warn_cxx_compat
7273 && struct_parse_info != NULL
7274 && !in_sizeof && !in_typeof && !in_alignof)
7275 VEC_safe_push (tree, heap, struct_parse_info->struct_types, enumtype);
7276
7277 return enumtype;
7278 }
7279
7280 /* Build and install a CONST_DECL for one value of the
7281 current enumeration type (one that was begun with start_enum).
7282 LOC is the location of the enumerator.
7283 Return a tree-list containing the CONST_DECL and its value.
7284 Assignment of sequential values by default is handled here. */
7285
7286 tree
7287 build_enumerator (location_t loc,
7288 struct c_enum_contents *the_enum, tree name, tree value)
7289 {
7290 tree decl, type;
7291
7292 /* Validate and default VALUE. */
7293
7294 if (value != 0)
7295 {
7296 /* Don't issue more errors for error_mark_node (i.e. an
7297 undeclared identifier) - just ignore the value expression. */
7298 if (value == error_mark_node)
7299 value = 0;
7300 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
7301 {
7302 error_at (loc, "enumerator value for %qE is not an integer constant",
7303 name);
7304 value = 0;
7305 }
7306 else
7307 {
7308 if (TREE_CODE (value) != INTEGER_CST)
7309 {
7310 value = c_fully_fold (value, false, NULL);
7311 if (TREE_CODE (value) == INTEGER_CST)
7312 pedwarn (loc, OPT_pedantic,
7313 "enumerator value for %qE is not an integer "
7314 "constant expression", name);
7315 }
7316 if (TREE_CODE (value) != INTEGER_CST)
7317 {
7318 error ("enumerator value for %qE is not an integer constant",
7319 name);
7320 value = 0;
7321 }
7322 else
7323 {
7324 value = default_conversion (value);
7325 constant_expression_warning (value);
7326 }
7327 }
7328 }
7329
7330 /* Default based on previous value. */
7331 /* It should no longer be possible to have NON_LVALUE_EXPR
7332 in the default. */
7333 if (value == 0)
7334 {
7335 value = the_enum->enum_next_value;
7336 if (the_enum->enum_overflow)
7337 error_at (loc, "overflow in enumeration values");
7338 }
7339 /* Even though the underlying type of an enum is unspecified, the
7340 type of enumeration constants is explicitly defined as int
7341 (6.4.4.3/2 in the C99 Standard). GCC allows any integer type as
7342 an extension. */
7343 else if (!int_fits_type_p (value, integer_type_node))
7344 pedwarn (loc, OPT_pedantic,
7345 "ISO C restricts enumerator values to range of %<int%>");
7346
7347 /* The ISO C Standard mandates enumerators to have type int, even
7348 though the underlying type of an enum type is unspecified.
7349 However, GCC allows enumerators of any integer type as an
7350 extensions. Here we convert any enumerators that fit in an int
7351 to type int, to avoid promotions to unsigned types when comparing
7352 integers with enumerators that fit in the int range. When
7353 -pedantic is given, we would have already warned about those that
7354 don't fit. We have to do this here rather than in finish_enum
7355 because this value may be used to define more enumerators. */
7356 if (int_fits_type_p (value, integer_type_node))
7357 value = convert (integer_type_node, value);
7358
7359 /* Set basis for default for next value. */
7360 the_enum->enum_next_value
7361 = build_binary_op
7362 (EXPR_HAS_LOCATION (value) ? EXPR_LOCATION (value) : input_location,
7363 PLUS_EXPR, value, integer_one_node, 0);
7364 the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
7365
7366 /* Now create a declaration for the enum value name. */
7367
7368 type = TREE_TYPE (value);
7369 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
7370 TYPE_PRECISION (integer_type_node)),
7371 (TYPE_PRECISION (type)
7372 >= TYPE_PRECISION (integer_type_node)
7373 && TYPE_UNSIGNED (type)));
7374
7375 decl = build_decl (loc, CONST_DECL, name, type);
7376 DECL_INITIAL (decl) = convert (type, value);
7377 pushdecl (decl);
7378
7379 return tree_cons (decl, value, NULL_TREE);
7380 }
7381
7382 \f
7383 /* Create the FUNCTION_DECL for a function definition.
7384 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
7385 the declaration; they describe the function's name and the type it returns,
7386 but twisted together in a fashion that parallels the syntax of C.
7387
7388 This function creates a binding context for the function body
7389 as well as setting up the FUNCTION_DECL in current_function_decl.
7390
7391 Returns 1 on success. If the DECLARATOR is not suitable for a function
7392 (it defines a datum instead), we return 0, which tells
7393 yyparse to report a parse error. */
7394
7395 int
7396 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
7397 tree attributes)
7398 {
7399 tree decl1, old_decl;
7400 tree restype, resdecl;
7401 location_t loc;
7402
7403 current_function_returns_value = 0; /* Assume, until we see it does. */
7404 current_function_returns_null = 0;
7405 current_function_returns_abnormally = 0;
7406 warn_about_return_type = 0;
7407 c_switch_stack = NULL;
7408
7409 /* Indicate no valid break/continue context by setting these variables
7410 to some non-null, non-label value. We'll notice and emit the proper
7411 error message in c_finish_bc_stmt. */
7412 c_break_label = c_cont_label = size_zero_node;
7413
7414 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
7415 &attributes, NULL, NULL, DEPRECATED_NORMAL);
7416
7417 /* If the declarator is not suitable for a function definition,
7418 cause a syntax error. */
7419 if (decl1 == 0)
7420 return 0;
7421
7422 loc = DECL_SOURCE_LOCATION (decl1);
7423
7424 decl_attributes (&decl1, attributes, 0);
7425
7426 if (DECL_DECLARED_INLINE_P (decl1)
7427 && DECL_UNINLINABLE (decl1)
7428 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
7429 warning_at (loc, OPT_Wattributes,
7430 "inline function %qD given attribute noinline",
7431 decl1);
7432
7433 /* Handle gnu_inline attribute. */
7434 if (declspecs->inline_p
7435 && !flag_gnu89_inline
7436 && TREE_CODE (decl1) == FUNCTION_DECL
7437 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
7438 || current_function_decl))
7439 {
7440 if (declspecs->storage_class != csc_static)
7441 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
7442 }
7443
7444 announce_function (decl1);
7445
7446 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
7447 {
7448 error_at (loc, "return type is an incomplete type");
7449 /* Make it return void instead. */
7450 TREE_TYPE (decl1)
7451 = build_function_type (void_type_node,
7452 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
7453 }
7454
7455 if (warn_about_return_type)
7456 pedwarn_c99 (loc, flag_isoc99 ? 0
7457 : (warn_return_type ? OPT_Wreturn_type : OPT_Wimplicit_int),
7458 "return type defaults to %<int%>");
7459
7460 /* Make the init_value nonzero so pushdecl knows this is not tentative.
7461 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
7462 DECL_INITIAL (decl1) = error_mark_node;
7463
7464 /* A nested function is not global. */
7465 if (current_function_decl != 0)
7466 TREE_PUBLIC (decl1) = 0;
7467
7468 /* If this definition isn't a prototype and we had a prototype declaration
7469 before, copy the arg type info from that prototype. */
7470 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
7471 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
7472 old_decl = 0;
7473 current_function_prototype_locus = UNKNOWN_LOCATION;
7474 current_function_prototype_built_in = false;
7475 current_function_prototype_arg_types = NULL_TREE;
7476 if (TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
7477 {
7478 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
7479 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
7480 TREE_TYPE (TREE_TYPE (old_decl))))
7481 {
7482 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
7483 TREE_TYPE (decl1));
7484 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
7485 current_function_prototype_built_in
7486 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
7487 current_function_prototype_arg_types
7488 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
7489 }
7490 if (TREE_PUBLIC (decl1))
7491 {
7492 /* If there is an external prototype declaration of this
7493 function, record its location but do not copy information
7494 to this decl. This may be an invisible declaration
7495 (built-in or in a scope which has finished) or simply
7496 have more refined argument types than any declaration
7497 found above. */
7498 struct c_binding *b;
7499 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
7500 if (B_IN_SCOPE (b, external_scope))
7501 break;
7502 if (b)
7503 {
7504 tree ext_decl, ext_type;
7505 ext_decl = b->decl;
7506 ext_type = b->u.type ? b->u.type : TREE_TYPE (ext_decl);
7507 if (TREE_CODE (ext_type) == FUNCTION_TYPE
7508 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
7509 TREE_TYPE (ext_type)))
7510 {
7511 current_function_prototype_locus
7512 = DECL_SOURCE_LOCATION (ext_decl);
7513 current_function_prototype_built_in
7514 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
7515 current_function_prototype_arg_types
7516 = TYPE_ARG_TYPES (ext_type);
7517 }
7518 }
7519 }
7520 }
7521
7522 /* Optionally warn of old-fashioned def with no previous prototype. */
7523 if (warn_strict_prototypes
7524 && old_decl != error_mark_node
7525 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
7526 && C_DECL_ISNT_PROTOTYPE (old_decl))
7527 warning_at (loc, OPT_Wstrict_prototypes,
7528 "function declaration isn%'t a prototype");
7529 /* Optionally warn of any global def with no previous prototype. */
7530 else if (warn_missing_prototypes
7531 && old_decl != error_mark_node
7532 && TREE_PUBLIC (decl1)
7533 && !MAIN_NAME_P (DECL_NAME (decl1))
7534 && C_DECL_ISNT_PROTOTYPE (old_decl))
7535 warning_at (loc, OPT_Wmissing_prototypes,
7536 "no previous prototype for %qD", decl1);
7537 /* Optionally warn of any def with no previous prototype
7538 if the function has already been used. */
7539 else if (warn_missing_prototypes
7540 && old_decl != 0
7541 && old_decl != error_mark_node
7542 && TREE_USED (old_decl)
7543 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
7544 warning_at (loc, OPT_Wmissing_prototypes,
7545 "%qD was used with no prototype before its definition", decl1);
7546 /* Optionally warn of any global def with no previous declaration. */
7547 else if (warn_missing_declarations
7548 && TREE_PUBLIC (decl1)
7549 && old_decl == 0
7550 && !MAIN_NAME_P (DECL_NAME (decl1)))
7551 warning_at (loc, OPT_Wmissing_declarations,
7552 "no previous declaration for %qD",
7553 decl1);
7554 /* Optionally warn of any def with no previous declaration
7555 if the function has already been used. */
7556 else if (warn_missing_declarations
7557 && old_decl != 0
7558 && old_decl != error_mark_node
7559 && TREE_USED (old_decl)
7560 && C_DECL_IMPLICIT (old_decl))
7561 warning_at (loc, OPT_Wmissing_declarations,
7562 "%qD was used with no declaration before its definition", decl1);
7563
7564 /* This function exists in static storage.
7565 (This does not mean `static' in the C sense!) */
7566 TREE_STATIC (decl1) = 1;
7567
7568 /* This is the earliest point at which we might know the assembler
7569 name of the function. Thus, if it's set before this, die horribly. */
7570 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
7571
7572 /* If #pragma weak was used, mark the decl weak now. */
7573 if (current_scope == file_scope)
7574 maybe_apply_pragma_weak (decl1);
7575
7576 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
7577 if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
7578 {
7579 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
7580 != integer_type_node)
7581 pedwarn (loc, OPT_Wmain, "return type of %qD is not %<int%>", decl1);
7582
7583 check_main_parameter_types (decl1);
7584
7585 if (!TREE_PUBLIC (decl1))
7586 pedwarn (loc, OPT_Wmain,
7587 "%qD is normally a non-static function", decl1);
7588 }
7589
7590 /* Record the decl so that the function name is defined.
7591 If we already have a decl for this name, and it is a FUNCTION_DECL,
7592 use the old decl. */
7593
7594 current_function_decl = pushdecl (decl1);
7595
7596 push_scope ();
7597 declare_parm_level ();
7598
7599 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
7600 resdecl = build_decl (loc, RESULT_DECL, NULL_TREE, restype);
7601 DECL_ARTIFICIAL (resdecl) = 1;
7602 DECL_IGNORED_P (resdecl) = 1;
7603 DECL_RESULT (current_function_decl) = resdecl;
7604
7605 start_fname_decls ();
7606
7607 return 1;
7608 }
7609 \f
7610 /* Subroutine of store_parm_decls which handles new-style function
7611 definitions (prototype format). The parms already have decls, so we
7612 need only record them as in effect and complain if any redundant
7613 old-style parm decls were written. */
7614 static void
7615 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
7616 {
7617 tree decl;
7618
7619 if (current_scope->bindings)
7620 {
7621 error_at (DECL_SOURCE_LOCATION (fndecl),
7622 "old-style parameter declarations in prototyped "
7623 "function definition");
7624
7625 /* Get rid of the old-style declarations. */
7626 pop_scope ();
7627 push_scope ();
7628 }
7629 /* Don't issue this warning for nested functions, and don't issue this
7630 warning if we got here because ARG_INFO_TYPES was error_mark_node
7631 (this happens when a function definition has just an ellipsis in
7632 its parameter list). */
7633 else if (!in_system_header && !current_function_scope
7634 && arg_info->types != error_mark_node)
7635 warning_at (DECL_SOURCE_LOCATION (fndecl), OPT_Wtraditional,
7636 "traditional C rejects ISO C style function definitions");
7637
7638 /* Now make all the parameter declarations visible in the function body.
7639 We can bypass most of the grunt work of pushdecl. */
7640 for (decl = arg_info->parms; decl; decl = TREE_CHAIN (decl))
7641 {
7642 DECL_CONTEXT (decl) = current_function_decl;
7643 if (DECL_NAME (decl))
7644 {
7645 bind (DECL_NAME (decl), decl, current_scope,
7646 /*invisible=*/false, /*nested=*/false,
7647 UNKNOWN_LOCATION);
7648 if (!TREE_USED (decl))
7649 warn_if_shadowing (decl);
7650 }
7651 else
7652 error_at (DECL_SOURCE_LOCATION (decl), "parameter name omitted");
7653 }
7654
7655 /* Record the parameter list in the function declaration. */
7656 DECL_ARGUMENTS (fndecl) = arg_info->parms;
7657
7658 /* Now make all the ancillary declarations visible, likewise. */
7659 for (decl = arg_info->others; decl; decl = TREE_CHAIN (decl))
7660 {
7661 DECL_CONTEXT (decl) = current_function_decl;
7662 if (DECL_NAME (decl))
7663 bind (DECL_NAME (decl), decl, current_scope,
7664 /*invisible=*/false,
7665 /*nested=*/(TREE_CODE (decl) == FUNCTION_DECL),
7666 UNKNOWN_LOCATION);
7667 }
7668
7669 /* And all the tag declarations. */
7670 for (decl = arg_info->tags; decl; decl = TREE_CHAIN (decl))
7671 if (TREE_PURPOSE (decl))
7672 bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope,
7673 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
7674 }
7675
7676 /* Subroutine of store_parm_decls which handles old-style function
7677 definitions (separate parameter list and declarations). */
7678
7679 static void
7680 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
7681 {
7682 struct c_binding *b;
7683 tree parm, decl, last;
7684 tree parmids = arg_info->parms;
7685 struct pointer_set_t *seen_args = pointer_set_create ();
7686
7687 if (!in_system_header)
7688 warning_at (DECL_SOURCE_LOCATION (fndecl),
7689 OPT_Wold_style_definition, "old-style function definition");
7690
7691 /* Match each formal parameter name with its declaration. Save each
7692 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
7693 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
7694 {
7695 if (TREE_VALUE (parm) == 0)
7696 {
7697 error_at (DECL_SOURCE_LOCATION (fndecl),
7698 "parameter name missing from parameter list");
7699 TREE_PURPOSE (parm) = 0;
7700 continue;
7701 }
7702
7703 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
7704 if (b && B_IN_CURRENT_SCOPE (b))
7705 {
7706 decl = b->decl;
7707 /* If we got something other than a PARM_DECL it is an error. */
7708 if (TREE_CODE (decl) != PARM_DECL)
7709 error_at (DECL_SOURCE_LOCATION (decl),
7710 "%qD declared as a non-parameter", decl);
7711 /* If the declaration is already marked, we have a duplicate
7712 name. Complain and ignore the duplicate. */
7713 else if (pointer_set_contains (seen_args, decl))
7714 {
7715 error_at (DECL_SOURCE_LOCATION (decl),
7716 "multiple parameters named %qD", decl);
7717 TREE_PURPOSE (parm) = 0;
7718 continue;
7719 }
7720 /* If the declaration says "void", complain and turn it into
7721 an int. */
7722 else if (VOID_TYPE_P (TREE_TYPE (decl)))
7723 {
7724 error_at (DECL_SOURCE_LOCATION (decl),
7725 "parameter %qD declared with void type", decl);
7726 TREE_TYPE (decl) = integer_type_node;
7727 DECL_ARG_TYPE (decl) = integer_type_node;
7728 layout_decl (decl, 0);
7729 }
7730 warn_if_shadowing (decl);
7731 }
7732 /* If no declaration found, default to int. */
7733 else
7734 {
7735 /* FIXME diagnostics: This should be the location of the argument,
7736 not the FNDECL. E.g., for an old-style declaration
7737
7738 int f10(v) { blah; }
7739
7740 We should use the location of the V, not the F10.
7741 Unfortunately, the V is an IDENTIFIER_NODE which has no
7742 location. In the future we need locations for c_arg_info
7743 entries.
7744
7745 See gcc.dg/Wshadow-3.c for an example of this problem. */
7746 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
7747 PARM_DECL, TREE_VALUE (parm), integer_type_node);
7748 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
7749 pushdecl (decl);
7750 warn_if_shadowing (decl);
7751
7752 if (flag_isoc99)
7753 pedwarn (DECL_SOURCE_LOCATION (decl),
7754 0, "type of %qD defaults to %<int%>", decl);
7755 else
7756 warning_at (DECL_SOURCE_LOCATION (decl),
7757 OPT_Wmissing_parameter_type,
7758 "type of %qD defaults to %<int%>", decl);
7759 }
7760
7761 TREE_PURPOSE (parm) = decl;
7762 pointer_set_insert (seen_args, decl);
7763 }
7764
7765 /* Now examine the parms chain for incomplete declarations
7766 and declarations with no corresponding names. */
7767
7768 for (b = current_scope->bindings; b; b = b->prev)
7769 {
7770 parm = b->decl;
7771 if (TREE_CODE (parm) != PARM_DECL)
7772 continue;
7773
7774 if (TREE_TYPE (parm) != error_mark_node
7775 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
7776 {
7777 error_at (DECL_SOURCE_LOCATION (parm),
7778 "parameter %qD has incomplete type", parm);
7779 TREE_TYPE (parm) = error_mark_node;
7780 }
7781
7782 if (!pointer_set_contains (seen_args, parm))
7783 {
7784 error_at (DECL_SOURCE_LOCATION (parm),
7785 "declaration for parameter %qD but no such parameter",
7786 parm);
7787
7788 /* Pretend the parameter was not missing.
7789 This gets us to a standard state and minimizes
7790 further error messages. */
7791 parmids = chainon (parmids, tree_cons (parm, 0, 0));
7792 }
7793 }
7794
7795 /* Chain the declarations together in the order of the list of
7796 names. Store that chain in the function decl, replacing the
7797 list of names. Update the current scope to match. */
7798 DECL_ARGUMENTS (fndecl) = 0;
7799
7800 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
7801 if (TREE_PURPOSE (parm))
7802 break;
7803 if (parm && TREE_PURPOSE (parm))
7804 {
7805 last = TREE_PURPOSE (parm);
7806 DECL_ARGUMENTS (fndecl) = last;
7807
7808 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
7809 if (TREE_PURPOSE (parm))
7810 {
7811 TREE_CHAIN (last) = TREE_PURPOSE (parm);
7812 last = TREE_PURPOSE (parm);
7813 }
7814 TREE_CHAIN (last) = 0;
7815 }
7816
7817 pointer_set_destroy (seen_args);
7818
7819 /* If there was a previous prototype,
7820 set the DECL_ARG_TYPE of each argument according to
7821 the type previously specified, and report any mismatches. */
7822
7823 if (current_function_prototype_arg_types)
7824 {
7825 tree type;
7826 for (parm = DECL_ARGUMENTS (fndecl),
7827 type = current_function_prototype_arg_types;
7828 parm || (type && TREE_VALUE (type) != error_mark_node
7829 && (TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node));
7830 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
7831 {
7832 if (parm == 0 || type == 0
7833 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
7834 {
7835 if (current_function_prototype_built_in)
7836 warning_at (DECL_SOURCE_LOCATION (fndecl),
7837 0, "number of arguments doesn%'t match "
7838 "built-in prototype");
7839 else
7840 {
7841 /* FIXME diagnostics: This should be the location of
7842 FNDECL, but there is bug when a prototype is
7843 declared inside function context, but defined
7844 outside of it (e.g., gcc.dg/pr15698-2.c). In
7845 which case FNDECL gets the location of the
7846 prototype, not the definition. */
7847 error_at (input_location,
7848 "number of arguments doesn%'t match prototype");
7849
7850 error_at (current_function_prototype_locus,
7851 "prototype declaration");
7852 }
7853 break;
7854 }
7855 /* Type for passing arg must be consistent with that
7856 declared for the arg. ISO C says we take the unqualified
7857 type for parameters declared with qualified type. */
7858 if (TREE_TYPE (parm) != error_mark_node
7859 && TREE_TYPE (type) != error_mark_node
7860 && !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
7861 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
7862 {
7863 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
7864 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
7865 {
7866 /* Adjust argument to match prototype. E.g. a previous
7867 `int foo(float);' prototype causes
7868 `int foo(x) float x; {...}' to be treated like
7869 `int foo(float x) {...}'. This is particularly
7870 useful for argument types like uid_t. */
7871 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
7872
7873 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
7874 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
7875 && TYPE_PRECISION (TREE_TYPE (parm))
7876 < TYPE_PRECISION (integer_type_node))
7877 DECL_ARG_TYPE (parm) = integer_type_node;
7878
7879 /* ??? Is it possible to get here with a
7880 built-in prototype or will it always have
7881 been diagnosed as conflicting with an
7882 old-style definition and discarded? */
7883 if (current_function_prototype_built_in)
7884 warning_at (DECL_SOURCE_LOCATION (parm),
7885 OPT_pedantic, "promoted argument %qD "
7886 "doesn%'t match built-in prototype", parm);
7887 else
7888 {
7889 pedwarn (DECL_SOURCE_LOCATION (parm),
7890 OPT_pedantic, "promoted argument %qD "
7891 "doesn%'t match prototype", parm);
7892 pedwarn (current_function_prototype_locus, OPT_pedantic,
7893 "prototype declaration");
7894 }
7895 }
7896 else
7897 {
7898 if (current_function_prototype_built_in)
7899 warning_at (DECL_SOURCE_LOCATION (parm),
7900 0, "argument %qD doesn%'t match "
7901 "built-in prototype", parm);
7902 else
7903 {
7904 error_at (DECL_SOURCE_LOCATION (parm),
7905 "argument %qD doesn%'t match prototype", parm);
7906 error_at (current_function_prototype_locus,
7907 "prototype declaration");
7908 }
7909 }
7910 }
7911 }
7912 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
7913 }
7914
7915 /* Otherwise, create a prototype that would match. */
7916
7917 else
7918 {
7919 tree actual = 0, last = 0, type;
7920
7921 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
7922 {
7923 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
7924 if (last)
7925 TREE_CHAIN (last) = type;
7926 else
7927 actual = type;
7928 last = type;
7929 }
7930 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
7931 if (last)
7932 TREE_CHAIN (last) = type;
7933 else
7934 actual = type;
7935
7936 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
7937 of the type of this function, but we need to avoid having this
7938 affect the types of other similarly-typed functions, so we must
7939 first force the generation of an identical (but separate) type
7940 node for the relevant function type. The new node we create
7941 will be a variant of the main variant of the original function
7942 type. */
7943
7944 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
7945
7946 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
7947 }
7948 }
7949
7950 /* Store parameter declarations passed in ARG_INFO into the current
7951 function declaration. */
7952
7953 void
7954 store_parm_decls_from (struct c_arg_info *arg_info)
7955 {
7956 current_function_arg_info = arg_info;
7957 store_parm_decls ();
7958 }
7959
7960 /* Store the parameter declarations into the current function declaration.
7961 This is called after parsing the parameter declarations, before
7962 digesting the body of the function.
7963
7964 For an old-style definition, construct a prototype out of the old-style
7965 parameter declarations and inject it into the function's type. */
7966
7967 void
7968 store_parm_decls (void)
7969 {
7970 tree fndecl = current_function_decl;
7971 bool proto;
7972
7973 /* The argument information block for FNDECL. */
7974 struct c_arg_info *arg_info = current_function_arg_info;
7975 current_function_arg_info = 0;
7976
7977 /* True if this definition is written with a prototype. Note:
7978 despite C99 6.7.5.3p14, we can *not* treat an empty argument
7979 list in a function definition as equivalent to (void) -- an
7980 empty argument list specifies the function has no parameters,
7981 but only (void) sets up a prototype for future calls. */
7982 proto = arg_info->types != 0;
7983
7984 if (proto)
7985 store_parm_decls_newstyle (fndecl, arg_info);
7986 else
7987 store_parm_decls_oldstyle (fndecl, arg_info);
7988
7989 /* The next call to push_scope will be a function body. */
7990
7991 next_is_function_body = true;
7992
7993 /* Write a record describing this function definition to the prototypes
7994 file (if requested). */
7995
7996 gen_aux_info_record (fndecl, 1, 0, proto);
7997
7998 /* Initialize the RTL code for the function. */
7999 allocate_struct_function (fndecl, false);
8000
8001 /* Begin the statement tree for this function. */
8002 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
8003
8004 /* ??? Insert the contents of the pending sizes list into the function
8005 to be evaluated. The only reason left to have this is
8006 void foo(int n, int array[n++])
8007 because we throw away the array type in favor of a pointer type, and
8008 thus won't naturally see the SAVE_EXPR containing the increment. All
8009 other pending sizes would be handled by gimplify_parameters. */
8010 {
8011 VEC(tree,gc) *pending_sizes = get_pending_sizes ();
8012 tree t;
8013 int i;
8014
8015 for (i = 0; VEC_iterate (tree, pending_sizes, i, t); i++)
8016 add_stmt (t);
8017 }
8018
8019 /* Even though we're inside a function body, we still don't want to
8020 call expand_expr to calculate the size of a variable-sized array.
8021 We haven't necessarily assigned RTL to all variables yet, so it's
8022 not safe to try to expand expressions involving them. */
8023 cfun->dont_save_pending_sizes_p = 1;
8024 }
8025 \f
8026
8027 /* Finish up a function declaration and compile that function
8028 all the way to assembler language output. The free the storage
8029 for the function definition.
8030
8031 This is called after parsing the body of the function definition. */
8032
8033 void
8034 finish_function (void)
8035 {
8036 tree fndecl = current_function_decl;
8037
8038 if (TREE_CODE (fndecl) == FUNCTION_DECL
8039 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
8040 {
8041 tree args = DECL_ARGUMENTS (fndecl);
8042 for (; args; args = TREE_CHAIN (args))
8043 {
8044 tree type = TREE_TYPE (args);
8045 if (INTEGRAL_TYPE_P (type)
8046 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
8047 DECL_ARG_TYPE (args) = integer_type_node;
8048 }
8049 }
8050
8051 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
8052 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
8053
8054 /* Must mark the RESULT_DECL as being in this function. */
8055
8056 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
8057 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
8058
8059 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted
8060 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
8061 == integer_type_node && flag_isoc99)
8062 {
8063 /* Hack. We don't want the middle-end to warn that this return
8064 is unreachable, so we mark its location as special. Using
8065 UNKNOWN_LOCATION has the problem that it gets clobbered in
8066 annotate_one_with_locus. A cleaner solution might be to
8067 ensure ! should_carry_locus_p (stmt), but that needs a flag.
8068 */
8069 c_finish_return (BUILTINS_LOCATION, integer_zero_node, NULL_TREE);
8070 }
8071
8072 /* Tie off the statement tree for this function. */
8073 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
8074
8075 finish_fname_decls ();
8076
8077 /* Complain if there's just no return statement. */
8078 if (warn_return_type
8079 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
8080 && !current_function_returns_value && !current_function_returns_null
8081 /* Don't complain if we are no-return. */
8082 && !current_function_returns_abnormally
8083 /* Don't complain if we are declared noreturn. */
8084 && !TREE_THIS_VOLATILE (fndecl)
8085 /* Don't warn for main(). */
8086 && !MAIN_NAME_P (DECL_NAME (fndecl))
8087 /* Or if they didn't actually specify a return type. */
8088 && !C_FUNCTION_IMPLICIT_INT (fndecl)
8089 /* Normally, with -Wreturn-type, flow will complain, but we might
8090 optimize out static functions. */
8091 && !TREE_PUBLIC (fndecl))
8092 {
8093 warning (OPT_Wreturn_type,
8094 "no return statement in function returning non-void");
8095 TREE_NO_WARNING (fndecl) = 1;
8096 }
8097
8098 /* Complain about parameters that are only set, but never otherwise used. */
8099 if (warn_unused_but_set_parameter)
8100 {
8101 tree decl;
8102
8103 for (decl = DECL_ARGUMENTS (fndecl);
8104 decl;
8105 decl = TREE_CHAIN (decl))
8106 if (TREE_USED (decl)
8107 && TREE_CODE (decl) == PARM_DECL
8108 && !DECL_READ_P (decl)
8109 && DECL_NAME (decl)
8110 && !DECL_ARTIFICIAL (decl)
8111 && !TREE_NO_WARNING (decl))
8112 warning_at (DECL_SOURCE_LOCATION (decl),
8113 OPT_Wunused_but_set_parameter,
8114 "parameter %qD set but not used", decl);
8115 }
8116
8117 /* Store the end of the function, so that we get good line number
8118 info for the epilogue. */
8119 cfun->function_end_locus = input_location;
8120
8121 /* Finalize the ELF visibility for the function. */
8122 c_determine_visibility (fndecl);
8123
8124 /* For GNU C extern inline functions disregard inline limits. */
8125 if (DECL_EXTERNAL (fndecl)
8126 && DECL_DECLARED_INLINE_P (fndecl))
8127 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
8128
8129 /* Genericize before inlining. Delay genericizing nested functions
8130 until their parent function is genericized. Since finalizing
8131 requires GENERIC, delay that as well. */
8132
8133 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
8134 && !undef_nested_function)
8135 {
8136 if (!decl_function_context (fndecl))
8137 {
8138 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
8139 c_genericize (fndecl);
8140
8141 /* ??? Objc emits functions after finalizing the compilation unit.
8142 This should be cleaned up later and this conditional removed. */
8143 if (cgraph_global_info_ready)
8144 {
8145 cgraph_add_new_function (fndecl, false);
8146 return;
8147 }
8148 cgraph_finalize_function (fndecl, false);
8149 }
8150 else
8151 {
8152 /* Register this function with cgraph just far enough to get it
8153 added to our parent's nested function list. Handy, since the
8154 C front end doesn't have such a list. */
8155 (void) cgraph_node (fndecl);
8156 }
8157 }
8158
8159 if (!decl_function_context (fndecl))
8160 undef_nested_function = false;
8161
8162 /* We're leaving the context of this function, so zap cfun.
8163 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
8164 tree_rest_of_compilation. */
8165 set_cfun (NULL);
8166 current_function_decl = NULL;
8167 }
8168 \f
8169 /* Check the declarations given in a for-loop for satisfying the C99
8170 constraints. If exactly one such decl is found, return it. LOC is
8171 the location of the opening parenthesis of the for loop. */
8172
8173 tree
8174 check_for_loop_decls (location_t loc)
8175 {
8176 struct c_binding *b;
8177 tree one_decl = NULL_TREE;
8178 int n_decls = 0;
8179
8180 if (!flag_isoc99)
8181 {
8182 static bool hint = true;
8183 /* If we get here, declarations have been used in a for loop without
8184 the C99 for loop scope. This doesn't make much sense, so don't
8185 allow it. */
8186 error_at (loc, "%<for%> loop initial declarations "
8187 "are only allowed in C99 mode");
8188 if (hint)
8189 {
8190 inform (loc,
8191 "use option -std=c99 or -std=gnu99 to compile your code");
8192 hint = false;
8193 }
8194 return NULL_TREE;
8195 }
8196 /* C99 subclause 6.8.5 paragraph 3:
8197
8198 [#3] The declaration part of a for statement shall only
8199 declare identifiers for objects having storage class auto or
8200 register.
8201
8202 It isn't clear whether, in this sentence, "identifiers" binds to
8203 "shall only declare" or to "objects" - that is, whether all identifiers
8204 declared must be identifiers for objects, or whether the restriction
8205 only applies to those that are. (A question on this in comp.std.c
8206 in November 2000 received no answer.) We implement the strictest
8207 interpretation, to avoid creating an extension which later causes
8208 problems. */
8209
8210 for (b = current_scope->bindings; b; b = b->prev)
8211 {
8212 tree id = b->id;
8213 tree decl = b->decl;
8214
8215 if (!id)
8216 continue;
8217
8218 switch (TREE_CODE (decl))
8219 {
8220 case VAR_DECL:
8221 {
8222 location_t decl_loc = DECL_SOURCE_LOCATION (decl);
8223 if (TREE_STATIC (decl))
8224 error_at (decl_loc,
8225 "declaration of static variable %qD in %<for%> loop "
8226 "initial declaration", decl);
8227 else if (DECL_EXTERNAL (decl))
8228 error_at (decl_loc,
8229 "declaration of %<extern%> variable %qD in %<for%> loop "
8230 "initial declaration", decl);
8231 }
8232 break;
8233
8234 case RECORD_TYPE:
8235 error_at (loc,
8236 "%<struct %E%> declared in %<for%> loop initial "
8237 "declaration", id);
8238 break;
8239 case UNION_TYPE:
8240 error_at (loc,
8241 "%<union %E%> declared in %<for%> loop initial declaration",
8242 id);
8243 break;
8244 case ENUMERAL_TYPE:
8245 error_at (loc, "%<enum %E%> declared in %<for%> loop "
8246 "initial declaration", id);
8247 break;
8248 default:
8249 error_at (loc, "declaration of non-variable "
8250 "%qD in %<for%> loop initial declaration", decl);
8251 }
8252
8253 n_decls++;
8254 one_decl = decl;
8255 }
8256
8257 return n_decls == 1 ? one_decl : NULL_TREE;
8258 }
8259 \f
8260 /* Save and reinitialize the variables
8261 used during compilation of a C function. */
8262
8263 void
8264 c_push_function_context (void)
8265 {
8266 struct language_function *p;
8267 p = GGC_NEW (struct language_function);
8268 cfun->language = p;
8269
8270 p->base.x_stmt_tree = c_stmt_tree;
8271 p->x_break_label = c_break_label;
8272 p->x_cont_label = c_cont_label;
8273 p->x_switch_stack = c_switch_stack;
8274 p->arg_info = current_function_arg_info;
8275 p->returns_value = current_function_returns_value;
8276 p->returns_null = current_function_returns_null;
8277 p->returns_abnormally = current_function_returns_abnormally;
8278 p->warn_about_return_type = warn_about_return_type;
8279
8280 push_function_context ();
8281 }
8282
8283 /* Restore the variables used during compilation of a C function. */
8284
8285 void
8286 c_pop_function_context (void)
8287 {
8288 struct language_function *p;
8289
8290 pop_function_context ();
8291 p = cfun->language;
8292 cfun->language = NULL;
8293
8294 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
8295 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
8296 {
8297 /* Stop pointing to the local nodes about to be freed. */
8298 /* But DECL_INITIAL must remain nonzero so we know this
8299 was an actual function definition. */
8300 DECL_INITIAL (current_function_decl) = error_mark_node;
8301 DECL_ARGUMENTS (current_function_decl) = 0;
8302 }
8303
8304 c_stmt_tree = p->base.x_stmt_tree;
8305 c_break_label = p->x_break_label;
8306 c_cont_label = p->x_cont_label;
8307 c_switch_stack = p->x_switch_stack;
8308 current_function_arg_info = p->arg_info;
8309 current_function_returns_value = p->returns_value;
8310 current_function_returns_null = p->returns_null;
8311 current_function_returns_abnormally = p->returns_abnormally;
8312 warn_about_return_type = p->warn_about_return_type;
8313 }
8314
8315 /* The functions below are required for functionality of doing
8316 function at once processing in the C front end. Currently these
8317 functions are not called from anywhere in the C front end, but as
8318 these changes continue, that will change. */
8319
8320 /* Returns the stmt_tree (if any) to which statements are currently
8321 being added. If there is no active statement-tree, NULL is
8322 returned. */
8323
8324 stmt_tree
8325 current_stmt_tree (void)
8326 {
8327 return &c_stmt_tree;
8328 }
8329
8330 /* Return the global value of T as a symbol. */
8331
8332 tree
8333 identifier_global_value (tree t)
8334 {
8335 struct c_binding *b;
8336
8337 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
8338 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
8339 return b->decl;
8340
8341 return 0;
8342 }
8343
8344 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
8345 otherwise the name is found in ridpointers from RID_INDEX. */
8346
8347 void
8348 record_builtin_type (enum rid rid_index, const char *name, tree type)
8349 {
8350 tree id, decl;
8351 if (name == 0)
8352 id = ridpointers[(int) rid_index];
8353 else
8354 id = get_identifier (name);
8355 decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, id, type);
8356 pushdecl (decl);
8357 if (debug_hooks->type_decl)
8358 debug_hooks->type_decl (decl, false);
8359 }
8360
8361 /* Build the void_list_node (void_type_node having been created). */
8362 tree
8363 build_void_list_node (void)
8364 {
8365 tree t = build_tree_list (NULL_TREE, void_type_node);
8366 return t;
8367 }
8368
8369 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
8370
8371 struct c_parm *
8372 build_c_parm (struct c_declspecs *specs, tree attrs,
8373 struct c_declarator *declarator)
8374 {
8375 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
8376 ret->specs = specs;
8377 ret->attrs = attrs;
8378 ret->declarator = declarator;
8379 return ret;
8380 }
8381
8382 /* Return a declarator with nested attributes. TARGET is the inner
8383 declarator to which these attributes apply. ATTRS are the
8384 attributes. */
8385
8386 struct c_declarator *
8387 build_attrs_declarator (tree attrs, struct c_declarator *target)
8388 {
8389 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
8390 ret->kind = cdk_attrs;
8391 ret->declarator = target;
8392 ret->u.attrs = attrs;
8393 return ret;
8394 }
8395
8396 /* Return a declarator for a function with arguments specified by ARGS
8397 and return type specified by TARGET. */
8398
8399 struct c_declarator *
8400 build_function_declarator (struct c_arg_info *args,
8401 struct c_declarator *target)
8402 {
8403 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
8404 ret->kind = cdk_function;
8405 ret->declarator = target;
8406 ret->u.arg_info = args;
8407 return ret;
8408 }
8409
8410 /* Return a declarator for the identifier IDENT (which may be
8411 NULL_TREE for an abstract declarator). */
8412
8413 struct c_declarator *
8414 build_id_declarator (tree ident)
8415 {
8416 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
8417 ret->kind = cdk_id;
8418 ret->declarator = 0;
8419 ret->u.id = ident;
8420 /* Default value - may get reset to a more precise location. */
8421 ret->id_loc = input_location;
8422 return ret;
8423 }
8424
8425 /* Return something to represent absolute declarators containing a *.
8426 TARGET is the absolute declarator that the * contains.
8427 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
8428 to apply to the pointer type. */
8429
8430 struct c_declarator *
8431 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
8432 struct c_declarator *target)
8433 {
8434 tree attrs;
8435 int quals = 0;
8436 struct c_declarator *itarget = target;
8437 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
8438 if (type_quals_attrs)
8439 {
8440 attrs = type_quals_attrs->attrs;
8441 quals = quals_from_declspecs (type_quals_attrs);
8442 if (attrs != NULL_TREE)
8443 itarget = build_attrs_declarator (attrs, target);
8444 }
8445 ret->kind = cdk_pointer;
8446 ret->declarator = itarget;
8447 ret->u.pointer_quals = quals;
8448 return ret;
8449 }
8450
8451 /* Return a pointer to a structure for an empty list of declaration
8452 specifiers. */
8453
8454 struct c_declspecs *
8455 build_null_declspecs (void)
8456 {
8457 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
8458 ret->type = 0;
8459 ret->expr = 0;
8460 ret->decl_attr = 0;
8461 ret->attrs = 0;
8462 ret->typespec_word = cts_none;
8463 ret->storage_class = csc_none;
8464 ret->expr_const_operands = true;
8465 ret->declspecs_seen_p = false;
8466 ret->type_seen_p = false;
8467 ret->non_sc_seen_p = false;
8468 ret->typedef_p = false;
8469 ret->tag_defined_p = false;
8470 ret->explicit_signed_p = false;
8471 ret->deprecated_p = false;
8472 ret->default_int_p = false;
8473 ret->long_p = false;
8474 ret->long_long_p = false;
8475 ret->short_p = false;
8476 ret->signed_p = false;
8477 ret->unsigned_p = false;
8478 ret->complex_p = false;
8479 ret->inline_p = false;
8480 ret->thread_p = false;
8481 ret->const_p = false;
8482 ret->volatile_p = false;
8483 ret->restrict_p = false;
8484 ret->saturating_p = false;
8485 ret->address_space = ADDR_SPACE_GENERIC;
8486 return ret;
8487 }
8488
8489 /* Add the address space ADDRSPACE to the declaration specifiers
8490 SPECS, returning SPECS. */
8491
8492 struct c_declspecs *
8493 declspecs_add_addrspace (struct c_declspecs *specs, addr_space_t as)
8494 {
8495 specs->non_sc_seen_p = true;
8496 specs->declspecs_seen_p = true;
8497
8498 if (!ADDR_SPACE_GENERIC_P (specs->address_space)
8499 && specs->address_space != as)
8500 error ("incompatible address space qualifiers %qs and %qs",
8501 c_addr_space_name (as),
8502 c_addr_space_name (specs->address_space));
8503 else
8504 specs->address_space = as;
8505 return specs;
8506 }
8507
8508 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
8509 returning SPECS. */
8510
8511 struct c_declspecs *
8512 declspecs_add_qual (struct c_declspecs *specs, tree qual)
8513 {
8514 enum rid i;
8515 bool dupe = false;
8516 specs->non_sc_seen_p = true;
8517 specs->declspecs_seen_p = true;
8518 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
8519 && C_IS_RESERVED_WORD (qual));
8520 i = C_RID_CODE (qual);
8521 switch (i)
8522 {
8523 case RID_CONST:
8524 dupe = specs->const_p;
8525 specs->const_p = true;
8526 break;
8527 case RID_VOLATILE:
8528 dupe = specs->volatile_p;
8529 specs->volatile_p = true;
8530 break;
8531 case RID_RESTRICT:
8532 dupe = specs->restrict_p;
8533 specs->restrict_p = true;
8534 break;
8535 default:
8536 gcc_unreachable ();
8537 }
8538 if (dupe && !flag_isoc99)
8539 pedwarn (input_location, OPT_pedantic, "duplicate %qE", qual);
8540 return specs;
8541 }
8542
8543 /* Add the type specifier TYPE to the declaration specifiers SPECS,
8544 returning SPECS. */
8545
8546 struct c_declspecs *
8547 declspecs_add_type (location_t loc, struct c_declspecs *specs,
8548 struct c_typespec spec)
8549 {
8550 tree type = spec.spec;
8551 specs->non_sc_seen_p = true;
8552 specs->declspecs_seen_p = true;
8553 specs->type_seen_p = true;
8554 if (TREE_DEPRECATED (type))
8555 specs->deprecated_p = true;
8556
8557 /* Handle type specifier keywords. */
8558 if (TREE_CODE (type) == IDENTIFIER_NODE
8559 && C_IS_RESERVED_WORD (type)
8560 && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
8561 {
8562 enum rid i = C_RID_CODE (type);
8563 if (specs->type)
8564 {
8565 error_at (loc, "two or more data types in declaration specifiers");
8566 return specs;
8567 }
8568 if ((int) i <= (int) RID_LAST_MODIFIER)
8569 {
8570 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
8571 bool dupe = false;
8572 switch (i)
8573 {
8574 case RID_LONG:
8575 if (specs->long_long_p)
8576 {
8577 error_at (loc, "%<long long long%> is too long for GCC");
8578 break;
8579 }
8580 if (specs->long_p)
8581 {
8582 if (specs->typespec_word == cts_double)
8583 {
8584 error_at (loc,
8585 ("both %<long long%> and %<double%> in "
8586 "declaration specifiers"));
8587 break;
8588 }
8589 pedwarn_c90 (loc, OPT_Wlong_long,
8590 "ISO C90 does not support %<long long%>");
8591 specs->long_long_p = 1;
8592 break;
8593 }
8594 if (specs->short_p)
8595 error_at (loc,
8596 ("both %<long%> and %<short%> in "
8597 "declaration specifiers"));
8598 else if (specs->typespec_word == cts_void)
8599 error_at (loc,
8600 ("both %<long%> and %<void%> in "
8601 "declaration specifiers"));
8602 else if (specs->typespec_word == cts_bool)
8603 error_at (loc,
8604 ("both %<long%> and %<_Bool%> in "
8605 "declaration specifiers"));
8606 else if (specs->typespec_word == cts_char)
8607 error_at (loc,
8608 ("both %<long%> and %<char%> in "
8609 "declaration specifiers"));
8610 else if (specs->typespec_word == cts_float)
8611 error_at (loc,
8612 ("both %<long%> and %<float%> in "
8613 "declaration specifiers"));
8614 else if (specs->typespec_word == cts_dfloat32)
8615 error_at (loc,
8616 ("both %<long%> and %<_Decimal32%> in "
8617 "declaration specifiers"));
8618 else if (specs->typespec_word == cts_dfloat64)
8619 error_at (loc,
8620 ("both %<long%> and %<_Decimal64%> in "
8621 "declaration specifiers"));
8622 else if (specs->typespec_word == cts_dfloat128)
8623 error_at (loc,
8624 ("both %<long%> and %<_Decimal128%> in "
8625 "declaration specifiers"));
8626 else
8627 specs->long_p = true;
8628 break;
8629 case RID_SHORT:
8630 dupe = specs->short_p;
8631 if (specs->long_p)
8632 error_at (loc,
8633 ("both %<long%> and %<short%> in "
8634 "declaration specifiers"));
8635 else if (specs->typespec_word == cts_void)
8636 error_at (loc,
8637 ("both %<short%> and %<void%> in "
8638 "declaration specifiers"));
8639 else if (specs->typespec_word == cts_bool)
8640 error_at (loc,
8641 ("both %<short%> and %<_Bool%> in "
8642 "declaration specifiers"));
8643 else if (specs->typespec_word == cts_char)
8644 error_at (loc,
8645 ("both %<short%> and %<char%> in "
8646 "declaration specifiers"));
8647 else if (specs->typespec_word == cts_float)
8648 error_at (loc,
8649 ("both %<short%> and %<float%> in "
8650 "declaration specifiers"));
8651 else if (specs->typespec_word == cts_double)
8652 error_at (loc,
8653 ("both %<short%> and %<double%> in "
8654 "declaration specifiers"));
8655 else if (specs->typespec_word == cts_dfloat32)
8656 error_at (loc,
8657 ("both %<short%> and %<_Decimal32%> in "
8658 "declaration specifiers"));
8659 else if (specs->typespec_word == cts_dfloat64)
8660 error_at (loc,
8661 ("both %<short%> and %<_Decimal64%> in "
8662 "declaration specifiers"));
8663 else if (specs->typespec_word == cts_dfloat128)
8664 error_at (loc,
8665 ("both %<short%> and %<_Decimal128%> in "
8666 "declaration specifiers"));
8667 else
8668 specs->short_p = true;
8669 break;
8670 case RID_SIGNED:
8671 dupe = specs->signed_p;
8672 if (specs->unsigned_p)
8673 error_at (loc,
8674 ("both %<signed%> and %<unsigned%> in "
8675 "declaration specifiers"));
8676 else if (specs->typespec_word == cts_void)
8677 error_at (loc,
8678 ("both %<signed%> and %<void%> in "
8679 "declaration specifiers"));
8680 else if (specs->typespec_word == cts_bool)
8681 error_at (loc,
8682 ("both %<signed%> and %<_Bool%> in "
8683 "declaration specifiers"));
8684 else if (specs->typespec_word == cts_float)
8685 error_at (loc,
8686 ("both %<signed%> and %<float%> in "
8687 "declaration specifiers"));
8688 else if (specs->typespec_word == cts_double)
8689 error_at (loc,
8690 ("both %<signed%> and %<double%> in "
8691 "declaration specifiers"));
8692 else if (specs->typespec_word == cts_dfloat32)
8693 error_at (loc,
8694 ("both %<signed%> and %<_Decimal32%> in "
8695 "declaration specifiers"));
8696 else if (specs->typespec_word == cts_dfloat64)
8697 error_at (loc,
8698 ("both %<signed%> and %<_Decimal64%> in "
8699 "declaration specifiers"));
8700 else if (specs->typespec_word == cts_dfloat128)
8701 error_at (loc,
8702 ("both %<signed%> and %<_Decimal128%> in "
8703 "declaration specifiers"));
8704 else
8705 specs->signed_p = true;
8706 break;
8707 case RID_UNSIGNED:
8708 dupe = specs->unsigned_p;
8709 if (specs->signed_p)
8710 error_at (loc,
8711 ("both %<signed%> and %<unsigned%> in "
8712 "declaration specifiers"));
8713 else if (specs->typespec_word == cts_void)
8714 error_at (loc,
8715 ("both %<unsigned%> and %<void%> in "
8716 "declaration specifiers"));
8717 else if (specs->typespec_word == cts_bool)
8718 error_at (loc,
8719 ("both %<unsigned%> and %<_Bool%> in "
8720 "declaration specifiers"));
8721 else if (specs->typespec_word == cts_float)
8722 error_at (loc,
8723 ("both %<unsigned%> and %<float%> in "
8724 "declaration specifiers"));
8725 else if (specs->typespec_word == cts_double)
8726 error_at (loc,
8727 ("both %<unsigned%> and %<double%> in "
8728 "declaration specifiers"));
8729 else if (specs->typespec_word == cts_dfloat32)
8730 error_at (loc,
8731 ("both %<unsigned%> and %<_Decimal32%> in "
8732 "declaration specifiers"));
8733 else if (specs->typespec_word == cts_dfloat64)
8734 error_at (loc,
8735 ("both %<unsigned%> and %<_Decimal64%> in "
8736 "declaration specifiers"));
8737 else if (specs->typespec_word == cts_dfloat128)
8738 error_at (loc,
8739 ("both %<unsigned%> and %<_Decimal128%> in "
8740 "declaration specifiers"));
8741 else
8742 specs->unsigned_p = true;
8743 break;
8744 case RID_COMPLEX:
8745 dupe = specs->complex_p;
8746 if (!flag_isoc99 && !in_system_header)
8747 pedwarn (loc, OPT_pedantic,
8748 "ISO C90 does not support complex types");
8749 if (specs->typespec_word == cts_void)
8750 error_at (loc,
8751 ("both %<complex%> and %<void%> in "
8752 "declaration specifiers"));
8753 else if (specs->typespec_word == cts_bool)
8754 error_at (loc,
8755 ("both %<complex%> and %<_Bool%> in "
8756 "declaration specifiers"));
8757 else if (specs->typespec_word == cts_dfloat32)
8758 error_at (loc,
8759 ("both %<complex%> and %<_Decimal32%> in "
8760 "declaration specifiers"));
8761 else if (specs->typespec_word == cts_dfloat64)
8762 error_at (loc,
8763 ("both %<complex%> and %<_Decimal64%> in "
8764 "declaration specifiers"));
8765 else if (specs->typespec_word == cts_dfloat128)
8766 error_at (loc,
8767 ("both %<complex%> and %<_Decimal128%> in "
8768 "declaration specifiers"));
8769 else if (specs->typespec_word == cts_fract)
8770 error_at (loc,
8771 ("both %<complex%> and %<_Fract%> in "
8772 "declaration specifiers"));
8773 else if (specs->typespec_word == cts_accum)
8774 error_at (loc,
8775 ("both %<complex%> and %<_Accum%> in "
8776 "declaration specifiers"));
8777 else if (specs->saturating_p)
8778 error_at (loc,
8779 ("both %<complex%> and %<_Sat%> in "
8780 "declaration specifiers"));
8781 else
8782 specs->complex_p = true;
8783 break;
8784 case RID_SAT:
8785 dupe = specs->saturating_p;
8786 pedwarn (loc, OPT_pedantic,
8787 "ISO C does not support saturating types");
8788 if (specs->typespec_word == cts_void)
8789 error_at (loc,
8790 ("both %<_Sat%> and %<void%> in "
8791 "declaration specifiers"));
8792 else if (specs->typespec_word == cts_bool)
8793 error_at (loc,
8794 ("both %<_Sat%> and %<_Bool%> in "
8795 "declaration specifiers"));
8796 else if (specs->typespec_word == cts_char)
8797 error_at (loc,
8798 ("both %<_Sat%> and %<char%> in "
8799 "declaration specifiers"));
8800 else if (specs->typespec_word == cts_int)
8801 error_at (loc,
8802 ("both %<_Sat%> and %<int%> in "
8803 "declaration specifiers"));
8804 else if (specs->typespec_word == cts_float)
8805 error_at (loc,
8806 ("both %<_Sat%> and %<float%> in "
8807 "declaration specifiers"));
8808 else if (specs->typespec_word == cts_double)
8809 error_at (loc,
8810 ("both %<_Sat%> and %<double%> in "
8811 "declaration specifiers"));
8812 else if (specs->typespec_word == cts_dfloat32)
8813 error_at (loc,
8814 ("both %<_Sat%> and %<_Decimal32%> in "
8815 "declaration specifiers"));
8816 else if (specs->typespec_word == cts_dfloat64)
8817 error_at (loc,
8818 ("both %<_Sat%> and %<_Decimal64%> in "
8819 "declaration specifiers"));
8820 else if (specs->typespec_word == cts_dfloat128)
8821 error_at (loc,
8822 ("both %<_Sat%> and %<_Decimal128%> in "
8823 "declaration specifiers"));
8824 else if (specs->complex_p)
8825 error_at (loc,
8826 ("both %<_Sat%> and %<complex%> in "
8827 "declaration specifiers"));
8828 else
8829 specs->saturating_p = true;
8830 break;
8831 default:
8832 gcc_unreachable ();
8833 }
8834
8835 if (dupe)
8836 error_at (loc, "duplicate %qE", type);
8837
8838 return specs;
8839 }
8840 else
8841 {
8842 /* "void", "_Bool", "char", "int", "float", "double", "_Decimal32",
8843 "_Decimal64", "_Decimal128", "_Fract" or "_Accum". */
8844 if (specs->typespec_word != cts_none)
8845 {
8846 error_at (loc,
8847 "two or more data types in declaration specifiers");
8848 return specs;
8849 }
8850 switch (i)
8851 {
8852 case RID_VOID:
8853 if (specs->long_p)
8854 error_at (loc,
8855 ("both %<long%> and %<void%> in "
8856 "declaration specifiers"));
8857 else if (specs->short_p)
8858 error_at (loc,
8859 ("both %<short%> and %<void%> in "
8860 "declaration specifiers"));
8861 else if (specs->signed_p)
8862 error_at (loc,
8863 ("both %<signed%> and %<void%> in "
8864 "declaration specifiers"));
8865 else if (specs->unsigned_p)
8866 error_at (loc,
8867 ("both %<unsigned%> and %<void%> in "
8868 "declaration specifiers"));
8869 else if (specs->complex_p)
8870 error_at (loc,
8871 ("both %<complex%> and %<void%> in "
8872 "declaration specifiers"));
8873 else if (specs->saturating_p)
8874 error_at (loc,
8875 ("both %<_Sat%> and %<void%> in "
8876 "declaration specifiers"));
8877 else
8878 specs->typespec_word = cts_void;
8879 return specs;
8880 case RID_BOOL:
8881 if (specs->long_p)
8882 error_at (loc,
8883 ("both %<long%> and %<_Bool%> in "
8884 "declaration specifiers"));
8885 else if (specs->short_p)
8886 error_at (loc,
8887 ("both %<short%> and %<_Bool%> in "
8888 "declaration specifiers"));
8889 else if (specs->signed_p)
8890 error_at (loc,
8891 ("both %<signed%> and %<_Bool%> in "
8892 "declaration specifiers"));
8893 else if (specs->unsigned_p)
8894 error_at (loc,
8895 ("both %<unsigned%> and %<_Bool%> in "
8896 "declaration specifiers"));
8897 else if (specs->complex_p)
8898 error_at (loc,
8899 ("both %<complex%> and %<_Bool%> in "
8900 "declaration specifiers"));
8901 else if (specs->saturating_p)
8902 error_at (loc,
8903 ("both %<_Sat%> and %<_Bool%> in "
8904 "declaration specifiers"));
8905 else
8906 specs->typespec_word = cts_bool;
8907 return specs;
8908 case RID_CHAR:
8909 if (specs->long_p)
8910 error_at (loc,
8911 ("both %<long%> and %<char%> in "
8912 "declaration specifiers"));
8913 else if (specs->short_p)
8914 error_at (loc,
8915 ("both %<short%> and %<char%> in "
8916 "declaration specifiers"));
8917 else if (specs->saturating_p)
8918 error_at (loc,
8919 ("both %<_Sat%> and %<char%> in "
8920 "declaration specifiers"));
8921 else
8922 specs->typespec_word = cts_char;
8923 return specs;
8924 case RID_INT:
8925 if (specs->saturating_p)
8926 error_at (loc,
8927 ("both %<_Sat%> and %<int%> in "
8928 "declaration specifiers"));
8929 else
8930 specs->typespec_word = cts_int;
8931 return specs;
8932 case RID_FLOAT:
8933 if (specs->long_p)
8934 error_at (loc,
8935 ("both %<long%> and %<float%> in "
8936 "declaration specifiers"));
8937 else if (specs->short_p)
8938 error_at (loc,
8939 ("both %<short%> and %<float%> in "
8940 "declaration specifiers"));
8941 else if (specs->signed_p)
8942 error_at (loc,
8943 ("both %<signed%> and %<float%> in "
8944 "declaration specifiers"));
8945 else if (specs->unsigned_p)
8946 error_at (loc,
8947 ("both %<unsigned%> and %<float%> in "
8948 "declaration specifiers"));
8949 else if (specs->saturating_p)
8950 error_at (loc,
8951 ("both %<_Sat%> and %<float%> in "
8952 "declaration specifiers"));
8953 else
8954 specs->typespec_word = cts_float;
8955 return specs;
8956 case RID_DOUBLE:
8957 if (specs->long_long_p)
8958 error_at (loc,
8959 ("both %<long long%> and %<double%> in "
8960 "declaration specifiers"));
8961 else if (specs->short_p)
8962 error_at (loc,
8963 ("both %<short%> and %<double%> in "
8964 "declaration specifiers"));
8965 else if (specs->signed_p)
8966 error_at (loc,
8967 ("both %<signed%> and %<double%> in "
8968 "declaration specifiers"));
8969 else if (specs->unsigned_p)
8970 error_at (loc,
8971 ("both %<unsigned%> and %<double%> in "
8972 "declaration specifiers"));
8973 else if (specs->saturating_p)
8974 error_at (loc,
8975 ("both %<_Sat%> and %<double%> in "
8976 "declaration specifiers"));
8977 else
8978 specs->typespec_word = cts_double;
8979 return specs;
8980 case RID_DFLOAT32:
8981 case RID_DFLOAT64:
8982 case RID_DFLOAT128:
8983 {
8984 const char *str;
8985 if (i == RID_DFLOAT32)
8986 str = "_Decimal32";
8987 else if (i == RID_DFLOAT64)
8988 str = "_Decimal64";
8989 else
8990 str = "_Decimal128";
8991 if (specs->long_long_p)
8992 error_at (loc,
8993 ("both %<long long%> and %<%s%> in "
8994 "declaration specifiers"),
8995 str);
8996 if (specs->long_p)
8997 error_at (loc,
8998 ("both %<long%> and %<%s%> in "
8999 "declaration specifiers"),
9000 str);
9001 else if (specs->short_p)
9002 error_at (loc,
9003 ("both %<short%> and %<%s%> in "
9004 "declaration specifiers"),
9005 str);
9006 else if (specs->signed_p)
9007 error_at (loc,
9008 ("both %<signed%> and %<%s%> in "
9009 "declaration specifiers"),
9010 str);
9011 else if (specs->unsigned_p)
9012 error_at (loc,
9013 ("both %<unsigned%> and %<%s%> in "
9014 "declaration specifiers"),
9015 str);
9016 else if (specs->complex_p)
9017 error_at (loc,
9018 ("both %<complex%> and %<%s%> in "
9019 "declaration specifiers"),
9020 str);
9021 else if (specs->saturating_p)
9022 error_at (loc,
9023 ("both %<_Sat%> and %<%s%> in "
9024 "declaration specifiers"),
9025 str);
9026 else if (i == RID_DFLOAT32)
9027 specs->typespec_word = cts_dfloat32;
9028 else if (i == RID_DFLOAT64)
9029 specs->typespec_word = cts_dfloat64;
9030 else
9031 specs->typespec_word = cts_dfloat128;
9032 }
9033 if (!targetm.decimal_float_supported_p ())
9034 error_at (loc,
9035 ("decimal floating point not supported "
9036 "for this target"));
9037 pedwarn (loc, OPT_pedantic,
9038 "ISO C does not support decimal floating point");
9039 return specs;
9040 case RID_FRACT:
9041 case RID_ACCUM:
9042 {
9043 const char *str;
9044 if (i == RID_FRACT)
9045 str = "_Fract";
9046 else
9047 str = "_Accum";
9048 if (specs->complex_p)
9049 error_at (loc,
9050 ("both %<complex%> and %<%s%> in "
9051 "declaration specifiers"),
9052 str);
9053 else if (i == RID_FRACT)
9054 specs->typespec_word = cts_fract;
9055 else
9056 specs->typespec_word = cts_accum;
9057 }
9058 if (!targetm.fixed_point_supported_p ())
9059 error_at (loc,
9060 "fixed-point types not supported for this target");
9061 pedwarn (loc, OPT_pedantic,
9062 "ISO C does not support fixed-point types");
9063 return specs;
9064 default:
9065 /* ObjC reserved word "id", handled below. */
9066 break;
9067 }
9068 }
9069 }
9070
9071 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
9072 form of ObjC type, cases such as "int" and "long" being handled
9073 above), a TYPE (struct, union, enum and typeof specifiers) or an
9074 ERROR_MARK. In none of these cases may there have previously
9075 been any type specifiers. */
9076 if (specs->type || specs->typespec_word != cts_none
9077 || specs->long_p || specs->short_p || specs->signed_p
9078 || specs->unsigned_p || specs->complex_p)
9079 error_at (loc, "two or more data types in declaration specifiers");
9080 else if (TREE_CODE (type) == TYPE_DECL)
9081 {
9082 if (TREE_TYPE (type) == error_mark_node)
9083 ; /* Allow the type to default to int to avoid cascading errors. */
9084 else
9085 {
9086 specs->type = TREE_TYPE (type);
9087 specs->decl_attr = DECL_ATTRIBUTES (type);
9088 specs->typedef_p = true;
9089 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
9090
9091 /* If this typedef name is defined in a struct, then a C++
9092 lookup would return a different value. */
9093 if (warn_cxx_compat
9094 && I_SYMBOL_BINDING (DECL_NAME (type))->in_struct)
9095 warning_at (loc, OPT_Wc___compat,
9096 "C++ lookup of %qD would return a field, not a type",
9097 type);
9098
9099 /* If we are parsing a struct, record that a struct field
9100 used a typedef. */
9101 if (warn_cxx_compat && struct_parse_info != NULL)
9102 VEC_safe_push (tree, heap, struct_parse_info->typedefs_seen, type);
9103 }
9104 }
9105 else if (TREE_CODE (type) == IDENTIFIER_NODE)
9106 {
9107 tree t = lookup_name (type);
9108 if (!t || TREE_CODE (t) != TYPE_DECL)
9109 error_at (loc, "%qE fails to be a typedef or built in type", type);
9110 else if (TREE_TYPE (t) == error_mark_node)
9111 ;
9112 else
9113 specs->type = TREE_TYPE (t);
9114 }
9115 else if (TREE_CODE (type) != ERROR_MARK)
9116 {
9117 if (spec.kind == ctsk_tagdef || spec.kind == ctsk_tagfirstref)
9118 specs->tag_defined_p = true;
9119 if (spec.kind == ctsk_typeof)
9120 {
9121 specs->typedef_p = true;
9122 if (spec.expr)
9123 {
9124 if (specs->expr)
9125 specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
9126 specs->expr, spec.expr);
9127 else
9128 specs->expr = spec.expr;
9129 specs->expr_const_operands &= spec.expr_const_operands;
9130 }
9131 }
9132 specs->type = type;
9133 }
9134
9135 return specs;
9136 }
9137
9138 /* Add the storage class specifier or function specifier SCSPEC to the
9139 declaration specifiers SPECS, returning SPECS. */
9140
9141 struct c_declspecs *
9142 declspecs_add_scspec (struct c_declspecs *specs, tree scspec)
9143 {
9144 enum rid i;
9145 enum c_storage_class n = csc_none;
9146 bool dupe = false;
9147 specs->declspecs_seen_p = true;
9148 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
9149 && C_IS_RESERVED_WORD (scspec));
9150 i = C_RID_CODE (scspec);
9151 if (specs->non_sc_seen_p)
9152 warning (OPT_Wold_style_declaration,
9153 "%qE is not at beginning of declaration", scspec);
9154 switch (i)
9155 {
9156 case RID_INLINE:
9157 /* C99 permits duplicate inline. Although of doubtful utility,
9158 it seems simplest to permit it in gnu89 mode as well, as
9159 there is also little utility in maintaining this as a
9160 difference between gnu89 and C99 inline. */
9161 dupe = false;
9162 specs->inline_p = true;
9163 break;
9164 case RID_THREAD:
9165 dupe = specs->thread_p;
9166 if (specs->storage_class == csc_auto)
9167 error ("%<__thread%> used with %<auto%>");
9168 else if (specs->storage_class == csc_register)
9169 error ("%<__thread%> used with %<register%>");
9170 else if (specs->storage_class == csc_typedef)
9171 error ("%<__thread%> used with %<typedef%>");
9172 else
9173 specs->thread_p = true;
9174 break;
9175 case RID_AUTO:
9176 n = csc_auto;
9177 break;
9178 case RID_EXTERN:
9179 n = csc_extern;
9180 /* Diagnose "__thread extern". */
9181 if (specs->thread_p)
9182 error ("%<__thread%> before %<extern%>");
9183 break;
9184 case RID_REGISTER:
9185 n = csc_register;
9186 break;
9187 case RID_STATIC:
9188 n = csc_static;
9189 /* Diagnose "__thread static". */
9190 if (specs->thread_p)
9191 error ("%<__thread%> before %<static%>");
9192 break;
9193 case RID_TYPEDEF:
9194 n = csc_typedef;
9195 break;
9196 default:
9197 gcc_unreachable ();
9198 }
9199 if (n != csc_none && n == specs->storage_class)
9200 dupe = true;
9201 if (dupe)
9202 error ("duplicate %qE", scspec);
9203 if (n != csc_none)
9204 {
9205 if (specs->storage_class != csc_none && n != specs->storage_class)
9206 {
9207 error ("multiple storage classes in declaration specifiers");
9208 }
9209 else
9210 {
9211 specs->storage_class = n;
9212 if (n != csc_extern && n != csc_static && specs->thread_p)
9213 {
9214 error ("%<__thread%> used with %qE", scspec);
9215 specs->thread_p = false;
9216 }
9217 }
9218 }
9219 return specs;
9220 }
9221
9222 /* Add the attributes ATTRS to the declaration specifiers SPECS,
9223 returning SPECS. */
9224
9225 struct c_declspecs *
9226 declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
9227 {
9228 specs->attrs = chainon (attrs, specs->attrs);
9229 specs->declspecs_seen_p = true;
9230 return specs;
9231 }
9232
9233 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
9234 specifiers with any other type specifier to determine the resulting
9235 type. This is where ISO C checks on complex types are made, since
9236 "_Complex long" is a prefix of the valid ISO C type "_Complex long
9237 double". */
9238
9239 struct c_declspecs *
9240 finish_declspecs (struct c_declspecs *specs)
9241 {
9242 /* If a type was specified as a whole, we have no modifiers and are
9243 done. */
9244 if (specs->type != NULL_TREE)
9245 {
9246 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
9247 && !specs->signed_p && !specs->unsigned_p
9248 && !specs->complex_p);
9249 return specs;
9250 }
9251
9252 /* If none of "void", "_Bool", "char", "int", "float" or "double"
9253 has been specified, treat it as "int" unless "_Complex" is
9254 present and there are no other specifiers. If we just have
9255 "_Complex", it is equivalent to "_Complex double", but e.g.
9256 "_Complex short" is equivalent to "_Complex short int". */
9257 if (specs->typespec_word == cts_none)
9258 {
9259 if (specs->saturating_p)
9260 {
9261 error ("%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
9262 if (!targetm.fixed_point_supported_p ())
9263 error ("fixed-point types not supported for this target");
9264 specs->typespec_word = cts_fract;
9265 }
9266 else if (specs->long_p || specs->short_p
9267 || specs->signed_p || specs->unsigned_p)
9268 {
9269 specs->typespec_word = cts_int;
9270 }
9271 else if (specs->complex_p)
9272 {
9273 specs->typespec_word = cts_double;
9274 pedwarn (input_location, OPT_pedantic,
9275 "ISO C does not support plain %<complex%> meaning "
9276 "%<double complex%>");
9277 }
9278 else
9279 {
9280 specs->typespec_word = cts_int;
9281 specs->default_int_p = true;
9282 /* We don't diagnose this here because grokdeclarator will
9283 give more specific diagnostics according to whether it is
9284 a function definition. */
9285 }
9286 }
9287
9288 /* If "signed" was specified, record this to distinguish "int" and
9289 "signed int" in the case of a bit-field with
9290 -funsigned-bitfields. */
9291 specs->explicit_signed_p = specs->signed_p;
9292
9293 /* Now compute the actual type. */
9294 switch (specs->typespec_word)
9295 {
9296 case cts_void:
9297 gcc_assert (!specs->long_p && !specs->short_p
9298 && !specs->signed_p && !specs->unsigned_p
9299 && !specs->complex_p);
9300 specs->type = void_type_node;
9301 break;
9302 case cts_bool:
9303 gcc_assert (!specs->long_p && !specs->short_p
9304 && !specs->signed_p && !specs->unsigned_p
9305 && !specs->complex_p);
9306 specs->type = boolean_type_node;
9307 break;
9308 case cts_char:
9309 gcc_assert (!specs->long_p && !specs->short_p);
9310 gcc_assert (!(specs->signed_p && specs->unsigned_p));
9311 if (specs->signed_p)
9312 specs->type = signed_char_type_node;
9313 else if (specs->unsigned_p)
9314 specs->type = unsigned_char_type_node;
9315 else
9316 specs->type = char_type_node;
9317 if (specs->complex_p)
9318 {
9319 pedwarn (input_location, OPT_pedantic,
9320 "ISO C does not support complex integer types");
9321 specs->type = build_complex_type (specs->type);
9322 }
9323 break;
9324 case cts_int:
9325 gcc_assert (!(specs->long_p && specs->short_p));
9326 gcc_assert (!(specs->signed_p && specs->unsigned_p));
9327 if (specs->long_long_p)
9328 specs->type = (specs->unsigned_p
9329 ? long_long_unsigned_type_node
9330 : long_long_integer_type_node);
9331 else if (specs->long_p)
9332 specs->type = (specs->unsigned_p
9333 ? long_unsigned_type_node
9334 : long_integer_type_node);
9335 else if (specs->short_p)
9336 specs->type = (specs->unsigned_p
9337 ? short_unsigned_type_node
9338 : short_integer_type_node);
9339 else
9340 specs->type = (specs->unsigned_p
9341 ? unsigned_type_node
9342 : integer_type_node);
9343 if (specs->complex_p)
9344 {
9345 pedwarn (input_location, OPT_pedantic,
9346 "ISO C does not support complex integer types");
9347 specs->type = build_complex_type (specs->type);
9348 }
9349 break;
9350 case cts_float:
9351 gcc_assert (!specs->long_p && !specs->short_p
9352 && !specs->signed_p && !specs->unsigned_p);
9353 specs->type = (specs->complex_p
9354 ? complex_float_type_node
9355 : float_type_node);
9356 break;
9357 case cts_double:
9358 gcc_assert (!specs->long_long_p && !specs->short_p
9359 && !specs->signed_p && !specs->unsigned_p);
9360 if (specs->long_p)
9361 {
9362 specs->type = (specs->complex_p
9363 ? complex_long_double_type_node
9364 : long_double_type_node);
9365 }
9366 else
9367 {
9368 specs->type = (specs->complex_p
9369 ? complex_double_type_node
9370 : double_type_node);
9371 }
9372 break;
9373 case cts_dfloat32:
9374 case cts_dfloat64:
9375 case cts_dfloat128:
9376 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
9377 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
9378 if (specs->typespec_word == cts_dfloat32)
9379 specs->type = dfloat32_type_node;
9380 else if (specs->typespec_word == cts_dfloat64)
9381 specs->type = dfloat64_type_node;
9382 else
9383 specs->type = dfloat128_type_node;
9384 break;
9385 case cts_fract:
9386 gcc_assert (!specs->complex_p);
9387 if (!targetm.fixed_point_supported_p ())
9388 specs->type = integer_type_node;
9389 else if (specs->saturating_p)
9390 {
9391 if (specs->long_long_p)
9392 specs->type = specs->unsigned_p
9393 ? sat_unsigned_long_long_fract_type_node
9394 : sat_long_long_fract_type_node;
9395 else if (specs->long_p)
9396 specs->type = specs->unsigned_p
9397 ? sat_unsigned_long_fract_type_node
9398 : sat_long_fract_type_node;
9399 else if (specs->short_p)
9400 specs->type = specs->unsigned_p
9401 ? sat_unsigned_short_fract_type_node
9402 : sat_short_fract_type_node;
9403 else
9404 specs->type = specs->unsigned_p
9405 ? sat_unsigned_fract_type_node
9406 : sat_fract_type_node;
9407 }
9408 else
9409 {
9410 if (specs->long_long_p)
9411 specs->type = specs->unsigned_p
9412 ? unsigned_long_long_fract_type_node
9413 : long_long_fract_type_node;
9414 else if (specs->long_p)
9415 specs->type = specs->unsigned_p
9416 ? unsigned_long_fract_type_node
9417 : long_fract_type_node;
9418 else if (specs->short_p)
9419 specs->type = specs->unsigned_p
9420 ? unsigned_short_fract_type_node
9421 : short_fract_type_node;
9422 else
9423 specs->type = specs->unsigned_p
9424 ? unsigned_fract_type_node
9425 : fract_type_node;
9426 }
9427 break;
9428 case cts_accum:
9429 gcc_assert (!specs->complex_p);
9430 if (!targetm.fixed_point_supported_p ())
9431 specs->type = integer_type_node;
9432 else if (specs->saturating_p)
9433 {
9434 if (specs->long_long_p)
9435 specs->type = specs->unsigned_p
9436 ? sat_unsigned_long_long_accum_type_node
9437 : sat_long_long_accum_type_node;
9438 else if (specs->long_p)
9439 specs->type = specs->unsigned_p
9440 ? sat_unsigned_long_accum_type_node
9441 : sat_long_accum_type_node;
9442 else if (specs->short_p)
9443 specs->type = specs->unsigned_p
9444 ? sat_unsigned_short_accum_type_node
9445 : sat_short_accum_type_node;
9446 else
9447 specs->type = specs->unsigned_p
9448 ? sat_unsigned_accum_type_node
9449 : sat_accum_type_node;
9450 }
9451 else
9452 {
9453 if (specs->long_long_p)
9454 specs->type = specs->unsigned_p
9455 ? unsigned_long_long_accum_type_node
9456 : long_long_accum_type_node;
9457 else if (specs->long_p)
9458 specs->type = specs->unsigned_p
9459 ? unsigned_long_accum_type_node
9460 : long_accum_type_node;
9461 else if (specs->short_p)
9462 specs->type = specs->unsigned_p
9463 ? unsigned_short_accum_type_node
9464 : short_accum_type_node;
9465 else
9466 specs->type = specs->unsigned_p
9467 ? unsigned_accum_type_node
9468 : accum_type_node;
9469 }
9470 break;
9471 default:
9472 gcc_unreachable ();
9473 }
9474
9475 return specs;
9476 }
9477
9478 /* A subroutine of c_write_global_declarations. Perform final processing
9479 on one file scope's declarations (or the external scope's declarations),
9480 GLOBALS. */
9481
9482 static void
9483 c_write_global_declarations_1 (tree globals)
9484 {
9485 tree decl;
9486 bool reconsider;
9487
9488 /* Process the decls in the order they were written. */
9489 for (decl = globals; decl; decl = TREE_CHAIN (decl))
9490 {
9491 /* Check for used but undefined static functions using the C
9492 standard's definition of "used", and set TREE_NO_WARNING so
9493 that check_global_declarations doesn't repeat the check. */
9494 if (TREE_CODE (decl) == FUNCTION_DECL
9495 && DECL_INITIAL (decl) == 0
9496 && DECL_EXTERNAL (decl)
9497 && !TREE_PUBLIC (decl)
9498 && C_DECL_USED (decl))
9499 {
9500 pedwarn (input_location, 0, "%q+F used but never defined", decl);
9501 TREE_NO_WARNING (decl) = 1;
9502 }
9503
9504 wrapup_global_declaration_1 (decl);
9505 }
9506
9507 do
9508 {
9509 reconsider = false;
9510 for (decl = globals; decl; decl = TREE_CHAIN (decl))
9511 reconsider |= wrapup_global_declaration_2 (decl);
9512 }
9513 while (reconsider);
9514
9515 for (decl = globals; decl; decl = TREE_CHAIN (decl))
9516 check_global_declaration_1 (decl);
9517 }
9518
9519 /* A subroutine of c_write_global_declarations Emit debug information for each
9520 of the declarations in GLOBALS. */
9521
9522 static void
9523 c_write_global_declarations_2 (tree globals)
9524 {
9525 tree decl;
9526
9527 for (decl = globals; decl ; decl = TREE_CHAIN (decl))
9528 debug_hooks->global_decl (decl);
9529 }
9530
9531 /* Preserve the external declarations scope across a garbage collect. */
9532 static GTY(()) tree ext_block;
9533
9534 void
9535 c_write_global_declarations (void)
9536 {
9537 tree t;
9538
9539 /* We don't want to do this if generating a PCH. */
9540 if (pch_file)
9541 return;
9542
9543 /* Don't waste time on further processing if -fsyntax-only.
9544 Continue for warning and errors issued during lowering though. */
9545 if (flag_syntax_only)
9546 return;
9547
9548 /* Close the external scope. */
9549 ext_block = pop_scope ();
9550 external_scope = 0;
9551 gcc_assert (!current_scope);
9552
9553 if (ext_block)
9554 {
9555 tree tmp = BLOCK_VARS (ext_block);
9556 int flags;
9557 FILE * stream = dump_begin (TDI_tu, &flags);
9558 if (stream && tmp)
9559 {
9560 dump_node (tmp, flags & ~TDF_SLIM, stream);
9561 dump_end (TDI_tu, stream);
9562 }
9563 }
9564
9565 /* Process all file scopes in this compilation, and the external_scope,
9566 through wrapup_global_declarations and check_global_declarations. */
9567 for (t = all_translation_units; t; t = TREE_CHAIN (t))
9568 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
9569 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
9570
9571 /* We're done parsing; proceed to optimize and emit assembly.
9572 FIXME: shouldn't be the front end's responsibility to call this. */
9573 cgraph_finalize_compilation_unit ();
9574
9575 /* After cgraph has had a chance to emit everything that's going to
9576 be emitted, output debug information for globals. */
9577 if (errorcount == 0 && sorrycount == 0)
9578 {
9579 timevar_push (TV_SYMOUT);
9580 for (t = all_translation_units; t; t = TREE_CHAIN (t))
9581 c_write_global_declarations_2 (BLOCK_VARS (DECL_INITIAL (t)));
9582 c_write_global_declarations_2 (BLOCK_VARS (ext_block));
9583 timevar_pop (TV_SYMOUT);
9584 }
9585
9586 ext_block = NULL;
9587 }
9588
9589 #include "gt-c-decl.h"