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