1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992, 93-98, 1999 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. */
21 /* $FreeBSD: src/contrib/gcc/c-common.c,v 1.8.2.5 2002/06/20 23:12:24 obrien Exp $ */
22 /* $DragonFly: src/contrib/gcc/Attic/c-common.c,v 1.2 2003/06/17 04:23:59 dillon Exp $ */
39 cpp_options parse_options;
40 static enum cpp_token cpp_token;
43 #ifndef WCHAR_TYPE_SIZE
45 #define WCHAR_TYPE_SIZE INT_TYPE_SIZE
47 #define WCHAR_TYPE_SIZE BITS_PER_WORD
51 extern struct obstack permanent_obstack;
53 /* Nonzero means the expression being parsed will never be evaluated.
54 This is a count, since unevaluated expressions can nest. */
57 enum attrs {A_PACKED, A_NOCOMMON, A_COMMON, A_NORETURN, A_CONST, A_T_UNION,
58 A_NO_CHECK_MEMORY_USAGE, A_NO_INSTRUMENT_FUNCTION,
59 A_CONSTRUCTOR, A_DESTRUCTOR, A_MODE, A_SECTION, A_ALIGNED,
60 A_UNUSED, A_FORMAT, A_FORMAT_ARG, A_WEAK, A_ALIAS};
62 enum format_type { printf_format_type, scanf_format_type,
63 strftime_format_type };
65 static void declare_hidden_char_array PROTO((const char *, const char *));
66 static void add_attribute PROTO((enum attrs, const char *,
68 static void init_attributes PROTO((void));
69 static void record_function_format PROTO((tree, tree, enum format_type,
71 static void record_international_format PROTO((tree, tree, int));
72 static tree c_find_base_decl PROTO((tree));
73 static int default_valid_lang_attribute PROTO ((tree, tree, tree, tree));
75 /* Keep a stack of if statements. We record the number of compound
76 statements seen up to the if keyword, as well as the line number
77 and file of the if. If a potentially ambiguous else is seen, that
78 fact is recorded; the warning is issued when we can be sure that
79 the enclosing if statement does not have an else branch. */
87 static void tfaff PROTO((void));
89 static if_elt *if_stack;
91 /* Amount of space in the if statement stack. */
92 static int if_stack_space = 0;
95 static int if_stack_pointer = 0;
97 /* Generate RTL for the start of an if-then, and record the start of it
98 for ambiguous else detection. */
101 c_expand_start_cond (cond, exitflag, compstmt_count)
106 /* Make sure there is enough space on the stack. */
107 if (if_stack_space == 0)
110 if_stack = (if_elt *)xmalloc (10 * sizeof (if_elt));
112 else if (if_stack_space == if_stack_pointer)
114 if_stack_space += 10;
115 if_stack = (if_elt *)xrealloc (if_stack, if_stack_space * sizeof (if_elt));
118 /* Record this if statement. */
119 if_stack[if_stack_pointer].compstmt_count = compstmt_count;
120 if_stack[if_stack_pointer].file = input_filename;
121 if_stack[if_stack_pointer].line = lineno;
122 if_stack[if_stack_pointer].needs_warning = 0;
125 expand_start_cond (cond, exitflag);
128 /* Generate RTL for the end of an if-then. Optionally warn if a nested
129 if statement had an ambiguous else clause. */
135 if (if_stack[if_stack_pointer].needs_warning)
136 warning_with_file_and_line (if_stack[if_stack_pointer].file,
137 if_stack[if_stack_pointer].line,
138 "suggest explicit braces to avoid ambiguous `else'");
142 /* Generate RTL between the then-clause and the else-clause
143 of an if-then-else. */
146 c_expand_start_else ()
148 /* An ambiguous else warning must be generated for the enclosing if
149 statement, unless we see an else branch for that one, too. */
151 && if_stack_pointer > 1
152 && (if_stack[if_stack_pointer - 1].compstmt_count
153 == if_stack[if_stack_pointer - 2].compstmt_count))
154 if_stack[if_stack_pointer - 2].needs_warning = 1;
156 /* Even if a nested if statement had an else branch, it can't be
157 ambiguous if this one also has an else. So don't warn in that
158 case. Also don't warn for any if statements nested in this else. */
159 if_stack[if_stack_pointer - 1].needs_warning = 0;
160 if_stack[if_stack_pointer - 1].compstmt_count--;
162 expand_start_else ();
165 /* Make bindings for __FUNCTION__, __PRETTY_FUNCTION__, and __func__. */
168 declare_function_name ()
170 const char *name, *printable_name;
172 if (current_function_decl == NULL)
175 printable_name = "top level";
179 /* Allow functions to be nameless (such as artificial ones). */
180 if (DECL_NAME (current_function_decl))
181 name = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
184 printable_name = (*decl_printable_name) (current_function_decl, 2);
187 declare_hidden_char_array ("__FUNCTION__", name);
188 declare_hidden_char_array ("__PRETTY_FUNCTION__", printable_name);
189 /* The ISO C people "of course" couldn't use __FUNCTION__ in the
190 ISO C 9x standard; instead a new variable is invented. */
191 declare_hidden_char_array ("__func__", name);
195 declare_hidden_char_array (name, value)
196 const char *name, *value;
198 tree decl, type, init;
201 /* If the default size of char arrays isn't big enough for the name,
202 or if we want to give warnings for large objects, make a bigger one. */
203 vlen = strlen (value) + 1;
204 type = char_array_type_node;
205 if (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < vlen
207 type = build_array_type (char_type_node,
208 build_index_type (build_int_2 (vlen, 0)));
209 push_obstacks_nochange ();
210 decl = build_decl (VAR_DECL, get_identifier (name), type);
211 TREE_STATIC (decl) = 1;
212 TREE_READONLY (decl) = 1;
213 TREE_ASM_WRITTEN (decl) = 1;
214 DECL_SOURCE_LINE (decl) = 0;
215 DECL_ARTIFICIAL (decl) = 1;
216 DECL_IN_SYSTEM_HEADER (decl) = 1;
217 DECL_IGNORED_P (decl) = 1;
218 init = build_string (vlen, value);
219 TREE_TYPE (init) = type;
220 DECL_INITIAL (decl) = init;
221 finish_decl (pushdecl (decl), init, NULL_TREE);
224 /* Given a chain of STRING_CST nodes,
225 concatenate them into one STRING_CST
226 and give it a suitable array-of-chars data type. */
229 combine_strings (strings)
232 register tree value, t;
233 register int length = 1;
236 int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
239 if (TREE_CHAIN (strings))
241 /* More than one in the chain, so concatenate. */
242 register char *p, *q;
244 /* Don't include the \0 at the end of each substring,
245 except for the last one.
246 Count wide strings and ordinary strings separately. */
247 for (t = strings; t; t = TREE_CHAIN (t))
249 if (TREE_TYPE (t) == wchar_array_type_node)
251 wide_length += (TREE_STRING_LENGTH (t) - wchar_bytes);
255 length += (TREE_STRING_LENGTH (t) - 1);
258 /* If anything is wide, the non-wides will be converted,
259 which makes them take more space. */
261 length = length * wchar_bytes + wide_length;
263 p = savealloc (length);
265 /* Copy the individual strings into the new combined string.
266 If the combined string is wide, convert the chars to ints
267 for any individual strings that are not wide. */
270 for (t = strings; t; t = TREE_CHAIN (t))
272 int len = (TREE_STRING_LENGTH (t)
273 - ((TREE_TYPE (t) == wchar_array_type_node)
275 if ((TREE_TYPE (t) == wchar_array_type_node) == wide_flag)
277 memcpy (q, TREE_STRING_POINTER (t), len);
283 for (i = 0; i < len; i++)
285 if (WCHAR_TYPE_SIZE == HOST_BITS_PER_SHORT)
286 ((short *) q)[i] = TREE_STRING_POINTER (t)[i];
288 ((int *) q)[i] = TREE_STRING_POINTER (t)[i];
290 q += len * wchar_bytes;
296 for (i = 0; i < wchar_bytes; i++)
302 value = make_node (STRING_CST);
303 TREE_STRING_POINTER (value) = p;
304 TREE_STRING_LENGTH (value) = length;
309 length = TREE_STRING_LENGTH (value);
310 if (TREE_TYPE (value) == wchar_array_type_node)
314 /* Compute the number of elements, for the array type. */
315 nchars = wide_flag ? length / wchar_bytes : length;
317 /* Create the array type for the string constant.
318 -Wwrite-strings says make the string constant an array of const char
319 so that copying it to a non-const pointer will get a warning.
320 For C++, this is the standard behavior. */
321 if (flag_const_strings
322 && (! flag_traditional && ! flag_writable_strings))
325 = build_type_variant (wide_flag ? wchar_type_node : char_type_node,
328 = build_array_type (elements,
329 build_index_type (build_int_2 (nchars - 1, 0)));
333 = build_array_type (wide_flag ? wchar_type_node : char_type_node,
334 build_index_type (build_int_2 (nchars - 1, 0)));
336 TREE_CONSTANT (value) = 1;
337 TREE_READONLY (value) = ! flag_writable_strings;
338 TREE_STATIC (value) = 1;
342 /* To speed up processing of attributes, we maintain an array of
343 IDENTIFIER_NODES and the corresponding attribute types. */
345 /* Array to hold attribute information. */
347 static struct {enum attrs id; tree name; int min, max, decl_req;} attrtab[50];
349 static int attrtab_idx = 0;
351 /* Add an entry to the attribute table above. */
354 add_attribute (id, string, min_len, max_len, decl_req)
357 int min_len, max_len;
362 attrtab[attrtab_idx].id = id;
363 attrtab[attrtab_idx].name = get_identifier (string);
364 attrtab[attrtab_idx].min = min_len;
365 attrtab[attrtab_idx].max = max_len;
366 attrtab[attrtab_idx++].decl_req = decl_req;
368 sprintf (buf, "__%s__", string);
370 attrtab[attrtab_idx].id = id;
371 attrtab[attrtab_idx].name = get_identifier (buf);
372 attrtab[attrtab_idx].min = min_len;
373 attrtab[attrtab_idx].max = max_len;
374 attrtab[attrtab_idx++].decl_req = decl_req;
377 /* Initialize attribute table. */
382 add_attribute (A_PACKED, "packed", 0, 0, 0);
383 add_attribute (A_NOCOMMON, "nocommon", 0, 0, 1);
384 add_attribute (A_COMMON, "common", 0, 0, 1);
385 add_attribute (A_NORETURN, "noreturn", 0, 0, 1);
386 add_attribute (A_NORETURN, "volatile", 0, 0, 1);
387 add_attribute (A_UNUSED, "unused", 0, 0, 0);
388 add_attribute (A_CONST, "const", 0, 0, 1);
389 add_attribute (A_T_UNION, "transparent_union", 0, 0, 0);
390 add_attribute (A_CONSTRUCTOR, "constructor", 0, 0, 1);
391 add_attribute (A_DESTRUCTOR, "destructor", 0, 0, 1);
392 add_attribute (A_MODE, "mode", 1, 1, 1);
393 add_attribute (A_SECTION, "section", 1, 1, 1);
394 add_attribute (A_ALIGNED, "aligned", 0, 1, 0);
395 add_attribute (A_FORMAT, "format", 3, 3, 1);
396 add_attribute (A_FORMAT_ARG, "format_arg", 1, 1, 1);
397 add_attribute (A_WEAK, "weak", 0, 0, 1);
398 add_attribute (A_ALIAS, "alias", 1, 1, 1);
399 add_attribute (A_NO_INSTRUMENT_FUNCTION, "no_instrument_function", 0, 0, 1);
400 add_attribute (A_NO_CHECK_MEMORY_USAGE, "no_check_memory_usage", 0, 0, 1);
403 /* Default implementation of valid_lang_attribute, below. By default, there
404 are no language-specific attributes. */
407 default_valid_lang_attribute (attr_name, attr_args, decl, type)
408 tree attr_name ATTRIBUTE_UNUSED;
409 tree attr_args ATTRIBUTE_UNUSED;
410 tree decl ATTRIBUTE_UNUSED;
411 tree type ATTRIBUTE_UNUSED;
416 /* Return a 1 if ATTR_NAME and ATTR_ARGS denote a valid language-specific
417 attribute for either declaration DECL or type TYPE and 0 otherwise. */
419 int (*valid_lang_attribute) PROTO ((tree, tree, tree, tree))
420 = default_valid_lang_attribute;
422 /* Process the attributes listed in ATTRIBUTES and PREFIX_ATTRIBUTES
423 and install them in NODE, which is either a DECL (including a TYPE_DECL)
424 or a TYPE. PREFIX_ATTRIBUTES can appear after the declaration specifiers
425 and declaration modifiers but before the declaration proper. */
428 decl_attributes (node, attributes, prefix_attributes)
429 tree node, attributes, prefix_attributes;
431 tree decl = 0, type = 0;
435 if (attrtab_idx == 0)
438 if (TREE_CODE_CLASS (TREE_CODE (node)) == 'd')
441 type = TREE_TYPE (decl);
442 is_type = TREE_CODE (node) == TYPE_DECL;
444 else if (TREE_CODE_CLASS (TREE_CODE (node)) == 't')
445 type = node, is_type = 1;
447 #ifdef PRAGMA_INSERT_ATTRIBUTES
448 /* If the code in c-pragma.c wants to insert some attributes then
449 allow it to do so. Do this before allowing machine back ends to
450 insert attributes, so that they have the opportunity to override
451 anything done here. */
452 PRAGMA_INSERT_ATTRIBUTES (node, & attributes, & prefix_attributes);
455 #ifdef INSERT_ATTRIBUTES
456 INSERT_ATTRIBUTES (node, & attributes, & prefix_attributes);
459 attributes = chainon (prefix_attributes, attributes);
461 for (a = attributes; a; a = TREE_CHAIN (a))
463 tree name = TREE_PURPOSE (a);
464 tree args = TREE_VALUE (a);
468 for (i = 0; i < attrtab_idx; i++)
469 if (attrtab[i].name == name)
472 if (i == attrtab_idx)
474 if (! valid_machine_attribute (name, args, decl, type)
475 && ! (* valid_lang_attribute) (name, args, decl, type))
476 warning ("`%s' attribute directive ignored",
477 IDENTIFIER_POINTER (name));
479 type = TREE_TYPE (decl);
482 else if (attrtab[i].decl_req && decl == 0)
484 warning ("`%s' attribute does not apply to types",
485 IDENTIFIER_POINTER (name));
488 else if (list_length (args) < attrtab[i].min
489 || list_length (args) > attrtab[i].max)
491 error ("wrong number of arguments specified for `%s' attribute",
492 IDENTIFIER_POINTER (name));
501 TYPE_PACKED (type) = 1;
502 else if (TREE_CODE (decl) == FIELD_DECL)
503 DECL_PACKED (decl) = 1;
504 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
505 used for DECL_REGISTER. It wouldn't mean anything anyway. */
507 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
511 if (TREE_CODE (decl) == VAR_DECL)
512 DECL_COMMON (decl) = 0;
514 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
518 if (TREE_CODE (decl) == VAR_DECL)
519 DECL_COMMON (decl) = 1;
521 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
525 if (TREE_CODE (decl) == FUNCTION_DECL)
526 TREE_THIS_VOLATILE (decl) = 1;
527 else if (TREE_CODE (type) == POINTER_TYPE
528 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
529 TREE_TYPE (decl) = type
531 (build_type_variant (TREE_TYPE (type),
532 TREE_READONLY (TREE_TYPE (type)), 1));
534 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
539 TREE_USED (type) = 1;
540 else if (TREE_CODE (decl) == PARM_DECL
541 || TREE_CODE (decl) == VAR_DECL
542 || TREE_CODE (decl) == FUNCTION_DECL
543 || TREE_CODE (decl) == LABEL_DECL)
544 TREE_USED (decl) = 1;
546 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
550 if (TREE_CODE (decl) == FUNCTION_DECL)
551 TREE_READONLY (decl) = 1;
552 else if (TREE_CODE (type) == POINTER_TYPE
553 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
554 TREE_TYPE (decl) = type
556 (build_type_variant (TREE_TYPE (type), 1,
557 TREE_THIS_VOLATILE (TREE_TYPE (type))));
559 warning ( "`%s' attribute ignored", IDENTIFIER_POINTER (name));
564 && TREE_CODE (type) == UNION_TYPE
566 || (TYPE_FIELDS (type) != 0
567 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))))
568 TYPE_TRANSPARENT_UNION (type) = 1;
569 else if (decl != 0 && TREE_CODE (decl) == PARM_DECL
570 && TREE_CODE (type) == UNION_TYPE
571 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))
572 DECL_TRANSPARENT_UNION (decl) = 1;
574 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
578 if (TREE_CODE (decl) == FUNCTION_DECL
579 && TREE_CODE (type) == FUNCTION_TYPE
580 && decl_function_context (decl) == 0)
582 DECL_STATIC_CONSTRUCTOR (decl) = 1;
583 TREE_USED (decl) = 1;
586 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
590 if (TREE_CODE (decl) == FUNCTION_DECL
591 && TREE_CODE (type) == FUNCTION_TYPE
592 && decl_function_context (decl) == 0)
594 DECL_STATIC_DESTRUCTOR (decl) = 1;
595 TREE_USED (decl) = 1;
598 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
602 if (TREE_CODE (TREE_VALUE (args)) != IDENTIFIER_NODE)
603 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
607 const char *p = IDENTIFIER_POINTER (TREE_VALUE (args));
608 int len = strlen (p);
609 enum machine_mode mode = VOIDmode;
612 if (len > 4 && p[0] == '_' && p[1] == '_'
613 && p[len - 1] == '_' && p[len - 2] == '_')
615 char *newp = (char *) alloca (len - 1);
617 strcpy (newp, &p[2]);
618 newp[len - 4] = '\0';
622 /* Give this decl a type with the specified mode.
623 First check for the special modes. */
624 if (! strcmp (p, "byte"))
626 else if (!strcmp (p, "word"))
628 else if (! strcmp (p, "pointer"))
631 for (j = 0; j < NUM_MACHINE_MODES; j++)
632 if (!strcmp (p, GET_MODE_NAME (j)))
633 mode = (enum machine_mode) j;
635 if (mode == VOIDmode)
636 error ("unknown machine mode `%s'", p);
637 else if (0 == (typefm = type_for_mode (mode,
638 TREE_UNSIGNED (type))))
639 error ("no data type for mode `%s'", p);
642 TREE_TYPE (decl) = type = typefm;
643 DECL_SIZE (decl) = 0;
644 layout_decl (decl, 0);
650 #ifdef ASM_OUTPUT_SECTION_NAME
651 if ((TREE_CODE (decl) == FUNCTION_DECL
652 || TREE_CODE (decl) == VAR_DECL)
653 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
655 if (TREE_CODE (decl) == VAR_DECL
656 && current_function_decl != NULL_TREE
657 && ! TREE_STATIC (decl))
658 error_with_decl (decl,
659 "section attribute cannot be specified for local variables");
660 /* The decl may have already been given a section attribute from
661 a previous declaration. Ensure they match. */
662 else if (DECL_SECTION_NAME (decl) != NULL_TREE
663 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
664 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
665 error_with_decl (node,
666 "section of `%s' conflicts with previous declaration");
668 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
671 error_with_decl (node,
672 "section attribute not allowed for `%s'");
674 error_with_decl (node,
675 "section attributes are not supported for this target");
682 = (args ? TREE_VALUE (args)
683 : size_int (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
686 /* Strip any NOPs of any kind. */
687 while (TREE_CODE (align_expr) == NOP_EXPR
688 || TREE_CODE (align_expr) == CONVERT_EXPR
689 || TREE_CODE (align_expr) == NON_LVALUE_EXPR)
690 align_expr = TREE_OPERAND (align_expr, 0);
692 if (TREE_CODE (align_expr) != INTEGER_CST)
694 error ("requested alignment is not a constant");
698 align = TREE_INT_CST_LOW (align_expr) * BITS_PER_UNIT;
700 if (exact_log2 (align) == -1)
701 error ("requested alignment is not a power of 2");
703 TYPE_ALIGN (type) = align;
704 else if (TREE_CODE (decl) != VAR_DECL
705 && TREE_CODE (decl) != FIELD_DECL)
706 error_with_decl (decl,
707 "alignment may not be specified for `%s'");
709 DECL_ALIGN (decl) = align;
715 tree format_type_id = TREE_VALUE (args);
716 tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
717 tree first_arg_num_expr
718 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
722 enum format_type format_type;
726 if (TREE_CODE (decl) != FUNCTION_DECL)
728 error_with_decl (decl,
729 "argument format specified for non-function `%s'");
733 if (TREE_CODE (format_type_id) != IDENTIFIER_NODE)
735 error_with_decl (decl, "unrecognized format specifier");
740 const char *p = IDENTIFIER_POINTER (format_type_id);
742 if (!strcmp (p, "printf") || !strcmp (p, "__printf__"))
744 format_type = printf_format_type;
747 else if (!strcmp (p, "printf0") || !strcmp (p, "__printf0__"))
749 format_type = printf_format_type;
752 else if (!strcmp (p, "scanf") || !strcmp (p, "__scanf__"))
754 format_type = scanf_format_type;
757 else if (!strcmp (p, "strftime")
758 || !strcmp (p, "__strftime__"))
760 format_type = strftime_format_type;
765 warning ("`%s' is an unrecognized format function type", p);
770 /* Strip any conversions from the string index and first arg number
771 and verify they are constants. */
772 while (TREE_CODE (format_num_expr) == NOP_EXPR
773 || TREE_CODE (format_num_expr) == CONVERT_EXPR
774 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
775 format_num_expr = TREE_OPERAND (format_num_expr, 0);
777 while (TREE_CODE (first_arg_num_expr) == NOP_EXPR
778 || TREE_CODE (first_arg_num_expr) == CONVERT_EXPR
779 || TREE_CODE (first_arg_num_expr) == NON_LVALUE_EXPR)
780 first_arg_num_expr = TREE_OPERAND (first_arg_num_expr, 0);
782 if (TREE_CODE (format_num_expr) != INTEGER_CST
783 || TREE_CODE (first_arg_num_expr) != INTEGER_CST)
785 error ("format string has non-constant operand number");
789 format_num = TREE_INT_CST_LOW (format_num_expr);
790 first_arg_num = TREE_INT_CST_LOW (first_arg_num_expr);
791 if (first_arg_num != 0 && first_arg_num <= format_num)
793 error ("format string arg follows the args to be formatted");
797 /* If a parameter list is specified, verify that the format_num
798 argument is actually a string, in case the format attribute
800 argument = TYPE_ARG_TYPES (type);
803 for (arg_num = 1; ; ++arg_num)
805 if (argument == 0 || arg_num == format_num)
807 argument = TREE_CHAIN (argument);
810 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
811 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
814 error ("format string arg not a string type");
817 if (first_arg_num != 0)
819 /* Verify that first_arg_num points to the last arg,
822 arg_num++, argument = TREE_CHAIN (argument);
823 if (arg_num != first_arg_num)
825 error ("args to be formatted is not ...");
831 record_function_format (DECL_NAME (decl),
832 DECL_ASSEMBLER_NAME (decl),
833 format_type, null_format_ok, format_num,
840 tree format_num_expr = TREE_VALUE (args);
841 int format_num, arg_num;
844 if (TREE_CODE (decl) != FUNCTION_DECL)
846 error_with_decl (decl,
847 "argument format specified for non-function `%s'");
851 /* Strip any conversions from the first arg number and verify it
853 while (TREE_CODE (format_num_expr) == NOP_EXPR
854 || TREE_CODE (format_num_expr) == CONVERT_EXPR
855 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
856 format_num_expr = TREE_OPERAND (format_num_expr, 0);
858 if (TREE_CODE (format_num_expr) != INTEGER_CST)
860 error ("format string has non-constant operand number");
864 format_num = TREE_INT_CST_LOW (format_num_expr);
866 /* If a parameter list is specified, verify that the format_num
867 argument is actually a string, in case the format attribute
869 argument = TYPE_ARG_TYPES (type);
872 for (arg_num = 1; ; ++arg_num)
874 if (argument == 0 || arg_num == format_num)
876 argument = TREE_CHAIN (argument);
879 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
880 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
883 error ("format string arg not a string type");
888 if (TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) != POINTER_TYPE
889 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_TYPE (decl))))
892 error ("function does not return string type");
896 record_international_format (DECL_NAME (decl),
897 DECL_ASSEMBLER_NAME (decl),
907 if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
908 || (TREE_CODE (decl) != FUNCTION_DECL && ! DECL_EXTERNAL (decl)))
909 error_with_decl (decl,
910 "`%s' defined both normally and as an alias");
911 else if (decl_function_context (decl) == 0)
915 id = TREE_VALUE (args);
916 if (TREE_CODE (id) != STRING_CST)
918 error ("alias arg not a string");
921 id = get_identifier (TREE_STRING_POINTER (id));
923 if (TREE_CODE (decl) == FUNCTION_DECL)
924 DECL_INITIAL (decl) = error_mark_node;
926 DECL_EXTERNAL (decl) = 0;
927 assemble_alias (decl, id);
930 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
933 case A_NO_CHECK_MEMORY_USAGE:
934 if (TREE_CODE (decl) != FUNCTION_DECL)
936 error_with_decl (decl,
937 "`%s' attribute applies only to functions",
938 IDENTIFIER_POINTER (name));
940 else if (DECL_INITIAL (decl))
942 error_with_decl (decl,
943 "can't set `%s' attribute after definition",
944 IDENTIFIER_POINTER (name));
947 DECL_NO_CHECK_MEMORY_USAGE (decl) = 1;
950 case A_NO_INSTRUMENT_FUNCTION:
951 if (TREE_CODE (decl) != FUNCTION_DECL)
953 error_with_decl (decl,
954 "`%s' attribute applies only to functions",
955 IDENTIFIER_POINTER (name));
957 else if (DECL_INITIAL (decl))
959 error_with_decl (decl,
960 "can't set `%s' attribute after definition",
961 IDENTIFIER_POINTER (name));
964 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
970 /* Split SPECS_ATTRS, a list of declspecs and prefix attributes, into two
971 lists. SPECS_ATTRS may also be just a typespec (eg: RECORD_TYPE).
973 The head of the declspec list is stored in DECLSPECS.
974 The head of the attribute list is stored in PREFIX_ATTRIBUTES.
976 Note that attributes in SPECS_ATTRS are stored in the TREE_PURPOSE of
977 the list elements. We drop the containing TREE_LIST nodes and link the
978 resulting attributes together the way decl_attributes expects them. */
981 split_specs_attrs (specs_attrs, declspecs, prefix_attributes)
983 tree *declspecs, *prefix_attributes;
985 tree t, s, a, next, specs, attrs;
987 /* This can happen after an __extension__ in pedantic mode. */
988 if (specs_attrs != NULL_TREE
989 && TREE_CODE (specs_attrs) == INTEGER_CST)
991 *declspecs = NULL_TREE;
992 *prefix_attributes = NULL_TREE;
996 /* This can happen in c++ (eg: decl: typespec initdecls ';'). */
997 if (specs_attrs != NULL_TREE
998 && TREE_CODE (specs_attrs) != TREE_LIST)
1000 *declspecs = specs_attrs;
1001 *prefix_attributes = NULL_TREE;
1005 /* Remember to keep the lists in the same order, element-wise. */
1007 specs = s = NULL_TREE;
1008 attrs = a = NULL_TREE;
1009 for (t = specs_attrs; t; t = next)
1011 next = TREE_CHAIN (t);
1012 /* Declspecs have a non-NULL TREE_VALUE. */
1013 if (TREE_VALUE (t) != NULL_TREE)
1015 if (specs == NULL_TREE)
1025 if (attrs == NULL_TREE)
1026 attrs = a = TREE_PURPOSE (t);
1029 TREE_CHAIN (a) = TREE_PURPOSE (t);
1030 a = TREE_PURPOSE (t);
1032 /* More attrs can be linked here, move A to the end. */
1033 while (TREE_CHAIN (a) != NULL_TREE)
1038 /* Terminate the lists. */
1040 TREE_CHAIN (s) = NULL_TREE;
1042 TREE_CHAIN (a) = NULL_TREE;
1046 *prefix_attributes = attrs;
1049 /* Strip attributes from SPECS_ATTRS, a list of declspecs and attributes.
1050 This function is used by the parser when a rule will accept attributes
1051 in a particular position, but we don't want to support that just yet.
1053 A warning is issued for every ignored attribute. */
1056 strip_attrs (specs_attrs)
1061 split_specs_attrs (specs_attrs, &specs, &attrs);
1065 warning ("`%s' attribute ignored",
1066 IDENTIFIER_POINTER (TREE_PURPOSE (attrs)));
1067 attrs = TREE_CHAIN (attrs);
1073 /* Check a printf/fprintf/sprintf/scanf/fscanf/sscanf format against
1074 a parameter list. */
1076 #define T_I &integer_type_node
1077 #define T_L &long_integer_type_node
1078 #define T_LL &long_long_integer_type_node
1079 #define T_S &short_integer_type_node
1080 #define T_UI &unsigned_type_node
1081 #define T_UL &long_unsigned_type_node
1082 #define T_ULL &long_long_unsigned_type_node
1083 #define T_US &short_unsigned_type_node
1084 #define T_F &float_type_node
1085 #define T_D &double_type_node
1086 #define T_LD &long_double_type_node
1087 #define T_C &char_type_node
1088 #define T_UC &unsigned_char_type_node
1089 #define T_V &void_type_node
1090 #define T_W &wchar_type_node
1091 #define T_ST &sizetype
1094 const char *format_chars;
1096 /* Type of argument if no length modifier is used. */
1098 /* Type of argument if length modifier for shortening to byte is used.
1099 If NULL, then this modifier is not allowed. */
1101 /* Type of argument if length modifier for shortening is used.
1102 If NULL, then this modifier is not allowed. */
1104 /* Type of argument if length modifier `l' is used.
1105 If NULL, then this modifier is not allowed. */
1107 /* Type of argument if length modifier `q' or `ll' is used.
1108 If NULL, then this modifier is not allowed. */
1110 /* Type of argument if length modifier `L' is used.
1111 If NULL, then this modifier is not allowed. */
1113 /* Type of argument if length modifier `Z' is used.
1114 If NULL, then this modifier is not allowed. */
1116 /* List of other modifier characters allowed with these options. */
1117 const char *flag_chars;
1120 static format_char_info print_char_table[] = {
1121 /* FreeBSD kernel extensions. */
1122 { "D", 1, T_C, NULL, NULL, NULL, NULL, NULL, NULL, "-wp" },
1123 { "b", 1, T_C, NULL, NULL, NULL, NULL, NULL, NULL, "-wp" },
1124 { "rz", 0, T_I, NULL, NULL, T_L, NULL, NULL, NULL, "-wp0 +#" },
1125 #define unextended_print_char_table (print_char_table + 3)
1126 { "di", 0, T_I, T_I, T_I, T_L, T_LL, T_LL, T_ST, "-wp0 +" },
1127 { "oxX", 0, T_UI, T_UI, T_UI, T_UL, T_ULL, T_ULL, T_ST, "-wp0#" },
1128 { "u", 0, T_UI, T_UI, T_UI, T_UL, T_ULL, T_ULL, T_ST, "-wp0" },
1129 /* A GNU extension. */
1130 { "m", 0, T_V, NULL, NULL, NULL, NULL, NULL, NULL, "-wp" },
1131 { "feEgGaA", 0, T_D, NULL, NULL, NULL, NULL, T_LD, NULL, "-wp0 +#" },
1132 { "c", 0, T_I, NULL, NULL, T_W, NULL, NULL, NULL, "-w" },
1133 { "C", 0, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "-w" },
1134 { "s", 1, T_C, NULL, NULL, T_W, NULL, NULL, NULL, "-wp" },
1135 { "S", 1, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "-wp" },
1136 { "p", 1, T_V, NULL, NULL, NULL, NULL, NULL, NULL, "-w" },
1137 { "n", 1, T_I, NULL, T_S, T_L, T_LL, NULL, NULL, "" },
1138 { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
1141 static format_char_info scan_char_table[] = {
1142 { "di", 1, T_I, T_C, T_S, T_L, T_LL, T_LL, NULL, "*" },
1143 { "ouxX", 1, T_UI, T_UC, T_US, T_UL, T_ULL, T_ULL, NULL, "*" },
1144 { "efgEGaA", 1, T_F, NULL, NULL, T_D, NULL, T_LD, NULL, "*" },
1145 { "c", 1, T_C, NULL, NULL, T_W, NULL, NULL, NULL, "*" },
1146 { "s", 1, T_C, NULL, NULL, T_W, NULL, NULL, NULL, "*a" },
1147 { "[", 1, T_C, NULL, NULL, NULL, NULL, NULL, NULL, "*a" },
1148 { "C", 1, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "*" },
1149 { "S", 1, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "*a" },
1150 { "p", 2, T_V, NULL, NULL, NULL, NULL, NULL, NULL, "*" },
1151 { "n", 1, T_I, T_C, T_S, T_L, T_LL, NULL, NULL, "" },
1152 { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
1155 /* Handle format characters recognized by glibc's strftime.c.
1156 '2' - MUST do years as only two digits
1157 '3' - MAY do years as only two digits (depending on locale)
1158 'E' - E modifier is acceptable
1159 'O' - O modifier is acceptable to Standard C
1160 'o' - O modifier is acceptable as a GNU extension
1161 'G' - other GNU extensions */
1163 static format_char_info time_char_table[] = {
1164 { "y", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "2EO-_0w" },
1165 { "D", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "2" },
1166 { "g", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "2O-_0w" },
1167 { "cx", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "3E" },
1168 { "%+RTXnrt", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "" },
1169 { "P", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "G" },
1170 { "HIMSUWdemw", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0Ow" },
1171 { "Vju", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0Oow" },
1172 { "Gklsz", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0OGw" },
1173 { "ABZa", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "^#" },
1174 { "p", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "#" },
1175 { "bh", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "^" },
1176 { "CY", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0EOw" },
1177 { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
1180 typedef struct function_format_info
1182 struct function_format_info *next; /* next structure on the list */
1183 tree name; /* identifier such as "printf" */
1184 tree assembler_name; /* optional mangled identifier (for C++) */
1185 enum format_type format_type; /* type of format (printf, scanf, etc.) */
1186 int null_format_ok; /* TRUE if the format string may be NULL */
1187 int format_num; /* number of format argument */
1188 int first_arg_num; /* number of first arg (zero for varargs) */
1189 } function_format_info;
1191 static function_format_info *function_format_list = NULL;
1193 typedef struct international_format_info
1195 struct international_format_info *next; /* next structure on the list */
1196 tree name; /* identifier such as "gettext" */
1197 tree assembler_name; /* optional mangled identifier (for C++) */
1198 int format_num; /* number of format argument */
1199 } international_format_info;
1201 static international_format_info *international_format_list = NULL;
1203 static void check_format_info PROTO((function_format_info *, tree));
1205 /* Initialize the table of functions to perform format checking on.
1206 The ANSI functions are always checked (whether <stdio.h> is
1207 included or not), since it is common to call printf without
1208 including <stdio.h>. There shouldn't be a problem with this,
1209 since ANSI reserves these function names whether you include the
1210 header file or not. In any case, the checking is harmless.
1212 Also initialize the name of function that modify the format string for
1213 internationalization purposes. */
1216 init_function_format_info ()
1218 record_function_format (get_identifier ("printf"), NULL_TREE,
1219 printf_format_type, 0, 1, 2);
1220 record_function_format (get_identifier ("fprintf"), NULL_TREE,
1221 printf_format_type, 0, 2, 3);
1222 record_function_format (get_identifier ("sprintf"), NULL_TREE,
1223 printf_format_type, 0, 2, 3);
1224 record_function_format (get_identifier ("scanf"), NULL_TREE,
1225 scanf_format_type, 0, 1, 2);
1226 record_function_format (get_identifier ("fscanf"), NULL_TREE,
1227 scanf_format_type, 0, 2, 3);
1228 record_function_format (get_identifier ("sscanf"), NULL_TREE,
1229 scanf_format_type, 0, 2, 3);
1230 record_function_format (get_identifier ("vprintf"), NULL_TREE,
1231 printf_format_type, 0, 1, 0);
1232 record_function_format (get_identifier ("vfprintf"), NULL_TREE,
1233 printf_format_type, 0, 2, 0);
1234 record_function_format (get_identifier ("vsprintf"), NULL_TREE,
1235 printf_format_type, 0, 2, 0);
1236 record_function_format (get_identifier ("strftime"), NULL_TREE,
1237 strftime_format_type, 0, 3, 0);
1239 record_international_format (get_identifier ("gettext"), NULL_TREE, 1);
1240 record_international_format (get_identifier ("dgettext"), NULL_TREE, 2);
1241 record_international_format (get_identifier ("dcgettext"), NULL_TREE, 2);
1244 /* Record information for argument format checking. FUNCTION_IDENT is
1245 the identifier node for the name of the function to check (its decl
1246 need not exist yet).
1247 FORMAT_TYPE specifies the type of format checking. FORMAT_NUM is the number
1248 of the argument which is the format control string (starting from 1).
1249 FIRST_ARG_NUM is the number of the first actual argument to check
1250 against the format string, or zero if no checking is not be done
1251 (e.g. for varargs such as vfprintf). */
1254 record_function_format (name, assembler_name, format_type, null_format_ok,
1255 format_num, first_arg_num)
1257 tree assembler_name;
1258 enum format_type format_type;
1263 function_format_info *info;
1265 /* Re-use existing structure if it's there. */
1267 for (info = function_format_list; info; info = info->next)
1269 if (info->name == name && info->assembler_name == assembler_name)
1274 info = (function_format_info *) xmalloc (sizeof (function_format_info));
1275 info->next = function_format_list;
1276 function_format_list = info;
1279 info->assembler_name = assembler_name;
1282 info->format_type = format_type;
1283 info->null_format_ok = null_format_ok;
1284 info->format_num = format_num;
1285 info->first_arg_num = first_arg_num;
1288 /* Record information for the names of function that modify the format
1289 argument to format functions. FUNCTION_IDENT is the identifier node for
1290 the name of the function (its decl need not exist yet) and FORMAT_NUM is
1291 the number of the argument which is the format control string (starting
1295 record_international_format (name, assembler_name, format_num)
1297 tree assembler_name;
1300 international_format_info *info;
1302 /* Re-use existing structure if it's there. */
1304 for (info = international_format_list; info; info = info->next)
1306 if (info->name == name && info->assembler_name == assembler_name)
1313 = (international_format_info *)
1314 xmalloc (sizeof (international_format_info));
1315 info->next = international_format_list;
1316 international_format_list = info;
1319 info->assembler_name = assembler_name;
1322 info->format_num = format_num;
1328 warning ("too few arguments for format");
1331 function_format_info *
1332 find_function_format (name, assembler_name)
1334 tree assembler_name;
1336 function_format_info *info;
1338 for (info = function_format_list; info; info = info->next)
1339 if (info->assembler_name
1340 ? (info->assembler_name == assembler_name)
1341 : (info->name == name))
1346 /* Check the argument list of a call to printf, scanf, etc.
1347 NAME is the function identifier.
1348 ASSEMBLER_NAME is the function's assembler identifier.
1349 (Either NAME or ASSEMBLER_NAME, but not both, may be NULL_TREE.)
1350 PARAMS is the list of argument values. */
1353 check_function_format (name, assembler_name, params)
1355 tree assembler_name;
1358 function_format_info *info;
1360 /* See if this function is a format function. */
1361 info = find_function_format (name, assembler_name);
1364 check_format_info (info, params);
1367 /* Check the argument list of a call to printf, scanf, etc.
1368 INFO points to the function_format_info structure.
1369 PARAMS is the list of argument values. */
1372 check_format_info (info, params)
1373 function_format_info *info;
1378 int suppressed, wide, precise;
1379 int length_char = 0;
1386 tree first_fillin_param;
1387 const char *format_chars;
1388 format_char_info *fci = NULL;
1390 int has_operand_number = 0;
1392 /* Skip to format argument. If the argument isn't available, there's
1393 no work for us to do; prototype checking will catch the problem. */
1394 for (arg_num = 1; ; ++arg_num)
1398 if (arg_num == info->format_num)
1400 params = TREE_CHAIN (params);
1402 format_tree = TREE_VALUE (params);
1403 params = TREE_CHAIN (params);
1404 if (format_tree == 0)
1407 /* We can only check the format if it's a string constant. */
1409 while (TREE_CODE (format_tree) == NOP_EXPR)
1410 format_tree = TREE_OPERAND (format_tree, 0); /* strip coercion */
1412 if (TREE_CODE (format_tree) == CALL_EXPR
1413 && TREE_CODE (TREE_OPERAND (format_tree, 0)) == ADDR_EXPR
1414 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0))
1417 tree function = TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0);
1419 /* See if this is a call to a known internationalization function
1420 that modifies the format arg. */
1421 international_format_info *info;
1423 for (info = international_format_list; info; info = info->next)
1424 if (info->assembler_name
1425 ? (info->assembler_name == DECL_ASSEMBLER_NAME (function))
1426 : (info->name == DECL_NAME (function)))
1431 for (inner_args = TREE_OPERAND (format_tree, 1), i = 1;
1433 inner_args = TREE_CHAIN (inner_args), i++)
1434 if (i == info->format_num)
1436 format_tree = TREE_VALUE (inner_args);
1438 while (TREE_CODE (format_tree) == NOP_EXPR)
1439 format_tree = TREE_OPERAND (format_tree, 0);
1444 if (TREE_CODE (format_tree) == COND_EXPR)
1446 format_tree = TREE_OPERAND(format_tree, 1);
1450 if (integer_zerop (format_tree))
1452 if (!info->null_format_ok)
1453 warning ("null format string");
1456 if (TREE_CODE (format_tree) != ADDR_EXPR)
1458 if ((info->first_arg_num == 0) &&
1459 (TREE_CODE(format_tree) == PARM_DECL))
1461 function_format_info *i2;
1465 /* Now, we need to determine if the current function is printf-like,
1466 and, if so, if the parameter we have here is as a parameter of
1467 the current function and is in the argument slot declared to
1468 contain the format argument. */
1470 p = current_function_decl;
1472 i2 = find_function_format (p->decl.name, p->decl.assembler_name);
1476 if (warn_format > 1)
1477 warning("non-constant format parameter");
1481 for (n = 1, p = current_function_decl->decl.arguments;
1482 (n < i2->format_num) && (p != NULL);
1483 n++, p = TREE_CHAIN(p))
1485 if ((p == NULL) || (n != i2->format_num))
1486 warning("can't find format arg for current format function");
1487 else if (p != format_tree)
1488 warning("format argument passed here is not declared as format argument");
1491 else if ((info->format_type != strftime_format_type) &&
1493 warning("non-constant format parameter");
1496 format_tree = TREE_OPERAND (format_tree, 0);
1497 if (warn_format > 1 &&
1498 (TREE_CODE (format_tree) == VAR_DECL) &&
1499 TREE_READONLY(format_tree) &&
1500 (DECL_INITIAL(format_tree) != NULL) &&
1501 TREE_CODE(DECL_INITIAL(format_tree)) == STRING_CST)
1502 format_tree = DECL_INITIAL(format_tree);
1504 if (TREE_CODE (format_tree) != STRING_CST)
1506 if ((info->format_type != strftime_format_type) &&
1508 warning("non-constant format parameter");
1511 format_chars = TREE_STRING_POINTER (format_tree);
1512 format_length = TREE_STRING_LENGTH (format_tree);
1513 if (format_length <= 1)
1514 warning ("zero-length format string");
1515 if (format_chars[--format_length] != 0)
1517 warning ("unterminated format string");
1520 /* Skip to first argument to check. */
1521 while (arg_num + 1 < info->first_arg_num)
1525 params = TREE_CHAIN (params);
1529 first_fillin_param = params;
1533 if (*format_chars == 0)
1535 if (format_chars - TREE_STRING_POINTER (format_tree) != format_length)
1536 warning ("embedded `\\0' in format");
1537 if (info->first_arg_num != 0 && params != 0 && ! has_operand_number)
1539 if (warn_format_extra_args)
1540 warning ("too many arguments for format");
1544 if (*format_chars++ != '%')
1546 if (*format_chars == 0)
1548 warning ("spurious trailing `%%' in format");
1551 if (*format_chars == '%')
1557 suppressed = wide = precise = FALSE;
1558 if (info->format_type == scanf_format_type)
1560 suppressed = *format_chars == '*';
1563 while (ISDIGIT (*format_chars))
1566 else if (info->format_type == strftime_format_type)
1568 while (*format_chars != 0 && index ("_-0^#", *format_chars) != 0)
1571 warning ("ANSI C does not support the strftime `%c' flag",
1573 if (index (flag_chars, *format_chars) != 0)
1575 warning ("repeated `%c' flag in format",
1581 i = strlen (flag_chars);
1582 flag_chars[i++] = *format_chars++;
1586 while (ISDIGIT ((unsigned char) *format_chars))
1591 if (wide && pedantic)
1592 warning ("ANSI C does not support strftime format width");
1593 if (*format_chars == 'E' || *format_chars == 'O')
1595 i = strlen (flag_chars);
1596 flag_chars[i++] = *format_chars++;
1598 if (*format_chars == 'E' || *format_chars == 'O')
1600 warning ("multiple E/O modifiers in format");
1601 while (*format_chars == 'E' || *format_chars == 'O')
1606 else if (info->format_type == printf_format_type)
1608 /* See if we have a number followed by a dollar sign. If we do,
1609 it is an operand number, so set PARAMS to that operand. */
1610 if (*format_chars >= '0' && *format_chars <= '9')
1612 const char *p = format_chars;
1614 while (*p >= '0' && *p++ <= '9')
1619 int opnum = atoi (format_chars);
1621 params = first_fillin_param;
1622 format_chars = p + 1;
1623 has_operand_number = 1;
1625 for (i = 1; i < opnum && params != 0; i++)
1626 params = TREE_CHAIN (params);
1628 if (opnum == 0 || params == 0)
1630 warning ("operand number out of range in format");
1636 while (*format_chars != 0 && index (" +#0-", *format_chars) != 0)
1638 if (index (flag_chars, *format_chars) != 0)
1639 warning ("repeated `%c' flag in format", *format_chars++);
1642 i = strlen (flag_chars);
1643 flag_chars[i++] = *format_chars++;
1647 /* "If the space and + flags both appear,
1648 the space flag will be ignored." */
1649 if (index (flag_chars, ' ') != 0
1650 && index (flag_chars, '+') != 0)
1651 warning ("use of both ` ' and `+' flags in format");
1652 /* "If the 0 and - flags both appear,
1653 the 0 flag will be ignored." */
1654 if (index (flag_chars, '0') != 0
1655 && index (flag_chars, '-') != 0)
1656 warning ("use of both `0' and `-' flags in format");
1657 if (*format_chars == '*')
1660 /* "...a field width...may be indicated by an asterisk.
1661 In this case, an int argument supplies the field width..." */
1668 if (info->first_arg_num != 0)
1670 cur_param = TREE_VALUE (params);
1671 params = TREE_CHAIN (params);
1673 /* size_t is generally not valid here.
1674 It will work on most machines, because size_t and int
1675 have the same mode. But might as well warn anyway,
1676 since it will fail on other machines. */
1677 /* XXX should we allow unsigned ints here? */
1678 if ((TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1679 != integer_type_node)
1681 (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1682 != unsigned_type_node))
1683 warning ("precision is not type int (arg %d)", arg_num);
1688 while (ISDIGIT (*format_chars))
1694 if (*format_chars == '.')
1698 if (*format_chars != '*' && !ISDIGIT (*format_chars))
1699 warning ("`.' not followed by `*' or digit in format");
1700 /* "...a...precision...may be indicated by an asterisk.
1701 In this case, an int argument supplies the...precision." */
1702 if (*format_chars == '*')
1704 if (info->first_arg_num != 0)
1712 cur_param = TREE_VALUE (params);
1713 params = TREE_CHAIN (params);
1715 if (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1716 != integer_type_node)
1717 warning ("field width is not type int (arg %d)",
1723 while (ISDIGIT (*format_chars))
1728 if (*format_chars == 'b')
1730 /* There should be an int arg to control the string arg. */
1736 if (info->first_arg_num != 0)
1738 cur_param = TREE_VALUE (params);
1739 params = TREE_CHAIN (params);
1741 if ((TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1742 != integer_type_node)
1744 (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1745 != unsigned_type_node))
1747 warning ("bitmap is not type int (arg %d)", arg_num);
1751 if (*format_chars == 'D')
1753 /* There should be an unsigned char * arg before the string arg. */
1759 if (info->first_arg_num != 0)
1761 cur_param = TREE_VALUE (params);
1762 params = TREE_CHAIN (params);
1764 cur_type = TREE_TYPE (cur_param);
1765 if (TREE_CODE (cur_type) != POINTER_TYPE
1766 || TYPE_MAIN_VARIANT (TREE_TYPE (cur_type))
1767 != unsigned_char_type_node)
1769 warning ("ethernet address is not type unsigned char *"
1778 if (info->format_type != strftime_format_type)
1780 if (*format_chars == 'h' || *format_chars == 'l')
1781 length_char = *format_chars++;
1782 else if ((*format_chars == 'q' || *format_chars == 'L')
1783 && !flag_format_extensions)
1785 length_char = *format_chars++;
1787 warning ("ANSI C does not support the `%c' length modifier",
1790 else if (*format_chars == 'Z')
1792 length_char = *format_chars++;
1794 warning ("ANSI C does not support the `Z' length modifier");
1798 if (length_char == 'l' && *format_chars == 'l')
1800 length_char = 'q', format_chars++;
1801 /* FIXME: Is allowed in ISO C 9x. */
1803 warning ("ANSI C does not support the `ll' length modifier");
1805 else if (length_char == 'h' && *format_chars == 'h')
1807 length_char = 'H', format_chars++;
1808 /* FIXME: Is allowed in ISO C 9x. */
1810 warning ("ANSI C does not support the `hh' length modifier");
1812 if (*format_chars == 'a' && info->format_type == scanf_format_type)
1814 if (format_chars[1] == 's' || format_chars[1] == 'S'
1815 || format_chars[1] == '[')
1817 /* `a' is used as a flag. */
1822 if (suppressed && length_char != 0)
1823 warning ("use of `*' and `%c' together in format", length_char);
1825 format_char = *format_chars;
1826 if (format_char == 0
1827 || (info->format_type != strftime_format_type && format_char == '%'))
1829 warning ("conversion lacks type at end of format");
1832 /* The m, C, and S formats are GNU extensions. */
1833 if (pedantic && info->format_type != strftime_format_type
1834 && (format_char == 'm' || format_char == 'C' || format_char == 'S'))
1835 warning ("ANSI C does not support the `%c' format", format_char);
1836 /* ??? The a and A formats are C9X extensions, and should be allowed
1837 when a C9X option is added. */
1838 if (pedantic && info->format_type != strftime_format_type
1839 && (format_char == 'a' || format_char == 'A'))
1840 warning ("ANSI C does not support the `%c' format", format_char);
1842 switch (info->format_type)
1844 case printf_format_type:
1845 fci = flag_format_extensions ? print_char_table
1846 : unextended_print_char_table;
1848 case scanf_format_type:
1849 fci = scan_char_table;
1851 case strftime_format_type:
1852 fci = time_char_table;
1857 while (fci->format_chars != 0
1858 && index (fci->format_chars, format_char) == 0)
1860 if (fci->format_chars == 0)
1862 if (format_char >= 040 && format_char < 0177)
1863 warning ("unknown conversion type character `%c' in format",
1866 warning ("unknown conversion type character 0x%x in format",
1872 if (index (fci->flag_chars, 'G') != 0)
1873 warning ("ANSI C does not support `%%%c'", format_char);
1874 if (index (fci->flag_chars, 'o') != 0
1875 && index (flag_chars, 'O') != 0)
1876 warning ("ANSI C does not support `%%O%c'", format_char);
1878 if (wide && index (fci->flag_chars, 'w') == 0)
1879 warning ("width used with `%c' format", format_char);
1880 if (index (fci->flag_chars, '2') != 0)
1881 warning ("`%%%c' yields only last 2 digits of year", format_char);
1882 else if (index (fci->flag_chars, '3') != 0)
1883 warning ("`%%%c' yields only last 2 digits of year in some locales on non-BSD systems",
1885 if (precise && index (fci->flag_chars, 'p') == 0)
1886 warning ("precision used with `%c' format", format_char);
1887 if (aflag && index (fci->flag_chars, 'a') == 0)
1889 warning ("`a' flag used with `%c' format", format_char);
1890 /* To simplify the following code. */
1893 /* The a flag is a GNU extension. */
1894 else if (pedantic && aflag)
1895 warning ("ANSI C does not support the `a' flag");
1896 if (info->format_type == scanf_format_type && format_char == '[')
1898 /* Skip over scan set, in case it happens to have '%' in it. */
1899 if (*format_chars == '^')
1901 /* Find closing bracket; if one is hit immediately, then
1902 it's part of the scan set rather than a terminator. */
1903 if (*format_chars == ']')
1905 while (*format_chars && *format_chars != ']')
1907 if (*format_chars != ']')
1908 /* The end of the format string was reached. */
1909 warning ("no closing `]' for `%%[' format");
1913 if (index (fci->flag_chars, '*') == 0)
1914 warning ("suppression of `%c' conversion in format", format_char);
1917 for (i = 0; flag_chars[i] != 0; ++i)
1919 if (index (fci->flag_chars, flag_chars[i]) == 0)
1920 warning ("flag `%c' used with type `%c'",
1921 flag_chars[i], format_char);
1923 if (info->format_type == strftime_format_type)
1925 if (precise && index (flag_chars, '0') != 0
1926 && (format_char == 'd' || format_char == 'i'
1927 || format_char == 'o' || format_char == 'u'
1928 || format_char == 'x' || format_char == 'X'))
1929 warning ("`0' flag ignored with precision specifier and `%c' format",
1931 switch (length_char)
1933 default: wanted_type = fci->nolen ? *(fci->nolen) : 0; break;
1934 case 'H': wanted_type = fci->hhlen ? *(fci->hhlen) : 0; break;
1935 case 'h': wanted_type = fci->hlen ? *(fci->hlen) : 0; break;
1936 case 'l': wanted_type = fci->llen ? *(fci->llen) : 0; break;
1937 case 'q': wanted_type = fci->qlen ? *(fci->qlen) : 0; break;
1938 case 'L': wanted_type = fci->bigllen ? *(fci->bigllen) : 0; break;
1939 case 'Z': wanted_type = fci->zlen ? *fci->zlen : 0; break;
1941 if (wanted_type == 0)
1942 warning ("use of `%c' length character with `%c' type character",
1943 length_char, format_char);
1945 /* Finally. . .check type of argument against desired type! */
1946 if (info->first_arg_num == 0)
1948 if (fci->pointer_count == 0 && wanted_type == void_type_node)
1949 /* This specifier takes no argument. */
1956 cur_param = TREE_VALUE (params);
1957 params = TREE_CHAIN (params);
1959 cur_type = TREE_TYPE (cur_param);
1961 STRIP_NOPS (cur_param);
1963 /* Check the types of any additional pointer arguments
1964 that precede the "real" argument. */
1965 for (i = 0; i < fci->pointer_count + aflag; ++i)
1967 if (TREE_CODE (cur_type) == POINTER_TYPE)
1969 cur_type = TREE_TYPE (cur_type);
1971 if (cur_param != 0 && TREE_CODE (cur_param) == ADDR_EXPR)
1972 cur_param = TREE_OPERAND (cur_param, 0);
1978 if (TREE_CODE (cur_type) != ERROR_MARK)
1979 warning ((fci->pointer_count + aflag == 1
1980 ? "format argument is not a pointer (arg %d)"
1981 : "format argument is not a pointer to a pointer (arg %d)"),
1986 /* See if this is an attempt to write into a const type with
1987 scanf or with printf "%n". */
1988 if ((info->format_type == scanf_format_type
1989 || (info->format_type == printf_format_type
1990 && format_char == 'n'))
1991 && i == fci->pointer_count + aflag
1993 && TREE_CODE (cur_type) != ERROR_MARK
1994 && (TYPE_READONLY (cur_type)
1996 && (TREE_CODE_CLASS (TREE_CODE (cur_param)) == 'c'
1997 || (TREE_CODE_CLASS (TREE_CODE (cur_param)) == 'd'
1998 && TREE_READONLY (cur_param))))))
1999 warning ("writing into constant object (arg %d)", arg_num);
2001 /* Check the type of the "real" argument, if there's a type we want. */
2002 if (i == fci->pointer_count + aflag && wanted_type != 0
2003 && TREE_CODE (cur_type) != ERROR_MARK
2004 && wanted_type != TYPE_MAIN_VARIANT (cur_type)
2005 /* If we want `void *', allow any pointer type.
2006 (Anything else would already have got a warning.) */
2007 && ! (wanted_type == void_type_node
2008 && fci->pointer_count > 0)
2009 /* Don't warn about differences merely in signedness. */
2010 && !(TREE_CODE (wanted_type) == INTEGER_TYPE
2011 && TREE_CODE (TYPE_MAIN_VARIANT (cur_type)) == INTEGER_TYPE
2012 && (TREE_UNSIGNED (wanted_type)
2013 ? wanted_type == (cur_type = unsigned_type (cur_type))
2014 : wanted_type == (cur_type = signed_type (cur_type))))
2015 /* Likewise, "signed char", "unsigned char" and "char" are
2016 equivalent but the above test won't consider them equivalent. */
2017 && ! (wanted_type == char_type_node
2018 && (TYPE_MAIN_VARIANT (cur_type) == signed_char_type_node
2019 || TYPE_MAIN_VARIANT (cur_type) == unsigned_char_type_node)))
2021 register const char *this;
2022 register const char *that;
2024 this = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (wanted_type)));
2026 if (TREE_CODE (cur_type) != ERROR_MARK
2027 && TYPE_NAME (cur_type) != 0
2028 && TREE_CODE (cur_type) != INTEGER_TYPE
2029 && !(TREE_CODE (cur_type) == POINTER_TYPE
2030 && TREE_CODE (TREE_TYPE (cur_type)) == INTEGER_TYPE))
2032 if (TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL
2033 && DECL_NAME (TYPE_NAME (cur_type)) != 0)
2034 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
2036 that = IDENTIFIER_POINTER (TYPE_NAME (cur_type));
2039 /* A nameless type can't possibly match what the format wants.
2040 So there will be a warning for it.
2041 Make up a string to describe vaguely what it is. */
2044 if (TREE_CODE (cur_type) == POINTER_TYPE)
2047 that = "different type";
2050 /* Make the warning better in case of mismatch of int vs long. */
2051 if (TREE_CODE (cur_type) == INTEGER_TYPE
2052 && TREE_CODE (wanted_type) == INTEGER_TYPE
2053 && TYPE_PRECISION (cur_type) == TYPE_PRECISION (wanted_type)
2054 && TYPE_NAME (cur_type) != 0
2055 && TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL)
2056 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
2058 if (strcmp (this, that) != 0)
2059 warning ("%s format, %s arg (arg %d)", this, that, arg_num);
2064 /* Print a warning if a constant expression had overflow in folding.
2065 Invoke this function on every expression that the language
2066 requires to be a constant expression.
2067 Note the ANSI C standard says it is erroneous for a
2068 constant expression to overflow. */
2071 constant_expression_warning (value)
2074 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
2075 || TREE_CODE (value) == COMPLEX_CST)
2076 && TREE_CONSTANT_OVERFLOW (value) && pedantic)
2077 pedwarn ("overflow in constant expression");
2080 /* Print a warning if an expression had overflow in folding.
2081 Invoke this function on every expression that
2082 (1) appears in the source code, and
2083 (2) might be a constant expression that overflowed, and
2084 (3) is not already checked by convert_and_check;
2085 however, do not invoke this function on operands of explicit casts. */
2088 overflow_warning (value)
2091 if ((TREE_CODE (value) == INTEGER_CST
2092 || (TREE_CODE (value) == COMPLEX_CST
2093 && TREE_CODE (TREE_REALPART (value)) == INTEGER_CST))
2094 && TREE_OVERFLOW (value))
2096 TREE_OVERFLOW (value) = 0;
2097 if (skip_evaluation == 0)
2098 warning ("integer overflow in expression");
2100 else if ((TREE_CODE (value) == REAL_CST
2101 || (TREE_CODE (value) == COMPLEX_CST
2102 && TREE_CODE (TREE_REALPART (value)) == REAL_CST))
2103 && TREE_OVERFLOW (value))
2105 TREE_OVERFLOW (value) = 0;
2106 if (skip_evaluation == 0)
2107 warning ("floating point overflow in expression");
2111 /* Print a warning if a large constant is truncated to unsigned,
2112 or if -Wconversion is used and a constant < 0 is converted to unsigned.
2113 Invoke this function on every expression that might be implicitly
2114 converted to an unsigned type. */
2117 unsigned_conversion_warning (result, operand)
2118 tree result, operand;
2120 if (TREE_CODE (operand) == INTEGER_CST
2121 && TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
2122 && TREE_UNSIGNED (TREE_TYPE (result))
2123 && skip_evaluation == 0
2124 && !int_fits_type_p (operand, TREE_TYPE (result)))
2126 if (!int_fits_type_p (operand, signed_type (TREE_TYPE (result))))
2127 /* This detects cases like converting -129 or 256 to unsigned char. */
2128 warning ("large integer implicitly truncated to unsigned type");
2129 else if (warn_conversion)
2130 warning ("negative integer implicitly converted to unsigned type");
2134 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2135 Invoke this function on every expression that is converted implicitly,
2136 i.e. because of language rules and not because of an explicit cast. */
2139 convert_and_check (type, expr)
2142 tree t = convert (type, expr);
2143 if (TREE_CODE (t) == INTEGER_CST)
2145 if (TREE_OVERFLOW (t))
2147 TREE_OVERFLOW (t) = 0;
2149 /* Do not diagnose overflow in a constant expression merely
2150 because a conversion overflowed. */
2151 TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (expr);
2153 /* No warning for converting 0x80000000 to int. */
2154 if (!(TREE_UNSIGNED (type) < TREE_UNSIGNED (TREE_TYPE (expr))
2155 && TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
2156 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (expr))))
2157 /* If EXPR fits in the unsigned version of TYPE,
2158 don't warn unless pedantic. */
2160 || TREE_UNSIGNED (type)
2161 || ! int_fits_type_p (expr, unsigned_type (type)))
2162 && skip_evaluation == 0)
2163 warning ("overflow in implicit constant conversion");
2166 unsigned_conversion_warning (t, expr);
2172 c_expand_expr_stmt (expr)
2175 /* Do default conversion if safe and possibly important,
2176 in case within ({...}). */
2177 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE && lvalue_p (expr))
2178 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
2179 expr = default_conversion (expr);
2181 if (TREE_TYPE (expr) != error_mark_node
2182 && TYPE_SIZE (TREE_TYPE (expr)) == 0
2183 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
2184 error ("expression statement has incomplete type");
2186 expand_expr_stmt (expr);
2189 /* Validate the expression after `case' and apply default promotions. */
2192 check_case_value (value)
2195 if (value == NULL_TREE)
2198 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
2199 STRIP_TYPE_NOPS (value);
2201 if (TREE_CODE (value) != INTEGER_CST
2202 && value != error_mark_node)
2204 error ("case label does not reduce to an integer constant");
2205 value = error_mark_node;
2208 /* Promote char or short to int. */
2209 value = default_conversion (value);
2211 constant_expression_warning (value);
2216 /* Return an integer type with BITS bits of precision,
2217 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
2220 type_for_size (bits, unsignedp)
2224 if (bits == TYPE_PRECISION (integer_type_node))
2225 return unsignedp ? unsigned_type_node : integer_type_node;
2227 if (bits == TYPE_PRECISION (signed_char_type_node))
2228 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2230 if (bits == TYPE_PRECISION (short_integer_type_node))
2231 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2233 if (bits == TYPE_PRECISION (long_integer_type_node))
2234 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2236 if (bits == TYPE_PRECISION (long_long_integer_type_node))
2237 return (unsignedp ? long_long_unsigned_type_node
2238 : long_long_integer_type_node);
2240 if (bits <= TYPE_PRECISION (intQI_type_node))
2241 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2243 if (bits <= TYPE_PRECISION (intHI_type_node))
2244 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2246 if (bits <= TYPE_PRECISION (intSI_type_node))
2247 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2249 if (bits <= TYPE_PRECISION (intDI_type_node))
2250 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2255 /* Return a data type that has machine mode MODE.
2256 If the mode is an integer,
2257 then UNSIGNEDP selects between signed and unsigned types. */
2260 type_for_mode (mode, unsignedp)
2261 enum machine_mode mode;
2264 if (mode == TYPE_MODE (integer_type_node))
2265 return unsignedp ? unsigned_type_node : integer_type_node;
2267 if (mode == TYPE_MODE (signed_char_type_node))
2268 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2270 if (mode == TYPE_MODE (short_integer_type_node))
2271 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2273 if (mode == TYPE_MODE (long_integer_type_node))
2274 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2276 if (mode == TYPE_MODE (long_long_integer_type_node))
2277 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
2279 if (mode == TYPE_MODE (intQI_type_node))
2280 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2282 if (mode == TYPE_MODE (intHI_type_node))
2283 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2285 if (mode == TYPE_MODE (intSI_type_node))
2286 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2288 if (mode == TYPE_MODE (intDI_type_node))
2289 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2291 #if HOST_BITS_PER_WIDE_INT >= 64
2292 if (mode == TYPE_MODE (intTI_type_node))
2293 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2296 if (mode == TYPE_MODE (float_type_node))
2297 return float_type_node;
2299 if (mode == TYPE_MODE (double_type_node))
2300 return double_type_node;
2302 if (mode == TYPE_MODE (long_double_type_node))
2303 return long_double_type_node;
2305 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
2306 return build_pointer_type (char_type_node);
2308 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
2309 return build_pointer_type (integer_type_node);
2314 /* Return the minimum number of bits needed to represent VALUE in a
2315 signed or unsigned type, UNSIGNEDP says which. */
2318 min_precision (value, unsignedp)
2324 /* If the value is negative, compute its negative minus 1. The latter
2325 adjustment is because the absolute value of the largest negative value
2326 is one larger than the largest positive value. This is equivalent to
2327 a bit-wise negation, so use that operation instead. */
2329 if (tree_int_cst_sgn (value) < 0)
2330 value = fold (build1 (BIT_NOT_EXPR, TREE_TYPE (value), value));
2332 /* Return the number of bits needed, taking into account the fact
2333 that we need one more bit for a signed than unsigned type. */
2335 if (integer_zerop (value))
2337 else if (TREE_INT_CST_HIGH (value) != 0)
2338 log = HOST_BITS_PER_WIDE_INT + floor_log2 (TREE_INT_CST_HIGH (value));
2340 log = floor_log2 (TREE_INT_CST_LOW (value));
2342 return log + 1 + ! unsignedp;
2345 /* Print an error message for invalid operands to arith operation CODE.
2346 NOP_EXPR is used as a special case (see truthvalue_conversion). */
2349 binary_op_error (code)
2350 enum tree_code code;
2352 register const char *opname;
2357 error ("invalid truth-value expression");
2361 opname = "+"; break;
2363 opname = "-"; break;
2365 opname = "*"; break;
2367 opname = "max"; break;
2369 opname = "min"; break;
2371 opname = "=="; break;
2373 opname = "!="; break;
2375 opname = "<="; break;
2377 opname = ">="; break;
2379 opname = "<"; break;
2381 opname = ">"; break;
2383 opname = "<<"; break;
2385 opname = ">>"; break;
2386 case TRUNC_MOD_EXPR:
2387 case FLOOR_MOD_EXPR:
2388 opname = "%"; break;
2389 case TRUNC_DIV_EXPR:
2390 case FLOOR_DIV_EXPR:
2391 opname = "/"; break;
2393 opname = "&"; break;
2395 opname = "|"; break;
2396 case TRUTH_ANDIF_EXPR:
2397 opname = "&&"; break;
2398 case TRUTH_ORIF_EXPR:
2399 opname = "||"; break;
2401 opname = "^"; break;
2404 opname = "rotate"; break;
2406 opname = "unknown"; break;
2408 error ("invalid operands to binary %s", opname);
2411 /* Subroutine of build_binary_op, used for comparison operations.
2412 See if the operands have both been converted from subword integer types
2413 and, if so, perhaps change them both back to their original type.
2414 This function is also responsible for converting the two operands
2415 to the proper common type for comparison.
2417 The arguments of this function are all pointers to local variables
2418 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
2419 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
2421 If this function returns nonzero, it means that the comparison has
2422 a constant value. What this function returns is an expression for
2426 shorten_compare (op0_ptr, op1_ptr, restype_ptr, rescode_ptr)
2427 tree *op0_ptr, *op1_ptr;
2429 enum tree_code *rescode_ptr;
2432 tree op0 = *op0_ptr;
2433 tree op1 = *op1_ptr;
2434 int unsignedp0, unsignedp1;
2436 tree primop0, primop1;
2437 enum tree_code code = *rescode_ptr;
2439 /* Throw away any conversions to wider types
2440 already present in the operands. */
2442 primop0 = get_narrower (op0, &unsignedp0);
2443 primop1 = get_narrower (op1, &unsignedp1);
2445 /* Handle the case that OP0 does not *contain* a conversion
2446 but it *requires* conversion to FINAL_TYPE. */
2448 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
2449 unsignedp0 = TREE_UNSIGNED (TREE_TYPE (op0));
2450 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
2451 unsignedp1 = TREE_UNSIGNED (TREE_TYPE (op1));
2453 /* If one of the operands must be floated, we cannot optimize. */
2454 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
2455 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
2457 /* If first arg is constant, swap the args (changing operation
2458 so value is preserved), for canonicalization. Don't do this if
2459 the second arg is 0. */
2461 if (TREE_CONSTANT (primop0)
2462 && ! integer_zerop (primop1) && ! real_zerop (primop1))
2464 register tree tem = primop0;
2465 register int temi = unsignedp0;
2473 unsignedp0 = unsignedp1;
2496 *rescode_ptr = code;
2499 /* If comparing an integer against a constant more bits wide,
2500 maybe we can deduce a value of 1 or 0 independent of the data.
2501 Or else truncate the constant now
2502 rather than extend the variable at run time.
2504 This is only interesting if the constant is the wider arg.
2505 Also, it is not safe if the constant is unsigned and the
2506 variable arg is signed, since in this case the variable
2507 would be sign-extended and then regarded as unsigned.
2508 Our technique fails in this case because the lowest/highest
2509 possible unsigned results don't follow naturally from the
2510 lowest/highest possible values of the variable operand.
2511 For just EQ_EXPR and NE_EXPR there is another technique that
2512 could be used: see if the constant can be faithfully represented
2513 in the other operand's type, by truncating it and reextending it
2514 and see if that preserves the constant's value. */
2516 if (!real1 && !real2
2517 && TREE_CODE (primop1) == INTEGER_CST
2518 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
2520 int min_gt, max_gt, min_lt, max_lt;
2521 tree maxval, minval;
2522 /* 1 if comparison is nominally unsigned. */
2523 int unsignedp = TREE_UNSIGNED (*restype_ptr);
2526 type = signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0));
2528 /* If TYPE is an enumeration, then we need to get its min/max
2529 values from it's underlying integral type, not the enumerated
2531 if (TREE_CODE (type) == ENUMERAL_TYPE)
2532 type = type_for_size (TYPE_PRECISION (type), unsignedp0);
2534 maxval = TYPE_MAX_VALUE (type);
2535 minval = TYPE_MIN_VALUE (type);
2537 if (unsignedp && !unsignedp0)
2538 *restype_ptr = signed_type (*restype_ptr);
2540 if (TREE_TYPE (primop1) != *restype_ptr)
2541 primop1 = convert (*restype_ptr, primop1);
2542 if (type != *restype_ptr)
2544 minval = convert (*restype_ptr, minval);
2545 maxval = convert (*restype_ptr, maxval);
2548 if (unsignedp && unsignedp0)
2550 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
2551 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
2552 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
2553 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
2557 min_gt = INT_CST_LT (primop1, minval);
2558 max_gt = INT_CST_LT (primop1, maxval);
2559 min_lt = INT_CST_LT (minval, primop1);
2560 max_lt = INT_CST_LT (maxval, primop1);
2564 /* This used to be a switch, but Genix compiler can't handle that. */
2565 if (code == NE_EXPR)
2567 if (max_lt || min_gt)
2568 val = boolean_true_node;
2570 else if (code == EQ_EXPR)
2572 if (max_lt || min_gt)
2573 val = boolean_false_node;
2575 else if (code == LT_EXPR)
2578 val = boolean_true_node;
2580 val = boolean_false_node;
2582 else if (code == GT_EXPR)
2585 val = boolean_true_node;
2587 val = boolean_false_node;
2589 else if (code == LE_EXPR)
2592 val = boolean_true_node;
2594 val = boolean_false_node;
2596 else if (code == GE_EXPR)
2599 val = boolean_true_node;
2601 val = boolean_false_node;
2604 /* If primop0 was sign-extended and unsigned comparison specd,
2605 we did a signed comparison above using the signed type bounds.
2606 But the comparison we output must be unsigned.
2608 Also, for inequalities, VAL is no good; but if the signed
2609 comparison had *any* fixed result, it follows that the
2610 unsigned comparison just tests the sign in reverse
2611 (positive values are LE, negative ones GE).
2612 So we can generate an unsigned comparison
2613 against an extreme value of the signed type. */
2615 if (unsignedp && !unsignedp0)
2622 primop1 = TYPE_MIN_VALUE (type);
2628 primop1 = TYPE_MAX_VALUE (type);
2635 type = unsigned_type (type);
2638 if (!max_gt && !unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
2640 /* This is the case of (char)x >?< 0x80, which people used to use
2641 expecting old C compilers to change the 0x80 into -0x80. */
2642 if (val == boolean_false_node)
2643 warning ("comparison is always false due to limited range of data type");
2644 if (val == boolean_true_node)
2645 warning ("comparison is always true due to limited range of data type");
2648 if (!min_lt && unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
2650 /* This is the case of (unsigned char)x >?< -1 or < 0. */
2651 if (val == boolean_false_node)
2652 warning ("comparison is always false due to limited range of data type");
2653 if (val == boolean_true_node)
2654 warning ("comparison is always true due to limited range of data type");
2659 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2660 if (TREE_SIDE_EFFECTS (primop0))
2661 return build (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
2665 /* Value is not predetermined, but do the comparison
2666 in the type of the operand that is not constant.
2667 TYPE is already properly set. */
2669 else if (real1 && real2
2670 && (TYPE_PRECISION (TREE_TYPE (primop0))
2671 == TYPE_PRECISION (TREE_TYPE (primop1))))
2672 type = TREE_TYPE (primop0);
2674 /* If args' natural types are both narrower than nominal type
2675 and both extend in the same manner, compare them
2676 in the type of the wider arg.
2677 Otherwise must actually extend both to the nominal
2678 common type lest different ways of extending
2680 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
2682 else if (unsignedp0 == unsignedp1 && real1 == real2
2683 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
2684 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
2686 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
2687 type = signed_or_unsigned_type (unsignedp0
2688 || TREE_UNSIGNED (*restype_ptr),
2690 /* Make sure shorter operand is extended the right way
2691 to match the longer operand. */
2692 primop0 = convert (signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0)),
2694 primop1 = convert (signed_or_unsigned_type (unsignedp1, TREE_TYPE (primop1)),
2699 /* Here we must do the comparison on the nominal type
2700 using the args exactly as we received them. */
2701 type = *restype_ptr;
2705 if (!real1 && !real2 && integer_zerop (primop1)
2706 && TREE_UNSIGNED (*restype_ptr))
2712 /* All unsigned values are >= 0, so we warn if extra warnings
2713 are requested. However, if OP0 is a constant that is
2714 >= 0, the signedness of the comparison isn't an issue,
2715 so suppress the warning. */
2717 && ! (TREE_CODE (primop0) == INTEGER_CST
2718 && ! TREE_OVERFLOW (convert (signed_type (type),
2720 warning ("comparison of unsigned expression >= 0 is always true");
2721 value = boolean_true_node;
2726 && ! (TREE_CODE (primop0) == INTEGER_CST
2727 && ! TREE_OVERFLOW (convert (signed_type (type),
2729 warning ("comparison of unsigned expression < 0 is always false");
2730 value = boolean_false_node;
2739 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2740 if (TREE_SIDE_EFFECTS (primop0))
2741 return build (COMPOUND_EXPR, TREE_TYPE (value),
2748 *op0_ptr = convert (type, primop0);
2749 *op1_ptr = convert (type, primop1);
2751 *restype_ptr = boolean_type_node;
2756 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
2757 or validate its data type for an `if' or `while' statement or ?..: exp.
2759 This preparation consists of taking the ordinary
2760 representation of an expression expr and producing a valid tree
2761 boolean expression describing whether expr is nonzero. We could
2762 simply always do build_binary_op (NE_EXPR, expr, boolean_false_node, 1),
2763 but we optimize comparisons, &&, ||, and !.
2765 The resulting type should always be `boolean_type_node'. */
2768 truthvalue_conversion (expr)
2771 if (TREE_CODE (expr) == ERROR_MARK)
2774 #if 0 /* This appears to be wrong for C++. */
2775 /* These really should return error_mark_node after 2.4 is stable.
2776 But not all callers handle ERROR_MARK properly. */
2777 switch (TREE_CODE (TREE_TYPE (expr)))
2780 error ("struct type value used where scalar is required");
2781 return boolean_false_node;
2784 error ("union type value used where scalar is required");
2785 return boolean_false_node;
2788 error ("array type value used where scalar is required");
2789 return boolean_false_node;
2796 switch (TREE_CODE (expr))
2798 /* It is simpler and generates better code to have only TRUTH_*_EXPR
2799 or comparison expressions as truth values at this level. */
2802 /* A one-bit unsigned bit-field is already acceptable. */
2803 if (1 == TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (expr, 1)))
2804 && TREE_UNSIGNED (TREE_OPERAND (expr, 1)))
2810 /* It is simpler and generates better code to have only TRUTH_*_EXPR
2811 or comparison expressions as truth values at this level. */
2813 if (integer_zerop (TREE_OPERAND (expr, 1)))
2814 return build_unary_op (TRUTH_NOT_EXPR, TREE_OPERAND (expr, 0), 0);
2816 case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
2817 case TRUTH_ANDIF_EXPR:
2818 case TRUTH_ORIF_EXPR:
2819 case TRUTH_AND_EXPR:
2821 case TRUTH_XOR_EXPR:
2822 case TRUTH_NOT_EXPR:
2823 TREE_TYPE (expr) = boolean_type_node;
2830 return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
2833 return real_zerop (expr) ? boolean_false_node : boolean_true_node;
2836 /* If we are taking the address of a external decl, it might be zero
2837 if it is weak, so we cannot optimize. */
2838 if (TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (expr, 0))) == 'd'
2839 && DECL_EXTERNAL (TREE_OPERAND (expr, 0)))
2842 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0)))
2843 return build (COMPOUND_EXPR, boolean_type_node,
2844 TREE_OPERAND (expr, 0), boolean_true_node);
2846 return boolean_true_node;
2849 return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
2850 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
2851 truthvalue_conversion (TREE_OPERAND (expr, 0)),
2852 truthvalue_conversion (TREE_OPERAND (expr, 1)),
2859 /* These don't change whether an object is non-zero or zero. */
2860 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2864 /* These don't change whether an object is zero or non-zero, but
2865 we can't ignore them if their second arg has side-effects. */
2866 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
2867 return build (COMPOUND_EXPR, boolean_type_node, TREE_OPERAND (expr, 1),
2868 truthvalue_conversion (TREE_OPERAND (expr, 0)));
2870 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2873 /* Distribute the conversion into the arms of a COND_EXPR. */
2874 return fold (build (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
2875 truthvalue_conversion (TREE_OPERAND (expr, 1)),
2876 truthvalue_conversion (TREE_OPERAND (expr, 2))));
2879 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
2880 since that affects how `default_conversion' will behave. */
2881 if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE
2882 || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
2884 /* fall through... */
2886 /* If this is widening the argument, we can ignore it. */
2887 if (TYPE_PRECISION (TREE_TYPE (expr))
2888 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
2889 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2893 /* With IEEE arithmetic, x - x may not equal 0, so we can't optimize
2895 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
2896 && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE)
2898 /* fall through... */
2900 /* This and MINUS_EXPR can be changed into a comparison of the
2902 if (TREE_TYPE (TREE_OPERAND (expr, 0))
2903 == TREE_TYPE (TREE_OPERAND (expr, 1)))
2904 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2905 TREE_OPERAND (expr, 1), 1);
2906 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2907 fold (build1 (NOP_EXPR,
2908 TREE_TYPE (TREE_OPERAND (expr, 0)),
2909 TREE_OPERAND (expr, 1))), 1);
2912 if (integer_onep (TREE_OPERAND (expr, 1))
2913 && TREE_TYPE (expr) != boolean_type_node)
2914 /* Using convert here would cause infinite recursion. */
2915 return build1 (NOP_EXPR, boolean_type_node, expr);
2919 if (warn_parentheses && C_EXP_ORIGINAL_CODE (expr) == MODIFY_EXPR)
2920 warning ("suggest parentheses around assignment used as truth value");
2927 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
2929 tree tem = save_expr (expr);
2930 return (build_binary_op
2931 ((TREE_SIDE_EFFECTS (expr)
2932 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
2933 truthvalue_conversion (build_unary_op (REALPART_EXPR, tem, 0)),
2934 truthvalue_conversion (build_unary_op (IMAGPART_EXPR, tem, 0)),
2938 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
2942 /* Read the rest of a #-directive from input stream FINPUT.
2943 In normal use, the directive name and the white space after it
2944 have already been read, so they won't be included in the result.
2945 We allow for the fact that the directive line may contain
2946 a newline embedded within a character or string literal which forms
2947 a part of the directive.
2949 The value is a string in a reusable buffer. It remains valid
2950 only until the next time this function is called. */
2951 unsigned char *yy_cur, *yy_lim;
2953 #define GETC() (yy_cur < yy_lim ? *yy_cur++ : yy_get_token ())
2954 #define UNGETC(c) ((c) == EOF ? 0 : yy_cur--)
2961 parse_in.limit = parse_in.token_buffer;
2962 cpp_token = cpp_get_token (&parse_in);
2963 if (cpp_token == CPP_EOF)
2965 yy_lim = CPP_PWRITTEN (&parse_in);
2966 yy_cur = parse_in.token_buffer;
2967 if (yy_cur < yy_lim)
2973 get_directive_line ()
2975 static char *directive_buffer = NULL;
2976 static unsigned buffer_length = 0;
2978 register char *buffer_limit;
2979 register int looking_for = 0;
2980 register int char_escaped = 0;
2982 if (buffer_length == 0)
2984 directive_buffer = (char *)xmalloc (128);
2985 buffer_length = 128;
2988 buffer_limit = &directive_buffer[buffer_length];
2990 for (p = directive_buffer; ; )
2994 /* Make buffer bigger if it is full. */
2995 if (p >= buffer_limit)
2997 register unsigned bytes_used = (p - directive_buffer);
3001 = (char *)xrealloc (directive_buffer, buffer_length);
3002 p = &directive_buffer[bytes_used];
3003 buffer_limit = &directive_buffer[buffer_length];
3008 /* Discard initial whitespace. */
3009 if ((c == ' ' || c == '\t') && p == directive_buffer)
3012 /* Detect the end of the directive. */
3013 if (c == '\n' && looking_for == 0)
3022 return directive_buffer;
3024 /* Handle string and character constant syntax. */
3027 if (looking_for == c && !char_escaped)
3028 looking_for = 0; /* Found terminator... stop looking. */
3031 if (c == '\'' || c == '"')
3032 looking_for = c; /* Don't stop buffering until we see another
3033 another one of these (or an EOF). */
3035 /* Handle backslash. */
3036 char_escaped = (c == '\\' && ! char_escaped);
3040 /* Read the rest of a #-directive from input stream FINPUT.
3041 In normal use, the directive name and the white space after it
3042 have already been read, so they won't be included in the result.
3043 We allow for the fact that the directive line may contain
3044 a newline embedded within a character or string literal which forms
3045 a part of the directive.
3047 The value is a string in a reusable buffer. It remains valid
3048 only until the next time this function is called.
3050 The terminating character ('\n' or EOF) is left in FINPUT for the
3051 caller to re-read. */
3054 get_directive_line (finput)
3055 register FILE *finput;
3057 static char *directive_buffer = NULL;
3058 static unsigned buffer_length = 0;
3060 register char *buffer_limit;
3061 register int looking_for = 0;
3062 register int char_escaped = 0;
3064 if (buffer_length == 0)
3066 directive_buffer = (char *)xmalloc (128);
3067 buffer_length = 128;
3070 buffer_limit = &directive_buffer[buffer_length];
3072 for (p = directive_buffer; ; )
3076 /* Make buffer bigger if it is full. */
3077 if (p >= buffer_limit)
3079 register unsigned bytes_used = (p - directive_buffer);
3083 = (char *)xrealloc (directive_buffer, buffer_length);
3084 p = &directive_buffer[bytes_used];
3085 buffer_limit = &directive_buffer[buffer_length];
3090 /* Discard initial whitespace. */
3091 if ((c == ' ' || c == '\t') && p == directive_buffer)
3094 /* Detect the end of the directive. */
3095 if (looking_for == 0
3096 && (c == '\n' || c == EOF))
3105 return directive_buffer;
3107 /* Handle string and character constant syntax. */
3110 if (looking_for == c && !char_escaped)
3111 looking_for = 0; /* Found terminator... stop looking. */
3114 if (c == '\'' || c == '"')
3115 looking_for = c; /* Don't stop buffering until we see another
3116 one of these (or an EOF). */
3118 /* Handle backslash. */
3119 char_escaped = (c == '\\' && ! char_escaped);
3122 #endif /* !USE_CPPLIB */
3124 /* Make a variant type in the proper way for C/C++, propagating qualifiers
3125 down to the element type of an array. */
3128 c_build_qualified_type (type, type_quals)
3132 /* A restrict-qualified pointer type must be a pointer to object or
3133 incomplete type. Note that the use of POINTER_TYPE_P also allows
3134 REFERENCE_TYPEs, which is appropriate for C++. Unfortunately,
3135 the C++ front-end also use POINTER_TYPE for pointer-to-member
3136 values, so even though it should be illegal to use `restrict'
3137 with such an entity we don't flag that here. Thus, special case
3138 code for that case is required in the C++ front-end. */
3139 if ((type_quals & TYPE_QUAL_RESTRICT)
3140 && (!POINTER_TYPE_P (type)
3141 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
3143 error ("invalid use of `restrict'");
3144 type_quals &= ~TYPE_QUAL_RESTRICT;
3147 if (TREE_CODE (type) == ARRAY_TYPE)
3148 return build_array_type (c_build_qualified_type (TREE_TYPE (type),
3150 TYPE_DOMAIN (type));
3151 return build_qualified_type (type, type_quals);
3154 /* Apply the TYPE_QUALS to the new DECL. */
3157 c_apply_type_quals_to_decl (type_quals, decl)
3161 if (type_quals & TYPE_QUAL_CONST)
3162 TREE_READONLY (decl) = 1;
3163 if (type_quals & TYPE_QUAL_VOLATILE)
3165 TREE_SIDE_EFFECTS (decl) = 1;
3166 TREE_THIS_VOLATILE (decl) = 1;
3168 if (type_quals & TYPE_QUAL_RESTRICT)
3170 if (!TREE_TYPE (decl)
3171 || !POINTER_TYPE_P (TREE_TYPE (decl))
3172 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (TREE_TYPE (decl))))
3173 error ("invalid use of `restrict'");
3174 else if (flag_strict_aliasing)
3176 /* No two restricted pointers can point at the same thing.
3177 However, a restricted pointer can point at the same thing
3178 as an unrestricted pointer, if that unrestricted pointer
3179 is based on the restricted pointer. So, we make the
3180 alias set for the restricted pointer a subset of the
3181 alias set for the type pointed to by the type of the
3184 int pointed_to_alias_set
3185 = get_alias_set (TREE_TYPE (TREE_TYPE (decl)));
3187 if (!pointed_to_alias_set)
3188 /* It's not legal to make a subset of alias set zero. */
3192 DECL_POINTER_ALIAS_SET (decl) = new_alias_set ();
3193 record_alias_subset (pointed_to_alias_set,
3194 DECL_POINTER_ALIAS_SET (decl));
3200 /* T is an expression with pointer type. Find the DECL on which this
3201 expression is based. (For example, in `a[i]' this would be `a'.)
3202 If there is no such DECL, or a unique decl cannot be determined,
3203 NULL_TREE is retured. */
3206 c_find_base_decl (t)
3212 if (t == NULL_TREE || t == error_mark_node)
3215 if (!POINTER_TYPE_P (TREE_TYPE (t)))
3220 if (TREE_CODE (t) == FIELD_DECL
3221 || TREE_CODE (t) == PARM_DECL
3222 || TREE_CODE (t) == VAR_DECL)
3223 /* Aha, we found a pointer-typed declaration. */
3226 /* It would be nice to deal with COMPONENT_REFs here. If we could
3227 tell that `a' and `b' were the same, then `a->f' and `b->f' are
3230 /* Handle general expressions. */
3231 switch (TREE_CODE_CLASS (TREE_CODE (t)))
3236 for (i = tree_code_length [(int) TREE_CODE (t)]; --i >= 0;)
3238 tree d = c_find_base_decl (TREE_OPERAND (t, i));
3243 else if (d && d != decl)
3244 /* Two different declarations. That's confusing; let's
3245 just assume we don't know what's going on. */
3258 /* Return the typed-based alias set for T, which may be an expression
3268 if (t == error_mark_node)
3271 type = (TREE_CODE_CLASS (TREE_CODE (t)) == 't')
3272 ? t : TREE_TYPE (t);
3274 if (type == error_mark_node)
3277 /* Deal with special cases first; for certain kinds of references
3278 we're interested in more than just the type. */
3280 if (TREE_CODE (t) == BIT_FIELD_REF)
3281 /* Perhaps reads and writes to this piece of data alias fields
3282 neighboring the bitfield. Perhaps that's impossible. For now,
3283 let's just assume that bitfields can alias everything, which is
3284 the conservative assumption. */
3287 /* Permit type-punning when accessing a union, provided the access
3288 is directly through the union. For example, this code does not
3289 permit taking the address of a union member and then storing
3290 through it. Even the type-punning allowed here is a GCC
3291 extension, albeit a common and useful one; the C standard says
3292 that such accesses have implementation-defined behavior. */
3294 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
3295 u = TREE_OPERAND (u, 0))
3296 if (TREE_CODE (u) == COMPONENT_REF
3297 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
3300 if (TREE_CODE (t) == INDIRECT_REF)
3302 /* Check for accesses through restrict-qualified pointers. */
3303 tree decl = c_find_base_decl (TREE_OPERAND (t, 0));
3305 if (decl && DECL_POINTER_ALIAS_SET_KNOWN_P (decl))
3306 /* We use the alias set indicated in the declaration. */
3307 return DECL_POINTER_ALIAS_SET (decl);
3310 /* From here on, only the type matters. */
3312 if (TREE_CODE (t) == COMPONENT_REF
3313 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (t, 1)))
3314 /* Since build_modify_expr calls get_unwidened for stores to
3315 component references, the type of a bit field can be changed
3316 from (say) `unsigned int : 16' to `unsigned short' or from
3317 `enum E : 16' to `short'. We want the real type of the
3318 bit-field in this case, not some the integral equivalent. */
3319 type = DECL_BIT_FIELD_TYPE (TREE_OPERAND (t, 1));
3321 if (TYPE_ALIAS_SET_KNOWN_P (type))
3322 /* If we've already calculated the value, just return it. */
3323 return TYPE_ALIAS_SET (type);
3324 else if (TYPE_MAIN_VARIANT (type) != type)
3325 /* The C standard specifically allows aliasing between
3326 cv-qualified variants of types. */
3327 TYPE_ALIAS_SET (type) = c_get_alias_set (TYPE_MAIN_VARIANT (type));
3328 else if (TREE_CODE (type) == INTEGER_TYPE)
3330 tree signed_variant;
3332 /* The C standard specifically allows aliasing between signed and
3333 unsigned variants of the same type. We treat the signed
3334 variant as canonical. */
3335 signed_variant = signed_type (type);
3337 if (signed_variant != type)
3338 TYPE_ALIAS_SET (type) = c_get_alias_set (signed_variant);
3339 else if (signed_variant == signed_char_type_node)
3340 /* The C standard guarantess that any object may be accessed
3341 via an lvalue that has character type. We don't have to
3342 check for unsigned_char_type_node or char_type_node because
3343 we are specifically looking at the signed variant. */
3344 TYPE_ALIAS_SET (type) = 0;
3346 else if (TREE_CODE (type) == ARRAY_TYPE)
3347 /* Anything that can alias one of the array elements can alias
3348 the entire array as well. */
3349 TYPE_ALIAS_SET (type) = c_get_alias_set (TREE_TYPE (type));
3350 else if (TREE_CODE (type) == FUNCTION_TYPE)
3351 /* There are no objects of FUNCTION_TYPE, so there's no point in
3352 using up an alias set for them. (There are, of course,
3353 pointers and references to functions, but that's
3355 TYPE_ALIAS_SET (type) = 0;
3356 else if (TREE_CODE (type) == RECORD_TYPE
3357 || TREE_CODE (type) == UNION_TYPE)
3358 /* If TYPE is a struct or union type then we're reading or
3359 writing an entire struct. Thus, we don't know anything about
3360 aliasing. (In theory, such an access can only alias objects
3361 whose type is the same as one of the fields, recursively, but
3362 we don't yet make any use of that information.) */
3363 TYPE_ALIAS_SET (type) = 0;
3364 else if (TREE_CODE (type) == POINTER_TYPE
3365 || TREE_CODE (type) == REFERENCE_TYPE)
3369 /* Unfortunately, there is no canonical form of a pointer type.
3370 In particular, if we have `typedef int I', then `int *', and
3371 `I *' are different types. So, we have to pick a canonical
3372 representative. We do this below.
3374 Technically, this approach is actually more conservative that
3375 it needs to be. In particular, `const int *' and `int *'
3376 chould be in different alias sets, according to the C and C++
3377 standard, since their types are not the same, and so,
3378 technically, an `int **' and `const int **' cannot point at
3381 But, the standard is wrong. In particular, this code is
3386 const int* const* cipp = &ip;
3388 And, it doesn't make sense for that to be legal unless you
3389 can dereference IPP and CIPP. So, we ignore cv-qualifiers on
3390 the pointed-to types. This issue has been reported to the
3392 t = TYPE_MAIN_VARIANT (TREE_TYPE (type));
3393 t = ((TREE_CODE (type) == POINTER_TYPE)
3394 ? build_pointer_type (t) : build_reference_type (t));
3396 TYPE_ALIAS_SET (type) = c_get_alias_set (t);
3399 if (!TYPE_ALIAS_SET_KNOWN_P (type))
3401 /* Types that are not allocated on the permanent obstack are not
3402 placed in the type hash table. Thus, there can be multiple
3403 copies of identical types in local scopes. In the long run,
3404 all types should be permanent. */
3405 if (! TREE_PERMANENT (type))
3406 TYPE_ALIAS_SET (type) = 0;
3408 /* TYPE is something we haven't seen before. Put it in a new
3410 TYPE_ALIAS_SET (type) = new_alias_set ();
3413 return TYPE_ALIAS_SET (type);