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