comparison gcc/c-decl.c.orig @ 57:326d9e06c2e3

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