* configure.in: Add xtensa-*-* targets to test for dwarf2 debug_line.
[official-gcc.git] / gcc / c-decl.c
blob61fcae0111a9749b3d3e3c4e193f6c020001a4c8
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.
5 This file is part of GCC.
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.
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.
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. */
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. */
26 /* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
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"
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) */
68 /* Nonzero if we have seen an invalid cross reference
69 to a struct, union, or enum, but not yet printed the message. */
71 tree pending_invalid_xref;
72 /* File and line to appear in the eventual error message. */
73 location_t pending_invalid_xref_location;
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. */
81 static tree enum_next_value;
83 /* Nonzero means that there was overflow computing enum_next_value. */
85 static int enum_overflow;
87 /* Parsing a function declarator leaves a list of parameter names
88 or a chain of parameter decls here. */
90 static tree last_function_parms;
92 /* ... and a chain of structure and enum types declared in the
93 parmlist here. */
95 static tree last_function_parm_tags;
97 /* ... and a chain of all non-parameter declarations (such as
98 CONST_DECLs from enumerations) here. */
100 static tree last_function_parm_others;
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. */
106 static tree current_function_parms;
108 /* Similar, for last_function_parm_tags. */
110 static tree current_function_parm_tags;
112 /* And for last_function_parm_others. */
114 static tree current_function_parm_others;
116 /* Similar, for the file and line that the prototype came from if this is
117 an old-style definition. */
119 static location_t current_function_prototype_locus;
121 /* The current statement tree. */
123 static GTY(()) struct stmt_tree_s c_stmt_tree;
125 /* The current scope statement stack. */
127 static GTY(()) tree c_scope_stmt_stack;
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;
133 /* All the builtins; this is a subset of the entries of global_scope. */
135 static GTY(()) tree first_builtin_decl;
136 static GTY(()) tree last_builtin_decl;
138 /* A DECL for the current file-scope context. */
140 static GTY(()) tree current_file_decl;
142 /* Set to 0 at beginning of a function definition, set to 1 if
143 a return statement that specifies a return value is seen. */
145 int current_function_returns_value;
147 /* Set to 0 at beginning of a function definition, set to 1 if
148 a return statement with no argument is seen. */
150 int current_function_returns_null;
152 /* Set to 0 at beginning of a function definition, set to 1 if
153 a call to a noreturn function is seen. */
155 int current_function_returns_abnormally;
157 /* Set to nonzero by `grokdeclarator' for a function
158 whose return type is defaulted, if warnings for this are desired. */
160 static int warn_about_return_type;
162 /* Nonzero when starting a function declared `extern inline'. */
164 static int current_extern_inline;
166 /* Each c_scope structure describes the complete contents of one scope.
167 Three scopes are distinguished specially: the innermost or current
168 scope, the innermost function scope, and the outermost or file scope.
170 Most declarations are recorded in the current scope.
172 All normal label declarations are recorded in the innermost
173 function scope, as are bindings of undeclared identifiers to
174 error_mark_node. (GCC permits nested functions as an extension,
175 hence the 'innermost' qualifier.) Explicitly declared labels
176 (using the __label__ extension) appear in the current scope.
178 Being in the global scope (current_scope == global_scope) causes
179 special behavior in several places below. Also, under some
180 conditions the Objective-C front end records declarations in the
181 global scope even though that isn't the current scope.
183 The order of the names, parms, and blocks lists matters, and they
184 are frequently appended to. To avoid having to walk all the way to
185 the end of the list on each insertion, or reverse the lists later,
186 we maintain a pointer to the last list entry for each of the lists.
188 The order of the tags, shadowed, and shadowed_tags
189 lists does not matter, so we just prepend to these lists. */
191 struct c_scope GTY(())
193 /* The scope containing this one. */
194 struct c_scope *outer;
196 /* The next outermost function scope. */
197 struct c_scope *outer_function;
199 /* All variables, constants, functions, labels, and typedef names. */
200 tree names;
201 tree names_last;
203 /* All parameter declarations. Used only in the outermost scope of
204 a function. */
205 tree parms;
206 tree parms_last;
208 /* All structure, union, and enum type tags. */
209 tree tags;
211 /* For each scope, a list of shadowed outer-scope definitions
212 to be restored when this scope is popped.
213 Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
214 whose TREE_VALUE is its old definition (a kind of ..._DECL node). */
215 tree shadowed;
217 /* For each scope, a list of shadowed outer-scope tag definitions
218 to be restored when this scope is popped.
219 Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
220 whose TREE_VALUE is its old definition (a kind of ..._TYPE node). */
221 tree shadowed_tags;
223 /* For each scope (except the global one), a chain of BLOCK nodes
224 for all the scopes that were entered and exited one level down. */
225 tree blocks;
226 tree blocks_last;
228 /* True if we are currently filling this scope with parameter
229 declarations. */
230 bool parm_flag : 1;
232 /* True if we already complained about forward parameter decls
233 in this scope. This prevents double warnings on
234 foo (int a; int b; ...) */
235 bool warned_forward_parm_decls : 1;
237 /* True if this is the outermost block scope of a function body.
238 This scope contains the parameters, the local variables declared
239 in the outermost block, and all the labels (except those in
240 nested functions, or declared at block scope with __label__). */
241 bool function_body : 1;
243 /* True means make a BLOCK for this scope no matter what. */
244 bool keep : 1;
247 /* The scope currently in effect. */
249 static GTY(()) struct c_scope *current_scope;
251 /* A chain of c_scope structures awaiting reuse. */
253 static GTY((deletable (""))) struct c_scope *scope_freelist;
255 /* The innermost function scope. Ordinary (not explicitly declared)
256 labels, bindings to error_mark_node, and the lazily-created
257 bindings of __func__ and its friends get this scope. */
259 static GTY(()) struct c_scope *current_function_scope;
261 /* The outermost scope, corresponding to the C "file scope". This is
262 created when the compiler is started and exists through the entire run. */
264 static GTY(()) struct c_scope *global_scope;
266 /* Append VAR to LIST in scope SCOPE. */
267 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
268 struct c_scope *s_ = (scope); \
269 tree d_ = (decl); \
270 if (s_->list##_last) \
271 TREE_CHAIN (s_->list##_last) = d_; \
272 else \
273 s_->list = d_; \
274 s_->list##_last = d_; \
275 } while (0)
277 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
278 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
279 struct c_scope *t_ = (tscope); \
280 struct c_scope *f_ = (fscope); \
281 if (t_->to##_last) \
282 TREE_CHAIN (t_->to##_last) = f_->from; \
283 else \
284 t_->to = f_->from; \
285 t_->to##_last = f_->from##_last; \
286 } while (0)
288 /* True means unconditionally make a BLOCK for the next scope pushed. */
290 static bool keep_next_level_flag;
292 /* True means the next call to pushlevel will be the outermost scope
293 of a function body, so do not push a new scope, merely cease
294 expecting parameter decls. */
296 static bool next_is_function_body;
298 /* Functions called automatically at the beginning and end of execution. */
300 tree static_ctors, static_dtors;
302 /* Forward declarations. */
304 static struct c_scope *make_scope (void);
305 static void pop_scope (void);
306 static tree match_builtin_function_types (tree, tree);
307 static int duplicate_decls (tree, tree, int, int);
308 static int redeclaration_error_message (tree, tree);
309 static tree make_label (tree, location_t);
310 static void bind_label (tree, tree, struct c_scope *);
311 static void implicit_decl_warning (tree);
312 static tree lookup_tag (enum tree_code, tree, int);
313 static tree lookup_name_current_level (tree);
314 static tree grokdeclarator (tree, tree, enum decl_context, int);
315 static tree grokparms (tree, int);
316 static void layout_array_type (tree);
317 static void store_parm_decls_newstyle (void);
318 static void store_parm_decls_oldstyle (void);
319 static tree c_make_fname_decl (tree, int);
320 static void c_expand_body_1 (tree, int);
321 static tree any_external_decl (tree);
322 static void record_external_decl (tree);
323 static void warn_if_shadowing (tree, tree);
324 static void clone_underlying_type (tree);
325 static bool flexible_array_type_p (tree);
326 static hashval_t link_hash_hash (const void *);
327 static int link_hash_eq (const void *, const void *);
329 /* States indicating how grokdeclarator() should handle declspecs marked
330 with __attribute__((deprecated)). An object declared as
331 __attribute__((deprecated)) suppresses warnings of uses of other
332 deprecated items. */
334 enum deprecated_states {
335 DEPRECATED_NORMAL,
336 DEPRECATED_SUPPRESS
339 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
341 void
342 c_print_identifier (FILE *file, tree node, int indent)
344 print_node (file, "symbol", IDENTIFIER_SYMBOL_VALUE (node), indent + 4);
345 print_node (file, "tag", IDENTIFIER_TAG_VALUE (node), indent + 4);
346 print_node (file, "label", IDENTIFIER_LABEL_VALUE (node), indent + 4);
347 if (C_IS_RESERVED_WORD (node))
349 tree rid = ridpointers[C_RID_CODE (node)];
350 indent_to (file, indent + 4);
351 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
352 (void *) rid, IDENTIFIER_POINTER (rid));
356 /* Hook called at end of compilation to assume 1 elt
357 for a file-scope tentative array defn that wasn't complete before. */
359 void
360 c_finish_incomplete_decl (tree decl)
362 if (TREE_CODE (decl) == VAR_DECL)
364 tree type = TREE_TYPE (decl);
365 if (type != error_mark_node
366 && TREE_CODE (type) == ARRAY_TYPE
367 && ! DECL_EXTERNAL (decl)
368 && TYPE_DOMAIN (type) == 0)
370 warning ("%Jarray '%D' assumed to have one element", decl, decl);
372 complete_array_type (type, NULL_TREE, 1);
374 layout_decl (decl, 0);
379 /* Reuse or create a struct for this scope. */
381 static struct c_scope *
382 make_scope (void)
384 struct c_scope *result;
385 if (scope_freelist)
387 result = scope_freelist;
388 scope_freelist = result->outer;
390 else
391 result = ggc_alloc_cleared (sizeof (struct c_scope));
393 return result;
396 /* Remove the topmost scope from the stack and add it to the
397 free list, updating current_function_scope if necessary. */
399 static void
400 pop_scope (void)
402 struct c_scope *scope = current_scope;
404 current_scope = scope->outer;
405 if (scope->function_body)
406 current_function_scope = scope->outer_function;
408 memset (scope, 0, sizeof (struct c_scope));
409 scope->outer = scope_freelist;
410 scope_freelist = scope;
413 /* The Objective-C front-end often needs to determine the current scope. */
415 void *
416 get_current_scope (void)
418 return current_scope;
421 /* The following function is used only by Objective-C. It needs to live here
422 because it accesses the innards of c_scope. */
424 void
425 objc_mark_locals_volatile (void *enclosing_blk)
427 struct c_scope *scope;
429 for (scope = current_scope;
430 scope && scope != enclosing_blk;
431 scope = scope->outer)
433 tree decl;
435 for (decl = scope->names; decl; decl = TREE_CHAIN (decl))
437 DECL_REGISTER (decl) = 0;
438 TREE_THIS_VOLATILE (decl) = 1;
440 /* Do not climb up past the current function. */
441 if (scope->function_body)
442 break;
446 /* Nonzero if we are currently in the global scope. */
449 global_bindings_p (void)
451 return current_scope == global_scope;
454 void
455 keep_next_level (void)
457 keep_next_level_flag = true;
460 /* Identify this scope as currently being filled with parameters. */
462 void
463 declare_parm_level (void)
465 current_scope->parm_flag = true;
468 /* Nonzero if currently making parm declarations. */
471 in_parm_level_p (void)
473 return current_scope->parm_flag;
476 /* Enter a new scope. The dummy parameter is for signature
477 compatibility with lang_hooks.decls.pushlevel. */
479 void
480 pushlevel (int dummy ATTRIBUTE_UNUSED)
482 if (next_is_function_body)
484 /* This is the transition from the parameters to the top level
485 of the function body. These are the same scope
486 (C99 6.2.1p4,6) so we do not push another scope structure.
487 next_is_function_body is set only by store_parm_decls, which
488 in turn is called when and only when we are about to
489 encounter the opening curly brace for the function body.
491 The outermost block of a function always gets a BLOCK node,
492 because the debugging output routines expect that each
493 function has at least one BLOCK. */
494 current_scope->parm_flag = false;
495 current_scope->function_body = true;
496 current_scope->keep = true;
497 current_scope->outer_function = current_function_scope;
498 current_function_scope = current_scope;
500 keep_next_level_flag = false;
501 next_is_function_body = false;
503 else
505 struct c_scope *scope = make_scope ();
507 scope->keep = keep_next_level_flag;
508 scope->outer = current_scope;
509 current_scope = scope;
510 keep_next_level_flag = false;
514 /* Exit a scope. Restore the state of the identifier-decl mappings
515 that were in effect when this scope was entered.
517 If KEEP is KEEP_YES (1), this scope had explicit declarations, so
518 create a BLOCK node to record its declarations and subblocks for
519 debugging output. If KEEP is KEEP_MAYBE, do so only if the names
520 or tags lists are nonempty.
522 The second parameter is ignored; it is present only for
523 signature compatibility with lang_hooks.decls.poplevel.
525 If FUNCTIONBODY is nonzero, this level is the body of a function,
526 even if current_scope->function_body is not set. This is used
527 by language-independent code that generates synthetic functions,
528 and cannot set current_scope->function_body.
530 FIXME: Eliminate the need for all arguments. */
532 tree
533 poplevel (int keep, int dummy ATTRIBUTE_UNUSED, int functionbody)
535 struct c_scope *scope = current_scope;
536 tree block;
537 tree decl;
538 tree p;
540 /* The following line does not use |= due to a bug in HP's C compiler */
541 scope->function_body = scope->function_body | functionbody;
543 if (keep == KEEP_MAYBE)
544 keep = (scope->names || scope->tags);
546 keep |= scope->keep;
547 keep |= scope->function_body;
549 /* If appropriate, create a BLOCK to record the decls for the life
550 of this function. */
551 block = 0;
552 if (keep)
554 block = make_node (BLOCK);
555 BLOCK_VARS (block) = scope->names;
556 BLOCK_SUBBLOCKS (block) = scope->blocks;
557 TREE_USED (block) = 1;
560 /* In each subblock, record that this is its superior. */
561 for (p = scope->blocks; p; p = TREE_CHAIN (p))
562 BLOCK_SUPERCONTEXT (p) = block;
564 /* Clear out the variable bindings in this scope.
566 Propagate TREE_ADDRESSABLE from nested functions to their
567 containing functions.
569 Issue warnings for unused variables and labels, and errors for
570 undefined labels, if there are any. */
572 for (p = scope->names; p; p = TREE_CHAIN (p))
574 switch (TREE_CODE (p))
576 case LABEL_DECL:
577 if (TREE_USED (p) && !DECL_INITIAL (p))
579 error ("%Jlabel `%D' used but not defined", p, p);
580 DECL_INITIAL (p) = error_mark_node;
582 else if (!TREE_USED (p) && warn_unused_label)
584 if (DECL_INITIAL (p))
585 warning ("%Jlabel `%D' defined but not used", p, p);
586 else
587 warning ("%Jlabel `%D' declared but not defined", p, p);
590 IDENTIFIER_LABEL_VALUE (DECL_NAME (p)) = 0;
591 break;
593 case FUNCTION_DECL:
594 if (! TREE_ASM_WRITTEN (p)
595 && DECL_INITIAL (p) != 0
596 && TREE_ADDRESSABLE (p)
597 && DECL_ABSTRACT_ORIGIN (p) != 0
598 && DECL_ABSTRACT_ORIGIN (p) != p)
599 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
600 goto normal;
602 case VAR_DECL:
603 /* Keep this in sync with stmt.c:warn_about_unused_variables.
604 No warnings when the global scope is popped because the
605 global scope isn't popped for the last translation unit,
606 so the warnings are done in c_write_global_declaration. */
607 if (warn_unused_variable && scope != global_scope
608 && !TREE_USED (p)
609 && !DECL_IN_SYSTEM_HEADER (p)
610 && DECL_NAME (p)
611 && !DECL_ARTIFICIAL (p))
612 warning ("%Junused variable `%D'", p, p);
613 /* fall through */
615 default:
616 normal:
617 if (DECL_NAME (p))
619 if (DECL_EXTERNAL (p) && scope != global_scope)
620 /* External decls stay in the symbol-value slot but are
621 inaccessible. */
622 C_DECL_INVISIBLE (p) = 1;
623 else
624 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (p)) = 0;
626 break;
630 /* Clear out the parameter bindings in this scope, if any.
631 Unused-parameter warnings are handled by function.c. */
632 for (p = scope->parms; p; p = TREE_CHAIN (p))
633 if (DECL_NAME (p))
634 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (p)) = 0;
636 /* Clear out the tag-meanings declared in this scope.
638 Set the TYPE_CONTEXTs for all of the tagged types belonging to
639 this scope so that they point to the appropriate construct, i.e.
640 either to the current FUNCTION_DECL node, or else to the BLOCK
641 node we just constructed.
643 Note that for tagged types whose scope is just the formal
644 parameter list for some function type specification, we can't
645 properly set their TYPE_CONTEXTs here, because we don't have a
646 pointer to the appropriate FUNCTION_TYPE node readily available
647 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
648 type nodes get set in `grokdeclarator' as soon as we have created
649 the FUNCTION_TYPE node which will represent the "scope" for these
650 "parameter list local" tagged types. */
652 decl = scope->function_body ? current_function_decl : block;
653 for (p = scope->tags; p; p = TREE_CHAIN (p))
655 if (TREE_PURPOSE (p))
656 IDENTIFIER_TAG_VALUE (TREE_PURPOSE (p)) = 0;
657 if (decl)
658 TYPE_CONTEXT (TREE_VALUE (p)) = decl;
661 /* Restore all name- and label-meanings from outer scopes that were
662 shadowed by this scope. */
663 for (p = scope->shadowed; p; p = TREE_CHAIN (p))
664 if (TREE_VALUE (p) && TREE_CODE (TREE_VALUE (p)) == LABEL_DECL)
665 IDENTIFIER_LABEL_VALUE (TREE_PURPOSE (p)) = TREE_VALUE (p);
666 else
667 IDENTIFIER_SYMBOL_VALUE (TREE_PURPOSE (p)) = TREE_VALUE (p);
669 /* Restore all tag-meanings from outer scopes that were shadowed by
670 this scope. */
671 for (p = scope->shadowed_tags; p; p = TREE_CHAIN (p))
672 IDENTIFIER_TAG_VALUE (TREE_PURPOSE (p)) = TREE_VALUE (p);
674 /* Dispose of the block that we just made inside some higher level. */
675 if (scope->function_body)
676 DECL_INITIAL (current_function_decl) = block;
677 else if (scope->outer)
679 if (block)
680 SCOPE_LIST_APPEND (scope->outer, blocks, block);
681 /* If we did not make a block for the scope just exited, any
682 blocks made for inner scopes must be carried forward so they
683 will later become subblocks of something else. */
684 else if (scope->blocks)
685 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
688 /* Pop the current scope, and free the structure for reuse. */
689 pop_scope ();
691 return block;
694 /* Insert BLOCK at the end of the list of subblocks of the current
695 scope. This is used when a BIND_EXPR is expanded, to handle the
696 BLOCK node inside the BIND_EXPR. */
698 void
699 insert_block (tree block)
701 TREE_USED (block) = 1;
702 SCOPE_LIST_APPEND (current_scope, blocks, block);
705 /* Set the BLOCK node for the innermost scope (the one we are
706 currently in). The RTL expansion machinery requires us to provide
707 this hook, but it is not useful in function-at-a-time mode. */
709 void
710 set_block (tree block ATTRIBUTE_UNUSED)
714 /* Push a definition or a declaration of struct, union or enum tag "name".
715 "type" should be the type node.
716 We assume that the tag "name" is not already defined.
718 Note that the definition may really be just a forward reference.
719 In that case, the TYPE_SIZE will be zero. */
721 void
722 pushtag (tree name, tree type)
724 struct c_scope *b = current_scope;
726 /* Record the identifier as the type's name if it has none. */
727 if (name)
729 if (TYPE_NAME (type) == 0)
730 TYPE_NAME (type) = name;
732 if (IDENTIFIER_TAG_VALUE (name))
733 b->shadowed_tags = tree_cons (name, IDENTIFIER_TAG_VALUE (name),
734 b->shadowed_tags);
735 IDENTIFIER_TAG_VALUE (name) = type;
738 b->tags = tree_cons (name, type, b->tags);
740 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
741 tagged type we just added to the current scope. This fake
742 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
743 to output a representation of a tagged type, and it also gives
744 us a convenient place to record the "scope start" address for the
745 tagged type. */
747 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
749 /* An approximation for now, so we can tell this is a function-scope tag.
750 This will be updated in poplevel. */
751 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
754 /* Subroutine of duplicate_decls. Allow harmless mismatches in return
755 and argument types provided that the type modes match. This function
756 return a unified type given a suitable match, and 0 otherwise. */
758 static tree
759 match_builtin_function_types (tree oldtype, tree newtype)
761 tree newrettype, oldrettype;
762 tree newargs, oldargs;
763 tree trytype, tryargs;
765 /* Accept the return type of the new declaration if same modes. */
766 oldrettype = TREE_TYPE (oldtype);
767 newrettype = TREE_TYPE (newtype);
769 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
770 return 0;
772 oldargs = TYPE_ARG_TYPES (oldtype);
773 newargs = TYPE_ARG_TYPES (newtype);
774 tryargs = newargs;
776 while (oldargs || newargs)
778 if (! oldargs
779 || ! newargs
780 || ! TREE_VALUE (oldargs)
781 || ! TREE_VALUE (newargs)
782 || TYPE_MODE (TREE_VALUE (oldargs))
783 != TYPE_MODE (TREE_VALUE (newargs)))
784 return 0;
786 oldargs = TREE_CHAIN (oldargs);
787 newargs = TREE_CHAIN (newargs);
790 trytype = build_function_type (newrettype, tryargs);
791 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
794 /* Handle when a new declaration NEWDECL
795 has the same name as an old one OLDDECL
796 in the same binding contour.
797 Prints an error message if appropriate.
799 If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
800 Otherwise, return 0.
802 When DIFFERENT_BINDING_LEVEL is true, NEWDECL is an external declaration,
803 and OLDDECL is in an outer scope and should thus not be changed. */
805 static int
806 duplicate_decls (tree newdecl, tree olddecl, int different_binding_level,
807 int different_tu)
809 int types_match = comptypes (TREE_TYPE (newdecl), TREE_TYPE (olddecl),
810 COMPARE_STRICT);
811 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
812 && DECL_INITIAL (newdecl) != 0);
813 tree oldtype = TREE_TYPE (olddecl);
814 tree newtype = TREE_TYPE (newdecl);
815 int errmsg = 0;
817 if (DECL_P (olddecl))
819 if (TREE_CODE (newdecl) == FUNCTION_DECL
820 && TREE_CODE (olddecl) == FUNCTION_DECL
821 && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
823 if (DECL_DECLARED_INLINE_P (newdecl)
824 && DECL_UNINLINABLE (newdecl)
825 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
826 /* Already warned elsewhere. */;
827 else if (DECL_DECLARED_INLINE_P (olddecl)
828 && DECL_UNINLINABLE (olddecl)
829 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
830 /* Already warned. */;
831 else if (DECL_DECLARED_INLINE_P (newdecl)
832 && ! DECL_DECLARED_INLINE_P (olddecl)
833 && DECL_UNINLINABLE (olddecl)
834 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
836 warning ("%Jfunction '%D' redeclared as inline",
837 newdecl, newdecl);
838 warning ("%Jprevious declaration of function '%D' "
839 "with attribute noinline", olddecl, olddecl);
841 else if (DECL_DECLARED_INLINE_P (olddecl)
842 && DECL_UNINLINABLE (newdecl)
843 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
845 warning ("%Jfunction '%D' redeclared with attribute noinline",
846 newdecl, newdecl);
847 warning ("%Jprevious declaration of function '%D' was inline",
848 olddecl, olddecl);
852 DECL_ATTRIBUTES (newdecl)
853 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
856 if (TREE_CODE (newtype) == ERROR_MARK
857 || TREE_CODE (oldtype) == ERROR_MARK)
858 types_match = 0;
860 /* New decl is completely inconsistent with the old one =>
861 tell caller to replace the old one.
862 This is always an error except in the case of shadowing a builtin. */
863 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
865 if (TREE_CODE (olddecl) == FUNCTION_DECL
866 && DECL_BUILT_IN (olddecl))
868 /* If you declare a built-in or predefined function name as static,
869 the old definition is overridden,
870 but optionally warn this was a bad choice of name. */
871 if (!TREE_PUBLIC (newdecl))
873 if (warn_shadow)
874 warning ("%Jshadowing built-in function '%D'",
875 newdecl, newdecl);
877 else
878 warning ("%Jbuilt-in function '%D' declared as non-function",
879 newdecl, newdecl);
881 else
883 error ("%J'%D' redeclared as different kind of symbol",
884 newdecl, newdecl);
885 error ("%Jprevious declaration of '%D'", olddecl, olddecl);
888 return 0;
891 /* For real parm decl following a forward decl, return 1 so old decl
892 will be reused. Only allow this to happen once. */
893 if (types_match && TREE_CODE (newdecl) == PARM_DECL
894 && TREE_ASM_WRITTEN (olddecl) && ! TREE_ASM_WRITTEN (newdecl))
896 TREE_ASM_WRITTEN (olddecl) = 0;
897 return 1;
900 /* The new declaration is the same kind of object as the old one.
901 The declarations may partially match. Print warnings if they don't
902 match enough. Ultimately, copy most of the information from the new
903 decl to the old one, and keep using the old one. */
905 if (TREE_CODE (olddecl) == FUNCTION_DECL && DECL_BUILT_IN (olddecl))
907 /* A function declaration for a built-in function. */
908 if (!TREE_PUBLIC (newdecl))
910 /* If you declare a built-in function name as static, the
911 built-in definition is overridden,
912 but optionally warn this was a bad choice of name. */
913 if (warn_shadow)
914 warning ("%Jshadowing built-in function '%D'", newdecl, newdecl);
915 /* Discard the old built-in function. */
916 return 0;
918 if (!types_match)
920 /* Accept harmless mismatch in function types.
921 This is for the ffs and fprintf builtins. */
922 tree trytype = match_builtin_function_types (oldtype, newtype);
924 if (trytype)
926 types_match = comptypes (newtype, trytype, COMPARE_STRICT);
927 if (types_match)
928 oldtype = trytype;
929 if (! different_binding_level)
930 TREE_TYPE (olddecl) = oldtype;
933 if (!types_match)
935 /* If types don't match for a built-in, throw away the built-in. */
936 warning ("%Jconflicting types for built-in function '%D'",
937 newdecl, newdecl);
938 return 0;
941 else if (TREE_CODE (olddecl) == FUNCTION_DECL
942 && DECL_SOURCE_LINE (olddecl) == 0)
944 /* A function declaration for a predeclared function
945 that isn't actually built in. */
946 if (!TREE_PUBLIC (newdecl))
948 /* If you declare it as static, the
949 default definition is overridden. */
950 return 0;
952 else if (!types_match)
954 /* If the types don't match, preserve volatility indication.
955 Later on, we will discard everything else about the
956 default declaration. */
957 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
960 /* Permit char *foo () to match void *foo (...) if not pedantic,
961 if one of them came from a system header file. */
962 else if (!types_match
963 && TREE_CODE (olddecl) == FUNCTION_DECL
964 && TREE_CODE (newdecl) == FUNCTION_DECL
965 && TREE_CODE (TREE_TYPE (oldtype)) == POINTER_TYPE
966 && TREE_CODE (TREE_TYPE (newtype)) == POINTER_TYPE
967 && (DECL_IN_SYSTEM_HEADER (olddecl)
968 || DECL_IN_SYSTEM_HEADER (newdecl))
969 && ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (newtype))) == void_type_node
970 && TYPE_ARG_TYPES (oldtype) == 0
971 && self_promoting_args_p (TYPE_ARG_TYPES (newtype))
972 && TREE_TYPE (TREE_TYPE (oldtype)) == char_type_node)
974 (TREE_TYPE (TREE_TYPE (newtype)) == char_type_node
975 && TYPE_ARG_TYPES (newtype) == 0
976 && self_promoting_args_p (TYPE_ARG_TYPES (oldtype))
977 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (oldtype))) == void_type_node)))
979 if (pedantic)
980 pedwarn ("%Jconflicting types for '%D'", newdecl, newdecl);
981 /* Make sure we keep void * as ret type, not char *. */
982 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (oldtype))) == void_type_node)
983 TREE_TYPE (newdecl) = newtype = oldtype;
985 /* Set DECL_IN_SYSTEM_HEADER, so that if we see another declaration
986 we will come back here again. */
987 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
989 /* Permit void foo (...) to match int foo (...) if the latter is the
990 definition and implicit int was used. See c-torture/compile/920625-2.c. */
991 else if (!types_match && new_is_definition
992 && TREE_CODE (olddecl) == FUNCTION_DECL
993 && TREE_CODE (newdecl) == FUNCTION_DECL
994 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
995 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
996 && C_FUNCTION_IMPLICIT_INT (newdecl))
998 pedwarn ("%Jconflicting types for '%D'", newdecl, newdecl);
999 /* Make sure we keep void as the return type. */
1000 TREE_TYPE (newdecl) = newtype = oldtype;
1001 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1003 else if (!types_match
1004 /* Permit char *foo (int, ...); followed by char *foo ();
1005 if not pedantic. */
1006 && ! (TREE_CODE (olddecl) == FUNCTION_DECL
1007 && ! pedantic
1008 /* Return types must still match. */
1009 && comptypes (TREE_TYPE (oldtype),
1010 TREE_TYPE (newtype), COMPARE_STRICT)
1011 && TYPE_ARG_TYPES (newtype) == 0))
1013 error ("%Jconflicting types for '%D'", newdecl, newdecl);
1014 /* Check for function type mismatch
1015 involving an empty arglist vs a nonempty one. */
1016 if (TREE_CODE (olddecl) == FUNCTION_DECL
1017 && comptypes (TREE_TYPE (oldtype),
1018 TREE_TYPE (newtype), COMPARE_STRICT)
1019 && ((TYPE_ARG_TYPES (oldtype) == 0
1020 && DECL_INITIAL (olddecl) == 0)
1022 (TYPE_ARG_TYPES (newtype) == 0
1023 && DECL_INITIAL (newdecl) == 0)))
1025 /* Classify the problem further. */
1026 tree t = TYPE_ARG_TYPES (oldtype);
1027 if (t == 0)
1028 t = TYPE_ARG_TYPES (newtype);
1029 for (; t; t = TREE_CHAIN (t))
1031 tree type = TREE_VALUE (t);
1033 if (TREE_CHAIN (t) == 0
1034 && TYPE_MAIN_VARIANT (type) != void_type_node)
1036 error ("a parameter list with an ellipsis can't match an empty parameter name list declaration");
1037 break;
1040 if (c_type_promotes_to (type) != type)
1042 error ("an argument type that has a default promotion can't match an empty parameter name list declaration");
1043 break;
1047 if (C_DECL_IMPLICIT (olddecl))
1048 error ("%Jprevious implicit declaration of '%D'", olddecl, olddecl);
1049 else
1050 error ("%Jprevious declaration of '%D'", olddecl, olddecl);
1052 /* This is safer because the initializer might contain references
1053 to variables that were declared between olddecl and newdecl. This
1054 will make the initializer invalid for olddecl in case it gets
1055 assigned to olddecl below. */
1056 if (TREE_CODE (newdecl) == VAR_DECL)
1057 DECL_INITIAL (newdecl) = 0;
1059 /* TLS cannot follow non-TLS declaration. */
1060 else if (TREE_CODE (olddecl) == VAR_DECL && TREE_CODE (newdecl) == VAR_DECL
1061 && !DECL_THREAD_LOCAL (olddecl) && DECL_THREAD_LOCAL (newdecl))
1063 error ("%Jthread-local declaration of '%D' follows non thread-local "
1064 "declaration", newdecl, newdecl);
1065 error ("%Jprevious declaration of '%D'", olddecl, olddecl);
1067 /* non-TLS declaration cannot follow TLS declaration. */
1068 else if (TREE_CODE (olddecl) == VAR_DECL && TREE_CODE (newdecl) == VAR_DECL
1069 && DECL_THREAD_LOCAL (olddecl) && !DECL_THREAD_LOCAL (newdecl))
1071 error ("%Jnon thread-local declaration of '%D' follows "
1072 "thread-local declaration", newdecl, newdecl);
1073 error ("%Jprevious declaration of '%D'", olddecl, olddecl);
1075 else
1077 errmsg = redeclaration_error_message (newdecl, olddecl);
1078 if (errmsg)
1080 switch (errmsg)
1082 case 1:
1083 error ("%Jredefinition of '%D'", newdecl, newdecl);
1084 break;
1085 case 2:
1086 error ("%Jredeclaration of '%D'", newdecl, newdecl);
1087 break;
1088 case 3:
1089 error ("%Jconflicting declarations of '%D'", newdecl, newdecl);
1090 break;
1091 default:
1092 abort ();
1095 if (DECL_INITIAL (olddecl)
1096 && current_scope == global_scope)
1097 error ("%J'%D' previously defined here", olddecl, olddecl);
1098 else
1099 error ("%J'%D' previously declared here", olddecl, olddecl);
1100 return 0;
1102 else if (TREE_CODE (newdecl) == TYPE_DECL
1103 && (DECL_IN_SYSTEM_HEADER (olddecl)
1104 || DECL_IN_SYSTEM_HEADER (newdecl)))
1106 warning ("%Jredefinition of '%D'", newdecl, newdecl);
1107 if (DECL_INITIAL (olddecl) && current_scope == global_scope)
1108 warning ("%J'%D' previously defined here", olddecl, olddecl);
1109 else
1110 warning ("%J'%D' previously declared here", olddecl, olddecl);
1112 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1113 && DECL_INITIAL (olddecl) != 0
1114 && TYPE_ARG_TYPES (oldtype) == 0
1115 && TYPE_ARG_TYPES (newtype) != 0
1116 && TYPE_ACTUAL_ARG_TYPES (oldtype) != 0)
1118 tree type, parm;
1119 int nargs;
1120 /* Prototype decl follows defn w/o prototype. */
1122 for (parm = TYPE_ACTUAL_ARG_TYPES (oldtype),
1123 type = TYPE_ARG_TYPES (newtype),
1124 nargs = 1;
1126 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type), nargs++)
1128 if (TYPE_MAIN_VARIANT (TREE_VALUE (parm)) == void_type_node
1129 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
1131 warning ("%Jprototype for '%D' follows", newdecl, newdecl);
1132 warning ("%Jnon-prototype definition here", olddecl);
1133 break;
1135 if (TYPE_MAIN_VARIANT (TREE_VALUE (parm)) == void_type_node
1136 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
1138 error ("%Jprototype for '%D' follows and number of "
1139 "arguments doesn't match", newdecl, newdecl);
1140 error ("%Jnon-prototype definition here", olddecl);
1141 errmsg = 1;
1142 break;
1144 /* Type for passing arg must be consistent
1145 with that declared for the arg. */
1146 if (! comptypes (TREE_VALUE (parm), TREE_VALUE (type),
1147 COMPARE_STRICT))
1149 error ("%Jprototype for '%D' follows and argument %d "
1150 "doesn't match", newdecl, newdecl, nargs);
1151 error ("%Jnon-prototype definition here", olddecl);
1152 errmsg = 1;
1153 break;
1157 /* Warn about mismatches in various flags. */
1158 else
1160 /* Warn if function is now inline
1161 but was previously declared not inline and has been called. */
1162 if (TREE_CODE (olddecl) == FUNCTION_DECL
1163 && ! DECL_DECLARED_INLINE_P (olddecl)
1164 && DECL_DECLARED_INLINE_P (newdecl)
1165 && TREE_USED (olddecl))
1166 warning ("%J'%D' declared inline after being called",
1167 newdecl, newdecl);
1168 if (TREE_CODE (olddecl) == FUNCTION_DECL
1169 && ! DECL_DECLARED_INLINE_P (olddecl)
1170 && DECL_DECLARED_INLINE_P (newdecl)
1171 && DECL_INITIAL (olddecl) != 0)
1172 warning ("%J'%D' declared inline after its definition",
1173 newdecl, newdecl);
1175 /* If pedantic, warn when static declaration follows a non-static
1176 declaration. Otherwise, do so only for functions. */
1177 if ((pedantic || TREE_CODE (olddecl) == FUNCTION_DECL)
1178 && TREE_PUBLIC (olddecl)
1179 && !TREE_PUBLIC (newdecl))
1180 warning ("%Jstatic declaration for '%D' follows non-static",
1181 newdecl, newdecl);
1183 /* If warn_traditional, warn when a non-static function
1184 declaration follows a static one. */
1185 if (warn_traditional && !in_system_header
1186 && TREE_CODE (olddecl) == FUNCTION_DECL
1187 && !TREE_PUBLIC (olddecl)
1188 && TREE_PUBLIC (newdecl))
1189 warning ("%Jnon-static declaration for '%D' follows static",
1190 newdecl, newdecl);
1192 /* Warn when const declaration follows a non-const
1193 declaration, but not for functions. */
1194 if (TREE_CODE (olddecl) != FUNCTION_DECL
1195 && !TREE_READONLY (olddecl)
1196 && TREE_READONLY (newdecl))
1197 warning ("%Jconst declaration for '%D' follows non-const",
1198 newdecl, newdecl);
1199 /* These bits are logically part of the type, for variables.
1200 But not for functions
1201 (where qualifiers are not valid ANSI anyway). */
1202 else if (pedantic && TREE_CODE (olddecl) != FUNCTION_DECL
1203 && (TREE_READONLY (newdecl) != TREE_READONLY (olddecl)
1204 || TREE_THIS_VOLATILE (newdecl) != TREE_THIS_VOLATILE (olddecl)))
1205 pedwarn ("%Jtype qualifiers for '%D' conflict with previous "
1206 "declaration", newdecl, newdecl);
1210 /* Optionally warn about more than one declaration for the same name. */
1211 if (errmsg == 0 && warn_redundant_decls && DECL_SOURCE_LINE (olddecl) != 0
1212 /* Don't warn about a function declaration
1213 followed by a definition. */
1214 && !(TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl) != 0
1215 && DECL_INITIAL (olddecl) == 0)
1216 /* Don't warn about extern decl followed by (tentative) definition. */
1217 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl)))
1219 warning ("%Jredundant redeclaration of '%D' in same scope",
1220 newdecl, newdecl);
1221 warning ("%Jprevious declaration of '%D'", olddecl, olddecl);
1224 /* Copy all the DECL_... slots specified in the new decl
1225 except for any that we copy here from the old type.
1227 Past this point, we don't change OLDTYPE and NEWTYPE
1228 even if we change the types of NEWDECL and OLDDECL. */
1230 if (types_match)
1232 /* When copying info to olddecl, we store into write_olddecl
1233 instead. This allows us to avoid modifying olddecl when
1234 different_binding_level is true. */
1235 tree write_olddecl = different_binding_level ? newdecl : olddecl;
1237 /* Merge the data types specified in the two decls. */
1238 if (TREE_CODE (newdecl) != FUNCTION_DECL || !DECL_BUILT_IN (olddecl))
1240 if (different_binding_level)
1242 if (TYPE_ARG_TYPES (oldtype) != 0
1243 && TYPE_ARG_TYPES (newtype) == 0)
1244 TREE_TYPE (newdecl) = common_type (newtype, oldtype);
1245 else
1246 TREE_TYPE (newdecl)
1247 = build_type_attribute_variant
1248 (newtype,
1249 merge_attributes (TYPE_ATTRIBUTES (newtype),
1250 TYPE_ATTRIBUTES (oldtype)));
1252 else
1253 TREE_TYPE (newdecl)
1254 = TREE_TYPE (olddecl)
1255 = common_type (newtype, oldtype);
1258 /* Lay the type out, unless already done. */
1259 if (oldtype != TREE_TYPE (newdecl))
1261 if (TREE_TYPE (newdecl) != error_mark_node)
1262 layout_type (TREE_TYPE (newdecl));
1263 if (TREE_CODE (newdecl) != FUNCTION_DECL
1264 && TREE_CODE (newdecl) != TYPE_DECL
1265 && TREE_CODE (newdecl) != CONST_DECL)
1266 layout_decl (newdecl, 0);
1268 else
1270 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1271 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1272 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1273 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1274 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1275 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1277 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1278 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1282 /* Keep the old rtl since we can safely use it. */
1283 COPY_DECL_RTL (olddecl, newdecl);
1285 /* Merge the type qualifiers. */
1286 if (TREE_READONLY (newdecl))
1287 TREE_READONLY (write_olddecl) = 1;
1289 if (TREE_THIS_VOLATILE (newdecl))
1291 TREE_THIS_VOLATILE (write_olddecl) = 1;
1292 if (TREE_CODE (newdecl) == VAR_DECL
1293 /* If an automatic variable is re-declared in the same
1294 function scope, but the old declaration was not
1295 volatile, make_var_volatile() would crash because the
1296 variable would have been assigned to a pseudo, not a
1297 MEM. Since this duplicate declaration is invalid
1298 anyway, we just skip the call. */
1299 && errmsg == 0)
1300 make_var_volatile (newdecl);
1303 /* Keep source location of definition rather than declaration. */
1304 /* When called with different_binding_level set, keep the old
1305 information so that meaningful diagnostics can be given. */
1306 if (DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0
1307 && ! different_binding_level)
1308 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1310 /* Merge the unused-warning information. */
1311 if (DECL_IN_SYSTEM_HEADER (olddecl))
1312 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1313 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1314 DECL_IN_SYSTEM_HEADER (write_olddecl) = 1;
1316 /* Merge the initialization information. */
1317 /* When called with different_binding_level set, don't copy over
1318 DECL_INITIAL, so that we don't accidentally change function
1319 declarations into function definitions. */
1320 if (DECL_INITIAL (newdecl) == 0 && ! different_binding_level)
1321 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1323 /* Merge the section attribute.
1324 We want to issue an error if the sections conflict but that must be
1325 done later in decl_attributes since we are called before attributes
1326 are assigned. */
1327 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1328 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1330 /* Copy the assembler name.
1331 Currently, it can only be defined in the prototype. */
1332 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1334 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1336 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1337 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1338 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1339 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1340 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1341 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1342 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1343 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1344 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1347 /* If cannot merge, then use the new type and qualifiers,
1348 and don't preserve the old rtl. */
1349 else if (! different_binding_level)
1351 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1352 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
1353 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
1354 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
1357 /* Merge the storage class information. */
1358 merge_weak (newdecl, olddecl);
1360 /* For functions, static overrides non-static. */
1361 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1363 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1364 /* This is since we don't automatically
1365 copy the attributes of NEWDECL into OLDDECL. */
1366 /* No need to worry about different_binding_level here because
1367 then TREE_PUBLIC (newdecl) was true. */
1368 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1369 /* If this clears `static', clear it in the identifier too. */
1370 if (! TREE_PUBLIC (olddecl))
1371 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1373 if (DECL_EXTERNAL (newdecl))
1375 if (! different_binding_level || different_tu)
1377 /* Don't mess with these flags on local externs; they remain
1378 external even if there's a declaration at file scope which
1379 isn't. */
1380 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1381 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1383 /* An extern decl does not override previous storage class. */
1384 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1385 if (! DECL_EXTERNAL (newdecl))
1387 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1388 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1389 /* If we have two non-EXTERNAL file-scope decls that are
1390 the same, only one of them should be written out. */
1391 if (different_tu)
1392 TREE_ASM_WRITTEN (newdecl) = 1;
1395 else
1397 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1398 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1401 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1403 /* If we're redefining a function previously defined as extern
1404 inline, make sure we emit debug info for the inline before we
1405 throw it away, in case it was inlined into a function that hasn't
1406 been written out yet. */
1407 if (new_is_definition && DECL_INITIAL (olddecl))
1409 if (TREE_USED (olddecl)
1410 /* In unit-at-a-time mode we never inline re-defined extern
1411 inline functions. */
1412 && !flag_unit_at_a_time)
1413 (*debug_hooks->outlining_inline_function) (olddecl);
1415 /* The new defn must not be inline. */
1416 DECL_INLINE (newdecl) = 0;
1417 DECL_UNINLINABLE (newdecl) = 1;
1419 else
1421 /* If either decl says `inline', this fn is inline,
1422 unless its definition was passed already. */
1423 if (DECL_DECLARED_INLINE_P (newdecl)
1424 || DECL_DECLARED_INLINE_P (olddecl))
1425 DECL_DECLARED_INLINE_P (newdecl) = 1;
1427 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1428 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1431 if (DECL_BUILT_IN (olddecl))
1433 /* Get rid of any built-in function if new arg types don't match it
1434 or if we have a function definition. */
1435 if (! types_match || new_is_definition)
1437 if (! different_binding_level)
1439 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1440 DECL_BUILT_IN_CLASS (olddecl) = NOT_BUILT_IN;
1443 else
1445 /* If redeclaring a builtin function, and not a definition,
1446 it stays built in. */
1447 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1448 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1452 /* Also preserve various other info from the definition. */
1453 if (! new_is_definition)
1455 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1456 /* When called with different_binding_level set, don't copy over
1457 DECL_INITIAL, so that we don't accidentally change function
1458 declarations into function definitions. */
1459 if (! different_binding_level)
1460 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1461 DECL_SAVED_INSNS (newdecl) = DECL_SAVED_INSNS (olddecl);
1462 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1463 DECL_ESTIMATED_INSNS (newdecl) = DECL_ESTIMATED_INSNS (olddecl);
1464 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1466 /* Set DECL_INLINE on the declaration if we've got a body
1467 from which to instantiate. */
1468 if (DECL_INLINE (olddecl) && ! DECL_UNINLINABLE (newdecl))
1470 DECL_INLINE (newdecl) = 1;
1471 DECL_ABSTRACT_ORIGIN (newdecl)
1472 = (different_binding_level
1473 ? DECL_ORIGIN (olddecl)
1474 : DECL_ABSTRACT_ORIGIN (olddecl));
1477 else
1479 /* If a previous declaration said inline, mark the
1480 definition as inlinable. */
1481 if (DECL_DECLARED_INLINE_P (newdecl)
1482 && ! DECL_UNINLINABLE (newdecl))
1483 DECL_INLINE (newdecl) = 1;
1486 if (different_binding_level)
1487 return 0;
1489 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1490 But preserve OLDDECL's DECL_UID. */
1492 unsigned olddecl_uid = DECL_UID (olddecl);
1494 memcpy ((char *) olddecl + sizeof (struct tree_common),
1495 (char *) newdecl + sizeof (struct tree_common),
1496 sizeof (struct tree_decl) - sizeof (struct tree_common));
1497 DECL_UID (olddecl) = olddecl_uid;
1500 /* NEWDECL contains the merged attribute lists.
1501 Update OLDDECL to be the same. */
1502 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
1504 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1505 so that encode_section_info has a chance to look at the new decl
1506 flags and attributes. */
1507 if (DECL_RTL_SET_P (olddecl)
1508 && (TREE_CODE (olddecl) == FUNCTION_DECL
1509 || (TREE_CODE (olddecl) == VAR_DECL
1510 && TREE_STATIC (olddecl))))
1511 make_decl_rtl (olddecl, NULL);
1513 return 1;
1516 /* Return any external DECL associated with ID, whether or not it is
1517 currently in scope. */
1519 static tree
1520 any_external_decl (tree id)
1522 tree decl = IDENTIFIER_SYMBOL_VALUE (id);
1523 tree t;
1525 if (decl == 0 || TREE_CODE (decl) == ERROR_MARK)
1526 return 0;
1527 else if (TREE_CODE (decl) != TYPE_DECL && DECL_EXTERNAL (decl))
1528 return decl;
1530 t = purpose_member (id, truly_local_externals);
1531 if (t)
1532 return TREE_VALUE (t);
1534 return 0;
1537 /* Record an external decl DECL. This only does something if a
1538 shadowing decl already exists. */
1539 static void
1540 record_external_decl (tree decl)
1542 tree name = DECL_NAME (decl);
1543 if (!IDENTIFIER_SYMBOL_VALUE (name))
1544 return;
1546 truly_local_externals = tree_cons (name, decl, truly_local_externals);
1549 /* Check whether decl-node X shadows an existing declaration.
1550 OLD is the old IDENTIFIER_SYMBOL_VALUE of the DECL_NAME of X,
1551 which might be a NULL_TREE. */
1552 static void
1553 warn_if_shadowing (tree x, tree old)
1555 const char *name;
1557 /* Nothing to shadow? */
1558 if (old == 0
1559 /* Shadow warnings not wanted? */
1560 || !warn_shadow
1561 /* No shadow warnings for internally generated vars. */
1562 || DECL_SOURCE_LINE (x) == 0
1563 /* No shadow warnings for vars made for inlining. */
1564 || DECL_FROM_INLINE (x)
1565 /* Don't warn about the parm names in function declarator
1566 within a function declarator.
1567 It would be nice to avoid warning in any function
1568 declarator in a declaration, as opposed to a definition,
1569 but there is no way to tell it's not a definition. */
1570 || (TREE_CODE (x) == PARM_DECL && current_scope->outer->parm_flag))
1571 return;
1573 name = IDENTIFIER_POINTER (DECL_NAME (x));
1574 if (TREE_CODE (old) == PARM_DECL)
1575 shadow_warning (SW_PARAM, name, old);
1576 else if (DECL_FILE_SCOPE_P (old))
1577 shadow_warning (SW_GLOBAL, name, old);
1578 else
1579 shadow_warning (SW_LOCAL, name, old);
1583 /* Subroutine of pushdecl.
1585 X is a TYPE_DECL for a typedef statement. Create a brand new
1586 ..._TYPE node (which will be just a variant of the existing
1587 ..._TYPE node with identical properties) and then install X
1588 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1590 The whole point here is to end up with a situation where each
1591 and every ..._TYPE node the compiler creates will be uniquely
1592 associated with AT MOST one node representing a typedef name.
1593 This way, even though the compiler substitutes corresponding
1594 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1595 early on, later parts of the compiler can always do the reverse
1596 translation and get back the corresponding typedef name. For
1597 example, given:
1599 typedef struct S MY_TYPE;
1600 MY_TYPE object;
1602 Later parts of the compiler might only know that `object' was of
1603 type `struct S' if it were not for code just below. With this
1604 code however, later parts of the compiler see something like:
1606 struct S' == struct S
1607 typedef struct S' MY_TYPE;
1608 struct S' object;
1610 And they can then deduce (from the node for type struct S') that
1611 the original object declaration was:
1613 MY_TYPE object;
1615 Being able to do this is important for proper support of protoize,
1616 and also for generating precise symbolic debugging information
1617 which takes full account of the programmer's (typedef) vocabulary.
1619 Obviously, we don't want to generate a duplicate ..._TYPE node if
1620 the TYPE_DECL node that we are now processing really represents a
1621 standard built-in type.
1623 Since all standard types are effectively declared at line zero
1624 in the source file, we can easily check to see if we are working
1625 on a standard type by checking the current value of lineno. */
1627 static void
1628 clone_underlying_type (tree x)
1630 if (DECL_SOURCE_LINE (x) == 0)
1632 if (TYPE_NAME (TREE_TYPE (x)) == 0)
1633 TYPE_NAME (TREE_TYPE (x)) = x;
1635 else if (TREE_TYPE (x) != error_mark_node
1636 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
1638 tree tt = TREE_TYPE (x);
1639 DECL_ORIGINAL_TYPE (x) = tt;
1640 tt = build_type_copy (tt);
1641 TYPE_NAME (tt) = x;
1642 TREE_USED (tt) = TREE_USED (x);
1643 TREE_TYPE (x) = tt;
1647 /* Record a decl-node X as belonging to the current lexical scope.
1648 Check for errors (such as an incompatible declaration for the same
1649 name already seen in the same scope).
1651 Returns either X or an old decl for the same name.
1652 If an old decl is returned, it may have been smashed
1653 to agree with what X says. */
1655 tree
1656 pushdecl (tree x)
1658 tree name = DECL_NAME (x);
1659 struct c_scope *scope = current_scope;
1661 #ifdef ENABLE_CHECKING
1662 if (error_mark_node == 0)
1663 /* Called too early. */
1664 abort ();
1665 #endif
1667 /* Functions need the lang_decl data. */
1668 if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_LANG_SPECIFIC (x))
1669 DECL_LANG_SPECIFIC (x) = ggc_alloc_cleared (sizeof (struct lang_decl));
1671 /* A local extern declaration for a function doesn't constitute nesting.
1672 A local auto declaration does, since it's a forward decl
1673 for a nested function coming later. */
1674 if (current_function_decl == NULL
1675 || ((TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
1676 && DECL_INITIAL (x) == 0 && DECL_EXTERNAL (x)))
1677 DECL_CONTEXT (x) = current_file_decl;
1678 else
1679 DECL_CONTEXT (x) = current_function_decl;
1681 if (name)
1683 tree old;
1685 if (warn_nested_externs
1686 && scope != global_scope
1687 && DECL_EXTERNAL (x)
1688 && !DECL_IN_SYSTEM_HEADER (x))
1689 warning ("nested extern declaration of `%s'",
1690 IDENTIFIER_POINTER (name));
1692 old = lookup_name_current_level (name);
1693 if (old && duplicate_decls (x, old, 0, false))
1695 /* For PARM_DECLs, old may be a forward declaration.
1696 If so, we want to remove it from its old location
1697 (in the variables chain) and rechain it in the
1698 location given by the new declaration. */
1699 if (TREE_CODE (x) == PARM_DECL)
1701 tree *p;
1702 for (p = &scope->names; *p; p = &TREE_CHAIN (*p))
1703 if (*p == old)
1705 *p = TREE_CHAIN (old);
1706 SCOPE_LIST_APPEND (scope, parms, old);
1707 break;
1710 return old;
1712 if (DECL_EXTERNAL (x) || scope == global_scope)
1714 /* Find and check against a previous, not-in-scope, external
1715 decl for this identifier. (C99 6.2.7p2: All declarations
1716 that refer to the same object or function shall have
1717 compatible type; otherwise, the behavior is undefined.) */
1718 tree ext = any_external_decl (name);
1719 if (ext)
1721 if (duplicate_decls (x, ext, scope != global_scope,
1722 false))
1723 x = copy_node (ext);
1725 else
1726 record_external_decl (x);
1729 if (TREE_CODE (x) == TYPE_DECL)
1730 clone_underlying_type (x);
1732 /* If storing a local value, there may already be one
1733 (inherited). If so, record it for restoration when this
1734 scope ends. Take care not to do this if we are replacing an
1735 older decl in the same scope (i.e. duplicate_decls returned
1736 false, above). */
1737 if (scope != global_scope
1738 && IDENTIFIER_SYMBOL_VALUE (name)
1739 && IDENTIFIER_SYMBOL_VALUE (name) != old)
1741 warn_if_shadowing (x, IDENTIFIER_SYMBOL_VALUE (name));
1742 scope->shadowed = tree_cons (name, IDENTIFIER_SYMBOL_VALUE (name),
1743 scope->shadowed);
1746 /* Install the new declaration in the requested scope. */
1747 IDENTIFIER_SYMBOL_VALUE (name) = x;
1748 C_DECL_INVISIBLE (x) = 0;
1750 /* If x's type is incomplete because it's based on a
1751 structure or union which has not yet been fully declared,
1752 attach it to that structure or union type, so we can go
1753 back and complete the variable declaration later, if the
1754 structure or union gets fully declared.
1756 If the input is erroneous, we can have error_mark in the type
1757 slot (e.g. "f(void a, ...)") - that doesn't count as an
1758 incomplete type. */
1759 if (TREE_TYPE (x) != error_mark_node
1760 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
1762 tree element = TREE_TYPE (x);
1764 while (TREE_CODE (element) == ARRAY_TYPE)
1765 element = TREE_TYPE (element);
1766 element = TYPE_MAIN_VARIANT (element);
1768 if ((TREE_CODE (element) == RECORD_TYPE
1769 || TREE_CODE (element) == UNION_TYPE)
1770 && (TREE_CODE (x) != TYPE_DECL
1771 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
1772 && !COMPLETE_TYPE_P (element))
1773 C_TYPE_INCOMPLETE_VARS (element)
1774 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
1778 if (TREE_CODE (x) == PARM_DECL)
1779 SCOPE_LIST_APPEND (scope, parms, x);
1780 else
1781 SCOPE_LIST_APPEND (scope, names, x);
1783 return x;
1786 /* Record X as belonging to the global scope (C99 "file scope").
1787 This is used only internally by the Objective-C front end,
1788 and is limited to its needs. duplicate_decls is not called;
1789 if there is any preexisting decl for this identifier, it is an ICE. */
1791 tree
1792 pushdecl_top_level (tree x)
1794 tree name;
1796 if (TREE_CODE (x) != VAR_DECL)
1797 abort ();
1799 name = DECL_NAME (x);
1801 if (IDENTIFIER_SYMBOL_VALUE (name))
1802 abort ();
1804 DECL_CONTEXT (x) = current_file_decl;
1805 IDENTIFIER_SYMBOL_VALUE (name) = x;
1807 SCOPE_LIST_APPEND (global_scope, names, x);
1808 return x;
1811 /* Generate an implicit declaration for identifier FUNCTIONID as a
1812 function of type int (). */
1814 tree
1815 implicitly_declare (tree functionid)
1817 tree decl = any_external_decl (functionid);
1819 if (decl)
1821 /* Implicit declaration of a function already declared
1822 (somehow) in a different scope, or as a built-in.
1823 If this is the first time this has happened, warn;
1824 then recycle the old declaration. */
1825 if (!C_DECL_IMPLICIT (decl))
1827 implicit_decl_warning (DECL_NAME (decl));
1828 if (! DECL_FILE_SCOPE_P (decl))
1829 warning ("%Jprevious declaration of '%D'", decl, decl);
1830 C_DECL_IMPLICIT (decl) = 1;
1832 /* If this function is global, then it must already be in the
1833 global scope, so there's no need to push it again. */
1834 if (current_scope == global_scope)
1835 return decl;
1836 /* If this is a local declaration, make a copy; we can't have
1837 the same DECL listed in two different scopes. */
1838 return pushdecl (copy_node (decl));
1841 /* Not seen before. */
1842 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
1843 DECL_EXTERNAL (decl) = 1;
1844 TREE_PUBLIC (decl) = 1;
1845 C_DECL_IMPLICIT (decl) = 1;
1846 implicit_decl_warning (functionid);
1848 /* C89 says implicit declarations are in the innermost block.
1849 So we record the decl in the standard fashion. */
1850 decl = pushdecl (decl);
1852 /* No need to call objc_check_decl here - it's a function type. */
1853 rest_of_decl_compilation (decl, NULL, 0, 0);
1855 /* Write a record describing this implicit function declaration
1856 to the prototypes file (if requested). */
1857 gen_aux_info_record (decl, 0, 1, 0);
1859 /* Possibly apply some default attributes to this implicit declaration. */
1860 decl_attributes (&decl, NULL_TREE, 0);
1862 return decl;
1865 static void
1866 implicit_decl_warning (tree id)
1868 const char *name = IDENTIFIER_POINTER (id);
1869 if (mesg_implicit_function_declaration == 2)
1870 error ("implicit declaration of function `%s'", name);
1871 else if (mesg_implicit_function_declaration == 1)
1872 warning ("implicit declaration of function `%s'", name);
1875 /* Return zero if the declaration NEWDECL is valid
1876 when the declaration OLDDECL (assumed to be for the same name)
1877 has already been seen.
1878 Otherwise return 1 if NEWDECL is a redefinition, 2 if it is a redeclaration,
1879 and 3 if it is a conflicting declaration. */
1881 static int
1882 redeclaration_error_message (tree newdecl, tree olddecl)
1884 if (TREE_CODE (newdecl) == TYPE_DECL)
1886 /* Do not complain about type redeclarations where at least one
1887 declaration was in a system header. */
1888 if (DECL_IN_SYSTEM_HEADER (olddecl) || DECL_IN_SYSTEM_HEADER (newdecl))
1889 return 0;
1890 return 1;
1892 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1894 /* Declarations of functions can insist on internal linkage
1895 but they can't be inconsistent with internal linkage,
1896 so there can be no error on that account.
1897 However defining the same name twice is no good. */
1898 if (DECL_INITIAL (olddecl) != 0 && DECL_INITIAL (newdecl) != 0
1899 /* However, defining once as extern inline and a second
1900 time in another way is ok. */
1901 && ! (DECL_DECLARED_INLINE_P (olddecl) && DECL_EXTERNAL (olddecl)
1902 && ! (DECL_DECLARED_INLINE_P (newdecl)
1903 && DECL_EXTERNAL (newdecl))))
1904 return 1;
1905 return 0;
1907 else if (DECL_FILE_SCOPE_P (newdecl))
1909 /* Objects declared at file scope: */
1910 /* If at least one is a reference, it's ok. */
1911 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
1912 return 0;
1913 /* Reject two definitions. */
1914 if (DECL_INITIAL (olddecl) != 0 && DECL_INITIAL (newdecl) != 0)
1915 return 1;
1916 /* Now we have two tentative defs, or one tentative and one real def. */
1917 /* Insist that the linkage match. */
1918 if (TREE_PUBLIC (olddecl) != TREE_PUBLIC (newdecl))
1919 return 3;
1920 return 0;
1922 else if (current_scope->parm_flag
1923 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1924 return 0;
1925 else
1927 /* Newdecl has block scope. If olddecl has block scope also, then
1928 reject two definitions, and reject a definition together with an
1929 external reference. Otherwise, it is OK, because newdecl must
1930 be an extern reference to olddecl. */
1931 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl))
1932 && DECL_CONTEXT (newdecl) == DECL_CONTEXT (olddecl))
1933 return 2;
1934 return 0;
1938 /* Issue an error message for a reference to an undeclared variable
1939 ID, including a reference to a builtin outside of function-call
1940 context. Establish a binding of the identifier to error_mark_node
1941 in an appropriate scope, which will suppress further errors for the
1942 same identifier. */
1943 void
1944 undeclared_variable (tree id)
1946 static bool already = false;
1947 struct c_scope *scope;
1949 if (current_function_decl == 0)
1951 error ("`%s' undeclared here (not in a function)",
1952 IDENTIFIER_POINTER (id));
1953 scope = current_scope;
1955 else
1957 error ("`%s' undeclared (first use in this function)",
1958 IDENTIFIER_POINTER (id));
1960 if (! already)
1962 error ("(Each undeclared identifier is reported only once");
1963 error ("for each function it appears in.)");
1964 already = true;
1967 scope = current_function_scope;
1970 scope->shadowed = tree_cons (id, IDENTIFIER_SYMBOL_VALUE (id),
1971 scope->shadowed);
1972 IDENTIFIER_SYMBOL_VALUE (id) = error_mark_node;
1975 /* Subroutine of lookup_label, declare_label, define_label: construct a
1976 LABEL_DECL with all the proper frills. */
1978 static tree
1979 make_label (tree name, location_t location)
1981 tree label = build_decl (LABEL_DECL, name, void_type_node);
1983 DECL_CONTEXT (label) = current_function_decl;
1984 DECL_MODE (label) = VOIDmode;
1985 DECL_SOURCE_LOCATION (label) = location;
1987 return label;
1990 /* Another subroutine of lookup_label, declare_label, define_label:
1991 set up the binding of name to LABEL_DECL in the given SCOPE. */
1993 static void
1994 bind_label (tree name, tree label, struct c_scope *scope)
1996 if (IDENTIFIER_LABEL_VALUE (name))
1997 scope->shadowed = tree_cons (name, IDENTIFIER_LABEL_VALUE (name),
1998 scope->shadowed);
1999 IDENTIFIER_LABEL_VALUE (name) = label;
2001 SCOPE_LIST_APPEND (scope, names, label);
2004 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2005 Create one if none exists so far for the current function.
2006 This is called when a label is used in a goto expression or
2007 has its address taken. */
2009 tree
2010 lookup_label (tree name)
2012 tree label;
2014 if (current_function_decl == 0)
2016 error ("label %s referenced outside of any function",
2017 IDENTIFIER_POINTER (name));
2018 return 0;
2021 /* Use a label already defined or ref'd with this name, but not if
2022 it is inherited from a containing function and wasn't declared
2023 using __label__. */
2024 label = IDENTIFIER_LABEL_VALUE (name);
2025 if (label && (DECL_CONTEXT (label) == current_function_decl
2026 || C_DECLARED_LABEL_FLAG (label)))
2028 /* If the label has only been declared, update its apparent
2029 location to point here, for better diagnostics if it
2030 turns out not to have been defined. */
2031 if (!TREE_USED (label))
2032 DECL_SOURCE_LOCATION (label) = input_location;
2033 return label;
2036 /* No label binding for that identifier; make one. */
2037 label = make_label (name, input_location);
2039 /* Ordinary labels go in the current function scope. */
2040 bind_label (name, label, current_function_scope);
2041 return label;
2044 /* Make a label named NAME in the current function, shadowing silently
2045 any that may be inherited from containing functions or containing
2046 scopes. This is called for __label__ declarations. */
2048 /* Note that valid use, if the label being shadowed comes from another
2049 scope in the same function, requires calling declare_nonlocal_label
2050 right away. (Is this still true? -zw 2003-07-17) */
2052 tree
2053 declare_label (tree name)
2055 tree label = IDENTIFIER_LABEL_VALUE (name);
2056 tree dup;
2058 /* Check to make sure that the label hasn't already been declared
2059 at this scope */
2060 for (dup = current_scope->names; dup; dup = TREE_CHAIN (dup))
2061 if (dup == label)
2063 error ("duplicate label declaration `%s'", IDENTIFIER_POINTER (name));
2064 error ("%Jthis is a previous declaration", dup);
2066 /* Just use the previous declaration. */
2067 return dup;
2070 label = make_label (name, input_location);
2071 C_DECLARED_LABEL_FLAG (label) = 1;
2073 /* Declared labels go in the current scope. */
2074 bind_label (name, label, current_scope);
2075 return label;
2078 /* Define a label, specifying the location in the source file.
2079 Return the LABEL_DECL node for the label, if the definition is valid.
2080 Otherwise return 0. */
2082 tree
2083 define_label (location_t location, tree name)
2085 tree label;
2087 /* Find any preexisting label with this name. It is an error
2088 if that label has already been defined in this function, or
2089 if there is a containing function with a declared label with
2090 the same name. */
2091 label = IDENTIFIER_LABEL_VALUE (name);
2093 if (label
2094 && ((DECL_CONTEXT (label) == current_function_decl
2095 && DECL_INITIAL (label) != 0)
2096 || (DECL_CONTEXT (label) != current_function_decl
2097 && C_DECLARED_LABEL_FLAG (label))))
2099 error ("%Hduplicate label `%D'", &location, label);
2100 if (DECL_INITIAL (label))
2101 error ("%J`%D' previously defined here", label, label);
2102 else
2103 error ("%J`%D' previously declared here", label, label);
2104 return 0;
2106 else if (label && DECL_CONTEXT (label) == current_function_decl)
2108 /* The label has been used or declared already in this function,
2109 but not defined. Update its location to point to this
2110 definition. */
2111 DECL_SOURCE_LOCATION (label) = location;
2113 else
2115 /* No label binding for that identifier; make one. */
2116 label = make_label (name, location);
2118 /* Ordinary labels go in the current function scope. */
2119 bind_label (name, label, current_function_scope);
2122 if (warn_traditional && !in_system_header && lookup_name (name))
2123 warning ("%Htraditional C lacks a separate namespace for labels, "
2124 "identifier `%s' conflicts", &location,
2125 IDENTIFIER_POINTER (name));
2127 /* Mark label as having been defined. */
2128 DECL_INITIAL (label) = error_mark_node;
2129 return label;
2132 /* Return the list of declarations of the current scope. */
2134 tree
2135 getdecls (void)
2137 return current_scope->names;
2141 /* Given NAME, an IDENTIFIER_NODE,
2142 return the structure (or union or enum) definition for that name.
2143 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2144 CODE says which kind of type the caller wants;
2145 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2146 If the wrong kind of type is found, an error is reported. */
2148 static tree
2149 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2151 tree tag = IDENTIFIER_TAG_VALUE (name);
2152 int thislevel = 0;
2154 if (!tag)
2155 return 0;
2157 /* We only care about whether it's in this level if
2158 thislevel_only was set or it might be a type clash. */
2159 if (thislevel_only || TREE_CODE (tag) != code)
2161 if (current_scope == global_scope
2162 || purpose_member (name, current_scope->tags))
2163 thislevel = 1;
2166 if (thislevel_only && !thislevel)
2167 return 0;
2169 if (TREE_CODE (tag) != code)
2171 /* Definition isn't the kind we were looking for. */
2172 pending_invalid_xref = name;
2173 pending_invalid_xref_location = input_location;
2175 /* If in the same binding level as a declaration as a tag
2176 of a different type, this must not be allowed to
2177 shadow that tag, so give the error immediately.
2178 (For example, "struct foo; union foo;" is invalid.) */
2179 if (thislevel)
2180 pending_xref_error ();
2182 return tag;
2185 /* Print an error message now
2186 for a recent invalid struct, union or enum cross reference.
2187 We don't print them immediately because they are not invalid
2188 when used in the `struct foo;' construct for shadowing. */
2190 void
2191 pending_xref_error (void)
2193 if (pending_invalid_xref != 0)
2194 error ("%H`%s' defined as wrong kind of tag",
2195 &pending_invalid_xref_location,
2196 IDENTIFIER_POINTER (pending_invalid_xref));
2197 pending_invalid_xref = 0;
2201 /* Look up NAME in the current scope and its superiors
2202 in the namespace of variables, functions and typedefs.
2203 Return a ..._DECL node of some kind representing its definition,
2204 or return 0 if it is undefined. */
2206 tree
2207 lookup_name (tree name)
2209 tree decl = IDENTIFIER_SYMBOL_VALUE (name);
2210 if (decl == 0 || decl == error_mark_node)
2211 return decl;
2212 if (C_DECL_INVISIBLE (decl))
2213 return 0;
2214 return decl;
2217 /* Similar to `lookup_name' but look only at the current scope. */
2219 static tree
2220 lookup_name_current_level (tree name)
2222 tree decl = IDENTIFIER_SYMBOL_VALUE (name);
2224 if (decl == 0 || decl == error_mark_node || C_DECL_INVISIBLE (decl))
2225 return 0;
2227 if (current_scope == global_scope)
2228 return decl;
2230 /* Scan the current scope for a decl with name NAME.
2231 For PARM_DECLs, we have to look at both ->parms and ->names, since
2232 forward parameter declarations wind up on the ->names list. */
2233 if (TREE_CODE (decl) == PARM_DECL
2234 && chain_member (decl, current_scope->parms))
2235 return decl;
2236 if (chain_member (decl, current_scope->names))
2237 return decl;
2239 return 0;
2242 /* Create the predefined scalar types of C,
2243 and some nodes representing standard constants (0, 1, (void *) 0).
2244 Initialize the global scope.
2245 Make definitions for built-in primitive functions. */
2247 void
2248 c_init_decl_processing (void)
2250 tree endlink;
2251 tree ptr_ftype_void, ptr_ftype_ptr;
2252 location_t save_loc = input_location;
2254 /* Adds some ggc roots, and reserved words for c-parse.in. */
2255 c_parse_init ();
2257 current_function_decl = 0;
2259 /* Make the c_scope structure for global names. */
2260 pushlevel (0);
2261 global_scope = current_scope;
2263 /* Declarations from c_common_nodes_and_builtins must not be associated
2264 with this input file, lest we get differences between using and not
2265 using preprocessed headers. */
2266 input_location.file = "<internal>";
2267 input_location.line = 0;
2269 /* Make the DECL for the toplevel file scope. */
2270 current_file_decl = build_decl (TRANSLATION_UNIT_DECL, NULL, NULL);
2272 build_common_tree_nodes (flag_signed_char);
2274 c_common_nodes_and_builtins ();
2276 /* In C, comparisons and TRUTH_* expressions have type int. */
2277 truthvalue_type_node = integer_type_node;
2278 truthvalue_true_node = integer_one_node;
2279 truthvalue_false_node = integer_zero_node;
2281 /* Even in C99, which has a real boolean type. */
2282 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2283 boolean_type_node));
2285 endlink = void_list_node;
2286 ptr_ftype_void = build_function_type (ptr_type_node, endlink);
2287 ptr_ftype_ptr
2288 = build_function_type (ptr_type_node,
2289 tree_cons (NULL_TREE, ptr_type_node, endlink));
2291 input_location = save_loc;
2293 pedantic_lvalues = pedantic;
2295 make_fname_decl = c_make_fname_decl;
2296 start_fname_decls ();
2298 first_builtin_decl = global_scope->names;
2299 last_builtin_decl = global_scope->names_last;
2302 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2303 decl, NAME is the initialization string and TYPE_DEP indicates whether
2304 NAME depended on the type of the function. As we don't yet implement
2305 delayed emission of static data, we mark the decl as emitted
2306 so it is not placed in the output. Anything using it must therefore pull
2307 out the STRING_CST initializer directly. FIXME. */
2309 static tree
2310 c_make_fname_decl (tree id, int type_dep)
2312 const char *name = fname_as_string (type_dep);
2313 tree decl, type, init;
2314 size_t length = strlen (name);
2316 type = build_array_type
2317 (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
2318 build_index_type (size_int (length)));
2320 decl = build_decl (VAR_DECL, id, type);
2322 TREE_STATIC (decl) = 1;
2323 TREE_READONLY (decl) = 1;
2324 DECL_ARTIFICIAL (decl) = 1;
2326 init = build_string (length + 1, name);
2327 TREE_TYPE (init) = type;
2328 DECL_INITIAL (decl) = init;
2330 TREE_USED (decl) = 1;
2332 if (current_function_decl)
2334 DECL_CONTEXT (decl) = current_function_decl;
2335 IDENTIFIER_SYMBOL_VALUE (id) = decl;
2336 SCOPE_LIST_APPEND (current_function_scope, names, decl);
2339 finish_decl (decl, init, NULL_TREE);
2341 return decl;
2344 /* Return a definition for a builtin function named NAME and whose data type
2345 is TYPE. TYPE should be a function type with argument types.
2346 FUNCTION_CODE tells later passes how to compile calls to this function.
2347 See tree.h for its possible values.
2349 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2350 the name to be called if we can't opencode the function. If
2351 ATTRS is nonzero, use that for the function's attribute list. */
2353 tree
2354 builtin_function (const char *name, tree type, int function_code,
2355 enum built_in_class class, const char *library_name,
2356 tree attrs)
2358 tree decl = build_decl (FUNCTION_DECL, get_identifier (name), type);
2359 DECL_EXTERNAL (decl) = 1;
2360 TREE_PUBLIC (decl) = 1;
2361 if (library_name)
2362 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2363 make_decl_rtl (decl, NULL);
2364 pushdecl (decl);
2365 DECL_BUILT_IN_CLASS (decl) = class;
2366 DECL_FUNCTION_CODE (decl) = function_code;
2368 /* Warn if a function in the namespace for users
2369 is used without an occasion to consider it declared. */
2370 if (name[0] != '_' || name[1] != '_')
2371 C_DECL_INVISIBLE (decl) = 1;
2373 /* Possibly apply some default attributes to this built-in function. */
2374 if (attrs)
2375 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2376 else
2377 decl_attributes (&decl, NULL_TREE, 0);
2379 return decl;
2382 /* Called when a declaration is seen that contains no names to declare.
2383 If its type is a reference to a structure, union or enum inherited
2384 from a containing scope, shadow that tag name for the current scope
2385 with a forward reference.
2386 If its type defines a new named structure or union
2387 or defines an enum, it is valid but we need not do anything here.
2388 Otherwise, it is an error. */
2390 void
2391 shadow_tag (tree declspecs)
2393 shadow_tag_warned (declspecs, 0);
2396 void
2397 shadow_tag_warned (tree declspecs, int warned)
2400 /* 1 => we have done a pedwarn. 2 => we have done a warning, but
2401 no pedwarn. */
2403 int found_tag = 0;
2404 tree link;
2405 tree specs, attrs;
2407 pending_invalid_xref = 0;
2409 /* Remove the attributes from declspecs, since they will confuse the
2410 following code. */
2411 split_specs_attrs (declspecs, &specs, &attrs);
2413 for (link = specs; link; link = TREE_CHAIN (link))
2415 tree value = TREE_VALUE (link);
2416 enum tree_code code = TREE_CODE (value);
2418 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2419 /* Used to test also that TYPE_SIZE (value) != 0.
2420 That caused warning for `struct foo;' at top level in the file. */
2422 tree name = TYPE_NAME (value);
2423 tree t;
2425 found_tag++;
2427 if (name == 0)
2429 if (warned != 1 && code != ENUMERAL_TYPE)
2430 /* Empty unnamed enum OK */
2432 pedwarn ("unnamed struct/union that defines no instances");
2433 warned = 1;
2436 else
2438 t = lookup_tag (code, name, 1);
2440 if (t == 0)
2442 t = make_node (code);
2443 pushtag (name, t);
2447 else
2449 if (!warned && ! in_system_header)
2451 warning ("useless keyword or type name in empty declaration");
2452 warned = 2;
2457 if (found_tag > 1)
2458 error ("two types specified in one empty declaration");
2460 if (warned != 1)
2462 if (found_tag == 0)
2463 pedwarn ("empty declaration");
2467 /* Construct an array declarator. EXPR is the expression inside [], or
2468 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
2469 to the pointer to which a parameter array is converted). STATIC_P is
2470 nonzero if "static" is inside the [], zero otherwise. VLA_UNSPEC_P
2471 is nonzero is the array is [*], a VLA of unspecified length which is
2472 nevertheless a complete type (not currently implemented by GCC),
2473 zero otherwise. The declarator is constructed as an ARRAY_REF
2474 (to be decoded by grokdeclarator), whose operand 0 is what's on the
2475 left of the [] (filled by in set_array_declarator_type) and operand 1
2476 is the expression inside; whose TREE_TYPE is the type qualifiers and
2477 which has TREE_STATIC set if "static" is used. */
2479 tree
2480 build_array_declarator (tree expr, tree quals, int static_p, int vla_unspec_p)
2482 tree decl;
2483 decl = build_nt (ARRAY_REF, NULL_TREE, expr);
2484 TREE_TYPE (decl) = quals;
2485 TREE_STATIC (decl) = (static_p ? 1 : 0);
2486 if (pedantic && !flag_isoc99)
2488 if (static_p || quals != NULL_TREE)
2489 pedwarn ("ISO C90 does not support `static' or type qualifiers in parameter array declarators");
2490 if (vla_unspec_p)
2491 pedwarn ("ISO C90 does not support `[*]' array declarators");
2493 if (vla_unspec_p)
2494 warning ("GCC does not yet properly implement `[*]' array declarators");
2495 return decl;
2498 /* Set the type of an array declarator. DECL is the declarator, as
2499 constructed by build_array_declarator; TYPE is what appears on the left
2500 of the [] and goes in operand 0. ABSTRACT_P is nonzero if it is an
2501 abstract declarator, zero otherwise; this is used to reject static and
2502 type qualifiers in abstract declarators, where they are not in the
2503 C99 grammar. */
2505 tree
2506 set_array_declarator_type (tree decl, tree type, int abstract_p)
2508 TREE_OPERAND (decl, 0) = type;
2509 if (abstract_p && (TREE_TYPE (decl) != NULL_TREE || TREE_STATIC (decl)))
2510 error ("static or type qualifiers in abstract declarator");
2511 return decl;
2514 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
2516 tree
2517 groktypename (tree typename)
2519 tree specs, attrs;
2521 if (TREE_CODE (typename) != TREE_LIST)
2522 return typename;
2524 split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
2526 typename = grokdeclarator (TREE_VALUE (typename), specs, TYPENAME, 0);
2528 /* Apply attributes. */
2529 decl_attributes (&typename, attrs, 0);
2531 return typename;
2534 /* Return a PARM_DECL node for a given pair of specs and declarator. */
2536 tree
2537 groktypename_in_parm_context (tree typename)
2539 if (TREE_CODE (typename) != TREE_LIST)
2540 return typename;
2541 return grokdeclarator (TREE_VALUE (typename),
2542 TREE_PURPOSE (typename),
2543 PARM, 0);
2546 /* Decode a declarator in an ordinary declaration or data definition.
2547 This is called as soon as the type information and variable name
2548 have been parsed, before parsing the initializer if any.
2549 Here we create the ..._DECL node, fill in its type,
2550 and put it on the list of decls for the current context.
2551 The ..._DECL node is returned as the value.
2553 Exception: for arrays where the length is not specified,
2554 the type is left null, to be filled in by `finish_decl'.
2556 Function definitions do not come here; they go to start_function
2557 instead. However, external and forward declarations of functions
2558 do go through here. Structure field declarations are done by
2559 grokfield and not through here. */
2561 tree
2562 start_decl (tree declarator, tree declspecs, int initialized, tree attributes)
2564 tree decl;
2565 tree tem;
2567 /* An object declared as __attribute__((deprecated)) suppresses
2568 warnings of uses of other deprecated items. */
2569 if (lookup_attribute ("deprecated", attributes))
2570 deprecated_state = DEPRECATED_SUPPRESS;
2572 decl = grokdeclarator (declarator, declspecs,
2573 NORMAL, initialized);
2575 deprecated_state = DEPRECATED_NORMAL;
2577 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
2578 && MAIN_NAME_P (DECL_NAME (decl)))
2579 warning ("%J'%D' is usually a function", decl, decl);
2581 if (initialized)
2582 /* Is it valid for this decl to have an initializer at all?
2583 If not, set INITIALIZED to zero, which will indirectly
2584 tell `finish_decl' to ignore the initializer once it is parsed. */
2585 switch (TREE_CODE (decl))
2587 case TYPE_DECL:
2588 error ("typedef `%s' is initialized (use __typeof__ instead)",
2589 IDENTIFIER_POINTER (DECL_NAME (decl)));
2590 initialized = 0;
2591 break;
2593 case FUNCTION_DECL:
2594 error ("function `%s' is initialized like a variable",
2595 IDENTIFIER_POINTER (DECL_NAME (decl)));
2596 initialized = 0;
2597 break;
2599 case PARM_DECL:
2600 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
2601 error ("parameter `%s' is initialized",
2602 IDENTIFIER_POINTER (DECL_NAME (decl)));
2603 initialized = 0;
2604 break;
2606 default:
2607 /* Don't allow initializations for incomplete types
2608 except for arrays which might be completed by the initialization. */
2610 /* This can happen if the array size is an undefined macro. We already
2611 gave a warning, so we don't need another one. */
2612 if (TREE_TYPE (decl) == error_mark_node)
2613 initialized = 0;
2614 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
2616 /* A complete type is ok if size is fixed. */
2618 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
2619 || C_DECL_VARIABLE_SIZE (decl))
2621 error ("variable-sized object may not be initialized");
2622 initialized = 0;
2625 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
2627 error ("variable `%s' has initializer but incomplete type",
2628 IDENTIFIER_POINTER (DECL_NAME (decl)));
2629 initialized = 0;
2631 else if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
2633 error ("elements of array `%s' have incomplete type",
2634 IDENTIFIER_POINTER (DECL_NAME (decl)));
2635 initialized = 0;
2639 if (initialized)
2641 DECL_EXTERNAL (decl) = 0;
2642 if (current_scope == global_scope)
2643 TREE_STATIC (decl) = 1;
2645 /* Tell `pushdecl' this is an initialized decl
2646 even though we don't yet have the initializer expression.
2647 Also tell `finish_decl' it may store the real initializer. */
2648 DECL_INITIAL (decl) = error_mark_node;
2651 /* If this is a function declaration, write a record describing it to the
2652 prototypes file (if requested). */
2654 if (TREE_CODE (decl) == FUNCTION_DECL)
2655 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
2657 /* ANSI specifies that a tentative definition which is not merged with
2658 a non-tentative definition behaves exactly like a definition with an
2659 initializer equal to zero. (Section 3.7.2)
2661 -fno-common gives strict ANSI behavior, though this tends to break
2662 a large body of code that grew up without this rule.
2664 Thread-local variables are never common, since there's no entrenched
2665 body of code to break, and it allows more efficient variable references
2666 in the presence of dynamic linking. */
2668 if (TREE_CODE (decl) == VAR_DECL
2669 && !initialized
2670 && TREE_PUBLIC (decl)
2671 && !DECL_THREAD_LOCAL (decl)
2672 && !flag_no_common)
2673 DECL_COMMON (decl) = 1;
2675 /* Set attributes here so if duplicate decl, will have proper attributes. */
2676 decl_attributes (&decl, attributes, 0);
2678 if (TREE_CODE (decl) == FUNCTION_DECL
2679 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
2681 tree ce = declarator;
2683 if (TREE_CODE (ce) == INDIRECT_REF)
2684 ce = TREE_OPERAND (declarator, 0);
2685 if (TREE_CODE (ce) == CALL_EXPR)
2687 tree args = TREE_PURPOSE (TREE_OPERAND (ce, 1));
2688 for (; args; args = TREE_CHAIN (args))
2690 tree type = TREE_TYPE (args);
2691 if (INTEGRAL_TYPE_P (type)
2692 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
2693 DECL_ARG_TYPE (args) = integer_type_node;
2698 if (TREE_CODE (decl) == FUNCTION_DECL
2699 && DECL_DECLARED_INLINE_P (decl)
2700 && DECL_UNINLINABLE (decl)
2701 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
2702 warning ("%Jinline function '%D' given attribute noinline", decl, decl);
2704 /* Add this decl to the current scope.
2705 TEM may equal DECL or it may be a previous decl of the same name. */
2706 tem = pushdecl (decl);
2708 /* For a local variable, define the RTL now. */
2709 if (current_scope != global_scope
2710 /* But not if this is a duplicate decl
2711 and we preserved the rtl from the previous one
2712 (which may or may not happen). */
2713 && !DECL_RTL_SET_P (tem)
2714 && DECL_FILE_SCOPE_P (tem))
2716 if (TREE_TYPE (tem) != error_mark_node
2717 && (COMPLETE_TYPE_P (TREE_TYPE (tem))
2718 || (TREE_CODE (TREE_TYPE (tem)) == ARRAY_TYPE
2719 && DECL_INITIAL (tem) != 0)))
2720 expand_decl (tem);
2723 return tem;
2726 /* Finish processing of a declaration;
2727 install its initial value.
2728 If the length of an array type is not known before,
2729 it must be determined now, from the initial value, or it is an error. */
2731 void
2732 finish_decl (tree decl, tree init, tree asmspec_tree)
2734 tree type = TREE_TYPE (decl);
2735 int was_incomplete = (DECL_SIZE (decl) == 0);
2736 const char *asmspec = 0;
2738 /* If a name was specified, get the string. */
2739 if (current_scope == global_scope)
2740 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
2741 if (asmspec_tree)
2742 asmspec = TREE_STRING_POINTER (asmspec_tree);
2744 /* If `start_decl' didn't like having an initialization, ignore it now. */
2745 if (init != 0 && DECL_INITIAL (decl) == 0)
2746 init = 0;
2748 /* Don't crash if parm is initialized. */
2749 if (TREE_CODE (decl) == PARM_DECL)
2750 init = 0;
2752 if (init)
2753 store_init_value (decl, init);
2755 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
2756 || TREE_CODE (decl) == FUNCTION_DECL
2757 || TREE_CODE (decl) == FIELD_DECL))
2758 objc_check_decl (decl);
2760 /* Deduce size of array from initialization, if not already known */
2761 if (TREE_CODE (type) == ARRAY_TYPE
2762 && TYPE_DOMAIN (type) == 0
2763 && TREE_CODE (decl) != TYPE_DECL)
2765 int do_default
2766 = (TREE_STATIC (decl)
2767 /* Even if pedantic, an external linkage array
2768 may have incomplete type at first. */
2769 ? pedantic && !TREE_PUBLIC (decl)
2770 : !DECL_EXTERNAL (decl));
2771 int failure
2772 = complete_array_type (type, DECL_INITIAL (decl), do_default);
2774 /* Get the completed type made by complete_array_type. */
2775 type = TREE_TYPE (decl);
2777 if (failure == 1)
2778 error ("%Jinitializer fails to determine size of '%D'", decl, decl);
2780 else if (failure == 2)
2782 if (do_default)
2783 error ("%Jarray size missing in '%D'", decl, decl);
2784 /* If a `static' var's size isn't known,
2785 make it extern as well as static, so it does not get
2786 allocated.
2787 If it is not `static', then do not mark extern;
2788 finish_incomplete_decl will give it a default size
2789 and it will get allocated. */
2790 else if (!pedantic && TREE_STATIC (decl) && ! TREE_PUBLIC (decl))
2791 DECL_EXTERNAL (decl) = 1;
2794 /* TYPE_MAX_VALUE is always one less than the number of elements
2795 in the array, because we start counting at zero. Therefore,
2796 warn only if the value is less than zero. */
2797 else if (pedantic && TYPE_DOMAIN (type) != 0
2798 && tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < 0)
2799 error ("%Jzero or negative size array '%D'", decl, decl);
2801 layout_decl (decl, 0);
2804 if (TREE_CODE (decl) == VAR_DECL)
2806 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
2807 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
2808 layout_decl (decl, 0);
2810 if (DECL_SIZE (decl) == 0
2811 /* Don't give an error if we already gave one earlier. */
2812 && TREE_TYPE (decl) != error_mark_node
2813 && (TREE_STATIC (decl)
2815 /* A static variable with an incomplete type
2816 is an error if it is initialized.
2817 Also if it is not file scope.
2818 Otherwise, let it through, but if it is not `extern'
2819 then it may cause an error message later. */
2820 (DECL_INITIAL (decl) != 0
2821 || !DECL_FILE_SCOPE_P (decl))
2823 /* An automatic variable with an incomplete type
2824 is an error. */
2825 !DECL_EXTERNAL (decl)))
2827 error ("%Jstorage size of '%D' isn't known", decl, decl);
2828 TREE_TYPE (decl) = error_mark_node;
2831 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
2832 && DECL_SIZE (decl) != 0)
2834 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
2835 constant_expression_warning (DECL_SIZE (decl));
2836 else
2837 error ("%Jstorage size of '%D' isn't constant", decl, decl);
2840 if (TREE_USED (type))
2841 TREE_USED (decl) = 1;
2844 /* If this is a function and an assembler name is specified, reset DECL_RTL
2845 so we can give it its new name. Also, update built_in_decls if it
2846 was a normal built-in. */
2847 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
2849 /* ASMSPEC is given, and not the name of a register. Mark the
2850 name with a star so assemble_name won't munge it. */
2851 char *starred = alloca (strlen (asmspec) + 2);
2852 starred[0] = '*';
2853 strcpy (starred + 1, asmspec);
2855 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
2857 tree builtin = built_in_decls [DECL_FUNCTION_CODE (decl)];
2858 SET_DECL_RTL (builtin, NULL_RTX);
2859 SET_DECL_ASSEMBLER_NAME (builtin, get_identifier (starred));
2860 #ifdef TARGET_MEM_FUNCTIONS
2861 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMCPY)
2862 init_block_move_fn (starred);
2863 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMSET)
2864 init_block_clear_fn (starred);
2865 #else
2866 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BCOPY)
2867 init_block_move_fn (starred);
2868 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BZERO)
2869 init_block_clear_fn (starred);
2870 #endif
2872 SET_DECL_RTL (decl, NULL_RTX);
2873 change_decl_assembler_name (decl, get_identifier (starred));
2876 /* If #pragma weak was used, mark the decl weak now. */
2877 if (current_scope == global_scope)
2878 maybe_apply_pragma_weak (decl);
2880 /* Output the assembler code and/or RTL code for variables and functions,
2881 unless the type is an undefined structure or union.
2882 If not, it will get done when the type is completed. */
2884 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
2886 /* This is a no-op in c-lang.c or something real in objc-act.c. */
2887 if (c_dialect_objc ())
2888 objc_check_decl (decl);
2890 if (DECL_FILE_SCOPE_P (decl))
2892 if (DECL_INITIAL (decl) == NULL_TREE
2893 || DECL_INITIAL (decl) == error_mark_node)
2894 /* Don't output anything
2895 when a tentative file-scope definition is seen.
2896 But at end of compilation, do output code for them. */
2897 DECL_DEFER_OUTPUT (decl) = 1;
2898 rest_of_decl_compilation (decl, asmspec, true, 0);
2900 else
2902 /* This is a local variable. If there is an ASMSPEC, the
2903 user has requested that we handle it specially. */
2904 if (asmspec)
2906 /* In conjunction with an ASMSPEC, the `register'
2907 keyword indicates that we should place the variable
2908 in a particular register. */
2909 if (DECL_REGISTER (decl))
2910 DECL_C_HARD_REGISTER (decl) = 1;
2912 /* If this is not a static variable, issue a warning.
2913 It doesn't make any sense to give an ASMSPEC for an
2914 ordinary, non-register local variable. Historically,
2915 GCC has accepted -- but ignored -- the ASMSPEC in
2916 this case. */
2917 if (TREE_CODE (decl) == VAR_DECL
2918 && !DECL_REGISTER (decl)
2919 && !TREE_STATIC (decl))
2920 warning ("%Jignoring asm-specifier for non-static local "
2921 "variable '%D'", decl, decl);
2922 else
2923 change_decl_assembler_name (decl, get_identifier (asmspec));
2926 if (TREE_CODE (decl) != FUNCTION_DECL)
2927 add_decl_stmt (decl);
2930 if (!DECL_FILE_SCOPE_P (decl))
2932 /* Recompute the RTL of a local array now
2933 if it used to be an incomplete type. */
2934 if (was_incomplete
2935 && ! TREE_STATIC (decl) && ! DECL_EXTERNAL (decl))
2937 /* If we used it already as memory, it must stay in memory. */
2938 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
2939 /* If it's still incomplete now, no init will save it. */
2940 if (DECL_SIZE (decl) == 0)
2941 DECL_INITIAL (decl) = 0;
2946 /* If this was marked 'used', be sure it will be output. */
2947 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
2948 mark_referenced (DECL_ASSEMBLER_NAME (decl));
2950 if (TREE_CODE (decl) == TYPE_DECL)
2951 rest_of_decl_compilation (decl, NULL, DECL_FILE_SCOPE_P (decl), 0);
2953 /* At the end of a declaration, throw away any variable type sizes
2954 of types defined inside that declaration. There is no use
2955 computing them in the following function definition. */
2956 if (current_scope == global_scope)
2957 get_pending_sizes ();
2959 /* Install a cleanup (aka destructor) if one was given. */
2960 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
2962 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
2963 if (attr)
2965 static bool eh_initialized_p;
2967 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
2968 tree cleanup_decl = lookup_name (cleanup_id);
2969 tree cleanup;
2971 /* Build "cleanup(&decl)" for the destructor. */
2972 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
2973 cleanup = build_tree_list (NULL_TREE, cleanup);
2974 cleanup = build_function_call (cleanup_decl, cleanup);
2976 /* Don't warn about decl unused; the cleanup uses it. */
2977 TREE_USED (decl) = 1;
2979 /* Initialize EH, if we've been told to do so. */
2980 if (flag_exceptions && !eh_initialized_p)
2982 eh_initialized_p = true;
2983 eh_personality_libfunc
2984 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
2985 ? "__gcc_personality_sj0"
2986 : "__gcc_personality_v0");
2987 using_eh_for_cleanups ();
2990 add_stmt (build_stmt (CLEANUP_STMT, decl, cleanup));
2995 /* Given a parsed parameter declaration, decode it into a PARM_DECL
2996 and push that on the current scope. */
2998 void
2999 push_parm_decl (tree parm)
3001 tree decl;
3003 /* Don't attempt to expand sizes while parsing this decl.
3004 (We can get here with i_s_e 1 somehow from Objective-C.) */
3005 int save_immediate_size_expand = immediate_size_expand;
3006 immediate_size_expand = 0;
3008 decl = grokdeclarator (TREE_VALUE (TREE_PURPOSE (parm)),
3009 TREE_PURPOSE (TREE_PURPOSE (parm)), PARM, 0);
3010 decl_attributes (&decl, TREE_VALUE (parm), 0);
3012 decl = pushdecl (decl);
3014 finish_decl (decl, NULL_TREE, NULL_TREE);
3016 immediate_size_expand = save_immediate_size_expand;
3019 /* Mark all the parameter declarations to date as forward decls,
3020 shift them to the variables list, and reset the parameters list.
3021 Also diagnose use of this extension. */
3023 void
3024 mark_forward_parm_decls (void)
3026 tree parm;
3028 if (pedantic && !current_scope->warned_forward_parm_decls)
3030 pedwarn ("ISO C forbids forward parameter declarations");
3031 current_scope->warned_forward_parm_decls = true;
3034 for (parm = current_scope->parms; parm; parm = TREE_CHAIN (parm))
3035 TREE_ASM_WRITTEN (parm) = 1;
3037 SCOPE_LIST_CONCAT (current_scope, names, current_scope, parms);
3038 current_scope->parms = 0;
3039 current_scope->parms_last = 0;
3042 static GTY(()) int compound_literal_number;
3044 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3045 literal, which may be an incomplete array type completed by the
3046 initializer; INIT is a CONSTRUCTOR that initializes the compound
3047 literal. */
3049 tree
3050 build_compound_literal (tree type, tree init)
3052 /* We do not use start_decl here because we have a type, not a declarator;
3053 and do not use finish_decl because the decl should be stored inside
3054 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_STMT. */
3055 tree decl = build_decl (VAR_DECL, NULL_TREE, type);
3056 tree complit;
3057 tree stmt;
3058 DECL_EXTERNAL (decl) = 0;
3059 TREE_PUBLIC (decl) = 0;
3060 TREE_STATIC (decl) = (current_scope == global_scope);
3061 DECL_CONTEXT (decl) = current_function_decl;
3062 TREE_USED (decl) = 1;
3063 TREE_TYPE (decl) = type;
3064 TREE_READONLY (decl) = TREE_READONLY (type);
3065 store_init_value (decl, init);
3067 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3069 int failure = complete_array_type (type, DECL_INITIAL (decl), 1);
3070 if (failure)
3071 abort ();
3074 type = TREE_TYPE (decl);
3075 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3076 return error_mark_node;
3078 stmt = build_stmt (DECL_STMT, decl);
3079 complit = build1 (COMPOUND_LITERAL_EXPR, TREE_TYPE (decl), stmt);
3080 TREE_SIDE_EFFECTS (complit) = 1;
3082 layout_decl (decl, 0);
3084 if (TREE_STATIC (decl))
3086 /* This decl needs a name for the assembler output. We also need
3087 a unique suffix to be added to the name. */
3088 char *name;
3090 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
3091 compound_literal_number);
3092 compound_literal_number++;
3093 DECL_NAME (decl) = get_identifier (name);
3094 DECL_DEFER_OUTPUT (decl) = 1;
3095 DECL_COMDAT (decl) = 1;
3096 DECL_ARTIFICIAL (decl) = 1;
3097 pushdecl (decl);
3098 rest_of_decl_compilation (decl, NULL, 1, 0);
3101 return complit;
3104 /* Make TYPE a complete type based on INITIAL_VALUE.
3105 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
3106 2 if there was no information (in which case assume 1 if DO_DEFAULT). */
3109 complete_array_type (tree type, tree initial_value, int do_default)
3111 tree maxindex = NULL_TREE;
3112 int value = 0;
3114 if (initial_value)
3116 /* Note MAXINDEX is really the maximum index,
3117 one less than the size. */
3118 if (TREE_CODE (initial_value) == STRING_CST)
3120 int eltsize
3121 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
3122 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
3123 / eltsize) - 1, 0);
3125 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
3127 tree elts = CONSTRUCTOR_ELTS (initial_value);
3128 maxindex = build_int_2 (-1, -1);
3129 for (; elts; elts = TREE_CHAIN (elts))
3131 if (TREE_PURPOSE (elts))
3132 maxindex = TREE_PURPOSE (elts);
3133 else
3134 maxindex = fold (build (PLUS_EXPR, integer_type_node,
3135 maxindex, integer_one_node));
3137 maxindex = copy_node (maxindex);
3139 else
3141 /* Make an error message unless that happened already. */
3142 if (initial_value != error_mark_node)
3143 value = 1;
3145 /* Prevent further error messages. */
3146 maxindex = build_int_2 (0, 0);
3150 if (!maxindex)
3152 if (do_default)
3153 maxindex = build_int_2 (0, 0);
3154 value = 2;
3157 if (maxindex)
3159 TYPE_DOMAIN (type) = build_index_type (maxindex);
3160 if (!TREE_TYPE (maxindex))
3161 TREE_TYPE (maxindex) = TYPE_DOMAIN (type);
3164 /* Lay out the type now that we can get the real answer. */
3166 layout_type (type);
3168 return value;
3171 /* Determine whether TYPE is a structure with a flexible array member,
3172 or a union containing such a structure (possibly recursively). */
3174 static bool
3175 flexible_array_type_p (tree type)
3177 tree x;
3178 switch (TREE_CODE (type))
3180 case RECORD_TYPE:
3181 x = TYPE_FIELDS (type);
3182 if (x == NULL_TREE)
3183 return false;
3184 while (TREE_CHAIN (x) != NULL_TREE)
3185 x = TREE_CHAIN (x);
3186 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3187 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3188 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3189 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3190 return true;
3191 return false;
3192 case UNION_TYPE:
3193 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3195 if (flexible_array_type_p (TREE_TYPE (x)))
3196 return true;
3198 return false;
3199 default:
3200 return false;
3204 /* Given declspecs and a declarator,
3205 determine the name and type of the object declared
3206 and construct a ..._DECL node for it.
3207 (In one case we can return a ..._TYPE node instead.
3208 For invalid input we sometimes return 0.)
3210 DECLSPECS is a chain of tree_list nodes whose value fields
3211 are the storage classes and type specifiers.
3213 DECL_CONTEXT says which syntactic context this declaration is in:
3214 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3215 FUNCDEF for a function definition. Like NORMAL but a few different
3216 error messages in each case. Return value may be zero meaning
3217 this definition is too screwy to try to parse.
3218 PARM for a parameter declaration (either within a function prototype
3219 or before a function body). Make a PARM_DECL, or return void_type_node.
3220 TYPENAME if for a typename (in a cast or sizeof).
3221 Don't make a DECL node; just return the ..._TYPE node.
3222 FIELD for a struct or union field; make a FIELD_DECL.
3223 BITFIELD for a field with specified width.
3224 INITIALIZED is 1 if the decl has an initializer.
3226 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3227 It may also be so in the PARM case, for a prototype where the
3228 argument type is specified but not the name.
3230 This function is where the complicated C meanings of `static'
3231 and `extern' are interpreted. */
3233 static tree
3234 grokdeclarator (tree declarator, tree declspecs,
3235 enum decl_context decl_context, int initialized)
3237 int specbits = 0;
3238 tree spec;
3239 tree type = NULL_TREE;
3240 int longlong = 0;
3241 int constp;
3242 int restrictp;
3243 int volatilep;
3244 int type_quals = TYPE_UNQUALIFIED;
3245 int inlinep;
3246 int explicit_int = 0;
3247 int explicit_char = 0;
3248 int defaulted_int = 0;
3249 tree typedef_decl = 0;
3250 const char *name;
3251 tree typedef_type = 0;
3252 int funcdef_flag = 0;
3253 enum tree_code innermost_code = ERROR_MARK;
3254 int bitfield = 0;
3255 int size_varies = 0;
3256 tree decl_attr = NULL_TREE;
3257 tree array_ptr_quals = NULL_TREE;
3258 int array_parm_static = 0;
3259 tree returned_attrs = NULL_TREE;
3261 if (decl_context == BITFIELD)
3262 bitfield = 1, decl_context = FIELD;
3264 if (decl_context == FUNCDEF)
3265 funcdef_flag = 1, decl_context = NORMAL;
3267 /* Look inside a declarator for the name being declared
3268 and get it as a string, for an error message. */
3270 tree decl = declarator;
3271 name = 0;
3273 while (decl)
3274 switch (TREE_CODE (decl))
3276 case ARRAY_REF:
3277 case INDIRECT_REF:
3278 case CALL_EXPR:
3279 innermost_code = TREE_CODE (decl);
3280 decl = TREE_OPERAND (decl, 0);
3281 break;
3283 case TREE_LIST:
3284 decl = TREE_VALUE (decl);
3285 break;
3287 case IDENTIFIER_NODE:
3288 name = IDENTIFIER_POINTER (decl);
3289 decl = 0;
3290 break;
3292 default:
3293 abort ();
3295 if (name == 0)
3296 name = "type name";
3299 /* A function definition's declarator must have the form of
3300 a function declarator. */
3302 if (funcdef_flag && innermost_code != CALL_EXPR)
3303 return 0;
3305 /* If this looks like a function definition, make it one,
3306 even if it occurs where parms are expected.
3307 Then store_parm_decls will reject it and not use it as a parm. */
3308 if (decl_context == NORMAL && !funcdef_flag
3309 && current_scope->parm_flag)
3310 decl_context = PARM;
3312 /* Look through the decl specs and record which ones appear.
3313 Some typespecs are defined as built-in typenames.
3314 Others, the ones that are modifiers of other types,
3315 are represented by bits in SPECBITS: set the bits for
3316 the modifiers that appear. Storage class keywords are also in SPECBITS.
3318 If there is a typedef name or a type, store the type in TYPE.
3319 This includes builtin typedefs such as `int'.
3321 Set EXPLICIT_INT or EXPLICIT_CHAR if the type is `int' or `char'
3322 and did not come from a user typedef.
3324 Set LONGLONG if `long' is mentioned twice. */
3326 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
3328 tree id = TREE_VALUE (spec);
3330 /* If the entire declaration is itself tagged as deprecated then
3331 suppress reports of deprecated items. */
3332 if (id && TREE_DEPRECATED (id))
3334 if (deprecated_state != DEPRECATED_SUPPRESS)
3335 warn_deprecated_use (id);
3338 if (id == ridpointers[(int) RID_INT])
3339 explicit_int = 1;
3340 if (id == ridpointers[(int) RID_CHAR])
3341 explicit_char = 1;
3343 if (TREE_CODE (id) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (id))
3345 enum rid i = C_RID_CODE (id);
3346 if ((int) i <= (int) RID_LAST_MODIFIER)
3348 if (i == RID_LONG && (specbits & (1 << (int) RID_LONG)))
3350 if (longlong)
3351 error ("`long long long' is too long for GCC");
3352 else
3354 if (pedantic && !flag_isoc99 && ! in_system_header
3355 && warn_long_long)
3356 pedwarn ("ISO C90 does not support `long long'");
3357 longlong = 1;
3360 else if (specbits & (1 << (int) i))
3362 if (i == RID_CONST || i == RID_VOLATILE || i == RID_RESTRICT)
3364 if (!flag_isoc99)
3365 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
3367 else
3368 error ("duplicate `%s'", IDENTIFIER_POINTER (id));
3371 /* Diagnose "__thread extern". Recall that this list
3372 is in the reverse order seen in the text. */
3373 if (i == RID_THREAD
3374 && (specbits & (1 << (int) RID_EXTERN
3375 | 1 << (int) RID_STATIC)))
3377 if (specbits & 1 << (int) RID_EXTERN)
3378 error ("`__thread' before `extern'");
3379 else
3380 error ("`__thread' before `static'");
3383 specbits |= 1 << (int) i;
3384 goto found;
3387 if (type)
3388 error ("two or more data types in declaration of `%s'", name);
3389 /* Actual typedefs come to us as TYPE_DECL nodes. */
3390 else if (TREE_CODE (id) == TYPE_DECL)
3392 if (TREE_TYPE (id) == error_mark_node)
3393 ; /* Allow the type to default to int to avoid cascading errors. */
3394 else
3396 type = TREE_TYPE (id);
3397 decl_attr = DECL_ATTRIBUTES (id);
3398 typedef_decl = id;
3401 /* Built-in types come as identifiers. */
3402 else if (TREE_CODE (id) == IDENTIFIER_NODE)
3404 tree t = lookup_name (id);
3405 if (TREE_TYPE (t) == error_mark_node)
3407 else if (!t || TREE_CODE (t) != TYPE_DECL)
3408 error ("`%s' fails to be a typedef or built in type",
3409 IDENTIFIER_POINTER (id));
3410 else
3412 type = TREE_TYPE (t);
3413 typedef_decl = t;
3416 else if (TREE_CODE (id) != ERROR_MARK)
3417 type = id;
3419 found:
3423 typedef_type = type;
3424 if (type)
3425 size_varies = C_TYPE_VARIABLE_SIZE (type);
3427 /* No type at all: default to `int', and set DEFAULTED_INT
3428 because it was not a user-defined typedef. */
3430 if (type == 0)
3432 if ((! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3433 | (1 << (int) RID_SIGNED)
3434 | (1 << (int) RID_UNSIGNED)
3435 | (1 << (int) RID_COMPLEX))))
3436 /* Don't warn about typedef foo = bar. */
3437 && ! (specbits & (1 << (int) RID_TYPEDEF) && initialized)
3438 && ! in_system_header)
3440 /* Issue a warning if this is an ISO C 99 program or if -Wreturn-type
3441 and this is a function, or if -Wimplicit; prefer the former
3442 warning since it is more explicit. */
3443 if ((warn_implicit_int || warn_return_type || flag_isoc99)
3444 && funcdef_flag)
3445 warn_about_return_type = 1;
3446 else if (warn_implicit_int || flag_isoc99)
3447 pedwarn_c99 ("type defaults to `int' in declaration of `%s'",
3448 name);
3451 defaulted_int = 1;
3452 type = integer_type_node;
3455 /* Now process the modifiers that were specified
3456 and check for invalid combinations. */
3458 /* Long double is a special combination. */
3460 if ((specbits & 1 << (int) RID_LONG) && ! longlong
3461 && TYPE_MAIN_VARIANT (type) == double_type_node)
3463 specbits &= ~(1 << (int) RID_LONG);
3464 type = long_double_type_node;
3467 /* Check all other uses of type modifiers. */
3469 if (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3470 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED)))
3472 int ok = 0;
3474 if ((specbits & 1 << (int) RID_LONG)
3475 && (specbits & 1 << (int) RID_SHORT))
3476 error ("both long and short specified for `%s'", name);
3477 else if (((specbits & 1 << (int) RID_LONG)
3478 || (specbits & 1 << (int) RID_SHORT))
3479 && explicit_char)
3480 error ("long or short specified with char for `%s'", name);
3481 else if (((specbits & 1 << (int) RID_LONG)
3482 || (specbits & 1 << (int) RID_SHORT))
3483 && TREE_CODE (type) == REAL_TYPE)
3485 static int already = 0;
3487 error ("long or short specified with floating type for `%s'", name);
3488 if (! already && ! pedantic)
3490 error ("the only valid combination is `long double'");
3491 already = 1;
3494 else if ((specbits & 1 << (int) RID_SIGNED)
3495 && (specbits & 1 << (int) RID_UNSIGNED))
3496 error ("both signed and unsigned specified for `%s'", name);
3497 else if (TREE_CODE (type) != INTEGER_TYPE)
3498 error ("long, short, signed or unsigned invalid for `%s'", name);
3499 else
3501 ok = 1;
3502 if (!explicit_int && !defaulted_int && !explicit_char)
3504 error ("long, short, signed or unsigned used invalidly for `%s'",
3505 name);
3506 ok = 0;
3510 /* Discard the type modifiers if they are invalid. */
3511 if (! ok)
3513 specbits &= ~((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3514 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED));
3515 longlong = 0;
3519 if ((specbits & (1 << (int) RID_COMPLEX))
3520 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
3522 error ("complex invalid for `%s'", name);
3523 specbits &= ~(1 << (int) RID_COMPLEX);
3526 /* Decide whether an integer type is signed or not.
3527 Optionally treat bitfields as signed by default. */
3528 if (specbits & 1 << (int) RID_UNSIGNED
3529 || (bitfield && ! flag_signed_bitfields
3530 && (explicit_int || defaulted_int || explicit_char
3531 /* A typedef for plain `int' without `signed'
3532 can be controlled just like plain `int'. */
3533 || ! (typedef_decl != 0
3534 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
3535 && TREE_CODE (type) != ENUMERAL_TYPE
3536 && !(specbits & 1 << (int) RID_SIGNED)))
3538 if (longlong)
3539 type = long_long_unsigned_type_node;
3540 else if (specbits & 1 << (int) RID_LONG)
3541 type = long_unsigned_type_node;
3542 else if (specbits & 1 << (int) RID_SHORT)
3543 type = short_unsigned_type_node;
3544 else if (type == char_type_node)
3545 type = unsigned_char_type_node;
3546 else if (typedef_decl)
3547 type = c_common_unsigned_type (type);
3548 else
3549 type = unsigned_type_node;
3551 else if ((specbits & 1 << (int) RID_SIGNED)
3552 && type == char_type_node)
3553 type = signed_char_type_node;
3554 else if (longlong)
3555 type = long_long_integer_type_node;
3556 else if (specbits & 1 << (int) RID_LONG)
3557 type = long_integer_type_node;
3558 else if (specbits & 1 << (int) RID_SHORT)
3559 type = short_integer_type_node;
3561 if (specbits & 1 << (int) RID_COMPLEX)
3563 if (pedantic && !flag_isoc99)
3564 pedwarn ("ISO C90 does not support complex types");
3565 /* If we just have "complex", it is equivalent to
3566 "complex double", but if any modifiers at all are specified it is
3567 the complex form of TYPE. E.g, "complex short" is
3568 "complex short int". */
3570 if (defaulted_int && ! longlong
3571 && ! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3572 | (1 << (int) RID_SIGNED)
3573 | (1 << (int) RID_UNSIGNED))))
3575 if (pedantic)
3576 pedwarn ("ISO C does not support plain `complex' meaning `double complex'");
3577 type = complex_double_type_node;
3579 else if (type == integer_type_node)
3581 if (pedantic)
3582 pedwarn ("ISO C does not support complex integer types");
3583 type = complex_integer_type_node;
3585 else if (type == float_type_node)
3586 type = complex_float_type_node;
3587 else if (type == double_type_node)
3588 type = complex_double_type_node;
3589 else if (type == long_double_type_node)
3590 type = complex_long_double_type_node;
3591 else
3593 if (pedantic)
3594 pedwarn ("ISO C does not support complex integer types");
3595 type = build_complex_type (type);
3599 /* Figure out the type qualifiers for the declaration. There are
3600 two ways a declaration can become qualified. One is something
3601 like `const int i' where the `const' is explicit. Another is
3602 something like `typedef const int CI; CI i' where the type of the
3603 declaration contains the `const'. */
3604 constp = !! (specbits & 1 << (int) RID_CONST) + TYPE_READONLY (type);
3605 restrictp = !! (specbits & 1 << (int) RID_RESTRICT) + TYPE_RESTRICT (type);
3606 volatilep = !! (specbits & 1 << (int) RID_VOLATILE) + TYPE_VOLATILE (type);
3607 inlinep = !! (specbits & (1 << (int) RID_INLINE));
3608 if (constp > 1 && ! flag_isoc99)
3609 pedwarn ("duplicate `const'");
3610 if (restrictp > 1 && ! flag_isoc99)
3611 pedwarn ("duplicate `restrict'");
3612 if (volatilep > 1 && ! flag_isoc99)
3613 pedwarn ("duplicate `volatile'");
3614 if (! flag_gen_aux_info && (TYPE_QUALS (type)))
3615 type = TYPE_MAIN_VARIANT (type);
3616 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
3617 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
3618 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
3620 /* Warn if two storage classes are given. Default to `auto'. */
3623 int nclasses = 0;
3625 if (specbits & 1 << (int) RID_AUTO) nclasses++;
3626 if (specbits & 1 << (int) RID_STATIC) nclasses++;
3627 if (specbits & 1 << (int) RID_EXTERN) nclasses++;
3628 if (specbits & 1 << (int) RID_REGISTER) nclasses++;
3629 if (specbits & 1 << (int) RID_TYPEDEF) nclasses++;
3631 /* "static __thread" and "extern __thread" are allowed. */
3632 if ((specbits & (1 << (int) RID_THREAD
3633 | 1 << (int) RID_STATIC
3634 | 1 << (int) RID_EXTERN)) == (1 << (int) RID_THREAD))
3635 nclasses++;
3637 /* Warn about storage classes that are invalid for certain
3638 kinds of declarations (parameters, typenames, etc.). */
3640 if (nclasses > 1)
3641 error ("multiple storage classes in declaration of `%s'", name);
3642 else if (funcdef_flag
3643 && (specbits
3644 & ((1 << (int) RID_REGISTER)
3645 | (1 << (int) RID_AUTO)
3646 | (1 << (int) RID_TYPEDEF)
3647 | (1 << (int) RID_THREAD))))
3649 if (specbits & 1 << (int) RID_AUTO
3650 && (pedantic || current_scope == global_scope))
3651 pedwarn ("function definition declared `auto'");
3652 if (specbits & 1 << (int) RID_REGISTER)
3653 error ("function definition declared `register'");
3654 if (specbits & 1 << (int) RID_TYPEDEF)
3655 error ("function definition declared `typedef'");
3656 if (specbits & 1 << (int) RID_THREAD)
3657 error ("function definition declared `__thread'");
3658 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3659 | (1 << (int) RID_AUTO) | (1 << (int) RID_THREAD));
3661 else if (decl_context != NORMAL && nclasses > 0)
3663 if (decl_context == PARM && specbits & 1 << (int) RID_REGISTER)
3665 else
3667 switch (decl_context)
3669 case FIELD:
3670 error ("storage class specified for structure field `%s'",
3671 name);
3672 break;
3673 case PARM:
3674 error ("storage class specified for parameter `%s'", name);
3675 break;
3676 default:
3677 error ("storage class specified for typename");
3678 break;
3680 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3681 | (1 << (int) RID_AUTO) | (1 << (int) RID_STATIC)
3682 | (1 << (int) RID_EXTERN) | (1 << (int) RID_THREAD));
3685 else if (specbits & 1 << (int) RID_EXTERN && initialized && ! funcdef_flag)
3687 /* `extern' with initialization is invalid if not at file scope. */
3688 if (current_scope == global_scope)
3689 warning ("`%s' initialized and declared `extern'", name);
3690 else
3691 error ("`%s' has both `extern' and initializer", name);
3693 else if (current_scope == global_scope)
3695 if (specbits & 1 << (int) RID_AUTO)
3696 error ("file-scope declaration of `%s' specifies `auto'", name);
3698 else
3700 if (specbits & 1 << (int) RID_EXTERN && funcdef_flag)
3701 error ("nested function `%s' declared `extern'", name);
3702 else if ((specbits & (1 << (int) RID_THREAD
3703 | 1 << (int) RID_EXTERN
3704 | 1 << (int) RID_STATIC))
3705 == (1 << (int) RID_THREAD))
3707 error ("function-scope `%s' implicitly auto and declared `__thread'",
3708 name);
3709 specbits &= ~(1 << (int) RID_THREAD);
3714 /* Now figure out the structure of the declarator proper.
3715 Descend through it, creating more complex types, until we reach
3716 the declared identifier (or NULL_TREE, in an absolute declarator). */
3718 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE)
3720 if (type == error_mark_node)
3722 declarator = TREE_OPERAND (declarator, 0);
3723 continue;
3726 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
3727 an INDIRECT_REF (for *...),
3728 a CALL_EXPR (for ...(...)),
3729 a TREE_LIST (for nested attributes),
3730 an identifier (for the name being declared)
3731 or a null pointer (for the place in an absolute declarator
3732 where the name was omitted).
3733 For the last two cases, we have just exited the loop.
3735 At this point, TYPE is the type of elements of an array,
3736 or for a function to return, or for a pointer to point to.
3737 After this sequence of ifs, TYPE is the type of the
3738 array or function or pointer, and DECLARATOR has had its
3739 outermost layer removed. */
3741 if (array_ptr_quals != NULL_TREE || array_parm_static)
3743 /* Only the innermost declarator (making a parameter be of
3744 array type which is converted to pointer type)
3745 may have static or type qualifiers. */
3746 error ("static or type qualifiers in non-parameter array declarator");
3747 array_ptr_quals = NULL_TREE;
3748 array_parm_static = 0;
3751 if (TREE_CODE (declarator) == TREE_LIST)
3753 /* We encode a declarator with embedded attributes using
3754 a TREE_LIST. */
3755 tree attrs = TREE_PURPOSE (declarator);
3756 tree inner_decl;
3757 int attr_flags = 0;
3758 declarator = TREE_VALUE (declarator);
3759 inner_decl = declarator;
3760 while (inner_decl != NULL_TREE
3761 && TREE_CODE (inner_decl) == TREE_LIST)
3762 inner_decl = TREE_VALUE (inner_decl);
3763 if (inner_decl == NULL_TREE
3764 || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
3765 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
3766 else if (TREE_CODE (inner_decl) == CALL_EXPR)
3767 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
3768 else if (TREE_CODE (inner_decl) == ARRAY_REF)
3769 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
3770 returned_attrs = decl_attributes (&type,
3771 chainon (returned_attrs, attrs),
3772 attr_flags);
3774 else if (TREE_CODE (declarator) == ARRAY_REF)
3776 tree itype = NULL_TREE;
3777 tree size = TREE_OPERAND (declarator, 1);
3778 /* The index is a signed object `sizetype' bits wide. */
3779 tree index_type = c_common_signed_type (sizetype);
3781 array_ptr_quals = TREE_TYPE (declarator);
3782 array_parm_static = TREE_STATIC (declarator);
3784 declarator = TREE_OPERAND (declarator, 0);
3786 /* Check for some types that there cannot be arrays of. */
3788 if (VOID_TYPE_P (type))
3790 error ("declaration of `%s' as array of voids", name);
3791 type = error_mark_node;
3794 if (TREE_CODE (type) == FUNCTION_TYPE)
3796 error ("declaration of `%s' as array of functions", name);
3797 type = error_mark_node;
3800 if (pedantic && flexible_array_type_p (type))
3801 pedwarn ("invalid use of structure with flexible array member");
3803 if (size == error_mark_node)
3804 type = error_mark_node;
3806 if (type == error_mark_node)
3807 continue;
3809 /* If size was specified, set ITYPE to a range-type for that size.
3810 Otherwise, ITYPE remains null. finish_decl may figure it out
3811 from an initial value. */
3813 if (size)
3815 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3816 STRIP_TYPE_NOPS (size);
3818 if (! INTEGRAL_TYPE_P (TREE_TYPE (size)))
3820 error ("size of array `%s' has non-integer type", name);
3821 size = integer_one_node;
3824 if (pedantic && integer_zerop (size))
3825 pedwarn ("ISO C forbids zero-size array `%s'", name);
3827 if (TREE_CODE (size) == INTEGER_CST)
3829 constant_expression_warning (size);
3830 if (tree_int_cst_sgn (size) < 0)
3832 error ("size of array `%s' is negative", name);
3833 size = integer_one_node;
3836 else
3838 /* Make sure the array size remains visibly nonconstant
3839 even if it is (eg) a const variable with known value. */
3840 size_varies = 1;
3842 if (!flag_isoc99 && pedantic)
3844 if (TREE_CONSTANT (size))
3845 pedwarn ("ISO C90 forbids array `%s' whose size can't be evaluated",
3846 name);
3847 else
3848 pedwarn ("ISO C90 forbids variable-size array `%s'",
3849 name);
3853 if (integer_zerop (size))
3855 /* A zero-length array cannot be represented with an
3856 unsigned index type, which is what we'll get with
3857 build_index_type. Create an open-ended range instead. */
3858 itype = build_range_type (sizetype, size, NULL_TREE);
3860 else
3862 /* Compute the maximum valid index, that is, size - 1.
3863 Do the calculation in index_type, so that if it is
3864 a variable the computations will be done in the
3865 proper mode. */
3866 itype = fold (build (MINUS_EXPR, index_type,
3867 convert (index_type, size),
3868 convert (index_type, size_one_node)));
3870 /* If that overflowed, the array is too big.
3871 ??? While a size of INT_MAX+1 technically shouldn't
3872 cause an overflow (because we subtract 1), the overflow
3873 is recorded during the conversion to index_type, before
3874 the subtraction. Handling this case seems like an
3875 unnecessary complication. */
3876 if (TREE_OVERFLOW (itype))
3878 error ("size of array `%s' is too large", name);
3879 type = error_mark_node;
3880 continue;
3883 if (size_varies)
3885 /* We must be able to distinguish the
3886 SAVE_EXPR_CONTEXT for the variably-sized type
3887 so that we can set it correctly in
3888 set_save_expr_context. The convention is
3889 that all SAVE_EXPRs that need to be reset
3890 have NULL_TREE for their SAVE_EXPR_CONTEXT. */
3891 tree cfd = current_function_decl;
3892 if (decl_context == PARM)
3893 current_function_decl = NULL_TREE;
3894 itype = variable_size (itype);
3895 if (decl_context == PARM)
3896 current_function_decl = cfd;
3898 itype = build_index_type (itype);
3901 else if (decl_context == FIELD)
3903 if (pedantic && !flag_isoc99 && !in_system_header)
3904 pedwarn ("ISO C90 does not support flexible array members");
3906 /* ISO C99 Flexible array members are effectively identical
3907 to GCC's zero-length array extension. */
3908 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
3911 /* If pedantic, complain about arrays of incomplete types. */
3913 if (pedantic && !COMPLETE_TYPE_P (type))
3914 pedwarn ("array type has incomplete element type");
3916 /* Build the array type itself, then merge any constancy or
3917 volatility into the target type. We must do it in this order
3918 to ensure that the TYPE_MAIN_VARIANT field of the array type
3919 is set correctly. */
3921 type = build_array_type (type, itype);
3922 if (type_quals)
3923 type = c_build_qualified_type (type, type_quals);
3925 if (size_varies)
3926 C_TYPE_VARIABLE_SIZE (type) = 1;
3928 /* The GCC extension for zero-length arrays differs from
3929 ISO flexible array members in that sizeof yields zero. */
3930 if (size && integer_zerop (size))
3932 layout_type (type);
3933 TYPE_SIZE (type) = bitsize_zero_node;
3934 TYPE_SIZE_UNIT (type) = size_zero_node;
3936 if (decl_context != PARM
3937 && (array_ptr_quals != NULL_TREE || array_parm_static))
3939 error ("static or type qualifiers in non-parameter array declarator");
3940 array_ptr_quals = NULL_TREE;
3941 array_parm_static = 0;
3944 else if (TREE_CODE (declarator) == CALL_EXPR)
3946 tree arg_types;
3948 /* Declaring a function type.
3949 Make sure we have a valid type for the function to return. */
3950 if (type == error_mark_node)
3951 continue;
3953 size_varies = 0;
3955 /* Warn about some types functions can't return. */
3957 if (TREE_CODE (type) == FUNCTION_TYPE)
3959 error ("`%s' declared as function returning a function", name);
3960 type = integer_type_node;
3962 if (TREE_CODE (type) == ARRAY_TYPE)
3964 error ("`%s' declared as function returning an array", name);
3965 type = integer_type_node;
3968 /* Construct the function type and go to the next
3969 inner layer of declarator. */
3971 arg_types = grokparms (TREE_OPERAND (declarator, 1),
3972 funcdef_flag
3973 /* Say it's a definition
3974 only for the CALL_EXPR
3975 closest to the identifier. */
3976 && TREE_CODE (TREE_OPERAND (declarator, 0)) == IDENTIFIER_NODE);
3977 /* Type qualifiers before the return type of the function
3978 qualify the return type, not the function type. */
3979 if (type_quals)
3981 /* Type qualifiers on a function return type are normally
3982 permitted by the standard but have no effect, so give a
3983 warning at -Wextra. Qualifiers on a void return type have
3984 meaning as a GNU extension, and are banned on function
3985 definitions in ISO C. FIXME: strictly we shouldn't
3986 pedwarn for qualified void return types except on function
3987 definitions, but not doing so could lead to the undesirable
3988 state of a "volatile void" function return type not being
3989 warned about, and a use of the function being compiled
3990 with GNU semantics, with no diagnostics under -pedantic. */
3991 if (VOID_TYPE_P (type) && pedantic && !in_system_header)
3992 pedwarn ("ISO C forbids qualified void function return type");
3993 else if (extra_warnings
3994 && !(VOID_TYPE_P (type)
3995 && type_quals == TYPE_QUAL_VOLATILE))
3996 warning ("type qualifiers ignored on function return type");
3998 type = c_build_qualified_type (type, type_quals);
4000 type_quals = TYPE_UNQUALIFIED;
4002 type = build_function_type (type, arg_types);
4003 declarator = TREE_OPERAND (declarator, 0);
4005 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4006 the formal parameter list of this FUNCTION_TYPE to point to
4007 the FUNCTION_TYPE node itself. */
4010 tree link;
4012 for (link = last_function_parm_tags;
4013 link;
4014 link = TREE_CHAIN (link))
4015 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4018 else if (TREE_CODE (declarator) == INDIRECT_REF)
4020 /* Merge any constancy or volatility into the target type
4021 for the pointer. */
4023 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4024 && type_quals)
4025 pedwarn ("ISO C forbids qualified function types");
4026 if (type_quals)
4027 type = c_build_qualified_type (type, type_quals);
4028 type_quals = TYPE_UNQUALIFIED;
4029 size_varies = 0;
4031 type = build_pointer_type (type);
4033 /* Process a list of type modifier keywords
4034 (such as const or volatile) that were given inside the `*'. */
4036 if (TREE_TYPE (declarator))
4038 tree typemodlist;
4039 int erred = 0;
4041 constp = 0;
4042 volatilep = 0;
4043 restrictp = 0;
4044 for (typemodlist = TREE_TYPE (declarator); typemodlist;
4045 typemodlist = TREE_CHAIN (typemodlist))
4047 tree qualifier = TREE_VALUE (typemodlist);
4049 if (C_IS_RESERVED_WORD (qualifier))
4051 if (C_RID_CODE (qualifier) == RID_CONST)
4052 constp++;
4053 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4054 volatilep++;
4055 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4056 restrictp++;
4057 else
4058 erred++;
4060 else
4061 erred++;
4064 if (erred)
4065 error ("invalid type modifier within pointer declarator");
4066 if (constp > 1 && ! flag_isoc99)
4067 pedwarn ("duplicate `const'");
4068 if (volatilep > 1 && ! flag_isoc99)
4069 pedwarn ("duplicate `volatile'");
4070 if (restrictp > 1 && ! flag_isoc99)
4071 pedwarn ("duplicate `restrict'");
4073 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4074 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4075 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4078 declarator = TREE_OPERAND (declarator, 0);
4080 else
4081 abort ();
4085 /* Now TYPE has the actual type. */
4087 /* Did array size calculations overflow? */
4089 if (TREE_CODE (type) == ARRAY_TYPE
4090 && COMPLETE_TYPE_P (type)
4091 && TREE_OVERFLOW (TYPE_SIZE (type)))
4093 error ("size of array `%s' is too large", name);
4094 /* If we proceed with the array type as it is, we'll eventually
4095 crash in tree_low_cst(). */
4096 type = error_mark_node;
4099 /* If this is declaring a typedef name, return a TYPE_DECL. */
4101 if (specbits & (1 << (int) RID_TYPEDEF))
4103 tree decl;
4104 /* Note that the grammar rejects storage classes
4105 in typenames, fields or parameters */
4106 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4107 && type_quals)
4108 pedwarn ("ISO C forbids qualified function types");
4109 if (type_quals)
4110 type = c_build_qualified_type (type, type_quals);
4111 decl = build_decl (TYPE_DECL, declarator, type);
4112 if ((specbits & (1 << (int) RID_SIGNED))
4113 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
4114 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4115 decl_attributes (&decl, returned_attrs, 0);
4116 return decl;
4119 /* Detect the case of an array type of unspecified size
4120 which came, as such, direct from a typedef name.
4121 We must copy the type, so that each identifier gets
4122 a distinct type, so that each identifier's size can be
4123 controlled separately by its own initializer. */
4125 if (type != 0 && typedef_type != 0
4126 && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0
4127 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
4129 type = build_array_type (TREE_TYPE (type), 0);
4130 if (size_varies)
4131 C_TYPE_VARIABLE_SIZE (type) = 1;
4134 /* If this is a type name (such as, in a cast or sizeof),
4135 compute the type and return it now. */
4137 if (decl_context == TYPENAME)
4139 /* Note that the grammar rejects storage classes
4140 in typenames, fields or parameters */
4141 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4142 && type_quals)
4143 pedwarn ("ISO C forbids const or volatile function types");
4144 if (type_quals)
4145 type = c_build_qualified_type (type, type_quals);
4146 decl_attributes (&type, returned_attrs, 0);
4147 return type;
4150 /* Aside from typedefs and type names (handle above),
4151 `void' at top level (not within pointer)
4152 is allowed only in public variables.
4153 We don't complain about parms either, but that is because
4154 a better error message can be made later. */
4156 if (VOID_TYPE_P (type) && decl_context != PARM
4157 && ! ((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4158 && ((specbits & (1 << (int) RID_EXTERN))
4159 || (current_scope == global_scope
4160 && !(specbits
4161 & ((1 << (int) RID_STATIC) | (1 << (int) RID_REGISTER)))))))
4163 error ("variable or field `%s' declared void", name);
4164 type = integer_type_node;
4167 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4168 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4171 tree decl;
4173 if (decl_context == PARM)
4175 tree type_as_written;
4176 tree promoted_type;
4178 /* A parameter declared as an array of T is really a pointer to T.
4179 One declared as a function is really a pointer to a function. */
4181 if (TREE_CODE (type) == ARRAY_TYPE)
4183 /* Transfer const-ness of array into that of type pointed to. */
4184 type = TREE_TYPE (type);
4185 if (type_quals)
4186 type = c_build_qualified_type (type, type_quals);
4187 type = build_pointer_type (type);
4188 type_quals = TYPE_UNQUALIFIED;
4189 if (array_ptr_quals)
4191 tree new_ptr_quals, new_ptr_attrs;
4192 int erred = 0;
4193 split_specs_attrs (array_ptr_quals, &new_ptr_quals, &new_ptr_attrs);
4194 /* We don't yet implement attributes in this context. */
4195 if (new_ptr_attrs != NULL_TREE)
4196 warning ("attributes in parameter array declarator ignored");
4198 constp = 0;
4199 volatilep = 0;
4200 restrictp = 0;
4201 for (; new_ptr_quals; new_ptr_quals = TREE_CHAIN (new_ptr_quals))
4203 tree qualifier = TREE_VALUE (new_ptr_quals);
4205 if (C_IS_RESERVED_WORD (qualifier))
4207 if (C_RID_CODE (qualifier) == RID_CONST)
4208 constp++;
4209 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4210 volatilep++;
4211 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4212 restrictp++;
4213 else
4214 erred++;
4216 else
4217 erred++;
4220 if (erred)
4221 error ("invalid type modifier within array declarator");
4223 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4224 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4225 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4227 size_varies = 0;
4229 else if (TREE_CODE (type) == FUNCTION_TYPE)
4231 if (pedantic && type_quals)
4232 pedwarn ("ISO C forbids qualified function types");
4233 if (type_quals)
4234 type = c_build_qualified_type (type, type_quals);
4235 type = build_pointer_type (type);
4236 type_quals = TYPE_UNQUALIFIED;
4238 else if (type_quals)
4239 type = c_build_qualified_type (type, type_quals);
4241 type_as_written = type;
4243 decl = build_decl (PARM_DECL, declarator, type);
4244 if (size_varies)
4245 C_DECL_VARIABLE_SIZE (decl) = 1;
4247 /* Compute the type actually passed in the parmlist,
4248 for the case where there is no prototype.
4249 (For example, shorts and chars are passed as ints.)
4250 When there is a prototype, this is overridden later. */
4252 if (type == error_mark_node)
4253 promoted_type = type;
4254 else
4255 promoted_type = c_type_promotes_to (type);
4257 DECL_ARG_TYPE (decl) = promoted_type;
4258 DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
4260 else if (decl_context == FIELD)
4262 /* Structure field. It may not be a function. */
4264 if (TREE_CODE (type) == FUNCTION_TYPE)
4266 error ("field `%s' declared as a function", name);
4267 type = build_pointer_type (type);
4269 else if (TREE_CODE (type) != ERROR_MARK
4270 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4272 error ("field `%s' has incomplete type", name);
4273 type = error_mark_node;
4275 /* Move type qualifiers down to element of an array. */
4276 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4277 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4278 type_quals),
4279 TYPE_DOMAIN (type));
4280 decl = build_decl (FIELD_DECL, declarator, type);
4281 DECL_NONADDRESSABLE_P (decl) = bitfield;
4283 if (size_varies)
4284 C_DECL_VARIABLE_SIZE (decl) = 1;
4286 else if (TREE_CODE (type) == FUNCTION_TYPE)
4288 /* Every function declaration is "external"
4289 except for those which are inside a function body
4290 in which `auto' is used.
4291 That is a case not specified by ANSI C,
4292 and we use it for forward declarations for nested functions. */
4293 int extern_ref = (!(specbits & (1 << (int) RID_AUTO))
4294 || current_scope == global_scope);
4296 if (specbits & (1 << (int) RID_AUTO)
4297 && (pedantic || current_scope == global_scope))
4298 pedwarn ("invalid storage class for function `%s'", name);
4299 if (specbits & (1 << (int) RID_REGISTER))
4300 error ("invalid storage class for function `%s'", name);
4301 if (specbits & (1 << (int) RID_THREAD))
4302 error ("invalid storage class for function `%s'", name);
4303 /* Function declaration not at file scope.
4304 Storage classes other than `extern' are not allowed
4305 and `extern' makes no difference. */
4306 if (current_scope != global_scope
4307 && (specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_INLINE)))
4308 && pedantic)
4309 pedwarn ("invalid storage class for function `%s'", name);
4311 decl = build_decl (FUNCTION_DECL, declarator, type);
4312 decl = build_decl_attribute_variant (decl, decl_attr);
4314 DECL_LANG_SPECIFIC (decl)
4315 = ggc_alloc_cleared (sizeof (struct lang_decl));
4317 if (pedantic && type_quals && ! DECL_IN_SYSTEM_HEADER (decl))
4318 pedwarn ("ISO C forbids qualified function types");
4320 /* GNU C interprets a `volatile void' return type to indicate
4321 that the function does not return. */
4322 if ((type_quals & TYPE_QUAL_VOLATILE)
4323 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4324 warning ("`noreturn' function returns non-void value");
4326 if (extern_ref)
4327 DECL_EXTERNAL (decl) = 1;
4328 /* Record absence of global scope for `static' or `auto'. */
4329 TREE_PUBLIC (decl)
4330 = !(specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_AUTO)));
4332 if (defaulted_int)
4333 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4335 /* Record presence of `inline', if it is reasonable. */
4336 if (MAIN_NAME_P (declarator))
4338 if (inlinep)
4339 warning ("cannot inline function `main'");
4341 else if (inlinep)
4343 /* Record that the function is declared `inline'. */
4344 DECL_DECLARED_INLINE_P (decl) = 1;
4346 /* Do not mark bare declarations as DECL_INLINE. Doing so
4347 in the presence of multiple declarations can result in
4348 the abstract origin pointing between the declarations,
4349 which will confuse dwarf2out. */
4350 if (initialized)
4352 DECL_INLINE (decl) = 1;
4353 if (specbits & (1 << (int) RID_EXTERN))
4354 current_extern_inline = 1;
4357 /* If -finline-functions, assume it can be inlined. This does
4358 two things: let the function be deferred until it is actually
4359 needed, and let dwarf2 know that the function is inlinable. */
4360 else if (flag_inline_trees == 2 && initialized)
4361 DECL_INLINE (decl) = 1;
4363 else
4365 /* It's a variable. */
4366 /* An uninitialized decl with `extern' is a reference. */
4367 int extern_ref = !initialized && (specbits & (1 << (int) RID_EXTERN));
4369 /* Move type qualifiers down to element of an array. */
4370 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4372 int saved_align = TYPE_ALIGN(type);
4373 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4374 type_quals),
4375 TYPE_DOMAIN (type));
4376 TYPE_ALIGN (type) = saved_align;
4378 else if (type_quals)
4379 type = c_build_qualified_type (type, type_quals);
4381 /* It is invalid to create an `extern' declaration for a
4382 variable if there is a global declaration that is
4383 `static'. */
4384 if (extern_ref && current_scope != global_scope)
4386 tree global_decl;
4388 global_decl = identifier_global_value (declarator);
4389 if (global_decl
4390 && TREE_CODE (global_decl) == VAR_DECL
4391 && !TREE_PUBLIC (global_decl))
4392 error ("variable previously declared `static' redeclared "
4393 "`extern'");
4396 decl = build_decl (VAR_DECL, declarator, type);
4397 if (size_varies)
4398 C_DECL_VARIABLE_SIZE (decl) = 1;
4400 if (inlinep)
4401 pedwarn ("%Jvariable '%D' declared `inline'", decl, decl);
4403 DECL_EXTERNAL (decl) = extern_ref;
4405 /* At file scope, the presence of a `static' or `register' storage
4406 class specifier, or the absence of all storage class specifiers
4407 makes this declaration a definition (perhaps tentative). Also,
4408 the absence of both `static' and `register' makes it public. */
4409 if (current_scope == global_scope)
4411 TREE_PUBLIC (decl) = !(specbits & ((1 << (int) RID_STATIC)
4412 | (1 << (int) RID_REGISTER)));
4413 TREE_STATIC (decl) = !extern_ref;
4415 /* Not at file scope, only `static' makes a static definition. */
4416 else
4418 TREE_STATIC (decl) = (specbits & (1 << (int) RID_STATIC)) != 0;
4419 TREE_PUBLIC (decl) = extern_ref;
4422 if (specbits & 1 << (int) RID_THREAD)
4424 if (targetm.have_tls)
4425 DECL_THREAD_LOCAL (decl) = 1;
4426 else
4427 /* A mere warning is sure to result in improper semantics
4428 at runtime. Don't bother to allow this to compile. */
4429 error ("thread-local storage not supported for this target");
4433 /* Record `register' declaration for warnings on &
4434 and in case doing stupid register allocation. */
4436 if (specbits & (1 << (int) RID_REGISTER))
4437 DECL_REGISTER (decl) = 1;
4439 /* Record constancy and volatility. */
4440 c_apply_type_quals_to_decl (type_quals, decl);
4442 /* If a type has volatile components, it should be stored in memory.
4443 Otherwise, the fact that those components are volatile
4444 will be ignored, and would even crash the compiler. */
4445 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
4446 c_mark_addressable (decl);
4448 #ifdef ENABLE_CHECKING
4449 /* This is the earliest point at which we might know the assembler
4450 name of a variable. Thus, if it's known before this, die horribly. */
4451 if (DECL_ASSEMBLER_NAME_SET_P (decl))
4452 abort ();
4453 #endif
4455 decl_attributes (&decl, returned_attrs, 0);
4457 return decl;
4461 /* Decode the parameter-list info for a function type or function definition.
4462 The argument is the value returned by `get_parm_info' (or made in parse.y
4463 if there is an identifier list instead of a parameter decl list).
4464 These two functions are separate because when a function returns
4465 or receives functions then each is called multiple times but the order
4466 of calls is different. The last call to `grokparms' is always the one
4467 that contains the formal parameter names of a function definition.
4469 Store in `last_function_parms' a chain of the decls of parms.
4470 Also store in `last_function_parm_tags' a chain of the struct, union,
4471 and enum tags declared among the parms.
4473 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4475 FUNCDEF_FLAG is nonzero for a function definition, 0 for
4476 a mere declaration. A nonempty identifier-list gets an error message
4477 when FUNCDEF_FLAG is zero. */
4479 static tree
4480 grokparms (tree parms_info, int funcdef_flag)
4482 tree first_parm = TREE_CHAIN (parms_info);
4484 last_function_parms = TREE_PURPOSE (parms_info);
4485 last_function_parm_tags = TREE_VALUE (parms_info);
4486 last_function_parm_others = TREE_TYPE (parms_info);
4488 if (warn_strict_prototypes && first_parm == 0 && !funcdef_flag
4489 && !in_system_header)
4490 warning ("function declaration isn't a prototype");
4492 if (first_parm != 0
4493 && TREE_CODE (TREE_VALUE (first_parm)) == IDENTIFIER_NODE)
4495 if (! funcdef_flag)
4496 pedwarn ("parameter names (without types) in function declaration");
4498 last_function_parms = first_parm;
4499 return 0;
4501 else
4503 tree parm;
4504 tree typelt;
4505 /* If the arg types are incomplete in a declaration,
4506 they must include undefined tags.
4507 These tags can never be defined in the scope of the declaration,
4508 so the types can never be completed,
4509 and no call can be compiled successfully. */
4511 for (parm = last_function_parms, typelt = first_parm;
4512 parm;
4513 parm = TREE_CHAIN (parm))
4514 /* Skip over any enumeration constants declared here. */
4515 if (TREE_CODE (parm) == PARM_DECL)
4517 /* Barf if the parameter itself has an incomplete type. */
4518 tree type = TREE_VALUE (typelt);
4519 if (type == error_mark_node)
4520 continue;
4521 if (!COMPLETE_TYPE_P (type))
4523 if (funcdef_flag && DECL_NAME (parm) != 0)
4524 error ("parameter `%s' has incomplete type",
4525 IDENTIFIER_POINTER (DECL_NAME (parm)));
4526 else
4527 warning ("parameter has incomplete type");
4528 if (funcdef_flag)
4530 TREE_VALUE (typelt) = error_mark_node;
4531 TREE_TYPE (parm) = error_mark_node;
4534 typelt = TREE_CHAIN (typelt);
4537 return first_parm;
4541 /* Return a tree_list node with info on a parameter list just parsed.
4542 The TREE_PURPOSE is a list of decls of those parms.
4543 The TREE_VALUE is a list of structure, union and enum tags defined.
4544 The TREE_CHAIN is a list of argument types to go in the FUNCTION_TYPE.
4545 The TREE_TYPE is a list of non-parameter decls which appeared with the
4546 parameters.
4547 This tree_list node is later fed to `grokparms'.
4549 VOID_AT_END nonzero means append `void' to the end of the type-list.
4550 Zero means the parmlist ended with an ellipsis so don't append `void'. */
4552 tree
4553 get_parm_info (int void_at_end)
4555 tree decl, type, list;
4556 tree types = 0;
4557 tree *last_type = &types;
4558 tree tags = current_scope->tags;
4559 tree parms = current_scope->parms;
4560 tree others = current_scope->names;
4561 static bool explained_incomplete_types = false;
4562 bool gave_void_only_once_err = false;
4564 /* Just "void" (and no ellipsis) is special. There are really no parms.
4565 But if the "void" is qualified (by "const" or "volatile"), or has a
4566 storage class specifier ("register"), then the behavior is undefined;
4567 issue an error. Typedefs for "void" are OK (see DR#157). */
4568 if (void_at_end && parms != 0
4569 && TREE_CHAIN (parms) == 0
4570 && VOID_TYPE_P (TREE_TYPE (parms))
4571 && !DECL_NAME (parms))
4573 if (TREE_THIS_VOLATILE (parms)
4574 || TREE_READONLY (parms)
4575 || DECL_REGISTER (parms))
4576 error ("\"void\" as only parameter may not be qualified");
4578 return tree_cons (0, 0, tree_cons (0, void_type_node, 0));
4581 /* Sanity check all of the parameter declarations. */
4582 for (decl = parms; decl; decl = TREE_CHAIN (decl))
4584 if (TREE_CODE (decl) != PARM_DECL)
4585 abort ();
4586 if (TREE_ASM_WRITTEN (decl))
4587 abort ();
4589 /* Since there is a prototype, args are passed in their
4590 declared types. The back end may override this. */
4591 type = TREE_TYPE (decl);
4592 DECL_ARG_TYPE (decl) = type;
4594 /* Check for (..., void, ...) and issue an error. */
4595 if (VOID_TYPE_P (type) && !DECL_NAME (decl) && !gave_void_only_once_err)
4597 error ("\"void\" must be the only parameter");
4598 gave_void_only_once_err = true;
4601 type = build_tree_list (0, type);
4602 *last_type = type;
4603 last_type = &TREE_CHAIN (type);
4606 /* Check the list of non-parameter decls for any forward parm decls
4607 that never got real decls. */
4608 for (decl = others; decl; decl = TREE_CHAIN (decl))
4609 if (TREE_CODE (decl) == PARM_DECL)
4611 if (!TREE_ASM_WRITTEN (decl))
4612 abort ();
4614 error ("%Jparameter \"%D\" has just a forward declaration",
4615 decl, decl);
4618 /* Warn about any struct, union or enum tags defined within this
4619 list. The scope of such types is limited to this declaration,
4620 which is rarely if ever desirable (it's impossible to call such
4621 a function with type-correct arguments). */
4622 for (decl = tags; decl; decl = TREE_CHAIN (decl))
4624 enum tree_code code = TREE_CODE (TREE_VALUE (decl));
4625 const char *keyword;
4626 /* An anonymous union parm type is meaningful as a GNU extension.
4627 So don't warn for that. */
4628 if (code == UNION_TYPE && TREE_PURPOSE (decl) == 0 && !pedantic)
4629 continue;
4631 /* The keyword should not be translated. */
4632 switch (code)
4634 case RECORD_TYPE: keyword = "struct"; break;
4635 case UNION_TYPE: keyword = "union"; break;
4636 case ENUMERAL_TYPE: keyword = "enum"; break;
4637 default: abort ();
4640 if (TREE_PURPOSE (decl))
4641 /* The first %s will be one of 'struct', 'union', or 'enum'. */
4642 warning ("\"%s %s\" declared inside parameter list",
4643 keyword, IDENTIFIER_POINTER (TREE_PURPOSE (decl)));
4644 else
4645 /* The %s will be one of 'struct', 'union', or 'enum'. */
4646 warning ("anonymous %s declared inside parameter list", keyword);
4648 if (! explained_incomplete_types)
4650 warning ("its scope is only this definition or declaration,"
4651 " which is probably not what you want");
4652 explained_incomplete_types = true;
4657 if (void_at_end)
4659 type = build_tree_list (0, void_type_node);
4660 *last_type = type;
4663 list = tree_cons (parms, tags, types);
4664 TREE_TYPE (list) = others;
4665 return list;
4668 /* Get the struct, enum or union (CODE says which) with tag NAME.
4669 Define the tag as a forward-reference if it is not defined. */
4671 tree
4672 xref_tag (enum tree_code code, tree name)
4674 /* If a cross reference is requested, look up the type
4675 already defined for this tag and return it. */
4677 tree ref = lookup_tag (code, name, 0);
4678 /* If this is the right type of tag, return what we found.
4679 (This reference will be shadowed by shadow_tag later if appropriate.)
4680 If this is the wrong type of tag, do not return it. If it was the
4681 wrong type in the same scope, we will have had an error
4682 message already; if in a different scope and declaring
4683 a name, pending_xref_error will give an error message; but if in a
4684 different scope and not declaring a name, this tag should
4685 shadow the previous declaration of a different type of tag, and
4686 this would not work properly if we return the reference found.
4687 (For example, with "struct foo" in an outer scope, "union foo;"
4688 must shadow that tag with a new one of union type.) */
4689 if (ref && TREE_CODE (ref) == code)
4690 return ref;
4692 /* If no such tag is yet defined, create a forward-reference node
4693 and record it as the "definition".
4694 When a real declaration of this type is found,
4695 the forward-reference will be altered into a real type. */
4697 ref = make_node (code);
4698 if (code == ENUMERAL_TYPE)
4700 /* Give the type a default layout like unsigned int
4701 to avoid crashing if it does not get defined. */
4702 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
4703 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
4704 TYPE_USER_ALIGN (ref) = 0;
4705 TREE_UNSIGNED (ref) = 1;
4706 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
4707 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
4708 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
4711 pushtag (name, ref);
4713 return ref;
4716 /* Make sure that the tag NAME is defined *in the current scope*
4717 at least as a forward reference.
4718 CODE says which kind of tag NAME ought to be. */
4720 tree
4721 start_struct (enum tree_code code, tree name)
4723 /* If there is already a tag defined at this scope
4724 (as a forward reference), just return it. */
4726 tree ref = 0;
4728 if (name != 0)
4729 ref = lookup_tag (code, name, 1);
4730 if (ref && TREE_CODE (ref) == code)
4732 if (TYPE_FIELDS (ref))
4734 if (code == UNION_TYPE)
4735 error ("redefinition of `union %s'", IDENTIFIER_POINTER (name));
4736 else
4737 error ("redefinition of `struct %s'", IDENTIFIER_POINTER (name));
4740 else
4742 /* Otherwise create a forward-reference just so the tag is in scope. */
4744 ref = make_node (code);
4745 pushtag (name, ref);
4748 C_TYPE_BEING_DEFINED (ref) = 1;
4749 TYPE_PACKED (ref) = flag_pack_struct;
4750 return ref;
4753 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
4754 of a structure component, returning a FIELD_DECL node.
4755 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node.
4757 This is done during the parsing of the struct declaration.
4758 The FIELD_DECL nodes are chained together and the lot of them
4759 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
4761 tree
4762 grokfield (tree declarator, tree declspecs, tree width)
4764 tree value;
4766 if (declarator == NULL_TREE && width == NULL_TREE)
4768 /* This is an unnamed decl.
4770 If we have something of the form "union { list } ;" then this
4771 is the anonymous union extension. Similarly for struct.
4773 If this is something of the form "struct foo;", then
4774 If MS extensions are enabled, this is handled as an
4775 anonymous struct.
4776 Otherwise this is a forward declaration of a structure tag.
4778 If this is something of the form "foo;" and foo is a TYPE_DECL, then
4779 If MS extensions are enabled and foo names a structure, then
4780 again this is an anonymous struct.
4781 Otherwise this is an error.
4783 Oh what a horrid tangled web we weave. I wonder if MS consciously
4784 took this from Plan 9 or if it was an accident of implementation
4785 that took root before someone noticed the bug... */
4787 tree type = TREE_VALUE (declspecs);
4789 if (flag_ms_extensions && TREE_CODE (type) == TYPE_DECL)
4790 type = TREE_TYPE (type);
4791 if (TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE)
4793 if (flag_ms_extensions)
4794 ; /* ok */
4795 else if (flag_iso)
4796 goto warn_unnamed_field;
4797 else if (TYPE_NAME (type) == NULL)
4798 ; /* ok */
4799 else
4800 goto warn_unnamed_field;
4802 else
4804 warn_unnamed_field:
4805 warning ("declaration does not declare anything");
4806 return NULL_TREE;
4810 value = grokdeclarator (declarator, declspecs, width ? BITFIELD : FIELD, 0);
4812 finish_decl (value, NULL_TREE, NULL_TREE);
4813 DECL_INITIAL (value) = width;
4815 return value;
4818 /* Generate an error for any duplicate field names in FIELDLIST. Munge
4819 the list such that this does not present a problem later. */
4821 static void
4822 detect_field_duplicates (tree fieldlist)
4824 tree x, y;
4825 int timeout = 10;
4827 /* First, see if there are more than "a few" fields.
4828 This is trivially true if there are zero or one fields. */
4829 if (!fieldlist)
4830 return;
4831 x = TREE_CHAIN (fieldlist);
4832 if (!x)
4833 return;
4834 do {
4835 timeout--;
4836 x = TREE_CHAIN (x);
4837 } while (timeout > 0 && x);
4839 /* If there were "few" fields, avoid the overhead of allocating
4840 a hash table. Instead just do the nested traversal thing. */
4841 if (timeout > 0)
4843 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
4844 if (DECL_NAME (x))
4846 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
4847 if (DECL_NAME (y) == DECL_NAME (x))
4849 error ("%Jduplicate member '%D'", x, x);
4850 DECL_NAME (x) = NULL_TREE;
4854 else
4856 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
4857 void **slot;
4859 for (x = fieldlist; x ; x = TREE_CHAIN (x))
4860 if ((y = DECL_NAME (x)) != 0)
4862 slot = htab_find_slot (htab, y, INSERT);
4863 if (*slot)
4865 error ("%Jduplicate member '%D'", x, x);
4866 DECL_NAME (x) = NULL_TREE;
4868 *slot = y;
4871 htab_delete (htab);
4875 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
4876 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
4877 ATTRIBUTES are attributes to be applied to the structure. */
4879 tree
4880 finish_struct (tree t, tree fieldlist, tree attributes)
4882 tree x;
4883 int toplevel = global_scope == current_scope;
4884 int saw_named_field;
4886 /* If this type was previously laid out as a forward reference,
4887 make sure we lay it out again. */
4889 TYPE_SIZE (t) = 0;
4891 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
4893 /* Nameless union parm types are useful as GCC extension. */
4894 if (! (TREE_CODE (t) == UNION_TYPE && TYPE_NAME (t) == 0) && !pedantic)
4895 /* Otherwise, warn about any struct or union def. in parmlist. */
4896 if (in_parm_level_p ())
4898 if (pedantic)
4899 pedwarn ("%s defined inside parms",
4900 TREE_CODE (t) == UNION_TYPE ? _("union") : _("structure"));
4901 else
4902 warning ("%s defined inside parms",
4903 TREE_CODE (t) == UNION_TYPE ? _("union") : _("structure"));
4906 if (pedantic)
4908 for (x = fieldlist; x; x = TREE_CHAIN (x))
4909 if (DECL_NAME (x) != 0)
4910 break;
4912 if (x == 0)
4913 pedwarn ("%s has no %s",
4914 TREE_CODE (t) == UNION_TYPE ? _("union") : _("struct"),
4915 fieldlist ? _("named members") : _("members"));
4918 /* Install struct as DECL_CONTEXT of each field decl.
4919 Also process specified field sizes,m which is found in the DECL_INITIAL.
4920 Store 0 there, except for ": 0" fields (so we can find them
4921 and delete them, below). */
4923 saw_named_field = 0;
4924 for (x = fieldlist; x; x = TREE_CHAIN (x))
4926 DECL_CONTEXT (x) = t;
4927 DECL_PACKED (x) |= TYPE_PACKED (t);
4929 /* If any field is const, the structure type is pseudo-const. */
4930 if (TREE_READONLY (x))
4931 C_TYPE_FIELDS_READONLY (t) = 1;
4932 else
4934 /* A field that is pseudo-const makes the structure likewise. */
4935 tree t1 = TREE_TYPE (x);
4936 while (TREE_CODE (t1) == ARRAY_TYPE)
4937 t1 = TREE_TYPE (t1);
4938 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
4939 && C_TYPE_FIELDS_READONLY (t1))
4940 C_TYPE_FIELDS_READONLY (t) = 1;
4943 /* Any field that is volatile means variables of this type must be
4944 treated in some ways as volatile. */
4945 if (TREE_THIS_VOLATILE (x))
4946 C_TYPE_FIELDS_VOLATILE (t) = 1;
4948 /* Any field of nominal variable size implies structure is too. */
4949 if (C_DECL_VARIABLE_SIZE (x))
4950 C_TYPE_VARIABLE_SIZE (t) = 1;
4952 /* Detect invalid nested redefinition. */
4953 if (TREE_TYPE (x) == t)
4954 error ("nested redefinition of `%s'",
4955 IDENTIFIER_POINTER (TYPE_NAME (t)));
4957 /* Detect invalid bit-field size. */
4958 if (DECL_INITIAL (x))
4959 STRIP_NOPS (DECL_INITIAL (x));
4960 if (DECL_INITIAL (x))
4962 if (TREE_CODE (DECL_INITIAL (x)) == INTEGER_CST)
4963 constant_expression_warning (DECL_INITIAL (x));
4964 else
4966 error ("%Jbit-field '%D' width not an integer constant", x, x);
4967 DECL_INITIAL (x) = NULL;
4971 /* Detect invalid bit-field type. */
4972 if (DECL_INITIAL (x)
4973 && TREE_CODE (TREE_TYPE (x)) != INTEGER_TYPE
4974 && TREE_CODE (TREE_TYPE (x)) != BOOLEAN_TYPE
4975 && TREE_CODE (TREE_TYPE (x)) != ENUMERAL_TYPE)
4977 error ("%Jbit-field '%D' has invalid type", x, x);
4978 DECL_INITIAL (x) = NULL;
4981 if (DECL_INITIAL (x) && pedantic
4982 && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != integer_type_node
4983 && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != unsigned_type_node
4984 && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != boolean_type_node
4985 /* Accept an enum that's equivalent to int or unsigned int. */
4986 && !(TREE_CODE (TREE_TYPE (x)) == ENUMERAL_TYPE
4987 && (TYPE_PRECISION (TREE_TYPE (x))
4988 == TYPE_PRECISION (integer_type_node))))
4989 pedwarn ("%Jbit-field '%D' type invalid in ISO C", x, x);
4991 /* Detect and ignore out of range field width and process valid
4992 field widths. */
4993 if (DECL_INITIAL (x))
4995 int max_width
4996 = (TYPE_MAIN_VARIANT (TREE_TYPE (x)) == boolean_type_node
4997 ? CHAR_TYPE_SIZE : TYPE_PRECISION (TREE_TYPE (x)));
4999 if (tree_int_cst_sgn (DECL_INITIAL (x)) < 0)
5000 error ("%Jnegative width in bit-field '%D'", x, x);
5001 else if (0 < compare_tree_int (DECL_INITIAL (x), max_width))
5002 pedwarn ("%Jwidth of '%D' exceeds its type", x, x);
5003 else if (integer_zerop (DECL_INITIAL (x)) && DECL_NAME (x) != 0)
5004 error ("%Jzero width for bit-field '%D'", x, x);
5005 else
5007 /* The test above has assured us that TREE_INT_CST_HIGH is 0. */
5008 unsigned HOST_WIDE_INT width
5009 = tree_low_cst (DECL_INITIAL (x), 1);
5011 if (TREE_CODE (TREE_TYPE (x)) == ENUMERAL_TYPE
5012 && (width < min_precision (TYPE_MIN_VALUE (TREE_TYPE (x)),
5013 TREE_UNSIGNED (TREE_TYPE (x)))
5014 || (width
5015 < min_precision (TYPE_MAX_VALUE (TREE_TYPE (x)),
5016 TREE_UNSIGNED (TREE_TYPE (x))))))
5017 warning ("%J'%D' is narrower than values of its type", x, x);
5019 DECL_SIZE (x) = bitsize_int (width);
5020 DECL_BIT_FIELD (x) = 1;
5021 SET_DECL_C_BIT_FIELD (x);
5025 DECL_INITIAL (x) = 0;
5027 /* Detect flexible array member in an invalid context. */
5028 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5029 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5030 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5031 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5033 if (TREE_CODE (t) == UNION_TYPE)
5034 error ("%Jflexible array member in union", x);
5035 else if (TREE_CHAIN (x) != NULL_TREE)
5036 error ("%Jflexible array member not at end of struct", x);
5037 else if (! saw_named_field)
5038 error ("%Jflexible array member in otherwise empty struct", x);
5041 if (pedantic && TREE_CODE (t) == RECORD_TYPE
5042 && flexible_array_type_p (TREE_TYPE (x)))
5043 pedwarn ("%Jinvalid use of structure with flexible array member", x);
5045 if (DECL_NAME (x))
5046 saw_named_field = 1;
5049 detect_field_duplicates (fieldlist);
5051 /* Now we have the nearly final fieldlist. Record it,
5052 then lay out the structure or union (including the fields). */
5054 TYPE_FIELDS (t) = fieldlist;
5056 layout_type (t);
5058 /* Delete all zero-width bit-fields from the fieldlist */
5060 tree *fieldlistp = &fieldlist;
5061 while (*fieldlistp)
5062 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp))
5063 *fieldlistp = TREE_CHAIN (*fieldlistp);
5064 else
5065 fieldlistp = &TREE_CHAIN (*fieldlistp);
5068 /* Now we have the truly final field list.
5069 Store it in this type and in the variants. */
5071 TYPE_FIELDS (t) = fieldlist;
5073 /* If there are lots of fields, sort so we can look through them fast.
5074 We arbitrarily consider 16 or more elts to be "a lot". */
5077 int len = 0;
5079 for (x = fieldlist; x; x = TREE_CHAIN (x))
5081 if (len > 15 || DECL_NAME (x) == NULL)
5082 break;
5083 len += 1;
5086 if (len > 15)
5088 tree *field_array;
5089 struct lang_type *space;
5090 struct sorted_fields_type *space2;
5092 len += list_length (x);
5094 /* Use the same allocation policy here that make_node uses, to
5095 ensure that this lives as long as the rest of the struct decl.
5096 All decls in an inline function need to be saved. */
5098 space = ggc_alloc (sizeof (struct lang_type));
5099 space2 = ggc_alloc (sizeof (struct sorted_fields_type) + len * sizeof (tree));
5101 len = 0;
5102 space->s = space2;
5103 field_array = &space2->elts[0];
5104 for (x = fieldlist; x; x = TREE_CHAIN (x))
5106 field_array[len++] = x;
5108 /* If there is anonymous struct or union, break out of the loop. */
5109 if (DECL_NAME (x) == NULL)
5110 break;
5112 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5113 if (x == NULL)
5115 TYPE_LANG_SPECIFIC (t) = space;
5116 TYPE_LANG_SPECIFIC (t)->s->len = len;
5117 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5118 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5123 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5125 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5126 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5127 TYPE_ALIGN (x) = TYPE_ALIGN (t);
5128 TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t);
5131 /* If this was supposed to be a transparent union, but we can't
5132 make it one, warn and turn off the flag. */
5133 if (TREE_CODE (t) == UNION_TYPE
5134 && TYPE_TRANSPARENT_UNION (t)
5135 && TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t)))
5137 TYPE_TRANSPARENT_UNION (t) = 0;
5138 warning ("union cannot be made transparent");
5141 /* If this structure or union completes the type of any previous
5142 variable declaration, lay it out and output its rtl. */
5143 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5145 x = TREE_CHAIN (x))
5147 tree decl = TREE_VALUE (x);
5148 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5149 layout_array_type (TREE_TYPE (decl));
5150 if (TREE_CODE (decl) != TYPE_DECL)
5152 layout_decl (decl, 0);
5153 if (c_dialect_objc ())
5154 objc_check_decl (decl);
5155 rest_of_decl_compilation (decl, NULL, toplevel, 0);
5156 if (! toplevel)
5157 expand_decl (decl);
5160 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5162 /* Finish debugging output for this type. */
5163 rest_of_type_compilation (t, toplevel);
5165 return t;
5168 /* Lay out the type T, and its element type, and so on. */
5170 static void
5171 layout_array_type (tree t)
5173 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5174 layout_array_type (TREE_TYPE (t));
5175 layout_type (t);
5178 /* Begin compiling the definition of an enumeration type.
5179 NAME is its name (or null if anonymous).
5180 Returns the type object, as yet incomplete.
5181 Also records info about it so that build_enumerator
5182 may be used to declare the individual values as they are read. */
5184 tree
5185 start_enum (tree name)
5187 tree enumtype = 0;
5189 /* If this is the real definition for a previous forward reference,
5190 fill in the contents in the same object that used to be the
5191 forward reference. */
5193 if (name != 0)
5194 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5196 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5198 enumtype = make_node (ENUMERAL_TYPE);
5199 pushtag (name, enumtype);
5202 C_TYPE_BEING_DEFINED (enumtype) = 1;
5204 if (TYPE_VALUES (enumtype) != 0)
5206 /* This enum is a named one that has been declared already. */
5207 error ("redeclaration of `enum %s'", IDENTIFIER_POINTER (name));
5209 /* Completely replace its old definition.
5210 The old enumerators remain defined, however. */
5211 TYPE_VALUES (enumtype) = 0;
5214 enum_next_value = integer_zero_node;
5215 enum_overflow = 0;
5217 if (flag_short_enums)
5218 TYPE_PACKED (enumtype) = 1;
5220 return enumtype;
5223 /* After processing and defining all the values of an enumeration type,
5224 install their decls in the enumeration type and finish it off.
5225 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5226 and ATTRIBUTES are the specified attributes.
5227 Returns ENUMTYPE. */
5229 tree
5230 finish_enum (tree enumtype, tree values, tree attributes)
5232 tree pair, tem;
5233 tree minnode = 0, maxnode = 0, enum_value_type;
5234 int precision, unsign;
5235 int toplevel = (global_scope == current_scope);
5237 if (in_parm_level_p ())
5238 warning ("enum defined inside parms");
5240 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5242 /* Calculate the maximum value of any enumerator in this type. */
5244 if (values == error_mark_node)
5245 minnode = maxnode = integer_zero_node;
5246 else
5248 minnode = maxnode = TREE_VALUE (values);
5249 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5251 tree value = TREE_VALUE (pair);
5252 if (tree_int_cst_lt (maxnode, value))
5253 maxnode = value;
5254 if (tree_int_cst_lt (value, minnode))
5255 minnode = value;
5259 /* Construct the final type of this enumeration. It is the same
5260 as one of the integral types - the narrowest one that fits, except
5261 that normally we only go as narrow as int - and signed iff any of
5262 the values are negative. */
5263 unsign = (tree_int_cst_sgn (minnode) >= 0);
5264 precision = MAX (min_precision (minnode, unsign),
5265 min_precision (maxnode, unsign));
5266 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5268 tree narrowest = c_common_type_for_size (precision, unsign);
5269 if (narrowest == 0)
5271 warning ("enumeration values exceed range of largest integer");
5272 narrowest = long_long_integer_type_node;
5275 precision = TYPE_PRECISION (narrowest);
5277 else
5278 precision = TYPE_PRECISION (integer_type_node);
5280 if (precision == TYPE_PRECISION (integer_type_node))
5281 enum_value_type = c_common_type_for_size (precision, 0);
5282 else
5283 enum_value_type = enumtype;
5285 TYPE_MIN_VALUE (enumtype) = minnode;
5286 TYPE_MAX_VALUE (enumtype) = maxnode;
5287 TYPE_PRECISION (enumtype) = precision;
5288 TREE_UNSIGNED (enumtype) = unsign;
5289 TYPE_SIZE (enumtype) = 0;
5290 layout_type (enumtype);
5292 if (values != error_mark_node)
5294 /* Change the type of the enumerators to be the enum type. We
5295 need to do this irrespective of the size of the enum, for
5296 proper type checking. Replace the DECL_INITIALs of the
5297 enumerators, and the value slots of the list, with copies
5298 that have the enum type; they cannot be modified in place
5299 because they may be shared (e.g. integer_zero_node) Finally,
5300 change the purpose slots to point to the names of the decls. */
5301 for (pair = values; pair; pair = TREE_CHAIN (pair))
5303 tree enu = TREE_PURPOSE (pair);
5305 TREE_TYPE (enu) = enumtype;
5307 /* The ISO C Standard mandates enumerators to have type int,
5308 even though the underlying type of an enum type is
5309 unspecified. Here we convert any enumerators that fit in
5310 an int to type int, to avoid promotions to unsigned types
5311 when comparing integers with enumerators that fit in the
5312 int range. When -pedantic is given, build_enumerator()
5313 would have already taken care of those that don't fit. */
5314 if (int_fits_type_p (DECL_INITIAL (enu), enum_value_type))
5315 DECL_INITIAL (enu) = convert (enum_value_type, DECL_INITIAL (enu));
5316 else
5317 DECL_INITIAL (enu) = convert (enumtype, DECL_INITIAL (enu));
5319 TREE_PURPOSE (pair) = DECL_NAME (enu);
5320 TREE_VALUE (pair) = DECL_INITIAL (enu);
5323 TYPE_VALUES (enumtype) = values;
5326 /* Fix up all variant types of this enum type. */
5327 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5329 if (tem == enumtype)
5330 continue;
5331 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5332 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5333 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5334 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5335 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5336 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5337 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5338 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5339 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5340 TREE_UNSIGNED (tem) = TREE_UNSIGNED (enumtype);
5343 /* Finish debugging output for this type. */
5344 rest_of_type_compilation (enumtype, toplevel);
5346 return enumtype;
5349 /* Build and install a CONST_DECL for one value of the
5350 current enumeration type (one that was begun with start_enum).
5351 Return a tree-list containing the CONST_DECL and its value.
5352 Assignment of sequential values by default is handled here. */
5354 tree
5355 build_enumerator (tree name, tree value)
5357 tree decl, type;
5359 /* Validate and default VALUE. */
5361 /* Remove no-op casts from the value. */
5362 if (value)
5363 STRIP_TYPE_NOPS (value);
5365 if (value != 0)
5367 if (TREE_CODE (value) == INTEGER_CST)
5369 value = default_conversion (value);
5370 constant_expression_warning (value);
5372 else
5374 error ("enumerator value for `%s' not integer constant",
5375 IDENTIFIER_POINTER (name));
5376 value = 0;
5380 /* Default based on previous value. */
5381 /* It should no longer be possible to have NON_LVALUE_EXPR
5382 in the default. */
5383 if (value == 0)
5385 value = enum_next_value;
5386 if (enum_overflow)
5387 error ("overflow in enumeration values");
5390 if (pedantic && ! int_fits_type_p (value, integer_type_node))
5392 pedwarn ("ISO C restricts enumerator values to range of `int'");
5393 value = convert (integer_type_node, value);
5396 /* Set basis for default for next value. */
5397 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
5398 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5400 /* Now create a declaration for the enum value name. */
5402 type = TREE_TYPE (value);
5403 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5404 TYPE_PRECISION (integer_type_node)),
5405 (TYPE_PRECISION (type)
5406 >= TYPE_PRECISION (integer_type_node)
5407 && TREE_UNSIGNED (type)));
5409 decl = build_decl (CONST_DECL, name, type);
5410 DECL_INITIAL (decl) = convert (type, value);
5411 pushdecl (decl);
5413 return tree_cons (decl, value, NULL_TREE);
5417 /* Create the FUNCTION_DECL for a function definition.
5418 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5419 the declaration; they describe the function's name and the type it returns,
5420 but twisted together in a fashion that parallels the syntax of C.
5422 This function creates a binding context for the function body
5423 as well as setting up the FUNCTION_DECL in current_function_decl.
5425 Returns 1 on success. If the DECLARATOR is not suitable for a function
5426 (it defines a datum instead), we return 0, which tells
5427 yyparse to report a parse error. */
5430 start_function (tree declspecs, tree declarator, tree attributes)
5432 tree decl1, old_decl;
5433 tree restype;
5434 int old_immediate_size_expand = immediate_size_expand;
5436 current_function_returns_value = 0; /* Assume, until we see it does. */
5437 current_function_returns_null = 0;
5438 current_function_returns_abnormally = 0;
5439 warn_about_return_type = 0;
5440 current_extern_inline = 0;
5442 /* Don't expand any sizes in the return type of the function. */
5443 immediate_size_expand = 0;
5445 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1);
5447 /* If the declarator is not suitable for a function definition,
5448 cause a syntax error. */
5449 if (decl1 == 0)
5451 immediate_size_expand = old_immediate_size_expand;
5452 return 0;
5455 decl_attributes (&decl1, attributes, 0);
5457 if (DECL_DECLARED_INLINE_P (decl1)
5458 && DECL_UNINLINABLE (decl1)
5459 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
5460 warning ("%Jinline function '%D' given attribute noinline", decl1, decl1);
5462 announce_function (decl1);
5464 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
5466 error ("return type is an incomplete type");
5467 /* Make it return void instead. */
5468 TREE_TYPE (decl1)
5469 = build_function_type (void_type_node,
5470 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
5473 if (warn_about_return_type)
5474 pedwarn_c99 ("return type defaults to `int'");
5476 /* Save the parm names or decls from this function's declarator
5477 where store_parm_decls will find them. */
5478 current_function_parms = last_function_parms;
5479 current_function_parm_tags = last_function_parm_tags;
5480 current_function_parm_others = last_function_parm_others;
5482 /* Make the init_value nonzero so pushdecl knows this is not tentative.
5483 error_mark_node is replaced below (in poplevel) with the BLOCK. */
5484 DECL_INITIAL (decl1) = error_mark_node;
5486 /* If this definition isn't a prototype and we had a prototype declaration
5487 before, copy the arg type info from that prototype.
5488 But not if what we had before was a builtin function. */
5489 old_decl = lookup_name_current_level (DECL_NAME (decl1));
5490 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
5491 && !DECL_BUILT_IN (old_decl)
5492 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5493 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (old_decl))))
5494 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
5496 TREE_TYPE (decl1) = TREE_TYPE (old_decl);
5497 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
5500 /* Optionally warn of old-fashioned def with no previous prototype. */
5501 if (warn_strict_prototypes
5502 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
5503 && C_DECL_ISNT_PROTOTYPE (old_decl))
5504 warning ("function declaration isn't a prototype");
5505 /* Optionally warn of any global def with no previous prototype. */
5506 else if (warn_missing_prototypes
5507 && TREE_PUBLIC (decl1)
5508 && ! MAIN_NAME_P (DECL_NAME (decl1))
5509 && C_DECL_ISNT_PROTOTYPE (old_decl))
5510 warning ("%Jno previous prototype for '%D'", decl1, decl1);
5511 /* Optionally warn of any def with no previous prototype
5512 if the function has already been used. */
5513 else if (warn_missing_prototypes
5514 && old_decl != 0 && TREE_USED (old_decl)
5515 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
5516 warning ("%J'%D' was used with no prototype before its definition",
5517 decl1, decl1);
5518 /* Optionally warn of any global def with no previous declaration. */
5519 else if (warn_missing_declarations
5520 && TREE_PUBLIC (decl1)
5521 && old_decl == 0
5522 && ! MAIN_NAME_P (DECL_NAME (decl1)))
5523 warning ("%Jno previous declaration for '%D'", decl1, decl1);
5524 /* Optionally warn of any def with no previous declaration
5525 if the function has already been used. */
5526 else if (warn_missing_declarations
5527 && old_decl != 0 && TREE_USED (old_decl)
5528 && C_DECL_IMPLICIT (old_decl))
5529 warning ("%J`%D' was used with no declaration before its definition",
5530 decl1, decl1);
5532 /* This is a definition, not a reference.
5533 So normally clear DECL_EXTERNAL.
5534 However, `extern inline' acts like a declaration
5535 except for defining how to inline. So set DECL_EXTERNAL in that case. */
5536 DECL_EXTERNAL (decl1) = current_extern_inline;
5538 /* This function exists in static storage.
5539 (This does not mean `static' in the C sense!) */
5540 TREE_STATIC (decl1) = 1;
5542 /* A nested function is not global. */
5543 if (current_function_decl != 0)
5544 TREE_PUBLIC (decl1) = 0;
5546 #ifdef ENABLE_CHECKING
5547 /* This is the earliest point at which we might know the assembler
5548 name of the function. Thus, if it's set before this, die horribly. */
5549 if (DECL_ASSEMBLER_NAME_SET_P (decl1))
5550 abort ();
5551 #endif
5553 /* If #pragma weak was used, mark the decl weak now. */
5554 if (current_scope == global_scope)
5555 maybe_apply_pragma_weak (decl1);
5557 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
5558 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
5560 tree args;
5561 int argct = 0;
5563 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5564 != integer_type_node)
5565 pedwarn ("%Jreturn type of '%D' is not `int'", decl1, decl1);
5567 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
5568 args = TREE_CHAIN (args))
5570 tree type = args ? TREE_VALUE (args) : 0;
5572 if (type == void_type_node)
5573 break;
5575 ++argct;
5576 switch (argct)
5578 case 1:
5579 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
5580 pedwarn ("%Jfirst argument of '%D' should be `int'",
5581 decl1, decl1);
5582 break;
5584 case 2:
5585 if (TREE_CODE (type) != POINTER_TYPE
5586 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5587 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5588 != char_type_node))
5589 pedwarn ("%Jsecond argument of '%D' should be 'char **'",
5590 decl1, decl1);
5591 break;
5593 case 3:
5594 if (TREE_CODE (type) != POINTER_TYPE
5595 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5596 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5597 != char_type_node))
5598 pedwarn ("%Jthird argument of '%D' should probably be "
5599 "'char **'", decl1, decl1);
5600 break;
5604 /* It is intentional that this message does not mention the third
5605 argument because it's only mentioned in an appendix of the
5606 standard. */
5607 if (argct > 0 && (argct < 2 || argct > 3))
5608 pedwarn ("%J'%D' takes only zero or two arguments", decl1, decl1);
5610 if (! TREE_PUBLIC (decl1))
5611 pedwarn ("%J'%D' is normally a non-static function", decl1, decl1);
5614 /* Record the decl so that the function name is defined.
5615 If we already have a decl for this name, and it is a FUNCTION_DECL,
5616 use the old decl. */
5618 current_function_decl = pushdecl (decl1);
5620 pushlevel (0);
5621 declare_parm_level ();
5623 make_decl_rtl (current_function_decl, NULL);
5625 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
5626 /* Promote the value to int before returning it. */
5627 if (c_promoting_integer_type_p (restype))
5629 /* It retains unsignedness if not really getting wider. */
5630 if (TREE_UNSIGNED (restype)
5631 && (TYPE_PRECISION (restype)
5632 == TYPE_PRECISION (integer_type_node)))
5633 restype = unsigned_type_node;
5634 else
5635 restype = integer_type_node;
5637 DECL_RESULT (current_function_decl)
5638 = build_decl (RESULT_DECL, NULL_TREE, restype);
5640 /* If this fcn was already referenced via a block-scope `extern' decl
5641 (or an implicit decl), propagate certain information about the usage. */
5642 if (TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (current_function_decl)))
5643 TREE_ADDRESSABLE (current_function_decl) = 1;
5645 immediate_size_expand = old_immediate_size_expand;
5647 start_fname_decls ();
5649 return 1;
5652 /* Subroutine of store_parm_decls which handles new-style function
5653 definitions (prototype format). The parms already have decls, so we
5654 need only record them as in effect and complain if any redundant
5655 old-style parm decls were written. */
5656 static void
5657 store_parm_decls_newstyle (void)
5659 tree decl, last;
5660 tree fndecl = current_function_decl;
5661 tree parms = current_function_parms;
5662 tree tags = current_function_parm_tags;
5663 tree others = current_function_parm_others;
5665 if (current_scope->parms || current_scope->names || current_scope->tags)
5667 error ("%Jold-style parameter declarations in prototyped "
5668 "function definition", fndecl);
5670 /* Get rid of the old-style declarations. */
5671 poplevel (0, 0, 0);
5672 pushlevel (0);
5675 /* Now make all the parameter declarations visible in the function body.
5676 We can bypass most of the grunt work of pushdecl. */
5677 for (last = 0, decl = parms; decl; last = decl, decl = TREE_CHAIN (decl))
5679 DECL_CONTEXT (decl) = current_function_decl;
5680 if (DECL_NAME (decl) == 0)
5681 error ("%Jparameter name omitted", decl);
5682 else
5684 if (IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)))
5685 current_scope->shadowed
5686 = tree_cons (DECL_NAME (decl),
5687 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)),
5688 current_scope->shadowed);
5689 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)) = decl;
5692 current_scope->parms = parms;
5693 current_scope->parms_last = last;
5695 /* Record the parameter list in the function declaration. */
5696 DECL_ARGUMENTS (fndecl) = parms;
5698 /* Now make all the ancillary declarations visible, likewise. */
5699 for (last = 0, decl = others; decl; last = decl, decl = TREE_CHAIN (decl))
5701 DECL_CONTEXT (decl) = current_function_decl;
5702 if (DECL_NAME (decl)
5703 && TYPE_MAIN_VARIANT (TREE_TYPE (decl)) != void_type_node)
5705 if (IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)))
5706 current_scope->shadowed
5707 = tree_cons (DECL_NAME (decl),
5708 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)),
5709 current_scope->shadowed);
5710 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)) = decl;
5713 current_scope->names = others;
5714 current_scope->names_last = last;
5716 /* And all the tag declarations. */
5717 for (decl = tags; decl; decl = TREE_CHAIN (decl))
5718 if (TREE_PURPOSE (decl))
5720 if (IDENTIFIER_TAG_VALUE (TREE_PURPOSE (decl)))
5721 current_scope->shadowed_tags
5722 = tree_cons (TREE_PURPOSE (decl),
5723 IDENTIFIER_SYMBOL_VALUE (TREE_PURPOSE (decl)),
5724 current_scope->shadowed_tags);
5725 IDENTIFIER_TAG_VALUE (TREE_PURPOSE (decl)) = TREE_VALUE (decl);
5727 current_scope->tags = tags;
5730 /* Subroutine of store_parm_decls which handles old-style function
5731 definitions (separate parameter list and declarations). */
5733 static void
5734 store_parm_decls_oldstyle (void)
5736 tree parm, decl, last;
5737 tree fndecl = current_function_decl;
5739 /* This is the identifier list from the function declarator. */
5740 tree parmids = current_function_parms;
5742 /* We use DECL_WEAK as a flag to show which parameters have been
5743 seen already, since it is not used on PARM_DECL. */
5744 #ifdef ENABLE_CHECKING
5745 for (parm = current_scope->parms; parm; parm = TREE_CHAIN (parm))
5746 if (DECL_WEAK (parm))
5747 abort ();
5748 #endif
5750 /* Match each formal parameter name with its declaration. Save each
5751 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
5752 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
5754 if (TREE_VALUE (parm) == 0)
5756 error ("%Jparameter name missing from parameter list", fndecl);
5757 TREE_PURPOSE (parm) = 0;
5758 continue;
5761 decl = IDENTIFIER_SYMBOL_VALUE (TREE_VALUE (parm));
5762 if (decl && DECL_CONTEXT (decl) == fndecl)
5764 /* If we got something other than a PARM_DECL it is an error. */
5765 if (TREE_CODE (decl) != PARM_DECL)
5766 error ("%J\"%D\" declared as a non-parameter", decl, decl);
5767 /* If the declaration is already marked, we have a duplicate
5768 name. Complain and ignore the duplicate. */
5769 else if (DECL_WEAK (decl))
5771 error ("%Jmultiple parameters named \"%D\"", decl, decl);
5772 TREE_PURPOSE (parm) = 0;
5773 continue;
5775 /* If the declaration says "void", complain and turn it into
5776 an int. */
5777 else if (VOID_TYPE_P (TREE_TYPE (decl)))
5779 error ("%Jparameter \"%D\" declared void", decl, decl);
5780 TREE_TYPE (decl) = integer_type_node;
5781 DECL_ARG_TYPE (decl) = integer_type_node;
5782 layout_decl (decl, 0);
5785 /* If no declaration found, default to int. */
5786 else
5788 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
5789 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
5790 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
5791 pushdecl (decl);
5793 if (flag_isoc99)
5794 pedwarn ("%Jtype of \"%D\" defaults to \"int\"", decl, decl);
5795 else if (extra_warnings)
5796 warning ("%Jtype of \"%D\" defaults to \"int\"", decl, decl);
5799 TREE_PURPOSE (parm) = decl;
5800 DECL_WEAK (decl) = 1;
5803 /* Now examine the parms chain for incomplete declarations
5804 and declarations with no corresponding names. */
5806 for (parm = current_scope->parms; parm; parm = TREE_CHAIN (parm))
5808 if (!COMPLETE_TYPE_P (TREE_TYPE (parm)))
5810 error ("%Jparameter \"%D\" has incomplete type", parm, parm);
5811 TREE_TYPE (parm) = error_mark_node;
5814 if (! DECL_WEAK (parm))
5816 error ("%Jdeclaration for parameter \"%D\" but no such parameter",
5817 parm, parm);
5819 /* Pretend the parameter was not missing.
5820 This gets us to a standard state and minimizes
5821 further error messages. */
5822 parmids = chainon (parmids, tree_cons (parm, 0, 0));
5826 /* Chain the declarations together in the order of the list of
5827 names. Store that chain in the function decl, replacing the
5828 list of names. Update the current scope to match. */
5829 DECL_ARGUMENTS (fndecl) = 0;
5831 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
5832 if (TREE_PURPOSE (parm))
5833 break;
5834 if (parm && TREE_PURPOSE (parm))
5836 last = TREE_PURPOSE (parm);
5837 DECL_ARGUMENTS (fndecl) = last;
5838 current_scope->parms = last;
5839 DECL_WEAK (last) = 0;
5841 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
5842 if (TREE_PURPOSE (parm))
5844 TREE_CHAIN (last) = TREE_PURPOSE (parm);
5845 last = TREE_PURPOSE (parm);
5846 DECL_WEAK (last) = 0;
5848 current_scope->parms_last = last;
5849 TREE_CHAIN (last) = 0;
5852 /* If there was a previous prototype,
5853 set the DECL_ARG_TYPE of each argument according to
5854 the type previously specified, and report any mismatches. */
5856 if (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
5858 tree type;
5859 for (parm = DECL_ARGUMENTS (fndecl),
5860 type = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
5861 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
5862 != void_type_node));
5863 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
5865 if (parm == 0 || type == 0
5866 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
5868 error ("number of arguments doesn't match prototype");
5869 error ("%Hprototype declaration",
5870 &current_function_prototype_locus);
5871 break;
5873 /* Type for passing arg must be consistent with that
5874 declared for the arg. ISO C says we take the unqualified
5875 type for parameters declared with qualified type. */
5876 if (! comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
5877 TYPE_MAIN_VARIANT (TREE_VALUE (type)),
5878 COMPARE_STRICT))
5880 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
5881 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
5883 /* Adjust argument to match prototype. E.g. a previous
5884 `int foo(float);' prototype causes
5885 `int foo(x) float x; {...}' to be treated like
5886 `int foo(float x) {...}'. This is particularly
5887 useful for argument types like uid_t. */
5888 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
5890 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
5891 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
5892 && TYPE_PRECISION (TREE_TYPE (parm))
5893 < TYPE_PRECISION (integer_type_node))
5894 DECL_ARG_TYPE (parm) = integer_type_node;
5896 if (pedantic)
5898 pedwarn ("promoted argument \"%D\" "
5899 "doesn't match prototype", parm);
5900 pedwarn ("%Hprototype declaration",
5901 &current_function_prototype_locus);
5904 else
5906 error ("argument \"%D\" doesn't match prototype", parm);
5907 error ("%Hprototype declaration",
5908 &current_function_prototype_locus);
5912 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
5915 /* Otherwise, create a prototype that would match. */
5917 else
5919 tree actual = 0, last = 0, type;
5921 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
5923 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
5924 if (last)
5925 TREE_CHAIN (last) = type;
5926 else
5927 actual = type;
5928 last = type;
5930 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
5931 if (last)
5932 TREE_CHAIN (last) = type;
5933 else
5934 actual = type;
5936 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
5937 of the type of this function, but we need to avoid having this
5938 affect the types of other similarly-typed functions, so we must
5939 first force the generation of an identical (but separate) type
5940 node for the relevant function type. The new node we create
5941 will be a variant of the main variant of the original function
5942 type. */
5944 TREE_TYPE (fndecl) = build_type_copy (TREE_TYPE (fndecl));
5946 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
5950 /* Store the parameter declarations into the current function declaration.
5951 This is called after parsing the parameter declarations, before
5952 digesting the body of the function.
5954 For an old-style definition, construct a prototype out of the old-style
5955 parameter declarations and inject it into the function's type. */
5957 void
5958 store_parm_decls (void)
5960 tree fndecl = current_function_decl;
5962 /* The function containing FNDECL, if any. */
5963 tree context = decl_function_context (fndecl);
5965 /* True if this definition is written with a prototype. */
5966 bool prototype = (current_function_parms
5967 && TREE_CODE (current_function_parms) != TREE_LIST);
5969 if (prototype)
5970 store_parm_decls_newstyle ();
5971 else
5972 store_parm_decls_oldstyle ();
5974 /* The next call to pushlevel will be a function body. */
5976 next_is_function_body = true;
5978 /* Write a record describing this function definition to the prototypes
5979 file (if requested). */
5981 gen_aux_info_record (fndecl, 1, 0, prototype);
5983 /* Initialize the RTL code for the function. */
5984 allocate_struct_function (fndecl);
5986 /* Begin the statement tree for this function. */
5987 begin_stmt_tree (&DECL_SAVED_TREE (fndecl));
5989 /* If this is a nested function, save away the sizes of any
5990 variable-size types so that we can expand them when generating
5991 RTL. */
5992 if (context)
5994 tree t;
5996 DECL_LANG_SPECIFIC (fndecl)->pending_sizes
5997 = nreverse (get_pending_sizes ());
5998 for (t = DECL_LANG_SPECIFIC (fndecl)->pending_sizes;
6000 t = TREE_CHAIN (t))
6001 SAVE_EXPR_CONTEXT (TREE_VALUE (t)) = context;
6004 /* This function is being processed in whole-function mode. */
6005 cfun->x_whole_function_mode_p = 1;
6007 /* Even though we're inside a function body, we still don't want to
6008 call expand_expr to calculate the size of a variable-sized array.
6009 We haven't necessarily assigned RTL to all variables yet, so it's
6010 not safe to try to expand expressions involving them. */
6011 immediate_size_expand = 0;
6012 cfun->x_dont_save_pending_sizes_p = 1;
6015 /* Finish up a function declaration and compile that function
6016 all the way to assembler language output. The free the storage
6017 for the function definition.
6019 This is called after parsing the body of the function definition. */
6021 void
6022 finish_function (void)
6024 tree fndecl = current_function_decl;
6026 /* When a function declaration is totally empty, e.g.
6027 void foo(void) { }
6028 (the argument list is irrelevant) the compstmt rule will not
6029 bother calling pushlevel/poplevel, which means we get here with
6030 the scope stack out of sync. Detect this situation by noticing
6031 that current_scope is still as store_parm_decls left it, and do
6032 a dummy push/pop to get back to consistency.
6033 Note that the call to pushlevel does not actually push another
6034 scope - see there for details. */
6036 if (current_scope->parm_flag && next_is_function_body)
6038 pushlevel (0);
6039 poplevel (0, 0, 0);
6042 if (TREE_CODE (fndecl) == FUNCTION_DECL
6043 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6045 tree args = DECL_ARGUMENTS (fndecl);
6046 for (; args; args = TREE_CHAIN (args))
6048 tree type = TREE_TYPE (args);
6049 if (INTEGRAL_TYPE_P (type)
6050 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6051 DECL_ARG_TYPE (args) = integer_type_node;
6055 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6057 /* Must mark the RESULT_DECL as being in this function. */
6059 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6061 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6063 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6064 != integer_type_node)
6066 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6067 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6068 if (!warn_main)
6069 pedwarn ("%Jreturn type of '%D' is not `int'", fndecl, fndecl);
6071 else
6073 #ifdef DEFAULT_MAIN_RETURN
6074 /* Make it so that `main' always returns success by default. */
6075 DEFAULT_MAIN_RETURN;
6076 #else
6077 if (flag_isoc99)
6078 c_expand_return (integer_zero_node);
6079 #endif
6083 finish_fname_decls ();
6085 /* Tie off the statement tree for this function. */
6086 finish_stmt_tree (&DECL_SAVED_TREE (fndecl));
6088 /* Complain if there's just no return statement. */
6089 if (warn_return_type
6090 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6091 && !current_function_returns_value && !current_function_returns_null
6092 /* Don't complain if we abort. */
6093 && !current_function_returns_abnormally
6094 /* Don't warn for main(). */
6095 && !MAIN_NAME_P (DECL_NAME (fndecl))
6096 /* Or if they didn't actually specify a return type. */
6097 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6098 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6099 inline function, as we might never be compiled separately. */
6100 && DECL_INLINE (fndecl))
6101 warning ("no return statement in function returning non-void");
6103 /* With just -Wextra, complain only if function returns both with
6104 and without a value. */
6105 if (extra_warnings
6106 && current_function_returns_value
6107 && current_function_returns_null)
6108 warning ("this function may return with or without a value");
6110 /* We're leaving the context of this function, so zap cfun. It's still in
6111 DECL_SAVED_INSNS, and we'll restore it in tree_rest_of_compilation. */
6112 cfun = NULL;
6114 /* ??? Objc emits functions after finalizing the compilation unit.
6115 This should be cleaned up later and this conditional removed. */
6116 if (!cgraph_global_info_ready)
6117 cgraph_finalize_function (fndecl, false);
6118 else
6119 c_expand_body (fndecl);
6120 current_function_decl = NULL;
6123 /* Generate the RTL for a deferred function FNDECL. */
6125 void
6126 c_expand_deferred_function (tree fndecl)
6128 /* DECL_INLINE or DECL_RESULT might got cleared after the inline
6129 function was deferred, e.g. in duplicate_decls. */
6130 if (DECL_INLINE (fndecl) && DECL_RESULT (fndecl))
6132 if (flag_inline_trees)
6134 timevar_push (TV_INTEGRATION);
6135 optimize_inline_calls (fndecl);
6136 timevar_pop (TV_INTEGRATION);
6138 c_expand_body (fndecl);
6139 current_function_decl = NULL;
6143 /* Generate the RTL for the body of FNDECL. If NESTED_P is nonzero,
6144 then we are already in the process of generating RTL for another
6145 function. */
6147 static void
6148 c_expand_body_1 (tree fndecl, int nested_p)
6150 if (nested_p)
6152 /* Make sure that we will evaluate variable-sized types involved
6153 in our function's type. */
6154 expand_pending_sizes (DECL_LANG_SPECIFIC (fndecl)->pending_sizes);
6156 /* Squirrel away our current state. */
6157 push_function_context ();
6160 tree_rest_of_compilation (fndecl, nested_p);
6162 if (nested_p)
6163 /* Return to the enclosing function. */
6164 pop_function_context ();
6166 if (DECL_STATIC_CONSTRUCTOR (fndecl))
6168 if (targetm.have_ctors_dtors)
6169 (* targetm.asm_out.constructor) (XEXP (DECL_RTL (fndecl), 0),
6170 DEFAULT_INIT_PRIORITY);
6171 else
6172 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6175 if (DECL_STATIC_DESTRUCTOR (fndecl))
6177 if (targetm.have_ctors_dtors)
6178 (* targetm.asm_out.destructor) (XEXP (DECL_RTL (fndecl), 0),
6179 DEFAULT_INIT_PRIORITY);
6180 else
6181 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6185 /* Like c_expand_body_1 but only for unnested functions. */
6187 void
6188 c_expand_body (tree fndecl)
6190 c_expand_body_1 (fndecl, 0);
6193 /* Check the declarations given in a for-loop for satisfying the C99
6194 constraints. */
6195 void
6196 check_for_loop_decls (void)
6198 tree t;
6200 if (!flag_isoc99)
6202 /* If we get here, declarations have been used in a for loop without
6203 the C99 for loop scope. This doesn't make much sense, so don't
6204 allow it. */
6205 error ("'for' loop initial declaration used outside C99 mode");
6206 return;
6208 /* C99 subclause 6.8.5 paragraph 3:
6210 [#3] The declaration part of a for statement shall only
6211 declare identifiers for objects having storage class auto or
6212 register.
6214 It isn't clear whether, in this sentence, "identifiers" binds to
6215 "shall only declare" or to "objects" - that is, whether all identifiers
6216 declared must be identifiers for objects, or whether the restriction
6217 only applies to those that are. (A question on this in comp.std.c
6218 in November 2000 received no answer.) We implement the strictest
6219 interpretation, to avoid creating an extension which later causes
6220 problems. */
6222 for (t = current_scope->tags; t; t = TREE_CHAIN (t))
6224 if (TREE_PURPOSE (t) != 0)
6226 enum tree_code code = TREE_CODE (TREE_VALUE (t));
6228 if (code == RECORD_TYPE)
6229 error ("'struct %s' declared in 'for' loop initial declaration",
6230 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6231 else if (code == UNION_TYPE)
6232 error ("'union %s' declared in 'for' loop initial declaration",
6233 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6234 else
6235 error ("'enum %s' declared in 'for' loop initial declaration",
6236 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6240 for (t = getdecls (); t; t = TREE_CHAIN (t))
6242 if (TREE_CODE (t) != VAR_DECL && DECL_NAME (t))
6243 error ("%Jdeclaration of non-variable '%D' in 'for' loop "
6244 "initial declaration", t, t);
6245 else if (TREE_STATIC (t))
6246 error ("%Jdeclaration of static variable '%D' in 'for' loop "
6247 "initial declaration", t, t);
6248 else if (DECL_EXTERNAL (t))
6249 error ("%Jdeclaration of 'extern' variable '%D' in 'for' loop "
6250 "initial declaration", t, t);
6254 /* Save and restore the variables in this file and elsewhere
6255 that keep track of the progress of compilation of the current function.
6256 Used for nested functions. */
6258 struct language_function GTY(())
6260 struct c_language_function base;
6261 int returns_value;
6262 int returns_null;
6263 int returns_abnormally;
6264 int warn_about_return_type;
6265 int extern_inline;
6268 /* Save and reinitialize the variables
6269 used during compilation of a C function. */
6271 void
6272 c_push_function_context (struct function *f)
6274 struct language_function *p;
6275 p = ggc_alloc (sizeof (struct language_function));
6276 f->language = p;
6278 p->base.x_stmt_tree = c_stmt_tree;
6279 p->base.x_scope_stmt_stack = c_scope_stmt_stack;
6280 p->returns_value = current_function_returns_value;
6281 p->returns_null = current_function_returns_null;
6282 p->returns_abnormally = current_function_returns_abnormally;
6283 p->warn_about_return_type = warn_about_return_type;
6284 p->extern_inline = current_extern_inline;
6287 /* Restore the variables used during compilation of a C function. */
6289 void
6290 c_pop_function_context (struct function *f)
6292 struct language_function *p = f->language;
6294 if (DECL_SAVED_INSNS (current_function_decl) == 0
6295 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6297 /* Stop pointing to the local nodes about to be freed. */
6298 /* But DECL_INITIAL must remain nonzero so we know this
6299 was an actual function definition. */
6300 DECL_INITIAL (current_function_decl) = error_mark_node;
6301 DECL_ARGUMENTS (current_function_decl) = 0;
6304 c_stmt_tree = p->base.x_stmt_tree;
6305 c_scope_stmt_stack = p->base.x_scope_stmt_stack;
6306 current_function_returns_value = p->returns_value;
6307 current_function_returns_null = p->returns_null;
6308 current_function_returns_abnormally = p->returns_abnormally;
6309 warn_about_return_type = p->warn_about_return_type;
6310 current_extern_inline = p->extern_inline;
6312 f->language = NULL;
6315 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6317 void
6318 c_dup_lang_specific_decl (tree decl)
6320 struct lang_decl *ld;
6322 if (!DECL_LANG_SPECIFIC (decl))
6323 return;
6325 ld = ggc_alloc (sizeof (struct lang_decl));
6326 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6327 DECL_LANG_SPECIFIC (decl) = ld;
6330 /* The functions below are required for functionality of doing
6331 function at once processing in the C front end. Currently these
6332 functions are not called from anywhere in the C front end, but as
6333 these changes continue, that will change. */
6335 /* Returns nonzero if the current statement is a full expression,
6336 i.e. temporaries created during that statement should be destroyed
6337 at the end of the statement. */
6340 stmts_are_full_exprs_p (void)
6342 return 0;
6345 /* Returns the stmt_tree (if any) to which statements are currently
6346 being added. If there is no active statement-tree, NULL is
6347 returned. */
6349 stmt_tree
6350 current_stmt_tree (void)
6352 return &c_stmt_tree;
6355 /* Returns the stack of SCOPE_STMTs for the current function. */
6357 tree *
6358 current_scope_stmt_stack (void)
6360 return &c_scope_stmt_stack;
6363 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6364 C. */
6367 anon_aggr_type_p (tree node ATTRIBUTE_UNUSED)
6369 return 0;
6372 /* Dummy function in place of callback used by C++. */
6374 void
6375 extract_interface_info (void)
6379 /* Return a new COMPOUND_STMT, after adding it to the current
6380 statement tree. */
6382 tree
6383 c_begin_compound_stmt (void)
6385 tree stmt;
6387 /* Create the COMPOUND_STMT. */
6388 stmt = add_stmt (build_stmt (COMPOUND_STMT, NULL_TREE));
6390 return stmt;
6393 /* Expand T (a DECL_STMT) if it declares an entity not handled by the
6394 common code. */
6396 void
6397 c_expand_decl_stmt (tree t)
6399 tree decl = DECL_STMT_DECL (t);
6401 /* Expand nested functions. */
6402 if (TREE_CODE (decl) == FUNCTION_DECL
6403 && DECL_CONTEXT (decl) == current_function_decl
6404 && DECL_SAVED_TREE (decl))
6405 c_expand_body_1 (decl, 1);
6408 /* Return the global value of T as a symbol. */
6410 tree
6411 identifier_global_value (tree t)
6413 tree decl = IDENTIFIER_SYMBOL_VALUE (t);
6414 if (decl == 0 || DECL_FILE_SCOPE_P (decl))
6415 return decl;
6417 /* Shadowed by something else; find the true global value. */
6418 for (decl = global_scope->names; decl; decl = TREE_CHAIN (decl))
6419 if (DECL_NAME (decl) == t)
6420 return decl;
6422 /* Only local values for this decl. */
6423 return 0;
6426 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6427 otherwise the name is found in ridpointers from RID_INDEX. */
6429 void
6430 record_builtin_type (enum rid rid_index, const char *name, tree type)
6432 tree id;
6433 if (name == 0)
6434 id = ridpointers[(int) rid_index];
6435 else
6436 id = get_identifier (name);
6437 pushdecl (build_decl (TYPE_DECL, id, type));
6440 /* Build the void_list_node (void_type_node having been created). */
6441 tree
6442 build_void_list_node (void)
6444 tree t = build_tree_list (NULL_TREE, void_type_node);
6445 return t;
6448 /* Return something to represent absolute declarators containing a *.
6449 TARGET is the absolute declarator that the * contains.
6450 TYPE_QUALS_ATTRS is a list of modifiers such as const or volatile
6451 to apply to the pointer type, represented as identifiers, possible mixed
6452 with attributes.
6454 We return an INDIRECT_REF whose "contents" are TARGET (inside a TREE_LIST,
6455 if attributes are present) and whose type is the modifier list. */
6457 tree
6458 make_pointer_declarator (tree type_quals_attrs, tree target)
6460 tree quals, attrs;
6461 tree itarget = target;
6462 split_specs_attrs (type_quals_attrs, &quals, &attrs);
6463 if (attrs != NULL_TREE)
6464 itarget = tree_cons (attrs, target, NULL_TREE);
6465 return build1 (INDIRECT_REF, quals, itarget);
6468 /* A wrapper around lhd_set_decl_assembler_name that gives static
6469 variables their C names if they are at file scope and only one
6470 translation unit is being compiled, for backwards compatibility
6471 with certain bizarre assembler hacks (like crtstuff.c). */
6473 void
6474 c_static_assembler_name (tree decl)
6476 if (num_in_fnames == 1
6477 && !TREE_PUBLIC (decl) && DECL_CONTEXT (decl)
6478 && TREE_CODE (DECL_CONTEXT (decl)) == TRANSLATION_UNIT_DECL)
6479 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
6480 else
6481 lhd_set_decl_assembler_name (decl);
6484 /* Hash and equality functions for link_hash_table: key off
6485 DECL_ASSEMBLER_NAME. */
6487 static hashval_t
6488 link_hash_hash (const void *x_p)
6490 tree x = (tree)x_p;
6491 return (hashval_t) (long)DECL_ASSEMBLER_NAME (x);
6494 static int
6495 link_hash_eq (const void *x1_p, const void *x2_p)
6497 tree x1 = (tree)x1_p;
6498 tree x2 = (tree)x2_p;
6499 return DECL_ASSEMBLER_NAME (x1) == DECL_ASSEMBLER_NAME (x2);
6502 /* Propagate information between definitions and uses between multiple
6503 translation units in TU_LIST based on linkage rules. */
6505 void
6506 merge_translation_unit_decls (void)
6508 const tree tu_list = current_file_decl;
6509 tree tu;
6510 tree decl;
6511 htab_t link_hash_table;
6512 tree block;
6514 /* Create the BLOCK that poplevel would have created, but don't
6515 actually call poplevel since that's expensive. */
6516 block = make_node (BLOCK);
6517 BLOCK_VARS (block) = current_scope->names;
6518 TREE_USED (block) = 1;
6519 DECL_INITIAL (current_file_decl) = block;
6521 /* If only one translation unit seen, no copying necessary. */
6522 if (TREE_CHAIN (tu_list) == NULL_TREE)
6523 return;
6525 link_hash_table = htab_create (1021, link_hash_hash, link_hash_eq, NULL);
6527 /* Enter any actual definitions into the hash table. */
6528 for (tu = tu_list; tu; tu = TREE_CHAIN (tu))
6529 for (decl = BLOCK_VARS (DECL_INITIAL (tu)); decl; decl = TREE_CHAIN (decl))
6530 if (TREE_PUBLIC (decl) && ! DECL_EXTERNAL (decl))
6532 PTR *slot;
6533 slot = htab_find_slot (link_hash_table, decl, INSERT);
6535 /* If we've already got a definition, work out which one is
6536 the real one, put it into the hash table, and make the
6537 other one DECL_EXTERNAL. This is important to avoid
6538 putting out two definitions of the same symbol in the
6539 assembly output. */
6540 if (*slot != NULL)
6542 tree old_decl = (tree) *slot;
6544 /* If this is weak or common or whatever, suppress it
6545 in favor of the other definition. */
6546 if (DECL_WEAK (decl))
6547 DECL_EXTERNAL (decl) = 1;
6548 else if (DECL_WEAK (old_decl) && ! DECL_WEAK (decl))
6549 DECL_EXTERNAL (old_decl) = 1;
6550 else if (DECL_COMMON (decl) || DECL_ONE_ONLY (decl))
6551 DECL_EXTERNAL (decl) = 1;
6552 else if (DECL_COMMON (old_decl) || DECL_ONE_ONLY (old_decl))
6553 DECL_EXTERNAL (old_decl) = 1;
6555 if (DECL_EXTERNAL (decl))
6557 DECL_INITIAL (decl) = NULL_TREE;
6558 DECL_COMMON (decl) = 0;
6559 DECL_ONE_ONLY (decl) = 0;
6560 DECL_WEAK (decl) = 0;
6562 else if (DECL_EXTERNAL (old_decl))
6564 DECL_INITIAL (old_decl) = NULL_TREE;
6565 DECL_COMMON (old_decl) = 0;
6566 DECL_ONE_ONLY (old_decl) = 0;
6567 DECL_WEAK (old_decl) = 0;
6568 *slot = decl;
6570 else
6572 error ("%Jredefinition of global '%D'", decl, decl);
6573 error ("%J'%D' previously defined here", old_decl, old_decl);
6576 else
6577 *slot = decl;
6580 /* Now insert the desired information from all the definitions
6581 into any plain declarations. */
6582 for (tu = tu_list; tu; tu = TREE_CHAIN (tu))
6583 for (decl = BLOCK_VARS (DECL_INITIAL (tu)); decl; decl = TREE_CHAIN (decl))
6584 if (TREE_PUBLIC (decl) && DECL_EXTERNAL (decl))
6586 tree global_decl;
6587 global_decl = htab_find (link_hash_table, decl);
6589 if (! global_decl)
6590 continue;
6592 /* Print any appropriate error messages, and partially merge
6593 the decls. */
6594 (void) duplicate_decls (decl, global_decl, true, true);
6597 htab_delete (link_hash_table);
6600 /* Perform final processing on file-scope data. */
6602 void
6603 c_write_global_declarations(void)
6605 tree link;
6607 for (link = current_file_decl; link; link = TREE_CHAIN (link))
6609 tree globals = BLOCK_VARS (DECL_INITIAL (link));
6610 int len = list_length (globals);
6611 tree *vec = xmalloc (sizeof (tree) * len);
6612 int i;
6613 tree decl;
6615 /* Process the decls in the order they were written. */
6617 for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
6618 vec[i] = decl;
6620 wrapup_global_declarations (vec, len);
6622 check_global_declarations (vec, len);
6624 /* Clean up. */
6625 free (vec);
6629 /* Reset the parser's state in preparation for a new file. */
6631 void
6632 c_reset_state (void)
6634 tree link;
6635 tree file_scope_decl;
6637 /* Pop the global scope. */
6638 if (current_scope != global_scope)
6639 current_scope = global_scope;
6640 file_scope_decl = current_file_decl;
6641 DECL_INITIAL (file_scope_decl) = poplevel (1, 0, 0);
6642 BLOCK_SUPERCONTEXT (DECL_INITIAL (file_scope_decl)) = file_scope_decl;
6643 truly_local_externals = NULL_TREE;
6645 /* Start a new global binding level. */
6646 pushlevel (0);
6647 global_scope = current_scope;
6648 current_file_decl = build_decl (TRANSLATION_UNIT_DECL, NULL, NULL);
6649 TREE_CHAIN (current_file_decl) = file_scope_decl;
6651 /* Reintroduce the builtin declarations. */
6652 for (link = first_builtin_decl;
6653 link != TREE_CHAIN (last_builtin_decl);
6654 link = TREE_CHAIN (link))
6655 pushdecl (copy_node (link));
6658 #include "gt-c-decl.h"