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