1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992, 93, 94, 95, 96, 97, 1998 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
30 #ifndef WCHAR_TYPE_SIZE
32 #define WCHAR_TYPE_SIZE INT_TYPE_SIZE
34 #define WCHAR_TYPE_SIZE BITS_PER_WORD
38 extern struct obstack permanent_obstack
;
40 /* Nonzero means the expression being parsed will never be evaluated.
41 This is a count, since unevaluated expressions can nest. */
44 enum attrs
{A_PACKED
, A_NOCOMMON
, A_COMMON
, A_NORETURN
, A_CONST
, A_T_UNION
,
45 A_CONSTRUCTOR
, A_DESTRUCTOR
, A_MODE
, A_SECTION
, A_ALIGNED
,
46 A_UNUSED
, A_FORMAT
, A_FORMAT_ARG
, A_WEAK
, A_ALIAS
};
48 enum format_type
{ printf_format_type
, scanf_format_type
,
49 strftime_format_type
};
51 static void declare_hidden_char_array
PROTO((char *, char *));
52 static void add_attribute
PROTO((enum attrs
, char *,
54 static void init_attributes
PROTO((void));
55 static void record_function_format
PROTO((tree
, tree
, enum format_type
,
57 static void record_international_format
PROTO((tree
, tree
, int));
59 /* Keep a stack of if statements. We record the number of compound
60 statements seen up to the if keyword, as well as the line number
61 and file of the if. If a potentially ambiguous else is seen, that
62 fact is recorded; the warning is issued when we can be sure that
63 the enclosing if statement does not have an else branch. */
72 static if_elt
*if_stack
;
74 /* Amount of space in the if statement stack. */
75 static int if_stack_space
= 0;
78 static int if_stack_pointer
= 0;
80 /* Generate RTL for the start of an if-then, and record the start of it
81 for ambiguous else detection. */
84 c_expand_start_cond (cond
, exitflag
, compstmt_count
)
89 /* Make sure there is enough space on the stack. */
90 if (if_stack_space
== 0)
93 if_stack
= (if_elt
*)xmalloc (10 * sizeof (if_elt
));
95 else if (if_stack_space
== if_stack_pointer
)
98 if_stack
= (if_elt
*)xrealloc (if_stack
, if_stack_space
* sizeof (if_elt
));
101 /* Record this if statement. */
102 if_stack
[if_stack_pointer
].compstmt_count
= compstmt_count
;
103 if_stack
[if_stack_pointer
].file
= input_filename
;
104 if_stack
[if_stack_pointer
].line
= lineno
;
105 if_stack
[if_stack_pointer
].needs_warning
= 0;
108 expand_start_cond (cond
, exitflag
);
111 /* Generate RTL for the end of an if-then. Optionally warn if a nested
112 if statement had an ambiguous else clause. */
118 if (if_stack
[if_stack_pointer
].needs_warning
)
119 warning_with_file_and_line (if_stack
[if_stack_pointer
].file
,
120 if_stack
[if_stack_pointer
].line
,
121 "suggest explicit braces to avoid ambiguous `else'");
125 /* Generate RTL between the then-clause and the else-clause
126 of an if-then-else. */
129 c_expand_start_else ()
131 /* An ambiguous else warning must be generated for the enclosing if
132 statement, unless we see an else branch for that one, too. */
134 && if_stack_pointer
> 1
135 && (if_stack
[if_stack_pointer
- 1].compstmt_count
136 == if_stack
[if_stack_pointer
- 2].compstmt_count
))
137 if_stack
[if_stack_pointer
- 2].needs_warning
= 1;
139 /* Even if a nested if statement had an else branch, it can't be
140 ambiguous if this one also has an else. So don't warn in that
141 case. Also don't warn for any if statements nested in this else. */
142 if_stack
[if_stack_pointer
- 1].needs_warning
= 0;
143 if_stack
[if_stack_pointer
- 1].compstmt_count
--;
145 expand_start_else ();
148 /* Make bindings for __FUNCTION__ and __PRETTY_FUNCTION__. */
151 declare_function_name ()
153 char *name
, *printable_name
;
155 if (current_function_decl
== NULL
)
158 printable_name
= "top level";
162 /* Allow functions to be nameless (such as artificial ones). */
163 if (DECL_NAME (current_function_decl
))
164 name
= IDENTIFIER_POINTER (DECL_NAME (current_function_decl
));
167 printable_name
= (*decl_printable_name
) (current_function_decl
, 2);
170 declare_hidden_char_array ("__FUNCTION__", name
);
171 declare_hidden_char_array ("__PRETTY_FUNCTION__", printable_name
);
175 declare_hidden_char_array (name
, value
)
178 tree decl
, type
, init
;
181 /* If the default size of char arrays isn't big enough for the name,
182 or if we want to give warnings for large objects, make a bigger one. */
183 vlen
= strlen (value
) + 1;
184 type
= char_array_type_node
;
185 if (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TYPE_DOMAIN (type
))) < vlen
187 type
= build_array_type (char_type_node
,
188 build_index_type (build_int_2 (vlen
, 0)));
189 push_obstacks_nochange ();
190 decl
= build_decl (VAR_DECL
, get_identifier (name
), type
);
191 TREE_STATIC (decl
) = 1;
192 TREE_READONLY (decl
) = 1;
193 TREE_ASM_WRITTEN (decl
) = 1;
194 DECL_SOURCE_LINE (decl
) = 0;
195 DECL_ARTIFICIAL (decl
) = 1;
196 DECL_IN_SYSTEM_HEADER (decl
) = 1;
197 DECL_IGNORED_P (decl
) = 1;
198 init
= build_string (vlen
, value
);
199 TREE_TYPE (init
) = type
;
200 DECL_INITIAL (decl
) = init
;
201 finish_decl (pushdecl (decl
), init
, NULL_TREE
);
204 /* Given a chain of STRING_CST nodes,
205 concatenate them into one STRING_CST
206 and give it a suitable array-of-chars data type. */
209 combine_strings (strings
)
212 register tree value
, t
;
213 register int length
= 1;
216 int wchar_bytes
= TYPE_PRECISION (wchar_type_node
) / BITS_PER_UNIT
;
219 if (TREE_CHAIN (strings
))
221 /* More than one in the chain, so concatenate. */
222 register char *p
, *q
;
224 /* Don't include the \0 at the end of each substring,
225 except for the last one.
226 Count wide strings and ordinary strings separately. */
227 for (t
= strings
; t
; t
= TREE_CHAIN (t
))
229 if (TREE_TYPE (t
) == wchar_array_type_node
)
231 wide_length
+= (TREE_STRING_LENGTH (t
) - wchar_bytes
);
235 length
+= (TREE_STRING_LENGTH (t
) - 1);
238 /* If anything is wide, the non-wides will be converted,
239 which makes them take more space. */
241 length
= length
* wchar_bytes
+ wide_length
;
243 p
= savealloc (length
);
245 /* Copy the individual strings into the new combined string.
246 If the combined string is wide, convert the chars to ints
247 for any individual strings that are not wide. */
250 for (t
= strings
; t
; t
= TREE_CHAIN (t
))
252 int len
= (TREE_STRING_LENGTH (t
)
253 - ((TREE_TYPE (t
) == wchar_array_type_node
)
255 if ((TREE_TYPE (t
) == wchar_array_type_node
) == wide_flag
)
257 bcopy (TREE_STRING_POINTER (t
), q
, len
);
263 for (i
= 0; i
< len
; i
++)
265 if (WCHAR_TYPE_SIZE
== HOST_BITS_PER_SHORT
)
266 ((short *) q
)[i
] = TREE_STRING_POINTER (t
)[i
];
268 ((int *) q
)[i
] = TREE_STRING_POINTER (t
)[i
];
270 q
+= len
* wchar_bytes
;
276 for (i
= 0; i
< wchar_bytes
; i
++)
282 value
= make_node (STRING_CST
);
283 TREE_STRING_POINTER (value
) = p
;
284 TREE_STRING_LENGTH (value
) = length
;
285 TREE_CONSTANT (value
) = 1;
290 length
= TREE_STRING_LENGTH (value
);
291 if (TREE_TYPE (value
) == wchar_array_type_node
)
295 /* Compute the number of elements, for the array type. */
296 nchars
= wide_flag
? length
/ wchar_bytes
: length
;
298 /* Create the array type for the string constant.
299 -Wwrite-strings says make the string constant an array of const char
300 so that copying it to a non-const pointer will get a warning. */
301 if (warn_write_strings
302 && (! flag_traditional
&& ! flag_writable_strings
))
305 = build_type_variant (wide_flag
? wchar_type_node
: char_type_node
,
308 = build_array_type (elements
,
309 build_index_type (build_int_2 (nchars
- 1, 0)));
313 = build_array_type (wide_flag
? wchar_type_node
: char_type_node
,
314 build_index_type (build_int_2 (nchars
- 1, 0)));
315 TREE_CONSTANT (value
) = 1;
316 TREE_STATIC (value
) = 1;
320 /* To speed up processing of attributes, we maintain an array of
321 IDENTIFIER_NODES and the corresponding attribute types. */
323 /* Array to hold attribute information. */
325 static struct {enum attrs id
; tree name
; int min
, max
, decl_req
;} attrtab
[50];
327 static int attrtab_idx
= 0;
329 /* Add an entry to the attribute table above. */
332 add_attribute (id
, string
, min_len
, max_len
, decl_req
)
335 int min_len
, max_len
;
340 attrtab
[attrtab_idx
].id
= id
;
341 attrtab
[attrtab_idx
].name
= get_identifier (string
);
342 attrtab
[attrtab_idx
].min
= min_len
;
343 attrtab
[attrtab_idx
].max
= max_len
;
344 attrtab
[attrtab_idx
++].decl_req
= decl_req
;
346 sprintf (buf
, "__%s__", string
);
348 attrtab
[attrtab_idx
].id
= id
;
349 attrtab
[attrtab_idx
].name
= get_identifier (buf
);
350 attrtab
[attrtab_idx
].min
= min_len
;
351 attrtab
[attrtab_idx
].max
= max_len
;
352 attrtab
[attrtab_idx
++].decl_req
= decl_req
;
355 /* Initialize attribute table. */
360 add_attribute (A_PACKED
, "packed", 0, 0, 0);
361 add_attribute (A_NOCOMMON
, "nocommon", 0, 0, 1);
362 add_attribute (A_COMMON
, "common", 0, 0, 1);
363 add_attribute (A_NORETURN
, "noreturn", 0, 0, 1);
364 add_attribute (A_NORETURN
, "volatile", 0, 0, 1);
365 add_attribute (A_UNUSED
, "unused", 0, 0, 0);
366 add_attribute (A_CONST
, "const", 0, 0, 1);
367 add_attribute (A_T_UNION
, "transparent_union", 0, 0, 0);
368 add_attribute (A_CONSTRUCTOR
, "constructor", 0, 0, 1);
369 add_attribute (A_DESTRUCTOR
, "destructor", 0, 0, 1);
370 add_attribute (A_MODE
, "mode", 1, 1, 1);
371 add_attribute (A_SECTION
, "section", 1, 1, 1);
372 add_attribute (A_ALIGNED
, "aligned", 0, 1, 0);
373 add_attribute (A_FORMAT
, "format", 3, 3, 1);
374 add_attribute (A_FORMAT_ARG
, "format_arg", 1, 1, 1);
375 add_attribute (A_WEAK
, "weak", 0, 0, 1);
376 add_attribute (A_ALIAS
, "alias", 1, 1, 1);
379 /* Process the attributes listed in ATTRIBUTES and PREFIX_ATTRIBUTES
380 and install them in NODE, which is either a DECL (including a TYPE_DECL)
381 or a TYPE. PREFIX_ATTRIBUTES can appear after the declaration specifiers
382 and declaration modifiers but before the declaration proper. */
385 decl_attributes (node
, attributes
, prefix_attributes
)
386 tree node
, attributes
, prefix_attributes
;
388 tree decl
= 0, type
= 0;
392 if (attrtab_idx
== 0)
395 if (TREE_CODE_CLASS (TREE_CODE (node
)) == 'd')
398 type
= TREE_TYPE (decl
);
399 is_type
= TREE_CODE (node
) == TYPE_DECL
;
401 else if (TREE_CODE_CLASS (TREE_CODE (node
)) == 't')
402 type
= node
, is_type
= 1;
404 attributes
= chainon (prefix_attributes
, attributes
);
406 for (a
= attributes
; a
; a
= TREE_CHAIN (a
))
408 tree name
= TREE_PURPOSE (a
);
409 tree args
= TREE_VALUE (a
);
413 for (i
= 0; i
< attrtab_idx
; i
++)
414 if (attrtab
[i
].name
== name
)
417 if (i
== attrtab_idx
)
419 if (! valid_machine_attribute (name
, args
, decl
, type
))
420 warning ("`%s' attribute directive ignored",
421 IDENTIFIER_POINTER (name
));
423 type
= TREE_TYPE (decl
);
426 else if (attrtab
[i
].decl_req
&& decl
== 0)
428 warning ("`%s' attribute does not apply to types",
429 IDENTIFIER_POINTER (name
));
432 else if (list_length (args
) < attrtab
[i
].min
433 || list_length (args
) > attrtab
[i
].max
)
435 error ("wrong number of arguments specified for `%s' attribute",
436 IDENTIFIER_POINTER (name
));
445 TYPE_PACKED (type
) = 1;
446 else if (TREE_CODE (decl
) == FIELD_DECL
)
447 DECL_PACKED (decl
) = 1;
448 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
449 used for DECL_REGISTER. It wouldn't mean anything anyway. */
451 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
455 if (TREE_CODE (decl
) == VAR_DECL
)
456 DECL_COMMON (decl
) = 0;
458 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
462 if (TREE_CODE (decl
) == VAR_DECL
)
463 DECL_COMMON (decl
) = 1;
465 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
469 if (TREE_CODE (decl
) == FUNCTION_DECL
)
470 TREE_THIS_VOLATILE (decl
) = 1;
471 else if (TREE_CODE (type
) == POINTER_TYPE
472 && TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
)
473 TREE_TYPE (decl
) = type
475 (build_type_variant (TREE_TYPE (type
),
476 TREE_READONLY (TREE_TYPE (type
)), 1));
478 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
483 TREE_USED (type
) = 1;
484 else if (TREE_CODE (decl
) == PARM_DECL
485 || TREE_CODE (decl
) == VAR_DECL
486 || TREE_CODE (decl
) == FUNCTION_DECL
)
487 TREE_USED (decl
) = 1;
489 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
493 if (TREE_CODE (decl
) == FUNCTION_DECL
)
494 TREE_READONLY (decl
) = 1;
495 else if (TREE_CODE (type
) == POINTER_TYPE
496 && TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
)
497 TREE_TYPE (decl
) = type
499 (build_type_variant (TREE_TYPE (type
), 1,
500 TREE_THIS_VOLATILE (TREE_TYPE (type
))));
502 warning ( "`%s' attribute ignored", IDENTIFIER_POINTER (name
));
507 && TREE_CODE (type
) == UNION_TYPE
509 || (TYPE_FIELDS (type
) != 0
510 && TYPE_MODE (type
) == DECL_MODE (TYPE_FIELDS (type
)))))
511 TYPE_TRANSPARENT_UNION (type
) = 1;
512 else if (decl
!= 0 && TREE_CODE (decl
) == PARM_DECL
513 && TREE_CODE (type
) == UNION_TYPE
514 && TYPE_MODE (type
) == DECL_MODE (TYPE_FIELDS (type
)))
515 DECL_TRANSPARENT_UNION (decl
) = 1;
517 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
521 if (TREE_CODE (decl
) == FUNCTION_DECL
522 && TREE_CODE (type
) == FUNCTION_TYPE
523 && decl_function_context (decl
) == 0)
525 DECL_STATIC_CONSTRUCTOR (decl
) = 1;
526 TREE_USED (decl
) = 1;
529 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
533 if (TREE_CODE (decl
) == FUNCTION_DECL
534 && TREE_CODE (type
) == FUNCTION_TYPE
535 && decl_function_context (decl
) == 0)
537 DECL_STATIC_DESTRUCTOR (decl
) = 1;
538 TREE_USED (decl
) = 1;
541 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
545 if (TREE_CODE (TREE_VALUE (args
)) != IDENTIFIER_NODE
)
546 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
550 char *p
= IDENTIFIER_POINTER (TREE_VALUE (args
));
551 int len
= strlen (p
);
552 enum machine_mode mode
= VOIDmode
;
555 if (len
> 4 && p
[0] == '_' && p
[1] == '_'
556 && p
[len
- 1] == '_' && p
[len
- 2] == '_')
558 char *newp
= (char *) alloca (len
- 1);
560 strcpy (newp
, &p
[2]);
561 newp
[len
- 4] = '\0';
565 /* Give this decl a type with the specified mode.
566 First check for the special modes. */
567 if (! strcmp (p
, "byte"))
569 else if (!strcmp (p
, "word"))
571 else if (! strcmp (p
, "pointer"))
574 for (j
= 0; j
< NUM_MACHINE_MODES
; j
++)
575 if (!strcmp (p
, GET_MODE_NAME (j
)))
576 mode
= (enum machine_mode
) j
;
578 if (mode
== VOIDmode
)
579 error ("unknown machine mode `%s'", p
);
580 else if (0 == (typefm
= type_for_mode (mode
,
581 TREE_UNSIGNED (type
))))
582 error ("no data type for mode `%s'", p
);
585 TREE_TYPE (decl
) = type
= typefm
;
586 DECL_SIZE (decl
) = 0;
587 layout_decl (decl
, 0);
593 #ifdef ASM_OUTPUT_SECTION_NAME
594 if ((TREE_CODE (decl
) == FUNCTION_DECL
595 || TREE_CODE (decl
) == VAR_DECL
)
596 && TREE_CODE (TREE_VALUE (args
)) == STRING_CST
)
598 if (TREE_CODE (decl
) == VAR_DECL
599 && current_function_decl
!= NULL_TREE
600 && ! TREE_STATIC (decl
))
601 error_with_decl (decl
,
602 "section attribute cannot be specified for local variables");
603 /* The decl may have already been given a section attribute from
604 a previous declaration. Ensure they match. */
605 else if (DECL_SECTION_NAME (decl
) != NULL_TREE
606 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl
)),
607 TREE_STRING_POINTER (TREE_VALUE (args
))) != 0)
608 error_with_decl (node
,
609 "section of `%s' conflicts with previous declaration");
611 DECL_SECTION_NAME (decl
) = TREE_VALUE (args
);
614 error_with_decl (node
,
615 "section attribute not allowed for `%s'");
617 error_with_decl (node
,
618 "section attributes are not supported for this target");
625 = (args
? TREE_VALUE (args
)
626 : size_int (BIGGEST_ALIGNMENT
/ BITS_PER_UNIT
));
629 /* Strip any NOPs of any kind. */
630 while (TREE_CODE (align_expr
) == NOP_EXPR
631 || TREE_CODE (align_expr
) == CONVERT_EXPR
632 || TREE_CODE (align_expr
) == NON_LVALUE_EXPR
)
633 align_expr
= TREE_OPERAND (align_expr
, 0);
635 if (TREE_CODE (align_expr
) != INTEGER_CST
)
637 error ("requested alignment is not a constant");
641 align
= TREE_INT_CST_LOW (align_expr
) * BITS_PER_UNIT
;
643 if (exact_log2 (align
) == -1)
644 error ("requested alignment is not a power of 2");
646 TYPE_ALIGN (type
) = align
;
647 else if (TREE_CODE (decl
) != VAR_DECL
648 && TREE_CODE (decl
) != FIELD_DECL
)
649 error_with_decl (decl
,
650 "alignment may not be specified for `%s'");
652 DECL_ALIGN (decl
) = align
;
658 tree format_type_id
= TREE_VALUE (args
);
659 tree format_num_expr
= TREE_VALUE (TREE_CHAIN (args
));
660 tree first_arg_num_expr
661 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args
)));
664 enum format_type format_type
;
668 if (TREE_CODE (decl
) != FUNCTION_DECL
)
670 error_with_decl (decl
,
671 "argument format specified for non-function `%s'");
675 if (TREE_CODE (format_type_id
) != IDENTIFIER_NODE
)
677 error ("unrecognized format specifier");
682 char *p
= IDENTIFIER_POINTER (format_type_id
);
684 if (!strcmp (p
, "printf") || !strcmp (p
, "__printf__"))
685 format_type
= printf_format_type
;
686 else if (!strcmp (p
, "scanf") || !strcmp (p
, "__scanf__"))
687 format_type
= scanf_format_type
;
688 else if (!strcmp (p
, "strftime")
689 || !strcmp (p
, "__strftime__"))
690 format_type
= strftime_format_type
;
693 error ("`%s' is an unrecognized format function type", p
);
698 /* Strip any conversions from the string index and first arg number
699 and verify they are constants. */
700 while (TREE_CODE (format_num_expr
) == NOP_EXPR
701 || TREE_CODE (format_num_expr
) == CONVERT_EXPR
702 || TREE_CODE (format_num_expr
) == NON_LVALUE_EXPR
)
703 format_num_expr
= TREE_OPERAND (format_num_expr
, 0);
705 while (TREE_CODE (first_arg_num_expr
) == NOP_EXPR
706 || TREE_CODE (first_arg_num_expr
) == CONVERT_EXPR
707 || TREE_CODE (first_arg_num_expr
) == NON_LVALUE_EXPR
)
708 first_arg_num_expr
= TREE_OPERAND (first_arg_num_expr
, 0);
710 if (TREE_CODE (format_num_expr
) != INTEGER_CST
711 || TREE_CODE (first_arg_num_expr
) != INTEGER_CST
)
713 error ("format string has non-constant operand number");
717 format_num
= TREE_INT_CST_LOW (format_num_expr
);
718 first_arg_num
= TREE_INT_CST_LOW (first_arg_num_expr
);
719 if (first_arg_num
!= 0 && first_arg_num
<= format_num
)
721 error ("format string arg follows the args to be formatted");
725 /* If a parameter list is specified, verify that the format_num
726 argument is actually a string, in case the format attribute
728 argument
= TYPE_ARG_TYPES (type
);
731 for (arg_num
= 1; ; ++arg_num
)
733 if (argument
== 0 || arg_num
== format_num
)
735 argument
= TREE_CHAIN (argument
);
738 || TREE_CODE (TREE_VALUE (argument
)) != POINTER_TYPE
739 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument
)))
742 error ("format string arg not a string type");
745 if (first_arg_num
!= 0)
747 /* Verify that first_arg_num points to the last arg,
750 arg_num
++, argument
= TREE_CHAIN (argument
);
751 if (arg_num
!= first_arg_num
)
753 error ("args to be formatted is not ...");
759 record_function_format (DECL_NAME (decl
),
760 DECL_ASSEMBLER_NAME (decl
),
761 format_type
, format_num
, first_arg_num
);
767 tree format_num_expr
= TREE_VALUE (args
);
768 int format_num
, arg_num
;
771 if (TREE_CODE (decl
) != FUNCTION_DECL
)
773 error_with_decl (decl
,
774 "argument format specified for non-function `%s'");
778 /* Strip any conversions from the first arg number and verify it
780 while (TREE_CODE (format_num_expr
) == NOP_EXPR
781 || TREE_CODE (format_num_expr
) == CONVERT_EXPR
782 || TREE_CODE (format_num_expr
) == NON_LVALUE_EXPR
)
783 format_num_expr
= TREE_OPERAND (format_num_expr
, 0);
785 if (TREE_CODE (format_num_expr
) != INTEGER_CST
)
787 error ("format string has non-constant operand number");
791 format_num
= TREE_INT_CST_LOW (format_num_expr
);
793 /* If a parameter list is specified, verify that the format_num
794 argument is actually a string, in case the format attribute
796 argument
= TYPE_ARG_TYPES (type
);
799 for (arg_num
= 1; ; ++arg_num
)
801 if (argument
== 0 || arg_num
== format_num
)
803 argument
= TREE_CHAIN (argument
);
806 || TREE_CODE (TREE_VALUE (argument
)) != POINTER_TYPE
807 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument
)))
810 error ("format string arg not a string type");
815 if (TREE_CODE (TREE_TYPE (TREE_TYPE (decl
))) != POINTER_TYPE
816 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_TYPE (decl
))))
819 error ("function does not return string type");
823 record_international_format (DECL_NAME (decl
),
824 DECL_ASSEMBLER_NAME (decl
),
834 if ((TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_INITIAL (decl
))
835 || (TREE_CODE (decl
) != FUNCTION_DECL
&& ! DECL_EXTERNAL (decl
)))
836 error_with_decl (decl
,
837 "`%s' defined both normally and as an alias");
838 else if (decl_function_context (decl
) == 0)
840 tree id
= get_identifier (TREE_STRING_POINTER
841 (TREE_VALUE (args
)));
842 if (TREE_CODE (decl
) == FUNCTION_DECL
)
843 DECL_INITIAL (decl
) = error_mark_node
;
845 DECL_EXTERNAL (decl
) = 0;
846 assemble_alias (decl
, id
);
849 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
855 /* Split SPECS_ATTRS, a list of declspecs and prefix attributes, into two
856 lists. SPECS_ATTRS may also be just a typespec (eg: RECORD_TYPE).
858 The head of the declspec list is stored in DECLSPECS.
859 The head of the attribute list is stored in PREFIX_ATTRIBUTES.
861 Note that attributes in SPECS_ATTRS are stored in the TREE_PURPOSE of
862 the list elements. We drop the containing TREE_LIST nodes and link the
863 resulting attributes together the way decl_attributes expects them. */
866 split_specs_attrs (specs_attrs
, declspecs
, prefix_attributes
)
868 tree
*declspecs
, *prefix_attributes
;
870 tree t
, s
, a
, next
, specs
, attrs
;
872 /* This can happen in c++ (eg: decl: typespec initdecls ';'). */
873 if (specs_attrs
!= NULL_TREE
874 && TREE_CODE (specs_attrs
) != TREE_LIST
)
876 *declspecs
= specs_attrs
;
877 *prefix_attributes
= NULL_TREE
;
881 /* Remember to keep the lists in the same order, element-wise. */
883 specs
= s
= NULL_TREE
;
884 attrs
= a
= NULL_TREE
;
885 for (t
= specs_attrs
; t
; t
= next
)
887 next
= TREE_CHAIN (t
);
888 /* Declspecs have a non-NULL TREE_VALUE. */
889 if (TREE_VALUE (t
) != NULL_TREE
)
891 if (specs
== NULL_TREE
)
901 if (attrs
== NULL_TREE
)
902 attrs
= a
= TREE_PURPOSE (t
);
905 TREE_CHAIN (a
) = TREE_PURPOSE (t
);
906 a
= TREE_PURPOSE (t
);
908 /* More attrs can be linked here, move A to the end. */
909 while (TREE_CHAIN (a
) != NULL_TREE
)
914 /* Terminate the lists. */
916 TREE_CHAIN (s
) = NULL_TREE
;
918 TREE_CHAIN (a
) = NULL_TREE
;
922 *prefix_attributes
= attrs
;
925 /* Strip attributes from SPECS_ATTRS, a list of declspecs and attributes.
926 This function is used by the parser when a rule will accept attributes
927 in a particular position, but we don't want to support that just yet.
929 A warning is issued for every ignored attribute. */
932 strip_attrs (specs_attrs
)
937 split_specs_attrs (specs_attrs
, &specs
, &attrs
);
941 warning ("`%s' attribute ignored",
942 IDENTIFIER_POINTER (TREE_PURPOSE (attrs
)));
943 attrs
= TREE_CHAIN (attrs
);
949 /* Check a printf/fprintf/sprintf/scanf/fscanf/sscanf format against
952 #define T_I &integer_type_node
953 #define T_L &long_integer_type_node
954 #define T_LL &long_long_integer_type_node
955 #define T_S &short_integer_type_node
956 #define T_UI &unsigned_type_node
957 #define T_UL &long_unsigned_type_node
958 #define T_ULL &long_long_unsigned_type_node
959 #define T_US &short_unsigned_type_node
960 #define T_F &float_type_node
961 #define T_D &double_type_node
962 #define T_LD &long_double_type_node
963 #define T_C &char_type_node
964 #define T_UC &unsigned_char_type_node
965 #define T_V &void_type_node
966 #define T_W &wchar_type_node
967 #define T_ST &sizetype
972 /* Type of argument if no length modifier is used. */
974 /* Type of argument if length modifier for shortening is used.
975 If NULL, then this modifier is not allowed. */
977 /* Type of argument if length modifier for shortening to byte if used.
978 If NULL, then this modifier is not allowed. */
980 /* Type of argument if length modifier `l' is used.
981 If NULL, then this modifier is not allowed. */
983 /* Type of argument if length modifier `q' or `ll' is used.
984 If NULL, then this modifier is not allowed. */
986 /* Type of argument if length modifier `L' is used.
987 If NULL, then this modifier is not allowed. */
989 /* Type of argument if length modifier `Z' is used.
990 If NULL, then this modifier is not allowed. */
992 /* List of other modifier characters allowed with these options. */
996 static format_char_info print_char_table
[] = {
997 { "di", 0, T_I
, T_I
, T_I
, T_L
, T_LL
, T_LL
, T_ST
, "-wp0 +" },
998 { "oxX", 0, T_UI
, T_UI
, T_UI
, T_UL
, T_ULL
, T_ULL
, T_ST
, "-wp0#" },
999 { "u", 0, T_UI
, T_UI
, T_UI
, T_UL
, T_ULL
, T_ULL
, T_ST
, "-wp0" },
1000 /* A GNU extension. */
1001 { "m", 0, T_V
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "-wp" },
1002 { "feEgGaA", 0, T_D
, NULL
, NULL
, NULL
, NULL
, T_LD
, NULL
, "-wp0 +#" },
1003 { "c", 0, T_I
, NULL
, NULL
, T_W
, NULL
, NULL
, NULL
, "-w" },
1004 { "C", 0, T_W
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "-w" },
1005 { "s", 1, T_C
, NULL
, NULL
, T_W
, NULL
, NULL
, NULL
, "-wp" },
1006 { "S", 1, T_W
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "-wp" },
1007 { "p", 1, T_V
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "-w" },
1008 { "n", 1, T_I
, NULL
, T_S
, T_L
, T_LL
, NULL
, NULL
, "" },
1012 static format_char_info scan_char_table
[] = {
1013 { "di", 1, T_I
, T_C
, T_S
, T_L
, T_LL
, T_LL
, NULL
, "*" },
1014 { "ouxX", 1, T_UI
, T_UC
, T_US
, T_UL
, T_ULL
, T_ULL
, NULL
, "*" },
1015 { "efgEGaA", 1, T_F
, NULL
, NULL
, T_D
, NULL
, T_LD
, NULL
, "*" },
1016 { "c", 1, T_C
, NULL
, NULL
, T_W
, NULL
, NULL
, NULL
, "*" },
1017 { "s", 1, T_C
, NULL
, NULL
, T_W
, NULL
, NULL
, NULL
, "*a" },
1018 { "[", 1, T_C
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "*a" },
1019 { "C", 1, T_W
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "*" },
1020 { "S", 1, T_W
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "*a" },
1021 { "p", 2, T_V
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "*" },
1022 { "n", 1, T_I
, T_C
, T_S
, T_L
, T_LL
, NULL
, NULL
, "" },
1026 /* Handle format characters recognized by glibc's strftime.c.
1027 '2' - MUST do years as only two digits
1028 '3' - MAY do years as only two digits (depending on locale)
1029 'E' - E modifier is acceptable
1030 'O' - O modifier is acceptable to Standard C
1031 'o' - O modifier is acceptable as a GNU extension
1032 'G' - other GNU extensions */
1034 static format_char_info time_char_table
[] = {
1035 { "y", 0, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "2EO-_0w" },
1036 { "D", 0, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "2" },
1037 { "g", 0, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "2O-_0w" },
1038 { "cx", 0, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "3E" },
1039 { "%RTXnrt", 0, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "" },
1040 { "P", 0, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "G" },
1041 { "HIMSUWdemw", 0, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "-_0Ow" },
1042 { "Vju", 0, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "-_0Oow" },
1043 { "Gklsz", 0, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "-_0OGw" },
1044 { "ABZa", 0, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "^#" },
1045 { "p", 0, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "#" },
1046 { "bh", 0, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "^" },
1047 { "CY", 0, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "-_0EOw" },
1051 typedef struct function_format_info
1053 struct function_format_info
*next
; /* next structure on the list */
1054 tree name
; /* identifier such as "printf" */
1055 tree assembler_name
; /* optional mangled identifier (for C++) */
1056 enum format_type format_type
; /* type of format (printf, scanf, etc.) */
1057 int format_num
; /* number of format argument */
1058 int first_arg_num
; /* number of first arg (zero for varargs) */
1059 } function_format_info
;
1061 static function_format_info
*function_format_list
= NULL
;
1063 typedef struct international_format_info
1065 struct international_format_info
*next
; /* next structure on the list */
1066 tree name
; /* identifier such as "gettext" */
1067 tree assembler_name
; /* optional mangled identifier (for C++) */
1068 int format_num
; /* number of format argument */
1069 } international_format_info
;
1071 static international_format_info
*international_format_list
= NULL
;
1073 static void check_format_info
PROTO((function_format_info
*, tree
));
1075 /* Initialize the table of functions to perform format checking on.
1076 The ANSI functions are always checked (whether <stdio.h> is
1077 included or not), since it is common to call printf without
1078 including <stdio.h>. There shouldn't be a problem with this,
1079 since ANSI reserves these function names whether you include the
1080 header file or not. In any case, the checking is harmless.
1082 Also initialize the name of function that modify the format string for
1083 internationalization purposes. */
1086 init_function_format_info ()
1088 record_function_format (get_identifier ("printf"), NULL_TREE
,
1089 printf_format_type
, 1, 2);
1090 record_function_format (get_identifier ("fprintf"), NULL_TREE
,
1091 printf_format_type
, 2, 3);
1092 record_function_format (get_identifier ("sprintf"), NULL_TREE
,
1093 printf_format_type
, 2, 3);
1094 record_function_format (get_identifier ("scanf"), NULL_TREE
,
1095 scanf_format_type
, 1, 2);
1096 record_function_format (get_identifier ("fscanf"), NULL_TREE
,
1097 scanf_format_type
, 2, 3);
1098 record_function_format (get_identifier ("sscanf"), NULL_TREE
,
1099 scanf_format_type
, 2, 3);
1100 record_function_format (get_identifier ("vprintf"), NULL_TREE
,
1101 printf_format_type
, 1, 0);
1102 record_function_format (get_identifier ("vfprintf"), NULL_TREE
,
1103 printf_format_type
, 2, 0);
1104 record_function_format (get_identifier ("vsprintf"), NULL_TREE
,
1105 printf_format_type
, 2, 0);
1106 record_function_format (get_identifier ("strftime"), NULL_TREE
,
1107 strftime_format_type
, 3, 0);
1109 record_international_format (get_identifier ("gettext"), NULL_TREE
, 1);
1110 record_international_format (get_identifier ("dgettext"), NULL_TREE
, 2);
1111 record_international_format (get_identifier ("dcgettext"), NULL_TREE
, 2);
1114 /* Record information for argument format checking. FUNCTION_IDENT is
1115 the identifier node for the name of the function to check (its decl
1116 need not exist yet).
1117 FORMAT_TYPE specifies the type of format checking. FORMAT_NUM is the number
1118 of the argument which is the format control string (starting from 1).
1119 FIRST_ARG_NUM is the number of the first actual argument to check
1120 against the format string, or zero if no checking is not be done
1121 (e.g. for varargs such as vfprintf). */
1124 record_function_format (name
, assembler_name
, format_type
,
1125 format_num
, first_arg_num
)
1127 tree assembler_name
;
1128 enum format_type format_type
;
1132 function_format_info
*info
;
1134 /* Re-use existing structure if it's there. */
1136 for (info
= function_format_list
; info
; info
= info
->next
)
1138 if (info
->name
== name
&& info
->assembler_name
== assembler_name
)
1143 info
= (function_format_info
*) xmalloc (sizeof (function_format_info
));
1144 info
->next
= function_format_list
;
1145 function_format_list
= info
;
1148 info
->assembler_name
= assembler_name
;
1151 info
->format_type
= format_type
;
1152 info
->format_num
= format_num
;
1153 info
->first_arg_num
= first_arg_num
;
1156 /* Record information for the names of function that modify the format
1157 argument to format functions. FUNCTION_IDENT is the identifier node for
1158 the name of the function (its decl need not exist yet) and FORMAT_NUM is
1159 the number of the argument which is the format control string (starting
1163 record_international_format (name
, assembler_name
, format_num
)
1165 tree assembler_name
;
1168 international_format_info
*info
;
1170 /* Re-use existing structure if it's there. */
1172 for (info
= international_format_list
; info
; info
= info
->next
)
1174 if (info
->name
== name
&& info
->assembler_name
== assembler_name
)
1181 = (international_format_info
*)
1182 xmalloc (sizeof (international_format_info
));
1183 info
->next
= international_format_list
;
1184 international_format_list
= info
;
1187 info
->assembler_name
= assembler_name
;
1190 info
->format_num
= format_num
;
1193 static char tfaff
[] = "too few arguments for format";
1195 /* Check the argument list of a call to printf, scanf, etc.
1196 NAME is the function identifier.
1197 ASSEMBLER_NAME is the function's assembler identifier.
1198 (Either NAME or ASSEMBLER_NAME, but not both, may be NULL_TREE.)
1199 PARAMS is the list of argument values. */
1202 check_function_format (name
, assembler_name
, params
)
1204 tree assembler_name
;
1207 function_format_info
*info
;
1209 /* See if this function is a format function. */
1210 for (info
= function_format_list
; info
; info
= info
->next
)
1212 if (info
->assembler_name
1213 ? (info
->assembler_name
== assembler_name
)
1214 : (info
->name
== name
))
1216 /* Yup; check it. */
1217 check_format_info (info
, params
);
1223 /* Check the argument list of a call to printf, scanf, etc.
1224 INFO points to the function_format_info structure.
1225 PARAMS is the list of argument values. */
1228 check_format_info (info
, params
)
1229 function_format_info
*info
;
1234 int suppressed
, wide
, precise
;
1238 int integral_format
;
1243 tree first_fillin_param
;
1245 format_char_info
*fci
;
1247 int has_operand_number
= 0;
1249 /* Skip to format argument. If the argument isn't available, there's
1250 no work for us to do; prototype checking will catch the problem. */
1251 for (arg_num
= 1; ; ++arg_num
)
1255 if (arg_num
== info
->format_num
)
1257 params
= TREE_CHAIN (params
);
1259 format_tree
= TREE_VALUE (params
);
1260 params
= TREE_CHAIN (params
);
1261 if (format_tree
== 0)
1264 /* We can only check the format if it's a string constant. */
1265 while (TREE_CODE (format_tree
) == NOP_EXPR
)
1266 format_tree
= TREE_OPERAND (format_tree
, 0); /* strip coercion */
1268 if (TREE_CODE (format_tree
) == CALL_EXPR
1269 && TREE_CODE (TREE_OPERAND (format_tree
, 0)) == ADDR_EXPR
1270 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (format_tree
, 0), 0))
1273 tree function
= TREE_OPERAND (TREE_OPERAND (format_tree
, 0), 0);
1275 /* See if this is a call to a known internationalization function
1276 that modifies the format arg. */
1277 international_format_info
*info
;
1279 for (info
= international_format_list
; info
; info
= info
->next
)
1280 if (info
->assembler_name
1281 ? (info
->assembler_name
== DECL_ASSEMBLER_NAME (function
))
1282 : (info
->name
== DECL_NAME (function
)))
1287 for (inner_args
= TREE_OPERAND (format_tree
, 1), i
= 1;
1289 inner_args
= TREE_CHAIN (inner_args
), i
++)
1290 if (i
== info
->format_num
)
1292 format_tree
= TREE_VALUE (inner_args
);
1294 while (TREE_CODE (format_tree
) == NOP_EXPR
)
1295 format_tree
= TREE_OPERAND (format_tree
, 0);
1300 if (integer_zerop (format_tree
))
1302 warning ("null format string");
1305 if (TREE_CODE (format_tree
) != ADDR_EXPR
)
1307 format_tree
= TREE_OPERAND (format_tree
, 0);
1308 if (TREE_CODE (format_tree
) != STRING_CST
)
1310 format_chars
= TREE_STRING_POINTER (format_tree
);
1311 format_length
= TREE_STRING_LENGTH (format_tree
);
1312 if (format_length
<= 1)
1313 warning ("zero-length format string");
1314 if (format_chars
[--format_length
] != 0)
1316 warning ("unterminated format string");
1319 /* Skip to first argument to check. */
1320 while (arg_num
+ 1 < info
->first_arg_num
)
1324 params
= TREE_CHAIN (params
);
1328 first_fillin_param
= params
;
1332 if (*format_chars
== 0)
1334 if (format_chars
- TREE_STRING_POINTER (format_tree
) != format_length
)
1335 warning ("embedded `\\0' in format");
1336 if (info
->first_arg_num
!= 0 && params
!= 0 && ! has_operand_number
)
1337 warning ("too many arguments for format");
1340 if (*format_chars
++ != '%')
1342 if (*format_chars
== 0)
1344 warning ("spurious trailing `%%' in format");
1347 if (*format_chars
== '%')
1353 suppressed
= wide
= precise
= FALSE
;
1354 if (info
->format_type
== scanf_format_type
)
1356 suppressed
= *format_chars
== '*';
1359 while (isdigit (*format_chars
))
1362 else if (info
->format_type
== strftime_format_type
)
1364 while (*format_chars
!= 0 && index ("_-0^#", *format_chars
) != 0)
1367 warning ("ANSI C does not support the strftime `%c' flag",
1369 if (index (flag_chars
, *format_chars
) != 0)
1371 warning ("repeated `%c' flag in format",
1377 i
= strlen (flag_chars
);
1378 flag_chars
[i
++] = *format_chars
++;
1382 while (isdigit ((unsigned char) *format_chars
))
1387 if (wide
&& pedantic
)
1388 warning ("ANSI C does not support strftime format width");
1389 if (*format_chars
== 'E' || *format_chars
== 'O')
1391 i
= strlen (flag_chars
);
1392 flag_chars
[i
++] = *format_chars
++;
1394 if (*format_chars
== 'E' || *format_chars
== 'O')
1396 warning ("multiple E/O modifiers in format");
1397 while (*format_chars
== 'E' || *format_chars
== 'O')
1402 else if (info
->format_type
== printf_format_type
)
1404 /* See if we have a number followed by a dollar sign. If we do,
1405 it is an operand number, so set PARAMS to that operand. */
1406 if (*format_chars
>= '0' && *format_chars
<= '9')
1408 char *p
= format_chars
;
1410 while (*p
>= '0' && *p
++ <= '9')
1415 int opnum
= atoi (format_chars
);
1417 params
= first_fillin_param
;
1418 format_chars
= p
+ 1;
1419 has_operand_number
= 1;
1421 for (i
= 1; i
< opnum
&& params
!= 0; i
++)
1422 params
= TREE_CHAIN (params
);
1424 if (opnum
== 0 || params
== 0)
1426 warning ("operand number out of range in format");
1432 while (*format_chars
!= 0 && index (" +#0-", *format_chars
) != 0)
1434 if (index (flag_chars
, *format_chars
) != 0)
1435 warning ("repeated `%c' flag in format", *format_chars
++);
1438 i
= strlen (flag_chars
);
1439 flag_chars
[i
++] = *format_chars
++;
1443 /* "If the space and + flags both appear,
1444 the space flag will be ignored." */
1445 if (index (flag_chars
, ' ') != 0
1446 && index (flag_chars
, '+') != 0)
1447 warning ("use of both ` ' and `+' flags in format");
1448 /* "If the 0 and - flags both appear,
1449 the 0 flag will be ignored." */
1450 if (index (flag_chars
, '0') != 0
1451 && index (flag_chars
, '-') != 0)
1452 warning ("use of both `0' and `-' flags in format");
1453 if (*format_chars
== '*')
1456 /* "...a field width...may be indicated by an asterisk.
1457 In this case, an int argument supplies the field width..." */
1464 if (info
->first_arg_num
!= 0)
1466 cur_param
= TREE_VALUE (params
);
1467 params
= TREE_CHAIN (params
);
1469 /* size_t is generally not valid here.
1470 It will work on most machines, because size_t and int
1471 have the same mode. But might as well warn anyway,
1472 since it will fail on other machines. */
1473 if ((TYPE_MAIN_VARIANT (TREE_TYPE (cur_param
))
1474 != integer_type_node
)
1476 (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param
))
1477 != unsigned_type_node
))
1478 warning ("field width is not type int (arg %d)", arg_num
);
1483 while (isdigit (*format_chars
))
1489 if (*format_chars
== '.')
1493 if (*format_chars
!= '*' && !isdigit (*format_chars
))
1494 warning ("`.' not followed by `*' or digit in format");
1495 /* "...a...precision...may be indicated by an asterisk.
1496 In this case, an int argument supplies the...precision." */
1497 if (*format_chars
== '*')
1499 if (info
->first_arg_num
!= 0)
1507 cur_param
= TREE_VALUE (params
);
1508 params
= TREE_CHAIN (params
);
1510 if (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param
))
1511 != integer_type_node
)
1512 warning ("field width is not type int (arg %d)",
1518 while (isdigit (*format_chars
))
1526 if (info
->format_type
!= strftime_format_type
)
1528 if (*format_chars
== 'h' || *format_chars
== 'l')
1529 length_char
= *format_chars
++;
1530 else if (*format_chars
== 'q' || *format_chars
== 'L')
1532 length_char
= *format_chars
++;
1534 warning ("ANSI C does not support the `%c' length modifier",
1537 else if (*format_chars
== 'Z')
1539 length_char
= *format_chars
++;
1541 warning ("ANSI C does not support the `Z' length modifier");
1545 if (length_char
== 'l' && *format_chars
== 'l')
1547 length_char
= 'q', format_chars
++;
1548 /* FIXME: Is allowed in ISO C 9x. */
1550 warning ("ANSI C does not support the `ll' length modifier");
1552 if (*format_chars
== 'a' && info
->format_type
== scanf_format_type
)
1554 if (format_chars
[1] == 's' || format_chars
[1] == 'S'
1555 || format_chars
[1] == '[')
1557 /* `a' is used as a flag. */
1562 if (suppressed
&& length_char
!= 0)
1563 warning ("use of `*' and `%c' together in format", length_char
);
1565 format_char
= *format_chars
;
1566 if (format_char
== 0
1567 || (info
->format_type
!= strftime_format_type
&& format_char
== '%'))
1569 warning ("conversion lacks type at end of format");
1572 /* The m, C, and S formats are GNU extensions. */
1573 if (pedantic
&& info
->format_type
!= strftime_format_type
1574 && (format_char
== 'm' || format_char
== 'C' || format_char
== 'S'))
1575 warning ("ANSI C does not support the `%c' format", format_char
);
1576 /* ??? The a and A formats are C9X extensions, and should be allowed
1577 when a C9X option is added. */
1578 if (pedantic
&& info
->format_type
!= strftime_format_type
1579 && (format_char
== 'a' || format_char
== 'A'))
1580 warning ("ANSI C does not support the `%c' format", format_char
);
1582 switch (info
->format_type
)
1584 case printf_format_type
:
1585 fci
= print_char_table
;
1587 case scanf_format_type
:
1588 fci
= scan_char_table
;
1590 case strftime_format_type
:
1591 fci
= time_char_table
;
1596 while (fci
->format_chars
!= 0
1597 && index (fci
->format_chars
, format_char
) == 0)
1599 if (fci
->format_chars
== 0)
1601 if (format_char
>= 040 && format_char
< 0177)
1602 warning ("unknown conversion type character `%c' in format",
1605 warning ("unknown conversion type character 0x%x in format",
1611 if (index (fci
->flag_chars
, 'G') != 0)
1612 warning ("ANSI C does not support `%%%c'", format_char
);
1613 if (index (fci
->flag_chars
, 'o') != 0
1614 && index (flag_chars
, 'O') != 0)
1615 warning ("ANSI C does not support `%%O%c'", format_char
);
1617 if (wide
&& index (fci
->flag_chars
, 'w') == 0)
1618 warning ("width used with `%c' format", format_char
);
1619 if (index (fci
->flag_chars
, '2') != 0)
1620 warning ("`%%%c' yields only last 2 digits of year", format_char
);
1621 else if (index (fci
->flag_chars
, '3') != 0)
1622 warning ("`%%%c' yields only last 2 digits of year in some locales",
1624 if (precise
&& index (fci
->flag_chars
, 'p') == 0)
1625 warning ("precision used with `%c' format", format_char
);
1626 if (aflag
&& index (fci
->flag_chars
, 'a') == 0)
1628 warning ("`a' flag used with `%c' format", format_char
);
1629 /* To simplify the following code. */
1632 /* The a flag is a GNU extension. */
1633 else if (pedantic
&& aflag
)
1634 warning ("ANSI C does not support the `a' flag");
1635 if (info
->format_type
== scanf_format_type
&& format_char
== '[')
1637 /* Skip over scan set, in case it happens to have '%' in it. */
1638 if (*format_chars
== '^')
1640 /* Find closing bracket; if one is hit immediately, then
1641 it's part of the scan set rather than a terminator. */
1642 if (*format_chars
== ']')
1644 while (*format_chars
&& *format_chars
!= ']')
1646 if (*format_chars
!= ']')
1647 /* The end of the format string was reached. */
1648 warning ("no closing `]' for `%%[' format");
1652 if (index (fci
->flag_chars
, '*') == 0)
1653 warning ("suppression of `%c' conversion in format", format_char
);
1656 for (i
= 0; flag_chars
[i
] != 0; ++i
)
1658 if (index (fci
->flag_chars
, flag_chars
[i
]) == 0)
1659 warning ("flag `%c' used with type `%c'",
1660 flag_chars
[i
], format_char
);
1662 if (info
->format_type
== strftime_format_type
)
1664 if (precise
&& index (flag_chars
, '0') != 0
1665 && (format_char
== 'd' || format_char
== 'i'
1666 || format_char
== 'o' || format_char
== 'u'
1667 || format_char
== 'x' || format_char
== 'x'))
1668 warning ("`0' flag ignored with precision specifier and `%c' format",
1670 switch (length_char
)
1672 default: wanted_type
= fci
->nolen
? *(fci
->nolen
) : 0; break;
1673 case 'H': wanted_type
= fci
->hhlen
? *(fci
->hhlen
) : 0; break;
1674 case 'h': wanted_type
= fci
->hlen
? *(fci
->hlen
) : 0; break;
1675 case 'l': wanted_type
= fci
->llen
? *(fci
->llen
) : 0; break;
1676 case 'q': wanted_type
= fci
->qlen
? *(fci
->qlen
) : 0; break;
1677 case 'L': wanted_type
= fci
->bigllen
? *(fci
->bigllen
) : 0; break;
1678 case 'Z': wanted_type
= fci
->zlen
? *fci
->zlen
: 0; break;
1680 if (wanted_type
== 0)
1681 warning ("use of `%c' length character with `%c' type character",
1682 length_char
, format_char
);
1685 ** XXX -- should kvetch about stuff such as
1689 ** scanf ("%d", &i);
1693 /* Finally. . .check type of argument against desired type! */
1694 if (info
->first_arg_num
== 0)
1696 if (fci
->pointer_count
== 0 && wanted_type
== void_type_node
)
1697 /* This specifier takes no argument. */
1704 cur_param
= TREE_VALUE (params
);
1705 params
= TREE_CHAIN (params
);
1707 cur_type
= TREE_TYPE (cur_param
);
1709 STRIP_NOPS (cur_param
);
1711 /* Check the types of any additional pointer arguments
1712 that precede the "real" argument. */
1713 for (i
= 0; i
< fci
->pointer_count
+ aflag
; ++i
)
1715 if (TREE_CODE (cur_type
) == POINTER_TYPE
)
1717 cur_type
= TREE_TYPE (cur_type
);
1719 if (cur_param
!= 0 && TREE_CODE (cur_param
) == ADDR_EXPR
)
1720 cur_param
= TREE_OPERAND (cur_param
, 0);
1726 if (TREE_CODE (cur_type
) != ERROR_MARK
)
1727 warning ("format argument is not a %s (arg %d)",
1728 ((fci
->pointer_count
+ aflag
== 1)
1729 ? "pointer" : "pointer to a pointer"),
1734 /* See if this is an attempt to write into a const type with
1736 if (info
->format_type
== scanf_format_type
1737 && i
== fci
->pointer_count
+ aflag
1739 && TREE_CODE (cur_type
) != ERROR_MARK
1740 && (TYPE_READONLY (cur_type
)
1742 && (TREE_CODE_CLASS (TREE_CODE (cur_param
)) == 'c'
1743 || (TREE_CODE_CLASS (TREE_CODE (cur_param
)) == 'd'
1744 && TREE_READONLY (cur_param
))))))
1745 warning ("writing into constant object (arg %d)", arg_num
);
1747 /* Check the type of the "real" argument, if there's a type we want. */
1748 if (i
== fci
->pointer_count
+ aflag
&& wanted_type
!= 0
1749 && TREE_CODE (cur_type
) != ERROR_MARK
1750 && wanted_type
!= TYPE_MAIN_VARIANT (cur_type
)
1751 /* If we want `void *', allow any pointer type.
1752 (Anything else would already have got a warning.) */
1753 && ! (wanted_type
== void_type_node
1754 && fci
->pointer_count
> 0)
1755 /* Don't warn about differences merely in signedness. */
1756 && !(TREE_CODE (wanted_type
) == INTEGER_TYPE
1757 && TREE_CODE (TYPE_MAIN_VARIANT (cur_type
)) == INTEGER_TYPE
1758 && (TREE_UNSIGNED (wanted_type
)
1759 ? wanted_type
== (cur_type
= unsigned_type (cur_type
))
1760 : wanted_type
== (cur_type
= signed_type (cur_type
))))
1761 /* Likewise, "signed char", "unsigned char" and "char" are
1762 equivalent but the above test won't consider them equivalent. */
1763 && ! (wanted_type
== char_type_node
1764 && (TYPE_MAIN_VARIANT (cur_type
) == signed_char_type_node
1765 || TYPE_MAIN_VARIANT (cur_type
) == unsigned_char_type_node
)))
1767 register char *this;
1768 register char *that
;
1770 this = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (wanted_type
)));
1772 if (TREE_CODE (cur_type
) != ERROR_MARK
1773 && TYPE_NAME (cur_type
) != 0
1774 && TREE_CODE (cur_type
) != INTEGER_TYPE
1775 && !(TREE_CODE (cur_type
) == POINTER_TYPE
1776 && TREE_CODE (TREE_TYPE (cur_type
)) == INTEGER_TYPE
))
1778 if (TREE_CODE (TYPE_NAME (cur_type
)) == TYPE_DECL
1779 && DECL_NAME (TYPE_NAME (cur_type
)) != 0)
1780 that
= IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type
)));
1782 that
= IDENTIFIER_POINTER (TYPE_NAME (cur_type
));
1785 /* A nameless type can't possibly match what the format wants.
1786 So there will be a warning for it.
1787 Make up a string to describe vaguely what it is. */
1790 if (TREE_CODE (cur_type
) == POINTER_TYPE
)
1793 that
= "different type";
1796 /* Make the warning better in case of mismatch of int vs long. */
1797 if (TREE_CODE (cur_type
) == INTEGER_TYPE
1798 && TREE_CODE (wanted_type
) == INTEGER_TYPE
1799 && TYPE_PRECISION (cur_type
) == TYPE_PRECISION (wanted_type
)
1800 && TYPE_NAME (cur_type
) != 0
1801 && TREE_CODE (TYPE_NAME (cur_type
)) == TYPE_DECL
)
1802 that
= IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type
)));
1804 if (strcmp (this, that
) != 0)
1805 warning ("%s format, %s arg (arg %d)", this, that
, arg_num
);
1810 /* Print a warning if a constant expression had overflow in folding.
1811 Invoke this function on every expression that the language
1812 requires to be a constant expression.
1813 Note the ANSI C standard says it is erroneous for a
1814 constant expression to overflow. */
1817 constant_expression_warning (value
)
1820 if ((TREE_CODE (value
) == INTEGER_CST
|| TREE_CODE (value
) == REAL_CST
1821 || TREE_CODE (value
) == COMPLEX_CST
)
1822 && TREE_CONSTANT_OVERFLOW (value
) && pedantic
)
1823 pedwarn ("overflow in constant expression");
1826 /* Print a warning if an expression had overflow in folding.
1827 Invoke this function on every expression that
1828 (1) appears in the source code, and
1829 (2) might be a constant expression that overflowed, and
1830 (3) is not already checked by convert_and_check;
1831 however, do not invoke this function on operands of explicit casts. */
1834 overflow_warning (value
)
1837 if ((TREE_CODE (value
) == INTEGER_CST
1838 || (TREE_CODE (value
) == COMPLEX_CST
1839 && TREE_CODE (TREE_REALPART (value
)) == INTEGER_CST
))
1840 && TREE_OVERFLOW (value
))
1842 TREE_OVERFLOW (value
) = 0;
1843 if (skip_evaluation
== 0)
1844 warning ("integer overflow in expression");
1846 else if ((TREE_CODE (value
) == REAL_CST
1847 || (TREE_CODE (value
) == COMPLEX_CST
1848 && TREE_CODE (TREE_REALPART (value
)) == REAL_CST
))
1849 && TREE_OVERFLOW (value
))
1851 TREE_OVERFLOW (value
) = 0;
1852 if (skip_evaluation
== 0)
1853 warning ("floating point overflow in expression");
1857 /* Print a warning if a large constant is truncated to unsigned,
1858 or if -Wconversion is used and a constant < 0 is converted to unsigned.
1859 Invoke this function on every expression that might be implicitly
1860 converted to an unsigned type. */
1863 unsigned_conversion_warning (result
, operand
)
1864 tree result
, operand
;
1866 if (TREE_CODE (operand
) == INTEGER_CST
1867 && TREE_CODE (TREE_TYPE (result
)) == INTEGER_TYPE
1868 && TREE_UNSIGNED (TREE_TYPE (result
))
1869 && skip_evaluation
== 0
1870 && !int_fits_type_p (operand
, TREE_TYPE (result
)))
1872 if (!int_fits_type_p (operand
, signed_type (TREE_TYPE (result
))))
1873 /* This detects cases like converting -129 or 256 to unsigned char. */
1874 warning ("large integer implicitly truncated to unsigned type");
1875 else if (warn_conversion
)
1876 warning ("negative integer implicitly converted to unsigned type");
1880 /* Convert EXPR to TYPE, warning about conversion problems with constants.
1881 Invoke this function on every expression that is converted implicitly,
1882 i.e. because of language rules and not because of an explicit cast. */
1885 convert_and_check (type
, expr
)
1888 tree t
= convert (type
, expr
);
1889 if (TREE_CODE (t
) == INTEGER_CST
)
1891 if (TREE_OVERFLOW (t
))
1893 TREE_OVERFLOW (t
) = 0;
1895 /* Do not diagnose overflow in a constant expression merely
1896 because a conversion overflowed. */
1897 TREE_CONSTANT_OVERFLOW (t
) = TREE_CONSTANT_OVERFLOW (expr
);
1899 /* No warning for converting 0x80000000 to int. */
1900 if (!(TREE_UNSIGNED (type
) < TREE_UNSIGNED (TREE_TYPE (expr
))
1901 && TREE_CODE (TREE_TYPE (expr
)) == INTEGER_TYPE
1902 && TYPE_PRECISION (type
) == TYPE_PRECISION (TREE_TYPE (expr
))))
1903 /* If EXPR fits in the unsigned version of TYPE,
1904 don't warn unless pedantic. */
1906 || TREE_UNSIGNED (type
)
1907 || ! int_fits_type_p (expr
, unsigned_type (type
)))
1908 && skip_evaluation
== 0)
1909 warning ("overflow in implicit constant conversion");
1912 unsigned_conversion_warning (t
, expr
);
1918 c_expand_expr_stmt (expr
)
1921 /* Do default conversion if safe and possibly important,
1922 in case within ({...}). */
1923 if ((TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
&& lvalue_p (expr
))
1924 || TREE_CODE (TREE_TYPE (expr
)) == FUNCTION_TYPE
)
1925 expr
= default_conversion (expr
);
1927 if (TREE_TYPE (expr
) != error_mark_node
1928 && TYPE_SIZE (TREE_TYPE (expr
)) == 0
1929 && TREE_CODE (TREE_TYPE (expr
)) != ARRAY_TYPE
)
1930 error ("expression statement has incomplete type");
1932 expand_expr_stmt (expr
);
1935 /* Validate the expression after `case' and apply default promotions. */
1938 check_case_value (value
)
1941 if (value
== NULL_TREE
)
1944 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1945 STRIP_TYPE_NOPS (value
);
1947 if (TREE_CODE (value
) != INTEGER_CST
1948 && value
!= error_mark_node
)
1950 error ("case label does not reduce to an integer constant");
1951 value
= error_mark_node
;
1954 /* Promote char or short to int. */
1955 value
= default_conversion (value
);
1957 constant_expression_warning (value
);
1962 /* Return an integer type with BITS bits of precision,
1963 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
1966 type_for_size (bits
, unsignedp
)
1970 if (bits
== TYPE_PRECISION (integer_type_node
))
1971 return unsignedp
? unsigned_type_node
: integer_type_node
;
1973 if (bits
== TYPE_PRECISION (signed_char_type_node
))
1974 return unsignedp
? unsigned_char_type_node
: signed_char_type_node
;
1976 if (bits
== TYPE_PRECISION (short_integer_type_node
))
1977 return unsignedp
? short_unsigned_type_node
: short_integer_type_node
;
1979 if (bits
== TYPE_PRECISION (long_integer_type_node
))
1980 return unsignedp
? long_unsigned_type_node
: long_integer_type_node
;
1982 if (bits
== TYPE_PRECISION (long_long_integer_type_node
))
1983 return (unsignedp
? long_long_unsigned_type_node
1984 : long_long_integer_type_node
);
1986 if (bits
<= TYPE_PRECISION (intQI_type_node
))
1987 return unsignedp
? unsigned_intQI_type_node
: intQI_type_node
;
1989 if (bits
<= TYPE_PRECISION (intHI_type_node
))
1990 return unsignedp
? unsigned_intHI_type_node
: intHI_type_node
;
1992 if (bits
<= TYPE_PRECISION (intSI_type_node
))
1993 return unsignedp
? unsigned_intSI_type_node
: intSI_type_node
;
1995 if (bits
<= TYPE_PRECISION (intDI_type_node
))
1996 return unsignedp
? unsigned_intDI_type_node
: intDI_type_node
;
2001 /* Return a data type that has machine mode MODE.
2002 If the mode is an integer,
2003 then UNSIGNEDP selects between signed and unsigned types. */
2006 type_for_mode (mode
, unsignedp
)
2007 enum machine_mode mode
;
2010 if (mode
== TYPE_MODE (integer_type_node
))
2011 return unsignedp
? unsigned_type_node
: integer_type_node
;
2013 if (mode
== TYPE_MODE (signed_char_type_node
))
2014 return unsignedp
? unsigned_char_type_node
: signed_char_type_node
;
2016 if (mode
== TYPE_MODE (short_integer_type_node
))
2017 return unsignedp
? short_unsigned_type_node
: short_integer_type_node
;
2019 if (mode
== TYPE_MODE (long_integer_type_node
))
2020 return unsignedp
? long_unsigned_type_node
: long_integer_type_node
;
2022 if (mode
== TYPE_MODE (long_long_integer_type_node
))
2023 return unsignedp
? long_long_unsigned_type_node
: long_long_integer_type_node
;
2025 if (mode
== TYPE_MODE (intQI_type_node
))
2026 return unsignedp
? unsigned_intQI_type_node
: intQI_type_node
;
2028 if (mode
== TYPE_MODE (intHI_type_node
))
2029 return unsignedp
? unsigned_intHI_type_node
: intHI_type_node
;
2031 if (mode
== TYPE_MODE (intSI_type_node
))
2032 return unsignedp
? unsigned_intSI_type_node
: intSI_type_node
;
2034 if (mode
== TYPE_MODE (intDI_type_node
))
2035 return unsignedp
? unsigned_intDI_type_node
: intDI_type_node
;
2037 if (mode
== TYPE_MODE (intTI_type_node
))
2038 return unsignedp
? unsigned_intTI_type_node
: intTI_type_node
;
2040 if (mode
== TYPE_MODE (float_type_node
))
2041 return float_type_node
;
2043 if (mode
== TYPE_MODE (double_type_node
))
2044 return double_type_node
;
2046 if (mode
== TYPE_MODE (long_double_type_node
))
2047 return long_double_type_node
;
2049 if (mode
== TYPE_MODE (build_pointer_type (char_type_node
)))
2050 return build_pointer_type (char_type_node
);
2052 if (mode
== TYPE_MODE (build_pointer_type (integer_type_node
)))
2053 return build_pointer_type (integer_type_node
);
2058 /* Return the minimum number of bits needed to represent VALUE in a
2059 signed or unsigned type, UNSIGNEDP says which. */
2062 min_precision (value
, unsignedp
)
2068 /* If the value is negative, compute its negative minus 1. The latter
2069 adjustment is because the absolute value of the largest negative value
2070 is one larger than the largest positive value. This is equivalent to
2071 a bit-wise negation, so use that operation instead. */
2073 if (tree_int_cst_sgn (value
) < 0)
2074 value
= fold (build1 (BIT_NOT_EXPR
, TREE_TYPE (value
), value
));
2076 /* Return the number of bits needed, taking into account the fact
2077 that we need one more bit for a signed than unsigned type. */
2079 if (integer_zerop (value
))
2081 else if (TREE_INT_CST_HIGH (value
) != 0)
2082 log
= HOST_BITS_PER_WIDE_INT
+ floor_log2 (TREE_INT_CST_HIGH (value
));
2084 log
= floor_log2 (TREE_INT_CST_LOW (value
));
2086 return log
+ 1 + ! unsignedp
;
2089 /* Print an error message for invalid operands to arith operation CODE.
2090 NOP_EXPR is used as a special case (see truthvalue_conversion). */
2093 binary_op_error (code
)
2094 enum tree_code code
;
2096 register char *opname
;
2101 error ("invalid truth-value expression");
2105 opname
= "+"; break;
2107 opname
= "-"; break;
2109 opname
= "*"; break;
2111 opname
= "max"; break;
2113 opname
= "min"; break;
2115 opname
= "=="; break;
2117 opname
= "!="; break;
2119 opname
= "<="; break;
2121 opname
= ">="; break;
2123 opname
= "<"; break;
2125 opname
= ">"; break;
2127 opname
= "<<"; break;
2129 opname
= ">>"; break;
2130 case TRUNC_MOD_EXPR
:
2131 case FLOOR_MOD_EXPR
:
2132 opname
= "%"; break;
2133 case TRUNC_DIV_EXPR
:
2134 case FLOOR_DIV_EXPR
:
2135 opname
= "/"; break;
2137 opname
= "&"; break;
2139 opname
= "|"; break;
2140 case TRUTH_ANDIF_EXPR
:
2141 opname
= "&&"; break;
2142 case TRUTH_ORIF_EXPR
:
2143 opname
= "||"; break;
2145 opname
= "^"; break;
2148 opname
= "rotate"; break;
2150 opname
= "unknown"; break;
2152 error ("invalid operands to binary %s", opname
);
2155 /* Subroutine of build_binary_op, used for comparison operations.
2156 See if the operands have both been converted from subword integer types
2157 and, if so, perhaps change them both back to their original type.
2158 This function is also responsible for converting the two operands
2159 to the proper common type for comparison.
2161 The arguments of this function are all pointers to local variables
2162 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
2163 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
2165 If this function returns nonzero, it means that the comparison has
2166 a constant value. What this function returns is an expression for
2170 shorten_compare (op0_ptr
, op1_ptr
, restype_ptr
, rescode_ptr
)
2171 tree
*op0_ptr
, *op1_ptr
;
2173 enum tree_code
*rescode_ptr
;
2176 tree op0
= *op0_ptr
;
2177 tree op1
= *op1_ptr
;
2178 int unsignedp0
, unsignedp1
;
2180 tree primop0
, primop1
;
2181 enum tree_code code
= *rescode_ptr
;
2183 /* Throw away any conversions to wider types
2184 already present in the operands. */
2186 primop0
= get_narrower (op0
, &unsignedp0
);
2187 primop1
= get_narrower (op1
, &unsignedp1
);
2189 /* Handle the case that OP0 does not *contain* a conversion
2190 but it *requires* conversion to FINAL_TYPE. */
2192 if (op0
== primop0
&& TREE_TYPE (op0
) != *restype_ptr
)
2193 unsignedp0
= TREE_UNSIGNED (TREE_TYPE (op0
));
2194 if (op1
== primop1
&& TREE_TYPE (op1
) != *restype_ptr
)
2195 unsignedp1
= TREE_UNSIGNED (TREE_TYPE (op1
));
2197 /* If one of the operands must be floated, we cannot optimize. */
2198 real1
= TREE_CODE (TREE_TYPE (primop0
)) == REAL_TYPE
;
2199 real2
= TREE_CODE (TREE_TYPE (primop1
)) == REAL_TYPE
;
2201 /* If first arg is constant, swap the args (changing operation
2202 so value is preserved), for canonicalization. Don't do this if
2203 the second arg is 0. */
2205 if (TREE_CONSTANT (primop0
)
2206 && ! integer_zerop (primop1
) && ! real_zerop (primop1
))
2208 register tree tem
= primop0
;
2209 register int temi
= unsignedp0
;
2217 unsignedp0
= unsignedp1
;
2240 *rescode_ptr
= code
;
2243 /* If comparing an integer against a constant more bits wide,
2244 maybe we can deduce a value of 1 or 0 independent of the data.
2245 Or else truncate the constant now
2246 rather than extend the variable at run time.
2248 This is only interesting if the constant is the wider arg.
2249 Also, it is not safe if the constant is unsigned and the
2250 variable arg is signed, since in this case the variable
2251 would be sign-extended and then regarded as unsigned.
2252 Our technique fails in this case because the lowest/highest
2253 possible unsigned results don't follow naturally from the
2254 lowest/highest possible values of the variable operand.
2255 For just EQ_EXPR and NE_EXPR there is another technique that
2256 could be used: see if the constant can be faithfully represented
2257 in the other operand's type, by truncating it and reextending it
2258 and see if that preserves the constant's value. */
2260 if (!real1
&& !real2
2261 && TREE_CODE (primop1
) == INTEGER_CST
2262 && TYPE_PRECISION (TREE_TYPE (primop0
)) < TYPE_PRECISION (*restype_ptr
))
2264 int min_gt
, max_gt
, min_lt
, max_lt
;
2265 tree maxval
, minval
;
2266 /* 1 if comparison is nominally unsigned. */
2267 int unsignedp
= TREE_UNSIGNED (*restype_ptr
);
2270 type
= signed_or_unsigned_type (unsignedp0
, TREE_TYPE (primop0
));
2272 maxval
= TYPE_MAX_VALUE (type
);
2273 minval
= TYPE_MIN_VALUE (type
);
2275 if (unsignedp
&& !unsignedp0
)
2276 *restype_ptr
= signed_type (*restype_ptr
);
2278 if (TREE_TYPE (primop1
) != *restype_ptr
)
2279 primop1
= convert (*restype_ptr
, primop1
);
2280 if (type
!= *restype_ptr
)
2282 minval
= convert (*restype_ptr
, minval
);
2283 maxval
= convert (*restype_ptr
, maxval
);
2286 if (unsignedp
&& unsignedp0
)
2288 min_gt
= INT_CST_LT_UNSIGNED (primop1
, minval
);
2289 max_gt
= INT_CST_LT_UNSIGNED (primop1
, maxval
);
2290 min_lt
= INT_CST_LT_UNSIGNED (minval
, primop1
);
2291 max_lt
= INT_CST_LT_UNSIGNED (maxval
, primop1
);
2295 min_gt
= INT_CST_LT (primop1
, minval
);
2296 max_gt
= INT_CST_LT (primop1
, maxval
);
2297 min_lt
= INT_CST_LT (minval
, primop1
);
2298 max_lt
= INT_CST_LT (maxval
, primop1
);
2302 /* This used to be a switch, but Genix compiler can't handle that. */
2303 if (code
== NE_EXPR
)
2305 if (max_lt
|| min_gt
)
2306 val
= boolean_true_node
;
2308 else if (code
== EQ_EXPR
)
2310 if (max_lt
|| min_gt
)
2311 val
= boolean_false_node
;
2313 else if (code
== LT_EXPR
)
2316 val
= boolean_true_node
;
2318 val
= boolean_false_node
;
2320 else if (code
== GT_EXPR
)
2323 val
= boolean_true_node
;
2325 val
= boolean_false_node
;
2327 else if (code
== LE_EXPR
)
2330 val
= boolean_true_node
;
2332 val
= boolean_false_node
;
2334 else if (code
== GE_EXPR
)
2337 val
= boolean_true_node
;
2339 val
= boolean_false_node
;
2342 /* If primop0 was sign-extended and unsigned comparison specd,
2343 we did a signed comparison above using the signed type bounds.
2344 But the comparison we output must be unsigned.
2346 Also, for inequalities, VAL is no good; but if the signed
2347 comparison had *any* fixed result, it follows that the
2348 unsigned comparison just tests the sign in reverse
2349 (positive values are LE, negative ones GE).
2350 So we can generate an unsigned comparison
2351 against an extreme value of the signed type. */
2353 if (unsignedp
&& !unsignedp0
)
2360 primop1
= TYPE_MIN_VALUE (type
);
2366 primop1
= TYPE_MAX_VALUE (type
);
2373 type
= unsigned_type (type
);
2376 if (!max_gt
&& !unsignedp0
&& TREE_CODE (primop0
) != INTEGER_CST
)
2378 /* This is the case of (char)x >?< 0x80, which people used to use
2379 expecting old C compilers to change the 0x80 into -0x80. */
2380 if (val
== boolean_false_node
)
2381 warning ("comparison is always 0 due to limited range of data type");
2382 if (val
== boolean_true_node
)
2383 warning ("comparison is always 1 due to limited range of data type");
2386 if (!min_lt
&& unsignedp0
&& TREE_CODE (primop0
) != INTEGER_CST
)
2388 /* This is the case of (unsigned char)x >?< -1 or < 0. */
2389 if (val
== boolean_false_node
)
2390 warning ("comparison is always 0 due to limited range of data type");
2391 if (val
== boolean_true_node
)
2392 warning ("comparison is always 1 due to limited range of data type");
2397 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2398 if (TREE_SIDE_EFFECTS (primop0
))
2399 return build (COMPOUND_EXPR
, TREE_TYPE (val
), primop0
, val
);
2403 /* Value is not predetermined, but do the comparison
2404 in the type of the operand that is not constant.
2405 TYPE is already properly set. */
2407 else if (real1
&& real2
2408 && (TYPE_PRECISION (TREE_TYPE (primop0
))
2409 == TYPE_PRECISION (TREE_TYPE (primop1
))))
2410 type
= TREE_TYPE (primop0
);
2412 /* If args' natural types are both narrower than nominal type
2413 and both extend in the same manner, compare them
2414 in the type of the wider arg.
2415 Otherwise must actually extend both to the nominal
2416 common type lest different ways of extending
2418 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
2420 else if (unsignedp0
== unsignedp1
&& real1
== real2
2421 && TYPE_PRECISION (TREE_TYPE (primop0
)) < TYPE_PRECISION (*restype_ptr
)
2422 && TYPE_PRECISION (TREE_TYPE (primop1
)) < TYPE_PRECISION (*restype_ptr
))
2424 type
= common_type (TREE_TYPE (primop0
), TREE_TYPE (primop1
));
2425 type
= signed_or_unsigned_type (unsignedp0
2426 || TREE_UNSIGNED (*restype_ptr
),
2428 /* Make sure shorter operand is extended the right way
2429 to match the longer operand. */
2430 primop0
= convert (signed_or_unsigned_type (unsignedp0
, TREE_TYPE (primop0
)),
2432 primop1
= convert (signed_or_unsigned_type (unsignedp1
, TREE_TYPE (primop1
)),
2437 /* Here we must do the comparison on the nominal type
2438 using the args exactly as we received them. */
2439 type
= *restype_ptr
;
2443 if (!real1
&& !real2
&& integer_zerop (primop1
)
2444 && TREE_UNSIGNED (*restype_ptr
))
2450 /* All unsigned values are >= 0, so we warn if extra warnings
2451 are requested. However, if OP0 is a constant that is
2452 >= 0, the signedness of the comparison isn't an issue,
2453 so suppress the warning. */
2455 && ! (TREE_CODE (primop0
) == INTEGER_CST
2456 && ! TREE_OVERFLOW (convert (signed_type (type
),
2458 warning ("unsigned value >= 0 is always 1");
2459 value
= boolean_true_node
;
2464 && ! (TREE_CODE (primop0
) == INTEGER_CST
2465 && ! TREE_OVERFLOW (convert (signed_type (type
),
2467 warning ("unsigned value < 0 is always 0");
2468 value
= boolean_false_node
;
2477 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2478 if (TREE_SIDE_EFFECTS (primop0
))
2479 return build (COMPOUND_EXPR
, TREE_TYPE (value
),
2486 *op0_ptr
= convert (type
, primop0
);
2487 *op1_ptr
= convert (type
, primop1
);
2489 *restype_ptr
= boolean_type_node
;
2494 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
2495 or validate its data type for an `if' or `while' statement or ?..: exp.
2497 This preparation consists of taking the ordinary
2498 representation of an expression expr and producing a valid tree
2499 boolean expression describing whether expr is nonzero. We could
2500 simply always do build_binary_op (NE_EXPR, expr, boolean_false_node, 1),
2501 but we optimize comparisons, &&, ||, and !.
2503 The resulting type should always be `boolean_type_node'. */
2506 truthvalue_conversion (expr
)
2509 if (TREE_CODE (expr
) == ERROR_MARK
)
2512 #if 0 /* This appears to be wrong for C++. */
2513 /* These really should return error_mark_node after 2.4 is stable.
2514 But not all callers handle ERROR_MARK properly. */
2515 switch (TREE_CODE (TREE_TYPE (expr
)))
2518 error ("struct type value used where scalar is required");
2519 return boolean_false_node
;
2522 error ("union type value used where scalar is required");
2523 return boolean_false_node
;
2526 error ("array type value used where scalar is required");
2527 return boolean_false_node
;
2534 switch (TREE_CODE (expr
))
2536 /* It is simpler and generates better code to have only TRUTH_*_EXPR
2537 or comparison expressions as truth values at this level. */
2540 /* A one-bit unsigned bit-field is already acceptable. */
2541 if (1 == TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (expr
, 1)))
2542 && TREE_UNSIGNED (TREE_OPERAND (expr
, 1)))
2548 /* It is simpler and generates better code to have only TRUTH_*_EXPR
2549 or comparison expressions as truth values at this level. */
2551 if (integer_zerop (TREE_OPERAND (expr
, 1)))
2552 return build_unary_op (TRUTH_NOT_EXPR
, TREE_OPERAND (expr
, 0), 0);
2554 case NE_EXPR
: case LE_EXPR
: case GE_EXPR
: case LT_EXPR
: case GT_EXPR
:
2555 case TRUTH_ANDIF_EXPR
:
2556 case TRUTH_ORIF_EXPR
:
2557 case TRUTH_AND_EXPR
:
2559 case TRUTH_XOR_EXPR
:
2560 case TRUTH_NOT_EXPR
:
2561 TREE_TYPE (expr
) = boolean_type_node
;
2568 return integer_zerop (expr
) ? boolean_false_node
: boolean_true_node
;
2571 return real_zerop (expr
) ? boolean_false_node
: boolean_true_node
;
2574 /* If we are taking the address of a external decl, it might be zero
2575 if it is weak, so we cannot optimize. */
2576 if (TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (expr
, 0))) == 'd'
2577 && DECL_EXTERNAL (TREE_OPERAND (expr
, 0)))
2580 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr
, 0)))
2581 return build (COMPOUND_EXPR
, boolean_type_node
,
2582 TREE_OPERAND (expr
, 0), boolean_true_node
);
2584 return boolean_true_node
;
2587 return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr
, 1))
2588 ? TRUTH_OR_EXPR
: TRUTH_ORIF_EXPR
),
2589 truthvalue_conversion (TREE_OPERAND (expr
, 0)),
2590 truthvalue_conversion (TREE_OPERAND (expr
, 1)),
2597 /* These don't change whether an object is non-zero or zero. */
2598 return truthvalue_conversion (TREE_OPERAND (expr
, 0));
2602 /* These don't change whether an object is zero or non-zero, but
2603 we can't ignore them if their second arg has side-effects. */
2604 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr
, 1)))
2605 return build (COMPOUND_EXPR
, boolean_type_node
, TREE_OPERAND (expr
, 1),
2606 truthvalue_conversion (TREE_OPERAND (expr
, 0)));
2608 return truthvalue_conversion (TREE_OPERAND (expr
, 0));
2611 /* Distribute the conversion into the arms of a COND_EXPR. */
2612 return fold (build (COND_EXPR
, boolean_type_node
, TREE_OPERAND (expr
, 0),
2613 truthvalue_conversion (TREE_OPERAND (expr
, 1)),
2614 truthvalue_conversion (TREE_OPERAND (expr
, 2))));
2617 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
2618 since that affects how `default_conversion' will behave. */
2619 if (TREE_CODE (TREE_TYPE (expr
)) == REFERENCE_TYPE
2620 || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0))) == REFERENCE_TYPE
)
2622 /* fall through... */
2624 /* If this is widening the argument, we can ignore it. */
2625 if (TYPE_PRECISION (TREE_TYPE (expr
))
2626 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr
, 0))))
2627 return truthvalue_conversion (TREE_OPERAND (expr
, 0));
2631 /* With IEEE arithmetic, x - x may not equal 0, so we can't optimize
2633 if (TARGET_FLOAT_FORMAT
== IEEE_FLOAT_FORMAT
2634 && TREE_CODE (TREE_TYPE (expr
)) == REAL_TYPE
)
2636 /* fall through... */
2638 /* This and MINUS_EXPR can be changed into a comparison of the
2640 if (TREE_TYPE (TREE_OPERAND (expr
, 0))
2641 == TREE_TYPE (TREE_OPERAND (expr
, 1)))
2642 return build_binary_op (NE_EXPR
, TREE_OPERAND (expr
, 0),
2643 TREE_OPERAND (expr
, 1), 1);
2644 return build_binary_op (NE_EXPR
, TREE_OPERAND (expr
, 0),
2645 fold (build1 (NOP_EXPR
,
2646 TREE_TYPE (TREE_OPERAND (expr
, 0)),
2647 TREE_OPERAND (expr
, 1))), 1);
2650 if (integer_onep (TREE_OPERAND (expr
, 1))
2651 && TREE_TYPE (expr
) != boolean_type_node
)
2652 /* Using convert here would cause infinite recursion. */
2653 return build1 (NOP_EXPR
, boolean_type_node
, expr
);
2657 if (warn_parentheses
&& C_EXP_ORIGINAL_CODE (expr
) == MODIFY_EXPR
)
2658 warning ("suggest parentheses around assignment used as truth value");
2665 if (TREE_CODE (TREE_TYPE (expr
)) == COMPLEX_TYPE
)
2666 return (build_binary_op
2667 ((TREE_SIDE_EFFECTS (expr
)
2668 ? TRUTH_OR_EXPR
: TRUTH_ORIF_EXPR
),
2669 truthvalue_conversion (build_unary_op (REALPART_EXPR
, expr
, 0)),
2670 truthvalue_conversion (build_unary_op (IMAGPART_EXPR
, expr
, 0)),
2673 return build_binary_op (NE_EXPR
, expr
, integer_zero_node
, 1);
2676 /* Read the rest of a #-directive from input stream FINPUT.
2677 In normal use, the directive name and the white space after it
2678 have already been read, so they won't be included in the result.
2679 We allow for the fact that the directive line may contain
2680 a newline embedded within a character or string literal which forms
2681 a part of the directive.
2683 The value is a string in a reusable buffer. It remains valid
2684 only until the next time this function is called.
2686 The terminating character ('\n' or EOF) is left in FINPUT for the
2687 caller to re-read. */
2690 get_directive_line (finput
)
2691 register FILE *finput
;
2693 static char *directive_buffer
= NULL
;
2694 static unsigned buffer_length
= 0;
2696 register char *buffer_limit
;
2697 register int looking_for
= 0;
2698 register int char_escaped
= 0;
2700 if (buffer_length
== 0)
2702 directive_buffer
= (char *)xmalloc (128);
2703 buffer_length
= 128;
2706 buffer_limit
= &directive_buffer
[buffer_length
];
2708 for (p
= directive_buffer
; ; )
2712 /* Make buffer bigger if it is full. */
2713 if (p
>= buffer_limit
)
2715 register unsigned bytes_used
= (p
- directive_buffer
);
2719 = (char *)xrealloc (directive_buffer
, buffer_length
);
2720 p
= &directive_buffer
[bytes_used
];
2721 buffer_limit
= &directive_buffer
[buffer_length
];
2726 /* Discard initial whitespace. */
2727 if ((c
== ' ' || c
== '\t') && p
== directive_buffer
)
2730 /* Detect the end of the directive. */
2731 if (looking_for
== 0
2732 && (c
== '\n' || c
== EOF
))
2741 return directive_buffer
;
2743 /* Handle string and character constant syntax. */
2746 if (looking_for
== c
&& !char_escaped
)
2747 looking_for
= 0; /* Found terminator... stop looking. */
2750 if (c
== '\'' || c
== '"')
2751 looking_for
= c
; /* Don't stop buffering until we see another
2752 another one of these (or an EOF). */
2754 /* Handle backslash. */
2755 char_escaped
= (c
== '\\' && ! char_escaped
);
2759 /* Make a variant type in the proper way for C/C++, propagating qualifiers
2760 down to the element type of an array. */
2763 c_build_type_variant (type
, constp
, volatilep
)
2765 int constp
, volatilep
;
2767 if (TREE_CODE (type
) == ARRAY_TYPE
)
2768 return build_array_type (c_build_type_variant (TREE_TYPE (type
),
2770 TYPE_DOMAIN (type
));
2771 return build_type_variant (type
, constp
, volatilep
);