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 $ */
38 cpp_options parse_options;
39 static enum cpp_token cpp_token;
42 #ifndef WCHAR_TYPE_SIZE
44 #define WCHAR_TYPE_SIZE INT_TYPE_SIZE
46 #define WCHAR_TYPE_SIZE BITS_PER_WORD
50 extern struct obstack permanent_obstack;
52 /* Nonzero means the expression being parsed will never be evaluated.
53 This is a count, since unevaluated expressions can nest. */
56 enum attrs {A_PACKED, A_NOCOMMON, A_COMMON, A_NORETURN, A_CONST, A_T_UNION,
57 A_NO_CHECK_MEMORY_USAGE, A_NO_INSTRUMENT_FUNCTION,
58 A_CONSTRUCTOR, A_DESTRUCTOR, A_MODE, A_SECTION, A_ALIGNED,
59 A_UNUSED, A_FORMAT, A_FORMAT_ARG, A_WEAK, A_ALIAS};
61 enum format_type { printf_format_type, scanf_format_type,
62 strftime_format_type };
64 static void declare_hidden_char_array PROTO((const char *, const char *));
65 static void add_attribute PROTO((enum attrs, const char *,
67 static void init_attributes PROTO((void));
68 static void record_function_format PROTO((tree, tree, enum format_type,
70 static void record_international_format PROTO((tree, tree, int));
71 static tree c_find_base_decl PROTO((tree));
72 static int default_valid_lang_attribute PROTO ((tree, tree, tree, tree));
74 /* Keep a stack of if statements. We record the number of compound
75 statements seen up to the if keyword, as well as the line number
76 and file of the if. If a potentially ambiguous else is seen, that
77 fact is recorded; the warning is issued when we can be sure that
78 the enclosing if statement does not have an else branch. */
86 static void tfaff PROTO((void));
88 static if_elt *if_stack;
90 /* Amount of space in the if statement stack. */
91 static int if_stack_space = 0;
94 static int if_stack_pointer = 0;
96 /* Generate RTL for the start of an if-then, and record the start of it
97 for ambiguous else detection. */
100 c_expand_start_cond (cond, exitflag, compstmt_count)
105 /* Make sure there is enough space on the stack. */
106 if (if_stack_space == 0)
109 if_stack = (if_elt *)xmalloc (10 * sizeof (if_elt));
111 else if (if_stack_space == if_stack_pointer)
113 if_stack_space += 10;
114 if_stack = (if_elt *)xrealloc (if_stack, if_stack_space * sizeof (if_elt));
117 /* Record this if statement. */
118 if_stack[if_stack_pointer].compstmt_count = compstmt_count;
119 if_stack[if_stack_pointer].file = input_filename;
120 if_stack[if_stack_pointer].line = lineno;
121 if_stack[if_stack_pointer].needs_warning = 0;
124 expand_start_cond (cond, exitflag);
127 /* Generate RTL for the end of an if-then. Optionally warn if a nested
128 if statement had an ambiguous else clause. */
134 if (if_stack[if_stack_pointer].needs_warning)
135 warning_with_file_and_line (if_stack[if_stack_pointer].file,
136 if_stack[if_stack_pointer].line,
137 "suggest explicit braces to avoid ambiguous `else'");
141 /* Generate RTL between the then-clause and the else-clause
142 of an if-then-else. */
145 c_expand_start_else ()
147 /* An ambiguous else warning must be generated for the enclosing if
148 statement, unless we see an else branch for that one, too. */
150 && if_stack_pointer > 1
151 && (if_stack[if_stack_pointer - 1].compstmt_count
152 == if_stack[if_stack_pointer - 2].compstmt_count))
153 if_stack[if_stack_pointer - 2].needs_warning = 1;
155 /* Even if a nested if statement had an else branch, it can't be
156 ambiguous if this one also has an else. So don't warn in that
157 case. Also don't warn for any if statements nested in this else. */
158 if_stack[if_stack_pointer - 1].needs_warning = 0;
159 if_stack[if_stack_pointer - 1].compstmt_count--;
161 expand_start_else ();
164 /* Make bindings for __FUNCTION__, __PRETTY_FUNCTION__, and __func__. */
167 declare_function_name ()
169 const char *name, *printable_name;
171 if (current_function_decl == NULL)
174 printable_name = "top level";
178 /* Allow functions to be nameless (such as artificial ones). */
179 if (DECL_NAME (current_function_decl))
180 name = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
183 printable_name = (*decl_printable_name) (current_function_decl, 2);
186 declare_hidden_char_array ("__FUNCTION__", name);
187 declare_hidden_char_array ("__PRETTY_FUNCTION__", printable_name);
188 /* The ISO C people "of course" couldn't use __FUNCTION__ in the
189 ISO C 9x standard; instead a new variable is invented. */
190 declare_hidden_char_array ("__func__", name);
194 declare_hidden_char_array (name, value)
195 const char *name, *value;
197 tree decl, type, init;
200 /* If the default size of char arrays isn't big enough for the name,
201 or if we want to give warnings for large objects, make a bigger one. */
202 vlen = strlen (value) + 1;
203 type = char_array_type_node;
204 if (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < vlen
206 type = build_array_type (char_type_node,
207 build_index_type (build_int_2 (vlen, 0)));
208 push_obstacks_nochange ();
209 decl = build_decl (VAR_DECL, get_identifier (name), type);
210 TREE_STATIC (decl) = 1;
211 TREE_READONLY (decl) = 1;
212 TREE_ASM_WRITTEN (decl) = 1;
213 DECL_SOURCE_LINE (decl) = 0;
214 DECL_ARTIFICIAL (decl) = 1;
215 DECL_IN_SYSTEM_HEADER (decl) = 1;
216 DECL_IGNORED_P (decl) = 1;
217 init = build_string (vlen, value);
218 TREE_TYPE (init) = type;
219 DECL_INITIAL (decl) = init;
220 finish_decl (pushdecl (decl), init, NULL_TREE);
223 /* Given a chain of STRING_CST nodes,
224 concatenate them into one STRING_CST
225 and give it a suitable array-of-chars data type. */
228 combine_strings (strings)
231 register tree value, t;
232 register int length = 1;
235 int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
238 if (TREE_CHAIN (strings))
240 /* More than one in the chain, so concatenate. */
241 register char *p, *q;
243 /* Don't include the \0 at the end of each substring,
244 except for the last one.
245 Count wide strings and ordinary strings separately. */
246 for (t = strings; t; t = TREE_CHAIN (t))
248 if (TREE_TYPE (t) == wchar_array_type_node)
250 wide_length += (TREE_STRING_LENGTH (t) - wchar_bytes);
254 length += (TREE_STRING_LENGTH (t) - 1);
257 /* If anything is wide, the non-wides will be converted,
258 which makes them take more space. */
260 length = length * wchar_bytes + wide_length;
262 p = savealloc (length);
264 /* Copy the individual strings into the new combined string.
265 If the combined string is wide, convert the chars to ints
266 for any individual strings that are not wide. */
269 for (t = strings; t; t = TREE_CHAIN (t))
271 int len = (TREE_STRING_LENGTH (t)
272 - ((TREE_TYPE (t) == wchar_array_type_node)
274 if ((TREE_TYPE (t) == wchar_array_type_node) == wide_flag)
276 memcpy (q, TREE_STRING_POINTER (t), len);
282 for (i = 0; i < len; i++)
284 if (WCHAR_TYPE_SIZE == HOST_BITS_PER_SHORT)
285 ((short *) q)[i] = TREE_STRING_POINTER (t)[i];
287 ((int *) q)[i] = TREE_STRING_POINTER (t)[i];
289 q += len * wchar_bytes;
295 for (i = 0; i < wchar_bytes; i++)
301 value = make_node (STRING_CST);
302 TREE_STRING_POINTER (value) = p;
303 TREE_STRING_LENGTH (value) = length;
308 length = TREE_STRING_LENGTH (value);
309 if (TREE_TYPE (value) == wchar_array_type_node)
313 /* Compute the number of elements, for the array type. */
314 nchars = wide_flag ? length / wchar_bytes : length;
316 /* Create the array type for the string constant.
317 -Wwrite-strings says make the string constant an array of const char
318 so that copying it to a non-const pointer will get a warning.
319 For C++, this is the standard behavior. */
320 if (flag_const_strings
321 && (! flag_traditional && ! flag_writable_strings))
324 = build_type_variant (wide_flag ? wchar_type_node : char_type_node,
327 = build_array_type (elements,
328 build_index_type (build_int_2 (nchars - 1, 0)));
332 = build_array_type (wide_flag ? wchar_type_node : char_type_node,
333 build_index_type (build_int_2 (nchars - 1, 0)));
335 TREE_CONSTANT (value) = 1;
336 TREE_READONLY (value) = ! flag_writable_strings;
337 TREE_STATIC (value) = 1;
341 /* To speed up processing of attributes, we maintain an array of
342 IDENTIFIER_NODES and the corresponding attribute types. */
344 /* Array to hold attribute information. */
346 static struct {enum attrs id; tree name; int min, max, decl_req;} attrtab[50];
348 static int attrtab_idx = 0;
350 /* Add an entry to the attribute table above. */
353 add_attribute (id, string, min_len, max_len, decl_req)
356 int min_len, max_len;
361 attrtab[attrtab_idx].id = id;
362 attrtab[attrtab_idx].name = get_identifier (string);
363 attrtab[attrtab_idx].min = min_len;
364 attrtab[attrtab_idx].max = max_len;
365 attrtab[attrtab_idx++].decl_req = decl_req;
367 sprintf (buf, "__%s__", string);
369 attrtab[attrtab_idx].id = id;
370 attrtab[attrtab_idx].name = get_identifier (buf);
371 attrtab[attrtab_idx].min = min_len;
372 attrtab[attrtab_idx].max = max_len;
373 attrtab[attrtab_idx++].decl_req = decl_req;
376 /* Initialize attribute table. */
381 add_attribute (A_PACKED, "packed", 0, 0, 0);
382 add_attribute (A_NOCOMMON, "nocommon", 0, 0, 1);
383 add_attribute (A_COMMON, "common", 0, 0, 1);
384 add_attribute (A_NORETURN, "noreturn", 0, 0, 1);
385 add_attribute (A_NORETURN, "volatile", 0, 0, 1);
386 add_attribute (A_UNUSED, "unused", 0, 0, 0);
387 add_attribute (A_CONST, "const", 0, 0, 1);
388 add_attribute (A_T_UNION, "transparent_union", 0, 0, 0);
389 add_attribute (A_CONSTRUCTOR, "constructor", 0, 0, 1);
390 add_attribute (A_DESTRUCTOR, "destructor", 0, 0, 1);
391 add_attribute (A_MODE, "mode", 1, 1, 1);
392 add_attribute (A_SECTION, "section", 1, 1, 1);
393 add_attribute (A_ALIGNED, "aligned", 0, 1, 0);
394 add_attribute (A_FORMAT, "format", 3, 3, 1);
395 add_attribute (A_FORMAT_ARG, "format_arg", 1, 1, 1);
396 add_attribute (A_WEAK, "weak", 0, 0, 1);
397 add_attribute (A_ALIAS, "alias", 1, 1, 1);
398 add_attribute (A_NO_INSTRUMENT_FUNCTION, "no_instrument_function", 0, 0, 1);
399 add_attribute (A_NO_CHECK_MEMORY_USAGE, "no_check_memory_usage", 0, 0, 1);
402 /* Default implementation of valid_lang_attribute, below. By default, there
403 are no language-specific attributes. */
406 default_valid_lang_attribute (attr_name, attr_args, decl, type)
407 tree attr_name ATTRIBUTE_UNUSED;
408 tree attr_args ATTRIBUTE_UNUSED;
409 tree decl ATTRIBUTE_UNUSED;
410 tree type ATTRIBUTE_UNUSED;
415 /* Return a 1 if ATTR_NAME and ATTR_ARGS denote a valid language-specific
416 attribute for either declaration DECL or type TYPE and 0 otherwise. */
418 int (*valid_lang_attribute) PROTO ((tree, tree, tree, tree))
419 = default_valid_lang_attribute;
421 /* Process the attributes listed in ATTRIBUTES and PREFIX_ATTRIBUTES
422 and install them in NODE, which is either a DECL (including a TYPE_DECL)
423 or a TYPE. PREFIX_ATTRIBUTES can appear after the declaration specifiers
424 and declaration modifiers but before the declaration proper. */
427 decl_attributes (node, attributes, prefix_attributes)
428 tree node, attributes, prefix_attributes;
430 tree decl = 0, type = 0;
434 if (attrtab_idx == 0)
437 if (TREE_CODE_CLASS (TREE_CODE (node)) == 'd')
440 type = TREE_TYPE (decl);
441 is_type = TREE_CODE (node) == TYPE_DECL;
443 else if (TREE_CODE_CLASS (TREE_CODE (node)) == 't')
444 type = node, is_type = 1;
446 #ifdef PRAGMA_INSERT_ATTRIBUTES
447 /* If the code in c-pragma.c wants to insert some attributes then
448 allow it to do so. Do this before allowing machine back ends to
449 insert attributes, so that they have the opportunity to override
450 anything done here. */
451 PRAGMA_INSERT_ATTRIBUTES (node, & attributes, & prefix_attributes);
454 #ifdef INSERT_ATTRIBUTES
455 INSERT_ATTRIBUTES (node, & attributes, & prefix_attributes);
458 attributes = chainon (prefix_attributes, attributes);
460 for (a = attributes; a; a = TREE_CHAIN (a))
462 tree name = TREE_PURPOSE (a);
463 tree args = TREE_VALUE (a);
467 for (i = 0; i < attrtab_idx; i++)
468 if (attrtab[i].name == name)
471 if (i == attrtab_idx)
473 if (! valid_machine_attribute (name, args, decl, type)
474 && ! (* valid_lang_attribute) (name, args, decl, type))
475 warning ("`%s' attribute directive ignored",
476 IDENTIFIER_POINTER (name));
478 type = TREE_TYPE (decl);
481 else if (attrtab[i].decl_req && decl == 0)
483 warning ("`%s' attribute does not apply to types",
484 IDENTIFIER_POINTER (name));
487 else if (list_length (args) < attrtab[i].min
488 || list_length (args) > attrtab[i].max)
490 error ("wrong number of arguments specified for `%s' attribute",
491 IDENTIFIER_POINTER (name));
500 TYPE_PACKED (type) = 1;
501 else if (TREE_CODE (decl) == FIELD_DECL)
502 DECL_PACKED (decl) = 1;
503 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
504 used for DECL_REGISTER. It wouldn't mean anything anyway. */
506 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
510 if (TREE_CODE (decl) == VAR_DECL)
511 DECL_COMMON (decl) = 0;
513 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
517 if (TREE_CODE (decl) == VAR_DECL)
518 DECL_COMMON (decl) = 1;
520 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
524 if (TREE_CODE (decl) == FUNCTION_DECL)
525 TREE_THIS_VOLATILE (decl) = 1;
526 else if (TREE_CODE (type) == POINTER_TYPE
527 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
528 TREE_TYPE (decl) = type
530 (build_type_variant (TREE_TYPE (type),
531 TREE_READONLY (TREE_TYPE (type)), 1));
533 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
538 TREE_USED (type) = 1;
539 else if (TREE_CODE (decl) == PARM_DECL
540 || TREE_CODE (decl) == VAR_DECL
541 || TREE_CODE (decl) == FUNCTION_DECL
542 || TREE_CODE (decl) == LABEL_DECL)
543 TREE_USED (decl) = 1;
545 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
549 if (TREE_CODE (decl) == FUNCTION_DECL)
550 TREE_READONLY (decl) = 1;
551 else if (TREE_CODE (type) == POINTER_TYPE
552 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
553 TREE_TYPE (decl) = type
555 (build_type_variant (TREE_TYPE (type), 1,
556 TREE_THIS_VOLATILE (TREE_TYPE (type))));
558 warning ( "`%s' attribute ignored", IDENTIFIER_POINTER (name));
563 && TREE_CODE (type) == UNION_TYPE
565 || (TYPE_FIELDS (type) != 0
566 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))))
567 TYPE_TRANSPARENT_UNION (type) = 1;
568 else if (decl != 0 && TREE_CODE (decl) == PARM_DECL
569 && TREE_CODE (type) == UNION_TYPE
570 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))
571 DECL_TRANSPARENT_UNION (decl) = 1;
573 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
577 if (TREE_CODE (decl) == FUNCTION_DECL
578 && TREE_CODE (type) == FUNCTION_TYPE
579 && decl_function_context (decl) == 0)
581 DECL_STATIC_CONSTRUCTOR (decl) = 1;
582 TREE_USED (decl) = 1;
585 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
589 if (TREE_CODE (decl) == FUNCTION_DECL
590 && TREE_CODE (type) == FUNCTION_TYPE
591 && decl_function_context (decl) == 0)
593 DECL_STATIC_DESTRUCTOR (decl) = 1;
594 TREE_USED (decl) = 1;
597 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
601 if (TREE_CODE (TREE_VALUE (args)) != IDENTIFIER_NODE)
602 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
606 const char *p = IDENTIFIER_POINTER (TREE_VALUE (args));
607 int len = strlen (p);
608 enum machine_mode mode = VOIDmode;
611 if (len > 4 && p[0] == '_' && p[1] == '_'
612 && p[len - 1] == '_' && p[len - 2] == '_')
614 char *newp = (char *) alloca (len - 1);
616 strcpy (newp, &p[2]);
617 newp[len - 4] = '\0';
621 /* Give this decl a type with the specified mode.
622 First check for the special modes. */
623 if (! strcmp (p, "byte"))
625 else if (!strcmp (p, "word"))
627 else if (! strcmp (p, "pointer"))
630 for (j = 0; j < NUM_MACHINE_MODES; j++)
631 if (!strcmp (p, GET_MODE_NAME (j)))
632 mode = (enum machine_mode) j;
634 if (mode == VOIDmode)
635 error ("unknown machine mode `%s'", p);
636 else if (0 == (typefm = type_for_mode (mode,
637 TREE_UNSIGNED (type))))
638 error ("no data type for mode `%s'", p);
641 TREE_TYPE (decl) = type = typefm;
642 DECL_SIZE (decl) = 0;
643 layout_decl (decl, 0);
649 #ifdef ASM_OUTPUT_SECTION_NAME
650 if ((TREE_CODE (decl) == FUNCTION_DECL
651 || TREE_CODE (decl) == VAR_DECL)
652 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
654 if (TREE_CODE (decl) == VAR_DECL
655 && current_function_decl != NULL_TREE
656 && ! TREE_STATIC (decl))
657 error_with_decl (decl,
658 "section attribute cannot be specified for local variables");
659 /* The decl may have already been given a section attribute from
660 a previous declaration. Ensure they match. */
661 else if (DECL_SECTION_NAME (decl) != NULL_TREE
662 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
663 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
664 error_with_decl (node,
665 "section of `%s' conflicts with previous declaration");
667 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
670 error_with_decl (node,
671 "section attribute not allowed for `%s'");
673 error_with_decl (node,
674 "section attributes are not supported for this target");
681 = (args ? TREE_VALUE (args)
682 : size_int (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
685 /* Strip any NOPs of any kind. */
686 while (TREE_CODE (align_expr) == NOP_EXPR
687 || TREE_CODE (align_expr) == CONVERT_EXPR
688 || TREE_CODE (align_expr) == NON_LVALUE_EXPR)
689 align_expr = TREE_OPERAND (align_expr, 0);
691 if (TREE_CODE (align_expr) != INTEGER_CST)
693 error ("requested alignment is not a constant");
697 align = TREE_INT_CST_LOW (align_expr) * BITS_PER_UNIT;
699 if (exact_log2 (align) == -1)
700 error ("requested alignment is not a power of 2");
702 TYPE_ALIGN (type) = align;
703 else if (TREE_CODE (decl) != VAR_DECL
704 && TREE_CODE (decl) != FIELD_DECL)
705 error_with_decl (decl,
706 "alignment may not be specified for `%s'");
708 DECL_ALIGN (decl) = align;
714 tree format_type_id = TREE_VALUE (args);
715 tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
716 tree first_arg_num_expr
717 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
721 enum format_type format_type;
725 if (TREE_CODE (decl) != FUNCTION_DECL)
727 error_with_decl (decl,
728 "argument format specified for non-function `%s'");
732 if (TREE_CODE (format_type_id) != IDENTIFIER_NODE)
734 error_with_decl (decl, "unrecognized format specifier");
739 const char *p = IDENTIFIER_POINTER (format_type_id);
741 if (!strcmp (p, "printf") || !strcmp (p, "__printf__"))
743 format_type = printf_format_type;
746 else if (!strcmp (p, "printf0") || !strcmp (p, "__printf0__"))
748 format_type = printf_format_type;
751 else if (!strcmp (p, "scanf") || !strcmp (p, "__scanf__"))
753 format_type = scanf_format_type;
756 else if (!strcmp (p, "strftime")
757 || !strcmp (p, "__strftime__"))
759 format_type = strftime_format_type;
764 warning ("`%s' is an unrecognized format function type", p);
769 /* Strip any conversions from the string index and first arg number
770 and verify they are constants. */
771 while (TREE_CODE (format_num_expr) == NOP_EXPR
772 || TREE_CODE (format_num_expr) == CONVERT_EXPR
773 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
774 format_num_expr = TREE_OPERAND (format_num_expr, 0);
776 while (TREE_CODE (first_arg_num_expr) == NOP_EXPR
777 || TREE_CODE (first_arg_num_expr) == CONVERT_EXPR
778 || TREE_CODE (first_arg_num_expr) == NON_LVALUE_EXPR)
779 first_arg_num_expr = TREE_OPERAND (first_arg_num_expr, 0);
781 if (TREE_CODE (format_num_expr) != INTEGER_CST
782 || TREE_CODE (first_arg_num_expr) != INTEGER_CST)
784 error ("format string has non-constant operand number");
788 format_num = TREE_INT_CST_LOW (format_num_expr);
789 first_arg_num = TREE_INT_CST_LOW (first_arg_num_expr);
790 if (first_arg_num != 0 && first_arg_num <= format_num)
792 error ("format string arg follows the args to be formatted");
796 /* If a parameter list is specified, verify that the format_num
797 argument is actually a string, in case the format attribute
799 argument = TYPE_ARG_TYPES (type);
802 for (arg_num = 1; ; ++arg_num)
804 if (argument == 0 || arg_num == format_num)
806 argument = TREE_CHAIN (argument);
809 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
810 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
813 error ("format string arg not a string type");
816 if (first_arg_num != 0)
818 /* Verify that first_arg_num points to the last arg,
821 arg_num++, argument = TREE_CHAIN (argument);
822 if (arg_num != first_arg_num)
824 error ("args to be formatted is not ...");
830 record_function_format (DECL_NAME (decl),
831 DECL_ASSEMBLER_NAME (decl),
832 format_type, null_format_ok, format_num,
839 tree format_num_expr = TREE_VALUE (args);
840 int format_num, arg_num;
843 if (TREE_CODE (decl) != FUNCTION_DECL)
845 error_with_decl (decl,
846 "argument format specified for non-function `%s'");
850 /* Strip any conversions from the first arg number and verify it
852 while (TREE_CODE (format_num_expr) == NOP_EXPR
853 || TREE_CODE (format_num_expr) == CONVERT_EXPR
854 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
855 format_num_expr = TREE_OPERAND (format_num_expr, 0);
857 if (TREE_CODE (format_num_expr) != INTEGER_CST)
859 error ("format string has non-constant operand number");
863 format_num = TREE_INT_CST_LOW (format_num_expr);
865 /* If a parameter list is specified, verify that the format_num
866 argument is actually a string, in case the format attribute
868 argument = TYPE_ARG_TYPES (type);
871 for (arg_num = 1; ; ++arg_num)
873 if (argument == 0 || arg_num == format_num)
875 argument = TREE_CHAIN (argument);
878 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
879 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
882 error ("format string arg not a string type");
887 if (TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) != POINTER_TYPE
888 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_TYPE (decl))))
891 error ("function does not return string type");
895 record_international_format (DECL_NAME (decl),
896 DECL_ASSEMBLER_NAME (decl),
906 if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
907 || (TREE_CODE (decl) != FUNCTION_DECL && ! DECL_EXTERNAL (decl)))
908 error_with_decl (decl,
909 "`%s' defined both normally and as an alias");
910 else if (decl_function_context (decl) == 0)
914 id = TREE_VALUE (args);
915 if (TREE_CODE (id) != STRING_CST)
917 error ("alias arg not a string");
920 id = get_identifier (TREE_STRING_POINTER (id));
922 if (TREE_CODE (decl) == FUNCTION_DECL)
923 DECL_INITIAL (decl) = error_mark_node;
925 DECL_EXTERNAL (decl) = 0;
926 assemble_alias (decl, id);
929 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
932 case A_NO_CHECK_MEMORY_USAGE:
933 if (TREE_CODE (decl) != FUNCTION_DECL)
935 error_with_decl (decl,
936 "`%s' attribute applies only to functions",
937 IDENTIFIER_POINTER (name));
939 else if (DECL_INITIAL (decl))
941 error_with_decl (decl,
942 "can't set `%s' attribute after definition",
943 IDENTIFIER_POINTER (name));
946 DECL_NO_CHECK_MEMORY_USAGE (decl) = 1;
949 case A_NO_INSTRUMENT_FUNCTION:
950 if (TREE_CODE (decl) != FUNCTION_DECL)
952 error_with_decl (decl,
953 "`%s' attribute applies only to functions",
954 IDENTIFIER_POINTER (name));
956 else if (DECL_INITIAL (decl))
958 error_with_decl (decl,
959 "can't set `%s' attribute after definition",
960 IDENTIFIER_POINTER (name));
963 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
969 /* Split SPECS_ATTRS, a list of declspecs and prefix attributes, into two
970 lists. SPECS_ATTRS may also be just a typespec (eg: RECORD_TYPE).
972 The head of the declspec list is stored in DECLSPECS.
973 The head of the attribute list is stored in PREFIX_ATTRIBUTES.
975 Note that attributes in SPECS_ATTRS are stored in the TREE_PURPOSE of
976 the list elements. We drop the containing TREE_LIST nodes and link the
977 resulting attributes together the way decl_attributes expects them. */
980 split_specs_attrs (specs_attrs, declspecs, prefix_attributes)
982 tree *declspecs, *prefix_attributes;
984 tree t, s, a, next, specs, attrs;
986 /* This can happen after an __extension__ in pedantic mode. */
987 if (specs_attrs != NULL_TREE
988 && TREE_CODE (specs_attrs) == INTEGER_CST)
990 *declspecs = NULL_TREE;
991 *prefix_attributes = NULL_TREE;
995 /* This can happen in c++ (eg: decl: typespec initdecls ';'). */
996 if (specs_attrs != NULL_TREE
997 && TREE_CODE (specs_attrs) != TREE_LIST)
999 *declspecs = specs_attrs;
1000 *prefix_attributes = NULL_TREE;
1004 /* Remember to keep the lists in the same order, element-wise. */
1006 specs = s = NULL_TREE;
1007 attrs = a = NULL_TREE;
1008 for (t = specs_attrs; t; t = next)
1010 next = TREE_CHAIN (t);
1011 /* Declspecs have a non-NULL TREE_VALUE. */
1012 if (TREE_VALUE (t) != NULL_TREE)
1014 if (specs == NULL_TREE)
1024 if (attrs == NULL_TREE)
1025 attrs = a = TREE_PURPOSE (t);
1028 TREE_CHAIN (a) = TREE_PURPOSE (t);
1029 a = TREE_PURPOSE (t);
1031 /* More attrs can be linked here, move A to the end. */
1032 while (TREE_CHAIN (a) != NULL_TREE)
1037 /* Terminate the lists. */
1039 TREE_CHAIN (s) = NULL_TREE;
1041 TREE_CHAIN (a) = NULL_TREE;
1045 *prefix_attributes = attrs;
1048 /* Strip attributes from SPECS_ATTRS, a list of declspecs and attributes.
1049 This function is used by the parser when a rule will accept attributes
1050 in a particular position, but we don't want to support that just yet.
1052 A warning is issued for every ignored attribute. */
1055 strip_attrs (specs_attrs)
1060 split_specs_attrs (specs_attrs, &specs, &attrs);
1064 warning ("`%s' attribute ignored",
1065 IDENTIFIER_POINTER (TREE_PURPOSE (attrs)));
1066 attrs = TREE_CHAIN (attrs);
1072 /* Check a printf/fprintf/sprintf/scanf/fscanf/sscanf format against
1073 a parameter list. */
1075 #define T_I &integer_type_node
1076 #define T_L &long_integer_type_node
1077 #define T_LL &long_long_integer_type_node
1078 #define T_S &short_integer_type_node
1079 #define T_UI &unsigned_type_node
1080 #define T_UL &long_unsigned_type_node
1081 #define T_ULL &long_long_unsigned_type_node
1082 #define T_US &short_unsigned_type_node
1083 #define T_F &float_type_node
1084 #define T_D &double_type_node
1085 #define T_LD &long_double_type_node
1086 #define T_C &char_type_node
1087 #define T_UC &unsigned_char_type_node
1088 #define T_V &void_type_node
1089 #define T_W &wchar_type_node
1090 #define T_ST &sizetype
1093 const char *format_chars;
1095 /* Type of argument if no length modifier is used. */
1097 /* Type of argument if length modifier for shortening to byte is used.
1098 If NULL, then this modifier is not allowed. */
1100 /* Type of argument if length modifier for shortening is used.
1101 If NULL, then this modifier is not allowed. */
1103 /* Type of argument if length modifier `l' is used.
1104 If NULL, then this modifier is not allowed. */
1106 /* Type of argument if length modifier `q' or `ll' is used.
1107 If NULL, then this modifier is not allowed. */
1109 /* Type of argument if length modifier `L' is used.
1110 If NULL, then this modifier is not allowed. */
1112 /* Type of argument if length modifier `Z' is used.
1113 If NULL, then this modifier is not allowed. */
1115 /* List of other modifier characters allowed with these options. */
1116 const char *flag_chars;
1119 static format_char_info print_char_table[] = {
1120 /* FreeBSD kernel extensions. */
1121 { "D", 1, T_C, NULL, NULL, NULL, NULL, NULL, NULL, "-wp" },
1122 { "b", 1, T_C, NULL, NULL, NULL, NULL, NULL, NULL, "-wp" },
1123 { "rz", 0, T_I, NULL, NULL, T_L, NULL, NULL, NULL, "-wp0 +#" },
1124 #define unextended_print_char_table (print_char_table + 3)
1125 { "di", 0, T_I, T_I, T_I, T_L, T_LL, T_LL, T_ST, "-wp0 +" },
1126 { "oxX", 0, T_UI, T_UI, T_UI, T_UL, T_ULL, T_ULL, T_ST, "-wp0#" },
1127 { "u", 0, T_UI, T_UI, T_UI, T_UL, T_ULL, T_ULL, T_ST, "-wp0" },
1128 /* A GNU extension. */
1129 { "m", 0, T_V, NULL, NULL, NULL, NULL, NULL, NULL, "-wp" },
1130 { "feEgGaA", 0, T_D, NULL, NULL, NULL, NULL, T_LD, NULL, "-wp0 +#" },
1131 { "c", 0, T_I, NULL, NULL, T_W, NULL, NULL, NULL, "-w" },
1132 { "C", 0, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "-w" },
1133 { "s", 1, T_C, NULL, NULL, T_W, NULL, NULL, NULL, "-wp" },
1134 { "S", 1, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "-wp" },
1135 { "p", 1, T_V, NULL, NULL, NULL, NULL, NULL, NULL, "-w" },
1136 { "n", 1, T_I, NULL, T_S, T_L, T_LL, NULL, NULL, "" },
1137 { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
1140 static format_char_info scan_char_table[] = {
1141 { "di", 1, T_I, T_C, T_S, T_L, T_LL, T_LL, NULL, "*" },
1142 { "ouxX", 1, T_UI, T_UC, T_US, T_UL, T_ULL, T_ULL, NULL, "*" },
1143 { "efgEGaA", 1, T_F, NULL, NULL, T_D, NULL, T_LD, NULL, "*" },
1144 { "c", 1, T_C, NULL, NULL, T_W, NULL, NULL, NULL, "*" },
1145 { "s", 1, T_C, NULL, NULL, T_W, NULL, NULL, NULL, "*a" },
1146 { "[", 1, T_C, NULL, NULL, NULL, NULL, NULL, NULL, "*a" },
1147 { "C", 1, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "*" },
1148 { "S", 1, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "*a" },
1149 { "p", 2, T_V, NULL, NULL, NULL, NULL, NULL, NULL, "*" },
1150 { "n", 1, T_I, T_C, T_S, T_L, T_LL, NULL, NULL, "" },
1151 { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
1154 /* Handle format characters recognized by glibc's strftime.c.
1155 '2' - MUST do years as only two digits
1156 '3' - MAY do years as only two digits (depending on locale)
1157 'E' - E modifier is acceptable
1158 'O' - O modifier is acceptable to Standard C
1159 'o' - O modifier is acceptable as a GNU extension
1160 'G' - other GNU extensions */
1162 static format_char_info time_char_table[] = {
1163 { "y", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "2EO-_0w" },
1164 { "D", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "2" },
1165 { "g", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "2O-_0w" },
1166 { "cx", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "3E" },
1167 { "%+RTXnrt", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "" },
1168 { "P", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "G" },
1169 { "HIMSUWdemw", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0Ow" },
1170 { "Vju", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0Oow" },
1171 { "Gklsz", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0OGw" },
1172 { "ABZa", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "^#" },
1173 { "p", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "#" },
1174 { "bh", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "^" },
1175 { "CY", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0EOw" },
1176 { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
1179 typedef struct function_format_info
1181 struct function_format_info *next; /* next structure on the list */
1182 tree name; /* identifier such as "printf" */
1183 tree assembler_name; /* optional mangled identifier (for C++) */
1184 enum format_type format_type; /* type of format (printf, scanf, etc.) */
1185 int null_format_ok; /* TRUE if the format string may be NULL */
1186 int format_num; /* number of format argument */
1187 int first_arg_num; /* number of first arg (zero for varargs) */
1188 } function_format_info;
1190 static function_format_info *function_format_list = NULL;
1192 typedef struct international_format_info
1194 struct international_format_info *next; /* next structure on the list */
1195 tree name; /* identifier such as "gettext" */
1196 tree assembler_name; /* optional mangled identifier (for C++) */
1197 int format_num; /* number of format argument */
1198 } international_format_info;
1200 static international_format_info *international_format_list = NULL;
1202 static void check_format_info PROTO((function_format_info *, tree));
1204 /* Initialize the table of functions to perform format checking on.
1205 The ANSI functions are always checked (whether <stdio.h> is
1206 included or not), since it is common to call printf without
1207 including <stdio.h>. There shouldn't be a problem with this,
1208 since ANSI reserves these function names whether you include the
1209 header file or not. In any case, the checking is harmless.
1211 Also initialize the name of function that modify the format string for
1212 internationalization purposes. */
1215 init_function_format_info ()
1217 record_function_format (get_identifier ("printf"), NULL_TREE,
1218 printf_format_type, 0, 1, 2);
1219 record_function_format (get_identifier ("fprintf"), NULL_TREE,
1220 printf_format_type, 0, 2, 3);
1221 record_function_format (get_identifier ("sprintf"), NULL_TREE,
1222 printf_format_type, 0, 2, 3);
1223 record_function_format (get_identifier ("scanf"), NULL_TREE,
1224 scanf_format_type, 0, 1, 2);
1225 record_function_format (get_identifier ("fscanf"), NULL_TREE,
1226 scanf_format_type, 0, 2, 3);
1227 record_function_format (get_identifier ("sscanf"), NULL_TREE,
1228 scanf_format_type, 0, 2, 3);
1229 record_function_format (get_identifier ("vprintf"), NULL_TREE,
1230 printf_format_type, 0, 1, 0);
1231 record_function_format (get_identifier ("vfprintf"), NULL_TREE,
1232 printf_format_type, 0, 2, 0);
1233 record_function_format (get_identifier ("vsprintf"), NULL_TREE,
1234 printf_format_type, 0, 2, 0);
1235 record_function_format (get_identifier ("strftime"), NULL_TREE,
1236 strftime_format_type, 0, 3, 0);
1238 record_international_format (get_identifier ("gettext"), NULL_TREE, 1);
1239 record_international_format (get_identifier ("dgettext"), NULL_TREE, 2);
1240 record_international_format (get_identifier ("dcgettext"), NULL_TREE, 2);
1243 /* Record information for argument format checking. FUNCTION_IDENT is
1244 the identifier node for the name of the function to check (its decl
1245 need not exist yet).
1246 FORMAT_TYPE specifies the type of format checking. FORMAT_NUM is the number
1247 of the argument which is the format control string (starting from 1).
1248 FIRST_ARG_NUM is the number of the first actual argument to check
1249 against the format string, or zero if no checking is not be done
1250 (e.g. for varargs such as vfprintf). */
1253 record_function_format (name, assembler_name, format_type, null_format_ok,
1254 format_num, first_arg_num)
1256 tree assembler_name;
1257 enum format_type format_type;
1262 function_format_info *info;
1264 /* Re-use existing structure if it's there. */
1266 for (info = function_format_list; info; info = info->next)
1268 if (info->name == name && info->assembler_name == assembler_name)
1273 info = (function_format_info *) xmalloc (sizeof (function_format_info));
1274 info->next = function_format_list;
1275 function_format_list = info;
1278 info->assembler_name = assembler_name;
1281 info->format_type = format_type;
1282 info->null_format_ok = null_format_ok;
1283 info->format_num = format_num;
1284 info->first_arg_num = first_arg_num;
1287 /* Record information for the names of function that modify the format
1288 argument to format functions. FUNCTION_IDENT is the identifier node for
1289 the name of the function (its decl need not exist yet) and FORMAT_NUM is
1290 the number of the argument which is the format control string (starting
1294 record_international_format (name, assembler_name, format_num)
1296 tree assembler_name;
1299 international_format_info *info;
1301 /* Re-use existing structure if it's there. */
1303 for (info = international_format_list; info; info = info->next)
1305 if (info->name == name && info->assembler_name == assembler_name)
1312 = (international_format_info *)
1313 xmalloc (sizeof (international_format_info));
1314 info->next = international_format_list;
1315 international_format_list = info;
1318 info->assembler_name = assembler_name;
1321 info->format_num = format_num;
1327 warning ("too few arguments for format");
1330 function_format_info *
1331 find_function_format (name, assembler_name)
1333 tree assembler_name;
1335 function_format_info *info;
1337 for (info = function_format_list; info; info = info->next)
1338 if (info->assembler_name
1339 ? (info->assembler_name == assembler_name)
1340 : (info->name == name))
1345 /* Check the argument list of a call to printf, scanf, etc.
1346 NAME is the function identifier.
1347 ASSEMBLER_NAME is the function's assembler identifier.
1348 (Either NAME or ASSEMBLER_NAME, but not both, may be NULL_TREE.)
1349 PARAMS is the list of argument values. */
1352 check_function_format (name, assembler_name, params)
1354 tree assembler_name;
1357 function_format_info *info;
1359 /* See if this function is a format function. */
1360 info = find_function_format (name, assembler_name);
1363 check_format_info (info, params);
1366 /* Check the argument list of a call to printf, scanf, etc.
1367 INFO points to the function_format_info structure.
1368 PARAMS is the list of argument values. */
1371 check_format_info (info, params)
1372 function_format_info *info;
1377 int suppressed, wide, precise;
1378 int length_char = 0;
1385 tree first_fillin_param;
1386 const char *format_chars;
1387 format_char_info *fci = NULL;
1389 int has_operand_number = 0;
1391 /* Skip to format argument. If the argument isn't available, there's
1392 no work for us to do; prototype checking will catch the problem. */
1393 for (arg_num = 1; ; ++arg_num)
1397 if (arg_num == info->format_num)
1399 params = TREE_CHAIN (params);
1401 format_tree = TREE_VALUE (params);
1402 params = TREE_CHAIN (params);
1403 if (format_tree == 0)
1406 /* We can only check the format if it's a string constant. */
1408 while (TREE_CODE (format_tree) == NOP_EXPR)
1409 format_tree = TREE_OPERAND (format_tree, 0); /* strip coercion */
1411 if (TREE_CODE (format_tree) == CALL_EXPR
1412 && TREE_CODE (TREE_OPERAND (format_tree, 0)) == ADDR_EXPR
1413 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0))
1416 tree function = TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0);
1418 /* See if this is a call to a known internationalization function
1419 that modifies the format arg. */
1420 international_format_info *info;
1422 for (info = international_format_list; info; info = info->next)
1423 if (info->assembler_name
1424 ? (info->assembler_name == DECL_ASSEMBLER_NAME (function))
1425 : (info->name == DECL_NAME (function)))
1430 for (inner_args = TREE_OPERAND (format_tree, 1), i = 1;
1432 inner_args = TREE_CHAIN (inner_args), i++)
1433 if (i == info->format_num)
1435 format_tree = TREE_VALUE (inner_args);
1437 while (TREE_CODE (format_tree) == NOP_EXPR)
1438 format_tree = TREE_OPERAND (format_tree, 0);
1443 if (TREE_CODE (format_tree) == COND_EXPR)
1445 format_tree = TREE_OPERAND(format_tree, 1);
1449 if (integer_zerop (format_tree))
1451 if (!info->null_format_ok)
1452 warning ("null format string");
1455 if (TREE_CODE (format_tree) != ADDR_EXPR)
1457 if ((info->first_arg_num == 0) &&
1458 (TREE_CODE(format_tree) == PARM_DECL))
1460 function_format_info *i2;
1464 /* Now, we need to determine if the current function is printf-like,
1465 and, if so, if the parameter we have here is as a parameter of
1466 the current function and is in the argument slot declared to
1467 contain the format argument. */
1469 p = current_function_decl;
1471 i2 = find_function_format (p->decl.name, p->decl.assembler_name);
1475 if (warn_format > 1)
1476 warning("non-constant format parameter");
1480 for (n = 1, p = current_function_decl->decl.arguments;
1481 (n < i2->format_num) && (p != NULL);
1482 n++, p = TREE_CHAIN(p))
1484 if ((p == NULL) || (n != i2->format_num))
1485 warning("can't find format arg for current format function");
1486 else if (p != format_tree)
1487 warning("format argument passed here is not declared as format argument");
1490 else if ((info->format_type != strftime_format_type) &&
1492 warning("non-constant format parameter");
1495 format_tree = TREE_OPERAND (format_tree, 0);
1496 if (warn_format > 1 &&
1497 (TREE_CODE (format_tree) == VAR_DECL) &&
1498 TREE_READONLY(format_tree) &&
1499 (DECL_INITIAL(format_tree) != NULL) &&
1500 TREE_CODE(DECL_INITIAL(format_tree)) == STRING_CST)
1501 format_tree = DECL_INITIAL(format_tree);
1503 if (TREE_CODE (format_tree) != STRING_CST)
1505 if ((info->format_type != strftime_format_type) &&
1507 warning("non-constant format parameter");
1510 format_chars = TREE_STRING_POINTER (format_tree);
1511 format_length = TREE_STRING_LENGTH (format_tree);
1512 if (format_length <= 1)
1513 warning ("zero-length format string");
1514 if (format_chars[--format_length] != 0)
1516 warning ("unterminated format string");
1519 /* Skip to first argument to check. */
1520 while (arg_num + 1 < info->first_arg_num)
1524 params = TREE_CHAIN (params);
1528 first_fillin_param = params;
1532 if (*format_chars == 0)
1534 if (format_chars - TREE_STRING_POINTER (format_tree) != format_length)
1535 warning ("embedded `\\0' in format");
1536 if (info->first_arg_num != 0 && params != 0 && ! has_operand_number)
1538 if (warn_format_extra_args)
1539 warning ("too many arguments for format");
1543 if (*format_chars++ != '%')
1545 if (*format_chars == 0)
1547 warning ("spurious trailing `%%' in format");
1550 if (*format_chars == '%')
1556 suppressed = wide = precise = FALSE;
1557 if (info->format_type == scanf_format_type)
1559 suppressed = *format_chars == '*';
1562 while (ISDIGIT (*format_chars))
1565 else if (info->format_type == strftime_format_type)
1567 while (*format_chars != 0 && index ("_-0^#", *format_chars) != 0)
1570 warning ("ANSI C does not support the strftime `%c' flag",
1572 if (index (flag_chars, *format_chars) != 0)
1574 warning ("repeated `%c' flag in format",
1580 i = strlen (flag_chars);
1581 flag_chars[i++] = *format_chars++;
1585 while (ISDIGIT ((unsigned char) *format_chars))
1590 if (wide && pedantic)
1591 warning ("ANSI C does not support strftime format width");
1592 if (*format_chars == 'E' || *format_chars == 'O')
1594 i = strlen (flag_chars);
1595 flag_chars[i++] = *format_chars++;
1597 if (*format_chars == 'E' || *format_chars == 'O')
1599 warning ("multiple E/O modifiers in format");
1600 while (*format_chars == 'E' || *format_chars == 'O')
1605 else if (info->format_type == printf_format_type)
1607 /* See if we have a number followed by a dollar sign. If we do,
1608 it is an operand number, so set PARAMS to that operand. */
1609 if (*format_chars >= '0' && *format_chars <= '9')
1611 const char *p = format_chars;
1613 while (*p >= '0' && *p++ <= '9')
1618 int opnum = atoi (format_chars);
1620 params = first_fillin_param;
1621 format_chars = p + 1;
1622 has_operand_number = 1;
1624 for (i = 1; i < opnum && params != 0; i++)
1625 params = TREE_CHAIN (params);
1627 if (opnum == 0 || params == 0)
1629 warning ("operand number out of range in format");
1635 while (*format_chars != 0 && index (" +#0-", *format_chars) != 0)
1637 if (index (flag_chars, *format_chars) != 0)
1638 warning ("repeated `%c' flag in format", *format_chars++);
1641 i = strlen (flag_chars);
1642 flag_chars[i++] = *format_chars++;
1646 /* "If the space and + flags both appear,
1647 the space flag will be ignored." */
1648 if (index (flag_chars, ' ') != 0
1649 && index (flag_chars, '+') != 0)
1650 warning ("use of both ` ' and `+' flags in format");
1651 /* "If the 0 and - flags both appear,
1652 the 0 flag will be ignored." */
1653 if (index (flag_chars, '0') != 0
1654 && index (flag_chars, '-') != 0)
1655 warning ("use of both `0' and `-' flags in format");
1656 if (*format_chars == '*')
1659 /* "...a field width...may be indicated by an asterisk.
1660 In this case, an int argument supplies the field width..." */
1667 if (info->first_arg_num != 0)
1669 cur_param = TREE_VALUE (params);
1670 params = TREE_CHAIN (params);
1672 /* size_t is generally not valid here.
1673 It will work on most machines, because size_t and int
1674 have the same mode. But might as well warn anyway,
1675 since it will fail on other machines. */
1676 /* XXX should we allow unsigned ints here? */
1677 if ((TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1678 != integer_type_node)
1680 (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1681 != unsigned_type_node))
1682 warning ("precision is not type int (arg %d)", arg_num);
1687 while (ISDIGIT (*format_chars))
1693 if (*format_chars == '.')
1697 if (*format_chars != '*' && !ISDIGIT (*format_chars))
1698 warning ("`.' not followed by `*' or digit in format");
1699 /* "...a...precision...may be indicated by an asterisk.
1700 In this case, an int argument supplies the...precision." */
1701 if (*format_chars == '*')
1703 if (info->first_arg_num != 0)
1711 cur_param = TREE_VALUE (params);
1712 params = TREE_CHAIN (params);
1714 if (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1715 != integer_type_node)
1716 warning ("field width is not type int (arg %d)",
1722 while (ISDIGIT (*format_chars))
1727 if (*format_chars == 'b')
1729 /* There should be an int arg to control the string arg. */
1735 if (info->first_arg_num != 0)
1737 cur_param = TREE_VALUE (params);
1738 params = TREE_CHAIN (params);
1740 if ((TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1741 != integer_type_node)
1743 (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1744 != unsigned_type_node))
1746 warning ("bitmap is not type int (arg %d)", arg_num);
1750 if (*format_chars == 'D')
1752 /* There should be an unsigned char * arg before the string arg. */
1758 if (info->first_arg_num != 0)
1760 cur_param = TREE_VALUE (params);
1761 params = TREE_CHAIN (params);
1763 cur_type = TREE_TYPE (cur_param);
1764 if (TREE_CODE (cur_type) != POINTER_TYPE
1765 || TYPE_MAIN_VARIANT (TREE_TYPE (cur_type))
1766 != unsigned_char_type_node)
1768 warning ("ethernet address is not type unsigned char *"
1777 if (info->format_type != strftime_format_type)
1779 if (*format_chars == 'h' || *format_chars == 'l')
1780 length_char = *format_chars++;
1781 else if ((*format_chars == 'q' || *format_chars == 'L')
1782 && !flag_format_extensions)
1784 length_char = *format_chars++;
1786 warning ("ANSI C does not support the `%c' length modifier",
1789 else if (*format_chars == 'Z')
1791 length_char = *format_chars++;
1793 warning ("ANSI C does not support the `Z' length modifier");
1797 if (length_char == 'l' && *format_chars == 'l')
1799 length_char = 'q', format_chars++;
1800 /* FIXME: Is allowed in ISO C 9x. */
1802 warning ("ANSI C does not support the `ll' length modifier");
1804 else if (length_char == 'h' && *format_chars == 'h')
1806 length_char = 'H', format_chars++;
1807 /* FIXME: Is allowed in ISO C 9x. */
1809 warning ("ANSI C does not support the `hh' length modifier");
1811 if (*format_chars == 'a' && info->format_type == scanf_format_type)
1813 if (format_chars[1] == 's' || format_chars[1] == 'S'
1814 || format_chars[1] == '[')
1816 /* `a' is used as a flag. */
1821 if (suppressed && length_char != 0)
1822 warning ("use of `*' and `%c' together in format", length_char);
1824 format_char = *format_chars;
1825 if (format_char == 0
1826 || (info->format_type != strftime_format_type && format_char == '%'))
1828 warning ("conversion lacks type at end of format");
1831 /* The m, C, and S formats are GNU extensions. */
1832 if (pedantic && info->format_type != strftime_format_type
1833 && (format_char == 'm' || format_char == 'C' || format_char == 'S'))
1834 warning ("ANSI C does not support the `%c' format", format_char);
1835 /* ??? The a and A formats are C9X extensions, and should be allowed
1836 when a C9X option is added. */
1837 if (pedantic && info->format_type != strftime_format_type
1838 && (format_char == 'a' || format_char == 'A'))
1839 warning ("ANSI C does not support the `%c' format", format_char);
1841 switch (info->format_type)
1843 case printf_format_type:
1844 fci = flag_format_extensions ? print_char_table
1845 : unextended_print_char_table;
1847 case scanf_format_type:
1848 fci = scan_char_table;
1850 case strftime_format_type:
1851 fci = time_char_table;
1856 while (fci->format_chars != 0
1857 && index (fci->format_chars, format_char) == 0)
1859 if (fci->format_chars == 0)
1861 if (format_char >= 040 && format_char < 0177)
1862 warning ("unknown conversion type character `%c' in format",
1865 warning ("unknown conversion type character 0x%x in format",
1871 if (index (fci->flag_chars, 'G') != 0)
1872 warning ("ANSI C does not support `%%%c'", format_char);
1873 if (index (fci->flag_chars, 'o') != 0
1874 && index (flag_chars, 'O') != 0)
1875 warning ("ANSI C does not support `%%O%c'", format_char);
1877 if (wide && index (fci->flag_chars, 'w') == 0)
1878 warning ("width used with `%c' format", format_char);
1879 if (index (fci->flag_chars, '2') != 0)
1880 warning ("`%%%c' yields only last 2 digits of year", format_char);
1881 else if (index (fci->flag_chars, '3') != 0)
1882 warning ("`%%%c' yields only last 2 digits of year in some locales on non-BSD systems",
1884 if (precise && index (fci->flag_chars, 'p') == 0)
1885 warning ("precision used with `%c' format", format_char);
1886 if (aflag && index (fci->flag_chars, 'a') == 0)
1888 warning ("`a' flag used with `%c' format", format_char);
1889 /* To simplify the following code. */
1892 /* The a flag is a GNU extension. */
1893 else if (pedantic && aflag)
1894 warning ("ANSI C does not support the `a' flag");
1895 if (info->format_type == scanf_format_type && format_char == '[')
1897 /* Skip over scan set, in case it happens to have '%' in it. */
1898 if (*format_chars == '^')
1900 /* Find closing bracket; if one is hit immediately, then
1901 it's part of the scan set rather than a terminator. */
1902 if (*format_chars == ']')
1904 while (*format_chars && *format_chars != ']')
1906 if (*format_chars != ']')
1907 /* The end of the format string was reached. */
1908 warning ("no closing `]' for `%%[' format");
1912 if (index (fci->flag_chars, '*') == 0)
1913 warning ("suppression of `%c' conversion in format", format_char);
1916 for (i = 0; flag_chars[i] != 0; ++i)
1918 if (index (fci->flag_chars, flag_chars[i]) == 0)
1919 warning ("flag `%c' used with type `%c'",
1920 flag_chars[i], format_char);
1922 if (info->format_type == strftime_format_type)
1924 if (precise && index (flag_chars, '0') != 0
1925 && (format_char == 'd' || format_char == 'i'
1926 || format_char == 'o' || format_char == 'u'
1927 || format_char == 'x' || format_char == 'X'))
1928 warning ("`0' flag ignored with precision specifier and `%c' format",
1930 switch (length_char)
1932 default: wanted_type = fci->nolen ? *(fci->nolen) : 0; break;
1933 case 'H': wanted_type = fci->hhlen ? *(fci->hhlen) : 0; break;
1934 case 'h': wanted_type = fci->hlen ? *(fci->hlen) : 0; break;
1935 case 'l': wanted_type = fci->llen ? *(fci->llen) : 0; break;
1936 case 'q': wanted_type = fci->qlen ? *(fci->qlen) : 0; break;
1937 case 'L': wanted_type = fci->bigllen ? *(fci->bigllen) : 0; break;
1938 case 'Z': wanted_type = fci->zlen ? *fci->zlen : 0; break;
1940 if (wanted_type == 0)
1941 warning ("use of `%c' length character with `%c' type character",
1942 length_char, format_char);
1944 /* Finally. . .check type of argument against desired type! */
1945 if (info->first_arg_num == 0)
1947 if (fci->pointer_count == 0 && wanted_type == void_type_node)
1948 /* This specifier takes no argument. */
1955 cur_param = TREE_VALUE (params);
1956 params = TREE_CHAIN (params);
1958 cur_type = TREE_TYPE (cur_param);
1960 STRIP_NOPS (cur_param);
1962 /* Check the types of any additional pointer arguments
1963 that precede the "real" argument. */
1964 for (i = 0; i < fci->pointer_count + aflag; ++i)
1966 if (TREE_CODE (cur_type) == POINTER_TYPE)
1968 cur_type = TREE_TYPE (cur_type);
1970 if (cur_param != 0 && TREE_CODE (cur_param) == ADDR_EXPR)
1971 cur_param = TREE_OPERAND (cur_param, 0);
1977 if (TREE_CODE (cur_type) != ERROR_MARK)
1978 warning ((fci->pointer_count + aflag == 1
1979 ? "format argument is not a pointer (arg %d)"
1980 : "format argument is not a pointer to a pointer (arg %d)"),
1985 /* See if this is an attempt to write into a const type with
1986 scanf or with printf "%n". */
1987 if ((info->format_type == scanf_format_type
1988 || (info->format_type == printf_format_type
1989 && format_char == 'n'))
1990 && i == fci->pointer_count + aflag
1992 && TREE_CODE (cur_type) != ERROR_MARK
1993 && (TYPE_READONLY (cur_type)
1995 && (TREE_CODE_CLASS (TREE_CODE (cur_param)) == 'c'
1996 || (TREE_CODE_CLASS (TREE_CODE (cur_param)) == 'd'
1997 && TREE_READONLY (cur_param))))))
1998 warning ("writing into constant object (arg %d)", arg_num);
2000 /* Check the type of the "real" argument, if there's a type we want. */
2001 if (i == fci->pointer_count + aflag && wanted_type != 0
2002 && TREE_CODE (cur_type) != ERROR_MARK
2003 && wanted_type != TYPE_MAIN_VARIANT (cur_type)
2004 /* If we want `void *', allow any pointer type.
2005 (Anything else would already have got a warning.) */
2006 && ! (wanted_type == void_type_node
2007 && fci->pointer_count > 0)
2008 /* Don't warn about differences merely in signedness. */
2009 && !(TREE_CODE (wanted_type) == INTEGER_TYPE
2010 && TREE_CODE (TYPE_MAIN_VARIANT (cur_type)) == INTEGER_TYPE
2011 && (TREE_UNSIGNED (wanted_type)
2012 ? wanted_type == (cur_type = unsigned_type (cur_type))
2013 : wanted_type == (cur_type = signed_type (cur_type))))
2014 /* Likewise, "signed char", "unsigned char" and "char" are
2015 equivalent but the above test won't consider them equivalent. */
2016 && ! (wanted_type == char_type_node
2017 && (TYPE_MAIN_VARIANT (cur_type) == signed_char_type_node
2018 || TYPE_MAIN_VARIANT (cur_type) == unsigned_char_type_node)))
2020 register const char *this;
2021 register const char *that;
2023 this = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (wanted_type)));
2025 if (TREE_CODE (cur_type) != ERROR_MARK
2026 && TYPE_NAME (cur_type) != 0
2027 && TREE_CODE (cur_type) != INTEGER_TYPE
2028 && !(TREE_CODE (cur_type) == POINTER_TYPE
2029 && TREE_CODE (TREE_TYPE (cur_type)) == INTEGER_TYPE))
2031 if (TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL
2032 && DECL_NAME (TYPE_NAME (cur_type)) != 0)
2033 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
2035 that = IDENTIFIER_POINTER (TYPE_NAME (cur_type));
2038 /* A nameless type can't possibly match what the format wants.
2039 So there will be a warning for it.
2040 Make up a string to describe vaguely what it is. */
2043 if (TREE_CODE (cur_type) == POINTER_TYPE)
2046 that = "different type";
2049 /* Make the warning better in case of mismatch of int vs long. */
2050 if (TREE_CODE (cur_type) == INTEGER_TYPE
2051 && TREE_CODE (wanted_type) == INTEGER_TYPE
2052 && TYPE_PRECISION (cur_type) == TYPE_PRECISION (wanted_type)
2053 && TYPE_NAME (cur_type) != 0
2054 && TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL)
2055 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
2057 if (strcmp (this, that) != 0)
2058 warning ("%s format, %s arg (arg %d)", this, that, arg_num);
2063 /* Print a warning if a constant expression had overflow in folding.
2064 Invoke this function on every expression that the language
2065 requires to be a constant expression.
2066 Note the ANSI C standard says it is erroneous for a
2067 constant expression to overflow. */
2070 constant_expression_warning (value)
2073 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
2074 || TREE_CODE (value) == COMPLEX_CST)
2075 && TREE_CONSTANT_OVERFLOW (value) && pedantic)
2076 pedwarn ("overflow in constant expression");
2079 /* Print a warning if an expression had overflow in folding.
2080 Invoke this function on every expression that
2081 (1) appears in the source code, and
2082 (2) might be a constant expression that overflowed, and
2083 (3) is not already checked by convert_and_check;
2084 however, do not invoke this function on operands of explicit casts. */
2087 overflow_warning (value)
2090 if ((TREE_CODE (value) == INTEGER_CST
2091 || (TREE_CODE (value) == COMPLEX_CST
2092 && TREE_CODE (TREE_REALPART (value)) == INTEGER_CST))
2093 && TREE_OVERFLOW (value))
2095 TREE_OVERFLOW (value) = 0;
2096 if (skip_evaluation == 0)
2097 warning ("integer overflow in expression");
2099 else if ((TREE_CODE (value) == REAL_CST
2100 || (TREE_CODE (value) == COMPLEX_CST
2101 && TREE_CODE (TREE_REALPART (value)) == REAL_CST))
2102 && TREE_OVERFLOW (value))
2104 TREE_OVERFLOW (value) = 0;
2105 if (skip_evaluation == 0)
2106 warning ("floating point overflow in expression");
2110 /* Print a warning if a large constant is truncated to unsigned,
2111 or if -Wconversion is used and a constant < 0 is converted to unsigned.
2112 Invoke this function on every expression that might be implicitly
2113 converted to an unsigned type. */
2116 unsigned_conversion_warning (result, operand)
2117 tree result, operand;
2119 if (TREE_CODE (operand) == INTEGER_CST
2120 && TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
2121 && TREE_UNSIGNED (TREE_TYPE (result))
2122 && skip_evaluation == 0
2123 && !int_fits_type_p (operand, TREE_TYPE (result)))
2125 if (!int_fits_type_p (operand, signed_type (TREE_TYPE (result))))
2126 /* This detects cases like converting -129 or 256 to unsigned char. */
2127 warning ("large integer implicitly truncated to unsigned type");
2128 else if (warn_conversion)
2129 warning ("negative integer implicitly converted to unsigned type");
2133 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2134 Invoke this function on every expression that is converted implicitly,
2135 i.e. because of language rules and not because of an explicit cast. */
2138 convert_and_check (type, expr)
2141 tree t = convert (type, expr);
2142 if (TREE_CODE (t) == INTEGER_CST)
2144 if (TREE_OVERFLOW (t))
2146 TREE_OVERFLOW (t) = 0;
2148 /* Do not diagnose overflow in a constant expression merely
2149 because a conversion overflowed. */
2150 TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (expr);
2152 /* No warning for converting 0x80000000 to int. */
2153 if (!(TREE_UNSIGNED (type) < TREE_UNSIGNED (TREE_TYPE (expr))
2154 && TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
2155 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (expr))))
2156 /* If EXPR fits in the unsigned version of TYPE,
2157 don't warn unless pedantic. */
2159 || TREE_UNSIGNED (type)
2160 || ! int_fits_type_p (expr, unsigned_type (type)))
2161 && skip_evaluation == 0)
2162 warning ("overflow in implicit constant conversion");
2165 unsigned_conversion_warning (t, expr);
2171 c_expand_expr_stmt (expr)
2174 /* Do default conversion if safe and possibly important,
2175 in case within ({...}). */
2176 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE && lvalue_p (expr))
2177 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
2178 expr = default_conversion (expr);
2180 if (TREE_TYPE (expr) != error_mark_node
2181 && TYPE_SIZE (TREE_TYPE (expr)) == 0
2182 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
2183 error ("expression statement has incomplete type");
2185 expand_expr_stmt (expr);
2188 /* Validate the expression after `case' and apply default promotions. */
2191 check_case_value (value)
2194 if (value == NULL_TREE)
2197 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
2198 STRIP_TYPE_NOPS (value);
2200 if (TREE_CODE (value) != INTEGER_CST
2201 && value != error_mark_node)
2203 error ("case label does not reduce to an integer constant");
2204 value = error_mark_node;
2207 /* Promote char or short to int. */
2208 value = default_conversion (value);
2210 constant_expression_warning (value);
2215 /* Return an integer type with BITS bits of precision,
2216 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
2219 type_for_size (bits, unsignedp)
2223 if (bits == TYPE_PRECISION (integer_type_node))
2224 return unsignedp ? unsigned_type_node : integer_type_node;
2226 if (bits == TYPE_PRECISION (signed_char_type_node))
2227 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2229 if (bits == TYPE_PRECISION (short_integer_type_node))
2230 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2232 if (bits == TYPE_PRECISION (long_integer_type_node))
2233 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2235 if (bits == TYPE_PRECISION (long_long_integer_type_node))
2236 return (unsignedp ? long_long_unsigned_type_node
2237 : long_long_integer_type_node);
2239 if (bits <= TYPE_PRECISION (intQI_type_node))
2240 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2242 if (bits <= TYPE_PRECISION (intHI_type_node))
2243 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2245 if (bits <= TYPE_PRECISION (intSI_type_node))
2246 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2248 if (bits <= TYPE_PRECISION (intDI_type_node))
2249 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2254 /* Return a data type that has machine mode MODE.
2255 If the mode is an integer,
2256 then UNSIGNEDP selects between signed and unsigned types. */
2259 type_for_mode (mode, unsignedp)
2260 enum machine_mode mode;
2263 if (mode == TYPE_MODE (integer_type_node))
2264 return unsignedp ? unsigned_type_node : integer_type_node;
2266 if (mode == TYPE_MODE (signed_char_type_node))
2267 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2269 if (mode == TYPE_MODE (short_integer_type_node))
2270 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2272 if (mode == TYPE_MODE (long_integer_type_node))
2273 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2275 if (mode == TYPE_MODE (long_long_integer_type_node))
2276 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
2278 if (mode == TYPE_MODE (intQI_type_node))
2279 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2281 if (mode == TYPE_MODE (intHI_type_node))
2282 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2284 if (mode == TYPE_MODE (intSI_type_node))
2285 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2287 if (mode == TYPE_MODE (intDI_type_node))
2288 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2290 #if HOST_BITS_PER_WIDE_INT >= 64
2291 if (mode == TYPE_MODE (intTI_type_node))
2292 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2295 if (mode == TYPE_MODE (float_type_node))
2296 return float_type_node;
2298 if (mode == TYPE_MODE (double_type_node))
2299 return double_type_node;
2301 if (mode == TYPE_MODE (long_double_type_node))
2302 return long_double_type_node;
2304 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
2305 return build_pointer_type (char_type_node);
2307 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
2308 return build_pointer_type (integer_type_node);
2313 /* Return the minimum number of bits needed to represent VALUE in a
2314 signed or unsigned type, UNSIGNEDP says which. */
2317 min_precision (value, unsignedp)
2323 /* If the value is negative, compute its negative minus 1. The latter
2324 adjustment is because the absolute value of the largest negative value
2325 is one larger than the largest positive value. This is equivalent to
2326 a bit-wise negation, so use that operation instead. */
2328 if (tree_int_cst_sgn (value) < 0)
2329 value = fold (build1 (BIT_NOT_EXPR, TREE_TYPE (value), value));
2331 /* Return the number of bits needed, taking into account the fact
2332 that we need one more bit for a signed than unsigned type. */
2334 if (integer_zerop (value))
2336 else if (TREE_INT_CST_HIGH (value) != 0)
2337 log = HOST_BITS_PER_WIDE_INT + floor_log2 (TREE_INT_CST_HIGH (value));
2339 log = floor_log2 (TREE_INT_CST_LOW (value));
2341 return log + 1 + ! unsignedp;
2344 /* Print an error message for invalid operands to arith operation CODE.
2345 NOP_EXPR is used as a special case (see truthvalue_conversion). */
2348 binary_op_error (code)
2349 enum tree_code code;
2351 register const char *opname;
2356 error ("invalid truth-value expression");
2360 opname = "+"; break;
2362 opname = "-"; break;
2364 opname = "*"; break;
2366 opname = "max"; break;
2368 opname = "min"; break;
2370 opname = "=="; break;
2372 opname = "!="; break;
2374 opname = "<="; break;
2376 opname = ">="; break;
2378 opname = "<"; break;
2380 opname = ">"; break;
2382 opname = "<<"; break;
2384 opname = ">>"; break;
2385 case TRUNC_MOD_EXPR:
2386 case FLOOR_MOD_EXPR:
2387 opname = "%"; break;
2388 case TRUNC_DIV_EXPR:
2389 case FLOOR_DIV_EXPR:
2390 opname = "/"; break;
2392 opname = "&"; break;
2394 opname = "|"; break;
2395 case TRUTH_ANDIF_EXPR:
2396 opname = "&&"; break;
2397 case TRUTH_ORIF_EXPR:
2398 opname = "||"; break;
2400 opname = "^"; break;
2403 opname = "rotate"; break;
2405 opname = "unknown"; break;
2407 error ("invalid operands to binary %s", opname);
2410 /* Subroutine of build_binary_op, used for comparison operations.
2411 See if the operands have both been converted from subword integer types
2412 and, if so, perhaps change them both back to their original type.
2413 This function is also responsible for converting the two operands
2414 to the proper common type for comparison.
2416 The arguments of this function are all pointers to local variables
2417 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
2418 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
2420 If this function returns nonzero, it means that the comparison has
2421 a constant value. What this function returns is an expression for
2425 shorten_compare (op0_ptr, op1_ptr, restype_ptr, rescode_ptr)
2426 tree *op0_ptr, *op1_ptr;
2428 enum tree_code *rescode_ptr;
2431 tree op0 = *op0_ptr;
2432 tree op1 = *op1_ptr;
2433 int unsignedp0, unsignedp1;
2435 tree primop0, primop1;
2436 enum tree_code code = *rescode_ptr;
2438 /* Throw away any conversions to wider types
2439 already present in the operands. */
2441 primop0 = get_narrower (op0, &unsignedp0);
2442 primop1 = get_narrower (op1, &unsignedp1);
2444 /* Handle the case that OP0 does not *contain* a conversion
2445 but it *requires* conversion to FINAL_TYPE. */
2447 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
2448 unsignedp0 = TREE_UNSIGNED (TREE_TYPE (op0));
2449 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
2450 unsignedp1 = TREE_UNSIGNED (TREE_TYPE (op1));
2452 /* If one of the operands must be floated, we cannot optimize. */
2453 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
2454 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
2456 /* If first arg is constant, swap the args (changing operation
2457 so value is preserved), for canonicalization. Don't do this if
2458 the second arg is 0. */
2460 if (TREE_CONSTANT (primop0)
2461 && ! integer_zerop (primop1) && ! real_zerop (primop1))
2463 register tree tem = primop0;
2464 register int temi = unsignedp0;
2472 unsignedp0 = unsignedp1;
2495 *rescode_ptr = code;
2498 /* If comparing an integer against a constant more bits wide,
2499 maybe we can deduce a value of 1 or 0 independent of the data.
2500 Or else truncate the constant now
2501 rather than extend the variable at run time.
2503 This is only interesting if the constant is the wider arg.
2504 Also, it is not safe if the constant is unsigned and the
2505 variable arg is signed, since in this case the variable
2506 would be sign-extended and then regarded as unsigned.
2507 Our technique fails in this case because the lowest/highest
2508 possible unsigned results don't follow naturally from the
2509 lowest/highest possible values of the variable operand.
2510 For just EQ_EXPR and NE_EXPR there is another technique that
2511 could be used: see if the constant can be faithfully represented
2512 in the other operand's type, by truncating it and reextending it
2513 and see if that preserves the constant's value. */
2515 if (!real1 && !real2
2516 && TREE_CODE (primop1) == INTEGER_CST
2517 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
2519 int min_gt, max_gt, min_lt, max_lt;
2520 tree maxval, minval;
2521 /* 1 if comparison is nominally unsigned. */
2522 int unsignedp = TREE_UNSIGNED (*restype_ptr);
2525 type = signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0));
2527 /* If TYPE is an enumeration, then we need to get its min/max
2528 values from it's underlying integral type, not the enumerated
2530 if (TREE_CODE (type) == ENUMERAL_TYPE)
2531 type = type_for_size (TYPE_PRECISION (type), unsignedp0);
2533 maxval = TYPE_MAX_VALUE (type);
2534 minval = TYPE_MIN_VALUE (type);
2536 if (unsignedp && !unsignedp0)
2537 *restype_ptr = signed_type (*restype_ptr);
2539 if (TREE_TYPE (primop1) != *restype_ptr)
2540 primop1 = convert (*restype_ptr, primop1);
2541 if (type != *restype_ptr)
2543 minval = convert (*restype_ptr, minval);
2544 maxval = convert (*restype_ptr, maxval);
2547 if (unsignedp && unsignedp0)
2549 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
2550 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
2551 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
2552 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
2556 min_gt = INT_CST_LT (primop1, minval);
2557 max_gt = INT_CST_LT (primop1, maxval);
2558 min_lt = INT_CST_LT (minval, primop1);
2559 max_lt = INT_CST_LT (maxval, primop1);
2563 /* This used to be a switch, but Genix compiler can't handle that. */
2564 if (code == NE_EXPR)
2566 if (max_lt || min_gt)
2567 val = boolean_true_node;
2569 else if (code == EQ_EXPR)
2571 if (max_lt || min_gt)
2572 val = boolean_false_node;
2574 else if (code == LT_EXPR)
2577 val = boolean_true_node;
2579 val = boolean_false_node;
2581 else if (code == GT_EXPR)
2584 val = boolean_true_node;
2586 val = boolean_false_node;
2588 else if (code == LE_EXPR)
2591 val = boolean_true_node;
2593 val = boolean_false_node;
2595 else if (code == GE_EXPR)
2598 val = boolean_true_node;
2600 val = boolean_false_node;
2603 /* If primop0 was sign-extended and unsigned comparison specd,
2604 we did a signed comparison above using the signed type bounds.
2605 But the comparison we output must be unsigned.
2607 Also, for inequalities, VAL is no good; but if the signed
2608 comparison had *any* fixed result, it follows that the
2609 unsigned comparison just tests the sign in reverse
2610 (positive values are LE, negative ones GE).
2611 So we can generate an unsigned comparison
2612 against an extreme value of the signed type. */
2614 if (unsignedp && !unsignedp0)
2621 primop1 = TYPE_MIN_VALUE (type);
2627 primop1 = TYPE_MAX_VALUE (type);
2634 type = unsigned_type (type);
2637 if (!max_gt && !unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
2639 /* This is the case of (char)x >?< 0x80, which people used to use
2640 expecting old C compilers to change the 0x80 into -0x80. */
2641 if (val == boolean_false_node)
2642 warning ("comparison is always false due to limited range of data type");
2643 if (val == boolean_true_node)
2644 warning ("comparison is always true due to limited range of data type");
2647 if (!min_lt && unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
2649 /* This is the case of (unsigned char)x >?< -1 or < 0. */
2650 if (val == boolean_false_node)
2651 warning ("comparison is always false due to limited range of data type");
2652 if (val == boolean_true_node)
2653 warning ("comparison is always true due to limited range of data type");
2658 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2659 if (TREE_SIDE_EFFECTS (primop0))
2660 return build (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
2664 /* Value is not predetermined, but do the comparison
2665 in the type of the operand that is not constant.
2666 TYPE is already properly set. */
2668 else if (real1 && real2
2669 && (TYPE_PRECISION (TREE_TYPE (primop0))
2670 == TYPE_PRECISION (TREE_TYPE (primop1))))
2671 type = TREE_TYPE (primop0);
2673 /* If args' natural types are both narrower than nominal type
2674 and both extend in the same manner, compare them
2675 in the type of the wider arg.
2676 Otherwise must actually extend both to the nominal
2677 common type lest different ways of extending
2679 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
2681 else if (unsignedp0 == unsignedp1 && real1 == real2
2682 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
2683 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
2685 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
2686 type = signed_or_unsigned_type (unsignedp0
2687 || TREE_UNSIGNED (*restype_ptr),
2689 /* Make sure shorter operand is extended the right way
2690 to match the longer operand. */
2691 primop0 = convert (signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0)),
2693 primop1 = convert (signed_or_unsigned_type (unsignedp1, TREE_TYPE (primop1)),
2698 /* Here we must do the comparison on the nominal type
2699 using the args exactly as we received them. */
2700 type = *restype_ptr;
2704 if (!real1 && !real2 && integer_zerop (primop1)
2705 && TREE_UNSIGNED (*restype_ptr))
2711 /* All unsigned values are >= 0, so we warn if extra warnings
2712 are requested. However, if OP0 is a constant that is
2713 >= 0, the signedness of the comparison isn't an issue,
2714 so suppress the warning. */
2716 && ! (TREE_CODE (primop0) == INTEGER_CST
2717 && ! TREE_OVERFLOW (convert (signed_type (type),
2719 warning ("comparison of unsigned expression >= 0 is always true");
2720 value = boolean_true_node;
2725 && ! (TREE_CODE (primop0) == INTEGER_CST
2726 && ! TREE_OVERFLOW (convert (signed_type (type),
2728 warning ("comparison of unsigned expression < 0 is always false");
2729 value = boolean_false_node;
2738 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2739 if (TREE_SIDE_EFFECTS (primop0))
2740 return build (COMPOUND_EXPR, TREE_TYPE (value),
2747 *op0_ptr = convert (type, primop0);
2748 *op1_ptr = convert (type, primop1);
2750 *restype_ptr = boolean_type_node;
2755 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
2756 or validate its data type for an `if' or `while' statement or ?..: exp.
2758 This preparation consists of taking the ordinary
2759 representation of an expression expr and producing a valid tree
2760 boolean expression describing whether expr is nonzero. We could
2761 simply always do build_binary_op (NE_EXPR, expr, boolean_false_node, 1),
2762 but we optimize comparisons, &&, ||, and !.
2764 The resulting type should always be `boolean_type_node'. */
2767 truthvalue_conversion (expr)
2770 if (TREE_CODE (expr) == ERROR_MARK)
2773 #if 0 /* This appears to be wrong for C++. */
2774 /* These really should return error_mark_node after 2.4 is stable.
2775 But not all callers handle ERROR_MARK properly. */
2776 switch (TREE_CODE (TREE_TYPE (expr)))
2779 error ("struct type value used where scalar is required");
2780 return boolean_false_node;
2783 error ("union type value used where scalar is required");
2784 return boolean_false_node;
2787 error ("array type value used where scalar is required");
2788 return boolean_false_node;
2795 switch (TREE_CODE (expr))
2797 /* It is simpler and generates better code to have only TRUTH_*_EXPR
2798 or comparison expressions as truth values at this level. */
2801 /* A one-bit unsigned bit-field is already acceptable. */
2802 if (1 == TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (expr, 1)))
2803 && TREE_UNSIGNED (TREE_OPERAND (expr, 1)))
2809 /* It is simpler and generates better code to have only TRUTH_*_EXPR
2810 or comparison expressions as truth values at this level. */
2812 if (integer_zerop (TREE_OPERAND (expr, 1)))
2813 return build_unary_op (TRUTH_NOT_EXPR, TREE_OPERAND (expr, 0), 0);
2815 case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
2816 case TRUTH_ANDIF_EXPR:
2817 case TRUTH_ORIF_EXPR:
2818 case TRUTH_AND_EXPR:
2820 case TRUTH_XOR_EXPR:
2821 case TRUTH_NOT_EXPR:
2822 TREE_TYPE (expr) = boolean_type_node;
2829 return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
2832 return real_zerop (expr) ? boolean_false_node : boolean_true_node;
2835 /* If we are taking the address of a external decl, it might be zero
2836 if it is weak, so we cannot optimize. */
2837 if (TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (expr, 0))) == 'd'
2838 && DECL_EXTERNAL (TREE_OPERAND (expr, 0)))
2841 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0)))
2842 return build (COMPOUND_EXPR, boolean_type_node,
2843 TREE_OPERAND (expr, 0), boolean_true_node);
2845 return boolean_true_node;
2848 return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
2849 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
2850 truthvalue_conversion (TREE_OPERAND (expr, 0)),
2851 truthvalue_conversion (TREE_OPERAND (expr, 1)),
2858 /* These don't change whether an object is non-zero or zero. */
2859 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2863 /* These don't change whether an object is zero or non-zero, but
2864 we can't ignore them if their second arg has side-effects. */
2865 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
2866 return build (COMPOUND_EXPR, boolean_type_node, TREE_OPERAND (expr, 1),
2867 truthvalue_conversion (TREE_OPERAND (expr, 0)));
2869 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2872 /* Distribute the conversion into the arms of a COND_EXPR. */
2873 return fold (build (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
2874 truthvalue_conversion (TREE_OPERAND (expr, 1)),
2875 truthvalue_conversion (TREE_OPERAND (expr, 2))));
2878 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
2879 since that affects how `default_conversion' will behave. */
2880 if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE
2881 || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
2883 /* fall through... */
2885 /* If this is widening the argument, we can ignore it. */
2886 if (TYPE_PRECISION (TREE_TYPE (expr))
2887 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
2888 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2892 /* With IEEE arithmetic, x - x may not equal 0, so we can't optimize
2894 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
2895 && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE)
2897 /* fall through... */
2899 /* This and MINUS_EXPR can be changed into a comparison of the
2901 if (TREE_TYPE (TREE_OPERAND (expr, 0))
2902 == TREE_TYPE (TREE_OPERAND (expr, 1)))
2903 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2904 TREE_OPERAND (expr, 1), 1);
2905 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2906 fold (build1 (NOP_EXPR,
2907 TREE_TYPE (TREE_OPERAND (expr, 0)),
2908 TREE_OPERAND (expr, 1))), 1);
2911 if (integer_onep (TREE_OPERAND (expr, 1))
2912 && TREE_TYPE (expr) != boolean_type_node)
2913 /* Using convert here would cause infinite recursion. */
2914 return build1 (NOP_EXPR, boolean_type_node, expr);
2918 if (warn_parentheses && C_EXP_ORIGINAL_CODE (expr) == MODIFY_EXPR)
2919 warning ("suggest parentheses around assignment used as truth value");
2926 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
2928 tree tem = save_expr (expr);
2929 return (build_binary_op
2930 ((TREE_SIDE_EFFECTS (expr)
2931 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
2932 truthvalue_conversion (build_unary_op (REALPART_EXPR, tem, 0)),
2933 truthvalue_conversion (build_unary_op (IMAGPART_EXPR, tem, 0)),
2937 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
2941 /* Read the rest of a #-directive from input stream FINPUT.
2942 In normal use, the directive name and the white space after it
2943 have already been read, so they won't be included in the result.
2944 We allow for the fact that the directive line may contain
2945 a newline embedded within a character or string literal which forms
2946 a part of the directive.
2948 The value is a string in a reusable buffer. It remains valid
2949 only until the next time this function is called. */
2950 unsigned char *yy_cur, *yy_lim;
2952 #define GETC() (yy_cur < yy_lim ? *yy_cur++ : yy_get_token ())
2953 #define UNGETC(c) ((c) == EOF ? 0 : yy_cur--)
2960 parse_in.limit = parse_in.token_buffer;
2961 cpp_token = cpp_get_token (&parse_in);
2962 if (cpp_token == CPP_EOF)
2964 yy_lim = CPP_PWRITTEN (&parse_in);
2965 yy_cur = parse_in.token_buffer;
2966 if (yy_cur < yy_lim)
2972 get_directive_line ()
2974 static char *directive_buffer = NULL;
2975 static unsigned buffer_length = 0;
2977 register char *buffer_limit;
2978 register int looking_for = 0;
2979 register int char_escaped = 0;
2981 if (buffer_length == 0)
2983 directive_buffer = (char *)xmalloc (128);
2984 buffer_length = 128;
2987 buffer_limit = &directive_buffer[buffer_length];
2989 for (p = directive_buffer; ; )
2993 /* Make buffer bigger if it is full. */
2994 if (p >= buffer_limit)
2996 register unsigned bytes_used = (p - directive_buffer);
3000 = (char *)xrealloc (directive_buffer, buffer_length);
3001 p = &directive_buffer[bytes_used];
3002 buffer_limit = &directive_buffer[buffer_length];
3007 /* Discard initial whitespace. */
3008 if ((c == ' ' || c == '\t') && p == directive_buffer)
3011 /* Detect the end of the directive. */
3012 if (c == '\n' && looking_for == 0)
3021 return directive_buffer;
3023 /* Handle string and character constant syntax. */
3026 if (looking_for == c && !char_escaped)
3027 looking_for = 0; /* Found terminator... stop looking. */
3030 if (c == '\'' || c == '"')
3031 looking_for = c; /* Don't stop buffering until we see another
3032 another one of these (or an EOF). */
3034 /* Handle backslash. */
3035 char_escaped = (c == '\\' && ! char_escaped);
3039 /* Read the rest of a #-directive from input stream FINPUT.
3040 In normal use, the directive name and the white space after it
3041 have already been read, so they won't be included in the result.
3042 We allow for the fact that the directive line may contain
3043 a newline embedded within a character or string literal which forms
3044 a part of the directive.
3046 The value is a string in a reusable buffer. It remains valid
3047 only until the next time this function is called.
3049 The terminating character ('\n' or EOF) is left in FINPUT for the
3050 caller to re-read. */
3053 get_directive_line (finput)
3054 register FILE *finput;
3056 static char *directive_buffer = NULL;
3057 static unsigned buffer_length = 0;
3059 register char *buffer_limit;
3060 register int looking_for = 0;
3061 register int char_escaped = 0;
3063 if (buffer_length == 0)
3065 directive_buffer = (char *)xmalloc (128);
3066 buffer_length = 128;
3069 buffer_limit = &directive_buffer[buffer_length];
3071 for (p = directive_buffer; ; )
3075 /* Make buffer bigger if it is full. */
3076 if (p >= buffer_limit)
3078 register unsigned bytes_used = (p - directive_buffer);
3082 = (char *)xrealloc (directive_buffer, buffer_length);
3083 p = &directive_buffer[bytes_used];
3084 buffer_limit = &directive_buffer[buffer_length];
3089 /* Discard initial whitespace. */
3090 if ((c == ' ' || c == '\t') && p == directive_buffer)
3093 /* Detect the end of the directive. */
3094 if (looking_for == 0
3095 && (c == '\n' || c == EOF))
3104 return directive_buffer;
3106 /* Handle string and character constant syntax. */
3109 if (looking_for == c && !char_escaped)
3110 looking_for = 0; /* Found terminator... stop looking. */
3113 if (c == '\'' || c == '"')
3114 looking_for = c; /* Don't stop buffering until we see another
3115 one of these (or an EOF). */
3117 /* Handle backslash. */
3118 char_escaped = (c == '\\' && ! char_escaped);
3121 #endif /* !USE_CPPLIB */
3123 /* Make a variant type in the proper way for C/C++, propagating qualifiers
3124 down to the element type of an array. */
3127 c_build_qualified_type (type, type_quals)
3131 /* A restrict-qualified pointer type must be a pointer to object or
3132 incomplete type. Note that the use of POINTER_TYPE_P also allows
3133 REFERENCE_TYPEs, which is appropriate for C++. Unfortunately,
3134 the C++ front-end also use POINTER_TYPE for pointer-to-member
3135 values, so even though it should be illegal to use `restrict'
3136 with such an entity we don't flag that here. Thus, special case
3137 code for that case is required in the C++ front-end. */
3138 if ((type_quals & TYPE_QUAL_RESTRICT)
3139 && (!POINTER_TYPE_P (type)
3140 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
3142 error ("invalid use of `restrict'");
3143 type_quals &= ~TYPE_QUAL_RESTRICT;
3146 if (TREE_CODE (type) == ARRAY_TYPE)
3147 return build_array_type (c_build_qualified_type (TREE_TYPE (type),
3149 TYPE_DOMAIN (type));
3150 return build_qualified_type (type, type_quals);
3153 /* Apply the TYPE_QUALS to the new DECL. */
3156 c_apply_type_quals_to_decl (type_quals, decl)
3160 if (type_quals & TYPE_QUAL_CONST)
3161 TREE_READONLY (decl) = 1;
3162 if (type_quals & TYPE_QUAL_VOLATILE)
3164 TREE_SIDE_EFFECTS (decl) = 1;
3165 TREE_THIS_VOLATILE (decl) = 1;
3167 if (type_quals & TYPE_QUAL_RESTRICT)
3169 if (!TREE_TYPE (decl)
3170 || !POINTER_TYPE_P (TREE_TYPE (decl))
3171 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (TREE_TYPE (decl))))
3172 error ("invalid use of `restrict'");
3173 else if (flag_strict_aliasing)
3175 /* No two restricted pointers can point at the same thing.
3176 However, a restricted pointer can point at the same thing
3177 as an unrestricted pointer, if that unrestricted pointer
3178 is based on the restricted pointer. So, we make the
3179 alias set for the restricted pointer a subset of the
3180 alias set for the type pointed to by the type of the
3183 int pointed_to_alias_set
3184 = get_alias_set (TREE_TYPE (TREE_TYPE (decl)));
3186 if (!pointed_to_alias_set)
3187 /* It's not legal to make a subset of alias set zero. */
3191 DECL_POINTER_ALIAS_SET (decl) = new_alias_set ();
3192 record_alias_subset (pointed_to_alias_set,
3193 DECL_POINTER_ALIAS_SET (decl));
3199 /* T is an expression with pointer type. Find the DECL on which this
3200 expression is based. (For example, in `a[i]' this would be `a'.)
3201 If there is no such DECL, or a unique decl cannot be determined,
3202 NULL_TREE is retured. */
3205 c_find_base_decl (t)
3211 if (t == NULL_TREE || t == error_mark_node)
3214 if (!POINTER_TYPE_P (TREE_TYPE (t)))
3219 if (TREE_CODE (t) == FIELD_DECL
3220 || TREE_CODE (t) == PARM_DECL
3221 || TREE_CODE (t) == VAR_DECL)
3222 /* Aha, we found a pointer-typed declaration. */
3225 /* It would be nice to deal with COMPONENT_REFs here. If we could
3226 tell that `a' and `b' were the same, then `a->f' and `b->f' are
3229 /* Handle general expressions. */
3230 switch (TREE_CODE_CLASS (TREE_CODE (t)))
3235 for (i = tree_code_length [(int) TREE_CODE (t)]; --i >= 0;)
3237 tree d = c_find_base_decl (TREE_OPERAND (t, i));
3242 else if (d && d != decl)
3243 /* Two different declarations. That's confusing; let's
3244 just assume we don't know what's going on. */
3257 /* Return the typed-based alias set for T, which may be an expression
3267 if (t == error_mark_node)
3270 type = (TREE_CODE_CLASS (TREE_CODE (t)) == 't')
3271 ? t : TREE_TYPE (t);
3273 if (type == error_mark_node)
3276 /* Deal with special cases first; for certain kinds of references
3277 we're interested in more than just the type. */
3279 if (TREE_CODE (t) == BIT_FIELD_REF)
3280 /* Perhaps reads and writes to this piece of data alias fields
3281 neighboring the bitfield. Perhaps that's impossible. For now,
3282 let's just assume that bitfields can alias everything, which is
3283 the conservative assumption. */
3286 /* Permit type-punning when accessing a union, provided the access
3287 is directly through the union. For example, this code does not
3288 permit taking the address of a union member and then storing
3289 through it. Even the type-punning allowed here is a GCC
3290 extension, albeit a common and useful one; the C standard says
3291 that such accesses have implementation-defined behavior. */
3293 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
3294 u = TREE_OPERAND (u, 0))
3295 if (TREE_CODE (u) == COMPONENT_REF
3296 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
3299 if (TREE_CODE (t) == INDIRECT_REF)
3301 /* Check for accesses through restrict-qualified pointers. */
3302 tree decl = c_find_base_decl (TREE_OPERAND (t, 0));
3304 if (decl && DECL_POINTER_ALIAS_SET_KNOWN_P (decl))
3305 /* We use the alias set indicated in the declaration. */
3306 return DECL_POINTER_ALIAS_SET (decl);
3309 /* From here on, only the type matters. */
3311 if (TREE_CODE (t) == COMPONENT_REF
3312 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (t, 1)))
3313 /* Since build_modify_expr calls get_unwidened for stores to
3314 component references, the type of a bit field can be changed
3315 from (say) `unsigned int : 16' to `unsigned short' or from
3316 `enum E : 16' to `short'. We want the real type of the
3317 bit-field in this case, not some the integral equivalent. */
3318 type = DECL_BIT_FIELD_TYPE (TREE_OPERAND (t, 1));
3320 if (TYPE_ALIAS_SET_KNOWN_P (type))
3321 /* If we've already calculated the value, just return it. */
3322 return TYPE_ALIAS_SET (type);
3323 else if (TYPE_MAIN_VARIANT (type) != type)
3324 /* The C standard specifically allows aliasing between
3325 cv-qualified variants of types. */
3326 TYPE_ALIAS_SET (type) = c_get_alias_set (TYPE_MAIN_VARIANT (type));
3327 else if (TREE_CODE (type) == INTEGER_TYPE)
3329 tree signed_variant;
3331 /* The C standard specifically allows aliasing between signed and
3332 unsigned variants of the same type. We treat the signed
3333 variant as canonical. */
3334 signed_variant = signed_type (type);
3336 if (signed_variant != type)
3337 TYPE_ALIAS_SET (type) = c_get_alias_set (signed_variant);
3338 else if (signed_variant == signed_char_type_node)
3339 /* The C standard guarantess that any object may be accessed
3340 via an lvalue that has character type. We don't have to
3341 check for unsigned_char_type_node or char_type_node because
3342 we are specifically looking at the signed variant. */
3343 TYPE_ALIAS_SET (type) = 0;
3345 else if (TREE_CODE (type) == ARRAY_TYPE)
3346 /* Anything that can alias one of the array elements can alias
3347 the entire array as well. */
3348 TYPE_ALIAS_SET (type) = c_get_alias_set (TREE_TYPE (type));
3349 else if (TREE_CODE (type) == FUNCTION_TYPE)
3350 /* There are no objects of FUNCTION_TYPE, so there's no point in
3351 using up an alias set for them. (There are, of course,
3352 pointers and references to functions, but that's
3354 TYPE_ALIAS_SET (type) = 0;
3355 else if (TREE_CODE (type) == RECORD_TYPE
3356 || TREE_CODE (type) == UNION_TYPE)
3357 /* If TYPE is a struct or union type then we're reading or
3358 writing an entire struct. Thus, we don't know anything about
3359 aliasing. (In theory, such an access can only alias objects
3360 whose type is the same as one of the fields, recursively, but
3361 we don't yet make any use of that information.) */
3362 TYPE_ALIAS_SET (type) = 0;
3363 else if (TREE_CODE (type) == POINTER_TYPE
3364 || TREE_CODE (type) == REFERENCE_TYPE)
3368 /* Unfortunately, there is no canonical form of a pointer type.
3369 In particular, if we have `typedef int I', then `int *', and
3370 `I *' are different types. So, we have to pick a canonical
3371 representative. We do this below.
3373 Technically, this approach is actually more conservative that
3374 it needs to be. In particular, `const int *' and `int *'
3375 chould be in different alias sets, according to the C and C++
3376 standard, since their types are not the same, and so,
3377 technically, an `int **' and `const int **' cannot point at
3380 But, the standard is wrong. In particular, this code is
3385 const int* const* cipp = &ip;
3387 And, it doesn't make sense for that to be legal unless you
3388 can dereference IPP and CIPP. So, we ignore cv-qualifiers on
3389 the pointed-to types. This issue has been reported to the
3391 t = TYPE_MAIN_VARIANT (TREE_TYPE (type));
3392 t = ((TREE_CODE (type) == POINTER_TYPE)
3393 ? build_pointer_type (t) : build_reference_type (t));
3395 TYPE_ALIAS_SET (type) = c_get_alias_set (t);
3398 if (!TYPE_ALIAS_SET_KNOWN_P (type))
3400 /* Types that are not allocated on the permanent obstack are not
3401 placed in the type hash table. Thus, there can be multiple
3402 copies of identical types in local scopes. In the long run,
3403 all types should be permanent. */
3404 if (! TREE_PERMANENT (type))
3405 TYPE_ALIAS_SET (type) = 0;
3407 /* TYPE is something we haven't seen before. Put it in a new
3409 TYPE_ALIAS_SET (type) = new_alias_set ();
3412 return TYPE_ALIAS_SET (type);