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