(all output patterns): Use new capabilities of arm_print_operand to
[official-gcc.git] / gcc / c-common.c
blob425f92e0a752267ec87040fba7f64c89e9fa152b
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)
9 any later version.
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. */
20 #include "config.h"
21 #include "tree.h"
22 #include "c-lex.h"
23 #include "c-tree.h"
24 #include "flags.h"
25 #include "obstack.h"
26 #include <stdio.h>
27 #include <ctype.h>
29 extern struct obstack permanent_obstack;
31 static void declare_hidden_char_array PROTO((char *, char *));
33 /* Make bindings for __FUNCTION__ and __PRETTY_FUNCTION__. */
35 void
36 declare_function_name ()
38 char *name, *printable_name;
40 if (current_function_decl == NULL)
42 name = "";
43 printable_name = "top level";
45 else
47 char *kind = "function";
48 if (TREE_CODE (TREE_TYPE (current_function_decl)) == METHOD_TYPE)
49 kind = "method";
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));
53 else
54 name = "";
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);
62 static void
63 declare_hidden_char_array (name, value)
64 char *name, *value;
66 tree decl, type, init;
67 int vlen;
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
74 || warn_larger_than)
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. */
95 tree
96 combine_strings (strings)
97 tree strings;
99 register tree value, t;
100 register int length = 1;
101 int wide_length = 0;
102 int wide_flag = 0;
103 int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
104 int nchars;
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);
119 wide_flag = 1;
121 else
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. */
127 if (wide_flag)
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. */
136 q = p;
137 for (t = strings; t; t = TREE_CHAIN (t))
139 int len = (TREE_STRING_LENGTH (t)
140 - ((TREE_TYPE (t) == wchar_array_type_node)
141 ? wchar_bytes : 1));
142 if ((TREE_TYPE (t) == wchar_array_type_node) == wide_flag)
144 bcopy (TREE_STRING_POINTER (t), q, len);
145 q += len;
147 else
149 int i;
150 for (i = 0; i < len; i++)
151 ((int *) q)[i] = TREE_STRING_POINTER (t)[i];
152 q += len * wchar_bytes;
155 if (wide_flag)
157 int i;
158 for (i = 0; i < wchar_bytes; i++)
159 *q++ = 0;
161 else
162 *q = 0;
164 value = make_node (STRING_CST);
165 TREE_STRING_POINTER (value) = p;
166 TREE_STRING_LENGTH (value) = length;
167 TREE_CONSTANT (value) = 1;
169 else
171 value = strings;
172 length = TREE_STRING_LENGTH (value);
173 if (TREE_TYPE (value) == wchar_array_type_node)
174 wide_flag = 1;
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))
186 tree elements
187 = build_type_variant (wide_flag ? wchar_type_node : char_type_node,
188 1, 0);
189 TREE_TYPE (value)
190 = build_array_type (elements,
191 build_index_type (build_int_2 (nchars - 1, 0)));
193 else
194 TREE_TYPE (value)
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;
199 return value;
202 /* Process the attributes listed in ATTRIBUTES
203 and install them in DECL. */
205 void
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)))
217 continue;
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. */
224 else
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)
236 = build_pointer_type
237 (build_type_variant (TREE_TYPE (type),
238 TREE_READONLY (TREE_TYPE (type)), 1));
239 else
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)
250 = build_pointer_type
251 (build_type_variant (TREE_TYPE (type), 1,
252 TREE_THIS_VOLATILE (TREE_TYPE (type))));
253 else
254 warning_with_decl (decl, "`const' attribute ignored");
256 else if (TREE_CODE (name) != TREE_LIST)
258 #ifdef VALID_MACHINE_ATTRIBUTE
259 if (VALID_MACHINE_ATTRIBUTE (type, new_attr, name))
261 register tree atlist;
263 for (atlist = new_attr; atlist; atlist = TREE_CHAIN (atlist))
264 if (TREE_VALUE (atlist) == name)
265 goto found_attr;
267 new_attr = tree_cons (NULL_TREE, name, new_attr);
268 found_attr:;
270 else
271 #endif
272 warning ("`%s' attribute directive ignored",
273 IDENTIFIER_POINTER (name));
275 else if ( args = TREE_CHAIN(name),
276 !strcmp (IDENTIFIER_POINTER (name = TREE_PURPOSE (name)), "mode")
277 && list_length (args) == 1
278 && TREE_CODE (TREE_VALUE (args)) == IDENTIFIER_NODE)
280 int i;
281 char *specified_name
282 = IDENTIFIER_POINTER (TREE_VALUE (args));
284 /* Give this decl a type with the specified mode. */
285 for (i = 0; i < NUM_MACHINE_MODES; i++)
286 if (!strcmp (specified_name, GET_MODE_NAME (i)))
288 tree typefm
289 = type_for_mode (i, TREE_UNSIGNED (type));
290 if (typefm != 0)
292 TREE_TYPE (decl) = type = typefm;
293 DECL_SIZE (decl) = 0;
294 layout_decl (decl, 0);
296 else
297 error ("no data type for mode `%s'", specified_name);
298 break;
300 if (i == NUM_MACHINE_MODES)
301 error_with_decl (decl, "unknown machine mode `%s'", specified_name);
303 else if (!strcmp (IDENTIFIER_POINTER (name), "section")
304 && list_length (args) == 1
305 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
307 #ifdef ASM_OUTPUT_SECTION_NAME
308 if (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
310 if (TREE_CODE (decl) == VAR_DECL && current_function_decl != NULL_TREE)
311 error_with_decl (decl,
312 "section attribute cannot be specified for local variables");
313 /* The decl may have already been given a section attribute from
314 a previous declaration. Ensure they match. */
315 else if (DECL_SECTION_NAME (decl) != NULL_TREE
316 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
317 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (args)))) != 0)
318 error_with_decl (decl,
319 "section of `%s' conflicts with previous declaration");
320 else
321 DECL_SECTION_NAME (decl) = TREE_VALUE (TREE_VALUE (args));
323 else
324 error_with_decl (decl,
325 "section attribute not allowed for `%s'");
326 #else
327 error_with_decl (decl, "section attributes are not supported for this target");
328 #endif
330 else if (!strcmp (IDENTIFIER_POINTER (name), "aligned")
331 && list_length (args) == 1
332 && TREE_CODE (TREE_VALUE (args)) == INTEGER_CST)
334 tree align_expr = TREE_VALUE (args);
335 int align;
337 /* Strip any NOPs of any kind. */
338 while (TREE_CODE (align_expr) == NOP_EXPR
339 || TREE_CODE (align_expr) == CONVERT_EXPR
340 || TREE_CODE (align_expr) == NON_LVALUE_EXPR)
341 align_expr = TREE_OPERAND (align_expr, 0);
343 if (TREE_CODE (align_expr) != INTEGER_CST)
345 error_with_decl (decl,
346 "requested alignment of `%s' is not a constant");
347 continue;
350 align = TREE_INT_CST_LOW (align_expr) * BITS_PER_UNIT;
352 if (exact_log2 (align) == -1)
353 error_with_decl (decl,
354 "requested alignment of `%s' is not a power of 2");
355 else if (TREE_CODE (decl) != VAR_DECL
356 && TREE_CODE (decl) != FIELD_DECL)
357 error_with_decl (decl,
358 "alignment specified for `%s'");
359 else
360 DECL_ALIGN (decl) = align;
362 else if (!strcmp (IDENTIFIER_POINTER (name), "format")
363 && list_length (args) == 3
364 && TREE_CODE (TREE_VALUE (args)) == IDENTIFIER_NODE
365 && TREE_CODE (TREE_VALUE (TREE_CHAIN (args))) == INTEGER_CST
366 && TREE_CODE (TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)))) == INTEGER_CST )
368 tree format_type = TREE_VALUE (args);
369 tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
370 tree first_arg_num_expr = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
371 int format_num;
372 int first_arg_num;
373 int is_scan;
374 tree argument;
375 int arg_num;
377 if (TREE_CODE (decl) != FUNCTION_DECL)
379 error_with_decl (decl,
380 "argument format specified for non-function `%s'");
381 continue;
384 if (!strcmp (IDENTIFIER_POINTER (format_type), "printf"))
385 is_scan = 0;
386 else if (!strcmp (IDENTIFIER_POINTER (format_type), "scanf"))
387 is_scan = 1;
388 else
390 error_with_decl (decl, "unrecognized format specifier for `%s'");
391 continue;
394 /* Strip any conversions from the string index and first arg number
395 and verify they are constants. */
396 while (TREE_CODE (format_num_expr) == NOP_EXPR
397 || TREE_CODE (format_num_expr) == CONVERT_EXPR
398 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
399 format_num_expr = TREE_OPERAND (format_num_expr, 0);
401 while (TREE_CODE (first_arg_num_expr) == NOP_EXPR
402 || TREE_CODE (first_arg_num_expr) == CONVERT_EXPR
403 || TREE_CODE (first_arg_num_expr) == NON_LVALUE_EXPR)
404 first_arg_num_expr = TREE_OPERAND (first_arg_num_expr, 0);
406 if (TREE_CODE (format_num_expr) != INTEGER_CST
407 || TREE_CODE (first_arg_num_expr) != INTEGER_CST)
409 error_with_decl (decl,
410 "format string for `%s' has non-constant operand number");
411 continue;
414 format_num = TREE_INT_CST_LOW (format_num_expr);
415 first_arg_num = TREE_INT_CST_LOW (first_arg_num_expr);
416 if (first_arg_num != 0 && first_arg_num <= format_num)
418 error_with_decl (decl,
419 "format string arg follows the args to be formatted, for `%s'");
420 continue;
423 /* If a parameter list is specified, verify that the format_num
424 argument is actually a string, in case the format attribute
425 is in error. */
426 argument = TYPE_ARG_TYPES (type);
427 if (argument)
429 for (arg_num = 1; ; ++arg_num)
431 if (argument == 0 || arg_num == format_num)
432 break;
433 argument = TREE_CHAIN (argument);
435 if (! argument
436 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
437 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
438 != char_type_node))
440 error_with_decl (decl,
441 "format string arg not a string type, for `%s'");
442 continue;
444 if (first_arg_num != 0)
446 /* Verify that first_arg_num points to the last arg, the ... */
447 while (argument)
448 arg_num++, argument = TREE_CHAIN (argument);
449 if (arg_num != first_arg_num)
451 error_with_decl (decl,
452 "args to be formatted is not ..., for `%s'");
453 continue;
458 record_function_format (DECL_NAME (decl), DECL_ASSEMBLER_NAME (decl),
459 is_scan, format_num, first_arg_num);
461 else
462 warning ("`%s' attribute directive ignored",
463 IDENTIFIER_POINTER (name));
465 TREE_TYPE (decl) = build_type_attribute_variant (type, new_attr);
468 /* Check a printf/fprintf/sprintf/scanf/fscanf/sscanf format against
469 a parameter list. */
471 #define T_I &integer_type_node
472 #define T_L &long_integer_type_node
473 #define T_S &short_integer_type_node
474 #define T_UI &unsigned_type_node
475 #define T_UL &long_unsigned_type_node
476 #define T_US &short_unsigned_type_node
477 #define T_F &float_type_node
478 #define T_D &double_type_node
479 #define T_LD &long_double_type_node
480 #define T_C &char_type_node
481 #define T_V &void_type_node
482 #define T_W &wchar_type_node
483 #define T_ST &sizetype
485 typedef struct {
486 char *format_chars;
487 int pointer_count;
488 /* Type of argument if no length modifier is used. */
489 tree *nolen;
490 /* Type of argument if length modifier for shortening is used.
491 If NULL, then this modifier is not allowed. */
492 tree *hlen;
493 /* Type of argument if length modifier `l' is used.
494 If NULL, then this modifier is not allowed. */
495 tree *llen;
496 /* Type of argument if length modifier `L' is used.
497 If NULL, then this modifier is not allowed. */
498 tree *bigllen;
499 /* List of other modifier characters allowed with these options. */
500 char *flag_chars;
501 } format_char_info;
503 static format_char_info print_char_table[] = {
504 { "di", 0, T_I, T_I, T_L, NULL, "-wp0 +" },
505 { "oxX", 0, T_UI, T_UI, T_UL, NULL, "-wp0#" },
506 { "u", 0, T_UI, T_UI, T_UL, NULL, "-wp0" },
507 /* Two GNU extensions. */
508 { "Z", 0, T_ST, NULL, NULL, NULL, "-wp0" },
509 { "m", 0, T_UI, T_UI, T_UL, NULL, "-wp" },
510 { "feEgG", 0, T_D, NULL, NULL, T_LD, "-wp0 +#" },
511 { "c", 0, T_I, NULL, T_W, NULL, "-w" },
512 { "C", 0, T_W, NULL, NULL, NULL, "-w" },
513 { "s", 1, T_C, NULL, T_W, NULL, "-wp" },
514 { "S", 1, T_W, NULL, NULL, NULL, "-wp" },
515 { "p", 1, T_V, NULL, NULL, NULL, "-w" },
516 { "n", 1, T_I, T_S, T_L, NULL, "" },
517 { NULL }
520 static format_char_info scan_char_table[] = {
521 { "di", 1, T_I, T_S, T_L, NULL, "*" },
522 { "ouxX", 1, T_UI, T_US, T_UL, NULL, "*" },
523 { "efgEG", 1, T_F, NULL, T_D, T_LD, "*" },
524 { "sc", 1, T_C, NULL, T_W, NULL, "*a" },
525 { "[", 1, T_C, NULL, NULL, NULL, "*a" },
526 { "C", 1, T_W, NULL, NULL, NULL, "*" },
527 { "S", 1, T_W, NULL, NULL, NULL, "*" },
528 { "p", 2, T_V, NULL, NULL, NULL, "*" },
529 { "n", 1, T_I, T_S, T_L, NULL, "" },
530 { NULL }
533 typedef struct function_format_info {
534 struct function_format_info *next; /* next structure on the list */
535 tree name; /* identifier such as "printf" */
536 tree assembler_name; /* optional mangled identifier (for C++) */
537 int is_scan; /* TRUE if *scanf */
538 int format_num; /* number of format argument */
539 int first_arg_num; /* number of first arg (zero for varargs) */
540 } function_format_info;
542 static function_format_info *function_format_list = NULL;
544 static void check_format_info PROTO((function_format_info *, tree));
546 /* Initialize the table of functions to perform format checking on.
547 The ANSI functions are always checked (whether <stdio.h> is
548 included or not), since it is common to call printf without
549 including <stdio.h>. There shouldn't be a problem with this,
550 since ANSI reserves these function names whether you include the
551 header file or not. In any case, the checking is harmless. */
553 void
554 init_function_format_info ()
556 record_function_format (get_identifier ("printf"), NULL_TREE, 0, 1, 2);
557 record_function_format (get_identifier ("fprintf"), NULL_TREE, 0, 2, 3);
558 record_function_format (get_identifier ("sprintf"), NULL_TREE, 0, 2, 3);
559 record_function_format (get_identifier ("scanf"), NULL_TREE, 1, 1, 2);
560 record_function_format (get_identifier ("fscanf"), NULL_TREE, 1, 2, 3);
561 record_function_format (get_identifier ("sscanf"), NULL_TREE, 1, 2, 3);
562 record_function_format (get_identifier ("vprintf"), NULL_TREE, 0, 1, 0);
563 record_function_format (get_identifier ("vfprintf"), NULL_TREE, 0, 2, 0);
564 record_function_format (get_identifier ("vsprintf"), NULL_TREE, 0, 2, 0);
567 /* Record information for argument format checking. FUNCTION_IDENT is
568 the identifier node for the name of the function to check (its decl
569 need not exist yet). IS_SCAN is true for scanf-type format checking;
570 false indicates printf-style format checking. FORMAT_NUM is the number
571 of the argument which is the format control string (starting from 1).
572 FIRST_ARG_NUM is the number of the first actual argument to check
573 against teh format string, or zero if no checking is not be done
574 (e.g. for varargs such as vfprintf). */
576 void
577 record_function_format (name, assembler_name, is_scan,
578 format_num, first_arg_num)
579 tree name;
580 tree assembler_name;
581 int is_scan;
582 int format_num;
583 int first_arg_num;
585 function_format_info *info;
587 /* Re-use existing structure if it's there. */
589 for (info = function_format_list; info; info = info->next)
591 if (info->name == name && info->assembler_name == assembler_name)
592 break;
594 if (! info)
596 info = (function_format_info *) xmalloc (sizeof (function_format_info));
597 info->next = function_format_list;
598 function_format_list = info;
600 info->name = name;
601 info->assembler_name = assembler_name;
604 info->is_scan = is_scan;
605 info->format_num = format_num;
606 info->first_arg_num = first_arg_num;
609 static char tfaff[] = "too few arguments for format";
611 /* Check the argument list of a call to printf, scanf, etc.
612 NAME is the function identifier.
613 ASSEMBLER_NAME is the function's assembler identifier.
614 (Either NAME or ASSEMBLER_NAME, but not both, may be NULL_TREE.)
615 PARAMS is the list of argument values. */
617 void
618 check_function_format (name, assembler_name, params)
619 tree name;
620 tree assembler_name;
621 tree params;
623 function_format_info *info;
625 /* See if this function is a format function. */
626 for (info = function_format_list; info; info = info->next)
628 if (info->assembler_name
629 ? (info->assembler_name == assembler_name)
630 : (info->name == name))
632 /* Yup; check it. */
633 check_format_info (info, params);
634 break;
639 /* Check the argument list of a call to printf, scanf, etc.
640 INFO points to the function_format_info structure.
641 PARAMS is the list of argument values. */
643 static void
644 check_format_info (info, params)
645 function_format_info *info;
646 tree params;
648 int i;
649 int arg_num;
650 int suppressed, wide, precise;
651 int length_char;
652 int format_char;
653 int format_length;
654 tree format_tree;
655 tree cur_param;
656 tree cur_type;
657 tree wanted_type;
658 tree first_fillin_param;
659 char *format_chars;
660 format_char_info *fci;
661 static char message[132];
662 char flag_chars[8];
663 int has_operand_number = 0;
665 /* Skip to format argument. If the argument isn't available, there's
666 no work for us to do; prototype checking will catch the problem. */
667 for (arg_num = 1; ; ++arg_num)
669 if (params == 0)
670 return;
671 if (arg_num == info->format_num)
672 break;
673 params = TREE_CHAIN (params);
675 format_tree = TREE_VALUE (params);
676 params = TREE_CHAIN (params);
677 if (format_tree == 0)
678 return;
679 /* We can only check the format if it's a string constant. */
680 while (TREE_CODE (format_tree) == NOP_EXPR)
681 format_tree = TREE_OPERAND (format_tree, 0); /* strip coercion */
682 if (format_tree == null_pointer_node)
684 warning ("null format string");
685 return;
687 if (TREE_CODE (format_tree) != ADDR_EXPR)
688 return;
689 format_tree = TREE_OPERAND (format_tree, 0);
690 if (TREE_CODE (format_tree) != STRING_CST)
691 return;
692 format_chars = TREE_STRING_POINTER (format_tree);
693 format_length = TREE_STRING_LENGTH (format_tree);
694 if (format_length <= 1)
695 warning ("zero-length format string");
696 if (format_chars[--format_length] != 0)
698 warning ("unterminated format string");
699 return;
701 /* Skip to first argument to check. */
702 while (arg_num + 1 < info->first_arg_num)
704 if (params == 0)
705 return;
706 params = TREE_CHAIN (params);
707 ++arg_num;
710 first_fillin_param = params;
711 while (1)
713 int aflag;
714 if (*format_chars == 0)
716 if (format_chars - TREE_STRING_POINTER (format_tree) != format_length)
717 warning ("embedded `\\0' in format");
718 if (info->first_arg_num != 0 && params != 0 && ! has_operand_number)
719 warning ("too many arguments for format");
720 return;
722 if (*format_chars++ != '%')
723 continue;
724 if (*format_chars == 0)
726 warning ("spurious trailing `%%' in format");
727 continue;
729 if (*format_chars == '%')
731 ++format_chars;
732 continue;
734 flag_chars[0] = 0;
735 suppressed = wide = precise = FALSE;
736 if (info->is_scan)
738 suppressed = *format_chars == '*';
739 if (suppressed)
740 ++format_chars;
741 while (isdigit (*format_chars))
742 ++format_chars;
744 else
746 /* See if we have a number followed by a dollar sign. If we do,
747 it is an operand number, so set PARAMS to that operand. */
748 if (*format_chars >= '0' && *format_chars <= '9')
750 char *p = format_chars;
752 while (*p >= '0' && *p++ <= '9')
755 if (*p == '$')
757 int opnum = atoi (format_chars);
759 params = first_fillin_param;
760 format_chars = p + 1;
761 has_operand_number = 1;
763 for (i = 1; i < opnum && params != 0; i++)
764 params = TREE_CHAIN (params);
766 if (opnum == 0 || params == 0)
768 warning ("operand number out of range in format");
769 return;
774 while (*format_chars != 0 && index (" +#0-", *format_chars) != 0)
776 if (index (flag_chars, *format_chars) != 0)
778 sprintf (message, "repeated `%c' flag in format",
779 *format_chars);
780 warning (message);
782 i = strlen (flag_chars);
783 flag_chars[i++] = *format_chars++;
784 flag_chars[i] = 0;
786 /* "If the space and + flags both appear,
787 the space flag will be ignored." */
788 if (index (flag_chars, ' ') != 0
789 && index (flag_chars, '+') != 0)
790 warning ("use of both ` ' and `+' flags in format");
791 /* "If the 0 and - flags both appear,
792 the 0 flag will be ignored." */
793 if (index (flag_chars, '0') != 0
794 && index (flag_chars, '-') != 0)
795 warning ("use of both `0' and `-' flags in format");
796 if (*format_chars == '*')
798 wide = TRUE;
799 /* "...a field width...may be indicated by an asterisk.
800 In this case, an int argument supplies the field width..." */
801 ++format_chars;
802 if (params == 0)
804 warning (tfaff);
805 return;
807 if (info->first_arg_num != 0)
809 cur_param = TREE_VALUE (params);
810 params = TREE_CHAIN (params);
811 ++arg_num;
812 /* size_t is generally not valid here.
813 It will work on most machines, because size_t and int
814 have the same mode. But might as well warn anyway,
815 since it will fail on other machines. */
816 if ((TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
817 != integer_type_node)
819 (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
820 != unsigned_type_node))
822 sprintf (message,
823 "field width is not type int (arg %d)",
824 arg_num);
825 warning (message);
829 else
831 while (isdigit (*format_chars))
833 wide = TRUE;
834 ++format_chars;
837 if (*format_chars == '.')
839 precise = TRUE;
840 ++format_chars;
841 if (*format_chars != '*' && !isdigit (*format_chars))
842 warning ("`.' not followed by `*' or digit in format");
843 /* "...a...precision...may be indicated by an asterisk.
844 In this case, an int argument supplies the...precision." */
845 if (*format_chars == '*')
847 if (info->first_arg_num != 0)
849 ++format_chars;
850 if (params == 0)
852 warning (tfaff);
853 return;
855 cur_param = TREE_VALUE (params);
856 params = TREE_CHAIN (params);
857 ++arg_num;
858 if (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
859 != integer_type_node)
861 sprintf (message,
862 "field width is not type int (arg %d)",
863 arg_num);
864 warning (message);
868 else
870 while (isdigit (*format_chars))
871 ++format_chars;
875 if (*format_chars == 'h' || *format_chars == 'l' || *format_chars == 'L')
876 length_char = *format_chars++;
877 else
878 length_char = 0;
879 aflag = 0;
880 if (*format_chars == 'a')
882 aflag = 1;
883 format_chars++;
885 if (suppressed && length_char != 0)
887 sprintf (message,
888 "use of `*' and `%c' together in format",
889 length_char);
890 warning (message);
892 format_char = *format_chars;
893 if (format_char == 0)
895 warning ("conversion lacks type at end of format");
896 continue;
898 format_chars++;
899 fci = info->is_scan ? scan_char_table : print_char_table;
900 while (fci->format_chars != 0
901 && index (fci->format_chars, format_char) == 0)
902 ++fci;
903 if (fci->format_chars == 0)
905 if (format_char >= 040 && format_char < 0177)
906 sprintf (message,
907 "unknown conversion type character `%c' in format",
908 format_char);
909 else
910 sprintf (message,
911 "unknown conversion type character 0x%x in format",
912 format_char);
913 warning (message);
914 continue;
916 if (wide && index (fci->flag_chars, 'w') == 0)
918 sprintf (message, "width used with `%c' format",
919 format_char);
920 warning (message);
922 if (precise && index (fci->flag_chars, 'p') == 0)
924 sprintf (message, "precision used with `%c' format",
925 format_char);
926 warning (message);
928 if (aflag && index (fci->flag_chars, 'a') == 0)
930 sprintf (message, "`a' flag used with `%c' format",
931 format_char);
932 warning (message);
934 if (info->is_scan && format_char == '[')
936 /* Skip over scan set, in case it happens to have '%' in it. */
937 if (*format_chars == '^')
938 ++format_chars;
939 /* Find closing bracket; if one is hit immediately, then
940 it's part of the scan set rather than a terminator. */
941 if (*format_chars == ']')
942 ++format_chars;
943 while (*format_chars && *format_chars != ']')
944 ++format_chars;
945 if (*format_chars != ']')
946 /* The end of the format string was reached. */
947 warning ("no closing `]' for `%%[' format");
949 if (suppressed)
951 if (index (fci->flag_chars, '*') == 0)
953 sprintf (message,
954 "suppression of `%c' conversion in format",
955 format_char);
956 warning (message);
958 continue;
960 for (i = 0; flag_chars[i] != 0; ++i)
962 if (index (fci->flag_chars, flag_chars[i]) == 0)
964 sprintf (message, "flag `%c' used with type `%c'",
965 flag_chars[i], format_char);
966 warning (message);
969 if (precise && index (flag_chars, '0') != 0
970 && (format_char == 'd' || format_char == 'i'
971 || format_char == 'o' || format_char == 'u'
972 || format_char == 'x' || format_char == 'x'))
974 sprintf (message,
975 "precision and `0' flag not both allowed with `%c' format",
976 format_char);
977 warning (message);
979 switch (length_char)
981 default: wanted_type = fci->nolen ? *(fci->nolen) : 0; break;
982 case 'h': wanted_type = fci->hlen ? *(fci->hlen) : 0; break;
983 case 'l': wanted_type = fci->llen ? *(fci->llen) : 0; break;
984 case 'L': wanted_type = fci->bigllen ? *(fci->bigllen) : 0; break;
986 if (wanted_type == 0)
988 sprintf (message,
989 "use of `%c' length character with `%c' type character",
990 length_char, format_char);
991 warning (message);
995 ** XXX -- should kvetch about stuff such as
996 ** {
997 ** const int i;
999 ** scanf ("%d", &i);
1000 ** }
1003 /* Finally. . .check type of argument against desired type! */
1004 if (info->first_arg_num == 0)
1005 continue;
1006 if (params == 0)
1008 warning (tfaff);
1009 return;
1011 cur_param = TREE_VALUE (params);
1012 params = TREE_CHAIN (params);
1013 ++arg_num;
1014 cur_type = TREE_TYPE (cur_param);
1016 /* Check the types of any additional pointer arguments
1017 that precede the "real" argument. */
1018 for (i = 0; i < fci->pointer_count; ++i)
1020 if (TREE_CODE (cur_type) == POINTER_TYPE)
1022 cur_type = TREE_TYPE (cur_type);
1023 continue;
1025 sprintf (message,
1026 "format argument is not a %s (arg %d)",
1027 ((fci->pointer_count == 1) ? "pointer" : "pointer to a pointer"),
1028 arg_num);
1029 warning (message);
1030 break;
1033 /* Check the type of the "real" argument, if there's a type we want. */
1034 if (i == fci->pointer_count && wanted_type != 0
1035 && wanted_type != TYPE_MAIN_VARIANT (cur_type)
1036 /* If we want `void *', allow any pointer type.
1037 (Anything else would already have got a warning.) */
1038 && ! (wanted_type == void_type_node
1039 && fci->pointer_count > 0)
1040 /* Don't warn about differences merely in signedness. */
1041 && !(TREE_CODE (wanted_type) == INTEGER_TYPE
1042 && TREE_CODE (TYPE_MAIN_VARIANT (cur_type)) == INTEGER_TYPE
1043 && (TREE_UNSIGNED (wanted_type)
1044 ? wanted_type == (cur_type = unsigned_type (cur_type))
1045 : wanted_type == (cur_type = signed_type (cur_type))))
1046 /* Likewise, "signed char", "unsigned char" and "char" are
1047 equivalent but the above test won't consider them equivalent. */
1048 && ! (wanted_type == char_type_node
1049 && (TYPE_MAIN_VARIANT (cur_type) == signed_char_type_node
1050 || TYPE_MAIN_VARIANT (cur_type) == unsigned_char_type_node)))
1052 register char *this;
1053 register char *that;
1055 this = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (wanted_type)));
1056 that = 0;
1057 if (TREE_CODE (cur_type) != ERROR_MARK
1058 && TYPE_NAME (cur_type) != 0
1059 && TREE_CODE (cur_type) != INTEGER_TYPE
1060 && !(TREE_CODE (cur_type) == POINTER_TYPE
1061 && TREE_CODE (TREE_TYPE (cur_type)) == INTEGER_TYPE))
1063 if (TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL
1064 && DECL_NAME (TYPE_NAME (cur_type)) != 0)
1065 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
1066 else
1067 that = IDENTIFIER_POINTER (TYPE_NAME (cur_type));
1070 /* A nameless type can't possibly match what the format wants.
1071 So there will be a warning for it.
1072 Make up a string to describe vaguely what it is. */
1073 if (that == 0)
1075 if (TREE_CODE (cur_type) == POINTER_TYPE)
1076 that = "pointer";
1077 else
1078 that = "different type";
1081 /* Make the warning better in case of mismatch of int vs long. */
1082 if (TREE_CODE (cur_type) == INTEGER_TYPE
1083 && TREE_CODE (wanted_type) == INTEGER_TYPE
1084 && TYPE_PRECISION (cur_type) == TYPE_PRECISION (wanted_type)
1085 && TYPE_NAME (cur_type) != 0
1086 && TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL)
1087 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
1089 if (strcmp (this, that) != 0)
1091 sprintf (message, "%s format, %s arg (arg %d)",
1092 this, that, arg_num);
1093 warning (message);
1099 /* Print a warning if a constant expression had overflow in folding.
1100 Invoke this function on every expression that the language
1101 requires to be a constant expression.
1102 Note the ANSI C standard says it is erroneous for a
1103 constant expression to overflow. */
1105 void
1106 constant_expression_warning (value)
1107 tree value;
1109 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1110 || TREE_CODE (value) == COMPLEX_CST)
1111 && TREE_CONSTANT_OVERFLOW (value) && pedantic)
1112 pedwarn ("overflow in constant expression");
1115 /* Print a warning if an expression had overflow in folding.
1116 Invoke this function on every expression that
1117 (1) appears in the source code, and
1118 (2) might be a constant expression that overflowed, and
1119 (3) is not already checked by convert_and_check;
1120 however, do not invoke this function on operands of explicit casts. */
1122 void
1123 overflow_warning (value)
1124 tree value;
1126 if ((TREE_CODE (value) == INTEGER_CST
1127 || (TREE_CODE (value) == COMPLEX_CST
1128 && TREE_CODE (TREE_REALPART (value)) == INTEGER_CST))
1129 && TREE_OVERFLOW (value))
1131 TREE_OVERFLOW (value) = 0;
1132 warning ("integer overflow in expression");
1134 else if ((TREE_CODE (value) == REAL_CST
1135 || (TREE_CODE (value) == COMPLEX_CST
1136 && TREE_CODE (TREE_REALPART (value)) == REAL_CST))
1137 && TREE_OVERFLOW (value))
1139 TREE_OVERFLOW (value) = 0;
1140 warning ("floating-pointer overflow in expression");
1144 /* Print a warning if a large constant is truncated to unsigned,
1145 or if -Wconversion is used and a constant < 0 is converted to unsigned.
1146 Invoke this function on every expression that might be implicitly
1147 converted to an unsigned type. */
1149 void
1150 unsigned_conversion_warning (result, operand)
1151 tree result, operand;
1153 if (TREE_CODE (operand) == INTEGER_CST
1154 && TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
1155 && TREE_UNSIGNED (TREE_TYPE (result))
1156 && !int_fits_type_p (operand, TREE_TYPE (result)))
1158 if (!int_fits_type_p (operand, signed_type (TREE_TYPE (result))))
1159 /* This detects cases like converting -129 or 256 to unsigned char. */
1160 warning ("large integer implicitly truncated to unsigned type");
1161 else if (warn_conversion)
1162 warning ("negative integer implicitly converted to unsigned type");
1166 /* Convert EXPR to TYPE, warning about conversion problems with constants.
1167 Invoke this function on every expression that is converted implicitly,
1168 i.e. because of language rules and not because of an explicit cast. */
1170 tree
1171 convert_and_check (type, expr)
1172 tree type, expr;
1174 tree t = convert (type, expr);
1175 if (TREE_CODE (t) == INTEGER_CST)
1177 if (TREE_OVERFLOW (t))
1179 TREE_OVERFLOW (t) = 0;
1181 /* No warning for converting 0x80000000 to int. */
1182 if (!(TREE_UNSIGNED (type) < TREE_UNSIGNED (TREE_TYPE (expr))
1183 && TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
1184 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (expr))))
1185 /* If EXPR fits in the unsigned version of TYPE,
1186 don't warn unless pedantic. */
1187 if (pedantic
1188 || TREE_UNSIGNED (type)
1189 || ! int_fits_type_p (expr, unsigned_type (type)))
1190 warning ("overflow in implicit constant conversion");
1192 else
1193 unsigned_conversion_warning (t, expr);
1195 return t;
1198 void
1199 c_expand_expr_stmt (expr)
1200 tree expr;
1202 /* Do default conversion if safe and possibly important,
1203 in case within ({...}). */
1204 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE && lvalue_p (expr))
1205 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
1206 expr = default_conversion (expr);
1208 if (TREE_TYPE (expr) != error_mark_node
1209 && TYPE_SIZE (TREE_TYPE (expr)) == 0
1210 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
1211 error ("expression statement has incomplete type");
1213 expand_expr_stmt (expr);
1216 /* Validate the expression after `case' and apply default promotions. */
1218 tree
1219 check_case_value (value)
1220 tree value;
1222 if (value == NULL_TREE)
1223 return value;
1225 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1226 STRIP_TYPE_NOPS (value);
1228 if (TREE_CODE (value) != INTEGER_CST
1229 && value != error_mark_node)
1231 error ("case label does not reduce to an integer constant");
1232 value = error_mark_node;
1234 else
1235 /* Promote char or short to int. */
1236 value = default_conversion (value);
1238 constant_expression_warning (value);
1240 return value;
1243 /* Return an integer type with BITS bits of precision,
1244 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
1246 tree
1247 type_for_size (bits, unsignedp)
1248 unsigned bits;
1249 int unsignedp;
1251 if (bits == TYPE_PRECISION (signed_char_type_node))
1252 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1254 if (bits == TYPE_PRECISION (short_integer_type_node))
1255 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1257 if (bits == TYPE_PRECISION (integer_type_node))
1258 return unsignedp ? unsigned_type_node : integer_type_node;
1260 if (bits == TYPE_PRECISION (long_integer_type_node))
1261 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1263 if (bits == TYPE_PRECISION (long_long_integer_type_node))
1264 return (unsignedp ? long_long_unsigned_type_node
1265 : long_long_integer_type_node);
1267 if (bits <= TYPE_PRECISION (intQI_type_node))
1268 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
1270 if (bits <= TYPE_PRECISION (intHI_type_node))
1271 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
1273 if (bits <= TYPE_PRECISION (intSI_type_node))
1274 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
1276 if (bits <= TYPE_PRECISION (intDI_type_node))
1277 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
1279 return 0;
1282 /* Return a data type that has machine mode MODE.
1283 If the mode is an integer,
1284 then UNSIGNEDP selects between signed and unsigned types. */
1286 tree
1287 type_for_mode (mode, unsignedp)
1288 enum machine_mode mode;
1289 int unsignedp;
1291 if (mode == TYPE_MODE (signed_char_type_node))
1292 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1294 if (mode == TYPE_MODE (short_integer_type_node))
1295 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1297 if (mode == TYPE_MODE (integer_type_node))
1298 return unsignedp ? unsigned_type_node : integer_type_node;
1300 if (mode == TYPE_MODE (long_integer_type_node))
1301 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1303 if (mode == TYPE_MODE (long_long_integer_type_node))
1304 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
1306 if (mode == TYPE_MODE (intQI_type_node))
1307 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
1309 if (mode == TYPE_MODE (intHI_type_node))
1310 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
1312 if (mode == TYPE_MODE (intSI_type_node))
1313 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
1315 if (mode == TYPE_MODE (intDI_type_node))
1316 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
1318 if (mode == TYPE_MODE (float_type_node))
1319 return float_type_node;
1321 if (mode == TYPE_MODE (double_type_node))
1322 return double_type_node;
1324 if (mode == TYPE_MODE (long_double_type_node))
1325 return long_double_type_node;
1327 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
1328 return build_pointer_type (char_type_node);
1330 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
1331 return build_pointer_type (integer_type_node);
1333 return 0;
1336 /* Print an error message for invalid operands to arith operation CODE.
1337 NOP_EXPR is used as a special case (see truthvalue_conversion). */
1339 void
1340 binary_op_error (code)
1341 enum tree_code code;
1343 register char *opname = "unknown";
1345 switch (code)
1347 case NOP_EXPR:
1348 error ("invalid truth-value expression");
1349 return;
1351 case PLUS_EXPR:
1352 opname = "+"; break;
1353 case MINUS_EXPR:
1354 opname = "-"; break;
1355 case MULT_EXPR:
1356 opname = "*"; break;
1357 case MAX_EXPR:
1358 opname = "max"; break;
1359 case MIN_EXPR:
1360 opname = "min"; break;
1361 case EQ_EXPR:
1362 opname = "=="; break;
1363 case NE_EXPR:
1364 opname = "!="; break;
1365 case LE_EXPR:
1366 opname = "<="; break;
1367 case GE_EXPR:
1368 opname = ">="; break;
1369 case LT_EXPR:
1370 opname = "<"; break;
1371 case GT_EXPR:
1372 opname = ">"; break;
1373 case LSHIFT_EXPR:
1374 opname = "<<"; break;
1375 case RSHIFT_EXPR:
1376 opname = ">>"; break;
1377 case TRUNC_MOD_EXPR:
1378 case FLOOR_MOD_EXPR:
1379 opname = "%"; break;
1380 case TRUNC_DIV_EXPR:
1381 case FLOOR_DIV_EXPR:
1382 opname = "/"; break;
1383 case BIT_AND_EXPR:
1384 opname = "&"; break;
1385 case BIT_IOR_EXPR:
1386 opname = "|"; break;
1387 case TRUTH_ANDIF_EXPR:
1388 opname = "&&"; break;
1389 case TRUTH_ORIF_EXPR:
1390 opname = "||"; break;
1391 case BIT_XOR_EXPR:
1392 opname = "^"; break;
1393 case LROTATE_EXPR:
1394 case RROTATE_EXPR:
1395 opname = "rotate"; break;
1397 error ("invalid operands to binary %s", opname);
1400 /* Subroutine of build_binary_op, used for comparison operations.
1401 See if the operands have both been converted from subword integer types
1402 and, if so, perhaps change them both back to their original type.
1403 This function is also responsible for converting the two operands
1404 to the proper common type for comparison.
1406 The arguments of this function are all pointers to local variables
1407 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
1408 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
1410 If this function returns nonzero, it means that the comparison has
1411 a constant value. What this function returns is an expression for
1412 that value. */
1414 tree
1415 shorten_compare (op0_ptr, op1_ptr, restype_ptr, rescode_ptr)
1416 tree *op0_ptr, *op1_ptr;
1417 tree *restype_ptr;
1418 enum tree_code *rescode_ptr;
1420 register tree type;
1421 tree op0 = *op0_ptr;
1422 tree op1 = *op1_ptr;
1423 int unsignedp0, unsignedp1;
1424 int real1, real2;
1425 tree primop0, primop1;
1426 enum tree_code code = *rescode_ptr;
1428 /* Throw away any conversions to wider types
1429 already present in the operands. */
1431 primop0 = get_narrower (op0, &unsignedp0);
1432 primop1 = get_narrower (op1, &unsignedp1);
1434 /* Handle the case that OP0 does not *contain* a conversion
1435 but it *requires* conversion to FINAL_TYPE. */
1437 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
1438 unsignedp0 = TREE_UNSIGNED (TREE_TYPE (op0));
1439 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
1440 unsignedp1 = TREE_UNSIGNED (TREE_TYPE (op1));
1442 /* If one of the operands must be floated, we cannot optimize. */
1443 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
1444 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
1446 /* If first arg is constant, swap the args (changing operation
1447 so value is preserved), for canonicalization. */
1449 if (TREE_CONSTANT (primop0))
1451 register tree tem = primop0;
1452 register int temi = unsignedp0;
1453 primop0 = primop1;
1454 primop1 = tem;
1455 tem = op0;
1456 op0 = op1;
1457 op1 = tem;
1458 *op0_ptr = op0;
1459 *op1_ptr = op1;
1460 unsignedp0 = unsignedp1;
1461 unsignedp1 = temi;
1462 temi = real1;
1463 real1 = real2;
1464 real2 = temi;
1466 switch (code)
1468 case LT_EXPR:
1469 code = GT_EXPR;
1470 break;
1471 case GT_EXPR:
1472 code = LT_EXPR;
1473 break;
1474 case LE_EXPR:
1475 code = GE_EXPR;
1476 break;
1477 case GE_EXPR:
1478 code = LE_EXPR;
1479 break;
1481 *rescode_ptr = code;
1484 /* If comparing an integer against a constant more bits wide,
1485 maybe we can deduce a value of 1 or 0 independent of the data.
1486 Or else truncate the constant now
1487 rather than extend the variable at run time.
1489 This is only interesting if the constant is the wider arg.
1490 Also, it is not safe if the constant is unsigned and the
1491 variable arg is signed, since in this case the variable
1492 would be sign-extended and then regarded as unsigned.
1493 Our technique fails in this case because the lowest/highest
1494 possible unsigned results don't follow naturally from the
1495 lowest/highest possible values of the variable operand.
1496 For just EQ_EXPR and NE_EXPR there is another technique that
1497 could be used: see if the constant can be faithfully represented
1498 in the other operand's type, by truncating it and reextending it
1499 and see if that preserves the constant's value. */
1501 if (!real1 && !real2
1502 && TREE_CODE (primop1) == INTEGER_CST
1503 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
1505 int min_gt, max_gt, min_lt, max_lt;
1506 tree maxval, minval;
1507 /* 1 if comparison is nominally unsigned. */
1508 int unsignedp = TREE_UNSIGNED (*restype_ptr);
1509 tree val;
1511 type = signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0));
1513 maxval = TYPE_MAX_VALUE (type);
1514 minval = TYPE_MIN_VALUE (type);
1516 if (unsignedp && !unsignedp0)
1517 *restype_ptr = signed_type (*restype_ptr);
1519 if (TREE_TYPE (primop1) != *restype_ptr)
1520 primop1 = convert (*restype_ptr, primop1);
1521 if (type != *restype_ptr)
1523 minval = convert (*restype_ptr, minval);
1524 maxval = convert (*restype_ptr, maxval);
1527 if (unsignedp && unsignedp0)
1529 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
1530 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
1531 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
1532 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
1534 else
1536 min_gt = INT_CST_LT (primop1, minval);
1537 max_gt = INT_CST_LT (primop1, maxval);
1538 min_lt = INT_CST_LT (minval, primop1);
1539 max_lt = INT_CST_LT (maxval, primop1);
1542 val = 0;
1543 /* This used to be a switch, but Genix compiler can't handle that. */
1544 if (code == NE_EXPR)
1546 if (max_lt || min_gt)
1547 val = integer_one_node;
1549 else if (code == EQ_EXPR)
1551 if (max_lt || min_gt)
1552 val = integer_zero_node;
1554 else if (code == LT_EXPR)
1556 if (max_lt)
1557 val = integer_one_node;
1558 if (!min_lt)
1559 val = integer_zero_node;
1561 else if (code == GT_EXPR)
1563 if (min_gt)
1564 val = integer_one_node;
1565 if (!max_gt)
1566 val = integer_zero_node;
1568 else if (code == LE_EXPR)
1570 if (!max_gt)
1571 val = integer_one_node;
1572 if (min_gt)
1573 val = integer_zero_node;
1575 else if (code == GE_EXPR)
1577 if (!min_lt)
1578 val = integer_one_node;
1579 if (max_lt)
1580 val = integer_zero_node;
1583 /* If primop0 was sign-extended and unsigned comparison specd,
1584 we did a signed comparison above using the signed type bounds.
1585 But the comparison we output must be unsigned.
1587 Also, for inequalities, VAL is no good; but if the signed
1588 comparison had *any* fixed result, it follows that the
1589 unsigned comparison just tests the sign in reverse
1590 (positive values are LE, negative ones GE).
1591 So we can generate an unsigned comparison
1592 against an extreme value of the signed type. */
1594 if (unsignedp && !unsignedp0)
1596 if (val != 0)
1597 switch (code)
1599 case LT_EXPR:
1600 case GE_EXPR:
1601 primop1 = TYPE_MIN_VALUE (type);
1602 val = 0;
1603 break;
1605 case LE_EXPR:
1606 case GT_EXPR:
1607 primop1 = TYPE_MAX_VALUE (type);
1608 val = 0;
1609 break;
1611 type = unsigned_type (type);
1614 if (!max_gt && !unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
1616 /* This is the case of (char)x >?< 0x80, which people used to use
1617 expecting old C compilers to change the 0x80 into -0x80. */
1618 if (val == integer_zero_node)
1619 warning ("comparison is always 0 due to limited range of data type");
1620 if (val == integer_one_node)
1621 warning ("comparison is always 1 due to limited range of data type");
1624 if (!min_lt && unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
1626 /* This is the case of (unsigned char)x >?< -1 or < 0. */
1627 if (val == integer_zero_node)
1628 warning ("comparison is always 0 due to limited range of data type");
1629 if (val == integer_one_node)
1630 warning ("comparison is always 1 due to limited range of data type");
1633 if (val != 0)
1635 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
1636 if (TREE_SIDE_EFFECTS (primop0))
1637 return build (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
1638 return val;
1641 /* Value is not predetermined, but do the comparison
1642 in the type of the operand that is not constant.
1643 TYPE is already properly set. */
1645 else if (real1 && real2
1646 && (TYPE_PRECISION (TREE_TYPE (primop0))
1647 == TYPE_PRECISION (TREE_TYPE (primop1))))
1648 type = TREE_TYPE (primop0);
1650 /* If args' natural types are both narrower than nominal type
1651 and both extend in the same manner, compare them
1652 in the type of the wider arg.
1653 Otherwise must actually extend both to the nominal
1654 common type lest different ways of extending
1655 alter the result.
1656 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
1658 else if (unsignedp0 == unsignedp1 && real1 == real2
1659 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
1660 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
1662 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
1663 type = signed_or_unsigned_type (unsignedp0
1664 || TREE_UNSIGNED (*restype_ptr),
1665 type);
1666 /* Make sure shorter operand is extended the right way
1667 to match the longer operand. */
1668 primop0 = convert (signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0)),
1669 primop0);
1670 primop1 = convert (signed_or_unsigned_type (unsignedp1, TREE_TYPE (primop1)),
1671 primop1);
1673 else
1675 /* Here we must do the comparison on the nominal type
1676 using the args exactly as we received them. */
1677 type = *restype_ptr;
1678 primop0 = op0;
1679 primop1 = op1;
1681 if (!real1 && !real2 && integer_zerop (primop1)
1682 && TREE_UNSIGNED (*restype_ptr))
1684 tree value = 0;
1685 switch (code)
1687 case GE_EXPR:
1688 if (extra_warnings)
1689 warning ("unsigned value >= 0 is always 1");
1690 value = integer_one_node;
1691 break;
1693 case LT_EXPR:
1694 if (extra_warnings)
1695 warning ("unsigned value < 0 is always 0");
1696 value = integer_zero_node;
1699 if (value != 0)
1701 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
1702 if (TREE_SIDE_EFFECTS (primop0))
1703 return build (COMPOUND_EXPR, TREE_TYPE (value),
1704 primop0, value);
1705 return value;
1710 *op0_ptr = convert (type, primop0);
1711 *op1_ptr = convert (type, primop1);
1713 *restype_ptr = integer_type_node;
1715 return 0;
1718 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
1719 or validate its data type for an `if' or `while' statement or ?..: exp.
1721 This preparation consists of taking the ordinary
1722 representation of an expression expr and producing a valid tree
1723 boolean expression describing whether expr is nonzero. We could
1724 simply always do build_binary_op (NE_EXPR, expr, integer_zero_node, 1),
1725 but we optimize comparisons, &&, ||, and !.
1727 The resulting type should always be `integer_type_node'. */
1729 tree
1730 truthvalue_conversion (expr)
1731 tree expr;
1733 if (TREE_CODE (expr) == ERROR_MARK)
1734 return expr;
1736 #if 0 /* This appears to be wrong for C++. */
1737 /* These really should return error_mark_node after 2.4 is stable.
1738 But not all callers handle ERROR_MARK properly. */
1739 switch (TREE_CODE (TREE_TYPE (expr)))
1741 case RECORD_TYPE:
1742 error ("struct type value used where scalar is required");
1743 return integer_zero_node;
1745 case UNION_TYPE:
1746 error ("union type value used where scalar is required");
1747 return integer_zero_node;
1749 case ARRAY_TYPE:
1750 error ("array type value used where scalar is required");
1751 return integer_zero_node;
1753 default:
1754 break;
1756 #endif /* 0 */
1758 switch (TREE_CODE (expr))
1760 /* It is simpler and generates better code to have only TRUTH_*_EXPR
1761 or comparison expressions as truth values at this level. */
1762 #if 0
1763 case COMPONENT_REF:
1764 /* A one-bit unsigned bit-field is already acceptable. */
1765 if (1 == TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (expr, 1)))
1766 && TREE_UNSIGNED (TREE_OPERAND (expr, 1)))
1767 return expr;
1768 break;
1769 #endif
1771 case EQ_EXPR:
1772 /* It is simpler and generates better code to have only TRUTH_*_EXPR
1773 or comparison expressions as truth values at this level. */
1774 #if 0
1775 if (integer_zerop (TREE_OPERAND (expr, 1)))
1776 return build_unary_op (TRUTH_NOT_EXPR, TREE_OPERAND (expr, 0), 0);
1777 #endif
1778 case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
1779 case TRUTH_ANDIF_EXPR:
1780 case TRUTH_ORIF_EXPR:
1781 case TRUTH_AND_EXPR:
1782 case TRUTH_OR_EXPR:
1783 case TRUTH_XOR_EXPR:
1784 return convert (integer_type_node, expr);
1786 case ERROR_MARK:
1787 return expr;
1789 case INTEGER_CST:
1790 return integer_zerop (expr) ? integer_zero_node : integer_one_node;
1792 case REAL_CST:
1793 return real_zerop (expr) ? integer_zero_node : integer_one_node;
1795 case ADDR_EXPR:
1796 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0)))
1797 return build (COMPOUND_EXPR, integer_type_node,
1798 TREE_OPERAND (expr, 0), integer_one_node);
1799 else
1800 return integer_one_node;
1802 case COMPLEX_EXPR:
1803 return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
1804 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
1805 truthvalue_conversion (TREE_OPERAND (expr, 0)),
1806 truthvalue_conversion (TREE_OPERAND (expr, 1)),
1809 case NEGATE_EXPR:
1810 case ABS_EXPR:
1811 case FLOAT_EXPR:
1812 case FFS_EXPR:
1813 /* These don't change whether an object is non-zero or zero. */
1814 return truthvalue_conversion (TREE_OPERAND (expr, 0));
1816 case LROTATE_EXPR:
1817 case RROTATE_EXPR:
1818 /* These don't change whether an object is zero or non-zero, but
1819 we can't ignore them if their second arg has side-effects. */
1820 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
1821 return build (COMPOUND_EXPR, integer_type_node, TREE_OPERAND (expr, 1),
1822 truthvalue_conversion (TREE_OPERAND (expr, 0)));
1823 else
1824 return truthvalue_conversion (TREE_OPERAND (expr, 0));
1826 case COND_EXPR:
1827 /* Distribute the conversion into the arms of a COND_EXPR. */
1828 return fold (build (COND_EXPR, integer_type_node, TREE_OPERAND (expr, 0),
1829 truthvalue_conversion (TREE_OPERAND (expr, 1)),
1830 truthvalue_conversion (TREE_OPERAND (expr, 2))));
1832 case CONVERT_EXPR:
1833 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
1834 since that affects how `default_conversion' will behave. */
1835 if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE
1836 || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
1837 break;
1838 /* fall through... */
1839 case NOP_EXPR:
1840 /* If this is widening the argument, we can ignore it. */
1841 if (TYPE_PRECISION (TREE_TYPE (expr))
1842 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
1843 return truthvalue_conversion (TREE_OPERAND (expr, 0));
1844 break;
1846 case MINUS_EXPR:
1847 /* With IEEE arithmetic, x - x may not equal 0, so we can't optimize
1848 this case. */
1849 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
1850 && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE)
1851 break;
1852 /* fall through... */
1853 case BIT_XOR_EXPR:
1854 /* This and MINUS_EXPR can be changed into a comparison of the
1855 two objects. */
1856 if (TREE_TYPE (TREE_OPERAND (expr, 0))
1857 == TREE_TYPE (TREE_OPERAND (expr, 1)))
1858 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
1859 TREE_OPERAND (expr, 1), 1);
1860 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
1861 fold (build1 (NOP_EXPR,
1862 TREE_TYPE (TREE_OPERAND (expr, 0)),
1863 TREE_OPERAND (expr, 1))), 1);
1865 case BIT_AND_EXPR:
1866 if (integer_onep (TREE_OPERAND (expr, 1)))
1867 return expr;
1869 case MODIFY_EXPR:
1870 if (warn_parentheses && C_EXP_ORIGINAL_CODE (expr) == MODIFY_EXPR)
1871 warning ("suggest parentheses around assignment used as truth value");
1872 break;
1875 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
1876 return (build_binary_op
1877 ((TREE_SIDE_EFFECTS (expr)
1878 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
1879 truthvalue_conversion (build_unary_op (REALPART_EXPR, expr, 0)),
1880 truthvalue_conversion (build_unary_op (IMAGPART_EXPR, expr, 0)),
1881 0));
1883 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
1886 /* Read the rest of a #-directive from input stream FINPUT.
1887 In normal use, the directive name and the white space after it
1888 have already been read, so they won't be included in the result.
1889 We allow for the fact that the directive line may contain
1890 a newline embedded within a character or string literal which forms
1891 a part of the directive.
1893 The value is a string in a reusable buffer. It remains valid
1894 only until the next time this function is called. */
1896 char *
1897 get_directive_line (finput)
1898 register FILE *finput;
1900 static char *directive_buffer = NULL;
1901 static unsigned buffer_length = 0;
1902 register char *p;
1903 register char *buffer_limit;
1904 register int looking_for = 0;
1905 register int char_escaped = 0;
1907 if (buffer_length == 0)
1909 directive_buffer = (char *)xmalloc (128);
1910 buffer_length = 128;
1913 buffer_limit = &directive_buffer[buffer_length];
1915 for (p = directive_buffer; ; )
1917 int c;
1919 /* Make buffer bigger if it is full. */
1920 if (p >= buffer_limit)
1922 register unsigned bytes_used = (p - directive_buffer);
1924 buffer_length *= 2;
1925 directive_buffer
1926 = (char *)xrealloc (directive_buffer, buffer_length);
1927 p = &directive_buffer[bytes_used];
1928 buffer_limit = &directive_buffer[buffer_length];
1931 c = getc (finput);
1933 /* Discard initial whitespace. */
1934 if ((c == ' ' || c == '\t') && p == directive_buffer)
1935 continue;
1937 /* Detect the end of the directive. */
1938 if (c == '\n' && looking_for == 0)
1940 ungetc (c, finput);
1941 c = '\0';
1944 *p++ = c;
1946 if (c == 0)
1947 return directive_buffer;
1949 /* Handle string and character constant syntax. */
1950 if (looking_for)
1952 if (looking_for == c && !char_escaped)
1953 looking_for = 0; /* Found terminator... stop looking. */
1955 else
1956 if (c == '\'' || c == '"')
1957 looking_for = c; /* Don't stop buffering until we see another
1958 another one of these (or an EOF). */
1960 /* Handle backslash. */
1961 char_escaped = (c == '\\' && ! char_escaped);
1965 /* Make a variant type in the proper way for C/C++, propagating qualifiers
1966 down to the element type of an array. */
1968 tree
1969 c_build_type_variant (type, constp, volatilep)
1970 tree type;
1971 int constp, volatilep;
1973 if (TREE_CODE (type) == ARRAY_TYPE)
1975 tree real_main_variant = TYPE_MAIN_VARIANT (type);
1977 push_obstacks (TYPE_OBSTACK (type), TYPE_OBSTACK (type));
1978 type = build_array_type (c_build_type_variant (TREE_TYPE (type),
1979 constp, volatilep),
1980 TYPE_DOMAIN (type));
1981 TYPE_MAIN_VARIANT (type) = real_main_variant;
1982 pop_obstacks ();
1984 return build_type_variant (type, constp, volatilep);