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