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