1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992, 93-98, 1999 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
36 cpp_options parse_options;
37 enum cpp_token cpp_token;
40 #ifndef WCHAR_TYPE_SIZE
42 #define WCHAR_TYPE_SIZE INT_TYPE_SIZE
44 #define WCHAR_TYPE_SIZE BITS_PER_WORD
48 /* The following symbols are subsumed in the c_global_trees array, and
49 listed here individually for documentation purposes.
51 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
53 tree short_integer_type_node;
54 tree long_integer_type_node;
55 tree long_long_integer_type_node;
57 tree short_unsigned_type_node;
58 tree long_unsigned_type_node;
59 tree long_long_unsigned_type_node;
61 tree boolean_type_node;
62 tree boolean_false_node;
63 tree boolean_true_node;
65 tree ptrdiff_type_node;
67 tree unsigned_char_type_node;
68 tree signed_char_type_node;
70 tree signed_wchar_type_node;
71 tree unsigned_wchar_type_node;
74 tree double_type_node;
75 tree long_double_type_node;
77 tree complex_integer_type_node;
78 tree complex_float_type_node;
79 tree complex_double_type_node;
80 tree complex_long_double_type_node;
88 tree unsigned_intQI_type_node;
89 tree unsigned_intHI_type_node;
90 tree unsigned_intSI_type_node;
91 tree unsigned_intDI_type_node;
92 tree unsigned_intTI_type_node;
94 tree widest_integer_literal_type_node;
95 tree widest_unsigned_literal_type_node;
97 Nodes for types `void *' and `const void *'.
99 tree ptr_type_node, const_ptr_type_node;
101 Nodes for types `char *' and `const char *'.
103 tree string_type_node, const_string_type_node;
105 Type `char[SOMENUMBER]'.
106 Used when an array of char is needed and the size is irrelevant.
108 tree char_array_type_node;
110 Type `int[SOMENUMBER]' or something like it.
111 Used when an array of int needed and the size is irrelevant.
113 tree int_array_type_node;
115 Type `wchar_t[SOMENUMBER]' or something like it.
116 Used when a wide string literal is created.
118 tree wchar_array_type_node;
120 Type `int ()' -- used for implicit declaration of functions.
122 tree default_function_type;
124 Function types `int (int)', etc.
130 tree ptr_ftype_sizetype;
132 A VOID_TYPE node, packaged in a TREE_LIST.
138 tree c_global_trees[CTI_MAX];
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};
149 enum format_type { printf_format_type, scanf_format_type,
150 strftime_format_type };
152 static void declare_hidden_char_array PROTO((const char *, const char *));
153 static void add_attribute PROTO((enum attrs, const char *,
155 static void init_attributes PROTO((void));
156 static void record_function_format PROTO((tree, tree, enum format_type,
158 static void record_international_format PROTO((tree, tree, int));
159 static tree c_find_base_decl PROTO((tree));
160 static int default_valid_lang_attribute PROTO ((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 PROTO((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 declare_hidden_char_array ("__FUNCTION__", name);
275 declare_hidden_char_array ("__PRETTY_FUNCTION__", printable_name);
276 /* The ISO C people "of course" couldn't use __FUNCTION__ in the
277 ISO C 9x standard; instead a new variable is invented. */
278 declare_hidden_char_array ("__func__", name);
282 declare_hidden_char_array (name, value)
283 const char *name, *value;
285 tree decl, type, init;
288 /* If the default size of char arrays isn't big enough for the name,
289 or if we want to give warnings for large objects, make a bigger one. */
290 vlen = strlen (value) + 1;
291 type = char_array_type_node;
292 if (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < vlen
294 type = build_array_type (char_type_node,
295 build_index_type (build_int_2 (vlen, 0)));
296 push_obstacks_nochange ();
297 decl = build_decl (VAR_DECL, get_identifier (name), type);
298 TREE_STATIC (decl) = 1;
299 TREE_READONLY (decl) = 1;
300 TREE_ASM_WRITTEN (decl) = 1;
301 DECL_SOURCE_LINE (decl) = 0;
302 DECL_ARTIFICIAL (decl) = 1;
303 DECL_IN_SYSTEM_HEADER (decl) = 1;
304 DECL_IGNORED_P (decl) = 1;
305 init = build_string (vlen, value);
306 TREE_TYPE (init) = type;
307 DECL_INITIAL (decl) = init;
308 finish_decl (pushdecl (decl), init, NULL_TREE);
311 /* Given a chain of STRING_CST nodes,
312 concatenate them into one STRING_CST
313 and give it a suitable array-of-chars data type. */
316 combine_strings (strings)
319 register tree value, t;
320 register int length = 1;
323 int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
326 if (TREE_CHAIN (strings))
328 /* More than one in the chain, so concatenate. */
329 register char *p, *q;
331 /* Don't include the \0 at the end of each substring,
332 except for the last one.
333 Count wide strings and ordinary strings separately. */
334 for (t = strings; t; t = TREE_CHAIN (t))
336 if (TREE_TYPE (t) == wchar_array_type_node)
338 wide_length += (TREE_STRING_LENGTH (t) - wchar_bytes);
342 length += (TREE_STRING_LENGTH (t) - 1);
345 /* If anything is wide, the non-wides will be converted,
346 which makes them take more space. */
348 length = length * wchar_bytes + wide_length;
350 p = savealloc (length);
352 /* Copy the individual strings into the new combined string.
353 If the combined string is wide, convert the chars to ints
354 for any individual strings that are not wide. */
357 for (t = strings; t; t = TREE_CHAIN (t))
359 int len = (TREE_STRING_LENGTH (t)
360 - ((TREE_TYPE (t) == wchar_array_type_node)
362 if ((TREE_TYPE (t) == wchar_array_type_node) == wide_flag)
364 memcpy (q, TREE_STRING_POINTER (t), len);
370 for (i = 0; i < len; i++)
372 if (WCHAR_TYPE_SIZE == HOST_BITS_PER_SHORT)
373 ((short *) q)[i] = TREE_STRING_POINTER (t)[i];
375 ((int *) q)[i] = TREE_STRING_POINTER (t)[i];
377 q += len * wchar_bytes;
383 for (i = 0; i < wchar_bytes; i++)
389 value = make_node (STRING_CST);
390 TREE_STRING_POINTER (value) = p;
391 TREE_STRING_LENGTH (value) = length;
396 length = TREE_STRING_LENGTH (value);
397 if (TREE_TYPE (value) == wchar_array_type_node)
401 /* Compute the number of elements, for the array type. */
402 nchars = wide_flag ? length / wchar_bytes : length;
404 /* Create the array type for the string constant.
405 -Wwrite-strings says make the string constant an array of const char
406 so that copying it to a non-const pointer will get a warning.
407 For C++, this is the standard behavior. */
408 if (flag_const_strings
409 && (! flag_traditional && ! flag_writable_strings))
412 = build_type_variant (wide_flag ? wchar_type_node : char_type_node,
415 = build_array_type (elements,
416 build_index_type (build_int_2 (nchars - 1, 0)));
420 = build_array_type (wide_flag ? wchar_type_node : char_type_node,
421 build_index_type (build_int_2 (nchars - 1, 0)));
423 TREE_READONLY (value) = TREE_CONSTANT (value) = ! flag_writable_strings;
424 TREE_STATIC (value) = 1;
428 /* To speed up processing of attributes, we maintain an array of
429 IDENTIFIER_NODES and the corresponding attribute types. */
431 /* Array to hold attribute information. */
433 static struct {enum attrs id; tree name; int min, max, decl_req;} attrtab[50];
435 static int attrtab_idx = 0;
437 /* Add an entry to the attribute table above. */
440 add_attribute (id, string, min_len, max_len, decl_req)
443 int min_len, max_len;
448 attrtab[attrtab_idx].id = id;
449 attrtab[attrtab_idx].name = get_identifier (string);
450 attrtab[attrtab_idx].min = min_len;
451 attrtab[attrtab_idx].max = max_len;
452 attrtab[attrtab_idx++].decl_req = decl_req;
454 sprintf (buf, "__%s__", string);
456 attrtab[attrtab_idx].id = id;
457 attrtab[attrtab_idx].name = get_identifier (buf);
458 attrtab[attrtab_idx].min = min_len;
459 attrtab[attrtab_idx].max = max_len;
460 attrtab[attrtab_idx++].decl_req = decl_req;
463 /* Initialize attribute table. */
468 add_attribute (A_PACKED, "packed", 0, 0, 0);
469 add_attribute (A_NOCOMMON, "nocommon", 0, 0, 1);
470 add_attribute (A_COMMON, "common", 0, 0, 1);
471 add_attribute (A_NORETURN, "noreturn", 0, 0, 1);
472 add_attribute (A_NORETURN, "volatile", 0, 0, 1);
473 add_attribute (A_UNUSED, "unused", 0, 0, 0);
474 add_attribute (A_CONST, "const", 0, 0, 1);
475 add_attribute (A_T_UNION, "transparent_union", 0, 0, 0);
476 add_attribute (A_CONSTRUCTOR, "constructor", 0, 0, 1);
477 add_attribute (A_DESTRUCTOR, "destructor", 0, 0, 1);
478 add_attribute (A_MODE, "mode", 1, 1, 1);
479 add_attribute (A_SECTION, "section", 1, 1, 1);
480 add_attribute (A_ALIGNED, "aligned", 0, 1, 0);
481 add_attribute (A_FORMAT, "format", 3, 3, 1);
482 add_attribute (A_FORMAT_ARG, "format_arg", 1, 1, 1);
483 add_attribute (A_WEAK, "weak", 0, 0, 1);
484 add_attribute (A_ALIAS, "alias", 1, 1, 1);
485 add_attribute (A_NO_INSTRUMENT_FUNCTION, "no_instrument_function", 0, 0, 1);
486 add_attribute (A_NO_CHECK_MEMORY_USAGE, "no_check_memory_usage", 0, 0, 1);
489 /* Default implementation of valid_lang_attribute, below. By default, there
490 are no language-specific attributes. */
493 default_valid_lang_attribute (attr_name, attr_args, decl, type)
494 tree attr_name ATTRIBUTE_UNUSED;
495 tree attr_args ATTRIBUTE_UNUSED;
496 tree decl ATTRIBUTE_UNUSED;
497 tree type ATTRIBUTE_UNUSED;
502 /* Return a 1 if ATTR_NAME and ATTR_ARGS denote a valid language-specific
503 attribute for either declaration DECL or type TYPE and 0 otherwise. */
505 int (*valid_lang_attribute) PROTO ((tree, tree, tree, tree))
506 = default_valid_lang_attribute;
508 /* Process the attributes listed in ATTRIBUTES and PREFIX_ATTRIBUTES
509 and install them in NODE, which is either a DECL (including a TYPE_DECL)
510 or a TYPE. PREFIX_ATTRIBUTES can appear after the declaration specifiers
511 and declaration modifiers but before the declaration proper. */
514 decl_attributes (node, attributes, prefix_attributes)
515 tree node, attributes, prefix_attributes;
517 tree decl = 0, type = 0;
521 if (attrtab_idx == 0)
524 if (TREE_CODE_CLASS (TREE_CODE (node)) == 'd')
527 type = TREE_TYPE (decl);
528 is_type = TREE_CODE (node) == TYPE_DECL;
530 else if (TREE_CODE_CLASS (TREE_CODE (node)) == 't')
531 type = node, is_type = 1;
533 #ifdef PRAGMA_INSERT_ATTRIBUTES
534 /* If the code in c-pragma.c wants to insert some attributes then
535 allow it to do so. Do this before allowing machine back ends to
536 insert attributes, so that they have the opportunity to override
537 anything done here. */
538 PRAGMA_INSERT_ATTRIBUTES (node, & attributes, & prefix_attributes);
541 #ifdef INSERT_ATTRIBUTES
542 INSERT_ATTRIBUTES (node, & attributes, & prefix_attributes);
545 attributes = chainon (prefix_attributes, attributes);
547 for (a = attributes; a; a = TREE_CHAIN (a))
549 tree name = TREE_PURPOSE (a);
550 tree args = TREE_VALUE (a);
554 for (i = 0; i < attrtab_idx; i++)
555 if (attrtab[i].name == name)
558 if (i == attrtab_idx)
560 if (! valid_machine_attribute (name, args, decl, type)
561 && ! (* valid_lang_attribute) (name, args, decl, type))
562 warning ("`%s' attribute directive ignored",
563 IDENTIFIER_POINTER (name));
565 type = TREE_TYPE (decl);
568 else if (attrtab[i].decl_req && decl == 0)
570 warning ("`%s' attribute does not apply to types",
571 IDENTIFIER_POINTER (name));
574 else if (list_length (args) < attrtab[i].min
575 || list_length (args) > attrtab[i].max)
577 error ("wrong number of arguments specified for `%s' attribute",
578 IDENTIFIER_POINTER (name));
587 TYPE_PACKED (type) = 1;
588 else if (TREE_CODE (decl) == FIELD_DECL)
589 DECL_PACKED (decl) = 1;
590 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
591 used for DECL_REGISTER. It wouldn't mean anything anyway. */
593 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
597 if (TREE_CODE (decl) == VAR_DECL)
598 DECL_COMMON (decl) = 0;
600 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
604 if (TREE_CODE (decl) == VAR_DECL)
605 DECL_COMMON (decl) = 1;
607 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
611 if (TREE_CODE (decl) == FUNCTION_DECL)
612 TREE_THIS_VOLATILE (decl) = 1;
613 else if (TREE_CODE (type) == POINTER_TYPE
614 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
615 TREE_TYPE (decl) = type
617 (build_type_variant (TREE_TYPE (type),
618 TREE_READONLY (TREE_TYPE (type)), 1));
620 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
625 TREE_USED (type) = 1;
626 else if (TREE_CODE (decl) == PARM_DECL
627 || TREE_CODE (decl) == VAR_DECL
628 || TREE_CODE (decl) == FUNCTION_DECL
629 || TREE_CODE (decl) == LABEL_DECL)
630 TREE_USED (decl) = 1;
632 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
636 if (TREE_CODE (decl) == FUNCTION_DECL)
637 TREE_READONLY (decl) = 1;
638 else if (TREE_CODE (type) == POINTER_TYPE
639 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
640 TREE_TYPE (decl) = type
642 (build_type_variant (TREE_TYPE (type), 1,
643 TREE_THIS_VOLATILE (TREE_TYPE (type))));
645 warning ( "`%s' attribute ignored", IDENTIFIER_POINTER (name));
650 && TREE_CODE (type) == UNION_TYPE
652 || (TYPE_FIELDS (type) != 0
653 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))))
654 TYPE_TRANSPARENT_UNION (type) = 1;
655 else if (decl != 0 && TREE_CODE (decl) == PARM_DECL
656 && TREE_CODE (type) == UNION_TYPE
657 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))
658 DECL_TRANSPARENT_UNION (decl) = 1;
660 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
664 if (TREE_CODE (decl) == FUNCTION_DECL
665 && TREE_CODE (type) == FUNCTION_TYPE
666 && decl_function_context (decl) == 0)
668 DECL_STATIC_CONSTRUCTOR (decl) = 1;
669 TREE_USED (decl) = 1;
672 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
676 if (TREE_CODE (decl) == FUNCTION_DECL
677 && TREE_CODE (type) == FUNCTION_TYPE
678 && decl_function_context (decl) == 0)
680 DECL_STATIC_DESTRUCTOR (decl) = 1;
681 TREE_USED (decl) = 1;
684 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
688 if (TREE_CODE (TREE_VALUE (args)) != IDENTIFIER_NODE)
689 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
693 const char *p = IDENTIFIER_POINTER (TREE_VALUE (args));
694 int len = strlen (p);
695 enum machine_mode mode = VOIDmode;
698 if (len > 4 && p[0] == '_' && p[1] == '_'
699 && p[len - 1] == '_' && p[len - 2] == '_')
701 char *newp = (char *) alloca (len - 1);
703 strcpy (newp, &p[2]);
704 newp[len - 4] = '\0';
708 /* Give this decl a type with the specified mode.
709 First check for the special modes. */
710 if (! strcmp (p, "byte"))
712 else if (!strcmp (p, "word"))
714 else if (! strcmp (p, "pointer"))
717 for (j = 0; j < NUM_MACHINE_MODES; j++)
718 if (!strcmp (p, GET_MODE_NAME (j)))
719 mode = (enum machine_mode) j;
721 if (mode == VOIDmode)
722 error ("unknown machine mode `%s'", p);
723 else if (0 == (typefm = type_for_mode (mode,
724 TREE_UNSIGNED (type))))
725 error ("no data type for mode `%s'", p);
728 TREE_TYPE (decl) = type = typefm;
729 DECL_SIZE (decl) = 0;
730 layout_decl (decl, 0);
736 #ifdef ASM_OUTPUT_SECTION_NAME
737 if ((TREE_CODE (decl) == FUNCTION_DECL
738 || TREE_CODE (decl) == VAR_DECL)
739 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
741 if (TREE_CODE (decl) == VAR_DECL
742 && current_function_decl != NULL_TREE
743 && ! TREE_STATIC (decl))
744 error_with_decl (decl,
745 "section attribute cannot be specified for local variables");
746 /* The decl may have already been given a section attribute from
747 a previous declaration. Ensure they match. */
748 else if (DECL_SECTION_NAME (decl) != NULL_TREE
749 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
750 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
751 error_with_decl (node,
752 "section of `%s' conflicts with previous declaration");
754 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
757 error_with_decl (node,
758 "section attribute not allowed for `%s'");
760 error_with_decl (node,
761 "section attributes are not supported for this target");
768 = (args ? TREE_VALUE (args)
769 : size_int (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
772 /* Strip any NOPs of any kind. */
773 while (TREE_CODE (align_expr) == NOP_EXPR
774 || TREE_CODE (align_expr) == CONVERT_EXPR
775 || TREE_CODE (align_expr) == NON_LVALUE_EXPR)
776 align_expr = TREE_OPERAND (align_expr, 0);
778 if (TREE_CODE (align_expr) != INTEGER_CST)
780 error ("requested alignment is not a constant");
784 align = TREE_INT_CST_LOW (align_expr) * BITS_PER_UNIT;
786 if (exact_log2 (align) == -1)
787 error ("requested alignment is not a power of 2");
789 TYPE_ALIGN (type) = align;
790 else if (TREE_CODE (decl) != VAR_DECL
791 && TREE_CODE (decl) != FIELD_DECL)
792 error_with_decl (decl,
793 "alignment may not be specified for `%s'");
795 DECL_ALIGN (decl) = align;
801 tree format_type_id = TREE_VALUE (args);
802 tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
803 tree first_arg_num_expr
804 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
807 enum format_type format_type;
811 if (TREE_CODE (decl) != FUNCTION_DECL)
813 error_with_decl (decl,
814 "argument format specified for non-function `%s'");
818 if (TREE_CODE (format_type_id) != IDENTIFIER_NODE)
820 error ("unrecognized format specifier");
825 const char *p = IDENTIFIER_POINTER (format_type_id);
827 if (!strcmp (p, "printf") || !strcmp (p, "__printf__"))
828 format_type = printf_format_type;
829 else if (!strcmp (p, "scanf") || !strcmp (p, "__scanf__"))
830 format_type = scanf_format_type;
831 else if (!strcmp (p, "strftime")
832 || !strcmp (p, "__strftime__"))
833 format_type = strftime_format_type;
836 warning ("`%s' is an unrecognized format function type", p);
841 /* Strip any conversions from the string index and first arg number
842 and verify they are constants. */
843 while (TREE_CODE (format_num_expr) == NOP_EXPR
844 || TREE_CODE (format_num_expr) == CONVERT_EXPR
845 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
846 format_num_expr = TREE_OPERAND (format_num_expr, 0);
848 while (TREE_CODE (first_arg_num_expr) == NOP_EXPR
849 || TREE_CODE (first_arg_num_expr) == CONVERT_EXPR
850 || TREE_CODE (first_arg_num_expr) == NON_LVALUE_EXPR)
851 first_arg_num_expr = TREE_OPERAND (first_arg_num_expr, 0);
853 if (TREE_CODE (format_num_expr) != INTEGER_CST
854 || TREE_CODE (first_arg_num_expr) != INTEGER_CST)
856 error ("format string has non-constant operand number");
860 format_num = TREE_INT_CST_LOW (format_num_expr);
861 first_arg_num = TREE_INT_CST_LOW (first_arg_num_expr);
862 if (first_arg_num != 0 && first_arg_num <= format_num)
864 error ("format string arg follows the args to be formatted");
868 /* If a parameter list is specified, verify that the format_num
869 argument is actually a string, in case the format attribute
871 argument = TYPE_ARG_TYPES (type);
874 for (arg_num = 1; ; ++arg_num)
876 if (argument == 0 || arg_num == format_num)
878 argument = TREE_CHAIN (argument);
881 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
882 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
885 error ("format string arg not a string type");
888 if (first_arg_num != 0)
890 /* Verify that first_arg_num points to the last arg,
893 arg_num++, argument = TREE_CHAIN (argument);
894 if (arg_num != first_arg_num)
896 error ("args to be formatted is not ...");
902 record_function_format (DECL_NAME (decl),
903 DECL_ASSEMBLER_NAME (decl),
904 format_type, format_num, first_arg_num);
910 tree format_num_expr = TREE_VALUE (args);
911 int format_num, arg_num;
914 if (TREE_CODE (decl) != FUNCTION_DECL)
916 error_with_decl (decl,
917 "argument format specified for non-function `%s'");
921 /* Strip any conversions from the first arg number and verify it
923 while (TREE_CODE (format_num_expr) == NOP_EXPR
924 || TREE_CODE (format_num_expr) == CONVERT_EXPR
925 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
926 format_num_expr = TREE_OPERAND (format_num_expr, 0);
928 if (TREE_CODE (format_num_expr) != INTEGER_CST)
930 error ("format string has non-constant operand number");
934 format_num = TREE_INT_CST_LOW (format_num_expr);
936 /* If a parameter list is specified, verify that the format_num
937 argument is actually a string, in case the format attribute
939 argument = TYPE_ARG_TYPES (type);
942 for (arg_num = 1; ; ++arg_num)
944 if (argument == 0 || arg_num == format_num)
946 argument = TREE_CHAIN (argument);
949 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
950 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
953 error ("format string arg not a string type");
958 if (TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) != POINTER_TYPE
959 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_TYPE (decl))))
962 error ("function does not return string type");
966 record_international_format (DECL_NAME (decl),
967 DECL_ASSEMBLER_NAME (decl),
977 if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
978 || (TREE_CODE (decl) != FUNCTION_DECL && ! DECL_EXTERNAL (decl)))
979 error_with_decl (decl,
980 "`%s' defined both normally and as an alias");
981 else if (decl_function_context (decl) == 0)
985 id = TREE_VALUE (args);
986 if (TREE_CODE (id) != STRING_CST)
988 error ("alias arg not a string");
991 id = get_identifier (TREE_STRING_POINTER (id));
993 if (TREE_CODE (decl) == FUNCTION_DECL)
994 DECL_INITIAL (decl) = error_mark_node;
996 DECL_EXTERNAL (decl) = 0;
997 assemble_alias (decl, id);
1000 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
1003 case A_NO_CHECK_MEMORY_USAGE:
1004 if (TREE_CODE (decl) != FUNCTION_DECL)
1006 error_with_decl (decl,
1007 "`%s' attribute applies only to functions",
1008 IDENTIFIER_POINTER (name));
1010 else if (DECL_INITIAL (decl))
1012 error_with_decl (decl,
1013 "can't set `%s' attribute after definition",
1014 IDENTIFIER_POINTER (name));
1017 DECL_NO_CHECK_MEMORY_USAGE (decl) = 1;
1020 case A_NO_INSTRUMENT_FUNCTION:
1021 if (TREE_CODE (decl) != FUNCTION_DECL)
1023 error_with_decl (decl,
1024 "`%s' attribute applies only to functions",
1025 IDENTIFIER_POINTER (name));
1027 else if (DECL_INITIAL (decl))
1029 error_with_decl (decl,
1030 "can't set `%s' attribute after definition",
1031 IDENTIFIER_POINTER (name));
1034 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1040 /* Split SPECS_ATTRS, a list of declspecs and prefix attributes, into two
1041 lists. SPECS_ATTRS may also be just a typespec (eg: RECORD_TYPE).
1043 The head of the declspec list is stored in DECLSPECS.
1044 The head of the attribute list is stored in PREFIX_ATTRIBUTES.
1046 Note that attributes in SPECS_ATTRS are stored in the TREE_PURPOSE of
1047 the list elements. We drop the containing TREE_LIST nodes and link the
1048 resulting attributes together the way decl_attributes expects them. */
1051 split_specs_attrs (specs_attrs, declspecs, prefix_attributes)
1053 tree *declspecs, *prefix_attributes;
1055 tree t, s, a, next, specs, attrs;
1057 /* This can happen in c++ (eg: decl: typespec initdecls ';'). */
1058 if (specs_attrs != NULL_TREE
1059 && TREE_CODE (specs_attrs) != TREE_LIST)
1061 *declspecs = specs_attrs;
1062 *prefix_attributes = NULL_TREE;
1066 /* Remember to keep the lists in the same order, element-wise. */
1068 specs = s = NULL_TREE;
1069 attrs = a = NULL_TREE;
1070 for (t = specs_attrs; t; t = next)
1072 next = TREE_CHAIN (t);
1073 /* Declspecs have a non-NULL TREE_VALUE. */
1074 if (TREE_VALUE (t) != NULL_TREE)
1076 if (specs == NULL_TREE)
1086 if (attrs == NULL_TREE)
1087 attrs = a = TREE_PURPOSE (t);
1090 TREE_CHAIN (a) = TREE_PURPOSE (t);
1091 a = TREE_PURPOSE (t);
1093 /* More attrs can be linked here, move A to the end. */
1094 while (TREE_CHAIN (a) != NULL_TREE)
1099 /* Terminate the lists. */
1101 TREE_CHAIN (s) = NULL_TREE;
1103 TREE_CHAIN (a) = NULL_TREE;
1107 *prefix_attributes = attrs;
1110 /* Strip attributes from SPECS_ATTRS, a list of declspecs and attributes.
1111 This function is used by the parser when a rule will accept attributes
1112 in a particular position, but we don't want to support that just yet.
1114 A warning is issued for every ignored attribute. */
1117 strip_attrs (specs_attrs)
1122 split_specs_attrs (specs_attrs, &specs, &attrs);
1126 warning ("`%s' attribute ignored",
1127 IDENTIFIER_POINTER (TREE_PURPOSE (attrs)));
1128 attrs = TREE_CHAIN (attrs);
1134 /* Check a printf/fprintf/sprintf/scanf/fscanf/sscanf format against
1135 a parameter list. */
1137 #define T_I &integer_type_node
1138 #define T_L &long_integer_type_node
1139 #define T_LL &long_long_integer_type_node
1140 #define T_S &short_integer_type_node
1141 #define T_UI &unsigned_type_node
1142 #define T_UL &long_unsigned_type_node
1143 #define T_ULL &long_long_unsigned_type_node
1144 #define T_US &short_unsigned_type_node
1145 #define T_F &float_type_node
1146 #define T_D &double_type_node
1147 #define T_LD &long_double_type_node
1148 #define T_C &char_type_node
1149 #define T_UC &unsigned_char_type_node
1150 #define T_V &void_type_node
1151 #define T_W &wchar_type_node
1152 #define T_ST &sizetype
1155 const char *format_chars;
1157 /* Type of argument if no length modifier is used. */
1159 /* Type of argument if length modifier for shortening to byte is used.
1160 If NULL, then this modifier is not allowed. */
1162 /* Type of argument if length modifier for shortening is used.
1163 If NULL, then this modifier is not allowed. */
1165 /* Type of argument if length modifier `l' is used.
1166 If NULL, then this modifier is not allowed. */
1168 /* Type of argument if length modifier `q' or `ll' is used.
1169 If NULL, then this modifier is not allowed. */
1171 /* Type of argument if length modifier `L' is used.
1172 If NULL, then this modifier is not allowed. */
1174 /* Type of argument if length modifier `Z' is used.
1175 If NULL, then this modifier is not allowed. */
1177 /* List of other modifier characters allowed with these options. */
1178 const char *flag_chars;
1181 static format_char_info print_char_table[] = {
1182 { "di", 0, T_I, T_I, T_I, T_L, T_LL, T_LL, T_ST, "-wp0 +" },
1183 { "oxX", 0, T_UI, T_UI, T_UI, T_UL, T_ULL, T_ULL, T_ST, "-wp0#" },
1184 { "u", 0, T_UI, T_UI, T_UI, T_UL, T_ULL, T_ULL, T_ST, "-wp0" },
1185 /* A GNU extension. */
1186 { "m", 0, T_V, NULL, NULL, NULL, NULL, NULL, NULL, "-wp" },
1187 { "feEgGaA", 0, T_D, NULL, NULL, NULL, NULL, T_LD, NULL, "-wp0 +#" },
1188 { "c", 0, T_I, NULL, NULL, T_W, NULL, NULL, NULL, "-w" },
1189 { "C", 0, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "-w" },
1190 { "s", 1, T_C, NULL, NULL, T_W, NULL, NULL, NULL, "-wp" },
1191 { "S", 1, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "-wp" },
1192 { "p", 1, T_V, NULL, NULL, NULL, NULL, NULL, NULL, "-w" },
1193 { "n", 1, T_I, NULL, T_S, T_L, T_LL, NULL, NULL, "" },
1194 { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
1197 static format_char_info scan_char_table[] = {
1198 { "di", 1, T_I, T_C, T_S, T_L, T_LL, T_LL, NULL, "*" },
1199 { "ouxX", 1, T_UI, T_UC, T_US, T_UL, T_ULL, T_ULL, NULL, "*" },
1200 { "efgEGaA", 1, T_F, NULL, NULL, T_D, NULL, T_LD, NULL, "*" },
1201 { "c", 1, T_C, NULL, NULL, T_W, NULL, NULL, NULL, "*" },
1202 { "s", 1, T_C, NULL, NULL, T_W, NULL, NULL, NULL, "*a" },
1203 { "[", 1, T_C, NULL, NULL, NULL, NULL, NULL, NULL, "*a" },
1204 { "C", 1, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "*" },
1205 { "S", 1, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "*a" },
1206 { "p", 2, T_V, NULL, NULL, NULL, NULL, NULL, NULL, "*" },
1207 { "n", 1, T_I, T_C, T_S, T_L, T_LL, NULL, NULL, "" },
1208 { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
1211 /* Handle format characters recognized by glibc's strftime.c.
1212 '2' - MUST do years as only two digits
1213 '3' - MAY do years as only two digits (depending on locale)
1214 'E' - E modifier is acceptable
1215 'O' - O modifier is acceptable to Standard C
1216 'o' - O modifier is acceptable as a GNU extension
1217 'G' - other GNU extensions */
1219 static format_char_info time_char_table[] = {
1220 { "y", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "2EO-_0w" },
1221 { "D", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "2" },
1222 { "g", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "2O-_0w" },
1223 { "cx", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "3E" },
1224 { "%RTXnrt", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "" },
1225 { "P", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "G" },
1226 { "HIMSUWdemw", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0Ow" },
1227 { "Vju", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0Oow" },
1228 { "Gklsz", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0OGw" },
1229 { "ABZa", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "^#" },
1230 { "p", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "#" },
1231 { "bh", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "^" },
1232 { "CY", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0EOw" },
1233 { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
1236 typedef struct function_format_info
1238 struct function_format_info *next; /* next structure on the list */
1239 tree name; /* identifier such as "printf" */
1240 tree assembler_name; /* optional mangled identifier (for C++) */
1241 enum format_type format_type; /* type of format (printf, scanf, etc.) */
1242 int format_num; /* number of format argument */
1243 int first_arg_num; /* number of first arg (zero for varargs) */
1244 } function_format_info;
1246 static function_format_info *function_format_list = NULL;
1248 typedef struct international_format_info
1250 struct international_format_info *next; /* next structure on the list */
1251 tree name; /* identifier such as "gettext" */
1252 tree assembler_name; /* optional mangled identifier (for C++) */
1253 int format_num; /* number of format argument */
1254 } international_format_info;
1256 static international_format_info *international_format_list = NULL;
1258 static void check_format_info PROTO((function_format_info *, tree));
1260 /* Initialize the table of functions to perform format checking on.
1261 The ANSI functions are always checked (whether <stdio.h> is
1262 included or not), since it is common to call printf without
1263 including <stdio.h>. There shouldn't be a problem with this,
1264 since ANSI reserves these function names whether you include the
1265 header file or not. In any case, the checking is harmless.
1267 Also initialize the name of function that modify the format string for
1268 internationalization purposes. */
1271 init_function_format_info ()
1273 record_function_format (get_identifier ("printf"), NULL_TREE,
1274 printf_format_type, 1, 2);
1275 record_function_format (get_identifier ("fprintf"), NULL_TREE,
1276 printf_format_type, 2, 3);
1277 record_function_format (get_identifier ("sprintf"), NULL_TREE,
1278 printf_format_type, 2, 3);
1279 record_function_format (get_identifier ("scanf"), NULL_TREE,
1280 scanf_format_type, 1, 2);
1281 record_function_format (get_identifier ("fscanf"), NULL_TREE,
1282 scanf_format_type, 2, 3);
1283 record_function_format (get_identifier ("sscanf"), NULL_TREE,
1284 scanf_format_type, 2, 3);
1285 record_function_format (get_identifier ("vprintf"), NULL_TREE,
1286 printf_format_type, 1, 0);
1287 record_function_format (get_identifier ("vfprintf"), NULL_TREE,
1288 printf_format_type, 2, 0);
1289 record_function_format (get_identifier ("vsprintf"), NULL_TREE,
1290 printf_format_type, 2, 0);
1291 record_function_format (get_identifier ("strftime"), NULL_TREE,
1292 strftime_format_type, 3, 0);
1294 record_international_format (get_identifier ("gettext"), NULL_TREE, 1);
1295 record_international_format (get_identifier ("dgettext"), NULL_TREE, 2);
1296 record_international_format (get_identifier ("dcgettext"), NULL_TREE, 2);
1299 /* Record information for argument format checking. FUNCTION_IDENT is
1300 the identifier node for the name of the function to check (its decl
1301 need not exist yet).
1302 FORMAT_TYPE specifies the type of format checking. FORMAT_NUM is the number
1303 of the argument which is the format control string (starting from 1).
1304 FIRST_ARG_NUM is the number of the first actual argument to check
1305 against the format string, or zero if no checking is not be done
1306 (e.g. for varargs such as vfprintf). */
1309 record_function_format (name, assembler_name, format_type,
1310 format_num, first_arg_num)
1312 tree assembler_name;
1313 enum format_type format_type;
1317 function_format_info *info;
1319 /* Re-use existing structure if it's there. */
1321 for (info = function_format_list; info; info = info->next)
1323 if (info->name == name && info->assembler_name == assembler_name)
1328 info = (function_format_info *) xmalloc (sizeof (function_format_info));
1329 info->next = function_format_list;
1330 function_format_list = info;
1333 info->assembler_name = assembler_name;
1336 info->format_type = format_type;
1337 info->format_num = format_num;
1338 info->first_arg_num = first_arg_num;
1341 /* Record information for the names of function that modify the format
1342 argument to format functions. FUNCTION_IDENT is the identifier node for
1343 the name of the function (its decl need not exist yet) and FORMAT_NUM is
1344 the number of the argument which is the format control string (starting
1348 record_international_format (name, assembler_name, format_num)
1350 tree assembler_name;
1353 international_format_info *info;
1355 /* Re-use existing structure if it's there. */
1357 for (info = international_format_list; info; info = info->next)
1359 if (info->name == name && info->assembler_name == assembler_name)
1366 = (international_format_info *)
1367 xmalloc (sizeof (international_format_info));
1368 info->next = international_format_list;
1369 international_format_list = info;
1372 info->assembler_name = assembler_name;
1375 info->format_num = format_num;
1381 warning ("too few arguments for format");
1384 /* Check the argument list of a call to printf, scanf, etc.
1385 NAME is the function identifier.
1386 ASSEMBLER_NAME is the function's assembler identifier.
1387 (Either NAME or ASSEMBLER_NAME, but not both, may be NULL_TREE.)
1388 PARAMS is the list of argument values. */
1391 check_function_format (name, assembler_name, params)
1393 tree assembler_name;
1396 function_format_info *info;
1398 /* See if this function is a format function. */
1399 for (info = function_format_list; info; info = info->next)
1401 if (info->assembler_name
1402 ? (info->assembler_name == assembler_name)
1403 : (info->name == name))
1405 /* Yup; check it. */
1406 check_format_info (info, params);
1412 /* Check the argument list of a call to printf, scanf, etc.
1413 INFO points to the function_format_info structure.
1414 PARAMS is the list of argument values. */
1417 check_format_info (info, params)
1418 function_format_info *info;
1423 int suppressed, wide, precise;
1424 int length_char = 0;
1431 tree first_fillin_param;
1432 const char *format_chars;
1433 format_char_info *fci = NULL;
1435 int has_operand_number = 0;
1437 /* Skip to format argument. If the argument isn't available, there's
1438 no work for us to do; prototype checking will catch the problem. */
1439 for (arg_num = 1; ; ++arg_num)
1443 if (arg_num == info->format_num)
1445 params = TREE_CHAIN (params);
1447 format_tree = TREE_VALUE (params);
1448 params = TREE_CHAIN (params);
1449 if (format_tree == 0)
1452 /* We can only check the format if it's a string constant. */
1453 while (TREE_CODE (format_tree) == NOP_EXPR)
1454 format_tree = TREE_OPERAND (format_tree, 0); /* strip coercion */
1456 if (TREE_CODE (format_tree) == CALL_EXPR
1457 && TREE_CODE (TREE_OPERAND (format_tree, 0)) == ADDR_EXPR
1458 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0))
1461 tree function = TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0);
1463 /* See if this is a call to a known internationalization function
1464 that modifies the format arg. */
1465 international_format_info *info;
1467 for (info = international_format_list; info; info = info->next)
1468 if (info->assembler_name
1469 ? (info->assembler_name == DECL_ASSEMBLER_NAME (function))
1470 : (info->name == DECL_NAME (function)))
1475 for (inner_args = TREE_OPERAND (format_tree, 1), i = 1;
1477 inner_args = TREE_CHAIN (inner_args), i++)
1478 if (i == info->format_num)
1480 format_tree = TREE_VALUE (inner_args);
1482 while (TREE_CODE (format_tree) == NOP_EXPR)
1483 format_tree = TREE_OPERAND (format_tree, 0);
1488 if (integer_zerop (format_tree))
1490 warning ("null format string");
1493 if (TREE_CODE (format_tree) != ADDR_EXPR)
1495 format_tree = TREE_OPERAND (format_tree, 0);
1496 if (TREE_CODE (format_tree) != STRING_CST)
1498 format_chars = TREE_STRING_POINTER (format_tree);
1499 format_length = TREE_STRING_LENGTH (format_tree);
1500 if (format_length <= 1)
1501 warning ("zero-length format string");
1502 if (format_chars[--format_length] != 0)
1504 warning ("unterminated format string");
1507 /* Skip to first argument to check. */
1508 while (arg_num + 1 < info->first_arg_num)
1512 params = TREE_CHAIN (params);
1516 first_fillin_param = params;
1520 if (*format_chars == 0)
1522 if (format_chars - TREE_STRING_POINTER (format_tree) != format_length)
1523 warning ("embedded `\\0' in format");
1524 if (info->first_arg_num != 0 && params != 0 && ! has_operand_number)
1525 warning ("too many arguments for format");
1528 if (*format_chars++ != '%')
1530 if (*format_chars == 0)
1532 warning ("spurious trailing `%%' in format");
1535 if (*format_chars == '%')
1541 suppressed = wide = precise = FALSE;
1542 if (info->format_type == scanf_format_type)
1544 suppressed = *format_chars == '*';
1547 while (ISDIGIT (*format_chars))
1550 else if (info->format_type == strftime_format_type)
1552 while (*format_chars != 0 && index ("_-0^#", *format_chars) != 0)
1555 warning ("ANSI C does not support the strftime `%c' flag",
1557 if (index (flag_chars, *format_chars) != 0)
1559 warning ("repeated `%c' flag in format",
1565 i = strlen (flag_chars);
1566 flag_chars[i++] = *format_chars++;
1570 while (ISDIGIT ((unsigned char) *format_chars))
1575 if (wide && pedantic)
1576 warning ("ANSI C does not support strftime format width");
1577 if (*format_chars == 'E' || *format_chars == 'O')
1579 i = strlen (flag_chars);
1580 flag_chars[i++] = *format_chars++;
1582 if (*format_chars == 'E' || *format_chars == 'O')
1584 warning ("multiple E/O modifiers in format");
1585 while (*format_chars == 'E' || *format_chars == 'O')
1590 else if (info->format_type == printf_format_type)
1592 /* See if we have a number followed by a dollar sign. If we do,
1593 it is an operand number, so set PARAMS to that operand. */
1594 if (*format_chars >= '0' && *format_chars <= '9')
1596 const char *p = format_chars;
1598 while (*p >= '0' && *p++ <= '9')
1603 int opnum = atoi (format_chars);
1605 params = first_fillin_param;
1606 format_chars = p + 1;
1607 has_operand_number = 1;
1609 for (i = 1; i < opnum && params != 0; i++)
1610 params = TREE_CHAIN (params);
1612 if (opnum == 0 || params == 0)
1614 warning ("operand number out of range in format");
1620 while (*format_chars != 0 && index (" +#0-", *format_chars) != 0)
1622 if (index (flag_chars, *format_chars) != 0)
1623 warning ("repeated `%c' flag in format", *format_chars++);
1626 i = strlen (flag_chars);
1627 flag_chars[i++] = *format_chars++;
1631 /* "If the space and + flags both appear,
1632 the space flag will be ignored." */
1633 if (index (flag_chars, ' ') != 0
1634 && index (flag_chars, '+') != 0)
1635 warning ("use of both ` ' and `+' flags in format");
1636 /* "If the 0 and - flags both appear,
1637 the 0 flag will be ignored." */
1638 if (index (flag_chars, '0') != 0
1639 && index (flag_chars, '-') != 0)
1640 warning ("use of both `0' and `-' flags in format");
1641 if (*format_chars == '*')
1644 /* "...a field width...may be indicated by an asterisk.
1645 In this case, an int argument supplies the field width..." */
1652 if (info->first_arg_num != 0)
1654 cur_param = TREE_VALUE (params);
1655 params = TREE_CHAIN (params);
1657 /* size_t is generally not valid here.
1658 It will work on most machines, because size_t and int
1659 have the same mode. But might as well warn anyway,
1660 since it will fail on other machines. */
1661 if ((TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1662 != integer_type_node)
1664 (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1665 != unsigned_type_node))
1666 warning ("field width is not type int (arg %d)", arg_num);
1671 while (ISDIGIT (*format_chars))
1677 if (*format_chars == '.')
1681 if (*format_chars != '*' && !ISDIGIT (*format_chars))
1682 warning ("`.' not followed by `*' or digit in format");
1683 /* "...a...precision...may be indicated by an asterisk.
1684 In this case, an int argument supplies the...precision." */
1685 if (*format_chars == '*')
1687 if (info->first_arg_num != 0)
1695 cur_param = TREE_VALUE (params);
1696 params = TREE_CHAIN (params);
1698 if (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1699 != integer_type_node)
1700 warning ("field width is not type int (arg %d)",
1706 while (ISDIGIT (*format_chars))
1714 if (info->format_type != strftime_format_type)
1716 if (*format_chars == 'h' || *format_chars == 'l')
1717 length_char = *format_chars++;
1718 else if (*format_chars == 'q' || *format_chars == 'L')
1720 length_char = *format_chars++;
1722 warning ("ANSI C does not support the `%c' length modifier",
1725 else if (*format_chars == 'Z')
1727 length_char = *format_chars++;
1729 warning ("ANSI C does not support the `Z' length modifier");
1733 if (length_char == 'l' && *format_chars == 'l')
1735 length_char = 'q', format_chars++;
1736 /* FIXME: Is allowed in ISO C 9x. */
1738 warning ("ANSI C does not support the `ll' length modifier");
1740 else if (length_char == 'h' && *format_chars == 'h')
1742 length_char = 'H', format_chars++;
1743 /* FIXME: Is allowed in ISO C 9x. */
1745 warning ("ANSI C does not support the `hh' length modifier");
1747 if (*format_chars == 'a' && info->format_type == scanf_format_type)
1749 if (format_chars[1] == 's' || format_chars[1] == 'S'
1750 || format_chars[1] == '[')
1752 /* `a' is used as a flag. */
1757 if (suppressed && length_char != 0)
1758 warning ("use of `*' and `%c' together in format", length_char);
1760 format_char = *format_chars;
1761 if (format_char == 0
1762 || (info->format_type != strftime_format_type && format_char == '%'))
1764 warning ("conversion lacks type at end of format");
1767 /* The m, C, and S formats are GNU extensions. */
1768 if (pedantic && info->format_type != strftime_format_type
1769 && (format_char == 'm' || format_char == 'C' || format_char == 'S'))
1770 warning ("ANSI C does not support the `%c' format", format_char);
1771 /* ??? The a and A formats are C9X extensions, and should be allowed
1772 when a C9X option is added. */
1773 if (pedantic && info->format_type != strftime_format_type
1774 && (format_char == 'a' || format_char == 'A'))
1775 warning ("ANSI C does not support the `%c' format", format_char);
1777 switch (info->format_type)
1779 case printf_format_type:
1780 fci = print_char_table;
1782 case scanf_format_type:
1783 fci = scan_char_table;
1785 case strftime_format_type:
1786 fci = time_char_table;
1791 while (fci->format_chars != 0
1792 && index (fci->format_chars, format_char) == 0)
1794 if (fci->format_chars == 0)
1796 if (format_char >= 040 && format_char < 0177)
1797 warning ("unknown conversion type character `%c' in format",
1800 warning ("unknown conversion type character 0x%x in format",
1806 if (index (fci->flag_chars, 'G') != 0)
1807 warning ("ANSI C does not support `%%%c'", format_char);
1808 if (index (fci->flag_chars, 'o') != 0
1809 && index (flag_chars, 'O') != 0)
1810 warning ("ANSI C does not support `%%O%c'", format_char);
1812 if (wide && index (fci->flag_chars, 'w') == 0)
1813 warning ("width used with `%c' format", format_char);
1814 if (index (fci->flag_chars, '2') != 0)
1815 warning ("`%%%c' yields only last 2 digits of year", format_char);
1816 else if (index (fci->flag_chars, '3') != 0)
1817 warning ("`%%%c' yields only last 2 digits of year in some locales",
1819 if (precise && index (fci->flag_chars, 'p') == 0)
1820 warning ("precision used with `%c' format", format_char);
1821 if (aflag && index (fci->flag_chars, 'a') == 0)
1823 warning ("`a' flag used with `%c' format", format_char);
1824 /* To simplify the following code. */
1827 /* The a flag is a GNU extension. */
1828 else if (pedantic && aflag)
1829 warning ("ANSI C does not support the `a' flag");
1830 if (info->format_type == scanf_format_type && format_char == '[')
1832 /* Skip over scan set, in case it happens to have '%' in it. */
1833 if (*format_chars == '^')
1835 /* Find closing bracket; if one is hit immediately, then
1836 it's part of the scan set rather than a terminator. */
1837 if (*format_chars == ']')
1839 while (*format_chars && *format_chars != ']')
1841 if (*format_chars != ']')
1842 /* The end of the format string was reached. */
1843 warning ("no closing `]' for `%%[' format");
1847 if (index (fci->flag_chars, '*') == 0)
1848 warning ("suppression of `%c' conversion in format", format_char);
1851 for (i = 0; flag_chars[i] != 0; ++i)
1853 if (index (fci->flag_chars, flag_chars[i]) == 0)
1854 warning ("flag `%c' used with type `%c'",
1855 flag_chars[i], format_char);
1857 if (info->format_type == strftime_format_type)
1859 if (precise && index (flag_chars, '0') != 0
1860 && (format_char == 'd' || format_char == 'i'
1861 || format_char == 'o' || format_char == 'u'
1862 || format_char == 'x' || format_char == 'X'))
1863 warning ("`0' flag ignored with precision specifier and `%c' format",
1865 switch (length_char)
1867 default: wanted_type = fci->nolen ? *(fci->nolen) : 0; break;
1868 case 'H': wanted_type = fci->hhlen ? *(fci->hhlen) : 0; break;
1869 case 'h': wanted_type = fci->hlen ? *(fci->hlen) : 0; break;
1870 case 'l': wanted_type = fci->llen ? *(fci->llen) : 0; break;
1871 case 'q': wanted_type = fci->qlen ? *(fci->qlen) : 0; break;
1872 case 'L': wanted_type = fci->bigllen ? *(fci->bigllen) : 0; break;
1873 case 'Z': wanted_type = fci->zlen ? *fci->zlen : 0; break;
1875 if (wanted_type == 0)
1876 warning ("use of `%c' length character with `%c' type character",
1877 length_char, format_char);
1879 /* Finally. . .check type of argument against desired type! */
1880 if (info->first_arg_num == 0)
1882 if (fci->pointer_count == 0 && wanted_type == void_type_node)
1883 /* This specifier takes no argument. */
1890 cur_param = TREE_VALUE (params);
1891 params = TREE_CHAIN (params);
1893 cur_type = TREE_TYPE (cur_param);
1895 STRIP_NOPS (cur_param);
1897 /* Check the types of any additional pointer arguments
1898 that precede the "real" argument. */
1899 for (i = 0; i < fci->pointer_count + aflag; ++i)
1901 if (TREE_CODE (cur_type) == POINTER_TYPE)
1903 cur_type = TREE_TYPE (cur_type);
1905 if (cur_param != 0 && TREE_CODE (cur_param) == ADDR_EXPR)
1906 cur_param = TREE_OPERAND (cur_param, 0);
1912 if (TREE_CODE (cur_type) != ERROR_MARK)
1913 warning ((fci->pointer_count + aflag == 1
1914 ? "format argument is not a pointer (arg %d)"
1915 : "format argument is not a pointer to a pointer (arg %d)"),
1920 /* See if this is an attempt to write into a const type with
1921 scanf or with printf "%n". */
1922 if ((info->format_type == scanf_format_type
1923 || (info->format_type == printf_format_type
1924 && format_char == 'n'))
1925 && i == fci->pointer_count + aflag
1927 && TREE_CODE (cur_type) != ERROR_MARK
1928 && (TYPE_READONLY (cur_type)
1930 && (TREE_CODE_CLASS (TREE_CODE (cur_param)) == 'c'
1931 || (TREE_CODE_CLASS (TREE_CODE (cur_param)) == 'd'
1932 && TREE_READONLY (cur_param))))))
1933 warning ("writing into constant object (arg %d)", arg_num);
1935 /* Check the type of the "real" argument, if there's a type we want. */
1936 if (i == fci->pointer_count + aflag && wanted_type != 0
1937 && TREE_CODE (cur_type) != ERROR_MARK
1938 && wanted_type != TYPE_MAIN_VARIANT (cur_type)
1939 /* If we want `void *', allow any pointer type.
1940 (Anything else would already have got a warning.) */
1941 && ! (wanted_type == void_type_node
1942 && fci->pointer_count > 0)
1943 /* Don't warn about differences merely in signedness. */
1944 && !(TREE_CODE (wanted_type) == INTEGER_TYPE
1945 && TREE_CODE (TYPE_MAIN_VARIANT (cur_type)) == INTEGER_TYPE
1946 && (TREE_UNSIGNED (wanted_type)
1947 ? wanted_type == (cur_type = unsigned_type (cur_type))
1948 : wanted_type == (cur_type = signed_type (cur_type))))
1949 /* Likewise, "signed char", "unsigned char" and "char" are
1950 equivalent but the above test won't consider them equivalent. */
1951 && ! (wanted_type == char_type_node
1952 && (TYPE_MAIN_VARIANT (cur_type) == signed_char_type_node
1953 || TYPE_MAIN_VARIANT (cur_type) == unsigned_char_type_node)))
1955 register const char *this;
1956 register const char *that;
1958 this = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (wanted_type)));
1960 if (TREE_CODE (cur_type) != ERROR_MARK
1961 && TYPE_NAME (cur_type) != 0
1962 && TREE_CODE (cur_type) != INTEGER_TYPE
1963 && !(TREE_CODE (cur_type) == POINTER_TYPE
1964 && TREE_CODE (TREE_TYPE (cur_type)) == INTEGER_TYPE))
1966 if (TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL
1967 && DECL_NAME (TYPE_NAME (cur_type)) != 0)
1968 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
1970 that = IDENTIFIER_POINTER (TYPE_NAME (cur_type));
1973 /* A nameless type can't possibly match what the format wants.
1974 So there will be a warning for it.
1975 Make up a string to describe vaguely what it is. */
1978 if (TREE_CODE (cur_type) == POINTER_TYPE)
1981 that = "different type";
1984 /* Make the warning better in case of mismatch of int vs long. */
1985 if (TREE_CODE (cur_type) == INTEGER_TYPE
1986 && TREE_CODE (wanted_type) == INTEGER_TYPE
1987 && TYPE_PRECISION (cur_type) == TYPE_PRECISION (wanted_type)
1988 && TYPE_NAME (cur_type) != 0
1989 && TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL)
1990 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
1992 if (strcmp (this, that) != 0)
1993 warning ("%s format, %s arg (arg %d)", this, that, arg_num);
1998 /* Print a warning if a constant expression had overflow in folding.
1999 Invoke this function on every expression that the language
2000 requires to be a constant expression.
2001 Note the ANSI C standard says it is erroneous for a
2002 constant expression to overflow. */
2005 constant_expression_warning (value)
2008 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
2009 || TREE_CODE (value) == COMPLEX_CST)
2010 && TREE_CONSTANT_OVERFLOW (value) && pedantic)
2011 pedwarn ("overflow in constant expression");
2014 /* Print a warning if an expression had overflow in folding.
2015 Invoke this function on every expression that
2016 (1) appears in the source code, and
2017 (2) might be a constant expression that overflowed, and
2018 (3) is not already checked by convert_and_check;
2019 however, do not invoke this function on operands of explicit casts. */
2022 overflow_warning (value)
2025 if ((TREE_CODE (value) == INTEGER_CST
2026 || (TREE_CODE (value) == COMPLEX_CST
2027 && TREE_CODE (TREE_REALPART (value)) == INTEGER_CST))
2028 && TREE_OVERFLOW (value))
2030 TREE_OVERFLOW (value) = 0;
2031 if (skip_evaluation == 0)
2032 warning ("integer overflow in expression");
2034 else if ((TREE_CODE (value) == REAL_CST
2035 || (TREE_CODE (value) == COMPLEX_CST
2036 && TREE_CODE (TREE_REALPART (value)) == REAL_CST))
2037 && TREE_OVERFLOW (value))
2039 TREE_OVERFLOW (value) = 0;
2040 if (skip_evaluation == 0)
2041 warning ("floating point overflow in expression");
2045 /* Print a warning if a large constant is truncated to unsigned,
2046 or if -Wconversion is used and a constant < 0 is converted to unsigned.
2047 Invoke this function on every expression that might be implicitly
2048 converted to an unsigned type. */
2051 unsigned_conversion_warning (result, operand)
2052 tree result, operand;
2054 if (TREE_CODE (operand) == INTEGER_CST
2055 && TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
2056 && TREE_UNSIGNED (TREE_TYPE (result))
2057 && skip_evaluation == 0
2058 && !int_fits_type_p (operand, TREE_TYPE (result)))
2060 if (!int_fits_type_p (operand, signed_type (TREE_TYPE (result))))
2061 /* This detects cases like converting -129 or 256 to unsigned char. */
2062 warning ("large integer implicitly truncated to unsigned type");
2063 else if (warn_conversion)
2064 warning ("negative integer implicitly converted to unsigned type");
2068 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2069 Invoke this function on every expression that is converted implicitly,
2070 i.e. because of language rules and not because of an explicit cast. */
2073 convert_and_check (type, expr)
2076 tree t = convert (type, expr);
2077 if (TREE_CODE (t) == INTEGER_CST)
2079 if (TREE_OVERFLOW (t))
2081 TREE_OVERFLOW (t) = 0;
2083 /* Do not diagnose overflow in a constant expression merely
2084 because a conversion overflowed. */
2085 TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (expr);
2087 /* No warning for converting 0x80000000 to int. */
2088 if (!(TREE_UNSIGNED (type) < TREE_UNSIGNED (TREE_TYPE (expr))
2089 && TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
2090 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (expr))))
2091 /* If EXPR fits in the unsigned version of TYPE,
2092 don't warn unless pedantic. */
2094 || TREE_UNSIGNED (type)
2095 || ! int_fits_type_p (expr, unsigned_type (type)))
2096 && skip_evaluation == 0)
2097 warning ("overflow in implicit constant conversion");
2100 unsigned_conversion_warning (t, expr);
2106 c_expand_expr_stmt (expr)
2109 /* Do default conversion if safe and possibly important,
2110 in case within ({...}). */
2111 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE && lvalue_p (expr))
2112 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
2113 expr = default_conversion (expr);
2115 if (TREE_TYPE (expr) != error_mark_node
2116 && TYPE_SIZE (TREE_TYPE (expr)) == 0
2117 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
2118 error ("expression statement has incomplete type");
2120 expand_expr_stmt (expr);
2123 /* Validate the expression after `case' and apply default promotions. */
2126 check_case_value (value)
2129 if (value == NULL_TREE)
2132 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
2133 STRIP_TYPE_NOPS (value);
2135 if (TREE_CODE (value) != INTEGER_CST
2136 && value != error_mark_node)
2138 error ("case label does not reduce to an integer constant");
2139 value = error_mark_node;
2142 /* Promote char or short to int. */
2143 value = default_conversion (value);
2145 constant_expression_warning (value);
2150 /* Return an integer type with BITS bits of precision,
2151 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
2154 type_for_size (bits, unsignedp)
2158 if (bits == TYPE_PRECISION (integer_type_node))
2159 return unsignedp ? unsigned_type_node : integer_type_node;
2161 if (bits == TYPE_PRECISION (signed_char_type_node))
2162 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2164 if (bits == TYPE_PRECISION (short_integer_type_node))
2165 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2167 if (bits == TYPE_PRECISION (long_integer_type_node))
2168 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2170 if (bits == TYPE_PRECISION (long_long_integer_type_node))
2171 return (unsignedp ? long_long_unsigned_type_node
2172 : long_long_integer_type_node);
2174 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
2175 return (unsignedp ? widest_unsigned_literal_type_node
2176 : widest_integer_literal_type_node);
2178 if (bits <= TYPE_PRECISION (intQI_type_node))
2179 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2181 if (bits <= TYPE_PRECISION (intHI_type_node))
2182 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2184 if (bits <= TYPE_PRECISION (intSI_type_node))
2185 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2187 if (bits <= TYPE_PRECISION (intDI_type_node))
2188 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2193 /* Return a data type that has machine mode MODE.
2194 If the mode is an integer,
2195 then UNSIGNEDP selects between signed and unsigned types. */
2198 type_for_mode (mode, unsignedp)
2199 enum machine_mode mode;
2202 if (mode == TYPE_MODE (integer_type_node))
2203 return unsignedp ? unsigned_type_node : integer_type_node;
2205 if (mode == TYPE_MODE (signed_char_type_node))
2206 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2208 if (mode == TYPE_MODE (short_integer_type_node))
2209 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2211 if (mode == TYPE_MODE (long_integer_type_node))
2212 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2214 if (mode == TYPE_MODE (long_long_integer_type_node))
2215 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
2217 if (mode == TYPE_MODE (widest_integer_literal_type_node))
2218 return unsignedp ? widest_unsigned_literal_type_node
2219 : widest_integer_literal_type_node;
2221 if (mode == TYPE_MODE (intQI_type_node))
2222 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2224 if (mode == TYPE_MODE (intHI_type_node))
2225 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2227 if (mode == TYPE_MODE (intSI_type_node))
2228 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2230 if (mode == TYPE_MODE (intDI_type_node))
2231 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2233 #if HOST_BITS_PER_WIDE_INT >= 64
2234 if (mode == TYPE_MODE (intTI_type_node))
2235 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2238 if (mode == TYPE_MODE (float_type_node))
2239 return float_type_node;
2241 if (mode == TYPE_MODE (double_type_node))
2242 return double_type_node;
2244 if (mode == TYPE_MODE (long_double_type_node))
2245 return long_double_type_node;
2247 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
2248 return build_pointer_type (char_type_node);
2250 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
2251 return build_pointer_type (integer_type_node);
2256 /* Return an unsigned type the same as TYPE in other respects. */
2258 unsigned_type (type)
2261 tree type1 = TYPE_MAIN_VARIANT (type);
2262 if (type1 == signed_char_type_node || type1 == char_type_node)
2263 return unsigned_char_type_node;
2264 if (type1 == integer_type_node)
2265 return unsigned_type_node;
2266 if (type1 == short_integer_type_node)
2267 return short_unsigned_type_node;
2268 if (type1 == long_integer_type_node)
2269 return long_unsigned_type_node;
2270 if (type1 == long_long_integer_type_node)
2271 return long_long_unsigned_type_node;
2272 if (type1 == widest_integer_literal_type_node)
2273 return widest_unsigned_literal_type_node;
2274 #if HOST_BITS_PER_WIDE_INT >= 64
2275 if (type1 == intTI_type_node)
2276 return unsigned_intTI_type_node;
2278 if (type1 == intDI_type_node)
2279 return unsigned_intDI_type_node;
2280 if (type1 == intSI_type_node)
2281 return unsigned_intSI_type_node;
2282 if (type1 == intHI_type_node)
2283 return unsigned_intHI_type_node;
2284 if (type1 == intQI_type_node)
2285 return unsigned_intQI_type_node;
2287 return signed_or_unsigned_type (1, type);
2290 /* Return a signed type the same as TYPE in other respects. */
2296 tree type1 = TYPE_MAIN_VARIANT (type);
2297 if (type1 == unsigned_char_type_node || type1 == char_type_node)
2298 return signed_char_type_node;
2299 if (type1 == unsigned_type_node)
2300 return integer_type_node;
2301 if (type1 == short_unsigned_type_node)
2302 return short_integer_type_node;
2303 if (type1 == long_unsigned_type_node)
2304 return long_integer_type_node;
2305 if (type1 == long_long_unsigned_type_node)
2306 return long_long_integer_type_node;
2307 if (type1 == widest_unsigned_literal_type_node)
2308 return widest_integer_literal_type_node;
2309 #if HOST_BITS_PER_WIDE_INT >= 64
2310 if (type1 == unsigned_intTI_type_node)
2311 return intTI_type_node;
2313 if (type1 == unsigned_intDI_type_node)
2314 return intDI_type_node;
2315 if (type1 == unsigned_intSI_type_node)
2316 return intSI_type_node;
2317 if (type1 == unsigned_intHI_type_node)
2318 return intHI_type_node;
2319 if (type1 == unsigned_intQI_type_node)
2320 return intQI_type_node;
2322 return signed_or_unsigned_type (0, type);
2325 /* Return a type the same as TYPE except unsigned or
2326 signed according to UNSIGNEDP. */
2329 signed_or_unsigned_type (unsignedp, type)
2333 if (! INTEGRAL_TYPE_P (type)
2334 || TREE_UNSIGNED (type) == unsignedp)
2337 if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node))
2338 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2339 if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2340 return unsignedp ? unsigned_type_node : integer_type_node;
2341 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node))
2342 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2343 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node))
2344 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2345 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node))
2346 return (unsignedp ? long_long_unsigned_type_node
2347 : long_long_integer_type_node);
2348 if (TYPE_PRECISION (type) == TYPE_PRECISION (widest_integer_literal_type_node))
2349 return (unsignedp ? widest_unsigned_literal_type_node
2350 : widest_integer_literal_type_node);
2354 /* Return the minimum number of bits needed to represent VALUE in a
2355 signed or unsigned type, UNSIGNEDP says which. */
2358 min_precision (value, unsignedp)
2364 /* If the value is negative, compute its negative minus 1. The latter
2365 adjustment is because the absolute value of the largest negative value
2366 is one larger than the largest positive value. This is equivalent to
2367 a bit-wise negation, so use that operation instead. */
2369 if (tree_int_cst_sgn (value) < 0)
2370 value = fold (build1 (BIT_NOT_EXPR, TREE_TYPE (value), value));
2372 /* Return the number of bits needed, taking into account the fact
2373 that we need one more bit for a signed than unsigned type. */
2375 if (integer_zerop (value))
2377 else if (TREE_INT_CST_HIGH (value) != 0)
2378 log = HOST_BITS_PER_WIDE_INT + floor_log2 (TREE_INT_CST_HIGH (value));
2380 log = floor_log2 (TREE_INT_CST_LOW (value));
2382 return log + 1 + ! unsignedp;
2385 /* Print an error message for invalid operands to arith operation CODE.
2386 NOP_EXPR is used as a special case (see truthvalue_conversion). */
2389 binary_op_error (code)
2390 enum tree_code code;
2392 register const char *opname;
2397 error ("invalid truth-value expression");
2401 opname = "+"; break;
2403 opname = "-"; break;
2405 opname = "*"; break;
2407 opname = "max"; break;
2409 opname = "min"; break;
2411 opname = "=="; break;
2413 opname = "!="; break;
2415 opname = "<="; break;
2417 opname = ">="; break;
2419 opname = "<"; break;
2421 opname = ">"; break;
2423 opname = "<<"; break;
2425 opname = ">>"; break;
2426 case TRUNC_MOD_EXPR:
2427 case FLOOR_MOD_EXPR:
2428 opname = "%"; break;
2429 case TRUNC_DIV_EXPR:
2430 case FLOOR_DIV_EXPR:
2431 opname = "/"; break;
2433 opname = "&"; break;
2435 opname = "|"; break;
2436 case TRUTH_ANDIF_EXPR:
2437 opname = "&&"; break;
2438 case TRUTH_ORIF_EXPR:
2439 opname = "||"; break;
2441 opname = "^"; break;
2444 opname = "rotate"; break;
2446 opname = "unknown"; break;
2448 error ("invalid operands to binary %s", opname);
2451 /* Subroutine of build_binary_op, used for comparison operations.
2452 See if the operands have both been converted from subword integer types
2453 and, if so, perhaps change them both back to their original type.
2454 This function is also responsible for converting the two operands
2455 to the proper common type for comparison.
2457 The arguments of this function are all pointers to local variables
2458 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
2459 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
2461 If this function returns nonzero, it means that the comparison has
2462 a constant value. What this function returns is an expression for
2466 shorten_compare (op0_ptr, op1_ptr, restype_ptr, rescode_ptr)
2467 tree *op0_ptr, *op1_ptr;
2469 enum tree_code *rescode_ptr;
2472 tree op0 = *op0_ptr;
2473 tree op1 = *op1_ptr;
2474 int unsignedp0, unsignedp1;
2476 tree primop0, primop1;
2477 enum tree_code code = *rescode_ptr;
2479 /* Throw away any conversions to wider types
2480 already present in the operands. */
2482 primop0 = get_narrower (op0, &unsignedp0);
2483 primop1 = get_narrower (op1, &unsignedp1);
2485 /* Handle the case that OP0 does not *contain* a conversion
2486 but it *requires* conversion to FINAL_TYPE. */
2488 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
2489 unsignedp0 = TREE_UNSIGNED (TREE_TYPE (op0));
2490 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
2491 unsignedp1 = TREE_UNSIGNED (TREE_TYPE (op1));
2493 /* If one of the operands must be floated, we cannot optimize. */
2494 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
2495 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
2497 /* If first arg is constant, swap the args (changing operation
2498 so value is preserved), for canonicalization. Don't do this if
2499 the second arg is 0. */
2501 if (TREE_CONSTANT (primop0)
2502 && ! integer_zerop (primop1) && ! real_zerop (primop1))
2504 register tree tem = primop0;
2505 register int temi = unsignedp0;
2513 unsignedp0 = unsignedp1;
2536 *rescode_ptr = code;
2539 /* If comparing an integer against a constant more bits wide,
2540 maybe we can deduce a value of 1 or 0 independent of the data.
2541 Or else truncate the constant now
2542 rather than extend the variable at run time.
2544 This is only interesting if the constant is the wider arg.
2545 Also, it is not safe if the constant is unsigned and the
2546 variable arg is signed, since in this case the variable
2547 would be sign-extended and then regarded as unsigned.
2548 Our technique fails in this case because the lowest/highest
2549 possible unsigned results don't follow naturally from the
2550 lowest/highest possible values of the variable operand.
2551 For just EQ_EXPR and NE_EXPR there is another technique that
2552 could be used: see if the constant can be faithfully represented
2553 in the other operand's type, by truncating it and reextending it
2554 and see if that preserves the constant's value. */
2556 if (!real1 && !real2
2557 && TREE_CODE (primop1) == INTEGER_CST
2558 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
2560 int min_gt, max_gt, min_lt, max_lt;
2561 tree maxval, minval;
2562 /* 1 if comparison is nominally unsigned. */
2563 int unsignedp = TREE_UNSIGNED (*restype_ptr);
2566 type = signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0));
2568 /* If TYPE is an enumeration, then we need to get its min/max
2569 values from it's underlying integral type, not the enumerated
2571 if (TREE_CODE (type) == ENUMERAL_TYPE)
2572 type = type_for_size (TYPE_PRECISION (type), unsignedp0);
2574 maxval = TYPE_MAX_VALUE (type);
2575 minval = TYPE_MIN_VALUE (type);
2577 if (unsignedp && !unsignedp0)
2578 *restype_ptr = signed_type (*restype_ptr);
2580 if (TREE_TYPE (primop1) != *restype_ptr)
2581 primop1 = convert (*restype_ptr, primop1);
2582 if (type != *restype_ptr)
2584 minval = convert (*restype_ptr, minval);
2585 maxval = convert (*restype_ptr, maxval);
2588 if (unsignedp && unsignedp0)
2590 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
2591 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
2592 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
2593 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
2597 min_gt = INT_CST_LT (primop1, minval);
2598 max_gt = INT_CST_LT (primop1, maxval);
2599 min_lt = INT_CST_LT (minval, primop1);
2600 max_lt = INT_CST_LT (maxval, primop1);
2604 /* This used to be a switch, but Genix compiler can't handle that. */
2605 if (code == NE_EXPR)
2607 if (max_lt || min_gt)
2608 val = boolean_true_node;
2610 else if (code == EQ_EXPR)
2612 if (max_lt || min_gt)
2613 val = boolean_false_node;
2615 else if (code == LT_EXPR)
2618 val = boolean_true_node;
2620 val = boolean_false_node;
2622 else if (code == GT_EXPR)
2625 val = boolean_true_node;
2627 val = boolean_false_node;
2629 else if (code == LE_EXPR)
2632 val = boolean_true_node;
2634 val = boolean_false_node;
2636 else if (code == GE_EXPR)
2639 val = boolean_true_node;
2641 val = boolean_false_node;
2644 /* If primop0 was sign-extended and unsigned comparison specd,
2645 we did a signed comparison above using the signed type bounds.
2646 But the comparison we output must be unsigned.
2648 Also, for inequalities, VAL is no good; but if the signed
2649 comparison had *any* fixed result, it follows that the
2650 unsigned comparison just tests the sign in reverse
2651 (positive values are LE, negative ones GE).
2652 So we can generate an unsigned comparison
2653 against an extreme value of the signed type. */
2655 if (unsignedp && !unsignedp0)
2662 primop1 = TYPE_MIN_VALUE (type);
2668 primop1 = TYPE_MAX_VALUE (type);
2675 type = unsigned_type (type);
2678 if (!max_gt && !unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
2680 /* This is the case of (char)x >?< 0x80, which people used to use
2681 expecting old C compilers to change the 0x80 into -0x80. */
2682 if (val == boolean_false_node)
2683 warning ("comparison is always false due to limited range of data type");
2684 if (val == boolean_true_node)
2685 warning ("comparison is always true due to limited range of data type");
2688 if (!min_lt && unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
2690 /* This is the case of (unsigned char)x >?< -1 or < 0. */
2691 if (val == boolean_false_node)
2692 warning ("comparison is always false due to limited range of data type");
2693 if (val == boolean_true_node)
2694 warning ("comparison is always true due to limited range of data type");
2699 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2700 if (TREE_SIDE_EFFECTS (primop0))
2701 return build (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
2705 /* Value is not predetermined, but do the comparison
2706 in the type of the operand that is not constant.
2707 TYPE is already properly set. */
2709 else if (real1 && real2
2710 && (TYPE_PRECISION (TREE_TYPE (primop0))
2711 == TYPE_PRECISION (TREE_TYPE (primop1))))
2712 type = TREE_TYPE (primop0);
2714 /* If args' natural types are both narrower than nominal type
2715 and both extend in the same manner, compare them
2716 in the type of the wider arg.
2717 Otherwise must actually extend both to the nominal
2718 common type lest different ways of extending
2720 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
2722 else if (unsignedp0 == unsignedp1 && real1 == real2
2723 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
2724 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
2726 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
2727 type = signed_or_unsigned_type (unsignedp0
2728 || TREE_UNSIGNED (*restype_ptr),
2730 /* Make sure shorter operand is extended the right way
2731 to match the longer operand. */
2732 primop0 = convert (signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0)),
2734 primop1 = convert (signed_or_unsigned_type (unsignedp1, TREE_TYPE (primop1)),
2739 /* Here we must do the comparison on the nominal type
2740 using the args exactly as we received them. */
2741 type = *restype_ptr;
2745 if (!real1 && !real2 && integer_zerop (primop1)
2746 && TREE_UNSIGNED (*restype_ptr))
2752 /* All unsigned values are >= 0, so we warn if extra warnings
2753 are requested. However, if OP0 is a constant that is
2754 >= 0, the signedness of the comparison isn't an issue,
2755 so suppress the warning. */
2757 && ! (TREE_CODE (primop0) == INTEGER_CST
2758 && ! TREE_OVERFLOW (convert (signed_type (type),
2760 warning ("comparison of unsigned expression >= 0 is always true");
2761 value = boolean_true_node;
2766 && ! (TREE_CODE (primop0) == INTEGER_CST
2767 && ! TREE_OVERFLOW (convert (signed_type (type),
2769 warning ("comparison of unsigned expression < 0 is always false");
2770 value = boolean_false_node;
2779 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2780 if (TREE_SIDE_EFFECTS (primop0))
2781 return build (COMPOUND_EXPR, TREE_TYPE (value),
2788 *op0_ptr = convert (type, primop0);
2789 *op1_ptr = convert (type, primop1);
2791 *restype_ptr = boolean_type_node;
2796 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
2797 or validate its data type for an `if' or `while' statement or ?..: exp.
2799 This preparation consists of taking the ordinary
2800 representation of an expression expr and producing a valid tree
2801 boolean expression describing whether expr is nonzero. We could
2802 simply always do build_binary_op (NE_EXPR, expr, boolean_false_node, 1),
2803 but we optimize comparisons, &&, ||, and !.
2805 The resulting type should always be `boolean_type_node'. */
2808 truthvalue_conversion (expr)
2811 if (TREE_CODE (expr) == ERROR_MARK)
2814 #if 0 /* This appears to be wrong for C++. */
2815 /* These really should return error_mark_node after 2.4 is stable.
2816 But not all callers handle ERROR_MARK properly. */
2817 switch (TREE_CODE (TREE_TYPE (expr)))
2820 error ("struct type value used where scalar is required");
2821 return boolean_false_node;
2824 error ("union type value used where scalar is required");
2825 return boolean_false_node;
2828 error ("array type value used where scalar is required");
2829 return boolean_false_node;
2836 switch (TREE_CODE (expr))
2838 /* It is simpler and generates better code to have only TRUTH_*_EXPR
2839 or comparison expressions as truth values at this level. */
2842 /* A one-bit unsigned bit-field is already acceptable. */
2843 if (1 == TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (expr, 1)))
2844 && TREE_UNSIGNED (TREE_OPERAND (expr, 1)))
2850 /* It is simpler and generates better code to have only TRUTH_*_EXPR
2851 or comparison expressions as truth values at this level. */
2853 if (integer_zerop (TREE_OPERAND (expr, 1)))
2854 return build_unary_op (TRUTH_NOT_EXPR, TREE_OPERAND (expr, 0), 0);
2856 case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
2857 case TRUTH_ANDIF_EXPR:
2858 case TRUTH_ORIF_EXPR:
2859 case TRUTH_AND_EXPR:
2861 case TRUTH_XOR_EXPR:
2862 case TRUTH_NOT_EXPR:
2863 TREE_TYPE (expr) = boolean_type_node;
2870 return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
2873 return real_zerop (expr) ? boolean_false_node : boolean_true_node;
2876 /* If we are taking the address of a external decl, it might be zero
2877 if it is weak, so we cannot optimize. */
2878 if (TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (expr, 0))) == 'd'
2879 && DECL_EXTERNAL (TREE_OPERAND (expr, 0)))
2882 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0)))
2883 return build (COMPOUND_EXPR, boolean_type_node,
2884 TREE_OPERAND (expr, 0), boolean_true_node);
2886 return boolean_true_node;
2889 return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
2890 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
2891 truthvalue_conversion (TREE_OPERAND (expr, 0)),
2892 truthvalue_conversion (TREE_OPERAND (expr, 1)),
2899 /* These don't change whether an object is non-zero or zero. */
2900 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2904 /* These don't change whether an object is zero or non-zero, but
2905 we can't ignore them if their second arg has side-effects. */
2906 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
2907 return build (COMPOUND_EXPR, boolean_type_node, TREE_OPERAND (expr, 1),
2908 truthvalue_conversion (TREE_OPERAND (expr, 0)));
2910 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2913 /* Distribute the conversion into the arms of a COND_EXPR. */
2914 return fold (build (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
2915 truthvalue_conversion (TREE_OPERAND (expr, 1)),
2916 truthvalue_conversion (TREE_OPERAND (expr, 2))));
2919 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
2920 since that affects how `default_conversion' will behave. */
2921 if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE
2922 || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
2924 /* fall through... */
2926 /* If this is widening the argument, we can ignore it. */
2927 if (TYPE_PRECISION (TREE_TYPE (expr))
2928 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
2929 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2933 /* With IEEE arithmetic, x - x may not equal 0, so we can't optimize
2935 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
2936 && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE)
2938 /* fall through... */
2940 /* This and MINUS_EXPR can be changed into a comparison of the
2942 if (TREE_TYPE (TREE_OPERAND (expr, 0))
2943 == TREE_TYPE (TREE_OPERAND (expr, 1)))
2944 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2945 TREE_OPERAND (expr, 1), 1);
2946 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2947 fold (build1 (NOP_EXPR,
2948 TREE_TYPE (TREE_OPERAND (expr, 0)),
2949 TREE_OPERAND (expr, 1))), 1);
2952 if (integer_onep (TREE_OPERAND (expr, 1))
2953 && TREE_TYPE (expr) != boolean_type_node)
2954 /* Using convert here would cause infinite recursion. */
2955 return build1 (NOP_EXPR, boolean_type_node, expr);
2959 if (warn_parentheses && C_EXP_ORIGINAL_CODE (expr) == MODIFY_EXPR)
2960 warning ("suggest parentheses around assignment used as truth value");
2967 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
2969 tree tem = save_expr (expr);
2970 return (build_binary_op
2971 ((TREE_SIDE_EFFECTS (expr)
2972 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
2973 truthvalue_conversion (build_unary_op (REALPART_EXPR, tem, 0)),
2974 truthvalue_conversion (build_unary_op (IMAGPART_EXPR, tem, 0)),
2978 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
2982 /* Read the rest of a #-directive from input stream FINPUT.
2983 In normal use, the directive name and the white space after it
2984 have already been read, so they won't be included in the result.
2985 We allow for the fact that the directive line may contain
2986 a newline embedded within a character or string literal which forms
2987 a part of the directive.
2989 The value is a string in a reusable buffer. It remains valid
2990 only until the next time this function is called. */
2991 unsigned char *yy_cur, *yy_lim;
2993 #define GETC() (yy_cur < yy_lim ? *yy_cur++ : yy_get_token ())
2994 #define UNGETC(c) ((c) == EOF ? 0 : yy_cur--)
3001 parse_in.limit = parse_in.token_buffer;
3002 cpp_token = cpp_get_token (&parse_in);
3003 if (cpp_token == CPP_EOF)
3005 yy_lim = CPP_PWRITTEN (&parse_in);
3006 yy_cur = parse_in.token_buffer;
3007 if (yy_cur < yy_lim)
3013 get_directive_line ()
3015 static char *directive_buffer = NULL;
3016 static unsigned buffer_length = 0;
3018 register char *buffer_limit;
3019 register int looking_for = 0;
3020 register int char_escaped = 0;
3022 if (buffer_length == 0)
3024 directive_buffer = (char *)xmalloc (128);
3025 buffer_length = 128;
3028 buffer_limit = &directive_buffer[buffer_length];
3030 for (p = directive_buffer; ; )
3034 /* Make buffer bigger if it is full. */
3035 if (p >= buffer_limit)
3037 register unsigned bytes_used = (p - directive_buffer);
3041 = (char *)xrealloc (directive_buffer, buffer_length);
3042 p = &directive_buffer[bytes_used];
3043 buffer_limit = &directive_buffer[buffer_length];
3048 /* Discard initial whitespace. */
3049 if ((c == ' ' || c == '\t') && p == directive_buffer)
3052 /* Detect the end of the directive. */
3053 if (c == '\n' && looking_for == 0)
3062 return directive_buffer;
3064 /* Handle string and character constant syntax. */
3067 if (looking_for == c && !char_escaped)
3068 looking_for = 0; /* Found terminator... stop looking. */
3071 if (c == '\'' || c == '"')
3072 looking_for = c; /* Don't stop buffering until we see another
3073 another one of these (or an EOF). */
3075 /* Handle backslash. */
3076 char_escaped = (c == '\\' && ! char_escaped);
3080 /* Read the rest of a #-directive from input stream FINPUT.
3081 In normal use, the directive name and the white space after it
3082 have already been read, so they won't be included in the result.
3083 We allow for the fact that the directive line may contain
3084 a newline embedded within a character or string literal which forms
3085 a part of the directive.
3087 The value is a string in a reusable buffer. It remains valid
3088 only until the next time this function is called.
3090 The terminating character ('\n' or EOF) is left in FINPUT for the
3091 caller to re-read. */
3094 get_directive_line (finput)
3095 register FILE *finput;
3097 static char *directive_buffer = NULL;
3098 static unsigned buffer_length = 0;
3100 register char *buffer_limit;
3101 register int looking_for = 0;
3102 register int char_escaped = 0;
3104 if (buffer_length == 0)
3106 directive_buffer = (char *)xmalloc (128);
3107 buffer_length = 128;
3110 buffer_limit = &directive_buffer[buffer_length];
3112 for (p = directive_buffer; ; )
3116 /* Make buffer bigger if it is full. */
3117 if (p >= buffer_limit)
3119 register unsigned bytes_used = (p - directive_buffer);
3123 = (char *)xrealloc (directive_buffer, buffer_length);
3124 p = &directive_buffer[bytes_used];
3125 buffer_limit = &directive_buffer[buffer_length];
3130 /* Discard initial whitespace. */
3131 if ((c == ' ' || c == '\t') && p == directive_buffer)
3134 /* Detect the end of the directive. */
3135 if (looking_for == 0
3136 && (c == '\n' || c == EOF))
3145 return directive_buffer;
3147 /* Handle string and character constant syntax. */
3150 if (looking_for == c && !char_escaped)
3151 looking_for = 0; /* Found terminator... stop looking. */
3154 if (c == '\'' || c == '"')
3155 looking_for = c; /* Don't stop buffering until we see another
3156 one of these (or an EOF). */
3158 /* Handle backslash. */
3159 char_escaped = (c == '\\' && ! char_escaped);
3162 #endif /* !USE_CPPLIB */
3164 /* Make a variant type in the proper way for C/C++, propagating qualifiers
3165 down to the element type of an array. */
3168 c_build_qualified_type (type, type_quals)
3172 /* A restrict-qualified pointer type must be a pointer to object or
3173 incomplete type. Note that the use of POINTER_TYPE_P also allows
3174 REFERENCE_TYPEs, which is appropriate for C++. Unfortunately,
3175 the C++ front-end also use POINTER_TYPE for pointer-to-member
3176 values, so even though it should be illegal to use `restrict'
3177 with such an entity we don't flag that here. Thus, special case
3178 code for that case is required in the C++ front-end. */
3179 if ((type_quals & TYPE_QUAL_RESTRICT)
3180 && (!POINTER_TYPE_P (type)
3181 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
3183 error ("invalid use of `restrict'");
3184 type_quals &= ~TYPE_QUAL_RESTRICT;
3187 if (TREE_CODE (type) == ARRAY_TYPE)
3188 return build_array_type (c_build_qualified_type (TREE_TYPE (type),
3190 TYPE_DOMAIN (type));
3191 return build_qualified_type (type, type_quals);
3194 /* Apply the TYPE_QUALS to the new DECL. */
3197 c_apply_type_quals_to_decl (type_quals, decl)
3201 if (type_quals & TYPE_QUAL_CONST)
3202 TREE_READONLY (decl) = 1;
3203 if (type_quals & TYPE_QUAL_VOLATILE)
3205 TREE_SIDE_EFFECTS (decl) = 1;
3206 TREE_THIS_VOLATILE (decl) = 1;
3208 if (type_quals & TYPE_QUAL_RESTRICT)
3210 if (!TREE_TYPE (decl)
3211 || !POINTER_TYPE_P (TREE_TYPE (decl))
3212 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (TREE_TYPE (decl))))
3213 error ("invalid use of `restrict'");
3214 else if (flag_strict_aliasing)
3216 /* No two restricted pointers can point at the same thing.
3217 However, a restricted pointer can point at the same thing
3218 as an unrestricted pointer, if that unrestricted pointer
3219 is based on the restricted pointer. So, we make the
3220 alias set for the restricted pointer a subset of the
3221 alias set for the type pointed to by the type of the
3224 int pointed_to_alias_set
3225 = get_alias_set (TREE_TYPE (TREE_TYPE (decl)));
3227 if (!pointed_to_alias_set)
3228 /* It's not legal to make a subset of alias set zero. */
3232 DECL_POINTER_ALIAS_SET (decl) = new_alias_set ();
3233 record_alias_subset (pointed_to_alias_set,
3234 DECL_POINTER_ALIAS_SET (decl));
3240 /* T is an expression with pointer type. Find the DECL on which this
3241 expression is based. (For example, in `a[i]' this would be `a'.)
3242 If there is no such DECL, or a unique decl cannot be determined,
3243 NULL_TREE is retured. */
3246 c_find_base_decl (t)
3252 if (t == NULL_TREE || t == error_mark_node)
3255 if (!POINTER_TYPE_P (TREE_TYPE (t)))
3260 if (TREE_CODE (t) == FIELD_DECL
3261 || TREE_CODE (t) == PARM_DECL
3262 || TREE_CODE (t) == VAR_DECL)
3263 /* Aha, we found a pointer-typed declaration. */
3266 /* It would be nice to deal with COMPONENT_REFs here. If we could
3267 tell that `a' and `b' were the same, then `a->f' and `b->f' are
3270 /* Handle general expressions. */
3271 switch (TREE_CODE_CLASS (TREE_CODE (t)))
3276 for (i = tree_code_length [(int) TREE_CODE (t)]; --i >= 0;)
3278 tree d = c_find_base_decl (TREE_OPERAND (t, i));
3283 else if (d && d != decl)
3284 /* Two different declarations. That's confusing; let's
3285 just assume we don't know what's going on. */
3298 /* Return the typed-based alias set for T, which may be an expression
3308 if (t == error_mark_node)
3311 type = (TREE_CODE_CLASS (TREE_CODE (t)) == 't')
3312 ? t : TREE_TYPE (t);
3314 if (type == error_mark_node)
3317 /* Deal with special cases first; for certain kinds of references
3318 we're interested in more than just the type. */
3320 if (TREE_CODE (t) == BIT_FIELD_REF)
3321 /* Perhaps reads and writes to this piece of data alias fields
3322 neighboring the bitfield. Perhaps that's impossible. For now,
3323 let's just assume that bitfields can alias everything, which is
3324 the conservative assumption. */
3327 /* Permit type-punning when accessing a union, provided the access
3328 is directly through the union. For example, this code does not
3329 permit taking the address of a union member and then storing
3330 through it. Even the type-punning allowed here is a GCC
3331 extension, albeit a common and useful one; the C standard says
3332 that such accesses have implementation-defined behavior. */
3334 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
3335 u = TREE_OPERAND (u, 0))
3336 if (TREE_CODE (u) == COMPONENT_REF
3337 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
3340 if (TREE_CODE (t) == INDIRECT_REF)
3342 /* Check for accesses through restrict-qualified pointers. */
3343 tree decl = c_find_base_decl (TREE_OPERAND (t, 0));
3345 if (decl && DECL_POINTER_ALIAS_SET_KNOWN_P (decl))
3346 /* We use the alias set indicated in the declaration. */
3347 return DECL_POINTER_ALIAS_SET (decl);
3350 /* From here on, only the type matters. */
3352 if (TREE_CODE (t) == COMPONENT_REF
3353 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (t, 1)))
3354 /* Since build_modify_expr calls get_unwidened for stores to
3355 component references, the type of a bit field can be changed
3356 from (say) `unsigned int : 16' to `unsigned short' or from
3357 `enum E : 16' to `short'. We want the real type of the
3358 bit-field in this case, not some the integral equivalent. */
3359 type = DECL_BIT_FIELD_TYPE (TREE_OPERAND (t, 1));
3361 if (TYPE_ALIAS_SET_KNOWN_P (type))
3362 /* If we've already calculated the value, just return it. */
3363 return TYPE_ALIAS_SET (type);
3364 else if (TYPE_MAIN_VARIANT (type) != type)
3365 /* The C standard specifically allows aliasing between
3366 cv-qualified variants of types. */
3367 TYPE_ALIAS_SET (type) = c_get_alias_set (TYPE_MAIN_VARIANT (type));
3368 else if (TREE_CODE (type) == INTEGER_TYPE)
3370 tree signed_variant;
3372 /* The C standard specifically allows aliasing between signed and
3373 unsigned variants of the same type. We treat the signed
3374 variant as canonical. */
3375 signed_variant = signed_type (type);
3377 if (signed_variant != type)
3378 TYPE_ALIAS_SET (type) = c_get_alias_set (signed_variant);
3379 else if (signed_variant == signed_char_type_node)
3380 /* The C standard guarantess that any object may be accessed
3381 via an lvalue that has character type. We don't have to
3382 check for unsigned_char_type_node or char_type_node because
3383 we are specifically looking at the signed variant. */
3384 TYPE_ALIAS_SET (type) = 0;
3386 else if (TREE_CODE (type) == ARRAY_TYPE)
3387 /* Anything that can alias one of the array elements can alias
3388 the entire array as well. */
3389 TYPE_ALIAS_SET (type) = c_get_alias_set (TREE_TYPE (type));
3390 else if (TREE_CODE (type) == FUNCTION_TYPE)
3391 /* There are no objects of FUNCTION_TYPE, so there's no point in
3392 using up an alias set for them. (There are, of course,
3393 pointers and references to functions, but that's
3395 TYPE_ALIAS_SET (type) = 0;
3396 else if (TREE_CODE (type) == RECORD_TYPE
3397 || TREE_CODE (type) == UNION_TYPE)
3398 /* If TYPE is a struct or union type then we're reading or
3399 writing an entire struct. Thus, we don't know anything about
3400 aliasing. (In theory, such an access can only alias objects
3401 whose type is the same as one of the fields, recursively, but
3402 we don't yet make any use of that information.) */
3403 TYPE_ALIAS_SET (type) = 0;
3405 if (!TYPE_ALIAS_SET_KNOWN_P (type))
3406 /* TYPE is something we haven't seen before. Put it in a new
3408 TYPE_ALIAS_SET (type) = new_alias_set ();
3410 return TYPE_ALIAS_SET (type);
3413 /* Build tree nodes and builtin functions common to both C and C++ language
3415 CPLUS_MODE is nonzero if we are called from the C++ frontend, we generate
3416 some stricter prototypes in that case.
3417 NO_BUILTINS and NO_NONANSI_BUILTINS contain the respective values of
3418 the language frontend flags flag_no_builtin and
3419 flag_no_nonansi_builtin. */
3421 c_common_nodes_and_builtins (cplus_mode, no_builtins, no_nonansi_builtins)
3422 int cplus_mode, no_builtins, no_nonansi_builtins;
3425 tree memcpy_ftype, memset_ftype, strlen_ftype;
3426 tree endlink, int_endlink, double_endlink, unsigned_endlink;
3427 tree sizetype_endlink;
3428 tree ptr_ftype, ptr_ftype_unsigned;
3429 tree void_ftype_any, void_ftype_int;
3430 tree double_ftype_double, double_ftype_double_double;
3431 tree float_ftype_float, ldouble_ftype_ldouble;
3432 tree int_ftype_cptr_cptr_sizet;
3433 tree int_ftype_string_string, string_ftype_ptr_ptr;
3434 tree long_ftype_long;
3435 /* Either char* or void*. */
3436 tree traditional_ptr_type_node;
3437 tree va_list_ptr_type_node;
3438 tree va_list_arg_type_node;
3440 #ifdef BUILD_VA_LIST_TYPE
3441 BUILD_VA_LIST_TYPE(va_list_type_node);
3443 va_list_type_node = ptr_type_node;
3445 pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_va_list"),
3446 va_list_type_node));
3448 va_list_ptr_type_node = build_pointer_type (va_list_type_node);
3450 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
3451 va_list_arg_type_node = build_pointer_type (TREE_TYPE (va_list_type_node));
3453 va_list_arg_type_node = va_list_type_node;
3455 endlink = void_list_node;
3456 int_endlink = tree_cons (NULL_TREE, integer_type_node, endlink);
3457 double_endlink = tree_cons (NULL_TREE, double_type_node, endlink);
3458 unsigned_endlink = tree_cons (NULL_TREE, unsigned_type_node, endlink);
3460 ptr_ftype = build_function_type (ptr_type_node, NULL_TREE);
3461 ptr_ftype_unsigned = build_function_type (ptr_type_node, unsigned_endlink);
3462 sizetype_endlink = tree_cons (NULL_TREE, sizetype, endlink);
3463 /* We realloc here because sizetype could be int or unsigned. S'ok. */
3464 ptr_ftype_sizetype = build_function_type (ptr_type_node, sizetype_endlink);
3466 void_ftype_any = build_function_type (void_type_node, NULL_TREE);
3467 void_ftype = build_function_type (void_type_node, endlink);
3468 void_ftype_int = build_function_type (void_type_node, int_endlink);
3470 = build_function_type (void_type_node,
3471 tree_cons (NULL_TREE, ptr_type_node, endlink));
3474 = build_function_type (float_type_node,
3475 tree_cons (NULL_TREE, float_type_node, endlink));
3478 = build_function_type (double_type_node, double_endlink);
3480 ldouble_ftype_ldouble
3481 = build_function_type (long_double_type_node,
3482 tree_cons (NULL_TREE, long_double_type_node,
3485 double_ftype_double_double
3486 = build_function_type (double_type_node,
3487 tree_cons (NULL_TREE, double_type_node,
3491 = build_function_type (integer_type_node, int_endlink);
3494 = build_function_type (long_integer_type_node,
3495 tree_cons (NULL_TREE, long_integer_type_node,
3498 int_ftype_cptr_cptr_sizet
3499 = build_function_type (integer_type_node,
3500 tree_cons (NULL_TREE, const_ptr_type_node,
3501 tree_cons (NULL_TREE, const_ptr_type_node,
3502 tree_cons (NULL_TREE,
3506 /* Prototype for strcpy. */
3507 string_ftype_ptr_ptr
3508 = build_function_type (string_type_node,
3509 tree_cons (NULL_TREE, string_type_node,
3510 tree_cons (NULL_TREE,
3511 const_string_type_node,
3514 /* Prototype for strcmp. */
3515 int_ftype_string_string
3516 = build_function_type (integer_type_node,
3517 tree_cons (NULL_TREE, const_string_type_node,
3518 tree_cons (NULL_TREE,
3519 const_string_type_node,
3522 /* Prototype for strlen. */
3524 = build_function_type ((flag_traditional && ! cplus_mode
3525 ? integer_type_node : sizetype),
3526 tree_cons (NULL_TREE, const_string_type_node,
3529 traditional_ptr_type_node = (flag_traditional && ! cplus_mode
3530 ? string_type_node : ptr_type_node);
3532 /* Prototype for memcpy. */
3534 = build_function_type (traditional_ptr_type_node,
3535 tree_cons (NULL_TREE, ptr_type_node,
3536 tree_cons (NULL_TREE, const_ptr_type_node,
3537 sizetype_endlink)));
3539 /* Prototype for memset. */
3541 = build_function_type (traditional_ptr_type_node,
3542 tree_cons (NULL_TREE, ptr_type_node,
3543 tree_cons (NULL_TREE, integer_type_node,
3544 tree_cons (NULL_TREE,
3548 builtin_function ("__builtin_constant_p", default_function_type,
3549 BUILT_IN_CONSTANT_P, NULL_PTR);
3551 builtin_function ("__builtin_return_address", ptr_ftype_unsigned,
3552 BUILT_IN_RETURN_ADDRESS, NULL_PTR);
3554 builtin_function ("__builtin_frame_address", ptr_ftype_unsigned,
3555 BUILT_IN_FRAME_ADDRESS, NULL_PTR);
3557 builtin_function ("__builtin_alloca", ptr_ftype_sizetype,
3558 BUILT_IN_ALLOCA, "alloca");
3559 builtin_function ("__builtin_ffs", int_ftype_int, BUILT_IN_FFS, NULL_PTR);
3560 /* Define alloca, ffs as builtins.
3561 Declare _exit just to mark it as volatile. */
3562 if (! no_builtins && ! no_nonansi_builtins)
3564 temp = builtin_function ("alloca", ptr_ftype_sizetype,
3565 BUILT_IN_ALLOCA, NULL_PTR);
3566 /* Suppress error if redefined as a non-function. */
3567 DECL_BUILT_IN_NONANSI (temp) = 1;
3568 temp = builtin_function ("ffs", int_ftype_int, BUILT_IN_FFS, NULL_PTR);
3569 /* Suppress error if redefined as a non-function. */
3570 DECL_BUILT_IN_NONANSI (temp) = 1;
3571 temp = builtin_function ("_exit", void_ftype_int,
3572 NOT_BUILT_IN, NULL_PTR);
3573 TREE_THIS_VOLATILE (temp) = 1;
3574 TREE_SIDE_EFFECTS (temp) = 1;
3575 /* Suppress error if redefined as a non-function. */
3576 DECL_BUILT_IN_NONANSI (temp) = 1;
3579 builtin_function ("__builtin_abs", int_ftype_int, BUILT_IN_ABS, NULL_PTR);
3580 builtin_function ("__builtin_fabsf", float_ftype_float, BUILT_IN_FABS,
3582 builtin_function ("__builtin_fabs", double_ftype_double, BUILT_IN_FABS,
3584 builtin_function ("__builtin_fabsl", ldouble_ftype_ldouble, BUILT_IN_FABS,
3586 builtin_function ("__builtin_labs", long_ftype_long, BUILT_IN_LABS,
3588 builtin_function ("__builtin_saveregs", ptr_ftype, BUILT_IN_SAVEREGS,
3590 builtin_function ("__builtin_classify_type", default_function_type,
3591 BUILT_IN_CLASSIFY_TYPE, NULL_PTR);
3592 builtin_function ("__builtin_next_arg", ptr_ftype, BUILT_IN_NEXT_ARG,
3594 builtin_function ("__builtin_args_info", int_ftype_int, BUILT_IN_ARGS_INFO,
3596 builtin_function ("__builtin_setjmp",
3597 build_function_type (integer_type_node,
3598 tree_cons (NULL_TREE, ptr_type_node,
3600 BUILT_IN_SETJMP, NULL_PTR);
3601 builtin_function ("__builtin_longjmp",
3602 build_function_type (void_type_node,
3603 tree_cons (NULL_TREE, ptr_type_node,
3604 tree_cons (NULL_TREE,
3607 BUILT_IN_LONGJMP, NULL_PTR);
3608 builtin_function ("__builtin_trap", void_ftype, BUILT_IN_TRAP, NULL_PTR);
3610 /* Untyped call and return. */
3611 builtin_function ("__builtin_apply_args", ptr_ftype,
3612 BUILT_IN_APPLY_ARGS, NULL_PTR);
3614 temp = tree_cons (NULL_TREE,
3615 build_pointer_type (build_function_type (void_type_node,
3617 tree_cons (NULL_TREE,
3619 tree_cons (NULL_TREE,
3622 builtin_function ("__builtin_apply",
3623 build_function_type (ptr_type_node, temp),
3624 BUILT_IN_APPLY, NULL_PTR);
3625 builtin_function ("__builtin_return", void_ftype_ptr,
3626 BUILT_IN_RETURN, NULL_PTR);
3628 /* Support for varargs.h and stdarg.h. */
3629 builtin_function ("__builtin_varargs_start",
3630 build_function_type (void_type_node,
3631 tree_cons (NULL_TREE,
3632 va_list_ptr_type_node,
3634 BUILT_IN_VARARGS_START, NULL_PTR);
3636 builtin_function ("__builtin_stdarg_start",
3637 build_function_type (void_type_node,
3638 tree_cons (NULL_TREE,
3639 va_list_ptr_type_node,
3641 BUILT_IN_STDARG_START, NULL_PTR);
3643 builtin_function ("__builtin_va_end",
3644 build_function_type (void_type_node,
3645 tree_cons (NULL_TREE,
3646 va_list_arg_type_node,
3648 BUILT_IN_VA_END, NULL_PTR);
3650 builtin_function ("__builtin_va_copy",
3651 build_function_type (void_type_node,
3652 tree_cons (NULL_TREE,
3653 va_list_ptr_type_node,
3654 tree_cons (NULL_TREE,
3655 va_list_arg_type_node,
3657 BUILT_IN_VA_COPY, NULL_PTR);
3659 /* Currently under experimentation. */
3660 builtin_function ("__builtin_memcpy", memcpy_ftype, BUILT_IN_MEMCPY,
3662 builtin_function ("__builtin_memcmp", int_ftype_cptr_cptr_sizet,
3663 BUILT_IN_MEMCMP, "memcmp");
3664 builtin_function ("__builtin_memset", memset_ftype, BUILT_IN_MEMSET,
3666 builtin_function ("__builtin_strcmp", int_ftype_string_string,
3667 BUILT_IN_STRCMP, "strcmp");
3668 builtin_function ("__builtin_strcpy", string_ftype_ptr_ptr,
3669 BUILT_IN_STRCPY, "strcpy");
3670 builtin_function ("__builtin_strlen", strlen_ftype,
3671 BUILT_IN_STRLEN, "strlen");
3672 builtin_function ("__builtin_sqrtf", float_ftype_float,
3673 BUILT_IN_FSQRT, "sqrtf");
3674 builtin_function ("__builtin_fsqrt", double_ftype_double,
3675 BUILT_IN_FSQRT, "sqrt");
3676 builtin_function ("__builtin_sqrtl", ldouble_ftype_ldouble,
3677 BUILT_IN_FSQRT, "sqrtl");
3678 builtin_function ("__builtin_sinf", float_ftype_float,
3679 BUILT_IN_SIN, "sinf");
3680 builtin_function ("__builtin_sin", double_ftype_double,
3681 BUILT_IN_SIN, "sin");
3682 builtin_function ("__builtin_sinl", ldouble_ftype_ldouble,
3683 BUILT_IN_SIN, "sinl");
3684 builtin_function ("__builtin_cosf", float_ftype_float,
3685 BUILT_IN_COS, "cosf");
3686 builtin_function ("__builtin_cos", double_ftype_double,
3687 BUILT_IN_COS, "cos");
3688 builtin_function ("__builtin_cosl", ldouble_ftype_ldouble,
3689 BUILT_IN_COS, "cosl");
3693 builtin_function ("abs", int_ftype_int, BUILT_IN_ABS, NULL_PTR);
3694 builtin_function ("fabsf", float_ftype_float, BUILT_IN_FABS, NULL_PTR);
3695 builtin_function ("fabs", double_ftype_double, BUILT_IN_FABS, NULL_PTR);
3696 builtin_function ("fabsl", ldouble_ftype_ldouble, BUILT_IN_FABS,
3698 builtin_function ("labs", long_ftype_long, BUILT_IN_LABS, NULL_PTR);
3699 builtin_function ("memcpy", memcpy_ftype, BUILT_IN_MEMCPY, NULL_PTR);
3700 builtin_function ("memcmp", int_ftype_cptr_cptr_sizet, BUILT_IN_MEMCMP,
3702 builtin_function ("memset", memset_ftype, BUILT_IN_MEMSET, NULL_PTR);
3703 builtin_function ("strcmp", int_ftype_string_string, BUILT_IN_STRCMP,
3705 builtin_function ("strcpy", string_ftype_ptr_ptr, BUILT_IN_STRCPY,
3707 builtin_function ("strlen", strlen_ftype, BUILT_IN_STRLEN, NULL_PTR);
3708 builtin_function ("sqrtf", float_ftype_float, BUILT_IN_FSQRT, NULL_PTR);
3709 builtin_function ("sqrt", double_ftype_double, BUILT_IN_FSQRT, NULL_PTR);
3710 builtin_function ("sqrtl", ldouble_ftype_ldouble, BUILT_IN_FSQRT,
3712 builtin_function ("sinf", float_ftype_float, BUILT_IN_SIN, NULL_PTR);
3713 builtin_function ("sin", double_ftype_double, BUILT_IN_SIN, NULL_PTR);
3714 builtin_function ("sinl", ldouble_ftype_ldouble, BUILT_IN_SIN, NULL_PTR);
3715 builtin_function ("cosf", float_ftype_float, BUILT_IN_COS, NULL_PTR);
3716 builtin_function ("cos", double_ftype_double, BUILT_IN_COS, NULL_PTR);
3717 builtin_function ("cosl", ldouble_ftype_ldouble, BUILT_IN_COS, NULL_PTR);
3719 /* Declare these functions volatile
3720 to avoid spurious "control drops through" warnings. */
3721 temp = builtin_function ("abort", cplus_mode ? void_ftype : void_ftype_any,
3722 NOT_BUILT_IN, NULL_PTR);
3723 TREE_THIS_VOLATILE (temp) = 1;
3724 TREE_SIDE_EFFECTS (temp) = 1;
3726 #if 0 /* ??? The C++ frontend used to do this. */
3727 /* Well, these are actually ANSI, but we can't set DECL_BUILT_IN on
3729 DECL_BUILT_IN_NONANSI (temp) = 1;
3731 temp = builtin_function ("exit",
3732 cplus_mode ? void_ftype_int : void_ftype_any,
3733 NOT_BUILT_IN, NULL_PTR);
3734 TREE_THIS_VOLATILE (temp) = 1;
3735 TREE_SIDE_EFFECTS (temp) = 1;
3737 #if 0 /* ??? The C++ frontend used to do this. */
3738 /* Well, these are actually ANSI, but we can't set DECL_BUILT_IN on
3740 DECL_BUILT_IN_NONANSI (temp) = 1;
3745 /* Support for these has not been written in either expand_builtin
3746 or build_function_call. */
3747 builtin_function ("__builtin_div", default_ftype, BUILT_IN_DIV, NULL_PTR);
3748 builtin_function ("__builtin_ldiv", default_ftype, BUILT_IN_LDIV, NULL_PTR);
3749 builtin_function ("__builtin_ffloor", double_ftype_double, BUILT_IN_FFLOOR,
3751 builtin_function ("__builtin_fceil", double_ftype_double, BUILT_IN_FCEIL,
3753 builtin_function ("__builtin_fmod", double_ftype_double_double,
3754 BUILT_IN_FMOD, NULL_PTR);
3755 builtin_function ("__builtin_frem", double_ftype_double_double,
3756 BUILT_IN_FREM, NULL_PTR);
3757 builtin_function ("__builtin_getexp", double_ftype_double, BUILT_IN_GETEXP,
3759 builtin_function ("__builtin_getman", double_ftype_double, BUILT_IN_GETMAN,
3765 build_va_arg (expr, type)
3768 return build1 (VA_ARG_EXPR, type, expr);
3771 /* Return nonzero if VALUE is a valid constant-valued expression
3772 for use in initializing a static variable; one that can be an
3773 element of a "constant" initializer.
3775 Return null_pointer_node if the value is absolute;
3776 if it is relocatable, return the variable that determines the relocation.
3777 We assume that VALUE has been folded as much as possible;
3778 therefore, we do not need to check for such things as
3779 arithmetic-combinations of integers. */
3782 initializer_constant_valid_p (value, endtype)
3786 switch (TREE_CODE (value))
3789 if ((TREE_CODE (TREE_TYPE (value)) == UNION_TYPE
3790 || TREE_CODE (TREE_TYPE (value)) == RECORD_TYPE)
3791 && TREE_CONSTANT (value)
3792 && CONSTRUCTOR_ELTS (value))
3794 initializer_constant_valid_p (TREE_VALUE (CONSTRUCTOR_ELTS (value)),
3797 return TREE_STATIC (value) ? null_pointer_node : 0;
3803 return null_pointer_node;
3806 return TREE_OPERAND (value, 0);
3808 case NON_LVALUE_EXPR:
3809 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
3813 /* Allow conversions between pointer types. */
3814 if (POINTER_TYPE_P (TREE_TYPE (value))
3815 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (value, 0))))
3816 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
3818 /* Allow conversions between real types. */
3819 if (FLOAT_TYPE_P (TREE_TYPE (value))
3820 && FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (value, 0))))
3821 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
3823 /* Allow length-preserving conversions between integer types. */
3824 if (INTEGRAL_TYPE_P (TREE_TYPE (value))
3825 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (value, 0)))
3826 && (TYPE_PRECISION (TREE_TYPE (value))
3827 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
3828 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
3830 /* Allow conversions between other integer types only if
3832 if (INTEGRAL_TYPE_P (TREE_TYPE (value))
3833 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (value, 0))))
3835 tree inner = initializer_constant_valid_p (TREE_OPERAND (value, 0),
3837 if (inner == null_pointer_node)
3838 return null_pointer_node;
3842 /* Allow (int) &foo provided int is as wide as a pointer. */
3843 if (INTEGRAL_TYPE_P (TREE_TYPE (value))
3844 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (value, 0)))
3845 && (TYPE_PRECISION (TREE_TYPE (value))
3846 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
3847 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
3850 /* Likewise conversions from int to pointers, but also allow
3851 conversions from 0. */
3852 if (POINTER_TYPE_P (TREE_TYPE (value))
3853 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (value, 0))))
3855 if (integer_zerop (TREE_OPERAND (value, 0)))
3856 return null_pointer_node;
3857 else if (TYPE_PRECISION (TREE_TYPE (value))
3858 <= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0))))
3859 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
3863 /* Allow conversions to union types if the value inside is okay. */
3864 if (TREE_CODE (TREE_TYPE (value)) == UNION_TYPE)
3865 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
3870 if (! INTEGRAL_TYPE_P (endtype)
3871 || TYPE_PRECISION (endtype) >= POINTER_SIZE)
3873 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
3875 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
3877 /* If either term is absolute, use the other terms relocation. */
3878 if (valid0 == null_pointer_node)
3880 if (valid1 == null_pointer_node)
3886 if (! INTEGRAL_TYPE_P (endtype)
3887 || TYPE_PRECISION (endtype) >= POINTER_SIZE)
3889 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
3891 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
3893 /* Win if second argument is absolute. */
3894 if (valid1 == null_pointer_node)
3896 /* Win if both arguments have the same relocation.
3897 Then the value is absolute. */
3898 if (valid0 == valid1)
3899 return null_pointer_node;
3902 /* Support differences between labels. */
3903 if (INTEGRAL_TYPE_P (endtype))
3906 op0 = TREE_OPERAND (value, 0);
3907 op1 = TREE_OPERAND (value, 1);
3911 if (TREE_CODE (op0) == ADDR_EXPR
3912 && TREE_CODE (TREE_OPERAND (op0, 0)) == LABEL_DECL
3913 && TREE_CODE (op1) == ADDR_EXPR
3914 && TREE_CODE (TREE_OPERAND (op1, 0)) == LABEL_DECL)
3915 return null_pointer_node;