1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992, 93, 94, 95, 96, 1997 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. */
43 #ifndef WCHAR_TYPE_SIZE
45 #define WCHAR_TYPE_SIZE INT_TYPE_SIZE
47 #define WCHAR_TYPE_SIZE BITS_PER_WORD
51 extern struct obstack permanent_obstack
;
53 /* Nonzero means the expression being parsed will never be evaluated.
54 This is a count, since unevaluated expressions can nest. */
57 enum attrs
{A_PACKED
, A_NOCOMMON
, A_COMMON
, A_NORETURN
, A_CONST
, A_T_UNION
,
58 A_CONSTRUCTOR
, A_DESTRUCTOR
, A_MODE
, A_SECTION
, A_ALIGNED
,
59 A_UNUSED
, A_FORMAT
, A_FORMAT_ARG
, A_WEAK
, A_ALIAS
};
61 static void declare_hidden_char_array
PROTO((char *, char *));
62 static void add_attribute
PROTO((enum attrs
, char *,
64 static void init_attributes
PROTO((void));
65 static void record_international_format
PROTO((tree
, tree
, int));
67 /* Keep a stack of if statements. We record the number of compound
68 statements seen up to the if keyword, as well as the line number
69 and file of the if. If a potentially ambiguous else is seen, that
70 fact is recorded; the warning is issued when we can be sure that
71 the enclosing if statement does not have an else branch. */
80 static if_elt
*if_stack
;
82 /* Amount of space in the if statement stack. */
83 static int if_stack_space
= 0;
86 static int if_stack_pointer
= 0;
88 /* Generate RTL for the start of an if-then, and record the start of it
89 for ambiguous else detection. */
92 c_expand_start_cond (cond
, exitflag
, compstmt_count
)
97 /* Make sure there is enough space on the stack. */
98 if (if_stack_space
== 0)
101 if_stack
= (if_elt
*)xmalloc (10 * sizeof (if_elt
));
103 else if (if_stack_space
== if_stack_pointer
)
105 if_stack_space
+= 10;
106 if_stack
= (if_elt
*)xrealloc (if_stack
, if_stack_space
* sizeof (if_elt
));
109 /* Record this if statement. */
110 if_stack
[if_stack_pointer
].compstmt_count
= compstmt_count
;
111 if_stack
[if_stack_pointer
].file
= input_filename
;
112 if_stack
[if_stack_pointer
].line
= lineno
;
113 if_stack
[if_stack_pointer
].needs_warning
= 0;
116 expand_start_cond (cond
, exitflag
);
119 /* Generate RTL for the end of an if-then. Optionally warn if a nested
120 if statement had an ambiguous else clause. */
126 if (if_stack
[if_stack_pointer
].needs_warning
)
127 warning_with_file_and_line (if_stack
[if_stack_pointer
].file
,
128 if_stack
[if_stack_pointer
].line
,
129 "suggest explicit braces to avoid ambiguous `else'");
133 /* Generate RTL between the then-clause and the else-clause
134 of an if-then-else. */
137 c_expand_start_else ()
139 /* An ambiguous else warning must be generated for the enclosing if
140 statement, unless we see an else branch for that one, too. */
142 && if_stack_pointer
> 1
143 && (if_stack
[if_stack_pointer
- 1].compstmt_count
144 == if_stack
[if_stack_pointer
- 2].compstmt_count
))
145 if_stack
[if_stack_pointer
- 2].needs_warning
= 1;
147 /* Even if a nested if statement had an else branch, it can't be
148 ambiguous if this one also has an else. So don't warn in that
149 case. Also don't warn for any if statements nested in this else. */
150 if_stack
[if_stack_pointer
- 1].needs_warning
= 0;
151 if_stack
[if_stack_pointer
- 1].compstmt_count
--;
153 expand_start_else ();
156 /* Make bindings for __FUNCTION__ and __PRETTY_FUNCTION__. */
159 declare_function_name ()
161 char *name
, *printable_name
;
163 if (current_function_decl
== NULL
)
166 printable_name
= "top level";
170 /* Allow functions to be nameless (such as artificial ones). */
171 if (DECL_NAME (current_function_decl
))
172 name
= IDENTIFIER_POINTER (DECL_NAME (current_function_decl
));
175 printable_name
= (*decl_printable_name
) (current_function_decl
, 2);
178 declare_hidden_char_array ("__FUNCTION__", name
);
179 declare_hidden_char_array ("__PRETTY_FUNCTION__", printable_name
);
183 declare_hidden_char_array (name
, value
)
186 tree decl
, type
, init
;
189 /* If the default size of char arrays isn't big enough for the name,
190 or if we want to give warnings for large objects, make a bigger one. */
191 vlen
= strlen (value
) + 1;
192 type
= char_array_type_node
;
193 if (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TREE_TYPE (type
))) < vlen
195 type
= build_array_type (char_type_node
,
196 build_index_type (build_int_2 (vlen
, 0)));
197 push_obstacks_nochange ();
198 decl
= build_decl (VAR_DECL
, get_identifier (name
), type
);
199 TREE_STATIC (decl
) = 1;
200 TREE_READONLY (decl
) = 1;
201 TREE_ASM_WRITTEN (decl
) = 1;
202 DECL_SOURCE_LINE (decl
) = 0;
203 DECL_ARTIFICIAL (decl
) = 1;
204 DECL_IN_SYSTEM_HEADER (decl
) = 1;
205 DECL_IGNORED_P (decl
) = 1;
206 init
= build_string (vlen
, value
);
207 TREE_TYPE (init
) = type
;
208 DECL_INITIAL (decl
) = init
;
209 finish_decl (pushdecl (decl
), init
, NULL_TREE
);
212 /* Given a chain of STRING_CST nodes,
213 concatenate them into one STRING_CST
214 and give it a suitable array-of-chars data type. */
217 combine_strings (strings
)
220 register tree value
, t
;
221 register int length
= 1;
224 int wchar_bytes
= TYPE_PRECISION (wchar_type_node
) / BITS_PER_UNIT
;
227 if (TREE_CHAIN (strings
))
229 /* More than one in the chain, so concatenate. */
230 register char *p
, *q
;
232 /* Don't include the \0 at the end of each substring,
233 except for the last one.
234 Count wide strings and ordinary strings separately. */
235 for (t
= strings
; t
; t
= TREE_CHAIN (t
))
237 if (TREE_TYPE (t
) == wchar_array_type_node
)
239 wide_length
+= (TREE_STRING_LENGTH (t
) - wchar_bytes
);
243 length
+= (TREE_STRING_LENGTH (t
) - 1);
246 /* If anything is wide, the non-wides will be converted,
247 which makes them take more space. */
249 length
= length
* wchar_bytes
+ wide_length
;
251 p
= savealloc (length
);
253 /* Copy the individual strings into the new combined string.
254 If the combined string is wide, convert the chars to ints
255 for any individual strings that are not wide. */
258 for (t
= strings
; t
; t
= TREE_CHAIN (t
))
260 int len
= (TREE_STRING_LENGTH (t
)
261 - ((TREE_TYPE (t
) == wchar_array_type_node
)
263 if ((TREE_TYPE (t
) == wchar_array_type_node
) == wide_flag
)
265 bcopy (TREE_STRING_POINTER (t
), q
, len
);
271 for (i
= 0; i
< len
; i
++)
273 if (WCHAR_TYPE_SIZE
== HOST_BITS_PER_SHORT
)
274 ((short *) q
)[i
] = TREE_STRING_POINTER (t
)[i
];
276 ((int *) q
)[i
] = TREE_STRING_POINTER (t
)[i
];
278 q
+= len
* wchar_bytes
;
284 for (i
= 0; i
< wchar_bytes
; i
++)
290 value
= make_node (STRING_CST
);
291 TREE_STRING_POINTER (value
) = p
;
292 TREE_STRING_LENGTH (value
) = length
;
293 TREE_CONSTANT (value
) = 1;
298 length
= TREE_STRING_LENGTH (value
);
299 if (TREE_TYPE (value
) == wchar_array_type_node
)
303 /* Compute the number of elements, for the array type. */
304 nchars
= wide_flag
? length
/ wchar_bytes
: length
;
306 /* Create the array type for the string constant.
307 -Wwrite-strings says make the string constant an array of const char
308 so that copying it to a non-const pointer will get a warning. */
309 if (warn_write_strings
310 && (! flag_traditional
&& ! flag_writable_strings
))
313 = build_type_variant (wide_flag
? wchar_type_node
: char_type_node
,
316 = build_array_type (elements
,
317 build_index_type (build_int_2 (nchars
- 1, 0)));
321 = build_array_type (wide_flag
? wchar_type_node
: char_type_node
,
322 build_index_type (build_int_2 (nchars
- 1, 0)));
323 TREE_CONSTANT (value
) = 1;
324 TREE_STATIC (value
) = 1;
328 /* To speed up processing of attributes, we maintain an array of
329 IDENTIFIER_NODES and the corresponding attribute types. */
331 /* Array to hold attribute information. */
333 static struct {enum attrs id
; tree name
; int min
, max
, decl_req
;} attrtab
[50];
335 static int attrtab_idx
= 0;
337 /* Add an entry to the attribute table above. */
340 add_attribute (id
, string
, min_len
, max_len
, decl_req
)
343 int min_len
, max_len
;
348 attrtab
[attrtab_idx
].id
= id
;
349 attrtab
[attrtab_idx
].name
= get_identifier (string
);
350 attrtab
[attrtab_idx
].min
= min_len
;
351 attrtab
[attrtab_idx
].max
= max_len
;
352 attrtab
[attrtab_idx
++].decl_req
= decl_req
;
354 sprintf (buf
, "__%s__", string
);
356 attrtab
[attrtab_idx
].id
= id
;
357 attrtab
[attrtab_idx
].name
= get_identifier (buf
);
358 attrtab
[attrtab_idx
].min
= min_len
;
359 attrtab
[attrtab_idx
].max
= max_len
;
360 attrtab
[attrtab_idx
++].decl_req
= decl_req
;
363 /* Initialize attribute table. */
368 add_attribute (A_PACKED
, "packed", 0, 0, 0);
369 add_attribute (A_NOCOMMON
, "nocommon", 0, 0, 1);
370 add_attribute (A_COMMON
, "common", 0, 0, 1);
371 add_attribute (A_NORETURN
, "noreturn", 0, 0, 1);
372 add_attribute (A_NORETURN
, "volatile", 0, 0, 1);
373 add_attribute (A_UNUSED
, "unused", 0, 0, 0);
374 add_attribute (A_CONST
, "const", 0, 0, 1);
375 add_attribute (A_T_UNION
, "transparent_union", 0, 0, 0);
376 add_attribute (A_CONSTRUCTOR
, "constructor", 0, 0, 1);
377 add_attribute (A_DESTRUCTOR
, "destructor", 0, 0, 1);
378 add_attribute (A_MODE
, "mode", 1, 1, 1);
379 add_attribute (A_SECTION
, "section", 1, 1, 1);
380 add_attribute (A_ALIGNED
, "aligned", 0, 1, 0);
381 add_attribute (A_FORMAT
, "format", 3, 3, 1);
382 add_attribute (A_FORMAT_ARG
, "format_arg", 1, 1, 1);
383 add_attribute (A_WEAK
, "weak", 0, 0, 1);
384 add_attribute (A_ALIAS
, "alias", 1, 1, 1);
387 /* Process the attributes listed in ATTRIBUTES and PREFIX_ATTRIBUTES
388 and install them in NODE, which is either a DECL (including a TYPE_DECL)
389 or a TYPE. PREFIX_ATTRIBUTES can appear after the declaration specifiers
390 and declaration modifiers but before the declaration proper. */
393 decl_attributes (node
, attributes
, prefix_attributes
)
394 tree node
, attributes
, prefix_attributes
;
400 if (attrtab_idx
== 0)
403 if (TREE_CODE_CLASS (TREE_CODE (node
)) == 'd')
406 type
= TREE_TYPE (decl
);
407 is_type
= TREE_CODE (node
) == TYPE_DECL
;
409 else if (TREE_CODE_CLASS (TREE_CODE (node
)) == 't')
410 type
= node
, is_type
= 1;
412 attributes
= chainon (prefix_attributes
, attributes
);
414 for (a
= attributes
; a
; a
= TREE_CHAIN (a
))
416 tree name
= TREE_PURPOSE (a
);
417 tree args
= TREE_VALUE (a
);
421 for (i
= 0; i
< attrtab_idx
; i
++)
422 if (attrtab
[i
].name
== name
)
425 if (i
== attrtab_idx
)
427 if (! valid_machine_attribute (name
, args
, decl
, type
))
428 warning ("`%s' attribute directive ignored",
429 IDENTIFIER_POINTER (name
));
431 type
= TREE_TYPE (decl
);
434 else if (attrtab
[i
].decl_req
&& decl
== 0)
436 warning ("`%s' attribute does not apply to types",
437 IDENTIFIER_POINTER (name
));
440 else if (list_length (args
) < attrtab
[i
].min
441 || list_length (args
) > attrtab
[i
].max
)
443 error ("wrong number of arguments specified for `%s' attribute",
444 IDENTIFIER_POINTER (name
));
453 TYPE_PACKED (type
) = 1;
454 else if (TREE_CODE (decl
) == FIELD_DECL
)
455 DECL_PACKED (decl
) = 1;
456 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
457 used for DECL_REGISTER. It wouldn't mean anything anyway. */
459 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
463 if (TREE_CODE (decl
) == VAR_DECL
)
464 DECL_COMMON (decl
) = 0;
466 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
470 if (TREE_CODE (decl
) == VAR_DECL
)
471 DECL_COMMON (decl
) = 1;
473 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
477 if (TREE_CODE (decl
) == FUNCTION_DECL
)
478 TREE_THIS_VOLATILE (decl
) = 1;
479 else if (TREE_CODE (type
) == POINTER_TYPE
480 && TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
)
481 TREE_TYPE (decl
) = type
483 (build_type_variant (TREE_TYPE (type
),
484 TREE_READONLY (TREE_TYPE (type
)), 1));
486 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
491 TREE_USED (type
) = 1;
492 else if (TREE_CODE (decl
) == PARM_DECL
493 || TREE_CODE (decl
) == VAR_DECL
494 || TREE_CODE (decl
) == FUNCTION_DECL
)
495 TREE_USED (decl
) = 1;
497 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
501 if (TREE_CODE (decl
) == FUNCTION_DECL
)
502 TREE_READONLY (decl
) = 1;
503 else if (TREE_CODE (type
) == POINTER_TYPE
504 && TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
)
505 TREE_TYPE (decl
) = type
507 (build_type_variant (TREE_TYPE (type
), 1,
508 TREE_THIS_VOLATILE (TREE_TYPE (type
))));
510 warning ( "`%s' attribute ignored", IDENTIFIER_POINTER (name
));
515 && TREE_CODE (type
) == UNION_TYPE
517 || (TYPE_FIELDS (type
) != 0
518 && TYPE_MODE (type
) == DECL_MODE (TYPE_FIELDS (type
)))))
519 TYPE_TRANSPARENT_UNION (type
) = 1;
520 else if (decl
!= 0 && TREE_CODE (decl
) == PARM_DECL
521 && TREE_CODE (type
) == UNION_TYPE
522 && TYPE_MODE (type
) == DECL_MODE (TYPE_FIELDS (type
)))
523 DECL_TRANSPARENT_UNION (decl
) = 1;
525 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
529 if (TREE_CODE (decl
) == FUNCTION_DECL
530 && TREE_CODE (type
) == FUNCTION_TYPE
531 && decl_function_context (decl
) == 0)
533 DECL_STATIC_CONSTRUCTOR (decl
) = 1;
534 TREE_USED (decl
) = 1;
537 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
541 if (TREE_CODE (decl
) == FUNCTION_DECL
542 && TREE_CODE (type
) == FUNCTION_TYPE
543 && decl_function_context (decl
) == 0)
545 DECL_STATIC_DESTRUCTOR (decl
) = 1;
546 TREE_USED (decl
) = 1;
549 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
553 if (TREE_CODE (TREE_VALUE (args
)) != IDENTIFIER_NODE
)
554 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
558 char *p
= IDENTIFIER_POINTER (TREE_VALUE (args
));
559 int len
= strlen (p
);
560 enum machine_mode mode
= VOIDmode
;
563 if (len
> 4 && p
[0] == '_' && p
[1] == '_'
564 && p
[len
- 1] == '_' && p
[len
- 2] == '_')
566 char *newp
= (char *) alloca (len
- 1);
568 strcpy (newp
, &p
[2]);
569 newp
[len
- 4] = '\0';
573 /* Give this decl a type with the specified mode.
574 First check for the special modes. */
575 if (! strcmp (p
, "byte"))
577 else if (!strcmp (p
, "word"))
579 else if (! strcmp (p
, "pointer"))
582 for (j
= 0; j
< NUM_MACHINE_MODES
; j
++)
583 if (!strcmp (p
, GET_MODE_NAME (j
)))
584 mode
= (enum machine_mode
) j
;
586 if (mode
== VOIDmode
)
587 error ("unknown machine mode `%s'", p
);
588 else if (0 == (typefm
= type_for_mode (mode
,
589 TREE_UNSIGNED (type
))))
590 error ("no data type for mode `%s'", p
);
593 TREE_TYPE (decl
) = type
= typefm
;
594 DECL_SIZE (decl
) = 0;
595 layout_decl (decl
, 0);
601 #ifdef ASM_OUTPUT_SECTION_NAME
602 if ((TREE_CODE (decl
) == FUNCTION_DECL
603 || TREE_CODE (decl
) == VAR_DECL
)
604 && TREE_CODE (TREE_VALUE (args
)) == STRING_CST
)
606 if (TREE_CODE (decl
) == VAR_DECL
607 && current_function_decl
!= NULL_TREE
608 && ! TREE_STATIC (decl
))
609 error_with_decl (decl
,
610 "section attribute cannot be specified for local variables");
611 /* The decl may have already been given a section attribute from
612 a previous declaration. Ensure they match. */
613 else if (DECL_SECTION_NAME (decl
) != NULL_TREE
614 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl
)),
615 TREE_STRING_POINTER (TREE_VALUE (args
))) != 0)
616 error_with_decl (node
,
617 "section of `%s' conflicts with previous declaration");
619 DECL_SECTION_NAME (decl
) = TREE_VALUE (args
);
622 error_with_decl (node
,
623 "section attribute not allowed for `%s'");
625 error_with_decl (node
,
626 "section attributes are not supported for this target");
633 = (args
? TREE_VALUE (args
)
634 : size_int (BIGGEST_ALIGNMENT
/ BITS_PER_UNIT
));
637 /* Strip any NOPs of any kind. */
638 while (TREE_CODE (align_expr
) == NOP_EXPR
639 || TREE_CODE (align_expr
) == CONVERT_EXPR
640 || TREE_CODE (align_expr
) == NON_LVALUE_EXPR
)
641 align_expr
= TREE_OPERAND (align_expr
, 0);
643 if (TREE_CODE (align_expr
) != INTEGER_CST
)
645 error ("requested alignment is not a constant");
649 align
= TREE_INT_CST_LOW (align_expr
) * BITS_PER_UNIT
;
651 if (exact_log2 (align
) == -1)
652 error ("requested alignment is not a power of 2");
654 TYPE_ALIGN (type
) = align
;
655 else if (TREE_CODE (decl
) != VAR_DECL
656 && TREE_CODE (decl
) != FIELD_DECL
)
657 error_with_decl (decl
,
658 "alignment may not be specified for `%s'");
660 DECL_ALIGN (decl
) = align
;
666 tree format_type
= TREE_VALUE (args
);
667 tree format_num_expr
= TREE_VALUE (TREE_CHAIN (args
));
668 tree first_arg_num_expr
669 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args
)));
676 if (TREE_CODE (decl
) != FUNCTION_DECL
)
678 error_with_decl (decl
,
679 "argument format specified for non-function `%s'");
683 if (TREE_CODE (format_type
) == IDENTIFIER_NODE
684 && (!strcmp (IDENTIFIER_POINTER (format_type
), "printf")
685 || !strcmp (IDENTIFIER_POINTER (format_type
),
688 else if (TREE_CODE (format_type
) == IDENTIFIER_NODE
689 && (!strcmp (IDENTIFIER_POINTER (format_type
), "scanf")
690 || !strcmp (IDENTIFIER_POINTER (format_type
),
693 else if (TREE_CODE (format_type
) == IDENTIFIER_NODE
)
695 warning ("`%s' is an unrecognized format function type",
696 IDENTIFIER_POINTER (format_type
));
701 error ("unrecognized format specifier");
705 /* Strip any conversions from the string index and first arg number
706 and verify they are constants. */
707 while (TREE_CODE (format_num_expr
) == NOP_EXPR
708 || TREE_CODE (format_num_expr
) == CONVERT_EXPR
709 || TREE_CODE (format_num_expr
) == NON_LVALUE_EXPR
)
710 format_num_expr
= TREE_OPERAND (format_num_expr
, 0);
712 while (TREE_CODE (first_arg_num_expr
) == NOP_EXPR
713 || TREE_CODE (first_arg_num_expr
) == CONVERT_EXPR
714 || TREE_CODE (first_arg_num_expr
) == NON_LVALUE_EXPR
)
715 first_arg_num_expr
= TREE_OPERAND (first_arg_num_expr
, 0);
717 if (TREE_CODE (format_num_expr
) != INTEGER_CST
718 || TREE_CODE (first_arg_num_expr
) != INTEGER_CST
)
720 error ("format string has non-constant operand number");
724 format_num
= TREE_INT_CST_LOW (format_num_expr
);
725 first_arg_num
= TREE_INT_CST_LOW (first_arg_num_expr
);
726 if (first_arg_num
!= 0 && first_arg_num
<= format_num
)
728 error ("format string arg follows the args to be formatted");
732 /* If a parameter list is specified, verify that the format_num
733 argument is actually a string, in case the format attribute
735 argument
= TYPE_ARG_TYPES (type
);
738 for (arg_num
= 1; ; ++arg_num
)
740 if (argument
== 0 || arg_num
== format_num
)
742 argument
= TREE_CHAIN (argument
);
745 || TREE_CODE (TREE_VALUE (argument
)) != POINTER_TYPE
746 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument
)))
749 error ("format string arg not a string type");
752 if (first_arg_num
!= 0)
754 /* Verify that first_arg_num points to the last arg,
757 arg_num
++, argument
= TREE_CHAIN (argument
);
758 if (arg_num
!= first_arg_num
)
760 error ("args to be formatted is not ...");
766 record_function_format (DECL_NAME (decl
),
767 DECL_ASSEMBLER_NAME (decl
),
768 is_scan
, format_num
, first_arg_num
);
774 tree format_num_expr
= TREE_VALUE (args
);
775 int format_num
, arg_num
;
778 if (TREE_CODE (decl
) != FUNCTION_DECL
)
780 error_with_decl (decl
,
781 "argument format specified for non-function `%s'");
785 /* Strip any conversions from the first arg number and verify it
787 while (TREE_CODE (format_num_expr
) == NOP_EXPR
788 || TREE_CODE (format_num_expr
) == CONVERT_EXPR
789 || TREE_CODE (format_num_expr
) == NON_LVALUE_EXPR
)
790 format_num_expr
= TREE_OPERAND (format_num_expr
, 0);
792 if (TREE_CODE (format_num_expr
) != INTEGER_CST
)
794 error ("format string has non-constant operand number");
798 format_num
= TREE_INT_CST_LOW (format_num_expr
);
800 /* If a parameter list is specified, verify that the format_num
801 argument is actually a string, in case the format attribute
803 argument
= TYPE_ARG_TYPES (type
);
806 for (arg_num
= 1; ; ++arg_num
)
808 if (argument
== 0 || arg_num
== format_num
)
810 argument
= TREE_CHAIN (argument
);
813 || TREE_CODE (TREE_VALUE (argument
)) != POINTER_TYPE
814 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument
)))
817 error ("format string arg not a string type");
822 if (TREE_CODE (TREE_TYPE (TREE_TYPE (decl
))) != POINTER_TYPE
823 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_TYPE (decl
))))
826 error ("function does not return string type");
830 record_international_format (DECL_NAME (decl
),
831 DECL_ASSEMBLER_NAME (decl
),
841 if ((TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_INITIAL (decl
))
842 || (TREE_CODE (decl
) != FUNCTION_DECL
&& ! DECL_EXTERNAL (decl
)))
843 error_with_decl (decl
,
844 "`%s' defined both normally and as an alias");
845 else if (decl_function_context (decl
) == 0)
847 tree id
= get_identifier (TREE_STRING_POINTER
848 (TREE_VALUE (args
)));
849 if (TREE_CODE (decl
) == FUNCTION_DECL
)
850 DECL_INITIAL (decl
) = error_mark_node
;
852 DECL_EXTERNAL (decl
) = 0;
853 assemble_alias (decl
, id
);
856 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
862 /* Split SPECS_ATTRS, a list of declspecs and prefix attributes, into two
863 lists. SPECS_ATTRS may also be just a typespec (eg: RECORD_TYPE).
865 The head of the declspec list is stored in DECLSPECS.
866 The head of the attribute list is stored in PREFIX_ATTRIBUTES.
868 Note that attributes in SPECS_ATTRS are stored in the TREE_PURPOSE of
869 the list elements. We drop the containing TREE_LIST nodes and link the
870 resulting attributes together the way decl_attributes expects them. */
873 split_specs_attrs (specs_attrs
, declspecs
, prefix_attributes
)
875 tree
*declspecs
, *prefix_attributes
;
877 tree t
, s
, a
, next
, specs
, attrs
;
879 /* This can happen in c++ (eg: decl: typespec initdecls ';'). */
880 if (specs_attrs
!= NULL_TREE
881 && TREE_CODE (specs_attrs
) != TREE_LIST
)
883 *declspecs
= specs_attrs
;
884 *prefix_attributes
= NULL_TREE
;
888 /* Remember to keep the lists in the same order, element-wise. */
890 specs
= s
= NULL_TREE
;
891 attrs
= a
= NULL_TREE
;
892 for (t
= specs_attrs
; t
; t
= next
)
894 next
= TREE_CHAIN (t
);
895 /* Declspecs have a non-NULL TREE_VALUE. */
896 if (TREE_VALUE (t
) != NULL_TREE
)
898 if (specs
== NULL_TREE
)
908 if (attrs
== NULL_TREE
)
909 attrs
= a
= TREE_PURPOSE (t
);
912 TREE_CHAIN (a
) = TREE_PURPOSE (t
);
913 a
= TREE_PURPOSE (t
);
915 /* More attrs can be linked here, move A to the end. */
916 while (TREE_CHAIN (a
) != NULL_TREE
)
921 /* Terminate the lists. */
923 TREE_CHAIN (s
) = NULL_TREE
;
925 TREE_CHAIN (a
) = NULL_TREE
;
929 *prefix_attributes
= attrs
;
932 /* Strip attributes from SPECS_ATTRS, a list of declspecs and attributes.
933 This function is used by the parser when a rule will accept attributes
934 in a particular position, but we don't want to support that just yet.
936 A warning is issued for every ignored attribute. */
939 strip_attrs (specs_attrs
)
944 split_specs_attrs (specs_attrs
, &specs
, &attrs
);
948 warning ("`%s' attribute ignored",
949 IDENTIFIER_POINTER (TREE_PURPOSE (attrs
)));
950 attrs
= TREE_CHAIN (attrs
);
956 /* Check a printf/fprintf/sprintf/scanf/fscanf/sscanf format against
959 #define T_I &integer_type_node
960 #define T_L &long_integer_type_node
961 #define T_LL &long_long_integer_type_node
962 #define T_S &short_integer_type_node
963 #define T_UI &unsigned_type_node
964 #define T_UL &long_unsigned_type_node
965 #define T_ULL &long_long_unsigned_type_node
966 #define T_US &short_unsigned_type_node
967 #define T_F &float_type_node
968 #define T_D &double_type_node
969 #define T_LD &long_double_type_node
970 #define T_C &char_type_node
971 #define T_V &void_type_node
972 #define T_W &wchar_type_node
973 #define T_ST &sizetype
978 /* Type of argument if no length modifier is used. */
980 /* Type of argument if length modifier for shortening is used.
981 If NULL, then this modifier is not allowed. */
983 /* Type of argument if length modifier `l' is used.
984 If NULL, then this modifier is not allowed. */
986 /* Type of argument if length modifier `q' or `ll' is used.
987 If NULL, then this modifier is not allowed. */
989 /* Type of argument if length modifier `L' is used.
990 If NULL, then this modifier is not allowed. */
992 /* List of other modifier characters allowed with these options. */
996 static format_char_info print_char_table
[] = {
997 { "di", 0, T_I
, T_I
, T_L
, T_LL
, T_LL
, "-wp0 +" },
998 { "oxX", 0, T_UI
, T_UI
, T_UL
, T_ULL
, T_ULL
, "-wp0#" },
999 { "u", 0, T_UI
, T_UI
, T_UL
, T_ULL
, T_ULL
, "-wp0" },
1000 /* Two GNU extensions. */
1001 { "Z", 0, T_ST
, NULL
, NULL
, NULL
, NULL
, "-wp0" },
1002 { "m", 0, T_V
, NULL
, NULL
, NULL
, NULL
, "-wp" },
1003 { "feEgGaA", 0, T_D
, NULL
, NULL
, NULL
, T_LD
, "-wp0 +#" },
1004 { "c", 0, T_I
, NULL
, T_W
, NULL
, NULL
, "-w" },
1005 { "C", 0, T_W
, NULL
, NULL
, NULL
, NULL
, "-w" },
1006 { "s", 1, T_C
, NULL
, T_W
, NULL
, NULL
, "-wp" },
1007 { "S", 1, T_W
, NULL
, NULL
, NULL
, NULL
, "-wp" },
1008 { "p", 1, T_V
, NULL
, NULL
, NULL
, NULL
, "-w" },
1009 { "n", 1, T_I
, T_S
, T_L
, T_LL
, NULL
, "" },
1013 static format_char_info scan_char_table
[] = {
1014 { "di", 1, T_I
, T_S
, T_L
, T_LL
, T_LL
, "*" },
1015 { "ouxX", 1, T_UI
, T_US
, T_UL
, T_ULL
, T_ULL
, "*" },
1016 { "efgEGaA", 1, T_F
, NULL
, T_D
, NULL
, T_LD
, "*" },
1017 { "sc", 1, T_C
, NULL
, T_W
, NULL
, NULL
, "*a" },
1018 { "[", 1, T_C
, NULL
, NULL
, NULL
, NULL
, "*a" },
1019 { "C", 1, T_W
, NULL
, NULL
, NULL
, NULL
, "*" },
1020 { "S", 1, T_W
, NULL
, NULL
, NULL
, NULL
, "*" },
1021 { "p", 2, T_V
, NULL
, NULL
, NULL
, NULL
, "*" },
1022 { "n", 1, T_I
, T_S
, T_L
, T_LL
, NULL
, "" },
1026 typedef struct function_format_info
1028 struct function_format_info
*next
; /* next structure on the list */
1029 tree name
; /* identifier such as "printf" */
1030 tree assembler_name
; /* optional mangled identifier (for C++) */
1031 int is_scan
; /* TRUE if *scanf */
1032 int format_num
; /* number of format argument */
1033 int first_arg_num
; /* number of first arg (zero for varargs) */
1034 } function_format_info
;
1036 static function_format_info
*function_format_list
= NULL
;
1038 typedef struct international_format_info
1040 struct international_format_info
*next
; /* next structure on the list */
1041 tree name
; /* identifier such as "gettext" */
1042 tree assembler_name
; /* optional mangled identifier (for C++) */
1043 int format_num
; /* number of format argument */
1044 } international_format_info
;
1046 static international_format_info
*international_format_list
= NULL
;
1048 static void check_format_info
PROTO((function_format_info
*, tree
));
1050 /* Initialize the table of functions to perform format checking on.
1051 The ANSI functions are always checked (whether <stdio.h> is
1052 included or not), since it is common to call printf without
1053 including <stdio.h>. There shouldn't be a problem with this,
1054 since ANSI reserves these function names whether you include the
1055 header file or not. In any case, the checking is harmless.
1057 Also initialize the name of function that modify the format string for
1058 internationalization purposes. */
1061 init_function_format_info ()
1063 record_function_format (get_identifier ("printf"), NULL_TREE
, 0, 1, 2);
1064 record_function_format (get_identifier ("fprintf"), NULL_TREE
, 0, 2, 3);
1065 record_function_format (get_identifier ("sprintf"), NULL_TREE
, 0, 2, 3);
1066 record_function_format (get_identifier ("scanf"), NULL_TREE
, 1, 1, 2);
1067 record_function_format (get_identifier ("fscanf"), NULL_TREE
, 1, 2, 3);
1068 record_function_format (get_identifier ("sscanf"), NULL_TREE
, 1, 2, 3);
1069 record_function_format (get_identifier ("vprintf"), NULL_TREE
, 0, 1, 0);
1070 record_function_format (get_identifier ("vfprintf"), NULL_TREE
, 0, 2, 0);
1071 record_function_format (get_identifier ("vsprintf"), NULL_TREE
, 0, 2, 0);
1073 record_international_format (get_identifier ("gettext"), NULL_TREE
, 1);
1074 record_international_format (get_identifier ("dgettext"), NULL_TREE
, 2);
1075 record_international_format (get_identifier ("dcgettext"), NULL_TREE
, 2);
1078 /* Record information for argument format checking. FUNCTION_IDENT is
1079 the identifier node for the name of the function to check (its decl
1080 need not exist yet). IS_SCAN is true for scanf-type format checking;
1081 false indicates printf-style format checking. FORMAT_NUM is the number
1082 of the argument which is the format control string (starting from 1).
1083 FIRST_ARG_NUM is the number of the first actual argument to check
1084 against the format string, or zero if no checking is not be done
1085 (e.g. for varargs such as vfprintf). */
1088 record_function_format (name
, assembler_name
, is_scan
,
1089 format_num
, first_arg_num
)
1091 tree assembler_name
;
1096 function_format_info
*info
;
1098 /* Re-use existing structure if it's there. */
1100 for (info
= function_format_list
; info
; info
= info
->next
)
1102 if (info
->name
== name
&& info
->assembler_name
== assembler_name
)
1107 info
= (function_format_info
*) xmalloc (sizeof (function_format_info
));
1108 info
->next
= function_format_list
;
1109 function_format_list
= info
;
1112 info
->assembler_name
= assembler_name
;
1115 info
->is_scan
= is_scan
;
1116 info
->format_num
= format_num
;
1117 info
->first_arg_num
= first_arg_num
;
1120 /* Record information for the names of function that modify the format
1121 argument to format functions. FUNCTION_IDENT is the identifier node for
1122 the name of the function (its decl need not exist yet) and FORMAT_NUM is
1123 the number of the argument which is the format control string (starting
1127 record_international_format (name
, assembler_name
, format_num
)
1129 tree assembler_name
;
1132 international_format_info
*info
;
1134 /* Re-use existing structure if it's there. */
1136 for (info
= international_format_list
; info
; info
= info
->next
)
1138 if (info
->name
== name
&& info
->assembler_name
== assembler_name
)
1145 = (international_format_info
*)
1146 xmalloc (sizeof (international_format_info
));
1147 info
->next
= international_format_list
;
1148 international_format_list
= info
;
1151 info
->assembler_name
= assembler_name
;
1154 info
->format_num
= format_num
;
1157 static char tfaff
[] = "too few arguments for format";
1159 /* Check the argument list of a call to printf, scanf, etc.
1160 NAME is the function identifier.
1161 ASSEMBLER_NAME is the function's assembler identifier.
1162 (Either NAME or ASSEMBLER_NAME, but not both, may be NULL_TREE.)
1163 PARAMS is the list of argument values. */
1166 check_function_format (name
, assembler_name
, params
)
1168 tree assembler_name
;
1171 function_format_info
*info
;
1173 /* See if this function is a format function. */
1174 for (info
= function_format_list
; info
; info
= info
->next
)
1176 if (info
->assembler_name
1177 ? (info
->assembler_name
== assembler_name
)
1178 : (info
->name
== name
))
1180 /* Yup; check it. */
1181 check_format_info (info
, params
);
1187 /* Check the argument list of a call to printf, scanf, etc.
1188 INFO points to the function_format_info structure.
1189 PARAMS is the list of argument values. */
1192 check_format_info (info
, params
)
1193 function_format_info
*info
;
1198 int suppressed
, wide
, precise
;
1206 tree first_fillin_param
;
1208 format_char_info
*fci
;
1209 static char message
[132];
1211 int has_operand_number
= 0;
1213 /* Skip to format argument. If the argument isn't available, there's
1214 no work for us to do; prototype checking will catch the problem. */
1215 for (arg_num
= 1; ; ++arg_num
)
1219 if (arg_num
== info
->format_num
)
1221 params
= TREE_CHAIN (params
);
1223 format_tree
= TREE_VALUE (params
);
1224 params
= TREE_CHAIN (params
);
1225 if (format_tree
== 0)
1228 /* We can only check the format if it's a string constant. */
1229 while (TREE_CODE (format_tree
) == NOP_EXPR
)
1230 format_tree
= TREE_OPERAND (format_tree
, 0); /* strip coercion */
1232 if (TREE_CODE (format_tree
) == CALL_EXPR
1233 && TREE_CODE (TREE_OPERAND (format_tree
, 0)) == ADDR_EXPR
1234 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (format_tree
, 0), 0))
1237 tree function
= TREE_OPERAND (TREE_OPERAND (format_tree
, 0), 0);
1239 /* See if this is a call to a known internationalization function
1240 that modifies the format arg. */
1241 international_format_info
*info
;
1243 for (info
= international_format_list
; info
; info
= info
->next
)
1244 if (info
->assembler_name
1245 ? (info
->assembler_name
== DECL_ASSEMBLER_NAME (function
))
1246 : (info
->name
== DECL_NAME (function
)))
1251 for (inner_args
= TREE_OPERAND (format_tree
, 1), i
= 1;
1253 inner_args
= TREE_CHAIN (inner_args
), i
++)
1254 if (i
== info
->format_num
)
1256 format_tree
= TREE_VALUE (inner_args
);
1258 while (TREE_CODE (format_tree
) == NOP_EXPR
)
1259 format_tree
= TREE_OPERAND (format_tree
, 0);
1264 if (integer_zerop (format_tree
))
1266 warning ("null format string");
1269 if (TREE_CODE (format_tree
) != ADDR_EXPR
)
1271 format_tree
= TREE_OPERAND (format_tree
, 0);
1272 if (TREE_CODE (format_tree
) != STRING_CST
)
1274 format_chars
= TREE_STRING_POINTER (format_tree
);
1275 format_length
= TREE_STRING_LENGTH (format_tree
);
1276 if (format_length
<= 1)
1277 warning ("zero-length format string");
1278 if (format_chars
[--format_length
] != 0)
1280 warning ("unterminated format string");
1283 /* Skip to first argument to check. */
1284 while (arg_num
+ 1 < info
->first_arg_num
)
1288 params
= TREE_CHAIN (params
);
1292 first_fillin_param
= params
;
1296 if (*format_chars
== 0)
1298 if (format_chars
- TREE_STRING_POINTER (format_tree
) != format_length
)
1299 warning ("embedded `\\0' in format");
1300 if (info
->first_arg_num
!= 0 && params
!= 0 && ! has_operand_number
)
1301 warning ("too many arguments for format");
1304 if (*format_chars
++ != '%')
1306 if (*format_chars
== 0)
1308 warning ("spurious trailing `%%' in format");
1311 if (*format_chars
== '%')
1317 suppressed
= wide
= precise
= FALSE
;
1320 suppressed
= *format_chars
== '*';
1323 while (isdigit (*format_chars
))
1328 /* See if we have a number followed by a dollar sign. If we do,
1329 it is an operand number, so set PARAMS to that operand. */
1330 if (*format_chars
>= '0' && *format_chars
<= '9')
1332 char *p
= format_chars
;
1334 while (*p
>= '0' && *p
++ <= '9')
1339 int opnum
= atoi (format_chars
);
1341 params
= first_fillin_param
;
1342 format_chars
= p
+ 1;
1343 has_operand_number
= 1;
1345 for (i
= 1; i
< opnum
&& params
!= 0; i
++)
1346 params
= TREE_CHAIN (params
);
1348 if (opnum
== 0 || params
== 0)
1350 warning ("operand number out of range in format");
1356 while (*format_chars
!= 0 && index (" +#0-", *format_chars
) != 0)
1358 if (index (flag_chars
, *format_chars
) != 0)
1360 sprintf (message
, "repeated `%c' flag in format",
1366 i
= strlen (flag_chars
);
1367 flag_chars
[i
++] = *format_chars
++;
1371 /* "If the space and + flags both appear,
1372 the space flag will be ignored." */
1373 if (index (flag_chars
, ' ') != 0
1374 && index (flag_chars
, '+') != 0)
1375 warning ("use of both ` ' and `+' flags in format");
1376 /* "If the 0 and - flags both appear,
1377 the 0 flag will be ignored." */
1378 if (index (flag_chars
, '0') != 0
1379 && index (flag_chars
, '-') != 0)
1380 warning ("use of both `0' and `-' flags in format");
1381 if (*format_chars
== '*')
1384 /* "...a field width...may be indicated by an asterisk.
1385 In this case, an int argument supplies the field width..." */
1392 if (info
->first_arg_num
!= 0)
1394 cur_param
= TREE_VALUE (params
);
1395 params
= TREE_CHAIN (params
);
1397 /* size_t is generally not valid here.
1398 It will work on most machines, because size_t and int
1399 have the same mode. But might as well warn anyway,
1400 since it will fail on other machines. */
1401 if ((TYPE_MAIN_VARIANT (TREE_TYPE (cur_param
))
1402 != integer_type_node
)
1404 (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param
))
1405 != unsigned_type_node
))
1408 "field width is not type int (arg %d)",
1416 while (isdigit (*format_chars
))
1422 if (*format_chars
== '.')
1426 if (*format_chars
!= '*' && !isdigit (*format_chars
))
1427 warning ("`.' not followed by `*' or digit in format");
1428 /* "...a...precision...may be indicated by an asterisk.
1429 In this case, an int argument supplies the...precision." */
1430 if (*format_chars
== '*')
1432 if (info
->first_arg_num
!= 0)
1440 cur_param
= TREE_VALUE (params
);
1441 params
= TREE_CHAIN (params
);
1443 if (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param
))
1444 != integer_type_node
)
1447 "field width is not type int (arg %d)",
1455 while (isdigit (*format_chars
))
1460 if (*format_chars
== 'h' || *format_chars
== 'l')
1461 length_char
= *format_chars
++;
1462 else if (*format_chars
== 'q' || *format_chars
== 'L')
1464 length_char
= *format_chars
++;
1466 pedwarn ("ANSI C does not support the `%c' length modifier",
1471 if (length_char
== 'l' && *format_chars
== 'l')
1473 length_char
= 'q', format_chars
++;
1475 pedwarn ("ANSI C does not support the `ll' length modifier");
1478 if (*format_chars
== 'a' && info
->is_scan
)
1480 if (format_chars
[1] == 's' || format_chars
[1] == 'S'
1481 || format_chars
[1] == '[')
1483 /* `a' is used as a flag. */
1488 if (suppressed
&& length_char
!= 0)
1491 "use of `*' and `%c' together in format",
1495 format_char
= *format_chars
;
1496 if (format_char
== 0 || format_char
== '%')
1498 warning ("conversion lacks type at end of format");
1502 fci
= info
->is_scan
? scan_char_table
: print_char_table
;
1503 while (fci
->format_chars
!= 0
1504 && index (fci
->format_chars
, format_char
) == 0)
1506 if (fci
->format_chars
== 0)
1508 if (format_char
>= 040 && format_char
< 0177)
1510 "unknown conversion type character `%c' in format",
1514 "unknown conversion type character 0x%x in format",
1519 if (wide
&& index (fci
->flag_chars
, 'w') == 0)
1521 sprintf (message
, "width used with `%c' format",
1525 if (precise
&& index (fci
->flag_chars
, 'p') == 0)
1527 sprintf (message
, "precision used with `%c' format",
1531 if (aflag
&& index (fci
->flag_chars
, 'a') == 0)
1533 sprintf (message
, "`a' flag used with `%c' format",
1536 /* To simplify the following code. */
1539 if (info
->is_scan
&& format_char
== '[')
1541 /* Skip over scan set, in case it happens to have '%' in it. */
1542 if (*format_chars
== '^')
1544 /* Find closing bracket; if one is hit immediately, then
1545 it's part of the scan set rather than a terminator. */
1546 if (*format_chars
== ']')
1548 while (*format_chars
&& *format_chars
!= ']')
1550 if (*format_chars
!= ']')
1551 /* The end of the format string was reached. */
1552 warning ("no closing `]' for `%%[' format");
1556 if (index (fci
->flag_chars
, '*') == 0)
1559 "suppression of `%c' conversion in format",
1565 for (i
= 0; flag_chars
[i
] != 0; ++i
)
1567 if (index (fci
->flag_chars
, flag_chars
[i
]) == 0)
1569 sprintf (message
, "flag `%c' used with type `%c'",
1570 flag_chars
[i
], format_char
);
1574 if (precise
&& index (flag_chars
, '0') != 0
1575 && (format_char
== 'd' || format_char
== 'i'
1576 || format_char
== 'o' || format_char
== 'u'
1577 || format_char
== 'x' || format_char
== 'x'))
1580 "`0' flag ignored with precision specifier and `%c' format",
1584 switch (length_char
)
1586 default: wanted_type
= fci
->nolen
? *(fci
->nolen
) : 0; break;
1587 case 'h': wanted_type
= fci
->hlen
? *(fci
->hlen
) : 0; break;
1588 case 'l': wanted_type
= fci
->llen
? *(fci
->llen
) : 0; break;
1589 case 'q': wanted_type
= fci
->qlen
? *(fci
->qlen
) : 0; break;
1590 case 'L': wanted_type
= fci
->bigllen
? *(fci
->bigllen
) : 0; break;
1592 if (wanted_type
== 0)
1595 "use of `%c' length character with `%c' type character",
1596 length_char
, format_char
);
1601 ** XXX -- should kvetch about stuff such as
1605 ** scanf ("%d", &i);
1609 /* Finally. . .check type of argument against desired type! */
1610 if (info
->first_arg_num
== 0)
1612 if (fci
->pointer_count
== 0 && wanted_type
== void_type_node
)
1613 /* This specifier takes no argument. */
1620 cur_param
= TREE_VALUE (params
);
1621 params
= TREE_CHAIN (params
);
1623 cur_type
= TREE_TYPE (cur_param
);
1625 STRIP_NOPS (cur_param
);
1627 /* Check the types of any additional pointer arguments
1628 that precede the "real" argument. */
1629 for (i
= 0; i
< fci
->pointer_count
+ aflag
; ++i
)
1631 if (TREE_CODE (cur_type
) == POINTER_TYPE
)
1633 cur_type
= TREE_TYPE (cur_type
);
1635 if (TREE_CODE (cur_param
) == ADDR_EXPR
)
1636 cur_param
= TREE_OPERAND (cur_param
, 0);
1642 if (TREE_CODE (cur_type
) != ERROR_MARK
)
1645 "format argument is not a %s (arg %d)",
1646 ((fci
->pointer_count
+ aflag
== 1)
1647 ? "pointer" : "pointer to a pointer"),
1654 /* See if this is an attempt to write into a const type with
1656 if (info
->is_scan
&& i
== fci
->pointer_count
+ aflag
1658 && TREE_CODE (cur_type
) != ERROR_MARK
1659 && (TYPE_READONLY (cur_type
)
1661 && (TREE_CODE_CLASS (TREE_CODE (cur_param
)) == 'c'
1662 || (TREE_CODE_CLASS (TREE_CODE (cur_param
)) == 'd'
1663 && TREE_READONLY (cur_param
))))))
1665 sprintf (message
, "writing into constant object (arg %d)", arg_num
);
1669 /* Check the type of the "real" argument, if there's a type we want. */
1670 if (i
== fci
->pointer_count
+ aflag
&& wanted_type
!= 0
1671 && TREE_CODE (cur_type
) != ERROR_MARK
1672 && wanted_type
!= TYPE_MAIN_VARIANT (cur_type
)
1673 /* If we want `void *', allow any pointer type.
1674 (Anything else would already have got a warning.) */
1675 && ! (wanted_type
== void_type_node
1676 && fci
->pointer_count
> 0)
1677 /* Don't warn about differences merely in signedness. */
1678 && !(TREE_CODE (wanted_type
) == INTEGER_TYPE
1679 && TREE_CODE (TYPE_MAIN_VARIANT (cur_type
)) == INTEGER_TYPE
1680 && (TREE_UNSIGNED (wanted_type
)
1681 ? wanted_type
== (cur_type
= unsigned_type (cur_type
))
1682 : wanted_type
== (cur_type
= signed_type (cur_type
))))
1683 /* Likewise, "signed char", "unsigned char" and "char" are
1684 equivalent but the above test won't consider them equivalent. */
1685 && ! (wanted_type
== char_type_node
1686 && (TYPE_MAIN_VARIANT (cur_type
) == signed_char_type_node
1687 || TYPE_MAIN_VARIANT (cur_type
) == unsigned_char_type_node
)))
1689 register char *this;
1690 register char *that
;
1692 this = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (wanted_type
)));
1694 if (TREE_CODE (cur_type
) != ERROR_MARK
1695 && TYPE_NAME (cur_type
) != 0
1696 && TREE_CODE (cur_type
) != INTEGER_TYPE
1697 && !(TREE_CODE (cur_type
) == POINTER_TYPE
1698 && TREE_CODE (TREE_TYPE (cur_type
)) == INTEGER_TYPE
))
1700 if (TREE_CODE (TYPE_NAME (cur_type
)) == TYPE_DECL
1701 && DECL_NAME (TYPE_NAME (cur_type
)) != 0)
1702 that
= IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type
)));
1704 that
= IDENTIFIER_POINTER (TYPE_NAME (cur_type
));
1707 /* A nameless type can't possibly match what the format wants.
1708 So there will be a warning for it.
1709 Make up a string to describe vaguely what it is. */
1712 if (TREE_CODE (cur_type
) == POINTER_TYPE
)
1715 that
= "different type";
1718 /* Make the warning better in case of mismatch of int vs long. */
1719 if (TREE_CODE (cur_type
) == INTEGER_TYPE
1720 && TREE_CODE (wanted_type
) == INTEGER_TYPE
1721 && TYPE_PRECISION (cur_type
) == TYPE_PRECISION (wanted_type
)
1722 && TYPE_NAME (cur_type
) != 0
1723 && TREE_CODE (TYPE_NAME (cur_type
)) == TYPE_DECL
)
1724 that
= IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type
)));
1726 if (strcmp (this, that
) != 0)
1728 sprintf (message
, "%s format, %s arg (arg %d)",
1729 this, that
, arg_num
);
1736 /* Print a warning if a constant expression had overflow in folding.
1737 Invoke this function on every expression that the language
1738 requires to be a constant expression.
1739 Note the ANSI C standard says it is erroneous for a
1740 constant expression to overflow. */
1743 constant_expression_warning (value
)
1746 if ((TREE_CODE (value
) == INTEGER_CST
|| TREE_CODE (value
) == REAL_CST
1747 || TREE_CODE (value
) == COMPLEX_CST
)
1748 && TREE_CONSTANT_OVERFLOW (value
) && pedantic
)
1749 pedwarn ("overflow in constant expression");
1752 /* Print a warning if an expression had overflow in folding.
1753 Invoke this function on every expression that
1754 (1) appears in the source code, and
1755 (2) might be a constant expression that overflowed, and
1756 (3) is not already checked by convert_and_check;
1757 however, do not invoke this function on operands of explicit casts. */
1760 overflow_warning (value
)
1763 if ((TREE_CODE (value
) == INTEGER_CST
1764 || (TREE_CODE (value
) == COMPLEX_CST
1765 && TREE_CODE (TREE_REALPART (value
)) == INTEGER_CST
))
1766 && TREE_OVERFLOW (value
))
1768 TREE_OVERFLOW (value
) = 0;
1769 if (skip_evaluation
== 0)
1770 warning ("integer overflow in expression");
1772 else if ((TREE_CODE (value
) == REAL_CST
1773 || (TREE_CODE (value
) == COMPLEX_CST
1774 && TREE_CODE (TREE_REALPART (value
)) == REAL_CST
))
1775 && TREE_OVERFLOW (value
))
1777 TREE_OVERFLOW (value
) = 0;
1778 if (skip_evaluation
== 0)
1779 warning ("floating point overflow in expression");
1783 /* Print a warning if a large constant is truncated to unsigned,
1784 or if -Wconversion is used and a constant < 0 is converted to unsigned.
1785 Invoke this function on every expression that might be implicitly
1786 converted to an unsigned type. */
1789 unsigned_conversion_warning (result
, operand
)
1790 tree result
, operand
;
1792 if (TREE_CODE (operand
) == INTEGER_CST
1793 && TREE_CODE (TREE_TYPE (result
)) == INTEGER_TYPE
1794 && TREE_UNSIGNED (TREE_TYPE (result
))
1795 && skip_evaluation
== 0
1796 && !int_fits_type_p (operand
, TREE_TYPE (result
)))
1798 if (!int_fits_type_p (operand
, signed_type (TREE_TYPE (result
))))
1799 /* This detects cases like converting -129 or 256 to unsigned char. */
1800 warning ("large integer implicitly truncated to unsigned type");
1801 else if (warn_conversion
)
1802 warning ("negative integer implicitly converted to unsigned type");
1806 /* Convert EXPR to TYPE, warning about conversion problems with constants.
1807 Invoke this function on every expression that is converted implicitly,
1808 i.e. because of language rules and not because of an explicit cast. */
1811 convert_and_check (type
, expr
)
1814 tree t
= convert (type
, expr
);
1815 if (TREE_CODE (t
) == INTEGER_CST
)
1817 if (TREE_OVERFLOW (t
))
1819 TREE_OVERFLOW (t
) = 0;
1821 /* Do not diagnose overflow in a constant expression merely
1822 because a conversion overflowed. */
1823 TREE_CONSTANT_OVERFLOW (t
) = TREE_CONSTANT_OVERFLOW (expr
);
1825 /* No warning for converting 0x80000000 to int. */
1826 if (!(TREE_UNSIGNED (type
) < TREE_UNSIGNED (TREE_TYPE (expr
))
1827 && TREE_CODE (TREE_TYPE (expr
)) == INTEGER_TYPE
1828 && TYPE_PRECISION (type
) == TYPE_PRECISION (TREE_TYPE (expr
))))
1829 /* If EXPR fits in the unsigned version of TYPE,
1830 don't warn unless pedantic. */
1832 || TREE_UNSIGNED (type
)
1833 || ! int_fits_type_p (expr
, unsigned_type (type
)))
1834 && skip_evaluation
== 0)
1835 warning ("overflow in implicit constant conversion");
1838 unsigned_conversion_warning (t
, expr
);
1844 c_expand_expr_stmt (expr
)
1847 /* Do default conversion if safe and possibly important,
1848 in case within ({...}). */
1849 if ((TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
&& lvalue_p (expr
))
1850 || TREE_CODE (TREE_TYPE (expr
)) == FUNCTION_TYPE
)
1851 expr
= default_conversion (expr
);
1853 if (TREE_TYPE (expr
) != error_mark_node
1854 && TYPE_SIZE (TREE_TYPE (expr
)) == 0
1855 && TREE_CODE (TREE_TYPE (expr
)) != ARRAY_TYPE
)
1856 error ("expression statement has incomplete type");
1858 expand_expr_stmt (expr
);
1861 /* Validate the expression after `case' and apply default promotions. */
1864 check_case_value (value
)
1867 if (value
== NULL_TREE
)
1870 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1871 STRIP_TYPE_NOPS (value
);
1873 if (TREE_CODE (value
) != INTEGER_CST
1874 && value
!= error_mark_node
)
1876 error ("case label does not reduce to an integer constant");
1877 value
= error_mark_node
;
1880 /* Promote char or short to int. */
1881 value
= default_conversion (value
);
1883 constant_expression_warning (value
);
1888 /* Return an integer type with BITS bits of precision,
1889 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
1892 type_for_size (bits
, unsignedp
)
1896 if (bits
== TYPE_PRECISION (integer_type_node
))
1897 return unsignedp
? unsigned_type_node
: integer_type_node
;
1899 if (bits
== TYPE_PRECISION (signed_char_type_node
))
1900 return unsignedp
? unsigned_char_type_node
: signed_char_type_node
;
1902 if (bits
== TYPE_PRECISION (short_integer_type_node
))
1903 return unsignedp
? short_unsigned_type_node
: short_integer_type_node
;
1905 if (bits
== TYPE_PRECISION (long_integer_type_node
))
1906 return unsignedp
? long_unsigned_type_node
: long_integer_type_node
;
1908 if (bits
== TYPE_PRECISION (long_long_integer_type_node
))
1909 return (unsignedp
? long_long_unsigned_type_node
1910 : long_long_integer_type_node
);
1912 if (bits
<= TYPE_PRECISION (intQI_type_node
))
1913 return unsignedp
? unsigned_intQI_type_node
: intQI_type_node
;
1915 if (bits
<= TYPE_PRECISION (intHI_type_node
))
1916 return unsignedp
? unsigned_intHI_type_node
: intHI_type_node
;
1918 if (bits
<= TYPE_PRECISION (intSI_type_node
))
1919 return unsignedp
? unsigned_intSI_type_node
: intSI_type_node
;
1921 if (bits
<= TYPE_PRECISION (intDI_type_node
))
1922 return unsignedp
? unsigned_intDI_type_node
: intDI_type_node
;
1927 /* Return a data type that has machine mode MODE.
1928 If the mode is an integer,
1929 then UNSIGNEDP selects between signed and unsigned types. */
1932 type_for_mode (mode
, unsignedp
)
1933 enum machine_mode mode
;
1936 if (mode
== TYPE_MODE (integer_type_node
))
1937 return unsignedp
? unsigned_type_node
: integer_type_node
;
1939 if (mode
== TYPE_MODE (signed_char_type_node
))
1940 return unsignedp
? unsigned_char_type_node
: signed_char_type_node
;
1942 if (mode
== TYPE_MODE (short_integer_type_node
))
1943 return unsignedp
? short_unsigned_type_node
: short_integer_type_node
;
1945 if (mode
== TYPE_MODE (long_integer_type_node
))
1946 return unsignedp
? long_unsigned_type_node
: long_integer_type_node
;
1948 if (mode
== TYPE_MODE (long_long_integer_type_node
))
1949 return unsignedp
? long_long_unsigned_type_node
: long_long_integer_type_node
;
1951 if (mode
== TYPE_MODE (intQI_type_node
))
1952 return unsignedp
? unsigned_intQI_type_node
: intQI_type_node
;
1954 if (mode
== TYPE_MODE (intHI_type_node
))
1955 return unsignedp
? unsigned_intHI_type_node
: intHI_type_node
;
1957 if (mode
== TYPE_MODE (intSI_type_node
))
1958 return unsignedp
? unsigned_intSI_type_node
: intSI_type_node
;
1960 if (mode
== TYPE_MODE (intDI_type_node
))
1961 return unsignedp
? unsigned_intDI_type_node
: intDI_type_node
;
1963 if (mode
== TYPE_MODE (float_type_node
))
1964 return float_type_node
;
1966 if (mode
== TYPE_MODE (double_type_node
))
1967 return double_type_node
;
1969 if (mode
== TYPE_MODE (long_double_type_node
))
1970 return long_double_type_node
;
1972 if (mode
== TYPE_MODE (build_pointer_type (char_type_node
)))
1973 return build_pointer_type (char_type_node
);
1975 if (mode
== TYPE_MODE (build_pointer_type (integer_type_node
)))
1976 return build_pointer_type (integer_type_node
);
1981 /* Return the minimum number of bits needed to represent VALUE in a
1982 signed or unsigned type, UNSIGNEDP says which. */
1985 min_precision (value
, unsignedp
)
1991 /* If the value is negative, compute its negative minus 1. The latter
1992 adjustment is because the absolute value of the largest negative value
1993 is one larger than the largest positive value. This is equivalent to
1994 a bit-wise negation, so use that operation instead. */
1996 if (tree_int_cst_sgn (value
) < 0)
1997 value
= fold (build1 (BIT_NOT_EXPR
, TREE_TYPE (value
), value
));
1999 /* Return the number of bits needed, taking into account the fact
2000 that we need one more bit for a signed than unsigned type. */
2002 if (integer_zerop (value
))
2004 else if (TREE_INT_CST_HIGH (value
) != 0)
2005 log
= HOST_BITS_PER_WIDE_INT
+ floor_log2 (TREE_INT_CST_HIGH (value
));
2007 log
= floor_log2 (TREE_INT_CST_LOW (value
));
2009 return log
+ 1 + ! unsignedp
;
2012 /* Print an error message for invalid operands to arith operation CODE.
2013 NOP_EXPR is used as a special case (see truthvalue_conversion). */
2016 binary_op_error (code
)
2017 enum tree_code code
;
2019 register char *opname
;
2024 error ("invalid truth-value expression");
2028 opname
= "+"; break;
2030 opname
= "-"; break;
2032 opname
= "*"; break;
2034 opname
= "max"; break;
2036 opname
= "min"; break;
2038 opname
= "=="; break;
2040 opname
= "!="; break;
2042 opname
= "<="; break;
2044 opname
= ">="; break;
2046 opname
= "<"; break;
2048 opname
= ">"; break;
2050 opname
= "<<"; break;
2052 opname
= ">>"; break;
2053 case TRUNC_MOD_EXPR
:
2054 case FLOOR_MOD_EXPR
:
2055 opname
= "%"; break;
2056 case TRUNC_DIV_EXPR
:
2057 case FLOOR_DIV_EXPR
:
2058 opname
= "/"; break;
2060 opname
= "&"; break;
2062 opname
= "|"; break;
2063 case TRUTH_ANDIF_EXPR
:
2064 opname
= "&&"; break;
2065 case TRUTH_ORIF_EXPR
:
2066 opname
= "||"; break;
2068 opname
= "^"; break;
2071 opname
= "rotate"; break;
2073 opname
= "unknown"; break;
2075 error ("invalid operands to binary %s", opname
);
2078 /* Subroutine of build_binary_op, used for comparison operations.
2079 See if the operands have both been converted from subword integer types
2080 and, if so, perhaps change them both back to their original type.
2081 This function is also responsible for converting the two operands
2082 to the proper common type for comparison.
2084 The arguments of this function are all pointers to local variables
2085 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
2086 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
2088 If this function returns nonzero, it means that the comparison has
2089 a constant value. What this function returns is an expression for
2093 shorten_compare (op0_ptr
, op1_ptr
, restype_ptr
, rescode_ptr
)
2094 tree
*op0_ptr
, *op1_ptr
;
2096 enum tree_code
*rescode_ptr
;
2099 tree op0
= *op0_ptr
;
2100 tree op1
= *op1_ptr
;
2101 int unsignedp0
, unsignedp1
;
2103 tree primop0
, primop1
;
2104 enum tree_code code
= *rescode_ptr
;
2106 /* Throw away any conversions to wider types
2107 already present in the operands. */
2109 primop0
= get_narrower (op0
, &unsignedp0
);
2110 primop1
= get_narrower (op1
, &unsignedp1
);
2112 /* Handle the case that OP0 does not *contain* a conversion
2113 but it *requires* conversion to FINAL_TYPE. */
2115 if (op0
== primop0
&& TREE_TYPE (op0
) != *restype_ptr
)
2116 unsignedp0
= TREE_UNSIGNED (TREE_TYPE (op0
));
2117 if (op1
== primop1
&& TREE_TYPE (op1
) != *restype_ptr
)
2118 unsignedp1
= TREE_UNSIGNED (TREE_TYPE (op1
));
2120 /* If one of the operands must be floated, we cannot optimize. */
2121 real1
= TREE_CODE (TREE_TYPE (primop0
)) == REAL_TYPE
;
2122 real2
= TREE_CODE (TREE_TYPE (primop1
)) == REAL_TYPE
;
2124 /* If first arg is constant, swap the args (changing operation
2125 so value is preserved), for canonicalization. Don't do this if
2126 the second arg is 0. */
2128 if (TREE_CONSTANT (primop0
)
2129 && ! integer_zerop (primop1
) && ! real_zerop (primop1
))
2131 register tree tem
= primop0
;
2132 register int temi
= unsignedp0
;
2140 unsignedp0
= unsignedp1
;
2163 *rescode_ptr
= code
;
2166 /* If comparing an integer against a constant more bits wide,
2167 maybe we can deduce a value of 1 or 0 independent of the data.
2168 Or else truncate the constant now
2169 rather than extend the variable at run time.
2171 This is only interesting if the constant is the wider arg.
2172 Also, it is not safe if the constant is unsigned and the
2173 variable arg is signed, since in this case the variable
2174 would be sign-extended and then regarded as unsigned.
2175 Our technique fails in this case because the lowest/highest
2176 possible unsigned results don't follow naturally from the
2177 lowest/highest possible values of the variable operand.
2178 For just EQ_EXPR and NE_EXPR there is another technique that
2179 could be used: see if the constant can be faithfully represented
2180 in the other operand's type, by truncating it and reextending it
2181 and see if that preserves the constant's value. */
2183 if (!real1
&& !real2
2184 && TREE_CODE (primop1
) == INTEGER_CST
2185 && TYPE_PRECISION (TREE_TYPE (primop0
)) < TYPE_PRECISION (*restype_ptr
))
2187 int min_gt
, max_gt
, min_lt
, max_lt
;
2188 tree maxval
, minval
;
2189 /* 1 if comparison is nominally unsigned. */
2190 int unsignedp
= TREE_UNSIGNED (*restype_ptr
);
2193 type
= signed_or_unsigned_type (unsignedp0
, TREE_TYPE (primop0
));
2195 maxval
= TYPE_MAX_VALUE (type
);
2196 minval
= TYPE_MIN_VALUE (type
);
2198 if (unsignedp
&& !unsignedp0
)
2199 *restype_ptr
= signed_type (*restype_ptr
);
2201 if (TREE_TYPE (primop1
) != *restype_ptr
)
2202 primop1
= convert (*restype_ptr
, primop1
);
2203 if (type
!= *restype_ptr
)
2205 minval
= convert (*restype_ptr
, minval
);
2206 maxval
= convert (*restype_ptr
, maxval
);
2209 if (unsignedp
&& unsignedp0
)
2211 min_gt
= INT_CST_LT_UNSIGNED (primop1
, minval
);
2212 max_gt
= INT_CST_LT_UNSIGNED (primop1
, maxval
);
2213 min_lt
= INT_CST_LT_UNSIGNED (minval
, primop1
);
2214 max_lt
= INT_CST_LT_UNSIGNED (maxval
, primop1
);
2218 min_gt
= INT_CST_LT (primop1
, minval
);
2219 max_gt
= INT_CST_LT (primop1
, maxval
);
2220 min_lt
= INT_CST_LT (minval
, primop1
);
2221 max_lt
= INT_CST_LT (maxval
, primop1
);
2225 /* This used to be a switch, but Genix compiler can't handle that. */
2226 if (code
== NE_EXPR
)
2228 if (max_lt
|| min_gt
)
2229 val
= boolean_true_node
;
2231 else if (code
== EQ_EXPR
)
2233 if (max_lt
|| min_gt
)
2234 val
= boolean_false_node
;
2236 else if (code
== LT_EXPR
)
2239 val
= boolean_true_node
;
2241 val
= boolean_false_node
;
2243 else if (code
== GT_EXPR
)
2246 val
= boolean_true_node
;
2248 val
= boolean_false_node
;
2250 else if (code
== LE_EXPR
)
2253 val
= boolean_true_node
;
2255 val
= boolean_false_node
;
2257 else if (code
== GE_EXPR
)
2260 val
= boolean_true_node
;
2262 val
= boolean_false_node
;
2265 /* If primop0 was sign-extended and unsigned comparison specd,
2266 we did a signed comparison above using the signed type bounds.
2267 But the comparison we output must be unsigned.
2269 Also, for inequalities, VAL is no good; but if the signed
2270 comparison had *any* fixed result, it follows that the
2271 unsigned comparison just tests the sign in reverse
2272 (positive values are LE, negative ones GE).
2273 So we can generate an unsigned comparison
2274 against an extreme value of the signed type. */
2276 if (unsignedp
&& !unsignedp0
)
2283 primop1
= TYPE_MIN_VALUE (type
);
2289 primop1
= TYPE_MAX_VALUE (type
);
2296 type
= unsigned_type (type
);
2299 if (!max_gt
&& !unsignedp0
&& TREE_CODE (primop0
) != INTEGER_CST
)
2301 /* This is the case of (char)x >?< 0x80, which people used to use
2302 expecting old C compilers to change the 0x80 into -0x80. */
2303 if (val
== boolean_false_node
)
2304 warning ("comparison is always 0 due to limited range of data type");
2305 if (val
== boolean_true_node
)
2306 warning ("comparison is always 1 due to limited range of data type");
2309 if (!min_lt
&& unsignedp0
&& TREE_CODE (primop0
) != INTEGER_CST
)
2311 /* This is the case of (unsigned char)x >?< -1 or < 0. */
2312 if (val
== boolean_false_node
)
2313 warning ("comparison is always 0 due to limited range of data type");
2314 if (val
== boolean_true_node
)
2315 warning ("comparison is always 1 due to limited range of data type");
2320 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2321 if (TREE_SIDE_EFFECTS (primop0
))
2322 return build (COMPOUND_EXPR
, TREE_TYPE (val
), primop0
, val
);
2326 /* Value is not predetermined, but do the comparison
2327 in the type of the operand that is not constant.
2328 TYPE is already properly set. */
2330 else if (real1
&& real2
2331 && (TYPE_PRECISION (TREE_TYPE (primop0
))
2332 == TYPE_PRECISION (TREE_TYPE (primop1
))))
2333 type
= TREE_TYPE (primop0
);
2335 /* If args' natural types are both narrower than nominal type
2336 and both extend in the same manner, compare them
2337 in the type of the wider arg.
2338 Otherwise must actually extend both to the nominal
2339 common type lest different ways of extending
2341 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
2343 else if (unsignedp0
== unsignedp1
&& real1
== real2
2344 && TYPE_PRECISION (TREE_TYPE (primop0
)) < TYPE_PRECISION (*restype_ptr
)
2345 && TYPE_PRECISION (TREE_TYPE (primop1
)) < TYPE_PRECISION (*restype_ptr
))
2347 type
= common_type (TREE_TYPE (primop0
), TREE_TYPE (primop1
));
2348 type
= signed_or_unsigned_type (unsignedp0
2349 || TREE_UNSIGNED (*restype_ptr
),
2351 /* Make sure shorter operand is extended the right way
2352 to match the longer operand. */
2353 primop0
= convert (signed_or_unsigned_type (unsignedp0
, TREE_TYPE (primop0
)),
2355 primop1
= convert (signed_or_unsigned_type (unsignedp1
, TREE_TYPE (primop1
)),
2360 /* Here we must do the comparison on the nominal type
2361 using the args exactly as we received them. */
2362 type
= *restype_ptr
;
2366 if (!real1
&& !real2
&& integer_zerop (primop1
)
2367 && TREE_UNSIGNED (*restype_ptr
))
2373 /* All unsigned values are >= 0, so we warn if extra warnings
2374 are requested. However, if OP0 is a constant that is
2375 >= 0, the signedness of the comparison isn't an issue,
2376 so suppress the warning. */
2378 && ! (TREE_CODE (primop0
) == INTEGER_CST
2379 && ! TREE_OVERFLOW (convert (signed_type (type
),
2381 warning ("unsigned value >= 0 is always 1");
2382 value
= boolean_true_node
;
2387 && ! (TREE_CODE (primop0
) == INTEGER_CST
2388 && ! TREE_OVERFLOW (convert (signed_type (type
),
2390 warning ("unsigned value < 0 is always 0");
2391 value
= boolean_false_node
;
2400 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2401 if (TREE_SIDE_EFFECTS (primop0
))
2402 return build (COMPOUND_EXPR
, TREE_TYPE (value
),
2409 *op0_ptr
= convert (type
, primop0
);
2410 *op1_ptr
= convert (type
, primop1
);
2412 *restype_ptr
= boolean_type_node
;
2417 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
2418 or validate its data type for an `if' or `while' statement or ?..: exp.
2420 This preparation consists of taking the ordinary
2421 representation of an expression expr and producing a valid tree
2422 boolean expression describing whether expr is nonzero. We could
2423 simply always do build_binary_op (NE_EXPR, expr, boolean_false_node, 1),
2424 but we optimize comparisons, &&, ||, and !.
2426 The resulting type should always be `boolean_type_node'. */
2429 truthvalue_conversion (expr
)
2432 if (TREE_CODE (expr
) == ERROR_MARK
)
2435 #if 0 /* This appears to be wrong for C++. */
2436 /* These really should return error_mark_node after 2.4 is stable.
2437 But not all callers handle ERROR_MARK properly. */
2438 switch (TREE_CODE (TREE_TYPE (expr
)))
2441 error ("struct type value used where scalar is required");
2442 return boolean_false_node
;
2445 error ("union type value used where scalar is required");
2446 return boolean_false_node
;
2449 error ("array type value used where scalar is required");
2450 return boolean_false_node
;
2457 switch (TREE_CODE (expr
))
2459 /* It is simpler and generates better code to have only TRUTH_*_EXPR
2460 or comparison expressions as truth values at this level. */
2463 /* A one-bit unsigned bit-field is already acceptable. */
2464 if (1 == TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (expr
, 1)))
2465 && TREE_UNSIGNED (TREE_OPERAND (expr
, 1)))
2471 /* It is simpler and generates better code to have only TRUTH_*_EXPR
2472 or comparison expressions as truth values at this level. */
2474 if (integer_zerop (TREE_OPERAND (expr
, 1)))
2475 return build_unary_op (TRUTH_NOT_EXPR
, TREE_OPERAND (expr
, 0), 0);
2477 case NE_EXPR
: case LE_EXPR
: case GE_EXPR
: case LT_EXPR
: case GT_EXPR
:
2478 case TRUTH_ANDIF_EXPR
:
2479 case TRUTH_ORIF_EXPR
:
2480 case TRUTH_AND_EXPR
:
2482 case TRUTH_XOR_EXPR
:
2483 case TRUTH_NOT_EXPR
:
2484 TREE_TYPE (expr
) = boolean_type_node
;
2491 return integer_zerop (expr
) ? boolean_false_node
: boolean_true_node
;
2494 return real_zerop (expr
) ? boolean_false_node
: boolean_true_node
;
2497 /* If we are taking the address of a external decl, it might be zero
2498 if it is weak, so we cannot optimize. */
2499 if (TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (expr
, 0))) == 'd'
2500 && DECL_EXTERNAL (TREE_OPERAND (expr
, 0)))
2503 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr
, 0)))
2504 return build (COMPOUND_EXPR
, boolean_type_node
,
2505 TREE_OPERAND (expr
, 0), boolean_true_node
);
2507 return boolean_true_node
;
2510 return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr
, 1))
2511 ? TRUTH_OR_EXPR
: TRUTH_ORIF_EXPR
),
2512 truthvalue_conversion (TREE_OPERAND (expr
, 0)),
2513 truthvalue_conversion (TREE_OPERAND (expr
, 1)),
2520 /* These don't change whether an object is non-zero or zero. */
2521 return truthvalue_conversion (TREE_OPERAND (expr
, 0));
2525 /* These don't change whether an object is zero or non-zero, but
2526 we can't ignore them if their second arg has side-effects. */
2527 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr
, 1)))
2528 return build (COMPOUND_EXPR
, boolean_type_node
, TREE_OPERAND (expr
, 1),
2529 truthvalue_conversion (TREE_OPERAND (expr
, 0)));
2531 return truthvalue_conversion (TREE_OPERAND (expr
, 0));
2534 /* Distribute the conversion into the arms of a COND_EXPR. */
2535 return fold (build (COND_EXPR
, boolean_type_node
, TREE_OPERAND (expr
, 0),
2536 truthvalue_conversion (TREE_OPERAND (expr
, 1)),
2537 truthvalue_conversion (TREE_OPERAND (expr
, 2))));
2540 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
2541 since that affects how `default_conversion' will behave. */
2542 if (TREE_CODE (TREE_TYPE (expr
)) == REFERENCE_TYPE
2543 || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0))) == REFERENCE_TYPE
)
2545 /* fall through... */
2547 /* If this is widening the argument, we can ignore it. */
2548 if (TYPE_PRECISION (TREE_TYPE (expr
))
2549 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr
, 0))))
2550 return truthvalue_conversion (TREE_OPERAND (expr
, 0));
2554 /* With IEEE arithmetic, x - x may not equal 0, so we can't optimize
2556 if (TARGET_FLOAT_FORMAT
== IEEE_FLOAT_FORMAT
2557 && TREE_CODE (TREE_TYPE (expr
)) == REAL_TYPE
)
2559 /* fall through... */
2561 /* This and MINUS_EXPR can be changed into a comparison of the
2563 if (TREE_TYPE (TREE_OPERAND (expr
, 0))
2564 == TREE_TYPE (TREE_OPERAND (expr
, 1)))
2565 return build_binary_op (NE_EXPR
, TREE_OPERAND (expr
, 0),
2566 TREE_OPERAND (expr
, 1), 1);
2567 return build_binary_op (NE_EXPR
, TREE_OPERAND (expr
, 0),
2568 fold (build1 (NOP_EXPR
,
2569 TREE_TYPE (TREE_OPERAND (expr
, 0)),
2570 TREE_OPERAND (expr
, 1))), 1);
2573 if (integer_onep (TREE_OPERAND (expr
, 1))
2574 && TREE_TYPE (expr
) != boolean_type_node
)
2575 /* Using convert here would cause infinite recursion. */
2576 return build1 (NOP_EXPR
, boolean_type_node
, expr
);
2580 if (warn_parentheses
&& C_EXP_ORIGINAL_CODE (expr
) == MODIFY_EXPR
)
2581 warning ("suggest parentheses around assignment used as truth value");
2588 if (TREE_CODE (TREE_TYPE (expr
)) == COMPLEX_TYPE
)
2589 return (build_binary_op
2590 ((TREE_SIDE_EFFECTS (expr
)
2591 ? TRUTH_OR_EXPR
: TRUTH_ORIF_EXPR
),
2592 truthvalue_conversion (build_unary_op (REALPART_EXPR
, expr
, 0)),
2593 truthvalue_conversion (build_unary_op (IMAGPART_EXPR
, expr
, 0)),
2596 return build_binary_op (NE_EXPR
, expr
, integer_zero_node
, 1);
2599 /* Read the rest of a #-directive from input stream FINPUT.
2600 In normal use, the directive name and the white space after it
2601 have already been read, so they won't be included in the result.
2602 We allow for the fact that the directive line may contain
2603 a newline embedded within a character or string literal which forms
2604 a part of the directive.
2606 The value is a string in a reusable buffer. It remains valid
2607 only until the next time this function is called.
2609 The terminating character ('\n' or EOF) is left in FINPUT for the
2610 caller to re-read. */
2613 get_directive_line (finput
)
2614 register FILE *finput
;
2616 static char *directive_buffer
= NULL
;
2617 static unsigned buffer_length
= 0;
2619 register char *buffer_limit
;
2620 register int looking_for
= 0;
2621 register int char_escaped
= 0;
2623 if (buffer_length
== 0)
2625 directive_buffer
= (char *)xmalloc (128);
2626 buffer_length
= 128;
2629 buffer_limit
= &directive_buffer
[buffer_length
];
2631 for (p
= directive_buffer
; ; )
2635 /* Make buffer bigger if it is full. */
2636 if (p
>= buffer_limit
)
2638 register unsigned bytes_used
= (p
- directive_buffer
);
2642 = (char *)xrealloc (directive_buffer
, buffer_length
);
2643 p
= &directive_buffer
[bytes_used
];
2644 buffer_limit
= &directive_buffer
[buffer_length
];
2649 /* Discard initial whitespace. */
2650 if ((c
== ' ' || c
== '\t') && p
== directive_buffer
)
2653 /* Detect the end of the directive. */
2654 if (looking_for
== 0
2655 && (c
== '\n' || c
== EOF
))
2664 return directive_buffer
;
2666 /* Handle string and character constant syntax. */
2669 if (looking_for
== c
&& !char_escaped
)
2670 looking_for
= 0; /* Found terminator... stop looking. */
2673 if (c
== '\'' || c
== '"')
2674 looking_for
= c
; /* Don't stop buffering until we see another
2675 another one of these (or an EOF). */
2677 /* Handle backslash. */
2678 char_escaped
= (c
== '\\' && ! char_escaped
);
2682 /* Make a variant type in the proper way for C/C++, propagating qualifiers
2683 down to the element type of an array. */
2686 c_build_type_variant (type
, constp
, volatilep
)
2688 int constp
, volatilep
;
2690 if (TREE_CODE (type
) == ARRAY_TYPE
)
2691 return build_array_type (c_build_type_variant (TREE_TYPE (type
),
2693 TYPE_DOMAIN (type
));
2694 return build_type_variant (type
, constp
, volatilep
);