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