Only create gcc/configargs.h if gcc build directory is present
[official-gcc.git] / gcc / c-common.c
blob3b88f187118946c86b3357210932a53f9394bfd9
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)
10 any later version.
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. */
22 #include "config.h"
23 #include "system.h"
24 #include "tree.h"
25 #include "flags.h"
26 #include "toplev.h"
27 #include "output.h"
28 #include "c-pragma.h"
29 #include "rtl.h"
30 #include "ggc.h"
31 #include "expr.h"
32 #include "c-common.h"
33 #include "tm_p.h"
34 #include "intl.h"
35 #include "diagnostic.h"
37 #if USE_CPPLIB
38 #include "cpplib.h"
39 cpp_reader parse_in;
40 #endif
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;
66 tree wchar_type_node;
67 tree signed_wchar_type_node;
68 tree unsigned_wchar_type_node;
70 tree float_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;
79 tree intQI_type_node;
80 tree intHI_type_node;
81 tree intSI_type_node;
82 tree intDI_type_node;
83 tree intTI_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.
123 tree int_ftype_int;
124 tree void_ftype;
125 tree void_ftype_ptr;
126 tree int_ftype_int;
127 tree ptr_ftype_sizetype;
129 A VOID_TYPE node, packaged in a TREE_LIST.
131 tree void_list_node;
133 The identifiers __FUNCTION__, __PRETTY_FUNCTION__, and __func__.
135 tree function_id_node;
136 tree pretty_function_id_node;
137 tree func_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. */
145 tree *ridpointers;
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. */
159 int skip_evaluation;
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 *,
171 int, int, int));
172 static void init_attributes PARAMS ((void));
173 static void record_function_format PARAMS ((tree, tree, enum format_type,
174 int, int));
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. */
183 typedef struct
185 int compstmt_count;
186 int line;
187 const char *file;
188 int needs_warning;
189 tree if_stmt;
190 } if_elt;
192 static if_elt *if_stack;
194 /* Amount of space in the if statement stack. */
195 static int if_stack_space = 0;
197 /* Stack pointer. */
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. */
203 void
204 c_expand_start_cond (cond, compstmt_count)
205 tree cond;
206 int compstmt_count;
208 tree if_stmt;
210 /* Make sure there is enough space on the stack. */
211 if (if_stack_space == 0)
213 if_stack_space = 10;
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;
224 add_stmt (if_stmt);
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;
232 if_stack_pointer++;
235 /* Called after the then-clause for an if-statement is processed. */
237 void
238 c_finish_then ()
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. */
247 void
248 c_expand_end_cond ()
250 if_stack_pointer--;
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. */
261 void
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. */
266 if (warn_parentheses
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. */
281 void
282 c_finish_else ()
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__. */
290 void
291 declare_function_name ()
293 const char *name, *printable_name;
295 if (current_function_decl == NULL)
297 name = "";
298 printable_name = "top level";
300 else
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));
305 else
306 name = "";
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. */
322 tree
323 combine_strings (strings)
324 tree strings;
326 register tree value, t;
327 register int length = 1;
328 int wide_length = 0;
329 int wide_flag = 0;
330 int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
331 int nchars;
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);
347 wide_flag = 1;
349 else
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. */
355 if (wide_flag)
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. */
364 q = p;
365 for (t = strings; t; t = TREE_CHAIN (t))
367 int len = (TREE_STRING_LENGTH (t)
368 - ((TREE_TYPE (t) == wchar_array_type_node)
369 ? wchar_bytes : 1));
370 if ((TREE_TYPE (t) == wchar_array_type_node) == wide_flag)
372 memcpy (q, TREE_STRING_POINTER (t), len);
373 q += len;
375 else
377 int i;
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];
382 else
383 ((int *) q)[i] = TREE_STRING_POINTER (t)[i];
385 q += len * wchar_bytes;
388 if (wide_flag)
390 int i;
391 for (i = 0; i < wchar_bytes; i++)
392 *q++ = 0;
394 else
395 *q = 0;
397 value = make_node (STRING_CST);
398 TREE_STRING_POINTER (value) = p;
399 TREE_STRING_LENGTH (value) = length;
401 else
403 value = strings;
404 length = TREE_STRING_LENGTH (value);
405 if (TREE_TYPE (value) == wchar_array_type_node)
406 wide_flag = 1;
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))
423 tree elements
424 = build_type_variant (wide_flag ? wchar_type_node : char_type_node,
425 1, 0);
426 TREE_TYPE (value)
427 = build_array_type (elements,
428 build_index_type (build_int_2 (nchars - 1, 0)));
430 else
431 TREE_TYPE (value)
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;
438 return value;
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. */
452 static void
453 add_attribute (id, string, min_len, max_len, decl_req)
454 enum attrs id;
455 const char *string;
456 int min_len, max_len;
457 int decl_req;
459 char buf[100];
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. */
478 static void
479 init_attributes ()
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. */
508 static int
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;
515 return 0;
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. */
529 void
530 decl_attributes (node, attributes, prefix_attributes)
531 tree node, attributes, prefix_attributes;
533 tree decl = 0, type = 0;
534 int is_type = 0;
535 tree a;
537 if (attrtab_idx == 0)
538 init_attributes ();
540 if (DECL_P (node))
542 decl = node;
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);
555 #endif
557 #ifdef INSERT_ATTRIBUTES
558 INSERT_ATTRIBUTES (node, & attributes, & prefix_attributes);
559 #endif
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);
567 int i;
568 enum attrs id;
570 for (i = 0; i < attrtab_idx; i++)
571 if (attrtab[i].name == name)
572 break;
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));
580 else if (decl != 0)
581 type = TREE_TYPE (decl);
582 continue;
584 else if (attrtab[i].decl_req && decl == 0)
586 warning ("`%s' attribute does not apply to types",
587 IDENTIFIER_POINTER (name));
588 continue;
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));
595 continue;
598 id = attrtab[i].id;
599 switch (id)
601 case A_PACKED:
602 if (is_type)
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. */
608 else
609 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
610 break;
612 case A_NOCOMMON:
613 if (TREE_CODE (decl) == VAR_DECL)
614 DECL_COMMON (decl) = 0;
615 else
616 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
617 break;
619 case A_COMMON:
620 if (TREE_CODE (decl) == VAR_DECL)
621 DECL_COMMON (decl) = 1;
622 else
623 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
624 break;
626 case A_NORETURN:
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
632 = build_pointer_type
633 (build_type_variant (TREE_TYPE (type),
634 TREE_READONLY (TREE_TYPE (type)), 1));
635 else
636 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
637 break;
639 case A_MALLOC:
640 if (TREE_CODE (decl) == FUNCTION_DECL)
641 DECL_IS_MALLOC (decl) = 1;
642 /* ??? TODO: Support types. */
643 else
644 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
645 break;
647 case A_UNUSED:
648 if (is_type)
649 if (decl)
650 TREE_USED (decl) = 1;
651 else
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;
658 else
659 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
660 break;
662 case A_CONST:
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
668 = build_pointer_type
669 (build_type_variant (TREE_TYPE (type), 1,
670 TREE_THIS_VOLATILE (TREE_TYPE (type))));
671 else
672 warning ( "`%s' attribute ignored", IDENTIFIER_POINTER (name));
673 break;
675 case A_PURE:
676 if (TREE_CODE (decl) == FUNCTION_DECL)
677 DECL_IS_PURE (decl) = 1;
678 /* ??? TODO: Support types. */
679 else
680 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
681 break;
684 case A_T_UNION:
685 if (is_type
686 && TREE_CODE (type) == UNION_TYPE
687 && (decl == 0
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;
695 else
696 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
697 break;
699 case A_CONSTRUCTOR:
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;
707 else
708 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
709 break;
711 case A_DESTRUCTOR:
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;
719 else
720 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
721 break;
723 case A_MODE:
724 if (TREE_CODE (TREE_VALUE (args)) != IDENTIFIER_NODE)
725 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
726 else
728 int j;
729 const char *p = IDENTIFIER_POINTER (TREE_VALUE (args));
730 int len = strlen (p);
731 enum machine_mode mode = VOIDmode;
732 tree typefm;
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';
741 p = newp;
744 /* Give this decl a type with the specified mode.
745 First check for the special modes. */
746 if (! strcmp (p, "byte"))
747 mode = byte_mode;
748 else if (!strcmp (p, "word"))
749 mode = word_mode;
750 else if (! strcmp (p, "pointer"))
751 mode = ptr_mode;
752 else
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);
762 else
764 if (TYPE_PRECISION (typefm) > (TREE_UNSIGNED (type)
765 ? TYPE_PRECISION(uintmax_type_node)
766 : TYPE_PRECISION(intmax_type_node))
767 && pedantic)
768 pedwarn ("type with more precision than %s",
769 TREE_UNSIGNED (type) ? "uintmax_t" : "intmax_t");
770 TREE_TYPE (decl) = type = typefm;
771 DECL_SIZE (decl) = DECL_SIZE_UNIT (decl) = 0;
772 layout_decl (decl, 0);
775 break;
777 case A_SECTION:
778 #ifdef ASM_OUTPUT_SECTION_NAME
779 if ((TREE_CODE (decl) == FUNCTION_DECL
780 || TREE_CODE (decl) == VAR_DECL)
781 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
783 if (TREE_CODE (decl) == VAR_DECL
784 && current_function_decl != NULL_TREE
785 && ! TREE_STATIC (decl))
786 error_with_decl (decl,
787 "section attribute cannot be specified for local variables");
788 /* The decl may have already been given a section attribute from
789 a previous declaration. Ensure they match. */
790 else if (DECL_SECTION_NAME (decl) != NULL_TREE
791 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
792 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
793 error_with_decl (node,
794 "section of `%s' conflicts with previous declaration");
795 else
796 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
798 else
799 error_with_decl (node,
800 "section attribute not allowed for `%s'");
801 #else
802 error_with_decl (node,
803 "section attributes are not supported for this target");
804 #endif
805 break;
807 case A_ALIGNED:
809 tree align_expr
810 = (args ? TREE_VALUE (args)
811 : size_int (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
812 int i;
814 /* Strip any NOPs of any kind. */
815 while (TREE_CODE (align_expr) == NOP_EXPR
816 || TREE_CODE (align_expr) == CONVERT_EXPR
817 || TREE_CODE (align_expr) == NON_LVALUE_EXPR)
818 align_expr = TREE_OPERAND (align_expr, 0);
820 if (TREE_CODE (align_expr) != INTEGER_CST)
822 error ("requested alignment is not a constant");
823 continue;
826 if ((i = tree_log2 (align_expr)) == -1)
827 error ("requested alignment is not a power of 2");
828 else if (i > HOST_BITS_PER_INT - 2)
829 error ("requested alignment is too large");
830 else if (is_type)
832 /* If we have a TYPE_DECL, then copy the type, so that we
833 don't accidentally modify a builtin type. See pushdecl. */
834 if (decl && TREE_TYPE (decl) != error_mark_node
835 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
837 tree tt = TREE_TYPE (decl);
838 DECL_ORIGINAL_TYPE (decl) = tt;
839 tt = build_type_copy (tt);
840 TYPE_NAME (tt) = decl;
841 TREE_USED (tt) = TREE_USED (decl);
842 TREE_TYPE (decl) = tt;
843 type = tt;
846 TYPE_ALIGN (type) = (1 << i) * BITS_PER_UNIT;
847 TYPE_USER_ALIGN (type) = 1;
849 else if (TREE_CODE (decl) != VAR_DECL
850 && TREE_CODE (decl) != FIELD_DECL)
851 error_with_decl (decl,
852 "alignment may not be specified for `%s'");
853 else
855 DECL_ALIGN (decl) = (1 << i) * BITS_PER_UNIT;
856 DECL_USER_ALIGN (decl) = 1;
859 break;
861 case A_FORMAT:
863 tree format_type_id = TREE_VALUE (args);
864 tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
865 tree first_arg_num_expr
866 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
867 unsigned HOST_WIDE_INT format_num, first_arg_num;
868 enum format_type format_type;
869 tree argument;
870 unsigned int arg_num;
872 if (TREE_CODE (decl) != FUNCTION_DECL)
874 error_with_decl (decl,
875 "argument format specified for non-function `%s'");
876 continue;
879 if (TREE_CODE (format_type_id) != IDENTIFIER_NODE)
881 error ("unrecognized format specifier");
882 continue;
884 else
886 const char *p = IDENTIFIER_POINTER (format_type_id);
888 if (!strcmp (p, "printf") || !strcmp (p, "__printf__"))
889 format_type = printf_format_type;
890 else if (!strcmp (p, "scanf") || !strcmp (p, "__scanf__"))
891 format_type = scanf_format_type;
892 else if (!strcmp (p, "strftime")
893 || !strcmp (p, "__strftime__"))
894 format_type = strftime_format_type;
895 else
897 warning ("`%s' is an unrecognized format function type", p);
898 continue;
902 /* Strip any conversions from the string index and first arg number
903 and verify they are constants. */
904 while (TREE_CODE (format_num_expr) == NOP_EXPR
905 || TREE_CODE (format_num_expr) == CONVERT_EXPR
906 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
907 format_num_expr = TREE_OPERAND (format_num_expr, 0);
909 while (TREE_CODE (first_arg_num_expr) == NOP_EXPR
910 || TREE_CODE (first_arg_num_expr) == CONVERT_EXPR
911 || TREE_CODE (first_arg_num_expr) == NON_LVALUE_EXPR)
912 first_arg_num_expr = TREE_OPERAND (first_arg_num_expr, 0);
914 if (TREE_CODE (format_num_expr) != INTEGER_CST
915 || TREE_INT_CST_HIGH (format_num_expr) != 0
916 || TREE_CODE (first_arg_num_expr) != INTEGER_CST
917 || TREE_INT_CST_HIGH (first_arg_num_expr) != 0)
919 error ("format string has invalid operand number");
920 continue;
923 format_num = TREE_INT_CST_LOW (format_num_expr);
924 first_arg_num = TREE_INT_CST_LOW (first_arg_num_expr);
925 if (first_arg_num != 0 && first_arg_num <= format_num)
927 error ("format string arg follows the args to be formatted");
928 continue;
931 /* If a parameter list is specified, verify that the format_num
932 argument is actually a string, in case the format attribute
933 is in error. */
934 argument = TYPE_ARG_TYPES (type);
935 if (argument)
937 for (arg_num = 1; argument != 0 && arg_num != format_num;
938 ++arg_num, argument = TREE_CHAIN (argument))
941 if (! argument
942 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
943 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
944 != char_type_node))
946 error ("format string arg not a string type");
947 continue;
950 else if (first_arg_num != 0)
952 /* Verify that first_arg_num points to the last arg,
953 the ... */
954 while (argument)
955 arg_num++, argument = TREE_CHAIN (argument);
957 if (arg_num != first_arg_num)
959 error ("args to be formatted is not '...'");
960 continue;
965 if (format_type == strftime_format_type && first_arg_num != 0)
967 error ("strftime formats cannot format arguments");
968 continue;
971 record_function_format (DECL_NAME (decl),
972 DECL_ASSEMBLER_NAME (decl),
973 format_type, format_num, first_arg_num);
974 break;
977 case A_FORMAT_ARG:
979 tree format_num_expr = TREE_VALUE (args);
980 unsigned HOST_WIDE_INT format_num;
981 unsigned int arg_num;
982 tree argument;
984 if (TREE_CODE (decl) != FUNCTION_DECL)
986 error_with_decl (decl,
987 "argument format specified for non-function `%s'");
988 continue;
991 /* Strip any conversions from the first arg number and verify it
992 is a constant. */
993 while (TREE_CODE (format_num_expr) == NOP_EXPR
994 || TREE_CODE (format_num_expr) == CONVERT_EXPR
995 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
996 format_num_expr = TREE_OPERAND (format_num_expr, 0);
998 if (TREE_CODE (format_num_expr) != INTEGER_CST
999 || TREE_INT_CST_HIGH (format_num_expr) != 0)
1001 error ("format string has invalid operand number");
1002 continue;
1005 format_num = TREE_INT_CST_LOW (format_num_expr);
1007 /* If a parameter list is specified, verify that the format_num
1008 argument is actually a string, in case the format attribute
1009 is in error. */
1010 argument = TYPE_ARG_TYPES (type);
1011 if (argument)
1013 for (arg_num = 1; argument != 0 && arg_num != format_num;
1014 ++arg_num, argument = TREE_CHAIN (argument))
1017 if (! argument
1018 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
1019 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
1020 != char_type_node))
1022 error ("format string arg not a string type");
1023 continue;
1027 if (TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) != POINTER_TYPE
1028 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_TYPE (decl))))
1029 != char_type_node))
1031 error ("function does not return string type");
1032 continue;
1035 record_international_format (DECL_NAME (decl),
1036 DECL_ASSEMBLER_NAME (decl),
1037 format_num);
1038 break;
1041 case A_WEAK:
1042 declare_weak (decl);
1043 break;
1045 case A_ALIAS:
1046 if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
1047 || (TREE_CODE (decl) != FUNCTION_DECL && ! DECL_EXTERNAL (decl)))
1048 error_with_decl (decl,
1049 "`%s' defined both normally and as an alias");
1050 else if (decl_function_context (decl) == 0)
1052 tree id;
1054 id = TREE_VALUE (args);
1055 if (TREE_CODE (id) != STRING_CST)
1057 error ("alias arg not a string");
1058 break;
1060 id = get_identifier (TREE_STRING_POINTER (id));
1061 /* This counts as a use of the object pointed to. */
1062 TREE_USED (id) = 1;
1064 if (TREE_CODE (decl) == FUNCTION_DECL)
1065 DECL_INITIAL (decl) = error_mark_node;
1066 else
1067 DECL_EXTERNAL (decl) = 0;
1068 assemble_alias (decl, id);
1070 else
1071 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
1072 break;
1074 case A_NO_CHECK_MEMORY_USAGE:
1075 if (TREE_CODE (decl) != FUNCTION_DECL)
1077 error_with_decl (decl,
1078 "`%s' attribute applies only to functions",
1079 IDENTIFIER_POINTER (name));
1081 else if (DECL_INITIAL (decl))
1083 error_with_decl (decl,
1084 "can't set `%s' attribute after definition",
1085 IDENTIFIER_POINTER (name));
1087 else
1088 DECL_NO_CHECK_MEMORY_USAGE (decl) = 1;
1089 break;
1091 case A_NO_INSTRUMENT_FUNCTION:
1092 if (TREE_CODE (decl) != FUNCTION_DECL)
1094 error_with_decl (decl,
1095 "`%s' attribute applies only to functions",
1096 IDENTIFIER_POINTER (name));
1098 else if (DECL_INITIAL (decl))
1100 error_with_decl (decl,
1101 "can't set `%s' attribute after definition",
1102 IDENTIFIER_POINTER (name));
1104 else
1105 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1106 break;
1108 case A_NO_LIMIT_STACK:
1109 if (TREE_CODE (decl) != FUNCTION_DECL)
1111 error_with_decl (decl,
1112 "`%s' attribute applies only to functions",
1113 IDENTIFIER_POINTER (name));
1115 else if (DECL_INITIAL (decl))
1117 error_with_decl (decl,
1118 "can't set `%s' attribute after definition",
1119 IDENTIFIER_POINTER (name));
1121 else
1122 DECL_NO_LIMIT_STACK (decl) = 1;
1123 break;
1128 /* Split SPECS_ATTRS, a list of declspecs and prefix attributes, into two
1129 lists. SPECS_ATTRS may also be just a typespec (eg: RECORD_TYPE).
1131 The head of the declspec list is stored in DECLSPECS.
1132 The head of the attribute list is stored in PREFIX_ATTRIBUTES.
1134 Note that attributes in SPECS_ATTRS are stored in the TREE_PURPOSE of
1135 the list elements. We drop the containing TREE_LIST nodes and link the
1136 resulting attributes together the way decl_attributes expects them. */
1138 void
1139 split_specs_attrs (specs_attrs, declspecs, prefix_attributes)
1140 tree specs_attrs;
1141 tree *declspecs, *prefix_attributes;
1143 tree t, s, a, next, specs, attrs;
1145 /* This can happen after an __extension__ in pedantic mode. */
1146 if (specs_attrs != NULL_TREE
1147 && TREE_CODE (specs_attrs) == INTEGER_CST)
1149 *declspecs = NULL_TREE;
1150 *prefix_attributes = NULL_TREE;
1151 return;
1154 /* This can happen in c++ (eg: decl: typespec initdecls ';'). */
1155 if (specs_attrs != NULL_TREE
1156 && TREE_CODE (specs_attrs) != TREE_LIST)
1158 *declspecs = specs_attrs;
1159 *prefix_attributes = NULL_TREE;
1160 return;
1163 /* Remember to keep the lists in the same order, element-wise. */
1165 specs = s = NULL_TREE;
1166 attrs = a = NULL_TREE;
1167 for (t = specs_attrs; t; t = next)
1169 next = TREE_CHAIN (t);
1170 /* Declspecs have a non-NULL TREE_VALUE. */
1171 if (TREE_VALUE (t) != NULL_TREE)
1173 if (specs == NULL_TREE)
1174 specs = s = t;
1175 else
1177 TREE_CHAIN (s) = t;
1178 s = t;
1181 else
1183 if (attrs == NULL_TREE)
1184 attrs = a = TREE_PURPOSE (t);
1185 else
1187 TREE_CHAIN (a) = TREE_PURPOSE (t);
1188 a = TREE_PURPOSE (t);
1190 /* More attrs can be linked here, move A to the end. */
1191 while (TREE_CHAIN (a) != NULL_TREE)
1192 a = TREE_CHAIN (a);
1196 /* Terminate the lists. */
1197 if (s != NULL_TREE)
1198 TREE_CHAIN (s) = NULL_TREE;
1199 if (a != NULL_TREE)
1200 TREE_CHAIN (a) = NULL_TREE;
1202 /* All done. */
1203 *declspecs = specs;
1204 *prefix_attributes = attrs;
1207 /* Strip attributes from SPECS_ATTRS, a list of declspecs and attributes.
1208 This function is used by the parser when a rule will accept attributes
1209 in a particular position, but we don't want to support that just yet.
1211 A warning is issued for every ignored attribute. */
1213 tree
1214 strip_attrs (specs_attrs)
1215 tree specs_attrs;
1217 tree specs, attrs;
1219 split_specs_attrs (specs_attrs, &specs, &attrs);
1221 while (attrs)
1223 warning ("`%s' attribute ignored",
1224 IDENTIFIER_POINTER (TREE_PURPOSE (attrs)));
1225 attrs = TREE_CHAIN (attrs);
1228 return specs;
1231 /* Check a printf/fprintf/sprintf/scanf/fscanf/sscanf format against
1232 a parameter list. */
1234 /* The meaningfully distinct length modifiers for format checking recognised
1235 by GCC. */
1236 enum format_lengths
1238 FMT_LEN_none,
1239 FMT_LEN_hh,
1240 FMT_LEN_h,
1241 FMT_LEN_l,
1242 FMT_LEN_ll,
1243 FMT_LEN_L,
1244 FMT_LEN_z,
1245 FMT_LEN_t,
1246 FMT_LEN_j,
1247 FMT_LEN_MAX
1251 /* The standard versions in which various format features appeared. */
1252 enum format_std_version
1254 STD_C89,
1255 STD_C94,
1256 STD_C99,
1257 STD_EXT
1261 /* Flags that may apply to a particular kind of format checked by GCC. */
1262 enum
1264 /* This format converts arguments of types determined by the
1265 format string. */
1266 FMT_FLAG_ARG_CONVERT = 1,
1267 /* The scanf allocation 'a' kludge applies to this format kind. */
1268 FMT_FLAG_SCANF_A_KLUDGE = 2,
1269 /* A % during parsing a specifier is allowed to be a modified % rather
1270 that indicating the format is broken and we are out-of-sync. */
1271 FMT_FLAG_FANCY_PERCENT_OK = 4
1272 /* Not included here: details of whether width or precision may occur
1273 (controlled by width_char and precision_char); details of whether
1274 '*' can be used for these (width_type and precision_type); details
1275 of whether length modifiers can occur (length_char_specs); details
1276 of when $ operand numbers are allowed (always, for the formats
1277 supported, if arguments are converted). */
1281 /* Structure describing a length modifier supported in format checking, and
1282 possibly a doubled version such as "hh". */
1283 typedef struct
1285 /* Name of the single-character length modifier. */
1286 const char *name;
1287 /* Index into a format_char_info.types array. */
1288 enum format_lengths index;
1289 /* Standard version this length appears in. */
1290 enum format_std_version std;
1291 /* Same, if the modifier can be repeated, or NULL if it can't. */
1292 const char *double_name;
1293 enum format_lengths double_index;
1294 enum format_std_version double_std;
1295 } format_length_info;
1298 /* Structure desribing the combination of a conversion specifier
1299 (or a set of specifiers which act identically) and a length modifier. */
1300 typedef struct
1302 /* The standard version this combination of length and type appeared in.
1303 This is only relevant if greater than those for length and type
1304 individually; otherwise it is ignored. */
1305 enum format_std_version std;
1306 /* The name to use for the type, if different from that generated internally
1307 (e.g., "signed size_t"). */
1308 const char *name;
1309 /* The type itself. */
1310 tree *type;
1311 } format_type_detail;
1314 /* Macros to fill out tables of these. */
1315 #define BADLEN { 0, NULL, NULL }
1316 #define NOLENGTHS { BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }
1319 /* Structure desribing a format conversion specifier (or a set of specifiers
1320 which act identically), and the length modifiers used with it. */
1321 typedef struct
1323 const char *format_chars;
1324 int pointer_count;
1325 enum format_std_version std;
1326 /* Types accepted for each length modifier. */
1327 format_type_detail types[FMT_LEN_MAX];
1328 /* List of other modifier characters allowed with these specifiers.
1329 This lists flags, and additionally "w" for width, "p" for precision,
1330 "a" for scanf "a" allocation extension (not applicable in C99 mode),
1331 "*" for scanf suppression, and "E" and "O" for those strftime
1332 modifiers. */
1333 const char *flag_chars;
1334 /* List of additional flags describing these conversion specifiers.
1335 "c" for generic character pointers being allowed, "2" for strftime
1336 two digit year formats, "3" for strftime formats giving two digit
1337 years in some locales, "4" for "2" which becomes "3" with an "E" modifier,
1338 "o" if use of strftime "O" is a GNU extension beyond C99,
1339 "W" if the argument is a pointer which is dereferenced and written into,
1340 "i" for printf integer formats where the '0' flag is ignored with
1341 precision, and "[" for the starting character of a scanf scanset. */
1342 const char *flags2;
1343 } format_char_info;
1346 /* Structure describing a flag accepted by some kind of format. */
1347 typedef struct
1349 /* The flag character in question (0 for end of array). */
1350 int flag_char;
1351 /* Zero if this entry describes the flag character in general, or a
1352 non-zero character that may be found in flags2 if it describes the
1353 flag when used with certain formats only. If the latter, only
1354 the first such entry found that applies to the current conversion
1355 specifier is used; the values of `name' and `long_name' it supplies
1356 will be used, if non-NULL and the standard version is higher than
1357 the unpredicated one, for any pedantic warning. For example, 'o'
1358 for strftime formats (meaning 'O' is an extension over C99). */
1359 int predicate;
1360 /* The name to use for this flag in diagnostic messages. For example,
1361 N_("`0' flag"), N_("field width"). */
1362 const char *name;
1363 /* Long name for this flag in diagnostic messages; currently only used for
1364 "ISO C does not support ...". For example, N_("the `I' printf flag"). */
1365 const char *long_name;
1366 /* The standard version in which it appeared. */
1367 enum format_std_version std;
1368 } format_flag_spec;
1371 /* Structure describing a combination of flags that is bad for some kind
1372 of format. */
1373 typedef struct
1375 /* The first flag character in question (0 for end of array). */
1376 int flag_char1;
1377 /* The second flag character. */
1378 int flag_char2;
1379 /* Non-zero if the message should say that the first flag is ignored with
1380 the second, zero if the combination should simply be objected to. */
1381 int ignored;
1382 /* Zero if this entry applies whenever this flag combination occurs,
1383 a non-zero character from flags2 if it only applies in some
1384 circumstances (e.g. 'i' for printf formats ignoring 0 with precision). */
1385 int predicate;
1386 } format_flag_pair;
1389 /* Structure describing a particular kind of format processed by GCC. */
1390 typedef struct
1392 /* The name of this kind of format, for use in diagnostics. */
1393 const char *name;
1394 /* Specifications of the length modifiers accepted; possibly NULL. */
1395 const format_length_info *length_char_specs;
1396 /* Details of the conversion specification characters accepted. */
1397 const format_char_info *conversion_specs;
1398 /* String listing the flag characters that are accepted. */
1399 const char *flag_chars;
1400 /* String listing modifier characters (strftime) accepted. May be NULL. */
1401 const char *modifier_chars;
1402 /* Details of the flag characters, including pseudo-flags. */
1403 const format_flag_spec *flag_specs;
1404 /* Details of bad combinations of flags. */
1405 const format_flag_pair *bad_flag_pairs;
1406 /* Flags applicable to this kind of format. */
1407 int flags;
1408 /* Flag character to treat a width as, or 0 if width not used. */
1409 int width_char;
1410 /* Flag character to treat a precision as, or 0 if precision not used. */
1411 int precision_char;
1412 /* If a flag character has the effect of suppressing the conversion of
1413 an argument ('*' in scanf), that flag character, otherwise 0. */
1414 int suppression_char;
1415 /* Flag character to treat a length modifier as (ignored if length
1416 modifiers not used). Need not be placed in flag_chars for conversion
1417 specifiers, but is used to check for bad combinations such as length
1418 modifier with assignment suppression in scanf. */
1419 int length_code_char;
1420 /* Pointer to type of argument expected if '*' is used for a width,
1421 or NULL if '*' not used for widths. */
1422 tree *width_type;
1423 /* Pointer to type of argument expected if '*' is used for a precision,
1424 or NULL if '*' not used for precisions. */
1425 tree *precision_type;
1426 } format_kind_info;
1429 /* Structure describing details of a type expected in format checking,
1430 and the type to check against it. */
1431 typedef struct format_wanted_type
1433 /* The type wanted. */
1434 tree wanted_type;
1435 /* The name of this type to use in diagnostics. */
1436 const char *wanted_type_name;
1437 /* The level of indirection through pointers at which this type occurs. */
1438 int pointer_count;
1439 /* Whether, when pointer_count is 1, to allow any character type when
1440 pedantic, rather than just the character or void type specified. */
1441 int char_lenient_flag;
1442 /* Whether the argument, dereferenced once, is written into and so the
1443 argument must not be a pointer to a const-qualified type. */
1444 int writing_in_flag;
1445 /* If warnings should be of the form "field precision is not type int",
1446 the name to use (in this case "field precision"), otherwise NULL,
1447 for "%s format, %s arg" type messages. If (in an extension), this
1448 is a pointer type, wanted_type_name should be set to include the
1449 terminating '*' characters of the type name to give a correct
1450 message. */
1451 const char *name;
1452 /* The actual parameter to check against the wanted type. */
1453 tree param;
1454 /* The argument number of that parameter. */
1455 int arg_num;
1456 /* The next type to check for this format conversion, or NULL if none. */
1457 struct format_wanted_type *next;
1458 } format_wanted_type;
1461 static const format_length_info printf_length_specs[] =
1463 { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99 },
1464 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C99 },
1465 { "q", FMT_LEN_ll, STD_EXT, NULL, 0, 0 },
1466 { "L", FMT_LEN_L, STD_C89, NULL, 0, 0 },
1467 { "z", FMT_LEN_z, STD_C99, NULL, 0, 0 },
1468 { "Z", FMT_LEN_z, STD_EXT, NULL, 0, 0 },
1469 { "t", FMT_LEN_t, STD_C99, NULL, 0, 0 },
1470 { "j", FMT_LEN_j, STD_C99, NULL, 0, 0 },
1471 { NULL, 0, 0, NULL, 0, 0 }
1475 /* This differs from printf_length_specs only in that "Z" is not accepted. */
1476 static const format_length_info scanf_length_specs[] =
1478 { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99 },
1479 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C99 },
1480 { "q", FMT_LEN_ll, STD_EXT, NULL, 0, 0 },
1481 { "L", FMT_LEN_L, STD_C89, NULL, 0, 0 },
1482 { "z", FMT_LEN_z, STD_C99, NULL, 0, 0 },
1483 { "t", FMT_LEN_t, STD_C99, NULL, 0, 0 },
1484 { "j", FMT_LEN_j, STD_C99, NULL, 0, 0 },
1485 { NULL, 0, 0, NULL, 0, 0 }
1489 static const format_flag_spec printf_flag_specs[] =
1491 { ' ', 0, N_("` ' flag"), N_("the ` ' printf flag"), STD_C89 },
1492 { '+', 0, N_("`+' flag"), N_("the `+' printf flag"), STD_C89 },
1493 { '#', 0, N_("`#' flag"), N_("the `#' printf flag"), STD_C89 },
1494 { '0', 0, N_("`0' flag"), N_("the `0' printf flag"), STD_C89 },
1495 { '-', 0, N_("`-' flag"), N_("the `-' printf flag"), STD_C89 },
1496 { '\'', 0, N_("`'' flag"), N_("the `'' printf flag"), STD_EXT },
1497 { 'I', 0, N_("`I' flag"), N_("the `I' printf flag"), STD_EXT },
1498 { 'w', 0, N_("field width"), N_("field width in printf format"), STD_C89 },
1499 { 'p', 0, N_("precision"), N_("precision in printf format"), STD_C89 },
1500 { 'L', 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
1501 { 0, 0, NULL, NULL, 0 }
1505 static const format_flag_pair printf_flag_pairs[] =
1507 { ' ', '+', 1, 0 },
1508 { '0', '-', 1, 0 },
1509 { '0', 'p', 1, 'i' },
1510 { 0, 0, 0, 0 }
1514 static const format_flag_spec scanf_flag_specs[] =
1516 { '*', 0, N_("assignment suppression"), N_("assignment suppression"), STD_C89 },
1517 { 'a', 0, N_("`a' flag"), N_("the `a' scanf flag"), STD_EXT },
1518 { 'w', 0, N_("field width"), N_("field width in scanf format"), STD_C89 },
1519 { 'L', 0, N_("length modifier"), N_("length modifier in scanf format"), STD_C89 },
1520 { '\'', 0, N_("`'' flag"), N_("the `'' scanf flag"), STD_EXT },
1521 { 'I', 0, N_("`I' flag"), N_("the `I' scanf flag"), STD_EXT },
1522 { 0, 0, NULL, NULL, 0 }
1526 static const format_flag_pair scanf_flag_pairs[] =
1528 { '*', 'L', 0, 0 },
1529 { 0, 0, 0, 0 }
1533 static const format_flag_spec strftime_flag_specs[] =
1535 { '_', 0, N_("`_' flag"), N_("the `_' strftime flag"), STD_EXT },
1536 { '-', 0, N_("`-' flag"), N_("the `-' strftime flag"), STD_EXT },
1537 { '0', 0, N_("`0' flag"), N_("the `0' strftime flag"), STD_EXT },
1538 { '^', 0, N_("`^' flag"), N_("the `^' strftime flag"), STD_EXT },
1539 { '#', 0, N_("`#' flag"), N_("the `#' strftime flag"), STD_EXT },
1540 { 'w', 0, N_("field width"), N_("field width in strftime format"), STD_EXT },
1541 { 'E', 0, N_("`E' modifier"), N_("the `E' strftime modifier"), STD_C99 },
1542 { 'O', 0, N_("`O' modifier"), N_("the `O' strftime modifier"), STD_C99 },
1543 { 'O', 'o', NULL, N_("the `O' modifier"), STD_EXT },
1544 { 0, 0, NULL, NULL, 0 }
1548 static const format_flag_pair strftime_flag_pairs[] =
1550 { 'E', 'O', 0, 0 },
1551 { '_', '-', 0, 0 },
1552 { '_', '0', 0, 0 },
1553 { '-', '0', 0, 0 },
1554 { '^', '#', 0, 0 },
1555 { 0, 0, 0, 0 }
1559 #define T_I &integer_type_node
1560 #define T89_I { STD_C89, NULL, T_I }
1561 #define T99_I { STD_C99, NULL, T_I }
1562 #define T_L &long_integer_type_node
1563 #define T89_L { STD_C89, NULL, T_L }
1564 #define T_LL &long_long_integer_type_node
1565 #define T99_LL { STD_C99, NULL, T_LL }
1566 #define TEX_LL { STD_EXT, NULL, T_LL }
1567 #define T_S &short_integer_type_node
1568 #define T89_S { STD_C89, NULL, T_S }
1569 #define T_UI &unsigned_type_node
1570 #define T89_UI { STD_C89, NULL, T_UI }
1571 #define T99_UI { STD_C99, NULL, T_UI }
1572 #define T_UL &long_unsigned_type_node
1573 #define T89_UL { STD_C89, NULL, T_UL }
1574 #define T_ULL &long_long_unsigned_type_node
1575 #define T99_ULL { STD_C99, NULL, T_ULL }
1576 #define TEX_ULL { STD_EXT, NULL, T_ULL }
1577 #define T_US &short_unsigned_type_node
1578 #define T89_US { STD_C89, NULL, T_US }
1579 #define T_F &float_type_node
1580 #define T89_F { STD_C89, NULL, T_F }
1581 #define T99_F { STD_C99, NULL, T_F }
1582 #define T_D &double_type_node
1583 #define T89_D { STD_C89, NULL, T_D }
1584 #define T99_D { STD_C99, NULL, T_D }
1585 #define T_LD &long_double_type_node
1586 #define T89_LD { STD_C89, NULL, T_LD }
1587 #define T99_LD { STD_C99, NULL, T_LD }
1588 #define T_C &char_type_node
1589 #define T89_C { STD_C89, NULL, T_C }
1590 #define T_SC &signed_char_type_node
1591 #define T99_SC { STD_C99, NULL, T_SC }
1592 #define T_UC &unsigned_char_type_node
1593 #define T99_UC { STD_C99, NULL, T_UC }
1594 #define T_V &void_type_node
1595 #define T89_V { STD_C89, NULL, T_V }
1596 #define T_W &wchar_type_node
1597 #define T94_W { STD_C94, "wchar_t", T_W }
1598 #define TEX_W { STD_EXT, "wchar_t", T_W }
1599 #define T_WI &wint_type_node
1600 #define T94_WI { STD_C94, "wint_t", T_WI }
1601 #define TEX_WI { STD_EXT, "wint_t", T_WI }
1602 #define T_ST &c_size_type_node
1603 #define T99_ST { STD_C99, "size_t", T_ST }
1604 #define T_SST &signed_size_type_node
1605 #define T99_SST { STD_C99, "signed size_t", T_SST }
1606 #define T_PD &ptrdiff_type_node
1607 #define T99_PD { STD_C99, "ptrdiff_t", T_PD }
1608 #define T_UPD &unsigned_ptrdiff_type_node
1609 #define T99_UPD { STD_C99, "unsigned ptrdiff_t", T_UPD }
1610 #define T_IM &intmax_type_node
1611 #define T99_IM { STD_C99, "intmax_t", T_IM }
1612 #define T_UIM &uintmax_type_node
1613 #define T99_UIM { STD_C99, "uintmax_t", T_UIM }
1615 static const format_char_info print_char_table[] =
1617 /* C89 conversion specifiers. */
1618 { "di", 0, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T99_LL, TEX_LL, T99_SST, T99_PD, T99_IM }, "-wp0 +'I", "i" },
1619 { "oxX", 0, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T99_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM }, "-wp0#", "i" },
1620 { "u", 0, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T99_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM }, "-wp0'I", "i" },
1621 { "fgG", 0, STD_C89, { T89_D, BADLEN, BADLEN, T99_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN }, "-wp0 +#'", "" },
1622 { "eE", 0, STD_C89, { T89_D, BADLEN, BADLEN, T99_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN }, "-wp0 +#", "" },
1623 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, T94_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "" },
1624 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "c" },
1625 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "c" },
1626 { "n", 1, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T99_LL, BADLEN, T99_SST, T99_PD, T99_IM }, "", "W" },
1627 /* C99 conversion specifiers. */
1628 { "F", 0, STD_C99, { T99_D, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN }, "-wp0 +#'", "" },
1629 { "aA", 0, STD_C99, { T99_D, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN }, "-wp0 +#", "" },
1630 /* X/Open conversion specifiers. */
1631 { "C", 0, STD_EXT, { TEX_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "" },
1632 { "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "" },
1633 /* GNU conversion specifiers. */
1634 { "m", 0, STD_EXT, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "" },
1635 { NULL, 0, 0, NOLENGTHS, NULL, NULL }
1638 static const format_char_info scan_char_table[] =
1640 /* C89 conversion specifiers. */
1641 { "di", 1, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T99_LL, TEX_LL, T99_SST, T99_PD, T99_IM }, "*w'I", "W" },
1642 { "u", 1, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T99_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM }, "*w'I", "W" },
1643 { "oxX", 1, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T99_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM }, "*w", "W" },
1644 { "efgEG", 1, STD_C89, { T89_F, BADLEN, BADLEN, T89_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN }, "*w'", "W" },
1645 { "c", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "cW" },
1646 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*aw", "cW" },
1647 { "[", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*aw", "cW[" },
1648 { "p", 2, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "W" },
1649 { "n", 1, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T99_LL, BADLEN, T99_SST, T99_PD, T99_IM }, "", "W" },
1650 /* C99 conversion specifiers. */
1651 { "FaA", 1, STD_C99, { T99_F, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN }, "*w'", "W" },
1652 /* X/Open conversion specifiers. */
1653 { "C", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "W" },
1654 { "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*aw", "W" },
1655 { NULL, 0, 0, NOLENGTHS, NULL, NULL }
1658 static format_char_info time_char_table[] =
1660 /* C89 conversion specifiers. */
1661 { "ABZab", 0, STD_C89, NOLENGTHS, "^#", "" },
1662 { "cx", 0, STD_C89, NOLENGTHS, "E", "3" },
1663 { "HIMSUWdmw", 0, STD_C89, NOLENGTHS, "-_0Ow", "" },
1664 { "j", 0, STD_C89, NOLENGTHS, "-_0Ow", "o" },
1665 { "p", 0, STD_C89, NOLENGTHS, "#", "" },
1666 { "X", 0, STD_C89, NOLENGTHS, "E", "" },
1667 { "y", 0, STD_C89, NOLENGTHS, "EO-_0w", "4" },
1668 { "Y", 0, STD_C89, NOLENGTHS, "-_0EOw", "o" },
1669 { "%", 0, STD_C89, NOLENGTHS, "", "" },
1670 /* C99 conversion specifiers. */
1671 { "C", 0, STD_C99, NOLENGTHS, "-_0EOw", "o" },
1672 { "D", 0, STD_C99, NOLENGTHS, "", "2" },
1673 { "eVu", 0, STD_C99, NOLENGTHS, "-_0Ow", "" },
1674 { "FRTnrt", 0, STD_C99, NOLENGTHS, "", "" },
1675 { "g", 0, STD_C99, NOLENGTHS, "O-_0w", "2o" },
1676 { "G", 0, STD_C99, NOLENGTHS, "-_0Ow", "o" },
1677 { "h", 0, STD_C99, NOLENGTHS, "^#", "" },
1678 { "z", 0, STD_C99, NOLENGTHS, "O", "o" },
1679 /* GNU conversion specifiers. */
1680 { "kls", 0, STD_EXT, NOLENGTHS, "-_0Ow", "" },
1681 { "P", 0, STD_EXT, NOLENGTHS, "", "" },
1682 { NULL, 0, 0, NOLENGTHS, NULL, NULL }
1686 /* This must be in the same order as enum format_type. */
1687 static const format_kind_info format_types[] =
1689 { "printf", printf_length_specs, print_char_table, " +#0-'I", NULL,
1690 printf_flag_specs, printf_flag_pairs,
1691 FMT_FLAG_ARG_CONVERT, 'w', 'p', 0, 'L',
1692 &integer_type_node, &integer_type_node
1694 { "scanf", scanf_length_specs, scan_char_table, "*'I", NULL,
1695 scanf_flag_specs, scanf_flag_pairs,
1696 FMT_FLAG_ARG_CONVERT|FMT_FLAG_SCANF_A_KLUDGE, 'w', 0, '*', 'L',
1697 NULL, NULL
1699 { "strftime", NULL, time_char_table, "_-0^#", "EO",
1700 strftime_flag_specs, strftime_flag_pairs,
1701 FMT_FLAG_FANCY_PERCENT_OK, 'w', 0, 0, 0,
1702 NULL, NULL
1707 typedef struct function_format_info
1709 struct function_format_info *next; /* next structure on the list */
1710 tree name; /* identifier such as "printf" */
1711 tree assembler_name; /* optional mangled identifier (for C++) */
1712 enum format_type format_type; /* type of format (printf, scanf, etc.) */
1713 int format_num; /* number of format argument */
1714 int first_arg_num; /* number of first arg (zero for varargs) */
1715 } function_format_info;
1717 static function_format_info *function_format_list = NULL;
1719 typedef struct international_format_info
1721 struct international_format_info *next; /* next structure on the list */
1722 tree name; /* identifier such as "gettext" */
1723 tree assembler_name; /* optional mangled identifier (for C++) */
1724 int format_num; /* number of format argument */
1725 } international_format_info;
1727 static international_format_info *international_format_list = NULL;
1729 /* Structure detailing the results of checking a format function call
1730 where the format expression may be a conditional expression with
1731 many leaves resulting from nested conditional expressions. */
1732 typedef struct
1734 /* Number of leaves of the format argument that could not be checked
1735 as they were not string literals. */
1736 int number_non_literal;
1737 /* Number of leaves of the format argument that were null pointers or
1738 string literals, but had extra format arguments. */
1739 int number_extra_args;
1740 /* Number of leaves of the format argument that were null pointers or
1741 string literals, but had extra format arguments and used $ operand
1742 numbers. */
1743 int number_dollar_extra_args;
1744 /* Number of leaves of the format argument that were wide string
1745 literals. */
1746 int number_wide;
1747 /* Number of leaves of the format argument that were empty strings. */
1748 int number_empty;
1749 /* Number of leaves of the format argument that were unterminated
1750 strings. */
1751 int number_unterminated;
1752 /* Number of leaves of the format argument that were not counted above. */
1753 int number_other;
1754 } format_check_results;
1756 static void check_format_info PARAMS ((int *, function_format_info *, tree));
1757 static void check_format_info_recurse PARAMS ((int *, format_check_results *,
1758 function_format_info *, tree,
1759 tree, int));
1760 static void check_format_info_main PARAMS ((int *, format_check_results *,
1761 function_format_info *,
1762 const char *, int, tree, int));
1763 static void status_warning PARAMS ((int *, const char *, ...))
1764 ATTRIBUTE_PRINTF_2;
1766 static void init_dollar_format_checking PARAMS ((int, tree));
1767 static int maybe_read_dollar_number PARAMS ((int *, const char **, int,
1768 tree, tree *));
1769 static void finish_dollar_format_checking PARAMS ((int *, format_check_results *));
1771 static const format_flag_spec *get_flag_spec PARAMS ((const format_flag_spec *,
1772 int, const char *));
1774 static void check_format_types PARAMS ((int *, format_wanted_type *));
1775 static int is_valid_printf_arglist PARAMS ((tree));
1776 static rtx c_expand_builtin PARAMS ((tree, rtx, enum machine_mode, enum expand_modifier));
1777 static rtx c_expand_builtin_printf PARAMS ((tree, rtx, enum machine_mode,
1778 enum expand_modifier, int));
1780 /* Initialize the table of functions to perform format checking on.
1781 The ISO C functions are always checked (whether <stdio.h> is
1782 included or not), since it is common to call printf without
1783 including <stdio.h>. There shouldn't be a problem with this,
1784 since ISO C reserves these function names whether you include the
1785 header file or not. In any case, the checking is harmless. With
1786 -ffreestanding, these default attributes are disabled, and must be
1787 specified manually if desired.
1789 Also initialize the name of function that modify the format string for
1790 internationalization purposes. */
1792 void
1793 init_function_format_info ()
1795 if (flag_hosted)
1797 /* Functions from ISO/IEC 9899:1990. */
1798 record_function_format (get_identifier ("printf"), NULL_TREE,
1799 printf_format_type, 1, 2);
1800 record_function_format (get_identifier ("__builtin_printf"), NULL_TREE,
1801 printf_format_type, 1, 2);
1802 record_function_format (get_identifier ("fprintf"), NULL_TREE,
1803 printf_format_type, 2, 3);
1804 record_function_format (get_identifier ("sprintf"), NULL_TREE,
1805 printf_format_type, 2, 3);
1806 record_function_format (get_identifier ("scanf"), NULL_TREE,
1807 scanf_format_type, 1, 2);
1808 record_function_format (get_identifier ("fscanf"), NULL_TREE,
1809 scanf_format_type, 2, 3);
1810 record_function_format (get_identifier ("sscanf"), NULL_TREE,
1811 scanf_format_type, 2, 3);
1812 record_function_format (get_identifier ("vprintf"), NULL_TREE,
1813 printf_format_type, 1, 0);
1814 record_function_format (get_identifier ("vfprintf"), NULL_TREE,
1815 printf_format_type, 2, 0);
1816 record_function_format (get_identifier ("vsprintf"), NULL_TREE,
1817 printf_format_type, 2, 0);
1818 record_function_format (get_identifier ("strftime"), NULL_TREE,
1819 strftime_format_type, 3, 0);
1822 if (flag_hosted && flag_isoc99)
1824 /* ISO C99 adds the snprintf and vscanf family functions. */
1825 record_function_format (get_identifier ("snprintf"), NULL_TREE,
1826 printf_format_type, 3, 4);
1827 record_function_format (get_identifier ("vsnprintf"), NULL_TREE,
1828 printf_format_type, 3, 0);
1829 record_function_format (get_identifier ("vscanf"), NULL_TREE,
1830 scanf_format_type, 1, 0);
1831 record_function_format (get_identifier ("vfscanf"), NULL_TREE,
1832 scanf_format_type, 2, 0);
1833 record_function_format (get_identifier ("vsscanf"), NULL_TREE,
1834 scanf_format_type, 2, 0);
1837 if (flag_hosted && flag_noniso_default_format_attributes)
1839 /* Uniforum/GNU gettext functions, not in ISO C. */
1840 record_international_format (get_identifier ("gettext"), NULL_TREE, 1);
1841 record_international_format (get_identifier ("dgettext"), NULL_TREE, 2);
1842 record_international_format (get_identifier ("dcgettext"), NULL_TREE, 2);
1846 /* Record information for argument format checking. FUNCTION_IDENT is
1847 the identifier node for the name of the function to check (its decl
1848 need not exist yet).
1849 FORMAT_TYPE specifies the type of format checking. FORMAT_NUM is the number
1850 of the argument which is the format control string (starting from 1).
1851 FIRST_ARG_NUM is the number of the first actual argument to check
1852 against the format string, or zero if no checking is not be done
1853 (e.g. for varargs such as vfprintf). */
1855 static void
1856 record_function_format (name, assembler_name, format_type,
1857 format_num, first_arg_num)
1858 tree name;
1859 tree assembler_name;
1860 enum format_type format_type;
1861 int format_num;
1862 int first_arg_num;
1864 function_format_info *info;
1866 /* Re-use existing structure if it's there. */
1868 for (info = function_format_list; info; info = info->next)
1870 if (info->name == name && info->assembler_name == assembler_name)
1871 break;
1873 if (! info)
1875 info = (function_format_info *) xmalloc (sizeof (function_format_info));
1876 info->next = function_format_list;
1877 function_format_list = info;
1879 info->name = name;
1880 info->assembler_name = assembler_name;
1883 info->format_type = format_type;
1884 info->format_num = format_num;
1885 info->first_arg_num = first_arg_num;
1888 /* Record information for the names of function that modify the format
1889 argument to format functions. FUNCTION_IDENT is the identifier node for
1890 the name of the function (its decl need not exist yet) and FORMAT_NUM is
1891 the number of the argument which is the format control string (starting
1892 from 1). */
1894 static void
1895 record_international_format (name, assembler_name, format_num)
1896 tree name;
1897 tree assembler_name;
1898 int format_num;
1900 international_format_info *info;
1902 /* Re-use existing structure if it's there. */
1904 for (info = international_format_list; info; info = info->next)
1906 if (info->name == name && info->assembler_name == assembler_name)
1907 break;
1910 if (! info)
1912 info
1913 = (international_format_info *)
1914 xmalloc (sizeof (international_format_info));
1915 info->next = international_format_list;
1916 international_format_list = info;
1918 info->name = name;
1919 info->assembler_name = assembler_name;
1922 info->format_num = format_num;
1925 /* Check the argument list of a call to printf, scanf, etc.
1926 NAME is the function identifier.
1927 ASSEMBLER_NAME is the function's assembler identifier.
1928 (Either NAME or ASSEMBLER_NAME, but not both, may be NULL_TREE.)
1929 PARAMS is the list of argument values. */
1931 void
1932 check_function_format (status, name, assembler_name, params)
1933 int *status;
1934 tree name;
1935 tree assembler_name;
1936 tree params;
1938 function_format_info *info;
1940 /* See if this function is a format function. */
1941 for (info = function_format_list; info; info = info->next)
1943 if (info->assembler_name
1944 ? (info->assembler_name == assembler_name)
1945 : (info->name == name))
1947 /* Yup; check it. */
1948 check_format_info (status, info, params);
1949 break;
1954 /* This function replaces `warning' inside the printf format checking
1955 functions. If the `status' parameter is non-NULL, then it is
1956 dereferenced and set to 1 whenever a warning is caught. Otherwise
1957 it warns as usual by replicating the innards of the warning
1958 function from diagnostic.c. */
1959 static void
1960 status_warning VPARAMS ((int *status, const char *msgid, ...))
1962 #ifndef ANSI_PROTOTYPES
1963 int *status;
1964 const char *msgid;
1965 #endif
1966 va_list ap;
1967 diagnostic_context dc;
1969 if (status)
1970 *status = 1;
1971 else
1973 VA_START (ap, msgid);
1975 #ifndef ANSI_PROTOTYPES
1976 status = va_arg (ap, int *);
1977 msgid = va_arg (ap, const char *);
1978 #endif
1980 /* This duplicates the warning function behavior. */
1981 set_diagnostic_context
1982 (&dc, msgid, &ap, input_filename, lineno, /* warn = */ 1);
1983 report_diagnostic (&dc);
1985 va_end (ap);
1989 /* Variables used by the checking of $ operand number formats. */
1990 static char *dollar_arguments_used = NULL;
1991 static int dollar_arguments_alloc = 0;
1992 static int dollar_arguments_count;
1993 static int dollar_first_arg_num;
1994 static int dollar_max_arg_used;
1995 static int dollar_format_warned;
1997 /* Initialize the checking for a format string that may contain $
1998 parameter number specifications; we will need to keep track of whether
1999 each parameter has been used. FIRST_ARG_NUM is the number of the first
2000 argument that is a parameter to the format, or 0 for a vprintf-style
2001 function; PARAMS is the list of arguments starting at this argument. */
2003 static void
2004 init_dollar_format_checking (first_arg_num, params)
2005 int first_arg_num;
2006 tree params;
2008 dollar_first_arg_num = first_arg_num;
2009 dollar_arguments_count = 0;
2010 dollar_max_arg_used = 0;
2011 dollar_format_warned = 0;
2012 if (first_arg_num > 0)
2014 while (params)
2016 dollar_arguments_count++;
2017 params = TREE_CHAIN (params);
2020 if (dollar_arguments_alloc < dollar_arguments_count)
2022 if (dollar_arguments_used)
2023 free (dollar_arguments_used);
2024 dollar_arguments_alloc = dollar_arguments_count;
2025 dollar_arguments_used = xmalloc (dollar_arguments_alloc);
2027 if (dollar_arguments_alloc)
2028 memset (dollar_arguments_used, 0, dollar_arguments_alloc);
2032 /* Look for a decimal number followed by a $ in *FORMAT. If DOLLAR_NEEDED
2033 is set, it is an error if one is not found; otherwise, it is OK. If
2034 such a number is found, check whether it is within range and mark that
2035 numbered operand as being used for later checking. Returns the operand
2036 number if found and within range, zero if no such number was found and
2037 this is OK, or -1 on error. PARAMS points to the first operand of the
2038 format; PARAM_PTR is made to point to the parameter referred to. If
2039 a $ format is found, *FORMAT is updated to point just after it. */
2041 static int
2042 maybe_read_dollar_number (status, format, dollar_needed, params, param_ptr)
2043 int *status;
2044 const char **format;
2045 int dollar_needed;
2046 tree params;
2047 tree *param_ptr;
2049 int argnum;
2050 int overflow_flag;
2051 const char *fcp = *format;
2052 if (*fcp < '0' || *fcp > '9')
2054 if (dollar_needed)
2056 status_warning (status, "missing $ operand number in format");
2057 return -1;
2059 else
2060 return 0;
2062 argnum = 0;
2063 overflow_flag = 0;
2064 while (*fcp >= '0' && *fcp <= '9')
2066 int nargnum;
2067 nargnum = 10 * argnum + (*fcp - '0');
2068 if (nargnum < 0 || nargnum / 10 != argnum)
2069 overflow_flag = 1;
2070 argnum = nargnum;
2071 fcp++;
2073 if (*fcp != '$')
2075 if (dollar_needed)
2077 status_warning (status, "missing $ operand number in format");
2078 return -1;
2080 else
2081 return 0;
2083 *format = fcp + 1;
2084 if (pedantic && !dollar_format_warned)
2086 status_warning (status, "ISO C does not support %%n$ operand number formats");
2087 dollar_format_warned = 1;
2089 if (overflow_flag || argnum == 0
2090 || (dollar_first_arg_num && argnum > dollar_arguments_count))
2092 status_warning (status, "operand number out of range in format");
2093 return -1;
2095 if (argnum > dollar_max_arg_used)
2096 dollar_max_arg_used = argnum;
2097 /* For vprintf-style functions we may need to allocate more memory to
2098 track which arguments are used. */
2099 while (dollar_arguments_alloc < dollar_max_arg_used)
2101 int nalloc;
2102 nalloc = 2 * dollar_arguments_alloc + 16;
2103 dollar_arguments_used = xrealloc (dollar_arguments_used, nalloc);
2104 memset (dollar_arguments_used + dollar_arguments_alloc, 0,
2105 nalloc - dollar_arguments_alloc);
2106 dollar_arguments_alloc = nalloc;
2108 dollar_arguments_used[argnum - 1] = 1;
2109 if (dollar_first_arg_num)
2111 int i;
2112 *param_ptr = params;
2113 for (i = 1; i < argnum && *param_ptr != 0; i++)
2114 *param_ptr = TREE_CHAIN (*param_ptr);
2116 if (*param_ptr == 0)
2118 /* This case shouldn't be caught here. */
2119 abort ();
2122 else
2123 *param_ptr = 0;
2124 return argnum;
2128 /* Finish the checking for a format string that used $ operand number formats
2129 instead of non-$ formats. We check for unused operands before used ones
2130 (a serious error, since the implementation of the format function
2131 can't know what types to pass to va_arg to find the later arguments).
2132 and for unused operands at the end of the format (if we know how many
2133 arguments the format had, so not for vprintf). If there were operand
2134 numbers out of range on a non-vprintf-style format, we won't have reached
2135 here. */
2137 static void
2138 finish_dollar_format_checking (status, res)
2139 int *status;
2140 format_check_results *res;
2142 int i;
2143 for (i = 0; i < dollar_max_arg_used; i++)
2145 if (!dollar_arguments_used[i])
2146 status_warning (status, "format argument %d unused before used argument %d in $-style format",
2147 i + 1, dollar_max_arg_used);
2149 if (dollar_first_arg_num && dollar_max_arg_used < dollar_arguments_count)
2151 res->number_other--;
2152 res->number_dollar_extra_args++;
2157 /* Retrieve the specification for a format flag. SPEC contains the
2158 specifications for format flags for the applicable kind of format.
2159 FLAG is the flag in question. If PREDICATES is NULL, the basic
2160 spec for that flag must be retrieved and this function aborts if
2161 it cannot be found. If PREDICATES is not NULL, it is a string listing
2162 possible predicates for the spec entry; if an entry predicated on any
2163 of these is found, it is returned, otherwise NULL is returned. */
2165 static const format_flag_spec *
2166 get_flag_spec (spec, flag, predicates)
2167 const format_flag_spec *spec;
2168 int flag;
2169 const char *predicates;
2171 int i;
2172 for (i = 0; spec[i].flag_char != 0; i++)
2174 if (spec[i].flag_char != flag)
2175 continue;
2176 if (predicates != NULL)
2178 if (spec[i].predicate != 0
2179 && index (predicates, spec[i].predicate) != 0)
2180 return &spec[i];
2182 else if (spec[i].predicate == 0)
2183 return &spec[i];
2185 if (predicates == NULL)
2186 abort ();
2187 else
2188 return NULL;
2192 /* Check the argument list of a call to printf, scanf, etc.
2193 INFO points to the function_format_info structure.
2194 PARAMS is the list of argument values. */
2196 static void
2197 check_format_info (status, info, params)
2198 int *status;
2199 function_format_info *info;
2200 tree params;
2202 int arg_num;
2203 tree format_tree;
2204 format_check_results res;
2205 /* Skip to format argument. If the argument isn't available, there's
2206 no work for us to do; prototype checking will catch the problem. */
2207 for (arg_num = 1; ; ++arg_num)
2209 if (params == 0)
2210 return;
2211 if (arg_num == info->format_num)
2212 break;
2213 params = TREE_CHAIN (params);
2215 format_tree = TREE_VALUE (params);
2216 params = TREE_CHAIN (params);
2217 if (format_tree == 0)
2218 return;
2220 res.number_non_literal = 0;
2221 res.number_extra_args = 0;
2222 res.number_dollar_extra_args = 0;
2223 res.number_wide = 0;
2224 res.number_empty = 0;
2225 res.number_unterminated = 0;
2226 res.number_other = 0;
2228 check_format_info_recurse (status, &res, info, format_tree, params, arg_num);
2230 if (res.number_non_literal > 0)
2232 /* Functions taking a va_list normally pass a non-literal format
2233 string. These functions typically are declared with
2234 first_arg_num == 0, so avoid warning in those cases. */
2235 if (info->first_arg_num != 0 && warn_format > 1)
2236 status_warning (status, "format not a string literal, argument types not checked");
2239 /* If there were extra arguments to the format, normally warn. However,
2240 the standard does say extra arguments are ignored, so in the specific
2241 case where we have multiple leaves (conditional expressions or
2242 ngettext) allow extra arguments if at least one leaf didn't have extra
2243 arguments, but was otherwise OK (either non-literal or checked OK).
2244 If the format is an empty string, this should be counted similarly to the
2245 case of extra format arguments. */
2246 if (res.number_extra_args > 0 && res.number_non_literal == 0
2247 && res.number_other == 0)
2248 status_warning (status, "too many arguments for format");
2249 if (res.number_dollar_extra_args > 0 && res.number_non_literal == 0
2250 && res.number_other == 0)
2251 status_warning (status, "unused arguments in $-style format");
2252 if (res.number_empty > 0 && res.number_non_literal == 0
2253 && res.number_other == 0)
2254 status_warning (status, "zero-length format string");
2256 if (res.number_wide > 0)
2257 status_warning (status, "format is a wide character string");
2259 if (res.number_unterminated > 0)
2260 status_warning (status, "unterminated format string");
2264 /* Recursively check a call to a format function. FORMAT_TREE is the
2265 format parameter, which may be a conditional expression in which
2266 both halves should be checked. ARG_NUM is the number of the
2267 format argument; PARAMS points just after it in the argument list. */
2269 static void
2270 check_format_info_recurse (status, res, info, format_tree, params, arg_num)
2271 int *status;
2272 format_check_results *res;
2273 function_format_info *info;
2274 tree format_tree;
2275 tree params;
2276 int arg_num;
2278 int format_length;
2279 const char *format_chars;
2281 if (TREE_CODE (format_tree) == NOP_EXPR)
2283 /* Strip coercion. */
2284 check_format_info_recurse (status, res, info,
2285 TREE_OPERAND (format_tree, 0), params,
2286 arg_num);
2287 return;
2290 if (TREE_CODE (format_tree) == CALL_EXPR
2291 && TREE_CODE (TREE_OPERAND (format_tree, 0)) == ADDR_EXPR
2292 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0))
2293 == FUNCTION_DECL))
2295 tree function = TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0);
2297 /* See if this is a call to a known internationalization function
2298 that modifies the format arg. */
2299 international_format_info *iinfo;
2301 for (iinfo = international_format_list; iinfo; iinfo = iinfo->next)
2302 if (iinfo->assembler_name
2303 ? (iinfo->assembler_name == DECL_ASSEMBLER_NAME (function))
2304 : (iinfo->name == DECL_NAME (function)))
2306 tree inner_args;
2307 int i;
2309 for (inner_args = TREE_OPERAND (format_tree, 1), i = 1;
2310 inner_args != 0;
2311 inner_args = TREE_CHAIN (inner_args), i++)
2312 if (i == iinfo->format_num)
2314 /* FIXME: with Marc Espie's __attribute__((nonnull))
2315 patch in GCC, we will have chained attributes,
2316 and be able to handle functions like ngettext
2317 with multiple format_arg attributes properly. */
2318 check_format_info_recurse (status, res, info,
2319 TREE_VALUE (inner_args), params,
2320 arg_num);
2321 return;
2326 if (TREE_CODE (format_tree) == COND_EXPR)
2328 /* Check both halves of the conditional expression. */
2329 check_format_info_recurse (status, res, info,
2330 TREE_OPERAND (format_tree, 1), params,
2331 arg_num);
2332 check_format_info_recurse (status, res, info,
2333 TREE_OPERAND (format_tree, 2), params,
2334 arg_num);
2335 return;
2338 if (integer_zerop (format_tree))
2340 /* FIXME: this warning should go away once Marc Espie's
2341 __attribute__((nonnull)) patch is in. Instead, checking for
2342 nonnull attributes should probably change this function to act
2343 specially if info == NULL and add a res->number_null entry for
2344 that case, or maybe add a function pointer to be called at
2345 the end instead of hardcoding check_format_info_main. */
2346 status_warning (status, "null format string");
2348 /* Skip to first argument to check, so we can see if this format
2349 has any arguments (it shouldn't). */
2350 while (arg_num + 1 < info->first_arg_num)
2352 if (params == 0)
2353 return;
2354 params = TREE_CHAIN (params);
2355 ++arg_num;
2358 if (params == 0)
2359 res->number_other++;
2360 else
2361 res->number_extra_args++;
2363 return;
2366 if (TREE_CODE (format_tree) != ADDR_EXPR)
2368 res->number_non_literal++;
2369 return;
2371 format_tree = TREE_OPERAND (format_tree, 0);
2372 if (TREE_CODE (format_tree) != STRING_CST)
2374 res->number_non_literal++;
2375 return;
2377 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (format_tree))) != char_type_node)
2379 res->number_wide++;
2380 return;
2382 format_chars = TREE_STRING_POINTER (format_tree);
2383 format_length = TREE_STRING_LENGTH (format_tree);
2384 if (format_length < 1)
2386 res->number_unterminated++;
2387 return;
2389 if (format_length == 1)
2391 res->number_empty++;
2392 return;
2394 if (format_chars[--format_length] != 0)
2396 res->number_unterminated++;
2397 return;
2400 /* Skip to first argument to check. */
2401 while (arg_num + 1 < info->first_arg_num)
2403 if (params == 0)
2404 return;
2405 params = TREE_CHAIN (params);
2406 ++arg_num;
2408 /* Provisionally increment res->number_other; check_format_info_main
2409 will decrement it if it finds there are extra arguments, but this way
2410 need not adjust it for every return. */
2411 res->number_other++;
2412 check_format_info_main (status, res, info, format_chars, format_length,
2413 params, arg_num);
2417 /* Do the main part of checking a call to a format function. FORMAT_CHARS
2418 is the NUL-terminated format string (which at this point may contain
2419 internal NUL characters); FORMAT_LENGTH is its length (excluding the
2420 terminating NUL character). ARG_NUM is one less than the number of
2421 the first format argument to check; PARAMS points to that format
2422 argument in the list of arguments. */
2424 static void
2425 check_format_info_main (status, res, info, format_chars, format_length,
2426 params, arg_num)
2427 int *status;
2428 format_check_results *res;
2429 function_format_info *info;
2430 const char *format_chars;
2431 int format_length;
2432 tree params;
2433 int arg_num;
2435 int i;
2436 int suppressed;
2437 const char *length_chars = NULL;
2438 enum format_lengths length_chars_val = FMT_LEN_none;
2439 enum format_std_version length_chars_std = STD_C89;
2440 int format_char;
2441 const char *orig_format_chars = format_chars;
2442 tree cur_param;
2443 tree wanted_type;
2444 int main_arg_num;
2445 tree main_arg_params;
2446 enum format_std_version wanted_type_std;
2447 const char *wanted_type_name;
2448 format_wanted_type width_wanted_type;
2449 format_wanted_type precision_wanted_type;
2450 format_wanted_type main_wanted_type;
2451 format_wanted_type *first_wanted_type;
2452 format_wanted_type *last_wanted_type;
2453 tree first_fillin_param;
2454 const format_kind_info *fki = NULL;
2455 const format_flag_spec *flag_specs = NULL;
2456 const format_flag_pair *bad_flag_pairs = NULL;
2457 const format_length_info *fli = NULL;
2458 const format_char_info *fci = NULL;
2459 char flag_chars[256];
2460 /* -1 if no conversions taking an operand have been found; 0 if one has
2461 and it didn't use $; 1 if $ formats are in use. */
2462 int has_operand_number = -1;
2464 first_fillin_param = params;
2465 init_dollar_format_checking (info->first_arg_num, first_fillin_param);
2466 fki = &format_types[info->format_type];
2467 flag_specs = fki->flag_specs;
2468 bad_flag_pairs = fki->bad_flag_pairs;
2469 while (1)
2471 int aflag;
2472 first_wanted_type = NULL;
2473 last_wanted_type = NULL;
2474 if (*format_chars == 0)
2476 if (format_chars - orig_format_chars != format_length)
2477 status_warning (status, "embedded `\\0' in format");
2478 if (info->first_arg_num != 0 && params != 0
2479 && has_operand_number <= 0)
2481 res->number_other--;
2482 res->number_extra_args++;
2484 if (has_operand_number > 0)
2485 finish_dollar_format_checking (status, res);
2486 return;
2488 if (*format_chars++ != '%')
2489 continue;
2490 if (*format_chars == 0)
2492 status_warning (status, "spurious trailing `%%' in format");
2493 continue;
2495 if (*format_chars == '%')
2497 ++format_chars;
2498 continue;
2500 flag_chars[0] = 0;
2501 suppressed = FALSE;
2502 main_arg_num = 0;
2503 main_arg_params = 0;
2505 if ((fki->flags & FMT_FLAG_ARG_CONVERT) && has_operand_number != 0)
2507 /* Possibly read a $ operand number at the start of the format.
2508 If one was previously used, one is required here. If one
2509 is not used here, we can't immediately conclude this is a
2510 format without them, since it could be printf %m or scanf %*. */
2511 int opnum;
2512 opnum = maybe_read_dollar_number (status, &format_chars, 0,
2513 first_fillin_param,
2514 &main_arg_params);
2515 if (opnum == -1)
2516 return;
2517 else if (opnum > 0)
2519 has_operand_number = 1;
2520 main_arg_num = opnum + info->first_arg_num - 1;
2524 /* Read any format flags, but do not yet validate them beyond removing
2525 duplicates, since in general validation depends on the rest of
2526 the format. */
2527 while (*format_chars != 0 && index (fki->flag_chars, *format_chars) != 0)
2529 if (index (flag_chars, *format_chars) != 0)
2531 const format_flag_spec *s = get_flag_spec (flag_specs,
2532 *format_chars, NULL);
2533 status_warning (status, "repeated %s in format", _(s->name));
2535 else
2537 i = strlen (flag_chars);
2538 flag_chars[i++] = *format_chars;
2539 flag_chars[i] = 0;
2541 ++format_chars;
2544 /* Read any format width, possibly * or *m$. */
2545 if (fki->width_char != 0)
2547 if (fki->width_type != NULL && *format_chars == '*')
2549 i = strlen (flag_chars);
2550 flag_chars[i++] = fki->width_char;
2551 flag_chars[i] = 0;
2552 /* "...a field width...may be indicated by an asterisk.
2553 In this case, an int argument supplies the field width..." */
2554 ++format_chars;
2555 if (params == 0)
2557 status_warning (status, "too few arguments for format");
2558 return;
2560 if (has_operand_number != 0)
2562 int opnum;
2563 opnum = maybe_read_dollar_number (status, &format_chars,
2564 has_operand_number == 1,
2565 first_fillin_param,
2566 &params);
2567 if (opnum == -1)
2568 return;
2569 else if (opnum > 0)
2571 has_operand_number = 1;
2572 arg_num = opnum + info->first_arg_num - 1;
2574 else
2575 has_operand_number = 0;
2577 if (info->first_arg_num != 0)
2579 cur_param = TREE_VALUE (params);
2580 if (has_operand_number <= 0)
2582 params = TREE_CHAIN (params);
2583 ++arg_num;
2585 width_wanted_type.wanted_type = *fki->width_type;
2586 width_wanted_type.wanted_type_name = NULL;
2587 width_wanted_type.pointer_count = 0;
2588 width_wanted_type.char_lenient_flag = 0;
2589 width_wanted_type.writing_in_flag = 0;
2590 width_wanted_type.name = _("field width");
2591 width_wanted_type.param = cur_param;
2592 width_wanted_type.arg_num = arg_num;
2593 width_wanted_type.next = NULL;
2594 if (last_wanted_type != 0)
2595 last_wanted_type->next = &width_wanted_type;
2596 if (first_wanted_type == 0)
2597 first_wanted_type = &width_wanted_type;
2598 last_wanted_type = &width_wanted_type;
2601 else
2603 /* Possibly read a numeric width. If the width is zero,
2604 we complain; for scanf this is bad according to the
2605 standard, and for printf and strftime it cannot occur
2606 because 0 is a flag. */
2607 int non_zero_width_char = FALSE;
2608 int found_width = FALSE;
2609 while (ISDIGIT (*format_chars))
2611 found_width = TRUE;
2612 if (*format_chars != '0')
2613 non_zero_width_char = TRUE;
2614 ++format_chars;
2616 if (found_width && !non_zero_width_char)
2617 status_warning (status, "zero width in scanf format");
2618 if (found_width)
2620 i = strlen (flag_chars);
2621 flag_chars[i++] = fki->width_char;
2622 flag_chars[i] = 0;
2627 /* Read any format precision, possibly * or *m$. */
2628 if (fki->precision_char != 0 && *format_chars == '.')
2630 ++format_chars;
2631 i = strlen (flag_chars);
2632 flag_chars[i++] = fki->precision_char;
2633 flag_chars[i] = 0;
2634 if (fki->precision_type != NULL && *format_chars == '*')
2636 /* "...a...precision...may be indicated by an asterisk.
2637 In this case, an int argument supplies the...precision." */
2638 ++format_chars;
2639 if (has_operand_number != 0)
2641 int opnum;
2642 opnum = maybe_read_dollar_number (status, &format_chars,
2643 has_operand_number == 1,
2644 first_fillin_param,
2645 &params);
2646 if (opnum == -1)
2647 return;
2648 else if (opnum > 0)
2650 has_operand_number = 1;
2651 arg_num = opnum + info->first_arg_num - 1;
2653 else
2654 has_operand_number = 0;
2656 if (info->first_arg_num != 0)
2658 if (params == 0)
2660 status_warning (status, "too few arguments for format");
2661 return;
2663 cur_param = TREE_VALUE (params);
2664 if (has_operand_number <= 0)
2666 params = TREE_CHAIN (params);
2667 ++arg_num;
2669 precision_wanted_type.wanted_type = *fki->precision_type;
2670 precision_wanted_type.wanted_type_name = NULL;
2671 precision_wanted_type.pointer_count = 0;
2672 precision_wanted_type.char_lenient_flag = 0;
2673 precision_wanted_type.writing_in_flag = 0;
2674 precision_wanted_type.name = _("field precision");
2675 precision_wanted_type.param = cur_param;
2676 precision_wanted_type.arg_num = arg_num;
2677 precision_wanted_type.next = NULL;
2678 if (last_wanted_type != 0)
2679 last_wanted_type->next = &precision_wanted_type;
2680 if (first_wanted_type == 0)
2681 first_wanted_type = &precision_wanted_type;
2682 last_wanted_type = &precision_wanted_type;
2685 else
2687 while (ISDIGIT (*format_chars))
2688 ++format_chars;
2692 /* Read any length modifier, if this kind of format has them. */
2693 fli = fki->length_char_specs;
2694 length_chars = NULL;
2695 length_chars_val = FMT_LEN_none;
2696 length_chars_std = STD_C89;
2697 if (fli)
2699 while (fli->name != 0 && fli->name[0] != *format_chars)
2700 fli++;
2701 if (fli->name != 0)
2703 format_chars++;
2704 if (fli->double_name != 0 && fli->name[0] == *format_chars)
2706 format_chars++;
2707 length_chars = fli->double_name;
2708 length_chars_val = fli->double_index;
2709 length_chars_std = fli->double_std;
2711 else
2713 length_chars = fli->name;
2714 length_chars_val = fli->index;
2715 length_chars_std = fli->std;
2717 i = strlen (flag_chars);
2718 flag_chars[i++] = fki->length_code_char;
2719 flag_chars[i] = 0;
2721 if (pedantic)
2723 /* Warn if the length modifier is non-standard. */
2724 if (length_chars_std == STD_EXT)
2725 status_warning (status, "ISO C does not support the `%s' %s length modifier",
2726 length_chars, fki->name);
2727 else if ((length_chars_std == STD_C99 && !flag_isoc99)
2728 || (length_chars_std == STD_C94 && !flag_isoc94))
2729 status_warning (status, "ISO C89 does not support the `%s' %s length modifier",
2730 length_chars, fki->name);
2734 /* Read any modifier (strftime E/O). */
2735 if (fki->modifier_chars != NULL)
2737 while (*format_chars != 0
2738 && index (fki->modifier_chars, *format_chars) != 0)
2740 if (index (flag_chars, *format_chars) != 0)
2742 const format_flag_spec *s = get_flag_spec (flag_specs,
2743 *format_chars, NULL);
2744 status_warning (status, "repeated %s in format", _(s->name));
2746 else
2748 i = strlen (flag_chars);
2749 flag_chars[i++] = *format_chars;
2750 flag_chars[i] = 0;
2752 ++format_chars;
2756 /* Handle the scanf allocation kludge. */
2757 if (fki->flags & FMT_FLAG_SCANF_A_KLUDGE)
2759 if (*format_chars == 'a' && !flag_isoc99)
2761 if (format_chars[1] == 's' || format_chars[1] == 'S'
2762 || format_chars[1] == '[')
2764 /* `a' is used as a flag. */
2765 i = strlen (flag_chars);
2766 flag_chars[i++] = 'a';
2767 flag_chars[i] = 0;
2768 format_chars++;
2773 format_char = *format_chars;
2774 if (format_char == 0
2775 || (!(fki->flags & FMT_FLAG_FANCY_PERCENT_OK) && format_char == '%'))
2777 status_warning (status, "conversion lacks type at end of format");
2778 continue;
2780 format_chars++;
2781 fci = fki->conversion_specs;
2782 while (fci->format_chars != 0
2783 && index (fci->format_chars, format_char) == 0)
2784 ++fci;
2785 if (fci->format_chars == 0)
2787 if (ISGRAPH(format_char))
2788 status_warning (status, "unknown conversion type character `%c' in format",
2789 format_char);
2790 else
2791 status_warning (status, "unknown conversion type character 0x%x in format",
2792 format_char);
2793 continue;
2795 if (pedantic)
2797 if (fci->std == STD_EXT)
2798 status_warning (status, "ISO C does not support the `%%%c' %s format",
2799 format_char, fki->name);
2800 else if ((fci->std == STD_C99 && !flag_isoc99)
2801 || (fci->std == STD_C94 && !flag_isoc94))
2802 status_warning (status, "ISO C89 does not support the `%%%c' %s format",
2803 format_char, fki->name);
2806 /* Validate the individual flags used, removing any that are invalid. */
2808 int d = 0;
2809 for (i = 0; flag_chars[i] != 0; i++)
2811 const format_flag_spec *s = get_flag_spec (flag_specs,
2812 flag_chars[i], NULL);
2813 flag_chars[i - d] = flag_chars[i];
2814 if (flag_chars[i] == fki->length_code_char)
2815 continue;
2816 if (index (fci->flag_chars, flag_chars[i]) == 0)
2818 status_warning (status, "%s used with `%%%c' %s format",
2819 _(s->name), format_char, fki->name);
2820 d++;
2821 continue;
2823 if (pedantic)
2825 const format_flag_spec *t;
2826 if (s->std == STD_EXT)
2827 status_warning (status, "ISO C does not support %s",
2828 _(s->long_name));
2829 else if ((s->std == STD_C99 && !flag_isoc99)
2830 || (s->std == STD_C94 && !flag_isoc94))
2831 status_warning (status, "ISO C89 does not support %s",
2832 _(s->long_name));
2833 t = get_flag_spec (flag_specs, flag_chars[i], fci->flags2);
2834 if (t != NULL && t->std > s->std)
2836 const char *long_name = (t->long_name != NULL
2837 ? t->long_name
2838 : s->long_name);
2839 if (t->std == STD_EXT)
2840 status_warning (status, "ISO C does not support %s with the `%%%c' %s format",
2841 _(long_name), format_char, fki->name);
2842 else if ((t->std == STD_C99 && !flag_isoc99)
2843 || (t->std == STD_C94 && !flag_isoc94))
2844 status_warning (status, "ISO C89 does not support %s with the `%%%c' %s format",
2845 _(long_name), format_char, fki->name);
2849 flag_chars[i - d] = 0;
2852 aflag = 0;
2853 if ((fki->flags & FMT_FLAG_SCANF_A_KLUDGE)
2854 && index (flag_chars, 'a') != 0)
2855 aflag = 1;
2857 if (fki->suppression_char
2858 && index (flag_chars, fki->suppression_char) != 0)
2859 suppressed = 1;
2861 /* Validate the pairs of flags used. */
2862 for (i = 0; bad_flag_pairs[i].flag_char1 != 0; i++)
2864 const format_flag_spec *s, *t;
2865 if (index (flag_chars, bad_flag_pairs[i].flag_char1) == 0)
2866 continue;
2867 if (index (flag_chars, bad_flag_pairs[i].flag_char2) == 0)
2868 continue;
2869 if (bad_flag_pairs[i].predicate != 0
2870 && index (fci->flags2, bad_flag_pairs[i].predicate) == 0)
2871 continue;
2872 s = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char1, NULL);
2873 t = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char2, NULL);
2874 if (bad_flag_pairs[i].ignored)
2876 if (bad_flag_pairs[i].predicate != 0)
2877 status_warning (status, "%s ignored with %s and `%%%c' %s format",
2878 _(s->name), _(t->name), format_char,
2879 fki->name);
2880 else
2881 status_warning (status, "%s ignored with %s in %s format",
2882 _(s->name), _(t->name), fki->name);
2884 else
2886 if (bad_flag_pairs[i].predicate != 0)
2887 status_warning (status, "use of %s and %s together with `%%%c' %s format",
2888 _(s->name), _(t->name), format_char,
2889 fki->name);
2890 else
2891 status_warning (status, "use of %s and %s together in %s format",
2892 _(s->name), _(t->name), fki->name);
2896 /* Give Y2K warnings. */
2898 int y2k_level = 0;
2899 if (index (fci->flags2, '4') != 0)
2900 if (index (flag_chars, 'E') != 0)
2901 y2k_level = 3;
2902 else
2903 y2k_level = 2;
2904 else if (index (fci->flags2, '3') != 0)
2905 y2k_level = 3;
2906 else if (index (fci->flags2, '2') != 0)
2907 y2k_level = 2;
2908 if (y2k_level == 3)
2909 status_warning (status, "`%%%c' yields only last 2 digits of year in some locales",
2910 format_char);
2911 else if (y2k_level == 2)
2912 status_warning (status, "`%%%c' yields only last 2 digits of year", format_char);
2915 if (index (fci->flags2, '[') != 0)
2917 /* Skip over scan set, in case it happens to have '%' in it. */
2918 if (*format_chars == '^')
2919 ++format_chars;
2920 /* Find closing bracket; if one is hit immediately, then
2921 it's part of the scan set rather than a terminator. */
2922 if (*format_chars == ']')
2923 ++format_chars;
2924 while (*format_chars && *format_chars != ']')
2925 ++format_chars;
2926 if (*format_chars != ']')
2927 /* The end of the format string was reached. */
2928 status_warning (status, "no closing `]' for `%%[' format");
2931 wanted_type = 0;
2932 wanted_type_name = 0;
2933 if (fki->flags & FMT_FLAG_ARG_CONVERT)
2935 wanted_type = (fci->types[length_chars_val].type
2936 ? *fci->types[length_chars_val].type : 0);
2937 wanted_type_name = fci->types[length_chars_val].name;
2938 wanted_type_std = fci->types[length_chars_val].std;
2939 if (wanted_type == 0)
2941 status_warning (status, "use of `%s' length modifier with `%c' type character",
2942 length_chars, format_char);
2943 /* Heuristic: skip one argument when an invalid length/type
2944 combination is encountered. */
2945 arg_num++;
2946 if (params == 0)
2948 status_warning (status, "too few arguments for format");
2949 return;
2951 params = TREE_CHAIN (params);
2952 continue;
2954 else if (pedantic
2955 /* Warn if non-standard, provided it is more non-standard
2956 than the length and type characters that may already
2957 have been warned for. */
2958 && wanted_type_std > length_chars_std
2959 && wanted_type_std > fci->std)
2961 if (wanted_type_std == STD_EXT)
2962 status_warning (status, "ISO C does not support the `%%%s%c' %s format",
2963 length_chars, format_char, fki->name);
2964 else if ((wanted_type_std == STD_C99 && !flag_isoc99)
2965 || (wanted_type_std == STD_C94 && !flag_isoc94))
2966 status_warning (status, "ISO C89 does not support the `%%%s%c' %s format",
2967 length_chars, format_char, fki->name);
2971 /* Finally. . .check type of argument against desired type! */
2972 if (info->first_arg_num == 0)
2973 continue;
2974 if ((fci->pointer_count == 0 && wanted_type == void_type_node)
2975 || suppressed)
2977 if (main_arg_num != 0)
2979 if (suppressed)
2980 status_warning (status, "operand number specified with suppressed assignment");
2981 else
2982 status_warning (status, "operand number specified for format taking no argument");
2985 else
2987 if (main_arg_num != 0)
2989 arg_num = main_arg_num;
2990 params = main_arg_params;
2992 else
2994 ++arg_num;
2995 if (has_operand_number > 0)
2997 status_warning (status, "missing $ operand number in format");
2998 return;
3000 else
3001 has_operand_number = 0;
3002 if (params == 0)
3004 status_warning (status, "too few arguments for format");
3005 return;
3008 cur_param = TREE_VALUE (params);
3009 params = TREE_CHAIN (params);
3010 main_wanted_type.wanted_type = wanted_type;
3011 main_wanted_type.wanted_type_name = wanted_type_name;
3012 main_wanted_type.pointer_count = fci->pointer_count + aflag;
3013 main_wanted_type.char_lenient_flag = 0;
3014 if (index (fci->flags2, 'c') != 0)
3015 main_wanted_type.char_lenient_flag = 1;
3016 main_wanted_type.writing_in_flag = 0;
3017 if (index (fci->flags2, 'W') != 0)
3018 main_wanted_type.writing_in_flag = 1;
3019 main_wanted_type.name = NULL;
3020 main_wanted_type.param = cur_param;
3021 main_wanted_type.arg_num = arg_num;
3022 main_wanted_type.next = NULL;
3023 if (last_wanted_type != 0)
3024 last_wanted_type->next = &main_wanted_type;
3025 if (first_wanted_type == 0)
3026 first_wanted_type = &main_wanted_type;
3027 last_wanted_type = &main_wanted_type;
3030 if (first_wanted_type != 0)
3031 check_format_types (status, first_wanted_type);
3037 /* Check the argument types from a single format conversion (possibly
3038 including width and precision arguments). */
3039 static void
3040 check_format_types (status, types)
3041 int *status;
3042 format_wanted_type *types;
3044 for (; types != 0; types = types->next)
3046 tree cur_param;
3047 tree cur_type;
3048 tree orig_cur_type;
3049 tree wanted_type;
3050 tree promoted_type;
3051 int arg_num;
3052 int i;
3053 int char_type_flag;
3054 cur_param = types->param;
3055 cur_type = TREE_TYPE (cur_param);
3056 if (TREE_CODE (cur_type) == ERROR_MARK)
3057 continue;
3058 char_type_flag = 0;
3059 wanted_type = types->wanted_type;
3060 arg_num = types->arg_num;
3062 /* The following should not occur here. */
3063 if (wanted_type == 0)
3064 abort ();
3065 if (wanted_type == void_type_node && types->pointer_count == 0)
3066 abort ();
3068 if (types->pointer_count == 0)
3070 promoted_type = simple_type_promotes_to (wanted_type);
3071 if (promoted_type != NULL_TREE)
3072 wanted_type = promoted_type;
3075 STRIP_NOPS (cur_param);
3077 /* Check the types of any additional pointer arguments
3078 that precede the "real" argument. */
3079 for (i = 0; i < types->pointer_count; ++i)
3081 if (TREE_CODE (cur_type) == POINTER_TYPE)
3083 cur_type = TREE_TYPE (cur_type);
3084 if (TREE_CODE (cur_type) == ERROR_MARK)
3085 break;
3087 if (cur_param != 0 && TREE_CODE (cur_param) == ADDR_EXPR)
3088 cur_param = TREE_OPERAND (cur_param, 0);
3089 else
3090 cur_param = 0;
3092 /* See if this is an attempt to write into a const type with
3093 scanf or with printf "%n". Note: the writing in happens
3094 at the first indirection only, if for example
3095 void * const * is passed to scanf %p; passing
3096 const void ** is simply passing an incompatible type. */
3097 if (types->writing_in_flag
3098 && i == 0
3099 && (TYPE_READONLY (cur_type)
3100 || (cur_param != 0
3101 && (TREE_CODE_CLASS (TREE_CODE (cur_param)) == 'c'
3102 || (DECL_P (cur_param)
3103 && TREE_READONLY (cur_param))))))
3104 status_warning (status, "writing into constant object (arg %d)", arg_num);
3106 /* If there are extra type qualifiers beyond the first
3107 indirection, then this makes the types technically
3108 incompatible. */
3109 if (i > 0
3110 && pedantic
3111 && (TYPE_READONLY (cur_type)
3112 || TYPE_VOLATILE (cur_type)
3113 || TYPE_RESTRICT (cur_type)))
3114 status_warning (status, "extra type qualifiers in format argument (arg %d)",
3115 arg_num);
3118 else
3120 if (types->pointer_count == 1)
3121 status_warning (status, "format argument is not a pointer (arg %d)", arg_num);
3122 else
3123 status_warning (status, "format argument is not a pointer to a pointer (arg %d)", arg_num);
3124 break;
3128 if (i < types->pointer_count)
3129 continue;
3131 orig_cur_type = cur_type;
3132 cur_type = TYPE_MAIN_VARIANT (cur_type);
3134 /* Check whether the argument type is a character type. This leniency
3135 only applies to certain formats, flagged with 'c'.
3137 if (types->char_lenient_flag)
3138 char_type_flag = (cur_type == char_type_node
3139 || cur_type == signed_char_type_node
3140 || cur_type == unsigned_char_type_node);
3142 /* Check the type of the "real" argument, if there's a type we want. */
3143 if (wanted_type == cur_type)
3144 continue;
3145 /* If we want `void *', allow any pointer type.
3146 (Anything else would already have got a warning.)
3147 With -pedantic, only allow pointers to void and to character
3148 types. */
3149 if (wanted_type == void_type_node
3150 && (!pedantic || (i == 1 && char_type_flag)))
3151 continue;
3152 /* Don't warn about differences merely in signedness, unless
3153 -pedantic. With -pedantic, warn if the type is a pointer
3154 target and not a character type, and for character types at
3155 a second level of indirection. */
3156 if (TREE_CODE (wanted_type) == INTEGER_TYPE
3157 && TREE_CODE (cur_type) == INTEGER_TYPE
3158 && (! pedantic || i == 0 || (i == 1 && char_type_flag))
3159 && (TREE_UNSIGNED (wanted_type)
3160 ? wanted_type == unsigned_type (cur_type)
3161 : wanted_type == signed_type (cur_type)))
3162 continue;
3163 /* Likewise, "signed char", "unsigned char" and "char" are
3164 equivalent but the above test won't consider them equivalent. */
3165 if (wanted_type == char_type_node
3166 && (! pedantic || i < 2)
3167 && char_type_flag)
3168 continue;
3169 /* Now we have a type mismatch. */
3171 register const char *this;
3172 register const char *that;
3174 this = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (wanted_type)));
3175 that = 0;
3176 if (TYPE_NAME (orig_cur_type) != 0
3177 && TREE_CODE (orig_cur_type) != INTEGER_TYPE
3178 && !(TREE_CODE (orig_cur_type) == POINTER_TYPE
3179 && TREE_CODE (TREE_TYPE (orig_cur_type)) == INTEGER_TYPE))
3181 if (TREE_CODE (TYPE_NAME (orig_cur_type)) == TYPE_DECL
3182 && DECL_NAME (TYPE_NAME (orig_cur_type)) != 0)
3183 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (orig_cur_type)));
3184 else
3185 that = IDENTIFIER_POINTER (TYPE_NAME (orig_cur_type));
3188 /* A nameless type can't possibly match what the format wants.
3189 So there will be a warning for it.
3190 Make up a string to describe vaguely what it is. */
3191 if (that == 0)
3193 if (TREE_CODE (orig_cur_type) == POINTER_TYPE)
3194 that = "pointer";
3195 else
3196 that = "different type";
3199 /* Make the warning better in case of mismatch of int vs long. */
3200 if (TREE_CODE (orig_cur_type) == INTEGER_TYPE
3201 && TREE_CODE (wanted_type) == INTEGER_TYPE
3202 && TYPE_PRECISION (orig_cur_type) == TYPE_PRECISION (wanted_type)
3203 && TYPE_NAME (orig_cur_type) != 0
3204 && TREE_CODE (TYPE_NAME (orig_cur_type)) == TYPE_DECL)
3205 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (orig_cur_type)));
3207 if (strcmp (this, that) != 0)
3209 /* There may be a better name for the format, e.g. size_t,
3210 but we should allow for programs with a perverse typedef
3211 making size_t something other than what the compiler
3212 thinks. */
3213 if (types->wanted_type_name != 0
3214 && strcmp (types->wanted_type_name, that) != 0)
3215 this = types->wanted_type_name;
3216 if (types->name != 0)
3217 status_warning (status, "%s is not type %s (arg %d)", types->name, this,
3218 arg_num);
3219 else
3220 status_warning (status, "%s format, %s arg (arg %d)", this, that, arg_num);
3226 /* Print a warning if a constant expression had overflow in folding.
3227 Invoke this function on every expression that the language
3228 requires to be a constant expression.
3229 Note the ANSI C standard says it is erroneous for a
3230 constant expression to overflow. */
3232 void
3233 constant_expression_warning (value)
3234 tree value;
3236 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
3237 || TREE_CODE (value) == COMPLEX_CST)
3238 && TREE_CONSTANT_OVERFLOW (value) && pedantic)
3239 pedwarn ("overflow in constant expression");
3242 /* Print a warning if an expression had overflow in folding.
3243 Invoke this function on every expression that
3244 (1) appears in the source code, and
3245 (2) might be a constant expression that overflowed, and
3246 (3) is not already checked by convert_and_check;
3247 however, do not invoke this function on operands of explicit casts. */
3249 void
3250 overflow_warning (value)
3251 tree value;
3253 if ((TREE_CODE (value) == INTEGER_CST
3254 || (TREE_CODE (value) == COMPLEX_CST
3255 && TREE_CODE (TREE_REALPART (value)) == INTEGER_CST))
3256 && TREE_OVERFLOW (value))
3258 TREE_OVERFLOW (value) = 0;
3259 if (skip_evaluation == 0)
3260 warning ("integer overflow in expression");
3262 else if ((TREE_CODE (value) == REAL_CST
3263 || (TREE_CODE (value) == COMPLEX_CST
3264 && TREE_CODE (TREE_REALPART (value)) == REAL_CST))
3265 && TREE_OVERFLOW (value))
3267 TREE_OVERFLOW (value) = 0;
3268 if (skip_evaluation == 0)
3269 warning ("floating point overflow in expression");
3273 /* Print a warning if a large constant is truncated to unsigned,
3274 or if -Wconversion is used and a constant < 0 is converted to unsigned.
3275 Invoke this function on every expression that might be implicitly
3276 converted to an unsigned type. */
3278 void
3279 unsigned_conversion_warning (result, operand)
3280 tree result, operand;
3282 if (TREE_CODE (operand) == INTEGER_CST
3283 && TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
3284 && TREE_UNSIGNED (TREE_TYPE (result))
3285 && skip_evaluation == 0
3286 && !int_fits_type_p (operand, TREE_TYPE (result)))
3288 if (!int_fits_type_p (operand, signed_type (TREE_TYPE (result))))
3289 /* This detects cases like converting -129 or 256 to unsigned char. */
3290 warning ("large integer implicitly truncated to unsigned type");
3291 else if (warn_conversion)
3292 warning ("negative integer implicitly converted to unsigned type");
3296 /* Convert EXPR to TYPE, warning about conversion problems with constants.
3297 Invoke this function on every expression that is converted implicitly,
3298 i.e. because of language rules and not because of an explicit cast. */
3300 tree
3301 convert_and_check (type, expr)
3302 tree type, expr;
3304 tree t = convert (type, expr);
3305 if (TREE_CODE (t) == INTEGER_CST)
3307 if (TREE_OVERFLOW (t))
3309 TREE_OVERFLOW (t) = 0;
3311 /* Do not diagnose overflow in a constant expression merely
3312 because a conversion overflowed. */
3313 TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (expr);
3315 /* No warning for converting 0x80000000 to int. */
3316 if (!(TREE_UNSIGNED (type) < TREE_UNSIGNED (TREE_TYPE (expr))
3317 && TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
3318 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (expr))))
3319 /* If EXPR fits in the unsigned version of TYPE,
3320 don't warn unless pedantic. */
3321 if ((pedantic
3322 || TREE_UNSIGNED (type)
3323 || ! int_fits_type_p (expr, unsigned_type (type)))
3324 && skip_evaluation == 0)
3325 warning ("overflow in implicit constant conversion");
3327 else
3328 unsigned_conversion_warning (t, expr);
3330 return t;
3333 void
3334 c_expand_expr_stmt (expr)
3335 tree expr;
3337 /* Do default conversion if safe and possibly important,
3338 in case within ({...}). */
3339 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE && lvalue_p (expr))
3340 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
3341 expr = default_conversion (expr);
3343 if (TREE_TYPE (expr) != error_mark_node
3344 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
3345 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
3346 error ("expression statement has incomplete type");
3348 last_expr_type = TREE_TYPE (expr);
3349 add_stmt (build_stmt (EXPR_STMT, expr));
3352 /* Validate the expression after `case' and apply default promotions. */
3354 tree
3355 check_case_value (value)
3356 tree value;
3358 if (value == NULL_TREE)
3359 return value;
3361 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3362 STRIP_TYPE_NOPS (value);
3363 /* In C++, the following is allowed:
3365 const int i = 3;
3366 switch (...) { case i: ... }
3368 So, we try to reduce the VALUE to a constant that way. */
3369 if (c_language == clk_cplusplus)
3371 value = decl_constant_value (value);
3372 STRIP_TYPE_NOPS (value);
3373 value = fold (value);
3376 if (TREE_CODE (value) != INTEGER_CST
3377 && value != error_mark_node)
3379 error ("case label does not reduce to an integer constant");
3380 value = error_mark_node;
3382 else
3383 /* Promote char or short to int. */
3384 value = default_conversion (value);
3386 constant_expression_warning (value);
3388 return value;
3391 /* Return an integer type with BITS bits of precision,
3392 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3394 tree
3395 type_for_size (bits, unsignedp)
3396 unsigned bits;
3397 int unsignedp;
3399 if (bits == TYPE_PRECISION (integer_type_node))
3400 return unsignedp ? unsigned_type_node : integer_type_node;
3402 if (bits == TYPE_PRECISION (signed_char_type_node))
3403 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3405 if (bits == TYPE_PRECISION (short_integer_type_node))
3406 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3408 if (bits == TYPE_PRECISION (long_integer_type_node))
3409 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3411 if (bits == TYPE_PRECISION (long_long_integer_type_node))
3412 return (unsignedp ? long_long_unsigned_type_node
3413 : long_long_integer_type_node);
3415 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3416 return (unsignedp ? widest_unsigned_literal_type_node
3417 : widest_integer_literal_type_node);
3419 if (bits <= TYPE_PRECISION (intQI_type_node))
3420 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3422 if (bits <= TYPE_PRECISION (intHI_type_node))
3423 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3425 if (bits <= TYPE_PRECISION (intSI_type_node))
3426 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3428 if (bits <= TYPE_PRECISION (intDI_type_node))
3429 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3431 return 0;
3434 /* Return a data type that has machine mode MODE.
3435 If the mode is an integer,
3436 then UNSIGNEDP selects between signed and unsigned types. */
3438 tree
3439 type_for_mode (mode, unsignedp)
3440 enum machine_mode mode;
3441 int unsignedp;
3443 if (mode == TYPE_MODE (integer_type_node))
3444 return unsignedp ? unsigned_type_node : integer_type_node;
3446 if (mode == TYPE_MODE (signed_char_type_node))
3447 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3449 if (mode == TYPE_MODE (short_integer_type_node))
3450 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3452 if (mode == TYPE_MODE (long_integer_type_node))
3453 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3455 if (mode == TYPE_MODE (long_long_integer_type_node))
3456 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3458 if (mode == TYPE_MODE (widest_integer_literal_type_node))
3459 return unsignedp ? widest_unsigned_literal_type_node
3460 : widest_integer_literal_type_node;
3462 if (mode == TYPE_MODE (intQI_type_node))
3463 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3465 if (mode == TYPE_MODE (intHI_type_node))
3466 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3468 if (mode == TYPE_MODE (intSI_type_node))
3469 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3471 if (mode == TYPE_MODE (intDI_type_node))
3472 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3474 #if HOST_BITS_PER_WIDE_INT >= 64
3475 if (mode == TYPE_MODE (intTI_type_node))
3476 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3477 #endif
3479 if (mode == TYPE_MODE (float_type_node))
3480 return float_type_node;
3482 if (mode == TYPE_MODE (double_type_node))
3483 return double_type_node;
3485 if (mode == TYPE_MODE (long_double_type_node))
3486 return long_double_type_node;
3488 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
3489 return build_pointer_type (char_type_node);
3491 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
3492 return build_pointer_type (integer_type_node);
3494 #ifdef VECTOR_MODE_SUPPORTED_P
3495 if (mode == TYPE_MODE (V4SF_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
3496 return V4SF_type_node;
3497 if (mode == TYPE_MODE (V4SI_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
3498 return V4SI_type_node;
3499 if (mode == TYPE_MODE (V2SI_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
3500 return V2SI_type_node;
3501 if (mode == TYPE_MODE (V4HI_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
3502 return V4HI_type_node;
3503 if (mode == TYPE_MODE (V8QI_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
3504 return V8QI_type_node;
3505 #endif
3507 return 0;
3510 /* Return an unsigned type the same as TYPE in other respects. */
3511 tree
3512 unsigned_type (type)
3513 tree type;
3515 tree type1 = TYPE_MAIN_VARIANT (type);
3516 if (type1 == signed_char_type_node || type1 == char_type_node)
3517 return unsigned_char_type_node;
3518 if (type1 == integer_type_node)
3519 return unsigned_type_node;
3520 if (type1 == short_integer_type_node)
3521 return short_unsigned_type_node;
3522 if (type1 == long_integer_type_node)
3523 return long_unsigned_type_node;
3524 if (type1 == long_long_integer_type_node)
3525 return long_long_unsigned_type_node;
3526 if (type1 == widest_integer_literal_type_node)
3527 return widest_unsigned_literal_type_node;
3528 #if HOST_BITS_PER_WIDE_INT >= 64
3529 if (type1 == intTI_type_node)
3530 return unsigned_intTI_type_node;
3531 #endif
3532 if (type1 == intDI_type_node)
3533 return unsigned_intDI_type_node;
3534 if (type1 == intSI_type_node)
3535 return unsigned_intSI_type_node;
3536 if (type1 == intHI_type_node)
3537 return unsigned_intHI_type_node;
3538 if (type1 == intQI_type_node)
3539 return unsigned_intQI_type_node;
3541 return signed_or_unsigned_type (1, type);
3544 /* Return a signed type the same as TYPE in other respects. */
3546 tree
3547 signed_type (type)
3548 tree type;
3550 tree type1 = TYPE_MAIN_VARIANT (type);
3551 if (type1 == unsigned_char_type_node || type1 == char_type_node)
3552 return signed_char_type_node;
3553 if (type1 == unsigned_type_node)
3554 return integer_type_node;
3555 if (type1 == short_unsigned_type_node)
3556 return short_integer_type_node;
3557 if (type1 == long_unsigned_type_node)
3558 return long_integer_type_node;
3559 if (type1 == long_long_unsigned_type_node)
3560 return long_long_integer_type_node;
3561 if (type1 == widest_unsigned_literal_type_node)
3562 return widest_integer_literal_type_node;
3563 #if HOST_BITS_PER_WIDE_INT >= 64
3564 if (type1 == unsigned_intTI_type_node)
3565 return intTI_type_node;
3566 #endif
3567 if (type1 == unsigned_intDI_type_node)
3568 return intDI_type_node;
3569 if (type1 == unsigned_intSI_type_node)
3570 return intSI_type_node;
3571 if (type1 == unsigned_intHI_type_node)
3572 return intHI_type_node;
3573 if (type1 == unsigned_intQI_type_node)
3574 return intQI_type_node;
3576 return signed_or_unsigned_type (0, type);
3579 /* Return a type the same as TYPE except unsigned or
3580 signed according to UNSIGNEDP. */
3582 tree
3583 signed_or_unsigned_type (unsignedp, type)
3584 int unsignedp;
3585 tree type;
3587 if (! INTEGRAL_TYPE_P (type)
3588 || TREE_UNSIGNED (type) == unsignedp)
3589 return type;
3591 if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node))
3592 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3593 if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
3594 return unsignedp ? unsigned_type_node : integer_type_node;
3595 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node))
3596 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3597 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node))
3598 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3599 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node))
3600 return (unsignedp ? long_long_unsigned_type_node
3601 : long_long_integer_type_node);
3602 if (TYPE_PRECISION (type) == TYPE_PRECISION (widest_integer_literal_type_node))
3603 return (unsignedp ? widest_unsigned_literal_type_node
3604 : widest_integer_literal_type_node);
3605 return type;
3608 /* Return the minimum number of bits needed to represent VALUE in a
3609 signed or unsigned type, UNSIGNEDP says which. */
3611 unsigned int
3612 min_precision (value, unsignedp)
3613 tree value;
3614 int unsignedp;
3616 int log;
3618 /* If the value is negative, compute its negative minus 1. The latter
3619 adjustment is because the absolute value of the largest negative value
3620 is one larger than the largest positive value. This is equivalent to
3621 a bit-wise negation, so use that operation instead. */
3623 if (tree_int_cst_sgn (value) < 0)
3624 value = fold (build1 (BIT_NOT_EXPR, TREE_TYPE (value), value));
3626 /* Return the number of bits needed, taking into account the fact
3627 that we need one more bit for a signed than unsigned type. */
3629 if (integer_zerop (value))
3630 log = 0;
3631 else
3632 log = tree_floor_log2 (value);
3634 return log + 1 + ! unsignedp;
3637 /* Print an error message for invalid operands to arith operation CODE.
3638 NOP_EXPR is used as a special case (see truthvalue_conversion). */
3640 void
3641 binary_op_error (code)
3642 enum tree_code code;
3644 register const char *opname;
3646 switch (code)
3648 case NOP_EXPR:
3649 error ("invalid truth-value expression");
3650 return;
3652 case PLUS_EXPR:
3653 opname = "+"; break;
3654 case MINUS_EXPR:
3655 opname = "-"; break;
3656 case MULT_EXPR:
3657 opname = "*"; break;
3658 case MAX_EXPR:
3659 opname = "max"; break;
3660 case MIN_EXPR:
3661 opname = "min"; break;
3662 case EQ_EXPR:
3663 opname = "=="; break;
3664 case NE_EXPR:
3665 opname = "!="; break;
3666 case LE_EXPR:
3667 opname = "<="; break;
3668 case GE_EXPR:
3669 opname = ">="; break;
3670 case LT_EXPR:
3671 opname = "<"; break;
3672 case GT_EXPR:
3673 opname = ">"; break;
3674 case LSHIFT_EXPR:
3675 opname = "<<"; break;
3676 case RSHIFT_EXPR:
3677 opname = ">>"; break;
3678 case TRUNC_MOD_EXPR:
3679 case FLOOR_MOD_EXPR:
3680 opname = "%"; break;
3681 case TRUNC_DIV_EXPR:
3682 case FLOOR_DIV_EXPR:
3683 opname = "/"; break;
3684 case BIT_AND_EXPR:
3685 opname = "&"; break;
3686 case BIT_IOR_EXPR:
3687 opname = "|"; break;
3688 case TRUTH_ANDIF_EXPR:
3689 opname = "&&"; break;
3690 case TRUTH_ORIF_EXPR:
3691 opname = "||"; break;
3692 case BIT_XOR_EXPR:
3693 opname = "^"; break;
3694 case LROTATE_EXPR:
3695 case RROTATE_EXPR:
3696 opname = "rotate"; break;
3697 default:
3698 opname = "unknown"; break;
3700 error ("invalid operands to binary %s", opname);
3703 /* Subroutine of build_binary_op, used for comparison operations.
3704 See if the operands have both been converted from subword integer types
3705 and, if so, perhaps change them both back to their original type.
3706 This function is also responsible for converting the two operands
3707 to the proper common type for comparison.
3709 The arguments of this function are all pointers to local variables
3710 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
3711 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
3713 If this function returns nonzero, it means that the comparison has
3714 a constant value. What this function returns is an expression for
3715 that value. */
3717 tree
3718 shorten_compare (op0_ptr, op1_ptr, restype_ptr, rescode_ptr)
3719 tree *op0_ptr, *op1_ptr;
3720 tree *restype_ptr;
3721 enum tree_code *rescode_ptr;
3723 register tree type;
3724 tree op0 = *op0_ptr;
3725 tree op1 = *op1_ptr;
3726 int unsignedp0, unsignedp1;
3727 int real1, real2;
3728 tree primop0, primop1;
3729 enum tree_code code = *rescode_ptr;
3731 /* Throw away any conversions to wider types
3732 already present in the operands. */
3734 primop0 = get_narrower (op0, &unsignedp0);
3735 primop1 = get_narrower (op1, &unsignedp1);
3737 /* Handle the case that OP0 does not *contain* a conversion
3738 but it *requires* conversion to FINAL_TYPE. */
3740 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
3741 unsignedp0 = TREE_UNSIGNED (TREE_TYPE (op0));
3742 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
3743 unsignedp1 = TREE_UNSIGNED (TREE_TYPE (op1));
3745 /* If one of the operands must be floated, we cannot optimize. */
3746 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
3747 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
3749 /* If first arg is constant, swap the args (changing operation
3750 so value is preserved), for canonicalization. Don't do this if
3751 the second arg is 0. */
3753 if (TREE_CONSTANT (primop0)
3754 && ! integer_zerop (primop1) && ! real_zerop (primop1))
3756 register tree tem = primop0;
3757 register int temi = unsignedp0;
3758 primop0 = primop1;
3759 primop1 = tem;
3760 tem = op0;
3761 op0 = op1;
3762 op1 = tem;
3763 *op0_ptr = op0;
3764 *op1_ptr = op1;
3765 unsignedp0 = unsignedp1;
3766 unsignedp1 = temi;
3767 temi = real1;
3768 real1 = real2;
3769 real2 = temi;
3771 switch (code)
3773 case LT_EXPR:
3774 code = GT_EXPR;
3775 break;
3776 case GT_EXPR:
3777 code = LT_EXPR;
3778 break;
3779 case LE_EXPR:
3780 code = GE_EXPR;
3781 break;
3782 case GE_EXPR:
3783 code = LE_EXPR;
3784 break;
3785 default:
3786 break;
3788 *rescode_ptr = code;
3791 /* If comparing an integer against a constant more bits wide,
3792 maybe we can deduce a value of 1 or 0 independent of the data.
3793 Or else truncate the constant now
3794 rather than extend the variable at run time.
3796 This is only interesting if the constant is the wider arg.
3797 Also, it is not safe if the constant is unsigned and the
3798 variable arg is signed, since in this case the variable
3799 would be sign-extended and then regarded as unsigned.
3800 Our technique fails in this case because the lowest/highest
3801 possible unsigned results don't follow naturally from the
3802 lowest/highest possible values of the variable operand.
3803 For just EQ_EXPR and NE_EXPR there is another technique that
3804 could be used: see if the constant can be faithfully represented
3805 in the other operand's type, by truncating it and reextending it
3806 and see if that preserves the constant's value. */
3808 if (!real1 && !real2
3809 && TREE_CODE (primop1) == INTEGER_CST
3810 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
3812 int min_gt, max_gt, min_lt, max_lt;
3813 tree maxval, minval;
3814 /* 1 if comparison is nominally unsigned. */
3815 int unsignedp = TREE_UNSIGNED (*restype_ptr);
3816 tree val;
3818 type = signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0));
3820 /* If TYPE is an enumeration, then we need to get its min/max
3821 values from it's underlying integral type, not the enumerated
3822 type itself. */
3823 if (TREE_CODE (type) == ENUMERAL_TYPE)
3824 type = type_for_size (TYPE_PRECISION (type), unsignedp0);
3826 maxval = TYPE_MAX_VALUE (type);
3827 minval = TYPE_MIN_VALUE (type);
3829 if (unsignedp && !unsignedp0)
3830 *restype_ptr = signed_type (*restype_ptr);
3832 if (TREE_TYPE (primop1) != *restype_ptr)
3833 primop1 = convert (*restype_ptr, primop1);
3834 if (type != *restype_ptr)
3836 minval = convert (*restype_ptr, minval);
3837 maxval = convert (*restype_ptr, maxval);
3840 if (unsignedp && unsignedp0)
3842 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
3843 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
3844 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
3845 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
3847 else
3849 min_gt = INT_CST_LT (primop1, minval);
3850 max_gt = INT_CST_LT (primop1, maxval);
3851 min_lt = INT_CST_LT (minval, primop1);
3852 max_lt = INT_CST_LT (maxval, primop1);
3855 val = 0;
3856 /* This used to be a switch, but Genix compiler can't handle that. */
3857 if (code == NE_EXPR)
3859 if (max_lt || min_gt)
3860 val = boolean_true_node;
3862 else if (code == EQ_EXPR)
3864 if (max_lt || min_gt)
3865 val = boolean_false_node;
3867 else if (code == LT_EXPR)
3869 if (max_lt)
3870 val = boolean_true_node;
3871 if (!min_lt)
3872 val = boolean_false_node;
3874 else if (code == GT_EXPR)
3876 if (min_gt)
3877 val = boolean_true_node;
3878 if (!max_gt)
3879 val = boolean_false_node;
3881 else if (code == LE_EXPR)
3883 if (!max_gt)
3884 val = boolean_true_node;
3885 if (min_gt)
3886 val = boolean_false_node;
3888 else if (code == GE_EXPR)
3890 if (!min_lt)
3891 val = boolean_true_node;
3892 if (max_lt)
3893 val = boolean_false_node;
3896 /* If primop0 was sign-extended and unsigned comparison specd,
3897 we did a signed comparison above using the signed type bounds.
3898 But the comparison we output must be unsigned.
3900 Also, for inequalities, VAL is no good; but if the signed
3901 comparison had *any* fixed result, it follows that the
3902 unsigned comparison just tests the sign in reverse
3903 (positive values are LE, negative ones GE).
3904 So we can generate an unsigned comparison
3905 against an extreme value of the signed type. */
3907 if (unsignedp && !unsignedp0)
3909 if (val != 0)
3910 switch (code)
3912 case LT_EXPR:
3913 case GE_EXPR:
3914 primop1 = TYPE_MIN_VALUE (type);
3915 val = 0;
3916 break;
3918 case LE_EXPR:
3919 case GT_EXPR:
3920 primop1 = TYPE_MAX_VALUE (type);
3921 val = 0;
3922 break;
3924 default:
3925 break;
3927 type = unsigned_type (type);
3930 if (!max_gt && !unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
3932 /* This is the case of (char)x >?< 0x80, which people used to use
3933 expecting old C compilers to change the 0x80 into -0x80. */
3934 if (val == boolean_false_node)
3935 warning ("comparison is always false due to limited range of data type");
3936 if (val == boolean_true_node)
3937 warning ("comparison is always true due to limited range of data type");
3940 if (!min_lt && unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
3942 /* This is the case of (unsigned char)x >?< -1 or < 0. */
3943 if (val == boolean_false_node)
3944 warning ("comparison is always false due to limited range of data type");
3945 if (val == boolean_true_node)
3946 warning ("comparison is always true due to limited range of data type");
3949 if (val != 0)
3951 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
3952 if (TREE_SIDE_EFFECTS (primop0))
3953 return build (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
3954 return val;
3957 /* Value is not predetermined, but do the comparison
3958 in the type of the operand that is not constant.
3959 TYPE is already properly set. */
3961 else if (real1 && real2
3962 && (TYPE_PRECISION (TREE_TYPE (primop0))
3963 == TYPE_PRECISION (TREE_TYPE (primop1))))
3964 type = TREE_TYPE (primop0);
3966 /* If args' natural types are both narrower than nominal type
3967 and both extend in the same manner, compare them
3968 in the type of the wider arg.
3969 Otherwise must actually extend both to the nominal
3970 common type lest different ways of extending
3971 alter the result.
3972 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
3974 else if (unsignedp0 == unsignedp1 && real1 == real2
3975 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
3976 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
3978 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
3979 type = signed_or_unsigned_type (unsignedp0
3980 || TREE_UNSIGNED (*restype_ptr),
3981 type);
3982 /* Make sure shorter operand is extended the right way
3983 to match the longer operand. */
3984 primop0 = convert (signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0)),
3985 primop0);
3986 primop1 = convert (signed_or_unsigned_type (unsignedp1, TREE_TYPE (primop1)),
3987 primop1);
3989 else
3991 /* Here we must do the comparison on the nominal type
3992 using the args exactly as we received them. */
3993 type = *restype_ptr;
3994 primop0 = op0;
3995 primop1 = op1;
3997 if (!real1 && !real2 && integer_zerop (primop1)
3998 && TREE_UNSIGNED (*restype_ptr))
4000 tree value = 0;
4001 switch (code)
4003 case GE_EXPR:
4004 /* All unsigned values are >= 0, so we warn if extra warnings
4005 are requested. However, if OP0 is a constant that is
4006 >= 0, the signedness of the comparison isn't an issue,
4007 so suppress the warning. */
4008 if (extra_warnings && !in_system_header
4009 && ! (TREE_CODE (primop0) == INTEGER_CST
4010 && ! TREE_OVERFLOW (convert (signed_type (type),
4011 primop0))))
4012 warning ("comparison of unsigned expression >= 0 is always true");
4013 value = boolean_true_node;
4014 break;
4016 case LT_EXPR:
4017 if (extra_warnings && !in_system_header
4018 && ! (TREE_CODE (primop0) == INTEGER_CST
4019 && ! TREE_OVERFLOW (convert (signed_type (type),
4020 primop0))))
4021 warning ("comparison of unsigned expression < 0 is always false");
4022 value = boolean_false_node;
4023 break;
4025 default:
4026 break;
4029 if (value != 0)
4031 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4032 if (TREE_SIDE_EFFECTS (primop0))
4033 return build (COMPOUND_EXPR, TREE_TYPE (value),
4034 primop0, value);
4035 return value;
4040 *op0_ptr = convert (type, primop0);
4041 *op1_ptr = convert (type, primop1);
4043 *restype_ptr = boolean_type_node;
4045 return 0;
4048 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
4049 or validate its data type for an `if' or `while' statement or ?..: exp.
4051 This preparation consists of taking the ordinary
4052 representation of an expression expr and producing a valid tree
4053 boolean expression describing whether expr is nonzero. We could
4054 simply always do build_binary_op (NE_EXPR, expr, boolean_false_node, 1),
4055 but we optimize comparisons, &&, ||, and !.
4057 The resulting type should always be `boolean_type_node'. */
4059 tree
4060 truthvalue_conversion (expr)
4061 tree expr;
4063 if (TREE_CODE (expr) == ERROR_MARK)
4064 return expr;
4066 #if 0 /* This appears to be wrong for C++. */
4067 /* These really should return error_mark_node after 2.4 is stable.
4068 But not all callers handle ERROR_MARK properly. */
4069 switch (TREE_CODE (TREE_TYPE (expr)))
4071 case RECORD_TYPE:
4072 error ("struct type value used where scalar is required");
4073 return boolean_false_node;
4075 case UNION_TYPE:
4076 error ("union type value used where scalar is required");
4077 return boolean_false_node;
4079 case ARRAY_TYPE:
4080 error ("array type value used where scalar is required");
4081 return boolean_false_node;
4083 default:
4084 break;
4086 #endif /* 0 */
4088 switch (TREE_CODE (expr))
4090 case EQ_EXPR:
4091 case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4092 case TRUTH_ANDIF_EXPR:
4093 case TRUTH_ORIF_EXPR:
4094 case TRUTH_AND_EXPR:
4095 case TRUTH_OR_EXPR:
4096 case TRUTH_XOR_EXPR:
4097 case TRUTH_NOT_EXPR:
4098 TREE_TYPE (expr) = boolean_type_node;
4099 return expr;
4101 case ERROR_MARK:
4102 return expr;
4104 case INTEGER_CST:
4105 return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
4107 case REAL_CST:
4108 return real_zerop (expr) ? boolean_false_node : boolean_true_node;
4110 case ADDR_EXPR:
4111 /* If we are taking the address of a external decl, it might be zero
4112 if it is weak, so we cannot optimize. */
4113 if (DECL_P (TREE_OPERAND (expr, 0))
4114 && DECL_EXTERNAL (TREE_OPERAND (expr, 0)))
4115 break;
4117 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0)))
4118 return build (COMPOUND_EXPR, boolean_type_node,
4119 TREE_OPERAND (expr, 0), boolean_true_node);
4120 else
4121 return boolean_true_node;
4123 case COMPLEX_EXPR:
4124 return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
4125 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4126 truthvalue_conversion (TREE_OPERAND (expr, 0)),
4127 truthvalue_conversion (TREE_OPERAND (expr, 1)),
4130 case NEGATE_EXPR:
4131 case ABS_EXPR:
4132 case FLOAT_EXPR:
4133 case FFS_EXPR:
4134 /* These don't change whether an object is non-zero or zero. */
4135 return truthvalue_conversion (TREE_OPERAND (expr, 0));
4137 case LROTATE_EXPR:
4138 case RROTATE_EXPR:
4139 /* These don't change whether an object is zero or non-zero, but
4140 we can't ignore them if their second arg has side-effects. */
4141 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
4142 return build (COMPOUND_EXPR, boolean_type_node, TREE_OPERAND (expr, 1),
4143 truthvalue_conversion (TREE_OPERAND (expr, 0)));
4144 else
4145 return truthvalue_conversion (TREE_OPERAND (expr, 0));
4147 case COND_EXPR:
4148 /* Distribute the conversion into the arms of a COND_EXPR. */
4149 return fold (build (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
4150 truthvalue_conversion (TREE_OPERAND (expr, 1)),
4151 truthvalue_conversion (TREE_OPERAND (expr, 2))));
4153 case CONVERT_EXPR:
4154 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4155 since that affects how `default_conversion' will behave. */
4156 if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE
4157 || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
4158 break;
4159 /* fall through... */
4160 case NOP_EXPR:
4161 /* If this is widening the argument, we can ignore it. */
4162 if (TYPE_PRECISION (TREE_TYPE (expr))
4163 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
4164 return truthvalue_conversion (TREE_OPERAND (expr, 0));
4165 break;
4167 case MINUS_EXPR:
4168 /* With IEEE arithmetic, x - x may not equal 0, so we can't optimize
4169 this case. */
4170 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
4171 && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE)
4172 break;
4173 /* fall through... */
4174 case BIT_XOR_EXPR:
4175 /* This and MINUS_EXPR can be changed into a comparison of the
4176 two objects. */
4177 if (TREE_TYPE (TREE_OPERAND (expr, 0))
4178 == TREE_TYPE (TREE_OPERAND (expr, 1)))
4179 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
4180 TREE_OPERAND (expr, 1), 1);
4181 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
4182 fold (build1 (NOP_EXPR,
4183 TREE_TYPE (TREE_OPERAND (expr, 0)),
4184 TREE_OPERAND (expr, 1))), 1);
4186 case BIT_AND_EXPR:
4187 if (integer_onep (TREE_OPERAND (expr, 1))
4188 && TREE_TYPE (expr) != boolean_type_node)
4189 /* Using convert here would cause infinite recursion. */
4190 return build1 (NOP_EXPR, boolean_type_node, expr);
4191 break;
4193 case MODIFY_EXPR:
4194 if (warn_parentheses && C_EXP_ORIGINAL_CODE (expr) == MODIFY_EXPR)
4195 warning ("suggest parentheses around assignment used as truth value");
4196 break;
4198 default:
4199 break;
4202 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
4204 tree tem = save_expr (expr);
4205 return (build_binary_op
4206 ((TREE_SIDE_EFFECTS (expr)
4207 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4208 truthvalue_conversion (build_unary_op (REALPART_EXPR, tem, 0)),
4209 truthvalue_conversion (build_unary_op (IMAGPART_EXPR, tem, 0)),
4210 0));
4213 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
4216 #if !USE_CPPLIB
4217 /* Read the rest of a #-directive from input stream FINPUT.
4218 In normal use, the directive name and the white space after it
4219 have already been read, so they won't be included in the result.
4220 We allow for the fact that the directive line may contain
4221 a newline embedded within a character or string literal which forms
4222 a part of the directive.
4224 The value is a string in a reusable buffer. It remains valid
4225 only until the next time this function is called.
4227 The terminating character ('\n' or EOF) is left in FINPUT for the
4228 caller to re-read. */
4230 char *
4231 get_directive_line (finput)
4232 register FILE *finput;
4234 static char *directive_buffer = NULL;
4235 static unsigned buffer_length = 0;
4236 register char *p;
4237 register char *buffer_limit;
4238 register int looking_for = 0;
4239 register int char_escaped = 0;
4241 if (buffer_length == 0)
4243 directive_buffer = (char *)xmalloc (128);
4244 buffer_length = 128;
4247 buffer_limit = &directive_buffer[buffer_length];
4249 for (p = directive_buffer; ; )
4251 int c;
4253 /* Make buffer bigger if it is full. */
4254 if (p >= buffer_limit)
4256 register unsigned bytes_used = (p - directive_buffer);
4258 buffer_length *= 2;
4259 directive_buffer
4260 = (char *)xrealloc (directive_buffer, buffer_length);
4261 p = &directive_buffer[bytes_used];
4262 buffer_limit = &directive_buffer[buffer_length];
4265 c = getc (finput);
4267 /* Discard initial whitespace. */
4268 if ((c == ' ' || c == '\t') && p == directive_buffer)
4269 continue;
4271 /* Detect the end of the directive. */
4272 if (looking_for == 0
4273 && (c == '\n' || c == EOF))
4275 ungetc (c, finput);
4276 c = '\0';
4279 *p++ = c;
4281 if (c == 0)
4282 return directive_buffer;
4284 /* Handle string and character constant syntax. */
4285 if (looking_for)
4287 if (looking_for == c && !char_escaped)
4288 looking_for = 0; /* Found terminator... stop looking. */
4290 else
4291 if (c == '\'' || c == '"')
4292 looking_for = c; /* Don't stop buffering until we see another
4293 one of these (or an EOF). */
4295 /* Handle backslash. */
4296 char_escaped = (c == '\\' && ! char_escaped);
4299 #endif /* USE_CPPLIB */
4301 /* Make a variant type in the proper way for C/C++, propagating qualifiers
4302 down to the element type of an array. */
4304 tree
4305 c_build_qualified_type (type, type_quals)
4306 tree type;
4307 int type_quals;
4309 /* A restrict-qualified pointer type must be a pointer to object or
4310 incomplete type. Note that the use of POINTER_TYPE_P also allows
4311 REFERENCE_TYPEs, which is appropriate for C++. Unfortunately,
4312 the C++ front-end also use POINTER_TYPE for pointer-to-member
4313 values, so even though it should be illegal to use `restrict'
4314 with such an entity we don't flag that here. Thus, special case
4315 code for that case is required in the C++ front-end. */
4316 if ((type_quals & TYPE_QUAL_RESTRICT)
4317 && (!POINTER_TYPE_P (type)
4318 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
4320 error ("invalid use of `restrict'");
4321 type_quals &= ~TYPE_QUAL_RESTRICT;
4324 if (TREE_CODE (type) == ARRAY_TYPE)
4325 return build_array_type (c_build_qualified_type (TREE_TYPE (type),
4326 type_quals),
4327 TYPE_DOMAIN (type));
4328 return build_qualified_type (type, type_quals);
4331 /* Apply the TYPE_QUALS to the new DECL. */
4333 void
4334 c_apply_type_quals_to_decl (type_quals, decl)
4335 int type_quals;
4336 tree decl;
4338 if ((type_quals & TYPE_QUAL_CONST)
4339 || (TREE_TYPE (decl)
4340 && TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE))
4341 TREE_READONLY (decl) = 1;
4342 if (type_quals & TYPE_QUAL_VOLATILE)
4344 TREE_SIDE_EFFECTS (decl) = 1;
4345 TREE_THIS_VOLATILE (decl) = 1;
4347 if (type_quals & TYPE_QUAL_RESTRICT)
4349 if (!TREE_TYPE (decl)
4350 || !POINTER_TYPE_P (TREE_TYPE (decl))
4351 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (TREE_TYPE (decl))))
4352 error ("invalid use of `restrict'");
4353 else if (flag_strict_aliasing)
4355 /* No two restricted pointers can point at the same thing.
4356 However, a restricted pointer can point at the same thing
4357 as an unrestricted pointer, if that unrestricted pointer
4358 is based on the restricted pointer. So, we make the
4359 alias set for the restricted pointer a subset of the
4360 alias set for the type pointed to by the type of the
4361 decl. */
4363 HOST_WIDE_INT pointed_to_alias_set
4364 = get_alias_set (TREE_TYPE (TREE_TYPE (decl)));
4366 if (pointed_to_alias_set == 0)
4367 /* It's not legal to make a subset of alias set zero. */
4369 else
4371 DECL_POINTER_ALIAS_SET (decl) = new_alias_set ();
4372 record_alias_subset (pointed_to_alias_set,
4373 DECL_POINTER_ALIAS_SET (decl));
4380 /* Return the typed-based alias set for T, which may be an expression
4381 or a type. Return -1 if we don't do anything special. */
4383 HOST_WIDE_INT
4384 lang_get_alias_set (t)
4385 tree t;
4387 tree u;
4389 /* Permit type-punning when accessing a union, provided the access
4390 is directly through the union. For example, this code does not
4391 permit taking the address of a union member and then storing
4392 through it. Even the type-punning allowed here is a GCC
4393 extension, albeit a common and useful one; the C standard says
4394 that such accesses have implementation-defined behavior. */
4395 for (u = t;
4396 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
4397 u = TREE_OPERAND (u, 0))
4398 if (TREE_CODE (u) == COMPONENT_REF
4399 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
4400 return 0;
4402 /* If this is a char *, the ANSI C standard says it can alias
4403 anything. Note that all references need do this. */
4404 if (TREE_CODE_CLASS (TREE_CODE (t)) == 'r'
4405 && TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
4406 && TYPE_PRECISION (TREE_TYPE (t)) == TYPE_PRECISION (char_type_node))
4407 return 0;
4409 /* That's all the expressions we handle specially. */
4410 if (! TYPE_P (t))
4411 return -1;
4413 /* The C standard specifically allows aliasing between signed and
4414 unsigned variants of the same type. We treat the signed
4415 variant as canonical. */
4416 if (TREE_CODE (t) == INTEGER_TYPE && TREE_UNSIGNED (t))
4418 tree t1 = signed_type (t);
4420 /* t1 == t can happen for boolean nodes which are always unsigned. */
4421 if (t1 != t)
4422 return get_alias_set (t1);
4424 else if (POINTER_TYPE_P (t))
4426 tree t1;
4428 /* Unfortunately, there is no canonical form of a pointer type.
4429 In particular, if we have `typedef int I', then `int *', and
4430 `I *' are different types. So, we have to pick a canonical
4431 representative. We do this below.
4433 Technically, this approach is actually more conservative that
4434 it needs to be. In particular, `const int *' and `int *'
4435 chould be in different alias sets, according to the C and C++
4436 standard, since their types are not the same, and so,
4437 technically, an `int **' and `const int **' cannot point at
4438 the same thing.
4440 But, the standard is wrong. In particular, this code is
4441 legal C++:
4443 int *ip;
4444 int **ipp = &ip;
4445 const int* const* cipp = &ip;
4447 And, it doesn't make sense for that to be legal unless you
4448 can dereference IPP and CIPP. So, we ignore cv-qualifiers on
4449 the pointed-to types. This issue has been reported to the
4450 C++ committee. */
4451 t1 = TYPE_MAIN_VARIANT (TREE_TYPE (t));
4452 t1 = ((TREE_CODE (t) == POINTER_TYPE)
4453 ? build_pointer_type (t1) : build_reference_type (t1));
4454 if (t1 != t)
4455 return get_alias_set (t1);
4457 /* It's not yet safe to use alias sets for classes in C++ because
4458 the TYPE_FIELDs list for a class doesn't mention base classes. */
4459 else if (c_language == clk_cplusplus && AGGREGATE_TYPE_P (t))
4460 return 0;
4462 return -1;
4465 /* Build tree nodes and builtin functions common to both C and C++ language
4466 frontends.
4467 CPLUS_MODE is nonzero if we are called from the C++ frontend, we generate
4468 some stricter prototypes in that case.
4469 NO_BUILTINS and NO_NONANSI_BUILTINS contain the respective values of
4470 the language frontend flags flag_no_builtin and
4471 flag_no_nonansi_builtin. */
4473 void
4474 c_common_nodes_and_builtins (cplus_mode, no_builtins, no_nonansi_builtins)
4475 int cplus_mode, no_builtins, no_nonansi_builtins;
4477 tree temp;
4478 tree memcpy_ftype, memset_ftype, strlen_ftype;
4479 tree bzero_ftype, bcmp_ftype, puts_ftype, printf_ftype;
4480 tree endlink, int_endlink, double_endlink, unsigned_endlink;
4481 tree sizetype_endlink;
4482 tree ptr_ftype, ptr_ftype_unsigned;
4483 tree void_ftype_any, void_ftype_int, int_ftype_any, sizet_ftype_any;
4484 tree double_ftype_double, double_ftype_double_double;
4485 tree float_ftype_float, ldouble_ftype_ldouble;
4486 tree int_ftype_cptr_cptr_sizet;
4487 tree int_ftype_string_string, string_ftype_ptr_ptr;
4488 tree long_ftype_long;
4489 tree longlong_ftype_longlong;
4490 /* Either char* or void*. */
4491 tree traditional_ptr_type_node;
4492 /* Either const char* or const void*. */
4493 tree traditional_cptr_type_node;
4494 tree traditional_len_type_node;
4495 tree traditional_len_endlink;
4496 tree va_list_ref_type_node;
4497 tree va_list_arg_type_node;
4499 pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_va_list"),
4500 va_list_type_node));
4502 pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_ptrdiff_t"),
4503 ptrdiff_type_node));
4505 pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_size_t"),
4506 sizetype));
4508 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
4510 va_list_arg_type_node = va_list_ref_type_node =
4511 build_pointer_type (TREE_TYPE (va_list_type_node));
4513 else
4515 va_list_arg_type_node = va_list_type_node;
4516 va_list_ref_type_node = build_reference_type (va_list_type_node);
4519 endlink = void_list_node;
4520 int_endlink = tree_cons (NULL_TREE, integer_type_node, endlink);
4521 double_endlink = tree_cons (NULL_TREE, double_type_node, endlink);
4522 unsigned_endlink = tree_cons (NULL_TREE, unsigned_type_node, endlink);
4524 ptr_ftype = build_function_type (ptr_type_node, NULL_TREE);
4525 ptr_ftype_unsigned = build_function_type (ptr_type_node, unsigned_endlink);
4526 sizetype_endlink = tree_cons (NULL_TREE, TYPE_DOMAIN (sizetype), endlink);
4527 /* We realloc here because sizetype could be int or unsigned. S'ok. */
4528 ptr_ftype_sizetype = build_function_type (ptr_type_node, sizetype_endlink);
4530 sizet_ftype_any = build_function_type (sizetype, NULL_TREE);
4531 int_ftype_any = build_function_type (integer_type_node, NULL_TREE);
4532 void_ftype_any = build_function_type (void_type_node, NULL_TREE);
4533 void_ftype = build_function_type (void_type_node, endlink);
4534 void_ftype_int = build_function_type (void_type_node, int_endlink);
4535 void_ftype_ptr
4536 = build_function_type (void_type_node,
4537 tree_cons (NULL_TREE, ptr_type_node, endlink));
4539 float_ftype_float
4540 = build_function_type (float_type_node,
4541 tree_cons (NULL_TREE, float_type_node, endlink));
4543 double_ftype_double
4544 = build_function_type (double_type_node, double_endlink);
4546 ldouble_ftype_ldouble
4547 = build_function_type (long_double_type_node,
4548 tree_cons (NULL_TREE, long_double_type_node,
4549 endlink));
4551 double_ftype_double_double
4552 = build_function_type (double_type_node,
4553 tree_cons (NULL_TREE, double_type_node,
4554 double_endlink));
4556 int_ftype_int
4557 = build_function_type (integer_type_node, int_endlink);
4559 long_ftype_long
4560 = build_function_type (long_integer_type_node,
4561 tree_cons (NULL_TREE, long_integer_type_node,
4562 endlink));
4564 longlong_ftype_longlong
4565 = build_function_type (long_long_integer_type_node,
4566 tree_cons (NULL_TREE, long_long_integer_type_node,
4567 endlink));
4569 int_ftype_cptr_cptr_sizet
4570 = build_function_type (integer_type_node,
4571 tree_cons (NULL_TREE, const_ptr_type_node,
4572 tree_cons (NULL_TREE, const_ptr_type_node,
4573 tree_cons (NULL_TREE,
4574 sizetype,
4575 endlink))));
4577 void_zero_node = build_int_2 (0, 0);
4578 TREE_TYPE (void_zero_node) = void_type_node;
4580 /* Prototype for strcpy. */
4581 string_ftype_ptr_ptr
4582 = build_function_type (string_type_node,
4583 tree_cons (NULL_TREE, string_type_node,
4584 tree_cons (NULL_TREE,
4585 const_string_type_node,
4586 endlink)));
4588 traditional_len_type_node = (flag_traditional && ! cplus_mode
4589 ? integer_type_node : sizetype);
4590 traditional_len_endlink = tree_cons (NULL_TREE, traditional_len_type_node,
4591 endlink);
4593 /* Prototype for strcmp. */
4594 int_ftype_string_string
4595 = build_function_type (integer_type_node,
4596 tree_cons (NULL_TREE, const_string_type_node,
4597 tree_cons (NULL_TREE,
4598 const_string_type_node,
4599 endlink)));
4601 /* Prototype for strlen. */
4602 strlen_ftype
4603 = build_function_type (traditional_len_type_node,
4604 tree_cons (NULL_TREE, const_string_type_node,
4605 endlink));
4607 traditional_ptr_type_node = (flag_traditional && ! cplus_mode
4608 ? string_type_node : ptr_type_node);
4609 traditional_cptr_type_node = (flag_traditional && ! cplus_mode
4610 ? const_string_type_node : const_ptr_type_node);
4612 /* Prototype for memcpy. */
4613 memcpy_ftype
4614 = build_function_type (traditional_ptr_type_node,
4615 tree_cons (NULL_TREE, ptr_type_node,
4616 tree_cons (NULL_TREE, const_ptr_type_node,
4617 sizetype_endlink)));
4619 /* Prototype for memset. */
4620 memset_ftype
4621 = build_function_type (traditional_ptr_type_node,
4622 tree_cons (NULL_TREE, ptr_type_node,
4623 tree_cons (NULL_TREE, integer_type_node,
4624 tree_cons (NULL_TREE,
4625 sizetype,
4626 endlink))));
4628 /* Prototype for bzero. */
4629 bzero_ftype
4630 = build_function_type (void_type_node,
4631 tree_cons (NULL_TREE, traditional_ptr_type_node,
4632 traditional_len_endlink));
4634 /* Prototype for bcmp. */
4635 bcmp_ftype
4636 = build_function_type (integer_type_node,
4637 tree_cons (NULL_TREE, traditional_cptr_type_node,
4638 tree_cons (NULL_TREE,
4639 traditional_cptr_type_node,
4640 traditional_len_endlink)));
4642 /* Prototype for puts. */
4643 puts_ftype
4644 = build_function_type (integer_type_node,
4645 tree_cons (NULL_TREE, const_string_type_node,
4646 endlink));
4648 /* Prototype for printf. */
4649 printf_ftype
4650 = build_function_type (integer_type_node,
4651 tree_cons (NULL_TREE, const_string_type_node,
4652 NULL_TREE));
4654 builtin_function ("__builtin_constant_p", default_function_type,
4655 BUILT_IN_CONSTANT_P, BUILT_IN_NORMAL, NULL_PTR);
4657 builtin_function ("__builtin_return_address", ptr_ftype_unsigned,
4658 BUILT_IN_RETURN_ADDRESS, BUILT_IN_NORMAL, NULL_PTR);
4660 builtin_function ("__builtin_frame_address", ptr_ftype_unsigned,
4661 BUILT_IN_FRAME_ADDRESS, BUILT_IN_NORMAL, NULL_PTR);
4663 builtin_function ("__builtin_alloca", ptr_ftype_sizetype,
4664 BUILT_IN_ALLOCA, BUILT_IN_NORMAL, "alloca");
4665 builtin_function ("__builtin_ffs", int_ftype_int, BUILT_IN_FFS,
4666 BUILT_IN_NORMAL, NULL_PTR);
4667 /* Define alloca, ffs as builtins.
4668 Declare _exit just to mark it as volatile. */
4669 if (! no_builtins && ! no_nonansi_builtins)
4671 #ifndef SMALL_STACK
4672 temp = builtin_function ("alloca", ptr_ftype_sizetype,
4673 BUILT_IN_ALLOCA, BUILT_IN_NORMAL, NULL_PTR);
4674 /* Suppress error if redefined as a non-function. */
4675 DECL_BUILT_IN_NONANSI (temp) = 1;
4676 #endif
4677 temp = builtin_function ("ffs", int_ftype_int, BUILT_IN_FFS,
4678 BUILT_IN_NORMAL, NULL_PTR);
4679 /* Suppress error if redefined as a non-function. */
4680 DECL_BUILT_IN_NONANSI (temp) = 1;
4681 temp = builtin_function ("_exit", void_ftype_int,
4682 0, NOT_BUILT_IN, NULL_PTR);
4683 TREE_THIS_VOLATILE (temp) = 1;
4684 TREE_SIDE_EFFECTS (temp) = 1;
4685 /* Suppress error if redefined as a non-function. */
4686 DECL_BUILT_IN_NONANSI (temp) = 1;
4688 /* The system prototypes for these functions have many
4689 variations, so don't specify parameters to avoid conflicts.
4690 The expand_* functions check the argument types anyway. */
4691 temp = builtin_function ("bzero", void_ftype_any,
4692 BUILT_IN_BZERO, BUILT_IN_NORMAL, NULL_PTR);
4693 DECL_BUILT_IN_NONANSI (temp) = 1;
4694 temp = builtin_function ("bcmp", int_ftype_any,
4695 BUILT_IN_BCMP, BUILT_IN_NORMAL, NULL_PTR);
4696 DECL_BUILT_IN_NONANSI (temp) = 1;
4699 builtin_function ("__builtin_abs", int_ftype_int, BUILT_IN_ABS,
4700 BUILT_IN_NORMAL, NULL_PTR);
4701 builtin_function ("__builtin_fabsf", float_ftype_float, BUILT_IN_FABS,
4702 BUILT_IN_NORMAL, NULL_PTR);
4703 builtin_function ("__builtin_fabs", double_ftype_double, BUILT_IN_FABS,
4704 BUILT_IN_NORMAL, NULL_PTR);
4705 builtin_function ("__builtin_fabsl", ldouble_ftype_ldouble, BUILT_IN_FABS,
4706 BUILT_IN_NORMAL, NULL_PTR);
4707 builtin_function ("__builtin_labs", long_ftype_long, BUILT_IN_LABS,
4708 BUILT_IN_NORMAL, NULL_PTR);
4709 builtin_function ("__builtin_llabs", longlong_ftype_longlong, BUILT_IN_LLABS,
4710 BUILT_IN_NORMAL, NULL_PTR);
4711 builtin_function ("__builtin_saveregs", ptr_ftype, BUILT_IN_SAVEREGS,
4712 BUILT_IN_NORMAL, NULL_PTR);
4713 builtin_function ("__builtin_classify_type", default_function_type,
4714 BUILT_IN_CLASSIFY_TYPE, BUILT_IN_NORMAL, NULL_PTR);
4715 builtin_function ("__builtin_next_arg", ptr_ftype, BUILT_IN_NEXT_ARG,
4716 BUILT_IN_NORMAL, NULL_PTR);
4717 builtin_function ("__builtin_args_info", int_ftype_int, BUILT_IN_ARGS_INFO,
4718 BUILT_IN_NORMAL, NULL_PTR);
4719 builtin_function ("__builtin_setjmp",
4720 build_function_type (integer_type_node,
4721 tree_cons (NULL_TREE, ptr_type_node,
4722 endlink)),
4723 BUILT_IN_SETJMP, BUILT_IN_NORMAL, NULL_PTR);
4724 builtin_function ("__builtin_longjmp",
4725 build_function_type (void_type_node,
4726 tree_cons (NULL_TREE, ptr_type_node,
4727 tree_cons (NULL_TREE,
4728 integer_type_node,
4729 endlink))),
4730 BUILT_IN_LONGJMP, BUILT_IN_NORMAL, NULL_PTR);
4731 builtin_function ("__builtin_trap", void_ftype, BUILT_IN_TRAP,
4732 BUILT_IN_NORMAL, NULL_PTR);
4734 /* ISO C99 IEEE Unordered compares. */
4735 builtin_function ("__builtin_isgreater", default_function_type,
4736 BUILT_IN_ISGREATER, BUILT_IN_NORMAL, NULL_PTR);
4737 builtin_function ("__builtin_isgreaterequal", default_function_type,
4738 BUILT_IN_ISGREATEREQUAL, BUILT_IN_NORMAL, NULL_PTR);
4739 builtin_function ("__builtin_isless", default_function_type,
4740 BUILT_IN_ISLESS, BUILT_IN_NORMAL, NULL_PTR);
4741 builtin_function ("__builtin_islessequal", default_function_type,
4742 BUILT_IN_ISLESSEQUAL, BUILT_IN_NORMAL, NULL_PTR);
4743 builtin_function ("__builtin_islessgreater", default_function_type,
4744 BUILT_IN_ISLESSGREATER, BUILT_IN_NORMAL, NULL_PTR);
4745 builtin_function ("__builtin_isunordered", default_function_type,
4746 BUILT_IN_ISUNORDERED, BUILT_IN_NORMAL, NULL_PTR);
4748 /* Untyped call and return. */
4749 builtin_function ("__builtin_apply_args", ptr_ftype,
4750 BUILT_IN_APPLY_ARGS, BUILT_IN_NORMAL, NULL_PTR);
4752 temp = tree_cons (NULL_TREE,
4753 build_pointer_type (build_function_type (void_type_node,
4754 NULL_TREE)),
4755 tree_cons (NULL_TREE,
4756 ptr_type_node,
4757 tree_cons (NULL_TREE,
4758 sizetype,
4759 endlink)));
4760 builtin_function ("__builtin_apply",
4761 build_function_type (ptr_type_node, temp),
4762 BUILT_IN_APPLY, BUILT_IN_NORMAL, NULL_PTR);
4763 builtin_function ("__builtin_return", void_ftype_ptr,
4764 BUILT_IN_RETURN, BUILT_IN_NORMAL, NULL_PTR);
4766 /* Support for varargs.h and stdarg.h. */
4767 builtin_function ("__builtin_varargs_start",
4768 build_function_type (void_type_node,
4769 tree_cons (NULL_TREE,
4770 va_list_ref_type_node,
4771 endlink)),
4772 BUILT_IN_VARARGS_START, BUILT_IN_NORMAL, NULL_PTR);
4774 builtin_function ("__builtin_stdarg_start",
4775 build_function_type (void_type_node,
4776 tree_cons (NULL_TREE,
4777 va_list_ref_type_node,
4778 NULL_TREE)),
4779 BUILT_IN_STDARG_START, BUILT_IN_NORMAL, NULL_PTR);
4781 builtin_function ("__builtin_va_end",
4782 build_function_type (void_type_node,
4783 tree_cons (NULL_TREE,
4784 va_list_ref_type_node,
4785 endlink)),
4786 BUILT_IN_VA_END, BUILT_IN_NORMAL, NULL_PTR);
4788 builtin_function ("__builtin_va_copy",
4789 build_function_type (void_type_node,
4790 tree_cons (NULL_TREE,
4791 va_list_ref_type_node,
4792 tree_cons (NULL_TREE,
4793 va_list_arg_type_node,
4794 endlink))),
4795 BUILT_IN_VA_COPY, BUILT_IN_NORMAL, NULL_PTR);
4797 /* ??? Ought to be `T __builtin_expect(T, T)' for any type T. */
4798 builtin_function ("__builtin_expect",
4799 build_function_type (long_integer_type_node,
4800 tree_cons (NULL_TREE,
4801 long_integer_type_node,
4802 tree_cons (NULL_TREE,
4803 long_integer_type_node,
4804 endlink))),
4805 BUILT_IN_EXPECT, BUILT_IN_NORMAL, NULL_PTR);
4807 /* Currently under experimentation. */
4808 builtin_function ("__builtin_memcpy", memcpy_ftype, BUILT_IN_MEMCPY,
4809 BUILT_IN_NORMAL, "memcpy");
4810 builtin_function ("__builtin_memcmp", int_ftype_cptr_cptr_sizet,
4811 BUILT_IN_MEMCMP, BUILT_IN_NORMAL, "memcmp");
4812 builtin_function ("__builtin_memset", memset_ftype,
4813 BUILT_IN_MEMSET, BUILT_IN_NORMAL, "memset");
4814 builtin_function ("__builtin_bzero", bzero_ftype,
4815 BUILT_IN_BZERO, BUILT_IN_NORMAL, "bzero");
4816 builtin_function ("__builtin_bcmp", bcmp_ftype,
4817 BUILT_IN_BCMP, BUILT_IN_NORMAL, "bcmp");
4818 builtin_function ("__builtin_strcmp", int_ftype_string_string,
4819 BUILT_IN_STRCMP, BUILT_IN_NORMAL, "strcmp");
4820 builtin_function ("__builtin_strcpy", string_ftype_ptr_ptr,
4821 BUILT_IN_STRCPY, BUILT_IN_NORMAL, "strcpy");
4822 builtin_function ("__builtin_strlen", strlen_ftype,
4823 BUILT_IN_STRLEN, BUILT_IN_NORMAL, "strlen");
4824 builtin_function ("__builtin_sqrtf", float_ftype_float,
4825 BUILT_IN_FSQRT, BUILT_IN_NORMAL, "sqrtf");
4826 builtin_function ("__builtin_fsqrt", double_ftype_double,
4827 BUILT_IN_FSQRT, BUILT_IN_NORMAL, "sqrt");
4828 builtin_function ("__builtin_sqrtl", ldouble_ftype_ldouble,
4829 BUILT_IN_FSQRT, BUILT_IN_NORMAL, "sqrtl");
4830 builtin_function ("__builtin_sinf", float_ftype_float,
4831 BUILT_IN_SIN, BUILT_IN_NORMAL, "sinf");
4832 builtin_function ("__builtin_sin", double_ftype_double,
4833 BUILT_IN_SIN, BUILT_IN_NORMAL, "sin");
4834 builtin_function ("__builtin_sinl", ldouble_ftype_ldouble,
4835 BUILT_IN_SIN, BUILT_IN_NORMAL, "sinl");
4836 builtin_function ("__builtin_cosf", float_ftype_float,
4837 BUILT_IN_COS, BUILT_IN_NORMAL, "cosf");
4838 builtin_function ("__builtin_cos", double_ftype_double,
4839 BUILT_IN_COS, BUILT_IN_NORMAL, "cos");
4840 builtin_function ("__builtin_cosl", ldouble_ftype_ldouble,
4841 BUILT_IN_COS, BUILT_IN_NORMAL, "cosl");
4842 built_in_decls[BUILT_IN_PUTCHAR] =
4843 builtin_function ("__builtin_putchar", int_ftype_int,
4844 BUILT_IN_PUTCHAR, BUILT_IN_NORMAL, "putchar");
4845 built_in_decls[BUILT_IN_PUTS] =
4846 builtin_function ("__builtin_puts", puts_ftype,
4847 BUILT_IN_PUTS, BUILT_IN_NORMAL, "puts");
4848 builtin_function ("__builtin_printf", printf_ftype,
4849 BUILT_IN_PRINTF, BUILT_IN_FRONTEND, "printf");
4850 /* We declare these without argument so that the initial declaration
4851 for these identifiers is a builtin. That allows us to redeclare
4852 them later with argument without worrying about the explicit
4853 declarations in stdio.h being taken as the initial declaration.
4854 Also, save the _DECL for these so we can use them later. */
4855 built_in_decls[BUILT_IN_FWRITE] =
4856 builtin_function ("__builtin_fwrite", sizet_ftype_any,
4857 BUILT_IN_FWRITE, BUILT_IN_NORMAL, "fwrite");
4858 built_in_decls[BUILT_IN_FPUTC] =
4859 builtin_function ("__builtin_fputc", int_ftype_any,
4860 BUILT_IN_FPUTC, BUILT_IN_NORMAL, "fputc");
4861 built_in_decls[BUILT_IN_FPUTS] =
4862 builtin_function ("__builtin_fputs", int_ftype_any,
4863 BUILT_IN_FPUTS, BUILT_IN_NORMAL, "fputs");
4865 if (! no_builtins)
4867 builtin_function ("abs", int_ftype_int, BUILT_IN_ABS,
4868 BUILT_IN_NORMAL, NULL_PTR);
4869 builtin_function ("fabsf", float_ftype_float, BUILT_IN_FABS,
4870 BUILT_IN_NORMAL, NULL_PTR);
4871 builtin_function ("fabs", double_ftype_double, BUILT_IN_FABS,
4872 BUILT_IN_NORMAL, NULL_PTR);
4873 builtin_function ("fabsl", ldouble_ftype_ldouble, BUILT_IN_FABS,
4874 BUILT_IN_NORMAL, NULL_PTR);
4875 builtin_function ("labs", long_ftype_long, BUILT_IN_LABS,
4876 BUILT_IN_NORMAL, NULL_PTR);
4877 if (flag_isoc99 || ! no_nonansi_builtins)
4878 builtin_function ("llabs", longlong_ftype_longlong, BUILT_IN_LLABS,
4879 BUILT_IN_NORMAL, NULL_PTR);
4880 builtin_function ("memcpy", memcpy_ftype, BUILT_IN_MEMCPY,
4881 BUILT_IN_NORMAL, NULL_PTR);
4882 builtin_function ("memcmp", int_ftype_cptr_cptr_sizet, BUILT_IN_MEMCMP,
4883 BUILT_IN_NORMAL, NULL_PTR);
4884 builtin_function ("memset", memset_ftype, BUILT_IN_MEMSET,
4885 BUILT_IN_NORMAL, NULL_PTR);
4886 builtin_function ("strcmp", int_ftype_string_string, BUILT_IN_STRCMP,
4887 BUILT_IN_NORMAL, NULL_PTR);
4888 builtin_function ("strcpy", string_ftype_ptr_ptr, BUILT_IN_STRCPY,
4889 BUILT_IN_NORMAL, NULL_PTR);
4890 builtin_function ("strlen", strlen_ftype, BUILT_IN_STRLEN,
4891 BUILT_IN_NORMAL, NULL_PTR);
4892 builtin_function ("sqrtf", float_ftype_float, BUILT_IN_FSQRT,
4893 BUILT_IN_NORMAL, NULL_PTR);
4894 builtin_function ("sqrt", double_ftype_double, BUILT_IN_FSQRT,
4895 BUILT_IN_NORMAL, NULL_PTR);
4896 builtin_function ("sqrtl", ldouble_ftype_ldouble, BUILT_IN_FSQRT,
4897 BUILT_IN_NORMAL, NULL_PTR);
4898 builtin_function ("sinf", float_ftype_float, BUILT_IN_SIN,
4899 BUILT_IN_NORMAL, NULL_PTR);
4900 builtin_function ("sin", double_ftype_double, BUILT_IN_SIN,
4901 BUILT_IN_NORMAL, NULL_PTR);
4902 builtin_function ("sinl", ldouble_ftype_ldouble, BUILT_IN_SIN,
4903 BUILT_IN_NORMAL, NULL_PTR);
4904 builtin_function ("cosf", float_ftype_float, BUILT_IN_COS,
4905 BUILT_IN_NORMAL, NULL_PTR);
4906 builtin_function ("cos", double_ftype_double, BUILT_IN_COS,
4907 BUILT_IN_NORMAL, NULL_PTR);
4908 builtin_function ("cosl", ldouble_ftype_ldouble, BUILT_IN_COS,
4909 BUILT_IN_NORMAL, NULL_PTR);
4910 builtin_function ("printf", printf_ftype, BUILT_IN_PRINTF,
4911 BUILT_IN_FRONTEND, NULL_PTR);
4912 /* We declare these without argument so that the initial
4913 declaration for these identifiers is a builtin. That allows
4914 us to redeclare them later with argument without worrying
4915 about the explicit declarations in stdio.h being taken as the
4916 initial declaration. */
4917 builtin_function ("fputc", int_ftype_any, BUILT_IN_FPUTC,
4918 BUILT_IN_NORMAL, NULL_PTR);
4919 builtin_function ("fputs", int_ftype_any, BUILT_IN_FPUTS,
4920 BUILT_IN_NORMAL, NULL_PTR);
4922 /* Declare these functions volatile
4923 to avoid spurious "control drops through" warnings. */
4924 temp = builtin_function ("abort", cplus_mode ? void_ftype : void_ftype_any,
4925 0, NOT_BUILT_IN, NULL_PTR);
4926 TREE_THIS_VOLATILE (temp) = 1;
4927 TREE_SIDE_EFFECTS (temp) = 1;
4929 #if 0 /* ??? The C++ frontend used to do this. */
4930 /* Well, these are actually ANSI, but we can't set DECL_BUILT_IN on
4931 them... */
4932 DECL_BUILT_IN_NONANSI (temp) = 1;
4933 #endif
4934 temp = builtin_function ("exit",
4935 cplus_mode ? void_ftype_int : void_ftype_any,
4936 0, NOT_BUILT_IN, NULL_PTR);
4937 TREE_THIS_VOLATILE (temp) = 1;
4938 TREE_SIDE_EFFECTS (temp) = 1;
4940 #if 0 /* ??? The C++ frontend used to do this. */
4941 /* Well, these are actually ANSI, but we can't set DECL_BUILT_IN on
4942 them... */
4943 DECL_BUILT_IN_NONANSI (temp) = 1;
4944 #endif
4947 #if 0
4948 /* Support for these has not been written in either expand_builtin
4949 or build_function_call. */
4950 builtin_function ("__builtin_div", default_ftype, BUILT_IN_DIV,
4951 BUILT_IN_NORMAL, NULL_PTR);
4952 builtin_function ("__builtin_ldiv", default_ftype, BUILT_IN_LDIV,
4953 BUILT_IN_NORMAL, NULL_PTR);
4954 builtin_function ("__builtin_ffloor", double_ftype_double, BUILT_IN_FFLOOR,
4955 BUILT_IN_NORMAL, NULL_PTR);
4956 builtin_function ("__builtin_fceil", double_ftype_double, BUILT_IN_FCEIL,
4957 BUILT_IN_NORMAL, NULL_PTR);
4958 builtin_function ("__builtin_fmod", double_ftype_double_double,
4959 BUILT_IN_FMOD, BUILT_IN_NORMAL, NULL_PTR);
4960 builtin_function ("__builtin_frem", double_ftype_double_double,
4961 BUILT_IN_FREM, BUILT_IN_NORMAL, NULL_PTR);
4962 builtin_function ("__builtin_getexp", double_ftype_double, BUILT_IN_GETEXP,
4963 BUILT_IN_NORMAL, NULL_PTR);
4964 builtin_function ("__builtin_getman", double_ftype_double, BUILT_IN_GETMAN,
4965 BUILT_IN_NORMAL, NULL_PTR);
4966 #endif
4968 main_identifier_node = get_identifier ("main");
4970 /* ??? Perhaps there's a better place to do this. But it is related
4971 to __builtin_va_arg, so it isn't that off-the-wall. */
4972 lang_type_promotes_to = simple_type_promotes_to;
4975 tree
4976 build_va_arg (expr, type)
4977 tree expr, type;
4979 return build1 (VA_ARG_EXPR, type, expr);
4982 /* Given a type, apply default promotions wrt unnamed function arguments
4983 and return the new type. Return NULL_TREE if no change. */
4984 /* ??? There is a function of the same name in the C++ front end that
4985 does something similar, but is more thorough and does not return NULL
4986 if no change. We could perhaps share code, but it would make the
4987 self_promoting_type property harder to identify. */
4989 tree
4990 simple_type_promotes_to (type)
4991 tree type;
4993 if (TYPE_MAIN_VARIANT (type) == float_type_node)
4994 return double_type_node;
4996 if (C_PROMOTING_INTEGER_TYPE_P (type))
4998 /* Traditionally, unsignedness is preserved in default promotions.
4999 Also preserve unsignedness if not really getting any wider. */
5000 if (TREE_UNSIGNED (type)
5001 && (flag_traditional
5002 || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
5003 return unsigned_type_node;
5004 return integer_type_node;
5007 return NULL_TREE;
5010 /* Return 1 if PARMS specifies a fixed number of parameters
5011 and none of their types is affected by default promotions. */
5014 self_promoting_args_p (parms)
5015 tree parms;
5017 register tree t;
5018 for (t = parms; t; t = TREE_CHAIN (t))
5020 register tree type = TREE_VALUE (t);
5022 if (TREE_CHAIN (t) == 0 && type != void_type_node)
5023 return 0;
5025 if (type == 0)
5026 return 0;
5028 if (TYPE_MAIN_VARIANT (type) == float_type_node)
5029 return 0;
5031 if (C_PROMOTING_INTEGER_TYPE_P (type))
5032 return 0;
5034 return 1;
5037 /* Recognize certain built-in functions so we can make tree-codes
5038 other than CALL_EXPR. We do this when it enables fold-const.c
5039 to do something useful. */
5040 /* ??? By rights this should go in builtins.c, but only C and C++
5041 implement build_{binary,unary}_op. Not exactly sure what bits
5042 of functionality are actually needed from those functions, or
5043 where the similar functionality exists in the other front ends. */
5045 tree
5046 expand_tree_builtin (function, params, coerced_params)
5047 tree function, params, coerced_params;
5049 enum tree_code code;
5051 if (DECL_BUILT_IN_CLASS (function) != BUILT_IN_NORMAL)
5052 return NULL_TREE;
5054 switch (DECL_FUNCTION_CODE (function))
5056 case BUILT_IN_ABS:
5057 case BUILT_IN_LABS:
5058 case BUILT_IN_LLABS:
5059 case BUILT_IN_FABS:
5060 if (coerced_params == 0)
5061 return integer_zero_node;
5062 return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
5064 case BUILT_IN_ISGREATER:
5065 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
5066 code = UNLE_EXPR;
5067 else
5068 code = LE_EXPR;
5069 goto unordered_cmp;
5071 case BUILT_IN_ISGREATEREQUAL:
5072 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
5073 code = UNLT_EXPR;
5074 else
5075 code = LT_EXPR;
5076 goto unordered_cmp;
5078 case BUILT_IN_ISLESS:
5079 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
5080 code = UNGE_EXPR;
5081 else
5082 code = GE_EXPR;
5083 goto unordered_cmp;
5085 case BUILT_IN_ISLESSEQUAL:
5086 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
5087 code = UNGT_EXPR;
5088 else
5089 code = GT_EXPR;
5090 goto unordered_cmp;
5092 case BUILT_IN_ISLESSGREATER:
5093 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
5094 code = UNEQ_EXPR;
5095 else
5096 code = EQ_EXPR;
5097 goto unordered_cmp;
5099 case BUILT_IN_ISUNORDERED:
5100 if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT)
5101 return integer_zero_node;
5102 code = UNORDERED_EXPR;
5103 goto unordered_cmp;
5105 unordered_cmp:
5107 tree arg0, arg1;
5109 if (params == 0
5110 || TREE_CHAIN (params) == 0)
5112 error ("too few arguments to function `%s'",
5113 IDENTIFIER_POINTER (DECL_NAME (function)));
5114 return error_mark_node;
5116 else if (TREE_CHAIN (TREE_CHAIN (params)) != 0)
5118 error ("too many arguments to function `%s'",
5119 IDENTIFIER_POINTER (DECL_NAME (function)));
5120 return error_mark_node;
5123 arg0 = TREE_VALUE (params);
5124 arg1 = TREE_VALUE (TREE_CHAIN (params));
5125 arg0 = build_binary_op (code, arg0, arg1, 0);
5126 if (code != UNORDERED_EXPR)
5127 arg0 = build_unary_op (TRUTH_NOT_EXPR, arg0, 0);
5128 return arg0;
5130 break;
5132 default:
5133 break;
5136 return NULL_TREE;
5139 /* Returns non-zero if CODE is the code for a statement. */
5142 statement_code_p (code)
5143 enum tree_code code;
5145 switch (code)
5147 case EXPR_STMT:
5148 case COMPOUND_STMT:
5149 case DECL_STMT:
5150 case IF_STMT:
5151 case FOR_STMT:
5152 case WHILE_STMT:
5153 case DO_STMT:
5154 case RETURN_STMT:
5155 case BREAK_STMT:
5156 case CONTINUE_STMT:
5157 case SCOPE_STMT:
5158 case SWITCH_STMT:
5159 case GOTO_STMT:
5160 case LABEL_STMT:
5161 case ASM_STMT:
5162 case CASE_LABEL:
5163 return 1;
5165 default:
5166 if (lang_statement_code_p)
5167 return (*lang_statement_code_p) (code);
5168 return 0;
5172 /* Walk the statemen tree, rooted at *tp. Apply FUNC to all the
5173 sub-trees of *TP in a pre-order traversal. FUNC is called with the
5174 DATA and the address of each sub-tree. If FUNC returns a non-NULL
5175 value, the traversal is aborted, and the value returned by FUNC is
5176 returned. If FUNC sets WALK_SUBTREES to zero, then the subtrees of
5177 the node being visited are not walked.
5179 We don't need a without_duplicates variant of this one because the
5180 statement tree is a tree, not a graph. */
5182 tree
5183 walk_stmt_tree (tp, func, data)
5184 tree *tp;
5185 walk_tree_fn func;
5186 void *data;
5188 enum tree_code code;
5189 int walk_subtrees;
5190 tree result;
5191 int i, len;
5193 #define WALK_SUBTREE(NODE) \
5194 do \
5196 result = walk_stmt_tree (&(NODE), func, data); \
5197 if (result) \
5198 return result; \
5200 while (0)
5202 /* Skip empty subtrees. */
5203 if (!*tp)
5204 return NULL_TREE;
5206 /* Skip subtrees below non-statement nodes. */
5207 if (!statement_code_p (TREE_CODE (*tp)))
5208 return NULL_TREE;
5210 /* Call the function. */
5211 walk_subtrees = 1;
5212 result = (*func) (tp, &walk_subtrees, data);
5214 /* If we found something, return it. */
5215 if (result)
5216 return result;
5218 /* Even if we didn't, FUNC may have decided that there was nothing
5219 interesting below this point in the tree. */
5220 if (!walk_subtrees)
5221 return NULL_TREE;
5223 /* FUNC may have modified the tree, recheck that we're looking at a
5224 statement node. */
5225 code = TREE_CODE (*tp);
5226 if (!statement_code_p (code))
5227 return NULL_TREE;
5229 /* Walk over all the sub-trees of this operand. Statement nodes never
5230 contain RTL, and we needn't worry about TARGET_EXPRs. */
5231 len = TREE_CODE_LENGTH (code);
5233 /* Go through the subtrees. We need to do this in forward order so
5234 that the scope of a FOR_EXPR is handled properly. */
5235 for (i = 0; i < len; ++i)
5236 WALK_SUBTREE (TREE_OPERAND (*tp, i));
5238 /* Finally visit the chain. This can be tail-recursion optimized if
5239 we write it this way. */
5240 return walk_stmt_tree (&TREE_CHAIN (*tp), func, data);
5242 #undef WALK_SUBTREE
5245 /* Used to compare case labels. K1 and K2 are actually tree nodes
5246 representing case labels, or NULL_TREE for a `default' label.
5247 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
5248 K2, and 0 if K1 and K2 are equal. */
5251 case_compare (k1, k2)
5252 splay_tree_key k1;
5253 splay_tree_key k2;
5255 /* Consider a NULL key (such as arises with a `default' label) to be
5256 smaller than anything else. */
5257 if (!k1)
5258 return k2 ? -1 : 0;
5259 else if (!k2)
5260 return k1 ? 1 : 0;
5262 return tree_int_cst_compare ((tree) k1, (tree) k2);
5265 /* Process a case label for the range LOW_VALUE ... HIGH_VALUE. If
5266 LOW_VALUE and HIGH_VALUE are both NULL_TREE then this case label is
5267 actually a `default' label. If only HIGH_VALUE is NULL_TREE, then
5268 case label was declared using the usual C/C++ syntax, rather than
5269 the GNU case range extension. CASES is a tree containing all the
5270 case ranges processed so far; COND is the condition for the
5271 switch-statement itself. Returns the CASE_LABEL created, or
5272 ERROR_MARK_NODE if no CASE_LABEL is created. */
5274 tree
5275 c_add_case_label (cases, cond, low_value, high_value)
5276 splay_tree cases;
5277 tree cond;
5278 tree low_value;
5279 tree high_value;
5281 tree type;
5282 tree label;
5283 tree case_label;
5284 splay_tree_node node;
5286 /* Create the LABEL_DECL itself. */
5287 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
5288 DECL_CONTEXT (label) = current_function_decl;
5290 /* If there was an error processing the switch condition, bail now
5291 before we get more confused. */
5292 if (!cond || cond == error_mark_node)
5294 /* Add a label anyhow so that the back-end doesn't think that
5295 the beginning of the switch is unreachable. */
5296 if (!cases->root)
5297 add_stmt (build_case_label (NULL_TREE, NULL_TREE, label));
5298 return error_mark_node;
5301 if ((low_value && TREE_TYPE (low_value)
5302 && POINTER_TYPE_P (TREE_TYPE (low_value)))
5303 || (high_value && TREE_TYPE (high_value)
5304 && POINTER_TYPE_P (TREE_TYPE (high_value))))
5305 error ("pointers are not permitted as case values");
5307 /* Case ranges are a GNU extension. */
5308 if (high_value && pedantic)
5310 if (c_language == clk_cplusplus)
5311 pedwarn ("ISO C++ forbids range expressions in switch statements");
5312 else
5313 pedwarn ("ISO C forbids range expressions in switch statements");
5316 type = TREE_TYPE (cond);
5317 if (low_value)
5319 low_value = check_case_value (low_value);
5320 low_value = convert_and_check (type, low_value);
5322 if (high_value)
5324 high_value = check_case_value (high_value);
5325 high_value = convert_and_check (type, high_value);
5328 /* If an error has occurred, bail out now. */
5329 if (low_value == error_mark_node || high_value == error_mark_node)
5331 if (!cases->root)
5332 add_stmt (build_case_label (NULL_TREE, NULL_TREE, label));
5333 return error_mark_node;
5336 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
5337 really a case range, even though it was written that way. Remove
5338 the HIGH_VALUE to simplify later processing. */
5339 if (tree_int_cst_equal (low_value, high_value))
5340 high_value = NULL_TREE;
5341 if (low_value && high_value
5342 && !tree_int_cst_lt (low_value, high_value))
5343 warning ("empty range specified");
5345 /* Look up the LOW_VALUE in the table of case labels we already
5346 have. */
5347 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
5348 /* If there was not an exact match, check for overlapping ranges.
5349 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
5350 that's a `default' label and the only overlap is an exact match. */
5351 if (!node && (low_value || high_value))
5353 splay_tree_node low_bound;
5354 splay_tree_node high_bound;
5356 /* Even though there wasn't an exact match, there might be an
5357 overlap between this case range and another case range.
5358 Since we've (inductively) not allowed any overlapping case
5359 ranges, we simply need to find the greatest low case label
5360 that is smaller that LOW_VALUE, and the smallest low case
5361 label that is greater than LOW_VALUE. If there is an overlap
5362 it will occur in one of these two ranges. */
5363 low_bound = splay_tree_predecessor (cases,
5364 (splay_tree_key) low_value);
5365 high_bound = splay_tree_successor (cases,
5366 (splay_tree_key) low_value);
5368 /* Check to see if the LOW_BOUND overlaps. It is smaller than
5369 the LOW_VALUE, so there is no need to check unless the
5370 LOW_BOUND is in fact itself a case range. */
5371 if (low_bound
5372 && CASE_HIGH ((tree) low_bound->value)
5373 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
5374 low_value) >= 0)
5375 node = low_bound;
5376 /* Check to see if the HIGH_BOUND overlaps. The low end of that
5377 range is bigger than the low end of the current range, so we
5378 are only interested if the current range is a real range, and
5379 not an ordinary case label. */
5380 else if (high_bound
5381 && high_value
5382 && (tree_int_cst_compare ((tree) high_bound->key,
5383 high_value)
5384 <= 0))
5385 node = high_bound;
5387 /* If there was an overlap, issue an error. */
5388 if (node)
5390 tree duplicate = CASE_LABEL_DECL ((tree) node->value);
5392 if (high_value)
5394 error ("duplicate (or overlapping) case value");
5395 error_with_decl (duplicate,
5396 "this is the first entry overlapping that value");
5398 else if (low_value)
5400 error ("duplicate case value") ;
5401 error_with_decl (duplicate, "previously used here");
5403 else
5405 error ("multiple default labels in one switch");
5406 error_with_decl (duplicate, "this is the first default label");
5408 if (!cases->root)
5409 add_stmt (build_case_label (NULL_TREE, NULL_TREE, label));
5412 /* Add a CASE_LABEL to the statement-tree. */
5413 case_label = add_stmt (build_case_label (low_value, high_value, label));
5414 /* Register this case label in the splay tree. */
5415 splay_tree_insert (cases,
5416 (splay_tree_key) low_value,
5417 (splay_tree_value) case_label);
5419 return case_label;
5422 /* Mark P (a stmt_tree) for GC. The use of a `void *' for the
5423 parameter allows this function to be used as a GC-marking
5424 function. */
5426 void
5427 mark_stmt_tree (p)
5428 void *p;
5430 stmt_tree st = (stmt_tree) p;
5432 ggc_mark_tree (st->x_last_stmt);
5433 ggc_mark_tree (st->x_last_expr_type);
5436 /* Mark LD for GC. */
5438 void
5439 c_mark_lang_decl (c)
5440 struct c_lang_decl *c;
5442 ggc_mark_tree (c->saved_tree);
5445 /* Mark F for GC. */
5447 void
5448 mark_c_language_function (f)
5449 struct language_function *f;
5451 if (!f)
5452 return;
5454 mark_stmt_tree (&f->x_stmt_tree);
5455 ggc_mark_tree (f->x_scope_stmt_stack);
5458 /* Hook used by expand_expr to expand language-specific tree codes. */
5461 c_expand_expr (exp, target, tmode, modifier)
5462 tree exp;
5463 rtx target;
5464 enum machine_mode tmode;
5465 enum expand_modifier modifier;
5467 switch (TREE_CODE (exp))
5469 case STMT_EXPR:
5471 tree rtl_expr;
5472 rtx result;
5474 /* Since expand_expr_stmt calls free_temp_slots after every
5475 expression statement, we must call push_temp_slots here.
5476 Otherwise, any temporaries in use now would be considered
5477 out-of-scope after the first EXPR_STMT from within the
5478 STMT_EXPR. */
5479 push_temp_slots ();
5480 rtl_expr = expand_start_stmt_expr ();
5481 expand_stmt (STMT_EXPR_STMT (exp));
5482 expand_end_stmt_expr (rtl_expr);
5483 result = expand_expr (rtl_expr, target, tmode, modifier);
5484 pop_temp_slots ();
5485 return result;
5487 break;
5489 case CALL_EXPR:
5491 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
5492 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
5493 == FUNCTION_DECL)
5494 && DECL_BUILT_IN (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
5495 && (DECL_BUILT_IN_CLASS (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
5496 == BUILT_IN_FRONTEND))
5497 return c_expand_builtin (exp, target, tmode, modifier);
5498 else
5499 abort();
5501 break;
5503 default:
5504 abort ();
5507 abort ();
5508 return NULL;
5511 /* Hook used by safe_from_p to handle language-specific tree codes. */
5514 c_safe_from_p (target, exp)
5515 rtx target;
5516 tree exp;
5518 /* We can see statements here when processing the body of a
5519 statement-expression. For a declaration statement declaring a
5520 variable, look at the variable's initializer. */
5521 if (TREE_CODE (exp) == DECL_STMT)
5523 tree decl = DECL_STMT_DECL (exp);
5525 if (TREE_CODE (decl) == VAR_DECL
5526 && DECL_INITIAL (decl)
5527 && !safe_from_p (target, DECL_INITIAL (decl), /*top_p=*/0))
5528 return 0;
5531 /* For any statement, we must follow the statement-chain. */
5532 if (statement_code_p (TREE_CODE (exp)) && TREE_CHAIN (exp))
5533 return safe_from_p (target, TREE_CHAIN (exp), /*top_p=*/0);
5535 /* Assume everything else is safe. */
5536 return 1;
5539 /* Tree code classes. */
5541 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
5543 static char c_tree_code_type[] = {
5544 'x',
5545 #include "c-common.def"
5547 #undef DEFTREECODE
5549 /* Table indexed by tree code giving number of expression
5550 operands beyond the fixed part of the node structure.
5551 Not used for types or decls. */
5553 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
5555 static int c_tree_code_length[] = {
5557 #include "c-common.def"
5559 #undef DEFTREECODE
5561 /* Names of tree components.
5562 Used for printing out the tree and error messages. */
5563 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
5565 static const char *c_tree_code_name[] = {
5566 "@@dummy",
5567 #include "c-common.def"
5569 #undef DEFTREECODE
5571 /* Adds the tree codes specific to the C front end to the list of all
5572 tree codes. */
5574 void
5575 add_c_tree_codes ()
5577 memcpy (tree_code_type + (int) LAST_AND_UNUSED_TREE_CODE,
5578 c_tree_code_type,
5579 (int)LAST_C_TREE_CODE - (int)LAST_AND_UNUSED_TREE_CODE);
5580 memcpy (tree_code_length + (int) LAST_AND_UNUSED_TREE_CODE,
5581 c_tree_code_length,
5582 (LAST_C_TREE_CODE - (int)LAST_AND_UNUSED_TREE_CODE) * sizeof (int));
5583 memcpy (tree_code_name + (int) LAST_AND_UNUSED_TREE_CODE,
5584 c_tree_code_name,
5585 (LAST_C_TREE_CODE - (int)LAST_AND_UNUSED_TREE_CODE) * sizeof (char *));
5588 #define CALLED_AS_BUILT_IN(NODE) \
5589 (!strncmp (IDENTIFIER_POINTER (DECL_NAME (NODE)), "__builtin_", 10))
5591 static rtx
5592 c_expand_builtin (exp, target, tmode, modifier)
5593 tree exp;
5594 rtx target;
5595 enum machine_mode tmode;
5596 enum expand_modifier modifier;
5598 tree type = TREE_TYPE (exp);
5599 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
5600 tree arglist = TREE_OPERAND (exp, 1);
5601 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5602 enum tree_code code = TREE_CODE (exp);
5603 const int ignore = (target == const0_rtx
5604 || ((code == NON_LVALUE_EXPR || code == NOP_EXPR
5605 || code == CONVERT_EXPR || code == REFERENCE_EXPR
5606 || code == COND_EXPR)
5607 && TREE_CODE (type) == VOID_TYPE));
5609 if (! optimize && ! CALLED_AS_BUILT_IN (fndecl))
5610 return expand_call (exp, target, ignore);
5612 switch (fcode)
5614 case BUILT_IN_PRINTF:
5615 target = c_expand_builtin_printf (arglist, target, tmode,
5616 modifier, ignore);
5617 if (target)
5618 return target;
5619 break;
5621 default: /* just do library call, if unknown builtin */
5622 error ("built-in function `%s' not currently supported",
5623 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
5626 /* The switch statement above can drop through to cause the function
5627 to be called normally. */
5628 return expand_call (exp, target, ignore);
5631 /* Check an arglist to *printf for problems. The arglist should start
5632 at the format specifier, with the remaining arguments immediately
5633 following it. */
5634 static int
5635 is_valid_printf_arglist (arglist)
5636 tree arglist;
5638 /* Save this value so we can restore it later. */
5639 const int SAVE_pedantic = pedantic;
5640 int diagnostic_occurred = 0;
5642 /* Set this to a known value so the user setting won't affect code
5643 generation. */
5644 pedantic = 1;
5645 /* Check to make sure there are no format specifier errors. */
5646 check_function_format (&diagnostic_occurred,
5647 maybe_get_identifier("printf"),
5648 NULL_TREE, arglist);
5650 /* Restore the value of `pedantic'. */
5651 pedantic = SAVE_pedantic;
5653 /* If calling `check_function_format_ptr' produces a warning, we
5654 return false, otherwise we return true. */
5655 return ! diagnostic_occurred;
5658 /* If the arguments passed to printf are suitable for optimizations,
5659 we attempt to transform the call. */
5660 static rtx
5661 c_expand_builtin_printf (arglist, target, tmode, modifier, ignore)
5662 tree arglist;
5663 rtx target;
5664 enum machine_mode tmode;
5665 enum expand_modifier modifier;
5666 int ignore;
5668 tree fn_putchar = built_in_decls[BUILT_IN_PUTCHAR],
5669 fn_puts = built_in_decls[BUILT_IN_PUTS];
5670 tree fn, format_arg, stripped_string;
5672 /* If the return value is used, or the replacement _DECL isn't
5673 initialized, don't do the transformation. */
5674 if (!ignore || !fn_putchar || !fn_puts)
5675 return 0;
5677 /* Verify the required arguments in the original call. */
5678 if (arglist == 0
5679 || (TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE))
5680 return 0;
5682 /* Check the specifier vs. the parameters. */
5683 if (!is_valid_printf_arglist (arglist))
5684 return 0;
5686 format_arg = TREE_VALUE (arglist);
5687 stripped_string = format_arg;
5688 STRIP_NOPS (stripped_string);
5689 if (stripped_string && TREE_CODE (stripped_string) == ADDR_EXPR)
5690 stripped_string = TREE_OPERAND (stripped_string, 0);
5692 /* If the format specifier isn't a STRING_CST, punt. */
5693 if (TREE_CODE (stripped_string) != STRING_CST)
5694 return 0;
5696 /* OK! We can attempt optimization. */
5698 /* If the format specifier was "%s\n", call __builtin_puts(arg2). */
5699 if (strcmp (TREE_STRING_POINTER (stripped_string), "%s\n") == 0)
5701 arglist = TREE_CHAIN (arglist);
5702 fn = fn_puts;
5704 /* If the format specifier was "%c", call __builtin_putchar (arg2). */
5705 else if (strcmp (TREE_STRING_POINTER (stripped_string), "%c") == 0)
5707 arglist = TREE_CHAIN (arglist);
5708 fn = fn_putchar;
5710 else
5712 /* We can't handle anything else with % args or %% ... yet. */
5713 if (strchr (TREE_STRING_POINTER (stripped_string), '%'))
5714 return 0;
5716 /* If the resulting constant string has a length of 1, call
5717 putchar. Note, TREE_STRING_LENGTH includes the terminating
5718 NULL in its count. */
5719 if (TREE_STRING_LENGTH (stripped_string) == 2)
5721 /* Given printf("c"), (where c is any one character,)
5722 convert "c"[0] to an int and pass that to the replacement
5723 function. */
5724 arglist = build_int_2 (TREE_STRING_POINTER (stripped_string)[0], 0);
5725 arglist = build_tree_list (NULL_TREE, arglist);
5727 fn = fn_putchar;
5729 /* If the resulting constant was "string\n", call
5730 __builtin_puts("string"). Ensure "string" has at least one
5731 character besides the trailing \n. Note, TREE_STRING_LENGTH
5732 includes the terminating NULL in its count. */
5733 else if (TREE_STRING_LENGTH (stripped_string) > 2
5734 && TREE_STRING_POINTER (stripped_string)
5735 [TREE_STRING_LENGTH (stripped_string) - 2] == '\n')
5737 /* Create a NULL-terminated string that's one char shorter
5738 than the original, stripping off the trailing '\n'. */
5739 const int newlen = TREE_STRING_LENGTH (stripped_string) - 1;
5740 char *newstr = (char *) alloca (newlen);
5741 memcpy (newstr, TREE_STRING_POINTER (stripped_string), newlen - 1);
5742 newstr[newlen - 1] = 0;
5744 arglist = combine_strings (build_string (newlen, newstr));
5745 arglist = build_tree_list (NULL_TREE, arglist);
5746 fn = fn_puts;
5748 else
5749 /* We'd like to arrange to call fputs(string) here, but we
5750 need stdout and don't have a way to get it ... yet. */
5751 return 0;
5754 return expand_expr (build_function_call (fn, arglist),
5755 (ignore ? const0_rtx : target),
5756 tmode, modifier);