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