]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c-decl.c
gcc_release (announce_snapshot): Use changedir instead of plain cd.
[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,
85b58ca5 3 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
51e29401 4
1322177d 5This file is part of GCC.
51e29401 6
1322177d
LB
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 2, or (at your option) any later
10version.
51e29401 11
1322177d
LB
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
51e29401
RS
16
17You should have received a copy of the GNU General Public License
1322177d
LB
18along with GCC; see the file COPYING. If not, write to the Free
19Software Foundation, 59 Temple Place - Suite 330, Boston, MA
2002111-1307, USA. */
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
ZW
31#include "coretypes.h"
32#include "tm.h"
993c790e 33#include "intl.h"
51e29401 34#include "tree.h"
4838c5ee 35#include "tree-inline.h"
051c57da 36#include "rtl.h"
51e29401 37#include "flags.h"
49ad7cfa 38#include "function.h"
e14417fa 39#include "output.h"
051c57da 40#include "expr.h"
51e29401 41#include "c-tree.h"
5f6da302 42#include "toplev.h"
1526a060 43#include "ggc.h"
809d4ef1 44#include "tm_p.h"
a0d85b75 45#include "cpplib.h"
672a6f42 46#include "target.h"
e1772ac0 47#include "debug.h"
40e941af 48#include "opts.h"
b932f770 49#include "timevar.h"
26f943fd 50#include "c-common.h"
ecb0eece 51#include "c-pragma.h"
1c4a429a 52#include "cgraph.h"
66ea6f4c 53#include "hashtab.h"
0bfa5f65
RH
54#include "libfuncs.h"
55#include "except.h"
48873ed2 56#include "langhooks-def.h"
a0d85b75 57
51e29401
RS
58/* In grokdeclarator, distinguish syntactic contexts of declarators. */
59enum decl_context
60{ NORMAL, /* Ordinary declaration */
61 FUNCDEF, /* Function definition */
62 PARM, /* Declaration of parm before function body */
63 FIELD, /* Declaration inside struct or union */
51e29401
RS
64 TYPENAME}; /* Typename (inside cast or sizeof) */
65
51e29401 66\f
51e29401
RS
67/* Nonzero if we have seen an invalid cross reference
68 to a struct, union, or enum, but not yet printed the message. */
69
70tree pending_invalid_xref;
71/* File and line to appear in the eventual error message. */
070588f0 72location_t pending_invalid_xref_location;
51e29401
RS
73
74/* While defining an enum type, this is 1 plus the last enumerator
18192b41
RK
75 constant value. Note that will do not have to save this or `enum_overflow'
76 around nested function definition since such a definition could only
77 occur in an enum value expression and we don't use these variables in
78 that case. */
51e29401
RS
79
80static tree enum_next_value;
81
93e3ba4f
RS
82/* Nonzero means that there was overflow computing enum_next_value. */
83
84static int enum_overflow;
85
77dbdb57
ZW
86/* These #defines are for clarity in working with the information block
87 returned by get_parm_info. */
88#define ARG_INFO_PARMS(args) TREE_PURPOSE(args)
89#define ARG_INFO_TAGS(args) TREE_VALUE(args)
90#define ARG_INFO_TYPES(args) TREE_CHAIN(args)
91#define ARG_INFO_OTHERS(args) TREE_TYPE(args)
51e29401 92
77dbdb57
ZW
93/* The file and line that the prototype came from if this is an
94 old-style definition; used for diagnostics in
95 store_parm_decls_oldstyle. */
eb1dfbb2 96
95035b6f 97static location_t current_function_prototype_locus;
50a9145c 98
8f17b5c5
MM
99/* The current statement tree. */
100
e2500fed 101static GTY(()) struct stmt_tree_s c_stmt_tree;
8f17b5c5
MM
102
103/* The current scope statement stack. */
104
e2500fed 105static GTY(()) tree c_scope_stmt_stack;
8f17b5c5 106
6614fd40 107/* State saving variables. */
e13e48e7
EC
108int c_in_iteration_stmt;
109int c_in_case_stmt;
110
339a28b9
ZW
111/* A list of external DECLs that appeared at block scope when there was
112 some other global meaning for that identifier. */
113static GTY(()) tree truly_local_externals;
114
fdc49e10 115/* All the builtins; this is a subset of the entries of global_scope. */
d1bd0ded 116
fdc49e10
ZW
117static GTY(()) tree first_builtin_decl;
118static GTY(()) tree last_builtin_decl;
d1bd0ded
GK
119
120/* A DECL for the current file-scope context. */
121
122static GTY(()) tree current_file_decl;
123
51e29401
RS
124/* Set to 0 at beginning of a function definition, set to 1 if
125 a return statement that specifies a return value is seen. */
126
127int current_function_returns_value;
128
129/* Set to 0 at beginning of a function definition, set to 1 if
130 a return statement with no argument is seen. */
131
132int current_function_returns_null;
133
5ce89b2e
JM
134/* Set to 0 at beginning of a function definition, set to 1 if
135 a call to a noreturn function is seen. */
136
137int current_function_returns_abnormally;
138
51e29401
RS
139/* Set to nonzero by `grokdeclarator' for a function
140 whose return type is defaulted, if warnings for this are desired. */
141
142static int warn_about_return_type;
143
929f3671 144/* Nonzero when starting a function declared `extern inline'. */
51e29401
RS
145
146static int current_extern_inline;
147\f
f91f41b2
ZW
148/* Each c_scope structure describes the complete contents of one scope.
149 Three scopes are distinguished specially: the innermost or current
150 scope, the innermost function scope, and the outermost or file scope.
151
152 Most declarations are recorded in the current scope.
153
154 All normal label declarations are recorded in the innermost
155 function scope, as are bindings of undeclared identifiers to
156 error_mark_node. (GCC permits nested functions as an extension,
157 hence the 'innermost' qualifier.) Explicitly declared labels
158 (using the __label__ extension) appear in the current scope.
159
160 Being in the global scope (current_scope == global_scope) causes
161 special behavior in several places below. Also, under some
162 conditions the Objective-C front end records declarations in the
163 global scope even though that isn't the current scope.
164
165 The order of the names, parms, and blocks lists matters, and they
166 are frequently appended to. To avoid having to walk all the way to
167 the end of the list on each insertion, or reverse the lists later,
168 we maintain a pointer to the last list entry for each of the lists.
169
bf7a697f 170 The order of the tags, shadowed, and shadowed_tags
f91f41b2 171 lists does not matter, so we just prepend to these lists. */
51e29401 172
f8521984 173struct c_scope GTY(())
14077d68
ZW
174{
175 /* The scope containing this one. */
176 struct c_scope *outer;
177
178 /* The next outermost function scope. */
179 struct c_scope *outer_function;
180
f91f41b2 181 /* All variables, constants, functions, labels, and typedef names. */
14077d68 182 tree names;
f91f41b2 183 tree names_last;
14077d68 184
55d54003 185 /* All parameter declarations. Used only in the outermost scope of
f91f41b2 186 a function. */
55d54003 187 tree parms;
f91f41b2 188 tree parms_last;
55d54003 189
14077d68
ZW
190 /* All structure, union, and enum type tags. */
191 tree tags;
192
193 /* For each scope, a list of shadowed outer-scope definitions
194 to be restored when this scope is popped.
195 Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
196 whose TREE_VALUE is its old definition (a kind of ..._DECL node). */
197 tree shadowed;
198
199 /* For each scope, a list of shadowed outer-scope tag definitions
200 to be restored when this scope is popped.
201 Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
202 whose TREE_VALUE is its old definition (a kind of ..._TYPE node). */
203 tree shadowed_tags;
204
205 /* For each scope (except the global one), a chain of BLOCK nodes
206 for all the scopes that were entered and exited one level down. */
207 tree blocks;
f91f41b2 208 tree blocks_last;
14077d68 209
14077d68
ZW
210 /* True if we are currently filling this scope with parameter
211 declarations. */
1c62e7b2 212 BOOL_BITFIELD parm_flag : 1;
14077d68 213
55d54003
ZW
214 /* True if we already complained about forward parameter decls
215 in this scope. This prevents double warnings on
216 foo (int a; int b; ...) */
1c62e7b2 217 BOOL_BITFIELD warned_forward_parm_decls : 1;
55d54003 218
14077d68
ZW
219 /* True if this is the outermost block scope of a function body.
220 This scope contains the parameters, the local variables declared
221 in the outermost block, and all the labels (except those in
222 nested functions, or declared at block scope with __label__). */
1c62e7b2 223 BOOL_BITFIELD function_body : 1;
14077d68
ZW
224
225 /* True means make a BLOCK for this scope no matter what. */
1c62e7b2 226 BOOL_BITFIELD keep : 1;
14077d68 227};
51e29401 228
f8521984 229/* The scope currently in effect. */
6645c3fa 230
f8521984 231static GTY(()) struct c_scope *current_scope;
51e29401 232
f8521984 233/* A chain of c_scope structures awaiting reuse. */
51e29401 234
f8521984 235static GTY((deletable (""))) struct c_scope *scope_freelist;
51e29401 236
14e33ee8
ZW
237/* The innermost function scope. Ordinary (not explicitly declared)
238 labels, bindings to error_mark_node, and the lazily-created
239 bindings of __func__ and its friends get this scope. */
14077d68 240
f8521984 241static GTY(()) struct c_scope *current_function_scope;
14e33ee8 242
f8521984
ZW
243/* The outermost scope, corresponding to the C "file scope". This is
244 created when the compiler is started and exists through the entire run. */
51e29401 245
f8521984 246static GTY(()) struct c_scope *global_scope;
51e29401 247
3d93cdfa 248/* Append VAR to LIST in scope SCOPE. */
f91f41b2
ZW
249#define SCOPE_LIST_APPEND(scope, list, decl) do { \
250 struct c_scope *s_ = (scope); \
251 tree d_ = (decl); \
252 if (s_->list##_last) \
253 TREE_CHAIN (s_->list##_last) = d_; \
254 else \
255 s_->list = d_; \
256 s_->list##_last = d_; \
257} while (0)
258
259/* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
260#define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
261 struct c_scope *t_ = (tscope); \
262 struct c_scope *f_ = (fscope); \
263 if (t_->to##_last) \
264 TREE_CHAIN (t_->to##_last) = f_->from; \
265 else \
266 t_->to = f_->from; \
267 t_->to##_last = f_->from##_last; \
268} while (0)
269
f8521984 270/* True means unconditionally make a BLOCK for the next scope pushed. */
51e29401 271
14e33ee8 272static bool keep_next_level_flag;
51e29401 273
a8ccdffe
ZW
274/* True means the next call to pushlevel will be the outermost scope
275 of a function body, so do not push a new scope, merely cease
276 expecting parameter decls. */
6645c3fa 277
a8ccdffe 278static bool next_is_function_body;
51e29401 279
2c5f4139
JM
280/* Functions called automatically at the beginning and end of execution. */
281
282tree static_ctors, static_dtors;
283
51e29401
RS
284/* Forward declarations. */
285
f8521984
ZW
286static struct c_scope *make_scope (void);
287static void pop_scope (void);
14e33ee8 288static tree make_label (tree, location_t);
f8521984 289static void bind_label (tree, tree, struct c_scope *);
35b1a6fa 290static void implicit_decl_warning (tree);
35b1a6fa
AJ
291static tree lookup_tag (enum tree_code, tree, int);
292static tree lookup_name_current_level (tree);
2ff7cce4 293static tree grokdeclarator (tree, tree, enum decl_context, int, tree *);
35b1a6fa
AJ
294static tree grokparms (tree, int);
295static void layout_array_type (tree);
296static tree c_make_fname_decl (tree, int);
297static void c_expand_body_1 (tree, int);
298static tree any_external_decl (tree);
299static void record_external_decl (tree);
300static void warn_if_shadowing (tree, tree);
2ff7cce4 301static void check_bitfield_type_and_width (tree *, tree *, const char *);
35b1a6fa
AJ
302static void clone_underlying_type (tree);
303static bool flexible_array_type_p (tree);
d1bd0ded
GK
304static hashval_t link_hash_hash (const void *);
305static int link_hash_eq (const void *, const void *);
51e29401 306\f
e23bd218
IR
307/* States indicating how grokdeclarator() should handle declspecs marked
308 with __attribute__((deprecated)). An object declared as
309 __attribute__((deprecated)) suppresses warnings of uses of other
310 deprecated items. */
35b1a6fa 311
e23bd218
IR
312enum deprecated_states {
313 DEPRECATED_NORMAL,
314 DEPRECATED_SUPPRESS
315};
316
317static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
318
51e29401 319void
35b1a6fa 320c_print_identifier (FILE *file, tree node, int indent)
51e29401 321{
339a28b9
ZW
322 print_node (file, "symbol", IDENTIFIER_SYMBOL_VALUE (node), indent + 4);
323 print_node (file, "tag", IDENTIFIER_TAG_VALUE (node), indent + 4);
51e29401 324 print_node (file, "label", IDENTIFIER_LABEL_VALUE (node), indent + 4);
0e5921e8
ZW
325 if (C_IS_RESERVED_WORD (node))
326 {
327 tree rid = ridpointers[C_RID_CODE (node)];
328 indent_to (file, indent + 4);
75b6f3fd
KG
329 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
330 (void *) rid, IDENTIFIER_POINTER (rid));
0e5921e8 331 }
51e29401
RS
332}
333\f
b4892310 334/* Hook called at end of compilation to assume 1 elt
f8521984 335 for a file-scope tentative array defn that wasn't complete before. */
6645c3fa 336
b4892310 337void
35b1a6fa 338c_finish_incomplete_decl (tree decl)
b4892310 339{
5cfac96e 340 if (TREE_CODE (decl) == VAR_DECL)
b4892310
RS
341 {
342 tree type = TREE_TYPE (decl);
5cfac96e
RK
343 if (type != error_mark_node
344 && TREE_CODE (type) == ARRAY_TYPE
53c5b5d7 345 && ! DECL_EXTERNAL (decl)
5cfac96e 346 && TYPE_DOMAIN (type) == 0)
b4892310 347 {
ddd2d57e 348 warning ("%Jarray '%D' assumed to have one element", decl, decl);
5cfac96e 349
b4892310
RS
350 complete_array_type (type, NULL_TREE, 1);
351
352 layout_decl (decl, 0);
353 }
354 }
355}
356\f
f8521984 357/* Reuse or create a struct for this scope. */
51e29401 358
f8521984
ZW
359static struct c_scope *
360make_scope (void)
51e29401 361{
f8521984
ZW
362 struct c_scope *result;
363 if (scope_freelist)
e2500fed 364 {
f8521984
ZW
365 result = scope_freelist;
366 scope_freelist = result->outer;
e2500fed
GK
367 }
368 else
f8521984 369 result = ggc_alloc_cleared (sizeof (struct c_scope));
e2500fed 370
339a28b9 371 return result;
6970c06a
ZW
372}
373
f8521984
ZW
374/* Remove the topmost scope from the stack and add it to the
375 free list, updating current_function_scope if necessary. */
e2500fed
GK
376
377static void
f8521984 378pop_scope (void)
e2500fed 379{
f8521984 380 struct c_scope *scope = current_scope;
35b1a6fa 381
f8521984 382 current_scope = scope->outer;
14e33ee8 383 if (scope->function_body)
f8521984 384 current_function_scope = scope->outer_function;
14e33ee8 385
f8521984
ZW
386 memset (scope, 0, sizeof (struct c_scope));
387 scope->outer = scope_freelist;
388 scope_freelist = scope;
51e29401
RS
389}
390
264fa2db
ZL
391/* The Objective-C front-end often needs to determine the current scope. */
392
393void *
394get_current_scope (void)
395{
396 return current_scope;
397}
398
399/* The following function is used only by Objective-C. It needs to live here
400 because it accesses the innards of c_scope. */
401
402void
403objc_mark_locals_volatile (void *enclosing_blk)
404{
405 struct c_scope *scope;
e13e48e7
EC
406
407 for (scope = current_scope;
264fa2db
ZL
408 scope && scope != enclosing_blk;
409 scope = scope->outer)
410 {
411 tree decl;
e13e48e7 412
264fa2db
ZL
413 for (decl = scope->names; decl; decl = TREE_CHAIN (decl))
414 {
415 DECL_REGISTER (decl) = 0;
416 TREE_THIS_VOLATILE (decl) = 1;
417 }
418 /* Do not climb up past the current function. */
419 if (scope->function_body)
420 break;
e13e48e7
EC
421 }
422}
423
f8521984 424/* Nonzero if we are currently in the global scope. */
51e29401
RS
425
426int
35b1a6fa 427global_bindings_p (void)
51e29401 428{
f8521984 429 return current_scope == global_scope;
51e29401
RS
430}
431
432void
35b1a6fa 433keep_next_level (void)
51e29401 434{
14e33ee8 435 keep_next_level_flag = true;
51e29401
RS
436}
437
f8521984 438/* Identify this scope as currently being filled with parameters. */
51e29401
RS
439
440void
eb1dfbb2 441declare_parm_level (void)
51e29401 442{
f8521984 443 current_scope->parm_flag = true;
51e29401
RS
444}
445
446/* Nonzero if currently making parm declarations. */
447
448int
35b1a6fa 449in_parm_level_p (void)
51e29401 450{
f8521984 451 return current_scope->parm_flag;
51e29401
RS
452}
453
14077d68
ZW
454/* Enter a new scope. The dummy parameter is for signature
455 compatibility with lang_hooks.decls.pushlevel. */
51e29401
RS
456
457void
35b1a6fa 458pushlevel (int dummy ATTRIBUTE_UNUSED)
51e29401 459{
a8ccdffe 460 if (next_is_function_body)
51e29401 461 {
339a28b9
ZW
462 /* This is the transition from the parameters to the top level
463 of the function body. These are the same scope
f8521984 464 (C99 6.2.1p4,6) so we do not push another scope structure.
a8ccdffe
ZW
465 next_is_function_body is set only by store_parm_decls, which
466 in turn is called when and only when we are about to
467 encounter the opening curly brace for the function body.
339a28b9 468
a8ccdffe
ZW
469 The outermost block of a function always gets a BLOCK node,
470 because the debugging output routines expect that each
59e4e217 471 function has at least one BLOCK. */
f8521984
ZW
472 current_scope->parm_flag = false;
473 current_scope->function_body = true;
a8ccdffe 474 current_scope->keep = true;
f8521984
ZW
475 current_scope->outer_function = current_function_scope;
476 current_function_scope = current_scope;
14e33ee8
ZW
477
478 keep_next_level_flag = false;
a8ccdffe 479 next_is_function_body = false;
51e29401 480 }
339a28b9
ZW
481 else
482 {
f8521984 483 struct c_scope *scope = make_scope ();
51e29401 484
f8521984
ZW
485 scope->keep = keep_next_level_flag;
486 scope->outer = current_scope;
487 current_scope = scope;
488 keep_next_level_flag = false;
339a28b9 489 }
0500d6f9 490}
6645c3fa 491
f8521984
ZW
492/* Exit a scope. Restore the state of the identifier-decl mappings
493 that were in effect when this scope was entered.
51e29401 494
f8521984
ZW
495 If KEEP is KEEP_YES (1), this scope had explicit declarations, so
496 create a BLOCK node to record its declarations and subblocks for
497 debugging output. If KEEP is KEEP_MAYBE, do so only if the names
498 or tags lists are nonempty.
51e29401 499
f91f41b2
ZW
500 The second parameter is ignored; it is present only for
501 signature compatibility with lang_hooks.decls.poplevel.
14077d68 502
51e29401 503 If FUNCTIONBODY is nonzero, this level is the body of a function,
14077d68
ZW
504 even if current_scope->function_body is not set. This is used
505 by language-independent code that generates synthetic functions,
506 and cannot set current_scope->function_body.
51e29401 507
14077d68 508 FIXME: Eliminate the need for all arguments. */
51e29401
RS
509
510tree
f91f41b2 511poplevel (int keep, int dummy ATTRIBUTE_UNUSED, int functionbody)
51e29401 512{
f91f41b2 513 struct c_scope *scope = current_scope;
339a28b9
ZW
514 tree block;
515 tree decl;
f91f41b2 516 tree p;
51e29401 517
6614fd40 518 /* The following line does not use |= due to a bug in HP's C compiler. */
dd5c7759 519 scope->function_body = scope->function_body | functionbody;
9cd51ef6
ZW
520
521 if (keep == KEEP_MAYBE)
f91f41b2
ZW
522 keep = (scope->names || scope->tags);
523
524 keep |= scope->keep;
525 keep |= scope->function_body;
526
527 /* If appropriate, create a BLOCK to record the decls for the life
528 of this function. */
529 block = 0;
530 if (keep)
531 {
532 block = make_node (BLOCK);
533 BLOCK_VARS (block) = scope->names;
534 BLOCK_SUBBLOCKS (block) = scope->blocks;
535 TREE_USED (block) = 1;
536 }
537
538 /* In each subblock, record that this is its superior. */
539 for (p = scope->blocks; p; p = TREE_CHAIN (p))
540 BLOCK_SUPERCONTEXT (p) = block;
541
542 /* Clear out the variable bindings in this scope.
339a28b9 543
339a28b9 544 Propagate TREE_ADDRESSABLE from nested functions to their
f91f41b2
ZW
545 containing functions.
546
547 Issue warnings for unused variables and labels, and errors for
548 undefined labels, if there are any. */
549
550 for (p = scope->names; p; p = TREE_CHAIN (p))
339a28b9 551 {
f91f41b2 552 switch (TREE_CODE (p))
14e33ee8 553 {
f91f41b2
ZW
554 case LABEL_DECL:
555 if (TREE_USED (p) && !DECL_INITIAL (p))
14e33ee8 556 {
ddd2d57e 557 error ("%Jlabel `%D' used but not defined", p, p);
f91f41b2 558 DECL_INITIAL (p) = error_mark_node;
14e33ee8 559 }
f91f41b2 560 else if (!TREE_USED (p) && warn_unused_label)
14e33ee8 561 {
f91f41b2 562 if (DECL_INITIAL (p))
ddd2d57e 563 warning ("%Jlabel `%D' defined but not used", p, p);
14e33ee8 564 else
ddd2d57e 565 warning ("%Jlabel `%D' declared but not defined", p, p);
14e33ee8 566 }
339a28b9 567
f91f41b2
ZW
568 IDENTIFIER_LABEL_VALUE (DECL_NAME (p)) = 0;
569 break;
339a28b9 570
f91f41b2
ZW
571 case FUNCTION_DECL:
572 if (! TREE_ASM_WRITTEN (p)
573 && DECL_INITIAL (p) != 0
574 && TREE_ADDRESSABLE (p)
575 && DECL_ABSTRACT_ORIGIN (p) != 0
576 && DECL_ABSTRACT_ORIGIN (p) != p)
577 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
578 goto normal;
579
580 case VAR_DECL:
beb235f8 581 /* Keep this in sync with stmt.c:warn_about_unused_variables.
f91f41b2
ZW
582 No warnings when the global scope is popped because the
583 global scope isn't popped for the last translation unit,
584 so the warnings are done in c_write_global_declaration. */
585 if (warn_unused_variable && scope != global_scope
586 && !TREE_USED (p)
587 && !DECL_IN_SYSTEM_HEADER (p)
588 && DECL_NAME (p)
589 && !DECL_ARTIFICIAL (p))
ddd2d57e 590 warning ("%Junused variable `%D'", p, p);
a1105617 591 /* Fall through. */
55d54003 592
f91f41b2
ZW
593 default:
594 normal:
595 if (DECL_NAME (p))
596 {
597 if (DECL_EXTERNAL (p) && scope != global_scope)
598 /* External decls stay in the symbol-value slot but are
599 inaccessible. */
600 C_DECL_INVISIBLE (p) = 1;
601 else
602 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (p)) = 0;
603 }
604 break;
605 }
606 }
339a28b9 607
f91f41b2
ZW
608 /* Clear out the parameter bindings in this scope, if any.
609 Unused-parameter warnings are handled by function.c. */
610 for (p = scope->parms; p; p = TREE_CHAIN (p))
611 if (DECL_NAME (p))
612 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (p)) = 0;
339a28b9 613
f91f41b2 614 /* Clear out the tag-meanings declared in this scope.
339a28b9 615
f91f41b2
ZW
616 Set the TYPE_CONTEXTs for all of the tagged types belonging to
617 this scope so that they point to the appropriate construct, i.e.
618 either to the current FUNCTION_DECL node, or else to the BLOCK
619 node we just constructed.
339a28b9 620
f91f41b2
ZW
621 Note that for tagged types whose scope is just the formal
622 parameter list for some function type specification, we can't
623 properly set their TYPE_CONTEXTs here, because we don't have a
624 pointer to the appropriate FUNCTION_TYPE node readily available
625 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
626 type nodes get set in `grokdeclarator' as soon as we have created
627 the FUNCTION_TYPE node which will represent the "scope" for these
628 "parameter list local" tagged types. */
339a28b9 629
f91f41b2
ZW
630 decl = scope->function_body ? current_function_decl : block;
631 for (p = scope->tags; p; p = TREE_CHAIN (p))
339a28b9 632 {
f91f41b2
ZW
633 if (TREE_PURPOSE (p))
634 IDENTIFIER_TAG_VALUE (TREE_PURPOSE (p)) = 0;
635 if (decl)
636 TYPE_CONTEXT (TREE_VALUE (p)) = decl;
339a28b9 637 }
51e29401 638
f91f41b2
ZW
639 /* Restore all name- and label-meanings from outer scopes that were
640 shadowed by this scope. */
641 for (p = scope->shadowed; p; p = TREE_CHAIN (p))
642 if (TREE_VALUE (p) && TREE_CODE (TREE_VALUE (p)) == LABEL_DECL)
643 IDENTIFIER_LABEL_VALUE (TREE_PURPOSE (p)) = TREE_VALUE (p);
644 else
645 IDENTIFIER_SYMBOL_VALUE (TREE_PURPOSE (p)) = TREE_VALUE (p);
8e5a0fcb 646
f91f41b2
ZW
647 /* Restore all tag-meanings from outer scopes that were shadowed by
648 this scope. */
649 for (p = scope->shadowed_tags; p; p = TREE_CHAIN (p))
650 IDENTIFIER_TAG_VALUE (TREE_PURPOSE (p)) = TREE_VALUE (p);
51e29401 651
f91f41b2 652 /* Dispose of the block that we just made inside some higher level. */
af3fbed1 653 if (scope->function_body && current_function_decl)
f91f41b2
ZW
654 DECL_INITIAL (current_function_decl) = block;
655 else if (scope->outer)
3bf40d18 656 {
f91f41b2
ZW
657 if (block)
658 SCOPE_LIST_APPEND (scope->outer, blocks, block);
659 /* If we did not make a block for the scope just exited, any
660 blocks made for inner scopes must be carried forward so they
661 will later become subblocks of something else. */
662 else if (scope->blocks)
663 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
3bf40d18 664 }
51e29401 665
f8521984
ZW
666 /* Pop the current scope, and free the structure for reuse. */
667 pop_scope ();
51e29401 668
51e29401
RS
669 return block;
670}
3bf40d18 671
f8521984
ZW
672/* Insert BLOCK at the end of the list of subblocks of the current
673 scope. This is used when a BIND_EXPR is expanded, to handle the
674 BLOCK node inside the BIND_EXPR. */
3bf40d18
RS
675
676void
35b1a6fa 677insert_block (tree block)
3bf40d18
RS
678{
679 TREE_USED (block) = 1;
f91f41b2 680 SCOPE_LIST_APPEND (current_scope, blocks, block);
3bf40d18
RS
681}
682
339a28b9
ZW
683/* Set the BLOCK node for the innermost scope (the one we are
684 currently in). The RTL expansion machinery requires us to provide
685 this hook, but it is not useful in function-at-a-time mode. */
3bf40d18 686
968e5643 687void
35b1a6fa 688set_block (tree block ATTRIBUTE_UNUSED)
3bf40d18 689{
3bf40d18 690}
51e29401 691\f
51e29401
RS
692/* Push a definition or a declaration of struct, union or enum tag "name".
693 "type" should be the type node.
694 We assume that the tag "name" is not already defined.
695
696 Note that the definition may really be just a forward reference.
697 In that case, the TYPE_SIZE will be zero. */
698
699void
35b1a6fa 700pushtag (tree name, tree type)
51e29401 701{
f8521984 702 struct c_scope *b = current_scope;
51e29401 703
14077d68 704 /* Record the identifier as the type's name if it has none. */
51e29401
RS
705 if (name)
706 {
51e29401
RS
707 if (TYPE_NAME (type) == 0)
708 TYPE_NAME (type) = name;
339a28b9
ZW
709
710 if (IDENTIFIER_TAG_VALUE (name))
711 b->shadowed_tags = tree_cons (name, IDENTIFIER_TAG_VALUE (name),
712 b->shadowed_tags);
713 IDENTIFIER_TAG_VALUE (name) = type;
51e29401
RS
714 }
715
4dd7201e 716 b->tags = tree_cons (name, type, b->tags);
c138f328 717
51e29401 718 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
f8521984 719 tagged type we just added to the current scope. This fake
51e29401 720 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
858a47b1 721 to output a representation of a tagged type, and it also gives
51e29401
RS
722 us a convenient place to record the "scope start" address for the
723 tagged type. */
724
8d9bfdc5 725 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
88dad228
JM
726
727 /* An approximation for now, so we can tell this is a function-scope tag.
728 This will be updated in poplevel. */
729 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
51e29401
RS
730}
731\f
3c6e6fbf 732/* Subroutine of compare_decls. Allow harmless mismatches in return
6907ddd3
RS
733 and argument types provided that the type modes match. This function
734 return a unified type given a suitable match, and 0 otherwise. */
735
736static tree
3c6e6fbf 737match_builtin_function_types (tree newtype, tree oldtype)
6907ddd3
RS
738{
739 tree newrettype, oldrettype;
740 tree newargs, oldargs;
741 tree trytype, tryargs;
742
743 /* Accept the return type of the new declaration if same modes. */
744 oldrettype = TREE_TYPE (oldtype);
745 newrettype = TREE_TYPE (newtype);
746
747 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
748 return 0;
749
750 oldargs = TYPE_ARG_TYPES (oldtype);
751 newargs = TYPE_ARG_TYPES (newtype);
752 tryargs = newargs;
753
754 while (oldargs || newargs)
755 {
756 if (! oldargs
757 || ! newargs
758 || ! TREE_VALUE (oldargs)
759 || ! TREE_VALUE (newargs)
760 || TYPE_MODE (TREE_VALUE (oldargs))
761 != TYPE_MODE (TREE_VALUE (newargs)))
762 return 0;
763
764 oldargs = TREE_CHAIN (oldargs);
765 newargs = TREE_CHAIN (newargs);
766 }
767
768 trytype = build_function_type (newrettype, tryargs);
769 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
770}
771
1f52178b 772/* Subroutine of diagnose_mismatched_decls. Check for function type
3c6e6fbf 773 mismatch involving an empty arglist vs a nonempty one and give clearer
9ac97460 774 diagnostics. */
3c6e6fbf
ZW
775static void
776diagnose_arglist_conflict (tree newdecl, tree olddecl,
777 tree newtype, tree oldtype)
778{
779 tree t;
51e29401 780
3c6e6fbf
ZW
781 if (TREE_CODE (olddecl) != FUNCTION_DECL
782 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype), COMPARE_STRICT)
783 || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
784 ||
785 (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
786 return;
bf44f7de 787
3c6e6fbf
ZW
788 t = TYPE_ARG_TYPES (oldtype);
789 if (t == 0)
790 t = TYPE_ARG_TYPES (newtype);
791 for (; t; t = TREE_CHAIN (t))
792 {
793 tree type = TREE_VALUE (t);
51e29401 794
3c6e6fbf
ZW
795 if (TREE_CHAIN (t) == 0
796 && TYPE_MAIN_VARIANT (type) != void_type_node)
797 {
d4968a11 798 inform ("a parameter list with an ellipsis can't match "
3c6e6fbf
ZW
799 "an empty parameter name list declaration");
800 break;
801 }
802
803 if (c_type_promotes_to (type) != type)
804 {
d4968a11 805 inform ("an argument type that has a default promotion can't match "
3c6e6fbf
ZW
806 "an empty parameter name list declaration");
807 break;
808 }
809 }
810}
51e29401 811
3c6e6fbf
ZW
812/* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
813 old-style function definition, NEWDECL is a prototype declaration.
814 Diagnose inconsistencies in the argument list. Returns TRUE if
815 the prototype is compatible, FALSE if not. */
816static bool
817validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
818{
bc298aa7
ZW
819 tree newargs, oldargs;
820 int i;
821
822 /* ??? Elsewhere TYPE_MAIN_VARIANT is not used in this context. */
823#define END_OF_ARGLIST(t) (TYPE_MAIN_VARIANT (t) == void_type_node)
824
825 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
826 newargs = TYPE_ARG_TYPES (newtype);
827 i = 1;
828
829 for (;;)
9162542e 830 {
bc298aa7
ZW
831 tree oldargtype = TREE_VALUE (oldargs);
832 tree newargtype = TREE_VALUE (newargs);
833
834 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
835 break;
836
837 /* Reaching the end of just one list means the two decls don't
838 agree on the number of arguments. */
839 if (END_OF_ARGLIST (oldargtype))
9162542e 840 {
bc298aa7
ZW
841 error ("%Jprototype for '%D' declares more arguments "
842 "than previous old-style definition", newdecl, newdecl);
843 return false;
9162542e 844 }
bc298aa7 845 else if (END_OF_ARGLIST (newargtype))
3c6e6fbf 846 {
bc298aa7
ZW
847 error ("%Jprototype for '%D' declares fewer arguments "
848 "than previous old-style definition", newdecl, newdecl);
3c6e6fbf
ZW
849 return false;
850 }
bc298aa7
ZW
851
852 /* Type for passing arg must be consistent with that declared
853 for the arg. */
854 else if (! comptypes (oldargtype, newargtype, COMPARE_STRICT))
3c6e6fbf 855 {
bc298aa7
ZW
856 error ("%Jprototype for '%D' declares arg %d with incompatible type",
857 newdecl, newdecl, i);
3c6e6fbf
ZW
858 return false;
859 }
bc298aa7
ZW
860
861 oldargs = TREE_CHAIN (oldargs);
862 newargs = TREE_CHAIN (newargs);
863 i++;
9162542e 864 }
bc298aa7
ZW
865
866 /* If we get here, no errors were found, but do issue a warning
867 for this poor-style construct. */
868 warning ("%Jprototype for '%D' follows non-prototype definition",
869 newdecl, newdecl);
870 return true;
871#undef END_OF_ARGLIST
3c6e6fbf 872}
4b4e3407 873
3c6e6fbf
ZW
874/* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
875 first in a pair of mismatched declarations, using the diagnostic
876 function DIAG. */
877static void
878locate_old_decl (tree decl, void (*diag)(const char *, ...))
879{
880 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
881 ;
882 else if (DECL_INITIAL (decl))
883 diag (N_("%Jprevious definition of '%D' was here"), decl, decl);
884 else if (C_DECL_IMPLICIT (decl))
885 diag (N_("%Jprevious implicit declaration of '%D' was here"), decl, decl);
886 else
887 diag (N_("%Jprevious declaration of '%D' was here"), decl, decl);
888}
51e29401 889
3c6e6fbf
ZW
890/* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
891 Returns true if the caller should proceed to merge the two, false
892 if OLDDECL should simply be discarded. As a side effect, issues
893 all necessary diagnostics for invalid or poor-style combinations.
894 If it returns true, writes the types of NEWDECL and OLDDECL to
895 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
896 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
897
898static bool
899diagnose_mismatched_decls (tree newdecl, tree olddecl,
900 tree *newtypep, tree *oldtypep)
901{
902 tree newtype, oldtype;
903 bool pedwarned = false;
904 bool warned = false;
905
906 /* If we have error_mark_node for either decl or type, just discard
907 the previous decl - we're in an error cascade already. */
908 if (olddecl == error_mark_node || newdecl == error_mark_node)
909 return false;
bc298aa7
ZW
910 *oldtypep = oldtype = TREE_TYPE (olddecl);
911 *newtypep = newtype = TREE_TYPE (newdecl);
3c6e6fbf
ZW
912 if (oldtype == error_mark_node || newtype == error_mark_node)
913 return false;
914
915 /* Two different categories of symbol altogether. This is an error
916 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
51e29401
RS
917 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
918 {
a6f78652
ZW
919 if (TREE_CODE (olddecl) != FUNCTION_DECL
920 || !DECL_BUILT_IN (olddecl) || !C_DECL_INVISIBLE (olddecl))
51e29401 921 {
3c6e6fbf
ZW
922 error ("%J'%D' redeclared as different kind of symbol",
923 newdecl, newdecl);
924 locate_old_decl (olddecl, error);
925 }
926 else if (TREE_PUBLIC (newdecl))
927 warning ("%Jbuilt-in function '%D' declared as non-function",
928 newdecl, newdecl);
929 else if (warn_shadow)
930 warning ("%Jshadowing built-in function '%D'",
931 newdecl, newdecl);
932 return false;
933 }
934
935 if (!comptypes (oldtype, newtype, COMPARE_STRICT))
936 {
a6f78652
ZW
937 if (TREE_CODE (olddecl) == FUNCTION_DECL
938 && DECL_BUILT_IN (olddecl) && C_DECL_INVISIBLE (olddecl))
3c6e6fbf
ZW
939 {
940 /* Accept harmless mismatch in function types.
941 This is for the ffs and fprintf builtins. */
942 tree trytype = match_builtin_function_types (newtype, oldtype);
943
944 if (trytype && comptypes (newtype, trytype, COMPARE_STRICT))
bc298aa7 945 *oldtypep = oldtype = trytype;
3c6e6fbf 946 else
51e29401 947 {
3c6e6fbf
ZW
948 /* If types don't match for a built-in, throw away the
949 built-in. No point in calling locate_old_decl here, it
9ac97460 950 won't print anything. */
3c6e6fbf
ZW
951 warning ("%Jconflicting types for built-in function '%D'",
952 newdecl, newdecl);
953 return false;
51e29401 954 }
3c6e6fbf
ZW
955 }
956 else if (TREE_CODE (olddecl) == FUNCTION_DECL
957 && DECL_SOURCE_LINE (olddecl) == 0)
958 {
959 /* A conflicting function declaration for a predeclared
960 function that isn't actually built in. Objective C uses
961 these. The new declaration silently overrides everything
962 but the volatility (i.e. noreturn) indication. See also
963 below. FIXME: Make Objective C use normal builtins. */
964 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
965 return false;
966 }
967 /* Permit void foo (...) to match int foo (...) if the latter is
968 the definition and implicit int was used. See
969 c-torture/compile/920625-2.c. */
970 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
971 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
972 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
973 && C_FUNCTION_IMPLICIT_INT (newdecl))
974 {
975 pedwarn ("%Jconflicting types for '%D'", newdecl, newdecl);
976 /* Make sure we keep void as the return type. */
bc298aa7 977 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
3c6e6fbf
ZW
978 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
979 pedwarned = true;
51e29401
RS
980 }
981 else
982 {
3c6e6fbf
ZW
983 error ("%Jconflicting types for '%D'", newdecl, newdecl);
984 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
985 locate_old_decl (olddecl, error);
986 return false;
51e29401 987 }
51e29401
RS
988 }
989
3c6e6fbf
ZW
990 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
991 but silently ignore the redeclaration if either is in a system
992 header. (Conflicting redeclarations were handled above.) */
993 if (TREE_CODE (newdecl) == TYPE_DECL)
339a28b9 994 {
3c6e6fbf 995 if (DECL_IN_SYSTEM_HEADER (newdecl) || DECL_IN_SYSTEM_HEADER (olddecl))
a1105617 996 return true; /* Allow OLDDECL to continue in use. */
3c6e6fbf
ZW
997
998 error ("%Jredefinition of typedef '%D'", newdecl, newdecl);
999 locate_old_decl (olddecl, error);
1000 return false;
339a28b9 1001 }
51e29401 1002
3c6e6fbf
ZW
1003 /* Function declarations can either be 'static' or 'extern' (no
1004 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1005 can never conflict with each other on account of linkage (6.2.2p4).
1006 Multiple definitions are not allowed (6.9p3,5) but GCC permits
1007 two definitions if one is 'extern inline' and one is not. The non-
1008 extern-inline definition supersedes the extern-inline definition. */
1009 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
51e29401 1010 {
04b8f97f
ZW
1011 /* If you declare a built-in function name as static, or
1012 define the built-in with an old-style definition (so we
1013 can't validate the argument list) the built-in definition is
1014 overridden, but optionally warn this was a bad choice of name. */
1015 if (DECL_BUILT_IN (olddecl)
a6f78652 1016 && C_DECL_INVISIBLE (olddecl)
04b8f97f
ZW
1017 && (!TREE_PUBLIC (newdecl)
1018 || (DECL_INITIAL (newdecl)
1019 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
51e29401 1020 {
51e29401 1021 if (warn_shadow)
ddd2d57e 1022 warning ("%Jshadowing built-in function '%D'", newdecl, newdecl);
51e29401 1023 /* Discard the old built-in function. */
3c6e6fbf 1024 return false;
51e29401 1025 }
3c6e6fbf
ZW
1026
1027 if (DECL_INITIAL (newdecl))
4c41bbfa 1028 {
3c6e6fbf
ZW
1029 if (DECL_INITIAL (olddecl)
1030 && !(DECL_DECLARED_INLINE_P (olddecl)
1031 && DECL_EXTERNAL (olddecl)
1032 && !(DECL_DECLARED_INLINE_P (newdecl)
1033 && DECL_EXTERNAL (newdecl))))
6907ddd3 1034 {
3c6e6fbf
ZW
1035 error ("%Jredefinition of '%D'", newdecl, newdecl);
1036 locate_old_decl (olddecl, error);
1037 return false;
4c41bbfa
RS
1038 }
1039 }
3c6e6fbf
ZW
1040 /* If we have a prototype after an old-style function definition,
1041 the argument types must be checked specially. */
1042 else if (DECL_INITIAL (olddecl)
1043 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1044 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1045 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
52b6a22f 1046 {
3c6e6fbf
ZW
1047 locate_old_decl (olddecl, error);
1048 return false;
52b6a22f 1049 }
3c6e6fbf
ZW
1050 /* Mismatched non-static and static is considered poor style.
1051 We only diagnose static then non-static if -Wtraditional,
1052 because it is the most convenient way to get some effects
1053 (see e.g. what unwind-dw2-fde-glibc.c does to the definition
1054 of _Unwind_Find_FDE in unwind-dw2-fde.c). Revisit? */
1055 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
51e29401 1056 {
3c6e6fbf
ZW
1057 /* A static function declaration for a predeclared function
1058 that isn't actually built in, silently overrides the
1059 default. Objective C uses these. See also above.
1060 FIXME: Make Objective C use normal builtins. */
1061 if (TREE_CODE (olddecl) == FUNCTION_DECL
1062 && DECL_SOURCE_LINE (olddecl) == 0)
1063 return false;
1064 else
1065 {
1066 warning ("%Jstatic declaration of '%D' follows "
1067 "non-static declaration", newdecl, newdecl);
1068 warned = true;
1069 }
51e29401 1070 }
3c6e6fbf
ZW
1071 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl)
1072 && warn_traditional)
e841f997 1073 {
3c6e6fbf
ZW
1074 warning ("%Jnon-static declaration of '%D' follows "
1075 "static declaration", newdecl, newdecl);
1076 warned = true;
e841f997 1077 }
51e29401 1078 }
3c6e6fbf 1079 else if (TREE_CODE (newdecl) == VAR_DECL)
339a28b9 1080 {
3c6e6fbf
ZW
1081 /* Only variables can be thread-local, and all declarations must
1082 agree on this property. */
1083 if (DECL_THREAD_LOCAL (newdecl) != DECL_THREAD_LOCAL (olddecl))
51e29401 1084 {
3c6e6fbf
ZW
1085 if (DECL_THREAD_LOCAL (newdecl))
1086 error ("%Jthread-local declaration of '%D' follows "
1087 "non-thread-local declaration", newdecl, newdecl);
1088 else
1089 error ("%Jnon-thread-local declaration of '%D' follows "
1090 "thread-local declaration", newdecl, newdecl);
51e29401 1091
3c6e6fbf
ZW
1092 locate_old_decl (olddecl, error);
1093 return false;
1094 }
51e29401 1095
3c6e6fbf
ZW
1096 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1097 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1098 {
1099 error ("%Jredefinition of '%D'", newdecl, newdecl);
1100 locate_old_decl (olddecl, error);
1101 return false;
1102 }
1103
1104 /* Objects declared at file scope: if at least one is 'extern',
1105 it's fine (6.2.2p4); otherwise the linkage must agree (6.2.2p7). */
1106 if (DECL_FILE_SCOPE_P (newdecl))
1107 {
1108 if (!DECL_EXTERNAL (newdecl)
1109 && !DECL_EXTERNAL (olddecl)
1110 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1111 {
1112 if (TREE_PUBLIC (newdecl))
1113 error ("%Jnon-static declaration of '%D' follows "
1114 "static declaration", newdecl, newdecl);
1115 else
1116 error ("%Jstatic declaration of '%D' follows "
1117 "non-static declaration", newdecl, newdecl);
1118
1119 locate_old_decl (olddecl, error);
1120 return false;
51e29401
RS
1121 }
1122 }
3c6e6fbf
ZW
1123 /* Two objects with the same name declared at the same block
1124 scope must both be external references (6.7p3). */
1125 else if (DECL_CONTEXT (newdecl) == DECL_CONTEXT (olddecl)
1126 && (!DECL_EXTERNAL (newdecl) || !DECL_EXTERNAL (olddecl)))
1127 {
1128 if (DECL_EXTERNAL (newdecl))
1129 error ("%Jextern declaration of '%D' follows "
1130 "declaration with no linkage", newdecl, newdecl);
1131 else if (DECL_EXTERNAL (olddecl))
1132 error ("%Jdeclaration of '%D' with no linkage follows "
1133 "extern declaration", newdecl, newdecl);
1134 else
1135 error ("%Jredeclaration of '%D' with no linkage",
1136 newdecl, newdecl);
ecf92f82 1137
3c6e6fbf
ZW
1138 locate_old_decl (olddecl, error);
1139 return false;
1140 }
1ae0ccb6 1141 }
3c6e6fbf
ZW
1142
1143 /* warnings */
1144 /* All decls must agree on a non-default visibility. */
1145 if (DECL_VISIBILITY (newdecl) != VISIBILITY_DEFAULT
1146 && DECL_VISIBILITY (olddecl) != VISIBILITY_DEFAULT
1147 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1ae0ccb6 1148 {
3c6e6fbf
ZW
1149 warning ("%Jredeclaration of '%D' with different visibility "
1150 "(old visibility preserved)", newdecl, newdecl);
1151 warned = true;
1ae0ccb6 1152 }
3c6e6fbf
ZW
1153
1154 if (TREE_CODE (newdecl) == FUNCTION_DECL)
51e29401 1155 {
3c6e6fbf
ZW
1156 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
1157 if (DECL_DECLARED_INLINE_P (newdecl)
1158 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
51e29401 1159 {
3c6e6fbf
ZW
1160 warning ("%Jinline declaration of '%D' follows "
1161 "declaration with attribute noinline", newdecl, newdecl);
1162 warned = true;
51e29401 1163 }
3c6e6fbf
ZW
1164 else if (DECL_DECLARED_INLINE_P (olddecl)
1165 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
4b4e3407 1166 {
3c6e6fbf
ZW
1167 warning ("%Jdeclaration of '%D' with attribute noinline follows "
1168 "inline declaration ", newdecl, newdecl);
1169 warned = true;
4b4e3407 1170 }
3c6e6fbf
ZW
1171
1172 /* Inline declaration after use or definition.
1173 ??? Should we still warn about this now we have unit-at-a-time
1174 mode and can get it right? */
1175 if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl))
51e29401 1176 {
3c6e6fbf 1177 if (TREE_USED (olddecl))
51e29401 1178 {
0bb03c11
JH
1179 warning ("%J'%D' declared inline after being called",
1180 olddecl, olddecl);
3c6e6fbf
ZW
1181 warned = true;
1182 }
1183 else if (DECL_INITIAL (olddecl))
1184 {
0bb03c11
JH
1185 warning ("%J'%D' declared inline after its definition",
1186 olddecl, olddecl);
3c6e6fbf 1187 warned = true;
51e29401 1188 }
51e29401 1189 }
3c6e6fbf 1190 }
3205a71e 1191 else /* PARM_DECL, VAR_DECL */
3c6e6fbf 1192 {
3205a71e
ZW
1193 /* Redeclaration of a PARM_DECL is invalid unless this is the
1194 real position of a forward-declared parameter (GCC extension). */
1195 if (TREE_CODE (newdecl) == PARM_DECL
1196 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1197 {
1198 error ("%Jredefinition of parameter '%D'", newdecl, newdecl);
1199 locate_old_decl (olddecl, error);
1200 return false;
1201 }
1202
3c6e6fbf
ZW
1203 /* These bits are only type qualifiers when applied to objects. */
1204 if (TREE_THIS_VOLATILE (newdecl) != TREE_THIS_VOLATILE (olddecl))
51e29401 1205 {
3c6e6fbf
ZW
1206 if (TREE_THIS_VOLATILE (newdecl))
1207 pedwarn ("%Jvolatile declaration of '%D' follows "
1208 "non-volatile declaration", newdecl, newdecl);
1209 else
1210 pedwarn ("%Jnon-volatile declaration of '%D' follows "
1211 "volatile declaration", newdecl, newdecl);
1212 pedwarned = true;
1213 }
1214 if (TREE_READONLY (newdecl) != TREE_READONLY (olddecl))
1215 {
1216 if (TREE_READONLY (newdecl))
1217 pedwarn ("%Jconst declaration of '%D' follows "
1218 "non-const declaration", newdecl, newdecl);
1219 else
1220 pedwarn ("%Jnon-const declaration of '%D' follows "
1221 "const declaration", newdecl, newdecl);
1222 pedwarned = true;
51e29401
RS
1223 }
1224 }
1225
3c6e6fbf
ZW
1226 /* Optional warning for completely redundant decls. */
1227 if (!warned && !pedwarned
1228 && warn_redundant_decls
1229 /* Don't warn about a function declaration followed by a
1230 definition. */
3205a71e
ZW
1231 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1232 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1233 /* Don't warn about an extern followed by a definition. */
1234 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1235 /* Don't warn about forward parameter decls. */
1236 && !(TREE_CODE (newdecl) == PARM_DECL
1237 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl)))
27f427f8 1238 {
3c6e6fbf
ZW
1239 warning ("%Jredundant redeclaration of '%D'", newdecl, newdecl);
1240 warned = true;
27f427f8
RS
1241 }
1242
3c6e6fbf
ZW
1243 /* Report location of previous decl/defn in a consistent manner. */
1244 if (warned || pedwarned)
1245 locate_old_decl (olddecl, pedwarned ? pedwarn : warning);
664b4b1e 1246
3c6e6fbf
ZW
1247 return true;
1248}
51e29401 1249
3c6e6fbf
ZW
1250/* Subroutine of duplicate_decls. NEWDECL has been found to be
1251 consistent with OLDDECL, but carries new information. Merge the
1ef82ef2 1252 new information into OLDDECL. This function issues no
3c6e6fbf
ZW
1253 diagnostics. */
1254
1255static void
1ef82ef2 1256merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
3c6e6fbf
ZW
1257{
1258 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1259 && DECL_INITIAL (newdecl) != 0);
1260
3c6e6fbf
ZW
1261 /* For real parm decl following a forward decl, return 1 so old decl
1262 will be reused. Only allow this to happen once. */
1263 if (TREE_CODE (newdecl) == PARM_DECL
1264 && TREE_ASM_WRITTEN (olddecl) && ! TREE_ASM_WRITTEN (newdecl))
51e29401 1265 {
3c6e6fbf
ZW
1266 TREE_ASM_WRITTEN (olddecl) = 0;
1267 return;
1268 }
1269
1270 DECL_ATTRIBUTES (newdecl)
1271 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
bf44f7de 1272
3c6e6fbf 1273 /* Merge the data types specified in the two decls. */
1ef82ef2
ZW
1274 TREE_TYPE (newdecl)
1275 = TREE_TYPE (olddecl)
1276 = common_type (newtype, oldtype);
51e29401 1277
3c6e6fbf
ZW
1278 /* Lay the type out, unless already done. */
1279 if (oldtype != TREE_TYPE (newdecl))
1280 {
1281 if (TREE_TYPE (newdecl) != error_mark_node)
1282 layout_type (TREE_TYPE (newdecl));
1283 if (TREE_CODE (newdecl) != FUNCTION_DECL
1284 && TREE_CODE (newdecl) != TYPE_DECL
1285 && TREE_CODE (newdecl) != CONST_DECL)
1286 layout_decl (newdecl, 0);
1287 }
1288 else
1289 {
1290 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1291 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1292 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1293 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1294 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1295 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1296 {
1297 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1298 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1299 }
1300 }
fc542d3c 1301
3c6e6fbf
ZW
1302 /* Keep the old rtl since we can safely use it. */
1303 COPY_DECL_RTL (olddecl, newdecl);
17aec3eb 1304
3c6e6fbf
ZW
1305 /* Merge the type qualifiers. */
1306 if (TREE_READONLY (newdecl))
1ef82ef2 1307 TREE_READONLY (olddecl) = 1;
facef326 1308
3c6e6fbf
ZW
1309 if (TREE_THIS_VOLATILE (newdecl))
1310 {
1ef82ef2 1311 TREE_THIS_VOLATILE (olddecl) = 1;
3c6e6fbf
ZW
1312 if (TREE_CODE (newdecl) == VAR_DECL)
1313 make_var_volatile (newdecl);
51e29401 1314 }
3c6e6fbf
ZW
1315
1316 /* Keep source location of definition rather than declaration. */
1ef82ef2 1317 if (DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
3c6e6fbf
ZW
1318 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1319
1320 /* Merge the unused-warning information. */
1321 if (DECL_IN_SYSTEM_HEADER (olddecl))
1322 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1323 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1ef82ef2 1324 DECL_IN_SYSTEM_HEADER (olddecl) = 1;
3c6e6fbf
ZW
1325
1326 /* Merge the initialization information. */
1ef82ef2 1327 if (DECL_INITIAL (newdecl) == 0)
3c6e6fbf
ZW
1328 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1329
1330 /* Merge the section attribute.
1331 We want to issue an error if the sections conflict but that must be
1332 done later in decl_attributes since we are called before attributes
1333 are assigned. */
1334 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1335 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1336
1337 /* Copy the assembler name.
1338 Currently, it can only be defined in the prototype. */
1339 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1340
1341 /* If either declaration has a nondefault visibility, use it. */
1342 if (DECL_VISIBILITY (olddecl) != VISIBILITY_DEFAULT)
1343 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1344
1345 if (TREE_CODE (newdecl) == FUNCTION_DECL)
51e29401 1346 {
3c6e6fbf
ZW
1347 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1348 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1349 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1350 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1351 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1352 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1353 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1354 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1355 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
51e29401
RS
1356 }
1357
1358 /* Merge the storage class information. */
45806a3f
FS
1359 merge_weak (newdecl, olddecl);
1360
51e29401
RS
1361 /* For functions, static overrides non-static. */
1362 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1363 {
1364 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1365 /* This is since we don't automatically
1366 copy the attributes of NEWDECL into OLDDECL. */
1367 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1368 /* If this clears `static', clear it in the identifier too. */
1369 if (! TREE_PUBLIC (olddecl))
1370 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1371 }
1394aabd 1372 if (DECL_EXTERNAL (newdecl))
51e29401 1373 {
1ef82ef2
ZW
1374 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1375 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1376
51e29401
RS
1377 /* An extern decl does not override previous storage class. */
1378 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
0ecef3cf 1379 if (! DECL_EXTERNAL (newdecl))
d1bd0ded
GK
1380 {
1381 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
b68a40b2 1382 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
d1bd0ded 1383 }
51e29401
RS
1384 }
1385 else
1386 {
1387 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
51e29401
RS
1388 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1389 }
850cba29 1390
bf44f7de 1391 if (TREE_CODE (newdecl) == FUNCTION_DECL)
51e29401 1392 {
5daf7c0a
JM
1393 /* If we're redefining a function previously defined as extern
1394 inline, make sure we emit debug info for the inline before we
1395 throw it away, in case it was inlined into a function that hasn't
1396 been written out yet. */
02a362d9 1397 if (new_is_definition && DECL_INITIAL (olddecl))
5daf7c0a 1398 {
1bb17c21
JH
1399 if (TREE_USED (olddecl)
1400 /* In unit-at-a-time mode we never inline re-defined extern
6614fd40 1401 inline functions. */
b684a3df
JH
1402 && !flag_unit_at_a_time
1403 && cgraph_function_possibly_inlined_p (olddecl))
02a362d9 1404 (*debug_hooks->outlining_inline_function) (olddecl);
17aec3eb 1405
5daf7c0a
JM
1406 /* The new defn must not be inline. */
1407 DECL_INLINE (newdecl) = 0;
1408 DECL_UNINLINABLE (newdecl) = 1;
1409 }
1410 else
1411 {
1412 /* If either decl says `inline', this fn is inline,
1413 unless its definition was passed already. */
31ed8fea 1414 if (DECL_DECLARED_INLINE_P (newdecl)
b79d5213
RH
1415 || DECL_DECLARED_INLINE_P (olddecl))
1416 DECL_DECLARED_INLINE_P (newdecl) = 1;
9162542e
AO
1417
1418 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1419 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
5daf7c0a 1420 }
17aec3eb 1421
51e29401
RS
1422 if (DECL_BUILT_IN (olddecl))
1423 {
a6f78652
ZW
1424 /* If redeclaring a builtin function, it stays built in. */
1425 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1426 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
51e29401 1427 }
b79d5213 1428
bf44f7de 1429 /* Also preserve various other info from the definition. */
bf44f7de
JW
1430 if (! new_is_definition)
1431 {
1432 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1ef82ef2 1433 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1da326c3 1434 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
23700f65 1435 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
bf44f7de 1436 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
b79d5213
RH
1437
1438 /* Set DECL_INLINE on the declaration if we've got a body
1439 from which to instantiate. */
1440 if (DECL_INLINE (olddecl) && ! DECL_UNINLINABLE (newdecl))
1441 {
1442 DECL_INLINE (newdecl) = 1;
1443 DECL_ABSTRACT_ORIGIN (newdecl)
1ef82ef2 1444 = DECL_ABSTRACT_ORIGIN (olddecl);
b79d5213
RH
1445 }
1446 }
1447 else
1448 {
1449 /* If a previous declaration said inline, mark the
1450 definition as inlinable. */
1451 if (DECL_DECLARED_INLINE_P (newdecl)
1452 && ! DECL_UNINLINABLE (newdecl))
1453 DECL_INLINE (newdecl) = 1;
bf44f7de
JW
1454 }
1455 }
51e29401 1456
850cba29 1457 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
e5e809f4 1458 But preserve OLDDECL's DECL_UID. */
850cba29 1459 {
b3694847 1460 unsigned olddecl_uid = DECL_UID (olddecl);
850cba29 1461
da61dec9
JM
1462 memcpy ((char *) olddecl + sizeof (struct tree_common),
1463 (char *) newdecl + sizeof (struct tree_common),
1464 sizeof (struct tree_decl) - sizeof (struct tree_common));
850cba29
RS
1465 DECL_UID (olddecl) = olddecl_uid;
1466 }
51e29401 1467
2306d91c
RH
1468 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1469 so that encode_section_info has a chance to look at the new decl
1470 flags and attributes. */
1471 if (DECL_RTL_SET_P (olddecl)
1472 && (TREE_CODE (olddecl) == FUNCTION_DECL
1473 || (TREE_CODE (olddecl) == VAR_DECL
1474 && TREE_STATIC (olddecl))))
1475 make_decl_rtl (olddecl, NULL);
51e29401
RS
1476}
1477
3c6e6fbf
ZW
1478/* Handle when a new declaration NEWDECL has the same name as an old
1479 one OLDDECL in the same binding contour. Prints an error message
1480 if appropriate.
1481
1482 If safely possible, alter OLDDECL to look like NEWDECL, and return
1ef82ef2 1483 true. Otherwise, return false. */
3c6e6fbf
ZW
1484
1485static bool
1ef82ef2 1486duplicate_decls (tree newdecl, tree olddecl)
3c6e6fbf
ZW
1487{
1488 tree newtype, oldtype;
1489
1490 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
1491 return false;
1492
1ef82ef2
ZW
1493 merge_decls (newdecl, olddecl, newtype, oldtype);
1494 return true;
3c6e6fbf
ZW
1495}
1496
1497\f
339a28b9
ZW
1498/* Return any external DECL associated with ID, whether or not it is
1499 currently in scope. */
29495994 1500
339a28b9 1501static tree
35b1a6fa 1502any_external_decl (tree id)
339a28b9
ZW
1503{
1504 tree decl = IDENTIFIER_SYMBOL_VALUE (id);
1505 tree t;
1506
29495994
RK
1507 if (decl == 0 || TREE_CODE (decl) == ERROR_MARK)
1508 return 0;
1509 else if (TREE_CODE (decl) != TYPE_DECL && DECL_EXTERNAL (decl))
339a28b9
ZW
1510 return decl;
1511
1512 t = purpose_member (id, truly_local_externals);
1513 if (t)
1514 return TREE_VALUE (t);
1515
1516 return 0;
1517}
1518
1519/* Record an external decl DECL. This only does something if a
1520 shadowing decl already exists. */
1521static void
35b1a6fa 1522record_external_decl (tree decl)
339a28b9
ZW
1523{
1524 tree name = DECL_NAME (decl);
1525 if (!IDENTIFIER_SYMBOL_VALUE (name))
1526 return;
1527
1528 truly_local_externals = tree_cons (name, decl, truly_local_externals);
1529}
1530
26f943fd 1531/* Check whether decl-node X shadows an existing declaration.
339a28b9 1532 OLD is the old IDENTIFIER_SYMBOL_VALUE of the DECL_NAME of X,
26f943fd
NB
1533 which might be a NULL_TREE. */
1534static void
35b1a6fa 1535warn_if_shadowing (tree x, tree old)
26f943fd 1536{
339a28b9
ZW
1537 /* Nothing to shadow? */
1538 if (old == 0
1539 /* Shadow warnings not wanted? */
1540 || !warn_shadow
1541 /* No shadow warnings for internally generated vars. */
f31686a3 1542 || DECL_SOURCE_LINE (x) == 0
339a28b9
ZW
1543 /* No shadow warnings for vars made for inlining. */
1544 || DECL_FROM_INLINE (x)
1545 /* Don't warn about the parm names in function declarator
1546 within a function declarator.
1547 It would be nice to avoid warning in any function
1548 declarator in a declaration, as opposed to a definition,
1549 but there is no way to tell it's not a definition. */
14077d68 1550 || (TREE_CODE (x) == PARM_DECL && current_scope->outer->parm_flag))
26f943fd
NB
1551 return;
1552
339a28b9 1553 if (TREE_CODE (old) == PARM_DECL)
a6f78652 1554 warning ("%Jdeclaration of '%D' shadows a parameter", x, x);
4b1e44be 1555 else if (DECL_FILE_SCOPE_P (old))
a6f78652 1556 warning ("%Jdeclaration of '%D' shadows a global declaration", x, x);
339a28b9 1557 else
a6f78652
ZW
1558 warning ("%Jdeclaration of '%D' shadows a previous local", x, x);
1559
1560 warning ("%Jshadowed declaration is here", old);
339a28b9
ZW
1561}
1562
1563
1564/* Subroutine of pushdecl.
1565
1566 X is a TYPE_DECL for a typedef statement. Create a brand new
1567 ..._TYPE node (which will be just a variant of the existing
1568 ..._TYPE node with identical properties) and then install X
1569 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1570
1571 The whole point here is to end up with a situation where each
1572 and every ..._TYPE node the compiler creates will be uniquely
1573 associated with AT MOST one node representing a typedef name.
1574 This way, even though the compiler substitutes corresponding
1575 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1576 early on, later parts of the compiler can always do the reverse
1577 translation and get back the corresponding typedef name. For
1578 example, given:
1579
1580 typedef struct S MY_TYPE;
1581 MY_TYPE object;
1582
1583 Later parts of the compiler might only know that `object' was of
1584 type `struct S' if it were not for code just below. With this
1585 code however, later parts of the compiler see something like:
1586
1587 struct S' == struct S
1588 typedef struct S' MY_TYPE;
1589 struct S' object;
1590
1591 And they can then deduce (from the node for type struct S') that
1592 the original object declaration was:
1593
1594 MY_TYPE object;
1595
1596 Being able to do this is important for proper support of protoize,
1597 and also for generating precise symbolic debugging information
1598 which takes full account of the programmer's (typedef) vocabulary.
1599
1600 Obviously, we don't want to generate a duplicate ..._TYPE node if
1601 the TYPE_DECL node that we are now processing really represents a
1602 standard built-in type.
1603
1604 Since all standard types are effectively declared at line zero
1605 in the source file, we can easily check to see if we are working
1606 on a standard type by checking the current value of lineno. */
1607
1608static void
35b1a6fa 1609clone_underlying_type (tree x)
339a28b9 1610{
f31686a3 1611 if (DECL_SOURCE_LINE (x) == 0)
26f943fd 1612 {
339a28b9
ZW
1613 if (TYPE_NAME (TREE_TYPE (x)) == 0)
1614 TYPE_NAME (TREE_TYPE (x)) = x;
1615 }
1616 else if (TREE_TYPE (x) != error_mark_node
1617 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
1618 {
1619 tree tt = TREE_TYPE (x);
1620 DECL_ORIGINAL_TYPE (x) = tt;
1621 tt = build_type_copy (tt);
1622 TYPE_NAME (tt) = x;
1623 TREE_USED (tt) = TREE_USED (x);
1624 TREE_TYPE (x) = tt;
26f943fd
NB
1625 }
1626}
1627
51e29401
RS
1628/* Record a decl-node X as belonging to the current lexical scope.
1629 Check for errors (such as an incompatible declaration for the same
1630 name already seen in the same scope).
1631
1632 Returns either X or an old decl for the same name.
1633 If an old decl is returned, it may have been smashed
1634 to agree with what X says. */
1635
1636tree
35b1a6fa 1637pushdecl (tree x)
51e29401 1638{
b3694847 1639 tree name = DECL_NAME (x);
f8521984 1640 struct c_scope *scope = current_scope;
339a28b9
ZW
1641
1642#ifdef ENABLE_CHECKING
1643 if (error_mark_node == 0)
1644 /* Called too early. */
1645 abort ();
1646#endif
51e29401 1647
31ed8fea
RH
1648 /* Functions need the lang_decl data. */
1649 if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_LANG_SPECIFIC (x))
703ad42b 1650 DECL_LANG_SPECIFIC (x) = ggc_alloc_cleared (sizeof (struct lang_decl));
31ed8fea 1651
89d7540d
RS
1652 /* A local extern declaration for a function doesn't constitute nesting.
1653 A local auto declaration does, since it's a forward decl
1654 for a nested function coming later. */
d1bd0ded
GK
1655 if (current_function_decl == NULL
1656 || ((TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
1657 && DECL_INITIAL (x) == 0 && DECL_EXTERNAL (x)))
1658 DECL_CONTEXT (x) = current_file_decl;
339a28b9
ZW
1659 else
1660 DECL_CONTEXT (x) = current_function_decl;
e13e48e7 1661
51e29401
RS
1662 if (name)
1663 {
339a28b9 1664 tree old;
51e29401 1665
234f46ae 1666 if (warn_nested_externs
f8521984 1667 && scope != global_scope
234f46ae 1668 && DECL_EXTERNAL (x)
234f46ae
NB
1669 && !DECL_IN_SYSTEM_HEADER (x))
1670 warning ("nested extern declaration of `%s'",
1671 IDENTIFIER_POINTER (name));
1672
339a28b9 1673 old = lookup_name_current_level (name);
1ef82ef2 1674 if (old && duplicate_decls (x, old))
55d54003
ZW
1675 {
1676 /* For PARM_DECLs, old may be a forward declaration.
1677 If so, we want to remove it from its old location
1678 (in the variables chain) and rechain it in the
1679 location given by the new declaration. */
1680 if (TREE_CODE (x) == PARM_DECL)
1681 {
1682 tree *p;
1683 for (p = &scope->names; *p; p = &TREE_CHAIN (*p))
1684 if (*p == old)
1685 {
1686 *p = TREE_CHAIN (old);
f91f41b2
ZW
1687 SCOPE_LIST_APPEND (scope, parms, old);
1688 break;
55d54003
ZW
1689 }
1690 }
1691 return old;
1692 }
f8521984 1693 if (DECL_EXTERNAL (x) || scope == global_scope)
6645c3fa 1694 {
339a28b9 1695 /* Find and check against a previous, not-in-scope, external
40fe4dd5
ZW
1696 decl for this identifier. (C99 6.2.7p2: All declarations
1697 that refer to the same object or function shall have
1698 compatible type; otherwise, the behavior is undefined.) */
1699 tree ext = any_external_decl (name);
339a28b9 1700 if (ext)
6645c3fa 1701 {
1ef82ef2 1702 if (duplicate_decls (x, ext))
77dbdb57
ZW
1703 {
1704 /* XXX This copy_node call violates the basic
1705 assumption that there is only one DECL for any
1706 given object. This causes all sorts of problems
1707 elsewhere. To correct it we must stop chaining
1708 DECLs directly within the scope structure (work
1709 in progress). -zw 2004-03-05 */
1710 x = copy_node (ext);
1711
1712 /* Kludge around one of the worst consequences of
1713 the above copy_node call, viz. that the arg_info
1714 block created by get_parm_info can survive in a
1715 copied FUNCTION_DECL after store_parm_decls is
1716 done with it, and confuse the debug info
1717 generators. */
1718 if (TREE_CODE (ext) == FUNCTION_DECL
1719 && DECL_ARGUMENTS (ext)
1720 && TREE_CODE (DECL_ARGUMENTS (ext)) == TREE_LIST)
1721 DECL_ARGUMENTS (ext) = 0;
1722 }
6645c3fa 1723 }
93f623fa 1724 else
339a28b9 1725 record_external_decl (x);
51e29401 1726 }
35b1a6fa 1727
339a28b9
ZW
1728 if (TREE_CODE (x) == TYPE_DECL)
1729 clone_underlying_type (x);
1730
1731 /* If storing a local value, there may already be one
1732 (inherited). If so, record it for restoration when this
f8521984
ZW
1733 scope ends. Take care not to do this if we are replacing an
1734 older decl in the same scope (i.e. duplicate_decls returned
1735 false, above). */
1736 if (scope != global_scope
339a28b9
ZW
1737 && IDENTIFIER_SYMBOL_VALUE (name)
1738 && IDENTIFIER_SYMBOL_VALUE (name) != old)
51e29401 1739 {
339a28b9
ZW
1740 warn_if_shadowing (x, IDENTIFIER_SYMBOL_VALUE (name));
1741 scope->shadowed = tree_cons (name, IDENTIFIER_SYMBOL_VALUE (name),
1742 scope->shadowed);
51e29401 1743 }
51e29401 1744
f8521984 1745 /* Install the new declaration in the requested scope. */
339a28b9
ZW
1746 IDENTIFIER_SYMBOL_VALUE (name) = x;
1747 C_DECL_INVISIBLE (x) = 0;
51e29401 1748
bf7a697f
ZW
1749 /* If x's type is incomplete because it's based on a
1750 structure or union which has not yet been fully declared,
1751 attach it to that structure or union type, so we can go
1752 back and complete the variable declaration later, if the
1753 structure or union gets fully declared.
1754
5667abce
ZW
1755 If the input is erroneous, we can have error_mark in the type
1756 slot (e.g. "f(void a, ...)") - that doesn't count as an
bf7a697f 1757 incomplete type. */
5667abce
ZW
1758 if (TREE_TYPE (x) != error_mark_node
1759 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
f79349c7
JJ
1760 {
1761 tree element = TREE_TYPE (x);
1762
1763 while (TREE_CODE (element) == ARRAY_TYPE)
1764 element = TREE_TYPE (element);
bf7a697f
ZW
1765 element = TYPE_MAIN_VARIANT (element);
1766
61c234ce
JJ
1767 if ((TREE_CODE (element) == RECORD_TYPE
1768 || TREE_CODE (element) == UNION_TYPE)
1769 && (TREE_CODE (x) != TYPE_DECL
130a1080
ZW
1770 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
1771 && !COMPLETE_TYPE_P (element))
bf7a697f
ZW
1772 C_TYPE_INCOMPLETE_VARS (element)
1773 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
f79349c7 1774 }
51e29401
RS
1775 }
1776
55d54003 1777 if (TREE_CODE (x) == PARM_DECL)
f91f41b2 1778 SCOPE_LIST_APPEND (scope, parms, x);
55d54003 1779 else
f91f41b2 1780 SCOPE_LIST_APPEND (scope, names, x);
51e29401
RS
1781
1782 return x;
1783}
1784
339a28b9 1785/* Record X as belonging to the global scope (C99 "file scope").
6356f892 1786 This is used only internally by the Objective-C front end,
f91f41b2
ZW
1787 and is limited to its needs. duplicate_decls is not called;
1788 if there is any preexisting decl for this identifier, it is an ICE. */
1789
339a28b9 1790tree
35b1a6fa 1791pushdecl_top_level (tree x)
6970c06a 1792{
f91f41b2 1793 tree name;
339a28b9
ZW
1794
1795 if (TREE_CODE (x) != VAR_DECL)
1796 abort ();
1797
1798 name = DECL_NAME (x);
339a28b9 1799
f91f41b2
ZW
1800 if (IDENTIFIER_SYMBOL_VALUE (name))
1801 abort ();
6970c06a 1802
d1bd0ded 1803 DECL_CONTEXT (x) = current_file_decl;
339a28b9 1804 IDENTIFIER_SYMBOL_VALUE (name) = x;
51e29401 1805
f91f41b2
ZW
1806 SCOPE_LIST_APPEND (global_scope, names, x);
1807 return x;
51e29401
RS
1808}
1809\f
339a28b9
ZW
1810/* Generate an implicit declaration for identifier FUNCTIONID as a
1811 function of type int (). */
51e29401
RS
1812
1813tree
35b1a6fa 1814implicitly_declare (tree functionid)
51e29401 1815{
339a28b9 1816 tree decl = any_external_decl (functionid);
51e29401 1817
f91f41b2 1818 if (decl)
339a28b9
ZW
1819 {
1820 /* Implicit declaration of a function already declared
1821 (somehow) in a different scope, or as a built-in.
1822 If this is the first time this has happened, warn;
1823 then recycle the old declaration. */
1824 if (!C_DECL_IMPLICIT (decl))
1825 {
1826 implicit_decl_warning (DECL_NAME (decl));
4b1e44be 1827 if (! DECL_FILE_SCOPE_P (decl))
ddd2d57e 1828 warning ("%Jprevious declaration of '%D'", decl, decl);
339a28b9
ZW
1829 C_DECL_IMPLICIT (decl) = 1;
1830 }
c8b718ba 1831 /* If this function is global, then it must already be in the
f8521984
ZW
1832 global scope, so there's no need to push it again. */
1833 if (current_scope == global_scope)
c8b718ba
MM
1834 return decl;
1835 /* If this is a local declaration, make a copy; we can't have
f8521984 1836 the same DECL listed in two different scopes. */
c8b718ba 1837 return pushdecl (copy_node (decl));
339a28b9 1838 }
51e29401 1839
339a28b9
ZW
1840 /* Not seen before. */
1841 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
1394aabd 1842 DECL_EXTERNAL (decl) = 1;
51e29401 1843 TREE_PUBLIC (decl) = 1;
339a28b9
ZW
1844 C_DECL_IMPLICIT (decl) = 1;
1845 implicit_decl_warning (functionid);
51e29401 1846
14077d68 1847 /* C89 says implicit declarations are in the innermost block.
f458d1d5 1848 So we record the decl in the standard fashion. */
339a28b9 1849 decl = pushdecl (decl);
51e29401 1850
339a28b9 1851 /* No need to call objc_check_decl here - it's a function type. */
6496a589 1852 rest_of_decl_compilation (decl, NULL, 0, 0);
51e29401 1853
14077d68
ZW
1854 /* Write a record describing this implicit function declaration
1855 to the prototypes file (if requested). */
51e29401
RS
1856 gen_aux_info_record (decl, 0, 1, 0);
1857
6431177a
JM
1858 /* Possibly apply some default attributes to this implicit declaration. */
1859 decl_attributes (&decl, NULL_TREE, 0);
1860
51e29401
RS
1861 return decl;
1862}
1863
339a28b9 1864static void
35b1a6fa 1865implicit_decl_warning (tree id)
111458f1 1866{
63ad61ed 1867 const char *name = IDENTIFIER_POINTER (id);
111458f1
ZW
1868 if (mesg_implicit_function_declaration == 2)
1869 error ("implicit declaration of function `%s'", name);
1870 else if (mesg_implicit_function_declaration == 1)
1871 warning ("implicit declaration of function `%s'", name);
1872}
1873
9cd51ef6
ZW
1874/* Issue an error message for a reference to an undeclared variable
1875 ID, including a reference to a builtin outside of function-call
1876 context. Establish a binding of the identifier to error_mark_node
1877 in an appropriate scope, which will suppress further errors for the
1878 same identifier. */
1879void
1880undeclared_variable (tree id)
1881{
1882 static bool already = false;
f91f41b2 1883 struct c_scope *scope;
9cd51ef6
ZW
1884
1885 if (current_function_decl == 0)
1886 {
1887 error ("`%s' undeclared here (not in a function)",
1888 IDENTIFIER_POINTER (id));
f91f41b2 1889 scope = current_scope;
9cd51ef6
ZW
1890 }
1891 else
1892 {
1893 error ("`%s' undeclared (first use in this function)",
1894 IDENTIFIER_POINTER (id));
1895
1896 if (! already)
1897 {
1898 error ("(Each undeclared identifier is reported only once");
1899 error ("for each function it appears in.)");
1900 already = true;
1901 }
1902
f91f41b2 1903 scope = current_function_scope;
9cd51ef6 1904 }
f91f41b2
ZW
1905
1906 scope->shadowed = tree_cons (id, IDENTIFIER_SYMBOL_VALUE (id),
1907 scope->shadowed);
1908 IDENTIFIER_SYMBOL_VALUE (id) = error_mark_node;
9cd51ef6 1909}
51e29401 1910\f
14e33ee8
ZW
1911/* Subroutine of lookup_label, declare_label, define_label: construct a
1912 LABEL_DECL with all the proper frills. */
1913
1914static tree
1915make_label (tree name, location_t location)
1916{
1917 tree label = build_decl (LABEL_DECL, name, void_type_node);
1918
1919 DECL_CONTEXT (label) = current_function_decl;
1920 DECL_MODE (label) = VOIDmode;
f31686a3 1921 DECL_SOURCE_LOCATION (label) = location;
14e33ee8
ZW
1922
1923 return label;
1924}
1925
1926/* Another subroutine of lookup_label, declare_label, define_label:
1927 set up the binding of name to LABEL_DECL in the given SCOPE. */
1928
1929static void
f8521984 1930bind_label (tree name, tree label, struct c_scope *scope)
14e33ee8
ZW
1931{
1932 if (IDENTIFIER_LABEL_VALUE (name))
1933 scope->shadowed = tree_cons (name, IDENTIFIER_LABEL_VALUE (name),
1934 scope->shadowed);
1935 IDENTIFIER_LABEL_VALUE (name) = label;
1936
f91f41b2 1937 SCOPE_LIST_APPEND (scope, names, label);
14e33ee8
ZW
1938}
1939
1940/* Get the LABEL_DECL corresponding to identifier NAME as a label.
51e29401 1941 Create one if none exists so far for the current function.
14e33ee8
ZW
1942 This is called when a label is used in a goto expression or
1943 has its address taken. */
51e29401
RS
1944
1945tree
14e33ee8 1946lookup_label (tree name)
51e29401 1947{
14e33ee8 1948 tree label;
51e29401 1949
7d9795e5
RS
1950 if (current_function_decl == 0)
1951 {
1952 error ("label %s referenced outside of any function",
14e33ee8 1953 IDENTIFIER_POINTER (name));
7d9795e5
RS
1954 return 0;
1955 }
1956
14e33ee8
ZW
1957 /* Use a label already defined or ref'd with this name, but not if
1958 it is inherited from a containing function and wasn't declared
1959 using __label__. */
1960 label = IDENTIFIER_LABEL_VALUE (name);
1961 if (label && (DECL_CONTEXT (label) == current_function_decl
1962 || C_DECLARED_LABEL_FLAG (label)))
51e29401 1963 {
14e33ee8
ZW
1964 /* If the label has only been declared, update its apparent
1965 location to point here, for better diagnostics if it
1966 turns out not to have been defined. */
1967 if (!TREE_USED (label))
f31686a3 1968 DECL_SOURCE_LOCATION (label) = input_location;
14e33ee8 1969 return label;
51e29401
RS
1970 }
1971
14e33ee8
ZW
1972 /* No label binding for that identifier; make one. */
1973 label = make_label (name, input_location);
51e29401 1974
14077d68 1975 /* Ordinary labels go in the current function scope. */
f8521984 1976 bind_label (name, label, current_function_scope);
14e33ee8 1977 return label;
51e29401
RS
1978}
1979
14e33ee8
ZW
1980/* Make a label named NAME in the current function, shadowing silently
1981 any that may be inherited from containing functions or containing
1982 scopes. This is called for __label__ declarations. */
51e29401 1983
14e33ee8
ZW
1984/* Note that valid use, if the label being shadowed comes from another
1985 scope in the same function, requires calling declare_nonlocal_label
1986 right away. (Is this still true? -zw 2003-07-17) */
51e29401
RS
1987
1988tree
14e33ee8 1989declare_label (tree name)
51e29401 1990{
14e33ee8
ZW
1991 tree label = IDENTIFIER_LABEL_VALUE (name);
1992 tree dup;
51e29401 1993
14e33ee8
ZW
1994 /* Check to make sure that the label hasn't already been declared
1995 at this scope */
f8521984 1996 for (dup = current_scope->names; dup; dup = TREE_CHAIN (dup))
14e33ee8
ZW
1997 if (dup == label)
1998 {
1999 error ("duplicate label declaration `%s'", IDENTIFIER_POINTER (name));
ddd2d57e 2000 error ("%Jthis is a previous declaration", dup);
a784882b 2001
14e33ee8
ZW
2002 /* Just use the previous declaration. */
2003 return dup;
2004 }
a784882b 2005
14e33ee8
ZW
2006 label = make_label (name, input_location);
2007 C_DECLARED_LABEL_FLAG (label) = 1;
51e29401 2008
14e33ee8 2009 /* Declared labels go in the current scope. */
f8521984 2010 bind_label (name, label, current_scope);
14e33ee8 2011 return label;
51e29401
RS
2012}
2013
2014/* Define a label, specifying the location in the source file.
2015 Return the LABEL_DECL node for the label, if the definition is valid.
2016 Otherwise return 0. */
2017
2018tree
5b030314 2019define_label (location_t location, tree name)
51e29401 2020{
14e33ee8
ZW
2021 tree label;
2022
2023 /* Find any preexisting label with this name. It is an error
2024 if that label has already been defined in this function, or
2025 if there is a containing function with a declared label with
2026 the same name. */
2027 label = IDENTIFIER_LABEL_VALUE (name);
2028
2029 if (label
2030 && ((DECL_CONTEXT (label) == current_function_decl
2031 && DECL_INITIAL (label) != 0)
2032 || (DECL_CONTEXT (label) != current_function_decl
2033 && C_DECLARED_LABEL_FLAG (label))))
51e29401 2034 {
14e33ee8
ZW
2035 error ("%Hduplicate label `%D'", &location, label);
2036 if (DECL_INITIAL (label))
ddd2d57e 2037 error ("%J`%D' previously defined here", label, label);
14e33ee8 2038 else
ddd2d57e 2039 error ("%J`%D' previously declared here", label, label);
14e33ee8 2040 return 0;
51e29401 2041 }
14e33ee8 2042 else if (label && DECL_CONTEXT (label) == current_function_decl)
51e29401 2043 {
14e33ee8
ZW
2044 /* The label has been used or declared already in this function,
2045 but not defined. Update its location to point to this
2046 definition. */
f31686a3 2047 DECL_SOURCE_LOCATION (label) = location;
51e29401
RS
2048 }
2049 else
2050 {
14e33ee8
ZW
2051 /* No label binding for that identifier; make one. */
2052 label = make_label (name, location);
2053
14077d68 2054 /* Ordinary labels go in the current function scope. */
f8521984 2055 bind_label (name, label, current_function_scope);
51e29401 2056 }
14e33ee8
ZW
2057
2058 if (warn_traditional && !in_system_header && lookup_name (name))
2059 warning ("%Htraditional C lacks a separate namespace for labels, "
2060 "identifier `%s' conflicts", &location,
2061 IDENTIFIER_POINTER (name));
2062
2063 /* Mark label as having been defined. */
2064 DECL_INITIAL (label) = error_mark_node;
2065 return label;
51e29401
RS
2066}
2067\f
21d13d83
ZW
2068/* Return the list of declarations of the current scope.
2069 This hook is optional and not implemented for C. */
51e29401
RS
2070
2071tree
35b1a6fa 2072getdecls (void)
51e29401 2073{
21d13d83 2074 return 0;
51e29401
RS
2075}
2076
51e29401
RS
2077\f
2078/* Given NAME, an IDENTIFIER_NODE,
2079 return the structure (or union or enum) definition for that name.
f8521984 2080 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
51e29401
RS
2081 CODE says which kind of type the caller wants;
2082 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2083 If the wrong kind of type is found, an error is reported. */
2084
2085static tree
35b1a6fa 2086lookup_tag (enum tree_code code, tree name, int thislevel_only)
51e29401 2087{
339a28b9
ZW
2088 tree tag = IDENTIFIER_TAG_VALUE (name);
2089 int thislevel = 0;
51e29401 2090
339a28b9
ZW
2091 if (!tag)
2092 return 0;
2093
2094 /* We only care about whether it's in this level if
2095 thislevel_only was set or it might be a type clash. */
2096 if (thislevel_only || TREE_CODE (tag) != code)
51e29401 2097 {
f8521984
ZW
2098 if (current_scope == global_scope
2099 || purpose_member (name, current_scope->tags))
339a28b9 2100 thislevel = 1;
51e29401 2101 }
339a28b9
ZW
2102
2103 if (thislevel_only && !thislevel)
2104 return 0;
2105
2106 if (TREE_CODE (tag) != code)
2107 {
2108 /* Definition isn't the kind we were looking for. */
2109 pending_invalid_xref = name;
070588f0 2110 pending_invalid_xref_location = input_location;
339a28b9
ZW
2111
2112 /* If in the same binding level as a declaration as a tag
2113 of a different type, this must not be allowed to
2114 shadow that tag, so give the error immediately.
2115 (For example, "struct foo; union foo;" is invalid.) */
2116 if (thislevel)
2117 pending_xref_error ();
2118 }
2119 return tag;
51e29401
RS
2120}
2121
2122/* Print an error message now
2123 for a recent invalid struct, union or enum cross reference.
2124 We don't print them immediately because they are not invalid
2125 when used in the `struct foo;' construct for shadowing. */
2126
2127void
35b1a6fa 2128pending_xref_error (void)
51e29401
RS
2129{
2130 if (pending_invalid_xref != 0)
95035b6f
GDR
2131 error ("%H`%s' defined as wrong kind of tag",
2132 &pending_invalid_xref_location,
2133 IDENTIFIER_POINTER (pending_invalid_xref));
51e29401
RS
2134 pending_invalid_xref = 0;
2135}
2136
51e29401 2137\f
f8521984 2138/* Look up NAME in the current scope and its superiors
51e29401
RS
2139 in the namespace of variables, functions and typedefs.
2140 Return a ..._DECL node of some kind representing its definition,
2141 or return 0 if it is undefined. */
2142
2143tree
35b1a6fa 2144lookup_name (tree name)
51e29401 2145{
339a28b9
ZW
2146 tree decl = IDENTIFIER_SYMBOL_VALUE (name);
2147 if (decl == 0 || decl == error_mark_node)
2148 return decl;
2149 if (C_DECL_INVISIBLE (decl))
2150 return 0;
2151 return decl;
51e29401
RS
2152}
2153
f8521984 2154/* Similar to `lookup_name' but look only at the current scope. */
51e29401 2155
339a28b9 2156static tree
35b1a6fa 2157lookup_name_current_level (tree name)
51e29401 2158{
339a28b9 2159 tree decl = IDENTIFIER_SYMBOL_VALUE (name);
51e29401 2160
339a28b9 2161 if (decl == 0 || decl == error_mark_node || C_DECL_INVISIBLE (decl))
51e29401
RS
2162 return 0;
2163
f8521984 2164 if (current_scope == global_scope)
339a28b9 2165 return decl;
51e29401 2166
55d54003
ZW
2167 /* Scan the current scope for a decl with name NAME.
2168 For PARM_DECLs, we have to look at both ->parms and ->names, since
2169 forward parameter declarations wind up on the ->names list. */
2170 if (TREE_CODE (decl) == PARM_DECL
2171 && chain_member (decl, current_scope->parms))
2172 return decl;
f8521984 2173 if (chain_member (decl, current_scope->names))
339a28b9
ZW
2174 return decl;
2175
2176 return 0;
51e29401
RS
2177}
2178\f
2179/* Create the predefined scalar types of C,
0f41302f 2180 and some nodes representing standard constants (0, 1, (void *) 0).
f8521984 2181 Initialize the global scope.
51e29401
RS
2182 Make definitions for built-in primitive functions. */
2183
2184void
35b1a6fa 2185c_init_decl_processing (void)
51e29401 2186{
b3694847 2187 tree endlink;
7f4edbcb 2188 tree ptr_ftype_void, ptr_ftype_ptr;
4714db5a 2189 location_t save_loc = input_location;
e13e48e7 2190
f5e99456
NB
2191 /* Adds some ggc roots, and reserved words for c-parse.in. */
2192 c_parse_init ();
2193
14077d68 2194 current_function_decl = 0;
6645c3fa 2195
f8521984 2196 /* Make the c_scope structure for global names. */
6645c3fa 2197 pushlevel (0);
f8521984 2198 global_scope = current_scope;
14077d68 2199
e3091a5f
R
2200 /* Declarations from c_common_nodes_and_builtins must not be associated
2201 with this input file, lest we get differences between using and not
2202 using preprocessed headers. */
4714db5a
NS
2203 input_location.file = "<internal>";
2204 input_location.line = 0;
51e29401 2205
d1bd0ded
GK
2206 /* Make the DECL for the toplevel file scope. */
2207 current_file_decl = build_decl (TRANSLATION_UNIT_DECL, NULL, NULL);
2208
81b3411c 2209 build_common_tree_nodes (flag_signed_char);
51e29401 2210
eaa7c03f 2211 c_common_nodes_and_builtins ();
51e29401 2212
de7df9eb
JM
2213 /* In C, comparisons and TRUTH_* expressions have type int. */
2214 truthvalue_type_node = integer_type_node;
2215 truthvalue_true_node = integer_one_node;
2216 truthvalue_false_node = integer_zero_node;
2217
2218 /* Even in C99, which has a real boolean type. */
19552aa5 2219 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
de7df9eb 2220 boolean_type_node));
19552aa5 2221
7f4edbcb 2222 endlink = void_list_node;
0021b564
JM
2223 ptr_ftype_void = build_function_type (ptr_type_node, endlink);
2224 ptr_ftype_ptr
2225 = build_function_type (ptr_type_node,
2226 tree_cons (NULL_TREE, ptr_type_node, endlink));
2227
4714db5a 2228 input_location = save_loc;
e3091a5f 2229
53cd18ec 2230 pedantic_lvalues = true;
f444e553 2231
2ce07e2d 2232 make_fname_decl = c_make_fname_decl;
0ba8a114 2233 start_fname_decls ();
d1bd0ded 2234
fdc49e10
ZW
2235 first_builtin_decl = global_scope->names;
2236 last_builtin_decl = global_scope->names_last;
51e29401
RS
2237}
2238
2ce07e2d
NS
2239/* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2240 decl, NAME is the initialization string and TYPE_DEP indicates whether
2241 NAME depended on the type of the function. As we don't yet implement
2242 delayed emission of static data, we mark the decl as emitted
2243 so it is not placed in the output. Anything using it must therefore pull
f91f41b2 2244 out the STRING_CST initializer directly. FIXME. */
2ce07e2d
NS
2245
2246static tree
35b1a6fa 2247c_make_fname_decl (tree id, int type_dep)
2ce07e2d 2248{
0ba8a114 2249 const char *name = fname_as_string (type_dep);
2ce07e2d
NS
2250 tree decl, type, init;
2251 size_t length = strlen (name);
2252
2253 type = build_array_type
2254 (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
0ba8a114 2255 build_index_type (size_int (length)));
2ce07e2d
NS
2256
2257 decl = build_decl (VAR_DECL, id, type);
35b1a6fa 2258
2ce07e2d
NS
2259 TREE_STATIC (decl) = 1;
2260 TREE_READONLY (decl) = 1;
2ce07e2d 2261 DECL_ARTIFICIAL (decl) = 1;
35b1a6fa 2262
2ce07e2d
NS
2263 init = build_string (length + 1, name);
2264 TREE_TYPE (init) = type;
2265 DECL_INITIAL (decl) = init;
0ba8a114
NS
2266
2267 TREE_USED (decl) = 1;
6cce57b0
JM
2268
2269 if (current_function_decl)
f91f41b2
ZW
2270 {
2271 DECL_CONTEXT (decl) = current_function_decl;
2272 IDENTIFIER_SYMBOL_VALUE (id) = decl;
2273 SCOPE_LIST_APPEND (current_function_scope, names, decl);
2274 }
339a28b9 2275
0ba8a114 2276 finish_decl (decl, init, NULL_TREE);
6645c3fa 2277
2ce07e2d
NS
2278 return decl;
2279}
2280
51e29401
RS
2281/* Return a definition for a builtin function named NAME and whose data type
2282 is TYPE. TYPE should be a function type with argument types.
2283 FUNCTION_CODE tells later passes how to compile calls to this function.
2284 See tree.h for its possible values.
2285
2286 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
6a2dd09a
RS
2287 the name to be called if we can't opencode the function. If
2288 ATTRS is nonzero, use that for the function's attribute list. */
51e29401 2289
929f3671 2290tree
35b1a6fa
AJ
2291builtin_function (const char *name, tree type, int function_code,
2292 enum built_in_class class, const char *library_name,
2293 tree attrs)
51e29401
RS
2294{
2295 tree decl = build_decl (FUNCTION_DECL, get_identifier (name), type);
1394aabd 2296 DECL_EXTERNAL (decl) = 1;
51e29401
RS
2297 TREE_PUBLIC (decl) = 1;
2298 if (library_name)
92643fea 2299 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
6496a589 2300 make_decl_rtl (decl, NULL);
51e29401 2301 pushdecl (decl);
26db82d8
BS
2302 DECL_BUILT_IN_CLASS (decl) = class;
2303 DECL_FUNCTION_CODE (decl) = function_code;
2304
6b19af32
RS
2305 /* Warn if a function in the namespace for users
2306 is used without an occasion to consider it declared. */
2307 if (name[0] != '_' || name[1] != '_')
339a28b9 2308 C_DECL_INVISIBLE (decl) = 1;
51e29401 2309
6431177a 2310 /* Possibly apply some default attributes to this built-in function. */
6a2dd09a
RS
2311 if (attrs)
2312 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2313 else
2314 decl_attributes (&decl, NULL_TREE, 0);
6431177a 2315
51e29401
RS
2316 return decl;
2317}
2318\f
2319/* Called when a declaration is seen that contains no names to declare.
2320 If its type is a reference to a structure, union or enum inherited
2321 from a containing scope, shadow that tag name for the current scope
2322 with a forward reference.
2323 If its type defines a new named structure or union
2324 or defines an enum, it is valid but we need not do anything here.
2325 Otherwise, it is an error. */
2326
2327void
35b1a6fa 2328shadow_tag (tree declspecs)
9282f2f9
RS
2329{
2330 shadow_tag_warned (declspecs, 0);
2331}
2332
2333void
35b1a6fa
AJ
2334shadow_tag_warned (tree declspecs, int warned)
2335
2336
773edaef
RK
2337 /* 1 => we have done a pedwarn. 2 => we have done a warning, but
2338 no pedwarn. */
51e29401
RS
2339{
2340 int found_tag = 0;
b3694847 2341 tree link;
d9525bec 2342 tree specs, attrs;
51e29401
RS
2343
2344 pending_invalid_xref = 0;
2345
d9525bec
BK
2346 /* Remove the attributes from declspecs, since they will confuse the
2347 following code. */
2348 split_specs_attrs (declspecs, &specs, &attrs);
2349
3bd89472 2350 for (link = specs; link; link = TREE_CHAIN (link))
51e29401 2351 {
b3694847
SS
2352 tree value = TREE_VALUE (link);
2353 enum tree_code code = TREE_CODE (value);
51e29401
RS
2354
2355 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2356 /* Used to test also that TYPE_SIZE (value) != 0.
2357 That caused warning for `struct foo;' at top level in the file. */
2358 {
339a28b9 2359 tree name = TYPE_NAME (value);
b3694847 2360 tree t;
51e29401
RS
2361
2362 found_tag++;
2363
2364 if (name == 0)
2365 {
773edaef
RK
2366 if (warned != 1 && code != ENUMERAL_TYPE)
2367 /* Empty unnamed enum OK */
51e29401
RS
2368 {
2369 pedwarn ("unnamed struct/union that defines no instances");
2370 warned = 1;
2371 }
2372 }
2373 else
2374 {
339a28b9 2375 t = lookup_tag (code, name, 1);
51e29401
RS
2376
2377 if (t == 0)
2378 {
2379 t = make_node (code);
2380 pushtag (name, t);
2381 }
2382 }
2383 }
2384 else
2385 {
efffbf71 2386 if (!warned && ! in_system_header)
773edaef
RK
2387 {
2388 warning ("useless keyword or type name in empty declaration");
2389 warned = 2;
2390 }
51e29401
RS
2391 }
2392 }
2393
773edaef
RK
2394 if (found_tag > 1)
2395 error ("two types specified in one empty declaration");
2396
2397 if (warned != 1)
51e29401 2398 {
51e29401
RS
2399 if (found_tag == 0)
2400 pedwarn ("empty declaration");
2401 }
2402}
2403\f
0e03329a
JM
2404/* Construct an array declarator. EXPR is the expression inside [], or
2405 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
2406 to the pointer to which a parameter array is converted). STATIC_P is
da7d8304
KH
2407 nonzero if "static" is inside the [], zero otherwise. VLA_UNSPEC_P
2408 is nonzero is the array is [*], a VLA of unspecified length which is
0e03329a
JM
2409 nevertheless a complete type (not currently implemented by GCC),
2410 zero otherwise. The declarator is constructed as an ARRAY_REF
2411 (to be decoded by grokdeclarator), whose operand 0 is what's on the
2412 left of the [] (filled by in set_array_declarator_type) and operand 1
2413 is the expression inside; whose TREE_TYPE is the type qualifiers and
2414 which has TREE_STATIC set if "static" is used. */
2415
2416tree
35b1a6fa 2417build_array_declarator (tree expr, tree quals, int static_p, int vla_unspec_p)
0e03329a
JM
2418{
2419 tree decl;
2420 decl = build_nt (ARRAY_REF, NULL_TREE, expr);
2421 TREE_TYPE (decl) = quals;
2422 TREE_STATIC (decl) = (static_p ? 1 : 0);
2423 if (pedantic && !flag_isoc99)
2424 {
2425 if (static_p || quals != NULL_TREE)
56508306 2426 pedwarn ("ISO C90 does not support `static' or type qualifiers in parameter array declarators");
0e03329a 2427 if (vla_unspec_p)
56508306 2428 pedwarn ("ISO C90 does not support `[*]' array declarators");
0e03329a
JM
2429 }
2430 if (vla_unspec_p)
2431 warning ("GCC does not yet properly implement `[*]' array declarators");
2432 return decl;
2433}
2434
2435/* Set the type of an array declarator. DECL is the declarator, as
2436 constructed by build_array_declarator; TYPE is what appears on the left
da7d8304 2437 of the [] and goes in operand 0. ABSTRACT_P is nonzero if it is an
0e03329a
JM
2438 abstract declarator, zero otherwise; this is used to reject static and
2439 type qualifiers in abstract declarators, where they are not in the
2440 C99 grammar. */
2441
2442tree
35b1a6fa 2443set_array_declarator_type (tree decl, tree type, int abstract_p)
0e03329a
JM
2444{
2445 TREE_OPERAND (decl, 0) = type;
2446 if (abstract_p && (TREE_TYPE (decl) != NULL_TREE || TREE_STATIC (decl)))
2447 error ("static or type qualifiers in abstract declarator");
2448 return decl;
2449}
2450\f
51e29401
RS
2451/* Decode a "typename", such as "int **", returning a ..._TYPE node. */
2452
2453tree
35b1a6fa 2454groktypename (tree typename)
51e29401 2455{
d3b4cd6f
AH
2456 tree specs, attrs;
2457
51e29401
RS
2458 if (TREE_CODE (typename) != TREE_LIST)
2459 return typename;
d3b4cd6f
AH
2460
2461 split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
2462
2ff7cce4
JM
2463 typename = grokdeclarator (TREE_VALUE (typename), specs, TYPENAME, 0,
2464 NULL);
d3b4cd6f
AH
2465
2466 /* Apply attributes. */
2467 decl_attributes (&typename, attrs, 0);
2468
2469 return typename;
51e29401
RS
2470}
2471
2472/* Return a PARM_DECL node for a given pair of specs and declarator. */
2473
2474tree
35b1a6fa 2475groktypename_in_parm_context (tree typename)
51e29401
RS
2476{
2477 if (TREE_CODE (typename) != TREE_LIST)
2478 return typename;
2479 return grokdeclarator (TREE_VALUE (typename),
2480 TREE_PURPOSE (typename),
2ff7cce4 2481 PARM, 0, NULL);
51e29401
RS
2482}
2483
2484/* Decode a declarator in an ordinary declaration or data definition.
2485 This is called as soon as the type information and variable name
2486 have been parsed, before parsing the initializer if any.
2487 Here we create the ..._DECL node, fill in its type,
2488 and put it on the list of decls for the current context.
2489 The ..._DECL node is returned as the value.
2490
2491 Exception: for arrays where the length is not specified,
2492 the type is left null, to be filled in by `finish_decl'.
2493
2494 Function definitions do not come here; they go to start_function
2495 instead. However, external and forward declarations of functions
2496 do go through here. Structure field declarations are done by
2497 grokfield and not through here. */
2498
51e29401 2499tree
35b1a6fa 2500start_decl (tree declarator, tree declspecs, int initialized, tree attributes)
51e29401 2501{
e23bd218 2502 tree decl;
b3694847 2503 tree tem;
35b1a6fa 2504
e23bd218
IR
2505 /* An object declared as __attribute__((deprecated)) suppresses
2506 warnings of uses of other deprecated items. */
2507 if (lookup_attribute ("deprecated", attributes))
2508 deprecated_state = DEPRECATED_SUPPRESS;
2509
2510 decl = grokdeclarator (declarator, declspecs,
2ff7cce4 2511 NORMAL, initialized, NULL);
35b1a6fa 2512
e23bd218 2513 deprecated_state = DEPRECATED_NORMAL;
51e29401 2514
6645c3fa 2515 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
5b47282c 2516 && MAIN_NAME_P (DECL_NAME (decl)))
ddd2d57e 2517 warning ("%J'%D' is usually a function", decl, decl);
b8705e61 2518
51e29401
RS
2519 if (initialized)
2520 /* Is it valid for this decl to have an initializer at all?
2521 If not, set INITIALIZED to zero, which will indirectly
2522 tell `finish_decl' to ignore the initializer once it is parsed. */
2523 switch (TREE_CODE (decl))
2524 {
2525 case TYPE_DECL:
4a7510cb 2526 error ("typedef `%s' is initialized (use __typeof__ instead)",
95f79357
ZW
2527 IDENTIFIER_POINTER (DECL_NAME (decl)));
2528 initialized = 0;
51e29401
RS
2529 break;
2530
2531 case FUNCTION_DECL:
2532 error ("function `%s' is initialized like a variable",
2533 IDENTIFIER_POINTER (DECL_NAME (decl)));
2534 initialized = 0;
2535 break;
2536
2537 case PARM_DECL:
2538 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
2539 error ("parameter `%s' is initialized",
2540 IDENTIFIER_POINTER (DECL_NAME (decl)));
2541 initialized = 0;
2542 break;
2543
2544 default:
2545 /* Don't allow initializations for incomplete types
2546 except for arrays which might be completed by the initialization. */
f4fce7ed
JW
2547
2548 /* This can happen if the array size is an undefined macro. We already
2549 gave a warning, so we don't need another one. */
2550 if (TREE_TYPE (decl) == error_mark_node)
2551 initialized = 0;
2552 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
51e29401
RS
2553 {
2554 /* A complete type is ok if size is fixed. */
2555
2556 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
2557 || C_DECL_VARIABLE_SIZE (decl))
2558 {
2559 error ("variable-sized object may not be initialized");
2560 initialized = 0;
2561 }
2562 }
2563 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
2564 {
2565 error ("variable `%s' has initializer but incomplete type",
2566 IDENTIFIER_POINTER (DECL_NAME (decl)));
2567 initialized = 0;
2568 }
d0f062fb 2569 else if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
51e29401
RS
2570 {
2571 error ("elements of array `%s' have incomplete type",
2572 IDENTIFIER_POINTER (DECL_NAME (decl)));
2573 initialized = 0;
2574 }
2575 }
2576
2577 if (initialized)
2578 {
1394aabd 2579 DECL_EXTERNAL (decl) = 0;
f8521984 2580 if (current_scope == global_scope)
51e29401
RS
2581 TREE_STATIC (decl) = 1;
2582
2583 /* Tell `pushdecl' this is an initialized decl
2584 even though we don't yet have the initializer expression.
2585 Also tell `finish_decl' it may store the real initializer. */
2586 DECL_INITIAL (decl) = error_mark_node;
2587 }
2588
2589 /* If this is a function declaration, write a record describing it to the
2590 prototypes file (if requested). */
2591
2592 if (TREE_CODE (decl) == FUNCTION_DECL)
2593 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
2594
2786cbad
JM
2595 /* ANSI specifies that a tentative definition which is not merged with
2596 a non-tentative definition behaves exactly like a definition with an
2597 initializer equal to zero. (Section 3.7.2)
3d78f2e9
RH
2598
2599 -fno-common gives strict ANSI behavior, though this tends to break
2600 a large body of code that grew up without this rule.
2601
2602 Thread-local variables are never common, since there's no entrenched
2603 body of code to break, and it allows more efficient variable references
95bd1dd7 2604 in the presence of dynamic linking. */
3d78f2e9
RH
2605
2606 if (TREE_CODE (decl) == VAR_DECL
2607 && !initialized
2608 && TREE_PUBLIC (decl)
2609 && !DECL_THREAD_LOCAL (decl)
2610 && !flag_no_common)
2786cbad 2611 DECL_COMMON (decl) = 1;
8615176a 2612
daa6d5ff 2613 /* Set attributes here so if duplicate decl, will have proper attributes. */
59387d2e 2614 decl_attributes (&decl, attributes, 0);
daa6d5ff 2615
61f71b34
DD
2616 if (TREE_CODE (decl) == FUNCTION_DECL
2617 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
2618 {
2619 tree ce = declarator;
2620
2621 if (TREE_CODE (ce) == INDIRECT_REF)
2622 ce = TREE_OPERAND (declarator, 0);
2623 if (TREE_CODE (ce) == CALL_EXPR)
2624 {
2625 tree args = TREE_PURPOSE (TREE_OPERAND (ce, 1));
2626 for (; args; args = TREE_CHAIN (args))
2627 {
2628 tree type = TREE_TYPE (args);
2629 if (INTEGRAL_TYPE_P (type)
2630 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
2631 DECL_ARG_TYPE (args) = integer_type_node;
2632 }
2633 }
2634 }
2635
9162542e
AO
2636 if (TREE_CODE (decl) == FUNCTION_DECL
2637 && DECL_DECLARED_INLINE_P (decl)
2638 && DECL_UNINLINABLE (decl)
2639 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
ddd2d57e 2640 warning ("%Jinline function '%D' given attribute noinline", decl, decl);
9162542e 2641
f8521984 2642 /* Add this decl to the current scope.
51e29401
RS
2643 TEM may equal DECL or it may be a previous decl of the same name. */
2644 tem = pushdecl (decl);
2645
2646 /* For a local variable, define the RTL now. */
f8521984 2647 if (current_scope != global_scope
51e29401
RS
2648 /* But not if this is a duplicate decl
2649 and we preserved the rtl from the previous one
2650 (which may or may not happen). */
19e7881c 2651 && !DECL_RTL_SET_P (tem)
4b1e44be 2652 && DECL_FILE_SCOPE_P (tem))
51e29401 2653 {
f4fce7ed 2654 if (TREE_TYPE (tem) != error_mark_node
14077d68
ZW
2655 && (COMPLETE_TYPE_P (TREE_TYPE (tem))
2656 || (TREE_CODE (TREE_TYPE (tem)) == ARRAY_TYPE
2657 && DECL_INITIAL (tem) != 0)))
51e29401
RS
2658 expand_decl (tem);
2659 }
2660
51e29401
RS
2661 return tem;
2662}
2663
2664/* Finish processing of a declaration;
2665 install its initial value.
2666 If the length of an array type is not known before,
2667 it must be determined now, from the initial value, or it is an error. */
2668
2669void
35b1a6fa 2670finish_decl (tree decl, tree init, tree asmspec_tree)
51e29401 2671{
b3694847 2672 tree type = TREE_TYPE (decl);
51e29401 2673 int was_incomplete = (DECL_SIZE (decl) == 0);
520a57c8 2674 const char *asmspec = 0;
51e29401 2675
6d2f8887 2676 /* If a name was specified, get the string. */
f8521984 2677 if (current_scope == global_scope)
41c64394 2678 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
51e29401 2679 if (asmspec_tree)
72f5a12b 2680 asmspec = TREE_STRING_POINTER (asmspec_tree);
51e29401
RS
2681
2682 /* If `start_decl' didn't like having an initialization, ignore it now. */
51e29401
RS
2683 if (init != 0 && DECL_INITIAL (decl) == 0)
2684 init = 0;
35b1a6fa 2685
51e29401
RS
2686 /* Don't crash if parm is initialized. */
2687 if (TREE_CODE (decl) == PARM_DECL)
2688 init = 0;
2689
2690 if (init)
95f79357 2691 store_init_value (decl, init);
51e29401 2692
264fa2db
ZL
2693 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
2694 || TREE_CODE (decl) == FUNCTION_DECL
2695 || TREE_CODE (decl) == FIELD_DECL))
2696 objc_check_decl (decl);
2697
6614fd40 2698 /* Deduce size of array from initialization, if not already known. */
51e29401
RS
2699 if (TREE_CODE (type) == ARRAY_TYPE
2700 && TYPE_DOMAIN (type) == 0
2701 && TREE_CODE (decl) != TYPE_DECL)
2702 {
2703 int do_default
2704 = (TREE_STATIC (decl)
2705 /* Even if pedantic, an external linkage array
2706 may have incomplete type at first. */
2707 ? pedantic && !TREE_PUBLIC (decl)
1394aabd 2708 : !DECL_EXTERNAL (decl));
51e29401
RS
2709 int failure
2710 = complete_array_type (type, DECL_INITIAL (decl), do_default);
2711
2712 /* Get the completed type made by complete_array_type. */
2713 type = TREE_TYPE (decl);
2714
2715 if (failure == 1)
ddd2d57e 2716 error ("%Jinitializer fails to determine size of '%D'", decl, decl);
51e29401 2717
5fa7c8ce 2718 else if (failure == 2)
51e29401
RS
2719 {
2720 if (do_default)
ddd2d57e 2721 error ("%Jarray size missing in '%D'", decl, decl);
b4892310
RS
2722 /* If a `static' var's size isn't known,
2723 make it extern as well as static, so it does not get
2724 allocated.
2725 If it is not `static', then do not mark extern;
2726 finish_incomplete_decl will give it a default size
2727 and it will get allocated. */
2728 else if (!pedantic && TREE_STATIC (decl) && ! TREE_PUBLIC (decl))
1394aabd 2729 DECL_EXTERNAL (decl) = 1;
51e29401
RS
2730 }
2731
7e44eda6
JW
2732 /* TYPE_MAX_VALUE is always one less than the number of elements
2733 in the array, because we start counting at zero. Therefore,
2734 warn only if the value is less than zero. */
5fa7c8ce
RK
2735 else if (pedantic && TYPE_DOMAIN (type) != 0
2736 && tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < 0)
ddd2d57e 2737 error ("%Jzero or negative size array '%D'", decl, decl);
51e29401
RS
2738
2739 layout_decl (decl, 0);
2740 }
2741
2742 if (TREE_CODE (decl) == VAR_DECL)
2743 {
f4fce7ed
JW
2744 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
2745 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
d575f110
RS
2746 layout_decl (decl, 0);
2747
a7f64d52 2748 if (DECL_SIZE (decl) == 0
f4fce7ed
JW
2749 /* Don't give an error if we already gave one earlier. */
2750 && TREE_TYPE (decl) != error_mark_node
a7f64d52
RS
2751 && (TREE_STATIC (decl)
2752 ?
2753 /* A static variable with an incomplete type
f3b4fb6e 2754 is an error if it is initialized.
70efc776 2755 Also if it is not file scope.
a7f64d52
RS
2756 Otherwise, let it through, but if it is not `extern'
2757 then it may cause an error message later. */
e3b776dc 2758 (DECL_INITIAL (decl) != 0
4b1e44be 2759 || !DECL_FILE_SCOPE_P (decl))
a7f64d52
RS
2760 :
2761 /* An automatic variable with an incomplete type
2762 is an error. */
70038ec9 2763 !DECL_EXTERNAL (decl)))
51e29401 2764 {
ddd2d57e 2765 error ("%Jstorage size of '%D' isn't known", decl, decl);
51e29401
RS
2766 TREE_TYPE (decl) = error_mark_node;
2767 }
2768
1394aabd 2769 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
90374cc2 2770 && DECL_SIZE (decl) != 0)
e681c5a1
RS
2771 {
2772 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
2773 constant_expression_warning (DECL_SIZE (decl));
2774 else
ddd2d57e 2775 error ("%Jstorage size of '%D' isn't constant", decl, decl);
e681c5a1 2776 }
e9a25f70 2777
6645c3fa 2778 if (TREE_USED (type))
e9a25f70 2779 TREE_USED (decl) = 1;
51e29401
RS
2780 }
2781
9661b15f
JJ
2782 /* If this is a function and an assembler name is specified, reset DECL_RTL
2783 so we can give it its new name. Also, update built_in_decls if it
2784 was a normal built-in. */
3c4afaa5 2785 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
6645c3fa 2786 {
52a8b7b8
R
2787 /* ASMSPEC is given, and not the name of a register. Mark the
2788 name with a star so assemble_name won't munge it. */
2789 char *starred = alloca (strlen (asmspec) + 2);
2790 starred[0] = '*';
2791 strcpy (starred + 1, asmspec);
2792
9661b15f
JJ
2793 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
2794 {
2795 tree builtin = built_in_decls [DECL_FUNCTION_CODE (decl)];
2796 SET_DECL_RTL (builtin, NULL_RTX);
52a8b7b8 2797 SET_DECL_ASSEMBLER_NAME (builtin, get_identifier (starred));
9661b15f
JJ
2798#ifdef TARGET_MEM_FUNCTIONS
2799 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMCPY)
52a8b7b8 2800 init_block_move_fn (starred);
9661b15f 2801 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMSET)
52a8b7b8 2802 init_block_clear_fn (starred);
9661b15f
JJ
2803#else
2804 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BCOPY)
52a8b7b8 2805 init_block_move_fn (starred);
9661b15f 2806 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BZERO)
52a8b7b8 2807 init_block_clear_fn (starred);
9661b15f
JJ
2808#endif
2809 }
19e7881c 2810 SET_DECL_RTL (decl, NULL_RTX);
fccc4eb2 2811 change_decl_assembler_name (decl, get_identifier (starred));
6645c3fa 2812 }
3c4afaa5 2813
d05cc98e 2814 /* If #pragma weak was used, mark the decl weak now. */
f8521984 2815 if (current_scope == global_scope)
d05cc98e
GK
2816 maybe_apply_pragma_weak (decl);
2817
51e29401
RS
2818 /* Output the assembler code and/or RTL code for variables and functions,
2819 unless the type is an undefined structure or union.
2820 If not, it will get done when the type is completed. */
2821
2822 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
2823 {
0f7866e7 2824 /* This is a no-op in c-lang.c or something real in objc-act.c. */
37fa72e9 2825 if (c_dialect_objc ())
0f7866e7 2826 objc_check_decl (decl);
8f17b5c5 2827
4b1e44be 2828 if (DECL_FILE_SCOPE_P (decl))
8cf8d8a2
JM
2829 {
2830 if (DECL_INITIAL (decl) == NULL_TREE
2831 || DECL_INITIAL (decl) == error_mark_node)
2832 /* Don't output anything
2833 when a tentative file-scope definition is seen.
2834 But at end of compilation, do output code for them. */
2835 DECL_DEFER_OUTPUT (decl) = 1;
d1bd0ded 2836 rest_of_decl_compilation (decl, asmspec, true, 0);
8cf8d8a2 2837 }
8f17b5c5
MM
2838 else
2839 {
0adc3c19
MM
2840 /* This is a local variable. If there is an ASMSPEC, the
2841 user has requested that we handle it specially. */
8f17b5c5 2842 if (asmspec)
3645c4dc 2843 {
0adc3c19
MM
2844 /* In conjunction with an ASMSPEC, the `register'
2845 keyword indicates that we should place the variable
2846 in a particular register. */
2847 if (DECL_REGISTER (decl))
2848 DECL_C_HARD_REGISTER (decl) = 1;
2849
2850 /* If this is not a static variable, issue a warning.
2851 It doesn't make any sense to give an ASMSPEC for an
2852 ordinary, non-register local variable. Historically,
2853 GCC has accepted -- but ignored -- the ASMSPEC in
2854 this case. */
35b1a6fa 2855 if (TREE_CODE (decl) == VAR_DECL
0adc3c19
MM
2856 && !DECL_REGISTER (decl)
2857 && !TREE_STATIC (decl))
ddd2d57e
RH
2858 warning ("%Jignoring asm-specifier for non-static local "
2859 "variable '%D'", decl, decl);
0adc3c19 2860 else
fccc4eb2 2861 change_decl_assembler_name (decl, get_identifier (asmspec));
3645c4dc 2862 }
0adc3c19 2863
c0a4369a
JJ
2864 if (TREE_CODE (decl) != FUNCTION_DECL)
2865 add_decl_stmt (decl);
8f17b5c5 2866 }
4dd7201e 2867
4b1e44be 2868 if (!DECL_FILE_SCOPE_P (decl))
51e29401
RS
2869 {
2870 /* Recompute the RTL of a local array now
2871 if it used to be an incomplete type. */
2872 if (was_incomplete
1394aabd 2873 && ! TREE_STATIC (decl) && ! DECL_EXTERNAL (decl))
51e29401
RS
2874 {
2875 /* If we used it already as memory, it must stay in memory. */
2876 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
2877 /* If it's still incomplete now, no init will save it. */
2878 if (DECL_SIZE (decl) == 0)
2879 DECL_INITIAL (decl) = 0;
51e29401 2880 }
51e29401
RS
2881 }
2882 }
2883
d1bd0ded
GK
2884 /* If this was marked 'used', be sure it will be output. */
2885 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
2886 mark_referenced (DECL_ASSEMBLER_NAME (decl));
2887
51e29401 2888 if (TREE_CODE (decl) == TYPE_DECL)
264fa2db 2889 rest_of_decl_compilation (decl, NULL, DECL_FILE_SCOPE_P (decl), 0);
51e29401 2890
51e29401
RS
2891 /* At the end of a declaration, throw away any variable type sizes
2892 of types defined inside that declaration. There is no use
2893 computing them in the following function definition. */
f8521984 2894 if (current_scope == global_scope)
51e29401 2895 get_pending_sizes ();
0bfa5f65
RH
2896
2897 /* Install a cleanup (aka destructor) if one was given. */
2898 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
2899 {
2900 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
2901 if (attr)
2902 {
2903 static bool eh_initialized_p;
2904
2905 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
2906 tree cleanup_decl = lookup_name (cleanup_id);
2907 tree cleanup;
2908
2909 /* Build "cleanup(&decl)" for the destructor. */
2910 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
2911 cleanup = build_tree_list (NULL_TREE, cleanup);
2912 cleanup = build_function_call (cleanup_decl, cleanup);
2913
2914 /* Don't warn about decl unused; the cleanup uses it. */
2915 TREE_USED (decl) = 1;
2916
2917 /* Initialize EH, if we've been told to do so. */
2918 if (flag_exceptions && !eh_initialized_p)
2919 {
2920 eh_initialized_p = true;
2921 eh_personality_libfunc
2922 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
2923 ? "__gcc_personality_sj0"
2924 : "__gcc_personality_v0");
2925 using_eh_for_cleanups ();
2926 }
2927
2928 add_stmt (build_stmt (CLEANUP_STMT, decl, cleanup));
2929 }
2930 }
51e29401
RS
2931}
2932
55d54003
ZW
2933/* Given a parsed parameter declaration, decode it into a PARM_DECL
2934 and push that on the current scope. */
51e29401
RS
2935
2936void
35b1a6fa 2937push_parm_decl (tree parm)
51e29401 2938{
6cc902a1 2939 tree decl;
14077d68
ZW
2940
2941 /* Don't attempt to expand sizes while parsing this decl.
2942 (We can get here with i_s_e 1 somehow from Objective-C.) */
2943 int save_immediate_size_expand = immediate_size_expand;
929f3671 2944 immediate_size_expand = 0;
51e29401 2945
005979f2 2946 decl = grokdeclarator (TREE_VALUE (TREE_PURPOSE (parm)),
2ff7cce4
JM
2947 TREE_PURPOSE (TREE_PURPOSE (parm)),
2948 PARM, 0, NULL);
59387d2e 2949 decl_attributes (&decl, TREE_VALUE (parm), 0);
6a5ed5bf 2950
51e29401
RS
2951 decl = pushdecl (decl);
2952
51e29401 2953 finish_decl (decl, NULL_TREE, NULL_TREE);
14077d68
ZW
2954
2955 immediate_size_expand = save_immediate_size_expand;
51e29401
RS
2956}
2957
f91f41b2
ZW
2958/* Mark all the parameter declarations to date as forward decls,
2959 shift them to the variables list, and reset the parameters list.
2960 Also diagnose use of this extension. */
51e29401
RS
2961
2962void
55d54003 2963mark_forward_parm_decls (void)
51e29401 2964{
f91f41b2 2965 tree parm;
55d54003
ZW
2966
2967 if (pedantic && !current_scope->warned_forward_parm_decls)
2968 {
2969 pedwarn ("ISO C forbids forward parameter declarations");
2970 current_scope->warned_forward_parm_decls = true;
2971 }
2972
f91f41b2 2973 for (parm = current_scope->parms; parm; parm = TREE_CHAIN (parm))
55d54003
ZW
2974 TREE_ASM_WRITTEN (parm) = 1;
2975
f91f41b2 2976 SCOPE_LIST_CONCAT (current_scope, names, current_scope, parms);
55d54003 2977 current_scope->parms = 0;
f91f41b2 2978 current_scope->parms_last = 0;
51e29401
RS
2979}
2980\f
17211ab5
GK
2981static GTY(()) int compound_literal_number;
2982
db3acfa5
JM
2983/* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
2984 literal, which may be an incomplete array type completed by the
2985 initializer; INIT is a CONSTRUCTOR that initializes the compound
2986 literal. */
2987
2988tree
35b1a6fa 2989build_compound_literal (tree type, tree init)
db3acfa5
JM
2990{
2991 /* We do not use start_decl here because we have a type, not a declarator;
2992 and do not use finish_decl because the decl should be stored inside
2993 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_STMT. */
2994 tree decl = build_decl (VAR_DECL, NULL_TREE, type);
2995 tree complit;
8d37a5c0 2996 tree stmt;
db3acfa5
JM
2997 DECL_EXTERNAL (decl) = 0;
2998 TREE_PUBLIC (decl) = 0;
f8521984 2999 TREE_STATIC (decl) = (current_scope == global_scope);
db3acfa5
JM
3000 DECL_CONTEXT (decl) = current_function_decl;
3001 TREE_USED (decl) = 1;
3002 TREE_TYPE (decl) = type;
e96eb215 3003 TREE_READONLY (decl) = TREE_READONLY (type);
db3acfa5
JM
3004 store_init_value (decl, init);
3005
3006 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3007 {
3008 int failure = complete_array_type (type, DECL_INITIAL (decl), 1);
3009 if (failure)
3010 abort ();
3011 }
3012
3013 type = TREE_TYPE (decl);
3014 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3015 return error_mark_node;
3016
8d37a5c0
JM
3017 stmt = build_stmt (DECL_STMT, decl);
3018 complit = build1 (COMPOUND_LITERAL_EXPR, TREE_TYPE (decl), stmt);
db3acfa5
JM
3019 TREE_SIDE_EFFECTS (complit) = 1;
3020
3021 layout_decl (decl, 0);
3022
3023 if (TREE_STATIC (decl))
3024 {
3025 /* This decl needs a name for the assembler output. We also need
cf3c4f56
JJ
3026 a unique suffix to be added to the name. */
3027 char *name;
cf3c4f56 3028
35b1a6fa 3029 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
17211ab5
GK
3030 compound_literal_number);
3031 compound_literal_number++;
cf3c4f56
JJ
3032 DECL_NAME (decl) = get_identifier (name);
3033 DECL_DEFER_OUTPUT (decl) = 1;
3034 DECL_COMDAT (decl) = 1;
3035 DECL_ARTIFICIAL (decl) = 1;
3036 pushdecl (decl);
db3acfa5 3037 rest_of_decl_compilation (decl, NULL, 1, 0);
db3acfa5
JM
3038 }
3039
3040 return complit;
3041}
3042\f
51e29401 3043/* Make TYPE a complete type based on INITIAL_VALUE.
929f3671 3044 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
51e29401
RS
3045 2 if there was no information (in which case assume 1 if DO_DEFAULT). */
3046
3047int
35b1a6fa 3048complete_array_type (tree type, tree initial_value, int do_default)
51e29401 3049{
b3694847 3050 tree maxindex = NULL_TREE;
51e29401
RS
3051 int value = 0;
3052
3053 if (initial_value)
3054 {
3055 /* Note MAXINDEX is really the maximum index,
3056 one less than the size. */
3057 if (TREE_CODE (initial_value) == STRING_CST)
3058 {
3059 int eltsize
3060 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
20bf3fac
RS
3061 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
3062 / eltsize) - 1, 0);
51e29401
RS
3063 }
3064 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
3065 {
ecd4cee0 3066 tree elts = CONSTRUCTOR_ELTS (initial_value);
fed3cef0 3067 maxindex = build_int_2 (-1, -1);
ecd4cee0
RS
3068 for (; elts; elts = TREE_CHAIN (elts))
3069 {
3070 if (TREE_PURPOSE (elts))
3071 maxindex = TREE_PURPOSE (elts);
3072 else
fed3cef0
RK
3073 maxindex = fold (build (PLUS_EXPR, integer_type_node,
3074 maxindex, integer_one_node));
ecd4cee0
RS
3075 }
3076 maxindex = copy_node (maxindex);
51e29401
RS
3077 }
3078 else
3079 {
3080 /* Make an error message unless that happened already. */
3081 if (initial_value != error_mark_node)
3082 value = 1;
3083
3084 /* Prevent further error messages. */
b4892310 3085 maxindex = build_int_2 (0, 0);
51e29401
RS
3086 }
3087 }
3088
3089 if (!maxindex)
3090 {
3091 if (do_default)
b4892310 3092 maxindex = build_int_2 (0, 0);
51e29401
RS
3093 value = 2;
3094 }
3095
3096 if (maxindex)
3097 {
3098 TYPE_DOMAIN (type) = build_index_type (maxindex);
3099 if (!TREE_TYPE (maxindex))
3100 TREE_TYPE (maxindex) = TYPE_DOMAIN (type);
3101 }
3102
3103 /* Lay out the type now that we can get the real answer. */
3104
3105 layout_type (type);
3106
3107 return value;
3108}
3109\f
2984fe64
JM
3110/* Determine whether TYPE is a structure with a flexible array member,
3111 or a union containing such a structure (possibly recursively). */
3112
3113static bool
35b1a6fa 3114flexible_array_type_p (tree type)
2984fe64
JM
3115{
3116 tree x;
3117 switch (TREE_CODE (type))
3118 {
3119 case RECORD_TYPE:
3120 x = TYPE_FIELDS (type);
3121 if (x == NULL_TREE)
3122 return false;
3123 while (TREE_CHAIN (x) != NULL_TREE)
3124 x = TREE_CHAIN (x);
3125 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3126 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3127 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3128 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3129 return true;
3130 return false;
3131 case UNION_TYPE:
3132 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3133 {
3134 if (flexible_array_type_p (TREE_TYPE (x)))
3135 return true;
3136 }
3137 return false;
3138 default:
3139 return false;
3140 }
3141}
3142\f
2ff7cce4
JM
3143/* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3144 replacing with appropriate values if they are invalid. */
3145static void
3146check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3147{
3148 tree type_mv;
3149 unsigned int max_width;
3150 unsigned HOST_WIDE_INT w;
3151 const char *name = orig_name ? orig_name: _("<anonymous>");
3152
3153 /* Necessary? */
3154 STRIP_NOPS (*width);
3155
3156 /* Detect and ignore out of range field width and process valid
3157 field widths. */
3158 if (TREE_CODE (*width) != INTEGER_CST)
3159 {
3160 error ("bit-field `%s' width not an integer constant", name);
3161 *width = integer_one_node;
3162 }
3163 else
3164 {
3165 constant_expression_warning (*width);
3166 if (tree_int_cst_sgn (*width) < 0)
3167 {
3168 error ("negative width in bit-field `%s'", name);
3169 *width = integer_one_node;
3170 }
3171 else if (integer_zerop (*width) && orig_name)
3172 {
3173 error ("zero width for bit-field `%s'", name);
3174 *width = integer_one_node;
3175 }
3176 }
3177
3178 /* Detect invalid bit-field type. */
3179 if (TREE_CODE (*type) != INTEGER_TYPE
3180 && TREE_CODE (*type) != BOOLEAN_TYPE
3181 && TREE_CODE (*type) != ENUMERAL_TYPE)
3182 {
3183 error ("bit-field `%s' has invalid type", name);
3184 *type = unsigned_type_node;
3185 }
3186
3187 type_mv = TYPE_MAIN_VARIANT (*type);
3188 if (pedantic
3189 && type_mv != integer_type_node
3190 && type_mv != unsigned_type_node
a3bf324c 3191 && type_mv != boolean_type_node)
2ff7cce4
JM
3192 pedwarn ("type of bit-field `%s' is a GCC extension", name);
3193
3194 if (type_mv == boolean_type_node)
3195 max_width = CHAR_TYPE_SIZE;
3196 else
3197 max_width = TYPE_PRECISION (*type);
3198
3199 if (0 < compare_tree_int (*width, max_width))
3200 {
3201 error ("width of `%s' exceeds its type", name);
3202 w = max_width;
3203 *width = build_int_2 (w, 0);
3204 }
3205 else
3206 w = tree_low_cst (*width, 1);
3207
3208 if (TREE_CODE (*type) == ENUMERAL_TYPE
3209 && (w < min_precision (TYPE_MIN_VALUE (*type), TREE_UNSIGNED (*type))
3210 || w < min_precision (TYPE_MAX_VALUE (*type), TREE_UNSIGNED (*type))))
3211 warning ("`%s' is narrower than values of its type", name);
3212}
3213\f
51e29401
RS
3214/* Given declspecs and a declarator,
3215 determine the name and type of the object declared
3216 and construct a ..._DECL node for it.
3217 (In one case we can return a ..._TYPE node instead.
3218 For invalid input we sometimes return 0.)
3219
3220 DECLSPECS is a chain of tree_list nodes whose value fields
3221 are the storage classes and type specifiers.
3222
3223 DECL_CONTEXT says which syntactic context this declaration is in:
3224 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3225 FUNCDEF for a function definition. Like NORMAL but a few different
3226 error messages in each case. Return value may be zero meaning
3227 this definition is too screwy to try to parse.
3228 PARM for a parameter declaration (either within a function prototype
3229 or before a function body). Make a PARM_DECL, or return void_type_node.
3230 TYPENAME if for a typename (in a cast or sizeof).
3231 Don't make a DECL node; just return the ..._TYPE node.
3232 FIELD for a struct or union field; make a FIELD_DECL.
51e29401 3233 INITIALIZED is 1 if the decl has an initializer.
2ff7cce4
JM
3234 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3235 representing the width of the bit-field.
51e29401
RS
3236
3237 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3238 It may also be so in the PARM case, for a prototype where the
3239 argument type is specified but not the name.
3240
3241 This function is where the complicated C meanings of `static'
929f3671 3242 and `extern' are interpreted. */
51e29401
RS
3243
3244static tree
35b1a6fa 3245grokdeclarator (tree declarator, tree declspecs,
2ff7cce4 3246 enum decl_context decl_context, int initialized, tree *width)
51e29401
RS
3247{
3248 int specbits = 0;
3249 tree spec;
3250 tree type = NULL_TREE;
3251 int longlong = 0;
3252 int constp;
3932261a 3253 int restrictp;
51e29401 3254 int volatilep;
3932261a 3255 int type_quals = TYPE_UNQUALIFIED;
51e29401
RS
3256 int inlinep;
3257 int explicit_int = 0;
3258 int explicit_char = 0;
5ab10c42 3259 int defaulted_int = 0;
51e29401 3260 tree typedef_decl = 0;
2ff7cce4 3261 const char *name, *orig_name;
51e29401
RS
3262 tree typedef_type = 0;
3263 int funcdef_flag = 0;
3264 enum tree_code innermost_code = ERROR_MARK;
929f3671 3265 int size_varies = 0;
91d231cb 3266 tree decl_attr = NULL_TREE;
0e03329a
JM
3267 tree array_ptr_quals = NULL_TREE;
3268 int array_parm_static = 0;
91d231cb 3269 tree returned_attrs = NULL_TREE;
2ff7cce4 3270 bool bitfield = width != NULL;
85b58ca5 3271 tree element_type;
77dbdb57 3272 tree arg_info = NULL_TREE;
51e29401
RS
3273
3274 if (decl_context == FUNCDEF)
3275 funcdef_flag = 1, decl_context = NORMAL;
3276
51e29401
RS
3277 /* Look inside a declarator for the name being declared
3278 and get it as a string, for an error message. */
3279 {
b3694847 3280 tree decl = declarator;
51e29401
RS
3281 name = 0;
3282
3283 while (decl)
3284 switch (TREE_CODE (decl))
3285 {
3286 case ARRAY_REF:
3287 case INDIRECT_REF:
3288 case CALL_EXPR:
3289 innermost_code = TREE_CODE (decl);
3290 decl = TREE_OPERAND (decl, 0);
3291 break;
3292
91d231cb
JM
3293 case TREE_LIST:
3294 decl = TREE_VALUE (decl);
3295 break;
3296
51e29401
RS
3297 case IDENTIFIER_NODE:
3298 name = IDENTIFIER_POINTER (decl);
3299 decl = 0;
3300 break;
3301
3302 default:
3303 abort ();
3304 }
2ff7cce4 3305 orig_name = name;
51e29401
RS
3306 if (name == 0)
3307 name = "type name";
3308 }
3309
3310 /* A function definition's declarator must have the form of
3311 a function declarator. */
3312
3313 if (funcdef_flag && innermost_code != CALL_EXPR)
3314 return 0;
3315
51e29401
RS
3316 /* If this looks like a function definition, make it one,
3317 even if it occurs where parms are expected.
3318 Then store_parm_decls will reject it and not use it as a parm. */
3319 if (decl_context == NORMAL && !funcdef_flag
f8521984 3320 && current_scope->parm_flag)
51e29401
RS
3321 decl_context = PARM;
3322
3323 /* Look through the decl specs and record which ones appear.
3324 Some typespecs are defined as built-in typenames.
3325 Others, the ones that are modifiers of other types,
3326 are represented by bits in SPECBITS: set the bits for
3327 the modifiers that appear. Storage class keywords are also in SPECBITS.
3328
3329 If there is a typedef name or a type, store the type in TYPE.
3330 This includes builtin typedefs such as `int'.
3331
3332 Set EXPLICIT_INT or EXPLICIT_CHAR if the type is `int' or `char'
3333 and did not come from a user typedef.
3334
3335 Set LONGLONG if `long' is mentioned twice. */
3336
3337 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
3338 {
b3694847 3339 tree id = TREE_VALUE (spec);
51e29401 3340
e23bd218
IR
3341 /* If the entire declaration is itself tagged as deprecated then
3342 suppress reports of deprecated items. */
3343 if (id && TREE_DEPRECATED (id))
3344 {
3345 if (deprecated_state != DEPRECATED_SUPPRESS)
3346 warn_deprecated_use (id);
3347 }
3348
51e29401
RS
3349 if (id == ridpointers[(int) RID_INT])
3350 explicit_int = 1;
3351 if (id == ridpointers[(int) RID_CHAR])
3352 explicit_char = 1;
3353
0e5921e8
ZW
3354 if (TREE_CODE (id) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (id))
3355 {
3356 enum rid i = C_RID_CODE (id);
dbbbbf3b 3357 if ((int) i <= (int) RID_LAST_MODIFIER)
0e5921e8 3358 {
3d78f2e9 3359 if (i == RID_LONG && (specbits & (1 << (int) RID_LONG)))
0e5921e8
ZW
3360 {
3361 if (longlong)
3362 error ("`long long long' is too long for GCC");
3363 else
3364 {
3365 if (pedantic && !flag_isoc99 && ! in_system_header
3366 && warn_long_long)
56508306 3367 pedwarn ("ISO C90 does not support `long long'");
0e5921e8
ZW
3368 longlong = 1;
3369 }
3370 }
dbbbbf3b 3371 else if (specbits & (1 << (int) i))
04e6db94
JM
3372 {
3373 if (i == RID_CONST || i == RID_VOLATILE || i == RID_RESTRICT)
3374 {
28fca7e4 3375 if (pedantic && !flag_isoc99)
04e6db94
JM
3376 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
3377 }
3378 else
3379 error ("duplicate `%s'", IDENTIFIER_POINTER (id));
3380 }
3d78f2e9
RH
3381
3382 /* Diagnose "__thread extern". Recall that this list
3383 is in the reverse order seen in the text. */
3384 if (i == RID_THREAD
3385 && (specbits & (1 << (int) RID_EXTERN
3386 | 1 << (int) RID_STATIC)))
3387 {
3388 if (specbits & 1 << (int) RID_EXTERN)
3389 error ("`__thread' before `extern'");
3390 else
3391 error ("`__thread' before `static'");
3392 }
3393
dbbbbf3b 3394 specbits |= 1 << (int) i;
0e5921e8
ZW
3395 goto found;
3396 }
3397 }
51e29401
RS
3398 if (type)
3399 error ("two or more data types in declaration of `%s'", name);
3400 /* Actual typedefs come to us as TYPE_DECL nodes. */
3401 else if (TREE_CODE (id) == TYPE_DECL)
3402 {
6169e5fd
JM
3403 if (TREE_TYPE (id) == error_mark_node)
3404 ; /* Allow the type to default to int to avoid cascading errors. */
3405 else
3406 {
3407 type = TREE_TYPE (id);
3408 decl_attr = DECL_ATTRIBUTES (id);
3409 typedef_decl = id;
3410 }
51e29401
RS
3411 }
3412 /* Built-in types come as identifiers. */
3413 else if (TREE_CODE (id) == IDENTIFIER_NODE)
3414 {
b3694847 3415 tree t = lookup_name (id);
51e29401
RS
3416 if (TREE_TYPE (t) == error_mark_node)
3417 ;
3418 else if (!t || TREE_CODE (t) != TYPE_DECL)
3419 error ("`%s' fails to be a typedef or built in type",
3420 IDENTIFIER_POINTER (id));
3421 else
3422 {
3423 type = TREE_TYPE (t);
3424 typedef_decl = t;
3425 }
3426 }
3427 else if (TREE_CODE (id) != ERROR_MARK)
3428 type = id;
3429
6645c3fa
KH
3430 found:
3431 ;
51e29401
RS
3432 }
3433
3434 typedef_type = type;
3435 if (type)
929f3671 3436 size_varies = C_TYPE_VARIABLE_SIZE (type);
51e29401 3437
5ab10c42 3438 /* No type at all: default to `int', and set DEFAULTED_INT
51e29401
RS
3439 because it was not a user-defined typedef. */
3440
3441 if (type == 0)
3442 {
f5963e61
JL
3443 if ((! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3444 | (1 << (int) RID_SIGNED)
60e9d01c
JM
3445 | (1 << (int) RID_UNSIGNED)
3446 | (1 << (int) RID_COMPLEX))))
03369c93
AS
3447 /* Don't warn about typedef foo = bar. */
3448 && ! (specbits & (1 << (int) RID_TYPEDEF) && initialized)
4dd7201e 3449 && ! in_system_header)
720283f2 3450 {
916269ab 3451 /* Issue a warning if this is an ISO C 99 program or if -Wreturn-type
6f4d7222
UD
3452 and this is a function, or if -Wimplicit; prefer the former
3453 warning since it is more explicit. */
f43b2795
JM
3454 if ((warn_implicit_int || warn_return_type || flag_isoc99)
3455 && funcdef_flag)
720283f2 3456 warn_about_return_type = 1;
916269ab 3457 else if (warn_implicit_int || flag_isoc99)
6645c3fa
KH
3458 pedwarn_c99 ("type defaults to `int' in declaration of `%s'",
3459 name);
720283f2
RK
3460 }
3461
5ab10c42 3462 defaulted_int = 1;
51e29401
RS
3463 type = integer_type_node;
3464 }
3465
3466 /* Now process the modifiers that were specified
3467 and check for invalid combinations. */
3468
3469 /* Long double is a special combination. */
3470
861bb6c1 3471 if ((specbits & 1 << (int) RID_LONG) && ! longlong
90d56da8 3472 && TYPE_MAIN_VARIANT (type) == double_type_node)
51e29401 3473 {
6645c3fa 3474 specbits &= ~(1 << (int) RID_LONG);
51e29401
RS
3475 type = long_double_type_node;
3476 }
3477
3478 /* Check all other uses of type modifiers. */
3479
3480 if (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3481 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED)))
3482 {
3483 int ok = 0;
3484
861bb6c1
JL
3485 if ((specbits & 1 << (int) RID_LONG)
3486 && (specbits & 1 << (int) RID_SHORT))
3487 error ("both long and short specified for `%s'", name);
51e29401
RS
3488 else if (((specbits & 1 << (int) RID_LONG)
3489 || (specbits & 1 << (int) RID_SHORT))
3490 && explicit_char)
3491 error ("long or short specified with char for `%s'", name);
3492 else if (((specbits & 1 << (int) RID_LONG)
3493 || (specbits & 1 << (int) RID_SHORT))
3494 && TREE_CODE (type) == REAL_TYPE)
861bb6c1
JL
3495 {
3496 static int already = 0;
3497
3498 error ("long or short specified with floating type for `%s'", name);
3499 if (! already && ! pedantic)
3500 {
3501 error ("the only valid combination is `long double'");
3502 already = 1;
3503 }
3504 }
51e29401
RS
3505 else if ((specbits & 1 << (int) RID_SIGNED)
3506 && (specbits & 1 << (int) RID_UNSIGNED))
861bb6c1
JL
3507 error ("both signed and unsigned specified for `%s'", name);
3508 else if (TREE_CODE (type) != INTEGER_TYPE)
3509 error ("long, short, signed or unsigned invalid for `%s'", name);
51e29401
RS
3510 else
3511 {
3512 ok = 1;
b462d4ab 3513 if (!explicit_int && !defaulted_int && !explicit_char)
51e29401 3514 {
b462d4ab
JM
3515 error ("long, short, signed or unsigned used invalidly for `%s'",
3516 name);
3517 ok = 0;
51e29401
RS
3518 }
3519 }
3520
3521 /* Discard the type modifiers if they are invalid. */
3522 if (! ok)
3523 {
3524 specbits &= ~((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3525 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED));
3526 longlong = 0;
3527 }
3528 }
3529
c470260b
RK
3530 if ((specbits & (1 << (int) RID_COMPLEX))
3531 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
3532 {
3533 error ("complex invalid for `%s'", name);
6645c3fa 3534 specbits &= ~(1 << (int) RID_COMPLEX);
c470260b
RK
3535 }
3536
51e29401 3537 /* Decide whether an integer type is signed or not.
2ff7cce4 3538 Optionally treat bit-fields as signed by default. */
51e29401 3539 if (specbits & 1 << (int) RID_UNSIGNED
51e29401 3540 || (bitfield && ! flag_signed_bitfields
5ab10c42 3541 && (explicit_int || defaulted_int || explicit_char
51e29401
RS
3542 /* A typedef for plain `int' without `signed'
3543 can be controlled just like plain `int'. */
3544 || ! (typedef_decl != 0
3545 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
3546 && TREE_CODE (type) != ENUMERAL_TYPE
3547 && !(specbits & 1 << (int) RID_SIGNED)))
3548 {
3549 if (longlong)
3550 type = long_long_unsigned_type_node;
3551 else if (specbits & 1 << (int) RID_LONG)
3552 type = long_unsigned_type_node;
3553 else if (specbits & 1 << (int) RID_SHORT)
3554 type = short_unsigned_type_node;
3555 else if (type == char_type_node)
3556 type = unsigned_char_type_node;
3557 else if (typedef_decl)
ceef8ce4 3558 type = c_common_unsigned_type (type);
51e29401
RS
3559 else
3560 type = unsigned_type_node;
3561 }
3562 else if ((specbits & 1 << (int) RID_SIGNED)
3563 && type == char_type_node)
3564 type = signed_char_type_node;
3565 else if (longlong)
3566 type = long_long_integer_type_node;
3567 else if (specbits & 1 << (int) RID_LONG)
3568 type = long_integer_type_node;
3569 else if (specbits & 1 << (int) RID_SHORT)
3570 type = short_integer_type_node;
c470260b
RK
3571
3572 if (specbits & 1 << (int) RID_COMPLEX)
5ab10c42 3573 {
60e9d01c 3574 if (pedantic && !flag_isoc99)
56508306 3575 pedwarn ("ISO C90 does not support complex types");
c470260b
RK
3576 /* If we just have "complex", it is equivalent to
3577 "complex double", but if any modifiers at all are specified it is
3578 the complex form of TYPE. E.g, "complex short" is
3579 "complex short int". */
3580
3581 if (defaulted_int && ! longlong
3582 && ! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3583 | (1 << (int) RID_SIGNED)
3584 | (1 << (int) RID_UNSIGNED))))
60e9d01c
JM
3585 {
3586 if (pedantic)
3587 pedwarn ("ISO C does not support plain `complex' meaning `double complex'");
3588 type = complex_double_type_node;
3589 }
5ab10c42 3590 else if (type == integer_type_node)
60e9d01c
JM
3591 {
3592 if (pedantic)
3593 pedwarn ("ISO C does not support complex integer types");
3594 type = complex_integer_type_node;
3595 }
5ab10c42
RS
3596 else if (type == float_type_node)
3597 type = complex_float_type_node;
3598 else if (type == double_type_node)
3599 type = complex_double_type_node;
3600 else if (type == long_double_type_node)
3601 type = complex_long_double_type_node;
3602 else
60e9d01c
JM
3603 {
3604 if (pedantic)
3605 pedwarn ("ISO C does not support complex integer types");
3606 type = build_complex_type (type);
3607 }
5ab10c42 3608 }
51e29401 3609
2ff7cce4
JM
3610 /* Check the type and width of a bit-field. */
3611 if (bitfield)
3612 check_bitfield_type_and_width (&type, width, orig_name);
3613
3932261a
MM
3614 /* Figure out the type qualifiers for the declaration. There are
3615 two ways a declaration can become qualified. One is something
3616 like `const int i' where the `const' is explicit. Another is
3617 something like `typedef const int CI; CI i' where the type of the
85b58ca5
JM
3618 declaration contains the `const'. A third possibility is that
3619 there is a type qualifier on the element type of a typedefed
3620 array type, in which case we should extract that qualifier so
3621 that c_apply_type_quals_to_decls receives the full list of
3622 qualifiers to work with (C90 is not entirely clear about whether
3623 duplicate qualifiers should be diagnosed in this case, but it
3624 seems most appropriate to do so). */
3625 element_type = strip_array_types (type);
3626 constp = !! (specbits & 1 << (int) RID_CONST) + TYPE_READONLY (element_type);
3627 restrictp
3628 = !! (specbits & 1 << (int) RID_RESTRICT) + TYPE_RESTRICT (element_type);
3629 volatilep
3630 = !! (specbits & 1 << (int) RID_VOLATILE) + TYPE_VOLATILE (element_type);
51e29401 3631 inlinep = !! (specbits & (1 << (int) RID_INLINE));
28fca7e4
RH
3632 if (pedantic && !flag_isoc99)
3633 {
3634 if (constp > 1)
3635 pedwarn ("duplicate `const'");
3636 if (restrictp > 1)
3637 pedwarn ("duplicate `restrict'");
3638 if (volatilep > 1)
3639 pedwarn ("duplicate `volatile'");
3640 }
3932261a 3641 if (! flag_gen_aux_info && (TYPE_QUALS (type)))
51e29401 3642 type = TYPE_MAIN_VARIANT (type);
3932261a
MM
3643 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
3644 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
3645 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
51e29401
RS
3646
3647 /* Warn if two storage classes are given. Default to `auto'. */
3648
3649 {
3650 int nclasses = 0;
3651
3652 if (specbits & 1 << (int) RID_AUTO) nclasses++;
3653 if (specbits & 1 << (int) RID_STATIC) nclasses++;
3654 if (specbits & 1 << (int) RID_EXTERN) nclasses++;
3655 if (specbits & 1 << (int) RID_REGISTER) nclasses++;
3656 if (specbits & 1 << (int) RID_TYPEDEF) nclasses++;
3657
3d78f2e9
RH
3658 /* "static __thread" and "extern __thread" are allowed. */
3659 if ((specbits & (1 << (int) RID_THREAD
3660 | 1 << (int) RID_STATIC
3661 | 1 << (int) RID_EXTERN)) == (1 << (int) RID_THREAD))
3662 nclasses++;
3663
51e29401
RS
3664 /* Warn about storage classes that are invalid for certain
3665 kinds of declarations (parameters, typenames, etc.). */
3666
3667 if (nclasses > 1)
3668 error ("multiple storage classes in declaration of `%s'", name);
3669 else if (funcdef_flag
3670 && (specbits
3671 & ((1 << (int) RID_REGISTER)
3672 | (1 << (int) RID_AUTO)
3d78f2e9
RH
3673 | (1 << (int) RID_TYPEDEF)
3674 | (1 << (int) RID_THREAD))))
51e29401
RS
3675 {
3676 if (specbits & 1 << (int) RID_AUTO
f8521984 3677 && (pedantic || current_scope == global_scope))
51e29401
RS
3678 pedwarn ("function definition declared `auto'");
3679 if (specbits & 1 << (int) RID_REGISTER)
3680 error ("function definition declared `register'");
3681 if (specbits & 1 << (int) RID_TYPEDEF)
3682 error ("function definition declared `typedef'");
3d78f2e9
RH
3683 if (specbits & 1 << (int) RID_THREAD)
3684 error ("function definition declared `__thread'");
6645c3fa 3685 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3d78f2e9 3686 | (1 << (int) RID_AUTO) | (1 << (int) RID_THREAD));
51e29401
RS
3687 }
3688 else if (decl_context != NORMAL && nclasses > 0)
3689 {
3690 if (decl_context == PARM && specbits & 1 << (int) RID_REGISTER)
3691 ;
3692 else
3693 {
913d0833
KG
3694 switch (decl_context)
3695 {
3696 case FIELD:
6645c3fa
KH
3697 error ("storage class specified for structure field `%s'",
3698 name);
913d0833
KG
3699 break;
3700 case PARM:
3701 error ("storage class specified for parameter `%s'", name);
3702 break;
3703 default:
3704 error ("storage class specified for typename");
3705 break;
3706 }
6645c3fa
KH
3707 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3708 | (1 << (int) RID_AUTO) | (1 << (int) RID_STATIC)
3d78f2e9 3709 | (1 << (int) RID_EXTERN) | (1 << (int) RID_THREAD));
51e29401
RS
3710 }
3711 }
3712 else if (specbits & 1 << (int) RID_EXTERN && initialized && ! funcdef_flag)
3713 {
f8521984
ZW
3714 /* `extern' with initialization is invalid if not at file scope. */
3715 if (current_scope == global_scope)
51e29401
RS
3716 warning ("`%s' initialized and declared `extern'", name);
3717 else
3718 error ("`%s' has both `extern' and initializer", name);
3719 }
f8521984 3720 else if (current_scope == global_scope)
3d78f2e9
RH
3721 {
3722 if (specbits & 1 << (int) RID_AUTO)
f8521984 3723 error ("file-scope declaration of `%s' specifies `auto'", name);
3d78f2e9
RH
3724 }
3725 else
3726 {
3727 if (specbits & 1 << (int) RID_EXTERN && funcdef_flag)
3728 error ("nested function `%s' declared `extern'", name);
3729 else if ((specbits & (1 << (int) RID_THREAD
3730 | 1 << (int) RID_EXTERN
3731 | 1 << (int) RID_STATIC))
3732 == (1 << (int) RID_THREAD))
3733 {
3734 error ("function-scope `%s' implicitly auto and declared `__thread'",
3735 name);
3736 specbits &= ~(1 << (int) RID_THREAD);
3737 }
3738 }
51e29401
RS
3739 }
3740
3741 /* Now figure out the structure of the declarator proper.
3742 Descend through it, creating more complex types, until we reach
3743 the declared identifier (or NULL_TREE, in an absolute declarator). */
3744
3745 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE)
3746 {
3747 if (type == error_mark_node)
3748 {
3749 declarator = TREE_OPERAND (declarator, 0);
3750 continue;
3751 }
3752
3753 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
3754 an INDIRECT_REF (for *...),
3755 a CALL_EXPR (for ...(...)),
91d231cb 3756 a TREE_LIST (for nested attributes),
51e29401
RS
3757 an identifier (for the name being declared)
3758 or a null pointer (for the place in an absolute declarator
3759 where the name was omitted).
3760 For the last two cases, we have just exited the loop.
3761
3762 At this point, TYPE is the type of elements of an array,
3763 or for a function to return, or for a pointer to point to.
3764 After this sequence of ifs, TYPE is the type of the
3765 array or function or pointer, and DECLARATOR has had its
3766 outermost layer removed. */
3767
0e03329a
JM
3768 if (array_ptr_quals != NULL_TREE || array_parm_static)
3769 {
3770 /* Only the innermost declarator (making a parameter be of
3771 array type which is converted to pointer type)
3772 may have static or type qualifiers. */
3773 error ("static or type qualifiers in non-parameter array declarator");
3774 array_ptr_quals = NULL_TREE;
3775 array_parm_static = 0;
3776 }
3777
91d231cb
JM
3778 if (TREE_CODE (declarator) == TREE_LIST)
3779 {
3780 /* We encode a declarator with embedded attributes using
3781 a TREE_LIST. */
3782 tree attrs = TREE_PURPOSE (declarator);
3783 tree inner_decl;
3784 int attr_flags = 0;
3785 declarator = TREE_VALUE (declarator);
3786 inner_decl = declarator;
3787 while (inner_decl != NULL_TREE
3788 && TREE_CODE (inner_decl) == TREE_LIST)
3789 inner_decl = TREE_VALUE (inner_decl);
3790 if (inner_decl == NULL_TREE
3791 || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
3792 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
ee96ce90 3793 else if (TREE_CODE (inner_decl) == CALL_EXPR)
91d231cb 3794 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
ee96ce90 3795 else if (TREE_CODE (inner_decl) == ARRAY_REF)
91d231cb
JM
3796 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
3797 returned_attrs = decl_attributes (&type,
3798 chainon (returned_attrs, attrs),
3799 attr_flags);
3800 }
3801 else if (TREE_CODE (declarator) == ARRAY_REF)
51e29401 3802 {
b3694847
SS
3803 tree itype = NULL_TREE;
3804 tree size = TREE_OPERAND (declarator, 1);
1ff1a2d2 3805 /* The index is a signed object `sizetype' bits wide. */
ceef8ce4 3806 tree index_type = c_common_signed_type (sizetype);
51e29401 3807
0e03329a
JM
3808 array_ptr_quals = TREE_TYPE (declarator);
3809 array_parm_static = TREE_STATIC (declarator);
3810
51e29401
RS
3811 declarator = TREE_OPERAND (declarator, 0);
3812
3813 /* Check for some types that there cannot be arrays of. */
3814
71653180 3815 if (VOID_TYPE_P (type))
51e29401
RS
3816 {
3817 error ("declaration of `%s' as array of voids", name);
3818 type = error_mark_node;
3819 }
3820
3821 if (TREE_CODE (type) == FUNCTION_TYPE)
3822 {
3823 error ("declaration of `%s' as array of functions", name);
3824 type = error_mark_node;
3825 }
3826
2984fe64
JM
3827 if (pedantic && flexible_array_type_p (type))
3828 pedwarn ("invalid use of structure with flexible array member");
3829
51e29401
RS
3830 if (size == error_mark_node)
3831 type = error_mark_node;
3832
3833 if (type == error_mark_node)
3834 continue;
3835
3836 /* If size was specified, set ITYPE to a range-type for that size.
3837 Otherwise, ITYPE remains null. finish_decl may figure it out
3838 from an initial value. */
3839
3840 if (size)
3841 {
3842 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
874a7be1 3843 STRIP_TYPE_NOPS (size);
51e29401 3844
2c1a2421 3845 if (! INTEGRAL_TYPE_P (TREE_TYPE (size)))
51e29401
RS
3846 {
3847 error ("size of array `%s' has non-integer type", name);
3848 size = integer_one_node;
3849 }
0a43d680 3850
51e29401 3851 if (pedantic && integer_zerop (size))
89abf8d1 3852 pedwarn ("ISO C forbids zero-size array `%s'", name);
0a43d680 3853
51e29401
RS
3854 if (TREE_CODE (size) == INTEGER_CST)
3855 {
90374cc2 3856 constant_expression_warning (size);
0a43d680 3857 if (tree_int_cst_sgn (size) < 0)
51e29401
RS
3858 {
3859 error ("size of array `%s' is negative", name);
3860 size = integer_one_node;
3861 }
51e29401
RS
3862 }
3863 else
3864 {
0a43d680
RK
3865 /* Make sure the array size remains visibly nonconstant
3866 even if it is (eg) a const variable with known value. */
3867 size_varies = 1;
3868
2d1b5436 3869 if (!flag_isoc99 && pedantic)
1a6603da
RS
3870 {
3871 if (TREE_CONSTANT (size))
56508306 3872 pedwarn ("ISO C90 forbids array `%s' whose size can't be evaluated",
6645c3fa 3873 name);
1a6603da 3874 else
56508306 3875 pedwarn ("ISO C90 forbids variable-size array `%s'",
6645c3fa 3876 name);
1a6603da 3877 }
51e29401 3878 }
0a43d680 3879
967e627a 3880 if (integer_zerop (size))
e9a25f70 3881 {
967e627a
RH
3882 /* A zero-length array cannot be represented with an
3883 unsigned index type, which is what we'll get with
a25f1211
RH
3884 build_index_type. Create an open-ended range instead. */
3885 itype = build_range_type (sizetype, size, NULL_TREE);
e9a25f70 3886 }
967e627a
RH
3887 else
3888 {
3889 /* Compute the maximum valid index, that is, size - 1.
3890 Do the calculation in index_type, so that if it is
3891 a variable the computations will be done in the
3892 proper mode. */
3893 itype = fold (build (MINUS_EXPR, index_type,
3894 convert (index_type, size),
3895 convert (index_type, size_one_node)));
3896
3897 /* If that overflowed, the array is too big.
3898 ??? While a size of INT_MAX+1 technically shouldn't
3899 cause an overflow (because we subtract 1), the overflow
3900 is recorded during the conversion to index_type, before
3901 the subtraction. Handling this case seems like an
3902 unnecessary complication. */
3903 if (TREE_OVERFLOW (itype))
3904 {
3905 error ("size of array `%s' is too large", name);
3906 type = error_mark_node;
3907 continue;
3908 }
e9a25f70 3909
967e627a 3910 if (size_varies)
46e33d43
MM
3911 {
3912 /* We must be able to distinguish the
3913 SAVE_EXPR_CONTEXT for the variably-sized type
3914 so that we can set it correctly in
3915 set_save_expr_context. The convention is
3916 that all SAVE_EXPRs that need to be reset
3917 have NULL_TREE for their SAVE_EXPR_CONTEXT. */
3918 tree cfd = current_function_decl;
3919 if (decl_context == PARM)
3920 current_function_decl = NULL_TREE;
3921 itype = variable_size (itype);
3922 if (decl_context == PARM)
3923 current_function_decl = cfd;
3924 }
967e627a
RH
3925 itype = build_index_type (itype);
3926 }
51e29401 3927 }
a25f1211
RH
3928 else if (decl_context == FIELD)
3929 {
a25f1211 3930 if (pedantic && !flag_isoc99 && !in_system_header)
56508306 3931 pedwarn ("ISO C90 does not support flexible array members");
a25f1211
RH
3932
3933 /* ISO C99 Flexible array members are effectively identical
3934 to GCC's zero-length array extension. */
3935 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
3936 }
51e29401 3937
c7b82833
JM
3938 /* If pedantic, complain about arrays of incomplete types. */
3939
3940 if (pedantic && !COMPLETE_TYPE_P (type))
3941 pedwarn ("array type has incomplete element type");
51e29401 3942
50e65854
JW
3943 /* Build the array type itself, then merge any constancy or
3944 volatility into the target type. We must do it in this order
3945 to ensure that the TYPE_MAIN_VARIANT field of the array type
3946 is set correctly. */
3947
3948 type = build_array_type (type, itype);
3932261a
MM
3949 if (type_quals)
3950 type = c_build_qualified_type (type, type_quals);
51e29401 3951
929f3671 3952 if (size_varies)
51e29401 3953 C_TYPE_VARIABLE_SIZE (type) = 1;
584ef5fe
RH
3954
3955 /* The GCC extension for zero-length arrays differs from
3956 ISO flexible array members in that sizeof yields zero. */
3957 if (size && integer_zerop (size))
3958 {
3959 layout_type (type);
3960 TYPE_SIZE (type) = bitsize_zero_node;
3961 TYPE_SIZE_UNIT (type) = size_zero_node;
3962 }
0e03329a
JM
3963 if (decl_context != PARM
3964 && (array_ptr_quals != NULL_TREE || array_parm_static))
3965 {
3966 error ("static or type qualifiers in non-parameter array declarator");
3967 array_ptr_quals = NULL_TREE;
3968 array_parm_static = 0;
3969 }
51e29401
RS
3970 }
3971 else if (TREE_CODE (declarator) == CALL_EXPR)
3972 {
77dbdb57
ZW
3973 /* Declaring a function type. Say it's a definition only
3974 for the CALL_EXPR closest to the identifier. */
3975 bool really_funcdef = (funcdef_flag
3976 && (TREE_CODE (TREE_OPERAND (declarator, 0))
3977 == IDENTIFIER_NODE));
51e29401
RS
3978 tree arg_types;
3979
77dbdb57 3980 /* Make sure we have a valid type for the function to return. */
51e29401
RS
3981 if (type == error_mark_node)
3982 continue;
3983
929f3671 3984 size_varies = 0;
51e29401
RS
3985
3986 /* Warn about some types functions can't return. */
3987
3988 if (TREE_CODE (type) == FUNCTION_TYPE)
3989 {
3990 error ("`%s' declared as function returning a function", name);
3991 type = integer_type_node;
3992 }
3993 if (TREE_CODE (type) == ARRAY_TYPE)
3994 {
3995 error ("`%s' declared as function returning an array", name);
3996 type = integer_type_node;
3997 }
3998
51e29401
RS
3999 /* Construct the function type and go to the next
4000 inner layer of declarator. */
77dbdb57
ZW
4001 arg_info = TREE_OPERAND (declarator, 1);
4002 arg_types = grokparms (arg_info, really_funcdef);
51e29401 4003
3932261a
MM
4004 /* Type qualifiers before the return type of the function
4005 qualify the return type, not the function type. */
4006 if (type_quals)
e0c9fbb7
JM
4007 {
4008 /* Type qualifiers on a function return type are normally
4009 permitted by the standard but have no effect, so give a
65ca2d60 4010 warning at -Wextra. Qualifiers on a void return type have
e0c9fbb7
JM
4011 meaning as a GNU extension, and are banned on function
4012 definitions in ISO C. FIXME: strictly we shouldn't
4013 pedwarn for qualified void return types except on function
4014 definitions, but not doing so could lead to the undesirable
4015 state of a "volatile void" function return type not being
4016 warned about, and a use of the function being compiled
4017 with GNU semantics, with no diagnostics under -pedantic. */
4018 if (VOID_TYPE_P (type) && pedantic && !in_system_header)
4019 pedwarn ("ISO C forbids qualified void function return type");
4020 else if (extra_warnings
4021 && !(VOID_TYPE_P (type)
4022 && type_quals == TYPE_QUAL_VOLATILE))
4023 warning ("type qualifiers ignored on function return type");
4024
4025 type = c_build_qualified_type (type, type_quals);
4026 }
3932261a 4027 type_quals = TYPE_UNQUALIFIED;
61df2ee2 4028
51e29401
RS
4029 type = build_function_type (type, arg_types);
4030 declarator = TREE_OPERAND (declarator, 0);
4031
4032 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4033 the formal parameter list of this FUNCTION_TYPE to point to
4034 the FUNCTION_TYPE node itself. */
4035
4036 {
b3694847 4037 tree link;
51e29401 4038
77dbdb57 4039 for (link = ARG_INFO_TAGS (arg_info);
51e29401
RS
4040 link;
4041 link = TREE_CHAIN (link))
4042 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4043 }
4044 }
4045 else if (TREE_CODE (declarator) == INDIRECT_REF)
4046 {
4047 /* Merge any constancy or volatility into the target type
4048 for the pointer. */
4049
4050 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
3932261a 4051 && type_quals)
89abf8d1 4052 pedwarn ("ISO C forbids qualified function types");
3932261a
MM
4053 if (type_quals)
4054 type = c_build_qualified_type (type, type_quals);
4055 type_quals = TYPE_UNQUALIFIED;
929f3671 4056 size_varies = 0;
51e29401
RS
4057
4058 type = build_pointer_type (type);
4059
4060 /* Process a list of type modifier keywords
4061 (such as const or volatile) that were given inside the `*'. */
4062
4063 if (TREE_TYPE (declarator))
4064 {
b3694847 4065 tree typemodlist;
51e29401 4066 int erred = 0;
3932261a
MM
4067
4068 constp = 0;
4069 volatilep = 0;
4070 restrictp = 0;
51e29401
RS
4071 for (typemodlist = TREE_TYPE (declarator); typemodlist;
4072 typemodlist = TREE_CHAIN (typemodlist))
4073 {
3932261a
MM
4074 tree qualifier = TREE_VALUE (typemodlist);
4075
0e5921e8 4076 if (C_IS_RESERVED_WORD (qualifier))
51e29401 4077 {
0e5921e8
ZW
4078 if (C_RID_CODE (qualifier) == RID_CONST)
4079 constp++;
4080 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4081 volatilep++;
4082 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4083 restrictp++;
4084 else
4085 erred++;
51e29401 4086 }
0e5921e8
ZW
4087 else
4088 erred++;
51e29401 4089 }
0e5921e8
ZW
4090
4091 if (erred)
4092 error ("invalid type modifier within pointer declarator");
28fca7e4
RH
4093 if (pedantic && !flag_isoc99)
4094 {
4095 if (constp > 1)
4096 pedwarn ("duplicate `const'");
4097 if (volatilep > 1)
4098 pedwarn ("duplicate `volatile'");
4099 if (restrictp > 1)
4100 pedwarn ("duplicate `restrict'");
4101 }
3932261a
MM
4102
4103 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4104 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4105 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
51e29401
RS
4106 }
4107
4108 declarator = TREE_OPERAND (declarator, 0);
4109 }
4110 else
4111 abort ();
4112
4113 }
4114
4115 /* Now TYPE has the actual type. */
4116
e9a25f70
JL
4117 /* Did array size calculations overflow? */
4118
4119 if (TREE_CODE (type) == ARRAY_TYPE
d0f062fb 4120 && COMPLETE_TYPE_P (type)
e9a25f70 4121 && TREE_OVERFLOW (TYPE_SIZE (type)))
68940175
AO
4122 {
4123 error ("size of array `%s' is too large", name);
684d9f3b 4124 /* If we proceed with the array type as it is, we'll eventually
68940175
AO
4125 crash in tree_low_cst(). */
4126 type = error_mark_node;
4127 }
e9a25f70 4128
51e29401
RS
4129 /* If this is declaring a typedef name, return a TYPE_DECL. */
4130
4131 if (specbits & (1 << (int) RID_TYPEDEF))
4132 {
4133 tree decl;
4134 /* Note that the grammar rejects storage classes
4135 in typenames, fields or parameters */
4136 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
3932261a 4137 && type_quals)
89abf8d1 4138 pedwarn ("ISO C forbids qualified function types");
3932261a
MM
4139 if (type_quals)
4140 type = c_build_qualified_type (type, type_quals);
51e29401
RS
4141 decl = build_decl (TYPE_DECL, declarator, type);
4142 if ((specbits & (1 << (int) RID_SIGNED))
4143 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
4144 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
91d231cb 4145 decl_attributes (&decl, returned_attrs, 0);
51e29401
RS
4146 return decl;
4147 }
4148
4149 /* Detect the case of an array type of unspecified size
4150 which came, as such, direct from a typedef name.
4151 We must copy the type, so that each identifier gets
4152 a distinct type, so that each identifier's size can be
4153 controlled separately by its own initializer. */
4154
4155 if (type != 0 && typedef_type != 0
a52fb89b
RK
4156 && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0
4157 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
51e29401
RS
4158 {
4159 type = build_array_type (TREE_TYPE (type), 0);
929f3671 4160 if (size_varies)
51e29401
RS
4161 C_TYPE_VARIABLE_SIZE (type) = 1;
4162 }
4163
4164 /* If this is a type name (such as, in a cast or sizeof),
4165 compute the type and return it now. */
4166
4167 if (decl_context == TYPENAME)
4168 {
4169 /* Note that the grammar rejects storage classes
4170 in typenames, fields or parameters */
4171 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
3932261a 4172 && type_quals)
89abf8d1 4173 pedwarn ("ISO C forbids const or volatile function types");
3932261a
MM
4174 if (type_quals)
4175 type = c_build_qualified_type (type, type_quals);
91d231cb 4176 decl_attributes (&type, returned_attrs, 0);
51e29401
RS
4177 return type;
4178 }
4179
61df2ee2
RS
4180 /* Aside from typedefs and type names (handle above),
4181 `void' at top level (not within pointer)
4182 is allowed only in public variables.
51e29401
RS
4183 We don't complain about parms either, but that is because
4184 a better error message can be made later. */
4185
71653180 4186 if (VOID_TYPE_P (type) && decl_context != PARM
61df2ee2
RS
4187 && ! ((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4188 && ((specbits & (1 << (int) RID_EXTERN))
f8521984 4189 || (current_scope == global_scope
61df2ee2
RS
4190 && !(specbits
4191 & ((1 << (int) RID_STATIC) | (1 << (int) RID_REGISTER)))))))
51e29401 4192 {
c40f7b33 4193 error ("variable or field `%s' declared void", name);
51e29401
RS
4194 type = integer_type_node;
4195 }
4196
4197 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4198 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4199
4200 {
91d231cb 4201 tree decl;
51e29401
RS
4202
4203 if (decl_context == PARM)
4204 {
0f38b811 4205 tree type_as_written;
c530479e 4206 tree promoted_type;
51e29401
RS
4207
4208 /* A parameter declared as an array of T is really a pointer to T.
4209 One declared as a function is really a pointer to a function. */
4210
4211 if (TREE_CODE (type) == ARRAY_TYPE)
4212 {
4213 /* Transfer const-ness of array into that of type pointed to. */
eaf2e788 4214 type = TREE_TYPE (type);
3932261a
MM
4215 if (type_quals)
4216 type = c_build_qualified_type (type, type_quals);
eaf2e788 4217 type = build_pointer_type (type);
3932261a 4218 type_quals = TYPE_UNQUALIFIED;
0e03329a
JM
4219 if (array_ptr_quals)
4220 {
4221 tree new_ptr_quals, new_ptr_attrs;
4222 int erred = 0;
4223 split_specs_attrs (array_ptr_quals, &new_ptr_quals, &new_ptr_attrs);
4224 /* We don't yet implement attributes in this context. */
4225 if (new_ptr_attrs != NULL_TREE)
4226 warning ("attributes in parameter array declarator ignored");
4227
4228 constp = 0;
4229 volatilep = 0;
4230 restrictp = 0;
4231 for (; new_ptr_quals; new_ptr_quals = TREE_CHAIN (new_ptr_quals))
4232 {
4233 tree qualifier = TREE_VALUE (new_ptr_quals);
4234
4235 if (C_IS_RESERVED_WORD (qualifier))
4236 {
4237 if (C_RID_CODE (qualifier) == RID_CONST)
4238 constp++;
4239 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4240 volatilep++;
4241 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4242 restrictp++;
4243 else
4244 erred++;
4245 }
4246 else
4247 erred++;
4248 }
4249
4250 if (erred)
4251 error ("invalid type modifier within array declarator");
4252
4253 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4254 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4255 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4256 }
929f3671 4257 size_varies = 0;
51e29401
RS
4258 }
4259 else if (TREE_CODE (type) == FUNCTION_TYPE)
4260 {
3932261a 4261 if (pedantic && type_quals)
89abf8d1 4262 pedwarn ("ISO C forbids qualified function types");
3932261a
MM
4263 if (type_quals)
4264 type = c_build_qualified_type (type, type_quals);
eaf2e788 4265 type = build_pointer_type (type);
3932261a 4266 type_quals = TYPE_UNQUALIFIED;
51e29401 4267 }
0f38b811
MM
4268 else if (type_quals)
4269 type = c_build_qualified_type (type, type_quals);
35b1a6fa 4270
0f38b811 4271 type_as_written = type;
51e29401 4272
51e29401 4273 decl = build_decl (PARM_DECL, declarator, type);
929f3671 4274 if (size_varies)
51e29401
RS
4275 C_DECL_VARIABLE_SIZE (decl) = 1;
4276
4277 /* Compute the type actually passed in the parmlist,
4278 for the case where there is no prototype.
4279 (For example, shorts and chars are passed as ints.)
4280 When there is a prototype, this is overridden later. */
4281
c530479e
RH
4282 if (type == error_mark_node)
4283 promoted_type = type;
4284 else
ab393bf1 4285 promoted_type = c_type_promotes_to (type);
51e29401 4286
c530479e 4287 DECL_ARG_TYPE (decl) = promoted_type;
51e29401
RS
4288 DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
4289 }
4290 else if (decl_context == FIELD)
4291 {
4292 /* Structure field. It may not be a function. */
dfd48d76 4293
51e29401
RS
4294 if (TREE_CODE (type) == FUNCTION_TYPE)
4295 {
c40f7b33 4296 error ("field `%s' declared as a function", name);
51e29401
RS
4297 type = build_pointer_type (type);
4298 }
d0f062fb
NS
4299 else if (TREE_CODE (type) != ERROR_MARK
4300 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
51e29401 4301 {
c40f7b33 4302 error ("field `%s' has incomplete type", name);
51e29401
RS
4303 type = error_mark_node;
4304 }
4305 /* Move type qualifiers down to element of an array. */
3932261a 4306 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
14077d68
ZW
4307 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4308 type_quals),
4309 TYPE_DOMAIN (type));
51e29401 4310 decl = build_decl (FIELD_DECL, declarator, type);
2bf105ab
RK
4311 DECL_NONADDRESSABLE_P (decl) = bitfield;
4312
929f3671 4313 if (size_varies)
51e29401
RS
4314 C_DECL_VARIABLE_SIZE (decl) = 1;
4315 }
4316 else if (TREE_CODE (type) == FUNCTION_TYPE)
4317 {
fd0b8fce
JW
4318 /* Every function declaration is "external"
4319 except for those which are inside a function body
4320 in which `auto' is used.
4321 That is a case not specified by ANSI C,
4322 and we use it for forward declarations for nested functions. */
4323 int extern_ref = (!(specbits & (1 << (int) RID_AUTO))
f8521984 4324 || current_scope == global_scope);
fd0b8fce 4325
51e29401 4326 if (specbits & (1 << (int) RID_AUTO)
f8521984 4327 && (pedantic || current_scope == global_scope))
c40f7b33 4328 pedwarn ("invalid storage class for function `%s'", name);
51e29401 4329 if (specbits & (1 << (int) RID_REGISTER))
c40f7b33 4330 error ("invalid storage class for function `%s'", name);
3d78f2e9
RH
4331 if (specbits & (1 << (int) RID_THREAD))
4332 error ("invalid storage class for function `%s'", name);
f8521984 4333 /* Function declaration not at file scope.
51e29401
RS
4334 Storage classes other than `extern' are not allowed
4335 and `extern' makes no difference. */
f8521984 4336 if (current_scope != global_scope
51e29401
RS
4337 && (specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_INLINE)))
4338 && pedantic)
c40f7b33 4339 pedwarn ("invalid storage class for function `%s'", name);
fd0b8fce 4340
51e29401 4341 decl = build_decl (FUNCTION_DECL, declarator, type);
91d231cb 4342 decl = build_decl_attribute_variant (decl, decl_attr);
51e29401 4343
703ad42b
KG
4344 DECL_LANG_SPECIFIC (decl)
4345 = ggc_alloc_cleared (sizeof (struct lang_decl));
31ed8fea 4346
3932261a 4347 if (pedantic && type_quals && ! DECL_IN_SYSTEM_HEADER (decl))
89abf8d1 4348 pedwarn ("ISO C forbids qualified function types");
51e29401 4349
3932261a
MM
4350 /* GNU C interprets a `volatile void' return type to indicate
4351 that the function does not return. */
4352 if ((type_quals & TYPE_QUAL_VOLATILE)
71653180 4353 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
11433f42 4354 warning ("`noreturn' function returns non-void value");
61df2ee2 4355
fd0b8fce 4356 if (extern_ref)
1394aabd 4357 DECL_EXTERNAL (decl) = 1;
51e29401
RS
4358 /* Record absence of global scope for `static' or `auto'. */
4359 TREE_PUBLIC (decl)
4360 = !(specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_AUTO)));
c40f7b33 4361
77dbdb57
ZW
4362 /* For a function definition, record the argument information
4363 block in DECL_ARGUMENTS where store_parm_decls will look
4364 for it. */
4365 if (funcdef_flag)
4366 DECL_ARGUMENTS (decl) = arg_info;
4367
5ce89b2e
JM
4368 if (defaulted_int)
4369 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4370
51e29401 4371 /* Record presence of `inline', if it is reasonable. */
31ed8fea 4372 if (MAIN_NAME_P (declarator))
51e29401 4373 {
31ed8fea 4374 if (inlinep)
51e29401 4375 warning ("cannot inline function `main'");
31ed8fea
RH
4376 }
4377 else if (inlinep)
4378 {
b3c3af2f 4379 /* Record that the function is declared `inline'. */
31ed8fea 4380 DECL_DECLARED_INLINE_P (decl) = 1;
51e29401 4381
b79d5213
RH
4382 /* Do not mark bare declarations as DECL_INLINE. Doing so
4383 in the presence of multiple declarations can result in
4384 the abstract origin pointing between the declarations,
4385 which will confuse dwarf2out. */
4386 if (initialized)
4387 {
4388 DECL_INLINE (decl) = 1;
4389 if (specbits & (1 << (int) RID_EXTERN))
4390 current_extern_inline = 1;
4391 }
51e29401 4392 }
31ed8fea
RH
4393 /* If -finline-functions, assume it can be inlined. This does
4394 two things: let the function be deferred until it is actually
4395 needed, and let dwarf2 know that the function is inlinable. */
b79d5213 4396 else if (flag_inline_trees == 2 && initialized)
b3c3af2f 4397 DECL_INLINE (decl) = 1;
51e29401
RS
4398 }
4399 else
4400 {
4401 /* It's a variable. */
fd0b8fce
JW
4402 /* An uninitialized decl with `extern' is a reference. */
4403 int extern_ref = !initialized && (specbits & (1 << (int) RID_EXTERN));
51e29401
RS
4404
4405 /* Move type qualifiers down to element of an array. */
3932261a 4406 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
51e29401 4407 {
54d7f9aa 4408 int saved_align = TYPE_ALIGN(type);
3932261a
MM
4409 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4410 type_quals),
51e29401 4411 TYPE_DOMAIN (type));
54d7f9aa 4412 TYPE_ALIGN (type) = saved_align;
51e29401 4413 }
0f38b811
MM
4414 else if (type_quals)
4415 type = c_build_qualified_type (type, type_quals);
f4e92987
MM
4416
4417 /* It is invalid to create an `extern' declaration for a
4418 variable if there is a global declaration that is
4419 `static'. */
f8521984 4420 if (extern_ref && current_scope != global_scope)
f4e92987
MM
4421 {
4422 tree global_decl;
4423
4424 global_decl = identifier_global_value (declarator);
35b1a6fa 4425 if (global_decl
f4e92987
MM
4426 && TREE_CODE (global_decl) == VAR_DECL
4427 && !TREE_PUBLIC (global_decl))
4428 error ("variable previously declared `static' redeclared "
4429 "`extern'");
4430 }
4431
51e29401 4432 decl = build_decl (VAR_DECL, declarator, type);
929f3671 4433 if (size_varies)
51e29401
RS
4434 C_DECL_VARIABLE_SIZE (decl) = 1;
4435
4436 if (inlinep)
ddd2d57e 4437 pedwarn ("%Jvariable '%D' declared `inline'", decl, decl);
51e29401 4438
fd0b8fce 4439 DECL_EXTERNAL (decl) = extern_ref;
3d78f2e9 4440
f8521984 4441 /* At file scope, the presence of a `static' or `register' storage
ee534ebf
RS
4442 class specifier, or the absence of all storage class specifiers
4443 makes this declaration a definition (perhaps tentative). Also,
4444 the absence of both `static' and `register' makes it public. */
f8521984 4445 if (current_scope == global_scope)
51e29401 4446 {
3d78f2e9
RH
4447 TREE_PUBLIC (decl) = !(specbits & ((1 << (int) RID_STATIC)
4448 | (1 << (int) RID_REGISTER)));
4449 TREE_STATIC (decl) = !extern_ref;
51e29401 4450 }
f8521984 4451 /* Not at file scope, only `static' makes a static definition. */
51e29401
RS
4452 else
4453 {
4454 TREE_STATIC (decl) = (specbits & (1 << (int) RID_STATIC)) != 0;
3d78f2e9
RH
4455 TREE_PUBLIC (decl) = extern_ref;
4456 }
4457
4458 if (specbits & 1 << (int) RID_THREAD)
4459 {
4460 if (targetm.have_tls)
4461 DECL_THREAD_LOCAL (decl) = 1;
4462 else
4463 /* A mere warning is sure to result in improper semantics
4464 at runtime. Don't bother to allow this to compile. */
4465 error ("thread-local storage not supported for this target");
51e29401
RS
4466 }
4467 }
4468
4469 /* Record `register' declaration for warnings on &
4470 and in case doing stupid register allocation. */
4471
4472 if (specbits & (1 << (int) RID_REGISTER))
1394aabd 4473 DECL_REGISTER (decl) = 1;
51e29401
RS
4474
4475 /* Record constancy and volatility. */
3932261a 4476 c_apply_type_quals_to_decl (type_quals, decl);
51e29401 4477
51e29401
RS
4478 /* If a type has volatile components, it should be stored in memory.
4479 Otherwise, the fact that those components are volatile
4480 will be ignored, and would even crash the compiler. */
4481 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
dffd7eb6 4482 c_mark_addressable (decl);
51e29401 4483
d05cc98e
GK
4484#ifdef ENABLE_CHECKING
4485 /* This is the earliest point at which we might know the assembler
4486 name of a variable. Thus, if it's known before this, die horribly. */
4487 if (DECL_ASSEMBLER_NAME_SET_P (decl))
4488 abort ();
4489#endif
4490
91d231cb
JM
4491 decl_attributes (&decl, returned_attrs, 0);
4492
51e29401
RS
4493 return decl;
4494 }
4495}
4496\f
51e29401
RS
4497/* Decode the parameter-list info for a function type or function definition.
4498 The argument is the value returned by `get_parm_info' (or made in parse.y
4499 if there is an identifier list instead of a parameter decl list).
4500 These two functions are separate because when a function returns
4501 or receives functions then each is called multiple times but the order
4502 of calls is different. The last call to `grokparms' is always the one
4503 that contains the formal parameter names of a function definition.
4504
51e29401
RS
4505 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4506
4507 FUNCDEF_FLAG is nonzero for a function definition, 0 for
4508 a mere declaration. A nonempty identifier-list gets an error message
4509 when FUNCDEF_FLAG is zero. */
4510
4511static tree
77dbdb57 4512grokparms (tree arg_info, int funcdef_flag)
51e29401 4513{
77dbdb57 4514 tree arg_types = ARG_INFO_TYPES (arg_info);
51e29401 4515
77dbdb57 4516 if (warn_strict_prototypes && arg_types == 0 && !funcdef_flag
27f427f8 4517 && !in_system_header)
51e29401
RS
4518 warning ("function declaration isn't a prototype");
4519
77dbdb57 4520 if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
51e29401
RS
4521 {
4522 if (! funcdef_flag)
4523 pedwarn ("parameter names (without types) in function declaration");
4524
77dbdb57
ZW
4525 ARG_INFO_PARMS (arg_info) = ARG_INFO_TYPES (arg_info);
4526 ARG_INFO_TYPES (arg_info) = 0;
51e29401
RS
4527 return 0;
4528 }
4529 else
4530 {
77dbdb57
ZW
4531 tree parm, type, typelt;
4532 unsigned int parmno;
4533
4534 /* If the arg types are incomplete in a declaration, they must
4535 include undefined tags. These tags can never be defined in
4536 the scope of the declaration, so the types can never be
4537 completed, and no call can be compiled successfully. */
4538
4539 for (parm = ARG_INFO_PARMS (arg_info), typelt = arg_types, parmno = 1;
eb1dfbb2 4540 parm;
77dbdb57
ZW
4541 parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
4542 {
4543 type = TREE_VALUE (typelt);
4544 if (type == error_mark_node)
4545 continue;
4546
4547 if (!COMPLETE_TYPE_P (type))
4548 {
4549 if (funcdef_flag)
4550 {
4551 if (DECL_NAME (parm))
4552 error ("%Jparameter %u ('%D') has incomplete type",
4553 parm, parmno, parm);
4554 else
4555 error ("%Jparameter %u has incomplete type",
4556 parm, parmno);
51e29401 4557
77dbdb57
ZW
4558 TREE_VALUE (typelt) = error_mark_node;
4559 TREE_TYPE (parm) = error_mark_node;
4560 }
4561 else
4562 {
4563 if (DECL_NAME (parm))
4564 warning ("%Jparameter %u ('%D') has incomplete type",
4565 parm, parmno, parm);
4566 else
4567 warning ("%Jparameter %u has incomplete type",
4568 parm, parmno);
4569 }
4570 }
4571 }
4572 return arg_types;
51e29401
RS
4573 }
4574}
4575
51e29401 4576/* Return a tree_list node with info on a parameter list just parsed.
77dbdb57
ZW
4577 This tree_list node should be examined using the ARG_INFO_* macros,
4578 defined above:
4579 ARG_INFO_PARMS: a list of parameter decls.
4580 ARG_INFO_TAGS: a list of structure, union and enum tags defined.
4581 ARG_INFO_TYPES: a list of argument types to go in the FUNCTION_TYPE.
4582 ARG_INFO_OTHERS: a list of non-parameter decls (notably enumeration
4583 constants) defined with the parameters.
4584
4585 This tree_list node is later fed to 'grokparms' and 'store_parm_decls'.
51e29401
RS
4586
4587 VOID_AT_END nonzero means append `void' to the end of the type-list.
4588 Zero means the parmlist ended with an ellipsis so don't append `void'. */
4589
4590tree
35b1a6fa 4591get_parm_info (int void_at_end)
51e29401 4592{
55d54003 4593 tree decl, type, list;
b3694847 4594 tree types = 0;
55d54003
ZW
4595 tree *last_type = &types;
4596 tree tags = current_scope->tags;
4597 tree parms = current_scope->parms;
4598 tree others = current_scope->names;
4599 static bool explained_incomplete_types = false;
4600 bool gave_void_only_once_err = false;
4601
77dbdb57
ZW
4602 /* Just 'void' (and no ellipsis) is special. There are really no parms.
4603 But if the 'void' is qualified (by 'const' or 'volatile'), or has a
4604 storage class specifier ('register'), then the behavior is undefined;
4605 issue an error. Typedefs for 'void' are OK (see DR#157). */
51e29401
RS
4606 if (void_at_end && parms != 0
4607 && TREE_CHAIN (parms) == 0
71653180 4608 && VOID_TYPE_P (TREE_TYPE (parms))
55d54003 4609 && !DECL_NAME (parms))
51e29401 4610 {
55d54003
ZW
4611 if (TREE_THIS_VOLATILE (parms)
4612 || TREE_READONLY (parms)
4613 || DECL_REGISTER (parms))
77dbdb57 4614 error ("'void' as only parameter may not be qualified");
55d54003 4615
77dbdb57
ZW
4616 list = make_node (TREE_LIST);
4617 ARG_INFO_TYPES (list) = build_tree_list (0, void_type_node);
4618 return list;
51e29401
RS
4619 }
4620
55d54003
ZW
4621 /* Sanity check all of the parameter declarations. */
4622 for (decl = parms; decl; decl = TREE_CHAIN (decl))
4623 {
77dbdb57 4624#ifdef ENABLE_CHECKING
e38e5ba8 4625 if (TREE_CODE (decl) != PARM_DECL)
55d54003
ZW
4626 abort ();
4627 if (TREE_ASM_WRITTEN (decl))
4628 abort ();
77dbdb57 4629#endif
55d54003
ZW
4630
4631 /* Since there is a prototype, args are passed in their
4632 declared types. The back end may override this. */
4633 type = TREE_TYPE (decl);
4634 DECL_ARG_TYPE (decl) = type;
55d54003
ZW
4635
4636 /* Check for (..., void, ...) and issue an error. */
4637 if (VOID_TYPE_P (type) && !DECL_NAME (decl) && !gave_void_only_once_err)
fc3ffe83 4638 {
77dbdb57 4639 error ("'void' must be the only parameter");
55d54003 4640 gave_void_only_once_err = true;
51e29401 4641 }
51e29401 4642
55d54003
ZW
4643 type = build_tree_list (0, type);
4644 *last_type = type;
4645 last_type = &TREE_CHAIN (type);
51e29401
RS
4646 }
4647
55d54003
ZW
4648 /* Check the list of non-parameter decls for any forward parm decls
4649 that never got real decls. */
4650 for (decl = others; decl; decl = TREE_CHAIN (decl))
51e29401
RS
4651 if (TREE_CODE (decl) == PARM_DECL)
4652 {
55d54003
ZW
4653 if (!TREE_ASM_WRITTEN (decl))
4654 abort ();
51e29401 4655
77dbdb57 4656 error ("%Jparameter '%D' has just a forward declaration",
ddd2d57e 4657 decl, decl);
55d54003 4658 }
51e29401 4659
55d54003
ZW
4660 /* Warn about any struct, union or enum tags defined within this
4661 list. The scope of such types is limited to this declaration,
4662 which is rarely if ever desirable (it's impossible to call such
4663 a function with type-correct arguments). */
4664 for (decl = tags; decl; decl = TREE_CHAIN (decl))
51e29401 4665 {
55d54003
ZW
4666 enum tree_code code = TREE_CODE (TREE_VALUE (decl));
4667 const char *keyword;
27301b30
RS
4668 /* An anonymous union parm type is meaningful as a GNU extension.
4669 So don't warn for that. */
55d54003 4670 if (code == UNION_TYPE && TREE_PURPOSE (decl) == 0 && !pedantic)
27301b30 4671 continue;
55d54003
ZW
4672
4673 /* The keyword should not be translated. */
4674 switch (code)
6645c3fa 4675 {
55d54003
ZW
4676 case RECORD_TYPE: keyword = "struct"; break;
4677 case UNION_TYPE: keyword = "union"; break;
4678 case ENUMERAL_TYPE: keyword = "enum"; break;
4679 default: abort ();
5e4adfba 4680 }
55d54003 4681
e13e48e7 4682 if (TREE_PURPOSE (decl))
77dbdb57
ZW
4683 /* The %s will be one of 'struct', 'union', or 'enum'. */
4684 warning ("'%s %E' declared inside parameter list",
4685 keyword, TREE_PURPOSE (decl));
55d54003
ZW
4686 else
4687 /* The %s will be one of 'struct', 'union', or 'enum'. */
4688 warning ("anonymous %s declared inside parameter list", keyword);
4689
4690 if (! explained_incomplete_types)
51e29401 4691 {
55d54003
ZW
4692 warning ("its scope is only this definition or declaration,"
4693 " which is probably not what you want");
4694 explained_incomplete_types = true;
51e29401
RS
4695 }
4696 }
55d54003
ZW
4697
4698
4699 if (void_at_end)
4700 {
4701 type = build_tree_list (0, void_type_node);
4702 *last_type = type;
4703 }
4704
77dbdb57
ZW
4705 list = make_node (TREE_LIST);
4706 ARG_INFO_PARMS (list) = parms;
4707 ARG_INFO_TAGS (list) = tags;
4708 ARG_INFO_TYPES (list) = types;
4709 ARG_INFO_OTHERS (list) = others;
55d54003 4710 return list;
51e29401
RS
4711}
4712\f
4713/* Get the struct, enum or union (CODE says which) with tag NAME.
4714 Define the tag as a forward-reference if it is not defined. */
4715
4716tree
35b1a6fa 4717xref_tag (enum tree_code code, tree name)
51e29401 4718{
51e29401
RS
4719 /* If a cross reference is requested, look up the type
4720 already defined for this tag and return it. */
4721
339a28b9 4722 tree ref = lookup_tag (code, name, 0);
f18b70f5
JM
4723 /* If this is the right type of tag, return what we found.
4724 (This reference will be shadowed by shadow_tag later if appropriate.)
4725 If this is the wrong type of tag, do not return it. If it was the
f8521984
ZW
4726 wrong type in the same scope, we will have had an error
4727 message already; if in a different scope and declaring
f18b70f5 4728 a name, pending_xref_error will give an error message; but if in a
f8521984 4729 different scope and not declaring a name, this tag should
f18b70f5
JM
4730 shadow the previous declaration of a different type of tag, and
4731 this would not work properly if we return the reference found.
4732 (For example, with "struct foo" in an outer scope, "union foo;"
4733 must shadow that tag with a new one of union type.) */
4734 if (ref && TREE_CODE (ref) == code)
51e29401
RS
4735 return ref;
4736
51e29401
RS
4737 /* If no such tag is yet defined, create a forward-reference node
4738 and record it as the "definition".
4739 When a real declaration of this type is found,
4740 the forward-reference will be altered into a real type. */
4741
4742 ref = make_node (code);
4743 if (code == ENUMERAL_TYPE)
4744 {
51e29401
RS
4745 /* Give the type a default layout like unsigned int
4746 to avoid crashing if it does not get defined. */
4747 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
4748 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
11cf4d18 4749 TYPE_USER_ALIGN (ref) = 0;
51e29401
RS
4750 TREE_UNSIGNED (ref) = 1;
4751 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
4752 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
4753 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
4754 }
4755
4756 pushtag (name, ref);
4757
51e29401
RS
4758 return ref;
4759}
4760\f
f8521984 4761/* Make sure that the tag NAME is defined *in the current scope*
51e29401 4762 at least as a forward reference.
4dd7201e 4763 CODE says which kind of tag NAME ought to be. */
51e29401
RS
4764
4765tree
35b1a6fa 4766start_struct (enum tree_code code, tree name)
51e29401 4767{
f8521984 4768 /* If there is already a tag defined at this scope
51e29401
RS
4769 (as a forward reference), just return it. */
4770
b3694847 4771 tree ref = 0;
51e29401
RS
4772
4773 if (name != 0)
339a28b9 4774 ref = lookup_tag (code, name, 1);
51e29401
RS
4775 if (ref && TREE_CODE (ref) == code)
4776 {
51e29401 4777 if (TYPE_FIELDS (ref))
53fcdc76
PB
4778 {
4779 if (code == UNION_TYPE)
5326cd3d 4780 error ("redefinition of `union %s'", IDENTIFIER_POINTER (name));
53fcdc76 4781 else
5326cd3d 4782 error ("redefinition of `struct %s'", IDENTIFIER_POINTER (name));
35b1a6fa 4783 }
51e29401 4784 }
5326cd3d
NS
4785 else
4786 {
4787 /* Otherwise create a forward-reference just so the tag is in scope. */
51e29401 4788
5326cd3d
NS
4789 ref = make_node (code);
4790 pushtag (name, ref);
4791 }
35b1a6fa 4792
51e29401 4793 C_TYPE_BEING_DEFINED (ref) = 1;
02eb6e90 4794 TYPE_PACKED (ref) = flag_pack_struct;
51e29401
RS
4795 return ref;
4796}
4797
4798/* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
4799 of a structure component, returning a FIELD_DECL node.
2ff7cce4 4800 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
51e29401
RS
4801
4802 This is done during the parsing of the struct declaration.
4803 The FIELD_DECL nodes are chained together and the lot of them
4804 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
4805
4806tree
bc4721b8 4807grokfield (tree declarator, tree declspecs, tree width)
51e29401
RS
4808{
4809 tree value;
4810
3e96a2fd
DD
4811 if (declarator == NULL_TREE && width == NULL_TREE)
4812 {
750491fc
RH
4813 /* This is an unnamed decl.
4814
4815 If we have something of the form "union { list } ;" then this
4816 is the anonymous union extension. Similarly for struct.
4817
4818 If this is something of the form "struct foo;", then
4819 If MS extensions are enabled, this is handled as an
4820 anonymous struct.
4821 Otherwise this is a forward declaration of a structure tag.
4822
4823 If this is something of the form "foo;" and foo is a TYPE_DECL, then
4824 If MS extensions are enabled and foo names a structure, then
4825 again this is an anonymous struct.
4826 Otherwise this is an error.
4827
95bd1dd7 4828 Oh what a horrid tangled web we weave. I wonder if MS consciously
750491fc
RH
4829 took this from Plan 9 or if it was an accident of implementation
4830 that took root before someone noticed the bug... */
4831
85d49058
JJ
4832 tree type = TREE_VALUE (declspecs);
4833
750491fc 4834 if (flag_ms_extensions && TREE_CODE (type) == TYPE_DECL)
85d49058 4835 type = TREE_TYPE (type);
750491fc
RH
4836 if (TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE)
4837 {
4838 if (flag_ms_extensions)
4839 ; /* ok */
4840 else if (flag_iso)
4841 goto warn_unnamed_field;
4842 else if (TYPE_NAME (type) == NULL)
4843 ; /* ok */
4844 else
4845 goto warn_unnamed_field;
4846 }
4847 else
3e96a2fd 4848 {
750491fc
RH
4849 warn_unnamed_field:
4850 warning ("declaration does not declare anything");
3e96a2fd
DD
4851 return NULL_TREE;
4852 }
4853 }
4854
2ff7cce4
JM
4855 value = grokdeclarator (declarator, declspecs, FIELD, 0,
4856 width ? &width : NULL);
51e29401 4857
8d9bfdc5 4858 finish_decl (value, NULL_TREE, NULL_TREE);
dfd48d76 4859 DECL_INITIAL (value) = width;
51e29401
RS
4860
4861 return value;
4862}
4863\f
66ea6f4c
RH
4864/* Generate an error for any duplicate field names in FIELDLIST. Munge
4865 the list such that this does not present a problem later. */
4866
4867static void
4868detect_field_duplicates (tree fieldlist)
4869{
4870 tree x, y;
4871 int timeout = 10;
4872
4873 /* First, see if there are more than "a few" fields.
4874 This is trivially true if there are zero or one fields. */
4875 if (!fieldlist)
4876 return;
4877 x = TREE_CHAIN (fieldlist);
4878 if (!x)
4879 return;
4880 do {
4881 timeout--;
4882 x = TREE_CHAIN (x);
4883 } while (timeout > 0 && x);
4884
4885 /* If there were "few" fields, avoid the overhead of allocating
4886 a hash table. Instead just do the nested traversal thing. */
4887 if (timeout > 0)
4888 {
4889 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
4890 if (DECL_NAME (x))
4891 {
4892 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
4893 if (DECL_NAME (y) == DECL_NAME (x))
4894 {
ddd2d57e 4895 error ("%Jduplicate member '%D'", x, x);
66ea6f4c
RH
4896 DECL_NAME (x) = NULL_TREE;
4897 }
4898 }
4899 }
4900 else
4901 {
4902 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
4903 void **slot;
4904
4905 for (x = fieldlist; x ; x = TREE_CHAIN (x))
4906 if ((y = DECL_NAME (x)) != 0)
4907 {
4908 slot = htab_find_slot (htab, y, INSERT);
4909 if (*slot)
4910 {
ddd2d57e 4911 error ("%Jduplicate member '%D'", x, x);
66ea6f4c
RH
4912 DECL_NAME (x) = NULL_TREE;
4913 }
4914 *slot = y;
4915 }
4916
4917 htab_delete (htab);
4918 }
4919}
4920
51e29401 4921/* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
7a0347ff 4922 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
4dd7201e 4923 ATTRIBUTES are attributes to be applied to the structure. */
51e29401
RS
4924
4925tree
35b1a6fa 4926finish_struct (tree t, tree fieldlist, tree attributes)
51e29401 4927{
b3694847 4928 tree x;
f8521984 4929 int toplevel = global_scope == current_scope;
ffc5c6a9 4930 int saw_named_field;
51e29401
RS
4931
4932 /* If this type was previously laid out as a forward reference,
4933 make sure we lay it out again. */
4934
4935 TYPE_SIZE (t) = 0;
4936
91d231cb 4937 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
10861e9a 4938
51e29401
RS
4939 /* Nameless union parm types are useful as GCC extension. */
4940 if (! (TREE_CODE (t) == UNION_TYPE && TYPE_NAME (t) == 0) && !pedantic)
4941 /* Otherwise, warn about any struct or union def. in parmlist. */
4942 if (in_parm_level_p ())
4943 {
4944 if (pedantic)
913d0833 4945 pedwarn ("%s defined inside parms",
5e4adfba 4946 TREE_CODE (t) == UNION_TYPE ? _("union") : _("structure"));
f458d1d5 4947 else
913d0833 4948 warning ("%s defined inside parms",
5e4adfba 4949 TREE_CODE (t) == UNION_TYPE ? _("union") : _("structure"));
51e29401
RS
4950 }
4951
9590fa72
RK
4952 if (pedantic)
4953 {
4954 for (x = fieldlist; x; x = TREE_CHAIN (x))
4955 if (DECL_NAME (x) != 0)
4956 break;
4957
4958 if (x == 0)
5e4adfba
PT
4959 pedwarn ("%s has no %s",
4960 TREE_CODE (t) == UNION_TYPE ? _("union") : _("struct"),
4961 fieldlist ? _("named members") : _("members"));
9590fa72 4962 }
51e29401 4963
dfd48d76
NB
4964 /* Install struct as DECL_CONTEXT of each field decl.
4965 Also process specified field sizes,m which is found in the DECL_INITIAL.
4966 Store 0 there, except for ": 0" fields (so we can find them
4967 and delete them, below). */
51e29401 4968
ffc5c6a9 4969 saw_named_field = 0;
51e29401
RS
4970 for (x = fieldlist; x; x = TREE_CHAIN (x))
4971 {
4972 DECL_CONTEXT (x) = t;
5be1df77 4973 DECL_PACKED (x) |= TYPE_PACKED (t);
51e29401
RS
4974
4975 /* If any field is const, the structure type is pseudo-const. */
4976 if (TREE_READONLY (x))
4977 C_TYPE_FIELDS_READONLY (t) = 1;
4978 else
4979 {
4980 /* A field that is pseudo-const makes the structure likewise. */
4981 tree t1 = TREE_TYPE (x);
4982 while (TREE_CODE (t1) == ARRAY_TYPE)
4983 t1 = TREE_TYPE (t1);
4984 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
4985 && C_TYPE_FIELDS_READONLY (t1))
4986 C_TYPE_FIELDS_READONLY (t) = 1;
4987 }
4988
4989 /* Any field that is volatile means variables of this type must be
4990 treated in some ways as volatile. */
4991 if (TREE_THIS_VOLATILE (x))
4992 C_TYPE_FIELDS_VOLATILE (t) = 1;
4993
4994 /* Any field of nominal variable size implies structure is too. */
4995 if (C_DECL_VARIABLE_SIZE (x))
4996 C_TYPE_VARIABLE_SIZE (t) = 1;
4997
8d7bbe5f
RS
4998 /* Detect invalid nested redefinition. */
4999 if (TREE_TYPE (x) == t)
5000 error ("nested redefinition of `%s'",
5001 IDENTIFIER_POINTER (TYPE_NAME (t)));
5002
dfd48d76
NB
5003 if (DECL_INITIAL (x))
5004 {
2ff7cce4
JM
5005 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5006 DECL_SIZE (x) = bitsize_int (width);
5007 DECL_BIT_FIELD (x) = 1;
5008 SET_DECL_C_BIT_FIELD (x);
dfd48d76
NB
5009 }
5010
dfd48d76
NB
5011 DECL_INITIAL (x) = 0;
5012
ffc5c6a9
RH
5013 /* Detect flexible array member in an invalid context. */
5014 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5015 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5016 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5017 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5018 {
5019 if (TREE_CODE (t) == UNION_TYPE)
ddd2d57e 5020 error ("%Jflexible array member in union", x);
ffc5c6a9 5021 else if (TREE_CHAIN (x) != NULL_TREE)
ddd2d57e 5022 error ("%Jflexible array member not at end of struct", x);
ffc5c6a9 5023 else if (! saw_named_field)
ddd2d57e 5024 error ("%Jflexible array member in otherwise empty struct", x);
ffc5c6a9 5025 }
2984fe64
JM
5026
5027 if (pedantic && TREE_CODE (t) == RECORD_TYPE
5028 && flexible_array_type_p (TREE_TYPE (x)))
ddd2d57e 5029 pedwarn ("%Jinvalid use of structure with flexible array member", x);
2984fe64 5030
ffc5c6a9
RH
5031 if (DECL_NAME (x))
5032 saw_named_field = 1;
05bccae2 5033 }
51e29401 5034
66ea6f4c 5035 detect_field_duplicates (fieldlist);
51e29401
RS
5036
5037 /* Now we have the nearly final fieldlist. Record it,
5038 then lay out the structure or union (including the fields). */
5039
5040 TYPE_FIELDS (t) = fieldlist;
5041
5042 layout_type (t);
5043
6614fd40 5044 /* Delete all zero-width bit-fields from the fieldlist. */
6fbfac92
JM
5045 {
5046 tree *fieldlistp = &fieldlist;
07b983cd
JM
5047 while (*fieldlistp)
5048 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp))
6fbfac92
JM
5049 *fieldlistp = TREE_CHAIN (*fieldlistp);
5050 else
5051 fieldlistp = &TREE_CHAIN (*fieldlistp);
5052 }
51e29401 5053
ffc5c6a9
RH
5054 /* Now we have the truly final field list.
5055 Store it in this type and in the variants. */
51e29401
RS
5056
5057 TYPE_FIELDS (t) = fieldlist;
5058
d07605f5 5059 /* If there are lots of fields, sort so we can look through them fast.
6614fd40 5060 We arbitrarily consider 16 or more elts to be "a lot". */
d07605f5
AP
5061
5062 {
5063 int len = 0;
5064
5065 for (x = fieldlist; x; x = TREE_CHAIN (x))
5066 {
5067 if (len > 15 || DECL_NAME (x) == NULL)
5068 break;
5069 len += 1;
5070 }
5071
5072 if (len > 15)
5073 {
5074 tree *field_array;
5075 struct lang_type *space;
5076 struct sorted_fields_type *space2;
e13e48e7 5077
d07605f5 5078 len += list_length (x);
e13e48e7 5079
d07605f5
AP
5080 /* Use the same allocation policy here that make_node uses, to
5081 ensure that this lives as long as the rest of the struct decl.
5082 All decls in an inline function need to be saved. */
e13e48e7 5083
d07605f5
AP
5084 space = ggc_alloc (sizeof (struct lang_type));
5085 space2 = ggc_alloc (sizeof (struct sorted_fields_type) + len * sizeof (tree));
e13e48e7 5086
d07605f5
AP
5087 len = 0;
5088 space->s = space2;
5089 field_array = &space2->elts[0];
5090 for (x = fieldlist; x; x = TREE_CHAIN (x))
5091 {
5092 field_array[len++] = x;
e13e48e7 5093
938d968e 5094 /* If there is anonymous struct or union, break out of the loop. */
d07605f5
AP
5095 if (DECL_NAME (x) == NULL)
5096 break;
5097 }
938d968e 5098 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
d07605f5
AP
5099 if (x == NULL)
5100 {
5101 TYPE_LANG_SPECIFIC (t) = space;
5102 TYPE_LANG_SPECIFIC (t)->s->len = len;
5103 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5104 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5105 }
5106 }
5107 }
e13e48e7 5108
51e29401
RS
5109 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5110 {
5111 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5112 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5113 TYPE_ALIGN (x) = TYPE_ALIGN (t);
11cf4d18 5114 TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t);
51e29401
RS
5115 }
5116
1604422c
RK
5117 /* If this was supposed to be a transparent union, but we can't
5118 make it one, warn and turn off the flag. */
5119 if (TREE_CODE (t) == UNION_TYPE
5120 && TYPE_TRANSPARENT_UNION (t)
5121 && TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t)))
5122 {
5123 TYPE_TRANSPARENT_UNION (t) = 0;
d787aad9 5124 warning ("union cannot be made transparent");
1604422c
RK
5125 }
5126
51e29401
RS
5127 /* If this structure or union completes the type of any previous
5128 variable declaration, lay it out and output its rtl. */
bf7a697f
ZW
5129 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5130 x;
5131 x = TREE_CHAIN (x))
51e29401 5132 {
bf7a697f
ZW
5133 tree decl = TREE_VALUE (x);
5134 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5135 layout_array_type (TREE_TYPE (decl));
5136 if (TREE_CODE (decl) != TYPE_DECL)
5137 {
5138 layout_decl (decl, 0);
5139 if (c_dialect_objc ())
5140 objc_check_decl (decl);
5141 rest_of_decl_compilation (decl, NULL, toplevel, 0);
5142 if (! toplevel)
5143 expand_decl (decl);
51e29401
RS
5144 }
5145 }
bf7a697f 5146 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
51e29401 5147
51e29401
RS
5148 /* Finish debugging output for this type. */
5149 rest_of_type_compilation (t, toplevel);
5150
5151 return t;
5152}
5153
5154/* Lay out the type T, and its element type, and so on. */
5155
5156static void
35b1a6fa 5157layout_array_type (tree t)
51e29401
RS
5158{
5159 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5160 layout_array_type (TREE_TYPE (t));
5161 layout_type (t);
5162}
5163\f
5164/* Begin compiling the definition of an enumeration type.
5165 NAME is its name (or null if anonymous).
5166 Returns the type object, as yet incomplete.
5167 Also records info about it so that build_enumerator
5168 may be used to declare the individual values as they are read. */
5169
5170tree
35b1a6fa 5171start_enum (tree name)
51e29401 5172{
b3694847 5173 tree enumtype = 0;
51e29401
RS
5174
5175 /* If this is the real definition for a previous forward reference,
5176 fill in the contents in the same object that used to be the
5177 forward reference. */
5178
5179 if (name != 0)
339a28b9 5180 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
51e29401
RS
5181
5182 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5183 {
5184 enumtype = make_node (ENUMERAL_TYPE);
5185 pushtag (name, enumtype);
5186 }
5187
5188 C_TYPE_BEING_DEFINED (enumtype) = 1;
5189
5190 if (TYPE_VALUES (enumtype) != 0)
5191 {
5192 /* This enum is a named one that has been declared already. */
5193 error ("redeclaration of `enum %s'", IDENTIFIER_POINTER (name));
5194
5195 /* Completely replace its old definition.
5196 The old enumerators remain defined, however. */
5197 TYPE_VALUES (enumtype) = 0;
5198 }
5199
5200 enum_next_value = integer_zero_node;
93e3ba4f 5201 enum_overflow = 0;
51e29401 5202
02eb6e90
RK
5203 if (flag_short_enums)
5204 TYPE_PACKED (enumtype) = 1;
5205
51e29401
RS
5206 return enumtype;
5207}
5208
5209/* After processing and defining all the values of an enumeration type,
5210 install their decls in the enumeration type and finish it off.
10861e9a
RK
5211 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5212 and ATTRIBUTES are the specified attributes.
51e29401
RS
5213 Returns ENUMTYPE. */
5214
5215tree
35b1a6fa 5216finish_enum (tree enumtype, tree values, tree attributes)
51e29401 5217{
b3694847 5218 tree pair, tem;
736f85c7 5219 tree minnode = 0, maxnode = 0, enum_value_type;
cb3ca04e 5220 int precision, unsign;
f8521984 5221 int toplevel = (global_scope == current_scope);
51e29401
RS
5222
5223 if (in_parm_level_p ())
5224 warning ("enum defined inside parms");
5225
91d231cb 5226 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
10861e9a 5227
51e29401
RS
5228 /* Calculate the maximum value of any enumerator in this type. */
5229
59116212
RK
5230 if (values == error_mark_node)
5231 minnode = maxnode = integer_zero_node;
5232 else
f500253d 5233 {
cb3ca04e
ZW
5234 minnode = maxnode = TREE_VALUE (values);
5235 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
f500253d 5236 {
cb3ca04e
ZW
5237 tree value = TREE_VALUE (pair);
5238 if (tree_int_cst_lt (maxnode, value))
5239 maxnode = value;
5240 if (tree_int_cst_lt (value, minnode))
5241 minnode = value;
f500253d 5242 }
cb3ca04e 5243 }
f500253d 5244
cb3ca04e
ZW
5245 /* Construct the final type of this enumeration. It is the same
5246 as one of the integral types - the narrowest one that fits, except
5247 that normally we only go as narrow as int - and signed iff any of
5248 the values are negative. */
5249 unsign = (tree_int_cst_sgn (minnode) >= 0);
5250 precision = MAX (min_precision (minnode, unsign),
5251 min_precision (maxnode, unsign));
1ada4cd0 5252 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
cb3ca04e 5253 {
b0c48229 5254 tree narrowest = c_common_type_for_size (precision, unsign);
1ada4cd0
JJ
5255 if (narrowest == 0)
5256 {
5257 warning ("enumeration values exceed range of largest integer");
5258 narrowest = long_long_integer_type_node;
5259 }
5260
5261 precision = TYPE_PRECISION (narrowest);
f500253d 5262 }
1ada4cd0
JJ
5263 else
5264 precision = TYPE_PRECISION (integer_type_node);
51e29401 5265
736f85c7 5266 if (precision == TYPE_PRECISION (integer_type_node))
b0c48229 5267 enum_value_type = c_common_type_for_size (precision, 0);
736f85c7
AO
5268 else
5269 enum_value_type = enumtype;
5270
cb3ca04e
ZW
5271 TYPE_MIN_VALUE (enumtype) = minnode;
5272 TYPE_MAX_VALUE (enumtype) = maxnode;
5273 TYPE_PRECISION (enumtype) = precision;
5274 TREE_UNSIGNED (enumtype) = unsign;
51e29401
RS
5275 TYPE_SIZE (enumtype) = 0;
5276 layout_type (enumtype);
5277
59116212 5278 if (values != error_mark_node)
75b46437 5279 {
cb3ca04e
ZW
5280 /* Change the type of the enumerators to be the enum type. We
5281 need to do this irrespective of the size of the enum, for
5282 proper type checking. Replace the DECL_INITIALs of the
5283 enumerators, and the value slots of the list, with copies
5284 that have the enum type; they cannot be modified in place
5285 because they may be shared (e.g. integer_zero_node) Finally,
5286 change the purpose slots to point to the names of the decls. */
59116212
RK
5287 for (pair = values; pair; pair = TREE_CHAIN (pair))
5288 {
cb3ca04e 5289 tree enu = TREE_PURPOSE (pair);
51e29401 5290
cb3ca04e 5291 TREE_TYPE (enu) = enumtype;
736f85c7
AO
5292
5293 /* The ISO C Standard mandates enumerators to have type int,
5294 even though the underlying type of an enum type is
5295 unspecified. Here we convert any enumerators that fit in
5296 an int to type int, to avoid promotions to unsigned types
5297 when comparing integers with enumerators that fit in the
5298 int range. When -pedantic is given, build_enumerator()
5299 would have already taken care of those that don't fit. */
5300 if (int_fits_type_p (DECL_INITIAL (enu), enum_value_type))
5301 DECL_INITIAL (enu) = convert (enum_value_type, DECL_INITIAL (enu));
5302 else
5303 DECL_INITIAL (enu) = convert (enumtype, DECL_INITIAL (enu));
cb3ca04e
ZW
5304
5305 TREE_PURPOSE (pair) = DECL_NAME (enu);
5306 TREE_VALUE (pair) = DECL_INITIAL (enu);
5307 }
51e29401 5308
59116212
RK
5309 TYPE_VALUES (enumtype) = values;
5310 }
51e29401 5311
fbe23ee7
RS
5312 /* Fix up all variant types of this enum type. */
5313 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5314 {
cb3ca04e
ZW
5315 if (tem == enumtype)
5316 continue;
fbe23ee7
RS
5317 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5318 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5319 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5320 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
def9b006 5321 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
fbe23ee7
RS
5322 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5323 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5324 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
11cf4d18 5325 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
fbe23ee7
RS
5326 TREE_UNSIGNED (tem) = TREE_UNSIGNED (enumtype);
5327 }
5328
51e29401
RS
5329 /* Finish debugging output for this type. */
5330 rest_of_type_compilation (enumtype, toplevel);
5331
5332 return enumtype;
5333}
5334
5335/* Build and install a CONST_DECL for one value of the
5336 current enumeration type (one that was begun with start_enum).
5337 Return a tree-list containing the CONST_DECL and its value.
5338 Assignment of sequential values by default is handled here. */
5339
5340tree
35b1a6fa 5341build_enumerator (tree name, tree value)
51e29401 5342{
b3694847 5343 tree decl, type;
51e29401
RS
5344
5345 /* Validate and default VALUE. */
5346
5347 /* Remove no-op casts from the value. */
cd7a1451 5348 if (value)
874a7be1 5349 STRIP_TYPE_NOPS (value);
51e29401 5350
90374cc2 5351 if (value != 0)
e681c5a1
RS
5352 {
5353 if (TREE_CODE (value) == INTEGER_CST)
25a1019f
RK
5354 {
5355 value = default_conversion (value);
5356 constant_expression_warning (value);
5357 }
e681c5a1
RS
5358 else
5359 {
5360 error ("enumerator value for `%s' not integer constant",
5361 IDENTIFIER_POINTER (name));
5362 value = 0;
5363 }
5364 }
51e29401
RS
5365
5366 /* Default based on previous value. */
5367 /* It should no longer be possible to have NON_LVALUE_EXPR
5368 in the default. */
5369 if (value == 0)
93e3ba4f
RS
5370 {
5371 value = enum_next_value;
5372 if (enum_overflow)
5373 error ("overflow in enumeration values");
5374 }
51e29401
RS
5375
5376 if (pedantic && ! int_fits_type_p (value, integer_type_node))
5377 {
89abf8d1 5378 pedwarn ("ISO C restricts enumerator values to range of `int'");
736f85c7 5379 value = convert (integer_type_node, value);
51e29401
RS
5380 }
5381
5382 /* Set basis for default for next value. */
5383 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
93e3ba4f 5384 enum_overflow = tree_int_cst_lt (enum_next_value, value);
51e29401
RS
5385
5386 /* Now create a declaration for the enum value name. */
5387
75b46437 5388 type = TREE_TYPE (value);
b0c48229
NB
5389 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5390 TYPE_PRECISION (integer_type_node)),
5391 (TYPE_PRECISION (type)
5392 >= TYPE_PRECISION (integer_type_node)
5393 && TREE_UNSIGNED (type)));
75b46437
RS
5394
5395 decl = build_decl (CONST_DECL, name, type);
0543d026 5396 DECL_INITIAL (decl) = convert (type, value);
51e29401
RS
5397 pushdecl (decl);
5398
4dd7201e 5399 return tree_cons (decl, value, NULL_TREE);
51e29401 5400}
8f17b5c5 5401
51e29401
RS
5402\f
5403/* Create the FUNCTION_DECL for a function definition.
f7a4cec0 5404 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5730cf69 5405 the declaration; they describe the function's name and the type it returns,
51e29401
RS
5406 but twisted together in a fashion that parallels the syntax of C.
5407
5408 This function creates a binding context for the function body
5409 as well as setting up the FUNCTION_DECL in current_function_decl.
5410
5411 Returns 1 on success. If the DECLARATOR is not suitable for a function
5412 (it defines a datum instead), we return 0, which tells
4dd7201e 5413 yyparse to report a parse error. */
51e29401
RS
5414
5415int
35b1a6fa 5416start_function (tree declspecs, tree declarator, tree attributes)
51e29401
RS
5417{
5418 tree decl1, old_decl;
5419 tree restype;
5415b705 5420 int old_immediate_size_expand = immediate_size_expand;
51e29401 5421
0f41302f 5422 current_function_returns_value = 0; /* Assume, until we see it does. */
51e29401 5423 current_function_returns_null = 0;
5ce89b2e 5424 current_function_returns_abnormally = 0;
51e29401
RS
5425 warn_about_return_type = 0;
5426 current_extern_inline = 0;
e13e48e7
EC
5427 c_in_iteration_stmt = 0;
5428 c_in_case_stmt = 0;
51e29401 5429
5415b705
RK
5430 /* Don't expand any sizes in the return type of the function. */
5431 immediate_size_expand = 0;
5432
2ff7cce4 5433 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, NULL);
51e29401
RS
5434
5435 /* If the declarator is not suitable for a function definition,
5436 cause a syntax error. */
5437 if (decl1 == 0)
e9a25f70
JL
5438 {
5439 immediate_size_expand = old_immediate_size_expand;
5440 return 0;
5441 }
51e29401 5442
59387d2e 5443 decl_attributes (&decl1, attributes, 0);
7665dfd4 5444
9162542e
AO
5445 if (DECL_DECLARED_INLINE_P (decl1)
5446 && DECL_UNINLINABLE (decl1)
5447 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
ddd2d57e 5448 warning ("%Jinline function '%D' given attribute noinline", decl1, decl1);
9162542e 5449
51e29401
RS
5450 announce_function (decl1);
5451
d0f062fb 5452 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
51e29401 5453 {
903f51d9 5454 error ("return type is an incomplete type");
51e29401
RS
5455 /* Make it return void instead. */
5456 TREE_TYPE (decl1)
5457 = build_function_type (void_type_node,
5458 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
5459 }
5460
5461 if (warn_about_return_type)
903f51d9 5462 pedwarn_c99 ("return type defaults to `int'");
51e29401 5463
51e29401
RS
5464 /* Make the init_value nonzero so pushdecl knows this is not tentative.
5465 error_mark_node is replaced below (in poplevel) with the BLOCK. */
5466 DECL_INITIAL (decl1) = error_mark_node;
5467
5468 /* If this definition isn't a prototype and we had a prototype declaration
5469 before, copy the arg type info from that prototype.
5470 But not if what we had before was a builtin function. */
5471 old_decl = lookup_name_current_level (DECL_NAME (decl1));
5472 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
5473 && !DECL_BUILT_IN (old_decl)
fa7d8b92
RK
5474 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5475 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (old_decl))))
51e29401 5476 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
50a9145c
JW
5477 {
5478 TREE_TYPE (decl1) = TREE_TYPE (old_decl);
f31686a3 5479 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
50a9145c 5480 }
51e29401
RS
5481
5482 /* Optionally warn of old-fashioned def with no previous prototype. */
5483 if (warn_strict_prototypes
5484 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
339a28b9 5485 && C_DECL_ISNT_PROTOTYPE (old_decl))
51e29401
RS
5486 warning ("function declaration isn't a prototype");
5487 /* Optionally warn of any global def with no previous prototype. */
5488 else if (warn_missing_prototypes
5489 && TREE_PUBLIC (decl1)
339a28b9
ZW
5490 && ! MAIN_NAME_P (DECL_NAME (decl1))
5491 && C_DECL_ISNT_PROTOTYPE (old_decl))
ddd2d57e 5492 warning ("%Jno previous prototype for '%D'", decl1, decl1);
51e29401
RS
5493 /* Optionally warn of any def with no previous prototype
5494 if the function has already been used. */
5495 else if (warn_missing_prototypes
5496 && old_decl != 0 && TREE_USED (old_decl)
6fc7c517 5497 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
ddd2d57e
RH
5498 warning ("%J'%D' was used with no prototype before its definition",
5499 decl1, decl1);
1474fe46
RK
5500 /* Optionally warn of any global def with no previous declaration. */
5501 else if (warn_missing_declarations
5502 && TREE_PUBLIC (decl1)
5503 && old_decl == 0
5b47282c 5504 && ! MAIN_NAME_P (DECL_NAME (decl1)))
ddd2d57e 5505 warning ("%Jno previous declaration for '%D'", decl1, decl1);
1474fe46
RK
5506 /* Optionally warn of any def with no previous declaration
5507 if the function has already been used. */
5508 else if (warn_missing_declarations
6fc7c517 5509 && old_decl != 0 && TREE_USED (old_decl)
339a28b9 5510 && C_DECL_IMPLICIT (old_decl))
ddd2d57e
RH
5511 warning ("%J`%D' was used with no declaration before its definition",
5512 decl1, decl1);
51e29401
RS
5513
5514 /* This is a definition, not a reference.
1394aabd 5515 So normally clear DECL_EXTERNAL.
51e29401 5516 However, `extern inline' acts like a declaration
1394aabd
RS
5517 except for defining how to inline. So set DECL_EXTERNAL in that case. */
5518 DECL_EXTERNAL (decl1) = current_extern_inline;
51e29401
RS
5519
5520 /* This function exists in static storage.
5521 (This does not mean `static' in the C sense!) */
5522 TREE_STATIC (decl1) = 1;
5523
5524 /* A nested function is not global. */
5525 if (current_function_decl != 0)
5526 TREE_PUBLIC (decl1) = 0;
5527
d05cc98e
GK
5528#ifdef ENABLE_CHECKING
5529 /* This is the earliest point at which we might know the assembler
5530 name of the function. Thus, if it's set before this, die horribly. */
5531 if (DECL_ASSEMBLER_NAME_SET_P (decl1))
5532 abort ();
5533#endif
5534
5535 /* If #pragma weak was used, mark the decl weak now. */
f8521984 5536 if (current_scope == global_scope)
d05cc98e
GK
5537 maybe_apply_pragma_weak (decl1);
5538
6645c3fa 5539 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
5b47282c 5540 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
b8705e61
RK
5541 {
5542 tree args;
5543 int argct = 0;
5544
5545 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
6645c3fa 5546 != integer_type_node)
ddd2d57e 5547 pedwarn ("%Jreturn type of '%D' is not `int'", decl1, decl1);
b8705e61
RK
5548
5549 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
5550 args = TREE_CHAIN (args))
5551 {
5552 tree type = args ? TREE_VALUE (args) : 0;
5553
5554 if (type == void_type_node)
5555 break;
5556
5557 ++argct;
5558 switch (argct)
5559 {
5560 case 1:
5561 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
ddd2d57e
RH
5562 pedwarn ("%Jfirst argument of '%D' should be `int'",
5563 decl1, decl1);
b8705e61
RK
5564 break;
5565
5566 case 2:
5567 if (TREE_CODE (type) != POINTER_TYPE
5568 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5569 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5570 != char_type_node))
ddd2d57e
RH
5571 pedwarn ("%Jsecond argument of '%D' should be 'char **'",
5572 decl1, decl1);
b8705e61
RK
5573 break;
5574
5575 case 3:
5576 if (TREE_CODE (type) != POINTER_TYPE
5577 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5578 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5579 != char_type_node))
ddd2d57e
RH
5580 pedwarn ("%Jthird argument of '%D' should probably be "
5581 "'char **'", decl1, decl1);
b8705e61
RK
5582 break;
5583 }
d71f83ca 5584 }
b8705e61 5585
d71f83ca 5586 /* It is intentional that this message does not mention the third
6d1c15cc
RK
5587 argument because it's only mentioned in an appendix of the
5588 standard. */
d71f83ca 5589 if (argct > 0 && (argct < 2 || argct > 3))
ddd2d57e 5590 pedwarn ("%J'%D' takes only zero or two arguments", decl1, decl1);
b8705e61 5591
b8705e61 5592 if (! TREE_PUBLIC (decl1))
ddd2d57e 5593 pedwarn ("%J'%D' is normally a non-static function", decl1, decl1);
b8705e61
RK
5594 }
5595
51e29401
RS
5596 /* Record the decl so that the function name is defined.
5597 If we already have a decl for this name, and it is a FUNCTION_DECL,
5598 use the old decl. */
5599
5600 current_function_decl = pushdecl (decl1);
5601
5602 pushlevel (0);
eb1dfbb2 5603 declare_parm_level ();
51e29401 5604
6c418184 5605 make_decl_rtl (current_function_decl, NULL);
51e29401
RS
5606
5607 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
5608 /* Promote the value to int before returning it. */
d72040f5 5609 if (c_promoting_integer_type_p (restype))
42dfa47f 5610 {
f458d1d5 5611 /* It retains unsignedness if not really getting wider. */
42dfa47f 5612 if (TREE_UNSIGNED (restype)
f458d1d5
ZW
5613 && (TYPE_PRECISION (restype)
5614 == TYPE_PRECISION (integer_type_node)))
42dfa47f
RS
5615 restype = unsigned_type_node;
5616 else
5617 restype = integer_type_node;
5618 }
8d9bfdc5
RK
5619 DECL_RESULT (current_function_decl)
5620 = build_decl (RESULT_DECL, NULL_TREE, restype);
51e29401 5621
51e29401
RS
5622 /* If this fcn was already referenced via a block-scope `extern' decl
5623 (or an implicit decl), propagate certain information about the usage. */
5624 if (TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (current_function_decl)))
5625 TREE_ADDRESSABLE (current_function_decl) = 1;
5626
5415b705
RK
5627 immediate_size_expand = old_immediate_size_expand;
5628
0ba8a114 5629 start_fname_decls ();
35b1a6fa 5630
51e29401
RS
5631 return 1;
5632}
51e29401 5633\f
66db6b62
ZW
5634/* Subroutine of store_parm_decls which handles new-style function
5635 definitions (prototype format). The parms already have decls, so we
5636 need only record them as in effect and complain if any redundant
5637 old-style parm decls were written. */
5638static void
77dbdb57 5639store_parm_decls_newstyle (tree fndecl, tree arg_info)
51e29401 5640{
f91f41b2 5641 tree decl, last;
77dbdb57
ZW
5642
5643 tree parms = ARG_INFO_PARMS (arg_info);
5644 tree tags = ARG_INFO_TAGS (arg_info);
5645 tree others = ARG_INFO_OTHERS (arg_info);
51e29401 5646
55d54003 5647 if (current_scope->parms || current_scope->names || current_scope->tags)
66db6b62 5648 {
ddd2d57e
RH
5649 error ("%Jold-style parameter declarations in prototyped "
5650 "function definition", fndecl);
51e29401 5651
66db6b62
ZW
5652 /* Get rid of the old-style declarations. */
5653 poplevel (0, 0, 0);
5654 pushlevel (0);
5655 }
51e29401 5656
55d54003
ZW
5657 /* Now make all the parameter declarations visible in the function body.
5658 We can bypass most of the grunt work of pushdecl. */
f91f41b2 5659 for (last = 0, decl = parms; decl; last = decl, decl = TREE_CHAIN (decl))
66db6b62 5660 {
55d54003 5661 DECL_CONTEXT (decl) = current_function_decl;
66db6b62 5662 if (DECL_NAME (decl) == 0)
ddd2d57e 5663 error ("%Jparameter name omitted", decl);
66db6b62 5664 else
55d54003
ZW
5665 {
5666 if (IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)))
5667 current_scope->shadowed
5668 = tree_cons (DECL_NAME (decl),
5669 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)),
5670 current_scope->shadowed);
5671 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)) = decl;
5672 }
66db6b62 5673 }
55d54003 5674 current_scope->parms = parms;
f91f41b2 5675 current_scope->parms_last = last;
26f943fd 5676
66db6b62 5677 /* Record the parameter list in the function declaration. */
55d54003 5678 DECL_ARGUMENTS (fndecl) = parms;
51e29401 5679
66db6b62 5680 /* Now make all the ancillary declarations visible, likewise. */
f91f41b2 5681 for (last = 0, decl = others; decl; last = decl, decl = TREE_CHAIN (decl))
55d54003
ZW
5682 {
5683 DECL_CONTEXT (decl) = current_function_decl;
5684 if (DECL_NAME (decl)
5685 && TYPE_MAIN_VARIANT (TREE_TYPE (decl)) != void_type_node)
5686 {
5687 if (IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)))
5688 current_scope->shadowed
5689 = tree_cons (DECL_NAME (decl),
5690 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)),
5691 current_scope->shadowed);
5692 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)) = decl;
5693 }
5694 }
f91f41b2
ZW
5695 current_scope->names = others;
5696 current_scope->names_last = last;
26f943fd 5697
66db6b62 5698 /* And all the tag declarations. */
55d54003
ZW
5699 for (decl = tags; decl; decl = TREE_CHAIN (decl))
5700 if (TREE_PURPOSE (decl))
5701 {
5702 if (IDENTIFIER_TAG_VALUE (TREE_PURPOSE (decl)))
5703 current_scope->shadowed_tags
5704 = tree_cons (TREE_PURPOSE (decl),
5705 IDENTIFIER_SYMBOL_VALUE (TREE_PURPOSE (decl)),
5706 current_scope->shadowed_tags);
5707 IDENTIFIER_TAG_VALUE (TREE_PURPOSE (decl)) = TREE_VALUE (decl);
5708 }
5709 current_scope->tags = tags;
66db6b62 5710}
1f731749 5711
66db6b62
ZW
5712/* Subroutine of store_parm_decls which handles old-style function
5713 definitions (separate parameter list and declarations). */
51e29401 5714
66db6b62 5715static void
77dbdb57 5716store_parm_decls_oldstyle (tree fndecl, tree arg_info)
66db6b62 5717{
55d54003 5718 tree parm, decl, last;
51e29401 5719
66db6b62 5720 /* This is the identifier list from the function declarator. */
77dbdb57 5721 tree parmids = ARG_INFO_PARMS (arg_info);
51e29401 5722
66db6b62 5723 /* We use DECL_WEAK as a flag to show which parameters have been
f91f41b2
ZW
5724 seen already, since it is not used on PARM_DECL. */
5725#ifdef ENABLE_CHECKING
55d54003 5726 for (parm = current_scope->parms; parm; parm = TREE_CHAIN (parm))
f91f41b2
ZW
5727 if (DECL_WEAK (parm))
5728 abort ();
5729#endif
66db6b62
ZW
5730
5731 /* Match each formal parameter name with its declaration. Save each
5732 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
5733 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
5734 {
5735 if (TREE_VALUE (parm) == 0)
7a0347ff 5736 {
ddd2d57e 5737 error ("%Jparameter name missing from parameter list", fndecl);
66db6b62
ZW
5738 TREE_PURPOSE (parm) = 0;
5739 continue;
7a0347ff 5740 }
51e29401 5741
66db6b62
ZW
5742 decl = IDENTIFIER_SYMBOL_VALUE (TREE_VALUE (parm));
5743 if (decl && DECL_CONTEXT (decl) == fndecl)
51e29401 5744 {
66db6b62
ZW
5745 /* If we got something other than a PARM_DECL it is an error. */
5746 if (TREE_CODE (decl) != PARM_DECL)
ddd2d57e 5747 error ("%J\"%D\" declared as a non-parameter", decl, decl);
66db6b62
ZW
5748 /* If the declaration is already marked, we have a duplicate
5749 name. Complain and ignore the duplicate. */
5750 else if (DECL_WEAK (decl))
51e29401 5751 {
ddd2d57e 5752 error ("%Jmultiple parameters named \"%D\"", decl, decl);
66db6b62
ZW
5753 TREE_PURPOSE (parm) = 0;
5754 continue;
51e29401 5755 }
66db6b62
ZW
5756 /* If the declaration says "void", complain and turn it into
5757 an int. */
5758 else if (VOID_TYPE_P (TREE_TYPE (decl)))
51e29401 5759 {
ddd2d57e 5760 error ("%Jparameter \"%D\" declared void", decl, decl);
66db6b62
ZW
5761 TREE_TYPE (decl) = integer_type_node;
5762 DECL_ARG_TYPE (decl) = integer_type_node;
5763 layout_decl (decl, 0);
51e29401
RS
5764 }
5765 }
66db6b62
ZW
5766 /* If no declaration found, default to int. */
5767 else
51e29401 5768 {
66db6b62
ZW
5769 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
5770 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
f31686a3 5771 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
66db6b62
ZW
5772 pushdecl (decl);
5773
5774 if (flag_isoc99)
ddd2d57e 5775 pedwarn ("%Jtype of \"%D\" defaults to \"int\"", decl, decl);
66db6b62 5776 else if (extra_warnings)
ddd2d57e 5777 warning ("%Jtype of \"%D\" defaults to \"int\"", decl, decl);
51e29401
RS
5778 }
5779
66db6b62
ZW
5780 TREE_PURPOSE (parm) = decl;
5781 DECL_WEAK (decl) = 1;
51e29401 5782 }
51e29401 5783
55d54003
ZW
5784 /* Now examine the parms chain for incomplete declarations
5785 and declarations with no corresponding names. */
51e29401 5786
55d54003 5787 for (parm = current_scope->parms; parm; parm = TREE_CHAIN (parm))
66db6b62 5788 {
66db6b62 5789 if (!COMPLETE_TYPE_P (TREE_TYPE (parm)))
51e29401 5790 {
ddd2d57e 5791 error ("%Jparameter \"%D\" has incomplete type", parm, parm);
66db6b62
ZW
5792 TREE_TYPE (parm) = error_mark_node;
5793 }
51e29401 5794
66db6b62
ZW
5795 if (! DECL_WEAK (parm))
5796 {
ddd2d57e
RH
5797 error ("%Jdeclaration for parameter \"%D\" but no such parameter",
5798 parm, parm);
51e29401 5799
66db6b62
ZW
5800 /* Pretend the parameter was not missing.
5801 This gets us to a standard state and minimizes
5802 further error messages. */
5803 parmids = chainon (parmids, tree_cons (parm, 0, 0));
51e29401 5804 }
66db6b62 5805 }
51e29401 5806
66db6b62
ZW
5807 /* Chain the declarations together in the order of the list of
5808 names. Store that chain in the function decl, replacing the
55d54003 5809 list of names. Update the current scope to match. */
66db6b62 5810 DECL_ARGUMENTS (fndecl) = 0;
66db6b62 5811
55d54003
ZW
5812 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
5813 if (TREE_PURPOSE (parm))
5814 break;
5815 if (parm && TREE_PURPOSE (parm))
5816 {
5817 last = TREE_PURPOSE (parm);
5818 DECL_ARGUMENTS (fndecl) = last;
5819 current_scope->parms = last;
5820 DECL_WEAK (last) = 0;
5821
5822 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
5823 if (TREE_PURPOSE (parm))
5824 {
5825 TREE_CHAIN (last) = TREE_PURPOSE (parm);
5826 last = TREE_PURPOSE (parm);
5827 DECL_WEAK (last) = 0;
5828 }
f91f41b2 5829 current_scope->parms_last = last;
55d54003
ZW
5830 TREE_CHAIN (last) = 0;
5831 }
51e29401 5832
66db6b62
ZW
5833 /* If there was a previous prototype,
5834 set the DECL_ARG_TYPE of each argument according to
5835 the type previously specified, and report any mismatches. */
51e29401 5836
66db6b62
ZW
5837 if (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
5838 {
5839 tree type;
5840 for (parm = DECL_ARGUMENTS (fndecl),
5841 type = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
5842 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
5843 != void_type_node));
5844 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
51e29401 5845 {
66db6b62
ZW
5846 if (parm == 0 || type == 0
5847 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
51e29401 5848 {
66db6b62
ZW
5849 error ("number of arguments doesn't match prototype");
5850 error ("%Hprototype declaration",
5851 &current_function_prototype_locus);
5852 break;
5853 }
5854 /* Type for passing arg must be consistent with that
5855 declared for the arg. ISO C says we take the unqualified
5856 type for parameters declared with qualified type. */
5857 if (! comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
5858 TYPE_MAIN_VARIANT (TREE_VALUE (type)),
5859 COMPARE_STRICT))
5860 {
5861 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
5862 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
51e29401 5863 {
66db6b62
ZW
5864 /* Adjust argument to match prototype. E.g. a previous
5865 `int foo(float);' prototype causes
5866 `int foo(x) float x; {...}' to be treated like
5867 `int foo(float x) {...}'. This is particularly
5868 useful for argument types like uid_t. */
5869 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
5870
61f71b34 5871 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
66db6b62
ZW
5872 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
5873 && TYPE_PRECISION (TREE_TYPE (parm))
5874 < TYPE_PRECISION (integer_type_node))
5875 DECL_ARG_TYPE (parm) = integer_type_node;
5876
5877 if (pedantic)
50a9145c 5878 {
66db6b62
ZW
5879 pedwarn ("promoted argument \"%D\" "
5880 "doesn't match prototype", parm);
5881 pedwarn ("%Hprototype declaration",
5882 &current_function_prototype_locus);
50a9145c 5883 }
51e29401 5884 }
66db6b62
ZW
5885 else
5886 {
5887 error ("argument \"%D\" doesn't match prototype", parm);
5888 error ("%Hprototype declaration",
5889 &current_function_prototype_locus);
5890 }
51e29401 5891 }
51e29401 5892 }
66db6b62
ZW
5893 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
5894 }
51e29401 5895
66db6b62 5896 /* Otherwise, create a prototype that would match. */
51e29401 5897
66db6b62
ZW
5898 else
5899 {
5900 tree actual = 0, last = 0, type;
51e29401 5901
66db6b62
ZW
5902 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
5903 {
5904 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
51e29401
RS
5905 if (last)
5906 TREE_CHAIN (last) = type;
5907 else
5908 actual = type;
66db6b62
ZW
5909 last = type;
5910 }
5911 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
5912 if (last)
5913 TREE_CHAIN (last) = type;
5914 else
5915 actual = type;
51e29401 5916
66db6b62
ZW
5917 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
5918 of the type of this function, but we need to avoid having this
5919 affect the types of other similarly-typed functions, so we must
5920 first force the generation of an identical (but separate) type
5921 node for the relevant function type. The new node we create
5922 will be a variant of the main variant of the original function
5923 type. */
c138f328 5924
66db6b62 5925 TREE_TYPE (fndecl) = build_type_copy (TREE_TYPE (fndecl));
c138f328 5926
66db6b62
ZW
5927 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
5928 }
66db6b62
ZW
5929}
5930
5931/* Store the parameter declarations into the current function declaration.
5932 This is called after parsing the parameter declarations, before
5933 digesting the body of the function.
5934
5935 For an old-style definition, construct a prototype out of the old-style
5936 parameter declarations and inject it into the function's type. */
5937
5938void
5939store_parm_decls (void)
5940{
5941 tree fndecl = current_function_decl;
5942
5943 /* The function containing FNDECL, if any. */
5944 tree context = decl_function_context (fndecl);
5945
77dbdb57
ZW
5946 /* The argument information block for FNDECL. */
5947 tree arg_info = DECL_ARGUMENTS (fndecl);
5948
5949 /* True if this definition is written with a prototype. Since this
5950 is a function definition, we can treat a null parameter list
5951 (i.e. "foo()") as prototyped (C99 6.7.5.3p14) - this reduces
5952 overhead. */
5953 bool prototype = (!ARG_INFO_PARMS (arg_info)
5954 || TREE_CODE (ARG_INFO_PARMS (arg_info)) != TREE_LIST);
66db6b62 5955
66db6b62 5956 if (prototype)
77dbdb57 5957 store_parm_decls_newstyle (fndecl, arg_info);
66db6b62 5958 else
77dbdb57 5959 store_parm_decls_oldstyle (fndecl, arg_info);
51e29401 5960
a8ccdffe 5961 /* The next call to pushlevel will be a function body. */
51e29401 5962
a8ccdffe 5963 next_is_function_body = true;
51e29401 5964
51e29401
RS
5965 /* Write a record describing this function definition to the prototypes
5966 file (if requested). */
5967
5968 gen_aux_info_record (fndecl, 1, 0, prototype);
5969
5970 /* Initialize the RTL code for the function. */
4985cde3 5971 allocate_struct_function (fndecl);
51e29401 5972
8f17b5c5 5973 /* Begin the statement tree for this function. */
66db6b62 5974 begin_stmt_tree (&DECL_SAVED_TREE (fndecl));
51e29401 5975
1f731749
MM
5976 /* If this is a nested function, save away the sizes of any
5977 variable-size types so that we can expand them when generating
5978 RTL. */
5979 if (context)
5980 {
5981 tree t;
5982
35b1a6fa 5983 DECL_LANG_SPECIFIC (fndecl)->pending_sizes
1f731749
MM
5984 = nreverse (get_pending_sizes ());
5985 for (t = DECL_LANG_SPECIFIC (fndecl)->pending_sizes;
5986 t;
5987 t = TREE_CHAIN (t))
5988 SAVE_EXPR_CONTEXT (TREE_VALUE (t)) = context;
5989 }
5990
8f17b5c5
MM
5991 /* This function is being processed in whole-function mode. */
5992 cfun->x_whole_function_mode_p = 1;
93e3ba4f 5993
8f17b5c5
MM
5994 /* Even though we're inside a function body, we still don't want to
5995 call expand_expr to calculate the size of a variable-sized array.
5996 We haven't necessarily assigned RTL to all variables yet, so it's
5997 not safe to try to expand expressions involving them. */
5998 immediate_size_expand = 0;
5999 cfun->x_dont_save_pending_sizes_p = 1;
51e29401
RS
6000}
6001\f
51e29401
RS
6002/* Finish up a function declaration and compile that function
6003 all the way to assembler language output. The free the storage
6004 for the function definition.
6005
4a46cbfb 6006 This is called after parsing the body of the function definition. */
51e29401
RS
6007
6008void
edaf3e03 6009finish_function (void)
51e29401 6010{
b3694847 6011 tree fndecl = current_function_decl;
51e29401 6012
339a28b9
ZW
6013 /* When a function declaration is totally empty, e.g.
6014 void foo(void) { }
6015 (the argument list is irrelevant) the compstmt rule will not
6016 bother calling pushlevel/poplevel, which means we get here with
a8ccdffe
ZW
6017 the scope stack out of sync. Detect this situation by noticing
6018 that current_scope is still as store_parm_decls left it, and do
6019 a dummy push/pop to get back to consistency.
6020 Note that the call to pushlevel does not actually push another
6021 scope - see there for details. */
6022
6023 if (current_scope->parm_flag && next_is_function_body)
339a28b9
ZW
6024 {
6025 pushlevel (0);
a8ccdffe 6026 poplevel (0, 0, 0);
339a28b9
ZW
6027 }
6028
61f71b34
DD
6029 if (TREE_CODE (fndecl) == FUNCTION_DECL
6030 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6031 {
6032 tree args = DECL_ARGUMENTS (fndecl);
6033 for (; args; args = TREE_CHAIN (args))
6034 {
6035 tree type = TREE_TYPE (args);
6036 if (INTEGRAL_TYPE_P (type)
6037 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6038 DECL_ARG_TYPE (args) = integer_type_node;
6039 }
6040 }
6041
67b28863 6042 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
af3fbed1 6043 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
51e29401
RS
6044
6045 /* Must mark the RESULT_DECL as being in this function. */
6046
67b28863 6047 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
af3fbed1 6048 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
51e29401 6049
5b47282c 6050 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
51e29401 6051 {
90d56da8
RS
6052 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6053 != integer_type_node)
b8705e61 6054 {
007aaed0 6055 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6645c3fa 6056 If warn_main is -1 (-Wno-main) we don't want to be warned. */
435ab236 6057 if (!warn_main)
ddd2d57e 6058 pedwarn ("%Jreturn type of '%D' is not `int'", fndecl, fndecl);
b8705e61 6059 }
8e077183
RS
6060 else
6061 {
cd4aafd5 6062#ifdef DEFAULT_MAIN_RETURN
8e077183
RS
6063 /* Make it so that `main' always returns success by default. */
6064 DEFAULT_MAIN_RETURN;
98be7846
JM
6065#else
6066 if (flag_isoc99)
6067 c_expand_return (integer_zero_node);
cd4aafd5 6068#endif
8e077183 6069 }
51e29401 6070 }
35b1a6fa 6071
0ba8a114 6072 finish_fname_decls ();
51e29401 6073
8f17b5c5
MM
6074 /* Tie off the statement tree for this function. */
6075 finish_stmt_tree (&DECL_SAVED_TREE (fndecl));
5ce89b2e
JM
6076
6077 /* Complain if there's just no return statement. */
46cfb101
JM
6078 if (warn_return_type
6079 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
5ce89b2e
JM
6080 && !current_function_returns_value && !current_function_returns_null
6081 /* Don't complain if we abort. */
6082 && !current_function_returns_abnormally
6083 /* Don't warn for main(). */
6084 && !MAIN_NAME_P (DECL_NAME (fndecl))
6085 /* Or if they didn't actually specify a return type. */
6086 && !C_FUNCTION_IMPLICIT_INT (fndecl)
46cfb101 6087 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
5ce89b2e 6088 inline function, as we might never be compiled separately. */
46cfb101 6089 && DECL_INLINE (fndecl))
5ce89b2e
JM
6090 warning ("no return statement in function returning non-void");
6091
03845b47
SB
6092 /* With just -Wextra, complain only if function returns both with
6093 and without a value. */
6094 if (extra_warnings
6095 && current_function_returns_value
6096 && current_function_returns_null)
6097 warning ("this function may return with or without a value");
6098
1da326c3
SB
6099 /* We're leaving the context of this function, so zap cfun.
6100 It's still in DECL_STRUCT_FUNCTION , and we'll restore it in
6101 tree_rest_of_compilation. */
8f17b5c5
MM
6102 cfun = NULL;
6103
4a46cbfb
JH
6104 /* ??? Objc emits functions after finalizing the compilation unit.
6105 This should be cleaned up later and this conditional removed. */
6106 if (!cgraph_global_info_ready)
6b00c969 6107 cgraph_finalize_function (fndecl, false);
4a46cbfb
JH
6108 else
6109 c_expand_body (fndecl);
6110 current_function_decl = NULL;
8f17b5c5
MM
6111}
6112
da7d8304 6113/* Generate the RTL for the body of FNDECL. If NESTED_P is nonzero,
8f17b5c5 6114 then we are already in the process of generating RTL for another
4a46cbfb 6115 function. */
8f17b5c5
MM
6116
6117static void
35b1a6fa 6118c_expand_body_1 (tree fndecl, int nested_p)
8f17b5c5 6119{
8f17b5c5 6120 if (nested_p)
1f731749
MM
6121 {
6122 /* Make sure that we will evaluate variable-sized types involved
6123 in our function's type. */
6124 expand_pending_sizes (DECL_LANG_SPECIFIC (fndecl)->pending_sizes);
c1f927e8
RH
6125
6126 /* Squirrel away our current state. */
6127 push_function_context ();
1f731749 6128 }
af3fbed1 6129
c1f927e8
RH
6130 tree_rest_of_compilation (fndecl, nested_p);
6131
6132 if (nested_p)
6133 /* Return to the enclosing function. */
6134 pop_function_context ();
f4e5c65b 6135
2c5f4139
JM
6136 if (DECL_STATIC_CONSTRUCTOR (fndecl))
6137 {
2cc07db4
RH
6138 if (targetm.have_ctors_dtors)
6139 (* targetm.asm_out.constructor) (XEXP (DECL_RTL (fndecl), 0),
6140 DEFAULT_INIT_PRIORITY);
2c5f4139 6141 else
2cc07db4 6142 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
2c5f4139 6143 }
47907859 6144
2c5f4139
JM
6145 if (DECL_STATIC_DESTRUCTOR (fndecl))
6146 {
2cc07db4
RH
6147 if (targetm.have_ctors_dtors)
6148 (* targetm.asm_out.destructor) (XEXP (DECL_RTL (fndecl), 0),
6149 DEFAULT_INIT_PRIORITY);
2c5f4139 6150 else
2cc07db4 6151 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
2c5f4139 6152 }
51e29401 6153}
e72fcfe8
JH
6154
6155/* Like c_expand_body_1 but only for unnested functions. */
6156
6157void
35b1a6fa 6158c_expand_body (tree fndecl)
e72fcfe8 6159{
af3fbed1 6160
67b28863 6161 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
af3fbed1 6162 c_expand_body_1 (fndecl, 0);
e72fcfe8 6163}
51e29401 6164\f
77c4d6c0
JM
6165/* Check the declarations given in a for-loop for satisfying the C99
6166 constraints. */
6167void
35b1a6fa 6168check_for_loop_decls (void)
77c4d6c0
JM
6169{
6170 tree t;
6171
6172 if (!flag_isoc99)
6173 {
6174 /* If we get here, declarations have been used in a for loop without
6175 the C99 for loop scope. This doesn't make much sense, so don't
6176 allow it. */
f91f41b2 6177 error ("'for' loop initial declaration used outside C99 mode");
77c4d6c0
JM
6178 return;
6179 }
6180 /* C99 subclause 6.8.5 paragraph 3:
6181
6182 [#3] The declaration part of a for statement shall only
6183 declare identifiers for objects having storage class auto or
6184 register.
6185
6186 It isn't clear whether, in this sentence, "identifiers" binds to
6187 "shall only declare" or to "objects" - that is, whether all identifiers
6188 declared must be identifiers for objects, or whether the restriction
6189 only applies to those that are. (A question on this in comp.std.c
6190 in November 2000 received no answer.) We implement the strictest
6191 interpretation, to avoid creating an extension which later causes
6192 problems. */
6193
f91f41b2 6194 for (t = current_scope->tags; t; t = TREE_CHAIN (t))
77c4d6c0
JM
6195 {
6196 if (TREE_PURPOSE (t) != 0)
53fcdc76
PB
6197 {
6198 enum tree_code code = TREE_CODE (TREE_VALUE (t));
35b1a6fa 6199
53fcdc76 6200 if (code == RECORD_TYPE)
f91f41b2 6201 error ("'struct %s' declared in 'for' loop initial declaration",
53fcdc76
PB
6202 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6203 else if (code == UNION_TYPE)
f91f41b2 6204 error ("'union %s' declared in 'for' loop initial declaration",
53fcdc76
PB
6205 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6206 else
f91f41b2 6207 error ("'enum %s' declared in 'for' loop initial declaration",
53fcdc76
PB
6208 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6209 }
77c4d6c0 6210 }
53fcdc76 6211
21d13d83 6212 for (t = current_scope->names; t; t = TREE_CHAIN (t))
77c4d6c0
JM
6213 {
6214 if (TREE_CODE (t) != VAR_DECL && DECL_NAME (t))
ddd2d57e
RH
6215 error ("%Jdeclaration of non-variable '%D' in 'for' loop "
6216 "initial declaration", t, t);
77c4d6c0 6217 else if (TREE_STATIC (t))
ddd2d57e
RH
6218 error ("%Jdeclaration of static variable '%D' in 'for' loop "
6219 "initial declaration", t, t);
77c4d6c0 6220 else if (DECL_EXTERNAL (t))
ddd2d57e
RH
6221 error ("%Jdeclaration of 'extern' variable '%D' in 'for' loop "
6222 "initial declaration", t, t);
77c4d6c0
JM
6223 }
6224}
6225\f
51e29401
RS
6226/* Save and reinitialize the variables
6227 used during compilation of a C function. */
6228
6229void
35b1a6fa 6230c_push_function_context (struct function *f)
51e29401 6231{
e2500fed 6232 struct language_function *p;
703ad42b 6233 p = ggc_alloc (sizeof (struct language_function));
e2500fed 6234 f->language = p;
51e29401 6235
8f17b5c5
MM
6236 p->base.x_stmt_tree = c_stmt_tree;
6237 p->base.x_scope_stmt_stack = c_scope_stmt_stack;
e13e48e7
EC
6238 p->x_in_iteration_stmt = c_in_iteration_stmt;
6239 p->x_in_case_stmt = c_in_case_stmt;
51e29401
RS
6240 p->returns_value = current_function_returns_value;
6241 p->returns_null = current_function_returns_null;
5ce89b2e 6242 p->returns_abnormally = current_function_returns_abnormally;
51e29401
RS
6243 p->warn_about_return_type = warn_about_return_type;
6244 p->extern_inline = current_extern_inline;
51e29401
RS
6245}
6246
6247/* Restore the variables used during compilation of a C function. */
6248
6249void
35b1a6fa 6250c_pop_function_context (struct function *f)
51e29401 6251{
e2500fed 6252 struct language_function *p = f->language;
51e29401 6253
1da326c3 6254 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
8f17b5c5 6255 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
51e29401
RS
6256 {
6257 /* Stop pointing to the local nodes about to be freed. */
6258 /* But DECL_INITIAL must remain nonzero so we know this
6259 was an actual function definition. */
6260 DECL_INITIAL (current_function_decl) = error_mark_node;
6261 DECL_ARGUMENTS (current_function_decl) = 0;
6262 }
6263
8f17b5c5
MM
6264 c_stmt_tree = p->base.x_stmt_tree;
6265 c_scope_stmt_stack = p->base.x_scope_stmt_stack;
e13e48e7
EC
6266 c_in_iteration_stmt = p->x_in_iteration_stmt;
6267 c_in_case_stmt = p->x_in_case_stmt;
51e29401
RS
6268 current_function_returns_value = p->returns_value;
6269 current_function_returns_null = p->returns_null;
5ce89b2e 6270 current_function_returns_abnormally = p->returns_abnormally;
51e29401
RS
6271 warn_about_return_type = p->warn_about_return_type;
6272 current_extern_inline = p->extern_inline;
51e29401 6273
e2500fed 6274 f->language = NULL;
1526a060
BS
6275}
6276
63e1b1c4 6277/* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
37105beb
JM
6278
6279void
35b1a6fa 6280c_dup_lang_specific_decl (tree decl)
37105beb 6281{
8f17b5c5
MM
6282 struct lang_decl *ld;
6283
6284 if (!DECL_LANG_SPECIFIC (decl))
6285 return;
6286
703ad42b
KG
6287 ld = ggc_alloc (sizeof (struct lang_decl));
6288 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
8f17b5c5 6289 DECL_LANG_SPECIFIC (decl) = ld;
37105beb 6290}
1526a060 6291
f2c5f623
BC
6292/* The functions below are required for functionality of doing
6293 function at once processing in the C front end. Currently these
6294 functions are not called from anywhere in the C front end, but as
6645c3fa 6295 these changes continue, that will change. */
f2c5f623 6296
da7d8304 6297/* Returns nonzero if the current statement is a full expression,
f2c5f623
BC
6298 i.e. temporaries created during that statement should be destroyed
6299 at the end of the statement. */
6300
6301int
35b1a6fa 6302stmts_are_full_exprs_p (void)
f2c5f623
BC
6303{
6304 return 0;
6305}
6306
ae499cce
MM
6307/* Returns the stmt_tree (if any) to which statements are currently
6308 being added. If there is no active statement-tree, NULL is
6309 returned. */
6310
6311stmt_tree
35b1a6fa 6312current_stmt_tree (void)
ae499cce 6313{
8f17b5c5
MM
6314 return &c_stmt_tree;
6315}
6316
6317/* Returns the stack of SCOPE_STMTs for the current function. */
6318
6319tree *
35b1a6fa 6320current_scope_stmt_stack (void)
8f17b5c5
MM
6321{
6322 return &c_scope_stmt_stack;
ae499cce
MM
6323}
6324
f2c5f623 6325/* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6645c3fa 6326 C. */
f2c5f623 6327
6645c3fa 6328int
35b1a6fa 6329anon_aggr_type_p (tree node ATTRIBUTE_UNUSED)
f2c5f623
BC
6330{
6331 return 0;
6332}
6333
8f17b5c5 6334/* Dummy function in place of callback used by C++. */
f2c5f623 6335
8f17b5c5 6336void
35b1a6fa 6337extract_interface_info (void)
f2c5f623 6338{
f2c5f623
BC
6339}
6340
8f17b5c5
MM
6341/* Return a new COMPOUND_STMT, after adding it to the current
6342 statement tree. */
f2c5f623 6343
8f17b5c5 6344tree
35b1a6fa 6345c_begin_compound_stmt (void)
f2c5f623 6346{
8f17b5c5 6347 tree stmt;
6645c3fa 6348
8f17b5c5
MM
6349 /* Create the COMPOUND_STMT. */
6350 stmt = add_stmt (build_stmt (COMPOUND_STMT, NULL_TREE));
21c7361e 6351
8f17b5c5 6352 return stmt;
f2c5f623
BC
6353}
6354
8f17b5c5
MM
6355/* Expand T (a DECL_STMT) if it declares an entity not handled by the
6356 common code. */
f2c5f623
BC
6357
6358void
35b1a6fa 6359c_expand_decl_stmt (tree t)
0e5921e8 6360{
8f17b5c5 6361 tree decl = DECL_STMT_DECL (t);
21c7361e 6362
8f17b5c5
MM
6363 /* Expand nested functions. */
6364 if (TREE_CODE (decl) == FUNCTION_DECL
6365 && DECL_CONTEXT (decl) == current_function_decl
6366 && DECL_SAVED_TREE (decl))
e72fcfe8 6367 c_expand_body_1 (decl, 1);
0e5921e8 6368}
5fd8e536 6369
339a28b9 6370/* Return the global value of T as a symbol. */
5fd8e536
JM
6371
6372tree
35b1a6fa 6373identifier_global_value (tree t)
5fd8e536 6374{
339a28b9 6375 tree decl = IDENTIFIER_SYMBOL_VALUE (t);
4b1e44be 6376 if (decl == 0 || DECL_FILE_SCOPE_P (decl))
339a28b9
ZW
6377 return decl;
6378
6379 /* Shadowed by something else; find the true global value. */
f8521984 6380 for (decl = global_scope->names; decl; decl = TREE_CHAIN (decl))
339a28b9
ZW
6381 if (DECL_NAME (decl) == t)
6382 return decl;
6383
6384 /* Only local values for this decl. */
6385 return 0;
5fd8e536 6386}
eaa7c03f
JM
6387
6388/* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6389 otherwise the name is found in ridpointers from RID_INDEX. */
6390
6391void
35b1a6fa 6392record_builtin_type (enum rid rid_index, const char *name, tree type)
eaa7c03f
JM
6393{
6394 tree id;
21d13d83 6395 tree tdecl;
eaa7c03f
JM
6396 if (name == 0)
6397 id = ridpointers[(int) rid_index];
6398 else
6399 id = get_identifier (name);
21d13d83
ZW
6400 tdecl = build_decl (TYPE_DECL, id, type);
6401 pushdecl (tdecl);
6402 debug_hooks->type_decl (tdecl, 0);
eaa7c03f
JM
6403}
6404
6405/* Build the void_list_node (void_type_node having been created). */
6406tree
35b1a6fa 6407build_void_list_node (void)
eaa7c03f
JM
6408{
6409 tree t = build_tree_list (NULL_TREE, void_type_node);
6410 return t;
6411}
81a75f0f
NB
6412
6413/* Return something to represent absolute declarators containing a *.
6414 TARGET is the absolute declarator that the * contains.
6415 TYPE_QUALS_ATTRS is a list of modifiers such as const or volatile
6416 to apply to the pointer type, represented as identifiers, possible mixed
6417 with attributes.
6418
6419 We return an INDIRECT_REF whose "contents" are TARGET (inside a TREE_LIST,
6420 if attributes are present) and whose type is the modifier list. */
6421
6422tree
35b1a6fa 6423make_pointer_declarator (tree type_quals_attrs, tree target)
81a75f0f
NB
6424{
6425 tree quals, attrs;
6426 tree itarget = target;
6427 split_specs_attrs (type_quals_attrs, &quals, &attrs);
6428 if (attrs != NULL_TREE)
6429 itarget = tree_cons (attrs, target, NULL_TREE);
6430 return build1 (INDIRECT_REF, quals, itarget);
6431}
e2500fed 6432
48873ed2 6433/* A wrapper around lhd_set_decl_assembler_name that gives static
f8521984 6434 variables their C names if they are at file scope and only one
48873ed2 6435 translation unit is being compiled, for backwards compatibility
a98ebe2e 6436 with certain bizarre assembler hacks (like crtstuff.c). */
48873ed2
GK
6437
6438void
6439c_static_assembler_name (tree decl)
6440{
6441 if (num_in_fnames == 1
863d3dfb 6442 && !TREE_PUBLIC (decl) && DECL_CONTEXT (decl)
48873ed2
GK
6443 && TREE_CODE (DECL_CONTEXT (decl)) == TRANSLATION_UNIT_DECL)
6444 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
6445 else
6446 lhd_set_decl_assembler_name (decl);
6447}
6448
d1bd0ded
GK
6449/* Hash and equality functions for link_hash_table: key off
6450 DECL_ASSEMBLER_NAME. */
6451
6452static hashval_t
6453link_hash_hash (const void *x_p)
6454{
6455 tree x = (tree)x_p;
9d4a30f2 6456 return (hashval_t) (long)DECL_ASSEMBLER_NAME (x);
d1bd0ded
GK
6457}
6458
6459static int
6460link_hash_eq (const void *x1_p, const void *x2_p)
6461{
6462 tree x1 = (tree)x1_p;
6463 tree x2 = (tree)x2_p;
6464 return DECL_ASSEMBLER_NAME (x1) == DECL_ASSEMBLER_NAME (x2);
6465}
6466
6467/* Propagate information between definitions and uses between multiple
6468 translation units in TU_LIST based on linkage rules. */
6469
6470void
6471merge_translation_unit_decls (void)
6472{
6473 const tree tu_list = current_file_decl;
6474 tree tu;
6475 tree decl;
6476 htab_t link_hash_table;
6477 tree block;
e13e48e7 6478
d1bd0ded
GK
6479 /* Create the BLOCK that poplevel would have created, but don't
6480 actually call poplevel since that's expensive. */
6481 block = make_node (BLOCK);
f8521984 6482 BLOCK_VARS (block) = current_scope->names;
d1bd0ded
GK
6483 TREE_USED (block) = 1;
6484 DECL_INITIAL (current_file_decl) = block;
6485
6486 /* If only one translation unit seen, no copying necessary. */
6487 if (TREE_CHAIN (tu_list) == NULL_TREE)
6488 return;
6489
6490 link_hash_table = htab_create (1021, link_hash_hash, link_hash_eq, NULL);
6491
6492 /* Enter any actual definitions into the hash table. */
6493 for (tu = tu_list; tu; tu = TREE_CHAIN (tu))
6494 for (decl = BLOCK_VARS (DECL_INITIAL (tu)); decl; decl = TREE_CHAIN (decl))
6495 if (TREE_PUBLIC (decl) && ! DECL_EXTERNAL (decl))
6496 {
6497 PTR *slot;
6498 slot = htab_find_slot (link_hash_table, decl, INSERT);
6499
6500 /* If we've already got a definition, work out which one is
6501 the real one, put it into the hash table, and make the
6502 other one DECL_EXTERNAL. This is important to avoid
6503 putting out two definitions of the same symbol in the
6504 assembly output. */
6505 if (*slot != NULL)
6506 {
6507 tree old_decl = (tree) *slot;
6508
6509 /* If this is weak or common or whatever, suppress it
02fa63cd 6510 in favor of the other definition. */
d1bd0ded
GK
6511 if (DECL_WEAK (decl))
6512 DECL_EXTERNAL (decl) = 1;
6513 else if (DECL_WEAK (old_decl) && ! DECL_WEAK (decl))
6514 DECL_EXTERNAL (old_decl) = 1;
6515 else if (DECL_COMMON (decl) || DECL_ONE_ONLY (decl))
6516 DECL_EXTERNAL (decl) = 1;
6517 else if (DECL_COMMON (old_decl) || DECL_ONE_ONLY (old_decl))
6518 DECL_EXTERNAL (old_decl) = 1;
e13e48e7 6519
d1bd0ded
GK
6520 if (DECL_EXTERNAL (decl))
6521 {
6522 DECL_INITIAL (decl) = NULL_TREE;
6523 DECL_COMMON (decl) = 0;
6524 DECL_ONE_ONLY (decl) = 0;
6525 DECL_WEAK (decl) = 0;
6526 }
6527 else if (DECL_EXTERNAL (old_decl))
6528 {
6529 DECL_INITIAL (old_decl) = NULL_TREE;
6530 DECL_COMMON (old_decl) = 0;
6531 DECL_ONE_ONLY (old_decl) = 0;
6532 DECL_WEAK (old_decl) = 0;
6533 *slot = decl;
6534 }
6535 else
6536 {
ddd2d57e
RH
6537 error ("%Jredefinition of global '%D'", decl, decl);
6538 error ("%J'%D' previously defined here", old_decl, old_decl);
d1bd0ded
GK
6539 }
6540 }
6541 else
6542 *slot = decl;
6543 }
6544
6545 /* Now insert the desired information from all the definitions
6546 into any plain declarations. */
6547 for (tu = tu_list; tu; tu = TREE_CHAIN (tu))
6548 for (decl = BLOCK_VARS (DECL_INITIAL (tu)); decl; decl = TREE_CHAIN (decl))
6549 if (TREE_PUBLIC (decl) && DECL_EXTERNAL (decl))
6550 {
6551 tree global_decl;
703ad42b 6552 global_decl = htab_find (link_hash_table, decl);
e13e48e7 6553
d1bd0ded
GK
6554 if (! global_decl)
6555 continue;
e13e48e7 6556
d1bd0ded
GK
6557 /* Print any appropriate error messages, and partially merge
6558 the decls. */
1ef82ef2 6559 (void) duplicate_decls (decl, global_decl);
d1bd0ded
GK
6560 }
6561
6562 htab_delete (link_hash_table);
6563}
6564
6565/* Perform final processing on file-scope data. */
6566
6567void
6568c_write_global_declarations(void)
6569{
6570 tree link;
e13e48e7 6571
d1bd0ded
GK
6572 for (link = current_file_decl; link; link = TREE_CHAIN (link))
6573 {
6574 tree globals = BLOCK_VARS (DECL_INITIAL (link));
6575 int len = list_length (globals);
703ad42b 6576 tree *vec = xmalloc (sizeof (tree) * len);
d1bd0ded
GK
6577 int i;
6578 tree decl;
e13e48e7 6579
f91f41b2
ZW
6580 /* Process the decls in the order they were written. */
6581
d1bd0ded 6582 for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
f91f41b2 6583 vec[i] = decl;
e13e48e7 6584
d1bd0ded 6585 wrapup_global_declarations (vec, len);
e13e48e7 6586
d1bd0ded 6587 check_global_declarations (vec, len);
e13e48e7 6588
d1bd0ded
GK
6589 /* Clean up. */
6590 free (vec);
6591 }
6592}
6593
6594/* Reset the parser's state in preparation for a new file. */
6595
6596void
6597c_reset_state (void)
6598{
6599 tree link;
6600 tree file_scope_decl;
e13e48e7 6601
f8521984
ZW
6602 /* Pop the global scope. */
6603 if (current_scope != global_scope)
6604 current_scope = global_scope;
d1bd0ded
GK
6605 file_scope_decl = current_file_decl;
6606 DECL_INITIAL (file_scope_decl) = poplevel (1, 0, 0);
766beae1 6607 BLOCK_SUPERCONTEXT (DECL_INITIAL (file_scope_decl)) = file_scope_decl;
d1bd0ded
GK
6608 truly_local_externals = NULL_TREE;
6609
6610 /* Start a new global binding level. */
6611 pushlevel (0);
f8521984 6612 global_scope = current_scope;
d1bd0ded
GK
6613 current_file_decl = build_decl (TRANSLATION_UNIT_DECL, NULL, NULL);
6614 TREE_CHAIN (current_file_decl) = file_scope_decl;
6615
fdc49e10
ZW
6616 /* Reintroduce the builtin declarations. */
6617 for (link = first_builtin_decl;
6618 link != TREE_CHAIN (last_builtin_decl);
6619 link = TREE_CHAIN (link))
d1bd0ded
GK
6620 pushdecl (copy_node (link));
6621}
6622
e2500fed 6623#include "gt-c-decl.h"