Deleted Added
full compact
c-decl.c (149846) c-decl.c (161660)
1/* Process declarations and variables for C compiler.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
4
5This file is part of GCC.
6
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.
11
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.
16
17You should have received a copy of the GNU General Public License
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. */
21
1/* Process declarations and variables for C compiler.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
4
5This file is part of GCC.
6
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.
11
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.
16
17You should have received a copy of the GNU General Public License
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. */
21
22/* $FreeBSD: head/contrib/gcc/c-decl.c 149846 2005-09-07 09:23:39Z obrien $ */
22/* $FreeBSD: head/contrib/gcc/c-decl.c 161660 2006-08-26 21:37:21Z kan $ */
23
24/* Process declarations and symbol lookup for C front end.
25 Also constructs types; the standard scalar types at initialization,
26 and structure, union, array and enum types when they are declared. */
27
28/* ??? not all decl nodes are given the most useful possible
29 line numbers. For example, the CONST_DECLs for enum values. */
30
31#include "config.h"
32#include "system.h"
33#include "coretypes.h"
34#include "tm.h"
35#include "intl.h"
36#include "tree.h"
37#include "tree-inline.h"
38#include "rtl.h"
39#include "flags.h"
40#include "function.h"
41#include "output.h"
42#include "expr.h"
43#include "c-tree.h"
44#include "toplev.h"
45#include "ggc.h"
46#include "tm_p.h"
47#include "cpplib.h"
48#include "target.h"
49#include "debug.h"
50#include "opts.h"
51#include "timevar.h"
52#include "c-common.h"
53#include "c-pragma.h"
54#include "cgraph.h"
55#include "hashtab.h"
56#include "libfuncs.h"
57#include "except.h"
58#include "langhooks-def.h"
59
60/* In grokdeclarator, distinguish syntactic contexts of declarators. */
61enum decl_context
62{ NORMAL, /* Ordinary declaration */
63 FUNCDEF, /* Function definition */
64 PARM, /* Declaration of parm before function body */
65 FIELD, /* Declaration inside struct or union */
66 TYPENAME}; /* Typename (inside cast or sizeof) */
67
68
69/* Nonzero if we have seen an invalid cross reference
70 to a struct, union, or enum, but not yet printed the message. */
71
72tree pending_invalid_xref;
73/* File and line to appear in the eventual error message. */
74location_t pending_invalid_xref_location;
75
76/* While defining an enum type, this is 1 plus the last enumerator
77 constant value. Note that will do not have to save this or `enum_overflow'
78 around nested function definition since such a definition could only
79 occur in an enum value expression and we don't use these variables in
80 that case. */
81
82static tree enum_next_value;
83
84/* Nonzero means that there was overflow computing enum_next_value. */
85
86static int enum_overflow;
87
88/* Parsing a function declarator leaves a list of parameter names
89 or a chain of parameter decls here. */
90
91static tree last_function_parms;
92
93/* ... and a chain of structure and enum types declared in the
94 parmlist here. */
95
96static tree last_function_parm_tags;
97
98/* ... and a chain of all non-parameter declarations (such as
99 CONST_DECLs from enumerations) here. */
100
101static tree last_function_parm_others;
102
103/* After parsing the declarator that starts a function definition,
104 `start_function' puts the list of parameter names or chain of decls here
105 for `store_parm_decls' to find. */
106
107static tree current_function_parms;
108
109/* Similar, for last_function_parm_tags. */
110
111static tree current_function_parm_tags;
112
113/* And for last_function_parm_others. */
114
115static tree current_function_parm_others;
116
117/* Similar, for the file and line that the prototype came from if this is
118 an old-style definition. */
119
120static location_t current_function_prototype_locus;
121
122/* The current statement tree. */
123
124static GTY(()) struct stmt_tree_s c_stmt_tree;
125
126/* The current scope statement stack. */
127
128static GTY(()) tree c_scope_stmt_stack;
129
130/* State saving variables. */
131int c_in_iteration_stmt;
132int c_in_case_stmt;
133
134/* A list of external DECLs that appeared at block scope when there was
135 some other global meaning for that identifier. */
136static GTY(()) tree truly_local_externals;
137
138/* All the builtins; this is a subset of the entries of global_scope. */
139
140static GTY(()) tree first_builtin_decl;
141static GTY(()) tree last_builtin_decl;
142
143/* A DECL for the current file-scope context. */
144
145static GTY(()) tree current_file_decl;
146
147/* Set to 0 at beginning of a function definition, set to 1 if
148 a return statement that specifies a return value is seen. */
149
150int current_function_returns_value;
151
152/* Set to 0 at beginning of a function definition, set to 1 if
153 a return statement with no argument is seen. */
154
155int current_function_returns_null;
156
157/* Set to 0 at beginning of a function definition, set to 1 if
158 a call to a noreturn function is seen. */
159
160int current_function_returns_abnormally;
161
162/* Set to nonzero by `grokdeclarator' for a function
163 whose return type is defaulted, if warnings for this are desired. */
164
165static int warn_about_return_type;
166
167/* Nonzero when starting a function declared `extern inline'. */
168
169static int current_extern_inline;
170
171/* Each c_scope structure describes the complete contents of one scope.
172 Three scopes are distinguished specially: the innermost or current
173 scope, the innermost function scope, and the outermost or file scope.
174
175 Most declarations are recorded in the current scope.
176
177 All normal label declarations are recorded in the innermost
178 function scope, as are bindings of undeclared identifiers to
179 error_mark_node. (GCC permits nested functions as an extension,
180 hence the 'innermost' qualifier.) Explicitly declared labels
181 (using the __label__ extension) appear in the current scope.
182
183 Being in the global scope (current_scope == global_scope) causes
184 special behavior in several places below. Also, under some
185 conditions the Objective-C front end records declarations in the
186 global scope even though that isn't the current scope.
187
188 The order of the names, parms, and blocks lists matters, and they
189 are frequently appended to. To avoid having to walk all the way to
190 the end of the list on each insertion, or reverse the lists later,
191 we maintain a pointer to the last list entry for each of the lists.
192
193 The order of the tags, shadowed, and shadowed_tags
194 lists does not matter, so we just prepend to these lists. */
195
196struct c_scope GTY(())
197{
198 /* The scope containing this one. */
199 struct c_scope *outer;
200
201 /* The next outermost function scope. */
202 struct c_scope *outer_function;
203
204 /* All variables, constants, functions, labels, and typedef names. */
205 tree names;
206 tree names_last;
207
208 /* All parameter declarations. Used only in the outermost scope of
209 a function. */
210 tree parms;
211 tree parms_last;
212
213 /* All structure, union, and enum type tags. */
214 tree tags;
215
216 /* For each scope, a list of shadowed outer-scope definitions
217 to be restored when this scope is popped.
218 Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
219 whose TREE_VALUE is its old definition (a kind of ..._DECL node). */
220 tree shadowed;
221
222 /* For each scope, a list of shadowed outer-scope tag definitions
223 to be restored when this scope is popped.
224 Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
225 whose TREE_VALUE is its old definition (a kind of ..._TYPE node). */
226 tree shadowed_tags;
227
228 /* For each scope (except the global one), a chain of BLOCK nodes
229 for all the scopes that were entered and exited one level down. */
230 tree blocks;
231 tree blocks_last;
232
233 /* True if we are currently filling this scope with parameter
234 declarations. */
235 BOOL_BITFIELD parm_flag : 1;
236
237 /* True if we already complained about forward parameter decls
238 in this scope. This prevents double warnings on
239 foo (int a; int b; ...) */
240 BOOL_BITFIELD warned_forward_parm_decls : 1;
241
242 /* True if this is the outermost block scope of a function body.
243 This scope contains the parameters, the local variables declared
244 in the outermost block, and all the labels (except those in
245 nested functions, or declared at block scope with __label__). */
246 BOOL_BITFIELD function_body : 1;
247
248 /* True means make a BLOCK for this scope no matter what. */
249 BOOL_BITFIELD keep : 1;
250};
251
252/* The scope currently in effect. */
253
254static GTY(()) struct c_scope *current_scope;
255
256/* A chain of c_scope structures awaiting reuse. */
257
258static GTY((deletable (""))) struct c_scope *scope_freelist;
259
260/* The innermost function scope. Ordinary (not explicitly declared)
261 labels, bindings to error_mark_node, and the lazily-created
262 bindings of __func__ and its friends get this scope. */
263
264static GTY(()) struct c_scope *current_function_scope;
265
266/* The outermost scope, corresponding to the C "file scope". This is
267 created when the compiler is started and exists through the entire run. */
268
269static GTY(()) struct c_scope *global_scope;
270
271/* Append VAR to LIST in scope SCOPE. */
272#define SCOPE_LIST_APPEND(scope, list, decl) do { \
273 struct c_scope *s_ = (scope); \
274 tree d_ = (decl); \
275 if (s_->list##_last) \
276 TREE_CHAIN (s_->list##_last) = d_; \
277 else \
278 s_->list = d_; \
279 s_->list##_last = d_; \
280} while (0)
281
282/* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
283#define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
284 struct c_scope *t_ = (tscope); \
285 struct c_scope *f_ = (fscope); \
286 if (t_->to##_last) \
287 TREE_CHAIN (t_->to##_last) = f_->from; \
288 else \
289 t_->to = f_->from; \
290 t_->to##_last = f_->from##_last; \
291} while (0)
292
293/* True means unconditionally make a BLOCK for the next scope pushed. */
294
295static bool keep_next_level_flag;
296
297/* True means the next call to pushlevel will be the outermost scope
298 of a function body, so do not push a new scope, merely cease
299 expecting parameter decls. */
300
301static bool next_is_function_body;
302
303/* Functions called automatically at the beginning and end of execution. */
304
305tree static_ctors, static_dtors;
306
307/* Forward declarations. */
308
309static struct c_scope *make_scope (void);
310static void pop_scope (void);
311static tree make_label (tree, location_t);
312static void bind_label (tree, tree, struct c_scope *);
313static void implicit_decl_warning (tree);
314static tree lookup_tag (enum tree_code, tree, int);
315static tree lookup_name_current_level (tree);
316static tree grokdeclarator (tree, tree, enum decl_context, int, tree *);
317static tree grokparms (tree, int);
318static void layout_array_type (tree);
319static void store_parm_decls_newstyle (void);
320static void store_parm_decls_oldstyle (void);
321static tree c_make_fname_decl (tree, int);
322static void c_expand_body_1 (tree, int);
323static tree any_external_decl (tree);
324static void record_external_decl (tree);
325static void warn_if_shadowing (tree, tree);
326static void check_bitfield_type_and_width (tree *, tree *, const char *);
327static void clone_underlying_type (tree);
328static bool flexible_array_type_p (tree);
329static hashval_t link_hash_hash (const void *);
330static int link_hash_eq (const void *, const void *);
331
332/* States indicating how grokdeclarator() should handle declspecs marked
333 with __attribute__((deprecated)). An object declared as
334 __attribute__((deprecated)) suppresses warnings of uses of other
335 deprecated items. */
336
337enum deprecated_states {
338 DEPRECATED_NORMAL,
339 DEPRECATED_SUPPRESS
340};
341
342static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
343
344void
345c_print_identifier (FILE *file, tree node, int indent)
346{
347 print_node (file, "symbol", IDENTIFIER_SYMBOL_VALUE (node), indent + 4);
348 print_node (file, "tag", IDENTIFIER_TAG_VALUE (node), indent + 4);
349 print_node (file, "label", IDENTIFIER_LABEL_VALUE (node), indent + 4);
350 if (C_IS_RESERVED_WORD (node))
351 {
352 tree rid = ridpointers[C_RID_CODE (node)];
353 indent_to (file, indent + 4);
354 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
355 (void *) rid, IDENTIFIER_POINTER (rid));
356 }
357}
358
359/* Hook called at end of compilation to assume 1 elt
360 for a file-scope tentative array defn that wasn't complete before. */
361
362void
363c_finish_incomplete_decl (tree decl)
364{
365 if (TREE_CODE (decl) == VAR_DECL)
366 {
367 tree type = TREE_TYPE (decl);
368 if (type != error_mark_node
369 && TREE_CODE (type) == ARRAY_TYPE
370 && ! DECL_EXTERNAL (decl)
371 && TYPE_DOMAIN (type) == 0)
372 {
373 warning ("%Jarray '%D' assumed to have one element", decl, decl);
374
375 complete_array_type (type, NULL_TREE, 1);
376
377 layout_decl (decl, 0);
378 }
379 }
380}
381
382/* Reuse or create a struct for this scope. */
383
384static struct c_scope *
385make_scope (void)
386{
387 struct c_scope *result;
388 if (scope_freelist)
389 {
390 result = scope_freelist;
391 scope_freelist = result->outer;
392 }
393 else
394 result = ggc_alloc_cleared (sizeof (struct c_scope));
395
396 return result;
397}
398
399/* Remove the topmost scope from the stack and add it to the
400 free list, updating current_function_scope if necessary. */
401
402static void
403pop_scope (void)
404{
405 struct c_scope *scope = current_scope;
406
407 current_scope = scope->outer;
408 if (scope->function_body)
409 current_function_scope = scope->outer_function;
410
411 memset (scope, 0, sizeof (struct c_scope));
412 scope->outer = scope_freelist;
413 scope_freelist = scope;
414}
415
416/* The Objective-C front-end often needs to determine the current scope. */
417
418void *
419get_current_scope (void)
420{
421 return current_scope;
422}
423
424/* The following function is used only by Objective-C. It needs to live here
425 because it accesses the innards of c_scope. */
426
427void
428objc_mark_locals_volatile (void *enclosing_blk)
429{
430 struct c_scope *scope;
431
432 for (scope = current_scope;
433 scope && scope != enclosing_blk;
434 scope = scope->outer)
435 {
436 tree decl;
437
438 for (decl = scope->names; decl; decl = TREE_CHAIN (decl))
439 {
440 DECL_REGISTER (decl) = 0;
441 TREE_THIS_VOLATILE (decl) = 1;
442 }
443 /* Do not climb up past the current function. */
444 if (scope->function_body)
445 break;
446 }
447}
448
449/* Nonzero if we are currently in the global scope. */
450
451int
452global_bindings_p (void)
453{
454 return current_scope == global_scope;
455}
456
457void
458keep_next_level (void)
459{
460 keep_next_level_flag = true;
461}
462
463/* Identify this scope as currently being filled with parameters. */
464
465void
466declare_parm_level (void)
467{
468 current_scope->parm_flag = true;
469}
470
471/* Nonzero if currently making parm declarations. */
472
473int
474in_parm_level_p (void)
475{
476 return current_scope->parm_flag;
477}
478
479/* Enter a new scope. The dummy parameter is for signature
480 compatibility with lang_hooks.decls.pushlevel. */
481
482void
483pushlevel (int dummy ATTRIBUTE_UNUSED)
484{
485 if (next_is_function_body)
486 {
487 /* This is the transition from the parameters to the top level
488 of the function body. These are the same scope
489 (C99 6.2.1p4,6) so we do not push another scope structure.
490 next_is_function_body is set only by store_parm_decls, which
491 in turn is called when and only when we are about to
492 encounter the opening curly brace for the function body.
493
494 The outermost block of a function always gets a BLOCK node,
495 because the debugging output routines expect that each
496 function has at least one BLOCK. */
497 current_scope->parm_flag = false;
498 current_scope->function_body = true;
499 current_scope->keep = true;
500 current_scope->outer_function = current_function_scope;
501 current_function_scope = current_scope;
502
503 keep_next_level_flag = false;
504 next_is_function_body = false;
505 }
506 else
507 {
508 struct c_scope *scope = make_scope ();
509
510 scope->keep = keep_next_level_flag;
511 scope->outer = current_scope;
512 current_scope = scope;
513 keep_next_level_flag = false;
514 }
515}
516
517/* Exit a scope. Restore the state of the identifier-decl mappings
518 that were in effect when this scope was entered.
519
520 If KEEP is KEEP_YES (1), this scope had explicit declarations, so
521 create a BLOCK node to record its declarations and subblocks for
522 debugging output. If KEEP is KEEP_MAYBE, do so only if the names
523 or tags lists are nonempty.
524
525 The second parameter is ignored; it is present only for
526 signature compatibility with lang_hooks.decls.poplevel.
527
528 If FUNCTIONBODY is nonzero, this level is the body of a function,
529 even if current_scope->function_body is not set. This is used
530 by language-independent code that generates synthetic functions,
531 and cannot set current_scope->function_body.
532
533 FIXME: Eliminate the need for all arguments. */
534
535tree
536poplevel (int keep, int dummy ATTRIBUTE_UNUSED, int functionbody)
537{
538 struct c_scope *scope = current_scope;
539 tree block;
540 tree decl;
541 tree p;
542
543 /* The following line does not use |= due to a bug in HP's C compiler. */
544 scope->function_body = scope->function_body | functionbody;
545
546 if (keep == KEEP_MAYBE)
547 keep = (scope->names || scope->tags);
548
549 keep |= scope->keep;
550 keep |= scope->function_body;
551
552 /* If appropriate, create a BLOCK to record the decls for the life
553 of this function. */
554 block = 0;
555 if (keep)
556 {
557 block = make_node (BLOCK);
558 BLOCK_VARS (block) = scope->names;
559 BLOCK_SUBBLOCKS (block) = scope->blocks;
560 TREE_USED (block) = 1;
561 }
562
563 /* In each subblock, record that this is its superior. */
564 for (p = scope->blocks; p; p = TREE_CHAIN (p))
565 BLOCK_SUPERCONTEXT (p) = block;
566
567 /* Clear out the variable bindings in this scope.
568
569 Propagate TREE_ADDRESSABLE from nested functions to their
570 containing functions.
571
572 Issue warnings for unused variables and labels, and errors for
573 undefined labels, if there are any. */
574
575 for (p = scope->names; p; p = TREE_CHAIN (p))
576 {
577 switch (TREE_CODE (p))
578 {
579 case LABEL_DECL:
580 if (TREE_USED (p) && !DECL_INITIAL (p))
581 {
582 error ("%Jlabel `%D' used but not defined", p, p);
583 DECL_INITIAL (p) = error_mark_node;
584 }
585 else if (!TREE_USED (p) && warn_unused_label)
586 {
587 if (DECL_INITIAL (p))
588 warning ("%Jlabel `%D' defined but not used", p, p);
589 else
590 warning ("%Jlabel `%D' declared but not defined", p, p);
591 }
592
593 IDENTIFIER_LABEL_VALUE (DECL_NAME (p)) = 0;
594 break;
595
596 case FUNCTION_DECL:
597 if (! TREE_ASM_WRITTEN (p)
598 && DECL_INITIAL (p) != 0
599 && TREE_ADDRESSABLE (p)
600 && DECL_ABSTRACT_ORIGIN (p) != 0
601 && DECL_ABSTRACT_ORIGIN (p) != p)
602 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
603 goto normal;
604
605 case VAR_DECL:
606 /* Keep this in sync with stmt.c:warn_about_unused_variables.
607 No warnings when the global scope is popped because the
608 global scope isn't popped for the last translation unit,
609 so the warnings are done in c_write_global_declaration. */
610 if (warn_unused_variable && scope != global_scope
611 && !TREE_USED (p)
612 && !DECL_IN_SYSTEM_HEADER (p)
613 && DECL_NAME (p)
614 && !DECL_ARTIFICIAL (p))
615 warning ("%Junused variable `%D'", p, p);
616 /* fall through */
617
618 default:
619 normal:
620 if (DECL_NAME (p))
621 {
622 if (DECL_EXTERNAL (p) && scope != global_scope)
623 /* External decls stay in the symbol-value slot but are
624 inaccessible. */
625 C_DECL_INVISIBLE (p) = 1;
626 else
627 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (p)) = 0;
628 }
629 break;
630 }
631 }
632
633 /* Clear out the parameter bindings in this scope, if any.
634 Unused-parameter warnings are handled by function.c. */
635 for (p = scope->parms; p; p = TREE_CHAIN (p))
636 if (DECL_NAME (p))
637 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (p)) = 0;
638
639 /* Clear out the tag-meanings declared in this scope.
640
641 Set the TYPE_CONTEXTs for all of the tagged types belonging to
642 this scope so that they point to the appropriate construct, i.e.
643 either to the current FUNCTION_DECL node, or else to the BLOCK
644 node we just constructed.
645
646 Note that for tagged types whose scope is just the formal
647 parameter list for some function type specification, we can't
648 properly set their TYPE_CONTEXTs here, because we don't have a
649 pointer to the appropriate FUNCTION_TYPE node readily available
650 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
651 type nodes get set in `grokdeclarator' as soon as we have created
652 the FUNCTION_TYPE node which will represent the "scope" for these
653 "parameter list local" tagged types. */
654
655 decl = scope->function_body ? current_function_decl : block;
656 for (p = scope->tags; p; p = TREE_CHAIN (p))
657 {
658 if (TREE_PURPOSE (p))
659 IDENTIFIER_TAG_VALUE (TREE_PURPOSE (p)) = 0;
660 if (decl)
661 TYPE_CONTEXT (TREE_VALUE (p)) = decl;
662 }
663
664 /* Restore all name- and label-meanings from outer scopes that were
665 shadowed by this scope. */
666 for (p = scope->shadowed; p; p = TREE_CHAIN (p))
667 if (TREE_VALUE (p) && TREE_CODE (TREE_VALUE (p)) == LABEL_DECL)
668 IDENTIFIER_LABEL_VALUE (TREE_PURPOSE (p)) = TREE_VALUE (p);
669 else
670 IDENTIFIER_SYMBOL_VALUE (TREE_PURPOSE (p)) = TREE_VALUE (p);
671
672 /* Restore all tag-meanings from outer scopes that were shadowed by
673 this scope. */
674 for (p = scope->shadowed_tags; p; p = TREE_CHAIN (p))
675 IDENTIFIER_TAG_VALUE (TREE_PURPOSE (p)) = TREE_VALUE (p);
676
677 /* Dispose of the block that we just made inside some higher level. */
678 if (scope->function_body && current_function_decl)
679 DECL_INITIAL (current_function_decl) = block;
680 else if (scope->outer)
681 {
682 if (block)
683 SCOPE_LIST_APPEND (scope->outer, blocks, block);
684 /* If we did not make a block for the scope just exited, any
685 blocks made for inner scopes must be carried forward so they
686 will later become subblocks of something else. */
687 else if (scope->blocks)
688 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
689 }
690
691 /* Pop the current scope, and free the structure for reuse. */
692 pop_scope ();
693
694 return block;
695}
696
697/* Insert BLOCK at the end of the list of subblocks of the current
698 scope. This is used when a BIND_EXPR is expanded, to handle the
699 BLOCK node inside the BIND_EXPR. */
700
701void
702insert_block (tree block)
703{
704 TREE_USED (block) = 1;
705 SCOPE_LIST_APPEND (current_scope, blocks, block);
706}
707
708/* Set the BLOCK node for the innermost scope (the one we are
709 currently in). The RTL expansion machinery requires us to provide
710 this hook, but it is not useful in function-at-a-time mode. */
711
712void
713set_block (tree block ATTRIBUTE_UNUSED)
714{
715}
716
717/* Push a definition or a declaration of struct, union or enum tag "name".
718 "type" should be the type node.
719 We assume that the tag "name" is not already defined.
720
721 Note that the definition may really be just a forward reference.
722 In that case, the TYPE_SIZE will be zero. */
723
724void
725pushtag (tree name, tree type)
726{
727 struct c_scope *b = current_scope;
728
729 /* Record the identifier as the type's name if it has none. */
730 if (name)
731 {
732 if (TYPE_NAME (type) == 0)
733 TYPE_NAME (type) = name;
734
735 if (IDENTIFIER_TAG_VALUE (name))
736 b->shadowed_tags = tree_cons (name, IDENTIFIER_TAG_VALUE (name),
737 b->shadowed_tags);
738 IDENTIFIER_TAG_VALUE (name) = type;
739 }
740
741 b->tags = tree_cons (name, type, b->tags);
742
743 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
744 tagged type we just added to the current scope. This fake
745 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
746 to output a representation of a tagged type, and it also gives
747 us a convenient place to record the "scope start" address for the
748 tagged type. */
749
750 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
751
752 /* An approximation for now, so we can tell this is a function-scope tag.
753 This will be updated in poplevel. */
754 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
755}
756
757/* Subroutine of compare_decls. Allow harmless mismatches in return
758 and argument types provided that the type modes match. This function
759 return a unified type given a suitable match, and 0 otherwise. */
760
761static tree
762match_builtin_function_types (tree newtype, tree oldtype)
763{
764 tree newrettype, oldrettype;
765 tree newargs, oldargs;
766 tree trytype, tryargs;
767
768 /* Accept the return type of the new declaration if same modes. */
769 oldrettype = TREE_TYPE (oldtype);
770 newrettype = TREE_TYPE (newtype);
771
772 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
773 return 0;
774
775 oldargs = TYPE_ARG_TYPES (oldtype);
776 newargs = TYPE_ARG_TYPES (newtype);
777 tryargs = newargs;
778
779 while (oldargs || newargs)
780 {
781 if (! oldargs
782 || ! newargs
783 || ! TREE_VALUE (oldargs)
784 || ! TREE_VALUE (newargs)
785 || TYPE_MODE (TREE_VALUE (oldargs))
786 != TYPE_MODE (TREE_VALUE (newargs)))
787 return 0;
788
789 oldargs = TREE_CHAIN (oldargs);
790 newargs = TREE_CHAIN (newargs);
791 }
792
793 trytype = build_function_type (newrettype, tryargs);
794 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
795}
796
797/* Subroutine of diagnose_mismathed_decls. Check for function type
798 mismatch involving an empty arglist vs a nonempty one and give clearer
799 diagnostics. */
800static void
801diagnose_arglist_conflict (tree newdecl, tree olddecl,
802 tree newtype, tree oldtype)
803{
804 tree t;
805
806 if (TREE_CODE (olddecl) != FUNCTION_DECL
807 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype), COMPARE_STRICT)
808 || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
809 ||
810 (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
811 return;
812
813 t = TYPE_ARG_TYPES (oldtype);
814 if (t == 0)
815 t = TYPE_ARG_TYPES (newtype);
816 for (; t; t = TREE_CHAIN (t))
817 {
818 tree type = TREE_VALUE (t);
819
820 if (TREE_CHAIN (t) == 0
821 && TYPE_MAIN_VARIANT (type) != void_type_node)
822 {
823 inform ("a parameter list with an ellipsis can't match "
824 "an empty parameter name list declaration");
825 break;
826 }
827
828 if (c_type_promotes_to (type) != type)
829 {
830 inform ("an argument type that has a default promotion can't match "
831 "an empty parameter name list declaration");
832 break;
833 }
834 }
835}
836
837/* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
838 old-style function definition, NEWDECL is a prototype declaration.
839 Diagnose inconsistencies in the argument list. Returns TRUE if
840 the prototype is compatible, FALSE if not. */
841static bool
842validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
843{
844 tree newargs, oldargs;
845 int i;
846
847 /* ??? Elsewhere TYPE_MAIN_VARIANT is not used in this context. */
848#define END_OF_ARGLIST(t) (TYPE_MAIN_VARIANT (t) == void_type_node)
849
850 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
851 newargs = TYPE_ARG_TYPES (newtype);
852 i = 1;
853
854 for (;;)
855 {
856 tree oldargtype = TREE_VALUE (oldargs);
857 tree newargtype = TREE_VALUE (newargs);
858
859 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
860 break;
861
862 /* Reaching the end of just one list means the two decls don't
863 agree on the number of arguments. */
864 if (END_OF_ARGLIST (oldargtype))
865 {
866 error ("%Jprototype for '%D' declares more arguments "
867 "than previous old-style definition", newdecl, newdecl);
868 return false;
869 }
870 else if (END_OF_ARGLIST (newargtype))
871 {
872 error ("%Jprototype for '%D' declares fewer arguments "
873 "than previous old-style definition", newdecl, newdecl);
874 return false;
875 }
876
877 /* Type for passing arg must be consistent with that declared
878 for the arg. */
879 else if (! comptypes (oldargtype, newargtype, COMPARE_STRICT))
880 {
881 error ("%Jprototype for '%D' declares arg %d with incompatible type",
882 newdecl, newdecl, i);
883 return false;
884 }
885
886 oldargs = TREE_CHAIN (oldargs);
887 newargs = TREE_CHAIN (newargs);
888 i++;
889 }
890
891 /* If we get here, no errors were found, but do issue a warning
892 for this poor-style construct. */
893 warning ("%Jprototype for '%D' follows non-prototype definition",
894 newdecl, newdecl);
895 return true;
896#undef END_OF_ARGLIST
897}
898
899/* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
900 first in a pair of mismatched declarations, using the diagnostic
901 function DIAG. */
902static void
903locate_old_decl (tree decl, void (*diag)(const char *, ...))
904{
905 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
906 ;
907 else if (DECL_INITIAL (decl))
908 diag (N_("%Jprevious definition of '%D' was here"), decl, decl);
909 else if (C_DECL_IMPLICIT (decl))
910 diag (N_("%Jprevious implicit declaration of '%D' was here"), decl, decl);
911 else
912 diag (N_("%Jprevious declaration of '%D' was here"), decl, decl);
913}
914
915/* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
916 Returns true if the caller should proceed to merge the two, false
917 if OLDDECL should simply be discarded. As a side effect, issues
918 all necessary diagnostics for invalid or poor-style combinations.
919 If it returns true, writes the types of NEWDECL and OLDDECL to
920 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
921 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
922
923static bool
924diagnose_mismatched_decls (tree newdecl, tree olddecl,
925 tree *newtypep, tree *oldtypep)
926{
927 tree newtype, oldtype;
928 bool pedwarned = false;
929 bool warned = false;
930
931 /* If we have error_mark_node for either decl or type, just discard
932 the previous decl - we're in an error cascade already. */
933 if (olddecl == error_mark_node || newdecl == error_mark_node)
934 return false;
935 *oldtypep = oldtype = TREE_TYPE (olddecl);
936 *newtypep = newtype = TREE_TYPE (newdecl);
937 if (oldtype == error_mark_node || newtype == error_mark_node)
938 return false;
939
940 /* Two different categories of symbol altogether. This is an error
941 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
942 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
943 {
944 if (TREE_CODE (olddecl) != FUNCTION_DECL
945 || !DECL_BUILT_IN (olddecl) || !C_DECL_INVISIBLE (olddecl))
946 {
947 error ("%J'%D' redeclared as different kind of symbol",
948 newdecl, newdecl);
949 locate_old_decl (olddecl, error);
950 }
951 else if (TREE_PUBLIC (newdecl))
952 warning ("%Jbuilt-in function '%D' declared as non-function",
953 newdecl, newdecl);
954 else if (warn_shadow)
955 warning ("%Jshadowing built-in function '%D'",
956 newdecl, newdecl);
957 return false;
958 }
959
23
24/* Process declarations and symbol lookup for C front end.
25 Also constructs types; the standard scalar types at initialization,
26 and structure, union, array and enum types when they are declared. */
27
28/* ??? not all decl nodes are given the most useful possible
29 line numbers. For example, the CONST_DECLs for enum values. */
30
31#include "config.h"
32#include "system.h"
33#include "coretypes.h"
34#include "tm.h"
35#include "intl.h"
36#include "tree.h"
37#include "tree-inline.h"
38#include "rtl.h"
39#include "flags.h"
40#include "function.h"
41#include "output.h"
42#include "expr.h"
43#include "c-tree.h"
44#include "toplev.h"
45#include "ggc.h"
46#include "tm_p.h"
47#include "cpplib.h"
48#include "target.h"
49#include "debug.h"
50#include "opts.h"
51#include "timevar.h"
52#include "c-common.h"
53#include "c-pragma.h"
54#include "cgraph.h"
55#include "hashtab.h"
56#include "libfuncs.h"
57#include "except.h"
58#include "langhooks-def.h"
59
60/* In grokdeclarator, distinguish syntactic contexts of declarators. */
61enum decl_context
62{ NORMAL, /* Ordinary declaration */
63 FUNCDEF, /* Function definition */
64 PARM, /* Declaration of parm before function body */
65 FIELD, /* Declaration inside struct or union */
66 TYPENAME}; /* Typename (inside cast or sizeof) */
67
68
69/* Nonzero if we have seen an invalid cross reference
70 to a struct, union, or enum, but not yet printed the message. */
71
72tree pending_invalid_xref;
73/* File and line to appear in the eventual error message. */
74location_t pending_invalid_xref_location;
75
76/* While defining an enum type, this is 1 plus the last enumerator
77 constant value. Note that will do not have to save this or `enum_overflow'
78 around nested function definition since such a definition could only
79 occur in an enum value expression and we don't use these variables in
80 that case. */
81
82static tree enum_next_value;
83
84/* Nonzero means that there was overflow computing enum_next_value. */
85
86static int enum_overflow;
87
88/* Parsing a function declarator leaves a list of parameter names
89 or a chain of parameter decls here. */
90
91static tree last_function_parms;
92
93/* ... and a chain of structure and enum types declared in the
94 parmlist here. */
95
96static tree last_function_parm_tags;
97
98/* ... and a chain of all non-parameter declarations (such as
99 CONST_DECLs from enumerations) here. */
100
101static tree last_function_parm_others;
102
103/* After parsing the declarator that starts a function definition,
104 `start_function' puts the list of parameter names or chain of decls here
105 for `store_parm_decls' to find. */
106
107static tree current_function_parms;
108
109/* Similar, for last_function_parm_tags. */
110
111static tree current_function_parm_tags;
112
113/* And for last_function_parm_others. */
114
115static tree current_function_parm_others;
116
117/* Similar, for the file and line that the prototype came from if this is
118 an old-style definition. */
119
120static location_t current_function_prototype_locus;
121
122/* The current statement tree. */
123
124static GTY(()) struct stmt_tree_s c_stmt_tree;
125
126/* The current scope statement stack. */
127
128static GTY(()) tree c_scope_stmt_stack;
129
130/* State saving variables. */
131int c_in_iteration_stmt;
132int c_in_case_stmt;
133
134/* A list of external DECLs that appeared at block scope when there was
135 some other global meaning for that identifier. */
136static GTY(()) tree truly_local_externals;
137
138/* All the builtins; this is a subset of the entries of global_scope. */
139
140static GTY(()) tree first_builtin_decl;
141static GTY(()) tree last_builtin_decl;
142
143/* A DECL for the current file-scope context. */
144
145static GTY(()) tree current_file_decl;
146
147/* Set to 0 at beginning of a function definition, set to 1 if
148 a return statement that specifies a return value is seen. */
149
150int current_function_returns_value;
151
152/* Set to 0 at beginning of a function definition, set to 1 if
153 a return statement with no argument is seen. */
154
155int current_function_returns_null;
156
157/* Set to 0 at beginning of a function definition, set to 1 if
158 a call to a noreturn function is seen. */
159
160int current_function_returns_abnormally;
161
162/* Set to nonzero by `grokdeclarator' for a function
163 whose return type is defaulted, if warnings for this are desired. */
164
165static int warn_about_return_type;
166
167/* Nonzero when starting a function declared `extern inline'. */
168
169static int current_extern_inline;
170
171/* Each c_scope structure describes the complete contents of one scope.
172 Three scopes are distinguished specially: the innermost or current
173 scope, the innermost function scope, and the outermost or file scope.
174
175 Most declarations are recorded in the current scope.
176
177 All normal label declarations are recorded in the innermost
178 function scope, as are bindings of undeclared identifiers to
179 error_mark_node. (GCC permits nested functions as an extension,
180 hence the 'innermost' qualifier.) Explicitly declared labels
181 (using the __label__ extension) appear in the current scope.
182
183 Being in the global scope (current_scope == global_scope) causes
184 special behavior in several places below. Also, under some
185 conditions the Objective-C front end records declarations in the
186 global scope even though that isn't the current scope.
187
188 The order of the names, parms, and blocks lists matters, and they
189 are frequently appended to. To avoid having to walk all the way to
190 the end of the list on each insertion, or reverse the lists later,
191 we maintain a pointer to the last list entry for each of the lists.
192
193 The order of the tags, shadowed, and shadowed_tags
194 lists does not matter, so we just prepend to these lists. */
195
196struct c_scope GTY(())
197{
198 /* The scope containing this one. */
199 struct c_scope *outer;
200
201 /* The next outermost function scope. */
202 struct c_scope *outer_function;
203
204 /* All variables, constants, functions, labels, and typedef names. */
205 tree names;
206 tree names_last;
207
208 /* All parameter declarations. Used only in the outermost scope of
209 a function. */
210 tree parms;
211 tree parms_last;
212
213 /* All structure, union, and enum type tags. */
214 tree tags;
215
216 /* For each scope, a list of shadowed outer-scope definitions
217 to be restored when this scope is popped.
218 Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
219 whose TREE_VALUE is its old definition (a kind of ..._DECL node). */
220 tree shadowed;
221
222 /* For each scope, a list of shadowed outer-scope tag definitions
223 to be restored when this scope is popped.
224 Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
225 whose TREE_VALUE is its old definition (a kind of ..._TYPE node). */
226 tree shadowed_tags;
227
228 /* For each scope (except the global one), a chain of BLOCK nodes
229 for all the scopes that were entered and exited one level down. */
230 tree blocks;
231 tree blocks_last;
232
233 /* True if we are currently filling this scope with parameter
234 declarations. */
235 BOOL_BITFIELD parm_flag : 1;
236
237 /* True if we already complained about forward parameter decls
238 in this scope. This prevents double warnings on
239 foo (int a; int b; ...) */
240 BOOL_BITFIELD warned_forward_parm_decls : 1;
241
242 /* True if this is the outermost block scope of a function body.
243 This scope contains the parameters, the local variables declared
244 in the outermost block, and all the labels (except those in
245 nested functions, or declared at block scope with __label__). */
246 BOOL_BITFIELD function_body : 1;
247
248 /* True means make a BLOCK for this scope no matter what. */
249 BOOL_BITFIELD keep : 1;
250};
251
252/* The scope currently in effect. */
253
254static GTY(()) struct c_scope *current_scope;
255
256/* A chain of c_scope structures awaiting reuse. */
257
258static GTY((deletable (""))) struct c_scope *scope_freelist;
259
260/* The innermost function scope. Ordinary (not explicitly declared)
261 labels, bindings to error_mark_node, and the lazily-created
262 bindings of __func__ and its friends get this scope. */
263
264static GTY(()) struct c_scope *current_function_scope;
265
266/* The outermost scope, corresponding to the C "file scope". This is
267 created when the compiler is started and exists through the entire run. */
268
269static GTY(()) struct c_scope *global_scope;
270
271/* Append VAR to LIST in scope SCOPE. */
272#define SCOPE_LIST_APPEND(scope, list, decl) do { \
273 struct c_scope *s_ = (scope); \
274 tree d_ = (decl); \
275 if (s_->list##_last) \
276 TREE_CHAIN (s_->list##_last) = d_; \
277 else \
278 s_->list = d_; \
279 s_->list##_last = d_; \
280} while (0)
281
282/* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
283#define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
284 struct c_scope *t_ = (tscope); \
285 struct c_scope *f_ = (fscope); \
286 if (t_->to##_last) \
287 TREE_CHAIN (t_->to##_last) = f_->from; \
288 else \
289 t_->to = f_->from; \
290 t_->to##_last = f_->from##_last; \
291} while (0)
292
293/* True means unconditionally make a BLOCK for the next scope pushed. */
294
295static bool keep_next_level_flag;
296
297/* True means the next call to pushlevel will be the outermost scope
298 of a function body, so do not push a new scope, merely cease
299 expecting parameter decls. */
300
301static bool next_is_function_body;
302
303/* Functions called automatically at the beginning and end of execution. */
304
305tree static_ctors, static_dtors;
306
307/* Forward declarations. */
308
309static struct c_scope *make_scope (void);
310static void pop_scope (void);
311static tree make_label (tree, location_t);
312static void bind_label (tree, tree, struct c_scope *);
313static void implicit_decl_warning (tree);
314static tree lookup_tag (enum tree_code, tree, int);
315static tree lookup_name_current_level (tree);
316static tree grokdeclarator (tree, tree, enum decl_context, int, tree *);
317static tree grokparms (tree, int);
318static void layout_array_type (tree);
319static void store_parm_decls_newstyle (void);
320static void store_parm_decls_oldstyle (void);
321static tree c_make_fname_decl (tree, int);
322static void c_expand_body_1 (tree, int);
323static tree any_external_decl (tree);
324static void record_external_decl (tree);
325static void warn_if_shadowing (tree, tree);
326static void check_bitfield_type_and_width (tree *, tree *, const char *);
327static void clone_underlying_type (tree);
328static bool flexible_array_type_p (tree);
329static hashval_t link_hash_hash (const void *);
330static int link_hash_eq (const void *, const void *);
331
332/* States indicating how grokdeclarator() should handle declspecs marked
333 with __attribute__((deprecated)). An object declared as
334 __attribute__((deprecated)) suppresses warnings of uses of other
335 deprecated items. */
336
337enum deprecated_states {
338 DEPRECATED_NORMAL,
339 DEPRECATED_SUPPRESS
340};
341
342static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
343
344void
345c_print_identifier (FILE *file, tree node, int indent)
346{
347 print_node (file, "symbol", IDENTIFIER_SYMBOL_VALUE (node), indent + 4);
348 print_node (file, "tag", IDENTIFIER_TAG_VALUE (node), indent + 4);
349 print_node (file, "label", IDENTIFIER_LABEL_VALUE (node), indent + 4);
350 if (C_IS_RESERVED_WORD (node))
351 {
352 tree rid = ridpointers[C_RID_CODE (node)];
353 indent_to (file, indent + 4);
354 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
355 (void *) rid, IDENTIFIER_POINTER (rid));
356 }
357}
358
359/* Hook called at end of compilation to assume 1 elt
360 for a file-scope tentative array defn that wasn't complete before. */
361
362void
363c_finish_incomplete_decl (tree decl)
364{
365 if (TREE_CODE (decl) == VAR_DECL)
366 {
367 tree type = TREE_TYPE (decl);
368 if (type != error_mark_node
369 && TREE_CODE (type) == ARRAY_TYPE
370 && ! DECL_EXTERNAL (decl)
371 && TYPE_DOMAIN (type) == 0)
372 {
373 warning ("%Jarray '%D' assumed to have one element", decl, decl);
374
375 complete_array_type (type, NULL_TREE, 1);
376
377 layout_decl (decl, 0);
378 }
379 }
380}
381
382/* Reuse or create a struct for this scope. */
383
384static struct c_scope *
385make_scope (void)
386{
387 struct c_scope *result;
388 if (scope_freelist)
389 {
390 result = scope_freelist;
391 scope_freelist = result->outer;
392 }
393 else
394 result = ggc_alloc_cleared (sizeof (struct c_scope));
395
396 return result;
397}
398
399/* Remove the topmost scope from the stack and add it to the
400 free list, updating current_function_scope if necessary. */
401
402static void
403pop_scope (void)
404{
405 struct c_scope *scope = current_scope;
406
407 current_scope = scope->outer;
408 if (scope->function_body)
409 current_function_scope = scope->outer_function;
410
411 memset (scope, 0, sizeof (struct c_scope));
412 scope->outer = scope_freelist;
413 scope_freelist = scope;
414}
415
416/* The Objective-C front-end often needs to determine the current scope. */
417
418void *
419get_current_scope (void)
420{
421 return current_scope;
422}
423
424/* The following function is used only by Objective-C. It needs to live here
425 because it accesses the innards of c_scope. */
426
427void
428objc_mark_locals_volatile (void *enclosing_blk)
429{
430 struct c_scope *scope;
431
432 for (scope = current_scope;
433 scope && scope != enclosing_blk;
434 scope = scope->outer)
435 {
436 tree decl;
437
438 for (decl = scope->names; decl; decl = TREE_CHAIN (decl))
439 {
440 DECL_REGISTER (decl) = 0;
441 TREE_THIS_VOLATILE (decl) = 1;
442 }
443 /* Do not climb up past the current function. */
444 if (scope->function_body)
445 break;
446 }
447}
448
449/* Nonzero if we are currently in the global scope. */
450
451int
452global_bindings_p (void)
453{
454 return current_scope == global_scope;
455}
456
457void
458keep_next_level (void)
459{
460 keep_next_level_flag = true;
461}
462
463/* Identify this scope as currently being filled with parameters. */
464
465void
466declare_parm_level (void)
467{
468 current_scope->parm_flag = true;
469}
470
471/* Nonzero if currently making parm declarations. */
472
473int
474in_parm_level_p (void)
475{
476 return current_scope->parm_flag;
477}
478
479/* Enter a new scope. The dummy parameter is for signature
480 compatibility with lang_hooks.decls.pushlevel. */
481
482void
483pushlevel (int dummy ATTRIBUTE_UNUSED)
484{
485 if (next_is_function_body)
486 {
487 /* This is the transition from the parameters to the top level
488 of the function body. These are the same scope
489 (C99 6.2.1p4,6) so we do not push another scope structure.
490 next_is_function_body is set only by store_parm_decls, which
491 in turn is called when and only when we are about to
492 encounter the opening curly brace for the function body.
493
494 The outermost block of a function always gets a BLOCK node,
495 because the debugging output routines expect that each
496 function has at least one BLOCK. */
497 current_scope->parm_flag = false;
498 current_scope->function_body = true;
499 current_scope->keep = true;
500 current_scope->outer_function = current_function_scope;
501 current_function_scope = current_scope;
502
503 keep_next_level_flag = false;
504 next_is_function_body = false;
505 }
506 else
507 {
508 struct c_scope *scope = make_scope ();
509
510 scope->keep = keep_next_level_flag;
511 scope->outer = current_scope;
512 current_scope = scope;
513 keep_next_level_flag = false;
514 }
515}
516
517/* Exit a scope. Restore the state of the identifier-decl mappings
518 that were in effect when this scope was entered.
519
520 If KEEP is KEEP_YES (1), this scope had explicit declarations, so
521 create a BLOCK node to record its declarations and subblocks for
522 debugging output. If KEEP is KEEP_MAYBE, do so only if the names
523 or tags lists are nonempty.
524
525 The second parameter is ignored; it is present only for
526 signature compatibility with lang_hooks.decls.poplevel.
527
528 If FUNCTIONBODY is nonzero, this level is the body of a function,
529 even if current_scope->function_body is not set. This is used
530 by language-independent code that generates synthetic functions,
531 and cannot set current_scope->function_body.
532
533 FIXME: Eliminate the need for all arguments. */
534
535tree
536poplevel (int keep, int dummy ATTRIBUTE_UNUSED, int functionbody)
537{
538 struct c_scope *scope = current_scope;
539 tree block;
540 tree decl;
541 tree p;
542
543 /* The following line does not use |= due to a bug in HP's C compiler. */
544 scope->function_body = scope->function_body | functionbody;
545
546 if (keep == KEEP_MAYBE)
547 keep = (scope->names || scope->tags);
548
549 keep |= scope->keep;
550 keep |= scope->function_body;
551
552 /* If appropriate, create a BLOCK to record the decls for the life
553 of this function. */
554 block = 0;
555 if (keep)
556 {
557 block = make_node (BLOCK);
558 BLOCK_VARS (block) = scope->names;
559 BLOCK_SUBBLOCKS (block) = scope->blocks;
560 TREE_USED (block) = 1;
561 }
562
563 /* In each subblock, record that this is its superior. */
564 for (p = scope->blocks; p; p = TREE_CHAIN (p))
565 BLOCK_SUPERCONTEXT (p) = block;
566
567 /* Clear out the variable bindings in this scope.
568
569 Propagate TREE_ADDRESSABLE from nested functions to their
570 containing functions.
571
572 Issue warnings for unused variables and labels, and errors for
573 undefined labels, if there are any. */
574
575 for (p = scope->names; p; p = TREE_CHAIN (p))
576 {
577 switch (TREE_CODE (p))
578 {
579 case LABEL_DECL:
580 if (TREE_USED (p) && !DECL_INITIAL (p))
581 {
582 error ("%Jlabel `%D' used but not defined", p, p);
583 DECL_INITIAL (p) = error_mark_node;
584 }
585 else if (!TREE_USED (p) && warn_unused_label)
586 {
587 if (DECL_INITIAL (p))
588 warning ("%Jlabel `%D' defined but not used", p, p);
589 else
590 warning ("%Jlabel `%D' declared but not defined", p, p);
591 }
592
593 IDENTIFIER_LABEL_VALUE (DECL_NAME (p)) = 0;
594 break;
595
596 case FUNCTION_DECL:
597 if (! TREE_ASM_WRITTEN (p)
598 && DECL_INITIAL (p) != 0
599 && TREE_ADDRESSABLE (p)
600 && DECL_ABSTRACT_ORIGIN (p) != 0
601 && DECL_ABSTRACT_ORIGIN (p) != p)
602 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
603 goto normal;
604
605 case VAR_DECL:
606 /* Keep this in sync with stmt.c:warn_about_unused_variables.
607 No warnings when the global scope is popped because the
608 global scope isn't popped for the last translation unit,
609 so the warnings are done in c_write_global_declaration. */
610 if (warn_unused_variable && scope != global_scope
611 && !TREE_USED (p)
612 && !DECL_IN_SYSTEM_HEADER (p)
613 && DECL_NAME (p)
614 && !DECL_ARTIFICIAL (p))
615 warning ("%Junused variable `%D'", p, p);
616 /* fall through */
617
618 default:
619 normal:
620 if (DECL_NAME (p))
621 {
622 if (DECL_EXTERNAL (p) && scope != global_scope)
623 /* External decls stay in the symbol-value slot but are
624 inaccessible. */
625 C_DECL_INVISIBLE (p) = 1;
626 else
627 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (p)) = 0;
628 }
629 break;
630 }
631 }
632
633 /* Clear out the parameter bindings in this scope, if any.
634 Unused-parameter warnings are handled by function.c. */
635 for (p = scope->parms; p; p = TREE_CHAIN (p))
636 if (DECL_NAME (p))
637 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (p)) = 0;
638
639 /* Clear out the tag-meanings declared in this scope.
640
641 Set the TYPE_CONTEXTs for all of the tagged types belonging to
642 this scope so that they point to the appropriate construct, i.e.
643 either to the current FUNCTION_DECL node, or else to the BLOCK
644 node we just constructed.
645
646 Note that for tagged types whose scope is just the formal
647 parameter list for some function type specification, we can't
648 properly set their TYPE_CONTEXTs here, because we don't have a
649 pointer to the appropriate FUNCTION_TYPE node readily available
650 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
651 type nodes get set in `grokdeclarator' as soon as we have created
652 the FUNCTION_TYPE node which will represent the "scope" for these
653 "parameter list local" tagged types. */
654
655 decl = scope->function_body ? current_function_decl : block;
656 for (p = scope->tags; p; p = TREE_CHAIN (p))
657 {
658 if (TREE_PURPOSE (p))
659 IDENTIFIER_TAG_VALUE (TREE_PURPOSE (p)) = 0;
660 if (decl)
661 TYPE_CONTEXT (TREE_VALUE (p)) = decl;
662 }
663
664 /* Restore all name- and label-meanings from outer scopes that were
665 shadowed by this scope. */
666 for (p = scope->shadowed; p; p = TREE_CHAIN (p))
667 if (TREE_VALUE (p) && TREE_CODE (TREE_VALUE (p)) == LABEL_DECL)
668 IDENTIFIER_LABEL_VALUE (TREE_PURPOSE (p)) = TREE_VALUE (p);
669 else
670 IDENTIFIER_SYMBOL_VALUE (TREE_PURPOSE (p)) = TREE_VALUE (p);
671
672 /* Restore all tag-meanings from outer scopes that were shadowed by
673 this scope. */
674 for (p = scope->shadowed_tags; p; p = TREE_CHAIN (p))
675 IDENTIFIER_TAG_VALUE (TREE_PURPOSE (p)) = TREE_VALUE (p);
676
677 /* Dispose of the block that we just made inside some higher level. */
678 if (scope->function_body && current_function_decl)
679 DECL_INITIAL (current_function_decl) = block;
680 else if (scope->outer)
681 {
682 if (block)
683 SCOPE_LIST_APPEND (scope->outer, blocks, block);
684 /* If we did not make a block for the scope just exited, any
685 blocks made for inner scopes must be carried forward so they
686 will later become subblocks of something else. */
687 else if (scope->blocks)
688 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
689 }
690
691 /* Pop the current scope, and free the structure for reuse. */
692 pop_scope ();
693
694 return block;
695}
696
697/* Insert BLOCK at the end of the list of subblocks of the current
698 scope. This is used when a BIND_EXPR is expanded, to handle the
699 BLOCK node inside the BIND_EXPR. */
700
701void
702insert_block (tree block)
703{
704 TREE_USED (block) = 1;
705 SCOPE_LIST_APPEND (current_scope, blocks, block);
706}
707
708/* Set the BLOCK node for the innermost scope (the one we are
709 currently in). The RTL expansion machinery requires us to provide
710 this hook, but it is not useful in function-at-a-time mode. */
711
712void
713set_block (tree block ATTRIBUTE_UNUSED)
714{
715}
716
717/* Push a definition or a declaration of struct, union or enum tag "name".
718 "type" should be the type node.
719 We assume that the tag "name" is not already defined.
720
721 Note that the definition may really be just a forward reference.
722 In that case, the TYPE_SIZE will be zero. */
723
724void
725pushtag (tree name, tree type)
726{
727 struct c_scope *b = current_scope;
728
729 /* Record the identifier as the type's name if it has none. */
730 if (name)
731 {
732 if (TYPE_NAME (type) == 0)
733 TYPE_NAME (type) = name;
734
735 if (IDENTIFIER_TAG_VALUE (name))
736 b->shadowed_tags = tree_cons (name, IDENTIFIER_TAG_VALUE (name),
737 b->shadowed_tags);
738 IDENTIFIER_TAG_VALUE (name) = type;
739 }
740
741 b->tags = tree_cons (name, type, b->tags);
742
743 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
744 tagged type we just added to the current scope. This fake
745 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
746 to output a representation of a tagged type, and it also gives
747 us a convenient place to record the "scope start" address for the
748 tagged type. */
749
750 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
751
752 /* An approximation for now, so we can tell this is a function-scope tag.
753 This will be updated in poplevel. */
754 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
755}
756
757/* Subroutine of compare_decls. Allow harmless mismatches in return
758 and argument types provided that the type modes match. This function
759 return a unified type given a suitable match, and 0 otherwise. */
760
761static tree
762match_builtin_function_types (tree newtype, tree oldtype)
763{
764 tree newrettype, oldrettype;
765 tree newargs, oldargs;
766 tree trytype, tryargs;
767
768 /* Accept the return type of the new declaration if same modes. */
769 oldrettype = TREE_TYPE (oldtype);
770 newrettype = TREE_TYPE (newtype);
771
772 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
773 return 0;
774
775 oldargs = TYPE_ARG_TYPES (oldtype);
776 newargs = TYPE_ARG_TYPES (newtype);
777 tryargs = newargs;
778
779 while (oldargs || newargs)
780 {
781 if (! oldargs
782 || ! newargs
783 || ! TREE_VALUE (oldargs)
784 || ! TREE_VALUE (newargs)
785 || TYPE_MODE (TREE_VALUE (oldargs))
786 != TYPE_MODE (TREE_VALUE (newargs)))
787 return 0;
788
789 oldargs = TREE_CHAIN (oldargs);
790 newargs = TREE_CHAIN (newargs);
791 }
792
793 trytype = build_function_type (newrettype, tryargs);
794 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
795}
796
797/* Subroutine of diagnose_mismathed_decls. Check for function type
798 mismatch involving an empty arglist vs a nonempty one and give clearer
799 diagnostics. */
800static void
801diagnose_arglist_conflict (tree newdecl, tree olddecl,
802 tree newtype, tree oldtype)
803{
804 tree t;
805
806 if (TREE_CODE (olddecl) != FUNCTION_DECL
807 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype), COMPARE_STRICT)
808 || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
809 ||
810 (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
811 return;
812
813 t = TYPE_ARG_TYPES (oldtype);
814 if (t == 0)
815 t = TYPE_ARG_TYPES (newtype);
816 for (; t; t = TREE_CHAIN (t))
817 {
818 tree type = TREE_VALUE (t);
819
820 if (TREE_CHAIN (t) == 0
821 && TYPE_MAIN_VARIANT (type) != void_type_node)
822 {
823 inform ("a parameter list with an ellipsis can't match "
824 "an empty parameter name list declaration");
825 break;
826 }
827
828 if (c_type_promotes_to (type) != type)
829 {
830 inform ("an argument type that has a default promotion can't match "
831 "an empty parameter name list declaration");
832 break;
833 }
834 }
835}
836
837/* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
838 old-style function definition, NEWDECL is a prototype declaration.
839 Diagnose inconsistencies in the argument list. Returns TRUE if
840 the prototype is compatible, FALSE if not. */
841static bool
842validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
843{
844 tree newargs, oldargs;
845 int i;
846
847 /* ??? Elsewhere TYPE_MAIN_VARIANT is not used in this context. */
848#define END_OF_ARGLIST(t) (TYPE_MAIN_VARIANT (t) == void_type_node)
849
850 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
851 newargs = TYPE_ARG_TYPES (newtype);
852 i = 1;
853
854 for (;;)
855 {
856 tree oldargtype = TREE_VALUE (oldargs);
857 tree newargtype = TREE_VALUE (newargs);
858
859 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
860 break;
861
862 /* Reaching the end of just one list means the two decls don't
863 agree on the number of arguments. */
864 if (END_OF_ARGLIST (oldargtype))
865 {
866 error ("%Jprototype for '%D' declares more arguments "
867 "than previous old-style definition", newdecl, newdecl);
868 return false;
869 }
870 else if (END_OF_ARGLIST (newargtype))
871 {
872 error ("%Jprototype for '%D' declares fewer arguments "
873 "than previous old-style definition", newdecl, newdecl);
874 return false;
875 }
876
877 /* Type for passing arg must be consistent with that declared
878 for the arg. */
879 else if (! comptypes (oldargtype, newargtype, COMPARE_STRICT))
880 {
881 error ("%Jprototype for '%D' declares arg %d with incompatible type",
882 newdecl, newdecl, i);
883 return false;
884 }
885
886 oldargs = TREE_CHAIN (oldargs);
887 newargs = TREE_CHAIN (newargs);
888 i++;
889 }
890
891 /* If we get here, no errors were found, but do issue a warning
892 for this poor-style construct. */
893 warning ("%Jprototype for '%D' follows non-prototype definition",
894 newdecl, newdecl);
895 return true;
896#undef END_OF_ARGLIST
897}
898
899/* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
900 first in a pair of mismatched declarations, using the diagnostic
901 function DIAG. */
902static void
903locate_old_decl (tree decl, void (*diag)(const char *, ...))
904{
905 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
906 ;
907 else if (DECL_INITIAL (decl))
908 diag (N_("%Jprevious definition of '%D' was here"), decl, decl);
909 else if (C_DECL_IMPLICIT (decl))
910 diag (N_("%Jprevious implicit declaration of '%D' was here"), decl, decl);
911 else
912 diag (N_("%Jprevious declaration of '%D' was here"), decl, decl);
913}
914
915/* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
916 Returns true if the caller should proceed to merge the two, false
917 if OLDDECL should simply be discarded. As a side effect, issues
918 all necessary diagnostics for invalid or poor-style combinations.
919 If it returns true, writes the types of NEWDECL and OLDDECL to
920 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
921 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
922
923static bool
924diagnose_mismatched_decls (tree newdecl, tree olddecl,
925 tree *newtypep, tree *oldtypep)
926{
927 tree newtype, oldtype;
928 bool pedwarned = false;
929 bool warned = false;
930
931 /* If we have error_mark_node for either decl or type, just discard
932 the previous decl - we're in an error cascade already. */
933 if (olddecl == error_mark_node || newdecl == error_mark_node)
934 return false;
935 *oldtypep = oldtype = TREE_TYPE (olddecl);
936 *newtypep = newtype = TREE_TYPE (newdecl);
937 if (oldtype == error_mark_node || newtype == error_mark_node)
938 return false;
939
940 /* Two different categories of symbol altogether. This is an error
941 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
942 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
943 {
944 if (TREE_CODE (olddecl) != FUNCTION_DECL
945 || !DECL_BUILT_IN (olddecl) || !C_DECL_INVISIBLE (olddecl))
946 {
947 error ("%J'%D' redeclared as different kind of symbol",
948 newdecl, newdecl);
949 locate_old_decl (olddecl, error);
950 }
951 else if (TREE_PUBLIC (newdecl))
952 warning ("%Jbuilt-in function '%D' declared as non-function",
953 newdecl, newdecl);
954 else if (warn_shadow)
955 warning ("%Jshadowing built-in function '%D'",
956 newdecl, newdecl);
957 return false;
958 }
959
960 /* Enumerators have no linkage, so may only be declared once in a
961 given scope. */
962 if (TREE_CODE (olddecl) == CONST_DECL)
963 {
964 error ("%Jredeclaration of enumerator `%D'", newdecl, newdecl);
965 locate_old_decl (olddecl, error);
966 return false;
967 }
968
960 if (!comptypes (oldtype, newtype, COMPARE_STRICT))
961 {
962 if (TREE_CODE (olddecl) == FUNCTION_DECL
963 && DECL_BUILT_IN (olddecl) && C_DECL_INVISIBLE (olddecl))
964 {
965 /* Accept harmless mismatch in function types.
966 This is for the ffs and fprintf builtins. */
967 tree trytype = match_builtin_function_types (newtype, oldtype);
968
969 if (trytype && comptypes (newtype, trytype, COMPARE_STRICT))
970 *oldtypep = oldtype = trytype;
971 else
972 {
973 /* If types don't match for a built-in, throw away the
974 built-in. No point in calling locate_old_decl here, it
975 won't print anything. */
976 warning ("%Jconflicting types for built-in function '%D'",
977 newdecl, newdecl);
978 return false;
979 }
980 }
981 else if (TREE_CODE (olddecl) == FUNCTION_DECL
982 && DECL_SOURCE_LINE (olddecl) == 0)
983 {
984 /* A conflicting function declaration for a predeclared
985 function that isn't actually built in. Objective C uses
986 these. The new declaration silently overrides everything
987 but the volatility (i.e. noreturn) indication. See also
988 below. FIXME: Make Objective C use normal builtins. */
989 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
990 return false;
991 }
992 /* Permit void foo (...) to match int foo (...) if the latter is
993 the definition and implicit int was used. See
994 c-torture/compile/920625-2.c. */
995 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
996 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
997 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
998 && C_FUNCTION_IMPLICIT_INT (newdecl))
999 {
1000 pedwarn ("%Jconflicting types for '%D'", newdecl, newdecl);
1001 /* Make sure we keep void as the return type. */
1002 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1003 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1004 pedwarned = true;
1005 }
1006 else
1007 {
1008 error ("%Jconflicting types for '%D'", newdecl, newdecl);
1009 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1010 locate_old_decl (olddecl, error);
1011 return false;
1012 }
1013 }
1014
1015 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1016 but silently ignore the redeclaration if either is in a system
1017 header. (Conflicting redeclarations were handled above.) */
1018 if (TREE_CODE (newdecl) == TYPE_DECL)
1019 {
1020 if (DECL_IN_SYSTEM_HEADER (newdecl) || DECL_IN_SYSTEM_HEADER (olddecl))
1021 return true; /* allow OLDDECL to continue in use */
1022
1023 error ("%Jredefinition of typedef '%D'", newdecl, newdecl);
1024 locate_old_decl (olddecl, error);
1025 return false;
1026 }
1027
1028 /* Function declarations can either be 'static' or 'extern' (no
1029 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1030 can never conflict with each other on account of linkage (6.2.2p4).
1031 Multiple definitions are not allowed (6.9p3,5) but GCC permits
1032 two definitions if one is 'extern inline' and one is not. The non-
1033 extern-inline definition supersedes the extern-inline definition. */
1034 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1035 {
1036 /* If you declare a built-in function name as static, or
1037 define the built-in with an old-style definition (so we
1038 can't validate the argument list) the built-in definition is
1039 overridden, but optionally warn this was a bad choice of name. */
1040 if (DECL_BUILT_IN (olddecl)
1041 && C_DECL_INVISIBLE (olddecl)
1042 && (!TREE_PUBLIC (newdecl)
1043 || (DECL_INITIAL (newdecl)
1044 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1045 {
1046 if (warn_shadow)
1047 warning ("%Jshadowing built-in function '%D'", newdecl, newdecl);
1048 /* Discard the old built-in function. */
1049 return false;
1050 }
1051
1052 if (DECL_INITIAL (newdecl))
1053 {
1054 if (DECL_INITIAL (olddecl)
1055 && !(DECL_DECLARED_INLINE_P (olddecl)
1056 && DECL_EXTERNAL (olddecl)
1057 && !(DECL_DECLARED_INLINE_P (newdecl)
1058 && DECL_EXTERNAL (newdecl))))
1059 {
1060 error ("%Jredefinition of '%D'", newdecl, newdecl);
1061 locate_old_decl (olddecl, error);
1062 return false;
1063 }
1064 }
1065 /* If we have a prototype after an old-style function definition,
1066 the argument types must be checked specially. */
1067 else if (DECL_INITIAL (olddecl)
1068 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1069 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1070 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1071 {
1072 locate_old_decl (olddecl, error);
1073 return false;
1074 }
1075 /* Mismatched non-static and static is considered poor style.
1076 We only diagnose static then non-static if -Wtraditional,
1077 because it is the most convenient way to get some effects
1078 (see e.g. what unwind-dw2-fde-glibc.c does to the definition
1079 of _Unwind_Find_FDE in unwind-dw2-fde.c). Revisit? */
1080 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1081 {
1082 /* A static function declaration for a predeclared function
1083 that isn't actually built in, silently overrides the
1084 default. Objective C uses these. See also above.
1085 FIXME: Make Objective C use normal builtins. */
1086 if (TREE_CODE (olddecl) == FUNCTION_DECL
1087 && DECL_SOURCE_LINE (olddecl) == 0)
1088 return false;
1089 else
1090 {
1091 warning ("%Jstatic declaration of '%D' follows "
1092 "non-static declaration", newdecl, newdecl);
1093 warned = true;
1094 }
1095 }
1096 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl)
1097 && warn_traditional)
1098 {
1099 warning ("%Jnon-static declaration of '%D' follows "
1100 "static declaration", newdecl, newdecl);
1101 warned = true;
1102 }
1103 }
1104 else if (TREE_CODE (newdecl) == VAR_DECL)
1105 {
1106 /* Only variables can be thread-local, and all declarations must
1107 agree on this property. */
1108 if (DECL_THREAD_LOCAL (newdecl) != DECL_THREAD_LOCAL (olddecl))
1109 {
1110 if (DECL_THREAD_LOCAL (newdecl))
1111 error ("%Jthread-local declaration of '%D' follows "
1112 "non-thread-local declaration", newdecl, newdecl);
1113 else
1114 error ("%Jnon-thread-local declaration of '%D' follows "
1115 "thread-local declaration", newdecl, newdecl);
1116
1117 locate_old_decl (olddecl, error);
1118 return false;
1119 }
1120
1121 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1122 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1123 {
1124 error ("%Jredefinition of '%D'", newdecl, newdecl);
1125 locate_old_decl (olddecl, error);
1126 return false;
1127 }
1128
1129 /* Objects declared at file scope: if at least one is 'extern',
1130 it's fine (6.2.2p4); otherwise the linkage must agree (6.2.2p7). */
1131 if (DECL_FILE_SCOPE_P (newdecl))
1132 {
1133 if (!DECL_EXTERNAL (newdecl)
1134 && !DECL_EXTERNAL (olddecl)
1135 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1136 {
1137 if (TREE_PUBLIC (newdecl))
1138 error ("%Jnon-static declaration of '%D' follows "
1139 "static declaration", newdecl, newdecl);
1140 else
1141 error ("%Jstatic declaration of '%D' follows "
1142 "non-static declaration", newdecl, newdecl);
1143
1144 locate_old_decl (olddecl, error);
1145 return false;
1146 }
1147 }
1148 /* Two objects with the same name declared at the same block
1149 scope must both be external references (6.7p3). */
1150 else if (DECL_CONTEXT (newdecl) == DECL_CONTEXT (olddecl)
1151 && (!DECL_EXTERNAL (newdecl) || !DECL_EXTERNAL (olddecl)))
1152 {
1153 if (DECL_EXTERNAL (newdecl))
1154 error ("%Jextern declaration of '%D' follows "
1155 "declaration with no linkage", newdecl, newdecl);
1156 else if (DECL_EXTERNAL (olddecl))
1157 error ("%Jdeclaration of '%D' with no linkage follows "
1158 "extern declaration", newdecl, newdecl);
1159 else
1160 error ("%Jredeclaration of '%D' with no linkage",
1161 newdecl, newdecl);
1162
1163 locate_old_decl (olddecl, error);
1164 return false;
1165 }
1166 }
1167
1168 /* warnings */
1169 /* All decls must agree on a non-default visibility. */
1170 if (DECL_VISIBILITY (newdecl) != VISIBILITY_DEFAULT
1171 && DECL_VISIBILITY (olddecl) != VISIBILITY_DEFAULT
1172 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1173 {
1174 warning ("%Jredeclaration of '%D' with different visibility "
1175 "(old visibility preserved)", newdecl, newdecl);
1176 warned = true;
1177 }
1178
1179 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1180 {
1181 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
1182 if (DECL_DECLARED_INLINE_P (newdecl)
1183 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1184 {
1185 warning ("%Jinline declaration of '%D' follows "
1186 "declaration with attribute noinline", newdecl, newdecl);
1187 warned = true;
1188 }
1189 else if (DECL_DECLARED_INLINE_P (olddecl)
1190 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1191 {
1192 warning ("%Jdeclaration of '%D' with attribute noinline follows "
1193 "inline declaration ", newdecl, newdecl);
1194 warned = true;
1195 }
1196
1197 /* Inline declaration after use or definition.
1198 ??? Should we still warn about this now we have unit-at-a-time
1199 mode and can get it right? */
1200 if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl))
1201 {
1202 if (TREE_USED (olddecl))
1203 {
1204 warning ("%J'%D' declared inline after being called",
1205 olddecl, olddecl);
1206 warned = true;
1207 }
1208 else if (DECL_INITIAL (olddecl))
1209 {
1210 warning ("%J'%D' declared inline after its definition",
1211 olddecl, olddecl);
1212 warned = true;
1213 }
1214 }
1215 }
1216 else /* PARM_DECL, VAR_DECL */
1217 {
1218 /* Redeclaration of a PARM_DECL is invalid unless this is the
1219 real position of a forward-declared parameter (GCC extension). */
1220 if (TREE_CODE (newdecl) == PARM_DECL
1221 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1222 {
1223 error ("%Jredefinition of parameter '%D'", newdecl, newdecl);
1224 locate_old_decl (olddecl, error);
1225 return false;
1226 }
1227
1228 /* These bits are only type qualifiers when applied to objects. */
1229 if (TREE_THIS_VOLATILE (newdecl) != TREE_THIS_VOLATILE (olddecl))
1230 {
1231 if (TREE_THIS_VOLATILE (newdecl))
1232 pedwarn ("%Jvolatile declaration of '%D' follows "
1233 "non-volatile declaration", newdecl, newdecl);
1234 else
1235 pedwarn ("%Jnon-volatile declaration of '%D' follows "
1236 "volatile declaration", newdecl, newdecl);
1237 pedwarned = true;
1238 }
1239 if (TREE_READONLY (newdecl) != TREE_READONLY (olddecl))
1240 {
1241 if (TREE_READONLY (newdecl))
1242 pedwarn ("%Jconst declaration of '%D' follows "
1243 "non-const declaration", newdecl, newdecl);
1244 else
1245 pedwarn ("%Jnon-const declaration of '%D' follows "
1246 "const declaration", newdecl, newdecl);
1247 pedwarned = true;
1248 }
1249 }
1250
1251 /* Optional warning for completely redundant decls. */
1252 if (!warned && !pedwarned
1253 && warn_redundant_decls
1254 /* Don't warn about a function declaration followed by a
1255 definition. */
1256 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1257 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1258 /* Don't warn about redundant redeclarations of builtins. */
1259 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1260 && !DECL_BUILT_IN (newdecl)
1261 && DECL_BUILT_IN (olddecl)
1262 && C_DECL_INVISIBLE (olddecl))
1263 /* Don't warn about an extern followed by a definition. */
1264 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1265 /* Don't warn about forward parameter decls. */
1266 && !(TREE_CODE (newdecl) == PARM_DECL
1267 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1268 /* Don't warn about a variable definition following a declaration. */
1269 && !(TREE_CODE (newdecl) == VAR_DECL
1270 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
1271 {
1272 warning ("%Jredundant redeclaration of '%D'", newdecl, newdecl);
1273 warned = true;
1274 }
1275
1276 /* Report location of previous decl/defn in a consistent manner. */
1277 if (warned || pedwarned)
1278 locate_old_decl (olddecl, pedwarned ? pedwarn : warning);
1279
1280 return true;
1281}
1282
1283/* Subroutine of duplicate_decls. NEWDECL has been found to be
1284 consistent with OLDDECL, but carries new information. Merge the
1285 new information into OLDDECL. This function issues no
1286 diagnostics. */
1287
1288static void
1289merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1290{
1291 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1292 && DECL_INITIAL (newdecl) != 0);
1293
1294 /* For real parm decl following a forward decl, return 1 so old decl
1295 will be reused. Only allow this to happen once. */
1296 if (TREE_CODE (newdecl) == PARM_DECL
1297 && TREE_ASM_WRITTEN (olddecl) && ! TREE_ASM_WRITTEN (newdecl))
1298 {
1299 TREE_ASM_WRITTEN (olddecl) = 0;
1300 return;
1301 }
1302
1303 DECL_ATTRIBUTES (newdecl)
1304 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
1305
1306 /* Merge the data types specified in the two decls. */
1307 TREE_TYPE (newdecl)
1308 = TREE_TYPE (olddecl)
1309 = common_type (newtype, oldtype);
1310
1311 /* Lay the type out, unless already done. */
1312 if (oldtype != TREE_TYPE (newdecl))
1313 {
1314 if (TREE_TYPE (newdecl) != error_mark_node)
1315 layout_type (TREE_TYPE (newdecl));
1316 if (TREE_CODE (newdecl) != FUNCTION_DECL
1317 && TREE_CODE (newdecl) != TYPE_DECL
1318 && TREE_CODE (newdecl) != CONST_DECL)
1319 layout_decl (newdecl, 0);
1320 }
1321 else
1322 {
1323 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1324 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1325 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1326 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1327 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1328 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1329 {
1330 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1331 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1332 }
1333 }
1334
1335 /* Keep the old rtl since we can safely use it. */
1336 COPY_DECL_RTL (olddecl, newdecl);
1337
1338 /* Merge the type qualifiers. */
1339 if (TREE_READONLY (newdecl))
1340 TREE_READONLY (olddecl) = 1;
1341
1342 if (TREE_THIS_VOLATILE (newdecl))
1343 {
1344 TREE_THIS_VOLATILE (olddecl) = 1;
1345 if (TREE_CODE (newdecl) == VAR_DECL)
1346 make_var_volatile (newdecl);
1347 }
1348
1349 /* Keep source location of definition rather than declaration. */
1350 if (DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1351 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1352
1353 /* Merge the unused-warning information. */
1354 if (DECL_IN_SYSTEM_HEADER (olddecl))
1355 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1356 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1357 DECL_IN_SYSTEM_HEADER (olddecl) = 1;
1358
1359 /* Merge the initialization information. */
1360 if (DECL_INITIAL (newdecl) == 0)
1361 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1362
1363 /* Merge the section attribute.
1364 We want to issue an error if the sections conflict but that must be
1365 done later in decl_attributes since we are called before attributes
1366 are assigned. */
1367 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1368 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1369
1370 /* Copy the assembler name.
1371 Currently, it can only be defined in the prototype. */
1372 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1373
1374 /* If either declaration has a nondefault visibility, use it. */
1375 if (DECL_VISIBILITY (olddecl) != VISIBILITY_DEFAULT)
1376 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1377
1378 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1379 {
1380 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1381 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1382 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1383 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1384 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1385 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1386 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1387 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1388 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1389 }
1390
1391 /* Merge the storage class information. */
1392 merge_weak (newdecl, olddecl);
1393
1394 /* For functions, static overrides non-static. */
1395 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1396 {
1397 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1398 /* This is since we don't automatically
1399 copy the attributes of NEWDECL into OLDDECL. */
1400 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1401 /* If this clears `static', clear it in the identifier too. */
1402 if (! TREE_PUBLIC (olddecl))
1403 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1404 }
1405 if (DECL_EXTERNAL (newdecl))
1406 {
1407 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1408 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1409
1410 /* An extern decl does not override previous storage class. */
1411 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1412 if (! DECL_EXTERNAL (newdecl))
1413 {
1414 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1415 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1416 }
1417 }
1418 else
1419 {
1420 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1421 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1422 }
1423
1424 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1425 {
1426 /* If we're redefining a function previously defined as extern
1427 inline, make sure we emit debug info for the inline before we
1428 throw it away, in case it was inlined into a function that hasn't
1429 been written out yet. */
1430 if (new_is_definition && DECL_INITIAL (olddecl))
1431 {
1432 if (TREE_USED (olddecl)
1433 /* In unit-at-a-time mode we never inline re-defined extern
1434 inline functions. */
1435 && !flag_unit_at_a_time
1436 && cgraph_function_possibly_inlined_p (olddecl))
1437 (*debug_hooks->outlining_inline_function) (olddecl);
1438
1439 /* The new defn must not be inline. */
1440 DECL_INLINE (newdecl) = 0;
1441 DECL_UNINLINABLE (newdecl) = 1;
1442 }
1443 else
1444 {
1445 /* If either decl says `inline', this fn is inline,
1446 unless its definition was passed already. */
1447 if (DECL_DECLARED_INLINE_P (newdecl)
1448 || DECL_DECLARED_INLINE_P (olddecl))
1449 DECL_DECLARED_INLINE_P (newdecl) = 1;
1450
1451 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1452 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1453 }
1454
1455 if (DECL_BUILT_IN (olddecl))
1456 {
1457 /* If redeclaring a builtin function, it stays built in. */
1458 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1459 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1460 }
1461
1462 /* Also preserve various other info from the definition. */
1463 if (! new_is_definition)
1464 {
1465 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1466 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1467 DECL_SAVED_INSNS (newdecl) = DECL_SAVED_INSNS (olddecl);
1468 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1469 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1470
1471 /* Set DECL_INLINE on the declaration if we've got a body
1472 from which to instantiate. */
1473 if (DECL_INLINE (olddecl) && ! DECL_UNINLINABLE (newdecl))
1474 {
1475 DECL_INLINE (newdecl) = 1;
1476 DECL_ABSTRACT_ORIGIN (newdecl)
1477 = DECL_ABSTRACT_ORIGIN (olddecl);
1478 }
1479 }
1480 else
1481 {
1482 /* If a previous declaration said inline, mark the
1483 definition as inlinable. */
1484 if (DECL_DECLARED_INLINE_P (newdecl)
1485 && ! DECL_UNINLINABLE (newdecl))
1486 DECL_INLINE (newdecl) = 1;
1487 }
1488 }
1489
1490 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1491 But preserve OLDDECL's DECL_UID and C_DECL_INVISIBLE. */
1492 {
1493 unsigned olddecl_uid = DECL_UID (olddecl);
1494 unsigned olddecl_invisible = C_DECL_INVISIBLE (olddecl);
1495
1496 memcpy ((char *) olddecl + sizeof (struct tree_common),
1497 (char *) newdecl + sizeof (struct tree_common),
1498 sizeof (struct tree_decl) - sizeof (struct tree_common));
1499 DECL_UID (olddecl) = olddecl_uid;
1500 C_DECL_INVISIBLE (olddecl) = olddecl_invisible;
1501 }
1502
1503 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1504 so that encode_section_info has a chance to look at the new decl
1505 flags and attributes. */
1506 if (DECL_RTL_SET_P (olddecl)
1507 && (TREE_CODE (olddecl) == FUNCTION_DECL
1508 || (TREE_CODE (olddecl) == VAR_DECL
1509 && TREE_STATIC (olddecl))))
1510 make_decl_rtl (olddecl, NULL);
1511}
1512
1513/* Handle when a new declaration NEWDECL has the same name as an old
1514 one OLDDECL in the same binding contour. Prints an error message
1515 if appropriate.
1516
1517 If safely possible, alter OLDDECL to look like NEWDECL, and return
1518 true. Otherwise, return false. */
1519
1520static bool
1521duplicate_decls (tree newdecl, tree olddecl)
1522{
1523 tree newtype, oldtype;
1524
1525 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
1526 return false;
1527
1528 merge_decls (newdecl, olddecl, newtype, oldtype);
1529 return true;
1530}
1531
1532
1533/* Return any external DECL associated with ID, whether or not it is
1534 currently in scope. */
1535
1536static tree
1537any_external_decl (tree id)
1538{
1539 tree decl = IDENTIFIER_SYMBOL_VALUE (id);
1540 tree t;
1541
1542 if (decl == 0 || TREE_CODE (decl) == ERROR_MARK)
1543 return 0;
1544 else if (TREE_CODE (decl) != TYPE_DECL && DECL_EXTERNAL (decl))
1545 return decl;
1546
1547 t = purpose_member (id, truly_local_externals);
1548 if (t)
1549 return TREE_VALUE (t);
1550
1551 return 0;
1552}
1553
1554/* Record an external decl DECL. This only does something if a
1555 shadowing decl already exists. */
1556static void
1557record_external_decl (tree decl)
1558{
1559 tree name = DECL_NAME (decl);
1560 if (!IDENTIFIER_SYMBOL_VALUE (name))
1561 return;
1562
1563 truly_local_externals = tree_cons (name, decl, truly_local_externals);
1564}
1565
1566/* Check whether decl-node X shadows an existing declaration.
1567 OLD is the old IDENTIFIER_SYMBOL_VALUE of the DECL_NAME of X,
1568 which might be a NULL_TREE. */
1569static void
1570warn_if_shadowing (tree x, tree old)
1571{
1572 /* Nothing to shadow? */
1573 if (old == 0
1574 /* Shadow warnings not wanted? */
1575 || !warn_shadow
1576 /* No shadow warnings for internally generated vars. */
1577 || DECL_SOURCE_LINE (x) == 0
1578 /* No shadow warnings for vars made for inlining. */
1579 || DECL_FROM_INLINE (x)
1580 /* Don't warn about the parm names in function declarator
1581 within a function declarator.
1582 It would be nice to avoid warning in any function
1583 declarator in a declaration, as opposed to a definition,
1584 but there is no way to tell it's not a definition. */
1585 || (TREE_CODE (x) == PARM_DECL && current_scope->outer->parm_flag)
1586 /* Shadow warnings only apply to local variables and parameters. */
1587 || (TREE_CODE (x) != PARM_DECL && DECL_FILE_SCOPE_P (x)))
1588 return;
1589
1590 if (TREE_CODE (old) == PARM_DECL)
1591 warning ("%Jdeclaration of '%D' shadows a parameter", x, x);
1592 else if (DECL_FILE_SCOPE_P (old))
1593 warning ("%Jdeclaration of '%D' shadows a global declaration", x, x);
1594 else
1595 warning ("%Jdeclaration of '%D' shadows a previous local", x, x);
1596
1597 warning ("%Jshadowed declaration is here", old);
1598}
1599
1600
1601/* Subroutine of pushdecl.
1602
1603 X is a TYPE_DECL for a typedef statement. Create a brand new
1604 ..._TYPE node (which will be just a variant of the existing
1605 ..._TYPE node with identical properties) and then install X
1606 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1607
1608 The whole point here is to end up with a situation where each
1609 and every ..._TYPE node the compiler creates will be uniquely
1610 associated with AT MOST one node representing a typedef name.
1611 This way, even though the compiler substitutes corresponding
1612 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1613 early on, later parts of the compiler can always do the reverse
1614 translation and get back the corresponding typedef name. For
1615 example, given:
1616
1617 typedef struct S MY_TYPE;
1618 MY_TYPE object;
1619
1620 Later parts of the compiler might only know that `object' was of
1621 type `struct S' if it were not for code just below. With this
1622 code however, later parts of the compiler see something like:
1623
1624 struct S' == struct S
1625 typedef struct S' MY_TYPE;
1626 struct S' object;
1627
1628 And they can then deduce (from the node for type struct S') that
1629 the original object declaration was:
1630
1631 MY_TYPE object;
1632
1633 Being able to do this is important for proper support of protoize,
1634 and also for generating precise symbolic debugging information
1635 which takes full account of the programmer's (typedef) vocabulary.
1636
1637 Obviously, we don't want to generate a duplicate ..._TYPE node if
1638 the TYPE_DECL node that we are now processing really represents a
1639 standard built-in type.
1640
1641 Since all standard types are effectively declared at line zero
1642 in the source file, we can easily check to see if we are working
1643 on a standard type by checking the current value of lineno. */
1644
1645static void
1646clone_underlying_type (tree x)
1647{
1648 if (DECL_SOURCE_LINE (x) == 0)
1649 {
1650 if (TYPE_NAME (TREE_TYPE (x)) == 0)
1651 TYPE_NAME (TREE_TYPE (x)) = x;
1652 }
1653 else if (TREE_TYPE (x) != error_mark_node
1654 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
1655 {
1656 tree tt = TREE_TYPE (x);
1657 DECL_ORIGINAL_TYPE (x) = tt;
1658 tt = build_type_copy (tt);
1659 TYPE_NAME (tt) = x;
1660 TREE_USED (tt) = TREE_USED (x);
1661 TREE_TYPE (x) = tt;
1662 }
1663}
1664
1665/* Record a decl-node X as belonging to the current lexical scope.
1666 Check for errors (such as an incompatible declaration for the same
1667 name already seen in the same scope).
1668
1669 Returns either X or an old decl for the same name.
1670 If an old decl is returned, it may have been smashed
1671 to agree with what X says. */
1672
1673tree
1674pushdecl (tree x)
1675{
1676 tree name = DECL_NAME (x);
1677 struct c_scope *scope = current_scope;
1678
1679#ifdef ENABLE_CHECKING
1680 if (error_mark_node == 0)
1681 /* Called too early. */
1682 abort ();
1683#endif
1684
1685 /* Functions need the lang_decl data. */
1686 if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_LANG_SPECIFIC (x))
1687 DECL_LANG_SPECIFIC (x) = ggc_alloc_cleared (sizeof (struct lang_decl));
1688
1689 /* A local extern declaration for a function doesn't constitute nesting.
1690 A local auto declaration does, since it's a forward decl
1691 for a nested function coming later. */
1692 if (current_function_decl == NULL
1693 || ((TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
1694 && DECL_INITIAL (x) == 0 && DECL_EXTERNAL (x)))
1695 DECL_CONTEXT (x) = current_file_decl;
1696 else
1697 DECL_CONTEXT (x) = current_function_decl;
1698
1699 if (name)
1700 {
1701 tree old;
1702
1703 if (warn_nested_externs
1704 && scope != global_scope
1705 && DECL_EXTERNAL (x)
1706 && !DECL_IN_SYSTEM_HEADER (x))
1707 warning ("nested extern declaration of `%s'",
1708 IDENTIFIER_POINTER (name));
1709
1710 old = lookup_name_current_level (name);
1711 if (old && duplicate_decls (x, old))
1712 {
1713 /* For PARM_DECLs, old may be a forward declaration.
1714 If so, we want to remove it from its old location
1715 (in the variables chain) and rechain it in the
1716 location given by the new declaration. */
1717 if (TREE_CODE (x) == PARM_DECL)
1718 {
1719 tree *p;
1720 for (p = &scope->names; *p; p = &TREE_CHAIN (*p))
1721 if (*p == old)
1722 {
1723 *p = TREE_CHAIN (old);
1724 SCOPE_LIST_APPEND (scope, parms, old);
1725 break;
1726 }
1727 }
1728 return old;
1729 }
1730 if (DECL_EXTERNAL (x) || scope == global_scope)
1731 {
1732 /* Find and check against a previous, not-in-scope, external
1733 decl for this identifier. (C99 6.2.7p2: All declarations
1734 that refer to the same object or function shall have
1735 compatible type; otherwise, the behavior is undefined.) */
1736 tree ext = any_external_decl (name);
1737 if (ext)
1738 {
1739 if (duplicate_decls (x, ext))
1740 x = copy_node (ext);
1741 }
1742 else
1743 record_external_decl (x);
1744 }
1745
1746 if (TREE_CODE (x) == TYPE_DECL)
1747 clone_underlying_type (x);
1748
1749 /* If storing a local value, there may already be one
1750 (inherited). If so, record it for restoration when this
1751 scope ends. Take care not to do this if we are replacing an
1752 older decl in the same scope (i.e. duplicate_decls returned
1753 false, above). */
1754 if (scope != global_scope)
1755 {
1756 tree inherited_decl = lookup_name (name);
1757 if (inherited_decl && inherited_decl != old)
1758 {
1759 warn_if_shadowing (x, inherited_decl);
1760 scope->shadowed = tree_cons (name, inherited_decl,
1761 scope->shadowed);
1762 }
1763 }
1764
1765 /* Install the new declaration in the requested scope. */
1766 IDENTIFIER_SYMBOL_VALUE (name) = x;
1767 C_DECL_INVISIBLE (x) = 0;
1768
1769 /* If x's type is incomplete because it's based on a
1770 structure or union which has not yet been fully declared,
1771 attach it to that structure or union type, so we can go
1772 back and complete the variable declaration later, if the
1773 structure or union gets fully declared.
1774
1775 If the input is erroneous, we can have error_mark in the type
1776 slot (e.g. "f(void a, ...)") - that doesn't count as an
1777 incomplete type. */
1778 if (TREE_TYPE (x) != error_mark_node
1779 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
1780 {
1781 tree element = TREE_TYPE (x);
1782
1783 while (TREE_CODE (element) == ARRAY_TYPE)
1784 element = TREE_TYPE (element);
1785 element = TYPE_MAIN_VARIANT (element);
1786
1787 if ((TREE_CODE (element) == RECORD_TYPE
1788 || TREE_CODE (element) == UNION_TYPE)
1789 && (TREE_CODE (x) != TYPE_DECL
1790 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
1791 && !COMPLETE_TYPE_P (element))
1792 C_TYPE_INCOMPLETE_VARS (element)
1793 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
1794 }
1795 }
1796
1797 if (TREE_CODE (x) == PARM_DECL)
1798 SCOPE_LIST_APPEND (scope, parms, x);
1799 else
1800 SCOPE_LIST_APPEND (scope, names, x);
1801
1802 return x;
1803}
1804
1805/* Record X as belonging to the global scope (C99 "file scope").
1806 This is used only internally by the Objective-C front end,
1807 and is limited to its needs. duplicate_decls is not called;
1808 if there is any preexisting decl for this identifier, it is an ICE. */
1809
1810tree
1811pushdecl_top_level (tree x)
1812{
1813 tree name;
1814
1815 if (TREE_CODE (x) != VAR_DECL)
1816 abort ();
1817
1818 name = DECL_NAME (x);
1819
1820 if (IDENTIFIER_SYMBOL_VALUE (name))
1821 abort ();
1822
1823 DECL_CONTEXT (x) = current_file_decl;
1824 IDENTIFIER_SYMBOL_VALUE (name) = x;
1825
1826 SCOPE_LIST_APPEND (global_scope, names, x);
1827 return x;
1828}
1829
1830/* Generate an implicit declaration for identifier FUNCTIONID as a
1831 function of type int (). */
1832
1833tree
1834implicitly_declare (tree functionid)
1835{
1836 tree decl = any_external_decl (functionid);
1837
1838 if (decl)
1839 {
1840 /* Implicit declaration of a function already declared
1841 (somehow) in a different scope, or as a built-in.
1842 If this is the first time this has happened, warn;
1843 then recycle the old declaration. */
1844 if (!C_DECL_IMPLICIT (decl))
1845 {
1846 implicit_decl_warning (DECL_NAME (decl));
1847 if (! DECL_FILE_SCOPE_P (decl))
1848 warning ("%Jprevious declaration of '%D'", decl, decl);
1849 C_DECL_IMPLICIT (decl) = 1;
1850 }
1851 /* If this function is global, then it must already be in the
1852 global scope, so there's no need to push it again. */
1853 if (current_scope == global_scope)
1854 return decl;
1855 /* If this is a local declaration, make a copy; we can't have
1856 the same DECL listed in two different scopes. */
1857 return pushdecl (copy_node (decl));
1858 }
1859
1860 /* Not seen before. */
1861 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
1862 DECL_EXTERNAL (decl) = 1;
1863 TREE_PUBLIC (decl) = 1;
1864 C_DECL_IMPLICIT (decl) = 1;
1865 implicit_decl_warning (functionid);
1866
1867 /* C89 says implicit declarations are in the innermost block.
1868 So we record the decl in the standard fashion. */
1869 decl = pushdecl (decl);
1870
1871 /* No need to call objc_check_decl here - it's a function type. */
1872 rest_of_decl_compilation (decl, NULL, 0, 0);
1873
1874 /* Write a record describing this implicit function declaration
1875 to the prototypes file (if requested). */
1876 gen_aux_info_record (decl, 0, 1, 0);
1877
1878 /* Possibly apply some default attributes to this implicit declaration. */
1879 decl_attributes (&decl, NULL_TREE, 0);
1880
1881 return decl;
1882}
1883
1884static void
1885implicit_decl_warning (tree id)
1886{
1887 const char *name = IDENTIFIER_POINTER (id);
1888 if (mesg_implicit_function_declaration == 2)
1889 error ("implicit declaration of function `%s'", name);
1890 else if (mesg_implicit_function_declaration == 1)
1891 warning ("implicit declaration of function `%s'", name);
1892}
1893
1894/* Issue an error message for a reference to an undeclared variable
1895 ID, including a reference to a builtin outside of function-call
1896 context. Establish a binding of the identifier to error_mark_node
1897 in an appropriate scope, which will suppress further errors for the
1898 same identifier. */
1899void
1900undeclared_variable (tree id)
1901{
1902 static bool already = false;
1903 struct c_scope *scope;
1904
1905 if (current_function_decl == 0)
1906 {
1907 error ("`%s' undeclared here (not in a function)",
1908 IDENTIFIER_POINTER (id));
1909 scope = current_scope;
1910 }
1911 else
1912 {
1913 error ("`%s' undeclared (first use in this function)",
1914 IDENTIFIER_POINTER (id));
1915
1916 if (! already)
1917 {
1918 error ("(Each undeclared identifier is reported only once");
1919 error ("for each function it appears in.)");
1920 already = true;
1921 }
1922
1923 scope = current_function_scope;
1924 }
1925
1926 scope->shadowed = tree_cons (id, IDENTIFIER_SYMBOL_VALUE (id),
1927 scope->shadowed);
1928 IDENTIFIER_SYMBOL_VALUE (id) = error_mark_node;
1929}
1930
1931/* Subroutine of lookup_label, declare_label, define_label: construct a
1932 LABEL_DECL with all the proper frills. */
1933
1934static tree
1935make_label (tree name, location_t location)
1936{
1937 tree label = build_decl (LABEL_DECL, name, void_type_node);
1938
1939 DECL_CONTEXT (label) = current_function_decl;
1940 DECL_MODE (label) = VOIDmode;
1941 DECL_SOURCE_LOCATION (label) = location;
1942
1943 return label;
1944}
1945
1946/* Another subroutine of lookup_label, declare_label, define_label:
1947 set up the binding of name to LABEL_DECL in the given SCOPE. */
1948
1949static void
1950bind_label (tree name, tree label, struct c_scope *scope)
1951{
1952 if (IDENTIFIER_LABEL_VALUE (name))
1953 scope->shadowed = tree_cons (name, IDENTIFIER_LABEL_VALUE (name),
1954 scope->shadowed);
1955 IDENTIFIER_LABEL_VALUE (name) = label;
1956
1957 SCOPE_LIST_APPEND (scope, names, label);
1958}
1959
1960/* Get the LABEL_DECL corresponding to identifier NAME as a label.
1961 Create one if none exists so far for the current function.
1962 This is called when a label is used in a goto expression or
1963 has its address taken. */
1964
1965tree
1966lookup_label (tree name)
1967{
1968 tree label;
1969
1970 if (current_function_decl == 0)
1971 {
1972 error ("label %s referenced outside of any function",
1973 IDENTIFIER_POINTER (name));
1974 return 0;
1975 }
1976
1977 /* Use a label already defined or ref'd with this name, but not if
1978 it is inherited from a containing function and wasn't declared
1979 using __label__. */
1980 label = IDENTIFIER_LABEL_VALUE (name);
1981 if (label && (DECL_CONTEXT (label) == current_function_decl
1982 || C_DECLARED_LABEL_FLAG (label)))
1983 {
1984 /* If the label has only been declared, update its apparent
1985 location to point here, for better diagnostics if it
1986 turns out not to have been defined. */
1987 if (!TREE_USED (label))
1988 DECL_SOURCE_LOCATION (label) = input_location;
1989 return label;
1990 }
1991
1992 /* No label binding for that identifier; make one. */
1993 label = make_label (name, input_location);
1994
1995 /* Ordinary labels go in the current function scope. */
1996 bind_label (name, label, current_function_scope);
1997 return label;
1998}
1999
2000/* Make a label named NAME in the current function, shadowing silently
2001 any that may be inherited from containing functions or containing
2002 scopes. This is called for __label__ declarations. */
2003
2004/* Note that valid use, if the label being shadowed comes from another
2005 scope in the same function, requires calling declare_nonlocal_label
2006 right away. (Is this still true? -zw 2003-07-17) */
2007
2008tree
2009declare_label (tree name)
2010{
2011 tree label = IDENTIFIER_LABEL_VALUE (name);
2012 tree dup;
2013
2014 /* Check to make sure that the label hasn't already been declared
2015 at this scope */
2016 for (dup = current_scope->names; dup; dup = TREE_CHAIN (dup))
2017 if (dup == label)
2018 {
2019 error ("duplicate label declaration `%s'", IDENTIFIER_POINTER (name));
2020 error ("%Jthis is a previous declaration", dup);
2021
2022 /* Just use the previous declaration. */
2023 return dup;
2024 }
2025
2026 label = make_label (name, input_location);
2027 C_DECLARED_LABEL_FLAG (label) = 1;
2028
2029 /* Declared labels go in the current scope. */
2030 bind_label (name, label, current_scope);
2031 return label;
2032}
2033
2034/* Define a label, specifying the location in the source file.
2035 Return the LABEL_DECL node for the label, if the definition is valid.
2036 Otherwise return 0. */
2037
2038tree
2039define_label (location_t location, tree name)
2040{
2041 tree label;
2042
2043 /* Find any preexisting label with this name. It is an error
2044 if that label has already been defined in this function, or
2045 if there is a containing function with a declared label with
2046 the same name. */
2047 label = IDENTIFIER_LABEL_VALUE (name);
2048
2049 if (label
2050 && ((DECL_CONTEXT (label) == current_function_decl
2051 && DECL_INITIAL (label) != 0)
2052 || (DECL_CONTEXT (label) != current_function_decl
2053 && C_DECLARED_LABEL_FLAG (label))))
2054 {
2055 error ("%Hduplicate label `%D'", &location, label);
2056 if (DECL_INITIAL (label))
2057 error ("%J`%D' previously defined here", label, label);
2058 else
2059 error ("%J`%D' previously declared here", label, label);
2060 return 0;
2061 }
2062 else if (label && DECL_CONTEXT (label) == current_function_decl)
2063 {
2064 /* The label has been used or declared already in this function,
2065 but not defined. Update its location to point to this
2066 definition. */
2067 DECL_SOURCE_LOCATION (label) = location;
2068 }
2069 else
2070 {
2071 /* No label binding for that identifier; make one. */
2072 label = make_label (name, location);
2073
2074 /* Ordinary labels go in the current function scope. */
2075 bind_label (name, label, current_function_scope);
2076 }
2077
2078 if (warn_traditional && !in_system_header && lookup_name (name))
2079 warning ("%Htraditional C lacks a separate namespace for labels, "
2080 "identifier `%s' conflicts", &location,
2081 IDENTIFIER_POINTER (name));
2082
2083 /* Mark label as having been defined. */
2084 DECL_INITIAL (label) = error_mark_node;
2085 return label;
2086}
2087
2088/* Return the list of declarations of the current scope. */
2089
2090tree
2091getdecls (void)
2092{
2093 return current_scope->names;
2094}
2095
2096
2097/* Given NAME, an IDENTIFIER_NODE,
2098 return the structure (or union or enum) definition for that name.
2099 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2100 CODE says which kind of type the caller wants;
2101 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2102 If the wrong kind of type is found, an error is reported. */
2103
2104static tree
2105lookup_tag (enum tree_code code, tree name, int thislevel_only)
2106{
2107 tree tag = IDENTIFIER_TAG_VALUE (name);
2108 int thislevel = 0;
2109
2110 if (!tag)
2111 return 0;
2112
2113 /* We only care about whether it's in this level if
2114 thislevel_only was set or it might be a type clash. */
2115 if (thislevel_only || TREE_CODE (tag) != code)
2116 {
2117 if (current_scope == global_scope
2118 || purpose_member (name, current_scope->tags))
2119 thislevel = 1;
2120 }
2121
2122 if (thislevel_only && !thislevel)
2123 return 0;
2124
2125 if (TREE_CODE (tag) != code)
2126 {
2127 /* Definition isn't the kind we were looking for. */
2128 pending_invalid_xref = name;
2129 pending_invalid_xref_location = input_location;
2130
2131 /* If in the same binding level as a declaration as a tag
2132 of a different type, this must not be allowed to
2133 shadow that tag, so give the error immediately.
2134 (For example, "struct foo; union foo;" is invalid.) */
2135 if (thislevel)
2136 pending_xref_error ();
2137 }
2138 return tag;
2139}
2140
2141/* Print an error message now
2142 for a recent invalid struct, union or enum cross reference.
2143 We don't print them immediately because they are not invalid
2144 when used in the `struct foo;' construct for shadowing. */
2145
2146void
2147pending_xref_error (void)
2148{
2149 if (pending_invalid_xref != 0)
2150 error ("%H`%s' defined as wrong kind of tag",
2151 &pending_invalid_xref_location,
2152 IDENTIFIER_POINTER (pending_invalid_xref));
2153 pending_invalid_xref = 0;
2154}
2155
2156
2157/* Look up NAME in the current scope and its superiors
2158 in the namespace of variables, functions and typedefs.
2159 Return a ..._DECL node of some kind representing its definition,
2160 or return 0 if it is undefined. */
2161
2162tree
2163lookup_name (tree name)
2164{
2165 tree decl = IDENTIFIER_SYMBOL_VALUE (name);
2166 if (decl == 0 || decl == error_mark_node)
2167 return decl;
2168 if (C_DECL_INVISIBLE (decl))
2169 return 0;
2170 return decl;
2171}
2172
2173/* Similar to `lookup_name' but look only at the current scope. */
2174
2175static tree
2176lookup_name_current_level (tree name)
2177{
2178 tree decl = IDENTIFIER_SYMBOL_VALUE (name);
2179
2180 if (decl == 0 || decl == error_mark_node || C_DECL_INVISIBLE (decl))
2181 return 0;
2182
2183 if (current_scope == global_scope)
2184 return decl;
2185
2186 /* Scan the current scope for a decl with name NAME.
2187 For PARM_DECLs, we have to look at both ->parms and ->names, since
2188 forward parameter declarations wind up on the ->names list. */
2189 if (TREE_CODE (decl) == PARM_DECL
2190 && chain_member (decl, current_scope->parms))
2191 return decl;
2192 if (chain_member (decl, current_scope->names))
2193 return decl;
2194
2195 return 0;
2196}
2197
2198/* Create the predefined scalar types of C,
2199 and some nodes representing standard constants (0, 1, (void *) 0).
2200 Initialize the global scope.
2201 Make definitions for built-in primitive functions. */
2202
2203void
2204c_init_decl_processing (void)
2205{
2206 tree endlink;
2207 tree ptr_ftype_void, ptr_ftype_ptr;
2208 location_t save_loc = input_location;
2209
2210 /* Adds some ggc roots, and reserved words for c-parse.in. */
2211 c_parse_init ();
2212
2213 current_function_decl = 0;
2214
2215 /* Make the c_scope structure for global names. */
2216 pushlevel (0);
2217 global_scope = current_scope;
2218
2219 /* Declarations from c_common_nodes_and_builtins must not be associated
2220 with this input file, lest we get differences between using and not
2221 using preprocessed headers. */
2222 input_location.file = "<internal>";
2223 input_location.line = 0;
2224
2225 /* Make the DECL for the toplevel file scope. */
2226 current_file_decl = build_decl (TRANSLATION_UNIT_DECL, NULL, NULL);
2227
2228 build_common_tree_nodes (flag_signed_char);
2229
2230 c_common_nodes_and_builtins ();
2231
2232 /* In C, comparisons and TRUTH_* expressions have type int. */
2233 truthvalue_type_node = integer_type_node;
2234 truthvalue_true_node = integer_one_node;
2235 truthvalue_false_node = integer_zero_node;
2236
2237 /* Even in C99, which has a real boolean type. */
2238 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2239 boolean_type_node));
2240
2241 endlink = void_list_node;
2242 ptr_ftype_void = build_function_type (ptr_type_node, endlink);
2243 ptr_ftype_ptr
2244 = build_function_type (ptr_type_node,
2245 tree_cons (NULL_TREE, ptr_type_node, endlink));
2246
2247 input_location = save_loc;
2248
2249 pedantic_lvalues = pedantic;
2250
2251 make_fname_decl = c_make_fname_decl;
2252 start_fname_decls ();
2253
2254 first_builtin_decl = global_scope->names;
2255 last_builtin_decl = global_scope->names_last;
2256}
2257
2258/* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2259 decl, NAME is the initialization string and TYPE_DEP indicates whether
2260 NAME depended on the type of the function. As we don't yet implement
2261 delayed emission of static data, we mark the decl as emitted
2262 so it is not placed in the output. Anything using it must therefore pull
2263 out the STRING_CST initializer directly. FIXME. */
2264
2265static tree
2266c_make_fname_decl (tree id, int type_dep)
2267{
2268 const char *name = fname_as_string (type_dep);
2269 tree decl, type, init;
2270 size_t length = strlen (name);
2271
2272 type = build_array_type
2273 (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
2274 build_index_type (size_int (length)));
2275
2276 decl = build_decl (VAR_DECL, id, type);
2277
2278 TREE_STATIC (decl) = 1;
2279 TREE_READONLY (decl) = 1;
2280 DECL_ARTIFICIAL (decl) = 1;
2281
2282 init = build_string (length + 1, name);
2283 TREE_TYPE (init) = type;
2284 DECL_INITIAL (decl) = init;
2285
2286 TREE_USED (decl) = 1;
2287
2288 if (current_function_decl)
2289 {
2290 DECL_CONTEXT (decl) = current_function_decl;
2291 IDENTIFIER_SYMBOL_VALUE (id) = decl;
2292 SCOPE_LIST_APPEND (current_function_scope, names, decl);
2293 }
2294
2295 finish_decl (decl, init, NULL_TREE);
2296
2297 return decl;
2298}
2299
2300/* Return a definition for a builtin function named NAME and whose data type
2301 is TYPE. TYPE should be a function type with argument types.
2302 FUNCTION_CODE tells later passes how to compile calls to this function.
2303 See tree.h for its possible values.
2304
2305 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2306 the name to be called if we can't opencode the function. If
2307 ATTRS is nonzero, use that for the function's attribute list. */
2308
2309tree
2310builtin_function (const char *name, tree type, int function_code,
2311 enum built_in_class class, const char *library_name,
2312 tree attrs)
2313{
2314 tree decl = build_decl (FUNCTION_DECL, get_identifier (name), type);
2315 DECL_EXTERNAL (decl) = 1;
2316 TREE_PUBLIC (decl) = 1;
2317 if (library_name)
2318 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2319 make_decl_rtl (decl, NULL);
2320 pushdecl (decl);
2321 DECL_BUILT_IN_CLASS (decl) = class;
2322 DECL_FUNCTION_CODE (decl) = function_code;
2323
2324 /* Warn if a function in the namespace for users
2325 is used without an occasion to consider it declared. */
2326 if (name[0] != '_' || name[1] != '_')
2327 C_DECL_INVISIBLE (decl) = 1;
2328
2329 /* Possibly apply some default attributes to this built-in function. */
2330 if (attrs)
2331 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2332 else
2333 decl_attributes (&decl, NULL_TREE, 0);
2334
2335 return decl;
2336}
2337
2338/* Called when a declaration is seen that contains no names to declare.
2339 If its type is a reference to a structure, union or enum inherited
2340 from a containing scope, shadow that tag name for the current scope
2341 with a forward reference.
2342 If its type defines a new named structure or union
2343 or defines an enum, it is valid but we need not do anything here.
2344 Otherwise, it is an error. */
2345
2346void
2347shadow_tag (tree declspecs)
2348{
2349 shadow_tag_warned (declspecs, 0);
2350}
2351
2352void
2353shadow_tag_warned (tree declspecs, int warned)
2354
2355
2356 /* 1 => we have done a pedwarn. 2 => we have done a warning, but
2357 no pedwarn. */
2358{
2359 int found_tag = 0;
2360 tree link;
2361 tree specs, attrs;
2362
2363 pending_invalid_xref = 0;
2364
2365 /* Remove the attributes from declspecs, since they will confuse the
2366 following code. */
2367 split_specs_attrs (declspecs, &specs, &attrs);
2368
2369 for (link = specs; link; link = TREE_CHAIN (link))
2370 {
2371 tree value = TREE_VALUE (link);
2372 enum tree_code code = TREE_CODE (value);
2373
2374 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2375 /* Used to test also that TYPE_SIZE (value) != 0.
2376 That caused warning for `struct foo;' at top level in the file. */
2377 {
2378 tree name = TYPE_NAME (value);
2379 tree t;
2380
2381 found_tag++;
2382
2383 if (name == 0)
2384 {
2385 if (warned != 1 && code != ENUMERAL_TYPE)
2386 /* Empty unnamed enum OK */
2387 {
2388 pedwarn ("unnamed struct/union that defines no instances");
2389 warned = 1;
2390 }
2391 }
2392 else
2393 {
2394 t = lookup_tag (code, name, 1);
2395
2396 if (t == 0)
2397 {
2398 t = make_node (code);
2399 pushtag (name, t);
2400 }
2401 }
2402 }
2403 else
2404 {
2405 if (!warned && ! in_system_header)
2406 {
2407 warning ("useless keyword or type name in empty declaration");
2408 warned = 2;
2409 }
2410 }
2411 }
2412
2413 if (found_tag > 1)
2414 error ("two types specified in one empty declaration");
2415
2416 if (warned != 1)
2417 {
2418 if (found_tag == 0)
2419 pedwarn ("empty declaration");
2420 }
2421}
2422
2423/* Construct an array declarator. EXPR is the expression inside [], or
2424 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
2425 to the pointer to which a parameter array is converted). STATIC_P is
2426 nonzero if "static" is inside the [], zero otherwise. VLA_UNSPEC_P
2427 is nonzero is the array is [*], a VLA of unspecified length which is
2428 nevertheless a complete type (not currently implemented by GCC),
2429 zero otherwise. The declarator is constructed as an ARRAY_REF
2430 (to be decoded by grokdeclarator), whose operand 0 is what's on the
2431 left of the [] (filled by in set_array_declarator_type) and operand 1
2432 is the expression inside; whose TREE_TYPE is the type qualifiers and
2433 which has TREE_STATIC set if "static" is used. */
2434
2435tree
2436build_array_declarator (tree expr, tree quals, int static_p, int vla_unspec_p)
2437{
2438 tree decl;
2439 decl = build_nt (ARRAY_REF, NULL_TREE, expr);
2440 TREE_TYPE (decl) = quals;
2441 TREE_STATIC (decl) = (static_p ? 1 : 0);
2442 if (pedantic && !flag_isoc99)
2443 {
2444 if (static_p || quals != NULL_TREE)
2445 pedwarn ("ISO C90 does not support `static' or type qualifiers in parameter array declarators");
2446 if (vla_unspec_p)
2447 pedwarn ("ISO C90 does not support `[*]' array declarators");
2448 }
2449 if (vla_unspec_p)
2450 warning ("GCC does not yet properly implement `[*]' array declarators");
2451 return decl;
2452}
2453
2454/* Set the type of an array declarator. DECL is the declarator, as
2455 constructed by build_array_declarator; TYPE is what appears on the left
2456 of the [] and goes in operand 0. ABSTRACT_P is nonzero if it is an
2457 abstract declarator, zero otherwise; this is used to reject static and
2458 type qualifiers in abstract declarators, where they are not in the
2459 C99 grammar. */
2460
2461tree
2462set_array_declarator_type (tree decl, tree type, int abstract_p)
2463{
2464 TREE_OPERAND (decl, 0) = type;
2465 if (abstract_p && (TREE_TYPE (decl) != NULL_TREE || TREE_STATIC (decl)))
2466 error ("static or type qualifiers in abstract declarator");
2467 return decl;
2468}
2469
2470/* Decode a "typename", such as "int **", returning a ..._TYPE node. */
2471
2472tree
2473groktypename (tree typename)
2474{
2475 tree specs, attrs;
2476
2477 if (TREE_CODE (typename) != TREE_LIST)
2478 return typename;
2479
2480 split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
2481
2482 typename = grokdeclarator (TREE_VALUE (typename), specs, TYPENAME, 0,
2483 NULL);
2484
2485 /* Apply attributes. */
2486 decl_attributes (&typename, attrs, 0);
2487
2488 return typename;
2489}
2490
2491/* Return a PARM_DECL node for a given pair of specs and declarator. */
2492
2493tree
2494groktypename_in_parm_context (tree typename)
2495{
2496 if (TREE_CODE (typename) != TREE_LIST)
2497 return typename;
2498 return grokdeclarator (TREE_VALUE (typename),
2499 TREE_PURPOSE (typename),
2500 PARM, 0, NULL);
2501}
2502
2503/* Decode a declarator in an ordinary declaration or data definition.
2504 This is called as soon as the type information and variable name
2505 have been parsed, before parsing the initializer if any.
2506 Here we create the ..._DECL node, fill in its type,
2507 and put it on the list of decls for the current context.
2508 The ..._DECL node is returned as the value.
2509
2510 Exception: for arrays where the length is not specified,
2511 the type is left null, to be filled in by `finish_decl'.
2512
2513 Function definitions do not come here; they go to start_function
2514 instead. However, external and forward declarations of functions
2515 do go through here. Structure field declarations are done by
2516 grokfield and not through here. */
2517
2518tree
2519start_decl (tree declarator, tree declspecs, int initialized, tree attributes)
2520{
2521 tree decl;
2522 tree tem;
2523
2524 /* An object declared as __attribute__((deprecated)) suppresses
2525 warnings of uses of other deprecated items. */
2526 if (lookup_attribute ("deprecated", attributes))
2527 deprecated_state = DEPRECATED_SUPPRESS;
2528
2529 decl = grokdeclarator (declarator, declspecs,
2530 NORMAL, initialized, NULL);
2531
2532 deprecated_state = DEPRECATED_NORMAL;
2533
2534 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
2535 && MAIN_NAME_P (DECL_NAME (decl)))
2536 warning ("%J'%D' is usually a function", decl, decl);
2537
2538 if (initialized)
2539 /* Is it valid for this decl to have an initializer at all?
2540 If not, set INITIALIZED to zero, which will indirectly
2541 tell `finish_decl' to ignore the initializer once it is parsed. */
2542 switch (TREE_CODE (decl))
2543 {
2544 case TYPE_DECL:
2545 error ("typedef `%s' is initialized (use __typeof__ instead)",
2546 IDENTIFIER_POINTER (DECL_NAME (decl)));
2547 initialized = 0;
2548 break;
2549
2550 case FUNCTION_DECL:
2551 error ("function `%s' is initialized like a variable",
2552 IDENTIFIER_POINTER (DECL_NAME (decl)));
2553 initialized = 0;
2554 break;
2555
2556 case PARM_DECL:
2557 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
2558 error ("parameter `%s' is initialized",
2559 IDENTIFIER_POINTER (DECL_NAME (decl)));
2560 initialized = 0;
2561 break;
2562
2563 default:
2564 /* Don't allow initializations for incomplete types
2565 except for arrays which might be completed by the initialization. */
2566
2567 /* This can happen if the array size is an undefined macro. We already
2568 gave a warning, so we don't need another one. */
2569 if (TREE_TYPE (decl) == error_mark_node)
2570 initialized = 0;
2571 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
2572 {
2573 /* A complete type is ok if size is fixed. */
2574
2575 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
2576 || C_DECL_VARIABLE_SIZE (decl))
2577 {
2578 error ("variable-sized object may not be initialized");
2579 initialized = 0;
2580 }
2581 }
2582 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
2583 {
2584 error ("variable `%s' has initializer but incomplete type",
2585 IDENTIFIER_POINTER (DECL_NAME (decl)));
2586 initialized = 0;
2587 }
2588 else if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
2589 {
2590 error ("elements of array `%s' have incomplete type",
2591 IDENTIFIER_POINTER (DECL_NAME (decl)));
2592 initialized = 0;
2593 }
2594 }
2595
2596 if (initialized)
2597 {
2598 DECL_EXTERNAL (decl) = 0;
2599 if (current_scope == global_scope)
2600 TREE_STATIC (decl) = 1;
2601
2602 /* Tell `pushdecl' this is an initialized decl
2603 even though we don't yet have the initializer expression.
2604 Also tell `finish_decl' it may store the real initializer. */
2605 DECL_INITIAL (decl) = error_mark_node;
2606 }
2607
2608 /* If this is a function declaration, write a record describing it to the
2609 prototypes file (if requested). */
2610
2611 if (TREE_CODE (decl) == FUNCTION_DECL)
2612 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
2613
2614 /* ANSI specifies that a tentative definition which is not merged with
2615 a non-tentative definition behaves exactly like a definition with an
2616 initializer equal to zero. (Section 3.7.2)
2617
2618 -fno-common gives strict ANSI behavior, though this tends to break
2619 a large body of code that grew up without this rule.
2620
2621 Thread-local variables are never common, since there's no entrenched
2622 body of code to break, and it allows more efficient variable references
2623 in the presence of dynamic linking. */
2624
2625 if (TREE_CODE (decl) == VAR_DECL
2626 && !initialized
2627 && TREE_PUBLIC (decl)
2628 && !DECL_THREAD_LOCAL (decl)
2629 && !flag_no_common)
2630 DECL_COMMON (decl) = 1;
2631
2632 /* Set attributes here so if duplicate decl, will have proper attributes. */
2633 decl_attributes (&decl, attributes, 0);
2634
2635 if (TREE_CODE (decl) == FUNCTION_DECL
2636 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
2637 {
2638 tree ce = declarator;
2639
2640 if (TREE_CODE (ce) == INDIRECT_REF)
2641 ce = TREE_OPERAND (declarator, 0);
2642 if (TREE_CODE (ce) == CALL_EXPR)
2643 {
2644 tree args = TREE_PURPOSE (TREE_OPERAND (ce, 1));
2645 for (; args; args = TREE_CHAIN (args))
2646 {
2647 tree type = TREE_TYPE (args);
2648 if (INTEGRAL_TYPE_P (type)
2649 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
2650 DECL_ARG_TYPE (args) = integer_type_node;
2651 }
2652 }
2653 }
2654
2655 if (TREE_CODE (decl) == FUNCTION_DECL
2656 && DECL_DECLARED_INLINE_P (decl)
2657 && DECL_UNINLINABLE (decl)
2658 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
2659 warning ("%Jinline function '%D' given attribute noinline", decl, decl);
2660
2661 /* Add this decl to the current scope.
2662 TEM may equal DECL or it may be a previous decl of the same name. */
2663 tem = pushdecl (decl);
2664
2665 /* For a local variable, define the RTL now. */
2666 if (current_scope != global_scope
2667 /* But not if this is a duplicate decl
2668 and we preserved the rtl from the previous one
2669 (which may or may not happen). */
2670 && !DECL_RTL_SET_P (tem)
2671 && DECL_FILE_SCOPE_P (tem))
2672 {
2673 if (TREE_TYPE (tem) != error_mark_node
2674 && (COMPLETE_TYPE_P (TREE_TYPE (tem))
2675 || (TREE_CODE (TREE_TYPE (tem)) == ARRAY_TYPE
2676 && DECL_INITIAL (tem) != 0)))
2677 expand_decl (tem);
2678 }
2679
2680 return tem;
2681}
2682
2683/* Finish processing of a declaration;
2684 install its initial value.
2685 If the length of an array type is not known before,
2686 it must be determined now, from the initial value, or it is an error. */
2687
2688void
2689finish_decl (tree decl, tree init, tree asmspec_tree)
2690{
2691 tree type = TREE_TYPE (decl);
2692 int was_incomplete = (DECL_SIZE (decl) == 0);
2693 const char *asmspec = 0;
2694
2695 /* If a name was specified, get the string. */
2696 if (current_scope == global_scope)
2697 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
2698 if (asmspec_tree)
2699 asmspec = TREE_STRING_POINTER (asmspec_tree);
2700
2701 /* If `start_decl' didn't like having an initialization, ignore it now. */
2702 if (init != 0 && DECL_INITIAL (decl) == 0)
2703 init = 0;
2704
2705 /* Don't crash if parm is initialized. */
2706 if (TREE_CODE (decl) == PARM_DECL)
2707 init = 0;
2708
2709 if (init)
2710 store_init_value (decl, init);
2711
2712 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
2713 || TREE_CODE (decl) == FUNCTION_DECL
2714 || TREE_CODE (decl) == FIELD_DECL))
2715 objc_check_decl (decl);
2716
2717 /* Deduce size of array from initialization, if not already known. */
2718 if (TREE_CODE (type) == ARRAY_TYPE
2719 && TYPE_DOMAIN (type) == 0
2720 && TREE_CODE (decl) != TYPE_DECL)
2721 {
2722 int do_default
2723 = (TREE_STATIC (decl)
2724 /* Even if pedantic, an external linkage array
2725 may have incomplete type at first. */
2726 ? pedantic && !TREE_PUBLIC (decl)
2727 : !DECL_EXTERNAL (decl));
2728 int failure
2729 = complete_array_type (type, DECL_INITIAL (decl), do_default);
2730
2731 /* Get the completed type made by complete_array_type. */
2732 type = TREE_TYPE (decl);
2733
2734 if (failure == 1)
2735 error ("%Jinitializer fails to determine size of '%D'", decl, decl);
2736
2737 else if (failure == 2)
2738 {
2739 if (do_default)
2740 error ("%Jarray size missing in '%D'", decl, decl);
2741 /* If a `static' var's size isn't known,
2742 make it extern as well as static, so it does not get
2743 allocated.
2744 If it is not `static', then do not mark extern;
2745 finish_incomplete_decl will give it a default size
2746 and it will get allocated. */
2747 else if (!pedantic && TREE_STATIC (decl) && ! TREE_PUBLIC (decl))
2748 DECL_EXTERNAL (decl) = 1;
2749 }
2750
2751 /* TYPE_MAX_VALUE is always one less than the number of elements
2752 in the array, because we start counting at zero. Therefore,
2753 warn only if the value is less than zero. */
2754 else if (pedantic && TYPE_DOMAIN (type) != 0
2755 && tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < 0)
2756 error ("%Jzero or negative size array '%D'", decl, decl);
2757
2758 layout_decl (decl, 0);
2759 }
2760
2761 if (TREE_CODE (decl) == VAR_DECL)
2762 {
2763 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
2764 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
2765 layout_decl (decl, 0);
2766
2767 if (DECL_SIZE (decl) == 0
2768 /* Don't give an error if we already gave one earlier. */
2769 && TREE_TYPE (decl) != error_mark_node
2770 && (TREE_STATIC (decl)
2771 ?
2772 /* A static variable with an incomplete type
2773 is an error if it is initialized.
2774 Also if it is not file scope.
2775 Otherwise, let it through, but if it is not `extern'
2776 then it may cause an error message later. */
2777 (DECL_INITIAL (decl) != 0
2778 || !DECL_FILE_SCOPE_P (decl))
2779 :
2780 /* An automatic variable with an incomplete type
2781 is an error. */
2782 !DECL_EXTERNAL (decl)))
2783 {
2784 error ("%Jstorage size of '%D' isn't known", decl, decl);
2785 TREE_TYPE (decl) = error_mark_node;
2786 }
2787
2788 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
2789 && DECL_SIZE (decl) != 0)
2790 {
2791 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
2792 constant_expression_warning (DECL_SIZE (decl));
2793 else
2794 error ("%Jstorage size of '%D' isn't constant", decl, decl);
2795 }
2796
2797 if (TREE_USED (type))
2798 TREE_USED (decl) = 1;
2799 }
2800
2801 /* If this is a function and an assembler name is specified, reset DECL_RTL
2802 so we can give it its new name. Also, update built_in_decls if it
2803 was a normal built-in. */
2804 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
2805 {
2806 /* ASMSPEC is given, and not the name of a register. Mark the
2807 name with a star so assemble_name won't munge it. */
2808 char *starred = alloca (strlen (asmspec) + 2);
2809 starred[0] = '*';
2810 strcpy (starred + 1, asmspec);
2811
2812 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
2813 {
2814 tree builtin = built_in_decls [DECL_FUNCTION_CODE (decl)];
2815 SET_DECL_RTL (builtin, NULL_RTX);
2816 SET_DECL_ASSEMBLER_NAME (builtin, get_identifier (starred));
2817#ifdef TARGET_MEM_FUNCTIONS
2818 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMCPY)
2819 init_block_move_fn (starred);
2820 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMSET)
2821 init_block_clear_fn (starred);
2822#else
2823 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BCOPY)
2824 init_block_move_fn (starred);
2825 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BZERO)
2826 init_block_clear_fn (starred);
2827#endif
2828 }
2829 SET_DECL_RTL (decl, NULL_RTX);
2830 change_decl_assembler_name (decl, get_identifier (starred));
2831 }
2832
2833 /* If #pragma weak was used, mark the decl weak now. */
2834 if (current_scope == global_scope)
2835 maybe_apply_pragma_weak (decl);
2836
2837 /* Output the assembler code and/or RTL code for variables and functions,
2838 unless the type is an undefined structure or union.
2839 If not, it will get done when the type is completed. */
2840
2841 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
2842 {
2843 /* This is a no-op in c-lang.c or something real in objc-act.c. */
2844 if (c_dialect_objc ())
2845 objc_check_decl (decl);
2846
2847 if (DECL_FILE_SCOPE_P (decl))
2848 {
2849 if (DECL_INITIAL (decl) == NULL_TREE
2850 || DECL_INITIAL (decl) == error_mark_node)
2851 /* Don't output anything
2852 when a tentative file-scope definition is seen.
2853 But at end of compilation, do output code for them. */
2854 DECL_DEFER_OUTPUT (decl) = 1;
2855 rest_of_decl_compilation (decl, asmspec, true, 0);
2856 }
2857 else
2858 {
2859 /* This is a local variable. If there is an ASMSPEC, the
2860 user has requested that we handle it specially. */
2861 if (asmspec)
2862 {
2863 /* In conjunction with an ASMSPEC, the `register'
2864 keyword indicates that we should place the variable
2865 in a particular register. */
2866 if (DECL_REGISTER (decl))
2867 DECL_C_HARD_REGISTER (decl) = 1;
2868
2869 /* If this is not a static variable, issue a warning.
2870 It doesn't make any sense to give an ASMSPEC for an
2871 ordinary, non-register local variable. Historically,
2872 GCC has accepted -- but ignored -- the ASMSPEC in
2873 this case. */
2874 if (TREE_CODE (decl) == VAR_DECL
2875 && !DECL_REGISTER (decl)
2876 && !TREE_STATIC (decl))
2877 warning ("%Jignoring asm-specifier for non-static local "
2878 "variable '%D'", decl, decl);
2879 else
2880 change_decl_assembler_name (decl, get_identifier (asmspec));
2881 }
2882
2883 if (TREE_CODE (decl) != FUNCTION_DECL)
2884 add_decl_stmt (decl);
2885 }
2886
2887 if (!DECL_FILE_SCOPE_P (decl))
2888 {
2889 /* Recompute the RTL of a local array now
2890 if it used to be an incomplete type. */
2891 if (was_incomplete
2892 && ! TREE_STATIC (decl) && ! DECL_EXTERNAL (decl))
2893 {
2894 /* If we used it already as memory, it must stay in memory. */
2895 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
2896 /* If it's still incomplete now, no init will save it. */
2897 if (DECL_SIZE (decl) == 0)
2898 DECL_INITIAL (decl) = 0;
2899 }
2900 }
2901 }
2902
2903 /* If this was marked 'used', be sure it will be output. */
2904 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
2905 mark_referenced (DECL_ASSEMBLER_NAME (decl));
2906
2907 if (TREE_CODE (decl) == TYPE_DECL)
2908 rest_of_decl_compilation (decl, NULL, DECL_FILE_SCOPE_P (decl), 0);
2909
2910 /* At the end of a declaration, throw away any variable type sizes
2911 of types defined inside that declaration. There is no use
2912 computing them in the following function definition. */
2913 if (current_scope == global_scope)
2914 get_pending_sizes ();
2915
2916 /* Install a cleanup (aka destructor) if one was given. */
2917 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
2918 {
2919 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
2920 if (attr)
2921 {
2922 static bool eh_initialized_p;
2923
2924 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
2925 tree cleanup_decl = lookup_name (cleanup_id);
2926 tree cleanup;
2927
2928 /* Build "cleanup(&decl)" for the destructor. */
2929 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
2930 cleanup = build_tree_list (NULL_TREE, cleanup);
2931 cleanup = build_function_call (cleanup_decl, cleanup);
2932
2933 /* Don't warn about decl unused; the cleanup uses it. */
2934 TREE_USED (decl) = 1;
2935
2936 /* Initialize EH, if we've been told to do so. */
2937 if (flag_exceptions && !eh_initialized_p)
2938 {
2939 eh_initialized_p = true;
2940 eh_personality_libfunc
2941 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
2942 ? "__gcc_personality_sj0"
2943 : "__gcc_personality_v0");
2944 using_eh_for_cleanups ();
2945 }
2946
2947 add_stmt (build_stmt (CLEANUP_STMT, decl, cleanup));
2948 }
2949 }
2950}
2951
2952/* Given a parsed parameter declaration, decode it into a PARM_DECL
2953 and push that on the current scope. */
2954
2955void
2956push_parm_decl (tree parm)
2957{
2958 tree decl;
969 if (!comptypes (oldtype, newtype, COMPARE_STRICT))
970 {
971 if (TREE_CODE (olddecl) == FUNCTION_DECL
972 && DECL_BUILT_IN (olddecl) && C_DECL_INVISIBLE (olddecl))
973 {
974 /* Accept harmless mismatch in function types.
975 This is for the ffs and fprintf builtins. */
976 tree trytype = match_builtin_function_types (newtype, oldtype);
977
978 if (trytype && comptypes (newtype, trytype, COMPARE_STRICT))
979 *oldtypep = oldtype = trytype;
980 else
981 {
982 /* If types don't match for a built-in, throw away the
983 built-in. No point in calling locate_old_decl here, it
984 won't print anything. */
985 warning ("%Jconflicting types for built-in function '%D'",
986 newdecl, newdecl);
987 return false;
988 }
989 }
990 else if (TREE_CODE (olddecl) == FUNCTION_DECL
991 && DECL_SOURCE_LINE (olddecl) == 0)
992 {
993 /* A conflicting function declaration for a predeclared
994 function that isn't actually built in. Objective C uses
995 these. The new declaration silently overrides everything
996 but the volatility (i.e. noreturn) indication. See also
997 below. FIXME: Make Objective C use normal builtins. */
998 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
999 return false;
1000 }
1001 /* Permit void foo (...) to match int foo (...) if the latter is
1002 the definition and implicit int was used. See
1003 c-torture/compile/920625-2.c. */
1004 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1005 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1006 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1007 && C_FUNCTION_IMPLICIT_INT (newdecl))
1008 {
1009 pedwarn ("%Jconflicting types for '%D'", newdecl, newdecl);
1010 /* Make sure we keep void as the return type. */
1011 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1012 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1013 pedwarned = true;
1014 }
1015 else
1016 {
1017 error ("%Jconflicting types for '%D'", newdecl, newdecl);
1018 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1019 locate_old_decl (olddecl, error);
1020 return false;
1021 }
1022 }
1023
1024 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1025 but silently ignore the redeclaration if either is in a system
1026 header. (Conflicting redeclarations were handled above.) */
1027 if (TREE_CODE (newdecl) == TYPE_DECL)
1028 {
1029 if (DECL_IN_SYSTEM_HEADER (newdecl) || DECL_IN_SYSTEM_HEADER (olddecl))
1030 return true; /* allow OLDDECL to continue in use */
1031
1032 error ("%Jredefinition of typedef '%D'", newdecl, newdecl);
1033 locate_old_decl (olddecl, error);
1034 return false;
1035 }
1036
1037 /* Function declarations can either be 'static' or 'extern' (no
1038 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1039 can never conflict with each other on account of linkage (6.2.2p4).
1040 Multiple definitions are not allowed (6.9p3,5) but GCC permits
1041 two definitions if one is 'extern inline' and one is not. The non-
1042 extern-inline definition supersedes the extern-inline definition. */
1043 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1044 {
1045 /* If you declare a built-in function name as static, or
1046 define the built-in with an old-style definition (so we
1047 can't validate the argument list) the built-in definition is
1048 overridden, but optionally warn this was a bad choice of name. */
1049 if (DECL_BUILT_IN (olddecl)
1050 && C_DECL_INVISIBLE (olddecl)
1051 && (!TREE_PUBLIC (newdecl)
1052 || (DECL_INITIAL (newdecl)
1053 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1054 {
1055 if (warn_shadow)
1056 warning ("%Jshadowing built-in function '%D'", newdecl, newdecl);
1057 /* Discard the old built-in function. */
1058 return false;
1059 }
1060
1061 if (DECL_INITIAL (newdecl))
1062 {
1063 if (DECL_INITIAL (olddecl)
1064 && !(DECL_DECLARED_INLINE_P (olddecl)
1065 && DECL_EXTERNAL (olddecl)
1066 && !(DECL_DECLARED_INLINE_P (newdecl)
1067 && DECL_EXTERNAL (newdecl))))
1068 {
1069 error ("%Jredefinition of '%D'", newdecl, newdecl);
1070 locate_old_decl (olddecl, error);
1071 return false;
1072 }
1073 }
1074 /* If we have a prototype after an old-style function definition,
1075 the argument types must be checked specially. */
1076 else if (DECL_INITIAL (olddecl)
1077 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1078 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1079 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1080 {
1081 locate_old_decl (olddecl, error);
1082 return false;
1083 }
1084 /* Mismatched non-static and static is considered poor style.
1085 We only diagnose static then non-static if -Wtraditional,
1086 because it is the most convenient way to get some effects
1087 (see e.g. what unwind-dw2-fde-glibc.c does to the definition
1088 of _Unwind_Find_FDE in unwind-dw2-fde.c). Revisit? */
1089 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1090 {
1091 /* A static function declaration for a predeclared function
1092 that isn't actually built in, silently overrides the
1093 default. Objective C uses these. See also above.
1094 FIXME: Make Objective C use normal builtins. */
1095 if (TREE_CODE (olddecl) == FUNCTION_DECL
1096 && DECL_SOURCE_LINE (olddecl) == 0)
1097 return false;
1098 else
1099 {
1100 warning ("%Jstatic declaration of '%D' follows "
1101 "non-static declaration", newdecl, newdecl);
1102 warned = true;
1103 }
1104 }
1105 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl)
1106 && warn_traditional)
1107 {
1108 warning ("%Jnon-static declaration of '%D' follows "
1109 "static declaration", newdecl, newdecl);
1110 warned = true;
1111 }
1112 }
1113 else if (TREE_CODE (newdecl) == VAR_DECL)
1114 {
1115 /* Only variables can be thread-local, and all declarations must
1116 agree on this property. */
1117 if (DECL_THREAD_LOCAL (newdecl) != DECL_THREAD_LOCAL (olddecl))
1118 {
1119 if (DECL_THREAD_LOCAL (newdecl))
1120 error ("%Jthread-local declaration of '%D' follows "
1121 "non-thread-local declaration", newdecl, newdecl);
1122 else
1123 error ("%Jnon-thread-local declaration of '%D' follows "
1124 "thread-local declaration", newdecl, newdecl);
1125
1126 locate_old_decl (olddecl, error);
1127 return false;
1128 }
1129
1130 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1131 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1132 {
1133 error ("%Jredefinition of '%D'", newdecl, newdecl);
1134 locate_old_decl (olddecl, error);
1135 return false;
1136 }
1137
1138 /* Objects declared at file scope: if at least one is 'extern',
1139 it's fine (6.2.2p4); otherwise the linkage must agree (6.2.2p7). */
1140 if (DECL_FILE_SCOPE_P (newdecl))
1141 {
1142 if (!DECL_EXTERNAL (newdecl)
1143 && !DECL_EXTERNAL (olddecl)
1144 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1145 {
1146 if (TREE_PUBLIC (newdecl))
1147 error ("%Jnon-static declaration of '%D' follows "
1148 "static declaration", newdecl, newdecl);
1149 else
1150 error ("%Jstatic declaration of '%D' follows "
1151 "non-static declaration", newdecl, newdecl);
1152
1153 locate_old_decl (olddecl, error);
1154 return false;
1155 }
1156 }
1157 /* Two objects with the same name declared at the same block
1158 scope must both be external references (6.7p3). */
1159 else if (DECL_CONTEXT (newdecl) == DECL_CONTEXT (olddecl)
1160 && (!DECL_EXTERNAL (newdecl) || !DECL_EXTERNAL (olddecl)))
1161 {
1162 if (DECL_EXTERNAL (newdecl))
1163 error ("%Jextern declaration of '%D' follows "
1164 "declaration with no linkage", newdecl, newdecl);
1165 else if (DECL_EXTERNAL (olddecl))
1166 error ("%Jdeclaration of '%D' with no linkage follows "
1167 "extern declaration", newdecl, newdecl);
1168 else
1169 error ("%Jredeclaration of '%D' with no linkage",
1170 newdecl, newdecl);
1171
1172 locate_old_decl (olddecl, error);
1173 return false;
1174 }
1175 }
1176
1177 /* warnings */
1178 /* All decls must agree on a non-default visibility. */
1179 if (DECL_VISIBILITY (newdecl) != VISIBILITY_DEFAULT
1180 && DECL_VISIBILITY (olddecl) != VISIBILITY_DEFAULT
1181 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1182 {
1183 warning ("%Jredeclaration of '%D' with different visibility "
1184 "(old visibility preserved)", newdecl, newdecl);
1185 warned = true;
1186 }
1187
1188 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1189 {
1190 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
1191 if (DECL_DECLARED_INLINE_P (newdecl)
1192 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1193 {
1194 warning ("%Jinline declaration of '%D' follows "
1195 "declaration with attribute noinline", newdecl, newdecl);
1196 warned = true;
1197 }
1198 else if (DECL_DECLARED_INLINE_P (olddecl)
1199 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1200 {
1201 warning ("%Jdeclaration of '%D' with attribute noinline follows "
1202 "inline declaration ", newdecl, newdecl);
1203 warned = true;
1204 }
1205
1206 /* Inline declaration after use or definition.
1207 ??? Should we still warn about this now we have unit-at-a-time
1208 mode and can get it right? */
1209 if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl))
1210 {
1211 if (TREE_USED (olddecl))
1212 {
1213 warning ("%J'%D' declared inline after being called",
1214 olddecl, olddecl);
1215 warned = true;
1216 }
1217 else if (DECL_INITIAL (olddecl))
1218 {
1219 warning ("%J'%D' declared inline after its definition",
1220 olddecl, olddecl);
1221 warned = true;
1222 }
1223 }
1224 }
1225 else /* PARM_DECL, VAR_DECL */
1226 {
1227 /* Redeclaration of a PARM_DECL is invalid unless this is the
1228 real position of a forward-declared parameter (GCC extension). */
1229 if (TREE_CODE (newdecl) == PARM_DECL
1230 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1231 {
1232 error ("%Jredefinition of parameter '%D'", newdecl, newdecl);
1233 locate_old_decl (olddecl, error);
1234 return false;
1235 }
1236
1237 /* These bits are only type qualifiers when applied to objects. */
1238 if (TREE_THIS_VOLATILE (newdecl) != TREE_THIS_VOLATILE (olddecl))
1239 {
1240 if (TREE_THIS_VOLATILE (newdecl))
1241 pedwarn ("%Jvolatile declaration of '%D' follows "
1242 "non-volatile declaration", newdecl, newdecl);
1243 else
1244 pedwarn ("%Jnon-volatile declaration of '%D' follows "
1245 "volatile declaration", newdecl, newdecl);
1246 pedwarned = true;
1247 }
1248 if (TREE_READONLY (newdecl) != TREE_READONLY (olddecl))
1249 {
1250 if (TREE_READONLY (newdecl))
1251 pedwarn ("%Jconst declaration of '%D' follows "
1252 "non-const declaration", newdecl, newdecl);
1253 else
1254 pedwarn ("%Jnon-const declaration of '%D' follows "
1255 "const declaration", newdecl, newdecl);
1256 pedwarned = true;
1257 }
1258 }
1259
1260 /* Optional warning for completely redundant decls. */
1261 if (!warned && !pedwarned
1262 && warn_redundant_decls
1263 /* Don't warn about a function declaration followed by a
1264 definition. */
1265 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1266 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1267 /* Don't warn about redundant redeclarations of builtins. */
1268 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1269 && !DECL_BUILT_IN (newdecl)
1270 && DECL_BUILT_IN (olddecl)
1271 && C_DECL_INVISIBLE (olddecl))
1272 /* Don't warn about an extern followed by a definition. */
1273 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1274 /* Don't warn about forward parameter decls. */
1275 && !(TREE_CODE (newdecl) == PARM_DECL
1276 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1277 /* Don't warn about a variable definition following a declaration. */
1278 && !(TREE_CODE (newdecl) == VAR_DECL
1279 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
1280 {
1281 warning ("%Jredundant redeclaration of '%D'", newdecl, newdecl);
1282 warned = true;
1283 }
1284
1285 /* Report location of previous decl/defn in a consistent manner. */
1286 if (warned || pedwarned)
1287 locate_old_decl (olddecl, pedwarned ? pedwarn : warning);
1288
1289 return true;
1290}
1291
1292/* Subroutine of duplicate_decls. NEWDECL has been found to be
1293 consistent with OLDDECL, but carries new information. Merge the
1294 new information into OLDDECL. This function issues no
1295 diagnostics. */
1296
1297static void
1298merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1299{
1300 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1301 && DECL_INITIAL (newdecl) != 0);
1302
1303 /* For real parm decl following a forward decl, return 1 so old decl
1304 will be reused. Only allow this to happen once. */
1305 if (TREE_CODE (newdecl) == PARM_DECL
1306 && TREE_ASM_WRITTEN (olddecl) && ! TREE_ASM_WRITTEN (newdecl))
1307 {
1308 TREE_ASM_WRITTEN (olddecl) = 0;
1309 return;
1310 }
1311
1312 DECL_ATTRIBUTES (newdecl)
1313 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
1314
1315 /* Merge the data types specified in the two decls. */
1316 TREE_TYPE (newdecl)
1317 = TREE_TYPE (olddecl)
1318 = common_type (newtype, oldtype);
1319
1320 /* Lay the type out, unless already done. */
1321 if (oldtype != TREE_TYPE (newdecl))
1322 {
1323 if (TREE_TYPE (newdecl) != error_mark_node)
1324 layout_type (TREE_TYPE (newdecl));
1325 if (TREE_CODE (newdecl) != FUNCTION_DECL
1326 && TREE_CODE (newdecl) != TYPE_DECL
1327 && TREE_CODE (newdecl) != CONST_DECL)
1328 layout_decl (newdecl, 0);
1329 }
1330 else
1331 {
1332 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1333 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1334 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1335 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1336 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1337 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1338 {
1339 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1340 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1341 }
1342 }
1343
1344 /* Keep the old rtl since we can safely use it. */
1345 COPY_DECL_RTL (olddecl, newdecl);
1346
1347 /* Merge the type qualifiers. */
1348 if (TREE_READONLY (newdecl))
1349 TREE_READONLY (olddecl) = 1;
1350
1351 if (TREE_THIS_VOLATILE (newdecl))
1352 {
1353 TREE_THIS_VOLATILE (olddecl) = 1;
1354 if (TREE_CODE (newdecl) == VAR_DECL)
1355 make_var_volatile (newdecl);
1356 }
1357
1358 /* Keep source location of definition rather than declaration. */
1359 if (DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1360 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1361
1362 /* Merge the unused-warning information. */
1363 if (DECL_IN_SYSTEM_HEADER (olddecl))
1364 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1365 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1366 DECL_IN_SYSTEM_HEADER (olddecl) = 1;
1367
1368 /* Merge the initialization information. */
1369 if (DECL_INITIAL (newdecl) == 0)
1370 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1371
1372 /* Merge the section attribute.
1373 We want to issue an error if the sections conflict but that must be
1374 done later in decl_attributes since we are called before attributes
1375 are assigned. */
1376 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1377 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1378
1379 /* Copy the assembler name.
1380 Currently, it can only be defined in the prototype. */
1381 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1382
1383 /* If either declaration has a nondefault visibility, use it. */
1384 if (DECL_VISIBILITY (olddecl) != VISIBILITY_DEFAULT)
1385 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1386
1387 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1388 {
1389 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1390 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1391 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1392 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1393 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1394 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1395 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1396 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1397 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1398 }
1399
1400 /* Merge the storage class information. */
1401 merge_weak (newdecl, olddecl);
1402
1403 /* For functions, static overrides non-static. */
1404 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1405 {
1406 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1407 /* This is since we don't automatically
1408 copy the attributes of NEWDECL into OLDDECL. */
1409 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1410 /* If this clears `static', clear it in the identifier too. */
1411 if (! TREE_PUBLIC (olddecl))
1412 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1413 }
1414 if (DECL_EXTERNAL (newdecl))
1415 {
1416 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1417 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1418
1419 /* An extern decl does not override previous storage class. */
1420 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1421 if (! DECL_EXTERNAL (newdecl))
1422 {
1423 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1424 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1425 }
1426 }
1427 else
1428 {
1429 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1430 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1431 }
1432
1433 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1434 {
1435 /* If we're redefining a function previously defined as extern
1436 inline, make sure we emit debug info for the inline before we
1437 throw it away, in case it was inlined into a function that hasn't
1438 been written out yet. */
1439 if (new_is_definition && DECL_INITIAL (olddecl))
1440 {
1441 if (TREE_USED (olddecl)
1442 /* In unit-at-a-time mode we never inline re-defined extern
1443 inline functions. */
1444 && !flag_unit_at_a_time
1445 && cgraph_function_possibly_inlined_p (olddecl))
1446 (*debug_hooks->outlining_inline_function) (olddecl);
1447
1448 /* The new defn must not be inline. */
1449 DECL_INLINE (newdecl) = 0;
1450 DECL_UNINLINABLE (newdecl) = 1;
1451 }
1452 else
1453 {
1454 /* If either decl says `inline', this fn is inline,
1455 unless its definition was passed already. */
1456 if (DECL_DECLARED_INLINE_P (newdecl)
1457 || DECL_DECLARED_INLINE_P (olddecl))
1458 DECL_DECLARED_INLINE_P (newdecl) = 1;
1459
1460 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1461 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1462 }
1463
1464 if (DECL_BUILT_IN (olddecl))
1465 {
1466 /* If redeclaring a builtin function, it stays built in. */
1467 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1468 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1469 }
1470
1471 /* Also preserve various other info from the definition. */
1472 if (! new_is_definition)
1473 {
1474 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1475 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1476 DECL_SAVED_INSNS (newdecl) = DECL_SAVED_INSNS (olddecl);
1477 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1478 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1479
1480 /* Set DECL_INLINE on the declaration if we've got a body
1481 from which to instantiate. */
1482 if (DECL_INLINE (olddecl) && ! DECL_UNINLINABLE (newdecl))
1483 {
1484 DECL_INLINE (newdecl) = 1;
1485 DECL_ABSTRACT_ORIGIN (newdecl)
1486 = DECL_ABSTRACT_ORIGIN (olddecl);
1487 }
1488 }
1489 else
1490 {
1491 /* If a previous declaration said inline, mark the
1492 definition as inlinable. */
1493 if (DECL_DECLARED_INLINE_P (newdecl)
1494 && ! DECL_UNINLINABLE (newdecl))
1495 DECL_INLINE (newdecl) = 1;
1496 }
1497 }
1498
1499 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1500 But preserve OLDDECL's DECL_UID and C_DECL_INVISIBLE. */
1501 {
1502 unsigned olddecl_uid = DECL_UID (olddecl);
1503 unsigned olddecl_invisible = C_DECL_INVISIBLE (olddecl);
1504
1505 memcpy ((char *) olddecl + sizeof (struct tree_common),
1506 (char *) newdecl + sizeof (struct tree_common),
1507 sizeof (struct tree_decl) - sizeof (struct tree_common));
1508 DECL_UID (olddecl) = olddecl_uid;
1509 C_DECL_INVISIBLE (olddecl) = olddecl_invisible;
1510 }
1511
1512 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1513 so that encode_section_info has a chance to look at the new decl
1514 flags and attributes. */
1515 if (DECL_RTL_SET_P (olddecl)
1516 && (TREE_CODE (olddecl) == FUNCTION_DECL
1517 || (TREE_CODE (olddecl) == VAR_DECL
1518 && TREE_STATIC (olddecl))))
1519 make_decl_rtl (olddecl, NULL);
1520}
1521
1522/* Handle when a new declaration NEWDECL has the same name as an old
1523 one OLDDECL in the same binding contour. Prints an error message
1524 if appropriate.
1525
1526 If safely possible, alter OLDDECL to look like NEWDECL, and return
1527 true. Otherwise, return false. */
1528
1529static bool
1530duplicate_decls (tree newdecl, tree olddecl)
1531{
1532 tree newtype, oldtype;
1533
1534 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
1535 return false;
1536
1537 merge_decls (newdecl, olddecl, newtype, oldtype);
1538 return true;
1539}
1540
1541
1542/* Return any external DECL associated with ID, whether or not it is
1543 currently in scope. */
1544
1545static tree
1546any_external_decl (tree id)
1547{
1548 tree decl = IDENTIFIER_SYMBOL_VALUE (id);
1549 tree t;
1550
1551 if (decl == 0 || TREE_CODE (decl) == ERROR_MARK)
1552 return 0;
1553 else if (TREE_CODE (decl) != TYPE_DECL && DECL_EXTERNAL (decl))
1554 return decl;
1555
1556 t = purpose_member (id, truly_local_externals);
1557 if (t)
1558 return TREE_VALUE (t);
1559
1560 return 0;
1561}
1562
1563/* Record an external decl DECL. This only does something if a
1564 shadowing decl already exists. */
1565static void
1566record_external_decl (tree decl)
1567{
1568 tree name = DECL_NAME (decl);
1569 if (!IDENTIFIER_SYMBOL_VALUE (name))
1570 return;
1571
1572 truly_local_externals = tree_cons (name, decl, truly_local_externals);
1573}
1574
1575/* Check whether decl-node X shadows an existing declaration.
1576 OLD is the old IDENTIFIER_SYMBOL_VALUE of the DECL_NAME of X,
1577 which might be a NULL_TREE. */
1578static void
1579warn_if_shadowing (tree x, tree old)
1580{
1581 /* Nothing to shadow? */
1582 if (old == 0
1583 /* Shadow warnings not wanted? */
1584 || !warn_shadow
1585 /* No shadow warnings for internally generated vars. */
1586 || DECL_SOURCE_LINE (x) == 0
1587 /* No shadow warnings for vars made for inlining. */
1588 || DECL_FROM_INLINE (x)
1589 /* Don't warn about the parm names in function declarator
1590 within a function declarator.
1591 It would be nice to avoid warning in any function
1592 declarator in a declaration, as opposed to a definition,
1593 but there is no way to tell it's not a definition. */
1594 || (TREE_CODE (x) == PARM_DECL && current_scope->outer->parm_flag)
1595 /* Shadow warnings only apply to local variables and parameters. */
1596 || (TREE_CODE (x) != PARM_DECL && DECL_FILE_SCOPE_P (x)))
1597 return;
1598
1599 if (TREE_CODE (old) == PARM_DECL)
1600 warning ("%Jdeclaration of '%D' shadows a parameter", x, x);
1601 else if (DECL_FILE_SCOPE_P (old))
1602 warning ("%Jdeclaration of '%D' shadows a global declaration", x, x);
1603 else
1604 warning ("%Jdeclaration of '%D' shadows a previous local", x, x);
1605
1606 warning ("%Jshadowed declaration is here", old);
1607}
1608
1609
1610/* Subroutine of pushdecl.
1611
1612 X is a TYPE_DECL for a typedef statement. Create a brand new
1613 ..._TYPE node (which will be just a variant of the existing
1614 ..._TYPE node with identical properties) and then install X
1615 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1616
1617 The whole point here is to end up with a situation where each
1618 and every ..._TYPE node the compiler creates will be uniquely
1619 associated with AT MOST one node representing a typedef name.
1620 This way, even though the compiler substitutes corresponding
1621 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1622 early on, later parts of the compiler can always do the reverse
1623 translation and get back the corresponding typedef name. For
1624 example, given:
1625
1626 typedef struct S MY_TYPE;
1627 MY_TYPE object;
1628
1629 Later parts of the compiler might only know that `object' was of
1630 type `struct S' if it were not for code just below. With this
1631 code however, later parts of the compiler see something like:
1632
1633 struct S' == struct S
1634 typedef struct S' MY_TYPE;
1635 struct S' object;
1636
1637 And they can then deduce (from the node for type struct S') that
1638 the original object declaration was:
1639
1640 MY_TYPE object;
1641
1642 Being able to do this is important for proper support of protoize,
1643 and also for generating precise symbolic debugging information
1644 which takes full account of the programmer's (typedef) vocabulary.
1645
1646 Obviously, we don't want to generate a duplicate ..._TYPE node if
1647 the TYPE_DECL node that we are now processing really represents a
1648 standard built-in type.
1649
1650 Since all standard types are effectively declared at line zero
1651 in the source file, we can easily check to see if we are working
1652 on a standard type by checking the current value of lineno. */
1653
1654static void
1655clone_underlying_type (tree x)
1656{
1657 if (DECL_SOURCE_LINE (x) == 0)
1658 {
1659 if (TYPE_NAME (TREE_TYPE (x)) == 0)
1660 TYPE_NAME (TREE_TYPE (x)) = x;
1661 }
1662 else if (TREE_TYPE (x) != error_mark_node
1663 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
1664 {
1665 tree tt = TREE_TYPE (x);
1666 DECL_ORIGINAL_TYPE (x) = tt;
1667 tt = build_type_copy (tt);
1668 TYPE_NAME (tt) = x;
1669 TREE_USED (tt) = TREE_USED (x);
1670 TREE_TYPE (x) = tt;
1671 }
1672}
1673
1674/* Record a decl-node X as belonging to the current lexical scope.
1675 Check for errors (such as an incompatible declaration for the same
1676 name already seen in the same scope).
1677
1678 Returns either X or an old decl for the same name.
1679 If an old decl is returned, it may have been smashed
1680 to agree with what X says. */
1681
1682tree
1683pushdecl (tree x)
1684{
1685 tree name = DECL_NAME (x);
1686 struct c_scope *scope = current_scope;
1687
1688#ifdef ENABLE_CHECKING
1689 if (error_mark_node == 0)
1690 /* Called too early. */
1691 abort ();
1692#endif
1693
1694 /* Functions need the lang_decl data. */
1695 if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_LANG_SPECIFIC (x))
1696 DECL_LANG_SPECIFIC (x) = ggc_alloc_cleared (sizeof (struct lang_decl));
1697
1698 /* A local extern declaration for a function doesn't constitute nesting.
1699 A local auto declaration does, since it's a forward decl
1700 for a nested function coming later. */
1701 if (current_function_decl == NULL
1702 || ((TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
1703 && DECL_INITIAL (x) == 0 && DECL_EXTERNAL (x)))
1704 DECL_CONTEXT (x) = current_file_decl;
1705 else
1706 DECL_CONTEXT (x) = current_function_decl;
1707
1708 if (name)
1709 {
1710 tree old;
1711
1712 if (warn_nested_externs
1713 && scope != global_scope
1714 && DECL_EXTERNAL (x)
1715 && !DECL_IN_SYSTEM_HEADER (x))
1716 warning ("nested extern declaration of `%s'",
1717 IDENTIFIER_POINTER (name));
1718
1719 old = lookup_name_current_level (name);
1720 if (old && duplicate_decls (x, old))
1721 {
1722 /* For PARM_DECLs, old may be a forward declaration.
1723 If so, we want to remove it from its old location
1724 (in the variables chain) and rechain it in the
1725 location given by the new declaration. */
1726 if (TREE_CODE (x) == PARM_DECL)
1727 {
1728 tree *p;
1729 for (p = &scope->names; *p; p = &TREE_CHAIN (*p))
1730 if (*p == old)
1731 {
1732 *p = TREE_CHAIN (old);
1733 SCOPE_LIST_APPEND (scope, parms, old);
1734 break;
1735 }
1736 }
1737 return old;
1738 }
1739 if (DECL_EXTERNAL (x) || scope == global_scope)
1740 {
1741 /* Find and check against a previous, not-in-scope, external
1742 decl for this identifier. (C99 6.2.7p2: All declarations
1743 that refer to the same object or function shall have
1744 compatible type; otherwise, the behavior is undefined.) */
1745 tree ext = any_external_decl (name);
1746 if (ext)
1747 {
1748 if (duplicate_decls (x, ext))
1749 x = copy_node (ext);
1750 }
1751 else
1752 record_external_decl (x);
1753 }
1754
1755 if (TREE_CODE (x) == TYPE_DECL)
1756 clone_underlying_type (x);
1757
1758 /* If storing a local value, there may already be one
1759 (inherited). If so, record it for restoration when this
1760 scope ends. Take care not to do this if we are replacing an
1761 older decl in the same scope (i.e. duplicate_decls returned
1762 false, above). */
1763 if (scope != global_scope)
1764 {
1765 tree inherited_decl = lookup_name (name);
1766 if (inherited_decl && inherited_decl != old)
1767 {
1768 warn_if_shadowing (x, inherited_decl);
1769 scope->shadowed = tree_cons (name, inherited_decl,
1770 scope->shadowed);
1771 }
1772 }
1773
1774 /* Install the new declaration in the requested scope. */
1775 IDENTIFIER_SYMBOL_VALUE (name) = x;
1776 C_DECL_INVISIBLE (x) = 0;
1777
1778 /* If x's type is incomplete because it's based on a
1779 structure or union which has not yet been fully declared,
1780 attach it to that structure or union type, so we can go
1781 back and complete the variable declaration later, if the
1782 structure or union gets fully declared.
1783
1784 If the input is erroneous, we can have error_mark in the type
1785 slot (e.g. "f(void a, ...)") - that doesn't count as an
1786 incomplete type. */
1787 if (TREE_TYPE (x) != error_mark_node
1788 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
1789 {
1790 tree element = TREE_TYPE (x);
1791
1792 while (TREE_CODE (element) == ARRAY_TYPE)
1793 element = TREE_TYPE (element);
1794 element = TYPE_MAIN_VARIANT (element);
1795
1796 if ((TREE_CODE (element) == RECORD_TYPE
1797 || TREE_CODE (element) == UNION_TYPE)
1798 && (TREE_CODE (x) != TYPE_DECL
1799 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
1800 && !COMPLETE_TYPE_P (element))
1801 C_TYPE_INCOMPLETE_VARS (element)
1802 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
1803 }
1804 }
1805
1806 if (TREE_CODE (x) == PARM_DECL)
1807 SCOPE_LIST_APPEND (scope, parms, x);
1808 else
1809 SCOPE_LIST_APPEND (scope, names, x);
1810
1811 return x;
1812}
1813
1814/* Record X as belonging to the global scope (C99 "file scope").
1815 This is used only internally by the Objective-C front end,
1816 and is limited to its needs. duplicate_decls is not called;
1817 if there is any preexisting decl for this identifier, it is an ICE. */
1818
1819tree
1820pushdecl_top_level (tree x)
1821{
1822 tree name;
1823
1824 if (TREE_CODE (x) != VAR_DECL)
1825 abort ();
1826
1827 name = DECL_NAME (x);
1828
1829 if (IDENTIFIER_SYMBOL_VALUE (name))
1830 abort ();
1831
1832 DECL_CONTEXT (x) = current_file_decl;
1833 IDENTIFIER_SYMBOL_VALUE (name) = x;
1834
1835 SCOPE_LIST_APPEND (global_scope, names, x);
1836 return x;
1837}
1838
1839/* Generate an implicit declaration for identifier FUNCTIONID as a
1840 function of type int (). */
1841
1842tree
1843implicitly_declare (tree functionid)
1844{
1845 tree decl = any_external_decl (functionid);
1846
1847 if (decl)
1848 {
1849 /* Implicit declaration of a function already declared
1850 (somehow) in a different scope, or as a built-in.
1851 If this is the first time this has happened, warn;
1852 then recycle the old declaration. */
1853 if (!C_DECL_IMPLICIT (decl))
1854 {
1855 implicit_decl_warning (DECL_NAME (decl));
1856 if (! DECL_FILE_SCOPE_P (decl))
1857 warning ("%Jprevious declaration of '%D'", decl, decl);
1858 C_DECL_IMPLICIT (decl) = 1;
1859 }
1860 /* If this function is global, then it must already be in the
1861 global scope, so there's no need to push it again. */
1862 if (current_scope == global_scope)
1863 return decl;
1864 /* If this is a local declaration, make a copy; we can't have
1865 the same DECL listed in two different scopes. */
1866 return pushdecl (copy_node (decl));
1867 }
1868
1869 /* Not seen before. */
1870 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
1871 DECL_EXTERNAL (decl) = 1;
1872 TREE_PUBLIC (decl) = 1;
1873 C_DECL_IMPLICIT (decl) = 1;
1874 implicit_decl_warning (functionid);
1875
1876 /* C89 says implicit declarations are in the innermost block.
1877 So we record the decl in the standard fashion. */
1878 decl = pushdecl (decl);
1879
1880 /* No need to call objc_check_decl here - it's a function type. */
1881 rest_of_decl_compilation (decl, NULL, 0, 0);
1882
1883 /* Write a record describing this implicit function declaration
1884 to the prototypes file (if requested). */
1885 gen_aux_info_record (decl, 0, 1, 0);
1886
1887 /* Possibly apply some default attributes to this implicit declaration. */
1888 decl_attributes (&decl, NULL_TREE, 0);
1889
1890 return decl;
1891}
1892
1893static void
1894implicit_decl_warning (tree id)
1895{
1896 const char *name = IDENTIFIER_POINTER (id);
1897 if (mesg_implicit_function_declaration == 2)
1898 error ("implicit declaration of function `%s'", name);
1899 else if (mesg_implicit_function_declaration == 1)
1900 warning ("implicit declaration of function `%s'", name);
1901}
1902
1903/* Issue an error message for a reference to an undeclared variable
1904 ID, including a reference to a builtin outside of function-call
1905 context. Establish a binding of the identifier to error_mark_node
1906 in an appropriate scope, which will suppress further errors for the
1907 same identifier. */
1908void
1909undeclared_variable (tree id)
1910{
1911 static bool already = false;
1912 struct c_scope *scope;
1913
1914 if (current_function_decl == 0)
1915 {
1916 error ("`%s' undeclared here (not in a function)",
1917 IDENTIFIER_POINTER (id));
1918 scope = current_scope;
1919 }
1920 else
1921 {
1922 error ("`%s' undeclared (first use in this function)",
1923 IDENTIFIER_POINTER (id));
1924
1925 if (! already)
1926 {
1927 error ("(Each undeclared identifier is reported only once");
1928 error ("for each function it appears in.)");
1929 already = true;
1930 }
1931
1932 scope = current_function_scope;
1933 }
1934
1935 scope->shadowed = tree_cons (id, IDENTIFIER_SYMBOL_VALUE (id),
1936 scope->shadowed);
1937 IDENTIFIER_SYMBOL_VALUE (id) = error_mark_node;
1938}
1939
1940/* Subroutine of lookup_label, declare_label, define_label: construct a
1941 LABEL_DECL with all the proper frills. */
1942
1943static tree
1944make_label (tree name, location_t location)
1945{
1946 tree label = build_decl (LABEL_DECL, name, void_type_node);
1947
1948 DECL_CONTEXT (label) = current_function_decl;
1949 DECL_MODE (label) = VOIDmode;
1950 DECL_SOURCE_LOCATION (label) = location;
1951
1952 return label;
1953}
1954
1955/* Another subroutine of lookup_label, declare_label, define_label:
1956 set up the binding of name to LABEL_DECL in the given SCOPE. */
1957
1958static void
1959bind_label (tree name, tree label, struct c_scope *scope)
1960{
1961 if (IDENTIFIER_LABEL_VALUE (name))
1962 scope->shadowed = tree_cons (name, IDENTIFIER_LABEL_VALUE (name),
1963 scope->shadowed);
1964 IDENTIFIER_LABEL_VALUE (name) = label;
1965
1966 SCOPE_LIST_APPEND (scope, names, label);
1967}
1968
1969/* Get the LABEL_DECL corresponding to identifier NAME as a label.
1970 Create one if none exists so far for the current function.
1971 This is called when a label is used in a goto expression or
1972 has its address taken. */
1973
1974tree
1975lookup_label (tree name)
1976{
1977 tree label;
1978
1979 if (current_function_decl == 0)
1980 {
1981 error ("label %s referenced outside of any function",
1982 IDENTIFIER_POINTER (name));
1983 return 0;
1984 }
1985
1986 /* Use a label already defined or ref'd with this name, but not if
1987 it is inherited from a containing function and wasn't declared
1988 using __label__. */
1989 label = IDENTIFIER_LABEL_VALUE (name);
1990 if (label && (DECL_CONTEXT (label) == current_function_decl
1991 || C_DECLARED_LABEL_FLAG (label)))
1992 {
1993 /* If the label has only been declared, update its apparent
1994 location to point here, for better diagnostics if it
1995 turns out not to have been defined. */
1996 if (!TREE_USED (label))
1997 DECL_SOURCE_LOCATION (label) = input_location;
1998 return label;
1999 }
2000
2001 /* No label binding for that identifier; make one. */
2002 label = make_label (name, input_location);
2003
2004 /* Ordinary labels go in the current function scope. */
2005 bind_label (name, label, current_function_scope);
2006 return label;
2007}
2008
2009/* Make a label named NAME in the current function, shadowing silently
2010 any that may be inherited from containing functions or containing
2011 scopes. This is called for __label__ declarations. */
2012
2013/* Note that valid use, if the label being shadowed comes from another
2014 scope in the same function, requires calling declare_nonlocal_label
2015 right away. (Is this still true? -zw 2003-07-17) */
2016
2017tree
2018declare_label (tree name)
2019{
2020 tree label = IDENTIFIER_LABEL_VALUE (name);
2021 tree dup;
2022
2023 /* Check to make sure that the label hasn't already been declared
2024 at this scope */
2025 for (dup = current_scope->names; dup; dup = TREE_CHAIN (dup))
2026 if (dup == label)
2027 {
2028 error ("duplicate label declaration `%s'", IDENTIFIER_POINTER (name));
2029 error ("%Jthis is a previous declaration", dup);
2030
2031 /* Just use the previous declaration. */
2032 return dup;
2033 }
2034
2035 label = make_label (name, input_location);
2036 C_DECLARED_LABEL_FLAG (label) = 1;
2037
2038 /* Declared labels go in the current scope. */
2039 bind_label (name, label, current_scope);
2040 return label;
2041}
2042
2043/* Define a label, specifying the location in the source file.
2044 Return the LABEL_DECL node for the label, if the definition is valid.
2045 Otherwise return 0. */
2046
2047tree
2048define_label (location_t location, tree name)
2049{
2050 tree label;
2051
2052 /* Find any preexisting label with this name. It is an error
2053 if that label has already been defined in this function, or
2054 if there is a containing function with a declared label with
2055 the same name. */
2056 label = IDENTIFIER_LABEL_VALUE (name);
2057
2058 if (label
2059 && ((DECL_CONTEXT (label) == current_function_decl
2060 && DECL_INITIAL (label) != 0)
2061 || (DECL_CONTEXT (label) != current_function_decl
2062 && C_DECLARED_LABEL_FLAG (label))))
2063 {
2064 error ("%Hduplicate label `%D'", &location, label);
2065 if (DECL_INITIAL (label))
2066 error ("%J`%D' previously defined here", label, label);
2067 else
2068 error ("%J`%D' previously declared here", label, label);
2069 return 0;
2070 }
2071 else if (label && DECL_CONTEXT (label) == current_function_decl)
2072 {
2073 /* The label has been used or declared already in this function,
2074 but not defined. Update its location to point to this
2075 definition. */
2076 DECL_SOURCE_LOCATION (label) = location;
2077 }
2078 else
2079 {
2080 /* No label binding for that identifier; make one. */
2081 label = make_label (name, location);
2082
2083 /* Ordinary labels go in the current function scope. */
2084 bind_label (name, label, current_function_scope);
2085 }
2086
2087 if (warn_traditional && !in_system_header && lookup_name (name))
2088 warning ("%Htraditional C lacks a separate namespace for labels, "
2089 "identifier `%s' conflicts", &location,
2090 IDENTIFIER_POINTER (name));
2091
2092 /* Mark label as having been defined. */
2093 DECL_INITIAL (label) = error_mark_node;
2094 return label;
2095}
2096
2097/* Return the list of declarations of the current scope. */
2098
2099tree
2100getdecls (void)
2101{
2102 return current_scope->names;
2103}
2104
2105
2106/* Given NAME, an IDENTIFIER_NODE,
2107 return the structure (or union or enum) definition for that name.
2108 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2109 CODE says which kind of type the caller wants;
2110 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2111 If the wrong kind of type is found, an error is reported. */
2112
2113static tree
2114lookup_tag (enum tree_code code, tree name, int thislevel_only)
2115{
2116 tree tag = IDENTIFIER_TAG_VALUE (name);
2117 int thislevel = 0;
2118
2119 if (!tag)
2120 return 0;
2121
2122 /* We only care about whether it's in this level if
2123 thislevel_only was set or it might be a type clash. */
2124 if (thislevel_only || TREE_CODE (tag) != code)
2125 {
2126 if (current_scope == global_scope
2127 || purpose_member (name, current_scope->tags))
2128 thislevel = 1;
2129 }
2130
2131 if (thislevel_only && !thislevel)
2132 return 0;
2133
2134 if (TREE_CODE (tag) != code)
2135 {
2136 /* Definition isn't the kind we were looking for. */
2137 pending_invalid_xref = name;
2138 pending_invalid_xref_location = input_location;
2139
2140 /* If in the same binding level as a declaration as a tag
2141 of a different type, this must not be allowed to
2142 shadow that tag, so give the error immediately.
2143 (For example, "struct foo; union foo;" is invalid.) */
2144 if (thislevel)
2145 pending_xref_error ();
2146 }
2147 return tag;
2148}
2149
2150/* Print an error message now
2151 for a recent invalid struct, union or enum cross reference.
2152 We don't print them immediately because they are not invalid
2153 when used in the `struct foo;' construct for shadowing. */
2154
2155void
2156pending_xref_error (void)
2157{
2158 if (pending_invalid_xref != 0)
2159 error ("%H`%s' defined as wrong kind of tag",
2160 &pending_invalid_xref_location,
2161 IDENTIFIER_POINTER (pending_invalid_xref));
2162 pending_invalid_xref = 0;
2163}
2164
2165
2166/* Look up NAME in the current scope and its superiors
2167 in the namespace of variables, functions and typedefs.
2168 Return a ..._DECL node of some kind representing its definition,
2169 or return 0 if it is undefined. */
2170
2171tree
2172lookup_name (tree name)
2173{
2174 tree decl = IDENTIFIER_SYMBOL_VALUE (name);
2175 if (decl == 0 || decl == error_mark_node)
2176 return decl;
2177 if (C_DECL_INVISIBLE (decl))
2178 return 0;
2179 return decl;
2180}
2181
2182/* Similar to `lookup_name' but look only at the current scope. */
2183
2184static tree
2185lookup_name_current_level (tree name)
2186{
2187 tree decl = IDENTIFIER_SYMBOL_VALUE (name);
2188
2189 if (decl == 0 || decl == error_mark_node || C_DECL_INVISIBLE (decl))
2190 return 0;
2191
2192 if (current_scope == global_scope)
2193 return decl;
2194
2195 /* Scan the current scope for a decl with name NAME.
2196 For PARM_DECLs, we have to look at both ->parms and ->names, since
2197 forward parameter declarations wind up on the ->names list. */
2198 if (TREE_CODE (decl) == PARM_DECL
2199 && chain_member (decl, current_scope->parms))
2200 return decl;
2201 if (chain_member (decl, current_scope->names))
2202 return decl;
2203
2204 return 0;
2205}
2206
2207/* Create the predefined scalar types of C,
2208 and some nodes representing standard constants (0, 1, (void *) 0).
2209 Initialize the global scope.
2210 Make definitions for built-in primitive functions. */
2211
2212void
2213c_init_decl_processing (void)
2214{
2215 tree endlink;
2216 tree ptr_ftype_void, ptr_ftype_ptr;
2217 location_t save_loc = input_location;
2218
2219 /* Adds some ggc roots, and reserved words for c-parse.in. */
2220 c_parse_init ();
2221
2222 current_function_decl = 0;
2223
2224 /* Make the c_scope structure for global names. */
2225 pushlevel (0);
2226 global_scope = current_scope;
2227
2228 /* Declarations from c_common_nodes_and_builtins must not be associated
2229 with this input file, lest we get differences between using and not
2230 using preprocessed headers. */
2231 input_location.file = "<internal>";
2232 input_location.line = 0;
2233
2234 /* Make the DECL for the toplevel file scope. */
2235 current_file_decl = build_decl (TRANSLATION_UNIT_DECL, NULL, NULL);
2236
2237 build_common_tree_nodes (flag_signed_char);
2238
2239 c_common_nodes_and_builtins ();
2240
2241 /* In C, comparisons and TRUTH_* expressions have type int. */
2242 truthvalue_type_node = integer_type_node;
2243 truthvalue_true_node = integer_one_node;
2244 truthvalue_false_node = integer_zero_node;
2245
2246 /* Even in C99, which has a real boolean type. */
2247 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2248 boolean_type_node));
2249
2250 endlink = void_list_node;
2251 ptr_ftype_void = build_function_type (ptr_type_node, endlink);
2252 ptr_ftype_ptr
2253 = build_function_type (ptr_type_node,
2254 tree_cons (NULL_TREE, ptr_type_node, endlink));
2255
2256 input_location = save_loc;
2257
2258 pedantic_lvalues = pedantic;
2259
2260 make_fname_decl = c_make_fname_decl;
2261 start_fname_decls ();
2262
2263 first_builtin_decl = global_scope->names;
2264 last_builtin_decl = global_scope->names_last;
2265}
2266
2267/* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2268 decl, NAME is the initialization string and TYPE_DEP indicates whether
2269 NAME depended on the type of the function. As we don't yet implement
2270 delayed emission of static data, we mark the decl as emitted
2271 so it is not placed in the output. Anything using it must therefore pull
2272 out the STRING_CST initializer directly. FIXME. */
2273
2274static tree
2275c_make_fname_decl (tree id, int type_dep)
2276{
2277 const char *name = fname_as_string (type_dep);
2278 tree decl, type, init;
2279 size_t length = strlen (name);
2280
2281 type = build_array_type
2282 (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
2283 build_index_type (size_int (length)));
2284
2285 decl = build_decl (VAR_DECL, id, type);
2286
2287 TREE_STATIC (decl) = 1;
2288 TREE_READONLY (decl) = 1;
2289 DECL_ARTIFICIAL (decl) = 1;
2290
2291 init = build_string (length + 1, name);
2292 TREE_TYPE (init) = type;
2293 DECL_INITIAL (decl) = init;
2294
2295 TREE_USED (decl) = 1;
2296
2297 if (current_function_decl)
2298 {
2299 DECL_CONTEXT (decl) = current_function_decl;
2300 IDENTIFIER_SYMBOL_VALUE (id) = decl;
2301 SCOPE_LIST_APPEND (current_function_scope, names, decl);
2302 }
2303
2304 finish_decl (decl, init, NULL_TREE);
2305
2306 return decl;
2307}
2308
2309/* Return a definition for a builtin function named NAME and whose data type
2310 is TYPE. TYPE should be a function type with argument types.
2311 FUNCTION_CODE tells later passes how to compile calls to this function.
2312 See tree.h for its possible values.
2313
2314 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2315 the name to be called if we can't opencode the function. If
2316 ATTRS is nonzero, use that for the function's attribute list. */
2317
2318tree
2319builtin_function (const char *name, tree type, int function_code,
2320 enum built_in_class class, const char *library_name,
2321 tree attrs)
2322{
2323 tree decl = build_decl (FUNCTION_DECL, get_identifier (name), type);
2324 DECL_EXTERNAL (decl) = 1;
2325 TREE_PUBLIC (decl) = 1;
2326 if (library_name)
2327 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2328 make_decl_rtl (decl, NULL);
2329 pushdecl (decl);
2330 DECL_BUILT_IN_CLASS (decl) = class;
2331 DECL_FUNCTION_CODE (decl) = function_code;
2332
2333 /* Warn if a function in the namespace for users
2334 is used without an occasion to consider it declared. */
2335 if (name[0] != '_' || name[1] != '_')
2336 C_DECL_INVISIBLE (decl) = 1;
2337
2338 /* Possibly apply some default attributes to this built-in function. */
2339 if (attrs)
2340 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2341 else
2342 decl_attributes (&decl, NULL_TREE, 0);
2343
2344 return decl;
2345}
2346
2347/* Called when a declaration is seen that contains no names to declare.
2348 If its type is a reference to a structure, union or enum inherited
2349 from a containing scope, shadow that tag name for the current scope
2350 with a forward reference.
2351 If its type defines a new named structure or union
2352 or defines an enum, it is valid but we need not do anything here.
2353 Otherwise, it is an error. */
2354
2355void
2356shadow_tag (tree declspecs)
2357{
2358 shadow_tag_warned (declspecs, 0);
2359}
2360
2361void
2362shadow_tag_warned (tree declspecs, int warned)
2363
2364
2365 /* 1 => we have done a pedwarn. 2 => we have done a warning, but
2366 no pedwarn. */
2367{
2368 int found_tag = 0;
2369 tree link;
2370 tree specs, attrs;
2371
2372 pending_invalid_xref = 0;
2373
2374 /* Remove the attributes from declspecs, since they will confuse the
2375 following code. */
2376 split_specs_attrs (declspecs, &specs, &attrs);
2377
2378 for (link = specs; link; link = TREE_CHAIN (link))
2379 {
2380 tree value = TREE_VALUE (link);
2381 enum tree_code code = TREE_CODE (value);
2382
2383 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2384 /* Used to test also that TYPE_SIZE (value) != 0.
2385 That caused warning for `struct foo;' at top level in the file. */
2386 {
2387 tree name = TYPE_NAME (value);
2388 tree t;
2389
2390 found_tag++;
2391
2392 if (name == 0)
2393 {
2394 if (warned != 1 && code != ENUMERAL_TYPE)
2395 /* Empty unnamed enum OK */
2396 {
2397 pedwarn ("unnamed struct/union that defines no instances");
2398 warned = 1;
2399 }
2400 }
2401 else
2402 {
2403 t = lookup_tag (code, name, 1);
2404
2405 if (t == 0)
2406 {
2407 t = make_node (code);
2408 pushtag (name, t);
2409 }
2410 }
2411 }
2412 else
2413 {
2414 if (!warned && ! in_system_header)
2415 {
2416 warning ("useless keyword or type name in empty declaration");
2417 warned = 2;
2418 }
2419 }
2420 }
2421
2422 if (found_tag > 1)
2423 error ("two types specified in one empty declaration");
2424
2425 if (warned != 1)
2426 {
2427 if (found_tag == 0)
2428 pedwarn ("empty declaration");
2429 }
2430}
2431
2432/* Construct an array declarator. EXPR is the expression inside [], or
2433 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
2434 to the pointer to which a parameter array is converted). STATIC_P is
2435 nonzero if "static" is inside the [], zero otherwise. VLA_UNSPEC_P
2436 is nonzero is the array is [*], a VLA of unspecified length which is
2437 nevertheless a complete type (not currently implemented by GCC),
2438 zero otherwise. The declarator is constructed as an ARRAY_REF
2439 (to be decoded by grokdeclarator), whose operand 0 is what's on the
2440 left of the [] (filled by in set_array_declarator_type) and operand 1
2441 is the expression inside; whose TREE_TYPE is the type qualifiers and
2442 which has TREE_STATIC set if "static" is used. */
2443
2444tree
2445build_array_declarator (tree expr, tree quals, int static_p, int vla_unspec_p)
2446{
2447 tree decl;
2448 decl = build_nt (ARRAY_REF, NULL_TREE, expr);
2449 TREE_TYPE (decl) = quals;
2450 TREE_STATIC (decl) = (static_p ? 1 : 0);
2451 if (pedantic && !flag_isoc99)
2452 {
2453 if (static_p || quals != NULL_TREE)
2454 pedwarn ("ISO C90 does not support `static' or type qualifiers in parameter array declarators");
2455 if (vla_unspec_p)
2456 pedwarn ("ISO C90 does not support `[*]' array declarators");
2457 }
2458 if (vla_unspec_p)
2459 warning ("GCC does not yet properly implement `[*]' array declarators");
2460 return decl;
2461}
2462
2463/* Set the type of an array declarator. DECL is the declarator, as
2464 constructed by build_array_declarator; TYPE is what appears on the left
2465 of the [] and goes in operand 0. ABSTRACT_P is nonzero if it is an
2466 abstract declarator, zero otherwise; this is used to reject static and
2467 type qualifiers in abstract declarators, where they are not in the
2468 C99 grammar. */
2469
2470tree
2471set_array_declarator_type (tree decl, tree type, int abstract_p)
2472{
2473 TREE_OPERAND (decl, 0) = type;
2474 if (abstract_p && (TREE_TYPE (decl) != NULL_TREE || TREE_STATIC (decl)))
2475 error ("static or type qualifiers in abstract declarator");
2476 return decl;
2477}
2478
2479/* Decode a "typename", such as "int **", returning a ..._TYPE node. */
2480
2481tree
2482groktypename (tree typename)
2483{
2484 tree specs, attrs;
2485
2486 if (TREE_CODE (typename) != TREE_LIST)
2487 return typename;
2488
2489 split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
2490
2491 typename = grokdeclarator (TREE_VALUE (typename), specs, TYPENAME, 0,
2492 NULL);
2493
2494 /* Apply attributes. */
2495 decl_attributes (&typename, attrs, 0);
2496
2497 return typename;
2498}
2499
2500/* Return a PARM_DECL node for a given pair of specs and declarator. */
2501
2502tree
2503groktypename_in_parm_context (tree typename)
2504{
2505 if (TREE_CODE (typename) != TREE_LIST)
2506 return typename;
2507 return grokdeclarator (TREE_VALUE (typename),
2508 TREE_PURPOSE (typename),
2509 PARM, 0, NULL);
2510}
2511
2512/* Decode a declarator in an ordinary declaration or data definition.
2513 This is called as soon as the type information and variable name
2514 have been parsed, before parsing the initializer if any.
2515 Here we create the ..._DECL node, fill in its type,
2516 and put it on the list of decls for the current context.
2517 The ..._DECL node is returned as the value.
2518
2519 Exception: for arrays where the length is not specified,
2520 the type is left null, to be filled in by `finish_decl'.
2521
2522 Function definitions do not come here; they go to start_function
2523 instead. However, external and forward declarations of functions
2524 do go through here. Structure field declarations are done by
2525 grokfield and not through here. */
2526
2527tree
2528start_decl (tree declarator, tree declspecs, int initialized, tree attributes)
2529{
2530 tree decl;
2531 tree tem;
2532
2533 /* An object declared as __attribute__((deprecated)) suppresses
2534 warnings of uses of other deprecated items. */
2535 if (lookup_attribute ("deprecated", attributes))
2536 deprecated_state = DEPRECATED_SUPPRESS;
2537
2538 decl = grokdeclarator (declarator, declspecs,
2539 NORMAL, initialized, NULL);
2540
2541 deprecated_state = DEPRECATED_NORMAL;
2542
2543 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
2544 && MAIN_NAME_P (DECL_NAME (decl)))
2545 warning ("%J'%D' is usually a function", decl, decl);
2546
2547 if (initialized)
2548 /* Is it valid for this decl to have an initializer at all?
2549 If not, set INITIALIZED to zero, which will indirectly
2550 tell `finish_decl' to ignore the initializer once it is parsed. */
2551 switch (TREE_CODE (decl))
2552 {
2553 case TYPE_DECL:
2554 error ("typedef `%s' is initialized (use __typeof__ instead)",
2555 IDENTIFIER_POINTER (DECL_NAME (decl)));
2556 initialized = 0;
2557 break;
2558
2559 case FUNCTION_DECL:
2560 error ("function `%s' is initialized like a variable",
2561 IDENTIFIER_POINTER (DECL_NAME (decl)));
2562 initialized = 0;
2563 break;
2564
2565 case PARM_DECL:
2566 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
2567 error ("parameter `%s' is initialized",
2568 IDENTIFIER_POINTER (DECL_NAME (decl)));
2569 initialized = 0;
2570 break;
2571
2572 default:
2573 /* Don't allow initializations for incomplete types
2574 except for arrays which might be completed by the initialization. */
2575
2576 /* This can happen if the array size is an undefined macro. We already
2577 gave a warning, so we don't need another one. */
2578 if (TREE_TYPE (decl) == error_mark_node)
2579 initialized = 0;
2580 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
2581 {
2582 /* A complete type is ok if size is fixed. */
2583
2584 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
2585 || C_DECL_VARIABLE_SIZE (decl))
2586 {
2587 error ("variable-sized object may not be initialized");
2588 initialized = 0;
2589 }
2590 }
2591 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
2592 {
2593 error ("variable `%s' has initializer but incomplete type",
2594 IDENTIFIER_POINTER (DECL_NAME (decl)));
2595 initialized = 0;
2596 }
2597 else if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
2598 {
2599 error ("elements of array `%s' have incomplete type",
2600 IDENTIFIER_POINTER (DECL_NAME (decl)));
2601 initialized = 0;
2602 }
2603 }
2604
2605 if (initialized)
2606 {
2607 DECL_EXTERNAL (decl) = 0;
2608 if (current_scope == global_scope)
2609 TREE_STATIC (decl) = 1;
2610
2611 /* Tell `pushdecl' this is an initialized decl
2612 even though we don't yet have the initializer expression.
2613 Also tell `finish_decl' it may store the real initializer. */
2614 DECL_INITIAL (decl) = error_mark_node;
2615 }
2616
2617 /* If this is a function declaration, write a record describing it to the
2618 prototypes file (if requested). */
2619
2620 if (TREE_CODE (decl) == FUNCTION_DECL)
2621 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
2622
2623 /* ANSI specifies that a tentative definition which is not merged with
2624 a non-tentative definition behaves exactly like a definition with an
2625 initializer equal to zero. (Section 3.7.2)
2626
2627 -fno-common gives strict ANSI behavior, though this tends to break
2628 a large body of code that grew up without this rule.
2629
2630 Thread-local variables are never common, since there's no entrenched
2631 body of code to break, and it allows more efficient variable references
2632 in the presence of dynamic linking. */
2633
2634 if (TREE_CODE (decl) == VAR_DECL
2635 && !initialized
2636 && TREE_PUBLIC (decl)
2637 && !DECL_THREAD_LOCAL (decl)
2638 && !flag_no_common)
2639 DECL_COMMON (decl) = 1;
2640
2641 /* Set attributes here so if duplicate decl, will have proper attributes. */
2642 decl_attributes (&decl, attributes, 0);
2643
2644 if (TREE_CODE (decl) == FUNCTION_DECL
2645 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
2646 {
2647 tree ce = declarator;
2648
2649 if (TREE_CODE (ce) == INDIRECT_REF)
2650 ce = TREE_OPERAND (declarator, 0);
2651 if (TREE_CODE (ce) == CALL_EXPR)
2652 {
2653 tree args = TREE_PURPOSE (TREE_OPERAND (ce, 1));
2654 for (; args; args = TREE_CHAIN (args))
2655 {
2656 tree type = TREE_TYPE (args);
2657 if (INTEGRAL_TYPE_P (type)
2658 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
2659 DECL_ARG_TYPE (args) = integer_type_node;
2660 }
2661 }
2662 }
2663
2664 if (TREE_CODE (decl) == FUNCTION_DECL
2665 && DECL_DECLARED_INLINE_P (decl)
2666 && DECL_UNINLINABLE (decl)
2667 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
2668 warning ("%Jinline function '%D' given attribute noinline", decl, decl);
2669
2670 /* Add this decl to the current scope.
2671 TEM may equal DECL or it may be a previous decl of the same name. */
2672 tem = pushdecl (decl);
2673
2674 /* For a local variable, define the RTL now. */
2675 if (current_scope != global_scope
2676 /* But not if this is a duplicate decl
2677 and we preserved the rtl from the previous one
2678 (which may or may not happen). */
2679 && !DECL_RTL_SET_P (tem)
2680 && DECL_FILE_SCOPE_P (tem))
2681 {
2682 if (TREE_TYPE (tem) != error_mark_node
2683 && (COMPLETE_TYPE_P (TREE_TYPE (tem))
2684 || (TREE_CODE (TREE_TYPE (tem)) == ARRAY_TYPE
2685 && DECL_INITIAL (tem) != 0)))
2686 expand_decl (tem);
2687 }
2688
2689 return tem;
2690}
2691
2692/* Finish processing of a declaration;
2693 install its initial value.
2694 If the length of an array type is not known before,
2695 it must be determined now, from the initial value, or it is an error. */
2696
2697void
2698finish_decl (tree decl, tree init, tree asmspec_tree)
2699{
2700 tree type = TREE_TYPE (decl);
2701 int was_incomplete = (DECL_SIZE (decl) == 0);
2702 const char *asmspec = 0;
2703
2704 /* If a name was specified, get the string. */
2705 if (current_scope == global_scope)
2706 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
2707 if (asmspec_tree)
2708 asmspec = TREE_STRING_POINTER (asmspec_tree);
2709
2710 /* If `start_decl' didn't like having an initialization, ignore it now. */
2711 if (init != 0 && DECL_INITIAL (decl) == 0)
2712 init = 0;
2713
2714 /* Don't crash if parm is initialized. */
2715 if (TREE_CODE (decl) == PARM_DECL)
2716 init = 0;
2717
2718 if (init)
2719 store_init_value (decl, init);
2720
2721 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
2722 || TREE_CODE (decl) == FUNCTION_DECL
2723 || TREE_CODE (decl) == FIELD_DECL))
2724 objc_check_decl (decl);
2725
2726 /* Deduce size of array from initialization, if not already known. */
2727 if (TREE_CODE (type) == ARRAY_TYPE
2728 && TYPE_DOMAIN (type) == 0
2729 && TREE_CODE (decl) != TYPE_DECL)
2730 {
2731 int do_default
2732 = (TREE_STATIC (decl)
2733 /* Even if pedantic, an external linkage array
2734 may have incomplete type at first. */
2735 ? pedantic && !TREE_PUBLIC (decl)
2736 : !DECL_EXTERNAL (decl));
2737 int failure
2738 = complete_array_type (type, DECL_INITIAL (decl), do_default);
2739
2740 /* Get the completed type made by complete_array_type. */
2741 type = TREE_TYPE (decl);
2742
2743 if (failure == 1)
2744 error ("%Jinitializer fails to determine size of '%D'", decl, decl);
2745
2746 else if (failure == 2)
2747 {
2748 if (do_default)
2749 error ("%Jarray size missing in '%D'", decl, decl);
2750 /* If a `static' var's size isn't known,
2751 make it extern as well as static, so it does not get
2752 allocated.
2753 If it is not `static', then do not mark extern;
2754 finish_incomplete_decl will give it a default size
2755 and it will get allocated. */
2756 else if (!pedantic && TREE_STATIC (decl) && ! TREE_PUBLIC (decl))
2757 DECL_EXTERNAL (decl) = 1;
2758 }
2759
2760 /* TYPE_MAX_VALUE is always one less than the number of elements
2761 in the array, because we start counting at zero. Therefore,
2762 warn only if the value is less than zero. */
2763 else if (pedantic && TYPE_DOMAIN (type) != 0
2764 && tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < 0)
2765 error ("%Jzero or negative size array '%D'", decl, decl);
2766
2767 layout_decl (decl, 0);
2768 }
2769
2770 if (TREE_CODE (decl) == VAR_DECL)
2771 {
2772 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
2773 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
2774 layout_decl (decl, 0);
2775
2776 if (DECL_SIZE (decl) == 0
2777 /* Don't give an error if we already gave one earlier. */
2778 && TREE_TYPE (decl) != error_mark_node
2779 && (TREE_STATIC (decl)
2780 ?
2781 /* A static variable with an incomplete type
2782 is an error if it is initialized.
2783 Also if it is not file scope.
2784 Otherwise, let it through, but if it is not `extern'
2785 then it may cause an error message later. */
2786 (DECL_INITIAL (decl) != 0
2787 || !DECL_FILE_SCOPE_P (decl))
2788 :
2789 /* An automatic variable with an incomplete type
2790 is an error. */
2791 !DECL_EXTERNAL (decl)))
2792 {
2793 error ("%Jstorage size of '%D' isn't known", decl, decl);
2794 TREE_TYPE (decl) = error_mark_node;
2795 }
2796
2797 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
2798 && DECL_SIZE (decl) != 0)
2799 {
2800 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
2801 constant_expression_warning (DECL_SIZE (decl));
2802 else
2803 error ("%Jstorage size of '%D' isn't constant", decl, decl);
2804 }
2805
2806 if (TREE_USED (type))
2807 TREE_USED (decl) = 1;
2808 }
2809
2810 /* If this is a function and an assembler name is specified, reset DECL_RTL
2811 so we can give it its new name. Also, update built_in_decls if it
2812 was a normal built-in. */
2813 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
2814 {
2815 /* ASMSPEC is given, and not the name of a register. Mark the
2816 name with a star so assemble_name won't munge it. */
2817 char *starred = alloca (strlen (asmspec) + 2);
2818 starred[0] = '*';
2819 strcpy (starred + 1, asmspec);
2820
2821 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
2822 {
2823 tree builtin = built_in_decls [DECL_FUNCTION_CODE (decl)];
2824 SET_DECL_RTL (builtin, NULL_RTX);
2825 SET_DECL_ASSEMBLER_NAME (builtin, get_identifier (starred));
2826#ifdef TARGET_MEM_FUNCTIONS
2827 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMCPY)
2828 init_block_move_fn (starred);
2829 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMSET)
2830 init_block_clear_fn (starred);
2831#else
2832 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BCOPY)
2833 init_block_move_fn (starred);
2834 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BZERO)
2835 init_block_clear_fn (starred);
2836#endif
2837 }
2838 SET_DECL_RTL (decl, NULL_RTX);
2839 change_decl_assembler_name (decl, get_identifier (starred));
2840 }
2841
2842 /* If #pragma weak was used, mark the decl weak now. */
2843 if (current_scope == global_scope)
2844 maybe_apply_pragma_weak (decl);
2845
2846 /* Output the assembler code and/or RTL code for variables and functions,
2847 unless the type is an undefined structure or union.
2848 If not, it will get done when the type is completed. */
2849
2850 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
2851 {
2852 /* This is a no-op in c-lang.c or something real in objc-act.c. */
2853 if (c_dialect_objc ())
2854 objc_check_decl (decl);
2855
2856 if (DECL_FILE_SCOPE_P (decl))
2857 {
2858 if (DECL_INITIAL (decl) == NULL_TREE
2859 || DECL_INITIAL (decl) == error_mark_node)
2860 /* Don't output anything
2861 when a tentative file-scope definition is seen.
2862 But at end of compilation, do output code for them. */
2863 DECL_DEFER_OUTPUT (decl) = 1;
2864 rest_of_decl_compilation (decl, asmspec, true, 0);
2865 }
2866 else
2867 {
2868 /* This is a local variable. If there is an ASMSPEC, the
2869 user has requested that we handle it specially. */
2870 if (asmspec)
2871 {
2872 /* In conjunction with an ASMSPEC, the `register'
2873 keyword indicates that we should place the variable
2874 in a particular register. */
2875 if (DECL_REGISTER (decl))
2876 DECL_C_HARD_REGISTER (decl) = 1;
2877
2878 /* If this is not a static variable, issue a warning.
2879 It doesn't make any sense to give an ASMSPEC for an
2880 ordinary, non-register local variable. Historically,
2881 GCC has accepted -- but ignored -- the ASMSPEC in
2882 this case. */
2883 if (TREE_CODE (decl) == VAR_DECL
2884 && !DECL_REGISTER (decl)
2885 && !TREE_STATIC (decl))
2886 warning ("%Jignoring asm-specifier for non-static local "
2887 "variable '%D'", decl, decl);
2888 else
2889 change_decl_assembler_name (decl, get_identifier (asmspec));
2890 }
2891
2892 if (TREE_CODE (decl) != FUNCTION_DECL)
2893 add_decl_stmt (decl);
2894 }
2895
2896 if (!DECL_FILE_SCOPE_P (decl))
2897 {
2898 /* Recompute the RTL of a local array now
2899 if it used to be an incomplete type. */
2900 if (was_incomplete
2901 && ! TREE_STATIC (decl) && ! DECL_EXTERNAL (decl))
2902 {
2903 /* If we used it already as memory, it must stay in memory. */
2904 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
2905 /* If it's still incomplete now, no init will save it. */
2906 if (DECL_SIZE (decl) == 0)
2907 DECL_INITIAL (decl) = 0;
2908 }
2909 }
2910 }
2911
2912 /* If this was marked 'used', be sure it will be output. */
2913 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
2914 mark_referenced (DECL_ASSEMBLER_NAME (decl));
2915
2916 if (TREE_CODE (decl) == TYPE_DECL)
2917 rest_of_decl_compilation (decl, NULL, DECL_FILE_SCOPE_P (decl), 0);
2918
2919 /* At the end of a declaration, throw away any variable type sizes
2920 of types defined inside that declaration. There is no use
2921 computing them in the following function definition. */
2922 if (current_scope == global_scope)
2923 get_pending_sizes ();
2924
2925 /* Install a cleanup (aka destructor) if one was given. */
2926 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
2927 {
2928 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
2929 if (attr)
2930 {
2931 static bool eh_initialized_p;
2932
2933 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
2934 tree cleanup_decl = lookup_name (cleanup_id);
2935 tree cleanup;
2936
2937 /* Build "cleanup(&decl)" for the destructor. */
2938 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
2939 cleanup = build_tree_list (NULL_TREE, cleanup);
2940 cleanup = build_function_call (cleanup_decl, cleanup);
2941
2942 /* Don't warn about decl unused; the cleanup uses it. */
2943 TREE_USED (decl) = 1;
2944
2945 /* Initialize EH, if we've been told to do so. */
2946 if (flag_exceptions && !eh_initialized_p)
2947 {
2948 eh_initialized_p = true;
2949 eh_personality_libfunc
2950 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
2951 ? "__gcc_personality_sj0"
2952 : "__gcc_personality_v0");
2953 using_eh_for_cleanups ();
2954 }
2955
2956 add_stmt (build_stmt (CLEANUP_STMT, decl, cleanup));
2957 }
2958 }
2959}
2960
2961/* Given a parsed parameter declaration, decode it into a PARM_DECL
2962 and push that on the current scope. */
2963
2964void
2965push_parm_decl (tree parm)
2966{
2967 tree decl;
2968 int old_dont_save_pending_sizes_p = 0;
2959
2960 /* Don't attempt to expand sizes while parsing this decl.
2961 (We can get here with i_s_e 1 somehow from Objective-C.) */
2962 int save_immediate_size_expand = immediate_size_expand;
2963 immediate_size_expand = 0;
2964
2969
2970 /* Don't attempt to expand sizes while parsing this decl.
2971 (We can get here with i_s_e 1 somehow from Objective-C.) */
2972 int save_immediate_size_expand = immediate_size_expand;
2973 immediate_size_expand = 0;
2974
2975 /* If this is a nested function, we do want to keep SAVE_EXPRs for
2976 the argument sizes, regardless of the parent's setting. */
2977 if (cfun)
2978 {
2979 old_dont_save_pending_sizes_p = cfun->x_dont_save_pending_sizes_p;
2980 cfun->x_dont_save_pending_sizes_p = 0;
2981 }
2982
2965 decl = grokdeclarator (TREE_VALUE (TREE_PURPOSE (parm)),
2966 TREE_PURPOSE (TREE_PURPOSE (parm)),
2967 PARM, 0, NULL);
2968 decl_attributes (&decl, TREE_VALUE (parm), 0);
2969
2970 decl = pushdecl (decl);
2971
2972 finish_decl (decl, NULL_TREE, NULL_TREE);
2973
2983 decl = grokdeclarator (TREE_VALUE (TREE_PURPOSE (parm)),
2984 TREE_PURPOSE (TREE_PURPOSE (parm)),
2985 PARM, 0, NULL);
2986 decl_attributes (&decl, TREE_VALUE (parm), 0);
2987
2988 decl = pushdecl (decl);
2989
2990 finish_decl (decl, NULL_TREE, NULL_TREE);
2991
2992 if (cfun)
2993 cfun->x_dont_save_pending_sizes_p = old_dont_save_pending_sizes_p;
2974 immediate_size_expand = save_immediate_size_expand;
2975}
2976
2977/* Mark all the parameter declarations to date as forward decls,
2978 shift them to the variables list, and reset the parameters list.
2979 Also diagnose use of this extension. */
2980
2981void
2982mark_forward_parm_decls (void)
2983{
2984 tree parm;
2985
2986 if (pedantic && !current_scope->warned_forward_parm_decls)
2987 {
2988 pedwarn ("ISO C forbids forward parameter declarations");
2989 current_scope->warned_forward_parm_decls = true;
2990 }
2991
2992 for (parm = current_scope->parms; parm; parm = TREE_CHAIN (parm))
2993 TREE_ASM_WRITTEN (parm) = 1;
2994
2995 SCOPE_LIST_CONCAT (current_scope, names, current_scope, parms);
2996 current_scope->parms = 0;
2997 current_scope->parms_last = 0;
2998}
2999
3000static GTY(()) int compound_literal_number;
3001
3002/* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3003 literal, which may be an incomplete array type completed by the
3004 initializer; INIT is a CONSTRUCTOR that initializes the compound
3005 literal. */
3006
3007tree
3008build_compound_literal (tree type, tree init)
3009{
3010 /* We do not use start_decl here because we have a type, not a declarator;
3011 and do not use finish_decl because the decl should be stored inside
3012 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_STMT. */
3013 tree decl = build_decl (VAR_DECL, NULL_TREE, type);
3014 tree complit;
3015 tree stmt;
3016 DECL_EXTERNAL (decl) = 0;
3017 TREE_PUBLIC (decl) = 0;
3018 TREE_STATIC (decl) = (current_scope == global_scope);
3019 DECL_CONTEXT (decl) = current_function_decl;
3020 TREE_USED (decl) = 1;
3021 TREE_TYPE (decl) = type;
3022 TREE_READONLY (decl) = TREE_READONLY (type);
3023 store_init_value (decl, init);
3024
3025 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3026 {
3027 int failure = complete_array_type (type, DECL_INITIAL (decl), 1);
3028 if (failure)
3029 abort ();
3030 }
3031
3032 type = TREE_TYPE (decl);
3033 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3034 return error_mark_node;
3035
3036 stmt = build_stmt (DECL_STMT, decl);
3037 complit = build1 (COMPOUND_LITERAL_EXPR, TREE_TYPE (decl), stmt);
3038 TREE_SIDE_EFFECTS (complit) = 1;
3039
3040 layout_decl (decl, 0);
3041
3042 if (TREE_STATIC (decl))
3043 {
3044 /* This decl needs a name for the assembler output. We also need
3045 a unique suffix to be added to the name. */
3046 char *name;
3047
3048 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
3049 compound_literal_number);
3050 compound_literal_number++;
3051 DECL_NAME (decl) = get_identifier (name);
3052 DECL_DEFER_OUTPUT (decl) = 1;
3053 DECL_COMDAT (decl) = 1;
3054 DECL_ARTIFICIAL (decl) = 1;
3055 pushdecl (decl);
3056 rest_of_decl_compilation (decl, NULL, 1, 0);
3057 }
3058
3059 return complit;
3060}
3061
3062/* Make TYPE a complete type based on INITIAL_VALUE.
3063 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
3064 2 if there was no information (in which case assume 1 if DO_DEFAULT). */
3065
3066int
3067complete_array_type (tree type, tree initial_value, int do_default)
3068{
3069 tree maxindex = NULL_TREE;
3070 int value = 0;
3071
3072 if (initial_value)
3073 {
3074 /* Note MAXINDEX is really the maximum index,
3075 one less than the size. */
3076 if (TREE_CODE (initial_value) == STRING_CST)
3077 {
3078 int eltsize
3079 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
3080 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
3081 / eltsize) - 1, 0);
3082 }
3083 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
3084 {
3085 tree elts = CONSTRUCTOR_ELTS (initial_value);
3086 maxindex = build_int_2 (-1, -1);
3087 for (; elts; elts = TREE_CHAIN (elts))
3088 {
3089 if (TREE_PURPOSE (elts))
3090 maxindex = TREE_PURPOSE (elts);
3091 else
3092 maxindex = fold (build (PLUS_EXPR, integer_type_node,
3093 maxindex, integer_one_node));
3094 }
3095 maxindex = copy_node (maxindex);
3096 }
3097 else
3098 {
3099 /* Make an error message unless that happened already. */
3100 if (initial_value != error_mark_node)
3101 value = 1;
3102
3103 /* Prevent further error messages. */
3104 maxindex = build_int_2 (0, 0);
3105 }
3106 }
3107
3108 if (!maxindex)
3109 {
3110 if (do_default)
3111 maxindex = build_int_2 (0, 0);
3112 value = 2;
3113 }
3114
3115 if (maxindex)
3116 {
3117 TYPE_DOMAIN (type) = build_index_type (maxindex);
3118 if (!TREE_TYPE (maxindex))
3119 TREE_TYPE (maxindex) = TYPE_DOMAIN (type);
3120 }
3121
3122 /* Lay out the type now that we can get the real answer. */
3123
3124 layout_type (type);
3125
3126 return value;
3127}
3128
3129/* Determine whether TYPE is a structure with a flexible array member,
3130 or a union containing such a structure (possibly recursively). */
3131
3132static bool
3133flexible_array_type_p (tree type)
3134{
3135 tree x;
3136 switch (TREE_CODE (type))
3137 {
3138 case RECORD_TYPE:
3139 x = TYPE_FIELDS (type);
3140 if (x == NULL_TREE)
3141 return false;
3142 while (TREE_CHAIN (x) != NULL_TREE)
3143 x = TREE_CHAIN (x);
3144 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3145 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3146 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3147 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3148 return true;
3149 return false;
3150 case UNION_TYPE:
3151 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3152 {
3153 if (flexible_array_type_p (TREE_TYPE (x)))
3154 return true;
3155 }
3156 return false;
3157 default:
3158 return false;
3159 }
3160}
3161
3162/* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3163 replacing with appropriate values if they are invalid. */
3164static void
3165check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3166{
3167 tree type_mv;
3168 unsigned int max_width;
3169 unsigned HOST_WIDE_INT w;
3170 const char *name = orig_name ? orig_name: _("<anonymous>");
3171
3172 /* Necessary? */
3173 STRIP_NOPS (*width);
3174
3175 /* Detect and ignore out of range field width and process valid
3176 field widths. */
3177 if (TREE_CODE (*width) != INTEGER_CST)
3178 {
3179 error ("bit-field `%s' width not an integer constant", name);
3180 *width = integer_one_node;
3181 }
3182 else
3183 {
3184 constant_expression_warning (*width);
3185 if (tree_int_cst_sgn (*width) < 0)
3186 {
3187 error ("negative width in bit-field `%s'", name);
3188 *width = integer_one_node;
3189 }
3190 else if (integer_zerop (*width) && orig_name)
3191 {
3192 error ("zero width for bit-field `%s'", name);
3193 *width = integer_one_node;
3194 }
3195 }
3196
3197 /* Detect invalid bit-field type. */
3198 if (TREE_CODE (*type) != INTEGER_TYPE
3199 && TREE_CODE (*type) != BOOLEAN_TYPE
3200 && TREE_CODE (*type) != ENUMERAL_TYPE)
3201 {
3202 error ("bit-field `%s' has invalid type", name);
3203 *type = unsigned_type_node;
3204 }
3205
3206 type_mv = TYPE_MAIN_VARIANT (*type);
3207 if (pedantic
3208 && type_mv != integer_type_node
3209 && type_mv != unsigned_type_node
3210 && type_mv != boolean_type_node)
3211 pedwarn ("type of bit-field `%s' is a GCC extension", name);
3212
3213 if (type_mv == boolean_type_node)
3214 max_width = CHAR_TYPE_SIZE;
3215 else
3216 max_width = TYPE_PRECISION (*type);
3217
3218 if (0 < compare_tree_int (*width, max_width))
3219 {
3220 error ("width of `%s' exceeds its type", name);
3221 w = max_width;
3222 *width = build_int_2 (w, 0);
3223 }
3224 else
3225 w = tree_low_cst (*width, 1);
3226
3227 if (TREE_CODE (*type) == ENUMERAL_TYPE
3228 && (w < min_precision (TYPE_MIN_VALUE (*type), TREE_UNSIGNED (*type))
3229 || w < min_precision (TYPE_MAX_VALUE (*type), TREE_UNSIGNED (*type))))
3230 warning ("`%s' is narrower than values of its type", name);
3231}
3232
3233/* Given declspecs and a declarator,
3234 determine the name and type of the object declared
3235 and construct a ..._DECL node for it.
3236 (In one case we can return a ..._TYPE node instead.
3237 For invalid input we sometimes return 0.)
3238
3239 DECLSPECS is a chain of tree_list nodes whose value fields
3240 are the storage classes and type specifiers.
3241
3242 DECL_CONTEXT says which syntactic context this declaration is in:
3243 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3244 FUNCDEF for a function definition. Like NORMAL but a few different
3245 error messages in each case. Return value may be zero meaning
3246 this definition is too screwy to try to parse.
3247 PARM for a parameter declaration (either within a function prototype
3248 or before a function body). Make a PARM_DECL, or return void_type_node.
3249 TYPENAME if for a typename (in a cast or sizeof).
3250 Don't make a DECL node; just return the ..._TYPE node.
3251 FIELD for a struct or union field; make a FIELD_DECL.
3252 INITIALIZED is 1 if the decl has an initializer.
3253 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3254 representing the width of the bit-field.
3255
3256 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3257 It may also be so in the PARM case, for a prototype where the
3258 argument type is specified but not the name.
3259
3260 This function is where the complicated C meanings of `static'
3261 and `extern' are interpreted. */
3262
3263static tree
3264grokdeclarator (tree declarator, tree declspecs,
3265 enum decl_context decl_context, int initialized, tree *width)
3266{
3267 int specbits = 0;
3268 tree spec;
3269 tree type = NULL_TREE;
3270 int longlong = 0;
3271 int constp;
3272 int restrictp;
3273 int volatilep;
3274 int type_quals = TYPE_UNQUALIFIED;
3275 int inlinep;
3276 int explicit_int = 0;
3277 int explicit_char = 0;
3278 int defaulted_int = 0;
3279 tree typedef_decl = 0;
3280 const char *name, *orig_name;
3281 tree typedef_type = 0;
3282 int funcdef_flag = 0;
3283 enum tree_code innermost_code = ERROR_MARK;
3284 int size_varies = 0;
3285 tree decl_attr = NULL_TREE;
3286 tree array_ptr_quals = NULL_TREE;
3287 int array_parm_static = 0;
3288 tree returned_attrs = NULL_TREE;
3289 bool bitfield = width != NULL;
3290 tree element_type;
3291
3292 if (decl_context == FUNCDEF)
3293 funcdef_flag = 1, decl_context = NORMAL;
3294
3295 /* Look inside a declarator for the name being declared
3296 and get it as a string, for an error message. */
3297 {
3298 tree decl = declarator;
3299 name = 0;
3300
3301 while (decl)
3302 switch (TREE_CODE (decl))
3303 {
3304 case ARRAY_REF:
3305 case INDIRECT_REF:
3306 case CALL_EXPR:
3307 innermost_code = TREE_CODE (decl);
3308 decl = TREE_OPERAND (decl, 0);
3309 break;
3310
3311 case TREE_LIST:
3312 decl = TREE_VALUE (decl);
3313 break;
3314
3315 case IDENTIFIER_NODE:
3316 name = IDENTIFIER_POINTER (decl);
3317 decl = 0;
3318 break;
3319
3320 default:
3321 abort ();
3322 }
3323 orig_name = name;
3324 if (name == 0)
3325 name = "type name";
3326 }
3327
3328 /* A function definition's declarator must have the form of
3329 a function declarator. */
3330
3331 if (funcdef_flag && innermost_code != CALL_EXPR)
3332 return 0;
3333
3334 /* If this looks like a function definition, make it one,
3335 even if it occurs where parms are expected.
3336 Then store_parm_decls will reject it and not use it as a parm. */
3337 if (decl_context == NORMAL && !funcdef_flag
3338 && current_scope->parm_flag)
3339 decl_context = PARM;
3340
3341 /* Look through the decl specs and record which ones appear.
3342 Some typespecs are defined as built-in typenames.
3343 Others, the ones that are modifiers of other types,
3344 are represented by bits in SPECBITS: set the bits for
3345 the modifiers that appear. Storage class keywords are also in SPECBITS.
3346
3347 If there is a typedef name or a type, store the type in TYPE.
3348 This includes builtin typedefs such as `int'.
3349
3350 Set EXPLICIT_INT or EXPLICIT_CHAR if the type is `int' or `char'
3351 and did not come from a user typedef.
3352
3353 Set LONGLONG if `long' is mentioned twice. */
3354
3355 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
3356 {
3357 tree id = TREE_VALUE (spec);
3358
3359 /* If the entire declaration is itself tagged as deprecated then
3360 suppress reports of deprecated items. */
3361 if (id && TREE_DEPRECATED (id))
3362 {
3363 if (deprecated_state != DEPRECATED_SUPPRESS)
3364 warn_deprecated_use (id);
3365 }
3366
3367 if (id == ridpointers[(int) RID_INT])
3368 explicit_int = 1;
3369 if (id == ridpointers[(int) RID_CHAR])
3370 explicit_char = 1;
3371
3372 if (TREE_CODE (id) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (id))
3373 {
3374 enum rid i = C_RID_CODE (id);
3375 if ((int) i <= (int) RID_LAST_MODIFIER)
3376 {
3377 if (i == RID_LONG && (specbits & (1 << (int) RID_LONG)))
3378 {
3379 if (longlong)
3380 error ("`long long long' is too long for GCC");
3381 else
3382 {
3383 if (pedantic && !flag_isoc99 && ! in_system_header
3384 && warn_long_long)
3385 pedwarn ("ISO C90 does not support `long long'");
3386 longlong = 1;
3387 }
3388 }
3389 else if (specbits & (1 << (int) i))
3390 {
3391 if (i == RID_CONST || i == RID_VOLATILE || i == RID_RESTRICT)
3392 {
3393 if (pedantic && !flag_isoc99)
3394 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
3395 }
3396 else
3397 error ("duplicate `%s'", IDENTIFIER_POINTER (id));
3398 }
3399
3400 /* Diagnose "__thread extern". Recall that this list
3401 is in the reverse order seen in the text. */
3402 if (i == RID_THREAD
3403 && (specbits & (1 << (int) RID_EXTERN
3404 | 1 << (int) RID_STATIC)))
3405 {
3406 if (specbits & 1 << (int) RID_EXTERN)
3407 error ("`__thread' before `extern'");
3408 else
3409 error ("`__thread' before `static'");
3410 }
3411
3412 specbits |= 1 << (int) i;
3413 goto found;
3414 }
3415 }
3416 if (type)
3417 error ("two or more data types in declaration of `%s'", name);
3418 /* Actual typedefs come to us as TYPE_DECL nodes. */
3419 else if (TREE_CODE (id) == TYPE_DECL)
3420 {
3421 if (TREE_TYPE (id) == error_mark_node)
3422 ; /* Allow the type to default to int to avoid cascading errors. */
3423 else
3424 {
3425 type = TREE_TYPE (id);
3426 decl_attr = DECL_ATTRIBUTES (id);
3427 typedef_decl = id;
3428 }
3429 }
3430 /* Built-in types come as identifiers. */
3431 else if (TREE_CODE (id) == IDENTIFIER_NODE)
3432 {
3433 tree t = lookup_name (id);
3434 if (TREE_TYPE (t) == error_mark_node)
3435 ;
3436 else if (!t || TREE_CODE (t) != TYPE_DECL)
3437 error ("`%s' fails to be a typedef or built in type",
3438 IDENTIFIER_POINTER (id));
3439 else
3440 {
3441 type = TREE_TYPE (t);
3442 typedef_decl = t;
3443 }
3444 }
3445 else if (TREE_CODE (id) != ERROR_MARK)
3446 type = id;
3447
3448 found:
3449 ;
3450 }
3451
3452 typedef_type = type;
3453 if (type)
3454 size_varies = C_TYPE_VARIABLE_SIZE (type);
3455
3456 /* No type at all: default to `int', and set DEFAULTED_INT
3457 because it was not a user-defined typedef. */
3458
3459 if (type == 0)
3460 {
3461 if ((! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3462 | (1 << (int) RID_SIGNED)
3463 | (1 << (int) RID_UNSIGNED)
3464 | (1 << (int) RID_COMPLEX))))
3465 /* Don't warn about typedef foo = bar. */
3466 && ! (specbits & (1 << (int) RID_TYPEDEF) && initialized)
3467 && ! in_system_header)
3468 {
3469 /* Issue a warning if this is an ISO C 99 program or if -Wreturn-type
3470 and this is a function, or if -Wimplicit; prefer the former
3471 warning since it is more explicit. */
3472 if ((warn_implicit_int || warn_return_type || flag_isoc99)
3473 && funcdef_flag)
3474 warn_about_return_type = 1;
3475 else if (warn_implicit_int || flag_isoc99)
3476 pedwarn_c99 ("type defaults to `int' in declaration of `%s'",
3477 name);
3478 }
3479
3480 defaulted_int = 1;
3481 type = integer_type_node;
3482 }
3483
3484 /* Now process the modifiers that were specified
3485 and check for invalid combinations. */
3486
3487 /* Long double is a special combination. */
3488
3489 if ((specbits & 1 << (int) RID_LONG) && ! longlong
3490 && TYPE_MAIN_VARIANT (type) == double_type_node)
3491 {
3492 specbits &= ~(1 << (int) RID_LONG);
3493 type = long_double_type_node;
3494 }
3495
3496 /* Check all other uses of type modifiers. */
3497
3498 if (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3499 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED)))
3500 {
3501 int ok = 0;
3502
3503 if ((specbits & 1 << (int) RID_LONG)
3504 && (specbits & 1 << (int) RID_SHORT))
3505 error ("both long and short specified for `%s'", name);
3506 else if (((specbits & 1 << (int) RID_LONG)
3507 || (specbits & 1 << (int) RID_SHORT))
3508 && explicit_char)
3509 error ("long or short specified with char for `%s'", name);
3510 else if (((specbits & 1 << (int) RID_LONG)
3511 || (specbits & 1 << (int) RID_SHORT))
3512 && TREE_CODE (type) == REAL_TYPE)
3513 {
3514 static int already = 0;
3515
3516 error ("long or short specified with floating type for `%s'", name);
3517 if (! already && ! pedantic)
3518 {
3519 error ("the only valid combination is `long double'");
3520 already = 1;
3521 }
3522 }
3523 else if ((specbits & 1 << (int) RID_SIGNED)
3524 && (specbits & 1 << (int) RID_UNSIGNED))
3525 error ("both signed and unsigned specified for `%s'", name);
3526 else if (TREE_CODE (type) != INTEGER_TYPE)
3527 error ("long, short, signed or unsigned invalid for `%s'", name);
3528 else
3529 {
3530 ok = 1;
3531 if (!explicit_int && !defaulted_int && !explicit_char)
3532 {
3533 error ("long, short, signed or unsigned used invalidly for `%s'",
3534 name);
3535 ok = 0;
3536 }
3537 }
3538
3539 /* Discard the type modifiers if they are invalid. */
3540 if (! ok)
3541 {
3542 specbits &= ~((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3543 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED));
3544 longlong = 0;
3545 }
3546 }
3547
3548 if ((specbits & (1 << (int) RID_COMPLEX))
3549 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
3550 {
3551 error ("complex invalid for `%s'", name);
3552 specbits &= ~(1 << (int) RID_COMPLEX);
3553 }
3554
3555 /* Decide whether an integer type is signed or not.
3556 Optionally treat bit-fields as signed by default. */
3557 if (specbits & 1 << (int) RID_UNSIGNED
3558 || (bitfield && ! flag_signed_bitfields
3559 && (explicit_int || defaulted_int || explicit_char
3560 /* A typedef for plain `int' without `signed'
3561 can be controlled just like plain `int'. */
3562 || ! (typedef_decl != 0
3563 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
3564 && TREE_CODE (type) != ENUMERAL_TYPE
3565 && !(specbits & 1 << (int) RID_SIGNED)))
3566 {
3567 if (longlong)
3568 type = long_long_unsigned_type_node;
3569 else if (specbits & 1 << (int) RID_LONG)
3570 type = long_unsigned_type_node;
3571 else if (specbits & 1 << (int) RID_SHORT)
3572 type = short_unsigned_type_node;
3573 else if (type == char_type_node)
3574 type = unsigned_char_type_node;
3575 else if (typedef_decl)
3576 type = c_common_unsigned_type (type);
3577 else
3578 type = unsigned_type_node;
3579 }
3580 else if ((specbits & 1 << (int) RID_SIGNED)
3581 && type == char_type_node)
3582 type = signed_char_type_node;
3583 else if (longlong)
3584 type = long_long_integer_type_node;
3585 else if (specbits & 1 << (int) RID_LONG)
3586 type = long_integer_type_node;
3587 else if (specbits & 1 << (int) RID_SHORT)
3588 type = short_integer_type_node;
3589
3590 if (specbits & 1 << (int) RID_COMPLEX)
3591 {
3592 if (pedantic && !flag_isoc99)
3593 pedwarn ("ISO C90 does not support complex types");
3594 /* If we just have "complex", it is equivalent to
3595 "complex double", but if any modifiers at all are specified it is
3596 the complex form of TYPE. E.g, "complex short" is
3597 "complex short int". */
3598
3599 if (defaulted_int && ! longlong
3600 && ! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3601 | (1 << (int) RID_SIGNED)
3602 | (1 << (int) RID_UNSIGNED))))
3603 {
3604 if (pedantic)
3605 pedwarn ("ISO C does not support plain `complex' meaning `double complex'");
3606 type = complex_double_type_node;
3607 }
3608 else if (type == integer_type_node)
3609 {
3610 if (pedantic)
3611 pedwarn ("ISO C does not support complex integer types");
3612 type = complex_integer_type_node;
3613 }
3614 else if (type == float_type_node)
3615 type = complex_float_type_node;
3616 else if (type == double_type_node)
3617 type = complex_double_type_node;
3618 else if (type == long_double_type_node)
3619 type = complex_long_double_type_node;
3620 else
3621 {
3622 if (pedantic)
3623 pedwarn ("ISO C does not support complex integer types");
3624 type = build_complex_type (type);
3625 }
3626 }
3627
3628 /* Figure out the type qualifiers for the declaration. There are
3629 two ways a declaration can become qualified. One is something
3630 like `const int i' where the `const' is explicit. Another is
3631 something like `typedef const int CI; CI i' where the type of the
3632 declaration contains the `const'. A third possibility is that
3633 there is a type qualifier on the element type of a typedefed
3634 array type, in which case we should extract that qualifier so
3635 that c_apply_type_quals_to_decls receives the full list of
3636 qualifiers to work with (C90 is not entirely clear about whether
3637 duplicate qualifiers should be diagnosed in this case, but it
3638 seems most appropriate to do so). */
3639 element_type = strip_array_types (type);
3640 constp = !! (specbits & 1 << (int) RID_CONST) + TYPE_READONLY (element_type);
3641 restrictp
3642 = !! (specbits & 1 << (int) RID_RESTRICT) + TYPE_RESTRICT (element_type);
3643 volatilep
3644 = !! (specbits & 1 << (int) RID_VOLATILE) + TYPE_VOLATILE (element_type);
3645 inlinep = !! (specbits & (1 << (int) RID_INLINE));
3646 if (pedantic && !flag_isoc99)
3647 {
3648 if (constp > 1)
3649 pedwarn ("duplicate `const'");
3650 if (restrictp > 1)
3651 pedwarn ("duplicate `restrict'");
3652 if (volatilep > 1)
3653 pedwarn ("duplicate `volatile'");
3654 }
3655 if (! flag_gen_aux_info && (TYPE_QUALS (type)))
3656 type = TYPE_MAIN_VARIANT (type);
3657 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
3658 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
3659 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
3660
3661 /* Warn if two storage classes are given. Default to `auto'. */
3662
3663 {
3664 int nclasses = 0;
3665
3666 if (specbits & 1 << (int) RID_AUTO) nclasses++;
3667 if (specbits & 1 << (int) RID_STATIC) nclasses++;
3668 if (specbits & 1 << (int) RID_EXTERN) nclasses++;
3669 if (specbits & 1 << (int) RID_REGISTER) nclasses++;
3670 if (specbits & 1 << (int) RID_TYPEDEF) nclasses++;
3671
3672 /* "static __thread" and "extern __thread" are allowed. */
3673 if ((specbits & (1 << (int) RID_THREAD
3674 | 1 << (int) RID_STATIC
3675 | 1 << (int) RID_EXTERN)) == (1 << (int) RID_THREAD))
3676 nclasses++;
3677
3678 /* Warn about storage classes that are invalid for certain
3679 kinds of declarations (parameters, typenames, etc.). */
3680
3681 if (nclasses > 1)
3682 error ("multiple storage classes in declaration of `%s'", name);
3683 else if (funcdef_flag
3684 && (specbits
3685 & ((1 << (int) RID_REGISTER)
3686 | (1 << (int) RID_AUTO)
3687 | (1 << (int) RID_TYPEDEF)
3688 | (1 << (int) RID_THREAD))))
3689 {
3690 if (specbits & 1 << (int) RID_AUTO
3691 && (pedantic || current_scope == global_scope))
3692 pedwarn ("function definition declared `auto'");
3693 if (specbits & 1 << (int) RID_REGISTER)
3694 error ("function definition declared `register'");
3695 if (specbits & 1 << (int) RID_TYPEDEF)
3696 error ("function definition declared `typedef'");
3697 if (specbits & 1 << (int) RID_THREAD)
3698 error ("function definition declared `__thread'");
3699 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3700 | (1 << (int) RID_AUTO) | (1 << (int) RID_THREAD));
3701 }
3702 else if (decl_context != NORMAL && nclasses > 0)
3703 {
3704 if (decl_context == PARM && specbits & 1 << (int) RID_REGISTER)
3705 ;
3706 else
3707 {
3708 switch (decl_context)
3709 {
3710 case FIELD:
3711 error ("storage class specified for structure field `%s'",
3712 name);
3713 break;
3714 case PARM:
3715 error ("storage class specified for parameter `%s'", name);
3716 break;
3717 default:
3718 error ("storage class specified for typename");
3719 break;
3720 }
3721 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3722 | (1 << (int) RID_AUTO) | (1 << (int) RID_STATIC)
3723 | (1 << (int) RID_EXTERN) | (1 << (int) RID_THREAD));
3724 }
3725 }
3726 else if (specbits & 1 << (int) RID_EXTERN && initialized && ! funcdef_flag)
3727 {
3728 /* `extern' with initialization is invalid if not at file scope. */
3729 if (current_scope == global_scope)
3730 warning ("`%s' initialized and declared `extern'", name);
3731 else
3732 error ("`%s' has both `extern' and initializer", name);
3733 }
3734 else if (current_scope == global_scope)
3735 {
3736 if (specbits & 1 << (int) RID_AUTO)
3737 error ("file-scope declaration of `%s' specifies `auto'", name);
3738 }
3739 else
3740 {
3741 if (specbits & 1 << (int) RID_EXTERN && funcdef_flag)
3742 error ("nested function `%s' declared `extern'", name);
3743 else if ((specbits & (1 << (int) RID_THREAD
3744 | 1 << (int) RID_EXTERN
3745 | 1 << (int) RID_STATIC))
3746 == (1 << (int) RID_THREAD))
3747 {
3748 error ("function-scope `%s' implicitly auto and declared `__thread'",
3749 name);
3750 specbits &= ~(1 << (int) RID_THREAD);
3751 }
3752 }
3753 }
3754
3755 /* Now figure out the structure of the declarator proper.
3756 Descend through it, creating more complex types, until we reach
3757 the declared identifier (or NULL_TREE, in an absolute declarator). */
3758
3759 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE)
3760 {
3761 if (type == error_mark_node)
3762 {
3763 declarator = TREE_OPERAND (declarator, 0);
3764 continue;
3765 }
3766
3767 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
3768 an INDIRECT_REF (for *...),
3769 a CALL_EXPR (for ...(...)),
3770 a TREE_LIST (for nested attributes),
3771 an identifier (for the name being declared)
3772 or a null pointer (for the place in an absolute declarator
3773 where the name was omitted).
3774 For the last two cases, we have just exited the loop.
3775
3776 At this point, TYPE is the type of elements of an array,
3777 or for a function to return, or for a pointer to point to.
3778 After this sequence of ifs, TYPE is the type of the
3779 array or function or pointer, and DECLARATOR has had its
3780 outermost layer removed. */
3781
3782 if (array_ptr_quals != NULL_TREE || array_parm_static)
3783 {
3784 /* Only the innermost declarator (making a parameter be of
3785 array type which is converted to pointer type)
3786 may have static or type qualifiers. */
3787 error ("static or type qualifiers in non-parameter array declarator");
3788 array_ptr_quals = NULL_TREE;
3789 array_parm_static = 0;
3790 }
3791
3792 if (TREE_CODE (declarator) == TREE_LIST)
3793 {
3794 /* We encode a declarator with embedded attributes using
3795 a TREE_LIST. */
3796 tree attrs = TREE_PURPOSE (declarator);
3797 tree inner_decl;
3798 int attr_flags = 0;
3799 declarator = TREE_VALUE (declarator);
3800 inner_decl = declarator;
3801 while (inner_decl != NULL_TREE
3802 && TREE_CODE (inner_decl) == TREE_LIST)
3803 inner_decl = TREE_VALUE (inner_decl);
3804 if (inner_decl == NULL_TREE
3805 || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
3806 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
3807 else if (TREE_CODE (inner_decl) == CALL_EXPR)
3808 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
3809 else if (TREE_CODE (inner_decl) == ARRAY_REF)
3810 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
3811 returned_attrs = decl_attributes (&type,
3812 chainon (returned_attrs, attrs),
3813 attr_flags);
3814 }
3815 else if (TREE_CODE (declarator) == ARRAY_REF)
3816 {
3817 tree itype = NULL_TREE;
3818 tree size = TREE_OPERAND (declarator, 1);
3819 /* The index is a signed object `sizetype' bits wide. */
3820 tree index_type = c_common_signed_type (sizetype);
3821
3822 array_ptr_quals = TREE_TYPE (declarator);
3823 array_parm_static = TREE_STATIC (declarator);
3824
3825 declarator = TREE_OPERAND (declarator, 0);
3826
3827 /* Check for some types that there cannot be arrays of. */
3828
3829 if (VOID_TYPE_P (type))
3830 {
3831 error ("declaration of `%s' as array of voids", name);
3832 type = error_mark_node;
3833 }
3834
3835 if (TREE_CODE (type) == FUNCTION_TYPE)
3836 {
3837 error ("declaration of `%s' as array of functions", name);
3838 type = error_mark_node;
3839 }
3840
3841 if (pedantic && !in_system_header && flexible_array_type_p (type))
3842 pedwarn ("invalid use of structure with flexible array member");
3843
3844 if (size == error_mark_node)
3845 type = error_mark_node;
3846
3847 if (type == error_mark_node)
3848 continue;
3849
3850 /* If size was specified, set ITYPE to a range-type for that size.
3851 Otherwise, ITYPE remains null. finish_decl may figure it out
3852 from an initial value. */
3853
3854 if (size)
3855 {
3856 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3857 STRIP_TYPE_NOPS (size);
3858
3859 if (! INTEGRAL_TYPE_P (TREE_TYPE (size)))
3860 {
3861 error ("size of array `%s' has non-integer type", name);
3862 size = integer_one_node;
3863 }
3864
3865 if (pedantic && integer_zerop (size))
3866 pedwarn ("ISO C forbids zero-size array `%s'", name);
3867
3868 if (TREE_CODE (size) == INTEGER_CST)
3869 {
3870 constant_expression_warning (size);
3871 if (tree_int_cst_sgn (size) < 0)
3872 {
3873 error ("size of array `%s' is negative", name);
3874 size = integer_one_node;
3875 }
3876 }
3877 else
3878 {
3879 /* Make sure the array size remains visibly nonconstant
3880 even if it is (eg) a const variable with known value. */
3881 size_varies = 1;
3882
3883 if (!flag_isoc99 && pedantic)
3884 {
3885 if (TREE_CONSTANT (size))
3886 pedwarn ("ISO C90 forbids array `%s' whose size can't be evaluated",
3887 name);
3888 else
3889 pedwarn ("ISO C90 forbids variable-size array `%s'",
3890 name);
3891 }
3892 }
3893
3894 if (integer_zerop (size))
3895 {
3896 /* A zero-length array cannot be represented with an
3897 unsigned index type, which is what we'll get with
3898 build_index_type. Create an open-ended range instead. */
3899 itype = build_range_type (sizetype, size, NULL_TREE);
3900 }
3901 else
3902 {
3903 /* Compute the maximum valid index, that is, size - 1.
3904 Do the calculation in index_type, so that if it is
3905 a variable the computations will be done in the
3906 proper mode. */
3907 itype = fold (build (MINUS_EXPR, index_type,
3908 convert (index_type, size),
3909 convert (index_type, size_one_node)));
3910
3911 /* If that overflowed, the array is too big.
3912 ??? While a size of INT_MAX+1 technically shouldn't
3913 cause an overflow (because we subtract 1), the overflow
3914 is recorded during the conversion to index_type, before
3915 the subtraction. Handling this case seems like an
3916 unnecessary complication. */
3917 if (TREE_OVERFLOW (itype))
3918 {
3919 error ("size of array `%s' is too large", name);
3920 type = error_mark_node;
3921 continue;
3922 }
3923
3924 if (size_varies)
3925 {
3926 /* We must be able to distinguish the
3927 SAVE_EXPR_CONTEXT for the variably-sized type
3928 so that we can set it correctly in
3929 set_save_expr_context. The convention is
3930 that all SAVE_EXPRs that need to be reset
3931 have NULL_TREE for their SAVE_EXPR_CONTEXT. */
3932 tree cfd = current_function_decl;
3933 if (decl_context == PARM)
3934 current_function_decl = NULL_TREE;
3935 itype = variable_size (itype);
3936 if (decl_context == PARM)
3937 current_function_decl = cfd;
3938 }
3939 itype = build_index_type (itype);
3940 }
3941 }
3942 else if (decl_context == FIELD)
3943 {
3944 if (pedantic && !flag_isoc99 && !in_system_header)
3945 pedwarn ("ISO C90 does not support flexible array members");
3946
3947 /* ISO C99 Flexible array members are effectively identical
3948 to GCC's zero-length array extension. */
3949 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
3950 }
3951
3952 /* If pedantic, complain about arrays of incomplete types. */
3953
3954 if (pedantic && !COMPLETE_TYPE_P (type))
3955 pedwarn ("array type has incomplete element type");
3956
3957 /* Build the array type itself, then merge any constancy or
3958 volatility into the target type. We must do it in this order
3959 to ensure that the TYPE_MAIN_VARIANT field of the array type
3960 is set correctly. */
3961
3962 type = build_array_type (type, itype);
3963 if (type_quals)
3964 type = c_build_qualified_type (type, type_quals);
3965
3966 if (size_varies)
3967 C_TYPE_VARIABLE_SIZE (type) = 1;
3968
3969 /* The GCC extension for zero-length arrays differs from
3970 ISO flexible array members in that sizeof yields zero. */
3971 if (size && integer_zerop (size))
3972 {
3973 layout_type (type);
3974 TYPE_SIZE (type) = bitsize_zero_node;
3975 TYPE_SIZE_UNIT (type) = size_zero_node;
3976 }
3977 if (decl_context != PARM
3978 && (array_ptr_quals != NULL_TREE || array_parm_static))
3979 {
3980 error ("static or type qualifiers in non-parameter array declarator");
3981 array_ptr_quals = NULL_TREE;
3982 array_parm_static = 0;
3983 }
3984 }
3985 else if (TREE_CODE (declarator) == CALL_EXPR)
3986 {
3987 /* Say it's a definition only for the declarator closest to
3988 the identifier, apart possibly from some attributes. */
3989 bool really_funcdef = false;
3990 tree arg_types;
3991 if (funcdef_flag)
3992 {
3993 tree t = TREE_OPERAND (declarator, 0);
3994 while (TREE_CODE (t) == TREE_LIST)
3995 t = TREE_VALUE (t);
3996 really_funcdef = (TREE_CODE (t) == IDENTIFIER_NODE);
3997 }
3998
3999 /* Declaring a function type.
4000 Make sure we have a valid type for the function to return. */
4001 if (type == error_mark_node)
4002 continue;
4003
4004 size_varies = 0;
4005
4006 /* Warn about some types functions can't return. */
4007
4008 if (TREE_CODE (type) == FUNCTION_TYPE)
4009 {
4010 error ("`%s' declared as function returning a function", name);
4011 type = integer_type_node;
4012 }
4013 if (TREE_CODE (type) == ARRAY_TYPE)
4014 {
4015 error ("`%s' declared as function returning an array", name);
4016 type = integer_type_node;
4017 }
4018
4019 /* Construct the function type and go to the next
4020 inner layer of declarator. */
4021
4022 arg_types = grokparms (TREE_OPERAND (declarator, 1),
4023 really_funcdef);
4024 /* Type qualifiers before the return type of the function
4025 qualify the return type, not the function type. */
4026 if (type_quals)
4027 {
4028 /* Type qualifiers on a function return type are normally
4029 permitted by the standard but have no effect, so give a
4030 warning at -Wextra. Qualifiers on a void return type have
4031 meaning as a GNU extension, and are banned on function
4032 definitions in ISO C. FIXME: strictly we shouldn't
4033 pedwarn for qualified void return types except on function
4034 definitions, but not doing so could lead to the undesirable
4035 state of a "volatile void" function return type not being
4036 warned about, and a use of the function being compiled
4037 with GNU semantics, with no diagnostics under -pedantic. */
4038 if (VOID_TYPE_P (type) && pedantic && !in_system_header)
4039 pedwarn ("ISO C forbids qualified void function return type");
4040 else if (extra_warnings
4041 && !(VOID_TYPE_P (type)
4042 && type_quals == TYPE_QUAL_VOLATILE))
4043 warning ("type qualifiers ignored on function return type");
4044
4045 type = c_build_qualified_type (type, type_quals);
4046 }
4047 type_quals = TYPE_UNQUALIFIED;
4048
4049 type = build_function_type (type, arg_types);
4050 declarator = TREE_OPERAND (declarator, 0);
4051
4052 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4053 the formal parameter list of this FUNCTION_TYPE to point to
4054 the FUNCTION_TYPE node itself. */
4055
4056 {
4057 tree link;
4058
4059 for (link = last_function_parm_tags;
4060 link;
4061 link = TREE_CHAIN (link))
4062 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4063 }
4064 }
4065 else if (TREE_CODE (declarator) == INDIRECT_REF)
4066 {
4067 /* Merge any constancy or volatility into the target type
4068 for the pointer. */
4069
4070 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4071 && type_quals)
4072 pedwarn ("ISO C forbids qualified function types");
4073 if (type_quals)
4074 type = c_build_qualified_type (type, type_quals);
4075 type_quals = TYPE_UNQUALIFIED;
4076 size_varies = 0;
4077
4078 type = build_pointer_type (type);
4079
4080 /* Process a list of type modifier keywords
4081 (such as const or volatile) that were given inside the `*'. */
4082
4083 if (TREE_TYPE (declarator))
4084 {
4085 tree typemodlist;
4086 int erred = 0;
4087
4088 constp = 0;
4089 volatilep = 0;
4090 restrictp = 0;
4091 for (typemodlist = TREE_TYPE (declarator); typemodlist;
4092 typemodlist = TREE_CHAIN (typemodlist))
4093 {
4094 tree qualifier = TREE_VALUE (typemodlist);
4095
4096 if (C_IS_RESERVED_WORD (qualifier))
4097 {
4098 if (C_RID_CODE (qualifier) == RID_CONST)
4099 constp++;
4100 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4101 volatilep++;
4102 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4103 restrictp++;
4104 else
4105 erred++;
4106 }
4107 else
4108 erred++;
4109 }
4110
4111 if (erred)
4112 error ("invalid type modifier within pointer declarator");
4113 if (pedantic && !flag_isoc99)
4114 {
4115 if (constp > 1)
4116 pedwarn ("duplicate `const'");
4117 if (volatilep > 1)
4118 pedwarn ("duplicate `volatile'");
4119 if (restrictp > 1)
4120 pedwarn ("duplicate `restrict'");
4121 }
4122
4123 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4124 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4125 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4126 }
4127
4128 declarator = TREE_OPERAND (declarator, 0);
4129 }
4130 else
4131 abort ();
4132
4133 }
4134
4135 /* Now TYPE has the actual type. */
4136
4137 /* Check the type and width of a bit-field. */
4138 if (bitfield)
4139 check_bitfield_type_and_width (&type, width, orig_name);
4140
4141 /* Did array size calculations overflow? */
4142
4143 if (TREE_CODE (type) == ARRAY_TYPE
4144 && COMPLETE_TYPE_P (type)
4145 && TREE_OVERFLOW (TYPE_SIZE (type)))
4146 {
4147 error ("size of array `%s' is too large", name);
4148 /* If we proceed with the array type as it is, we'll eventually
4149 crash in tree_low_cst(). */
4150 type = error_mark_node;
4151 }
4152
4153 /* If this is declaring a typedef name, return a TYPE_DECL. */
4154
4155 if (specbits & (1 << (int) RID_TYPEDEF))
4156 {
4157 tree decl;
4158 /* Note that the grammar rejects storage classes
4159 in typenames, fields or parameters */
4160 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4161 && type_quals)
4162 pedwarn ("ISO C forbids qualified function types");
4163 if (type_quals)
4164 type = c_build_qualified_type (type, type_quals);
4165 decl = build_decl (TYPE_DECL, declarator, type);
4166 if ((specbits & (1 << (int) RID_SIGNED))
4167 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
4168 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4169 decl_attributes (&decl, returned_attrs, 0);
4170 return decl;
4171 }
4172
4173 /* Detect the case of an array type of unspecified size
4174 which came, as such, direct from a typedef name.
4175 We must copy the type, so that each identifier gets
4176 a distinct type, so that each identifier's size can be
4177 controlled separately by its own initializer. */
4178
4179 if (type != 0 && typedef_type != 0
4180 && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0
4181 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
4182 {
4183 type = build_array_type (TREE_TYPE (type), 0);
4184 if (size_varies)
4185 C_TYPE_VARIABLE_SIZE (type) = 1;
4186 }
4187
4188 /* If this is a type name (such as, in a cast or sizeof),
4189 compute the type and return it now. */
4190
4191 if (decl_context == TYPENAME)
4192 {
4193 /* Note that the grammar rejects storage classes
4194 in typenames, fields or parameters */
4195 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4196 && type_quals)
4197 pedwarn ("ISO C forbids const or volatile function types");
4198 if (type_quals)
4199 type = c_build_qualified_type (type, type_quals);
4200 decl_attributes (&type, returned_attrs, 0);
4201 return type;
4202 }
4203
4204 /* Aside from typedefs and type names (handle above),
4205 `void' at top level (not within pointer)
4206 is allowed only in public variables.
4207 We don't complain about parms either, but that is because
4208 a better error message can be made later. */
4209
4210 if (VOID_TYPE_P (type) && decl_context != PARM
4211 && ! ((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4212 && ((specbits & (1 << (int) RID_EXTERN))
4213 || (current_scope == global_scope
4214 && !(specbits
4215 & ((1 << (int) RID_STATIC) | (1 << (int) RID_REGISTER)))))))
4216 {
4217 error ("variable or field `%s' declared void", name);
4218 type = integer_type_node;
4219 }
4220
4221 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4222 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4223
4224 {
4225 tree decl;
4226
4227 if (decl_context == PARM)
4228 {
4229 tree type_as_written;
4230 tree promoted_type;
4231
4232 /* A parameter declared as an array of T is really a pointer to T.
4233 One declared as a function is really a pointer to a function. */
4234
4235 if (TREE_CODE (type) == ARRAY_TYPE)
4236 {
4237 /* Transfer const-ness of array into that of type pointed to. */
4238 type = TREE_TYPE (type);
4239 if (type_quals)
4240 type = c_build_qualified_type (type, type_quals);
4241 type = build_pointer_type (type);
4242 type_quals = TYPE_UNQUALIFIED;
4243 if (array_ptr_quals)
4244 {
4245 tree new_ptr_quals, new_ptr_attrs;
4246 int erred = 0;
4247 split_specs_attrs (array_ptr_quals, &new_ptr_quals, &new_ptr_attrs);
4248 /* We don't yet implement attributes in this context. */
4249 if (new_ptr_attrs != NULL_TREE)
4250 warning ("attributes in parameter array declarator ignored");
4251
4252 constp = 0;
4253 volatilep = 0;
4254 restrictp = 0;
4255 for (; new_ptr_quals; new_ptr_quals = TREE_CHAIN (new_ptr_quals))
4256 {
4257 tree qualifier = TREE_VALUE (new_ptr_quals);
4258
4259 if (C_IS_RESERVED_WORD (qualifier))
4260 {
4261 if (C_RID_CODE (qualifier) == RID_CONST)
4262 constp++;
4263 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4264 volatilep++;
4265 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4266 restrictp++;
4267 else
4268 erred++;
4269 }
4270 else
4271 erred++;
4272 }
4273
4274 if (erred)
4275 error ("invalid type modifier within array declarator");
4276
4277 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4278 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4279 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4280 }
4281 size_varies = 0;
4282 }
4283 else if (TREE_CODE (type) == FUNCTION_TYPE)
4284 {
4285 if (pedantic && type_quals)
4286 pedwarn ("ISO C forbids qualified function types");
4287 if (type_quals)
4288 type = c_build_qualified_type (type, type_quals);
4289 type = build_pointer_type (type);
4290 type_quals = TYPE_UNQUALIFIED;
4291 }
4292 else if (type_quals)
4293 type = c_build_qualified_type (type, type_quals);
4294
4295 type_as_written = type;
4296
4297 decl = build_decl (PARM_DECL, declarator, type);
4298 if (size_varies)
4299 C_DECL_VARIABLE_SIZE (decl) = 1;
4300
4301 /* Compute the type actually passed in the parmlist,
4302 for the case where there is no prototype.
4303 (For example, shorts and chars are passed as ints.)
4304 When there is a prototype, this is overridden later. */
4305
4306 if (type == error_mark_node)
4307 promoted_type = type;
4308 else
4309 promoted_type = c_type_promotes_to (type);
4310
4311 DECL_ARG_TYPE (decl) = promoted_type;
4312 DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
4313 }
4314 else if (decl_context == FIELD)
4315 {
4316 /* Structure field. It may not be a function. */
4317
4318 if (TREE_CODE (type) == FUNCTION_TYPE)
4319 {
4320 error ("field `%s' declared as a function", name);
4321 type = build_pointer_type (type);
4322 }
4323 else if (TREE_CODE (type) != ERROR_MARK
4324 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4325 {
4326 error ("field `%s' has incomplete type", name);
4327 type = error_mark_node;
4328 }
4329 /* Move type qualifiers down to element of an array. */
4330 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4331 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4332 type_quals),
4333 TYPE_DOMAIN (type));
4334 decl = build_decl (FIELD_DECL, declarator, type);
4335 DECL_NONADDRESSABLE_P (decl) = bitfield;
4336
4337 if (size_varies)
4338 C_DECL_VARIABLE_SIZE (decl) = 1;
4339 }
4340 else if (TREE_CODE (type) == FUNCTION_TYPE)
4341 {
4342 /* Every function declaration is "external"
4343 except for those which are inside a function body
4344 in which `auto' is used.
4345 That is a case not specified by ANSI C,
4346 and we use it for forward declarations for nested functions. */
4347 int extern_ref = (!(specbits & (1 << (int) RID_AUTO))
4348 || current_scope == global_scope);
4349
4350 if (specbits & (1 << (int) RID_AUTO)
4351 && (pedantic || current_scope == global_scope))
4352 pedwarn ("invalid storage class for function `%s'", name);
4353 if (specbits & (1 << (int) RID_REGISTER))
4354 error ("invalid storage class for function `%s'", name);
4355 if (specbits & (1 << (int) RID_THREAD))
4356 error ("invalid storage class for function `%s'", name);
4357 /* Function declaration not at file scope.
4358 Storage classes other than `extern' are not allowed
4359 and `extern' makes no difference. */
4360 if (current_scope != global_scope
4361 && (specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_INLINE)))
4362 && pedantic)
4363 pedwarn ("invalid storage class for function `%s'", name);
4364
4365 decl = build_decl (FUNCTION_DECL, declarator, type);
4366 decl = build_decl_attribute_variant (decl, decl_attr);
4367
4368 DECL_LANG_SPECIFIC (decl)
4369 = ggc_alloc_cleared (sizeof (struct lang_decl));
4370
4371 if (pedantic && type_quals && ! DECL_IN_SYSTEM_HEADER (decl))
4372 pedwarn ("ISO C forbids qualified function types");
4373
4374 /* GNU C interprets a `volatile void' return type to indicate
4375 that the function does not return. */
4376 if ((type_quals & TYPE_QUAL_VOLATILE)
4377 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4378 warning ("`noreturn' function returns non-void value");
4379
4380 if (extern_ref)
4381 DECL_EXTERNAL (decl) = 1;
4382 /* Record absence of global scope for `static' or `auto'. */
4383 TREE_PUBLIC (decl)
4384 = !(specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_AUTO)));
4385
4386 if (defaulted_int)
4387 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4388
4389 /* Record presence of `inline', if it is reasonable. */
4390 if (MAIN_NAME_P (declarator))
4391 {
4392 if (inlinep)
4393 warning ("cannot inline function `main'");
4394 }
4395 else if (inlinep)
4396 {
4397 /* Record that the function is declared `inline'. */
4398 DECL_DECLARED_INLINE_P (decl) = 1;
4399
4400 /* Do not mark bare declarations as DECL_INLINE. Doing so
4401 in the presence of multiple declarations can result in
4402 the abstract origin pointing between the declarations,
4403 which will confuse dwarf2out. */
4404 if (initialized)
4405 {
4406 DECL_INLINE (decl) = 1;
4407 if (specbits & (1 << (int) RID_EXTERN))
4408 current_extern_inline = 1;
4409 }
4410 }
4411 /* If -finline-functions, assume it can be inlined. This does
4412 two things: let the function be deferred until it is actually
4413 needed, and let dwarf2 know that the function is inlinable. */
4414 else if (flag_inline_trees == 2 && initialized)
4415 DECL_INLINE (decl) = 1;
4416 }
4417 else
4418 {
4419 /* It's a variable. */
4420 /* An uninitialized decl with `extern' is a reference. */
4421 int extern_ref = !initialized && (specbits & (1 << (int) RID_EXTERN));
4422
4423 /* Move type qualifiers down to element of an array. */
4424 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4425 {
4426 int saved_align = TYPE_ALIGN(type);
4427 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4428 type_quals),
4429 TYPE_DOMAIN (type));
4430 TYPE_ALIGN (type) = saved_align;
4431 }
4432 else if (type_quals)
4433 type = c_build_qualified_type (type, type_quals);
4434
4435 /* It is invalid to create an `extern' declaration for a
4436 variable if there is a global declaration that is
4437 `static' and the global declaration is not visible. */
4438 if (extern_ref && current_scope != global_scope)
4439 {
4440 tree global_decl;
4441
4442 global_decl = identifier_global_value (declarator);
4443 if (global_decl
4444 && TREE_CODE (global_decl) == VAR_DECL
4445 && lookup_name (declarator) != global_decl
4446 && !TREE_PUBLIC (global_decl))
4447 error ("variable previously declared `static' redeclared "
4448 "`extern'");
4449 }
4450
4451 decl = build_decl (VAR_DECL, declarator, type);
4452 if (size_varies)
4453 C_DECL_VARIABLE_SIZE (decl) = 1;
4454
4455 if (inlinep)
4456 pedwarn ("%Jvariable '%D' declared `inline'", decl, decl);
4457
4458 DECL_EXTERNAL (decl) = extern_ref;
4459
4460 /* At file scope, the presence of a `static' or `register' storage
4461 class specifier, or the absence of all storage class specifiers
4462 makes this declaration a definition (perhaps tentative). Also,
4463 the absence of both `static' and `register' makes it public. */
4464 if (current_scope == global_scope)
4465 {
4466 TREE_PUBLIC (decl) = !(specbits & ((1 << (int) RID_STATIC)
4467 | (1 << (int) RID_REGISTER)));
4468 TREE_STATIC (decl) = !extern_ref;
4469 }
4470 /* Not at file scope, only `static' makes a static definition. */
4471 else
4472 {
4473 TREE_STATIC (decl) = (specbits & (1 << (int) RID_STATIC)) != 0;
4474 TREE_PUBLIC (decl) = extern_ref;
4475 }
4476
4477 if (specbits & 1 << (int) RID_THREAD)
4478 {
4479 if (targetm.have_tls)
4480 DECL_THREAD_LOCAL (decl) = 1;
4481 else
4482 /* A mere warning is sure to result in improper semantics
4483 at runtime. Don't bother to allow this to compile. */
4484 error ("thread-local storage not supported for this target");
4485 }
4486 }
4487
4488 /* Record `register' declaration for warnings on &
4489 and in case doing stupid register allocation. */
4490
4491 if (specbits & (1 << (int) RID_REGISTER))
4492 DECL_REGISTER (decl) = 1;
4493
4494 /* Record constancy and volatility. */
4495 c_apply_type_quals_to_decl (type_quals, decl);
4496
4497 /* If a type has volatile components, it should be stored in memory.
4498 Otherwise, the fact that those components are volatile
4499 will be ignored, and would even crash the compiler. */
4500 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
4501 c_mark_addressable (decl);
4502
4503#ifdef ENABLE_CHECKING
4504 /* This is the earliest point at which we might know the assembler
4505 name of a variable. Thus, if it's known before this, die horribly. */
4506 if (DECL_ASSEMBLER_NAME_SET_P (decl))
4507 abort ();
4508#endif
4509
4510 decl_attributes (&decl, returned_attrs, 0);
4511
4512 return decl;
4513 }
4514}
4515
4516/* Decode the parameter-list info for a function type or function definition.
4517 The argument is the value returned by `get_parm_info' (or made in parse.y
4518 if there is an identifier list instead of a parameter decl list).
4519 These two functions are separate because when a function returns
4520 or receives functions then each is called multiple times but the order
4521 of calls is different. The last call to `grokparms' is always the one
4522 that contains the formal parameter names of a function definition.
4523
4524 Store in `last_function_parms' a chain of the decls of parms.
4525 Also store in `last_function_parm_tags' a chain of the struct, union,
4526 and enum tags declared among the parms.
4527
4528 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4529
4530 FUNCDEF_FLAG is nonzero for a function definition, 0 for
4531 a mere declaration. A nonempty identifier-list gets an error message
4532 when FUNCDEF_FLAG is zero. */
4533
4534static tree
4535grokparms (tree parms_info, int funcdef_flag)
4536{
4537 tree first_parm = TREE_CHAIN (parms_info);
4538
4539 last_function_parms = TREE_PURPOSE (parms_info);
4540 last_function_parm_tags = TREE_VALUE (parms_info);
4541 last_function_parm_others = TREE_TYPE (parms_info);
4542
4543 if (warn_strict_prototypes && first_parm == 0 && !funcdef_flag
4544 && !in_system_header)
4545 warning ("function declaration isn't a prototype");
4546
4547 if (first_parm != 0
4548 && TREE_CODE (TREE_VALUE (first_parm)) == IDENTIFIER_NODE)
4549 {
4550 if (! funcdef_flag)
4551 pedwarn ("parameter names (without types) in function declaration");
4552
4553 last_function_parms = first_parm;
4554 return 0;
4555 }
4556 else
4557 {
4558 tree parm;
4559 tree typelt;
4560 /* If the arg types are incomplete in a declaration,
4561 they must include undefined tags.
4562 These tags can never be defined in the scope of the declaration,
4563 so the types can never be completed,
4564 and no call can be compiled successfully. */
4565
4566 for (parm = last_function_parms, typelt = first_parm;
4567 parm;
4568 parm = TREE_CHAIN (parm))
4569 /* Skip over any enumeration constants declared here. */
4570 if (TREE_CODE (parm) == PARM_DECL)
4571 {
4572 /* Barf if the parameter itself has an incomplete type. */
4573 tree type = TREE_VALUE (typelt);
4574 if (type == error_mark_node)
4575 continue;
4576 if (!COMPLETE_TYPE_P (type))
4577 {
4578 if (funcdef_flag && DECL_NAME (parm) != 0)
4579 error ("parameter `%s' has incomplete type",
4580 IDENTIFIER_POINTER (DECL_NAME (parm)));
4581 else
4582 warning ("parameter has incomplete type");
4583 if (funcdef_flag)
4584 {
4585 TREE_VALUE (typelt) = error_mark_node;
4586 TREE_TYPE (parm) = error_mark_node;
4587 }
4588 }
4589 typelt = TREE_CHAIN (typelt);
4590 }
4591
4592 return first_parm;
4593 }
4594}
4595
4596/* Return a tree_list node with info on a parameter list just parsed.
4597 The TREE_PURPOSE is a list of decls of those parms.
4598 The TREE_VALUE is a list of structure, union and enum tags defined.
4599 The TREE_CHAIN is a list of argument types to go in the FUNCTION_TYPE.
4600 The TREE_TYPE is a list of non-parameter decls which appeared with the
4601 parameters.
4602 This tree_list node is later fed to `grokparms'.
4603
4604 VOID_AT_END nonzero means append `void' to the end of the type-list.
4605 Zero means the parmlist ended with an ellipsis so don't append `void'. */
4606
4607tree
4608get_parm_info (int void_at_end)
4609{
4610 tree decl, type, list;
4611 tree types = 0;
4612 tree *last_type = &types;
4613 tree tags = current_scope->tags;
4614 tree parms = current_scope->parms;
4615 tree others = current_scope->names;
4616 static bool explained_incomplete_types = false;
4617 bool gave_void_only_once_err = false;
4618
4619 /* Just "void" (and no ellipsis) is special. There are really no parms.
4620 But if the "void" is qualified (by "const" or "volatile"), or has a
4621 storage class specifier ("register"), then the behavior is undefined;
4622 issue an error. Typedefs for "void" are OK (see DR#157). */
4623 if (void_at_end && parms != 0
4624 && TREE_CHAIN (parms) == 0
4625 && VOID_TYPE_P (TREE_TYPE (parms))
4626 && !DECL_NAME (parms))
4627 {
4628 if (TREE_THIS_VOLATILE (parms)
4629 || TREE_READONLY (parms)
4630 || DECL_REGISTER (parms))
4631 error ("\"void\" as only parameter may not be qualified");
4632
4633 return tree_cons (0, 0, tree_cons (0, void_type_node, 0));
4634 }
4635
4636 /* Sanity check all of the parameter declarations. */
4637 for (decl = parms; decl; decl = TREE_CHAIN (decl))
4638 {
4639 if (TREE_CODE (decl) != PARM_DECL)
4640 abort ();
4641 if (TREE_ASM_WRITTEN (decl))
4642 abort ();
4643
4644 /* Since there is a prototype, args are passed in their
4645 declared types. The back end may override this. */
4646 type = TREE_TYPE (decl);
4647 DECL_ARG_TYPE (decl) = type;
4648
4649 /* Check for (..., void, ...) and issue an error. */
4650 if (VOID_TYPE_P (type) && !DECL_NAME (decl) && !gave_void_only_once_err)
4651 {
4652 error ("\"void\" must be the only parameter");
4653 gave_void_only_once_err = true;
4654 }
4655
4656 type = build_tree_list (0, type);
4657 *last_type = type;
4658 last_type = &TREE_CHAIN (type);
4659 }
4660
4661 /* Check the list of non-parameter decls for any forward parm decls
4662 that never got real decls. */
4663 for (decl = others; decl; decl = TREE_CHAIN (decl))
4664 if (TREE_CODE (decl) == PARM_DECL)
4665 {
4666 if (!TREE_ASM_WRITTEN (decl))
4667 abort ();
4668
4669 error ("%Jparameter \"%D\" has just a forward declaration",
4670 decl, decl);
4671 }
4672
4673 /* Warn about any struct, union or enum tags defined within this
4674 list. The scope of such types is limited to this declaration,
4675 which is rarely if ever desirable (it's impossible to call such
4676 a function with type-correct arguments). */
4677 for (decl = tags; decl; decl = TREE_CHAIN (decl))
4678 {
4679 enum tree_code code = TREE_CODE (TREE_VALUE (decl));
4680 const char *keyword;
4681 /* An anonymous union parm type is meaningful as a GNU extension.
4682 So don't warn for that. */
4683 if (code == UNION_TYPE && TREE_PURPOSE (decl) == 0 && !pedantic)
4684 continue;
4685
4686 /* The keyword should not be translated. */
4687 switch (code)
4688 {
4689 case RECORD_TYPE: keyword = "struct"; break;
4690 case UNION_TYPE: keyword = "union"; break;
4691 case ENUMERAL_TYPE: keyword = "enum"; break;
4692 default: abort ();
4693 }
4694
4695 if (TREE_PURPOSE (decl))
4696 /* The first %s will be one of 'struct', 'union', or 'enum'. */
4697 warning ("\"%s %s\" declared inside parameter list",
4698 keyword, IDENTIFIER_POINTER (TREE_PURPOSE (decl)));
4699 else
4700 /* The %s will be one of 'struct', 'union', or 'enum'. */
4701 warning ("anonymous %s declared inside parameter list", keyword);
4702
4703 if (! explained_incomplete_types)
4704 {
4705 warning ("its scope is only this definition or declaration,"
4706 " which is probably not what you want");
4707 explained_incomplete_types = true;
4708 }
4709 }
4710
4711
4712 if (void_at_end)
4713 {
4714 type = build_tree_list (0, void_type_node);
4715 *last_type = type;
4716 }
4717
4718 list = tree_cons (parms, tags, types);
4719 TREE_TYPE (list) = others;
4720 return list;
4721}
4722
4723/* Get the struct, enum or union (CODE says which) with tag NAME.
4724 Define the tag as a forward-reference if it is not defined. */
4725
4726tree
4727xref_tag (enum tree_code code, tree name)
4728{
4729 /* If a cross reference is requested, look up the type
4730 already defined for this tag and return it. */
4731
4732 tree ref = lookup_tag (code, name, 0);
4733 /* If this is the right type of tag, return what we found.
4734 (This reference will be shadowed by shadow_tag later if appropriate.)
4735 If this is the wrong type of tag, do not return it. If it was the
4736 wrong type in the same scope, we will have had an error
4737 message already; if in a different scope and declaring
4738 a name, pending_xref_error will give an error message; but if in a
4739 different scope and not declaring a name, this tag should
4740 shadow the previous declaration of a different type of tag, and
4741 this would not work properly if we return the reference found.
4742 (For example, with "struct foo" in an outer scope, "union foo;"
4743 must shadow that tag with a new one of union type.) */
4744 if (ref && TREE_CODE (ref) == code)
4745 return ref;
4746
4747 /* If no such tag is yet defined, create a forward-reference node
4748 and record it as the "definition".
4749 When a real declaration of this type is found,
4750 the forward-reference will be altered into a real type. */
4751
4752 ref = make_node (code);
4753 if (code == ENUMERAL_TYPE)
4754 {
4755 /* Give the type a default layout like unsigned int
4756 to avoid crashing if it does not get defined. */
4757 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
4758 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
4759 TYPE_USER_ALIGN (ref) = 0;
4760 TREE_UNSIGNED (ref) = 1;
4761 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
4762 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
4763 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
4764 }
4765
4766 pushtag (name, ref);
4767
4768 return ref;
4769}
4770
4771/* Make sure that the tag NAME is defined *in the current scope*
4772 at least as a forward reference.
4773 CODE says which kind of tag NAME ought to be. */
4774
4775tree
4776start_struct (enum tree_code code, tree name)
4777{
4778 /* If there is already a tag defined at this scope
4779 (as a forward reference), just return it. */
4780
4781 tree ref = 0;
4782
4783 if (name != 0)
4784 ref = lookup_tag (code, name, 1);
4785 if (ref && TREE_CODE (ref) == code)
4786 {
2994 immediate_size_expand = save_immediate_size_expand;
2995}
2996
2997/* Mark all the parameter declarations to date as forward decls,
2998 shift them to the variables list, and reset the parameters list.
2999 Also diagnose use of this extension. */
3000
3001void
3002mark_forward_parm_decls (void)
3003{
3004 tree parm;
3005
3006 if (pedantic && !current_scope->warned_forward_parm_decls)
3007 {
3008 pedwarn ("ISO C forbids forward parameter declarations");
3009 current_scope->warned_forward_parm_decls = true;
3010 }
3011
3012 for (parm = current_scope->parms; parm; parm = TREE_CHAIN (parm))
3013 TREE_ASM_WRITTEN (parm) = 1;
3014
3015 SCOPE_LIST_CONCAT (current_scope, names, current_scope, parms);
3016 current_scope->parms = 0;
3017 current_scope->parms_last = 0;
3018}
3019
3020static GTY(()) int compound_literal_number;
3021
3022/* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3023 literal, which may be an incomplete array type completed by the
3024 initializer; INIT is a CONSTRUCTOR that initializes the compound
3025 literal. */
3026
3027tree
3028build_compound_literal (tree type, tree init)
3029{
3030 /* We do not use start_decl here because we have a type, not a declarator;
3031 and do not use finish_decl because the decl should be stored inside
3032 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_STMT. */
3033 tree decl = build_decl (VAR_DECL, NULL_TREE, type);
3034 tree complit;
3035 tree stmt;
3036 DECL_EXTERNAL (decl) = 0;
3037 TREE_PUBLIC (decl) = 0;
3038 TREE_STATIC (decl) = (current_scope == global_scope);
3039 DECL_CONTEXT (decl) = current_function_decl;
3040 TREE_USED (decl) = 1;
3041 TREE_TYPE (decl) = type;
3042 TREE_READONLY (decl) = TREE_READONLY (type);
3043 store_init_value (decl, init);
3044
3045 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3046 {
3047 int failure = complete_array_type (type, DECL_INITIAL (decl), 1);
3048 if (failure)
3049 abort ();
3050 }
3051
3052 type = TREE_TYPE (decl);
3053 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3054 return error_mark_node;
3055
3056 stmt = build_stmt (DECL_STMT, decl);
3057 complit = build1 (COMPOUND_LITERAL_EXPR, TREE_TYPE (decl), stmt);
3058 TREE_SIDE_EFFECTS (complit) = 1;
3059
3060 layout_decl (decl, 0);
3061
3062 if (TREE_STATIC (decl))
3063 {
3064 /* This decl needs a name for the assembler output. We also need
3065 a unique suffix to be added to the name. */
3066 char *name;
3067
3068 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
3069 compound_literal_number);
3070 compound_literal_number++;
3071 DECL_NAME (decl) = get_identifier (name);
3072 DECL_DEFER_OUTPUT (decl) = 1;
3073 DECL_COMDAT (decl) = 1;
3074 DECL_ARTIFICIAL (decl) = 1;
3075 pushdecl (decl);
3076 rest_of_decl_compilation (decl, NULL, 1, 0);
3077 }
3078
3079 return complit;
3080}
3081
3082/* Make TYPE a complete type based on INITIAL_VALUE.
3083 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
3084 2 if there was no information (in which case assume 1 if DO_DEFAULT). */
3085
3086int
3087complete_array_type (tree type, tree initial_value, int do_default)
3088{
3089 tree maxindex = NULL_TREE;
3090 int value = 0;
3091
3092 if (initial_value)
3093 {
3094 /* Note MAXINDEX is really the maximum index,
3095 one less than the size. */
3096 if (TREE_CODE (initial_value) == STRING_CST)
3097 {
3098 int eltsize
3099 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
3100 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
3101 / eltsize) - 1, 0);
3102 }
3103 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
3104 {
3105 tree elts = CONSTRUCTOR_ELTS (initial_value);
3106 maxindex = build_int_2 (-1, -1);
3107 for (; elts; elts = TREE_CHAIN (elts))
3108 {
3109 if (TREE_PURPOSE (elts))
3110 maxindex = TREE_PURPOSE (elts);
3111 else
3112 maxindex = fold (build (PLUS_EXPR, integer_type_node,
3113 maxindex, integer_one_node));
3114 }
3115 maxindex = copy_node (maxindex);
3116 }
3117 else
3118 {
3119 /* Make an error message unless that happened already. */
3120 if (initial_value != error_mark_node)
3121 value = 1;
3122
3123 /* Prevent further error messages. */
3124 maxindex = build_int_2 (0, 0);
3125 }
3126 }
3127
3128 if (!maxindex)
3129 {
3130 if (do_default)
3131 maxindex = build_int_2 (0, 0);
3132 value = 2;
3133 }
3134
3135 if (maxindex)
3136 {
3137 TYPE_DOMAIN (type) = build_index_type (maxindex);
3138 if (!TREE_TYPE (maxindex))
3139 TREE_TYPE (maxindex) = TYPE_DOMAIN (type);
3140 }
3141
3142 /* Lay out the type now that we can get the real answer. */
3143
3144 layout_type (type);
3145
3146 return value;
3147}
3148
3149/* Determine whether TYPE is a structure with a flexible array member,
3150 or a union containing such a structure (possibly recursively). */
3151
3152static bool
3153flexible_array_type_p (tree type)
3154{
3155 tree x;
3156 switch (TREE_CODE (type))
3157 {
3158 case RECORD_TYPE:
3159 x = TYPE_FIELDS (type);
3160 if (x == NULL_TREE)
3161 return false;
3162 while (TREE_CHAIN (x) != NULL_TREE)
3163 x = TREE_CHAIN (x);
3164 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3165 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3166 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3167 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3168 return true;
3169 return false;
3170 case UNION_TYPE:
3171 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3172 {
3173 if (flexible_array_type_p (TREE_TYPE (x)))
3174 return true;
3175 }
3176 return false;
3177 default:
3178 return false;
3179 }
3180}
3181
3182/* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3183 replacing with appropriate values if they are invalid. */
3184static void
3185check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3186{
3187 tree type_mv;
3188 unsigned int max_width;
3189 unsigned HOST_WIDE_INT w;
3190 const char *name = orig_name ? orig_name: _("<anonymous>");
3191
3192 /* Necessary? */
3193 STRIP_NOPS (*width);
3194
3195 /* Detect and ignore out of range field width and process valid
3196 field widths. */
3197 if (TREE_CODE (*width) != INTEGER_CST)
3198 {
3199 error ("bit-field `%s' width not an integer constant", name);
3200 *width = integer_one_node;
3201 }
3202 else
3203 {
3204 constant_expression_warning (*width);
3205 if (tree_int_cst_sgn (*width) < 0)
3206 {
3207 error ("negative width in bit-field `%s'", name);
3208 *width = integer_one_node;
3209 }
3210 else if (integer_zerop (*width) && orig_name)
3211 {
3212 error ("zero width for bit-field `%s'", name);
3213 *width = integer_one_node;
3214 }
3215 }
3216
3217 /* Detect invalid bit-field type. */
3218 if (TREE_CODE (*type) != INTEGER_TYPE
3219 && TREE_CODE (*type) != BOOLEAN_TYPE
3220 && TREE_CODE (*type) != ENUMERAL_TYPE)
3221 {
3222 error ("bit-field `%s' has invalid type", name);
3223 *type = unsigned_type_node;
3224 }
3225
3226 type_mv = TYPE_MAIN_VARIANT (*type);
3227 if (pedantic
3228 && type_mv != integer_type_node
3229 && type_mv != unsigned_type_node
3230 && type_mv != boolean_type_node)
3231 pedwarn ("type of bit-field `%s' is a GCC extension", name);
3232
3233 if (type_mv == boolean_type_node)
3234 max_width = CHAR_TYPE_SIZE;
3235 else
3236 max_width = TYPE_PRECISION (*type);
3237
3238 if (0 < compare_tree_int (*width, max_width))
3239 {
3240 error ("width of `%s' exceeds its type", name);
3241 w = max_width;
3242 *width = build_int_2 (w, 0);
3243 }
3244 else
3245 w = tree_low_cst (*width, 1);
3246
3247 if (TREE_CODE (*type) == ENUMERAL_TYPE
3248 && (w < min_precision (TYPE_MIN_VALUE (*type), TREE_UNSIGNED (*type))
3249 || w < min_precision (TYPE_MAX_VALUE (*type), TREE_UNSIGNED (*type))))
3250 warning ("`%s' is narrower than values of its type", name);
3251}
3252
3253/* Given declspecs and a declarator,
3254 determine the name and type of the object declared
3255 and construct a ..._DECL node for it.
3256 (In one case we can return a ..._TYPE node instead.
3257 For invalid input we sometimes return 0.)
3258
3259 DECLSPECS is a chain of tree_list nodes whose value fields
3260 are the storage classes and type specifiers.
3261
3262 DECL_CONTEXT says which syntactic context this declaration is in:
3263 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3264 FUNCDEF for a function definition. Like NORMAL but a few different
3265 error messages in each case. Return value may be zero meaning
3266 this definition is too screwy to try to parse.
3267 PARM for a parameter declaration (either within a function prototype
3268 or before a function body). Make a PARM_DECL, or return void_type_node.
3269 TYPENAME if for a typename (in a cast or sizeof).
3270 Don't make a DECL node; just return the ..._TYPE node.
3271 FIELD for a struct or union field; make a FIELD_DECL.
3272 INITIALIZED is 1 if the decl has an initializer.
3273 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3274 representing the width of the bit-field.
3275
3276 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3277 It may also be so in the PARM case, for a prototype where the
3278 argument type is specified but not the name.
3279
3280 This function is where the complicated C meanings of `static'
3281 and `extern' are interpreted. */
3282
3283static tree
3284grokdeclarator (tree declarator, tree declspecs,
3285 enum decl_context decl_context, int initialized, tree *width)
3286{
3287 int specbits = 0;
3288 tree spec;
3289 tree type = NULL_TREE;
3290 int longlong = 0;
3291 int constp;
3292 int restrictp;
3293 int volatilep;
3294 int type_quals = TYPE_UNQUALIFIED;
3295 int inlinep;
3296 int explicit_int = 0;
3297 int explicit_char = 0;
3298 int defaulted_int = 0;
3299 tree typedef_decl = 0;
3300 const char *name, *orig_name;
3301 tree typedef_type = 0;
3302 int funcdef_flag = 0;
3303 enum tree_code innermost_code = ERROR_MARK;
3304 int size_varies = 0;
3305 tree decl_attr = NULL_TREE;
3306 tree array_ptr_quals = NULL_TREE;
3307 int array_parm_static = 0;
3308 tree returned_attrs = NULL_TREE;
3309 bool bitfield = width != NULL;
3310 tree element_type;
3311
3312 if (decl_context == FUNCDEF)
3313 funcdef_flag = 1, decl_context = NORMAL;
3314
3315 /* Look inside a declarator for the name being declared
3316 and get it as a string, for an error message. */
3317 {
3318 tree decl = declarator;
3319 name = 0;
3320
3321 while (decl)
3322 switch (TREE_CODE (decl))
3323 {
3324 case ARRAY_REF:
3325 case INDIRECT_REF:
3326 case CALL_EXPR:
3327 innermost_code = TREE_CODE (decl);
3328 decl = TREE_OPERAND (decl, 0);
3329 break;
3330
3331 case TREE_LIST:
3332 decl = TREE_VALUE (decl);
3333 break;
3334
3335 case IDENTIFIER_NODE:
3336 name = IDENTIFIER_POINTER (decl);
3337 decl = 0;
3338 break;
3339
3340 default:
3341 abort ();
3342 }
3343 orig_name = name;
3344 if (name == 0)
3345 name = "type name";
3346 }
3347
3348 /* A function definition's declarator must have the form of
3349 a function declarator. */
3350
3351 if (funcdef_flag && innermost_code != CALL_EXPR)
3352 return 0;
3353
3354 /* If this looks like a function definition, make it one,
3355 even if it occurs where parms are expected.
3356 Then store_parm_decls will reject it and not use it as a parm. */
3357 if (decl_context == NORMAL && !funcdef_flag
3358 && current_scope->parm_flag)
3359 decl_context = PARM;
3360
3361 /* Look through the decl specs and record which ones appear.
3362 Some typespecs are defined as built-in typenames.
3363 Others, the ones that are modifiers of other types,
3364 are represented by bits in SPECBITS: set the bits for
3365 the modifiers that appear. Storage class keywords are also in SPECBITS.
3366
3367 If there is a typedef name or a type, store the type in TYPE.
3368 This includes builtin typedefs such as `int'.
3369
3370 Set EXPLICIT_INT or EXPLICIT_CHAR if the type is `int' or `char'
3371 and did not come from a user typedef.
3372
3373 Set LONGLONG if `long' is mentioned twice. */
3374
3375 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
3376 {
3377 tree id = TREE_VALUE (spec);
3378
3379 /* If the entire declaration is itself tagged as deprecated then
3380 suppress reports of deprecated items. */
3381 if (id && TREE_DEPRECATED (id))
3382 {
3383 if (deprecated_state != DEPRECATED_SUPPRESS)
3384 warn_deprecated_use (id);
3385 }
3386
3387 if (id == ridpointers[(int) RID_INT])
3388 explicit_int = 1;
3389 if (id == ridpointers[(int) RID_CHAR])
3390 explicit_char = 1;
3391
3392 if (TREE_CODE (id) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (id))
3393 {
3394 enum rid i = C_RID_CODE (id);
3395 if ((int) i <= (int) RID_LAST_MODIFIER)
3396 {
3397 if (i == RID_LONG && (specbits & (1 << (int) RID_LONG)))
3398 {
3399 if (longlong)
3400 error ("`long long long' is too long for GCC");
3401 else
3402 {
3403 if (pedantic && !flag_isoc99 && ! in_system_header
3404 && warn_long_long)
3405 pedwarn ("ISO C90 does not support `long long'");
3406 longlong = 1;
3407 }
3408 }
3409 else if (specbits & (1 << (int) i))
3410 {
3411 if (i == RID_CONST || i == RID_VOLATILE || i == RID_RESTRICT)
3412 {
3413 if (pedantic && !flag_isoc99)
3414 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
3415 }
3416 else
3417 error ("duplicate `%s'", IDENTIFIER_POINTER (id));
3418 }
3419
3420 /* Diagnose "__thread extern". Recall that this list
3421 is in the reverse order seen in the text. */
3422 if (i == RID_THREAD
3423 && (specbits & (1 << (int) RID_EXTERN
3424 | 1 << (int) RID_STATIC)))
3425 {
3426 if (specbits & 1 << (int) RID_EXTERN)
3427 error ("`__thread' before `extern'");
3428 else
3429 error ("`__thread' before `static'");
3430 }
3431
3432 specbits |= 1 << (int) i;
3433 goto found;
3434 }
3435 }
3436 if (type)
3437 error ("two or more data types in declaration of `%s'", name);
3438 /* Actual typedefs come to us as TYPE_DECL nodes. */
3439 else if (TREE_CODE (id) == TYPE_DECL)
3440 {
3441 if (TREE_TYPE (id) == error_mark_node)
3442 ; /* Allow the type to default to int to avoid cascading errors. */
3443 else
3444 {
3445 type = TREE_TYPE (id);
3446 decl_attr = DECL_ATTRIBUTES (id);
3447 typedef_decl = id;
3448 }
3449 }
3450 /* Built-in types come as identifiers. */
3451 else if (TREE_CODE (id) == IDENTIFIER_NODE)
3452 {
3453 tree t = lookup_name (id);
3454 if (TREE_TYPE (t) == error_mark_node)
3455 ;
3456 else if (!t || TREE_CODE (t) != TYPE_DECL)
3457 error ("`%s' fails to be a typedef or built in type",
3458 IDENTIFIER_POINTER (id));
3459 else
3460 {
3461 type = TREE_TYPE (t);
3462 typedef_decl = t;
3463 }
3464 }
3465 else if (TREE_CODE (id) != ERROR_MARK)
3466 type = id;
3467
3468 found:
3469 ;
3470 }
3471
3472 typedef_type = type;
3473 if (type)
3474 size_varies = C_TYPE_VARIABLE_SIZE (type);
3475
3476 /* No type at all: default to `int', and set DEFAULTED_INT
3477 because it was not a user-defined typedef. */
3478
3479 if (type == 0)
3480 {
3481 if ((! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3482 | (1 << (int) RID_SIGNED)
3483 | (1 << (int) RID_UNSIGNED)
3484 | (1 << (int) RID_COMPLEX))))
3485 /* Don't warn about typedef foo = bar. */
3486 && ! (specbits & (1 << (int) RID_TYPEDEF) && initialized)
3487 && ! in_system_header)
3488 {
3489 /* Issue a warning if this is an ISO C 99 program or if -Wreturn-type
3490 and this is a function, or if -Wimplicit; prefer the former
3491 warning since it is more explicit. */
3492 if ((warn_implicit_int || warn_return_type || flag_isoc99)
3493 && funcdef_flag)
3494 warn_about_return_type = 1;
3495 else if (warn_implicit_int || flag_isoc99)
3496 pedwarn_c99 ("type defaults to `int' in declaration of `%s'",
3497 name);
3498 }
3499
3500 defaulted_int = 1;
3501 type = integer_type_node;
3502 }
3503
3504 /* Now process the modifiers that were specified
3505 and check for invalid combinations. */
3506
3507 /* Long double is a special combination. */
3508
3509 if ((specbits & 1 << (int) RID_LONG) && ! longlong
3510 && TYPE_MAIN_VARIANT (type) == double_type_node)
3511 {
3512 specbits &= ~(1 << (int) RID_LONG);
3513 type = long_double_type_node;
3514 }
3515
3516 /* Check all other uses of type modifiers. */
3517
3518 if (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3519 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED)))
3520 {
3521 int ok = 0;
3522
3523 if ((specbits & 1 << (int) RID_LONG)
3524 && (specbits & 1 << (int) RID_SHORT))
3525 error ("both long and short specified for `%s'", name);
3526 else if (((specbits & 1 << (int) RID_LONG)
3527 || (specbits & 1 << (int) RID_SHORT))
3528 && explicit_char)
3529 error ("long or short specified with char for `%s'", name);
3530 else if (((specbits & 1 << (int) RID_LONG)
3531 || (specbits & 1 << (int) RID_SHORT))
3532 && TREE_CODE (type) == REAL_TYPE)
3533 {
3534 static int already = 0;
3535
3536 error ("long or short specified with floating type for `%s'", name);
3537 if (! already && ! pedantic)
3538 {
3539 error ("the only valid combination is `long double'");
3540 already = 1;
3541 }
3542 }
3543 else if ((specbits & 1 << (int) RID_SIGNED)
3544 && (specbits & 1 << (int) RID_UNSIGNED))
3545 error ("both signed and unsigned specified for `%s'", name);
3546 else if (TREE_CODE (type) != INTEGER_TYPE)
3547 error ("long, short, signed or unsigned invalid for `%s'", name);
3548 else
3549 {
3550 ok = 1;
3551 if (!explicit_int && !defaulted_int && !explicit_char)
3552 {
3553 error ("long, short, signed or unsigned used invalidly for `%s'",
3554 name);
3555 ok = 0;
3556 }
3557 }
3558
3559 /* Discard the type modifiers if they are invalid. */
3560 if (! ok)
3561 {
3562 specbits &= ~((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3563 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED));
3564 longlong = 0;
3565 }
3566 }
3567
3568 if ((specbits & (1 << (int) RID_COMPLEX))
3569 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
3570 {
3571 error ("complex invalid for `%s'", name);
3572 specbits &= ~(1 << (int) RID_COMPLEX);
3573 }
3574
3575 /* Decide whether an integer type is signed or not.
3576 Optionally treat bit-fields as signed by default. */
3577 if (specbits & 1 << (int) RID_UNSIGNED
3578 || (bitfield && ! flag_signed_bitfields
3579 && (explicit_int || defaulted_int || explicit_char
3580 /* A typedef for plain `int' without `signed'
3581 can be controlled just like plain `int'. */
3582 || ! (typedef_decl != 0
3583 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
3584 && TREE_CODE (type) != ENUMERAL_TYPE
3585 && !(specbits & 1 << (int) RID_SIGNED)))
3586 {
3587 if (longlong)
3588 type = long_long_unsigned_type_node;
3589 else if (specbits & 1 << (int) RID_LONG)
3590 type = long_unsigned_type_node;
3591 else if (specbits & 1 << (int) RID_SHORT)
3592 type = short_unsigned_type_node;
3593 else if (type == char_type_node)
3594 type = unsigned_char_type_node;
3595 else if (typedef_decl)
3596 type = c_common_unsigned_type (type);
3597 else
3598 type = unsigned_type_node;
3599 }
3600 else if ((specbits & 1 << (int) RID_SIGNED)
3601 && type == char_type_node)
3602 type = signed_char_type_node;
3603 else if (longlong)
3604 type = long_long_integer_type_node;
3605 else if (specbits & 1 << (int) RID_LONG)
3606 type = long_integer_type_node;
3607 else if (specbits & 1 << (int) RID_SHORT)
3608 type = short_integer_type_node;
3609
3610 if (specbits & 1 << (int) RID_COMPLEX)
3611 {
3612 if (pedantic && !flag_isoc99)
3613 pedwarn ("ISO C90 does not support complex types");
3614 /* If we just have "complex", it is equivalent to
3615 "complex double", but if any modifiers at all are specified it is
3616 the complex form of TYPE. E.g, "complex short" is
3617 "complex short int". */
3618
3619 if (defaulted_int && ! longlong
3620 && ! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3621 | (1 << (int) RID_SIGNED)
3622 | (1 << (int) RID_UNSIGNED))))
3623 {
3624 if (pedantic)
3625 pedwarn ("ISO C does not support plain `complex' meaning `double complex'");
3626 type = complex_double_type_node;
3627 }
3628 else if (type == integer_type_node)
3629 {
3630 if (pedantic)
3631 pedwarn ("ISO C does not support complex integer types");
3632 type = complex_integer_type_node;
3633 }
3634 else if (type == float_type_node)
3635 type = complex_float_type_node;
3636 else if (type == double_type_node)
3637 type = complex_double_type_node;
3638 else if (type == long_double_type_node)
3639 type = complex_long_double_type_node;
3640 else
3641 {
3642 if (pedantic)
3643 pedwarn ("ISO C does not support complex integer types");
3644 type = build_complex_type (type);
3645 }
3646 }
3647
3648 /* Figure out the type qualifiers for the declaration. There are
3649 two ways a declaration can become qualified. One is something
3650 like `const int i' where the `const' is explicit. Another is
3651 something like `typedef const int CI; CI i' where the type of the
3652 declaration contains the `const'. A third possibility is that
3653 there is a type qualifier on the element type of a typedefed
3654 array type, in which case we should extract that qualifier so
3655 that c_apply_type_quals_to_decls receives the full list of
3656 qualifiers to work with (C90 is not entirely clear about whether
3657 duplicate qualifiers should be diagnosed in this case, but it
3658 seems most appropriate to do so). */
3659 element_type = strip_array_types (type);
3660 constp = !! (specbits & 1 << (int) RID_CONST) + TYPE_READONLY (element_type);
3661 restrictp
3662 = !! (specbits & 1 << (int) RID_RESTRICT) + TYPE_RESTRICT (element_type);
3663 volatilep
3664 = !! (specbits & 1 << (int) RID_VOLATILE) + TYPE_VOLATILE (element_type);
3665 inlinep = !! (specbits & (1 << (int) RID_INLINE));
3666 if (pedantic && !flag_isoc99)
3667 {
3668 if (constp > 1)
3669 pedwarn ("duplicate `const'");
3670 if (restrictp > 1)
3671 pedwarn ("duplicate `restrict'");
3672 if (volatilep > 1)
3673 pedwarn ("duplicate `volatile'");
3674 }
3675 if (! flag_gen_aux_info && (TYPE_QUALS (type)))
3676 type = TYPE_MAIN_VARIANT (type);
3677 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
3678 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
3679 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
3680
3681 /* Warn if two storage classes are given. Default to `auto'. */
3682
3683 {
3684 int nclasses = 0;
3685
3686 if (specbits & 1 << (int) RID_AUTO) nclasses++;
3687 if (specbits & 1 << (int) RID_STATIC) nclasses++;
3688 if (specbits & 1 << (int) RID_EXTERN) nclasses++;
3689 if (specbits & 1 << (int) RID_REGISTER) nclasses++;
3690 if (specbits & 1 << (int) RID_TYPEDEF) nclasses++;
3691
3692 /* "static __thread" and "extern __thread" are allowed. */
3693 if ((specbits & (1 << (int) RID_THREAD
3694 | 1 << (int) RID_STATIC
3695 | 1 << (int) RID_EXTERN)) == (1 << (int) RID_THREAD))
3696 nclasses++;
3697
3698 /* Warn about storage classes that are invalid for certain
3699 kinds of declarations (parameters, typenames, etc.). */
3700
3701 if (nclasses > 1)
3702 error ("multiple storage classes in declaration of `%s'", name);
3703 else if (funcdef_flag
3704 && (specbits
3705 & ((1 << (int) RID_REGISTER)
3706 | (1 << (int) RID_AUTO)
3707 | (1 << (int) RID_TYPEDEF)
3708 | (1 << (int) RID_THREAD))))
3709 {
3710 if (specbits & 1 << (int) RID_AUTO
3711 && (pedantic || current_scope == global_scope))
3712 pedwarn ("function definition declared `auto'");
3713 if (specbits & 1 << (int) RID_REGISTER)
3714 error ("function definition declared `register'");
3715 if (specbits & 1 << (int) RID_TYPEDEF)
3716 error ("function definition declared `typedef'");
3717 if (specbits & 1 << (int) RID_THREAD)
3718 error ("function definition declared `__thread'");
3719 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3720 | (1 << (int) RID_AUTO) | (1 << (int) RID_THREAD));
3721 }
3722 else if (decl_context != NORMAL && nclasses > 0)
3723 {
3724 if (decl_context == PARM && specbits & 1 << (int) RID_REGISTER)
3725 ;
3726 else
3727 {
3728 switch (decl_context)
3729 {
3730 case FIELD:
3731 error ("storage class specified for structure field `%s'",
3732 name);
3733 break;
3734 case PARM:
3735 error ("storage class specified for parameter `%s'", name);
3736 break;
3737 default:
3738 error ("storage class specified for typename");
3739 break;
3740 }
3741 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3742 | (1 << (int) RID_AUTO) | (1 << (int) RID_STATIC)
3743 | (1 << (int) RID_EXTERN) | (1 << (int) RID_THREAD));
3744 }
3745 }
3746 else if (specbits & 1 << (int) RID_EXTERN && initialized && ! funcdef_flag)
3747 {
3748 /* `extern' with initialization is invalid if not at file scope. */
3749 if (current_scope == global_scope)
3750 warning ("`%s' initialized and declared `extern'", name);
3751 else
3752 error ("`%s' has both `extern' and initializer", name);
3753 }
3754 else if (current_scope == global_scope)
3755 {
3756 if (specbits & 1 << (int) RID_AUTO)
3757 error ("file-scope declaration of `%s' specifies `auto'", name);
3758 }
3759 else
3760 {
3761 if (specbits & 1 << (int) RID_EXTERN && funcdef_flag)
3762 error ("nested function `%s' declared `extern'", name);
3763 else if ((specbits & (1 << (int) RID_THREAD
3764 | 1 << (int) RID_EXTERN
3765 | 1 << (int) RID_STATIC))
3766 == (1 << (int) RID_THREAD))
3767 {
3768 error ("function-scope `%s' implicitly auto and declared `__thread'",
3769 name);
3770 specbits &= ~(1 << (int) RID_THREAD);
3771 }
3772 }
3773 }
3774
3775 /* Now figure out the structure of the declarator proper.
3776 Descend through it, creating more complex types, until we reach
3777 the declared identifier (or NULL_TREE, in an absolute declarator). */
3778
3779 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE)
3780 {
3781 if (type == error_mark_node)
3782 {
3783 declarator = TREE_OPERAND (declarator, 0);
3784 continue;
3785 }
3786
3787 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
3788 an INDIRECT_REF (for *...),
3789 a CALL_EXPR (for ...(...)),
3790 a TREE_LIST (for nested attributes),
3791 an identifier (for the name being declared)
3792 or a null pointer (for the place in an absolute declarator
3793 where the name was omitted).
3794 For the last two cases, we have just exited the loop.
3795
3796 At this point, TYPE is the type of elements of an array,
3797 or for a function to return, or for a pointer to point to.
3798 After this sequence of ifs, TYPE is the type of the
3799 array or function or pointer, and DECLARATOR has had its
3800 outermost layer removed. */
3801
3802 if (array_ptr_quals != NULL_TREE || array_parm_static)
3803 {
3804 /* Only the innermost declarator (making a parameter be of
3805 array type which is converted to pointer type)
3806 may have static or type qualifiers. */
3807 error ("static or type qualifiers in non-parameter array declarator");
3808 array_ptr_quals = NULL_TREE;
3809 array_parm_static = 0;
3810 }
3811
3812 if (TREE_CODE (declarator) == TREE_LIST)
3813 {
3814 /* We encode a declarator with embedded attributes using
3815 a TREE_LIST. */
3816 tree attrs = TREE_PURPOSE (declarator);
3817 tree inner_decl;
3818 int attr_flags = 0;
3819 declarator = TREE_VALUE (declarator);
3820 inner_decl = declarator;
3821 while (inner_decl != NULL_TREE
3822 && TREE_CODE (inner_decl) == TREE_LIST)
3823 inner_decl = TREE_VALUE (inner_decl);
3824 if (inner_decl == NULL_TREE
3825 || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
3826 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
3827 else if (TREE_CODE (inner_decl) == CALL_EXPR)
3828 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
3829 else if (TREE_CODE (inner_decl) == ARRAY_REF)
3830 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
3831 returned_attrs = decl_attributes (&type,
3832 chainon (returned_attrs, attrs),
3833 attr_flags);
3834 }
3835 else if (TREE_CODE (declarator) == ARRAY_REF)
3836 {
3837 tree itype = NULL_TREE;
3838 tree size = TREE_OPERAND (declarator, 1);
3839 /* The index is a signed object `sizetype' bits wide. */
3840 tree index_type = c_common_signed_type (sizetype);
3841
3842 array_ptr_quals = TREE_TYPE (declarator);
3843 array_parm_static = TREE_STATIC (declarator);
3844
3845 declarator = TREE_OPERAND (declarator, 0);
3846
3847 /* Check for some types that there cannot be arrays of. */
3848
3849 if (VOID_TYPE_P (type))
3850 {
3851 error ("declaration of `%s' as array of voids", name);
3852 type = error_mark_node;
3853 }
3854
3855 if (TREE_CODE (type) == FUNCTION_TYPE)
3856 {
3857 error ("declaration of `%s' as array of functions", name);
3858 type = error_mark_node;
3859 }
3860
3861 if (pedantic && !in_system_header && flexible_array_type_p (type))
3862 pedwarn ("invalid use of structure with flexible array member");
3863
3864 if (size == error_mark_node)
3865 type = error_mark_node;
3866
3867 if (type == error_mark_node)
3868 continue;
3869
3870 /* If size was specified, set ITYPE to a range-type for that size.
3871 Otherwise, ITYPE remains null. finish_decl may figure it out
3872 from an initial value. */
3873
3874 if (size)
3875 {
3876 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3877 STRIP_TYPE_NOPS (size);
3878
3879 if (! INTEGRAL_TYPE_P (TREE_TYPE (size)))
3880 {
3881 error ("size of array `%s' has non-integer type", name);
3882 size = integer_one_node;
3883 }
3884
3885 if (pedantic && integer_zerop (size))
3886 pedwarn ("ISO C forbids zero-size array `%s'", name);
3887
3888 if (TREE_CODE (size) == INTEGER_CST)
3889 {
3890 constant_expression_warning (size);
3891 if (tree_int_cst_sgn (size) < 0)
3892 {
3893 error ("size of array `%s' is negative", name);
3894 size = integer_one_node;
3895 }
3896 }
3897 else
3898 {
3899 /* Make sure the array size remains visibly nonconstant
3900 even if it is (eg) a const variable with known value. */
3901 size_varies = 1;
3902
3903 if (!flag_isoc99 && pedantic)
3904 {
3905 if (TREE_CONSTANT (size))
3906 pedwarn ("ISO C90 forbids array `%s' whose size can't be evaluated",
3907 name);
3908 else
3909 pedwarn ("ISO C90 forbids variable-size array `%s'",
3910 name);
3911 }
3912 }
3913
3914 if (integer_zerop (size))
3915 {
3916 /* A zero-length array cannot be represented with an
3917 unsigned index type, which is what we'll get with
3918 build_index_type. Create an open-ended range instead. */
3919 itype = build_range_type (sizetype, size, NULL_TREE);
3920 }
3921 else
3922 {
3923 /* Compute the maximum valid index, that is, size - 1.
3924 Do the calculation in index_type, so that if it is
3925 a variable the computations will be done in the
3926 proper mode. */
3927 itype = fold (build (MINUS_EXPR, index_type,
3928 convert (index_type, size),
3929 convert (index_type, size_one_node)));
3930
3931 /* If that overflowed, the array is too big.
3932 ??? While a size of INT_MAX+1 technically shouldn't
3933 cause an overflow (because we subtract 1), the overflow
3934 is recorded during the conversion to index_type, before
3935 the subtraction. Handling this case seems like an
3936 unnecessary complication. */
3937 if (TREE_OVERFLOW (itype))
3938 {
3939 error ("size of array `%s' is too large", name);
3940 type = error_mark_node;
3941 continue;
3942 }
3943
3944 if (size_varies)
3945 {
3946 /* We must be able to distinguish the
3947 SAVE_EXPR_CONTEXT for the variably-sized type
3948 so that we can set it correctly in
3949 set_save_expr_context. The convention is
3950 that all SAVE_EXPRs that need to be reset
3951 have NULL_TREE for their SAVE_EXPR_CONTEXT. */
3952 tree cfd = current_function_decl;
3953 if (decl_context == PARM)
3954 current_function_decl = NULL_TREE;
3955 itype = variable_size (itype);
3956 if (decl_context == PARM)
3957 current_function_decl = cfd;
3958 }
3959 itype = build_index_type (itype);
3960 }
3961 }
3962 else if (decl_context == FIELD)
3963 {
3964 if (pedantic && !flag_isoc99 && !in_system_header)
3965 pedwarn ("ISO C90 does not support flexible array members");
3966
3967 /* ISO C99 Flexible array members are effectively identical
3968 to GCC's zero-length array extension. */
3969 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
3970 }
3971
3972 /* If pedantic, complain about arrays of incomplete types. */
3973
3974 if (pedantic && !COMPLETE_TYPE_P (type))
3975 pedwarn ("array type has incomplete element type");
3976
3977 /* Build the array type itself, then merge any constancy or
3978 volatility into the target type. We must do it in this order
3979 to ensure that the TYPE_MAIN_VARIANT field of the array type
3980 is set correctly. */
3981
3982 type = build_array_type (type, itype);
3983 if (type_quals)
3984 type = c_build_qualified_type (type, type_quals);
3985
3986 if (size_varies)
3987 C_TYPE_VARIABLE_SIZE (type) = 1;
3988
3989 /* The GCC extension for zero-length arrays differs from
3990 ISO flexible array members in that sizeof yields zero. */
3991 if (size && integer_zerop (size))
3992 {
3993 layout_type (type);
3994 TYPE_SIZE (type) = bitsize_zero_node;
3995 TYPE_SIZE_UNIT (type) = size_zero_node;
3996 }
3997 if (decl_context != PARM
3998 && (array_ptr_quals != NULL_TREE || array_parm_static))
3999 {
4000 error ("static or type qualifiers in non-parameter array declarator");
4001 array_ptr_quals = NULL_TREE;
4002 array_parm_static = 0;
4003 }
4004 }
4005 else if (TREE_CODE (declarator) == CALL_EXPR)
4006 {
4007 /* Say it's a definition only for the declarator closest to
4008 the identifier, apart possibly from some attributes. */
4009 bool really_funcdef = false;
4010 tree arg_types;
4011 if (funcdef_flag)
4012 {
4013 tree t = TREE_OPERAND (declarator, 0);
4014 while (TREE_CODE (t) == TREE_LIST)
4015 t = TREE_VALUE (t);
4016 really_funcdef = (TREE_CODE (t) == IDENTIFIER_NODE);
4017 }
4018
4019 /* Declaring a function type.
4020 Make sure we have a valid type for the function to return. */
4021 if (type == error_mark_node)
4022 continue;
4023
4024 size_varies = 0;
4025
4026 /* Warn about some types functions can't return. */
4027
4028 if (TREE_CODE (type) == FUNCTION_TYPE)
4029 {
4030 error ("`%s' declared as function returning a function", name);
4031 type = integer_type_node;
4032 }
4033 if (TREE_CODE (type) == ARRAY_TYPE)
4034 {
4035 error ("`%s' declared as function returning an array", name);
4036 type = integer_type_node;
4037 }
4038
4039 /* Construct the function type and go to the next
4040 inner layer of declarator. */
4041
4042 arg_types = grokparms (TREE_OPERAND (declarator, 1),
4043 really_funcdef);
4044 /* Type qualifiers before the return type of the function
4045 qualify the return type, not the function type. */
4046 if (type_quals)
4047 {
4048 /* Type qualifiers on a function return type are normally
4049 permitted by the standard but have no effect, so give a
4050 warning at -Wextra. Qualifiers on a void return type have
4051 meaning as a GNU extension, and are banned on function
4052 definitions in ISO C. FIXME: strictly we shouldn't
4053 pedwarn for qualified void return types except on function
4054 definitions, but not doing so could lead to the undesirable
4055 state of a "volatile void" function return type not being
4056 warned about, and a use of the function being compiled
4057 with GNU semantics, with no diagnostics under -pedantic. */
4058 if (VOID_TYPE_P (type) && pedantic && !in_system_header)
4059 pedwarn ("ISO C forbids qualified void function return type");
4060 else if (extra_warnings
4061 && !(VOID_TYPE_P (type)
4062 && type_quals == TYPE_QUAL_VOLATILE))
4063 warning ("type qualifiers ignored on function return type");
4064
4065 type = c_build_qualified_type (type, type_quals);
4066 }
4067 type_quals = TYPE_UNQUALIFIED;
4068
4069 type = build_function_type (type, arg_types);
4070 declarator = TREE_OPERAND (declarator, 0);
4071
4072 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4073 the formal parameter list of this FUNCTION_TYPE to point to
4074 the FUNCTION_TYPE node itself. */
4075
4076 {
4077 tree link;
4078
4079 for (link = last_function_parm_tags;
4080 link;
4081 link = TREE_CHAIN (link))
4082 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4083 }
4084 }
4085 else if (TREE_CODE (declarator) == INDIRECT_REF)
4086 {
4087 /* Merge any constancy or volatility into the target type
4088 for the pointer. */
4089
4090 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4091 && type_quals)
4092 pedwarn ("ISO C forbids qualified function types");
4093 if (type_quals)
4094 type = c_build_qualified_type (type, type_quals);
4095 type_quals = TYPE_UNQUALIFIED;
4096 size_varies = 0;
4097
4098 type = build_pointer_type (type);
4099
4100 /* Process a list of type modifier keywords
4101 (such as const or volatile) that were given inside the `*'. */
4102
4103 if (TREE_TYPE (declarator))
4104 {
4105 tree typemodlist;
4106 int erred = 0;
4107
4108 constp = 0;
4109 volatilep = 0;
4110 restrictp = 0;
4111 for (typemodlist = TREE_TYPE (declarator); typemodlist;
4112 typemodlist = TREE_CHAIN (typemodlist))
4113 {
4114 tree qualifier = TREE_VALUE (typemodlist);
4115
4116 if (C_IS_RESERVED_WORD (qualifier))
4117 {
4118 if (C_RID_CODE (qualifier) == RID_CONST)
4119 constp++;
4120 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4121 volatilep++;
4122 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4123 restrictp++;
4124 else
4125 erred++;
4126 }
4127 else
4128 erred++;
4129 }
4130
4131 if (erred)
4132 error ("invalid type modifier within pointer declarator");
4133 if (pedantic && !flag_isoc99)
4134 {
4135 if (constp > 1)
4136 pedwarn ("duplicate `const'");
4137 if (volatilep > 1)
4138 pedwarn ("duplicate `volatile'");
4139 if (restrictp > 1)
4140 pedwarn ("duplicate `restrict'");
4141 }
4142
4143 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4144 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4145 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4146 }
4147
4148 declarator = TREE_OPERAND (declarator, 0);
4149 }
4150 else
4151 abort ();
4152
4153 }
4154
4155 /* Now TYPE has the actual type. */
4156
4157 /* Check the type and width of a bit-field. */
4158 if (bitfield)
4159 check_bitfield_type_and_width (&type, width, orig_name);
4160
4161 /* Did array size calculations overflow? */
4162
4163 if (TREE_CODE (type) == ARRAY_TYPE
4164 && COMPLETE_TYPE_P (type)
4165 && TREE_OVERFLOW (TYPE_SIZE (type)))
4166 {
4167 error ("size of array `%s' is too large", name);
4168 /* If we proceed with the array type as it is, we'll eventually
4169 crash in tree_low_cst(). */
4170 type = error_mark_node;
4171 }
4172
4173 /* If this is declaring a typedef name, return a TYPE_DECL. */
4174
4175 if (specbits & (1 << (int) RID_TYPEDEF))
4176 {
4177 tree decl;
4178 /* Note that the grammar rejects storage classes
4179 in typenames, fields or parameters */
4180 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4181 && type_quals)
4182 pedwarn ("ISO C forbids qualified function types");
4183 if (type_quals)
4184 type = c_build_qualified_type (type, type_quals);
4185 decl = build_decl (TYPE_DECL, declarator, type);
4186 if ((specbits & (1 << (int) RID_SIGNED))
4187 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
4188 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4189 decl_attributes (&decl, returned_attrs, 0);
4190 return decl;
4191 }
4192
4193 /* Detect the case of an array type of unspecified size
4194 which came, as such, direct from a typedef name.
4195 We must copy the type, so that each identifier gets
4196 a distinct type, so that each identifier's size can be
4197 controlled separately by its own initializer. */
4198
4199 if (type != 0 && typedef_type != 0
4200 && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0
4201 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
4202 {
4203 type = build_array_type (TREE_TYPE (type), 0);
4204 if (size_varies)
4205 C_TYPE_VARIABLE_SIZE (type) = 1;
4206 }
4207
4208 /* If this is a type name (such as, in a cast or sizeof),
4209 compute the type and return it now. */
4210
4211 if (decl_context == TYPENAME)
4212 {
4213 /* Note that the grammar rejects storage classes
4214 in typenames, fields or parameters */
4215 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4216 && type_quals)
4217 pedwarn ("ISO C forbids const or volatile function types");
4218 if (type_quals)
4219 type = c_build_qualified_type (type, type_quals);
4220 decl_attributes (&type, returned_attrs, 0);
4221 return type;
4222 }
4223
4224 /* Aside from typedefs and type names (handle above),
4225 `void' at top level (not within pointer)
4226 is allowed only in public variables.
4227 We don't complain about parms either, but that is because
4228 a better error message can be made later. */
4229
4230 if (VOID_TYPE_P (type) && decl_context != PARM
4231 && ! ((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4232 && ((specbits & (1 << (int) RID_EXTERN))
4233 || (current_scope == global_scope
4234 && !(specbits
4235 & ((1 << (int) RID_STATIC) | (1 << (int) RID_REGISTER)))))))
4236 {
4237 error ("variable or field `%s' declared void", name);
4238 type = integer_type_node;
4239 }
4240
4241 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4242 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4243
4244 {
4245 tree decl;
4246
4247 if (decl_context == PARM)
4248 {
4249 tree type_as_written;
4250 tree promoted_type;
4251
4252 /* A parameter declared as an array of T is really a pointer to T.
4253 One declared as a function is really a pointer to a function. */
4254
4255 if (TREE_CODE (type) == ARRAY_TYPE)
4256 {
4257 /* Transfer const-ness of array into that of type pointed to. */
4258 type = TREE_TYPE (type);
4259 if (type_quals)
4260 type = c_build_qualified_type (type, type_quals);
4261 type = build_pointer_type (type);
4262 type_quals = TYPE_UNQUALIFIED;
4263 if (array_ptr_quals)
4264 {
4265 tree new_ptr_quals, new_ptr_attrs;
4266 int erred = 0;
4267 split_specs_attrs (array_ptr_quals, &new_ptr_quals, &new_ptr_attrs);
4268 /* We don't yet implement attributes in this context. */
4269 if (new_ptr_attrs != NULL_TREE)
4270 warning ("attributes in parameter array declarator ignored");
4271
4272 constp = 0;
4273 volatilep = 0;
4274 restrictp = 0;
4275 for (; new_ptr_quals; new_ptr_quals = TREE_CHAIN (new_ptr_quals))
4276 {
4277 tree qualifier = TREE_VALUE (new_ptr_quals);
4278
4279 if (C_IS_RESERVED_WORD (qualifier))
4280 {
4281 if (C_RID_CODE (qualifier) == RID_CONST)
4282 constp++;
4283 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4284 volatilep++;
4285 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4286 restrictp++;
4287 else
4288 erred++;
4289 }
4290 else
4291 erred++;
4292 }
4293
4294 if (erred)
4295 error ("invalid type modifier within array declarator");
4296
4297 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4298 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4299 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4300 }
4301 size_varies = 0;
4302 }
4303 else if (TREE_CODE (type) == FUNCTION_TYPE)
4304 {
4305 if (pedantic && type_quals)
4306 pedwarn ("ISO C forbids qualified function types");
4307 if (type_quals)
4308 type = c_build_qualified_type (type, type_quals);
4309 type = build_pointer_type (type);
4310 type_quals = TYPE_UNQUALIFIED;
4311 }
4312 else if (type_quals)
4313 type = c_build_qualified_type (type, type_quals);
4314
4315 type_as_written = type;
4316
4317 decl = build_decl (PARM_DECL, declarator, type);
4318 if (size_varies)
4319 C_DECL_VARIABLE_SIZE (decl) = 1;
4320
4321 /* Compute the type actually passed in the parmlist,
4322 for the case where there is no prototype.
4323 (For example, shorts and chars are passed as ints.)
4324 When there is a prototype, this is overridden later. */
4325
4326 if (type == error_mark_node)
4327 promoted_type = type;
4328 else
4329 promoted_type = c_type_promotes_to (type);
4330
4331 DECL_ARG_TYPE (decl) = promoted_type;
4332 DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
4333 }
4334 else if (decl_context == FIELD)
4335 {
4336 /* Structure field. It may not be a function. */
4337
4338 if (TREE_CODE (type) == FUNCTION_TYPE)
4339 {
4340 error ("field `%s' declared as a function", name);
4341 type = build_pointer_type (type);
4342 }
4343 else if (TREE_CODE (type) != ERROR_MARK
4344 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4345 {
4346 error ("field `%s' has incomplete type", name);
4347 type = error_mark_node;
4348 }
4349 /* Move type qualifiers down to element of an array. */
4350 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4351 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4352 type_quals),
4353 TYPE_DOMAIN (type));
4354 decl = build_decl (FIELD_DECL, declarator, type);
4355 DECL_NONADDRESSABLE_P (decl) = bitfield;
4356
4357 if (size_varies)
4358 C_DECL_VARIABLE_SIZE (decl) = 1;
4359 }
4360 else if (TREE_CODE (type) == FUNCTION_TYPE)
4361 {
4362 /* Every function declaration is "external"
4363 except for those which are inside a function body
4364 in which `auto' is used.
4365 That is a case not specified by ANSI C,
4366 and we use it for forward declarations for nested functions. */
4367 int extern_ref = (!(specbits & (1 << (int) RID_AUTO))
4368 || current_scope == global_scope);
4369
4370 if (specbits & (1 << (int) RID_AUTO)
4371 && (pedantic || current_scope == global_scope))
4372 pedwarn ("invalid storage class for function `%s'", name);
4373 if (specbits & (1 << (int) RID_REGISTER))
4374 error ("invalid storage class for function `%s'", name);
4375 if (specbits & (1 << (int) RID_THREAD))
4376 error ("invalid storage class for function `%s'", name);
4377 /* Function declaration not at file scope.
4378 Storage classes other than `extern' are not allowed
4379 and `extern' makes no difference. */
4380 if (current_scope != global_scope
4381 && (specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_INLINE)))
4382 && pedantic)
4383 pedwarn ("invalid storage class for function `%s'", name);
4384
4385 decl = build_decl (FUNCTION_DECL, declarator, type);
4386 decl = build_decl_attribute_variant (decl, decl_attr);
4387
4388 DECL_LANG_SPECIFIC (decl)
4389 = ggc_alloc_cleared (sizeof (struct lang_decl));
4390
4391 if (pedantic && type_quals && ! DECL_IN_SYSTEM_HEADER (decl))
4392 pedwarn ("ISO C forbids qualified function types");
4393
4394 /* GNU C interprets a `volatile void' return type to indicate
4395 that the function does not return. */
4396 if ((type_quals & TYPE_QUAL_VOLATILE)
4397 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4398 warning ("`noreturn' function returns non-void value");
4399
4400 if (extern_ref)
4401 DECL_EXTERNAL (decl) = 1;
4402 /* Record absence of global scope for `static' or `auto'. */
4403 TREE_PUBLIC (decl)
4404 = !(specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_AUTO)));
4405
4406 if (defaulted_int)
4407 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4408
4409 /* Record presence of `inline', if it is reasonable. */
4410 if (MAIN_NAME_P (declarator))
4411 {
4412 if (inlinep)
4413 warning ("cannot inline function `main'");
4414 }
4415 else if (inlinep)
4416 {
4417 /* Record that the function is declared `inline'. */
4418 DECL_DECLARED_INLINE_P (decl) = 1;
4419
4420 /* Do not mark bare declarations as DECL_INLINE. Doing so
4421 in the presence of multiple declarations can result in
4422 the abstract origin pointing between the declarations,
4423 which will confuse dwarf2out. */
4424 if (initialized)
4425 {
4426 DECL_INLINE (decl) = 1;
4427 if (specbits & (1 << (int) RID_EXTERN))
4428 current_extern_inline = 1;
4429 }
4430 }
4431 /* If -finline-functions, assume it can be inlined. This does
4432 two things: let the function be deferred until it is actually
4433 needed, and let dwarf2 know that the function is inlinable. */
4434 else if (flag_inline_trees == 2 && initialized)
4435 DECL_INLINE (decl) = 1;
4436 }
4437 else
4438 {
4439 /* It's a variable. */
4440 /* An uninitialized decl with `extern' is a reference. */
4441 int extern_ref = !initialized && (specbits & (1 << (int) RID_EXTERN));
4442
4443 /* Move type qualifiers down to element of an array. */
4444 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4445 {
4446 int saved_align = TYPE_ALIGN(type);
4447 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4448 type_quals),
4449 TYPE_DOMAIN (type));
4450 TYPE_ALIGN (type) = saved_align;
4451 }
4452 else if (type_quals)
4453 type = c_build_qualified_type (type, type_quals);
4454
4455 /* It is invalid to create an `extern' declaration for a
4456 variable if there is a global declaration that is
4457 `static' and the global declaration is not visible. */
4458 if (extern_ref && current_scope != global_scope)
4459 {
4460 tree global_decl;
4461
4462 global_decl = identifier_global_value (declarator);
4463 if (global_decl
4464 && TREE_CODE (global_decl) == VAR_DECL
4465 && lookup_name (declarator) != global_decl
4466 && !TREE_PUBLIC (global_decl))
4467 error ("variable previously declared `static' redeclared "
4468 "`extern'");
4469 }
4470
4471 decl = build_decl (VAR_DECL, declarator, type);
4472 if (size_varies)
4473 C_DECL_VARIABLE_SIZE (decl) = 1;
4474
4475 if (inlinep)
4476 pedwarn ("%Jvariable '%D' declared `inline'", decl, decl);
4477
4478 DECL_EXTERNAL (decl) = extern_ref;
4479
4480 /* At file scope, the presence of a `static' or `register' storage
4481 class specifier, or the absence of all storage class specifiers
4482 makes this declaration a definition (perhaps tentative). Also,
4483 the absence of both `static' and `register' makes it public. */
4484 if (current_scope == global_scope)
4485 {
4486 TREE_PUBLIC (decl) = !(specbits & ((1 << (int) RID_STATIC)
4487 | (1 << (int) RID_REGISTER)));
4488 TREE_STATIC (decl) = !extern_ref;
4489 }
4490 /* Not at file scope, only `static' makes a static definition. */
4491 else
4492 {
4493 TREE_STATIC (decl) = (specbits & (1 << (int) RID_STATIC)) != 0;
4494 TREE_PUBLIC (decl) = extern_ref;
4495 }
4496
4497 if (specbits & 1 << (int) RID_THREAD)
4498 {
4499 if (targetm.have_tls)
4500 DECL_THREAD_LOCAL (decl) = 1;
4501 else
4502 /* A mere warning is sure to result in improper semantics
4503 at runtime. Don't bother to allow this to compile. */
4504 error ("thread-local storage not supported for this target");
4505 }
4506 }
4507
4508 /* Record `register' declaration for warnings on &
4509 and in case doing stupid register allocation. */
4510
4511 if (specbits & (1 << (int) RID_REGISTER))
4512 DECL_REGISTER (decl) = 1;
4513
4514 /* Record constancy and volatility. */
4515 c_apply_type_quals_to_decl (type_quals, decl);
4516
4517 /* If a type has volatile components, it should be stored in memory.
4518 Otherwise, the fact that those components are volatile
4519 will be ignored, and would even crash the compiler. */
4520 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
4521 c_mark_addressable (decl);
4522
4523#ifdef ENABLE_CHECKING
4524 /* This is the earliest point at which we might know the assembler
4525 name of a variable. Thus, if it's known before this, die horribly. */
4526 if (DECL_ASSEMBLER_NAME_SET_P (decl))
4527 abort ();
4528#endif
4529
4530 decl_attributes (&decl, returned_attrs, 0);
4531
4532 return decl;
4533 }
4534}
4535
4536/* Decode the parameter-list info for a function type or function definition.
4537 The argument is the value returned by `get_parm_info' (or made in parse.y
4538 if there is an identifier list instead of a parameter decl list).
4539 These two functions are separate because when a function returns
4540 or receives functions then each is called multiple times but the order
4541 of calls is different. The last call to `grokparms' is always the one
4542 that contains the formal parameter names of a function definition.
4543
4544 Store in `last_function_parms' a chain of the decls of parms.
4545 Also store in `last_function_parm_tags' a chain of the struct, union,
4546 and enum tags declared among the parms.
4547
4548 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4549
4550 FUNCDEF_FLAG is nonzero for a function definition, 0 for
4551 a mere declaration. A nonempty identifier-list gets an error message
4552 when FUNCDEF_FLAG is zero. */
4553
4554static tree
4555grokparms (tree parms_info, int funcdef_flag)
4556{
4557 tree first_parm = TREE_CHAIN (parms_info);
4558
4559 last_function_parms = TREE_PURPOSE (parms_info);
4560 last_function_parm_tags = TREE_VALUE (parms_info);
4561 last_function_parm_others = TREE_TYPE (parms_info);
4562
4563 if (warn_strict_prototypes && first_parm == 0 && !funcdef_flag
4564 && !in_system_header)
4565 warning ("function declaration isn't a prototype");
4566
4567 if (first_parm != 0
4568 && TREE_CODE (TREE_VALUE (first_parm)) == IDENTIFIER_NODE)
4569 {
4570 if (! funcdef_flag)
4571 pedwarn ("parameter names (without types) in function declaration");
4572
4573 last_function_parms = first_parm;
4574 return 0;
4575 }
4576 else
4577 {
4578 tree parm;
4579 tree typelt;
4580 /* If the arg types are incomplete in a declaration,
4581 they must include undefined tags.
4582 These tags can never be defined in the scope of the declaration,
4583 so the types can never be completed,
4584 and no call can be compiled successfully. */
4585
4586 for (parm = last_function_parms, typelt = first_parm;
4587 parm;
4588 parm = TREE_CHAIN (parm))
4589 /* Skip over any enumeration constants declared here. */
4590 if (TREE_CODE (parm) == PARM_DECL)
4591 {
4592 /* Barf if the parameter itself has an incomplete type. */
4593 tree type = TREE_VALUE (typelt);
4594 if (type == error_mark_node)
4595 continue;
4596 if (!COMPLETE_TYPE_P (type))
4597 {
4598 if (funcdef_flag && DECL_NAME (parm) != 0)
4599 error ("parameter `%s' has incomplete type",
4600 IDENTIFIER_POINTER (DECL_NAME (parm)));
4601 else
4602 warning ("parameter has incomplete type");
4603 if (funcdef_flag)
4604 {
4605 TREE_VALUE (typelt) = error_mark_node;
4606 TREE_TYPE (parm) = error_mark_node;
4607 }
4608 }
4609 typelt = TREE_CHAIN (typelt);
4610 }
4611
4612 return first_parm;
4613 }
4614}
4615
4616/* Return a tree_list node with info on a parameter list just parsed.
4617 The TREE_PURPOSE is a list of decls of those parms.
4618 The TREE_VALUE is a list of structure, union and enum tags defined.
4619 The TREE_CHAIN is a list of argument types to go in the FUNCTION_TYPE.
4620 The TREE_TYPE is a list of non-parameter decls which appeared with the
4621 parameters.
4622 This tree_list node is later fed to `grokparms'.
4623
4624 VOID_AT_END nonzero means append `void' to the end of the type-list.
4625 Zero means the parmlist ended with an ellipsis so don't append `void'. */
4626
4627tree
4628get_parm_info (int void_at_end)
4629{
4630 tree decl, type, list;
4631 tree types = 0;
4632 tree *last_type = &types;
4633 tree tags = current_scope->tags;
4634 tree parms = current_scope->parms;
4635 tree others = current_scope->names;
4636 static bool explained_incomplete_types = false;
4637 bool gave_void_only_once_err = false;
4638
4639 /* Just "void" (and no ellipsis) is special. There are really no parms.
4640 But if the "void" is qualified (by "const" or "volatile"), or has a
4641 storage class specifier ("register"), then the behavior is undefined;
4642 issue an error. Typedefs for "void" are OK (see DR#157). */
4643 if (void_at_end && parms != 0
4644 && TREE_CHAIN (parms) == 0
4645 && VOID_TYPE_P (TREE_TYPE (parms))
4646 && !DECL_NAME (parms))
4647 {
4648 if (TREE_THIS_VOLATILE (parms)
4649 || TREE_READONLY (parms)
4650 || DECL_REGISTER (parms))
4651 error ("\"void\" as only parameter may not be qualified");
4652
4653 return tree_cons (0, 0, tree_cons (0, void_type_node, 0));
4654 }
4655
4656 /* Sanity check all of the parameter declarations. */
4657 for (decl = parms; decl; decl = TREE_CHAIN (decl))
4658 {
4659 if (TREE_CODE (decl) != PARM_DECL)
4660 abort ();
4661 if (TREE_ASM_WRITTEN (decl))
4662 abort ();
4663
4664 /* Since there is a prototype, args are passed in their
4665 declared types. The back end may override this. */
4666 type = TREE_TYPE (decl);
4667 DECL_ARG_TYPE (decl) = type;
4668
4669 /* Check for (..., void, ...) and issue an error. */
4670 if (VOID_TYPE_P (type) && !DECL_NAME (decl) && !gave_void_only_once_err)
4671 {
4672 error ("\"void\" must be the only parameter");
4673 gave_void_only_once_err = true;
4674 }
4675
4676 type = build_tree_list (0, type);
4677 *last_type = type;
4678 last_type = &TREE_CHAIN (type);
4679 }
4680
4681 /* Check the list of non-parameter decls for any forward parm decls
4682 that never got real decls. */
4683 for (decl = others; decl; decl = TREE_CHAIN (decl))
4684 if (TREE_CODE (decl) == PARM_DECL)
4685 {
4686 if (!TREE_ASM_WRITTEN (decl))
4687 abort ();
4688
4689 error ("%Jparameter \"%D\" has just a forward declaration",
4690 decl, decl);
4691 }
4692
4693 /* Warn about any struct, union or enum tags defined within this
4694 list. The scope of such types is limited to this declaration,
4695 which is rarely if ever desirable (it's impossible to call such
4696 a function with type-correct arguments). */
4697 for (decl = tags; decl; decl = TREE_CHAIN (decl))
4698 {
4699 enum tree_code code = TREE_CODE (TREE_VALUE (decl));
4700 const char *keyword;
4701 /* An anonymous union parm type is meaningful as a GNU extension.
4702 So don't warn for that. */
4703 if (code == UNION_TYPE && TREE_PURPOSE (decl) == 0 && !pedantic)
4704 continue;
4705
4706 /* The keyword should not be translated. */
4707 switch (code)
4708 {
4709 case RECORD_TYPE: keyword = "struct"; break;
4710 case UNION_TYPE: keyword = "union"; break;
4711 case ENUMERAL_TYPE: keyword = "enum"; break;
4712 default: abort ();
4713 }
4714
4715 if (TREE_PURPOSE (decl))
4716 /* The first %s will be one of 'struct', 'union', or 'enum'. */
4717 warning ("\"%s %s\" declared inside parameter list",
4718 keyword, IDENTIFIER_POINTER (TREE_PURPOSE (decl)));
4719 else
4720 /* The %s will be one of 'struct', 'union', or 'enum'. */
4721 warning ("anonymous %s declared inside parameter list", keyword);
4722
4723 if (! explained_incomplete_types)
4724 {
4725 warning ("its scope is only this definition or declaration,"
4726 " which is probably not what you want");
4727 explained_incomplete_types = true;
4728 }
4729 }
4730
4731
4732 if (void_at_end)
4733 {
4734 type = build_tree_list (0, void_type_node);
4735 *last_type = type;
4736 }
4737
4738 list = tree_cons (parms, tags, types);
4739 TREE_TYPE (list) = others;
4740 return list;
4741}
4742
4743/* Get the struct, enum or union (CODE says which) with tag NAME.
4744 Define the tag as a forward-reference if it is not defined. */
4745
4746tree
4747xref_tag (enum tree_code code, tree name)
4748{
4749 /* If a cross reference is requested, look up the type
4750 already defined for this tag and return it. */
4751
4752 tree ref = lookup_tag (code, name, 0);
4753 /* If this is the right type of tag, return what we found.
4754 (This reference will be shadowed by shadow_tag later if appropriate.)
4755 If this is the wrong type of tag, do not return it. If it was the
4756 wrong type in the same scope, we will have had an error
4757 message already; if in a different scope and declaring
4758 a name, pending_xref_error will give an error message; but if in a
4759 different scope and not declaring a name, this tag should
4760 shadow the previous declaration of a different type of tag, and
4761 this would not work properly if we return the reference found.
4762 (For example, with "struct foo" in an outer scope, "union foo;"
4763 must shadow that tag with a new one of union type.) */
4764 if (ref && TREE_CODE (ref) == code)
4765 return ref;
4766
4767 /* If no such tag is yet defined, create a forward-reference node
4768 and record it as the "definition".
4769 When a real declaration of this type is found,
4770 the forward-reference will be altered into a real type. */
4771
4772 ref = make_node (code);
4773 if (code == ENUMERAL_TYPE)
4774 {
4775 /* Give the type a default layout like unsigned int
4776 to avoid crashing if it does not get defined. */
4777 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
4778 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
4779 TYPE_USER_ALIGN (ref) = 0;
4780 TREE_UNSIGNED (ref) = 1;
4781 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
4782 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
4783 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
4784 }
4785
4786 pushtag (name, ref);
4787
4788 return ref;
4789}
4790
4791/* Make sure that the tag NAME is defined *in the current scope*
4792 at least as a forward reference.
4793 CODE says which kind of tag NAME ought to be. */
4794
4795tree
4796start_struct (enum tree_code code, tree name)
4797{
4798 /* If there is already a tag defined at this scope
4799 (as a forward reference), just return it. */
4800
4801 tree ref = 0;
4802
4803 if (name != 0)
4804 ref = lookup_tag (code, name, 1);
4805 if (ref && TREE_CODE (ref) == code)
4806 {
4787 if (TYPE_FIELDS (ref))
4807 if (TYPE_SIZE (ref))
4788 {
4789 if (code == UNION_TYPE)
4790 error ("redefinition of `union %s'", IDENTIFIER_POINTER (name));
4791 else
4792 error ("redefinition of `struct %s'", IDENTIFIER_POINTER (name));
4793 }
4808 {
4809 if (code == UNION_TYPE)
4810 error ("redefinition of `union %s'", IDENTIFIER_POINTER (name));
4811 else
4812 error ("redefinition of `struct %s'", IDENTIFIER_POINTER (name));
4813 }
4814 else if (C_TYPE_BEING_DEFINED (ref))
4815 {
4816 if (code == UNION_TYPE)
4817 error ("nested redefinition of `union %s'",
4818 IDENTIFIER_POINTER (name));
4819 else
4820 error ("nested redefinition of `struct %s'",
4821 IDENTIFIER_POINTER (name));
4822 }
4794 }
4795 else
4796 {
4797 /* Otherwise create a forward-reference just so the tag is in scope. */
4798
4799 ref = make_node (code);
4800 pushtag (name, ref);
4801 }
4802
4803 C_TYPE_BEING_DEFINED (ref) = 1;
4804 TYPE_PACKED (ref) = flag_pack_struct;
4805 return ref;
4806}
4807
4808/* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
4809 of a structure component, returning a FIELD_DECL node.
4810 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
4811
4812 This is done during the parsing of the struct declaration.
4813 The FIELD_DECL nodes are chained together and the lot of them
4814 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
4815
4816tree
4817grokfield (tree declarator, tree declspecs, tree width)
4818{
4819 tree value;
4820
4821 if (declarator == NULL_TREE && width == NULL_TREE)
4822 {
4823 /* This is an unnamed decl.
4824
4825 If we have something of the form "union { list } ;" then this
4826 is the anonymous union extension. Similarly for struct.
4827
4828 If this is something of the form "struct foo;", then
4829 If MS extensions are enabled, this is handled as an
4830 anonymous struct.
4831 Otherwise this is a forward declaration of a structure tag.
4832
4833 If this is something of the form "foo;" and foo is a TYPE_DECL, then
4834 If MS extensions are enabled and foo names a structure, then
4835 again this is an anonymous struct.
4836 Otherwise this is an error.
4837
4838 Oh what a horrid tangled web we weave. I wonder if MS consciously
4839 took this from Plan 9 or if it was an accident of implementation
4840 that took root before someone noticed the bug... */
4841
4842 tree type = TREE_VALUE (declspecs);
4843
4844 if (flag_ms_extensions && TREE_CODE (type) == TYPE_DECL)
4845 type = TREE_TYPE (type);
4846 if (TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE)
4847 {
4848 if (flag_ms_extensions)
4849 ; /* ok */
4850 else if (flag_iso)
4851 goto warn_unnamed_field;
4852 else if (TYPE_NAME (type) == NULL)
4853 ; /* ok */
4854 else
4855 goto warn_unnamed_field;
4856 }
4857 else
4858 {
4859 warn_unnamed_field:
4860 warning ("declaration does not declare anything");
4861 return NULL_TREE;
4862 }
4863 }
4864
4865 value = grokdeclarator (declarator, declspecs, FIELD, 0,
4866 width ? &width : NULL);
4867
4868 finish_decl (value, NULL_TREE, NULL_TREE);
4869 DECL_INITIAL (value) = width;
4870
4871 return value;
4872}
4873
4874/* Generate an error for any duplicate field names in FIELDLIST. Munge
4875 the list such that this does not present a problem later. */
4876
4877static void
4878detect_field_duplicates (tree fieldlist)
4879{
4880 tree x, y;
4881 int timeout = 10;
4882
4883 /* First, see if there are more than "a few" fields.
4884 This is trivially true if there are zero or one fields. */
4885 if (!fieldlist)
4886 return;
4887 x = TREE_CHAIN (fieldlist);
4888 if (!x)
4889 return;
4890 do {
4891 timeout--;
4892 x = TREE_CHAIN (x);
4893 } while (timeout > 0 && x);
4894
4895 /* If there were "few" fields, avoid the overhead of allocating
4896 a hash table. Instead just do the nested traversal thing. */
4897 if (timeout > 0)
4898 {
4899 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
4900 if (DECL_NAME (x))
4901 {
4902 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
4903 if (DECL_NAME (y) == DECL_NAME (x))
4904 {
4905 error ("%Jduplicate member '%D'", x, x);
4906 DECL_NAME (x) = NULL_TREE;
4907 }
4908 }
4909 }
4910 else
4911 {
4912 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
4913 void **slot;
4914
4915 for (x = fieldlist; x ; x = TREE_CHAIN (x))
4916 if ((y = DECL_NAME (x)) != 0)
4917 {
4918 slot = htab_find_slot (htab, y, INSERT);
4919 if (*slot)
4920 {
4921 error ("%Jduplicate member '%D'", x, x);
4922 DECL_NAME (x) = NULL_TREE;
4923 }
4924 *slot = y;
4925 }
4926
4927 htab_delete (htab);
4928 }
4929}
4930
4931/* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
4932 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
4933 ATTRIBUTES are attributes to be applied to the structure. */
4934
4935tree
4936finish_struct (tree t, tree fieldlist, tree attributes)
4937{
4938 tree x;
4939 int toplevel = global_scope == current_scope;
4940 int saw_named_field;
4941
4942 /* If this type was previously laid out as a forward reference,
4943 make sure we lay it out again. */
4944
4945 TYPE_SIZE (t) = 0;
4946
4947 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
4948
4949 /* Nameless union parm types are useful as GCC extension. */
4950 if (! (TREE_CODE (t) == UNION_TYPE && TYPE_NAME (t) == 0) && !pedantic)
4951 /* Otherwise, warn about any struct or union def. in parmlist. */
4952 if (in_parm_level_p ())
4953 {
4954 if (pedantic)
4955 pedwarn ("%s defined inside parms",
4956 TREE_CODE (t) == UNION_TYPE ? _("union") : _("structure"));
4957 else
4958 warning ("%s defined inside parms",
4959 TREE_CODE (t) == UNION_TYPE ? _("union") : _("structure"));
4960 }
4961
4962 if (pedantic)
4963 {
4964 for (x = fieldlist; x; x = TREE_CHAIN (x))
4965 if (DECL_NAME (x) != 0)
4966 break;
4967
4968 if (x == 0)
4969 pedwarn ("%s has no %s",
4970 TREE_CODE (t) == UNION_TYPE ? _("union") : _("struct"),
4971 fieldlist ? _("named members") : _("members"));
4972 }
4973
4974 /* Install struct as DECL_CONTEXT of each field decl.
4975 Also process specified field sizes,m which is found in the DECL_INITIAL.
4976 Store 0 there, except for ": 0" fields (so we can find them
4977 and delete them, below). */
4978
4979 saw_named_field = 0;
4980 for (x = fieldlist; x; x = TREE_CHAIN (x))
4981 {
4982 DECL_CONTEXT (x) = t;
4983 DECL_PACKED (x) |= TYPE_PACKED (t);
4984
4985 /* If any field is const, the structure type is pseudo-const. */
4986 if (TREE_READONLY (x))
4987 C_TYPE_FIELDS_READONLY (t) = 1;
4988 else
4989 {
4990 /* A field that is pseudo-const makes the structure likewise. */
4991 tree t1 = TREE_TYPE (x);
4992 while (TREE_CODE (t1) == ARRAY_TYPE)
4993 t1 = TREE_TYPE (t1);
4994 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
4995 && C_TYPE_FIELDS_READONLY (t1))
4996 C_TYPE_FIELDS_READONLY (t) = 1;
4997 }
4998
4999 /* Any field that is volatile means variables of this type must be
5000 treated in some ways as volatile. */
5001 if (TREE_THIS_VOLATILE (x))
5002 C_TYPE_FIELDS_VOLATILE (t) = 1;
5003
5004 /* Any field of nominal variable size implies structure is too. */
5005 if (C_DECL_VARIABLE_SIZE (x))
5006 C_TYPE_VARIABLE_SIZE (t) = 1;
5007
4823 }
4824 else
4825 {
4826 /* Otherwise create a forward-reference just so the tag is in scope. */
4827
4828 ref = make_node (code);
4829 pushtag (name, ref);
4830 }
4831
4832 C_TYPE_BEING_DEFINED (ref) = 1;
4833 TYPE_PACKED (ref) = flag_pack_struct;
4834 return ref;
4835}
4836
4837/* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
4838 of a structure component, returning a FIELD_DECL node.
4839 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
4840
4841 This is done during the parsing of the struct declaration.
4842 The FIELD_DECL nodes are chained together and the lot of them
4843 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
4844
4845tree
4846grokfield (tree declarator, tree declspecs, tree width)
4847{
4848 tree value;
4849
4850 if (declarator == NULL_TREE && width == NULL_TREE)
4851 {
4852 /* This is an unnamed decl.
4853
4854 If we have something of the form "union { list } ;" then this
4855 is the anonymous union extension. Similarly for struct.
4856
4857 If this is something of the form "struct foo;", then
4858 If MS extensions are enabled, this is handled as an
4859 anonymous struct.
4860 Otherwise this is a forward declaration of a structure tag.
4861
4862 If this is something of the form "foo;" and foo is a TYPE_DECL, then
4863 If MS extensions are enabled and foo names a structure, then
4864 again this is an anonymous struct.
4865 Otherwise this is an error.
4866
4867 Oh what a horrid tangled web we weave. I wonder if MS consciously
4868 took this from Plan 9 or if it was an accident of implementation
4869 that took root before someone noticed the bug... */
4870
4871 tree type = TREE_VALUE (declspecs);
4872
4873 if (flag_ms_extensions && TREE_CODE (type) == TYPE_DECL)
4874 type = TREE_TYPE (type);
4875 if (TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE)
4876 {
4877 if (flag_ms_extensions)
4878 ; /* ok */
4879 else if (flag_iso)
4880 goto warn_unnamed_field;
4881 else if (TYPE_NAME (type) == NULL)
4882 ; /* ok */
4883 else
4884 goto warn_unnamed_field;
4885 }
4886 else
4887 {
4888 warn_unnamed_field:
4889 warning ("declaration does not declare anything");
4890 return NULL_TREE;
4891 }
4892 }
4893
4894 value = grokdeclarator (declarator, declspecs, FIELD, 0,
4895 width ? &width : NULL);
4896
4897 finish_decl (value, NULL_TREE, NULL_TREE);
4898 DECL_INITIAL (value) = width;
4899
4900 return value;
4901}
4902
4903/* Generate an error for any duplicate field names in FIELDLIST. Munge
4904 the list such that this does not present a problem later. */
4905
4906static void
4907detect_field_duplicates (tree fieldlist)
4908{
4909 tree x, y;
4910 int timeout = 10;
4911
4912 /* First, see if there are more than "a few" fields.
4913 This is trivially true if there are zero or one fields. */
4914 if (!fieldlist)
4915 return;
4916 x = TREE_CHAIN (fieldlist);
4917 if (!x)
4918 return;
4919 do {
4920 timeout--;
4921 x = TREE_CHAIN (x);
4922 } while (timeout > 0 && x);
4923
4924 /* If there were "few" fields, avoid the overhead of allocating
4925 a hash table. Instead just do the nested traversal thing. */
4926 if (timeout > 0)
4927 {
4928 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
4929 if (DECL_NAME (x))
4930 {
4931 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
4932 if (DECL_NAME (y) == DECL_NAME (x))
4933 {
4934 error ("%Jduplicate member '%D'", x, x);
4935 DECL_NAME (x) = NULL_TREE;
4936 }
4937 }
4938 }
4939 else
4940 {
4941 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
4942 void **slot;
4943
4944 for (x = fieldlist; x ; x = TREE_CHAIN (x))
4945 if ((y = DECL_NAME (x)) != 0)
4946 {
4947 slot = htab_find_slot (htab, y, INSERT);
4948 if (*slot)
4949 {
4950 error ("%Jduplicate member '%D'", x, x);
4951 DECL_NAME (x) = NULL_TREE;
4952 }
4953 *slot = y;
4954 }
4955
4956 htab_delete (htab);
4957 }
4958}
4959
4960/* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
4961 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
4962 ATTRIBUTES are attributes to be applied to the structure. */
4963
4964tree
4965finish_struct (tree t, tree fieldlist, tree attributes)
4966{
4967 tree x;
4968 int toplevel = global_scope == current_scope;
4969 int saw_named_field;
4970
4971 /* If this type was previously laid out as a forward reference,
4972 make sure we lay it out again. */
4973
4974 TYPE_SIZE (t) = 0;
4975
4976 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
4977
4978 /* Nameless union parm types are useful as GCC extension. */
4979 if (! (TREE_CODE (t) == UNION_TYPE && TYPE_NAME (t) == 0) && !pedantic)
4980 /* Otherwise, warn about any struct or union def. in parmlist. */
4981 if (in_parm_level_p ())
4982 {
4983 if (pedantic)
4984 pedwarn ("%s defined inside parms",
4985 TREE_CODE (t) == UNION_TYPE ? _("union") : _("structure"));
4986 else
4987 warning ("%s defined inside parms",
4988 TREE_CODE (t) == UNION_TYPE ? _("union") : _("structure"));
4989 }
4990
4991 if (pedantic)
4992 {
4993 for (x = fieldlist; x; x = TREE_CHAIN (x))
4994 if (DECL_NAME (x) != 0)
4995 break;
4996
4997 if (x == 0)
4998 pedwarn ("%s has no %s",
4999 TREE_CODE (t) == UNION_TYPE ? _("union") : _("struct"),
5000 fieldlist ? _("named members") : _("members"));
5001 }
5002
5003 /* Install struct as DECL_CONTEXT of each field decl.
5004 Also process specified field sizes,m which is found in the DECL_INITIAL.
5005 Store 0 there, except for ": 0" fields (so we can find them
5006 and delete them, below). */
5007
5008 saw_named_field = 0;
5009 for (x = fieldlist; x; x = TREE_CHAIN (x))
5010 {
5011 DECL_CONTEXT (x) = t;
5012 DECL_PACKED (x) |= TYPE_PACKED (t);
5013
5014 /* If any field is const, the structure type is pseudo-const. */
5015 if (TREE_READONLY (x))
5016 C_TYPE_FIELDS_READONLY (t) = 1;
5017 else
5018 {
5019 /* A field that is pseudo-const makes the structure likewise. */
5020 tree t1 = TREE_TYPE (x);
5021 while (TREE_CODE (t1) == ARRAY_TYPE)
5022 t1 = TREE_TYPE (t1);
5023 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5024 && C_TYPE_FIELDS_READONLY (t1))
5025 C_TYPE_FIELDS_READONLY (t) = 1;
5026 }
5027
5028 /* Any field that is volatile means variables of this type must be
5029 treated in some ways as volatile. */
5030 if (TREE_THIS_VOLATILE (x))
5031 C_TYPE_FIELDS_VOLATILE (t) = 1;
5032
5033 /* Any field of nominal variable size implies structure is too. */
5034 if (C_DECL_VARIABLE_SIZE (x))
5035 C_TYPE_VARIABLE_SIZE (t) = 1;
5036
5008 /* Detect invalid nested redefinition. */
5009 if (TREE_TYPE (x) == t)
5010 error ("nested redefinition of `%s'",
5011 IDENTIFIER_POINTER (TYPE_NAME (t)));
5012
5013 if (DECL_INITIAL (x))
5014 {
5015 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5016 DECL_SIZE (x) = bitsize_int (width);
5017 DECL_BIT_FIELD (x) = 1;
5018 SET_DECL_C_BIT_FIELD (x);
5019 }
5020
5021 DECL_INITIAL (x) = 0;
5022
5023 /* Detect flexible array member in an invalid context. */
5024 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5025 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5026 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5027 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5028 {
5029 if (TREE_CODE (t) == UNION_TYPE)
5030 {
5031 error ("%Jflexible array member in union", x);
5032 TREE_TYPE (x) = error_mark_node;
5033 }
5034 else if (TREE_CHAIN (x) != NULL_TREE)
5035 {
5036 error ("%Jflexible array member not at end of struct", x);
5037 TREE_TYPE (x) = error_mark_node;
5038 }
5039 else if (! saw_named_field)
5040 {
5041 error ("%Jflexible array member in otherwise empty struct", x);
5042 TREE_TYPE (x) = error_mark_node;
5043 }
5044 }
5045
5046 if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE
5047 && flexible_array_type_p (TREE_TYPE (x)))
5048 pedwarn ("%Jinvalid use of structure with flexible array member", x);
5049
5050 if (DECL_NAME (x))
5051 saw_named_field = 1;
5052 }
5053
5054 detect_field_duplicates (fieldlist);
5055
5056 /* Now we have the nearly final fieldlist. Record it,
5057 then lay out the structure or union (including the fields). */
5058
5059 TYPE_FIELDS (t) = fieldlist;
5060
5061 layout_type (t);
5062
5063 /* Delete all zero-width bit-fields from the fieldlist. */
5064 {
5065 tree *fieldlistp = &fieldlist;
5066 while (*fieldlistp)
5067 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp))
5068 *fieldlistp = TREE_CHAIN (*fieldlistp);
5069 else
5070 fieldlistp = &TREE_CHAIN (*fieldlistp);
5071 }
5072
5073 /* Now we have the truly final field list.
5074 Store it in this type and in the variants. */
5075
5076 TYPE_FIELDS (t) = fieldlist;
5077
5078 /* If there are lots of fields, sort so we can look through them fast.
5079 We arbitrarily consider 16 or more elts to be "a lot". */
5080
5081 {
5082 int len = 0;
5083
5084 for (x = fieldlist; x; x = TREE_CHAIN (x))
5085 {
5086 if (len > 15 || DECL_NAME (x) == NULL)
5087 break;
5088 len += 1;
5089 }
5090
5091 if (len > 15)
5092 {
5093 tree *field_array;
5094 struct lang_type *space;
5095 struct sorted_fields_type *space2;
5096
5097 len += list_length (x);
5098
5099 /* Use the same allocation policy here that make_node uses, to
5100 ensure that this lives as long as the rest of the struct decl.
5101 All decls in an inline function need to be saved. */
5102
5103 space = ggc_alloc (sizeof (struct lang_type));
5104 space2 = ggc_alloc (sizeof (struct sorted_fields_type) + len * sizeof (tree));
5105
5106 len = 0;
5107 space->s = space2;
5108 field_array = &space2->elts[0];
5109 for (x = fieldlist; x; x = TREE_CHAIN (x))
5110 {
5111 field_array[len++] = x;
5112
5113 /* If there is anonymous struct or union, break out of the loop. */
5114 if (DECL_NAME (x) == NULL)
5115 break;
5116 }
5117 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5118 if (x == NULL)
5119 {
5120 TYPE_LANG_SPECIFIC (t) = space;
5121 TYPE_LANG_SPECIFIC (t)->s->len = len;
5122 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5123 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5124 }
5125 }
5126 }
5127
5128 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5129 {
5130 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5131 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5132 TYPE_ALIGN (x) = TYPE_ALIGN (t);
5133 TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t);
5037 if (DECL_INITIAL (x))
5038 {
5039 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5040 DECL_SIZE (x) = bitsize_int (width);
5041 DECL_BIT_FIELD (x) = 1;
5042 SET_DECL_C_BIT_FIELD (x);
5043 }
5044
5045 DECL_INITIAL (x) = 0;
5046
5047 /* Detect flexible array member in an invalid context. */
5048 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5049 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5050 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5051 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5052 {
5053 if (TREE_CODE (t) == UNION_TYPE)
5054 {
5055 error ("%Jflexible array member in union", x);
5056 TREE_TYPE (x) = error_mark_node;
5057 }
5058 else if (TREE_CHAIN (x) != NULL_TREE)
5059 {
5060 error ("%Jflexible array member not at end of struct", x);
5061 TREE_TYPE (x) = error_mark_node;
5062 }
5063 else if (! saw_named_field)
5064 {
5065 error ("%Jflexible array member in otherwise empty struct", x);
5066 TREE_TYPE (x) = error_mark_node;
5067 }
5068 }
5069
5070 if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE
5071 && flexible_array_type_p (TREE_TYPE (x)))
5072 pedwarn ("%Jinvalid use of structure with flexible array member", x);
5073
5074 if (DECL_NAME (x))
5075 saw_named_field = 1;
5076 }
5077
5078 detect_field_duplicates (fieldlist);
5079
5080 /* Now we have the nearly final fieldlist. Record it,
5081 then lay out the structure or union (including the fields). */
5082
5083 TYPE_FIELDS (t) = fieldlist;
5084
5085 layout_type (t);
5086
5087 /* Delete all zero-width bit-fields from the fieldlist. */
5088 {
5089 tree *fieldlistp = &fieldlist;
5090 while (*fieldlistp)
5091 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp))
5092 *fieldlistp = TREE_CHAIN (*fieldlistp);
5093 else
5094 fieldlistp = &TREE_CHAIN (*fieldlistp);
5095 }
5096
5097 /* Now we have the truly final field list.
5098 Store it in this type and in the variants. */
5099
5100 TYPE_FIELDS (t) = fieldlist;
5101
5102 /* If there are lots of fields, sort so we can look through them fast.
5103 We arbitrarily consider 16 or more elts to be "a lot". */
5104
5105 {
5106 int len = 0;
5107
5108 for (x = fieldlist; x; x = TREE_CHAIN (x))
5109 {
5110 if (len > 15 || DECL_NAME (x) == NULL)
5111 break;
5112 len += 1;
5113 }
5114
5115 if (len > 15)
5116 {
5117 tree *field_array;
5118 struct lang_type *space;
5119 struct sorted_fields_type *space2;
5120
5121 len += list_length (x);
5122
5123 /* Use the same allocation policy here that make_node uses, to
5124 ensure that this lives as long as the rest of the struct decl.
5125 All decls in an inline function need to be saved. */
5126
5127 space = ggc_alloc (sizeof (struct lang_type));
5128 space2 = ggc_alloc (sizeof (struct sorted_fields_type) + len * sizeof (tree));
5129
5130 len = 0;
5131 space->s = space2;
5132 field_array = &space2->elts[0];
5133 for (x = fieldlist; x; x = TREE_CHAIN (x))
5134 {
5135 field_array[len++] = x;
5136
5137 /* If there is anonymous struct or union, break out of the loop. */
5138 if (DECL_NAME (x) == NULL)
5139 break;
5140 }
5141 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5142 if (x == NULL)
5143 {
5144 TYPE_LANG_SPECIFIC (t) = space;
5145 TYPE_LANG_SPECIFIC (t)->s->len = len;
5146 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5147 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5148 }
5149 }
5150 }
5151
5152 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5153 {
5154 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5155 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5156 TYPE_ALIGN (x) = TYPE_ALIGN (t);
5157 TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t);
5158 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
5159 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
5160 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
5134 }
5135
5136 /* If this was supposed to be a transparent union, but we can't
5137 make it one, warn and turn off the flag. */
5138 if (TREE_CODE (t) == UNION_TYPE
5139 && TYPE_TRANSPARENT_UNION (t)
5140 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
5141 {
5142 TYPE_TRANSPARENT_UNION (t) = 0;
5143 warning ("union cannot be made transparent");
5144 }
5145
5146 /* If this structure or union completes the type of any previous
5147 variable declaration, lay it out and output its rtl. */
5148 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5149 x;
5150 x = TREE_CHAIN (x))
5151 {
5152 tree decl = TREE_VALUE (x);
5153 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5154 layout_array_type (TREE_TYPE (decl));
5155 if (TREE_CODE (decl) != TYPE_DECL)
5156 {
5157 layout_decl (decl, 0);
5158 if (c_dialect_objc ())
5159 objc_check_decl (decl);
5160 rest_of_decl_compilation (decl, NULL, toplevel, 0);
5161 if (! toplevel)
5162 expand_decl (decl);
5163 }
5164 }
5165 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5166
5167 /* Finish debugging output for this type. */
5168 rest_of_type_compilation (t, toplevel);
5169
5170 return t;
5171}
5172
5173/* Lay out the type T, and its element type, and so on. */
5174
5175static void
5176layout_array_type (tree t)
5177{
5178 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5179 layout_array_type (TREE_TYPE (t));
5180 layout_type (t);
5181}
5182
5183/* Begin compiling the definition of an enumeration type.
5184 NAME is its name (or null if anonymous).
5185 Returns the type object, as yet incomplete.
5186 Also records info about it so that build_enumerator
5187 may be used to declare the individual values as they are read. */
5188
5189tree
5190start_enum (tree name)
5191{
5192 tree enumtype = 0;
5193
5194 /* If this is the real definition for a previous forward reference,
5195 fill in the contents in the same object that used to be the
5196 forward reference. */
5197
5198 if (name != 0)
5199 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5200
5201 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5202 {
5203 enumtype = make_node (ENUMERAL_TYPE);
5204 pushtag (name, enumtype);
5205 }
5206
5161 }
5162
5163 /* If this was supposed to be a transparent union, but we can't
5164 make it one, warn and turn off the flag. */
5165 if (TREE_CODE (t) == UNION_TYPE
5166 && TYPE_TRANSPARENT_UNION (t)
5167 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
5168 {
5169 TYPE_TRANSPARENT_UNION (t) = 0;
5170 warning ("union cannot be made transparent");
5171 }
5172
5173 /* If this structure or union completes the type of any previous
5174 variable declaration, lay it out and output its rtl. */
5175 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5176 x;
5177 x = TREE_CHAIN (x))
5178 {
5179 tree decl = TREE_VALUE (x);
5180 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5181 layout_array_type (TREE_TYPE (decl));
5182 if (TREE_CODE (decl) != TYPE_DECL)
5183 {
5184 layout_decl (decl, 0);
5185 if (c_dialect_objc ())
5186 objc_check_decl (decl);
5187 rest_of_decl_compilation (decl, NULL, toplevel, 0);
5188 if (! toplevel)
5189 expand_decl (decl);
5190 }
5191 }
5192 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5193
5194 /* Finish debugging output for this type. */
5195 rest_of_type_compilation (t, toplevel);
5196
5197 return t;
5198}
5199
5200/* Lay out the type T, and its element type, and so on. */
5201
5202static void
5203layout_array_type (tree t)
5204{
5205 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5206 layout_array_type (TREE_TYPE (t));
5207 layout_type (t);
5208}
5209
5210/* Begin compiling the definition of an enumeration type.
5211 NAME is its name (or null if anonymous).
5212 Returns the type object, as yet incomplete.
5213 Also records info about it so that build_enumerator
5214 may be used to declare the individual values as they are read. */
5215
5216tree
5217start_enum (tree name)
5218{
5219 tree enumtype = 0;
5220
5221 /* If this is the real definition for a previous forward reference,
5222 fill in the contents in the same object that used to be the
5223 forward reference. */
5224
5225 if (name != 0)
5226 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5227
5228 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5229 {
5230 enumtype = make_node (ENUMERAL_TYPE);
5231 pushtag (name, enumtype);
5232 }
5233
5234 if (C_TYPE_BEING_DEFINED (enumtype))
5235 error ("nested redefinition of `enum %s'", IDENTIFIER_POINTER (name));
5236
5207 C_TYPE_BEING_DEFINED (enumtype) = 1;
5208
5209 if (TYPE_VALUES (enumtype) != 0)
5210 {
5211 /* This enum is a named one that has been declared already. */
5212 error ("redeclaration of `enum %s'", IDENTIFIER_POINTER (name));
5213
5214 /* Completely replace its old definition.
5215 The old enumerators remain defined, however. */
5216 TYPE_VALUES (enumtype) = 0;
5217 }
5218
5219 enum_next_value = integer_zero_node;
5220 enum_overflow = 0;
5221
5222 if (flag_short_enums)
5223 TYPE_PACKED (enumtype) = 1;
5224
5225 return enumtype;
5226}
5227
5228/* After processing and defining all the values of an enumeration type,
5229 install their decls in the enumeration type and finish it off.
5230 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5231 and ATTRIBUTES are the specified attributes.
5232 Returns ENUMTYPE. */
5233
5234tree
5235finish_enum (tree enumtype, tree values, tree attributes)
5236{
5237 tree pair, tem;
5238 tree minnode = 0, maxnode = 0, enum_value_type;
5239 int precision, unsign;
5240 int toplevel = (global_scope == current_scope);
5241
5242 if (in_parm_level_p ())
5243 warning ("enum defined inside parms");
5244
5245 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5246
5247 /* Calculate the maximum value of any enumerator in this type. */
5248
5249 if (values == error_mark_node)
5250 minnode = maxnode = integer_zero_node;
5251 else
5252 {
5253 minnode = maxnode = TREE_VALUE (values);
5254 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5255 {
5256 tree value = TREE_VALUE (pair);
5257 if (tree_int_cst_lt (maxnode, value))
5258 maxnode = value;
5259 if (tree_int_cst_lt (value, minnode))
5260 minnode = value;
5261 }
5262 }
5263
5264 /* Construct the final type of this enumeration. It is the same
5265 as one of the integral types - the narrowest one that fits, except
5266 that normally we only go as narrow as int - and signed iff any of
5267 the values are negative. */
5268 unsign = (tree_int_cst_sgn (minnode) >= 0);
5269 precision = MAX (min_precision (minnode, unsign),
5270 min_precision (maxnode, unsign));
5271 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5272 {
5273 tree narrowest = c_common_type_for_size (precision, unsign);
5274 if (narrowest == 0)
5275 {
5276 warning ("enumeration values exceed range of largest integer");
5277 narrowest = long_long_integer_type_node;
5278 }
5279
5280 precision = TYPE_PRECISION (narrowest);
5281 }
5282 else
5283 precision = TYPE_PRECISION (integer_type_node);
5284
5285 if (precision == TYPE_PRECISION (integer_type_node))
5286 enum_value_type = c_common_type_for_size (precision, 0);
5287 else
5288 enum_value_type = enumtype;
5289
5290 TYPE_MIN_VALUE (enumtype) = minnode;
5291 TYPE_MAX_VALUE (enumtype) = maxnode;
5292 TREE_UNSIGNED (enumtype) = unsign;
5293 TYPE_SIZE (enumtype) = 0;
5294
5295 /* If the precision of the type was specific with an attribute and it
5296 was too small, give an error. Otherwise, use it. */
5297 if (TYPE_PRECISION (enumtype))
5298 {
5299 if (precision > TYPE_PRECISION (enumtype))
5300 error ("specified mode too small for enumeral values");
5301 }
5302 else
5303 TYPE_PRECISION (enumtype) = precision;
5304
5305 layout_type (enumtype);
5306
5307 if (values != error_mark_node)
5308 {
5309 /* Change the type of the enumerators to be the enum type. We
5310 need to do this irrespective of the size of the enum, for
5311 proper type checking. Replace the DECL_INITIALs of the
5312 enumerators, and the value slots of the list, with copies
5313 that have the enum type; they cannot be modified in place
5314 because they may be shared (e.g. integer_zero_node) Finally,
5315 change the purpose slots to point to the names of the decls. */
5316 for (pair = values; pair; pair = TREE_CHAIN (pair))
5317 {
5318 tree enu = TREE_PURPOSE (pair);
5319
5320 TREE_TYPE (enu) = enumtype;
5321
5322 /* The ISO C Standard mandates enumerators to have type int,
5323 even though the underlying type of an enum type is
5324 unspecified. Here we convert any enumerators that fit in
5325 an int to type int, to avoid promotions to unsigned types
5326 when comparing integers with enumerators that fit in the
5327 int range. When -pedantic is given, build_enumerator()
5328 would have already taken care of those that don't fit. */
5329 if (int_fits_type_p (DECL_INITIAL (enu), enum_value_type))
5330 DECL_INITIAL (enu) = convert (enum_value_type, DECL_INITIAL (enu));
5331 else
5332 DECL_INITIAL (enu) = convert (enumtype, DECL_INITIAL (enu));
5333
5334 TREE_PURPOSE (pair) = DECL_NAME (enu);
5335 TREE_VALUE (pair) = DECL_INITIAL (enu);
5336 }
5337
5338 TYPE_VALUES (enumtype) = values;
5339 }
5340
5341 /* Fix up all variant types of this enum type. */
5342 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5343 {
5344 if (tem == enumtype)
5345 continue;
5346 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5347 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5348 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5349 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5350 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5351 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5352 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5353 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5354 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5355 TREE_UNSIGNED (tem) = TREE_UNSIGNED (enumtype);
5356 }
5357
5358 /* Finish debugging output for this type. */
5359 rest_of_type_compilation (enumtype, toplevel);
5360
5361 return enumtype;
5362}
5363
5364/* Build and install a CONST_DECL for one value of the
5365 current enumeration type (one that was begun with start_enum).
5366 Return a tree-list containing the CONST_DECL and its value.
5367 Assignment of sequential values by default is handled here. */
5368
5369tree
5370build_enumerator (tree name, tree value)
5371{
5372 tree decl, type;
5373
5374 /* Validate and default VALUE. */
5375
5376 /* Remove no-op casts from the value. */
5377 if (value)
5378 STRIP_TYPE_NOPS (value);
5379
5380 if (value != 0)
5381 {
5382 if (TREE_CODE (value) == INTEGER_CST)
5383 {
5384 value = default_conversion (value);
5385 constant_expression_warning (value);
5386 }
5387 else
5388 {
5389 error ("enumerator value for `%s' not integer constant",
5390 IDENTIFIER_POINTER (name));
5391 value = 0;
5392 }
5393 }
5394
5395 /* Default based on previous value. */
5396 /* It should no longer be possible to have NON_LVALUE_EXPR
5397 in the default. */
5398 if (value == 0)
5399 {
5400 value = enum_next_value;
5401 if (enum_overflow)
5402 error ("overflow in enumeration values");
5403 }
5404
5405 if (pedantic && ! int_fits_type_p (value, integer_type_node))
5406 {
5407 pedwarn ("ISO C restricts enumerator values to range of `int'");
5408 value = convert (integer_type_node, value);
5409 }
5410
5411 /* Set basis for default for next value. */
5412 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
5413 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5414
5415 /* Now create a declaration for the enum value name. */
5416
5417 type = TREE_TYPE (value);
5418 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5419 TYPE_PRECISION (integer_type_node)),
5420 (TYPE_PRECISION (type)
5421 >= TYPE_PRECISION (integer_type_node)
5422 && TREE_UNSIGNED (type)));
5423
5424 decl = build_decl (CONST_DECL, name, type);
5425 DECL_INITIAL (decl) = convert (type, value);
5426 pushdecl (decl);
5427
5428 return tree_cons (decl, value, NULL_TREE);
5429}
5430
5431
5432/* Create the FUNCTION_DECL for a function definition.
5433 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5434 the declaration; they describe the function's name and the type it returns,
5435 but twisted together in a fashion that parallels the syntax of C.
5436
5437 This function creates a binding context for the function body
5438 as well as setting up the FUNCTION_DECL in current_function_decl.
5439
5440 Returns 1 on success. If the DECLARATOR is not suitable for a function
5441 (it defines a datum instead), we return 0, which tells
5442 yyparse to report a parse error. */
5443
5444int
5445start_function (tree declspecs, tree declarator, tree attributes)
5446{
5447 tree decl1, old_decl;
5448 tree restype;
5449 int old_immediate_size_expand = immediate_size_expand;
5450
5451 current_function_returns_value = 0; /* Assume, until we see it does. */
5452 current_function_returns_null = 0;
5453 current_function_returns_abnormally = 0;
5454 warn_about_return_type = 0;
5455 current_extern_inline = 0;
5456 c_in_iteration_stmt = 0;
5457 c_in_case_stmt = 0;
5458
5459 /* Don't expand any sizes in the return type of the function. */
5460 immediate_size_expand = 0;
5461
5462 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, NULL);
5463
5464 /* If the declarator is not suitable for a function definition,
5465 cause a syntax error. */
5466 if (decl1 == 0)
5467 {
5468 immediate_size_expand = old_immediate_size_expand;
5469 return 0;
5470 }
5471
5472 decl_attributes (&decl1, attributes, 0);
5473
5474 if (DECL_DECLARED_INLINE_P (decl1)
5475 && DECL_UNINLINABLE (decl1)
5476 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
5477 warning ("%Jinline function '%D' given attribute noinline", decl1, decl1);
5478
5479 announce_function (decl1);
5480
5481 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
5482 {
5483 error ("return type is an incomplete type");
5484 /* Make it return void instead. */
5485 TREE_TYPE (decl1)
5486 = build_function_type (void_type_node,
5487 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
5488 }
5489
5490 if (warn_about_return_type)
5491 pedwarn_c99 ("return type defaults to `int'");
5492
5493 /* Save the parm names or decls from this function's declarator
5494 where store_parm_decls will find them. */
5495 current_function_parms = last_function_parms;
5496 current_function_parm_tags = last_function_parm_tags;
5497 current_function_parm_others = last_function_parm_others;
5498
5499 /* Make the init_value nonzero so pushdecl knows this is not tentative.
5500 error_mark_node is replaced below (in poplevel) with the BLOCK. */
5501 DECL_INITIAL (decl1) = error_mark_node;
5502
5503 /* If this definition isn't a prototype and we had a prototype declaration
5504 before, copy the arg type info from that prototype.
5505 But not if what we had before was a builtin function. */
5506 old_decl = lookup_name_current_level (DECL_NAME (decl1));
5507 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
5508 && !DECL_BUILT_IN (old_decl)
5509 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5510 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (old_decl))))
5511 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
5512 {
5513 TREE_TYPE (decl1) = TREE_TYPE (old_decl);
5514 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
5515 }
5516
5517 /* Optionally warn of old-fashioned def with no previous prototype. */
5518 if (warn_strict_prototypes
5519 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
5520 && C_DECL_ISNT_PROTOTYPE (old_decl))
5521 warning ("function declaration isn't a prototype");
5522 /* Optionally warn of any global def with no previous prototype. */
5523 else if (warn_missing_prototypes
5524 && TREE_PUBLIC (decl1)
5525 && ! MAIN_NAME_P (DECL_NAME (decl1))
5526 && C_DECL_ISNT_PROTOTYPE (old_decl))
5527 warning ("%Jno previous prototype for '%D'", decl1, decl1);
5528 /* Optionally warn of any def with no previous prototype
5529 if the function has already been used. */
5530 else if (warn_missing_prototypes
5531 && old_decl != 0 && TREE_USED (old_decl)
5532 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
5533 warning ("%J'%D' was used with no prototype before its definition",
5534 decl1, decl1);
5535 /* Optionally warn of any global def with no previous declaration. */
5536 else if (warn_missing_declarations
5537 && TREE_PUBLIC (decl1)
5538 && old_decl == 0
5539 && ! MAIN_NAME_P (DECL_NAME (decl1)))
5540 warning ("%Jno previous declaration for '%D'", decl1, decl1);
5541 /* Optionally warn of any def with no previous declaration
5542 if the function has already been used. */
5543 else if (warn_missing_declarations
5544 && old_decl != 0 && TREE_USED (old_decl)
5545 && C_DECL_IMPLICIT (old_decl))
5546 warning ("%J`%D' was used with no declaration before its definition",
5547 decl1, decl1);
5548
5549 /* This is a definition, not a reference.
5550 So normally clear DECL_EXTERNAL.
5551 However, `extern inline' acts like a declaration
5552 except for defining how to inline. So set DECL_EXTERNAL in that case. */
5553 DECL_EXTERNAL (decl1) = current_extern_inline;
5554
5555 /* This function exists in static storage.
5556 (This does not mean `static' in the C sense!) */
5557 TREE_STATIC (decl1) = 1;
5558
5559 /* A nested function is not global. */
5560 if (current_function_decl != 0)
5561 TREE_PUBLIC (decl1) = 0;
5562
5563#ifdef ENABLE_CHECKING
5564 /* This is the earliest point at which we might know the assembler
5565 name of the function. Thus, if it's set before this, die horribly. */
5566 if (DECL_ASSEMBLER_NAME_SET_P (decl1))
5567 abort ();
5568#endif
5569
5570 /* If #pragma weak was used, mark the decl weak now. */
5571 if (current_scope == global_scope)
5572 maybe_apply_pragma_weak (decl1);
5573
5574 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
5575 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
5576 {
5577 tree args;
5578 int argct = 0;
5579
5580 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5581 != integer_type_node)
5582 pedwarn ("%Jreturn type of '%D' is not `int'", decl1, decl1);
5583
5584 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
5585 args = TREE_CHAIN (args))
5586 {
5587 tree type = args ? TREE_VALUE (args) : 0;
5588
5589 if (type == void_type_node)
5590 break;
5591
5592 ++argct;
5593 switch (argct)
5594 {
5595 case 1:
5596 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
5597 pedwarn ("%Jfirst argument of '%D' should be `int'",
5598 decl1, decl1);
5599 break;
5600
5601 case 2:
5602 if (TREE_CODE (type) != POINTER_TYPE
5603 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5604 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5605 != char_type_node))
5606 pedwarn ("%Jsecond argument of '%D' should be 'char **'",
5607 decl1, decl1);
5608 break;
5609
5610 case 3:
5611 if (TREE_CODE (type) != POINTER_TYPE
5612 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5613 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5614 != char_type_node))
5615 pedwarn ("%Jthird argument of '%D' should probably be "
5616 "'char **'", decl1, decl1);
5617 break;
5618 }
5619 }
5620
5621 /* It is intentional that this message does not mention the third
5622 argument because it's only mentioned in an appendix of the
5623 standard. */
5624 if (argct > 0 && (argct < 2 || argct > 3))
5625 pedwarn ("%J'%D' takes only zero or two arguments", decl1, decl1);
5626
5627 if (! TREE_PUBLIC (decl1))
5628 pedwarn ("%J'%D' is normally a non-static function", decl1, decl1);
5629 }
5630
5631 /* Record the decl so that the function name is defined.
5632 If we already have a decl for this name, and it is a FUNCTION_DECL,
5633 use the old decl. */
5634
5635 current_function_decl = pushdecl (decl1);
5636
5637 pushlevel (0);
5638 declare_parm_level ();
5639
5640 make_decl_rtl (current_function_decl, NULL);
5641
5642 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
5643 /* Promote the value to int before returning it. */
5644 if (c_promoting_integer_type_p (restype))
5645 {
5646 /* It retains unsignedness if not really getting wider. */
5647 if (TREE_UNSIGNED (restype)
5648 && (TYPE_PRECISION (restype)
5649 == TYPE_PRECISION (integer_type_node)))
5650 restype = unsigned_type_node;
5651 else
5652 restype = integer_type_node;
5653 }
5654 DECL_RESULT (current_function_decl)
5655 = build_decl (RESULT_DECL, NULL_TREE, restype);
5656
5657 /* If this fcn was already referenced via a block-scope `extern' decl
5658 (or an implicit decl), propagate certain information about the usage. */
5659 if (TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (current_function_decl)))
5660 TREE_ADDRESSABLE (current_function_decl) = 1;
5661
5662 immediate_size_expand = old_immediate_size_expand;
5663
5664 start_fname_decls ();
5665
5666 return 1;
5667}
5668
5669/* Subroutine of store_parm_decls which handles new-style function
5670 definitions (prototype format). The parms already have decls, so we
5671 need only record them as in effect and complain if any redundant
5672 old-style parm decls were written. */
5673static void
5674store_parm_decls_newstyle (void)
5675{
5676 tree decl, last;
5677 tree fndecl = current_function_decl;
5678 tree parms = current_function_parms;
5679 tree tags = current_function_parm_tags;
5680 tree others = current_function_parm_others;
5681
5682 if (current_scope->parms || current_scope->names || current_scope->tags)
5683 {
5684 error ("%Jold-style parameter declarations in prototyped "
5685 "function definition", fndecl);
5686
5687 /* Get rid of the old-style declarations. */
5688 poplevel (0, 0, 0);
5689 pushlevel (0);
5690 }
5691
5692 /* Now make all the parameter declarations visible in the function body.
5693 We can bypass most of the grunt work of pushdecl. */
5694 for (last = 0, decl = parms; decl; last = decl, decl = TREE_CHAIN (decl))
5695 {
5696 DECL_CONTEXT (decl) = current_function_decl;
5697 if (DECL_NAME (decl) == 0)
5698 error ("%Jparameter name omitted", decl);
5699 else
5700 {
5701 if (IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)))
5702 current_scope->shadowed
5703 = tree_cons (DECL_NAME (decl),
5704 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)),
5705 current_scope->shadowed);
5706 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)) = decl;
5707 }
5708 }
5709 current_scope->parms = parms;
5710 current_scope->parms_last = last;
5711
5712 /* Record the parameter list in the function declaration. */
5713 DECL_ARGUMENTS (fndecl) = parms;
5714
5715 /* Now make all the ancillary declarations visible, likewise. */
5716 for (last = 0, decl = others; decl; last = decl, decl = TREE_CHAIN (decl))
5717 {
5718 DECL_CONTEXT (decl) = current_function_decl;
5719 if (DECL_NAME (decl)
5720 && TYPE_MAIN_VARIANT (TREE_TYPE (decl)) != void_type_node)
5721 {
5722 if (IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)))
5723 current_scope->shadowed
5724 = tree_cons (DECL_NAME (decl),
5725 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)),
5726 current_scope->shadowed);
5727 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)) = decl;
5728 }
5729 }
5730 current_scope->names = others;
5731 current_scope->names_last = last;
5732
5733 /* And all the tag declarations. */
5734 for (decl = tags; decl; decl = TREE_CHAIN (decl))
5735 if (TREE_PURPOSE (decl))
5736 {
5737 if (IDENTIFIER_TAG_VALUE (TREE_PURPOSE (decl)))
5738 current_scope->shadowed_tags
5739 = tree_cons (TREE_PURPOSE (decl),
5740 IDENTIFIER_SYMBOL_VALUE (TREE_PURPOSE (decl)),
5741 current_scope->shadowed_tags);
5742 IDENTIFIER_TAG_VALUE (TREE_PURPOSE (decl)) = TREE_VALUE (decl);
5743 }
5744 current_scope->tags = tags;
5745}
5746
5747/* Subroutine of store_parm_decls which handles old-style function
5748 definitions (separate parameter list and declarations). */
5749
5750static void
5751store_parm_decls_oldstyle (void)
5752{
5753 tree parm, decl, last;
5754 tree fndecl = current_function_decl;
5755
5756 /* This is the identifier list from the function declarator. */
5757 tree parmids = current_function_parms;
5758
5759 /* We use DECL_WEAK as a flag to show which parameters have been
5760 seen already, since it is not used on PARM_DECL. */
5761#ifdef ENABLE_CHECKING
5762 for (parm = current_scope->parms; parm; parm = TREE_CHAIN (parm))
5763 if (DECL_WEAK (parm))
5764 abort ();
5765#endif
5766
5767 /* Match each formal parameter name with its declaration. Save each
5768 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
5769 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
5770 {
5771 if (TREE_VALUE (parm) == 0)
5772 {
5773 error ("%Jparameter name missing from parameter list", fndecl);
5774 TREE_PURPOSE (parm) = 0;
5775 continue;
5776 }
5777
5778 decl = IDENTIFIER_SYMBOL_VALUE (TREE_VALUE (parm));
5779 if (decl && DECL_CONTEXT (decl) == fndecl)
5780 {
5781 /* If we got something other than a PARM_DECL it is an error. */
5782 if (TREE_CODE (decl) != PARM_DECL)
5783 error ("%J\"%D\" declared as a non-parameter", decl, decl);
5784 /* If the declaration is already marked, we have a duplicate
5785 name. Complain and ignore the duplicate. */
5786 else if (DECL_WEAK (decl))
5787 {
5788 error ("%Jmultiple parameters named \"%D\"", decl, decl);
5789 TREE_PURPOSE (parm) = 0;
5790 continue;
5791 }
5792 /* If the declaration says "void", complain and turn it into
5793 an int. */
5794 else if (VOID_TYPE_P (TREE_TYPE (decl)))
5795 {
5796 error ("%Jparameter \"%D\" declared void", decl, decl);
5797 TREE_TYPE (decl) = integer_type_node;
5798 DECL_ARG_TYPE (decl) = integer_type_node;
5799 layout_decl (decl, 0);
5800 }
5801 }
5802 /* If no declaration found, default to int. */
5803 else
5804 {
5805 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
5806 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
5807 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
5808 pushdecl (decl);
5809
5810 if (flag_isoc99)
5811 pedwarn ("%Jtype of \"%D\" defaults to \"int\"", decl, decl);
5812 else if (extra_warnings)
5813 warning ("%Jtype of \"%D\" defaults to \"int\"", decl, decl);
5814 }
5815
5816 TREE_PURPOSE (parm) = decl;
5817 DECL_WEAK (decl) = 1;
5818 }
5819
5820 /* Now examine the parms chain for incomplete declarations
5821 and declarations with no corresponding names. */
5822
5823 for (parm = current_scope->parms; parm; parm = TREE_CHAIN (parm))
5824 {
5825 if (!COMPLETE_TYPE_P (TREE_TYPE (parm)))
5826 {
5827 error ("%Jparameter \"%D\" has incomplete type", parm, parm);
5828 TREE_TYPE (parm) = error_mark_node;
5829 }
5830
5831 if (! DECL_WEAK (parm))
5832 {
5833 error ("%Jdeclaration for parameter \"%D\" but no such parameter",
5834 parm, parm);
5835
5836 /* Pretend the parameter was not missing.
5837 This gets us to a standard state and minimizes
5838 further error messages. */
5839 parmids = chainon (parmids, tree_cons (parm, 0, 0));
5840 }
5841 }
5842
5843 /* Chain the declarations together in the order of the list of
5844 names. Store that chain in the function decl, replacing the
5845 list of names. Update the current scope to match. */
5846 DECL_ARGUMENTS (fndecl) = 0;
5847
5848 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
5849 if (TREE_PURPOSE (parm))
5850 break;
5851 if (parm && TREE_PURPOSE (parm))
5852 {
5853 last = TREE_PURPOSE (parm);
5854 DECL_ARGUMENTS (fndecl) = last;
5855 current_scope->parms = last;
5856 DECL_WEAK (last) = 0;
5857
5858 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
5859 if (TREE_PURPOSE (parm))
5860 {
5861 TREE_CHAIN (last) = TREE_PURPOSE (parm);
5862 last = TREE_PURPOSE (parm);
5863 DECL_WEAK (last) = 0;
5864 }
5865 current_scope->parms_last = last;
5866 TREE_CHAIN (last) = 0;
5867 }
5868
5869 /* If there was a previous prototype,
5870 set the DECL_ARG_TYPE of each argument according to
5871 the type previously specified, and report any mismatches. */
5872
5873 if (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
5874 {
5875 tree type;
5876 for (parm = DECL_ARGUMENTS (fndecl),
5877 type = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
5878 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
5879 != void_type_node));
5880 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
5881 {
5882 if (parm == 0 || type == 0
5883 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
5884 {
5885 error ("number of arguments doesn't match prototype");
5886 error ("%Hprototype declaration",
5887 &current_function_prototype_locus);
5888 break;
5889 }
5890 /* Type for passing arg must be consistent with that
5891 declared for the arg. ISO C says we take the unqualified
5892 type for parameters declared with qualified type. */
5893 if (! comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
5894 TYPE_MAIN_VARIANT (TREE_VALUE (type)),
5895 COMPARE_STRICT))
5896 {
5897 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
5898 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
5899 {
5900 /* Adjust argument to match prototype. E.g. a previous
5901 `int foo(float);' prototype causes
5902 `int foo(x) float x; {...}' to be treated like
5903 `int foo(float x) {...}'. This is particularly
5904 useful for argument types like uid_t. */
5905 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
5906
5907 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
5908 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
5909 && TYPE_PRECISION (TREE_TYPE (parm))
5910 < TYPE_PRECISION (integer_type_node))
5911 DECL_ARG_TYPE (parm) = integer_type_node;
5912
5913 if (pedantic)
5914 {
5915 pedwarn ("promoted argument \"%D\" "
5916 "doesn't match prototype", parm);
5917 pedwarn ("%Hprototype declaration",
5918 &current_function_prototype_locus);
5919 }
5920 }
5921 else
5922 {
5923 error ("argument \"%D\" doesn't match prototype", parm);
5924 error ("%Hprototype declaration",
5925 &current_function_prototype_locus);
5926 }
5927 }
5928 }
5929 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
5930 }
5931
5932 /* Otherwise, create a prototype that would match. */
5933
5934 else
5935 {
5936 tree actual = 0, last = 0, type;
5937
5938 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
5939 {
5940 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
5941 if (last)
5942 TREE_CHAIN (last) = type;
5943 else
5944 actual = type;
5945 last = type;
5946 }
5947 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
5948 if (last)
5949 TREE_CHAIN (last) = type;
5950 else
5951 actual = type;
5952
5953 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
5954 of the type of this function, but we need to avoid having this
5955 affect the types of other similarly-typed functions, so we must
5956 first force the generation of an identical (but separate) type
5957 node for the relevant function type. The new node we create
5958 will be a variant of the main variant of the original function
5959 type. */
5960
5961 TREE_TYPE (fndecl) = build_type_copy (TREE_TYPE (fndecl));
5962
5963 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
5964 }
5965}
5966
5967/* Store the parameter declarations into the current function declaration.
5968 This is called after parsing the parameter declarations, before
5969 digesting the body of the function.
5970
5971 For an old-style definition, construct a prototype out of the old-style
5972 parameter declarations and inject it into the function's type. */
5973
5974void
5975store_parm_decls (void)
5976{
5977 tree fndecl = current_function_decl;
5978
5237 C_TYPE_BEING_DEFINED (enumtype) = 1;
5238
5239 if (TYPE_VALUES (enumtype) != 0)
5240 {
5241 /* This enum is a named one that has been declared already. */
5242 error ("redeclaration of `enum %s'", IDENTIFIER_POINTER (name));
5243
5244 /* Completely replace its old definition.
5245 The old enumerators remain defined, however. */
5246 TYPE_VALUES (enumtype) = 0;
5247 }
5248
5249 enum_next_value = integer_zero_node;
5250 enum_overflow = 0;
5251
5252 if (flag_short_enums)
5253 TYPE_PACKED (enumtype) = 1;
5254
5255 return enumtype;
5256}
5257
5258/* After processing and defining all the values of an enumeration type,
5259 install their decls in the enumeration type and finish it off.
5260 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5261 and ATTRIBUTES are the specified attributes.
5262 Returns ENUMTYPE. */
5263
5264tree
5265finish_enum (tree enumtype, tree values, tree attributes)
5266{
5267 tree pair, tem;
5268 tree minnode = 0, maxnode = 0, enum_value_type;
5269 int precision, unsign;
5270 int toplevel = (global_scope == current_scope);
5271
5272 if (in_parm_level_p ())
5273 warning ("enum defined inside parms");
5274
5275 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5276
5277 /* Calculate the maximum value of any enumerator in this type. */
5278
5279 if (values == error_mark_node)
5280 minnode = maxnode = integer_zero_node;
5281 else
5282 {
5283 minnode = maxnode = TREE_VALUE (values);
5284 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5285 {
5286 tree value = TREE_VALUE (pair);
5287 if (tree_int_cst_lt (maxnode, value))
5288 maxnode = value;
5289 if (tree_int_cst_lt (value, minnode))
5290 minnode = value;
5291 }
5292 }
5293
5294 /* Construct the final type of this enumeration. It is the same
5295 as one of the integral types - the narrowest one that fits, except
5296 that normally we only go as narrow as int - and signed iff any of
5297 the values are negative. */
5298 unsign = (tree_int_cst_sgn (minnode) >= 0);
5299 precision = MAX (min_precision (minnode, unsign),
5300 min_precision (maxnode, unsign));
5301 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5302 {
5303 tree narrowest = c_common_type_for_size (precision, unsign);
5304 if (narrowest == 0)
5305 {
5306 warning ("enumeration values exceed range of largest integer");
5307 narrowest = long_long_integer_type_node;
5308 }
5309
5310 precision = TYPE_PRECISION (narrowest);
5311 }
5312 else
5313 precision = TYPE_PRECISION (integer_type_node);
5314
5315 if (precision == TYPE_PRECISION (integer_type_node))
5316 enum_value_type = c_common_type_for_size (precision, 0);
5317 else
5318 enum_value_type = enumtype;
5319
5320 TYPE_MIN_VALUE (enumtype) = minnode;
5321 TYPE_MAX_VALUE (enumtype) = maxnode;
5322 TREE_UNSIGNED (enumtype) = unsign;
5323 TYPE_SIZE (enumtype) = 0;
5324
5325 /* If the precision of the type was specific with an attribute and it
5326 was too small, give an error. Otherwise, use it. */
5327 if (TYPE_PRECISION (enumtype))
5328 {
5329 if (precision > TYPE_PRECISION (enumtype))
5330 error ("specified mode too small for enumeral values");
5331 }
5332 else
5333 TYPE_PRECISION (enumtype) = precision;
5334
5335 layout_type (enumtype);
5336
5337 if (values != error_mark_node)
5338 {
5339 /* Change the type of the enumerators to be the enum type. We
5340 need to do this irrespective of the size of the enum, for
5341 proper type checking. Replace the DECL_INITIALs of the
5342 enumerators, and the value slots of the list, with copies
5343 that have the enum type; they cannot be modified in place
5344 because they may be shared (e.g. integer_zero_node) Finally,
5345 change the purpose slots to point to the names of the decls. */
5346 for (pair = values; pair; pair = TREE_CHAIN (pair))
5347 {
5348 tree enu = TREE_PURPOSE (pair);
5349
5350 TREE_TYPE (enu) = enumtype;
5351
5352 /* The ISO C Standard mandates enumerators to have type int,
5353 even though the underlying type of an enum type is
5354 unspecified. Here we convert any enumerators that fit in
5355 an int to type int, to avoid promotions to unsigned types
5356 when comparing integers with enumerators that fit in the
5357 int range. When -pedantic is given, build_enumerator()
5358 would have already taken care of those that don't fit. */
5359 if (int_fits_type_p (DECL_INITIAL (enu), enum_value_type))
5360 DECL_INITIAL (enu) = convert (enum_value_type, DECL_INITIAL (enu));
5361 else
5362 DECL_INITIAL (enu) = convert (enumtype, DECL_INITIAL (enu));
5363
5364 TREE_PURPOSE (pair) = DECL_NAME (enu);
5365 TREE_VALUE (pair) = DECL_INITIAL (enu);
5366 }
5367
5368 TYPE_VALUES (enumtype) = values;
5369 }
5370
5371 /* Fix up all variant types of this enum type. */
5372 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5373 {
5374 if (tem == enumtype)
5375 continue;
5376 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5377 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5378 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5379 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5380 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5381 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5382 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5383 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5384 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5385 TREE_UNSIGNED (tem) = TREE_UNSIGNED (enumtype);
5386 }
5387
5388 /* Finish debugging output for this type. */
5389 rest_of_type_compilation (enumtype, toplevel);
5390
5391 return enumtype;
5392}
5393
5394/* Build and install a CONST_DECL for one value of the
5395 current enumeration type (one that was begun with start_enum).
5396 Return a tree-list containing the CONST_DECL and its value.
5397 Assignment of sequential values by default is handled here. */
5398
5399tree
5400build_enumerator (tree name, tree value)
5401{
5402 tree decl, type;
5403
5404 /* Validate and default VALUE. */
5405
5406 /* Remove no-op casts from the value. */
5407 if (value)
5408 STRIP_TYPE_NOPS (value);
5409
5410 if (value != 0)
5411 {
5412 if (TREE_CODE (value) == INTEGER_CST)
5413 {
5414 value = default_conversion (value);
5415 constant_expression_warning (value);
5416 }
5417 else
5418 {
5419 error ("enumerator value for `%s' not integer constant",
5420 IDENTIFIER_POINTER (name));
5421 value = 0;
5422 }
5423 }
5424
5425 /* Default based on previous value. */
5426 /* It should no longer be possible to have NON_LVALUE_EXPR
5427 in the default. */
5428 if (value == 0)
5429 {
5430 value = enum_next_value;
5431 if (enum_overflow)
5432 error ("overflow in enumeration values");
5433 }
5434
5435 if (pedantic && ! int_fits_type_p (value, integer_type_node))
5436 {
5437 pedwarn ("ISO C restricts enumerator values to range of `int'");
5438 value = convert (integer_type_node, value);
5439 }
5440
5441 /* Set basis for default for next value. */
5442 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
5443 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5444
5445 /* Now create a declaration for the enum value name. */
5446
5447 type = TREE_TYPE (value);
5448 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5449 TYPE_PRECISION (integer_type_node)),
5450 (TYPE_PRECISION (type)
5451 >= TYPE_PRECISION (integer_type_node)
5452 && TREE_UNSIGNED (type)));
5453
5454 decl = build_decl (CONST_DECL, name, type);
5455 DECL_INITIAL (decl) = convert (type, value);
5456 pushdecl (decl);
5457
5458 return tree_cons (decl, value, NULL_TREE);
5459}
5460
5461
5462/* Create the FUNCTION_DECL for a function definition.
5463 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5464 the declaration; they describe the function's name and the type it returns,
5465 but twisted together in a fashion that parallels the syntax of C.
5466
5467 This function creates a binding context for the function body
5468 as well as setting up the FUNCTION_DECL in current_function_decl.
5469
5470 Returns 1 on success. If the DECLARATOR is not suitable for a function
5471 (it defines a datum instead), we return 0, which tells
5472 yyparse to report a parse error. */
5473
5474int
5475start_function (tree declspecs, tree declarator, tree attributes)
5476{
5477 tree decl1, old_decl;
5478 tree restype;
5479 int old_immediate_size_expand = immediate_size_expand;
5480
5481 current_function_returns_value = 0; /* Assume, until we see it does. */
5482 current_function_returns_null = 0;
5483 current_function_returns_abnormally = 0;
5484 warn_about_return_type = 0;
5485 current_extern_inline = 0;
5486 c_in_iteration_stmt = 0;
5487 c_in_case_stmt = 0;
5488
5489 /* Don't expand any sizes in the return type of the function. */
5490 immediate_size_expand = 0;
5491
5492 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, NULL);
5493
5494 /* If the declarator is not suitable for a function definition,
5495 cause a syntax error. */
5496 if (decl1 == 0)
5497 {
5498 immediate_size_expand = old_immediate_size_expand;
5499 return 0;
5500 }
5501
5502 decl_attributes (&decl1, attributes, 0);
5503
5504 if (DECL_DECLARED_INLINE_P (decl1)
5505 && DECL_UNINLINABLE (decl1)
5506 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
5507 warning ("%Jinline function '%D' given attribute noinline", decl1, decl1);
5508
5509 announce_function (decl1);
5510
5511 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
5512 {
5513 error ("return type is an incomplete type");
5514 /* Make it return void instead. */
5515 TREE_TYPE (decl1)
5516 = build_function_type (void_type_node,
5517 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
5518 }
5519
5520 if (warn_about_return_type)
5521 pedwarn_c99 ("return type defaults to `int'");
5522
5523 /* Save the parm names or decls from this function's declarator
5524 where store_parm_decls will find them. */
5525 current_function_parms = last_function_parms;
5526 current_function_parm_tags = last_function_parm_tags;
5527 current_function_parm_others = last_function_parm_others;
5528
5529 /* Make the init_value nonzero so pushdecl knows this is not tentative.
5530 error_mark_node is replaced below (in poplevel) with the BLOCK. */
5531 DECL_INITIAL (decl1) = error_mark_node;
5532
5533 /* If this definition isn't a prototype and we had a prototype declaration
5534 before, copy the arg type info from that prototype.
5535 But not if what we had before was a builtin function. */
5536 old_decl = lookup_name_current_level (DECL_NAME (decl1));
5537 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
5538 && !DECL_BUILT_IN (old_decl)
5539 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5540 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (old_decl))))
5541 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
5542 {
5543 TREE_TYPE (decl1) = TREE_TYPE (old_decl);
5544 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
5545 }
5546
5547 /* Optionally warn of old-fashioned def with no previous prototype. */
5548 if (warn_strict_prototypes
5549 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
5550 && C_DECL_ISNT_PROTOTYPE (old_decl))
5551 warning ("function declaration isn't a prototype");
5552 /* Optionally warn of any global def with no previous prototype. */
5553 else if (warn_missing_prototypes
5554 && TREE_PUBLIC (decl1)
5555 && ! MAIN_NAME_P (DECL_NAME (decl1))
5556 && C_DECL_ISNT_PROTOTYPE (old_decl))
5557 warning ("%Jno previous prototype for '%D'", decl1, decl1);
5558 /* Optionally warn of any def with no previous prototype
5559 if the function has already been used. */
5560 else if (warn_missing_prototypes
5561 && old_decl != 0 && TREE_USED (old_decl)
5562 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
5563 warning ("%J'%D' was used with no prototype before its definition",
5564 decl1, decl1);
5565 /* Optionally warn of any global def with no previous declaration. */
5566 else if (warn_missing_declarations
5567 && TREE_PUBLIC (decl1)
5568 && old_decl == 0
5569 && ! MAIN_NAME_P (DECL_NAME (decl1)))
5570 warning ("%Jno previous declaration for '%D'", decl1, decl1);
5571 /* Optionally warn of any def with no previous declaration
5572 if the function has already been used. */
5573 else if (warn_missing_declarations
5574 && old_decl != 0 && TREE_USED (old_decl)
5575 && C_DECL_IMPLICIT (old_decl))
5576 warning ("%J`%D' was used with no declaration before its definition",
5577 decl1, decl1);
5578
5579 /* This is a definition, not a reference.
5580 So normally clear DECL_EXTERNAL.
5581 However, `extern inline' acts like a declaration
5582 except for defining how to inline. So set DECL_EXTERNAL in that case. */
5583 DECL_EXTERNAL (decl1) = current_extern_inline;
5584
5585 /* This function exists in static storage.
5586 (This does not mean `static' in the C sense!) */
5587 TREE_STATIC (decl1) = 1;
5588
5589 /* A nested function is not global. */
5590 if (current_function_decl != 0)
5591 TREE_PUBLIC (decl1) = 0;
5592
5593#ifdef ENABLE_CHECKING
5594 /* This is the earliest point at which we might know the assembler
5595 name of the function. Thus, if it's set before this, die horribly. */
5596 if (DECL_ASSEMBLER_NAME_SET_P (decl1))
5597 abort ();
5598#endif
5599
5600 /* If #pragma weak was used, mark the decl weak now. */
5601 if (current_scope == global_scope)
5602 maybe_apply_pragma_weak (decl1);
5603
5604 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
5605 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
5606 {
5607 tree args;
5608 int argct = 0;
5609
5610 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5611 != integer_type_node)
5612 pedwarn ("%Jreturn type of '%D' is not `int'", decl1, decl1);
5613
5614 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
5615 args = TREE_CHAIN (args))
5616 {
5617 tree type = args ? TREE_VALUE (args) : 0;
5618
5619 if (type == void_type_node)
5620 break;
5621
5622 ++argct;
5623 switch (argct)
5624 {
5625 case 1:
5626 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
5627 pedwarn ("%Jfirst argument of '%D' should be `int'",
5628 decl1, decl1);
5629 break;
5630
5631 case 2:
5632 if (TREE_CODE (type) != POINTER_TYPE
5633 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5634 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5635 != char_type_node))
5636 pedwarn ("%Jsecond argument of '%D' should be 'char **'",
5637 decl1, decl1);
5638 break;
5639
5640 case 3:
5641 if (TREE_CODE (type) != POINTER_TYPE
5642 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5643 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5644 != char_type_node))
5645 pedwarn ("%Jthird argument of '%D' should probably be "
5646 "'char **'", decl1, decl1);
5647 break;
5648 }
5649 }
5650
5651 /* It is intentional that this message does not mention the third
5652 argument because it's only mentioned in an appendix of the
5653 standard. */
5654 if (argct > 0 && (argct < 2 || argct > 3))
5655 pedwarn ("%J'%D' takes only zero or two arguments", decl1, decl1);
5656
5657 if (! TREE_PUBLIC (decl1))
5658 pedwarn ("%J'%D' is normally a non-static function", decl1, decl1);
5659 }
5660
5661 /* Record the decl so that the function name is defined.
5662 If we already have a decl for this name, and it is a FUNCTION_DECL,
5663 use the old decl. */
5664
5665 current_function_decl = pushdecl (decl1);
5666
5667 pushlevel (0);
5668 declare_parm_level ();
5669
5670 make_decl_rtl (current_function_decl, NULL);
5671
5672 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
5673 /* Promote the value to int before returning it. */
5674 if (c_promoting_integer_type_p (restype))
5675 {
5676 /* It retains unsignedness if not really getting wider. */
5677 if (TREE_UNSIGNED (restype)
5678 && (TYPE_PRECISION (restype)
5679 == TYPE_PRECISION (integer_type_node)))
5680 restype = unsigned_type_node;
5681 else
5682 restype = integer_type_node;
5683 }
5684 DECL_RESULT (current_function_decl)
5685 = build_decl (RESULT_DECL, NULL_TREE, restype);
5686
5687 /* If this fcn was already referenced via a block-scope `extern' decl
5688 (or an implicit decl), propagate certain information about the usage. */
5689 if (TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (current_function_decl)))
5690 TREE_ADDRESSABLE (current_function_decl) = 1;
5691
5692 immediate_size_expand = old_immediate_size_expand;
5693
5694 start_fname_decls ();
5695
5696 return 1;
5697}
5698
5699/* Subroutine of store_parm_decls which handles new-style function
5700 definitions (prototype format). The parms already have decls, so we
5701 need only record them as in effect and complain if any redundant
5702 old-style parm decls were written. */
5703static void
5704store_parm_decls_newstyle (void)
5705{
5706 tree decl, last;
5707 tree fndecl = current_function_decl;
5708 tree parms = current_function_parms;
5709 tree tags = current_function_parm_tags;
5710 tree others = current_function_parm_others;
5711
5712 if (current_scope->parms || current_scope->names || current_scope->tags)
5713 {
5714 error ("%Jold-style parameter declarations in prototyped "
5715 "function definition", fndecl);
5716
5717 /* Get rid of the old-style declarations. */
5718 poplevel (0, 0, 0);
5719 pushlevel (0);
5720 }
5721
5722 /* Now make all the parameter declarations visible in the function body.
5723 We can bypass most of the grunt work of pushdecl. */
5724 for (last = 0, decl = parms; decl; last = decl, decl = TREE_CHAIN (decl))
5725 {
5726 DECL_CONTEXT (decl) = current_function_decl;
5727 if (DECL_NAME (decl) == 0)
5728 error ("%Jparameter name omitted", decl);
5729 else
5730 {
5731 if (IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)))
5732 current_scope->shadowed
5733 = tree_cons (DECL_NAME (decl),
5734 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)),
5735 current_scope->shadowed);
5736 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)) = decl;
5737 }
5738 }
5739 current_scope->parms = parms;
5740 current_scope->parms_last = last;
5741
5742 /* Record the parameter list in the function declaration. */
5743 DECL_ARGUMENTS (fndecl) = parms;
5744
5745 /* Now make all the ancillary declarations visible, likewise. */
5746 for (last = 0, decl = others; decl; last = decl, decl = TREE_CHAIN (decl))
5747 {
5748 DECL_CONTEXT (decl) = current_function_decl;
5749 if (DECL_NAME (decl)
5750 && TYPE_MAIN_VARIANT (TREE_TYPE (decl)) != void_type_node)
5751 {
5752 if (IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)))
5753 current_scope->shadowed
5754 = tree_cons (DECL_NAME (decl),
5755 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)),
5756 current_scope->shadowed);
5757 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)) = decl;
5758 }
5759 }
5760 current_scope->names = others;
5761 current_scope->names_last = last;
5762
5763 /* And all the tag declarations. */
5764 for (decl = tags; decl; decl = TREE_CHAIN (decl))
5765 if (TREE_PURPOSE (decl))
5766 {
5767 if (IDENTIFIER_TAG_VALUE (TREE_PURPOSE (decl)))
5768 current_scope->shadowed_tags
5769 = tree_cons (TREE_PURPOSE (decl),
5770 IDENTIFIER_SYMBOL_VALUE (TREE_PURPOSE (decl)),
5771 current_scope->shadowed_tags);
5772 IDENTIFIER_TAG_VALUE (TREE_PURPOSE (decl)) = TREE_VALUE (decl);
5773 }
5774 current_scope->tags = tags;
5775}
5776
5777/* Subroutine of store_parm_decls which handles old-style function
5778 definitions (separate parameter list and declarations). */
5779
5780static void
5781store_parm_decls_oldstyle (void)
5782{
5783 tree parm, decl, last;
5784 tree fndecl = current_function_decl;
5785
5786 /* This is the identifier list from the function declarator. */
5787 tree parmids = current_function_parms;
5788
5789 /* We use DECL_WEAK as a flag to show which parameters have been
5790 seen already, since it is not used on PARM_DECL. */
5791#ifdef ENABLE_CHECKING
5792 for (parm = current_scope->parms; parm; parm = TREE_CHAIN (parm))
5793 if (DECL_WEAK (parm))
5794 abort ();
5795#endif
5796
5797 /* Match each formal parameter name with its declaration. Save each
5798 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
5799 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
5800 {
5801 if (TREE_VALUE (parm) == 0)
5802 {
5803 error ("%Jparameter name missing from parameter list", fndecl);
5804 TREE_PURPOSE (parm) = 0;
5805 continue;
5806 }
5807
5808 decl = IDENTIFIER_SYMBOL_VALUE (TREE_VALUE (parm));
5809 if (decl && DECL_CONTEXT (decl) == fndecl)
5810 {
5811 /* If we got something other than a PARM_DECL it is an error. */
5812 if (TREE_CODE (decl) != PARM_DECL)
5813 error ("%J\"%D\" declared as a non-parameter", decl, decl);
5814 /* If the declaration is already marked, we have a duplicate
5815 name. Complain and ignore the duplicate. */
5816 else if (DECL_WEAK (decl))
5817 {
5818 error ("%Jmultiple parameters named \"%D\"", decl, decl);
5819 TREE_PURPOSE (parm) = 0;
5820 continue;
5821 }
5822 /* If the declaration says "void", complain and turn it into
5823 an int. */
5824 else if (VOID_TYPE_P (TREE_TYPE (decl)))
5825 {
5826 error ("%Jparameter \"%D\" declared void", decl, decl);
5827 TREE_TYPE (decl) = integer_type_node;
5828 DECL_ARG_TYPE (decl) = integer_type_node;
5829 layout_decl (decl, 0);
5830 }
5831 }
5832 /* If no declaration found, default to int. */
5833 else
5834 {
5835 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
5836 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
5837 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
5838 pushdecl (decl);
5839
5840 if (flag_isoc99)
5841 pedwarn ("%Jtype of \"%D\" defaults to \"int\"", decl, decl);
5842 else if (extra_warnings)
5843 warning ("%Jtype of \"%D\" defaults to \"int\"", decl, decl);
5844 }
5845
5846 TREE_PURPOSE (parm) = decl;
5847 DECL_WEAK (decl) = 1;
5848 }
5849
5850 /* Now examine the parms chain for incomplete declarations
5851 and declarations with no corresponding names. */
5852
5853 for (parm = current_scope->parms; parm; parm = TREE_CHAIN (parm))
5854 {
5855 if (!COMPLETE_TYPE_P (TREE_TYPE (parm)))
5856 {
5857 error ("%Jparameter \"%D\" has incomplete type", parm, parm);
5858 TREE_TYPE (parm) = error_mark_node;
5859 }
5860
5861 if (! DECL_WEAK (parm))
5862 {
5863 error ("%Jdeclaration for parameter \"%D\" but no such parameter",
5864 parm, parm);
5865
5866 /* Pretend the parameter was not missing.
5867 This gets us to a standard state and minimizes
5868 further error messages. */
5869 parmids = chainon (parmids, tree_cons (parm, 0, 0));
5870 }
5871 }
5872
5873 /* Chain the declarations together in the order of the list of
5874 names. Store that chain in the function decl, replacing the
5875 list of names. Update the current scope to match. */
5876 DECL_ARGUMENTS (fndecl) = 0;
5877
5878 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
5879 if (TREE_PURPOSE (parm))
5880 break;
5881 if (parm && TREE_PURPOSE (parm))
5882 {
5883 last = TREE_PURPOSE (parm);
5884 DECL_ARGUMENTS (fndecl) = last;
5885 current_scope->parms = last;
5886 DECL_WEAK (last) = 0;
5887
5888 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
5889 if (TREE_PURPOSE (parm))
5890 {
5891 TREE_CHAIN (last) = TREE_PURPOSE (parm);
5892 last = TREE_PURPOSE (parm);
5893 DECL_WEAK (last) = 0;
5894 }
5895 current_scope->parms_last = last;
5896 TREE_CHAIN (last) = 0;
5897 }
5898
5899 /* If there was a previous prototype,
5900 set the DECL_ARG_TYPE of each argument according to
5901 the type previously specified, and report any mismatches. */
5902
5903 if (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
5904 {
5905 tree type;
5906 for (parm = DECL_ARGUMENTS (fndecl),
5907 type = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
5908 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
5909 != void_type_node));
5910 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
5911 {
5912 if (parm == 0 || type == 0
5913 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
5914 {
5915 error ("number of arguments doesn't match prototype");
5916 error ("%Hprototype declaration",
5917 &current_function_prototype_locus);
5918 break;
5919 }
5920 /* Type for passing arg must be consistent with that
5921 declared for the arg. ISO C says we take the unqualified
5922 type for parameters declared with qualified type. */
5923 if (! comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
5924 TYPE_MAIN_VARIANT (TREE_VALUE (type)),
5925 COMPARE_STRICT))
5926 {
5927 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
5928 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
5929 {
5930 /* Adjust argument to match prototype. E.g. a previous
5931 `int foo(float);' prototype causes
5932 `int foo(x) float x; {...}' to be treated like
5933 `int foo(float x) {...}'. This is particularly
5934 useful for argument types like uid_t. */
5935 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
5936
5937 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
5938 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
5939 && TYPE_PRECISION (TREE_TYPE (parm))
5940 < TYPE_PRECISION (integer_type_node))
5941 DECL_ARG_TYPE (parm) = integer_type_node;
5942
5943 if (pedantic)
5944 {
5945 pedwarn ("promoted argument \"%D\" "
5946 "doesn't match prototype", parm);
5947 pedwarn ("%Hprototype declaration",
5948 &current_function_prototype_locus);
5949 }
5950 }
5951 else
5952 {
5953 error ("argument \"%D\" doesn't match prototype", parm);
5954 error ("%Hprototype declaration",
5955 &current_function_prototype_locus);
5956 }
5957 }
5958 }
5959 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
5960 }
5961
5962 /* Otherwise, create a prototype that would match. */
5963
5964 else
5965 {
5966 tree actual = 0, last = 0, type;
5967
5968 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
5969 {
5970 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
5971 if (last)
5972 TREE_CHAIN (last) = type;
5973 else
5974 actual = type;
5975 last = type;
5976 }
5977 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
5978 if (last)
5979 TREE_CHAIN (last) = type;
5980 else
5981 actual = type;
5982
5983 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
5984 of the type of this function, but we need to avoid having this
5985 affect the types of other similarly-typed functions, so we must
5986 first force the generation of an identical (but separate) type
5987 node for the relevant function type. The new node we create
5988 will be a variant of the main variant of the original function
5989 type. */
5990
5991 TREE_TYPE (fndecl) = build_type_copy (TREE_TYPE (fndecl));
5992
5993 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
5994 }
5995}
5996
5997/* Store the parameter declarations into the current function declaration.
5998 This is called after parsing the parameter declarations, before
5999 digesting the body of the function.
6000
6001 For an old-style definition, construct a prototype out of the old-style
6002 parameter declarations and inject it into the function's type. */
6003
6004void
6005store_parm_decls (void)
6006{
6007 tree fndecl = current_function_decl;
6008
5979 /* The function containing FNDECL, if any. */
5980 tree context = decl_function_context (fndecl);
5981
5982 /* True if this definition is written with a prototype. */
5983 bool prototype = (current_function_parms
5984 && TREE_CODE (current_function_parms) != TREE_LIST);
5985
5986 if (prototype)
5987 store_parm_decls_newstyle ();
5988 else
5989 store_parm_decls_oldstyle ();
5990
5991 /* The next call to pushlevel will be a function body. */
5992
5993 next_is_function_body = true;
5994
5995 /* Write a record describing this function definition to the prototypes
5996 file (if requested). */
5997
5998 gen_aux_info_record (fndecl, 1, 0, prototype);
5999
6000 /* Initialize the RTL code for the function. */
6001 allocate_struct_function (fndecl);
6002
6003 /* Begin the statement tree for this function. */
6004 begin_stmt_tree (&DECL_SAVED_TREE (fndecl));
6005
6009 /* True if this definition is written with a prototype. */
6010 bool prototype = (current_function_parms
6011 && TREE_CODE (current_function_parms) != TREE_LIST);
6012
6013 if (prototype)
6014 store_parm_decls_newstyle ();
6015 else
6016 store_parm_decls_oldstyle ();
6017
6018 /* The next call to pushlevel will be a function body. */
6019
6020 next_is_function_body = true;
6021
6022 /* Write a record describing this function definition to the prototypes
6023 file (if requested). */
6024
6025 gen_aux_info_record (fndecl, 1, 0, prototype);
6026
6027 /* Initialize the RTL code for the function. */
6028 allocate_struct_function (fndecl);
6029
6030 /* Begin the statement tree for this function. */
6031 begin_stmt_tree (&DECL_SAVED_TREE (fndecl));
6032
6006 /* If this is a nested function, save away the sizes of any
6007 variable-size types so that we can expand them when generating
6008 RTL. */
6009 if (context)
6010 {
6011 tree t;
6033 /* Save away the sizes of any variable-size types so that we can
6034 expand them when generating RTL. */
6035 DECL_LANG_SPECIFIC (fndecl)->pending_sizes = get_pending_sizes ();
6012
6036
6013 DECL_LANG_SPECIFIC (fndecl)->pending_sizes
6014 = nreverse (get_pending_sizes ());
6015 for (t = DECL_LANG_SPECIFIC (fndecl)->pending_sizes;
6016 t;
6017 t = TREE_CHAIN (t))
6018 SAVE_EXPR_CONTEXT (TREE_VALUE (t)) = context;
6019 }
6020
6021 /* This function is being processed in whole-function mode. */
6022 cfun->x_whole_function_mode_p = 1;
6023
6024 /* Even though we're inside a function body, we still don't want to
6025 call expand_expr to calculate the size of a variable-sized array.
6026 We haven't necessarily assigned RTL to all variables yet, so it's
6027 not safe to try to expand expressions involving them. */
6028 immediate_size_expand = 0;
6029 cfun->x_dont_save_pending_sizes_p = 1;
6030}
6031
6032/* Finish up a function declaration and compile that function
6033 all the way to assembler language output. The free the storage
6034 for the function definition.
6035
6036 This is called after parsing the body of the function definition. */
6037
6038void
6039finish_function (void)
6040{
6041 tree fndecl = current_function_decl;
6042
6043 /* When a function declaration is totally empty, e.g.
6044 void foo(void) { }
6045 (the argument list is irrelevant) the compstmt rule will not
6046 bother calling pushlevel/poplevel, which means we get here with
6047 the scope stack out of sync. Detect this situation by noticing
6048 that current_scope is still as store_parm_decls left it, and do
6049 a dummy push/pop to get back to consistency.
6050 Note that the call to pushlevel does not actually push another
6051 scope - see there for details. */
6052
6053 if (current_scope->parm_flag && next_is_function_body)
6054 {
6055 pushlevel (0);
6056 poplevel (0, 0, 0);
6057 }
6058
6059 if (TREE_CODE (fndecl) == FUNCTION_DECL
6060 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6061 {
6062 tree args = DECL_ARGUMENTS (fndecl);
6063 for (; args; args = TREE_CHAIN (args))
6064 {
6065 tree type = TREE_TYPE (args);
6066 if (INTEGRAL_TYPE_P (type)
6067 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6068 DECL_ARG_TYPE (args) = integer_type_node;
6069 }
6070 }
6071
6072 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6073 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6074
6075 /* Must mark the RESULT_DECL as being in this function. */
6076
6077 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6078 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6079
6080 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6081 {
6082 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6083 != integer_type_node)
6084 {
6085 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6086 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6087 if (!warn_main)
6088 pedwarn ("%Jreturn type of '%D' is not `int'", fndecl, fndecl);
6089 }
6090 else
6091 {
6092#ifdef DEFAULT_MAIN_RETURN
6093 /* Make it so that `main' always returns success by default. */
6094 DEFAULT_MAIN_RETURN;
6095#else
6096 if (flag_isoc99)
6097 c_expand_return (integer_zero_node);
6098#endif
6099 }
6100 }
6101
6102 finish_fname_decls ();
6103
6104 /* Tie off the statement tree for this function. */
6105 finish_stmt_tree (&DECL_SAVED_TREE (fndecl));
6106
6107 /* Complain if there's just no return statement. */
6108 if (warn_return_type
6109 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6110 && !current_function_returns_value && !current_function_returns_null
6111 /* Don't complain if we abort. */
6112 && !current_function_returns_abnormally
6113 /* Don't warn for main(). */
6114 && !MAIN_NAME_P (DECL_NAME (fndecl))
6115 /* Or if they didn't actually specify a return type. */
6116 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6117 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6118 inline function, as we might never be compiled separately. */
6119 && DECL_INLINE (fndecl))
6120 warning ("no return statement in function returning non-void");
6121
6122 /* With just -Wextra, complain only if function returns both with
6123 and without a value. */
6124 if (extra_warnings
6125 && current_function_returns_value
6126 && current_function_returns_null)
6127 warning ("this function may return with or without a value");
6128
6129 /* We're leaving the context of this function, so zap cfun. It's still in
6130 DECL_SAVED_INSNS, and we'll restore it in tree_rest_of_compilation. */
6131 cfun = NULL;
6132
6133 /* ??? Objc emits functions after finalizing the compilation unit.
6134 This should be cleaned up later and this conditional removed. */
6135 if (!cgraph_global_info_ready)
6136 cgraph_finalize_function (fndecl, false);
6137 else
6138 c_expand_body (fndecl);
6139 current_function_decl = NULL;
6140}
6141
6142/* Generate the RTL for a deferred function FNDECL. */
6143
6144void
6145c_expand_deferred_function (tree fndecl)
6146{
6147 /* DECL_INLINE or DECL_RESULT might got cleared after the inline
6148 function was deferred, e.g. in duplicate_decls. */
6149 if (DECL_INLINE (fndecl) && DECL_RESULT (fndecl))
6150 {
6151 if (flag_inline_trees)
6152 {
6153 timevar_push (TV_INTEGRATION);
6154 optimize_inline_calls (fndecl);
6155 timevar_pop (TV_INTEGRATION);
6156 }
6157 c_expand_body (fndecl);
6158 current_function_decl = NULL;
6159 }
6160}
6161
6162/* Generate the RTL for the body of FNDECL. If NESTED_P is nonzero,
6163 then we are already in the process of generating RTL for another
6164 function. */
6165
6166static void
6167c_expand_body_1 (tree fndecl, int nested_p)
6168{
6169 if (nested_p)
6037 /* This function is being processed in whole-function mode. */
6038 cfun->x_whole_function_mode_p = 1;
6039
6040 /* Even though we're inside a function body, we still don't want to
6041 call expand_expr to calculate the size of a variable-sized array.
6042 We haven't necessarily assigned RTL to all variables yet, so it's
6043 not safe to try to expand expressions involving them. */
6044 immediate_size_expand = 0;
6045 cfun->x_dont_save_pending_sizes_p = 1;
6046}
6047
6048/* Finish up a function declaration and compile that function
6049 all the way to assembler language output. The free the storage
6050 for the function definition.
6051
6052 This is called after parsing the body of the function definition. */
6053
6054void
6055finish_function (void)
6056{
6057 tree fndecl = current_function_decl;
6058
6059 /* When a function declaration is totally empty, e.g.
6060 void foo(void) { }
6061 (the argument list is irrelevant) the compstmt rule will not
6062 bother calling pushlevel/poplevel, which means we get here with
6063 the scope stack out of sync. Detect this situation by noticing
6064 that current_scope is still as store_parm_decls left it, and do
6065 a dummy push/pop to get back to consistency.
6066 Note that the call to pushlevel does not actually push another
6067 scope - see there for details. */
6068
6069 if (current_scope->parm_flag && next_is_function_body)
6070 {
6071 pushlevel (0);
6072 poplevel (0, 0, 0);
6073 }
6074
6075 if (TREE_CODE (fndecl) == FUNCTION_DECL
6076 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6077 {
6078 tree args = DECL_ARGUMENTS (fndecl);
6079 for (; args; args = TREE_CHAIN (args))
6080 {
6081 tree type = TREE_TYPE (args);
6082 if (INTEGRAL_TYPE_P (type)
6083 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6084 DECL_ARG_TYPE (args) = integer_type_node;
6085 }
6086 }
6087
6088 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6089 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6090
6091 /* Must mark the RESULT_DECL as being in this function. */
6092
6093 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6094 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6095
6096 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6097 {
6098 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6099 != integer_type_node)
6100 {
6101 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6102 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6103 if (!warn_main)
6104 pedwarn ("%Jreturn type of '%D' is not `int'", fndecl, fndecl);
6105 }
6106 else
6107 {
6108#ifdef DEFAULT_MAIN_RETURN
6109 /* Make it so that `main' always returns success by default. */
6110 DEFAULT_MAIN_RETURN;
6111#else
6112 if (flag_isoc99)
6113 c_expand_return (integer_zero_node);
6114#endif
6115 }
6116 }
6117
6118 finish_fname_decls ();
6119
6120 /* Tie off the statement tree for this function. */
6121 finish_stmt_tree (&DECL_SAVED_TREE (fndecl));
6122
6123 /* Complain if there's just no return statement. */
6124 if (warn_return_type
6125 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6126 && !current_function_returns_value && !current_function_returns_null
6127 /* Don't complain if we abort. */
6128 && !current_function_returns_abnormally
6129 /* Don't warn for main(). */
6130 && !MAIN_NAME_P (DECL_NAME (fndecl))
6131 /* Or if they didn't actually specify a return type. */
6132 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6133 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6134 inline function, as we might never be compiled separately. */
6135 && DECL_INLINE (fndecl))
6136 warning ("no return statement in function returning non-void");
6137
6138 /* With just -Wextra, complain only if function returns both with
6139 and without a value. */
6140 if (extra_warnings
6141 && current_function_returns_value
6142 && current_function_returns_null)
6143 warning ("this function may return with or without a value");
6144
6145 /* We're leaving the context of this function, so zap cfun. It's still in
6146 DECL_SAVED_INSNS, and we'll restore it in tree_rest_of_compilation. */
6147 cfun = NULL;
6148
6149 /* ??? Objc emits functions after finalizing the compilation unit.
6150 This should be cleaned up later and this conditional removed. */
6151 if (!cgraph_global_info_ready)
6152 cgraph_finalize_function (fndecl, false);
6153 else
6154 c_expand_body (fndecl);
6155 current_function_decl = NULL;
6156}
6157
6158/* Generate the RTL for a deferred function FNDECL. */
6159
6160void
6161c_expand_deferred_function (tree fndecl)
6162{
6163 /* DECL_INLINE or DECL_RESULT might got cleared after the inline
6164 function was deferred, e.g. in duplicate_decls. */
6165 if (DECL_INLINE (fndecl) && DECL_RESULT (fndecl))
6166 {
6167 if (flag_inline_trees)
6168 {
6169 timevar_push (TV_INTEGRATION);
6170 optimize_inline_calls (fndecl);
6171 timevar_pop (TV_INTEGRATION);
6172 }
6173 c_expand_body (fndecl);
6174 current_function_decl = NULL;
6175 }
6176}
6177
6178/* Generate the RTL for the body of FNDECL. If NESTED_P is nonzero,
6179 then we are already in the process of generating RTL for another
6180 function. */
6181
6182static void
6183c_expand_body_1 (tree fndecl, int nested_p)
6184{
6185 if (nested_p)
6170 {
6171 /* Make sure that we will evaluate variable-sized types involved
6172 in our function's type. */
6173 expand_pending_sizes (DECL_LANG_SPECIFIC (fndecl)->pending_sizes);
6174
6175 /* Squirrel away our current state. */
6176 push_function_context ();
6177 }
6186 /* Squirrel away our current state. */
6187 push_function_context ();
6178
6188
6189 /* Make sure that we will evaluate variable-sized types involved
6190 in our function's type. */
6191 put_pending_sizes (DECL_LANG_SPECIFIC (fndecl)->pending_sizes);
6179 tree_rest_of_compilation (fndecl, nested_p);
6180
6181 if (nested_p)
6182 /* Return to the enclosing function. */
6183 pop_function_context ();
6184
6185 if (DECL_STATIC_CONSTRUCTOR (fndecl))
6186 {
6187 if (targetm.have_ctors_dtors)
6188 (* targetm.asm_out.constructor) (XEXP (DECL_RTL (fndecl), 0),
6189 DEFAULT_INIT_PRIORITY);
6190 else
6191 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6192 }
6193
6194 if (DECL_STATIC_DESTRUCTOR (fndecl))
6195 {
6196 if (targetm.have_ctors_dtors)
6197 (* targetm.asm_out.destructor) (XEXP (DECL_RTL (fndecl), 0),
6198 DEFAULT_INIT_PRIORITY);
6199 else
6200 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6201 }
6202}
6203
6204/* Like c_expand_body_1 but only for unnested functions. */
6205
6206void
6207c_expand_body (tree fndecl)
6208{
6209
6210 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6211 c_expand_body_1 (fndecl, 0);
6212}
6213
6214/* Check the declarations given in a for-loop for satisfying the C99
6215 constraints. */
6216void
6217check_for_loop_decls (void)
6218{
6219 tree t;
6220
6221 if (!flag_isoc99)
6222 {
6223 /* If we get here, declarations have been used in a for loop without
6224 the C99 for loop scope. This doesn't make much sense, so don't
6225 allow it. */
6226 error ("'for' loop initial declaration used outside C99 mode");
6227 return;
6228 }
6229 /* C99 subclause 6.8.5 paragraph 3:
6230
6231 [#3] The declaration part of a for statement shall only
6232 declare identifiers for objects having storage class auto or
6233 register.
6234
6235 It isn't clear whether, in this sentence, "identifiers" binds to
6236 "shall only declare" or to "objects" - that is, whether all identifiers
6237 declared must be identifiers for objects, or whether the restriction
6238 only applies to those that are. (A question on this in comp.std.c
6239 in November 2000 received no answer.) We implement the strictest
6240 interpretation, to avoid creating an extension which later causes
6241 problems. */
6242
6243 for (t = current_scope->tags; t; t = TREE_CHAIN (t))
6244 {
6245 if (TREE_PURPOSE (t) != 0)
6246 {
6247 enum tree_code code = TREE_CODE (TREE_VALUE (t));
6248
6249 if (code == RECORD_TYPE)
6250 error ("'struct %s' declared in 'for' loop initial declaration",
6251 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6252 else if (code == UNION_TYPE)
6253 error ("'union %s' declared in 'for' loop initial declaration",
6254 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6255 else
6256 error ("'enum %s' declared in 'for' loop initial declaration",
6257 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6258 }
6259 }
6260
6261 for (t = getdecls (); t; t = TREE_CHAIN (t))
6262 {
6263 if (TREE_CODE (t) != VAR_DECL && DECL_NAME (t))
6264 error ("%Jdeclaration of non-variable '%D' in 'for' loop "
6265 "initial declaration", t, t);
6266 else if (TREE_STATIC (t))
6267 error ("%Jdeclaration of static variable '%D' in 'for' loop "
6268 "initial declaration", t, t);
6269 else if (DECL_EXTERNAL (t))
6270 error ("%Jdeclaration of 'extern' variable '%D' in 'for' loop "
6271 "initial declaration", t, t);
6272 }
6273}
6274
6275/* Save and reinitialize the variables
6276 used during compilation of a C function. */
6277
6278void
6279c_push_function_context (struct function *f)
6280{
6281 struct language_function *p;
6282 p = ggc_alloc (sizeof (struct language_function));
6283 f->language = p;
6284
6285 p->base.x_stmt_tree = c_stmt_tree;
6286 p->base.x_scope_stmt_stack = c_scope_stmt_stack;
6287 p->x_in_iteration_stmt = c_in_iteration_stmt;
6288 p->x_in_case_stmt = c_in_case_stmt;
6289 p->returns_value = current_function_returns_value;
6290 p->returns_null = current_function_returns_null;
6291 p->returns_abnormally = current_function_returns_abnormally;
6292 p->warn_about_return_type = warn_about_return_type;
6293 p->extern_inline = current_extern_inline;
6294}
6295
6296/* Restore the variables used during compilation of a C function. */
6297
6298void
6299c_pop_function_context (struct function *f)
6300{
6301 struct language_function *p = f->language;
6302
6303 if (DECL_SAVED_INSNS (current_function_decl) == 0
6304 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6305 {
6306 /* Stop pointing to the local nodes about to be freed. */
6307 /* But DECL_INITIAL must remain nonzero so we know this
6308 was an actual function definition. */
6309 DECL_INITIAL (current_function_decl) = error_mark_node;
6310 DECL_ARGUMENTS (current_function_decl) = 0;
6311 }
6312
6313 c_stmt_tree = p->base.x_stmt_tree;
6314 c_scope_stmt_stack = p->base.x_scope_stmt_stack;
6315 c_in_iteration_stmt = p->x_in_iteration_stmt;
6316 c_in_case_stmt = p->x_in_case_stmt;
6317 current_function_returns_value = p->returns_value;
6318 current_function_returns_null = p->returns_null;
6319 current_function_returns_abnormally = p->returns_abnormally;
6320 warn_about_return_type = p->warn_about_return_type;
6321 current_extern_inline = p->extern_inline;
6322
6323 f->language = NULL;
6324}
6325
6326/* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6327
6328void
6329c_dup_lang_specific_decl (tree decl)
6330{
6331 struct lang_decl *ld;
6332
6333 if (!DECL_LANG_SPECIFIC (decl))
6334 return;
6335
6336 ld = ggc_alloc (sizeof (struct lang_decl));
6337 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6338 DECL_LANG_SPECIFIC (decl) = ld;
6339}
6340
6341/* The functions below are required for functionality of doing
6342 function at once processing in the C front end. Currently these
6343 functions are not called from anywhere in the C front end, but as
6344 these changes continue, that will change. */
6345
6346/* Returns nonzero if the current statement is a full expression,
6347 i.e. temporaries created during that statement should be destroyed
6348 at the end of the statement. */
6349
6350int
6351stmts_are_full_exprs_p (void)
6352{
6353 return 0;
6354}
6355
6356/* Returns the stmt_tree (if any) to which statements are currently
6357 being added. If there is no active statement-tree, NULL is
6358 returned. */
6359
6360stmt_tree
6361current_stmt_tree (void)
6362{
6363 return &c_stmt_tree;
6364}
6365
6366/* Returns the stack of SCOPE_STMTs for the current function. */
6367
6368tree *
6369current_scope_stmt_stack (void)
6370{
6371 return &c_scope_stmt_stack;
6372}
6373
6374/* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6375 C. */
6376
6377int
6378anon_aggr_type_p (tree node ATTRIBUTE_UNUSED)
6379{
6380 return 0;
6381}
6382
6383/* Dummy function in place of callback used by C++. */
6384
6385void
6386extract_interface_info (void)
6387{
6388}
6389
6390/* Return a new COMPOUND_STMT, after adding it to the current
6391 statement tree. */
6392
6393tree
6394c_begin_compound_stmt (void)
6395{
6396 tree stmt;
6397
6398 /* Create the COMPOUND_STMT. */
6192 tree_rest_of_compilation (fndecl, nested_p);
6193
6194 if (nested_p)
6195 /* Return to the enclosing function. */
6196 pop_function_context ();
6197
6198 if (DECL_STATIC_CONSTRUCTOR (fndecl))
6199 {
6200 if (targetm.have_ctors_dtors)
6201 (* targetm.asm_out.constructor) (XEXP (DECL_RTL (fndecl), 0),
6202 DEFAULT_INIT_PRIORITY);
6203 else
6204 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6205 }
6206
6207 if (DECL_STATIC_DESTRUCTOR (fndecl))
6208 {
6209 if (targetm.have_ctors_dtors)
6210 (* targetm.asm_out.destructor) (XEXP (DECL_RTL (fndecl), 0),
6211 DEFAULT_INIT_PRIORITY);
6212 else
6213 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6214 }
6215}
6216
6217/* Like c_expand_body_1 but only for unnested functions. */
6218
6219void
6220c_expand_body (tree fndecl)
6221{
6222
6223 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6224 c_expand_body_1 (fndecl, 0);
6225}
6226
6227/* Check the declarations given in a for-loop for satisfying the C99
6228 constraints. */
6229void
6230check_for_loop_decls (void)
6231{
6232 tree t;
6233
6234 if (!flag_isoc99)
6235 {
6236 /* If we get here, declarations have been used in a for loop without
6237 the C99 for loop scope. This doesn't make much sense, so don't
6238 allow it. */
6239 error ("'for' loop initial declaration used outside C99 mode");
6240 return;
6241 }
6242 /* C99 subclause 6.8.5 paragraph 3:
6243
6244 [#3] The declaration part of a for statement shall only
6245 declare identifiers for objects having storage class auto or
6246 register.
6247
6248 It isn't clear whether, in this sentence, "identifiers" binds to
6249 "shall only declare" or to "objects" - that is, whether all identifiers
6250 declared must be identifiers for objects, or whether the restriction
6251 only applies to those that are. (A question on this in comp.std.c
6252 in November 2000 received no answer.) We implement the strictest
6253 interpretation, to avoid creating an extension which later causes
6254 problems. */
6255
6256 for (t = current_scope->tags; t; t = TREE_CHAIN (t))
6257 {
6258 if (TREE_PURPOSE (t) != 0)
6259 {
6260 enum tree_code code = TREE_CODE (TREE_VALUE (t));
6261
6262 if (code == RECORD_TYPE)
6263 error ("'struct %s' declared in 'for' loop initial declaration",
6264 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6265 else if (code == UNION_TYPE)
6266 error ("'union %s' declared in 'for' loop initial declaration",
6267 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6268 else
6269 error ("'enum %s' declared in 'for' loop initial declaration",
6270 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6271 }
6272 }
6273
6274 for (t = getdecls (); t; t = TREE_CHAIN (t))
6275 {
6276 if (TREE_CODE (t) != VAR_DECL && DECL_NAME (t))
6277 error ("%Jdeclaration of non-variable '%D' in 'for' loop "
6278 "initial declaration", t, t);
6279 else if (TREE_STATIC (t))
6280 error ("%Jdeclaration of static variable '%D' in 'for' loop "
6281 "initial declaration", t, t);
6282 else if (DECL_EXTERNAL (t))
6283 error ("%Jdeclaration of 'extern' variable '%D' in 'for' loop "
6284 "initial declaration", t, t);
6285 }
6286}
6287
6288/* Save and reinitialize the variables
6289 used during compilation of a C function. */
6290
6291void
6292c_push_function_context (struct function *f)
6293{
6294 struct language_function *p;
6295 p = ggc_alloc (sizeof (struct language_function));
6296 f->language = p;
6297
6298 p->base.x_stmt_tree = c_stmt_tree;
6299 p->base.x_scope_stmt_stack = c_scope_stmt_stack;
6300 p->x_in_iteration_stmt = c_in_iteration_stmt;
6301 p->x_in_case_stmt = c_in_case_stmt;
6302 p->returns_value = current_function_returns_value;
6303 p->returns_null = current_function_returns_null;
6304 p->returns_abnormally = current_function_returns_abnormally;
6305 p->warn_about_return_type = warn_about_return_type;
6306 p->extern_inline = current_extern_inline;
6307}
6308
6309/* Restore the variables used during compilation of a C function. */
6310
6311void
6312c_pop_function_context (struct function *f)
6313{
6314 struct language_function *p = f->language;
6315
6316 if (DECL_SAVED_INSNS (current_function_decl) == 0
6317 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6318 {
6319 /* Stop pointing to the local nodes about to be freed. */
6320 /* But DECL_INITIAL must remain nonzero so we know this
6321 was an actual function definition. */
6322 DECL_INITIAL (current_function_decl) = error_mark_node;
6323 DECL_ARGUMENTS (current_function_decl) = 0;
6324 }
6325
6326 c_stmt_tree = p->base.x_stmt_tree;
6327 c_scope_stmt_stack = p->base.x_scope_stmt_stack;
6328 c_in_iteration_stmt = p->x_in_iteration_stmt;
6329 c_in_case_stmt = p->x_in_case_stmt;
6330 current_function_returns_value = p->returns_value;
6331 current_function_returns_null = p->returns_null;
6332 current_function_returns_abnormally = p->returns_abnormally;
6333 warn_about_return_type = p->warn_about_return_type;
6334 current_extern_inline = p->extern_inline;
6335
6336 f->language = NULL;
6337}
6338
6339/* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6340
6341void
6342c_dup_lang_specific_decl (tree decl)
6343{
6344 struct lang_decl *ld;
6345
6346 if (!DECL_LANG_SPECIFIC (decl))
6347 return;
6348
6349 ld = ggc_alloc (sizeof (struct lang_decl));
6350 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6351 DECL_LANG_SPECIFIC (decl) = ld;
6352}
6353
6354/* The functions below are required for functionality of doing
6355 function at once processing in the C front end. Currently these
6356 functions are not called from anywhere in the C front end, but as
6357 these changes continue, that will change. */
6358
6359/* Returns nonzero if the current statement is a full expression,
6360 i.e. temporaries created during that statement should be destroyed
6361 at the end of the statement. */
6362
6363int
6364stmts_are_full_exprs_p (void)
6365{
6366 return 0;
6367}
6368
6369/* Returns the stmt_tree (if any) to which statements are currently
6370 being added. If there is no active statement-tree, NULL is
6371 returned. */
6372
6373stmt_tree
6374current_stmt_tree (void)
6375{
6376 return &c_stmt_tree;
6377}
6378
6379/* Returns the stack of SCOPE_STMTs for the current function. */
6380
6381tree *
6382current_scope_stmt_stack (void)
6383{
6384 return &c_scope_stmt_stack;
6385}
6386
6387/* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6388 C. */
6389
6390int
6391anon_aggr_type_p (tree node ATTRIBUTE_UNUSED)
6392{
6393 return 0;
6394}
6395
6396/* Dummy function in place of callback used by C++. */
6397
6398void
6399extract_interface_info (void)
6400{
6401}
6402
6403/* Return a new COMPOUND_STMT, after adding it to the current
6404 statement tree. */
6405
6406tree
6407c_begin_compound_stmt (void)
6408{
6409 tree stmt;
6410
6411 /* Create the COMPOUND_STMT. */
6399 stmt = add_stmt (build_stmt (COMPOUND_STMT, NULL_TREE));
6412 stmt = add_stmt (build_stmt (COMPOUND_STMT, error_mark_node));
6400
6401 return stmt;
6402}
6403
6404/* Expand T (a DECL_STMT) if it declares an entity not handled by the
6405 common code. */
6406
6407void
6408c_expand_decl_stmt (tree t)
6409{
6410 tree decl = DECL_STMT_DECL (t);
6411
6412 /* Expand nested functions. */
6413 if (TREE_CODE (decl) == FUNCTION_DECL
6414 && DECL_CONTEXT (decl) == current_function_decl
6415 && DECL_SAVED_TREE (decl))
6416 c_expand_body_1 (decl, 1);
6417}
6418
6419/* Return the global value of T as a symbol. */
6420
6421tree
6422identifier_global_value (tree t)
6423{
6424 tree decl = IDENTIFIER_SYMBOL_VALUE (t);
6425 if (decl == 0 || DECL_FILE_SCOPE_P (decl))
6426 return decl;
6427
6428 /* Shadowed by something else; find the true global value. */
6429 for (decl = global_scope->names; decl; decl = TREE_CHAIN (decl))
6430 if (DECL_NAME (decl) == t)
6431 return decl;
6432
6433 /* Only local values for this decl. */
6434 return 0;
6435}
6436
6437/* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6438 otherwise the name is found in ridpointers from RID_INDEX. */
6439
6440void
6441record_builtin_type (enum rid rid_index, const char *name, tree type)
6442{
6443 tree id;
6444 if (name == 0)
6445 id = ridpointers[(int) rid_index];
6446 else
6447 id = get_identifier (name);
6448 pushdecl (build_decl (TYPE_DECL, id, type));
6449}
6450
6451/* Build the void_list_node (void_type_node having been created). */
6452tree
6453build_void_list_node (void)
6454{
6455 tree t = build_tree_list (NULL_TREE, void_type_node);
6456 return t;
6457}
6458
6459/* Return something to represent absolute declarators containing a *.
6460 TARGET is the absolute declarator that the * contains.
6461 TYPE_QUALS_ATTRS is a list of modifiers such as const or volatile
6462 to apply to the pointer type, represented as identifiers, possible mixed
6463 with attributes.
6464
6465 We return an INDIRECT_REF whose "contents" are TARGET (inside a TREE_LIST,
6466 if attributes are present) and whose type is the modifier list. */
6467
6468tree
6469make_pointer_declarator (tree type_quals_attrs, tree target)
6470{
6471 tree quals, attrs;
6472 tree itarget = target;
6473 split_specs_attrs (type_quals_attrs, &quals, &attrs);
6474 if (attrs != NULL_TREE)
6475 itarget = tree_cons (attrs, target, NULL_TREE);
6476 return build1 (INDIRECT_REF, quals, itarget);
6477}
6478
6479/* A wrapper around lhd_set_decl_assembler_name that gives static
6480 variables their C names if they are at file scope and only one
6481 translation unit is being compiled, for backwards compatibility
6482 with certain bizarre assembler hacks (like crtstuff.c). */
6483
6484void
6485c_static_assembler_name (tree decl)
6486{
6487 if (num_in_fnames == 1
6488 && !TREE_PUBLIC (decl) && DECL_CONTEXT (decl)
6489 && TREE_CODE (DECL_CONTEXT (decl)) == TRANSLATION_UNIT_DECL)
6490 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
6491 else
6492 lhd_set_decl_assembler_name (decl);
6493}
6494
6495/* Hash and equality functions for link_hash_table: key off
6496 DECL_ASSEMBLER_NAME. */
6497
6498static hashval_t
6499link_hash_hash (const void *x_p)
6500{
6501 tree x = (tree)x_p;
6502 return (hashval_t) (long)DECL_ASSEMBLER_NAME (x);
6503}
6504
6505static int
6506link_hash_eq (const void *x1_p, const void *x2_p)
6507{
6508 tree x1 = (tree)x1_p;
6509 tree x2 = (tree)x2_p;
6510 return DECL_ASSEMBLER_NAME (x1) == DECL_ASSEMBLER_NAME (x2);
6511}
6512
6513/* Propagate information between definitions and uses between multiple
6514 translation units in TU_LIST based on linkage rules. */
6515
6516void
6517merge_translation_unit_decls (void)
6518{
6519 const tree tu_list = current_file_decl;
6520 tree tu;
6521 tree decl;
6522 htab_t link_hash_table;
6523 tree block;
6524
6525 /* Create the BLOCK that poplevel would have created, but don't
6526 actually call poplevel since that's expensive. */
6527 block = make_node (BLOCK);
6528 BLOCK_VARS (block) = current_scope->names;
6529 TREE_USED (block) = 1;
6530 DECL_INITIAL (current_file_decl) = block;
6531
6532 /* If only one translation unit seen, no copying necessary. */
6533 if (TREE_CHAIN (tu_list) == NULL_TREE)
6534 return;
6535
6536 link_hash_table = htab_create (1021, link_hash_hash, link_hash_eq, NULL);
6537
6538 /* Enter any actual definitions into the hash table. */
6539 for (tu = tu_list; tu; tu = TREE_CHAIN (tu))
6540 for (decl = BLOCK_VARS (DECL_INITIAL (tu)); decl; decl = TREE_CHAIN (decl))
6541 if (TREE_PUBLIC (decl) && ! DECL_EXTERNAL (decl))
6542 {
6543 PTR *slot;
6544 slot = htab_find_slot (link_hash_table, decl, INSERT);
6545
6546 /* If we've already got a definition, work out which one is
6547 the real one, put it into the hash table, and make the
6548 other one DECL_EXTERNAL. This is important to avoid
6549 putting out two definitions of the same symbol in the
6550 assembly output. */
6551 if (*slot != NULL)
6552 {
6553 tree old_decl = (tree) *slot;
6554
6555 /* If this is weak or common or whatever, suppress it
6556 in favor of the other definition. */
6557 if (DECL_WEAK (decl))
6558 DECL_EXTERNAL (decl) = 1;
6559 else if (DECL_WEAK (old_decl) && ! DECL_WEAK (decl))
6560 DECL_EXTERNAL (old_decl) = 1;
6561 else if (DECL_COMMON (decl) || DECL_ONE_ONLY (decl))
6562 DECL_EXTERNAL (decl) = 1;
6563 else if (DECL_COMMON (old_decl) || DECL_ONE_ONLY (old_decl))
6564 DECL_EXTERNAL (old_decl) = 1;
6565
6566 if (DECL_EXTERNAL (decl))
6567 {
6568 DECL_INITIAL (decl) = NULL_TREE;
6569 DECL_COMMON (decl) = 0;
6570 DECL_ONE_ONLY (decl) = 0;
6571 DECL_WEAK (decl) = 0;
6572 }
6573 else if (DECL_EXTERNAL (old_decl))
6574 {
6575 DECL_INITIAL (old_decl) = NULL_TREE;
6576 DECL_COMMON (old_decl) = 0;
6577 DECL_ONE_ONLY (old_decl) = 0;
6578 DECL_WEAK (old_decl) = 0;
6579 *slot = decl;
6580 }
6581 else
6582 {
6583 error ("%Jredefinition of global '%D'", decl, decl);
6584 error ("%J'%D' previously defined here", old_decl, old_decl);
6585 }
6586 }
6587 else
6588 *slot = decl;
6589 }
6590
6591 /* Now insert the desired information from all the definitions
6592 into any plain declarations. */
6593 for (tu = tu_list; tu; tu = TREE_CHAIN (tu))
6594 for (decl = BLOCK_VARS (DECL_INITIAL (tu)); decl; decl = TREE_CHAIN (decl))
6595 if (TREE_PUBLIC (decl) && DECL_EXTERNAL (decl))
6596 {
6597 tree global_decl;
6598 global_decl = htab_find (link_hash_table, decl);
6599
6600 if (! global_decl)
6601 continue;
6602
6603 /* Print any appropriate error messages, and partially merge
6604 the decls. */
6605 (void) duplicate_decls (decl, global_decl);
6606 }
6607
6608 htab_delete (link_hash_table);
6609}
6610
6611/* Perform final processing on file-scope data. */
6612
6613void
6614c_write_global_declarations(void)
6615{
6616 tree link;
6617
6618 for (link = current_file_decl; link; link = TREE_CHAIN (link))
6619 {
6620 tree globals = BLOCK_VARS (DECL_INITIAL (link));
6621 int len = list_length (globals);
6622 tree *vec = xmalloc (sizeof (tree) * len);
6623 int i;
6624 tree decl;
6625
6626 /* Process the decls in the order they were written. */
6627
6628 for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
6629 vec[i] = decl;
6630
6631 wrapup_global_declarations (vec, len);
6632
6633 check_global_declarations (vec, len);
6634
6635 /* Clean up. */
6636 free (vec);
6637 }
6638}
6639
6640/* Reset the parser's state in preparation for a new file. */
6641
6642void
6643c_reset_state (void)
6644{
6645 tree link;
6646 tree file_scope_decl;
6647
6648 /* Pop the global scope. */
6649 if (current_scope != global_scope)
6650 current_scope = global_scope;
6651 file_scope_decl = current_file_decl;
6652 DECL_INITIAL (file_scope_decl) = poplevel (1, 0, 0);
6653 BLOCK_SUPERCONTEXT (DECL_INITIAL (file_scope_decl)) = file_scope_decl;
6654 truly_local_externals = NULL_TREE;
6655
6656 /* Start a new global binding level. */
6657 pushlevel (0);
6658 global_scope = current_scope;
6659 current_file_decl = build_decl (TRANSLATION_UNIT_DECL, NULL, NULL);
6660 TREE_CHAIN (current_file_decl) = file_scope_decl;
6661
6662 /* Reintroduce the builtin declarations. */
6663 for (link = first_builtin_decl;
6664 link != TREE_CHAIN (last_builtin_decl);
6665 link = TREE_CHAIN (link))
6666 pushdecl (copy_node (link));
6667}
6668
6669#include "gt-c-decl.h"
6413
6414 return stmt;
6415}
6416
6417/* Expand T (a DECL_STMT) if it declares an entity not handled by the
6418 common code. */
6419
6420void
6421c_expand_decl_stmt (tree t)
6422{
6423 tree decl = DECL_STMT_DECL (t);
6424
6425 /* Expand nested functions. */
6426 if (TREE_CODE (decl) == FUNCTION_DECL
6427 && DECL_CONTEXT (decl) == current_function_decl
6428 && DECL_SAVED_TREE (decl))
6429 c_expand_body_1 (decl, 1);
6430}
6431
6432/* Return the global value of T as a symbol. */
6433
6434tree
6435identifier_global_value (tree t)
6436{
6437 tree decl = IDENTIFIER_SYMBOL_VALUE (t);
6438 if (decl == 0 || DECL_FILE_SCOPE_P (decl))
6439 return decl;
6440
6441 /* Shadowed by something else; find the true global value. */
6442 for (decl = global_scope->names; decl; decl = TREE_CHAIN (decl))
6443 if (DECL_NAME (decl) == t)
6444 return decl;
6445
6446 /* Only local values for this decl. */
6447 return 0;
6448}
6449
6450/* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6451 otherwise the name is found in ridpointers from RID_INDEX. */
6452
6453void
6454record_builtin_type (enum rid rid_index, const char *name, tree type)
6455{
6456 tree id;
6457 if (name == 0)
6458 id = ridpointers[(int) rid_index];
6459 else
6460 id = get_identifier (name);
6461 pushdecl (build_decl (TYPE_DECL, id, type));
6462}
6463
6464/* Build the void_list_node (void_type_node having been created). */
6465tree
6466build_void_list_node (void)
6467{
6468 tree t = build_tree_list (NULL_TREE, void_type_node);
6469 return t;
6470}
6471
6472/* Return something to represent absolute declarators containing a *.
6473 TARGET is the absolute declarator that the * contains.
6474 TYPE_QUALS_ATTRS is a list of modifiers such as const or volatile
6475 to apply to the pointer type, represented as identifiers, possible mixed
6476 with attributes.
6477
6478 We return an INDIRECT_REF whose "contents" are TARGET (inside a TREE_LIST,
6479 if attributes are present) and whose type is the modifier list. */
6480
6481tree
6482make_pointer_declarator (tree type_quals_attrs, tree target)
6483{
6484 tree quals, attrs;
6485 tree itarget = target;
6486 split_specs_attrs (type_quals_attrs, &quals, &attrs);
6487 if (attrs != NULL_TREE)
6488 itarget = tree_cons (attrs, target, NULL_TREE);
6489 return build1 (INDIRECT_REF, quals, itarget);
6490}
6491
6492/* A wrapper around lhd_set_decl_assembler_name that gives static
6493 variables their C names if they are at file scope and only one
6494 translation unit is being compiled, for backwards compatibility
6495 with certain bizarre assembler hacks (like crtstuff.c). */
6496
6497void
6498c_static_assembler_name (tree decl)
6499{
6500 if (num_in_fnames == 1
6501 && !TREE_PUBLIC (decl) && DECL_CONTEXT (decl)
6502 && TREE_CODE (DECL_CONTEXT (decl)) == TRANSLATION_UNIT_DECL)
6503 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
6504 else
6505 lhd_set_decl_assembler_name (decl);
6506}
6507
6508/* Hash and equality functions for link_hash_table: key off
6509 DECL_ASSEMBLER_NAME. */
6510
6511static hashval_t
6512link_hash_hash (const void *x_p)
6513{
6514 tree x = (tree)x_p;
6515 return (hashval_t) (long)DECL_ASSEMBLER_NAME (x);
6516}
6517
6518static int
6519link_hash_eq (const void *x1_p, const void *x2_p)
6520{
6521 tree x1 = (tree)x1_p;
6522 tree x2 = (tree)x2_p;
6523 return DECL_ASSEMBLER_NAME (x1) == DECL_ASSEMBLER_NAME (x2);
6524}
6525
6526/* Propagate information between definitions and uses between multiple
6527 translation units in TU_LIST based on linkage rules. */
6528
6529void
6530merge_translation_unit_decls (void)
6531{
6532 const tree tu_list = current_file_decl;
6533 tree tu;
6534 tree decl;
6535 htab_t link_hash_table;
6536 tree block;
6537
6538 /* Create the BLOCK that poplevel would have created, but don't
6539 actually call poplevel since that's expensive. */
6540 block = make_node (BLOCK);
6541 BLOCK_VARS (block) = current_scope->names;
6542 TREE_USED (block) = 1;
6543 DECL_INITIAL (current_file_decl) = block;
6544
6545 /* If only one translation unit seen, no copying necessary. */
6546 if (TREE_CHAIN (tu_list) == NULL_TREE)
6547 return;
6548
6549 link_hash_table = htab_create (1021, link_hash_hash, link_hash_eq, NULL);
6550
6551 /* Enter any actual definitions into the hash table. */
6552 for (tu = tu_list; tu; tu = TREE_CHAIN (tu))
6553 for (decl = BLOCK_VARS (DECL_INITIAL (tu)); decl; decl = TREE_CHAIN (decl))
6554 if (TREE_PUBLIC (decl) && ! DECL_EXTERNAL (decl))
6555 {
6556 PTR *slot;
6557 slot = htab_find_slot (link_hash_table, decl, INSERT);
6558
6559 /* If we've already got a definition, work out which one is
6560 the real one, put it into the hash table, and make the
6561 other one DECL_EXTERNAL. This is important to avoid
6562 putting out two definitions of the same symbol in the
6563 assembly output. */
6564 if (*slot != NULL)
6565 {
6566 tree old_decl = (tree) *slot;
6567
6568 /* If this is weak or common or whatever, suppress it
6569 in favor of the other definition. */
6570 if (DECL_WEAK (decl))
6571 DECL_EXTERNAL (decl) = 1;
6572 else if (DECL_WEAK (old_decl) && ! DECL_WEAK (decl))
6573 DECL_EXTERNAL (old_decl) = 1;
6574 else if (DECL_COMMON (decl) || DECL_ONE_ONLY (decl))
6575 DECL_EXTERNAL (decl) = 1;
6576 else if (DECL_COMMON (old_decl) || DECL_ONE_ONLY (old_decl))
6577 DECL_EXTERNAL (old_decl) = 1;
6578
6579 if (DECL_EXTERNAL (decl))
6580 {
6581 DECL_INITIAL (decl) = NULL_TREE;
6582 DECL_COMMON (decl) = 0;
6583 DECL_ONE_ONLY (decl) = 0;
6584 DECL_WEAK (decl) = 0;
6585 }
6586 else if (DECL_EXTERNAL (old_decl))
6587 {
6588 DECL_INITIAL (old_decl) = NULL_TREE;
6589 DECL_COMMON (old_decl) = 0;
6590 DECL_ONE_ONLY (old_decl) = 0;
6591 DECL_WEAK (old_decl) = 0;
6592 *slot = decl;
6593 }
6594 else
6595 {
6596 error ("%Jredefinition of global '%D'", decl, decl);
6597 error ("%J'%D' previously defined here", old_decl, old_decl);
6598 }
6599 }
6600 else
6601 *slot = decl;
6602 }
6603
6604 /* Now insert the desired information from all the definitions
6605 into any plain declarations. */
6606 for (tu = tu_list; tu; tu = TREE_CHAIN (tu))
6607 for (decl = BLOCK_VARS (DECL_INITIAL (tu)); decl; decl = TREE_CHAIN (decl))
6608 if (TREE_PUBLIC (decl) && DECL_EXTERNAL (decl))
6609 {
6610 tree global_decl;
6611 global_decl = htab_find (link_hash_table, decl);
6612
6613 if (! global_decl)
6614 continue;
6615
6616 /* Print any appropriate error messages, and partially merge
6617 the decls. */
6618 (void) duplicate_decls (decl, global_decl);
6619 }
6620
6621 htab_delete (link_hash_table);
6622}
6623
6624/* Perform final processing on file-scope data. */
6625
6626void
6627c_write_global_declarations(void)
6628{
6629 tree link;
6630
6631 for (link = current_file_decl; link; link = TREE_CHAIN (link))
6632 {
6633 tree globals = BLOCK_VARS (DECL_INITIAL (link));
6634 int len = list_length (globals);
6635 tree *vec = xmalloc (sizeof (tree) * len);
6636 int i;
6637 tree decl;
6638
6639 /* Process the decls in the order they were written. */
6640
6641 for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
6642 vec[i] = decl;
6643
6644 wrapup_global_declarations (vec, len);
6645
6646 check_global_declarations (vec, len);
6647
6648 /* Clean up. */
6649 free (vec);
6650 }
6651}
6652
6653/* Reset the parser's state in preparation for a new file. */
6654
6655void
6656c_reset_state (void)
6657{
6658 tree link;
6659 tree file_scope_decl;
6660
6661 /* Pop the global scope. */
6662 if (current_scope != global_scope)
6663 current_scope = global_scope;
6664 file_scope_decl = current_file_decl;
6665 DECL_INITIAL (file_scope_decl) = poplevel (1, 0, 0);
6666 BLOCK_SUPERCONTEXT (DECL_INITIAL (file_scope_decl)) = file_scope_decl;
6667 truly_local_externals = NULL_TREE;
6668
6669 /* Start a new global binding level. */
6670 pushlevel (0);
6671 global_scope = current_scope;
6672 current_file_decl = build_decl (TRANSLATION_UNIT_DECL, NULL, NULL);
6673 TREE_CHAIN (current_file_decl) = file_scope_decl;
6674
6675 /* Reintroduce the builtin declarations. */
6676 for (link = first_builtin_decl;
6677 link != TREE_CHAIN (last_builtin_decl);
6678 link = TREE_CHAIN (link))
6679 pushdecl (copy_node (link));
6680}
6681
6682#include "gt-c-decl.h"