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