2003-09-21 Toon Moene <toon@moene.indiv.nluug.nl>
[official-gcc.git] / gcc / c-decl.c
blobdb35f9a9ea9cf127f400d724fc450ea4b57cdb11
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 /* Nonzero if we are currently in the global scope. */
416 global_bindings_p (void)
418 return current_scope == global_scope;
421 void
422 keep_next_level (void)
424 keep_next_level_flag = true;
427 /* Identify this scope as currently being filled with parameters. */
429 void
430 declare_parm_level (void)
432 current_scope->parm_flag = true;
435 /* Nonzero if currently making parm declarations. */
438 in_parm_level_p (void)
440 return current_scope->parm_flag;
443 /* Enter a new scope. The dummy parameter is for signature
444 compatibility with lang_hooks.decls.pushlevel. */
446 void
447 pushlevel (int dummy ATTRIBUTE_UNUSED)
449 if (next_is_function_body)
451 /* This is the transition from the parameters to the top level
452 of the function body. These are the same scope
453 (C99 6.2.1p4,6) so we do not push another scope structure.
454 next_is_function_body is set only by store_parm_decls, which
455 in turn is called when and only when we are about to
456 encounter the opening curly brace for the function body.
458 The outermost block of a function always gets a BLOCK node,
459 because the debugging output routines expect that each
460 function has at least one BLOCK. */
461 current_scope->parm_flag = false;
462 current_scope->function_body = true;
463 current_scope->keep = true;
464 current_scope->outer_function = current_function_scope;
465 current_function_scope = current_scope;
467 keep_next_level_flag = false;
468 next_is_function_body = false;
470 else
472 struct c_scope *scope = make_scope ();
474 scope->keep = keep_next_level_flag;
475 scope->outer = current_scope;
476 current_scope = scope;
477 keep_next_level_flag = false;
481 /* Exit a scope. Restore the state of the identifier-decl mappings
482 that were in effect when this scope was entered.
484 If KEEP is KEEP_YES (1), this scope had explicit declarations, so
485 create a BLOCK node to record its declarations and subblocks for
486 debugging output. If KEEP is KEEP_MAYBE, do so only if the names
487 or tags lists are nonempty.
489 The second parameter is ignored; it is present only for
490 signature compatibility with lang_hooks.decls.poplevel.
492 If FUNCTIONBODY is nonzero, this level is the body of a function,
493 even if current_scope->function_body is not set. This is used
494 by language-independent code that generates synthetic functions,
495 and cannot set current_scope->function_body.
497 FIXME: Eliminate the need for all arguments. */
499 tree
500 poplevel (int keep, int dummy ATTRIBUTE_UNUSED, int functionbody)
502 struct c_scope *scope = current_scope;
503 tree block;
504 tree decl;
505 tree p;
507 /* The following line does not use |= due to a bug in HP's C compiler */
508 scope->function_body = scope->function_body | functionbody;
510 if (keep == KEEP_MAYBE)
511 keep = (scope->names || scope->tags);
513 keep |= scope->keep;
514 keep |= scope->function_body;
516 /* If appropriate, create a BLOCK to record the decls for the life
517 of this function. */
518 block = 0;
519 if (keep)
521 block = make_node (BLOCK);
522 BLOCK_VARS (block) = scope->names;
523 BLOCK_SUBBLOCKS (block) = scope->blocks;
524 TREE_USED (block) = 1;
527 /* In each subblock, record that this is its superior. */
528 for (p = scope->blocks; p; p = TREE_CHAIN (p))
529 BLOCK_SUPERCONTEXT (p) = block;
531 /* Clear out the variable bindings in this scope.
533 Propagate TREE_ADDRESSABLE from nested functions to their
534 containing functions.
536 Issue warnings for unused variables and labels, and errors for
537 undefined labels, if there are any. */
539 for (p = scope->names; p; p = TREE_CHAIN (p))
541 switch (TREE_CODE (p))
543 case LABEL_DECL:
544 if (TREE_USED (p) && !DECL_INITIAL (p))
546 error ("%Jlabel `%D' used but not defined", p, p);
547 DECL_INITIAL (p) = error_mark_node;
549 else if (!TREE_USED (p) && warn_unused_label)
551 if (DECL_INITIAL (p))
552 warning ("%Jlabel `%D' defined but not used", p, p);
553 else
554 warning ("%Jlabel `%D' declared but not defined", p, p);
557 IDENTIFIER_LABEL_VALUE (DECL_NAME (p)) = 0;
558 break;
560 case FUNCTION_DECL:
561 if (! TREE_ASM_WRITTEN (p)
562 && DECL_INITIAL (p) != 0
563 && TREE_ADDRESSABLE (p)
564 && DECL_ABSTRACT_ORIGIN (p) != 0
565 && DECL_ABSTRACT_ORIGIN (p) != p)
566 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
567 goto normal;
569 case VAR_DECL:
570 /* Keep this in sync with stmt.c:warn_about_unused_variables.
571 No warnings when the global scope is popped because the
572 global scope isn't popped for the last translation unit,
573 so the warnings are done in c_write_global_declaration. */
574 if (warn_unused_variable && scope != global_scope
575 && !TREE_USED (p)
576 && !DECL_IN_SYSTEM_HEADER (p)
577 && DECL_NAME (p)
578 && !DECL_ARTIFICIAL (p))
579 warning ("%Junused variable `%D'", p, p);
580 /* fall through */
582 default:
583 normal:
584 if (DECL_NAME (p))
586 if (DECL_EXTERNAL (p) && scope != global_scope)
587 /* External decls stay in the symbol-value slot but are
588 inaccessible. */
589 C_DECL_INVISIBLE (p) = 1;
590 else
591 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (p)) = 0;
593 break;
597 /* Clear out the parameter bindings in this scope, if any.
598 Unused-parameter warnings are handled by function.c. */
599 for (p = scope->parms; p; p = TREE_CHAIN (p))
600 if (DECL_NAME (p))
601 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (p)) = 0;
603 /* Clear out the tag-meanings declared in this scope.
605 Set the TYPE_CONTEXTs for all of the tagged types belonging to
606 this scope so that they point to the appropriate construct, i.e.
607 either to the current FUNCTION_DECL node, or else to the BLOCK
608 node we just constructed.
610 Note that for tagged types whose scope is just the formal
611 parameter list for some function type specification, we can't
612 properly set their TYPE_CONTEXTs here, because we don't have a
613 pointer to the appropriate FUNCTION_TYPE node readily available
614 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
615 type nodes get set in `grokdeclarator' as soon as we have created
616 the FUNCTION_TYPE node which will represent the "scope" for these
617 "parameter list local" tagged types. */
619 decl = scope->function_body ? current_function_decl : block;
620 for (p = scope->tags; p; p = TREE_CHAIN (p))
622 if (TREE_PURPOSE (p))
623 IDENTIFIER_TAG_VALUE (TREE_PURPOSE (p)) = 0;
624 if (decl)
625 TYPE_CONTEXT (TREE_VALUE (p)) = decl;
628 /* Restore all name- and label-meanings from outer scopes that were
629 shadowed by this scope. */
630 for (p = scope->shadowed; p; p = TREE_CHAIN (p))
631 if (TREE_VALUE (p) && TREE_CODE (TREE_VALUE (p)) == LABEL_DECL)
632 IDENTIFIER_LABEL_VALUE (TREE_PURPOSE (p)) = TREE_VALUE (p);
633 else
634 IDENTIFIER_SYMBOL_VALUE (TREE_PURPOSE (p)) = TREE_VALUE (p);
636 /* Restore all tag-meanings from outer scopes that were shadowed by
637 this scope. */
638 for (p = scope->shadowed_tags; p; p = TREE_CHAIN (p))
639 IDENTIFIER_TAG_VALUE (TREE_PURPOSE (p)) = TREE_VALUE (p);
641 /* Dispose of the block that we just made inside some higher level. */
642 if (scope->function_body)
643 DECL_INITIAL (current_function_decl) = block;
644 else if (scope->outer)
646 if (block)
647 SCOPE_LIST_APPEND (scope->outer, blocks, block);
648 /* If we did not make a block for the scope just exited, any
649 blocks made for inner scopes must be carried forward so they
650 will later become subblocks of something else. */
651 else if (scope->blocks)
652 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
655 /* Pop the current scope, and free the structure for reuse. */
656 pop_scope ();
658 return block;
661 /* Insert BLOCK at the end of the list of subblocks of the current
662 scope. This is used when a BIND_EXPR is expanded, to handle the
663 BLOCK node inside the BIND_EXPR. */
665 void
666 insert_block (tree block)
668 TREE_USED (block) = 1;
669 SCOPE_LIST_APPEND (current_scope, blocks, block);
672 /* Set the BLOCK node for the innermost scope (the one we are
673 currently in). The RTL expansion machinery requires us to provide
674 this hook, but it is not useful in function-at-a-time mode. */
676 void
677 set_block (tree block ATTRIBUTE_UNUSED)
681 /* Push a definition or a declaration of struct, union or enum tag "name".
682 "type" should be the type node.
683 We assume that the tag "name" is not already defined.
685 Note that the definition may really be just a forward reference.
686 In that case, the TYPE_SIZE will be zero. */
688 void
689 pushtag (tree name, tree type)
691 struct c_scope *b = current_scope;
693 /* Record the identifier as the type's name if it has none. */
694 if (name)
696 if (TYPE_NAME (type) == 0)
697 TYPE_NAME (type) = name;
699 if (IDENTIFIER_TAG_VALUE (name))
700 b->shadowed_tags = tree_cons (name, IDENTIFIER_TAG_VALUE (name),
701 b->shadowed_tags);
702 IDENTIFIER_TAG_VALUE (name) = type;
705 b->tags = tree_cons (name, type, b->tags);
707 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
708 tagged type we just added to the current scope. This fake
709 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
710 to output a representation of a tagged type, and it also gives
711 us a convenient place to record the "scope start" address for the
712 tagged type. */
714 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
716 /* An approximation for now, so we can tell this is a function-scope tag.
717 This will be updated in poplevel. */
718 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
721 /* Subroutine of duplicate_decls. Allow harmless mismatches in return
722 and argument types provided that the type modes match. This function
723 return a unified type given a suitable match, and 0 otherwise. */
725 static tree
726 match_builtin_function_types (tree oldtype, tree newtype)
728 tree newrettype, oldrettype;
729 tree newargs, oldargs;
730 tree trytype, tryargs;
732 /* Accept the return type of the new declaration if same modes. */
733 oldrettype = TREE_TYPE (oldtype);
734 newrettype = TREE_TYPE (newtype);
736 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
737 return 0;
739 oldargs = TYPE_ARG_TYPES (oldtype);
740 newargs = TYPE_ARG_TYPES (newtype);
741 tryargs = newargs;
743 while (oldargs || newargs)
745 if (! oldargs
746 || ! newargs
747 || ! TREE_VALUE (oldargs)
748 || ! TREE_VALUE (newargs)
749 || TYPE_MODE (TREE_VALUE (oldargs))
750 != TYPE_MODE (TREE_VALUE (newargs)))
751 return 0;
753 oldargs = TREE_CHAIN (oldargs);
754 newargs = TREE_CHAIN (newargs);
757 trytype = build_function_type (newrettype, tryargs);
758 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
761 /* Handle when a new declaration NEWDECL
762 has the same name as an old one OLDDECL
763 in the same binding contour.
764 Prints an error message if appropriate.
766 If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
767 Otherwise, return 0.
769 When DIFFERENT_BINDING_LEVEL is true, NEWDECL is an external declaration,
770 and OLDDECL is in an outer scope and should thus not be changed. */
772 static int
773 duplicate_decls (tree newdecl, tree olddecl, int different_binding_level,
774 int different_tu)
776 int types_match = comptypes (TREE_TYPE (newdecl), TREE_TYPE (olddecl),
777 COMPARE_STRICT);
778 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
779 && DECL_INITIAL (newdecl) != 0);
780 tree oldtype = TREE_TYPE (olddecl);
781 tree newtype = TREE_TYPE (newdecl);
782 int errmsg = 0;
784 if (DECL_P (olddecl))
786 if (TREE_CODE (newdecl) == FUNCTION_DECL
787 && TREE_CODE (olddecl) == FUNCTION_DECL
788 && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
790 if (DECL_DECLARED_INLINE_P (newdecl)
791 && DECL_UNINLINABLE (newdecl)
792 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
793 /* Already warned elsewhere. */;
794 else if (DECL_DECLARED_INLINE_P (olddecl)
795 && DECL_UNINLINABLE (olddecl)
796 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
797 /* Already warned. */;
798 else if (DECL_DECLARED_INLINE_P (newdecl)
799 && ! DECL_DECLARED_INLINE_P (olddecl)
800 && DECL_UNINLINABLE (olddecl)
801 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
803 warning ("%Jfunction '%D' redeclared as inline",
804 newdecl, newdecl);
805 warning ("%Jprevious declaration of function '%D' "
806 "with attribute noinline", olddecl, olddecl);
808 else if (DECL_DECLARED_INLINE_P (olddecl)
809 && DECL_UNINLINABLE (newdecl)
810 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
812 warning ("%Jfunction '%D' redeclared with attribute noinline",
813 newdecl, newdecl);
814 warning ("%Jprevious declaration of function '%D' was inline",
815 olddecl, olddecl);
819 DECL_ATTRIBUTES (newdecl)
820 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
823 if (TREE_CODE (newtype) == ERROR_MARK
824 || TREE_CODE (oldtype) == ERROR_MARK)
825 types_match = 0;
827 /* New decl is completely inconsistent with the old one =>
828 tell caller to replace the old one.
829 This is always an error except in the case of shadowing a builtin. */
830 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
832 if (TREE_CODE (olddecl) == FUNCTION_DECL
833 && DECL_BUILT_IN (olddecl))
835 /* If you declare a built-in or predefined function name as static,
836 the old definition is overridden,
837 but optionally warn this was a bad choice of name. */
838 if (!TREE_PUBLIC (newdecl))
840 if (warn_shadow)
841 warning ("%Jshadowing built-in function '%D'",
842 newdecl, newdecl);
844 else
845 warning ("%Jbuilt-in function '%D' declared as non-function",
846 newdecl, newdecl);
848 else
850 error ("%J'%D' redeclared as different kind of symbol",
851 newdecl, newdecl);
852 error ("%Jprevious declaration of '%D'", olddecl, olddecl);
855 return 0;
858 /* For real parm decl following a forward decl, return 1 so old decl
859 will be reused. Only allow this to happen once. */
860 if (types_match && TREE_CODE (newdecl) == PARM_DECL
861 && TREE_ASM_WRITTEN (olddecl) && ! TREE_ASM_WRITTEN (newdecl))
863 TREE_ASM_WRITTEN (olddecl) = 0;
864 return 1;
867 /* The new declaration is the same kind of object as the old one.
868 The declarations may partially match. Print warnings if they don't
869 match enough. Ultimately, copy most of the information from the new
870 decl to the old one, and keep using the old one. */
872 if (TREE_CODE (olddecl) == FUNCTION_DECL && DECL_BUILT_IN (olddecl))
874 /* A function declaration for a built-in function. */
875 if (!TREE_PUBLIC (newdecl))
877 /* If you declare a built-in function name as static, the
878 built-in definition is overridden,
879 but optionally warn this was a bad choice of name. */
880 if (warn_shadow)
881 warning ("%Jshadowing built-in function '%D'", newdecl, newdecl);
882 /* Discard the old built-in function. */
883 return 0;
885 if (!types_match)
887 /* Accept harmless mismatch in function types.
888 This is for the ffs and fprintf builtins. */
889 tree trytype = match_builtin_function_types (oldtype, newtype);
891 if (trytype)
893 types_match = comptypes (newtype, trytype, COMPARE_STRICT);
894 if (types_match)
895 oldtype = trytype;
896 if (! different_binding_level)
897 TREE_TYPE (olddecl) = oldtype;
900 if (!types_match)
902 /* If types don't match for a built-in, throw away the built-in. */
903 warning ("%Jconflicting types for built-in function '%D'",
904 newdecl, newdecl);
905 return 0;
908 else if (TREE_CODE (olddecl) == FUNCTION_DECL
909 && DECL_SOURCE_LINE (olddecl) == 0)
911 /* A function declaration for a predeclared function
912 that isn't actually built in. */
913 if (!TREE_PUBLIC (newdecl))
915 /* If you declare it as static, the
916 default definition is overridden. */
917 return 0;
919 else if (!types_match)
921 /* If the types don't match, preserve volatility indication.
922 Later on, we will discard everything else about the
923 default declaration. */
924 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
927 /* Permit char *foo () to match void *foo (...) if not pedantic,
928 if one of them came from a system header file. */
929 else if (!types_match
930 && TREE_CODE (olddecl) == FUNCTION_DECL
931 && TREE_CODE (newdecl) == FUNCTION_DECL
932 && TREE_CODE (TREE_TYPE (oldtype)) == POINTER_TYPE
933 && TREE_CODE (TREE_TYPE (newtype)) == POINTER_TYPE
934 && (DECL_IN_SYSTEM_HEADER (olddecl)
935 || DECL_IN_SYSTEM_HEADER (newdecl))
936 && ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (newtype))) == void_type_node
937 && TYPE_ARG_TYPES (oldtype) == 0
938 && self_promoting_args_p (TYPE_ARG_TYPES (newtype))
939 && TREE_TYPE (TREE_TYPE (oldtype)) == char_type_node)
941 (TREE_TYPE (TREE_TYPE (newtype)) == char_type_node
942 && TYPE_ARG_TYPES (newtype) == 0
943 && self_promoting_args_p (TYPE_ARG_TYPES (oldtype))
944 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (oldtype))) == void_type_node)))
946 if (pedantic)
947 pedwarn ("%Jconflicting types for '%D'", newdecl, newdecl);
948 /* Make sure we keep void * as ret type, not char *. */
949 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (oldtype))) == void_type_node)
950 TREE_TYPE (newdecl) = newtype = oldtype;
952 /* Set DECL_IN_SYSTEM_HEADER, so that if we see another declaration
953 we will come back here again. */
954 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
956 /* Permit void foo (...) to match int foo (...) if the latter is the
957 definition and implicit int was used. See c-torture/compile/920625-2.c. */
958 else if (!types_match && new_is_definition
959 && TREE_CODE (olddecl) == FUNCTION_DECL
960 && TREE_CODE (newdecl) == FUNCTION_DECL
961 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
962 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
963 && C_FUNCTION_IMPLICIT_INT (newdecl))
965 pedwarn ("%Jconflicting types for '%D'", newdecl, newdecl);
966 /* Make sure we keep void as the return type. */
967 TREE_TYPE (newdecl) = newtype = oldtype;
968 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
970 else if (!types_match
971 /* Permit char *foo (int, ...); followed by char *foo ();
972 if not pedantic. */
973 && ! (TREE_CODE (olddecl) == FUNCTION_DECL
974 && ! pedantic
975 /* Return types must still match. */
976 && comptypes (TREE_TYPE (oldtype),
977 TREE_TYPE (newtype), COMPARE_STRICT)
978 && TYPE_ARG_TYPES (newtype) == 0))
980 error ("%Jconflicting types for '%D'", newdecl, newdecl);
981 /* Check for function type mismatch
982 involving an empty arglist vs a nonempty one. */
983 if (TREE_CODE (olddecl) == FUNCTION_DECL
984 && comptypes (TREE_TYPE (oldtype),
985 TREE_TYPE (newtype), COMPARE_STRICT)
986 && ((TYPE_ARG_TYPES (oldtype) == 0
987 && DECL_INITIAL (olddecl) == 0)
989 (TYPE_ARG_TYPES (newtype) == 0
990 && DECL_INITIAL (newdecl) == 0)))
992 /* Classify the problem further. */
993 tree t = TYPE_ARG_TYPES (oldtype);
994 if (t == 0)
995 t = TYPE_ARG_TYPES (newtype);
996 for (; t; t = TREE_CHAIN (t))
998 tree type = TREE_VALUE (t);
1000 if (TREE_CHAIN (t) == 0
1001 && TYPE_MAIN_VARIANT (type) != void_type_node)
1003 error ("a parameter list with an ellipsis can't match an empty parameter name list declaration");
1004 break;
1007 if (c_type_promotes_to (type) != type)
1009 error ("an argument type that has a default promotion can't match an empty parameter name list declaration");
1010 break;
1014 if (C_DECL_IMPLICIT (olddecl))
1015 error ("%Jprevious implicit declaration of '%D'", olddecl, olddecl);
1016 else
1017 error ("%Jprevious declaration of '%D'", olddecl, olddecl);
1019 /* This is safer because the initializer might contain references
1020 to variables that were declared between olddecl and newdecl. This
1021 will make the initializer invalid for olddecl in case it gets
1022 assigned to olddecl below. */
1023 if (TREE_CODE (newdecl) == VAR_DECL)
1024 DECL_INITIAL (newdecl) = 0;
1026 /* TLS cannot follow non-TLS declaration. */
1027 else if (TREE_CODE (olddecl) == VAR_DECL && TREE_CODE (newdecl) == VAR_DECL
1028 && !DECL_THREAD_LOCAL (olddecl) && DECL_THREAD_LOCAL (newdecl))
1030 error ("%Jthread-local declaration of '%D' follows non thread-local "
1031 "declaration", newdecl, newdecl);
1032 error ("%Jprevious declaration of '%D'", olddecl, olddecl);
1034 /* non-TLS declaration cannot follow TLS declaration. */
1035 else if (TREE_CODE (olddecl) == VAR_DECL && TREE_CODE (newdecl) == VAR_DECL
1036 && DECL_THREAD_LOCAL (olddecl) && !DECL_THREAD_LOCAL (newdecl))
1038 error ("%Jnon thread-local declaration of '%D' follows "
1039 "thread-local declaration", newdecl, newdecl);
1040 error ("%Jprevious declaration of '%D'", olddecl, olddecl);
1042 else
1044 errmsg = redeclaration_error_message (newdecl, olddecl);
1045 if (errmsg)
1047 switch (errmsg)
1049 case 1:
1050 error ("%Jredefinition of '%D'", newdecl, newdecl);
1051 break;
1052 case 2:
1053 error ("%Jredeclaration of '%D'", newdecl, newdecl);
1054 break;
1055 case 3:
1056 error ("%Jconflicting declarations of '%D'", newdecl, newdecl);
1057 break;
1058 default:
1059 abort ();
1062 if (DECL_INITIAL (olddecl)
1063 && current_scope == global_scope)
1064 error ("%J'%D' previously defined here", olddecl, olddecl);
1065 else
1066 error ("%J'%D' previously declared here", olddecl, olddecl);
1067 return 0;
1069 else if (TREE_CODE (newdecl) == TYPE_DECL
1070 && (DECL_IN_SYSTEM_HEADER (olddecl)
1071 || DECL_IN_SYSTEM_HEADER (newdecl)))
1073 warning ("%Jredefinition of '%D'", newdecl, newdecl);
1074 if (DECL_INITIAL (olddecl) && current_scope == global_scope)
1075 warning ("%J'%D' previously defined here", olddecl, olddecl);
1076 else
1077 warning ("%J'%D' previously declared here", olddecl, olddecl);
1079 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1080 && DECL_INITIAL (olddecl) != 0
1081 && TYPE_ARG_TYPES (oldtype) == 0
1082 && TYPE_ARG_TYPES (newtype) != 0
1083 && TYPE_ACTUAL_ARG_TYPES (oldtype) != 0)
1085 tree type, parm;
1086 int nargs;
1087 /* Prototype decl follows defn w/o prototype. */
1089 for (parm = TYPE_ACTUAL_ARG_TYPES (oldtype),
1090 type = TYPE_ARG_TYPES (newtype),
1091 nargs = 1;
1093 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type), nargs++)
1095 if (TYPE_MAIN_VARIANT (TREE_VALUE (parm)) == void_type_node
1096 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
1098 warning ("%Jprototype for '%D' follows", newdecl, newdecl);
1099 warning ("%Jnon-prototype definition here", olddecl);
1100 break;
1102 if (TYPE_MAIN_VARIANT (TREE_VALUE (parm)) == void_type_node
1103 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
1105 error ("%Jprototype for '%D' follows and number of "
1106 "arguments doesn't match", newdecl, newdecl);
1107 error ("%Jnon-prototype definition here", olddecl);
1108 errmsg = 1;
1109 break;
1111 /* Type for passing arg must be consistent
1112 with that declared for the arg. */
1113 if (! comptypes (TREE_VALUE (parm), TREE_VALUE (type),
1114 COMPARE_STRICT))
1116 error ("%Jprototype for '%D' follows and argument %d "
1117 "doesn't match", newdecl, newdecl, nargs);
1118 error ("%Jnon-prototype definition here", olddecl);
1119 errmsg = 1;
1120 break;
1124 /* Warn about mismatches in various flags. */
1125 else
1127 /* Warn if function is now inline
1128 but was previously declared not inline and has been called. */
1129 if (TREE_CODE (olddecl) == FUNCTION_DECL
1130 && ! DECL_DECLARED_INLINE_P (olddecl)
1131 && DECL_DECLARED_INLINE_P (newdecl)
1132 && TREE_USED (olddecl))
1133 warning ("%J'%D' declared inline after being called",
1134 newdecl, newdecl);
1135 if (TREE_CODE (olddecl) == FUNCTION_DECL
1136 && ! DECL_DECLARED_INLINE_P (olddecl)
1137 && DECL_DECLARED_INLINE_P (newdecl)
1138 && DECL_INITIAL (olddecl) != 0)
1139 warning ("%J'%D' declared inline after its definition",
1140 newdecl, newdecl);
1142 /* If pedantic, warn when static declaration follows a non-static
1143 declaration. Otherwise, do so only for functions. */
1144 if ((pedantic || TREE_CODE (olddecl) == FUNCTION_DECL)
1145 && TREE_PUBLIC (olddecl)
1146 && !TREE_PUBLIC (newdecl))
1147 warning ("%Jstatic declaration for '%D' follows non-static",
1148 newdecl, newdecl);
1150 /* If warn_traditional, warn when a non-static function
1151 declaration follows a static one. */
1152 if (warn_traditional && !in_system_header
1153 && TREE_CODE (olddecl) == FUNCTION_DECL
1154 && !TREE_PUBLIC (olddecl)
1155 && TREE_PUBLIC (newdecl))
1156 warning ("%Jnon-static declaration for '%D' follows static",
1157 newdecl, newdecl);
1159 /* Warn when const declaration follows a non-const
1160 declaration, but not for functions. */
1161 if (TREE_CODE (olddecl) != FUNCTION_DECL
1162 && !TREE_READONLY (olddecl)
1163 && TREE_READONLY (newdecl))
1164 warning ("%Jconst declaration for '%D' follows non-const",
1165 newdecl, newdecl);
1166 /* These bits are logically part of the type, for variables.
1167 But not for functions
1168 (where qualifiers are not valid ANSI anyway). */
1169 else if (pedantic && TREE_CODE (olddecl) != FUNCTION_DECL
1170 && (TREE_READONLY (newdecl) != TREE_READONLY (olddecl)
1171 || TREE_THIS_VOLATILE (newdecl) != TREE_THIS_VOLATILE (olddecl)))
1172 pedwarn ("%Jtype qualifiers for '%D' conflict with previous "
1173 "declaration", newdecl, newdecl);
1177 /* Optionally warn about more than one declaration for the same name. */
1178 if (errmsg == 0 && warn_redundant_decls && DECL_SOURCE_LINE (olddecl) != 0
1179 /* Don't warn about a function declaration
1180 followed by a definition. */
1181 && !(TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl) != 0
1182 && DECL_INITIAL (olddecl) == 0)
1183 /* Don't warn about extern decl followed by (tentative) definition. */
1184 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl)))
1186 warning ("%Jredundant redeclaration of '%D' in same scope",
1187 newdecl, newdecl);
1188 warning ("%Jprevious declaration of '%D'", olddecl, olddecl);
1191 /* Copy all the DECL_... slots specified in the new decl
1192 except for any that we copy here from the old type.
1194 Past this point, we don't change OLDTYPE and NEWTYPE
1195 even if we change the types of NEWDECL and OLDDECL. */
1197 if (types_match)
1199 /* When copying info to olddecl, we store into write_olddecl
1200 instead. This allows us to avoid modifying olddecl when
1201 different_binding_level is true. */
1202 tree write_olddecl = different_binding_level ? newdecl : olddecl;
1204 /* Merge the data types specified in the two decls. */
1205 if (TREE_CODE (newdecl) != FUNCTION_DECL || !DECL_BUILT_IN (olddecl))
1207 if (different_binding_level)
1209 if (TYPE_ARG_TYPES (oldtype) != 0
1210 && TYPE_ARG_TYPES (newtype) == 0)
1211 TREE_TYPE (newdecl) = common_type (newtype, oldtype);
1212 else
1213 TREE_TYPE (newdecl)
1214 = build_type_attribute_variant
1215 (newtype,
1216 merge_attributes (TYPE_ATTRIBUTES (newtype),
1217 TYPE_ATTRIBUTES (oldtype)));
1219 else
1220 TREE_TYPE (newdecl)
1221 = TREE_TYPE (olddecl)
1222 = common_type (newtype, oldtype);
1225 /* Lay the type out, unless already done. */
1226 if (oldtype != TREE_TYPE (newdecl))
1228 if (TREE_TYPE (newdecl) != error_mark_node)
1229 layout_type (TREE_TYPE (newdecl));
1230 if (TREE_CODE (newdecl) != FUNCTION_DECL
1231 && TREE_CODE (newdecl) != TYPE_DECL
1232 && TREE_CODE (newdecl) != CONST_DECL)
1233 layout_decl (newdecl, 0);
1235 else
1237 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1238 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1239 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1240 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1241 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1242 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1244 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1245 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1249 /* Keep the old rtl since we can safely use it. */
1250 COPY_DECL_RTL (olddecl, newdecl);
1252 /* Merge the type qualifiers. */
1253 if (TREE_READONLY (newdecl))
1254 TREE_READONLY (write_olddecl) = 1;
1256 if (TREE_THIS_VOLATILE (newdecl))
1258 TREE_THIS_VOLATILE (write_olddecl) = 1;
1259 if (TREE_CODE (newdecl) == VAR_DECL
1260 /* If an automatic variable is re-declared in the same
1261 function scope, but the old declaration was not
1262 volatile, make_var_volatile() would crash because the
1263 variable would have been assigned to a pseudo, not a
1264 MEM. Since this duplicate declaration is invalid
1265 anyway, we just skip the call. */
1266 && errmsg == 0)
1267 make_var_volatile (newdecl);
1270 /* Keep source location of definition rather than declaration. */
1271 /* When called with different_binding_level set, keep the old
1272 information so that meaningful diagnostics can be given. */
1273 if (DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0
1274 && ! different_binding_level)
1276 DECL_SOURCE_LINE (newdecl) = DECL_SOURCE_LINE (olddecl);
1277 DECL_SOURCE_FILE (newdecl) = DECL_SOURCE_FILE (olddecl);
1280 /* Merge the unused-warning information. */
1281 if (DECL_IN_SYSTEM_HEADER (olddecl))
1282 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1283 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1284 DECL_IN_SYSTEM_HEADER (write_olddecl) = 1;
1286 /* Merge the initialization information. */
1287 /* When called with different_binding_level set, don't copy over
1288 DECL_INITIAL, so that we don't accidentally change function
1289 declarations into function definitions. */
1290 if (DECL_INITIAL (newdecl) == 0 && ! different_binding_level)
1291 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1293 /* Merge the section attribute.
1294 We want to issue an error if the sections conflict but that must be
1295 done later in decl_attributes since we are called before attributes
1296 are assigned. */
1297 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1298 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1300 /* Copy the assembler name.
1301 Currently, it can only be defined in the prototype. */
1302 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1304 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1306 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1307 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1308 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1309 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1310 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1311 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1312 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1313 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1314 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1317 /* If cannot merge, then use the new type and qualifiers,
1318 and don't preserve the old rtl. */
1319 else if (! different_binding_level)
1321 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1322 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
1323 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
1324 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
1327 /* Merge the storage class information. */
1328 merge_weak (newdecl, olddecl);
1330 /* For functions, static overrides non-static. */
1331 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1333 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1334 /* This is since we don't automatically
1335 copy the attributes of NEWDECL into OLDDECL. */
1336 /* No need to worry about different_binding_level here because
1337 then TREE_PUBLIC (newdecl) was true. */
1338 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1339 /* If this clears `static', clear it in the identifier too. */
1340 if (! TREE_PUBLIC (olddecl))
1341 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1343 if (DECL_EXTERNAL (newdecl))
1345 if (! different_binding_level || different_tu)
1347 /* Don't mess with these flags on local externs; they remain
1348 external even if there's a declaration at file scope which
1349 isn't. */
1350 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1351 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1353 /* An extern decl does not override previous storage class. */
1354 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1355 if (! DECL_EXTERNAL (newdecl))
1357 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1358 /* If we have two non-EXTERNAL file-scope decls that are
1359 the same, only one of them should be written out. */
1360 if (different_tu)
1361 TREE_ASM_WRITTEN (newdecl) = 1;
1364 else
1366 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1367 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1370 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1372 /* If we're redefining a function previously defined as extern
1373 inline, make sure we emit debug info for the inline before we
1374 throw it away, in case it was inlined into a function that hasn't
1375 been written out yet. */
1376 if (new_is_definition && DECL_INITIAL (olddecl))
1378 if (TREE_USED (olddecl))
1379 (*debug_hooks->outlining_inline_function) (olddecl);
1381 /* The new defn must not be inline. */
1382 DECL_INLINE (newdecl) = 0;
1383 DECL_UNINLINABLE (newdecl) = 1;
1385 else
1387 /* If either decl says `inline', this fn is inline,
1388 unless its definition was passed already. */
1389 if (DECL_DECLARED_INLINE_P (newdecl)
1390 || DECL_DECLARED_INLINE_P (olddecl))
1391 DECL_DECLARED_INLINE_P (newdecl) = 1;
1393 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1394 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1397 if (DECL_BUILT_IN (olddecl))
1399 /* Get rid of any built-in function if new arg types don't match it
1400 or if we have a function definition. */
1401 if (! types_match || new_is_definition)
1403 if (! different_binding_level)
1405 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1406 DECL_BUILT_IN_CLASS (olddecl) = NOT_BUILT_IN;
1409 else
1411 /* If redeclaring a builtin function, and not a definition,
1412 it stays built in. */
1413 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1414 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1418 /* Also preserve various other info from the definition. */
1419 if (! new_is_definition)
1421 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1422 /* When called with different_binding_level set, don't copy over
1423 DECL_INITIAL, so that we don't accidentally change function
1424 declarations into function definitions. */
1425 if (! different_binding_level)
1426 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1427 DECL_SAVED_INSNS (newdecl) = DECL_SAVED_INSNS (olddecl);
1428 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1429 DECL_ESTIMATED_INSNS (newdecl) = DECL_ESTIMATED_INSNS (olddecl);
1430 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1432 /* Set DECL_INLINE on the declaration if we've got a body
1433 from which to instantiate. */
1434 if (DECL_INLINE (olddecl) && ! DECL_UNINLINABLE (newdecl))
1436 DECL_INLINE (newdecl) = 1;
1437 DECL_ABSTRACT_ORIGIN (newdecl)
1438 = (different_binding_level
1439 ? DECL_ORIGIN (olddecl)
1440 : DECL_ABSTRACT_ORIGIN (olddecl));
1443 else
1445 /* If a previous declaration said inline, mark the
1446 definition as inlinable. */
1447 if (DECL_DECLARED_INLINE_P (newdecl)
1448 && ! DECL_UNINLINABLE (newdecl))
1449 DECL_INLINE (newdecl) = 1;
1452 if (different_binding_level)
1453 return 0;
1455 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1456 But preserve OLDDECL's DECL_UID. */
1458 unsigned olddecl_uid = DECL_UID (olddecl);
1460 memcpy ((char *) olddecl + sizeof (struct tree_common),
1461 (char *) newdecl + sizeof (struct tree_common),
1462 sizeof (struct tree_decl) - sizeof (struct tree_common));
1463 DECL_UID (olddecl) = olddecl_uid;
1466 /* NEWDECL contains the merged attribute lists.
1467 Update OLDDECL to be the same. */
1468 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
1470 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1471 so that encode_section_info has a chance to look at the new decl
1472 flags and attributes. */
1473 if (DECL_RTL_SET_P (olddecl)
1474 && (TREE_CODE (olddecl) == FUNCTION_DECL
1475 || (TREE_CODE (olddecl) == VAR_DECL
1476 && TREE_STATIC (olddecl))))
1477 make_decl_rtl (olddecl, NULL);
1479 return 1;
1482 /* Return any external DECL associated with ID, whether or not it is
1483 currently in scope. */
1485 static tree
1486 any_external_decl (tree id)
1488 tree decl = IDENTIFIER_SYMBOL_VALUE (id);
1489 tree t;
1491 if (decl == 0 || TREE_CODE (decl) == ERROR_MARK)
1492 return 0;
1493 else if (TREE_CODE (decl) != TYPE_DECL && DECL_EXTERNAL (decl))
1494 return decl;
1496 t = purpose_member (id, truly_local_externals);
1497 if (t)
1498 return TREE_VALUE (t);
1500 return 0;
1503 /* Record an external decl DECL. This only does something if a
1504 shadowing decl already exists. */
1505 static void
1506 record_external_decl (tree decl)
1508 tree name = DECL_NAME (decl);
1509 if (!IDENTIFIER_SYMBOL_VALUE (name))
1510 return;
1512 truly_local_externals = tree_cons (name, decl, truly_local_externals);
1515 /* Check whether decl-node X shadows an existing declaration.
1516 OLD is the old IDENTIFIER_SYMBOL_VALUE of the DECL_NAME of X,
1517 which might be a NULL_TREE. */
1518 static void
1519 warn_if_shadowing (tree x, tree old)
1521 const char *name;
1523 /* Nothing to shadow? */
1524 if (old == 0
1525 /* Shadow warnings not wanted? */
1526 || !warn_shadow
1527 /* No shadow warnings for internally generated vars. */
1528 || DECL_SOURCE_LINE (x) == 0
1529 /* No shadow warnings for vars made for inlining. */
1530 || DECL_FROM_INLINE (x)
1531 /* Don't warn about the parm names in function declarator
1532 within a function declarator.
1533 It would be nice to avoid warning in any function
1534 declarator in a declaration, as opposed to a definition,
1535 but there is no way to tell it's not a definition. */
1536 || (TREE_CODE (x) == PARM_DECL && current_scope->outer->parm_flag))
1537 return;
1539 name = IDENTIFIER_POINTER (DECL_NAME (x));
1540 if (TREE_CODE (old) == PARM_DECL)
1541 shadow_warning (SW_PARAM, name, old);
1542 else if (DECL_FILE_SCOPE_P (old))
1543 shadow_warning (SW_GLOBAL, name, old);
1544 else
1545 shadow_warning (SW_LOCAL, name, old);
1549 /* Subroutine of pushdecl.
1551 X is a TYPE_DECL for a typedef statement. Create a brand new
1552 ..._TYPE node (which will be just a variant of the existing
1553 ..._TYPE node with identical properties) and then install X
1554 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1556 The whole point here is to end up with a situation where each
1557 and every ..._TYPE node the compiler creates will be uniquely
1558 associated with AT MOST one node representing a typedef name.
1559 This way, even though the compiler substitutes corresponding
1560 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1561 early on, later parts of the compiler can always do the reverse
1562 translation and get back the corresponding typedef name. For
1563 example, given:
1565 typedef struct S MY_TYPE;
1566 MY_TYPE object;
1568 Later parts of the compiler might only know that `object' was of
1569 type `struct S' if it were not for code just below. With this
1570 code however, later parts of the compiler see something like:
1572 struct S' == struct S
1573 typedef struct S' MY_TYPE;
1574 struct S' object;
1576 And they can then deduce (from the node for type struct S') that
1577 the original object declaration was:
1579 MY_TYPE object;
1581 Being able to do this is important for proper support of protoize,
1582 and also for generating precise symbolic debugging information
1583 which takes full account of the programmer's (typedef) vocabulary.
1585 Obviously, we don't want to generate a duplicate ..._TYPE node if
1586 the TYPE_DECL node that we are now processing really represents a
1587 standard built-in type.
1589 Since all standard types are effectively declared at line zero
1590 in the source file, we can easily check to see if we are working
1591 on a standard type by checking the current value of lineno. */
1593 static void
1594 clone_underlying_type (tree x)
1596 if (DECL_SOURCE_LINE (x) == 0)
1598 if (TYPE_NAME (TREE_TYPE (x)) == 0)
1599 TYPE_NAME (TREE_TYPE (x)) = x;
1601 else if (TREE_TYPE (x) != error_mark_node
1602 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
1604 tree tt = TREE_TYPE (x);
1605 DECL_ORIGINAL_TYPE (x) = tt;
1606 tt = build_type_copy (tt);
1607 TYPE_NAME (tt) = x;
1608 TREE_USED (tt) = TREE_USED (x);
1609 TREE_TYPE (x) = tt;
1613 /* Record a decl-node X as belonging to the current lexical scope.
1614 Check for errors (such as an incompatible declaration for the same
1615 name already seen in the same scope).
1617 Returns either X or an old decl for the same name.
1618 If an old decl is returned, it may have been smashed
1619 to agree with what X says. */
1621 tree
1622 pushdecl (tree x)
1624 tree name = DECL_NAME (x);
1625 struct c_scope *scope = current_scope;
1627 #ifdef ENABLE_CHECKING
1628 if (error_mark_node == 0)
1629 /* Called too early. */
1630 abort ();
1631 #endif
1633 /* Functions need the lang_decl data. */
1634 if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_LANG_SPECIFIC (x))
1635 DECL_LANG_SPECIFIC (x) = ggc_alloc_cleared (sizeof (struct lang_decl));
1637 /* A local extern declaration for a function doesn't constitute nesting.
1638 A local auto declaration does, since it's a forward decl
1639 for a nested function coming later. */
1640 if (current_function_decl == NULL
1641 || ((TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
1642 && DECL_INITIAL (x) == 0 && DECL_EXTERNAL (x)))
1643 DECL_CONTEXT (x) = current_file_decl;
1644 else
1645 DECL_CONTEXT (x) = current_function_decl;
1647 if (name)
1649 tree old;
1651 if (warn_nested_externs
1652 && scope != global_scope
1653 && DECL_EXTERNAL (x)
1654 && !DECL_IN_SYSTEM_HEADER (x))
1655 warning ("nested extern declaration of `%s'",
1656 IDENTIFIER_POINTER (name));
1658 old = lookup_name_current_level (name);
1659 if (old && duplicate_decls (x, old, 0, false))
1661 /* For PARM_DECLs, old may be a forward declaration.
1662 If so, we want to remove it from its old location
1663 (in the variables chain) and rechain it in the
1664 location given by the new declaration. */
1665 if (TREE_CODE (x) == PARM_DECL)
1667 tree *p;
1668 for (p = &scope->names; *p; p = &TREE_CHAIN (*p))
1669 if (*p == old)
1671 *p = TREE_CHAIN (old);
1672 SCOPE_LIST_APPEND (scope, parms, old);
1673 break;
1676 return old;
1678 if (DECL_EXTERNAL (x) || scope == global_scope)
1680 /* Find and check against a previous, not-in-scope, external
1681 decl for this identifier. (C99 s???: If two declarations
1682 with external linkage, referring to the same object, have
1683 incompatible types, the behavior is undefined). */
1684 tree ext = any_external_decl (name);
1685 if (ext)
1687 if (duplicate_decls (x, ext, scope != global_scope,
1688 false))
1689 x = copy_node (ext);
1691 else
1692 record_external_decl (x);
1695 if (TREE_CODE (x) == TYPE_DECL)
1696 clone_underlying_type (x);
1698 /* If storing a local value, there may already be one
1699 (inherited). If so, record it for restoration when this
1700 scope ends. Take care not to do this if we are replacing an
1701 older decl in the same scope (i.e. duplicate_decls returned
1702 false, above). */
1703 if (scope != global_scope
1704 && IDENTIFIER_SYMBOL_VALUE (name)
1705 && IDENTIFIER_SYMBOL_VALUE (name) != old)
1707 warn_if_shadowing (x, IDENTIFIER_SYMBOL_VALUE (name));
1708 scope->shadowed = tree_cons (name, IDENTIFIER_SYMBOL_VALUE (name),
1709 scope->shadowed);
1712 /* Install the new declaration in the requested scope. */
1713 IDENTIFIER_SYMBOL_VALUE (name) = x;
1714 C_DECL_INVISIBLE (x) = 0;
1716 /* If x's type is incomplete because it's based on a
1717 structure or union which has not yet been fully declared,
1718 attach it to that structure or union type, so we can go
1719 back and complete the variable declaration later, if the
1720 structure or union gets fully declared.
1722 If the input is erroneous, we can have error_mark in the type
1723 slot (e.g. "f(void a, ...)") - that doesn't count as an
1724 incomplete type. */
1725 if (TREE_TYPE (x) != error_mark_node
1726 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
1728 tree element = TREE_TYPE (x);
1730 while (TREE_CODE (element) == ARRAY_TYPE)
1731 element = TREE_TYPE (element);
1732 element = TYPE_MAIN_VARIANT (element);
1734 if ((TREE_CODE (element) == RECORD_TYPE
1735 || TREE_CODE (element) == UNION_TYPE)
1736 && (TREE_CODE (x) != TYPE_DECL
1737 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
1738 && !COMPLETE_TYPE_P (element))
1739 C_TYPE_INCOMPLETE_VARS (element)
1740 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
1744 if (TREE_CODE (x) == PARM_DECL)
1745 SCOPE_LIST_APPEND (scope, parms, x);
1746 else
1747 SCOPE_LIST_APPEND (scope, names, x);
1749 return x;
1752 /* Record X as belonging to the global scope (C99 "file scope").
1753 This is used only internally by the Objective-C front end,
1754 and is limited to its needs. duplicate_decls is not called;
1755 if there is any preexisting decl for this identifier, it is an ICE. */
1757 tree
1758 pushdecl_top_level (tree x)
1760 tree name;
1762 if (TREE_CODE (x) != VAR_DECL)
1763 abort ();
1765 name = DECL_NAME (x);
1767 if (IDENTIFIER_SYMBOL_VALUE (name))
1768 abort ();
1770 DECL_CONTEXT (x) = current_file_decl;
1771 IDENTIFIER_SYMBOL_VALUE (name) = x;
1773 SCOPE_LIST_APPEND (global_scope, names, x);
1774 return x;
1777 /* Generate an implicit declaration for identifier FUNCTIONID as a
1778 function of type int (). */
1780 tree
1781 implicitly_declare (tree functionid)
1783 tree decl = any_external_decl (functionid);
1785 if (decl)
1787 /* Implicit declaration of a function already declared
1788 (somehow) in a different scope, or as a built-in.
1789 If this is the first time this has happened, warn;
1790 then recycle the old declaration. */
1791 if (!C_DECL_IMPLICIT (decl))
1793 implicit_decl_warning (DECL_NAME (decl));
1794 if (! DECL_FILE_SCOPE_P (decl))
1795 warning ("%Jprevious declaration of '%D'", decl, decl);
1796 C_DECL_IMPLICIT (decl) = 1;
1798 /* If this function is global, then it must already be in the
1799 global scope, so there's no need to push it again. */
1800 if (current_scope == global_scope)
1801 return decl;
1802 /* If this is a local declaration, make a copy; we can't have
1803 the same DECL listed in two different scopes. */
1804 return pushdecl (copy_node (decl));
1807 /* Not seen before. */
1808 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
1809 DECL_EXTERNAL (decl) = 1;
1810 TREE_PUBLIC (decl) = 1;
1811 C_DECL_IMPLICIT (decl) = 1;
1812 implicit_decl_warning (functionid);
1814 /* C89 says implicit declarations are in the innermost block.
1815 So we record the decl in the standard fashion. */
1816 decl = pushdecl (decl);
1818 /* No need to call objc_check_decl here - it's a function type. */
1819 rest_of_decl_compilation (decl, NULL, 0, 0);
1821 /* Write a record describing this implicit function declaration
1822 to the prototypes file (if requested). */
1823 gen_aux_info_record (decl, 0, 1, 0);
1825 /* Possibly apply some default attributes to this implicit declaration. */
1826 decl_attributes (&decl, NULL_TREE, 0);
1828 return decl;
1831 static void
1832 implicit_decl_warning (tree id)
1834 const char *name = IDENTIFIER_POINTER (id);
1835 if (mesg_implicit_function_declaration == 2)
1836 error ("implicit declaration of function `%s'", name);
1837 else if (mesg_implicit_function_declaration == 1)
1838 warning ("implicit declaration of function `%s'", name);
1841 /* Return zero if the declaration NEWDECL is valid
1842 when the declaration OLDDECL (assumed to be for the same name)
1843 has already been seen.
1844 Otherwise return 1 if NEWDECL is a redefinition, 2 if it is a redeclaration,
1845 and 3 if it is a conflicting declaration. */
1847 static int
1848 redeclaration_error_message (tree newdecl, tree olddecl)
1850 if (TREE_CODE (newdecl) == TYPE_DECL)
1852 /* Do not complain about type redeclarations where at least one
1853 declaration was in a system header. */
1854 if (DECL_IN_SYSTEM_HEADER (olddecl) || DECL_IN_SYSTEM_HEADER (newdecl))
1855 return 0;
1856 return 1;
1858 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1860 /* Declarations of functions can insist on internal linkage
1861 but they can't be inconsistent with internal linkage,
1862 so there can be no error on that account.
1863 However defining the same name twice is no good. */
1864 if (DECL_INITIAL (olddecl) != 0 && DECL_INITIAL (newdecl) != 0
1865 /* However, defining once as extern inline and a second
1866 time in another way is ok. */
1867 && ! (DECL_DECLARED_INLINE_P (olddecl) && DECL_EXTERNAL (olddecl)
1868 && ! (DECL_DECLARED_INLINE_P (newdecl)
1869 && DECL_EXTERNAL (newdecl))))
1870 return 1;
1871 return 0;
1873 else if (DECL_FILE_SCOPE_P (newdecl))
1875 /* Objects declared at file scope: */
1876 /* If at least one is a reference, it's ok. */
1877 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
1878 return 0;
1879 /* Reject two definitions. */
1880 if (DECL_INITIAL (olddecl) != 0 && DECL_INITIAL (newdecl) != 0)
1881 return 1;
1882 /* Now we have two tentative defs, or one tentative and one real def. */
1883 /* Insist that the linkage match. */
1884 if (TREE_PUBLIC (olddecl) != TREE_PUBLIC (newdecl))
1885 return 3;
1886 return 0;
1888 else if (current_scope->parm_flag
1889 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1890 return 0;
1891 else
1893 /* Newdecl has block scope. If olddecl has block scope also, then
1894 reject two definitions, and reject a definition together with an
1895 external reference. Otherwise, it is OK, because newdecl must
1896 be an extern reference to olddecl. */
1897 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl))
1898 && DECL_CONTEXT (newdecl) == DECL_CONTEXT (olddecl))
1899 return 2;
1900 return 0;
1904 /* Issue an error message for a reference to an undeclared variable
1905 ID, including a reference to a builtin outside of function-call
1906 context. Establish a binding of the identifier to error_mark_node
1907 in an appropriate scope, which will suppress further errors for the
1908 same identifier. */
1909 void
1910 undeclared_variable (tree id)
1912 static bool already = false;
1913 struct c_scope *scope;
1915 if (current_function_decl == 0)
1917 error ("`%s' undeclared here (not in a function)",
1918 IDENTIFIER_POINTER (id));
1919 scope = current_scope;
1921 else
1923 error ("`%s' undeclared (first use in this function)",
1924 IDENTIFIER_POINTER (id));
1926 if (! already)
1928 error ("(Each undeclared identifier is reported only once");
1929 error ("for each function it appears in.)");
1930 already = true;
1933 scope = current_function_scope;
1936 scope->shadowed = tree_cons (id, IDENTIFIER_SYMBOL_VALUE (id),
1937 scope->shadowed);
1938 IDENTIFIER_SYMBOL_VALUE (id) = error_mark_node;
1941 /* Subroutine of lookup_label, declare_label, define_label: construct a
1942 LABEL_DECL with all the proper frills. */
1944 static tree
1945 make_label (tree name, location_t location)
1947 tree label = build_decl (LABEL_DECL, name, void_type_node);
1949 DECL_CONTEXT (label) = current_function_decl;
1950 DECL_MODE (label) = VOIDmode;
1951 DECL_SOURCE_LOCATION (label) = location;
1953 return label;
1956 /* Another subroutine of lookup_label, declare_label, define_label:
1957 set up the binding of name to LABEL_DECL in the given SCOPE. */
1959 static void
1960 bind_label (tree name, tree label, struct c_scope *scope)
1962 if (IDENTIFIER_LABEL_VALUE (name))
1963 scope->shadowed = tree_cons (name, IDENTIFIER_LABEL_VALUE (name),
1964 scope->shadowed);
1965 IDENTIFIER_LABEL_VALUE (name) = label;
1967 SCOPE_LIST_APPEND (scope, names, label);
1970 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
1971 Create one if none exists so far for the current function.
1972 This is called when a label is used in a goto expression or
1973 has its address taken. */
1975 tree
1976 lookup_label (tree name)
1978 tree label;
1980 if (current_function_decl == 0)
1982 error ("label %s referenced outside of any function",
1983 IDENTIFIER_POINTER (name));
1984 return 0;
1987 /* Use a label already defined or ref'd with this name, but not if
1988 it is inherited from a containing function and wasn't declared
1989 using __label__. */
1990 label = IDENTIFIER_LABEL_VALUE (name);
1991 if (label && (DECL_CONTEXT (label) == current_function_decl
1992 || C_DECLARED_LABEL_FLAG (label)))
1994 /* If the label has only been declared, update its apparent
1995 location to point here, for better diagnostics if it
1996 turns out not to have been defined. */
1997 if (!TREE_USED (label))
1998 DECL_SOURCE_LOCATION (label) = input_location;
1999 return label;
2002 /* No label binding for that identifier; make one. */
2003 label = make_label (name, input_location);
2005 /* Ordinary labels go in the current function scope. */
2006 bind_label (name, label, current_function_scope);
2007 return label;
2010 /* Make a label named NAME in the current function, shadowing silently
2011 any that may be inherited from containing functions or containing
2012 scopes. This is called for __label__ declarations. */
2014 /* Note that valid use, if the label being shadowed comes from another
2015 scope in the same function, requires calling declare_nonlocal_label
2016 right away. (Is this still true? -zw 2003-07-17) */
2018 tree
2019 declare_label (tree name)
2021 tree label = IDENTIFIER_LABEL_VALUE (name);
2022 tree dup;
2024 /* Check to make sure that the label hasn't already been declared
2025 at this scope */
2026 for (dup = current_scope->names; dup; dup = TREE_CHAIN (dup))
2027 if (dup == label)
2029 error ("duplicate label declaration `%s'", IDENTIFIER_POINTER (name));
2030 error ("%Jthis is a previous declaration", dup);
2032 /* Just use the previous declaration. */
2033 return dup;
2036 label = make_label (name, input_location);
2037 C_DECLARED_LABEL_FLAG (label) = 1;
2039 /* Declared labels go in the current scope. */
2040 bind_label (name, label, current_scope);
2041 return label;
2044 /* Define a label, specifying the location in the source file.
2045 Return the LABEL_DECL node for the label, if the definition is valid.
2046 Otherwise return 0. */
2048 tree
2049 define_label (location_t location, tree name)
2051 tree label;
2053 /* Find any preexisting label with this name. It is an error
2054 if that label has already been defined in this function, or
2055 if there is a containing function with a declared label with
2056 the same name. */
2057 label = IDENTIFIER_LABEL_VALUE (name);
2059 if (label
2060 && ((DECL_CONTEXT (label) == current_function_decl
2061 && DECL_INITIAL (label) != 0)
2062 || (DECL_CONTEXT (label) != current_function_decl
2063 && C_DECLARED_LABEL_FLAG (label))))
2065 error ("%Hduplicate label `%D'", &location, label);
2066 if (DECL_INITIAL (label))
2067 error ("%J`%D' previously defined here", label, label);
2068 else
2069 error ("%J`%D' previously declared here", label, label);
2070 return 0;
2072 else if (label && DECL_CONTEXT (label) == current_function_decl)
2074 /* The label has been used or declared already in this function,
2075 but not defined. Update its location to point to this
2076 definition. */
2077 DECL_SOURCE_LOCATION (label) = location;
2079 else
2081 /* No label binding for that identifier; make one. */
2082 label = make_label (name, location);
2084 /* Ordinary labels go in the current function scope. */
2085 bind_label (name, label, current_function_scope);
2088 if (warn_traditional && !in_system_header && lookup_name (name))
2089 warning ("%Htraditional C lacks a separate namespace for labels, "
2090 "identifier `%s' conflicts", &location,
2091 IDENTIFIER_POINTER (name));
2093 /* Mark label as having been defined. */
2094 DECL_INITIAL (label) = error_mark_node;
2095 return label;
2098 /* Return the list of declarations of the current scope. */
2100 tree
2101 getdecls (void)
2103 return current_scope->names;
2107 /* Given NAME, an IDENTIFIER_NODE,
2108 return the structure (or union or enum) definition for that name.
2109 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2110 CODE says which kind of type the caller wants;
2111 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2112 If the wrong kind of type is found, an error is reported. */
2114 static tree
2115 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2117 tree tag = IDENTIFIER_TAG_VALUE (name);
2118 int thislevel = 0;
2120 if (!tag)
2121 return 0;
2123 /* We only care about whether it's in this level if
2124 thislevel_only was set or it might be a type clash. */
2125 if (thislevel_only || TREE_CODE (tag) != code)
2127 if (current_scope == global_scope
2128 || purpose_member (name, current_scope->tags))
2129 thislevel = 1;
2132 if (thislevel_only && !thislevel)
2133 return 0;
2135 if (TREE_CODE (tag) != code)
2137 /* Definition isn't the kind we were looking for. */
2138 pending_invalid_xref = name;
2139 pending_invalid_xref_location = input_location;
2141 /* If in the same binding level as a declaration as a tag
2142 of a different type, this must not be allowed to
2143 shadow that tag, so give the error immediately.
2144 (For example, "struct foo; union foo;" is invalid.) */
2145 if (thislevel)
2146 pending_xref_error ();
2148 return tag;
2151 /* Print an error message now
2152 for a recent invalid struct, union or enum cross reference.
2153 We don't print them immediately because they are not invalid
2154 when used in the `struct foo;' construct for shadowing. */
2156 void
2157 pending_xref_error (void)
2159 if (pending_invalid_xref != 0)
2160 error ("%H`%s' defined as wrong kind of tag",
2161 &pending_invalid_xref_location,
2162 IDENTIFIER_POINTER (pending_invalid_xref));
2163 pending_invalid_xref = 0;
2167 /* Look up NAME in the current scope and its superiors
2168 in the namespace of variables, functions and typedefs.
2169 Return a ..._DECL node of some kind representing its definition,
2170 or return 0 if it is undefined. */
2172 tree
2173 lookup_name (tree name)
2175 tree decl = IDENTIFIER_SYMBOL_VALUE (name);
2176 if (decl == 0 || decl == error_mark_node)
2177 return decl;
2178 if (C_DECL_INVISIBLE (decl))
2179 return 0;
2180 return decl;
2183 /* Similar to `lookup_name' but look only at the current scope. */
2185 static tree
2186 lookup_name_current_level (tree name)
2188 tree decl = IDENTIFIER_SYMBOL_VALUE (name);
2190 if (decl == 0 || decl == error_mark_node || C_DECL_INVISIBLE (decl))
2191 return 0;
2193 if (current_scope == global_scope)
2194 return decl;
2196 /* Scan the current scope for a decl with name NAME.
2197 For PARM_DECLs, we have to look at both ->parms and ->names, since
2198 forward parameter declarations wind up on the ->names list. */
2199 if (TREE_CODE (decl) == PARM_DECL
2200 && chain_member (decl, current_scope->parms))
2201 return decl;
2202 if (chain_member (decl, current_scope->names))
2203 return decl;
2205 return 0;
2208 /* Create the predefined scalar types of C,
2209 and some nodes representing standard constants (0, 1, (void *) 0).
2210 Initialize the global scope.
2211 Make definitions for built-in primitive functions. */
2213 void
2214 c_init_decl_processing (void)
2216 tree endlink;
2217 tree ptr_ftype_void, ptr_ftype_ptr;
2218 location_t save_loc = input_location;
2220 /* Adds some ggc roots, and reserved words for c-parse.in. */
2221 c_parse_init ();
2223 current_function_decl = 0;
2225 /* Make the c_scope structure for global names. */
2226 pushlevel (0);
2227 global_scope = current_scope;
2229 /* Declarations from c_common_nodes_and_builtins must not be associated
2230 with this input file, lest we get differences between using and not
2231 using preprocessed headers. */
2232 input_location.file = "<internal>";
2233 input_location.line = 0;
2235 /* Make the DECL for the toplevel file scope. */
2236 current_file_decl = build_decl (TRANSLATION_UNIT_DECL, NULL, NULL);
2238 build_common_tree_nodes (flag_signed_char);
2240 c_common_nodes_and_builtins ();
2242 /* In C, comparisons and TRUTH_* expressions have type int. */
2243 truthvalue_type_node = integer_type_node;
2244 truthvalue_true_node = integer_one_node;
2245 truthvalue_false_node = integer_zero_node;
2247 /* Even in C99, which has a real boolean type. */
2248 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2249 boolean_type_node));
2251 endlink = void_list_node;
2252 ptr_ftype_void = build_function_type (ptr_type_node, endlink);
2253 ptr_ftype_ptr
2254 = build_function_type (ptr_type_node,
2255 tree_cons (NULL_TREE, ptr_type_node, endlink));
2257 input_location = save_loc;
2259 pedantic_lvalues = pedantic;
2261 make_fname_decl = c_make_fname_decl;
2262 start_fname_decls ();
2264 first_builtin_decl = global_scope->names;
2265 last_builtin_decl = global_scope->names_last;
2268 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2269 decl, NAME is the initialization string and TYPE_DEP indicates whether
2270 NAME depended on the type of the function. As we don't yet implement
2271 delayed emission of static data, we mark the decl as emitted
2272 so it is not placed in the output. Anything using it must therefore pull
2273 out the STRING_CST initializer directly. FIXME. */
2275 static tree
2276 c_make_fname_decl (tree id, int type_dep)
2278 const char *name = fname_as_string (type_dep);
2279 tree decl, type, init;
2280 size_t length = strlen (name);
2282 type = build_array_type
2283 (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
2284 build_index_type (size_int (length)));
2286 decl = build_decl (VAR_DECL, id, type);
2288 TREE_STATIC (decl) = 1;
2289 TREE_READONLY (decl) = 1;
2290 DECL_ARTIFICIAL (decl) = 1;
2292 init = build_string (length + 1, name);
2293 TREE_TYPE (init) = type;
2294 DECL_INITIAL (decl) = init;
2296 TREE_USED (decl) = 1;
2298 if (current_function_decl)
2300 DECL_CONTEXT (decl) = current_function_decl;
2301 IDENTIFIER_SYMBOL_VALUE (id) = decl;
2302 SCOPE_LIST_APPEND (current_function_scope, names, decl);
2305 finish_decl (decl, init, NULL_TREE);
2307 return decl;
2310 /* Return a definition for a builtin function named NAME and whose data type
2311 is TYPE. TYPE should be a function type with argument types.
2312 FUNCTION_CODE tells later passes how to compile calls to this function.
2313 See tree.h for its possible values.
2315 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2316 the name to be called if we can't opencode the function. If
2317 ATTRS is nonzero, use that for the function's attribute list. */
2319 tree
2320 builtin_function (const char *name, tree type, int function_code,
2321 enum built_in_class class, const char *library_name,
2322 tree attrs)
2324 tree decl = build_decl (FUNCTION_DECL, get_identifier (name), type);
2325 DECL_EXTERNAL (decl) = 1;
2326 TREE_PUBLIC (decl) = 1;
2327 if (library_name)
2328 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2329 make_decl_rtl (decl, NULL);
2330 pushdecl (decl);
2331 DECL_BUILT_IN_CLASS (decl) = class;
2332 DECL_FUNCTION_CODE (decl) = function_code;
2334 /* Warn if a function in the namespace for users
2335 is used without an occasion to consider it declared. */
2336 if (name[0] != '_' || name[1] != '_')
2337 C_DECL_INVISIBLE (decl) = 1;
2339 /* Possibly apply some default attributes to this built-in function. */
2340 if (attrs)
2341 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2342 else
2343 decl_attributes (&decl, NULL_TREE, 0);
2345 return decl;
2348 /* Called when a declaration is seen that contains no names to declare.
2349 If its type is a reference to a structure, union or enum inherited
2350 from a containing scope, shadow that tag name for the current scope
2351 with a forward reference.
2352 If its type defines a new named structure or union
2353 or defines an enum, it is valid but we need not do anything here.
2354 Otherwise, it is an error. */
2356 void
2357 shadow_tag (tree declspecs)
2359 shadow_tag_warned (declspecs, 0);
2362 void
2363 shadow_tag_warned (tree declspecs, int warned)
2366 /* 1 => we have done a pedwarn. 2 => we have done a warning, but
2367 no pedwarn. */
2369 int found_tag = 0;
2370 tree link;
2371 tree specs, attrs;
2373 pending_invalid_xref = 0;
2375 /* Remove the attributes from declspecs, since they will confuse the
2376 following code. */
2377 split_specs_attrs (declspecs, &specs, &attrs);
2379 for (link = specs; link; link = TREE_CHAIN (link))
2381 tree value = TREE_VALUE (link);
2382 enum tree_code code = TREE_CODE (value);
2384 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2385 /* Used to test also that TYPE_SIZE (value) != 0.
2386 That caused warning for `struct foo;' at top level in the file. */
2388 tree name = TYPE_NAME (value);
2389 tree t;
2391 found_tag++;
2393 if (name == 0)
2395 if (warned != 1 && code != ENUMERAL_TYPE)
2396 /* Empty unnamed enum OK */
2398 pedwarn ("unnamed struct/union that defines no instances");
2399 warned = 1;
2402 else
2404 t = lookup_tag (code, name, 1);
2406 if (t == 0)
2408 t = make_node (code);
2409 pushtag (name, t);
2413 else
2415 if (!warned && ! in_system_header)
2417 warning ("useless keyword or type name in empty declaration");
2418 warned = 2;
2423 if (found_tag > 1)
2424 error ("two types specified in one empty declaration");
2426 if (warned != 1)
2428 if (found_tag == 0)
2429 pedwarn ("empty declaration");
2433 /* Construct an array declarator. EXPR is the expression inside [], or
2434 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
2435 to the pointer to which a parameter array is converted). STATIC_P is
2436 nonzero if "static" is inside the [], zero otherwise. VLA_UNSPEC_P
2437 is nonzero is the array is [*], a VLA of unspecified length which is
2438 nevertheless a complete type (not currently implemented by GCC),
2439 zero otherwise. The declarator is constructed as an ARRAY_REF
2440 (to be decoded by grokdeclarator), whose operand 0 is what's on the
2441 left of the [] (filled by in set_array_declarator_type) and operand 1
2442 is the expression inside; whose TREE_TYPE is the type qualifiers and
2443 which has TREE_STATIC set if "static" is used. */
2445 tree
2446 build_array_declarator (tree expr, tree quals, int static_p, int vla_unspec_p)
2448 tree decl;
2449 decl = build_nt (ARRAY_REF, NULL_TREE, expr);
2450 TREE_TYPE (decl) = quals;
2451 TREE_STATIC (decl) = (static_p ? 1 : 0);
2452 if (pedantic && !flag_isoc99)
2454 if (static_p || quals != NULL_TREE)
2455 pedwarn ("ISO C90 does not support `static' or type qualifiers in parameter array declarators");
2456 if (vla_unspec_p)
2457 pedwarn ("ISO C90 does not support `[*]' array declarators");
2459 if (vla_unspec_p)
2460 warning ("GCC does not yet properly implement `[*]' array declarators");
2461 return decl;
2464 /* Set the type of an array declarator. DECL is the declarator, as
2465 constructed by build_array_declarator; TYPE is what appears on the left
2466 of the [] and goes in operand 0. ABSTRACT_P is nonzero if it is an
2467 abstract declarator, zero otherwise; this is used to reject static and
2468 type qualifiers in abstract declarators, where they are not in the
2469 C99 grammar. */
2471 tree
2472 set_array_declarator_type (tree decl, tree type, int abstract_p)
2474 TREE_OPERAND (decl, 0) = type;
2475 if (abstract_p && (TREE_TYPE (decl) != NULL_TREE || TREE_STATIC (decl)))
2476 error ("static or type qualifiers in abstract declarator");
2477 return decl;
2480 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
2482 tree
2483 groktypename (tree typename)
2485 tree specs, attrs;
2487 if (TREE_CODE (typename) != TREE_LIST)
2488 return typename;
2490 split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
2492 typename = grokdeclarator (TREE_VALUE (typename), specs, TYPENAME, 0);
2494 /* Apply attributes. */
2495 decl_attributes (&typename, attrs, 0);
2497 return typename;
2500 /* Return a PARM_DECL node for a given pair of specs and declarator. */
2502 tree
2503 groktypename_in_parm_context (tree typename)
2505 if (TREE_CODE (typename) != TREE_LIST)
2506 return typename;
2507 return grokdeclarator (TREE_VALUE (typename),
2508 TREE_PURPOSE (typename),
2509 PARM, 0);
2512 /* Decode a declarator in an ordinary declaration or data definition.
2513 This is called as soon as the type information and variable name
2514 have been parsed, before parsing the initializer if any.
2515 Here we create the ..._DECL node, fill in its type,
2516 and put it on the list of decls for the current context.
2517 The ..._DECL node is returned as the value.
2519 Exception: for arrays where the length is not specified,
2520 the type is left null, to be filled in by `finish_decl'.
2522 Function definitions do not come here; they go to start_function
2523 instead. However, external and forward declarations of functions
2524 do go through here. Structure field declarations are done by
2525 grokfield and not through here. */
2527 tree
2528 start_decl (tree declarator, tree declspecs, int initialized, tree attributes)
2530 tree decl;
2531 tree tem;
2533 /* An object declared as __attribute__((deprecated)) suppresses
2534 warnings of uses of other deprecated items. */
2535 if (lookup_attribute ("deprecated", attributes))
2536 deprecated_state = DEPRECATED_SUPPRESS;
2538 decl = grokdeclarator (declarator, declspecs,
2539 NORMAL, initialized);
2541 deprecated_state = DEPRECATED_NORMAL;
2543 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
2544 && MAIN_NAME_P (DECL_NAME (decl)))
2545 warning ("%J'%D' is usually a function", decl, decl);
2547 if (initialized)
2548 /* Is it valid for this decl to have an initializer at all?
2549 If not, set INITIALIZED to zero, which will indirectly
2550 tell `finish_decl' to ignore the initializer once it is parsed. */
2551 switch (TREE_CODE (decl))
2553 case TYPE_DECL:
2554 error ("typedef `%s' is initialized (use __typeof__ instead)",
2555 IDENTIFIER_POINTER (DECL_NAME (decl)));
2556 initialized = 0;
2557 break;
2559 case FUNCTION_DECL:
2560 error ("function `%s' is initialized like a variable",
2561 IDENTIFIER_POINTER (DECL_NAME (decl)));
2562 initialized = 0;
2563 break;
2565 case PARM_DECL:
2566 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
2567 error ("parameter `%s' is initialized",
2568 IDENTIFIER_POINTER (DECL_NAME (decl)));
2569 initialized = 0;
2570 break;
2572 default:
2573 /* Don't allow initializations for incomplete types
2574 except for arrays which might be completed by the initialization. */
2576 /* This can happen if the array size is an undefined macro. We already
2577 gave a warning, so we don't need another one. */
2578 if (TREE_TYPE (decl) == error_mark_node)
2579 initialized = 0;
2580 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
2582 /* A complete type is ok if size is fixed. */
2584 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
2585 || C_DECL_VARIABLE_SIZE (decl))
2587 error ("variable-sized object may not be initialized");
2588 initialized = 0;
2591 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
2593 error ("variable `%s' has initializer but incomplete type",
2594 IDENTIFIER_POINTER (DECL_NAME (decl)));
2595 initialized = 0;
2597 else if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
2599 error ("elements of array `%s' have incomplete type",
2600 IDENTIFIER_POINTER (DECL_NAME (decl)));
2601 initialized = 0;
2605 if (initialized)
2607 DECL_EXTERNAL (decl) = 0;
2608 if (current_scope == global_scope)
2609 TREE_STATIC (decl) = 1;
2611 /* Tell `pushdecl' this is an initialized decl
2612 even though we don't yet have the initializer expression.
2613 Also tell `finish_decl' it may store the real initializer. */
2614 DECL_INITIAL (decl) = error_mark_node;
2617 /* If this is a function declaration, write a record describing it to the
2618 prototypes file (if requested). */
2620 if (TREE_CODE (decl) == FUNCTION_DECL)
2621 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
2623 /* ANSI specifies that a tentative definition which is not merged with
2624 a non-tentative definition behaves exactly like a definition with an
2625 initializer equal to zero. (Section 3.7.2)
2627 -fno-common gives strict ANSI behavior, though this tends to break
2628 a large body of code that grew up without this rule.
2630 Thread-local variables are never common, since there's no entrenched
2631 body of code to break, and it allows more efficient variable references
2632 in the presence of dynamic linking. */
2634 if (TREE_CODE (decl) == VAR_DECL
2635 && !initialized
2636 && TREE_PUBLIC (decl)
2637 && !DECL_THREAD_LOCAL (decl)
2638 && !flag_no_common)
2639 DECL_COMMON (decl) = 1;
2641 /* Set attributes here so if duplicate decl, will have proper attributes. */
2642 decl_attributes (&decl, attributes, 0);
2644 if (TREE_CODE (decl) == FUNCTION_DECL
2645 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
2647 tree ce = declarator;
2649 if (TREE_CODE (ce) == INDIRECT_REF)
2650 ce = TREE_OPERAND (declarator, 0);
2651 if (TREE_CODE (ce) == CALL_EXPR)
2653 tree args = TREE_PURPOSE (TREE_OPERAND (ce, 1));
2654 for (; args; args = TREE_CHAIN (args))
2656 tree type = TREE_TYPE (args);
2657 if (INTEGRAL_TYPE_P (type)
2658 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
2659 DECL_ARG_TYPE (args) = integer_type_node;
2664 if (TREE_CODE (decl) == FUNCTION_DECL
2665 && DECL_DECLARED_INLINE_P (decl)
2666 && DECL_UNINLINABLE (decl)
2667 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
2668 warning ("%Jinline function '%D' given attribute noinline", decl, decl);
2670 /* Add this decl to the current scope.
2671 TEM may equal DECL or it may be a previous decl of the same name. */
2672 tem = pushdecl (decl);
2674 /* For a local variable, define the RTL now. */
2675 if (current_scope != global_scope
2676 /* But not if this is a duplicate decl
2677 and we preserved the rtl from the previous one
2678 (which may or may not happen). */
2679 && !DECL_RTL_SET_P (tem)
2680 && DECL_FILE_SCOPE_P (tem))
2682 if (TREE_TYPE (tem) != error_mark_node
2683 && (COMPLETE_TYPE_P (TREE_TYPE (tem))
2684 || (TREE_CODE (TREE_TYPE (tem)) == ARRAY_TYPE
2685 && DECL_INITIAL (tem) != 0)))
2686 expand_decl (tem);
2689 return tem;
2692 /* Finish processing of a declaration;
2693 install its initial value.
2694 If the length of an array type is not known before,
2695 it must be determined now, from the initial value, or it is an error. */
2697 void
2698 finish_decl (tree decl, tree init, tree asmspec_tree)
2700 tree type = TREE_TYPE (decl);
2701 int was_incomplete = (DECL_SIZE (decl) == 0);
2702 const char *asmspec = 0;
2704 /* If a name was specified, get the string. */
2705 if (current_scope == global_scope)
2706 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
2707 if (asmspec_tree)
2708 asmspec = TREE_STRING_POINTER (asmspec_tree);
2710 /* If `start_decl' didn't like having an initialization, ignore it now. */
2711 if (init != 0 && DECL_INITIAL (decl) == 0)
2712 init = 0;
2714 /* Don't crash if parm is initialized. */
2715 if (TREE_CODE (decl) == PARM_DECL)
2716 init = 0;
2718 if (init)
2719 store_init_value (decl, init);
2721 /* Deduce size of array from initialization, if not already known */
2722 if (TREE_CODE (type) == ARRAY_TYPE
2723 && TYPE_DOMAIN (type) == 0
2724 && TREE_CODE (decl) != TYPE_DECL)
2726 int do_default
2727 = (TREE_STATIC (decl)
2728 /* Even if pedantic, an external linkage array
2729 may have incomplete type at first. */
2730 ? pedantic && !TREE_PUBLIC (decl)
2731 : !DECL_EXTERNAL (decl));
2732 int failure
2733 = complete_array_type (type, DECL_INITIAL (decl), do_default);
2735 /* Get the completed type made by complete_array_type. */
2736 type = TREE_TYPE (decl);
2738 if (failure == 1)
2739 error ("%Jinitializer fails to determine size of '%D'", decl, decl);
2741 else if (failure == 2)
2743 if (do_default)
2744 error ("%Jarray size missing in '%D'", decl, decl);
2745 /* If a `static' var's size isn't known,
2746 make it extern as well as static, so it does not get
2747 allocated.
2748 If it is not `static', then do not mark extern;
2749 finish_incomplete_decl will give it a default size
2750 and it will get allocated. */
2751 else if (!pedantic && TREE_STATIC (decl) && ! TREE_PUBLIC (decl))
2752 DECL_EXTERNAL (decl) = 1;
2755 /* TYPE_MAX_VALUE is always one less than the number of elements
2756 in the array, because we start counting at zero. Therefore,
2757 warn only if the value is less than zero. */
2758 else if (pedantic && TYPE_DOMAIN (type) != 0
2759 && tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < 0)
2760 error ("%Jzero or negative size array '%D'", decl, decl);
2762 layout_decl (decl, 0);
2765 if (TREE_CODE (decl) == VAR_DECL)
2767 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
2768 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
2769 layout_decl (decl, 0);
2771 if (DECL_SIZE (decl) == 0
2772 /* Don't give an error if we already gave one earlier. */
2773 && TREE_TYPE (decl) != error_mark_node
2774 && (TREE_STATIC (decl)
2776 /* A static variable with an incomplete type
2777 is an error if it is initialized.
2778 Also if it is not file scope.
2779 Otherwise, let it through, but if it is not `extern'
2780 then it may cause an error message later. */
2781 (DECL_INITIAL (decl) != 0
2782 || !DECL_FILE_SCOPE_P (decl))
2784 /* An automatic variable with an incomplete type
2785 is an error. */
2786 !DECL_EXTERNAL (decl)))
2788 error ("%Jstorage size of '%D' isn't known", decl, decl);
2789 TREE_TYPE (decl) = error_mark_node;
2792 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
2793 && DECL_SIZE (decl) != 0)
2795 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
2796 constant_expression_warning (DECL_SIZE (decl));
2797 else
2798 error ("%Jstorage size of '%D' isn't constant", decl, decl);
2801 if (TREE_USED (type))
2802 TREE_USED (decl) = 1;
2805 /* If this is a function and an assembler name is specified, reset DECL_RTL
2806 so we can give it its new name. Also, update built_in_decls if it
2807 was a normal built-in. */
2808 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
2810 /* ASMSPEC is given, and not the name of a register. Mark the
2811 name with a star so assemble_name won't munge it. */
2812 char *starred = alloca (strlen (asmspec) + 2);
2813 starred[0] = '*';
2814 strcpy (starred + 1, asmspec);
2816 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
2818 tree builtin = built_in_decls [DECL_FUNCTION_CODE (decl)];
2819 SET_DECL_RTL (builtin, NULL_RTX);
2820 SET_DECL_ASSEMBLER_NAME (builtin, get_identifier (starred));
2821 #ifdef TARGET_MEM_FUNCTIONS
2822 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMCPY)
2823 init_block_move_fn (starred);
2824 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMSET)
2825 init_block_clear_fn (starred);
2826 #else
2827 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BCOPY)
2828 init_block_move_fn (starred);
2829 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BZERO)
2830 init_block_clear_fn (starred);
2831 #endif
2833 SET_DECL_RTL (decl, NULL_RTX);
2834 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (starred));
2837 /* If #pragma weak was used, mark the decl weak now. */
2838 if (current_scope == global_scope)
2839 maybe_apply_pragma_weak (decl);
2841 /* Output the assembler code and/or RTL code for variables and functions,
2842 unless the type is an undefined structure or union.
2843 If not, it will get done when the type is completed. */
2845 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
2847 /* This is a no-op in c-lang.c or something real in objc-act.c. */
2848 if (c_dialect_objc ())
2849 objc_check_decl (decl);
2851 if (DECL_FILE_SCOPE_P (decl))
2853 if (DECL_INITIAL (decl) == NULL_TREE
2854 || DECL_INITIAL (decl) == error_mark_node)
2855 /* Don't output anything
2856 when a tentative file-scope definition is seen.
2857 But at end of compilation, do output code for them. */
2858 DECL_DEFER_OUTPUT (decl) = 1;
2859 rest_of_decl_compilation (decl, asmspec, true, 0);
2861 else
2863 /* This is a local variable. If there is an ASMSPEC, the
2864 user has requested that we handle it specially. */
2865 if (asmspec)
2867 /* In conjunction with an ASMSPEC, the `register'
2868 keyword indicates that we should place the variable
2869 in a particular register. */
2870 if (DECL_REGISTER (decl))
2871 DECL_C_HARD_REGISTER (decl) = 1;
2873 /* If this is not a static variable, issue a warning.
2874 It doesn't make any sense to give an ASMSPEC for an
2875 ordinary, non-register local variable. Historically,
2876 GCC has accepted -- but ignored -- the ASMSPEC in
2877 this case. */
2878 if (TREE_CODE (decl) == VAR_DECL
2879 && !DECL_REGISTER (decl)
2880 && !TREE_STATIC (decl))
2881 warning ("%Jignoring asm-specifier for non-static local "
2882 "variable '%D'", decl, decl);
2883 else
2884 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
2887 if (TREE_CODE (decl) != FUNCTION_DECL)
2888 add_decl_stmt (decl);
2891 if (!DECL_FILE_SCOPE_P (decl))
2893 /* Recompute the RTL of a local array now
2894 if it used to be an incomplete type. */
2895 if (was_incomplete
2896 && ! TREE_STATIC (decl) && ! DECL_EXTERNAL (decl))
2898 /* If we used it already as memory, it must stay in memory. */
2899 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
2900 /* If it's still incomplete now, no init will save it. */
2901 if (DECL_SIZE (decl) == 0)
2902 DECL_INITIAL (decl) = 0;
2907 /* If this was marked 'used', be sure it will be output. */
2908 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
2909 mark_referenced (DECL_ASSEMBLER_NAME (decl));
2911 if (TREE_CODE (decl) == TYPE_DECL)
2913 /* This is a no-op in c-lang.c or something real in objc-act.c. */
2914 if (c_dialect_objc ())
2915 objc_check_decl (decl);
2916 rest_of_decl_compilation (decl, NULL, DECL_FILE_SCOPE_P (decl), 0);
2919 /* At the end of a declaration, throw away any variable type sizes
2920 of types defined inside that declaration. There is no use
2921 computing them in the following function definition. */
2922 if (current_scope == global_scope)
2923 get_pending_sizes ();
2925 /* Install a cleanup (aka destructor) if one was given. */
2926 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
2928 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
2929 if (attr)
2931 static bool eh_initialized_p;
2933 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
2934 tree cleanup_decl = lookup_name (cleanup_id);
2935 tree cleanup;
2937 /* Build "cleanup(&decl)" for the destructor. */
2938 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
2939 cleanup = build_tree_list (NULL_TREE, cleanup);
2940 cleanup = build_function_call (cleanup_decl, cleanup);
2942 /* Don't warn about decl unused; the cleanup uses it. */
2943 TREE_USED (decl) = 1;
2945 /* Initialize EH, if we've been told to do so. */
2946 if (flag_exceptions && !eh_initialized_p)
2948 eh_initialized_p = true;
2949 eh_personality_libfunc
2950 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
2951 ? "__gcc_personality_sj0"
2952 : "__gcc_personality_v0");
2953 using_eh_for_cleanups ();
2956 add_stmt (build_stmt (CLEANUP_STMT, decl, cleanup));
2961 /* Given a parsed parameter declaration, decode it into a PARM_DECL
2962 and push that on the current scope. */
2964 void
2965 push_parm_decl (tree parm)
2967 tree decl;
2969 /* Don't attempt to expand sizes while parsing this decl.
2970 (We can get here with i_s_e 1 somehow from Objective-C.) */
2971 int save_immediate_size_expand = immediate_size_expand;
2972 immediate_size_expand = 0;
2974 decl = grokdeclarator (TREE_VALUE (TREE_PURPOSE (parm)),
2975 TREE_PURPOSE (TREE_PURPOSE (parm)), PARM, 0);
2976 decl_attributes (&decl, TREE_VALUE (parm), 0);
2978 decl = pushdecl (decl);
2980 finish_decl (decl, NULL_TREE, NULL_TREE);
2982 immediate_size_expand = save_immediate_size_expand;
2985 /* Mark all the parameter declarations to date as forward decls,
2986 shift them to the variables list, and reset the parameters list.
2987 Also diagnose use of this extension. */
2989 void
2990 mark_forward_parm_decls (void)
2992 tree parm;
2994 if (pedantic && !current_scope->warned_forward_parm_decls)
2996 pedwarn ("ISO C forbids forward parameter declarations");
2997 current_scope->warned_forward_parm_decls = true;
3000 for (parm = current_scope->parms; parm; parm = TREE_CHAIN (parm))
3001 TREE_ASM_WRITTEN (parm) = 1;
3003 SCOPE_LIST_CONCAT (current_scope, names, current_scope, parms);
3004 current_scope->parms = 0;
3005 current_scope->parms_last = 0;
3008 static GTY(()) int compound_literal_number;
3010 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3011 literal, which may be an incomplete array type completed by the
3012 initializer; INIT is a CONSTRUCTOR that initializes the compound
3013 literal. */
3015 tree
3016 build_compound_literal (tree type, tree init)
3018 /* We do not use start_decl here because we have a type, not a declarator;
3019 and do not use finish_decl because the decl should be stored inside
3020 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_STMT. */
3021 tree decl = build_decl (VAR_DECL, NULL_TREE, type);
3022 tree complit;
3023 tree stmt;
3024 DECL_EXTERNAL (decl) = 0;
3025 TREE_PUBLIC (decl) = 0;
3026 TREE_STATIC (decl) = (current_scope == global_scope);
3027 DECL_CONTEXT (decl) = current_function_decl;
3028 TREE_USED (decl) = 1;
3029 TREE_TYPE (decl) = type;
3030 TREE_READONLY (decl) = TREE_READONLY (type);
3031 store_init_value (decl, init);
3033 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3035 int failure = complete_array_type (type, DECL_INITIAL (decl), 1);
3036 if (failure)
3037 abort ();
3040 type = TREE_TYPE (decl);
3041 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3042 return error_mark_node;
3044 stmt = build_stmt (DECL_STMT, decl);
3045 complit = build1 (COMPOUND_LITERAL_EXPR, TREE_TYPE (decl), stmt);
3046 TREE_SIDE_EFFECTS (complit) = 1;
3048 layout_decl (decl, 0);
3050 if (TREE_STATIC (decl))
3052 /* This decl needs a name for the assembler output. We also need
3053 a unique suffix to be added to the name. */
3054 char *name;
3056 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
3057 compound_literal_number);
3058 compound_literal_number++;
3059 DECL_NAME (decl) = get_identifier (name);
3060 DECL_DEFER_OUTPUT (decl) = 1;
3061 DECL_COMDAT (decl) = 1;
3062 DECL_ARTIFICIAL (decl) = 1;
3063 pushdecl (decl);
3064 rest_of_decl_compilation (decl, NULL, 1, 0);
3067 return complit;
3070 /* Make TYPE a complete type based on INITIAL_VALUE.
3071 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
3072 2 if there was no information (in which case assume 1 if DO_DEFAULT). */
3075 complete_array_type (tree type, tree initial_value, int do_default)
3077 tree maxindex = NULL_TREE;
3078 int value = 0;
3080 if (initial_value)
3082 /* Note MAXINDEX is really the maximum index,
3083 one less than the size. */
3084 if (TREE_CODE (initial_value) == STRING_CST)
3086 int eltsize
3087 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
3088 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
3089 / eltsize) - 1, 0);
3091 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
3093 tree elts = CONSTRUCTOR_ELTS (initial_value);
3094 maxindex = build_int_2 (-1, -1);
3095 for (; elts; elts = TREE_CHAIN (elts))
3097 if (TREE_PURPOSE (elts))
3098 maxindex = TREE_PURPOSE (elts);
3099 else
3100 maxindex = fold (build (PLUS_EXPR, integer_type_node,
3101 maxindex, integer_one_node));
3103 maxindex = copy_node (maxindex);
3105 else
3107 /* Make an error message unless that happened already. */
3108 if (initial_value != error_mark_node)
3109 value = 1;
3111 /* Prevent further error messages. */
3112 maxindex = build_int_2 (0, 0);
3116 if (!maxindex)
3118 if (do_default)
3119 maxindex = build_int_2 (0, 0);
3120 value = 2;
3123 if (maxindex)
3125 TYPE_DOMAIN (type) = build_index_type (maxindex);
3126 if (!TREE_TYPE (maxindex))
3127 TREE_TYPE (maxindex) = TYPE_DOMAIN (type);
3130 /* Lay out the type now that we can get the real answer. */
3132 layout_type (type);
3134 return value;
3137 /* Determine whether TYPE is a structure with a flexible array member,
3138 or a union containing such a structure (possibly recursively). */
3140 static bool
3141 flexible_array_type_p (tree type)
3143 tree x;
3144 switch (TREE_CODE (type))
3146 case RECORD_TYPE:
3147 x = TYPE_FIELDS (type);
3148 if (x == NULL_TREE)
3149 return false;
3150 while (TREE_CHAIN (x) != NULL_TREE)
3151 x = TREE_CHAIN (x);
3152 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3153 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3154 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3155 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3156 return true;
3157 return false;
3158 case UNION_TYPE:
3159 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3161 if (flexible_array_type_p (TREE_TYPE (x)))
3162 return true;
3164 return false;
3165 default:
3166 return false;
3170 /* Given declspecs and a declarator,
3171 determine the name and type of the object declared
3172 and construct a ..._DECL node for it.
3173 (In one case we can return a ..._TYPE node instead.
3174 For invalid input we sometimes return 0.)
3176 DECLSPECS is a chain of tree_list nodes whose value fields
3177 are the storage classes and type specifiers.
3179 DECL_CONTEXT says which syntactic context this declaration is in:
3180 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3181 FUNCDEF for a function definition. Like NORMAL but a few different
3182 error messages in each case. Return value may be zero meaning
3183 this definition is too screwy to try to parse.
3184 PARM for a parameter declaration (either within a function prototype
3185 or before a function body). Make a PARM_DECL, or return void_type_node.
3186 TYPENAME if for a typename (in a cast or sizeof).
3187 Don't make a DECL node; just return the ..._TYPE node.
3188 FIELD for a struct or union field; make a FIELD_DECL.
3189 BITFIELD for a field with specified width.
3190 INITIALIZED is 1 if the decl has an initializer.
3192 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3193 It may also be so in the PARM case, for a prototype where the
3194 argument type is specified but not the name.
3196 This function is where the complicated C meanings of `static'
3197 and `extern' are interpreted. */
3199 static tree
3200 grokdeclarator (tree declarator, tree declspecs,
3201 enum decl_context decl_context, int initialized)
3203 int specbits = 0;
3204 tree spec;
3205 tree type = NULL_TREE;
3206 int longlong = 0;
3207 int constp;
3208 int restrictp;
3209 int volatilep;
3210 int type_quals = TYPE_UNQUALIFIED;
3211 int inlinep;
3212 int explicit_int = 0;
3213 int explicit_char = 0;
3214 int defaulted_int = 0;
3215 tree typedef_decl = 0;
3216 const char *name;
3217 tree typedef_type = 0;
3218 int funcdef_flag = 0;
3219 enum tree_code innermost_code = ERROR_MARK;
3220 int bitfield = 0;
3221 int size_varies = 0;
3222 tree decl_attr = NULL_TREE;
3223 tree array_ptr_quals = NULL_TREE;
3224 int array_parm_static = 0;
3225 tree returned_attrs = NULL_TREE;
3227 if (decl_context == BITFIELD)
3228 bitfield = 1, decl_context = FIELD;
3230 if (decl_context == FUNCDEF)
3231 funcdef_flag = 1, decl_context = NORMAL;
3233 /* Look inside a declarator for the name being declared
3234 and get it as a string, for an error message. */
3236 tree decl = declarator;
3237 name = 0;
3239 while (decl)
3240 switch (TREE_CODE (decl))
3242 case ARRAY_REF:
3243 case INDIRECT_REF:
3244 case CALL_EXPR:
3245 innermost_code = TREE_CODE (decl);
3246 decl = TREE_OPERAND (decl, 0);
3247 break;
3249 case TREE_LIST:
3250 decl = TREE_VALUE (decl);
3251 break;
3253 case IDENTIFIER_NODE:
3254 name = IDENTIFIER_POINTER (decl);
3255 decl = 0;
3256 break;
3258 default:
3259 abort ();
3261 if (name == 0)
3262 name = "type name";
3265 /* A function definition's declarator must have the form of
3266 a function declarator. */
3268 if (funcdef_flag && innermost_code != CALL_EXPR)
3269 return 0;
3271 /* If this looks like a function definition, make it one,
3272 even if it occurs where parms are expected.
3273 Then store_parm_decls will reject it and not use it as a parm. */
3274 if (decl_context == NORMAL && !funcdef_flag
3275 && current_scope->parm_flag)
3276 decl_context = PARM;
3278 /* Look through the decl specs and record which ones appear.
3279 Some typespecs are defined as built-in typenames.
3280 Others, the ones that are modifiers of other types,
3281 are represented by bits in SPECBITS: set the bits for
3282 the modifiers that appear. Storage class keywords are also in SPECBITS.
3284 If there is a typedef name or a type, store the type in TYPE.
3285 This includes builtin typedefs such as `int'.
3287 Set EXPLICIT_INT or EXPLICIT_CHAR if the type is `int' or `char'
3288 and did not come from a user typedef.
3290 Set LONGLONG if `long' is mentioned twice. */
3292 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
3294 tree id = TREE_VALUE (spec);
3296 /* If the entire declaration is itself tagged as deprecated then
3297 suppress reports of deprecated items. */
3298 if (id && TREE_DEPRECATED (id))
3300 if (deprecated_state != DEPRECATED_SUPPRESS)
3301 warn_deprecated_use (id);
3304 if (id == ridpointers[(int) RID_INT])
3305 explicit_int = 1;
3306 if (id == ridpointers[(int) RID_CHAR])
3307 explicit_char = 1;
3309 if (TREE_CODE (id) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (id))
3311 enum rid i = C_RID_CODE (id);
3312 if ((int) i <= (int) RID_LAST_MODIFIER)
3314 if (i == RID_LONG && (specbits & (1 << (int) RID_LONG)))
3316 if (longlong)
3317 error ("`long long long' is too long for GCC");
3318 else
3320 if (pedantic && !flag_isoc99 && ! in_system_header
3321 && warn_long_long)
3322 pedwarn ("ISO C90 does not support `long long'");
3323 longlong = 1;
3326 else if (specbits & (1 << (int) i))
3328 if (i == RID_CONST || i == RID_VOLATILE || i == RID_RESTRICT)
3330 if (!flag_isoc99)
3331 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
3333 else
3334 error ("duplicate `%s'", IDENTIFIER_POINTER (id));
3337 /* Diagnose "__thread extern". Recall that this list
3338 is in the reverse order seen in the text. */
3339 if (i == RID_THREAD
3340 && (specbits & (1 << (int) RID_EXTERN
3341 | 1 << (int) RID_STATIC)))
3343 if (specbits & 1 << (int) RID_EXTERN)
3344 error ("`__thread' before `extern'");
3345 else
3346 error ("`__thread' before `static'");
3349 specbits |= 1 << (int) i;
3350 goto found;
3353 if (type)
3354 error ("two or more data types in declaration of `%s'", name);
3355 /* Actual typedefs come to us as TYPE_DECL nodes. */
3356 else if (TREE_CODE (id) == TYPE_DECL)
3358 if (TREE_TYPE (id) == error_mark_node)
3359 ; /* Allow the type to default to int to avoid cascading errors. */
3360 else
3362 type = TREE_TYPE (id);
3363 decl_attr = DECL_ATTRIBUTES (id);
3364 typedef_decl = id;
3367 /* Built-in types come as identifiers. */
3368 else if (TREE_CODE (id) == IDENTIFIER_NODE)
3370 tree t = lookup_name (id);
3371 if (TREE_TYPE (t) == error_mark_node)
3373 else if (!t || TREE_CODE (t) != TYPE_DECL)
3374 error ("`%s' fails to be a typedef or built in type",
3375 IDENTIFIER_POINTER (id));
3376 else
3378 type = TREE_TYPE (t);
3379 typedef_decl = t;
3382 else if (TREE_CODE (id) != ERROR_MARK)
3383 type = id;
3385 found:
3389 typedef_type = type;
3390 if (type)
3391 size_varies = C_TYPE_VARIABLE_SIZE (type);
3393 /* No type at all: default to `int', and set DEFAULTED_INT
3394 because it was not a user-defined typedef. */
3396 if (type == 0)
3398 if ((! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3399 | (1 << (int) RID_SIGNED)
3400 | (1 << (int) RID_UNSIGNED)
3401 | (1 << (int) RID_COMPLEX))))
3402 /* Don't warn about typedef foo = bar. */
3403 && ! (specbits & (1 << (int) RID_TYPEDEF) && initialized)
3404 && ! in_system_header)
3406 /* Issue a warning if this is an ISO C 99 program or if -Wreturn-type
3407 and this is a function, or if -Wimplicit; prefer the former
3408 warning since it is more explicit. */
3409 if ((warn_implicit_int || warn_return_type || flag_isoc99)
3410 && funcdef_flag)
3411 warn_about_return_type = 1;
3412 else if (warn_implicit_int || flag_isoc99)
3413 pedwarn_c99 ("type defaults to `int' in declaration of `%s'",
3414 name);
3417 defaulted_int = 1;
3418 type = integer_type_node;
3421 /* Now process the modifiers that were specified
3422 and check for invalid combinations. */
3424 /* Long double is a special combination. */
3426 if ((specbits & 1 << (int) RID_LONG) && ! longlong
3427 && TYPE_MAIN_VARIANT (type) == double_type_node)
3429 specbits &= ~(1 << (int) RID_LONG);
3430 type = long_double_type_node;
3433 /* Check all other uses of type modifiers. */
3435 if (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3436 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED)))
3438 int ok = 0;
3440 if ((specbits & 1 << (int) RID_LONG)
3441 && (specbits & 1 << (int) RID_SHORT))
3442 error ("both long and short specified for `%s'", name);
3443 else if (((specbits & 1 << (int) RID_LONG)
3444 || (specbits & 1 << (int) RID_SHORT))
3445 && explicit_char)
3446 error ("long or short specified with char for `%s'", name);
3447 else if (((specbits & 1 << (int) RID_LONG)
3448 || (specbits & 1 << (int) RID_SHORT))
3449 && TREE_CODE (type) == REAL_TYPE)
3451 static int already = 0;
3453 error ("long or short specified with floating type for `%s'", name);
3454 if (! already && ! pedantic)
3456 error ("the only valid combination is `long double'");
3457 already = 1;
3460 else if ((specbits & 1 << (int) RID_SIGNED)
3461 && (specbits & 1 << (int) RID_UNSIGNED))
3462 error ("both signed and unsigned specified for `%s'", name);
3463 else if (TREE_CODE (type) != INTEGER_TYPE)
3464 error ("long, short, signed or unsigned invalid for `%s'", name);
3465 else
3467 ok = 1;
3468 if (!explicit_int && !defaulted_int && !explicit_char)
3470 error ("long, short, signed or unsigned used invalidly for `%s'",
3471 name);
3472 ok = 0;
3476 /* Discard the type modifiers if they are invalid. */
3477 if (! ok)
3479 specbits &= ~((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3480 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED));
3481 longlong = 0;
3485 if ((specbits & (1 << (int) RID_COMPLEX))
3486 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
3488 error ("complex invalid for `%s'", name);
3489 specbits &= ~(1 << (int) RID_COMPLEX);
3492 /* Decide whether an integer type is signed or not.
3493 Optionally treat bitfields as signed by default. */
3494 if (specbits & 1 << (int) RID_UNSIGNED
3495 || (bitfield && ! flag_signed_bitfields
3496 && (explicit_int || defaulted_int || explicit_char
3497 /* A typedef for plain `int' without `signed'
3498 can be controlled just like plain `int'. */
3499 || ! (typedef_decl != 0
3500 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
3501 && TREE_CODE (type) != ENUMERAL_TYPE
3502 && !(specbits & 1 << (int) RID_SIGNED)))
3504 if (longlong)
3505 type = long_long_unsigned_type_node;
3506 else if (specbits & 1 << (int) RID_LONG)
3507 type = long_unsigned_type_node;
3508 else if (specbits & 1 << (int) RID_SHORT)
3509 type = short_unsigned_type_node;
3510 else if (type == char_type_node)
3511 type = unsigned_char_type_node;
3512 else if (typedef_decl)
3513 type = c_common_unsigned_type (type);
3514 else
3515 type = unsigned_type_node;
3517 else if ((specbits & 1 << (int) RID_SIGNED)
3518 && type == char_type_node)
3519 type = signed_char_type_node;
3520 else if (longlong)
3521 type = long_long_integer_type_node;
3522 else if (specbits & 1 << (int) RID_LONG)
3523 type = long_integer_type_node;
3524 else if (specbits & 1 << (int) RID_SHORT)
3525 type = short_integer_type_node;
3527 if (specbits & 1 << (int) RID_COMPLEX)
3529 if (pedantic && !flag_isoc99)
3530 pedwarn ("ISO C90 does not support complex types");
3531 /* If we just have "complex", it is equivalent to
3532 "complex double", but if any modifiers at all are specified it is
3533 the complex form of TYPE. E.g, "complex short" is
3534 "complex short int". */
3536 if (defaulted_int && ! longlong
3537 && ! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3538 | (1 << (int) RID_SIGNED)
3539 | (1 << (int) RID_UNSIGNED))))
3541 if (pedantic)
3542 pedwarn ("ISO C does not support plain `complex' meaning `double complex'");
3543 type = complex_double_type_node;
3545 else if (type == integer_type_node)
3547 if (pedantic)
3548 pedwarn ("ISO C does not support complex integer types");
3549 type = complex_integer_type_node;
3551 else if (type == float_type_node)
3552 type = complex_float_type_node;
3553 else if (type == double_type_node)
3554 type = complex_double_type_node;
3555 else if (type == long_double_type_node)
3556 type = complex_long_double_type_node;
3557 else
3559 if (pedantic)
3560 pedwarn ("ISO C does not support complex integer types");
3561 type = build_complex_type (type);
3565 /* Figure out the type qualifiers for the declaration. There are
3566 two ways a declaration can become qualified. One is something
3567 like `const int i' where the `const' is explicit. Another is
3568 something like `typedef const int CI; CI i' where the type of the
3569 declaration contains the `const'. */
3570 constp = !! (specbits & 1 << (int) RID_CONST) + TYPE_READONLY (type);
3571 restrictp = !! (specbits & 1 << (int) RID_RESTRICT) + TYPE_RESTRICT (type);
3572 volatilep = !! (specbits & 1 << (int) RID_VOLATILE) + TYPE_VOLATILE (type);
3573 inlinep = !! (specbits & (1 << (int) RID_INLINE));
3574 if (constp > 1 && ! flag_isoc99)
3575 pedwarn ("duplicate `const'");
3576 if (restrictp > 1 && ! flag_isoc99)
3577 pedwarn ("duplicate `restrict'");
3578 if (volatilep > 1 && ! flag_isoc99)
3579 pedwarn ("duplicate `volatile'");
3580 if (! flag_gen_aux_info && (TYPE_QUALS (type)))
3581 type = TYPE_MAIN_VARIANT (type);
3582 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
3583 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
3584 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
3586 /* Warn if two storage classes are given. Default to `auto'. */
3589 int nclasses = 0;
3591 if (specbits & 1 << (int) RID_AUTO) nclasses++;
3592 if (specbits & 1 << (int) RID_STATIC) nclasses++;
3593 if (specbits & 1 << (int) RID_EXTERN) nclasses++;
3594 if (specbits & 1 << (int) RID_REGISTER) nclasses++;
3595 if (specbits & 1 << (int) RID_TYPEDEF) nclasses++;
3597 /* "static __thread" and "extern __thread" are allowed. */
3598 if ((specbits & (1 << (int) RID_THREAD
3599 | 1 << (int) RID_STATIC
3600 | 1 << (int) RID_EXTERN)) == (1 << (int) RID_THREAD))
3601 nclasses++;
3603 /* Warn about storage classes that are invalid for certain
3604 kinds of declarations (parameters, typenames, etc.). */
3606 if (nclasses > 1)
3607 error ("multiple storage classes in declaration of `%s'", name);
3608 else if (funcdef_flag
3609 && (specbits
3610 & ((1 << (int) RID_REGISTER)
3611 | (1 << (int) RID_AUTO)
3612 | (1 << (int) RID_TYPEDEF)
3613 | (1 << (int) RID_THREAD))))
3615 if (specbits & 1 << (int) RID_AUTO
3616 && (pedantic || current_scope == global_scope))
3617 pedwarn ("function definition declared `auto'");
3618 if (specbits & 1 << (int) RID_REGISTER)
3619 error ("function definition declared `register'");
3620 if (specbits & 1 << (int) RID_TYPEDEF)
3621 error ("function definition declared `typedef'");
3622 if (specbits & 1 << (int) RID_THREAD)
3623 error ("function definition declared `__thread'");
3624 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3625 | (1 << (int) RID_AUTO) | (1 << (int) RID_THREAD));
3627 else if (decl_context != NORMAL && nclasses > 0)
3629 if (decl_context == PARM && specbits & 1 << (int) RID_REGISTER)
3631 else
3633 switch (decl_context)
3635 case FIELD:
3636 error ("storage class specified for structure field `%s'",
3637 name);
3638 break;
3639 case PARM:
3640 error ("storage class specified for parameter `%s'", name);
3641 break;
3642 default:
3643 error ("storage class specified for typename");
3644 break;
3646 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3647 | (1 << (int) RID_AUTO) | (1 << (int) RID_STATIC)
3648 | (1 << (int) RID_EXTERN) | (1 << (int) RID_THREAD));
3651 else if (specbits & 1 << (int) RID_EXTERN && initialized && ! funcdef_flag)
3653 /* `extern' with initialization is invalid if not at file scope. */
3654 if (current_scope == global_scope)
3655 warning ("`%s' initialized and declared `extern'", name);
3656 else
3657 error ("`%s' has both `extern' and initializer", name);
3659 else if (current_scope == global_scope)
3661 if (specbits & 1 << (int) RID_AUTO)
3662 error ("file-scope declaration of `%s' specifies `auto'", name);
3664 else
3666 if (specbits & 1 << (int) RID_EXTERN && funcdef_flag)
3667 error ("nested function `%s' declared `extern'", name);
3668 else if ((specbits & (1 << (int) RID_THREAD
3669 | 1 << (int) RID_EXTERN
3670 | 1 << (int) RID_STATIC))
3671 == (1 << (int) RID_THREAD))
3673 error ("function-scope `%s' implicitly auto and declared `__thread'",
3674 name);
3675 specbits &= ~(1 << (int) RID_THREAD);
3680 /* Now figure out the structure of the declarator proper.
3681 Descend through it, creating more complex types, until we reach
3682 the declared identifier (or NULL_TREE, in an absolute declarator). */
3684 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE)
3686 if (type == error_mark_node)
3688 declarator = TREE_OPERAND (declarator, 0);
3689 continue;
3692 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
3693 an INDIRECT_REF (for *...),
3694 a CALL_EXPR (for ...(...)),
3695 a TREE_LIST (for nested attributes),
3696 an identifier (for the name being declared)
3697 or a null pointer (for the place in an absolute declarator
3698 where the name was omitted).
3699 For the last two cases, we have just exited the loop.
3701 At this point, TYPE is the type of elements of an array,
3702 or for a function to return, or for a pointer to point to.
3703 After this sequence of ifs, TYPE is the type of the
3704 array or function or pointer, and DECLARATOR has had its
3705 outermost layer removed. */
3707 if (array_ptr_quals != NULL_TREE || array_parm_static)
3709 /* Only the innermost declarator (making a parameter be of
3710 array type which is converted to pointer type)
3711 may have static or type qualifiers. */
3712 error ("static or type qualifiers in non-parameter array declarator");
3713 array_ptr_quals = NULL_TREE;
3714 array_parm_static = 0;
3717 if (TREE_CODE (declarator) == TREE_LIST)
3719 /* We encode a declarator with embedded attributes using
3720 a TREE_LIST. */
3721 tree attrs = TREE_PURPOSE (declarator);
3722 tree inner_decl;
3723 int attr_flags = 0;
3724 declarator = TREE_VALUE (declarator);
3725 inner_decl = declarator;
3726 while (inner_decl != NULL_TREE
3727 && TREE_CODE (inner_decl) == TREE_LIST)
3728 inner_decl = TREE_VALUE (inner_decl);
3729 if (inner_decl == NULL_TREE
3730 || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
3731 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
3732 else if (TREE_CODE (inner_decl) == CALL_EXPR)
3733 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
3734 else if (TREE_CODE (inner_decl) == ARRAY_REF)
3735 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
3736 returned_attrs = decl_attributes (&type,
3737 chainon (returned_attrs, attrs),
3738 attr_flags);
3740 else if (TREE_CODE (declarator) == ARRAY_REF)
3742 tree itype = NULL_TREE;
3743 tree size = TREE_OPERAND (declarator, 1);
3744 /* The index is a signed object `sizetype' bits wide. */
3745 tree index_type = c_common_signed_type (sizetype);
3747 array_ptr_quals = TREE_TYPE (declarator);
3748 array_parm_static = TREE_STATIC (declarator);
3750 declarator = TREE_OPERAND (declarator, 0);
3752 /* Check for some types that there cannot be arrays of. */
3754 if (VOID_TYPE_P (type))
3756 error ("declaration of `%s' as array of voids", name);
3757 type = error_mark_node;
3760 if (TREE_CODE (type) == FUNCTION_TYPE)
3762 error ("declaration of `%s' as array of functions", name);
3763 type = error_mark_node;
3766 if (pedantic && flexible_array_type_p (type))
3767 pedwarn ("invalid use of structure with flexible array member");
3769 if (size == error_mark_node)
3770 type = error_mark_node;
3772 if (type == error_mark_node)
3773 continue;
3775 /* If size was specified, set ITYPE to a range-type for that size.
3776 Otherwise, ITYPE remains null. finish_decl may figure it out
3777 from an initial value. */
3779 if (size)
3781 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3782 STRIP_TYPE_NOPS (size);
3784 if (! INTEGRAL_TYPE_P (TREE_TYPE (size)))
3786 error ("size of array `%s' has non-integer type", name);
3787 size = integer_one_node;
3790 if (pedantic && integer_zerop (size))
3791 pedwarn ("ISO C forbids zero-size array `%s'", name);
3793 if (TREE_CODE (size) == INTEGER_CST)
3795 constant_expression_warning (size);
3796 if (tree_int_cst_sgn (size) < 0)
3798 error ("size of array `%s' is negative", name);
3799 size = integer_one_node;
3802 else
3804 /* Make sure the array size remains visibly nonconstant
3805 even if it is (eg) a const variable with known value. */
3806 size_varies = 1;
3808 if (!flag_isoc99 && pedantic)
3810 if (TREE_CONSTANT (size))
3811 pedwarn ("ISO C90 forbids array `%s' whose size can't be evaluated",
3812 name);
3813 else
3814 pedwarn ("ISO C90 forbids variable-size array `%s'",
3815 name);
3819 if (integer_zerop (size))
3821 /* A zero-length array cannot be represented with an
3822 unsigned index type, which is what we'll get with
3823 build_index_type. Create an open-ended range instead. */
3824 itype = build_range_type (sizetype, size, NULL_TREE);
3826 else
3828 /* Compute the maximum valid index, that is, size - 1.
3829 Do the calculation in index_type, so that if it is
3830 a variable the computations will be done in the
3831 proper mode. */
3832 itype = fold (build (MINUS_EXPR, index_type,
3833 convert (index_type, size),
3834 convert (index_type, size_one_node)));
3836 /* If that overflowed, the array is too big.
3837 ??? While a size of INT_MAX+1 technically shouldn't
3838 cause an overflow (because we subtract 1), the overflow
3839 is recorded during the conversion to index_type, before
3840 the subtraction. Handling this case seems like an
3841 unnecessary complication. */
3842 if (TREE_OVERFLOW (itype))
3844 error ("size of array `%s' is too large", name);
3845 type = error_mark_node;
3846 continue;
3849 if (size_varies)
3851 /* We must be able to distinguish the
3852 SAVE_EXPR_CONTEXT for the variably-sized type
3853 so that we can set it correctly in
3854 set_save_expr_context. The convention is
3855 that all SAVE_EXPRs that need to be reset
3856 have NULL_TREE for their SAVE_EXPR_CONTEXT. */
3857 tree cfd = current_function_decl;
3858 if (decl_context == PARM)
3859 current_function_decl = NULL_TREE;
3860 itype = variable_size (itype);
3861 if (decl_context == PARM)
3862 current_function_decl = cfd;
3864 itype = build_index_type (itype);
3867 else if (decl_context == FIELD)
3869 if (pedantic && !flag_isoc99 && !in_system_header)
3870 pedwarn ("ISO C90 does not support flexible array members");
3872 /* ISO C99 Flexible array members are effectively identical
3873 to GCC's zero-length array extension. */
3874 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
3877 /* If pedantic, complain about arrays of incomplete types. */
3879 if (pedantic && !COMPLETE_TYPE_P (type))
3880 pedwarn ("array type has incomplete element type");
3882 /* Build the array type itself, then merge any constancy or
3883 volatility into the target type. We must do it in this order
3884 to ensure that the TYPE_MAIN_VARIANT field of the array type
3885 is set correctly. */
3887 type = build_array_type (type, itype);
3888 if (type_quals)
3889 type = c_build_qualified_type (type, type_quals);
3891 if (size_varies)
3892 C_TYPE_VARIABLE_SIZE (type) = 1;
3894 /* The GCC extension for zero-length arrays differs from
3895 ISO flexible array members in that sizeof yields zero. */
3896 if (size && integer_zerop (size))
3898 layout_type (type);
3899 TYPE_SIZE (type) = bitsize_zero_node;
3900 TYPE_SIZE_UNIT (type) = size_zero_node;
3902 if (decl_context != PARM
3903 && (array_ptr_quals != NULL_TREE || array_parm_static))
3905 error ("static or type qualifiers in non-parameter array declarator");
3906 array_ptr_quals = NULL_TREE;
3907 array_parm_static = 0;
3910 else if (TREE_CODE (declarator) == CALL_EXPR)
3912 tree arg_types;
3914 /* Declaring a function type.
3915 Make sure we have a valid type for the function to return. */
3916 if (type == error_mark_node)
3917 continue;
3919 size_varies = 0;
3921 /* Warn about some types functions can't return. */
3923 if (TREE_CODE (type) == FUNCTION_TYPE)
3925 error ("`%s' declared as function returning a function", name);
3926 type = integer_type_node;
3928 if (TREE_CODE (type) == ARRAY_TYPE)
3930 error ("`%s' declared as function returning an array", name);
3931 type = integer_type_node;
3934 /* Construct the function type and go to the next
3935 inner layer of declarator. */
3937 arg_types = grokparms (TREE_OPERAND (declarator, 1),
3938 funcdef_flag
3939 /* Say it's a definition
3940 only for the CALL_EXPR
3941 closest to the identifier. */
3942 && TREE_CODE (TREE_OPERAND (declarator, 0)) == IDENTIFIER_NODE);
3943 /* Type qualifiers before the return type of the function
3944 qualify the return type, not the function type. */
3945 if (type_quals)
3947 /* Type qualifiers on a function return type are normally
3948 permitted by the standard but have no effect, so give a
3949 warning at -Wextra. Qualifiers on a void return type have
3950 meaning as a GNU extension, and are banned on function
3951 definitions in ISO C. FIXME: strictly we shouldn't
3952 pedwarn for qualified void return types except on function
3953 definitions, but not doing so could lead to the undesirable
3954 state of a "volatile void" function return type not being
3955 warned about, and a use of the function being compiled
3956 with GNU semantics, with no diagnostics under -pedantic. */
3957 if (VOID_TYPE_P (type) && pedantic && !in_system_header)
3958 pedwarn ("ISO C forbids qualified void function return type");
3959 else if (extra_warnings
3960 && !(VOID_TYPE_P (type)
3961 && type_quals == TYPE_QUAL_VOLATILE))
3962 warning ("type qualifiers ignored on function return type");
3964 type = c_build_qualified_type (type, type_quals);
3966 type_quals = TYPE_UNQUALIFIED;
3968 type = build_function_type (type, arg_types);
3969 declarator = TREE_OPERAND (declarator, 0);
3971 /* Set the TYPE_CONTEXTs for each tagged type which is local to
3972 the formal parameter list of this FUNCTION_TYPE to point to
3973 the FUNCTION_TYPE node itself. */
3976 tree link;
3978 for (link = last_function_parm_tags;
3979 link;
3980 link = TREE_CHAIN (link))
3981 TYPE_CONTEXT (TREE_VALUE (link)) = type;
3984 else if (TREE_CODE (declarator) == INDIRECT_REF)
3986 /* Merge any constancy or volatility into the target type
3987 for the pointer. */
3989 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
3990 && type_quals)
3991 pedwarn ("ISO C forbids qualified function types");
3992 if (type_quals)
3993 type = c_build_qualified_type (type, type_quals);
3994 type_quals = TYPE_UNQUALIFIED;
3995 size_varies = 0;
3997 type = build_pointer_type (type);
3999 /* Process a list of type modifier keywords
4000 (such as const or volatile) that were given inside the `*'. */
4002 if (TREE_TYPE (declarator))
4004 tree typemodlist;
4005 int erred = 0;
4007 constp = 0;
4008 volatilep = 0;
4009 restrictp = 0;
4010 for (typemodlist = TREE_TYPE (declarator); typemodlist;
4011 typemodlist = TREE_CHAIN (typemodlist))
4013 tree qualifier = TREE_VALUE (typemodlist);
4015 if (C_IS_RESERVED_WORD (qualifier))
4017 if (C_RID_CODE (qualifier) == RID_CONST)
4018 constp++;
4019 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4020 volatilep++;
4021 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4022 restrictp++;
4023 else
4024 erred++;
4026 else
4027 erred++;
4030 if (erred)
4031 error ("invalid type modifier within pointer declarator");
4032 if (constp > 1 && ! flag_isoc99)
4033 pedwarn ("duplicate `const'");
4034 if (volatilep > 1 && ! flag_isoc99)
4035 pedwarn ("duplicate `volatile'");
4036 if (restrictp > 1 && ! flag_isoc99)
4037 pedwarn ("duplicate `restrict'");
4039 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4040 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4041 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4044 declarator = TREE_OPERAND (declarator, 0);
4046 else
4047 abort ();
4051 /* Now TYPE has the actual type. */
4053 /* Did array size calculations overflow? */
4055 if (TREE_CODE (type) == ARRAY_TYPE
4056 && COMPLETE_TYPE_P (type)
4057 && TREE_OVERFLOW (TYPE_SIZE (type)))
4059 error ("size of array `%s' is too large", name);
4060 /* If we proceed with the array type as it is, we'll eventually
4061 crash in tree_low_cst(). */
4062 type = error_mark_node;
4065 /* If this is declaring a typedef name, return a TYPE_DECL. */
4067 if (specbits & (1 << (int) RID_TYPEDEF))
4069 tree decl;
4070 /* Note that the grammar rejects storage classes
4071 in typenames, fields or parameters */
4072 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4073 && type_quals)
4074 pedwarn ("ISO C forbids qualified function types");
4075 if (type_quals)
4076 type = c_build_qualified_type (type, type_quals);
4077 decl = build_decl (TYPE_DECL, declarator, type);
4078 if ((specbits & (1 << (int) RID_SIGNED))
4079 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
4080 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4081 decl_attributes (&decl, returned_attrs, 0);
4082 return decl;
4085 /* Detect the case of an array type of unspecified size
4086 which came, as such, direct from a typedef name.
4087 We must copy the type, so that each identifier gets
4088 a distinct type, so that each identifier's size can be
4089 controlled separately by its own initializer. */
4091 if (type != 0 && typedef_type != 0
4092 && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0
4093 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
4095 type = build_array_type (TREE_TYPE (type), 0);
4096 if (size_varies)
4097 C_TYPE_VARIABLE_SIZE (type) = 1;
4100 /* If this is a type name (such as, in a cast or sizeof),
4101 compute the type and return it now. */
4103 if (decl_context == TYPENAME)
4105 /* Note that the grammar rejects storage classes
4106 in typenames, fields or parameters */
4107 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4108 && type_quals)
4109 pedwarn ("ISO C forbids const or volatile function types");
4110 if (type_quals)
4111 type = c_build_qualified_type (type, type_quals);
4112 decl_attributes (&type, returned_attrs, 0);
4113 return type;
4116 /* Aside from typedefs and type names (handle above),
4117 `void' at top level (not within pointer)
4118 is allowed only in public variables.
4119 We don't complain about parms either, but that is because
4120 a better error message can be made later. */
4122 if (VOID_TYPE_P (type) && decl_context != PARM
4123 && ! ((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4124 && ((specbits & (1 << (int) RID_EXTERN))
4125 || (current_scope == global_scope
4126 && !(specbits
4127 & ((1 << (int) RID_STATIC) | (1 << (int) RID_REGISTER)))))))
4129 error ("variable or field `%s' declared void", name);
4130 type = integer_type_node;
4133 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4134 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4137 tree decl;
4139 if (decl_context == PARM)
4141 tree type_as_written;
4142 tree promoted_type;
4144 /* A parameter declared as an array of T is really a pointer to T.
4145 One declared as a function is really a pointer to a function. */
4147 if (TREE_CODE (type) == ARRAY_TYPE)
4149 /* Transfer const-ness of array into that of type pointed to. */
4150 type = TREE_TYPE (type);
4151 if (type_quals)
4152 type = c_build_qualified_type (type, type_quals);
4153 type = build_pointer_type (type);
4154 type_quals = TYPE_UNQUALIFIED;
4155 if (array_ptr_quals)
4157 tree new_ptr_quals, new_ptr_attrs;
4158 int erred = 0;
4159 split_specs_attrs (array_ptr_quals, &new_ptr_quals, &new_ptr_attrs);
4160 /* We don't yet implement attributes in this context. */
4161 if (new_ptr_attrs != NULL_TREE)
4162 warning ("attributes in parameter array declarator ignored");
4164 constp = 0;
4165 volatilep = 0;
4166 restrictp = 0;
4167 for (; new_ptr_quals; new_ptr_quals = TREE_CHAIN (new_ptr_quals))
4169 tree qualifier = TREE_VALUE (new_ptr_quals);
4171 if (C_IS_RESERVED_WORD (qualifier))
4173 if (C_RID_CODE (qualifier) == RID_CONST)
4174 constp++;
4175 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4176 volatilep++;
4177 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4178 restrictp++;
4179 else
4180 erred++;
4182 else
4183 erred++;
4186 if (erred)
4187 error ("invalid type modifier within array declarator");
4189 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4190 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4191 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4193 size_varies = 0;
4195 else if (TREE_CODE (type) == FUNCTION_TYPE)
4197 if (pedantic && type_quals)
4198 pedwarn ("ISO C forbids qualified function types");
4199 if (type_quals)
4200 type = c_build_qualified_type (type, type_quals);
4201 type = build_pointer_type (type);
4202 type_quals = TYPE_UNQUALIFIED;
4204 else if (type_quals)
4205 type = c_build_qualified_type (type, type_quals);
4207 type_as_written = type;
4209 decl = build_decl (PARM_DECL, declarator, type);
4210 if (size_varies)
4211 C_DECL_VARIABLE_SIZE (decl) = 1;
4213 /* Compute the type actually passed in the parmlist,
4214 for the case where there is no prototype.
4215 (For example, shorts and chars are passed as ints.)
4216 When there is a prototype, this is overridden later. */
4218 if (type == error_mark_node)
4219 promoted_type = type;
4220 else
4221 promoted_type = c_type_promotes_to (type);
4223 DECL_ARG_TYPE (decl) = promoted_type;
4224 DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
4226 else if (decl_context == FIELD)
4228 /* Structure field. It may not be a function. */
4230 if (TREE_CODE (type) == FUNCTION_TYPE)
4232 error ("field `%s' declared as a function", name);
4233 type = build_pointer_type (type);
4235 else if (TREE_CODE (type) != ERROR_MARK
4236 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4238 error ("field `%s' has incomplete type", name);
4239 type = error_mark_node;
4241 /* Move type qualifiers down to element of an array. */
4242 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4243 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4244 type_quals),
4245 TYPE_DOMAIN (type));
4246 decl = build_decl (FIELD_DECL, declarator, type);
4247 DECL_NONADDRESSABLE_P (decl) = bitfield;
4249 if (size_varies)
4250 C_DECL_VARIABLE_SIZE (decl) = 1;
4252 else if (TREE_CODE (type) == FUNCTION_TYPE)
4254 /* Every function declaration is "external"
4255 except for those which are inside a function body
4256 in which `auto' is used.
4257 That is a case not specified by ANSI C,
4258 and we use it for forward declarations for nested functions. */
4259 int extern_ref = (!(specbits & (1 << (int) RID_AUTO))
4260 || current_scope == global_scope);
4262 if (specbits & (1 << (int) RID_AUTO)
4263 && (pedantic || current_scope == global_scope))
4264 pedwarn ("invalid storage class for function `%s'", name);
4265 if (specbits & (1 << (int) RID_REGISTER))
4266 error ("invalid storage class for function `%s'", name);
4267 if (specbits & (1 << (int) RID_THREAD))
4268 error ("invalid storage class for function `%s'", name);
4269 /* Function declaration not at file scope.
4270 Storage classes other than `extern' are not allowed
4271 and `extern' makes no difference. */
4272 if (current_scope != global_scope
4273 && (specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_INLINE)))
4274 && pedantic)
4275 pedwarn ("invalid storage class for function `%s'", name);
4277 decl = build_decl (FUNCTION_DECL, declarator, type);
4278 decl = build_decl_attribute_variant (decl, decl_attr);
4280 DECL_LANG_SPECIFIC (decl)
4281 = ggc_alloc_cleared (sizeof (struct lang_decl));
4283 if (pedantic && type_quals && ! DECL_IN_SYSTEM_HEADER (decl))
4284 pedwarn ("ISO C forbids qualified function types");
4286 /* GNU C interprets a `volatile void' return type to indicate
4287 that the function does not return. */
4288 if ((type_quals & TYPE_QUAL_VOLATILE)
4289 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4290 warning ("`noreturn' function returns non-void value");
4292 if (extern_ref)
4293 DECL_EXTERNAL (decl) = 1;
4294 /* Record absence of global scope for `static' or `auto'. */
4295 TREE_PUBLIC (decl)
4296 = !(specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_AUTO)));
4298 if (defaulted_int)
4299 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4301 /* Record presence of `inline', if it is reasonable. */
4302 if (MAIN_NAME_P (declarator))
4304 if (inlinep)
4305 warning ("cannot inline function `main'");
4307 else if (inlinep)
4309 /* Record that the function is declared `inline'. */
4310 DECL_DECLARED_INLINE_P (decl) = 1;
4312 /* Do not mark bare declarations as DECL_INLINE. Doing so
4313 in the presence of multiple declarations can result in
4314 the abstract origin pointing between the declarations,
4315 which will confuse dwarf2out. */
4316 if (initialized)
4318 DECL_INLINE (decl) = 1;
4319 if (specbits & (1 << (int) RID_EXTERN))
4320 current_extern_inline = 1;
4323 /* If -finline-functions, assume it can be inlined. This does
4324 two things: let the function be deferred until it is actually
4325 needed, and let dwarf2 know that the function is inlinable. */
4326 else if (flag_inline_trees == 2 && initialized)
4327 DECL_INLINE (decl) = 1;
4329 else
4331 /* It's a variable. */
4332 /* An uninitialized decl with `extern' is a reference. */
4333 int extern_ref = !initialized && (specbits & (1 << (int) RID_EXTERN));
4335 /* Move type qualifiers down to element of an array. */
4336 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4338 int saved_align = TYPE_ALIGN(type);
4339 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4340 type_quals),
4341 TYPE_DOMAIN (type));
4342 TYPE_ALIGN (type) = saved_align;
4344 else if (type_quals)
4345 type = c_build_qualified_type (type, type_quals);
4347 /* It is invalid to create an `extern' declaration for a
4348 variable if there is a global declaration that is
4349 `static'. */
4350 if (extern_ref && current_scope != global_scope)
4352 tree global_decl;
4354 global_decl = identifier_global_value (declarator);
4355 if (global_decl
4356 && TREE_CODE (global_decl) == VAR_DECL
4357 && !TREE_PUBLIC (global_decl))
4358 error ("variable previously declared `static' redeclared "
4359 "`extern'");
4362 decl = build_decl (VAR_DECL, declarator, type);
4363 if (size_varies)
4364 C_DECL_VARIABLE_SIZE (decl) = 1;
4366 if (inlinep)
4367 pedwarn ("%Jvariable '%D' declared `inline'", decl, decl);
4369 DECL_EXTERNAL (decl) = extern_ref;
4371 /* At file scope, the presence of a `static' or `register' storage
4372 class specifier, or the absence of all storage class specifiers
4373 makes this declaration a definition (perhaps tentative). Also,
4374 the absence of both `static' and `register' makes it public. */
4375 if (current_scope == global_scope)
4377 TREE_PUBLIC (decl) = !(specbits & ((1 << (int) RID_STATIC)
4378 | (1 << (int) RID_REGISTER)));
4379 TREE_STATIC (decl) = !extern_ref;
4381 /* Not at file scope, only `static' makes a static definition. */
4382 else
4384 TREE_STATIC (decl) = (specbits & (1 << (int) RID_STATIC)) != 0;
4385 TREE_PUBLIC (decl) = extern_ref;
4388 if (specbits & 1 << (int) RID_THREAD)
4390 if (targetm.have_tls)
4391 DECL_THREAD_LOCAL (decl) = 1;
4392 else
4393 /* A mere warning is sure to result in improper semantics
4394 at runtime. Don't bother to allow this to compile. */
4395 error ("thread-local storage not supported for this target");
4399 /* Record `register' declaration for warnings on &
4400 and in case doing stupid register allocation. */
4402 if (specbits & (1 << (int) RID_REGISTER))
4403 DECL_REGISTER (decl) = 1;
4405 /* Record constancy and volatility. */
4406 c_apply_type_quals_to_decl (type_quals, decl);
4408 /* If a type has volatile components, it should be stored in memory.
4409 Otherwise, the fact that those components are volatile
4410 will be ignored, and would even crash the compiler. */
4411 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
4412 c_mark_addressable (decl);
4414 #ifdef ENABLE_CHECKING
4415 /* This is the earliest point at which we might know the assembler
4416 name of a variable. Thus, if it's known before this, die horribly. */
4417 if (DECL_ASSEMBLER_NAME_SET_P (decl))
4418 abort ();
4419 #endif
4421 decl_attributes (&decl, returned_attrs, 0);
4423 return decl;
4427 /* Decode the parameter-list info for a function type or function definition.
4428 The argument is the value returned by `get_parm_info' (or made in parse.y
4429 if there is an identifier list instead of a parameter decl list).
4430 These two functions are separate because when a function returns
4431 or receives functions then each is called multiple times but the order
4432 of calls is different. The last call to `grokparms' is always the one
4433 that contains the formal parameter names of a function definition.
4435 Store in `last_function_parms' a chain of the decls of parms.
4436 Also store in `last_function_parm_tags' a chain of the struct, union,
4437 and enum tags declared among the parms.
4439 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4441 FUNCDEF_FLAG is nonzero for a function definition, 0 for
4442 a mere declaration. A nonempty identifier-list gets an error message
4443 when FUNCDEF_FLAG is zero. */
4445 static tree
4446 grokparms (tree parms_info, int funcdef_flag)
4448 tree first_parm = TREE_CHAIN (parms_info);
4450 last_function_parms = TREE_PURPOSE (parms_info);
4451 last_function_parm_tags = TREE_VALUE (parms_info);
4452 last_function_parm_others = TREE_TYPE (parms_info);
4454 if (warn_strict_prototypes && first_parm == 0 && !funcdef_flag
4455 && !in_system_header)
4456 warning ("function declaration isn't a prototype");
4458 if (first_parm != 0
4459 && TREE_CODE (TREE_VALUE (first_parm)) == IDENTIFIER_NODE)
4461 if (! funcdef_flag)
4462 pedwarn ("parameter names (without types) in function declaration");
4464 last_function_parms = first_parm;
4465 return 0;
4467 else
4469 tree parm;
4470 tree typelt;
4471 /* If the arg types are incomplete in a declaration,
4472 they must include undefined tags.
4473 These tags can never be defined in the scope of the declaration,
4474 so the types can never be completed,
4475 and no call can be compiled successfully. */
4477 for (parm = last_function_parms, typelt = first_parm;
4478 parm;
4479 parm = TREE_CHAIN (parm))
4480 /* Skip over any enumeration constants declared here. */
4481 if (TREE_CODE (parm) == PARM_DECL)
4483 /* Barf if the parameter itself has an incomplete type. */
4484 tree type = TREE_VALUE (typelt);
4485 if (type == error_mark_node)
4486 continue;
4487 if (!COMPLETE_TYPE_P (type))
4489 if (funcdef_flag && DECL_NAME (parm) != 0)
4490 error ("parameter `%s' has incomplete type",
4491 IDENTIFIER_POINTER (DECL_NAME (parm)));
4492 else
4493 warning ("parameter has incomplete type");
4494 if (funcdef_flag)
4496 TREE_VALUE (typelt) = error_mark_node;
4497 TREE_TYPE (parm) = error_mark_node;
4500 typelt = TREE_CHAIN (typelt);
4503 return first_parm;
4507 /* Return a tree_list node with info on a parameter list just parsed.
4508 The TREE_PURPOSE is a list of decls of those parms.
4509 The TREE_VALUE is a list of structure, union and enum tags defined.
4510 The TREE_CHAIN is a list of argument types to go in the FUNCTION_TYPE.
4511 The TREE_TYPE is a list of non-parameter decls which appeared with the
4512 parameters.
4513 This tree_list node is later fed to `grokparms'.
4515 VOID_AT_END nonzero means append `void' to the end of the type-list.
4516 Zero means the parmlist ended with an ellipsis so don't append `void'. */
4518 tree
4519 get_parm_info (int void_at_end)
4521 tree decl, type, list;
4522 tree types = 0;
4523 tree *last_type = &types;
4524 tree tags = current_scope->tags;
4525 tree parms = current_scope->parms;
4526 tree others = current_scope->names;
4527 static bool explained_incomplete_types = false;
4528 bool gave_void_only_once_err = false;
4530 /* Just "void" (and no ellipsis) is special. There are really no parms.
4531 But if the "void" is qualified (by "const" or "volatile"), or has a
4532 storage class specifier ("register"), then the behavior is undefined;
4533 issue an error. Typedefs for "void" are OK (see DR#157). */
4534 if (void_at_end && parms != 0
4535 && TREE_CHAIN (parms) == 0
4536 && VOID_TYPE_P (TREE_TYPE (parms))
4537 && !DECL_NAME (parms))
4539 if (TREE_THIS_VOLATILE (parms)
4540 || TREE_READONLY (parms)
4541 || DECL_REGISTER (parms))
4542 error ("\"void\" as only parameter may not be qualified");
4544 return tree_cons (0, 0, tree_cons (0, void_type_node, 0));
4547 /* Sanity check all of the parameter declarations. */
4548 for (decl = parms; decl; decl = TREE_CHAIN (decl))
4550 if (TREE_CODE (decl) != PARM_DECL)
4551 abort ();
4552 if (TREE_ASM_WRITTEN (decl))
4553 abort ();
4555 /* Since there is a prototype, args are passed in their
4556 declared types. The back end may override this. */
4557 type = TREE_TYPE (decl);
4558 DECL_ARG_TYPE (decl) = type;
4560 /* Check for (..., void, ...) and issue an error. */
4561 if (VOID_TYPE_P (type) && !DECL_NAME (decl) && !gave_void_only_once_err)
4563 error ("\"void\" must be the only parameter");
4564 gave_void_only_once_err = true;
4567 type = build_tree_list (0, type);
4568 *last_type = type;
4569 last_type = &TREE_CHAIN (type);
4572 /* Check the list of non-parameter decls for any forward parm decls
4573 that never got real decls. */
4574 for (decl = others; decl; decl = TREE_CHAIN (decl))
4575 if (TREE_CODE (decl) == PARM_DECL)
4577 if (!TREE_ASM_WRITTEN (decl))
4578 abort ();
4580 error ("%Jparameter \"%D\" has just a forward declaration",
4581 decl, decl);
4584 /* Warn about any struct, union or enum tags defined within this
4585 list. The scope of such types is limited to this declaration,
4586 which is rarely if ever desirable (it's impossible to call such
4587 a function with type-correct arguments). */
4588 for (decl = tags; decl; decl = TREE_CHAIN (decl))
4590 enum tree_code code = TREE_CODE (TREE_VALUE (decl));
4591 const char *keyword;
4592 /* An anonymous union parm type is meaningful as a GNU extension.
4593 So don't warn for that. */
4594 if (code == UNION_TYPE && TREE_PURPOSE (decl) == 0 && !pedantic)
4595 continue;
4597 /* The keyword should not be translated. */
4598 switch (code)
4600 case RECORD_TYPE: keyword = "struct"; break;
4601 case UNION_TYPE: keyword = "union"; break;
4602 case ENUMERAL_TYPE: keyword = "enum"; break;
4603 default: abort ();
4606 if (TREE_PURPOSE (decl))
4607 /* The first %s will be one of 'struct', 'union', or 'enum'. */
4608 warning ("\"%s %s\" declared inside parameter list",
4609 keyword, IDENTIFIER_POINTER (TREE_PURPOSE (decl)));
4610 else
4611 /* The %s will be one of 'struct', 'union', or 'enum'. */
4612 warning ("anonymous %s declared inside parameter list", keyword);
4614 if (! explained_incomplete_types)
4616 warning ("its scope is only this definition or declaration,"
4617 " which is probably not what you want");
4618 explained_incomplete_types = true;
4623 if (void_at_end)
4625 type = build_tree_list (0, void_type_node);
4626 *last_type = type;
4629 list = tree_cons (parms, tags, types);
4630 TREE_TYPE (list) = others;
4631 return list;
4634 /* Get the struct, enum or union (CODE says which) with tag NAME.
4635 Define the tag as a forward-reference if it is not defined. */
4637 tree
4638 xref_tag (enum tree_code code, tree name)
4640 /* If a cross reference is requested, look up the type
4641 already defined for this tag and return it. */
4643 tree ref = lookup_tag (code, name, 0);
4644 /* If this is the right type of tag, return what we found.
4645 (This reference will be shadowed by shadow_tag later if appropriate.)
4646 If this is the wrong type of tag, do not return it. If it was the
4647 wrong type in the same scope, we will have had an error
4648 message already; if in a different scope and declaring
4649 a name, pending_xref_error will give an error message; but if in a
4650 different scope and not declaring a name, this tag should
4651 shadow the previous declaration of a different type of tag, and
4652 this would not work properly if we return the reference found.
4653 (For example, with "struct foo" in an outer scope, "union foo;"
4654 must shadow that tag with a new one of union type.) */
4655 if (ref && TREE_CODE (ref) == code)
4656 return ref;
4658 /* If no such tag is yet defined, create a forward-reference node
4659 and record it as the "definition".
4660 When a real declaration of this type is found,
4661 the forward-reference will be altered into a real type. */
4663 ref = make_node (code);
4664 if (code == ENUMERAL_TYPE)
4666 /* Give the type a default layout like unsigned int
4667 to avoid crashing if it does not get defined. */
4668 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
4669 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
4670 TYPE_USER_ALIGN (ref) = 0;
4671 TREE_UNSIGNED (ref) = 1;
4672 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
4673 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
4674 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
4677 pushtag (name, ref);
4679 return ref;
4682 /* Make sure that the tag NAME is defined *in the current scope*
4683 at least as a forward reference.
4684 CODE says which kind of tag NAME ought to be. */
4686 tree
4687 start_struct (enum tree_code code, tree name)
4689 /* If there is already a tag defined at this scope
4690 (as a forward reference), just return it. */
4692 tree ref = 0;
4694 if (name != 0)
4695 ref = lookup_tag (code, name, 1);
4696 if (ref && TREE_CODE (ref) == code)
4698 if (TYPE_FIELDS (ref))
4700 if (code == UNION_TYPE)
4701 error ("redefinition of `union %s'", IDENTIFIER_POINTER (name));
4702 else
4703 error ("redefinition of `struct %s'", IDENTIFIER_POINTER (name));
4706 else
4708 /* Otherwise create a forward-reference just so the tag is in scope. */
4710 ref = make_node (code);
4711 pushtag (name, ref);
4714 C_TYPE_BEING_DEFINED (ref) = 1;
4715 TYPE_PACKED (ref) = flag_pack_struct;
4716 return ref;
4719 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
4720 of a structure component, returning a FIELD_DECL node.
4721 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node.
4723 This is done during the parsing of the struct declaration.
4724 The FIELD_DECL nodes are chained together and the lot of them
4725 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
4727 tree
4728 grokfield (tree declarator, tree declspecs, tree width)
4730 tree value;
4732 if (declarator == NULL_TREE && width == NULL_TREE)
4734 /* This is an unnamed decl.
4736 If we have something of the form "union { list } ;" then this
4737 is the anonymous union extension. Similarly for struct.
4739 If this is something of the form "struct foo;", then
4740 If MS extensions are enabled, this is handled as an
4741 anonymous struct.
4742 Otherwise this is a forward declaration of a structure tag.
4744 If this is something of the form "foo;" and foo is a TYPE_DECL, then
4745 If MS extensions are enabled and foo names a structure, then
4746 again this is an anonymous struct.
4747 Otherwise this is an error.
4749 Oh what a horrid tangled web we weave. I wonder if MS consciously
4750 took this from Plan 9 or if it was an accident of implementation
4751 that took root before someone noticed the bug... */
4753 tree type = TREE_VALUE (declspecs);
4755 if (flag_ms_extensions && TREE_CODE (type) == TYPE_DECL)
4756 type = TREE_TYPE (type);
4757 if (TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE)
4759 if (flag_ms_extensions)
4760 ; /* ok */
4761 else if (flag_iso)
4762 goto warn_unnamed_field;
4763 else if (TYPE_NAME (type) == NULL)
4764 ; /* ok */
4765 else
4766 goto warn_unnamed_field;
4768 else
4770 warn_unnamed_field:
4771 warning ("declaration does not declare anything");
4772 return NULL_TREE;
4776 value = grokdeclarator (declarator, declspecs, width ? BITFIELD : FIELD, 0);
4778 finish_decl (value, NULL_TREE, NULL_TREE);
4779 DECL_INITIAL (value) = width;
4781 if (c_dialect_objc ())
4782 objc_check_decl (value);
4783 return value;
4786 /* Generate an error for any duplicate field names in FIELDLIST. Munge
4787 the list such that this does not present a problem later. */
4789 static void
4790 detect_field_duplicates (tree fieldlist)
4792 tree x, y;
4793 int timeout = 10;
4795 /* First, see if there are more than "a few" fields.
4796 This is trivially true if there are zero or one fields. */
4797 if (!fieldlist)
4798 return;
4799 x = TREE_CHAIN (fieldlist);
4800 if (!x)
4801 return;
4802 do {
4803 timeout--;
4804 x = TREE_CHAIN (x);
4805 } while (timeout > 0 && x);
4807 /* If there were "few" fields, avoid the overhead of allocating
4808 a hash table. Instead just do the nested traversal thing. */
4809 if (timeout > 0)
4811 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
4812 if (DECL_NAME (x))
4814 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
4815 if (DECL_NAME (y) == DECL_NAME (x))
4817 error ("%Jduplicate member '%D'", x, x);
4818 DECL_NAME (x) = NULL_TREE;
4822 else
4824 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
4825 void **slot;
4827 for (x = fieldlist; x ; x = TREE_CHAIN (x))
4828 if ((y = DECL_NAME (x)) != 0)
4830 slot = htab_find_slot (htab, y, INSERT);
4831 if (*slot)
4833 error ("%Jduplicate member '%D'", x, x);
4834 DECL_NAME (x) = NULL_TREE;
4836 *slot = y;
4839 htab_delete (htab);
4843 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
4844 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
4845 ATTRIBUTES are attributes to be applied to the structure. */
4847 tree
4848 finish_struct (tree t, tree fieldlist, tree attributes)
4850 tree x;
4851 int toplevel = global_scope == current_scope;
4852 int saw_named_field;
4854 /* If this type was previously laid out as a forward reference,
4855 make sure we lay it out again. */
4857 TYPE_SIZE (t) = 0;
4859 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
4861 /* Nameless union parm types are useful as GCC extension. */
4862 if (! (TREE_CODE (t) == UNION_TYPE && TYPE_NAME (t) == 0) && !pedantic)
4863 /* Otherwise, warn about any struct or union def. in parmlist. */
4864 if (in_parm_level_p ())
4866 if (pedantic)
4867 pedwarn ("%s defined inside parms",
4868 TREE_CODE (t) == UNION_TYPE ? _("union") : _("structure"));
4869 else
4870 warning ("%s defined inside parms",
4871 TREE_CODE (t) == UNION_TYPE ? _("union") : _("structure"));
4874 if (pedantic)
4876 for (x = fieldlist; x; x = TREE_CHAIN (x))
4877 if (DECL_NAME (x) != 0)
4878 break;
4880 if (x == 0)
4881 pedwarn ("%s has no %s",
4882 TREE_CODE (t) == UNION_TYPE ? _("union") : _("struct"),
4883 fieldlist ? _("named members") : _("members"));
4886 /* Install struct as DECL_CONTEXT of each field decl.
4887 Also process specified field sizes,m which is found in the DECL_INITIAL.
4888 Store 0 there, except for ": 0" fields (so we can find them
4889 and delete them, below). */
4891 saw_named_field = 0;
4892 for (x = fieldlist; x; x = TREE_CHAIN (x))
4894 DECL_CONTEXT (x) = t;
4895 DECL_PACKED (x) |= TYPE_PACKED (t);
4897 /* If any field is const, the structure type is pseudo-const. */
4898 if (TREE_READONLY (x))
4899 C_TYPE_FIELDS_READONLY (t) = 1;
4900 else
4902 /* A field that is pseudo-const makes the structure likewise. */
4903 tree t1 = TREE_TYPE (x);
4904 while (TREE_CODE (t1) == ARRAY_TYPE)
4905 t1 = TREE_TYPE (t1);
4906 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
4907 && C_TYPE_FIELDS_READONLY (t1))
4908 C_TYPE_FIELDS_READONLY (t) = 1;
4911 /* Any field that is volatile means variables of this type must be
4912 treated in some ways as volatile. */
4913 if (TREE_THIS_VOLATILE (x))
4914 C_TYPE_FIELDS_VOLATILE (t) = 1;
4916 /* Any field of nominal variable size implies structure is too. */
4917 if (C_DECL_VARIABLE_SIZE (x))
4918 C_TYPE_VARIABLE_SIZE (t) = 1;
4920 /* Detect invalid nested redefinition. */
4921 if (TREE_TYPE (x) == t)
4922 error ("nested redefinition of `%s'",
4923 IDENTIFIER_POINTER (TYPE_NAME (t)));
4925 /* Detect invalid bit-field size. */
4926 if (DECL_INITIAL (x))
4927 STRIP_NOPS (DECL_INITIAL (x));
4928 if (DECL_INITIAL (x))
4930 if (TREE_CODE (DECL_INITIAL (x)) == INTEGER_CST)
4931 constant_expression_warning (DECL_INITIAL (x));
4932 else
4934 error ("%Jbit-field '%D' width not an integer constant", x, x);
4935 DECL_INITIAL (x) = NULL;
4939 /* Detect invalid bit-field type. */
4940 if (DECL_INITIAL (x)
4941 && TREE_CODE (TREE_TYPE (x)) != INTEGER_TYPE
4942 && TREE_CODE (TREE_TYPE (x)) != BOOLEAN_TYPE
4943 && TREE_CODE (TREE_TYPE (x)) != ENUMERAL_TYPE)
4945 error ("%Jbit-field '%D' has invalid type", x, x);
4946 DECL_INITIAL (x) = NULL;
4949 if (DECL_INITIAL (x) && pedantic
4950 && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != integer_type_node
4951 && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != unsigned_type_node
4952 && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != boolean_type_node
4953 /* Accept an enum that's equivalent to int or unsigned int. */
4954 && !(TREE_CODE (TREE_TYPE (x)) == ENUMERAL_TYPE
4955 && (TYPE_PRECISION (TREE_TYPE (x))
4956 == TYPE_PRECISION (integer_type_node))))
4957 pedwarn ("%Jbit-field '%D' type invalid in ISO C", x, x);
4959 /* Detect and ignore out of range field width and process valid
4960 field widths. */
4961 if (DECL_INITIAL (x))
4963 int max_width
4964 = (TYPE_MAIN_VARIANT (TREE_TYPE (x)) == boolean_type_node
4965 ? CHAR_TYPE_SIZE : TYPE_PRECISION (TREE_TYPE (x)));
4967 if (tree_int_cst_sgn (DECL_INITIAL (x)) < 0)
4968 error ("%Jnegative width in bit-field '%D'", x, x);
4969 else if (0 < compare_tree_int (DECL_INITIAL (x), max_width))
4970 pedwarn ("%Jwidth of '%D' exceeds its type", x, x);
4971 else if (integer_zerop (DECL_INITIAL (x)) && DECL_NAME (x) != 0)
4972 error ("%Jzero width for bit-field '%D'", x, x);
4973 else
4975 /* The test above has assured us that TREE_INT_CST_HIGH is 0. */
4976 unsigned HOST_WIDE_INT width
4977 = tree_low_cst (DECL_INITIAL (x), 1);
4979 if (TREE_CODE (TREE_TYPE (x)) == ENUMERAL_TYPE
4980 && (width < min_precision (TYPE_MIN_VALUE (TREE_TYPE (x)),
4981 TREE_UNSIGNED (TREE_TYPE (x)))
4982 || (width
4983 < min_precision (TYPE_MAX_VALUE (TREE_TYPE (x)),
4984 TREE_UNSIGNED (TREE_TYPE (x))))))
4985 warning ("%J'%D' is narrower than values of its type", x, x);
4987 DECL_SIZE (x) = bitsize_int (width);
4988 DECL_BIT_FIELD (x) = 1;
4989 SET_DECL_C_BIT_FIELD (x);
4993 DECL_INITIAL (x) = 0;
4995 /* Detect flexible array member in an invalid context. */
4996 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
4997 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
4998 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
4999 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5001 if (TREE_CODE (t) == UNION_TYPE)
5002 error ("%Jflexible array member in union", x);
5003 else if (TREE_CHAIN (x) != NULL_TREE)
5004 error ("%Jflexible array member not at end of struct", x);
5005 else if (! saw_named_field)
5006 error ("%Jflexible array member in otherwise empty struct", x);
5009 if (pedantic && TREE_CODE (t) == RECORD_TYPE
5010 && flexible_array_type_p (TREE_TYPE (x)))
5011 pedwarn ("%Jinvalid use of structure with flexible array member", x);
5013 if (DECL_NAME (x))
5014 saw_named_field = 1;
5017 detect_field_duplicates (fieldlist);
5019 /* Now we have the nearly final fieldlist. Record it,
5020 then lay out the structure or union (including the fields). */
5022 TYPE_FIELDS (t) = fieldlist;
5024 layout_type (t);
5026 /* Delete all zero-width bit-fields from the fieldlist */
5028 tree *fieldlistp = &fieldlist;
5029 while (*fieldlistp)
5030 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp))
5031 *fieldlistp = TREE_CHAIN (*fieldlistp);
5032 else
5033 fieldlistp = &TREE_CHAIN (*fieldlistp);
5036 /* Now we have the truly final field list.
5037 Store it in this type and in the variants. */
5039 TYPE_FIELDS (t) = fieldlist;
5041 /* If there are lots of fields, sort so we can look through them fast.
5042 We arbitrarily consider 16 or more elts to be "a lot". */
5045 int len = 0;
5047 for (x = fieldlist; x; x = TREE_CHAIN (x))
5049 if (len > 15 || DECL_NAME (x) == NULL)
5050 break;
5051 len += 1;
5054 if (len > 15)
5056 tree *field_array;
5057 struct lang_type *space;
5058 struct sorted_fields_type *space2;
5060 len += list_length (x);
5062 /* Use the same allocation policy here that make_node uses, to
5063 ensure that this lives as long as the rest of the struct decl.
5064 All decls in an inline function need to be saved. */
5066 space = ggc_alloc (sizeof (struct lang_type));
5067 space2 = ggc_alloc (sizeof (struct sorted_fields_type) + len * sizeof (tree));
5069 len = 0;
5070 space->s = space2;
5071 field_array = &space2->elts[0];
5072 for (x = fieldlist; x; x = TREE_CHAIN (x))
5074 field_array[len++] = x;
5076 /* If there is anonymous struct or union, break out of the loop. */
5077 if (DECL_NAME (x) == NULL)
5078 break;
5080 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5081 if (x == NULL)
5083 TYPE_LANG_SPECIFIC (t) = space;
5084 TYPE_LANG_SPECIFIC (t)->s->len = len;
5085 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5086 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5091 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5093 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5094 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5095 TYPE_ALIGN (x) = TYPE_ALIGN (t);
5096 TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t);
5099 /* If this was supposed to be a transparent union, but we can't
5100 make it one, warn and turn off the flag. */
5101 if (TREE_CODE (t) == UNION_TYPE
5102 && TYPE_TRANSPARENT_UNION (t)
5103 && TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t)))
5105 TYPE_TRANSPARENT_UNION (t) = 0;
5106 warning ("union cannot be made transparent");
5109 /* If this structure or union completes the type of any previous
5110 variable declaration, lay it out and output its rtl. */
5111 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5113 x = TREE_CHAIN (x))
5115 tree decl = TREE_VALUE (x);
5116 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5117 layout_array_type (TREE_TYPE (decl));
5118 if (TREE_CODE (decl) != TYPE_DECL)
5120 layout_decl (decl, 0);
5121 if (c_dialect_objc ())
5122 objc_check_decl (decl);
5123 rest_of_decl_compilation (decl, NULL, toplevel, 0);
5124 if (! toplevel)
5125 expand_decl (decl);
5128 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5130 /* Finish debugging output for this type. */
5131 rest_of_type_compilation (t, toplevel);
5133 return t;
5136 /* Lay out the type T, and its element type, and so on. */
5138 static void
5139 layout_array_type (tree t)
5141 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5142 layout_array_type (TREE_TYPE (t));
5143 layout_type (t);
5146 /* Begin compiling the definition of an enumeration type.
5147 NAME is its name (or null if anonymous).
5148 Returns the type object, as yet incomplete.
5149 Also records info about it so that build_enumerator
5150 may be used to declare the individual values as they are read. */
5152 tree
5153 start_enum (tree name)
5155 tree enumtype = 0;
5157 /* If this is the real definition for a previous forward reference,
5158 fill in the contents in the same object that used to be the
5159 forward reference. */
5161 if (name != 0)
5162 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5164 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5166 enumtype = make_node (ENUMERAL_TYPE);
5167 pushtag (name, enumtype);
5170 C_TYPE_BEING_DEFINED (enumtype) = 1;
5172 if (TYPE_VALUES (enumtype) != 0)
5174 /* This enum is a named one that has been declared already. */
5175 error ("redeclaration of `enum %s'", IDENTIFIER_POINTER (name));
5177 /* Completely replace its old definition.
5178 The old enumerators remain defined, however. */
5179 TYPE_VALUES (enumtype) = 0;
5182 enum_next_value = integer_zero_node;
5183 enum_overflow = 0;
5185 if (flag_short_enums)
5186 TYPE_PACKED (enumtype) = 1;
5188 return enumtype;
5191 /* After processing and defining all the values of an enumeration type,
5192 install their decls in the enumeration type and finish it off.
5193 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5194 and ATTRIBUTES are the specified attributes.
5195 Returns ENUMTYPE. */
5197 tree
5198 finish_enum (tree enumtype, tree values, tree attributes)
5200 tree pair, tem;
5201 tree minnode = 0, maxnode = 0, enum_value_type;
5202 int precision, unsign;
5203 int toplevel = (global_scope == current_scope);
5205 if (in_parm_level_p ())
5206 warning ("enum defined inside parms");
5208 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5210 /* Calculate the maximum value of any enumerator in this type. */
5212 if (values == error_mark_node)
5213 minnode = maxnode = integer_zero_node;
5214 else
5216 minnode = maxnode = TREE_VALUE (values);
5217 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5219 tree value = TREE_VALUE (pair);
5220 if (tree_int_cst_lt (maxnode, value))
5221 maxnode = value;
5222 if (tree_int_cst_lt (value, minnode))
5223 minnode = value;
5227 /* Construct the final type of this enumeration. It is the same
5228 as one of the integral types - the narrowest one that fits, except
5229 that normally we only go as narrow as int - and signed iff any of
5230 the values are negative. */
5231 unsign = (tree_int_cst_sgn (minnode) >= 0);
5232 precision = MAX (min_precision (minnode, unsign),
5233 min_precision (maxnode, unsign));
5234 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5236 tree narrowest = c_common_type_for_size (precision, unsign);
5237 if (narrowest == 0)
5239 warning ("enumeration values exceed range of largest integer");
5240 narrowest = long_long_integer_type_node;
5243 precision = TYPE_PRECISION (narrowest);
5245 else
5246 precision = TYPE_PRECISION (integer_type_node);
5248 if (precision == TYPE_PRECISION (integer_type_node))
5249 enum_value_type = c_common_type_for_size (precision, 0);
5250 else
5251 enum_value_type = enumtype;
5253 TYPE_MIN_VALUE (enumtype) = minnode;
5254 TYPE_MAX_VALUE (enumtype) = maxnode;
5255 TYPE_PRECISION (enumtype) = precision;
5256 TREE_UNSIGNED (enumtype) = unsign;
5257 TYPE_SIZE (enumtype) = 0;
5258 layout_type (enumtype);
5260 if (values != error_mark_node)
5262 /* Change the type of the enumerators to be the enum type. We
5263 need to do this irrespective of the size of the enum, for
5264 proper type checking. Replace the DECL_INITIALs of the
5265 enumerators, and the value slots of the list, with copies
5266 that have the enum type; they cannot be modified in place
5267 because they may be shared (e.g. integer_zero_node) Finally,
5268 change the purpose slots to point to the names of the decls. */
5269 for (pair = values; pair; pair = TREE_CHAIN (pair))
5271 tree enu = TREE_PURPOSE (pair);
5273 TREE_TYPE (enu) = enumtype;
5275 /* The ISO C Standard mandates enumerators to have type int,
5276 even though the underlying type of an enum type is
5277 unspecified. Here we convert any enumerators that fit in
5278 an int to type int, to avoid promotions to unsigned types
5279 when comparing integers with enumerators that fit in the
5280 int range. When -pedantic is given, build_enumerator()
5281 would have already taken care of those that don't fit. */
5282 if (int_fits_type_p (DECL_INITIAL (enu), enum_value_type))
5283 DECL_INITIAL (enu) = convert (enum_value_type, DECL_INITIAL (enu));
5284 else
5285 DECL_INITIAL (enu) = convert (enumtype, DECL_INITIAL (enu));
5287 TREE_PURPOSE (pair) = DECL_NAME (enu);
5288 TREE_VALUE (pair) = DECL_INITIAL (enu);
5291 TYPE_VALUES (enumtype) = values;
5294 /* Fix up all variant types of this enum type. */
5295 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5297 if (tem == enumtype)
5298 continue;
5299 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5300 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5301 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5302 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5303 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5304 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5305 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5306 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5307 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5308 TREE_UNSIGNED (tem) = TREE_UNSIGNED (enumtype);
5311 /* Finish debugging output for this type. */
5312 rest_of_type_compilation (enumtype, toplevel);
5314 return enumtype;
5317 /* Build and install a CONST_DECL for one value of the
5318 current enumeration type (one that was begun with start_enum).
5319 Return a tree-list containing the CONST_DECL and its value.
5320 Assignment of sequential values by default is handled here. */
5322 tree
5323 build_enumerator (tree name, tree value)
5325 tree decl, type;
5327 /* Validate and default VALUE. */
5329 /* Remove no-op casts from the value. */
5330 if (value)
5331 STRIP_TYPE_NOPS (value);
5333 if (value != 0)
5335 if (TREE_CODE (value) == INTEGER_CST)
5337 value = default_conversion (value);
5338 constant_expression_warning (value);
5340 else
5342 error ("enumerator value for `%s' not integer constant",
5343 IDENTIFIER_POINTER (name));
5344 value = 0;
5348 /* Default based on previous value. */
5349 /* It should no longer be possible to have NON_LVALUE_EXPR
5350 in the default. */
5351 if (value == 0)
5353 value = enum_next_value;
5354 if (enum_overflow)
5355 error ("overflow in enumeration values");
5358 if (pedantic && ! int_fits_type_p (value, integer_type_node))
5360 pedwarn ("ISO C restricts enumerator values to range of `int'");
5361 value = convert (integer_type_node, value);
5364 /* Set basis for default for next value. */
5365 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
5366 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5368 /* Now create a declaration for the enum value name. */
5370 type = TREE_TYPE (value);
5371 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5372 TYPE_PRECISION (integer_type_node)),
5373 (TYPE_PRECISION (type)
5374 >= TYPE_PRECISION (integer_type_node)
5375 && TREE_UNSIGNED (type)));
5377 decl = build_decl (CONST_DECL, name, type);
5378 DECL_INITIAL (decl) = convert (type, value);
5379 pushdecl (decl);
5381 return tree_cons (decl, value, NULL_TREE);
5385 /* Create the FUNCTION_DECL for a function definition.
5386 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5387 the declaration; they describe the function's name and the type it returns,
5388 but twisted together in a fashion that parallels the syntax of C.
5390 This function creates a binding context for the function body
5391 as well as setting up the FUNCTION_DECL in current_function_decl.
5393 Returns 1 on success. If the DECLARATOR is not suitable for a function
5394 (it defines a datum instead), we return 0, which tells
5395 yyparse to report a parse error. */
5398 start_function (tree declspecs, tree declarator, tree attributes)
5400 tree decl1, old_decl;
5401 tree restype;
5402 int old_immediate_size_expand = immediate_size_expand;
5404 current_function_returns_value = 0; /* Assume, until we see it does. */
5405 current_function_returns_null = 0;
5406 current_function_returns_abnormally = 0;
5407 warn_about_return_type = 0;
5408 current_extern_inline = 0;
5410 /* Don't expand any sizes in the return type of the function. */
5411 immediate_size_expand = 0;
5413 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1);
5415 /* If the declarator is not suitable for a function definition,
5416 cause a syntax error. */
5417 if (decl1 == 0)
5419 immediate_size_expand = old_immediate_size_expand;
5420 return 0;
5423 decl_attributes (&decl1, attributes, 0);
5425 if (DECL_DECLARED_INLINE_P (decl1)
5426 && DECL_UNINLINABLE (decl1)
5427 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
5428 warning ("%Jinline function '%D' given attribute noinline", decl1, decl1);
5430 announce_function (decl1);
5432 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
5434 error ("return type is an incomplete type");
5435 /* Make it return void instead. */
5436 TREE_TYPE (decl1)
5437 = build_function_type (void_type_node,
5438 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
5441 if (warn_about_return_type)
5442 pedwarn_c99 ("return type defaults to `int'");
5444 /* Save the parm names or decls from this function's declarator
5445 where store_parm_decls will find them. */
5446 current_function_parms = last_function_parms;
5447 current_function_parm_tags = last_function_parm_tags;
5448 current_function_parm_others = last_function_parm_others;
5450 /* Make the init_value nonzero so pushdecl knows this is not tentative.
5451 error_mark_node is replaced below (in poplevel) with the BLOCK. */
5452 DECL_INITIAL (decl1) = error_mark_node;
5454 /* If this definition isn't a prototype and we had a prototype declaration
5455 before, copy the arg type info from that prototype.
5456 But not if what we had before was a builtin function. */
5457 old_decl = lookup_name_current_level (DECL_NAME (decl1));
5458 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
5459 && !DECL_BUILT_IN (old_decl)
5460 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5461 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (old_decl))))
5462 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
5464 TREE_TYPE (decl1) = TREE_TYPE (old_decl);
5465 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
5468 /* Optionally warn of old-fashioned def with no previous prototype. */
5469 if (warn_strict_prototypes
5470 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
5471 && C_DECL_ISNT_PROTOTYPE (old_decl))
5472 warning ("function declaration isn't a prototype");
5473 /* Optionally warn of any global def with no previous prototype. */
5474 else if (warn_missing_prototypes
5475 && TREE_PUBLIC (decl1)
5476 && ! MAIN_NAME_P (DECL_NAME (decl1))
5477 && C_DECL_ISNT_PROTOTYPE (old_decl))
5478 warning ("%Jno previous prototype for '%D'", decl1, decl1);
5479 /* Optionally warn of any def with no previous prototype
5480 if the function has already been used. */
5481 else if (warn_missing_prototypes
5482 && old_decl != 0 && TREE_USED (old_decl)
5483 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
5484 warning ("%J'%D' was used with no prototype before its definition",
5485 decl1, decl1);
5486 /* Optionally warn of any global def with no previous declaration. */
5487 else if (warn_missing_declarations
5488 && TREE_PUBLIC (decl1)
5489 && old_decl == 0
5490 && ! MAIN_NAME_P (DECL_NAME (decl1)))
5491 warning ("%Jno previous declaration for '%D'", decl1, decl1);
5492 /* Optionally warn of any def with no previous declaration
5493 if the function has already been used. */
5494 else if (warn_missing_declarations
5495 && old_decl != 0 && TREE_USED (old_decl)
5496 && C_DECL_IMPLICIT (old_decl))
5497 warning ("%J`%D' was used with no declaration before its definition",
5498 decl1, decl1);
5500 /* This is a definition, not a reference.
5501 So normally clear DECL_EXTERNAL.
5502 However, `extern inline' acts like a declaration
5503 except for defining how to inline. So set DECL_EXTERNAL in that case. */
5504 DECL_EXTERNAL (decl1) = current_extern_inline;
5506 /* This function exists in static storage.
5507 (This does not mean `static' in the C sense!) */
5508 TREE_STATIC (decl1) = 1;
5510 /* A nested function is not global. */
5511 if (current_function_decl != 0)
5512 TREE_PUBLIC (decl1) = 0;
5514 #ifdef ENABLE_CHECKING
5515 /* This is the earliest point at which we might know the assembler
5516 name of the function. Thus, if it's set before this, die horribly. */
5517 if (DECL_ASSEMBLER_NAME_SET_P (decl1))
5518 abort ();
5519 #endif
5521 /* If #pragma weak was used, mark the decl weak now. */
5522 if (current_scope == global_scope)
5523 maybe_apply_pragma_weak (decl1);
5525 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
5526 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
5528 tree args;
5529 int argct = 0;
5531 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5532 != integer_type_node)
5533 pedwarn ("%Jreturn type of '%D' is not `int'", decl1, decl1);
5535 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
5536 args = TREE_CHAIN (args))
5538 tree type = args ? TREE_VALUE (args) : 0;
5540 if (type == void_type_node)
5541 break;
5543 ++argct;
5544 switch (argct)
5546 case 1:
5547 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
5548 pedwarn ("%Jfirst argument of '%D' should be `int'",
5549 decl1, decl1);
5550 break;
5552 case 2:
5553 if (TREE_CODE (type) != POINTER_TYPE
5554 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5555 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5556 != char_type_node))
5557 pedwarn ("%Jsecond argument of '%D' should be 'char **'",
5558 decl1, decl1);
5559 break;
5561 case 3:
5562 if (TREE_CODE (type) != POINTER_TYPE
5563 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5564 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5565 != char_type_node))
5566 pedwarn ("%Jthird argument of '%D' should probably be "
5567 "'char **'", decl1, decl1);
5568 break;
5572 /* It is intentional that this message does not mention the third
5573 argument because it's only mentioned in an appendix of the
5574 standard. */
5575 if (argct > 0 && (argct < 2 || argct > 3))
5576 pedwarn ("%J'%D' takes only zero or two arguments", decl1, decl1);
5578 if (! TREE_PUBLIC (decl1))
5579 pedwarn ("%J'%D' is normally a non-static function", decl1, decl1);
5582 /* Record the decl so that the function name is defined.
5583 If we already have a decl for this name, and it is a FUNCTION_DECL,
5584 use the old decl. */
5586 current_function_decl = pushdecl (decl1);
5588 pushlevel (0);
5589 declare_parm_level ();
5591 make_decl_rtl (current_function_decl, NULL);
5593 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
5594 /* Promote the value to int before returning it. */
5595 if (c_promoting_integer_type_p (restype))
5597 /* It retains unsignedness if not really getting wider. */
5598 if (TREE_UNSIGNED (restype)
5599 && (TYPE_PRECISION (restype)
5600 == TYPE_PRECISION (integer_type_node)))
5601 restype = unsigned_type_node;
5602 else
5603 restype = integer_type_node;
5605 DECL_RESULT (current_function_decl)
5606 = build_decl (RESULT_DECL, NULL_TREE, restype);
5608 /* If this fcn was already referenced via a block-scope `extern' decl
5609 (or an implicit decl), propagate certain information about the usage. */
5610 if (TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (current_function_decl)))
5611 TREE_ADDRESSABLE (current_function_decl) = 1;
5613 immediate_size_expand = old_immediate_size_expand;
5615 start_fname_decls ();
5617 return 1;
5620 /* Subroutine of store_parm_decls which handles new-style function
5621 definitions (prototype format). The parms already have decls, so we
5622 need only record them as in effect and complain if any redundant
5623 old-style parm decls were written. */
5624 static void
5625 store_parm_decls_newstyle (void)
5627 tree decl, last;
5628 tree fndecl = current_function_decl;
5629 tree parms = current_function_parms;
5630 tree tags = current_function_parm_tags;
5631 tree others = current_function_parm_others;
5633 if (current_scope->parms || current_scope->names || current_scope->tags)
5635 error ("%Jold-style parameter declarations in prototyped "
5636 "function definition", fndecl);
5638 /* Get rid of the old-style declarations. */
5639 poplevel (0, 0, 0);
5640 pushlevel (0);
5643 /* Now make all the parameter declarations visible in the function body.
5644 We can bypass most of the grunt work of pushdecl. */
5645 for (last = 0, decl = parms; decl; last = decl, decl = TREE_CHAIN (decl))
5647 DECL_CONTEXT (decl) = current_function_decl;
5648 if (DECL_NAME (decl) == 0)
5649 error ("%Jparameter name omitted", decl);
5650 else
5652 if (IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)))
5653 current_scope->shadowed
5654 = tree_cons (DECL_NAME (decl),
5655 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)),
5656 current_scope->shadowed);
5657 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)) = decl;
5660 current_scope->parms = parms;
5661 current_scope->parms_last = last;
5663 /* Record the parameter list in the function declaration. */
5664 DECL_ARGUMENTS (fndecl) = parms;
5666 /* Now make all the ancillary declarations visible, likewise. */
5667 for (last = 0, decl = others; decl; last = decl, decl = TREE_CHAIN (decl))
5669 DECL_CONTEXT (decl) = current_function_decl;
5670 if (DECL_NAME (decl)
5671 && TYPE_MAIN_VARIANT (TREE_TYPE (decl)) != void_type_node)
5673 if (IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)))
5674 current_scope->shadowed
5675 = tree_cons (DECL_NAME (decl),
5676 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)),
5677 current_scope->shadowed);
5678 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)) = decl;
5681 current_scope->names = others;
5682 current_scope->names_last = last;
5684 /* And all the tag declarations. */
5685 for (decl = tags; decl; decl = TREE_CHAIN (decl))
5686 if (TREE_PURPOSE (decl))
5688 if (IDENTIFIER_TAG_VALUE (TREE_PURPOSE (decl)))
5689 current_scope->shadowed_tags
5690 = tree_cons (TREE_PURPOSE (decl),
5691 IDENTIFIER_SYMBOL_VALUE (TREE_PURPOSE (decl)),
5692 current_scope->shadowed_tags);
5693 IDENTIFIER_TAG_VALUE (TREE_PURPOSE (decl)) = TREE_VALUE (decl);
5695 current_scope->tags = tags;
5698 /* Subroutine of store_parm_decls which handles old-style function
5699 definitions (separate parameter list and declarations). */
5701 static void
5702 store_parm_decls_oldstyle (void)
5704 tree parm, decl, last;
5705 tree fndecl = current_function_decl;
5707 /* This is the identifier list from the function declarator. */
5708 tree parmids = current_function_parms;
5710 /* We use DECL_WEAK as a flag to show which parameters have been
5711 seen already, since it is not used on PARM_DECL. */
5712 #ifdef ENABLE_CHECKING
5713 for (parm = current_scope->parms; parm; parm = TREE_CHAIN (parm))
5714 if (DECL_WEAK (parm))
5715 abort ();
5716 #endif
5718 /* Match each formal parameter name with its declaration. Save each
5719 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
5720 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
5722 if (TREE_VALUE (parm) == 0)
5724 error ("%Jparameter name missing from parameter list", fndecl);
5725 TREE_PURPOSE (parm) = 0;
5726 continue;
5729 decl = IDENTIFIER_SYMBOL_VALUE (TREE_VALUE (parm));
5730 if (decl && DECL_CONTEXT (decl) == fndecl)
5732 /* If we got something other than a PARM_DECL it is an error. */
5733 if (TREE_CODE (decl) != PARM_DECL)
5734 error ("%J\"%D\" declared as a non-parameter", decl, decl);
5735 /* If the declaration is already marked, we have a duplicate
5736 name. Complain and ignore the duplicate. */
5737 else if (DECL_WEAK (decl))
5739 error ("%Jmultiple parameters named \"%D\"", decl, decl);
5740 TREE_PURPOSE (parm) = 0;
5741 continue;
5743 /* If the declaration says "void", complain and turn it into
5744 an int. */
5745 else if (VOID_TYPE_P (TREE_TYPE (decl)))
5747 error ("%Jparameter \"%D\" declared void", decl, decl);
5748 TREE_TYPE (decl) = integer_type_node;
5749 DECL_ARG_TYPE (decl) = integer_type_node;
5750 layout_decl (decl, 0);
5753 /* If no declaration found, default to int. */
5754 else
5756 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
5757 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
5758 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
5759 pushdecl (decl);
5761 if (flag_isoc99)
5762 pedwarn ("%Jtype of \"%D\" defaults to \"int\"", decl, decl);
5763 else if (extra_warnings)
5764 warning ("%Jtype of \"%D\" defaults to \"int\"", decl, decl);
5767 TREE_PURPOSE (parm) = decl;
5768 DECL_WEAK (decl) = 1;
5771 /* Now examine the parms chain for incomplete declarations
5772 and declarations with no corresponding names. */
5774 for (parm = current_scope->parms; parm; parm = TREE_CHAIN (parm))
5776 if (!COMPLETE_TYPE_P (TREE_TYPE (parm)))
5778 error ("%Jparameter \"%D\" has incomplete type", parm, parm);
5779 TREE_TYPE (parm) = error_mark_node;
5782 if (! DECL_WEAK (parm))
5784 error ("%Jdeclaration for parameter \"%D\" but no such parameter",
5785 parm, parm);
5787 /* Pretend the parameter was not missing.
5788 This gets us to a standard state and minimizes
5789 further error messages. */
5790 parmids = chainon (parmids, tree_cons (parm, 0, 0));
5794 /* Chain the declarations together in the order of the list of
5795 names. Store that chain in the function decl, replacing the
5796 list of names. Update the current scope to match. */
5797 DECL_ARGUMENTS (fndecl) = 0;
5799 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
5800 if (TREE_PURPOSE (parm))
5801 break;
5802 if (parm && TREE_PURPOSE (parm))
5804 last = TREE_PURPOSE (parm);
5805 DECL_ARGUMENTS (fndecl) = last;
5806 current_scope->parms = last;
5807 DECL_WEAK (last) = 0;
5809 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
5810 if (TREE_PURPOSE (parm))
5812 TREE_CHAIN (last) = TREE_PURPOSE (parm);
5813 last = TREE_PURPOSE (parm);
5814 DECL_WEAK (last) = 0;
5816 current_scope->parms_last = last;
5817 TREE_CHAIN (last) = 0;
5820 /* If there was a previous prototype,
5821 set the DECL_ARG_TYPE of each argument according to
5822 the type previously specified, and report any mismatches. */
5824 if (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
5826 tree type;
5827 for (parm = DECL_ARGUMENTS (fndecl),
5828 type = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
5829 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
5830 != void_type_node));
5831 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
5833 if (parm == 0 || type == 0
5834 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
5836 error ("number of arguments doesn't match prototype");
5837 error ("%Hprototype declaration",
5838 &current_function_prototype_locus);
5839 break;
5841 /* Type for passing arg must be consistent with that
5842 declared for the arg. ISO C says we take the unqualified
5843 type for parameters declared with qualified type. */
5844 if (! comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
5845 TYPE_MAIN_VARIANT (TREE_VALUE (type)),
5846 COMPARE_STRICT))
5848 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
5849 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
5851 /* Adjust argument to match prototype. E.g. a previous
5852 `int foo(float);' prototype causes
5853 `int foo(x) float x; {...}' to be treated like
5854 `int foo(float x) {...}'. This is particularly
5855 useful for argument types like uid_t. */
5856 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
5858 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
5859 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
5860 && TYPE_PRECISION (TREE_TYPE (parm))
5861 < TYPE_PRECISION (integer_type_node))
5862 DECL_ARG_TYPE (parm) = integer_type_node;
5864 if (pedantic)
5866 pedwarn ("promoted argument \"%D\" "
5867 "doesn't match prototype", parm);
5868 pedwarn ("%Hprototype declaration",
5869 &current_function_prototype_locus);
5872 else
5874 error ("argument \"%D\" doesn't match prototype", parm);
5875 error ("%Hprototype declaration",
5876 &current_function_prototype_locus);
5880 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
5883 /* Otherwise, create a prototype that would match. */
5885 else
5887 tree actual = 0, last = 0, type;
5889 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
5891 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
5892 if (last)
5893 TREE_CHAIN (last) = type;
5894 else
5895 actual = type;
5896 last = type;
5898 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
5899 if (last)
5900 TREE_CHAIN (last) = type;
5901 else
5902 actual = type;
5904 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
5905 of the type of this function, but we need to avoid having this
5906 affect the types of other similarly-typed functions, so we must
5907 first force the generation of an identical (but separate) type
5908 node for the relevant function type. The new node we create
5909 will be a variant of the main variant of the original function
5910 type. */
5912 TREE_TYPE (fndecl) = build_type_copy (TREE_TYPE (fndecl));
5914 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
5918 /* Store the parameter declarations into the current function declaration.
5919 This is called after parsing the parameter declarations, before
5920 digesting the body of the function.
5922 For an old-style definition, construct a prototype out of the old-style
5923 parameter declarations and inject it into the function's type. */
5925 void
5926 store_parm_decls (void)
5928 tree fndecl = current_function_decl;
5930 /* The function containing FNDECL, if any. */
5931 tree context = decl_function_context (fndecl);
5933 /* True if this definition is written with a prototype. */
5934 bool prototype = (current_function_parms
5935 && TREE_CODE (current_function_parms) != TREE_LIST);
5937 if (prototype)
5938 store_parm_decls_newstyle ();
5939 else
5940 store_parm_decls_oldstyle ();
5942 /* The next call to pushlevel will be a function body. */
5944 next_is_function_body = true;
5946 /* Write a record describing this function definition to the prototypes
5947 file (if requested). */
5949 gen_aux_info_record (fndecl, 1, 0, prototype);
5951 /* Initialize the RTL code for the function. */
5952 allocate_struct_function (fndecl);
5954 /* Begin the statement tree for this function. */
5955 begin_stmt_tree (&DECL_SAVED_TREE (fndecl));
5957 /* If this is a nested function, save away the sizes of any
5958 variable-size types so that we can expand them when generating
5959 RTL. */
5960 if (context)
5962 tree t;
5964 DECL_LANG_SPECIFIC (fndecl)->pending_sizes
5965 = nreverse (get_pending_sizes ());
5966 for (t = DECL_LANG_SPECIFIC (fndecl)->pending_sizes;
5968 t = TREE_CHAIN (t))
5969 SAVE_EXPR_CONTEXT (TREE_VALUE (t)) = context;
5972 /* This function is being processed in whole-function mode. */
5973 cfun->x_whole_function_mode_p = 1;
5975 /* Even though we're inside a function body, we still don't want to
5976 call expand_expr to calculate the size of a variable-sized array.
5977 We haven't necessarily assigned RTL to all variables yet, so it's
5978 not safe to try to expand expressions involving them. */
5979 immediate_size_expand = 0;
5980 cfun->x_dont_save_pending_sizes_p = 1;
5983 /* Finish up a function declaration and compile that function
5984 all the way to assembler language output. The free the storage
5985 for the function definition.
5987 This is called after parsing the body of the function definition. */
5989 void
5990 finish_function ()
5992 tree fndecl = current_function_decl;
5994 /* When a function declaration is totally empty, e.g.
5995 void foo(void) { }
5996 (the argument list is irrelevant) the compstmt rule will not
5997 bother calling pushlevel/poplevel, which means we get here with
5998 the scope stack out of sync. Detect this situation by noticing
5999 that current_scope is still as store_parm_decls left it, and do
6000 a dummy push/pop to get back to consistency.
6001 Note that the call to pushlevel does not actually push another
6002 scope - see there for details. */
6004 if (current_scope->parm_flag && next_is_function_body)
6006 pushlevel (0);
6007 poplevel (0, 0, 0);
6010 if (TREE_CODE (fndecl) == FUNCTION_DECL
6011 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6013 tree args = DECL_ARGUMENTS (fndecl);
6014 for (; args; args = TREE_CHAIN (args))
6016 tree type = TREE_TYPE (args);
6017 if (INTEGRAL_TYPE_P (type)
6018 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6019 DECL_ARG_TYPE (args) = integer_type_node;
6023 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6025 /* Must mark the RESULT_DECL as being in this function. */
6027 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6029 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6031 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6032 != integer_type_node)
6034 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6035 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6036 if (!warn_main)
6037 pedwarn ("%Jreturn type of '%D' is not `int'", fndecl, fndecl);
6039 else
6041 #ifdef DEFAULT_MAIN_RETURN
6042 /* Make it so that `main' always returns success by default. */
6043 DEFAULT_MAIN_RETURN;
6044 #else
6045 if (flag_isoc99)
6046 c_expand_return (integer_zero_node);
6047 #endif
6051 finish_fname_decls ();
6053 /* Tie off the statement tree for this function. */
6054 finish_stmt_tree (&DECL_SAVED_TREE (fndecl));
6056 /* Complain if there's just no return statement. */
6057 if (warn_return_type
6058 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6059 && !current_function_returns_value && !current_function_returns_null
6060 /* Don't complain if we abort. */
6061 && !current_function_returns_abnormally
6062 /* Don't warn for main(). */
6063 && !MAIN_NAME_P (DECL_NAME (fndecl))
6064 /* Or if they didn't actually specify a return type. */
6065 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6066 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6067 inline function, as we might never be compiled separately. */
6068 && DECL_INLINE (fndecl))
6069 warning ("no return statement in function returning non-void");
6071 /* With just -Wextra, complain only if function returns both with
6072 and without a value. */
6073 if (extra_warnings
6074 && current_function_returns_value
6075 && current_function_returns_null)
6076 warning ("this function may return with or without a value");
6078 /* We're leaving the context of this function, so zap cfun. It's still in
6079 DECL_SAVED_INSNS, and we'll restore it in tree_rest_of_compilation. */
6080 cfun = NULL;
6082 /* ??? Objc emits functions after finalizing the compilation unit.
6083 This should be cleaned up later and this conditional removed. */
6084 if (!cgraph_global_info_ready)
6085 cgraph_finalize_function (fndecl, false);
6086 else
6087 c_expand_body (fndecl);
6088 current_function_decl = NULL;
6091 /* Generate the RTL for a deferred function FNDECL. */
6093 void
6094 c_expand_deferred_function (tree fndecl)
6096 /* DECL_INLINE or DECL_RESULT might got cleared after the inline
6097 function was deferred, e.g. in duplicate_decls. */
6098 if (DECL_INLINE (fndecl) && DECL_RESULT (fndecl))
6100 if (flag_inline_trees)
6102 timevar_push (TV_INTEGRATION);
6103 optimize_inline_calls (fndecl);
6104 timevar_pop (TV_INTEGRATION);
6106 c_expand_body (fndecl);
6107 current_function_decl = NULL;
6111 /* Generate the RTL for the body of FNDECL. If NESTED_P is nonzero,
6112 then we are already in the process of generating RTL for another
6113 function. */
6115 static void
6116 c_expand_body_1 (tree fndecl, int nested_p)
6118 if (nested_p)
6120 /* Make sure that we will evaluate variable-sized types involved
6121 in our function's type. */
6122 expand_pending_sizes (DECL_LANG_SPECIFIC (fndecl)->pending_sizes);
6124 /* Squirrel away our current state. */
6125 push_function_context ();
6128 tree_rest_of_compilation (fndecl, nested_p);
6130 if (nested_p)
6131 /* Return to the enclosing function. */
6132 pop_function_context ();
6134 if (DECL_STATIC_CONSTRUCTOR (fndecl))
6136 if (targetm.have_ctors_dtors)
6137 (* targetm.asm_out.constructor) (XEXP (DECL_RTL (fndecl), 0),
6138 DEFAULT_INIT_PRIORITY);
6139 else
6140 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6143 if (DECL_STATIC_DESTRUCTOR (fndecl))
6145 if (targetm.have_ctors_dtors)
6146 (* targetm.asm_out.destructor) (XEXP (DECL_RTL (fndecl), 0),
6147 DEFAULT_INIT_PRIORITY);
6148 else
6149 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6153 /* Like c_expand_body_1 but only for unnested functions. */
6155 void
6156 c_expand_body (tree fndecl)
6158 c_expand_body_1 (fndecl, 0);
6161 /* Check the declarations given in a for-loop for satisfying the C99
6162 constraints. */
6163 void
6164 check_for_loop_decls (void)
6166 tree t;
6168 if (!flag_isoc99)
6170 /* If we get here, declarations have been used in a for loop without
6171 the C99 for loop scope. This doesn't make much sense, so don't
6172 allow it. */
6173 error ("'for' loop initial declaration used outside C99 mode");
6174 return;
6176 /* C99 subclause 6.8.5 paragraph 3:
6178 [#3] The declaration part of a for statement shall only
6179 declare identifiers for objects having storage class auto or
6180 register.
6182 It isn't clear whether, in this sentence, "identifiers" binds to
6183 "shall only declare" or to "objects" - that is, whether all identifiers
6184 declared must be identifiers for objects, or whether the restriction
6185 only applies to those that are. (A question on this in comp.std.c
6186 in November 2000 received no answer.) We implement the strictest
6187 interpretation, to avoid creating an extension which later causes
6188 problems. */
6190 for (t = current_scope->tags; t; t = TREE_CHAIN (t))
6192 if (TREE_PURPOSE (t) != 0)
6194 enum tree_code code = TREE_CODE (TREE_VALUE (t));
6196 if (code == RECORD_TYPE)
6197 error ("'struct %s' declared in 'for' loop initial declaration",
6198 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6199 else if (code == UNION_TYPE)
6200 error ("'union %s' declared in 'for' loop initial declaration",
6201 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6202 else
6203 error ("'enum %s' declared in 'for' loop initial declaration",
6204 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6208 for (t = getdecls (); t; t = TREE_CHAIN (t))
6210 if (TREE_CODE (t) != VAR_DECL && DECL_NAME (t))
6211 error ("%Jdeclaration of non-variable '%D' in 'for' loop "
6212 "initial declaration", t, t);
6213 else if (TREE_STATIC (t))
6214 error ("%Jdeclaration of static variable '%D' in 'for' loop "
6215 "initial declaration", t, t);
6216 else if (DECL_EXTERNAL (t))
6217 error ("%Jdeclaration of 'extern' variable '%D' in 'for' loop "
6218 "initial declaration", t, t);
6222 /* Save and restore the variables in this file and elsewhere
6223 that keep track of the progress of compilation of the current function.
6224 Used for nested functions. */
6226 struct language_function GTY(())
6228 struct c_language_function base;
6229 int returns_value;
6230 int returns_null;
6231 int returns_abnormally;
6232 int warn_about_return_type;
6233 int extern_inline;
6236 /* Save and reinitialize the variables
6237 used during compilation of a C function. */
6239 void
6240 c_push_function_context (struct function *f)
6242 struct language_function *p;
6243 p = ggc_alloc (sizeof (struct language_function));
6244 f->language = p;
6246 p->base.x_stmt_tree = c_stmt_tree;
6247 p->base.x_scope_stmt_stack = c_scope_stmt_stack;
6248 p->returns_value = current_function_returns_value;
6249 p->returns_null = current_function_returns_null;
6250 p->returns_abnormally = current_function_returns_abnormally;
6251 p->warn_about_return_type = warn_about_return_type;
6252 p->extern_inline = current_extern_inline;
6255 /* Restore the variables used during compilation of a C function. */
6257 void
6258 c_pop_function_context (struct function *f)
6260 struct language_function *p = f->language;
6262 if (DECL_SAVED_INSNS (current_function_decl) == 0
6263 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6265 /* Stop pointing to the local nodes about to be freed. */
6266 /* But DECL_INITIAL must remain nonzero so we know this
6267 was an actual function definition. */
6268 DECL_INITIAL (current_function_decl) = error_mark_node;
6269 DECL_ARGUMENTS (current_function_decl) = 0;
6272 c_stmt_tree = p->base.x_stmt_tree;
6273 c_scope_stmt_stack = p->base.x_scope_stmt_stack;
6274 current_function_returns_value = p->returns_value;
6275 current_function_returns_null = p->returns_null;
6276 current_function_returns_abnormally = p->returns_abnormally;
6277 warn_about_return_type = p->warn_about_return_type;
6278 current_extern_inline = p->extern_inline;
6280 f->language = NULL;
6283 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6285 void
6286 c_dup_lang_specific_decl (tree decl)
6288 struct lang_decl *ld;
6290 if (!DECL_LANG_SPECIFIC (decl))
6291 return;
6293 ld = ggc_alloc (sizeof (struct lang_decl));
6294 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6295 DECL_LANG_SPECIFIC (decl) = ld;
6298 /* The functions below are required for functionality of doing
6299 function at once processing in the C front end. Currently these
6300 functions are not called from anywhere in the C front end, but as
6301 these changes continue, that will change. */
6303 /* Returns nonzero if the current statement is a full expression,
6304 i.e. temporaries created during that statement should be destroyed
6305 at the end of the statement. */
6308 stmts_are_full_exprs_p (void)
6310 return 0;
6313 /* Returns the stmt_tree (if any) to which statements are currently
6314 being added. If there is no active statement-tree, NULL is
6315 returned. */
6317 stmt_tree
6318 current_stmt_tree (void)
6320 return &c_stmt_tree;
6323 /* Returns the stack of SCOPE_STMTs for the current function. */
6325 tree *
6326 current_scope_stmt_stack (void)
6328 return &c_scope_stmt_stack;
6331 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6332 C. */
6335 anon_aggr_type_p (tree node ATTRIBUTE_UNUSED)
6337 return 0;
6340 /* Dummy function in place of callback used by C++. */
6342 void
6343 extract_interface_info (void)
6347 /* Return a new COMPOUND_STMT, after adding it to the current
6348 statement tree. */
6350 tree
6351 c_begin_compound_stmt (void)
6353 tree stmt;
6355 /* Create the COMPOUND_STMT. */
6356 stmt = add_stmt (build_stmt (COMPOUND_STMT, NULL_TREE));
6358 return stmt;
6361 /* Expand T (a DECL_STMT) if it declares an entity not handled by the
6362 common code. */
6364 void
6365 c_expand_decl_stmt (tree t)
6367 tree decl = DECL_STMT_DECL (t);
6369 /* Expand nested functions. */
6370 if (TREE_CODE (decl) == FUNCTION_DECL
6371 && DECL_CONTEXT (decl) == current_function_decl
6372 && DECL_SAVED_TREE (decl))
6373 c_expand_body_1 (decl, 1);
6376 /* Return the global value of T as a symbol. */
6378 tree
6379 identifier_global_value (tree t)
6381 tree decl = IDENTIFIER_SYMBOL_VALUE (t);
6382 if (decl == 0 || DECL_FILE_SCOPE_P (decl))
6383 return decl;
6385 /* Shadowed by something else; find the true global value. */
6386 for (decl = global_scope->names; decl; decl = TREE_CHAIN (decl))
6387 if (DECL_NAME (decl) == t)
6388 return decl;
6390 /* Only local values for this decl. */
6391 return 0;
6394 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6395 otherwise the name is found in ridpointers from RID_INDEX. */
6397 void
6398 record_builtin_type (enum rid rid_index, const char *name, tree type)
6400 tree id;
6401 if (name == 0)
6402 id = ridpointers[(int) rid_index];
6403 else
6404 id = get_identifier (name);
6405 pushdecl (build_decl (TYPE_DECL, id, type));
6408 /* Build the void_list_node (void_type_node having been created). */
6409 tree
6410 build_void_list_node (void)
6412 tree t = build_tree_list (NULL_TREE, void_type_node);
6413 return t;
6416 /* Return something to represent absolute declarators containing a *.
6417 TARGET is the absolute declarator that the * contains.
6418 TYPE_QUALS_ATTRS is a list of modifiers such as const or volatile
6419 to apply to the pointer type, represented as identifiers, possible mixed
6420 with attributes.
6422 We return an INDIRECT_REF whose "contents" are TARGET (inside a TREE_LIST,
6423 if attributes are present) and whose type is the modifier list. */
6425 tree
6426 make_pointer_declarator (tree type_quals_attrs, tree target)
6428 tree quals, attrs;
6429 tree itarget = target;
6430 split_specs_attrs (type_quals_attrs, &quals, &attrs);
6431 if (attrs != NULL_TREE)
6432 itarget = tree_cons (attrs, target, NULL_TREE);
6433 return build1 (INDIRECT_REF, quals, itarget);
6436 /* A wrapper around lhd_set_decl_assembler_name that gives static
6437 variables their C names if they are at file scope and only one
6438 translation unit is being compiled, for backwards compatibility
6439 with certain bizarre assembler hacks (like crtstuff.c). */
6441 void
6442 c_static_assembler_name (tree decl)
6444 if (num_in_fnames == 1
6445 && !TREE_PUBLIC (decl) && DECL_CONTEXT (decl)
6446 && TREE_CODE (DECL_CONTEXT (decl)) == TRANSLATION_UNIT_DECL)
6447 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
6448 else
6449 lhd_set_decl_assembler_name (decl);
6452 /* Hash and equality functions for link_hash_table: key off
6453 DECL_ASSEMBLER_NAME. */
6455 static hashval_t
6456 link_hash_hash (const void *x_p)
6458 tree x = (tree)x_p;
6459 return (hashval_t) (long)DECL_ASSEMBLER_NAME (x);
6462 static int
6463 link_hash_eq (const void *x1_p, const void *x2_p)
6465 tree x1 = (tree)x1_p;
6466 tree x2 = (tree)x2_p;
6467 return DECL_ASSEMBLER_NAME (x1) == DECL_ASSEMBLER_NAME (x2);
6470 /* Propagate information between definitions and uses between multiple
6471 translation units in TU_LIST based on linkage rules. */
6473 void
6474 merge_translation_unit_decls (void)
6476 const tree tu_list = current_file_decl;
6477 tree tu;
6478 tree decl;
6479 htab_t link_hash_table;
6480 tree block;
6482 /* Create the BLOCK that poplevel would have created, but don't
6483 actually call poplevel since that's expensive. */
6484 block = make_node (BLOCK);
6485 BLOCK_VARS (block) = current_scope->names;
6486 TREE_USED (block) = 1;
6487 DECL_INITIAL (current_file_decl) = block;
6489 /* If only one translation unit seen, no copying necessary. */
6490 if (TREE_CHAIN (tu_list) == NULL_TREE)
6491 return;
6493 link_hash_table = htab_create (1021, link_hash_hash, link_hash_eq, NULL);
6495 /* Enter any actual definitions into the hash table. */
6496 for (tu = tu_list; tu; tu = TREE_CHAIN (tu))
6497 for (decl = BLOCK_VARS (DECL_INITIAL (tu)); decl; decl = TREE_CHAIN (decl))
6498 if (TREE_PUBLIC (decl) && ! DECL_EXTERNAL (decl))
6500 PTR *slot;
6501 slot = htab_find_slot (link_hash_table, decl, INSERT);
6503 /* If we've already got a definition, work out which one is
6504 the real one, put it into the hash table, and make the
6505 other one DECL_EXTERNAL. This is important to avoid
6506 putting out two definitions of the same symbol in the
6507 assembly output. */
6508 if (*slot != NULL)
6510 tree old_decl = (tree) *slot;
6512 /* If this is weak or common or whatever, suppress it
6513 in favor of the other definition. */
6514 if (DECL_WEAK (decl))
6515 DECL_EXTERNAL (decl) = 1;
6516 else if (DECL_WEAK (old_decl) && ! DECL_WEAK (decl))
6517 DECL_EXTERNAL (old_decl) = 1;
6518 else if (DECL_COMMON (decl) || DECL_ONE_ONLY (decl))
6519 DECL_EXTERNAL (decl) = 1;
6520 else if (DECL_COMMON (old_decl) || DECL_ONE_ONLY (old_decl))
6521 DECL_EXTERNAL (old_decl) = 1;
6523 if (DECL_EXTERNAL (decl))
6525 DECL_INITIAL (decl) = NULL_TREE;
6526 DECL_COMMON (decl) = 0;
6527 DECL_ONE_ONLY (decl) = 0;
6528 DECL_WEAK (decl) = 0;
6530 else if (DECL_EXTERNAL (old_decl))
6532 DECL_INITIAL (old_decl) = NULL_TREE;
6533 DECL_COMMON (old_decl) = 0;
6534 DECL_ONE_ONLY (old_decl) = 0;
6535 DECL_WEAK (old_decl) = 0;
6536 *slot = decl;
6538 else
6540 error ("%Jredefinition of global '%D'", decl, decl);
6541 error ("%J'%D' previously defined here", old_decl, old_decl);
6544 else
6545 *slot = decl;
6548 /* Now insert the desired information from all the definitions
6549 into any plain declarations. */
6550 for (tu = tu_list; tu; tu = TREE_CHAIN (tu))
6551 for (decl = BLOCK_VARS (DECL_INITIAL (tu)); decl; decl = TREE_CHAIN (decl))
6552 if (TREE_PUBLIC (decl) && DECL_EXTERNAL (decl))
6554 tree global_decl;
6555 global_decl = htab_find (link_hash_table, decl);
6557 if (! global_decl)
6558 continue;
6560 /* Print any appropriate error messages, and partially merge
6561 the decls. */
6562 (void) duplicate_decls (decl, global_decl, true, true);
6565 htab_delete (link_hash_table);
6568 /* Perform final processing on file-scope data. */
6570 void
6571 c_write_global_declarations(void)
6573 tree link;
6575 for (link = current_file_decl; link; link = TREE_CHAIN (link))
6577 tree globals = BLOCK_VARS (DECL_INITIAL (link));
6578 int len = list_length (globals);
6579 tree *vec = xmalloc (sizeof (tree) * len);
6580 int i;
6581 tree decl;
6583 /* Process the decls in the order they were written. */
6585 for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
6586 vec[i] = decl;
6588 wrapup_global_declarations (vec, len);
6590 check_global_declarations (vec, len);
6592 /* Clean up. */
6593 free (vec);
6597 /* Reset the parser's state in preparation for a new file. */
6599 void
6600 c_reset_state (void)
6602 tree link;
6603 tree file_scope_decl;
6605 /* Pop the global scope. */
6606 if (current_scope != global_scope)
6607 current_scope = global_scope;
6608 file_scope_decl = current_file_decl;
6609 DECL_INITIAL (file_scope_decl) = poplevel (1, 0, 0);
6610 BLOCK_SUPERCONTEXT (DECL_INITIAL (file_scope_decl)) = file_scope_decl;
6611 truly_local_externals = NULL_TREE;
6613 /* Start a new global binding level. */
6614 pushlevel (0);
6615 global_scope = current_scope;
6616 current_file_decl = build_decl (TRANSLATION_UNIT_DECL, NULL, NULL);
6617 TREE_CHAIN (current_file_decl) = file_scope_decl;
6619 /* Reintroduce the builtin declarations. */
6620 for (link = first_builtin_decl;
6621 link != TREE_CHAIN (last_builtin_decl);
6622 link = TREE_CHAIN (link))
6623 pushdecl (copy_node (link));
6626 #include "gt-c-decl.h"