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