1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000
3 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
39 cpp_options parse_options;
40 enum cpp_token cpp_token;
43 #undef WCHAR_TYPE_SIZE
44 #define WCHAR_TYPE_SIZE TYPE_PRECISION (wchar_type_node)
46 /* The following symbols are subsumed in the c_global_trees array, and
47 listed here individually for documentation purposes.
49 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
51 tree short_integer_type_node;
52 tree long_integer_type_node;
53 tree long_long_integer_type_node;
55 tree short_unsigned_type_node;
56 tree long_unsigned_type_node;
57 tree long_long_unsigned_type_node;
59 tree boolean_type_node;
60 tree boolean_false_node;
61 tree boolean_true_node;
63 tree ptrdiff_type_node;
65 tree unsigned_char_type_node;
66 tree signed_char_type_node;
68 tree signed_wchar_type_node;
69 tree unsigned_wchar_type_node;
72 tree double_type_node;
73 tree long_double_type_node;
75 tree complex_integer_type_node;
76 tree complex_float_type_node;
77 tree complex_double_type_node;
78 tree complex_long_double_type_node;
86 tree unsigned_intQI_type_node;
87 tree unsigned_intHI_type_node;
88 tree unsigned_intSI_type_node;
89 tree unsigned_intDI_type_node;
90 tree unsigned_intTI_type_node;
92 tree widest_integer_literal_type_node;
93 tree widest_unsigned_literal_type_node;
95 Nodes for types `void *' and `const void *'.
97 tree ptr_type_node, const_ptr_type_node;
99 Nodes for types `char *' and `const char *'.
101 tree string_type_node, const_string_type_node;
103 Type `char[SOMENUMBER]'.
104 Used when an array of char is needed and the size is irrelevant.
106 tree char_array_type_node;
108 Type `int[SOMENUMBER]' or something like it.
109 Used when an array of int needed and the size is irrelevant.
111 tree int_array_type_node;
113 Type `wchar_t[SOMENUMBER]' or something like it.
114 Used when a wide string literal is created.
116 tree wchar_array_type_node;
118 Type `int ()' -- used for implicit declaration of functions.
120 tree default_function_type;
122 Function types `int (int)', etc.
128 tree ptr_ftype_sizetype;
130 A VOID_TYPE node, packaged in a TREE_LIST.
136 tree c_global_trees[CTI_MAX];
138 tree (*make_fname_decl) PARAMS ((tree, const char *, int));
140 /* Nonzero means the expression being parsed will never be evaluated.
141 This is a count, since unevaluated expressions can nest. */
144 enum attrs {A_PACKED, A_NOCOMMON, A_COMMON, A_NORETURN, A_CONST, A_T_UNION,
145 A_NO_CHECK_MEMORY_USAGE, A_NO_INSTRUMENT_FUNCTION,
146 A_CONSTRUCTOR, A_DESTRUCTOR, A_MODE, A_SECTION, A_ALIGNED,
147 A_UNUSED, A_FORMAT, A_FORMAT_ARG, A_WEAK, A_ALIAS, A_MALLOC,
148 A_NO_LIMIT_STACK, A_PURE};
150 enum format_type { printf_format_type, scanf_format_type,
151 strftime_format_type };
153 static void add_attribute PARAMS ((enum attrs, const char *,
155 static void init_attributes PARAMS ((void));
156 static void record_function_format PARAMS ((tree, tree, enum format_type,
158 static void record_international_format PARAMS ((tree, tree, int));
159 static tree c_find_base_decl PARAMS ((tree));
160 static int default_valid_lang_attribute PARAMS ((tree, tree, tree, tree));
162 /* Keep a stack of if statements. We record the number of compound
163 statements seen up to the if keyword, as well as the line number
164 and file of the if. If a potentially ambiguous else is seen, that
165 fact is recorded; the warning is issued when we can be sure that
166 the enclosing if statement does not have an else branch. */
174 static void tfaff PARAMS ((void));
176 static if_elt *if_stack;
178 /* Amount of space in the if statement stack. */
179 static int if_stack_space = 0;
182 static int if_stack_pointer = 0;
184 /* Generate RTL for the start of an if-then, and record the start of it
185 for ambiguous else detection. */
188 c_expand_start_cond (cond, exitflag, compstmt_count)
193 /* Make sure there is enough space on the stack. */
194 if (if_stack_space == 0)
197 if_stack = (if_elt *)xmalloc (10 * sizeof (if_elt));
199 else if (if_stack_space == if_stack_pointer)
201 if_stack_space += 10;
202 if_stack = (if_elt *)xrealloc (if_stack, if_stack_space * sizeof (if_elt));
205 /* Record this if statement. */
206 if_stack[if_stack_pointer].compstmt_count = compstmt_count;
207 if_stack[if_stack_pointer].file = input_filename;
208 if_stack[if_stack_pointer].line = lineno;
209 if_stack[if_stack_pointer].needs_warning = 0;
212 expand_start_cond (cond, exitflag);
215 /* Generate RTL for the end of an if-then. Optionally warn if a nested
216 if statement had an ambiguous else clause. */
222 if (if_stack[if_stack_pointer].needs_warning)
223 warning_with_file_and_line (if_stack[if_stack_pointer].file,
224 if_stack[if_stack_pointer].line,
225 "suggest explicit braces to avoid ambiguous `else'");
229 /* Generate RTL between the then-clause and the else-clause
230 of an if-then-else. */
233 c_expand_start_else ()
235 /* An ambiguous else warning must be generated for the enclosing if
236 statement, unless we see an else branch for that one, too. */
238 && if_stack_pointer > 1
239 && (if_stack[if_stack_pointer - 1].compstmt_count
240 == if_stack[if_stack_pointer - 2].compstmt_count))
241 if_stack[if_stack_pointer - 2].needs_warning = 1;
243 /* Even if a nested if statement had an else branch, it can't be
244 ambiguous if this one also has an else. So don't warn in that
245 case. Also don't warn for any if statements nested in this else. */
246 if_stack[if_stack_pointer - 1].needs_warning = 0;
247 if_stack[if_stack_pointer - 1].compstmt_count--;
249 expand_start_else ();
252 /* Make bindings for __FUNCTION__, __PRETTY_FUNCTION__, and __func__. */
255 declare_function_name ()
257 const char *name, *printable_name;
259 if (current_function_decl == NULL)
262 printable_name = "top level";
266 /* Allow functions to be nameless (such as artificial ones). */
267 if (DECL_NAME (current_function_decl))
268 name = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
271 printable_name = (*decl_printable_name) (current_function_decl, 2);
274 (*make_fname_decl) (get_identifier ("__FUNCTION__"), name, 0);
275 (*make_fname_decl) (get_identifier ("__PRETTY_FUNCTION__"), printable_name, 1);
276 /* The ISO C people "of course" couldn't use __FUNCTION__ in the
277 ISO C 99 standard; instead a new variable is invented. */
278 (*make_fname_decl) (get_identifier ("__func__"), name, 0);
281 /* Given a chain of STRING_CST nodes,
282 concatenate them into one STRING_CST
283 and give it a suitable array-of-chars data type. */
286 combine_strings (strings)
289 register tree value, t;
290 register int length = 1;
293 int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
296 if (TREE_CHAIN (strings))
298 /* More than one in the chain, so concatenate. */
299 register char *p, *q;
301 /* Don't include the \0 at the end of each substring,
302 except for the last one.
303 Count wide strings and ordinary strings separately. */
304 for (t = strings; t; t = TREE_CHAIN (t))
306 if (TREE_TYPE (t) == wchar_array_type_node)
308 wide_length += (TREE_STRING_LENGTH (t) - wchar_bytes);
312 length += (TREE_STRING_LENGTH (t) - 1);
315 /* If anything is wide, the non-wides will be converted,
316 which makes them take more space. */
318 length = length * wchar_bytes + wide_length;
320 p = ggc_alloc_string (NULL, length);
322 /* Copy the individual strings into the new combined string.
323 If the combined string is wide, convert the chars to ints
324 for any individual strings that are not wide. */
327 for (t = strings; t; t = TREE_CHAIN (t))
329 int len = (TREE_STRING_LENGTH (t)
330 - ((TREE_TYPE (t) == wchar_array_type_node)
332 if ((TREE_TYPE (t) == wchar_array_type_node) == wide_flag)
334 memcpy (q, TREE_STRING_POINTER (t), len);
340 for (i = 0; i < len; i++)
342 if (WCHAR_TYPE_SIZE == HOST_BITS_PER_SHORT)
343 ((short *) q)[i] = TREE_STRING_POINTER (t)[i];
345 ((int *) q)[i] = TREE_STRING_POINTER (t)[i];
347 q += len * wchar_bytes;
353 for (i = 0; i < wchar_bytes; i++)
359 value = make_node (STRING_CST);
360 TREE_STRING_POINTER (value) = p;
361 TREE_STRING_LENGTH (value) = length;
366 length = TREE_STRING_LENGTH (value);
367 if (TREE_TYPE (value) == wchar_array_type_node)
371 /* Compute the number of elements, for the array type. */
372 nchars = wide_flag ? length / wchar_bytes : length;
374 /* Create the array type for the string constant.
375 -Wwrite-strings says make the string constant an array of const char
376 so that copying it to a non-const pointer will get a warning.
377 For C++, this is the standard behavior. */
378 if (flag_const_strings
379 && (! flag_traditional && ! flag_writable_strings))
382 = build_type_variant (wide_flag ? wchar_type_node : char_type_node,
385 = build_array_type (elements,
386 build_index_type (build_int_2 (nchars - 1, 0)));
390 = build_array_type (wide_flag ? wchar_type_node : char_type_node,
391 build_index_type (build_int_2 (nchars - 1, 0)));
393 TREE_CONSTANT (value) = 1;
394 TREE_READONLY (value) = ! flag_writable_strings;
395 TREE_STATIC (value) = 1;
399 /* To speed up processing of attributes, we maintain an array of
400 IDENTIFIER_NODES and the corresponding attribute types. */
402 /* Array to hold attribute information. */
404 static struct {enum attrs id; tree name; int min, max, decl_req;} attrtab[50];
406 static int attrtab_idx = 0;
408 /* Add an entry to the attribute table above. */
411 add_attribute (id, string, min_len, max_len, decl_req)
414 int min_len, max_len;
419 attrtab[attrtab_idx].id = id;
420 attrtab[attrtab_idx].name = get_identifier (string);
421 attrtab[attrtab_idx].min = min_len;
422 attrtab[attrtab_idx].max = max_len;
423 attrtab[attrtab_idx++].decl_req = decl_req;
425 sprintf (buf, "__%s__", string);
427 attrtab[attrtab_idx].id = id;
428 attrtab[attrtab_idx].name = get_identifier (buf);
429 attrtab[attrtab_idx].min = min_len;
430 attrtab[attrtab_idx].max = max_len;
431 attrtab[attrtab_idx++].decl_req = decl_req;
434 /* Initialize attribute table. */
439 add_attribute (A_PACKED, "packed", 0, 0, 0);
440 add_attribute (A_NOCOMMON, "nocommon", 0, 0, 1);
441 add_attribute (A_COMMON, "common", 0, 0, 1);
442 add_attribute (A_NORETURN, "noreturn", 0, 0, 1);
443 add_attribute (A_NORETURN, "volatile", 0, 0, 1);
444 add_attribute (A_UNUSED, "unused", 0, 0, 0);
445 add_attribute (A_CONST, "const", 0, 0, 1);
446 add_attribute (A_T_UNION, "transparent_union", 0, 0, 0);
447 add_attribute (A_CONSTRUCTOR, "constructor", 0, 0, 1);
448 add_attribute (A_DESTRUCTOR, "destructor", 0, 0, 1);
449 add_attribute (A_MODE, "mode", 1, 1, 1);
450 add_attribute (A_SECTION, "section", 1, 1, 1);
451 add_attribute (A_ALIGNED, "aligned", 0, 1, 0);
452 add_attribute (A_FORMAT, "format", 3, 3, 1);
453 add_attribute (A_FORMAT_ARG, "format_arg", 1, 1, 1);
454 add_attribute (A_WEAK, "weak", 0, 0, 1);
455 add_attribute (A_ALIAS, "alias", 1, 1, 1);
456 add_attribute (A_NO_INSTRUMENT_FUNCTION, "no_instrument_function", 0, 0, 1);
457 add_attribute (A_NO_CHECK_MEMORY_USAGE, "no_check_memory_usage", 0, 0, 1);
458 add_attribute (A_MALLOC, "malloc", 0, 0, 1);
459 add_attribute (A_NO_LIMIT_STACK, "no_stack_limit", 0, 0, 1);
460 add_attribute (A_PURE, "pure", 0, 0, 1);
463 /* Default implementation of valid_lang_attribute, below. By default, there
464 are no language-specific attributes. */
467 default_valid_lang_attribute (attr_name, attr_args, decl, type)
468 tree attr_name ATTRIBUTE_UNUSED;
469 tree attr_args ATTRIBUTE_UNUSED;
470 tree decl ATTRIBUTE_UNUSED;
471 tree type ATTRIBUTE_UNUSED;
476 /* Return a 1 if ATTR_NAME and ATTR_ARGS denote a valid language-specific
477 attribute for either declaration DECL or type TYPE and 0 otherwise. */
479 int (*valid_lang_attribute) PARAMS ((tree, tree, tree, tree))
480 = default_valid_lang_attribute;
482 /* Process the attributes listed in ATTRIBUTES and PREFIX_ATTRIBUTES
483 and install them in NODE, which is either a DECL (including a TYPE_DECL)
484 or a TYPE. PREFIX_ATTRIBUTES can appear after the declaration specifiers
485 and declaration modifiers but before the declaration proper. */
488 decl_attributes (node, attributes, prefix_attributes)
489 tree node, attributes, prefix_attributes;
491 tree decl = 0, type = 0;
495 if (attrtab_idx == 0)
501 type = TREE_TYPE (decl);
502 is_type = TREE_CODE (node) == TYPE_DECL;
504 else if (TYPE_P (node))
505 type = node, is_type = 1;
507 #ifdef PRAGMA_INSERT_ATTRIBUTES
508 /* If the code in c-pragma.c wants to insert some attributes then
509 allow it to do so. Do this before allowing machine back ends to
510 insert attributes, so that they have the opportunity to override
511 anything done here. */
512 PRAGMA_INSERT_ATTRIBUTES (node, & attributes, & prefix_attributes);
515 #ifdef INSERT_ATTRIBUTES
516 INSERT_ATTRIBUTES (node, & attributes, & prefix_attributes);
519 attributes = chainon (prefix_attributes, attributes);
521 for (a = attributes; a; a = TREE_CHAIN (a))
523 tree name = TREE_PURPOSE (a);
524 tree args = TREE_VALUE (a);
528 for (i = 0; i < attrtab_idx; i++)
529 if (attrtab[i].name == name)
532 if (i == attrtab_idx)
534 if (! valid_machine_attribute (name, args, decl, type)
535 && ! (* valid_lang_attribute) (name, args, decl, type))
536 warning ("`%s' attribute directive ignored",
537 IDENTIFIER_POINTER (name));
539 type = TREE_TYPE (decl);
542 else if (attrtab[i].decl_req && decl == 0)
544 warning ("`%s' attribute does not apply to types",
545 IDENTIFIER_POINTER (name));
548 else if (list_length (args) < attrtab[i].min
549 || list_length (args) > attrtab[i].max)
551 error ("wrong number of arguments specified for `%s' attribute",
552 IDENTIFIER_POINTER (name));
561 TYPE_PACKED (type) = 1;
562 else if (TREE_CODE (decl) == FIELD_DECL)
563 DECL_PACKED (decl) = 1;
564 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
565 used for DECL_REGISTER. It wouldn't mean anything anyway. */
567 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
571 if (TREE_CODE (decl) == VAR_DECL)
572 DECL_COMMON (decl) = 0;
574 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
578 if (TREE_CODE (decl) == VAR_DECL)
579 DECL_COMMON (decl) = 1;
581 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
585 if (TREE_CODE (decl) == FUNCTION_DECL)
586 TREE_THIS_VOLATILE (decl) = 1;
587 else if (TREE_CODE (type) == POINTER_TYPE
588 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
589 TREE_TYPE (decl) = type
591 (build_type_variant (TREE_TYPE (type),
592 TREE_READONLY (TREE_TYPE (type)), 1));
594 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
598 if (TREE_CODE (decl) == FUNCTION_DECL)
599 DECL_IS_MALLOC (decl) = 1;
600 /* ??? TODO: Support types. */
602 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
607 TREE_USED (type) = 1;
608 else if (TREE_CODE (decl) == PARM_DECL
609 || TREE_CODE (decl) == VAR_DECL
610 || TREE_CODE (decl) == FUNCTION_DECL
611 || TREE_CODE (decl) == LABEL_DECL)
612 TREE_USED (decl) = 1;
614 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
618 if (TREE_CODE (decl) == FUNCTION_DECL)
619 TREE_READONLY (decl) = 1;
620 else if (TREE_CODE (type) == POINTER_TYPE
621 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
622 TREE_TYPE (decl) = type
624 (build_type_variant (TREE_TYPE (type), 1,
625 TREE_THIS_VOLATILE (TREE_TYPE (type))));
627 warning ( "`%s' attribute ignored", IDENTIFIER_POINTER (name));
631 if (TREE_CODE (decl) == FUNCTION_DECL)
632 DECL_IS_PURE (decl) = 1;
633 /* ??? TODO: Support types. */
635 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
641 && TREE_CODE (type) == UNION_TYPE
643 || (TYPE_FIELDS (type) != 0
644 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))))
645 TYPE_TRANSPARENT_UNION (type) = 1;
646 else if (decl != 0 && TREE_CODE (decl) == PARM_DECL
647 && TREE_CODE (type) == UNION_TYPE
648 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))
649 DECL_TRANSPARENT_UNION (decl) = 1;
651 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
655 if (TREE_CODE (decl) == FUNCTION_DECL
656 && TREE_CODE (type) == FUNCTION_TYPE
657 && decl_function_context (decl) == 0)
659 DECL_STATIC_CONSTRUCTOR (decl) = 1;
660 TREE_USED (decl) = 1;
663 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
667 if (TREE_CODE (decl) == FUNCTION_DECL
668 && TREE_CODE (type) == FUNCTION_TYPE
669 && decl_function_context (decl) == 0)
671 DECL_STATIC_DESTRUCTOR (decl) = 1;
672 TREE_USED (decl) = 1;
675 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
679 if (TREE_CODE (TREE_VALUE (args)) != IDENTIFIER_NODE)
680 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
684 const char *p = IDENTIFIER_POINTER (TREE_VALUE (args));
685 int len = strlen (p);
686 enum machine_mode mode = VOIDmode;
689 if (len > 4 && p[0] == '_' && p[1] == '_'
690 && p[len - 1] == '_' && p[len - 2] == '_')
692 char *newp = (char *) alloca (len - 1);
694 strcpy (newp, &p[2]);
695 newp[len - 4] = '\0';
699 /* Give this decl a type with the specified mode.
700 First check for the special modes. */
701 if (! strcmp (p, "byte"))
703 else if (!strcmp (p, "word"))
705 else if (! strcmp (p, "pointer"))
708 for (j = 0; j < NUM_MACHINE_MODES; j++)
709 if (!strcmp (p, GET_MODE_NAME (j)))
710 mode = (enum machine_mode) j;
712 if (mode == VOIDmode)
713 error ("unknown machine mode `%s'", p);
714 else if (0 == (typefm = type_for_mode (mode,
715 TREE_UNSIGNED (type))))
716 error ("no data type for mode `%s'", p);
719 TREE_TYPE (decl) = type = typefm;
720 DECL_SIZE (decl) = DECL_SIZE_UNIT (decl) = 0;
721 layout_decl (decl, 0);
727 #ifdef ASM_OUTPUT_SECTION_NAME
728 if ((TREE_CODE (decl) == FUNCTION_DECL
729 || TREE_CODE (decl) == VAR_DECL)
730 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
732 if (TREE_CODE (decl) == VAR_DECL
733 && current_function_decl != NULL_TREE
734 && ! TREE_STATIC (decl))
735 error_with_decl (decl,
736 "section attribute cannot be specified for local variables");
737 /* The decl may have already been given a section attribute from
738 a previous declaration. Ensure they match. */
739 else if (DECL_SECTION_NAME (decl) != NULL_TREE
740 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
741 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
742 error_with_decl (node,
743 "section of `%s' conflicts with previous declaration");
745 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
748 error_with_decl (node,
749 "section attribute not allowed for `%s'");
751 error_with_decl (node,
752 "section attributes are not supported for this target");
759 = (args ? TREE_VALUE (args)
760 : size_int (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
763 /* Strip any NOPs of any kind. */
764 while (TREE_CODE (align_expr) == NOP_EXPR
765 || TREE_CODE (align_expr) == CONVERT_EXPR
766 || TREE_CODE (align_expr) == NON_LVALUE_EXPR)
767 align_expr = TREE_OPERAND (align_expr, 0);
769 if (TREE_CODE (align_expr) != INTEGER_CST)
771 error ("requested alignment is not a constant");
775 if ((i = tree_log2 (align_expr)) == -1)
776 error ("requested alignment is not a power of 2");
777 else if (i > HOST_BITS_PER_INT - 2)
778 error ("requested alignment is too large");
780 TYPE_ALIGN (type) = (1 << i) * BITS_PER_UNIT;
781 else if (TREE_CODE (decl) != VAR_DECL
782 && TREE_CODE (decl) != FIELD_DECL)
783 error_with_decl (decl,
784 "alignment may not be specified for `%s'");
786 DECL_ALIGN (decl) = (1 << i) * BITS_PER_UNIT;
792 tree format_type_id = TREE_VALUE (args);
793 tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
794 tree first_arg_num_expr
795 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
796 unsigned HOST_WIDE_INT format_num, first_arg_num;
797 enum format_type format_type;
799 unsigned int arg_num;
801 if (TREE_CODE (decl) != FUNCTION_DECL)
803 error_with_decl (decl,
804 "argument format specified for non-function `%s'");
808 if (TREE_CODE (format_type_id) != IDENTIFIER_NODE)
810 error ("unrecognized format specifier");
815 const char *p = IDENTIFIER_POINTER (format_type_id);
817 if (!strcmp (p, "printf") || !strcmp (p, "__printf__"))
818 format_type = printf_format_type;
819 else if (!strcmp (p, "scanf") || !strcmp (p, "__scanf__"))
820 format_type = scanf_format_type;
821 else if (!strcmp (p, "strftime")
822 || !strcmp (p, "__strftime__"))
823 format_type = strftime_format_type;
826 warning ("`%s' is an unrecognized format function type", p);
831 /* Strip any conversions from the string index and first arg number
832 and verify they are constants. */
833 while (TREE_CODE (format_num_expr) == NOP_EXPR
834 || TREE_CODE (format_num_expr) == CONVERT_EXPR
835 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
836 format_num_expr = TREE_OPERAND (format_num_expr, 0);
838 while (TREE_CODE (first_arg_num_expr) == NOP_EXPR
839 || TREE_CODE (first_arg_num_expr) == CONVERT_EXPR
840 || TREE_CODE (first_arg_num_expr) == NON_LVALUE_EXPR)
841 first_arg_num_expr = TREE_OPERAND (first_arg_num_expr, 0);
843 if (TREE_CODE (format_num_expr) != INTEGER_CST
844 || TREE_INT_CST_HIGH (format_num_expr) != 0
845 || TREE_CODE (first_arg_num_expr) != INTEGER_CST
846 || TREE_INT_CST_HIGH (first_arg_num_expr) != 0)
848 error ("format string has invalid operand number");
852 format_num = TREE_INT_CST_LOW (format_num_expr);
853 first_arg_num = TREE_INT_CST_LOW (first_arg_num_expr);
854 if (first_arg_num != 0 && first_arg_num <= format_num)
856 error ("format string arg follows the args to be formatted");
860 /* If a parameter list is specified, verify that the format_num
861 argument is actually a string, in case the format attribute
863 argument = TYPE_ARG_TYPES (type);
866 for (arg_num = 1; argument != 0 && arg_num != format_num;
867 ++arg_num, argument = TREE_CHAIN (argument))
871 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
872 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
875 error ("format string arg not a string type");
879 else if (first_arg_num != 0)
881 /* Verify that first_arg_num points to the last arg,
884 arg_num++, argument = TREE_CHAIN (argument);
886 if (arg_num != first_arg_num)
888 error ("args to be formatted is not '...'");
894 record_function_format (DECL_NAME (decl),
895 DECL_ASSEMBLER_NAME (decl),
896 format_type, format_num, first_arg_num);
902 tree format_num_expr = TREE_VALUE (args);
903 unsigned HOST_WIDE_INT format_num;
904 unsigned int arg_num;
907 if (TREE_CODE (decl) != FUNCTION_DECL)
909 error_with_decl (decl,
910 "argument format specified for non-function `%s'");
914 /* Strip any conversions from the first arg number and verify it
916 while (TREE_CODE (format_num_expr) == NOP_EXPR
917 || TREE_CODE (format_num_expr) == CONVERT_EXPR
918 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
919 format_num_expr = TREE_OPERAND (format_num_expr, 0);
921 if (TREE_CODE (format_num_expr) != INTEGER_CST
922 || TREE_INT_CST_HIGH (format_num_expr) != 0)
924 error ("format string has invalid operand number");
928 format_num = TREE_INT_CST_LOW (format_num_expr);
930 /* If a parameter list is specified, verify that the format_num
931 argument is actually a string, in case the format attribute
933 argument = TYPE_ARG_TYPES (type);
936 for (arg_num = 1; argument != 0 && arg_num != format_num;
937 ++arg_num, argument = TREE_CHAIN (argument))
941 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
942 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
945 error ("format string arg not a string type");
950 if (TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) != POINTER_TYPE
951 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_TYPE (decl))))
954 error ("function does not return string type");
958 record_international_format (DECL_NAME (decl),
959 DECL_ASSEMBLER_NAME (decl),
969 if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
970 || (TREE_CODE (decl) != FUNCTION_DECL && ! DECL_EXTERNAL (decl)))
971 error_with_decl (decl,
972 "`%s' defined both normally and as an alias");
973 else if (decl_function_context (decl) == 0)
977 id = TREE_VALUE (args);
978 if (TREE_CODE (id) != STRING_CST)
980 error ("alias arg not a string");
983 id = get_identifier (TREE_STRING_POINTER (id));
985 if (TREE_CODE (decl) == FUNCTION_DECL)
986 DECL_INITIAL (decl) = error_mark_node;
988 DECL_EXTERNAL (decl) = 0;
989 assemble_alias (decl, id);
992 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
995 case A_NO_CHECK_MEMORY_USAGE:
996 if (TREE_CODE (decl) != FUNCTION_DECL)
998 error_with_decl (decl,
999 "`%s' attribute applies only to functions",
1000 IDENTIFIER_POINTER (name));
1002 else if (DECL_INITIAL (decl))
1004 error_with_decl (decl,
1005 "can't set `%s' attribute after definition",
1006 IDENTIFIER_POINTER (name));
1009 DECL_NO_CHECK_MEMORY_USAGE (decl) = 1;
1012 case A_NO_INSTRUMENT_FUNCTION:
1013 if (TREE_CODE (decl) != FUNCTION_DECL)
1015 error_with_decl (decl,
1016 "`%s' attribute applies only to functions",
1017 IDENTIFIER_POINTER (name));
1019 else if (DECL_INITIAL (decl))
1021 error_with_decl (decl,
1022 "can't set `%s' attribute after definition",
1023 IDENTIFIER_POINTER (name));
1026 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1029 case A_NO_LIMIT_STACK:
1030 if (TREE_CODE (decl) != FUNCTION_DECL)
1032 error_with_decl (decl,
1033 "`%s' attribute applies only to functions",
1034 IDENTIFIER_POINTER (name));
1036 else if (DECL_INITIAL (decl))
1038 error_with_decl (decl,
1039 "can't set `%s' attribute after definition",
1040 IDENTIFIER_POINTER (name));
1043 DECL_NO_LIMIT_STACK (decl) = 1;
1049 /* Split SPECS_ATTRS, a list of declspecs and prefix attributes, into two
1050 lists. SPECS_ATTRS may also be just a typespec (eg: RECORD_TYPE).
1052 The head of the declspec list is stored in DECLSPECS.
1053 The head of the attribute list is stored in PREFIX_ATTRIBUTES.
1055 Note that attributes in SPECS_ATTRS are stored in the TREE_PURPOSE of
1056 the list elements. We drop the containing TREE_LIST nodes and link the
1057 resulting attributes together the way decl_attributes expects them. */
1060 split_specs_attrs (specs_attrs, declspecs, prefix_attributes)
1062 tree *declspecs, *prefix_attributes;
1064 tree t, s, a, next, specs, attrs;
1066 /* This can happen after an __extension__ in pedantic mode. */
1067 if (specs_attrs != NULL_TREE
1068 && TREE_CODE (specs_attrs) == INTEGER_CST)
1070 *declspecs = NULL_TREE;
1071 *prefix_attributes = NULL_TREE;
1075 /* This can happen in c++ (eg: decl: typespec initdecls ';'). */
1076 if (specs_attrs != NULL_TREE
1077 && TREE_CODE (specs_attrs) != TREE_LIST)
1079 *declspecs = specs_attrs;
1080 *prefix_attributes = NULL_TREE;
1084 /* Remember to keep the lists in the same order, element-wise. */
1086 specs = s = NULL_TREE;
1087 attrs = a = NULL_TREE;
1088 for (t = specs_attrs; t; t = next)
1090 next = TREE_CHAIN (t);
1091 /* Declspecs have a non-NULL TREE_VALUE. */
1092 if (TREE_VALUE (t) != NULL_TREE)
1094 if (specs == NULL_TREE)
1104 if (attrs == NULL_TREE)
1105 attrs = a = TREE_PURPOSE (t);
1108 TREE_CHAIN (a) = TREE_PURPOSE (t);
1109 a = TREE_PURPOSE (t);
1111 /* More attrs can be linked here, move A to the end. */
1112 while (TREE_CHAIN (a) != NULL_TREE)
1117 /* Terminate the lists. */
1119 TREE_CHAIN (s) = NULL_TREE;
1121 TREE_CHAIN (a) = NULL_TREE;
1125 *prefix_attributes = attrs;
1128 /* Strip attributes from SPECS_ATTRS, a list of declspecs and attributes.
1129 This function is used by the parser when a rule will accept attributes
1130 in a particular position, but we don't want to support that just yet.
1132 A warning is issued for every ignored attribute. */
1135 strip_attrs (specs_attrs)
1140 split_specs_attrs (specs_attrs, &specs, &attrs);
1144 warning ("`%s' attribute ignored",
1145 IDENTIFIER_POINTER (TREE_PURPOSE (attrs)));
1146 attrs = TREE_CHAIN (attrs);
1152 /* Check a printf/fprintf/sprintf/scanf/fscanf/sscanf format against
1153 a parameter list. */
1155 #define T_I &integer_type_node
1156 #define T_L &long_integer_type_node
1157 #define T_LL &long_long_integer_type_node
1158 #define T_S &short_integer_type_node
1159 #define T_UI &unsigned_type_node
1160 #define T_UL &long_unsigned_type_node
1161 #define T_ULL &long_long_unsigned_type_node
1162 #define T_US &short_unsigned_type_node
1163 #define T_F &float_type_node
1164 #define T_D &double_type_node
1165 #define T_LD &long_double_type_node
1166 #define T_C &char_type_node
1167 #define T_UC &unsigned_char_type_node
1168 #define T_V &void_type_node
1169 #define T_W &wchar_type_node
1170 #define T_ST &sizetype
1173 const char *format_chars;
1175 /* Type of argument if no length modifier is used. */
1177 /* Type of argument if length modifier for shortening to byte is used.
1178 If NULL, then this modifier is not allowed. */
1180 /* Type of argument if length modifier for shortening is used.
1181 If NULL, then this modifier is not allowed. */
1183 /* Type of argument if length modifier `l' is used.
1184 If NULL, then this modifier is not allowed. */
1186 /* Type of argument if length modifier `q' or `ll' is used.
1187 If NULL, then this modifier is not allowed. */
1189 /* Type of argument if length modifier `L' is used.
1190 If NULL, then this modifier is not allowed. */
1192 /* Type of argument if length modifiers 'z' or `Z' is used.
1193 If NULL, then this modifier is not allowed. */
1195 /* List of other modifier characters allowed with these options. */
1196 const char *flag_chars;
1199 static format_char_info print_char_table[] = {
1200 { "di", 0, T_I, T_I, T_I, T_L, T_LL, T_LL, T_ST, "-wp0 +" },
1201 { "oxX", 0, T_UI, T_UI, T_UI, T_UL, T_ULL, T_ULL, T_ST, "-wp0#" },
1202 { "u", 0, T_UI, T_UI, T_UI, T_UL, T_ULL, T_ULL, T_ST, "-wp0" },
1203 /* A GNU extension. */
1204 { "m", 0, T_V, NULL, NULL, NULL, NULL, NULL, NULL, "-wp" },
1205 { "feEgGaA", 0, T_D, NULL, NULL, NULL, NULL, T_LD, NULL, "-wp0 +#" },
1206 { "c", 0, T_I, NULL, NULL, T_W, NULL, NULL, NULL, "-w" },
1207 { "C", 0, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "-w" },
1208 { "s", 1, T_C, NULL, NULL, T_W, NULL, NULL, NULL, "-wp" },
1209 { "S", 1, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "-wp" },
1210 { "p", 1, T_V, NULL, NULL, NULL, NULL, NULL, NULL, "-w" },
1211 { "n", 1, T_I, NULL, T_S, T_L, T_LL, NULL, NULL, "" },
1212 { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
1215 static format_char_info scan_char_table[] = {
1216 { "di", 1, T_I, T_C, T_S, T_L, T_LL, T_LL, NULL, "*" },
1217 { "ouxX", 1, T_UI, T_UC, T_US, T_UL, T_ULL, T_ULL, NULL, "*" },
1218 { "efgEGaA", 1, T_F, NULL, NULL, T_D, NULL, T_LD, NULL, "*" },
1219 { "c", 1, T_C, NULL, NULL, T_W, NULL, NULL, NULL, "*" },
1220 { "s", 1, T_C, NULL, NULL, T_W, NULL, NULL, NULL, "*a" },
1221 { "[", 1, T_C, NULL, NULL, NULL, NULL, NULL, NULL, "*a" },
1222 { "C", 1, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "*" },
1223 { "S", 1, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "*a" },
1224 { "p", 2, T_V, NULL, NULL, NULL, NULL, NULL, NULL, "*" },
1225 { "n", 1, T_I, T_C, T_S, T_L, T_LL, NULL, NULL, "" },
1226 { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
1229 /* Handle format characters recognized by glibc's strftime.c.
1230 '2' - MUST do years as only two digits
1231 '3' - MAY do years as only two digits (depending on locale)
1232 'E' - E modifier is acceptable
1233 'O' - O modifier is acceptable to Standard C
1234 'o' - O modifier is acceptable as a GNU extension
1235 'G' - other GNU extensions */
1237 static format_char_info time_char_table[] = {
1238 { "y", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "2EO-_0w" },
1239 { "D", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "2" },
1240 { "g", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "2O-_0w" },
1241 { "cx", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "3E" },
1242 { "%RTXnrt", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "" },
1243 { "P", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "G" },
1244 { "HIMSUWdemw", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0Ow" },
1245 { "Vju", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0Oow" },
1246 { "Gklsz", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0OGw" },
1247 { "ABZa", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "^#" },
1248 { "p", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "#" },
1249 { "bh", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "^" },
1250 { "CY", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0EOw" },
1251 { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
1254 typedef struct function_format_info
1256 struct function_format_info *next; /* next structure on the list */
1257 tree name; /* identifier such as "printf" */
1258 tree assembler_name; /* optional mangled identifier (for C++) */
1259 enum format_type format_type; /* type of format (printf, scanf, etc.) */
1260 int format_num; /* number of format argument */
1261 int first_arg_num; /* number of first arg (zero for varargs) */
1262 } function_format_info;
1264 static function_format_info *function_format_list = NULL;
1266 typedef struct international_format_info
1268 struct international_format_info *next; /* next structure on the list */
1269 tree name; /* identifier such as "gettext" */
1270 tree assembler_name; /* optional mangled identifier (for C++) */
1271 int format_num; /* number of format argument */
1272 } international_format_info;
1274 static international_format_info *international_format_list = NULL;
1276 static void check_format_info PARAMS ((function_format_info *, tree));
1278 /* Initialize the table of functions to perform format checking on.
1279 The ANSI functions are always checked (whether <stdio.h> is
1280 included or not), since it is common to call printf without
1281 including <stdio.h>. There shouldn't be a problem with this,
1282 since ANSI reserves these function names whether you include the
1283 header file or not. In any case, the checking is harmless.
1285 Also initialize the name of function that modify the format string for
1286 internationalization purposes. */
1289 init_function_format_info ()
1291 record_function_format (get_identifier ("printf"), NULL_TREE,
1292 printf_format_type, 1, 2);
1293 record_function_format (get_identifier ("fprintf"), NULL_TREE,
1294 printf_format_type, 2, 3);
1295 record_function_format (get_identifier ("sprintf"), NULL_TREE,
1296 printf_format_type, 2, 3);
1297 record_function_format (get_identifier ("scanf"), NULL_TREE,
1298 scanf_format_type, 1, 2);
1299 record_function_format (get_identifier ("fscanf"), NULL_TREE,
1300 scanf_format_type, 2, 3);
1301 record_function_format (get_identifier ("sscanf"), NULL_TREE,
1302 scanf_format_type, 2, 3);
1303 record_function_format (get_identifier ("vprintf"), NULL_TREE,
1304 printf_format_type, 1, 0);
1305 record_function_format (get_identifier ("vfprintf"), NULL_TREE,
1306 printf_format_type, 2, 0);
1307 record_function_format (get_identifier ("vsprintf"), NULL_TREE,
1308 printf_format_type, 2, 0);
1309 record_function_format (get_identifier ("strftime"), NULL_TREE,
1310 strftime_format_type, 3, 0);
1312 record_international_format (get_identifier ("gettext"), NULL_TREE, 1);
1313 record_international_format (get_identifier ("dgettext"), NULL_TREE, 2);
1314 record_international_format (get_identifier ("dcgettext"), NULL_TREE, 2);
1317 /* Record information for argument format checking. FUNCTION_IDENT is
1318 the identifier node for the name of the function to check (its decl
1319 need not exist yet).
1320 FORMAT_TYPE specifies the type of format checking. FORMAT_NUM is the number
1321 of the argument which is the format control string (starting from 1).
1322 FIRST_ARG_NUM is the number of the first actual argument to check
1323 against the format string, or zero if no checking is not be done
1324 (e.g. for varargs such as vfprintf). */
1327 record_function_format (name, assembler_name, format_type,
1328 format_num, first_arg_num)
1330 tree assembler_name;
1331 enum format_type format_type;
1335 function_format_info *info;
1337 /* Re-use existing structure if it's there. */
1339 for (info = function_format_list; info; info = info->next)
1341 if (info->name == name && info->assembler_name == assembler_name)
1346 info = (function_format_info *) xmalloc (sizeof (function_format_info));
1347 info->next = function_format_list;
1348 function_format_list = info;
1351 info->assembler_name = assembler_name;
1354 info->format_type = format_type;
1355 info->format_num = format_num;
1356 info->first_arg_num = first_arg_num;
1359 /* Record information for the names of function that modify the format
1360 argument to format functions. FUNCTION_IDENT is the identifier node for
1361 the name of the function (its decl need not exist yet) and FORMAT_NUM is
1362 the number of the argument which is the format control string (starting
1366 record_international_format (name, assembler_name, format_num)
1368 tree assembler_name;
1371 international_format_info *info;
1373 /* Re-use existing structure if it's there. */
1375 for (info = international_format_list; info; info = info->next)
1377 if (info->name == name && info->assembler_name == assembler_name)
1384 = (international_format_info *)
1385 xmalloc (sizeof (international_format_info));
1386 info->next = international_format_list;
1387 international_format_list = info;
1390 info->assembler_name = assembler_name;
1393 info->format_num = format_num;
1399 warning ("too few arguments for format");
1402 /* Check the argument list of a call to printf, scanf, etc.
1403 NAME is the function identifier.
1404 ASSEMBLER_NAME is the function's assembler identifier.
1405 (Either NAME or ASSEMBLER_NAME, but not both, may be NULL_TREE.)
1406 PARAMS is the list of argument values. */
1409 check_function_format (name, assembler_name, params)
1411 tree assembler_name;
1414 function_format_info *info;
1416 /* See if this function is a format function. */
1417 for (info = function_format_list; info; info = info->next)
1419 if (info->assembler_name
1420 ? (info->assembler_name == assembler_name)
1421 : (info->name == name))
1423 /* Yup; check it. */
1424 check_format_info (info, params);
1430 /* Check the argument list of a call to printf, scanf, etc.
1431 INFO points to the function_format_info structure.
1432 PARAMS is the list of argument values. */
1435 check_format_info (info, params)
1436 function_format_info *info;
1441 int suppressed, wide, precise;
1442 int length_char = 0;
1449 tree first_fillin_param;
1450 const char *format_chars;
1451 format_char_info *fci = NULL;
1453 int has_operand_number = 0;
1455 /* Skip to format argument. If the argument isn't available, there's
1456 no work for us to do; prototype checking will catch the problem. */
1457 for (arg_num = 1; ; ++arg_num)
1461 if (arg_num == info->format_num)
1463 params = TREE_CHAIN (params);
1465 format_tree = TREE_VALUE (params);
1466 params = TREE_CHAIN (params);
1467 if (format_tree == 0)
1470 /* We can only check the format if it's a string constant. */
1471 while (TREE_CODE (format_tree) == NOP_EXPR)
1472 format_tree = TREE_OPERAND (format_tree, 0); /* strip coercion */
1474 if (TREE_CODE (format_tree) == CALL_EXPR
1475 && TREE_CODE (TREE_OPERAND (format_tree, 0)) == ADDR_EXPR
1476 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0))
1479 tree function = TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0);
1481 /* See if this is a call to a known internationalization function
1482 that modifies the format arg. */
1483 international_format_info *info;
1485 for (info = international_format_list; info; info = info->next)
1486 if (info->assembler_name
1487 ? (info->assembler_name == DECL_ASSEMBLER_NAME (function))
1488 : (info->name == DECL_NAME (function)))
1493 for (inner_args = TREE_OPERAND (format_tree, 1), i = 1;
1495 inner_args = TREE_CHAIN (inner_args), i++)
1496 if (i == info->format_num)
1498 format_tree = TREE_VALUE (inner_args);
1500 while (TREE_CODE (format_tree) == NOP_EXPR)
1501 format_tree = TREE_OPERAND (format_tree, 0);
1506 if (integer_zerop (format_tree))
1508 warning ("null format string");
1511 if (TREE_CODE (format_tree) != ADDR_EXPR)
1513 /* The user may get multiple warnings if the supplied argument
1514 isn't even a string pointer. */
1515 /* Functions taking a va_list normally pass a non-literal format
1516 string. These functions typically are declared with
1517 first_arg_num == 0, so avoid warning in those cases. */
1518 if (info->first_arg_num != 0 && warn_format > 1)
1519 warning ("format not a string literal, argument types not checked");
1522 format_tree = TREE_OPERAND (format_tree, 0);
1523 if (TREE_CODE (format_tree) != STRING_CST)
1525 /* The user may get multiple warnings if the supplied argument
1526 isn't even a string pointer. */
1527 /* Functions taking a va_list normally pass a non-literal format
1528 string. These functions typically are declared with
1529 first_arg_num == 0, so avoid warning in those cases. */
1530 if (info->first_arg_num != 0 && warn_format > 1)
1531 warning ("format not a string literal, argument types not checked");
1534 format_chars = TREE_STRING_POINTER (format_tree);
1535 format_length = TREE_STRING_LENGTH (format_tree);
1536 if (format_length <= 1)
1537 warning ("zero-length format string");
1538 if (format_chars[--format_length] != 0)
1540 warning ("unterminated format string");
1543 /* Skip to first argument to check. */
1544 while (arg_num + 1 < info->first_arg_num)
1548 params = TREE_CHAIN (params);
1552 first_fillin_param = params;
1556 if (*format_chars == 0)
1558 if (format_chars - TREE_STRING_POINTER (format_tree) != format_length)
1559 warning ("embedded `\\0' in format");
1560 if (info->first_arg_num != 0 && params != 0 && ! has_operand_number)
1561 warning ("too many arguments for format");
1564 if (*format_chars++ != '%')
1566 if (*format_chars == 0)
1568 warning ("spurious trailing `%%' in format");
1571 if (*format_chars == '%')
1577 suppressed = wide = precise = FALSE;
1578 if (info->format_type == scanf_format_type)
1580 suppressed = *format_chars == '*';
1583 while (ISDIGIT (*format_chars))
1586 else if (info->format_type == strftime_format_type)
1588 while (*format_chars != 0 && index ("_-0^#", *format_chars) != 0)
1591 warning ("ANSI C does not support the strftime `%c' flag",
1593 if (index (flag_chars, *format_chars) != 0)
1595 warning ("repeated `%c' flag in format",
1601 i = strlen (flag_chars);
1602 flag_chars[i++] = *format_chars++;
1606 while (ISDIGIT ((unsigned char) *format_chars))
1611 if (wide && pedantic)
1612 warning ("ANSI C does not support strftime format width");
1613 if (*format_chars == 'E' || *format_chars == 'O')
1615 i = strlen (flag_chars);
1616 flag_chars[i++] = *format_chars++;
1618 if (*format_chars == 'E' || *format_chars == 'O')
1620 warning ("multiple E/O modifiers in format");
1621 while (*format_chars == 'E' || *format_chars == 'O')
1626 else if (info->format_type == printf_format_type)
1628 /* See if we have a number followed by a dollar sign. If we do,
1629 it is an operand number, so set PARAMS to that operand. */
1630 if (*format_chars >= '0' && *format_chars <= '9')
1632 const char *p = format_chars;
1634 while (*p >= '0' && *p++ <= '9')
1639 int opnum = atoi (format_chars);
1641 params = first_fillin_param;
1642 format_chars = p + 1;
1643 has_operand_number = 1;
1645 for (i = 1; i < opnum && params != 0; i++)
1646 params = TREE_CHAIN (params);
1648 if (opnum == 0 || params == 0)
1650 warning ("operand number out of range in format");
1656 while (*format_chars != 0 && index (" +#0-", *format_chars) != 0)
1658 if (index (flag_chars, *format_chars) != 0)
1659 warning ("repeated `%c' flag in format", *format_chars++);
1662 i = strlen (flag_chars);
1663 flag_chars[i++] = *format_chars++;
1667 /* "If the space and + flags both appear,
1668 the space flag will be ignored." */
1669 if (index (flag_chars, ' ') != 0
1670 && index (flag_chars, '+') != 0)
1671 warning ("use of both ` ' and `+' flags in format");
1672 /* "If the 0 and - flags both appear,
1673 the 0 flag will be ignored." */
1674 if (index (flag_chars, '0') != 0
1675 && index (flag_chars, '-') != 0)
1676 warning ("use of both `0' and `-' flags in format");
1677 if (*format_chars == '*')
1680 /* "...a field width...may be indicated by an asterisk.
1681 In this case, an int argument supplies the field width..." */
1688 if (info->first_arg_num != 0)
1690 cur_param = TREE_VALUE (params);
1691 params = TREE_CHAIN (params);
1693 /* size_t is generally not valid here.
1694 It will work on most machines, because size_t and int
1695 have the same mode. But might as well warn anyway,
1696 since it will fail on other machines. */
1697 if ((TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1698 != integer_type_node)
1700 (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1701 != unsigned_type_node))
1702 warning ("field width is not type int (arg %d)", arg_num);
1707 while (ISDIGIT (*format_chars))
1713 if (*format_chars == '.')
1717 if (*format_chars != '*' && !ISDIGIT (*format_chars))
1718 warning ("`.' not followed by `*' or digit in format");
1719 /* "...a...precision...may be indicated by an asterisk.
1720 In this case, an int argument supplies the...precision." */
1721 if (*format_chars == '*')
1723 if (info->first_arg_num != 0)
1731 cur_param = TREE_VALUE (params);
1732 params = TREE_CHAIN (params);
1734 if (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1735 != integer_type_node)
1736 warning ("field width is not type int (arg %d)",
1742 while (ISDIGIT (*format_chars))
1750 if (info->format_type != strftime_format_type)
1752 if (*format_chars == 'h' || *format_chars == 'l')
1753 length_char = *format_chars++;
1754 else if (*format_chars == 'q' || *format_chars == 'L')
1756 length_char = *format_chars++;
1758 warning ("ANSI C does not support the `%c' length modifier",
1761 else if (*format_chars == 'Z' || *format_chars == 'z')
1763 length_char = *format_chars++;
1764 if (pedantic && (length_char == 'Z' || !flag_isoc99))
1765 warning ("ANSI C does not support the `%c' length modifier",
1770 if (length_char == 'l' && *format_chars == 'l')
1772 length_char = 'q', format_chars++;
1773 if (pedantic && !flag_isoc99)
1774 warning ("ANSI C does not support the `ll' length modifier");
1776 else if (length_char == 'h' && *format_chars == 'h')
1778 length_char = 'H', format_chars++;
1779 if (pedantic && !flag_isoc99)
1780 warning ("ANSI C does not support the `hh' length modifier");
1782 if (*format_chars == 'a' && info->format_type == scanf_format_type)
1784 if (format_chars[1] == 's' || format_chars[1] == 'S'
1785 || format_chars[1] == '[')
1787 /* `a' is used as a flag. */
1792 if (suppressed && length_char != 0)
1793 warning ("use of `*' and `%c' together in format", length_char);
1795 format_char = *format_chars;
1796 if (format_char == 0
1797 || (info->format_type != strftime_format_type && format_char == '%'))
1799 warning ("conversion lacks type at end of format");
1802 /* The m, C, and S formats are GNU extensions. */
1803 if (pedantic && info->format_type != strftime_format_type
1804 && (format_char == 'm' || format_char == 'C' || format_char == 'S'))
1805 warning ("ANSI C does not support the `%c' format", format_char);
1806 /* The a and A formats are C99 extensions. */
1807 if (pedantic && info->format_type != strftime_format_type
1808 && (format_char == 'a' || format_char == 'A')
1810 warning ("ANSI C does not support the `%c' format", format_char);
1812 switch (info->format_type)
1814 case printf_format_type:
1815 fci = print_char_table;
1817 case scanf_format_type:
1818 fci = scan_char_table;
1820 case strftime_format_type:
1821 fci = time_char_table;
1826 while (fci->format_chars != 0
1827 && index (fci->format_chars, format_char) == 0)
1829 if (fci->format_chars == 0)
1831 if (ISGRAPH(format_char))
1832 warning ("unknown conversion type character `%c' in format",
1835 warning ("unknown conversion type character 0x%x in format",
1841 if (index (fci->flag_chars, 'G') != 0)
1842 warning ("ANSI C does not support `%%%c'", format_char);
1843 if (index (fci->flag_chars, 'o') != 0
1844 && index (flag_chars, 'O') != 0)
1845 warning ("ANSI C does not support `%%O%c'", format_char);
1847 if (wide && index (fci->flag_chars, 'w') == 0)
1848 warning ("width used with `%c' format", format_char);
1849 if (index (fci->flag_chars, '2') != 0)
1850 warning ("`%%%c' yields only last 2 digits of year", format_char);
1851 else if (index (fci->flag_chars, '3') != 0)
1852 warning ("`%%%c' yields only last 2 digits of year in some locales",
1854 if (precise && index (fci->flag_chars, 'p') == 0)
1855 warning ("precision used with `%c' format", format_char);
1856 if (aflag && index (fci->flag_chars, 'a') == 0)
1858 warning ("`a' flag used with `%c' format", format_char);
1859 /* To simplify the following code. */
1862 /* The a flag is a GNU extension. */
1863 else if (pedantic && aflag)
1864 warning ("ANSI C does not support the `a' flag");
1865 if (info->format_type == scanf_format_type && format_char == '[')
1867 /* Skip over scan set, in case it happens to have '%' in it. */
1868 if (*format_chars == '^')
1870 /* Find closing bracket; if one is hit immediately, then
1871 it's part of the scan set rather than a terminator. */
1872 if (*format_chars == ']')
1874 while (*format_chars && *format_chars != ']')
1876 if (*format_chars != ']')
1877 /* The end of the format string was reached. */
1878 warning ("no closing `]' for `%%[' format");
1882 if (index (fci->flag_chars, '*') == 0)
1883 warning ("suppression of `%c' conversion in format", format_char);
1886 for (i = 0; flag_chars[i] != 0; ++i)
1888 if (index (fci->flag_chars, flag_chars[i]) == 0)
1889 warning ("flag `%c' used with type `%c'",
1890 flag_chars[i], format_char);
1892 if (info->format_type == strftime_format_type)
1894 if (precise && index (flag_chars, '0') != 0
1895 && (format_char == 'd' || format_char == 'i'
1896 || format_char == 'o' || format_char == 'u'
1897 || format_char == 'x' || format_char == 'X'))
1898 warning ("`0' flag ignored with precision specifier and `%c' format",
1900 switch (length_char)
1902 default: wanted_type = fci->nolen ? *(fci->nolen) : 0; break;
1903 case 'H': wanted_type = fci->hhlen ? *(fci->hhlen) : 0; break;
1904 case 'h': wanted_type = fci->hlen ? *(fci->hlen) : 0; break;
1905 case 'l': wanted_type = fci->llen ? *(fci->llen) : 0; break;
1906 case 'q': wanted_type = fci->qlen ? *(fci->qlen) : 0; break;
1907 case 'L': wanted_type = fci->bigllen ? *(fci->bigllen) : 0; break;
1908 case 'z': case 'Z': wanted_type = fci->zlen ? *fci->zlen : 0; break;
1910 if (wanted_type == 0)
1911 warning ("use of `%c' length character with `%c' type character",
1912 length_char, format_char);
1914 /* Finally. . .check type of argument against desired type! */
1915 if (info->first_arg_num == 0)
1917 if (fci->pointer_count == 0 && wanted_type == void_type_node)
1918 /* This specifier takes no argument. */
1925 cur_param = TREE_VALUE (params);
1926 params = TREE_CHAIN (params);
1928 cur_type = TREE_TYPE (cur_param);
1930 STRIP_NOPS (cur_param);
1932 /* Check the types of any additional pointer arguments
1933 that precede the "real" argument. */
1934 for (i = 0; i < fci->pointer_count + aflag; ++i)
1936 if (TREE_CODE (cur_type) == POINTER_TYPE)
1938 cur_type = TREE_TYPE (cur_type);
1940 if (cur_param != 0 && TREE_CODE (cur_param) == ADDR_EXPR)
1941 cur_param = TREE_OPERAND (cur_param, 0);
1947 if (TREE_CODE (cur_type) != ERROR_MARK)
1949 if (fci->pointer_count + aflag == 1)
1950 warning ("format argument is not a pointer (arg %d)", arg_num);
1952 warning ("format argument is not a pointer to a pointer (arg %d)", arg_num);
1957 /* See if this is an attempt to write into a const type with
1958 scanf or with printf "%n". */
1959 if ((info->format_type == scanf_format_type
1960 || (info->format_type == printf_format_type
1961 && format_char == 'n'))
1962 && i == fci->pointer_count + aflag
1964 && TREE_CODE (cur_type) != ERROR_MARK
1965 && (TYPE_READONLY (cur_type)
1967 && (TREE_CODE_CLASS (TREE_CODE (cur_param)) == 'c'
1968 || (DECL_P (cur_param) && TREE_READONLY (cur_param))))))
1969 warning ("writing into constant object (arg %d)", arg_num);
1971 /* Check the type of the "real" argument, if there's a type we want. */
1972 if (i == fci->pointer_count + aflag && wanted_type != 0
1973 && TREE_CODE (cur_type) != ERROR_MARK
1974 && wanted_type != TYPE_MAIN_VARIANT (cur_type)
1975 /* If we want `void *', allow any pointer type.
1976 (Anything else would already have got a warning.) */
1977 && ! (wanted_type == void_type_node
1978 && fci->pointer_count > 0)
1979 /* Don't warn about differences merely in signedness. */
1980 && !(TREE_CODE (wanted_type) == INTEGER_TYPE
1981 && TREE_CODE (TYPE_MAIN_VARIANT (cur_type)) == INTEGER_TYPE
1982 && (TREE_UNSIGNED (wanted_type)
1983 ? wanted_type == (cur_type = unsigned_type (cur_type))
1984 : wanted_type == (cur_type = signed_type (cur_type))))
1985 /* Likewise, "signed char", "unsigned char" and "char" are
1986 equivalent but the above test won't consider them equivalent. */
1987 && ! (wanted_type == char_type_node
1988 && (TYPE_MAIN_VARIANT (cur_type) == signed_char_type_node
1989 || TYPE_MAIN_VARIANT (cur_type) == unsigned_char_type_node)))
1991 register const char *this;
1992 register const char *that;
1994 this = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (wanted_type)));
1996 if (TREE_CODE (cur_type) != ERROR_MARK
1997 && TYPE_NAME (cur_type) != 0
1998 && TREE_CODE (cur_type) != INTEGER_TYPE
1999 && !(TREE_CODE (cur_type) == POINTER_TYPE
2000 && TREE_CODE (TREE_TYPE (cur_type)) == INTEGER_TYPE))
2002 if (TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL
2003 && DECL_NAME (TYPE_NAME (cur_type)) != 0)
2004 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
2006 that = IDENTIFIER_POINTER (TYPE_NAME (cur_type));
2009 /* A nameless type can't possibly match what the format wants.
2010 So there will be a warning for it.
2011 Make up a string to describe vaguely what it is. */
2014 if (TREE_CODE (cur_type) == POINTER_TYPE)
2017 that = "different type";
2020 /* Make the warning better in case of mismatch of int vs long. */
2021 if (TREE_CODE (cur_type) == INTEGER_TYPE
2022 && TREE_CODE (wanted_type) == INTEGER_TYPE
2023 && TYPE_PRECISION (cur_type) == TYPE_PRECISION (wanted_type)
2024 && TYPE_NAME (cur_type) != 0
2025 && TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL)
2026 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
2028 if (strcmp (this, that) != 0)
2029 warning ("%s format, %s arg (arg %d)", this, that, arg_num);
2034 /* Print a warning if a constant expression had overflow in folding.
2035 Invoke this function on every expression that the language
2036 requires to be a constant expression.
2037 Note the ANSI C standard says it is erroneous for a
2038 constant expression to overflow. */
2041 constant_expression_warning (value)
2044 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
2045 || TREE_CODE (value) == COMPLEX_CST)
2046 && TREE_CONSTANT_OVERFLOW (value) && pedantic)
2047 pedwarn ("overflow in constant expression");
2050 /* Print a warning if an expression had overflow in folding.
2051 Invoke this function on every expression that
2052 (1) appears in the source code, and
2053 (2) might be a constant expression that overflowed, and
2054 (3) is not already checked by convert_and_check;
2055 however, do not invoke this function on operands of explicit casts. */
2058 overflow_warning (value)
2061 if ((TREE_CODE (value) == INTEGER_CST
2062 || (TREE_CODE (value) == COMPLEX_CST
2063 && TREE_CODE (TREE_REALPART (value)) == INTEGER_CST))
2064 && TREE_OVERFLOW (value))
2066 TREE_OVERFLOW (value) = 0;
2067 if (skip_evaluation == 0)
2068 warning ("integer overflow in expression");
2070 else if ((TREE_CODE (value) == REAL_CST
2071 || (TREE_CODE (value) == COMPLEX_CST
2072 && TREE_CODE (TREE_REALPART (value)) == REAL_CST))
2073 && TREE_OVERFLOW (value))
2075 TREE_OVERFLOW (value) = 0;
2076 if (skip_evaluation == 0)
2077 warning ("floating point overflow in expression");
2081 /* Print a warning if a large constant is truncated to unsigned,
2082 or if -Wconversion is used and a constant < 0 is converted to unsigned.
2083 Invoke this function on every expression that might be implicitly
2084 converted to an unsigned type. */
2087 unsigned_conversion_warning (result, operand)
2088 tree result, operand;
2090 if (TREE_CODE (operand) == INTEGER_CST
2091 && TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
2092 && TREE_UNSIGNED (TREE_TYPE (result))
2093 && skip_evaluation == 0
2094 && !int_fits_type_p (operand, TREE_TYPE (result)))
2096 if (!int_fits_type_p (operand, signed_type (TREE_TYPE (result))))
2097 /* This detects cases like converting -129 or 256 to unsigned char. */
2098 warning ("large integer implicitly truncated to unsigned type");
2099 else if (warn_conversion)
2100 warning ("negative integer implicitly converted to unsigned type");
2104 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2105 Invoke this function on every expression that is converted implicitly,
2106 i.e. because of language rules and not because of an explicit cast. */
2109 convert_and_check (type, expr)
2112 tree t = convert (type, expr);
2113 if (TREE_CODE (t) == INTEGER_CST)
2115 if (TREE_OVERFLOW (t))
2117 TREE_OVERFLOW (t) = 0;
2119 /* Do not diagnose overflow in a constant expression merely
2120 because a conversion overflowed. */
2121 TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (expr);
2123 /* No warning for converting 0x80000000 to int. */
2124 if (!(TREE_UNSIGNED (type) < TREE_UNSIGNED (TREE_TYPE (expr))
2125 && TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
2126 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (expr))))
2127 /* If EXPR fits in the unsigned version of TYPE,
2128 don't warn unless pedantic. */
2130 || TREE_UNSIGNED (type)
2131 || ! int_fits_type_p (expr, unsigned_type (type)))
2132 && skip_evaluation == 0)
2133 warning ("overflow in implicit constant conversion");
2136 unsigned_conversion_warning (t, expr);
2142 c_expand_expr_stmt (expr)
2145 /* Do default conversion if safe and possibly important,
2146 in case within ({...}). */
2147 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE && lvalue_p (expr))
2148 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
2149 expr = default_conversion (expr);
2151 if (TREE_TYPE (expr) != error_mark_node
2152 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
2153 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
2154 error ("expression statement has incomplete type");
2156 expand_expr_stmt (expr);
2159 /* Validate the expression after `case' and apply default promotions. */
2162 check_case_value (value)
2165 if (value == NULL_TREE)
2168 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
2169 STRIP_TYPE_NOPS (value);
2171 if (TREE_CODE (value) != INTEGER_CST
2172 && value != error_mark_node)
2174 error ("case label does not reduce to an integer constant");
2175 value = error_mark_node;
2178 /* Promote char or short to int. */
2179 value = default_conversion (value);
2181 constant_expression_warning (value);
2186 /* Return an integer type with BITS bits of precision,
2187 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
2190 type_for_size (bits, unsignedp)
2194 if (bits == TYPE_PRECISION (integer_type_node))
2195 return unsignedp ? unsigned_type_node : integer_type_node;
2197 if (bits == TYPE_PRECISION (signed_char_type_node))
2198 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2200 if (bits == TYPE_PRECISION (short_integer_type_node))
2201 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2203 if (bits == TYPE_PRECISION (long_integer_type_node))
2204 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2206 if (bits == TYPE_PRECISION (long_long_integer_type_node))
2207 return (unsignedp ? long_long_unsigned_type_node
2208 : long_long_integer_type_node);
2210 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
2211 return (unsignedp ? widest_unsigned_literal_type_node
2212 : widest_integer_literal_type_node);
2214 if (bits <= TYPE_PRECISION (intQI_type_node))
2215 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2217 if (bits <= TYPE_PRECISION (intHI_type_node))
2218 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2220 if (bits <= TYPE_PRECISION (intSI_type_node))
2221 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2223 if (bits <= TYPE_PRECISION (intDI_type_node))
2224 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2229 /* Return a data type that has machine mode MODE.
2230 If the mode is an integer,
2231 then UNSIGNEDP selects between signed and unsigned types. */
2234 type_for_mode (mode, unsignedp)
2235 enum machine_mode mode;
2238 if (mode == TYPE_MODE (integer_type_node))
2239 return unsignedp ? unsigned_type_node : integer_type_node;
2241 if (mode == TYPE_MODE (signed_char_type_node))
2242 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2244 if (mode == TYPE_MODE (short_integer_type_node))
2245 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2247 if (mode == TYPE_MODE (long_integer_type_node))
2248 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2250 if (mode == TYPE_MODE (long_long_integer_type_node))
2251 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
2253 if (mode == TYPE_MODE (widest_integer_literal_type_node))
2254 return unsignedp ? widest_unsigned_literal_type_node
2255 : widest_integer_literal_type_node;
2257 if (mode == TYPE_MODE (intQI_type_node))
2258 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2260 if (mode == TYPE_MODE (intHI_type_node))
2261 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2263 if (mode == TYPE_MODE (intSI_type_node))
2264 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2266 if (mode == TYPE_MODE (intDI_type_node))
2267 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2269 #if HOST_BITS_PER_WIDE_INT >= 64
2270 if (mode == TYPE_MODE (intTI_type_node))
2271 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2274 if (mode == TYPE_MODE (float_type_node))
2275 return float_type_node;
2277 if (mode == TYPE_MODE (double_type_node))
2278 return double_type_node;
2280 if (mode == TYPE_MODE (long_double_type_node))
2281 return long_double_type_node;
2283 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
2284 return build_pointer_type (char_type_node);
2286 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
2287 return build_pointer_type (integer_type_node);
2292 /* Return an unsigned type the same as TYPE in other respects. */
2294 unsigned_type (type)
2297 tree type1 = TYPE_MAIN_VARIANT (type);
2298 if (type1 == signed_char_type_node || type1 == char_type_node)
2299 return unsigned_char_type_node;
2300 if (type1 == integer_type_node)
2301 return unsigned_type_node;
2302 if (type1 == short_integer_type_node)
2303 return short_unsigned_type_node;
2304 if (type1 == long_integer_type_node)
2305 return long_unsigned_type_node;
2306 if (type1 == long_long_integer_type_node)
2307 return long_long_unsigned_type_node;
2308 if (type1 == widest_integer_literal_type_node)
2309 return widest_unsigned_literal_type_node;
2310 #if HOST_BITS_PER_WIDE_INT >= 64
2311 if (type1 == intTI_type_node)
2312 return unsigned_intTI_type_node;
2314 if (type1 == intDI_type_node)
2315 return unsigned_intDI_type_node;
2316 if (type1 == intSI_type_node)
2317 return unsigned_intSI_type_node;
2318 if (type1 == intHI_type_node)
2319 return unsigned_intHI_type_node;
2320 if (type1 == intQI_type_node)
2321 return unsigned_intQI_type_node;
2323 return signed_or_unsigned_type (1, type);
2326 /* Return a signed type the same as TYPE in other respects. */
2332 tree type1 = TYPE_MAIN_VARIANT (type);
2333 if (type1 == unsigned_char_type_node || type1 == char_type_node)
2334 return signed_char_type_node;
2335 if (type1 == unsigned_type_node)
2336 return integer_type_node;
2337 if (type1 == short_unsigned_type_node)
2338 return short_integer_type_node;
2339 if (type1 == long_unsigned_type_node)
2340 return long_integer_type_node;
2341 if (type1 == long_long_unsigned_type_node)
2342 return long_long_integer_type_node;
2343 if (type1 == widest_unsigned_literal_type_node)
2344 return widest_integer_literal_type_node;
2345 #if HOST_BITS_PER_WIDE_INT >= 64
2346 if (type1 == unsigned_intTI_type_node)
2347 return intTI_type_node;
2349 if (type1 == unsigned_intDI_type_node)
2350 return intDI_type_node;
2351 if (type1 == unsigned_intSI_type_node)
2352 return intSI_type_node;
2353 if (type1 == unsigned_intHI_type_node)
2354 return intHI_type_node;
2355 if (type1 == unsigned_intQI_type_node)
2356 return intQI_type_node;
2358 return signed_or_unsigned_type (0, type);
2361 /* Return a type the same as TYPE except unsigned or
2362 signed according to UNSIGNEDP. */
2365 signed_or_unsigned_type (unsignedp, type)
2369 if (! INTEGRAL_TYPE_P (type)
2370 || TREE_UNSIGNED (type) == unsignedp)
2373 if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node))
2374 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2375 if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2376 return unsignedp ? unsigned_type_node : integer_type_node;
2377 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node))
2378 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2379 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node))
2380 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2381 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node))
2382 return (unsignedp ? long_long_unsigned_type_node
2383 : long_long_integer_type_node);
2384 if (TYPE_PRECISION (type) == TYPE_PRECISION (widest_integer_literal_type_node))
2385 return (unsignedp ? widest_unsigned_literal_type_node
2386 : widest_integer_literal_type_node);
2390 /* Return the minimum number of bits needed to represent VALUE in a
2391 signed or unsigned type, UNSIGNEDP says which. */
2394 min_precision (value, unsignedp)
2400 /* If the value is negative, compute its negative minus 1. The latter
2401 adjustment is because the absolute value of the largest negative value
2402 is one larger than the largest positive value. This is equivalent to
2403 a bit-wise negation, so use that operation instead. */
2405 if (tree_int_cst_sgn (value) < 0)
2406 value = fold (build1 (BIT_NOT_EXPR, TREE_TYPE (value), value));
2408 /* Return the number of bits needed, taking into account the fact
2409 that we need one more bit for a signed than unsigned type. */
2411 if (integer_zerop (value))
2414 log = tree_floor_log2 (value);
2416 return log + 1 + ! unsignedp;
2419 /* Print an error message for invalid operands to arith operation CODE.
2420 NOP_EXPR is used as a special case (see truthvalue_conversion). */
2423 binary_op_error (code)
2424 enum tree_code code;
2426 register const char *opname;
2431 error ("invalid truth-value expression");
2435 opname = "+"; break;
2437 opname = "-"; break;
2439 opname = "*"; break;
2441 opname = "max"; break;
2443 opname = "min"; break;
2445 opname = "=="; break;
2447 opname = "!="; break;
2449 opname = "<="; break;
2451 opname = ">="; break;
2453 opname = "<"; break;
2455 opname = ">"; break;
2457 opname = "<<"; break;
2459 opname = ">>"; break;
2460 case TRUNC_MOD_EXPR:
2461 case FLOOR_MOD_EXPR:
2462 opname = "%"; break;
2463 case TRUNC_DIV_EXPR:
2464 case FLOOR_DIV_EXPR:
2465 opname = "/"; break;
2467 opname = "&"; break;
2469 opname = "|"; break;
2470 case TRUTH_ANDIF_EXPR:
2471 opname = "&&"; break;
2472 case TRUTH_ORIF_EXPR:
2473 opname = "||"; break;
2475 opname = "^"; break;
2478 opname = "rotate"; break;
2480 opname = "unknown"; break;
2482 error ("invalid operands to binary %s", opname);
2485 /* Subroutine of build_binary_op, used for comparison operations.
2486 See if the operands have both been converted from subword integer types
2487 and, if so, perhaps change them both back to their original type.
2488 This function is also responsible for converting the two operands
2489 to the proper common type for comparison.
2491 The arguments of this function are all pointers to local variables
2492 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
2493 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
2495 If this function returns nonzero, it means that the comparison has
2496 a constant value. What this function returns is an expression for
2500 shorten_compare (op0_ptr, op1_ptr, restype_ptr, rescode_ptr)
2501 tree *op0_ptr, *op1_ptr;
2503 enum tree_code *rescode_ptr;
2506 tree op0 = *op0_ptr;
2507 tree op1 = *op1_ptr;
2508 int unsignedp0, unsignedp1;
2510 tree primop0, primop1;
2511 enum tree_code code = *rescode_ptr;
2513 /* Throw away any conversions to wider types
2514 already present in the operands. */
2516 primop0 = get_narrower (op0, &unsignedp0);
2517 primop1 = get_narrower (op1, &unsignedp1);
2519 /* Handle the case that OP0 does not *contain* a conversion
2520 but it *requires* conversion to FINAL_TYPE. */
2522 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
2523 unsignedp0 = TREE_UNSIGNED (TREE_TYPE (op0));
2524 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
2525 unsignedp1 = TREE_UNSIGNED (TREE_TYPE (op1));
2527 /* If one of the operands must be floated, we cannot optimize. */
2528 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
2529 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
2531 /* If first arg is constant, swap the args (changing operation
2532 so value is preserved), for canonicalization. Don't do this if
2533 the second arg is 0. */
2535 if (TREE_CONSTANT (primop0)
2536 && ! integer_zerop (primop1) && ! real_zerop (primop1))
2538 register tree tem = primop0;
2539 register int temi = unsignedp0;
2547 unsignedp0 = unsignedp1;
2570 *rescode_ptr = code;
2573 /* If comparing an integer against a constant more bits wide,
2574 maybe we can deduce a value of 1 or 0 independent of the data.
2575 Or else truncate the constant now
2576 rather than extend the variable at run time.
2578 This is only interesting if the constant is the wider arg.
2579 Also, it is not safe if the constant is unsigned and the
2580 variable arg is signed, since in this case the variable
2581 would be sign-extended and then regarded as unsigned.
2582 Our technique fails in this case because the lowest/highest
2583 possible unsigned results don't follow naturally from the
2584 lowest/highest possible values of the variable operand.
2585 For just EQ_EXPR and NE_EXPR there is another technique that
2586 could be used: see if the constant can be faithfully represented
2587 in the other operand's type, by truncating it and reextending it
2588 and see if that preserves the constant's value. */
2590 if (!real1 && !real2
2591 && TREE_CODE (primop1) == INTEGER_CST
2592 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
2594 int min_gt, max_gt, min_lt, max_lt;
2595 tree maxval, minval;
2596 /* 1 if comparison is nominally unsigned. */
2597 int unsignedp = TREE_UNSIGNED (*restype_ptr);
2600 type = signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0));
2602 /* If TYPE is an enumeration, then we need to get its min/max
2603 values from it's underlying integral type, not the enumerated
2605 if (TREE_CODE (type) == ENUMERAL_TYPE)
2606 type = type_for_size (TYPE_PRECISION (type), unsignedp0);
2608 maxval = TYPE_MAX_VALUE (type);
2609 minval = TYPE_MIN_VALUE (type);
2611 if (unsignedp && !unsignedp0)
2612 *restype_ptr = signed_type (*restype_ptr);
2614 if (TREE_TYPE (primop1) != *restype_ptr)
2615 primop1 = convert (*restype_ptr, primop1);
2616 if (type != *restype_ptr)
2618 minval = convert (*restype_ptr, minval);
2619 maxval = convert (*restype_ptr, maxval);
2622 if (unsignedp && unsignedp0)
2624 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
2625 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
2626 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
2627 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
2631 min_gt = INT_CST_LT (primop1, minval);
2632 max_gt = INT_CST_LT (primop1, maxval);
2633 min_lt = INT_CST_LT (minval, primop1);
2634 max_lt = INT_CST_LT (maxval, primop1);
2638 /* This used to be a switch, but Genix compiler can't handle that. */
2639 if (code == NE_EXPR)
2641 if (max_lt || min_gt)
2642 val = boolean_true_node;
2644 else if (code == EQ_EXPR)
2646 if (max_lt || min_gt)
2647 val = boolean_false_node;
2649 else if (code == LT_EXPR)
2652 val = boolean_true_node;
2654 val = boolean_false_node;
2656 else if (code == GT_EXPR)
2659 val = boolean_true_node;
2661 val = boolean_false_node;
2663 else if (code == LE_EXPR)
2666 val = boolean_true_node;
2668 val = boolean_false_node;
2670 else if (code == GE_EXPR)
2673 val = boolean_true_node;
2675 val = boolean_false_node;
2678 /* If primop0 was sign-extended and unsigned comparison specd,
2679 we did a signed comparison above using the signed type bounds.
2680 But the comparison we output must be unsigned.
2682 Also, for inequalities, VAL is no good; but if the signed
2683 comparison had *any* fixed result, it follows that the
2684 unsigned comparison just tests the sign in reverse
2685 (positive values are LE, negative ones GE).
2686 So we can generate an unsigned comparison
2687 against an extreme value of the signed type. */
2689 if (unsignedp && !unsignedp0)
2696 primop1 = TYPE_MIN_VALUE (type);
2702 primop1 = TYPE_MAX_VALUE (type);
2709 type = unsigned_type (type);
2712 if (!max_gt && !unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
2714 /* This is the case of (char)x >?< 0x80, which people used to use
2715 expecting old C compilers to change the 0x80 into -0x80. */
2716 if (val == boolean_false_node)
2717 warning ("comparison is always false due to limited range of data type");
2718 if (val == boolean_true_node)
2719 warning ("comparison is always true due to limited range of data type");
2722 if (!min_lt && unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
2724 /* This is the case of (unsigned char)x >?< -1 or < 0. */
2725 if (val == boolean_false_node)
2726 warning ("comparison is always false due to limited range of data type");
2727 if (val == boolean_true_node)
2728 warning ("comparison is always true due to limited range of data type");
2733 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2734 if (TREE_SIDE_EFFECTS (primop0))
2735 return build (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
2739 /* Value is not predetermined, but do the comparison
2740 in the type of the operand that is not constant.
2741 TYPE is already properly set. */
2743 else if (real1 && real2
2744 && (TYPE_PRECISION (TREE_TYPE (primop0))
2745 == TYPE_PRECISION (TREE_TYPE (primop1))))
2746 type = TREE_TYPE (primop0);
2748 /* If args' natural types are both narrower than nominal type
2749 and both extend in the same manner, compare them
2750 in the type of the wider arg.
2751 Otherwise must actually extend both to the nominal
2752 common type lest different ways of extending
2754 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
2756 else if (unsignedp0 == unsignedp1 && real1 == real2
2757 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
2758 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
2760 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
2761 type = signed_or_unsigned_type (unsignedp0
2762 || TREE_UNSIGNED (*restype_ptr),
2764 /* Make sure shorter operand is extended the right way
2765 to match the longer operand. */
2766 primop0 = convert (signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0)),
2768 primop1 = convert (signed_or_unsigned_type (unsignedp1, TREE_TYPE (primop1)),
2773 /* Here we must do the comparison on the nominal type
2774 using the args exactly as we received them. */
2775 type = *restype_ptr;
2779 if (!real1 && !real2 && integer_zerop (primop1)
2780 && TREE_UNSIGNED (*restype_ptr))
2786 /* All unsigned values are >= 0, so we warn if extra warnings
2787 are requested. However, if OP0 is a constant that is
2788 >= 0, the signedness of the comparison isn't an issue,
2789 so suppress the warning. */
2791 && ! (TREE_CODE (primop0) == INTEGER_CST
2792 && ! TREE_OVERFLOW (convert (signed_type (type),
2794 warning ("comparison of unsigned expression >= 0 is always true");
2795 value = boolean_true_node;
2800 && ! (TREE_CODE (primop0) == INTEGER_CST
2801 && ! TREE_OVERFLOW (convert (signed_type (type),
2803 warning ("comparison of unsigned expression < 0 is always false");
2804 value = boolean_false_node;
2813 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2814 if (TREE_SIDE_EFFECTS (primop0))
2815 return build (COMPOUND_EXPR, TREE_TYPE (value),
2822 *op0_ptr = convert (type, primop0);
2823 *op1_ptr = convert (type, primop1);
2825 *restype_ptr = boolean_type_node;
2830 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
2831 or validate its data type for an `if' or `while' statement or ?..: exp.
2833 This preparation consists of taking the ordinary
2834 representation of an expression expr and producing a valid tree
2835 boolean expression describing whether expr is nonzero. We could
2836 simply always do build_binary_op (NE_EXPR, expr, boolean_false_node, 1),
2837 but we optimize comparisons, &&, ||, and !.
2839 The resulting type should always be `boolean_type_node'. */
2842 truthvalue_conversion (expr)
2845 if (TREE_CODE (expr) == ERROR_MARK)
2848 #if 0 /* This appears to be wrong for C++. */
2849 /* These really should return error_mark_node after 2.4 is stable.
2850 But not all callers handle ERROR_MARK properly. */
2851 switch (TREE_CODE (TREE_TYPE (expr)))
2854 error ("struct type value used where scalar is required");
2855 return boolean_false_node;
2858 error ("union type value used where scalar is required");
2859 return boolean_false_node;
2862 error ("array type value used where scalar is required");
2863 return boolean_false_node;
2870 switch (TREE_CODE (expr))
2873 case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
2874 case TRUTH_ANDIF_EXPR:
2875 case TRUTH_ORIF_EXPR:
2876 case TRUTH_AND_EXPR:
2878 case TRUTH_XOR_EXPR:
2879 case TRUTH_NOT_EXPR:
2880 TREE_TYPE (expr) = boolean_type_node;
2887 return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
2890 return real_zerop (expr) ? boolean_false_node : boolean_true_node;
2893 /* If we are taking the address of a external decl, it might be zero
2894 if it is weak, so we cannot optimize. */
2895 if (DECL_P (TREE_OPERAND (expr, 0))
2896 && DECL_EXTERNAL (TREE_OPERAND (expr, 0)))
2899 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0)))
2900 return build (COMPOUND_EXPR, boolean_type_node,
2901 TREE_OPERAND (expr, 0), boolean_true_node);
2903 return boolean_true_node;
2906 return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
2907 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
2908 truthvalue_conversion (TREE_OPERAND (expr, 0)),
2909 truthvalue_conversion (TREE_OPERAND (expr, 1)),
2916 /* These don't change whether an object is non-zero or zero. */
2917 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2921 /* These don't change whether an object is zero or non-zero, but
2922 we can't ignore them if their second arg has side-effects. */
2923 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
2924 return build (COMPOUND_EXPR, boolean_type_node, TREE_OPERAND (expr, 1),
2925 truthvalue_conversion (TREE_OPERAND (expr, 0)));
2927 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2930 /* Distribute the conversion into the arms of a COND_EXPR. */
2931 return fold (build (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
2932 truthvalue_conversion (TREE_OPERAND (expr, 1)),
2933 truthvalue_conversion (TREE_OPERAND (expr, 2))));
2936 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
2937 since that affects how `default_conversion' will behave. */
2938 if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE
2939 || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
2941 /* fall through... */
2943 /* If this is widening the argument, we can ignore it. */
2944 if (TYPE_PRECISION (TREE_TYPE (expr))
2945 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
2946 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2950 /* With IEEE arithmetic, x - x may not equal 0, so we can't optimize
2952 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
2953 && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE)
2955 /* fall through... */
2957 /* This and MINUS_EXPR can be changed into a comparison of the
2959 if (TREE_TYPE (TREE_OPERAND (expr, 0))
2960 == TREE_TYPE (TREE_OPERAND (expr, 1)))
2961 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2962 TREE_OPERAND (expr, 1), 1);
2963 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2964 fold (build1 (NOP_EXPR,
2965 TREE_TYPE (TREE_OPERAND (expr, 0)),
2966 TREE_OPERAND (expr, 1))), 1);
2969 if (integer_onep (TREE_OPERAND (expr, 1))
2970 && TREE_TYPE (expr) != boolean_type_node)
2971 /* Using convert here would cause infinite recursion. */
2972 return build1 (NOP_EXPR, boolean_type_node, expr);
2976 if (warn_parentheses && C_EXP_ORIGINAL_CODE (expr) == MODIFY_EXPR)
2977 warning ("suggest parentheses around assignment used as truth value");
2984 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
2986 tree tem = save_expr (expr);
2987 return (build_binary_op
2988 ((TREE_SIDE_EFFECTS (expr)
2989 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
2990 truthvalue_conversion (build_unary_op (REALPART_EXPR, tem, 0)),
2991 truthvalue_conversion (build_unary_op (IMAGPART_EXPR, tem, 0)),
2995 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
2999 /* Read the rest of a #-directive from input stream FINPUT.
3000 In normal use, the directive name and the white space after it
3001 have already been read, so they won't be included in the result.
3002 We allow for the fact that the directive line may contain
3003 a newline embedded within a character or string literal which forms
3004 a part of the directive.
3006 The value is a string in a reusable buffer. It remains valid
3007 only until the next time this function is called. */
3008 unsigned char *yy_cur, *yy_lim;
3010 #define GETC() (yy_cur < yy_lim ? *yy_cur++ : yy_get_token ())
3011 #define UNGETC(c) ((c) == EOF ? 0 : yy_cur--)
3018 parse_in.limit = parse_in.token_buffer;
3019 cpp_token = cpp_get_token (&parse_in);
3020 if (cpp_token == CPP_EOF)
3022 yy_lim = CPP_PWRITTEN (&parse_in);
3023 yy_cur = parse_in.token_buffer;
3024 if (yy_cur < yy_lim)
3030 get_directive_line ()
3032 static char *directive_buffer = NULL;
3033 static unsigned buffer_length = 0;
3035 register char *buffer_limit;
3036 register int looking_for = 0;
3037 register int char_escaped = 0;
3039 if (buffer_length == 0)
3041 directive_buffer = (char *)xmalloc (128);
3042 buffer_length = 128;
3045 buffer_limit = &directive_buffer[buffer_length];
3047 for (p = directive_buffer; ; )
3051 /* Make buffer bigger if it is full. */
3052 if (p >= buffer_limit)
3054 register unsigned bytes_used = (p - directive_buffer);
3058 = (char *)xrealloc (directive_buffer, buffer_length);
3059 p = &directive_buffer[bytes_used];
3060 buffer_limit = &directive_buffer[buffer_length];
3065 /* Discard initial whitespace. */
3066 if ((c == ' ' || c == '\t') && p == directive_buffer)
3069 /* Detect the end of the directive. */
3070 if (c == '\n' && looking_for == 0)
3079 return directive_buffer;
3081 /* Handle string and character constant syntax. */
3084 if (looking_for == c && !char_escaped)
3085 looking_for = 0; /* Found terminator... stop looking. */
3088 if (c == '\'' || c == '"')
3089 looking_for = c; /* Don't stop buffering until we see another
3090 another one of these (or an EOF). */
3092 /* Handle backslash. */
3093 char_escaped = (c == '\\' && ! char_escaped);
3097 /* Read the rest of a #-directive from input stream FINPUT.
3098 In normal use, the directive name and the white space after it
3099 have already been read, so they won't be included in the result.
3100 We allow for the fact that the directive line may contain
3101 a newline embedded within a character or string literal which forms
3102 a part of the directive.
3104 The value is a string in a reusable buffer. It remains valid
3105 only until the next time this function is called.
3107 The terminating character ('\n' or EOF) is left in FINPUT for the
3108 caller to re-read. */
3111 get_directive_line (finput)
3112 register FILE *finput;
3114 static char *directive_buffer = NULL;
3115 static unsigned buffer_length = 0;
3117 register char *buffer_limit;
3118 register int looking_for = 0;
3119 register int char_escaped = 0;
3121 if (buffer_length == 0)
3123 directive_buffer = (char *)xmalloc (128);
3124 buffer_length = 128;
3127 buffer_limit = &directive_buffer[buffer_length];
3129 for (p = directive_buffer; ; )
3133 /* Make buffer bigger if it is full. */
3134 if (p >= buffer_limit)
3136 register unsigned bytes_used = (p - directive_buffer);
3140 = (char *)xrealloc (directive_buffer, buffer_length);
3141 p = &directive_buffer[bytes_used];
3142 buffer_limit = &directive_buffer[buffer_length];
3147 /* Discard initial whitespace. */
3148 if ((c == ' ' || c == '\t') && p == directive_buffer)
3151 /* Detect the end of the directive. */
3152 if (looking_for == 0
3153 && (c == '\n' || c == EOF))
3162 return directive_buffer;
3164 /* Handle string and character constant syntax. */
3167 if (looking_for == c && !char_escaped)
3168 looking_for = 0; /* Found terminator... stop looking. */
3171 if (c == '\'' || c == '"')
3172 looking_for = c; /* Don't stop buffering until we see another
3173 one of these (or an EOF). */
3175 /* Handle backslash. */
3176 char_escaped = (c == '\\' && ! char_escaped);
3179 #endif /* !USE_CPPLIB */
3181 /* Make a variant type in the proper way for C/C++, propagating qualifiers
3182 down to the element type of an array. */
3185 c_build_qualified_type (type, type_quals)
3189 /* A restrict-qualified pointer type must be a pointer to object or
3190 incomplete type. Note that the use of POINTER_TYPE_P also allows
3191 REFERENCE_TYPEs, which is appropriate for C++. Unfortunately,
3192 the C++ front-end also use POINTER_TYPE for pointer-to-member
3193 values, so even though it should be illegal to use `restrict'
3194 with such an entity we don't flag that here. Thus, special case
3195 code for that case is required in the C++ front-end. */
3196 if ((type_quals & TYPE_QUAL_RESTRICT)
3197 && (!POINTER_TYPE_P (type)
3198 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
3200 error ("invalid use of `restrict'");
3201 type_quals &= ~TYPE_QUAL_RESTRICT;
3204 if (TREE_CODE (type) == ARRAY_TYPE)
3205 return build_array_type (c_build_qualified_type (TREE_TYPE (type),
3207 TYPE_DOMAIN (type));
3208 return build_qualified_type (type, type_quals);
3211 /* Apply the TYPE_QUALS to the new DECL. */
3214 c_apply_type_quals_to_decl (type_quals, decl)
3218 if (type_quals & TYPE_QUAL_CONST)
3219 TREE_READONLY (decl) = 1;
3220 if (type_quals & TYPE_QUAL_VOLATILE)
3222 TREE_SIDE_EFFECTS (decl) = 1;
3223 TREE_THIS_VOLATILE (decl) = 1;
3225 if (type_quals & TYPE_QUAL_RESTRICT)
3227 if (!TREE_TYPE (decl)
3228 || !POINTER_TYPE_P (TREE_TYPE (decl))
3229 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (TREE_TYPE (decl))))
3230 error ("invalid use of `restrict'");
3231 else if (flag_strict_aliasing)
3233 /* No two restricted pointers can point at the same thing.
3234 However, a restricted pointer can point at the same thing
3235 as an unrestricted pointer, if that unrestricted pointer
3236 is based on the restricted pointer. So, we make the
3237 alias set for the restricted pointer a subset of the
3238 alias set for the type pointed to by the type of the
3241 int pointed_to_alias_set
3242 = get_alias_set (TREE_TYPE (TREE_TYPE (decl)));
3244 if (!pointed_to_alias_set)
3245 /* It's not legal to make a subset of alias set zero. */
3249 DECL_POINTER_ALIAS_SET (decl) = new_alias_set ();
3250 record_alias_subset (pointed_to_alias_set,
3251 DECL_POINTER_ALIAS_SET (decl));
3257 /* T is an expression with pointer type. Find the DECL on which this
3258 expression is based. (For example, in `a[i]' this would be `a'.)
3259 If there is no such DECL, or a unique decl cannot be determined,
3260 NULL_TREE is retured. */
3263 c_find_base_decl (t)
3269 if (t == NULL_TREE || t == error_mark_node)
3272 if (!POINTER_TYPE_P (TREE_TYPE (t)))
3277 if (TREE_CODE (t) == FIELD_DECL
3278 || TREE_CODE (t) == PARM_DECL
3279 || TREE_CODE (t) == VAR_DECL)
3280 /* Aha, we found a pointer-typed declaration. */
3283 /* It would be nice to deal with COMPONENT_REFs here. If we could
3284 tell that `a' and `b' were the same, then `a->f' and `b->f' are
3287 /* Handle general expressions. */
3288 switch (TREE_CODE_CLASS (TREE_CODE (t)))
3293 for (i = tree_code_length [(int) TREE_CODE (t)]; --i >= 0;)
3295 tree d = c_find_base_decl (TREE_OPERAND (t, i));
3300 else if (d && d != decl)
3301 /* Two different declarations. That's confusing; let's
3302 just assume we don't know what's going on. */
3315 /* Return the typed-based alias set for T, which may be an expression
3325 if (t == error_mark_node)
3328 type = (TYPE_P (t)) ? t : TREE_TYPE (t);
3330 if (type == error_mark_node)
3333 /* Deal with special cases first; for certain kinds of references
3334 we're interested in more than just the type. */
3336 if (TREE_CODE (t) == BIT_FIELD_REF)
3337 /* Perhaps reads and writes to this piece of data alias fields
3338 neighboring the bitfield. Perhaps that's impossible. For now,
3339 let's just assume that bitfields can alias everything, which is
3340 the conservative assumption. */
3343 /* Permit type-punning when accessing a union, provided the access
3344 is directly through the union. For example, this code does not
3345 permit taking the address of a union member and then storing
3346 through it. Even the type-punning allowed here is a GCC
3347 extension, albeit a common and useful one; the C standard says
3348 that such accesses have implementation-defined behavior. */
3350 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
3351 u = TREE_OPERAND (u, 0))
3352 if (TREE_CODE (u) == COMPONENT_REF
3353 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
3356 if (TREE_CODE (t) == INDIRECT_REF)
3358 /* Check for accesses through restrict-qualified pointers. */
3359 tree decl = c_find_base_decl (TREE_OPERAND (t, 0));
3361 if (decl && DECL_POINTER_ALIAS_SET_KNOWN_P (decl))
3362 /* We use the alias set indicated in the declaration. */
3363 return DECL_POINTER_ALIAS_SET (decl);
3366 /* From here on, only the type matters. */
3368 if (TREE_CODE (t) == COMPONENT_REF
3369 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (t, 1)))
3370 /* Since build_modify_expr calls get_unwidened for stores to
3371 component references, the type of a bit field can be changed
3372 from (say) `unsigned int : 16' to `unsigned short' or from
3373 `enum E : 16' to `short'. We want the real type of the
3374 bit-field in this case, not some the integral equivalent. */
3375 type = DECL_BIT_FIELD_TYPE (TREE_OPERAND (t, 1));
3377 if (TYPE_ALIAS_SET_KNOWN_P (type))
3378 /* If we've already calculated the value, just return it. */
3379 return TYPE_ALIAS_SET (type);
3380 else if (TYPE_MAIN_VARIANT (type) != type)
3381 /* The C standard specifically allows aliasing between
3382 cv-qualified variants of types. */
3383 TYPE_ALIAS_SET (type) = c_get_alias_set (TYPE_MAIN_VARIANT (type));
3384 else if (TREE_CODE (type) == INTEGER_TYPE)
3386 tree signed_variant;
3388 /* The C standard specifically allows aliasing between signed and
3389 unsigned variants of the same type. We treat the signed
3390 variant as canonical. */
3391 signed_variant = signed_type (type);
3393 if (signed_variant != type)
3394 TYPE_ALIAS_SET (type) = c_get_alias_set (signed_variant);
3395 else if (signed_variant == signed_char_type_node)
3396 /* The C standard guarantess that any object may be accessed
3397 via an lvalue that has character type. We don't have to
3398 check for unsigned_char_type_node or char_type_node because
3399 we are specifically looking at the signed variant. */
3400 TYPE_ALIAS_SET (type) = 0;
3402 else if (TREE_CODE (type) == ARRAY_TYPE)
3403 /* Anything that can alias one of the array elements can alias
3404 the entire array as well. */
3405 TYPE_ALIAS_SET (type) = c_get_alias_set (TREE_TYPE (type));
3406 else if (TREE_CODE (type) == FUNCTION_TYPE)
3407 /* There are no objects of FUNCTION_TYPE, so there's no point in
3408 using up an alias set for them. (There are, of course,
3409 pointers and references to functions, but that's
3411 TYPE_ALIAS_SET (type) = 0;
3412 else if (TREE_CODE (type) == RECORD_TYPE
3413 || TREE_CODE (type) == UNION_TYPE)
3414 /* If TYPE is a struct or union type then we're reading or
3415 writing an entire struct. Thus, we don't know anything about
3416 aliasing. (In theory, such an access can only alias objects
3417 whose type is the same as one of the fields, recursively, but
3418 we don't yet make any use of that information.) */
3419 TYPE_ALIAS_SET (type) = 0;
3420 else if (TREE_CODE (type) == POINTER_TYPE
3421 || TREE_CODE (type) == REFERENCE_TYPE)
3425 /* Unfortunately, there is no canonical form of a pointer type.
3426 In particular, if we have `typedef int I', then `int *', and
3427 `I *' are different types. So, we have to pick a canonical
3428 representative. We do this below.
3430 Technically, this approach is actually more conservative that
3431 it needs to be. In particular, `const int *' and `int *'
3432 chould be in different alias sets, according to the C and C++
3433 standard, since their types are not the same, and so,
3434 technically, an `int **' and `const int **' cannot point at
3437 But, the standard is wrong. In particular, this code is
3442 const int* const* cipp = &ip;
3444 And, it doesn't make sense for that to be legal unless you
3445 can dereference IPP and CIPP. So, we ignore cv-qualifiers on
3446 the pointed-to types. This issue has been reported to the
3448 t = TYPE_MAIN_VARIANT (TREE_TYPE (type));
3449 t = ((TREE_CODE (type) == POINTER_TYPE)
3450 ? build_pointer_type (t) : build_reference_type (t));
3452 TYPE_ALIAS_SET (type) = c_get_alias_set (t);
3455 if (!TYPE_ALIAS_SET_KNOWN_P (type))
3456 /* TYPE is something we haven't seen before. Put it in a new
3458 TYPE_ALIAS_SET (type) = new_alias_set ();
3460 return TYPE_ALIAS_SET (type);
3463 /* Build tree nodes and builtin functions common to both C and C++ language
3465 CPLUS_MODE is nonzero if we are called from the C++ frontend, we generate
3466 some stricter prototypes in that case.
3467 NO_BUILTINS and NO_NONANSI_BUILTINS contain the respective values of
3468 the language frontend flags flag_no_builtin and
3469 flag_no_nonansi_builtin. */
3471 c_common_nodes_and_builtins (cplus_mode, no_builtins, no_nonansi_builtins)
3472 int cplus_mode, no_builtins, no_nonansi_builtins;
3475 tree memcpy_ftype, memset_ftype, strlen_ftype;
3476 tree bzero_ftype, bcmp_ftype;
3477 tree endlink, int_endlink, double_endlink, unsigned_endlink;
3478 tree sizetype_endlink;
3479 tree ptr_ftype, ptr_ftype_unsigned;
3480 tree void_ftype_any, void_ftype_int, int_ftype_any;
3481 tree double_ftype_double, double_ftype_double_double;
3482 tree float_ftype_float, ldouble_ftype_ldouble;
3483 tree int_ftype_cptr_cptr_sizet;
3484 tree int_ftype_string_string, string_ftype_ptr_ptr;
3485 tree long_ftype_long;
3486 /* Either char* or void*. */
3487 tree traditional_ptr_type_node;
3488 /* Either const char* or const void*. */
3489 tree traditional_cptr_type_node;
3490 tree traditional_len_type_node;
3491 tree traditional_len_endlink;
3492 tree va_list_ref_type_node;
3493 tree va_list_arg_type_node;
3495 pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_va_list"),
3496 va_list_type_node));
3498 pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_ptrdiff_t"),
3499 ptrdiff_type_node));
3501 pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_size_t"),
3504 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
3506 va_list_arg_type_node = va_list_ref_type_node =
3507 build_pointer_type (TREE_TYPE (va_list_type_node));
3511 va_list_arg_type_node = va_list_type_node;
3512 va_list_ref_type_node = build_reference_type (va_list_type_node);
3515 endlink = void_list_node;
3516 int_endlink = tree_cons (NULL_TREE, integer_type_node, endlink);
3517 double_endlink = tree_cons (NULL_TREE, double_type_node, endlink);
3518 unsigned_endlink = tree_cons (NULL_TREE, unsigned_type_node, endlink);
3520 ptr_ftype = build_function_type (ptr_type_node, NULL_TREE);
3521 ptr_ftype_unsigned = build_function_type (ptr_type_node, unsigned_endlink);
3522 sizetype_endlink = tree_cons (NULL_TREE, TYPE_DOMAIN (sizetype), endlink);
3523 /* We realloc here because sizetype could be int or unsigned. S'ok. */
3524 ptr_ftype_sizetype = build_function_type (ptr_type_node, sizetype_endlink);
3526 int_ftype_any = build_function_type (integer_type_node, NULL_TREE);
3527 void_ftype_any = build_function_type (void_type_node, NULL_TREE);
3528 void_ftype = build_function_type (void_type_node, endlink);
3529 void_ftype_int = build_function_type (void_type_node, int_endlink);
3531 = build_function_type (void_type_node,
3532 tree_cons (NULL_TREE, ptr_type_node, endlink));
3535 = build_function_type (float_type_node,
3536 tree_cons (NULL_TREE, float_type_node, endlink));
3539 = build_function_type (double_type_node, double_endlink);
3541 ldouble_ftype_ldouble
3542 = build_function_type (long_double_type_node,
3543 tree_cons (NULL_TREE, long_double_type_node,
3546 double_ftype_double_double
3547 = build_function_type (double_type_node,
3548 tree_cons (NULL_TREE, double_type_node,
3552 = build_function_type (integer_type_node, int_endlink);
3555 = build_function_type (long_integer_type_node,
3556 tree_cons (NULL_TREE, long_integer_type_node,
3559 int_ftype_cptr_cptr_sizet
3560 = build_function_type (integer_type_node,
3561 tree_cons (NULL_TREE, const_ptr_type_node,
3562 tree_cons (NULL_TREE, const_ptr_type_node,
3563 tree_cons (NULL_TREE,
3567 /* Prototype for strcpy. */
3568 string_ftype_ptr_ptr
3569 = build_function_type (string_type_node,
3570 tree_cons (NULL_TREE, string_type_node,
3571 tree_cons (NULL_TREE,
3572 const_string_type_node,
3575 traditional_len_type_node = (flag_traditional && ! cplus_mode
3576 ? integer_type_node : sizetype);
3577 traditional_len_endlink = tree_cons (NULL_TREE, traditional_len_type_node,
3580 /* Prototype for strcmp. */
3581 int_ftype_string_string
3582 = build_function_type (integer_type_node,
3583 tree_cons (NULL_TREE, const_string_type_node,
3584 tree_cons (NULL_TREE,
3585 const_string_type_node,
3588 /* Prototype for strlen. */
3590 = build_function_type (traditional_len_type_node,
3591 tree_cons (NULL_TREE, const_string_type_node,
3594 traditional_ptr_type_node = (flag_traditional && ! cplus_mode
3595 ? string_type_node : ptr_type_node);
3596 traditional_cptr_type_node = (flag_traditional && ! cplus_mode
3597 ? const_string_type_node : const_ptr_type_node);
3599 /* Prototype for memcpy. */
3601 = build_function_type (traditional_ptr_type_node,
3602 tree_cons (NULL_TREE, ptr_type_node,
3603 tree_cons (NULL_TREE, const_ptr_type_node,
3604 sizetype_endlink)));
3606 /* Prototype for memset. */
3608 = build_function_type (traditional_ptr_type_node,
3609 tree_cons (NULL_TREE, ptr_type_node,
3610 tree_cons (NULL_TREE, integer_type_node,
3611 tree_cons (NULL_TREE,
3615 /* Prototype for bzero. */
3617 = build_function_type (void_type_node,
3618 tree_cons (NULL_TREE, traditional_ptr_type_node,
3619 traditional_len_endlink));
3621 /* Prototype for bcmp. */
3623 = build_function_type (integer_type_node,
3624 tree_cons (NULL_TREE, traditional_cptr_type_node,
3625 tree_cons (NULL_TREE,
3626 traditional_cptr_type_node,
3627 traditional_len_endlink)));
3629 builtin_function ("__builtin_constant_p", default_function_type,
3630 BUILT_IN_CONSTANT_P, BUILT_IN_NORMAL, NULL_PTR);
3632 builtin_function ("__builtin_return_address", ptr_ftype_unsigned,
3633 BUILT_IN_RETURN_ADDRESS, BUILT_IN_NORMAL, NULL_PTR);
3635 builtin_function ("__builtin_frame_address", ptr_ftype_unsigned,
3636 BUILT_IN_FRAME_ADDRESS, BUILT_IN_NORMAL, NULL_PTR);
3638 builtin_function ("__builtin_alloca", ptr_ftype_sizetype,
3639 BUILT_IN_ALLOCA, BUILT_IN_NORMAL, "alloca");
3640 builtin_function ("__builtin_ffs", int_ftype_int, BUILT_IN_FFS,
3641 BUILT_IN_NORMAL, NULL_PTR);
3642 /* Define alloca, ffs as builtins.
3643 Declare _exit just to mark it as volatile. */
3644 if (! no_builtins && ! no_nonansi_builtins)
3647 temp = builtin_function ("alloca", ptr_ftype_sizetype,
3648 BUILT_IN_ALLOCA, BUILT_IN_NORMAL, NULL_PTR);
3649 /* Suppress error if redefined as a non-function. */
3650 DECL_BUILT_IN_NONANSI (temp) = 1;
3652 temp = builtin_function ("ffs", int_ftype_int, BUILT_IN_FFS,
3653 BUILT_IN_NORMAL, NULL_PTR);
3654 /* Suppress error if redefined as a non-function. */
3655 DECL_BUILT_IN_NONANSI (temp) = 1;
3656 temp = builtin_function ("_exit", void_ftype_int,
3657 0, NOT_BUILT_IN, NULL_PTR);
3658 TREE_THIS_VOLATILE (temp) = 1;
3659 TREE_SIDE_EFFECTS (temp) = 1;
3660 /* Suppress error if redefined as a non-function. */
3661 DECL_BUILT_IN_NONANSI (temp) = 1;
3663 /* The system prototypes for these functions have many
3664 variations, so don't specify parameters to avoid conflicts.
3665 The expand_* functions check the argument types anyway. */
3666 temp = builtin_function ("bzero", void_ftype_any,
3667 BUILT_IN_BZERO, BUILT_IN_NORMAL, NULL_PTR);
3668 DECL_BUILT_IN_NONANSI (temp) = 1;
3669 temp = builtin_function ("bcmp", int_ftype_any,
3670 BUILT_IN_BCMP, BUILT_IN_NORMAL, NULL_PTR);
3671 DECL_BUILT_IN_NONANSI (temp) = 1;
3674 builtin_function ("__builtin_abs", int_ftype_int, BUILT_IN_ABS,
3675 BUILT_IN_NORMAL, NULL_PTR);
3676 builtin_function ("__builtin_fabsf", float_ftype_float, BUILT_IN_FABS,
3677 BUILT_IN_NORMAL, NULL_PTR);
3678 builtin_function ("__builtin_fabs", double_ftype_double, BUILT_IN_FABS,
3679 BUILT_IN_NORMAL, NULL_PTR);
3680 builtin_function ("__builtin_fabsl", ldouble_ftype_ldouble, BUILT_IN_FABS,
3681 BUILT_IN_NORMAL, NULL_PTR);
3682 builtin_function ("__builtin_labs", long_ftype_long, BUILT_IN_LABS,
3683 BUILT_IN_NORMAL, NULL_PTR);
3684 builtin_function ("__builtin_saveregs", ptr_ftype, BUILT_IN_SAVEREGS,
3685 BUILT_IN_NORMAL, NULL_PTR);
3686 builtin_function ("__builtin_classify_type", default_function_type,
3687 BUILT_IN_CLASSIFY_TYPE, BUILT_IN_NORMAL, NULL_PTR);
3688 builtin_function ("__builtin_next_arg", ptr_ftype, BUILT_IN_NEXT_ARG,
3689 BUILT_IN_NORMAL, NULL_PTR);
3690 builtin_function ("__builtin_args_info", int_ftype_int, BUILT_IN_ARGS_INFO,
3691 BUILT_IN_NORMAL, NULL_PTR);
3692 builtin_function ("__builtin_setjmp",
3693 build_function_type (integer_type_node,
3694 tree_cons (NULL_TREE, ptr_type_node,
3696 BUILT_IN_SETJMP, BUILT_IN_NORMAL, NULL_PTR);
3697 builtin_function ("__builtin_longjmp",
3698 build_function_type (void_type_node,
3699 tree_cons (NULL_TREE, ptr_type_node,
3700 tree_cons (NULL_TREE,
3703 BUILT_IN_LONGJMP, BUILT_IN_NORMAL, NULL_PTR);
3704 builtin_function ("__builtin_trap", void_ftype, BUILT_IN_TRAP,
3705 BUILT_IN_NORMAL, NULL_PTR);
3707 /* ISO C99 IEEE Unordered compares. */
3708 builtin_function ("__builtin_isgreater", default_function_type,
3709 BUILT_IN_ISGREATER, BUILT_IN_NORMAL, NULL_PTR);
3710 builtin_function ("__builtin_isgreaterequal", default_function_type,
3711 BUILT_IN_ISGREATEREQUAL, BUILT_IN_NORMAL, NULL_PTR);
3712 builtin_function ("__builtin_isless", default_function_type,
3713 BUILT_IN_ISLESS, BUILT_IN_NORMAL, NULL_PTR);
3714 builtin_function ("__builtin_islessequal", default_function_type,
3715 BUILT_IN_ISLESSEQUAL, BUILT_IN_NORMAL, NULL_PTR);
3716 builtin_function ("__builtin_islessgreater", default_function_type,
3717 BUILT_IN_ISLESSGREATER, BUILT_IN_NORMAL, NULL_PTR);
3718 builtin_function ("__builtin_isunordered", default_function_type,
3719 BUILT_IN_ISUNORDERED, BUILT_IN_NORMAL, NULL_PTR);
3721 /* Untyped call and return. */
3722 builtin_function ("__builtin_apply_args", ptr_ftype,
3723 BUILT_IN_APPLY_ARGS, BUILT_IN_NORMAL, NULL_PTR);
3725 temp = tree_cons (NULL_TREE,
3726 build_pointer_type (build_function_type (void_type_node,
3728 tree_cons (NULL_TREE,
3730 tree_cons (NULL_TREE,
3733 builtin_function ("__builtin_apply",
3734 build_function_type (ptr_type_node, temp),
3735 BUILT_IN_APPLY, BUILT_IN_NORMAL, NULL_PTR);
3736 builtin_function ("__builtin_return", void_ftype_ptr,
3737 BUILT_IN_RETURN, BUILT_IN_NORMAL, NULL_PTR);
3739 /* Support for varargs.h and stdarg.h. */
3740 builtin_function ("__builtin_varargs_start",
3741 build_function_type (void_type_node,
3742 tree_cons (NULL_TREE,
3743 va_list_ref_type_node,
3745 BUILT_IN_VARARGS_START, BUILT_IN_NORMAL, NULL_PTR);
3747 builtin_function ("__builtin_stdarg_start",
3748 build_function_type (void_type_node,
3749 tree_cons (NULL_TREE,
3750 va_list_ref_type_node,
3752 BUILT_IN_STDARG_START, BUILT_IN_NORMAL, NULL_PTR);
3754 builtin_function ("__builtin_va_end",
3755 build_function_type (void_type_node,
3756 tree_cons (NULL_TREE,
3757 va_list_ref_type_node,
3759 BUILT_IN_VA_END, BUILT_IN_NORMAL, NULL_PTR);
3761 builtin_function ("__builtin_va_copy",
3762 build_function_type (void_type_node,
3763 tree_cons (NULL_TREE,
3764 va_list_ref_type_node,
3765 tree_cons (NULL_TREE,
3766 va_list_arg_type_node,
3768 BUILT_IN_VA_COPY, BUILT_IN_NORMAL, NULL_PTR);
3770 /* Currently under experimentation. */
3771 builtin_function ("__builtin_memcpy", memcpy_ftype, BUILT_IN_MEMCPY,
3772 BUILT_IN_NORMAL, "memcpy");
3773 builtin_function ("__builtin_memcmp", int_ftype_cptr_cptr_sizet,
3774 BUILT_IN_MEMCMP, BUILT_IN_NORMAL, "memcmp");
3775 builtin_function ("__builtin_memset", memset_ftype,
3776 BUILT_IN_MEMSET, BUILT_IN_NORMAL, "memset");
3777 builtin_function ("__builtin_bzero", bzero_ftype,
3778 BUILT_IN_BZERO, BUILT_IN_NORMAL, "bzero");
3779 builtin_function ("__builtin_bcmp", bcmp_ftype,
3780 BUILT_IN_BCMP, BUILT_IN_NORMAL, "bcmp");
3781 builtin_function ("__builtin_strcmp", int_ftype_string_string,
3782 BUILT_IN_STRCMP, BUILT_IN_NORMAL, "strcmp");
3783 builtin_function ("__builtin_strcpy", string_ftype_ptr_ptr,
3784 BUILT_IN_STRCPY, BUILT_IN_NORMAL, "strcpy");
3785 builtin_function ("__builtin_strlen", strlen_ftype,
3786 BUILT_IN_STRLEN, BUILT_IN_NORMAL, "strlen");
3787 builtin_function ("__builtin_sqrtf", float_ftype_float,
3788 BUILT_IN_FSQRT, BUILT_IN_NORMAL, "sqrtf");
3789 builtin_function ("__builtin_fsqrt", double_ftype_double,
3790 BUILT_IN_FSQRT, BUILT_IN_NORMAL, "sqrt");
3791 builtin_function ("__builtin_sqrtl", ldouble_ftype_ldouble,
3792 BUILT_IN_FSQRT, BUILT_IN_NORMAL, "sqrtl");
3793 builtin_function ("__builtin_sinf", float_ftype_float,
3794 BUILT_IN_SIN, BUILT_IN_NORMAL, "sinf");
3795 builtin_function ("__builtin_sin", double_ftype_double,
3796 BUILT_IN_SIN, BUILT_IN_NORMAL, "sin");
3797 builtin_function ("__builtin_sinl", ldouble_ftype_ldouble,
3798 BUILT_IN_SIN, BUILT_IN_NORMAL, "sinl");
3799 builtin_function ("__builtin_cosf", float_ftype_float,
3800 BUILT_IN_COS, BUILT_IN_NORMAL, "cosf");
3801 builtin_function ("__builtin_cos", double_ftype_double,
3802 BUILT_IN_COS, BUILT_IN_NORMAL, "cos");
3803 builtin_function ("__builtin_cosl", ldouble_ftype_ldouble,
3804 BUILT_IN_COS, BUILT_IN_NORMAL, "cosl");
3808 builtin_function ("abs", int_ftype_int, BUILT_IN_ABS,
3809 BUILT_IN_NORMAL, NULL_PTR);
3810 builtin_function ("fabsf", float_ftype_float, BUILT_IN_FABS,
3811 BUILT_IN_NORMAL, NULL_PTR);
3812 builtin_function ("fabs", double_ftype_double, BUILT_IN_FABS,
3813 BUILT_IN_NORMAL, NULL_PTR);
3814 builtin_function ("fabsl", ldouble_ftype_ldouble, BUILT_IN_FABS,
3815 BUILT_IN_NORMAL, NULL_PTR);
3816 builtin_function ("labs", long_ftype_long, BUILT_IN_LABS,
3817 BUILT_IN_NORMAL, NULL_PTR);
3818 builtin_function ("memcpy", memcpy_ftype, BUILT_IN_MEMCPY,
3819 BUILT_IN_NORMAL, NULL_PTR);
3820 builtin_function ("memcmp", int_ftype_cptr_cptr_sizet, BUILT_IN_MEMCMP,
3821 BUILT_IN_NORMAL, NULL_PTR);
3822 builtin_function ("memset", memset_ftype, BUILT_IN_MEMSET,
3823 BUILT_IN_NORMAL, NULL_PTR);
3824 builtin_function ("strcmp", int_ftype_string_string, BUILT_IN_STRCMP,
3825 BUILT_IN_NORMAL, NULL_PTR);
3826 builtin_function ("strcpy", string_ftype_ptr_ptr, BUILT_IN_STRCPY,
3827 BUILT_IN_NORMAL, NULL_PTR);
3828 builtin_function ("strlen", strlen_ftype, BUILT_IN_STRLEN,
3829 BUILT_IN_NORMAL, NULL_PTR);
3830 builtin_function ("sqrtf", float_ftype_float, BUILT_IN_FSQRT,
3831 BUILT_IN_NORMAL, NULL_PTR);
3832 builtin_function ("sqrt", double_ftype_double, BUILT_IN_FSQRT,
3833 BUILT_IN_NORMAL, NULL_PTR);
3834 builtin_function ("sqrtl", ldouble_ftype_ldouble, BUILT_IN_FSQRT,
3835 BUILT_IN_NORMAL, NULL_PTR);
3836 builtin_function ("sinf", float_ftype_float, BUILT_IN_SIN,
3837 BUILT_IN_NORMAL, NULL_PTR);
3838 builtin_function ("sin", double_ftype_double, BUILT_IN_SIN,
3839 BUILT_IN_NORMAL, NULL_PTR);
3840 builtin_function ("sinl", ldouble_ftype_ldouble, BUILT_IN_SIN,
3841 BUILT_IN_NORMAL, NULL_PTR);
3842 builtin_function ("cosf", float_ftype_float, BUILT_IN_COS,
3843 BUILT_IN_NORMAL, NULL_PTR);
3844 builtin_function ("cos", double_ftype_double, BUILT_IN_COS,
3845 BUILT_IN_NORMAL, NULL_PTR);
3846 builtin_function ("cosl", ldouble_ftype_ldouble, BUILT_IN_COS,
3847 BUILT_IN_NORMAL, NULL_PTR);
3849 /* Declare these functions volatile
3850 to avoid spurious "control drops through" warnings. */
3851 temp = builtin_function ("abort", cplus_mode ? void_ftype : void_ftype_any,
3852 0, NOT_BUILT_IN, NULL_PTR);
3853 TREE_THIS_VOLATILE (temp) = 1;
3854 TREE_SIDE_EFFECTS (temp) = 1;
3856 #if 0 /* ??? The C++ frontend used to do this. */
3857 /* Well, these are actually ANSI, but we can't set DECL_BUILT_IN on
3859 DECL_BUILT_IN_NONANSI (temp) = 1;
3861 temp = builtin_function ("exit",
3862 cplus_mode ? void_ftype_int : void_ftype_any,
3863 0, NOT_BUILT_IN, NULL_PTR);
3864 TREE_THIS_VOLATILE (temp) = 1;
3865 TREE_SIDE_EFFECTS (temp) = 1;
3867 #if 0 /* ??? The C++ frontend used to do this. */
3868 /* Well, these are actually ANSI, but we can't set DECL_BUILT_IN on
3870 DECL_BUILT_IN_NONANSI (temp) = 1;
3875 /* Support for these has not been written in either expand_builtin
3876 or build_function_call. */
3877 builtin_function ("__builtin_div", default_ftype, BUILT_IN_DIV,
3878 BUILT_IN_NORMAL, NULL_PTR);
3879 builtin_function ("__builtin_ldiv", default_ftype, BUILT_IN_LDIV,
3880 BUILT_IN_NORMAL, NULL_PTR);
3881 builtin_function ("__builtin_ffloor", double_ftype_double, BUILT_IN_FFLOOR,
3882 BUILT_IN_NORMAL, NULL_PTR);
3883 builtin_function ("__builtin_fceil", double_ftype_double, BUILT_IN_FCEIL,
3884 BUILT_IN_NORMAL, NULL_PTR);
3885 builtin_function ("__builtin_fmod", double_ftype_double_double,
3886 BUILT_IN_FMOD, BUILT_IN_NORMAL, NULL_PTR);
3887 builtin_function ("__builtin_frem", double_ftype_double_double,
3888 BUILT_IN_FREM, BUILT_IN_NORMAL, NULL_PTR);
3889 builtin_function ("__builtin_getexp", double_ftype_double, BUILT_IN_GETEXP,
3890 BUILT_IN_NORMAL, NULL_PTR);
3891 builtin_function ("__builtin_getman", double_ftype_double, BUILT_IN_GETMAN,
3892 BUILT_IN_NORMAL, NULL_PTR);
3895 /* ??? Perhaps there's a better place to do this. But it is related
3896 to __builtin_va_arg, so it isn't that off-the-wall. */
3897 lang_type_promotes_to = simple_type_promotes_to;
3901 build_va_arg (expr, type)
3904 return build1 (VA_ARG_EXPR, type, expr);
3907 /* Given a type, apply default promotions wrt unnamed function arguments
3908 and return the new type. Return NULL_TREE if no change. */
3909 /* ??? There is a function of the same name in the C++ front end that
3910 does something similar, but is more thorough and does not return NULL
3911 if no change. We could perhaps share code, but it would make the
3912 self_promoting_type property harder to identify. */
3915 simple_type_promotes_to (type)
3918 if (TYPE_MAIN_VARIANT (type) == float_type_node)
3919 return double_type_node;
3921 if (C_PROMOTING_INTEGER_TYPE_P (type))
3923 /* Traditionally, unsignedness is preserved in default promotions.
3924 Also preserve unsignedness if not really getting any wider. */
3925 if (TREE_UNSIGNED (type)
3926 && (flag_traditional
3927 || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
3928 return unsigned_type_node;
3929 return integer_type_node;
3935 /* Return 1 if PARMS specifies a fixed number of parameters
3936 and none of their types is affected by default promotions. */
3939 self_promoting_args_p (parms)
3943 for (t = parms; t; t = TREE_CHAIN (t))
3945 register tree type = TREE_VALUE (t);
3947 if (TREE_CHAIN (t) == 0 && type != void_type_node)
3953 if (TYPE_MAIN_VARIANT (type) == float_type_node)
3956 if (C_PROMOTING_INTEGER_TYPE_P (type))
3962 /* Recognize certain built-in functions so we can make tree-codes
3963 other than CALL_EXPR. We do this when it enables fold-const.c
3964 to do something useful. */
3965 /* ??? By rights this should go in builtins.c, but only C and C++
3966 implement build_{binary,unary}_op. Not exactly sure what bits
3967 of functionality are actually needed from those functions, or
3968 where the similar functionality exists in the other front ends. */
3971 expand_tree_builtin (function, params, coerced_params)
3972 tree function, params, coerced_params;
3974 enum tree_code code;
3976 if (DECL_BUILT_IN_CLASS (function) != BUILT_IN_NORMAL)
3979 switch (DECL_FUNCTION_CODE (function))
3984 if (coerced_params == 0)
3985 return integer_zero_node;
3986 return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
3988 case BUILT_IN_ISGREATER:
3989 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
3995 case BUILT_IN_ISGREATEREQUAL:
3996 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
4002 case BUILT_IN_ISLESS:
4003 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
4009 case BUILT_IN_ISLESSEQUAL:
4010 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
4016 case BUILT_IN_ISLESSGREATER:
4017 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
4023 case BUILT_IN_ISUNORDERED:
4024 if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT)
4025 return integer_zero_node;
4026 code = UNORDERED_EXPR;
4034 || TREE_CHAIN (params) == 0)
4036 error ("too few arguments to function `%s'",
4037 IDENTIFIER_POINTER (DECL_NAME (function)));
4038 return error_mark_node;
4040 else if (TREE_CHAIN (TREE_CHAIN (params)) != 0)
4042 error ("too many arguments to function `%s'",
4043 IDENTIFIER_POINTER (DECL_NAME (function)));
4044 return error_mark_node;
4047 arg0 = TREE_VALUE (params);
4048 arg1 = TREE_VALUE (TREE_CHAIN (params));
4049 arg0 = build_binary_op (code, arg0, arg1, 0);
4050 if (code != UNORDERED_EXPR)
4051 arg0 = build_unary_op (TRUTH_NOT_EXPR, arg0, 0);