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