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