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