1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992, 1993, 1994, 1995 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 enum attrs {A_PACKED, A_NOCOMMON, A_NORETURN, A_CONST, A_T_UNION,
32 A_CONSTRUCTOR, A_DESTRUCTOR, A_MODE, A_SECTION, A_ALIGNED,
35 static void declare_hidden_char_array PROTO((char *, char *));
36 static void add_attribute PROTO((enum attrs, char *,
38 static void init_attributes PROTO((void));
40 /* Make bindings for __FUNCTION__ and __PRETTY_FUNCTION__. */
43 declare_function_name ()
45 char *name, *printable_name;
47 if (current_function_decl == NULL)
50 printable_name = "top level";
54 char *kind = "function";
55 if (TREE_CODE (TREE_TYPE (current_function_decl)) == METHOD_TYPE)
57 /* Allow functions to be nameless (such as artificial ones). */
58 if (DECL_NAME (current_function_decl))
59 name = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
62 printable_name = (*decl_printable_name) (current_function_decl, &kind);
65 declare_hidden_char_array ("__FUNCTION__", name);
66 declare_hidden_char_array ("__PRETTY_FUNCTION__", printable_name);
70 declare_hidden_char_array (name, value)
73 tree decl, type, init;
76 /* If the default size of char arrays isn't big enough for the name,
77 or if we want to give warnings for large objects, make a bigger one. */
78 vlen = strlen (value) + 1;
79 type = char_array_type_node;
80 if (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TREE_TYPE (type))) < vlen
82 type = build_array_type (char_type_node,
83 build_index_type (build_int_2 (vlen, 0)));
84 push_obstacks_nochange ();
85 decl = build_decl (VAR_DECL, get_identifier (name), type);
86 TREE_STATIC (decl) = 1;
87 TREE_READONLY (decl) = 1;
88 TREE_ASM_WRITTEN (decl) = 1;
89 DECL_SOURCE_LINE (decl) = 0;
90 DECL_IN_SYSTEM_HEADER (decl) = 1;
91 DECL_IGNORED_P (decl) = 1;
92 init = build_string (vlen, value);
93 TREE_TYPE (init) = type;
94 DECL_INITIAL (decl) = init;
95 finish_decl (pushdecl (decl), init, NULL_TREE);
98 /* Given a chain of STRING_CST nodes,
99 concatenate them into one STRING_CST
100 and give it a suitable array-of-chars data type. */
103 combine_strings (strings)
106 register tree value, t;
107 register int length = 1;
110 int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
113 if (TREE_CHAIN (strings))
115 /* More than one in the chain, so concatenate. */
116 register char *p, *q;
118 /* Don't include the \0 at the end of each substring,
119 except for the last one.
120 Count wide strings and ordinary strings separately. */
121 for (t = strings; t; t = TREE_CHAIN (t))
123 if (TREE_TYPE (t) == wchar_array_type_node)
125 wide_length += (TREE_STRING_LENGTH (t) - wchar_bytes);
129 length += (TREE_STRING_LENGTH (t) - 1);
132 /* If anything is wide, the non-wides will be converted,
133 which makes them take more space. */
135 length = length * wchar_bytes + wide_length;
137 p = savealloc (length);
139 /* Copy the individual strings into the new combined string.
140 If the combined string is wide, convert the chars to ints
141 for any individual strings that are not wide. */
144 for (t = strings; t; t = TREE_CHAIN (t))
146 int len = (TREE_STRING_LENGTH (t)
147 - ((TREE_TYPE (t) == wchar_array_type_node)
149 if ((TREE_TYPE (t) == wchar_array_type_node) == wide_flag)
151 bcopy (TREE_STRING_POINTER (t), q, len);
157 for (i = 0; i < len; i++)
158 ((int *) q)[i] = TREE_STRING_POINTER (t)[i];
159 q += len * wchar_bytes;
165 for (i = 0; i < wchar_bytes; i++)
171 value = make_node (STRING_CST);
172 TREE_STRING_POINTER (value) = p;
173 TREE_STRING_LENGTH (value) = length;
174 TREE_CONSTANT (value) = 1;
179 length = TREE_STRING_LENGTH (value);
180 if (TREE_TYPE (value) == wchar_array_type_node)
184 /* Compute the number of elements, for the array type. */
185 nchars = wide_flag ? length / wchar_bytes : length;
187 /* Create the array type for the string constant.
188 -Wwrite-strings says make the string constant an array of const char
189 so that copying it to a non-const pointer will get a warning. */
190 if (warn_write_strings
191 && (! flag_traditional && ! flag_writable_strings))
194 = build_type_variant (wide_flag ? wchar_type_node : char_type_node,
197 = build_array_type (elements,
198 build_index_type (build_int_2 (nchars - 1, 0)));
202 = build_array_type (wide_flag ? wchar_type_node : char_type_node,
203 build_index_type (build_int_2 (nchars - 1, 0)));
204 TREE_CONSTANT (value) = 1;
205 TREE_STATIC (value) = 1;
209 /* To speed up processing of attributes, we maintain an array of
210 IDENTIFIER_NODES and the corresponding attribute types. */
212 /* Array to hold attribute information. */
214 static struct {enum attrs id; tree name; int min, max, decl_req;} attrtab[50];
216 static int attrtab_idx = 0;
218 /* Add an entry to the attribute table above. */
221 add_attribute (id, string, min_len, max_len, decl_req)
224 int min_len, max_len;
229 attrtab[attrtab_idx].id = id;
230 attrtab[attrtab_idx].name = get_identifier (string);
231 attrtab[attrtab_idx].min = min_len;
232 attrtab[attrtab_idx].max = max_len;
233 attrtab[attrtab_idx++].decl_req = decl_req;
235 sprintf (buf, "__%s__", string);
237 attrtab[attrtab_idx].id = id;
238 attrtab[attrtab_idx].name = get_identifier (buf);
239 attrtab[attrtab_idx].min = min_len;
240 attrtab[attrtab_idx].max = max_len;
241 attrtab[attrtab_idx++].decl_req = decl_req;
244 /* Initialize attribute table. */
249 add_attribute (A_PACKED, "packed", 0, 0, 1);
250 add_attribute (A_NOCOMMON, "nocommon", 0, 0, 1);
251 add_attribute (A_NORETURN, "noreturn", 0, 0, 1);
252 add_attribute (A_NORETURN, "volatile", 0, 0, 1);
253 add_attribute (A_CONST, "const", 0, 0, 1);
254 add_attribute (A_T_UNION, "transparent_union", 0, 0, 0);
255 add_attribute (A_CONSTRUCTOR, "constructor", 0, 0, 1);
256 add_attribute (A_DESTRUCTOR, "destructor", 0, 0, 1);
257 add_attribute (A_MODE, "mode", 1, 1, 1);
258 add_attribute (A_SECTION, "section", 1, 1, 1);
259 add_attribute (A_ALIGNED, "aligned", 0, 1, 0);
260 add_attribute (A_FORMAT, "format", 3, 3, 1);
263 /* Process the attributes listed in ATTRIBUTES and PREFIX_ATTRIBUTES
264 and install them in NODE, which is either a DECL (including a TYPE_DECL)
265 or a TYPE. PREFIX_ATTRIBUTES can appear after the declaration specifiers
266 and declaration modifiers but before the declaration proper. */
269 decl_attributes (node, attributes, prefix_attributes)
270 tree node, attributes, prefix_attributes;
276 if (attrtab_idx == 0)
279 if (TREE_CODE_CLASS (TREE_CODE (node)) == 'd')
282 type = TREE_TYPE (decl);
283 is_type = TREE_CODE (node) == TYPE_DECL;
285 else if (TREE_CODE_CLASS (TREE_CODE (node)) == 't')
286 type = node, is_type = 1;
288 attributes = chainon (prefix_attributes, attributes);
290 for (a = attributes; a; a = TREE_CHAIN (a))
292 tree name = TREE_PURPOSE (a);
293 tree args = TREE_VALUE (a);
297 for (i = 0; i < attrtab_idx; i++)
298 if (attrtab[i].name == name)
302 && ! valid_machine_attribute (name, args, decl, type))
304 warning ("`%s' attribute directive ignored",
305 IDENTIFIER_POINTER (name));
308 else if (attrtab[i].decl_req && decl == 0)
310 warning ("`%s' attribute does not apply to types",
311 IDENTIFIER_POINTER (name));
314 else if (list_length (args) < attrtab[i].min
315 || list_length (args) > attrtab[i].max)
317 error ("wrong number of arguments specified for `%s' attribute",
318 IDENTIFIER_POINTER (name));
326 if (TREE_CODE (decl) == FIELD_DECL)
327 DECL_PACKED (decl) = 1;
328 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
329 used for DECL_REGISTER. It wouldn't mean anything anyway. */
331 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
335 if (TREE_CODE (decl) == VAR_DECL)
336 DECL_COMMON (decl) = 0;
338 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
342 if (TREE_CODE (decl) == FUNCTION_DECL)
343 TREE_THIS_VOLATILE (decl) = 1;
344 else if (TREE_CODE (type) == POINTER_TYPE
345 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
346 TREE_TYPE (decl) = type
348 (build_type_variant (TREE_TYPE (type),
349 TREE_READONLY (TREE_TYPE (type)), 1));
351 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
355 if (TREE_CODE (decl) == FUNCTION_DECL)
356 TREE_READONLY (decl) = 1;
357 else if (TREE_CODE (type) == POINTER_TYPE
358 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
359 TREE_TYPE (decl) = type
361 (build_type_variant (TREE_TYPE (type), 1,
362 TREE_THIS_VOLATILE (TREE_TYPE (type))));
364 warning ( "`%s' attribute ignored", IDENTIFIER_POINTER (name));
368 if (decl != 0 && TREE_CODE (decl) == PARM_DECL
369 && TREE_CODE (type) == UNION_TYPE
370 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))
371 DECL_TRANSPARENT_UNION (decl) = 1;
373 && TREE_CODE (type) == UNION_TYPE
374 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))
375 TYPE_TRANSPARENT_UNION (type) = 1;
377 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
381 if (TREE_CODE (decl) == FUNCTION_DECL
382 && TREE_CODE (type) == FUNCTION_TYPE
383 && decl_function_context (decl) == 0)
384 DECL_STATIC_CONSTRUCTOR (decl) = 1;
386 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
390 if (TREE_CODE (decl) == FUNCTION_DECL
391 && TREE_CODE (type) == FUNCTION_TYPE
392 && decl_function_context (decl) == 0)
393 DECL_STATIC_DESTRUCTOR (decl) = 1;
395 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
399 if (TREE_CODE (TREE_VALUE (args)) != IDENTIFIER_NODE)
400 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
404 char *p = IDENTIFIER_POINTER (TREE_VALUE (args));
405 int len = strlen (p);
406 enum machine_mode mode = VOIDmode;
409 if (len > 4 && p[0] == '_' && p[1] == '_'
410 && p[len - 1] == '_' && p[len - 2] == '_')
412 char *newp = (char *) alloca (len - 2);
414 strcpy (newp, &p[2]);
415 newp[len - 4] = '\0';
419 /* Give this decl a type with the specified mode.
420 First check for the special modes. */
421 if (! strcmp (p, "byte"))
423 else if (!strcmp (p, "word"))
425 else if (! strcmp (p, "pointer"))
428 for (j = 0; j < NUM_MACHINE_MODES; j++)
429 if (!strcmp (p, GET_MODE_NAME (j)))
430 mode = (enum machine_mode) j;
432 if (mode == VOIDmode)
433 error ("unknown machine mode `%s'", p);
434 else if (0 == (typefm = type_for_mode (mode,
435 TREE_UNSIGNED (type))))
436 error ("no data type for mode `%s'", p);
439 TREE_TYPE (decl) = type = typefm;
440 DECL_SIZE (decl) = 0;
441 layout_decl (decl, 0);
447 #ifdef ASM_OUTPUT_SECTION_NAME
448 if ((TREE_CODE (decl) == FUNCTION_DECL
449 || TREE_CODE (decl) == VAR_DECL)
450 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
452 if (TREE_CODE (decl) == VAR_DECL
453 && current_function_decl != NULL_TREE)
454 error_with_decl (decl,
455 "section attribute cannot be specified for local variables");
456 /* The decl may have already been given a section attribute from
457 a previous declaration. Ensure they match. */
458 else if (DECL_SECTION_NAME (decl) != NULL_TREE
459 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
460 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
461 error_with_decl (node,
462 "section of `%s' conflicts with previous declaration");
464 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
467 error_with_decl (node,
468 "section attribute not allowed for `%s'");
470 error_with_decl (node,
471 "section attributes are not supported for this target");
478 = args ? TREE_VALUE (args) : size_int (BIGGEST_ALIGNMENT);
481 /* Strip any NOPs of any kind. */
482 while (TREE_CODE (align_expr) == NOP_EXPR
483 || TREE_CODE (align_expr) == CONVERT_EXPR
484 || TREE_CODE (align_expr) == NON_LVALUE_EXPR)
485 align_expr = TREE_OPERAND (align_expr, 0);
487 if (TREE_CODE (align_expr) != INTEGER_CST)
489 error ("requested alignment is not a constant");
493 align = TREE_INT_CST_LOW (align_expr) * BITS_PER_UNIT;
495 if (exact_log2 (align) == -1)
496 error ("requested alignment is not a power of 2");
498 TYPE_ALIGN (TREE_TYPE (decl)) = align;
499 else if (TREE_CODE (decl) != VAR_DECL
500 && TREE_CODE (decl) != FIELD_DECL)
501 error_with_decl (decl,
502 "alignment may not be specified for `%s'");
504 DECL_ALIGN (decl) = align;
510 tree format_type = TREE_VALUE (args);
511 tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
512 tree first_arg_num_expr
513 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
520 if (TREE_CODE (decl) != FUNCTION_DECL)
522 error_with_decl (decl,
523 "argument format specified for non-function `%s'");
527 if (TREE_CODE (format_type) == IDENTIFIER_NODE
528 && (!strcmp (IDENTIFIER_POINTER (format_type), "printf")
529 || !strcmp (IDENTIFIER_POINTER (format_type),
532 else if (TREE_CODE (format_type) == IDENTIFIER_NODE
533 && (!strcmp (IDENTIFIER_POINTER (format_type), "scanf")
534 || !strcmp (IDENTIFIER_POINTER (format_type),
539 error ("unrecognized format specifier for `%s'");
543 /* Strip any conversions from the string index and first arg number
544 and verify they are constants. */
545 while (TREE_CODE (format_num_expr) == NOP_EXPR
546 || TREE_CODE (format_num_expr) == CONVERT_EXPR
547 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
548 format_num_expr = TREE_OPERAND (format_num_expr, 0);
550 while (TREE_CODE (first_arg_num_expr) == NOP_EXPR
551 || TREE_CODE (first_arg_num_expr) == CONVERT_EXPR
552 || TREE_CODE (first_arg_num_expr) == NON_LVALUE_EXPR)
553 first_arg_num_expr = TREE_OPERAND (first_arg_num_expr, 0);
555 if (TREE_CODE (format_num_expr) != INTEGER_CST
556 || TREE_CODE (first_arg_num_expr) != INTEGER_CST)
558 error ("format string has non-constant operand number");
562 format_num = TREE_INT_CST_LOW (format_num_expr);
563 first_arg_num = TREE_INT_CST_LOW (first_arg_num_expr);
564 if (first_arg_num != 0 && first_arg_num <= format_num)
566 error ("format string arg follows the args to be formatted");
570 /* If a parameter list is specified, verify that the format_num
571 argument is actually a string, in case the format attribute
573 argument = TYPE_ARG_TYPES (type);
576 for (arg_num = 1; ; ++arg_num)
578 if (argument == 0 || arg_num == format_num)
580 argument = TREE_CHAIN (argument);
583 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
584 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
587 error ("format string arg not a string type");
590 if (first_arg_num != 0)
592 /* Verify that first_arg_num points to the last arg,
595 arg_num++, argument = TREE_CHAIN (argument);
596 if (arg_num != first_arg_num)
598 error ("args to be formatted is not ...");
604 record_function_format (DECL_NAME (decl),
605 DECL_ASSEMBLER_NAME (decl),
606 is_scan, format_num, first_arg_num);
613 /* Check a printf/fprintf/sprintf/scanf/fscanf/sscanf format against
616 #define T_I &integer_type_node
617 #define T_L &long_integer_type_node
618 #define T_LL &long_long_integer_type_node
619 #define T_S &short_integer_type_node
620 #define T_UI &unsigned_type_node
621 #define T_UL &long_unsigned_type_node
622 #define T_ULL &long_long_unsigned_type_node
623 #define T_US &short_unsigned_type_node
624 #define T_F &float_type_node
625 #define T_D &double_type_node
626 #define T_LD &long_double_type_node
627 #define T_C &char_type_node
628 #define T_V &void_type_node
629 #define T_W &wchar_type_node
630 #define T_ST &sizetype
635 /* Type of argument if no length modifier is used. */
637 /* Type of argument if length modifier for shortening is used.
638 If NULL, then this modifier is not allowed. */
640 /* Type of argument if length modifier `l' is used.
641 If NULL, then this modifier is not allowed. */
643 /* Type of argument if length modifier `q' or `ll' is used.
644 If NULL, then this modifier is not allowed. */
646 /* Type of argument if length modifier `L' is used.
647 If NULL, then this modifier is not allowed. */
649 /* List of other modifier characters allowed with these options. */
653 static format_char_info print_char_table[] = {
654 { "di", 0, T_I, T_I, T_L, T_LL, T_LL, "-wp0 +" },
655 { "oxX", 0, T_UI, T_UI, T_UL, T_ULL, T_ULL, "-wp0#" },
656 { "u", 0, T_UI, T_UI, T_UL, T_ULL, T_ULL, "-wp0" },
657 /* Two GNU extensions. */
658 { "Z", 0, T_ST, NULL, NULL, NULL, NULL, "-wp0" },
659 { "m", 0, T_UI, T_UI, T_UL, NULL, NULL, "-wp" },
660 { "feEgG", 0, T_D, NULL, NULL, NULL, T_LD, "-wp0 +#" },
661 { "c", 0, T_I, NULL, T_W, NULL, NULL, "-w" },
662 { "C", 0, T_W, NULL, NULL, NULL, NULL, "-w" },
663 { "s", 1, T_C, NULL, T_W, NULL, NULL, "-wp" },
664 { "S", 1, T_W, NULL, NULL, NULL, NULL, "-wp" },
665 { "p", 1, T_V, NULL, NULL, NULL, NULL, "-w" },
666 { "n", 1, T_I, T_S, T_L, T_LL, NULL, "" },
670 static format_char_info scan_char_table[] = {
671 { "di", 1, T_I, T_S, T_L, T_LL, T_LL, "*" },
672 { "ouxX", 1, T_UI, T_US, T_UL, T_ULL, T_ULL, "*" },
673 { "efgEG", 1, T_F, NULL, T_D, NULL, T_LD, "*" },
674 { "sc", 1, T_C, NULL, T_W, NULL, NULL, "*a" },
675 { "[", 1, T_C, NULL, NULL, NULL, NULL, "*a" },
676 { "C", 1, T_W, NULL, NULL, NULL, NULL, "*" },
677 { "S", 1, T_W, NULL, NULL, NULL, NULL, "*" },
678 { "p", 2, T_V, NULL, NULL, NULL, NULL, "*" },
679 { "n", 1, T_I, T_S, T_L, T_LL, NULL, "" },
683 typedef struct function_format_info {
684 struct function_format_info *next; /* next structure on the list */
685 tree name; /* identifier such as "printf" */
686 tree assembler_name; /* optional mangled identifier (for C++) */
687 int is_scan; /* TRUE if *scanf */
688 int format_num; /* number of format argument */
689 int first_arg_num; /* number of first arg (zero for varargs) */
690 } function_format_info;
692 static function_format_info *function_format_list = NULL;
694 static void check_format_info PROTO((function_format_info *, tree));
696 /* Initialize the table of functions to perform format checking on.
697 The ANSI functions are always checked (whether <stdio.h> is
698 included or not), since it is common to call printf without
699 including <stdio.h>. There shouldn't be a problem with this,
700 since ANSI reserves these function names whether you include the
701 header file or not. In any case, the checking is harmless. */
704 init_function_format_info ()
706 record_function_format (get_identifier ("printf"), NULL_TREE, 0, 1, 2);
707 record_function_format (get_identifier ("fprintf"), NULL_TREE, 0, 2, 3);
708 record_function_format (get_identifier ("sprintf"), NULL_TREE, 0, 2, 3);
709 record_function_format (get_identifier ("scanf"), NULL_TREE, 1, 1, 2);
710 record_function_format (get_identifier ("fscanf"), NULL_TREE, 1, 2, 3);
711 record_function_format (get_identifier ("sscanf"), NULL_TREE, 1, 2, 3);
712 record_function_format (get_identifier ("vprintf"), NULL_TREE, 0, 1, 0);
713 record_function_format (get_identifier ("vfprintf"), NULL_TREE, 0, 2, 0);
714 record_function_format (get_identifier ("vsprintf"), NULL_TREE, 0, 2, 0);
717 /* Record information for argument format checking. FUNCTION_IDENT is
718 the identifier node for the name of the function to check (its decl
719 need not exist yet). IS_SCAN is true for scanf-type format checking;
720 false indicates printf-style format checking. FORMAT_NUM is the number
721 of the argument which is the format control string (starting from 1).
722 FIRST_ARG_NUM is the number of the first actual argument to check
723 against teh format string, or zero if no checking is not be done
724 (e.g. for varargs such as vfprintf). */
727 record_function_format (name, assembler_name, is_scan,
728 format_num, first_arg_num)
735 function_format_info *info;
737 /* Re-use existing structure if it's there. */
739 for (info = function_format_list; info; info = info->next)
741 if (info->name == name && info->assembler_name == assembler_name)
746 info = (function_format_info *) xmalloc (sizeof (function_format_info));
747 info->next = function_format_list;
748 function_format_list = info;
751 info->assembler_name = assembler_name;
754 info->is_scan = is_scan;
755 info->format_num = format_num;
756 info->first_arg_num = first_arg_num;
759 static char tfaff[] = "too few arguments for format";
761 /* Check the argument list of a call to printf, scanf, etc.
762 NAME is the function identifier.
763 ASSEMBLER_NAME is the function's assembler identifier.
764 (Either NAME or ASSEMBLER_NAME, but not both, may be NULL_TREE.)
765 PARAMS is the list of argument values. */
768 check_function_format (name, assembler_name, params)
773 function_format_info *info;
775 /* See if this function is a format function. */
776 for (info = function_format_list; info; info = info->next)
778 if (info->assembler_name
779 ? (info->assembler_name == assembler_name)
780 : (info->name == name))
783 check_format_info (info, params);
789 /* Check the argument list of a call to printf, scanf, etc.
790 INFO points to the function_format_info structure.
791 PARAMS is the list of argument values. */
794 check_format_info (info, params)
795 function_format_info *info;
800 int suppressed, wide, precise;
808 tree first_fillin_param;
810 format_char_info *fci;
811 static char message[132];
813 int has_operand_number = 0;
815 /* Skip to format argument. If the argument isn't available, there's
816 no work for us to do; prototype checking will catch the problem. */
817 for (arg_num = 1; ; ++arg_num)
821 if (arg_num == info->format_num)
823 params = TREE_CHAIN (params);
825 format_tree = TREE_VALUE (params);
826 params = TREE_CHAIN (params);
827 if (format_tree == 0)
829 /* We can only check the format if it's a string constant. */
830 while (TREE_CODE (format_tree) == NOP_EXPR)
831 format_tree = TREE_OPERAND (format_tree, 0); /* strip coercion */
832 if (format_tree == null_pointer_node)
834 warning ("null format string");
837 if (TREE_CODE (format_tree) != ADDR_EXPR)
839 format_tree = TREE_OPERAND (format_tree, 0);
840 if (TREE_CODE (format_tree) != STRING_CST)
842 format_chars = TREE_STRING_POINTER (format_tree);
843 format_length = TREE_STRING_LENGTH (format_tree);
844 if (format_length <= 1)
845 warning ("zero-length format string");
846 if (format_chars[--format_length] != 0)
848 warning ("unterminated format string");
851 /* Skip to first argument to check. */
852 while (arg_num + 1 < info->first_arg_num)
856 params = TREE_CHAIN (params);
860 first_fillin_param = params;
864 if (*format_chars == 0)
866 if (format_chars - TREE_STRING_POINTER (format_tree) != format_length)
867 warning ("embedded `\\0' in format");
868 if (info->first_arg_num != 0 && params != 0 && ! has_operand_number)
869 warning ("too many arguments for format");
872 if (*format_chars++ != '%')
874 if (*format_chars == 0)
876 warning ("spurious trailing `%%' in format");
879 if (*format_chars == '%')
885 suppressed = wide = precise = FALSE;
888 suppressed = *format_chars == '*';
891 while (isdigit (*format_chars))
896 /* See if we have a number followed by a dollar sign. If we do,
897 it is an operand number, so set PARAMS to that operand. */
898 if (*format_chars >= '0' && *format_chars <= '9')
900 char *p = format_chars;
902 while (*p >= '0' && *p++ <= '9')
907 int opnum = atoi (format_chars);
909 params = first_fillin_param;
910 format_chars = p + 1;
911 has_operand_number = 1;
913 for (i = 1; i < opnum && params != 0; i++)
914 params = TREE_CHAIN (params);
916 if (opnum == 0 || params == 0)
918 warning ("operand number out of range in format");
924 while (*format_chars != 0 && index (" +#0-", *format_chars) != 0)
926 if (index (flag_chars, *format_chars) != 0)
928 sprintf (message, "repeated `%c' flag in format",
932 i = strlen (flag_chars);
933 flag_chars[i++] = *format_chars++;
936 /* "If the space and + flags both appear,
937 the space flag will be ignored." */
938 if (index (flag_chars, ' ') != 0
939 && index (flag_chars, '+') != 0)
940 warning ("use of both ` ' and `+' flags in format");
941 /* "If the 0 and - flags both appear,
942 the 0 flag will be ignored." */
943 if (index (flag_chars, '0') != 0
944 && index (flag_chars, '-') != 0)
945 warning ("use of both `0' and `-' flags in format");
946 if (*format_chars == '*')
949 /* "...a field width...may be indicated by an asterisk.
950 In this case, an int argument supplies the field width..." */
957 if (info->first_arg_num != 0)
959 cur_param = TREE_VALUE (params);
960 params = TREE_CHAIN (params);
962 /* size_t is generally not valid here.
963 It will work on most machines, because size_t and int
964 have the same mode. But might as well warn anyway,
965 since it will fail on other machines. */
966 if ((TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
967 != integer_type_node)
969 (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
970 != unsigned_type_node))
973 "field width is not type int (arg %d)",
981 while (isdigit (*format_chars))
987 if (*format_chars == '.')
991 if (*format_chars != '*' && !isdigit (*format_chars))
992 warning ("`.' not followed by `*' or digit in format");
993 /* "...a...precision...may be indicated by an asterisk.
994 In this case, an int argument supplies the...precision." */
995 if (*format_chars == '*')
997 if (info->first_arg_num != 0)
1005 cur_param = TREE_VALUE (params);
1006 params = TREE_CHAIN (params);
1008 if (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1009 != integer_type_node)
1012 "field width is not type int (arg %d)",
1020 while (isdigit (*format_chars))
1025 if (*format_chars == 'h' || *format_chars == 'l' || *format_chars == 'q' ||
1026 *format_chars == 'L')
1027 length_char = *format_chars++;
1030 if (length_char == 'l' && *format_chars == 'l')
1031 length_char = 'q', format_chars++;
1033 if (*format_chars == 'a')
1038 if (suppressed && length_char != 0)
1041 "use of `*' and `%c' together in format",
1045 format_char = *format_chars;
1046 if (format_char == 0)
1048 warning ("conversion lacks type at end of format");
1052 fci = info->is_scan ? scan_char_table : print_char_table;
1053 while (fci->format_chars != 0
1054 && index (fci->format_chars, format_char) == 0)
1056 if (fci->format_chars == 0)
1058 if (format_char >= 040 && format_char < 0177)
1060 "unknown conversion type character `%c' in format",
1064 "unknown conversion type character 0x%x in format",
1069 if (wide && index (fci->flag_chars, 'w') == 0)
1071 sprintf (message, "width used with `%c' format",
1075 if (precise && index (fci->flag_chars, 'p') == 0)
1077 sprintf (message, "precision used with `%c' format",
1081 if (aflag && index (fci->flag_chars, 'a') == 0)
1083 sprintf (message, "`a' flag used with `%c' format",
1087 if (info->is_scan && format_char == '[')
1089 /* Skip over scan set, in case it happens to have '%' in it. */
1090 if (*format_chars == '^')
1092 /* Find closing bracket; if one is hit immediately, then
1093 it's part of the scan set rather than a terminator. */
1094 if (*format_chars == ']')
1096 while (*format_chars && *format_chars != ']')
1098 if (*format_chars != ']')
1099 /* The end of the format string was reached. */
1100 warning ("no closing `]' for `%%[' format");
1104 if (index (fci->flag_chars, '*') == 0)
1107 "suppression of `%c' conversion in format",
1113 for (i = 0; flag_chars[i] != 0; ++i)
1115 if (index (fci->flag_chars, flag_chars[i]) == 0)
1117 sprintf (message, "flag `%c' used with type `%c'",
1118 flag_chars[i], format_char);
1122 if (precise && index (flag_chars, '0') != 0
1123 && (format_char == 'd' || format_char == 'i'
1124 || format_char == 'o' || format_char == 'u'
1125 || format_char == 'x' || format_char == 'x'))
1128 "precision and `0' flag not both allowed with `%c' format",
1132 switch (length_char)
1134 default: wanted_type = fci->nolen ? *(fci->nolen) : 0; break;
1135 case 'h': wanted_type = fci->hlen ? *(fci->hlen) : 0; break;
1136 case 'l': wanted_type = fci->llen ? *(fci->llen) : 0; break;
1137 case 'q': wanted_type = fci->qlen ? *(fci->qlen) : 0; break;
1138 case 'L': wanted_type = fci->bigllen ? *(fci->bigllen) : 0; break;
1140 if (wanted_type == 0)
1143 "use of `%c' length character with `%c' type character",
1144 length_char, format_char);
1149 ** XXX -- should kvetch about stuff such as
1153 ** scanf ("%d", &i);
1157 /* Finally. . .check type of argument against desired type! */
1158 if (info->first_arg_num == 0)
1165 cur_param = TREE_VALUE (params);
1166 params = TREE_CHAIN (params);
1168 cur_type = TREE_TYPE (cur_param);
1170 /* Check the types of any additional pointer arguments
1171 that precede the "real" argument. */
1172 for (i = 0; i < fci->pointer_count; ++i)
1174 if (TREE_CODE (cur_type) == POINTER_TYPE)
1176 cur_type = TREE_TYPE (cur_type);
1180 "format argument is not a %s (arg %d)",
1181 ((fci->pointer_count == 1) ? "pointer" : "pointer to a pointer"),
1187 /* Check the type of the "real" argument, if there's a type we want. */
1188 if (i == fci->pointer_count && wanted_type != 0
1189 && wanted_type != TYPE_MAIN_VARIANT (cur_type)
1190 /* If we want `void *', allow any pointer type.
1191 (Anything else would already have got a warning.) */
1192 && ! (wanted_type == void_type_node
1193 && fci->pointer_count > 0)
1194 /* Don't warn about differences merely in signedness. */
1195 && !(TREE_CODE (wanted_type) == INTEGER_TYPE
1196 && TREE_CODE (TYPE_MAIN_VARIANT (cur_type)) == INTEGER_TYPE
1197 && (TREE_UNSIGNED (wanted_type)
1198 ? wanted_type == (cur_type = unsigned_type (cur_type))
1199 : wanted_type == (cur_type = signed_type (cur_type))))
1200 /* Likewise, "signed char", "unsigned char" and "char" are
1201 equivalent but the above test won't consider them equivalent. */
1202 && ! (wanted_type == char_type_node
1203 && (TYPE_MAIN_VARIANT (cur_type) == signed_char_type_node
1204 || TYPE_MAIN_VARIANT (cur_type) == unsigned_char_type_node)))
1206 register char *this;
1207 register char *that;
1209 this = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (wanted_type)));
1211 if (TREE_CODE (cur_type) != ERROR_MARK
1212 && TYPE_NAME (cur_type) != 0
1213 && TREE_CODE (cur_type) != INTEGER_TYPE
1214 && !(TREE_CODE (cur_type) == POINTER_TYPE
1215 && TREE_CODE (TREE_TYPE (cur_type)) == INTEGER_TYPE))
1217 if (TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL
1218 && DECL_NAME (TYPE_NAME (cur_type)) != 0)
1219 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
1221 that = IDENTIFIER_POINTER (TYPE_NAME (cur_type));
1224 /* A nameless type can't possibly match what the format wants.
1225 So there will be a warning for it.
1226 Make up a string to describe vaguely what it is. */
1229 if (TREE_CODE (cur_type) == POINTER_TYPE)
1232 that = "different type";
1235 /* Make the warning better in case of mismatch of int vs long. */
1236 if (TREE_CODE (cur_type) == INTEGER_TYPE
1237 && TREE_CODE (wanted_type) == INTEGER_TYPE
1238 && TYPE_PRECISION (cur_type) == TYPE_PRECISION (wanted_type)
1239 && TYPE_NAME (cur_type) != 0
1240 && TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL)
1241 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
1243 if (strcmp (this, that) != 0)
1245 sprintf (message, "%s format, %s arg (arg %d)",
1246 this, that, arg_num);
1253 /* Print a warning if a constant expression had overflow in folding.
1254 Invoke this function on every expression that the language
1255 requires to be a constant expression.
1256 Note the ANSI C standard says it is erroneous for a
1257 constant expression to overflow. */
1260 constant_expression_warning (value)
1263 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1264 || TREE_CODE (value) == COMPLEX_CST)
1265 && TREE_CONSTANT_OVERFLOW (value) && pedantic)
1266 pedwarn ("overflow in constant expression");
1269 /* Print a warning if an expression had overflow in folding.
1270 Invoke this function on every expression that
1271 (1) appears in the source code, and
1272 (2) might be a constant expression that overflowed, and
1273 (3) is not already checked by convert_and_check;
1274 however, do not invoke this function on operands of explicit casts. */
1277 overflow_warning (value)
1280 if ((TREE_CODE (value) == INTEGER_CST
1281 || (TREE_CODE (value) == COMPLEX_CST
1282 && TREE_CODE (TREE_REALPART (value)) == INTEGER_CST))
1283 && TREE_OVERFLOW (value))
1285 TREE_OVERFLOW (value) = 0;
1286 warning ("integer overflow in expression");
1288 else if ((TREE_CODE (value) == REAL_CST
1289 || (TREE_CODE (value) == COMPLEX_CST
1290 && TREE_CODE (TREE_REALPART (value)) == REAL_CST))
1291 && TREE_OVERFLOW (value))
1293 TREE_OVERFLOW (value) = 0;
1294 warning ("floating-pointer overflow in expression");
1298 /* Print a warning if a large constant is truncated to unsigned,
1299 or if -Wconversion is used and a constant < 0 is converted to unsigned.
1300 Invoke this function on every expression that might be implicitly
1301 converted to an unsigned type. */
1304 unsigned_conversion_warning (result, operand)
1305 tree result, operand;
1307 if (TREE_CODE (operand) == INTEGER_CST
1308 && TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
1309 && TREE_UNSIGNED (TREE_TYPE (result))
1310 && !int_fits_type_p (operand, TREE_TYPE (result)))
1312 if (!int_fits_type_p (operand, signed_type (TREE_TYPE (result))))
1313 /* This detects cases like converting -129 or 256 to unsigned char. */
1314 warning ("large integer implicitly truncated to unsigned type");
1315 else if (warn_conversion)
1316 warning ("negative integer implicitly converted to unsigned type");
1320 /* Convert EXPR to TYPE, warning about conversion problems with constants.
1321 Invoke this function on every expression that is converted implicitly,
1322 i.e. because of language rules and not because of an explicit cast. */
1325 convert_and_check (type, expr)
1328 tree t = convert (type, expr);
1329 if (TREE_CODE (t) == INTEGER_CST)
1331 if (TREE_OVERFLOW (t))
1333 TREE_OVERFLOW (t) = 0;
1335 /* No warning for converting 0x80000000 to int. */
1336 if (!(TREE_UNSIGNED (type) < TREE_UNSIGNED (TREE_TYPE (expr))
1337 && TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
1338 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (expr))))
1339 /* If EXPR fits in the unsigned version of TYPE,
1340 don't warn unless pedantic. */
1342 || TREE_UNSIGNED (type)
1343 || ! int_fits_type_p (expr, unsigned_type (type)))
1344 warning ("overflow in implicit constant conversion");
1347 unsigned_conversion_warning (t, expr);
1353 c_expand_expr_stmt (expr)
1356 /* Do default conversion if safe and possibly important,
1357 in case within ({...}). */
1358 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE && lvalue_p (expr))
1359 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
1360 expr = default_conversion (expr);
1362 if (TREE_TYPE (expr) != error_mark_node
1363 && TYPE_SIZE (TREE_TYPE (expr)) == 0
1364 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
1365 error ("expression statement has incomplete type");
1367 expand_expr_stmt (expr);
1370 /* Validate the expression after `case' and apply default promotions. */
1373 check_case_value (value)
1376 if (value == NULL_TREE)
1379 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1380 STRIP_TYPE_NOPS (value);
1382 if (TREE_CODE (value) != INTEGER_CST
1383 && value != error_mark_node)
1385 error ("case label does not reduce to an integer constant");
1386 value = error_mark_node;
1389 /* Promote char or short to int. */
1390 value = default_conversion (value);
1392 constant_expression_warning (value);
1397 /* Return an integer type with BITS bits of precision,
1398 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
1401 type_for_size (bits, unsignedp)
1405 if (bits == TYPE_PRECISION (integer_type_node))
1406 return unsignedp ? unsigned_type_node : integer_type_node;
1408 if (bits == TYPE_PRECISION (signed_char_type_node))
1409 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1411 if (bits == TYPE_PRECISION (short_integer_type_node))
1412 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1414 if (bits == TYPE_PRECISION (long_integer_type_node))
1415 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1417 if (bits == TYPE_PRECISION (long_long_integer_type_node))
1418 return (unsignedp ? long_long_unsigned_type_node
1419 : long_long_integer_type_node);
1421 if (bits <= TYPE_PRECISION (intQI_type_node))
1422 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
1424 if (bits <= TYPE_PRECISION (intHI_type_node))
1425 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
1427 if (bits <= TYPE_PRECISION (intSI_type_node))
1428 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
1430 if (bits <= TYPE_PRECISION (intDI_type_node))
1431 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
1436 /* Return a data type that has machine mode MODE.
1437 If the mode is an integer,
1438 then UNSIGNEDP selects between signed and unsigned types. */
1441 type_for_mode (mode, unsignedp)
1442 enum machine_mode mode;
1445 if (mode == TYPE_MODE (integer_type_node))
1446 return unsignedp ? unsigned_type_node : integer_type_node;
1448 if (mode == TYPE_MODE (signed_char_type_node))
1449 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1451 if (mode == TYPE_MODE (short_integer_type_node))
1452 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1454 if (mode == TYPE_MODE (long_integer_type_node))
1455 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1457 if (mode == TYPE_MODE (long_long_integer_type_node))
1458 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
1460 if (mode == TYPE_MODE (intQI_type_node))
1461 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
1463 if (mode == TYPE_MODE (intHI_type_node))
1464 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
1466 if (mode == TYPE_MODE (intSI_type_node))
1467 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
1469 if (mode == TYPE_MODE (intDI_type_node))
1470 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
1472 if (mode == TYPE_MODE (float_type_node))
1473 return float_type_node;
1475 if (mode == TYPE_MODE (double_type_node))
1476 return double_type_node;
1478 if (mode == TYPE_MODE (long_double_type_node))
1479 return long_double_type_node;
1481 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
1482 return build_pointer_type (char_type_node);
1484 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
1485 return build_pointer_type (integer_type_node);
1490 /* Return the minimum number of bits needed to represent VALUE in a
1491 signed or unsigned type, UNSIGNEDP says which. */
1494 min_precision (value, unsignedp)
1500 /* If the value is negative, compute its negative minus 1. The latter
1501 adjustment is because the absolute value of the largest negative value
1502 is one larger than the largest positive value. This is equivalent to
1503 a bit-wise negation, so use that operation instead. */
1505 if (tree_int_cst_sgn (value) < 0)
1506 value = fold (build1 (BIT_NOT_EXPR, TREE_TYPE (value), value));
1508 /* Return the number of bits needed, taking into account the fact
1509 that we need one more bit for a signed than unsigned type. */
1511 if (integer_zerop (value))
1513 else if (TREE_INT_CST_HIGH (value) != 0)
1514 log = HOST_BITS_PER_WIDE_INT + floor_log2 (TREE_INT_CST_HIGH (value));
1516 log = floor_log2 (TREE_INT_CST_LOW (value));
1518 return log + 1 + ! unsignedp;
1521 /* Print an error message for invalid operands to arith operation CODE.
1522 NOP_EXPR is used as a special case (see truthvalue_conversion). */
1525 binary_op_error (code)
1526 enum tree_code code;
1528 register char *opname = "unknown";
1533 error ("invalid truth-value expression");
1537 opname = "+"; break;
1539 opname = "-"; break;
1541 opname = "*"; break;
1543 opname = "max"; break;
1545 opname = "min"; break;
1547 opname = "=="; break;
1549 opname = "!="; break;
1551 opname = "<="; break;
1553 opname = ">="; break;
1555 opname = "<"; break;
1557 opname = ">"; break;
1559 opname = "<<"; break;
1561 opname = ">>"; break;
1562 case TRUNC_MOD_EXPR:
1563 case FLOOR_MOD_EXPR:
1564 opname = "%"; break;
1565 case TRUNC_DIV_EXPR:
1566 case FLOOR_DIV_EXPR:
1567 opname = "/"; break;
1569 opname = "&"; break;
1571 opname = "|"; break;
1572 case TRUTH_ANDIF_EXPR:
1573 opname = "&&"; break;
1574 case TRUTH_ORIF_EXPR:
1575 opname = "||"; break;
1577 opname = "^"; break;
1580 opname = "rotate"; break;
1582 error ("invalid operands to binary %s", opname);
1585 /* Subroutine of build_binary_op, used for comparison operations.
1586 See if the operands have both been converted from subword integer types
1587 and, if so, perhaps change them both back to their original type.
1588 This function is also responsible for converting the two operands
1589 to the proper common type for comparison.
1591 The arguments of this function are all pointers to local variables
1592 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
1593 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
1595 If this function returns nonzero, it means that the comparison has
1596 a constant value. What this function returns is an expression for
1600 shorten_compare (op0_ptr, op1_ptr, restype_ptr, rescode_ptr)
1601 tree *op0_ptr, *op1_ptr;
1603 enum tree_code *rescode_ptr;
1606 tree op0 = *op0_ptr;
1607 tree op1 = *op1_ptr;
1608 int unsignedp0, unsignedp1;
1610 tree primop0, primop1;
1611 enum tree_code code = *rescode_ptr;
1613 /* Throw away any conversions to wider types
1614 already present in the operands. */
1616 primop0 = get_narrower (op0, &unsignedp0);
1617 primop1 = get_narrower (op1, &unsignedp1);
1619 /* Handle the case that OP0 does not *contain* a conversion
1620 but it *requires* conversion to FINAL_TYPE. */
1622 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
1623 unsignedp0 = TREE_UNSIGNED (TREE_TYPE (op0));
1624 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
1625 unsignedp1 = TREE_UNSIGNED (TREE_TYPE (op1));
1627 /* If one of the operands must be floated, we cannot optimize. */
1628 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
1629 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
1631 /* If first arg is constant, swap the args (changing operation
1632 so value is preserved), for canonicalization. Don't do this if
1633 the second arg is 0. */
1635 if (TREE_CONSTANT (primop0)
1636 && ! integer_zerop (primop1) && ! real_zerop (primop1))
1638 register tree tem = primop0;
1639 register int temi = unsignedp0;
1647 unsignedp0 = unsignedp1;
1668 *rescode_ptr = code;
1671 /* If comparing an integer against a constant more bits wide,
1672 maybe we can deduce a value of 1 or 0 independent of the data.
1673 Or else truncate the constant now
1674 rather than extend the variable at run time.
1676 This is only interesting if the constant is the wider arg.
1677 Also, it is not safe if the constant is unsigned and the
1678 variable arg is signed, since in this case the variable
1679 would be sign-extended and then regarded as unsigned.
1680 Our technique fails in this case because the lowest/highest
1681 possible unsigned results don't follow naturally from the
1682 lowest/highest possible values of the variable operand.
1683 For just EQ_EXPR and NE_EXPR there is another technique that
1684 could be used: see if the constant can be faithfully represented
1685 in the other operand's type, by truncating it and reextending it
1686 and see if that preserves the constant's value. */
1688 if (!real1 && !real2
1689 && TREE_CODE (primop1) == INTEGER_CST
1690 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
1692 int min_gt, max_gt, min_lt, max_lt;
1693 tree maxval, minval;
1694 /* 1 if comparison is nominally unsigned. */
1695 int unsignedp = TREE_UNSIGNED (*restype_ptr);
1698 type = signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0));
1700 maxval = TYPE_MAX_VALUE (type);
1701 minval = TYPE_MIN_VALUE (type);
1703 if (unsignedp && !unsignedp0)
1704 *restype_ptr = signed_type (*restype_ptr);
1706 if (TREE_TYPE (primop1) != *restype_ptr)
1707 primop1 = convert (*restype_ptr, primop1);
1708 if (type != *restype_ptr)
1710 minval = convert (*restype_ptr, minval);
1711 maxval = convert (*restype_ptr, maxval);
1714 if (unsignedp && unsignedp0)
1716 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
1717 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
1718 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
1719 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
1723 min_gt = INT_CST_LT (primop1, minval);
1724 max_gt = INT_CST_LT (primop1, maxval);
1725 min_lt = INT_CST_LT (minval, primop1);
1726 max_lt = INT_CST_LT (maxval, primop1);
1730 /* This used to be a switch, but Genix compiler can't handle that. */
1731 if (code == NE_EXPR)
1733 if (max_lt || min_gt)
1734 val = boolean_true_node;
1736 else if (code == EQ_EXPR)
1738 if (max_lt || min_gt)
1739 val = boolean_false_node;
1741 else if (code == LT_EXPR)
1744 val = boolean_true_node;
1746 val = boolean_false_node;
1748 else if (code == GT_EXPR)
1751 val = boolean_true_node;
1753 val = boolean_false_node;
1755 else if (code == LE_EXPR)
1758 val = boolean_true_node;
1760 val = boolean_false_node;
1762 else if (code == GE_EXPR)
1765 val = boolean_true_node;
1767 val = boolean_false_node;
1770 /* If primop0 was sign-extended and unsigned comparison specd,
1771 we did a signed comparison above using the signed type bounds.
1772 But the comparison we output must be unsigned.
1774 Also, for inequalities, VAL is no good; but if the signed
1775 comparison had *any* fixed result, it follows that the
1776 unsigned comparison just tests the sign in reverse
1777 (positive values are LE, negative ones GE).
1778 So we can generate an unsigned comparison
1779 against an extreme value of the signed type. */
1781 if (unsignedp && !unsignedp0)
1788 primop1 = TYPE_MIN_VALUE (type);
1794 primop1 = TYPE_MAX_VALUE (type);
1798 type = unsigned_type (type);
1801 if (!max_gt && !unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
1803 /* This is the case of (char)x >?< 0x80, which people used to use
1804 expecting old C compilers to change the 0x80 into -0x80. */
1805 if (val == boolean_false_node)
1806 warning ("comparison is always 0 due to limited range of data type");
1807 if (val == boolean_true_node)
1808 warning ("comparison is always 1 due to limited range of data type");
1811 if (!min_lt && unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
1813 /* This is the case of (unsigned char)x >?< -1 or < 0. */
1814 if (val == boolean_false_node)
1815 warning ("comparison is always 0 due to limited range of data type");
1816 if (val == boolean_true_node)
1817 warning ("comparison is always 1 due to limited range of data type");
1822 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
1823 if (TREE_SIDE_EFFECTS (primop0))
1824 return build (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
1828 /* Value is not predetermined, but do the comparison
1829 in the type of the operand that is not constant.
1830 TYPE is already properly set. */
1832 else if (real1 && real2
1833 && (TYPE_PRECISION (TREE_TYPE (primop0))
1834 == TYPE_PRECISION (TREE_TYPE (primop1))))
1835 type = TREE_TYPE (primop0);
1837 /* If args' natural types are both narrower than nominal type
1838 and both extend in the same manner, compare them
1839 in the type of the wider arg.
1840 Otherwise must actually extend both to the nominal
1841 common type lest different ways of extending
1843 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
1845 else if (unsignedp0 == unsignedp1 && real1 == real2
1846 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
1847 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
1849 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
1850 type = signed_or_unsigned_type (unsignedp0
1851 || TREE_UNSIGNED (*restype_ptr),
1853 /* Make sure shorter operand is extended the right way
1854 to match the longer operand. */
1855 primop0 = convert (signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0)),
1857 primop1 = convert (signed_or_unsigned_type (unsignedp1, TREE_TYPE (primop1)),
1862 /* Here we must do the comparison on the nominal type
1863 using the args exactly as we received them. */
1864 type = *restype_ptr;
1868 if (!real1 && !real2 && integer_zerop (primop1)
1869 && TREE_UNSIGNED (*restype_ptr))
1875 /* All unsigned values are >= 0, so we warn if extra warnings
1876 are requested. However, if OP0 is a constant that is
1877 >= 0, the signedness of the comparison isn't an issue,
1878 so suppress the warning. */
1880 && ! (TREE_CODE (primop0) == INTEGER_CST
1881 && ! TREE_OVERFLOW (convert (signed_type (type),
1883 warning ("unsigned value >= 0 is always 1");
1884 value = boolean_true_node;
1889 && ! (TREE_CODE (primop0) == INTEGER_CST
1890 && ! TREE_OVERFLOW (convert (signed_type (type),
1892 warning ("unsigned value < 0 is always 0");
1893 value = boolean_false_node;
1898 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
1899 if (TREE_SIDE_EFFECTS (primop0))
1900 return build (COMPOUND_EXPR, TREE_TYPE (value),
1907 *op0_ptr = convert (type, primop0);
1908 *op1_ptr = convert (type, primop1);
1910 *restype_ptr = boolean_type_node;
1915 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
1916 or validate its data type for an `if' or `while' statement or ?..: exp.
1918 This preparation consists of taking the ordinary
1919 representation of an expression expr and producing a valid tree
1920 boolean expression describing whether expr is nonzero. We could
1921 simply always do build_binary_op (NE_EXPR, expr, boolean_false_node, 1),
1922 but we optimize comparisons, &&, ||, and !.
1924 The resulting type should always be `boolean_type_node'. */
1927 truthvalue_conversion (expr)
1930 if (TREE_CODE (expr) == ERROR_MARK)
1933 #if 0 /* This appears to be wrong for C++. */
1934 /* These really should return error_mark_node after 2.4 is stable.
1935 But not all callers handle ERROR_MARK properly. */
1936 switch (TREE_CODE (TREE_TYPE (expr)))
1939 error ("struct type value used where scalar is required");
1940 return boolean_false_node;
1943 error ("union type value used where scalar is required");
1944 return boolean_false_node;
1947 error ("array type value used where scalar is required");
1948 return boolean_false_node;
1955 switch (TREE_CODE (expr))
1957 /* It is simpler and generates better code to have only TRUTH_*_EXPR
1958 or comparison expressions as truth values at this level. */
1961 /* A one-bit unsigned bit-field is already acceptable. */
1962 if (1 == TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (expr, 1)))
1963 && TREE_UNSIGNED (TREE_OPERAND (expr, 1)))
1969 /* It is simpler and generates better code to have only TRUTH_*_EXPR
1970 or comparison expressions as truth values at this level. */
1972 if (integer_zerop (TREE_OPERAND (expr, 1)))
1973 return build_unary_op (TRUTH_NOT_EXPR, TREE_OPERAND (expr, 0), 0);
1975 case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
1976 case TRUTH_ANDIF_EXPR:
1977 case TRUTH_ORIF_EXPR:
1978 case TRUTH_AND_EXPR:
1980 case TRUTH_XOR_EXPR:
1981 TREE_TYPE (expr) = boolean_type_node;
1988 return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
1991 return real_zerop (expr) ? boolean_false_node : boolean_true_node;
1994 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0)))
1995 return build (COMPOUND_EXPR, boolean_type_node,
1996 TREE_OPERAND (expr, 0), boolean_true_node);
1998 return boolean_true_node;
2001 return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
2002 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
2003 truthvalue_conversion (TREE_OPERAND (expr, 0)),
2004 truthvalue_conversion (TREE_OPERAND (expr, 1)),
2011 /* These don't change whether an object is non-zero or zero. */
2012 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2016 /* These don't change whether an object is zero or non-zero, but
2017 we can't ignore them if their second arg has side-effects. */
2018 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
2019 return build (COMPOUND_EXPR, boolean_type_node, TREE_OPERAND (expr, 1),
2020 truthvalue_conversion (TREE_OPERAND (expr, 0)));
2022 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2025 /* Distribute the conversion into the arms of a COND_EXPR. */
2026 return fold (build (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
2027 truthvalue_conversion (TREE_OPERAND (expr, 1)),
2028 truthvalue_conversion (TREE_OPERAND (expr, 2))));
2031 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
2032 since that affects how `default_conversion' will behave. */
2033 if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE
2034 || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
2036 /* fall through... */
2038 /* If this is widening the argument, we can ignore it. */
2039 if (TYPE_PRECISION (TREE_TYPE (expr))
2040 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
2041 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2045 /* With IEEE arithmetic, x - x may not equal 0, so we can't optimize
2047 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
2048 && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE)
2050 /* fall through... */
2052 /* This and MINUS_EXPR can be changed into a comparison of the
2054 if (TREE_TYPE (TREE_OPERAND (expr, 0))
2055 == TREE_TYPE (TREE_OPERAND (expr, 1)))
2056 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2057 TREE_OPERAND (expr, 1), 1);
2058 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2059 fold (build1 (NOP_EXPR,
2060 TREE_TYPE (TREE_OPERAND (expr, 0)),
2061 TREE_OPERAND (expr, 1))), 1);
2064 if (integer_onep (TREE_OPERAND (expr, 1)))
2068 if (warn_parentheses && C_EXP_ORIGINAL_CODE (expr) == MODIFY_EXPR)
2069 warning ("suggest parentheses around assignment used as truth value");
2073 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
2074 return (build_binary_op
2075 ((TREE_SIDE_EFFECTS (expr)
2076 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
2077 truthvalue_conversion (build_unary_op (REALPART_EXPR, expr, 0)),
2078 truthvalue_conversion (build_unary_op (IMAGPART_EXPR, expr, 0)),
2081 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
2084 /* Read the rest of a #-directive from input stream FINPUT.
2085 In normal use, the directive name and the white space after it
2086 have already been read, so they won't be included in the result.
2087 We allow for the fact that the directive line may contain
2088 a newline embedded within a character or string literal which forms
2089 a part of the directive.
2091 The value is a string in a reusable buffer. It remains valid
2092 only until the next time this function is called. */
2095 get_directive_line (finput)
2096 register FILE *finput;
2098 static char *directive_buffer = NULL;
2099 static unsigned buffer_length = 0;
2101 register char *buffer_limit;
2102 register int looking_for = 0;
2103 register int char_escaped = 0;
2105 if (buffer_length == 0)
2107 directive_buffer = (char *)xmalloc (128);
2108 buffer_length = 128;
2111 buffer_limit = &directive_buffer[buffer_length];
2113 for (p = directive_buffer; ; )
2117 /* Make buffer bigger if it is full. */
2118 if (p >= buffer_limit)
2120 register unsigned bytes_used = (p - directive_buffer);
2124 = (char *)xrealloc (directive_buffer, buffer_length);
2125 p = &directive_buffer[bytes_used];
2126 buffer_limit = &directive_buffer[buffer_length];
2131 /* Discard initial whitespace. */
2132 if ((c == ' ' || c == '\t') && p == directive_buffer)
2135 /* Detect the end of the directive. */
2136 if (c == '\n' && looking_for == 0)
2145 return directive_buffer;
2147 /* Handle string and character constant syntax. */
2150 if (looking_for == c && !char_escaped)
2151 looking_for = 0; /* Found terminator... stop looking. */
2154 if (c == '\'' || c == '"')
2155 looking_for = c; /* Don't stop buffering until we see another
2156 another one of these (or an EOF). */
2158 /* Handle backslash. */
2159 char_escaped = (c == '\\' && ! char_escaped);
2163 /* Make a variant type in the proper way for C/C++, propagating qualifiers
2164 down to the element type of an array. */
2167 c_build_type_variant (type, constp, volatilep)
2169 int constp, volatilep;
2171 if (TREE_CODE (type) == ARRAY_TYPE)
2173 tree real_main_variant = TYPE_MAIN_VARIANT (type);
2175 push_obstacks (TYPE_OBSTACK (real_main_variant),
2176 TYPE_OBSTACK (real_main_variant));
2177 type = build_array_type (c_build_type_variant (TREE_TYPE (type),
2179 TYPE_DOMAIN (type));
2181 /* TYPE must be on same obstack as REAL_MAIN_VARIANT. If not,
2182 make a copy. (TYPE might have come from the hash table and
2183 REAL_MAIN_VARIANT might be in some function's obstack.) */
2185 if (TYPE_OBSTACK (type) != TYPE_OBSTACK (real_main_variant))
2187 type = copy_node (type);
2188 TYPE_POINTER_TO (type) = TYPE_REFERENCE_TO (type) = 0;
2191 TYPE_MAIN_VARIANT (type) = real_main_variant;
2194 return build_type_variant (type, constp, volatilep);