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