Makefile.in (stmp-docobjdir): New target; ensure $docobjdir exists.
[official-gcc.git] / gcc / c-decl.c
blob6a87c427295db56324bb9a9772c5d32816e9ce72
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 "timevar.h"
49 #include "c-common.h"
50 #include "c-pragma.h"
51 #include "cgraph.h"
52 #include "hashtab.h"
53 #include "libfuncs.h"
54 #include "except.h"
55 #include "langhooks-def.h"
57 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
58 enum decl_context
59 { NORMAL, /* Ordinary declaration */
60 FUNCDEF, /* Function definition */
61 PARM, /* Declaration of parm before function body */
62 FIELD, /* Declaration inside struct or union */
63 BITFIELD, /* Likewise but with specified width */
64 TYPENAME}; /* Typename (inside cast or sizeof) */
67 /* Nonzero if we have seen an invalid cross reference
68 to a struct, union, or enum, but not yet printed the message. */
70 tree pending_invalid_xref;
71 /* File and line to appear in the eventual error message. */
72 location_t pending_invalid_xref_location;
74 /* While defining an enum type, this is 1 plus the last enumerator
75 constant value. Note that will do not have to save this or `enum_overflow'
76 around nested function definition since such a definition could only
77 occur in an enum value expression and we don't use these variables in
78 that case. */
80 static tree enum_next_value;
82 /* Nonzero means that there was overflow computing enum_next_value. */
84 static int enum_overflow;
86 /* Parsing a function declarator leaves a list of parameter names
87 or a chain of parameter decls here. */
89 static tree last_function_parms;
91 /* ... and a chain of structure and enum types declared in the
92 parmlist here. */
94 static tree last_function_parm_tags;
96 /* After parsing the declarator that starts a function definition,
97 `start_function' puts the list of parameter names or chain of decls here
98 for `store_parm_decls' to find. */
100 static tree current_function_parms;
102 /* Similar, for last_function_parm_tags. */
104 static tree current_function_parm_tags;
106 /* Similar, for the file and line that the prototype came from if this is
107 an old-style definition. */
109 static location_t current_function_prototype_locus;
111 /* The current statement tree. */
113 static GTY(()) struct stmt_tree_s c_stmt_tree;
115 /* The current scope statement stack. */
117 static GTY(()) tree c_scope_stmt_stack;
119 /* A list of external DECLs that appeared at block scope when there was
120 some other global meaning for that identifier. */
121 static GTY(()) tree truly_local_externals;
123 /* A list of the builtin file-scope DECLs. */
125 static GTY(()) tree builtin_decls;
127 /* A DECL for the current file-scope context. */
129 static GTY(()) tree current_file_decl;
131 /* Set to 0 at beginning of a function definition, set to 1 if
132 a return statement that specifies a return value is seen. */
134 int current_function_returns_value;
136 /* Set to 0 at beginning of a function definition, set to 1 if
137 a return statement with no argument is seen. */
139 int current_function_returns_null;
141 /* Set to 0 at beginning of a function definition, set to 1 if
142 a call to a noreturn function is seen. */
144 int current_function_returns_abnormally;
146 /* Set to nonzero by `grokdeclarator' for a function
147 whose return type is defaulted, if warnings for this are desired. */
149 static int warn_about_return_type;
151 /* Nonzero when starting a function declared `extern inline'. */
153 static int current_extern_inline;
155 /* For each binding contour we allocate a c_scope structure
156 * which records the names defined in that contour.
157 * Contours include:
158 * 0) the global one
159 * 1) one for each function definition,
160 * where internal declarations of the parameters appear.
161 * 2) one for each compound statement,
162 * to record its declarations.
164 * The current meaning of a name can be found by searching the nested
165 * scopes from the current one out to the global one.
168 struct c_scope GTY(())
170 /* The scope containing this one. */
171 struct c_scope *outer;
173 /* The next outermost function scope. */
174 struct c_scope *outer_function;
176 /* All variables, constants, functions, labels, and typedef names.
177 They are in the reverse of the order supplied. */
178 tree names;
180 /* All structure, union, and enum type tags. */
181 tree tags;
183 /* For each scope, a list of shadowed outer-scope definitions
184 to be restored when this scope is popped.
185 Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
186 whose TREE_VALUE is its old definition (a kind of ..._DECL node). */
187 tree shadowed;
189 /* For each scope, a list of shadowed outer-scope tag definitions
190 to be restored when this scope is popped.
191 Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
192 whose TREE_VALUE is its old definition (a kind of ..._TYPE node). */
193 tree shadowed_tags;
195 /* For each scope (except the global one), a chain of BLOCK nodes
196 for all the scopes that were entered and exited one level down. */
197 tree blocks;
199 /* Variable declarations with incomplete type in this scope. */
200 tree incomplete_list;
202 /* A list of decls giving the (reversed) specified order of parms,
203 not including any forward-decls in the parmlist.
204 This is so we can put the parms in proper order for assign_parms. */
205 tree parm_order;
207 /* True if we are currently filling this scope with parameter
208 declarations. */
209 bool parm_flag : 1;
211 /* True if this is the outermost block scope of a function body.
212 This scope contains the parameters, the local variables declared
213 in the outermost block, and all the labels (except those in
214 nested functions, or declared at block scope with __label__). */
215 bool function_body : 1;
217 /* True means make a BLOCK for this scope no matter what. */
218 bool keep : 1;
221 /* The scope currently in effect. */
223 static GTY(()) struct c_scope *current_scope;
225 /* A chain of c_scope structures awaiting reuse. */
227 static GTY((deletable (""))) struct c_scope *scope_freelist;
229 /* The innermost function scope. Ordinary (not explicitly declared)
230 labels, bindings to error_mark_node, and the lazily-created
231 bindings of __func__ and its friends get this scope. */
233 static GTY(()) struct c_scope *current_function_scope;
235 /* The outermost scope, corresponding to the C "file scope". This is
236 created when the compiler is started and exists through the entire run. */
238 static GTY(()) struct c_scope *global_scope;
240 /* True means unconditionally make a BLOCK for the next scope pushed. */
242 static bool keep_next_level_flag;
244 /* True means the next call to pushlevel will be the outermost scope
245 of a function body, so do not push a new scope, merely cease
246 expecting parameter decls. */
248 static bool next_is_function_body;
250 /* Functions called automatically at the beginning and end of execution. */
252 tree static_ctors, static_dtors;
254 /* Forward declarations. */
256 static struct c_scope *make_scope (void);
257 static void pop_scope (void);
258 static tree match_builtin_function_types (tree, tree);
259 static int duplicate_decls (tree, tree, int, int);
260 static int redeclaration_error_message (tree, tree);
261 static tree make_label (tree, location_t);
262 static void bind_label (tree, tree, struct c_scope *);
263 static void implicit_decl_warning (tree);
264 static void storedecls (tree);
265 static void storetags (tree);
266 static tree lookup_tag (enum tree_code, tree, int);
267 static tree lookup_name_current_level (tree);
268 static tree grokdeclarator (tree, tree, enum decl_context, int);
269 static tree grokparms (tree, int);
270 static void layout_array_type (tree);
271 static void store_parm_decls_newstyle (void);
272 static void store_parm_decls_oldstyle (void);
273 static tree c_make_fname_decl (tree, int);
274 static void c_expand_body_1 (tree, int);
275 static tree any_external_decl (tree);
276 static void record_external_decl (tree);
277 static void warn_if_shadowing (tree, tree);
278 static void clone_underlying_type (tree);
279 static void pushdecl_function_level (tree, tree);
280 static bool flexible_array_type_p (tree);
281 static hashval_t link_hash_hash (const void *);
282 static int link_hash_eq (const void *, const void *);
284 /* States indicating how grokdeclarator() should handle declspecs marked
285 with __attribute__((deprecated)). An object declared as
286 __attribute__((deprecated)) suppresses warnings of uses of other
287 deprecated items. */
289 enum deprecated_states {
290 DEPRECATED_NORMAL,
291 DEPRECATED_SUPPRESS
294 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
296 void
297 c_print_identifier (FILE *file, tree node, int indent)
299 print_node (file, "symbol", IDENTIFIER_SYMBOL_VALUE (node), indent + 4);
300 print_node (file, "tag", IDENTIFIER_TAG_VALUE (node), indent + 4);
301 print_node (file, "label", IDENTIFIER_LABEL_VALUE (node), indent + 4);
302 if (C_IS_RESERVED_WORD (node))
304 tree rid = ridpointers[C_RID_CODE (node)];
305 indent_to (file, indent + 4);
306 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
307 (void *) rid, IDENTIFIER_POINTER (rid));
311 /* Hook called at end of compilation to assume 1 elt
312 for a file-scope tentative array defn that wasn't complete before. */
314 void
315 c_finish_incomplete_decl (tree decl)
317 if (TREE_CODE (decl) == VAR_DECL)
319 tree type = TREE_TYPE (decl);
320 if (type != error_mark_node
321 && TREE_CODE (type) == ARRAY_TYPE
322 && ! DECL_EXTERNAL (decl)
323 && TYPE_DOMAIN (type) == 0)
325 warning ("%Harray '%D' assumed to have one element",
326 &DECL_SOURCE_LOCATION (decl), decl);
328 complete_array_type (type, NULL_TREE, 1);
330 layout_decl (decl, 0);
335 /* Reuse or create a struct for this scope. */
337 static struct c_scope *
338 make_scope (void)
340 struct c_scope *result;
341 if (scope_freelist)
343 result = scope_freelist;
344 scope_freelist = result->outer;
346 else
347 result = ggc_alloc_cleared (sizeof (struct c_scope));
349 return result;
352 /* Remove the topmost scope from the stack and add it to the
353 free list, updating current_function_scope if necessary. */
355 static void
356 pop_scope (void)
358 struct c_scope *scope = current_scope;
360 current_scope = scope->outer;
361 if (scope->function_body)
362 current_function_scope = scope->outer_function;
364 memset (scope, 0, sizeof (struct c_scope));
365 scope->outer = scope_freelist;
366 scope_freelist = scope;
369 /* Nonzero if we are currently in the global scope. */
372 global_bindings_p (void)
374 return current_scope == global_scope;
377 void
378 keep_next_level (void)
380 keep_next_level_flag = true;
383 /* Identify this scope as currently being filled with parameters. */
385 void
386 declare_parm_level (void)
388 current_scope->parm_flag = true;
391 /* Nonzero if currently making parm declarations. */
394 in_parm_level_p (void)
396 return current_scope->parm_flag;
399 /* Enter a new scope. The dummy parameter is for signature
400 compatibility with lang_hooks.decls.pushlevel. */
402 void
403 pushlevel (int dummy ATTRIBUTE_UNUSED)
405 if (next_is_function_body)
407 /* This is the transition from the parameters to the top level
408 of the function body. These are the same scope
409 (C99 6.2.1p4,6) so we do not push another scope structure.
410 next_is_function_body is set only by store_parm_decls, which
411 in turn is called when and only when we are about to
412 encounter the opening curly brace for the function body.
414 The outermost block of a function always gets a BLOCK node,
415 because the debugging output routines expect that each
416 function has at least one BLOCK. */
417 current_scope->parm_flag = false;
418 current_scope->function_body = true;
419 current_scope->keep = true;
420 current_scope->outer_function = current_function_scope;
421 current_function_scope = current_scope;
423 keep_next_level_flag = false;
424 next_is_function_body = false;
426 else
428 struct c_scope *scope = make_scope ();
430 scope->keep = keep_next_level_flag;
431 scope->outer = current_scope;
432 current_scope = scope;
433 keep_next_level_flag = false;
437 /* Exit a scope. Restore the state of the identifier-decl mappings
438 that were in effect when this scope was entered.
440 If KEEP is KEEP_YES (1), this scope had explicit declarations, so
441 create a BLOCK node to record its declarations and subblocks for
442 debugging output. If KEEP is KEEP_MAYBE, do so only if the names
443 or tags lists are nonempty.
445 If REVERSE is nonzero, reverse the order of decls before putting
446 them into the BLOCK.
448 If FUNCTIONBODY is nonzero, this level is the body of a function,
449 even if current_scope->function_body is not set. This is used
450 by language-independent code that generates synthetic functions,
451 and cannot set current_scope->function_body.
453 FIXME: Eliminate the need for all arguments. */
455 tree
456 poplevel (int keep, int reverse, int functionbody)
458 tree link;
459 tree block;
460 tree decl;
461 tree decls = current_scope->names;
462 tree tags = current_scope->tags;
463 tree subblocks = current_scope->blocks;
465 functionbody |= current_scope->function_body;
467 if (keep == KEEP_MAYBE)
468 keep = (current_scope->names || current_scope->tags);
470 keep |= current_scope->keep;
471 keep |= functionbody;
473 /* We used to warn about unused variables in expand_end_bindings,
474 i.e. while generating RTL. But in function-at-a-time mode we may
475 choose to never expand a function at all (e.g. auto inlining), so
476 we do this explicitly now.
477 No warnings when the global scope is popped because the global
478 scope isn't popped for the last translation unit, so the warnings
479 are done in c_write_global_declaration. */
480 if (current_scope != global_scope)
481 warn_about_unused_variables (decls);
483 /* Clear out the name-meanings declared in this scope.
484 Propagate TREE_ADDRESSABLE from nested functions to their
485 containing functions. */
486 for (link = decls; link; link = TREE_CHAIN (link))
488 if (TREE_CODE (link) == LABEL_DECL)
490 if (TREE_USED (link) && DECL_INITIAL (link) == 0)
492 error ("%Hlabel `%D' used but not defined",
493 &DECL_SOURCE_LOCATION (link), link);
494 /* Avoid crashing later. */
495 DECL_INITIAL (link) = error_mark_node;
497 else if (!TREE_USED (link) && warn_unused_label)
499 if (DECL_INITIAL (link) != 0)
500 warning ("%Hlabel `%D' defined but not used",
501 &DECL_SOURCE_LOCATION (link), link);
502 else
503 warning ("%Hlabel `%D' declared but not defined",
504 &DECL_SOURCE_LOCATION (link), link);
506 IDENTIFIER_LABEL_VALUE (DECL_NAME (link)) = 0;
508 else if (DECL_NAME (link) != 0)
510 if (DECL_EXTERNAL (link)
511 && current_scope != global_scope)
512 /* External decls stay in the symbol-value slot but are
513 inaccessible. */
514 C_DECL_INVISIBLE (link) = 1;
515 else
516 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (link)) = 0;
519 if (TREE_CODE (link) == FUNCTION_DECL
520 && ! TREE_ASM_WRITTEN (link)
521 && DECL_INITIAL (link) != 0
522 && TREE_ADDRESSABLE (link)
523 && DECL_ABSTRACT_ORIGIN (link) != 0
524 && DECL_ABSTRACT_ORIGIN (link) != link)
525 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (link)) = 1;
528 /* Clear out the tag-meanings declared in this scope. */
529 for (link = tags; link; link = TREE_CHAIN (link))
530 if (TREE_PURPOSE (link))
531 IDENTIFIER_TAG_VALUE (TREE_PURPOSE (link)) = 0;
533 /* Restore all name- and label-meanings from outer scopes that were
534 shadowed by this scope. */
536 for (link = current_scope->shadowed; link; link = TREE_CHAIN (link))
537 if (TREE_VALUE (link) && TREE_CODE (TREE_VALUE (link)) == LABEL_DECL)
538 IDENTIFIER_LABEL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
539 else
540 IDENTIFIER_SYMBOL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
542 /* Restore all tag-meanings from outer scopes that were shadowed by
543 this scope. */
545 for (link = current_scope->shadowed_tags; link;
546 link = TREE_CHAIN (link))
547 IDENTIFIER_TAG_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
549 /* If this is the outermost block of a function, remove all
550 PARM_DECLs from current_scope->names; they are already
551 stored in DECL_ARGUMENTS of cfun->decl in proper order, should
552 not be put in BLOCK_VARS, and furthermore reversing them will
553 cause trouble later. They are all together at the end of the
554 list. */
555 if (functionbody && decls)
557 if (TREE_CODE (decls) == PARM_DECL)
558 decls = 0;
559 else
561 link = decls;
562 while (TREE_CHAIN (link)
563 && TREE_CODE (TREE_CHAIN (link)) != PARM_DECL)
564 link = TREE_CHAIN (link);
566 TREE_CHAIN (link) = 0;
570 /* Get the decls in the order they were written.
571 Usually current_scope->names is in reverse order.
572 But parameter decls were previously put in forward order. */
574 if (reverse)
575 decls = nreverse (decls);
577 /* If appropriate, create a BLOCK to record the decls for the life
578 of this function. */
580 block = 0;
581 if (keep)
583 block = make_node (BLOCK);
584 BLOCK_VARS (block) = decls;
585 BLOCK_SUBBLOCKS (block) = subblocks;
586 TREE_USED (block) = 1;
589 /* In each subblock, record that this is its superior. */
591 for (link = subblocks; link; link = TREE_CHAIN (link))
592 BLOCK_SUPERCONTEXT (link) = block;
594 /* Set the TYPE_CONTEXTs for all of the tagged types belonging to this
595 binding contour so that they point to the appropriate construct, i.e.
596 either to the current FUNCTION_DECL node, or else to the BLOCK node
597 we just constructed.
599 Note that for tagged types whose scope is just the formal parameter
600 list for some function type specification, we can't properly set
601 their TYPE_CONTEXTs here, because we don't have a pointer to the
602 appropriate FUNCTION_TYPE node readily available to us. For those
603 cases, the TYPE_CONTEXTs of the relevant tagged type nodes get set
604 in `grokdeclarator' as soon as we have created the FUNCTION_TYPE
605 node which will represent the "scope" for these "parameter list local"
606 tagged types. */
608 decl = functionbody ? current_function_decl : block;
609 if (decl)
610 for (link = tags; link; link = TREE_CHAIN (link))
611 TYPE_CONTEXT (TREE_VALUE (link)) = decl;
613 /* Pop the current scope, and free the structure for reuse. */
614 pop_scope ();
616 /* Dispose of the block that we just made inside some higher level. */
617 if (functionbody)
618 DECL_INITIAL (current_function_decl) = block;
619 else if (block && current_scope)
620 current_scope->blocks
621 = chainon (current_scope->blocks, block);
622 /* If we did not make a block for the scope just exited, any blocks
623 made for inner scopes (since they cannot be recorded as subblocks
624 here) must be carried forward so they will later become subblocks
625 of something else. */
626 else if (! block && subblocks)
627 current_scope->blocks
628 = chainon (current_scope->blocks, subblocks);
630 return block;
633 /* Insert BLOCK at the end of the list of subblocks of the current
634 scope. This is used when a BIND_EXPR is expanded, to handle the
635 BLOCK node inside the BIND_EXPR. */
637 void
638 insert_block (tree block)
640 TREE_USED (block) = 1;
641 current_scope->blocks
642 = chainon (current_scope->blocks, block);
645 /* Set the BLOCK node for the innermost scope (the one we are
646 currently in). The RTL expansion machinery requires us to provide
647 this hook, but it is not useful in function-at-a-time mode. */
649 void
650 set_block (tree block ATTRIBUTE_UNUSED)
654 /* Push a definition or a declaration of struct, union or enum tag "name".
655 "type" should be the type node.
656 We assume that the tag "name" is not already defined.
658 Note that the definition may really be just a forward reference.
659 In that case, the TYPE_SIZE will be zero. */
661 void
662 pushtag (tree name, tree type)
664 struct c_scope *b = current_scope;
666 /* Record the identifier as the type's name if it has none. */
667 if (name)
669 if (TYPE_NAME (type) == 0)
670 TYPE_NAME (type) = name;
672 if (IDENTIFIER_TAG_VALUE (name))
673 b->shadowed_tags = tree_cons (name, IDENTIFIER_TAG_VALUE (name),
674 b->shadowed_tags);
675 IDENTIFIER_TAG_VALUE (name) = type;
678 b->tags = tree_cons (name, type, b->tags);
680 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
681 tagged type we just added to the current scope. This fake
682 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
683 to output a representation of a tagged type, and it also gives
684 us a convenient place to record the "scope start" address for the
685 tagged type. */
687 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
689 /* An approximation for now, so we can tell this is a function-scope tag.
690 This will be updated in poplevel. */
691 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
694 /* Subroutine of duplicate_decls. Allow harmless mismatches in return
695 and argument types provided that the type modes match. This function
696 return a unified type given a suitable match, and 0 otherwise. */
698 static tree
699 match_builtin_function_types (tree oldtype, tree newtype)
701 tree newrettype, oldrettype;
702 tree newargs, oldargs;
703 tree trytype, tryargs;
705 /* Accept the return type of the new declaration if same modes. */
706 oldrettype = TREE_TYPE (oldtype);
707 newrettype = TREE_TYPE (newtype);
709 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
710 return 0;
712 oldargs = TYPE_ARG_TYPES (oldtype);
713 newargs = TYPE_ARG_TYPES (newtype);
714 tryargs = newargs;
716 while (oldargs || newargs)
718 if (! oldargs
719 || ! newargs
720 || ! TREE_VALUE (oldargs)
721 || ! TREE_VALUE (newargs)
722 || TYPE_MODE (TREE_VALUE (oldargs))
723 != TYPE_MODE (TREE_VALUE (newargs)))
724 return 0;
726 oldargs = TREE_CHAIN (oldargs);
727 newargs = TREE_CHAIN (newargs);
730 trytype = build_function_type (newrettype, tryargs);
731 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
734 /* Handle when a new declaration NEWDECL
735 has the same name as an old one OLDDECL
736 in the same binding contour.
737 Prints an error message if appropriate.
739 If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
740 Otherwise, return 0.
742 When DIFFERENT_BINDING_LEVEL is true, NEWDECL is an external declaration,
743 and OLDDECL is in an outer scope and should thus not be changed. */
745 static int
746 duplicate_decls (tree newdecl, tree olddecl, int different_binding_level,
747 int different_tu)
749 int comptype_flags = (different_tu ? COMPARE_DIFFERENT_TU
750 : COMPARE_STRICT);
751 int types_match = comptypes (TREE_TYPE (newdecl), TREE_TYPE (olddecl),
752 comptype_flags);
753 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
754 && DECL_INITIAL (newdecl) != 0);
755 tree oldtype = TREE_TYPE (olddecl);
756 tree newtype = TREE_TYPE (newdecl);
757 int errmsg = 0;
759 if (DECL_P (olddecl))
761 if (TREE_CODE (newdecl) == FUNCTION_DECL
762 && TREE_CODE (olddecl) == FUNCTION_DECL
763 && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
765 if (DECL_DECLARED_INLINE_P (newdecl)
766 && DECL_UNINLINABLE (newdecl)
767 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
768 /* Already warned elsewhere. */;
769 else if (DECL_DECLARED_INLINE_P (olddecl)
770 && DECL_UNINLINABLE (olddecl)
771 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
772 /* Already warned. */;
773 else if (DECL_DECLARED_INLINE_P (newdecl)
774 && ! DECL_DECLARED_INLINE_P (olddecl)
775 && DECL_UNINLINABLE (olddecl)
776 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
778 warning ("%Hfunction '%D' redeclared as inline",
779 &DECL_SOURCE_LOCATION (newdecl), newdecl);
780 warning ("%Hprevious declaration of function '%D' "
781 "with attribute noinline",
782 &DECL_SOURCE_LOCATION (olddecl), olddecl);
784 else if (DECL_DECLARED_INLINE_P (olddecl)
785 && DECL_UNINLINABLE (newdecl)
786 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
788 warning ("%Hfunction '%D' redeclared with attribute noinline",
789 &DECL_SOURCE_LOCATION (newdecl), newdecl);
790 warning ("%Hprevious declaration of function '%D' was inline",
791 &DECL_SOURCE_LOCATION (olddecl), olddecl);
795 DECL_ATTRIBUTES (newdecl)
796 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
799 if (TREE_CODE (newtype) == ERROR_MARK
800 || TREE_CODE (oldtype) == ERROR_MARK)
801 types_match = 0;
803 /* New decl is completely inconsistent with the old one =>
804 tell caller to replace the old one.
805 This is always an error except in the case of shadowing a builtin. */
806 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
808 if (TREE_CODE (olddecl) == FUNCTION_DECL
809 && DECL_BUILT_IN (olddecl))
811 /* If you declare a built-in or predefined function name as static,
812 the old definition is overridden,
813 but optionally warn this was a bad choice of name. */
814 if (!TREE_PUBLIC (newdecl))
816 if (warn_shadow)
817 warning ("%Hshadowing built-in function '%D'",
818 &DECL_SOURCE_LOCATION (newdecl), newdecl);
820 else
821 warning ("%Hbuilt-in function '%D' declared as non-function",
822 &DECL_SOURCE_LOCATION (newdecl), newdecl);
824 else
826 error ("%H'%D' redeclared as different kind of symbol",
827 &DECL_SOURCE_LOCATION (newdecl), newdecl);
828 error ("%Hprevious declaration of '%D'",
829 &DECL_SOURCE_LOCATION (olddecl), olddecl);
832 return 0;
835 /* For real parm decl following a forward decl, return 1 so old decl
836 will be reused. Only allow this to happen once. */
837 if (types_match && TREE_CODE (newdecl) == PARM_DECL
838 && TREE_ASM_WRITTEN (olddecl) && ! TREE_ASM_WRITTEN (newdecl))
840 TREE_ASM_WRITTEN (olddecl) = 0;
841 return 1;
844 /* The new declaration is the same kind of object as the old one.
845 The declarations may partially match. Print warnings if they don't
846 match enough. Ultimately, copy most of the information from the new
847 decl to the old one, and keep using the old one. */
849 if (TREE_CODE (olddecl) == FUNCTION_DECL && DECL_BUILT_IN (olddecl))
851 /* A function declaration for a built-in function. */
852 if (!TREE_PUBLIC (newdecl))
854 /* If you declare a built-in function name as static, the
855 built-in definition is overridden,
856 but optionally warn this was a bad choice of name. */
857 if (warn_shadow)
858 warning ("%Hshadowing built-in function '%D'",
859 &DECL_SOURCE_LOCATION (newdecl), newdecl);
860 /* Discard the old built-in function. */
861 return 0;
863 if (!types_match)
865 /* Accept harmless mismatch in function types.
866 This is for the ffs and fprintf builtins. */
867 tree trytype = match_builtin_function_types (oldtype, newtype);
869 if (trytype)
871 types_match = comptypes (newtype, trytype, comptype_flags);
872 if (types_match)
873 oldtype = trytype;
874 if (! different_binding_level)
875 TREE_TYPE (olddecl) = oldtype;
878 if (!types_match)
880 /* If types don't match for a built-in, throw away the built-in. */
881 warning ("%Hconflicting types for built-in function '%D'",
882 &DECL_SOURCE_LOCATION (newdecl), newdecl);
883 return 0;
886 else if (TREE_CODE (olddecl) == FUNCTION_DECL
887 && DECL_SOURCE_LINE (olddecl) == 0)
889 /* A function declaration for a predeclared function
890 that isn't actually built in. */
891 if (!TREE_PUBLIC (newdecl))
893 /* If you declare it as static, the
894 default definition is overridden. */
895 return 0;
897 else if (!types_match)
899 /* If the types don't match, preserve volatility indication.
900 Later on, we will discard everything else about the
901 default declaration. */
902 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
905 /* Permit char *foo () to match void *foo (...) if not pedantic,
906 if one of them came from a system header file. */
907 else if (!types_match
908 && TREE_CODE (olddecl) == FUNCTION_DECL
909 && TREE_CODE (newdecl) == FUNCTION_DECL
910 && TREE_CODE (TREE_TYPE (oldtype)) == POINTER_TYPE
911 && TREE_CODE (TREE_TYPE (newtype)) == POINTER_TYPE
912 && (DECL_IN_SYSTEM_HEADER (olddecl)
913 || DECL_IN_SYSTEM_HEADER (newdecl))
914 && ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (newtype))) == void_type_node
915 && TYPE_ARG_TYPES (oldtype) == 0
916 && self_promoting_args_p (TYPE_ARG_TYPES (newtype))
917 && TREE_TYPE (TREE_TYPE (oldtype)) == char_type_node)
919 (TREE_TYPE (TREE_TYPE (newtype)) == char_type_node
920 && TYPE_ARG_TYPES (newtype) == 0
921 && self_promoting_args_p (TYPE_ARG_TYPES (oldtype))
922 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (oldtype))) == void_type_node)))
924 if (pedantic)
925 pedwarn ("%Hconflicting types for '%D'",
926 &DECL_SOURCE_LOCATION (newdecl), newdecl);
927 /* Make sure we keep void * as ret type, not char *. */
928 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (oldtype))) == void_type_node)
929 TREE_TYPE (newdecl) = newtype = oldtype;
931 /* Set DECL_IN_SYSTEM_HEADER, so that if we see another declaration
932 we will come back here again. */
933 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
935 /* Permit void foo (...) to match int foo (...) if the latter is the
936 definition and implicit int was used. See c-torture/compile/920625-2.c. */
937 else if (!types_match && new_is_definition
938 && TREE_CODE (olddecl) == FUNCTION_DECL
939 && TREE_CODE (newdecl) == FUNCTION_DECL
940 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
941 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
942 && C_FUNCTION_IMPLICIT_INT (newdecl))
944 pedwarn ("%Hconflicting types for '%D'",
945 &DECL_SOURCE_LOCATION (newdecl), newdecl);
946 /* Make sure we keep void as the return type. */
947 TREE_TYPE (newdecl) = newtype = oldtype;
948 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
950 else if (!types_match
951 /* Permit char *foo (int, ...); followed by char *foo ();
952 if not pedantic. */
953 && ! (TREE_CODE (olddecl) == FUNCTION_DECL
954 && ! pedantic
955 /* Return types must still match. */
956 && comptypes (TREE_TYPE (oldtype),
957 TREE_TYPE (newtype), comptype_flags)
958 && TYPE_ARG_TYPES (newtype) == 0))
960 error ("%Hconflicting types for '%D'",
961 &DECL_SOURCE_LOCATION (newdecl), newdecl);
962 /* Check for function type mismatch
963 involving an empty arglist vs a nonempty one. */
964 if (TREE_CODE (olddecl) == FUNCTION_DECL
965 && comptypes (TREE_TYPE (oldtype),
966 TREE_TYPE (newtype), comptype_flags)
967 && ((TYPE_ARG_TYPES (oldtype) == 0
968 && DECL_INITIAL (olddecl) == 0)
970 (TYPE_ARG_TYPES (newtype) == 0
971 && DECL_INITIAL (newdecl) == 0)))
973 /* Classify the problem further. */
974 tree t = TYPE_ARG_TYPES (oldtype);
975 if (t == 0)
976 t = TYPE_ARG_TYPES (newtype);
977 for (; t; t = TREE_CHAIN (t))
979 tree type = TREE_VALUE (t);
981 if (TREE_CHAIN (t) == 0
982 && TYPE_MAIN_VARIANT (type) != void_type_node)
984 error ("a parameter list with an ellipsis can't match an empty parameter name list declaration");
985 break;
988 if (c_type_promotes_to (type) != type)
990 error ("an argument type that has a default promotion can't match an empty parameter name list declaration");
991 break;
995 if (C_DECL_IMPLICIT (olddecl))
996 error ("%Hprevious implicit declaration of '%D'",
997 &DECL_SOURCE_LOCATION (olddecl), olddecl);
998 else
999 error ("%Hprevious declaration of '%D'",
1000 &DECL_SOURCE_LOCATION (olddecl), olddecl);
1002 /* This is safer because the initializer might contain references
1003 to variables that were declared between olddecl and newdecl. This
1004 will make the initializer invalid for olddecl in case it gets
1005 assigned to olddecl below. */
1006 if (TREE_CODE (newdecl) == VAR_DECL)
1007 DECL_INITIAL (newdecl) = 0;
1009 /* TLS cannot follow non-TLS declaration. */
1010 else if (TREE_CODE (olddecl) == VAR_DECL && TREE_CODE (newdecl) == VAR_DECL
1011 && !DECL_THREAD_LOCAL (olddecl) && DECL_THREAD_LOCAL (newdecl))
1013 error ("%Hthread-local declaration of '%D' follows non thread-local "
1014 "declaration", &DECL_SOURCE_LOCATION (newdecl), newdecl);
1015 error ("%Hprevious declaration of '%D'",
1016 &DECL_SOURCE_LOCATION (olddecl), olddecl);
1018 /* non-TLS declaration cannot follow TLS declaration. */
1019 else if (TREE_CODE (olddecl) == VAR_DECL && TREE_CODE (newdecl) == VAR_DECL
1020 && DECL_THREAD_LOCAL (olddecl) && !DECL_THREAD_LOCAL (newdecl))
1022 error ("%Hnon thread-local declaration of '%D' follows "
1023 "thread-local declaration",
1024 &DECL_SOURCE_LOCATION (newdecl), newdecl);
1025 error ("%Hprevious declaration of '%D'",
1026 &DECL_SOURCE_LOCATION (olddecl), olddecl);
1028 else
1030 errmsg = redeclaration_error_message (newdecl, olddecl);
1031 if (errmsg)
1033 const location_t *locus = &DECL_SOURCE_LOCATION (newdecl);
1034 switch (errmsg)
1036 case 1:
1037 error ("%Hredefinition of '%D'", locus, newdecl);
1038 break;
1039 case 2:
1040 error ("%Hredeclaration of '%D'", locus, newdecl);
1041 break;
1042 case 3:
1043 error ("%Hconflicting declarations of '%D'", locus, newdecl);
1044 break;
1045 default:
1046 abort ();
1049 locus = &DECL_SOURCE_LOCATION (olddecl);
1050 if (DECL_INITIAL (olddecl)
1051 && current_scope == global_scope)
1052 error ("%H'%D' previously defined here", locus, olddecl);
1053 else
1054 error ("%H'%D' previously declared here", locus, olddecl);
1055 return 0;
1057 else if (TREE_CODE (newdecl) == TYPE_DECL
1058 && (DECL_IN_SYSTEM_HEADER (olddecl)
1059 || DECL_IN_SYSTEM_HEADER (newdecl)))
1061 const location_t *locus = &DECL_SOURCE_LOCATION (newdecl);
1062 warning ("%Hredefinition of '%D'", locus, newdecl);
1063 locus = &DECL_SOURCE_LOCATION (olddecl);
1064 if (DECL_INITIAL (olddecl)
1065 && current_scope == global_scope)
1066 warning ("%H'%D' previously defined here", locus, olddecl);
1067 else
1068 warning ("%H'%D' previously declared here", locus, olddecl);
1070 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1071 && DECL_INITIAL (olddecl) != 0
1072 && TYPE_ARG_TYPES (oldtype) == 0
1073 && TYPE_ARG_TYPES (newtype) != 0
1074 && TYPE_ACTUAL_ARG_TYPES (oldtype) != 0)
1076 tree type, parm;
1077 int nargs;
1078 /* Prototype decl follows defn w/o prototype. */
1080 for (parm = TYPE_ACTUAL_ARG_TYPES (oldtype),
1081 type = TYPE_ARG_TYPES (newtype),
1082 nargs = 1;
1084 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type), nargs++)
1086 if (TYPE_MAIN_VARIANT (TREE_VALUE (parm)) == void_type_node
1087 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
1089 const location_t *locus = &DECL_SOURCE_LOCATION (newdecl);
1090 warning ("%Hprototype for '%D' follows", locus, newdecl);
1091 locus = &DECL_SOURCE_LOCATION (olddecl);
1092 warning ("%Hnon-prototype definition here", locus);
1093 break;
1095 if (TYPE_MAIN_VARIANT (TREE_VALUE (parm)) == void_type_node
1096 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
1098 const location_t *locus = &DECL_SOURCE_LOCATION (newdecl);
1099 error ("%Hprototype for '%D' follows and number of "
1100 "arguments doesn't match", locus, newdecl);
1101 locus = &DECL_SOURCE_LOCATION (olddecl);
1102 error ("%Hnon-prototype definition here", locus);
1103 errmsg = 1;
1104 break;
1106 /* Type for passing arg must be consistent
1107 with that declared for the arg. */
1108 if (! comptypes (TREE_VALUE (parm), TREE_VALUE (type),
1109 comptype_flags))
1111 const location_t *locus = &DECL_SOURCE_LOCATION (newdecl);
1112 error ("%Hprototype for '%D' follows and argument %d "
1113 "doesn't match", locus, newdecl, nargs);
1114 locus = &DECL_SOURCE_LOCATION (olddecl);
1115 error ("%Hnon-prototype definition here", locus);
1116 errmsg = 1;
1117 break;
1121 /* Warn about mismatches in various flags. */
1122 else
1124 const location_t *locus = &DECL_SOURCE_LOCATION (newdecl);
1126 /* Warn if function is now inline
1127 but was previously declared not inline and has been called. */
1128 if (TREE_CODE (olddecl) == FUNCTION_DECL
1129 && ! DECL_DECLARED_INLINE_P (olddecl)
1130 && DECL_DECLARED_INLINE_P (newdecl)
1131 && TREE_USED (olddecl))
1132 warning ("%H'%D' declared inline after being called",
1133 locus, newdecl);
1134 if (TREE_CODE (olddecl) == FUNCTION_DECL
1135 && ! DECL_DECLARED_INLINE_P (olddecl)
1136 && DECL_DECLARED_INLINE_P (newdecl)
1137 && DECL_INITIAL (olddecl) != 0)
1138 warning ("%H'%D' declared inline after its definition",
1139 locus, newdecl);
1141 /* If pedantic, warn when static declaration follows a non-static
1142 declaration. Otherwise, do so only for functions. */
1143 if ((pedantic || TREE_CODE (olddecl) == FUNCTION_DECL)
1144 && TREE_PUBLIC (olddecl)
1145 && !TREE_PUBLIC (newdecl))
1146 warning ("%Hstatic declaration for '%D' follows non-static",
1147 locus, newdecl);
1149 /* If warn_traditional, warn when a non-static function
1150 declaration follows a static one. */
1151 if (warn_traditional && !in_system_header
1152 && TREE_CODE (olddecl) == FUNCTION_DECL
1153 && !TREE_PUBLIC (olddecl)
1154 && TREE_PUBLIC (newdecl))
1155 warning ("%Hnon-static declaration for '%D' follows static",
1156 locus, newdecl);
1158 /* Warn when const declaration follows a non-const
1159 declaration, but not for functions. */
1160 if (TREE_CODE (olddecl) != FUNCTION_DECL
1161 && !TREE_READONLY (olddecl)
1162 && TREE_READONLY (newdecl))
1163 warning ("%Hconst declaration for '%D' follows non-const",
1164 locus, newdecl);
1165 /* These bits are logically part of the type, for variables.
1166 But not for functions
1167 (where qualifiers are not valid ANSI anyway). */
1168 else if (pedantic && TREE_CODE (olddecl) != FUNCTION_DECL
1169 && (TREE_READONLY (newdecl) != TREE_READONLY (olddecl)
1170 || TREE_THIS_VOLATILE (newdecl) != TREE_THIS_VOLATILE (olddecl)))
1171 pedwarn ("%Htype qualifiers for '%D' conflict with previous "
1172 "declaration", locus, newdecl);
1176 /* Optionally warn about more than one declaration for the same name. */
1177 if (errmsg == 0 && warn_redundant_decls && DECL_SOURCE_LINE (olddecl) != 0
1178 /* Don't warn about a function declaration
1179 followed by a definition. */
1180 && !(TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl) != 0
1181 && DECL_INITIAL (olddecl) == 0)
1182 /* Don't warn about extern decl followed by (tentative) definition. */
1183 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl)))
1185 warning ("%Hredundant redeclaration of '%D' in same scope",
1186 &DECL_SOURCE_LOCATION (newdecl), newdecl);
1187 warning ("%Hprevious declaration of '%D'",
1188 &DECL_SOURCE_LOCATION (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 (C_DECL_FILE_SCOPE (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))
1660 return old;
1661 if (DECL_EXTERNAL (x) || scope == global_scope)
1663 /* Find and check against a previous, not-in-scope, external
1664 decl for this identifier. (C99 s???: If two declarations
1665 with external linkage, referring to the same object, have
1666 incompatible types, the behavior is undefined). */
1667 tree ext = any_external_decl (name);
1668 if (ext)
1670 if (duplicate_decls (x, ext, scope != global_scope,
1671 false))
1672 x = copy_node (ext);
1674 else
1675 record_external_decl (x);
1678 if (TREE_CODE (x) == TYPE_DECL)
1679 clone_underlying_type (x);
1681 /* If storing a local value, there may already be one
1682 (inherited). If so, record it for restoration when this
1683 scope ends. Take care not to do this if we are replacing an
1684 older decl in the same scope (i.e. duplicate_decls returned
1685 false, above). */
1686 if (scope != global_scope
1687 && IDENTIFIER_SYMBOL_VALUE (name)
1688 && IDENTIFIER_SYMBOL_VALUE (name) != old)
1690 warn_if_shadowing (x, IDENTIFIER_SYMBOL_VALUE (name));
1691 scope->shadowed = tree_cons (name, IDENTIFIER_SYMBOL_VALUE (name),
1692 scope->shadowed);
1695 /* Install the new declaration in the requested scope. */
1696 IDENTIFIER_SYMBOL_VALUE (name) = x;
1697 C_DECL_INVISIBLE (x) = 0;
1699 /* Keep list of variables in this scope with incomplete type.
1700 If the input is erroneous, we can have error_mark in the type
1701 slot (e.g. "f(void a, ...)") - that doesn't count as an
1702 incomplete type.
1704 FIXME: Chain these off the TYPE_DECL for the incomplete type,
1705 then we don't have to do (potentially quite costly) searches
1706 in finish_struct. */
1707 if (TREE_TYPE (x) != error_mark_node
1708 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
1710 tree element = TREE_TYPE (x);
1712 while (TREE_CODE (element) == ARRAY_TYPE)
1713 element = TREE_TYPE (element);
1714 if (TREE_CODE (element) == RECORD_TYPE
1715 || TREE_CODE (element) == UNION_TYPE)
1716 scope->incomplete_list = tree_cons (NULL_TREE, x,
1717 scope->incomplete_list);
1721 /* Put decls on list in reverse order.
1722 We will reverse them later if necessary. */
1723 TREE_CHAIN (x) = scope->names;
1724 scope->names = x;
1726 return x;
1729 /* Record X as belonging to the global scope (C99 "file scope").
1730 This is used only internally by the Objective-C front end,
1731 and is limited to its needs. It will hork if there is _any_
1732 visible binding for X (not just a global one). */
1733 tree
1734 pushdecl_top_level (tree x)
1736 tree name, old;
1738 if (TREE_CODE (x) != VAR_DECL)
1739 abort ();
1741 name = DECL_NAME (x);
1742 old = IDENTIFIER_SYMBOL_VALUE (name);
1744 if (old)
1746 if (DECL_CONTEXT (old))
1747 abort ();
1749 if (!duplicate_decls (x, old, 0, false))
1750 abort ();
1752 return old;
1755 DECL_CONTEXT (x) = current_file_decl;
1756 IDENTIFIER_SYMBOL_VALUE (name) = x;
1757 TREE_CHAIN (x) = global_scope->names;
1758 global_scope->names = x;
1759 return x;
1762 /* Record X as belonging to the outermost scope of the current
1763 function. This is used only internally, by c_make_fname_decl and
1764 undeclared_variable, and is limited to their needs. The NAME is
1765 provided as a separate argument because undeclared_variable wants to
1766 use error_mark_node for X. For VAR_DECLs, duplicate_decls is not
1767 called; if there is any preexisting decl for this identifier, it is
1768 an ICE. */
1769 static void
1770 pushdecl_function_level (tree x, tree name)
1772 struct c_scope *scope = current_function_scope;
1774 if (x == error_mark_node)
1775 scope->shadowed = tree_cons (name, IDENTIFIER_SYMBOL_VALUE (name),
1776 scope->shadowed);
1777 else if (TREE_CODE (x) == VAR_DECL)
1779 if (name != DECL_NAME (x))
1780 abort ();
1781 if (IDENTIFIER_SYMBOL_VALUE (name))
1782 abort ();
1784 DECL_CONTEXT (x) = current_function_decl;
1785 TREE_CHAIN (x) = scope->names;
1786 scope->names = x;
1789 IDENTIFIER_SYMBOL_VALUE (name) = x;
1792 /* Generate an implicit declaration for identifier FUNCTIONID as a
1793 function of type int (). */
1795 tree
1796 implicitly_declare (tree functionid)
1798 tree decl = any_external_decl (functionid);
1800 if (decl && decl != error_mark_node)
1802 /* Implicit declaration of a function already declared
1803 (somehow) in a different scope, or as a built-in.
1804 If this is the first time this has happened, warn;
1805 then recycle the old declaration. */
1806 if (!C_DECL_IMPLICIT (decl))
1808 implicit_decl_warning (DECL_NAME (decl));
1809 if (! C_DECL_FILE_SCOPE (decl))
1810 warning ("%Hprevious declaration of '%D'",
1811 &DECL_SOURCE_LOCATION (decl), decl);
1812 C_DECL_IMPLICIT (decl) = 1;
1814 /* If this function is global, then it must already be in the
1815 global scope, so there's no need to push it again. */
1816 if (current_scope == global_scope)
1817 return decl;
1818 /* If this is a local declaration, make a copy; we can't have
1819 the same DECL listed in two different scopes. */
1820 return pushdecl (copy_node (decl));
1823 /* Not seen before. */
1824 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
1825 DECL_EXTERNAL (decl) = 1;
1826 TREE_PUBLIC (decl) = 1;
1827 C_DECL_IMPLICIT (decl) = 1;
1828 implicit_decl_warning (functionid);
1830 /* C89 says implicit declarations are in the innermost block.
1831 So we record the decl in the standard fashion. */
1832 decl = pushdecl (decl);
1834 /* No need to call objc_check_decl here - it's a function type. */
1835 rest_of_decl_compilation (decl, NULL, 0, 0);
1837 /* Write a record describing this implicit function declaration
1838 to the prototypes file (if requested). */
1839 gen_aux_info_record (decl, 0, 1, 0);
1841 /* Possibly apply some default attributes to this implicit declaration. */
1842 decl_attributes (&decl, NULL_TREE, 0);
1844 return decl;
1847 static void
1848 implicit_decl_warning (tree id)
1850 const char *name = IDENTIFIER_POINTER (id);
1851 if (mesg_implicit_function_declaration == 2)
1852 error ("implicit declaration of function `%s'", name);
1853 else if (mesg_implicit_function_declaration == 1)
1854 warning ("implicit declaration of function `%s'", name);
1857 /* Return zero if the declaration NEWDECL is valid
1858 when the declaration OLDDECL (assumed to be for the same name)
1859 has already been seen.
1860 Otherwise return 1 if NEWDECL is a redefinition, 2 if it is a redeclaration,
1861 and 3 if it is a conflicting declaration. */
1863 static int
1864 redeclaration_error_message (tree newdecl, tree olddecl)
1866 if (TREE_CODE (newdecl) == TYPE_DECL)
1868 /* Do not complain about type redeclarations where at least one
1869 declaration was in a system header. */
1870 if (DECL_IN_SYSTEM_HEADER (olddecl) || DECL_IN_SYSTEM_HEADER (newdecl))
1871 return 0;
1872 return 1;
1874 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1876 /* Declarations of functions can insist on internal linkage
1877 but they can't be inconsistent with internal linkage,
1878 so there can be no error on that account.
1879 However defining the same name twice is no good. */
1880 if (DECL_INITIAL (olddecl) != 0 && DECL_INITIAL (newdecl) != 0
1881 /* However, defining once as extern inline and a second
1882 time in another way is ok. */
1883 && ! (DECL_DECLARED_INLINE_P (olddecl) && DECL_EXTERNAL (olddecl)
1884 && ! (DECL_DECLARED_INLINE_P (newdecl)
1885 && DECL_EXTERNAL (newdecl))))
1886 return 1;
1887 return 0;
1889 else if (C_DECL_FILE_SCOPE (newdecl))
1891 /* Objects declared at file scope: */
1892 /* If at least one is a reference, it's ok. */
1893 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
1894 return 0;
1895 /* Reject two definitions. */
1896 if (DECL_INITIAL (olddecl) != 0 && DECL_INITIAL (newdecl) != 0)
1897 return 1;
1898 /* Now we have two tentative defs, or one tentative and one real def. */
1899 /* Insist that the linkage match. */
1900 if (TREE_PUBLIC (olddecl) != TREE_PUBLIC (newdecl))
1901 return 3;
1902 return 0;
1904 else if (current_scope->parm_flag
1905 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1906 return 0;
1907 else
1909 /* Newdecl has block scope. If olddecl has block scope also, then
1910 reject two definitions, and reject a definition together with an
1911 external reference. Otherwise, it is OK, because newdecl must
1912 be an extern reference to olddecl. */
1913 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl))
1914 && DECL_CONTEXT (newdecl) == DECL_CONTEXT (olddecl))
1915 return 2;
1916 return 0;
1920 /* Issue an error message for a reference to an undeclared variable
1921 ID, including a reference to a builtin outside of function-call
1922 context. Establish a binding of the identifier to error_mark_node
1923 in an appropriate scope, which will suppress further errors for the
1924 same identifier. */
1925 void
1926 undeclared_variable (tree id)
1928 static bool already = false;
1930 if (current_function_decl == 0)
1932 error ("`%s' undeclared here (not in a function)",
1933 IDENTIFIER_POINTER (id));
1934 IDENTIFIER_SYMBOL_VALUE (id) = error_mark_node;
1936 else
1938 error ("`%s' undeclared (first use in this function)",
1939 IDENTIFIER_POINTER (id));
1941 if (! already)
1943 error ("(Each undeclared identifier is reported only once");
1944 error ("for each function it appears in.)");
1945 already = true;
1948 pushdecl_function_level (error_mark_node, id);
1952 /* Subroutine of lookup_label, declare_label, define_label: construct a
1953 LABEL_DECL with all the proper frills. */
1955 static tree
1956 make_label (tree name, location_t location)
1958 tree label = build_decl (LABEL_DECL, name, void_type_node);
1960 DECL_CONTEXT (label) = current_function_decl;
1961 DECL_MODE (label) = VOIDmode;
1962 DECL_SOURCE_LOCATION (label) = location;
1964 return label;
1967 /* Another subroutine of lookup_label, declare_label, define_label:
1968 set up the binding of name to LABEL_DECL in the given SCOPE. */
1970 static void
1971 bind_label (tree name, tree label, struct c_scope *scope)
1973 if (IDENTIFIER_LABEL_VALUE (name))
1974 scope->shadowed = tree_cons (name, IDENTIFIER_LABEL_VALUE (name),
1975 scope->shadowed);
1976 IDENTIFIER_LABEL_VALUE (name) = label;
1978 TREE_CHAIN (label) = scope->names;
1979 scope->names = label;
1982 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
1983 Create one if none exists so far for the current function.
1984 This is called when a label is used in a goto expression or
1985 has its address taken. */
1987 tree
1988 lookup_label (tree name)
1990 tree label;
1992 if (current_function_decl == 0)
1994 error ("label %s referenced outside of any function",
1995 IDENTIFIER_POINTER (name));
1996 return 0;
1999 /* Use a label already defined or ref'd with this name, but not if
2000 it is inherited from a containing function and wasn't declared
2001 using __label__. */
2002 label = IDENTIFIER_LABEL_VALUE (name);
2003 if (label && (DECL_CONTEXT (label) == current_function_decl
2004 || C_DECLARED_LABEL_FLAG (label)))
2006 /* If the label has only been declared, update its apparent
2007 location to point here, for better diagnostics if it
2008 turns out not to have been defined. */
2009 if (!TREE_USED (label))
2010 DECL_SOURCE_LOCATION (label) = input_location;
2011 return label;
2014 /* No label binding for that identifier; make one. */
2015 label = make_label (name, input_location);
2017 /* Ordinary labels go in the current function scope. */
2018 bind_label (name, label, current_function_scope);
2019 return label;
2022 /* Make a label named NAME in the current function, shadowing silently
2023 any that may be inherited from containing functions or containing
2024 scopes. This is called for __label__ declarations. */
2026 /* Note that valid use, if the label being shadowed comes from another
2027 scope in the same function, requires calling declare_nonlocal_label
2028 right away. (Is this still true? -zw 2003-07-17) */
2030 tree
2031 declare_label (tree name)
2033 tree label = IDENTIFIER_LABEL_VALUE (name);
2034 tree dup;
2036 /* Check to make sure that the label hasn't already been declared
2037 at this scope */
2038 for (dup = current_scope->names; dup; dup = TREE_CHAIN (dup))
2039 if (dup == label)
2041 error ("duplicate label declaration `%s'", IDENTIFIER_POINTER (name));
2042 error ("%Hthis is a previous declaration",
2043 &DECL_SOURCE_LOCATION (dup));
2045 /* Just use the previous declaration. */
2046 return dup;
2049 label = make_label (name, input_location);
2050 C_DECLARED_LABEL_FLAG (label) = 1;
2052 /* Declared labels go in the current scope. */
2053 bind_label (name, label, current_scope);
2054 return label;
2057 /* Define a label, specifying the location in the source file.
2058 Return the LABEL_DECL node for the label, if the definition is valid.
2059 Otherwise return 0. */
2061 tree
2062 define_label (location_t location, tree name)
2064 tree label;
2066 /* Find any preexisting label with this name. It is an error
2067 if that label has already been defined in this function, or
2068 if there is a containing function with a declared label with
2069 the same name. */
2070 label = IDENTIFIER_LABEL_VALUE (name);
2072 if (label
2073 && ((DECL_CONTEXT (label) == current_function_decl
2074 && DECL_INITIAL (label) != 0)
2075 || (DECL_CONTEXT (label) != current_function_decl
2076 && C_DECLARED_LABEL_FLAG (label))))
2078 location_t *prev_loc = &DECL_SOURCE_LOCATION (label);
2079 error ("%Hduplicate label `%D'", &location, label);
2080 if (DECL_INITIAL (label))
2081 error ("%H`%D' previously defined here", prev_loc, label);
2082 else
2083 error ("%H`%D' previously declared here", prev_loc, label);
2084 return 0;
2086 else if (label && DECL_CONTEXT (label) == current_function_decl)
2088 /* The label has been used or declared already in this function,
2089 but not defined. Update its location to point to this
2090 definition. */
2091 DECL_SOURCE_LOCATION (label) = location;
2093 else
2095 /* No label binding for that identifier; make one. */
2096 label = make_label (name, location);
2098 /* Ordinary labels go in the current function scope. */
2099 bind_label (name, label, current_function_scope);
2102 if (warn_traditional && !in_system_header && lookup_name (name))
2103 warning ("%Htraditional C lacks a separate namespace for labels, "
2104 "identifier `%s' conflicts", &location,
2105 IDENTIFIER_POINTER (name));
2107 /* Mark label as having been defined. */
2108 DECL_INITIAL (label) = error_mark_node;
2109 return label;
2112 /* Return the list of declarations of the current scope.
2113 Note that this list is in reverse order. */
2115 tree
2116 getdecls (void)
2118 return current_scope->names;
2121 /* Return the list of type-tags (for structs, etc) of the current scope. */
2123 tree
2124 gettags (void)
2126 return current_scope->tags;
2129 /* Store the list of declarations of the current scope.
2130 This is done for the parameter declarations of a function being defined,
2131 after they are modified in the light of any missing parameters. */
2133 static void
2134 storedecls (tree decls)
2136 current_scope->names = decls;
2139 /* Similarly, store the list of tags of the current scope. */
2141 static void
2142 storetags (tree tags)
2144 current_scope->tags = tags;
2147 /* Given NAME, an IDENTIFIER_NODE,
2148 return the structure (or union or enum) definition for that name.
2149 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2150 CODE says which kind of type the caller wants;
2151 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2152 If the wrong kind of type is found, an error is reported. */
2154 static tree
2155 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2157 tree tag = IDENTIFIER_TAG_VALUE (name);
2158 int thislevel = 0;
2160 if (!tag)
2161 return 0;
2163 /* We only care about whether it's in this level if
2164 thislevel_only was set or it might be a type clash. */
2165 if (thislevel_only || TREE_CODE (tag) != code)
2167 if (current_scope == global_scope
2168 || purpose_member (name, current_scope->tags))
2169 thislevel = 1;
2172 if (thislevel_only && !thislevel)
2173 return 0;
2175 if (TREE_CODE (tag) != code)
2177 /* Definition isn't the kind we were looking for. */
2178 pending_invalid_xref = name;
2179 pending_invalid_xref_location = input_location;
2181 /* If in the same binding level as a declaration as a tag
2182 of a different type, this must not be allowed to
2183 shadow that tag, so give the error immediately.
2184 (For example, "struct foo; union foo;" is invalid.) */
2185 if (thislevel)
2186 pending_xref_error ();
2188 return tag;
2191 /* Print an error message now
2192 for a recent invalid struct, union or enum cross reference.
2193 We don't print them immediately because they are not invalid
2194 when used in the `struct foo;' construct for shadowing. */
2196 void
2197 pending_xref_error (void)
2199 if (pending_invalid_xref != 0)
2200 error ("%H`%s' defined as wrong kind of tag",
2201 &pending_invalid_xref_location,
2202 IDENTIFIER_POINTER (pending_invalid_xref));
2203 pending_invalid_xref = 0;
2207 /* Look up NAME in the current scope and its superiors
2208 in the namespace of variables, functions and typedefs.
2209 Return a ..._DECL node of some kind representing its definition,
2210 or return 0 if it is undefined. */
2212 tree
2213 lookup_name (tree name)
2215 tree decl = IDENTIFIER_SYMBOL_VALUE (name);
2216 if (decl == 0 || decl == error_mark_node)
2217 return decl;
2218 if (C_DECL_INVISIBLE (decl))
2219 return 0;
2220 return decl;
2223 /* Similar to `lookup_name' but look only at the current scope. */
2225 static tree
2226 lookup_name_current_level (tree name)
2228 tree decl = IDENTIFIER_SYMBOL_VALUE (name);
2230 if (decl == 0 || decl == error_mark_node || C_DECL_INVISIBLE (decl))
2231 return 0;
2233 if (current_scope == global_scope)
2234 return decl;
2236 /* Scan the current scope for a decl with name NAME. */
2237 if (chain_member (decl, current_scope->names))
2238 return decl;
2240 return 0;
2243 /* Create the predefined scalar types of C,
2244 and some nodes representing standard constants (0, 1, (void *) 0).
2245 Initialize the global scope.
2246 Make definitions for built-in primitive functions. */
2248 void
2249 c_init_decl_processing (void)
2251 tree endlink;
2252 tree ptr_ftype_void, ptr_ftype_ptr;
2253 location_t save_loc = input_location;
2255 /* Adds some ggc roots, and reserved words for c-parse.in. */
2256 c_parse_init ();
2258 current_function_decl = 0;
2260 /* Make the c_scope structure for global names. */
2261 pushlevel (0);
2262 global_scope = current_scope;
2264 /* Declarations from c_common_nodes_and_builtins must not be associated
2265 with this input file, lest we get differences between using and not
2266 using preprocessed headers. */
2267 input_location.file = "<internal>";
2268 input_location.line = 0;
2270 /* Make the DECL for the toplevel file scope. */
2271 current_file_decl = build_decl (TRANSLATION_UNIT_DECL, NULL, NULL);
2273 build_common_tree_nodes (flag_signed_char);
2275 c_common_nodes_and_builtins ();
2277 /* In C, comparisons and TRUTH_* expressions have type int. */
2278 truthvalue_type_node = integer_type_node;
2279 truthvalue_true_node = integer_one_node;
2280 truthvalue_false_node = integer_zero_node;
2282 /* Even in C99, which has a real boolean type. */
2283 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2284 boolean_type_node));
2286 endlink = void_list_node;
2287 ptr_ftype_void = build_function_type (ptr_type_node, endlink);
2288 ptr_ftype_ptr
2289 = build_function_type (ptr_type_node,
2290 tree_cons (NULL_TREE, ptr_type_node, endlink));
2292 input_location = save_loc;
2294 pedantic_lvalues = pedantic;
2296 make_fname_decl = c_make_fname_decl;
2297 start_fname_decls ();
2299 builtin_decls = global_scope->names;
2302 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2303 decl, NAME is the initialization string and TYPE_DEP indicates whether
2304 NAME depended on the type of the function. As we don't yet implement
2305 delayed emission of static data, we mark the decl as emitted
2306 so it is not placed in the output. Anything using it must therefore pull
2307 out the STRING_CST initializer directly. This does mean that these names
2308 are string merging candidates, which is wrong for C99's __func__. FIXME. */
2310 static tree
2311 c_make_fname_decl (tree id, int type_dep)
2313 const char *name = fname_as_string (type_dep);
2314 tree decl, type, init;
2315 size_t length = strlen (name);
2317 type = build_array_type
2318 (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
2319 build_index_type (size_int (length)));
2321 decl = build_decl (VAR_DECL, id, type);
2323 TREE_STATIC (decl) = 1;
2324 TREE_READONLY (decl) = 1;
2325 DECL_ARTIFICIAL (decl) = 1;
2327 init = build_string (length + 1, name);
2328 TREE_TYPE (init) = type;
2329 DECL_INITIAL (decl) = init;
2331 TREE_USED (decl) = 1;
2333 if (current_function_decl)
2334 pushdecl_function_level (decl, DECL_NAME (decl));
2336 finish_decl (decl, init, NULL_TREE);
2338 return decl;
2341 /* Return a definition for a builtin function named NAME and whose data type
2342 is TYPE. TYPE should be a function type with argument types.
2343 FUNCTION_CODE tells later passes how to compile calls to this function.
2344 See tree.h for its possible values.
2346 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2347 the name to be called if we can't opencode the function. If
2348 ATTRS is nonzero, use that for the function's attribute list. */
2350 tree
2351 builtin_function (const char *name, tree type, int function_code,
2352 enum built_in_class class, const char *library_name,
2353 tree attrs)
2355 tree decl = build_decl (FUNCTION_DECL, get_identifier (name), type);
2356 DECL_EXTERNAL (decl) = 1;
2357 TREE_PUBLIC (decl) = 1;
2358 if (library_name)
2359 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2360 make_decl_rtl (decl, NULL);
2361 pushdecl (decl);
2362 DECL_BUILT_IN_CLASS (decl) = class;
2363 DECL_FUNCTION_CODE (decl) = function_code;
2365 /* Warn if a function in the namespace for users
2366 is used without an occasion to consider it declared. */
2367 if (name[0] != '_' || name[1] != '_')
2368 C_DECL_INVISIBLE (decl) = 1;
2370 /* Possibly apply some default attributes to this built-in function. */
2371 if (attrs)
2372 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2373 else
2374 decl_attributes (&decl, NULL_TREE, 0);
2376 return decl;
2379 /* Apply default attributes to a function, if a system function with default
2380 attributes. */
2382 void
2383 c_insert_default_attributes (tree decl)
2385 if (!TREE_PUBLIC (decl))
2386 return;
2387 c_common_insert_default_attributes (decl);
2390 /* Called when a declaration is seen that contains no names to declare.
2391 If its type is a reference to a structure, union or enum inherited
2392 from a containing scope, shadow that tag name for the current scope
2393 with a forward reference.
2394 If its type defines a new named structure or union
2395 or defines an enum, it is valid but we need not do anything here.
2396 Otherwise, it is an error. */
2398 void
2399 shadow_tag (tree declspecs)
2401 shadow_tag_warned (declspecs, 0);
2404 void
2405 shadow_tag_warned (tree declspecs, int warned)
2408 /* 1 => we have done a pedwarn. 2 => we have done a warning, but
2409 no pedwarn. */
2411 int found_tag = 0;
2412 tree link;
2413 tree specs, attrs;
2415 pending_invalid_xref = 0;
2417 /* Remove the attributes from declspecs, since they will confuse the
2418 following code. */
2419 split_specs_attrs (declspecs, &specs, &attrs);
2421 for (link = specs; link; link = TREE_CHAIN (link))
2423 tree value = TREE_VALUE (link);
2424 enum tree_code code = TREE_CODE (value);
2426 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2427 /* Used to test also that TYPE_SIZE (value) != 0.
2428 That caused warning for `struct foo;' at top level in the file. */
2430 tree name = TYPE_NAME (value);
2431 tree t;
2433 found_tag++;
2435 if (name == 0)
2437 if (warned != 1 && code != ENUMERAL_TYPE)
2438 /* Empty unnamed enum OK */
2440 pedwarn ("unnamed struct/union that defines no instances");
2441 warned = 1;
2444 else
2446 t = lookup_tag (code, name, 1);
2448 if (t == 0)
2450 t = make_node (code);
2451 pushtag (name, t);
2455 else
2457 if (!warned && ! in_system_header)
2459 warning ("useless keyword or type name in empty declaration");
2460 warned = 2;
2465 if (found_tag > 1)
2466 error ("two types specified in one empty declaration");
2468 if (warned != 1)
2470 if (found_tag == 0)
2471 pedwarn ("empty declaration");
2475 /* Construct an array declarator. EXPR is the expression inside [], or
2476 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
2477 to the pointer to which a parameter array is converted). STATIC_P is
2478 nonzero if "static" is inside the [], zero otherwise. VLA_UNSPEC_P
2479 is nonzero is the array is [*], a VLA of unspecified length which is
2480 nevertheless a complete type (not currently implemented by GCC),
2481 zero otherwise. The declarator is constructed as an ARRAY_REF
2482 (to be decoded by grokdeclarator), whose operand 0 is what's on the
2483 left of the [] (filled by in set_array_declarator_type) and operand 1
2484 is the expression inside; whose TREE_TYPE is the type qualifiers and
2485 which has TREE_STATIC set if "static" is used. */
2487 tree
2488 build_array_declarator (tree expr, tree quals, int static_p, int vla_unspec_p)
2490 tree decl;
2491 decl = build_nt (ARRAY_REF, NULL_TREE, expr);
2492 TREE_TYPE (decl) = quals;
2493 TREE_STATIC (decl) = (static_p ? 1 : 0);
2494 if (pedantic && !flag_isoc99)
2496 if (static_p || quals != NULL_TREE)
2497 pedwarn ("ISO C90 does not support `static' or type qualifiers in parameter array declarators");
2498 if (vla_unspec_p)
2499 pedwarn ("ISO C90 does not support `[*]' array declarators");
2501 if (vla_unspec_p)
2502 warning ("GCC does not yet properly implement `[*]' array declarators");
2503 return decl;
2506 /* Set the type of an array declarator. DECL is the declarator, as
2507 constructed by build_array_declarator; TYPE is what appears on the left
2508 of the [] and goes in operand 0. ABSTRACT_P is nonzero if it is an
2509 abstract declarator, zero otherwise; this is used to reject static and
2510 type qualifiers in abstract declarators, where they are not in the
2511 C99 grammar. */
2513 tree
2514 set_array_declarator_type (tree decl, tree type, int abstract_p)
2516 TREE_OPERAND (decl, 0) = type;
2517 if (abstract_p && (TREE_TYPE (decl) != NULL_TREE || TREE_STATIC (decl)))
2518 error ("static or type qualifiers in abstract declarator");
2519 return decl;
2522 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
2524 tree
2525 groktypename (tree typename)
2527 tree specs, attrs;
2529 if (TREE_CODE (typename) != TREE_LIST)
2530 return typename;
2532 split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
2534 typename = grokdeclarator (TREE_VALUE (typename), specs, TYPENAME, 0);
2536 /* Apply attributes. */
2537 decl_attributes (&typename, attrs, 0);
2539 return typename;
2542 /* Return a PARM_DECL node for a given pair of specs and declarator. */
2544 tree
2545 groktypename_in_parm_context (tree typename)
2547 if (TREE_CODE (typename) != TREE_LIST)
2548 return typename;
2549 return grokdeclarator (TREE_VALUE (typename),
2550 TREE_PURPOSE (typename),
2551 PARM, 0);
2554 /* Decode a declarator in an ordinary declaration or data definition.
2555 This is called as soon as the type information and variable name
2556 have been parsed, before parsing the initializer if any.
2557 Here we create the ..._DECL node, fill in its type,
2558 and put it on the list of decls for the current context.
2559 The ..._DECL node is returned as the value.
2561 Exception: for arrays where the length is not specified,
2562 the type is left null, to be filled in by `finish_decl'.
2564 Function definitions do not come here; they go to start_function
2565 instead. However, external and forward declarations of functions
2566 do go through here. Structure field declarations are done by
2567 grokfield and not through here. */
2569 tree
2570 start_decl (tree declarator, tree declspecs, int initialized, tree attributes)
2572 tree decl;
2573 tree tem;
2575 /* An object declared as __attribute__((deprecated)) suppresses
2576 warnings of uses of other deprecated items. */
2577 if (lookup_attribute ("deprecated", attributes))
2578 deprecated_state = DEPRECATED_SUPPRESS;
2580 decl = grokdeclarator (declarator, declspecs,
2581 NORMAL, initialized);
2583 deprecated_state = DEPRECATED_NORMAL;
2585 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
2586 && MAIN_NAME_P (DECL_NAME (decl)))
2587 warning ("%H'%D' is usually a function",
2588 &DECL_SOURCE_LOCATION (decl), decl);
2590 if (initialized)
2591 /* Is it valid for this decl to have an initializer at all?
2592 If not, set INITIALIZED to zero, which will indirectly
2593 tell `finish_decl' to ignore the initializer once it is parsed. */
2594 switch (TREE_CODE (decl))
2596 case TYPE_DECL:
2597 error ("typedef `%s' is initialized (use __typeof__ instead)",
2598 IDENTIFIER_POINTER (DECL_NAME (decl)));
2599 initialized = 0;
2600 break;
2602 case FUNCTION_DECL:
2603 error ("function `%s' is initialized like a variable",
2604 IDENTIFIER_POINTER (DECL_NAME (decl)));
2605 initialized = 0;
2606 break;
2608 case PARM_DECL:
2609 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
2610 error ("parameter `%s' is initialized",
2611 IDENTIFIER_POINTER (DECL_NAME (decl)));
2612 initialized = 0;
2613 break;
2615 default:
2616 /* Don't allow initializations for incomplete types
2617 except for arrays which might be completed by the initialization. */
2619 /* This can happen if the array size is an undefined macro. We already
2620 gave a warning, so we don't need another one. */
2621 if (TREE_TYPE (decl) == error_mark_node)
2622 initialized = 0;
2623 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
2625 /* A complete type is ok if size is fixed. */
2627 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
2628 || C_DECL_VARIABLE_SIZE (decl))
2630 error ("variable-sized object may not be initialized");
2631 initialized = 0;
2634 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
2636 error ("variable `%s' has initializer but incomplete type",
2637 IDENTIFIER_POINTER (DECL_NAME (decl)));
2638 initialized = 0;
2640 else if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
2642 error ("elements of array `%s' have incomplete type",
2643 IDENTIFIER_POINTER (DECL_NAME (decl)));
2644 initialized = 0;
2648 if (initialized)
2650 DECL_EXTERNAL (decl) = 0;
2651 if (current_scope == global_scope)
2652 TREE_STATIC (decl) = 1;
2654 /* Tell `pushdecl' this is an initialized decl
2655 even though we don't yet have the initializer expression.
2656 Also tell `finish_decl' it may store the real initializer. */
2657 DECL_INITIAL (decl) = error_mark_node;
2660 /* If this is a function declaration, write a record describing it to the
2661 prototypes file (if requested). */
2663 if (TREE_CODE (decl) == FUNCTION_DECL)
2664 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
2666 /* ANSI specifies that a tentative definition which is not merged with
2667 a non-tentative definition behaves exactly like a definition with an
2668 initializer equal to zero. (Section 3.7.2)
2670 -fno-common gives strict ANSI behavior, though this tends to break
2671 a large body of code that grew up without this rule.
2673 Thread-local variables are never common, since there's no entrenched
2674 body of code to break, and it allows more efficient variable references
2675 in the presence of dynamic linking. */
2677 if (TREE_CODE (decl) == VAR_DECL
2678 && !initialized
2679 && TREE_PUBLIC (decl)
2680 && !DECL_THREAD_LOCAL (decl)
2681 && !flag_no_common)
2682 DECL_COMMON (decl) = 1;
2684 /* Set attributes here so if duplicate decl, will have proper attributes. */
2685 decl_attributes (&decl, attributes, 0);
2687 if (TREE_CODE (decl) == FUNCTION_DECL
2688 && DECL_DECLARED_INLINE_P (decl)
2689 && DECL_UNINLINABLE (decl)
2690 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
2691 warning ("%Hinline function '%D' given attribute noinline",
2692 &DECL_SOURCE_LOCATION (decl), decl);
2694 /* Add this decl to the current scope.
2695 TEM may equal DECL or it may be a previous decl of the same name. */
2696 tem = pushdecl (decl);
2698 /* For a local variable, define the RTL now. */
2699 if (current_scope != global_scope
2700 /* But not if this is a duplicate decl
2701 and we preserved the rtl from the previous one
2702 (which may or may not happen). */
2703 && !DECL_RTL_SET_P (tem)
2704 && C_DECL_FILE_SCOPE (tem))
2706 if (TREE_TYPE (tem) != error_mark_node
2707 && (COMPLETE_TYPE_P (TREE_TYPE (tem))
2708 || (TREE_CODE (TREE_TYPE (tem)) == ARRAY_TYPE
2709 && DECL_INITIAL (tem) != 0)))
2710 expand_decl (tem);
2713 return tem;
2716 /* Finish processing of a declaration;
2717 install its initial value.
2718 If the length of an array type is not known before,
2719 it must be determined now, from the initial value, or it is an error. */
2721 void
2722 finish_decl (tree decl, tree init, tree asmspec_tree)
2724 tree type = TREE_TYPE (decl);
2725 int was_incomplete = (DECL_SIZE (decl) == 0);
2726 const char *asmspec = 0;
2728 /* If a name was specified, get the string. */
2729 if (current_scope == global_scope)
2730 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
2731 if (asmspec_tree)
2732 asmspec = TREE_STRING_POINTER (asmspec_tree);
2734 /* If `start_decl' didn't like having an initialization, ignore it now. */
2735 if (init != 0 && DECL_INITIAL (decl) == 0)
2736 init = 0;
2738 /* Don't crash if parm is initialized. */
2739 if (TREE_CODE (decl) == PARM_DECL)
2740 init = 0;
2742 if (init)
2743 store_init_value (decl, init);
2745 /* Deduce size of array from initialization, if not already known */
2746 if (TREE_CODE (type) == ARRAY_TYPE
2747 && TYPE_DOMAIN (type) == 0
2748 && TREE_CODE (decl) != TYPE_DECL)
2750 int do_default
2751 = (TREE_STATIC (decl)
2752 /* Even if pedantic, an external linkage array
2753 may have incomplete type at first. */
2754 ? pedantic && !TREE_PUBLIC (decl)
2755 : !DECL_EXTERNAL (decl));
2756 int failure
2757 = complete_array_type (type, DECL_INITIAL (decl), do_default);
2759 /* Get the completed type made by complete_array_type. */
2760 type = TREE_TYPE (decl);
2762 if (failure == 1)
2763 error ("%Hinitializer fails to determine size of '%D'",
2764 &DECL_SOURCE_LOCATION (decl), decl);
2766 else if (failure == 2)
2768 if (do_default)
2769 error ("%Harray size missing in '%D'",
2770 &DECL_SOURCE_LOCATION (decl), decl);
2771 /* If a `static' var's size isn't known,
2772 make it extern as well as static, so it does not get
2773 allocated.
2774 If it is not `static', then do not mark extern;
2775 finish_incomplete_decl will give it a default size
2776 and it will get allocated. */
2777 else if (!pedantic && TREE_STATIC (decl) && ! TREE_PUBLIC (decl))
2778 DECL_EXTERNAL (decl) = 1;
2781 /* TYPE_MAX_VALUE is always one less than the number of elements
2782 in the array, because we start counting at zero. Therefore,
2783 warn only if the value is less than zero. */
2784 else if (pedantic && TYPE_DOMAIN (type) != 0
2785 && tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < 0)
2786 error ("%Hzero or negative size array '%D'",
2787 &DECL_SOURCE_LOCATION (decl), decl);
2789 layout_decl (decl, 0);
2792 if (TREE_CODE (decl) == VAR_DECL)
2794 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
2795 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
2796 layout_decl (decl, 0);
2798 if (DECL_SIZE (decl) == 0
2799 /* Don't give an error if we already gave one earlier. */
2800 && TREE_TYPE (decl) != error_mark_node
2801 && (TREE_STATIC (decl)
2803 /* A static variable with an incomplete type
2804 is an error if it is initialized.
2805 Also if it is not file scope.
2806 Otherwise, let it through, but if it is not `extern'
2807 then it may cause an error message later. */
2808 (DECL_INITIAL (decl) != 0
2809 || !C_DECL_FILE_SCOPE (decl))
2811 /* An automatic variable with an incomplete type
2812 is an error. */
2813 !DECL_EXTERNAL (decl)))
2815 error ("%Hstorage size of '%D' isn't known",
2816 &DECL_SOURCE_LOCATION (decl), decl);
2817 TREE_TYPE (decl) = error_mark_node;
2820 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
2821 && DECL_SIZE (decl) != 0)
2823 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
2824 constant_expression_warning (DECL_SIZE (decl));
2825 else
2826 error ("%Hstorage size of '%D' isn't constant",
2827 &DECL_SOURCE_LOCATION (decl), decl);
2830 if (TREE_USED (type))
2831 TREE_USED (decl) = 1;
2834 /* If this is a function and an assembler name is specified, reset DECL_RTL
2835 so we can give it its new name. Also, update built_in_decls if it
2836 was a normal built-in. */
2837 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
2839 /* ASMSPEC is given, and not the name of a register. Mark the
2840 name with a star so assemble_name won't munge it. */
2841 char *starred = alloca (strlen (asmspec) + 2);
2842 starred[0] = '*';
2843 strcpy (starred + 1, asmspec);
2845 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
2847 tree builtin = built_in_decls [DECL_FUNCTION_CODE (decl)];
2848 SET_DECL_RTL (builtin, NULL_RTX);
2849 SET_DECL_ASSEMBLER_NAME (builtin, get_identifier (starred));
2850 #ifdef TARGET_MEM_FUNCTIONS
2851 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMCPY)
2852 init_block_move_fn (starred);
2853 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMSET)
2854 init_block_clear_fn (starred);
2855 #else
2856 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BCOPY)
2857 init_block_move_fn (starred);
2858 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BZERO)
2859 init_block_clear_fn (starred);
2860 #endif
2862 SET_DECL_RTL (decl, NULL_RTX);
2863 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (starred));
2866 /* If #pragma weak was used, mark the decl weak now. */
2867 if (current_scope == global_scope)
2868 maybe_apply_pragma_weak (decl);
2870 /* Output the assembler code and/or RTL code for variables and functions,
2871 unless the type is an undefined structure or union.
2872 If not, it will get done when the type is completed. */
2874 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
2876 /* This is a no-op in c-lang.c or something real in objc-act.c. */
2877 if (c_dialect_objc ())
2878 objc_check_decl (decl);
2880 if (C_DECL_FILE_SCOPE (decl))
2882 if (DECL_INITIAL (decl) == NULL_TREE
2883 || DECL_INITIAL (decl) == error_mark_node)
2884 /* Don't output anything
2885 when a tentative file-scope definition is seen.
2886 But at end of compilation, do output code for them. */
2887 DECL_DEFER_OUTPUT (decl) = 1;
2888 rest_of_decl_compilation (decl, asmspec, true, 0);
2890 else
2892 /* This is a local variable. If there is an ASMSPEC, the
2893 user has requested that we handle it specially. */
2894 if (asmspec)
2896 /* In conjunction with an ASMSPEC, the `register'
2897 keyword indicates that we should place the variable
2898 in a particular register. */
2899 if (DECL_REGISTER (decl))
2900 DECL_C_HARD_REGISTER (decl) = 1;
2902 /* If this is not a static variable, issue a warning.
2903 It doesn't make any sense to give an ASMSPEC for an
2904 ordinary, non-register local variable. Historically,
2905 GCC has accepted -- but ignored -- the ASMSPEC in
2906 this case. */
2907 if (TREE_CODE (decl) == VAR_DECL
2908 && !DECL_REGISTER (decl)
2909 && !TREE_STATIC (decl))
2910 warning ("%Hignoring asm-specifier for non-static local "
2911 "variable '%D'", &DECL_SOURCE_LOCATION (decl), decl);
2912 else
2913 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
2916 if (TREE_CODE (decl) != FUNCTION_DECL)
2917 add_decl_stmt (decl);
2920 if (!C_DECL_FILE_SCOPE (decl))
2922 /* Recompute the RTL of a local array now
2923 if it used to be an incomplete type. */
2924 if (was_incomplete
2925 && ! TREE_STATIC (decl) && ! DECL_EXTERNAL (decl))
2927 /* If we used it already as memory, it must stay in memory. */
2928 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
2929 /* If it's still incomplete now, no init will save it. */
2930 if (DECL_SIZE (decl) == 0)
2931 DECL_INITIAL (decl) = 0;
2936 /* If this was marked 'used', be sure it will be output. */
2937 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
2938 mark_referenced (DECL_ASSEMBLER_NAME (decl));
2940 if (TREE_CODE (decl) == TYPE_DECL)
2942 /* This is a no-op in c-lang.c or something real in objc-act.c. */
2943 if (c_dialect_objc ())
2944 objc_check_decl (decl);
2945 rest_of_decl_compilation (decl, NULL, C_DECL_FILE_SCOPE (decl), 0);
2948 /* At the end of a declaration, throw away any variable type sizes
2949 of types defined inside that declaration. There is no use
2950 computing them in the following function definition. */
2951 if (current_scope == global_scope)
2952 get_pending_sizes ();
2954 /* Install a cleanup (aka destructor) if one was given. */
2955 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
2957 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
2958 if (attr)
2960 static bool eh_initialized_p;
2962 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
2963 tree cleanup_decl = lookup_name (cleanup_id);
2964 tree cleanup;
2966 /* Build "cleanup(&decl)" for the destructor. */
2967 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
2968 cleanup = build_tree_list (NULL_TREE, cleanup);
2969 cleanup = build_function_call (cleanup_decl, cleanup);
2971 /* Don't warn about decl unused; the cleanup uses it. */
2972 TREE_USED (decl) = 1;
2974 /* Initialize EH, if we've been told to do so. */
2975 if (flag_exceptions && !eh_initialized_p)
2977 eh_initialized_p = true;
2978 eh_personality_libfunc
2979 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
2980 ? "__gcc_personality_sj0"
2981 : "__gcc_personality_v0");
2982 using_eh_for_cleanups ();
2985 add_stmt (build_stmt (CLEANUP_STMT, decl, cleanup));
2990 /* Given a parsed parameter declaration,
2991 decode it into a PARM_DECL and push that on the current scope.
2992 Also, for the sake of forward parm decls,
2993 record the given order of parms in `parm_order'. */
2995 void
2996 push_parm_decl (tree parm)
2998 tree decl;
3000 /* Don't attempt to expand sizes while parsing this decl.
3001 (We can get here with i_s_e 1 somehow from Objective-C.) */
3002 int save_immediate_size_expand = immediate_size_expand;
3003 immediate_size_expand = 0;
3005 decl = grokdeclarator (TREE_VALUE (TREE_PURPOSE (parm)),
3006 TREE_PURPOSE (TREE_PURPOSE (parm)), PARM, 0);
3007 decl_attributes (&decl, TREE_VALUE (parm), 0);
3009 decl = pushdecl (decl);
3011 current_scope->parm_order
3012 = tree_cons (NULL_TREE, decl, current_scope->parm_order);
3014 finish_decl (decl, NULL_TREE, NULL_TREE);
3016 immediate_size_expand = save_immediate_size_expand;
3019 /* Clear the given order of parms in `parm_order'.
3020 Used at start of parm list,
3021 and also at semicolon terminating forward decls. */
3023 void
3024 clear_parm_order (void)
3026 current_scope->parm_order = NULL_TREE;
3029 static GTY(()) int compound_literal_number;
3031 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3032 literal, which may be an incomplete array type completed by the
3033 initializer; INIT is a CONSTRUCTOR that initializes the compound
3034 literal. */
3036 tree
3037 build_compound_literal (tree type, tree init)
3039 /* We do not use start_decl here because we have a type, not a declarator;
3040 and do not use finish_decl because the decl should be stored inside
3041 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_STMT. */
3042 tree decl = build_decl (VAR_DECL, NULL_TREE, type);
3043 tree complit;
3044 tree stmt;
3045 DECL_EXTERNAL (decl) = 0;
3046 TREE_PUBLIC (decl) = 0;
3047 TREE_STATIC (decl) = (current_scope == global_scope);
3048 DECL_CONTEXT (decl) = current_function_decl;
3049 TREE_USED (decl) = 1;
3050 TREE_TYPE (decl) = type;
3051 TREE_READONLY (decl) = TREE_READONLY (type);
3052 store_init_value (decl, init);
3054 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3056 int failure = complete_array_type (type, DECL_INITIAL (decl), 1);
3057 if (failure)
3058 abort ();
3061 type = TREE_TYPE (decl);
3062 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3063 return error_mark_node;
3065 stmt = build_stmt (DECL_STMT, decl);
3066 complit = build1 (COMPOUND_LITERAL_EXPR, TREE_TYPE (decl), stmt);
3067 TREE_SIDE_EFFECTS (complit) = 1;
3069 layout_decl (decl, 0);
3071 if (TREE_STATIC (decl))
3073 /* This decl needs a name for the assembler output. We also need
3074 a unique suffix to be added to the name. */
3075 char *name;
3077 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
3078 compound_literal_number);
3079 compound_literal_number++;
3080 DECL_NAME (decl) = get_identifier (name);
3081 DECL_DEFER_OUTPUT (decl) = 1;
3082 DECL_COMDAT (decl) = 1;
3083 DECL_ARTIFICIAL (decl) = 1;
3084 pushdecl (decl);
3085 rest_of_decl_compilation (decl, NULL, 1, 0);
3088 return complit;
3091 /* Make TYPE a complete type based on INITIAL_VALUE.
3092 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
3093 2 if there was no information (in which case assume 1 if DO_DEFAULT). */
3096 complete_array_type (tree type, tree initial_value, int do_default)
3098 tree maxindex = NULL_TREE;
3099 int value = 0;
3101 if (initial_value)
3103 /* Note MAXINDEX is really the maximum index,
3104 one less than the size. */
3105 if (TREE_CODE (initial_value) == STRING_CST)
3107 int eltsize
3108 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
3109 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
3110 / eltsize) - 1, 0);
3112 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
3114 tree elts = CONSTRUCTOR_ELTS (initial_value);
3115 maxindex = build_int_2 (-1, -1);
3116 for (; elts; elts = TREE_CHAIN (elts))
3118 if (TREE_PURPOSE (elts))
3119 maxindex = TREE_PURPOSE (elts);
3120 else
3121 maxindex = fold (build (PLUS_EXPR, integer_type_node,
3122 maxindex, integer_one_node));
3124 maxindex = copy_node (maxindex);
3126 else
3128 /* Make an error message unless that happened already. */
3129 if (initial_value != error_mark_node)
3130 value = 1;
3132 /* Prevent further error messages. */
3133 maxindex = build_int_2 (0, 0);
3137 if (!maxindex)
3139 if (do_default)
3140 maxindex = build_int_2 (0, 0);
3141 value = 2;
3144 if (maxindex)
3146 TYPE_DOMAIN (type) = build_index_type (maxindex);
3147 if (!TREE_TYPE (maxindex))
3148 TREE_TYPE (maxindex) = TYPE_DOMAIN (type);
3151 /* Lay out the type now that we can get the real answer. */
3153 layout_type (type);
3155 return value;
3158 /* Determine whether TYPE is a structure with a flexible array member,
3159 or a union containing such a structure (possibly recursively). */
3161 static bool
3162 flexible_array_type_p (tree type)
3164 tree x;
3165 switch (TREE_CODE (type))
3167 case RECORD_TYPE:
3168 x = TYPE_FIELDS (type);
3169 if (x == NULL_TREE)
3170 return false;
3171 while (TREE_CHAIN (x) != NULL_TREE)
3172 x = TREE_CHAIN (x);
3173 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3174 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3175 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3176 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3177 return true;
3178 return false;
3179 case UNION_TYPE:
3180 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3182 if (flexible_array_type_p (TREE_TYPE (x)))
3183 return true;
3185 return false;
3186 default:
3187 return false;
3191 /* Given declspecs and a declarator,
3192 determine the name and type of the object declared
3193 and construct a ..._DECL node for it.
3194 (In one case we can return a ..._TYPE node instead.
3195 For invalid input we sometimes return 0.)
3197 DECLSPECS is a chain of tree_list nodes whose value fields
3198 are the storage classes and type specifiers.
3200 DECL_CONTEXT says which syntactic context this declaration is in:
3201 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3202 FUNCDEF for a function definition. Like NORMAL but a few different
3203 error messages in each case. Return value may be zero meaning
3204 this definition is too screwy to try to parse.
3205 PARM for a parameter declaration (either within a function prototype
3206 or before a function body). Make a PARM_DECL, or return void_type_node.
3207 TYPENAME if for a typename (in a cast or sizeof).
3208 Don't make a DECL node; just return the ..._TYPE node.
3209 FIELD for a struct or union field; make a FIELD_DECL.
3210 BITFIELD for a field with specified width.
3211 INITIALIZED is 1 if the decl has an initializer.
3213 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3214 It may also be so in the PARM case, for a prototype where the
3215 argument type is specified but not the name.
3217 This function is where the complicated C meanings of `static'
3218 and `extern' are interpreted. */
3220 static tree
3221 grokdeclarator (tree declarator, tree declspecs,
3222 enum decl_context decl_context, int initialized)
3224 int specbits = 0;
3225 tree spec;
3226 tree type = NULL_TREE;
3227 int longlong = 0;
3228 int constp;
3229 int restrictp;
3230 int volatilep;
3231 int type_quals = TYPE_UNQUALIFIED;
3232 int inlinep;
3233 int explicit_int = 0;
3234 int explicit_char = 0;
3235 int defaulted_int = 0;
3236 tree typedef_decl = 0;
3237 const char *name;
3238 tree typedef_type = 0;
3239 int funcdef_flag = 0;
3240 enum tree_code innermost_code = ERROR_MARK;
3241 int bitfield = 0;
3242 int size_varies = 0;
3243 tree decl_attr = NULL_TREE;
3244 tree array_ptr_quals = NULL_TREE;
3245 int array_parm_static = 0;
3246 tree returned_attrs = NULL_TREE;
3248 if (decl_context == BITFIELD)
3249 bitfield = 1, decl_context = FIELD;
3251 if (decl_context == FUNCDEF)
3252 funcdef_flag = 1, decl_context = NORMAL;
3254 /* Look inside a declarator for the name being declared
3255 and get it as a string, for an error message. */
3257 tree decl = declarator;
3258 name = 0;
3260 while (decl)
3261 switch (TREE_CODE (decl))
3263 case ARRAY_REF:
3264 case INDIRECT_REF:
3265 case CALL_EXPR:
3266 innermost_code = TREE_CODE (decl);
3267 decl = TREE_OPERAND (decl, 0);
3268 break;
3270 case TREE_LIST:
3271 decl = TREE_VALUE (decl);
3272 break;
3274 case IDENTIFIER_NODE:
3275 name = IDENTIFIER_POINTER (decl);
3276 decl = 0;
3277 break;
3279 default:
3280 abort ();
3282 if (name == 0)
3283 name = "type name";
3286 /* A function definition's declarator must have the form of
3287 a function declarator. */
3289 if (funcdef_flag && innermost_code != CALL_EXPR)
3290 return 0;
3292 /* If this looks like a function definition, make it one,
3293 even if it occurs where parms are expected.
3294 Then store_parm_decls will reject it and not use it as a parm. */
3295 if (decl_context == NORMAL && !funcdef_flag
3296 && current_scope->parm_flag)
3297 decl_context = PARM;
3299 /* Look through the decl specs and record which ones appear.
3300 Some typespecs are defined as built-in typenames.
3301 Others, the ones that are modifiers of other types,
3302 are represented by bits in SPECBITS: set the bits for
3303 the modifiers that appear. Storage class keywords are also in SPECBITS.
3305 If there is a typedef name or a type, store the type in TYPE.
3306 This includes builtin typedefs such as `int'.
3308 Set EXPLICIT_INT or EXPLICIT_CHAR if the type is `int' or `char'
3309 and did not come from a user typedef.
3311 Set LONGLONG if `long' is mentioned twice. */
3313 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
3315 tree id = TREE_VALUE (spec);
3317 /* If the entire declaration is itself tagged as deprecated then
3318 suppress reports of deprecated items. */
3319 if (id && TREE_DEPRECATED (id))
3321 if (deprecated_state != DEPRECATED_SUPPRESS)
3322 warn_deprecated_use (id);
3325 if (id == ridpointers[(int) RID_INT])
3326 explicit_int = 1;
3327 if (id == ridpointers[(int) RID_CHAR])
3328 explicit_char = 1;
3330 if (TREE_CODE (id) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (id))
3332 enum rid i = C_RID_CODE (id);
3333 if ((int) i <= (int) RID_LAST_MODIFIER)
3335 if (i == RID_LONG && (specbits & (1 << (int) RID_LONG)))
3337 if (longlong)
3338 error ("`long long long' is too long for GCC");
3339 else
3341 if (pedantic && !flag_isoc99 && ! in_system_header
3342 && warn_long_long)
3343 pedwarn ("ISO C90 does not support `long long'");
3344 longlong = 1;
3347 else if (specbits & (1 << (int) i))
3349 if (i == RID_CONST || i == RID_VOLATILE || i == RID_RESTRICT)
3351 if (!flag_isoc99)
3352 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
3354 else
3355 error ("duplicate `%s'", IDENTIFIER_POINTER (id));
3358 /* Diagnose "__thread extern". Recall that this list
3359 is in the reverse order seen in the text. */
3360 if (i == RID_THREAD
3361 && (specbits & (1 << (int) RID_EXTERN
3362 | 1 << (int) RID_STATIC)))
3364 if (specbits & 1 << (int) RID_EXTERN)
3365 error ("`__thread' before `extern'");
3366 else
3367 error ("`__thread' before `static'");
3370 specbits |= 1 << (int) i;
3371 goto found;
3374 if (type)
3375 error ("two or more data types in declaration of `%s'", name);
3376 /* Actual typedefs come to us as TYPE_DECL nodes. */
3377 else if (TREE_CODE (id) == TYPE_DECL)
3379 if (TREE_TYPE (id) == error_mark_node)
3380 ; /* Allow the type to default to int to avoid cascading errors. */
3381 else
3383 type = TREE_TYPE (id);
3384 decl_attr = DECL_ATTRIBUTES (id);
3385 typedef_decl = id;
3388 /* Built-in types come as identifiers. */
3389 else if (TREE_CODE (id) == IDENTIFIER_NODE)
3391 tree t = lookup_name (id);
3392 if (TREE_TYPE (t) == error_mark_node)
3394 else if (!t || TREE_CODE (t) != TYPE_DECL)
3395 error ("`%s' fails to be a typedef or built in type",
3396 IDENTIFIER_POINTER (id));
3397 else
3399 type = TREE_TYPE (t);
3400 typedef_decl = t;
3403 else if (TREE_CODE (id) != ERROR_MARK)
3404 type = id;
3406 found:
3410 typedef_type = type;
3411 if (type)
3412 size_varies = C_TYPE_VARIABLE_SIZE (type);
3414 /* No type at all: default to `int', and set DEFAULTED_INT
3415 because it was not a user-defined typedef. */
3417 if (type == 0)
3419 if ((! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3420 | (1 << (int) RID_SIGNED)
3421 | (1 << (int) RID_UNSIGNED)
3422 | (1 << (int) RID_COMPLEX))))
3423 /* Don't warn about typedef foo = bar. */
3424 && ! (specbits & (1 << (int) RID_TYPEDEF) && initialized)
3425 && ! in_system_header)
3427 /* Issue a warning if this is an ISO C 99 program or if -Wreturn-type
3428 and this is a function, or if -Wimplicit; prefer the former
3429 warning since it is more explicit. */
3430 if ((warn_implicit_int || warn_return_type || flag_isoc99)
3431 && funcdef_flag)
3432 warn_about_return_type = 1;
3433 else if (warn_implicit_int || flag_isoc99)
3434 pedwarn_c99 ("type defaults to `int' in declaration of `%s'",
3435 name);
3438 defaulted_int = 1;
3439 type = integer_type_node;
3442 /* Now process the modifiers that were specified
3443 and check for invalid combinations. */
3445 /* Long double is a special combination. */
3447 if ((specbits & 1 << (int) RID_LONG) && ! longlong
3448 && TYPE_MAIN_VARIANT (type) == double_type_node)
3450 specbits &= ~(1 << (int) RID_LONG);
3451 type = long_double_type_node;
3454 /* Check all other uses of type modifiers. */
3456 if (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3457 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED)))
3459 int ok = 0;
3461 if ((specbits & 1 << (int) RID_LONG)
3462 && (specbits & 1 << (int) RID_SHORT))
3463 error ("both long and short specified for `%s'", name);
3464 else if (((specbits & 1 << (int) RID_LONG)
3465 || (specbits & 1 << (int) RID_SHORT))
3466 && explicit_char)
3467 error ("long or short specified with char for `%s'", name);
3468 else if (((specbits & 1 << (int) RID_LONG)
3469 || (specbits & 1 << (int) RID_SHORT))
3470 && TREE_CODE (type) == REAL_TYPE)
3472 static int already = 0;
3474 error ("long or short specified with floating type for `%s'", name);
3475 if (! already && ! pedantic)
3477 error ("the only valid combination is `long double'");
3478 already = 1;
3481 else if ((specbits & 1 << (int) RID_SIGNED)
3482 && (specbits & 1 << (int) RID_UNSIGNED))
3483 error ("both signed and unsigned specified for `%s'", name);
3484 else if (TREE_CODE (type) != INTEGER_TYPE)
3485 error ("long, short, signed or unsigned invalid for `%s'", name);
3486 else
3488 ok = 1;
3489 if (!explicit_int && !defaulted_int && !explicit_char)
3491 error ("long, short, signed or unsigned used invalidly for `%s'",
3492 name);
3493 ok = 0;
3497 /* Discard the type modifiers if they are invalid. */
3498 if (! ok)
3500 specbits &= ~((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3501 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED));
3502 longlong = 0;
3506 if ((specbits & (1 << (int) RID_COMPLEX))
3507 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
3509 error ("complex invalid for `%s'", name);
3510 specbits &= ~(1 << (int) RID_COMPLEX);
3513 /* Decide whether an integer type is signed or not.
3514 Optionally treat bitfields as signed by default. */
3515 if (specbits & 1 << (int) RID_UNSIGNED
3516 || (bitfield && ! flag_signed_bitfields
3517 && (explicit_int || defaulted_int || explicit_char
3518 /* A typedef for plain `int' without `signed'
3519 can be controlled just like plain `int'. */
3520 || ! (typedef_decl != 0
3521 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
3522 && TREE_CODE (type) != ENUMERAL_TYPE
3523 && !(specbits & 1 << (int) RID_SIGNED)))
3525 if (longlong)
3526 type = long_long_unsigned_type_node;
3527 else if (specbits & 1 << (int) RID_LONG)
3528 type = long_unsigned_type_node;
3529 else if (specbits & 1 << (int) RID_SHORT)
3530 type = short_unsigned_type_node;
3531 else if (type == char_type_node)
3532 type = unsigned_char_type_node;
3533 else if (typedef_decl)
3534 type = c_common_unsigned_type (type);
3535 else
3536 type = unsigned_type_node;
3538 else if ((specbits & 1 << (int) RID_SIGNED)
3539 && type == char_type_node)
3540 type = signed_char_type_node;
3541 else if (longlong)
3542 type = long_long_integer_type_node;
3543 else if (specbits & 1 << (int) RID_LONG)
3544 type = long_integer_type_node;
3545 else if (specbits & 1 << (int) RID_SHORT)
3546 type = short_integer_type_node;
3548 if (specbits & 1 << (int) RID_COMPLEX)
3550 if (pedantic && !flag_isoc99)
3551 pedwarn ("ISO C90 does not support complex types");
3552 /* If we just have "complex", it is equivalent to
3553 "complex double", but if any modifiers at all are specified it is
3554 the complex form of TYPE. E.g, "complex short" is
3555 "complex short int". */
3557 if (defaulted_int && ! longlong
3558 && ! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3559 | (1 << (int) RID_SIGNED)
3560 | (1 << (int) RID_UNSIGNED))))
3562 if (pedantic)
3563 pedwarn ("ISO C does not support plain `complex' meaning `double complex'");
3564 type = complex_double_type_node;
3566 else if (type == integer_type_node)
3568 if (pedantic)
3569 pedwarn ("ISO C does not support complex integer types");
3570 type = complex_integer_type_node;
3572 else if (type == float_type_node)
3573 type = complex_float_type_node;
3574 else if (type == double_type_node)
3575 type = complex_double_type_node;
3576 else if (type == long_double_type_node)
3577 type = complex_long_double_type_node;
3578 else
3580 if (pedantic)
3581 pedwarn ("ISO C does not support complex integer types");
3582 type = build_complex_type (type);
3586 /* Figure out the type qualifiers for the declaration. There are
3587 two ways a declaration can become qualified. One is something
3588 like `const int i' where the `const' is explicit. Another is
3589 something like `typedef const int CI; CI i' where the type of the
3590 declaration contains the `const'. */
3591 constp = !! (specbits & 1 << (int) RID_CONST) + TYPE_READONLY (type);
3592 restrictp = !! (specbits & 1 << (int) RID_RESTRICT) + TYPE_RESTRICT (type);
3593 volatilep = !! (specbits & 1 << (int) RID_VOLATILE) + TYPE_VOLATILE (type);
3594 inlinep = !! (specbits & (1 << (int) RID_INLINE));
3595 if (constp > 1 && ! flag_isoc99)
3596 pedwarn ("duplicate `const'");
3597 if (restrictp > 1 && ! flag_isoc99)
3598 pedwarn ("duplicate `restrict'");
3599 if (volatilep > 1 && ! flag_isoc99)
3600 pedwarn ("duplicate `volatile'");
3601 if (! flag_gen_aux_info && (TYPE_QUALS (type)))
3602 type = TYPE_MAIN_VARIANT (type);
3603 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
3604 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
3605 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
3607 /* Warn if two storage classes are given. Default to `auto'. */
3610 int nclasses = 0;
3612 if (specbits & 1 << (int) RID_AUTO) nclasses++;
3613 if (specbits & 1 << (int) RID_STATIC) nclasses++;
3614 if (specbits & 1 << (int) RID_EXTERN) nclasses++;
3615 if (specbits & 1 << (int) RID_REGISTER) nclasses++;
3616 if (specbits & 1 << (int) RID_TYPEDEF) nclasses++;
3618 /* "static __thread" and "extern __thread" are allowed. */
3619 if ((specbits & (1 << (int) RID_THREAD
3620 | 1 << (int) RID_STATIC
3621 | 1 << (int) RID_EXTERN)) == (1 << (int) RID_THREAD))
3622 nclasses++;
3624 /* Warn about storage classes that are invalid for certain
3625 kinds of declarations (parameters, typenames, etc.). */
3627 if (nclasses > 1)
3628 error ("multiple storage classes in declaration of `%s'", name);
3629 else if (funcdef_flag
3630 && (specbits
3631 & ((1 << (int) RID_REGISTER)
3632 | (1 << (int) RID_AUTO)
3633 | (1 << (int) RID_TYPEDEF)
3634 | (1 << (int) RID_THREAD))))
3636 if (specbits & 1 << (int) RID_AUTO
3637 && (pedantic || current_scope == global_scope))
3638 pedwarn ("function definition declared `auto'");
3639 if (specbits & 1 << (int) RID_REGISTER)
3640 error ("function definition declared `register'");
3641 if (specbits & 1 << (int) RID_TYPEDEF)
3642 error ("function definition declared `typedef'");
3643 if (specbits & 1 << (int) RID_THREAD)
3644 error ("function definition declared `__thread'");
3645 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3646 | (1 << (int) RID_AUTO) | (1 << (int) RID_THREAD));
3648 else if (decl_context != NORMAL && nclasses > 0)
3650 if (decl_context == PARM && specbits & 1 << (int) RID_REGISTER)
3652 else
3654 switch (decl_context)
3656 case FIELD:
3657 error ("storage class specified for structure field `%s'",
3658 name);
3659 break;
3660 case PARM:
3661 error ("storage class specified for parameter `%s'", name);
3662 break;
3663 default:
3664 error ("storage class specified for typename");
3665 break;
3667 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3668 | (1 << (int) RID_AUTO) | (1 << (int) RID_STATIC)
3669 | (1 << (int) RID_EXTERN) | (1 << (int) RID_THREAD));
3672 else if (specbits & 1 << (int) RID_EXTERN && initialized && ! funcdef_flag)
3674 /* `extern' with initialization is invalid if not at file scope. */
3675 if (current_scope == global_scope)
3676 warning ("`%s' initialized and declared `extern'", name);
3677 else
3678 error ("`%s' has both `extern' and initializer", name);
3680 else if (current_scope == global_scope)
3682 if (specbits & 1 << (int) RID_AUTO)
3683 error ("file-scope declaration of `%s' specifies `auto'", name);
3685 else
3687 if (specbits & 1 << (int) RID_EXTERN && funcdef_flag)
3688 error ("nested function `%s' declared `extern'", name);
3689 else if ((specbits & (1 << (int) RID_THREAD
3690 | 1 << (int) RID_EXTERN
3691 | 1 << (int) RID_STATIC))
3692 == (1 << (int) RID_THREAD))
3694 error ("function-scope `%s' implicitly auto and declared `__thread'",
3695 name);
3696 specbits &= ~(1 << (int) RID_THREAD);
3701 /* Now figure out the structure of the declarator proper.
3702 Descend through it, creating more complex types, until we reach
3703 the declared identifier (or NULL_TREE, in an absolute declarator). */
3705 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE)
3707 if (type == error_mark_node)
3709 declarator = TREE_OPERAND (declarator, 0);
3710 continue;
3713 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
3714 an INDIRECT_REF (for *...),
3715 a CALL_EXPR (for ...(...)),
3716 a TREE_LIST (for nested attributes),
3717 an identifier (for the name being declared)
3718 or a null pointer (for the place in an absolute declarator
3719 where the name was omitted).
3720 For the last two cases, we have just exited the loop.
3722 At this point, TYPE is the type of elements of an array,
3723 or for a function to return, or for a pointer to point to.
3724 After this sequence of ifs, TYPE is the type of the
3725 array or function or pointer, and DECLARATOR has had its
3726 outermost layer removed. */
3728 if (array_ptr_quals != NULL_TREE || array_parm_static)
3730 /* Only the innermost declarator (making a parameter be of
3731 array type which is converted to pointer type)
3732 may have static or type qualifiers. */
3733 error ("static or type qualifiers in non-parameter array declarator");
3734 array_ptr_quals = NULL_TREE;
3735 array_parm_static = 0;
3738 if (TREE_CODE (declarator) == TREE_LIST)
3740 /* We encode a declarator with embedded attributes using
3741 a TREE_LIST. */
3742 tree attrs = TREE_PURPOSE (declarator);
3743 tree inner_decl;
3744 int attr_flags = 0;
3745 declarator = TREE_VALUE (declarator);
3746 inner_decl = declarator;
3747 while (inner_decl != NULL_TREE
3748 && TREE_CODE (inner_decl) == TREE_LIST)
3749 inner_decl = TREE_VALUE (inner_decl);
3750 if (inner_decl == NULL_TREE
3751 || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
3752 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
3753 else if (TREE_CODE (inner_decl) == CALL_EXPR)
3754 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
3755 else if (TREE_CODE (inner_decl) == ARRAY_REF)
3756 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
3757 returned_attrs = decl_attributes (&type,
3758 chainon (returned_attrs, attrs),
3759 attr_flags);
3761 else if (TREE_CODE (declarator) == ARRAY_REF)
3763 tree itype = NULL_TREE;
3764 tree size = TREE_OPERAND (declarator, 1);
3765 /* The index is a signed object `sizetype' bits wide. */
3766 tree index_type = c_common_signed_type (sizetype);
3768 array_ptr_quals = TREE_TYPE (declarator);
3769 array_parm_static = TREE_STATIC (declarator);
3771 declarator = TREE_OPERAND (declarator, 0);
3773 /* Check for some types that there cannot be arrays of. */
3775 if (VOID_TYPE_P (type))
3777 error ("declaration of `%s' as array of voids", name);
3778 type = error_mark_node;
3781 if (TREE_CODE (type) == FUNCTION_TYPE)
3783 error ("declaration of `%s' as array of functions", name);
3784 type = error_mark_node;
3787 if (pedantic && flexible_array_type_p (type))
3788 pedwarn ("invalid use of structure with flexible array member");
3790 if (size == error_mark_node)
3791 type = error_mark_node;
3793 if (type == error_mark_node)
3794 continue;
3796 /* If size was specified, set ITYPE to a range-type for that size.
3797 Otherwise, ITYPE remains null. finish_decl may figure it out
3798 from an initial value. */
3800 if (size)
3802 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3803 STRIP_TYPE_NOPS (size);
3805 if (! INTEGRAL_TYPE_P (TREE_TYPE (size)))
3807 error ("size of array `%s' has non-integer type", name);
3808 size = integer_one_node;
3811 if (pedantic && integer_zerop (size))
3812 pedwarn ("ISO C forbids zero-size array `%s'", name);
3814 if (TREE_CODE (size) == INTEGER_CST)
3816 constant_expression_warning (size);
3817 if (tree_int_cst_sgn (size) < 0)
3819 error ("size of array `%s' is negative", name);
3820 size = integer_one_node;
3823 else
3825 /* Make sure the array size remains visibly nonconstant
3826 even if it is (eg) a const variable with known value. */
3827 size_varies = 1;
3829 if (!flag_isoc99 && pedantic)
3831 if (TREE_CONSTANT (size))
3832 pedwarn ("ISO C90 forbids array `%s' whose size can't be evaluated",
3833 name);
3834 else
3835 pedwarn ("ISO C90 forbids variable-size array `%s'",
3836 name);
3840 if (integer_zerop (size))
3842 /* A zero-length array cannot be represented with an
3843 unsigned index type, which is what we'll get with
3844 build_index_type. Create an open-ended range instead. */
3845 itype = build_range_type (sizetype, size, NULL_TREE);
3847 else
3849 /* Compute the maximum valid index, that is, size - 1.
3850 Do the calculation in index_type, so that if it is
3851 a variable the computations will be done in the
3852 proper mode. */
3853 itype = fold (build (MINUS_EXPR, index_type,
3854 convert (index_type, size),
3855 convert (index_type, size_one_node)));
3857 /* If that overflowed, the array is too big.
3858 ??? While a size of INT_MAX+1 technically shouldn't
3859 cause an overflow (because we subtract 1), the overflow
3860 is recorded during the conversion to index_type, before
3861 the subtraction. Handling this case seems like an
3862 unnecessary complication. */
3863 if (TREE_OVERFLOW (itype))
3865 error ("size of array `%s' is too large", name);
3866 type = error_mark_node;
3867 continue;
3870 if (size_varies)
3872 /* We must be able to distinguish the
3873 SAVE_EXPR_CONTEXT for the variably-sized type
3874 so that we can set it correctly in
3875 set_save_expr_context. The convention is
3876 that all SAVE_EXPRs that need to be reset
3877 have NULL_TREE for their SAVE_EXPR_CONTEXT. */
3878 tree cfd = current_function_decl;
3879 if (decl_context == PARM)
3880 current_function_decl = NULL_TREE;
3881 itype = variable_size (itype);
3882 if (decl_context == PARM)
3883 current_function_decl = cfd;
3885 itype = build_index_type (itype);
3888 else if (decl_context == FIELD)
3890 if (pedantic && !flag_isoc99 && !in_system_header)
3891 pedwarn ("ISO C90 does not support flexible array members");
3893 /* ISO C99 Flexible array members are effectively identical
3894 to GCC's zero-length array extension. */
3895 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
3898 /* If pedantic, complain about arrays of incomplete types. */
3900 if (pedantic && !COMPLETE_TYPE_P (type))
3901 pedwarn ("array type has incomplete element type");
3903 /* Build the array type itself, then merge any constancy or
3904 volatility into the target type. We must do it in this order
3905 to ensure that the TYPE_MAIN_VARIANT field of the array type
3906 is set correctly. */
3908 type = build_array_type (type, itype);
3909 if (type_quals)
3910 type = c_build_qualified_type (type, type_quals);
3912 if (size_varies)
3913 C_TYPE_VARIABLE_SIZE (type) = 1;
3915 /* The GCC extension for zero-length arrays differs from
3916 ISO flexible array members in that sizeof yields zero. */
3917 if (size && integer_zerop (size))
3919 layout_type (type);
3920 TYPE_SIZE (type) = bitsize_zero_node;
3921 TYPE_SIZE_UNIT (type) = size_zero_node;
3923 if (decl_context != PARM
3924 && (array_ptr_quals != NULL_TREE || array_parm_static))
3926 error ("static or type qualifiers in non-parameter array declarator");
3927 array_ptr_quals = NULL_TREE;
3928 array_parm_static = 0;
3931 else if (TREE_CODE (declarator) == CALL_EXPR)
3933 tree arg_types;
3935 /* Declaring a function type.
3936 Make sure we have a valid type for the function to return. */
3937 if (type == error_mark_node)
3938 continue;
3940 size_varies = 0;
3942 /* Warn about some types functions can't return. */
3944 if (TREE_CODE (type) == FUNCTION_TYPE)
3946 error ("`%s' declared as function returning a function", name);
3947 type = integer_type_node;
3949 if (TREE_CODE (type) == ARRAY_TYPE)
3951 error ("`%s' declared as function returning an array", name);
3952 type = integer_type_node;
3955 /* Construct the function type and go to the next
3956 inner layer of declarator. */
3958 arg_types = grokparms (TREE_OPERAND (declarator, 1),
3959 funcdef_flag
3960 /* Say it's a definition
3961 only for the CALL_EXPR
3962 closest to the identifier. */
3963 && TREE_CODE (TREE_OPERAND (declarator, 0)) == IDENTIFIER_NODE);
3964 /* Type qualifiers before the return type of the function
3965 qualify the return type, not the function type. */
3966 if (type_quals)
3968 /* Type qualifiers on a function return type are normally
3969 permitted by the standard but have no effect, so give a
3970 warning at -Wextra. Qualifiers on a void return type have
3971 meaning as a GNU extension, and are banned on function
3972 definitions in ISO C. FIXME: strictly we shouldn't
3973 pedwarn for qualified void return types except on function
3974 definitions, but not doing so could lead to the undesirable
3975 state of a "volatile void" function return type not being
3976 warned about, and a use of the function being compiled
3977 with GNU semantics, with no diagnostics under -pedantic. */
3978 if (VOID_TYPE_P (type) && pedantic && !in_system_header)
3979 pedwarn ("ISO C forbids qualified void function return type");
3980 else if (extra_warnings
3981 && !(VOID_TYPE_P (type)
3982 && type_quals == TYPE_QUAL_VOLATILE))
3983 warning ("type qualifiers ignored on function return type");
3985 type = c_build_qualified_type (type, type_quals);
3987 type_quals = TYPE_UNQUALIFIED;
3989 type = build_function_type (type, arg_types);
3990 declarator = TREE_OPERAND (declarator, 0);
3992 /* Set the TYPE_CONTEXTs for each tagged type which is local to
3993 the formal parameter list of this FUNCTION_TYPE to point to
3994 the FUNCTION_TYPE node itself. */
3997 tree link;
3999 for (link = last_function_parm_tags;
4000 link;
4001 link = TREE_CHAIN (link))
4002 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4005 else if (TREE_CODE (declarator) == INDIRECT_REF)
4007 /* Merge any constancy or volatility into the target type
4008 for the pointer. */
4010 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4011 && type_quals)
4012 pedwarn ("ISO C forbids qualified function types");
4013 if (type_quals)
4014 type = c_build_qualified_type (type, type_quals);
4015 type_quals = TYPE_UNQUALIFIED;
4016 size_varies = 0;
4018 type = build_pointer_type (type);
4020 /* Process a list of type modifier keywords
4021 (such as const or volatile) that were given inside the `*'. */
4023 if (TREE_TYPE (declarator))
4025 tree typemodlist;
4026 int erred = 0;
4028 constp = 0;
4029 volatilep = 0;
4030 restrictp = 0;
4031 for (typemodlist = TREE_TYPE (declarator); typemodlist;
4032 typemodlist = TREE_CHAIN (typemodlist))
4034 tree qualifier = TREE_VALUE (typemodlist);
4036 if (C_IS_RESERVED_WORD (qualifier))
4038 if (C_RID_CODE (qualifier) == RID_CONST)
4039 constp++;
4040 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4041 volatilep++;
4042 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4043 restrictp++;
4044 else
4045 erred++;
4047 else
4048 erred++;
4051 if (erred)
4052 error ("invalid type modifier within pointer declarator");
4053 if (constp > 1 && ! flag_isoc99)
4054 pedwarn ("duplicate `const'");
4055 if (volatilep > 1 && ! flag_isoc99)
4056 pedwarn ("duplicate `volatile'");
4057 if (restrictp > 1 && ! flag_isoc99)
4058 pedwarn ("duplicate `restrict'");
4060 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4061 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4062 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4065 declarator = TREE_OPERAND (declarator, 0);
4067 else
4068 abort ();
4072 /* Now TYPE has the actual type. */
4074 /* Did array size calculations overflow? */
4076 if (TREE_CODE (type) == ARRAY_TYPE
4077 && COMPLETE_TYPE_P (type)
4078 && TREE_OVERFLOW (TYPE_SIZE (type)))
4080 error ("size of array `%s' is too large", name);
4081 /* If we proceed with the array type as it is, we'll eventually
4082 crash in tree_low_cst(). */
4083 type = error_mark_node;
4086 /* If this is declaring a typedef name, return a TYPE_DECL. */
4088 if (specbits & (1 << (int) RID_TYPEDEF))
4090 tree decl;
4091 /* Note that the grammar rejects storage classes
4092 in typenames, fields or parameters */
4093 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4094 && type_quals)
4095 pedwarn ("ISO C forbids qualified function types");
4096 if (type_quals)
4097 type = c_build_qualified_type (type, type_quals);
4098 decl = build_decl (TYPE_DECL, declarator, type);
4099 if ((specbits & (1 << (int) RID_SIGNED))
4100 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
4101 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4102 decl_attributes (&decl, returned_attrs, 0);
4103 return decl;
4106 /* Detect the case of an array type of unspecified size
4107 which came, as such, direct from a typedef name.
4108 We must copy the type, so that each identifier gets
4109 a distinct type, so that each identifier's size can be
4110 controlled separately by its own initializer. */
4112 if (type != 0 && typedef_type != 0
4113 && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0
4114 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
4116 type = build_array_type (TREE_TYPE (type), 0);
4117 if (size_varies)
4118 C_TYPE_VARIABLE_SIZE (type) = 1;
4121 /* If this is a type name (such as, in a cast or sizeof),
4122 compute the type and return it now. */
4124 if (decl_context == TYPENAME)
4126 /* Note that the grammar rejects storage classes
4127 in typenames, fields or parameters */
4128 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4129 && type_quals)
4130 pedwarn ("ISO C forbids const or volatile function types");
4131 if (type_quals)
4132 type = c_build_qualified_type (type, type_quals);
4133 decl_attributes (&type, returned_attrs, 0);
4134 return type;
4137 /* Aside from typedefs and type names (handle above),
4138 `void' at top level (not within pointer)
4139 is allowed only in public variables.
4140 We don't complain about parms either, but that is because
4141 a better error message can be made later. */
4143 if (VOID_TYPE_P (type) && decl_context != PARM
4144 && ! ((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4145 && ((specbits & (1 << (int) RID_EXTERN))
4146 || (current_scope == global_scope
4147 && !(specbits
4148 & ((1 << (int) RID_STATIC) | (1 << (int) RID_REGISTER)))))))
4150 error ("variable or field `%s' declared void", name);
4151 type = integer_type_node;
4154 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4155 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4158 tree decl;
4160 if (decl_context == PARM)
4162 tree type_as_written;
4163 tree promoted_type;
4165 /* A parameter declared as an array of T is really a pointer to T.
4166 One declared as a function is really a pointer to a function. */
4168 if (TREE_CODE (type) == ARRAY_TYPE)
4170 /* Transfer const-ness of array into that of type pointed to. */
4171 type = TREE_TYPE (type);
4172 if (type_quals)
4173 type = c_build_qualified_type (type, type_quals);
4174 type = build_pointer_type (type);
4175 type_quals = TYPE_UNQUALIFIED;
4176 if (array_ptr_quals)
4178 tree new_ptr_quals, new_ptr_attrs;
4179 int erred = 0;
4180 split_specs_attrs (array_ptr_quals, &new_ptr_quals, &new_ptr_attrs);
4181 /* We don't yet implement attributes in this context. */
4182 if (new_ptr_attrs != NULL_TREE)
4183 warning ("attributes in parameter array declarator ignored");
4185 constp = 0;
4186 volatilep = 0;
4187 restrictp = 0;
4188 for (; new_ptr_quals; new_ptr_quals = TREE_CHAIN (new_ptr_quals))
4190 tree qualifier = TREE_VALUE (new_ptr_quals);
4192 if (C_IS_RESERVED_WORD (qualifier))
4194 if (C_RID_CODE (qualifier) == RID_CONST)
4195 constp++;
4196 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4197 volatilep++;
4198 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4199 restrictp++;
4200 else
4201 erred++;
4203 else
4204 erred++;
4207 if (erred)
4208 error ("invalid type modifier within array declarator");
4210 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4211 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4212 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4214 size_varies = 0;
4216 else if (TREE_CODE (type) == FUNCTION_TYPE)
4218 if (pedantic && type_quals)
4219 pedwarn ("ISO C forbids qualified function types");
4220 if (type_quals)
4221 type = c_build_qualified_type (type, type_quals);
4222 type = build_pointer_type (type);
4223 type_quals = TYPE_UNQUALIFIED;
4225 else if (type_quals)
4226 type = c_build_qualified_type (type, type_quals);
4228 type_as_written = type;
4230 decl = build_decl (PARM_DECL, declarator, type);
4231 if (size_varies)
4232 C_DECL_VARIABLE_SIZE (decl) = 1;
4234 /* Compute the type actually passed in the parmlist,
4235 for the case where there is no prototype.
4236 (For example, shorts and chars are passed as ints.)
4237 When there is a prototype, this is overridden later. */
4239 if (type == error_mark_node)
4240 promoted_type = type;
4241 else
4242 promoted_type = c_type_promotes_to (type);
4244 DECL_ARG_TYPE (decl) = promoted_type;
4245 DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
4247 else if (decl_context == FIELD)
4249 /* Structure field. It may not be a function. */
4251 if (TREE_CODE (type) == FUNCTION_TYPE)
4253 error ("field `%s' declared as a function", name);
4254 type = build_pointer_type (type);
4256 else if (TREE_CODE (type) != ERROR_MARK
4257 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4259 error ("field `%s' has incomplete type", name);
4260 type = error_mark_node;
4262 /* Move type qualifiers down to element of an array. */
4263 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4264 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4265 type_quals),
4266 TYPE_DOMAIN (type));
4267 decl = build_decl (FIELD_DECL, declarator, type);
4268 DECL_NONADDRESSABLE_P (decl) = bitfield;
4270 if (size_varies)
4271 C_DECL_VARIABLE_SIZE (decl) = 1;
4273 else if (TREE_CODE (type) == FUNCTION_TYPE)
4275 /* Every function declaration is "external"
4276 except for those which are inside a function body
4277 in which `auto' is used.
4278 That is a case not specified by ANSI C,
4279 and we use it for forward declarations for nested functions. */
4280 int extern_ref = (!(specbits & (1 << (int) RID_AUTO))
4281 || current_scope == global_scope);
4283 if (specbits & (1 << (int) RID_AUTO)
4284 && (pedantic || current_scope == global_scope))
4285 pedwarn ("invalid storage class for function `%s'", name);
4286 if (specbits & (1 << (int) RID_REGISTER))
4287 error ("invalid storage class for function `%s'", name);
4288 if (specbits & (1 << (int) RID_THREAD))
4289 error ("invalid storage class for function `%s'", name);
4290 /* Function declaration not at file scope.
4291 Storage classes other than `extern' are not allowed
4292 and `extern' makes no difference. */
4293 if (current_scope != global_scope
4294 && (specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_INLINE)))
4295 && pedantic)
4296 pedwarn ("invalid storage class for function `%s'", name);
4298 decl = build_decl (FUNCTION_DECL, declarator, type);
4299 decl = build_decl_attribute_variant (decl, decl_attr);
4301 DECL_LANG_SPECIFIC (decl)
4302 = ggc_alloc_cleared (sizeof (struct lang_decl));
4304 if (pedantic && type_quals && ! DECL_IN_SYSTEM_HEADER (decl))
4305 pedwarn ("ISO C forbids qualified function types");
4307 /* GNU C interprets a `volatile void' return type to indicate
4308 that the function does not return. */
4309 if ((type_quals & TYPE_QUAL_VOLATILE)
4310 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4311 warning ("`noreturn' function returns non-void value");
4313 if (extern_ref)
4314 DECL_EXTERNAL (decl) = 1;
4315 /* Record absence of global scope for `static' or `auto'. */
4316 TREE_PUBLIC (decl)
4317 = !(specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_AUTO)));
4319 if (defaulted_int)
4320 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4322 /* Record presence of `inline', if it is reasonable. */
4323 if (MAIN_NAME_P (declarator))
4325 if (inlinep)
4326 warning ("cannot inline function `main'");
4328 else if (inlinep)
4330 /* Assume that otherwise the function can be inlined. */
4331 DECL_DECLARED_INLINE_P (decl) = 1;
4333 /* Do not mark bare declarations as DECL_INLINE. Doing so
4334 in the presence of multiple declarations can result in
4335 the abstract origin pointing between the declarations,
4336 which will confuse dwarf2out. */
4337 if (initialized)
4339 DECL_INLINE (decl) = 1;
4340 if (specbits & (1 << (int) RID_EXTERN))
4341 current_extern_inline = 1;
4344 /* If -finline-functions, assume it can be inlined. This does
4345 two things: let the function be deferred until it is actually
4346 needed, and let dwarf2 know that the function is inlinable. */
4347 else if (flag_inline_trees == 2 && initialized)
4349 if (!DECL_INLINE (decl))
4350 DID_INLINE_FUNC (decl) = 1;
4351 DECL_INLINE (decl) = 1;
4352 DECL_DECLARED_INLINE_P (decl) = 0;
4355 else
4357 /* It's a variable. */
4358 /* An uninitialized decl with `extern' is a reference. */
4359 int extern_ref = !initialized && (specbits & (1 << (int) RID_EXTERN));
4361 /* Move type qualifiers down to element of an array. */
4362 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4364 int saved_align = TYPE_ALIGN(type);
4365 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4366 type_quals),
4367 TYPE_DOMAIN (type));
4368 TYPE_ALIGN (type) = saved_align;
4370 else if (type_quals)
4371 type = c_build_qualified_type (type, type_quals);
4373 /* It is invalid to create an `extern' declaration for a
4374 variable if there is a global declaration that is
4375 `static'. */
4376 if (extern_ref && current_scope != global_scope)
4378 tree global_decl;
4380 global_decl = identifier_global_value (declarator);
4381 if (global_decl
4382 && TREE_CODE (global_decl) == VAR_DECL
4383 && !TREE_PUBLIC (global_decl))
4384 error ("variable previously declared `static' redeclared "
4385 "`extern'");
4388 decl = build_decl (VAR_DECL, declarator, type);
4389 if (size_varies)
4390 C_DECL_VARIABLE_SIZE (decl) = 1;
4392 if (inlinep)
4393 pedwarn ("%Hvariable '%D' declared `inline'",
4394 &DECL_SOURCE_LOCATION (decl), decl);
4396 DECL_EXTERNAL (decl) = extern_ref;
4398 /* At file scope, the presence of a `static' or `register' storage
4399 class specifier, or the absence of all storage class specifiers
4400 makes this declaration a definition (perhaps tentative). Also,
4401 the absence of both `static' and `register' makes it public. */
4402 if (current_scope == global_scope)
4404 TREE_PUBLIC (decl) = !(specbits & ((1 << (int) RID_STATIC)
4405 | (1 << (int) RID_REGISTER)));
4406 TREE_STATIC (decl) = !extern_ref;
4408 /* Not at file scope, only `static' makes a static definition. */
4409 else
4411 TREE_STATIC (decl) = (specbits & (1 << (int) RID_STATIC)) != 0;
4412 TREE_PUBLIC (decl) = extern_ref;
4415 if (specbits & 1 << (int) RID_THREAD)
4417 if (targetm.have_tls)
4418 DECL_THREAD_LOCAL (decl) = 1;
4419 else
4420 /* A mere warning is sure to result in improper semantics
4421 at runtime. Don't bother to allow this to compile. */
4422 error ("thread-local storage not supported for this target");
4426 /* Record `register' declaration for warnings on &
4427 and in case doing stupid register allocation. */
4429 if (specbits & (1 << (int) RID_REGISTER))
4430 DECL_REGISTER (decl) = 1;
4432 /* Record constancy and volatility. */
4433 c_apply_type_quals_to_decl (type_quals, decl);
4435 /* If a type has volatile components, it should be stored in memory.
4436 Otherwise, the fact that those components are volatile
4437 will be ignored, and would even crash the compiler. */
4438 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
4439 c_mark_addressable (decl);
4441 #ifdef ENABLE_CHECKING
4442 /* This is the earliest point at which we might know the assembler
4443 name of a variable. Thus, if it's known before this, die horribly. */
4444 if (DECL_ASSEMBLER_NAME_SET_P (decl))
4445 abort ();
4446 #endif
4448 decl_attributes (&decl, returned_attrs, 0);
4450 return decl;
4454 /* Decode the parameter-list info for a function type or function definition.
4455 The argument is the value returned by `get_parm_info' (or made in parse.y
4456 if there is an identifier list instead of a parameter decl list).
4457 These two functions are separate because when a function returns
4458 or receives functions then each is called multiple times but the order
4459 of calls is different. The last call to `grokparms' is always the one
4460 that contains the formal parameter names of a function definition.
4462 Store in `last_function_parms' a chain of the decls of parms.
4463 Also store in `last_function_parm_tags' a chain of the struct, union,
4464 and enum tags declared among the parms.
4466 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4468 FUNCDEF_FLAG is nonzero for a function definition, 0 for
4469 a mere declaration. A nonempty identifier-list gets an error message
4470 when FUNCDEF_FLAG is zero. */
4472 static tree
4473 grokparms (tree parms_info, int funcdef_flag)
4475 tree first_parm = TREE_CHAIN (parms_info);
4477 last_function_parms = TREE_PURPOSE (parms_info);
4478 last_function_parm_tags = TREE_VALUE (parms_info);
4480 if (warn_strict_prototypes && first_parm == 0 && !funcdef_flag
4481 && !in_system_header)
4482 warning ("function declaration isn't a prototype");
4484 if (first_parm != 0
4485 && TREE_CODE (TREE_VALUE (first_parm)) == IDENTIFIER_NODE)
4487 if (! funcdef_flag)
4488 pedwarn ("parameter names (without types) in function declaration");
4490 last_function_parms = first_parm;
4491 return 0;
4493 else
4495 tree parm;
4496 tree typelt;
4497 /* If the arg types are incomplete in a declaration,
4498 they must include undefined tags.
4499 These tags can never be defined in the scope of the declaration,
4500 so the types can never be completed,
4501 and no call can be compiled successfully. */
4503 for (parm = last_function_parms, typelt = first_parm;
4504 parm;
4505 parm = TREE_CHAIN (parm))
4506 /* Skip over any enumeration constants declared here. */
4507 if (TREE_CODE (parm) == PARM_DECL)
4509 /* Barf if the parameter itself has an incomplete type. */
4510 tree type = TREE_VALUE (typelt);
4511 if (type == error_mark_node)
4512 continue;
4513 if (!COMPLETE_TYPE_P (type))
4515 if (funcdef_flag && DECL_NAME (parm) != 0)
4516 error ("parameter `%s' has incomplete type",
4517 IDENTIFIER_POINTER (DECL_NAME (parm)));
4518 else
4519 warning ("parameter has incomplete type");
4520 if (funcdef_flag)
4522 TREE_VALUE (typelt) = error_mark_node;
4523 TREE_TYPE (parm) = error_mark_node;
4526 typelt = TREE_CHAIN (typelt);
4529 return first_parm;
4533 /* Return a tree_list node with info on a parameter list just parsed.
4534 The TREE_PURPOSE is a list of decls of those parms.
4535 The TREE_VALUE is a list of structure, union and enum tags defined.
4536 The TREE_CHAIN is a list of argument types to go in the FUNCTION_TYPE.
4537 This tree_list node is later fed to `grokparms'.
4539 VOID_AT_END nonzero means append `void' to the end of the type-list.
4540 Zero means the parmlist ended with an ellipsis so don't append `void'. */
4542 tree
4543 get_parm_info (int void_at_end)
4545 tree decl, t;
4546 tree types = 0;
4547 int erred = 0;
4548 tree tags = gettags ();
4549 tree parms = getdecls ();
4550 tree new_parms = 0;
4551 tree order = current_scope->parm_order;
4553 /* Just `void' (and no ellipsis) is special. There are really no parms.
4554 But if the `void' is qualified (by `const' or `volatile') or has a
4555 storage class specifier (`register'), then the behavior is undefined;
4556 by not counting it as the special case of `void' we will cause an
4557 error later. Typedefs for `void' are OK (see DR#157). */
4558 if (void_at_end && parms != 0
4559 && TREE_CHAIN (parms) == 0
4560 && VOID_TYPE_P (TREE_TYPE (parms))
4561 && ! TREE_THIS_VOLATILE (parms)
4562 && ! TREE_READONLY (parms)
4563 && ! DECL_REGISTER (parms)
4564 && DECL_NAME (parms) == 0)
4566 parms = NULL_TREE;
4567 storedecls (NULL_TREE);
4568 return tree_cons (NULL_TREE, NULL_TREE,
4569 tree_cons (NULL_TREE, void_type_node, NULL_TREE));
4572 /* Extract enumerator values and other non-parms declared with the parms.
4573 Likewise any forward parm decls that didn't have real parm decls. */
4574 for (decl = parms; decl;)
4576 tree next = TREE_CHAIN (decl);
4578 if (TREE_CODE (decl) != PARM_DECL)
4580 TREE_CHAIN (decl) = new_parms;
4581 new_parms = decl;
4583 else if (TREE_ASM_WRITTEN (decl))
4585 error ("%Hparameter '%D' has just a forward declaration",
4586 &DECL_SOURCE_LOCATION (decl), decl);
4587 TREE_CHAIN (decl) = new_parms;
4588 new_parms = decl;
4590 decl = next;
4593 /* Put the parm decls back in the order they were in in the parm list. */
4594 for (t = order; t; t = TREE_CHAIN (t))
4596 if (TREE_CHAIN (t))
4597 TREE_CHAIN (TREE_VALUE (t)) = TREE_VALUE (TREE_CHAIN (t));
4598 else
4599 TREE_CHAIN (TREE_VALUE (t)) = 0;
4602 new_parms = chainon (order ? nreverse (TREE_VALUE (order)) : 0,
4603 new_parms);
4605 /* Store the parmlist in the scope structure since the old one
4606 is no longer a valid list. (We have changed the chain pointers.) */
4607 storedecls (new_parms);
4609 for (decl = new_parms; decl; decl = TREE_CHAIN (decl))
4610 /* There may also be declarations for enumerators if an enumeration
4611 type is declared among the parms. Ignore them here. */
4612 if (TREE_CODE (decl) == PARM_DECL)
4614 /* Since there is a prototype,
4615 args are passed in their declared types. */
4616 tree type = TREE_TYPE (decl);
4617 DECL_ARG_TYPE (decl) = type;
4618 if (PROMOTE_PROTOTYPES
4619 && INTEGRAL_TYPE_P (type)
4620 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4621 DECL_ARG_TYPE (decl) = integer_type_node;
4623 types = tree_cons (NULL_TREE, TREE_TYPE (decl), types);
4624 if (VOID_TYPE_P (TREE_VALUE (types)) && ! erred
4625 && DECL_NAME (decl) == 0)
4627 error ("`void' in parameter list must be the entire list");
4628 erred = 1;
4632 if (void_at_end)
4633 return tree_cons (new_parms, tags,
4634 nreverse (tree_cons (NULL_TREE, void_type_node, types)));
4636 return tree_cons (new_parms, tags, nreverse (types));
4639 /* At end of parameter list, warn about any struct, union or enum tags
4640 defined within. Do so because these types cannot ever become complete. */
4642 void
4643 parmlist_tags_warning (void)
4645 tree elt;
4646 static int already;
4648 for (elt = current_scope->tags; elt; elt = TREE_CHAIN (elt))
4650 enum tree_code code = TREE_CODE (TREE_VALUE (elt));
4651 /* An anonymous union parm type is meaningful as a GNU extension.
4652 So don't warn for that. */
4653 if (code == UNION_TYPE && TREE_PURPOSE (elt) == 0 && !pedantic)
4654 continue;
4655 if (TREE_PURPOSE (elt) != 0)
4657 if (code == RECORD_TYPE)
4658 warning ("`struct %s' declared inside parameter list",
4659 IDENTIFIER_POINTER (TREE_PURPOSE (elt)));
4660 else if (code == UNION_TYPE)
4661 warning ("`union %s' declared inside parameter list",
4662 IDENTIFIER_POINTER (TREE_PURPOSE (elt)));
4663 else
4664 warning ("`enum %s' declared inside parameter list",
4665 IDENTIFIER_POINTER (TREE_PURPOSE (elt)));
4667 else
4669 /* For translation these need to be separate warnings */
4670 if (code == RECORD_TYPE)
4671 warning ("anonymous struct declared inside parameter list");
4672 else if (code == UNION_TYPE)
4673 warning ("anonymous union declared inside parameter list");
4674 else
4675 warning ("anonymous enum declared inside parameter list");
4677 if (! already)
4679 warning ("its scope is only this definition or declaration, which is probably not what you want");
4680 already = 1;
4685 /* Get the struct, enum or union (CODE says which) with tag NAME.
4686 Define the tag as a forward-reference if it is not defined. */
4688 tree
4689 xref_tag (enum tree_code code, tree name)
4691 /* If a cross reference is requested, look up the type
4692 already defined for this tag and return it. */
4694 tree ref = lookup_tag (code, name, 0);
4695 /* If this is the right type of tag, return what we found.
4696 (This reference will be shadowed by shadow_tag later if appropriate.)
4697 If this is the wrong type of tag, do not return it. If it was the
4698 wrong type in the same scope, we will have had an error
4699 message already; if in a different scope and declaring
4700 a name, pending_xref_error will give an error message; but if in a
4701 different scope and not declaring a name, this tag should
4702 shadow the previous declaration of a different type of tag, and
4703 this would not work properly if we return the reference found.
4704 (For example, with "struct foo" in an outer scope, "union foo;"
4705 must shadow that tag with a new one of union type.) */
4706 if (ref && TREE_CODE (ref) == code)
4707 return ref;
4709 /* If no such tag is yet defined, create a forward-reference node
4710 and record it as the "definition".
4711 When a real declaration of this type is found,
4712 the forward-reference will be altered into a real type. */
4714 ref = make_node (code);
4715 if (code == ENUMERAL_TYPE)
4717 /* Give the type a default layout like unsigned int
4718 to avoid crashing if it does not get defined. */
4719 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
4720 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
4721 TYPE_USER_ALIGN (ref) = 0;
4722 TREE_UNSIGNED (ref) = 1;
4723 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
4724 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
4725 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
4728 pushtag (name, ref);
4730 return ref;
4733 /* Make sure that the tag NAME is defined *in the current scope*
4734 at least as a forward reference.
4735 CODE says which kind of tag NAME ought to be. */
4737 tree
4738 start_struct (enum tree_code code, tree name)
4740 /* If there is already a tag defined at this scope
4741 (as a forward reference), just return it. */
4743 tree ref = 0;
4745 if (name != 0)
4746 ref = lookup_tag (code, name, 1);
4747 if (ref && TREE_CODE (ref) == code)
4749 if (TYPE_FIELDS (ref))
4751 if (code == UNION_TYPE)
4752 error ("redefinition of `union %s'", IDENTIFIER_POINTER (name));
4753 else
4754 error ("redefinition of `struct %s'", IDENTIFIER_POINTER (name));
4757 else
4759 /* Otherwise create a forward-reference just so the tag is in scope. */
4761 ref = make_node (code);
4762 pushtag (name, ref);
4765 C_TYPE_BEING_DEFINED (ref) = 1;
4766 TYPE_PACKED (ref) = flag_pack_struct;
4767 return ref;
4770 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
4771 of a structure component, returning a FIELD_DECL node.
4772 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node.
4774 This is done during the parsing of the struct declaration.
4775 The FIELD_DECL nodes are chained together and the lot of them
4776 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
4778 tree
4779 grokfield (tree declarator, tree declspecs, tree width)
4781 tree value;
4783 if (declarator == NULL_TREE && width == NULL_TREE)
4785 /* This is an unnamed decl.
4787 If we have something of the form "union { list } ;" then this
4788 is the anonymous union extension. Similarly for struct.
4790 If this is something of the form "struct foo;", then
4791 If MS extensions are enabled, this is handled as an
4792 anonymous struct.
4793 Otherwise this is a forward declaration of a structure tag.
4795 If this is something of the form "foo;" and foo is a TYPE_DECL, then
4796 If MS extensions are enabled and foo names a structure, then
4797 again this is an anonymous struct.
4798 Otherwise this is an error.
4800 Oh what a horrid tangled web we weave. I wonder if MS consciously
4801 took this from Plan 9 or if it was an accident of implementation
4802 that took root before someone noticed the bug... */
4804 tree type = TREE_VALUE (declspecs);
4806 if (flag_ms_extensions && TREE_CODE (type) == TYPE_DECL)
4807 type = TREE_TYPE (type);
4808 if (TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE)
4810 if (flag_ms_extensions)
4811 ; /* ok */
4812 else if (flag_iso)
4813 goto warn_unnamed_field;
4814 else if (TYPE_NAME (type) == NULL)
4815 ; /* ok */
4816 else
4817 goto warn_unnamed_field;
4819 else
4821 warn_unnamed_field:
4822 warning ("declaration does not declare anything");
4823 return NULL_TREE;
4827 value = grokdeclarator (declarator, declspecs, width ? BITFIELD : FIELD, 0);
4829 finish_decl (value, NULL_TREE, NULL_TREE);
4830 DECL_INITIAL (value) = width;
4832 if (c_dialect_objc ())
4833 objc_check_decl (value);
4834 return value;
4837 /* Generate an error for any duplicate field names in FIELDLIST. Munge
4838 the list such that this does not present a problem later. */
4840 static void
4841 detect_field_duplicates (tree fieldlist)
4843 tree x, y;
4844 int timeout = 10;
4846 /* First, see if there are more than "a few" fields.
4847 This is trivially true if there are zero or one fields. */
4848 if (!fieldlist)
4849 return;
4850 x = TREE_CHAIN (fieldlist);
4851 if (!x)
4852 return;
4853 do {
4854 timeout--;
4855 x = TREE_CHAIN (x);
4856 } while (timeout > 0 && x);
4858 /* If there were "few" fields, avoid the overhead of allocating
4859 a hash table. Instead just do the nested traversal thing. */
4860 if (timeout > 0)
4862 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
4863 if (DECL_NAME (x))
4865 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
4866 if (DECL_NAME (y) == DECL_NAME (x))
4868 error ("%Hduplicate member '%D'",
4869 &DECL_SOURCE_LOCATION (x), x);
4870 DECL_NAME (x) = NULL_TREE;
4874 else
4876 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
4877 void **slot;
4879 for (x = fieldlist; x ; x = TREE_CHAIN (x))
4880 if ((y = DECL_NAME (x)) != 0)
4882 slot = htab_find_slot (htab, y, INSERT);
4883 if (*slot)
4885 error ("%Hduplicate member '%D'",
4886 &DECL_SOURCE_LOCATION (x), x);
4887 DECL_NAME (x) = NULL_TREE;
4889 *slot = y;
4892 htab_delete (htab);
4896 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
4897 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
4898 ATTRIBUTES are attributes to be applied to the structure. */
4900 tree
4901 finish_struct (tree t, tree fieldlist, tree attributes)
4903 tree x;
4904 int toplevel = global_scope == current_scope;
4905 int saw_named_field;
4907 /* If this type was previously laid out as a forward reference,
4908 make sure we lay it out again. */
4910 TYPE_SIZE (t) = 0;
4912 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
4914 /* Nameless union parm types are useful as GCC extension. */
4915 if (! (TREE_CODE (t) == UNION_TYPE && TYPE_NAME (t) == 0) && !pedantic)
4916 /* Otherwise, warn about any struct or union def. in parmlist. */
4917 if (in_parm_level_p ())
4919 if (pedantic)
4920 pedwarn ("%s defined inside parms",
4921 TREE_CODE (t) == UNION_TYPE ? _("union") : _("structure"));
4922 else
4923 warning ("%s defined inside parms",
4924 TREE_CODE (t) == UNION_TYPE ? _("union") : _("structure"));
4927 if (pedantic)
4929 for (x = fieldlist; x; x = TREE_CHAIN (x))
4930 if (DECL_NAME (x) != 0)
4931 break;
4933 if (x == 0)
4934 pedwarn ("%s has no %s",
4935 TREE_CODE (t) == UNION_TYPE ? _("union") : _("struct"),
4936 fieldlist ? _("named members") : _("members"));
4939 /* Install struct as DECL_CONTEXT of each field decl.
4940 Also process specified field sizes,m which is found in the DECL_INITIAL.
4941 Store 0 there, except for ": 0" fields (so we can find them
4942 and delete them, below). */
4944 saw_named_field = 0;
4945 for (x = fieldlist; x; x = TREE_CHAIN (x))
4947 DECL_CONTEXT (x) = t;
4948 DECL_PACKED (x) |= TYPE_PACKED (t);
4950 /* If any field is const, the structure type is pseudo-const. */
4951 if (TREE_READONLY (x))
4952 C_TYPE_FIELDS_READONLY (t) = 1;
4953 else
4955 /* A field that is pseudo-const makes the structure likewise. */
4956 tree t1 = TREE_TYPE (x);
4957 while (TREE_CODE (t1) == ARRAY_TYPE)
4958 t1 = TREE_TYPE (t1);
4959 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
4960 && C_TYPE_FIELDS_READONLY (t1))
4961 C_TYPE_FIELDS_READONLY (t) = 1;
4964 /* Any field that is volatile means variables of this type must be
4965 treated in some ways as volatile. */
4966 if (TREE_THIS_VOLATILE (x))
4967 C_TYPE_FIELDS_VOLATILE (t) = 1;
4969 /* Any field of nominal variable size implies structure is too. */
4970 if (C_DECL_VARIABLE_SIZE (x))
4971 C_TYPE_VARIABLE_SIZE (t) = 1;
4973 /* Detect invalid nested redefinition. */
4974 if (TREE_TYPE (x) == t)
4975 error ("nested redefinition of `%s'",
4976 IDENTIFIER_POINTER (TYPE_NAME (t)));
4978 /* Detect invalid bit-field size. */
4979 if (DECL_INITIAL (x))
4980 STRIP_NOPS (DECL_INITIAL (x));
4981 if (DECL_INITIAL (x))
4983 if (TREE_CODE (DECL_INITIAL (x)) == INTEGER_CST)
4984 constant_expression_warning (DECL_INITIAL (x));
4985 else
4987 error ("%Hbit-field '%D' width not an integer constant",
4988 &DECL_SOURCE_LOCATION (x), x);
4989 DECL_INITIAL (x) = NULL;
4993 /* Detect invalid bit-field type. */
4994 if (DECL_INITIAL (x)
4995 && TREE_CODE (TREE_TYPE (x)) != INTEGER_TYPE
4996 && TREE_CODE (TREE_TYPE (x)) != BOOLEAN_TYPE
4997 && TREE_CODE (TREE_TYPE (x)) != ENUMERAL_TYPE)
4999 error ("%Hbit-field '%D' has invalid type",
5000 &DECL_SOURCE_LOCATION (x), x);
5001 DECL_INITIAL (x) = NULL;
5004 if (DECL_INITIAL (x) && pedantic
5005 && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != integer_type_node
5006 && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != unsigned_type_node
5007 && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != boolean_type_node
5008 /* Accept an enum that's equivalent to int or unsigned int. */
5009 && !(TREE_CODE (TREE_TYPE (x)) == ENUMERAL_TYPE
5010 && (TYPE_PRECISION (TREE_TYPE (x))
5011 == TYPE_PRECISION (integer_type_node))))
5012 pedwarn ("%Hbit-field '%D' type invalid in ISO C",
5013 &DECL_SOURCE_LOCATION (x), x);
5015 /* Detect and ignore out of range field width and process valid
5016 field widths. */
5017 if (DECL_INITIAL (x))
5019 int max_width
5020 = (TYPE_MAIN_VARIANT (TREE_TYPE (x)) == boolean_type_node
5021 ? CHAR_TYPE_SIZE : TYPE_PRECISION (TREE_TYPE (x)));
5023 if (tree_int_cst_sgn (DECL_INITIAL (x)) < 0)
5024 error ("%Hnegative width in bit-field '%D'",
5025 &DECL_SOURCE_LOCATION (x), x);
5026 else if (0 < compare_tree_int (DECL_INITIAL (x), max_width))
5027 pedwarn ("%Hwidth of '%D' exceeds its type",
5028 &DECL_SOURCE_LOCATION (x), x);
5029 else if (integer_zerop (DECL_INITIAL (x)) && DECL_NAME (x) != 0)
5030 error ("%Hzero width for bit-field '%D'",
5031 &DECL_SOURCE_LOCATION (x), x);
5032 else
5034 /* The test above has assured us that TREE_INT_CST_HIGH is 0. */
5035 unsigned HOST_WIDE_INT width
5036 = tree_low_cst (DECL_INITIAL (x), 1);
5038 if (TREE_CODE (TREE_TYPE (x)) == ENUMERAL_TYPE
5039 && (width < min_precision (TYPE_MIN_VALUE (TREE_TYPE (x)),
5040 TREE_UNSIGNED (TREE_TYPE (x)))
5041 || (width
5042 < min_precision (TYPE_MAX_VALUE (TREE_TYPE (x)),
5043 TREE_UNSIGNED (TREE_TYPE (x))))))
5044 warning ("%H'%D' is narrower than values of its type",
5045 &DECL_SOURCE_LOCATION (x), x);
5047 DECL_SIZE (x) = bitsize_int (width);
5048 DECL_BIT_FIELD (x) = 1;
5049 SET_DECL_C_BIT_FIELD (x);
5053 DECL_INITIAL (x) = 0;
5055 /* Detect flexible array member in an invalid context. */
5056 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5057 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5058 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5059 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5061 if (TREE_CODE (t) == UNION_TYPE)
5062 error ("%Hflexible array member in union",
5063 &DECL_SOURCE_LOCATION (x));
5064 else if (TREE_CHAIN (x) != NULL_TREE)
5065 error ("%Hflexible array member not at end of struct",
5066 &DECL_SOURCE_LOCATION (x));
5067 else if (! saw_named_field)
5068 error ("%Hflexible array member in otherwise empty struct",
5069 &DECL_SOURCE_LOCATION (x));
5072 if (pedantic && TREE_CODE (t) == RECORD_TYPE
5073 && flexible_array_type_p (TREE_TYPE (x)))
5074 pedwarn ("%Hinvalid use of structure with flexible array member",
5075 &DECL_SOURCE_LOCATION (x));
5077 if (DECL_NAME (x))
5078 saw_named_field = 1;
5081 detect_field_duplicates (fieldlist);
5083 /* Now we have the nearly final fieldlist. Record it,
5084 then lay out the structure or union (including the fields). */
5086 TYPE_FIELDS (t) = fieldlist;
5088 layout_type (t);
5090 /* Delete all zero-width bit-fields from the fieldlist */
5092 tree *fieldlistp = &fieldlist;
5093 while (*fieldlistp)
5094 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp))
5095 *fieldlistp = TREE_CHAIN (*fieldlistp);
5096 else
5097 fieldlistp = &TREE_CHAIN (*fieldlistp);
5100 /* Now we have the truly final field list.
5101 Store it in this type and in the variants. */
5103 TYPE_FIELDS (t) = fieldlist;
5105 /* If there are lots of fields, sort so we can look through them fast.
5106 We arbitrarily consider 16 or more elts to be "a lot". */
5109 int len = 0;
5111 for (x = fieldlist; x; x = TREE_CHAIN (x))
5113 if (len > 15 || DECL_NAME (x) == NULL)
5114 break;
5115 len += 1;
5118 if (len > 15)
5120 tree *field_array;
5121 struct lang_type *space;
5122 struct sorted_fields_type *space2;
5124 len += list_length (x);
5126 /* Use the same allocation policy here that make_node uses, to
5127 ensure that this lives as long as the rest of the struct decl.
5128 All decls in an inline function need to be saved. */
5130 space = ggc_alloc (sizeof (struct lang_type));
5131 space2 = ggc_alloc (sizeof (struct sorted_fields_type) + len * sizeof (tree));
5133 len = 0;
5134 space->s = space2;
5135 field_array = &space2->elts[0];
5136 for (x = fieldlist; x; x = TREE_CHAIN (x))
5138 field_array[len++] = x;
5140 /* If there is anonymous struct or union, break out of the loop. */
5141 if (DECL_NAME (x) == NULL)
5142 break;
5144 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5145 if (x == NULL)
5147 TYPE_LANG_SPECIFIC (t) = space;
5148 TYPE_LANG_SPECIFIC (t)->s->len = len;
5149 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5150 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5155 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5157 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5158 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5159 TYPE_ALIGN (x) = TYPE_ALIGN (t);
5160 TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t);
5163 /* If this was supposed to be a transparent union, but we can't
5164 make it one, warn and turn off the flag. */
5165 if (TREE_CODE (t) == UNION_TYPE
5166 && TYPE_TRANSPARENT_UNION (t)
5167 && TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t)))
5169 TYPE_TRANSPARENT_UNION (t) = 0;
5170 warning ("union cannot be made transparent");
5173 /* If this structure or union completes the type of any previous
5174 variable declaration, lay it out and output its rtl. */
5176 if (current_scope->incomplete_list != NULL_TREE)
5178 tree prev = NULL_TREE;
5180 for (x = current_scope->incomplete_list; x; x = TREE_CHAIN (x))
5182 tree decl = TREE_VALUE (x);
5184 if (TYPE_MAIN_VARIANT (TREE_TYPE (decl)) == TYPE_MAIN_VARIANT (t)
5185 && TREE_CODE (decl) != TYPE_DECL)
5187 layout_decl (decl, 0);
5188 /* This is a no-op in c-lang.c or something real in objc-act.c. */
5189 if (c_dialect_objc ())
5190 objc_check_decl (decl);
5191 rest_of_decl_compilation (decl, NULL, toplevel, 0);
5192 if (! toplevel)
5193 expand_decl (decl);
5194 /* Unlink X from the incomplete list. */
5195 if (prev)
5196 TREE_CHAIN (prev) = TREE_CHAIN (x);
5197 else
5198 current_scope->incomplete_list = TREE_CHAIN (x);
5200 else if (!COMPLETE_TYPE_P (TREE_TYPE (decl))
5201 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5203 tree element = TREE_TYPE (decl);
5204 while (TREE_CODE (element) == ARRAY_TYPE)
5205 element = TREE_TYPE (element);
5206 if (element == t)
5208 layout_array_type (TREE_TYPE (decl));
5209 if (TREE_CODE (decl) != TYPE_DECL)
5211 layout_decl (decl, 0);
5212 if (c_dialect_objc ())
5213 objc_check_decl (decl);
5214 rest_of_decl_compilation (decl, NULL, toplevel, 0);
5215 if (! toplevel)
5216 expand_decl (decl);
5218 /* Unlink X from the incomplete list. */
5219 if (prev)
5220 TREE_CHAIN (prev) = TREE_CHAIN (x);
5221 else
5222 current_scope->incomplete_list = TREE_CHAIN (x);
5228 /* Finish debugging output for this type. */
5229 rest_of_type_compilation (t, toplevel);
5231 return t;
5234 /* Lay out the type T, and its element type, and so on. */
5236 static void
5237 layout_array_type (tree t)
5239 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5240 layout_array_type (TREE_TYPE (t));
5241 layout_type (t);
5244 /* Begin compiling the definition of an enumeration type.
5245 NAME is its name (or null if anonymous).
5246 Returns the type object, as yet incomplete.
5247 Also records info about it so that build_enumerator
5248 may be used to declare the individual values as they are read. */
5250 tree
5251 start_enum (tree name)
5253 tree enumtype = 0;
5255 /* If this is the real definition for a previous forward reference,
5256 fill in the contents in the same object that used to be the
5257 forward reference. */
5259 if (name != 0)
5260 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5262 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5264 enumtype = make_node (ENUMERAL_TYPE);
5265 pushtag (name, enumtype);
5268 C_TYPE_BEING_DEFINED (enumtype) = 1;
5270 if (TYPE_VALUES (enumtype) != 0)
5272 /* This enum is a named one that has been declared already. */
5273 error ("redeclaration of `enum %s'", IDENTIFIER_POINTER (name));
5275 /* Completely replace its old definition.
5276 The old enumerators remain defined, however. */
5277 TYPE_VALUES (enumtype) = 0;
5280 enum_next_value = integer_zero_node;
5281 enum_overflow = 0;
5283 if (flag_short_enums)
5284 TYPE_PACKED (enumtype) = 1;
5286 return enumtype;
5289 /* After processing and defining all the values of an enumeration type,
5290 install their decls in the enumeration type and finish it off.
5291 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5292 and ATTRIBUTES are the specified attributes.
5293 Returns ENUMTYPE. */
5295 tree
5296 finish_enum (tree enumtype, tree values, tree attributes)
5298 tree pair, tem;
5299 tree minnode = 0, maxnode = 0, enum_value_type;
5300 int precision, unsign;
5301 int toplevel = (global_scope == current_scope);
5303 if (in_parm_level_p ())
5304 warning ("enum defined inside parms");
5306 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5308 /* Calculate the maximum value of any enumerator in this type. */
5310 if (values == error_mark_node)
5311 minnode = maxnode = integer_zero_node;
5312 else
5314 minnode = maxnode = TREE_VALUE (values);
5315 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5317 tree value = TREE_VALUE (pair);
5318 if (tree_int_cst_lt (maxnode, value))
5319 maxnode = value;
5320 if (tree_int_cst_lt (value, minnode))
5321 minnode = value;
5325 /* Construct the final type of this enumeration. It is the same
5326 as one of the integral types - the narrowest one that fits, except
5327 that normally we only go as narrow as int - and signed iff any of
5328 the values are negative. */
5329 unsign = (tree_int_cst_sgn (minnode) >= 0);
5330 precision = MAX (min_precision (minnode, unsign),
5331 min_precision (maxnode, unsign));
5332 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5334 tree narrowest = c_common_type_for_size (precision, unsign);
5335 if (narrowest == 0)
5337 warning ("enumeration values exceed range of largest integer");
5338 narrowest = long_long_integer_type_node;
5341 precision = TYPE_PRECISION (narrowest);
5343 else
5344 precision = TYPE_PRECISION (integer_type_node);
5346 if (precision == TYPE_PRECISION (integer_type_node))
5347 enum_value_type = c_common_type_for_size (precision, 0);
5348 else
5349 enum_value_type = enumtype;
5351 TYPE_MIN_VALUE (enumtype) = minnode;
5352 TYPE_MAX_VALUE (enumtype) = maxnode;
5353 TYPE_PRECISION (enumtype) = precision;
5354 TREE_UNSIGNED (enumtype) = unsign;
5355 TYPE_SIZE (enumtype) = 0;
5356 layout_type (enumtype);
5358 if (values != error_mark_node)
5360 /* Change the type of the enumerators to be the enum type. We
5361 need to do this irrespective of the size of the enum, for
5362 proper type checking. Replace the DECL_INITIALs of the
5363 enumerators, and the value slots of the list, with copies
5364 that have the enum type; they cannot be modified in place
5365 because they may be shared (e.g. integer_zero_node) Finally,
5366 change the purpose slots to point to the names of the decls. */
5367 for (pair = values; pair; pair = TREE_CHAIN (pair))
5369 tree enu = TREE_PURPOSE (pair);
5371 TREE_TYPE (enu) = enumtype;
5373 /* The ISO C Standard mandates enumerators to have type int,
5374 even though the underlying type of an enum type is
5375 unspecified. Here we convert any enumerators that fit in
5376 an int to type int, to avoid promotions to unsigned types
5377 when comparing integers with enumerators that fit in the
5378 int range. When -pedantic is given, build_enumerator()
5379 would have already taken care of those that don't fit. */
5380 if (int_fits_type_p (DECL_INITIAL (enu), enum_value_type))
5381 DECL_INITIAL (enu) = convert (enum_value_type, DECL_INITIAL (enu));
5382 else
5383 DECL_INITIAL (enu) = convert (enumtype, DECL_INITIAL (enu));
5385 TREE_PURPOSE (pair) = DECL_NAME (enu);
5386 TREE_VALUE (pair) = DECL_INITIAL (enu);
5389 TYPE_VALUES (enumtype) = values;
5392 /* Fix up all variant types of this enum type. */
5393 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5395 if (tem == enumtype)
5396 continue;
5397 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5398 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5399 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5400 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5401 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5402 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5403 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5404 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5405 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5406 TREE_UNSIGNED (tem) = TREE_UNSIGNED (enumtype);
5409 /* Finish debugging output for this type. */
5410 rest_of_type_compilation (enumtype, toplevel);
5412 return enumtype;
5415 /* Build and install a CONST_DECL for one value of the
5416 current enumeration type (one that was begun with start_enum).
5417 Return a tree-list containing the CONST_DECL and its value.
5418 Assignment of sequential values by default is handled here. */
5420 tree
5421 build_enumerator (tree name, tree value)
5423 tree decl, type;
5425 /* Validate and default VALUE. */
5427 /* Remove no-op casts from the value. */
5428 if (value)
5429 STRIP_TYPE_NOPS (value);
5431 if (value != 0)
5433 if (TREE_CODE (value) == INTEGER_CST)
5435 value = default_conversion (value);
5436 constant_expression_warning (value);
5438 else
5440 error ("enumerator value for `%s' not integer constant",
5441 IDENTIFIER_POINTER (name));
5442 value = 0;
5446 /* Default based on previous value. */
5447 /* It should no longer be possible to have NON_LVALUE_EXPR
5448 in the default. */
5449 if (value == 0)
5451 value = enum_next_value;
5452 if (enum_overflow)
5453 error ("overflow in enumeration values");
5456 if (pedantic && ! int_fits_type_p (value, integer_type_node))
5458 pedwarn ("ISO C restricts enumerator values to range of `int'");
5459 value = convert (integer_type_node, value);
5462 /* Set basis for default for next value. */
5463 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
5464 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5466 /* Now create a declaration for the enum value name. */
5468 type = TREE_TYPE (value);
5469 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5470 TYPE_PRECISION (integer_type_node)),
5471 (TYPE_PRECISION (type)
5472 >= TYPE_PRECISION (integer_type_node)
5473 && TREE_UNSIGNED (type)));
5475 decl = build_decl (CONST_DECL, name, type);
5476 DECL_INITIAL (decl) = convert (type, value);
5477 pushdecl (decl);
5479 return tree_cons (decl, value, NULL_TREE);
5483 /* Create the FUNCTION_DECL for a function definition.
5484 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5485 the declaration; they describe the function's name and the type it returns,
5486 but twisted together in a fashion that parallels the syntax of C.
5488 This function creates a binding context for the function body
5489 as well as setting up the FUNCTION_DECL in current_function_decl.
5491 Returns 1 on success. If the DECLARATOR is not suitable for a function
5492 (it defines a datum instead), we return 0, which tells
5493 yyparse to report a parse error. */
5496 start_function (tree declspecs, tree declarator, tree attributes)
5498 tree decl1, old_decl;
5499 tree restype;
5500 int old_immediate_size_expand = immediate_size_expand;
5502 current_function_returns_value = 0; /* Assume, until we see it does. */
5503 current_function_returns_null = 0;
5504 current_function_returns_abnormally = 0;
5505 warn_about_return_type = 0;
5506 current_extern_inline = 0;
5508 /* Don't expand any sizes in the return type of the function. */
5509 immediate_size_expand = 0;
5511 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1);
5513 /* If the declarator is not suitable for a function definition,
5514 cause a syntax error. */
5515 if (decl1 == 0)
5517 immediate_size_expand = old_immediate_size_expand;
5518 return 0;
5521 decl_attributes (&decl1, attributes, 0);
5523 if (DECL_DECLARED_INLINE_P (decl1)
5524 && DECL_UNINLINABLE (decl1)
5525 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
5526 warning ("%Hinline function '%D' given attribute noinline",
5527 &DECL_SOURCE_LOCATION (decl1), decl1);
5529 announce_function (decl1);
5531 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
5533 error ("return type is an incomplete type");
5534 /* Make it return void instead. */
5535 TREE_TYPE (decl1)
5536 = build_function_type (void_type_node,
5537 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
5540 if (warn_about_return_type)
5541 pedwarn_c99 ("return type defaults to `int'");
5543 /* Save the parm names or decls from this function's declarator
5544 where store_parm_decls will find them. */
5545 current_function_parms = last_function_parms;
5546 current_function_parm_tags = last_function_parm_tags;
5548 /* Make the init_value nonzero so pushdecl knows this is not tentative.
5549 error_mark_node is replaced below (in poplevel) with the BLOCK. */
5550 DECL_INITIAL (decl1) = error_mark_node;
5552 /* If this definition isn't a prototype and we had a prototype declaration
5553 before, copy the arg type info from that prototype.
5554 But not if what we had before was a builtin function. */
5555 old_decl = lookup_name_current_level (DECL_NAME (decl1));
5556 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
5557 && !DECL_BUILT_IN (old_decl)
5558 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5559 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (old_decl))))
5560 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
5562 TREE_TYPE (decl1) = TREE_TYPE (old_decl);
5563 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
5566 /* Optionally warn of old-fashioned def with no previous prototype. */
5567 if (warn_strict_prototypes
5568 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
5569 && C_DECL_ISNT_PROTOTYPE (old_decl))
5570 warning ("function declaration isn't a prototype");
5571 /* Optionally warn of any global def with no previous prototype. */
5572 else if (warn_missing_prototypes
5573 && TREE_PUBLIC (decl1)
5574 && ! MAIN_NAME_P (DECL_NAME (decl1))
5575 && C_DECL_ISNT_PROTOTYPE (old_decl))
5576 warning ("%Hno previous prototype for '%D'",
5577 &DECL_SOURCE_LOCATION (decl1), decl1);
5578 /* Optionally warn of any def with no previous prototype
5579 if the function has already been used. */
5580 else if (warn_missing_prototypes
5581 && old_decl != 0 && TREE_USED (old_decl)
5582 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
5583 warning ("%H'%D' was used with no prototype before its definition",
5584 &DECL_SOURCE_LOCATION (decl1), decl1);
5585 /* Optionally warn of any global def with no previous declaration. */
5586 else if (warn_missing_declarations
5587 && TREE_PUBLIC (decl1)
5588 && old_decl == 0
5589 && ! MAIN_NAME_P (DECL_NAME (decl1)))
5590 warning ("%Hno previous declaration for '%D'",
5591 &DECL_SOURCE_LOCATION (decl1), decl1);
5592 /* Optionally warn of any def with no previous declaration
5593 if the function has already been used. */
5594 else if (warn_missing_declarations
5595 && old_decl != 0 && TREE_USED (old_decl)
5596 && C_DECL_IMPLICIT (old_decl))
5597 warning ("%H`%D' was used with no declaration before its definition",
5598 &DECL_SOURCE_LOCATION (decl1), decl1);
5600 /* This is a definition, not a reference.
5601 So normally clear DECL_EXTERNAL.
5602 However, `extern inline' acts like a declaration
5603 except for defining how to inline. So set DECL_EXTERNAL in that case. */
5604 DECL_EXTERNAL (decl1) = current_extern_inline;
5606 /* This function exists in static storage.
5607 (This does not mean `static' in the C sense!) */
5608 TREE_STATIC (decl1) = 1;
5610 /* A nested function is not global. */
5611 if (current_function_decl != 0)
5612 TREE_PUBLIC (decl1) = 0;
5614 #ifdef ENABLE_CHECKING
5615 /* This is the earliest point at which we might know the assembler
5616 name of the function. Thus, if it's set before this, die horribly. */
5617 if (DECL_ASSEMBLER_NAME_SET_P (decl1))
5618 abort ();
5619 #endif
5621 /* If #pragma weak was used, mark the decl weak now. */
5622 if (current_scope == global_scope)
5623 maybe_apply_pragma_weak (decl1);
5625 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
5626 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
5628 tree args;
5629 int argct = 0;
5630 const location_t *locus = &DECL_SOURCE_LOCATION (decl1);
5632 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5633 != integer_type_node)
5634 pedwarn ("%Hreturn type of '%D' is not `int'", locus, decl1);
5636 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
5637 args = TREE_CHAIN (args))
5639 tree type = args ? TREE_VALUE (args) : 0;
5641 if (type == void_type_node)
5642 break;
5644 ++argct;
5645 switch (argct)
5647 case 1:
5648 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
5649 pedwarn ("%Hfirst argument of '%D' should be `int'",
5650 locus, decl1);
5651 break;
5653 case 2:
5654 if (TREE_CODE (type) != POINTER_TYPE
5655 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5656 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5657 != char_type_node))
5658 pedwarn ("%Hsecond argument of '%D' should be 'char **'",
5659 locus, decl1);
5660 break;
5662 case 3:
5663 if (TREE_CODE (type) != POINTER_TYPE
5664 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5665 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5666 != char_type_node))
5667 pedwarn ("%Hthird argument of '%D' should probably be "
5668 "'char **'", locus, decl1);
5669 break;
5673 /* It is intentional that this message does not mention the third
5674 argument because it's only mentioned in an appendix of the
5675 standard. */
5676 if (argct > 0 && (argct < 2 || argct > 3))
5677 pedwarn ("%H'%D' takes only zero or two arguments", locus, decl1);
5679 if (! TREE_PUBLIC (decl1))
5680 pedwarn ("%H'%D' is normally a non-static function", locus, decl1);
5683 /* Record the decl so that the function name is defined.
5684 If we already have a decl for this name, and it is a FUNCTION_DECL,
5685 use the old decl. */
5687 current_function_decl = pushdecl (decl1);
5689 pushlevel (0);
5690 declare_parm_level ();
5692 make_decl_rtl (current_function_decl, NULL);
5694 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
5695 /* Promote the value to int before returning it. */
5696 if (c_promoting_integer_type_p (restype))
5698 /* It retains unsignedness if not really getting wider. */
5699 if (TREE_UNSIGNED (restype)
5700 && (TYPE_PRECISION (restype)
5701 == TYPE_PRECISION (integer_type_node)))
5702 restype = unsigned_type_node;
5703 else
5704 restype = integer_type_node;
5706 DECL_RESULT (current_function_decl)
5707 = build_decl (RESULT_DECL, NULL_TREE, restype);
5709 /* If this fcn was already referenced via a block-scope `extern' decl
5710 (or an implicit decl), propagate certain information about the usage. */
5711 if (TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (current_function_decl)))
5712 TREE_ADDRESSABLE (current_function_decl) = 1;
5714 immediate_size_expand = old_immediate_size_expand;
5716 start_fname_decls ();
5718 return 1;
5721 /* Subroutine of store_parm_decls which handles new-style function
5722 definitions (prototype format). The parms already have decls, so we
5723 need only record them as in effect and complain if any redundant
5724 old-style parm decls were written. */
5725 static void
5726 store_parm_decls_newstyle (void)
5728 tree decl, next;
5729 tree fndecl = current_function_decl;
5730 tree parms = current_function_parms;
5731 tree tags = current_function_parm_tags;
5733 /* This is anything which appeared in current_function_parms that
5734 wasn't a PARM_DECL. */
5735 tree nonparms = 0;
5737 if (current_scope->names || current_scope->tags)
5739 error ("%Hold-style parameter declarations in prototyped "
5740 "function definition", &DECL_SOURCE_LOCATION (fndecl));
5742 /* Get rid of the old-style declarations. */
5743 poplevel (0, 0, 0);
5744 pushlevel (0);
5747 /* Now make all the parameter declarations visible in the function body. */
5748 parms = nreverse (parms);
5749 for (decl = parms; decl; decl = next)
5751 next = TREE_CHAIN (decl);
5752 if (TREE_CODE (decl) != PARM_DECL)
5754 /* If we find an enum constant or a type tag,
5755 put it aside for the moment. */
5756 TREE_CHAIN (decl) = 0;
5757 nonparms = chainon (nonparms, decl);
5758 continue;
5761 if (DECL_NAME (decl) == 0)
5762 error ("%Hparameter name omitted", &DECL_SOURCE_LOCATION (decl));
5763 else
5764 pushdecl (decl);
5767 /* Record the parameter list in the function declaration. */
5768 DECL_ARGUMENTS (fndecl) = getdecls ();
5770 /* Now make all the ancillary declarations visible, likewise. */
5771 for (decl = nonparms; decl; decl = TREE_CHAIN (decl))
5772 if (DECL_NAME (decl) != 0
5773 && TYPE_MAIN_VARIANT (TREE_TYPE (decl)) != void_type_node)
5774 pushdecl (decl);
5776 /* And all the tag declarations. */
5777 storetags (tags);
5780 /* Subroutine of store_parm_decls which handles old-style function
5781 definitions (separate parameter list and declarations). */
5783 static void
5784 store_parm_decls_oldstyle (void)
5786 tree parm, decl, next;
5787 tree fndecl = current_function_decl;
5789 /* This is the identifier list from the function declarator. */
5790 tree parmids = current_function_parms;
5792 /* This is anything which appeared in current_scope->names that
5793 wasn't a PARM_DECL. */
5794 tree nonparms;
5796 /* We use DECL_WEAK as a flag to show which parameters have been
5797 seen already, since it is not used on PARM_DECL or CONST_DECL. */
5798 for (parm = current_scope->names; parm; parm = TREE_CHAIN (parm))
5799 DECL_WEAK (parm) = 0;
5801 /* Match each formal parameter name with its declaration. Save each
5802 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
5803 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
5805 if (TREE_VALUE (parm) == 0)
5807 error ("%Hparameter name missing from parameter list",
5808 &DECL_SOURCE_LOCATION (fndecl));
5809 TREE_PURPOSE (parm) = 0;
5810 continue;
5813 decl = IDENTIFIER_SYMBOL_VALUE (TREE_VALUE (parm));
5814 if (decl && DECL_CONTEXT (decl) == fndecl)
5816 const location_t *locus = &DECL_SOURCE_LOCATION (decl);
5817 /* If we got something other than a PARM_DECL it is an error. */
5818 if (TREE_CODE (decl) != PARM_DECL)
5819 error ("%H\"%D\" declared as a non-parameter", locus, decl);
5820 /* If the declaration is already marked, we have a duplicate
5821 name. Complain and ignore the duplicate. */
5822 else if (DECL_WEAK (decl))
5824 error ("%Hmultiple parameters named \"%D\"", locus, decl);
5825 TREE_PURPOSE (parm) = 0;
5826 continue;
5828 /* If the declaration says "void", complain and turn it into
5829 an int. */
5830 else if (VOID_TYPE_P (TREE_TYPE (decl)))
5832 error ("%Hparameter \"%D\" declared void", locus, decl);
5833 TREE_TYPE (decl) = integer_type_node;
5834 DECL_ARG_TYPE (decl) = integer_type_node;
5835 layout_decl (decl, 0);
5838 /* If no declaration found, default to int. */
5839 else
5841 const location_t *locus = &DECL_SOURCE_LOCATION (fndecl);
5842 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
5843 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
5844 DECL_SOURCE_LOCATION (decl) = *locus;
5845 pushdecl (decl);
5847 if (flag_isoc99)
5848 pedwarn ("%Htype of \"%D\" defaults to \"int\"", locus, decl);
5849 else if (extra_warnings)
5850 warning ("%Htype of \"%D\" defaults to \"int\"", locus, decl);
5853 TREE_PURPOSE (parm) = decl;
5854 DECL_WEAK (decl) = 1;
5857 /* Put anything which is in current_scope->names and which is
5858 not a PARM_DECL onto the list NONPARMS. (The types of
5859 non-parm things which might appear on the list include
5860 enumerators and NULL-named TYPE_DECL nodes.) Complain about
5861 any actual PARM_DECLs not matched with any names. */
5863 nonparms = 0;
5864 for (parm = current_scope->names; parm; parm = next)
5866 const location_t *locus = &DECL_SOURCE_LOCATION (parm);
5867 next = TREE_CHAIN (parm);
5868 TREE_CHAIN (parm) = 0;
5870 if (TREE_CODE (parm) != PARM_DECL)
5872 nonparms = chainon (nonparms, parm);
5873 continue;
5876 if (!COMPLETE_TYPE_P (TREE_TYPE (parm)))
5878 error ("%Hparameter \"%D\" has incomplete type", locus, parm);
5879 TREE_TYPE (parm) = error_mark_node;
5882 if (! DECL_WEAK (parm))
5884 error ("%Hdeclaration for parameter \"%D\" but no such parameter",
5885 locus, parm);
5887 /* Pretend the parameter was not missing.
5888 This gets us to a standard state and minimizes
5889 further error messages. */
5890 parmids = chainon (parmids, tree_cons (parm, 0, 0));
5894 /* Chain the declarations together in the order of the list of
5895 names. Store that chain in the function decl, replacing the
5896 list of names. */
5897 DECL_ARGUMENTS (fndecl) = 0;
5899 tree last;
5900 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
5901 if (TREE_PURPOSE (parm))
5902 break;
5903 if (parm && TREE_PURPOSE (parm))
5905 last = TREE_PURPOSE (parm);
5906 DECL_ARGUMENTS (fndecl) = last;
5907 DECL_WEAK (last) = 0;
5909 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
5910 if (TREE_PURPOSE (parm))
5912 TREE_CHAIN (last) = TREE_PURPOSE (parm);
5913 last = TREE_PURPOSE (parm);
5914 DECL_WEAK (last) = 0;
5916 TREE_CHAIN (last) = 0;
5920 /* If there was a previous prototype,
5921 set the DECL_ARG_TYPE of each argument according to
5922 the type previously specified, and report any mismatches. */
5924 if (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
5926 tree type;
5927 for (parm = DECL_ARGUMENTS (fndecl),
5928 type = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
5929 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
5930 != void_type_node));
5931 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
5933 if (parm == 0 || type == 0
5934 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
5936 error ("number of arguments doesn't match prototype");
5937 error ("%Hprototype declaration",
5938 &current_function_prototype_locus);
5939 break;
5941 /* Type for passing arg must be consistent with that
5942 declared for the arg. ISO C says we take the unqualified
5943 type for parameters declared with qualified type. */
5944 if (! comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
5945 TYPE_MAIN_VARIANT (TREE_VALUE (type)),
5946 COMPARE_STRICT))
5948 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
5949 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
5951 /* Adjust argument to match prototype. E.g. a previous
5952 `int foo(float);' prototype causes
5953 `int foo(x) float x; {...}' to be treated like
5954 `int foo(float x) {...}'. This is particularly
5955 useful for argument types like uid_t. */
5956 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
5958 if (PROMOTE_PROTOTYPES
5959 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
5960 && TYPE_PRECISION (TREE_TYPE (parm))
5961 < TYPE_PRECISION (integer_type_node))
5962 DECL_ARG_TYPE (parm) = integer_type_node;
5964 if (pedantic)
5966 pedwarn ("promoted argument \"%D\" "
5967 "doesn't match prototype", parm);
5968 pedwarn ("%Hprototype declaration",
5969 &current_function_prototype_locus);
5972 else
5974 error ("argument \"%D\" doesn't match prototype", parm);
5975 error ("%Hprototype declaration",
5976 &current_function_prototype_locus);
5980 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
5983 /* Otherwise, create a prototype that would match. */
5985 else
5987 tree actual = 0, last = 0, type;
5989 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
5991 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
5992 if (last)
5993 TREE_CHAIN (last) = type;
5994 else
5995 actual = type;
5996 last = type;
5998 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
5999 if (last)
6000 TREE_CHAIN (last) = type;
6001 else
6002 actual = type;
6004 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6005 of the type of this function, but we need to avoid having this
6006 affect the types of other similarly-typed functions, so we must
6007 first force the generation of an identical (but separate) type
6008 node for the relevant function type. The new node we create
6009 will be a variant of the main variant of the original function
6010 type. */
6012 TREE_TYPE (fndecl) = build_type_copy (TREE_TYPE (fndecl));
6014 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6017 /* Now store the final chain of decls for the arguments
6018 as the decl-chain of the current lexical scope.
6019 Put the enumerators in as well, at the front so that
6020 DECL_ARGUMENTS is not modified. */
6022 storedecls (chainon (nonparms, DECL_ARGUMENTS (fndecl)));
6025 /* Store the parameter declarations into the current function declaration.
6026 This is called after parsing the parameter declarations, before
6027 digesting the body of the function.
6029 For an old-style definition, construct a prototype out of the old-style
6030 parameter declarations and inject it into the function's type. */
6032 void
6033 store_parm_decls (void)
6035 tree fndecl = current_function_decl;
6037 /* The function containing FNDECL, if any. */
6038 tree context = decl_function_context (fndecl);
6040 /* True if this definition is written with a prototype. */
6041 bool prototype = (current_function_parms
6042 && TREE_CODE (current_function_parms) != TREE_LIST);
6044 /* Don't re-emit shadow warnings. */
6045 bool saved_warn_shadow = warn_shadow;
6046 warn_shadow = false;
6048 if (prototype)
6049 store_parm_decls_newstyle ();
6050 else
6051 store_parm_decls_oldstyle ();
6053 /* The next call to pushlevel will be a function body. */
6055 next_is_function_body = true;
6057 /* Write a record describing this function definition to the prototypes
6058 file (if requested). */
6060 gen_aux_info_record (fndecl, 1, 0, prototype);
6062 /* Initialize the RTL code for the function. */
6063 init_function_start (fndecl);
6065 /* Begin the statement tree for this function. */
6066 begin_stmt_tree (&DECL_SAVED_TREE (fndecl));
6068 /* If this is a nested function, save away the sizes of any
6069 variable-size types so that we can expand them when generating
6070 RTL. */
6071 if (context)
6073 tree t;
6075 DECL_LANG_SPECIFIC (fndecl)->pending_sizes
6076 = nreverse (get_pending_sizes ());
6077 for (t = DECL_LANG_SPECIFIC (fndecl)->pending_sizes;
6079 t = TREE_CHAIN (t))
6080 SAVE_EXPR_CONTEXT (TREE_VALUE (t)) = context;
6083 /* This function is being processed in whole-function mode. */
6084 cfun->x_whole_function_mode_p = 1;
6086 /* Even though we're inside a function body, we still don't want to
6087 call expand_expr to calculate the size of a variable-sized array.
6088 We haven't necessarily assigned RTL to all variables yet, so it's
6089 not safe to try to expand expressions involving them. */
6090 immediate_size_expand = 0;
6091 cfun->x_dont_save_pending_sizes_p = 1;
6093 warn_shadow = saved_warn_shadow;
6096 /* Finish up a function declaration and compile that function
6097 all the way to assembler language output. The free the storage
6098 for the function definition.
6100 This is called after parsing the body of the function definition.
6102 NESTED is nonzero if the function being finished is nested in another.
6103 CAN_DEFER_P is nonzero if the function may be deferred. */
6105 void
6106 finish_function (int nested, int can_defer_p)
6108 tree fndecl = current_function_decl;
6110 /* When a function declaration is totally empty, e.g.
6111 void foo(void) { }
6112 (the argument list is irrelevant) the compstmt rule will not
6113 bother calling pushlevel/poplevel, which means we get here with
6114 the scope stack out of sync. Detect this situation by noticing
6115 that current_scope is still as store_parm_decls left it, and do
6116 a dummy push/pop to get back to consistency.
6117 Note that the call to pushlevel does not actually push another
6118 scope - see there for details. */
6120 if (current_scope->parm_flag && next_is_function_body)
6122 pushlevel (0);
6123 poplevel (0, 0, 0);
6126 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6128 /* Must mark the RESULT_DECL as being in this function. */
6130 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6132 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6134 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6135 != integer_type_node)
6137 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6138 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6139 if (!warn_main)
6140 pedwarn ("%Hreturn type of '%D' is not `int'",
6141 &DECL_SOURCE_LOCATION (fndecl), fndecl);
6143 else
6145 #ifdef DEFAULT_MAIN_RETURN
6146 /* Make it so that `main' always returns success by default. */
6147 DEFAULT_MAIN_RETURN;
6148 #else
6149 if (flag_isoc99)
6150 c_expand_return (integer_zero_node);
6151 #endif
6155 finish_fname_decls ();
6157 /* Tie off the statement tree for this function. */
6158 finish_stmt_tree (&DECL_SAVED_TREE (fndecl));
6160 /* Complain if there's just no return statement. */
6161 if (warn_return_type
6162 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6163 && !current_function_returns_value && !current_function_returns_null
6164 /* Don't complain if we abort. */
6165 && !current_function_returns_abnormally
6166 /* Don't warn for main(). */
6167 && !MAIN_NAME_P (DECL_NAME (fndecl))
6168 /* Or if they didn't actually specify a return type. */
6169 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6170 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6171 inline function, as we might never be compiled separately. */
6172 && DECL_INLINE (fndecl))
6173 warning ("no return statement in function returning non-void");
6175 /* Clear out memory we no longer need. */
6176 free_after_parsing (cfun);
6177 /* Since we never call rest_of_compilation, we never clear
6178 CFUN. Do so explicitly. */
6179 free_after_compilation (cfun);
6180 cfun = NULL;
6182 if (flag_unit_at_a_time && can_defer_p)
6184 cgraph_finalize_function (fndecl, DECL_SAVED_TREE (fndecl));
6185 current_function_decl = NULL;
6186 return;
6189 if (! nested)
6191 /* Function is parsed.
6192 Generate RTL for the body of this function or defer
6193 it for later expansion. */
6194 int uninlinable = 1;
6196 /* There's no reason to do any of the work here if we're only doing
6197 semantic analysis; this code just generates RTL. */
6198 if (flag_syntax_only)
6200 current_function_decl = NULL;
6201 DECL_SAVED_TREE (fndecl) = NULL_TREE;
6202 return;
6205 if (flag_inline_trees)
6207 /* First, cache whether the current function is inlinable. Some
6208 predicates depend on cfun and current_function_decl to
6209 function completely. */
6210 timevar_push (TV_INTEGRATION);
6211 uninlinable = ! tree_inlinable_function_p (fndecl, 0);
6213 if (can_defer_p
6214 /* We defer functions marked inline *even if* the function
6215 itself is not inlinable. This is because we don't yet
6216 know if the function will actually be used; we may be
6217 able to avoid emitting it entirely. */
6218 && (! uninlinable || DECL_DECLARED_INLINE_P (fndecl))
6219 /* Save function tree for inlining. Should return 0 if the
6220 language does not support function deferring or the
6221 function could not be deferred. */
6222 && defer_fn (fndecl))
6224 /* Let the back-end know that this function exists. */
6225 (*debug_hooks->deferred_inline_function) (fndecl);
6226 timevar_pop (TV_INTEGRATION);
6227 current_function_decl = NULL;
6228 return;
6231 /* Then, inline any functions called in it. */
6232 optimize_inline_calls (fndecl);
6233 timevar_pop (TV_INTEGRATION);
6236 c_expand_body (fndecl);
6238 /* Keep the function body if it's needed for inlining or dumping. */
6239 if (uninlinable && !dump_enabled_p (TDI_all))
6241 /* Allow the body of the function to be garbage collected. */
6242 DECL_SAVED_TREE (fndecl) = NULL_TREE;
6245 /* Let the error reporting routines know that we're outside a
6246 function. For a nested function, this value is used in
6247 c_pop_function_context and then reset via pop_function_context. */
6248 current_function_decl = NULL;
6252 /* Generate the RTL for a deferred function FNDECL. */
6254 void
6255 c_expand_deferred_function (tree fndecl)
6257 /* DECL_INLINE or DECL_RESULT might got cleared after the inline
6258 function was deferred, e.g. in duplicate_decls. */
6259 if (DECL_INLINE (fndecl) && DECL_RESULT (fndecl))
6261 if (flag_inline_trees)
6263 timevar_push (TV_INTEGRATION);
6264 optimize_inline_calls (fndecl);
6265 timevar_pop (TV_INTEGRATION);
6267 c_expand_body (fndecl);
6268 current_function_decl = NULL;
6272 /* Called to move the SAVE_EXPRs for parameter declarations in a
6273 nested function into the nested function. DATA is really the
6274 nested FUNCTION_DECL. */
6276 static tree
6277 set_save_expr_context (tree *tp,
6278 int *walk_subtrees,
6279 void *data)
6281 if (TREE_CODE (*tp) == SAVE_EXPR && !SAVE_EXPR_CONTEXT (*tp))
6282 SAVE_EXPR_CONTEXT (*tp) = (tree) data;
6283 /* Do not walk back into the SAVE_EXPR_CONTEXT; that will cause
6284 circularity. */
6285 else if (DECL_P (*tp))
6286 *walk_subtrees = 0;
6288 return NULL_TREE;
6291 /* Generate the RTL for the body of FNDECL. If NESTED_P is nonzero,
6292 then we are already in the process of generating RTL for another
6293 function. If can_defer_p is zero, we won't attempt to defer the
6294 generation of RTL. */
6296 static void
6297 c_expand_body_1 (tree fndecl, int nested_p)
6299 timevar_push (TV_EXPAND);
6301 if (nested_p)
6303 /* Make sure that we will evaluate variable-sized types involved
6304 in our function's type. */
6305 expand_pending_sizes (DECL_LANG_SPECIFIC (fndecl)->pending_sizes);
6306 /* Squirrel away our current state. */
6307 push_function_context ();
6310 /* Initialize the RTL code for the function. */
6311 current_function_decl = fndecl;
6312 input_location = DECL_SOURCE_LOCATION (fndecl);
6313 init_function_start (fndecl);
6315 /* This function is being processed in whole-function mode. */
6316 cfun->x_whole_function_mode_p = 1;
6318 /* Even though we're inside a function body, we still don't want to
6319 call expand_expr to calculate the size of a variable-sized array.
6320 We haven't necessarily assigned RTL to all variables yet, so it's
6321 not safe to try to expand expressions involving them. */
6322 immediate_size_expand = 0;
6323 cfun->x_dont_save_pending_sizes_p = 1;
6325 /* Set up parameters and prepare for return, for the function. */
6326 expand_function_start (fndecl, 0);
6328 /* If the function has a variably modified type, there may be
6329 SAVE_EXPRs in the parameter types. Their context must be set to
6330 refer to this function; they cannot be expanded in the containing
6331 function. */
6332 if (decl_function_context (fndecl)
6333 && variably_modified_type_p (TREE_TYPE (fndecl)))
6334 walk_tree (&TREE_TYPE (fndecl), set_save_expr_context, fndecl,
6335 NULL);
6337 /* If this function is `main', emit a call to `__main'
6338 to run global initializers, etc. */
6339 if (DECL_NAME (fndecl)
6340 && MAIN_NAME_P (DECL_NAME (fndecl))
6341 && C_DECL_FILE_SCOPE (fndecl))
6342 expand_main_function ();
6344 /* Generate the RTL for this function. */
6345 expand_stmt (DECL_SAVED_TREE (fndecl));
6347 /* We hard-wired immediate_size_expand to zero above.
6348 expand_function_end will decrement this variable. So, we set the
6349 variable to one here, so that after the decrement it will remain
6350 zero. */
6351 immediate_size_expand = 1;
6353 /* Allow language dialects to perform special processing. */
6354 if (lang_expand_function_end)
6355 (*lang_expand_function_end) ();
6357 /* Generate rtl for function exit. */
6358 expand_function_end ();
6360 /* If this is a nested function, protect the local variables in the stack
6361 above us from being collected while we're compiling this function. */
6362 if (nested_p)
6363 ggc_push_context ();
6365 /* Run the optimizers and output the assembler code for this function. */
6366 rest_of_compilation (fndecl);
6368 /* Undo the GC context switch. */
6369 if (nested_p)
6370 ggc_pop_context ();
6372 /* With just -Wextra, complain only if function returns both with
6373 and without a value. */
6374 if (extra_warnings
6375 && current_function_returns_value
6376 && current_function_returns_null)
6377 warning ("this function may return with or without a value");
6379 /* If requested, warn about function definitions where the function will
6380 return a value (usually of some struct or union type) which itself will
6381 take up a lot of stack space. */
6383 if (warn_larger_than && !DECL_EXTERNAL (fndecl) && TREE_TYPE (fndecl))
6385 tree ret_type = TREE_TYPE (TREE_TYPE (fndecl));
6387 if (ret_type && TYPE_SIZE_UNIT (ret_type)
6388 && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
6389 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type),
6390 larger_than_size))
6392 const location_t *locus = &DECL_SOURCE_LOCATION (fndecl);
6393 unsigned int size_as_int
6394 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
6396 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
6397 warning ("%Hsize of return value of '%D' is %u bytes",
6398 locus, fndecl, size_as_int);
6399 else
6400 warning ("%Hsize of return value of '%D' is larger than %wd bytes",
6401 locus, fndecl, larger_than_size);
6405 if (DECL_SAVED_INSNS (fndecl) == 0 && ! nested_p
6406 && ! flag_inline_trees)
6408 /* Stop pointing to the local nodes about to be freed.
6409 But DECL_INITIAL must remain nonzero so we know this
6410 was an actual function definition.
6411 For a nested function, this is done in c_pop_function_context.
6412 If rest_of_compilation set this to 0, leave it 0. */
6413 if (DECL_INITIAL (fndecl) != 0)
6414 DECL_INITIAL (fndecl) = error_mark_node;
6416 DECL_ARGUMENTS (fndecl) = 0;
6419 if (DECL_STATIC_CONSTRUCTOR (fndecl))
6421 if (targetm.have_ctors_dtors)
6422 (* targetm.asm_out.constructor) (XEXP (DECL_RTL (fndecl), 0),
6423 DEFAULT_INIT_PRIORITY);
6424 else
6425 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6428 if (DECL_STATIC_DESTRUCTOR (fndecl))
6430 if (targetm.have_ctors_dtors)
6431 (* targetm.asm_out.destructor) (XEXP (DECL_RTL (fndecl), 0),
6432 DEFAULT_INIT_PRIORITY);
6433 else
6434 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6437 if (nested_p)
6438 /* Return to the enclosing function. */
6439 pop_function_context ();
6440 timevar_pop (TV_EXPAND);
6443 /* Like c_expand_body_1 but only for unnested functions. */
6445 void
6446 c_expand_body (tree fndecl)
6448 c_expand_body_1 (fndecl, 0);
6451 /* Check the declarations given in a for-loop for satisfying the C99
6452 constraints. */
6453 void
6454 check_for_loop_decls (void)
6456 tree t;
6458 if (!flag_isoc99)
6460 /* If we get here, declarations have been used in a for loop without
6461 the C99 for loop scope. This doesn't make much sense, so don't
6462 allow it. */
6463 error ("`for' loop initial declaration used outside C99 mode");
6464 return;
6466 /* C99 subclause 6.8.5 paragraph 3:
6468 [#3] The declaration part of a for statement shall only
6469 declare identifiers for objects having storage class auto or
6470 register.
6472 It isn't clear whether, in this sentence, "identifiers" binds to
6473 "shall only declare" or to "objects" - that is, whether all identifiers
6474 declared must be identifiers for objects, or whether the restriction
6475 only applies to those that are. (A question on this in comp.std.c
6476 in November 2000 received no answer.) We implement the strictest
6477 interpretation, to avoid creating an extension which later causes
6478 problems. */
6480 for (t = gettags (); t; t = TREE_CHAIN (t))
6482 if (TREE_PURPOSE (t) != 0)
6484 enum tree_code code = TREE_CODE (TREE_VALUE (t));
6486 if (code == RECORD_TYPE)
6487 error ("`struct %s' declared in `for' loop initial declaration",
6488 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6489 else if (code == UNION_TYPE)
6490 error ("`union %s' declared in `for' loop initial declaration",
6491 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6492 else
6493 error ("`enum %s' declared in `for' loop initial declaration",
6494 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6498 for (t = getdecls (); t; t = TREE_CHAIN (t))
6500 const location_t *locus = &DECL_SOURCE_LOCATION (t);
6501 if (TREE_CODE (t) != VAR_DECL && DECL_NAME (t))
6502 error ("%Hdeclaration of non-variable '%D' in 'for' loop "
6503 "initial declaration", locus, t);
6504 else if (TREE_STATIC (t))
6505 error ("%Hdeclaration of static variable '%D' in 'for' loop "
6506 "initial declaration", locus, t);
6507 else if (DECL_EXTERNAL (t))
6508 error ("%Hdeclaration of 'extern' variable '%D' in 'for' loop "
6509 "initial declaration", locus, t);
6513 /* Save and restore the variables in this file and elsewhere
6514 that keep track of the progress of compilation of the current function.
6515 Used for nested functions. */
6517 struct language_function GTY(())
6519 struct c_language_function base;
6520 int returns_value;
6521 int returns_null;
6522 int returns_abnormally;
6523 int warn_about_return_type;
6524 int extern_inline;
6527 /* Save and reinitialize the variables
6528 used during compilation of a C function. */
6530 void
6531 c_push_function_context (struct function *f)
6533 struct language_function *p;
6534 p = ggc_alloc (sizeof (struct language_function));
6535 f->language = p;
6537 p->base.x_stmt_tree = c_stmt_tree;
6538 p->base.x_scope_stmt_stack = c_scope_stmt_stack;
6539 p->returns_value = current_function_returns_value;
6540 p->returns_null = current_function_returns_null;
6541 p->returns_abnormally = current_function_returns_abnormally;
6542 p->warn_about_return_type = warn_about_return_type;
6543 p->extern_inline = current_extern_inline;
6546 /* Restore the variables used during compilation of a C function. */
6548 void
6549 c_pop_function_context (struct function *f)
6551 struct language_function *p = f->language;
6553 if (DECL_SAVED_INSNS (current_function_decl) == 0
6554 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6556 /* Stop pointing to the local nodes about to be freed. */
6557 /* But DECL_INITIAL must remain nonzero so we know this
6558 was an actual function definition. */
6559 DECL_INITIAL (current_function_decl) = error_mark_node;
6560 DECL_ARGUMENTS (current_function_decl) = 0;
6563 c_stmt_tree = p->base.x_stmt_tree;
6564 c_scope_stmt_stack = p->base.x_scope_stmt_stack;
6565 current_function_returns_value = p->returns_value;
6566 current_function_returns_null = p->returns_null;
6567 current_function_returns_abnormally = p->returns_abnormally;
6568 warn_about_return_type = p->warn_about_return_type;
6569 current_extern_inline = p->extern_inline;
6571 f->language = NULL;
6574 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6576 void
6577 c_dup_lang_specific_decl (tree decl)
6579 struct lang_decl *ld;
6581 if (!DECL_LANG_SPECIFIC (decl))
6582 return;
6584 ld = ggc_alloc (sizeof (struct lang_decl));
6585 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6586 DECL_LANG_SPECIFIC (decl) = ld;
6589 /* The functions below are required for functionality of doing
6590 function at once processing in the C front end. Currently these
6591 functions are not called from anywhere in the C front end, but as
6592 these changes continue, that will change. */
6594 /* Returns nonzero if the current statement is a full expression,
6595 i.e. temporaries created during that statement should be destroyed
6596 at the end of the statement. */
6599 stmts_are_full_exprs_p (void)
6601 return 0;
6604 /* Returns the stmt_tree (if any) to which statements are currently
6605 being added. If there is no active statement-tree, NULL is
6606 returned. */
6608 stmt_tree
6609 current_stmt_tree (void)
6611 return &c_stmt_tree;
6614 /* Returns the stack of SCOPE_STMTs for the current function. */
6616 tree *
6617 current_scope_stmt_stack (void)
6619 return &c_scope_stmt_stack;
6622 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6623 C. */
6626 anon_aggr_type_p (tree node ATTRIBUTE_UNUSED)
6628 return 0;
6631 /* Dummy function in place of callback used by C++. */
6633 void
6634 extract_interface_info (void)
6638 /* Return a new COMPOUND_STMT, after adding it to the current
6639 statement tree. */
6641 tree
6642 c_begin_compound_stmt (void)
6644 tree stmt;
6646 /* Create the COMPOUND_STMT. */
6647 stmt = add_stmt (build_stmt (COMPOUND_STMT, NULL_TREE));
6649 return stmt;
6652 /* Expand T (a DECL_STMT) if it declares an entity not handled by the
6653 common code. */
6655 void
6656 c_expand_decl_stmt (tree t)
6658 tree decl = DECL_STMT_DECL (t);
6660 /* Expand nested functions. */
6661 if (TREE_CODE (decl) == FUNCTION_DECL
6662 && DECL_CONTEXT (decl) == current_function_decl
6663 && DECL_SAVED_TREE (decl))
6664 c_expand_body_1 (decl, 1);
6667 /* Return the global value of T as a symbol. */
6669 tree
6670 identifier_global_value (tree t)
6672 tree decl = IDENTIFIER_SYMBOL_VALUE (t);
6673 if (decl == 0 || C_DECL_FILE_SCOPE (decl))
6674 return decl;
6676 /* Shadowed by something else; find the true global value. */
6677 for (decl = global_scope->names; decl; decl = TREE_CHAIN (decl))
6678 if (DECL_NAME (decl) == t)
6679 return decl;
6681 /* Only local values for this decl. */
6682 return 0;
6685 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6686 otherwise the name is found in ridpointers from RID_INDEX. */
6688 void
6689 record_builtin_type (enum rid rid_index, const char *name, tree type)
6691 tree id;
6692 if (name == 0)
6693 id = ridpointers[(int) rid_index];
6694 else
6695 id = get_identifier (name);
6696 pushdecl (build_decl (TYPE_DECL, id, type));
6699 /* Build the void_list_node (void_type_node having been created). */
6700 tree
6701 build_void_list_node (void)
6703 tree t = build_tree_list (NULL_TREE, void_type_node);
6704 return t;
6707 /* Return something to represent absolute declarators containing a *.
6708 TARGET is the absolute declarator that the * contains.
6709 TYPE_QUALS_ATTRS is a list of modifiers such as const or volatile
6710 to apply to the pointer type, represented as identifiers, possible mixed
6711 with attributes.
6713 We return an INDIRECT_REF whose "contents" are TARGET (inside a TREE_LIST,
6714 if attributes are present) and whose type is the modifier list. */
6716 tree
6717 make_pointer_declarator (tree type_quals_attrs, tree target)
6719 tree quals, attrs;
6720 tree itarget = target;
6721 split_specs_attrs (type_quals_attrs, &quals, &attrs);
6722 if (attrs != NULL_TREE)
6723 itarget = tree_cons (attrs, target, NULL_TREE);
6724 return build1 (INDIRECT_REF, quals, itarget);
6727 /* A wrapper around lhd_set_decl_assembler_name that gives static
6728 variables their C names if they are at file scope and only one
6729 translation unit is being compiled, for backwards compatibility
6730 with certain bizzare assembler hacks (like crtstuff.c). */
6732 void
6733 c_static_assembler_name (tree decl)
6735 if (num_in_fnames == 1
6736 && !TREE_PUBLIC (decl) && DECL_CONTEXT (decl)
6737 && TREE_CODE (DECL_CONTEXT (decl)) == TRANSLATION_UNIT_DECL)
6738 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
6739 else
6740 lhd_set_decl_assembler_name (decl);
6743 /* Hash and equality functions for link_hash_table: key off
6744 DECL_ASSEMBLER_NAME. */
6746 static hashval_t
6747 link_hash_hash (const void *x_p)
6749 tree x = (tree)x_p;
6750 return (hashval_t) (long)DECL_ASSEMBLER_NAME (x);
6753 static int
6754 link_hash_eq (const void *x1_p, const void *x2_p)
6756 tree x1 = (tree)x1_p;
6757 tree x2 = (tree)x2_p;
6758 return DECL_ASSEMBLER_NAME (x1) == DECL_ASSEMBLER_NAME (x2);
6761 /* Propagate information between definitions and uses between multiple
6762 translation units in TU_LIST based on linkage rules. */
6764 void
6765 merge_translation_unit_decls (void)
6767 const tree tu_list = current_file_decl;
6768 tree tu;
6769 tree decl;
6770 htab_t link_hash_table;
6771 tree block;
6773 /* Create the BLOCK that poplevel would have created, but don't
6774 actually call poplevel since that's expensive. */
6775 block = make_node (BLOCK);
6776 BLOCK_VARS (block) = current_scope->names;
6777 TREE_USED (block) = 1;
6778 DECL_INITIAL (current_file_decl) = block;
6780 /* If only one translation unit seen, no copying necessary. */
6781 if (TREE_CHAIN (tu_list) == NULL_TREE)
6782 return;
6784 link_hash_table = htab_create (1021, link_hash_hash, link_hash_eq, NULL);
6786 /* Enter any actual definitions into the hash table. */
6787 for (tu = tu_list; tu; tu = TREE_CHAIN (tu))
6788 for (decl = BLOCK_VARS (DECL_INITIAL (tu)); decl; decl = TREE_CHAIN (decl))
6789 if (TREE_PUBLIC (decl) && ! DECL_EXTERNAL (decl))
6791 PTR *slot;
6792 slot = htab_find_slot (link_hash_table, decl, INSERT);
6794 /* If we've already got a definition, work out which one is
6795 the real one, put it into the hash table, and make the
6796 other one DECL_EXTERNAL. This is important to avoid
6797 putting out two definitions of the same symbol in the
6798 assembly output. */
6799 if (*slot != NULL)
6801 tree old_decl = (tree) *slot;
6803 /* If this is weak or common or whatever, suppress it
6804 in favour of the other definition. */
6805 if (DECL_WEAK (decl))
6806 DECL_EXTERNAL (decl) = 1;
6807 else if (DECL_WEAK (old_decl) && ! DECL_WEAK (decl))
6808 DECL_EXTERNAL (old_decl) = 1;
6809 else if (DECL_COMMON (decl) || DECL_ONE_ONLY (decl))
6810 DECL_EXTERNAL (decl) = 1;
6811 else if (DECL_COMMON (old_decl) || DECL_ONE_ONLY (old_decl))
6812 DECL_EXTERNAL (old_decl) = 1;
6814 if (DECL_EXTERNAL (decl))
6816 DECL_INITIAL (decl) = NULL_TREE;
6817 DECL_COMMON (decl) = 0;
6818 DECL_ONE_ONLY (decl) = 0;
6819 DECL_WEAK (decl) = 0;
6821 else if (DECL_EXTERNAL (old_decl))
6823 DECL_INITIAL (old_decl) = NULL_TREE;
6824 DECL_COMMON (old_decl) = 0;
6825 DECL_ONE_ONLY (old_decl) = 0;
6826 DECL_WEAK (old_decl) = 0;
6827 *slot = decl;
6829 else
6831 error ("%Hredefinition of global '%D'",
6832 &DECL_SOURCE_LOCATION (decl), decl);
6833 error ("%H'%D' previously defined here",
6834 &DECL_SOURCE_LOCATION (old_decl), old_decl);
6837 else
6838 *slot = decl;
6841 /* Now insert the desired information from all the definitions
6842 into any plain declarations. */
6843 for (tu = tu_list; tu; tu = TREE_CHAIN (tu))
6844 for (decl = BLOCK_VARS (DECL_INITIAL (tu)); decl; decl = TREE_CHAIN (decl))
6845 if (TREE_PUBLIC (decl) && DECL_EXTERNAL (decl))
6847 tree global_decl;
6848 global_decl = htab_find (link_hash_table, decl);
6850 if (! global_decl)
6851 continue;
6853 /* Print any appropriate error messages, and partially merge
6854 the decls. */
6855 (void) duplicate_decls (decl, global_decl, true, true);
6858 htab_delete (link_hash_table);
6861 /* Perform final processing on file-scope data. */
6863 void
6864 c_write_global_declarations(void)
6866 tree link;
6868 for (link = current_file_decl; link; link = TREE_CHAIN (link))
6870 tree globals = BLOCK_VARS (DECL_INITIAL (link));
6871 int len = list_length (globals);
6872 tree *vec = xmalloc (sizeof (tree) * len);
6873 int i;
6874 tree decl;
6876 /* Process the decls in reverse order--earliest first.
6877 Put them into VEC from back to front, then take out from front. */
6879 for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
6880 vec[len - i - 1] = decl;
6882 wrapup_global_declarations (vec, len);
6884 check_global_declarations (vec, len);
6886 /* Clean up. */
6887 free (vec);
6891 /* Reset the parser's state in preparation for a new file. */
6893 void
6894 c_reset_state (void)
6896 tree link;
6897 tree file_scope_decl;
6899 /* Pop the global scope. */
6900 if (current_scope != global_scope)
6901 current_scope = global_scope;
6902 file_scope_decl = current_file_decl;
6903 DECL_INITIAL (file_scope_decl) = poplevel (1, 0, 0);
6904 truly_local_externals = NULL_TREE;
6906 /* Start a new global binding level. */
6907 pushlevel (0);
6908 global_scope = current_scope;
6909 current_file_decl = build_decl (TRANSLATION_UNIT_DECL, NULL, NULL);
6910 TREE_CHAIN (current_file_decl) = file_scope_decl;
6912 /* Reintroduce the global declarations. */
6913 for (link = builtin_decls; link; link = TREE_CHAIN (link))
6914 pushdecl (copy_node (link));
6917 #include "gt-c-decl.h"