1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992, 1993, 1994 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,
70 or if we want to give warnings for large objects, make a bigger one. */
71 vlen
= strlen (value
) + 1;
72 type
= char_array_type_node
;
73 if (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TREE_TYPE (type
))) < vlen
75 type
= build_array_type (char_type_node
,
76 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
;
209 tree a
, name
, args
, type
, new_attr
;
211 type
= TREE_TYPE (decl
);
213 new_attr
= TYPE_ATTRIBUTES (type
);
215 for (a
= attributes
; a
; a
= TREE_CHAIN (a
))
216 if (!(name
= TREE_VALUE (a
)))
218 else if (name
== get_identifier ("packed"))
220 if (TREE_CODE (decl
) == FIELD_DECL
)
221 DECL_PACKED (decl
) = 1;
222 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
223 used for DECL_REGISTER. It wouldn't mean anything anyway. */
225 warning_with_decl (decl
, "`packed' attribute ignore");
228 else if (TREE_VALUE (a
) == get_identifier ("noreturn")
229 || TREE_VALUE (a
) == get_identifier ("volatile"))
231 if (TREE_CODE (decl
) == FUNCTION_DECL
)
232 TREE_THIS_VOLATILE (decl
) = 1;
233 else if (TREE_CODE (type
) == POINTER_TYPE
234 && TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
)
235 TREE_TYPE (decl
) = type
237 (build_type_variant (TREE_TYPE (type
),
238 TREE_READONLY (TREE_TYPE (type
)), 1));
240 warning_with_decl (decl
, "`%s' attribute ignored",
241 IDENTIFIER_POINTER (TREE_VALUE (a
)));
243 else if (TREE_VALUE (a
) == get_identifier ("const"))
245 if (TREE_CODE (decl
) == FUNCTION_DECL
)
246 TREE_READONLY (decl
) = 1;
247 else if (TREE_CODE (type
) == POINTER_TYPE
248 && TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
)
249 TREE_TYPE (decl
) = type
251 (build_type_variant (TREE_TYPE (type
), 1,
252 TREE_THIS_VOLATILE (TREE_TYPE (type
))));
254 warning_with_decl (decl
, "`const' attribute ignored");
256 else if (TREE_VALUE (a
) == get_identifier ("transparent_union"))
258 if (TREE_CODE (decl
) == PARM_DECL
259 && TREE_CODE (type
) == UNION_TYPE
260 && TYPE_MODE (type
) == DECL_MODE (TYPE_FIELDS (type
)))
261 DECL_TRANSPARENT_UNION (decl
) = 1;
262 else if (TREE_CODE (decl
) == TYPE_DECL
263 && TREE_CODE (type
) == UNION_TYPE
264 && TYPE_MODE (type
) == DECL_MODE (TYPE_FIELDS (type
)))
265 TYPE_TRANSPARENT_UNION (type
) = 1;
267 warning_with_decl (decl
, "`transparent_union' attribute ignored");
269 else if (TREE_CODE (name
) != TREE_LIST
)
271 #ifdef VALID_MACHINE_ATTRIBUTE
272 if (VALID_MACHINE_ATTRIBUTE (type
, new_attr
, name
))
274 register tree atlist
;
276 for (atlist
= new_attr
; atlist
; atlist
= TREE_CHAIN (atlist
))
277 if (TREE_VALUE (atlist
) == name
)
280 new_attr
= tree_cons (NULL_TREE
, name
, new_attr
);
285 warning ("`%s' attribute directive ignored",
286 IDENTIFIER_POINTER (name
));
288 else if ( args
= TREE_CHAIN(name
),
289 !strcmp (IDENTIFIER_POINTER (name
= TREE_PURPOSE (name
)), "mode")
290 && list_length (args
) == 1
291 && TREE_CODE (TREE_VALUE (args
)) == IDENTIFIER_NODE
)
295 = IDENTIFIER_POINTER (TREE_VALUE (args
));
297 /* Give this decl a type with the specified mode. */
298 for (i
= 0; i
< NUM_MACHINE_MODES
; i
++)
299 if (!strcmp (specified_name
, GET_MODE_NAME (i
)))
302 = type_for_mode (i
, TREE_UNSIGNED (type
));
305 TREE_TYPE (decl
) = type
= typefm
;
306 DECL_SIZE (decl
) = 0;
307 layout_decl (decl
, 0);
310 error ("no data type for mode `%s'", specified_name
);
313 if (i
== NUM_MACHINE_MODES
)
314 error_with_decl (decl
, "unknown machine mode `%s'", specified_name
);
316 else if (!strcmp (IDENTIFIER_POINTER (name
), "section")
317 && list_length (args
) == 1
318 && TREE_CODE (TREE_VALUE (args
)) == STRING_CST
)
320 #ifdef ASM_OUTPUT_SECTION_NAME
321 if (TREE_CODE (decl
) == FUNCTION_DECL
|| TREE_CODE (decl
) == VAR_DECL
)
323 if (TREE_CODE (decl
) == VAR_DECL
&& current_function_decl
!= NULL_TREE
)
324 error_with_decl (decl
,
325 "section attribute cannot be specified for local variables");
326 /* The decl may have already been given a section attribute from
327 a previous declaration. Ensure they match. */
328 else if (DECL_SECTION_NAME (decl
) != NULL_TREE
329 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl
)),
330 TREE_STRING_POINTER (TREE_VALUE (args
))) != 0)
331 error_with_decl (decl
,
332 "section of `%s' conflicts with previous declaration");
334 DECL_SECTION_NAME (decl
) = TREE_VALUE (args
);
337 error_with_decl (decl
,
338 "section attribute not allowed for `%s'");
340 error_with_decl (decl
, "section attributes are not supported for this target");
343 else if (!strcmp (IDENTIFIER_POINTER (name
), "aligned")
344 && list_length (args
) == 1
345 && TREE_CODE (TREE_VALUE (args
)) == INTEGER_CST
)
347 tree align_expr
= TREE_VALUE (args
);
350 /* Strip any NOPs of any kind. */
351 while (TREE_CODE (align_expr
) == NOP_EXPR
352 || TREE_CODE (align_expr
) == CONVERT_EXPR
353 || TREE_CODE (align_expr
) == NON_LVALUE_EXPR
)
354 align_expr
= TREE_OPERAND (align_expr
, 0);
356 if (TREE_CODE (align_expr
) != INTEGER_CST
)
358 error_with_decl (decl
,
359 "requested alignment of `%s' is not a constant");
363 align
= TREE_INT_CST_LOW (align_expr
) * BITS_PER_UNIT
;
365 if (exact_log2 (align
) == -1)
366 error_with_decl (decl
,
367 "requested alignment of `%s' is not a power of 2");
368 else if (TREE_CODE (decl
) != VAR_DECL
369 && TREE_CODE (decl
) != FIELD_DECL
)
370 error_with_decl (decl
,
371 "alignment specified for `%s'");
373 DECL_ALIGN (decl
) = align
;
375 else if (!strcmp (IDENTIFIER_POINTER (name
), "format")
376 && list_length (args
) == 3
377 && TREE_CODE (TREE_VALUE (args
)) == IDENTIFIER_NODE
378 && TREE_CODE (TREE_VALUE (TREE_CHAIN (args
))) == INTEGER_CST
379 && TREE_CODE (TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args
)))) == INTEGER_CST
)
381 tree format_type
= TREE_VALUE (args
);
382 tree format_num_expr
= TREE_VALUE (TREE_CHAIN (args
));
383 tree first_arg_num_expr
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args
)));
390 if (TREE_CODE (decl
) != FUNCTION_DECL
)
392 error_with_decl (decl
,
393 "argument format specified for non-function `%s'");
397 if (!strcmp (IDENTIFIER_POINTER (format_type
), "printf"))
399 else if (!strcmp (IDENTIFIER_POINTER (format_type
), "scanf"))
403 error_with_decl (decl
, "unrecognized format specifier for `%s'");
407 /* Strip any conversions from the string index and first arg number
408 and verify they are constants. */
409 while (TREE_CODE (format_num_expr
) == NOP_EXPR
410 || TREE_CODE (format_num_expr
) == CONVERT_EXPR
411 || TREE_CODE (format_num_expr
) == NON_LVALUE_EXPR
)
412 format_num_expr
= TREE_OPERAND (format_num_expr
, 0);
414 while (TREE_CODE (first_arg_num_expr
) == NOP_EXPR
415 || TREE_CODE (first_arg_num_expr
) == CONVERT_EXPR
416 || TREE_CODE (first_arg_num_expr
) == NON_LVALUE_EXPR
)
417 first_arg_num_expr
= TREE_OPERAND (first_arg_num_expr
, 0);
419 if (TREE_CODE (format_num_expr
) != INTEGER_CST
420 || TREE_CODE (first_arg_num_expr
) != INTEGER_CST
)
422 error_with_decl (decl
,
423 "format string for `%s' has non-constant operand number");
427 format_num
= TREE_INT_CST_LOW (format_num_expr
);
428 first_arg_num
= TREE_INT_CST_LOW (first_arg_num_expr
);
429 if (first_arg_num
!= 0 && first_arg_num
<= format_num
)
431 error_with_decl (decl
,
432 "format string arg follows the args to be formatted, for `%s'");
436 /* If a parameter list is specified, verify that the format_num
437 argument is actually a string, in case the format attribute
439 argument
= TYPE_ARG_TYPES (type
);
442 for (arg_num
= 1; ; ++arg_num
)
444 if (argument
== 0 || arg_num
== format_num
)
446 argument
= TREE_CHAIN (argument
);
449 || TREE_CODE (TREE_VALUE (argument
)) != POINTER_TYPE
450 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument
)))
453 error_with_decl (decl
,
454 "format string arg not a string type, for `%s'");
457 if (first_arg_num
!= 0)
459 /* Verify that first_arg_num points to the last arg, the ... */
461 arg_num
++, argument
= TREE_CHAIN (argument
);
462 if (arg_num
!= first_arg_num
)
464 error_with_decl (decl
,
465 "args to be formatted is not ..., for `%s'");
471 record_function_format (DECL_NAME (decl
), DECL_ASSEMBLER_NAME (decl
),
472 is_scan
, format_num
, first_arg_num
);
475 warning ("`%s' attribute directive ignored",
476 IDENTIFIER_POINTER (name
));
478 TREE_TYPE (decl
) = build_type_attribute_variant (type
, new_attr
);
481 /* Check a printf/fprintf/sprintf/scanf/fscanf/sscanf format against
484 #define T_I &integer_type_node
485 #define T_L &long_integer_type_node
486 #define T_LL &long_long_integer_type_node
487 #define T_S &short_integer_type_node
488 #define T_UI &unsigned_type_node
489 #define T_UL &long_unsigned_type_node
490 #define T_ULL &long_long_unsigned_type_node
491 #define T_US &short_unsigned_type_node
492 #define T_F &float_type_node
493 #define T_D &double_type_node
494 #define T_LD &long_double_type_node
495 #define T_C &char_type_node
496 #define T_V &void_type_node
497 #define T_W &wchar_type_node
498 #define T_ST &sizetype
503 /* Type of argument if no length modifier is used. */
505 /* Type of argument if length modifier for shortening is used.
506 If NULL, then this modifier is not allowed. */
508 /* Type of argument if length modifier `l' is used.
509 If NULL, then this modifier is not allowed. */
511 /* Type of argument if length modifier `q' is used.
512 If NULL, then this modifier is not allowed. */
514 /* Type of argument if length modifier `L' is used.
515 If NULL, then this modifier is not allowed. */
517 /* List of other modifier characters allowed with these options. */
521 static format_char_info print_char_table
[] = {
522 { "di", 0, T_I
, T_I
, T_L
, T_LL
, NULL
, "-wp0 +" },
523 { "oxX", 0, T_UI
, T_UI
, T_UL
, T_ULL
, NULL
, "-wp0#" },
524 { "u", 0, T_UI
, T_UI
, T_UL
, T_ULL
, NULL
, "-wp0" },
525 /* Two GNU extensions. */
526 { "Z", 0, T_ST
, NULL
, NULL
, NULL
, NULL
, "-wp0" },
527 { "m", 0, T_UI
, T_UI
, T_UL
, NULL
, NULL
, "-wp" },
528 { "feEgG", 0, T_D
, NULL
, NULL
, NULL
, T_LD
, "-wp0 +#" },
529 { "c", 0, T_I
, NULL
, T_W
, NULL
, NULL
, "-w" },
530 { "C", 0, T_W
, NULL
, NULL
, NULL
, NULL
, "-w" },
531 { "s", 1, T_C
, NULL
, T_W
, NULL
, NULL
, "-wp" },
532 { "S", 1, T_W
, NULL
, NULL
, NULL
, NULL
, "-wp" },
533 { "p", 1, T_V
, NULL
, NULL
, NULL
, NULL
, "-w" },
534 { "n", 1, T_I
, T_S
, T_L
, T_LL
, NULL
, "" },
538 static format_char_info scan_char_table
[] = {
539 { "di", 1, T_I
, T_S
, T_L
, T_LL
, NULL
, "*" },
540 { "ouxX", 1, T_UI
, T_US
, T_UL
, T_ULL
, NULL
, "*" },
541 { "efgEG", 1, T_F
, NULL
, T_D
, NULL
, T_LD
, "*" },
542 { "sc", 1, T_C
, NULL
, T_W
, NULL
, NULL
, "*a" },
543 { "[", 1, T_C
, NULL
, NULL
, NULL
, NULL
, "*a" },
544 { "C", 1, T_W
, NULL
, NULL
, NULL
, NULL
, "*" },
545 { "S", 1, T_W
, NULL
, NULL
, NULL
, NULL
, "*" },
546 { "p", 2, T_V
, NULL
, NULL
, NULL
, NULL
, "*" },
547 { "n", 1, T_I
, T_S
, T_L
, T_LL
, NULL
, "" },
551 typedef struct function_format_info
{
552 struct function_format_info
*next
; /* next structure on the list */
553 tree name
; /* identifier such as "printf" */
554 tree assembler_name
; /* optional mangled identifier (for C++) */
555 int is_scan
; /* TRUE if *scanf */
556 int format_num
; /* number of format argument */
557 int first_arg_num
; /* number of first arg (zero for varargs) */
558 } function_format_info
;
560 static function_format_info
*function_format_list
= NULL
;
562 static void check_format_info
PROTO((function_format_info
*, tree
));
564 /* Initialize the table of functions to perform format checking on.
565 The ANSI functions are always checked (whether <stdio.h> is
566 included or not), since it is common to call printf without
567 including <stdio.h>. There shouldn't be a problem with this,
568 since ANSI reserves these function names whether you include the
569 header file or not. In any case, the checking is harmless. */
572 init_function_format_info ()
574 record_function_format (get_identifier ("printf"), NULL_TREE
, 0, 1, 2);
575 record_function_format (get_identifier ("fprintf"), NULL_TREE
, 0, 2, 3);
576 record_function_format (get_identifier ("sprintf"), NULL_TREE
, 0, 2, 3);
577 record_function_format (get_identifier ("scanf"), NULL_TREE
, 1, 1, 2);
578 record_function_format (get_identifier ("fscanf"), NULL_TREE
, 1, 2, 3);
579 record_function_format (get_identifier ("sscanf"), NULL_TREE
, 1, 2, 3);
580 record_function_format (get_identifier ("vprintf"), NULL_TREE
, 0, 1, 0);
581 record_function_format (get_identifier ("vfprintf"), NULL_TREE
, 0, 2, 0);
582 record_function_format (get_identifier ("vsprintf"), NULL_TREE
, 0, 2, 0);
585 /* Record information for argument format checking. FUNCTION_IDENT is
586 the identifier node for the name of the function to check (its decl
587 need not exist yet). IS_SCAN is true for scanf-type format checking;
588 false indicates printf-style format checking. FORMAT_NUM is the number
589 of the argument which is the format control string (starting from 1).
590 FIRST_ARG_NUM is the number of the first actual argument to check
591 against teh format string, or zero if no checking is not be done
592 (e.g. for varargs such as vfprintf). */
595 record_function_format (name
, assembler_name
, is_scan
,
596 format_num
, first_arg_num
)
603 function_format_info
*info
;
605 /* Re-use existing structure if it's there. */
607 for (info
= function_format_list
; info
; info
= info
->next
)
609 if (info
->name
== name
&& info
->assembler_name
== assembler_name
)
614 info
= (function_format_info
*) xmalloc (sizeof (function_format_info
));
615 info
->next
= function_format_list
;
616 function_format_list
= info
;
619 info
->assembler_name
= assembler_name
;
622 info
->is_scan
= is_scan
;
623 info
->format_num
= format_num
;
624 info
->first_arg_num
= first_arg_num
;
627 static char tfaff
[] = "too few arguments for format";
629 /* Check the argument list of a call to printf, scanf, etc.
630 NAME is the function identifier.
631 ASSEMBLER_NAME is the function's assembler identifier.
632 (Either NAME or ASSEMBLER_NAME, but not both, may be NULL_TREE.)
633 PARAMS is the list of argument values. */
636 check_function_format (name
, assembler_name
, params
)
641 function_format_info
*info
;
643 /* See if this function is a format function. */
644 for (info
= function_format_list
; info
; info
= info
->next
)
646 if (info
->assembler_name
647 ? (info
->assembler_name
== assembler_name
)
648 : (info
->name
== name
))
651 check_format_info (info
, params
);
657 /* Check the argument list of a call to printf, scanf, etc.
658 INFO points to the function_format_info structure.
659 PARAMS is the list of argument values. */
662 check_format_info (info
, params
)
663 function_format_info
*info
;
668 int suppressed
, wide
, precise
;
676 tree first_fillin_param
;
678 format_char_info
*fci
;
679 static char message
[132];
681 int has_operand_number
= 0;
683 /* Skip to format argument. If the argument isn't available, there's
684 no work for us to do; prototype checking will catch the problem. */
685 for (arg_num
= 1; ; ++arg_num
)
689 if (arg_num
== info
->format_num
)
691 params
= TREE_CHAIN (params
);
693 format_tree
= TREE_VALUE (params
);
694 params
= TREE_CHAIN (params
);
695 if (format_tree
== 0)
697 /* We can only check the format if it's a string constant. */
698 while (TREE_CODE (format_tree
) == NOP_EXPR
)
699 format_tree
= TREE_OPERAND (format_tree
, 0); /* strip coercion */
700 if (format_tree
== null_pointer_node
)
702 warning ("null format string");
705 if (TREE_CODE (format_tree
) != ADDR_EXPR
)
707 format_tree
= TREE_OPERAND (format_tree
, 0);
708 if (TREE_CODE (format_tree
) != STRING_CST
)
710 format_chars
= TREE_STRING_POINTER (format_tree
);
711 format_length
= TREE_STRING_LENGTH (format_tree
);
712 if (format_length
<= 1)
713 warning ("zero-length format string");
714 if (format_chars
[--format_length
] != 0)
716 warning ("unterminated format string");
719 /* Skip to first argument to check. */
720 while (arg_num
+ 1 < info
->first_arg_num
)
724 params
= TREE_CHAIN (params
);
728 first_fillin_param
= params
;
732 if (*format_chars
== 0)
734 if (format_chars
- TREE_STRING_POINTER (format_tree
) != format_length
)
735 warning ("embedded `\\0' in format");
736 if (info
->first_arg_num
!= 0 && params
!= 0 && ! has_operand_number
)
737 warning ("too many arguments for format");
740 if (*format_chars
++ != '%')
742 if (*format_chars
== 0)
744 warning ("spurious trailing `%%' in format");
747 if (*format_chars
== '%')
753 suppressed
= wide
= precise
= FALSE
;
756 suppressed
= *format_chars
== '*';
759 while (isdigit (*format_chars
))
764 /* See if we have a number followed by a dollar sign. If we do,
765 it is an operand number, so set PARAMS to that operand. */
766 if (*format_chars
>= '0' && *format_chars
<= '9')
768 char *p
= format_chars
;
770 while (*p
>= '0' && *p
++ <= '9')
775 int opnum
= atoi (format_chars
);
777 params
= first_fillin_param
;
778 format_chars
= p
+ 1;
779 has_operand_number
= 1;
781 for (i
= 1; i
< opnum
&& params
!= 0; i
++)
782 params
= TREE_CHAIN (params
);
784 if (opnum
== 0 || params
== 0)
786 warning ("operand number out of range in format");
792 while (*format_chars
!= 0 && index (" +#0-", *format_chars
) != 0)
794 if (index (flag_chars
, *format_chars
) != 0)
796 sprintf (message
, "repeated `%c' flag in format",
800 i
= strlen (flag_chars
);
801 flag_chars
[i
++] = *format_chars
++;
804 /* "If the space and + flags both appear,
805 the space flag will be ignored." */
806 if (index (flag_chars
, ' ') != 0
807 && index (flag_chars
, '+') != 0)
808 warning ("use of both ` ' and `+' flags in format");
809 /* "If the 0 and - flags both appear,
810 the 0 flag will be ignored." */
811 if (index (flag_chars
, '0') != 0
812 && index (flag_chars
, '-') != 0)
813 warning ("use of both `0' and `-' flags in format");
814 if (*format_chars
== '*')
817 /* "...a field width...may be indicated by an asterisk.
818 In this case, an int argument supplies the field width..." */
825 if (info
->first_arg_num
!= 0)
827 cur_param
= TREE_VALUE (params
);
828 params
= TREE_CHAIN (params
);
830 /* size_t is generally not valid here.
831 It will work on most machines, because size_t and int
832 have the same mode. But might as well warn anyway,
833 since it will fail on other machines. */
834 if ((TYPE_MAIN_VARIANT (TREE_TYPE (cur_param
))
835 != integer_type_node
)
837 (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param
))
838 != unsigned_type_node
))
841 "field width is not type int (arg %d)",
849 while (isdigit (*format_chars
))
855 if (*format_chars
== '.')
859 if (*format_chars
!= '*' && !isdigit (*format_chars
))
860 warning ("`.' not followed by `*' or digit in format");
861 /* "...a...precision...may be indicated by an asterisk.
862 In this case, an int argument supplies the...precision." */
863 if (*format_chars
== '*')
865 if (info
->first_arg_num
!= 0)
873 cur_param
= TREE_VALUE (params
);
874 params
= TREE_CHAIN (params
);
876 if (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param
))
877 != integer_type_node
)
880 "field width is not type int (arg %d)",
888 while (isdigit (*format_chars
))
893 if (*format_chars
== 'h' || *format_chars
== 'l' || *format_chars
== 'q' ||
894 *format_chars
== 'L')
895 length_char
= *format_chars
++;
899 if (*format_chars
== 'a')
904 if (suppressed
&& length_char
!= 0)
907 "use of `*' and `%c' together in format",
911 format_char
= *format_chars
;
912 if (format_char
== 0)
914 warning ("conversion lacks type at end of format");
918 fci
= info
->is_scan
? scan_char_table
: print_char_table
;
919 while (fci
->format_chars
!= 0
920 && index (fci
->format_chars
, format_char
) == 0)
922 if (fci
->format_chars
== 0)
924 if (format_char
>= 040 && format_char
< 0177)
926 "unknown conversion type character `%c' in format",
930 "unknown conversion type character 0x%x in format",
935 if (wide
&& index (fci
->flag_chars
, 'w') == 0)
937 sprintf (message
, "width used with `%c' format",
941 if (precise
&& index (fci
->flag_chars
, 'p') == 0)
943 sprintf (message
, "precision used with `%c' format",
947 if (aflag
&& index (fci
->flag_chars
, 'a') == 0)
949 sprintf (message
, "`a' flag used with `%c' format",
953 if (info
->is_scan
&& format_char
== '[')
955 /* Skip over scan set, in case it happens to have '%' in it. */
956 if (*format_chars
== '^')
958 /* Find closing bracket; if one is hit immediately, then
959 it's part of the scan set rather than a terminator. */
960 if (*format_chars
== ']')
962 while (*format_chars
&& *format_chars
!= ']')
964 if (*format_chars
!= ']')
965 /* The end of the format string was reached. */
966 warning ("no closing `]' for `%%[' format");
970 if (index (fci
->flag_chars
, '*') == 0)
973 "suppression of `%c' conversion in format",
979 for (i
= 0; flag_chars
[i
] != 0; ++i
)
981 if (index (fci
->flag_chars
, flag_chars
[i
]) == 0)
983 sprintf (message
, "flag `%c' used with type `%c'",
984 flag_chars
[i
], format_char
);
988 if (precise
&& index (flag_chars
, '0') != 0
989 && (format_char
== 'd' || format_char
== 'i'
990 || format_char
== 'o' || format_char
== 'u'
991 || format_char
== 'x' || format_char
== 'x'))
994 "precision and `0' flag not both allowed with `%c' format",
1000 default: wanted_type
= fci
->nolen
? *(fci
->nolen
) : 0; break;
1001 case 'h': wanted_type
= fci
->hlen
? *(fci
->hlen
) : 0; break;
1002 case 'l': wanted_type
= fci
->llen
? *(fci
->llen
) : 0; break;
1003 case 'q': wanted_type
= fci
->qlen
? *(fci
->qlen
) : 0; break;
1004 case 'L': wanted_type
= fci
->bigllen
? *(fci
->bigllen
) : 0; break;
1006 if (wanted_type
== 0)
1009 "use of `%c' length character with `%c' type character",
1010 length_char
, format_char
);
1015 ** XXX -- should kvetch about stuff such as
1019 ** scanf ("%d", &i);
1023 /* Finally. . .check type of argument against desired type! */
1024 if (info
->first_arg_num
== 0)
1031 cur_param
= TREE_VALUE (params
);
1032 params
= TREE_CHAIN (params
);
1034 cur_type
= TREE_TYPE (cur_param
);
1036 /* Check the types of any additional pointer arguments
1037 that precede the "real" argument. */
1038 for (i
= 0; i
< fci
->pointer_count
; ++i
)
1040 if (TREE_CODE (cur_type
) == POINTER_TYPE
)
1042 cur_type
= TREE_TYPE (cur_type
);
1046 "format argument is not a %s (arg %d)",
1047 ((fci
->pointer_count
== 1) ? "pointer" : "pointer to a pointer"),
1053 /* Check the type of the "real" argument, if there's a type we want. */
1054 if (i
== fci
->pointer_count
&& wanted_type
!= 0
1055 && wanted_type
!= TYPE_MAIN_VARIANT (cur_type
)
1056 /* If we want `void *', allow any pointer type.
1057 (Anything else would already have got a warning.) */
1058 && ! (wanted_type
== void_type_node
1059 && fci
->pointer_count
> 0)
1060 /* Don't warn about differences merely in signedness. */
1061 && !(TREE_CODE (wanted_type
) == INTEGER_TYPE
1062 && TREE_CODE (TYPE_MAIN_VARIANT (cur_type
)) == INTEGER_TYPE
1063 && (TREE_UNSIGNED (wanted_type
)
1064 ? wanted_type
== (cur_type
= unsigned_type (cur_type
))
1065 : wanted_type
== (cur_type
= signed_type (cur_type
))))
1066 /* Likewise, "signed char", "unsigned char" and "char" are
1067 equivalent but the above test won't consider them equivalent. */
1068 && ! (wanted_type
== char_type_node
1069 && (TYPE_MAIN_VARIANT (cur_type
) == signed_char_type_node
1070 || TYPE_MAIN_VARIANT (cur_type
) == unsigned_char_type_node
)))
1072 register char *this;
1073 register char *that
;
1075 this = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (wanted_type
)));
1077 if (TREE_CODE (cur_type
) != ERROR_MARK
1078 && TYPE_NAME (cur_type
) != 0
1079 && TREE_CODE (cur_type
) != INTEGER_TYPE
1080 && !(TREE_CODE (cur_type
) == POINTER_TYPE
1081 && TREE_CODE (TREE_TYPE (cur_type
)) == INTEGER_TYPE
))
1083 if (TREE_CODE (TYPE_NAME (cur_type
)) == TYPE_DECL
1084 && DECL_NAME (TYPE_NAME (cur_type
)) != 0)
1085 that
= IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type
)));
1087 that
= IDENTIFIER_POINTER (TYPE_NAME (cur_type
));
1090 /* A nameless type can't possibly match what the format wants.
1091 So there will be a warning for it.
1092 Make up a string to describe vaguely what it is. */
1095 if (TREE_CODE (cur_type
) == POINTER_TYPE
)
1098 that
= "different type";
1101 /* Make the warning better in case of mismatch of int vs long. */
1102 if (TREE_CODE (cur_type
) == INTEGER_TYPE
1103 && TREE_CODE (wanted_type
) == INTEGER_TYPE
1104 && TYPE_PRECISION (cur_type
) == TYPE_PRECISION (wanted_type
)
1105 && TYPE_NAME (cur_type
) != 0
1106 && TREE_CODE (TYPE_NAME (cur_type
)) == TYPE_DECL
)
1107 that
= IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type
)));
1109 if (strcmp (this, that
) != 0)
1111 sprintf (message
, "%s format, %s arg (arg %d)",
1112 this, that
, arg_num
);
1119 /* Print a warning if a constant expression had overflow in folding.
1120 Invoke this function on every expression that the language
1121 requires to be a constant expression.
1122 Note the ANSI C standard says it is erroneous for a
1123 constant expression to overflow. */
1126 constant_expression_warning (value
)
1129 if ((TREE_CODE (value
) == INTEGER_CST
|| TREE_CODE (value
) == REAL_CST
1130 || TREE_CODE (value
) == COMPLEX_CST
)
1131 && TREE_CONSTANT_OVERFLOW (value
) && pedantic
)
1132 pedwarn ("overflow in constant expression");
1135 /* Print a warning if an expression had overflow in folding.
1136 Invoke this function on every expression that
1137 (1) appears in the source code, and
1138 (2) might be a constant expression that overflowed, and
1139 (3) is not already checked by convert_and_check;
1140 however, do not invoke this function on operands of explicit casts. */
1143 overflow_warning (value
)
1146 if ((TREE_CODE (value
) == INTEGER_CST
1147 || (TREE_CODE (value
) == COMPLEX_CST
1148 && TREE_CODE (TREE_REALPART (value
)) == INTEGER_CST
))
1149 && TREE_OVERFLOW (value
))
1151 TREE_OVERFLOW (value
) = 0;
1152 warning ("integer overflow in expression");
1154 else if ((TREE_CODE (value
) == REAL_CST
1155 || (TREE_CODE (value
) == COMPLEX_CST
1156 && TREE_CODE (TREE_REALPART (value
)) == REAL_CST
))
1157 && TREE_OVERFLOW (value
))
1159 TREE_OVERFLOW (value
) = 0;
1160 warning ("floating-pointer overflow in expression");
1164 /* Print a warning if a large constant is truncated to unsigned,
1165 or if -Wconversion is used and a constant < 0 is converted to unsigned.
1166 Invoke this function on every expression that might be implicitly
1167 converted to an unsigned type. */
1170 unsigned_conversion_warning (result
, operand
)
1171 tree result
, operand
;
1173 if (TREE_CODE (operand
) == INTEGER_CST
1174 && TREE_CODE (TREE_TYPE (result
)) == INTEGER_TYPE
1175 && TREE_UNSIGNED (TREE_TYPE (result
))
1176 && !int_fits_type_p (operand
, TREE_TYPE (result
)))
1178 if (!int_fits_type_p (operand
, signed_type (TREE_TYPE (result
))))
1179 /* This detects cases like converting -129 or 256 to unsigned char. */
1180 warning ("large integer implicitly truncated to unsigned type");
1181 else if (warn_conversion
)
1182 warning ("negative integer implicitly converted to unsigned type");
1186 /* Convert EXPR to TYPE, warning about conversion problems with constants.
1187 Invoke this function on every expression that is converted implicitly,
1188 i.e. because of language rules and not because of an explicit cast. */
1191 convert_and_check (type
, expr
)
1194 tree t
= convert (type
, expr
);
1195 if (TREE_CODE (t
) == INTEGER_CST
)
1197 if (TREE_OVERFLOW (t
))
1199 TREE_OVERFLOW (t
) = 0;
1201 /* No warning for converting 0x80000000 to int. */
1202 if (!(TREE_UNSIGNED (type
) < TREE_UNSIGNED (TREE_TYPE (expr
))
1203 && TREE_CODE (TREE_TYPE (expr
)) == INTEGER_TYPE
1204 && TYPE_PRECISION (type
) == TYPE_PRECISION (TREE_TYPE (expr
))))
1205 /* If EXPR fits in the unsigned version of TYPE,
1206 don't warn unless pedantic. */
1208 || TREE_UNSIGNED (type
)
1209 || ! int_fits_type_p (expr
, unsigned_type (type
)))
1210 warning ("overflow in implicit constant conversion");
1213 unsigned_conversion_warning (t
, expr
);
1219 c_expand_expr_stmt (expr
)
1222 /* Do default conversion if safe and possibly important,
1223 in case within ({...}). */
1224 if ((TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
&& lvalue_p (expr
))
1225 || TREE_CODE (TREE_TYPE (expr
)) == FUNCTION_TYPE
)
1226 expr
= default_conversion (expr
);
1228 if (TREE_TYPE (expr
) != error_mark_node
1229 && TYPE_SIZE (TREE_TYPE (expr
)) == 0
1230 && TREE_CODE (TREE_TYPE (expr
)) != ARRAY_TYPE
)
1231 error ("expression statement has incomplete type");
1233 expand_expr_stmt (expr
);
1236 /* Validate the expression after `case' and apply default promotions. */
1239 check_case_value (value
)
1242 if (value
== NULL_TREE
)
1245 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1246 STRIP_TYPE_NOPS (value
);
1248 if (TREE_CODE (value
) != INTEGER_CST
1249 && value
!= error_mark_node
)
1251 error ("case label does not reduce to an integer constant");
1252 value
= error_mark_node
;
1255 /* Promote char or short to int. */
1256 value
= default_conversion (value
);
1258 constant_expression_warning (value
);
1263 /* Return an integer type with BITS bits of precision,
1264 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
1267 type_for_size (bits
, unsignedp
)
1271 if (bits
== TYPE_PRECISION (signed_char_type_node
))
1272 return unsignedp
? unsigned_char_type_node
: signed_char_type_node
;
1274 if (bits
== TYPE_PRECISION (short_integer_type_node
))
1275 return unsignedp
? short_unsigned_type_node
: short_integer_type_node
;
1277 if (bits
== TYPE_PRECISION (integer_type_node
))
1278 return unsignedp
? unsigned_type_node
: integer_type_node
;
1280 if (bits
== TYPE_PRECISION (long_integer_type_node
))
1281 return unsignedp
? long_unsigned_type_node
: long_integer_type_node
;
1283 if (bits
== TYPE_PRECISION (long_long_integer_type_node
))
1284 return (unsignedp
? long_long_unsigned_type_node
1285 : long_long_integer_type_node
);
1287 if (bits
<= TYPE_PRECISION (intQI_type_node
))
1288 return unsignedp
? unsigned_intQI_type_node
: intQI_type_node
;
1290 if (bits
<= TYPE_PRECISION (intHI_type_node
))
1291 return unsignedp
? unsigned_intHI_type_node
: intHI_type_node
;
1293 if (bits
<= TYPE_PRECISION (intSI_type_node
))
1294 return unsignedp
? unsigned_intSI_type_node
: intSI_type_node
;
1296 if (bits
<= TYPE_PRECISION (intDI_type_node
))
1297 return unsignedp
? unsigned_intDI_type_node
: intDI_type_node
;
1302 /* Return a data type that has machine mode MODE.
1303 If the mode is an integer,
1304 then UNSIGNEDP selects between signed and unsigned types. */
1307 type_for_mode (mode
, unsignedp
)
1308 enum machine_mode mode
;
1311 if (mode
== TYPE_MODE (signed_char_type_node
))
1312 return unsignedp
? unsigned_char_type_node
: signed_char_type_node
;
1314 if (mode
== TYPE_MODE (short_integer_type_node
))
1315 return unsignedp
? short_unsigned_type_node
: short_integer_type_node
;
1317 if (mode
== TYPE_MODE (integer_type_node
))
1318 return unsignedp
? unsigned_type_node
: integer_type_node
;
1320 if (mode
== TYPE_MODE (long_integer_type_node
))
1321 return unsignedp
? long_unsigned_type_node
: long_integer_type_node
;
1323 if (mode
== TYPE_MODE (long_long_integer_type_node
))
1324 return unsignedp
? long_long_unsigned_type_node
: long_long_integer_type_node
;
1326 if (mode
== TYPE_MODE (intQI_type_node
))
1327 return unsignedp
? unsigned_intQI_type_node
: intQI_type_node
;
1329 if (mode
== TYPE_MODE (intHI_type_node
))
1330 return unsignedp
? unsigned_intHI_type_node
: intHI_type_node
;
1332 if (mode
== TYPE_MODE (intSI_type_node
))
1333 return unsignedp
? unsigned_intSI_type_node
: intSI_type_node
;
1335 if (mode
== TYPE_MODE (intDI_type_node
))
1336 return unsignedp
? unsigned_intDI_type_node
: intDI_type_node
;
1338 if (mode
== TYPE_MODE (float_type_node
))
1339 return float_type_node
;
1341 if (mode
== TYPE_MODE (double_type_node
))
1342 return double_type_node
;
1344 if (mode
== TYPE_MODE (long_double_type_node
))
1345 return long_double_type_node
;
1347 if (mode
== TYPE_MODE (build_pointer_type (char_type_node
)))
1348 return build_pointer_type (char_type_node
);
1350 if (mode
== TYPE_MODE (build_pointer_type (integer_type_node
)))
1351 return build_pointer_type (integer_type_node
);
1356 /* Return the minimum number of bits needed to represent VALUE in a
1357 signed or unsigned type, UNSIGNEDP says which. */
1360 min_precision (value
, unsignedp
)
1366 /* If the value is negative, compute its negative minus 1. The latter
1367 adjustment is because the absolute value of the largest negative value
1368 is one larger than the largest positive value. This is equivalent to
1369 a bit-wise negation, so use that operation instead. */
1371 if (tree_int_cst_sgn (value
) < 0)
1372 value
= fold (build1 (BIT_NOT_EXPR
, TREE_TYPE (value
), value
));
1374 /* Return the number of bits needed, taking into account the fact
1375 that we need one more bit for a signed than unsigned type. */
1377 if (integer_zerop (value
))
1379 else if (TREE_INT_CST_HIGH (value
) != 0)
1380 log
= HOST_BITS_PER_WIDE_INT
+ floor_log2 (TREE_INT_CST_HIGH (value
));
1382 log
= floor_log2 (TREE_INT_CST_LOW (value
));
1384 return log
+ 1 + ! unsignedp
;
1387 /* Print an error message for invalid operands to arith operation CODE.
1388 NOP_EXPR is used as a special case (see truthvalue_conversion). */
1391 binary_op_error (code
)
1392 enum tree_code code
;
1394 register char *opname
= "unknown";
1399 error ("invalid truth-value expression");
1403 opname
= "+"; break;
1405 opname
= "-"; break;
1407 opname
= "*"; break;
1409 opname
= "max"; break;
1411 opname
= "min"; break;
1413 opname
= "=="; break;
1415 opname
= "!="; break;
1417 opname
= "<="; break;
1419 opname
= ">="; break;
1421 opname
= "<"; break;
1423 opname
= ">"; break;
1425 opname
= "<<"; break;
1427 opname
= ">>"; break;
1428 case TRUNC_MOD_EXPR
:
1429 case FLOOR_MOD_EXPR
:
1430 opname
= "%"; break;
1431 case TRUNC_DIV_EXPR
:
1432 case FLOOR_DIV_EXPR
:
1433 opname
= "/"; break;
1435 opname
= "&"; break;
1437 opname
= "|"; break;
1438 case TRUTH_ANDIF_EXPR
:
1439 opname
= "&&"; break;
1440 case TRUTH_ORIF_EXPR
:
1441 opname
= "||"; break;
1443 opname
= "^"; break;
1446 opname
= "rotate"; break;
1448 error ("invalid operands to binary %s", opname
);
1451 /* Subroutine of build_binary_op, used for comparison operations.
1452 See if the operands have both been converted from subword integer types
1453 and, if so, perhaps change them both back to their original type.
1454 This function is also responsible for converting the two operands
1455 to the proper common type for comparison.
1457 The arguments of this function are all pointers to local variables
1458 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
1459 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
1461 If this function returns nonzero, it means that the comparison has
1462 a constant value. What this function returns is an expression for
1466 shorten_compare (op0_ptr
, op1_ptr
, restype_ptr
, rescode_ptr
)
1467 tree
*op0_ptr
, *op1_ptr
;
1469 enum tree_code
*rescode_ptr
;
1472 tree op0
= *op0_ptr
;
1473 tree op1
= *op1_ptr
;
1474 int unsignedp0
, unsignedp1
;
1476 tree primop0
, primop1
;
1477 enum tree_code code
= *rescode_ptr
;
1479 /* Throw away any conversions to wider types
1480 already present in the operands. */
1482 primop0
= get_narrower (op0
, &unsignedp0
);
1483 primop1
= get_narrower (op1
, &unsignedp1
);
1485 /* Handle the case that OP0 does not *contain* a conversion
1486 but it *requires* conversion to FINAL_TYPE. */
1488 if (op0
== primop0
&& TREE_TYPE (op0
) != *restype_ptr
)
1489 unsignedp0
= TREE_UNSIGNED (TREE_TYPE (op0
));
1490 if (op1
== primop1
&& TREE_TYPE (op1
) != *restype_ptr
)
1491 unsignedp1
= TREE_UNSIGNED (TREE_TYPE (op1
));
1493 /* If one of the operands must be floated, we cannot optimize. */
1494 real1
= TREE_CODE (TREE_TYPE (primop0
)) == REAL_TYPE
;
1495 real2
= TREE_CODE (TREE_TYPE (primop1
)) == REAL_TYPE
;
1497 /* If first arg is constant, swap the args (changing operation
1498 so value is preserved), for canonicalization. Don't do this if
1499 the second arg is 0. */
1501 if (TREE_CONSTANT (primop0
)
1502 && ! integer_zerop (primop1
) && ! real_zerop (primop1
))
1504 register tree tem
= primop0
;
1505 register int temi
= unsignedp0
;
1513 unsignedp0
= unsignedp1
;
1534 *rescode_ptr
= code
;
1537 /* If comparing an integer against a constant more bits wide,
1538 maybe we can deduce a value of 1 or 0 independent of the data.
1539 Or else truncate the constant now
1540 rather than extend the variable at run time.
1542 This is only interesting if the constant is the wider arg.
1543 Also, it is not safe if the constant is unsigned and the
1544 variable arg is signed, since in this case the variable
1545 would be sign-extended and then regarded as unsigned.
1546 Our technique fails in this case because the lowest/highest
1547 possible unsigned results don't follow naturally from the
1548 lowest/highest possible values of the variable operand.
1549 For just EQ_EXPR and NE_EXPR there is another technique that
1550 could be used: see if the constant can be faithfully represented
1551 in the other operand's type, by truncating it and reextending it
1552 and see if that preserves the constant's value. */
1554 if (!real1
&& !real2
1555 && TREE_CODE (primop1
) == INTEGER_CST
1556 && TYPE_PRECISION (TREE_TYPE (primop0
)) < TYPE_PRECISION (*restype_ptr
))
1558 int min_gt
, max_gt
, min_lt
, max_lt
;
1559 tree maxval
, minval
;
1560 /* 1 if comparison is nominally unsigned. */
1561 int unsignedp
= TREE_UNSIGNED (*restype_ptr
);
1564 type
= signed_or_unsigned_type (unsignedp0
, TREE_TYPE (primop0
));
1566 maxval
= TYPE_MAX_VALUE (type
);
1567 minval
= TYPE_MIN_VALUE (type
);
1569 if (unsignedp
&& !unsignedp0
)
1570 *restype_ptr
= signed_type (*restype_ptr
);
1572 if (TREE_TYPE (primop1
) != *restype_ptr
)
1573 primop1
= convert (*restype_ptr
, primop1
);
1574 if (type
!= *restype_ptr
)
1576 minval
= convert (*restype_ptr
, minval
);
1577 maxval
= convert (*restype_ptr
, maxval
);
1580 if (unsignedp
&& unsignedp0
)
1582 min_gt
= INT_CST_LT_UNSIGNED (primop1
, minval
);
1583 max_gt
= INT_CST_LT_UNSIGNED (primop1
, maxval
);
1584 min_lt
= INT_CST_LT_UNSIGNED (minval
, primop1
);
1585 max_lt
= INT_CST_LT_UNSIGNED (maxval
, primop1
);
1589 min_gt
= INT_CST_LT (primop1
, minval
);
1590 max_gt
= INT_CST_LT (primop1
, maxval
);
1591 min_lt
= INT_CST_LT (minval
, primop1
);
1592 max_lt
= INT_CST_LT (maxval
, primop1
);
1596 /* This used to be a switch, but Genix compiler can't handle that. */
1597 if (code
== NE_EXPR
)
1599 if (max_lt
|| min_gt
)
1600 val
= integer_one_node
;
1602 else if (code
== EQ_EXPR
)
1604 if (max_lt
|| min_gt
)
1605 val
= integer_zero_node
;
1607 else if (code
== LT_EXPR
)
1610 val
= integer_one_node
;
1612 val
= integer_zero_node
;
1614 else if (code
== GT_EXPR
)
1617 val
= integer_one_node
;
1619 val
= integer_zero_node
;
1621 else if (code
== LE_EXPR
)
1624 val
= integer_one_node
;
1626 val
= integer_zero_node
;
1628 else if (code
== GE_EXPR
)
1631 val
= integer_one_node
;
1633 val
= integer_zero_node
;
1636 /* If primop0 was sign-extended and unsigned comparison specd,
1637 we did a signed comparison above using the signed type bounds.
1638 But the comparison we output must be unsigned.
1640 Also, for inequalities, VAL is no good; but if the signed
1641 comparison had *any* fixed result, it follows that the
1642 unsigned comparison just tests the sign in reverse
1643 (positive values are LE, negative ones GE).
1644 So we can generate an unsigned comparison
1645 against an extreme value of the signed type. */
1647 if (unsignedp
&& !unsignedp0
)
1654 primop1
= TYPE_MIN_VALUE (type
);
1660 primop1
= TYPE_MAX_VALUE (type
);
1664 type
= unsigned_type (type
);
1667 if (!max_gt
&& !unsignedp0
&& TREE_CODE (primop0
) != INTEGER_CST
)
1669 /* This is the case of (char)x >?< 0x80, which people used to use
1670 expecting old C compilers to change the 0x80 into -0x80. */
1671 if (val
== integer_zero_node
)
1672 warning ("comparison is always 0 due to limited range of data type");
1673 if (val
== integer_one_node
)
1674 warning ("comparison is always 1 due to limited range of data type");
1677 if (!min_lt
&& unsignedp0
&& TREE_CODE (primop0
) != INTEGER_CST
)
1679 /* This is the case of (unsigned char)x >?< -1 or < 0. */
1680 if (val
== integer_zero_node
)
1681 warning ("comparison is always 0 due to limited range of data type");
1682 if (val
== integer_one_node
)
1683 warning ("comparison is always 1 due to limited range of data type");
1688 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
1689 if (TREE_SIDE_EFFECTS (primop0
))
1690 return build (COMPOUND_EXPR
, TREE_TYPE (val
), primop0
, val
);
1694 /* Value is not predetermined, but do the comparison
1695 in the type of the operand that is not constant.
1696 TYPE is already properly set. */
1698 else if (real1
&& real2
1699 && (TYPE_PRECISION (TREE_TYPE (primop0
))
1700 == TYPE_PRECISION (TREE_TYPE (primop1
))))
1701 type
= TREE_TYPE (primop0
);
1703 /* If args' natural types are both narrower than nominal type
1704 and both extend in the same manner, compare them
1705 in the type of the wider arg.
1706 Otherwise must actually extend both to the nominal
1707 common type lest different ways of extending
1709 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
1711 else if (unsignedp0
== unsignedp1
&& real1
== real2
1712 && TYPE_PRECISION (TREE_TYPE (primop0
)) < TYPE_PRECISION (*restype_ptr
)
1713 && TYPE_PRECISION (TREE_TYPE (primop1
)) < TYPE_PRECISION (*restype_ptr
))
1715 type
= common_type (TREE_TYPE (primop0
), TREE_TYPE (primop1
));
1716 type
= signed_or_unsigned_type (unsignedp0
1717 || TREE_UNSIGNED (*restype_ptr
),
1719 /* Make sure shorter operand is extended the right way
1720 to match the longer operand. */
1721 primop0
= convert (signed_or_unsigned_type (unsignedp0
, TREE_TYPE (primop0
)),
1723 primop1
= convert (signed_or_unsigned_type (unsignedp1
, TREE_TYPE (primop1
)),
1728 /* Here we must do the comparison on the nominal type
1729 using the args exactly as we received them. */
1730 type
= *restype_ptr
;
1734 if (!real1
&& !real2
&& integer_zerop (primop1
)
1735 && TREE_UNSIGNED (*restype_ptr
))
1741 /* All unsigned values are >= 0, so we warn if extra warnings
1742 are requested. However, if OP0 is a constant that is
1743 >= 0, the signedness of the comparison isn't an issue,
1744 so suppress the warning. */
1746 && ! (TREE_CODE (primop0
) == INTEGER_CST
1747 && ! TREE_OVERFLOW (convert (signed_type (type
),
1749 warning ("unsigned value >= 0 is always 1");
1750 value
= integer_one_node
;
1755 && ! (TREE_CODE (primop0
) == INTEGER_CST
1756 && ! TREE_OVERFLOW (convert (signed_type (type
),
1758 warning ("unsigned value < 0 is always 0");
1759 value
= integer_zero_node
;
1764 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
1765 if (TREE_SIDE_EFFECTS (primop0
))
1766 return build (COMPOUND_EXPR
, TREE_TYPE (value
),
1773 *op0_ptr
= convert (type
, primop0
);
1774 *op1_ptr
= convert (type
, primop1
);
1776 *restype_ptr
= integer_type_node
;
1781 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
1782 or validate its data type for an `if' or `while' statement or ?..: exp.
1784 This preparation consists of taking the ordinary
1785 representation of an expression expr and producing a valid tree
1786 boolean expression describing whether expr is nonzero. We could
1787 simply always do build_binary_op (NE_EXPR, expr, integer_zero_node, 1),
1788 but we optimize comparisons, &&, ||, and !.
1790 The resulting type should always be `integer_type_node'. */
1793 truthvalue_conversion (expr
)
1796 if (TREE_CODE (expr
) == ERROR_MARK
)
1799 #if 0 /* This appears to be wrong for C++. */
1800 /* These really should return error_mark_node after 2.4 is stable.
1801 But not all callers handle ERROR_MARK properly. */
1802 switch (TREE_CODE (TREE_TYPE (expr
)))
1805 error ("struct type value used where scalar is required");
1806 return integer_zero_node
;
1809 error ("union type value used where scalar is required");
1810 return integer_zero_node
;
1813 error ("array type value used where scalar is required");
1814 return integer_zero_node
;
1821 switch (TREE_CODE (expr
))
1823 /* It is simpler and generates better code to have only TRUTH_*_EXPR
1824 or comparison expressions as truth values at this level. */
1827 /* A one-bit unsigned bit-field is already acceptable. */
1828 if (1 == TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (expr
, 1)))
1829 && TREE_UNSIGNED (TREE_OPERAND (expr
, 1)))
1835 /* It is simpler and generates better code to have only TRUTH_*_EXPR
1836 or comparison expressions as truth values at this level. */
1838 if (integer_zerop (TREE_OPERAND (expr
, 1)))
1839 return build_unary_op (TRUTH_NOT_EXPR
, TREE_OPERAND (expr
, 0), 0);
1841 case NE_EXPR
: case LE_EXPR
: case GE_EXPR
: case LT_EXPR
: case GT_EXPR
:
1842 case TRUTH_ANDIF_EXPR
:
1843 case TRUTH_ORIF_EXPR
:
1844 case TRUTH_AND_EXPR
:
1846 case TRUTH_XOR_EXPR
:
1847 return convert (integer_type_node
, expr
);
1853 return integer_zerop (expr
) ? integer_zero_node
: integer_one_node
;
1856 return real_zerop (expr
) ? integer_zero_node
: integer_one_node
;
1859 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr
, 0)))
1860 return build (COMPOUND_EXPR
, integer_type_node
,
1861 TREE_OPERAND (expr
, 0), integer_one_node
);
1863 return integer_one_node
;
1866 return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr
, 1))
1867 ? TRUTH_OR_EXPR
: TRUTH_ORIF_EXPR
),
1868 truthvalue_conversion (TREE_OPERAND (expr
, 0)),
1869 truthvalue_conversion (TREE_OPERAND (expr
, 1)),
1876 /* These don't change whether an object is non-zero or zero. */
1877 return truthvalue_conversion (TREE_OPERAND (expr
, 0));
1881 /* These don't change whether an object is zero or non-zero, but
1882 we can't ignore them if their second arg has side-effects. */
1883 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr
, 1)))
1884 return build (COMPOUND_EXPR
, integer_type_node
, TREE_OPERAND (expr
, 1),
1885 truthvalue_conversion (TREE_OPERAND (expr
, 0)));
1887 return truthvalue_conversion (TREE_OPERAND (expr
, 0));
1890 /* Distribute the conversion into the arms of a COND_EXPR. */
1891 return fold (build (COND_EXPR
, integer_type_node
, TREE_OPERAND (expr
, 0),
1892 truthvalue_conversion (TREE_OPERAND (expr
, 1)),
1893 truthvalue_conversion (TREE_OPERAND (expr
, 2))));
1896 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
1897 since that affects how `default_conversion' will behave. */
1898 if (TREE_CODE (TREE_TYPE (expr
)) == REFERENCE_TYPE
1899 || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0))) == REFERENCE_TYPE
)
1901 /* fall through... */
1903 /* If this is widening the argument, we can ignore it. */
1904 if (TYPE_PRECISION (TREE_TYPE (expr
))
1905 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr
, 0))))
1906 return truthvalue_conversion (TREE_OPERAND (expr
, 0));
1910 /* With IEEE arithmetic, x - x may not equal 0, so we can't optimize
1912 if (TARGET_FLOAT_FORMAT
== IEEE_FLOAT_FORMAT
1913 && TREE_CODE (TREE_TYPE (expr
)) == REAL_TYPE
)
1915 /* fall through... */
1917 /* This and MINUS_EXPR can be changed into a comparison of the
1919 if (TREE_TYPE (TREE_OPERAND (expr
, 0))
1920 == TREE_TYPE (TREE_OPERAND (expr
, 1)))
1921 return build_binary_op (NE_EXPR
, TREE_OPERAND (expr
, 0),
1922 TREE_OPERAND (expr
, 1), 1);
1923 return build_binary_op (NE_EXPR
, TREE_OPERAND (expr
, 0),
1924 fold (build1 (NOP_EXPR
,
1925 TREE_TYPE (TREE_OPERAND (expr
, 0)),
1926 TREE_OPERAND (expr
, 1))), 1);
1929 if (integer_onep (TREE_OPERAND (expr
, 1)))
1933 if (warn_parentheses
&& C_EXP_ORIGINAL_CODE (expr
) == MODIFY_EXPR
)
1934 warning ("suggest parentheses around assignment used as truth value");
1938 if (TREE_CODE (TREE_TYPE (expr
)) == COMPLEX_TYPE
)
1939 return (build_binary_op
1940 ((TREE_SIDE_EFFECTS (expr
)
1941 ? TRUTH_OR_EXPR
: TRUTH_ORIF_EXPR
),
1942 truthvalue_conversion (build_unary_op (REALPART_EXPR
, expr
, 0)),
1943 truthvalue_conversion (build_unary_op (IMAGPART_EXPR
, expr
, 0)),
1946 return build_binary_op (NE_EXPR
, expr
, integer_zero_node
, 1);
1949 /* Read the rest of a #-directive from input stream FINPUT.
1950 In normal use, the directive name and the white space after it
1951 have already been read, so they won't be included in the result.
1952 We allow for the fact that the directive line may contain
1953 a newline embedded within a character or string literal which forms
1954 a part of the directive.
1956 The value is a string in a reusable buffer. It remains valid
1957 only until the next time this function is called. */
1960 get_directive_line (finput
)
1961 register FILE *finput
;
1963 static char *directive_buffer
= NULL
;
1964 static unsigned buffer_length
= 0;
1966 register char *buffer_limit
;
1967 register int looking_for
= 0;
1968 register int char_escaped
= 0;
1970 if (buffer_length
== 0)
1972 directive_buffer
= (char *)xmalloc (128);
1973 buffer_length
= 128;
1976 buffer_limit
= &directive_buffer
[buffer_length
];
1978 for (p
= directive_buffer
; ; )
1982 /* Make buffer bigger if it is full. */
1983 if (p
>= buffer_limit
)
1985 register unsigned bytes_used
= (p
- directive_buffer
);
1989 = (char *)xrealloc (directive_buffer
, buffer_length
);
1990 p
= &directive_buffer
[bytes_used
];
1991 buffer_limit
= &directive_buffer
[buffer_length
];
1996 /* Discard initial whitespace. */
1997 if ((c
== ' ' || c
== '\t') && p
== directive_buffer
)
2000 /* Detect the end of the directive. */
2001 if (c
== '\n' && looking_for
== 0)
2010 return directive_buffer
;
2012 /* Handle string and character constant syntax. */
2015 if (looking_for
== c
&& !char_escaped
)
2016 looking_for
= 0; /* Found terminator... stop looking. */
2019 if (c
== '\'' || c
== '"')
2020 looking_for
= c
; /* Don't stop buffering until we see another
2021 another one of these (or an EOF). */
2023 /* Handle backslash. */
2024 char_escaped
= (c
== '\\' && ! char_escaped
);
2028 /* Make a variant type in the proper way for C/C++, propagating qualifiers
2029 down to the element type of an array. */
2032 c_build_type_variant (type
, constp
, volatilep
)
2034 int constp
, volatilep
;
2036 if (TREE_CODE (type
) == ARRAY_TYPE
)
2038 tree real_main_variant
= TYPE_MAIN_VARIANT (type
);
2040 push_obstacks (TYPE_OBSTACK (real_main_variant
),
2041 TYPE_OBSTACK (real_main_variant
));
2042 type
= build_array_type (c_build_type_variant (TREE_TYPE (type
),
2044 TYPE_DOMAIN (type
));
2046 /* TYPE must be on same obstack as REAL_MAIN_VARIANT. If not,
2047 make a copy. (TYPE might have come from the hash table and
2048 REAL_MAIN_VARIANT might be in some function's obstack.) */
2050 if (TYPE_OBSTACK (type
) != TYPE_OBSTACK (real_main_variant
))
2052 type
= copy_node (type
);
2053 TYPE_POINTER_TO (type
) = TYPE_REFERENCE_TO (type
) = 0;
2056 TYPE_MAIN_VARIANT (type
) = real_main_variant
;
2059 return build_type_variant (type
, constp
, volatilep
);