1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992, 1993 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
29 extern struct obstack permanent_obstack
;
31 static void declare_hidden_char_array
PROTO((char *, char *));
33 /* Make bindings for __FUNCTION__ and __PRETTY_FUNCTION__. */
36 declare_function_name ()
38 char *name
, *printable_name
;
40 if (current_function_decl
== NULL
)
43 printable_name
= "top level";
47 char *kind
= "function";
48 if (TREE_CODE (TREE_TYPE (current_function_decl
)) == METHOD_TYPE
)
50 /* Allow functions to be nameless (such as artificial ones). */
51 if (DECL_NAME (current_function_decl
))
52 name
= IDENTIFIER_POINTER (DECL_NAME (current_function_decl
));
55 printable_name
= (*decl_printable_name
) (current_function_decl
, &kind
);
58 declare_hidden_char_array ("__FUNCTION__", name
);
59 declare_hidden_char_array ("__PRETTY_FUNCTION__", printable_name
);
63 declare_hidden_char_array (name
, value
)
66 tree decl
, type
, init
;
69 /* If the default size of char arrays isn't big enough for the name,
71 vlen
= strlen (value
) + 1;
72 type
= char_array_type_node
;
73 if (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TREE_TYPE (type
))) < vlen
)
74 type
= build_array_type (char_type_node
,
75 build_index_type (build_int_2 (vlen
, 0)));
77 push_obstacks_nochange ();
78 decl
= build_decl (VAR_DECL
, get_identifier (name
), type
);
79 TREE_STATIC (decl
) = 1;
80 TREE_READONLY (decl
) = 1;
81 TREE_ASM_WRITTEN (decl
) = 1;
82 DECL_SOURCE_LINE (decl
) = 0;
83 DECL_IN_SYSTEM_HEADER (decl
) = 1;
84 DECL_IGNORED_P (decl
) = 1;
85 init
= build_string (vlen
, value
);
86 TREE_TYPE (init
) = type
;
87 DECL_INITIAL (decl
) = init
;
88 finish_decl (pushdecl (decl
), init
, NULL_TREE
);
91 /* Given a chain of STRING_CST nodes,
92 concatenate them into one STRING_CST
93 and give it a suitable array-of-chars data type. */
96 combine_strings (strings
)
99 register tree value
, t
;
100 register int length
= 1;
103 int wchar_bytes
= TYPE_PRECISION (wchar_type_node
) / BITS_PER_UNIT
;
106 if (TREE_CHAIN (strings
))
108 /* More than one in the chain, so concatenate. */
109 register char *p
, *q
;
111 /* Don't include the \0 at the end of each substring,
112 except for the last one.
113 Count wide strings and ordinary strings separately. */
114 for (t
= strings
; t
; t
= TREE_CHAIN (t
))
116 if (TREE_TYPE (t
) == wchar_array_type_node
)
118 wide_length
+= (TREE_STRING_LENGTH (t
) - wchar_bytes
);
122 length
+= (TREE_STRING_LENGTH (t
) - 1);
125 /* If anything is wide, the non-wides will be converted,
126 which makes them take more space. */
128 length
= length
* wchar_bytes
+ wide_length
;
130 p
= savealloc (length
);
132 /* Copy the individual strings into the new combined string.
133 If the combined string is wide, convert the chars to ints
134 for any individual strings that are not wide. */
137 for (t
= strings
; t
; t
= TREE_CHAIN (t
))
139 int len
= (TREE_STRING_LENGTH (t
)
140 - ((TREE_TYPE (t
) == wchar_array_type_node
)
142 if ((TREE_TYPE (t
) == wchar_array_type_node
) == wide_flag
)
144 bcopy (TREE_STRING_POINTER (t
), q
, len
);
150 for (i
= 0; i
< len
; i
++)
151 ((int *) q
)[i
] = TREE_STRING_POINTER (t
)[i
];
152 q
+= len
* wchar_bytes
;
158 for (i
= 0; i
< wchar_bytes
; i
++)
164 value
= make_node (STRING_CST
);
165 TREE_STRING_POINTER (value
) = p
;
166 TREE_STRING_LENGTH (value
) = length
;
167 TREE_CONSTANT (value
) = 1;
172 length
= TREE_STRING_LENGTH (value
);
173 if (TREE_TYPE (value
) == wchar_array_type_node
)
177 /* Compute the number of elements, for the array type. */
178 nchars
= wide_flag
? length
/ wchar_bytes
: length
;
180 /* Create the array type for the string constant.
181 -Wwrite-strings says make the string constant an array of const char
182 so that copying it to a non-const pointer will get a warning. */
183 if (warn_write_strings
184 && (! flag_traditional
&& ! flag_writable_strings
))
187 = build_type_variant (wide_flag
? wchar_type_node
: char_type_node
,
190 = build_array_type (elements
,
191 build_index_type (build_int_2 (nchars
- 1, 0)));
195 = build_array_type (wide_flag
? wchar_type_node
: char_type_node
,
196 build_index_type (build_int_2 (nchars
- 1, 0)));
197 TREE_CONSTANT (value
) = 1;
198 TREE_STATIC (value
) = 1;
202 /* Process the attributes listed in ATTRIBUTES
203 and install them in DECL. */
206 decl_attributes (decl
, attributes
)
207 tree decl
, attributes
;
210 for (a
= attributes
; a
; a
= TREE_CHAIN (a
))
211 if (TREE_VALUE (a
) == get_identifier ("packed"))
213 if (TREE_CODE (decl
) == FIELD_DECL
)
214 DECL_PACKED (decl
) = 1;
215 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
216 used for DECL_REGISTER. It wouldn't mean anything anyway. */
218 else if (TREE_VALUE (a
) == get_identifier ("noreturn")
219 || TREE_VALUE (a
) == get_identifier ("volatile"))
221 if (TREE_CODE (decl
) == FUNCTION_DECL
)
222 TREE_THIS_VOLATILE (decl
) = 1;
224 else if (TREE_VALUE (a
) == get_identifier ("const"))
226 if (TREE_CODE (decl
) == FUNCTION_DECL
)
227 TREE_READONLY (decl
) = 1;
229 else if (TREE_VALUE (a
) != 0
230 && TREE_CODE (TREE_VALUE (a
)) == TREE_LIST
231 && TREE_PURPOSE (TREE_VALUE (a
)) == get_identifier ("mode"))
235 = IDENTIFIER_POINTER (TREE_VALUE (TREE_VALUE (a
)));
237 /* Give this decl a type with the specified mode. */
238 for (i
= 0; i
< NUM_MACHINE_MODES
; i
++)
239 if (!strcmp (specified_name
, GET_MODE_NAME (i
)))
242 = type_for_mode (i
, TREE_UNSIGNED (TREE_TYPE (decl
)));
245 TREE_TYPE (decl
) = type
;
246 DECL_SIZE (decl
) = 0;
247 layout_decl (decl
, 0);
250 error ("no data type for mode `%s'", specified_name
);
253 if (i
== NUM_MACHINE_MODES
)
254 error ("unknown machine mode `%s'", specified_name
);
256 else if (TREE_VALUE (a
) != 0
257 && TREE_CODE (TREE_VALUE (a
)) == TREE_LIST
258 && TREE_PURPOSE (TREE_VALUE (a
)) == get_identifier ("aligned"))
260 int align
= TREE_INT_CST_LOW (TREE_VALUE (TREE_VALUE (a
)))
263 if (exact_log2 (align
) == -1)
264 error_with_decl (decl
,
265 "requested alignment of `%s' is not a power of 2");
266 else if (TREE_CODE (decl
) != VAR_DECL
267 && TREE_CODE (decl
) != FIELD_DECL
)
268 error_with_decl (decl
,
269 "alignment specified for `%s'");
271 DECL_ALIGN (decl
) = align
;
273 else if (TREE_VALUE (a
) != 0
274 && TREE_CODE (TREE_VALUE (a
)) == TREE_LIST
275 && TREE_PURPOSE (TREE_VALUE (a
)) == get_identifier ("format"))
277 tree list
= TREE_VALUE (TREE_VALUE (a
));
278 tree format_type
= TREE_PURPOSE (list
);
279 int format_num
= TREE_INT_CST_LOW (TREE_PURPOSE (TREE_VALUE (list
)));
280 int first_arg_num
= TREE_INT_CST_LOW (TREE_VALUE (TREE_VALUE (list
)));
285 if (TREE_CODE (decl
) != FUNCTION_DECL
)
287 error_with_decl (decl
,
288 "argument format specified for non-function `%s'");
292 if (format_type
== get_identifier ("printf"))
294 else if (format_type
== get_identifier ("scanf"))
298 error_with_decl (decl
, "unrecognized format specifier for `%s'");
302 if (first_arg_num
!= 0 && first_arg_num
<= format_num
)
304 error_with_decl (decl
,
305 "format string arg follows the args to be formatted, for `%s'");
309 /* Verify that the format_num argument is actually a string, in case
310 the format attribute is in error. */
311 argument
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
312 for (arg_num
= 1; ; ++arg_num
)
314 if (argument
== 0 || arg_num
== format_num
)
316 argument
= TREE_CHAIN (argument
);
319 || TREE_CODE (TREE_VALUE (argument
)) != POINTER_TYPE
320 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument
)))
323 error_with_decl (decl
,
324 "format string arg not a string type, for `%s'");
327 if (first_arg_num
!= 0)
329 /* Verify that first_arg_num points to the last arg, the ... */
331 arg_num
++, argument
= TREE_CHAIN (argument
);
332 if (arg_num
!= first_arg_num
)
334 error_with_decl (decl
,
335 "args to be formatted is not ..., for `%s'");
340 record_function_format (DECL_NAME (decl
), DECL_ASSEMBLER_NAME (decl
),
341 is_scan
, format_num
, first_arg_num
);
345 /* Check a printf/fprintf/sprintf/scanf/fscanf/sscanf format against
348 #define T_I &integer_type_node
349 #define T_L &long_integer_type_node
350 #define T_S &short_integer_type_node
351 #define T_UI &unsigned_type_node
352 #define T_UL &long_unsigned_type_node
353 #define T_US &short_unsigned_type_node
354 #define T_F &float_type_node
355 #define T_D &double_type_node
356 #define T_LD &long_double_type_node
357 #define T_C &char_type_node
358 #define T_V &void_type_node
359 #define T_W &wchar_type_node
364 /* Type of argument if no length modifier is used. */
366 /* Type of argument if length modifier for shortening is used.
367 If NULL, then this modifier is not allowed. */
369 /* Type of argument if length modifier `l' is used.
370 If NULL, then this modifier is not allowed. */
372 /* Type of argument if length modifier `L' is used.
373 If NULL, then this modifier is not allowed. */
375 /* List of other modifier characters allowed with these options. */
379 static format_char_info print_char_table
[] = {
380 { "di", 0, T_I
, T_I
, T_L
, NULL
, "-wp0 +" },
381 { "oxX", 0, T_UI
, T_UI
, T_UL
, NULL
, "-wp0#" },
382 { "u", 0, T_UI
, T_UI
, T_UL
, NULL
, "-wp0" },
383 { "feEgG", 0, T_D
, NULL
, NULL
, T_LD
, "-wp0 +#" },
384 { "c", 0, T_I
, NULL
, T_W
, NULL
, "-w" },
385 { "C", 0, T_W
, NULL
, NULL
, NULL
, "-w" },
386 { "s", 1, T_C
, NULL
, T_W
, NULL
, "-wp" },
387 { "S", 1, T_W
, NULL
, NULL
, NULL
, "-wp" },
388 { "p", 1, T_V
, NULL
, NULL
, NULL
, "-w" },
389 { "n", 1, T_I
, T_S
, T_L
, NULL
, "" },
393 static format_char_info scan_char_table
[] = {
394 { "di", 1, T_I
, T_S
, T_L
, NULL
, "*" },
395 { "ouxX", 1, T_UI
, T_US
, T_UL
, NULL
, "*" },
396 { "efgEG", 1, T_F
, NULL
, T_D
, T_LD
, "*" },
397 { "sc", 1, T_C
, NULL
, T_W
, NULL
, "*" },
398 { "[", 1, T_C
, NULL
, NULL
, NULL
, "*" },
399 { "C", 1, T_W
, NULL
, NULL
, NULL
, "*" },
400 { "S", 1, T_W
, NULL
, NULL
, NULL
, "*" },
401 { "p", 2, T_V
, NULL
, NULL
, NULL
, "*" },
402 { "n", 1, T_I
, T_S
, T_L
, NULL
, "" },
406 typedef struct function_format_info
{
407 struct function_format_info
*next
; /* next structure on the list */
408 tree name
; /* identifier such as "printf" */
409 tree assembler_name
; /* optional mangled identifier (for C++) */
410 int is_scan
; /* TRUE if *scanf */
411 int format_num
; /* number of format argument */
412 int first_arg_num
; /* number of first arg (zero for varargs) */
413 } function_format_info
;
415 static function_format_info
*function_format_list
= NULL
;
417 static void check_format_info
PROTO((function_format_info
*, tree
));
419 /* Initialize the table of functions to perform format checking on.
420 The ANSI functions are always checked (whether <stdio.h> is
421 included or not), since it is common to call printf without
422 including <stdio.h>. There shouldn't be a problem with this,
423 since ANSI reserves these function names whether you include the
424 header file or not. In any case, the checking is harmless. */
427 init_function_format_info ()
429 record_function_format (get_identifier ("printf"), NULL_TREE
, 0, 1, 2);
430 record_function_format (get_identifier ("fprintf"), NULL_TREE
, 0, 2, 3);
431 record_function_format (get_identifier ("sprintf"), NULL_TREE
, 0, 2, 3);
432 record_function_format (get_identifier ("scanf"), NULL_TREE
, 1, 1, 2);
433 record_function_format (get_identifier ("fscanf"), NULL_TREE
, 1, 2, 3);
434 record_function_format (get_identifier ("sscanf"), NULL_TREE
, 1, 2, 3);
435 record_function_format (get_identifier ("vprintf"), NULL_TREE
, 0, 1, 0);
436 record_function_format (get_identifier ("vfprintf"), NULL_TREE
, 0, 2, 0);
437 record_function_format (get_identifier ("vsprintf"), NULL_TREE
, 0, 2, 0);
440 /* Record information for argument format checking. FUNCTION_IDENT is
441 the identifier node for the name of the function to check (its decl
442 need not exist yet). IS_SCAN is true for scanf-type format checking;
443 false indicates printf-style format checking. FORMAT_NUM is the number
444 of the argument which is the format control string (starting from 1).
445 FIRST_ARG_NUM is the number of the first actual argument to check
446 against teh format string, or zero if no checking is not be done
447 (e.g. for varargs such as vfprintf). */
450 record_function_format (name
, assembler_name
, is_scan
,
451 format_num
, first_arg_num
)
458 function_format_info
*info
;
460 /* Re-use existing structure if it's there. */
462 for (info
= function_format_list
; info
; info
= info
->next
)
464 if (info
->name
== name
&& info
->assembler_name
== assembler_name
)
469 info
= (function_format_info
*) xmalloc (sizeof (function_format_info
));
470 info
->next
= function_format_list
;
471 function_format_list
= info
;
474 info
->assembler_name
= assembler_name
;
477 info
->is_scan
= is_scan
;
478 info
->format_num
= format_num
;
479 info
->first_arg_num
= first_arg_num
;
482 static char tfaff
[] = "too few arguments for format";
484 /* Check the argument list of a call to printf, scanf, etc.
485 NAME is the function identifier.
486 ASSEMBLER_NAME is the function's assembler identifier.
487 (Either NAME or ASSEMBLER_NAME, but not both, may be NULL_TREE.)
488 PARAMS is the list of argument values. */
491 check_function_format (name
, assembler_name
, params
)
496 function_format_info
*info
;
498 /* See if this function is a format function. */
499 for (info
= function_format_list
; info
; info
= info
->next
)
501 if (info
->assembler_name
502 ? (info
->assembler_name
== assembler_name
)
503 : (info
->name
== name
))
506 check_format_info (info
, params
);
512 /* Check the argument list of a call to printf, scanf, etc.
513 INFO points to the function_format_info structure.
514 PARAMS is the list of argument values. */
517 check_format_info (info
, params
)
518 function_format_info
*info
;
523 int suppressed
, wide
, precise
;
531 tree first_fillin_param
;
533 format_char_info
*fci
;
534 static char message
[132];
536 int has_operand_number
= 0;
538 /* Skip to format argument. If the argument isn't available, there's
539 no work for us to do; prototype checking will catch the problem. */
540 for (arg_num
= 1; ; ++arg_num
)
544 if (arg_num
== info
->format_num
)
546 params
= TREE_CHAIN (params
);
548 format_tree
= TREE_VALUE (params
);
549 params
= TREE_CHAIN (params
);
550 if (format_tree
== 0)
552 /* We can only check the format if it's a string constant. */
553 while (TREE_CODE (format_tree
) == NOP_EXPR
)
554 format_tree
= TREE_OPERAND (format_tree
, 0); /* strip coercion */
555 if (format_tree
== null_pointer_node
)
557 warning ("null format string");
560 if (TREE_CODE (format_tree
) != ADDR_EXPR
)
562 format_tree
= TREE_OPERAND (format_tree
, 0);
563 if (TREE_CODE (format_tree
) != STRING_CST
)
565 format_chars
= TREE_STRING_POINTER (format_tree
);
566 format_length
= TREE_STRING_LENGTH (format_tree
);
567 if (format_length
<= 1)
568 warning ("zero-length format string");
569 if (format_chars
[--format_length
] != 0)
571 warning ("unterminated format string");
574 /* Skip to first argument to check. */
575 while (arg_num
+ 1 < info
->first_arg_num
)
579 params
= TREE_CHAIN (params
);
583 first_fillin_param
= params
;
586 if (*format_chars
== 0)
588 if (format_chars
- TREE_STRING_POINTER (format_tree
) != format_length
)
589 warning ("embedded `\\0' in format");
590 if (info
->first_arg_num
!= 0 && params
!= 0 && ! has_operand_number
)
591 warning ("too many arguments for format");
594 if (*format_chars
++ != '%')
596 if (*format_chars
== 0)
598 warning ("spurious trailing `%%' in format");
601 if (*format_chars
== '%')
607 suppressed
= wide
= precise
= FALSE
;
610 suppressed
= *format_chars
== '*';
613 while (isdigit (*format_chars
))
618 /* See if we have a number followed by a dollar sign. If we do,
619 it is an operand number, so set PARAMS to that operand. */
620 if (*format_chars
>= '0' && *format_chars
<= '9')
622 char *p
= format_chars
;
624 while (*p
>= '0' && *p
++ <= '9')
629 int opnum
= atoi (format_chars
);
631 params
= first_fillin_param
;
632 format_chars
= p
+ 1;
633 has_operand_number
= 1;
635 for (i
= 1; i
< opnum
&& params
!= 0; i
++)
636 params
= TREE_CHAIN (params
);
638 if (opnum
== 0 || params
== 0)
640 warning ("operand number out of range in format");
646 while (*format_chars
!= 0 && index (" +#0-", *format_chars
) != 0)
648 if (index (flag_chars
, *format_chars
) != 0)
650 sprintf (message
, "repeated `%c' flag in format",
654 i
= strlen (flag_chars
);
655 flag_chars
[i
++] = *format_chars
++;
658 /* "If the space and + flags both appear,
659 the space flag will be ignored." */
660 if (index (flag_chars
, ' ') != 0
661 && index (flag_chars
, '+') != 0)
662 warning ("use of both ` ' and `+' flags in format");
663 /* "If the 0 and - flags both appear,
664 the 0 flag will be ignored." */
665 if (index (flag_chars
, '0') != 0
666 && index (flag_chars
, '-') != 0)
667 warning ("use of both `0' and `-' flags in format");
668 if (*format_chars
== '*')
671 /* "...a field width...may be indicated by an asterisk.
672 In this case, an int argument supplies the field width..." */
679 if (info
->first_arg_num
!= 0)
681 cur_param
= TREE_VALUE (params
);
682 params
= TREE_CHAIN (params
);
684 /* size_t is generally not valid here.
685 It will work on most machines, because size_t and int
686 have the same mode. But might as well warn anyway,
687 since it will fail on other machines. */
688 if ((TYPE_MAIN_VARIANT (TREE_TYPE (cur_param
))
689 != integer_type_node
)
691 (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param
))
692 != unsigned_type_node
))
695 "field width is not type int (arg %d)",
703 while (isdigit (*format_chars
))
709 if (*format_chars
== '.')
713 if (*format_chars
!= '*' && !isdigit (*format_chars
))
714 warning ("`.' not followed by `*' or digit in format");
715 /* "...a...precision...may be indicated by an asterisk.
716 In this case, an int argument supplies the...precision." */
717 if (*format_chars
== '*')
719 if (info
->first_arg_num
!= 0)
727 cur_param
= TREE_VALUE (params
);
728 params
= TREE_CHAIN (params
);
730 if (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param
))
731 != integer_type_node
)
734 "field width is not type int (arg %d)",
742 while (isdigit (*format_chars
))
747 if (*format_chars
== 'h' || *format_chars
== 'l' || *format_chars
== 'L')
748 length_char
= *format_chars
++;
751 if (suppressed
&& length_char
!= 0)
754 "use of `*' and `%c' together in format",
758 format_char
= *format_chars
;
759 if (format_char
== 0)
761 warning ("conversion lacks type at end of format");
765 fci
= info
->is_scan
? scan_char_table
: print_char_table
;
766 while (fci
->format_chars
!= 0
767 && index (fci
->format_chars
, format_char
) == 0)
769 if (fci
->format_chars
== 0)
771 if (format_char
>= 040 && format_char
< 0177)
773 "unknown conversion type character `%c' in format",
777 "unknown conversion type character 0x%x in format",
782 if (wide
&& index (fci
->flag_chars
, 'w') == 0)
784 sprintf (message
, "width used with `%c' format",
788 if (precise
&& index (fci
->flag_chars
, 'p') == 0)
790 sprintf (message
, "precision used with `%c' format",
794 if (info
->is_scan
&& format_char
== '[')
796 /* Skip over scan set, in case it happens to have '%' in it. */
797 if (*format_chars
== '^')
799 /* Find closing bracket; if one is hit immediately, then
800 it's part of the scan set rather than a terminator. */
801 if (*format_chars
== ']')
803 while (*format_chars
&& *format_chars
!= ']')
805 if (*format_chars
!= ']')
806 /* The end of the format string was reached. */
807 warning ("no closing `]' for `%%[' format");
811 if (index (fci
->flag_chars
, '*') == 0)
814 "suppression of `%c' conversion in format",
820 for (i
= 0; flag_chars
[i
] != 0; ++i
)
822 if (index (fci
->flag_chars
, flag_chars
[i
]) == 0)
824 sprintf (message
, "flag `%c' used with type `%c'",
825 flag_chars
[i
], format_char
);
829 if (precise
&& index (flag_chars
, '0') != 0
830 && (format_char
== 'd' || format_char
== 'i'
831 || format_char
== 'o' || format_char
== 'u'
832 || format_char
== 'x' || format_char
== 'x'))
835 "precision and `0' flag not both allowed with `%c' format",
841 default: wanted_type
= fci
->nolen
? *(fci
->nolen
) : 0; break;
842 case 'h': wanted_type
= fci
->hlen
? *(fci
->hlen
) : 0; break;
843 case 'l': wanted_type
= fci
->llen
? *(fci
->llen
) : 0; break;
844 case 'L': wanted_type
= fci
->bigllen
? *(fci
->bigllen
) : 0; break;
846 if (wanted_type
== 0)
849 "use of `%c' length character with `%c' type character",
850 length_char
, format_char
);
855 ** XXX -- should kvetch about stuff such as
863 /* Finally. . .check type of argument against desired type! */
864 if (info
->first_arg_num
== 0)
871 cur_param
= TREE_VALUE (params
);
872 params
= TREE_CHAIN (params
);
874 cur_type
= TREE_TYPE (cur_param
);
876 /* Check the types of any additional pointer arguments
877 that precede the "real" argument. */
878 for (i
= 0; i
< fci
->pointer_count
; ++i
)
880 if (TREE_CODE (cur_type
) == POINTER_TYPE
)
882 cur_type
= TREE_TYPE (cur_type
);
886 "format argument is not a %s (arg %d)",
887 ((fci
->pointer_count
== 1) ? "pointer" : "pointer to a pointer"),
893 /* Check the type of the "real" argument, if there's a type we want. */
894 if (i
== fci
->pointer_count
&& wanted_type
!= 0
895 && wanted_type
!= TYPE_MAIN_VARIANT (cur_type
)
896 /* If we want `void *', allow any pointer type.
897 (Anything else would already have got a warning.) */
898 && ! (wanted_type
== void_type_node
899 && fci
->pointer_count
> 0)
900 /* Don't warn about differences merely in signedness. */
901 && !(TREE_CODE (wanted_type
) == INTEGER_TYPE
902 && TREE_CODE (cur_type
) == INTEGER_TYPE
903 && (TREE_UNSIGNED (wanted_type
)
904 ? wanted_type
== unsigned_type (cur_type
)
905 : wanted_type
== signed_type (cur_type
)))
906 /* Likewise, "signed char", "unsigned char" and "char" are
907 equivalent but the above test won't consider them equivalent. */
908 && ! (wanted_type
== char_type_node
909 && (cur_type
== signed_char_type_node
910 || cur_type
== unsigned_char_type_node
)))
915 this = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (wanted_type
)));
917 if (TREE_CODE (cur_type
) != ERROR_MARK
918 && TYPE_NAME (cur_type
) != 0
919 && TREE_CODE (cur_type
) != INTEGER_TYPE
920 && !(TREE_CODE (cur_type
) == POINTER_TYPE
921 && TREE_CODE (TREE_TYPE (cur_type
)) == INTEGER_TYPE
))
923 if (TREE_CODE (TYPE_NAME (cur_type
)) == TYPE_DECL
924 && DECL_NAME (TYPE_NAME (cur_type
)) != 0)
925 that
= IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type
)));
927 that
= IDENTIFIER_POINTER (TYPE_NAME (cur_type
));
930 /* A nameless type can't possibly match what the format wants.
931 So there will be a warning for it.
932 Make up a string to describe vaguely what it is. */
935 if (TREE_CODE (cur_type
) == POINTER_TYPE
)
938 that
= "different type";
941 /* Make the warning better in case of mismatch of int vs long. */
942 if (TREE_CODE (cur_type
) == INTEGER_TYPE
943 && TREE_CODE (wanted_type
) == INTEGER_TYPE
944 && TYPE_PRECISION (cur_type
) == TYPE_PRECISION (wanted_type
)
945 && TYPE_NAME (cur_type
) != 0
946 && TREE_CODE (TYPE_NAME (cur_type
)) == TYPE_DECL
)
947 that
= IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type
)));
949 if (strcmp (this, that
) != 0)
951 sprintf (message
, "%s format, %s arg (arg %d)",
952 this, that
, arg_num
);
959 /* Print a warning if a constant expression had overflow in folding.
960 Invoke this function on every expression that the language
961 requires to be a constant expression.
962 Note the ANSI C standard says it is erroneous for a
963 constant expression to overflow. */
966 constant_expression_warning (value
)
969 if (TREE_CODE (value
) == INTEGER_CST
&& TREE_CONSTANT_OVERFLOW (value
))
971 pedwarn ("overflow in constant expression");
974 /* Print a warning if an expression had overflow in folding.
975 Invoke this function on every expression that
976 (1) appears in the source code, and
977 (2) might be a constant expression that overflowed, and
978 (3) is not already checked by convert_and_check;
979 however, do not invoke this function on operands of explicit casts. */
982 overflow_warning (value
)
985 if (TREE_CODE (value
) == INTEGER_CST
&& TREE_OVERFLOW (value
))
987 TREE_OVERFLOW (value
) = 0;
988 warning ("integer overflow in expression");
992 /* Print a warning if a large constant is truncated to unsigned,
993 or if -Wconversion is used and a constant < 0 is converted to unsigned.
994 Invoke this function on every expression that might be implicitly
995 converted to an unsigned type. */
998 unsigned_conversion_warning (result
, operand
)
999 tree result
, operand
;
1001 if (TREE_CODE (operand
) == INTEGER_CST
1002 && TREE_CODE (TREE_TYPE (result
)) == INTEGER_TYPE
1003 && TREE_UNSIGNED (TREE_TYPE (result
))
1004 && !int_fits_type_p (operand
, TREE_TYPE (result
)))
1006 if (!int_fits_type_p (operand
, signed_type (TREE_TYPE (result
))))
1007 /* This detects cases like converting -129 or 256 to unsigned char. */
1008 warning ("large integer implicitly truncated to unsigned type");
1009 else if (warn_conversion
)
1010 warning ("negative integer implicitly converted to unsigned type");
1014 /* Convert EXPR to TYPE, warning about conversion problems with constants.
1015 Invoke this function on every expression that is converted implicitly,
1016 i.e. because of language rules and not because of an explicit cast. */
1019 convert_and_check (type
, expr
)
1022 tree t
= convert (type
, expr
);
1023 if (TREE_CODE (t
) == INTEGER_CST
)
1025 if (TREE_OVERFLOW (t
))
1027 TREE_OVERFLOW (t
) = 0;
1029 /* No warning for converting 0x80000000 to int. */
1030 if (!(TREE_UNSIGNED (type
) < TREE_UNSIGNED (TREE_TYPE (expr
))
1031 && TREE_CODE (TREE_TYPE (expr
)) == INTEGER_TYPE
1032 && TYPE_PRECISION (type
) == TYPE_PRECISION (TREE_TYPE (expr
))))
1033 /* If EXPR fits in the unsigned version of TYPE,
1034 don't warn unless pedantic. */
1036 || TREE_UNSIGNED (type
)
1037 || ! int_fits_type_p (expr
, unsigned_type (type
)))
1038 warning ("overflow in implicit constant conversion");
1041 unsigned_conversion_warning (t
, expr
);
1047 c_expand_expr_stmt (expr
)
1050 /* Do default conversion if safe and possibly important,
1051 in case within ({...}). */
1052 if ((TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
&& lvalue_p (expr
))
1053 || TREE_CODE (TREE_TYPE (expr
)) == FUNCTION_TYPE
)
1054 expr
= default_conversion (expr
);
1056 if (TREE_TYPE (expr
) != error_mark_node
1057 && TYPE_SIZE (TREE_TYPE (expr
)) == 0
1058 && TREE_CODE (TREE_TYPE (expr
)) != ARRAY_TYPE
)
1059 error ("expression statement has incomplete type");
1061 expand_expr_stmt (expr
);
1064 /* Validate the expression after `case' and apply default promotions. */
1067 check_case_value (value
)
1070 if (value
== NULL_TREE
)
1073 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1074 STRIP_TYPE_NOPS (value
);
1076 if (TREE_CODE (value
) != INTEGER_CST
1077 && value
!= error_mark_node
)
1079 error ("case label does not reduce to an integer constant");
1080 value
= error_mark_node
;
1083 /* Promote char or short to int. */
1084 value
= default_conversion (value
);
1086 constant_expression_warning (value
);
1091 /* Return an integer type with BITS bits of precision,
1092 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
1095 type_for_size (bits
, unsignedp
)
1099 if (bits
== TYPE_PRECISION (signed_char_type_node
))
1100 return unsignedp
? unsigned_char_type_node
: signed_char_type_node
;
1102 if (bits
== TYPE_PRECISION (short_integer_type_node
))
1103 return unsignedp
? short_unsigned_type_node
: short_integer_type_node
;
1105 if (bits
== TYPE_PRECISION (integer_type_node
))
1106 return unsignedp
? unsigned_type_node
: integer_type_node
;
1108 if (bits
== TYPE_PRECISION (long_integer_type_node
))
1109 return unsignedp
? long_unsigned_type_node
: long_integer_type_node
;
1111 if (bits
== TYPE_PRECISION (long_long_integer_type_node
))
1112 return (unsignedp
? long_long_unsigned_type_node
1113 : long_long_integer_type_node
);
1115 if (bits
<= TYPE_PRECISION (intQI_type_node
))
1116 return unsignedp
? unsigned_intQI_type_node
: intQI_type_node
;
1118 if (bits
<= TYPE_PRECISION (intHI_type_node
))
1119 return unsignedp
? unsigned_intHI_type_node
: intHI_type_node
;
1121 if (bits
<= TYPE_PRECISION (intSI_type_node
))
1122 return unsignedp
? unsigned_intSI_type_node
: intSI_type_node
;
1124 if (bits
<= TYPE_PRECISION (intDI_type_node
))
1125 return unsignedp
? unsigned_intDI_type_node
: intDI_type_node
;
1130 /* Return a data type that has machine mode MODE.
1131 If the mode is an integer,
1132 then UNSIGNEDP selects between signed and unsigned types. */
1135 type_for_mode (mode
, unsignedp
)
1136 enum machine_mode mode
;
1139 if (mode
== TYPE_MODE (signed_char_type_node
))
1140 return unsignedp
? unsigned_char_type_node
: signed_char_type_node
;
1142 if (mode
== TYPE_MODE (short_integer_type_node
))
1143 return unsignedp
? short_unsigned_type_node
: short_integer_type_node
;
1145 if (mode
== TYPE_MODE (integer_type_node
))
1146 return unsignedp
? unsigned_type_node
: integer_type_node
;
1148 if (mode
== TYPE_MODE (long_integer_type_node
))
1149 return unsignedp
? long_unsigned_type_node
: long_integer_type_node
;
1151 if (mode
== TYPE_MODE (long_long_integer_type_node
))
1152 return unsignedp
? long_long_unsigned_type_node
: long_long_integer_type_node
;
1154 if (mode
== TYPE_MODE (intQI_type_node
))
1155 return unsignedp
? unsigned_intQI_type_node
: intQI_type_node
;
1157 if (mode
== TYPE_MODE (intHI_type_node
))
1158 return unsignedp
? unsigned_intHI_type_node
: intHI_type_node
;
1160 if (mode
== TYPE_MODE (intSI_type_node
))
1161 return unsignedp
? unsigned_intSI_type_node
: intSI_type_node
;
1163 if (mode
== TYPE_MODE (intDI_type_node
))
1164 return unsignedp
? unsigned_intDI_type_node
: intDI_type_node
;
1166 if (mode
== TYPE_MODE (float_type_node
))
1167 return float_type_node
;
1169 if (mode
== TYPE_MODE (double_type_node
))
1170 return double_type_node
;
1172 if (mode
== TYPE_MODE (long_double_type_node
))
1173 return long_double_type_node
;
1175 if (mode
== TYPE_MODE (build_pointer_type (char_type_node
)))
1176 return build_pointer_type (char_type_node
);
1178 if (mode
== TYPE_MODE (build_pointer_type (integer_type_node
)))
1179 return build_pointer_type (integer_type_node
);
1184 /* Print an error message for invalid operands to arith operation CODE.
1185 NOP_EXPR is used as a special case (see truthvalue_conversion). */
1188 binary_op_error (code
)
1189 enum tree_code code
;
1191 register char *opname
;
1195 error ("invalid truth-value expression");
1199 opname
= "+"; break;
1201 opname
= "-"; break;
1203 opname
= "*"; break;
1205 opname
= "max"; break;
1207 opname
= "min"; break;
1209 opname
= "=="; break;
1211 opname
= "!="; break;
1213 opname
= "<="; break;
1215 opname
= ">="; break;
1217 opname
= "<"; break;
1219 opname
= ">"; break;
1221 opname
= "<<"; break;
1223 opname
= ">>"; break;
1224 case TRUNC_MOD_EXPR
:
1225 case FLOOR_MOD_EXPR
:
1226 opname
= "%"; break;
1227 case TRUNC_DIV_EXPR
:
1228 case FLOOR_DIV_EXPR
:
1229 opname
= "/"; break;
1231 opname
= "&"; break;
1233 opname
= "|"; break;
1234 case TRUTH_ANDIF_EXPR
:
1235 opname
= "&&"; break;
1236 case TRUTH_ORIF_EXPR
:
1237 opname
= "||"; break;
1239 opname
= "^"; break;
1242 opname
= "rotate"; break;
1244 error ("invalid operands to binary %s", opname
);
1247 /* Subroutine of build_binary_op, used for comparison operations.
1248 See if the operands have both been converted from subword integer types
1249 and, if so, perhaps change them both back to their original type.
1250 This function is also responsible for converting the two operands
1251 to the proper common type for comparison.
1253 The arguments of this function are all pointers to local variables
1254 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
1255 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
1257 If this function returns nonzero, it means that the comparison has
1258 a constant value. What this function returns is an expression for
1262 shorten_compare (op0_ptr
, op1_ptr
, restype_ptr
, rescode_ptr
)
1263 tree
*op0_ptr
, *op1_ptr
;
1265 enum tree_code
*rescode_ptr
;
1268 tree op0
= *op0_ptr
;
1269 tree op1
= *op1_ptr
;
1270 int unsignedp0
, unsignedp1
;
1272 tree primop0
, primop1
;
1273 enum tree_code code
= *rescode_ptr
;
1275 /* Throw away any conversions to wider types
1276 already present in the operands. */
1278 primop0
= get_narrower (op0
, &unsignedp0
);
1279 primop1
= get_narrower (op1
, &unsignedp1
);
1281 /* Handle the case that OP0 does not *contain* a conversion
1282 but it *requires* conversion to FINAL_TYPE. */
1284 if (op0
== primop0
&& TREE_TYPE (op0
) != *restype_ptr
)
1285 unsignedp0
= TREE_UNSIGNED (TREE_TYPE (op0
));
1286 if (op1
== primop1
&& TREE_TYPE (op1
) != *restype_ptr
)
1287 unsignedp1
= TREE_UNSIGNED (TREE_TYPE (op1
));
1289 /* If one of the operands must be floated, we cannot optimize. */
1290 real1
= TREE_CODE (TREE_TYPE (primop0
)) == REAL_TYPE
;
1291 real2
= TREE_CODE (TREE_TYPE (primop1
)) == REAL_TYPE
;
1293 /* If first arg is constant, swap the args (changing operation
1294 so value is preserved), for canonicalization. */
1296 if (TREE_CONSTANT (primop0
))
1298 register tree tem
= primop0
;
1299 register int temi
= unsignedp0
;
1307 unsignedp0
= unsignedp1
;
1328 *rescode_ptr
= code
;
1331 /* If comparing an integer against a constant more bits wide,
1332 maybe we can deduce a value of 1 or 0 independent of the data.
1333 Or else truncate the constant now
1334 rather than extend the variable at run time.
1336 This is only interesting if the constant is the wider arg.
1337 Also, it is not safe if the constant is unsigned and the
1338 variable arg is signed, since in this case the variable
1339 would be sign-extended and then regarded as unsigned.
1340 Our technique fails in this case because the lowest/highest
1341 possible unsigned results don't follow naturally from the
1342 lowest/highest possible values of the variable operand.
1343 For just EQ_EXPR and NE_EXPR there is another technique that
1344 could be used: see if the constant can be faithfully represented
1345 in the other operand's type, by truncating it and reextending it
1346 and see if that preserves the constant's value. */
1348 if (!real1
&& !real2
1349 && TREE_CODE (primop1
) == INTEGER_CST
1350 && TYPE_PRECISION (TREE_TYPE (primop0
)) < TYPE_PRECISION (*restype_ptr
))
1352 int min_gt
, max_gt
, min_lt
, max_lt
;
1353 tree maxval
, minval
;
1354 /* 1 if comparison is nominally unsigned. */
1355 int unsignedp
= TREE_UNSIGNED (*restype_ptr
);
1358 type
= signed_or_unsigned_type (unsignedp0
, TREE_TYPE (primop0
));
1360 maxval
= TYPE_MAX_VALUE (type
);
1361 minval
= TYPE_MIN_VALUE (type
);
1363 if (unsignedp
&& !unsignedp0
)
1364 *restype_ptr
= signed_type (*restype_ptr
);
1366 if (TREE_TYPE (primop1
) != *restype_ptr
)
1367 primop1
= convert (*restype_ptr
, primop1
);
1368 if (type
!= *restype_ptr
)
1370 minval
= convert (*restype_ptr
, minval
);
1371 maxval
= convert (*restype_ptr
, maxval
);
1374 if (unsignedp
&& unsignedp0
)
1376 min_gt
= INT_CST_LT_UNSIGNED (primop1
, minval
);
1377 max_gt
= INT_CST_LT_UNSIGNED (primop1
, maxval
);
1378 min_lt
= INT_CST_LT_UNSIGNED (minval
, primop1
);
1379 max_lt
= INT_CST_LT_UNSIGNED (maxval
, primop1
);
1383 min_gt
= INT_CST_LT (primop1
, minval
);
1384 max_gt
= INT_CST_LT (primop1
, maxval
);
1385 min_lt
= INT_CST_LT (minval
, primop1
);
1386 max_lt
= INT_CST_LT (maxval
, primop1
);
1390 /* This used to be a switch, but Genix compiler can't handle that. */
1391 if (code
== NE_EXPR
)
1393 if (max_lt
|| min_gt
)
1394 val
= integer_one_node
;
1396 else if (code
== EQ_EXPR
)
1398 if (max_lt
|| min_gt
)
1399 val
= integer_zero_node
;
1401 else if (code
== LT_EXPR
)
1404 val
= integer_one_node
;
1406 val
= integer_zero_node
;
1408 else if (code
== GT_EXPR
)
1411 val
= integer_one_node
;
1413 val
= integer_zero_node
;
1415 else if (code
== LE_EXPR
)
1418 val
= integer_one_node
;
1420 val
= integer_zero_node
;
1422 else if (code
== GE_EXPR
)
1425 val
= integer_one_node
;
1427 val
= integer_zero_node
;
1430 /* If primop0 was sign-extended and unsigned comparison specd,
1431 we did a signed comparison above using the signed type bounds.
1432 But the comparison we output must be unsigned.
1434 Also, for inequalities, VAL is no good; but if the signed
1435 comparison had *any* fixed result, it follows that the
1436 unsigned comparison just tests the sign in reverse
1437 (positive values are LE, negative ones GE).
1438 So we can generate an unsigned comparison
1439 against an extreme value of the signed type. */
1441 if (unsignedp
&& !unsignedp0
)
1448 primop1
= TYPE_MIN_VALUE (type
);
1454 primop1
= TYPE_MAX_VALUE (type
);
1458 type
= unsigned_type (type
);
1461 if (!max_gt
&& !unsignedp0
&& TREE_CODE (primop1
) != INTEGER_CST
)
1463 /* This is the case of (char)x >?< 0x80, which people used to use
1464 expecting old C compilers to change the 0x80 into -0x80. */
1465 if (val
== integer_zero_node
)
1466 warning ("comparison is always 0 due to limited range of data type");
1467 if (val
== integer_one_node
)
1468 warning ("comparison is always 1 due to limited range of data type");
1471 if (!min_lt
&& unsignedp0
&& TREE_CODE (primop1
) != INTEGER_CST
)
1473 /* This is the case of (unsigned char)x >?< -1 or < 0. */
1474 if (val
== integer_zero_node
)
1475 warning ("comparison is always 0 due to limited range of data type");
1476 if (val
== integer_one_node
)
1477 warning ("comparison is always 1 due to limited range of data type");
1482 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
1483 if (TREE_SIDE_EFFECTS (primop0
))
1484 return build (COMPOUND_EXPR
, TREE_TYPE (val
), primop0
, val
);
1488 /* Value is not predetermined, but do the comparison
1489 in the type of the operand that is not constant.
1490 TYPE is already properly set. */
1492 else if (real1
&& real2
1493 && (TYPE_PRECISION (TREE_TYPE (primop0
))
1494 == TYPE_PRECISION (TREE_TYPE (primop1
))))
1495 type
= TREE_TYPE (primop0
);
1497 /* If args' natural types are both narrower than nominal type
1498 and both extend in the same manner, compare them
1499 in the type of the wider arg.
1500 Otherwise must actually extend both to the nominal
1501 common type lest different ways of extending
1503 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
1505 else if (unsignedp0
== unsignedp1
&& real1
== real2
1506 && TYPE_PRECISION (TREE_TYPE (primop0
)) < TYPE_PRECISION (*restype_ptr
)
1507 && TYPE_PRECISION (TREE_TYPE (primop1
)) < TYPE_PRECISION (*restype_ptr
))
1509 type
= common_type (TREE_TYPE (primop0
), TREE_TYPE (primop1
));
1510 type
= signed_or_unsigned_type (unsignedp0
1511 || TREE_UNSIGNED (*restype_ptr
),
1513 /* Make sure shorter operand is extended the right way
1514 to match the longer operand. */
1515 primop0
= convert (signed_or_unsigned_type (unsignedp0
, TREE_TYPE (primop0
)),
1517 primop1
= convert (signed_or_unsigned_type (unsignedp1
, TREE_TYPE (primop1
)),
1522 /* Here we must do the comparison on the nominal type
1523 using the args exactly as we received them. */
1524 type
= *restype_ptr
;
1528 if (!real1
&& !real2
&& integer_zerop (primop1
)
1529 && TREE_UNSIGNED (TREE_TYPE (primop0
)))
1536 warning ("unsigned value >= 0 is always 1");
1537 value
= integer_one_node
;
1542 warning ("unsigned value < 0 is always 0");
1543 value
= integer_zero_node
;
1548 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
1549 if (TREE_SIDE_EFFECTS (primop0
))
1550 return build (COMPOUND_EXPR
, TREE_TYPE (value
),
1557 *op0_ptr
= convert (type
, primop0
);
1558 *op1_ptr
= convert (type
, primop1
);
1560 *restype_ptr
= integer_type_node
;
1565 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
1566 or validate its data type for an `if' or `while' statement or ?..: exp.
1568 This preparation consists of taking the ordinary
1569 representation of an expression expr and producing a valid tree
1570 boolean expression describing whether expr is nonzero. We could
1571 simply always do build_binary_op (NE_EXPR, expr, integer_zero_node, 1),
1572 but we optimize comparisons, &&, ||, and !.
1574 The resulting type should always be `integer_type_node'. */
1577 truthvalue_conversion (expr
)
1580 register enum tree_code code
;
1582 if (TREE_CODE (expr
) == ERROR_MARK
)
1585 #if 0 /* This appears to be wrong for C++. */
1586 /* These really should return error_mark_node after 2.4 is stable.
1587 But not all callers handle ERROR_MARK properly. */
1588 switch (TREE_CODE (TREE_TYPE (expr
)))
1591 error ("struct type value used where scalar is required");
1592 return integer_zero_node
;
1595 error ("union type value used where scalar is required");
1596 return integer_zero_node
;
1599 error ("array type value used where scalar is required");
1600 return integer_zero_node
;
1607 switch (TREE_CODE (expr
))
1609 /* It is simpler and generates better code to have only TRUTH_*_EXPR
1610 or comparison expressions as truth values at this level. */
1613 /* A one-bit unsigned bit-field is already acceptable. */
1614 if (1 == TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (expr
, 1)))
1615 && TREE_UNSIGNED (TREE_OPERAND (expr
, 1)))
1621 /* It is simpler and generates better code to have only TRUTH_*_EXPR
1622 or comparison expressions as truth values at this level. */
1624 if (integer_zerop (TREE_OPERAND (expr
, 1)))
1625 return build_unary_op (TRUTH_NOT_EXPR
, TREE_OPERAND (expr
, 0), 0);
1627 case NE_EXPR
: case LE_EXPR
: case GE_EXPR
: case LT_EXPR
: case GT_EXPR
:
1628 case TRUTH_ANDIF_EXPR
:
1629 case TRUTH_ORIF_EXPR
:
1630 case TRUTH_AND_EXPR
:
1632 case TRUTH_XOR_EXPR
:
1637 return integer_zerop (expr
) ? integer_zero_node
: integer_one_node
;
1640 return real_zerop (expr
) ? integer_zero_node
: integer_one_node
;
1643 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr
, 0)))
1644 return build (COMPOUND_EXPR
, integer_type_node
,
1645 TREE_OPERAND (expr
, 0), integer_one_node
);
1647 return integer_one_node
;
1650 return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr
, 1))
1651 ? TRUTH_OR_EXPR
: TRUTH_ORIF_EXPR
),
1652 truthvalue_conversion (TREE_OPERAND (expr
, 0)),
1653 truthvalue_conversion (TREE_OPERAND (expr
, 1)),
1660 /* These don't change whether an object is non-zero or zero. */
1661 return truthvalue_conversion (TREE_OPERAND (expr
, 0));
1665 /* These don't change whether an object is zero or non-zero, but
1666 we can't ignore them if their second arg has side-effects. */
1667 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr
, 1)))
1668 return build (COMPOUND_EXPR
, integer_type_node
, TREE_OPERAND (expr
, 1),
1669 truthvalue_conversion (TREE_OPERAND (expr
, 0)));
1671 return truthvalue_conversion (TREE_OPERAND (expr
, 0));
1674 /* Distribute the conversion into the arms of a COND_EXPR. */
1675 return fold (build (COND_EXPR
, integer_type_node
, TREE_OPERAND (expr
, 0),
1676 truthvalue_conversion (TREE_OPERAND (expr
, 1)),
1677 truthvalue_conversion (TREE_OPERAND (expr
, 2))));
1680 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
1681 since that affects how `default_conversion' will behave. */
1682 if (TREE_CODE (TREE_TYPE (expr
)) == REFERENCE_TYPE
1683 || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0))) == REFERENCE_TYPE
)
1685 /* fall through... */
1687 /* If this is widening the argument, we can ignore it. */
1688 if (TYPE_PRECISION (TREE_TYPE (expr
))
1689 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr
, 0))))
1690 return truthvalue_conversion (TREE_OPERAND (expr
, 0));
1694 /* With IEEE arithmetic, x - x may not equal 0, so we can't optimize
1696 if (TARGET_FLOAT_FORMAT
== IEEE_FLOAT_FORMAT
1697 && TREE_CODE (TREE_TYPE (expr
)) == REAL_TYPE
)
1699 /* fall through... */
1701 /* This and MINUS_EXPR can be changed into a comparison of the
1703 if (TREE_TYPE (TREE_OPERAND (expr
, 0))
1704 == TREE_TYPE (TREE_OPERAND (expr
, 1)))
1705 return build_binary_op (NE_EXPR
, TREE_OPERAND (expr
, 0),
1706 TREE_OPERAND (expr
, 1), 1);
1707 return build_binary_op (NE_EXPR
, TREE_OPERAND (expr
, 0),
1708 fold (build1 (NOP_EXPR
,
1709 TREE_TYPE (TREE_OPERAND (expr
, 0)),
1710 TREE_OPERAND (expr
, 1))), 1);
1713 if (warn_parentheses
&& C_EXP_ORIGINAL_CODE (expr
) == MODIFY_EXPR
)
1714 warning ("suggest parentheses around assignment used as truth value");
1718 if (TREE_CODE (TREE_TYPE (expr
)) == COMPLEX_TYPE
)
1719 return (build_binary_op
1720 ((TREE_SIDE_EFFECTS (expr
)
1721 ? TRUTH_OR_EXPR
: TRUTH_ORIF_EXPR
),
1722 truthvalue_conversion (build_unary_op (REALPART_EXPR
, expr
, 0)),
1723 truthvalue_conversion (build_unary_op (IMAGPART_EXPR
, expr
, 0)),
1726 return build_binary_op (NE_EXPR
, expr
, integer_zero_node
, 1);
1729 /* Read the rest of a #-directive from input stream FINPUT.
1730 In normal use, the directive name and the white space after it
1731 have already been read, so they won't be included in the result.
1732 We allow for the fact that the directive line may contain
1733 a newline embedded within a character or string literal which forms
1734 a part of the directive.
1736 The value is a string in a reusable buffer. It remains valid
1737 only until the next time this function is called. */
1740 get_directive_line (finput
)
1741 register FILE *finput
;
1743 static char *directive_buffer
= NULL
;
1744 static unsigned buffer_length
= 0;
1746 register char *buffer_limit
;
1747 register int looking_for
= 0;
1748 register int char_escaped
= 0;
1750 if (buffer_length
== 0)
1752 directive_buffer
= (char *)xmalloc (128);
1753 buffer_length
= 128;
1756 buffer_limit
= &directive_buffer
[buffer_length
];
1758 for (p
= directive_buffer
; ; )
1762 /* Make buffer bigger if it is full. */
1763 if (p
>= buffer_limit
)
1765 register unsigned bytes_used
= (p
- directive_buffer
);
1769 = (char *)xrealloc (directive_buffer
, buffer_length
);
1770 p
= &directive_buffer
[bytes_used
];
1771 buffer_limit
= &directive_buffer
[buffer_length
];
1776 /* Discard initial whitespace. */
1777 if ((c
== ' ' || c
== '\t') && p
== directive_buffer
)
1780 /* Detect the end of the directive. */
1781 if (c
== '\n' && looking_for
== 0)
1790 return directive_buffer
;
1792 /* Handle string and character constant syntax. */
1795 if (looking_for
== c
&& !char_escaped
)
1796 looking_for
= 0; /* Found terminator... stop looking. */
1799 if (c
== '\'' || c
== '"')
1800 looking_for
= c
; /* Don't stop buffering until we see another
1801 another one of these (or an EOF). */
1803 /* Handle backslash. */
1804 char_escaped
= (c
== '\\' && ! char_escaped
);
1808 /* Make a variant type in the proper way for C/C++, propagating qualifiers
1809 down to the element type of an array. */
1812 c_build_type_variant (type
, constp
, volatilep
)
1814 int constp
, volatilep
;
1816 if (TREE_CODE (type
) == ARRAY_TYPE
)
1818 tree real_main_variant
= TYPE_MAIN_VARIANT (type
);
1820 push_obstacks (TYPE_OBSTACK (type
), TYPE_OBSTACK (type
));
1821 type
= build_array_type (c_build_type_variant (TREE_TYPE (type
),
1823 TYPE_DOMAIN (type
));
1824 TYPE_MAIN_VARIANT (type
) = real_main_variant
;
1827 return build_type_variant (type
, constp
, volatilep
);