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, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
30 extern struct obstack permanent_obstack;
32 enum attrs {A_PACKED, A_NOCOMMON, A_NORETURN, A_CONST, A_T_UNION,
33 A_CONSTRUCTOR, A_DESTRUCTOR, A_MODE, A_SECTION, A_ALIGNED,
34 A_UNUSED, A_FORMAT, A_WEAK, A_ALIAS};
36 static void declare_hidden_char_array PROTO((char *, char *));
37 static void add_attribute PROTO((enum attrs, char *,
39 static void init_attributes PROTO((void));
41 /* Make bindings for __FUNCTION__ and __PRETTY_FUNCTION__. */
44 declare_function_name ()
46 char *name, *printable_name;
48 if (current_function_decl == NULL)
51 printable_name = "top level";
55 char *kind = "function";
56 if (TREE_CODE (TREE_TYPE (current_function_decl)) == METHOD_TYPE)
58 /* Allow functions to be nameless (such as artificial ones). */
59 if (DECL_NAME (current_function_decl))
60 name = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
63 printable_name = (*decl_printable_name) (current_function_decl, &kind);
66 declare_hidden_char_array ("__FUNCTION__", name);
67 declare_hidden_char_array ("__PRETTY_FUNCTION__", printable_name);
71 declare_hidden_char_array (name, value)
74 tree decl, type, init;
77 /* If the default size of char arrays isn't big enough for the name,
78 or if we want to give warnings for large objects, make a bigger one. */
79 vlen = strlen (value) + 1;
80 type = char_array_type_node;
81 if (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TREE_TYPE (type))) < vlen
83 type = build_array_type (char_type_node,
84 build_index_type (build_int_2 (vlen, 0)));
85 push_obstacks_nochange ();
86 decl = build_decl (VAR_DECL, get_identifier (name), type);
87 TREE_STATIC (decl) = 1;
88 TREE_READONLY (decl) = 1;
89 TREE_ASM_WRITTEN (decl) = 1;
90 DECL_SOURCE_LINE (decl) = 0;
91 DECL_ARTIFICIAL (decl) = 1;
92 DECL_IN_SYSTEM_HEADER (decl) = 1;
93 DECL_IGNORED_P (decl) = 1;
94 init = build_string (vlen, value);
95 TREE_TYPE (init) = type;
96 DECL_INITIAL (decl) = init;
97 finish_decl (pushdecl (decl), init, NULL_TREE);
100 /* Given a chain of STRING_CST nodes,
101 concatenate them into one STRING_CST
102 and give it a suitable array-of-chars data type. */
105 combine_strings (strings)
108 register tree value, t;
109 register int length = 1;
112 int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
115 if (TREE_CHAIN (strings))
117 /* More than one in the chain, so concatenate. */
118 register char *p, *q;
120 /* Don't include the \0 at the end of each substring,
121 except for the last one.
122 Count wide strings and ordinary strings separately. */
123 for (t = strings; t; t = TREE_CHAIN (t))
125 if (TREE_TYPE (t) == wchar_array_type_node)
127 wide_length += (TREE_STRING_LENGTH (t) - wchar_bytes);
131 length += (TREE_STRING_LENGTH (t) - 1);
134 /* If anything is wide, the non-wides will be converted,
135 which makes them take more space. */
137 length = length * wchar_bytes + wide_length;
139 p = savealloc (length);
141 /* Copy the individual strings into the new combined string.
142 If the combined string is wide, convert the chars to ints
143 for any individual strings that are not wide. */
146 for (t = strings; t; t = TREE_CHAIN (t))
148 int len = (TREE_STRING_LENGTH (t)
149 - ((TREE_TYPE (t) == wchar_array_type_node)
151 if ((TREE_TYPE (t) == wchar_array_type_node) == wide_flag)
153 bcopy (TREE_STRING_POINTER (t), q, len);
159 for (i = 0; i < len; i++)
160 ((int *) q)[i] = TREE_STRING_POINTER (t)[i];
161 q += len * wchar_bytes;
167 for (i = 0; i < wchar_bytes; i++)
173 value = make_node (STRING_CST);
174 TREE_STRING_POINTER (value) = p;
175 TREE_STRING_LENGTH (value) = length;
176 TREE_CONSTANT (value) = 1;
181 length = TREE_STRING_LENGTH (value);
182 if (TREE_TYPE (value) == wchar_array_type_node)
186 /* Compute the number of elements, for the array type. */
187 nchars = wide_flag ? length / wchar_bytes : length;
189 /* Create the array type for the string constant.
190 -Wwrite-strings says make the string constant an array of const char
191 so that copying it to a non-const pointer will get a warning. */
192 if (warn_write_strings
193 && (! flag_traditional && ! flag_writable_strings))
196 = build_type_variant (wide_flag ? wchar_type_node : char_type_node,
199 = build_array_type (elements,
200 build_index_type (build_int_2 (nchars - 1, 0)));
204 = build_array_type (wide_flag ? wchar_type_node : char_type_node,
205 build_index_type (build_int_2 (nchars - 1, 0)));
206 TREE_CONSTANT (value) = 1;
207 TREE_STATIC (value) = 1;
211 /* To speed up processing of attributes, we maintain an array of
212 IDENTIFIER_NODES and the corresponding attribute types. */
214 /* Array to hold attribute information. */
216 static struct {enum attrs id; tree name; int min, max, decl_req;} attrtab[50];
218 static int attrtab_idx = 0;
220 /* Add an entry to the attribute table above. */
223 add_attribute (id, string, min_len, max_len, decl_req)
226 int min_len, max_len;
231 attrtab[attrtab_idx].id = id;
232 attrtab[attrtab_idx].name = get_identifier (string);
233 attrtab[attrtab_idx].min = min_len;
234 attrtab[attrtab_idx].max = max_len;
235 attrtab[attrtab_idx++].decl_req = decl_req;
237 sprintf (buf, "__%s__", string);
239 attrtab[attrtab_idx].id = id;
240 attrtab[attrtab_idx].name = get_identifier (buf);
241 attrtab[attrtab_idx].min = min_len;
242 attrtab[attrtab_idx].max = max_len;
243 attrtab[attrtab_idx++].decl_req = decl_req;
246 /* Initialize attribute table. */
251 add_attribute (A_PACKED, "packed", 0, 0, 0);
252 add_attribute (A_NOCOMMON, "nocommon", 0, 0, 1);
253 add_attribute (A_NORETURN, "noreturn", 0, 0, 1);
254 add_attribute (A_NORETURN, "volatile", 0, 0, 1);
255 add_attribute (A_UNUSED, "unused", 0, 0, 1);
256 add_attribute (A_CONST, "const", 0, 0, 1);
257 add_attribute (A_T_UNION, "transparent_union", 0, 0, 0);
258 add_attribute (A_CONSTRUCTOR, "constructor", 0, 0, 1);
259 add_attribute (A_DESTRUCTOR, "destructor", 0, 0, 1);
260 add_attribute (A_MODE, "mode", 1, 1, 1);
261 add_attribute (A_SECTION, "section", 1, 1, 1);
262 add_attribute (A_ALIGNED, "aligned", 0, 1, 0);
263 add_attribute (A_FORMAT, "format", 3, 3, 1);
264 add_attribute (A_WEAK, "weak", 0, 0, 1);
265 add_attribute (A_ALIAS, "alias", 1, 1, 1);
268 /* Process the attributes listed in ATTRIBUTES and PREFIX_ATTRIBUTES
269 and install them in NODE, which is either a DECL (including a TYPE_DECL)
270 or a TYPE. PREFIX_ATTRIBUTES can appear after the declaration specifiers
271 and declaration modifiers but before the declaration proper. */
274 decl_attributes (node, attributes, prefix_attributes)
275 tree node, attributes, prefix_attributes;
281 if (attrtab_idx == 0)
284 if (TREE_CODE_CLASS (TREE_CODE (node)) == 'd')
287 type = TREE_TYPE (decl);
288 is_type = TREE_CODE (node) == TYPE_DECL;
290 else if (TREE_CODE_CLASS (TREE_CODE (node)) == 't')
291 type = node, is_type = 1;
293 attributes = chainon (prefix_attributes, attributes);
295 for (a = attributes; a; a = TREE_CHAIN (a))
297 tree name = TREE_PURPOSE (a);
298 tree args = TREE_VALUE (a);
302 for (i = 0; i < attrtab_idx; i++)
303 if (attrtab[i].name == name)
306 if (i == attrtab_idx)
308 if (! valid_machine_attribute (name, args, decl, type))
309 warning ("`%s' attribute directive ignored",
310 IDENTIFIER_POINTER (name));
313 else if (attrtab[i].decl_req && decl == 0)
315 warning ("`%s' attribute does not apply to types",
316 IDENTIFIER_POINTER (name));
319 else if (list_length (args) < attrtab[i].min
320 || list_length (args) > attrtab[i].max)
322 error ("wrong number of arguments specified for `%s' attribute",
323 IDENTIFIER_POINTER (name));
332 TYPE_PACKED (type) = 1;
333 else if (TREE_CODE (decl) == FIELD_DECL)
334 DECL_PACKED (decl) = 1;
335 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
336 used for DECL_REGISTER. It wouldn't mean anything anyway. */
338 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
342 if (TREE_CODE (decl) == VAR_DECL)
343 DECL_COMMON (decl) = 0;
345 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
349 if (TREE_CODE (decl) == FUNCTION_DECL)
350 TREE_THIS_VOLATILE (decl) = 1;
351 else if (TREE_CODE (type) == POINTER_TYPE
352 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
353 TREE_TYPE (decl) = type
355 (build_type_variant (TREE_TYPE (type),
356 TREE_READONLY (TREE_TYPE (type)), 1));
358 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
362 if (TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == VAR_DECL
363 || TREE_CODE (decl) == FUNCTION_DECL)
364 TREE_USED (decl) = 1;
366 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
370 if (TREE_CODE (decl) == FUNCTION_DECL)
371 TREE_READONLY (decl) = 1;
372 else if (TREE_CODE (type) == POINTER_TYPE
373 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
374 TREE_TYPE (decl) = type
376 (build_type_variant (TREE_TYPE (type), 1,
377 TREE_THIS_VOLATILE (TREE_TYPE (type))));
379 warning ( "`%s' attribute ignored", IDENTIFIER_POINTER (name));
384 && TREE_CODE (type) == UNION_TYPE
386 || TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type))))
387 TYPE_TRANSPARENT_UNION (type) = 1;
388 else if (decl != 0 && TREE_CODE (decl) == PARM_DECL
389 && TREE_CODE (type) == UNION_TYPE
390 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))
391 DECL_TRANSPARENT_UNION (decl) = 1;
393 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
397 if (TREE_CODE (decl) == FUNCTION_DECL
398 && TREE_CODE (type) == FUNCTION_TYPE
399 && decl_function_context (decl) == 0)
401 DECL_STATIC_CONSTRUCTOR (decl) = 1;
402 TREE_USED (decl) = 1;
405 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
409 if (TREE_CODE (decl) == FUNCTION_DECL
410 && TREE_CODE (type) == FUNCTION_TYPE
411 && decl_function_context (decl) == 0)
413 DECL_STATIC_DESTRUCTOR (decl) = 1;
414 TREE_USED (decl) = 1;
417 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
421 if (TREE_CODE (TREE_VALUE (args)) != IDENTIFIER_NODE)
422 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
426 char *p = IDENTIFIER_POINTER (TREE_VALUE (args));
427 int len = strlen (p);
428 enum machine_mode mode = VOIDmode;
431 if (len > 4 && p[0] == '_' && p[1] == '_'
432 && p[len - 1] == '_' && p[len - 2] == '_')
434 char *newp = (char *) alloca (len - 1);
436 strcpy (newp, &p[2]);
437 newp[len - 4] = '\0';
441 /* Give this decl a type with the specified mode.
442 First check for the special modes. */
443 if (! strcmp (p, "byte"))
445 else if (!strcmp (p, "word"))
447 else if (! strcmp (p, "pointer"))
450 for (j = 0; j < NUM_MACHINE_MODES; j++)
451 if (!strcmp (p, GET_MODE_NAME (j)))
452 mode = (enum machine_mode) j;
454 if (mode == VOIDmode)
455 error ("unknown machine mode `%s'", p);
456 else if (0 == (typefm = type_for_mode (mode,
457 TREE_UNSIGNED (type))))
458 error ("no data type for mode `%s'", p);
461 TREE_TYPE (decl) = type = typefm;
462 DECL_SIZE (decl) = 0;
463 layout_decl (decl, 0);
469 #ifdef ASM_OUTPUT_SECTION_NAME
470 if ((TREE_CODE (decl) == FUNCTION_DECL
471 || TREE_CODE (decl) == VAR_DECL)
472 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
474 if (TREE_CODE (decl) == VAR_DECL
475 && current_function_decl != NULL_TREE)
476 error_with_decl (decl,
477 "section attribute cannot be specified for local variables");
478 /* The decl may have already been given a section attribute from
479 a previous declaration. Ensure they match. */
480 else if (DECL_SECTION_NAME (decl) != NULL_TREE
481 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
482 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
483 error_with_decl (node,
484 "section of `%s' conflicts with previous declaration");
486 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
489 error_with_decl (node,
490 "section attribute not allowed for `%s'");
492 error_with_decl (node,
493 "section attributes are not supported for this target");
500 = args ? TREE_VALUE (args) : size_int (BIGGEST_ALIGNMENT);
503 /* Strip any NOPs of any kind. */
504 while (TREE_CODE (align_expr) == NOP_EXPR
505 || TREE_CODE (align_expr) == CONVERT_EXPR
506 || TREE_CODE (align_expr) == NON_LVALUE_EXPR)
507 align_expr = TREE_OPERAND (align_expr, 0);
509 if (TREE_CODE (align_expr) != INTEGER_CST)
511 error ("requested alignment is not a constant");
515 align = TREE_INT_CST_LOW (align_expr) * BITS_PER_UNIT;
517 if (exact_log2 (align) == -1)
518 error ("requested alignment is not a power of 2");
520 TYPE_ALIGN (type) = align;
521 else if (TREE_CODE (decl) != VAR_DECL
522 && TREE_CODE (decl) != FIELD_DECL)
523 error_with_decl (decl,
524 "alignment may not be specified for `%s'");
526 DECL_ALIGN (decl) = align;
532 tree format_type = TREE_VALUE (args);
533 tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
534 tree first_arg_num_expr
535 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
542 if (TREE_CODE (decl) != FUNCTION_DECL)
544 error_with_decl (decl,
545 "argument format specified for non-function `%s'");
549 if (TREE_CODE (format_type) == IDENTIFIER_NODE
550 && (!strcmp (IDENTIFIER_POINTER (format_type), "printf")
551 || !strcmp (IDENTIFIER_POINTER (format_type),
554 else if (TREE_CODE (format_type) == IDENTIFIER_NODE
555 && (!strcmp (IDENTIFIER_POINTER (format_type), "scanf")
556 || !strcmp (IDENTIFIER_POINTER (format_type),
561 error ("unrecognized format specifier for `%s'");
565 /* Strip any conversions from the string index and first arg number
566 and verify they are constants. */
567 while (TREE_CODE (format_num_expr) == NOP_EXPR
568 || TREE_CODE (format_num_expr) == CONVERT_EXPR
569 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
570 format_num_expr = TREE_OPERAND (format_num_expr, 0);
572 while (TREE_CODE (first_arg_num_expr) == NOP_EXPR
573 || TREE_CODE (first_arg_num_expr) == CONVERT_EXPR
574 || TREE_CODE (first_arg_num_expr) == NON_LVALUE_EXPR)
575 first_arg_num_expr = TREE_OPERAND (first_arg_num_expr, 0);
577 if (TREE_CODE (format_num_expr) != INTEGER_CST
578 || TREE_CODE (first_arg_num_expr) != INTEGER_CST)
580 error ("format string has non-constant operand number");
584 format_num = TREE_INT_CST_LOW (format_num_expr);
585 first_arg_num = TREE_INT_CST_LOW (first_arg_num_expr);
586 if (first_arg_num != 0 && first_arg_num <= format_num)
588 error ("format string arg follows the args to be formatted");
592 /* If a parameter list is specified, verify that the format_num
593 argument is actually a string, in case the format attribute
595 argument = TYPE_ARG_TYPES (type);
598 for (arg_num = 1; ; ++arg_num)
600 if (argument == 0 || arg_num == format_num)
602 argument = TREE_CHAIN (argument);
605 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
606 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
609 error ("format string arg not a string type");
612 if (first_arg_num != 0)
614 /* Verify that first_arg_num points to the last arg,
617 arg_num++, argument = TREE_CHAIN (argument);
618 if (arg_num != first_arg_num)
620 error ("args to be formatted is not ...");
626 record_function_format (DECL_NAME (decl),
627 DECL_ASSEMBLER_NAME (decl),
628 is_scan, format_num, first_arg_num);
637 if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
638 || TREE_CODE (decl) != FUNCTION_DECL && ! DECL_EXTERNAL (decl))
639 error_with_decl (decl,
640 "`%s' defined both normally and as an alias");
641 else if (decl_function_context (decl) == 0)
643 tree id = get_identifier (TREE_STRING_POINTER
644 (TREE_VALUE (args)));
645 if (TREE_CODE (decl) == FUNCTION_DECL)
646 DECL_INITIAL (decl) = error_mark_node;
648 DECL_EXTERNAL (decl) = 0;
649 assemble_alias (decl, id);
652 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
658 /* Check a printf/fprintf/sprintf/scanf/fscanf/sscanf format against
661 #define T_I &integer_type_node
662 #define T_L &long_integer_type_node
663 #define T_LL &long_long_integer_type_node
664 #define T_S &short_integer_type_node
665 #define T_UI &unsigned_type_node
666 #define T_UL &long_unsigned_type_node
667 #define T_ULL &long_long_unsigned_type_node
668 #define T_US &short_unsigned_type_node
669 #define T_F &float_type_node
670 #define T_D &double_type_node
671 #define T_LD &long_double_type_node
672 #define T_C &char_type_node
673 #define T_V &void_type_node
674 #define T_W &wchar_type_node
675 #define T_ST &sizetype
680 /* Type of argument if no length modifier is used. */
682 /* Type of argument if length modifier for shortening is used.
683 If NULL, then this modifier is not allowed. */
685 /* Type of argument if length modifier `l' is used.
686 If NULL, then this modifier is not allowed. */
688 /* Type of argument if length modifier `q' or `ll' is used.
689 If NULL, then this modifier is not allowed. */
691 /* Type of argument if length modifier `L' is used.
692 If NULL, then this modifier is not allowed. */
694 /* List of other modifier characters allowed with these options. */
698 static format_char_info print_char_table[] = {
699 { "di", 0, T_I, T_I, T_L, T_LL, T_LL, "-wp0 +" },
700 { "oxX", 0, T_UI, T_UI, T_UL, T_ULL, T_ULL, "-wp0#" },
701 { "u", 0, T_UI, T_UI, T_UL, T_ULL, T_ULL, "-wp0" },
702 /* Two GNU extensions. */
703 { "Z", 0, T_ST, NULL, NULL, NULL, NULL, "-wp0" },
704 { "m", 0, T_V, NULL, NULL, NULL, NULL, "-wp" },
705 { "feEgG", 0, T_D, NULL, NULL, NULL, T_LD, "-wp0 +#" },
706 { "c", 0, T_I, NULL, T_W, NULL, NULL, "-w" },
707 { "C", 0, T_W, NULL, NULL, NULL, NULL, "-w" },
708 { "s", 1, T_C, NULL, T_W, NULL, NULL, "-wp" },
709 { "S", 1, T_W, NULL, NULL, NULL, NULL, "-wp" },
710 { "p", 1, T_V, NULL, NULL, NULL, NULL, "-w" },
711 { "n", 1, T_I, T_S, T_L, T_LL, NULL, "" },
715 static format_char_info scan_char_table[] = {
716 { "di", 1, T_I, T_S, T_L, T_LL, T_LL, "*" },
717 { "ouxX", 1, T_UI, T_US, T_UL, T_ULL, T_ULL, "*" },
718 { "efgEG", 1, T_F, NULL, T_D, NULL, T_LD, "*" },
719 { "sc", 1, T_C, NULL, T_W, NULL, NULL, "*a" },
720 { "[", 1, T_C, NULL, NULL, NULL, NULL, "*a" },
721 { "C", 1, T_W, NULL, NULL, NULL, NULL, "*" },
722 { "S", 1, T_W, NULL, NULL, NULL, NULL, "*" },
723 { "p", 2, T_V, NULL, NULL, NULL, NULL, "*" },
724 { "n", 1, T_I, T_S, T_L, T_LL, NULL, "" },
728 typedef struct function_format_info {
729 struct function_format_info *next; /* next structure on the list */
730 tree name; /* identifier such as "printf" */
731 tree assembler_name; /* optional mangled identifier (for C++) */
732 int is_scan; /* TRUE if *scanf */
733 int format_num; /* number of format argument */
734 int first_arg_num; /* number of first arg (zero for varargs) */
735 } function_format_info;
737 static function_format_info *function_format_list = NULL;
739 static void check_format_info PROTO((function_format_info *, tree));
741 /* Initialize the table of functions to perform format checking on.
742 The ANSI functions are always checked (whether <stdio.h> is
743 included or not), since it is common to call printf without
744 including <stdio.h>. There shouldn't be a problem with this,
745 since ANSI reserves these function names whether you include the
746 header file or not. In any case, the checking is harmless. */
749 init_function_format_info ()
751 record_function_format (get_identifier ("printf"), NULL_TREE, 0, 1, 2);
752 record_function_format (get_identifier ("fprintf"), NULL_TREE, 0, 2, 3);
753 record_function_format (get_identifier ("sprintf"), NULL_TREE, 0, 2, 3);
754 record_function_format (get_identifier ("scanf"), NULL_TREE, 1, 1, 2);
755 record_function_format (get_identifier ("fscanf"), NULL_TREE, 1, 2, 3);
756 record_function_format (get_identifier ("sscanf"), NULL_TREE, 1, 2, 3);
757 record_function_format (get_identifier ("vprintf"), NULL_TREE, 0, 1, 0);
758 record_function_format (get_identifier ("vfprintf"), NULL_TREE, 0, 2, 0);
759 record_function_format (get_identifier ("vsprintf"), NULL_TREE, 0, 2, 0);
762 /* Record information for argument format checking. FUNCTION_IDENT is
763 the identifier node for the name of the function to check (its decl
764 need not exist yet). IS_SCAN is true for scanf-type format checking;
765 false indicates printf-style format checking. FORMAT_NUM is the number
766 of the argument which is the format control string (starting from 1).
767 FIRST_ARG_NUM is the number of the first actual argument to check
768 against teh format string, or zero if no checking is not be done
769 (e.g. for varargs such as vfprintf). */
772 record_function_format (name, assembler_name, is_scan,
773 format_num, first_arg_num)
780 function_format_info *info;
782 /* Re-use existing structure if it's there. */
784 for (info = function_format_list; info; info = info->next)
786 if (info->name == name && info->assembler_name == assembler_name)
791 info = (function_format_info *) xmalloc (sizeof (function_format_info));
792 info->next = function_format_list;
793 function_format_list = info;
796 info->assembler_name = assembler_name;
799 info->is_scan = is_scan;
800 info->format_num = format_num;
801 info->first_arg_num = first_arg_num;
804 static char tfaff[] = "too few arguments for format";
806 /* Check the argument list of a call to printf, scanf, etc.
807 NAME is the function identifier.
808 ASSEMBLER_NAME is the function's assembler identifier.
809 (Either NAME or ASSEMBLER_NAME, but not both, may be NULL_TREE.)
810 PARAMS is the list of argument values. */
813 check_function_format (name, assembler_name, params)
818 function_format_info *info;
820 /* See if this function is a format function. */
821 for (info = function_format_list; info; info = info->next)
823 if (info->assembler_name
824 ? (info->assembler_name == assembler_name)
825 : (info->name == name))
828 check_format_info (info, params);
834 /* Check the argument list of a call to printf, scanf, etc.
835 INFO points to the function_format_info structure.
836 PARAMS is the list of argument values. */
839 check_format_info (info, params)
840 function_format_info *info;
845 int suppressed, wide, precise;
853 tree first_fillin_param;
855 format_char_info *fci;
856 static char message[132];
858 int has_operand_number = 0;
860 /* Skip to format argument. If the argument isn't available, there's
861 no work for us to do; prototype checking will catch the problem. */
862 for (arg_num = 1; ; ++arg_num)
866 if (arg_num == info->format_num)
868 params = TREE_CHAIN (params);
870 format_tree = TREE_VALUE (params);
871 params = TREE_CHAIN (params);
872 if (format_tree == 0)
874 /* We can only check the format if it's a string constant. */
875 while (TREE_CODE (format_tree) == NOP_EXPR)
876 format_tree = TREE_OPERAND (format_tree, 0); /* strip coercion */
877 if (format_tree == null_pointer_node)
879 warning ("null format string");
882 if (TREE_CODE (format_tree) != ADDR_EXPR)
884 format_tree = TREE_OPERAND (format_tree, 0);
885 if (TREE_CODE (format_tree) != STRING_CST)
887 format_chars = TREE_STRING_POINTER (format_tree);
888 format_length = TREE_STRING_LENGTH (format_tree);
889 if (format_length <= 1)
890 warning ("zero-length format string");
891 if (format_chars[--format_length] != 0)
893 warning ("unterminated format string");
896 /* Skip to first argument to check. */
897 while (arg_num + 1 < info->first_arg_num)
901 params = TREE_CHAIN (params);
905 first_fillin_param = params;
909 if (*format_chars == 0)
911 if (format_chars - TREE_STRING_POINTER (format_tree) != format_length)
912 warning ("embedded `\\0' in format");
913 if (info->first_arg_num != 0 && params != 0 && ! has_operand_number)
914 warning ("too many arguments for format");
917 if (*format_chars++ != '%')
919 if (*format_chars == 0)
921 warning ("spurious trailing `%%' in format");
924 if (*format_chars == '%')
930 suppressed = wide = precise = FALSE;
933 suppressed = *format_chars == '*';
936 while (isdigit (*format_chars))
941 /* See if we have a number followed by a dollar sign. If we do,
942 it is an operand number, so set PARAMS to that operand. */
943 if (*format_chars >= '0' && *format_chars <= '9')
945 char *p = format_chars;
947 while (*p >= '0' && *p++ <= '9')
952 int opnum = atoi (format_chars);
954 params = first_fillin_param;
955 format_chars = p + 1;
956 has_operand_number = 1;
958 for (i = 1; i < opnum && params != 0; i++)
959 params = TREE_CHAIN (params);
961 if (opnum == 0 || params == 0)
963 warning ("operand number out of range in format");
969 while (*format_chars != 0 && index (" +#0-", *format_chars) != 0)
971 if (index (flag_chars, *format_chars) != 0)
973 sprintf (message, "repeated `%c' flag in format",
977 i = strlen (flag_chars);
978 flag_chars[i++] = *format_chars++;
981 /* "If the space and + flags both appear,
982 the space flag will be ignored." */
983 if (index (flag_chars, ' ') != 0
984 && index (flag_chars, '+') != 0)
985 warning ("use of both ` ' and `+' flags in format");
986 /* "If the 0 and - flags both appear,
987 the 0 flag will be ignored." */
988 if (index (flag_chars, '0') != 0
989 && index (flag_chars, '-') != 0)
990 warning ("use of both `0' and `-' flags in format");
991 if (*format_chars == '*')
994 /* "...a field width...may be indicated by an asterisk.
995 In this case, an int argument supplies the field width..." */
1002 if (info->first_arg_num != 0)
1004 cur_param = TREE_VALUE (params);
1005 params = TREE_CHAIN (params);
1007 /* size_t is generally not valid here.
1008 It will work on most machines, because size_t and int
1009 have the same mode. But might as well warn anyway,
1010 since it will fail on other machines. */
1011 if ((TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1012 != integer_type_node)
1014 (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1015 != unsigned_type_node))
1018 "field width is not type int (arg %d)",
1026 while (isdigit (*format_chars))
1032 if (*format_chars == '.')
1036 if (*format_chars != '*' && !isdigit (*format_chars))
1037 warning ("`.' not followed by `*' or digit in format");
1038 /* "...a...precision...may be indicated by an asterisk.
1039 In this case, an int argument supplies the...precision." */
1040 if (*format_chars == '*')
1042 if (info->first_arg_num != 0)
1050 cur_param = TREE_VALUE (params);
1051 params = TREE_CHAIN (params);
1053 if (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1054 != integer_type_node)
1057 "field width is not type int (arg %d)",
1065 while (isdigit (*format_chars))
1070 if (*format_chars == 'h' || *format_chars == 'l' || *format_chars == 'q' ||
1071 *format_chars == 'L')
1072 length_char = *format_chars++;
1075 if (length_char == 'l' && *format_chars == 'l')
1076 length_char = 'q', format_chars++;
1078 if (*format_chars == 'a')
1083 if (suppressed && length_char != 0)
1086 "use of `*' and `%c' together in format",
1090 format_char = *format_chars;
1091 if (format_char == 0)
1093 warning ("conversion lacks type at end of format");
1097 fci = info->is_scan ? scan_char_table : print_char_table;
1098 while (fci->format_chars != 0
1099 && index (fci->format_chars, format_char) == 0)
1101 if (fci->format_chars == 0)
1103 if (format_char >= 040 && format_char < 0177)
1105 "unknown conversion type character `%c' in format",
1109 "unknown conversion type character 0x%x in format",
1114 if (wide && index (fci->flag_chars, 'w') == 0)
1116 sprintf (message, "width used with `%c' format",
1120 if (precise && index (fci->flag_chars, 'p') == 0)
1122 sprintf (message, "precision used with `%c' format",
1126 if (aflag && index (fci->flag_chars, 'a') == 0)
1128 sprintf (message, "`a' flag used with `%c' format",
1132 if (info->is_scan && format_char == '[')
1134 /* Skip over scan set, in case it happens to have '%' in it. */
1135 if (*format_chars == '^')
1137 /* Find closing bracket; if one is hit immediately, then
1138 it's part of the scan set rather than a terminator. */
1139 if (*format_chars == ']')
1141 while (*format_chars && *format_chars != ']')
1143 if (*format_chars != ']')
1144 /* The end of the format string was reached. */
1145 warning ("no closing `]' for `%%[' format");
1149 if (index (fci->flag_chars, '*') == 0)
1152 "suppression of `%c' conversion in format",
1158 for (i = 0; flag_chars[i] != 0; ++i)
1160 if (index (fci->flag_chars, flag_chars[i]) == 0)
1162 sprintf (message, "flag `%c' used with type `%c'",
1163 flag_chars[i], format_char);
1167 if (precise && index (flag_chars, '0') != 0
1168 && (format_char == 'd' || format_char == 'i'
1169 || format_char == 'o' || format_char == 'u'
1170 || format_char == 'x' || format_char == 'x'))
1173 "precision and `0' flag not both allowed with `%c' format",
1177 switch (length_char)
1179 default: wanted_type = fci->nolen ? *(fci->nolen) : 0; break;
1180 case 'h': wanted_type = fci->hlen ? *(fci->hlen) : 0; break;
1181 case 'l': wanted_type = fci->llen ? *(fci->llen) : 0; break;
1182 case 'q': wanted_type = fci->qlen ? *(fci->qlen) : 0; break;
1183 case 'L': wanted_type = fci->bigllen ? *(fci->bigllen) : 0; break;
1185 if (wanted_type == 0)
1188 "use of `%c' length character with `%c' type character",
1189 length_char, format_char);
1194 ** XXX -- should kvetch about stuff such as
1198 ** scanf ("%d", &i);
1202 /* Finally. . .check type of argument against desired type! */
1203 if (info->first_arg_num == 0)
1205 if (fci->pointer_count == 0 && wanted_type == void_type_node)
1206 /* This specifier takes no argument. */
1213 cur_param = TREE_VALUE (params);
1214 params = TREE_CHAIN (params);
1216 cur_type = TREE_TYPE (cur_param);
1218 /* Check the types of any additional pointer arguments
1219 that precede the "real" argument. */
1220 for (i = 0; i < fci->pointer_count; ++i)
1222 if (TREE_CODE (cur_type) == POINTER_TYPE)
1224 cur_type = TREE_TYPE (cur_type);
1228 "format argument is not a %s (arg %d)",
1229 ((fci->pointer_count == 1) ? "pointer" : "pointer to a pointer"),
1235 /* Check the type of the "real" argument, if there's a type we want. */
1236 if (i == fci->pointer_count && wanted_type != 0
1237 && wanted_type != TYPE_MAIN_VARIANT (cur_type)
1238 /* If we want `void *', allow any pointer type.
1239 (Anything else would already have got a warning.) */
1240 && ! (wanted_type == void_type_node
1241 && fci->pointer_count > 0)
1242 /* Don't warn about differences merely in signedness. */
1243 && !(TREE_CODE (wanted_type) == INTEGER_TYPE
1244 && TREE_CODE (TYPE_MAIN_VARIANT (cur_type)) == INTEGER_TYPE
1245 && (TREE_UNSIGNED (wanted_type)
1246 ? wanted_type == (cur_type = unsigned_type (cur_type))
1247 : wanted_type == (cur_type = signed_type (cur_type))))
1248 /* Likewise, "signed char", "unsigned char" and "char" are
1249 equivalent but the above test won't consider them equivalent. */
1250 && ! (wanted_type == char_type_node
1251 && (TYPE_MAIN_VARIANT (cur_type) == signed_char_type_node
1252 || TYPE_MAIN_VARIANT (cur_type) == unsigned_char_type_node)))
1254 register char *this;
1255 register char *that;
1257 this = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (wanted_type)));
1259 if (TREE_CODE (cur_type) != ERROR_MARK
1260 && TYPE_NAME (cur_type) != 0
1261 && TREE_CODE (cur_type) != INTEGER_TYPE
1262 && !(TREE_CODE (cur_type) == POINTER_TYPE
1263 && TREE_CODE (TREE_TYPE (cur_type)) == INTEGER_TYPE))
1265 if (TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL
1266 && DECL_NAME (TYPE_NAME (cur_type)) != 0)
1267 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
1269 that = IDENTIFIER_POINTER (TYPE_NAME (cur_type));
1272 /* A nameless type can't possibly match what the format wants.
1273 So there will be a warning for it.
1274 Make up a string to describe vaguely what it is. */
1277 if (TREE_CODE (cur_type) == POINTER_TYPE)
1280 that = "different type";
1283 /* Make the warning better in case of mismatch of int vs long. */
1284 if (TREE_CODE (cur_type) == INTEGER_TYPE
1285 && TREE_CODE (wanted_type) == INTEGER_TYPE
1286 && TYPE_PRECISION (cur_type) == TYPE_PRECISION (wanted_type)
1287 && TYPE_NAME (cur_type) != 0
1288 && TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL)
1289 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
1291 if (strcmp (this, that) != 0)
1293 sprintf (message, "%s format, %s arg (arg %d)",
1294 this, that, arg_num);
1301 /* Print a warning if a constant expression had overflow in folding.
1302 Invoke this function on every expression that the language
1303 requires to be a constant expression.
1304 Note the ANSI C standard says it is erroneous for a
1305 constant expression to overflow. */
1308 constant_expression_warning (value)
1311 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1312 || TREE_CODE (value) == COMPLEX_CST)
1313 && TREE_CONSTANT_OVERFLOW (value) && pedantic)
1314 pedwarn ("overflow in constant expression");
1317 /* Print a warning if an expression had overflow in folding.
1318 Invoke this function on every expression that
1319 (1) appears in the source code, and
1320 (2) might be a constant expression that overflowed, and
1321 (3) is not already checked by convert_and_check;
1322 however, do not invoke this function on operands of explicit casts. */
1325 overflow_warning (value)
1328 if ((TREE_CODE (value) == INTEGER_CST
1329 || (TREE_CODE (value) == COMPLEX_CST
1330 && TREE_CODE (TREE_REALPART (value)) == INTEGER_CST))
1331 && TREE_OVERFLOW (value))
1333 TREE_OVERFLOW (value) = 0;
1334 warning ("integer overflow in expression");
1336 else if ((TREE_CODE (value) == REAL_CST
1337 || (TREE_CODE (value) == COMPLEX_CST
1338 && TREE_CODE (TREE_REALPART (value)) == REAL_CST))
1339 && TREE_OVERFLOW (value))
1341 TREE_OVERFLOW (value) = 0;
1342 warning ("floating-pointer overflow in expression");
1346 /* Print a warning if a large constant is truncated to unsigned,
1347 or if -Wconversion is used and a constant < 0 is converted to unsigned.
1348 Invoke this function on every expression that might be implicitly
1349 converted to an unsigned type. */
1352 unsigned_conversion_warning (result, operand)
1353 tree result, operand;
1355 if (TREE_CODE (operand) == INTEGER_CST
1356 && TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
1357 && TREE_UNSIGNED (TREE_TYPE (result))
1358 && !int_fits_type_p (operand, TREE_TYPE (result)))
1360 if (!int_fits_type_p (operand, signed_type (TREE_TYPE (result))))
1361 /* This detects cases like converting -129 or 256 to unsigned char. */
1362 warning ("large integer implicitly truncated to unsigned type");
1363 else if (warn_conversion)
1364 warning ("negative integer implicitly converted to unsigned type");
1368 /* Convert EXPR to TYPE, warning about conversion problems with constants.
1369 Invoke this function on every expression that is converted implicitly,
1370 i.e. because of language rules and not because of an explicit cast. */
1373 convert_and_check (type, expr)
1376 tree t = convert (type, expr);
1377 if (TREE_CODE (t) == INTEGER_CST)
1379 if (TREE_OVERFLOW (t))
1381 TREE_OVERFLOW (t) = 0;
1383 /* Do not diagnose overflow in a constant expression merely
1384 because a conversion overflowed. */
1385 TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (expr);
1387 /* No warning for converting 0x80000000 to int. */
1388 if (!(TREE_UNSIGNED (type) < TREE_UNSIGNED (TREE_TYPE (expr))
1389 && TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
1390 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (expr))))
1391 /* If EXPR fits in the unsigned version of TYPE,
1392 don't warn unless pedantic. */
1394 || TREE_UNSIGNED (type)
1395 || ! int_fits_type_p (expr, unsigned_type (type)))
1396 warning ("overflow in implicit constant conversion");
1399 unsigned_conversion_warning (t, expr);
1405 c_expand_expr_stmt (expr)
1408 /* Do default conversion if safe and possibly important,
1409 in case within ({...}). */
1410 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE && lvalue_p (expr))
1411 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
1412 expr = default_conversion (expr);
1414 if (TREE_TYPE (expr) != error_mark_node
1415 && TYPE_SIZE (TREE_TYPE (expr)) == 0
1416 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
1417 error ("expression statement has incomplete type");
1419 expand_expr_stmt (expr);
1422 /* Validate the expression after `case' and apply default promotions. */
1425 check_case_value (value)
1428 if (value == NULL_TREE)
1431 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1432 STRIP_TYPE_NOPS (value);
1434 if (TREE_CODE (value) != INTEGER_CST
1435 && value != error_mark_node)
1437 error ("case label does not reduce to an integer constant");
1438 value = error_mark_node;
1441 /* Promote char or short to int. */
1442 value = default_conversion (value);
1444 constant_expression_warning (value);
1449 /* Return an integer type with BITS bits of precision,
1450 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
1453 type_for_size (bits, unsignedp)
1457 if (bits == TYPE_PRECISION (integer_type_node))
1458 return unsignedp ? unsigned_type_node : integer_type_node;
1460 if (bits == TYPE_PRECISION (signed_char_type_node))
1461 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1463 if (bits == TYPE_PRECISION (short_integer_type_node))
1464 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1466 if (bits == TYPE_PRECISION (long_integer_type_node))
1467 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1469 if (bits == TYPE_PRECISION (long_long_integer_type_node))
1470 return (unsignedp ? long_long_unsigned_type_node
1471 : long_long_integer_type_node);
1473 if (bits <= TYPE_PRECISION (intQI_type_node))
1474 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
1476 if (bits <= TYPE_PRECISION (intHI_type_node))
1477 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
1479 if (bits <= TYPE_PRECISION (intSI_type_node))
1480 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
1482 if (bits <= TYPE_PRECISION (intDI_type_node))
1483 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
1488 /* Return a data type that has machine mode MODE.
1489 If the mode is an integer,
1490 then UNSIGNEDP selects between signed and unsigned types. */
1493 type_for_mode (mode, unsignedp)
1494 enum machine_mode mode;
1497 if (mode == TYPE_MODE (integer_type_node))
1498 return unsignedp ? unsigned_type_node : integer_type_node;
1500 if (mode == TYPE_MODE (signed_char_type_node))
1501 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1503 if (mode == TYPE_MODE (short_integer_type_node))
1504 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1506 if (mode == TYPE_MODE (long_integer_type_node))
1507 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1509 if (mode == TYPE_MODE (long_long_integer_type_node))
1510 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
1512 if (mode == TYPE_MODE (intQI_type_node))
1513 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
1515 if (mode == TYPE_MODE (intHI_type_node))
1516 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
1518 if (mode == TYPE_MODE (intSI_type_node))
1519 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
1521 if (mode == TYPE_MODE (intDI_type_node))
1522 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
1524 if (mode == TYPE_MODE (float_type_node))
1525 return float_type_node;
1527 if (mode == TYPE_MODE (double_type_node))
1528 return double_type_node;
1530 if (mode == TYPE_MODE (long_double_type_node))
1531 return long_double_type_node;
1533 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
1534 return build_pointer_type (char_type_node);
1536 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
1537 return build_pointer_type (integer_type_node);
1542 /* Return the minimum number of bits needed to represent VALUE in a
1543 signed or unsigned type, UNSIGNEDP says which. */
1546 min_precision (value, unsignedp)
1552 /* If the value is negative, compute its negative minus 1. The latter
1553 adjustment is because the absolute value of the largest negative value
1554 is one larger than the largest positive value. This is equivalent to
1555 a bit-wise negation, so use that operation instead. */
1557 if (tree_int_cst_sgn (value) < 0)
1558 value = fold (build1 (BIT_NOT_EXPR, TREE_TYPE (value), value));
1560 /* Return the number of bits needed, taking into account the fact
1561 that we need one more bit for a signed than unsigned type. */
1563 if (integer_zerop (value))
1565 else if (TREE_INT_CST_HIGH (value) != 0)
1566 log = HOST_BITS_PER_WIDE_INT + floor_log2 (TREE_INT_CST_HIGH (value));
1568 log = floor_log2 (TREE_INT_CST_LOW (value));
1570 return log + 1 + ! unsignedp;
1573 /* Print an error message for invalid operands to arith operation CODE.
1574 NOP_EXPR is used as a special case (see truthvalue_conversion). */
1577 binary_op_error (code)
1578 enum tree_code code;
1580 register char *opname = "unknown";
1585 error ("invalid truth-value expression");
1589 opname = "+"; break;
1591 opname = "-"; break;
1593 opname = "*"; break;
1595 opname = "max"; break;
1597 opname = "min"; break;
1599 opname = "=="; break;
1601 opname = "!="; break;
1603 opname = "<="; break;
1605 opname = ">="; break;
1607 opname = "<"; break;
1609 opname = ">"; break;
1611 opname = "<<"; break;
1613 opname = ">>"; break;
1614 case TRUNC_MOD_EXPR:
1615 case FLOOR_MOD_EXPR:
1616 opname = "%"; break;
1617 case TRUNC_DIV_EXPR:
1618 case FLOOR_DIV_EXPR:
1619 opname = "/"; break;
1621 opname = "&"; break;
1623 opname = "|"; break;
1624 case TRUTH_ANDIF_EXPR:
1625 opname = "&&"; break;
1626 case TRUTH_ORIF_EXPR:
1627 opname = "||"; break;
1629 opname = "^"; break;
1632 opname = "rotate"; break;
1634 error ("invalid operands to binary %s", opname);
1637 /* Subroutine of build_binary_op, used for comparison operations.
1638 See if the operands have both been converted from subword integer types
1639 and, if so, perhaps change them both back to their original type.
1640 This function is also responsible for converting the two operands
1641 to the proper common type for comparison.
1643 The arguments of this function are all pointers to local variables
1644 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
1645 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
1647 If this function returns nonzero, it means that the comparison has
1648 a constant value. What this function returns is an expression for
1652 shorten_compare (op0_ptr, op1_ptr, restype_ptr, rescode_ptr)
1653 tree *op0_ptr, *op1_ptr;
1655 enum tree_code *rescode_ptr;
1658 tree op0 = *op0_ptr;
1659 tree op1 = *op1_ptr;
1660 int unsignedp0, unsignedp1;
1662 tree primop0, primop1;
1663 enum tree_code code = *rescode_ptr;
1665 /* Throw away any conversions to wider types
1666 already present in the operands. */
1668 primop0 = get_narrower (op0, &unsignedp0);
1669 primop1 = get_narrower (op1, &unsignedp1);
1671 /* Handle the case that OP0 does not *contain* a conversion
1672 but it *requires* conversion to FINAL_TYPE. */
1674 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
1675 unsignedp0 = TREE_UNSIGNED (TREE_TYPE (op0));
1676 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
1677 unsignedp1 = TREE_UNSIGNED (TREE_TYPE (op1));
1679 /* If one of the operands must be floated, we cannot optimize. */
1680 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
1681 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
1683 /* If first arg is constant, swap the args (changing operation
1684 so value is preserved), for canonicalization. Don't do this if
1685 the second arg is 0. */
1687 if (TREE_CONSTANT (primop0)
1688 && ! integer_zerop (primop1) && ! real_zerop (primop1))
1690 register tree tem = primop0;
1691 register int temi = unsignedp0;
1699 unsignedp0 = unsignedp1;
1720 *rescode_ptr = code;
1723 /* If comparing an integer against a constant more bits wide,
1724 maybe we can deduce a value of 1 or 0 independent of the data.
1725 Or else truncate the constant now
1726 rather than extend the variable at run time.
1728 This is only interesting if the constant is the wider arg.
1729 Also, it is not safe if the constant is unsigned and the
1730 variable arg is signed, since in this case the variable
1731 would be sign-extended and then regarded as unsigned.
1732 Our technique fails in this case because the lowest/highest
1733 possible unsigned results don't follow naturally from the
1734 lowest/highest possible values of the variable operand.
1735 For just EQ_EXPR and NE_EXPR there is another technique that
1736 could be used: see if the constant can be faithfully represented
1737 in the other operand's type, by truncating it and reextending it
1738 and see if that preserves the constant's value. */
1740 if (!real1 && !real2
1741 && TREE_CODE (primop1) == INTEGER_CST
1742 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
1744 int min_gt, max_gt, min_lt, max_lt;
1745 tree maxval, minval;
1746 /* 1 if comparison is nominally unsigned. */
1747 int unsignedp = TREE_UNSIGNED (*restype_ptr);
1750 type = signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0));
1752 maxval = TYPE_MAX_VALUE (type);
1753 minval = TYPE_MIN_VALUE (type);
1755 if (unsignedp && !unsignedp0)
1756 *restype_ptr = signed_type (*restype_ptr);
1758 if (TREE_TYPE (primop1) != *restype_ptr)
1759 primop1 = convert (*restype_ptr, primop1);
1760 if (type != *restype_ptr)
1762 minval = convert (*restype_ptr, minval);
1763 maxval = convert (*restype_ptr, maxval);
1766 if (unsignedp && unsignedp0)
1768 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
1769 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
1770 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
1771 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
1775 min_gt = INT_CST_LT (primop1, minval);
1776 max_gt = INT_CST_LT (primop1, maxval);
1777 min_lt = INT_CST_LT (minval, primop1);
1778 max_lt = INT_CST_LT (maxval, primop1);
1782 /* This used to be a switch, but Genix compiler can't handle that. */
1783 if (code == NE_EXPR)
1785 if (max_lt || min_gt)
1786 val = boolean_true_node;
1788 else if (code == EQ_EXPR)
1790 if (max_lt || min_gt)
1791 val = boolean_false_node;
1793 else if (code == LT_EXPR)
1796 val = boolean_true_node;
1798 val = boolean_false_node;
1800 else if (code == GT_EXPR)
1803 val = boolean_true_node;
1805 val = boolean_false_node;
1807 else if (code == LE_EXPR)
1810 val = boolean_true_node;
1812 val = boolean_false_node;
1814 else if (code == GE_EXPR)
1817 val = boolean_true_node;
1819 val = boolean_false_node;
1822 /* If primop0 was sign-extended and unsigned comparison specd,
1823 we did a signed comparison above using the signed type bounds.
1824 But the comparison we output must be unsigned.
1826 Also, for inequalities, VAL is no good; but if the signed
1827 comparison had *any* fixed result, it follows that the
1828 unsigned comparison just tests the sign in reverse
1829 (positive values are LE, negative ones GE).
1830 So we can generate an unsigned comparison
1831 against an extreme value of the signed type. */
1833 if (unsignedp && !unsignedp0)
1840 primop1 = TYPE_MIN_VALUE (type);
1846 primop1 = TYPE_MAX_VALUE (type);
1850 type = unsigned_type (type);
1853 if (!max_gt && !unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
1855 /* This is the case of (char)x >?< 0x80, which people used to use
1856 expecting old C compilers to change the 0x80 into -0x80. */
1857 if (val == boolean_false_node)
1858 warning ("comparison is always 0 due to limited range of data type");
1859 if (val == boolean_true_node)
1860 warning ("comparison is always 1 due to limited range of data type");
1863 if (!min_lt && unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
1865 /* This is the case of (unsigned char)x >?< -1 or < 0. */
1866 if (val == boolean_false_node)
1867 warning ("comparison is always 0 due to limited range of data type");
1868 if (val == boolean_true_node)
1869 warning ("comparison is always 1 due to limited range of data type");
1874 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
1875 if (TREE_SIDE_EFFECTS (primop0))
1876 return build (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
1880 /* Value is not predetermined, but do the comparison
1881 in the type of the operand that is not constant.
1882 TYPE is already properly set. */
1884 else if (real1 && real2
1885 && (TYPE_PRECISION (TREE_TYPE (primop0))
1886 == TYPE_PRECISION (TREE_TYPE (primop1))))
1887 type = TREE_TYPE (primop0);
1889 /* If args' natural types are both narrower than nominal type
1890 and both extend in the same manner, compare them
1891 in the type of the wider arg.
1892 Otherwise must actually extend both to the nominal
1893 common type lest different ways of extending
1895 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
1897 else if (unsignedp0 == unsignedp1 && real1 == real2
1898 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
1899 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
1901 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
1902 type = signed_or_unsigned_type (unsignedp0
1903 || TREE_UNSIGNED (*restype_ptr),
1905 /* Make sure shorter operand is extended the right way
1906 to match the longer operand. */
1907 primop0 = convert (signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0)),
1909 primop1 = convert (signed_or_unsigned_type (unsignedp1, TREE_TYPE (primop1)),
1914 /* Here we must do the comparison on the nominal type
1915 using the args exactly as we received them. */
1916 type = *restype_ptr;
1920 if (!real1 && !real2 && integer_zerop (primop1)
1921 && TREE_UNSIGNED (*restype_ptr))
1927 /* All unsigned values are >= 0, so we warn if extra warnings
1928 are requested. However, if OP0 is a constant that is
1929 >= 0, the signedness of the comparison isn't an issue,
1930 so suppress the warning. */
1932 && ! (TREE_CODE (primop0) == INTEGER_CST
1933 && ! TREE_OVERFLOW (convert (signed_type (type),
1935 warning ("unsigned value >= 0 is always 1");
1936 value = boolean_true_node;
1941 && ! (TREE_CODE (primop0) == INTEGER_CST
1942 && ! TREE_OVERFLOW (convert (signed_type (type),
1944 warning ("unsigned value < 0 is always 0");
1945 value = boolean_false_node;
1950 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
1951 if (TREE_SIDE_EFFECTS (primop0))
1952 return build (COMPOUND_EXPR, TREE_TYPE (value),
1959 *op0_ptr = convert (type, primop0);
1960 *op1_ptr = convert (type, primop1);
1962 *restype_ptr = boolean_type_node;
1967 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
1968 or validate its data type for an `if' or `while' statement or ?..: exp.
1970 This preparation consists of taking the ordinary
1971 representation of an expression expr and producing a valid tree
1972 boolean expression describing whether expr is nonzero. We could
1973 simply always do build_binary_op (NE_EXPR, expr, boolean_false_node, 1),
1974 but we optimize comparisons, &&, ||, and !.
1976 The resulting type should always be `boolean_type_node'. */
1979 truthvalue_conversion (expr)
1982 if (TREE_CODE (expr) == ERROR_MARK)
1985 #if 0 /* This appears to be wrong for C++. */
1986 /* These really should return error_mark_node after 2.4 is stable.
1987 But not all callers handle ERROR_MARK properly. */
1988 switch (TREE_CODE (TREE_TYPE (expr)))
1991 error ("struct type value used where scalar is required");
1992 return boolean_false_node;
1995 error ("union type value used where scalar is required");
1996 return boolean_false_node;
1999 error ("array type value used where scalar is required");
2000 return boolean_false_node;
2007 switch (TREE_CODE (expr))
2009 /* It is simpler and generates better code to have only TRUTH_*_EXPR
2010 or comparison expressions as truth values at this level. */
2013 /* A one-bit unsigned bit-field is already acceptable. */
2014 if (1 == TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (expr, 1)))
2015 && TREE_UNSIGNED (TREE_OPERAND (expr, 1)))
2021 /* It is simpler and generates better code to have only TRUTH_*_EXPR
2022 or comparison expressions as truth values at this level. */
2024 if (integer_zerop (TREE_OPERAND (expr, 1)))
2025 return build_unary_op (TRUTH_NOT_EXPR, TREE_OPERAND (expr, 0), 0);
2027 case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
2028 case TRUTH_ANDIF_EXPR:
2029 case TRUTH_ORIF_EXPR:
2030 case TRUTH_AND_EXPR:
2032 case TRUTH_XOR_EXPR:
2033 case TRUTH_NOT_EXPR:
2034 TREE_TYPE (expr) = boolean_type_node;
2041 return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
2044 return real_zerop (expr) ? boolean_false_node : boolean_true_node;
2047 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0)))
2048 return build (COMPOUND_EXPR, boolean_type_node,
2049 TREE_OPERAND (expr, 0), boolean_true_node);
2051 return boolean_true_node;
2054 return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
2055 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
2056 truthvalue_conversion (TREE_OPERAND (expr, 0)),
2057 truthvalue_conversion (TREE_OPERAND (expr, 1)),
2064 /* These don't change whether an object is non-zero or zero. */
2065 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2069 /* These don't change whether an object is zero or non-zero, but
2070 we can't ignore them if their second arg has side-effects. */
2071 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
2072 return build (COMPOUND_EXPR, boolean_type_node, TREE_OPERAND (expr, 1),
2073 truthvalue_conversion (TREE_OPERAND (expr, 0)));
2075 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2078 /* Distribute the conversion into the arms of a COND_EXPR. */
2079 return fold (build (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
2080 truthvalue_conversion (TREE_OPERAND (expr, 1)),
2081 truthvalue_conversion (TREE_OPERAND (expr, 2))));
2084 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
2085 since that affects how `default_conversion' will behave. */
2086 if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE
2087 || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
2089 /* fall through... */
2091 /* If this is widening the argument, we can ignore it. */
2092 if (TYPE_PRECISION (TREE_TYPE (expr))
2093 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
2094 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2098 /* With IEEE arithmetic, x - x may not equal 0, so we can't optimize
2100 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
2101 && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE)
2103 /* fall through... */
2105 /* This and MINUS_EXPR can be changed into a comparison of the
2107 if (TREE_TYPE (TREE_OPERAND (expr, 0))
2108 == TREE_TYPE (TREE_OPERAND (expr, 1)))
2109 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2110 TREE_OPERAND (expr, 1), 1);
2111 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2112 fold (build1 (NOP_EXPR,
2113 TREE_TYPE (TREE_OPERAND (expr, 0)),
2114 TREE_OPERAND (expr, 1))), 1);
2117 if (integer_onep (TREE_OPERAND (expr, 1))
2118 && TREE_TYPE (expr) != boolean_type_node)
2119 /* Using convert here would cause infinite recursion. */
2120 return build1 (NOP_EXPR, boolean_type_node, expr);
2124 if (warn_parentheses && C_EXP_ORIGINAL_CODE (expr) == MODIFY_EXPR)
2125 warning ("suggest parentheses around assignment used as truth value");
2129 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
2130 return (build_binary_op
2131 ((TREE_SIDE_EFFECTS (expr)
2132 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
2133 truthvalue_conversion (build_unary_op (REALPART_EXPR, expr, 0)),
2134 truthvalue_conversion (build_unary_op (IMAGPART_EXPR, expr, 0)),
2137 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
2140 /* Read the rest of a #-directive from input stream FINPUT.
2141 In normal use, the directive name and the white space after it
2142 have already been read, so they won't be included in the result.
2143 We allow for the fact that the directive line may contain
2144 a newline embedded within a character or string literal which forms
2145 a part of the directive.
2147 The value is a string in a reusable buffer. It remains valid
2148 only until the next time this function is called. */
2151 get_directive_line (finput)
2152 register FILE *finput;
2154 static char *directive_buffer = NULL;
2155 static unsigned buffer_length = 0;
2157 register char *buffer_limit;
2158 register int looking_for = 0;
2159 register int char_escaped = 0;
2161 if (buffer_length == 0)
2163 directive_buffer = (char *)xmalloc (128);
2164 buffer_length = 128;
2167 buffer_limit = &directive_buffer[buffer_length];
2169 for (p = directive_buffer; ; )
2173 /* Make buffer bigger if it is full. */
2174 if (p >= buffer_limit)
2176 register unsigned bytes_used = (p - directive_buffer);
2180 = (char *)xrealloc (directive_buffer, buffer_length);
2181 p = &directive_buffer[bytes_used];
2182 buffer_limit = &directive_buffer[buffer_length];
2187 /* Discard initial whitespace. */
2188 if ((c == ' ' || c == '\t') && p == directive_buffer)
2191 /* Detect the end of the directive. */
2192 if (c == '\n' && looking_for == 0)
2201 return directive_buffer;
2203 /* Handle string and character constant syntax. */
2206 if (looking_for == c && !char_escaped)
2207 looking_for = 0; /* Found terminator... stop looking. */
2210 if (c == '\'' || c == '"')
2211 looking_for = c; /* Don't stop buffering until we see another
2212 another one of these (or an EOF). */
2214 /* Handle backslash. */
2215 char_escaped = (c == '\\' && ! char_escaped);
2219 /* Make a variant type in the proper way for C/C++, propagating qualifiers
2220 down to the element type of an array. */
2223 c_build_type_variant (type, constp, volatilep)
2225 int constp, volatilep;
2227 if (TREE_CODE (type) == ARRAY_TYPE)
2229 tree real_main_variant = TYPE_MAIN_VARIANT (type);
2231 push_obstacks (TYPE_OBSTACK (real_main_variant),
2232 TYPE_OBSTACK (real_main_variant));
2233 type = build_array_type (c_build_type_variant (TREE_TYPE (type),
2235 TYPE_DOMAIN (type));
2237 /* TYPE must be on same obstack as REAL_MAIN_VARIANT. If not,
2238 make a copy. (TYPE might have come from the hash table and
2239 REAL_MAIN_VARIANT might be in some function's obstack.) */
2241 if (TYPE_OBSTACK (type) != TYPE_OBSTACK (real_main_variant))
2243 type = copy_node (type);
2244 TYPE_POINTER_TO (type) = TYPE_REFERENCE_TO (type) = 0;
2247 TYPE_MAIN_VARIANT (type) = real_main_variant;
2250 return build_type_variant (type, constp, volatilep);