]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c-decl.c
backport: ChangeLog.tuples: ChangeLog from gimple-tuples-branch.
[thirdparty/gcc.git] / gcc / c-decl.c
CommitLineData
51e29401 1/* Process declarations and variables for C compiler.
21c7361e 2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
1ea193c2
ILT
3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 Free Software Foundation, Inc.
51e29401 5
1322177d 6This file is part of GCC.
51e29401 7
1322177d
LB
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
9dcd6f09 10Software Foundation; either version 3, or (at your option) any later
1322177d 11version.
51e29401 12
1322177d
LB
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.
51e29401
RS
17
18You should have received a copy of the GNU General Public License
9dcd6f09
NC
19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
51e29401 21
51e29401
RS
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"
670ee920 30#include "system.h"
4977bab6 31#include "coretypes.h"
43e05e45 32#include "input.h"
4977bab6 33#include "tm.h"
993c790e 34#include "intl.h"
51e29401 35#include "tree.h"
4838c5ee 36#include "tree-inline.h"
051c57da 37#include "rtl.h"
51e29401 38#include "flags.h"
49ad7cfa 39#include "function.h"
e14417fa 40#include "output.h"
051c57da 41#include "expr.h"
51e29401 42#include "c-tree.h"
5f6da302 43#include "toplev.h"
1526a060 44#include "ggc.h"
809d4ef1 45#include "tm_p.h"
a0d85b75 46#include "cpplib.h"
672a6f42 47#include "target.h"
e1772ac0 48#include "debug.h"
40e941af 49#include "opts.h"
b932f770 50#include "timevar.h"
26f943fd 51#include "c-common.h"
ecb0eece 52#include "c-pragma.h"
6de9cd9a
DN
53#include "langhooks.h"
54#include "tree-mudflap.h"
726a989a
RB
55#include "gimple.h"
56#include "tree-iterator.h"
6de9cd9a
DN
57#include "diagnostic.h"
58#include "tree-dump.h"
1c4a429a 59#include "cgraph.h"
66ea6f4c 60#include "hashtab.h"
0bfa5f65
RH
61#include "libfuncs.h"
62#include "except.h"
48873ed2 63#include "langhooks-def.h"
820cc88f 64#include "pointer-set.h"
726a989a 65#include "gimple.h"
a0d85b75 66
51e29401
RS
67/* In grokdeclarator, distinguish syntactic contexts of declarators. */
68enum decl_context
69{ NORMAL, /* Ordinary declaration */
70 FUNCDEF, /* Function definition */
71 PARM, /* Declaration of parm before function body */
72 FIELD, /* Declaration inside struct or union */
51e29401
RS
73 TYPENAME}; /* Typename (inside cast or sizeof) */
74
0da300cd
TT
75/* States indicating how grokdeclarator() should handle declspecs marked
76 with __attribute__((deprecated)). An object declared as
77 __attribute__((deprecated)) suppresses warnings of uses of other
78 deprecated items. */
79
80enum deprecated_states {
81 DEPRECATED_NORMAL,
82 DEPRECATED_SUPPRESS
83};
84
51e29401 85\f
51e29401
RS
86/* Nonzero if we have seen an invalid cross reference
87 to a struct, union, or enum, but not yet printed the message. */
51e29401 88tree pending_invalid_xref;
093c7153 89
51e29401 90/* File and line to appear in the eventual error message. */
070588f0 91location_t pending_invalid_xref_location;
51e29401 92
093c7153
RH
93/* True means we've initialized exception handling. */
94bool c_eh_initialized_p;
95
77dbdb57
ZW
96/* The file and line that the prototype came from if this is an
97 old-style definition; used for diagnostics in
98 store_parm_decls_oldstyle. */
eb1dfbb2 99
95035b6f 100static location_t current_function_prototype_locus;
50a9145c 101
6fb58bba
JM
102/* Whether this prototype was built-in. */
103
104static bool current_function_prototype_built_in;
105
106/* The argument type information of this prototype. */
107
108static tree current_function_prototype_arg_types;
109
7b112487
JM
110/* The argument information structure for the function currently being
111 defined. */
112
f8893e47 113static struct c_arg_info *current_function_arg_info;
7b112487 114
bc4b653b
JM
115/* The obstack on which parser and related data structures, which are
116 not live beyond their top-level declaration or definition, are
117 allocated. */
118struct obstack parser_obstack;
119
8f17b5c5
MM
120/* The current statement tree. */
121
e2500fed 122static GTY(()) struct stmt_tree_s c_stmt_tree;
8f17b5c5 123
6614fd40 124/* State saving variables. */
506e2710
RH
125tree c_break_label;
126tree c_cont_label;
e13e48e7 127
eecec698
ZW
128/* Linked list of TRANSLATION_UNIT_DECLS for the translation units
129 included in this invocation. Note that the current translation
130 unit is not included in this list. */
d1bd0ded 131
eecec698 132static GTY(()) tree all_translation_units;
d1bd0ded 133
f75fbaf7
ZW
134/* A list of decls to be made automatically visible in each file scope. */
135static GTY(()) tree visible_builtins;
136
51e29401
RS
137/* Set to 0 at beginning of a function definition, set to 1 if
138 a return statement that specifies a return value is seen. */
139
140int current_function_returns_value;
141
142/* Set to 0 at beginning of a function definition, set to 1 if
143 a return statement with no argument is seen. */
144
145int current_function_returns_null;
146
5ce89b2e
JM
147/* Set to 0 at beginning of a function definition, set to 1 if
148 a call to a noreturn function is seen. */
149
150int current_function_returns_abnormally;
151
51e29401
RS
152/* Set to nonzero by `grokdeclarator' for a function
153 whose return type is defaulted, if warnings for this are desired. */
154
155static int warn_about_return_type;
156
73aea290
JM
157/* Nonzero when the current toplevel function contains a declaration
158 of a nested function which is never defined. */
159
160static bool undef_nested_function;
161
f75fbaf7
ZW
162/* True means global_bindings_p should return false even if the scope stack
163 says we are in file scope. */
164bool c_override_global_bindings_to_false;
165
51e29401 166\f
f75fbaf7
ZW
167/* Each c_binding structure describes one binding of an identifier to
168 a decl. All the decls in a scope - irrespective of namespace - are
169 chained together by the ->prev field, which (as the name implies)
170 runs in reverse order. All the decls in a given namespace bound to
171 a given identifier are chained by the ->shadowed field, which runs
9aaabf8a 172 from inner to outer scopes.
f75fbaf7
ZW
173
174 The ->decl field usually points to a DECL node, but there are two
175 exceptions. In the namespace of type tags, the bound entity is a
176 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
177 identifier is encountered, it is bound to error_mark_node to
178 suppress further errors about that identifier in the current
9aaabf8a
ZW
179 function.
180
0b410f0b
JM
181 The ->type field stores the type of the declaration in this scope;
182 if NULL, the type is the type of the ->decl field. This is only of
183 relevance for objects with external or internal linkage which may
184 be redeclared in inner scopes, forming composite types that only
185 persist for the duration of those scopes. In the external scope,
186 this stores the composite of all the types declared for this
187 object, visible or not. The ->inner_comp field (used only at file
188 scope) stores whether an incomplete array type at file scope was
189 completed at an inner scope to an array size other than 1.
190
9aaabf8a
ZW
191 The depth field is copied from the scope structure that holds this
192 decl. It is used to preserve the proper ordering of the ->shadowed
193 field (see bind()) and also for a handful of special-case checks.
194 Finally, the invisible bit is true for a decl which should be
195 ignored for purposes of normal name lookup, and the nested bit is
196 true for a decl that's been bound a second time in an inner scope;
197 in all such cases, the binding in the outer scope will have its
198 invisible bit true. */
f75fbaf7 199
18d5f982 200struct c_binding GTY((chain_next ("%h.prev")))
f75fbaf7
ZW
201{
202 tree decl; /* the decl bound */
0b410f0b 203 tree type; /* the type in this scope */
f75fbaf7
ZW
204 tree id; /* the identifier it's bound to */
205 struct c_binding *prev; /* the previous decl in this scope */
206 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
9aaabf8a
ZW
207 unsigned int depth : 28; /* depth of this scope */
208 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
209 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
0b410f0b
JM
210 BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
211 /* one free bit */
f75fbaf7 212};
9aaabf8a
ZW
213#define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
214#define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
215#define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
216#define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
f75fbaf7
ZW
217
218#define I_SYMBOL_BINDING(node) \
3f75a254 219 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->symbol_binding)
f75fbaf7
ZW
220#define I_SYMBOL_DECL(node) \
221 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
222
223#define I_TAG_BINDING(node) \
3f75a254 224 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->tag_binding)
f75fbaf7
ZW
225#define I_TAG_DECL(node) \
226 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
227
228#define I_LABEL_BINDING(node) \
3f75a254 229 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->label_binding)
f75fbaf7
ZW
230#define I_LABEL_DECL(node) \
231 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
232
18d5f982
ZW
233/* Each C symbol points to three linked lists of c_binding structures.
234 These describe the values of the identifier in the three different
235 namespaces defined by the language. */
236
237struct lang_identifier GTY(())
238{
239 struct c_common_identifier common_id;
240 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
241 struct c_binding *tag_binding; /* struct/union/enum tags */
242 struct c_binding *label_binding; /* labels */
243};
244
245/* Validate c-lang.c's assumptions. */
246extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
247[(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
248
249/* The resulting tree type. */
250
251union lang_tree_node
252 GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
726a989a 253 chain_next ("TREE_CODE (&%h.generic) == INTEGER_TYPE ? (union lang_tree_node *) TYPE_NEXT_VARIANT (&%h.generic) : ((union lang_tree_node *) TREE_CHAIN (&%h.generic))")))
18d5f982
ZW
254{
255 union tree_node GTY ((tag ("0"),
256 desc ("tree_node_structure (&%h)")))
257 generic;
258 struct lang_identifier GTY ((tag ("1"))) identifier;
259};
260
f75fbaf7
ZW
261/* Each c_scope structure describes the complete contents of one
262 scope. Four scopes are distinguished specially: the innermost or
263 current scope, the innermost function scope, the file scope (always
264 the second to outermost) and the outermost or external scope.
f91f41b2
ZW
265
266 Most declarations are recorded in the current scope.
267
268 All normal label declarations are recorded in the innermost
269 function scope, as are bindings of undeclared identifiers to
270 error_mark_node. (GCC permits nested functions as an extension,
271 hence the 'innermost' qualifier.) Explicitly declared labels
272 (using the __label__ extension) appear in the current scope.
273
f75fbaf7 274 Being in the file scope (current_scope == file_scope) causes
f91f41b2
ZW
275 special behavior in several places below. Also, under some
276 conditions the Objective-C front end records declarations in the
f75fbaf7
ZW
277 file scope even though that isn't the current scope.
278
279 All declarations with external linkage are recorded in the external
280 scope, even if they aren't visible there; this models the fact that
281 such declarations are visible to the entire program, and (with a
282 bit of cleverness, see pushdecl) allows diagnosis of some violations
283 of C99 6.2.2p7 and 6.2.7p2:
284
285 If, within the same translation unit, the same identifier appears
286 with both internal and external linkage, the behavior is
287 undefined.
f91f41b2 288
f75fbaf7
ZW
289 All declarations that refer to the same object or function shall
290 have compatible type; otherwise, the behavior is undefined.
291
292 Initially only the built-in declarations, which describe compiler
293 intrinsic functions plus a subset of the standard library, are in
294 this scope.
295
296 The order of the blocks list matters, and it is frequently appended
297 to. To avoid having to walk all the way to the end of the list on
298 each insertion, or reverse the list later, we maintain a pointer to
299 the last list entry. (FIXME: It should be feasible to use a reversed
300 list here.)
301
302 The bindings list is strictly in reverse order of declarations;
303 pop_scope relies on this. */
f91f41b2 304
51e29401 305
18d5f982 306struct c_scope GTY((chain_next ("%h.outer")))
14077d68
ZW
307{
308 /* The scope containing this one. */
309 struct c_scope *outer;
310
311 /* The next outermost function scope. */
312 struct c_scope *outer_function;
313
f75fbaf7
ZW
314 /* All bindings in this scope. */
315 struct c_binding *bindings;
14077d68
ZW
316
317 /* For each scope (except the global one), a chain of BLOCK nodes
318 for all the scopes that were entered and exited one level down. */
319 tree blocks;
f91f41b2 320 tree blocks_last;
14077d68 321
f75fbaf7
ZW
322 /* The depth of this scope. Used to keep the ->shadowed chain of
323 bindings sorted innermost to outermost. */
324 unsigned int depth : 28;
325
14077d68
ZW
326 /* True if we are currently filling this scope with parameter
327 declarations. */
1c62e7b2 328 BOOL_BITFIELD parm_flag : 1;
14077d68 329
52ffd86e
MS
330 /* True if we saw [*] in this scope. Used to give an error messages
331 if these appears in a function definition. */
332 BOOL_BITFIELD had_vla_unspec : 1;
333
55d54003
ZW
334 /* True if we already complained about forward parameter decls
335 in this scope. This prevents double warnings on
336 foo (int a; int b; ...) */
1c62e7b2 337 BOOL_BITFIELD warned_forward_parm_decls : 1;
55d54003 338
14077d68
ZW
339 /* True if this is the outermost block scope of a function body.
340 This scope contains the parameters, the local variables declared
341 in the outermost block, and all the labels (except those in
342 nested functions, or declared at block scope with __label__). */
1c62e7b2 343 BOOL_BITFIELD function_body : 1;
14077d68
ZW
344
345 /* True means make a BLOCK for this scope no matter what. */
1c62e7b2 346 BOOL_BITFIELD keep : 1;
14077d68 347};
51e29401 348
f8521984 349/* The scope currently in effect. */
6645c3fa 350
f8521984 351static GTY(()) struct c_scope *current_scope;
51e29401 352
14e33ee8
ZW
353/* The innermost function scope. Ordinary (not explicitly declared)
354 labels, bindings to error_mark_node, and the lazily-created
355 bindings of __func__ and its friends get this scope. */
14077d68 356
f8521984 357static GTY(()) struct c_scope *current_function_scope;
14e33ee8 358
f75fbaf7
ZW
359/* The C file scope. This is reset for each input translation unit. */
360
361static GTY(()) struct c_scope *file_scope;
362
363/* The outermost scope. This is used for all declarations with
364 external linkage, and only these, hence the name. */
365
366static GTY(()) struct c_scope *external_scope;
367
368/* A chain of c_scope structures awaiting reuse. */
369
1431042e 370static GTY((deletable)) struct c_scope *scope_freelist;
f75fbaf7
ZW
371
372/* A chain of c_binding structures awaiting reuse. */
51e29401 373
1431042e 374static GTY((deletable)) struct c_binding *binding_freelist;
51e29401 375
3d93cdfa 376/* Append VAR to LIST in scope SCOPE. */
f91f41b2
ZW
377#define SCOPE_LIST_APPEND(scope, list, decl) do { \
378 struct c_scope *s_ = (scope); \
379 tree d_ = (decl); \
380 if (s_->list##_last) \
87caf699 381 BLOCK_CHAIN (s_->list##_last) = d_; \
f91f41b2
ZW
382 else \
383 s_->list = d_; \
384 s_->list##_last = d_; \
385} while (0)
386
387/* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
388#define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
389 struct c_scope *t_ = (tscope); \
390 struct c_scope *f_ = (fscope); \
391 if (t_->to##_last) \
87caf699 392 BLOCK_CHAIN (t_->to##_last) = f_->from; \
f91f41b2
ZW
393 else \
394 t_->to = f_->from; \
395 t_->to##_last = f_->from##_last; \
396} while (0)
397
f8521984 398/* True means unconditionally make a BLOCK for the next scope pushed. */
51e29401 399
14e33ee8 400static bool keep_next_level_flag;
51e29401 401
f75fbaf7 402/* True means the next call to push_scope will be the outermost scope
a8ccdffe
ZW
403 of a function body, so do not push a new scope, merely cease
404 expecting parameter decls. */
6645c3fa 405
a8ccdffe 406static bool next_is_function_body;
51e29401
RS
407
408/* Forward declarations. */
f75fbaf7
ZW
409static tree lookup_name_in_scope (tree, struct c_scope *);
410static tree c_make_fname_decl (tree, int);
deb176fa
JM
411static tree grokdeclarator (const struct c_declarator *,
412 struct c_declspecs *,
b9baeecd 413 enum decl_context, bool, tree *, tree *,
0da300cd 414 enum deprecated_states);
f8893e47 415static tree grokparms (struct c_arg_info *, bool);
35b1a6fa 416static void layout_array_type (tree);
51e29401 417\f
ed3d0b14
ILT
418/* T is a statement. Add it to the statement-tree. This is the
419 C/ObjC version--C++ has a slightly different version of this
420 function. */
421
422tree
423add_stmt (tree t)
424{
425 enum tree_code code = TREE_CODE (t);
426
07beea0d 427 if (CAN_HAVE_LOCATION_P (t) && code != LABEL_EXPR)
ed3d0b14
ILT
428 {
429 if (!EXPR_HAS_LOCATION (t))
430 SET_EXPR_LOCATION (t, input_location);
431 }
432
433 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
434 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
435
436 /* Add T to the statement-tree. Non-side-effect statements need to be
437 recorded during statement expressions. */
438 append_to_statement_list_force (t, &cur_stmt_list);
439
440 return t;
441}
442\f
e23bd218 443
51e29401 444void
35b1a6fa 445c_print_identifier (FILE *file, tree node, int indent)
51e29401 446{
f75fbaf7
ZW
447 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
448 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
449 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
eea1139b 450 if (C_IS_RESERVED_WORD (node) && C_RID_CODE (node) != RID_CXX_COMPAT_WARN)
0e5921e8
ZW
451 {
452 tree rid = ridpointers[C_RID_CODE (node)];
453 indent_to (file, indent + 4);
06145cb9 454 fprintf (file, "rid %p \"%s\"",
75b6f3fd 455 (void *) rid, IDENTIFIER_POINTER (rid));
0e5921e8 456 }
51e29401 457}
f75fbaf7
ZW
458
459/* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
460 which may be any of several kinds of DECL or TYPE or error_mark_node,
461 in the scope SCOPE. */
462static void
9aaabf8a 463bind (tree name, tree decl, struct c_scope *scope, bool invisible, bool nested)
f75fbaf7
ZW
464{
465 struct c_binding *b, **here;
466
467 if (binding_freelist)
468 {
469 b = binding_freelist;
470 binding_freelist = b->prev;
471 }
472 else
5d038c4c 473 b = GGC_NEW (struct c_binding);
f75fbaf7
ZW
474
475 b->shadowed = 0;
476 b->decl = decl;
477 b->id = name;
9aaabf8a
ZW
478 b->depth = scope->depth;
479 b->invisible = invisible;
480 b->nested = nested;
0b410f0b
JM
481 b->inner_comp = 0;
482
483 b->type = 0;
f75fbaf7
ZW
484
485 b->prev = scope->bindings;
486 scope->bindings = b;
487
488 if (!name)
489 return;
490
491 switch (TREE_CODE (decl))
492 {
493 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
494 case ENUMERAL_TYPE:
495 case UNION_TYPE:
496 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
497 case VAR_DECL:
498 case FUNCTION_DECL:
499 case TYPE_DECL:
500 case CONST_DECL:
501 case PARM_DECL:
502 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
503
504 default:
366de0ce 505 gcc_unreachable ();
f75fbaf7
ZW
506 }
507
508 /* Locate the appropriate place in the chain of shadowed decls
509 to insert this binding. Normally, scope == current_scope and
510 this does nothing. */
9aaabf8a 511 while (*here && (*here)->depth > scope->depth)
f75fbaf7
ZW
512 here = &(*here)->shadowed;
513
514 b->shadowed = *here;
515 *here = b;
516}
517
518/* Clear the binding structure B, stick it on the binding_freelist,
519 and return the former value of b->prev. This is used by pop_scope
520 and get_parm_info to iterate destructively over all the bindings
521 from a given scope. */
522static struct c_binding *
523free_binding_and_advance (struct c_binding *b)
524{
525 struct c_binding *prev = b->prev;
526
9aaabf8a 527 memset (b, 0, sizeof (struct c_binding));
f75fbaf7
ZW
528 b->prev = binding_freelist;
529 binding_freelist = b;
530
531 return prev;
532}
533
51e29401 534\f
b4892310 535/* Hook called at end of compilation to assume 1 elt
f8521984 536 for a file-scope tentative array defn that wasn't complete before. */
6645c3fa 537
b4892310 538void
35b1a6fa 539c_finish_incomplete_decl (tree decl)
b4892310 540{
5cfac96e 541 if (TREE_CODE (decl) == VAR_DECL)
b4892310
RS
542 {
543 tree type = TREE_TYPE (decl);
5cfac96e
RK
544 if (type != error_mark_node
545 && TREE_CODE (type) == ARRAY_TYPE
3f75a254 546 && !DECL_EXTERNAL (decl)
5cfac96e 547 && TYPE_DOMAIN (type) == 0)
b4892310 548 {
dee15844 549 warning (0, "array %q+D assumed to have one element", decl);
5cfac96e 550
aab038d5 551 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
b4892310
RS
552
553 layout_decl (decl, 0);
554 }
555 }
556}
557\f
264fa2db
ZL
558/* The Objective-C front-end often needs to determine the current scope. */
559
560void *
bc095846 561objc_get_current_scope (void)
264fa2db
ZL
562{
563 return current_scope;
564}
565
566/* The following function is used only by Objective-C. It needs to live here
567 because it accesses the innards of c_scope. */
568
569void
570objc_mark_locals_volatile (void *enclosing_blk)
571{
572 struct c_scope *scope;
f75fbaf7 573 struct c_binding *b;
e13e48e7
EC
574
575 for (scope = current_scope;
264fa2db
ZL
576 scope && scope != enclosing_blk;
577 scope = scope->outer)
578 {
f75fbaf7 579 for (b = scope->bindings; b; b = b->prev)
58393038 580 objc_volatilize_decl (b->decl);
f75fbaf7 581
264fa2db
ZL
582 /* Do not climb up past the current function. */
583 if (scope->function_body)
584 break;
e13e48e7
EC
585 }
586}
587
f75fbaf7 588/* Nonzero if we are currently in file scope. */
51e29401
RS
589
590int
35b1a6fa 591global_bindings_p (void)
51e29401 592{
f75fbaf7 593 return current_scope == file_scope && !c_override_global_bindings_to_false;
51e29401
RS
594}
595
596void
35b1a6fa 597keep_next_level (void)
51e29401 598{
14e33ee8 599 keep_next_level_flag = true;
51e29401
RS
600}
601
f8521984 602/* Identify this scope as currently being filled with parameters. */
51e29401
RS
603
604void
eb1dfbb2 605declare_parm_level (void)
51e29401 606{
f8521984 607 current_scope->parm_flag = true;
51e29401
RS
608}
609
51e29401 610void
f75fbaf7 611push_scope (void)
51e29401 612{
a8ccdffe 613 if (next_is_function_body)
51e29401 614 {
339a28b9
ZW
615 /* This is the transition from the parameters to the top level
616 of the function body. These are the same scope
f8521984 617 (C99 6.2.1p4,6) so we do not push another scope structure.
a8ccdffe
ZW
618 next_is_function_body is set only by store_parm_decls, which
619 in turn is called when and only when we are about to
620 encounter the opening curly brace for the function body.
339a28b9 621
a8ccdffe
ZW
622 The outermost block of a function always gets a BLOCK node,
623 because the debugging output routines expect that each
59e4e217 624 function has at least one BLOCK. */
f8521984
ZW
625 current_scope->parm_flag = false;
626 current_scope->function_body = true;
a8ccdffe 627 current_scope->keep = true;
f8521984
ZW
628 current_scope->outer_function = current_function_scope;
629 current_function_scope = current_scope;
14e33ee8
ZW
630
631 keep_next_level_flag = false;
a8ccdffe 632 next_is_function_body = false;
51e29401 633 }
339a28b9
ZW
634 else
635 {
f75fbaf7
ZW
636 struct c_scope *scope;
637 if (scope_freelist)
638 {
639 scope = scope_freelist;
640 scope_freelist = scope->outer;
641 }
642 else
5d038c4c 643 scope = GGC_CNEW (struct c_scope);
51e29401 644
f8521984
ZW
645 scope->keep = keep_next_level_flag;
646 scope->outer = current_scope;
f75fbaf7
ZW
647 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
648
649 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
650 possible. */
651 if (current_scope && scope->depth == 0)
652 {
653 scope->depth--;
72c4e4db 654 sorry ("GCC supports only %u nested scopes", scope->depth);
f75fbaf7 655 }
118a3a8b 656
f8521984
ZW
657 current_scope = scope;
658 keep_next_level_flag = false;
339a28b9 659 }
0500d6f9 660}
6645c3fa 661
9affb2c7
ZW
662/* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
663
664static void
665set_type_context (tree type, tree context)
666{
667 for (type = TYPE_MAIN_VARIANT (type); type;
668 type = TYPE_NEXT_VARIANT (type))
669 TYPE_CONTEXT (type) = context;
670}
671
f8521984 672/* Exit a scope. Restore the state of the identifier-decl mappings
f75fbaf7
ZW
673 that were in effect when this scope was entered. Return a BLOCK
674 node containing all the DECLs in this scope that are of interest
675 to debug info generation. */
51e29401
RS
676
677tree
f75fbaf7 678pop_scope (void)
51e29401 679{
f91f41b2 680 struct c_scope *scope = current_scope;
f75fbaf7
ZW
681 tree block, context, p;
682 struct c_binding *b;
9cd51ef6 683
f75fbaf7
ZW
684 bool functionbody = scope->function_body;
685 bool keep = functionbody || scope->keep || scope->bindings;
f91f41b2 686
187230a7
JM
687 c_end_vm_scope (scope->depth);
688
f91f41b2
ZW
689 /* If appropriate, create a BLOCK to record the decls for the life
690 of this function. */
691 block = 0;
692 if (keep)
693 {
694 block = make_node (BLOCK);
f91f41b2
ZW
695 BLOCK_SUBBLOCKS (block) = scope->blocks;
696 TREE_USED (block) = 1;
f91f41b2 697
f75fbaf7 698 /* In each subblock, record that this is its superior. */
87caf699 699 for (p = scope->blocks; p; p = BLOCK_CHAIN (p))
f75fbaf7 700 BLOCK_SUPERCONTEXT (p) = block;
f91f41b2 701
f75fbaf7
ZW
702 BLOCK_VARS (block) = 0;
703 }
339a28b9 704
f75fbaf7
ZW
705 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
706 scope must be set so that they point to the appropriate
707 construct, i.e. either to the current FUNCTION_DECL node, or
708 else to the BLOCK node we just constructed.
f91f41b2 709
f75fbaf7
ZW
710 Note that for tagged types whose scope is just the formal
711 parameter list for some function type specification, we can't
712 properly set their TYPE_CONTEXTs here, because we don't have a
713 pointer to the appropriate FUNCTION_TYPE node readily available
714 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
715 type nodes get set in `grokdeclarator' as soon as we have created
716 the FUNCTION_TYPE node which will represent the "scope" for these
717 "parameter list local" tagged types. */
718 if (scope->function_body)
719 context = current_function_decl;
720 else if (scope == file_scope)
eecec698
ZW
721 {
722 tree file_decl = build_decl (TRANSLATION_UNIT_DECL, 0, 0);
723 TREE_CHAIN (file_decl) = all_translation_units;
724 all_translation_units = file_decl;
725 context = file_decl;
726 }
f75fbaf7
ZW
727 else
728 context = block;
f91f41b2 729
f75fbaf7
ZW
730 /* Clear all bindings in this scope. */
731 for (b = scope->bindings; b; b = free_binding_and_advance (b))
339a28b9 732 {
f75fbaf7 733 p = b->decl;
f91f41b2 734 switch (TREE_CODE (p))
14e33ee8 735 {
f91f41b2 736 case LABEL_DECL:
f75fbaf7 737 /* Warnings for unused labels, errors for undefined labels. */
f91f41b2 738 if (TREE_USED (p) && !DECL_INITIAL (p))
14e33ee8 739 {
dee15844 740 error ("label %q+D used but not defined", p);
f91f41b2 741 DECL_INITIAL (p) = error_mark_node;
14e33ee8 742 }
c616e51b
MLI
743 else
744 warn_for_unused_label (p);
745
f75fbaf7
ZW
746 /* Labels go in BLOCK_VARS. */
747 TREE_CHAIN (p) = BLOCK_VARS (block);
748 BLOCK_VARS (block) = p;
366de0ce 749 gcc_assert (I_LABEL_BINDING (b->id) == b);
c22cacf3
MS
750 I_LABEL_BINDING (b->id) = b->shadowed;
751 break;
339a28b9 752
f75fbaf7
ZW
753 case ENUMERAL_TYPE:
754 case UNION_TYPE:
755 case RECORD_TYPE:
9affb2c7 756 set_type_context (p, context);
f75fbaf7
ZW
757
758 /* Types may not have tag-names, in which case the type
759 appears in the bindings list with b->id NULL. */
760 if (b->id)
761 {
366de0ce 762 gcc_assert (I_TAG_BINDING (b->id) == b);
f75fbaf7
ZW
763 I_TAG_BINDING (b->id) = b->shadowed;
764 }
c22cacf3 765 break;
339a28b9 766
f91f41b2 767 case FUNCTION_DECL:
f75fbaf7
ZW
768 /* Propagate TREE_ADDRESSABLE from nested functions to their
769 containing functions. */
3f75a254 770 if (!TREE_ASM_WRITTEN (p)
f91f41b2
ZW
771 && DECL_INITIAL (p) != 0
772 && TREE_ADDRESSABLE (p)
773 && DECL_ABSTRACT_ORIGIN (p) != 0
774 && DECL_ABSTRACT_ORIGIN (p) != p)
775 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
73aea290 776 if (!DECL_EXTERNAL (p)
71113fcd
GK
777 && !DECL_INITIAL (p)
778 && scope != file_scope
779 && scope != external_scope)
73aea290 780 {
dee15844 781 error ("nested function %q+D declared but never defined", p);
73aea290
JM
782 undef_nested_function = true;
783 }
71113fcd
GK
784 /* C99 6.7.4p6: "a function with external linkage... declared
785 with an inline function specifier ... shall also be defined in the
786 same translation unit." */
787 else if (DECL_DECLARED_INLINE_P (p)
788 && TREE_PUBLIC (p)
789 && !DECL_INITIAL (p)
da1c7394 790 && !flag_gnu89_inline)
fcf73884 791 pedwarn (0, "inline function %q+D declared but never defined", p);
71113fcd 792
f75fbaf7 793 goto common_symbol;
f91f41b2
ZW
794
795 case VAR_DECL:
caf93cb0 796 /* Warnings for unused variables. */
3176a0c2 797 if (!TREE_USED (p)
f95f80d1 798 && !TREE_NO_WARNING (p)
f91f41b2
ZW
799 && !DECL_IN_SYSTEM_HEADER (p)
800 && DECL_NAME (p)
f75fbaf7 801 && !DECL_ARTIFICIAL (p)
29128154 802 && scope != file_scope
c22cacf3 803 && scope != external_scope)
dee15844 804 warning (OPT_Wunused_variable, "unused variable %q+D", p);
f75fbaf7 805
0b410f0b
JM
806 if (b->inner_comp)
807 {
dee15844
JM
808 error ("type of array %q+D completed incompatibly with"
809 " implicit initialization", p);
0b410f0b
JM
810 }
811
a1105617 812 /* Fall through. */
f75fbaf7
ZW
813 case TYPE_DECL:
814 case CONST_DECL:
815 common_symbol:
816 /* All of these go in BLOCK_VARS, but only if this is the
817 binding in the home scope. */
9aaabf8a 818 if (!b->nested)
f75fbaf7
ZW
819 {
820 TREE_CHAIN (p) = BLOCK_VARS (block);
821 BLOCK_VARS (block) = p;
822 }
5b02f0e0
ZW
823 /* If this is the file scope, and we are processing more
824 than one translation unit in this compilation, set
825 DECL_CONTEXT of each decl to the TRANSLATION_UNIT_DECL.
826 This makes same_translation_unit_p work, and causes
827 static declarations to be given disambiguating suffixes. */
828 if (scope == file_scope && num_in_fnames > 1)
9affb2c7
ZW
829 {
830 DECL_CONTEXT (p) = context;
831 if (TREE_CODE (p) == TYPE_DECL)
832 set_type_context (TREE_TYPE (p), context);
833 }
55d54003 834
f75fbaf7
ZW
835 /* Fall through. */
836 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
837 already been put there by store_parm_decls. Unused-
838 parameter warnings are handled by function.c.
839 error_mark_node obviously does not go in BLOCK_VARS and
840 does not get unused-variable warnings. */
841 case PARM_DECL:
842 case ERROR_MARK:
843 /* It is possible for a decl not to have a name. We get
844 here with b->id NULL in this case. */
845 if (b->id)
f91f41b2 846 {
366de0ce 847 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
f75fbaf7 848 I_SYMBOL_BINDING (b->id) = b->shadowed;
0b410f0b
JM
849 if (b->shadowed && b->shadowed->type)
850 TREE_TYPE (b->shadowed->decl) = b->shadowed->type;
f91f41b2
ZW
851 }
852 break;
f75fbaf7
ZW
853
854 default:
366de0ce 855 gcc_unreachable ();
f91f41b2
ZW
856 }
857 }
339a28b9 858
118a3a8b 859
f75fbaf7
ZW
860 /* Dispose of the block that we just made inside some higher level. */
861 if ((scope->function_body || scope == file_scope) && context)
339a28b9 862 {
f75fbaf7
ZW
863 DECL_INITIAL (context) = block;
864 BLOCK_SUPERCONTEXT (block) = context;
339a28b9 865 }
f91f41b2 866 else if (scope->outer)
3bf40d18 867 {
f91f41b2
ZW
868 if (block)
869 SCOPE_LIST_APPEND (scope->outer, blocks, block);
870 /* If we did not make a block for the scope just exited, any
871 blocks made for inner scopes must be carried forward so they
872 will later become subblocks of something else. */
873 else if (scope->blocks)
874 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
3bf40d18 875 }
51e29401 876
f8521984 877 /* Pop the current scope, and free the structure for reuse. */
f75fbaf7
ZW
878 current_scope = scope->outer;
879 if (scope->function_body)
880 current_function_scope = scope->outer_function;
881
882 memset (scope, 0, sizeof (struct c_scope));
883 scope->outer = scope_freelist;
884 scope_freelist = scope;
51e29401 885
51e29401
RS
886 return block;
887}
3bf40d18 888
f75fbaf7
ZW
889void
890push_file_scope (void)
891{
892 tree decl;
f75fbaf7 893
f6bf7bb6
AP
894 if (file_scope)
895 return;
896
f75fbaf7
ZW
897 push_scope ();
898 file_scope = current_scope;
899
900 start_fname_decls ();
901
902 for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl))
9aaabf8a
ZW
903 bind (DECL_NAME (decl), decl, file_scope,
904 /*invisible=*/false, /*nested=*/true);
f75fbaf7
ZW
905}
906
907void
908pop_file_scope (void)
909{
910 /* In case there were missing closebraces, get us back to the global
911 binding level. */
912 while (current_scope != file_scope)
913 pop_scope ();
914
915 /* __FUNCTION__ is defined at file scope (""). This
916 call may not be necessary as my tests indicate it
917 still works without it. */
918 finish_fname_decls ();
919
36c1b0de
ZW
920 /* This is the point to write out a PCH if we're doing that.
921 In that case we do not want to do anything else. */
f75fbaf7 922 if (pch_file)
36c1b0de
ZW
923 {
924 c_common_write_pch ();
925 return;
926 }
f75fbaf7 927
36c1b0de 928 /* Pop off the file scope and close this translation unit. */
f75fbaf7
ZW
929 pop_scope ();
930 file_scope = 0;
bedb9fc0
RH
931
932 maybe_apply_pending_pragma_weaks ();
36c1b0de 933 cgraph_finalize_compilation_unit ();
f75fbaf7
ZW
934}
935
51e29401 936\f
51e29401
RS
937/* Push a definition or a declaration of struct, union or enum tag "name".
938 "type" should be the type node.
939 We assume that the tag "name" is not already defined.
940
941 Note that the definition may really be just a forward reference.
942 In that case, the TYPE_SIZE will be zero. */
943
f75fbaf7 944static void
35b1a6fa 945pushtag (tree name, tree type)
51e29401 946{
14077d68 947 /* Record the identifier as the type's name if it has none. */
f75fbaf7
ZW
948 if (name && !TYPE_NAME (type))
949 TYPE_NAME (type) = name;
9aaabf8a 950 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false);
c138f328 951
51e29401 952 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
f8521984 953 tagged type we just added to the current scope. This fake
51e29401 954 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
858a47b1 955 to output a representation of a tagged type, and it also gives
51e29401
RS
956 us a convenient place to record the "scope start" address for the
957 tagged type. */
958
8d9bfdc5 959 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
88dad228
JM
960
961 /* An approximation for now, so we can tell this is a function-scope tag.
f75fbaf7 962 This will be updated in pop_scope. */
88dad228 963 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
51e29401
RS
964}
965\f
3c6e6fbf 966/* Subroutine of compare_decls. Allow harmless mismatches in return
6907ddd3
RS
967 and argument types provided that the type modes match. This function
968 return a unified type given a suitable match, and 0 otherwise. */
969
970static tree
3c6e6fbf 971match_builtin_function_types (tree newtype, tree oldtype)
6907ddd3
RS
972{
973 tree newrettype, oldrettype;
974 tree newargs, oldargs;
975 tree trytype, tryargs;
976
977 /* Accept the return type of the new declaration if same modes. */
978 oldrettype = TREE_TYPE (oldtype);
979 newrettype = TREE_TYPE (newtype);
980
981 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
982 return 0;
983
984 oldargs = TYPE_ARG_TYPES (oldtype);
985 newargs = TYPE_ARG_TYPES (newtype);
986 tryargs = newargs;
987
988 while (oldargs || newargs)
989 {
3f75a254
JM
990 if (!oldargs
991 || !newargs
992 || !TREE_VALUE (oldargs)
993 || !TREE_VALUE (newargs)
6907ddd3
RS
994 || TYPE_MODE (TREE_VALUE (oldargs))
995 != TYPE_MODE (TREE_VALUE (newargs)))
996 return 0;
997
998 oldargs = TREE_CHAIN (oldargs);
999 newargs = TREE_CHAIN (newargs);
1000 }
1001
1002 trytype = build_function_type (newrettype, tryargs);
1003 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
1004}
1005
1f52178b 1006/* Subroutine of diagnose_mismatched_decls. Check for function type
3c6e6fbf 1007 mismatch involving an empty arglist vs a nonempty one and give clearer
9ac97460 1008 diagnostics. */
3c6e6fbf
ZW
1009static void
1010diagnose_arglist_conflict (tree newdecl, tree olddecl,
1011 tree newtype, tree oldtype)
1012{
1013 tree t;
51e29401 1014
3c6e6fbf 1015 if (TREE_CODE (olddecl) != FUNCTION_DECL
132da1a5 1016 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
3c6e6fbf
ZW
1017 || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
1018 ||
1019 (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
1020 return;
bf44f7de 1021
3c6e6fbf
ZW
1022 t = TYPE_ARG_TYPES (oldtype);
1023 if (t == 0)
1024 t = TYPE_ARG_TYPES (newtype);
1025 for (; t; t = TREE_CHAIN (t))
1026 {
1027 tree type = TREE_VALUE (t);
51e29401 1028
3c6e6fbf
ZW
1029 if (TREE_CHAIN (t) == 0
1030 && TYPE_MAIN_VARIANT (type) != void_type_node)
1031 {
9e637a26 1032 inform ("a parameter list with an ellipsis can%'t match "
3c6e6fbf
ZW
1033 "an empty parameter name list declaration");
1034 break;
1035 }
1036
1037 if (c_type_promotes_to (type) != type)
1038 {
9e637a26 1039 inform ("an argument type that has a default promotion can%'t match "
3c6e6fbf
ZW
1040 "an empty parameter name list declaration");
1041 break;
1042 }
1043 }
1044}
51e29401 1045
3c6e6fbf
ZW
1046/* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1047 old-style function definition, NEWDECL is a prototype declaration.
1048 Diagnose inconsistencies in the argument list. Returns TRUE if
1049 the prototype is compatible, FALSE if not. */
1050static bool
1051validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1052{
bc298aa7
ZW
1053 tree newargs, oldargs;
1054 int i;
1055
2c83be0c 1056#define END_OF_ARGLIST(t) ((t) == void_type_node)
bc298aa7
ZW
1057
1058 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1059 newargs = TYPE_ARG_TYPES (newtype);
1060 i = 1;
1061
1062 for (;;)
9162542e 1063 {
ffc44ab6
VR
1064 tree oldargtype = TREE_VALUE (oldargs);
1065 tree newargtype = TREE_VALUE (newargs);
1066
1067 if (oldargtype == error_mark_node || newargtype == error_mark_node)
1068 return false;
1069
1070 oldargtype = TYPE_MAIN_VARIANT (oldargtype);
1071 newargtype = TYPE_MAIN_VARIANT (newargtype);
bc298aa7
ZW
1072
1073 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1074 break;
1075
1076 /* Reaching the end of just one list means the two decls don't
1077 agree on the number of arguments. */
1078 if (END_OF_ARGLIST (oldargtype))
9162542e 1079 {
dee15844
JM
1080 error ("prototype for %q+D declares more arguments "
1081 "than previous old-style definition", newdecl);
bc298aa7 1082 return false;
9162542e 1083 }
bc298aa7 1084 else if (END_OF_ARGLIST (newargtype))
3c6e6fbf 1085 {
dee15844
JM
1086 error ("prototype for %q+D declares fewer arguments "
1087 "than previous old-style definition", newdecl);
3c6e6fbf
ZW
1088 return false;
1089 }
bc298aa7
ZW
1090
1091 /* Type for passing arg must be consistent with that declared
1092 for the arg. */
3f75a254 1093 else if (!comptypes (oldargtype, newargtype))
3c6e6fbf 1094 {
dee15844 1095 error ("prototype for %q+D declares argument %d"
40b97a2e 1096 " with incompatible type",
dee15844 1097 newdecl, i);
3c6e6fbf
ZW
1098 return false;
1099 }
bc298aa7
ZW
1100
1101 oldargs = TREE_CHAIN (oldargs);
1102 newargs = TREE_CHAIN (newargs);
1103 i++;
9162542e 1104 }
bc298aa7
ZW
1105
1106 /* If we get here, no errors were found, but do issue a warning
1107 for this poor-style construct. */
dee15844
JM
1108 warning (0, "prototype for %q+D follows non-prototype definition",
1109 newdecl);
bc298aa7
ZW
1110 return true;
1111#undef END_OF_ARGLIST
3c6e6fbf 1112}
4b4e3407 1113
3c6e6fbf
ZW
1114/* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1115 first in a pair of mismatched declarations, using the diagnostic
1116 function DIAG. */
1117static void
eb2de0c0 1118locate_old_decl (tree decl, void (*diag)(const char *, ...) ATTRIBUTE_GCC_CDIAG(1,2))
3c6e6fbf
ZW
1119{
1120 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1121 ;
1122 else if (DECL_INITIAL (decl))
dee15844 1123 diag (G_("previous definition of %q+D was here"), decl);
3c6e6fbf 1124 else if (C_DECL_IMPLICIT (decl))
dee15844 1125 diag (G_("previous implicit declaration of %q+D was here"), decl);
3c6e6fbf 1126 else
dee15844 1127 diag (G_("previous declaration of %q+D was here"), decl);
3c6e6fbf 1128}
51e29401 1129
3c6e6fbf
ZW
1130/* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1131 Returns true if the caller should proceed to merge the two, false
1132 if OLDDECL should simply be discarded. As a side effect, issues
1133 all necessary diagnostics for invalid or poor-style combinations.
1134 If it returns true, writes the types of NEWDECL and OLDDECL to
1135 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1136 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1137
1138static bool
1139diagnose_mismatched_decls (tree newdecl, tree olddecl,
1140 tree *newtypep, tree *oldtypep)
1141{
1142 tree newtype, oldtype;
1143 bool pedwarned = false;
1144 bool warned = false;
f8f5c4b3 1145 bool retval = true;
3c6e6fbf 1146
6663ee3b
EC
1147#define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
1148 && DECL_EXTERNAL (DECL))
1149
3c6e6fbf
ZW
1150 /* If we have error_mark_node for either decl or type, just discard
1151 the previous decl - we're in an error cascade already. */
1152 if (olddecl == error_mark_node || newdecl == error_mark_node)
1153 return false;
bc298aa7
ZW
1154 *oldtypep = oldtype = TREE_TYPE (olddecl);
1155 *newtypep = newtype = TREE_TYPE (newdecl);
3c6e6fbf
ZW
1156 if (oldtype == error_mark_node || newtype == error_mark_node)
1157 return false;
1158
1159 /* Two different categories of symbol altogether. This is an error
1160 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
51e29401
RS
1161 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1162 {
f75fbaf7
ZW
1163 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1164 && DECL_BUILT_IN (olddecl)
1165 && !C_DECL_DECLARED_BUILTIN (olddecl)))
51e29401 1166 {
dee15844 1167 error ("%q+D redeclared as different kind of symbol", newdecl);
3c6e6fbf
ZW
1168 locate_old_decl (olddecl, error);
1169 }
1170 else if (TREE_PUBLIC (newdecl))
dee15844
JM
1171 warning (0, "built-in function %q+D declared as non-function",
1172 newdecl);
44c21c7f 1173 else
dee15844
JM
1174 warning (OPT_Wshadow, "declaration of %q+D shadows "
1175 "a built-in function", newdecl);
3c6e6fbf
ZW
1176 return false;
1177 }
1178
085e33aa
JM
1179 /* Enumerators have no linkage, so may only be declared once in a
1180 given scope. */
1181 if (TREE_CODE (olddecl) == CONST_DECL)
1182 {
dee15844 1183 error ("redeclaration of enumerator %q+D", newdecl);
085e33aa
JM
1184 locate_old_decl (olddecl, error);
1185 return false;
1186 }
1187
132da1a5 1188 if (!comptypes (oldtype, newtype))
3c6e6fbf 1189 {
a6f78652 1190 if (TREE_CODE (olddecl) == FUNCTION_DECL
f75fbaf7 1191 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
3c6e6fbf
ZW
1192 {
1193 /* Accept harmless mismatch in function types.
1194 This is for the ffs and fprintf builtins. */
1195 tree trytype = match_builtin_function_types (newtype, oldtype);
1196
132da1a5 1197 if (trytype && comptypes (newtype, trytype))
bc298aa7 1198 *oldtypep = oldtype = trytype;
3c6e6fbf 1199 else
51e29401 1200 {
3c6e6fbf
ZW
1201 /* If types don't match for a built-in, throw away the
1202 built-in. No point in calling locate_old_decl here, it
9ac97460 1203 won't print anything. */
dee15844
JM
1204 warning (0, "conflicting types for built-in function %q+D",
1205 newdecl);
3c6e6fbf 1206 return false;
51e29401 1207 }
3c6e6fbf
ZW
1208 }
1209 else if (TREE_CODE (olddecl) == FUNCTION_DECL
3c20847b 1210 && DECL_IS_BUILTIN (olddecl))
3c6e6fbf
ZW
1211 {
1212 /* A conflicting function declaration for a predeclared
1213 function that isn't actually built in. Objective C uses
1214 these. The new declaration silently overrides everything
1215 but the volatility (i.e. noreturn) indication. See also
1216 below. FIXME: Make Objective C use normal builtins. */
1217 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1218 return false;
1219 }
1220 /* Permit void foo (...) to match int foo (...) if the latter is
1221 the definition and implicit int was used. See
1222 c-torture/compile/920625-2.c. */
1223 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1224 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1225 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
12869142 1226 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
3c6e6fbf 1227 {
fcf73884 1228 pedwarn (0, "conflicting types for %q+D", newdecl);
3c6e6fbf 1229 /* Make sure we keep void as the return type. */
bc298aa7 1230 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
3c6e6fbf
ZW
1231 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1232 pedwarned = true;
51e29401 1233 }
12869142
DJ
1234 /* Permit void foo (...) to match an earlier call to foo (...) with
1235 no declared type (thus, implicitly int). */
1236 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1237 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1238 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1239 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1240 {
fcf73884 1241 pedwarn (0, "conflicting types for %q+D", newdecl);
12869142
DJ
1242 /* Make sure we keep void as the return type. */
1243 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1244 pedwarned = true;
1245 }
51e29401
RS
1246 else
1247 {
118a3a8b 1248 if (TYPE_QUALS (newtype) != TYPE_QUALS (oldtype))
dee15844 1249 error ("conflicting type qualifiers for %q+D", newdecl);
118a3a8b 1250 else
dee15844 1251 error ("conflicting types for %q+D", newdecl);
3c6e6fbf
ZW
1252 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1253 locate_old_decl (olddecl, error);
1254 return false;
51e29401 1255 }
51e29401
RS
1256 }
1257
3c6e6fbf
ZW
1258 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1259 but silently ignore the redeclaration if either is in a system
1260 header. (Conflicting redeclarations were handled above.) */
1261 if (TREE_CODE (newdecl) == TYPE_DECL)
339a28b9 1262 {
df0ed6c5
RAE
1263 if (DECL_IN_SYSTEM_HEADER (newdecl)
1264 || DECL_IN_SYSTEM_HEADER (olddecl)
1265 || TREE_NO_WARNING (newdecl)
1266 || TREE_NO_WARNING (olddecl))
a1105617 1267 return true; /* Allow OLDDECL to continue in use. */
118a3a8b 1268
dee15844 1269 error ("redefinition of typedef %q+D", newdecl);
3c6e6fbf
ZW
1270 locate_old_decl (olddecl, error);
1271 return false;
339a28b9 1272 }
51e29401 1273
3c6e6fbf
ZW
1274 /* Function declarations can either be 'static' or 'extern' (no
1275 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
71113fcd
GK
1276 can never conflict with each other on account of linkage
1277 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
1278 gnu89 mode permits two definitions if one is 'extern inline' and
1279 one is not. The non- extern-inline definition supersedes the
1280 extern-inline definition. */
6663ee3b 1281
3c6e6fbf 1282 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
51e29401 1283 {
04b8f97f
ZW
1284 /* If you declare a built-in function name as static, or
1285 define the built-in with an old-style definition (so we
1286 can't validate the argument list) the built-in definition is
1287 overridden, but optionally warn this was a bad choice of name. */
1288 if (DECL_BUILT_IN (olddecl)
f75fbaf7 1289 && !C_DECL_DECLARED_BUILTIN (olddecl)
04b8f97f
ZW
1290 && (!TREE_PUBLIC (newdecl)
1291 || (DECL_INITIAL (newdecl)
1292 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
51e29401 1293 {
dee15844
JM
1294 warning (OPT_Wshadow, "declaration of %q+D shadows "
1295 "a built-in function", newdecl);
51e29401 1296 /* Discard the old built-in function. */
3c6e6fbf 1297 return false;
51e29401 1298 }
118a3a8b 1299
3c6e6fbf 1300 if (DECL_INITIAL (newdecl))
4c41bbfa 1301 {
f8f5c4b3 1302 if (DECL_INITIAL (olddecl))
6907ddd3 1303 {
6663ee3b 1304 /* If both decls are in the same TU and the new declaration
619519c8 1305 isn't overriding an extern inline reject the new decl.
71113fcd
GK
1306 In c99, no overriding is allowed in the same translation
1307 unit. */
1308 if ((!DECL_EXTERN_INLINE (olddecl)
1309 || DECL_EXTERN_INLINE (newdecl)
da1c7394 1310 || (!flag_gnu89_inline
4eb7fd83
JJ
1311 && (!DECL_DECLARED_INLINE_P (olddecl)
1312 || !lookup_attribute ("gnu_inline",
1313 DECL_ATTRIBUTES (olddecl)))
1314 && (!DECL_DECLARED_INLINE_P (newdecl)
1315 || !lookup_attribute ("gnu_inline",
1316 DECL_ATTRIBUTES (newdecl))))
4eb7fd83 1317 )
f8f5c4b3
AP
1318 && same_translation_unit_p (newdecl, olddecl))
1319 {
dee15844 1320 error ("redefinition of %q+D", newdecl);
f8f5c4b3
AP
1321 locate_old_decl (olddecl, error);
1322 return false;
1323 }
6663ee3b 1324 }
4c41bbfa 1325 }
3c6e6fbf
ZW
1326 /* If we have a prototype after an old-style function definition,
1327 the argument types must be checked specially. */
1328 else if (DECL_INITIAL (olddecl)
1329 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1330 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1331 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
52b6a22f 1332 {
3c6e6fbf
ZW
1333 locate_old_decl (olddecl, error);
1334 return false;
52b6a22f 1335 }
9aaabf8a
ZW
1336 /* A non-static declaration (even an "extern") followed by a
1337 static declaration is undefined behavior per C99 6.2.2p3-5,7.
1338 The same is true for a static forward declaration at block
1339 scope followed by a non-static declaration/definition at file
1340 scope. Static followed by non-static at the same scope is
1341 not undefined behavior, and is the most convenient way to get
1342 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
1343 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
8c27b7d4 1344 we do diagnose it if -Wtraditional. */
3c6e6fbf 1345 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
51e29401 1346 {
9aaabf8a
ZW
1347 /* Two exceptions to the rule. If olddecl is an extern
1348 inline, or a predeclared function that isn't actually
1349 built in, newdecl silently overrides olddecl. The latter
1350 occur only in Objective C; see also above. (FIXME: Make
1351 Objective C use normal builtins.) */
1352 if (!DECL_IS_BUILTIN (olddecl)
6663ee3b 1353 && !DECL_EXTERN_INLINE (olddecl))
3c6e6fbf 1354 {
dee15844
JM
1355 error ("static declaration of %q+D follows "
1356 "non-static declaration", newdecl);
9aaabf8a 1357 locate_old_decl (olddecl, error);
3c6e6fbf 1358 }
9aaabf8a 1359 return false;
51e29401 1360 }
9aaabf8a 1361 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
e841f997 1362 {
9aaabf8a
ZW
1363 if (DECL_CONTEXT (olddecl))
1364 {
dee15844
JM
1365 error ("non-static declaration of %q+D follows "
1366 "static declaration", newdecl);
9aaabf8a
ZW
1367 locate_old_decl (olddecl, error);
1368 return false;
1369 }
1370 else if (warn_traditional)
1371 {
dee15844
JM
1372 warning (OPT_Wtraditional, "non-static declaration of %q+D "
1373 "follows static declaration", newdecl);
9aaabf8a
ZW
1374 warned = true;
1375 }
e841f997 1376 }
4eb7fd83
JJ
1377
1378 /* Make sure gnu_inline attribute is either not present, or
1379 present on all inline decls. */
1380 if (DECL_DECLARED_INLINE_P (olddecl)
1381 && DECL_DECLARED_INLINE_P (newdecl))
1382 {
1383 bool newa = lookup_attribute ("gnu_inline",
1384 DECL_ATTRIBUTES (newdecl)) != NULL;
1385 bool olda = lookup_attribute ("gnu_inline",
1386 DECL_ATTRIBUTES (olddecl)) != NULL;
1387 if (newa != olda)
1388 {
1389 error ("%<gnu_inline%> attribute present on %q+D",
1390 newa ? newdecl : olddecl);
1391 error ("%Jbut not here", newa ? olddecl : newdecl);
1392 }
1393 }
51e29401 1394 }
3c6e6fbf 1395 else if (TREE_CODE (newdecl) == VAR_DECL)
339a28b9 1396 {
3c6e6fbf
ZW
1397 /* Only variables can be thread-local, and all declarations must
1398 agree on this property. */
953ff289
DN
1399 if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
1400 {
1401 /* Nothing to check. Since OLDDECL is marked threadprivate
1402 and NEWDECL does not have a thread-local attribute, we
1403 will merge the threadprivate attribute into NEWDECL. */
1404 ;
1405 }
1406 else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
51e29401 1407 {
c2f7fa15 1408 if (DECL_THREAD_LOCAL_P (newdecl))
dee15844
JM
1409 error ("thread-local declaration of %q+D follows "
1410 "non-thread-local declaration", newdecl);
3c6e6fbf 1411 else
dee15844
JM
1412 error ("non-thread-local declaration of %q+D follows "
1413 "thread-local declaration", newdecl);
51e29401 1414
3c6e6fbf
ZW
1415 locate_old_decl (olddecl, error);
1416 return false;
1417 }
51e29401 1418
3c6e6fbf
ZW
1419 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1420 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1421 {
dee15844 1422 error ("redefinition of %q+D", newdecl);
3c6e6fbf
ZW
1423 locate_old_decl (olddecl, error);
1424 return false;
1425 }
1426
9aaabf8a
ZW
1427 /* Objects declared at file scope: if the first declaration had
1428 external linkage (even if it was an external reference) the
1429 second must have external linkage as well, or the behavior is
1430 undefined. If the first declaration had internal linkage, then
1431 the second must too, or else be an external reference (in which
1432 case the composite declaration still has internal linkage).
1433 As for function declarations, we warn about the static-then-
1434 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
1435 if (DECL_FILE_SCOPE_P (newdecl)
1436 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
3c6e6fbf 1437 {
9aaabf8a
ZW
1438 if (DECL_EXTERNAL (newdecl))
1439 {
558d1f81
JM
1440 if (!DECL_FILE_SCOPE_P (olddecl))
1441 {
dee15844
JM
1442 error ("extern declaration of %q+D follows "
1443 "declaration with no linkage", newdecl);
558d1f81
JM
1444 locate_old_decl (olddecl, error);
1445 return false;
1446 }
1447 else if (warn_traditional)
9aaabf8a 1448 {
dee15844
JM
1449 warning (OPT_Wtraditional, "non-static declaration of %q+D "
1450 "follows static declaration", newdecl);
9aaabf8a
ZW
1451 warned = true;
1452 }
1453 }
1454 else
3c6e6fbf
ZW
1455 {
1456 if (TREE_PUBLIC (newdecl))
dee15844
JM
1457 error ("non-static declaration of %q+D follows "
1458 "static declaration", newdecl);
3c6e6fbf 1459 else
dee15844
JM
1460 error ("static declaration of %q+D follows "
1461 "non-static declaration", newdecl);
3c6e6fbf
ZW
1462
1463 locate_old_decl (olddecl, error);
1464 return false;
51e29401
RS
1465 }
1466 }
3c6e6fbf
ZW
1467 /* Two objects with the same name declared at the same block
1468 scope must both be external references (6.7p3). */
558d1f81 1469 else if (!DECL_FILE_SCOPE_P (newdecl))
3c6e6fbf
ZW
1470 {
1471 if (DECL_EXTERNAL (newdecl))
0b410f0b
JM
1472 {
1473 /* Extern with initializer at block scope, which will
1474 already have received an error. */
1475 }
3c6e6fbf 1476 else if (DECL_EXTERNAL (olddecl))
0b410f0b 1477 {
dee15844
JM
1478 error ("declaration of %q+D with no linkage follows "
1479 "extern declaration", newdecl);
0b410f0b
JM
1480 locate_old_decl (olddecl, error);
1481 }
3c6e6fbf 1482 else
0b410f0b 1483 {
dee15844 1484 error ("redeclaration of %q+D with no linkage", newdecl);
0b410f0b
JM
1485 locate_old_decl (olddecl, error);
1486 }
ecf92f82 1487
3c6e6fbf
ZW
1488 return false;
1489 }
1ae0ccb6 1490 }
3c6e6fbf
ZW
1491
1492 /* warnings */
d7afec4b 1493 /* All decls must agree on a visibility. */
c22cacf3 1494 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
820cc88f 1495 && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
3c6e6fbf 1496 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1ae0ccb6 1497 {
dee15844
JM
1498 warning (0, "redeclaration of %q+D with different visibility "
1499 "(old visibility preserved)", newdecl);
3c6e6fbf 1500 warned = true;
1ae0ccb6 1501 }
3c6e6fbf
ZW
1502
1503 if (TREE_CODE (newdecl) == FUNCTION_DECL)
51e29401 1504 {
3c6e6fbf
ZW
1505 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
1506 if (DECL_DECLARED_INLINE_P (newdecl)
1507 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
51e29401 1508 {
dee15844
JM
1509 warning (OPT_Wattributes, "inline declaration of %qD follows "
1510 "declaration with attribute noinline", newdecl);
3c6e6fbf 1511 warned = true;
51e29401 1512 }
3c6e6fbf
ZW
1513 else if (DECL_DECLARED_INLINE_P (olddecl)
1514 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
4b4e3407 1515 {
dee15844
JM
1516 warning (OPT_Wattributes, "declaration of %q+D with attribute "
1517 "noinline follows inline declaration ", newdecl);
3c6e6fbf 1518 warned = true;
4b4e3407 1519 }
3c6e6fbf 1520 }
3205a71e 1521 else /* PARM_DECL, VAR_DECL */
3c6e6fbf 1522 {
f75fbaf7
ZW
1523 /* Redeclaration of a parameter is a constraint violation (this is
1524 not explicitly stated, but follows from C99 6.7p3 [no more than
1525 one declaration of the same identifier with no linkage in the
1526 same scope, except type tags] and 6.2.2p6 [parameters have no
1527 linkage]). We must check for a forward parameter declaration,
1528 indicated by TREE_ASM_WRITTEN on the old declaration - this is
1529 an extension, the mandatory diagnostic for which is handled by
1530 mark_forward_parm_decls. */
1531
3205a71e
ZW
1532 if (TREE_CODE (newdecl) == PARM_DECL
1533 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1534 {
dee15844 1535 error ("redefinition of parameter %q+D", newdecl);
3205a71e
ZW
1536 locate_old_decl (olddecl, error);
1537 return false;
1538 }
51e29401
RS
1539 }
1540
3c6e6fbf
ZW
1541 /* Optional warning for completely redundant decls. */
1542 if (!warned && !pedwarned
1543 && warn_redundant_decls
1544 /* Don't warn about a function declaration followed by a
1545 definition. */
3205a71e
ZW
1546 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1547 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
8c27b7d4 1548 /* Don't warn about redundant redeclarations of builtins. */
c5e36c09
ZW
1549 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1550 && !DECL_BUILT_IN (newdecl)
1551 && DECL_BUILT_IN (olddecl)
1552 && !C_DECL_DECLARED_BUILTIN (olddecl))
3205a71e
ZW
1553 /* Don't warn about an extern followed by a definition. */
1554 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1555 /* Don't warn about forward parameter decls. */
1556 && !(TREE_CODE (newdecl) == PARM_DECL
52d09157
CR
1557 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1558 /* Don't warn about a variable definition following a declaration. */
1559 && !(TREE_CODE (newdecl) == VAR_DECL
1560 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
27f427f8 1561 {
dee15844
JM
1562 warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
1563 newdecl);
3c6e6fbf 1564 warned = true;
27f427f8
RS
1565 }
1566
3c6e6fbf
ZW
1567 /* Report location of previous decl/defn in a consistent manner. */
1568 if (warned || pedwarned)
fcf73884 1569 locate_old_decl (olddecl, pedwarned ? pedwarn0 : warning0);
664b4b1e 1570
6663ee3b
EC
1571#undef DECL_EXTERN_INLINE
1572
f8f5c4b3 1573 return retval;
3c6e6fbf 1574}
51e29401 1575
3c6e6fbf
ZW
1576/* Subroutine of duplicate_decls. NEWDECL has been found to be
1577 consistent with OLDDECL, but carries new information. Merge the
1ef82ef2 1578 new information into OLDDECL. This function issues no
3c6e6fbf
ZW
1579 diagnostics. */
1580
1581static void
1ef82ef2 1582merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
3c6e6fbf 1583{
71113fcd
GK
1584 bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1585 && DECL_INITIAL (newdecl) != 0);
1586 bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
1587 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != 0);
1588 bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
1589 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) != 0);
1590 bool extern_changed = false;
3c6e6fbf 1591
f75fbaf7
ZW
1592 /* For real parm decl following a forward decl, rechain the old decl
1593 in its new location and clear TREE_ASM_WRITTEN (it's not a
1594 forward decl anymore). */
3c6e6fbf 1595 if (TREE_CODE (newdecl) == PARM_DECL
3f75a254 1596 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
51e29401 1597 {
f75fbaf7
ZW
1598 struct c_binding *b, **here;
1599
1600 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
1601 if ((*here)->decl == olddecl)
1602 goto found;
366de0ce 1603 gcc_unreachable ();
f75fbaf7
ZW
1604
1605 found:
1606 b = *here;
1607 *here = b->prev;
1608 b->prev = current_scope->bindings;
1609 current_scope->bindings = b;
1610
3c6e6fbf 1611 TREE_ASM_WRITTEN (olddecl) = 0;
3c6e6fbf
ZW
1612 }
1613
1614 DECL_ATTRIBUTES (newdecl)
5fd9b178 1615 = targetm.merge_decl_attributes (olddecl, newdecl);
bf44f7de 1616
3c6e6fbf 1617 /* Merge the data types specified in the two decls. */
1ef82ef2
ZW
1618 TREE_TYPE (newdecl)
1619 = TREE_TYPE (olddecl)
10bc1b1b 1620 = composite_type (newtype, oldtype);
51e29401 1621
3c6e6fbf 1622 /* Lay the type out, unless already done. */
2587f865 1623 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
3c6e6fbf
ZW
1624 {
1625 if (TREE_TYPE (newdecl) != error_mark_node)
1626 layout_type (TREE_TYPE (newdecl));
1627 if (TREE_CODE (newdecl) != FUNCTION_DECL
1628 && TREE_CODE (newdecl) != TYPE_DECL
1629 && TREE_CODE (newdecl) != CONST_DECL)
1630 layout_decl (newdecl, 0);
1631 }
1632 else
1633 {
1634 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1635 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1636 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1637 DECL_MODE (newdecl) = DECL_MODE (olddecl);
837edd5f
GK
1638 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1639 {
1640 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
8220334a 1641 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
837edd5f 1642 }
3c6e6fbf 1643 }
fc542d3c 1644
9a60b229
JJ
1645 /* Keep the old rtl since we can safely use it. */
1646 if (HAS_RTL_P (olddecl))
1647 COPY_DECL_RTL (olddecl, newdecl);
17aec3eb 1648
3c6e6fbf
ZW
1649 /* Merge the type qualifiers. */
1650 if (TREE_READONLY (newdecl))
1ef82ef2 1651 TREE_READONLY (olddecl) = 1;
facef326 1652
3c6e6fbf 1653 if (TREE_THIS_VOLATILE (newdecl))
ee9f69b4 1654 TREE_THIS_VOLATILE (olddecl) = 1;
3c6e6fbf 1655
302a2cc5
JM
1656 /* Merge deprecatedness. */
1657 if (TREE_DEPRECATED (newdecl))
1658 TREE_DEPRECATED (olddecl) = 1;
1659
238fe3c5
RAE
1660 /* If a decl is in a system header and the other isn't, keep the one on the
1661 system header. Otherwise, keep source location of definition rather than
1662 declaration and of prototype rather than non-prototype unless that
1663 prototype is built-in. */
1664 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
1665 && DECL_IN_SYSTEM_HEADER (olddecl)
1666 && !DECL_IN_SYSTEM_HEADER (newdecl) )
aa14403d 1667 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
238fe3c5
RAE
1668 else if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
1669 && DECL_IN_SYSTEM_HEADER (newdecl)
1670 && !DECL_IN_SYSTEM_HEADER (olddecl))
aa14403d 1671 DECL_SOURCE_LOCATION (olddecl) = DECL_SOURCE_LOCATION (newdecl);
238fe3c5
RAE
1672 else if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1673 || (old_is_prototype && !new_is_prototype
1674 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
3c6e6fbf
ZW
1675 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1676
3c6e6fbf 1677 /* Merge the initialization information. */
1ef82ef2 1678 if (DECL_INITIAL (newdecl) == 0)
3c6e6fbf
ZW
1679 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1680
953ff289
DN
1681 /* Merge the threadprivate attribute. */
1682 if (TREE_CODE (olddecl) == VAR_DECL && C_DECL_THREADPRIVATE_P (olddecl))
1683 {
1684 DECL_TLS_MODEL (newdecl) = DECL_TLS_MODEL (olddecl);
1685 C_DECL_THREADPRIVATE_P (newdecl) = 1;
1686 }
1687
fbbc17c7
MS
1688 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
1689 {
fbbc17c7
MS
1690 /* Merge the section attribute.
1691 We want to issue an error if the sections conflict but that
1692 must be done later in decl_attributes since we are called
1693 before attributes are assigned. */
1694 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1695 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1696
1697 /* Copy the assembler name.
1698 Currently, it can only be defined in the prototype. */
1699 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1700
1701 /* Use visibility of whichever declaration had it specified */
1702 if (DECL_VISIBILITY_SPECIFIED (olddecl))
1703 {
1704 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1705 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
1706 }
1707
1708 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1709 {
1710 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1711 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1712 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1713 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1714 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1715 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
fbbc17c7 1716 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1ea193c2 1717 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
becfd6e5
KZ
1718 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1719 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
fbbc17c7
MS
1720 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
1721 }
1722
1723 /* Merge the storage class information. */
1724 merge_weak (newdecl, olddecl);
1725
1726 /* For functions, static overrides non-static. */
1727 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1728 {
1729 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1730 /* This is since we don't automatically
1731 copy the attributes of NEWDECL into OLDDECL. */
1732 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1733 /* If this clears `static', clear it in the identifier too. */
1734 if (!TREE_PUBLIC (olddecl))
1735 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1736 }
1737 }
1738
4eb7fd83
JJ
1739 /* In c99, 'extern' declaration before (or after) 'inline' means this
1740 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
1741 is present. */
1742 if (TREE_CODE (newdecl) == FUNCTION_DECL
da1c7394 1743 && !flag_gnu89_inline
4eb7fd83
JJ
1744 && (DECL_DECLARED_INLINE_P (newdecl)
1745 || DECL_DECLARED_INLINE_P (olddecl))
1746 && (!DECL_DECLARED_INLINE_P (newdecl)
1747 || !DECL_DECLARED_INLINE_P (olddecl)
1748 || !DECL_EXTERNAL (olddecl))
1749 && DECL_EXTERNAL (newdecl)
6cf59865 1750 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))
74b0ec7c 1751 && !current_function_decl)
4eb7fd83 1752 DECL_EXTERNAL (newdecl) = 0;
71113fcd 1753
fbbc17c7
MS
1754 if (DECL_EXTERNAL (newdecl))
1755 {
1756 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1757 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1758
1759 /* An extern decl does not override previous storage class. */
1760 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1761 if (!DECL_EXTERNAL (newdecl))
1762 {
1763 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1764 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1765 }
1766 }
1767 else
1768 {
1769 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1770 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1771 }
1772
1773 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1774 {
1775 /* If we're redefining a function previously defined as extern
1776 inline, make sure we emit debug info for the inline before we
1777 throw it away, in case it was inlined into a function that
1778 hasn't been written out yet. */
1779 if (new_is_definition && DECL_INITIAL (olddecl))
1780 {
1781 if (TREE_USED (olddecl)
7e8b322a
JH
1782 /* We never inline re-defined extern inline functions.
1783 FIXME: This would be better handled by keeping both functions
1784 as separate declarations. */
fbbc17c7
MS
1785 && cgraph_function_possibly_inlined_p (olddecl))
1786 (*debug_hooks->outlining_inline_function) (olddecl);
1787
1788 /* The new defn must not be inline. */
1789 DECL_INLINE (newdecl) = 0;
1790 DECL_UNINLINABLE (newdecl) = 1;
1791 }
1792 else
1793 {
1794 /* If either decl says `inline', this fn is inline, unless
1795 its definition was passed already. */
1796 if (DECL_DECLARED_INLINE_P (newdecl)
1797 || DECL_DECLARED_INLINE_P (olddecl))
1798 DECL_DECLARED_INLINE_P (newdecl) = 1;
1799
1800 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1801 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
c536a6a7
RG
1802
1803 DECL_DISREGARD_INLINE_LIMITS (newdecl)
1804 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
1805 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
1806 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
fbbc17c7
MS
1807 }
1808
1809 if (DECL_BUILT_IN (olddecl))
1810 {
1811 /* If redeclaring a builtin function, it stays built in.
1812 But it gets tagged as having been declared. */
1813 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1814 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1815 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1816 if (new_is_prototype)
1817 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
1818 else
1819 C_DECL_BUILTIN_PROTOTYPE (newdecl)
1820 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
1821 }
1822
ab442df7
MM
1823 /* Preserve function specific target and optimization options */
1824 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
1825 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
1826 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
1827 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
1828
1829 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
1830 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
1831 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
1832 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
1833
fbbc17c7
MS
1834 /* Also preserve various other info from the definition. */
1835 if (!new_is_definition)
1836 {
1837 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1838 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1839 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1840 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
726a989a 1841 gimple_set_body (newdecl, gimple_body (olddecl));
fbbc17c7
MS
1842 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1843
1844 /* Set DECL_INLINE on the declaration if we've got a body
1845 from which to instantiate. */
1846 if (DECL_INLINE (olddecl) && !DECL_UNINLINABLE (newdecl))
1847 {
1848 DECL_INLINE (newdecl) = 1;
1849 DECL_ABSTRACT_ORIGIN (newdecl)
1850 = DECL_ABSTRACT_ORIGIN (olddecl);
1851 }
1852 }
1853 else
1854 {
1855 /* If a previous declaration said inline, mark the
1856 definition as inlinable. */
1857 if (DECL_DECLARED_INLINE_P (newdecl)
1858 && !DECL_UNINLINABLE (newdecl))
1859 DECL_INLINE (newdecl) = 1;
1860 }
1861 }
1862
71113fcd
GK
1863 extern_changed = DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl);
1864
fbbc17c7 1865 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
9affb2c7 1866 But preserve OLDDECL's DECL_UID and DECL_CONTEXT. */
850cba29 1867 {
b3694847 1868 unsigned olddecl_uid = DECL_UID (olddecl);
9affb2c7 1869 tree olddecl_context = DECL_CONTEXT (olddecl);
c22cacf3 1870
da61dec9
JM
1871 memcpy ((char *) olddecl + sizeof (struct tree_common),
1872 (char *) newdecl + sizeof (struct tree_common),
820cc88f
DB
1873 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
1874 switch (TREE_CODE (olddecl))
1875 {
726a989a
RB
1876 case FUNCTION_DECL:
1877 gimple_set_body (olddecl, gimple_body (newdecl));
1878 /* fall through */
1879
820cc88f
DB
1880 case FIELD_DECL:
1881 case VAR_DECL:
1882 case PARM_DECL:
1883 case LABEL_DECL:
1884 case RESULT_DECL:
1885 case CONST_DECL:
1886 case TYPE_DECL:
820cc88f
DB
1887 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
1888 (char *) newdecl + sizeof (struct tree_decl_common),
1889 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
1890 break;
1891
1892 default:
c22cacf3 1893
820cc88f
DB
1894 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
1895 (char *) newdecl + sizeof (struct tree_decl_common),
1896 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
1897 }
850cba29 1898 DECL_UID (olddecl) = olddecl_uid;
9affb2c7 1899 DECL_CONTEXT (olddecl) = olddecl_context;
850cba29 1900 }
51e29401 1901
2306d91c
RH
1902 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1903 so that encode_section_info has a chance to look at the new decl
1904 flags and attributes. */
1905 if (DECL_RTL_SET_P (olddecl)
1906 && (TREE_CODE (olddecl) == FUNCTION_DECL
1907 || (TREE_CODE (olddecl) == VAR_DECL
1908 && TREE_STATIC (olddecl))))
0e6df31e 1909 make_decl_rtl (olddecl);
71113fcd
GK
1910
1911 /* If we changed a function from DECL_EXTERNAL to !DECL_EXTERNAL,
1912 and the definition is coming from the old version, cgraph needs
1913 to be called again. */
f0c882ab 1914 if (extern_changed && !new_is_definition
71113fcd 1915 && TREE_CODE (olddecl) == FUNCTION_DECL && DECL_INITIAL (olddecl))
f0c882ab 1916 cgraph_mark_if_needed (olddecl);
51e29401
RS
1917}
1918
3c6e6fbf
ZW
1919/* Handle when a new declaration NEWDECL has the same name as an old
1920 one OLDDECL in the same binding contour. Prints an error message
1921 if appropriate.
1922
1923 If safely possible, alter OLDDECL to look like NEWDECL, and return
1ef82ef2 1924 true. Otherwise, return false. */
3c6e6fbf
ZW
1925
1926static bool
1ef82ef2 1927duplicate_decls (tree newdecl, tree olddecl)
3c6e6fbf 1928{
6de9cd9a 1929 tree newtype = NULL, oldtype = NULL;
3c6e6fbf
ZW
1930
1931 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
f95f80d1 1932 {
f9837879 1933 /* Avoid `unused variable' and other warnings for OLDDECL. */
f95f80d1
SB
1934 TREE_NO_WARNING (olddecl) = 1;
1935 return false;
1936 }
3c6e6fbf 1937
1ef82ef2
ZW
1938 merge_decls (newdecl, olddecl, newtype, oldtype);
1939 return true;
3c6e6fbf 1940}
118a3a8b 1941
3c6e6fbf 1942\f
1ad463f4 1943/* Check whether decl-node NEW_DECL shadows an existing declaration. */
339a28b9 1944static void
1ad463f4 1945warn_if_shadowing (tree new_decl)
339a28b9 1946{
f75fbaf7 1947 struct c_binding *b;
339a28b9 1948
f75fbaf7
ZW
1949 /* Shadow warnings wanted? */
1950 if (!warn_shadow
339a28b9 1951 /* No shadow warnings for internally generated vars. */
1ad463f4 1952 || DECL_IS_BUILTIN (new_decl)
339a28b9 1953 /* No shadow warnings for vars made for inlining. */
7c47d6e9 1954 || DECL_FROM_INLINE (new_decl))
26f943fd
NB
1955 return;
1956
9aaabf8a 1957 /* Is anything being shadowed? Invisible decls do not count. */
1ad463f4
BI
1958 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
1959 if (b->decl && b->decl != new_decl && !b->invisible)
f75fbaf7 1960 {
1ad463f4 1961 tree old_decl = b->decl;
f75fbaf7 1962
d8dd2f3a
JJ
1963 if (old_decl == error_mark_node)
1964 {
b9b8dde3
DD
1965 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
1966 "non-variable", new_decl);
d8dd2f3a
JJ
1967 break;
1968 }
1969 else if (TREE_CODE (old_decl) == PARM_DECL)
b9b8dde3 1970 warning (OPT_Wshadow, "declaration of %q+D shadows a parameter",
dee15844 1971 new_decl);
b9b8dde3
DD
1972 else if (DECL_FILE_SCOPE_P (old_decl))
1973 warning (OPT_Wshadow, "declaration of %q+D shadows a global "
1974 "declaration", new_decl);
1ad463f4
BI
1975 else if (TREE_CODE (old_decl) == FUNCTION_DECL
1976 && DECL_BUILT_IN (old_decl))
d8dd2f3a 1977 {
b9b8dde3
DD
1978 warning (OPT_Wshadow, "declaration of %q+D shadows "
1979 "a built-in function", new_decl);
d8dd2f3a
JJ
1980 break;
1981 }
f75fbaf7 1982 else
b9b8dde3 1983 warning (OPT_Wshadow, "declaration of %q+D shadows a previous local",
dee15844 1984 new_decl);
f75fbaf7 1985
b9b8dde3 1986 warning (OPT_Wshadow, "%Jshadowed declaration is here", old_decl);
a6f78652 1987
f75fbaf7
ZW
1988 break;
1989 }
339a28b9
ZW
1990}
1991
1992
1993/* Subroutine of pushdecl.
1994
1995 X is a TYPE_DECL for a typedef statement. Create a brand new
1996 ..._TYPE node (which will be just a variant of the existing
1997 ..._TYPE node with identical properties) and then install X
1998 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1999
2000 The whole point here is to end up with a situation where each
2001 and every ..._TYPE node the compiler creates will be uniquely
2002 associated with AT MOST one node representing a typedef name.
2003 This way, even though the compiler substitutes corresponding
2004 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
2005 early on, later parts of the compiler can always do the reverse
2006 translation and get back the corresponding typedef name. For
2007 example, given:
2008
c22cacf3 2009 typedef struct S MY_TYPE;
339a28b9
ZW
2010 MY_TYPE object;
2011
2012 Later parts of the compiler might only know that `object' was of
2013 type `struct S' if it were not for code just below. With this
2014 code however, later parts of the compiler see something like:
2015
2016 struct S' == struct S
2017 typedef struct S' MY_TYPE;
2018 struct S' object;
2019
2020 And they can then deduce (from the node for type struct S') that
2021 the original object declaration was:
2022
2023 MY_TYPE object;
2024
2025 Being able to do this is important for proper support of protoize,
2026 and also for generating precise symbolic debugging information
2027 which takes full account of the programmer's (typedef) vocabulary.
2028
2029 Obviously, we don't want to generate a duplicate ..._TYPE node if
2030 the TYPE_DECL node that we are now processing really represents a
f9837879 2031 standard built-in type. */
339a28b9
ZW
2032
2033static void
35b1a6fa 2034clone_underlying_type (tree x)
339a28b9 2035{
3c20847b 2036 if (DECL_IS_BUILTIN (x))
26f943fd 2037 {
339a28b9
ZW
2038 if (TYPE_NAME (TREE_TYPE (x)) == 0)
2039 TYPE_NAME (TREE_TYPE (x)) = x;
2040 }
2041 else if (TREE_TYPE (x) != error_mark_node
2042 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
2043 {
2044 tree tt = TREE_TYPE (x);
2045 DECL_ORIGINAL_TYPE (x) = tt;
8dd16ecc 2046 tt = build_variant_type_copy (tt);
339a28b9
ZW
2047 TYPE_NAME (tt) = x;
2048 TREE_USED (tt) = TREE_USED (x);
2049 TREE_TYPE (x) = tt;
26f943fd
NB
2050 }
2051}
2052
51e29401
RS
2053/* Record a decl-node X as belonging to the current lexical scope.
2054 Check for errors (such as an incompatible declaration for the same
2055 name already seen in the same scope).
2056
2057 Returns either X or an old decl for the same name.
2058 If an old decl is returned, it may have been smashed
2059 to agree with what X says. */
2060
2061tree
35b1a6fa 2062pushdecl (tree x)
51e29401 2063{
b3694847 2064 tree name = DECL_NAME (x);
f8521984 2065 struct c_scope *scope = current_scope;
f75fbaf7 2066 struct c_binding *b;
9aaabf8a 2067 bool nested = false;
51e29401 2068
eecec698
ZW
2069 /* Must set DECL_CONTEXT for everything not at file scope or
2070 DECL_FILE_SCOPE_P won't work. Local externs don't count
2071 unless they have initializers (which generate code). */
2072 if (current_function_decl
2073 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
2074 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
339a28b9 2075 DECL_CONTEXT (x) = current_function_decl;
e13e48e7 2076
187230a7
JM
2077 /* If this is of variably modified type, prevent jumping into its
2078 scope. */
2079 if ((TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == TYPE_DECL)
2080 && variably_modified_type_p (TREE_TYPE (x), NULL_TREE))
2081 c_begin_vm_scope (scope->depth);
2082
f75fbaf7
ZW
2083 /* Anonymous decls are just inserted in the scope. */
2084 if (!name)
51e29401 2085 {
9aaabf8a 2086 bind (name, x, scope, /*invisible=*/false, /*nested=*/false);
f75fbaf7
ZW
2087 return x;
2088 }
2089
2090 /* First, see if there is another declaration with the same name in
2091 the current scope. If there is, duplicate_decls may do all the
2092 work for us. If duplicate_decls returns false, that indicates
2093 two incompatible decls in the same scope; we are to silently
2094 replace the old one (duplicate_decls has issued all appropriate
2095 diagnostics). In particular, we should not consider possible
2096 duplicates in the external scope, or shadowing. */
2097 b = I_SYMBOL_BINDING (name);
9aaabf8a 2098 if (b && B_IN_SCOPE (b, scope))
f75fbaf7 2099 {
2798c11f
JM
2100 struct c_binding *b_ext, *b_use;
2101 tree type = TREE_TYPE (x);
2102 tree visdecl = b->decl;
2103 tree vistype = TREE_TYPE (visdecl);
0b410f0b
JM
2104 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2105 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2106 b->inner_comp = false;
2798c11f
JM
2107 b_use = b;
2108 b_ext = b;
2109 /* If this is an external linkage declaration, we should check
2110 for compatibility with the type in the external scope before
2111 setting the type at this scope based on the visible
2112 information only. */
2113 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2114 {
2115 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2116 b_ext = b_ext->shadowed;
2117 if (b_ext)
2118 {
2119 b_use = b_ext;
2120 if (b_use->type)
2121 TREE_TYPE (b_use->decl) = b_use->type;
2122 }
2123 }
2124 if (duplicate_decls (x, b_use->decl))
2125 {
2126 if (b_use != b)
2127 {
2128 /* Save the updated type in the external scope and
2129 restore the proper type for this scope. */
2130 tree thistype;
2131 if (comptypes (vistype, type))
2132 thistype = composite_type (vistype, type);
2133 else
2134 thistype = TREE_TYPE (b_use->decl);
2135 b_use->type = TREE_TYPE (b_use->decl);
2136 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2137 && DECL_BUILT_IN (b_use->decl))
2138 thistype
2139 = build_type_attribute_variant (thistype,
2140 TYPE_ATTRIBUTES
2141 (b_use->type));
2142 TREE_TYPE (b_use->decl) = thistype;
2143 }
2144 return b_use->decl;
2145 }
f75fbaf7
ZW
2146 else
2147 goto skip_external_and_shadow_checks;
2148 }
2149
2150 /* All declarations with external linkage, and all external
2151 references, go in the external scope, no matter what scope is
2152 current. However, the binding in that scope is ignored for
2153 purposes of normal name lookup. A separate binding structure is
2154 created in the requested scope; this governs the normal
2155 visibility of the symbol.
51e29401 2156
f75fbaf7
ZW
2157 The binding in the externals scope is used exclusively for
2158 detecting duplicate declarations of the same object, no matter
2159 what scope they are in; this is what we do here. (C99 6.2.7p2:
2160 All declarations that refer to the same object or function shall
2161 have compatible type; otherwise, the behavior is undefined.) */
2162 if (DECL_EXTERNAL (x) || scope == file_scope)
2163 {
0b410f0b
JM
2164 tree type = TREE_TYPE (x);
2165 tree vistype = 0;
2166 tree visdecl = 0;
2167 bool type_saved = false;
2168 if (b && !B_IN_EXTERNAL_SCOPE (b)
2169 && (TREE_CODE (b->decl) == FUNCTION_DECL
2170 || TREE_CODE (b->decl) == VAR_DECL)
2171 && DECL_FILE_SCOPE_P (b->decl))
2172 {
2173 visdecl = b->decl;
2174 vistype = TREE_TYPE (visdecl);
2175 }
3176a0c2 2176 if (scope != file_scope
234f46ae 2177 && !DECL_IN_SYSTEM_HEADER (x))
3176a0c2 2178 warning (OPT_Wnested_externs, "nested extern declaration of %qD", x);
234f46ae 2179
9aaabf8a 2180 while (b && !B_IN_EXTERNAL_SCOPE (b))
0b410f0b
JM
2181 {
2182 /* If this decl might be modified, save its type. This is
2183 done here rather than when the decl is first bound
2184 because the type may change after first binding, through
2185 being completed or through attributes being added. If we
2186 encounter multiple such decls, only the first should have
2187 its type saved; the others will already have had their
2188 proper types saved and the types will not have changed as
2189 their scopes will not have been re-entered. */
d8dd2f3a 2190 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
0b410f0b
JM
2191 {
2192 b->type = TREE_TYPE (b->decl);
2193 type_saved = true;
2194 }
2195 if (B_IN_FILE_SCOPE (b)
2196 && TREE_CODE (b->decl) == VAR_DECL
2197 && TREE_STATIC (b->decl)
2198 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2199 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2200 && TREE_CODE (type) == ARRAY_TYPE
2201 && TYPE_DOMAIN (type)
2202 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2203 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2204 {
2205 /* Array type completed in inner scope, which should be
2206 diagnosed if the completion does not have size 1 and
2207 it does not get completed in the file scope. */
2208 b->inner_comp = true;
2209 }
2210 b = b->shadowed;
2211 }
2212
2213 /* If a matching external declaration has been found, set its
2214 type to the composite of all the types of that declaration.
2215 After the consistency checks, it will be reset to the
2216 composite of the visible types only. */
2217 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2218 && b->type)
2219 TREE_TYPE (b->decl) = b->type;
f75fbaf7
ZW
2220
2221 /* The point of the same_translation_unit_p check here is,
2222 we want to detect a duplicate decl for a construct like
2223 foo() { extern bar(); } ... static bar(); but not if
2224 they are in different translation units. In any case,
2225 the static does not go in the externals scope. */
2226 if (b
9affb2c7 2227 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
f75fbaf7 2228 && duplicate_decls (x, b->decl))
55d54003 2229 {
0b410f0b 2230 tree thistype;
2798c11f
JM
2231 if (vistype)
2232 {
2233 if (comptypes (vistype, type))
2234 thistype = composite_type (vistype, type);
2235 else
2236 thistype = TREE_TYPE (b->decl);
2237 }
2238 else
2239 thistype = type;
0b410f0b
JM
2240 b->type = TREE_TYPE (b->decl);
2241 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2242 thistype
2243 = build_type_attribute_variant (thistype,
2244 TYPE_ATTRIBUTES (b->type));
2245 TREE_TYPE (b->decl) = thistype;
9aaabf8a 2246 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true);
f75fbaf7 2247 return b->decl;
55d54003 2248 }
9affb2c7 2249 else if (TREE_PUBLIC (x))
6645c3fa 2250 {
0b410f0b
JM
2251 if (visdecl && !b && duplicate_decls (x, visdecl))
2252 {
2253 /* An external declaration at block scope referring to a
2254 visible entity with internal linkage. The composite
2255 type will already be correct for this scope, so we
2256 just need to fall through to make the declaration in
2257 this scope. */
2258 nested = true;
1b36c818 2259 x = visdecl;
0b410f0b
JM
2260 }
2261 else
2262 {
2263 bind (name, x, external_scope, /*invisible=*/true,
2264 /*nested=*/false);
2265 nested = true;
2266 }
51e29401 2267 }
f75fbaf7 2268 }
35b1a6fa 2269
7c47d6e9
JM
2270 if (TREE_CODE (x) != PARM_DECL)
2271 warn_if_shadowing (x);
51e29401 2272
f75fbaf7
ZW
2273 skip_external_and_shadow_checks:
2274 if (TREE_CODE (x) == TYPE_DECL)
2275 clone_underlying_type (x);
51e29401 2276
9aaabf8a 2277 bind (name, x, scope, /*invisible=*/false, nested);
f75fbaf7
ZW
2278
2279 /* If x's type is incomplete because it's based on a
2280 structure or union which has not yet been fully declared,
2281 attach it to that structure or union type, so we can go
2282 back and complete the variable declaration later, if the
2283 structure or union gets fully declared.
51e29401 2284
f75fbaf7
ZW
2285 If the input is erroneous, we can have error_mark in the type
2286 slot (e.g. "f(void a, ...)") - that doesn't count as an
2287 incomplete type. */
2288 if (TREE_TYPE (x) != error_mark_node
2289 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2290 {
2291 tree element = TREE_TYPE (x);
2292
2293 while (TREE_CODE (element) == ARRAY_TYPE)
2294 element = TREE_TYPE (element);
2295 element = TYPE_MAIN_VARIANT (element);
2296
2297 if ((TREE_CODE (element) == RECORD_TYPE
2298 || TREE_CODE (element) == UNION_TYPE)
2299 && (TREE_CODE (x) != TYPE_DECL
2300 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
2301 && !COMPLETE_TYPE_P (element))
2302 C_TYPE_INCOMPLETE_VARS (element)
2303 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
2304 }
51e29401
RS
2305 return x;
2306}
2307
f75fbaf7 2308/* Record X as belonging to file scope.
6356f892 2309 This is used only internally by the Objective-C front end,
f91f41b2
ZW
2310 and is limited to its needs. duplicate_decls is not called;
2311 if there is any preexisting decl for this identifier, it is an ICE. */
2312
339a28b9 2313tree
35b1a6fa 2314pushdecl_top_level (tree x)
6970c06a 2315{
f91f41b2 2316 tree name;
9aaabf8a 2317 bool nested = false;
943db347 2318 gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
339a28b9
ZW
2319
2320 name = DECL_NAME (x);
339a28b9 2321
943db347 2322 gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
6970c06a 2323
9affb2c7 2324 if (TREE_PUBLIC (x))
f75fbaf7 2325 {
9aaabf8a
ZW
2326 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false);
2327 nested = true;
f75fbaf7
ZW
2328 }
2329 if (file_scope)
9aaabf8a 2330 bind (name, x, file_scope, /*invisible=*/false, nested);
51e29401 2331
f91f41b2 2332 return x;
51e29401
RS
2333}
2334\f
f75fbaf7
ZW
2335static void
2336implicit_decl_warning (tree id, tree olddecl)
2337{
dc90f45b 2338 if (warn_implicit_function_declaration)
f75fbaf7 2339 {
dc90f45b 2340 if (flag_isoc99)
fcf73884
MLI
2341 pedwarn (OPT_Wimplicit_function_declaration,
2342 G_("implicit declaration of function %qE"), id);
dc90f45b
MLI
2343 else
2344 warning (OPT_Wimplicit_function_declaration,
2345 G_("implicit declaration of function %qE"), id);
2346 if (olddecl)
2347 locate_old_decl (olddecl, inform);
f75fbaf7 2348 }
f75fbaf7
ZW
2349}
2350
339a28b9
ZW
2351/* Generate an implicit declaration for identifier FUNCTIONID as a
2352 function of type int (). */
51e29401
RS
2353
2354tree
35b1a6fa 2355implicitly_declare (tree functionid)
51e29401 2356{
0b410f0b
JM
2357 struct c_binding *b;
2358 tree decl = 0;
1ae57298
MM
2359 tree asmspec_tree;
2360
0b410f0b
JM
2361 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
2362 {
2363 if (B_IN_SCOPE (b, external_scope))
2364 {
2365 decl = b->decl;
2366 break;
2367 }
2368 }
51e29401 2369
f91f41b2 2370 if (decl)
339a28b9 2371 {
d8dd2f3a
JJ
2372 if (decl == error_mark_node)
2373 return decl;
2374
f75fbaf7
ZW
2375 /* FIXME: Objective-C has weird not-really-builtin functions
2376 which are supposed to be visible automatically. They wind up
2377 in the external scope because they're pushed before the file
2378 scope gets created. Catch this here and rebind them into the
2379 file scope. */
3c20847b 2380 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
f75fbaf7 2381 {
9aaabf8a
ZW
2382 bind (functionid, decl, file_scope,
2383 /*invisible=*/false, /*nested=*/true);
f75fbaf7
ZW
2384 return decl;
2385 }
2386 else
339a28b9 2387 {
0b410f0b
JM
2388 tree newtype = default_function_type;
2389 if (b->type)
2390 TREE_TYPE (decl) = b->type;
f75fbaf7
ZW
2391 /* Implicit declaration of a function already declared
2392 (somehow) in a different scope, or as a built-in.
2393 If this is the first time this has happened, warn;
0b410f0b 2394 then recycle the old declaration but with the new type. */
f75fbaf7
ZW
2395 if (!C_DECL_IMPLICIT (decl))
2396 {
2397 implicit_decl_warning (functionid, decl);
2398 C_DECL_IMPLICIT (decl) = 1;
2399 }
9cbe78fb
JM
2400 if (DECL_BUILT_IN (decl))
2401 {
0b410f0b
JM
2402 newtype = build_type_attribute_variant (newtype,
2403 TYPE_ATTRIBUTES
2404 (TREE_TYPE (decl)));
2405 if (!comptypes (newtype, TREE_TYPE (decl)))
9cbe78fb 2406 {
d4ee4d25 2407 warning (0, "incompatible implicit declaration of built-in"
9cbe78fb 2408 " function %qD", decl);
0b410f0b 2409 newtype = TREE_TYPE (decl);
9cbe78fb
JM
2410 }
2411 }
2412 else
2413 {
0b410f0b 2414 if (!comptypes (newtype, TREE_TYPE (decl)))
9cbe78fb
JM
2415 {
2416 error ("incompatible implicit declaration of function %qD",
2417 decl);
2418 locate_old_decl (decl, error);
2419 }
2420 }
0b410f0b
JM
2421 b->type = TREE_TYPE (decl);
2422 TREE_TYPE (decl) = newtype;
9aaabf8a
ZW
2423 bind (functionid, decl, current_scope,
2424 /*invisible=*/false, /*nested=*/true);
f75fbaf7 2425 return decl;
339a28b9 2426 }
339a28b9 2427 }
51e29401 2428
339a28b9
ZW
2429 /* Not seen before. */
2430 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
1394aabd 2431 DECL_EXTERNAL (decl) = 1;
51e29401 2432 TREE_PUBLIC (decl) = 1;
339a28b9 2433 C_DECL_IMPLICIT (decl) = 1;
f75fbaf7 2434 implicit_decl_warning (functionid, 0);
1ae57298
MM
2435 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
2436 if (asmspec_tree)
2437 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
51e29401 2438
14077d68 2439 /* C89 says implicit declarations are in the innermost block.
f458d1d5 2440 So we record the decl in the standard fashion. */
339a28b9 2441 decl = pushdecl (decl);
51e29401 2442
339a28b9 2443 /* No need to call objc_check_decl here - it's a function type. */
0e6df31e 2444 rest_of_decl_compilation (decl, 0, 0);
51e29401 2445
14077d68
ZW
2446 /* Write a record describing this implicit function declaration
2447 to the prototypes file (if requested). */
51e29401
RS
2448 gen_aux_info_record (decl, 0, 1, 0);
2449
6431177a
JM
2450 /* Possibly apply some default attributes to this implicit declaration. */
2451 decl_attributes (&decl, NULL_TREE, 0);
2452
51e29401
RS
2453 return decl;
2454}
2455
9cd51ef6
ZW
2456/* Issue an error message for a reference to an undeclared variable
2457 ID, including a reference to a builtin outside of function-call
2458 context. Establish a binding of the identifier to error_mark_node
2459 in an appropriate scope, which will suppress further errors for the
766beb40 2460 same identifier. The error message should be given location LOC. */
9cd51ef6 2461void
766beb40 2462undeclared_variable (tree id, location_t loc)
9cd51ef6
ZW
2463{
2464 static bool already = false;
f91f41b2 2465 struct c_scope *scope;
9cd51ef6
ZW
2466
2467 if (current_function_decl == 0)
2468 {
766beb40 2469 error ("%H%qE undeclared here (not in a function)", &loc, id);
f91f41b2 2470 scope = current_scope;
9cd51ef6
ZW
2471 }
2472 else
2473 {
766beb40 2474 error ("%H%qE undeclared (first use in this function)", &loc, id);
9cd51ef6 2475
3f75a254 2476 if (!already)
9cd51ef6 2477 {
766beb40
JM
2478 error ("%H(Each undeclared identifier is reported only once", &loc);
2479 error ("%Hfor each function it appears in.)", &loc);
9cd51ef6
ZW
2480 already = true;
2481 }
2482
f75fbaf7 2483 /* If we are parsing old-style parameter decls, current_function_decl
c22cacf3 2484 will be nonnull but current_function_scope will be null. */
f75fbaf7 2485 scope = current_function_scope ? current_function_scope : current_scope;
9cd51ef6 2486 }
9aaabf8a 2487 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false);
9cd51ef6 2488}
51e29401 2489\f
14e33ee8
ZW
2490/* Subroutine of lookup_label, declare_label, define_label: construct a
2491 LABEL_DECL with all the proper frills. */
2492
2493static tree
2494make_label (tree name, location_t location)
2495{
2496 tree label = build_decl (LABEL_DECL, name, void_type_node);
2497
2498 DECL_CONTEXT (label) = current_function_decl;
2499 DECL_MODE (label) = VOIDmode;
f31686a3 2500 DECL_SOURCE_LOCATION (label) = location;
14e33ee8
ZW
2501
2502 return label;
2503}
2504
14e33ee8 2505/* Get the LABEL_DECL corresponding to identifier NAME as a label.
51e29401 2506 Create one if none exists so far for the current function.
14e33ee8
ZW
2507 This is called when a label is used in a goto expression or
2508 has its address taken. */
51e29401
RS
2509
2510tree
14e33ee8 2511lookup_label (tree name)
51e29401 2512{
14e33ee8 2513 tree label;
51e29401 2514
7d9795e5
RS
2515 if (current_function_decl == 0)
2516 {
c51a1ba9 2517 error ("label %qE referenced outside of any function", name);
7d9795e5
RS
2518 return 0;
2519 }
2520
14e33ee8
ZW
2521 /* Use a label already defined or ref'd with this name, but not if
2522 it is inherited from a containing function and wasn't declared
2523 using __label__. */
f75fbaf7 2524 label = I_LABEL_DECL (name);
14e33ee8
ZW
2525 if (label && (DECL_CONTEXT (label) == current_function_decl
2526 || C_DECLARED_LABEL_FLAG (label)))
51e29401 2527 {
14e33ee8
ZW
2528 /* If the label has only been declared, update its apparent
2529 location to point here, for better diagnostics if it
2530 turns out not to have been defined. */
2531 if (!TREE_USED (label))
f31686a3 2532 DECL_SOURCE_LOCATION (label) = input_location;
14e33ee8 2533 return label;
51e29401
RS
2534 }
2535
14e33ee8
ZW
2536 /* No label binding for that identifier; make one. */
2537 label = make_label (name, input_location);
51e29401 2538
14077d68 2539 /* Ordinary labels go in the current function scope. */
9aaabf8a
ZW
2540 bind (name, label, current_function_scope,
2541 /*invisible=*/false, /*nested=*/false);
14e33ee8 2542 return label;
51e29401
RS
2543}
2544
14e33ee8
ZW
2545/* Make a label named NAME in the current function, shadowing silently
2546 any that may be inherited from containing functions or containing
2547 scopes. This is called for __label__ declarations. */
51e29401 2548
51e29401 2549tree
14e33ee8 2550declare_label (tree name)
51e29401 2551{
f75fbaf7
ZW
2552 struct c_binding *b = I_LABEL_BINDING (name);
2553 tree label;
51e29401 2554
14e33ee8
ZW
2555 /* Check to make sure that the label hasn't already been declared
2556 at this scope */
9aaabf8a 2557 if (b && B_IN_CURRENT_SCOPE (b))
f75fbaf7 2558 {
c51a1ba9 2559 error ("duplicate label declaration %qE", name);
f75fbaf7 2560 locate_old_decl (b->decl, error);
a784882b 2561
f75fbaf7
ZW
2562 /* Just use the previous declaration. */
2563 return b->decl;
2564 }
a784882b 2565
14e33ee8
ZW
2566 label = make_label (name, input_location);
2567 C_DECLARED_LABEL_FLAG (label) = 1;
51e29401 2568
14e33ee8 2569 /* Declared labels go in the current scope. */
9aaabf8a
ZW
2570 bind (name, label, current_scope,
2571 /*invisible=*/false, /*nested=*/false);
14e33ee8 2572 return label;
51e29401
RS
2573}
2574
2575/* Define a label, specifying the location in the source file.
2576 Return the LABEL_DECL node for the label, if the definition is valid.
2577 Otherwise return 0. */
2578
2579tree
5b030314 2580define_label (location_t location, tree name)
51e29401 2581{
14e33ee8
ZW
2582 /* Find any preexisting label with this name. It is an error
2583 if that label has already been defined in this function, or
2584 if there is a containing function with a declared label with
2585 the same name. */
f75fbaf7 2586 tree label = I_LABEL_DECL (name);
187230a7 2587 struct c_label_list *nlist_se, *nlist_vm;
14e33ee8
ZW
2588
2589 if (label
2590 && ((DECL_CONTEXT (label) == current_function_decl
2591 && DECL_INITIAL (label) != 0)
2592 || (DECL_CONTEXT (label) != current_function_decl
2593 && C_DECLARED_LABEL_FLAG (label))))
51e29401 2594 {
bda67431 2595 error ("%Hduplicate label %qD", &location, label);
f75fbaf7 2596 locate_old_decl (label, error);
14e33ee8 2597 return 0;
51e29401 2598 }
14e33ee8 2599 else if (label && DECL_CONTEXT (label) == current_function_decl)
51e29401 2600 {
14e33ee8
ZW
2601 /* The label has been used or declared already in this function,
2602 but not defined. Update its location to point to this
2603 definition. */
16ef3acc
JM
2604 if (C_DECL_UNDEFINABLE_STMT_EXPR (label))
2605 error ("%Jjump into statement expression", label);
187230a7
JM
2606 if (C_DECL_UNDEFINABLE_VM (label))
2607 error ("%Jjump into scope of identifier with variably modified type",
2608 label);
f31686a3 2609 DECL_SOURCE_LOCATION (label) = location;
51e29401
RS
2610 }
2611 else
2612 {
14e33ee8
ZW
2613 /* No label binding for that identifier; make one. */
2614 label = make_label (name, location);
2615
14077d68 2616 /* Ordinary labels go in the current function scope. */
9aaabf8a
ZW
2617 bind (name, label, current_function_scope,
2618 /*invisible=*/false, /*nested=*/false);
51e29401 2619 }
14e33ee8 2620
44c21c7f
DD
2621 if (!in_system_header && lookup_name (name))
2622 warning (OPT_Wtraditional, "%Htraditional C lacks a separate namespace "
c22cacf3 2623 "for labels, identifier %qE conflicts", &location, name);
14e33ee8 2624
187230a7
JM
2625 nlist_se = XOBNEW (&parser_obstack, struct c_label_list);
2626 nlist_se->next = label_context_stack_se->labels_def;
2627 nlist_se->label = label;
2628 label_context_stack_se->labels_def = nlist_se;
2629
2630 nlist_vm = XOBNEW (&parser_obstack, struct c_label_list);
2631 nlist_vm->next = label_context_stack_vm->labels_def;
2632 nlist_vm->label = label;
2633 label_context_stack_vm->labels_def = nlist_vm;
16ef3acc 2634
14e33ee8
ZW
2635 /* Mark label as having been defined. */
2636 DECL_INITIAL (label) = error_mark_node;
2637 return label;
51e29401 2638}
51e29401
RS
2639\f
2640/* Given NAME, an IDENTIFIER_NODE,
2641 return the structure (or union or enum) definition for that name.
f8521984 2642 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
51e29401
RS
2643 CODE says which kind of type the caller wants;
2644 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2645 If the wrong kind of type is found, an error is reported. */
2646
2647static tree
35b1a6fa 2648lookup_tag (enum tree_code code, tree name, int thislevel_only)
51e29401 2649{
f75fbaf7 2650 struct c_binding *b = I_TAG_BINDING (name);
339a28b9 2651 int thislevel = 0;
51e29401 2652
f75fbaf7 2653 if (!b || !b->decl)
339a28b9
ZW
2654 return 0;
2655
2656 /* We only care about whether it's in this level if
2657 thislevel_only was set or it might be a type clash. */
f75fbaf7
ZW
2658 if (thislevel_only || TREE_CODE (b->decl) != code)
2659 {
2660 /* For our purposes, a tag in the external scope is the same as
2661 a tag in the file scope. (Primarily relevant to Objective-C
2662 and its builtin structure tags, which get pushed before the
118a3a8b 2663 file scope is created.) */
9aaabf8a
ZW
2664 if (B_IN_CURRENT_SCOPE (b)
2665 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
339a28b9 2666 thislevel = 1;
51e29401 2667 }
339a28b9
ZW
2668
2669 if (thislevel_only && !thislevel)
2670 return 0;
2671
f75fbaf7 2672 if (TREE_CODE (b->decl) != code)
339a28b9
ZW
2673 {
2674 /* Definition isn't the kind we were looking for. */
2675 pending_invalid_xref = name;
070588f0 2676 pending_invalid_xref_location = input_location;
339a28b9
ZW
2677
2678 /* If in the same binding level as a declaration as a tag
2679 of a different type, this must not be allowed to
2680 shadow that tag, so give the error immediately.
2681 (For example, "struct foo; union foo;" is invalid.) */
2682 if (thislevel)
2683 pending_xref_error ();
2684 }
f75fbaf7 2685 return b->decl;
51e29401
RS
2686}
2687
2688/* Print an error message now
2689 for a recent invalid struct, union or enum cross reference.
2690 We don't print them immediately because they are not invalid
2691 when used in the `struct foo;' construct for shadowing. */
2692
2693void
35b1a6fa 2694pending_xref_error (void)
51e29401
RS
2695{
2696 if (pending_invalid_xref != 0)
c51a1ba9 2697 error ("%H%qE defined as wrong kind of tag",
c22cacf3 2698 &pending_invalid_xref_location, pending_invalid_xref);
51e29401
RS
2699 pending_invalid_xref = 0;
2700}
2701
51e29401 2702\f
f8521984 2703/* Look up NAME in the current scope and its superiors
51e29401
RS
2704 in the namespace of variables, functions and typedefs.
2705 Return a ..._DECL node of some kind representing its definition,
2706 or return 0 if it is undefined. */
2707
2708tree
35b1a6fa 2709lookup_name (tree name)
51e29401 2710{
f75fbaf7 2711 struct c_binding *b = I_SYMBOL_BINDING (name);
9aaabf8a 2712 if (b && !b->invisible)
f75fbaf7
ZW
2713 return b->decl;
2714 return 0;
51e29401
RS
2715}
2716
f75fbaf7 2717/* Similar to `lookup_name' but look only at the indicated scope. */
51e29401 2718
339a28b9 2719static tree
f75fbaf7 2720lookup_name_in_scope (tree name, struct c_scope *scope)
51e29401 2721{
f75fbaf7 2722 struct c_binding *b;
339a28b9 2723
f75fbaf7 2724 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
9aaabf8a 2725 if (B_IN_SCOPE (b, scope))
f75fbaf7 2726 return b->decl;
339a28b9 2727 return 0;
51e29401
RS
2728}
2729\f
2730/* Create the predefined scalar types of C,
0f41302f 2731 and some nodes representing standard constants (0, 1, (void *) 0).
f8521984 2732 Initialize the global scope.
51e29401
RS
2733 Make definitions for built-in primitive functions. */
2734
2735void
35b1a6fa 2736c_init_decl_processing (void)
51e29401 2737{
4714db5a 2738 location_t save_loc = input_location;
e13e48e7 2739
27bf414c 2740 /* Initialize reserved words for parser. */
f5e99456
NB
2741 c_parse_init ();
2742
14077d68 2743 current_function_decl = 0;
6645c3fa 2744
bc4b653b
JM
2745 gcc_obstack_init (&parser_obstack);
2746
f75fbaf7
ZW
2747 /* Make the externals scope. */
2748 push_scope ();
2749 external_scope = current_scope;
14077d68 2750
e3091a5f
R
2751 /* Declarations from c_common_nodes_and_builtins must not be associated
2752 with this input file, lest we get differences between using and not
2753 using preprocessed headers. */
3c20847b 2754 input_location = BUILTINS_LOCATION;
51e29401 2755
8c1d6d62 2756 build_common_tree_nodes (flag_signed_char, false);
51e29401 2757
eaa7c03f 2758 c_common_nodes_and_builtins ();
51e29401 2759
de7df9eb
JM
2760 /* In C, comparisons and TRUTH_* expressions have type int. */
2761 truthvalue_type_node = integer_type_node;
2762 truthvalue_true_node = integer_one_node;
2763 truthvalue_false_node = integer_zero_node;
2764
2765 /* Even in C99, which has a real boolean type. */
19552aa5 2766 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
de7df9eb 2767 boolean_type_node));
19552aa5 2768
4714db5a 2769 input_location = save_loc;
e3091a5f 2770
53cd18ec 2771 pedantic_lvalues = true;
f444e553 2772
2ce07e2d 2773 make_fname_decl = c_make_fname_decl;
0ba8a114 2774 start_fname_decls ();
51e29401
RS
2775}
2776
2ce07e2d
NS
2777/* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2778 decl, NAME is the initialization string and TYPE_DEP indicates whether
2779 NAME depended on the type of the function. As we don't yet implement
2780 delayed emission of static data, we mark the decl as emitted
2781 so it is not placed in the output. Anything using it must therefore pull
f91f41b2 2782 out the STRING_CST initializer directly. FIXME. */
2ce07e2d
NS
2783
2784static tree
35b1a6fa 2785c_make_fname_decl (tree id, int type_dep)
2ce07e2d 2786{
0ba8a114 2787 const char *name = fname_as_string (type_dep);
2ce07e2d
NS
2788 tree decl, type, init;
2789 size_t length = strlen (name);
2790
46df2823
JM
2791 type = build_array_type (char_type_node,
2792 build_index_type (size_int (length)));
2793 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
2ce07e2d
NS
2794
2795 decl = build_decl (VAR_DECL, id, type);
35b1a6fa 2796
2ce07e2d
NS
2797 TREE_STATIC (decl) = 1;
2798 TREE_READONLY (decl) = 1;
2ce07e2d 2799 DECL_ARTIFICIAL (decl) = 1;
35b1a6fa 2800
2ce07e2d 2801 init = build_string (length + 1, name);
b1d5455a 2802 free (CONST_CAST (char *, name));
2ce07e2d
NS
2803 TREE_TYPE (init) = type;
2804 DECL_INITIAL (decl) = init;
0ba8a114
NS
2805
2806 TREE_USED (decl) = 1;
6cce57b0 2807
1a4259dc
JH
2808 if (current_function_decl
2809 /* For invalid programs like this:
2810
2811 void foo()
2812 const char* p = __FUNCTION__;
2813
2814 the __FUNCTION__ is believed to appear in K&R style function
2815 parameter declarator. In that case we still don't have
2816 function_scope. */
2817 && (!errorcount || current_function_scope))
f91f41b2
ZW
2818 {
2819 DECL_CONTEXT (decl) = current_function_decl;
9aaabf8a
ZW
2820 bind (id, decl, current_function_scope,
2821 /*invisible=*/false, /*nested=*/false);
f91f41b2 2822 }
339a28b9 2823
0ba8a114 2824 finish_decl (decl, init, NULL_TREE);
6645c3fa 2825
2ce07e2d
NS
2826 return decl;
2827}
2828
929f3671 2829tree
c79efc4d 2830c_builtin_function (tree decl)
51e29401 2831{
c79efc4d
RÁE
2832 tree type = TREE_TYPE (decl);
2833 tree id = DECL_NAME (decl);
2834
2835 const char *name = IDENTIFIER_POINTER (id);
6fb58bba 2836 C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0);
26db82d8 2837
f75fbaf7 2838 /* Should never be called on a symbol with a preexisting meaning. */
366de0ce 2839 gcc_assert (!I_SYMBOL_BINDING (id));
f75fbaf7 2840
9aaabf8a 2841 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false);
f75fbaf7
ZW
2842
2843 /* Builtins in the implementation namespace are made visible without
2844 needing to be explicitly declared. See push_file_scope. */
2845 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2846 {
2847 TREE_CHAIN (decl) = visible_builtins;
2848 visible_builtins = decl;
2849 }
51e29401
RS
2850
2851 return decl;
2852}
2853\f
2854/* Called when a declaration is seen that contains no names to declare.
2855 If its type is a reference to a structure, union or enum inherited
2856 from a containing scope, shadow that tag name for the current scope
2857 with a forward reference.
2858 If its type defines a new named structure or union
2859 or defines an enum, it is valid but we need not do anything here.
2860 Otherwise, it is an error. */
2861
2862void
deb176fa 2863shadow_tag (const struct c_declspecs *declspecs)
9282f2f9
RS
2864{
2865 shadow_tag_warned (declspecs, 0);
2866}
2867
0e6df31e
GK
2868/* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
2869 but no pedwarn. */
9282f2f9 2870void
deb176fa 2871shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
51e29401 2872{
deb176fa 2873 bool found_tag = false;
51e29401 2874
98c3a782 2875 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
51e29401 2876 {
deb176fa 2877 tree value = declspecs->type;
b3694847 2878 enum tree_code code = TREE_CODE (value);
51e29401
RS
2879
2880 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2881 /* Used to test also that TYPE_SIZE (value) != 0.
2882 That caused warning for `struct foo;' at top level in the file. */
2883 {
339a28b9 2884 tree name = TYPE_NAME (value);
b3694847 2885 tree t;
51e29401 2886
deb176fa 2887 found_tag = true;
51e29401
RS
2888
2889 if (name == 0)
2890 {
773edaef
RK
2891 if (warned != 1 && code != ENUMERAL_TYPE)
2892 /* Empty unnamed enum OK */
51e29401 2893 {
fcf73884 2894 pedwarn (0, "unnamed struct/union that defines no instances");
51e29401
RS
2895 warned = 1;
2896 }
2897 }
81da229b
JM
2898 else if (!declspecs->tag_defined_p
2899 && declspecs->storage_class != csc_none)
2900 {
2901 if (warned != 1)
fcf73884
MLI
2902 pedwarn (0, "empty declaration with storage class specifier "
2903 "does not redeclare tag");
81da229b
JM
2904 warned = 1;
2905 pending_xref_error ();
2906 }
2907 else if (!declspecs->tag_defined_p
2908 && (declspecs->const_p
2909 || declspecs->volatile_p
2910 || declspecs->restrict_p))
2911 {
2912 if (warned != 1)
fcf73884
MLI
2913 pedwarn (0, "empty declaration with type qualifier "
2914 "does not redeclare tag");
81da229b
JM
2915 warned = 1;
2916 pending_xref_error ();
2917 }
51e29401
RS
2918 else
2919 {
81da229b 2920 pending_invalid_xref = 0;
339a28b9 2921 t = lookup_tag (code, name, 1);
51e29401
RS
2922
2923 if (t == 0)
2924 {
2925 t = make_node (code);
2926 pushtag (name, t);
2927 }
2928 }
2929 }
2930 else
2931 {
deb176fa 2932 if (warned != 1 && !in_system_header)
773edaef 2933 {
fcf73884 2934 pedwarn (0, "useless type name in empty declaration");
deb176fa 2935 warned = 1;
773edaef 2936 }
51e29401
RS
2937 }
2938 }
616aeba2 2939 else if (warned != 1 && !in_system_header && declspecs->typedef_p)
deb176fa 2940 {
fcf73884 2941 pedwarn (0, "useless type name in empty declaration");
deb176fa
JM
2942 warned = 1;
2943 }
2944
81da229b
JM
2945 pending_invalid_xref = 0;
2946
9a26d6ee
JM
2947 if (declspecs->inline_p)
2948 {
2949 error ("%<inline%> in empty declaration");
2950 warned = 1;
2951 }
2952
2953 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
2954 {
2955 error ("%<auto%> in file-scope empty declaration");
2956 warned = 1;
2957 }
2958
2959 if (current_scope == file_scope && declspecs->storage_class == csc_register)
2960 {
2961 error ("%<register%> in file-scope empty declaration");
2962 warned = 1;
2963 }
2964
2965 if (!warned && !in_system_header && declspecs->storage_class != csc_none)
2966 {
d4ee4d25 2967 warning (0, "useless storage class specifier in empty declaration");
9a26d6ee
JM
2968 warned = 2;
2969 }
2970
2971 if (!warned && !in_system_header && declspecs->thread_p)
2972 {
d4ee4d25 2973 warning (0, "useless %<__thread%> in empty declaration");
9a26d6ee
JM
2974 warned = 2;
2975 }
2976
3b53cddc
JM
2977 if (!warned && !in_system_header && (declspecs->const_p
2978 || declspecs->volatile_p
2979 || declspecs->restrict_p))
2980 {
d4ee4d25 2981 warning (0, "useless type qualifier in empty declaration");
3b53cddc
JM
2982 warned = 2;
2983 }
2984
773edaef 2985 if (warned != 1)
51e29401 2986 {
deb176fa 2987 if (!found_tag)
fcf73884 2988 pedwarn (0, "empty declaration");
51e29401
RS
2989 }
2990}
2991\f
3b53cddc
JM
2992
2993/* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
2994 bits. SPECS represents declaration specifiers that the grammar
2995 only permits to contain type qualifiers and attributes. */
2996
2997int
2998quals_from_declspecs (const struct c_declspecs *specs)
2999{
3000 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
3001 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
3002 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0));
3003 gcc_assert (!specs->type
3004 && !specs->decl_attr
98c3a782 3005 && specs->typespec_word == cts_none
3b53cddc
JM
3006 && specs->storage_class == csc_none
3007 && !specs->typedef_p
98c3a782 3008 && !specs->explicit_signed_p
3b53cddc 3009 && !specs->deprecated_p
98c3a782 3010 && !specs->long_p
3b53cddc 3011 && !specs->long_long_p
98c3a782
JM
3012 && !specs->short_p
3013 && !specs->signed_p
3014 && !specs->unsigned_p
3015 && !specs->complex_p
3b53cddc
JM
3016 && !specs->inline_p
3017 && !specs->thread_p);
3018 return quals;
3019}
3020
52ffd86e
MS
3021/* Construct an array declarator. EXPR is the expression inside [],
3022 or NULL_TREE. QUALS are the type qualifiers inside the [] (to be
3023 applied to the pointer to which a parameter array is converted).
3024 STATIC_P is true if "static" is inside the [], false otherwise.
3025 VLA_UNSPEC_P is true if the array is [*], a VLA of unspecified
3026 length which is nevertheless a complete type, false otherwise. The
3027 field for the contained declarator is left to be filled in by
3028 set_array_declarator_inner. */
0e03329a 3029
f8893e47 3030struct c_declarator *
deb176fa 3031build_array_declarator (tree expr, struct c_declspecs *quals, bool static_p,
60919bce 3032 bool vla_unspec_p)
0e03329a 3033{
f8893e47
JM
3034 struct c_declarator *declarator = XOBNEW (&parser_obstack,
3035 struct c_declarator);
3036 declarator->kind = cdk_array;
3037 declarator->declarator = 0;
3038 declarator->u.array.dimen = expr;
3b53cddc
JM
3039 if (quals)
3040 {
3041 declarator->u.array.attrs = quals->attrs;
3042 declarator->u.array.quals = quals_from_declspecs (quals);
3043 }
3044 else
3045 {
3046 declarator->u.array.attrs = NULL_TREE;
3047 declarator->u.array.quals = 0;
3048 }
f8893e47
JM
3049 declarator->u.array.static_p = static_p;
3050 declarator->u.array.vla_unspec_p = vla_unspec_p;
fcf73884 3051 if (!flag_isoc99)
0e03329a 3052 {
deb176fa 3053 if (static_p || quals != NULL)
fcf73884 3054 pedwarn (OPT_pedantic, "ISO C90 does not support %<static%> or type "
bda67431 3055 "qualifiers in parameter array declarators");
0e03329a 3056 if (vla_unspec_p)
fcf73884 3057 pedwarn (OPT_pedantic, "ISO C90 does not support %<[*]%> array declarators");
0e03329a
JM
3058 }
3059 if (vla_unspec_p)
52ffd86e
MS
3060 {
3061 if (!current_scope->parm_flag)
3062 {
3063 /* C99 6.7.5.2p4 */
3064 error ("%<[*]%> not allowed in other than function prototype scope");
3065 declarator->u.array.vla_unspec_p = false;
3066 return NULL;
3067 }
3068 current_scope->had_vla_unspec = true;
3069 }
f8893e47 3070 return declarator;
0e03329a
JM
3071}
3072
f8893e47
JM
3073/* Set the contained declarator of an array declarator. DECL is the
3074 declarator, as constructed by build_array_declarator; INNER is what
6ac0194d 3075 appears on the left of the []. */
0e03329a 3076
f8893e47
JM
3077struct c_declarator *
3078set_array_declarator_inner (struct c_declarator *decl,
6ac0194d 3079 struct c_declarator *inner)
0e03329a 3080{
f8893e47 3081 decl->declarator = inner;
0e03329a
JM
3082 return decl;
3083}
1f0f7ceb
RS
3084
3085/* INIT is a constructor that forms DECL's initializer. If the final
3086 element initializes a flexible array field, add the size of that
3087 initializer to DECL's size. */
3088
3089static void
3090add_flexible_array_elts_to_size (tree decl, tree init)
3091{
1f0f7ceb
RS
3092 tree elt, type;
3093
2fa6dddd 3094 if (VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (init)))
1f0f7ceb
RS
3095 return;
3096
2fa6dddd 3097 elt = VEC_last (constructor_elt, CONSTRUCTOR_ELTS (init))->value;
1f0f7ceb
RS
3098 type = TREE_TYPE (elt);
3099 if (TREE_CODE (type) == ARRAY_TYPE
3100 && TYPE_SIZE (type) == NULL_TREE
3101 && TYPE_DOMAIN (type) != NULL_TREE
3102 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
3103 {
3104 complete_array_type (&type, elt, false);
3105 DECL_SIZE (decl)
3106 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
3107 DECL_SIZE_UNIT (decl)
3108 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
3109 }
3110}
0e03329a 3111\f
51e29401
RS
3112/* Decode a "typename", such as "int **", returning a ..._TYPE node. */
3113
3114tree
f8893e47 3115groktypename (struct c_type_name *type_name)
51e29401 3116{
f8893e47 3117 tree type;
deb176fa 3118 tree attrs = type_name->specs->attrs;
d3b4cd6f 3119
deb176fa 3120 type_name->specs->attrs = NULL_TREE;
d3b4cd6f 3121
deb176fa 3122 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
b9baeecd 3123 false, NULL, &attrs, DEPRECATED_NORMAL);
d3b4cd6f
AH
3124
3125 /* Apply attributes. */
f8893e47 3126 decl_attributes (&type, attrs, 0);
d3b4cd6f 3127
f8893e47 3128 return type;
51e29401
RS
3129}
3130
51e29401
RS
3131/* Decode a declarator in an ordinary declaration or data definition.
3132 This is called as soon as the type information and variable name
3133 have been parsed, before parsing the initializer if any.
3134 Here we create the ..._DECL node, fill in its type,
3135 and put it on the list of decls for the current context.
3136 The ..._DECL node is returned as the value.
3137
3138 Exception: for arrays where the length is not specified,
3139 the type is left null, to be filled in by `finish_decl'.
3140
3141 Function definitions do not come here; they go to start_function
3142 instead. However, external and forward declarations of functions
3143 do go through here. Structure field declarations are done by
3144 grokfield and not through here. */
3145
51e29401 3146tree
deb176fa 3147start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
f8893e47 3148 bool initialized, tree attributes)
51e29401 3149{
e23bd218 3150 tree decl;
b3694847 3151 tree tem;
0da300cd 3152 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
35b1a6fa 3153
e23bd218
IR
3154 /* An object declared as __attribute__((deprecated)) suppresses
3155 warnings of uses of other deprecated items. */
3156 if (lookup_attribute ("deprecated", attributes))
3157 deprecated_state = DEPRECATED_SUPPRESS;
3158
3159 decl = grokdeclarator (declarator, declspecs,
b9baeecd 3160 NORMAL, initialized, NULL, &attributes,
0da300cd 3161 deprecated_state);
6f17bbcf
JM
3162 if (!decl)
3163 return 0;
35b1a6fa 3164
6645c3fa 3165 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
5b47282c 3166 && MAIN_NAME_P (DECL_NAME (decl)))
dee15844 3167 warning (OPT_Wmain, "%q+D is usually a function", decl);
b8705e61 3168
51e29401
RS
3169 if (initialized)
3170 /* Is it valid for this decl to have an initializer at all?
3171 If not, set INITIALIZED to zero, which will indirectly
f75fbaf7 3172 tell 'finish_decl' to ignore the initializer once it is parsed. */
51e29401
RS
3173 switch (TREE_CODE (decl))
3174 {
3175 case TYPE_DECL:
bda67431 3176 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
95f79357 3177 initialized = 0;
51e29401
RS
3178 break;
3179
3180 case FUNCTION_DECL:
bda67431 3181 error ("function %qD is initialized like a variable", decl);
51e29401
RS
3182 initialized = 0;
3183 break;
3184
3185 case PARM_DECL:
3186 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
bda67431 3187 error ("parameter %qD is initialized", decl);
51e29401
RS
3188 initialized = 0;
3189 break;
3190
3191 default:
f75fbaf7
ZW
3192 /* Don't allow initializations for incomplete types except for
3193 arrays which might be completed by the initialization. */
f4fce7ed 3194
f75fbaf7
ZW
3195 /* This can happen if the array size is an undefined macro.
3196 We already gave a warning, so we don't need another one. */
f4fce7ed
JW
3197 if (TREE_TYPE (decl) == error_mark_node)
3198 initialized = 0;
3199 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
51e29401
RS
3200 {
3201 /* A complete type is ok if size is fixed. */
3202
3203 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
3204 || C_DECL_VARIABLE_SIZE (decl))
3205 {
3206 error ("variable-sized object may not be initialized");
3207 initialized = 0;
3208 }
3209 }
3210 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
3211 {
bda67431 3212 error ("variable %qD has initializer but incomplete type", decl);
51e29401
RS
3213 initialized = 0;
3214 }
ef787822
JM
3215 else if (C_DECL_VARIABLE_SIZE (decl))
3216 {
3217 /* Although C99 is unclear about whether incomplete arrays
3218 of VLAs themselves count as VLAs, it does not make
3219 sense to permit them to be initialized given that
3220 ordinary VLAs may not be initialized. */
3221 error ("variable-sized object may not be initialized");
3222 initialized = 0;
3223 }
51e29401
RS
3224 }
3225
3226 if (initialized)
3227 {
f75fbaf7 3228 if (current_scope == file_scope)
51e29401
RS
3229 TREE_STATIC (decl) = 1;
3230
f75fbaf7 3231 /* Tell 'pushdecl' this is an initialized decl
51e29401 3232 even though we don't yet have the initializer expression.
f75fbaf7 3233 Also tell 'finish_decl' it may store the real initializer. */
51e29401
RS
3234 DECL_INITIAL (decl) = error_mark_node;
3235 }
3236
3237 /* If this is a function declaration, write a record describing it to the
3238 prototypes file (if requested). */
3239
3240 if (TREE_CODE (decl) == FUNCTION_DECL)
3241 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
3242
2786cbad
JM
3243 /* ANSI specifies that a tentative definition which is not merged with
3244 a non-tentative definition behaves exactly like a definition with an
3245 initializer equal to zero. (Section 3.7.2)
3d78f2e9
RH
3246
3247 -fno-common gives strict ANSI behavior, though this tends to break
3248 a large body of code that grew up without this rule.
3249
3250 Thread-local variables are never common, since there's no entrenched
3251 body of code to break, and it allows more efficient variable references
95bd1dd7 3252 in the presence of dynamic linking. */
3d78f2e9
RH
3253
3254 if (TREE_CODE (decl) == VAR_DECL
3255 && !initialized
3256 && TREE_PUBLIC (decl)
c2f7fa15 3257 && !DECL_THREAD_LOCAL_P (decl)
3d78f2e9 3258 && !flag_no_common)
2786cbad 3259 DECL_COMMON (decl) = 1;
8615176a 3260
daa6d5ff 3261 /* Set attributes here so if duplicate decl, will have proper attributes. */
59387d2e 3262 decl_attributes (&decl, attributes, 0);
daa6d5ff 3263
4eb7fd83
JJ
3264 /* Handle gnu_inline attribute. */
3265 if (declspecs->inline_p
da1c7394 3266 && !flag_gnu89_inline
4eb7fd83 3267 && TREE_CODE (decl) == FUNCTION_DECL
6cf59865
JJ
3268 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
3269 || current_function_decl))
4eb7fd83
JJ
3270 {
3271 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
3272 ;
3273 else if (declspecs->storage_class != csc_static)
3274 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
3275 }
4eb7fd83 3276
61f71b34
DD
3277 if (TREE_CODE (decl) == FUNCTION_DECL
3278 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
3279 {
f8893e47 3280 struct c_declarator *ce = declarator;
61f71b34 3281
f8893e47
JM
3282 if (ce->kind == cdk_pointer)
3283 ce = declarator->declarator;
3284 if (ce->kind == cdk_function)
61f71b34 3285 {
f8893e47 3286 tree args = ce->u.arg_info->parms;
61f71b34
DD
3287 for (; args; args = TREE_CHAIN (args))
3288 {
3289 tree type = TREE_TYPE (args);
1f4f60fc 3290 if (type && INTEGRAL_TYPE_P (type)
61f71b34
DD
3291 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
3292 DECL_ARG_TYPE (args) = integer_type_node;
3293 }
3294 }
3295 }
3296
9162542e
AO
3297 if (TREE_CODE (decl) == FUNCTION_DECL
3298 && DECL_DECLARED_INLINE_P (decl)
3299 && DECL_UNINLINABLE (decl)
3300 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
dee15844
JM
3301 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
3302 decl);
9162542e 3303
71113fcd
GK
3304 /* C99 6.7.4p3: An inline definition of a function with external
3305 linkage shall not contain a definition of a modifiable object
3306 with static storage duration... */
3307 if (TREE_CODE (decl) == VAR_DECL
3308 && current_scope != file_scope
3309 && TREE_STATIC (decl)
1033ffa8 3310 && !TREE_READONLY (decl)
71113fcd
GK
3311 && DECL_DECLARED_INLINE_P (current_function_decl)
3312 && DECL_EXTERNAL (current_function_decl))
fcf73884
MLI
3313 pedwarn (0, "%q+D is static but declared in inline function %qD "
3314 "which is not static", decl, current_function_decl);
71113fcd 3315
f8521984 3316 /* Add this decl to the current scope.
51e29401
RS
3317 TEM may equal DECL or it may be a previous decl of the same name. */
3318 tem = pushdecl (decl);
3319
c1bbfd3c
DJ
3320 if (initialized && DECL_EXTERNAL (tem))
3321 {
3322 DECL_EXTERNAL (tem) = 0;
3323 TREE_STATIC (tem) = 1;
3324 }
bbbcb2e1 3325
51e29401
RS
3326 return tem;
3327}
3328
5c386a95
JJ
3329/* Initialize EH if not initialized yet and exceptions are enabled. */
3330
3331void
3332c_maybe_initialize_eh (void)
3333{
3334 if (!flag_exceptions || c_eh_initialized_p)
3335 return;
3336
3337 c_eh_initialized_p = true;
3338 eh_personality_libfunc
3339 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3340 ? "__gcc_personality_sj0"
3341 : "__gcc_personality_v0");
3342 default_init_unwind_resume_libfunc ();
3343 using_eh_for_cleanups ();
3344}
3345
51e29401
RS
3346/* Finish processing of a declaration;
3347 install its initial value.
3348 If the length of an array type is not known before,
3349 it must be determined now, from the initial value, or it is an error. */
3350
3351void
35b1a6fa 3352finish_decl (tree decl, tree init, tree asmspec_tree)
51e29401 3353{
aa24028d 3354 tree type;
51e29401 3355 int was_incomplete = (DECL_SIZE (decl) == 0);
520a57c8 3356 const char *asmspec = 0;
51e29401 3357
6d2f8887 3358 /* If a name was specified, get the string. */
1ae57298
MM
3359 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
3360 && DECL_FILE_SCOPE_P (decl))
41c64394 3361 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
51e29401 3362 if (asmspec_tree)
72f5a12b 3363 asmspec = TREE_STRING_POINTER (asmspec_tree);
51e29401
RS
3364
3365 /* If `start_decl' didn't like having an initialization, ignore it now. */
51e29401
RS
3366 if (init != 0 && DECL_INITIAL (decl) == 0)
3367 init = 0;
35b1a6fa 3368
51e29401
RS
3369 /* Don't crash if parm is initialized. */
3370 if (TREE_CODE (decl) == PARM_DECL)
3371 init = 0;
3372
3373 if (init)
95f79357 3374 store_init_value (decl, init);
51e29401 3375
264fa2db 3376 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
0e6df31e
GK
3377 || TREE_CODE (decl) == FUNCTION_DECL
3378 || TREE_CODE (decl) == FIELD_DECL))
264fa2db
ZL
3379 objc_check_decl (decl);
3380
aa24028d
AP
3381 type = TREE_TYPE (decl);
3382
6614fd40 3383 /* Deduce size of array from initialization, if not already known. */
51e29401
RS
3384 if (TREE_CODE (type) == ARRAY_TYPE
3385 && TYPE_DOMAIN (type) == 0
3386 && TREE_CODE (decl) != TYPE_DECL)
3387 {
aab038d5 3388 bool do_default
51e29401
RS
3389 = (TREE_STATIC (decl)
3390 /* Even if pedantic, an external linkage array
3391 may have incomplete type at first. */
3392 ? pedantic && !TREE_PUBLIC (decl)
1394aabd 3393 : !DECL_EXTERNAL (decl));
51e29401 3394 int failure
aab038d5
RH
3395 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
3396 do_default);
51e29401
RS
3397
3398 /* Get the completed type made by complete_array_type. */
3399 type = TREE_TYPE (decl);
3400
317a9ac3 3401 switch (failure)
51e29401 3402 {
317a9ac3 3403 case 1:
dee15844 3404 error ("initializer fails to determine size of %q+D", decl);
317a9ac3
RH
3405 break;
3406
3407 case 2:
51e29401 3408 if (do_default)
dee15844 3409 error ("array size missing in %q+D", decl);
b4892310
RS
3410 /* If a `static' var's size isn't known,
3411 make it extern as well as static, so it does not get
3412 allocated.
3413 If it is not `static', then do not mark extern;
3414 finish_incomplete_decl will give it a default size
3415 and it will get allocated. */
3f75a254 3416 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
1394aabd 3417 DECL_EXTERNAL (decl) = 1;
317a9ac3
RH
3418 break;
3419
3420 case 3:
dee15844 3421 error ("zero or negative size array %q+D", decl);
317a9ac3
RH
3422 break;
3423
3424 case 0:
3425 /* For global variables, update the copy of the type that
3426 exists in the binding. */
3427 if (TREE_PUBLIC (decl))
3428 {
3429 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
3430 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
3431 b_ext = b_ext->shadowed;
3432 if (b_ext)
3433 {
3434 if (b_ext->type)
3435 b_ext->type = composite_type (b_ext->type, type);
3436 else
3437 b_ext->type = type;
3438 }
3439 }
3440 break;
3441
3442 default:
3443 gcc_unreachable ();
51e29401 3444 }
51e29401 3445
aab038d5
RH
3446 if (DECL_INITIAL (decl))
3447 TREE_TYPE (DECL_INITIAL (decl)) = type;
3448
51e29401
RS
3449 layout_decl (decl, 0);
3450 }
3451
3452 if (TREE_CODE (decl) == VAR_DECL)
3453 {
1f0f7ceb
RS
3454 if (init && TREE_CODE (init) == CONSTRUCTOR)
3455 add_flexible_array_elts_to_size (decl, init);
3456
f4fce7ed
JW
3457 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
3458 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
d575f110
RS
3459 layout_decl (decl, 0);
3460
a7f64d52 3461 if (DECL_SIZE (decl) == 0
f4fce7ed
JW
3462 /* Don't give an error if we already gave one earlier. */
3463 && TREE_TYPE (decl) != error_mark_node
a7f64d52 3464 && (TREE_STATIC (decl)
0e6df31e
GK
3465 /* A static variable with an incomplete type
3466 is an error if it is initialized.
3467 Also if it is not file scope.
3468 Otherwise, let it through, but if it is not `extern'
3469 then it may cause an error message later. */
3470 ? (DECL_INITIAL (decl) != 0
4b1e44be 3471 || !DECL_FILE_SCOPE_P (decl))
0e6df31e
GK
3472 /* An automatic variable with an incomplete type
3473 is an error. */
3474 : !DECL_EXTERNAL (decl)))
3475 {
dee15844 3476 error ("storage size of %q+D isn%'t known", decl);
0e6df31e
GK
3477 TREE_TYPE (decl) = error_mark_node;
3478 }
51e29401 3479
1394aabd 3480 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
90374cc2 3481 && DECL_SIZE (decl) != 0)
e681c5a1
RS
3482 {
3483 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
3484 constant_expression_warning (DECL_SIZE (decl));
3485 else
b97d8153
JJ
3486 {
3487 error ("storage size of %q+D isn%'t constant", decl);
3488 TREE_TYPE (decl) = error_mark_node;
3489 }
e681c5a1 3490 }
e9a25f70 3491
6645c3fa 3492 if (TREE_USED (type))
e9a25f70 3493 TREE_USED (decl) = 1;
51e29401
RS
3494 }
3495
9661b15f
JJ
3496 /* If this is a function and an assembler name is specified, reset DECL_RTL
3497 so we can give it its new name. Also, update built_in_decls if it
3498 was a normal built-in. */
3c4afaa5 3499 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
6645c3fa 3500 {
9661b15f 3501 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
b482789c 3502 set_builtin_user_assembler_name (decl, asmspec);
0e6df31e 3503 set_user_assembler_name (decl, asmspec);
6645c3fa 3504 }
3c4afaa5 3505
d05cc98e 3506 /* If #pragma weak was used, mark the decl weak now. */
3693d46c 3507 maybe_apply_pragma_weak (decl);
d05cc98e 3508
51e29401
RS
3509 /* Output the assembler code and/or RTL code for variables and functions,
3510 unless the type is an undefined structure or union.
3511 If not, it will get done when the type is completed. */
3512
3513 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3514 {
b9e75696
JM
3515 /* Determine the ELF visibility. */
3516 if (TREE_PUBLIC (decl))
3517 c_determine_visibility (decl);
3518
0f7866e7 3519 /* This is a no-op in c-lang.c or something real in objc-act.c. */
37fa72e9 3520 if (c_dialect_objc ())
0f7866e7 3521 objc_check_decl (decl);
8f17b5c5 3522
c22cacf3 3523 if (asmspec)
0e6df31e
GK
3524 {
3525 /* If this is not a static variable, issue a warning.
3526 It doesn't make any sense to give an ASMSPEC for an
3527 ordinary, non-register local variable. Historically,
3528 GCC has accepted -- but ignored -- the ASMSPEC in
3529 this case. */
3f75a254 3530 if (!DECL_FILE_SCOPE_P (decl)
0e6df31e
GK
3531 && TREE_CODE (decl) == VAR_DECL
3532 && !C_DECL_REGISTER (decl)
3533 && !TREE_STATIC (decl))
dee15844
JM
3534 warning (0, "ignoring asm-specifier for non-static local "
3535 "variable %q+D", decl);
0e6df31e
GK
3536 else
3537 set_user_assembler_name (decl, asmspec);
3538 }
c22cacf3 3539
4b1e44be 3540 if (DECL_FILE_SCOPE_P (decl))
8cf8d8a2
JM
3541 {
3542 if (DECL_INITIAL (decl) == NULL_TREE
3543 || DECL_INITIAL (decl) == error_mark_node)
3544 /* Don't output anything
3545 when a tentative file-scope definition is seen.
3546 But at end of compilation, do output code for them. */
3547 DECL_DEFER_OUTPUT (decl) = 1;
0e6df31e 3548 rest_of_decl_compilation (decl, true, 0);
8cf8d8a2 3549 }
8f17b5c5
MM
3550 else
3551 {
0e6df31e
GK
3552 /* In conjunction with an ASMSPEC, the `register'
3553 keyword indicates that we should place the variable
3554 in a particular register. */
3555 if (asmspec && C_DECL_REGISTER (decl))
3645c4dc 3556 {
0e6df31e
GK
3557 DECL_HARD_REGISTER (decl) = 1;
3558 /* This cannot be done for a structure with volatile
3559 fields, on which DECL_REGISTER will have been
3560 reset. */
3561 if (!DECL_REGISTER (decl))
3562 error ("cannot put object with volatile field into register");
3645c4dc 3563 }
0adc3c19 3564
c0a4369a 3565 if (TREE_CODE (decl) != FUNCTION_DECL)
174283a3
RH
3566 {
3567 /* If we're building a variable sized type, and we might be
3568 reachable other than via the top of the current binding
3569 level, then create a new BIND_EXPR so that we deallocate
3570 the object at the right time. */
3571 /* Note that DECL_SIZE can be null due to errors. */
3572 if (DECL_SIZE (decl)
3573 && !TREE_CONSTANT (DECL_SIZE (decl))
3574 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
3575 {
3576 tree bind;
53fb4de3 3577 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
174283a3
RH
3578 TREE_SIDE_EFFECTS (bind) = 1;
3579 add_stmt (bind);
3580 BIND_EXPR_BODY (bind) = push_stmt_list ();
3581 }
3582 add_stmt (build_stmt (DECL_EXPR, decl));
3583 }
8f17b5c5 3584 }
c22cacf3 3585
4dd7201e 3586
4b1e44be 3587 if (!DECL_FILE_SCOPE_P (decl))
51e29401
RS
3588 {
3589 /* Recompute the RTL of a local array now
3590 if it used to be an incomplete type. */
3591 if (was_incomplete
3f75a254 3592 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
51e29401
RS
3593 {
3594 /* If we used it already as memory, it must stay in memory. */
3595 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3596 /* If it's still incomplete now, no init will save it. */
3597 if (DECL_SIZE (decl) == 0)
3598 DECL_INITIAL (decl) = 0;
51e29401 3599 }
51e29401
RS
3600 }
3601 }
3602
3603 if (TREE_CODE (decl) == TYPE_DECL)
1c9766da
RK
3604 {
3605 if (!DECL_FILE_SCOPE_P (decl)
5377d5ba 3606 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
350fae66 3607 add_stmt (build_stmt (DECL_EXPR, decl));
1c9766da 3608
0e6df31e 3609 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
1c9766da 3610 }
51e29401 3611
51e29401
RS
3612 /* At the end of a declaration, throw away any variable type sizes
3613 of types defined inside that declaration. There is no use
3614 computing them in the following function definition. */
f75fbaf7 3615 if (current_scope == file_scope)
51e29401 3616 get_pending_sizes ();
0bfa5f65
RH
3617
3618 /* Install a cleanup (aka destructor) if one was given. */
3619 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
3620 {
3621 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
3622 if (attr)
3623 {
0bfa5f65
RH
3624 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
3625 tree cleanup_decl = lookup_name (cleanup_id);
3626 tree cleanup;
3627
3628 /* Build "cleanup(&decl)" for the destructor. */
3629 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
3630 cleanup = build_tree_list (NULL_TREE, cleanup);
3631 cleanup = build_function_call (cleanup_decl, cleanup);
3632
3633 /* Don't warn about decl unused; the cleanup uses it. */
3634 TREE_USED (decl) = 1;
325c3691 3635 TREE_USED (cleanup_decl) = 1;
0bfa5f65
RH
3636
3637 /* Initialize EH, if we've been told to do so. */
5c386a95 3638 c_maybe_initialize_eh ();
0bfa5f65 3639
325c3691 3640 push_cleanup (decl, cleanup, false);
0bfa5f65
RH
3641 }
3642 }
51e29401
RS
3643}
3644
c34be55e
ZL
3645/* Given a parsed parameter declaration, decode it into a PARM_DECL. */
3646
3647tree
f8893e47 3648grokparm (const struct c_parm *parm)
c34be55e 3649{
b9baeecd 3650 tree attrs = parm->attrs;
f8893e47 3651 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
b9baeecd 3652 NULL, &attrs, DEPRECATED_NORMAL);
c34be55e 3653
b9baeecd 3654 decl_attributes (&decl, attrs, 0);
c34be55e
ZL
3655
3656 return decl;
3657}
3658
55d54003
ZW
3659/* Given a parsed parameter declaration, decode it into a PARM_DECL
3660 and push that on the current scope. */
51e29401
RS
3661
3662void
f8893e47 3663push_parm_decl (const struct c_parm *parm)
51e29401 3664{
b9baeecd 3665 tree attrs = parm->attrs;
6cc902a1 3666 tree decl;
14077d68 3667
0da300cd 3668 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
b9baeecd
RS
3669 &attrs, DEPRECATED_NORMAL);
3670 decl_attributes (&decl, attrs, 0);
6a5ed5bf 3671
51e29401
RS
3672 decl = pushdecl (decl);
3673
51e29401
RS
3674 finish_decl (decl, NULL_TREE, NULL_TREE);
3675}
3676
f75fbaf7 3677/* Mark all the parameter declarations to date as forward decls.
f91f41b2 3678 Also diagnose use of this extension. */
51e29401
RS
3679
3680void
55d54003 3681mark_forward_parm_decls (void)
51e29401 3682{
f75fbaf7 3683 struct c_binding *b;
55d54003
ZW
3684
3685 if (pedantic && !current_scope->warned_forward_parm_decls)
3686 {
fcf73884 3687 pedwarn (OPT_pedantic, "ISO C forbids forward parameter declarations");
55d54003
ZW
3688 current_scope->warned_forward_parm_decls = true;
3689 }
3690
f75fbaf7
ZW
3691 for (b = current_scope->bindings; b; b = b->prev)
3692 if (TREE_CODE (b->decl) == PARM_DECL)
3693 TREE_ASM_WRITTEN (b->decl) = 1;
51e29401
RS
3694}
3695\f
db3acfa5
JM
3696/* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3697 literal, which may be an incomplete array type completed by the
3698 initializer; INIT is a CONSTRUCTOR that initializes the compound
3699 literal. */
3700
3701tree
35b1a6fa 3702build_compound_literal (tree type, tree init)
db3acfa5
JM
3703{
3704 /* We do not use start_decl here because we have a type, not a declarator;
3705 and do not use finish_decl because the decl should be stored inside
350fae66 3706 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
ef787822 3707 tree decl;
db3acfa5 3708 tree complit;
8d37a5c0 3709 tree stmt;
ef787822
JM
3710
3711 if (type == error_mark_node)
3712 return error_mark_node;
3713
3714 decl = build_decl (VAR_DECL, NULL_TREE, type);
db3acfa5
JM
3715 DECL_EXTERNAL (decl) = 0;
3716 TREE_PUBLIC (decl) = 0;
f75fbaf7 3717 TREE_STATIC (decl) = (current_scope == file_scope);
db3acfa5
JM
3718 DECL_CONTEXT (decl) = current_function_decl;
3719 TREE_USED (decl) = 1;
3720 TREE_TYPE (decl) = type;
4f976745 3721 TREE_READONLY (decl) = TYPE_READONLY (type);
db3acfa5
JM
3722 store_init_value (decl, init);
3723
3724 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3725 {
aab038d5
RH
3726 int failure = complete_array_type (&TREE_TYPE (decl),
3727 DECL_INITIAL (decl), true);
366de0ce 3728 gcc_assert (!failure);
aab038d5
RH
3729
3730 type = TREE_TYPE (decl);
3731 TREE_TYPE (DECL_INITIAL (decl)) = type;
db3acfa5
JM
3732 }
3733
db3acfa5
JM
3734 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3735 return error_mark_node;
3736
350fae66 3737 stmt = build_stmt (DECL_EXPR, decl);
aab038d5 3738 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
db3acfa5
JM
3739 TREE_SIDE_EFFECTS (complit) = 1;
3740
3741 layout_decl (decl, 0);
3742
3743 if (TREE_STATIC (decl))
3744 {
3b2db49f
MM
3745 /* This decl needs a name for the assembler output. */
3746 set_compound_literal_name (decl);
cf3c4f56
JJ
3747 DECL_DEFER_OUTPUT (decl) = 1;
3748 DECL_COMDAT (decl) = 1;
3749 DECL_ARTIFICIAL (decl) = 1;
78e0d62b 3750 DECL_IGNORED_P (decl) = 1;
cf3c4f56 3751 pushdecl (decl);
0e6df31e 3752 rest_of_decl_compilation (decl, 1, 0);
db3acfa5
JM
3753 }
3754
3755 return complit;
3756}
3757\f
2984fe64
JM
3758/* Determine whether TYPE is a structure with a flexible array member,
3759 or a union containing such a structure (possibly recursively). */
3760
3761static bool
35b1a6fa 3762flexible_array_type_p (tree type)
2984fe64
JM
3763{
3764 tree x;
3765 switch (TREE_CODE (type))
3766 {
3767 case RECORD_TYPE:
3768 x = TYPE_FIELDS (type);
3769 if (x == NULL_TREE)
3770 return false;
3771 while (TREE_CHAIN (x) != NULL_TREE)
3772 x = TREE_CHAIN (x);
3773 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3774 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3775 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3776 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3777 return true;
3778 return false;
3779 case UNION_TYPE:
3780 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3781 {
3782 if (flexible_array_type_p (TREE_TYPE (x)))
3783 return true;
3784 }
3785 return false;
3786 default:
3787 return false;
3788 }
3789}
3790\f
2ff7cce4
JM
3791/* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3792 replacing with appropriate values if they are invalid. */
3793static void
3794check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3795{
3796 tree type_mv;
3797 unsigned int max_width;
3798 unsigned HOST_WIDE_INT w;
3799 const char *name = orig_name ? orig_name: _("<anonymous>");
3800
2ff7cce4
JM
3801 /* Detect and ignore out of range field width and process valid
3802 field widths. */
411ffa02
JM
3803 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width))
3804 || TREE_CODE (*width) != INTEGER_CST)
2ff7cce4 3805 {
bda67431 3806 error ("bit-field %qs width not an integer constant", name);
2ff7cce4
JM
3807 *width = integer_one_node;
3808 }
3809 else
3810 {
3811 constant_expression_warning (*width);
3812 if (tree_int_cst_sgn (*width) < 0)
3813 {
bda67431 3814 error ("negative width in bit-field %qs", name);
2ff7cce4
JM
3815 *width = integer_one_node;
3816 }
3817 else if (integer_zerop (*width) && orig_name)
3818 {
bda67431 3819 error ("zero width for bit-field %qs", name);
2ff7cce4
JM
3820 *width = integer_one_node;
3821 }
3822 }
3823
3824 /* Detect invalid bit-field type. */
3825 if (TREE_CODE (*type) != INTEGER_TYPE
3826 && TREE_CODE (*type) != BOOLEAN_TYPE
3827 && TREE_CODE (*type) != ENUMERAL_TYPE)
3828 {
bda67431 3829 error ("bit-field %qs has invalid type", name);
2ff7cce4
JM
3830 *type = unsigned_type_node;
3831 }
3832
3833 type_mv = TYPE_MAIN_VARIANT (*type);
fcf73884 3834 if (!in_system_header
2ff7cce4
JM
3835 && type_mv != integer_type_node
3836 && type_mv != unsigned_type_node
a3bf324c 3837 && type_mv != boolean_type_node)
fcf73884 3838 pedwarn (OPT_pedantic, "type of bit-field %qs is a GCC extension", name);
2ff7cce4 3839
02668aba 3840 max_width = TYPE_PRECISION (*type);
2ff7cce4
JM
3841
3842 if (0 < compare_tree_int (*width, max_width))
3843 {
bda67431 3844 error ("width of %qs exceeds its type", name);
2ff7cce4 3845 w = max_width;
7d60be94 3846 *width = build_int_cst (NULL_TREE, w);
2ff7cce4
JM
3847 }
3848 else
3849 w = tree_low_cst (*width, 1);
3850
6de9cd9a
DN
3851 if (TREE_CODE (*type) == ENUMERAL_TYPE)
3852 {
3853 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
118a3a8b 3854 if (!lt
c22cacf3 3855 || w < min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
6de9cd9a 3856 || w < min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
d4ee4d25 3857 warning (0, "%qs is narrower than values of its type", name);
6de9cd9a 3858 }
2ff7cce4 3859}
0b359b01 3860
2ff7cce4 3861\f
50d50fc7
SP
3862
3863/* Print warning about variable length array if necessary. */
3864
3865static void
3866warn_variable_length_array (const char *name, tree size)
3867{
50d50fc7
SP
3868 int const_size = TREE_CONSTANT (size);
3869
fcf73884 3870 if (!flag_isoc99 && pedantic && warn_vla != 0)
50d50fc7
SP
3871 {
3872 if (const_size)
3873 {
3874 if (name)
fcf73884 3875 pedwarn (OPT_Wvla, "ISO C90 forbids array %qs whose size "
50d50fc7
SP
3876 "can%'t be evaluated",
3877 name);
3878 else
fcf73884 3879 pedwarn (OPT_Wvla, "ISO C90 forbids array whose size "
50d50fc7
SP
3880 "can%'t be evaluated");
3881 }
3882 else
3883 {
3884 if (name)
fcf73884 3885 pedwarn (OPT_Wvla, "ISO C90 forbids variable length array %qs",
50d50fc7
SP
3886 name);
3887 else
fcf73884 3888 pedwarn (OPT_Wvla, "ISO C90 forbids variable length array");
50d50fc7
SP
3889 }
3890 }
3891 else if (warn_vla > 0)
3892 {
3893 if (const_size)
3894 {
3895 if (name)
3896 warning (OPT_Wvla,
3897 "the size of array %qs can"
3898 "%'t be evaluated", name);
3899 else
3900 warning (OPT_Wvla,
3901 "the size of array can %'t be evaluated");
3902 }
3903 else
3904 {
3905 if (name)
3906 warning (OPT_Wvla,
3907 "variable length array %qs is used",
3908 name);
3909 else
3910 warning (OPT_Wvla,
3911 "variable length array is used");
3912 }
3913 }
3914}
3915
51e29401
RS
3916/* Given declspecs and a declarator,
3917 determine the name and type of the object declared
3918 and construct a ..._DECL node for it.
3919 (In one case we can return a ..._TYPE node instead.
3920 For invalid input we sometimes return 0.)
3921
deb176fa 3922 DECLSPECS is a c_declspecs structure for the declaration specifiers.
51e29401
RS
3923
3924 DECL_CONTEXT says which syntactic context this declaration is in:
3925 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3926 FUNCDEF for a function definition. Like NORMAL but a few different
3927 error messages in each case. Return value may be zero meaning
3928 this definition is too screwy to try to parse.
3929 PARM for a parameter declaration (either within a function prototype
3930 or before a function body). Make a PARM_DECL, or return void_type_node.
3931 TYPENAME if for a typename (in a cast or sizeof).
3932 Don't make a DECL node; just return the ..._TYPE node.
3933 FIELD for a struct or union field; make a FIELD_DECL.
60919bce 3934 INITIALIZED is true if the decl has an initializer.
2ff7cce4
JM
3935 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3936 representing the width of the bit-field.
b9baeecd
RS
3937 DECL_ATTRS points to the list of attributes that should be added to this
3938 decl. Any nested attributes that belong on the decl itself will be
3939 added to this list.
0da300cd
TT
3940 DEPRECATED_STATE is a deprecated_states value indicating whether
3941 deprecation warnings should be suppressed.
51e29401
RS
3942
3943 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3944 It may also be so in the PARM case, for a prototype where the
3945 argument type is specified but not the name.
3946
3947 This function is where the complicated C meanings of `static'
929f3671 3948 and `extern' are interpreted. */
51e29401
RS
3949
3950static tree
deb176fa
JM
3951grokdeclarator (const struct c_declarator *declarator,
3952 struct c_declspecs *declspecs,
0da300cd 3953 enum decl_context decl_context, bool initialized, tree *width,
b9baeecd 3954 tree *decl_attrs, enum deprecated_states deprecated_state)
51e29401 3955{
deb176fa 3956 tree type = declspecs->type;
9a26d6ee
JM
3957 bool threadp = declspecs->thread_p;
3958 enum c_storage_class storage_class = declspecs->storage_class;
51e29401 3959 int constp;
3932261a 3960 int restrictp;
51e29401 3961 int volatilep;
3932261a 3962 int type_quals = TYPE_UNQUALIFIED;
2ff7cce4 3963 const char *name, *orig_name;
52ffd86e 3964 bool funcdef_flag = false;
f8893e47 3965 bool funcdef_syntax = false;
929f3671 3966 int size_varies = 0;
98c3a782 3967 tree decl_attr = declspecs->decl_attr;
3b53cddc
JM
3968 int array_ptr_quals = TYPE_UNQUALIFIED;
3969 tree array_ptr_attrs = NULL_TREE;
0e03329a 3970 int array_parm_static = 0;
52ffd86e 3971 bool array_parm_vla_unspec_p = false;
91d231cb 3972 tree returned_attrs = NULL_TREE;
2ff7cce4 3973 bool bitfield = width != NULL;
85b58ca5 3974 tree element_type;
f8893e47 3975 struct c_arg_info *arg_info = 0;
51e29401
RS
3976
3977 if (decl_context == FUNCDEF)
52ffd86e 3978 funcdef_flag = true, decl_context = NORMAL;
51e29401 3979
51e29401
RS
3980 /* Look inside a declarator for the name being declared
3981 and get it as a string, for an error message. */
3982 {
f8893e47 3983 const struct c_declarator *decl = declarator;
51e29401
RS
3984 name = 0;
3985
3986 while (decl)
f8893e47 3987 switch (decl->kind)
51e29401 3988 {
f8893e47
JM
3989 case cdk_function:
3990 case cdk_array:
3991 case cdk_pointer:
3992 funcdef_syntax = (decl->kind == cdk_function);
3993 decl = decl->declarator;
51e29401
RS
3994 break;
3995
f8893e47
JM
3996 case cdk_attrs:
3997 decl = decl->declarator;
91d231cb
JM
3998 break;
3999
f8893e47
JM
4000 case cdk_id:
4001 if (decl->u.id)
4002 name = IDENTIFIER_POINTER (decl->u.id);
51e29401
RS
4003 decl = 0;
4004 break;
4005
4006 default:
366de0ce 4007 gcc_unreachable ();
51e29401 4008 }
2ff7cce4 4009 orig_name = name;
51e29401
RS
4010 if (name == 0)
4011 name = "type name";
4012 }
4013
4014 /* A function definition's declarator must have the form of
4015 a function declarator. */
4016
f8893e47 4017 if (funcdef_flag && !funcdef_syntax)
51e29401
RS
4018 return 0;
4019
51e29401
RS
4020 /* If this looks like a function definition, make it one,
4021 even if it occurs where parms are expected.
4022 Then store_parm_decls will reject it and not use it as a parm. */
f75fbaf7 4023 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
51e29401
RS
4024 decl_context = PARM;
4025
deb176fa 4026 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
616aeba2 4027 warn_deprecated_use (declspecs->type);
51e29401 4028
5c076987
JM
4029 if ((decl_context == NORMAL || decl_context == FIELD)
4030 && current_scope == file_scope
4031 && variably_modified_type_p (type, NULL_TREE))
4032 {
4033 error ("variably modified %qs at file scope", name);
4034 type = integer_type_node;
4035 }
4036
98c3a782 4037 size_varies = C_TYPE_VARIABLE_SIZE (type);
51e29401 4038
98c3a782 4039 /* Diagnose defaulting to "int". */
720283f2 4040
98c3a782 4041 if (declspecs->default_int_p && !in_system_header)
51e29401 4042 {
98c3a782
JM
4043 /* Issue a warning if this is an ISO C 99 program or if
4044 -Wreturn-type and this is a function, or if -Wimplicit;
4045 prefer the former warning since it is more explicit. */
4046 if ((warn_implicit_int || warn_return_type || flag_isoc99)
4047 && funcdef_flag)
4048 warn_about_return_type = 1;
fcf73884
MLI
4049 else
4050 pedwarn_c99 (flag_isoc99 ? 0 : OPT_Wimplicit_int,
4051 "type defaults to %<int%> in declaration of %qs", name);
51e29401
RS
4052 }
4053
98c3a782
JM
4054 /* Adjust the type if a bit-field is being declared,
4055 -funsigned-bitfields applied and the type is not explicitly
4056 "signed". */
4057 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
4058 && TREE_CODE (type) == INTEGER_TYPE)
726a989a 4059 type = unsigned_type_for (type);
51e29401 4060
3932261a
MM
4061 /* Figure out the type qualifiers for the declaration. There are
4062 two ways a declaration can become qualified. One is something
4063 like `const int i' where the `const' is explicit. Another is
4064 something like `typedef const int CI; CI i' where the type of the
85b58ca5
JM
4065 declaration contains the `const'. A third possibility is that
4066 there is a type qualifier on the element type of a typedefed
4067 array type, in which case we should extract that qualifier so
5f04ff80 4068 that c_apply_type_quals_to_decl receives the full list of
85b58ca5
JM
4069 qualifiers to work with (C90 is not entirely clear about whether
4070 duplicate qualifiers should be diagnosed in this case, but it
4071 seems most appropriate to do so). */
4072 element_type = strip_array_types (type);
3b53cddc
JM
4073 constp = declspecs->const_p + TYPE_READONLY (element_type);
4074 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
4075 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
28fca7e4
RH
4076 if (pedantic && !flag_isoc99)
4077 {
4078 if (constp > 1)
fcf73884 4079 pedwarn (OPT_pedantic, "duplicate %<const%>");
28fca7e4 4080 if (restrictp > 1)
fcf73884 4081 pedwarn (OPT_pedantic, "duplicate %<restrict%>");
28fca7e4 4082 if (volatilep > 1)
fcf73884 4083 pedwarn (OPT_pedantic, "duplicate %<volatile%>");
28fca7e4 4084 }
46df2823 4085 if (!flag_gen_aux_info && (TYPE_QUALS (element_type)))
51e29401 4086 type = TYPE_MAIN_VARIANT (type);
3932261a
MM
4087 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4088 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4089 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
51e29401 4090
9a26d6ee
JM
4091 /* Warn about storage classes that are invalid for certain
4092 kinds of declarations (parameters, typenames, etc.). */
4093
4094 if (funcdef_flag
4095 && (threadp
4096 || storage_class == csc_auto
4097 || storage_class == csc_register
4098 || storage_class == csc_typedef))
4099 {
fcf73884
MLI
4100 if (storage_class == csc_auto)
4101 pedwarn ((current_scope == file_scope) ? 0 : OPT_pedantic,
4102 "function definition declared %<auto%>");
9a26d6ee
JM
4103 if (storage_class == csc_register)
4104 error ("function definition declared %<register%>");
4105 if (storage_class == csc_typedef)
4106 error ("function definition declared %<typedef%>");
4107 if (threadp)
4108 error ("function definition declared %<__thread%>");
4109 threadp = false;
4110 if (storage_class == csc_auto
4111 || storage_class == csc_register
4112 || storage_class == csc_typedef)
4113 storage_class = csc_none;
4114 }
4115 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
4116 {
4117 if (decl_context == PARM && storage_class == csc_register)
4118 ;
4119 else
4120 {
4121 switch (decl_context)
4122 {
4123 case FIELD:
4124 error ("storage class specified for structure field %qs",
4125 name);
4126 break;
4127 case PARM:
4128 error ("storage class specified for parameter %qs", name);
4129 break;
4130 default:
4131 error ("storage class specified for typename");
4132 break;
4133 }
4134 storage_class = csc_none;
4135 threadp = false;
4136 }
4137 }
4138 else if (storage_class == csc_extern
4139 && initialized
4140 && !funcdef_flag)
4141 {
4142 /* 'extern' with initialization is invalid if not at file scope. */
b8b47f42
GDR
4143 if (current_scope == file_scope)
4144 {
4145 /* It is fine to have 'extern const' when compiling at C
4146 and C++ intersection. */
4147 if (!(warn_cxx_compat && constp))
4148 warning (0, "%qs initialized and declared %<extern%>", name);
4149 }
9a26d6ee
JM
4150 else
4151 error ("%qs has both %<extern%> and initializer", name);
4152 }
4153 else if (current_scope == file_scope)
4154 {
4155 if (storage_class == csc_auto)
bda67431 4156 error ("file-scope declaration of %qs specifies %<auto%>", name);
9a26d6ee 4157 if (pedantic && storage_class == csc_register)
fcf73884 4158 pedwarn (OPT_pedantic, "file-scope declaration of %qs specifies %<register%>", name);
9a26d6ee
JM
4159 }
4160 else
4161 {
4162 if (storage_class == csc_extern && funcdef_flag)
bda67431 4163 error ("nested function %qs declared %<extern%>", name);
9a26d6ee
JM
4164 else if (threadp && storage_class == csc_none)
4165 {
4166 error ("function-scope %qs implicitly auto and declared "
4167 "%<__thread%>",
4168 name);
4169 threadp = false;
4170 }
4171 }
51e29401
RS
4172
4173 /* Now figure out the structure of the declarator proper.
4174 Descend through it, creating more complex types, until we reach
46df2823
JM
4175 the declared identifier (or NULL_TREE, in an absolute declarator).
4176 At each stage we maintain an unqualified version of the type
4177 together with any qualifiers that should be applied to it with
4178 c_build_qualified_type; this way, array types including
4179 multidimensional array types are first built up in unqualified
4180 form and then the qualified form is created with
4181 TYPE_MAIN_VARIANT pointing to the unqualified form. */
51e29401 4182
f8893e47 4183 while (declarator && declarator->kind != cdk_id)
51e29401
RS
4184 {
4185 if (type == error_mark_node)
4186 {
f8893e47 4187 declarator = declarator->declarator;
51e29401
RS
4188 continue;
4189 }
4190
f8893e47
JM
4191 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
4192 a cdk_pointer (for *...),
4193 a cdk_function (for ...(...)),
4194 a cdk_attrs (for nested attributes),
4195 or a cdk_id (for the name being declared
4196 or the place in an absolute declarator
51e29401 4197 where the name was omitted).
f8893e47 4198 For the last case, we have just exited the loop.
51e29401
RS
4199
4200 At this point, TYPE is the type of elements of an array,
4201 or for a function to return, or for a pointer to point to.
4202 After this sequence of ifs, TYPE is the type of the
4203 array or function or pointer, and DECLARATOR has had its
4204 outermost layer removed. */
4205
3b53cddc
JM
4206 if (array_ptr_quals != TYPE_UNQUALIFIED
4207 || array_ptr_attrs != NULL_TREE
4208 || array_parm_static)
0e03329a
JM
4209 {
4210 /* Only the innermost declarator (making a parameter be of
4211 array type which is converted to pointer type)
4212 may have static or type qualifiers. */
4213 error ("static or type qualifiers in non-parameter array declarator");
3b53cddc
JM
4214 array_ptr_quals = TYPE_UNQUALIFIED;
4215 array_ptr_attrs = NULL_TREE;
0e03329a
JM
4216 array_parm_static = 0;
4217 }
4218
f8893e47 4219 switch (declarator->kind)
51e29401 4220 {
f8893e47 4221 case cdk_attrs:
366de0ce 4222 {
f8893e47
JM
4223 /* A declarator with embedded attributes. */
4224 tree attrs = declarator->u.attrs;
4225 const struct c_declarator *inner_decl;
366de0ce 4226 int attr_flags = 0;
f8893e47 4227 declarator = declarator->declarator;
366de0ce 4228 inner_decl = declarator;
f8893e47
JM
4229 while (inner_decl->kind == cdk_attrs)
4230 inner_decl = inner_decl->declarator;
4231 if (inner_decl->kind == cdk_id)
366de0ce 4232 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
f8893e47 4233 else if (inner_decl->kind == cdk_function)
366de0ce 4234 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
f8893e47 4235 else if (inner_decl->kind == cdk_array)
366de0ce
NS
4236 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
4237 returned_attrs = decl_attributes (&type,
4238 chainon (returned_attrs, attrs),
4239 attr_flags);
4240 break;
4241 }
f8893e47 4242 case cdk_array:
366de0ce
NS
4243 {
4244 tree itype = NULL_TREE;
f8893e47 4245 tree size = declarator->u.array.dimen;
366de0ce
NS
4246 /* The index is a signed object `sizetype' bits wide. */
4247 tree index_type = c_common_signed_type (sizetype);
4248
f8893e47 4249 array_ptr_quals = declarator->u.array.quals;
3b53cddc 4250 array_ptr_attrs = declarator->u.array.attrs;
f8893e47 4251 array_parm_static = declarator->u.array.static_p;
52ffd86e
MS
4252 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
4253
f8893e47 4254 declarator = declarator->declarator;
366de0ce
NS
4255
4256 /* Check for some types that there cannot be arrays of. */
c22cacf3 4257
366de0ce
NS
4258 if (VOID_TYPE_P (type))
4259 {
bda67431 4260 error ("declaration of %qs as array of voids", name);
366de0ce
NS
4261 type = error_mark_node;
4262 }
c22cacf3 4263
366de0ce
NS
4264 if (TREE_CODE (type) == FUNCTION_TYPE)
4265 {
bda67431 4266 error ("declaration of %qs as array of functions", name);
366de0ce
NS
4267 type = error_mark_node;
4268 }
c22cacf3 4269
366de0ce 4270 if (pedantic && !in_system_header && flexible_array_type_p (type))
fcf73884 4271 pedwarn (OPT_pedantic, "invalid use of structure with flexible array member");
c22cacf3 4272
366de0ce 4273 if (size == error_mark_node)
51e29401 4274 type = error_mark_node;
c22cacf3 4275
366de0ce
NS
4276 if (type == error_mark_node)
4277 continue;
e9a25f70 4278
366de0ce
NS
4279 /* If size was specified, set ITYPE to a range-type for
4280 that size. Otherwise, ITYPE remains null. finish_decl
4281 may figure it out from an initial value. */
a25f1211 4282
366de0ce
NS
4283 if (size)
4284 {
4285 /* Strip NON_LVALUE_EXPRs since we aren't using as an
4286 lvalue. */
4287 STRIP_TYPE_NOPS (size);
c22cacf3 4288
3f75a254 4289 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
366de0ce 4290 {
bda67431 4291 error ("size of array %qs has non-integer type", name);
366de0ce
NS
4292 size = integer_one_node;
4293 }
c22cacf3 4294
366de0ce 4295 if (pedantic && integer_zerop (size))
fcf73884 4296 pedwarn (OPT_pedantic, "ISO C forbids zero-size array %qs", name);
c22cacf3 4297
366de0ce
NS
4298 if (TREE_CODE (size) == INTEGER_CST)
4299 {
4300 constant_expression_warning (size);
4301 if (tree_int_cst_sgn (size) < 0)
4302 {
bda67431 4303 error ("size of array %qs is negative", name);
366de0ce
NS
4304 size = integer_one_node;
4305 }
4306 }
5c076987
JM
4307 else if ((decl_context == NORMAL || decl_context == FIELD)
4308 && current_scope == file_scope)
4309 {
4310 error ("variably modified %qs at file scope", name);
4311 size = integer_one_node;
4312 }
366de0ce
NS
4313 else
4314 {
4315 /* Make sure the array size remains visibly
4316 nonconstant even if it is (eg) a const variable
4317 with known value. */
4318 size_varies = 1;
50d50fc7 4319 warn_variable_length_array (orig_name, size);
366de0ce 4320 }
51e29401 4321
366de0ce
NS
4322 if (integer_zerop (size))
4323 {
c22cacf3
MS
4324 /* A zero-length array cannot be represented with
4325 an unsigned index type, which is what we'll
4326 get with build_index_type. Create an
4327 open-ended range instead. */
366de0ce
NS
4328 itype = build_range_type (sizetype, size, NULL_TREE);
4329 }
4330 else
4331 {
8b0b9aef
RH
4332 /* Arrange for the SAVE_EXPR on the inside of the
4333 MINUS_EXPR, which allows the -1 to get folded
4334 with the +1 that happens when building TYPE_SIZE. */
4335 if (size_varies)
4336 size = variable_size (size);
4337
366de0ce
NS
4338 /* Compute the maximum valid index, that is, size
4339 - 1. Do the calculation in index_type, so that
4340 if it is a variable the computations will be
4341 done in the proper mode. */
987b67bc
KH
4342 itype = fold_build2 (MINUS_EXPR, index_type,
4343 convert (index_type, size),
4344 convert (index_type,
4345 size_one_node));
366de0ce 4346
c22cacf3 4347 /* If that overflowed, the array is too big. ???
366de0ce
NS
4348 While a size of INT_MAX+1 technically shouldn't
4349 cause an overflow (because we subtract 1), the
4350 overflow is recorded during the conversion to
4351 index_type, before the subtraction. Handling
4352 this case seems like an unnecessary
4353 complication. */
6414bad6
RS
4354 if (TREE_CODE (itype) == INTEGER_CST
4355 && TREE_OVERFLOW (itype))
366de0ce 4356 {
bda67431 4357 error ("size of array %qs is too large", name);
366de0ce
NS
4358 type = error_mark_node;
4359 continue;
4360 }
c22cacf3 4361
366de0ce
NS
4362 itype = build_index_type (itype);
4363 }
4364 }
4365 else if (decl_context == FIELD)
4366 {
4367 if (pedantic && !flag_isoc99 && !in_system_header)
fcf73884 4368 pedwarn (OPT_pedantic, "ISO C90 does not support flexible array members");
c7b82833 4369
366de0ce
NS
4370 /* ISO C99 Flexible array members are effectively
4371 identical to GCC's zero-length array extension. */
4372 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4373 }
52ffd86e
MS
4374 else if (decl_context == PARM)
4375 {
4376 if (array_parm_vla_unspec_p)
4377 {
4378 if (! orig_name)
4379 {
4380 /* C99 6.7.5.2p4 */
4381 error ("%<[*]%> not allowed in other than a declaration");
4382 }
4383
4384 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4385 size_varies = 1;
4386 }
4387 }
8d9f82d5
AO
4388 else if (decl_context == TYPENAME)
4389 {
4390 if (array_parm_vla_unspec_p)
4391 {
4392 /* The error is printed elsewhere. We use this to
4393 avoid messing up with incomplete array types of
4394 the same type, that would otherwise be modified
4395 below. */
4396 itype = build_range_type (sizetype, size_zero_node,
4397 NULL_TREE);
4398 }
4399 }
51e29401 4400
b4519d39
SB
4401 /* Complain about arrays of incomplete types. */
4402 if (!COMPLETE_TYPE_P (type))
4403 {
4404 error ("array type has incomplete element type");
c22cacf3 4405 type = error_mark_node;
b4519d39
SB
4406 }
4407 else
8d9f82d5
AO
4408 /* When itype is NULL, a shared incomplete array type is
4409 returned for all array of a given type. Elsewhere we
4410 make sure we don't complete that type before copying
4411 it, but here we want to make sure we don't ever
4412 modify the shared type, so we gcc_assert (itype)
4413 below. */
b4519d39 4414 type = build_array_type (type, itype);
51e29401 4415
9df594d7 4416 if (type != error_mark_node)
366de0ce 4417 {
9df594d7 4418 if (size_varies)
5c076987 4419 {
8d9f82d5
AO
4420 /* It is ok to modify type here even if itype is
4421 NULL: if size_varies, we're in a
96ff6c8c 4422 multi-dimensional array and the inner type has
8d9f82d5
AO
4423 variable size, so the enclosing shared array type
4424 must too. */
5c076987
JM
4425 if (size && TREE_CODE (size) == INTEGER_CST)
4426 type
4427 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
4428 C_TYPE_VARIABLE_SIZE (type) = 1;
4429 }
9df594d7
RG
4430
4431 /* The GCC extension for zero-length arrays differs from
4432 ISO flexible array members in that sizeof yields
4433 zero. */
4434 if (size && integer_zerop (size))
4435 {
8d9f82d5 4436 gcc_assert (itype);
9df594d7
RG
4437 TYPE_SIZE (type) = bitsize_zero_node;
4438 TYPE_SIZE_UNIT (type) = size_zero_node;
4439 }
52ffd86e
MS
4440 if (array_parm_vla_unspec_p)
4441 {
8d9f82d5 4442 gcc_assert (itype);
52ffd86e
MS
4443 /* The type is complete. C99 6.7.5.2p4 */
4444 TYPE_SIZE (type) = bitsize_zero_node;
4445 TYPE_SIZE_UNIT (type) = size_zero_node;
4446 }
366de0ce 4447 }
51e29401 4448
366de0ce 4449 if (decl_context != PARM
3b53cddc
JM
4450 && (array_ptr_quals != TYPE_UNQUALIFIED
4451 || array_ptr_attrs != NULL_TREE
4452 || array_parm_static))
366de0ce
NS
4453 {
4454 error ("static or type qualifiers in non-parameter array declarator");
3b53cddc
JM
4455 array_ptr_quals = TYPE_UNQUALIFIED;
4456 array_ptr_attrs = NULL_TREE;
366de0ce
NS
4457 array_parm_static = 0;
4458 }
4459 break;
4460 }
f8893e47 4461 case cdk_function:
366de0ce
NS
4462 {
4463 /* Say it's a definition only for the declarator closest
4464 to the identifier, apart possibly from some
4465 attributes. */
4466 bool really_funcdef = false;
4467 tree arg_types;
4468 if (funcdef_flag)
4469 {
f8893e47
JM
4470 const struct c_declarator *t = declarator->declarator;
4471 while (t->kind == cdk_attrs)
4472 t = t->declarator;
4473 really_funcdef = (t->kind == cdk_id);
366de0ce 4474 }
51e29401 4475
366de0ce
NS
4476 /* Declaring a function type. Make sure we have a valid
4477 type for the function to return. */
4478 if (type == error_mark_node)
4479 continue;
c22cacf3 4480
366de0ce 4481 size_varies = 0;
51e29401 4482
366de0ce
NS
4483 /* Warn about some types functions can't return. */
4484 if (TREE_CODE (type) == FUNCTION_TYPE)
4485 {
bda67431 4486 error ("%qs declared as function returning a function", name);
366de0ce
NS
4487 type = integer_type_node;
4488 }
4489 if (TREE_CODE (type) == ARRAY_TYPE)
4490 {
bda67431 4491 error ("%qs declared as function returning an array", name);
366de0ce
NS
4492 type = integer_type_node;
4493 }
51e29401 4494
366de0ce
NS
4495 /* Construct the function type and go to the next
4496 inner layer of declarator. */
f8893e47 4497 arg_info = declarator->u.arg_info;
366de0ce 4498 arg_types = grokparms (arg_info, really_funcdef);
3542a5c0
JM
4499 if (really_funcdef)
4500 put_pending_sizes (arg_info->pending_sizes);
51e29401 4501
366de0ce
NS
4502 /* Type qualifiers before the return type of the function
4503 qualify the return type, not the function type. */
4504 if (type_quals)
4505 {
c22cacf3 4506 /* Type qualifiers on a function return type are
366de0ce
NS
4507 normally permitted by the standard but have no
4508 effect, so give a warning at -Wreturn-type.
4509 Qualifiers on a void return type are banned on
4510 function definitions in ISO C; GCC used to used
4511 them for noreturn functions. */
4512 if (VOID_TYPE_P (type) && really_funcdef)
fcf73884 4513 pedwarn (0, "function definition has qualified void return type");
44c21c7f 4514 else
5db2e9ca 4515 warning (OPT_Wignored_qualifiers,
44c21c7f 4516 "type qualifiers ignored on function return type");
c22cacf3 4517
366de0ce
NS
4518 type = c_build_qualified_type (type, type_quals);
4519 }
4520 type_quals = TYPE_UNQUALIFIED;
c22cacf3 4521
366de0ce 4522 type = build_function_type (type, arg_types);
f8893e47 4523 declarator = declarator->declarator;
c22cacf3 4524
366de0ce
NS
4525 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4526 the formal parameter list of this FUNCTION_TYPE to point to
4527 the FUNCTION_TYPE node itself. */
e0c9fbb7 4528 {
366de0ce 4529 tree link;
c22cacf3 4530
f8893e47 4531 for (link = arg_info->tags;
366de0ce
NS
4532 link;
4533 link = TREE_CHAIN (link))
4534 TYPE_CONTEXT (TREE_VALUE (link)) = type;
e0c9fbb7 4535 }
366de0ce
NS
4536 break;
4537 }
f8893e47 4538 case cdk_pointer:
51e29401 4539 {
366de0ce
NS
4540 /* Merge any constancy or volatility into the target type
4541 for the pointer. */
51e29401 4542
366de0ce
NS
4543 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4544 && type_quals)
fcf73884 4545 pedwarn (OPT_pedantic, "ISO C forbids qualified function types");
366de0ce
NS
4546 if (type_quals)
4547 type = c_build_qualified_type (type, type_quals);
366de0ce 4548 size_varies = 0;
3b53cddc 4549
706c4bb7
OH
4550 /* When the pointed-to type involves components of variable size,
4551 care must be taken to ensure that the size evaluation code is
4552 emitted early enough to dominate all the possible later uses
4553 and late enough for the variables on which it depends to have
4554 been assigned.
4555
4556 This is expected to happen automatically when the pointed-to
4557 type has a name/declaration of it's own, but special attention
4558 is required if the type is anonymous.
4559
4560 We handle the NORMAL and FIELD contexts here by attaching an
4561 artificial TYPE_DECL to such pointed-to type. This forces the
4562 sizes evaluation at a safe point and ensures it is not deferred
4563 until e.g. within a deeper conditional context.
4564
4565 We expect nothing to be needed here for PARM or TYPENAME.
4566 Pushing a TYPE_DECL at this point for TYPENAME would actually
4567 be incorrect, as we might be in the middle of an expression
4568 with side effects on the pointed-to type size "arguments" prior
4569 to the pointer declaration point and the fake TYPE_DECL in the
4570 enclosing context would force the size evaluation prior to the
4571 side effects. */
4572
4573 if (!TYPE_NAME (type)
4574 && (decl_context == NORMAL || decl_context == FIELD)
4575 && variably_modified_type_p (type, NULL_TREE))
4576 {
4577 tree decl = build_decl (TYPE_DECL, NULL_TREE, type);
4578 DECL_ARTIFICIAL (decl) = 1;
4579 pushdecl (decl);
4580 finish_decl (decl, NULL_TREE, NULL_TREE);
4581 TYPE_NAME (type) = decl;
4582 }
4583
366de0ce 4584 type = build_pointer_type (type);
c22cacf3 4585
deb176fa
JM
4586 /* Process type qualifiers (such as const or volatile)
4587 that were given inside the `*'. */
3b53cddc 4588 type_quals = declarator->u.pointer_quals;
deb176fa 4589
f8893e47 4590 declarator = declarator->declarator;
366de0ce 4591 break;
51e29401 4592 }
366de0ce
NS
4593 default:
4594 gcc_unreachable ();
51e29401 4595 }
51e29401 4596 }
b9baeecd 4597 *decl_attrs = chainon (returned_attrs, *decl_attrs);
51e29401 4598
46df2823
JM
4599 /* Now TYPE has the actual type, apart from any qualifiers in
4600 TYPE_QUALS. */
51e29401 4601
d134f39e
JM
4602 /* Check the type and width of a bit-field. */
4603 if (bitfield)
4604 check_bitfield_type_and_width (&type, width, orig_name);
4605
e9a25f70
JL
4606 /* Did array size calculations overflow? */
4607
4608 if (TREE_CODE (type) == ARRAY_TYPE
d0f062fb 4609 && COMPLETE_TYPE_P (type)
9bc2a5cb 4610 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
355a9e43 4611 && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
68940175 4612 {
bda67431 4613 error ("size of array %qs is too large", name);
684d9f3b 4614 /* If we proceed with the array type as it is, we'll eventually
68940175
AO
4615 crash in tree_low_cst(). */
4616 type = error_mark_node;
4617 }
e9a25f70 4618
51e29401
RS
4619 /* If this is declaring a typedef name, return a TYPE_DECL. */
4620
9a26d6ee 4621 if (storage_class == csc_typedef)
51e29401
RS
4622 {
4623 tree decl;
51e29401 4624 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
3932261a 4625 && type_quals)
fcf73884 4626 pedwarn (OPT_pedantic, "ISO C forbids qualified function types");
3932261a
MM
4627 if (type_quals)
4628 type = c_build_qualified_type (type, type_quals);
f8893e47 4629 decl = build_decl (TYPE_DECL, declarator->u.id, type);
5af28c74 4630 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
98c3a782 4631 if (declspecs->explicit_signed_p)
51e29401 4632 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
61f4f1cc 4633 if (declspecs->inline_p)
fcf73884 4634 pedwarn (0, "typedef %q+D declared %<inline%>", decl);
51e29401
RS
4635 return decl;
4636 }
4637
51e29401
RS
4638 /* If this is a type name (such as, in a cast or sizeof),
4639 compute the type and return it now. */
4640
4641 if (decl_context == TYPENAME)
4642 {
61f4f1cc
JM
4643 /* Note that the grammar rejects storage classes in typenames
4644 and fields. */
4645 gcc_assert (storage_class == csc_none && !threadp
4646 && !declspecs->inline_p);
51e29401 4647 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
3932261a 4648 && type_quals)
fcf73884 4649 pedwarn (OPT_pedantic, "ISO C forbids const or volatile function types");
3932261a
MM
4650 if (type_quals)
4651 type = c_build_qualified_type (type, type_quals);
51e29401
RS
4652 return type;
4653 }
4654
52ffd86e
MS
4655 if (pedantic && decl_context == FIELD
4656 && variably_modified_type_p (type, NULL_TREE))
4657 {
4658 /* C99 6.7.2.1p8 */
fcf73884
MLI
4659 pedwarn (OPT_pedantic,
4660 "a member of a structure or union cannot have a variably modified type");
52ffd86e
MS
4661 }
4662
61df2ee2
RS
4663 /* Aside from typedefs and type names (handle above),
4664 `void' at top level (not within pointer)
4665 is allowed only in public variables.
51e29401
RS
4666 We don't complain about parms either, but that is because
4667 a better error message can be made later. */
4668
71653180 4669 if (VOID_TYPE_P (type) && decl_context != PARM
3f75a254 4670 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
9a26d6ee 4671 && (storage_class == csc_extern
f75fbaf7 4672 || (current_scope == file_scope
9a26d6ee
JM
4673 && !(storage_class == csc_static
4674 || storage_class == csc_register)))))
51e29401 4675 {
bda67431 4676 error ("variable or field %qs declared void", name);
51e29401
RS
4677 type = integer_type_node;
4678 }
4679
4680 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4681 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4682
4683 {
91d231cb 4684 tree decl;
51e29401
RS
4685
4686 if (decl_context == PARM)
4687 {
c530479e 4688 tree promoted_type;
51e29401
RS
4689
4690 /* A parameter declared as an array of T is really a pointer to T.
4691 One declared as a function is really a pointer to a function. */
4692
4693 if (TREE_CODE (type) == ARRAY_TYPE)
4694 {
4695 /* Transfer const-ness of array into that of type pointed to. */
eaf2e788 4696 type = TREE_TYPE (type);
3932261a
MM
4697 if (type_quals)
4698 type = c_build_qualified_type (type, type_quals);
eaf2e788 4699 type = build_pointer_type (type);
3b53cddc 4700 type_quals = array_ptr_quals;
b09869ff
JM
4701 if (type_quals)
4702 type = c_build_qualified_type (type, type_quals);
0e03329a 4703
3b53cddc
JM
4704 /* We don't yet implement attributes in this context. */
4705 if (array_ptr_attrs != NULL_TREE)
5c498b10
DD
4706 warning (OPT_Wattributes,
4707 "attributes in parameter array declarator ignored");
0e03329a 4708
929f3671 4709 size_varies = 0;
51e29401
RS
4710 }
4711 else if (TREE_CODE (type) == FUNCTION_TYPE)
4712 {
fcf73884
MLI
4713 if (type_quals)
4714 pedwarn (OPT_pedantic, "ISO C forbids qualified function types");
3932261a
MM
4715 if (type_quals)
4716 type = c_build_qualified_type (type, type_quals);
eaf2e788 4717 type = build_pointer_type (type);
3932261a 4718 type_quals = TYPE_UNQUALIFIED;
51e29401 4719 }
0f38b811
MM
4720 else if (type_quals)
4721 type = c_build_qualified_type (type, type_quals);
35b1a6fa 4722
f8893e47 4723 decl = build_decl (PARM_DECL, declarator->u.id, type);
5af28c74 4724 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
929f3671 4725 if (size_varies)
51e29401
RS
4726 C_DECL_VARIABLE_SIZE (decl) = 1;
4727
4728 /* Compute the type actually passed in the parmlist,
4729 for the case where there is no prototype.
4730 (For example, shorts and chars are passed as ints.)
4731 When there is a prototype, this is overridden later. */
4732
c530479e
RH
4733 if (type == error_mark_node)
4734 promoted_type = type;
4735 else
ab393bf1 4736 promoted_type = c_type_promotes_to (type);
51e29401 4737
c530479e 4738 DECL_ARG_TYPE (decl) = promoted_type;
61f4f1cc 4739 if (declspecs->inline_p)
fcf73884 4740 pedwarn (0, "parameter %q+D declared %<inline%>", decl);
51e29401
RS
4741 }
4742 else if (decl_context == FIELD)
4743 {
61f4f1cc
JM
4744 /* Note that the grammar rejects storage classes in typenames
4745 and fields. */
4746 gcc_assert (storage_class == csc_none && !threadp
4747 && !declspecs->inline_p);
4748
51e29401 4749 /* Structure field. It may not be a function. */
dfd48d76 4750
51e29401
RS
4751 if (TREE_CODE (type) == FUNCTION_TYPE)
4752 {
bda67431 4753 error ("field %qs declared as a function", name);
51e29401
RS
4754 type = build_pointer_type (type);
4755 }
d0f062fb 4756 else if (TREE_CODE (type) != ERROR_MARK
c22cacf3 4757 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
51e29401 4758 {
bda67431 4759 error ("field %qs has incomplete type", name);
51e29401
RS
4760 type = error_mark_node;
4761 }
46df2823 4762 type = c_build_qualified_type (type, type_quals);
f8893e47 4763 decl = build_decl (FIELD_DECL, declarator->u.id, type);
5af28c74 4764 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
2bf105ab 4765 DECL_NONADDRESSABLE_P (decl) = bitfield;
ab53bae2
AO
4766 if (bitfield && !declarator->u.id)
4767 TREE_NO_WARNING (decl) = 1;
2bf105ab 4768
929f3671 4769 if (size_varies)
51e29401
RS
4770 C_DECL_VARIABLE_SIZE (decl) = 1;
4771 }
4772 else if (TREE_CODE (type) == FUNCTION_TYPE)
4773 {
9a26d6ee 4774 if (storage_class == csc_register || threadp)
1ac0ac8b
JM
4775 {
4776 error ("invalid storage class for function %qs", name);
1ac0ac8b 4777 }
ff164b22
GK
4778 else if (current_scope != file_scope)
4779 {
4780 /* Function declaration not at file scope. Storage
4781 classes other than `extern' are not allowed, C99
4782 6.7.1p5, and `extern' makes no difference. However,
4783 GCC allows 'auto', perhaps with 'inline', to support
4784 nested functions. */
9a26d6ee 4785 if (storage_class == csc_auto)
fcf73884 4786 pedwarn (OPT_pedantic, "invalid storage class for function %qs", name);
6f17bbcf 4787 else if (storage_class == csc_static)
1ac0ac8b 4788 {
c22cacf3
MS
4789 error ("invalid storage class for function %qs", name);
4790 if (funcdef_flag)
6f17bbcf
JM
4791 storage_class = declspecs->storage_class = csc_none;
4792 else
4793 return 0;
1ac0ac8b 4794 }
ff164b22 4795 }
fd0b8fce 4796
6f17bbcf 4797 decl = build_decl (FUNCTION_DECL, declarator->u.id, type);
5af28c74 4798 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
6f17bbcf
JM
4799 decl = build_decl_attribute_variant (decl, decl_attr);
4800
3f75a254 4801 if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
fcf73884
MLI
4802 pedwarn (OPT_pedantic,
4803 "ISO C forbids qualified function types");
51e29401 4804
e508a019 4805 /* GNU C interprets a volatile-qualified function type to indicate
3932261a
MM
4806 that the function does not return. */
4807 if ((type_quals & TYPE_QUAL_VOLATILE)
71653180 4808 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
d4ee4d25 4809 warning (0, "%<noreturn%> function returns non-void value");
61df2ee2 4810
9affb2c7
ZW
4811 /* Every function declaration is an external reference
4812 (DECL_EXTERNAL) except for those which are not at file
4813 scope and are explicitly declared "auto". This is
4814 forbidden by standard C (C99 6.7.1p5) and is interpreted by
4815 GCC to signify a forward declaration of a nested function. */
9a26d6ee 4816 if (storage_class == csc_auto && current_scope != file_scope)
9affb2c7 4817 DECL_EXTERNAL (decl) = 0;
71113fcd
GK
4818 /* In C99, a function which is declared 'inline' with 'extern'
4819 is not an external reference (which is confusing). It
4820 means that the later definition of the function must be output
4821 in this file, C99 6.7.4p6. In GNU C89, a function declared
4822 'extern inline' is an external reference. */
4823 else if (declspecs->inline_p && storage_class != csc_static)
da1c7394
ILT
4824 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
4825 == flag_gnu89_inline);
9affb2c7 4826 else
71113fcd 4827 DECL_EXTERNAL (decl) = !initialized;
caf93cb0 4828
51e29401
RS
4829 /* Record absence of global scope for `static' or `auto'. */
4830 TREE_PUBLIC (decl)
9a26d6ee 4831 = !(storage_class == csc_static || storage_class == csc_auto);
c40f7b33 4832
77dbdb57 4833 /* For a function definition, record the argument information
7b112487 4834 block where store_parm_decls will look for it. */
77dbdb57 4835 if (funcdef_flag)
7b112487 4836 current_function_arg_info = arg_info;
77dbdb57 4837
98c3a782 4838 if (declspecs->default_int_p)
5ce89b2e
JM
4839 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4840
51e29401 4841 /* Record presence of `inline', if it is reasonable. */
61f4f1cc 4842 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
51e29401 4843 {
9a26d6ee 4844 if (declspecs->inline_p)
fcf73884 4845 pedwarn (0, "cannot inline function %<main%>");
31ed8fea 4846 }
9a26d6ee 4847 else if (declspecs->inline_p)
31ed8fea 4848 {
b3c3af2f 4849 /* Record that the function is declared `inline'. */
31ed8fea 4850 DECL_DECLARED_INLINE_P (decl) = 1;
51e29401 4851
b79d5213
RH
4852 /* Do not mark bare declarations as DECL_INLINE. Doing so
4853 in the presence of multiple declarations can result in
4854 the abstract origin pointing between the declarations,
4855 which will confuse dwarf2out. */
4856 if (initialized)
71113fcd 4857 DECL_INLINE (decl) = 1;
51e29401 4858 }
7e8b322a 4859 else if (initialized)
b3c3af2f 4860 DECL_INLINE (decl) = 1;
51e29401
RS
4861 }
4862 else
4863 {
4864 /* It's a variable. */
fd0b8fce 4865 /* An uninitialized decl with `extern' is a reference. */
9a26d6ee 4866 int extern_ref = !initialized && storage_class == csc_extern;
51e29401 4867
46df2823 4868 type = c_build_qualified_type (type, type_quals);
f4e92987 4869
f75fbaf7
ZW
4870 /* C99 6.2.2p7: It is invalid (compile-time undefined
4871 behavior) to create an 'extern' declaration for a
f4e92987 4872 variable if there is a global declaration that is
f75fbaf7
ZW
4873 'static' and the global declaration is not visible.
4874 (If the static declaration _is_ currently visible,
4875 the 'extern' declaration is taken to refer to that decl.) */
4876 if (extern_ref && current_scope != file_scope)
f4e92987 4877 {
f8893e47
JM
4878 tree global_decl = identifier_global_value (declarator->u.id);
4879 tree visible_decl = lookup_name (declarator->u.id);
f4e92987 4880
35b1a6fa 4881 if (global_decl
f75fbaf7 4882 && global_decl != visible_decl
f4e92987
MM
4883 && TREE_CODE (global_decl) == VAR_DECL
4884 && !TREE_PUBLIC (global_decl))
bda67431
JM
4885 error ("variable previously declared %<static%> redeclared "
4886 "%<extern%>");
f4e92987
MM
4887 }
4888
f8893e47 4889 decl = build_decl (VAR_DECL, declarator->u.id, type);
6037d88d 4890 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
929f3671 4891 if (size_varies)
51e29401
RS
4892 C_DECL_VARIABLE_SIZE (decl) = 1;
4893
9a26d6ee 4894 if (declspecs->inline_p)
fcf73884 4895 pedwarn (0, "variable %q+D declared %<inline%>", decl);
51e29401 4896
bbbcb2e1
JM
4897 /* At file scope, an initialized extern declaration may follow
4898 a static declaration. In that case, DECL_EXTERNAL will be
4899 reset later in start_decl. */
9a26d6ee 4900 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
3d78f2e9 4901
f8521984 4902 /* At file scope, the presence of a `static' or `register' storage
ee534ebf
RS
4903 class specifier, or the absence of all storage class specifiers
4904 makes this declaration a definition (perhaps tentative). Also,
fcc207bf 4905 the absence of `static' makes it public. */
f75fbaf7 4906 if (current_scope == file_scope)
51e29401 4907 {
fcc207bf 4908 TREE_PUBLIC (decl) = storage_class != csc_static;
3d78f2e9 4909 TREE_STATIC (decl) = !extern_ref;
51e29401 4910 }
f8521984 4911 /* Not at file scope, only `static' makes a static definition. */
51e29401
RS
4912 else
4913 {
9a26d6ee 4914 TREE_STATIC (decl) = (storage_class == csc_static);
3d78f2e9
RH
4915 TREE_PUBLIC (decl) = extern_ref;
4916 }
4917
9a26d6ee 4918 if (threadp)
8893239d 4919 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
51e29401
RS
4920 }
4921
52ffd86e
MS
4922 if (storage_class == csc_extern
4923 && variably_modified_type_p (type, NULL_TREE))
4924 {
4925 /* C99 6.7.5.2p2 */
4926 error ("object with variably modified type must have no linkage");
4927 }
4928
51e29401
RS
4929 /* Record `register' declaration for warnings on &
4930 and in case doing stupid register allocation. */
4931
9a26d6ee 4932 if (storage_class == csc_register)
5baeaac0
JM
4933 {
4934 C_DECL_REGISTER (decl) = 1;
4935 DECL_REGISTER (decl) = 1;
4936 }
51e29401
RS
4937
4938 /* Record constancy and volatility. */
3932261a 4939 c_apply_type_quals_to_decl (type_quals, decl);
51e29401 4940
51e29401
RS
4941 /* If a type has volatile components, it should be stored in memory.
4942 Otherwise, the fact that those components are volatile
820cc88f
DB
4943 will be ignored, and would even crash the compiler.
4944 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
4945 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
4946 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
4947 || TREE_CODE (decl) == RESULT_DECL))
5baeaac0
JM
4948 {
4949 /* It is not an error for a structure with volatile fields to
4950 be declared register, but reset DECL_REGISTER since it
4951 cannot actually go in a register. */
4952 int was_reg = C_DECL_REGISTER (decl);
4953 C_DECL_REGISTER (decl) = 0;
4954 DECL_REGISTER (decl) = 0;
4955 c_mark_addressable (decl);
4956 C_DECL_REGISTER (decl) = was_reg;
4957 }
51e29401 4958
d05cc98e
GK
4959 /* This is the earliest point at which we might know the assembler
4960 name of a variable. Thus, if it's known before this, die horribly. */
366de0ce 4961 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
d05cc98e 4962
51e29401
RS
4963 return decl;
4964 }
4965}
4966\f
51e29401 4967/* Decode the parameter-list info for a function type or function definition.
52ffd86e 4968 The argument is the value returned by `get_parm_info' (or made in c-parse.c
51e29401
RS
4969 if there is an identifier list instead of a parameter decl list).
4970 These two functions are separate because when a function returns
4971 or receives functions then each is called multiple times but the order
4972 of calls is different. The last call to `grokparms' is always the one
4973 that contains the formal parameter names of a function definition.
4974
51e29401
RS
4975 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4976
60919bce 4977 FUNCDEF_FLAG is true for a function definition, false for
51e29401 4978 a mere declaration. A nonempty identifier-list gets an error message
60919bce 4979 when FUNCDEF_FLAG is false. */
51e29401
RS
4980
4981static tree
f8893e47 4982grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
51e29401 4983{
f8893e47 4984 tree arg_types = arg_info->types;
51e29401 4985
52ffd86e
MS
4986 if (funcdef_flag && arg_info->had_vla_unspec)
4987 {
4988 /* A function definition isn't function prototype scope C99 6.2.1p4. */
4989 /* C99 6.7.5.2p4 */
4990 error ("%<[*]%> not allowed in other than function prototype scope");
4991 }
4992
3176a0c2
DD
4993 if (arg_types == 0 && !funcdef_flag && !in_system_header)
4994 warning (OPT_Wstrict_prototypes,
4995 "function declaration isn%'t a prototype");
51e29401 4996
f75fbaf7
ZW
4997 if (arg_types == error_mark_node)
4998 return 0; /* don't set TYPE_ARG_TYPES in this case */
4999
5000 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
51e29401 5001 {
3f75a254 5002 if (!funcdef_flag)
fcf73884 5003 pedwarn (0, "parameter names (without types) in function declaration");
51e29401 5004
f8893e47
JM
5005 arg_info->parms = arg_info->types;
5006 arg_info->types = 0;
51e29401
RS
5007 return 0;
5008 }
5009 else
5010 {
77dbdb57
ZW
5011 tree parm, type, typelt;
5012 unsigned int parmno;
5013
bba5733f
JM
5014 /* If there is a parameter of incomplete type in a definition,
5015 this is an error. In a declaration this is valid, and a
5016 struct or union type may be completed later, before any calls
5017 or definition of the function. In the case where the tag was
5018 first declared within the parameter list, a warning has
5019 already been given. If a parameter has void type, then
5020 however the function cannot be defined or called, so
5021 warn. */
77dbdb57 5022
f8893e47 5023 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
eb1dfbb2 5024 parm;
77dbdb57
ZW
5025 parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
5026 {
5027 type = TREE_VALUE (typelt);
5028 if (type == error_mark_node)
5029 continue;
5030
5031 if (!COMPLETE_TYPE_P (type))
5032 {
5033 if (funcdef_flag)
5034 {
5035 if (DECL_NAME (parm))
dee15844
JM
5036 error ("parameter %u (%q+D) has incomplete type",
5037 parmno, parm);
77dbdb57
ZW
5038 else
5039 error ("%Jparameter %u has incomplete type",
5040 parm, parmno);
51e29401 5041
77dbdb57
ZW
5042 TREE_VALUE (typelt) = error_mark_node;
5043 TREE_TYPE (parm) = error_mark_node;
5044 }
bba5733f 5045 else if (VOID_TYPE_P (type))
77dbdb57
ZW
5046 {
5047 if (DECL_NAME (parm))
dee15844
JM
5048 warning (0, "parameter %u (%q+D) has void type",
5049 parmno, parm);
77dbdb57 5050 else
d4ee4d25 5051 warning (0, "%Jparameter %u has void type",
77dbdb57
ZW
5052 parm, parmno);
5053 }
5054 }
7c47d6e9
JM
5055
5056 if (DECL_NAME (parm) && TREE_USED (parm))
5057 warn_if_shadowing (parm);
77dbdb57
ZW
5058 }
5059 return arg_types;
51e29401
RS
5060 }
5061}
5062
f8893e47
JM
5063/* Take apart the current scope and return a c_arg_info structure with
5064 info on a parameter list just parsed.
77dbdb57 5065
f8893e47 5066 This structure is later fed to 'grokparms' and 'store_parm_decls'.
51e29401 5067
f75fbaf7
ZW
5068 ELLIPSIS being true means the argument list ended in '...' so don't
5069 append a sentinel (void_list_node) to the end of the type-list. */
51e29401 5070
f8893e47 5071struct c_arg_info *
f75fbaf7 5072get_parm_info (bool ellipsis)
51e29401 5073{
f75fbaf7 5074 struct c_binding *b = current_scope->bindings;
f8893e47
JM
5075 struct c_arg_info *arg_info = XOBNEW (&parser_obstack,
5076 struct c_arg_info);
f75fbaf7
ZW
5077 tree parms = 0;
5078 tree tags = 0;
5079 tree types = 0;
5080 tree others = 0;
5081
55d54003
ZW
5082 static bool explained_incomplete_types = false;
5083 bool gave_void_only_once_err = false;
5084
f8893e47
JM
5085 arg_info->parms = 0;
5086 arg_info->tags = 0;
5087 arg_info->types = 0;
5088 arg_info->others = 0;
3542a5c0 5089 arg_info->pending_sizes = 0;
52ffd86e 5090 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
f8893e47 5091
f75fbaf7
ZW
5092 /* The bindings in this scope must not get put into a block.
5093 We will take care of deleting the binding nodes. */
5094 current_scope->bindings = 0;
51e29401 5095
f75fbaf7
ZW
5096 /* This function is only called if there was *something* on the
5097 parameter list. */
366de0ce 5098 gcc_assert (b);
55d54003 5099
f75fbaf7
ZW
5100 /* A parameter list consisting solely of 'void' indicates that the
5101 function takes no arguments. But if the 'void' is qualified
5102 (by 'const' or 'volatile'), or has a storage class specifier
5103 ('register'), then the behavior is undefined; issue an error.
5104 Typedefs for 'void' are OK (see DR#157). */
c22cacf3 5105 if (b->prev == 0 /* one binding */
f75fbaf7
ZW
5106 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
5107 && !DECL_NAME (b->decl) /* anonymous */
5108 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
5109 {
5110 if (TREE_THIS_VOLATILE (b->decl)
5111 || TREE_READONLY (b->decl)
5baeaac0 5112 || C_DECL_REGISTER (b->decl))
bda67431 5113 error ("%<void%> as only parameter may not be qualified");
55d54003 5114
f75fbaf7
ZW
5115 /* There cannot be an ellipsis. */
5116 if (ellipsis)
bda67431 5117 error ("%<void%> must be the only parameter");
51e29401 5118
f8893e47 5119 arg_info->types = void_list_node;
f75fbaf7 5120 return arg_info;
51e29401
RS
5121 }
5122
f75fbaf7
ZW
5123 if (!ellipsis)
5124 types = void_list_node;
51e29401 5125
f75fbaf7
ZW
5126 /* Break up the bindings list into parms, tags, types, and others;
5127 apply sanity checks; purge the name-to-decl bindings. */
5128 while (b)
51e29401 5129 {
f75fbaf7
ZW
5130 tree decl = b->decl;
5131 tree type = TREE_TYPE (decl);
55d54003 5132 const char *keyword;
55d54003 5133
f75fbaf7 5134 switch (TREE_CODE (decl))
6645c3fa 5135 {
f75fbaf7
ZW
5136 case PARM_DECL:
5137 if (b->id)
5138 {
366de0ce 5139 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
f75fbaf7
ZW
5140 I_SYMBOL_BINDING (b->id) = b->shadowed;
5141 }
55d54003 5142
f75fbaf7
ZW
5143 /* Check for forward decls that never got their actual decl. */
5144 if (TREE_ASM_WRITTEN (decl))
dee15844 5145 error ("parameter %q+D has just a forward declaration", decl);
f75fbaf7
ZW
5146 /* Check for (..., void, ...) and issue an error. */
5147 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
5148 {
5149 if (!gave_void_only_once_err)
5150 {
bda67431 5151 error ("%<void%> must be the only parameter");
f75fbaf7
ZW
5152 gave_void_only_once_err = true;
5153 }
5154 }
5155 else
5156 {
5157 /* Valid parameter, add it to the list. */
5158 TREE_CHAIN (decl) = parms;
5159 parms = decl;
5160
5161 /* Since there is a prototype, args are passed in their
5162 declared types. The back end may override this later. */
5163 DECL_ARG_TYPE (decl) = type;
5164 types = tree_cons (0, type, types);
5165 }
5166 break;
55d54003 5167
56141a6a 5168 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
f75fbaf7 5169 case UNION_TYPE: keyword = "union"; goto tag;
56141a6a 5170 case RECORD_TYPE: keyword = "struct"; goto tag;
f75fbaf7
ZW
5171 tag:
5172 /* Types may not have tag-names, in which case the type
5173 appears in the bindings list with b->id NULL. */
5174 if (b->id)
5175 {
366de0ce 5176 gcc_assert (I_TAG_BINDING (b->id) == b);
f75fbaf7
ZW
5177 I_TAG_BINDING (b->id) = b->shadowed;
5178 }
55d54003 5179
f75fbaf7
ZW
5180 /* Warn about any struct, union or enum tags defined in a
5181 parameter list. The scope of such types is limited to
5182 the parameter list, which is rarely if ever desirable
5183 (it's impossible to call such a function with type-
5184 correct arguments). An anonymous union parm type is
5185 meaningful as a GNU extension, so don't warn for that. */
5186 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
5187 {
5188 if (b->id)
5189 /* The %s will be one of 'struct', 'union', or 'enum'. */
d4ee4d25 5190 warning (0, "%<%s %E%> declared inside parameter list",
f75fbaf7
ZW
5191 keyword, b->id);
5192 else
5193 /* The %s will be one of 'struct', 'union', or 'enum'. */
d4ee4d25 5194 warning (0, "anonymous %s declared inside parameter list",
f75fbaf7 5195 keyword);
118a3a8b 5196
3f75a254 5197 if (!explained_incomplete_types)
f75fbaf7 5198 {
d4ee4d25 5199 warning (0, "its scope is only this definition or declaration,"
f75fbaf7
ZW
5200 " which is probably not what you want");
5201 explained_incomplete_types = true;
5202 }
5203 }
55d54003 5204
f75fbaf7
ZW
5205 tags = tree_cons (b->id, decl, tags);
5206 break;
5207
5208 case CONST_DECL:
5209 case TYPE_DECL:
ee060229 5210 case FUNCTION_DECL:
f75fbaf7
ZW
5211 /* CONST_DECLs appear here when we have an embedded enum,
5212 and TYPE_DECLs appear here when we have an embedded struct
5213 or union. No warnings for this - we already warned about the
ee060229
JM
5214 type itself. FUNCTION_DECLs appear when there is an implicit
5215 function declaration in the parameter list. */
5216
18d5f982
ZW
5217 TREE_CHAIN (decl) = others;
5218 others = decl;
5219 /* fall through */
5220
5221 case ERROR_MARK:
5222 /* error_mark_node appears here when we have an undeclared
5223 variable. Just throw it away. */
f75fbaf7
ZW
5224 if (b->id)
5225 {
366de0ce 5226 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
f75fbaf7
ZW
5227 I_SYMBOL_BINDING (b->id) = b->shadowed;
5228 }
f75fbaf7
ZW
5229 break;
5230
5231 /* Other things that might be encountered. */
5232 case LABEL_DECL:
f75fbaf7 5233 case VAR_DECL:
f75fbaf7 5234 default:
366de0ce 5235 gcc_unreachable ();
f75fbaf7
ZW
5236 }
5237
5238 b = free_binding_and_advance (b);
55d54003
ZW
5239 }
5240
f8893e47
JM
5241 arg_info->parms = parms;
5242 arg_info->tags = tags;
5243 arg_info->types = types;
5244 arg_info->others = others;
3542a5c0 5245 arg_info->pending_sizes = get_pending_sizes ();
f75fbaf7 5246 return arg_info;
51e29401
RS
5247}
5248\f
5249/* Get the struct, enum or union (CODE says which) with tag NAME.
81da229b
JM
5250 Define the tag as a forward-reference if it is not defined.
5251 Return a c_typespec structure for the type specifier. */
51e29401 5252
81da229b
JM
5253struct c_typespec
5254parser_xref_tag (enum tree_code code, tree name)
51e29401 5255{
81da229b 5256 struct c_typespec ret;
51e29401
RS
5257 /* If a cross reference is requested, look up the type
5258 already defined for this tag and return it. */
5259
339a28b9 5260 tree ref = lookup_tag (code, name, 0);
f18b70f5
JM
5261 /* If this is the right type of tag, return what we found.
5262 (This reference will be shadowed by shadow_tag later if appropriate.)
5263 If this is the wrong type of tag, do not return it. If it was the
f8521984
ZW
5264 wrong type in the same scope, we will have had an error
5265 message already; if in a different scope and declaring
f18b70f5 5266 a name, pending_xref_error will give an error message; but if in a
f8521984 5267 different scope and not declaring a name, this tag should
f18b70f5
JM
5268 shadow the previous declaration of a different type of tag, and
5269 this would not work properly if we return the reference found.
5270 (For example, with "struct foo" in an outer scope, "union foo;"
5271 must shadow that tag with a new one of union type.) */
81da229b 5272 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
f18b70f5 5273 if (ref && TREE_CODE (ref) == code)
81da229b
JM
5274 {
5275 ret.spec = ref;
5276 return ret;
5277 }
51e29401 5278
51e29401
RS
5279 /* If no such tag is yet defined, create a forward-reference node
5280 and record it as the "definition".
5281 When a real declaration of this type is found,
5282 the forward-reference will be altered into a real type. */
5283
5284 ref = make_node (code);
5285 if (code == ENUMERAL_TYPE)
5286 {
51e29401
RS
5287 /* Give the type a default layout like unsigned int
5288 to avoid crashing if it does not get defined. */
5289 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
5290 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
11cf4d18 5291 TYPE_USER_ALIGN (ref) = 0;
8df83eae 5292 TYPE_UNSIGNED (ref) = 1;
51e29401
RS
5293 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
5294 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
5295 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
5296 }
5297
5298 pushtag (name, ref);
5299
81da229b
JM
5300 ret.spec = ref;
5301 return ret;
5302}
5303
5304/* Get the struct, enum or union (CODE says which) with tag NAME.
5305 Define the tag as a forward-reference if it is not defined.
5306 Return a tree for the type. */
5307
5308tree
5309xref_tag (enum tree_code code, tree name)
5310{
5311 return parser_xref_tag (code, name).spec;
51e29401
RS
5312}
5313\f
f8521984 5314/* Make sure that the tag NAME is defined *in the current scope*
51e29401 5315 at least as a forward reference.
4dd7201e 5316 CODE says which kind of tag NAME ought to be. */
51e29401
RS
5317
5318tree
35b1a6fa 5319start_struct (enum tree_code code, tree name)
51e29401 5320{
f8521984 5321 /* If there is already a tag defined at this scope
51e29401
RS
5322 (as a forward reference), just return it. */
5323
b3694847 5324 tree ref = 0;
51e29401
RS
5325
5326 if (name != 0)
339a28b9 5327 ref = lookup_tag (code, name, 1);
51e29401
RS
5328 if (ref && TREE_CODE (ref) == code)
5329 {
085e33aa 5330 if (TYPE_SIZE (ref))
c22cacf3 5331 {
53fcdc76 5332 if (code == UNION_TYPE)
c51a1ba9 5333 error ("redefinition of %<union %E%>", name);
c22cacf3 5334 else
c51a1ba9 5335 error ("redefinition of %<struct %E%>", name);
35b1a6fa 5336 }
085e33aa
JM
5337 else if (C_TYPE_BEING_DEFINED (ref))
5338 {
5339 if (code == UNION_TYPE)
c51a1ba9 5340 error ("nested redefinition of %<union %E%>", name);
c22cacf3 5341 else
c51a1ba9 5342 error ("nested redefinition of %<struct %E%>", name);
71113fcd
GK
5343 /* Don't create structures that contain themselves. */
5344 ref = NULL_TREE;
085e33aa 5345 }
51e29401
RS
5346 }
5347
71113fcd
GK
5348 /* Otherwise create a forward-reference just so the tag is in scope. */
5349
5350 if (ref == NULL_TREE || TREE_CODE (ref) != code)
5351 {
5326cd3d
NS
5352 ref = make_node (code);
5353 pushtag (name, ref);
5354 }
35b1a6fa 5355
51e29401 5356 C_TYPE_BEING_DEFINED (ref) = 1;
02eb6e90 5357 TYPE_PACKED (ref) = flag_pack_struct;
51e29401
RS
5358 return ref;
5359}
5360
f8893e47 5361/* Process the specs, declarator and width (NULL if omitted)
51e29401 5362 of a structure component, returning a FIELD_DECL node.
2ff7cce4 5363 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
b9baeecd 5364 DECL_ATTRS is as for grokdeclarator.
51e29401
RS
5365
5366 This is done during the parsing of the struct declaration.
5367 The FIELD_DECL nodes are chained together and the lot of them
5368 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
5369
5370tree
deb176fa 5371grokfield (struct c_declarator *declarator, struct c_declspecs *declspecs,
b9baeecd 5372 tree width, tree *decl_attrs)
51e29401
RS
5373{
5374 tree value;
5375
f8893e47
JM
5376 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
5377 && width == NULL_TREE)
3e96a2fd 5378 {
750491fc
RH
5379 /* This is an unnamed decl.
5380
5381 If we have something of the form "union { list } ;" then this
5382 is the anonymous union extension. Similarly for struct.
5383
5384 If this is something of the form "struct foo;", then
5385 If MS extensions are enabled, this is handled as an
5386 anonymous struct.
5387 Otherwise this is a forward declaration of a structure tag.
5388
5389 If this is something of the form "foo;" and foo is a TYPE_DECL, then
5390 If MS extensions are enabled and foo names a structure, then
5391 again this is an anonymous struct.
5392 Otherwise this is an error.
5393
95bd1dd7 5394 Oh what a horrid tangled web we weave. I wonder if MS consciously
750491fc
RH
5395 took this from Plan 9 or if it was an accident of implementation
5396 that took root before someone noticed the bug... */
5397
deb176fa 5398 tree type = declspecs->type;
216a5f1b
JM
5399 bool type_ok = (TREE_CODE (type) == RECORD_TYPE
5400 || TREE_CODE (type) == UNION_TYPE);
5401 bool ok = false;
85d49058 5402
216a5f1b 5403 if (type_ok
616aeba2 5404 && (flag_ms_extensions || !declspecs->typedef_p))
750491fc
RH
5405 {
5406 if (flag_ms_extensions)
216a5f1b 5407 ok = true;
750491fc 5408 else if (flag_iso)
216a5f1b 5409 ok = false;
750491fc 5410 else if (TYPE_NAME (type) == NULL)
216a5f1b 5411 ok = true;
750491fc 5412 else
216a5f1b 5413 ok = false;
750491fc 5414 }
216a5f1b 5415 if (!ok)
3e96a2fd 5416 {
fcf73884 5417 pedwarn (0, "declaration does not declare anything");
3e96a2fd
DD
5418 return NULL_TREE;
5419 }
fcf73884 5420 pedwarn (OPT_pedantic, "ISO C doesn%'t support unnamed structs/unions");
3e96a2fd
DD
5421 }
5422
60919bce 5423 value = grokdeclarator (declarator, declspecs, FIELD, false,
b9baeecd
RS
5424 width ? &width : NULL, decl_attrs,
5425 DEPRECATED_NORMAL);
51e29401 5426
8d9bfdc5 5427 finish_decl (value, NULL_TREE, NULL_TREE);
dfd48d76 5428 DECL_INITIAL (value) = width;
51e29401
RS
5429
5430 return value;
5431}
5432\f
66ea6f4c
RH
5433/* Generate an error for any duplicate field names in FIELDLIST. Munge
5434 the list such that this does not present a problem later. */
5435
5436static void
5437detect_field_duplicates (tree fieldlist)
5438{
5439 tree x, y;
5440 int timeout = 10;
5441
5442 /* First, see if there are more than "a few" fields.
5443 This is trivially true if there are zero or one fields. */
5444 if (!fieldlist)
5445 return;
5446 x = TREE_CHAIN (fieldlist);
5447 if (!x)
5448 return;
5449 do {
5450 timeout--;
5451 x = TREE_CHAIN (x);
5452 } while (timeout > 0 && x);
5453
5454 /* If there were "few" fields, avoid the overhead of allocating
5455 a hash table. Instead just do the nested traversal thing. */
5456 if (timeout > 0)
5457 {
5458 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
5459 if (DECL_NAME (x))
5460 {
5461 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
5462 if (DECL_NAME (y) == DECL_NAME (x))
5463 {
dee15844 5464 error ("duplicate member %q+D", x);
66ea6f4c
RH
5465 DECL_NAME (x) = NULL_TREE;
5466 }
5467 }
5468 }
5469 else
5470 {
5471 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
5472 void **slot;
5473
5474 for (x = fieldlist; x ; x = TREE_CHAIN (x))
5475 if ((y = DECL_NAME (x)) != 0)
5476 {
5477 slot = htab_find_slot (htab, y, INSERT);
5478 if (*slot)
5479 {
dee15844 5480 error ("duplicate member %q+D", x);
66ea6f4c
RH
5481 DECL_NAME (x) = NULL_TREE;
5482 }
5483 *slot = y;
5484 }
5485
5486 htab_delete (htab);
5487 }
5488}
5489
51e29401 5490/* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
7a0347ff 5491 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
4dd7201e 5492 ATTRIBUTES are attributes to be applied to the structure. */
51e29401
RS
5493
5494tree
35b1a6fa 5495finish_struct (tree t, tree fieldlist, tree attributes)
51e29401 5496{
b3694847 5497 tree x;
f75fbaf7 5498 bool toplevel = file_scope == current_scope;
ffc5c6a9 5499 int saw_named_field;
51e29401
RS
5500
5501 /* If this type was previously laid out as a forward reference,
5502 make sure we lay it out again. */
5503
5504 TYPE_SIZE (t) = 0;
5505
91d231cb 5506 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
10861e9a 5507
9590fa72
RK
5508 if (pedantic)
5509 {
5510 for (x = fieldlist; x; x = TREE_CHAIN (x))
5511 if (DECL_NAME (x) != 0)
5512 break;
5513
5514 if (x == 0)
cc0b9d52
JM
5515 {
5516 if (TREE_CODE (t) == UNION_TYPE)
5517 {
5518 if (fieldlist)
fcf73884 5519 pedwarn (OPT_pedantic, "union has no named members");
cc0b9d52 5520 else
fcf73884 5521 pedwarn (OPT_pedantic, "union has no members");
cc0b9d52
JM
5522 }
5523 else
5524 {
5525 if (fieldlist)
fcf73884 5526 pedwarn (OPT_pedantic, "struct has no named members");
cc0b9d52 5527 else
fcf73884 5528 pedwarn (OPT_pedantic, "struct has no members");
cc0b9d52
JM
5529 }
5530 }
9590fa72 5531 }
51e29401 5532
dfd48d76 5533 /* Install struct as DECL_CONTEXT of each field decl.
bc15d0ef
JM
5534 Also process specified field sizes, found in the DECL_INITIAL,
5535 storing 0 there after the type has been changed to precision equal
5536 to its width, rather than the precision of the specified standard
5537 type. (Correct layout requires the original type to have been preserved
5538 until now.) */
51e29401 5539
ffc5c6a9 5540 saw_named_field = 0;
51e29401
RS
5541 for (x = fieldlist; x; x = TREE_CHAIN (x))
5542 {
4271a6f3
VR
5543 if (TREE_TYPE (x) == error_mark_node)
5544 continue;
5545
51e29401 5546 DECL_CONTEXT (x) = t;
646c0835
NS
5547
5548 if (TYPE_PACKED (t) && TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
5549 DECL_PACKED (x) = 1;
51e29401
RS
5550
5551 /* If any field is const, the structure type is pseudo-const. */
5552 if (TREE_READONLY (x))
5553 C_TYPE_FIELDS_READONLY (t) = 1;
5554 else
5555 {
5556 /* A field that is pseudo-const makes the structure likewise. */
5557 tree t1 = TREE_TYPE (x);
5558 while (TREE_CODE (t1) == ARRAY_TYPE)
5559 t1 = TREE_TYPE (t1);
5560 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5561 && C_TYPE_FIELDS_READONLY (t1))
5562 C_TYPE_FIELDS_READONLY (t) = 1;
5563 }
5564
5565 /* Any field that is volatile means variables of this type must be
5566 treated in some ways as volatile. */
5567 if (TREE_THIS_VOLATILE (x))
5568 C_TYPE_FIELDS_VOLATILE (t) = 1;
5569
5570 /* Any field of nominal variable size implies structure is too. */
5571 if (C_DECL_VARIABLE_SIZE (x))
5572 C_TYPE_VARIABLE_SIZE (t) = 1;
5573
dfd48d76
NB
5574 if (DECL_INITIAL (x))
5575 {
2ff7cce4
JM
5576 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5577 DECL_SIZE (x) = bitsize_int (width);
5578 DECL_BIT_FIELD (x) = 1;
5579 SET_DECL_C_BIT_FIELD (x);
dfd48d76
NB
5580 }
5581
ffc5c6a9
RH
5582 /* Detect flexible array member in an invalid context. */
5583 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5584 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5585 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5586 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5587 {
5588 if (TREE_CODE (t) == UNION_TYPE)
d915eec0
JJ
5589 {
5590 error ("%Jflexible array member in union", x);
5591 TREE_TYPE (x) = error_mark_node;
5592 }
ffc5c6a9 5593 else if (TREE_CHAIN (x) != NULL_TREE)
d915eec0
JJ
5594 {
5595 error ("%Jflexible array member not at end of struct", x);
5596 TREE_TYPE (x) = error_mark_node;
5597 }
3f75a254 5598 else if (!saw_named_field)
d915eec0
JJ
5599 {
5600 error ("%Jflexible array member in otherwise empty struct", x);
5601 TREE_TYPE (x) = error_mark_node;
5602 }
ffc5c6a9 5603 }
2984fe64 5604
ec3e6835 5605 if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE
2984fe64 5606 && flexible_array_type_p (TREE_TYPE (x)))
fcf73884
MLI
5607 pedwarn (OPT_pedantic,
5608 "%Jinvalid use of structure with flexible array member", x);
2984fe64 5609
ffc5c6a9
RH
5610 if (DECL_NAME (x))
5611 saw_named_field = 1;
05bccae2 5612 }
51e29401 5613
66ea6f4c 5614 detect_field_duplicates (fieldlist);
51e29401
RS
5615
5616 /* Now we have the nearly final fieldlist. Record it,
5617 then lay out the structure or union (including the fields). */
5618
5619 TYPE_FIELDS (t) = fieldlist;
5620
5621 layout_type (t);
5622
bc15d0ef 5623 /* Give bit-fields their proper types. */
6fbfac92
JM
5624 {
5625 tree *fieldlistp = &fieldlist;
07b983cd 5626 while (*fieldlistp)
bc15d0ef
JM
5627 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
5628 && TREE_TYPE (*fieldlistp) != error_mark_node)
5629 {
5630 unsigned HOST_WIDE_INT width
5631 = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
5632 tree type = TREE_TYPE (*fieldlistp);
5633 if (width != TYPE_PRECISION (type))
48cc8d3b
RH
5634 {
5635 TREE_TYPE (*fieldlistp)
0b359b01 5636 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
48cc8d3b
RH
5637 DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
5638 }
bc15d0ef
JM
5639 DECL_INITIAL (*fieldlistp) = 0;
5640 }
6fbfac92
JM
5641 else
5642 fieldlistp = &TREE_CHAIN (*fieldlistp);
5643 }
51e29401 5644
ffc5c6a9
RH
5645 /* Now we have the truly final field list.
5646 Store it in this type and in the variants. */
51e29401
RS
5647
5648 TYPE_FIELDS (t) = fieldlist;
5649
d07605f5 5650 /* If there are lots of fields, sort so we can look through them fast.
6614fd40 5651 We arbitrarily consider 16 or more elts to be "a lot". */
d07605f5
AP
5652
5653 {
5654 int len = 0;
5655
5656 for (x = fieldlist; x; x = TREE_CHAIN (x))
5657 {
c22cacf3
MS
5658 if (len > 15 || DECL_NAME (x) == NULL)
5659 break;
5660 len += 1;
d07605f5
AP
5661 }
5662
5663 if (len > 15)
5664 {
c22cacf3
MS
5665 tree *field_array;
5666 struct lang_type *space;
5667 struct sorted_fields_type *space2;
e13e48e7 5668
c22cacf3 5669 len += list_length (x);
e13e48e7 5670
c22cacf3
MS
5671 /* Use the same allocation policy here that make_node uses, to
5672 ensure that this lives as long as the rest of the struct decl.
5673 All decls in an inline function need to be saved. */
e13e48e7 5674
c22cacf3
MS
5675 space = GGC_CNEW (struct lang_type);
5676 space2 = GGC_NEWVAR (struct sorted_fields_type,
5d038c4c 5677 sizeof (struct sorted_fields_type) + len * sizeof (tree));
e13e48e7 5678
c22cacf3 5679 len = 0;
d07605f5
AP
5680 space->s = space2;
5681 field_array = &space2->elts[0];
c22cacf3
MS
5682 for (x = fieldlist; x; x = TREE_CHAIN (x))
5683 {
5684 field_array[len++] = x;
5685
5686 /* If there is anonymous struct or union, break out of the loop. */
5687 if (DECL_NAME (x) == NULL)
5688 break;
5689 }
5690 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5691 if (x == NULL)
5692 {
5693 TYPE_LANG_SPECIFIC (t) = space;
5694 TYPE_LANG_SPECIFIC (t)->s->len = len;
5695 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5696 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5697 }
d07605f5
AP
5698 }
5699 }
e13e48e7 5700
51e29401
RS
5701 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5702 {
5703 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5704 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
692a7aa3
JM
5705 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
5706 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
5707 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
51e29401
RS
5708 }
5709
1604422c
RK
5710 /* If this was supposed to be a transparent union, but we can't
5711 make it one, warn and turn off the flag. */
5712 if (TREE_CODE (t) == UNION_TYPE
5713 && TYPE_TRANSPARENT_UNION (t)
eb3490b9 5714 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
1604422c
RK
5715 {
5716 TYPE_TRANSPARENT_UNION (t) = 0;
d4ee4d25 5717 warning (0, "union cannot be made transparent");
1604422c
RK
5718 }
5719
51e29401
RS
5720 /* If this structure or union completes the type of any previous
5721 variable declaration, lay it out and output its rtl. */
bf7a697f
ZW
5722 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5723 x;
5724 x = TREE_CHAIN (x))
51e29401 5725 {
bf7a697f
ZW
5726 tree decl = TREE_VALUE (x);
5727 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5728 layout_array_type (TREE_TYPE (decl));
5729 if (TREE_CODE (decl) != TYPE_DECL)
5730 {
5731 layout_decl (decl, 0);
5732 if (c_dialect_objc ())
5733 objc_check_decl (decl);
0e6df31e 5734 rest_of_decl_compilation (decl, toplevel, 0);
3f75a254 5735 if (!toplevel)
bf7a697f 5736 expand_decl (decl);
51e29401
RS
5737 }
5738 }
bf7a697f 5739 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
51e29401 5740
51e29401
RS
5741 /* Finish debugging output for this type. */
5742 rest_of_type_compilation (t, toplevel);
5743
ca6af4f8
RH
5744 /* If we're inside a function proper, i.e. not file-scope and not still
5745 parsing parameters, then arrange for the size of a variable sized type
5746 to be bound now. */
52ffd86e 5747 if (cur_stmt_list && variably_modified_type_p (t, NULL_TREE))
ca6af4f8
RH
5748 add_stmt (build_stmt (DECL_EXPR, build_decl (TYPE_DECL, NULL, t)));
5749
51e29401
RS
5750 return t;
5751}
5752
5753/* Lay out the type T, and its element type, and so on. */
5754
5755static void
35b1a6fa 5756layout_array_type (tree t)
51e29401
RS
5757{
5758 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5759 layout_array_type (TREE_TYPE (t));
5760 layout_type (t);
5761}
5762\f
5763/* Begin compiling the definition of an enumeration type.
5764 NAME is its name (or null if anonymous).
5765 Returns the type object, as yet incomplete.
5766 Also records info about it so that build_enumerator
5767 may be used to declare the individual values as they are read. */
5768
5769tree
7114359f 5770start_enum (struct c_enum_contents *the_enum, tree name)
51e29401 5771{
b3694847 5772 tree enumtype = 0;
51e29401
RS
5773
5774 /* If this is the real definition for a previous forward reference,
5775 fill in the contents in the same object that used to be the
5776 forward reference. */
5777
5778 if (name != 0)
339a28b9 5779 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
51e29401
RS
5780
5781 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5782 {
5783 enumtype = make_node (ENUMERAL_TYPE);
5784 pushtag (name, enumtype);
5785 }
5786
085e33aa 5787 if (C_TYPE_BEING_DEFINED (enumtype))
c51a1ba9 5788 error ("nested redefinition of %<enum %E%>", name);
085e33aa 5789
51e29401
RS
5790 C_TYPE_BEING_DEFINED (enumtype) = 1;
5791
5792 if (TYPE_VALUES (enumtype) != 0)
5793 {
5794 /* This enum is a named one that has been declared already. */
c51a1ba9 5795 error ("redeclaration of %<enum %E%>", name);
51e29401
RS
5796
5797 /* Completely replace its old definition.
5798 The old enumerators remain defined, however. */
5799 TYPE_VALUES (enumtype) = 0;
5800 }
5801
7114359f
TT
5802 the_enum->enum_next_value = integer_zero_node;
5803 the_enum->enum_overflow = 0;
51e29401 5804
02eb6e90
RK
5805 if (flag_short_enums)
5806 TYPE_PACKED (enumtype) = 1;
5807
51e29401
RS
5808 return enumtype;
5809}
5810
5811/* After processing and defining all the values of an enumeration type,
5812 install their decls in the enumeration type and finish it off.
10861e9a
RK
5813 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5814 and ATTRIBUTES are the specified attributes.
51e29401
RS
5815 Returns ENUMTYPE. */
5816
5817tree
35b1a6fa 5818finish_enum (tree enumtype, tree values, tree attributes)
51e29401 5819{
b3694847 5820 tree pair, tem;
6de9cd9a 5821 tree minnode = 0, maxnode = 0;
cb3ca04e 5822 int precision, unsign;
f75fbaf7 5823 bool toplevel = (file_scope == current_scope);
6de9cd9a 5824 struct lang_type *lt;
51e29401 5825
91d231cb 5826 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
10861e9a 5827
51e29401
RS
5828 /* Calculate the maximum value of any enumerator in this type. */
5829
59116212
RK
5830 if (values == error_mark_node)
5831 minnode = maxnode = integer_zero_node;
5832 else
f500253d 5833 {
cb3ca04e
ZW
5834 minnode = maxnode = TREE_VALUE (values);
5835 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
f500253d 5836 {
cb3ca04e
ZW
5837 tree value = TREE_VALUE (pair);
5838 if (tree_int_cst_lt (maxnode, value))
5839 maxnode = value;
5840 if (tree_int_cst_lt (value, minnode))
5841 minnode = value;
f500253d 5842 }
cb3ca04e 5843 }
f500253d 5844
cb3ca04e
ZW
5845 /* Construct the final type of this enumeration. It is the same
5846 as one of the integral types - the narrowest one that fits, except
5847 that normally we only go as narrow as int - and signed iff any of
5848 the values are negative. */
5849 unsign = (tree_int_cst_sgn (minnode) >= 0);
5850 precision = MAX (min_precision (minnode, unsign),
5851 min_precision (maxnode, unsign));
ec8465a5 5852
1ada4cd0 5853 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
cb3ca04e 5854 {
6de9cd9a
DN
5855 tem = c_common_type_for_size (precision, unsign);
5856 if (tem == NULL)
1ada4cd0 5857 {
d4ee4d25 5858 warning (0, "enumeration values exceed range of largest integer");
6de9cd9a 5859 tem = long_long_integer_type_node;
1ada4cd0 5860 }
f500253d 5861 }
1ada4cd0 5862 else
6de9cd9a 5863 tem = unsign ? unsigned_type_node : integer_type_node;
736f85c7 5864
6de9cd9a
DN
5865 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
5866 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
6de9cd9a 5867 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
51e29401 5868 TYPE_SIZE (enumtype) = 0;
ec8465a5
RK
5869
5870 /* If the precision of the type was specific with an attribute and it
5871 was too small, give an error. Otherwise, use it. */
5872 if (TYPE_PRECISION (enumtype))
5873 {
5874 if (precision > TYPE_PRECISION (enumtype))
5875 error ("specified mode too small for enumeral values");
5876 }
5877 else
5878 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
5879
51e29401
RS
5880 layout_type (enumtype);
5881
59116212 5882 if (values != error_mark_node)
75b46437 5883 {
cb3ca04e
ZW
5884 /* Change the type of the enumerators to be the enum type. We
5885 need to do this irrespective of the size of the enum, for
5886 proper type checking. Replace the DECL_INITIALs of the
5887 enumerators, and the value slots of the list, with copies
5888 that have the enum type; they cannot be modified in place
5889 because they may be shared (e.g. integer_zero_node) Finally,
5890 change the purpose slots to point to the names of the decls. */
59116212
RK
5891 for (pair = values; pair; pair = TREE_CHAIN (pair))
5892 {
cb3ca04e 5893 tree enu = TREE_PURPOSE (pair);
6de9cd9a 5894 tree ini = DECL_INITIAL (enu);
51e29401 5895
cb3ca04e 5896 TREE_TYPE (enu) = enumtype;
736f85c7
AO
5897
5898 /* The ISO C Standard mandates enumerators to have type int,
5899 even though the underlying type of an enum type is
5900 unspecified. Here we convert any enumerators that fit in
5901 an int to type int, to avoid promotions to unsigned types
5902 when comparing integers with enumerators that fit in the
5903 int range. When -pedantic is given, build_enumerator()
5904 would have already taken care of those that don't fit. */
6de9cd9a
DN
5905 if (int_fits_type_p (ini, integer_type_node))
5906 tem = integer_type_node;
736f85c7 5907 else
6de9cd9a
DN
5908 tem = enumtype;
5909 ini = convert (tem, ini);
cb3ca04e 5910
6de9cd9a 5911 DECL_INITIAL (enu) = ini;
cb3ca04e 5912 TREE_PURPOSE (pair) = DECL_NAME (enu);
6de9cd9a 5913 TREE_VALUE (pair) = ini;
cb3ca04e 5914 }
51e29401 5915
59116212
RK
5916 TYPE_VALUES (enumtype) = values;
5917 }
51e29401 5918
6de9cd9a
DN
5919 /* Record the min/max values so that we can warn about bit-field
5920 enumerations that are too small for the values. */
5d038c4c 5921 lt = GGC_CNEW (struct lang_type);
6de9cd9a
DN
5922 lt->enum_min = minnode;
5923 lt->enum_max = maxnode;
5924 TYPE_LANG_SPECIFIC (enumtype) = lt;
5925
fbe23ee7
RS
5926 /* Fix up all variant types of this enum type. */
5927 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5928 {
cb3ca04e
ZW
5929 if (tem == enumtype)
5930 continue;
fbe23ee7
RS
5931 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5932 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5933 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5934 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
def9b006 5935 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
fbe23ee7
RS
5936 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5937 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5938 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
11cf4d18 5939 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
8df83eae 5940 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
6de9cd9a 5941 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
fbe23ee7
RS
5942 }
5943
51e29401
RS
5944 /* Finish debugging output for this type. */
5945 rest_of_type_compilation (enumtype, toplevel);
5946
5947 return enumtype;
5948}
5949
5950/* Build and install a CONST_DECL for one value of the
5951 current enumeration type (one that was begun with start_enum).
5952 Return a tree-list containing the CONST_DECL and its value.
5953 Assignment of sequential values by default is handled here. */
5954
5955tree
7114359f 5956build_enumerator (struct c_enum_contents *the_enum, tree name, tree value)
51e29401 5957{
b3694847 5958 tree decl, type;
51e29401
RS
5959
5960 /* Validate and default VALUE. */
5961
90374cc2 5962 if (value != 0)
e681c5a1 5963 {
f75fbaf7
ZW
5964 /* Don't issue more errors for error_mark_node (i.e. an
5965 undeclared identifier) - just ignore the value expression. */
5966 if (value == error_mark_node)
5967 value = 0;
411ffa02
JM
5968 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
5969 || TREE_CODE (value) != INTEGER_CST)
25a1019f 5970 {
bda67431 5971 error ("enumerator value for %qE is not an integer constant", name);
f75fbaf7 5972 value = 0;
25a1019f 5973 }
e681c5a1
RS
5974 else
5975 {
f75fbaf7
ZW
5976 value = default_conversion (value);
5977 constant_expression_warning (value);
e681c5a1
RS
5978 }
5979 }
51e29401
RS
5980
5981 /* Default based on previous value. */
5982 /* It should no longer be possible to have NON_LVALUE_EXPR
5983 in the default. */
5984 if (value == 0)
93e3ba4f 5985 {
7114359f
TT
5986 value = the_enum->enum_next_value;
5987 if (the_enum->enum_overflow)
93e3ba4f
RS
5988 error ("overflow in enumeration values");
5989 }
51e29401 5990
3f75a254 5991 if (pedantic && !int_fits_type_p (value, integer_type_node))
51e29401 5992 {
fcf73884 5993 pedwarn (OPT_pedantic, "ISO C restricts enumerator values to range of %<int%>");
f75fbaf7
ZW
5994 /* XXX This causes -pedantic to change the meaning of the program.
5995 Remove? -zw 2004-03-15 */
736f85c7 5996 value = convert (integer_type_node, value);
51e29401
RS
5997 }
5998
5999 /* Set basis for default for next value. */
7114359f
TT
6000 the_enum->enum_next_value = build_binary_op (PLUS_EXPR, value,
6001 integer_one_node, 0);
6002 the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
51e29401
RS
6003
6004 /* Now create a declaration for the enum value name. */
6005
75b46437 6006 type = TREE_TYPE (value);
b0c48229
NB
6007 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
6008 TYPE_PRECISION (integer_type_node)),
6009 (TYPE_PRECISION (type)
6010 >= TYPE_PRECISION (integer_type_node)
8df83eae 6011 && TYPE_UNSIGNED (type)));
75b46437
RS
6012
6013 decl = build_decl (CONST_DECL, name, type);
0543d026 6014 DECL_INITIAL (decl) = convert (type, value);
51e29401
RS
6015 pushdecl (decl);
6016
4dd7201e 6017 return tree_cons (decl, value, NULL_TREE);
51e29401 6018}
8f17b5c5 6019
51e29401
RS
6020\f
6021/* Create the FUNCTION_DECL for a function definition.
f7a4cec0 6022 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5730cf69 6023 the declaration; they describe the function's name and the type it returns,
51e29401
RS
6024 but twisted together in a fashion that parallels the syntax of C.
6025
6026 This function creates a binding context for the function body
6027 as well as setting up the FUNCTION_DECL in current_function_decl.
6028
6029 Returns 1 on success. If the DECLARATOR is not suitable for a function
6030 (it defines a datum instead), we return 0, which tells
4dd7201e 6031 yyparse to report a parse error. */
51e29401
RS
6032
6033int
deb176fa 6034start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
f8893e47 6035 tree attributes)
51e29401
RS
6036{
6037 tree decl1, old_decl;
b785f485 6038 tree restype, resdecl;
187230a7
JM
6039 struct c_label_context_se *nstack_se;
6040 struct c_label_context_vm *nstack_vm;
51e29401 6041
0f41302f 6042 current_function_returns_value = 0; /* Assume, until we see it does. */
51e29401 6043 current_function_returns_null = 0;
5ce89b2e 6044 current_function_returns_abnormally = 0;
51e29401 6045 warn_about_return_type = 0;
506e2710
RH
6046 c_switch_stack = NULL;
6047
187230a7
JM
6048 nstack_se = XOBNEW (&parser_obstack, struct c_label_context_se);
6049 nstack_se->labels_def = NULL;
6050 nstack_se->labels_used = NULL;
6051 nstack_se->next = label_context_stack_se;
6052 label_context_stack_se = nstack_se;
6053
6054 nstack_vm = XOBNEW (&parser_obstack, struct c_label_context_vm);
6055 nstack_vm->labels_def = NULL;
6056 nstack_vm->labels_used = NULL;
6057 nstack_vm->scope = 0;
6058 nstack_vm->next = label_context_stack_vm;
6059 label_context_stack_vm = nstack_vm;
16ef3acc 6060
506e2710
RH
6061 /* Indicate no valid break/continue context by setting these variables
6062 to some non-null, non-label value. We'll notice and emit the proper
6063 error message in c_finish_bc_stmt. */
6064 c_break_label = c_cont_label = size_zero_node;
51e29401 6065
0da300cd 6066 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
b9baeecd 6067 &attributes, DEPRECATED_NORMAL);
51e29401
RS
6068
6069 /* If the declarator is not suitable for a function definition,
6070 cause a syntax error. */
6071 if (decl1 == 0)
d4d1cfd4
JM
6072 {
6073 label_context_stack_se = label_context_stack_se->next;
6074 label_context_stack_vm = label_context_stack_vm->next;
6075 return 0;
6076 }
51e29401 6077
59387d2e 6078 decl_attributes (&decl1, attributes, 0);
7665dfd4 6079
9162542e
AO
6080 if (DECL_DECLARED_INLINE_P (decl1)
6081 && DECL_UNINLINABLE (decl1)
6082 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
dee15844
JM
6083 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
6084 decl1);
9162542e 6085
4eb7fd83
JJ
6086 /* Handle gnu_inline attribute. */
6087 if (declspecs->inline_p
da1c7394 6088 && !flag_gnu89_inline
4eb7fd83 6089 && TREE_CODE (decl1) == FUNCTION_DECL
6cf59865
JJ
6090 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
6091 || current_function_decl))
4eb7fd83
JJ
6092 {
6093 if (declspecs->storage_class != csc_static)
6094 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
6095 }
4eb7fd83 6096
51e29401
RS
6097 announce_function (decl1);
6098
d0f062fb 6099 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
51e29401 6100 {
903f51d9 6101 error ("return type is an incomplete type");
51e29401
RS
6102 /* Make it return void instead. */
6103 TREE_TYPE (decl1)
6104 = build_function_type (void_type_node,
6105 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
6106 }
6107
6108 if (warn_about_return_type)
fcf73884
MLI
6109 pedwarn_c99 (flag_isoc99 ? 0
6110 : (warn_return_type ? OPT_Wreturn_type : OPT_Wimplicit_int),
6111 "return type defaults to %<int%>");
51e29401 6112
51e29401 6113 /* Make the init_value nonzero so pushdecl knows this is not tentative.
f75fbaf7 6114 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
51e29401
RS
6115 DECL_INITIAL (decl1) = error_mark_node;
6116
6117 /* If this definition isn't a prototype and we had a prototype declaration
46097c76 6118 before, copy the arg type info from that prototype. */
f75fbaf7 6119 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
d8890adf
JM
6120 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
6121 old_decl = 0;
6fb58bba
JM
6122 current_function_prototype_locus = UNKNOWN_LOCATION;
6123 current_function_prototype_built_in = false;
6124 current_function_prototype_arg_types = NULL_TREE;
6125 if (TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
6126 {
6127 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
6128 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
6129 TREE_TYPE (TREE_TYPE (old_decl))))
6130 {
6131 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
6132 TREE_TYPE (decl1));
6133 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
6134 current_function_prototype_built_in
6135 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
6136 current_function_prototype_arg_types
6137 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
6138 }
6139 if (TREE_PUBLIC (decl1))
6140 {
6141 /* If there is an external prototype declaration of this
6142 function, record its location but do not copy information
6143 to this decl. This may be an invisible declaration
6144 (built-in or in a scope which has finished) or simply
6145 have more refined argument types than any declaration
6146 found above. */
6147 struct c_binding *b;
6148 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
6149 if (B_IN_SCOPE (b, external_scope))
6150 break;
6151 if (b)
6152 {
6153 tree ext_decl, ext_type;
6154 ext_decl = b->decl;
6155 ext_type = b->type ? b->type : TREE_TYPE (ext_decl);
6156 if (TREE_CODE (ext_type) == FUNCTION_TYPE
6157 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
6158 TREE_TYPE (ext_type)))
6159 {
6160 current_function_prototype_locus
6161 = DECL_SOURCE_LOCATION (ext_decl);
6162 current_function_prototype_built_in
6163 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
6164 current_function_prototype_arg_types
6165 = TYPE_ARG_TYPES (ext_type);
6166 }
6167 }
6168 }
50a9145c 6169 }
51e29401
RS
6170
6171 /* Optionally warn of old-fashioned def with no previous prototype. */
6172 if (warn_strict_prototypes
1e55c0e2 6173 && old_decl != error_mark_node
51e29401 6174 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
339a28b9 6175 && C_DECL_ISNT_PROTOTYPE (old_decl))
3176a0c2
DD
6176 warning (OPT_Wstrict_prototypes,
6177 "function declaration isn%'t a prototype");
51e29401
RS
6178 /* Optionally warn of any global def with no previous prototype. */
6179 else if (warn_missing_prototypes
1e55c0e2 6180 && old_decl != error_mark_node
51e29401 6181 && TREE_PUBLIC (decl1)
3f75a254 6182 && !MAIN_NAME_P (DECL_NAME (decl1))
339a28b9 6183 && C_DECL_ISNT_PROTOTYPE (old_decl))
dee15844 6184 warning (OPT_Wmissing_prototypes, "no previous prototype for %q+D", decl1);
51e29401
RS
6185 /* Optionally warn of any def with no previous prototype
6186 if the function has already been used. */
6187 else if (warn_missing_prototypes
1e55c0e2
JM
6188 && old_decl != 0
6189 && old_decl != error_mark_node
6190 && TREE_USED (old_decl)
6fc7c517 6191 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
3176a0c2 6192 warning (OPT_Wmissing_prototypes,
dee15844 6193 "%q+D was used with no prototype before its definition", decl1);
1474fe46
RK
6194 /* Optionally warn of any global def with no previous declaration. */
6195 else if (warn_missing_declarations
6196 && TREE_PUBLIC (decl1)
6197 && old_decl == 0
3f75a254 6198 && !MAIN_NAME_P (DECL_NAME (decl1)))
dee15844
JM
6199 warning (OPT_Wmissing_declarations, "no previous declaration for %q+D",
6200 decl1);
1474fe46
RK
6201 /* Optionally warn of any def with no previous declaration
6202 if the function has already been used. */
6203 else if (warn_missing_declarations
1e55c0e2
JM
6204 && old_decl != 0
6205 && old_decl != error_mark_node
6206 && TREE_USED (old_decl)
339a28b9 6207 && C_DECL_IMPLICIT (old_decl))
3176a0c2 6208 warning (OPT_Wmissing_declarations,
dee15844 6209 "%q+D was used with no declaration before its definition", decl1);
51e29401 6210
51e29401
RS
6211 /* This function exists in static storage.
6212 (This does not mean `static' in the C sense!) */
6213 TREE_STATIC (decl1) = 1;
6214
6215 /* A nested function is not global. */
6216 if (current_function_decl != 0)
6217 TREE_PUBLIC (decl1) = 0;
6218
d05cc98e
GK
6219 /* This is the earliest point at which we might know the assembler
6220 name of the function. Thus, if it's set before this, die horribly. */
366de0ce 6221 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
d05cc98e
GK
6222
6223 /* If #pragma weak was used, mark the decl weak now. */
f75fbaf7 6224 if (current_scope == file_scope)
d05cc98e
GK
6225 maybe_apply_pragma_weak (decl1);
6226
6645c3fa 6227 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
5b47282c 6228 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
b8705e61 6229 {
b8705e61 6230 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
6645c3fa 6231 != integer_type_node)
fcf73884 6232 pedwarn (OPT_Wmain, "return type of %q+D is not %<int%>", decl1);
b8705e61 6233
a1e45ff0 6234 check_main_parameter_types(decl1);
b8705e61 6235
3f75a254 6236 if (!TREE_PUBLIC (decl1))
fcf73884 6237 pedwarn (OPT_Wmain, "%q+D is normally a non-static function", decl1);
b8705e61
RK
6238 }
6239
51e29401
RS
6240 /* Record the decl so that the function name is defined.
6241 If we already have a decl for this name, and it is a FUNCTION_DECL,
6242 use the old decl. */
6243
6244 current_function_decl = pushdecl (decl1);
6245
f75fbaf7 6246 push_scope ();
eb1dfbb2 6247 declare_parm_level ();
51e29401 6248
51e29401 6249 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
b785f485
RH
6250 resdecl = build_decl (RESULT_DECL, NULL_TREE, restype);
6251 DECL_ARTIFICIAL (resdecl) = 1;
6252 DECL_IGNORED_P (resdecl) = 1;
6253 DECL_RESULT (current_function_decl) = resdecl;
51e29401 6254
0ba8a114 6255 start_fname_decls ();
35b1a6fa 6256
51e29401
RS
6257 return 1;
6258}
51e29401 6259\f
66db6b62
ZW
6260/* Subroutine of store_parm_decls which handles new-style function
6261 definitions (prototype format). The parms already have decls, so we
6262 need only record them as in effect and complain if any redundant
6263 old-style parm decls were written. */
d4e43dff 6264static void
f8893e47 6265store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
51e29401 6266{
f75fbaf7 6267 tree decl;
51e29401 6268
f75fbaf7 6269 if (current_scope->bindings)
66db6b62 6270 {
ddd2d57e
RH
6271 error ("%Jold-style parameter declarations in prototyped "
6272 "function definition", fndecl);
51e29401 6273
66db6b62 6274 /* Get rid of the old-style declarations. */
f75fbaf7
ZW
6275 pop_scope ();
6276 push_scope ();
6277 }
6278 /* Don't issue this warning for nested functions, and don't issue this
6279 warning if we got here because ARG_INFO_TYPES was error_mark_node
6280 (this happens when a function definition has just an ellipsis in
6281 its parameter list). */
3176a0c2 6282 else if (!in_system_header && !current_function_scope
f8893e47 6283 && arg_info->types != error_mark_node)
3176a0c2
DD
6284 warning (OPT_Wtraditional,
6285 "%Jtraditional C rejects ISO C style function definitions",
f75fbaf7 6286 fndecl);
51e29401 6287
55d54003
ZW
6288 /* Now make all the parameter declarations visible in the function body.
6289 We can bypass most of the grunt work of pushdecl. */
f8893e47 6290 for (decl = arg_info->parms; decl; decl = TREE_CHAIN (decl))
66db6b62 6291 {
55d54003 6292 DECL_CONTEXT (decl) = current_function_decl;
f75fbaf7 6293 if (DECL_NAME (decl))
7c47d6e9
JM
6294 {
6295 bind (DECL_NAME (decl), decl, current_scope,
6296 /*invisible=*/false, /*nested=*/false);
6297 if (!TREE_USED (decl))
6298 warn_if_shadowing (decl);
6299 }
66db6b62 6300 else
f75fbaf7 6301 error ("%Jparameter name omitted", decl);
66db6b62 6302 }
26f943fd 6303
66db6b62 6304 /* Record the parameter list in the function declaration. */
f8893e47 6305 DECL_ARGUMENTS (fndecl) = arg_info->parms;
51e29401 6306
66db6b62 6307 /* Now make all the ancillary declarations visible, likewise. */
f8893e47 6308 for (decl = arg_info->others; decl; decl = TREE_CHAIN (decl))
55d54003
ZW
6309 {
6310 DECL_CONTEXT (decl) = current_function_decl;
f75fbaf7 6311 if (DECL_NAME (decl))
9aaabf8a
ZW
6312 bind (DECL_NAME (decl), decl, current_scope,
6313 /*invisible=*/false, /*nested=*/false);
55d54003 6314 }
26f943fd 6315
66db6b62 6316 /* And all the tag declarations. */
f8893e47 6317 for (decl = arg_info->tags; decl; decl = TREE_CHAIN (decl))
55d54003 6318 if (TREE_PURPOSE (decl))
9aaabf8a
ZW
6319 bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope,
6320 /*invisible=*/false, /*nested=*/false);
66db6b62 6321}
1f731749 6322
66db6b62
ZW
6323/* Subroutine of store_parm_decls which handles old-style function
6324 definitions (separate parameter list and declarations). */
51e29401 6325
66db6b62 6326static void
f8893e47 6327store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
66db6b62 6328{
f75fbaf7 6329 struct c_binding *b;
55d54003 6330 tree parm, decl, last;
f8893e47 6331 tree parmids = arg_info->parms;
820cc88f 6332 struct pointer_set_t *seen_args = pointer_set_create ();
66db6b62 6333
ccf08a6e 6334 if (!in_system_header)
44c21c7f
DD
6335 warning (OPT_Wold_style_definition, "%Jold-style function definition",
6336 fndecl);
f75fbaf7 6337
66db6b62
ZW
6338 /* Match each formal parameter name with its declaration. Save each
6339 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
6340 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6341 {
6342 if (TREE_VALUE (parm) == 0)
7a0347ff 6343 {
ddd2d57e 6344 error ("%Jparameter name missing from parameter list", fndecl);
66db6b62
ZW
6345 TREE_PURPOSE (parm) = 0;
6346 continue;
7a0347ff 6347 }
51e29401 6348
f75fbaf7 6349 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
9aaabf8a 6350 if (b && B_IN_CURRENT_SCOPE (b))
51e29401 6351 {
f75fbaf7 6352 decl = b->decl;
66db6b62
ZW
6353 /* If we got something other than a PARM_DECL it is an error. */
6354 if (TREE_CODE (decl) != PARM_DECL)
dee15844 6355 error ("%q+D declared as a non-parameter", decl);
66db6b62
ZW
6356 /* If the declaration is already marked, we have a duplicate
6357 name. Complain and ignore the duplicate. */
820cc88f 6358 else if (pointer_set_contains (seen_args, decl))
51e29401 6359 {
dee15844 6360 error ("multiple parameters named %q+D", decl);
66db6b62
ZW
6361 TREE_PURPOSE (parm) = 0;
6362 continue;
51e29401 6363 }
66db6b62
ZW
6364 /* If the declaration says "void", complain and turn it into
6365 an int. */
6366 else if (VOID_TYPE_P (TREE_TYPE (decl)))
51e29401 6367 {
dee15844 6368 error ("parameter %q+D declared with void type", decl);
66db6b62
ZW
6369 TREE_TYPE (decl) = integer_type_node;
6370 DECL_ARG_TYPE (decl) = integer_type_node;
6371 layout_decl (decl, 0);
51e29401 6372 }
7c47d6e9 6373 warn_if_shadowing (decl);
51e29401 6374 }
66db6b62
ZW
6375 /* If no declaration found, default to int. */
6376 else
51e29401 6377 {
66db6b62
ZW
6378 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
6379 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
f31686a3 6380 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
66db6b62 6381 pushdecl (decl);
7c47d6e9 6382 warn_if_shadowing (decl);
66db6b62
ZW
6383
6384 if (flag_isoc99)
fcf73884 6385 pedwarn (0, "type of %q+D defaults to %<int%>", decl);
cb4af25a
MLI
6386 else
6387 warning (OPT_Wmissing_parameter_type, "type of %q+D defaults to %<int%>", decl);
51e29401
RS
6388 }
6389
66db6b62 6390 TREE_PURPOSE (parm) = decl;
820cc88f 6391 pointer_set_insert (seen_args, decl);
51e29401 6392 }
51e29401 6393
55d54003
ZW
6394 /* Now examine the parms chain for incomplete declarations
6395 and declarations with no corresponding names. */
51e29401 6396
f75fbaf7 6397 for (b = current_scope->bindings; b; b = b->prev)
66db6b62 6398 {
f75fbaf7
ZW
6399 parm = b->decl;
6400 if (TREE_CODE (parm) != PARM_DECL)
6401 continue;
6402
559f2da5
RH
6403 if (TREE_TYPE (parm) != error_mark_node
6404 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
51e29401 6405 {
dee15844 6406 error ("parameter %q+D has incomplete type", parm);
66db6b62
ZW
6407 TREE_TYPE (parm) = error_mark_node;
6408 }
51e29401 6409
820cc88f 6410 if (!pointer_set_contains (seen_args, parm))
66db6b62 6411 {
dee15844 6412 error ("declaration for parameter %q+D but no such parameter", parm);
51e29401 6413
66db6b62
ZW
6414 /* Pretend the parameter was not missing.
6415 This gets us to a standard state and minimizes
6416 further error messages. */
6417 parmids = chainon (parmids, tree_cons (parm, 0, 0));
51e29401 6418 }
66db6b62 6419 }
51e29401 6420
66db6b62
ZW
6421 /* Chain the declarations together in the order of the list of
6422 names. Store that chain in the function decl, replacing the
55d54003 6423 list of names. Update the current scope to match. */
66db6b62 6424 DECL_ARGUMENTS (fndecl) = 0;
66db6b62 6425
55d54003
ZW
6426 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6427 if (TREE_PURPOSE (parm))
6428 break;
6429 if (parm && TREE_PURPOSE (parm))
6430 {
6431 last = TREE_PURPOSE (parm);
6432 DECL_ARGUMENTS (fndecl) = last;
55d54003
ZW
6433
6434 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
6435 if (TREE_PURPOSE (parm))
6436 {
6437 TREE_CHAIN (last) = TREE_PURPOSE (parm);
6438 last = TREE_PURPOSE (parm);
55d54003
ZW
6439 }
6440 TREE_CHAIN (last) = 0;
6441 }
51e29401 6442
820cc88f
DB
6443 pointer_set_destroy (seen_args);
6444
66db6b62
ZW
6445 /* If there was a previous prototype,
6446 set the DECL_ARG_TYPE of each argument according to
6447 the type previously specified, and report any mismatches. */
51e29401 6448
6fb58bba 6449 if (current_function_prototype_arg_types)
66db6b62
ZW
6450 {
6451 tree type;
6452 for (parm = DECL_ARGUMENTS (fndecl),
6fb58bba 6453 type = current_function_prototype_arg_types;
f1f66b42
LM
6454 parm || (type && TREE_VALUE (type) != error_mark_node
6455 && (TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node));
66db6b62 6456 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
51e29401 6457 {
66db6b62
ZW
6458 if (parm == 0 || type == 0
6459 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
51e29401 6460 {
6fb58bba
JM
6461 if (current_function_prototype_built_in)
6462 warning (0, "number of arguments doesn%'t match "
6463 "built-in prototype");
6464 else
6465 {
6466 error ("number of arguments doesn%'t match prototype");
6467 error ("%Hprototype declaration",
6468 &current_function_prototype_locus);
6469 }
66db6b62
ZW
6470 break;
6471 }
6472 /* Type for passing arg must be consistent with that
6473 declared for the arg. ISO C says we take the unqualified
6474 type for parameters declared with qualified type. */
bb686a19
VR
6475 if (TREE_TYPE (parm) != error_mark_node
6476 && TREE_TYPE (type) != error_mark_node
6477 && !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6478 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
66db6b62
ZW
6479 {
6480 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6481 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
51e29401 6482 {
66db6b62
ZW
6483 /* Adjust argument to match prototype. E.g. a previous
6484 `int foo(float);' prototype causes
6485 `int foo(x) float x; {...}' to be treated like
6486 `int foo(float x) {...}'. This is particularly
6487 useful for argument types like uid_t. */
6488 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6489
61f71b34 6490 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
66db6b62
ZW
6491 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6492 && TYPE_PRECISION (TREE_TYPE (parm))
6493 < TYPE_PRECISION (integer_type_node))
6494 DECL_ARG_TYPE (parm) = integer_type_node;
6495
fcf73884
MLI
6496 /* ??? Is it possible to get here with a
6497 built-in prototype or will it always have
6498 been diagnosed as conflicting with an
6499 old-style definition and discarded? */
6500 if (current_function_prototype_built_in)
6501 warning (OPT_pedantic, "promoted argument %qD "
6502 "doesn%'t match built-in prototype", parm);
6503 else
50a9145c 6504 {
fcf73884
MLI
6505 pedwarn (OPT_pedantic, "promoted argument %qD "
6506 "doesn%'t match prototype", parm);
6507 pedwarn (OPT_pedantic, "%Hprototype declaration",
6508 &current_function_prototype_locus);
50a9145c 6509 }
51e29401 6510 }
66db6b62
ZW
6511 else
6512 {
6fb58bba
JM
6513 if (current_function_prototype_built_in)
6514 warning (0, "argument %qD doesn%'t match "
6515 "built-in prototype", parm);
6516 else
6517 {
6518 error ("argument %qD doesn%'t match prototype", parm);
6519 error ("%Hprototype declaration",
6520 &current_function_prototype_locus);
6521 }
66db6b62 6522 }
51e29401 6523 }
51e29401 6524 }
66db6b62
ZW
6525 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6526 }
51e29401 6527
66db6b62 6528 /* Otherwise, create a prototype that would match. */
51e29401 6529
66db6b62
ZW
6530 else
6531 {
6532 tree actual = 0, last = 0, type;
51e29401 6533
66db6b62
ZW
6534 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6535 {
6536 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
51e29401
RS
6537 if (last)
6538 TREE_CHAIN (last) = type;
6539 else
6540 actual = type;
66db6b62
ZW
6541 last = type;
6542 }
6543 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6544 if (last)
6545 TREE_CHAIN (last) = type;
6546 else
6547 actual = type;
51e29401 6548
66db6b62
ZW
6549 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6550 of the type of this function, but we need to avoid having this
6551 affect the types of other similarly-typed functions, so we must
6552 first force the generation of an identical (but separate) type
6553 node for the relevant function type. The new node we create
6554 will be a variant of the main variant of the original function
6555 type. */
c138f328 6556
8dd16ecc 6557 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
c138f328 6558
66db6b62
ZW
6559 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6560 }
66db6b62
ZW
6561}
6562
d4e43dff
ZL
6563/* Store parameter declarations passed in ARG_INFO into the current
6564 function declaration. */
6565
6566void
f8893e47 6567store_parm_decls_from (struct c_arg_info *arg_info)
d4e43dff
ZL
6568{
6569 current_function_arg_info = arg_info;
6570 store_parm_decls ();
6571}
6572
66db6b62
ZW
6573/* Store the parameter declarations into the current function declaration.
6574 This is called after parsing the parameter declarations, before
6575 digesting the body of the function.
6576
6577 For an old-style definition, construct a prototype out of the old-style
6578 parameter declarations and inject it into the function's type. */
6579
6580void
6581store_parm_decls (void)
6582{
6583 tree fndecl = current_function_decl;
7b112487 6584 bool proto;
66db6b62 6585
77dbdb57 6586 /* The argument information block for FNDECL. */
f8893e47 6587 struct c_arg_info *arg_info = current_function_arg_info;
7b112487 6588 current_function_arg_info = 0;
77dbdb57 6589
f75fbaf7
ZW
6590 /* True if this definition is written with a prototype. Note:
6591 despite C99 6.7.5.3p14, we can *not* treat an empty argument
6592 list in a function definition as equivalent to (void) -- an
6593 empty argument list specifies the function has no parameters,
6594 but only (void) sets up a prototype for future calls. */
f8893e47 6595 proto = arg_info->types != 0;
66db6b62 6596
f75fbaf7 6597 if (proto)
77dbdb57 6598 store_parm_decls_newstyle (fndecl, arg_info);
66db6b62 6599 else
77dbdb57 6600 store_parm_decls_oldstyle (fndecl, arg_info);
51e29401 6601
f75fbaf7 6602 /* The next call to push_scope will be a function body. */
51e29401 6603
a8ccdffe 6604 next_is_function_body = true;
51e29401 6605
51e29401
RS
6606 /* Write a record describing this function definition to the prototypes
6607 file (if requested). */
6608
f75fbaf7 6609 gen_aux_info_record (fndecl, 1, 0, proto);
51e29401
RS
6610
6611 /* Initialize the RTL code for the function. */
182e0d71 6612 allocate_struct_function (fndecl, false);
51e29401 6613
8f17b5c5 6614 /* Begin the statement tree for this function. */
325c3691 6615 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
51e29401 6616
82c82743 6617 /* ??? Insert the contents of the pending sizes list into the function
4744afba
RH
6618 to be evaluated. The only reason left to have this is
6619 void foo(int n, int array[n++])
6620 because we throw away the array type in favor of a pointer type, and
6621 thus won't naturally see the SAVE_EXPR containing the increment. All
6622 other pending sizes would be handled by gimplify_parameters. */
82c82743
RH
6623 {
6624 tree t;
6625 for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t))
6626 add_stmt (TREE_VALUE (t));
6627 }
1f731749 6628
8f17b5c5
MM
6629 /* Even though we're inside a function body, we still don't want to
6630 call expand_expr to calculate the size of a variable-sized array.
6631 We haven't necessarily assigned RTL to all variables yet, so it's
6632 not safe to try to expand expressions involving them. */
e3b5732b 6633 cfun->dont_save_pending_sizes_p = 1;
51e29401
RS
6634}
6635\f
953ff289
DN
6636/* Emit diagnostics that require gimple input for detection. Operate on
6637 FNDECL and all its nested functions. */
6de9cd9a
DN
6638
6639static void
953ff289 6640c_gimple_diagnostics_recursively (tree fndecl)
6de9cd9a
DN
6641{
6642 struct cgraph_node *cgn;
726a989a 6643 gimple_seq body = gimple_body (fndecl);
6de9cd9a
DN
6644
6645 /* Handle attribute((warn_unused_result)). Relies on gimple input. */
726a989a 6646 c_warn_unused_result (body);
6de9cd9a 6647
953ff289
DN
6648 /* Notice when OpenMP structured block constraints are violated. */
6649 if (flag_openmp)
6650 diagnose_omp_structured_block_errors (fndecl);
6651
6de9cd9a
DN
6652 /* Finalize all nested functions now. */
6653 cgn = cgraph_node (fndecl);
6654 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
953ff289 6655 c_gimple_diagnostics_recursively (cgn->decl);
6de9cd9a
DN
6656}
6657
51e29401
RS
6658/* Finish up a function declaration and compile that function
6659 all the way to assembler language output. The free the storage
6660 for the function definition.
6661
4a46cbfb 6662 This is called after parsing the body of the function definition. */
51e29401
RS
6663
6664void
edaf3e03 6665finish_function (void)
51e29401 6666{
b3694847 6667 tree fndecl = current_function_decl;
51e29401 6668
187230a7
JM
6669 label_context_stack_se = label_context_stack_se->next;
6670 label_context_stack_vm = label_context_stack_vm->next;
16ef3acc 6671
61f71b34
DD
6672 if (TREE_CODE (fndecl) == FUNCTION_DECL
6673 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6674 {
6675 tree args = DECL_ARGUMENTS (fndecl);
6676 for (; args; args = TREE_CHAIN (args))
c22cacf3
MS
6677 {
6678 tree type = TREE_TYPE (args);
6679 if (INTEGRAL_TYPE_P (type)
6680 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6681 DECL_ARG_TYPE (args) = integer_type_node;
6682 }
61f71b34
DD
6683 }
6684
67b28863 6685 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
af3fbed1 6686 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
51e29401
RS
6687
6688 /* Must mark the RESULT_DECL as being in this function. */
6689
67b28863 6690 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
af3fbed1 6691 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
51e29401 6692
5b47282c 6693 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
51e29401 6694 {
90d56da8
RS
6695 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6696 != integer_type_node)
b8705e61 6697 {
007aaed0 6698 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6645c3fa 6699 If warn_main is -1 (-Wno-main) we don't want to be warned. */
435ab236 6700 if (!warn_main)
fcf73884 6701 pedwarn (0, "return type of %q+D is not %<int%>", fndecl);
b8705e61 6702 }
8e077183
RS
6703 else
6704 {
98be7846 6705 if (flag_isoc99)
43e05e45
SB
6706 {
6707 tree stmt = c_finish_return (integer_zero_node);
b747e45d
PB
6708 /* Hack. We don't want the middle-end to warn that this return
6709 is unreachable, so we mark its location as special. Using
6710 UNKNOWN_LOCATION has the problem that it gets clobbered in
6711 annotate_one_with_locus. A cleaner solution might be to
6712 ensure ! should_carry_locus_p (stmt), but that needs a flag.
6713 */
6714 SET_EXPR_LOCATION (stmt, BUILTINS_LOCATION);
43e05e45 6715 }
8e077183 6716 }
51e29401 6717 }
35b1a6fa 6718
8f17b5c5 6719 /* Tie off the statement tree for this function. */
325c3691
RH
6720 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
6721
6722 finish_fname_decls ();
5ce89b2e
JM
6723
6724 /* Complain if there's just no return statement. */
46cfb101
JM
6725 if (warn_return_type
6726 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
5ce89b2e 6727 && !current_function_returns_value && !current_function_returns_null
41806d92 6728 /* Don't complain if we are no-return. */
5ce89b2e
JM
6729 && !current_function_returns_abnormally
6730 /* Don't warn for main(). */
6731 && !MAIN_NAME_P (DECL_NAME (fndecl))
6732 /* Or if they didn't actually specify a return type. */
6733 && !C_FUNCTION_IMPLICIT_INT (fndecl)
7e8b322a
JH
6734 /* Normally, with -Wreturn-type, flow will complain, but we might
6735 optimize out static functions. */
6736 && !TREE_PUBLIC (fndecl))
9da58884 6737 {
3176a0c2
DD
6738 warning (OPT_Wreturn_type,
6739 "no return statement in function returning non-void");
9da58884
JH
6740 TREE_NO_WARNING (fndecl) = 1;
6741 }
5ce89b2e 6742
6de9cd9a
DN
6743 /* Store the end of the function, so that we get good line number
6744 info for the epilogue. */
6745 cfun->function_end_locus = input_location;
6746
b2ca3702
MM
6747 /* Finalize the ELF visibility for the function. */
6748 c_determine_visibility (fndecl);
6749
c536a6a7
RG
6750 /* For GNU C extern inline functions disregard inline limits. */
6751 if (DECL_EXTERNAL (fndecl)
6752 && DECL_DECLARED_INLINE_P (fndecl))
6753 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
6754
6de9cd9a
DN
6755 /* Genericize before inlining. Delay genericizing nested functions
6756 until their parent function is genericized. Since finalizing
6757 requires GENERIC, delay that as well. */
118a3a8b 6758
73aea290
JM
6759 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
6760 && !undef_nested_function)
6de9cd9a
DN
6761 {
6762 if (!decl_function_context (fndecl))
c22cacf3
MS
6763 {
6764 c_genericize (fndecl);
6765 c_gimple_diagnostics_recursively (fndecl);
1a94c545
JH
6766
6767 /* ??? Objc emits functions after finalizing the compilation unit.
6768 This should be cleaned up later and this conditional removed. */
6769 if (cgraph_global_info_ready)
6770 {
e89d6010 6771 cgraph_add_new_function (fndecl, false);
1a94c545
JH
6772 return;
6773 }
6774
6775 cgraph_finalize_function (fndecl, false);
c22cacf3 6776 }
6de9cd9a 6777 else
c22cacf3
MS
6778 {
6779 /* Register this function with cgraph just far enough to get it
6780 added to our parent's nested function list. Handy, since the
6781 C front end doesn't have such a list. */
6782 (void) cgraph_node (fndecl);
6783 }
6de9cd9a
DN
6784 }
6785
73aea290
JM
6786 if (!decl_function_context (fndecl))
6787 undef_nested_function = false;
6788
1da326c3 6789 /* We're leaving the context of this function, so zap cfun.
6de9cd9a 6790 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
1da326c3 6791 tree_rest_of_compilation. */
db2960f4 6792 set_cfun (NULL);
4a46cbfb 6793 current_function_decl = NULL;
8f17b5c5 6794}
51e29401 6795\f
77c4d6c0 6796/* Check the declarations given in a for-loop for satisfying the C99
953ff289
DN
6797 constraints. If exactly one such decl is found, return it. */
6798
6799tree
35b1a6fa 6800check_for_loop_decls (void)
77c4d6c0 6801{
f75fbaf7 6802 struct c_binding *b;
953ff289
DN
6803 tree one_decl = NULL_TREE;
6804 int n_decls = 0;
6805
77c4d6c0
JM
6806
6807 if (!flag_isoc99)
6808 {
6809 /* If we get here, declarations have been used in a for loop without
6810 the C99 for loop scope. This doesn't make much sense, so don't
6811 allow it. */
bda67431 6812 error ("%<for%> loop initial declaration used outside C99 mode");
953ff289 6813 return NULL_TREE;
77c4d6c0
JM
6814 }
6815 /* C99 subclause 6.8.5 paragraph 3:
6816
6817 [#3] The declaration part of a for statement shall only
6818 declare identifiers for objects having storage class auto or
6819 register.
6820
6821 It isn't clear whether, in this sentence, "identifiers" binds to
6822 "shall only declare" or to "objects" - that is, whether all identifiers
6823 declared must be identifiers for objects, or whether the restriction
6824 only applies to those that are. (A question on this in comp.std.c
6825 in November 2000 received no answer.) We implement the strictest
6826 interpretation, to avoid creating an extension which later causes
6827 problems. */
6828
f75fbaf7 6829 for (b = current_scope->bindings; b; b = b->prev)
77c4d6c0 6830 {
f75fbaf7
ZW
6831 tree id = b->id;
6832 tree decl = b->decl;
53fcdc76 6833
f75fbaf7
ZW
6834 if (!id)
6835 continue;
6836
6837 switch (TREE_CODE (decl))
6838 {
6839 case VAR_DECL:
6840 if (TREE_STATIC (decl))
dee15844
JM
6841 error ("declaration of static variable %q+D in %<for%> loop "
6842 "initial declaration", decl);
f75fbaf7 6843 else if (DECL_EXTERNAL (decl))
dee15844
JM
6844 error ("declaration of %<extern%> variable %q+D in %<for%> loop "
6845 "initial declaration", decl);
f75fbaf7
ZW
6846 break;
6847
6848 case RECORD_TYPE:
bda67431
JM
6849 error ("%<struct %E%> declared in %<for%> loop initial declaration",
6850 id);
f75fbaf7
ZW
6851 break;
6852 case UNION_TYPE:
bda67431
JM
6853 error ("%<union %E%> declared in %<for%> loop initial declaration",
6854 id);
f75fbaf7
ZW
6855 break;
6856 case ENUMERAL_TYPE:
bda67431
JM
6857 error ("%<enum %E%> declared in %<for%> loop initial declaration",
6858 id);
f75fbaf7
ZW
6859 break;
6860 default:
dee15844
JM
6861 error ("declaration of non-variable %q+D in %<for%> loop "
6862 "initial declaration", decl);
f75fbaf7 6863 }
953ff289
DN
6864
6865 n_decls++;
6866 one_decl = decl;
77c4d6c0 6867 }
953ff289
DN
6868
6869 return n_decls == 1 ? one_decl : NULL_TREE;
77c4d6c0
JM
6870}
6871\f
51e29401
RS
6872/* Save and reinitialize the variables
6873 used during compilation of a C function. */
6874
6875void
d2784db4 6876c_push_function_context (void)
51e29401 6877{
e2500fed 6878 struct language_function *p;
5d038c4c 6879 p = GGC_NEW (struct language_function);
d2784db4 6880 cfun->language = p;
51e29401 6881
8f17b5c5 6882 p->base.x_stmt_tree = c_stmt_tree;
506e2710
RH
6883 p->x_break_label = c_break_label;
6884 p->x_cont_label = c_cont_label;
6885 p->x_switch_stack = c_switch_stack;
7b112487 6886 p->arg_info = current_function_arg_info;
51e29401
RS
6887 p->returns_value = current_function_returns_value;
6888 p->returns_null = current_function_returns_null;
5ce89b2e 6889 p->returns_abnormally = current_function_returns_abnormally;
51e29401 6890 p->warn_about_return_type = warn_about_return_type;
d2784db4
PB
6891
6892 push_function_context ();
51e29401
RS
6893}
6894
6895/* Restore the variables used during compilation of a C function. */
6896
6897void
d2784db4 6898c_pop_function_context (void)
51e29401 6899{
d2784db4
PB
6900 struct language_function *p;
6901
6902 pop_function_context ();
6903 p = cfun->language;
6904 cfun->language = NULL;
51e29401 6905
1da326c3 6906 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
8f17b5c5 6907 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
51e29401
RS
6908 {
6909 /* Stop pointing to the local nodes about to be freed. */
6910 /* But DECL_INITIAL must remain nonzero so we know this
6911 was an actual function definition. */
6912 DECL_INITIAL (current_function_decl) = error_mark_node;
6913 DECL_ARGUMENTS (current_function_decl) = 0;
6914 }
6915
8f17b5c5 6916 c_stmt_tree = p->base.x_stmt_tree;
506e2710
RH
6917 c_break_label = p->x_break_label;
6918 c_cont_label = p->x_cont_label;
6919 c_switch_stack = p->x_switch_stack;
7b112487 6920 current_function_arg_info = p->arg_info;
51e29401
RS
6921 current_function_returns_value = p->returns_value;
6922 current_function_returns_null = p->returns_null;
5ce89b2e 6923 current_function_returns_abnormally = p->returns_abnormally;
51e29401 6924 warn_about_return_type = p->warn_about_return_type;
1526a060
BS
6925}
6926
63e1b1c4 6927/* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
37105beb
JM
6928
6929void
35b1a6fa 6930c_dup_lang_specific_decl (tree decl)
37105beb 6931{
8f17b5c5
MM
6932 struct lang_decl *ld;
6933
6934 if (!DECL_LANG_SPECIFIC (decl))
6935 return;
6936
5d038c4c 6937 ld = GGC_NEW (struct lang_decl);
703ad42b 6938 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
8f17b5c5 6939 DECL_LANG_SPECIFIC (decl) = ld;
37105beb 6940}
1526a060 6941
f2c5f623
BC
6942/* The functions below are required for functionality of doing
6943 function at once processing in the C front end. Currently these
6944 functions are not called from anywhere in the C front end, but as
6645c3fa 6945 these changes continue, that will change. */
f2c5f623 6946
ae499cce
MM
6947/* Returns the stmt_tree (if any) to which statements are currently
6948 being added. If there is no active statement-tree, NULL is
6949 returned. */
6950
6951stmt_tree
35b1a6fa 6952current_stmt_tree (void)
ae499cce 6953{
8f17b5c5
MM
6954 return &c_stmt_tree;
6955}
6956
f2c5f623 6957/* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6645c3fa 6958 C. */
f2c5f623 6959
6645c3fa 6960int
58f9752a 6961anon_aggr_type_p (const_tree ARG_UNUSED (node))
f2c5f623
BC
6962{
6963 return 0;
6964}
6965
339a28b9 6966/* Return the global value of T as a symbol. */
5fd8e536
JM
6967
6968tree
35b1a6fa 6969identifier_global_value (tree t)
5fd8e536 6970{
f75fbaf7 6971 struct c_binding *b;
339a28b9 6972
f75fbaf7 6973 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
9aaabf8a 6974 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
f75fbaf7 6975 return b->decl;
339a28b9 6976
339a28b9 6977 return 0;
5fd8e536 6978}
eaa7c03f
JM
6979
6980/* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6981 otherwise the name is found in ridpointers from RID_INDEX. */
6982
6983void
35b1a6fa 6984record_builtin_type (enum rid rid_index, const char *name, tree type)
eaa7c03f 6985{
ca043393 6986 tree id, decl;
eaa7c03f
JM
6987 if (name == 0)
6988 id = ridpointers[(int) rid_index];
6989 else
6990 id = get_identifier (name);
ca043393
ZW
6991 decl = build_decl (TYPE_DECL, id, type);
6992 pushdecl (decl);
6993 if (debug_hooks->type_decl)
6994 debug_hooks->type_decl (decl, false);
eaa7c03f
JM
6995}
6996
6997/* Build the void_list_node (void_type_node having been created). */
6998tree
35b1a6fa 6999build_void_list_node (void)
eaa7c03f
JM
7000{
7001 tree t = build_tree_list (NULL_TREE, void_type_node);
7002 return t;
7003}
81a75f0f 7004
f8893e47 7005/* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
00325bce 7006
f8893e47 7007struct c_parm *
deb176fa
JM
7008build_c_parm (struct c_declspecs *specs, tree attrs,
7009 struct c_declarator *declarator)
00325bce 7010{
f8893e47
JM
7011 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
7012 ret->specs = specs;
7013 ret->attrs = attrs;
7014 ret->declarator = declarator;
7015 return ret;
00325bce
JM
7016}
7017
7018/* Return a declarator with nested attributes. TARGET is the inner
7019 declarator to which these attributes apply. ATTRS are the
7020 attributes. */
7021
f8893e47
JM
7022struct c_declarator *
7023build_attrs_declarator (tree attrs, struct c_declarator *target)
00325bce 7024{
f8893e47
JM
7025 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7026 ret->kind = cdk_attrs;
7027 ret->declarator = target;
7028 ret->u.attrs = attrs;
7029 return ret;
00325bce
JM
7030}
7031
7032/* Return a declarator for a function with arguments specified by ARGS
7033 and return type specified by TARGET. */
7034
f8893e47
JM
7035struct c_declarator *
7036build_function_declarator (struct c_arg_info *args,
7037 struct c_declarator *target)
00325bce 7038{
f8893e47
JM
7039 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7040 ret->kind = cdk_function;
7041 ret->declarator = target;
7042 ret->u.arg_info = args;
7043 return ret;
7044}
7045
7046/* Return a declarator for the identifier IDENT (which may be
7047 NULL_TREE for an abstract declarator). */
7048
7049struct c_declarator *
7050build_id_declarator (tree ident)
7051{
7052 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7053 ret->kind = cdk_id;
7054 ret->declarator = 0;
7055 ret->u.id = ident;
6037d88d
PB
7056 /* Default value - may get reset to a more precise location. */
7057 ret->id_loc = input_location;
f8893e47 7058 return ret;
00325bce
JM
7059}
7060
81a75f0f
NB
7061/* Return something to represent absolute declarators containing a *.
7062 TARGET is the absolute declarator that the * contains.
deb176fa
JM
7063 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
7064 to apply to the pointer type. */
81a75f0f 7065
f8893e47 7066struct c_declarator *
deb176fa
JM
7067make_pointer_declarator (struct c_declspecs *type_quals_attrs,
7068 struct c_declarator *target)
81a75f0f 7069{
deb176fa 7070 tree attrs;
3b53cddc 7071 int quals = 0;
f8893e47
JM
7072 struct c_declarator *itarget = target;
7073 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
deb176fa
JM
7074 if (type_quals_attrs)
7075 {
7076 attrs = type_quals_attrs->attrs;
3b53cddc 7077 quals = quals_from_declspecs (type_quals_attrs);
deb176fa
JM
7078 if (attrs != NULL_TREE)
7079 itarget = build_attrs_declarator (attrs, target);
7080 }
f8893e47
JM
7081 ret->kind = cdk_pointer;
7082 ret->declarator = itarget;
3b53cddc 7083 ret->u.pointer_quals = quals;
deb176fa
JM
7084 return ret;
7085}
7086
7087/* Return a pointer to a structure for an empty list of declaration
7088 specifiers. */
7089
7090struct c_declspecs *
7091build_null_declspecs (void)
7092{
7093 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
7094 ret->type = 0;
deb176fa
JM
7095 ret->decl_attr = 0;
7096 ret->attrs = 0;
98c3a782 7097 ret->typespec_word = cts_none;
9a26d6ee 7098 ret->storage_class = csc_none;
27bf414c
JM
7099 ret->declspecs_seen_p = false;
7100 ret->type_seen_p = false;
deb176fa 7101 ret->non_sc_seen_p = false;
616aeba2 7102 ret->typedef_p = false;
81da229b 7103 ret->tag_defined_p = false;
98c3a782 7104 ret->explicit_signed_p = false;
deb176fa 7105 ret->deprecated_p = false;
98c3a782
JM
7106 ret->default_int_p = false;
7107 ret->long_p = false;
deb176fa 7108 ret->long_long_p = false;
98c3a782
JM
7109 ret->short_p = false;
7110 ret->signed_p = false;
7111 ret->unsigned_p = false;
7112 ret->complex_p = false;
9a26d6ee
JM
7113 ret->inline_p = false;
7114 ret->thread_p = false;
3b53cddc
JM
7115 ret->const_p = false;
7116 ret->volatile_p = false;
7117 ret->restrict_p = false;
ab22c1fa 7118 ret->saturating_p = false;
f8893e47 7119 return ret;
81a75f0f 7120}
e2500fed 7121
deb176fa
JM
7122/* Add the type qualifier QUAL to the declaration specifiers SPECS,
7123 returning SPECS. */
7124
7125struct c_declspecs *
7126declspecs_add_qual (struct c_declspecs *specs, tree qual)
7127{
7128 enum rid i;
3b53cddc 7129 bool dupe = false;
deb176fa 7130 specs->non_sc_seen_p = true;
27bf414c 7131 specs->declspecs_seen_p = true;
deb176fa
JM
7132 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
7133 && C_IS_RESERVED_WORD (qual));
7134 i = C_RID_CODE (qual);
3b53cddc
JM
7135 switch (i)
7136 {
7137 case RID_CONST:
7138 dupe = specs->const_p;
7139 specs->const_p = true;
7140 break;
7141 case RID_VOLATILE:
7142 dupe = specs->volatile_p;
7143 specs->volatile_p = true;
7144 break;
7145 case RID_RESTRICT:
7146 dupe = specs->restrict_p;
7147 specs->restrict_p = true;
7148 break;
7149 default:
7150 gcc_unreachable ();
7151 }
fcf73884
MLI
7152 if (dupe && !flag_isoc99)
7153 pedwarn (OPT_pedantic, "duplicate %qE", qual);
deb176fa
JM
7154 return specs;
7155}
7156
7157/* Add the type specifier TYPE to the declaration specifiers SPECS,
7158 returning SPECS. */
7159
7160struct c_declspecs *
81da229b 7161declspecs_add_type (struct c_declspecs *specs, struct c_typespec spec)
deb176fa 7162{
81da229b 7163 tree type = spec.spec;
deb176fa 7164 specs->non_sc_seen_p = true;
27bf414c
JM
7165 specs->declspecs_seen_p = true;
7166 specs->type_seen_p = true;
deb176fa
JM
7167 if (TREE_DEPRECATED (type))
7168 specs->deprecated_p = true;
deb176fa 7169
98c3a782 7170 /* Handle type specifier keywords. */
eea1139b
ILT
7171 if (TREE_CODE (type) == IDENTIFIER_NODE
7172 && C_IS_RESERVED_WORD (type)
7173 && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
deb176fa
JM
7174 {
7175 enum rid i = C_RID_CODE (type);
98c3a782
JM
7176 if (specs->type)
7177 {
7178 error ("two or more data types in declaration specifiers");
7179 return specs;
7180 }
deb176fa
JM
7181 if ((int) i <= (int) RID_LAST_MODIFIER)
7182 {
ab22c1fa 7183 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
98c3a782
JM
7184 bool dupe = false;
7185 switch (i)
deb176fa 7186 {
98c3a782 7187 case RID_LONG:
deb176fa 7188 if (specs->long_long_p)
deb176fa 7189 {
98c3a782
JM
7190 error ("%<long long long%> is too long for GCC");
7191 break;
7192 }
7193 if (specs->long_p)
7194 {
7195 if (specs->typespec_word == cts_double)
7196 {
7197 error ("both %<long long%> and %<double%> in "
7198 "declaration specifiers");
7199 break;
7200 }
fcf73884
MLI
7201 if (pedantic && !flag_isoc99 && !in_system_header)
7202 pedwarn (OPT_Wlong_long, "ISO C90 does not support %<long long%>");
deb176fa 7203 specs->long_long_p = 1;
98c3a782 7204 break;
deb176fa 7205 }
98c3a782
JM
7206 if (specs->short_p)
7207 error ("both %<long%> and %<short%> in "
7208 "declaration specifiers");
7209 else if (specs->typespec_word == cts_void)
7210 error ("both %<long%> and %<void%> in "
7211 "declaration specifiers");
7212 else if (specs->typespec_word == cts_bool)
7213 error ("both %<long%> and %<_Bool%> in "
7214 "declaration specifiers");
7215 else if (specs->typespec_word == cts_char)
7216 error ("both %<long%> and %<char%> in "
7217 "declaration specifiers");
7218 else if (specs->typespec_word == cts_float)
7219 error ("both %<long%> and %<float%> in "
7220 "declaration specifiers");
9a8ce21f
JG
7221 else if (specs->typespec_word == cts_dfloat32)
7222 error ("both %<long%> and %<_Decimal32%> in "
7223 "declaration specifiers");
7224 else if (specs->typespec_word == cts_dfloat64)
7225 error ("both %<long%> and %<_Decimal64%> in "
7226 "declaration specifiers");
7227 else if (specs->typespec_word == cts_dfloat128)
7228 error ("both %<long%> and %<_Decimal128%> in "
7229 "declaration specifiers");
98c3a782
JM
7230 else
7231 specs->long_p = true;
7232 break;
7233 case RID_SHORT:
7234 dupe = specs->short_p;
7235 if (specs->long_p)
7236 error ("both %<long%> and %<short%> in "
7237 "declaration specifiers");
7238 else if (specs->typespec_word == cts_void)
7239 error ("both %<short%> and %<void%> in "
7240 "declaration specifiers");
7241 else if (specs->typespec_word == cts_bool)
7242 error ("both %<short%> and %<_Bool%> in "
7243 "declaration specifiers");
7244 else if (specs->typespec_word == cts_char)
7245 error ("both %<short%> and %<char%> in "
7246 "declaration specifiers");
7247 else if (specs->typespec_word == cts_float)
7248 error ("both %<short%> and %<float%> in "
7249 "declaration specifiers");
7250 else if (specs->typespec_word == cts_double)
7251 error ("both %<short%> and %<double%> in "
7252 "declaration specifiers");
9a8ce21f
JG
7253 else if (specs->typespec_word == cts_dfloat32)
7254 error ("both %<short%> and %<_Decimal32%> in "
7255 "declaration specifiers");
7256 else if (specs->typespec_word == cts_dfloat64)
7257 error ("both %<short%> and %<_Decimal64%> in "
7258 "declaration specifiers");
7259 else if (specs->typespec_word == cts_dfloat128)
7260 error ("both %<short%> and %<_Decimal128%> in "
7261 "declaration specifiers");
98c3a782
JM
7262 else
7263 specs->short_p = true;
7264 break;
7265 case RID_SIGNED:
7266 dupe = specs->signed_p;
7267 if (specs->unsigned_p)
7268 error ("both %<signed%> and %<unsigned%> in "
7269 "declaration specifiers");
7270 else if (specs->typespec_word == cts_void)
7271 error ("both %<signed%> and %<void%> in "
7272 "declaration specifiers");
7273 else if (specs->typespec_word == cts_bool)
7274 error ("both %<signed%> and %<_Bool%> in "
7275 "declaration specifiers");
7276 else if (specs->typespec_word == cts_float)
7277 error ("both %<signed%> and %<float%> in "
7278 "declaration specifiers");
7279 else if (specs->typespec_word == cts_double)
7280 error ("both %<signed%> and %<double%> in "
7281 "declaration specifiers");
9a8ce21f
JG
7282 else if (specs->typespec_word == cts_dfloat32)
7283 error ("both %<signed%> and %<_Decimal32%> in "
7284 "declaration specifiers");
7285 else if (specs->typespec_word == cts_dfloat64)
7286 error ("both %<signed%> and %<_Decimal64%> in "
7287 "declaration specifiers");
7288 else if (specs->typespec_word == cts_dfloat128)
7289 error ("both %<signed%> and %<_Decimal128%> in "
7290 "declaration specifiers");
98c3a782
JM
7291 else
7292 specs->signed_p = true;
7293 break;
7294 case RID_UNSIGNED:
7295 dupe = specs->unsigned_p;
7296 if (specs->signed_p)
7297 error ("both %<signed%> and %<unsigned%> in "
7298 "declaration specifiers");
7299 else if (specs->typespec_word == cts_void)
7300 error ("both %<unsigned%> and %<void%> in "
7301 "declaration specifiers");
7302 else if (specs->typespec_word == cts_bool)
7303 error ("both %<unsigned%> and %<_Bool%> in "
7304 "declaration specifiers");
7305 else if (specs->typespec_word == cts_float)
7306 error ("both %<unsigned%> and %<float%> in "
7307 "declaration specifiers");
7308 else if (specs->typespec_word == cts_double)
7309 error ("both %<unsigned%> and %<double%> in "
7310 "declaration specifiers");
9a8ce21f
JG
7311 else if (specs->typespec_word == cts_dfloat32)
7312 error ("both %<unsigned%> and %<_Decimal32%> in "
7313 "declaration specifiers");
7314 else if (specs->typespec_word == cts_dfloat64)
7315 error ("both %<unsigned%> and %<_Decimal64%> in "
7316 "declaration specifiers");
7317 else if (specs->typespec_word == cts_dfloat128)
7318 error ("both %<unsigned%> and %<_Decimal128%> in "
7319 "declaration specifiers");
98c3a782
JM
7320 else
7321 specs->unsigned_p = true;
7322 break;
7323 case RID_COMPLEX:
7324 dupe = specs->complex_p;
fcf73884
MLI
7325 if (!flag_isoc99 && !in_system_header)
7326 pedwarn (OPT_pedantic, "ISO C90 does not support complex types");
98c3a782
JM
7327 if (specs->typespec_word == cts_void)
7328 error ("both %<complex%> and %<void%> in "
7329 "declaration specifiers");
7330 else if (specs->typespec_word == cts_bool)
7331 error ("both %<complex%> and %<_Bool%> in "
7332 "declaration specifiers");
9a8ce21f
JG
7333 else if (specs->typespec_word == cts_dfloat32)
7334 error ("both %<complex%> and %<_Decimal32%> in "
7335 "declaration specifiers");
7336 else if (specs->typespec_word == cts_dfloat64)
7337 error ("both %<complex%> and %<_Decimal64%> in "
7338 "declaration specifiers");
7339 else if (specs->typespec_word == cts_dfloat128)
7340 error ("both %<complex%> and %<_Decimal128%> in "
7341 "declaration specifiers");
ab22c1fa
CF
7342 else if (specs->typespec_word == cts_fract)
7343 error ("both %<complex%> and %<_Fract%> in "
7344 "declaration specifiers");
7345 else if (specs->typespec_word == cts_accum)
7346 error ("both %<complex%> and %<_Accum%> in "
7347 "declaration specifiers");
7348 else if (specs->saturating_p)
7349 error ("both %<complex%> and %<_Sat%> in "
7350 "declaration specifiers");
98c3a782
JM
7351 else
7352 specs->complex_p = true;
7353 break;
ab22c1fa
CF
7354 case RID_SAT:
7355 dupe = specs->saturating_p;
fcf73884 7356 pedwarn (OPT_pedantic, "ISO C does not support saturating types");
ab22c1fa
CF
7357 if (specs->typespec_word == cts_void)
7358 error ("both %<_Sat%> and %<void%> in "
7359 "declaration specifiers");
7360 else if (specs->typespec_word == cts_bool)
7361 error ("both %<_Sat%> and %<_Bool%> in "
7362 "declaration specifiers");
7363 else if (specs->typespec_word == cts_char)
7364 error ("both %<_Sat%> and %<char%> in "
7365 "declaration specifiers");
7366 else if (specs->typespec_word == cts_int)
7367 error ("both %<_Sat%> and %<int%> in "
7368 "declaration specifiers");
7369 else if (specs->typespec_word == cts_float)
7370 error ("both %<_Sat%> and %<float%> in "
7371 "declaration specifiers");
7372 else if (specs->typespec_word == cts_double)
7373 error ("both %<_Sat%> and %<double%> in "
7374 "declaration specifiers");
7375 else if (specs->typespec_word == cts_dfloat32)
7376 error ("both %<_Sat%> and %<_Decimal32%> in "
7377 "declaration specifiers");
7378 else if (specs->typespec_word == cts_dfloat64)
7379 error ("both %<_Sat%> and %<_Decimal64%> in "
7380 "declaration specifiers");
7381 else if (specs->typespec_word == cts_dfloat128)
7382 error ("both %<_Sat%> and %<_Decimal128%> in "
7383 "declaration specifiers");
7384 else if (specs->complex_p)
7385 error ("both %<_Sat%> and %<complex%> in "
7386 "declaration specifiers");
7387 else
7388 specs->saturating_p = true;
7389 break;
98c3a782
JM
7390 default:
7391 gcc_unreachable ();
deb176fa 7392 }
98c3a782
JM
7393
7394 if (dupe)
c51a1ba9 7395 error ("duplicate %qE", type);
deb176fa 7396
deb176fa
JM
7397 return specs;
7398 }
98c3a782
JM
7399 else
7400 {
ab22c1fa
CF
7401 /* "void", "_Bool", "char", "int", "float", "double", "_Decimal32",
7402 "_Decimal64", "_Decimal128", "_Fract" or "_Accum". */
98c3a782
JM
7403 if (specs->typespec_word != cts_none)
7404 {
7405 error ("two or more data types in declaration specifiers");
7406 return specs;
7407 }
7408 switch (i)
7409 {
7410 case RID_VOID:
7411 if (specs->long_p)
7412 error ("both %<long%> and %<void%> in "
7413 "declaration specifiers");
7414 else if (specs->short_p)
7415 error ("both %<short%> and %<void%> in "
7416 "declaration specifiers");
7417 else if (specs->signed_p)
7418 error ("both %<signed%> and %<void%> in "
7419 "declaration specifiers");
7420 else if (specs->unsigned_p)
7421 error ("both %<unsigned%> and %<void%> in "
7422 "declaration specifiers");
7423 else if (specs->complex_p)
7424 error ("both %<complex%> and %<void%> in "
7425 "declaration specifiers");
ab22c1fa
CF
7426 else if (specs->saturating_p)
7427 error ("both %<_Sat%> and %<void%> in "
7428 "declaration specifiers");
98c3a782
JM
7429 else
7430 specs->typespec_word = cts_void;
7431 return specs;
7432 case RID_BOOL:
7433 if (specs->long_p)
7434 error ("both %<long%> and %<_Bool%> in "
7435 "declaration specifiers");
7436 else if (specs->short_p)
7437 error ("both %<short%> and %<_Bool%> in "
7438 "declaration specifiers");
7439 else if (specs->signed_p)
7440 error ("both %<signed%> and %<_Bool%> in "
7441 "declaration specifiers");
7442 else if (specs->unsigned_p)
7443 error ("both %<unsigned%> and %<_Bool%> in "
7444 "declaration specifiers");
7445 else if (specs->complex_p)
7446 error ("both %<complex%> and %<_Bool%> in "
7447 "declaration specifiers");
ab22c1fa
CF
7448 else if (specs->saturating_p)
7449 error ("both %<_Sat%> and %<_Bool%> in "
7450 "declaration specifiers");
98c3a782
JM
7451 else
7452 specs->typespec_word = cts_bool;
7453 return specs;
7454 case RID_CHAR:
7455 if (specs->long_p)
7456 error ("both %<long%> and %<char%> in "
7457 "declaration specifiers");
7458 else if (specs->short_p)
7459 error ("both %<short%> and %<char%> in "
7460 "declaration specifiers");
ab22c1fa
CF
7461 else if (specs->saturating_p)
7462 error ("both %<_Sat%> and %<char%> in "
7463 "declaration specifiers");
98c3a782
JM
7464 else
7465 specs->typespec_word = cts_char;
7466 return specs;
7467 case RID_INT:
ab22c1fa
CF
7468 if (specs->saturating_p)
7469 error ("both %<_Sat%> and %<int%> in "
7470 "declaration specifiers");
7471 else
7472 specs->typespec_word = cts_int;
98c3a782
JM
7473 return specs;
7474 case RID_FLOAT:
7475 if (specs->long_p)
7476 error ("both %<long%> and %<float%> in "
7477 "declaration specifiers");
7478 else if (specs->short_p)
7479 error ("both %<short%> and %<float%> in "
7480 "declaration specifiers");
7481 else if (specs->signed_p)
7482 error ("both %<signed%> and %<float%> in "
7483 "declaration specifiers");
7484 else if (specs->unsigned_p)
7485 error ("both %<unsigned%> and %<float%> in "
7486 "declaration specifiers");
ab22c1fa
CF
7487 else if (specs->saturating_p)
7488 error ("both %<_Sat%> and %<float%> in "
7489 "declaration specifiers");
98c3a782
JM
7490 else
7491 specs->typespec_word = cts_float;
7492 return specs;
7493 case RID_DOUBLE:
7494 if (specs->long_long_p)
7495 error ("both %<long long%> and %<double%> in "
7496 "declaration specifiers");
7497 else if (specs->short_p)
7498 error ("both %<short%> and %<double%> in "
7499 "declaration specifiers");
7500 else if (specs->signed_p)
7501 error ("both %<signed%> and %<double%> in "
7502 "declaration specifiers");
7503 else if (specs->unsigned_p)
7504 error ("both %<unsigned%> and %<double%> in "
7505 "declaration specifiers");
ab22c1fa
CF
7506 else if (specs->saturating_p)
7507 error ("both %<_Sat%> and %<double%> in "
7508 "declaration specifiers");
98c3a782
JM
7509 else
7510 specs->typespec_word = cts_double;
7511 return specs;
9a8ce21f
JG
7512 case RID_DFLOAT32:
7513 case RID_DFLOAT64:
7514 case RID_DFLOAT128:
7515 {
7516 const char *str;
7517 if (i == RID_DFLOAT32)
7518 str = "_Decimal32";
7519 else if (i == RID_DFLOAT64)
7520 str = "_Decimal64";
7521 else
7522 str = "_Decimal128";
7523 if (specs->long_long_p)
7524 error ("both %<long long%> and %<%s%> in "
7525 "declaration specifiers", str);
7526 if (specs->long_p)
7527 error ("both %<long%> and %<%s%> in "
7528 "declaration specifiers", str);
7529 else if (specs->short_p)
7530 error ("both %<short%> and %<%s%> in "
7531 "declaration specifiers", str);
7532 else if (specs->signed_p)
7533 error ("both %<signed%> and %<%s%> in "
7534 "declaration specifiers", str);
7535 else if (specs->unsigned_p)
7536 error ("both %<unsigned%> and %<%s%> in "
7537 "declaration specifiers", str);
7538 else if (specs->complex_p)
7539 error ("both %<complex%> and %<%s%> in "
7540 "declaration specifiers", str);
ab22c1fa
CF
7541 else if (specs->saturating_p)
7542 error ("both %<_Sat%> and %<%s%> in "
7543 "declaration specifiers", str);
9a8ce21f
JG
7544 else if (i == RID_DFLOAT32)
7545 specs->typespec_word = cts_dfloat32;
7546 else if (i == RID_DFLOAT64)
7547 specs->typespec_word = cts_dfloat64;
7548 else
7549 specs->typespec_word = cts_dfloat128;
7550 }
7551 if (!targetm.decimal_float_supported_p ())
7552 error ("decimal floating point not supported for this target");
fcf73884
MLI
7553 pedwarn (OPT_pedantic,
7554 "ISO C does not support decimal floating point");
9a8ce21f 7555 return specs;
ab22c1fa
CF
7556 case RID_FRACT:
7557 case RID_ACCUM:
7558 {
7559 const char *str;
7560 if (i == RID_FRACT)
7561 str = "_Fract";
7562 else
7563 str = "_Accum";
7564 if (specs->complex_p)
7565 error ("both %<complex%> and %<%s%> in "
7566 "declaration specifiers", str);
7567 else if (i == RID_FRACT)
7568 specs->typespec_word = cts_fract;
7569 else
7570 specs->typespec_word = cts_accum;
7571 }
7572 if (!targetm.fixed_point_supported_p ())
7573 error ("fixed-point types not supported for this target");
fcf73884
MLI
7574 pedwarn (OPT_pedantic,
7575 "ISO C does not support fixed-point types");
ab22c1fa 7576 return specs;
98c3a782
JM
7577 default:
7578 /* ObjC reserved word "id", handled below. */
7579 break;
7580 }
7581 }
deb176fa 7582 }
98c3a782
JM
7583
7584 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
7585 form of ObjC type, cases such as "int" and "long" being handled
7586 above), a TYPE (struct, union, enum and typeof specifiers) or an
7587 ERROR_MARK. In none of these cases may there have previously
7588 been any type specifiers. */
7589 if (specs->type || specs->typespec_word != cts_none
7590 || specs->long_p || specs->short_p || specs->signed_p
7591 || specs->unsigned_p || specs->complex_p)
deb176fa 7592 error ("two or more data types in declaration specifiers");
deb176fa
JM
7593 else if (TREE_CODE (type) == TYPE_DECL)
7594 {
7595 if (TREE_TYPE (type) == error_mark_node)
7596 ; /* Allow the type to default to int to avoid cascading errors. */
7597 else
7598 {
7599 specs->type = TREE_TYPE (type);
7600 specs->decl_attr = DECL_ATTRIBUTES (type);
616aeba2 7601 specs->typedef_p = true;
98c3a782 7602 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
deb176fa
JM
7603 }
7604 }
deb176fa
JM
7605 else if (TREE_CODE (type) == IDENTIFIER_NODE)
7606 {
7607 tree t = lookup_name (type);
7608 if (!t || TREE_CODE (t) != TYPE_DECL)
c51a1ba9 7609 error ("%qE fails to be a typedef or built in type", type);
deb176fa
JM
7610 else if (TREE_TYPE (t) == error_mark_node)
7611 ;
7612 else
7613 specs->type = TREE_TYPE (t);
7614 }
7615 else if (TREE_CODE (type) != ERROR_MARK)
81da229b
JM
7616 {
7617 if (spec.kind == ctsk_tagdef || spec.kind == ctsk_tagfirstref)
7618 specs->tag_defined_p = true;
7619 if (spec.kind == ctsk_typeof)
7620 specs->typedef_p = true;
7621 specs->type = type;
7622 }
deb176fa
JM
7623
7624 return specs;
7625}
7626
7627/* Add the storage class specifier or function specifier SCSPEC to the
7628 declaration specifiers SPECS, returning SPECS. */
7629
7630struct c_declspecs *
7631declspecs_add_scspec (struct c_declspecs *specs, tree scspec)
7632{
7633 enum rid i;
9a26d6ee
JM
7634 enum c_storage_class n = csc_none;
7635 bool dupe = false;
27bf414c 7636 specs->declspecs_seen_p = true;
deb176fa
JM
7637 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
7638 && C_IS_RESERVED_WORD (scspec));
7639 i = C_RID_CODE (scspec);
b1ed4cb4
MLI
7640 if (specs->non_sc_seen_p)
7641 warning (OPT_Wold_style_declaration,
7642 "%qE is not at beginning of declaration", scspec);
9a26d6ee
JM
7643 switch (i)
7644 {
7645 case RID_INLINE:
61f4f1cc
JM
7646 /* C99 permits duplicate inline. Although of doubtful utility,
7647 it seems simplest to permit it in gnu89 mode as well, as
7648 there is also little utility in maintaining this as a
7649 difference between gnu89 and C99 inline. */
7650 dupe = false;
9a26d6ee
JM
7651 specs->inline_p = true;
7652 break;
7653 case RID_THREAD:
7654 dupe = specs->thread_p;
7655 if (specs->storage_class == csc_auto)
7656 error ("%<__thread%> used with %<auto%>");
7657 else if (specs->storage_class == csc_register)
7658 error ("%<__thread%> used with %<register%>");
7659 else if (specs->storage_class == csc_typedef)
7660 error ("%<__thread%> used with %<typedef%>");
7661 else
7662 specs->thread_p = true;
7663 break;
7664 case RID_AUTO:
7665 n = csc_auto;
7666 break;
7667 case RID_EXTERN:
7668 n = csc_extern;
7669 /* Diagnose "__thread extern". */
7670 if (specs->thread_p)
deb176fa 7671 error ("%<__thread%> before %<extern%>");
9a26d6ee
JM
7672 break;
7673 case RID_REGISTER:
7674 n = csc_register;
7675 break;
7676 case RID_STATIC:
7677 n = csc_static;
7678 /* Diagnose "__thread static". */
7679 if (specs->thread_p)
deb176fa 7680 error ("%<__thread%> before %<static%>");
9a26d6ee
JM
7681 break;
7682 case RID_TYPEDEF:
7683 n = csc_typedef;
7684 break;
7685 default:
7686 gcc_unreachable ();
7687 }
7688 if (n != csc_none && n == specs->storage_class)
7689 dupe = true;
7690 if (dupe)
c51a1ba9 7691 error ("duplicate %qE", scspec);
9a26d6ee
JM
7692 if (n != csc_none)
7693 {
7694 if (specs->storage_class != csc_none && n != specs->storage_class)
7695 {
7696 error ("multiple storage classes in declaration specifiers");
7697 }
7698 else
7699 {
7700 specs->storage_class = n;
7701 if (n != csc_extern && n != csc_static && specs->thread_p)
7702 {
c51a1ba9 7703 error ("%<__thread%> used with %qE", scspec);
9a26d6ee
JM
7704 specs->thread_p = false;
7705 }
7706 }
deb176fa 7707 }
deb176fa
JM
7708 return specs;
7709}
7710
7711/* Add the attributes ATTRS to the declaration specifiers SPECS,
7712 returning SPECS. */
7713
7714struct c_declspecs *
7715declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
7716{
7717 specs->attrs = chainon (attrs, specs->attrs);
27bf414c 7718 specs->declspecs_seen_p = true;
deb176fa
JM
7719 return specs;
7720}
7721
98c3a782
JM
7722/* Combine "long", "short", "signed", "unsigned" and "_Complex" type
7723 specifiers with any other type specifier to determine the resulting
7724 type. This is where ISO C checks on complex types are made, since
7725 "_Complex long" is a prefix of the valid ISO C type "_Complex long
7726 double". */
7727
7728struct c_declspecs *
7729finish_declspecs (struct c_declspecs *specs)
7730{
7731 /* If a type was specified as a whole, we have no modifiers and are
7732 done. */
7733 if (specs->type != NULL_TREE)
7734 {
7735 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
7736 && !specs->signed_p && !specs->unsigned_p
7737 && !specs->complex_p);
7738 return specs;
7739 }
7740
7741 /* If none of "void", "_Bool", "char", "int", "float" or "double"
7742 has been specified, treat it as "int" unless "_Complex" is
7743 present and there are no other specifiers. If we just have
7744 "_Complex", it is equivalent to "_Complex double", but e.g.
7745 "_Complex short" is equivalent to "_Complex short int". */
7746 if (specs->typespec_word == cts_none)
7747 {
ab22c1fa 7748 if (specs->saturating_p)
819fec00
NS
7749 {
7750 error ("%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
7751 specs->typespec_word = cts_fract;
7752 }
7753 else if (specs->long_p || specs->short_p
7754 || specs->signed_p || specs->unsigned_p)
98c3a782
JM
7755 {
7756 specs->typespec_word = cts_int;
7757 }
7758 else if (specs->complex_p)
7759 {
7760 specs->typespec_word = cts_double;
fcf73884
MLI
7761 pedwarn (OPT_pedantic,
7762 "ISO C does not support plain %<complex%> meaning "
7763 "%<double complex%>");
98c3a782
JM
7764 }
7765 else
7766 {
7767 specs->typespec_word = cts_int;
7768 specs->default_int_p = true;
7769 /* We don't diagnose this here because grokdeclarator will
7770 give more specific diagnostics according to whether it is
7771 a function definition. */
7772 }
7773 }
7774
7775 /* If "signed" was specified, record this to distinguish "int" and
7776 "signed int" in the case of a bit-field with
7777 -funsigned-bitfields. */
7778 specs->explicit_signed_p = specs->signed_p;
7779
7780 /* Now compute the actual type. */
7781 switch (specs->typespec_word)
7782 {
7783 case cts_void:
7784 gcc_assert (!specs->long_p && !specs->short_p
7785 && !specs->signed_p && !specs->unsigned_p
7786 && !specs->complex_p);
7787 specs->type = void_type_node;
7788 break;
7789 case cts_bool:
7790 gcc_assert (!specs->long_p && !specs->short_p
7791 && !specs->signed_p && !specs->unsigned_p
7792 && !specs->complex_p);
7793 specs->type = boolean_type_node;
7794 break;
7795 case cts_char:
7796 gcc_assert (!specs->long_p && !specs->short_p);
7797 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7798 if (specs->signed_p)
7799 specs->type = signed_char_type_node;
7800 else if (specs->unsigned_p)
7801 specs->type = unsigned_char_type_node;
7802 else
7803 specs->type = char_type_node;
7804 if (specs->complex_p)
7805 {
fcf73884
MLI
7806 pedwarn (OPT_pedantic,
7807 "ISO C does not support complex integer types");
98c3a782
JM
7808 specs->type = build_complex_type (specs->type);
7809 }
7810 break;
7811 case cts_int:
7812 gcc_assert (!(specs->long_p && specs->short_p));
7813 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7814 if (specs->long_long_p)
7815 specs->type = (specs->unsigned_p
7816 ? long_long_unsigned_type_node
7817 : long_long_integer_type_node);
7818 else if (specs->long_p)
7819 specs->type = (specs->unsigned_p
7820 ? long_unsigned_type_node
7821 : long_integer_type_node);
7822 else if (specs->short_p)
7823 specs->type = (specs->unsigned_p
7824 ? short_unsigned_type_node
7825 : short_integer_type_node);
7826 else
7827 specs->type = (specs->unsigned_p
7828 ? unsigned_type_node
7829 : integer_type_node);
7830 if (specs->complex_p)
7831 {
fcf73884
MLI
7832 pedwarn (OPT_pedantic,
7833 "ISO C does not support complex integer types");
98c3a782
JM
7834 specs->type = build_complex_type (specs->type);
7835 }
7836 break;
7837 case cts_float:
7838 gcc_assert (!specs->long_p && !specs->short_p
7839 && !specs->signed_p && !specs->unsigned_p);
7840 specs->type = (specs->complex_p
7841 ? complex_float_type_node
7842 : float_type_node);
7843 break;
7844 case cts_double:
7845 gcc_assert (!specs->long_long_p && !specs->short_p
7846 && !specs->signed_p && !specs->unsigned_p);
7847 if (specs->long_p)
7848 {
7849 specs->type = (specs->complex_p
7850 ? complex_long_double_type_node
7851 : long_double_type_node);
7852 }
7853 else
7854 {
7855 specs->type = (specs->complex_p
7856 ? complex_double_type_node
7857 : double_type_node);
7858 }
7859 break;
9a8ce21f
JG
7860 case cts_dfloat32:
7861 case cts_dfloat64:
7862 case cts_dfloat128:
7863 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
7864 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
7865 if (specs->typespec_word == cts_dfloat32)
7866 specs->type = dfloat32_type_node;
7867 else if (specs->typespec_word == cts_dfloat64)
7868 specs->type = dfloat64_type_node;
7869 else
7870 specs->type = dfloat128_type_node;
7871 break;
ab22c1fa
CF
7872 case cts_fract:
7873 gcc_assert (!specs->complex_p);
7874 if (specs->saturating_p)
7875 {
7876 if (specs->long_long_p)
7877 specs->type = specs->unsigned_p
7878 ? sat_unsigned_long_long_fract_type_node
7879 : sat_long_long_fract_type_node;
7880 else if (specs->long_p)
7881 specs->type = specs->unsigned_p
7882 ? sat_unsigned_long_fract_type_node
7883 : sat_long_fract_type_node;
7884 else if (specs->short_p)
7885 specs->type = specs->unsigned_p
7886 ? sat_unsigned_short_fract_type_node
7887 : sat_short_fract_type_node;
7888 else
7889 specs->type = specs->unsigned_p
7890 ? sat_unsigned_fract_type_node
7891 : sat_fract_type_node;
7892 }
7893 else
7894 {
7895 if (specs->long_long_p)
7896 specs->type = specs->unsigned_p
7897 ? unsigned_long_long_fract_type_node
7898 : long_long_fract_type_node;
7899 else if (specs->long_p)
7900 specs->type = specs->unsigned_p
7901 ? unsigned_long_fract_type_node
7902 : long_fract_type_node;
7903 else if (specs->short_p)
7904 specs->type = specs->unsigned_p
7905 ? unsigned_short_fract_type_node
7906 : short_fract_type_node;
7907 else
7908 specs->type = specs->unsigned_p
7909 ? unsigned_fract_type_node
7910 : fract_type_node;
7911 }
7912 break;
7913 case cts_accum:
7914 gcc_assert (!specs->complex_p);
7915 if (specs->saturating_p)
7916 {
7917 if (specs->long_long_p)
7918 specs->type = specs->unsigned_p
7919 ? sat_unsigned_long_long_accum_type_node
7920 : sat_long_long_accum_type_node;
7921 else if (specs->long_p)
7922 specs->type = specs->unsigned_p
7923 ? sat_unsigned_long_accum_type_node
7924 : sat_long_accum_type_node;
7925 else if (specs->short_p)
7926 specs->type = specs->unsigned_p
7927 ? sat_unsigned_short_accum_type_node
7928 : sat_short_accum_type_node;
7929 else
7930 specs->type = specs->unsigned_p
7931 ? sat_unsigned_accum_type_node
7932 : sat_accum_type_node;
7933 }
7934 else
7935 {
7936 if (specs->long_long_p)
7937 specs->type = specs->unsigned_p
7938 ? unsigned_long_long_accum_type_node
7939 : long_long_accum_type_node;
7940 else if (specs->long_p)
7941 specs->type = specs->unsigned_p
7942 ? unsigned_long_accum_type_node
7943 : long_accum_type_node;
7944 else if (specs->short_p)
7945 specs->type = specs->unsigned_p
7946 ? unsigned_short_accum_type_node
7947 : short_accum_type_node;
7948 else
7949 specs->type = specs->unsigned_p
7950 ? unsigned_accum_type_node
7951 : accum_type_node;
7952 }
7953 break;
98c3a782
JM
7954 default:
7955 gcc_unreachable ();
7956 }
7957
7958 return specs;
7959}
7960
db5f8b93
RH
7961/* A subroutine of c_write_global_declarations. Perform final processing
7962 on one file scope's declarations (or the external scope's declarations),
7963 GLOBALS. */
7964
f75fbaf7
ZW
7965static void
7966c_write_global_declarations_1 (tree globals)
d1bd0ded 7967{
d1bd0ded 7968 tree decl;
df4b9cfe 7969 bool reconsider;
118a3a8b 7970
f75fbaf7 7971 /* Process the decls in the order they were written. */
db5f8b93 7972 for (decl = globals; decl; decl = TREE_CHAIN (decl))
bc4b653b 7973 {
bc4b653b
JM
7974 /* Check for used but undefined static functions using the C
7975 standard's definition of "used", and set TREE_NO_WARNING so
7976 that check_global_declarations doesn't repeat the check. */
7977 if (TREE_CODE (decl) == FUNCTION_DECL
7978 && DECL_INITIAL (decl) == 0
7979 && DECL_EXTERNAL (decl)
7980 && !TREE_PUBLIC (decl)
7981 && C_DECL_USED (decl))
7982 {
fcf73884 7983 pedwarn (0, "%q+F used but never defined", decl);
bc4b653b
JM
7984 TREE_NO_WARNING (decl) = 1;
7985 }
db5f8b93
RH
7986
7987 wrapup_global_declaration_1 (decl);
bc4b653b 7988 }
df4b9cfe
JJ
7989
7990 do
7991 {
7992 reconsider = false;
7993 for (decl = globals; decl; decl = TREE_CHAIN (decl))
7994 reconsider |= wrapup_global_declaration_2 (decl);
7995 }
7996 while (reconsider);
7997
7998 for (decl = globals; decl; decl = TREE_CHAIN (decl))
7999 check_global_declaration_1 (decl);
db5f8b93 8000}
d1bd0ded 8001
db5f8b93
RH
8002/* A subroutine of c_write_global_declarations Emit debug information for each
8003 of the declarations in GLOBALS. */
118a3a8b 8004
db5f8b93
RH
8005static void
8006c_write_global_declarations_2 (tree globals)
8007{
8008 tree decl;
8009
8010 for (decl = globals; decl ; decl = TREE_CHAIN (decl))
8011 debug_hooks->global_decl (decl);
d1bd0ded
GK
8012}
8013
db5f8b93
RH
8014/* Preserve the external declarations scope across a garbage collect. */
8015static GTY(()) tree ext_block;
8016
d1bd0ded 8017void
f75fbaf7 8018c_write_global_declarations (void)
d1bd0ded 8019{
db5f8b93 8020 tree t;
e13e48e7 8021
f75fbaf7
ZW
8022 /* We don't want to do this if generating a PCH. */
8023 if (pch_file)
8024 return;
d1bd0ded 8025
9affb2c7
ZW
8026 /* Don't waste time on further processing if -fsyntax-only or we've
8027 encountered errors. */
8028 if (flag_syntax_only || errorcount || sorrycount || cpp_errors (parse_in))
8029 return;
8030
36c1b0de
ZW
8031 /* Close the external scope. */
8032 ext_block = pop_scope ();
8033 external_scope = 0;
366de0ce 8034 gcc_assert (!current_scope);
36c1b0de 8035
7f3b2bda
GDR
8036 if (ext_block)
8037 {
8038 tree tmp = BLOCK_VARS (ext_block);
8039 int flags;
8040 FILE * stream = dump_begin (TDI_tu, &flags);
8041 if (stream && tmp)
c22cacf3
MS
8042 {
8043 dump_node (tmp, flags & ~TDF_SLIM, stream);
8044 dump_end (TDI_tu, stream);
8045 }
7f3b2bda 8046 }
db5f8b93
RH
8047
8048 /* Process all file scopes in this compilation, and the external_scope,
8049 through wrapup_global_declarations and check_global_declarations. */
8050 for (t = all_translation_units; t; t = TREE_CHAIN (t))
8051 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
36c1b0de
ZW
8052 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
8053
36c1b0de
ZW
8054 /* We're done parsing; proceed to optimize and emit assembly.
8055 FIXME: shouldn't be the front end's responsibility to call this. */
8056 cgraph_optimize ();
db5f8b93
RH
8057
8058 /* After cgraph has had a chance to emit everything that's going to
8059 be emitted, output debug information for globals. */
8060 if (errorcount == 0 && sorrycount == 0)
8061 {
8062 timevar_push (TV_SYMOUT);
8063 for (t = all_translation_units; t; t = TREE_CHAIN (t))
8064 c_write_global_declarations_2 (BLOCK_VARS (DECL_INITIAL (t)));
8065 c_write_global_declarations_2 (BLOCK_VARS (ext_block));
8066 timevar_pop (TV_SYMOUT);
8067 }
8068
8069 ext_block = NULL;
d1bd0ded
GK
8070}
8071
e2500fed 8072#include "gt-c-decl.h"