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