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
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
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
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. */
31 #include "coretypes.h"
35 #include "tree-inline.h"
56 #include "langhooks-def.h"
58 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
60 { NORMAL
, /* Ordinary declaration */
61 FUNCDEF
, /* Function definition */
62 PARM
, /* Declaration of parm before function body */
63 FIELD
, /* Declaration inside struct or union */
64 BITFIELD
, /* Likewise but with specified width */
65 TYPENAME
}; /* Typename (inside cast or sizeof) */
68 /* Nonzero if we have seen an invalid cross reference
69 to a struct, union, or enum, but not yet printed the message. */
71 tree pending_invalid_xref
;
72 /* File and line to appear in the eventual error message. */
73 location_t pending_invalid_xref_location
;
75 /* While defining an enum type, this is 1 plus the last enumerator
76 constant value. Note that will do not have to save this or `enum_overflow'
77 around nested function definition since such a definition could only
78 occur in an enum value expression and we don't use these variables in
81 static tree enum_next_value
;
83 /* Nonzero means that there was overflow computing enum_next_value. */
85 static int enum_overflow
;
87 /* Parsing a function declarator leaves a list of parameter names
88 or a chain of parameter decls here. */
90 static tree last_function_parms
;
92 /* ... and a chain of structure and enum types declared in the
95 static tree last_function_parm_tags
;
97 /* ... and a chain of all non-parameter declarations (such as
98 CONST_DECLs from enumerations) here. */
100 static tree last_function_parm_vars
;
102 /* After parsing the declarator that starts a function definition,
103 `start_function' puts the list of parameter names or chain of decls here
104 for `store_parm_decls' to find. */
106 static tree current_function_parms
;
108 /* Similar, for last_function_parm_tags. */
110 static tree current_function_parm_tags
;
112 /* And for last_function_parm_vars. */
114 static tree current_function_parm_vars
;
116 /* Similar, for the file and line that the prototype came from if this is
117 an old-style definition. */
119 static location_t current_function_prototype_locus
;
121 /* The current statement tree. */
123 static GTY(()) struct stmt_tree_s c_stmt_tree
;
125 /* The current scope statement stack. */
127 static GTY(()) tree c_scope_stmt_stack
;
129 /* A list of external DECLs that appeared at block scope when there was
130 some other global meaning for that identifier. */
131 static GTY(()) tree truly_local_externals
;
133 /* A list of the builtin file-scope DECLs. */
135 static GTY(()) tree builtin_decls
;
137 /* A DECL for the current file-scope context. */
139 static GTY(()) tree current_file_decl
;
141 /* Set to 0 at beginning of a function definition, set to 1 if
142 a return statement that specifies a return value is seen. */
144 int current_function_returns_value
;
146 /* Set to 0 at beginning of a function definition, set to 1 if
147 a return statement with no argument is seen. */
149 int current_function_returns_null
;
151 /* Set to 0 at beginning of a function definition, set to 1 if
152 a call to a noreturn function is seen. */
154 int current_function_returns_abnormally
;
156 /* Set to nonzero by `grokdeclarator' for a function
157 whose return type is defaulted, if warnings for this are desired. */
159 static int warn_about_return_type
;
161 /* Nonzero when starting a function declared `extern inline'. */
163 static int current_extern_inline
;
165 /* For each binding contour we allocate a c_scope structure
166 * which records the names defined in that contour.
169 * 1) one for each function definition,
170 * where internal declarations of the parameters appear.
171 * 2) one for each compound statement,
172 * to record its declarations.
174 * The current meaning of a name can be found by searching the nested
175 * scopes from the current one out to the global one.
178 struct c_scope
GTY(())
180 /* The scope containing this one. */
181 struct c_scope
*outer
;
183 /* The next outermost function scope. */
184 struct c_scope
*outer_function
;
186 /* All variables, constants, functions, labels, and typedef names.
187 They are in the reverse of the order supplied. */
190 /* All parameter declarations. Used only in the outermost scope of
191 a function. Again, in the reverse of the order supplied. */
194 /* All structure, union, and enum type tags. */
197 /* For each scope, a list of shadowed outer-scope definitions
198 to be restored when this scope is popped.
199 Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
200 whose TREE_VALUE is its old definition (a kind of ..._DECL node). */
203 /* For each scope, a list of shadowed outer-scope tag definitions
204 to be restored when this scope is popped.
205 Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
206 whose TREE_VALUE is its old definition (a kind of ..._TYPE node). */
209 /* For each scope (except the global one), a chain of BLOCK nodes
210 for all the scopes that were entered and exited one level down. */
213 /* Variable declarations with incomplete type in this scope. */
214 tree incomplete_list
;
216 /* True if we are currently filling this scope with parameter
220 /* True if we already complained about forward parameter decls
221 in this scope. This prevents double warnings on
222 foo (int a; int b; ...) */
223 bool warned_forward_parm_decls
: 1;
225 /* True if this is the outermost block scope of a function body.
226 This scope contains the parameters, the local variables declared
227 in the outermost block, and all the labels (except those in
228 nested functions, or declared at block scope with __label__). */
229 bool function_body
: 1;
231 /* True means make a BLOCK for this scope no matter what. */
235 /* The scope currently in effect. */
237 static GTY(()) struct c_scope
*current_scope
;
239 /* A chain of c_scope structures awaiting reuse. */
241 static GTY((deletable (""))) struct c_scope
*scope_freelist
;
243 /* The innermost function scope. Ordinary (not explicitly declared)
244 labels, bindings to error_mark_node, and the lazily-created
245 bindings of __func__ and its friends get this scope. */
247 static GTY(()) struct c_scope
*current_function_scope
;
249 /* The outermost scope, corresponding to the C "file scope". This is
250 created when the compiler is started and exists through the entire run. */
252 static GTY(()) struct c_scope
*global_scope
;
254 /* True means unconditionally make a BLOCK for the next scope pushed. */
256 static bool keep_next_level_flag
;
258 /* True means the next call to pushlevel will be the outermost scope
259 of a function body, so do not push a new scope, merely cease
260 expecting parameter decls. */
262 static bool next_is_function_body
;
264 /* Functions called automatically at the beginning and end of execution. */
266 tree static_ctors
, static_dtors
;
268 /* Forward declarations. */
270 static struct c_scope
*make_scope (void);
271 static void pop_scope (void);
272 static tree
match_builtin_function_types (tree
, tree
);
273 static int duplicate_decls (tree
, tree
, int, int);
274 static int redeclaration_error_message (tree
, tree
);
275 static tree
make_label (tree
, location_t
);
276 static void bind_label (tree
, tree
, struct c_scope
*);
277 static void implicit_decl_warning (tree
);
278 static tree
lookup_tag (enum tree_code
, tree
, int);
279 static tree
lookup_name_current_level (tree
);
280 static tree
grokdeclarator (tree
, tree
, enum decl_context
, int);
281 static tree
grokparms (tree
, int);
282 static void layout_array_type (tree
);
283 static void store_parm_decls_newstyle (void);
284 static void store_parm_decls_oldstyle (void);
285 static tree
c_make_fname_decl (tree
, int);
286 static void c_expand_body_1 (tree
, int);
287 static tree
any_external_decl (tree
);
288 static void record_external_decl (tree
);
289 static void warn_if_shadowing (tree
, tree
);
290 static void clone_underlying_type (tree
);
291 static void pushdecl_function_level (tree
, tree
);
292 static bool flexible_array_type_p (tree
);
293 static hashval_t
link_hash_hash (const void *);
294 static int link_hash_eq (const void *, const void *);
296 /* States indicating how grokdeclarator() should handle declspecs marked
297 with __attribute__((deprecated)). An object declared as
298 __attribute__((deprecated)) suppresses warnings of uses of other
301 enum deprecated_states
{
306 static enum deprecated_states deprecated_state
= DEPRECATED_NORMAL
;
309 c_print_identifier (FILE *file
, tree node
, int indent
)
311 print_node (file
, "symbol", IDENTIFIER_SYMBOL_VALUE (node
), indent
+ 4);
312 print_node (file
, "tag", IDENTIFIER_TAG_VALUE (node
), indent
+ 4);
313 print_node (file
, "label", IDENTIFIER_LABEL_VALUE (node
), indent
+ 4);
314 if (C_IS_RESERVED_WORD (node
))
316 tree rid
= ridpointers
[C_RID_CODE (node
)];
317 indent_to (file
, indent
+ 4);
318 fprintf (file
, "rid " HOST_PTR_PRINTF
" \"%s\"",
319 (void *) rid
, IDENTIFIER_POINTER (rid
));
323 /* Hook called at end of compilation to assume 1 elt
324 for a file-scope tentative array defn that wasn't complete before. */
327 c_finish_incomplete_decl (tree decl
)
329 if (TREE_CODE (decl
) == VAR_DECL
)
331 tree type
= TREE_TYPE (decl
);
332 if (type
!= error_mark_node
333 && TREE_CODE (type
) == ARRAY_TYPE
334 && ! DECL_EXTERNAL (decl
)
335 && TYPE_DOMAIN (type
) == 0)
337 warning ("%Harray '%D' assumed to have one element",
338 &DECL_SOURCE_LOCATION (decl
), decl
);
340 complete_array_type (type
, NULL_TREE
, 1);
342 layout_decl (decl
, 0);
347 /* Reuse or create a struct for this scope. */
349 static struct c_scope
*
352 struct c_scope
*result
;
355 result
= scope_freelist
;
356 scope_freelist
= result
->outer
;
359 result
= ggc_alloc_cleared (sizeof (struct c_scope
));
364 /* Remove the topmost scope from the stack and add it to the
365 free list, updating current_function_scope if necessary. */
370 struct c_scope
*scope
= current_scope
;
372 current_scope
= scope
->outer
;
373 if (scope
->function_body
)
374 current_function_scope
= scope
->outer_function
;
376 memset (scope
, 0, sizeof (struct c_scope
));
377 scope
->outer
= scope_freelist
;
378 scope_freelist
= scope
;
381 /* Nonzero if we are currently in the global scope. */
384 global_bindings_p (void)
386 return current_scope
== global_scope
;
390 keep_next_level (void)
392 keep_next_level_flag
= true;
395 /* Identify this scope as currently being filled with parameters. */
398 declare_parm_level (void)
400 current_scope
->parm_flag
= true;
403 /* Nonzero if currently making parm declarations. */
406 in_parm_level_p (void)
408 return current_scope
->parm_flag
;
411 /* Enter a new scope. The dummy parameter is for signature
412 compatibility with lang_hooks.decls.pushlevel. */
415 pushlevel (int dummy ATTRIBUTE_UNUSED
)
417 if (next_is_function_body
)
419 /* This is the transition from the parameters to the top level
420 of the function body. These are the same scope
421 (C99 6.2.1p4,6) so we do not push another scope structure.
422 next_is_function_body is set only by store_parm_decls, which
423 in turn is called when and only when we are about to
424 encounter the opening curly brace for the function body.
426 The outermost block of a function always gets a BLOCK node,
427 because the debugging output routines expect that each
428 function has at least one BLOCK. */
429 current_scope
->parm_flag
= false;
430 current_scope
->function_body
= true;
431 current_scope
->keep
= true;
432 current_scope
->outer_function
= current_function_scope
;
433 current_function_scope
= current_scope
;
435 keep_next_level_flag
= false;
436 next_is_function_body
= false;
440 struct c_scope
*scope
= make_scope ();
442 scope
->keep
= keep_next_level_flag
;
443 scope
->outer
= current_scope
;
444 current_scope
= scope
;
445 keep_next_level_flag
= false;
449 /* Exit a scope. Restore the state of the identifier-decl mappings
450 that were in effect when this scope was entered.
452 If KEEP is KEEP_YES (1), this scope had explicit declarations, so
453 create a BLOCK node to record its declarations and subblocks for
454 debugging output. If KEEP is KEEP_MAYBE, do so only if the names
455 or tags lists are nonempty.
457 If REVERSE is nonzero, reverse the order of decls before putting
460 If FUNCTIONBODY is nonzero, this level is the body of a function,
461 even if current_scope->function_body is not set. This is used
462 by language-independent code that generates synthetic functions,
463 and cannot set current_scope->function_body.
465 FIXME: Eliminate the need for all arguments. */
468 poplevel (int keep
, int reverse
, int functionbody
)
473 tree decls
= current_scope
->names
;
474 tree tags
= current_scope
->tags
;
475 tree subblocks
= current_scope
->blocks
;
477 functionbody
|= current_scope
->function_body
;
479 if (keep
== KEEP_MAYBE
)
480 keep
= (current_scope
->names
|| current_scope
->tags
);
482 keep
|= current_scope
->keep
;
483 keep
|= functionbody
;
485 /* We used to warn about unused variables in expand_end_bindings,
486 i.e. while generating RTL. But in function-at-a-time mode we may
487 choose to never expand a function at all (e.g. auto inlining), so
488 we do this explicitly now.
489 No warnings when the global scope is popped because the global
490 scope isn't popped for the last translation unit, so the warnings
491 are done in c_write_global_declaration. */
492 if (current_scope
!= global_scope
)
493 warn_about_unused_variables (decls
);
495 /* Clear out the name-meanings declared in this scope.
496 Propagate TREE_ADDRESSABLE from nested functions to their
497 containing functions. */
498 for (link
= decls
; link
; link
= TREE_CHAIN (link
))
500 if (TREE_CODE (link
) == LABEL_DECL
)
502 if (TREE_USED (link
) && DECL_INITIAL (link
) == 0)
504 error ("%Hlabel `%D' used but not defined",
505 &DECL_SOURCE_LOCATION (link
), link
);
506 /* Avoid crashing later. */
507 DECL_INITIAL (link
) = error_mark_node
;
509 else if (!TREE_USED (link
) && warn_unused_label
)
511 if (DECL_INITIAL (link
) != 0)
512 warning ("%Hlabel `%D' defined but not used",
513 &DECL_SOURCE_LOCATION (link
), link
);
515 warning ("%Hlabel `%D' declared but not defined",
516 &DECL_SOURCE_LOCATION (link
), link
);
518 IDENTIFIER_LABEL_VALUE (DECL_NAME (link
)) = 0;
520 else if (DECL_NAME (link
) != 0)
522 if (DECL_EXTERNAL (link
)
523 && current_scope
!= global_scope
)
524 /* External decls stay in the symbol-value slot but are
526 C_DECL_INVISIBLE (link
) = 1;
528 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (link
)) = 0;
531 if (TREE_CODE (link
) == FUNCTION_DECL
532 && ! TREE_ASM_WRITTEN (link
)
533 && DECL_INITIAL (link
) != 0
534 && TREE_ADDRESSABLE (link
)
535 && DECL_ABSTRACT_ORIGIN (link
) != 0
536 && DECL_ABSTRACT_ORIGIN (link
) != link
)
537 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (link
)) = 1;
540 /* Clear out the parameter bindings declared in this scope.
541 Unused-parameter warnings are handled by function.c. */
542 for (link
= current_scope
->parms
; link
; link
= TREE_CHAIN (link
))
543 if (DECL_NAME (link
))
544 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (link
)) = 0;
546 /* Clear out the tag-meanings declared in this scope. */
547 for (link
= tags
; link
; link
= TREE_CHAIN (link
))
548 if (TREE_PURPOSE (link
))
549 IDENTIFIER_TAG_VALUE (TREE_PURPOSE (link
)) = 0;
551 /* Restore all name- and label-meanings from outer scopes that were
552 shadowed by this scope. */
554 for (link
= current_scope
->shadowed
; link
; link
= TREE_CHAIN (link
))
555 if (TREE_VALUE (link
) && TREE_CODE (TREE_VALUE (link
)) == LABEL_DECL
)
556 IDENTIFIER_LABEL_VALUE (TREE_PURPOSE (link
)) = TREE_VALUE (link
);
558 IDENTIFIER_SYMBOL_VALUE (TREE_PURPOSE (link
)) = TREE_VALUE (link
);
560 /* Restore all tag-meanings from outer scopes that were shadowed by
563 for (link
= current_scope
->shadowed_tags
; link
;
564 link
= TREE_CHAIN (link
))
565 IDENTIFIER_TAG_VALUE (TREE_PURPOSE (link
)) = TREE_VALUE (link
);
567 /* If this is the outermost block of a function, remove all
568 PARM_DECLs from current_scope->names; they are already
569 stored in DECL_ARGUMENTS of cfun->decl in proper order, should
570 not be put in BLOCK_VARS, and furthermore reversing them will
571 cause trouble later. They are all together at the end of the
573 if (functionbody
&& decls
)
575 if (TREE_CODE (decls
) == PARM_DECL
)
580 while (TREE_CHAIN (link
)
581 && TREE_CODE (TREE_CHAIN (link
)) != PARM_DECL
)
582 link
= TREE_CHAIN (link
);
584 TREE_CHAIN (link
) = 0;
588 /* Get the decls in the order they were written.
589 Usually current_scope->names is in reverse order.
590 But parameter decls were previously put in forward order. */
593 decls
= nreverse (decls
);
595 /* If appropriate, create a BLOCK to record the decls for the life
601 block
= make_node (BLOCK
);
602 BLOCK_VARS (block
) = decls
;
603 BLOCK_SUBBLOCKS (block
) = subblocks
;
604 TREE_USED (block
) = 1;
607 /* In each subblock, record that this is its superior. */
609 for (link
= subblocks
; link
; link
= TREE_CHAIN (link
))
610 BLOCK_SUPERCONTEXT (link
) = block
;
612 /* Set the TYPE_CONTEXTs for all of the tagged types belonging to this
613 binding contour so that they point to the appropriate construct, i.e.
614 either to the current FUNCTION_DECL node, or else to the BLOCK node
617 Note that for tagged types whose scope is just the formal parameter
618 list for some function type specification, we can't properly set
619 their TYPE_CONTEXTs here, because we don't have a pointer to the
620 appropriate FUNCTION_TYPE node readily available to us. For those
621 cases, the TYPE_CONTEXTs of the relevant tagged type nodes get set
622 in `grokdeclarator' as soon as we have created the FUNCTION_TYPE
623 node which will represent the "scope" for these "parameter list local"
626 decl
= functionbody
? current_function_decl
: block
;
628 for (link
= tags
; link
; link
= TREE_CHAIN (link
))
629 TYPE_CONTEXT (TREE_VALUE (link
)) = decl
;
631 /* Pop the current scope, and free the structure for reuse. */
634 /* Dispose of the block that we just made inside some higher level. */
636 DECL_INITIAL (current_function_decl
) = block
;
637 else if (block
&& current_scope
)
638 current_scope
->blocks
639 = chainon (current_scope
->blocks
, block
);
640 /* If we did not make a block for the scope just exited, any blocks
641 made for inner scopes (since they cannot be recorded as subblocks
642 here) must be carried forward so they will later become subblocks
643 of something else. */
644 else if (! block
&& subblocks
)
645 current_scope
->blocks
646 = chainon (current_scope
->blocks
, subblocks
);
651 /* Insert BLOCK at the end of the list of subblocks of the current
652 scope. This is used when a BIND_EXPR is expanded, to handle the
653 BLOCK node inside the BIND_EXPR. */
656 insert_block (tree block
)
658 TREE_USED (block
) = 1;
659 current_scope
->blocks
660 = chainon (current_scope
->blocks
, block
);
663 /* Set the BLOCK node for the innermost scope (the one we are
664 currently in). The RTL expansion machinery requires us to provide
665 this hook, but it is not useful in function-at-a-time mode. */
668 set_block (tree block ATTRIBUTE_UNUSED
)
672 /* Push a definition or a declaration of struct, union or enum tag "name".
673 "type" should be the type node.
674 We assume that the tag "name" is not already defined.
676 Note that the definition may really be just a forward reference.
677 In that case, the TYPE_SIZE will be zero. */
680 pushtag (tree name
, tree type
)
682 struct c_scope
*b
= current_scope
;
684 /* Record the identifier as the type's name if it has none. */
687 if (TYPE_NAME (type
) == 0)
688 TYPE_NAME (type
) = name
;
690 if (IDENTIFIER_TAG_VALUE (name
))
691 b
->shadowed_tags
= tree_cons (name
, IDENTIFIER_TAG_VALUE (name
),
693 IDENTIFIER_TAG_VALUE (name
) = type
;
696 b
->tags
= tree_cons (name
, type
, b
->tags
);
698 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
699 tagged type we just added to the current scope. This fake
700 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
701 to output a representation of a tagged type, and it also gives
702 us a convenient place to record the "scope start" address for the
705 TYPE_STUB_DECL (type
) = pushdecl (build_decl (TYPE_DECL
, NULL_TREE
, type
));
707 /* An approximation for now, so we can tell this is a function-scope tag.
708 This will be updated in poplevel. */
709 TYPE_CONTEXT (type
) = DECL_CONTEXT (TYPE_STUB_DECL (type
));
712 /* Subroutine of duplicate_decls. Allow harmless mismatches in return
713 and argument types provided that the type modes match. This function
714 return a unified type given a suitable match, and 0 otherwise. */
717 match_builtin_function_types (tree oldtype
, tree newtype
)
719 tree newrettype
, oldrettype
;
720 tree newargs
, oldargs
;
721 tree trytype
, tryargs
;
723 /* Accept the return type of the new declaration if same modes. */
724 oldrettype
= TREE_TYPE (oldtype
);
725 newrettype
= TREE_TYPE (newtype
);
727 if (TYPE_MODE (oldrettype
) != TYPE_MODE (newrettype
))
730 oldargs
= TYPE_ARG_TYPES (oldtype
);
731 newargs
= TYPE_ARG_TYPES (newtype
);
734 while (oldargs
|| newargs
)
738 || ! TREE_VALUE (oldargs
)
739 || ! TREE_VALUE (newargs
)
740 || TYPE_MODE (TREE_VALUE (oldargs
))
741 != TYPE_MODE (TREE_VALUE (newargs
)))
744 oldargs
= TREE_CHAIN (oldargs
);
745 newargs
= TREE_CHAIN (newargs
);
748 trytype
= build_function_type (newrettype
, tryargs
);
749 return build_type_attribute_variant (trytype
, TYPE_ATTRIBUTES (oldtype
));
752 /* Handle when a new declaration NEWDECL
753 has the same name as an old one OLDDECL
754 in the same binding contour.
755 Prints an error message if appropriate.
757 If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
760 When DIFFERENT_BINDING_LEVEL is true, NEWDECL is an external declaration,
761 and OLDDECL is in an outer scope and should thus not be changed. */
764 duplicate_decls (tree newdecl
, tree olddecl
, int different_binding_level
,
767 int comptype_flags
= (different_tu
? COMPARE_DIFFERENT_TU
769 int types_match
= comptypes (TREE_TYPE (newdecl
), TREE_TYPE (olddecl
),
771 int new_is_definition
= (TREE_CODE (newdecl
) == FUNCTION_DECL
772 && DECL_INITIAL (newdecl
) != 0);
773 tree oldtype
= TREE_TYPE (olddecl
);
774 tree newtype
= TREE_TYPE (newdecl
);
777 if (DECL_P (olddecl
))
779 if (TREE_CODE (newdecl
) == FUNCTION_DECL
780 && TREE_CODE (olddecl
) == FUNCTION_DECL
781 && (DECL_UNINLINABLE (newdecl
) || DECL_UNINLINABLE (olddecl
)))
783 if (DECL_DECLARED_INLINE_P (newdecl
)
784 && DECL_UNINLINABLE (newdecl
)
785 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl
)))
786 /* Already warned elsewhere. */;
787 else if (DECL_DECLARED_INLINE_P (olddecl
)
788 && DECL_UNINLINABLE (olddecl
)
789 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl
)))
790 /* Already warned. */;
791 else if (DECL_DECLARED_INLINE_P (newdecl
)
792 && ! DECL_DECLARED_INLINE_P (olddecl
)
793 && DECL_UNINLINABLE (olddecl
)
794 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl
)))
796 warning ("%Hfunction '%D' redeclared as inline",
797 &DECL_SOURCE_LOCATION (newdecl
), newdecl
);
798 warning ("%Hprevious declaration of function '%D' "
799 "with attribute noinline",
800 &DECL_SOURCE_LOCATION (olddecl
), olddecl
);
802 else if (DECL_DECLARED_INLINE_P (olddecl
)
803 && DECL_UNINLINABLE (newdecl
)
804 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl
)))
806 warning ("%Hfunction '%D' redeclared with attribute noinline",
807 &DECL_SOURCE_LOCATION (newdecl
), newdecl
);
808 warning ("%Hprevious declaration of function '%D' was inline",
809 &DECL_SOURCE_LOCATION (olddecl
), olddecl
);
813 DECL_ATTRIBUTES (newdecl
)
814 = (*targetm
.merge_decl_attributes
) (olddecl
, newdecl
);
817 if (TREE_CODE (newtype
) == ERROR_MARK
818 || TREE_CODE (oldtype
) == ERROR_MARK
)
821 /* New decl is completely inconsistent with the old one =>
822 tell caller to replace the old one.
823 This is always an error except in the case of shadowing a builtin. */
824 if (TREE_CODE (olddecl
) != TREE_CODE (newdecl
))
826 if (TREE_CODE (olddecl
) == FUNCTION_DECL
827 && DECL_BUILT_IN (olddecl
))
829 /* If you declare a built-in or predefined function name as static,
830 the old definition is overridden,
831 but optionally warn this was a bad choice of name. */
832 if (!TREE_PUBLIC (newdecl
))
835 warning ("%Hshadowing built-in function '%D'",
836 &DECL_SOURCE_LOCATION (newdecl
), newdecl
);
839 warning ("%Hbuilt-in function '%D' declared as non-function",
840 &DECL_SOURCE_LOCATION (newdecl
), newdecl
);
844 error ("%H'%D' redeclared as different kind of symbol",
845 &DECL_SOURCE_LOCATION (newdecl
), newdecl
);
846 error ("%Hprevious declaration of '%D'",
847 &DECL_SOURCE_LOCATION (olddecl
), olddecl
);
853 /* For real parm decl following a forward decl, return 1 so old decl
854 will be reused. Only allow this to happen once. */
855 if (types_match
&& TREE_CODE (newdecl
) == PARM_DECL
856 && TREE_ASM_WRITTEN (olddecl
) && ! TREE_ASM_WRITTEN (newdecl
))
858 TREE_ASM_WRITTEN (olddecl
) = 0;
862 /* The new declaration is the same kind of object as the old one.
863 The declarations may partially match. Print warnings if they don't
864 match enough. Ultimately, copy most of the information from the new
865 decl to the old one, and keep using the old one. */
867 if (TREE_CODE (olddecl
) == FUNCTION_DECL
&& DECL_BUILT_IN (olddecl
))
869 /* A function declaration for a built-in function. */
870 if (!TREE_PUBLIC (newdecl
))
872 /* If you declare a built-in function name as static, the
873 built-in definition is overridden,
874 but optionally warn this was a bad choice of name. */
876 warning ("%Hshadowing built-in function '%D'",
877 &DECL_SOURCE_LOCATION (newdecl
), newdecl
);
878 /* Discard the old built-in function. */
883 /* Accept harmless mismatch in function types.
884 This is for the ffs and fprintf builtins. */
885 tree trytype
= match_builtin_function_types (oldtype
, newtype
);
889 types_match
= comptypes (newtype
, trytype
, comptype_flags
);
892 if (! different_binding_level
)
893 TREE_TYPE (olddecl
) = oldtype
;
898 /* If types don't match for a built-in, throw away the built-in. */
899 warning ("%Hconflicting types for built-in function '%D'",
900 &DECL_SOURCE_LOCATION (newdecl
), newdecl
);
904 else if (TREE_CODE (olddecl
) == FUNCTION_DECL
905 && DECL_SOURCE_LINE (olddecl
) == 0)
907 /* A function declaration for a predeclared function
908 that isn't actually built in. */
909 if (!TREE_PUBLIC (newdecl
))
911 /* If you declare it as static, the
912 default definition is overridden. */
915 else if (!types_match
)
917 /* If the types don't match, preserve volatility indication.
918 Later on, we will discard everything else about the
919 default declaration. */
920 TREE_THIS_VOLATILE (newdecl
) |= TREE_THIS_VOLATILE (olddecl
);
923 /* Permit char *foo () to match void *foo (...) if not pedantic,
924 if one of them came from a system header file. */
925 else if (!types_match
926 && TREE_CODE (olddecl
) == FUNCTION_DECL
927 && TREE_CODE (newdecl
) == FUNCTION_DECL
928 && TREE_CODE (TREE_TYPE (oldtype
)) == POINTER_TYPE
929 && TREE_CODE (TREE_TYPE (newtype
)) == POINTER_TYPE
930 && (DECL_IN_SYSTEM_HEADER (olddecl
)
931 || DECL_IN_SYSTEM_HEADER (newdecl
))
932 && ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (newtype
))) == void_type_node
933 && TYPE_ARG_TYPES (oldtype
) == 0
934 && self_promoting_args_p (TYPE_ARG_TYPES (newtype
))
935 && TREE_TYPE (TREE_TYPE (oldtype
)) == char_type_node
)
937 (TREE_TYPE (TREE_TYPE (newtype
)) == char_type_node
938 && TYPE_ARG_TYPES (newtype
) == 0
939 && self_promoting_args_p (TYPE_ARG_TYPES (oldtype
))
940 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (oldtype
))) == void_type_node
)))
943 pedwarn ("%Hconflicting types for '%D'",
944 &DECL_SOURCE_LOCATION (newdecl
), newdecl
);
945 /* Make sure we keep void * as ret type, not char *. */
946 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (oldtype
))) == void_type_node
)
947 TREE_TYPE (newdecl
) = newtype
= oldtype
;
949 /* Set DECL_IN_SYSTEM_HEADER, so that if we see another declaration
950 we will come back here again. */
951 DECL_IN_SYSTEM_HEADER (newdecl
) = 1;
953 /* Permit void foo (...) to match int foo (...) if the latter is the
954 definition and implicit int was used. See c-torture/compile/920625-2.c. */
955 else if (!types_match
&& new_is_definition
956 && TREE_CODE (olddecl
) == FUNCTION_DECL
957 && TREE_CODE (newdecl
) == FUNCTION_DECL
958 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype
)) == void_type_node
959 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype
)) == integer_type_node
960 && C_FUNCTION_IMPLICIT_INT (newdecl
))
962 pedwarn ("%Hconflicting types for '%D'",
963 &DECL_SOURCE_LOCATION (newdecl
), newdecl
);
964 /* Make sure we keep void as the return type. */
965 TREE_TYPE (newdecl
) = newtype
= oldtype
;
966 C_FUNCTION_IMPLICIT_INT (newdecl
) = 0;
968 else if (!types_match
969 /* Permit char *foo (int, ...); followed by char *foo ();
971 && ! (TREE_CODE (olddecl
) == FUNCTION_DECL
973 /* Return types must still match. */
974 && comptypes (TREE_TYPE (oldtype
),
975 TREE_TYPE (newtype
), comptype_flags
)
976 && TYPE_ARG_TYPES (newtype
) == 0))
978 error ("%Hconflicting types for '%D'",
979 &DECL_SOURCE_LOCATION (newdecl
), newdecl
);
980 /* Check for function type mismatch
981 involving an empty arglist vs a nonempty one. */
982 if (TREE_CODE (olddecl
) == FUNCTION_DECL
983 && comptypes (TREE_TYPE (oldtype
),
984 TREE_TYPE (newtype
), comptype_flags
)
985 && ((TYPE_ARG_TYPES (oldtype
) == 0
986 && DECL_INITIAL (olddecl
) == 0)
988 (TYPE_ARG_TYPES (newtype
) == 0
989 && DECL_INITIAL (newdecl
) == 0)))
991 /* Classify the problem further. */
992 tree t
= TYPE_ARG_TYPES (oldtype
);
994 t
= TYPE_ARG_TYPES (newtype
);
995 for (; t
; t
= TREE_CHAIN (t
))
997 tree type
= TREE_VALUE (t
);
999 if (TREE_CHAIN (t
) == 0
1000 && TYPE_MAIN_VARIANT (type
) != void_type_node
)
1002 error ("a parameter list with an ellipsis can't match an empty parameter name list declaration");
1006 if (c_type_promotes_to (type
) != type
)
1008 error ("an argument type that has a default promotion can't match an empty parameter name list declaration");
1013 if (C_DECL_IMPLICIT (olddecl
))
1014 error ("%Hprevious implicit declaration of '%D'",
1015 &DECL_SOURCE_LOCATION (olddecl
), olddecl
);
1017 error ("%Hprevious declaration of '%D'",
1018 &DECL_SOURCE_LOCATION (olddecl
), olddecl
);
1020 /* This is safer because the initializer might contain references
1021 to variables that were declared between olddecl and newdecl. This
1022 will make the initializer invalid for olddecl in case it gets
1023 assigned to olddecl below. */
1024 if (TREE_CODE (newdecl
) == VAR_DECL
)
1025 DECL_INITIAL (newdecl
) = 0;
1027 /* TLS cannot follow non-TLS declaration. */
1028 else if (TREE_CODE (olddecl
) == VAR_DECL
&& TREE_CODE (newdecl
) == VAR_DECL
1029 && !DECL_THREAD_LOCAL (olddecl
) && DECL_THREAD_LOCAL (newdecl
))
1031 error ("%Hthread-local declaration of '%D' follows non thread-local "
1032 "declaration", &DECL_SOURCE_LOCATION (newdecl
), newdecl
);
1033 error ("%Hprevious declaration of '%D'",
1034 &DECL_SOURCE_LOCATION (olddecl
), olddecl
);
1036 /* non-TLS declaration cannot follow TLS declaration. */
1037 else if (TREE_CODE (olddecl
) == VAR_DECL
&& TREE_CODE (newdecl
) == VAR_DECL
1038 && DECL_THREAD_LOCAL (olddecl
) && !DECL_THREAD_LOCAL (newdecl
))
1040 error ("%Hnon thread-local declaration of '%D' follows "
1041 "thread-local declaration",
1042 &DECL_SOURCE_LOCATION (newdecl
), newdecl
);
1043 error ("%Hprevious declaration of '%D'",
1044 &DECL_SOURCE_LOCATION (olddecl
), olddecl
);
1048 errmsg
= redeclaration_error_message (newdecl
, olddecl
);
1051 const location_t
*locus
= &DECL_SOURCE_LOCATION (newdecl
);
1055 error ("%Hredefinition of '%D'", locus
, newdecl
);
1058 error ("%Hredeclaration of '%D'", locus
, newdecl
);
1061 error ("%Hconflicting declarations of '%D'", locus
, newdecl
);
1067 locus
= &DECL_SOURCE_LOCATION (olddecl
);
1068 if (DECL_INITIAL (olddecl
)
1069 && current_scope
== global_scope
)
1070 error ("%H'%D' previously defined here", locus
, olddecl
);
1072 error ("%H'%D' previously declared here", locus
, olddecl
);
1075 else if (TREE_CODE (newdecl
) == TYPE_DECL
1076 && (DECL_IN_SYSTEM_HEADER (olddecl
)
1077 || DECL_IN_SYSTEM_HEADER (newdecl
)))
1079 const location_t
*locus
= &DECL_SOURCE_LOCATION (newdecl
);
1080 warning ("%Hredefinition of '%D'", locus
, newdecl
);
1081 locus
= &DECL_SOURCE_LOCATION (olddecl
);
1082 if (DECL_INITIAL (olddecl
)
1083 && current_scope
== global_scope
)
1084 warning ("%H'%D' previously defined here", locus
, olddecl
);
1086 warning ("%H'%D' previously declared here", locus
, olddecl
);
1088 else if (TREE_CODE (olddecl
) == FUNCTION_DECL
1089 && DECL_INITIAL (olddecl
) != 0
1090 && TYPE_ARG_TYPES (oldtype
) == 0
1091 && TYPE_ARG_TYPES (newtype
) != 0
1092 && TYPE_ACTUAL_ARG_TYPES (oldtype
) != 0)
1096 /* Prototype decl follows defn w/o prototype. */
1098 for (parm
= TYPE_ACTUAL_ARG_TYPES (oldtype
),
1099 type
= TYPE_ARG_TYPES (newtype
),
1102 parm
= TREE_CHAIN (parm
), type
= TREE_CHAIN (type
), nargs
++)
1104 if (TYPE_MAIN_VARIANT (TREE_VALUE (parm
)) == void_type_node
1105 && TYPE_MAIN_VARIANT (TREE_VALUE (type
)) == void_type_node
)
1107 const location_t
*locus
= &DECL_SOURCE_LOCATION (newdecl
);
1108 warning ("%Hprototype for '%D' follows", locus
, newdecl
);
1109 locus
= &DECL_SOURCE_LOCATION (olddecl
);
1110 warning ("%Hnon-prototype definition here", locus
);
1113 if (TYPE_MAIN_VARIANT (TREE_VALUE (parm
)) == void_type_node
1114 || TYPE_MAIN_VARIANT (TREE_VALUE (type
)) == void_type_node
)
1116 const location_t
*locus
= &DECL_SOURCE_LOCATION (newdecl
);
1117 error ("%Hprototype for '%D' follows and number of "
1118 "arguments doesn't match", locus
, newdecl
);
1119 locus
= &DECL_SOURCE_LOCATION (olddecl
);
1120 error ("%Hnon-prototype definition here", locus
);
1124 /* Type for passing arg must be consistent
1125 with that declared for the arg. */
1126 if (! comptypes (TREE_VALUE (parm
), TREE_VALUE (type
),
1129 const location_t
*locus
= &DECL_SOURCE_LOCATION (newdecl
);
1130 error ("%Hprototype for '%D' follows and argument %d "
1131 "doesn't match", locus
, newdecl
, nargs
);
1132 locus
= &DECL_SOURCE_LOCATION (olddecl
);
1133 error ("%Hnon-prototype definition here", locus
);
1139 /* Warn about mismatches in various flags. */
1142 const location_t
*locus
= &DECL_SOURCE_LOCATION (newdecl
);
1144 /* Warn if function is now inline
1145 but was previously declared not inline and has been called. */
1146 if (TREE_CODE (olddecl
) == FUNCTION_DECL
1147 && ! DECL_DECLARED_INLINE_P (olddecl
)
1148 && DECL_DECLARED_INLINE_P (newdecl
)
1149 && TREE_USED (olddecl
))
1150 warning ("%H'%D' declared inline after being called",
1152 if (TREE_CODE (olddecl
) == FUNCTION_DECL
1153 && ! DECL_DECLARED_INLINE_P (olddecl
)
1154 && DECL_DECLARED_INLINE_P (newdecl
)
1155 && DECL_INITIAL (olddecl
) != 0)
1156 warning ("%H'%D' declared inline after its definition",
1159 /* If pedantic, warn when static declaration follows a non-static
1160 declaration. Otherwise, do so only for functions. */
1161 if ((pedantic
|| TREE_CODE (olddecl
) == FUNCTION_DECL
)
1162 && TREE_PUBLIC (olddecl
)
1163 && !TREE_PUBLIC (newdecl
))
1164 warning ("%Hstatic declaration for '%D' follows non-static",
1167 /* If warn_traditional, warn when a non-static function
1168 declaration follows a static one. */
1169 if (warn_traditional
&& !in_system_header
1170 && TREE_CODE (olddecl
) == FUNCTION_DECL
1171 && !TREE_PUBLIC (olddecl
)
1172 && TREE_PUBLIC (newdecl
))
1173 warning ("%Hnon-static declaration for '%D' follows static",
1176 /* Warn when const declaration follows a non-const
1177 declaration, but not for functions. */
1178 if (TREE_CODE (olddecl
) != FUNCTION_DECL
1179 && !TREE_READONLY (olddecl
)
1180 && TREE_READONLY (newdecl
))
1181 warning ("%Hconst declaration for '%D' follows non-const",
1183 /* These bits are logically part of the type, for variables.
1184 But not for functions
1185 (where qualifiers are not valid ANSI anyway). */
1186 else if (pedantic
&& TREE_CODE (olddecl
) != FUNCTION_DECL
1187 && (TREE_READONLY (newdecl
) != TREE_READONLY (olddecl
)
1188 || TREE_THIS_VOLATILE (newdecl
) != TREE_THIS_VOLATILE (olddecl
)))
1189 pedwarn ("%Htype qualifiers for '%D' conflict with previous "
1190 "declaration", locus
, newdecl
);
1194 /* Optionally warn about more than one declaration for the same name. */
1195 if (errmsg
== 0 && warn_redundant_decls
&& DECL_SOURCE_LINE (olddecl
) != 0
1196 /* Don't warn about a function declaration
1197 followed by a definition. */
1198 && !(TREE_CODE (newdecl
) == FUNCTION_DECL
&& DECL_INITIAL (newdecl
) != 0
1199 && DECL_INITIAL (olddecl
) == 0)
1200 /* Don't warn about extern decl followed by (tentative) definition. */
1201 && !(DECL_EXTERNAL (olddecl
) && ! DECL_EXTERNAL (newdecl
)))
1203 warning ("%Hredundant redeclaration of '%D' in same scope",
1204 &DECL_SOURCE_LOCATION (newdecl
), newdecl
);
1205 warning ("%Hprevious declaration of '%D'",
1206 &DECL_SOURCE_LOCATION (olddecl
), olddecl
);
1209 /* Copy all the DECL_... slots specified in the new decl
1210 except for any that we copy here from the old type.
1212 Past this point, we don't change OLDTYPE and NEWTYPE
1213 even if we change the types of NEWDECL and OLDDECL. */
1217 /* When copying info to olddecl, we store into write_olddecl
1218 instead. This allows us to avoid modifying olddecl when
1219 different_binding_level is true. */
1220 tree write_olddecl
= different_binding_level
? newdecl
: olddecl
;
1222 /* Merge the data types specified in the two decls. */
1223 if (TREE_CODE (newdecl
) != FUNCTION_DECL
|| !DECL_BUILT_IN (olddecl
))
1225 if (different_binding_level
)
1227 if (TYPE_ARG_TYPES (oldtype
) != 0
1228 && TYPE_ARG_TYPES (newtype
) == 0)
1229 TREE_TYPE (newdecl
) = common_type (newtype
, oldtype
);
1232 = build_type_attribute_variant
1234 merge_attributes (TYPE_ATTRIBUTES (newtype
),
1235 TYPE_ATTRIBUTES (oldtype
)));
1239 = TREE_TYPE (olddecl
)
1240 = common_type (newtype
, oldtype
);
1243 /* Lay the type out, unless already done. */
1244 if (oldtype
!= TREE_TYPE (newdecl
))
1246 if (TREE_TYPE (newdecl
) != error_mark_node
)
1247 layout_type (TREE_TYPE (newdecl
));
1248 if (TREE_CODE (newdecl
) != FUNCTION_DECL
1249 && TREE_CODE (newdecl
) != TYPE_DECL
1250 && TREE_CODE (newdecl
) != CONST_DECL
)
1251 layout_decl (newdecl
, 0);
1255 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1256 DECL_SIZE (newdecl
) = DECL_SIZE (olddecl
);
1257 DECL_SIZE_UNIT (newdecl
) = DECL_SIZE_UNIT (olddecl
);
1258 DECL_MODE (newdecl
) = DECL_MODE (olddecl
);
1259 if (TREE_CODE (olddecl
) != FUNCTION_DECL
)
1260 if (DECL_ALIGN (olddecl
) > DECL_ALIGN (newdecl
))
1262 DECL_ALIGN (newdecl
) = DECL_ALIGN (olddecl
);
1263 DECL_USER_ALIGN (newdecl
) |= DECL_ALIGN (olddecl
);
1267 /* Keep the old rtl since we can safely use it. */
1268 COPY_DECL_RTL (olddecl
, newdecl
);
1270 /* Merge the type qualifiers. */
1271 if (TREE_READONLY (newdecl
))
1272 TREE_READONLY (write_olddecl
) = 1;
1274 if (TREE_THIS_VOLATILE (newdecl
))
1276 TREE_THIS_VOLATILE (write_olddecl
) = 1;
1277 if (TREE_CODE (newdecl
) == VAR_DECL
1278 /* If an automatic variable is re-declared in the same
1279 function scope, but the old declaration was not
1280 volatile, make_var_volatile() would crash because the
1281 variable would have been assigned to a pseudo, not a
1282 MEM. Since this duplicate declaration is invalid
1283 anyway, we just skip the call. */
1285 make_var_volatile (newdecl
);
1288 /* Keep source location of definition rather than declaration. */
1289 /* When called with different_binding_level set, keep the old
1290 information so that meaningful diagnostics can be given. */
1291 if (DECL_INITIAL (newdecl
) == 0 && DECL_INITIAL (olddecl
) != 0
1292 && ! different_binding_level
)
1294 DECL_SOURCE_LINE (newdecl
) = DECL_SOURCE_LINE (olddecl
);
1295 DECL_SOURCE_FILE (newdecl
) = DECL_SOURCE_FILE (olddecl
);
1298 /* Merge the unused-warning information. */
1299 if (DECL_IN_SYSTEM_HEADER (olddecl
))
1300 DECL_IN_SYSTEM_HEADER (newdecl
) = 1;
1301 else if (DECL_IN_SYSTEM_HEADER (newdecl
))
1302 DECL_IN_SYSTEM_HEADER (write_olddecl
) = 1;
1304 /* Merge the initialization information. */
1305 /* When called with different_binding_level set, don't copy over
1306 DECL_INITIAL, so that we don't accidentally change function
1307 declarations into function definitions. */
1308 if (DECL_INITIAL (newdecl
) == 0 && ! different_binding_level
)
1309 DECL_INITIAL (newdecl
) = DECL_INITIAL (olddecl
);
1311 /* Merge the section attribute.
1312 We want to issue an error if the sections conflict but that must be
1313 done later in decl_attributes since we are called before attributes
1315 if (DECL_SECTION_NAME (newdecl
) == NULL_TREE
)
1316 DECL_SECTION_NAME (newdecl
) = DECL_SECTION_NAME (olddecl
);
1318 /* Copy the assembler name.
1319 Currently, it can only be defined in the prototype. */
1320 COPY_DECL_ASSEMBLER_NAME (olddecl
, newdecl
);
1322 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
1324 DECL_STATIC_CONSTRUCTOR(newdecl
) |= DECL_STATIC_CONSTRUCTOR(olddecl
);
1325 DECL_STATIC_DESTRUCTOR (newdecl
) |= DECL_STATIC_DESTRUCTOR (olddecl
);
1326 DECL_NO_LIMIT_STACK (newdecl
) |= DECL_NO_LIMIT_STACK (olddecl
);
1327 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl
)
1328 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl
);
1329 TREE_THIS_VOLATILE (newdecl
) |= TREE_THIS_VOLATILE (olddecl
);
1330 TREE_READONLY (newdecl
) |= TREE_READONLY (olddecl
);
1331 DECL_IS_MALLOC (newdecl
) |= DECL_IS_MALLOC (olddecl
);
1332 DECL_IS_PURE (newdecl
) |= DECL_IS_PURE (olddecl
);
1335 /* If cannot merge, then use the new type and qualifiers,
1336 and don't preserve the old rtl. */
1337 else if (! different_binding_level
)
1339 TREE_TYPE (olddecl
) = TREE_TYPE (newdecl
);
1340 TREE_READONLY (olddecl
) = TREE_READONLY (newdecl
);
1341 TREE_THIS_VOLATILE (olddecl
) = TREE_THIS_VOLATILE (newdecl
);
1342 TREE_SIDE_EFFECTS (olddecl
) = TREE_SIDE_EFFECTS (newdecl
);
1345 /* Merge the storage class information. */
1346 merge_weak (newdecl
, olddecl
);
1348 /* For functions, static overrides non-static. */
1349 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
1351 TREE_PUBLIC (newdecl
) &= TREE_PUBLIC (olddecl
);
1352 /* This is since we don't automatically
1353 copy the attributes of NEWDECL into OLDDECL. */
1354 /* No need to worry about different_binding_level here because
1355 then TREE_PUBLIC (newdecl) was true. */
1356 TREE_PUBLIC (olddecl
) = TREE_PUBLIC (newdecl
);
1357 /* If this clears `static', clear it in the identifier too. */
1358 if (! TREE_PUBLIC (olddecl
))
1359 TREE_PUBLIC (DECL_NAME (olddecl
)) = 0;
1361 if (DECL_EXTERNAL (newdecl
))
1363 if (! different_binding_level
|| different_tu
)
1365 /* Don't mess with these flags on local externs; they remain
1366 external even if there's a declaration at file scope which
1368 TREE_STATIC (newdecl
) = TREE_STATIC (olddecl
);
1369 DECL_EXTERNAL (newdecl
) = DECL_EXTERNAL (olddecl
);
1371 /* An extern decl does not override previous storage class. */
1372 TREE_PUBLIC (newdecl
) = TREE_PUBLIC (olddecl
);
1373 if (! DECL_EXTERNAL (newdecl
))
1375 DECL_CONTEXT (newdecl
) = DECL_CONTEXT (olddecl
);
1376 /* If we have two non-EXTERNAL file-scope decls that are
1377 the same, only one of them should be written out. */
1379 TREE_ASM_WRITTEN (newdecl
) = 1;
1384 TREE_STATIC (olddecl
) = TREE_STATIC (newdecl
);
1385 TREE_PUBLIC (olddecl
) = TREE_PUBLIC (newdecl
);
1388 if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
1390 /* If we're redefining a function previously defined as extern
1391 inline, make sure we emit debug info for the inline before we
1392 throw it away, in case it was inlined into a function that hasn't
1393 been written out yet. */
1394 if (new_is_definition
&& DECL_INITIAL (olddecl
))
1396 if (TREE_USED (olddecl
))
1397 (*debug_hooks
->outlining_inline_function
) (olddecl
);
1399 /* The new defn must not be inline. */
1400 DECL_INLINE (newdecl
) = 0;
1401 DECL_UNINLINABLE (newdecl
) = 1;
1405 /* If either decl says `inline', this fn is inline,
1406 unless its definition was passed already. */
1407 if (DECL_DECLARED_INLINE_P (newdecl
)
1408 || DECL_DECLARED_INLINE_P (olddecl
))
1409 DECL_DECLARED_INLINE_P (newdecl
) = 1;
1411 DECL_UNINLINABLE (newdecl
) = DECL_UNINLINABLE (olddecl
)
1412 = (DECL_UNINLINABLE (newdecl
) || DECL_UNINLINABLE (olddecl
));
1415 if (DECL_BUILT_IN (olddecl
))
1417 /* Get rid of any built-in function if new arg types don't match it
1418 or if we have a function definition. */
1419 if (! types_match
|| new_is_definition
)
1421 if (! different_binding_level
)
1423 TREE_TYPE (olddecl
) = TREE_TYPE (newdecl
);
1424 DECL_BUILT_IN_CLASS (olddecl
) = NOT_BUILT_IN
;
1429 /* If redeclaring a builtin function, and not a definition,
1430 it stays built in. */
1431 DECL_BUILT_IN_CLASS (newdecl
) = DECL_BUILT_IN_CLASS (olddecl
);
1432 DECL_FUNCTION_CODE (newdecl
) = DECL_FUNCTION_CODE (olddecl
);
1436 /* Also preserve various other info from the definition. */
1437 if (! new_is_definition
)
1439 DECL_RESULT (newdecl
) = DECL_RESULT (olddecl
);
1440 /* When called with different_binding_level set, don't copy over
1441 DECL_INITIAL, so that we don't accidentally change function
1442 declarations into function definitions. */
1443 if (! different_binding_level
)
1444 DECL_INITIAL (newdecl
) = DECL_INITIAL (olddecl
);
1445 DECL_SAVED_INSNS (newdecl
) = DECL_SAVED_INSNS (olddecl
);
1446 DECL_SAVED_TREE (newdecl
) = DECL_SAVED_TREE (olddecl
);
1447 DECL_ESTIMATED_INSNS (newdecl
) = DECL_ESTIMATED_INSNS (olddecl
);
1448 DECL_ARGUMENTS (newdecl
) = DECL_ARGUMENTS (olddecl
);
1450 /* Set DECL_INLINE on the declaration if we've got a body
1451 from which to instantiate. */
1452 if (DECL_INLINE (olddecl
) && ! DECL_UNINLINABLE (newdecl
))
1454 DECL_INLINE (newdecl
) = 1;
1455 DECL_ABSTRACT_ORIGIN (newdecl
)
1456 = (different_binding_level
1457 ? DECL_ORIGIN (olddecl
)
1458 : DECL_ABSTRACT_ORIGIN (olddecl
));
1463 /* If a previous declaration said inline, mark the
1464 definition as inlinable. */
1465 if (DECL_DECLARED_INLINE_P (newdecl
)
1466 && ! DECL_UNINLINABLE (newdecl
))
1467 DECL_INLINE (newdecl
) = 1;
1470 if (different_binding_level
)
1473 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1474 But preserve OLDDECL's DECL_UID. */
1476 unsigned olddecl_uid
= DECL_UID (olddecl
);
1478 memcpy ((char *) olddecl
+ sizeof (struct tree_common
),
1479 (char *) newdecl
+ sizeof (struct tree_common
),
1480 sizeof (struct tree_decl
) - sizeof (struct tree_common
));
1481 DECL_UID (olddecl
) = olddecl_uid
;
1484 /* NEWDECL contains the merged attribute lists.
1485 Update OLDDECL to be the same. */
1486 DECL_ATTRIBUTES (olddecl
) = DECL_ATTRIBUTES (newdecl
);
1488 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1489 so that encode_section_info has a chance to look at the new decl
1490 flags and attributes. */
1491 if (DECL_RTL_SET_P (olddecl
)
1492 && (TREE_CODE (olddecl
) == FUNCTION_DECL
1493 || (TREE_CODE (olddecl
) == VAR_DECL
1494 && TREE_STATIC (olddecl
))))
1495 make_decl_rtl (olddecl
, NULL
);
1500 /* Return any external DECL associated with ID, whether or not it is
1501 currently in scope. */
1504 any_external_decl (tree id
)
1506 tree decl
= IDENTIFIER_SYMBOL_VALUE (id
);
1509 if (decl
== 0 || TREE_CODE (decl
) == ERROR_MARK
)
1511 else if (TREE_CODE (decl
) != TYPE_DECL
&& DECL_EXTERNAL (decl
))
1514 t
= purpose_member (id
, truly_local_externals
);
1516 return TREE_VALUE (t
);
1521 /* Record an external decl DECL. This only does something if a
1522 shadowing decl already exists. */
1524 record_external_decl (tree decl
)
1526 tree name
= DECL_NAME (decl
);
1527 if (!IDENTIFIER_SYMBOL_VALUE (name
))
1530 truly_local_externals
= tree_cons (name
, decl
, truly_local_externals
);
1533 /* Check whether decl-node X shadows an existing declaration.
1534 OLD is the old IDENTIFIER_SYMBOL_VALUE of the DECL_NAME of X,
1535 which might be a NULL_TREE. */
1537 warn_if_shadowing (tree x
, tree old
)
1541 /* Nothing to shadow? */
1543 /* Shadow warnings not wanted? */
1545 /* No shadow warnings for internally generated vars. */
1546 || DECL_SOURCE_LINE (x
) == 0
1547 /* No shadow warnings for vars made for inlining. */
1548 || DECL_FROM_INLINE (x
)
1549 /* Don't warn about the parm names in function declarator
1550 within a function declarator.
1551 It would be nice to avoid warning in any function
1552 declarator in a declaration, as opposed to a definition,
1553 but there is no way to tell it's not a definition. */
1554 || (TREE_CODE (x
) == PARM_DECL
&& current_scope
->outer
->parm_flag
))
1557 name
= IDENTIFIER_POINTER (DECL_NAME (x
));
1558 if (TREE_CODE (old
) == PARM_DECL
)
1559 shadow_warning (SW_PARAM
, name
, old
);
1560 else if (C_DECL_FILE_SCOPE (old
))
1561 shadow_warning (SW_GLOBAL
, name
, old
);
1563 shadow_warning (SW_LOCAL
, name
, old
);
1567 /* Subroutine of pushdecl.
1569 X is a TYPE_DECL for a typedef statement. Create a brand new
1570 ..._TYPE node (which will be just a variant of the existing
1571 ..._TYPE node with identical properties) and then install X
1572 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1574 The whole point here is to end up with a situation where each
1575 and every ..._TYPE node the compiler creates will be uniquely
1576 associated with AT MOST one node representing a typedef name.
1577 This way, even though the compiler substitutes corresponding
1578 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1579 early on, later parts of the compiler can always do the reverse
1580 translation and get back the corresponding typedef name. For
1583 typedef struct S MY_TYPE;
1586 Later parts of the compiler might only know that `object' was of
1587 type `struct S' if it were not for code just below. With this
1588 code however, later parts of the compiler see something like:
1590 struct S' == struct S
1591 typedef struct S' MY_TYPE;
1594 And they can then deduce (from the node for type struct S') that
1595 the original object declaration was:
1599 Being able to do this is important for proper support of protoize,
1600 and also for generating precise symbolic debugging information
1601 which takes full account of the programmer's (typedef) vocabulary.
1603 Obviously, we don't want to generate a duplicate ..._TYPE node if
1604 the TYPE_DECL node that we are now processing really represents a
1605 standard built-in type.
1607 Since all standard types are effectively declared at line zero
1608 in the source file, we can easily check to see if we are working
1609 on a standard type by checking the current value of lineno. */
1612 clone_underlying_type (tree x
)
1614 if (DECL_SOURCE_LINE (x
) == 0)
1616 if (TYPE_NAME (TREE_TYPE (x
)) == 0)
1617 TYPE_NAME (TREE_TYPE (x
)) = x
;
1619 else if (TREE_TYPE (x
) != error_mark_node
1620 && DECL_ORIGINAL_TYPE (x
) == NULL_TREE
)
1622 tree tt
= TREE_TYPE (x
);
1623 DECL_ORIGINAL_TYPE (x
) = tt
;
1624 tt
= build_type_copy (tt
);
1626 TREE_USED (tt
) = TREE_USED (x
);
1631 /* Record a decl-node X as belonging to the current lexical scope.
1632 Check for errors (such as an incompatible declaration for the same
1633 name already seen in the same scope).
1635 Returns either X or an old decl for the same name.
1636 If an old decl is returned, it may have been smashed
1637 to agree with what X says. */
1642 tree name
= DECL_NAME (x
);
1643 struct c_scope
*scope
= current_scope
;
1645 #ifdef ENABLE_CHECKING
1646 if (error_mark_node
== 0)
1647 /* Called too early. */
1651 /* Functions need the lang_decl data. */
1652 if (TREE_CODE (x
) == FUNCTION_DECL
&& ! DECL_LANG_SPECIFIC (x
))
1653 DECL_LANG_SPECIFIC (x
) = ggc_alloc_cleared (sizeof (struct lang_decl
));
1655 /* A local extern declaration for a function doesn't constitute nesting.
1656 A local auto declaration does, since it's a forward decl
1657 for a nested function coming later. */
1658 if (current_function_decl
== NULL
1659 || ((TREE_CODE (x
) == FUNCTION_DECL
|| TREE_CODE (x
) == VAR_DECL
)
1660 && DECL_INITIAL (x
) == 0 && DECL_EXTERNAL (x
)))
1661 DECL_CONTEXT (x
) = current_file_decl
;
1663 DECL_CONTEXT (x
) = current_function_decl
;
1669 if (warn_nested_externs
1670 && scope
!= global_scope
1671 && DECL_EXTERNAL (x
)
1672 && !DECL_IN_SYSTEM_HEADER (x
))
1673 warning ("nested extern declaration of `%s'",
1674 IDENTIFIER_POINTER (name
));
1676 old
= lookup_name_current_level (name
);
1677 if (old
&& duplicate_decls (x
, old
, 0, false))
1679 /* For PARM_DECLs, old may be a forward declaration.
1680 If so, we want to remove it from its old location
1681 (in the variables chain) and rechain it in the
1682 location given by the new declaration. */
1683 if (TREE_CODE (x
) == PARM_DECL
)
1686 for (p
= &scope
->names
; *p
; p
= &TREE_CHAIN (*p
))
1689 *p
= TREE_CHAIN (old
);
1690 TREE_CHAIN (old
) = scope
->parms
;
1696 if (DECL_EXTERNAL (x
) || scope
== global_scope
)
1698 /* Find and check against a previous, not-in-scope, external
1699 decl for this identifier. (C99 s???: If two declarations
1700 with external linkage, referring to the same object, have
1701 incompatible types, the behavior is undefined). */
1702 tree ext
= any_external_decl (name
);
1705 if (duplicate_decls (x
, ext
, scope
!= global_scope
,
1707 x
= copy_node (ext
);
1710 record_external_decl (x
);
1713 if (TREE_CODE (x
) == TYPE_DECL
)
1714 clone_underlying_type (x
);
1716 /* If storing a local value, there may already be one
1717 (inherited). If so, record it for restoration when this
1718 scope ends. Take care not to do this if we are replacing an
1719 older decl in the same scope (i.e. duplicate_decls returned
1721 if (scope
!= global_scope
1722 && IDENTIFIER_SYMBOL_VALUE (name
)
1723 && IDENTIFIER_SYMBOL_VALUE (name
) != old
)
1725 warn_if_shadowing (x
, IDENTIFIER_SYMBOL_VALUE (name
));
1726 scope
->shadowed
= tree_cons (name
, IDENTIFIER_SYMBOL_VALUE (name
),
1730 /* Install the new declaration in the requested scope. */
1731 IDENTIFIER_SYMBOL_VALUE (name
) = x
;
1732 C_DECL_INVISIBLE (x
) = 0;
1734 /* Keep list of variables in this scope with incomplete type.
1735 If the input is erroneous, we can have error_mark in the type
1736 slot (e.g. "f(void a, ...)") - that doesn't count as an
1739 FIXME: Chain these off the TYPE_DECL for the incomplete type,
1740 then we don't have to do (potentially quite costly) searches
1741 in finish_struct. */
1742 if (TREE_TYPE (x
) != error_mark_node
1743 && !COMPLETE_TYPE_P (TREE_TYPE (x
)))
1745 tree element
= TREE_TYPE (x
);
1747 while (TREE_CODE (element
) == ARRAY_TYPE
)
1748 element
= TREE_TYPE (element
);
1749 if (TREE_CODE (element
) == RECORD_TYPE
1750 || TREE_CODE (element
) == UNION_TYPE
)
1751 scope
->incomplete_list
= tree_cons (NULL_TREE
, x
,
1752 scope
->incomplete_list
);
1756 /* Put decls on list in reverse order.
1757 We will reverse them later if necessary. */
1758 if (TREE_CODE (x
) == PARM_DECL
)
1760 TREE_CHAIN (x
) = scope
->parms
;
1765 TREE_CHAIN (x
) = scope
->names
;
1772 /* Record X as belonging to the global scope (C99 "file scope").
1773 This is used only internally by the Objective-C front end,
1774 and is limited to its needs. It will hork if there is _any_
1775 visible binding for X (not just a global one). */
1777 pushdecl_top_level (tree x
)
1781 if (TREE_CODE (x
) != VAR_DECL
)
1784 name
= DECL_NAME (x
);
1785 old
= IDENTIFIER_SYMBOL_VALUE (name
);
1789 if (DECL_CONTEXT (old
))
1792 if (!duplicate_decls (x
, old
, 0, false))
1798 DECL_CONTEXT (x
) = current_file_decl
;
1799 IDENTIFIER_SYMBOL_VALUE (name
) = x
;
1800 TREE_CHAIN (x
) = global_scope
->names
;
1801 global_scope
->names
= x
;
1805 /* Record X as belonging to the outermost scope of the current
1806 function. This is used only internally, by c_make_fname_decl and
1807 undeclared_variable, and is limited to their needs. The NAME is
1808 provided as a separate argument because undeclared_variable wants to
1809 use error_mark_node for X. For VAR_DECLs, duplicate_decls is not
1810 called; if there is any preexisting decl for this identifier, it is
1813 pushdecl_function_level (tree x
, tree name
)
1815 struct c_scope
*scope
= current_function_scope
;
1817 if (x
== error_mark_node
)
1818 scope
->shadowed
= tree_cons (name
, IDENTIFIER_SYMBOL_VALUE (name
),
1820 else if (TREE_CODE (x
) == VAR_DECL
)
1822 if (name
!= DECL_NAME (x
))
1824 if (IDENTIFIER_SYMBOL_VALUE (name
))
1827 DECL_CONTEXT (x
) = current_function_decl
;
1828 TREE_CHAIN (x
) = scope
->names
;
1832 IDENTIFIER_SYMBOL_VALUE (name
) = x
;
1835 /* Generate an implicit declaration for identifier FUNCTIONID as a
1836 function of type int (). */
1839 implicitly_declare (tree functionid
)
1841 tree decl
= any_external_decl (functionid
);
1843 if (decl
&& decl
!= error_mark_node
)
1845 /* Implicit declaration of a function already declared
1846 (somehow) in a different scope, or as a built-in.
1847 If this is the first time this has happened, warn;
1848 then recycle the old declaration. */
1849 if (!C_DECL_IMPLICIT (decl
))
1851 implicit_decl_warning (DECL_NAME (decl
));
1852 if (! C_DECL_FILE_SCOPE (decl
))
1853 warning ("%Hprevious declaration of '%D'",
1854 &DECL_SOURCE_LOCATION (decl
), decl
);
1855 C_DECL_IMPLICIT (decl
) = 1;
1857 /* If this function is global, then it must already be in the
1858 global scope, so there's no need to push it again. */
1859 if (current_scope
== global_scope
)
1861 /* If this is a local declaration, make a copy; we can't have
1862 the same DECL listed in two different scopes. */
1863 return pushdecl (copy_node (decl
));
1866 /* Not seen before. */
1867 decl
= build_decl (FUNCTION_DECL
, functionid
, default_function_type
);
1868 DECL_EXTERNAL (decl
) = 1;
1869 TREE_PUBLIC (decl
) = 1;
1870 C_DECL_IMPLICIT (decl
) = 1;
1871 implicit_decl_warning (functionid
);
1873 /* C89 says implicit declarations are in the innermost block.
1874 So we record the decl in the standard fashion. */
1875 decl
= pushdecl (decl
);
1877 /* No need to call objc_check_decl here - it's a function type. */
1878 rest_of_decl_compilation (decl
, NULL
, 0, 0);
1880 /* Write a record describing this implicit function declaration
1881 to the prototypes file (if requested). */
1882 gen_aux_info_record (decl
, 0, 1, 0);
1884 /* Possibly apply some default attributes to this implicit declaration. */
1885 decl_attributes (&decl
, NULL_TREE
, 0);
1891 implicit_decl_warning (tree id
)
1893 const char *name
= IDENTIFIER_POINTER (id
);
1894 if (mesg_implicit_function_declaration
== 2)
1895 error ("implicit declaration of function `%s'", name
);
1896 else if (mesg_implicit_function_declaration
== 1)
1897 warning ("implicit declaration of function `%s'", name
);
1900 /* Return zero if the declaration NEWDECL is valid
1901 when the declaration OLDDECL (assumed to be for the same name)
1902 has already been seen.
1903 Otherwise return 1 if NEWDECL is a redefinition, 2 if it is a redeclaration,
1904 and 3 if it is a conflicting declaration. */
1907 redeclaration_error_message (tree newdecl
, tree olddecl
)
1909 if (TREE_CODE (newdecl
) == TYPE_DECL
)
1911 /* Do not complain about type redeclarations where at least one
1912 declaration was in a system header. */
1913 if (DECL_IN_SYSTEM_HEADER (olddecl
) || DECL_IN_SYSTEM_HEADER (newdecl
))
1917 else if (TREE_CODE (newdecl
) == FUNCTION_DECL
)
1919 /* Declarations of functions can insist on internal linkage
1920 but they can't be inconsistent with internal linkage,
1921 so there can be no error on that account.
1922 However defining the same name twice is no good. */
1923 if (DECL_INITIAL (olddecl
) != 0 && DECL_INITIAL (newdecl
) != 0
1924 /* However, defining once as extern inline and a second
1925 time in another way is ok. */
1926 && ! (DECL_DECLARED_INLINE_P (olddecl
) && DECL_EXTERNAL (olddecl
)
1927 && ! (DECL_DECLARED_INLINE_P (newdecl
)
1928 && DECL_EXTERNAL (newdecl
))))
1932 else if (C_DECL_FILE_SCOPE (newdecl
))
1934 /* Objects declared at file scope: */
1935 /* If at least one is a reference, it's ok. */
1936 if (DECL_EXTERNAL (newdecl
) || DECL_EXTERNAL (olddecl
))
1938 /* Reject two definitions. */
1939 if (DECL_INITIAL (olddecl
) != 0 && DECL_INITIAL (newdecl
) != 0)
1941 /* Now we have two tentative defs, or one tentative and one real def. */
1942 /* Insist that the linkage match. */
1943 if (TREE_PUBLIC (olddecl
) != TREE_PUBLIC (newdecl
))
1947 else if (current_scope
->parm_flag
1948 && TREE_ASM_WRITTEN (olddecl
) && !TREE_ASM_WRITTEN (newdecl
))
1952 /* Newdecl has block scope. If olddecl has block scope also, then
1953 reject two definitions, and reject a definition together with an
1954 external reference. Otherwise, it is OK, because newdecl must
1955 be an extern reference to olddecl. */
1956 if (!(DECL_EXTERNAL (newdecl
) && DECL_EXTERNAL (olddecl
))
1957 && DECL_CONTEXT (newdecl
) == DECL_CONTEXT (olddecl
))
1963 /* Issue an error message for a reference to an undeclared variable
1964 ID, including a reference to a builtin outside of function-call
1965 context. Establish a binding of the identifier to error_mark_node
1966 in an appropriate scope, which will suppress further errors for the
1969 undeclared_variable (tree id
)
1971 static bool already
= false;
1973 if (current_function_decl
== 0)
1975 error ("`%s' undeclared here (not in a function)",
1976 IDENTIFIER_POINTER (id
));
1977 IDENTIFIER_SYMBOL_VALUE (id
) = error_mark_node
;
1981 error ("`%s' undeclared (first use in this function)",
1982 IDENTIFIER_POINTER (id
));
1986 error ("(Each undeclared identifier is reported only once");
1987 error ("for each function it appears in.)");
1991 pushdecl_function_level (error_mark_node
, id
);
1995 /* Subroutine of lookup_label, declare_label, define_label: construct a
1996 LABEL_DECL with all the proper frills. */
1999 make_label (tree name
, location_t location
)
2001 tree label
= build_decl (LABEL_DECL
, name
, void_type_node
);
2003 DECL_CONTEXT (label
) = current_function_decl
;
2004 DECL_MODE (label
) = VOIDmode
;
2005 DECL_SOURCE_LOCATION (label
) = location
;
2010 /* Another subroutine of lookup_label, declare_label, define_label:
2011 set up the binding of name to LABEL_DECL in the given SCOPE. */
2014 bind_label (tree name
, tree label
, struct c_scope
*scope
)
2016 if (IDENTIFIER_LABEL_VALUE (name
))
2017 scope
->shadowed
= tree_cons (name
, IDENTIFIER_LABEL_VALUE (name
),
2019 IDENTIFIER_LABEL_VALUE (name
) = label
;
2021 TREE_CHAIN (label
) = scope
->names
;
2022 scope
->names
= label
;
2025 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2026 Create one if none exists so far for the current function.
2027 This is called when a label is used in a goto expression or
2028 has its address taken. */
2031 lookup_label (tree name
)
2035 if (current_function_decl
== 0)
2037 error ("label %s referenced outside of any function",
2038 IDENTIFIER_POINTER (name
));
2042 /* Use a label already defined or ref'd with this name, but not if
2043 it is inherited from a containing function and wasn't declared
2045 label
= IDENTIFIER_LABEL_VALUE (name
);
2046 if (label
&& (DECL_CONTEXT (label
) == current_function_decl
2047 || C_DECLARED_LABEL_FLAG (label
)))
2049 /* If the label has only been declared, update its apparent
2050 location to point here, for better diagnostics if it
2051 turns out not to have been defined. */
2052 if (!TREE_USED (label
))
2053 DECL_SOURCE_LOCATION (label
) = input_location
;
2057 /* No label binding for that identifier; make one. */
2058 label
= make_label (name
, input_location
);
2060 /* Ordinary labels go in the current function scope. */
2061 bind_label (name
, label
, current_function_scope
);
2065 /* Make a label named NAME in the current function, shadowing silently
2066 any that may be inherited from containing functions or containing
2067 scopes. This is called for __label__ declarations. */
2069 /* Note that valid use, if the label being shadowed comes from another
2070 scope in the same function, requires calling declare_nonlocal_label
2071 right away. (Is this still true? -zw 2003-07-17) */
2074 declare_label (tree name
)
2076 tree label
= IDENTIFIER_LABEL_VALUE (name
);
2079 /* Check to make sure that the label hasn't already been declared
2081 for (dup
= current_scope
->names
; dup
; dup
= TREE_CHAIN (dup
))
2084 error ("duplicate label declaration `%s'", IDENTIFIER_POINTER (name
));
2085 error ("%Hthis is a previous declaration",
2086 &DECL_SOURCE_LOCATION (dup
));
2088 /* Just use the previous declaration. */
2092 label
= make_label (name
, input_location
);
2093 C_DECLARED_LABEL_FLAG (label
) = 1;
2095 /* Declared labels go in the current scope. */
2096 bind_label (name
, label
, current_scope
);
2100 /* Define a label, specifying the location in the source file.
2101 Return the LABEL_DECL node for the label, if the definition is valid.
2102 Otherwise return 0. */
2105 define_label (location_t location
, tree name
)
2109 /* Find any preexisting label with this name. It is an error
2110 if that label has already been defined in this function, or
2111 if there is a containing function with a declared label with
2113 label
= IDENTIFIER_LABEL_VALUE (name
);
2116 && ((DECL_CONTEXT (label
) == current_function_decl
2117 && DECL_INITIAL (label
) != 0)
2118 || (DECL_CONTEXT (label
) != current_function_decl
2119 && C_DECLARED_LABEL_FLAG (label
))))
2121 location_t
*prev_loc
= &DECL_SOURCE_LOCATION (label
);
2122 error ("%Hduplicate label `%D'", &location
, label
);
2123 if (DECL_INITIAL (label
))
2124 error ("%H`%D' previously defined here", prev_loc
, label
);
2126 error ("%H`%D' previously declared here", prev_loc
, label
);
2129 else if (label
&& DECL_CONTEXT (label
) == current_function_decl
)
2131 /* The label has been used or declared already in this function,
2132 but not defined. Update its location to point to this
2134 DECL_SOURCE_LOCATION (label
) = location
;
2138 /* No label binding for that identifier; make one. */
2139 label
= make_label (name
, location
);
2141 /* Ordinary labels go in the current function scope. */
2142 bind_label (name
, label
, current_function_scope
);
2145 if (warn_traditional
&& !in_system_header
&& lookup_name (name
))
2146 warning ("%Htraditional C lacks a separate namespace for labels, "
2147 "identifier `%s' conflicts", &location
,
2148 IDENTIFIER_POINTER (name
));
2150 /* Mark label as having been defined. */
2151 DECL_INITIAL (label
) = error_mark_node
;
2155 /* Return the list of declarations of the current scope.
2156 Note that this list is in reverse order. */
2161 return current_scope
->names
;
2164 /* Return the list of type-tags (for structs, etc) of the current scope. */
2169 return current_scope
->tags
;
2172 /* Given NAME, an IDENTIFIER_NODE,
2173 return the structure (or union or enum) definition for that name.
2174 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2175 CODE says which kind of type the caller wants;
2176 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2177 If the wrong kind of type is found, an error is reported. */
2180 lookup_tag (enum tree_code code
, tree name
, int thislevel_only
)
2182 tree tag
= IDENTIFIER_TAG_VALUE (name
);
2188 /* We only care about whether it's in this level if
2189 thislevel_only was set or it might be a type clash. */
2190 if (thislevel_only
|| TREE_CODE (tag
) != code
)
2192 if (current_scope
== global_scope
2193 || purpose_member (name
, current_scope
->tags
))
2197 if (thislevel_only
&& !thislevel
)
2200 if (TREE_CODE (tag
) != code
)
2202 /* Definition isn't the kind we were looking for. */
2203 pending_invalid_xref
= name
;
2204 pending_invalid_xref_location
= input_location
;
2206 /* If in the same binding level as a declaration as a tag
2207 of a different type, this must not be allowed to
2208 shadow that tag, so give the error immediately.
2209 (For example, "struct foo; union foo;" is invalid.) */
2211 pending_xref_error ();
2216 /* Print an error message now
2217 for a recent invalid struct, union or enum cross reference.
2218 We don't print them immediately because they are not invalid
2219 when used in the `struct foo;' construct for shadowing. */
2222 pending_xref_error (void)
2224 if (pending_invalid_xref
!= 0)
2225 error ("%H`%s' defined as wrong kind of tag",
2226 &pending_invalid_xref_location
,
2227 IDENTIFIER_POINTER (pending_invalid_xref
));
2228 pending_invalid_xref
= 0;
2232 /* Look up NAME in the current scope and its superiors
2233 in the namespace of variables, functions and typedefs.
2234 Return a ..._DECL node of some kind representing its definition,
2235 or return 0 if it is undefined. */
2238 lookup_name (tree name
)
2240 tree decl
= IDENTIFIER_SYMBOL_VALUE (name
);
2241 if (decl
== 0 || decl
== error_mark_node
)
2243 if (C_DECL_INVISIBLE (decl
))
2248 /* Similar to `lookup_name' but look only at the current scope. */
2251 lookup_name_current_level (tree name
)
2253 tree decl
= IDENTIFIER_SYMBOL_VALUE (name
);
2255 if (decl
== 0 || decl
== error_mark_node
|| C_DECL_INVISIBLE (decl
))
2258 if (current_scope
== global_scope
)
2261 /* Scan the current scope for a decl with name NAME.
2262 For PARM_DECLs, we have to look at both ->parms and ->names, since
2263 forward parameter declarations wind up on the ->names list. */
2264 if (TREE_CODE (decl
) == PARM_DECL
2265 && chain_member (decl
, current_scope
->parms
))
2267 if (chain_member (decl
, current_scope
->names
))
2273 /* Create the predefined scalar types of C,
2274 and some nodes representing standard constants (0, 1, (void *) 0).
2275 Initialize the global scope.
2276 Make definitions for built-in primitive functions. */
2279 c_init_decl_processing (void)
2282 tree ptr_ftype_void
, ptr_ftype_ptr
;
2283 location_t save_loc
= input_location
;
2285 /* Adds some ggc roots, and reserved words for c-parse.in. */
2288 current_function_decl
= 0;
2290 /* Make the c_scope structure for global names. */
2292 global_scope
= current_scope
;
2294 /* Declarations from c_common_nodes_and_builtins must not be associated
2295 with this input file, lest we get differences between using and not
2296 using preprocessed headers. */
2297 input_location
.file
= "<internal>";
2298 input_location
.line
= 0;
2300 /* Make the DECL for the toplevel file scope. */
2301 current_file_decl
= build_decl (TRANSLATION_UNIT_DECL
, NULL
, NULL
);
2303 build_common_tree_nodes (flag_signed_char
);
2305 c_common_nodes_and_builtins ();
2307 /* In C, comparisons and TRUTH_* expressions have type int. */
2308 truthvalue_type_node
= integer_type_node
;
2309 truthvalue_true_node
= integer_one_node
;
2310 truthvalue_false_node
= integer_zero_node
;
2312 /* Even in C99, which has a real boolean type. */
2313 pushdecl (build_decl (TYPE_DECL
, get_identifier ("_Bool"),
2314 boolean_type_node
));
2316 endlink
= void_list_node
;
2317 ptr_ftype_void
= build_function_type (ptr_type_node
, endlink
);
2319 = build_function_type (ptr_type_node
,
2320 tree_cons (NULL_TREE
, ptr_type_node
, endlink
));
2322 input_location
= save_loc
;
2324 pedantic_lvalues
= pedantic
;
2326 make_fname_decl
= c_make_fname_decl
;
2327 start_fname_decls ();
2329 builtin_decls
= global_scope
->names
;
2332 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2333 decl, NAME is the initialization string and TYPE_DEP indicates whether
2334 NAME depended on the type of the function. As we don't yet implement
2335 delayed emission of static data, we mark the decl as emitted
2336 so it is not placed in the output. Anything using it must therefore pull
2337 out the STRING_CST initializer directly. This does mean that these names
2338 are string merging candidates, which is wrong for C99's __func__. FIXME. */
2341 c_make_fname_decl (tree id
, int type_dep
)
2343 const char *name
= fname_as_string (type_dep
);
2344 tree decl
, type
, init
;
2345 size_t length
= strlen (name
);
2347 type
= build_array_type
2348 (build_qualified_type (char_type_node
, TYPE_QUAL_CONST
),
2349 build_index_type (size_int (length
)));
2351 decl
= build_decl (VAR_DECL
, id
, type
);
2353 TREE_STATIC (decl
) = 1;
2354 TREE_READONLY (decl
) = 1;
2355 DECL_ARTIFICIAL (decl
) = 1;
2357 init
= build_string (length
+ 1, name
);
2358 TREE_TYPE (init
) = type
;
2359 DECL_INITIAL (decl
) = init
;
2361 TREE_USED (decl
) = 1;
2363 if (current_function_decl
)
2364 pushdecl_function_level (decl
, DECL_NAME (decl
));
2366 finish_decl (decl
, init
, NULL_TREE
);
2371 /* Return a definition for a builtin function named NAME and whose data type
2372 is TYPE. TYPE should be a function type with argument types.
2373 FUNCTION_CODE tells later passes how to compile calls to this function.
2374 See tree.h for its possible values.
2376 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2377 the name to be called if we can't opencode the function. If
2378 ATTRS is nonzero, use that for the function's attribute list. */
2381 builtin_function (const char *name
, tree type
, int function_code
,
2382 enum built_in_class
class, const char *library_name
,
2385 tree decl
= build_decl (FUNCTION_DECL
, get_identifier (name
), type
);
2386 DECL_EXTERNAL (decl
) = 1;
2387 TREE_PUBLIC (decl
) = 1;
2389 SET_DECL_ASSEMBLER_NAME (decl
, get_identifier (library_name
));
2390 make_decl_rtl (decl
, NULL
);
2392 DECL_BUILT_IN_CLASS (decl
) = class;
2393 DECL_FUNCTION_CODE (decl
) = function_code
;
2395 /* Warn if a function in the namespace for users
2396 is used without an occasion to consider it declared. */
2397 if (name
[0] != '_' || name
[1] != '_')
2398 C_DECL_INVISIBLE (decl
) = 1;
2400 /* Possibly apply some default attributes to this built-in function. */
2402 decl_attributes (&decl
, attrs
, ATTR_FLAG_BUILT_IN
);
2404 decl_attributes (&decl
, NULL_TREE
, 0);
2409 /* Apply default attributes to a function, if a system function with default
2413 c_insert_default_attributes (tree decl
)
2415 if (!TREE_PUBLIC (decl
))
2417 c_common_insert_default_attributes (decl
);
2420 /* Called when a declaration is seen that contains no names to declare.
2421 If its type is a reference to a structure, union or enum inherited
2422 from a containing scope, shadow that tag name for the current scope
2423 with a forward reference.
2424 If its type defines a new named structure or union
2425 or defines an enum, it is valid but we need not do anything here.
2426 Otherwise, it is an error. */
2429 shadow_tag (tree declspecs
)
2431 shadow_tag_warned (declspecs
, 0);
2435 shadow_tag_warned (tree declspecs
, int warned
)
2438 /* 1 => we have done a pedwarn. 2 => we have done a warning, but
2445 pending_invalid_xref
= 0;
2447 /* Remove the attributes from declspecs, since they will confuse the
2449 split_specs_attrs (declspecs
, &specs
, &attrs
);
2451 for (link
= specs
; link
; link
= TREE_CHAIN (link
))
2453 tree value
= TREE_VALUE (link
);
2454 enum tree_code code
= TREE_CODE (value
);
2456 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
|| code
== ENUMERAL_TYPE
)
2457 /* Used to test also that TYPE_SIZE (value) != 0.
2458 That caused warning for `struct foo;' at top level in the file. */
2460 tree name
= TYPE_NAME (value
);
2467 if (warned
!= 1 && code
!= ENUMERAL_TYPE
)
2468 /* Empty unnamed enum OK */
2470 pedwarn ("unnamed struct/union that defines no instances");
2476 t
= lookup_tag (code
, name
, 1);
2480 t
= make_node (code
);
2487 if (!warned
&& ! in_system_header
)
2489 warning ("useless keyword or type name in empty declaration");
2496 error ("two types specified in one empty declaration");
2501 pedwarn ("empty declaration");
2505 /* Construct an array declarator. EXPR is the expression inside [], or
2506 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
2507 to the pointer to which a parameter array is converted). STATIC_P is
2508 nonzero if "static" is inside the [], zero otherwise. VLA_UNSPEC_P
2509 is nonzero is the array is [*], a VLA of unspecified length which is
2510 nevertheless a complete type (not currently implemented by GCC),
2511 zero otherwise. The declarator is constructed as an ARRAY_REF
2512 (to be decoded by grokdeclarator), whose operand 0 is what's on the
2513 left of the [] (filled by in set_array_declarator_type) and operand 1
2514 is the expression inside; whose TREE_TYPE is the type qualifiers and
2515 which has TREE_STATIC set if "static" is used. */
2518 build_array_declarator (tree expr
, tree quals
, int static_p
, int vla_unspec_p
)
2521 decl
= build_nt (ARRAY_REF
, NULL_TREE
, expr
);
2522 TREE_TYPE (decl
) = quals
;
2523 TREE_STATIC (decl
) = (static_p
? 1 : 0);
2524 if (pedantic
&& !flag_isoc99
)
2526 if (static_p
|| quals
!= NULL_TREE
)
2527 pedwarn ("ISO C90 does not support `static' or type qualifiers in parameter array declarators");
2529 pedwarn ("ISO C90 does not support `[*]' array declarators");
2532 warning ("GCC does not yet properly implement `[*]' array declarators");
2536 /* Set the type of an array declarator. DECL is the declarator, as
2537 constructed by build_array_declarator; TYPE is what appears on the left
2538 of the [] and goes in operand 0. ABSTRACT_P is nonzero if it is an
2539 abstract declarator, zero otherwise; this is used to reject static and
2540 type qualifiers in abstract declarators, where they are not in the
2544 set_array_declarator_type (tree decl
, tree type
, int abstract_p
)
2546 TREE_OPERAND (decl
, 0) = type
;
2547 if (abstract_p
&& (TREE_TYPE (decl
) != NULL_TREE
|| TREE_STATIC (decl
)))
2548 error ("static or type qualifiers in abstract declarator");
2552 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
2555 groktypename (tree typename
)
2559 if (TREE_CODE (typename
) != TREE_LIST
)
2562 split_specs_attrs (TREE_PURPOSE (typename
), &specs
, &attrs
);
2564 typename
= grokdeclarator (TREE_VALUE (typename
), specs
, TYPENAME
, 0);
2566 /* Apply attributes. */
2567 decl_attributes (&typename
, attrs
, 0);
2572 /* Return a PARM_DECL node for a given pair of specs and declarator. */
2575 groktypename_in_parm_context (tree typename
)
2577 if (TREE_CODE (typename
) != TREE_LIST
)
2579 return grokdeclarator (TREE_VALUE (typename
),
2580 TREE_PURPOSE (typename
),
2584 /* Decode a declarator in an ordinary declaration or data definition.
2585 This is called as soon as the type information and variable name
2586 have been parsed, before parsing the initializer if any.
2587 Here we create the ..._DECL node, fill in its type,
2588 and put it on the list of decls for the current context.
2589 The ..._DECL node is returned as the value.
2591 Exception: for arrays where the length is not specified,
2592 the type is left null, to be filled in by `finish_decl'.
2594 Function definitions do not come here; they go to start_function
2595 instead. However, external and forward declarations of functions
2596 do go through here. Structure field declarations are done by
2597 grokfield and not through here. */
2600 start_decl (tree declarator
, tree declspecs
, int initialized
, tree attributes
)
2605 /* An object declared as __attribute__((deprecated)) suppresses
2606 warnings of uses of other deprecated items. */
2607 if (lookup_attribute ("deprecated", attributes
))
2608 deprecated_state
= DEPRECATED_SUPPRESS
;
2610 decl
= grokdeclarator (declarator
, declspecs
,
2611 NORMAL
, initialized
);
2613 deprecated_state
= DEPRECATED_NORMAL
;
2615 if (warn_main
> 0 && TREE_CODE (decl
) != FUNCTION_DECL
2616 && MAIN_NAME_P (DECL_NAME (decl
)))
2617 warning ("%H'%D' is usually a function",
2618 &DECL_SOURCE_LOCATION (decl
), decl
);
2621 /* Is it valid for this decl to have an initializer at all?
2622 If not, set INITIALIZED to zero, which will indirectly
2623 tell `finish_decl' to ignore the initializer once it is parsed. */
2624 switch (TREE_CODE (decl
))
2627 error ("typedef `%s' is initialized (use __typeof__ instead)",
2628 IDENTIFIER_POINTER (DECL_NAME (decl
)));
2633 error ("function `%s' is initialized like a variable",
2634 IDENTIFIER_POINTER (DECL_NAME (decl
)));
2639 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
2640 error ("parameter `%s' is initialized",
2641 IDENTIFIER_POINTER (DECL_NAME (decl
)));
2646 /* Don't allow initializations for incomplete types
2647 except for arrays which might be completed by the initialization. */
2649 /* This can happen if the array size is an undefined macro. We already
2650 gave a warning, so we don't need another one. */
2651 if (TREE_TYPE (decl
) == error_mark_node
)
2653 else if (COMPLETE_TYPE_P (TREE_TYPE (decl
)))
2655 /* A complete type is ok if size is fixed. */
2657 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl
))) != INTEGER_CST
2658 || C_DECL_VARIABLE_SIZE (decl
))
2660 error ("variable-sized object may not be initialized");
2664 else if (TREE_CODE (TREE_TYPE (decl
)) != ARRAY_TYPE
)
2666 error ("variable `%s' has initializer but incomplete type",
2667 IDENTIFIER_POINTER (DECL_NAME (decl
)));
2670 else if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (decl
))))
2672 error ("elements of array `%s' have incomplete type",
2673 IDENTIFIER_POINTER (DECL_NAME (decl
)));
2680 DECL_EXTERNAL (decl
) = 0;
2681 if (current_scope
== global_scope
)
2682 TREE_STATIC (decl
) = 1;
2684 /* Tell `pushdecl' this is an initialized decl
2685 even though we don't yet have the initializer expression.
2686 Also tell `finish_decl' it may store the real initializer. */
2687 DECL_INITIAL (decl
) = error_mark_node
;
2690 /* If this is a function declaration, write a record describing it to the
2691 prototypes file (if requested). */
2693 if (TREE_CODE (decl
) == FUNCTION_DECL
)
2694 gen_aux_info_record (decl
, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl
)) != 0);
2696 /* ANSI specifies that a tentative definition which is not merged with
2697 a non-tentative definition behaves exactly like a definition with an
2698 initializer equal to zero. (Section 3.7.2)
2700 -fno-common gives strict ANSI behavior, though this tends to break
2701 a large body of code that grew up without this rule.
2703 Thread-local variables are never common, since there's no entrenched
2704 body of code to break, and it allows more efficient variable references
2705 in the presence of dynamic linking. */
2707 if (TREE_CODE (decl
) == VAR_DECL
2709 && TREE_PUBLIC (decl
)
2710 && !DECL_THREAD_LOCAL (decl
)
2712 DECL_COMMON (decl
) = 1;
2714 /* Set attributes here so if duplicate decl, will have proper attributes. */
2715 decl_attributes (&decl
, attributes
, 0);
2717 if (TREE_CODE (decl
) == FUNCTION_DECL
2718 && DECL_DECLARED_INLINE_P (decl
)
2719 && DECL_UNINLINABLE (decl
)
2720 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl
)))
2721 warning ("%Hinline function '%D' given attribute noinline",
2722 &DECL_SOURCE_LOCATION (decl
), decl
);
2724 /* Add this decl to the current scope.
2725 TEM may equal DECL or it may be a previous decl of the same name. */
2726 tem
= pushdecl (decl
);
2728 /* For a local variable, define the RTL now. */
2729 if (current_scope
!= global_scope
2730 /* But not if this is a duplicate decl
2731 and we preserved the rtl from the previous one
2732 (which may or may not happen). */
2733 && !DECL_RTL_SET_P (tem
)
2734 && C_DECL_FILE_SCOPE (tem
))
2736 if (TREE_TYPE (tem
) != error_mark_node
2737 && (COMPLETE_TYPE_P (TREE_TYPE (tem
))
2738 || (TREE_CODE (TREE_TYPE (tem
)) == ARRAY_TYPE
2739 && DECL_INITIAL (tem
) != 0)))
2746 /* Finish processing of a declaration;
2747 install its initial value.
2748 If the length of an array type is not known before,
2749 it must be determined now, from the initial value, or it is an error. */
2752 finish_decl (tree decl
, tree init
, tree asmspec_tree
)
2754 tree type
= TREE_TYPE (decl
);
2755 int was_incomplete
= (DECL_SIZE (decl
) == 0);
2756 const char *asmspec
= 0;
2758 /* If a name was specified, get the string. */
2759 if (current_scope
== global_scope
)
2760 asmspec_tree
= maybe_apply_renaming_pragma (decl
, asmspec_tree
);
2762 asmspec
= TREE_STRING_POINTER (asmspec_tree
);
2764 /* If `start_decl' didn't like having an initialization, ignore it now. */
2765 if (init
!= 0 && DECL_INITIAL (decl
) == 0)
2768 /* Don't crash if parm is initialized. */
2769 if (TREE_CODE (decl
) == PARM_DECL
)
2773 store_init_value (decl
, init
);
2775 /* Deduce size of array from initialization, if not already known */
2776 if (TREE_CODE (type
) == ARRAY_TYPE
2777 && TYPE_DOMAIN (type
) == 0
2778 && TREE_CODE (decl
) != TYPE_DECL
)
2781 = (TREE_STATIC (decl
)
2782 /* Even if pedantic, an external linkage array
2783 may have incomplete type at first. */
2784 ? pedantic
&& !TREE_PUBLIC (decl
)
2785 : !DECL_EXTERNAL (decl
));
2787 = complete_array_type (type
, DECL_INITIAL (decl
), do_default
);
2789 /* Get the completed type made by complete_array_type. */
2790 type
= TREE_TYPE (decl
);
2793 error ("%Hinitializer fails to determine size of '%D'",
2794 &DECL_SOURCE_LOCATION (decl
), decl
);
2796 else if (failure
== 2)
2799 error ("%Harray size missing in '%D'",
2800 &DECL_SOURCE_LOCATION (decl
), decl
);
2801 /* If a `static' var's size isn't known,
2802 make it extern as well as static, so it does not get
2804 If it is not `static', then do not mark extern;
2805 finish_incomplete_decl will give it a default size
2806 and it will get allocated. */
2807 else if (!pedantic
&& TREE_STATIC (decl
) && ! TREE_PUBLIC (decl
))
2808 DECL_EXTERNAL (decl
) = 1;
2811 /* TYPE_MAX_VALUE is always one less than the number of elements
2812 in the array, because we start counting at zero. Therefore,
2813 warn only if the value is less than zero. */
2814 else if (pedantic
&& TYPE_DOMAIN (type
) != 0
2815 && tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (type
))) < 0)
2816 error ("%Hzero or negative size array '%D'",
2817 &DECL_SOURCE_LOCATION (decl
), decl
);
2819 layout_decl (decl
, 0);
2822 if (TREE_CODE (decl
) == VAR_DECL
)
2824 if (DECL_SIZE (decl
) == 0 && TREE_TYPE (decl
) != error_mark_node
2825 && COMPLETE_TYPE_P (TREE_TYPE (decl
)))
2826 layout_decl (decl
, 0);
2828 if (DECL_SIZE (decl
) == 0
2829 /* Don't give an error if we already gave one earlier. */
2830 && TREE_TYPE (decl
) != error_mark_node
2831 && (TREE_STATIC (decl
)
2833 /* A static variable with an incomplete type
2834 is an error if it is initialized.
2835 Also if it is not file scope.
2836 Otherwise, let it through, but if it is not `extern'
2837 then it may cause an error message later. */
2838 (DECL_INITIAL (decl
) != 0
2839 || !C_DECL_FILE_SCOPE (decl
))
2841 /* An automatic variable with an incomplete type
2843 !DECL_EXTERNAL (decl
)))
2845 error ("%Hstorage size of '%D' isn't known",
2846 &DECL_SOURCE_LOCATION (decl
), decl
);
2847 TREE_TYPE (decl
) = error_mark_node
;
2850 if ((DECL_EXTERNAL (decl
) || TREE_STATIC (decl
))
2851 && DECL_SIZE (decl
) != 0)
2853 if (TREE_CODE (DECL_SIZE (decl
)) == INTEGER_CST
)
2854 constant_expression_warning (DECL_SIZE (decl
));
2856 error ("%Hstorage size of '%D' isn't constant",
2857 &DECL_SOURCE_LOCATION (decl
), decl
);
2860 if (TREE_USED (type
))
2861 TREE_USED (decl
) = 1;
2864 /* If this is a function and an assembler name is specified, reset DECL_RTL
2865 so we can give it its new name. Also, update built_in_decls if it
2866 was a normal built-in. */
2867 if (TREE_CODE (decl
) == FUNCTION_DECL
&& asmspec
)
2869 /* ASMSPEC is given, and not the name of a register. Mark the
2870 name with a star so assemble_name won't munge it. */
2871 char *starred
= alloca (strlen (asmspec
) + 2);
2873 strcpy (starred
+ 1, asmspec
);
2875 if (DECL_BUILT_IN_CLASS (decl
) == BUILT_IN_NORMAL
)
2877 tree builtin
= built_in_decls
[DECL_FUNCTION_CODE (decl
)];
2878 SET_DECL_RTL (builtin
, NULL_RTX
);
2879 SET_DECL_ASSEMBLER_NAME (builtin
, get_identifier (starred
));
2880 #ifdef TARGET_MEM_FUNCTIONS
2881 if (DECL_FUNCTION_CODE (decl
) == BUILT_IN_MEMCPY
)
2882 init_block_move_fn (starred
);
2883 else if (DECL_FUNCTION_CODE (decl
) == BUILT_IN_MEMSET
)
2884 init_block_clear_fn (starred
);
2886 if (DECL_FUNCTION_CODE (decl
) == BUILT_IN_BCOPY
)
2887 init_block_move_fn (starred
);
2888 else if (DECL_FUNCTION_CODE (decl
) == BUILT_IN_BZERO
)
2889 init_block_clear_fn (starred
);
2892 SET_DECL_RTL (decl
, NULL_RTX
);
2893 SET_DECL_ASSEMBLER_NAME (decl
, get_identifier (starred
));
2896 /* If #pragma weak was used, mark the decl weak now. */
2897 if (current_scope
== global_scope
)
2898 maybe_apply_pragma_weak (decl
);
2900 /* Output the assembler code and/or RTL code for variables and functions,
2901 unless the type is an undefined structure or union.
2902 If not, it will get done when the type is completed. */
2904 if (TREE_CODE (decl
) == VAR_DECL
|| TREE_CODE (decl
) == FUNCTION_DECL
)
2906 /* This is a no-op in c-lang.c or something real in objc-act.c. */
2907 if (c_dialect_objc ())
2908 objc_check_decl (decl
);
2910 if (C_DECL_FILE_SCOPE (decl
))
2912 if (DECL_INITIAL (decl
) == NULL_TREE
2913 || DECL_INITIAL (decl
) == error_mark_node
)
2914 /* Don't output anything
2915 when a tentative file-scope definition is seen.
2916 But at end of compilation, do output code for them. */
2917 DECL_DEFER_OUTPUT (decl
) = 1;
2918 rest_of_decl_compilation (decl
, asmspec
, true, 0);
2922 /* This is a local variable. If there is an ASMSPEC, the
2923 user has requested that we handle it specially. */
2926 /* In conjunction with an ASMSPEC, the `register'
2927 keyword indicates that we should place the variable
2928 in a particular register. */
2929 if (DECL_REGISTER (decl
))
2930 DECL_C_HARD_REGISTER (decl
) = 1;
2932 /* If this is not a static variable, issue a warning.
2933 It doesn't make any sense to give an ASMSPEC for an
2934 ordinary, non-register local variable. Historically,
2935 GCC has accepted -- but ignored -- the ASMSPEC in
2937 if (TREE_CODE (decl
) == VAR_DECL
2938 && !DECL_REGISTER (decl
)
2939 && !TREE_STATIC (decl
))
2940 warning ("%Hignoring asm-specifier for non-static local "
2941 "variable '%D'", &DECL_SOURCE_LOCATION (decl
), decl
);
2943 SET_DECL_ASSEMBLER_NAME (decl
, get_identifier (asmspec
));
2946 if (TREE_CODE (decl
) != FUNCTION_DECL
)
2947 add_decl_stmt (decl
);
2950 if (!C_DECL_FILE_SCOPE (decl
))
2952 /* Recompute the RTL of a local array now
2953 if it used to be an incomplete type. */
2955 && ! TREE_STATIC (decl
) && ! DECL_EXTERNAL (decl
))
2957 /* If we used it already as memory, it must stay in memory. */
2958 TREE_ADDRESSABLE (decl
) = TREE_USED (decl
);
2959 /* If it's still incomplete now, no init will save it. */
2960 if (DECL_SIZE (decl
) == 0)
2961 DECL_INITIAL (decl
) = 0;
2966 /* If this was marked 'used', be sure it will be output. */
2967 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl
)))
2968 mark_referenced (DECL_ASSEMBLER_NAME (decl
));
2970 if (TREE_CODE (decl
) == TYPE_DECL
)
2972 /* This is a no-op in c-lang.c or something real in objc-act.c. */
2973 if (c_dialect_objc ())
2974 objc_check_decl (decl
);
2975 rest_of_decl_compilation (decl
, NULL
, C_DECL_FILE_SCOPE (decl
), 0);
2978 /* At the end of a declaration, throw away any variable type sizes
2979 of types defined inside that declaration. There is no use
2980 computing them in the following function definition. */
2981 if (current_scope
== global_scope
)
2982 get_pending_sizes ();
2984 /* Install a cleanup (aka destructor) if one was given. */
2985 if (TREE_CODE (decl
) == VAR_DECL
&& !TREE_STATIC (decl
))
2987 tree attr
= lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl
));
2990 static bool eh_initialized_p
;
2992 tree cleanup_id
= TREE_VALUE (TREE_VALUE (attr
));
2993 tree cleanup_decl
= lookup_name (cleanup_id
);
2996 /* Build "cleanup(&decl)" for the destructor. */
2997 cleanup
= build_unary_op (ADDR_EXPR
, decl
, 0);
2998 cleanup
= build_tree_list (NULL_TREE
, cleanup
);
2999 cleanup
= build_function_call (cleanup_decl
, cleanup
);
3001 /* Don't warn about decl unused; the cleanup uses it. */
3002 TREE_USED (decl
) = 1;
3004 /* Initialize EH, if we've been told to do so. */
3005 if (flag_exceptions
&& !eh_initialized_p
)
3007 eh_initialized_p
= true;
3008 eh_personality_libfunc
3009 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3010 ? "__gcc_personality_sj0"
3011 : "__gcc_personality_v0");
3012 using_eh_for_cleanups ();
3015 add_stmt (build_stmt (CLEANUP_STMT
, decl
, cleanup
));
3020 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3021 and push that on the current scope. */
3024 push_parm_decl (tree parm
)
3028 /* Don't attempt to expand sizes while parsing this decl.
3029 (We can get here with i_s_e 1 somehow from Objective-C.) */
3030 int save_immediate_size_expand
= immediate_size_expand
;
3031 immediate_size_expand
= 0;
3033 decl
= grokdeclarator (TREE_VALUE (TREE_PURPOSE (parm
)),
3034 TREE_PURPOSE (TREE_PURPOSE (parm
)), PARM
, 0);
3035 decl_attributes (&decl
, TREE_VALUE (parm
), 0);
3037 decl
= pushdecl (decl
);
3039 finish_decl (decl
, NULL_TREE
, NULL_TREE
);
3041 immediate_size_expand
= save_immediate_size_expand
;
3044 /* Shift all the existing parameter decls to the variables list,
3045 and reset the parameters list. Used when a ; terminating
3046 forward parameter decls is encountered. */
3049 mark_forward_parm_decls (void)
3053 if (pedantic
&& !current_scope
->warned_forward_parm_decls
)
3055 pedwarn ("ISO C forbids forward parameter declarations");
3056 current_scope
->warned_forward_parm_decls
= true;
3059 for (last
= 0, parm
= current_scope
->parms
;
3061 last
= parm
, parm
= TREE_CHAIN (parm
))
3062 TREE_ASM_WRITTEN (parm
) = 1;
3064 TREE_CHAIN (last
) = current_scope
->names
;
3065 current_scope
->names
= current_scope
->parms
;
3066 current_scope
->parms
= 0;
3069 static GTY(()) int compound_literal_number
;
3071 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3072 literal, which may be an incomplete array type completed by the
3073 initializer; INIT is a CONSTRUCTOR that initializes the compound
3077 build_compound_literal (tree type
, tree init
)
3079 /* We do not use start_decl here because we have a type, not a declarator;
3080 and do not use finish_decl because the decl should be stored inside
3081 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_STMT. */
3082 tree decl
= build_decl (VAR_DECL
, NULL_TREE
, type
);
3085 DECL_EXTERNAL (decl
) = 0;
3086 TREE_PUBLIC (decl
) = 0;
3087 TREE_STATIC (decl
) = (current_scope
== global_scope
);
3088 DECL_CONTEXT (decl
) = current_function_decl
;
3089 TREE_USED (decl
) = 1;
3090 TREE_TYPE (decl
) = type
;
3091 TREE_READONLY (decl
) = TREE_READONLY (type
);
3092 store_init_value (decl
, init
);
3094 if (TREE_CODE (type
) == ARRAY_TYPE
&& !COMPLETE_TYPE_P (type
))
3096 int failure
= complete_array_type (type
, DECL_INITIAL (decl
), 1);
3101 type
= TREE_TYPE (decl
);
3102 if (type
== error_mark_node
|| !COMPLETE_TYPE_P (type
))
3103 return error_mark_node
;
3105 stmt
= build_stmt (DECL_STMT
, decl
);
3106 complit
= build1 (COMPOUND_LITERAL_EXPR
, TREE_TYPE (decl
), stmt
);
3107 TREE_SIDE_EFFECTS (complit
) = 1;
3109 layout_decl (decl
, 0);
3111 if (TREE_STATIC (decl
))
3113 /* This decl needs a name for the assembler output. We also need
3114 a unique suffix to be added to the name. */
3117 ASM_FORMAT_PRIVATE_NAME (name
, "__compound_literal",
3118 compound_literal_number
);
3119 compound_literal_number
++;
3120 DECL_NAME (decl
) = get_identifier (name
);
3121 DECL_DEFER_OUTPUT (decl
) = 1;
3122 DECL_COMDAT (decl
) = 1;
3123 DECL_ARTIFICIAL (decl
) = 1;
3125 rest_of_decl_compilation (decl
, NULL
, 1, 0);
3131 /* Make TYPE a complete type based on INITIAL_VALUE.
3132 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
3133 2 if there was no information (in which case assume 1 if DO_DEFAULT). */
3136 complete_array_type (tree type
, tree initial_value
, int do_default
)
3138 tree maxindex
= NULL_TREE
;
3143 /* Note MAXINDEX is really the maximum index,
3144 one less than the size. */
3145 if (TREE_CODE (initial_value
) == STRING_CST
)
3148 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value
)));
3149 maxindex
= build_int_2 ((TREE_STRING_LENGTH (initial_value
)
3152 else if (TREE_CODE (initial_value
) == CONSTRUCTOR
)
3154 tree elts
= CONSTRUCTOR_ELTS (initial_value
);
3155 maxindex
= build_int_2 (-1, -1);
3156 for (; elts
; elts
= TREE_CHAIN (elts
))
3158 if (TREE_PURPOSE (elts
))
3159 maxindex
= TREE_PURPOSE (elts
);
3161 maxindex
= fold (build (PLUS_EXPR
, integer_type_node
,
3162 maxindex
, integer_one_node
));
3164 maxindex
= copy_node (maxindex
);
3168 /* Make an error message unless that happened already. */
3169 if (initial_value
!= error_mark_node
)
3172 /* Prevent further error messages. */
3173 maxindex
= build_int_2 (0, 0);
3180 maxindex
= build_int_2 (0, 0);
3186 TYPE_DOMAIN (type
) = build_index_type (maxindex
);
3187 if (!TREE_TYPE (maxindex
))
3188 TREE_TYPE (maxindex
) = TYPE_DOMAIN (type
);
3191 /* Lay out the type now that we can get the real answer. */
3198 /* Determine whether TYPE is a structure with a flexible array member,
3199 or a union containing such a structure (possibly recursively). */
3202 flexible_array_type_p (tree type
)
3205 switch (TREE_CODE (type
))
3208 x
= TYPE_FIELDS (type
);
3211 while (TREE_CHAIN (x
) != NULL_TREE
)
3213 if (TREE_CODE (TREE_TYPE (x
)) == ARRAY_TYPE
3214 && TYPE_SIZE (TREE_TYPE (x
)) == NULL_TREE
3215 && TYPE_DOMAIN (TREE_TYPE (x
)) != NULL_TREE
3216 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x
))) == NULL_TREE
)
3220 for (x
= TYPE_FIELDS (type
); x
!= NULL_TREE
; x
= TREE_CHAIN (x
))
3222 if (flexible_array_type_p (TREE_TYPE (x
)))
3231 /* Given declspecs and a declarator,
3232 determine the name and type of the object declared
3233 and construct a ..._DECL node for it.
3234 (In one case we can return a ..._TYPE node instead.
3235 For invalid input we sometimes return 0.)
3237 DECLSPECS is a chain of tree_list nodes whose value fields
3238 are the storage classes and type specifiers.
3240 DECL_CONTEXT says which syntactic context this declaration is in:
3241 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3242 FUNCDEF for a function definition. Like NORMAL but a few different
3243 error messages in each case. Return value may be zero meaning
3244 this definition is too screwy to try to parse.
3245 PARM for a parameter declaration (either within a function prototype
3246 or before a function body). Make a PARM_DECL, or return void_type_node.
3247 TYPENAME if for a typename (in a cast or sizeof).
3248 Don't make a DECL node; just return the ..._TYPE node.
3249 FIELD for a struct or union field; make a FIELD_DECL.
3250 BITFIELD for a field with specified width.
3251 INITIALIZED is 1 if the decl has an initializer.
3253 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3254 It may also be so in the PARM case, for a prototype where the
3255 argument type is specified but not the name.
3257 This function is where the complicated C meanings of `static'
3258 and `extern' are interpreted. */
3261 grokdeclarator (tree declarator
, tree declspecs
,
3262 enum decl_context decl_context
, int initialized
)
3266 tree type
= NULL_TREE
;
3271 int type_quals
= TYPE_UNQUALIFIED
;
3273 int explicit_int
= 0;
3274 int explicit_char
= 0;
3275 int defaulted_int
= 0;
3276 tree typedef_decl
= 0;
3278 tree typedef_type
= 0;
3279 int funcdef_flag
= 0;
3280 enum tree_code innermost_code
= ERROR_MARK
;
3282 int size_varies
= 0;
3283 tree decl_attr
= NULL_TREE
;
3284 tree array_ptr_quals
= NULL_TREE
;
3285 int array_parm_static
= 0;
3286 tree returned_attrs
= NULL_TREE
;
3288 if (decl_context
== BITFIELD
)
3289 bitfield
= 1, decl_context
= FIELD
;
3291 if (decl_context
== FUNCDEF
)
3292 funcdef_flag
= 1, decl_context
= NORMAL
;
3294 /* Look inside a declarator for the name being declared
3295 and get it as a string, for an error message. */
3297 tree decl
= declarator
;
3301 switch (TREE_CODE (decl
))
3306 innermost_code
= TREE_CODE (decl
);
3307 decl
= TREE_OPERAND (decl
, 0);
3311 decl
= TREE_VALUE (decl
);
3314 case IDENTIFIER_NODE
:
3315 name
= IDENTIFIER_POINTER (decl
);
3326 /* A function definition's declarator must have the form of
3327 a function declarator. */
3329 if (funcdef_flag
&& innermost_code
!= CALL_EXPR
)
3332 /* If this looks like a function definition, make it one,
3333 even if it occurs where parms are expected.
3334 Then store_parm_decls will reject it and not use it as a parm. */
3335 if (decl_context
== NORMAL
&& !funcdef_flag
3336 && current_scope
->parm_flag
)
3337 decl_context
= PARM
;
3339 /* Look through the decl specs and record which ones appear.
3340 Some typespecs are defined as built-in typenames.
3341 Others, the ones that are modifiers of other types,
3342 are represented by bits in SPECBITS: set the bits for
3343 the modifiers that appear. Storage class keywords are also in SPECBITS.
3345 If there is a typedef name or a type, store the type in TYPE.
3346 This includes builtin typedefs such as `int'.
3348 Set EXPLICIT_INT or EXPLICIT_CHAR if the type is `int' or `char'
3349 and did not come from a user typedef.
3351 Set LONGLONG if `long' is mentioned twice. */
3353 for (spec
= declspecs
; spec
; spec
= TREE_CHAIN (spec
))
3355 tree id
= TREE_VALUE (spec
);
3357 /* If the entire declaration is itself tagged as deprecated then
3358 suppress reports of deprecated items. */
3359 if (id
&& TREE_DEPRECATED (id
))
3361 if (deprecated_state
!= DEPRECATED_SUPPRESS
)
3362 warn_deprecated_use (id
);
3365 if (id
== ridpointers
[(int) RID_INT
])
3367 if (id
== ridpointers
[(int) RID_CHAR
])
3370 if (TREE_CODE (id
) == IDENTIFIER_NODE
&& C_IS_RESERVED_WORD (id
))
3372 enum rid i
= C_RID_CODE (id
);
3373 if ((int) i
<= (int) RID_LAST_MODIFIER
)
3375 if (i
== RID_LONG
&& (specbits
& (1 << (int) RID_LONG
)))
3378 error ("`long long long' is too long for GCC");
3381 if (pedantic
&& !flag_isoc99
&& ! in_system_header
3383 pedwarn ("ISO C90 does not support `long long'");
3387 else if (specbits
& (1 << (int) i
))
3389 if (i
== RID_CONST
|| i
== RID_VOLATILE
|| i
== RID_RESTRICT
)
3392 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id
));
3395 error ("duplicate `%s'", IDENTIFIER_POINTER (id
));
3398 /* Diagnose "__thread extern". Recall that this list
3399 is in the reverse order seen in the text. */
3401 && (specbits
& (1 << (int) RID_EXTERN
3402 | 1 << (int) RID_STATIC
)))
3404 if (specbits
& 1 << (int) RID_EXTERN
)
3405 error ("`__thread' before `extern'");
3407 error ("`__thread' before `static'");
3410 specbits
|= 1 << (int) i
;
3415 error ("two or more data types in declaration of `%s'", name
);
3416 /* Actual typedefs come to us as TYPE_DECL nodes. */
3417 else if (TREE_CODE (id
) == TYPE_DECL
)
3419 if (TREE_TYPE (id
) == error_mark_node
)
3420 ; /* Allow the type to default to int to avoid cascading errors. */
3423 type
= TREE_TYPE (id
);
3424 decl_attr
= DECL_ATTRIBUTES (id
);
3428 /* Built-in types come as identifiers. */
3429 else if (TREE_CODE (id
) == IDENTIFIER_NODE
)
3431 tree t
= lookup_name (id
);
3432 if (TREE_TYPE (t
) == error_mark_node
)
3434 else if (!t
|| TREE_CODE (t
) != TYPE_DECL
)
3435 error ("`%s' fails to be a typedef or built in type",
3436 IDENTIFIER_POINTER (id
));
3439 type
= TREE_TYPE (t
);
3443 else if (TREE_CODE (id
) != ERROR_MARK
)
3450 typedef_type
= type
;
3452 size_varies
= C_TYPE_VARIABLE_SIZE (type
);
3454 /* No type at all: default to `int', and set DEFAULTED_INT
3455 because it was not a user-defined typedef. */
3459 if ((! (specbits
& ((1 << (int) RID_LONG
) | (1 << (int) RID_SHORT
)
3460 | (1 << (int) RID_SIGNED
)
3461 | (1 << (int) RID_UNSIGNED
)
3462 | (1 << (int) RID_COMPLEX
))))
3463 /* Don't warn about typedef foo = bar. */
3464 && ! (specbits
& (1 << (int) RID_TYPEDEF
) && initialized
)
3465 && ! in_system_header
)
3467 /* Issue a warning if this is an ISO C 99 program or if -Wreturn-type
3468 and this is a function, or if -Wimplicit; prefer the former
3469 warning since it is more explicit. */
3470 if ((warn_implicit_int
|| warn_return_type
|| flag_isoc99
)
3472 warn_about_return_type
= 1;
3473 else if (warn_implicit_int
|| flag_isoc99
)
3474 pedwarn_c99 ("type defaults to `int' in declaration of `%s'",
3479 type
= integer_type_node
;
3482 /* Now process the modifiers that were specified
3483 and check for invalid combinations. */
3485 /* Long double is a special combination. */
3487 if ((specbits
& 1 << (int) RID_LONG
) && ! longlong
3488 && TYPE_MAIN_VARIANT (type
) == double_type_node
)
3490 specbits
&= ~(1 << (int) RID_LONG
);
3491 type
= long_double_type_node
;
3494 /* Check all other uses of type modifiers. */
3496 if (specbits
& ((1 << (int) RID_LONG
) | (1 << (int) RID_SHORT
)
3497 | (1 << (int) RID_UNSIGNED
) | (1 << (int) RID_SIGNED
)))
3501 if ((specbits
& 1 << (int) RID_LONG
)
3502 && (specbits
& 1 << (int) RID_SHORT
))
3503 error ("both long and short specified for `%s'", name
);
3504 else if (((specbits
& 1 << (int) RID_LONG
)
3505 || (specbits
& 1 << (int) RID_SHORT
))
3507 error ("long or short specified with char for `%s'", name
);
3508 else if (((specbits
& 1 << (int) RID_LONG
)
3509 || (specbits
& 1 << (int) RID_SHORT
))
3510 && TREE_CODE (type
) == REAL_TYPE
)
3512 static int already
= 0;
3514 error ("long or short specified with floating type for `%s'", name
);
3515 if (! already
&& ! pedantic
)
3517 error ("the only valid combination is `long double'");
3521 else if ((specbits
& 1 << (int) RID_SIGNED
)
3522 && (specbits
& 1 << (int) RID_UNSIGNED
))
3523 error ("both signed and unsigned specified for `%s'", name
);
3524 else if (TREE_CODE (type
) != INTEGER_TYPE
)
3525 error ("long, short, signed or unsigned invalid for `%s'", name
);
3529 if (!explicit_int
&& !defaulted_int
&& !explicit_char
)
3531 error ("long, short, signed or unsigned used invalidly for `%s'",
3537 /* Discard the type modifiers if they are invalid. */
3540 specbits
&= ~((1 << (int) RID_LONG
) | (1 << (int) RID_SHORT
)
3541 | (1 << (int) RID_UNSIGNED
) | (1 << (int) RID_SIGNED
));
3546 if ((specbits
& (1 << (int) RID_COMPLEX
))
3547 && TREE_CODE (type
) != INTEGER_TYPE
&& TREE_CODE (type
) != REAL_TYPE
)
3549 error ("complex invalid for `%s'", name
);
3550 specbits
&= ~(1 << (int) RID_COMPLEX
);
3553 /* Decide whether an integer type is signed or not.
3554 Optionally treat bitfields as signed by default. */
3555 if (specbits
& 1 << (int) RID_UNSIGNED
3556 || (bitfield
&& ! flag_signed_bitfields
3557 && (explicit_int
|| defaulted_int
|| explicit_char
3558 /* A typedef for plain `int' without `signed'
3559 can be controlled just like plain `int'. */
3560 || ! (typedef_decl
!= 0
3561 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl
)))
3562 && TREE_CODE (type
) != ENUMERAL_TYPE
3563 && !(specbits
& 1 << (int) RID_SIGNED
)))
3566 type
= long_long_unsigned_type_node
;
3567 else if (specbits
& 1 << (int) RID_LONG
)
3568 type
= long_unsigned_type_node
;
3569 else if (specbits
& 1 << (int) RID_SHORT
)
3570 type
= short_unsigned_type_node
;
3571 else if (type
== char_type_node
)
3572 type
= unsigned_char_type_node
;
3573 else if (typedef_decl
)
3574 type
= c_common_unsigned_type (type
);
3576 type
= unsigned_type_node
;
3578 else if ((specbits
& 1 << (int) RID_SIGNED
)
3579 && type
== char_type_node
)
3580 type
= signed_char_type_node
;
3582 type
= long_long_integer_type_node
;
3583 else if (specbits
& 1 << (int) RID_LONG
)
3584 type
= long_integer_type_node
;
3585 else if (specbits
& 1 << (int) RID_SHORT
)
3586 type
= short_integer_type_node
;
3588 if (specbits
& 1 << (int) RID_COMPLEX
)
3590 if (pedantic
&& !flag_isoc99
)
3591 pedwarn ("ISO C90 does not support complex types");
3592 /* If we just have "complex", it is equivalent to
3593 "complex double", but if any modifiers at all are specified it is
3594 the complex form of TYPE. E.g, "complex short" is
3595 "complex short int". */
3597 if (defaulted_int
&& ! longlong
3598 && ! (specbits
& ((1 << (int) RID_LONG
) | (1 << (int) RID_SHORT
)
3599 | (1 << (int) RID_SIGNED
)
3600 | (1 << (int) RID_UNSIGNED
))))
3603 pedwarn ("ISO C does not support plain `complex' meaning `double complex'");
3604 type
= complex_double_type_node
;
3606 else if (type
== integer_type_node
)
3609 pedwarn ("ISO C does not support complex integer types");
3610 type
= complex_integer_type_node
;
3612 else if (type
== float_type_node
)
3613 type
= complex_float_type_node
;
3614 else if (type
== double_type_node
)
3615 type
= complex_double_type_node
;
3616 else if (type
== long_double_type_node
)
3617 type
= complex_long_double_type_node
;
3621 pedwarn ("ISO C does not support complex integer types");
3622 type
= build_complex_type (type
);
3626 /* Figure out the type qualifiers for the declaration. There are
3627 two ways a declaration can become qualified. One is something
3628 like `const int i' where the `const' is explicit. Another is
3629 something like `typedef const int CI; CI i' where the type of the
3630 declaration contains the `const'. */
3631 constp
= !! (specbits
& 1 << (int) RID_CONST
) + TYPE_READONLY (type
);
3632 restrictp
= !! (specbits
& 1 << (int) RID_RESTRICT
) + TYPE_RESTRICT (type
);
3633 volatilep
= !! (specbits
& 1 << (int) RID_VOLATILE
) + TYPE_VOLATILE (type
);
3634 inlinep
= !! (specbits
& (1 << (int) RID_INLINE
));
3635 if (constp
> 1 && ! flag_isoc99
)
3636 pedwarn ("duplicate `const'");
3637 if (restrictp
> 1 && ! flag_isoc99
)
3638 pedwarn ("duplicate `restrict'");
3639 if (volatilep
> 1 && ! flag_isoc99
)
3640 pedwarn ("duplicate `volatile'");
3641 if (! flag_gen_aux_info
&& (TYPE_QUALS (type
)))
3642 type
= TYPE_MAIN_VARIANT (type
);
3643 type_quals
= ((constp
? TYPE_QUAL_CONST
: 0)
3644 | (restrictp
? TYPE_QUAL_RESTRICT
: 0)
3645 | (volatilep
? TYPE_QUAL_VOLATILE
: 0));
3647 /* Warn if two storage classes are given. Default to `auto'. */
3652 if (specbits
& 1 << (int) RID_AUTO
) nclasses
++;
3653 if (specbits
& 1 << (int) RID_STATIC
) nclasses
++;
3654 if (specbits
& 1 << (int) RID_EXTERN
) nclasses
++;
3655 if (specbits
& 1 << (int) RID_REGISTER
) nclasses
++;
3656 if (specbits
& 1 << (int) RID_TYPEDEF
) nclasses
++;
3658 /* "static __thread" and "extern __thread" are allowed. */
3659 if ((specbits
& (1 << (int) RID_THREAD
3660 | 1 << (int) RID_STATIC
3661 | 1 << (int) RID_EXTERN
)) == (1 << (int) RID_THREAD
))
3664 /* Warn about storage classes that are invalid for certain
3665 kinds of declarations (parameters, typenames, etc.). */
3668 error ("multiple storage classes in declaration of `%s'", name
);
3669 else if (funcdef_flag
3671 & ((1 << (int) RID_REGISTER
)
3672 | (1 << (int) RID_AUTO
)
3673 | (1 << (int) RID_TYPEDEF
)
3674 | (1 << (int) RID_THREAD
))))
3676 if (specbits
& 1 << (int) RID_AUTO
3677 && (pedantic
|| current_scope
== global_scope
))
3678 pedwarn ("function definition declared `auto'");
3679 if (specbits
& 1 << (int) RID_REGISTER
)
3680 error ("function definition declared `register'");
3681 if (specbits
& 1 << (int) RID_TYPEDEF
)
3682 error ("function definition declared `typedef'");
3683 if (specbits
& 1 << (int) RID_THREAD
)
3684 error ("function definition declared `__thread'");
3685 specbits
&= ~((1 << (int) RID_TYPEDEF
) | (1 << (int) RID_REGISTER
)
3686 | (1 << (int) RID_AUTO
) | (1 << (int) RID_THREAD
));
3688 else if (decl_context
!= NORMAL
&& nclasses
> 0)
3690 if (decl_context
== PARM
&& specbits
& 1 << (int) RID_REGISTER
)
3694 switch (decl_context
)
3697 error ("storage class specified for structure field `%s'",
3701 error ("storage class specified for parameter `%s'", name
);
3704 error ("storage class specified for typename");
3707 specbits
&= ~((1 << (int) RID_TYPEDEF
) | (1 << (int) RID_REGISTER
)
3708 | (1 << (int) RID_AUTO
) | (1 << (int) RID_STATIC
)
3709 | (1 << (int) RID_EXTERN
) | (1 << (int) RID_THREAD
));
3712 else if (specbits
& 1 << (int) RID_EXTERN
&& initialized
&& ! funcdef_flag
)
3714 /* `extern' with initialization is invalid if not at file scope. */
3715 if (current_scope
== global_scope
)
3716 warning ("`%s' initialized and declared `extern'", name
);
3718 error ("`%s' has both `extern' and initializer", name
);
3720 else if (current_scope
== global_scope
)
3722 if (specbits
& 1 << (int) RID_AUTO
)
3723 error ("file-scope declaration of `%s' specifies `auto'", name
);
3727 if (specbits
& 1 << (int) RID_EXTERN
&& funcdef_flag
)
3728 error ("nested function `%s' declared `extern'", name
);
3729 else if ((specbits
& (1 << (int) RID_THREAD
3730 | 1 << (int) RID_EXTERN
3731 | 1 << (int) RID_STATIC
))
3732 == (1 << (int) RID_THREAD
))
3734 error ("function-scope `%s' implicitly auto and declared `__thread'",
3736 specbits
&= ~(1 << (int) RID_THREAD
);
3741 /* Now figure out the structure of the declarator proper.
3742 Descend through it, creating more complex types, until we reach
3743 the declared identifier (or NULL_TREE, in an absolute declarator). */
3745 while (declarator
&& TREE_CODE (declarator
) != IDENTIFIER_NODE
)
3747 if (type
== error_mark_node
)
3749 declarator
= TREE_OPERAND (declarator
, 0);
3753 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
3754 an INDIRECT_REF (for *...),
3755 a CALL_EXPR (for ...(...)),
3756 a TREE_LIST (for nested attributes),
3757 an identifier (for the name being declared)
3758 or a null pointer (for the place in an absolute declarator
3759 where the name was omitted).
3760 For the last two cases, we have just exited the loop.
3762 At this point, TYPE is the type of elements of an array,
3763 or for a function to return, or for a pointer to point to.
3764 After this sequence of ifs, TYPE is the type of the
3765 array or function or pointer, and DECLARATOR has had its
3766 outermost layer removed. */
3768 if (array_ptr_quals
!= NULL_TREE
|| array_parm_static
)
3770 /* Only the innermost declarator (making a parameter be of
3771 array type which is converted to pointer type)
3772 may have static or type qualifiers. */
3773 error ("static or type qualifiers in non-parameter array declarator");
3774 array_ptr_quals
= NULL_TREE
;
3775 array_parm_static
= 0;
3778 if (TREE_CODE (declarator
) == TREE_LIST
)
3780 /* We encode a declarator with embedded attributes using
3782 tree attrs
= TREE_PURPOSE (declarator
);
3785 declarator
= TREE_VALUE (declarator
);
3786 inner_decl
= declarator
;
3787 while (inner_decl
!= NULL_TREE
3788 && TREE_CODE (inner_decl
) == TREE_LIST
)
3789 inner_decl
= TREE_VALUE (inner_decl
);
3790 if (inner_decl
== NULL_TREE
3791 || TREE_CODE (inner_decl
) == IDENTIFIER_NODE
)
3792 attr_flags
|= (int) ATTR_FLAG_DECL_NEXT
;
3793 else if (TREE_CODE (inner_decl
) == CALL_EXPR
)
3794 attr_flags
|= (int) ATTR_FLAG_FUNCTION_NEXT
;
3795 else if (TREE_CODE (inner_decl
) == ARRAY_REF
)
3796 attr_flags
|= (int) ATTR_FLAG_ARRAY_NEXT
;
3797 returned_attrs
= decl_attributes (&type
,
3798 chainon (returned_attrs
, attrs
),
3801 else if (TREE_CODE (declarator
) == ARRAY_REF
)
3803 tree itype
= NULL_TREE
;
3804 tree size
= TREE_OPERAND (declarator
, 1);
3805 /* The index is a signed object `sizetype' bits wide. */
3806 tree index_type
= c_common_signed_type (sizetype
);
3808 array_ptr_quals
= TREE_TYPE (declarator
);
3809 array_parm_static
= TREE_STATIC (declarator
);
3811 declarator
= TREE_OPERAND (declarator
, 0);
3813 /* Check for some types that there cannot be arrays of. */
3815 if (VOID_TYPE_P (type
))
3817 error ("declaration of `%s' as array of voids", name
);
3818 type
= error_mark_node
;
3821 if (TREE_CODE (type
) == FUNCTION_TYPE
)
3823 error ("declaration of `%s' as array of functions", name
);
3824 type
= error_mark_node
;
3827 if (pedantic
&& flexible_array_type_p (type
))
3828 pedwarn ("invalid use of structure with flexible array member");
3830 if (size
== error_mark_node
)
3831 type
= error_mark_node
;
3833 if (type
== error_mark_node
)
3836 /* If size was specified, set ITYPE to a range-type for that size.
3837 Otherwise, ITYPE remains null. finish_decl may figure it out
3838 from an initial value. */
3842 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3843 STRIP_TYPE_NOPS (size
);
3845 if (! INTEGRAL_TYPE_P (TREE_TYPE (size
)))
3847 error ("size of array `%s' has non-integer type", name
);
3848 size
= integer_one_node
;
3851 if (pedantic
&& integer_zerop (size
))
3852 pedwarn ("ISO C forbids zero-size array `%s'", name
);
3854 if (TREE_CODE (size
) == INTEGER_CST
)
3856 constant_expression_warning (size
);
3857 if (tree_int_cst_sgn (size
) < 0)
3859 error ("size of array `%s' is negative", name
);
3860 size
= integer_one_node
;
3865 /* Make sure the array size remains visibly nonconstant
3866 even if it is (eg) a const variable with known value. */
3869 if (!flag_isoc99
&& pedantic
)
3871 if (TREE_CONSTANT (size
))
3872 pedwarn ("ISO C90 forbids array `%s' whose size can't be evaluated",
3875 pedwarn ("ISO C90 forbids variable-size array `%s'",
3880 if (integer_zerop (size
))
3882 /* A zero-length array cannot be represented with an
3883 unsigned index type, which is what we'll get with
3884 build_index_type. Create an open-ended range instead. */
3885 itype
= build_range_type (sizetype
, size
, NULL_TREE
);
3889 /* Compute the maximum valid index, that is, size - 1.
3890 Do the calculation in index_type, so that if it is
3891 a variable the computations will be done in the
3893 itype
= fold (build (MINUS_EXPR
, index_type
,
3894 convert (index_type
, size
),
3895 convert (index_type
, size_one_node
)));
3897 /* If that overflowed, the array is too big.
3898 ??? While a size of INT_MAX+1 technically shouldn't
3899 cause an overflow (because we subtract 1), the overflow
3900 is recorded during the conversion to index_type, before
3901 the subtraction. Handling this case seems like an
3902 unnecessary complication. */
3903 if (TREE_OVERFLOW (itype
))
3905 error ("size of array `%s' is too large", name
);
3906 type
= error_mark_node
;
3912 /* We must be able to distinguish the
3913 SAVE_EXPR_CONTEXT for the variably-sized type
3914 so that we can set it correctly in
3915 set_save_expr_context. The convention is
3916 that all SAVE_EXPRs that need to be reset
3917 have NULL_TREE for their SAVE_EXPR_CONTEXT. */
3918 tree cfd
= current_function_decl
;
3919 if (decl_context
== PARM
)
3920 current_function_decl
= NULL_TREE
;
3921 itype
= variable_size (itype
);
3922 if (decl_context
== PARM
)
3923 current_function_decl
= cfd
;
3925 itype
= build_index_type (itype
);
3928 else if (decl_context
== FIELD
)
3930 if (pedantic
&& !flag_isoc99
&& !in_system_header
)
3931 pedwarn ("ISO C90 does not support flexible array members");
3933 /* ISO C99 Flexible array members are effectively identical
3934 to GCC's zero-length array extension. */
3935 itype
= build_range_type (sizetype
, size_zero_node
, NULL_TREE
);
3938 /* If pedantic, complain about arrays of incomplete types. */
3940 if (pedantic
&& !COMPLETE_TYPE_P (type
))
3941 pedwarn ("array type has incomplete element type");
3943 /* Build the array type itself, then merge any constancy or
3944 volatility into the target type. We must do it in this order
3945 to ensure that the TYPE_MAIN_VARIANT field of the array type
3946 is set correctly. */
3948 type
= build_array_type (type
, itype
);
3950 type
= c_build_qualified_type (type
, type_quals
);
3953 C_TYPE_VARIABLE_SIZE (type
) = 1;
3955 /* The GCC extension for zero-length arrays differs from
3956 ISO flexible array members in that sizeof yields zero. */
3957 if (size
&& integer_zerop (size
))
3960 TYPE_SIZE (type
) = bitsize_zero_node
;
3961 TYPE_SIZE_UNIT (type
) = size_zero_node
;
3963 if (decl_context
!= PARM
3964 && (array_ptr_quals
!= NULL_TREE
|| array_parm_static
))
3966 error ("static or type qualifiers in non-parameter array declarator");
3967 array_ptr_quals
= NULL_TREE
;
3968 array_parm_static
= 0;
3971 else if (TREE_CODE (declarator
) == CALL_EXPR
)
3975 /* Declaring a function type.
3976 Make sure we have a valid type for the function to return. */
3977 if (type
== error_mark_node
)
3982 /* Warn about some types functions can't return. */
3984 if (TREE_CODE (type
) == FUNCTION_TYPE
)
3986 error ("`%s' declared as function returning a function", name
);
3987 type
= integer_type_node
;
3989 if (TREE_CODE (type
) == ARRAY_TYPE
)
3991 error ("`%s' declared as function returning an array", name
);
3992 type
= integer_type_node
;
3995 /* Construct the function type and go to the next
3996 inner layer of declarator. */
3998 arg_types
= grokparms (TREE_OPERAND (declarator
, 1),
4000 /* Say it's a definition
4001 only for the CALL_EXPR
4002 closest to the identifier. */
4003 && TREE_CODE (TREE_OPERAND (declarator
, 0)) == IDENTIFIER_NODE
);
4004 /* Type qualifiers before the return type of the function
4005 qualify the return type, not the function type. */
4008 /* Type qualifiers on a function return type are normally
4009 permitted by the standard but have no effect, so give a
4010 warning at -Wextra. Qualifiers on a void return type have
4011 meaning as a GNU extension, and are banned on function
4012 definitions in ISO C. FIXME: strictly we shouldn't
4013 pedwarn for qualified void return types except on function
4014 definitions, but not doing so could lead to the undesirable
4015 state of a "volatile void" function return type not being
4016 warned about, and a use of the function being compiled
4017 with GNU semantics, with no diagnostics under -pedantic. */
4018 if (VOID_TYPE_P (type
) && pedantic
&& !in_system_header
)
4019 pedwarn ("ISO C forbids qualified void function return type");
4020 else if (extra_warnings
4021 && !(VOID_TYPE_P (type
)
4022 && type_quals
== TYPE_QUAL_VOLATILE
))
4023 warning ("type qualifiers ignored on function return type");
4025 type
= c_build_qualified_type (type
, type_quals
);
4027 type_quals
= TYPE_UNQUALIFIED
;
4029 type
= build_function_type (type
, arg_types
);
4030 declarator
= TREE_OPERAND (declarator
, 0);
4032 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4033 the formal parameter list of this FUNCTION_TYPE to point to
4034 the FUNCTION_TYPE node itself. */
4039 for (link
= last_function_parm_tags
;
4041 link
= TREE_CHAIN (link
))
4042 TYPE_CONTEXT (TREE_VALUE (link
)) = type
;
4045 else if (TREE_CODE (declarator
) == INDIRECT_REF
)
4047 /* Merge any constancy or volatility into the target type
4050 if (pedantic
&& TREE_CODE (type
) == FUNCTION_TYPE
4052 pedwarn ("ISO C forbids qualified function types");
4054 type
= c_build_qualified_type (type
, type_quals
);
4055 type_quals
= TYPE_UNQUALIFIED
;
4058 type
= build_pointer_type (type
);
4060 /* Process a list of type modifier keywords
4061 (such as const or volatile) that were given inside the `*'. */
4063 if (TREE_TYPE (declarator
))
4071 for (typemodlist
= TREE_TYPE (declarator
); typemodlist
;
4072 typemodlist
= TREE_CHAIN (typemodlist
))
4074 tree qualifier
= TREE_VALUE (typemodlist
);
4076 if (C_IS_RESERVED_WORD (qualifier
))
4078 if (C_RID_CODE (qualifier
) == RID_CONST
)
4080 else if (C_RID_CODE (qualifier
) == RID_VOLATILE
)
4082 else if (C_RID_CODE (qualifier
) == RID_RESTRICT
)
4092 error ("invalid type modifier within pointer declarator");
4093 if (constp
> 1 && ! flag_isoc99
)
4094 pedwarn ("duplicate `const'");
4095 if (volatilep
> 1 && ! flag_isoc99
)
4096 pedwarn ("duplicate `volatile'");
4097 if (restrictp
> 1 && ! flag_isoc99
)
4098 pedwarn ("duplicate `restrict'");
4100 type_quals
= ((constp
? TYPE_QUAL_CONST
: 0)
4101 | (restrictp
? TYPE_QUAL_RESTRICT
: 0)
4102 | (volatilep
? TYPE_QUAL_VOLATILE
: 0));
4105 declarator
= TREE_OPERAND (declarator
, 0);
4112 /* Now TYPE has the actual type. */
4114 /* Did array size calculations overflow? */
4116 if (TREE_CODE (type
) == ARRAY_TYPE
4117 && COMPLETE_TYPE_P (type
)
4118 && TREE_OVERFLOW (TYPE_SIZE (type
)))
4120 error ("size of array `%s' is too large", name
);
4121 /* If we proceed with the array type as it is, we'll eventually
4122 crash in tree_low_cst(). */
4123 type
= error_mark_node
;
4126 /* If this is declaring a typedef name, return a TYPE_DECL. */
4128 if (specbits
& (1 << (int) RID_TYPEDEF
))
4131 /* Note that the grammar rejects storage classes
4132 in typenames, fields or parameters */
4133 if (pedantic
&& TREE_CODE (type
) == FUNCTION_TYPE
4135 pedwarn ("ISO C forbids qualified function types");
4137 type
= c_build_qualified_type (type
, type_quals
);
4138 decl
= build_decl (TYPE_DECL
, declarator
, type
);
4139 if ((specbits
& (1 << (int) RID_SIGNED
))
4140 || (typedef_decl
&& C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl
)))
4141 C_TYPEDEF_EXPLICITLY_SIGNED (decl
) = 1;
4142 decl_attributes (&decl
, returned_attrs
, 0);
4146 /* Detect the case of an array type of unspecified size
4147 which came, as such, direct from a typedef name.
4148 We must copy the type, so that each identifier gets
4149 a distinct type, so that each identifier's size can be
4150 controlled separately by its own initializer. */
4152 if (type
!= 0 && typedef_type
!= 0
4153 && TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
) == 0
4154 && TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (typedef_type
))
4156 type
= build_array_type (TREE_TYPE (type
), 0);
4158 C_TYPE_VARIABLE_SIZE (type
) = 1;
4161 /* If this is a type name (such as, in a cast or sizeof),
4162 compute the type and return it now. */
4164 if (decl_context
== TYPENAME
)
4166 /* Note that the grammar rejects storage classes
4167 in typenames, fields or parameters */
4168 if (pedantic
&& TREE_CODE (type
) == FUNCTION_TYPE
4170 pedwarn ("ISO C forbids const or volatile function types");
4172 type
= c_build_qualified_type (type
, type_quals
);
4173 decl_attributes (&type
, returned_attrs
, 0);
4177 /* Aside from typedefs and type names (handle above),
4178 `void' at top level (not within pointer)
4179 is allowed only in public variables.
4180 We don't complain about parms either, but that is because
4181 a better error message can be made later. */
4183 if (VOID_TYPE_P (type
) && decl_context
!= PARM
4184 && ! ((decl_context
!= FIELD
&& TREE_CODE (type
) != FUNCTION_TYPE
)
4185 && ((specbits
& (1 << (int) RID_EXTERN
))
4186 || (current_scope
== global_scope
4188 & ((1 << (int) RID_STATIC
) | (1 << (int) RID_REGISTER
)))))))
4190 error ("variable or field `%s' declared void", name
);
4191 type
= integer_type_node
;
4194 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4195 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4200 if (decl_context
== PARM
)
4202 tree type_as_written
;
4205 /* A parameter declared as an array of T is really a pointer to T.
4206 One declared as a function is really a pointer to a function. */
4208 if (TREE_CODE (type
) == ARRAY_TYPE
)
4210 /* Transfer const-ness of array into that of type pointed to. */
4211 type
= TREE_TYPE (type
);
4213 type
= c_build_qualified_type (type
, type_quals
);
4214 type
= build_pointer_type (type
);
4215 type_quals
= TYPE_UNQUALIFIED
;
4216 if (array_ptr_quals
)
4218 tree new_ptr_quals
, new_ptr_attrs
;
4220 split_specs_attrs (array_ptr_quals
, &new_ptr_quals
, &new_ptr_attrs
);
4221 /* We don't yet implement attributes in this context. */
4222 if (new_ptr_attrs
!= NULL_TREE
)
4223 warning ("attributes in parameter array declarator ignored");
4228 for (; new_ptr_quals
; new_ptr_quals
= TREE_CHAIN (new_ptr_quals
))
4230 tree qualifier
= TREE_VALUE (new_ptr_quals
);
4232 if (C_IS_RESERVED_WORD (qualifier
))
4234 if (C_RID_CODE (qualifier
) == RID_CONST
)
4236 else if (C_RID_CODE (qualifier
) == RID_VOLATILE
)
4238 else if (C_RID_CODE (qualifier
) == RID_RESTRICT
)
4248 error ("invalid type modifier within array declarator");
4250 type_quals
= ((constp
? TYPE_QUAL_CONST
: 0)
4251 | (restrictp
? TYPE_QUAL_RESTRICT
: 0)
4252 | (volatilep
? TYPE_QUAL_VOLATILE
: 0));
4256 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
4258 if (pedantic
&& type_quals
)
4259 pedwarn ("ISO C forbids qualified function types");
4261 type
= c_build_qualified_type (type
, type_quals
);
4262 type
= build_pointer_type (type
);
4263 type_quals
= TYPE_UNQUALIFIED
;
4265 else if (type_quals
)
4266 type
= c_build_qualified_type (type
, type_quals
);
4268 type_as_written
= type
;
4270 decl
= build_decl (PARM_DECL
, declarator
, type
);
4272 C_DECL_VARIABLE_SIZE (decl
) = 1;
4274 /* Compute the type actually passed in the parmlist,
4275 for the case where there is no prototype.
4276 (For example, shorts and chars are passed as ints.)
4277 When there is a prototype, this is overridden later. */
4279 if (type
== error_mark_node
)
4280 promoted_type
= type
;
4282 promoted_type
= c_type_promotes_to (type
);
4284 DECL_ARG_TYPE (decl
) = promoted_type
;
4285 DECL_ARG_TYPE_AS_WRITTEN (decl
) = type_as_written
;
4287 else if (decl_context
== FIELD
)
4289 /* Structure field. It may not be a function. */
4291 if (TREE_CODE (type
) == FUNCTION_TYPE
)
4293 error ("field `%s' declared as a function", name
);
4294 type
= build_pointer_type (type
);
4296 else if (TREE_CODE (type
) != ERROR_MARK
4297 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type
))
4299 error ("field `%s' has incomplete type", name
);
4300 type
= error_mark_node
;
4302 /* Move type qualifiers down to element of an array. */
4303 if (TREE_CODE (type
) == ARRAY_TYPE
&& type_quals
)
4304 type
= build_array_type (c_build_qualified_type (TREE_TYPE (type
),
4306 TYPE_DOMAIN (type
));
4307 decl
= build_decl (FIELD_DECL
, declarator
, type
);
4308 DECL_NONADDRESSABLE_P (decl
) = bitfield
;
4311 C_DECL_VARIABLE_SIZE (decl
) = 1;
4313 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
4315 /* Every function declaration is "external"
4316 except for those which are inside a function body
4317 in which `auto' is used.
4318 That is a case not specified by ANSI C,
4319 and we use it for forward declarations for nested functions. */
4320 int extern_ref
= (!(specbits
& (1 << (int) RID_AUTO
))
4321 || current_scope
== global_scope
);
4323 if (specbits
& (1 << (int) RID_AUTO
)
4324 && (pedantic
|| current_scope
== global_scope
))
4325 pedwarn ("invalid storage class for function `%s'", name
);
4326 if (specbits
& (1 << (int) RID_REGISTER
))
4327 error ("invalid storage class for function `%s'", name
);
4328 if (specbits
& (1 << (int) RID_THREAD
))
4329 error ("invalid storage class for function `%s'", name
);
4330 /* Function declaration not at file scope.
4331 Storage classes other than `extern' are not allowed
4332 and `extern' makes no difference. */
4333 if (current_scope
!= global_scope
4334 && (specbits
& ((1 << (int) RID_STATIC
) | (1 << (int) RID_INLINE
)))
4336 pedwarn ("invalid storage class for function `%s'", name
);
4338 decl
= build_decl (FUNCTION_DECL
, declarator
, type
);
4339 decl
= build_decl_attribute_variant (decl
, decl_attr
);
4341 DECL_LANG_SPECIFIC (decl
)
4342 = ggc_alloc_cleared (sizeof (struct lang_decl
));
4344 if (pedantic
&& type_quals
&& ! DECL_IN_SYSTEM_HEADER (decl
))
4345 pedwarn ("ISO C forbids qualified function types");
4347 /* GNU C interprets a `volatile void' return type to indicate
4348 that the function does not return. */
4349 if ((type_quals
& TYPE_QUAL_VOLATILE
)
4350 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl
))))
4351 warning ("`noreturn' function returns non-void value");
4354 DECL_EXTERNAL (decl
) = 1;
4355 /* Record absence of global scope for `static' or `auto'. */
4357 = !(specbits
& ((1 << (int) RID_STATIC
) | (1 << (int) RID_AUTO
)));
4360 C_FUNCTION_IMPLICIT_INT (decl
) = 1;
4362 /* Record presence of `inline', if it is reasonable. */
4363 if (MAIN_NAME_P (declarator
))
4366 warning ("cannot inline function `main'");
4370 /* Assume that otherwise the function can be inlined. */
4371 DECL_DECLARED_INLINE_P (decl
) = 1;
4373 /* Do not mark bare declarations as DECL_INLINE. Doing so
4374 in the presence of multiple declarations can result in
4375 the abstract origin pointing between the declarations,
4376 which will confuse dwarf2out. */
4379 DECL_INLINE (decl
) = 1;
4380 if (specbits
& (1 << (int) RID_EXTERN
))
4381 current_extern_inline
= 1;
4384 /* If -finline-functions, assume it can be inlined. This does
4385 two things: let the function be deferred until it is actually
4386 needed, and let dwarf2 know that the function is inlinable. */
4387 else if (flag_inline_trees
== 2 && initialized
)
4389 if (!DECL_INLINE (decl
))
4390 DID_INLINE_FUNC (decl
) = 1;
4391 DECL_INLINE (decl
) = 1;
4392 DECL_DECLARED_INLINE_P (decl
) = 0;
4397 /* It's a variable. */
4398 /* An uninitialized decl with `extern' is a reference. */
4399 int extern_ref
= !initialized
&& (specbits
& (1 << (int) RID_EXTERN
));
4401 /* Move type qualifiers down to element of an array. */
4402 if (TREE_CODE (type
) == ARRAY_TYPE
&& type_quals
)
4404 int saved_align
= TYPE_ALIGN(type
);
4405 type
= build_array_type (c_build_qualified_type (TREE_TYPE (type
),
4407 TYPE_DOMAIN (type
));
4408 TYPE_ALIGN (type
) = saved_align
;
4410 else if (type_quals
)
4411 type
= c_build_qualified_type (type
, type_quals
);
4413 /* It is invalid to create an `extern' declaration for a
4414 variable if there is a global declaration that is
4416 if (extern_ref
&& current_scope
!= global_scope
)
4420 global_decl
= identifier_global_value (declarator
);
4422 && TREE_CODE (global_decl
) == VAR_DECL
4423 && !TREE_PUBLIC (global_decl
))
4424 error ("variable previously declared `static' redeclared "
4428 decl
= build_decl (VAR_DECL
, declarator
, type
);
4430 C_DECL_VARIABLE_SIZE (decl
) = 1;
4433 pedwarn ("%Hvariable '%D' declared `inline'",
4434 &DECL_SOURCE_LOCATION (decl
), decl
);
4436 DECL_EXTERNAL (decl
) = extern_ref
;
4438 /* At file scope, the presence of a `static' or `register' storage
4439 class specifier, or the absence of all storage class specifiers
4440 makes this declaration a definition (perhaps tentative). Also,
4441 the absence of both `static' and `register' makes it public. */
4442 if (current_scope
== global_scope
)
4444 TREE_PUBLIC (decl
) = !(specbits
& ((1 << (int) RID_STATIC
)
4445 | (1 << (int) RID_REGISTER
)));
4446 TREE_STATIC (decl
) = !extern_ref
;
4448 /* Not at file scope, only `static' makes a static definition. */
4451 TREE_STATIC (decl
) = (specbits
& (1 << (int) RID_STATIC
)) != 0;
4452 TREE_PUBLIC (decl
) = extern_ref
;
4455 if (specbits
& 1 << (int) RID_THREAD
)
4457 if (targetm
.have_tls
)
4458 DECL_THREAD_LOCAL (decl
) = 1;
4460 /* A mere warning is sure to result in improper semantics
4461 at runtime. Don't bother to allow this to compile. */
4462 error ("thread-local storage not supported for this target");
4466 /* Record `register' declaration for warnings on &
4467 and in case doing stupid register allocation. */
4469 if (specbits
& (1 << (int) RID_REGISTER
))
4470 DECL_REGISTER (decl
) = 1;
4472 /* Record constancy and volatility. */
4473 c_apply_type_quals_to_decl (type_quals
, decl
);
4475 /* If a type has volatile components, it should be stored in memory.
4476 Otherwise, the fact that those components are volatile
4477 will be ignored, and would even crash the compiler. */
4478 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl
)))
4479 c_mark_addressable (decl
);
4481 #ifdef ENABLE_CHECKING
4482 /* This is the earliest point at which we might know the assembler
4483 name of a variable. Thus, if it's known before this, die horribly. */
4484 if (DECL_ASSEMBLER_NAME_SET_P (decl
))
4488 decl_attributes (&decl
, returned_attrs
, 0);
4494 /* Decode the parameter-list info for a function type or function definition.
4495 The argument is the value returned by `get_parm_info' (or made in parse.y
4496 if there is an identifier list instead of a parameter decl list).
4497 These two functions are separate because when a function returns
4498 or receives functions then each is called multiple times but the order
4499 of calls is different. The last call to `grokparms' is always the one
4500 that contains the formal parameter names of a function definition.
4502 Store in `last_function_parms' a chain of the decls of parms.
4503 Also store in `last_function_parm_tags' a chain of the struct, union,
4504 and enum tags declared among the parms.
4506 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4508 FUNCDEF_FLAG is nonzero for a function definition, 0 for
4509 a mere declaration. A nonempty identifier-list gets an error message
4510 when FUNCDEF_FLAG is zero. */
4513 grokparms (tree parms_info
, int funcdef_flag
)
4515 tree first_parm
= TREE_CHAIN (parms_info
);
4517 last_function_parms
= TREE_PURPOSE (parms_info
);
4518 last_function_parm_tags
= TREE_VALUE (parms_info
);
4519 last_function_parm_vars
= TREE_TYPE (parms_info
);
4521 if (warn_strict_prototypes
&& first_parm
== 0 && !funcdef_flag
4522 && !in_system_header
)
4523 warning ("function declaration isn't a prototype");
4526 && TREE_CODE (TREE_VALUE (first_parm
)) == IDENTIFIER_NODE
)
4529 pedwarn ("parameter names (without types) in function declaration");
4531 last_function_parms
= first_parm
;
4538 /* If the arg types are incomplete in a declaration,
4539 they must include undefined tags.
4540 These tags can never be defined in the scope of the declaration,
4541 so the types can never be completed,
4542 and no call can be compiled successfully. */
4544 for (parm
= last_function_parms
, typelt
= first_parm
;
4546 parm
= TREE_CHAIN (parm
))
4547 /* Skip over any enumeration constants declared here. */
4548 if (TREE_CODE (parm
) == PARM_DECL
)
4550 /* Barf if the parameter itself has an incomplete type. */
4551 tree type
= TREE_VALUE (typelt
);
4552 if (type
== error_mark_node
)
4554 if (!COMPLETE_TYPE_P (type
))
4556 if (funcdef_flag
&& DECL_NAME (parm
) != 0)
4557 error ("parameter `%s' has incomplete type",
4558 IDENTIFIER_POINTER (DECL_NAME (parm
)));
4560 warning ("parameter has incomplete type");
4563 TREE_VALUE (typelt
) = error_mark_node
;
4564 TREE_TYPE (parm
) = error_mark_node
;
4567 typelt
= TREE_CHAIN (typelt
);
4574 /* Return a tree_list node with info on a parameter list just parsed.
4575 The TREE_PURPOSE is a list of decls of those parms.
4576 The TREE_VALUE is a list of structure, union and enum tags defined.
4577 The TREE_CHAIN is a list of argument types to go in the FUNCTION_TYPE.
4578 The TREE_TYPE is a list of non-parameter decls which appeared with the
4580 This tree_list node is later fed to `grokparms'.
4582 VOID_AT_END nonzero means append `void' to the end of the type-list.
4583 Zero means the parmlist ended with an ellipsis so don't append `void'. */
4586 get_parm_info (int void_at_end
)
4588 tree decl
, type
, list
;
4590 tree
*last_type
= &types
;
4591 tree tags
= current_scope
->tags
;
4592 tree parms
= current_scope
->parms
;
4593 tree others
= current_scope
->names
;
4594 static bool explained_incomplete_types
= false;
4595 bool gave_void_only_once_err
= false;
4597 /* Just "void" (and no ellipsis) is special. There are really no parms.
4598 But if the "void" is qualified (by "const" or "volatile"), or has a
4599 storage class specifier ("register"), then the behavior is undefined;
4600 issue an error. Typedefs for "void" are OK (see DR#157). */
4601 if (void_at_end
&& parms
!= 0
4602 && TREE_CHAIN (parms
) == 0
4603 && VOID_TYPE_P (TREE_TYPE (parms
))
4604 && !DECL_NAME (parms
))
4606 if (TREE_THIS_VOLATILE (parms
)
4607 || TREE_READONLY (parms
)
4608 || DECL_REGISTER (parms
))
4609 error ("\"void\" as only parameter may not be qualified");
4611 return tree_cons (0, 0, tree_cons (0, void_type_node
, 0));
4615 current_scope
->parms
= parms
= nreverse (parms
);
4617 /* Sanity check all of the parameter declarations. */
4618 for (decl
= parms
; decl
; decl
= TREE_CHAIN (decl
))
4620 if (TREE_CODE (decl
) != PARM_DECL
)
4622 if (TREE_ASM_WRITTEN (decl
))
4625 /* Since there is a prototype, args are passed in their
4626 declared types. The back end may override this. */
4627 type
= TREE_TYPE (decl
);
4628 DECL_ARG_TYPE (decl
) = type
;
4629 if (PROMOTE_PROTOTYPES
4630 && INTEGRAL_TYPE_P (type
)
4631 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
))
4632 DECL_ARG_TYPE (decl
) = integer_type_node
;
4634 /* Check for (..., void, ...) and issue an error. */
4635 if (VOID_TYPE_P (type
) && !DECL_NAME (decl
) && !gave_void_only_once_err
)
4637 error ("\"void\" must be the only parameter");
4638 gave_void_only_once_err
= true;
4641 type
= build_tree_list (0, type
);
4643 last_type
= &TREE_CHAIN (type
);
4646 /* Check the list of non-parameter decls for any forward parm decls
4647 that never got real decls. */
4648 for (decl
= others
; decl
; decl
= TREE_CHAIN (decl
))
4649 if (TREE_CODE (decl
) == PARM_DECL
)
4651 if (!TREE_ASM_WRITTEN (decl
))
4654 error ("%Hparameter \"%D\" has just a forward declaration",
4655 &DECL_SOURCE_LOCATION (decl
), decl
);
4658 /* Warn about any struct, union or enum tags defined within this
4659 list. The scope of such types is limited to this declaration,
4660 which is rarely if ever desirable (it's impossible to call such
4661 a function with type-correct arguments). */
4662 for (decl
= tags
; decl
; decl
= TREE_CHAIN (decl
))
4664 enum tree_code code
= TREE_CODE (TREE_VALUE (decl
));
4665 const char *keyword
;
4666 /* An anonymous union parm type is meaningful as a GNU extension.
4667 So don't warn for that. */
4668 if (code
== UNION_TYPE
&& TREE_PURPOSE (decl
) == 0 && !pedantic
)
4671 /* The keyword should not be translated. */
4674 case RECORD_TYPE
: keyword
= "struct"; break;
4675 case UNION_TYPE
: keyword
= "union"; break;
4676 case ENUMERAL_TYPE
: keyword
= "enum"; break;
4680 if (TREE_PURPOSE (decl
))
4681 /* The first %s will be one of 'struct', 'union', or 'enum'. */
4682 warning ("\"%s %s\" declared inside parameter list",
4683 keyword
, IDENTIFIER_POINTER (TREE_PURPOSE (decl
)));
4685 /* The %s will be one of 'struct', 'union', or 'enum'. */
4686 warning ("anonymous %s declared inside parameter list", keyword
);
4688 if (! explained_incomplete_types
)
4690 warning ("its scope is only this definition or declaration,"
4691 " which is probably not what you want");
4692 explained_incomplete_types
= true;
4699 type
= build_tree_list (0, void_type_node
);
4703 list
= tree_cons (parms
, tags
, types
);
4704 TREE_TYPE (list
) = others
;
4708 /* Get the struct, enum or union (CODE says which) with tag NAME.
4709 Define the tag as a forward-reference if it is not defined. */
4712 xref_tag (enum tree_code code
, tree name
)
4714 /* If a cross reference is requested, look up the type
4715 already defined for this tag and return it. */
4717 tree ref
= lookup_tag (code
, name
, 0);
4718 /* If this is the right type of tag, return what we found.
4719 (This reference will be shadowed by shadow_tag later if appropriate.)
4720 If this is the wrong type of tag, do not return it. If it was the
4721 wrong type in the same scope, we will have had an error
4722 message already; if in a different scope and declaring
4723 a name, pending_xref_error will give an error message; but if in a
4724 different scope and not declaring a name, this tag should
4725 shadow the previous declaration of a different type of tag, and
4726 this would not work properly if we return the reference found.
4727 (For example, with "struct foo" in an outer scope, "union foo;"
4728 must shadow that tag with a new one of union type.) */
4729 if (ref
&& TREE_CODE (ref
) == code
)
4732 /* If no such tag is yet defined, create a forward-reference node
4733 and record it as the "definition".
4734 When a real declaration of this type is found,
4735 the forward-reference will be altered into a real type. */
4737 ref
= make_node (code
);
4738 if (code
== ENUMERAL_TYPE
)
4740 /* Give the type a default layout like unsigned int
4741 to avoid crashing if it does not get defined. */
4742 TYPE_MODE (ref
) = TYPE_MODE (unsigned_type_node
);
4743 TYPE_ALIGN (ref
) = TYPE_ALIGN (unsigned_type_node
);
4744 TYPE_USER_ALIGN (ref
) = 0;
4745 TREE_UNSIGNED (ref
) = 1;
4746 TYPE_PRECISION (ref
) = TYPE_PRECISION (unsigned_type_node
);
4747 TYPE_MIN_VALUE (ref
) = TYPE_MIN_VALUE (unsigned_type_node
);
4748 TYPE_MAX_VALUE (ref
) = TYPE_MAX_VALUE (unsigned_type_node
);
4751 pushtag (name
, ref
);
4756 /* Make sure that the tag NAME is defined *in the current scope*
4757 at least as a forward reference.
4758 CODE says which kind of tag NAME ought to be. */
4761 start_struct (enum tree_code code
, tree name
)
4763 /* If there is already a tag defined at this scope
4764 (as a forward reference), just return it. */
4769 ref
= lookup_tag (code
, name
, 1);
4770 if (ref
&& TREE_CODE (ref
) == code
)
4772 if (TYPE_FIELDS (ref
))
4774 if (code
== UNION_TYPE
)
4775 error ("redefinition of `union %s'", IDENTIFIER_POINTER (name
));
4777 error ("redefinition of `struct %s'", IDENTIFIER_POINTER (name
));
4782 /* Otherwise create a forward-reference just so the tag is in scope. */
4784 ref
= make_node (code
);
4785 pushtag (name
, ref
);
4788 C_TYPE_BEING_DEFINED (ref
) = 1;
4789 TYPE_PACKED (ref
) = flag_pack_struct
;
4793 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
4794 of a structure component, returning a FIELD_DECL node.
4795 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node.
4797 This is done during the parsing of the struct declaration.
4798 The FIELD_DECL nodes are chained together and the lot of them
4799 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
4802 grokfield (tree declarator
, tree declspecs
, tree width
)
4806 if (declarator
== NULL_TREE
&& width
== NULL_TREE
)
4808 /* This is an unnamed decl.
4810 If we have something of the form "union { list } ;" then this
4811 is the anonymous union extension. Similarly for struct.
4813 If this is something of the form "struct foo;", then
4814 If MS extensions are enabled, this is handled as an
4816 Otherwise this is a forward declaration of a structure tag.
4818 If this is something of the form "foo;" and foo is a TYPE_DECL, then
4819 If MS extensions are enabled and foo names a structure, then
4820 again this is an anonymous struct.
4821 Otherwise this is an error.
4823 Oh what a horrid tangled web we weave. I wonder if MS consciously
4824 took this from Plan 9 or if it was an accident of implementation
4825 that took root before someone noticed the bug... */
4827 tree type
= TREE_VALUE (declspecs
);
4829 if (flag_ms_extensions
&& TREE_CODE (type
) == TYPE_DECL
)
4830 type
= TREE_TYPE (type
);
4831 if (TREE_CODE (type
) == RECORD_TYPE
|| TREE_CODE (type
) == UNION_TYPE
)
4833 if (flag_ms_extensions
)
4836 goto warn_unnamed_field
;
4837 else if (TYPE_NAME (type
) == NULL
)
4840 goto warn_unnamed_field
;
4845 warning ("declaration does not declare anything");
4850 value
= grokdeclarator (declarator
, declspecs
, width
? BITFIELD
: FIELD
, 0);
4852 finish_decl (value
, NULL_TREE
, NULL_TREE
);
4853 DECL_INITIAL (value
) = width
;
4855 if (c_dialect_objc ())
4856 objc_check_decl (value
);
4860 /* Generate an error for any duplicate field names in FIELDLIST. Munge
4861 the list such that this does not present a problem later. */
4864 detect_field_duplicates (tree fieldlist
)
4869 /* First, see if there are more than "a few" fields.
4870 This is trivially true if there are zero or one fields. */
4873 x
= TREE_CHAIN (fieldlist
);
4879 } while (timeout
> 0 && x
);
4881 /* If there were "few" fields, avoid the overhead of allocating
4882 a hash table. Instead just do the nested traversal thing. */
4885 for (x
= TREE_CHAIN (fieldlist
); x
; x
= TREE_CHAIN (x
))
4888 for (y
= fieldlist
; y
!= x
; y
= TREE_CHAIN (y
))
4889 if (DECL_NAME (y
) == DECL_NAME (x
))
4891 error ("%Hduplicate member '%D'",
4892 &DECL_SOURCE_LOCATION (x
), x
);
4893 DECL_NAME (x
) = NULL_TREE
;
4899 htab_t htab
= htab_create (37, htab_hash_pointer
, htab_eq_pointer
, NULL
);
4902 for (x
= fieldlist
; x
; x
= TREE_CHAIN (x
))
4903 if ((y
= DECL_NAME (x
)) != 0)
4905 slot
= htab_find_slot (htab
, y
, INSERT
);
4908 error ("%Hduplicate member '%D'",
4909 &DECL_SOURCE_LOCATION (x
), x
);
4910 DECL_NAME (x
) = NULL_TREE
;
4919 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
4920 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
4921 ATTRIBUTES are attributes to be applied to the structure. */
4924 finish_struct (tree t
, tree fieldlist
, tree attributes
)
4927 int toplevel
= global_scope
== current_scope
;
4928 int saw_named_field
;
4930 /* If this type was previously laid out as a forward reference,
4931 make sure we lay it out again. */
4935 decl_attributes (&t
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
4937 /* Nameless union parm types are useful as GCC extension. */
4938 if (! (TREE_CODE (t
) == UNION_TYPE
&& TYPE_NAME (t
) == 0) && !pedantic
)
4939 /* Otherwise, warn about any struct or union def. in parmlist. */
4940 if (in_parm_level_p ())
4943 pedwarn ("%s defined inside parms",
4944 TREE_CODE (t
) == UNION_TYPE
? _("union") : _("structure"));
4946 warning ("%s defined inside parms",
4947 TREE_CODE (t
) == UNION_TYPE
? _("union") : _("structure"));
4952 for (x
= fieldlist
; x
; x
= TREE_CHAIN (x
))
4953 if (DECL_NAME (x
) != 0)
4957 pedwarn ("%s has no %s",
4958 TREE_CODE (t
) == UNION_TYPE
? _("union") : _("struct"),
4959 fieldlist
? _("named members") : _("members"));
4962 /* Install struct as DECL_CONTEXT of each field decl.
4963 Also process specified field sizes,m which is found in the DECL_INITIAL.
4964 Store 0 there, except for ": 0" fields (so we can find them
4965 and delete them, below). */
4967 saw_named_field
= 0;
4968 for (x
= fieldlist
; x
; x
= TREE_CHAIN (x
))
4970 DECL_CONTEXT (x
) = t
;
4971 DECL_PACKED (x
) |= TYPE_PACKED (t
);
4973 /* If any field is const, the structure type is pseudo-const. */
4974 if (TREE_READONLY (x
))
4975 C_TYPE_FIELDS_READONLY (t
) = 1;
4978 /* A field that is pseudo-const makes the structure likewise. */
4979 tree t1
= TREE_TYPE (x
);
4980 while (TREE_CODE (t1
) == ARRAY_TYPE
)
4981 t1
= TREE_TYPE (t1
);
4982 if ((TREE_CODE (t1
) == RECORD_TYPE
|| TREE_CODE (t1
) == UNION_TYPE
)
4983 && C_TYPE_FIELDS_READONLY (t1
))
4984 C_TYPE_FIELDS_READONLY (t
) = 1;
4987 /* Any field that is volatile means variables of this type must be
4988 treated in some ways as volatile. */
4989 if (TREE_THIS_VOLATILE (x
))
4990 C_TYPE_FIELDS_VOLATILE (t
) = 1;
4992 /* Any field of nominal variable size implies structure is too. */
4993 if (C_DECL_VARIABLE_SIZE (x
))
4994 C_TYPE_VARIABLE_SIZE (t
) = 1;
4996 /* Detect invalid nested redefinition. */
4997 if (TREE_TYPE (x
) == t
)
4998 error ("nested redefinition of `%s'",
4999 IDENTIFIER_POINTER (TYPE_NAME (t
)));
5001 /* Detect invalid bit-field size. */
5002 if (DECL_INITIAL (x
))
5003 STRIP_NOPS (DECL_INITIAL (x
));
5004 if (DECL_INITIAL (x
))
5006 if (TREE_CODE (DECL_INITIAL (x
)) == INTEGER_CST
)
5007 constant_expression_warning (DECL_INITIAL (x
));
5010 error ("%Hbit-field '%D' width not an integer constant",
5011 &DECL_SOURCE_LOCATION (x
), x
);
5012 DECL_INITIAL (x
) = NULL
;
5016 /* Detect invalid bit-field type. */
5017 if (DECL_INITIAL (x
)
5018 && TREE_CODE (TREE_TYPE (x
)) != INTEGER_TYPE
5019 && TREE_CODE (TREE_TYPE (x
)) != BOOLEAN_TYPE
5020 && TREE_CODE (TREE_TYPE (x
)) != ENUMERAL_TYPE
)
5022 error ("%Hbit-field '%D' has invalid type",
5023 &DECL_SOURCE_LOCATION (x
), x
);
5024 DECL_INITIAL (x
) = NULL
;
5027 if (DECL_INITIAL (x
) && pedantic
5028 && TYPE_MAIN_VARIANT (TREE_TYPE (x
)) != integer_type_node
5029 && TYPE_MAIN_VARIANT (TREE_TYPE (x
)) != unsigned_type_node
5030 && TYPE_MAIN_VARIANT (TREE_TYPE (x
)) != boolean_type_node
5031 /* Accept an enum that's equivalent to int or unsigned int. */
5032 && !(TREE_CODE (TREE_TYPE (x
)) == ENUMERAL_TYPE
5033 && (TYPE_PRECISION (TREE_TYPE (x
))
5034 == TYPE_PRECISION (integer_type_node
))))
5035 pedwarn ("%Hbit-field '%D' type invalid in ISO C",
5036 &DECL_SOURCE_LOCATION (x
), x
);
5038 /* Detect and ignore out of range field width and process valid
5040 if (DECL_INITIAL (x
))
5043 = (TYPE_MAIN_VARIANT (TREE_TYPE (x
)) == boolean_type_node
5044 ? CHAR_TYPE_SIZE
: TYPE_PRECISION (TREE_TYPE (x
)));
5046 if (tree_int_cst_sgn (DECL_INITIAL (x
)) < 0)
5047 error ("%Hnegative width in bit-field '%D'",
5048 &DECL_SOURCE_LOCATION (x
), x
);
5049 else if (0 < compare_tree_int (DECL_INITIAL (x
), max_width
))
5050 pedwarn ("%Hwidth of '%D' exceeds its type",
5051 &DECL_SOURCE_LOCATION (x
), x
);
5052 else if (integer_zerop (DECL_INITIAL (x
)) && DECL_NAME (x
) != 0)
5053 error ("%Hzero width for bit-field '%D'",
5054 &DECL_SOURCE_LOCATION (x
), x
);
5057 /* The test above has assured us that TREE_INT_CST_HIGH is 0. */
5058 unsigned HOST_WIDE_INT width
5059 = tree_low_cst (DECL_INITIAL (x
), 1);
5061 if (TREE_CODE (TREE_TYPE (x
)) == ENUMERAL_TYPE
5062 && (width
< min_precision (TYPE_MIN_VALUE (TREE_TYPE (x
)),
5063 TREE_UNSIGNED (TREE_TYPE (x
)))
5065 < min_precision (TYPE_MAX_VALUE (TREE_TYPE (x
)),
5066 TREE_UNSIGNED (TREE_TYPE (x
))))))
5067 warning ("%H'%D' is narrower than values of its type",
5068 &DECL_SOURCE_LOCATION (x
), x
);
5070 DECL_SIZE (x
) = bitsize_int (width
);
5071 DECL_BIT_FIELD (x
) = 1;
5072 SET_DECL_C_BIT_FIELD (x
);
5076 DECL_INITIAL (x
) = 0;
5078 /* Detect flexible array member in an invalid context. */
5079 if (TREE_CODE (TREE_TYPE (x
)) == ARRAY_TYPE
5080 && TYPE_SIZE (TREE_TYPE (x
)) == NULL_TREE
5081 && TYPE_DOMAIN (TREE_TYPE (x
)) != NULL_TREE
5082 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x
))) == NULL_TREE
)
5084 if (TREE_CODE (t
) == UNION_TYPE
)
5085 error ("%Hflexible array member in union",
5086 &DECL_SOURCE_LOCATION (x
));
5087 else if (TREE_CHAIN (x
) != NULL_TREE
)
5088 error ("%Hflexible array member not at end of struct",
5089 &DECL_SOURCE_LOCATION (x
));
5090 else if (! saw_named_field
)
5091 error ("%Hflexible array member in otherwise empty struct",
5092 &DECL_SOURCE_LOCATION (x
));
5095 if (pedantic
&& TREE_CODE (t
) == RECORD_TYPE
5096 && flexible_array_type_p (TREE_TYPE (x
)))
5097 pedwarn ("%Hinvalid use of structure with flexible array member",
5098 &DECL_SOURCE_LOCATION (x
));
5101 saw_named_field
= 1;
5104 detect_field_duplicates (fieldlist
);
5106 /* Now we have the nearly final fieldlist. Record it,
5107 then lay out the structure or union (including the fields). */
5109 TYPE_FIELDS (t
) = fieldlist
;
5113 /* Delete all zero-width bit-fields from the fieldlist */
5115 tree
*fieldlistp
= &fieldlist
;
5117 if (TREE_CODE (*fieldlistp
) == FIELD_DECL
&& DECL_INITIAL (*fieldlistp
))
5118 *fieldlistp
= TREE_CHAIN (*fieldlistp
);
5120 fieldlistp
= &TREE_CHAIN (*fieldlistp
);
5123 /* Now we have the truly final field list.
5124 Store it in this type and in the variants. */
5126 TYPE_FIELDS (t
) = fieldlist
;
5128 /* If there are lots of fields, sort so we can look through them fast.
5129 We arbitrarily consider 16 or more elts to be "a lot". */
5134 for (x
= fieldlist
; x
; x
= TREE_CHAIN (x
))
5136 if (len
> 15 || DECL_NAME (x
) == NULL
)
5144 struct lang_type
*space
;
5145 struct sorted_fields_type
*space2
;
5147 len
+= list_length (x
);
5149 /* Use the same allocation policy here that make_node uses, to
5150 ensure that this lives as long as the rest of the struct decl.
5151 All decls in an inline function need to be saved. */
5153 space
= ggc_alloc (sizeof (struct lang_type
));
5154 space2
= ggc_alloc (sizeof (struct sorted_fields_type
) + len
* sizeof (tree
));
5158 field_array
= &space2
->elts
[0];
5159 for (x
= fieldlist
; x
; x
= TREE_CHAIN (x
))
5161 field_array
[len
++] = x
;
5163 /* If there is anonymous struct or union, break out of the loop. */
5164 if (DECL_NAME (x
) == NULL
)
5167 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5170 TYPE_LANG_SPECIFIC (t
) = space
;
5171 TYPE_LANG_SPECIFIC (t
)->s
->len
= len
;
5172 field_array
= TYPE_LANG_SPECIFIC (t
)->s
->elts
;
5173 qsort (field_array
, len
, sizeof (tree
), field_decl_cmp
);
5178 for (x
= TYPE_MAIN_VARIANT (t
); x
; x
= TYPE_NEXT_VARIANT (x
))
5180 TYPE_FIELDS (x
) = TYPE_FIELDS (t
);
5181 TYPE_LANG_SPECIFIC (x
) = TYPE_LANG_SPECIFIC (t
);
5182 TYPE_ALIGN (x
) = TYPE_ALIGN (t
);
5183 TYPE_USER_ALIGN (x
) = TYPE_USER_ALIGN (t
);
5186 /* If this was supposed to be a transparent union, but we can't
5187 make it one, warn and turn off the flag. */
5188 if (TREE_CODE (t
) == UNION_TYPE
5189 && TYPE_TRANSPARENT_UNION (t
)
5190 && TYPE_MODE (t
) != DECL_MODE (TYPE_FIELDS (t
)))
5192 TYPE_TRANSPARENT_UNION (t
) = 0;
5193 warning ("union cannot be made transparent");
5196 /* If this structure or union completes the type of any previous
5197 variable declaration, lay it out and output its rtl. */
5199 if (current_scope
->incomplete_list
!= NULL_TREE
)
5201 tree prev
= NULL_TREE
;
5203 for (x
= current_scope
->incomplete_list
; x
; x
= TREE_CHAIN (x
))
5205 tree decl
= TREE_VALUE (x
);
5207 if (TYPE_MAIN_VARIANT (TREE_TYPE (decl
)) == TYPE_MAIN_VARIANT (t
)
5208 && TREE_CODE (decl
) != TYPE_DECL
)
5210 layout_decl (decl
, 0);
5211 /* This is a no-op in c-lang.c or something real in objc-act.c. */
5212 if (c_dialect_objc ())
5213 objc_check_decl (decl
);
5214 rest_of_decl_compilation (decl
, NULL
, toplevel
, 0);
5217 /* Unlink X from the incomplete list. */
5219 TREE_CHAIN (prev
) = TREE_CHAIN (x
);
5221 current_scope
->incomplete_list
= TREE_CHAIN (x
);
5223 else if (!COMPLETE_TYPE_P (TREE_TYPE (decl
))
5224 && TREE_CODE (TREE_TYPE (decl
)) == ARRAY_TYPE
)
5226 tree element
= TREE_TYPE (decl
);
5227 while (TREE_CODE (element
) == ARRAY_TYPE
)
5228 element
= TREE_TYPE (element
);
5231 layout_array_type (TREE_TYPE (decl
));
5232 if (TREE_CODE (decl
) != TYPE_DECL
)
5234 layout_decl (decl
, 0);
5235 if (c_dialect_objc ())
5236 objc_check_decl (decl
);
5237 rest_of_decl_compilation (decl
, NULL
, toplevel
, 0);
5241 /* Unlink X from the incomplete list. */
5243 TREE_CHAIN (prev
) = TREE_CHAIN (x
);
5245 current_scope
->incomplete_list
= TREE_CHAIN (x
);
5251 /* Finish debugging output for this type. */
5252 rest_of_type_compilation (t
, toplevel
);
5257 /* Lay out the type T, and its element type, and so on. */
5260 layout_array_type (tree t
)
5262 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
5263 layout_array_type (TREE_TYPE (t
));
5267 /* Begin compiling the definition of an enumeration type.
5268 NAME is its name (or null if anonymous).
5269 Returns the type object, as yet incomplete.
5270 Also records info about it so that build_enumerator
5271 may be used to declare the individual values as they are read. */
5274 start_enum (tree name
)
5278 /* If this is the real definition for a previous forward reference,
5279 fill in the contents in the same object that used to be the
5280 forward reference. */
5283 enumtype
= lookup_tag (ENUMERAL_TYPE
, name
, 1);
5285 if (enumtype
== 0 || TREE_CODE (enumtype
) != ENUMERAL_TYPE
)
5287 enumtype
= make_node (ENUMERAL_TYPE
);
5288 pushtag (name
, enumtype
);
5291 C_TYPE_BEING_DEFINED (enumtype
) = 1;
5293 if (TYPE_VALUES (enumtype
) != 0)
5295 /* This enum is a named one that has been declared already. */
5296 error ("redeclaration of `enum %s'", IDENTIFIER_POINTER (name
));
5298 /* Completely replace its old definition.
5299 The old enumerators remain defined, however. */
5300 TYPE_VALUES (enumtype
) = 0;
5303 enum_next_value
= integer_zero_node
;
5306 if (flag_short_enums
)
5307 TYPE_PACKED (enumtype
) = 1;
5312 /* After processing and defining all the values of an enumeration type,
5313 install their decls in the enumeration type and finish it off.
5314 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5315 and ATTRIBUTES are the specified attributes.
5316 Returns ENUMTYPE. */
5319 finish_enum (tree enumtype
, tree values
, tree attributes
)
5322 tree minnode
= 0, maxnode
= 0, enum_value_type
;
5323 int precision
, unsign
;
5324 int toplevel
= (global_scope
== current_scope
);
5326 if (in_parm_level_p ())
5327 warning ("enum defined inside parms");
5329 decl_attributes (&enumtype
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
5331 /* Calculate the maximum value of any enumerator in this type. */
5333 if (values
== error_mark_node
)
5334 minnode
= maxnode
= integer_zero_node
;
5337 minnode
= maxnode
= TREE_VALUE (values
);
5338 for (pair
= TREE_CHAIN (values
); pair
; pair
= TREE_CHAIN (pair
))
5340 tree value
= TREE_VALUE (pair
);
5341 if (tree_int_cst_lt (maxnode
, value
))
5343 if (tree_int_cst_lt (value
, minnode
))
5348 /* Construct the final type of this enumeration. It is the same
5349 as one of the integral types - the narrowest one that fits, except
5350 that normally we only go as narrow as int - and signed iff any of
5351 the values are negative. */
5352 unsign
= (tree_int_cst_sgn (minnode
) >= 0);
5353 precision
= MAX (min_precision (minnode
, unsign
),
5354 min_precision (maxnode
, unsign
));
5355 if (TYPE_PACKED (enumtype
) || precision
> TYPE_PRECISION (integer_type_node
))
5357 tree narrowest
= c_common_type_for_size (precision
, unsign
);
5360 warning ("enumeration values exceed range of largest integer");
5361 narrowest
= long_long_integer_type_node
;
5364 precision
= TYPE_PRECISION (narrowest
);
5367 precision
= TYPE_PRECISION (integer_type_node
);
5369 if (precision
== TYPE_PRECISION (integer_type_node
))
5370 enum_value_type
= c_common_type_for_size (precision
, 0);
5372 enum_value_type
= enumtype
;
5374 TYPE_MIN_VALUE (enumtype
) = minnode
;
5375 TYPE_MAX_VALUE (enumtype
) = maxnode
;
5376 TYPE_PRECISION (enumtype
) = precision
;
5377 TREE_UNSIGNED (enumtype
) = unsign
;
5378 TYPE_SIZE (enumtype
) = 0;
5379 layout_type (enumtype
);
5381 if (values
!= error_mark_node
)
5383 /* Change the type of the enumerators to be the enum type. We
5384 need to do this irrespective of the size of the enum, for
5385 proper type checking. Replace the DECL_INITIALs of the
5386 enumerators, and the value slots of the list, with copies
5387 that have the enum type; they cannot be modified in place
5388 because they may be shared (e.g. integer_zero_node) Finally,
5389 change the purpose slots to point to the names of the decls. */
5390 for (pair
= values
; pair
; pair
= TREE_CHAIN (pair
))
5392 tree enu
= TREE_PURPOSE (pair
);
5394 TREE_TYPE (enu
) = enumtype
;
5396 /* The ISO C Standard mandates enumerators to have type int,
5397 even though the underlying type of an enum type is
5398 unspecified. Here we convert any enumerators that fit in
5399 an int to type int, to avoid promotions to unsigned types
5400 when comparing integers with enumerators that fit in the
5401 int range. When -pedantic is given, build_enumerator()
5402 would have already taken care of those that don't fit. */
5403 if (int_fits_type_p (DECL_INITIAL (enu
), enum_value_type
))
5404 DECL_INITIAL (enu
) = convert (enum_value_type
, DECL_INITIAL (enu
));
5406 DECL_INITIAL (enu
) = convert (enumtype
, DECL_INITIAL (enu
));
5408 TREE_PURPOSE (pair
) = DECL_NAME (enu
);
5409 TREE_VALUE (pair
) = DECL_INITIAL (enu
);
5412 TYPE_VALUES (enumtype
) = values
;
5415 /* Fix up all variant types of this enum type. */
5416 for (tem
= TYPE_MAIN_VARIANT (enumtype
); tem
; tem
= TYPE_NEXT_VARIANT (tem
))
5418 if (tem
== enumtype
)
5420 TYPE_VALUES (tem
) = TYPE_VALUES (enumtype
);
5421 TYPE_MIN_VALUE (tem
) = TYPE_MIN_VALUE (enumtype
);
5422 TYPE_MAX_VALUE (tem
) = TYPE_MAX_VALUE (enumtype
);
5423 TYPE_SIZE (tem
) = TYPE_SIZE (enumtype
);
5424 TYPE_SIZE_UNIT (tem
) = TYPE_SIZE_UNIT (enumtype
);
5425 TYPE_MODE (tem
) = TYPE_MODE (enumtype
);
5426 TYPE_PRECISION (tem
) = TYPE_PRECISION (enumtype
);
5427 TYPE_ALIGN (tem
) = TYPE_ALIGN (enumtype
);
5428 TYPE_USER_ALIGN (tem
) = TYPE_USER_ALIGN (enumtype
);
5429 TREE_UNSIGNED (tem
) = TREE_UNSIGNED (enumtype
);
5432 /* Finish debugging output for this type. */
5433 rest_of_type_compilation (enumtype
, toplevel
);
5438 /* Build and install a CONST_DECL for one value of the
5439 current enumeration type (one that was begun with start_enum).
5440 Return a tree-list containing the CONST_DECL and its value.
5441 Assignment of sequential values by default is handled here. */
5444 build_enumerator (tree name
, tree value
)
5448 /* Validate and default VALUE. */
5450 /* Remove no-op casts from the value. */
5452 STRIP_TYPE_NOPS (value
);
5456 if (TREE_CODE (value
) == INTEGER_CST
)
5458 value
= default_conversion (value
);
5459 constant_expression_warning (value
);
5463 error ("enumerator value for `%s' not integer constant",
5464 IDENTIFIER_POINTER (name
));
5469 /* Default based on previous value. */
5470 /* It should no longer be possible to have NON_LVALUE_EXPR
5474 value
= enum_next_value
;
5476 error ("overflow in enumeration values");
5479 if (pedantic
&& ! int_fits_type_p (value
, integer_type_node
))
5481 pedwarn ("ISO C restricts enumerator values to range of `int'");
5482 value
= convert (integer_type_node
, value
);
5485 /* Set basis for default for next value. */
5486 enum_next_value
= build_binary_op (PLUS_EXPR
, value
, integer_one_node
, 0);
5487 enum_overflow
= tree_int_cst_lt (enum_next_value
, value
);
5489 /* Now create a declaration for the enum value name. */
5491 type
= TREE_TYPE (value
);
5492 type
= c_common_type_for_size (MAX (TYPE_PRECISION (type
),
5493 TYPE_PRECISION (integer_type_node
)),
5494 (TYPE_PRECISION (type
)
5495 >= TYPE_PRECISION (integer_type_node
)
5496 && TREE_UNSIGNED (type
)));
5498 decl
= build_decl (CONST_DECL
, name
, type
);
5499 DECL_INITIAL (decl
) = convert (type
, value
);
5502 return tree_cons (decl
, value
, NULL_TREE
);
5506 /* Create the FUNCTION_DECL for a function definition.
5507 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5508 the declaration; they describe the function's name and the type it returns,
5509 but twisted together in a fashion that parallels the syntax of C.
5511 This function creates a binding context for the function body
5512 as well as setting up the FUNCTION_DECL in current_function_decl.
5514 Returns 1 on success. If the DECLARATOR is not suitable for a function
5515 (it defines a datum instead), we return 0, which tells
5516 yyparse to report a parse error. */
5519 start_function (tree declspecs
, tree declarator
, tree attributes
)
5521 tree decl1
, old_decl
;
5523 int old_immediate_size_expand
= immediate_size_expand
;
5525 current_function_returns_value
= 0; /* Assume, until we see it does. */
5526 current_function_returns_null
= 0;
5527 current_function_returns_abnormally
= 0;
5528 warn_about_return_type
= 0;
5529 current_extern_inline
= 0;
5531 /* Don't expand any sizes in the return type of the function. */
5532 immediate_size_expand
= 0;
5534 decl1
= grokdeclarator (declarator
, declspecs
, FUNCDEF
, 1);
5536 /* If the declarator is not suitable for a function definition,
5537 cause a syntax error. */
5540 immediate_size_expand
= old_immediate_size_expand
;
5544 decl_attributes (&decl1
, attributes
, 0);
5546 if (DECL_DECLARED_INLINE_P (decl1
)
5547 && DECL_UNINLINABLE (decl1
)
5548 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1
)))
5549 warning ("%Hinline function '%D' given attribute noinline",
5550 &DECL_SOURCE_LOCATION (decl1
), decl1
);
5552 announce_function (decl1
);
5554 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1
))))
5556 error ("return type is an incomplete type");
5557 /* Make it return void instead. */
5559 = build_function_type (void_type_node
,
5560 TYPE_ARG_TYPES (TREE_TYPE (decl1
)));
5563 if (warn_about_return_type
)
5564 pedwarn_c99 ("return type defaults to `int'");
5566 /* Save the parm names or decls from this function's declarator
5567 where store_parm_decls will find them. */
5568 current_function_parms
= last_function_parms
;
5569 current_function_parm_tags
= last_function_parm_tags
;
5570 current_function_parm_vars
= last_function_parm_vars
;
5572 /* Make the init_value nonzero so pushdecl knows this is not tentative.
5573 error_mark_node is replaced below (in poplevel) with the BLOCK. */
5574 DECL_INITIAL (decl1
) = error_mark_node
;
5576 /* If this definition isn't a prototype and we had a prototype declaration
5577 before, copy the arg type info from that prototype.
5578 But not if what we had before was a builtin function. */
5579 old_decl
= lookup_name_current_level (DECL_NAME (decl1
));
5580 if (old_decl
!= 0 && TREE_CODE (TREE_TYPE (old_decl
)) == FUNCTION_TYPE
5581 && !DECL_BUILT_IN (old_decl
)
5582 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1
)))
5583 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (old_decl
))))
5584 && TYPE_ARG_TYPES (TREE_TYPE (decl1
)) == 0)
5586 TREE_TYPE (decl1
) = TREE_TYPE (old_decl
);
5587 current_function_prototype_locus
= DECL_SOURCE_LOCATION (old_decl
);
5590 /* Optionally warn of old-fashioned def with no previous prototype. */
5591 if (warn_strict_prototypes
5592 && TYPE_ARG_TYPES (TREE_TYPE (decl1
)) == 0
5593 && C_DECL_ISNT_PROTOTYPE (old_decl
))
5594 warning ("function declaration isn't a prototype");
5595 /* Optionally warn of any global def with no previous prototype. */
5596 else if (warn_missing_prototypes
5597 && TREE_PUBLIC (decl1
)
5598 && ! MAIN_NAME_P (DECL_NAME (decl1
))
5599 && C_DECL_ISNT_PROTOTYPE (old_decl
))
5600 warning ("%Hno previous prototype for '%D'",
5601 &DECL_SOURCE_LOCATION (decl1
), decl1
);
5602 /* Optionally warn of any def with no previous prototype
5603 if the function has already been used. */
5604 else if (warn_missing_prototypes
5605 && old_decl
!= 0 && TREE_USED (old_decl
)
5606 && TYPE_ARG_TYPES (TREE_TYPE (old_decl
)) == 0)
5607 warning ("%H'%D' was used with no prototype before its definition",
5608 &DECL_SOURCE_LOCATION (decl1
), decl1
);
5609 /* Optionally warn of any global def with no previous declaration. */
5610 else if (warn_missing_declarations
5611 && TREE_PUBLIC (decl1
)
5613 && ! MAIN_NAME_P (DECL_NAME (decl1
)))
5614 warning ("%Hno previous declaration for '%D'",
5615 &DECL_SOURCE_LOCATION (decl1
), decl1
);
5616 /* Optionally warn of any def with no previous declaration
5617 if the function has already been used. */
5618 else if (warn_missing_declarations
5619 && old_decl
!= 0 && TREE_USED (old_decl
)
5620 && C_DECL_IMPLICIT (old_decl
))
5621 warning ("%H`%D' was used with no declaration before its definition",
5622 &DECL_SOURCE_LOCATION (decl1
), decl1
);
5624 /* This is a definition, not a reference.
5625 So normally clear DECL_EXTERNAL.
5626 However, `extern inline' acts like a declaration
5627 except for defining how to inline. So set DECL_EXTERNAL in that case. */
5628 DECL_EXTERNAL (decl1
) = current_extern_inline
;
5630 /* This function exists in static storage.
5631 (This does not mean `static' in the C sense!) */
5632 TREE_STATIC (decl1
) = 1;
5634 /* A nested function is not global. */
5635 if (current_function_decl
!= 0)
5636 TREE_PUBLIC (decl1
) = 0;
5638 #ifdef ENABLE_CHECKING
5639 /* This is the earliest point at which we might know the assembler
5640 name of the function. Thus, if it's set before this, die horribly. */
5641 if (DECL_ASSEMBLER_NAME_SET_P (decl1
))
5645 /* If #pragma weak was used, mark the decl weak now. */
5646 if (current_scope
== global_scope
)
5647 maybe_apply_pragma_weak (decl1
);
5649 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
5650 if (warn_main
> 0 && MAIN_NAME_P (DECL_NAME (decl1
)))
5654 const location_t
*locus
= &DECL_SOURCE_LOCATION (decl1
);
5656 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1
)))
5657 != integer_type_node
)
5658 pedwarn ("%Hreturn type of '%D' is not `int'", locus
, decl1
);
5660 for (args
= TYPE_ARG_TYPES (TREE_TYPE (decl1
)); args
;
5661 args
= TREE_CHAIN (args
))
5663 tree type
= args
? TREE_VALUE (args
) : 0;
5665 if (type
== void_type_node
)
5672 if (TYPE_MAIN_VARIANT (type
) != integer_type_node
)
5673 pedwarn ("%Hfirst argument of '%D' should be `int'",
5678 if (TREE_CODE (type
) != POINTER_TYPE
5679 || TREE_CODE (TREE_TYPE (type
)) != POINTER_TYPE
5680 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type
)))
5682 pedwarn ("%Hsecond argument of '%D' should be 'char **'",
5687 if (TREE_CODE (type
) != POINTER_TYPE
5688 || TREE_CODE (TREE_TYPE (type
)) != POINTER_TYPE
5689 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type
)))
5691 pedwarn ("%Hthird argument of '%D' should probably be "
5692 "'char **'", locus
, decl1
);
5697 /* It is intentional that this message does not mention the third
5698 argument because it's only mentioned in an appendix of the
5700 if (argct
> 0 && (argct
< 2 || argct
> 3))
5701 pedwarn ("%H'%D' takes only zero or two arguments", locus
, decl1
);
5703 if (! TREE_PUBLIC (decl1
))
5704 pedwarn ("%H'%D' is normally a non-static function", locus
, decl1
);
5707 /* Record the decl so that the function name is defined.
5708 If we already have a decl for this name, and it is a FUNCTION_DECL,
5709 use the old decl. */
5711 current_function_decl
= pushdecl (decl1
);
5714 declare_parm_level ();
5716 make_decl_rtl (current_function_decl
, NULL
);
5718 restype
= TREE_TYPE (TREE_TYPE (current_function_decl
));
5719 /* Promote the value to int before returning it. */
5720 if (c_promoting_integer_type_p (restype
))
5722 /* It retains unsignedness if not really getting wider. */
5723 if (TREE_UNSIGNED (restype
)
5724 && (TYPE_PRECISION (restype
)
5725 == TYPE_PRECISION (integer_type_node
)))
5726 restype
= unsigned_type_node
;
5728 restype
= integer_type_node
;
5730 DECL_RESULT (current_function_decl
)
5731 = build_decl (RESULT_DECL
, NULL_TREE
, restype
);
5733 /* If this fcn was already referenced via a block-scope `extern' decl
5734 (or an implicit decl), propagate certain information about the usage. */
5735 if (TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (current_function_decl
)))
5736 TREE_ADDRESSABLE (current_function_decl
) = 1;
5738 immediate_size_expand
= old_immediate_size_expand
;
5740 start_fname_decls ();
5745 /* Subroutine of store_parm_decls which handles new-style function
5746 definitions (prototype format). The parms already have decls, so we
5747 need only record them as in effect and complain if any redundant
5748 old-style parm decls were written. */
5750 store_parm_decls_newstyle (void)
5753 tree fndecl
= current_function_decl
;
5754 tree parms
= current_function_parms
;
5755 tree tags
= current_function_parm_tags
;
5756 tree vars
= current_function_parm_vars
;
5758 if (current_scope
->parms
|| current_scope
->names
|| current_scope
->tags
)
5760 error ("%Hold-style parameter declarations in prototyped "
5761 "function definition", &DECL_SOURCE_LOCATION (fndecl
));
5763 /* Get rid of the old-style declarations. */
5768 /* Now make all the parameter declarations visible in the function body.
5769 We can bypass most of the grunt work of pushdecl. */
5770 for (decl
= parms
; decl
; decl
= TREE_CHAIN (decl
))
5772 DECL_CONTEXT (decl
) = current_function_decl
;
5774 if (DECL_NAME (decl
) == 0)
5775 error ("%Hparameter name omitted", &DECL_SOURCE_LOCATION (decl
));
5778 if (IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl
)))
5779 current_scope
->shadowed
5780 = tree_cons (DECL_NAME (decl
),
5781 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl
)),
5782 current_scope
->shadowed
);
5783 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl
)) = decl
;
5786 current_scope
->parms
= parms
;
5788 /* Record the parameter list in the function declaration. */
5789 DECL_ARGUMENTS (fndecl
) = parms
;
5791 /* Now make all the ancillary declarations visible, likewise. */
5792 for (decl
= vars
; decl
; decl
= TREE_CHAIN (decl
))
5794 DECL_CONTEXT (decl
) = current_function_decl
;
5795 if (DECL_NAME (decl
)
5796 && TYPE_MAIN_VARIANT (TREE_TYPE (decl
)) != void_type_node
)
5798 if (IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl
)))
5799 current_scope
->shadowed
5800 = tree_cons (DECL_NAME (decl
),
5801 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl
)),
5802 current_scope
->shadowed
);
5803 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl
)) = decl
;
5806 current_scope
->names
= vars
;
5808 /* And all the tag declarations. */
5809 for (decl
= tags
; decl
; decl
= TREE_CHAIN (decl
))
5810 if (TREE_PURPOSE (decl
))
5812 if (IDENTIFIER_TAG_VALUE (TREE_PURPOSE (decl
)))
5813 current_scope
->shadowed_tags
5814 = tree_cons (TREE_PURPOSE (decl
),
5815 IDENTIFIER_SYMBOL_VALUE (TREE_PURPOSE (decl
)),
5816 current_scope
->shadowed_tags
);
5817 IDENTIFIER_TAG_VALUE (TREE_PURPOSE (decl
)) = TREE_VALUE (decl
);
5819 current_scope
->tags
= tags
;
5822 /* Subroutine of store_parm_decls which handles old-style function
5823 definitions (separate parameter list and declarations). */
5826 store_parm_decls_oldstyle (void)
5828 tree parm
, decl
, last
;
5829 tree fndecl
= current_function_decl
;
5831 /* This is the identifier list from the function declarator. */
5832 tree parmids
= current_function_parms
;
5834 /* We use DECL_WEAK as a flag to show which parameters have been
5835 seen already, since it is not used on PARM_DECL or CONST_DECL. */
5836 for (parm
= current_scope
->parms
; parm
; parm
= TREE_CHAIN (parm
))
5837 DECL_WEAK (parm
) = 0;
5839 /* Match each formal parameter name with its declaration. Save each
5840 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
5841 for (parm
= parmids
; parm
; parm
= TREE_CHAIN (parm
))
5843 if (TREE_VALUE (parm
) == 0)
5845 error ("%Hparameter name missing from parameter list",
5846 &DECL_SOURCE_LOCATION (fndecl
));
5847 TREE_PURPOSE (parm
) = 0;
5851 decl
= IDENTIFIER_SYMBOL_VALUE (TREE_VALUE (parm
));
5852 if (decl
&& DECL_CONTEXT (decl
) == fndecl
)
5854 const location_t
*locus
= &DECL_SOURCE_LOCATION (decl
);
5855 /* If we got something other than a PARM_DECL it is an error. */
5856 if (TREE_CODE (decl
) != PARM_DECL
)
5857 error ("%H\"%D\" declared as a non-parameter", locus
, decl
);
5858 /* If the declaration is already marked, we have a duplicate
5859 name. Complain and ignore the duplicate. */
5860 else if (DECL_WEAK (decl
))
5862 error ("%Hmultiple parameters named \"%D\"", locus
, decl
);
5863 TREE_PURPOSE (parm
) = 0;
5866 /* If the declaration says "void", complain and turn it into
5868 else if (VOID_TYPE_P (TREE_TYPE (decl
)))
5870 error ("%Hparameter \"%D\" declared void", locus
, decl
);
5871 TREE_TYPE (decl
) = integer_type_node
;
5872 DECL_ARG_TYPE (decl
) = integer_type_node
;
5873 layout_decl (decl
, 0);
5876 /* If no declaration found, default to int. */
5879 const location_t
*locus
= &DECL_SOURCE_LOCATION (fndecl
);
5880 decl
= build_decl (PARM_DECL
, TREE_VALUE (parm
), integer_type_node
);
5881 DECL_ARG_TYPE (decl
) = TREE_TYPE (decl
);
5882 DECL_SOURCE_LOCATION (decl
) = *locus
;
5886 pedwarn ("%Htype of \"%D\" defaults to \"int\"", locus
, decl
);
5887 else if (extra_warnings
)
5888 warning ("%Htype of \"%D\" defaults to \"int\"", locus
, decl
);
5891 TREE_PURPOSE (parm
) = decl
;
5892 DECL_WEAK (decl
) = 1;
5895 /* Now examine the parms chain for incomplete declarations
5896 and declarations with no corresponding names. */
5898 for (parm
= current_scope
->parms
; parm
; parm
= TREE_CHAIN (parm
))
5900 const location_t
*locus
= &DECL_SOURCE_LOCATION (parm
);
5902 if (!COMPLETE_TYPE_P (TREE_TYPE (parm
)))
5904 error ("%Hparameter \"%D\" has incomplete type", locus
, parm
);
5905 TREE_TYPE (parm
) = error_mark_node
;
5908 if (! DECL_WEAK (parm
))
5910 error ("%Hdeclaration for parameter \"%D\" but no such parameter",
5913 /* Pretend the parameter was not missing.
5914 This gets us to a standard state and minimizes
5915 further error messages. */
5916 parmids
= chainon (parmids
, tree_cons (parm
, 0, 0));
5920 /* Chain the declarations together in the order of the list of
5921 names. Store that chain in the function decl, replacing the
5922 list of names. Update the current scope to match. */
5923 DECL_ARGUMENTS (fndecl
) = 0;
5925 for (parm
= parmids
; parm
; parm
= TREE_CHAIN (parm
))
5926 if (TREE_PURPOSE (parm
))
5928 if (parm
&& TREE_PURPOSE (parm
))
5930 last
= TREE_PURPOSE (parm
);
5931 DECL_ARGUMENTS (fndecl
) = last
;
5932 current_scope
->parms
= last
;
5933 DECL_WEAK (last
) = 0;
5935 for (parm
= TREE_CHAIN (parm
); parm
; parm
= TREE_CHAIN (parm
))
5936 if (TREE_PURPOSE (parm
))
5938 TREE_CHAIN (last
) = TREE_PURPOSE (parm
);
5939 last
= TREE_PURPOSE (parm
);
5940 DECL_WEAK (last
) = 0;
5942 TREE_CHAIN (last
) = 0;
5945 /* If there was a previous prototype,
5946 set the DECL_ARG_TYPE of each argument according to
5947 the type previously specified, and report any mismatches. */
5949 if (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)))
5952 for (parm
= DECL_ARGUMENTS (fndecl
),
5953 type
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
5954 parm
|| (type
&& (TYPE_MAIN_VARIANT (TREE_VALUE (type
))
5955 != void_type_node
));
5956 parm
= TREE_CHAIN (parm
), type
= TREE_CHAIN (type
))
5958 if (parm
== 0 || type
== 0
5959 || TYPE_MAIN_VARIANT (TREE_VALUE (type
)) == void_type_node
)
5961 error ("number of arguments doesn't match prototype");
5962 error ("%Hprototype declaration",
5963 ¤t_function_prototype_locus
);
5966 /* Type for passing arg must be consistent with that
5967 declared for the arg. ISO C says we take the unqualified
5968 type for parameters declared with qualified type. */
5969 if (! comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm
)),
5970 TYPE_MAIN_VARIANT (TREE_VALUE (type
)),
5973 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm
))
5974 == TYPE_MAIN_VARIANT (TREE_VALUE (type
)))
5976 /* Adjust argument to match prototype. E.g. a previous
5977 `int foo(float);' prototype causes
5978 `int foo(x) float x; {...}' to be treated like
5979 `int foo(float x) {...}'. This is particularly
5980 useful for argument types like uid_t. */
5981 DECL_ARG_TYPE (parm
) = TREE_TYPE (parm
);
5983 if (PROMOTE_PROTOTYPES
5984 && INTEGRAL_TYPE_P (TREE_TYPE (parm
))
5985 && TYPE_PRECISION (TREE_TYPE (parm
))
5986 < TYPE_PRECISION (integer_type_node
))
5987 DECL_ARG_TYPE (parm
) = integer_type_node
;
5991 pedwarn ("promoted argument \"%D\" "
5992 "doesn't match prototype", parm
);
5993 pedwarn ("%Hprototype declaration",
5994 ¤t_function_prototype_locus
);
5999 error ("argument \"%D\" doesn't match prototype", parm
);
6000 error ("%Hprototype declaration",
6001 ¤t_function_prototype_locus
);
6005 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl
)) = 0;
6008 /* Otherwise, create a prototype that would match. */
6012 tree actual
= 0, last
= 0, type
;
6014 for (parm
= DECL_ARGUMENTS (fndecl
); parm
; parm
= TREE_CHAIN (parm
))
6016 type
= tree_cons (NULL_TREE
, DECL_ARG_TYPE (parm
), NULL_TREE
);
6018 TREE_CHAIN (last
) = type
;
6023 type
= tree_cons (NULL_TREE
, void_type_node
, NULL_TREE
);
6025 TREE_CHAIN (last
) = type
;
6029 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6030 of the type of this function, but we need to avoid having this
6031 affect the types of other similarly-typed functions, so we must
6032 first force the generation of an identical (but separate) type
6033 node for the relevant function type. The new node we create
6034 will be a variant of the main variant of the original function
6037 TREE_TYPE (fndecl
) = build_type_copy (TREE_TYPE (fndecl
));
6039 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl
)) = actual
;
6043 /* Store the parameter declarations into the current function declaration.
6044 This is called after parsing the parameter declarations, before
6045 digesting the body of the function.
6047 For an old-style definition, construct a prototype out of the old-style
6048 parameter declarations and inject it into the function's type. */
6051 store_parm_decls (void)
6053 tree fndecl
= current_function_decl
;
6055 /* The function containing FNDECL, if any. */
6056 tree context
= decl_function_context (fndecl
);
6058 /* True if this definition is written with a prototype. */
6059 bool prototype
= (current_function_parms
6060 && TREE_CODE (current_function_parms
) != TREE_LIST
);
6063 store_parm_decls_newstyle ();
6065 store_parm_decls_oldstyle ();
6067 /* The next call to pushlevel will be a function body. */
6069 next_is_function_body
= true;
6071 /* Write a record describing this function definition to the prototypes
6072 file (if requested). */
6074 gen_aux_info_record (fndecl
, 1, 0, prototype
);
6076 /* Initialize the RTL code for the function. */
6077 init_function_start (fndecl
);
6079 /* Begin the statement tree for this function. */
6080 begin_stmt_tree (&DECL_SAVED_TREE (fndecl
));
6082 /* If this is a nested function, save away the sizes of any
6083 variable-size types so that we can expand them when generating
6089 DECL_LANG_SPECIFIC (fndecl
)->pending_sizes
6090 = nreverse (get_pending_sizes ());
6091 for (t
= DECL_LANG_SPECIFIC (fndecl
)->pending_sizes
;
6094 SAVE_EXPR_CONTEXT (TREE_VALUE (t
)) = context
;
6097 /* This function is being processed in whole-function mode. */
6098 cfun
->x_whole_function_mode_p
= 1;
6100 /* Even though we're inside a function body, we still don't want to
6101 call expand_expr to calculate the size of a variable-sized array.
6102 We haven't necessarily assigned RTL to all variables yet, so it's
6103 not safe to try to expand expressions involving them. */
6104 immediate_size_expand
= 0;
6105 cfun
->x_dont_save_pending_sizes_p
= 1;
6108 /* Finish up a function declaration and compile that function
6109 all the way to assembler language output. The free the storage
6110 for the function definition.
6112 This is called after parsing the body of the function definition.
6114 NESTED is nonzero if the function being finished is nested in another.
6115 CAN_DEFER_P is nonzero if the function may be deferred. */
6118 finish_function (int nested
, int can_defer_p
)
6120 tree fndecl
= current_function_decl
;
6122 /* When a function declaration is totally empty, e.g.
6124 (the argument list is irrelevant) the compstmt rule will not
6125 bother calling pushlevel/poplevel, which means we get here with
6126 the scope stack out of sync. Detect this situation by noticing
6127 that current_scope is still as store_parm_decls left it, and do
6128 a dummy push/pop to get back to consistency.
6129 Note that the call to pushlevel does not actually push another
6130 scope - see there for details. */
6132 if (current_scope
->parm_flag
&& next_is_function_body
)
6138 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl
)) = fndecl
;
6140 /* Must mark the RESULT_DECL as being in this function. */
6142 DECL_CONTEXT (DECL_RESULT (fndecl
)) = fndecl
;
6144 if (MAIN_NAME_P (DECL_NAME (fndecl
)) && flag_hosted
)
6146 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl
)))
6147 != integer_type_node
)
6149 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6150 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6152 pedwarn ("%Hreturn type of '%D' is not `int'",
6153 &DECL_SOURCE_LOCATION (fndecl
), fndecl
);
6157 #ifdef DEFAULT_MAIN_RETURN
6158 /* Make it so that `main' always returns success by default. */
6159 DEFAULT_MAIN_RETURN
;
6162 c_expand_return (integer_zero_node
);
6167 finish_fname_decls ();
6169 /* Tie off the statement tree for this function. */
6170 finish_stmt_tree (&DECL_SAVED_TREE (fndecl
));
6172 /* Complain if there's just no return statement. */
6173 if (warn_return_type
6174 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl
))) != VOID_TYPE
6175 && !current_function_returns_value
&& !current_function_returns_null
6176 /* Don't complain if we abort. */
6177 && !current_function_returns_abnormally
6178 /* Don't warn for main(). */
6179 && !MAIN_NAME_P (DECL_NAME (fndecl
))
6180 /* Or if they didn't actually specify a return type. */
6181 && !C_FUNCTION_IMPLICIT_INT (fndecl
)
6182 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6183 inline function, as we might never be compiled separately. */
6184 && DECL_INLINE (fndecl
))
6185 warning ("no return statement in function returning non-void");
6187 /* Clear out memory we no longer need. */
6188 free_after_parsing (cfun
);
6189 /* Since we never call rest_of_compilation, we never clear
6190 CFUN. Do so explicitly. */
6191 free_after_compilation (cfun
);
6194 if (flag_unit_at_a_time
&& can_defer_p
)
6196 cgraph_finalize_function (fndecl
, DECL_SAVED_TREE (fndecl
));
6197 current_function_decl
= NULL
;
6203 /* Function is parsed.
6204 Generate RTL for the body of this function or defer
6205 it for later expansion. */
6206 int uninlinable
= 1;
6208 /* There's no reason to do any of the work here if we're only doing
6209 semantic analysis; this code just generates RTL. */
6210 if (flag_syntax_only
)
6212 current_function_decl
= NULL
;
6213 DECL_SAVED_TREE (fndecl
) = NULL_TREE
;
6217 if (flag_inline_trees
)
6219 /* First, cache whether the current function is inlinable. Some
6220 predicates depend on cfun and current_function_decl to
6221 function completely. */
6222 timevar_push (TV_INTEGRATION
);
6223 uninlinable
= ! tree_inlinable_function_p (fndecl
, 0);
6226 /* We defer functions marked inline *even if* the function
6227 itself is not inlinable. This is because we don't yet
6228 know if the function will actually be used; we may be
6229 able to avoid emitting it entirely. */
6230 && (! uninlinable
|| DECL_DECLARED_INLINE_P (fndecl
))
6231 /* Save function tree for inlining. Should return 0 if the
6232 language does not support function deferring or the
6233 function could not be deferred. */
6234 && defer_fn (fndecl
))
6236 /* Let the back-end know that this function exists. */
6237 (*debug_hooks
->deferred_inline_function
) (fndecl
);
6238 timevar_pop (TV_INTEGRATION
);
6239 current_function_decl
= NULL
;
6243 /* Then, inline any functions called in it. */
6244 optimize_inline_calls (fndecl
);
6245 timevar_pop (TV_INTEGRATION
);
6248 c_expand_body (fndecl
);
6250 /* Keep the function body if it's needed for inlining or dumping. */
6251 if (uninlinable
&& !dump_enabled_p (TDI_all
))
6253 /* Allow the body of the function to be garbage collected. */
6254 DECL_SAVED_TREE (fndecl
) = NULL_TREE
;
6257 /* Let the error reporting routines know that we're outside a
6258 function. For a nested function, this value is used in
6259 c_pop_function_context and then reset via pop_function_context. */
6260 current_function_decl
= NULL
;
6264 /* Generate the RTL for a deferred function FNDECL. */
6267 c_expand_deferred_function (tree fndecl
)
6269 /* DECL_INLINE or DECL_RESULT might got cleared after the inline
6270 function was deferred, e.g. in duplicate_decls. */
6271 if (DECL_INLINE (fndecl
) && DECL_RESULT (fndecl
))
6273 if (flag_inline_trees
)
6275 timevar_push (TV_INTEGRATION
);
6276 optimize_inline_calls (fndecl
);
6277 timevar_pop (TV_INTEGRATION
);
6279 c_expand_body (fndecl
);
6280 current_function_decl
= NULL
;
6284 /* Called to move the SAVE_EXPRs for parameter declarations in a
6285 nested function into the nested function. DATA is really the
6286 nested FUNCTION_DECL. */
6289 set_save_expr_context (tree
*tp
,
6293 if (TREE_CODE (*tp
) == SAVE_EXPR
&& !SAVE_EXPR_CONTEXT (*tp
))
6294 SAVE_EXPR_CONTEXT (*tp
) = (tree
) data
;
6295 /* Do not walk back into the SAVE_EXPR_CONTEXT; that will cause
6297 else if (DECL_P (*tp
))
6303 /* Generate the RTL for the body of FNDECL. If NESTED_P is nonzero,
6304 then we are already in the process of generating RTL for another
6305 function. If can_defer_p is zero, we won't attempt to defer the
6306 generation of RTL. */
6309 c_expand_body_1 (tree fndecl
, int nested_p
)
6311 timevar_push (TV_EXPAND
);
6315 /* Make sure that we will evaluate variable-sized types involved
6316 in our function's type. */
6317 expand_pending_sizes (DECL_LANG_SPECIFIC (fndecl
)->pending_sizes
);
6318 /* Squirrel away our current state. */
6319 push_function_context ();
6322 /* Initialize the RTL code for the function. */
6323 current_function_decl
= fndecl
;
6324 input_location
= DECL_SOURCE_LOCATION (fndecl
);
6325 init_function_start (fndecl
);
6327 /* This function is being processed in whole-function mode. */
6328 cfun
->x_whole_function_mode_p
= 1;
6330 /* Even though we're inside a function body, we still don't want to
6331 call expand_expr to calculate the size of a variable-sized array.
6332 We haven't necessarily assigned RTL to all variables yet, so it's
6333 not safe to try to expand expressions involving them. */
6334 immediate_size_expand
= 0;
6335 cfun
->x_dont_save_pending_sizes_p
= 1;
6337 /* Set up parameters and prepare for return, for the function. */
6338 expand_function_start (fndecl
, 0);
6340 /* If the function has a variably modified type, there may be
6341 SAVE_EXPRs in the parameter types. Their context must be set to
6342 refer to this function; they cannot be expanded in the containing
6344 if (decl_function_context (fndecl
)
6345 && variably_modified_type_p (TREE_TYPE (fndecl
)))
6346 walk_tree (&TREE_TYPE (fndecl
), set_save_expr_context
, fndecl
,
6349 /* If this function is `main', emit a call to `__main'
6350 to run global initializers, etc. */
6351 if (DECL_NAME (fndecl
)
6352 && MAIN_NAME_P (DECL_NAME (fndecl
))
6353 && C_DECL_FILE_SCOPE (fndecl
))
6354 expand_main_function ();
6356 /* Generate the RTL for this function. */
6357 expand_stmt (DECL_SAVED_TREE (fndecl
));
6359 /* We hard-wired immediate_size_expand to zero above.
6360 expand_function_end will decrement this variable. So, we set the
6361 variable to one here, so that after the decrement it will remain
6363 immediate_size_expand
= 1;
6365 /* Allow language dialects to perform special processing. */
6366 if (lang_expand_function_end
)
6367 (*lang_expand_function_end
) ();
6369 /* Generate rtl for function exit. */
6370 expand_function_end ();
6372 /* If this is a nested function, protect the local variables in the stack
6373 above us from being collected while we're compiling this function. */
6375 ggc_push_context ();
6377 /* Run the optimizers and output the assembler code for this function. */
6378 rest_of_compilation (fndecl
);
6380 /* Undo the GC context switch. */
6384 /* With just -Wextra, complain only if function returns both with
6385 and without a value. */
6387 && current_function_returns_value
6388 && current_function_returns_null
)
6389 warning ("this function may return with or without a value");
6391 /* If requested, warn about function definitions where the function will
6392 return a value (usually of some struct or union type) which itself will
6393 take up a lot of stack space. */
6395 if (warn_larger_than
&& !DECL_EXTERNAL (fndecl
) && TREE_TYPE (fndecl
))
6397 tree ret_type
= TREE_TYPE (TREE_TYPE (fndecl
));
6399 if (ret_type
&& TYPE_SIZE_UNIT (ret_type
)
6400 && TREE_CODE (TYPE_SIZE_UNIT (ret_type
)) == INTEGER_CST
6401 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type
),
6404 const location_t
*locus
= &DECL_SOURCE_LOCATION (fndecl
);
6405 unsigned int size_as_int
6406 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type
));
6408 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type
), size_as_int
) == 0)
6409 warning ("%Hsize of return value of '%D' is %u bytes",
6410 locus
, fndecl
, size_as_int
);
6412 warning ("%Hsize of return value of '%D' is larger than %wd bytes",
6413 locus
, fndecl
, larger_than_size
);
6417 if (DECL_SAVED_INSNS (fndecl
) == 0 && ! nested_p
6418 && ! flag_inline_trees
)
6420 /* Stop pointing to the local nodes about to be freed.
6421 But DECL_INITIAL must remain nonzero so we know this
6422 was an actual function definition.
6423 For a nested function, this is done in c_pop_function_context.
6424 If rest_of_compilation set this to 0, leave it 0. */
6425 if (DECL_INITIAL (fndecl
) != 0)
6426 DECL_INITIAL (fndecl
) = error_mark_node
;
6428 DECL_ARGUMENTS (fndecl
) = 0;
6431 if (DECL_STATIC_CONSTRUCTOR (fndecl
))
6433 if (targetm
.have_ctors_dtors
)
6434 (* targetm
.asm_out
.constructor
) (XEXP (DECL_RTL (fndecl
), 0),
6435 DEFAULT_INIT_PRIORITY
);
6437 static_ctors
= tree_cons (NULL_TREE
, fndecl
, static_ctors
);
6440 if (DECL_STATIC_DESTRUCTOR (fndecl
))
6442 if (targetm
.have_ctors_dtors
)
6443 (* targetm
.asm_out
.destructor
) (XEXP (DECL_RTL (fndecl
), 0),
6444 DEFAULT_INIT_PRIORITY
);
6446 static_dtors
= tree_cons (NULL_TREE
, fndecl
, static_dtors
);
6450 /* Return to the enclosing function. */
6451 pop_function_context ();
6452 timevar_pop (TV_EXPAND
);
6455 /* Like c_expand_body_1 but only for unnested functions. */
6458 c_expand_body (tree fndecl
)
6460 c_expand_body_1 (fndecl
, 0);
6463 /* Check the declarations given in a for-loop for satisfying the C99
6466 check_for_loop_decls (void)
6472 /* If we get here, declarations have been used in a for loop without
6473 the C99 for loop scope. This doesn't make much sense, so don't
6475 error ("`for' loop initial declaration used outside C99 mode");
6478 /* C99 subclause 6.8.5 paragraph 3:
6480 [#3] The declaration part of a for statement shall only
6481 declare identifiers for objects having storage class auto or
6484 It isn't clear whether, in this sentence, "identifiers" binds to
6485 "shall only declare" or to "objects" - that is, whether all identifiers
6486 declared must be identifiers for objects, or whether the restriction
6487 only applies to those that are. (A question on this in comp.std.c
6488 in November 2000 received no answer.) We implement the strictest
6489 interpretation, to avoid creating an extension which later causes
6492 for (t
= gettags (); t
; t
= TREE_CHAIN (t
))
6494 if (TREE_PURPOSE (t
) != 0)
6496 enum tree_code code
= TREE_CODE (TREE_VALUE (t
));
6498 if (code
== RECORD_TYPE
)
6499 error ("`struct %s' declared in `for' loop initial declaration",
6500 IDENTIFIER_POINTER (TREE_PURPOSE (t
)));
6501 else if (code
== UNION_TYPE
)
6502 error ("`union %s' declared in `for' loop initial declaration",
6503 IDENTIFIER_POINTER (TREE_PURPOSE (t
)));
6505 error ("`enum %s' declared in `for' loop initial declaration",
6506 IDENTIFIER_POINTER (TREE_PURPOSE (t
)));
6510 for (t
= getdecls (); t
; t
= TREE_CHAIN (t
))
6512 const location_t
*locus
= &DECL_SOURCE_LOCATION (t
);
6513 if (TREE_CODE (t
) != VAR_DECL
&& DECL_NAME (t
))
6514 error ("%Hdeclaration of non-variable '%D' in 'for' loop "
6515 "initial declaration", locus
, t
);
6516 else if (TREE_STATIC (t
))
6517 error ("%Hdeclaration of static variable '%D' in 'for' loop "
6518 "initial declaration", locus
, t
);
6519 else if (DECL_EXTERNAL (t
))
6520 error ("%Hdeclaration of 'extern' variable '%D' in 'for' loop "
6521 "initial declaration", locus
, t
);
6525 /* Save and restore the variables in this file and elsewhere
6526 that keep track of the progress of compilation of the current function.
6527 Used for nested functions. */
6529 struct language_function
GTY(())
6531 struct c_language_function base
;
6534 int returns_abnormally
;
6535 int warn_about_return_type
;
6539 /* Save and reinitialize the variables
6540 used during compilation of a C function. */
6543 c_push_function_context (struct function
*f
)
6545 struct language_function
*p
;
6546 p
= ggc_alloc (sizeof (struct language_function
));
6549 p
->base
.x_stmt_tree
= c_stmt_tree
;
6550 p
->base
.x_scope_stmt_stack
= c_scope_stmt_stack
;
6551 p
->returns_value
= current_function_returns_value
;
6552 p
->returns_null
= current_function_returns_null
;
6553 p
->returns_abnormally
= current_function_returns_abnormally
;
6554 p
->warn_about_return_type
= warn_about_return_type
;
6555 p
->extern_inline
= current_extern_inline
;
6558 /* Restore the variables used during compilation of a C function. */
6561 c_pop_function_context (struct function
*f
)
6563 struct language_function
*p
= f
->language
;
6565 if (DECL_SAVED_INSNS (current_function_decl
) == 0
6566 && DECL_SAVED_TREE (current_function_decl
) == NULL_TREE
)
6568 /* Stop pointing to the local nodes about to be freed. */
6569 /* But DECL_INITIAL must remain nonzero so we know this
6570 was an actual function definition. */
6571 DECL_INITIAL (current_function_decl
) = error_mark_node
;
6572 DECL_ARGUMENTS (current_function_decl
) = 0;
6575 c_stmt_tree
= p
->base
.x_stmt_tree
;
6576 c_scope_stmt_stack
= p
->base
.x_scope_stmt_stack
;
6577 current_function_returns_value
= p
->returns_value
;
6578 current_function_returns_null
= p
->returns_null
;
6579 current_function_returns_abnormally
= p
->returns_abnormally
;
6580 warn_about_return_type
= p
->warn_about_return_type
;
6581 current_extern_inline
= p
->extern_inline
;
6586 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6589 c_dup_lang_specific_decl (tree decl
)
6591 struct lang_decl
*ld
;
6593 if (!DECL_LANG_SPECIFIC (decl
))
6596 ld
= ggc_alloc (sizeof (struct lang_decl
));
6597 memcpy (ld
, DECL_LANG_SPECIFIC (decl
), sizeof (struct lang_decl
));
6598 DECL_LANG_SPECIFIC (decl
) = ld
;
6601 /* The functions below are required for functionality of doing
6602 function at once processing in the C front end. Currently these
6603 functions are not called from anywhere in the C front end, but as
6604 these changes continue, that will change. */
6606 /* Returns nonzero if the current statement is a full expression,
6607 i.e. temporaries created during that statement should be destroyed
6608 at the end of the statement. */
6611 stmts_are_full_exprs_p (void)
6616 /* Returns the stmt_tree (if any) to which statements are currently
6617 being added. If there is no active statement-tree, NULL is
6621 current_stmt_tree (void)
6623 return &c_stmt_tree
;
6626 /* Returns the stack of SCOPE_STMTs for the current function. */
6629 current_scope_stmt_stack (void)
6631 return &c_scope_stmt_stack
;
6634 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6638 anon_aggr_type_p (tree node ATTRIBUTE_UNUSED
)
6643 /* Dummy function in place of callback used by C++. */
6646 extract_interface_info (void)
6650 /* Return a new COMPOUND_STMT, after adding it to the current
6654 c_begin_compound_stmt (void)
6658 /* Create the COMPOUND_STMT. */
6659 stmt
= add_stmt (build_stmt (COMPOUND_STMT
, NULL_TREE
));
6664 /* Expand T (a DECL_STMT) if it declares an entity not handled by the
6668 c_expand_decl_stmt (tree t
)
6670 tree decl
= DECL_STMT_DECL (t
);
6672 /* Expand nested functions. */
6673 if (TREE_CODE (decl
) == FUNCTION_DECL
6674 && DECL_CONTEXT (decl
) == current_function_decl
6675 && DECL_SAVED_TREE (decl
))
6676 c_expand_body_1 (decl
, 1);
6679 /* Return the global value of T as a symbol. */
6682 identifier_global_value (tree t
)
6684 tree decl
= IDENTIFIER_SYMBOL_VALUE (t
);
6685 if (decl
== 0 || C_DECL_FILE_SCOPE (decl
))
6688 /* Shadowed by something else; find the true global value. */
6689 for (decl
= global_scope
->names
; decl
; decl
= TREE_CHAIN (decl
))
6690 if (DECL_NAME (decl
) == t
)
6693 /* Only local values for this decl. */
6697 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6698 otherwise the name is found in ridpointers from RID_INDEX. */
6701 record_builtin_type (enum rid rid_index
, const char *name
, tree type
)
6705 id
= ridpointers
[(int) rid_index
];
6707 id
= get_identifier (name
);
6708 pushdecl (build_decl (TYPE_DECL
, id
, type
));
6711 /* Build the void_list_node (void_type_node having been created). */
6713 build_void_list_node (void)
6715 tree t
= build_tree_list (NULL_TREE
, void_type_node
);
6719 /* Return something to represent absolute declarators containing a *.
6720 TARGET is the absolute declarator that the * contains.
6721 TYPE_QUALS_ATTRS is a list of modifiers such as const or volatile
6722 to apply to the pointer type, represented as identifiers, possible mixed
6725 We return an INDIRECT_REF whose "contents" are TARGET (inside a TREE_LIST,
6726 if attributes are present) and whose type is the modifier list. */
6729 make_pointer_declarator (tree type_quals_attrs
, tree target
)
6732 tree itarget
= target
;
6733 split_specs_attrs (type_quals_attrs
, &quals
, &attrs
);
6734 if (attrs
!= NULL_TREE
)
6735 itarget
= tree_cons (attrs
, target
, NULL_TREE
);
6736 return build1 (INDIRECT_REF
, quals
, itarget
);
6739 /* A wrapper around lhd_set_decl_assembler_name that gives static
6740 variables their C names if they are at file scope and only one
6741 translation unit is being compiled, for backwards compatibility
6742 with certain bizzare assembler hacks (like crtstuff.c). */
6745 c_static_assembler_name (tree decl
)
6747 if (num_in_fnames
== 1
6748 && !TREE_PUBLIC (decl
) && DECL_CONTEXT (decl
)
6749 && TREE_CODE (DECL_CONTEXT (decl
)) == TRANSLATION_UNIT_DECL
)
6750 SET_DECL_ASSEMBLER_NAME (decl
, DECL_NAME (decl
));
6752 lhd_set_decl_assembler_name (decl
);
6755 /* Hash and equality functions for link_hash_table: key off
6756 DECL_ASSEMBLER_NAME. */
6759 link_hash_hash (const void *x_p
)
6762 return (hashval_t
) (long)DECL_ASSEMBLER_NAME (x
);
6766 link_hash_eq (const void *x1_p
, const void *x2_p
)
6768 tree x1
= (tree
)x1_p
;
6769 tree x2
= (tree
)x2_p
;
6770 return DECL_ASSEMBLER_NAME (x1
) == DECL_ASSEMBLER_NAME (x2
);
6773 /* Propagate information between definitions and uses between multiple
6774 translation units in TU_LIST based on linkage rules. */
6777 merge_translation_unit_decls (void)
6779 const tree tu_list
= current_file_decl
;
6782 htab_t link_hash_table
;
6785 /* Create the BLOCK that poplevel would have created, but don't
6786 actually call poplevel since that's expensive. */
6787 block
= make_node (BLOCK
);
6788 BLOCK_VARS (block
) = current_scope
->names
;
6789 TREE_USED (block
) = 1;
6790 DECL_INITIAL (current_file_decl
) = block
;
6792 /* If only one translation unit seen, no copying necessary. */
6793 if (TREE_CHAIN (tu_list
) == NULL_TREE
)
6796 link_hash_table
= htab_create (1021, link_hash_hash
, link_hash_eq
, NULL
);
6798 /* Enter any actual definitions into the hash table. */
6799 for (tu
= tu_list
; tu
; tu
= TREE_CHAIN (tu
))
6800 for (decl
= BLOCK_VARS (DECL_INITIAL (tu
)); decl
; decl
= TREE_CHAIN (decl
))
6801 if (TREE_PUBLIC (decl
) && ! DECL_EXTERNAL (decl
))
6804 slot
= htab_find_slot (link_hash_table
, decl
, INSERT
);
6806 /* If we've already got a definition, work out which one is
6807 the real one, put it into the hash table, and make the
6808 other one DECL_EXTERNAL. This is important to avoid
6809 putting out two definitions of the same symbol in the
6813 tree old_decl
= (tree
) *slot
;
6815 /* If this is weak or common or whatever, suppress it
6816 in favour of the other definition. */
6817 if (DECL_WEAK (decl
))
6818 DECL_EXTERNAL (decl
) = 1;
6819 else if (DECL_WEAK (old_decl
) && ! DECL_WEAK (decl
))
6820 DECL_EXTERNAL (old_decl
) = 1;
6821 else if (DECL_COMMON (decl
) || DECL_ONE_ONLY (decl
))
6822 DECL_EXTERNAL (decl
) = 1;
6823 else if (DECL_COMMON (old_decl
) || DECL_ONE_ONLY (old_decl
))
6824 DECL_EXTERNAL (old_decl
) = 1;
6826 if (DECL_EXTERNAL (decl
))
6828 DECL_INITIAL (decl
) = NULL_TREE
;
6829 DECL_COMMON (decl
) = 0;
6830 DECL_ONE_ONLY (decl
) = 0;
6831 DECL_WEAK (decl
) = 0;
6833 else if (DECL_EXTERNAL (old_decl
))
6835 DECL_INITIAL (old_decl
) = NULL_TREE
;
6836 DECL_COMMON (old_decl
) = 0;
6837 DECL_ONE_ONLY (old_decl
) = 0;
6838 DECL_WEAK (old_decl
) = 0;
6843 error ("%Hredefinition of global '%D'",
6844 &DECL_SOURCE_LOCATION (decl
), decl
);
6845 error ("%H'%D' previously defined here",
6846 &DECL_SOURCE_LOCATION (old_decl
), old_decl
);
6853 /* Now insert the desired information from all the definitions
6854 into any plain declarations. */
6855 for (tu
= tu_list
; tu
; tu
= TREE_CHAIN (tu
))
6856 for (decl
= BLOCK_VARS (DECL_INITIAL (tu
)); decl
; decl
= TREE_CHAIN (decl
))
6857 if (TREE_PUBLIC (decl
) && DECL_EXTERNAL (decl
))
6860 global_decl
= htab_find (link_hash_table
, decl
);
6865 /* Print any appropriate error messages, and partially merge
6867 (void) duplicate_decls (decl
, global_decl
, true, true);
6870 htab_delete (link_hash_table
);
6873 /* Perform final processing on file-scope data. */
6876 c_write_global_declarations(void)
6880 for (link
= current_file_decl
; link
; link
= TREE_CHAIN (link
))
6882 tree globals
= BLOCK_VARS (DECL_INITIAL (link
));
6883 int len
= list_length (globals
);
6884 tree
*vec
= xmalloc (sizeof (tree
) * len
);
6888 /* Process the decls in reverse order--earliest first.
6889 Put them into VEC from back to front, then take out from front. */
6891 for (i
= 0, decl
= globals
; i
< len
; i
++, decl
= TREE_CHAIN (decl
))
6892 vec
[len
- i
- 1] = decl
;
6894 wrapup_global_declarations (vec
, len
);
6896 check_global_declarations (vec
, len
);
6903 /* Reset the parser's state in preparation for a new file. */
6906 c_reset_state (void)
6909 tree file_scope_decl
;
6911 /* Pop the global scope. */
6912 if (current_scope
!= global_scope
)
6913 current_scope
= global_scope
;
6914 file_scope_decl
= current_file_decl
;
6915 DECL_INITIAL (file_scope_decl
) = poplevel (1, 0, 0);
6916 truly_local_externals
= NULL_TREE
;
6918 /* Start a new global binding level. */
6920 global_scope
= current_scope
;
6921 current_file_decl
= build_decl (TRANSLATION_UNIT_DECL
, NULL
, NULL
);
6922 TREE_CHAIN (current_file_decl
) = file_scope_decl
;
6924 /* Reintroduce the global declarations. */
6925 for (link
= builtin_decls
; link
; link
= TREE_CHAIN (link
))
6926 pushdecl (copy_node (link
));
6929 #include "gt-c-decl.h"