1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000
3 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
35 #include "diagnostic.h"
42 #undef WCHAR_TYPE_SIZE
43 #define WCHAR_TYPE_SIZE TYPE_PRECISION (wchar_type_node)
45 /* The following symbols are subsumed in the c_global_trees array, and
46 listed here individually for documentation purposes.
48 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
50 tree short_integer_type_node;
51 tree long_integer_type_node;
52 tree long_long_integer_type_node;
54 tree short_unsigned_type_node;
55 tree long_unsigned_type_node;
56 tree long_long_unsigned_type_node;
58 tree boolean_type_node;
59 tree boolean_false_node;
60 tree boolean_true_node;
62 tree ptrdiff_type_node;
64 tree unsigned_char_type_node;
65 tree signed_char_type_node;
67 tree signed_wchar_type_node;
68 tree unsigned_wchar_type_node;
71 tree double_type_node;
72 tree long_double_type_node;
74 tree complex_integer_type_node;
75 tree complex_float_type_node;
76 tree complex_double_type_node;
77 tree complex_long_double_type_node;
85 tree unsigned_intQI_type_node;
86 tree unsigned_intHI_type_node;
87 tree unsigned_intSI_type_node;
88 tree unsigned_intDI_type_node;
89 tree unsigned_intTI_type_node;
91 tree widest_integer_literal_type_node;
92 tree widest_unsigned_literal_type_node;
94 Nodes for types `void *' and `const void *'.
96 tree ptr_type_node, const_ptr_type_node;
98 Nodes for types `char *' and `const char *'.
100 tree string_type_node, const_string_type_node;
102 Type `char[SOMENUMBER]'.
103 Used when an array of char is needed and the size is irrelevant.
105 tree char_array_type_node;
107 Type `int[SOMENUMBER]' or something like it.
108 Used when an array of int needed and the size is irrelevant.
110 tree int_array_type_node;
112 Type `wchar_t[SOMENUMBER]' or something like it.
113 Used when a wide string literal is created.
115 tree wchar_array_type_node;
117 Type `int ()' -- used for implicit declaration of functions.
119 tree default_function_type;
121 Function types `int (int)', etc.
127 tree ptr_ftype_sizetype;
129 A VOID_TYPE node, packaged in a TREE_LIST.
133 The identifiers __FUNCTION__, __PRETTY_FUNCTION__, and __func__.
135 tree function_id_node;
136 tree pretty_function_id_node;
141 tree c_global_trees[CTI_MAX];
143 /* The elements of `ridpointers' are identifier nodes for the reserved
144 type names and storage classes. It is indexed by a RID_... value. */
147 tree (*make_fname_decl) PARAMS ((tree, const char *, int));
149 /* If non-NULL, the address of a language-specific function that
150 returns 1 for language-specific statement codes. */
151 int (*lang_statement_code_p) PARAMS ((enum tree_code));
153 /* If non-NULL, the address of a language-specific function that takes
154 any action required right before expand_function_end is called. */
155 void (*lang_expand_function_end) PARAMS ((void));
157 /* Nonzero means the expression being parsed will never be evaluated.
158 This is a count, since unevaluated expressions can nest. */
161 enum attrs {A_PACKED, A_NOCOMMON, A_COMMON, A_NORETURN, A_CONST, A_T_UNION,
162 A_NO_CHECK_MEMORY_USAGE, A_NO_INSTRUMENT_FUNCTION,
163 A_CONSTRUCTOR, A_DESTRUCTOR, A_MODE, A_SECTION, A_ALIGNED,
164 A_UNUSED, A_FORMAT, A_FORMAT_ARG, A_WEAK, A_ALIAS, A_MALLOC,
165 A_NO_LIMIT_STACK, A_PURE};
167 enum format_type { printf_format_type, scanf_format_type,
168 strftime_format_type };
170 static void add_attribute PARAMS ((enum attrs, const char *,
172 static void init_attributes PARAMS ((void));
173 static void record_function_format PARAMS ((tree, tree, enum format_type,
175 static void record_international_format PARAMS ((tree, tree, int));
176 static int default_valid_lang_attribute PARAMS ((tree, tree, tree, tree));
178 /* Keep a stack of if statements. We record the number of compound
179 statements seen up to the if keyword, as well as the line number
180 and file of the if. If a potentially ambiguous else is seen, that
181 fact is recorded; the warning is issued when we can be sure that
182 the enclosing if statement does not have an else branch. */
192 static if_elt *if_stack;
194 /* Amount of space in the if statement stack. */
195 static int if_stack_space = 0;
198 static int if_stack_pointer = 0;
200 /* Record the start of an if-then, and record the start of it
201 for ambiguous else detection. */
204 c_expand_start_cond (cond, compstmt_count)
210 /* Make sure there is enough space on the stack. */
211 if (if_stack_space == 0)
214 if_stack = (if_elt *)xmalloc (10 * sizeof (if_elt));
216 else if (if_stack_space == if_stack_pointer)
218 if_stack_space += 10;
219 if_stack = (if_elt *)xrealloc (if_stack, if_stack_space * sizeof (if_elt));
222 if_stmt = build_stmt (IF_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
223 IF_COND (if_stmt) = cond;
226 /* Record this if statement. */
227 if_stack[if_stack_pointer].compstmt_count = compstmt_count;
228 if_stack[if_stack_pointer].file = input_filename;
229 if_stack[if_stack_pointer].line = lineno;
230 if_stack[if_stack_pointer].needs_warning = 0;
231 if_stack[if_stack_pointer].if_stmt = if_stmt;
235 /* Called after the then-clause for an if-statement is processed. */
240 tree if_stmt = if_stack[if_stack_pointer - 1].if_stmt;
241 RECHAIN_STMTS (if_stmt, THEN_CLAUSE (if_stmt));
244 /* Record the end of an if-then. Optionally warn if a nested
245 if statement had an ambiguous else clause. */
251 if (if_stack[if_stack_pointer].needs_warning)
252 warning_with_file_and_line (if_stack[if_stack_pointer].file,
253 if_stack[if_stack_pointer].line,
254 "suggest explicit braces to avoid ambiguous `else'");
255 last_expr_type = NULL_TREE;
258 /* Called between the then-clause and the else-clause
259 of an if-then-else. */
262 c_expand_start_else ()
264 /* An ambiguous else warning must be generated for the enclosing if
265 statement, unless we see an else branch for that one, too. */
267 && if_stack_pointer > 1
268 && (if_stack[if_stack_pointer - 1].compstmt_count
269 == if_stack[if_stack_pointer - 2].compstmt_count))
270 if_stack[if_stack_pointer - 2].needs_warning = 1;
272 /* Even if a nested if statement had an else branch, it can't be
273 ambiguous if this one also has an else. So don't warn in that
274 case. Also don't warn for any if statements nested in this else. */
275 if_stack[if_stack_pointer - 1].needs_warning = 0;
276 if_stack[if_stack_pointer - 1].compstmt_count--;
279 /* Called after the else-clause for an if-statement is processed. */
284 tree if_stmt = if_stack[if_stack_pointer - 1].if_stmt;
285 RECHAIN_STMTS (if_stmt, ELSE_CLAUSE (if_stmt));
288 /* Make bindings for __FUNCTION__, __PRETTY_FUNCTION__, and __func__. */
291 declare_function_name ()
293 const char *name, *printable_name;
295 if (current_function_decl == NULL)
298 printable_name = "top level";
302 /* Allow functions to be nameless (such as artificial ones). */
303 if (DECL_NAME (current_function_decl))
304 name = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
307 printable_name = (*decl_printable_name) (current_function_decl, 2);
309 /* ISO C99 defines __func__, which is a variable, not a string
310 constant, and which is not a defined symbol at file scope. */
311 (*make_fname_decl) (func_id_node, name, 0);
314 (*make_fname_decl) (function_id_node, name, 0);
315 (*make_fname_decl) (pretty_function_id_node, printable_name, 1);
318 /* Given a chain of STRING_CST nodes,
319 concatenate them into one STRING_CST
320 and give it a suitable array-of-chars data type. */
323 combine_strings (strings)
326 register tree value, t;
327 register int length = 1;
330 int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
332 const int nchars_max = flag_isoc99 ? 4095 : 509;
334 if (TREE_CHAIN (strings))
336 /* More than one in the chain, so concatenate. */
337 register char *p, *q;
339 /* Don't include the \0 at the end of each substring,
340 except for the last one.
341 Count wide strings and ordinary strings separately. */
342 for (t = strings; t; t = TREE_CHAIN (t))
344 if (TREE_TYPE (t) == wchar_array_type_node)
346 wide_length += (TREE_STRING_LENGTH (t) - wchar_bytes);
350 length += (TREE_STRING_LENGTH (t) - 1);
353 /* If anything is wide, the non-wides will be converted,
354 which makes them take more space. */
356 length = length * wchar_bytes + wide_length;
358 p = ggc_alloc_string (NULL, length);
360 /* Copy the individual strings into the new combined string.
361 If the combined string is wide, convert the chars to ints
362 for any individual strings that are not wide. */
365 for (t = strings; t; t = TREE_CHAIN (t))
367 int len = (TREE_STRING_LENGTH (t)
368 - ((TREE_TYPE (t) == wchar_array_type_node)
370 if ((TREE_TYPE (t) == wchar_array_type_node) == wide_flag)
372 memcpy (q, TREE_STRING_POINTER (t), len);
378 for (i = 0; i < len; i++)
380 if (WCHAR_TYPE_SIZE == HOST_BITS_PER_SHORT)
381 ((short *) q)[i] = TREE_STRING_POINTER (t)[i];
383 ((int *) q)[i] = TREE_STRING_POINTER (t)[i];
385 q += len * wchar_bytes;
391 for (i = 0; i < wchar_bytes; i++)
397 value = make_node (STRING_CST);
398 TREE_STRING_POINTER (value) = p;
399 TREE_STRING_LENGTH (value) = length;
404 length = TREE_STRING_LENGTH (value);
405 if (TREE_TYPE (value) == wchar_array_type_node)
409 /* Compute the number of elements, for the array type. */
410 nchars = wide_flag ? length / wchar_bytes : length;
412 if (pedantic && nchars > nchars_max)
413 pedwarn ("string length `%d' is greater than the minimum length `%d' ISO C%d is required to support",
414 nchars, nchars_max, flag_isoc99 ? 99 : 89);
416 /* Create the array type for the string constant.
417 -Wwrite-strings says make the string constant an array of const char
418 so that copying it to a non-const pointer will get a warning.
419 For C++, this is the standard behavior. */
420 if (flag_const_strings
421 && (! flag_traditional && ! flag_writable_strings))
424 = build_type_variant (wide_flag ? wchar_type_node : char_type_node,
427 = build_array_type (elements,
428 build_index_type (build_int_2 (nchars - 1, 0)));
432 = build_array_type (wide_flag ? wchar_type_node : char_type_node,
433 build_index_type (build_int_2 (nchars - 1, 0)));
435 TREE_CONSTANT (value) = 1;
436 TREE_READONLY (value) = ! flag_writable_strings;
437 TREE_STATIC (value) = 1;
441 /* To speed up processing of attributes, we maintain an array of
442 IDENTIFIER_NODES and the corresponding attribute types. */
444 /* Array to hold attribute information. */
446 static struct {enum attrs id; tree name; int min, max, decl_req;} attrtab[50];
448 static int attrtab_idx = 0;
450 /* Add an entry to the attribute table above. */
453 add_attribute (id, string, min_len, max_len, decl_req)
456 int min_len, max_len;
461 attrtab[attrtab_idx].id = id;
462 attrtab[attrtab_idx].name = get_identifier (string);
463 attrtab[attrtab_idx].min = min_len;
464 attrtab[attrtab_idx].max = max_len;
465 attrtab[attrtab_idx++].decl_req = decl_req;
467 sprintf (buf, "__%s__", string);
469 attrtab[attrtab_idx].id = id;
470 attrtab[attrtab_idx].name = get_identifier (buf);
471 attrtab[attrtab_idx].min = min_len;
472 attrtab[attrtab_idx].max = max_len;
473 attrtab[attrtab_idx++].decl_req = decl_req;
476 /* Initialize attribute table. */
481 add_attribute (A_PACKED, "packed", 0, 0, 0);
482 add_attribute (A_NOCOMMON, "nocommon", 0, 0, 1);
483 add_attribute (A_COMMON, "common", 0, 0, 1);
484 add_attribute (A_NORETURN, "noreturn", 0, 0, 1);
485 add_attribute (A_NORETURN, "volatile", 0, 0, 1);
486 add_attribute (A_UNUSED, "unused", 0, 0, 0);
487 add_attribute (A_CONST, "const", 0, 0, 1);
488 add_attribute (A_T_UNION, "transparent_union", 0, 0, 0);
489 add_attribute (A_CONSTRUCTOR, "constructor", 0, 0, 1);
490 add_attribute (A_DESTRUCTOR, "destructor", 0, 0, 1);
491 add_attribute (A_MODE, "mode", 1, 1, 1);
492 add_attribute (A_SECTION, "section", 1, 1, 1);
493 add_attribute (A_ALIGNED, "aligned", 0, 1, 0);
494 add_attribute (A_FORMAT, "format", 3, 3, 1);
495 add_attribute (A_FORMAT_ARG, "format_arg", 1, 1, 1);
496 add_attribute (A_WEAK, "weak", 0, 0, 1);
497 add_attribute (A_ALIAS, "alias", 1, 1, 1);
498 add_attribute (A_NO_INSTRUMENT_FUNCTION, "no_instrument_function", 0, 0, 1);
499 add_attribute (A_NO_CHECK_MEMORY_USAGE, "no_check_memory_usage", 0, 0, 1);
500 add_attribute (A_MALLOC, "malloc", 0, 0, 1);
501 add_attribute (A_NO_LIMIT_STACK, "no_stack_limit", 0, 0, 1);
502 add_attribute (A_PURE, "pure", 0, 0, 1);
505 /* Default implementation of valid_lang_attribute, below. By default, there
506 are no language-specific attributes. */
509 default_valid_lang_attribute (attr_name, attr_args, decl, type)
510 tree attr_name ATTRIBUTE_UNUSED;
511 tree attr_args ATTRIBUTE_UNUSED;
512 tree decl ATTRIBUTE_UNUSED;
513 tree type ATTRIBUTE_UNUSED;
518 /* Return a 1 if ATTR_NAME and ATTR_ARGS denote a valid language-specific
519 attribute for either declaration DECL or type TYPE and 0 otherwise. */
521 int (*valid_lang_attribute) PARAMS ((tree, tree, tree, tree))
522 = default_valid_lang_attribute;
524 /* Process the attributes listed in ATTRIBUTES and PREFIX_ATTRIBUTES
525 and install them in NODE, which is either a DECL (including a TYPE_DECL)
526 or a TYPE. PREFIX_ATTRIBUTES can appear after the declaration specifiers
527 and declaration modifiers but before the declaration proper. */
530 decl_attributes (node, attributes, prefix_attributes)
531 tree node, attributes, prefix_attributes;
533 tree decl = 0, type = 0;
537 if (attrtab_idx == 0)
543 type = TREE_TYPE (decl);
544 is_type = TREE_CODE (node) == TYPE_DECL;
546 else if (TYPE_P (node))
547 type = node, is_type = 1;
549 #ifdef PRAGMA_INSERT_ATTRIBUTES
550 /* If the code in c-pragma.c wants to insert some attributes then
551 allow it to do so. Do this before allowing machine back ends to
552 insert attributes, so that they have the opportunity to override
553 anything done here. */
554 PRAGMA_INSERT_ATTRIBUTES (node, & attributes, & prefix_attributes);
557 #ifdef INSERT_ATTRIBUTES
558 INSERT_ATTRIBUTES (node, & attributes, & prefix_attributes);
561 attributes = chainon (prefix_attributes, attributes);
563 for (a = attributes; a; a = TREE_CHAIN (a))
565 tree name = TREE_PURPOSE (a);
566 tree args = TREE_VALUE (a);
570 for (i = 0; i < attrtab_idx; i++)
571 if (attrtab[i].name == name)
574 if (i == attrtab_idx)
576 if (! valid_machine_attribute (name, args, decl, type)
577 && ! (* valid_lang_attribute) (name, args, decl, type))
578 warning ("`%s' attribute directive ignored",
579 IDENTIFIER_POINTER (name));
581 type = TREE_TYPE (decl);
584 else if (attrtab[i].decl_req && decl == 0)
586 warning ("`%s' attribute does not apply to types",
587 IDENTIFIER_POINTER (name));
590 else if (list_length (args) < attrtab[i].min
591 || list_length (args) > attrtab[i].max)
593 error ("wrong number of arguments specified for `%s' attribute",
594 IDENTIFIER_POINTER (name));
603 TYPE_PACKED (type) = 1;
604 else if (TREE_CODE (decl) == FIELD_DECL)
605 DECL_PACKED (decl) = 1;
606 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
607 used for DECL_REGISTER. It wouldn't mean anything anyway. */
609 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
613 if (TREE_CODE (decl) == VAR_DECL)
614 DECL_COMMON (decl) = 0;
616 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
620 if (TREE_CODE (decl) == VAR_DECL)
621 DECL_COMMON (decl) = 1;
623 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
627 if (TREE_CODE (decl) == FUNCTION_DECL)
628 TREE_THIS_VOLATILE (decl) = 1;
629 else if (TREE_CODE (type) == POINTER_TYPE
630 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
631 TREE_TYPE (decl) = type
633 (build_type_variant (TREE_TYPE (type),
634 TREE_READONLY (TREE_TYPE (type)), 1));
636 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
640 if (TREE_CODE (decl) == FUNCTION_DECL)
641 DECL_IS_MALLOC (decl) = 1;
642 /* ??? TODO: Support types. */
644 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
650 TREE_USED (decl) = 1;
652 TREE_USED (type) = 1;
653 else if (TREE_CODE (decl) == PARM_DECL
654 || TREE_CODE (decl) == VAR_DECL
655 || TREE_CODE (decl) == FUNCTION_DECL
656 || TREE_CODE (decl) == LABEL_DECL)
657 TREE_USED (decl) = 1;
659 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
663 if (TREE_CODE (decl) == FUNCTION_DECL)
664 TREE_READONLY (decl) = 1;
665 else if (TREE_CODE (type) == POINTER_TYPE
666 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
667 TREE_TYPE (decl) = type
669 (build_type_variant (TREE_TYPE (type), 1,
670 TREE_THIS_VOLATILE (TREE_TYPE (type))));
672 warning ( "`%s' attribute ignored", IDENTIFIER_POINTER (name));
676 if (TREE_CODE (decl) == FUNCTION_DECL)
677 DECL_IS_PURE (decl) = 1;
678 /* ??? TODO: Support types. */
680 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
686 && TREE_CODE (type) == UNION_TYPE
688 || (TYPE_FIELDS (type) != 0
689 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))))
690 TYPE_TRANSPARENT_UNION (type) = 1;
691 else if (decl != 0 && TREE_CODE (decl) == PARM_DECL
692 && TREE_CODE (type) == UNION_TYPE
693 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))
694 DECL_TRANSPARENT_UNION (decl) = 1;
696 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
700 if (TREE_CODE (decl) == FUNCTION_DECL
701 && TREE_CODE (type) == FUNCTION_TYPE
702 && decl_function_context (decl) == 0)
704 DECL_STATIC_CONSTRUCTOR (decl) = 1;
705 TREE_USED (decl) = 1;
708 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
712 if (TREE_CODE (decl) == FUNCTION_DECL
713 && TREE_CODE (type) == FUNCTION_TYPE
714 && decl_function_context (decl) == 0)
716 DECL_STATIC_DESTRUCTOR (decl) = 1;
717 TREE_USED (decl) = 1;
720 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
724 if (TREE_CODE (TREE_VALUE (args)) != IDENTIFIER_NODE)
725 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
729 const char *p = IDENTIFIER_POINTER (TREE_VALUE (args));
730 int len = strlen (p);
731 enum machine_mode mode = VOIDmode;
734 if (len > 4 && p[0] == '_' && p[1] == '_'
735 && p[len - 1] == '_' && p[len - 2] == '_')
737 char *newp = (char *) alloca (len - 1);
739 strcpy (newp, &p[2]);
740 newp[len - 4] = '\0';
744 /* Give this decl a type with the specified mode.
745 First check for the special modes. */
746 if (! strcmp (p, "byte"))
748 else if (!strcmp (p, "word"))
750 else if (! strcmp (p, "pointer"))
753 for (j = 0; j < NUM_MACHINE_MODES; j++)
754 if (!strcmp (p, GET_MODE_NAME (j)))
755 mode = (enum machine_mode) j;
757 if (mode == VOIDmode)
758 error ("unknown machine mode `%s'", p);
759 else if (0 == (typefm = type_for_mode (mode,
760 TREE_UNSIGNED (type))))
761 error ("no data type for mode `%s'", p);
764 TREE_TYPE (decl) = type = typefm;
765 DECL_SIZE (decl) = DECL_SIZE_UNIT (decl) = 0;
766 layout_decl (decl, 0);
772 #ifdef ASM_OUTPUT_SECTION_NAME
773 if ((TREE_CODE (decl) == FUNCTION_DECL
774 || TREE_CODE (decl) == VAR_DECL)
775 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
777 if (TREE_CODE (decl) == VAR_DECL
778 && current_function_decl != NULL_TREE
779 && ! TREE_STATIC (decl))
780 error_with_decl (decl,
781 "section attribute cannot be specified for local variables");
782 /* The decl may have already been given a section attribute from
783 a previous declaration. Ensure they match. */
784 else if (DECL_SECTION_NAME (decl) != NULL_TREE
785 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
786 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
787 error_with_decl (node,
788 "section of `%s' conflicts with previous declaration");
790 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
793 error_with_decl (node,
794 "section attribute not allowed for `%s'");
796 error_with_decl (node,
797 "section attributes are not supported for this target");
804 = (args ? TREE_VALUE (args)
805 : size_int (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
808 /* Strip any NOPs of any kind. */
809 while (TREE_CODE (align_expr) == NOP_EXPR
810 || TREE_CODE (align_expr) == CONVERT_EXPR
811 || TREE_CODE (align_expr) == NON_LVALUE_EXPR)
812 align_expr = TREE_OPERAND (align_expr, 0);
814 if (TREE_CODE (align_expr) != INTEGER_CST)
816 error ("requested alignment is not a constant");
820 if ((i = tree_log2 (align_expr)) == -1)
821 error ("requested alignment is not a power of 2");
822 else if (i > HOST_BITS_PER_INT - 2)
823 error ("requested alignment is too large");
826 /* If we have a TYPE_DECL, then copy the type, so that we
827 don't accidentally modify a builtin type. See pushdecl. */
828 if (decl && TREE_TYPE (decl) != error_mark_node
829 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
831 tree tt = TREE_TYPE (decl);
832 DECL_ORIGINAL_TYPE (decl) = tt;
833 tt = build_type_copy (tt);
834 TYPE_NAME (tt) = decl;
835 TREE_USED (tt) = TREE_USED (decl);
836 TREE_TYPE (decl) = tt;
840 TYPE_ALIGN (type) = (1 << i) * BITS_PER_UNIT;
841 TYPE_USER_ALIGN (type) = 1;
843 else if (TREE_CODE (decl) != VAR_DECL
844 && TREE_CODE (decl) != FIELD_DECL)
845 error_with_decl (decl,
846 "alignment may not be specified for `%s'");
849 DECL_ALIGN (decl) = (1 << i) * BITS_PER_UNIT;
850 DECL_USER_ALIGN (decl) = 1;
857 tree format_type_id = TREE_VALUE (args);
858 tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
859 tree first_arg_num_expr
860 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
861 unsigned HOST_WIDE_INT format_num, first_arg_num;
862 enum format_type format_type;
864 unsigned int arg_num;
866 if (TREE_CODE (decl) != FUNCTION_DECL)
868 error_with_decl (decl,
869 "argument format specified for non-function `%s'");
873 if (TREE_CODE (format_type_id) != IDENTIFIER_NODE)
875 error ("unrecognized format specifier");
880 const char *p = IDENTIFIER_POINTER (format_type_id);
882 if (!strcmp (p, "printf") || !strcmp (p, "__printf__"))
883 format_type = printf_format_type;
884 else if (!strcmp (p, "scanf") || !strcmp (p, "__scanf__"))
885 format_type = scanf_format_type;
886 else if (!strcmp (p, "strftime")
887 || !strcmp (p, "__strftime__"))
888 format_type = strftime_format_type;
891 warning ("`%s' is an unrecognized format function type", p);
896 /* Strip any conversions from the string index and first arg number
897 and verify they are constants. */
898 while (TREE_CODE (format_num_expr) == NOP_EXPR
899 || TREE_CODE (format_num_expr) == CONVERT_EXPR
900 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
901 format_num_expr = TREE_OPERAND (format_num_expr, 0);
903 while (TREE_CODE (first_arg_num_expr) == NOP_EXPR
904 || TREE_CODE (first_arg_num_expr) == CONVERT_EXPR
905 || TREE_CODE (first_arg_num_expr) == NON_LVALUE_EXPR)
906 first_arg_num_expr = TREE_OPERAND (first_arg_num_expr, 0);
908 if (TREE_CODE (format_num_expr) != INTEGER_CST
909 || TREE_INT_CST_HIGH (format_num_expr) != 0
910 || TREE_CODE (first_arg_num_expr) != INTEGER_CST
911 || TREE_INT_CST_HIGH (first_arg_num_expr) != 0)
913 error ("format string has invalid operand number");
917 format_num = TREE_INT_CST_LOW (format_num_expr);
918 first_arg_num = TREE_INT_CST_LOW (first_arg_num_expr);
919 if (first_arg_num != 0 && first_arg_num <= format_num)
921 error ("format string arg follows the args to be formatted");
925 /* If a parameter list is specified, verify that the format_num
926 argument is actually a string, in case the format attribute
928 argument = TYPE_ARG_TYPES (type);
931 for (arg_num = 1; argument != 0 && arg_num != format_num;
932 ++arg_num, argument = TREE_CHAIN (argument))
936 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
937 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
940 error ("format string arg not a string type");
944 else if (first_arg_num != 0)
946 /* Verify that first_arg_num points to the last arg,
949 arg_num++, argument = TREE_CHAIN (argument);
951 if (arg_num != first_arg_num)
953 error ("args to be formatted is not '...'");
959 record_function_format (DECL_NAME (decl),
960 DECL_ASSEMBLER_NAME (decl),
961 format_type, format_num, first_arg_num);
967 tree format_num_expr = TREE_VALUE (args);
968 unsigned HOST_WIDE_INT format_num;
969 unsigned int arg_num;
972 if (TREE_CODE (decl) != FUNCTION_DECL)
974 error_with_decl (decl,
975 "argument format specified for non-function `%s'");
979 /* Strip any conversions from the first arg number and verify it
981 while (TREE_CODE (format_num_expr) == NOP_EXPR
982 || TREE_CODE (format_num_expr) == CONVERT_EXPR
983 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
984 format_num_expr = TREE_OPERAND (format_num_expr, 0);
986 if (TREE_CODE (format_num_expr) != INTEGER_CST
987 || TREE_INT_CST_HIGH (format_num_expr) != 0)
989 error ("format string has invalid operand number");
993 format_num = TREE_INT_CST_LOW (format_num_expr);
995 /* If a parameter list is specified, verify that the format_num
996 argument is actually a string, in case the format attribute
998 argument = TYPE_ARG_TYPES (type);
1001 for (arg_num = 1; argument != 0 && arg_num != format_num;
1002 ++arg_num, argument = TREE_CHAIN (argument))
1006 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
1007 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
1010 error ("format string arg not a string type");
1015 if (TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) != POINTER_TYPE
1016 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_TYPE (decl))))
1019 error ("function does not return string type");
1023 record_international_format (DECL_NAME (decl),
1024 DECL_ASSEMBLER_NAME (decl),
1030 declare_weak (decl);
1034 if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
1035 || (TREE_CODE (decl) != FUNCTION_DECL && ! DECL_EXTERNAL (decl)))
1036 error_with_decl (decl,
1037 "`%s' defined both normally and as an alias");
1038 else if (decl_function_context (decl) == 0)
1042 id = TREE_VALUE (args);
1043 if (TREE_CODE (id) != STRING_CST)
1045 error ("alias arg not a string");
1048 id = get_identifier (TREE_STRING_POINTER (id));
1049 /* This counts as a use of the object pointed to. */
1052 if (TREE_CODE (decl) == FUNCTION_DECL)
1053 DECL_INITIAL (decl) = error_mark_node;
1055 DECL_EXTERNAL (decl) = 0;
1056 assemble_alias (decl, id);
1059 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
1062 case A_NO_CHECK_MEMORY_USAGE:
1063 if (TREE_CODE (decl) != FUNCTION_DECL)
1065 error_with_decl (decl,
1066 "`%s' attribute applies only to functions",
1067 IDENTIFIER_POINTER (name));
1069 else if (DECL_INITIAL (decl))
1071 error_with_decl (decl,
1072 "can't set `%s' attribute after definition",
1073 IDENTIFIER_POINTER (name));
1076 DECL_NO_CHECK_MEMORY_USAGE (decl) = 1;
1079 case A_NO_INSTRUMENT_FUNCTION:
1080 if (TREE_CODE (decl) != FUNCTION_DECL)
1082 error_with_decl (decl,
1083 "`%s' attribute applies only to functions",
1084 IDENTIFIER_POINTER (name));
1086 else if (DECL_INITIAL (decl))
1088 error_with_decl (decl,
1089 "can't set `%s' attribute after definition",
1090 IDENTIFIER_POINTER (name));
1093 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1096 case A_NO_LIMIT_STACK:
1097 if (TREE_CODE (decl) != FUNCTION_DECL)
1099 error_with_decl (decl,
1100 "`%s' attribute applies only to functions",
1101 IDENTIFIER_POINTER (name));
1103 else if (DECL_INITIAL (decl))
1105 error_with_decl (decl,
1106 "can't set `%s' attribute after definition",
1107 IDENTIFIER_POINTER (name));
1110 DECL_NO_LIMIT_STACK (decl) = 1;
1116 /* Split SPECS_ATTRS, a list of declspecs and prefix attributes, into two
1117 lists. SPECS_ATTRS may also be just a typespec (eg: RECORD_TYPE).
1119 The head of the declspec list is stored in DECLSPECS.
1120 The head of the attribute list is stored in PREFIX_ATTRIBUTES.
1122 Note that attributes in SPECS_ATTRS are stored in the TREE_PURPOSE of
1123 the list elements. We drop the containing TREE_LIST nodes and link the
1124 resulting attributes together the way decl_attributes expects them. */
1127 split_specs_attrs (specs_attrs, declspecs, prefix_attributes)
1129 tree *declspecs, *prefix_attributes;
1131 tree t, s, a, next, specs, attrs;
1133 /* This can happen after an __extension__ in pedantic mode. */
1134 if (specs_attrs != NULL_TREE
1135 && TREE_CODE (specs_attrs) == INTEGER_CST)
1137 *declspecs = NULL_TREE;
1138 *prefix_attributes = NULL_TREE;
1142 /* This can happen in c++ (eg: decl: typespec initdecls ';'). */
1143 if (specs_attrs != NULL_TREE
1144 && TREE_CODE (specs_attrs) != TREE_LIST)
1146 *declspecs = specs_attrs;
1147 *prefix_attributes = NULL_TREE;
1151 /* Remember to keep the lists in the same order, element-wise. */
1153 specs = s = NULL_TREE;
1154 attrs = a = NULL_TREE;
1155 for (t = specs_attrs; t; t = next)
1157 next = TREE_CHAIN (t);
1158 /* Declspecs have a non-NULL TREE_VALUE. */
1159 if (TREE_VALUE (t) != NULL_TREE)
1161 if (specs == NULL_TREE)
1171 if (attrs == NULL_TREE)
1172 attrs = a = TREE_PURPOSE (t);
1175 TREE_CHAIN (a) = TREE_PURPOSE (t);
1176 a = TREE_PURPOSE (t);
1178 /* More attrs can be linked here, move A to the end. */
1179 while (TREE_CHAIN (a) != NULL_TREE)
1184 /* Terminate the lists. */
1186 TREE_CHAIN (s) = NULL_TREE;
1188 TREE_CHAIN (a) = NULL_TREE;
1192 *prefix_attributes = attrs;
1195 /* Strip attributes from SPECS_ATTRS, a list of declspecs and attributes.
1196 This function is used by the parser when a rule will accept attributes
1197 in a particular position, but we don't want to support that just yet.
1199 A warning is issued for every ignored attribute. */
1202 strip_attrs (specs_attrs)
1207 split_specs_attrs (specs_attrs, &specs, &attrs);
1211 warning ("`%s' attribute ignored",
1212 IDENTIFIER_POINTER (TREE_PURPOSE (attrs)));
1213 attrs = TREE_CHAIN (attrs);
1219 /* Check a printf/fprintf/sprintf/scanf/fscanf/sscanf format against
1220 a parameter list. */
1222 /* The meaningfully distinct length modifiers for format checking recognised
1239 /* The standard versions in which various format features appeared. */
1240 enum format_std_version
1249 /* Structure describing a length modifier supported in format checking, and
1250 possibly a doubled version such as "hh". */
1253 /* Name of the single-character length modifier. */
1255 /* Index into a format_char_info.types array. */
1256 enum format_lengths index;
1257 /* Standard version this length appears in. */
1258 enum format_std_version std;
1259 /* Same, if the modifier can be repeated, or NULL if it can't. */
1260 const char *double_name;
1261 enum format_lengths double_index;
1262 enum format_std_version double_std;
1263 } format_length_info;
1266 /* Structure desribing the combination of a conversion specifier
1267 (or a set of specifiers which act identically) and a length modifier. */
1270 /* The standard version this combination of length and type appeared in.
1271 This is only relevant if greater than those for length and type
1272 individually; otherwise it is ignored. */
1273 enum format_std_version std;
1274 /* The name to use for the type, if different from that generated internally
1275 (e.g., "signed size_t"). */
1277 /* The type itself. */
1279 } format_type_detail;
1282 /* Macros to fill out tables of these. */
1283 #define BADLEN { 0, NULL, NULL }
1284 #define NOLENGTHS { BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }
1287 /* Structure desribing a format conversion specifier (or a set of specifiers
1288 which act identically), and the length modifiers used with it. */
1291 const char *format_chars;
1293 enum format_std_version std;
1294 /* Types accepted for each length modifier. */
1295 format_type_detail types[FMT_LEN_MAX];
1296 /* List of other modifier characters allowed with these options.
1297 This lists flags, and additionally "w" for width, "p" for precision,
1298 "c" for generic character pointers being allowed, "a" for scanf
1299 "a" allocation extension (not applicable in C99 mode), "*" for
1300 scanf suppression, "2" for strftime two digit year formats, "3"
1301 for strftime formats giving two digit years in some locales, "E"
1302 and "O" for those strftime modifiers, "o" if use of strftime "O"
1303 is a GNU extension beyond C99, and "W" if the argument is a
1304 pointer which is dereferenced and written into. */
1305 const char *flag_chars;
1309 /* Structure describing a particular kind of format processed by GCC. */
1312 /* The name of this kind of format, for use in diagnostics. */
1314 /* Specifications of the length modifiers accepted; possibly NULL. */
1315 const format_length_info *length_char_specs;
1316 /* Details of the conversion specification characters accepted. */
1317 const format_char_info *conversion_specs;
1321 /* Structure describing details of a type expected in format checking,
1322 and the type to check against it. */
1323 typedef struct format_wanted_type
1325 /* The type wanted. */
1327 /* The name of this type to use in diagnostics. */
1328 const char *wanted_type_name;
1329 /* The level of indirection through pointers at which this type occurs. */
1331 /* Whether, when pointer_count is 1, to allow any character type when
1332 pedantic, rather than just the character or void type specified. */
1333 int char_lenient_flag;
1334 /* Whether the argument, dereferenced once, is written into and so the
1335 argument must not be a pointer to a const-qualified type. */
1336 int writing_in_flag;
1337 /* If warnings should be of the form "field precision is not type int",
1338 the name to use (in this case "field precision"), otherwise NULL,
1339 for "%s format, %s arg" type messages. If (in an extension), this
1340 is a pointer type, wanted_type_name should be set to include the
1341 terminating '*' characters of the type name to give a correct
1344 /* The actual parameter to check against the wanted type. */
1346 /* The argument number of that parameter. */
1348 /* The next type to check for this format conversion, or NULL if none. */
1349 struct format_wanted_type *next;
1350 } format_wanted_type;
1353 static const format_length_info printf_length_specs[] =
1355 { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99 },
1356 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C99 },
1357 { "q", FMT_LEN_ll, STD_EXT, NULL, 0, 0 },
1358 { "L", FMT_LEN_L, STD_C89, NULL, 0, 0 },
1359 { "z", FMT_LEN_z, STD_C99, NULL, 0, 0 },
1360 { "Z", FMT_LEN_z, STD_EXT, NULL, 0, 0 },
1361 { "t", FMT_LEN_t, STD_C99, NULL, 0, 0 },
1362 { "j", FMT_LEN_j, STD_C99, NULL, 0, 0 },
1363 { NULL, 0, 0, NULL, 0, 0 }
1367 /* This differs from printf_length_specs only in that "Z" is not accepted. */
1368 static const format_length_info scanf_length_specs[] =
1370 { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99 },
1371 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C99 },
1372 { "q", FMT_LEN_ll, STD_EXT, NULL, 0, 0 },
1373 { "L", FMT_LEN_L, STD_C89, NULL, 0, 0 },
1374 { "z", FMT_LEN_z, STD_C99, NULL, 0, 0 },
1375 { "t", FMT_LEN_t, STD_C99, NULL, 0, 0 },
1376 { "j", FMT_LEN_j, STD_C99, NULL, 0, 0 },
1377 { NULL, 0, 0, NULL, 0, 0 }
1381 #define T_I &integer_type_node
1382 #define T89_I { STD_C89, NULL, T_I }
1383 #define T99_I { STD_C99, NULL, T_I }
1384 #define T_L &long_integer_type_node
1385 #define T89_L { STD_C89, NULL, T_L }
1386 #define T_LL &long_long_integer_type_node
1387 #define T99_LL { STD_C99, NULL, T_LL }
1388 #define TEX_LL { STD_EXT, NULL, T_LL }
1389 #define T_S &short_integer_type_node
1390 #define T89_S { STD_C89, NULL, T_S }
1391 #define T_UI &unsigned_type_node
1392 #define T89_UI { STD_C89, NULL, T_UI }
1393 #define T99_UI { STD_C99, NULL, T_UI }
1394 #define T_UL &long_unsigned_type_node
1395 #define T89_UL { STD_C89, NULL, T_UL }
1396 #define T_ULL &long_long_unsigned_type_node
1397 #define T99_ULL { STD_C99, NULL, T_ULL }
1398 #define TEX_ULL { STD_EXT, NULL, T_ULL }
1399 #define T_US &short_unsigned_type_node
1400 #define T89_US { STD_C89, NULL, T_US }
1401 #define T_F &float_type_node
1402 #define T89_F { STD_C89, NULL, T_F }
1403 #define T99_F { STD_C99, NULL, T_F }
1404 #define T_D &double_type_node
1405 #define T89_D { STD_C89, NULL, T_D }
1406 #define T99_D { STD_C99, NULL, T_D }
1407 #define T_LD &long_double_type_node
1408 #define T89_LD { STD_C89, NULL, T_LD }
1409 #define T99_LD { STD_C99, NULL, T_LD }
1410 #define T_C &char_type_node
1411 #define T89_C { STD_C89, NULL, T_C }
1412 #define T_SC &signed_char_type_node
1413 #define T99_SC { STD_C99, NULL, T_SC }
1414 #define T_UC &unsigned_char_type_node
1415 #define T99_UC { STD_C99, NULL, T_UC }
1416 #define T_V &void_type_node
1417 #define T89_V { STD_C89, NULL, T_V }
1418 #define T_W &wchar_type_node
1419 #define T94_W { STD_C94, "wchar_t", T_W }
1420 #define TEX_W { STD_EXT, "wchar_t", T_W }
1421 #define T_WI &wint_type_node
1422 #define T94_WI { STD_C94, "wint_t", T_WI }
1423 #define TEX_WI { STD_EXT, "wint_t", T_WI }
1424 #define T_ST &c_size_type_node
1425 #define T99_ST { STD_C99, "size_t", T_ST }
1426 #define T_SST &signed_size_type_node
1427 #define T99_SST { STD_C99, "signed size_t", T_SST }
1428 #define T_PD &ptrdiff_type_node
1429 #define T99_PD { STD_C99, "ptrdiff_t", T_PD }
1430 #define T_UPD &unsigned_ptrdiff_type_node
1431 #define T99_UPD { STD_C99, "unsigned ptrdiff_t", T_UPD }
1432 #define T_IM NULL /* intmax_t not yet implemented. */
1433 #define T99_IM { STD_C99, "intmax_t", T_IM }
1434 #define T_UIM NULL /* uintmax_t not yet implemented. */
1435 #define T99_UIM { STD_C99, "uintmax_t", T_UIM }
1437 static const format_char_info print_char_table[] =
1439 /* C89 conversion specifiers. */
1440 { "di", 0, STD_C89, { T89_I, T99_I, T89_I, T89_L, T99_LL, TEX_LL, T99_SST, T99_PD, T99_IM }, "-wp0 +'I" },
1441 { "oxX", 0, STD_C89, { T89_UI, T99_UI, T89_UI, T89_UL, T99_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM }, "-wp0#" },
1442 { "u", 0, STD_C89, { T89_UI, T99_UI, T89_UI, T89_UL, T99_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM }, "-wp0'I" },
1443 { "fgG", 0, STD_C89, { T89_D, BADLEN, BADLEN, T99_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN }, "-wp0 +#'" },
1444 { "eE", 0, STD_C89, { T89_D, BADLEN, BADLEN, T99_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN }, "-wp0 +#" },
1445 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, T94_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w" },
1446 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wpc" },
1447 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wc" },
1448 { "n", 1, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T99_LL, BADLEN, T99_SST, T99_PD, T99_IM }, "W" },
1449 /* C99 conversion specifiers. */
1450 { "F", 0, STD_C99, { T99_D, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN }, "-wp0 +#'" },
1451 { "aA", 0, STD_C99, { T99_D, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN }, "-wp0 +#" },
1452 /* X/Open conversion specifiers. */
1453 { "C", 0, STD_EXT, { TEX_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w" },
1454 { "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp" },
1455 /* GNU conversion specifiers. */
1456 { "m", 0, STD_EXT, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp" },
1457 { NULL, 0, 0, NOLENGTHS, NULL }
1460 static const format_char_info scan_char_table[] =
1462 /* C89 conversion specifiers. */
1463 { "di", 1, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T99_LL, TEX_LL, T99_SST, T99_PD, T99_IM }, "*wW" },
1464 { "ouxX", 1, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T99_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM }, "*wW" },
1465 { "efgEG", 1, STD_C89, { T89_F, BADLEN, BADLEN, T89_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN }, "*wW" },
1466 { "c", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*cwW" },
1467 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*acwW" },
1468 { "[", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*acwW" },
1469 { "p", 2, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*wW" },
1470 { "n", 1, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T99_LL, BADLEN, T99_SST, T99_PD, T99_IM }, "W" },
1471 /* C99 conversion specifiers. */
1472 { "FaA", 1, STD_C99, { T99_F, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN }, "*wW" },
1473 /* X/Open conversion specifiers. */
1474 { "C", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*wW" },
1475 { "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*awW" },
1476 { NULL, 0, 0, NOLENGTHS, NULL }
1479 static format_char_info time_char_table[] =
1481 /* C89 conversion specifiers. */
1482 { "ABZab", 0, STD_C89, NOLENGTHS, "^#" },
1483 { "cx", 0, STD_C89, NOLENGTHS, "3E" },
1484 { "HIMSUWdmw", 0, STD_C89, NOLENGTHS, "-_0Ow" },
1485 { "j", 0, STD_C89, NOLENGTHS, "-_0Oow" },
1486 { "p", 0, STD_C89, NOLENGTHS, "#" },
1487 { "X", 0, STD_C89, NOLENGTHS, "E" },
1488 { "y", 0, STD_C89, NOLENGTHS, "2EO-_0w" },
1489 { "Y", 0, STD_C89, NOLENGTHS, "-_0EOow" },
1490 { "%", 0, STD_C89, NOLENGTHS, "" },
1491 /* C99 conversion specifiers. */
1492 { "C", 0, STD_C99, NOLENGTHS, "-_0EOow" },
1493 { "D", 0, STD_C99, NOLENGTHS, "2" },
1494 { "eVu", 0, STD_C99, NOLENGTHS, "-_0Ow" },
1495 { "FRTnrt", 0, STD_C99, NOLENGTHS, "" },
1496 { "g", 0, STD_C99, NOLENGTHS, "2Oo-_0w" },
1497 { "G", 0, STD_C99, NOLENGTHS, "-_0Oow" },
1498 { "h", 0, STD_C99, NOLENGTHS, "^#" },
1499 { "z", 0, STD_C99, NOLENGTHS, "Oo" },
1500 /* GNU conversion specifiers. */
1501 { "kls", 0, STD_EXT, NOLENGTHS, "-_0Ow" },
1502 { "P", 0, STD_EXT, NOLENGTHS, "" },
1503 { NULL, 0, 0, NOLENGTHS, NULL }
1507 /* This must be in the same order as enum format_type. */
1508 static const format_kind_info format_types[] =
1510 { "printf", printf_length_specs, print_char_table },
1511 { "scanf", scanf_length_specs, scan_char_table },
1512 { "strftime", NULL, time_char_table }
1516 typedef struct function_format_info
1518 struct function_format_info *next; /* next structure on the list */
1519 tree name; /* identifier such as "printf" */
1520 tree assembler_name; /* optional mangled identifier (for C++) */
1521 enum format_type format_type; /* type of format (printf, scanf, etc.) */
1522 int format_num; /* number of format argument */
1523 int first_arg_num; /* number of first arg (zero for varargs) */
1524 } function_format_info;
1526 static function_format_info *function_format_list = NULL;
1528 typedef struct international_format_info
1530 struct international_format_info *next; /* next structure on the list */
1531 tree name; /* identifier such as "gettext" */
1532 tree assembler_name; /* optional mangled identifier (for C++) */
1533 int format_num; /* number of format argument */
1534 } international_format_info;
1536 static international_format_info *international_format_list = NULL;
1538 static void check_format_info PARAMS ((int *, function_format_info *, tree));
1539 static void status_warning PARAMS ((int *, const char *, ...))
1542 static void init_dollar_format_checking PARAMS ((int, tree));
1543 static int maybe_read_dollar_number PARAMS ((int *, const char **, int,
1545 static void finish_dollar_format_checking PARAMS ((int *));
1547 static void check_format_types PARAMS ((int *, format_wanted_type *));
1549 /* Initialize the table of functions to perform format checking on.
1550 The ISO C functions are always checked (whether <stdio.h> is
1551 included or not), since it is common to call printf without
1552 including <stdio.h>. There shouldn't be a problem with this,
1553 since ISO C reserves these function names whether you include the
1554 header file or not. In any case, the checking is harmless. With
1555 -ffreestanding, these default attributes are disabled, and must be
1556 specified manually if desired.
1558 Also initialize the name of function that modify the format string for
1559 internationalization purposes. */
1562 init_function_format_info ()
1566 /* Functions from ISO/IEC 9899:1990. */
1567 record_function_format (get_identifier ("printf"), NULL_TREE,
1568 printf_format_type, 1, 2);
1569 record_function_format (get_identifier ("fprintf"), NULL_TREE,
1570 printf_format_type, 2, 3);
1571 record_function_format (get_identifier ("sprintf"), NULL_TREE,
1572 printf_format_type, 2, 3);
1573 record_function_format (get_identifier ("scanf"), NULL_TREE,
1574 scanf_format_type, 1, 2);
1575 record_function_format (get_identifier ("fscanf"), NULL_TREE,
1576 scanf_format_type, 2, 3);
1577 record_function_format (get_identifier ("sscanf"), NULL_TREE,
1578 scanf_format_type, 2, 3);
1579 record_function_format (get_identifier ("vprintf"), NULL_TREE,
1580 printf_format_type, 1, 0);
1581 record_function_format (get_identifier ("vfprintf"), NULL_TREE,
1582 printf_format_type, 2, 0);
1583 record_function_format (get_identifier ("vsprintf"), NULL_TREE,
1584 printf_format_type, 2, 0);
1585 record_function_format (get_identifier ("strftime"), NULL_TREE,
1586 strftime_format_type, 3, 0);
1589 if (flag_hosted && flag_isoc99)
1591 /* ISO C99 adds the snprintf and vscanf family functions. */
1592 record_function_format (get_identifier ("snprintf"), NULL_TREE,
1593 printf_format_type, 3, 4);
1594 record_function_format (get_identifier ("vsnprintf"), NULL_TREE,
1595 printf_format_type, 3, 0);
1596 record_function_format (get_identifier ("vscanf"), NULL_TREE,
1597 scanf_format_type, 1, 0);
1598 record_function_format (get_identifier ("vfscanf"), NULL_TREE,
1599 scanf_format_type, 2, 0);
1600 record_function_format (get_identifier ("vsscanf"), NULL_TREE,
1601 scanf_format_type, 2, 0);
1604 if (flag_hosted && flag_noniso_default_format_attributes)
1606 /* Uniforum/GNU gettext functions, not in ISO C. */
1607 record_international_format (get_identifier ("gettext"), NULL_TREE, 1);
1608 record_international_format (get_identifier ("dgettext"), NULL_TREE, 2);
1609 record_international_format (get_identifier ("dcgettext"), NULL_TREE, 2);
1613 /* Record information for argument format checking. FUNCTION_IDENT is
1614 the identifier node for the name of the function to check (its decl
1615 need not exist yet).
1616 FORMAT_TYPE specifies the type of format checking. FORMAT_NUM is the number
1617 of the argument which is the format control string (starting from 1).
1618 FIRST_ARG_NUM is the number of the first actual argument to check
1619 against the format string, or zero if no checking is not be done
1620 (e.g. for varargs such as vfprintf). */
1623 record_function_format (name, assembler_name, format_type,
1624 format_num, first_arg_num)
1626 tree assembler_name;
1627 enum format_type format_type;
1631 function_format_info *info;
1633 /* Re-use existing structure if it's there. */
1635 for (info = function_format_list; info; info = info->next)
1637 if (info->name == name && info->assembler_name == assembler_name)
1642 info = (function_format_info *) xmalloc (sizeof (function_format_info));
1643 info->next = function_format_list;
1644 function_format_list = info;
1647 info->assembler_name = assembler_name;
1650 info->format_type = format_type;
1651 info->format_num = format_num;
1652 info->first_arg_num = first_arg_num;
1655 /* Record information for the names of function that modify the format
1656 argument to format functions. FUNCTION_IDENT is the identifier node for
1657 the name of the function (its decl need not exist yet) and FORMAT_NUM is
1658 the number of the argument which is the format control string (starting
1662 record_international_format (name, assembler_name, format_num)
1664 tree assembler_name;
1667 international_format_info *info;
1669 /* Re-use existing structure if it's there. */
1671 for (info = international_format_list; info; info = info->next)
1673 if (info->name == name && info->assembler_name == assembler_name)
1680 = (international_format_info *)
1681 xmalloc (sizeof (international_format_info));
1682 info->next = international_format_list;
1683 international_format_list = info;
1686 info->assembler_name = assembler_name;
1689 info->format_num = format_num;
1692 /* Check the argument list of a call to printf, scanf, etc.
1693 NAME is the function identifier.
1694 ASSEMBLER_NAME is the function's assembler identifier.
1695 (Either NAME or ASSEMBLER_NAME, but not both, may be NULL_TREE.)
1696 PARAMS is the list of argument values. */
1699 check_function_format (status, name, assembler_name, params)
1702 tree assembler_name;
1705 function_format_info *info;
1707 /* See if this function is a format function. */
1708 for (info = function_format_list; info; info = info->next)
1710 if (info->assembler_name
1711 ? (info->assembler_name == assembler_name)
1712 : (info->name == name))
1714 /* Yup; check it. */
1715 check_format_info (status, info, params);
1721 /* This function replaces `warning' inside the printf format checking
1722 functions. If the `status' parameter is non-NULL, then it is
1723 dereferenced and set to 1 whenever a warning is caught. Otherwise
1724 it warns as usual by replicating the innards of the warning
1725 function from diagnostic.c. */
1727 status_warning VPARAMS ((int *status, const char *msgid, ...))
1729 #ifndef ANSI_PROTOTYPES
1734 diagnostic_context dc;
1740 VA_START (ap, msgid);
1742 #ifndef ANSI_PROTOTYPES
1743 status = va_arg (ap, int *);
1744 msgid = va_arg (ap, const char *);
1747 /* This duplicates the warning function behavior. */
1748 set_diagnostic_context
1749 (&dc, msgid, &ap, input_filename, lineno, /* warn = */ 1);
1750 report_diagnostic (&dc);
1756 /* Variables used by the checking of $ operand number formats. */
1757 static char *dollar_arguments_used = NULL;
1758 static int dollar_arguments_alloc = 0;
1759 static int dollar_arguments_count;
1760 static int dollar_first_arg_num;
1761 static int dollar_max_arg_used;
1762 static int dollar_format_warned;
1764 /* Initialize the checking for a format string that may contain $
1765 parameter number specifications; we will need to keep track of whether
1766 each parameter has been used. FIRST_ARG_NUM is the number of the first
1767 argument that is a parameter to the format, or 0 for a vprintf-style
1768 function; PARAMS is the list of arguments starting at this argument. */
1771 init_dollar_format_checking (first_arg_num, params)
1775 dollar_first_arg_num = first_arg_num;
1776 dollar_arguments_count = 0;
1777 dollar_max_arg_used = 0;
1778 dollar_format_warned = 0;
1779 if (first_arg_num > 0)
1783 dollar_arguments_count++;
1784 params = TREE_CHAIN (params);
1787 if (dollar_arguments_alloc < dollar_arguments_count)
1789 if (dollar_arguments_used)
1790 free (dollar_arguments_used);
1791 dollar_arguments_alloc = dollar_arguments_count;
1792 dollar_arguments_used = xmalloc (dollar_arguments_alloc);
1794 if (dollar_arguments_alloc)
1795 memset (dollar_arguments_used, 0, dollar_arguments_alloc);
1799 /* Look for a decimal number followed by a $ in *FORMAT. If DOLLAR_NEEDED
1800 is set, it is an error if one is not found; otherwise, it is OK. If
1801 such a number is found, check whether it is within range and mark that
1802 numbered operand as being used for later checking. Returns the operand
1803 number if found and within range, zero if no such number was found and
1804 this is OK, or -1 on error. PARAMS points to the first operand of the
1805 format; PARAM_PTR is made to point to the parameter referred to. If
1806 a $ format is found, *FORMAT is updated to point just after it. */
1809 maybe_read_dollar_number (status, format, dollar_needed, params, param_ptr)
1811 const char **format;
1818 const char *fcp = *format;
1819 if (*fcp < '0' || *fcp > '9')
1823 status_warning (status, "missing $ operand number in format");
1831 while (*fcp >= '0' && *fcp <= '9')
1834 nargnum = 10 * argnum + (*fcp - '0');
1835 if (nargnum < 0 || nargnum / 10 != argnum)
1844 status_warning (status, "missing $ operand number in format");
1851 if (pedantic && !dollar_format_warned)
1853 status_warning (status, "ISO C does not support %%n$ operand number formats");
1854 dollar_format_warned = 1;
1856 if (overflow_flag || argnum == 0
1857 || (dollar_first_arg_num && argnum > dollar_arguments_count))
1859 status_warning (status, "operand number out of range in format");
1862 if (argnum > dollar_max_arg_used)
1863 dollar_max_arg_used = argnum;
1864 /* For vprintf-style functions we may need to allocate more memory to
1865 track which arguments are used. */
1866 while (dollar_arguments_alloc < dollar_max_arg_used)
1869 nalloc = 2 * dollar_arguments_alloc + 16;
1870 dollar_arguments_used = xrealloc (dollar_arguments_used, nalloc);
1871 memset (dollar_arguments_used + dollar_arguments_alloc, 0,
1872 nalloc - dollar_arguments_alloc);
1873 dollar_arguments_alloc = nalloc;
1875 dollar_arguments_used[argnum - 1] = 1;
1876 if (dollar_first_arg_num)
1879 *param_ptr = params;
1880 for (i = 1; i < argnum && *param_ptr != 0; i++)
1881 *param_ptr = TREE_CHAIN (*param_ptr);
1883 if (*param_ptr == 0)
1885 /* This case shouldn't be caught here. */
1895 /* Finish the checking for a format string that used $ operand number formats
1896 instead of non-$ formats. We check for unused operands before used ones
1897 (a serious error, since the implementation of the format function
1898 can't know what types to pass to va_arg to find the later arguments).
1899 and for unused operands at the end of the format (if we know how many
1900 arguments the format had, so not for vprintf). If there were operand
1901 numbers out of range on a non-vprintf-style format, we won't have reached
1905 finish_dollar_format_checking (status)
1909 for (i = 0; i < dollar_max_arg_used; i++)
1911 if (!dollar_arguments_used[i])
1912 status_warning (status, "format argument %d unused before used argument %d in $-style format",
1913 i + 1, dollar_max_arg_used);
1915 if (dollar_first_arg_num && dollar_max_arg_used < dollar_arguments_count)
1916 status_warning (status, "unused arguments in $-style format");
1920 /* Check the argument list of a call to printf, scanf, etc.
1921 INFO points to the function_format_info structure.
1922 PARAMS is the list of argument values. */
1925 check_format_info (status, info, params)
1927 function_format_info *info;
1932 int suppressed, wide, precise;
1933 const char *length_chars = NULL;
1934 enum format_lengths length_chars_val = FMT_LEN_none;
1935 enum format_std_version length_chars_std = STD_C89;
1942 tree main_arg_params;
1943 enum format_std_version wanted_type_std;
1944 const char *wanted_type_name;
1945 format_wanted_type width_wanted_type;
1946 format_wanted_type precision_wanted_type;
1947 format_wanted_type main_wanted_type;
1948 format_wanted_type *first_wanted_type;
1949 format_wanted_type *last_wanted_type;
1950 tree first_fillin_param;
1951 const char *format_chars;
1952 const format_kind_info *fki = NULL;
1953 const format_length_info *fli = NULL;
1954 const format_char_info *fci = NULL;
1956 /* -1 if no conversions taking an operand have been found; 0 if one has
1957 and it didn't use $; 1 if $ formats are in use. */
1958 int has_operand_number = -1;
1960 /* Skip to format argument. If the argument isn't available, there's
1961 no work for us to do; prototype checking will catch the problem. */
1962 for (arg_num = 1; ; ++arg_num)
1966 if (arg_num == info->format_num)
1968 params = TREE_CHAIN (params);
1970 format_tree = TREE_VALUE (params);
1971 params = TREE_CHAIN (params);
1972 if (format_tree == 0)
1975 /* We can only check the format if it's a string constant. */
1976 while (TREE_CODE (format_tree) == NOP_EXPR)
1977 format_tree = TREE_OPERAND (format_tree, 0); /* strip coercion */
1979 if (TREE_CODE (format_tree) == CALL_EXPR
1980 && TREE_CODE (TREE_OPERAND (format_tree, 0)) == ADDR_EXPR
1981 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0))
1984 tree function = TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0);
1986 /* See if this is a call to a known internationalization function
1987 that modifies the format arg. */
1988 international_format_info *info;
1990 for (info = international_format_list; info; info = info->next)
1991 if (info->assembler_name
1992 ? (info->assembler_name == DECL_ASSEMBLER_NAME (function))
1993 : (info->name == DECL_NAME (function)))
1998 for (inner_args = TREE_OPERAND (format_tree, 1), i = 1;
2000 inner_args = TREE_CHAIN (inner_args), i++)
2001 if (i == info->format_num)
2003 format_tree = TREE_VALUE (inner_args);
2005 while (TREE_CODE (format_tree) == NOP_EXPR)
2006 format_tree = TREE_OPERAND (format_tree, 0);
2011 if (integer_zerop (format_tree))
2013 status_warning (status, "null format string");
2016 if (TREE_CODE (format_tree) != ADDR_EXPR)
2018 /* The user may get multiple warnings if the supplied argument
2019 isn't even a string pointer. */
2020 /* Functions taking a va_list normally pass a non-literal format
2021 string. These functions typically are declared with
2022 first_arg_num == 0, so avoid warning in those cases. */
2023 if (info->first_arg_num != 0 && warn_format > 1)
2024 status_warning (status, "format not a string literal, argument types not checked");
2027 format_tree = TREE_OPERAND (format_tree, 0);
2028 if (TREE_CODE (format_tree) != STRING_CST)
2030 /* The user may get multiple warnings if the supplied argument
2031 isn't even a string pointer. */
2032 /* Functions taking a va_list normally pass a non-literal format
2033 string. These functions typically are declared with
2034 first_arg_num == 0, so avoid warning in those cases. */
2035 if (info->first_arg_num != 0 && warn_format > 1)
2036 status_warning (status, "format not a string literal, argument types not checked");
2039 format_chars = TREE_STRING_POINTER (format_tree);
2040 format_length = TREE_STRING_LENGTH (format_tree);
2041 if (format_length <= 1)
2042 status_warning (status, "zero-length format string");
2043 if (format_chars[--format_length] != 0)
2045 status_warning (status, "unterminated format string");
2048 /* Skip to first argument to check. */
2049 while (arg_num + 1 < info->first_arg_num)
2053 params = TREE_CHAIN (params);
2057 first_fillin_param = params;
2058 init_dollar_format_checking (info->first_arg_num, first_fillin_param);
2059 fki = &format_types[info->format_type];
2063 first_wanted_type = NULL;
2064 last_wanted_type = NULL;
2065 if (*format_chars == 0)
2067 if (format_chars - TREE_STRING_POINTER (format_tree) != format_length)
2068 status_warning (status, "embedded `\\0' in format");
2069 if (info->first_arg_num != 0 && params != 0
2070 && has_operand_number <= 0)
2071 status_warning (status, "too many arguments for format");
2072 if (has_operand_number > 0)
2073 finish_dollar_format_checking (status);
2076 if (*format_chars++ != '%')
2078 if (*format_chars == 0)
2080 status_warning (status, "spurious trailing `%%' in format");
2083 if (*format_chars == '%')
2089 suppressed = wide = precise = FALSE;
2091 main_arg_params = 0;
2092 if (info->format_type == scanf_format_type)
2094 int non_zero_width_char = FALSE;
2095 suppressed = *format_chars == '*';
2098 else if (has_operand_number != 0)
2101 opnum = maybe_read_dollar_number (status, &format_chars,
2102 has_operand_number == 1,
2109 has_operand_number = 1;
2110 main_arg_num = opnum + info->first_arg_num - 1;
2113 has_operand_number = 0;
2115 while (ISDIGIT (*format_chars))
2118 if (*format_chars != '0')
2119 non_zero_width_char = TRUE;
2122 if (wide && !non_zero_width_char)
2123 status_warning (status, "zero width in scanf format");
2125 else if (info->format_type == strftime_format_type)
2127 while (*format_chars != 0 && index ("_-0^#", *format_chars) != 0)
2130 status_warning (status, "ISO C does not support the strftime `%c' flag",
2132 if (index (flag_chars, *format_chars) != 0)
2134 status_warning (status, "repeated `%c' flag in format",
2140 i = strlen (flag_chars);
2141 flag_chars[i++] = *format_chars++;
2145 while (ISDIGIT ((unsigned char) *format_chars))
2150 if (wide && pedantic)
2151 status_warning (status, "ISO C does not support strftime format width");
2152 if (*format_chars == 'E' || *format_chars == 'O')
2154 i = strlen (flag_chars);
2155 flag_chars[i++] = *format_chars++;
2157 if (*format_chars == 'E' || *format_chars == 'O')
2159 status_warning (status, "multiple E/O modifiers in format");
2160 while (*format_chars == 'E' || *format_chars == 'O')
2165 else if (info->format_type == printf_format_type)
2167 if (has_operand_number != 0)
2170 opnum = maybe_read_dollar_number (status, &format_chars,
2171 0, first_fillin_param,
2177 has_operand_number = 1;
2178 main_arg_num = opnum + info->first_arg_num - 1;
2182 while (*format_chars != 0 && index (" +#0-'I", *format_chars) != 0)
2184 if (index (flag_chars, *format_chars) != 0)
2185 status_warning (status, "repeated `%c' flag in format", *format_chars++);
2188 i = strlen (flag_chars);
2189 flag_chars[i++] = *format_chars++;
2193 /* "If the space and + flags both appear,
2194 the space flag will be ignored." */
2195 if (index (flag_chars, ' ') != 0
2196 && index (flag_chars, '+') != 0)
2197 status_warning (status, "use of both ` ' and `+' flags in format");
2198 /* "If the 0 and - flags both appear,
2199 the 0 flag will be ignored." */
2200 if (index (flag_chars, '0') != 0
2201 && index (flag_chars, '-') != 0)
2202 status_warning (status, "use of both `0' and `-' flags in format");
2203 if (index (flag_chars, '\'') && pedantic)
2204 status_warning (status, "ISO C does not support the `'' format flag");
2205 if (index (flag_chars, 'I') && pedantic)
2206 status_warning (status, "ISO C does not support the `I' format flag");
2207 if (*format_chars == '*')
2210 /* "...a field width...may be indicated by an asterisk.
2211 In this case, an int argument supplies the field width..." */
2215 status_warning (status, "too few arguments for format");
2218 if (has_operand_number != 0)
2221 opnum = maybe_read_dollar_number (status, &format_chars,
2222 has_operand_number == 1,
2229 has_operand_number = 1;
2230 arg_num = opnum + info->first_arg_num - 1;
2233 has_operand_number = 0;
2235 if (info->first_arg_num != 0)
2237 cur_param = TREE_VALUE (params);
2238 if (has_operand_number <= 0)
2240 params = TREE_CHAIN (params);
2243 width_wanted_type.wanted_type = integer_type_node;
2244 width_wanted_type.wanted_type_name = NULL;
2245 width_wanted_type.pointer_count = 0;
2246 width_wanted_type.char_lenient_flag = 0;
2247 width_wanted_type.writing_in_flag = 0;
2248 width_wanted_type.name = _("field width");
2249 width_wanted_type.param = cur_param;
2250 width_wanted_type.arg_num = arg_num;
2251 width_wanted_type.next = NULL;
2252 if (last_wanted_type != 0)
2253 last_wanted_type->next = &width_wanted_type;
2254 if (first_wanted_type == 0)
2255 first_wanted_type = &width_wanted_type;
2256 last_wanted_type = &width_wanted_type;
2261 while (ISDIGIT (*format_chars))
2267 if (*format_chars == '.')
2271 /* "...a...precision...may be indicated by an asterisk.
2272 In this case, an int argument supplies the...precision." */
2273 if (*format_chars == '*')
2276 if (has_operand_number != 0)
2279 opnum = maybe_read_dollar_number (status, &format_chars,
2280 has_operand_number == 1,
2287 has_operand_number = 1;
2288 arg_num = opnum + info->first_arg_num - 1;
2291 has_operand_number = 0;
2293 if (info->first_arg_num != 0)
2297 status_warning (status, "too few arguments for format");
2300 cur_param = TREE_VALUE (params);
2301 if (has_operand_number <= 0)
2303 params = TREE_CHAIN (params);
2306 precision_wanted_type.wanted_type = integer_type_node;
2307 precision_wanted_type.wanted_type_name = NULL;
2308 precision_wanted_type.pointer_count = 0;
2309 precision_wanted_type.char_lenient_flag = 0;
2310 precision_wanted_type.writing_in_flag = 0;
2311 precision_wanted_type.name = _("field precision");
2312 precision_wanted_type.param = cur_param;
2313 precision_wanted_type.arg_num = arg_num;
2314 precision_wanted_type.next = NULL;
2315 if (last_wanted_type != 0)
2316 last_wanted_type->next = &precision_wanted_type;
2317 if (first_wanted_type == 0)
2318 first_wanted_type = &precision_wanted_type;
2319 last_wanted_type = &precision_wanted_type;
2324 while (ISDIGIT (*format_chars))
2332 fli = fki->length_char_specs;
2335 while (fli->name != 0 && fli->name[0] != *format_chars)
2340 if (fli->double_name != 0 && fli->name[0] == *format_chars)
2343 length_chars = fli->double_name;
2344 length_chars_val = fli->double_index;
2345 length_chars_std = fli->double_std;
2349 length_chars = fli->name;
2350 length_chars_val = fli->index;
2351 length_chars_std = fli->std;
2356 length_chars = NULL;
2357 length_chars_val = FMT_LEN_none;
2358 length_chars_std = STD_C89;
2362 /* Warn if the length modifier is non-standard. */
2363 if (length_chars_std == STD_EXT)
2364 status_warning (status, "ISO C does not support the `%s' %s length modifier",
2365 length_chars, fki->name);
2366 else if ((length_chars_std == STD_C99 && !flag_isoc99)
2367 || (length_chars_std == STD_C94 && !flag_isoc94))
2368 status_warning (status, "ISO C89 does not support the `%s' %s length modifier",
2369 length_chars, fki->name);
2371 if (*format_chars == 'a' && info->format_type == scanf_format_type
2374 if (format_chars[1] == 's' || format_chars[1] == 'S'
2375 || format_chars[1] == '[')
2377 /* `a' is used as a flag. */
2382 if (suppressed && length_chars_val != FMT_LEN_none)
2383 status_warning (status, "use of `*' and `%s' together in format", length_chars);
2385 format_char = *format_chars;
2386 if (format_char == 0
2387 || (info->format_type != strftime_format_type && format_char == '%'))
2389 status_warning (status, "conversion lacks type at end of format");
2393 fci = fki->conversion_specs;
2394 while (fci->format_chars != 0
2395 && index (fci->format_chars, format_char) == 0)
2397 if (fci->format_chars == 0)
2399 if (ISGRAPH(format_char))
2400 status_warning (status, "unknown conversion type character `%c' in format",
2403 status_warning (status, "unknown conversion type character 0x%x in format",
2409 if (fci->std == STD_EXT)
2410 status_warning (status, "ISO C does not support the `%%%c' %s format",
2411 format_char, fki->name);
2412 else if ((fci->std == STD_C99 && !flag_isoc99)
2413 || (fci->std == STD_C94 && !flag_isoc94))
2414 status_warning (status, "ISO C89 does not support the `%%%c' %s format",
2415 format_char, fki->name);
2416 if (index (flag_chars, 'O') != 0)
2418 if (index (fci->flag_chars, 'o') != 0)
2419 status_warning (status, "ISO C does not support `%%O%c'", format_char);
2420 else if (!flag_isoc99 && index (fci->flag_chars, 'O') != 0)
2421 status_warning (status, "ISO C89 does not support `%%O%c'", format_char);
2423 if (!flag_isoc99 && index (flag_chars, 'E'))
2424 status_warning (status, "ISO C89 does not support `%%E%c'", format_char);
2426 if (wide && index (fci->flag_chars, 'w') == 0)
2427 status_warning (status, "width used with `%c' format", format_char);
2428 if (index (fci->flag_chars, '3') != 0
2429 || (format_char == 'y' && index (flag_chars, 'E')))
2430 status_warning (status, "`%%%c' yields only last 2 digits of year in some locales",
2432 else if (index (fci->flag_chars, '2') != 0)
2433 status_warning (status, "`%%%c' yields only last 2 digits of year", format_char);
2434 if (precise && index (fci->flag_chars, 'p') == 0)
2435 status_warning (status, "precision used with `%c' format", format_char);
2436 if (aflag && index (fci->flag_chars, 'a') == 0)
2438 status_warning (status, "`a' flag used with `%c' format", format_char);
2439 /* To simplify the following code. */
2442 /* The a flag is a GNU extension. */
2443 else if (pedantic && aflag)
2444 status_warning (status, "ISO C does not support the `a' flag");
2445 if (info->format_type == scanf_format_type && format_char == '[')
2447 /* Skip over scan set, in case it happens to have '%' in it. */
2448 if (*format_chars == '^')
2450 /* Find closing bracket; if one is hit immediately, then
2451 it's part of the scan set rather than a terminator. */
2452 if (*format_chars == ']')
2454 while (*format_chars && *format_chars != ']')
2456 if (*format_chars != ']')
2457 /* The end of the format string was reached. */
2458 status_warning (status, "no closing `]' for `%%[' format");
2462 if (index (fci->flag_chars, '*') == 0)
2463 status_warning (status, "suppression of `%c' conversion in format", format_char);
2466 for (i = 0; flag_chars[i] != 0; ++i)
2468 if (index (fci->flag_chars, flag_chars[i]) == 0)
2469 status_warning (status, "flag `%c' used with type `%c'",
2470 flag_chars[i], format_char);
2472 if (info->format_type == strftime_format_type)
2474 if (precise && index (flag_chars, '0') != 0
2475 && (format_char == 'd' || format_char == 'i'
2476 || format_char == 'o' || format_char == 'u'
2477 || format_char == 'x' || format_char == 'X'))
2478 status_warning (status, "`0' flag ignored with precision specifier and `%c' format",
2480 wanted_type = (fci->types[length_chars_val].type
2481 ? *fci->types[length_chars_val].type : 0);
2482 wanted_type_name = fci->types[length_chars_val].name;
2483 wanted_type_std = fci->types[length_chars_val].std;
2484 if (wanted_type == 0)
2486 status_warning (status, "use of `%s' length modifier with `%c' type character",
2487 length_chars, format_char);
2488 /* Heuristic: skip one argument when an invalid length/type
2489 combination is encountered. */
2493 status_warning (status, "too few arguments for format");
2496 params = TREE_CHAIN (params);
2500 /* Warn if non-standard, provided it is more non-standard
2501 than the length and type characters that may already
2502 have been warned for. */
2503 && wanted_type_std > length_chars_std
2504 && wanted_type_std > fci->std)
2506 if (wanted_type_std == STD_EXT)
2507 status_warning (status, "ISO C does not support the `%%%s%c' %s format",
2508 length_chars, format_char, fki->name);
2509 else if ((wanted_type_std == STD_C99 && !flag_isoc99)
2510 || (wanted_type_std == STD_C94 && !flag_isoc94))
2511 status_warning (status, "ISO C89 does not support the `%%%s%c' %s format",
2512 length_chars, format_char, fki->name);
2515 /* Finally. . .check type of argument against desired type! */
2516 if (info->first_arg_num == 0)
2518 if (fci->pointer_count == 0 && wanted_type == void_type_node)
2520 if (main_arg_num != 0)
2521 status_warning (status, "operand number specified for format taking no argument");
2525 if (main_arg_num != 0)
2527 arg_num = main_arg_num;
2528 params = main_arg_params;
2533 if (has_operand_number > 0)
2535 status_warning (status, "missing $ operand number in format");
2539 has_operand_number = 0;
2542 status_warning (status, "too few arguments for format");
2546 cur_param = TREE_VALUE (params);
2547 params = TREE_CHAIN (params);
2548 main_wanted_type.wanted_type = wanted_type;
2549 main_wanted_type.wanted_type_name = wanted_type_name;
2550 main_wanted_type.pointer_count = fci->pointer_count + aflag;
2551 main_wanted_type.char_lenient_flag = 0;
2552 if (index (fci->flag_chars, 'c') != 0)
2553 main_wanted_type.char_lenient_flag = 1;
2554 main_wanted_type.writing_in_flag = 0;
2555 if (index (fci->flag_chars, 'W') != 0)
2556 main_wanted_type.writing_in_flag = 1;
2557 main_wanted_type.name = NULL;
2558 main_wanted_type.param = cur_param;
2559 main_wanted_type.arg_num = arg_num;
2560 main_wanted_type.next = NULL;
2561 if (last_wanted_type != 0)
2562 last_wanted_type->next = &main_wanted_type;
2563 if (first_wanted_type == 0)
2564 first_wanted_type = &main_wanted_type;
2565 last_wanted_type = &main_wanted_type;
2568 if (first_wanted_type != 0)
2569 check_format_types (status, first_wanted_type);
2575 /* Check the argument types from a single format conversion (possibly
2576 including width and precision arguments). */
2578 check_format_types (status, types)
2580 format_wanted_type *types;
2582 for (; types != 0; types = types->next)
2591 cur_param = types->param;
2592 cur_type = TREE_TYPE (cur_param);
2593 if (TREE_CODE (cur_type) == ERROR_MARK)
2596 wanted_type = types->wanted_type;
2597 arg_num = types->arg_num;
2599 /* The following should not occur here. */
2600 if (wanted_type == 0)
2602 if (wanted_type == void_type_node && types->pointer_count == 0)
2605 STRIP_NOPS (cur_param);
2607 /* Check the types of any additional pointer arguments
2608 that precede the "real" argument. */
2609 for (i = 0; i < types->pointer_count; ++i)
2611 if (TREE_CODE (cur_type) == POINTER_TYPE)
2613 cur_type = TREE_TYPE (cur_type);
2614 if (TREE_CODE (cur_type) == ERROR_MARK)
2617 if (cur_param != 0 && TREE_CODE (cur_param) == ADDR_EXPR)
2618 cur_param = TREE_OPERAND (cur_param, 0);
2622 /* See if this is an attempt to write into a const type with
2623 scanf or with printf "%n". Note: the writing in happens
2624 at the first indirection only, if for example
2625 void * const * is passed to scanf %p; passing
2626 const void ** is simply passing an incompatible type. */
2627 if (types->writing_in_flag
2629 && (TYPE_READONLY (cur_type)
2631 && (TREE_CODE_CLASS (TREE_CODE (cur_param)) == 'c'
2632 || (DECL_P (cur_param)
2633 && TREE_READONLY (cur_param))))))
2634 status_warning (status, "writing into constant object (arg %d)", arg_num);
2636 /* If there are extra type qualifiers beyond the first
2637 indirection, then this makes the types technically
2641 && (TYPE_READONLY (cur_type)
2642 || TYPE_VOLATILE (cur_type)
2643 || TYPE_RESTRICT (cur_type)))
2644 status_warning (status, "extra type qualifiers in format argument (arg %d)",
2650 if (types->pointer_count == 1)
2651 status_warning (status, "format argument is not a pointer (arg %d)", arg_num);
2653 status_warning (status, "format argument is not a pointer to a pointer (arg %d)", arg_num);
2658 if (i < types->pointer_count)
2661 orig_cur_type = cur_type;
2662 cur_type = TYPE_MAIN_VARIANT (cur_type);
2664 /* Check whether the argument type is a character type. This leniency
2665 only applies to certain formats, flagged with 'c'.
2667 if (types->char_lenient_flag)
2668 char_type_flag = (cur_type == char_type_node
2669 || cur_type == signed_char_type_node
2670 || cur_type == unsigned_char_type_node);
2672 /* Check the type of the "real" argument, if there's a type we want. */
2673 if (wanted_type == cur_type)
2675 /* If we want `void *', allow any pointer type.
2676 (Anything else would already have got a warning.)
2677 With -pedantic, only allow pointers to void and to character
2679 if (wanted_type == void_type_node
2680 && (!pedantic || (i == 1 && char_type_flag)))
2682 /* Don't warn about differences merely in signedness, unless
2683 -pedantic. With -pedantic, warn if the type is a pointer
2684 target and not a character type, and for character types at
2685 a second level of indirection. */
2686 if (TREE_CODE (wanted_type) == INTEGER_TYPE
2687 && TREE_CODE (cur_type) == INTEGER_TYPE
2688 && (! pedantic || i == 0 || (i == 1 && char_type_flag))
2689 && (TREE_UNSIGNED (wanted_type)
2690 ? wanted_type == unsigned_type (cur_type)
2691 : wanted_type == signed_type (cur_type)))
2693 /* Likewise, "signed char", "unsigned char" and "char" are
2694 equivalent but the above test won't consider them equivalent. */
2695 if (wanted_type == char_type_node
2696 && (! pedantic || i < 2)
2699 /* Now we have a type mismatch. */
2701 register const char *this;
2702 register const char *that;
2704 this = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (wanted_type)));
2706 if (TYPE_NAME (orig_cur_type) != 0
2707 && TREE_CODE (orig_cur_type) != INTEGER_TYPE
2708 && !(TREE_CODE (orig_cur_type) == POINTER_TYPE
2709 && TREE_CODE (TREE_TYPE (orig_cur_type)) == INTEGER_TYPE))
2711 if (TREE_CODE (TYPE_NAME (orig_cur_type)) == TYPE_DECL
2712 && DECL_NAME (TYPE_NAME (orig_cur_type)) != 0)
2713 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (orig_cur_type)));
2715 that = IDENTIFIER_POINTER (TYPE_NAME (orig_cur_type));
2718 /* A nameless type can't possibly match what the format wants.
2719 So there will be a warning for it.
2720 Make up a string to describe vaguely what it is. */
2723 if (TREE_CODE (orig_cur_type) == POINTER_TYPE)
2726 that = "different type";
2729 /* Make the warning better in case of mismatch of int vs long. */
2730 if (TREE_CODE (orig_cur_type) == INTEGER_TYPE
2731 && TREE_CODE (wanted_type) == INTEGER_TYPE
2732 && TYPE_PRECISION (orig_cur_type) == TYPE_PRECISION (wanted_type)
2733 && TYPE_NAME (orig_cur_type) != 0
2734 && TREE_CODE (TYPE_NAME (orig_cur_type)) == TYPE_DECL)
2735 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (orig_cur_type)));
2737 if (strcmp (this, that) != 0)
2739 /* There may be a better name for the format, e.g. size_t,
2740 but we should allow for programs with a perverse typedef
2741 making size_t something other than what the compiler
2743 if (types->wanted_type_name != 0
2744 && strcmp (types->wanted_type_name, that) != 0)
2745 this = types->wanted_type_name;
2746 if (types->name != 0)
2747 status_warning (status, "%s is not type %s (arg %d)", types->name, this,
2750 status_warning (status, "%s format, %s arg (arg %d)", this, that, arg_num);
2756 /* Print a warning if a constant expression had overflow in folding.
2757 Invoke this function on every expression that the language
2758 requires to be a constant expression.
2759 Note the ANSI C standard says it is erroneous for a
2760 constant expression to overflow. */
2763 constant_expression_warning (value)
2766 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
2767 || TREE_CODE (value) == COMPLEX_CST)
2768 && TREE_CONSTANT_OVERFLOW (value) && pedantic)
2769 pedwarn ("overflow in constant expression");
2772 /* Print a warning if an expression had overflow in folding.
2773 Invoke this function on every expression that
2774 (1) appears in the source code, and
2775 (2) might be a constant expression that overflowed, and
2776 (3) is not already checked by convert_and_check;
2777 however, do not invoke this function on operands of explicit casts. */
2780 overflow_warning (value)
2783 if ((TREE_CODE (value) == INTEGER_CST
2784 || (TREE_CODE (value) == COMPLEX_CST
2785 && TREE_CODE (TREE_REALPART (value)) == INTEGER_CST))
2786 && TREE_OVERFLOW (value))
2788 TREE_OVERFLOW (value) = 0;
2789 if (skip_evaluation == 0)
2790 warning ("integer overflow in expression");
2792 else if ((TREE_CODE (value) == REAL_CST
2793 || (TREE_CODE (value) == COMPLEX_CST
2794 && TREE_CODE (TREE_REALPART (value)) == REAL_CST))
2795 && TREE_OVERFLOW (value))
2797 TREE_OVERFLOW (value) = 0;
2798 if (skip_evaluation == 0)
2799 warning ("floating point overflow in expression");
2803 /* Print a warning if a large constant is truncated to unsigned,
2804 or if -Wconversion is used and a constant < 0 is converted to unsigned.
2805 Invoke this function on every expression that might be implicitly
2806 converted to an unsigned type. */
2809 unsigned_conversion_warning (result, operand)
2810 tree result, operand;
2812 if (TREE_CODE (operand) == INTEGER_CST
2813 && TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
2814 && TREE_UNSIGNED (TREE_TYPE (result))
2815 && skip_evaluation == 0
2816 && !int_fits_type_p (operand, TREE_TYPE (result)))
2818 if (!int_fits_type_p (operand, signed_type (TREE_TYPE (result))))
2819 /* This detects cases like converting -129 or 256 to unsigned char. */
2820 warning ("large integer implicitly truncated to unsigned type");
2821 else if (warn_conversion)
2822 warning ("negative integer implicitly converted to unsigned type");
2826 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2827 Invoke this function on every expression that is converted implicitly,
2828 i.e. because of language rules and not because of an explicit cast. */
2831 convert_and_check (type, expr)
2834 tree t = convert (type, expr);
2835 if (TREE_CODE (t) == INTEGER_CST)
2837 if (TREE_OVERFLOW (t))
2839 TREE_OVERFLOW (t) = 0;
2841 /* Do not diagnose overflow in a constant expression merely
2842 because a conversion overflowed. */
2843 TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (expr);
2845 /* No warning for converting 0x80000000 to int. */
2846 if (!(TREE_UNSIGNED (type) < TREE_UNSIGNED (TREE_TYPE (expr))
2847 && TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
2848 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (expr))))
2849 /* If EXPR fits in the unsigned version of TYPE,
2850 don't warn unless pedantic. */
2852 || TREE_UNSIGNED (type)
2853 || ! int_fits_type_p (expr, unsigned_type (type)))
2854 && skip_evaluation == 0)
2855 warning ("overflow in implicit constant conversion");
2858 unsigned_conversion_warning (t, expr);
2864 c_expand_expr_stmt (expr)
2867 /* Do default conversion if safe and possibly important,
2868 in case within ({...}). */
2869 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE && lvalue_p (expr))
2870 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
2871 expr = default_conversion (expr);
2873 if (TREE_TYPE (expr) != error_mark_node
2874 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
2875 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
2876 error ("expression statement has incomplete type");
2878 last_expr_type = TREE_TYPE (expr);
2879 add_stmt (build_stmt (EXPR_STMT, expr));
2882 /* Validate the expression after `case' and apply default promotions. */
2885 check_case_value (value)
2888 if (value == NULL_TREE)
2891 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
2892 STRIP_TYPE_NOPS (value);
2893 /* In C++, the following is allowed:
2896 switch (...) { case i: ... }
2898 So, we try to reduce the VALUE to a constant that way. */
2899 if (c_language == clk_cplusplus)
2901 value = decl_constant_value (value);
2902 STRIP_TYPE_NOPS (value);
2903 value = fold (value);
2906 if (TREE_CODE (value) != INTEGER_CST
2907 && value != error_mark_node)
2909 error ("case label does not reduce to an integer constant");
2910 value = error_mark_node;
2913 /* Promote char or short to int. */
2914 value = default_conversion (value);
2916 constant_expression_warning (value);
2921 /* Return an integer type with BITS bits of precision,
2922 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
2925 type_for_size (bits, unsignedp)
2929 if (bits == TYPE_PRECISION (integer_type_node))
2930 return unsignedp ? unsigned_type_node : integer_type_node;
2932 if (bits == TYPE_PRECISION (signed_char_type_node))
2933 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2935 if (bits == TYPE_PRECISION (short_integer_type_node))
2936 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2938 if (bits == TYPE_PRECISION (long_integer_type_node))
2939 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2941 if (bits == TYPE_PRECISION (long_long_integer_type_node))
2942 return (unsignedp ? long_long_unsigned_type_node
2943 : long_long_integer_type_node);
2945 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
2946 return (unsignedp ? widest_unsigned_literal_type_node
2947 : widest_integer_literal_type_node);
2949 if (bits <= TYPE_PRECISION (intQI_type_node))
2950 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2952 if (bits <= TYPE_PRECISION (intHI_type_node))
2953 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2955 if (bits <= TYPE_PRECISION (intSI_type_node))
2956 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2958 if (bits <= TYPE_PRECISION (intDI_type_node))
2959 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2964 /* Return a data type that has machine mode MODE.
2965 If the mode is an integer,
2966 then UNSIGNEDP selects between signed and unsigned types. */
2969 type_for_mode (mode, unsignedp)
2970 enum machine_mode mode;
2973 if (mode == TYPE_MODE (integer_type_node))
2974 return unsignedp ? unsigned_type_node : integer_type_node;
2976 if (mode == TYPE_MODE (signed_char_type_node))
2977 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2979 if (mode == TYPE_MODE (short_integer_type_node))
2980 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2982 if (mode == TYPE_MODE (long_integer_type_node))
2983 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2985 if (mode == TYPE_MODE (long_long_integer_type_node))
2986 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
2988 if (mode == TYPE_MODE (widest_integer_literal_type_node))
2989 return unsignedp ? widest_unsigned_literal_type_node
2990 : widest_integer_literal_type_node;
2992 if (mode == TYPE_MODE (intQI_type_node))
2993 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2995 if (mode == TYPE_MODE (intHI_type_node))
2996 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2998 if (mode == TYPE_MODE (intSI_type_node))
2999 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3001 if (mode == TYPE_MODE (intDI_type_node))
3002 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3004 #if HOST_BITS_PER_WIDE_INT >= 64
3005 if (mode == TYPE_MODE (intTI_type_node))
3006 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3009 if (mode == TYPE_MODE (float_type_node))
3010 return float_type_node;
3012 if (mode == TYPE_MODE (double_type_node))
3013 return double_type_node;
3015 if (mode == TYPE_MODE (long_double_type_node))
3016 return long_double_type_node;
3018 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
3019 return build_pointer_type (char_type_node);
3021 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
3022 return build_pointer_type (integer_type_node);
3024 #ifdef VECTOR_MODE_SUPPORTED_P
3025 if (mode == TYPE_MODE (V4SF_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
3026 return V4SF_type_node;
3027 if (mode == TYPE_MODE (V4SI_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
3028 return V4SI_type_node;
3029 if (mode == TYPE_MODE (V2SI_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
3030 return V2SI_type_node;
3031 if (mode == TYPE_MODE (V4HI_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
3032 return V4HI_type_node;
3033 if (mode == TYPE_MODE (V8QI_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
3034 return V8QI_type_node;
3040 /* Return an unsigned type the same as TYPE in other respects. */
3042 unsigned_type (type)
3045 tree type1 = TYPE_MAIN_VARIANT (type);
3046 if (type1 == signed_char_type_node || type1 == char_type_node)
3047 return unsigned_char_type_node;
3048 if (type1 == integer_type_node)
3049 return unsigned_type_node;
3050 if (type1 == short_integer_type_node)
3051 return short_unsigned_type_node;
3052 if (type1 == long_integer_type_node)
3053 return long_unsigned_type_node;
3054 if (type1 == long_long_integer_type_node)
3055 return long_long_unsigned_type_node;
3056 if (type1 == widest_integer_literal_type_node)
3057 return widest_unsigned_literal_type_node;
3058 #if HOST_BITS_PER_WIDE_INT >= 64
3059 if (type1 == intTI_type_node)
3060 return unsigned_intTI_type_node;
3062 if (type1 == intDI_type_node)
3063 return unsigned_intDI_type_node;
3064 if (type1 == intSI_type_node)
3065 return unsigned_intSI_type_node;
3066 if (type1 == intHI_type_node)
3067 return unsigned_intHI_type_node;
3068 if (type1 == intQI_type_node)
3069 return unsigned_intQI_type_node;
3071 return signed_or_unsigned_type (1, type);
3074 /* Return a signed type the same as TYPE in other respects. */
3080 tree type1 = TYPE_MAIN_VARIANT (type);
3081 if (type1 == unsigned_char_type_node || type1 == char_type_node)
3082 return signed_char_type_node;
3083 if (type1 == unsigned_type_node)
3084 return integer_type_node;
3085 if (type1 == short_unsigned_type_node)
3086 return short_integer_type_node;
3087 if (type1 == long_unsigned_type_node)
3088 return long_integer_type_node;
3089 if (type1 == long_long_unsigned_type_node)
3090 return long_long_integer_type_node;
3091 if (type1 == widest_unsigned_literal_type_node)
3092 return widest_integer_literal_type_node;
3093 #if HOST_BITS_PER_WIDE_INT >= 64
3094 if (type1 == unsigned_intTI_type_node)
3095 return intTI_type_node;
3097 if (type1 == unsigned_intDI_type_node)
3098 return intDI_type_node;
3099 if (type1 == unsigned_intSI_type_node)
3100 return intSI_type_node;
3101 if (type1 == unsigned_intHI_type_node)
3102 return intHI_type_node;
3103 if (type1 == unsigned_intQI_type_node)
3104 return intQI_type_node;
3106 return signed_or_unsigned_type (0, type);
3109 /* Return a type the same as TYPE except unsigned or
3110 signed according to UNSIGNEDP. */
3113 signed_or_unsigned_type (unsignedp, type)
3117 if (! INTEGRAL_TYPE_P (type)
3118 || TREE_UNSIGNED (type) == unsignedp)
3121 if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node))
3122 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3123 if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
3124 return unsignedp ? unsigned_type_node : integer_type_node;
3125 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node))
3126 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3127 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node))
3128 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3129 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node))
3130 return (unsignedp ? long_long_unsigned_type_node
3131 : long_long_integer_type_node);
3132 if (TYPE_PRECISION (type) == TYPE_PRECISION (widest_integer_literal_type_node))
3133 return (unsignedp ? widest_unsigned_literal_type_node
3134 : widest_integer_literal_type_node);
3138 /* Return the minimum number of bits needed to represent VALUE in a
3139 signed or unsigned type, UNSIGNEDP says which. */
3142 min_precision (value, unsignedp)
3148 /* If the value is negative, compute its negative minus 1. The latter
3149 adjustment is because the absolute value of the largest negative value
3150 is one larger than the largest positive value. This is equivalent to
3151 a bit-wise negation, so use that operation instead. */
3153 if (tree_int_cst_sgn (value) < 0)
3154 value = fold (build1 (BIT_NOT_EXPR, TREE_TYPE (value), value));
3156 /* Return the number of bits needed, taking into account the fact
3157 that we need one more bit for a signed than unsigned type. */
3159 if (integer_zerop (value))
3162 log = tree_floor_log2 (value);
3164 return log + 1 + ! unsignedp;
3167 /* Print an error message for invalid operands to arith operation CODE.
3168 NOP_EXPR is used as a special case (see truthvalue_conversion). */
3171 binary_op_error (code)
3172 enum tree_code code;
3174 register const char *opname;
3179 error ("invalid truth-value expression");
3183 opname = "+"; break;
3185 opname = "-"; break;
3187 opname = "*"; break;
3189 opname = "max"; break;
3191 opname = "min"; break;
3193 opname = "=="; break;
3195 opname = "!="; break;
3197 opname = "<="; break;
3199 opname = ">="; break;
3201 opname = "<"; break;
3203 opname = ">"; break;
3205 opname = "<<"; break;
3207 opname = ">>"; break;
3208 case TRUNC_MOD_EXPR:
3209 case FLOOR_MOD_EXPR:
3210 opname = "%"; break;
3211 case TRUNC_DIV_EXPR:
3212 case FLOOR_DIV_EXPR:
3213 opname = "/"; break;
3215 opname = "&"; break;
3217 opname = "|"; break;
3218 case TRUTH_ANDIF_EXPR:
3219 opname = "&&"; break;
3220 case TRUTH_ORIF_EXPR:
3221 opname = "||"; break;
3223 opname = "^"; break;
3226 opname = "rotate"; break;
3228 opname = "unknown"; break;
3230 error ("invalid operands to binary %s", opname);
3233 /* Subroutine of build_binary_op, used for comparison operations.
3234 See if the operands have both been converted from subword integer types
3235 and, if so, perhaps change them both back to their original type.
3236 This function is also responsible for converting the two operands
3237 to the proper common type for comparison.
3239 The arguments of this function are all pointers to local variables
3240 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
3241 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
3243 If this function returns nonzero, it means that the comparison has
3244 a constant value. What this function returns is an expression for
3248 shorten_compare (op0_ptr, op1_ptr, restype_ptr, rescode_ptr)
3249 tree *op0_ptr, *op1_ptr;
3251 enum tree_code *rescode_ptr;
3254 tree op0 = *op0_ptr;
3255 tree op1 = *op1_ptr;
3256 int unsignedp0, unsignedp1;
3258 tree primop0, primop1;
3259 enum tree_code code = *rescode_ptr;
3261 /* Throw away any conversions to wider types
3262 already present in the operands. */
3264 primop0 = get_narrower (op0, &unsignedp0);
3265 primop1 = get_narrower (op1, &unsignedp1);
3267 /* Handle the case that OP0 does not *contain* a conversion
3268 but it *requires* conversion to FINAL_TYPE. */
3270 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
3271 unsignedp0 = TREE_UNSIGNED (TREE_TYPE (op0));
3272 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
3273 unsignedp1 = TREE_UNSIGNED (TREE_TYPE (op1));
3275 /* If one of the operands must be floated, we cannot optimize. */
3276 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
3277 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
3279 /* If first arg is constant, swap the args (changing operation
3280 so value is preserved), for canonicalization. Don't do this if
3281 the second arg is 0. */
3283 if (TREE_CONSTANT (primop0)
3284 && ! integer_zerop (primop1) && ! real_zerop (primop1))
3286 register tree tem = primop0;
3287 register int temi = unsignedp0;
3295 unsignedp0 = unsignedp1;
3318 *rescode_ptr = code;
3321 /* If comparing an integer against a constant more bits wide,
3322 maybe we can deduce a value of 1 or 0 independent of the data.
3323 Or else truncate the constant now
3324 rather than extend the variable at run time.
3326 This is only interesting if the constant is the wider arg.
3327 Also, it is not safe if the constant is unsigned and the
3328 variable arg is signed, since in this case the variable
3329 would be sign-extended and then regarded as unsigned.
3330 Our technique fails in this case because the lowest/highest
3331 possible unsigned results don't follow naturally from the
3332 lowest/highest possible values of the variable operand.
3333 For just EQ_EXPR and NE_EXPR there is another technique that
3334 could be used: see if the constant can be faithfully represented
3335 in the other operand's type, by truncating it and reextending it
3336 and see if that preserves the constant's value. */
3338 if (!real1 && !real2
3339 && TREE_CODE (primop1) == INTEGER_CST
3340 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
3342 int min_gt, max_gt, min_lt, max_lt;
3343 tree maxval, minval;
3344 /* 1 if comparison is nominally unsigned. */
3345 int unsignedp = TREE_UNSIGNED (*restype_ptr);
3348 type = signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0));
3350 /* If TYPE is an enumeration, then we need to get its min/max
3351 values from it's underlying integral type, not the enumerated
3353 if (TREE_CODE (type) == ENUMERAL_TYPE)
3354 type = type_for_size (TYPE_PRECISION (type), unsignedp0);
3356 maxval = TYPE_MAX_VALUE (type);
3357 minval = TYPE_MIN_VALUE (type);
3359 if (unsignedp && !unsignedp0)
3360 *restype_ptr = signed_type (*restype_ptr);
3362 if (TREE_TYPE (primop1) != *restype_ptr)
3363 primop1 = convert (*restype_ptr, primop1);
3364 if (type != *restype_ptr)
3366 minval = convert (*restype_ptr, minval);
3367 maxval = convert (*restype_ptr, maxval);
3370 if (unsignedp && unsignedp0)
3372 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
3373 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
3374 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
3375 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
3379 min_gt = INT_CST_LT (primop1, minval);
3380 max_gt = INT_CST_LT (primop1, maxval);
3381 min_lt = INT_CST_LT (minval, primop1);
3382 max_lt = INT_CST_LT (maxval, primop1);
3386 /* This used to be a switch, but Genix compiler can't handle that. */
3387 if (code == NE_EXPR)
3389 if (max_lt || min_gt)
3390 val = boolean_true_node;
3392 else if (code == EQ_EXPR)
3394 if (max_lt || min_gt)
3395 val = boolean_false_node;
3397 else if (code == LT_EXPR)
3400 val = boolean_true_node;
3402 val = boolean_false_node;
3404 else if (code == GT_EXPR)
3407 val = boolean_true_node;
3409 val = boolean_false_node;
3411 else if (code == LE_EXPR)
3414 val = boolean_true_node;
3416 val = boolean_false_node;
3418 else if (code == GE_EXPR)
3421 val = boolean_true_node;
3423 val = boolean_false_node;
3426 /* If primop0 was sign-extended and unsigned comparison specd,
3427 we did a signed comparison above using the signed type bounds.
3428 But the comparison we output must be unsigned.
3430 Also, for inequalities, VAL is no good; but if the signed
3431 comparison had *any* fixed result, it follows that the
3432 unsigned comparison just tests the sign in reverse
3433 (positive values are LE, negative ones GE).
3434 So we can generate an unsigned comparison
3435 against an extreme value of the signed type. */
3437 if (unsignedp && !unsignedp0)
3444 primop1 = TYPE_MIN_VALUE (type);
3450 primop1 = TYPE_MAX_VALUE (type);
3457 type = unsigned_type (type);
3460 if (!max_gt && !unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
3462 /* This is the case of (char)x >?< 0x80, which people used to use
3463 expecting old C compilers to change the 0x80 into -0x80. */
3464 if (val == boolean_false_node)
3465 warning ("comparison is always false due to limited range of data type");
3466 if (val == boolean_true_node)
3467 warning ("comparison is always true due to limited range of data type");
3470 if (!min_lt && unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
3472 /* This is the case of (unsigned char)x >?< -1 or < 0. */
3473 if (val == boolean_false_node)
3474 warning ("comparison is always false due to limited range of data type");
3475 if (val == boolean_true_node)
3476 warning ("comparison is always true due to limited range of data type");
3481 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
3482 if (TREE_SIDE_EFFECTS (primop0))
3483 return build (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
3487 /* Value is not predetermined, but do the comparison
3488 in the type of the operand that is not constant.
3489 TYPE is already properly set. */
3491 else if (real1 && real2
3492 && (TYPE_PRECISION (TREE_TYPE (primop0))
3493 == TYPE_PRECISION (TREE_TYPE (primop1))))
3494 type = TREE_TYPE (primop0);
3496 /* If args' natural types are both narrower than nominal type
3497 and both extend in the same manner, compare them
3498 in the type of the wider arg.
3499 Otherwise must actually extend both to the nominal
3500 common type lest different ways of extending
3502 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
3504 else if (unsignedp0 == unsignedp1 && real1 == real2
3505 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
3506 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
3508 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
3509 type = signed_or_unsigned_type (unsignedp0
3510 || TREE_UNSIGNED (*restype_ptr),
3512 /* Make sure shorter operand is extended the right way
3513 to match the longer operand. */
3514 primop0 = convert (signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0)),
3516 primop1 = convert (signed_or_unsigned_type (unsignedp1, TREE_TYPE (primop1)),
3521 /* Here we must do the comparison on the nominal type
3522 using the args exactly as we received them. */
3523 type = *restype_ptr;
3527 if (!real1 && !real2 && integer_zerop (primop1)
3528 && TREE_UNSIGNED (*restype_ptr))
3534 /* All unsigned values are >= 0, so we warn if extra warnings
3535 are requested. However, if OP0 is a constant that is
3536 >= 0, the signedness of the comparison isn't an issue,
3537 so suppress the warning. */
3538 if (extra_warnings && !in_system_header
3539 && ! (TREE_CODE (primop0) == INTEGER_CST
3540 && ! TREE_OVERFLOW (convert (signed_type (type),
3542 warning ("comparison of unsigned expression >= 0 is always true");
3543 value = boolean_true_node;
3547 if (extra_warnings && !in_system_header
3548 && ! (TREE_CODE (primop0) == INTEGER_CST
3549 && ! TREE_OVERFLOW (convert (signed_type (type),
3551 warning ("comparison of unsigned expression < 0 is always false");
3552 value = boolean_false_node;
3561 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
3562 if (TREE_SIDE_EFFECTS (primop0))
3563 return build (COMPOUND_EXPR, TREE_TYPE (value),
3570 *op0_ptr = convert (type, primop0);
3571 *op1_ptr = convert (type, primop1);
3573 *restype_ptr = boolean_type_node;
3578 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
3579 or validate its data type for an `if' or `while' statement or ?..: exp.
3581 This preparation consists of taking the ordinary
3582 representation of an expression expr and producing a valid tree
3583 boolean expression describing whether expr is nonzero. We could
3584 simply always do build_binary_op (NE_EXPR, expr, boolean_false_node, 1),
3585 but we optimize comparisons, &&, ||, and !.
3587 The resulting type should always be `boolean_type_node'. */
3590 truthvalue_conversion (expr)
3593 if (TREE_CODE (expr) == ERROR_MARK)
3596 #if 0 /* This appears to be wrong for C++. */
3597 /* These really should return error_mark_node after 2.4 is stable.
3598 But not all callers handle ERROR_MARK properly. */
3599 switch (TREE_CODE (TREE_TYPE (expr)))
3602 error ("struct type value used where scalar is required");
3603 return boolean_false_node;
3606 error ("union type value used where scalar is required");
3607 return boolean_false_node;
3610 error ("array type value used where scalar is required");
3611 return boolean_false_node;
3618 switch (TREE_CODE (expr))
3621 case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
3622 case TRUTH_ANDIF_EXPR:
3623 case TRUTH_ORIF_EXPR:
3624 case TRUTH_AND_EXPR:
3626 case TRUTH_XOR_EXPR:
3627 case TRUTH_NOT_EXPR:
3628 TREE_TYPE (expr) = boolean_type_node;
3635 return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
3638 return real_zerop (expr) ? boolean_false_node : boolean_true_node;
3641 /* If we are taking the address of a external decl, it might be zero
3642 if it is weak, so we cannot optimize. */
3643 if (DECL_P (TREE_OPERAND (expr, 0))
3644 && DECL_EXTERNAL (TREE_OPERAND (expr, 0)))
3647 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0)))
3648 return build (COMPOUND_EXPR, boolean_type_node,
3649 TREE_OPERAND (expr, 0), boolean_true_node);
3651 return boolean_true_node;
3654 return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
3655 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
3656 truthvalue_conversion (TREE_OPERAND (expr, 0)),
3657 truthvalue_conversion (TREE_OPERAND (expr, 1)),
3664 /* These don't change whether an object is non-zero or zero. */
3665 return truthvalue_conversion (TREE_OPERAND (expr, 0));
3669 /* These don't change whether an object is zero or non-zero, but
3670 we can't ignore them if their second arg has side-effects. */
3671 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
3672 return build (COMPOUND_EXPR, boolean_type_node, TREE_OPERAND (expr, 1),
3673 truthvalue_conversion (TREE_OPERAND (expr, 0)));
3675 return truthvalue_conversion (TREE_OPERAND (expr, 0));
3678 /* Distribute the conversion into the arms of a COND_EXPR. */
3679 return fold (build (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
3680 truthvalue_conversion (TREE_OPERAND (expr, 1)),
3681 truthvalue_conversion (TREE_OPERAND (expr, 2))));
3684 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
3685 since that affects how `default_conversion' will behave. */
3686 if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE
3687 || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
3689 /* fall through... */
3691 /* If this is widening the argument, we can ignore it. */
3692 if (TYPE_PRECISION (TREE_TYPE (expr))
3693 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
3694 return truthvalue_conversion (TREE_OPERAND (expr, 0));
3698 /* With IEEE arithmetic, x - x may not equal 0, so we can't optimize
3700 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
3701 && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE)
3703 /* fall through... */
3705 /* This and MINUS_EXPR can be changed into a comparison of the
3707 if (TREE_TYPE (TREE_OPERAND (expr, 0))
3708 == TREE_TYPE (TREE_OPERAND (expr, 1)))
3709 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
3710 TREE_OPERAND (expr, 1), 1);
3711 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
3712 fold (build1 (NOP_EXPR,
3713 TREE_TYPE (TREE_OPERAND (expr, 0)),
3714 TREE_OPERAND (expr, 1))), 1);
3717 if (integer_onep (TREE_OPERAND (expr, 1))
3718 && TREE_TYPE (expr) != boolean_type_node)
3719 /* Using convert here would cause infinite recursion. */
3720 return build1 (NOP_EXPR, boolean_type_node, expr);
3724 if (warn_parentheses && C_EXP_ORIGINAL_CODE (expr) == MODIFY_EXPR)
3725 warning ("suggest parentheses around assignment used as truth value");
3732 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
3734 tree tem = save_expr (expr);
3735 return (build_binary_op
3736 ((TREE_SIDE_EFFECTS (expr)
3737 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
3738 truthvalue_conversion (build_unary_op (REALPART_EXPR, tem, 0)),
3739 truthvalue_conversion (build_unary_op (IMAGPART_EXPR, tem, 0)),
3743 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
3747 /* Read the rest of a #-directive from input stream FINPUT.
3748 In normal use, the directive name and the white space after it
3749 have already been read, so they won't be included in the result.
3750 We allow for the fact that the directive line may contain
3751 a newline embedded within a character or string literal which forms
3752 a part of the directive.
3754 The value is a string in a reusable buffer. It remains valid
3755 only until the next time this function is called.
3757 The terminating character ('\n' or EOF) is left in FINPUT for the
3758 caller to re-read. */
3761 get_directive_line (finput)
3762 register FILE *finput;
3764 static char *directive_buffer = NULL;
3765 static unsigned buffer_length = 0;
3767 register char *buffer_limit;
3768 register int looking_for = 0;
3769 register int char_escaped = 0;
3771 if (buffer_length == 0)
3773 directive_buffer = (char *)xmalloc (128);
3774 buffer_length = 128;
3777 buffer_limit = &directive_buffer[buffer_length];
3779 for (p = directive_buffer; ; )
3783 /* Make buffer bigger if it is full. */
3784 if (p >= buffer_limit)
3786 register unsigned bytes_used = (p - directive_buffer);
3790 = (char *)xrealloc (directive_buffer, buffer_length);
3791 p = &directive_buffer[bytes_used];
3792 buffer_limit = &directive_buffer[buffer_length];
3797 /* Discard initial whitespace. */
3798 if ((c == ' ' || c == '\t') && p == directive_buffer)
3801 /* Detect the end of the directive. */
3802 if (looking_for == 0
3803 && (c == '\n' || c == EOF))
3812 return directive_buffer;
3814 /* Handle string and character constant syntax. */
3817 if (looking_for == c && !char_escaped)
3818 looking_for = 0; /* Found terminator... stop looking. */
3821 if (c == '\'' || c == '"')
3822 looking_for = c; /* Don't stop buffering until we see another
3823 one of these (or an EOF). */
3825 /* Handle backslash. */
3826 char_escaped = (c == '\\' && ! char_escaped);
3829 #endif /* USE_CPPLIB */
3831 /* Make a variant type in the proper way for C/C++, propagating qualifiers
3832 down to the element type of an array. */
3835 c_build_qualified_type (type, type_quals)
3839 /* A restrict-qualified pointer type must be a pointer to object or
3840 incomplete type. Note that the use of POINTER_TYPE_P also allows
3841 REFERENCE_TYPEs, which is appropriate for C++. Unfortunately,
3842 the C++ front-end also use POINTER_TYPE for pointer-to-member
3843 values, so even though it should be illegal to use `restrict'
3844 with such an entity we don't flag that here. Thus, special case
3845 code for that case is required in the C++ front-end. */
3846 if ((type_quals & TYPE_QUAL_RESTRICT)
3847 && (!POINTER_TYPE_P (type)
3848 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
3850 error ("invalid use of `restrict'");
3851 type_quals &= ~TYPE_QUAL_RESTRICT;
3854 if (TREE_CODE (type) == ARRAY_TYPE)
3855 return build_array_type (c_build_qualified_type (TREE_TYPE (type),
3857 TYPE_DOMAIN (type));
3858 return build_qualified_type (type, type_quals);
3861 /* Apply the TYPE_QUALS to the new DECL. */
3864 c_apply_type_quals_to_decl (type_quals, decl)
3868 if ((type_quals & TYPE_QUAL_CONST)
3869 || (TREE_TYPE (decl)
3870 && TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE))
3871 TREE_READONLY (decl) = 1;
3872 if (type_quals & TYPE_QUAL_VOLATILE)
3874 TREE_SIDE_EFFECTS (decl) = 1;
3875 TREE_THIS_VOLATILE (decl) = 1;
3877 if (type_quals & TYPE_QUAL_RESTRICT)
3879 if (!TREE_TYPE (decl)
3880 || !POINTER_TYPE_P (TREE_TYPE (decl))
3881 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (TREE_TYPE (decl))))
3882 error ("invalid use of `restrict'");
3883 else if (flag_strict_aliasing)
3885 /* No two restricted pointers can point at the same thing.
3886 However, a restricted pointer can point at the same thing
3887 as an unrestricted pointer, if that unrestricted pointer
3888 is based on the restricted pointer. So, we make the
3889 alias set for the restricted pointer a subset of the
3890 alias set for the type pointed to by the type of the
3893 HOST_WIDE_INT pointed_to_alias_set
3894 = get_alias_set (TREE_TYPE (TREE_TYPE (decl)));
3896 if (pointed_to_alias_set == 0)
3897 /* It's not legal to make a subset of alias set zero. */
3901 DECL_POINTER_ALIAS_SET (decl) = new_alias_set ();
3902 record_alias_subset (pointed_to_alias_set,
3903 DECL_POINTER_ALIAS_SET (decl));
3910 /* Return the typed-based alias set for T, which may be an expression
3911 or a type. Return -1 if we don't do anything special. */
3914 lang_get_alias_set (t)
3919 /* Permit type-punning when accessing a union, provided the access
3920 is directly through the union. For example, this code does not
3921 permit taking the address of a union member and then storing
3922 through it. Even the type-punning allowed here is a GCC
3923 extension, albeit a common and useful one; the C standard says
3924 that such accesses have implementation-defined behavior. */
3926 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
3927 u = TREE_OPERAND (u, 0))
3928 if (TREE_CODE (u) == COMPONENT_REF
3929 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
3932 /* If this is a char *, the ANSI C standard says it can alias
3933 anything. Note that all references need do this. */
3934 if (TREE_CODE_CLASS (TREE_CODE (t)) == 'r'
3935 && TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
3936 && TYPE_PRECISION (TREE_TYPE (t)) == TYPE_PRECISION (char_type_node))
3939 /* That's all the expressions we handle specially. */
3943 /* The C standard specifically allows aliasing between signed and
3944 unsigned variants of the same type. We treat the signed
3945 variant as canonical. */
3946 if (TREE_CODE (t) == INTEGER_TYPE && TREE_UNSIGNED (t))
3948 tree t1 = signed_type (t);
3950 /* t1 == t can happen for boolean nodes which are always unsigned. */
3952 return get_alias_set (t1);
3954 else if (POINTER_TYPE_P (t))
3958 /* Unfortunately, there is no canonical form of a pointer type.
3959 In particular, if we have `typedef int I', then `int *', and
3960 `I *' are different types. So, we have to pick a canonical
3961 representative. We do this below.
3963 Technically, this approach is actually more conservative that
3964 it needs to be. In particular, `const int *' and `int *'
3965 chould be in different alias sets, according to the C and C++
3966 standard, since their types are not the same, and so,
3967 technically, an `int **' and `const int **' cannot point at
3970 But, the standard is wrong. In particular, this code is
3975 const int* const* cipp = &ip;
3977 And, it doesn't make sense for that to be legal unless you
3978 can dereference IPP and CIPP. So, we ignore cv-qualifiers on
3979 the pointed-to types. This issue has been reported to the
3981 t1 = TYPE_MAIN_VARIANT (TREE_TYPE (t));
3982 t1 = ((TREE_CODE (t) == POINTER_TYPE)
3983 ? build_pointer_type (t1) : build_reference_type (t1));
3985 return get_alias_set (t1);
3987 /* It's not yet safe to use alias sets for classes in C++ because
3988 the TYPE_FIELDs list for a class doesn't mention base classes. */
3989 else if (c_language == clk_cplusplus && AGGREGATE_TYPE_P (t))
3995 /* Build tree nodes and builtin functions common to both C and C++ language
3997 CPLUS_MODE is nonzero if we are called from the C++ frontend, we generate
3998 some stricter prototypes in that case.
3999 NO_BUILTINS and NO_NONANSI_BUILTINS contain the respective values of
4000 the language frontend flags flag_no_builtin and
4001 flag_no_nonansi_builtin. */
4004 c_common_nodes_and_builtins (cplus_mode, no_builtins, no_nonansi_builtins)
4005 int cplus_mode, no_builtins, no_nonansi_builtins;
4008 tree memcpy_ftype, memset_ftype, strlen_ftype;
4009 tree bzero_ftype, bcmp_ftype;
4010 tree endlink, int_endlink, double_endlink, unsigned_endlink;
4011 tree sizetype_endlink;
4012 tree ptr_ftype, ptr_ftype_unsigned;
4013 tree void_ftype_any, void_ftype_int, int_ftype_any;
4014 tree double_ftype_double, double_ftype_double_double;
4015 tree float_ftype_float, ldouble_ftype_ldouble;
4016 tree int_ftype_cptr_cptr_sizet;
4017 tree int_ftype_string_string, string_ftype_ptr_ptr;
4018 tree long_ftype_long;
4019 /* Either char* or void*. */
4020 tree traditional_ptr_type_node;
4021 /* Either const char* or const void*. */
4022 tree traditional_cptr_type_node;
4023 tree traditional_len_type_node;
4024 tree traditional_len_endlink;
4025 tree va_list_ref_type_node;
4026 tree va_list_arg_type_node;
4028 pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_va_list"),
4029 va_list_type_node));
4031 pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_ptrdiff_t"),
4032 ptrdiff_type_node));
4034 pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_size_t"),
4037 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
4039 va_list_arg_type_node = va_list_ref_type_node =
4040 build_pointer_type (TREE_TYPE (va_list_type_node));
4044 va_list_arg_type_node = va_list_type_node;
4045 va_list_ref_type_node = build_reference_type (va_list_type_node);
4048 endlink = void_list_node;
4049 int_endlink = tree_cons (NULL_TREE, integer_type_node, endlink);
4050 double_endlink = tree_cons (NULL_TREE, double_type_node, endlink);
4051 unsigned_endlink = tree_cons (NULL_TREE, unsigned_type_node, endlink);
4053 ptr_ftype = build_function_type (ptr_type_node, NULL_TREE);
4054 ptr_ftype_unsigned = build_function_type (ptr_type_node, unsigned_endlink);
4055 sizetype_endlink = tree_cons (NULL_TREE, TYPE_DOMAIN (sizetype), endlink);
4056 /* We realloc here because sizetype could be int or unsigned. S'ok. */
4057 ptr_ftype_sizetype = build_function_type (ptr_type_node, sizetype_endlink);
4059 int_ftype_any = build_function_type (integer_type_node, NULL_TREE);
4060 void_ftype_any = build_function_type (void_type_node, NULL_TREE);
4061 void_ftype = build_function_type (void_type_node, endlink);
4062 void_ftype_int = build_function_type (void_type_node, int_endlink);
4064 = build_function_type (void_type_node,
4065 tree_cons (NULL_TREE, ptr_type_node, endlink));
4068 = build_function_type (float_type_node,
4069 tree_cons (NULL_TREE, float_type_node, endlink));
4072 = build_function_type (double_type_node, double_endlink);
4074 ldouble_ftype_ldouble
4075 = build_function_type (long_double_type_node,
4076 tree_cons (NULL_TREE, long_double_type_node,
4079 double_ftype_double_double
4080 = build_function_type (double_type_node,
4081 tree_cons (NULL_TREE, double_type_node,
4085 = build_function_type (integer_type_node, int_endlink);
4088 = build_function_type (long_integer_type_node,
4089 tree_cons (NULL_TREE, long_integer_type_node,
4092 int_ftype_cptr_cptr_sizet
4093 = build_function_type (integer_type_node,
4094 tree_cons (NULL_TREE, const_ptr_type_node,
4095 tree_cons (NULL_TREE, const_ptr_type_node,
4096 tree_cons (NULL_TREE,
4100 void_zero_node = build_int_2 (0, 0);
4101 TREE_TYPE (void_zero_node) = void_type_node;
4103 /* Prototype for strcpy. */
4104 string_ftype_ptr_ptr
4105 = build_function_type (string_type_node,
4106 tree_cons (NULL_TREE, string_type_node,
4107 tree_cons (NULL_TREE,
4108 const_string_type_node,
4111 traditional_len_type_node = (flag_traditional && ! cplus_mode
4112 ? integer_type_node : sizetype);
4113 traditional_len_endlink = tree_cons (NULL_TREE, traditional_len_type_node,
4116 /* Prototype for strcmp. */
4117 int_ftype_string_string
4118 = build_function_type (integer_type_node,
4119 tree_cons (NULL_TREE, const_string_type_node,
4120 tree_cons (NULL_TREE,
4121 const_string_type_node,
4124 /* Prototype for strlen. */
4126 = build_function_type (traditional_len_type_node,
4127 tree_cons (NULL_TREE, const_string_type_node,
4130 traditional_ptr_type_node = (flag_traditional && ! cplus_mode
4131 ? string_type_node : ptr_type_node);
4132 traditional_cptr_type_node = (flag_traditional && ! cplus_mode
4133 ? const_string_type_node : const_ptr_type_node);
4135 /* Prototype for memcpy. */
4137 = build_function_type (traditional_ptr_type_node,
4138 tree_cons (NULL_TREE, ptr_type_node,
4139 tree_cons (NULL_TREE, const_ptr_type_node,
4140 sizetype_endlink)));
4142 /* Prototype for memset. */
4144 = build_function_type (traditional_ptr_type_node,
4145 tree_cons (NULL_TREE, ptr_type_node,
4146 tree_cons (NULL_TREE, integer_type_node,
4147 tree_cons (NULL_TREE,
4151 /* Prototype for bzero. */
4153 = build_function_type (void_type_node,
4154 tree_cons (NULL_TREE, traditional_ptr_type_node,
4155 traditional_len_endlink));
4157 /* Prototype for bcmp. */
4159 = build_function_type (integer_type_node,
4160 tree_cons (NULL_TREE, traditional_cptr_type_node,
4161 tree_cons (NULL_TREE,
4162 traditional_cptr_type_node,
4163 traditional_len_endlink)));
4165 builtin_function ("__builtin_constant_p", default_function_type,
4166 BUILT_IN_CONSTANT_P, BUILT_IN_NORMAL, NULL_PTR);
4168 builtin_function ("__builtin_return_address", ptr_ftype_unsigned,
4169 BUILT_IN_RETURN_ADDRESS, BUILT_IN_NORMAL, NULL_PTR);
4171 builtin_function ("__builtin_frame_address", ptr_ftype_unsigned,
4172 BUILT_IN_FRAME_ADDRESS, BUILT_IN_NORMAL, NULL_PTR);
4174 builtin_function ("__builtin_alloca", ptr_ftype_sizetype,
4175 BUILT_IN_ALLOCA, BUILT_IN_NORMAL, "alloca");
4176 builtin_function ("__builtin_ffs", int_ftype_int, BUILT_IN_FFS,
4177 BUILT_IN_NORMAL, NULL_PTR);
4178 /* Define alloca, ffs as builtins.
4179 Declare _exit just to mark it as volatile. */
4180 if (! no_builtins && ! no_nonansi_builtins)
4183 temp = builtin_function ("alloca", ptr_ftype_sizetype,
4184 BUILT_IN_ALLOCA, BUILT_IN_NORMAL, NULL_PTR);
4185 /* Suppress error if redefined as a non-function. */
4186 DECL_BUILT_IN_NONANSI (temp) = 1;
4188 temp = builtin_function ("ffs", int_ftype_int, BUILT_IN_FFS,
4189 BUILT_IN_NORMAL, NULL_PTR);
4190 /* Suppress error if redefined as a non-function. */
4191 DECL_BUILT_IN_NONANSI (temp) = 1;
4192 temp = builtin_function ("_exit", void_ftype_int,
4193 0, NOT_BUILT_IN, NULL_PTR);
4194 TREE_THIS_VOLATILE (temp) = 1;
4195 TREE_SIDE_EFFECTS (temp) = 1;
4196 /* Suppress error if redefined as a non-function. */
4197 DECL_BUILT_IN_NONANSI (temp) = 1;
4199 /* The system prototypes for these functions have many
4200 variations, so don't specify parameters to avoid conflicts.
4201 The expand_* functions check the argument types anyway. */
4202 temp = builtin_function ("bzero", void_ftype_any,
4203 BUILT_IN_BZERO, BUILT_IN_NORMAL, NULL_PTR);
4204 DECL_BUILT_IN_NONANSI (temp) = 1;
4205 temp = builtin_function ("bcmp", int_ftype_any,
4206 BUILT_IN_BCMP, BUILT_IN_NORMAL, NULL_PTR);
4207 DECL_BUILT_IN_NONANSI (temp) = 1;
4210 builtin_function ("__builtin_abs", int_ftype_int, BUILT_IN_ABS,
4211 BUILT_IN_NORMAL, NULL_PTR);
4212 builtin_function ("__builtin_fabsf", float_ftype_float, BUILT_IN_FABS,
4213 BUILT_IN_NORMAL, NULL_PTR);
4214 builtin_function ("__builtin_fabs", double_ftype_double, BUILT_IN_FABS,
4215 BUILT_IN_NORMAL, NULL_PTR);
4216 builtin_function ("__builtin_fabsl", ldouble_ftype_ldouble, BUILT_IN_FABS,
4217 BUILT_IN_NORMAL, NULL_PTR);
4218 builtin_function ("__builtin_labs", long_ftype_long, BUILT_IN_LABS,
4219 BUILT_IN_NORMAL, NULL_PTR);
4220 builtin_function ("__builtin_saveregs", ptr_ftype, BUILT_IN_SAVEREGS,
4221 BUILT_IN_NORMAL, NULL_PTR);
4222 builtin_function ("__builtin_classify_type", default_function_type,
4223 BUILT_IN_CLASSIFY_TYPE, BUILT_IN_NORMAL, NULL_PTR);
4224 builtin_function ("__builtin_next_arg", ptr_ftype, BUILT_IN_NEXT_ARG,
4225 BUILT_IN_NORMAL, NULL_PTR);
4226 builtin_function ("__builtin_args_info", int_ftype_int, BUILT_IN_ARGS_INFO,
4227 BUILT_IN_NORMAL, NULL_PTR);
4228 builtin_function ("__builtin_setjmp",
4229 build_function_type (integer_type_node,
4230 tree_cons (NULL_TREE, ptr_type_node,
4232 BUILT_IN_SETJMP, BUILT_IN_NORMAL, NULL_PTR);
4233 builtin_function ("__builtin_longjmp",
4234 build_function_type (void_type_node,
4235 tree_cons (NULL_TREE, ptr_type_node,
4236 tree_cons (NULL_TREE,
4239 BUILT_IN_LONGJMP, BUILT_IN_NORMAL, NULL_PTR);
4240 builtin_function ("__builtin_trap", void_ftype, BUILT_IN_TRAP,
4241 BUILT_IN_NORMAL, NULL_PTR);
4243 /* ISO C99 IEEE Unordered compares. */
4244 builtin_function ("__builtin_isgreater", default_function_type,
4245 BUILT_IN_ISGREATER, BUILT_IN_NORMAL, NULL_PTR);
4246 builtin_function ("__builtin_isgreaterequal", default_function_type,
4247 BUILT_IN_ISGREATEREQUAL, BUILT_IN_NORMAL, NULL_PTR);
4248 builtin_function ("__builtin_isless", default_function_type,
4249 BUILT_IN_ISLESS, BUILT_IN_NORMAL, NULL_PTR);
4250 builtin_function ("__builtin_islessequal", default_function_type,
4251 BUILT_IN_ISLESSEQUAL, BUILT_IN_NORMAL, NULL_PTR);
4252 builtin_function ("__builtin_islessgreater", default_function_type,
4253 BUILT_IN_ISLESSGREATER, BUILT_IN_NORMAL, NULL_PTR);
4254 builtin_function ("__builtin_isunordered", default_function_type,
4255 BUILT_IN_ISUNORDERED, BUILT_IN_NORMAL, NULL_PTR);
4257 /* Untyped call and return. */
4258 builtin_function ("__builtin_apply_args", ptr_ftype,
4259 BUILT_IN_APPLY_ARGS, BUILT_IN_NORMAL, NULL_PTR);
4261 temp = tree_cons (NULL_TREE,
4262 build_pointer_type (build_function_type (void_type_node,
4264 tree_cons (NULL_TREE,
4266 tree_cons (NULL_TREE,
4269 builtin_function ("__builtin_apply",
4270 build_function_type (ptr_type_node, temp),
4271 BUILT_IN_APPLY, BUILT_IN_NORMAL, NULL_PTR);
4272 builtin_function ("__builtin_return", void_ftype_ptr,
4273 BUILT_IN_RETURN, BUILT_IN_NORMAL, NULL_PTR);
4275 /* Support for varargs.h and stdarg.h. */
4276 builtin_function ("__builtin_varargs_start",
4277 build_function_type (void_type_node,
4278 tree_cons (NULL_TREE,
4279 va_list_ref_type_node,
4281 BUILT_IN_VARARGS_START, BUILT_IN_NORMAL, NULL_PTR);
4283 builtin_function ("__builtin_stdarg_start",
4284 build_function_type (void_type_node,
4285 tree_cons (NULL_TREE,
4286 va_list_ref_type_node,
4288 BUILT_IN_STDARG_START, BUILT_IN_NORMAL, NULL_PTR);
4290 builtin_function ("__builtin_va_end",
4291 build_function_type (void_type_node,
4292 tree_cons (NULL_TREE,
4293 va_list_ref_type_node,
4295 BUILT_IN_VA_END, BUILT_IN_NORMAL, NULL_PTR);
4297 builtin_function ("__builtin_va_copy",
4298 build_function_type (void_type_node,
4299 tree_cons (NULL_TREE,
4300 va_list_ref_type_node,
4301 tree_cons (NULL_TREE,
4302 va_list_arg_type_node,
4304 BUILT_IN_VA_COPY, BUILT_IN_NORMAL, NULL_PTR);
4306 /* ??? Ought to be `T __builtin_expect(T, T)' for any type T. */
4307 builtin_function ("__builtin_expect",
4308 build_function_type (long_integer_type_node,
4309 tree_cons (NULL_TREE,
4310 long_integer_type_node,
4311 tree_cons (NULL_TREE,
4312 long_integer_type_node,
4314 BUILT_IN_EXPECT, BUILT_IN_NORMAL, NULL_PTR);
4316 /* Currently under experimentation. */
4317 builtin_function ("__builtin_memcpy", memcpy_ftype, BUILT_IN_MEMCPY,
4318 BUILT_IN_NORMAL, "memcpy");
4319 builtin_function ("__builtin_memcmp", int_ftype_cptr_cptr_sizet,
4320 BUILT_IN_MEMCMP, BUILT_IN_NORMAL, "memcmp");
4321 builtin_function ("__builtin_memset", memset_ftype,
4322 BUILT_IN_MEMSET, BUILT_IN_NORMAL, "memset");
4323 builtin_function ("__builtin_bzero", bzero_ftype,
4324 BUILT_IN_BZERO, BUILT_IN_NORMAL, "bzero");
4325 builtin_function ("__builtin_bcmp", bcmp_ftype,
4326 BUILT_IN_BCMP, BUILT_IN_NORMAL, "bcmp");
4327 builtin_function ("__builtin_strcmp", int_ftype_string_string,
4328 BUILT_IN_STRCMP, BUILT_IN_NORMAL, "strcmp");
4329 builtin_function ("__builtin_strcpy", string_ftype_ptr_ptr,
4330 BUILT_IN_STRCPY, BUILT_IN_NORMAL, "strcpy");
4331 builtin_function ("__builtin_strlen", strlen_ftype,
4332 BUILT_IN_STRLEN, BUILT_IN_NORMAL, "strlen");
4333 builtin_function ("__builtin_sqrtf", float_ftype_float,
4334 BUILT_IN_FSQRT, BUILT_IN_NORMAL, "sqrtf");
4335 builtin_function ("__builtin_fsqrt", double_ftype_double,
4336 BUILT_IN_FSQRT, BUILT_IN_NORMAL, "sqrt");
4337 builtin_function ("__builtin_sqrtl", ldouble_ftype_ldouble,
4338 BUILT_IN_FSQRT, BUILT_IN_NORMAL, "sqrtl");
4339 builtin_function ("__builtin_sinf", float_ftype_float,
4340 BUILT_IN_SIN, BUILT_IN_NORMAL, "sinf");
4341 builtin_function ("__builtin_sin", double_ftype_double,
4342 BUILT_IN_SIN, BUILT_IN_NORMAL, "sin");
4343 builtin_function ("__builtin_sinl", ldouble_ftype_ldouble,
4344 BUILT_IN_SIN, BUILT_IN_NORMAL, "sinl");
4345 builtin_function ("__builtin_cosf", float_ftype_float,
4346 BUILT_IN_COS, BUILT_IN_NORMAL, "cosf");
4347 builtin_function ("__builtin_cos", double_ftype_double,
4348 BUILT_IN_COS, BUILT_IN_NORMAL, "cos");
4349 builtin_function ("__builtin_cosl", ldouble_ftype_ldouble,
4350 BUILT_IN_COS, BUILT_IN_NORMAL, "cosl");
4351 /* We declare these without argument so that the initial declaration
4352 for these identifiers is a builtin. That allows us to redeclare
4353 them later with argument without worrying about the explicit
4354 declarations in stdio.h being taken as the initial declaration.
4355 Also, save the _DECL for these so we can use them later. */
4356 built_in_decls[BUILT_IN_FPUTC] =
4357 builtin_function ("__builtin_fputc", int_ftype_any,
4358 BUILT_IN_FPUTC, BUILT_IN_NORMAL, "fputc");
4359 built_in_decls[BUILT_IN_FPUTS] =
4360 builtin_function ("__builtin_fputs", int_ftype_any,
4361 BUILT_IN_FPUTS, BUILT_IN_NORMAL, "fputs");
4365 builtin_function ("abs", int_ftype_int, BUILT_IN_ABS,
4366 BUILT_IN_NORMAL, NULL_PTR);
4367 builtin_function ("fabsf", float_ftype_float, BUILT_IN_FABS,
4368 BUILT_IN_NORMAL, NULL_PTR);
4369 builtin_function ("fabs", double_ftype_double, BUILT_IN_FABS,
4370 BUILT_IN_NORMAL, NULL_PTR);
4371 builtin_function ("fabsl", ldouble_ftype_ldouble, BUILT_IN_FABS,
4372 BUILT_IN_NORMAL, NULL_PTR);
4373 builtin_function ("labs", long_ftype_long, BUILT_IN_LABS,
4374 BUILT_IN_NORMAL, NULL_PTR);
4375 builtin_function ("memcpy", memcpy_ftype, BUILT_IN_MEMCPY,
4376 BUILT_IN_NORMAL, NULL_PTR);
4377 builtin_function ("memcmp", int_ftype_cptr_cptr_sizet, BUILT_IN_MEMCMP,
4378 BUILT_IN_NORMAL, NULL_PTR);
4379 builtin_function ("memset", memset_ftype, BUILT_IN_MEMSET,
4380 BUILT_IN_NORMAL, NULL_PTR);
4381 builtin_function ("strcmp", int_ftype_string_string, BUILT_IN_STRCMP,
4382 BUILT_IN_NORMAL, NULL_PTR);
4383 builtin_function ("strcpy", string_ftype_ptr_ptr, BUILT_IN_STRCPY,
4384 BUILT_IN_NORMAL, NULL_PTR);
4385 builtin_function ("strlen", strlen_ftype, BUILT_IN_STRLEN,
4386 BUILT_IN_NORMAL, NULL_PTR);
4387 builtin_function ("sqrtf", float_ftype_float, BUILT_IN_FSQRT,
4388 BUILT_IN_NORMAL, NULL_PTR);
4389 builtin_function ("sqrt", double_ftype_double, BUILT_IN_FSQRT,
4390 BUILT_IN_NORMAL, NULL_PTR);
4391 builtin_function ("sqrtl", ldouble_ftype_ldouble, BUILT_IN_FSQRT,
4392 BUILT_IN_NORMAL, NULL_PTR);
4393 builtin_function ("sinf", float_ftype_float, BUILT_IN_SIN,
4394 BUILT_IN_NORMAL, NULL_PTR);
4395 builtin_function ("sin", double_ftype_double, BUILT_IN_SIN,
4396 BUILT_IN_NORMAL, NULL_PTR);
4397 builtin_function ("sinl", ldouble_ftype_ldouble, BUILT_IN_SIN,
4398 BUILT_IN_NORMAL, NULL_PTR);
4399 builtin_function ("cosf", float_ftype_float, BUILT_IN_COS,
4400 BUILT_IN_NORMAL, NULL_PTR);
4401 builtin_function ("cos", double_ftype_double, BUILT_IN_COS,
4402 BUILT_IN_NORMAL, NULL_PTR);
4403 builtin_function ("cosl", ldouble_ftype_ldouble, BUILT_IN_COS,
4404 BUILT_IN_NORMAL, NULL_PTR);
4405 /* We declare these without argument so that the initial
4406 declaration for these identifiers is a builtin. That allows
4407 us to redeclare them later with argument without worrying
4408 about the explicit declarations in stdio.h being taken as the
4409 initial declaration. */
4410 builtin_function ("fputc", int_ftype_any, BUILT_IN_FPUTC,
4411 BUILT_IN_NORMAL, NULL_PTR);
4412 builtin_function ("fputs", int_ftype_any, BUILT_IN_FPUTS,
4413 BUILT_IN_NORMAL, NULL_PTR);
4415 /* Declare these functions volatile
4416 to avoid spurious "control drops through" warnings. */
4417 temp = builtin_function ("abort", cplus_mode ? void_ftype : void_ftype_any,
4418 0, NOT_BUILT_IN, NULL_PTR);
4419 TREE_THIS_VOLATILE (temp) = 1;
4420 TREE_SIDE_EFFECTS (temp) = 1;
4422 #if 0 /* ??? The C++ frontend used to do this. */
4423 /* Well, these are actually ANSI, but we can't set DECL_BUILT_IN on
4425 DECL_BUILT_IN_NONANSI (temp) = 1;
4427 temp = builtin_function ("exit",
4428 cplus_mode ? void_ftype_int : void_ftype_any,
4429 0, NOT_BUILT_IN, NULL_PTR);
4430 TREE_THIS_VOLATILE (temp) = 1;
4431 TREE_SIDE_EFFECTS (temp) = 1;
4433 #if 0 /* ??? The C++ frontend used to do this. */
4434 /* Well, these are actually ANSI, but we can't set DECL_BUILT_IN on
4436 DECL_BUILT_IN_NONANSI (temp) = 1;
4441 /* Support for these has not been written in either expand_builtin
4442 or build_function_call. */
4443 builtin_function ("__builtin_div", default_ftype, BUILT_IN_DIV,
4444 BUILT_IN_NORMAL, NULL_PTR);
4445 builtin_function ("__builtin_ldiv", default_ftype, BUILT_IN_LDIV,
4446 BUILT_IN_NORMAL, NULL_PTR);
4447 builtin_function ("__builtin_ffloor", double_ftype_double, BUILT_IN_FFLOOR,
4448 BUILT_IN_NORMAL, NULL_PTR);
4449 builtin_function ("__builtin_fceil", double_ftype_double, BUILT_IN_FCEIL,
4450 BUILT_IN_NORMAL, NULL_PTR);
4451 builtin_function ("__builtin_fmod", double_ftype_double_double,
4452 BUILT_IN_FMOD, BUILT_IN_NORMAL, NULL_PTR);
4453 builtin_function ("__builtin_frem", double_ftype_double_double,
4454 BUILT_IN_FREM, BUILT_IN_NORMAL, NULL_PTR);
4455 builtin_function ("__builtin_getexp", double_ftype_double, BUILT_IN_GETEXP,
4456 BUILT_IN_NORMAL, NULL_PTR);
4457 builtin_function ("__builtin_getman", double_ftype_double, BUILT_IN_GETMAN,
4458 BUILT_IN_NORMAL, NULL_PTR);
4461 main_identifier_node = get_identifier ("main");
4463 /* ??? Perhaps there's a better place to do this. But it is related
4464 to __builtin_va_arg, so it isn't that off-the-wall. */
4465 lang_type_promotes_to = simple_type_promotes_to;
4469 build_va_arg (expr, type)
4472 return build1 (VA_ARG_EXPR, type, expr);
4475 /* Given a type, apply default promotions wrt unnamed function arguments
4476 and return the new type. Return NULL_TREE if no change. */
4477 /* ??? There is a function of the same name in the C++ front end that
4478 does something similar, but is more thorough and does not return NULL
4479 if no change. We could perhaps share code, but it would make the
4480 self_promoting_type property harder to identify. */
4483 simple_type_promotes_to (type)
4486 if (TYPE_MAIN_VARIANT (type) == float_type_node)
4487 return double_type_node;
4489 if (C_PROMOTING_INTEGER_TYPE_P (type))
4491 /* Traditionally, unsignedness is preserved in default promotions.
4492 Also preserve unsignedness if not really getting any wider. */
4493 if (TREE_UNSIGNED (type)
4494 && (flag_traditional
4495 || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
4496 return unsigned_type_node;
4497 return integer_type_node;
4503 /* Return 1 if PARMS specifies a fixed number of parameters
4504 and none of their types is affected by default promotions. */
4507 self_promoting_args_p (parms)
4511 for (t = parms; t; t = TREE_CHAIN (t))
4513 register tree type = TREE_VALUE (t);
4515 if (TREE_CHAIN (t) == 0 && type != void_type_node)
4521 if (TYPE_MAIN_VARIANT (type) == float_type_node)
4524 if (C_PROMOTING_INTEGER_TYPE_P (type))
4530 /* Recognize certain built-in functions so we can make tree-codes
4531 other than CALL_EXPR. We do this when it enables fold-const.c
4532 to do something useful. */
4533 /* ??? By rights this should go in builtins.c, but only C and C++
4534 implement build_{binary,unary}_op. Not exactly sure what bits
4535 of functionality are actually needed from those functions, or
4536 where the similar functionality exists in the other front ends. */
4539 expand_tree_builtin (function, params, coerced_params)
4540 tree function, params, coerced_params;
4542 enum tree_code code;
4544 if (DECL_BUILT_IN_CLASS (function) != BUILT_IN_NORMAL)
4547 switch (DECL_FUNCTION_CODE (function))
4552 if (coerced_params == 0)
4553 return integer_zero_node;
4554 return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
4556 case BUILT_IN_ISGREATER:
4557 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
4563 case BUILT_IN_ISGREATEREQUAL:
4564 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
4570 case BUILT_IN_ISLESS:
4571 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
4577 case BUILT_IN_ISLESSEQUAL:
4578 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
4584 case BUILT_IN_ISLESSGREATER:
4585 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
4591 case BUILT_IN_ISUNORDERED:
4592 if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT)
4593 return integer_zero_node;
4594 code = UNORDERED_EXPR;
4602 || TREE_CHAIN (params) == 0)
4604 error ("too few arguments to function `%s'",
4605 IDENTIFIER_POINTER (DECL_NAME (function)));
4606 return error_mark_node;
4608 else if (TREE_CHAIN (TREE_CHAIN (params)) != 0)
4610 error ("too many arguments to function `%s'",
4611 IDENTIFIER_POINTER (DECL_NAME (function)));
4612 return error_mark_node;
4615 arg0 = TREE_VALUE (params);
4616 arg1 = TREE_VALUE (TREE_CHAIN (params));
4617 arg0 = build_binary_op (code, arg0, arg1, 0);
4618 if (code != UNORDERED_EXPR)
4619 arg0 = build_unary_op (TRUTH_NOT_EXPR, arg0, 0);
4631 /* Returns non-zero if CODE is the code for a statement. */
4634 statement_code_p (code)
4635 enum tree_code code;
4658 if (lang_statement_code_p)
4659 return (*lang_statement_code_p) (code);
4664 /* Walk the statemen tree, rooted at *tp. Apply FUNC to all the
4665 sub-trees of *TP in a pre-order traversal. FUNC is called with the
4666 DATA and the address of each sub-tree. If FUNC returns a non-NULL
4667 value, the traversal is aborted, and the value returned by FUNC is
4668 returned. If FUNC sets WALK_SUBTREES to zero, then the subtrees of
4669 the node being visited are not walked.
4671 We don't need a without_duplicates variant of this one because the
4672 statement tree is a tree, not a graph. */
4675 walk_stmt_tree (tp, func, data)
4680 enum tree_code code;
4685 #define WALK_SUBTREE(NODE) \
4688 result = walk_stmt_tree (&(NODE), func, data); \
4694 /* Skip empty subtrees. */
4698 /* Skip subtrees below non-statement nodes. */
4699 if (!statement_code_p (TREE_CODE (*tp)))
4702 /* Call the function. */
4704 result = (*func) (tp, &walk_subtrees, data);
4706 /* If we found something, return it. */
4710 /* Even if we didn't, FUNC may have decided that there was nothing
4711 interesting below this point in the tree. */
4715 /* FUNC may have modified the tree, recheck that we're looking at a
4717 code = TREE_CODE (*tp);
4718 if (!statement_code_p (code))
4721 /* Walk over all the sub-trees of this operand. Statement nodes never
4722 contain RTL, and we needn't worry about TARGET_EXPRs. */
4723 len = TREE_CODE_LENGTH (code);
4725 /* Go through the subtrees. We need to do this in forward order so
4726 that the scope of a FOR_EXPR is handled properly. */
4727 for (i = 0; i < len; ++i)
4728 WALK_SUBTREE (TREE_OPERAND (*tp, i));
4730 /* Finally visit the chain. This can be tail-recursion optimized if
4731 we write it this way. */
4732 return walk_stmt_tree (&TREE_CHAIN (*tp), func, data);
4737 /* Used to compare case labels. K1 and K2 are actually tree nodes
4738 representing case labels, or NULL_TREE for a `default' label.
4739 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
4740 K2, and 0 if K1 and K2 are equal. */
4743 case_compare (k1, k2)
4747 /* Consider a NULL key (such as arises with a `default' label) to be
4748 smaller than anything else. */
4754 return tree_int_cst_compare ((tree) k1, (tree) k2);
4757 /* Process a case label for the range LOW_VALUE ... HIGH_VALUE. If
4758 LOW_VALUE and HIGH_VALUE are both NULL_TREE then this case label is
4759 actually a `default' label. If only HIGH_VALUE is NULL_TREE, then
4760 case label was declared using the usual C/C++ syntax, rather than
4761 the GNU case range extension. CASES is a tree containing all the
4762 case ranges processed so far; COND is the condition for the
4763 switch-statement itself. Returns the CASE_LABEL created, or
4764 ERROR_MARK_NODE if no CASE_LABEL is created. */
4767 c_add_case_label (cases, cond, low_value, high_value)
4776 splay_tree_node node;
4778 /* Create the LABEL_DECL itself. */
4779 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
4780 DECL_CONTEXT (label) = current_function_decl;
4782 /* If there was an error processing the switch condition, bail now
4783 before we get more confused. */
4784 if (!cond || cond == error_mark_node)
4786 /* Add a label anyhow so that the back-end doesn't think that
4787 the beginning of the switch is unreachable. */
4789 add_stmt (build_case_label (NULL_TREE, NULL_TREE, label));
4790 return error_mark_node;
4793 if ((low_value && TREE_TYPE (low_value)
4794 && POINTER_TYPE_P (TREE_TYPE (low_value)))
4795 || (high_value && TREE_TYPE (high_value)
4796 && POINTER_TYPE_P (TREE_TYPE (high_value))))
4797 error ("pointers are not permitted as case values");
4799 /* Case ranges are a GNU extension. */
4800 if (high_value && pedantic)
4802 if (c_language == clk_cplusplus)
4803 pedwarn ("ISO C++ forbids range expressions in switch statements");
4805 pedwarn ("ISO C forbids range expressions in switch statements");
4808 type = TREE_TYPE (cond);
4811 low_value = check_case_value (low_value);
4812 low_value = convert_and_check (type, low_value);
4816 high_value = check_case_value (high_value);
4817 high_value = convert_and_check (type, high_value);
4820 /* If an error has occurred, bail out now. */
4821 if (low_value == error_mark_node || high_value == error_mark_node)
4824 add_stmt (build_case_label (NULL_TREE, NULL_TREE, label));
4825 return error_mark_node;
4828 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
4829 really a case range, even though it was written that way. Remove
4830 the HIGH_VALUE to simplify later processing. */
4831 if (tree_int_cst_equal (low_value, high_value))
4832 high_value = NULL_TREE;
4833 if (low_value && high_value
4834 && !tree_int_cst_lt (low_value, high_value))
4835 warning ("empty range specified");
4837 /* Look up the LOW_VALUE in the table of case labels we already
4839 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
4840 /* If there was not an exact match, check for overlapping ranges.
4841 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
4842 that's a `default' label and the only overlap is an exact match. */
4843 if (!node && (low_value || high_value))
4845 splay_tree_node low_bound;
4846 splay_tree_node high_bound;
4848 /* Even though there wasn't an exact match, there might be an
4849 overlap between this case range and another case range.
4850 Since we've (inductively) not allowed any overlapping case
4851 ranges, we simply need to find the greatest low case label
4852 that is smaller that LOW_VALUE, and the smallest low case
4853 label that is greater than LOW_VALUE. If there is an overlap
4854 it will occur in one of these two ranges. */
4855 low_bound = splay_tree_predecessor (cases,
4856 (splay_tree_key) low_value);
4857 high_bound = splay_tree_successor (cases,
4858 (splay_tree_key) low_value);
4860 /* Check to see if the LOW_BOUND overlaps. It is smaller than
4861 the LOW_VALUE, so there is no need to check unless the
4862 LOW_BOUND is in fact itself a case range. */
4864 && CASE_HIGH ((tree) low_bound->value)
4865 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
4868 /* Check to see if the HIGH_BOUND overlaps. The low end of that
4869 range is bigger than the low end of the current range, so we
4870 are only interested if the current range is a real range, and
4871 not an ordinary case label. */
4874 && (tree_int_cst_compare ((tree) high_bound->key,
4879 /* If there was an overlap, issue an error. */
4882 tree duplicate = CASE_LABEL_DECL ((tree) node->value);
4886 error ("duplicate (or overlapping) case value");
4887 error_with_decl (duplicate,
4888 "this is the first entry overlapping that value");
4892 error ("duplicate case value") ;
4893 error_with_decl (duplicate, "previously used here");
4897 error ("multiple default labels in one switch");
4898 error_with_decl (duplicate, "this is the first default label");
4901 add_stmt (build_case_label (NULL_TREE, NULL_TREE, label));
4904 /* Add a CASE_LABEL to the statement-tree. */
4905 case_label = add_stmt (build_case_label (low_value, high_value, label));
4906 /* Register this case label in the splay tree. */
4907 splay_tree_insert (cases,
4908 (splay_tree_key) low_value,
4909 (splay_tree_value) case_label);
4914 /* Mark P (a stmt_tree) for GC. The use of a `void *' for the
4915 parameter allows this function to be used as a GC-marking
4922 stmt_tree st = (stmt_tree) p;
4924 ggc_mark_tree (st->x_last_stmt);
4925 ggc_mark_tree (st->x_last_expr_type);
4928 /* Mark LD for GC. */
4931 c_mark_lang_decl (c)
4932 struct c_lang_decl *c;
4934 ggc_mark_tree (c->saved_tree);
4937 /* Mark F for GC. */
4940 mark_c_language_function (f)
4941 struct language_function *f;
4946 mark_stmt_tree (&f->x_stmt_tree);
4947 ggc_mark_tree (f->x_scope_stmt_stack);
4950 /* Hook used by expand_expr to expand language-specific tree codes. */
4953 c_expand_expr (exp, target, tmode, modifier)
4956 enum machine_mode tmode;
4957 enum expand_modifier modifier;
4959 switch (TREE_CODE (exp))
4966 /* Since expand_expr_stmt calls free_temp_slots after every
4967 expression statement, we must call push_temp_slots here.
4968 Otherwise, any temporaries in use now would be considered
4969 out-of-scope after the first EXPR_STMT from within the
4972 rtl_expr = expand_start_stmt_expr ();
4973 expand_stmt (STMT_EXPR_STMT (exp));
4974 expand_end_stmt_expr (rtl_expr);
4975 result = expand_expr (rtl_expr, target, tmode, modifier);
4989 /* Hook used by safe_from_p to handle language-specific tree codes. */
4992 c_safe_from_p (target, exp)
4996 /* We can see statements here when processing the body of a
4997 statement-expression. For a declaration statement declaring a
4998 variable, look at the variable's initializer. */
4999 if (TREE_CODE (exp) == DECL_STMT)
5001 tree decl = DECL_STMT_DECL (exp);
5003 if (TREE_CODE (decl) == VAR_DECL
5004 && DECL_INITIAL (decl)
5005 && !safe_from_p (target, DECL_INITIAL (decl), /*top_p=*/0))
5009 /* For any statement, we must follow the statement-chain. */
5010 if (statement_code_p (TREE_CODE (exp)) && TREE_CHAIN (exp))
5011 return safe_from_p (target, TREE_CHAIN (exp), /*top_p=*/0);
5013 /* Assume everything else is safe. */
5017 /* Tree code classes. */
5019 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
5021 static char c_tree_code_type[] = {
5023 #include "c-common.def"
5027 /* Table indexed by tree code giving number of expression
5028 operands beyond the fixed part of the node structure.
5029 Not used for types or decls. */
5031 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
5033 static int c_tree_code_length[] = {
5035 #include "c-common.def"
5039 /* Names of tree components.
5040 Used for printing out the tree and error messages. */
5041 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
5043 static const char *c_tree_code_name[] = {
5045 #include "c-common.def"
5049 /* Adds the tree codes specific to the C front end to the list of all
5055 memcpy (tree_code_type + (int) LAST_AND_UNUSED_TREE_CODE,
5057 (int)LAST_C_TREE_CODE - (int)LAST_AND_UNUSED_TREE_CODE);
5058 memcpy (tree_code_length + (int) LAST_AND_UNUSED_TREE_CODE,
5060 (LAST_C_TREE_CODE - (int)LAST_AND_UNUSED_TREE_CODE) * sizeof (int));
5061 memcpy (tree_code_name + (int) LAST_AND_UNUSED_TREE_CODE,
5063 (LAST_C_TREE_CODE - (int)LAST_AND_UNUSED_TREE_CODE) * sizeof (char *));