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