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. */
30 #ifndef WCHAR_TYPE_SIZE
32 #define WCHAR_TYPE_SIZE INT_TYPE_SIZE
34 #define WCHAR_TYPE_SIZE BITS_PER_WORD
38 extern struct obstack permanent_obstack
;
40 /* Nonzero means the expression being parsed will never be evaluated.
41 This is a count, since unevaluated expressions can nest. */
44 enum attrs
{A_PACKED
, A_NOCOMMON
, A_COMMON
, A_NORETURN
, A_CONST
, A_T_UNION
,
45 A_CONSTRUCTOR
, A_DESTRUCTOR
, A_MODE
, A_SECTION
, A_ALIGNED
,
46 A_UNUSED
, A_FORMAT
, A_FORMAT_ARG
, A_WEAK
, A_ALIAS
};
48 static void declare_hidden_char_array
PROTO((char *, char *));
49 static void add_attribute
PROTO((enum attrs
, char *,
51 static void init_attributes
PROTO((void));
52 static void record_international_format
PROTO((tree
, tree
, int));
54 /* Keep a stack of if statements. The value recorded is the number of
55 compound statements seen up to the if keyword. */
58 /* Amount of space in the if statement stack. */
59 static int if_stack_space
= 0;
62 static int if_stack_pointer
= 0;
65 c_expand_start_cond (cond
, exitflag
, compstmt_count
)
70 /* Make sure there is enough space on the stack. */
71 if (if_stack_space
== 0)
74 if_stack
= (int *)xmalloc (10 * sizeof (int));
76 else if (if_stack_space
== if_stack_pointer
)
79 if_stack
= (int *)xrealloc (if_stack
, if_stack_space
* sizeof (int));
82 /* Record this if statement. */
83 if_stack
[if_stack_pointer
++] = compstmt_count
;
85 expand_start_cond (cond
, exitflag
);
96 c_expand_start_else ()
99 && if_stack_pointer
> 1
100 && if_stack
[if_stack_pointer
- 1] == if_stack
[if_stack_pointer
- 2])
101 warning ("suggest explicit braces to avoid ambiguous `else'");
103 /* This if statement can no longer cause a dangling else. */
104 if_stack
[if_stack_pointer
- 1]--;
106 expand_start_else ();
109 /* Make bindings for __FUNCTION__ and __PRETTY_FUNCTION__. */
112 declare_function_name ()
114 char *name
, *printable_name
;
116 if (current_function_decl
== NULL
)
119 printable_name
= "top level";
123 /* Allow functions to be nameless (such as artificial ones). */
124 if (DECL_NAME (current_function_decl
))
125 name
= IDENTIFIER_POINTER (DECL_NAME (current_function_decl
));
128 printable_name
= (*decl_printable_name
) (current_function_decl
, 2);
131 declare_hidden_char_array ("__FUNCTION__", name
);
132 declare_hidden_char_array ("__PRETTY_FUNCTION__", printable_name
);
136 declare_hidden_char_array (name
, value
)
139 tree decl
, type
, init
;
142 /* If the default size of char arrays isn't big enough for the name,
143 or if we want to give warnings for large objects, make a bigger one. */
144 vlen
= strlen (value
) + 1;
145 type
= char_array_type_node
;
146 if (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TREE_TYPE (type
))) < vlen
148 type
= build_array_type (char_type_node
,
149 build_index_type (build_int_2 (vlen
, 0)));
150 push_obstacks_nochange ();
151 decl
= build_decl (VAR_DECL
, get_identifier (name
), type
);
152 TREE_STATIC (decl
) = 1;
153 TREE_READONLY (decl
) = 1;
154 TREE_ASM_WRITTEN (decl
) = 1;
155 DECL_SOURCE_LINE (decl
) = 0;
156 DECL_ARTIFICIAL (decl
) = 1;
157 DECL_IN_SYSTEM_HEADER (decl
) = 1;
158 DECL_IGNORED_P (decl
) = 1;
159 init
= build_string (vlen
, value
);
160 TREE_TYPE (init
) = type
;
161 DECL_INITIAL (decl
) = init
;
162 finish_decl (pushdecl (decl
), init
, NULL_TREE
);
165 /* Given a chain of STRING_CST nodes,
166 concatenate them into one STRING_CST
167 and give it a suitable array-of-chars data type. */
170 combine_strings (strings
)
173 register tree value
, t
;
174 register int length
= 1;
177 int wchar_bytes
= TYPE_PRECISION (wchar_type_node
) / BITS_PER_UNIT
;
180 if (TREE_CHAIN (strings
))
182 /* More than one in the chain, so concatenate. */
183 register char *p
, *q
;
185 /* Don't include the \0 at the end of each substring,
186 except for the last one.
187 Count wide strings and ordinary strings separately. */
188 for (t
= strings
; t
; t
= TREE_CHAIN (t
))
190 if (TREE_TYPE (t
) == wchar_array_type_node
)
192 wide_length
+= (TREE_STRING_LENGTH (t
) - wchar_bytes
);
196 length
+= (TREE_STRING_LENGTH (t
) - 1);
199 /* If anything is wide, the non-wides will be converted,
200 which makes them take more space. */
202 length
= length
* wchar_bytes
+ wide_length
;
204 p
= savealloc (length
);
206 /* Copy the individual strings into the new combined string.
207 If the combined string is wide, convert the chars to ints
208 for any individual strings that are not wide. */
211 for (t
= strings
; t
; t
= TREE_CHAIN (t
))
213 int len
= (TREE_STRING_LENGTH (t
)
214 - ((TREE_TYPE (t
) == wchar_array_type_node
)
216 if ((TREE_TYPE (t
) == wchar_array_type_node
) == wide_flag
)
218 bcopy (TREE_STRING_POINTER (t
), q
, len
);
224 for (i
= 0; i
< len
; i
++)
226 if (WCHAR_TYPE_SIZE
== HOST_BITS_PER_SHORT
)
227 ((short *) q
)[i
] = TREE_STRING_POINTER (t
)[i
];
229 ((int *) q
)[i
] = TREE_STRING_POINTER (t
)[i
];
231 q
+= len
* wchar_bytes
;
237 for (i
= 0; i
< wchar_bytes
; i
++)
243 value
= make_node (STRING_CST
);
244 TREE_STRING_POINTER (value
) = p
;
245 TREE_STRING_LENGTH (value
) = length
;
246 TREE_CONSTANT (value
) = 1;
251 length
= TREE_STRING_LENGTH (value
);
252 if (TREE_TYPE (value
) == wchar_array_type_node
)
256 /* Compute the number of elements, for the array type. */
257 nchars
= wide_flag
? length
/ wchar_bytes
: length
;
259 /* Create the array type for the string constant.
260 -Wwrite-strings says make the string constant an array of const char
261 so that copying it to a non-const pointer will get a warning. */
262 if (warn_write_strings
263 && (! flag_traditional
&& ! flag_writable_strings
))
266 = build_type_variant (wide_flag
? wchar_type_node
: char_type_node
,
269 = build_array_type (elements
,
270 build_index_type (build_int_2 (nchars
- 1, 0)));
274 = build_array_type (wide_flag
? wchar_type_node
: char_type_node
,
275 build_index_type (build_int_2 (nchars
- 1, 0)));
276 TREE_CONSTANT (value
) = 1;
277 TREE_STATIC (value
) = 1;
281 /* To speed up processing of attributes, we maintain an array of
282 IDENTIFIER_NODES and the corresponding attribute types. */
284 /* Array to hold attribute information. */
286 static struct {enum attrs id
; tree name
; int min
, max
, decl_req
;} attrtab
[50];
288 static int attrtab_idx
= 0;
290 /* Add an entry to the attribute table above. */
293 add_attribute (id
, string
, min_len
, max_len
, decl_req
)
296 int min_len
, max_len
;
301 attrtab
[attrtab_idx
].id
= id
;
302 attrtab
[attrtab_idx
].name
= get_identifier (string
);
303 attrtab
[attrtab_idx
].min
= min_len
;
304 attrtab
[attrtab_idx
].max
= max_len
;
305 attrtab
[attrtab_idx
++].decl_req
= decl_req
;
307 sprintf (buf
, "__%s__", string
);
309 attrtab
[attrtab_idx
].id
= id
;
310 attrtab
[attrtab_idx
].name
= get_identifier (buf
);
311 attrtab
[attrtab_idx
].min
= min_len
;
312 attrtab
[attrtab_idx
].max
= max_len
;
313 attrtab
[attrtab_idx
++].decl_req
= decl_req
;
316 /* Initialize attribute table. */
321 add_attribute (A_PACKED
, "packed", 0, 0, 0);
322 add_attribute (A_NOCOMMON
, "nocommon", 0, 0, 1);
323 add_attribute (A_COMMON
, "common", 0, 0, 1);
324 add_attribute (A_NORETURN
, "noreturn", 0, 0, 1);
325 add_attribute (A_NORETURN
, "volatile", 0, 0, 1);
326 add_attribute (A_UNUSED
, "unused", 0, 0, 0);
327 add_attribute (A_CONST
, "const", 0, 0, 1);
328 add_attribute (A_T_UNION
, "transparent_union", 0, 0, 0);
329 add_attribute (A_CONSTRUCTOR
, "constructor", 0, 0, 1);
330 add_attribute (A_DESTRUCTOR
, "destructor", 0, 0, 1);
331 add_attribute (A_MODE
, "mode", 1, 1, 1);
332 add_attribute (A_SECTION
, "section", 1, 1, 1);
333 add_attribute (A_ALIGNED
, "aligned", 0, 1, 0);
334 add_attribute (A_FORMAT
, "format", 3, 3, 1);
335 add_attribute (A_FORMAT_ARG
, "format_arg", 1, 1, 1);
336 add_attribute (A_WEAK
, "weak", 0, 0, 1);
337 add_attribute (A_ALIAS
, "alias", 1, 1, 1);
340 /* Process the attributes listed in ATTRIBUTES and PREFIX_ATTRIBUTES
341 and install them in NODE, which is either a DECL (including a TYPE_DECL)
342 or a TYPE. PREFIX_ATTRIBUTES can appear after the declaration specifiers
343 and declaration modifiers but before the declaration proper. */
346 decl_attributes (node
, attributes
, prefix_attributes
)
347 tree node
, attributes
, prefix_attributes
;
353 if (attrtab_idx
== 0)
356 if (TREE_CODE_CLASS (TREE_CODE (node
)) == 'd')
359 type
= TREE_TYPE (decl
);
360 is_type
= TREE_CODE (node
) == TYPE_DECL
;
362 else if (TREE_CODE_CLASS (TREE_CODE (node
)) == 't')
363 type
= node
, is_type
= 1;
365 attributes
= chainon (prefix_attributes
, attributes
);
367 for (a
= attributes
; a
; a
= TREE_CHAIN (a
))
369 tree name
= TREE_PURPOSE (a
);
370 tree args
= TREE_VALUE (a
);
374 for (i
= 0; i
< attrtab_idx
; i
++)
375 if (attrtab
[i
].name
== name
)
378 if (i
== attrtab_idx
)
380 if (! valid_machine_attribute (name
, args
, decl
, type
))
381 warning ("`%s' attribute directive ignored",
382 IDENTIFIER_POINTER (name
));
384 type
= TREE_TYPE (decl
);
387 else if (attrtab
[i
].decl_req
&& decl
== 0)
389 warning ("`%s' attribute does not apply to types",
390 IDENTIFIER_POINTER (name
));
393 else if (list_length (args
) < attrtab
[i
].min
394 || list_length (args
) > attrtab
[i
].max
)
396 error ("wrong number of arguments specified for `%s' attribute",
397 IDENTIFIER_POINTER (name
));
406 TYPE_PACKED (type
) = 1;
407 else if (TREE_CODE (decl
) == FIELD_DECL
)
408 DECL_PACKED (decl
) = 1;
409 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
410 used for DECL_REGISTER. It wouldn't mean anything anyway. */
412 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
416 if (TREE_CODE (decl
) == VAR_DECL
)
417 DECL_COMMON (decl
) = 0;
419 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
423 if (TREE_CODE (decl
) == VAR_DECL
)
424 DECL_COMMON (decl
) = 1;
426 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
430 if (TREE_CODE (decl
) == FUNCTION_DECL
)
431 TREE_THIS_VOLATILE (decl
) = 1;
432 else if (TREE_CODE (type
) == POINTER_TYPE
433 && TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
)
434 TREE_TYPE (decl
) = type
436 (build_type_variant (TREE_TYPE (type
),
437 TREE_READONLY (TREE_TYPE (type
)), 1));
439 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
444 TREE_USED (type
) = 1;
445 else if (TREE_CODE (decl
) == PARM_DECL
446 || TREE_CODE (decl
) == VAR_DECL
447 || TREE_CODE (decl
) == FUNCTION_DECL
)
448 TREE_USED (decl
) = 1;
450 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
454 if (TREE_CODE (decl
) == FUNCTION_DECL
)
455 TREE_READONLY (decl
) = 1;
456 else if (TREE_CODE (type
) == POINTER_TYPE
457 && TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
)
458 TREE_TYPE (decl
) = type
460 (build_type_variant (TREE_TYPE (type
), 1,
461 TREE_THIS_VOLATILE (TREE_TYPE (type
))));
463 warning ( "`%s' attribute ignored", IDENTIFIER_POINTER (name
));
468 && TREE_CODE (type
) == UNION_TYPE
470 || (TYPE_FIELDS (type
) != 0
471 && TYPE_MODE (type
) == DECL_MODE (TYPE_FIELDS (type
)))))
472 TYPE_TRANSPARENT_UNION (type
) = 1;
473 else if (decl
!= 0 && TREE_CODE (decl
) == PARM_DECL
474 && TREE_CODE (type
) == UNION_TYPE
475 && TYPE_MODE (type
) == DECL_MODE (TYPE_FIELDS (type
)))
476 DECL_TRANSPARENT_UNION (decl
) = 1;
478 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
482 if (TREE_CODE (decl
) == FUNCTION_DECL
483 && TREE_CODE (type
) == FUNCTION_TYPE
484 && decl_function_context (decl
) == 0)
486 DECL_STATIC_CONSTRUCTOR (decl
) = 1;
487 TREE_USED (decl
) = 1;
490 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
494 if (TREE_CODE (decl
) == FUNCTION_DECL
495 && TREE_CODE (type
) == FUNCTION_TYPE
496 && decl_function_context (decl
) == 0)
498 DECL_STATIC_DESTRUCTOR (decl
) = 1;
499 TREE_USED (decl
) = 1;
502 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
506 if (TREE_CODE (TREE_VALUE (args
)) != IDENTIFIER_NODE
)
507 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
511 char *p
= IDENTIFIER_POINTER (TREE_VALUE (args
));
512 int len
= strlen (p
);
513 enum machine_mode mode
= VOIDmode
;
516 if (len
> 4 && p
[0] == '_' && p
[1] == '_'
517 && p
[len
- 1] == '_' && p
[len
- 2] == '_')
519 char *newp
= (char *) alloca (len
- 1);
521 strcpy (newp
, &p
[2]);
522 newp
[len
- 4] = '\0';
526 /* Give this decl a type with the specified mode.
527 First check for the special modes. */
528 if (! strcmp (p
, "byte"))
530 else if (!strcmp (p
, "word"))
532 else if (! strcmp (p
, "pointer"))
535 for (j
= 0; j
< NUM_MACHINE_MODES
; j
++)
536 if (!strcmp (p
, GET_MODE_NAME (j
)))
537 mode
= (enum machine_mode
) j
;
539 if (mode
== VOIDmode
)
540 error ("unknown machine mode `%s'", p
);
541 else if (0 == (typefm
= type_for_mode (mode
,
542 TREE_UNSIGNED (type
))))
543 error ("no data type for mode `%s'", p
);
546 TREE_TYPE (decl
) = type
= typefm
;
547 DECL_SIZE (decl
) = 0;
548 layout_decl (decl
, 0);
554 #ifdef ASM_OUTPUT_SECTION_NAME
555 if ((TREE_CODE (decl
) == FUNCTION_DECL
556 || TREE_CODE (decl
) == VAR_DECL
)
557 && TREE_CODE (TREE_VALUE (args
)) == STRING_CST
)
559 if (TREE_CODE (decl
) == VAR_DECL
560 && current_function_decl
!= NULL_TREE
561 && ! TREE_STATIC (decl
))
562 error_with_decl (decl
,
563 "section attribute cannot be specified for local variables");
564 /* The decl may have already been given a section attribute from
565 a previous declaration. Ensure they match. */
566 else if (DECL_SECTION_NAME (decl
) != NULL_TREE
567 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl
)),
568 TREE_STRING_POINTER (TREE_VALUE (args
))) != 0)
569 error_with_decl (node
,
570 "section of `%s' conflicts with previous declaration");
572 DECL_SECTION_NAME (decl
) = TREE_VALUE (args
);
575 error_with_decl (node
,
576 "section attribute not allowed for `%s'");
578 error_with_decl (node
,
579 "section attributes are not supported for this target");
586 = (args
? TREE_VALUE (args
)
587 : size_int (BIGGEST_ALIGNMENT
/ BITS_PER_UNIT
));
590 /* Strip any NOPs of any kind. */
591 while (TREE_CODE (align_expr
) == NOP_EXPR
592 || TREE_CODE (align_expr
) == CONVERT_EXPR
593 || TREE_CODE (align_expr
) == NON_LVALUE_EXPR
)
594 align_expr
= TREE_OPERAND (align_expr
, 0);
596 if (TREE_CODE (align_expr
) != INTEGER_CST
)
598 error ("requested alignment is not a constant");
602 align
= TREE_INT_CST_LOW (align_expr
) * BITS_PER_UNIT
;
604 if (exact_log2 (align
) == -1)
605 error ("requested alignment is not a power of 2");
607 TYPE_ALIGN (type
) = align
;
608 else if (TREE_CODE (decl
) != VAR_DECL
609 && TREE_CODE (decl
) != FIELD_DECL
)
610 error_with_decl (decl
,
611 "alignment may not be specified for `%s'");
613 DECL_ALIGN (decl
) = align
;
619 tree format_type
= TREE_VALUE (args
);
620 tree format_num_expr
= TREE_VALUE (TREE_CHAIN (args
));
621 tree first_arg_num_expr
622 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args
)));
629 if (TREE_CODE (decl
) != FUNCTION_DECL
)
631 error_with_decl (decl
,
632 "argument format specified for non-function `%s'");
636 if (TREE_CODE (format_type
) == IDENTIFIER_NODE
637 && (!strcmp (IDENTIFIER_POINTER (format_type
), "printf")
638 || !strcmp (IDENTIFIER_POINTER (format_type
),
641 else if (TREE_CODE (format_type
) == IDENTIFIER_NODE
642 && (!strcmp (IDENTIFIER_POINTER (format_type
), "scanf")
643 || !strcmp (IDENTIFIER_POINTER (format_type
),
646 else if (TREE_CODE (format_type
) == IDENTIFIER_NODE
)
648 warning ("`%s' is an unrecognized format function type",
649 IDENTIFIER_POINTER (format_type
));
654 error ("unrecognized format specifier");
658 /* Strip any conversions from the string index and first arg number
659 and verify they are constants. */
660 while (TREE_CODE (format_num_expr
) == NOP_EXPR
661 || TREE_CODE (format_num_expr
) == CONVERT_EXPR
662 || TREE_CODE (format_num_expr
) == NON_LVALUE_EXPR
)
663 format_num_expr
= TREE_OPERAND (format_num_expr
, 0);
665 while (TREE_CODE (first_arg_num_expr
) == NOP_EXPR
666 || TREE_CODE (first_arg_num_expr
) == CONVERT_EXPR
667 || TREE_CODE (first_arg_num_expr
) == NON_LVALUE_EXPR
)
668 first_arg_num_expr
= TREE_OPERAND (first_arg_num_expr
, 0);
670 if (TREE_CODE (format_num_expr
) != INTEGER_CST
671 || TREE_CODE (first_arg_num_expr
) != INTEGER_CST
)
673 error ("format string has non-constant operand number");
677 format_num
= TREE_INT_CST_LOW (format_num_expr
);
678 first_arg_num
= TREE_INT_CST_LOW (first_arg_num_expr
);
679 if (first_arg_num
!= 0 && first_arg_num
<= format_num
)
681 error ("format string arg follows the args to be formatted");
685 /* If a parameter list is specified, verify that the format_num
686 argument is actually a string, in case the format attribute
688 argument
= TYPE_ARG_TYPES (type
);
691 for (arg_num
= 1; ; ++arg_num
)
693 if (argument
== 0 || arg_num
== format_num
)
695 argument
= TREE_CHAIN (argument
);
698 || TREE_CODE (TREE_VALUE (argument
)) != POINTER_TYPE
699 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument
)))
702 error ("format string arg not a string type");
705 if (first_arg_num
!= 0)
707 /* Verify that first_arg_num points to the last arg,
710 arg_num
++, argument
= TREE_CHAIN (argument
);
711 if (arg_num
!= first_arg_num
)
713 error ("args to be formatted is not ...");
719 record_function_format (DECL_NAME (decl
),
720 DECL_ASSEMBLER_NAME (decl
),
721 is_scan
, format_num
, first_arg_num
);
727 tree format_num_expr
= TREE_VALUE (args
);
728 int format_num
, arg_num
;
731 if (TREE_CODE (decl
) != FUNCTION_DECL
)
733 error_with_decl (decl
,
734 "argument format specified for non-function `%s'");
738 /* Strip any conversions from the first arg number and verify it
740 while (TREE_CODE (format_num_expr
) == NOP_EXPR
741 || TREE_CODE (format_num_expr
) == CONVERT_EXPR
742 || TREE_CODE (format_num_expr
) == NON_LVALUE_EXPR
)
743 format_num_expr
= TREE_OPERAND (format_num_expr
, 0);
745 if (TREE_CODE (format_num_expr
) != INTEGER_CST
)
747 error ("format string has non-constant operand number");
751 format_num
= TREE_INT_CST_LOW (format_num_expr
);
753 /* If a parameter list is specified, verify that the format_num
754 argument is actually a string, in case the format attribute
756 argument
= TYPE_ARG_TYPES (type
);
759 for (arg_num
= 1; ; ++arg_num
)
761 if (argument
== 0 || arg_num
== format_num
)
763 argument
= TREE_CHAIN (argument
);
766 || TREE_CODE (TREE_VALUE (argument
)) != POINTER_TYPE
767 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument
)))
770 error ("format string arg not a string type");
775 if (TREE_CODE (TREE_TYPE (TREE_TYPE (decl
))) != POINTER_TYPE
776 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_TYPE (decl
))))
779 error ("function does not return string type");
783 record_international_format (DECL_NAME (decl
),
784 DECL_ASSEMBLER_NAME (decl
),
794 if ((TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_INITIAL (decl
))
795 || (TREE_CODE (decl
) != FUNCTION_DECL
&& ! DECL_EXTERNAL (decl
)))
796 error_with_decl (decl
,
797 "`%s' defined both normally and as an alias");
798 else if (decl_function_context (decl
) == 0)
800 tree id
= get_identifier (TREE_STRING_POINTER
801 (TREE_VALUE (args
)));
802 if (TREE_CODE (decl
) == FUNCTION_DECL
)
803 DECL_INITIAL (decl
) = error_mark_node
;
805 DECL_EXTERNAL (decl
) = 0;
806 assemble_alias (decl
, id
);
809 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
815 /* Split SPECS_ATTRS, a list of declspecs and prefix attributes, into two
816 lists. SPECS_ATTRS may also be just a typespec (eg: RECORD_TYPE).
818 The head of the declspec list is stored in DECLSPECS.
819 The head of the attribute list is stored in PREFIX_ATTRIBUTES.
821 Note that attributes in SPECS_ATTRS are stored in the TREE_PURPOSE of
822 the list elements. We drop the containing TREE_LIST nodes and link the
823 resulting attributes together the way decl_attributes expects them. */
826 split_specs_attrs (specs_attrs
, declspecs
, prefix_attributes
)
828 tree
*declspecs
, *prefix_attributes
;
830 tree t
, s
, a
, next
, specs
, attrs
;
832 /* This can happen in c++ (eg: decl: typespec initdecls ';'). */
833 if (specs_attrs
!= NULL_TREE
834 && TREE_CODE (specs_attrs
) != TREE_LIST
)
836 *declspecs
= specs_attrs
;
837 *prefix_attributes
= NULL_TREE
;
841 /* Remember to keep the lists in the same order, element-wise. */
843 specs
= s
= NULL_TREE
;
844 attrs
= a
= NULL_TREE
;
845 for (t
= specs_attrs
; t
; t
= next
)
847 next
= TREE_CHAIN (t
);
848 /* Declspecs have a non-NULL TREE_VALUE. */
849 if (TREE_VALUE (t
) != NULL_TREE
)
851 if (specs
== NULL_TREE
)
861 if (attrs
== NULL_TREE
)
862 attrs
= a
= TREE_PURPOSE (t
);
865 TREE_CHAIN (a
) = TREE_PURPOSE (t
);
866 a
= TREE_PURPOSE (t
);
868 /* More attrs can be linked here, move A to the end. */
869 while (TREE_CHAIN (a
) != NULL_TREE
)
874 /* Terminate the lists. */
876 TREE_CHAIN (s
) = NULL_TREE
;
878 TREE_CHAIN (a
) = NULL_TREE
;
882 *prefix_attributes
= attrs
;
885 /* Check a printf/fprintf/sprintf/scanf/fscanf/sscanf format against
888 #define T_I &integer_type_node
889 #define T_L &long_integer_type_node
890 #define T_LL &long_long_integer_type_node
891 #define T_S &short_integer_type_node
892 #define T_UI &unsigned_type_node
893 #define T_UL &long_unsigned_type_node
894 #define T_ULL &long_long_unsigned_type_node
895 #define T_US &short_unsigned_type_node
896 #define T_F &float_type_node
897 #define T_D &double_type_node
898 #define T_LD &long_double_type_node
899 #define T_C &char_type_node
900 #define T_V &void_type_node
901 #define T_W &wchar_type_node
902 #define T_ST &sizetype
907 /* Type of argument if no length modifier is used. */
909 /* Type of argument if length modifier for shortening is used.
910 If NULL, then this modifier is not allowed. */
912 /* Type of argument if length modifier `l' is used.
913 If NULL, then this modifier is not allowed. */
915 /* Type of argument if length modifier `q' or `ll' is used.
916 If NULL, then this modifier is not allowed. */
918 /* Type of argument if length modifier `L' is used.
919 If NULL, then this modifier is not allowed. */
921 /* List of other modifier characters allowed with these options. */
925 static format_char_info print_char_table
[] = {
926 { "di", 0, T_I
, T_I
, T_L
, T_LL
, T_LL
, "-wp0 +" },
927 { "oxX", 0, T_UI
, T_UI
, T_UL
, T_ULL
, T_ULL
, "-wp0#" },
928 { "u", 0, T_UI
, T_UI
, T_UL
, T_ULL
, T_ULL
, "-wp0" },
929 /* Two GNU extensions. */
930 { "Z", 0, T_ST
, NULL
, NULL
, NULL
, NULL
, "-wp0" },
931 { "m", 0, T_V
, NULL
, NULL
, NULL
, NULL
, "-wp" },
932 { "feEgGaA", 0, T_D
, NULL
, NULL
, NULL
, T_LD
, "-wp0 +#" },
933 { "c", 0, T_I
, NULL
, T_W
, NULL
, NULL
, "-w" },
934 { "C", 0, T_W
, NULL
, NULL
, NULL
, NULL
, "-w" },
935 { "s", 1, T_C
, NULL
, T_W
, NULL
, NULL
, "-wp" },
936 { "S", 1, T_W
, NULL
, NULL
, NULL
, NULL
, "-wp" },
937 { "p", 1, T_V
, NULL
, NULL
, NULL
, NULL
, "-w" },
938 { "n", 1, T_I
, T_S
, T_L
, T_LL
, NULL
, "" },
942 static format_char_info scan_char_table
[] = {
943 { "di", 1, T_I
, T_S
, T_L
, T_LL
, T_LL
, "*" },
944 { "ouxX", 1, T_UI
, T_US
, T_UL
, T_ULL
, T_ULL
, "*" },
945 { "efgEGaA", 1, T_F
, NULL
, T_D
, NULL
, T_LD
, "*" },
946 { "sc", 1, T_C
, NULL
, T_W
, NULL
, NULL
, "*a" },
947 { "[", 1, T_C
, NULL
, NULL
, NULL
, NULL
, "*a" },
948 { "C", 1, T_W
, NULL
, NULL
, NULL
, NULL
, "*" },
949 { "S", 1, T_W
, NULL
, NULL
, NULL
, NULL
, "*" },
950 { "p", 2, T_V
, NULL
, NULL
, NULL
, NULL
, "*" },
951 { "n", 1, T_I
, T_S
, T_L
, T_LL
, NULL
, "" },
955 typedef struct function_format_info
957 struct function_format_info
*next
; /* next structure on the list */
958 tree name
; /* identifier such as "printf" */
959 tree assembler_name
; /* optional mangled identifier (for C++) */
960 int is_scan
; /* TRUE if *scanf */
961 int format_num
; /* number of format argument */
962 int first_arg_num
; /* number of first arg (zero for varargs) */
963 } function_format_info
;
965 static function_format_info
*function_format_list
= NULL
;
967 typedef struct international_format_info
969 struct international_format_info
*next
; /* next structure on the list */
970 tree name
; /* identifier such as "gettext" */
971 tree assembler_name
; /* optional mangled identifier (for C++) */
972 int format_num
; /* number of format argument */
973 } international_format_info
;
975 static international_format_info
*international_format_list
= NULL
;
977 static void check_format_info
PROTO((function_format_info
*, tree
));
979 /* Initialize the table of functions to perform format checking on.
980 The ANSI functions are always checked (whether <stdio.h> is
981 included or not), since it is common to call printf without
982 including <stdio.h>. There shouldn't be a problem with this,
983 since ANSI reserves these function names whether you include the
984 header file or not. In any case, the checking is harmless.
986 Also initialize the name of function that modify the format string for
987 internationalization purposes. */
990 init_function_format_info ()
992 record_function_format (get_identifier ("printf"), NULL_TREE
, 0, 1, 2);
993 record_function_format (get_identifier ("fprintf"), NULL_TREE
, 0, 2, 3);
994 record_function_format (get_identifier ("sprintf"), NULL_TREE
, 0, 2, 3);
995 record_function_format (get_identifier ("scanf"), NULL_TREE
, 1, 1, 2);
996 record_function_format (get_identifier ("fscanf"), NULL_TREE
, 1, 2, 3);
997 record_function_format (get_identifier ("sscanf"), NULL_TREE
, 1, 2, 3);
998 record_function_format (get_identifier ("vprintf"), NULL_TREE
, 0, 1, 0);
999 record_function_format (get_identifier ("vfprintf"), NULL_TREE
, 0, 2, 0);
1000 record_function_format (get_identifier ("vsprintf"), NULL_TREE
, 0, 2, 0);
1002 record_international_format (get_identifier ("gettext"), NULL_TREE
, 1);
1003 record_international_format (get_identifier ("dgettext"), NULL_TREE
, 2);
1004 record_international_format (get_identifier ("dcgettext"), NULL_TREE
, 2);
1007 /* Record information for argument format checking. FUNCTION_IDENT is
1008 the identifier node for the name of the function to check (its decl
1009 need not exist yet). IS_SCAN is true for scanf-type format checking;
1010 false indicates printf-style format checking. FORMAT_NUM is the number
1011 of the argument which is the format control string (starting from 1).
1012 FIRST_ARG_NUM is the number of the first actual argument to check
1013 against the format string, or zero if no checking is not be done
1014 (e.g. for varargs such as vfprintf). */
1017 record_function_format (name
, assembler_name
, is_scan
,
1018 format_num
, first_arg_num
)
1020 tree assembler_name
;
1025 function_format_info
*info
;
1027 /* Re-use existing structure if it's there. */
1029 for (info
= function_format_list
; info
; info
= info
->next
)
1031 if (info
->name
== name
&& info
->assembler_name
== assembler_name
)
1036 info
= (function_format_info
*) xmalloc (sizeof (function_format_info
));
1037 info
->next
= function_format_list
;
1038 function_format_list
= info
;
1041 info
->assembler_name
= assembler_name
;
1044 info
->is_scan
= is_scan
;
1045 info
->format_num
= format_num
;
1046 info
->first_arg_num
= first_arg_num
;
1049 /* Record information for the names of function that modify the format
1050 argument to format functions. FUNCTION_IDENT is the identifier node for
1051 the name of the function (its decl need not exist yet) and FORMAT_NUM is
1052 the number of the argument which is the format control string (starting
1056 record_international_format (name
, assembler_name
, format_num
)
1058 tree assembler_name
;
1061 international_format_info
*info
;
1063 /* Re-use existing structure if it's there. */
1065 for (info
= international_format_list
; info
; info
= info
->next
)
1067 if (info
->name
== name
&& info
->assembler_name
== assembler_name
)
1074 = (international_format_info
*)
1075 xmalloc (sizeof (international_format_info
));
1076 info
->next
= international_format_list
;
1077 international_format_list
= info
;
1080 info
->assembler_name
= assembler_name
;
1083 info
->format_num
= format_num
;
1086 static char tfaff
[] = "too few arguments for format";
1088 /* Check the argument list of a call to printf, scanf, etc.
1089 NAME is the function identifier.
1090 ASSEMBLER_NAME is the function's assembler identifier.
1091 (Either NAME or ASSEMBLER_NAME, but not both, may be NULL_TREE.)
1092 PARAMS is the list of argument values. */
1095 check_function_format (name
, assembler_name
, params
)
1097 tree assembler_name
;
1100 function_format_info
*info
;
1102 /* See if this function is a format function. */
1103 for (info
= function_format_list
; info
; info
= info
->next
)
1105 if (info
->assembler_name
1106 ? (info
->assembler_name
== assembler_name
)
1107 : (info
->name
== name
))
1109 /* Yup; check it. */
1110 check_format_info (info
, params
);
1116 /* Check the argument list of a call to printf, scanf, etc.
1117 INFO points to the function_format_info structure.
1118 PARAMS is the list of argument values. */
1121 check_format_info (info
, params
)
1122 function_format_info
*info
;
1127 int suppressed
, wide
, precise
;
1135 tree first_fillin_param
;
1137 format_char_info
*fci
;
1138 static char message
[132];
1140 int has_operand_number
= 0;
1142 /* Skip to format argument. If the argument isn't available, there's
1143 no work for us to do; prototype checking will catch the problem. */
1144 for (arg_num
= 1; ; ++arg_num
)
1148 if (arg_num
== info
->format_num
)
1150 params
= TREE_CHAIN (params
);
1152 format_tree
= TREE_VALUE (params
);
1153 params
= TREE_CHAIN (params
);
1154 if (format_tree
== 0)
1157 /* We can only check the format if it's a string constant. */
1158 while (TREE_CODE (format_tree
) == NOP_EXPR
)
1159 format_tree
= TREE_OPERAND (format_tree
, 0); /* strip coercion */
1161 if (TREE_CODE (format_tree
) == CALL_EXPR
1162 && TREE_CODE (TREE_OPERAND (format_tree
, 0)) == ADDR_EXPR
1163 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (format_tree
, 0), 0))
1166 tree function
= TREE_OPERAND (TREE_OPERAND (format_tree
, 0), 0);
1168 /* See if this is a call to a known internationalization function
1169 that modifies the format arg. */
1170 international_format_info
*info
;
1172 for (info
= international_format_list
; info
; info
= info
->next
)
1173 if (info
->assembler_name
1174 ? (info
->assembler_name
== DECL_ASSEMBLER_NAME (function
))
1175 : (info
->name
== DECL_NAME (function
)))
1180 for (inner_args
= TREE_OPERAND (format_tree
, 1), i
= 1;
1182 inner_args
= TREE_CHAIN (inner_args
), i
++)
1183 if (i
== info
->format_num
)
1185 format_tree
= TREE_VALUE (inner_args
);
1187 while (TREE_CODE (format_tree
) == NOP_EXPR
)
1188 format_tree
= TREE_OPERAND (format_tree
, 0);
1193 if (integer_zerop (format_tree
))
1195 warning ("null format string");
1198 if (TREE_CODE (format_tree
) != ADDR_EXPR
)
1200 format_tree
= TREE_OPERAND (format_tree
, 0);
1201 if (TREE_CODE (format_tree
) != STRING_CST
)
1203 format_chars
= TREE_STRING_POINTER (format_tree
);
1204 format_length
= TREE_STRING_LENGTH (format_tree
);
1205 if (format_length
<= 1)
1206 warning ("zero-length format string");
1207 if (format_chars
[--format_length
] != 0)
1209 warning ("unterminated format string");
1212 /* Skip to first argument to check. */
1213 while (arg_num
+ 1 < info
->first_arg_num
)
1217 params
= TREE_CHAIN (params
);
1221 first_fillin_param
= params
;
1225 if (*format_chars
== 0)
1227 if (format_chars
- TREE_STRING_POINTER (format_tree
) != format_length
)
1228 warning ("embedded `\\0' in format");
1229 if (info
->first_arg_num
!= 0 && params
!= 0 && ! has_operand_number
)
1230 warning ("too many arguments for format");
1233 if (*format_chars
++ != '%')
1235 if (*format_chars
== 0)
1237 warning ("spurious trailing `%%' in format");
1240 if (*format_chars
== '%')
1246 suppressed
= wide
= precise
= FALSE
;
1249 suppressed
= *format_chars
== '*';
1252 while (isdigit (*format_chars
))
1257 /* See if we have a number followed by a dollar sign. If we do,
1258 it is an operand number, so set PARAMS to that operand. */
1259 if (*format_chars
>= '0' && *format_chars
<= '9')
1261 char *p
= format_chars
;
1263 while (*p
>= '0' && *p
++ <= '9')
1268 int opnum
= atoi (format_chars
);
1270 params
= first_fillin_param
;
1271 format_chars
= p
+ 1;
1272 has_operand_number
= 1;
1274 for (i
= 1; i
< opnum
&& params
!= 0; i
++)
1275 params
= TREE_CHAIN (params
);
1277 if (opnum
== 0 || params
== 0)
1279 warning ("operand number out of range in format");
1285 while (*format_chars
!= 0 && index (" +#0-", *format_chars
) != 0)
1287 if (index (flag_chars
, *format_chars
) != 0)
1289 sprintf (message
, "repeated `%c' flag in format",
1295 i
= strlen (flag_chars
);
1296 flag_chars
[i
++] = *format_chars
++;
1300 /* "If the space and + flags both appear,
1301 the space flag will be ignored." */
1302 if (index (flag_chars
, ' ') != 0
1303 && index (flag_chars
, '+') != 0)
1304 warning ("use of both ` ' and `+' flags in format");
1305 /* "If the 0 and - flags both appear,
1306 the 0 flag will be ignored." */
1307 if (index (flag_chars
, '0') != 0
1308 && index (flag_chars
, '-') != 0)
1309 warning ("use of both `0' and `-' flags in format");
1310 if (*format_chars
== '*')
1313 /* "...a field width...may be indicated by an asterisk.
1314 In this case, an int argument supplies the field width..." */
1321 if (info
->first_arg_num
!= 0)
1323 cur_param
= TREE_VALUE (params
);
1324 params
= TREE_CHAIN (params
);
1326 /* size_t is generally not valid here.
1327 It will work on most machines, because size_t and int
1328 have the same mode. But might as well warn anyway,
1329 since it will fail on other machines. */
1330 if ((TYPE_MAIN_VARIANT (TREE_TYPE (cur_param
))
1331 != integer_type_node
)
1333 (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param
))
1334 != unsigned_type_node
))
1337 "field width is not type int (arg %d)",
1345 while (isdigit (*format_chars
))
1351 if (*format_chars
== '.')
1355 if (*format_chars
!= '*' && !isdigit (*format_chars
))
1356 warning ("`.' not followed by `*' or digit in format");
1357 /* "...a...precision...may be indicated by an asterisk.
1358 In this case, an int argument supplies the...precision." */
1359 if (*format_chars
== '*')
1361 if (info
->first_arg_num
!= 0)
1369 cur_param
= TREE_VALUE (params
);
1370 params
= TREE_CHAIN (params
);
1372 if (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param
))
1373 != integer_type_node
)
1376 "field width is not type int (arg %d)",
1384 while (isdigit (*format_chars
))
1389 if (*format_chars
== 'h' || *format_chars
== 'l')
1390 length_char
= *format_chars
++;
1391 else if (*format_chars
== 'q' || *format_chars
== 'L')
1393 length_char
= *format_chars
++;
1395 pedwarn ("ANSI C does not support the `%c' length modifier",
1400 if (length_char
== 'l' && *format_chars
== 'l')
1402 length_char
= 'q', format_chars
++;
1404 pedwarn ("ANSI C does not support the `ll' length modifier");
1407 if (*format_chars
== 'a' && info
->is_scan
)
1409 if (format_chars
[1] == 's' || format_chars
[1] == 'S'
1410 || format_chars
[1] == '[')
1412 /* `a' is used as a flag. */
1417 if (suppressed
&& length_char
!= 0)
1420 "use of `*' and `%c' together in format",
1424 format_char
= *format_chars
;
1425 if (format_char
== 0 || format_char
== '%')
1427 warning ("conversion lacks type at end of format");
1431 fci
= info
->is_scan
? scan_char_table
: print_char_table
;
1432 while (fci
->format_chars
!= 0
1433 && index (fci
->format_chars
, format_char
) == 0)
1435 if (fci
->format_chars
== 0)
1437 if (format_char
>= 040 && format_char
< 0177)
1439 "unknown conversion type character `%c' in format",
1443 "unknown conversion type character 0x%x in format",
1448 if (wide
&& index (fci
->flag_chars
, 'w') == 0)
1450 sprintf (message
, "width used with `%c' format",
1454 if (precise
&& index (fci
->flag_chars
, 'p') == 0)
1456 sprintf (message
, "precision used with `%c' format",
1460 if (aflag
&& index (fci
->flag_chars
, 'a') == 0)
1462 sprintf (message
, "`a' flag used with `%c' format",
1465 /* To simplify the following code. */
1468 if (info
->is_scan
&& format_char
== '[')
1470 /* Skip over scan set, in case it happens to have '%' in it. */
1471 if (*format_chars
== '^')
1473 /* Find closing bracket; if one is hit immediately, then
1474 it's part of the scan set rather than a terminator. */
1475 if (*format_chars
== ']')
1477 while (*format_chars
&& *format_chars
!= ']')
1479 if (*format_chars
!= ']')
1480 /* The end of the format string was reached. */
1481 warning ("no closing `]' for `%%[' format");
1485 if (index (fci
->flag_chars
, '*') == 0)
1488 "suppression of `%c' conversion in format",
1494 for (i
= 0; flag_chars
[i
] != 0; ++i
)
1496 if (index (fci
->flag_chars
, flag_chars
[i
]) == 0)
1498 sprintf (message
, "flag `%c' used with type `%c'",
1499 flag_chars
[i
], format_char
);
1503 if (precise
&& index (flag_chars
, '0') != 0
1504 && (format_char
== 'd' || format_char
== 'i'
1505 || format_char
== 'o' || format_char
== 'u'
1506 || format_char
== 'x' || format_char
== 'x'))
1509 "`0' flag ignored with precision specifier and `%c' format",
1513 switch (length_char
)
1515 default: wanted_type
= fci
->nolen
? *(fci
->nolen
) : 0; break;
1516 case 'h': wanted_type
= fci
->hlen
? *(fci
->hlen
) : 0; break;
1517 case 'l': wanted_type
= fci
->llen
? *(fci
->llen
) : 0; break;
1518 case 'q': wanted_type
= fci
->qlen
? *(fci
->qlen
) : 0; break;
1519 case 'L': wanted_type
= fci
->bigllen
? *(fci
->bigllen
) : 0; break;
1521 if (wanted_type
== 0)
1524 "use of `%c' length character with `%c' type character",
1525 length_char
, format_char
);
1530 ** XXX -- should kvetch about stuff such as
1534 ** scanf ("%d", &i);
1538 /* Finally. . .check type of argument against desired type! */
1539 if (info
->first_arg_num
== 0)
1541 if (fci
->pointer_count
== 0 && wanted_type
== void_type_node
)
1542 /* This specifier takes no argument. */
1549 cur_param
= TREE_VALUE (params
);
1550 params
= TREE_CHAIN (params
);
1552 cur_type
= TREE_TYPE (cur_param
);
1554 STRIP_NOPS (cur_param
);
1556 /* Check the types of any additional pointer arguments
1557 that precede the "real" argument. */
1558 for (i
= 0; i
< fci
->pointer_count
+ aflag
; ++i
)
1560 if (TREE_CODE (cur_type
) == POINTER_TYPE
)
1562 cur_type
= TREE_TYPE (cur_type
);
1564 if (TREE_CODE (cur_param
) == ADDR_EXPR
)
1565 cur_param
= TREE_OPERAND (cur_param
, 0);
1571 if (TREE_CODE (cur_type
) != ERROR_MARK
)
1574 "format argument is not a %s (arg %d)",
1575 ((fci
->pointer_count
+ aflag
== 1)
1576 ? "pointer" : "pointer to a pointer"),
1583 /* See if this is an attempt to write into a const type with
1585 if (info
->is_scan
&& i
== fci
->pointer_count
+ aflag
1587 && TREE_CODE (cur_type
) != ERROR_MARK
1588 && (TYPE_READONLY (cur_type
)
1590 && (TREE_CODE_CLASS (TREE_CODE (cur_param
)) == 'c'
1591 || (TREE_CODE_CLASS (TREE_CODE (cur_param
)) == 'd'
1592 && TREE_READONLY (cur_param
))))))
1594 sprintf (message
, "writing into constant object (arg %d)", arg_num
);
1598 /* Check the type of the "real" argument, if there's a type we want. */
1599 if (i
== fci
->pointer_count
+ aflag
&& wanted_type
!= 0
1600 && TREE_CODE (cur_type
) != ERROR_MARK
1601 && wanted_type
!= TYPE_MAIN_VARIANT (cur_type
)
1602 /* If we want `void *', allow any pointer type.
1603 (Anything else would already have got a warning.) */
1604 && ! (wanted_type
== void_type_node
1605 && fci
->pointer_count
> 0)
1606 /* Don't warn about differences merely in signedness. */
1607 && !(TREE_CODE (wanted_type
) == INTEGER_TYPE
1608 && TREE_CODE (TYPE_MAIN_VARIANT (cur_type
)) == INTEGER_TYPE
1609 && (TREE_UNSIGNED (wanted_type
)
1610 ? wanted_type
== (cur_type
= unsigned_type (cur_type
))
1611 : wanted_type
== (cur_type
= signed_type (cur_type
))))
1612 /* Likewise, "signed char", "unsigned char" and "char" are
1613 equivalent but the above test won't consider them equivalent. */
1614 && ! (wanted_type
== char_type_node
1615 && (TYPE_MAIN_VARIANT (cur_type
) == signed_char_type_node
1616 || TYPE_MAIN_VARIANT (cur_type
) == unsigned_char_type_node
)))
1618 register char *this;
1619 register char *that
;
1621 this = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (wanted_type
)));
1623 if (TREE_CODE (cur_type
) != ERROR_MARK
1624 && TYPE_NAME (cur_type
) != 0
1625 && TREE_CODE (cur_type
) != INTEGER_TYPE
1626 && !(TREE_CODE (cur_type
) == POINTER_TYPE
1627 && TREE_CODE (TREE_TYPE (cur_type
)) == INTEGER_TYPE
))
1629 if (TREE_CODE (TYPE_NAME (cur_type
)) == TYPE_DECL
1630 && DECL_NAME (TYPE_NAME (cur_type
)) != 0)
1631 that
= IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type
)));
1633 that
= IDENTIFIER_POINTER (TYPE_NAME (cur_type
));
1636 /* A nameless type can't possibly match what the format wants.
1637 So there will be a warning for it.
1638 Make up a string to describe vaguely what it is. */
1641 if (TREE_CODE (cur_type
) == POINTER_TYPE
)
1644 that
= "different type";
1647 /* Make the warning better in case of mismatch of int vs long. */
1648 if (TREE_CODE (cur_type
) == INTEGER_TYPE
1649 && TREE_CODE (wanted_type
) == INTEGER_TYPE
1650 && TYPE_PRECISION (cur_type
) == TYPE_PRECISION (wanted_type
)
1651 && TYPE_NAME (cur_type
) != 0
1652 && TREE_CODE (TYPE_NAME (cur_type
)) == TYPE_DECL
)
1653 that
= IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type
)));
1655 if (strcmp (this, that
) != 0)
1657 sprintf (message
, "%s format, %s arg (arg %d)",
1658 this, that
, arg_num
);
1665 /* Print a warning if a constant expression had overflow in folding.
1666 Invoke this function on every expression that the language
1667 requires to be a constant expression.
1668 Note the ANSI C standard says it is erroneous for a
1669 constant expression to overflow. */
1672 constant_expression_warning (value
)
1675 if ((TREE_CODE (value
) == INTEGER_CST
|| TREE_CODE (value
) == REAL_CST
1676 || TREE_CODE (value
) == COMPLEX_CST
)
1677 && TREE_CONSTANT_OVERFLOW (value
) && pedantic
)
1678 pedwarn ("overflow in constant expression");
1681 /* Print a warning if an expression had overflow in folding.
1682 Invoke this function on every expression that
1683 (1) appears in the source code, and
1684 (2) might be a constant expression that overflowed, and
1685 (3) is not already checked by convert_and_check;
1686 however, do not invoke this function on operands of explicit casts. */
1689 overflow_warning (value
)
1692 if ((TREE_CODE (value
) == INTEGER_CST
1693 || (TREE_CODE (value
) == COMPLEX_CST
1694 && TREE_CODE (TREE_REALPART (value
)) == INTEGER_CST
))
1695 && TREE_OVERFLOW (value
))
1697 TREE_OVERFLOW (value
) = 0;
1698 if (skip_evaluation
== 0)
1699 warning ("integer overflow in expression");
1701 else if ((TREE_CODE (value
) == REAL_CST
1702 || (TREE_CODE (value
) == COMPLEX_CST
1703 && TREE_CODE (TREE_REALPART (value
)) == REAL_CST
))
1704 && TREE_OVERFLOW (value
))
1706 TREE_OVERFLOW (value
) = 0;
1707 if (skip_evaluation
== 0)
1708 warning ("floating point overflow in expression");
1712 /* Print a warning if a large constant is truncated to unsigned,
1713 or if -Wconversion is used and a constant < 0 is converted to unsigned.
1714 Invoke this function on every expression that might be implicitly
1715 converted to an unsigned type. */
1718 unsigned_conversion_warning (result
, operand
)
1719 tree result
, operand
;
1721 if (TREE_CODE (operand
) == INTEGER_CST
1722 && TREE_CODE (TREE_TYPE (result
)) == INTEGER_TYPE
1723 && TREE_UNSIGNED (TREE_TYPE (result
))
1724 && skip_evaluation
== 0
1725 && !int_fits_type_p (operand
, TREE_TYPE (result
)))
1727 if (!int_fits_type_p (operand
, signed_type (TREE_TYPE (result
))))
1728 /* This detects cases like converting -129 or 256 to unsigned char. */
1729 warning ("large integer implicitly truncated to unsigned type");
1730 else if (warn_conversion
)
1731 warning ("negative integer implicitly converted to unsigned type");
1735 /* Convert EXPR to TYPE, warning about conversion problems with constants.
1736 Invoke this function on every expression that is converted implicitly,
1737 i.e. because of language rules and not because of an explicit cast. */
1740 convert_and_check (type
, expr
)
1743 tree t
= convert (type
, expr
);
1744 if (TREE_CODE (t
) == INTEGER_CST
)
1746 if (TREE_OVERFLOW (t
))
1748 TREE_OVERFLOW (t
) = 0;
1750 /* Do not diagnose overflow in a constant expression merely
1751 because a conversion overflowed. */
1752 TREE_CONSTANT_OVERFLOW (t
) = TREE_CONSTANT_OVERFLOW (expr
);
1754 /* No warning for converting 0x80000000 to int. */
1755 if (!(TREE_UNSIGNED (type
) < TREE_UNSIGNED (TREE_TYPE (expr
))
1756 && TREE_CODE (TREE_TYPE (expr
)) == INTEGER_TYPE
1757 && TYPE_PRECISION (type
) == TYPE_PRECISION (TREE_TYPE (expr
))))
1758 /* If EXPR fits in the unsigned version of TYPE,
1759 don't warn unless pedantic. */
1761 || TREE_UNSIGNED (type
)
1762 || ! int_fits_type_p (expr
, unsigned_type (type
)))
1763 && skip_evaluation
== 0)
1764 warning ("overflow in implicit constant conversion");
1767 unsigned_conversion_warning (t
, expr
);
1773 c_expand_expr_stmt (expr
)
1776 /* Do default conversion if safe and possibly important,
1777 in case within ({...}). */
1778 if ((TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
&& lvalue_p (expr
))
1779 || TREE_CODE (TREE_TYPE (expr
)) == FUNCTION_TYPE
)
1780 expr
= default_conversion (expr
);
1782 if (TREE_TYPE (expr
) != error_mark_node
1783 && TYPE_SIZE (TREE_TYPE (expr
)) == 0
1784 && TREE_CODE (TREE_TYPE (expr
)) != ARRAY_TYPE
)
1785 error ("expression statement has incomplete type");
1787 expand_expr_stmt (expr
);
1790 /* Validate the expression after `case' and apply default promotions. */
1793 check_case_value (value
)
1796 if (value
== NULL_TREE
)
1799 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1800 STRIP_TYPE_NOPS (value
);
1802 if (TREE_CODE (value
) != INTEGER_CST
1803 && value
!= error_mark_node
)
1805 error ("case label does not reduce to an integer constant");
1806 value
= error_mark_node
;
1809 /* Promote char or short to int. */
1810 value
= default_conversion (value
);
1812 constant_expression_warning (value
);
1817 /* Return an integer type with BITS bits of precision,
1818 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
1821 type_for_size (bits
, unsignedp
)
1825 if (bits
== TYPE_PRECISION (integer_type_node
))
1826 return unsignedp
? unsigned_type_node
: integer_type_node
;
1828 if (bits
== TYPE_PRECISION (signed_char_type_node
))
1829 return unsignedp
? unsigned_char_type_node
: signed_char_type_node
;
1831 if (bits
== TYPE_PRECISION (short_integer_type_node
))
1832 return unsignedp
? short_unsigned_type_node
: short_integer_type_node
;
1834 if (bits
== TYPE_PRECISION (long_integer_type_node
))
1835 return unsignedp
? long_unsigned_type_node
: long_integer_type_node
;
1837 if (bits
== TYPE_PRECISION (long_long_integer_type_node
))
1838 return (unsignedp
? long_long_unsigned_type_node
1839 : long_long_integer_type_node
);
1841 if (bits
<= TYPE_PRECISION (intQI_type_node
))
1842 return unsignedp
? unsigned_intQI_type_node
: intQI_type_node
;
1844 if (bits
<= TYPE_PRECISION (intHI_type_node
))
1845 return unsignedp
? unsigned_intHI_type_node
: intHI_type_node
;
1847 if (bits
<= TYPE_PRECISION (intSI_type_node
))
1848 return unsignedp
? unsigned_intSI_type_node
: intSI_type_node
;
1850 if (bits
<= TYPE_PRECISION (intDI_type_node
))
1851 return unsignedp
? unsigned_intDI_type_node
: intDI_type_node
;
1856 /* Return a data type that has machine mode MODE.
1857 If the mode is an integer,
1858 then UNSIGNEDP selects between signed and unsigned types. */
1861 type_for_mode (mode
, unsignedp
)
1862 enum machine_mode mode
;
1865 if (mode
== TYPE_MODE (integer_type_node
))
1866 return unsignedp
? unsigned_type_node
: integer_type_node
;
1868 if (mode
== TYPE_MODE (signed_char_type_node
))
1869 return unsignedp
? unsigned_char_type_node
: signed_char_type_node
;
1871 if (mode
== TYPE_MODE (short_integer_type_node
))
1872 return unsignedp
? short_unsigned_type_node
: short_integer_type_node
;
1874 if (mode
== TYPE_MODE (long_integer_type_node
))
1875 return unsignedp
? long_unsigned_type_node
: long_integer_type_node
;
1877 if (mode
== TYPE_MODE (long_long_integer_type_node
))
1878 return unsignedp
? long_long_unsigned_type_node
: long_long_integer_type_node
;
1880 if (mode
== TYPE_MODE (intQI_type_node
))
1881 return unsignedp
? unsigned_intQI_type_node
: intQI_type_node
;
1883 if (mode
== TYPE_MODE (intHI_type_node
))
1884 return unsignedp
? unsigned_intHI_type_node
: intHI_type_node
;
1886 if (mode
== TYPE_MODE (intSI_type_node
))
1887 return unsignedp
? unsigned_intSI_type_node
: intSI_type_node
;
1889 if (mode
== TYPE_MODE (intDI_type_node
))
1890 return unsignedp
? unsigned_intDI_type_node
: intDI_type_node
;
1892 if (mode
== TYPE_MODE (float_type_node
))
1893 return float_type_node
;
1895 if (mode
== TYPE_MODE (double_type_node
))
1896 return double_type_node
;
1898 if (mode
== TYPE_MODE (long_double_type_node
))
1899 return long_double_type_node
;
1901 if (mode
== TYPE_MODE (build_pointer_type (char_type_node
)))
1902 return build_pointer_type (char_type_node
);
1904 if (mode
== TYPE_MODE (build_pointer_type (integer_type_node
)))
1905 return build_pointer_type (integer_type_node
);
1910 /* Return the minimum number of bits needed to represent VALUE in a
1911 signed or unsigned type, UNSIGNEDP says which. */
1914 min_precision (value
, unsignedp
)
1920 /* If the value is negative, compute its negative minus 1. The latter
1921 adjustment is because the absolute value of the largest negative value
1922 is one larger than the largest positive value. This is equivalent to
1923 a bit-wise negation, so use that operation instead. */
1925 if (tree_int_cst_sgn (value
) < 0)
1926 value
= fold (build1 (BIT_NOT_EXPR
, TREE_TYPE (value
), value
));
1928 /* Return the number of bits needed, taking into account the fact
1929 that we need one more bit for a signed than unsigned type. */
1931 if (integer_zerop (value
))
1933 else if (TREE_INT_CST_HIGH (value
) != 0)
1934 log
= HOST_BITS_PER_WIDE_INT
+ floor_log2 (TREE_INT_CST_HIGH (value
));
1936 log
= floor_log2 (TREE_INT_CST_LOW (value
));
1938 return log
+ 1 + ! unsignedp
;
1941 /* Print an error message for invalid operands to arith operation CODE.
1942 NOP_EXPR is used as a special case (see truthvalue_conversion). */
1945 binary_op_error (code
)
1946 enum tree_code code
;
1948 register char *opname
;
1953 error ("invalid truth-value expression");
1957 opname
= "+"; break;
1959 opname
= "-"; break;
1961 opname
= "*"; break;
1963 opname
= "max"; break;
1965 opname
= "min"; break;
1967 opname
= "=="; break;
1969 opname
= "!="; break;
1971 opname
= "<="; break;
1973 opname
= ">="; break;
1975 opname
= "<"; break;
1977 opname
= ">"; break;
1979 opname
= "<<"; break;
1981 opname
= ">>"; break;
1982 case TRUNC_MOD_EXPR
:
1983 case FLOOR_MOD_EXPR
:
1984 opname
= "%"; break;
1985 case TRUNC_DIV_EXPR
:
1986 case FLOOR_DIV_EXPR
:
1987 opname
= "/"; break;
1989 opname
= "&"; break;
1991 opname
= "|"; break;
1992 case TRUTH_ANDIF_EXPR
:
1993 opname
= "&&"; break;
1994 case TRUTH_ORIF_EXPR
:
1995 opname
= "||"; break;
1997 opname
= "^"; break;
2000 opname
= "rotate"; break;
2002 opname
= "unknown"; break;
2004 error ("invalid operands to binary %s", opname
);
2007 /* Subroutine of build_binary_op, used for comparison operations.
2008 See if the operands have both been converted from subword integer types
2009 and, if so, perhaps change them both back to their original type.
2010 This function is also responsible for converting the two operands
2011 to the proper common type for comparison.
2013 The arguments of this function are all pointers to local variables
2014 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
2015 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
2017 If this function returns nonzero, it means that the comparison has
2018 a constant value. What this function returns is an expression for
2022 shorten_compare (op0_ptr
, op1_ptr
, restype_ptr
, rescode_ptr
)
2023 tree
*op0_ptr
, *op1_ptr
;
2025 enum tree_code
*rescode_ptr
;
2028 tree op0
= *op0_ptr
;
2029 tree op1
= *op1_ptr
;
2030 int unsignedp0
, unsignedp1
;
2032 tree primop0
, primop1
;
2033 enum tree_code code
= *rescode_ptr
;
2035 /* Throw away any conversions to wider types
2036 already present in the operands. */
2038 primop0
= get_narrower (op0
, &unsignedp0
);
2039 primop1
= get_narrower (op1
, &unsignedp1
);
2041 /* Handle the case that OP0 does not *contain* a conversion
2042 but it *requires* conversion to FINAL_TYPE. */
2044 if (op0
== primop0
&& TREE_TYPE (op0
) != *restype_ptr
)
2045 unsignedp0
= TREE_UNSIGNED (TREE_TYPE (op0
));
2046 if (op1
== primop1
&& TREE_TYPE (op1
) != *restype_ptr
)
2047 unsignedp1
= TREE_UNSIGNED (TREE_TYPE (op1
));
2049 /* If one of the operands must be floated, we cannot optimize. */
2050 real1
= TREE_CODE (TREE_TYPE (primop0
)) == REAL_TYPE
;
2051 real2
= TREE_CODE (TREE_TYPE (primop1
)) == REAL_TYPE
;
2053 /* If first arg is constant, swap the args (changing operation
2054 so value is preserved), for canonicalization. Don't do this if
2055 the second arg is 0. */
2057 if (TREE_CONSTANT (primop0
)
2058 && ! integer_zerop (primop1
) && ! real_zerop (primop1
))
2060 register tree tem
= primop0
;
2061 register int temi
= unsignedp0
;
2069 unsignedp0
= unsignedp1
;
2092 *rescode_ptr
= code
;
2095 /* If comparing an integer against a constant more bits wide,
2096 maybe we can deduce a value of 1 or 0 independent of the data.
2097 Or else truncate the constant now
2098 rather than extend the variable at run time.
2100 This is only interesting if the constant is the wider arg.
2101 Also, it is not safe if the constant is unsigned and the
2102 variable arg is signed, since in this case the variable
2103 would be sign-extended and then regarded as unsigned.
2104 Our technique fails in this case because the lowest/highest
2105 possible unsigned results don't follow naturally from the
2106 lowest/highest possible values of the variable operand.
2107 For just EQ_EXPR and NE_EXPR there is another technique that
2108 could be used: see if the constant can be faithfully represented
2109 in the other operand's type, by truncating it and reextending it
2110 and see if that preserves the constant's value. */
2112 if (!real1
&& !real2
2113 && TREE_CODE (primop1
) == INTEGER_CST
2114 && TYPE_PRECISION (TREE_TYPE (primop0
)) < TYPE_PRECISION (*restype_ptr
))
2116 int min_gt
, max_gt
, min_lt
, max_lt
;
2117 tree maxval
, minval
;
2118 /* 1 if comparison is nominally unsigned. */
2119 int unsignedp
= TREE_UNSIGNED (*restype_ptr
);
2122 type
= signed_or_unsigned_type (unsignedp0
, TREE_TYPE (primop0
));
2124 maxval
= TYPE_MAX_VALUE (type
);
2125 minval
= TYPE_MIN_VALUE (type
);
2127 if (unsignedp
&& !unsignedp0
)
2128 *restype_ptr
= signed_type (*restype_ptr
);
2130 if (TREE_TYPE (primop1
) != *restype_ptr
)
2131 primop1
= convert (*restype_ptr
, primop1
);
2132 if (type
!= *restype_ptr
)
2134 minval
= convert (*restype_ptr
, minval
);
2135 maxval
= convert (*restype_ptr
, maxval
);
2138 if (unsignedp
&& unsignedp0
)
2140 min_gt
= INT_CST_LT_UNSIGNED (primop1
, minval
);
2141 max_gt
= INT_CST_LT_UNSIGNED (primop1
, maxval
);
2142 min_lt
= INT_CST_LT_UNSIGNED (minval
, primop1
);
2143 max_lt
= INT_CST_LT_UNSIGNED (maxval
, primop1
);
2147 min_gt
= INT_CST_LT (primop1
, minval
);
2148 max_gt
= INT_CST_LT (primop1
, maxval
);
2149 min_lt
= INT_CST_LT (minval
, primop1
);
2150 max_lt
= INT_CST_LT (maxval
, primop1
);
2154 /* This used to be a switch, but Genix compiler can't handle that. */
2155 if (code
== NE_EXPR
)
2157 if (max_lt
|| min_gt
)
2158 val
= boolean_true_node
;
2160 else if (code
== EQ_EXPR
)
2162 if (max_lt
|| min_gt
)
2163 val
= boolean_false_node
;
2165 else if (code
== LT_EXPR
)
2168 val
= boolean_true_node
;
2170 val
= boolean_false_node
;
2172 else if (code
== GT_EXPR
)
2175 val
= boolean_true_node
;
2177 val
= boolean_false_node
;
2179 else if (code
== LE_EXPR
)
2182 val
= boolean_true_node
;
2184 val
= boolean_false_node
;
2186 else if (code
== GE_EXPR
)
2189 val
= boolean_true_node
;
2191 val
= boolean_false_node
;
2194 /* If primop0 was sign-extended and unsigned comparison specd,
2195 we did a signed comparison above using the signed type bounds.
2196 But the comparison we output must be unsigned.
2198 Also, for inequalities, VAL is no good; but if the signed
2199 comparison had *any* fixed result, it follows that the
2200 unsigned comparison just tests the sign in reverse
2201 (positive values are LE, negative ones GE).
2202 So we can generate an unsigned comparison
2203 against an extreme value of the signed type. */
2205 if (unsignedp
&& !unsignedp0
)
2212 primop1
= TYPE_MIN_VALUE (type
);
2218 primop1
= TYPE_MAX_VALUE (type
);
2225 type
= unsigned_type (type
);
2228 if (!max_gt
&& !unsignedp0
&& TREE_CODE (primop0
) != INTEGER_CST
)
2230 /* This is the case of (char)x >?< 0x80, which people used to use
2231 expecting old C compilers to change the 0x80 into -0x80. */
2232 if (val
== boolean_false_node
)
2233 warning ("comparison is always 0 due to limited range of data type");
2234 if (val
== boolean_true_node
)
2235 warning ("comparison is always 1 due to limited range of data type");
2238 if (!min_lt
&& unsignedp0
&& TREE_CODE (primop0
) != INTEGER_CST
)
2240 /* This is the case of (unsigned char)x >?< -1 or < 0. */
2241 if (val
== boolean_false_node
)
2242 warning ("comparison is always 0 due to limited range of data type");
2243 if (val
== boolean_true_node
)
2244 warning ("comparison is always 1 due to limited range of data type");
2249 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2250 if (TREE_SIDE_EFFECTS (primop0
))
2251 return build (COMPOUND_EXPR
, TREE_TYPE (val
), primop0
, val
);
2255 /* Value is not predetermined, but do the comparison
2256 in the type of the operand that is not constant.
2257 TYPE is already properly set. */
2259 else if (real1
&& real2
2260 && (TYPE_PRECISION (TREE_TYPE (primop0
))
2261 == TYPE_PRECISION (TREE_TYPE (primop1
))))
2262 type
= TREE_TYPE (primop0
);
2264 /* If args' natural types are both narrower than nominal type
2265 and both extend in the same manner, compare them
2266 in the type of the wider arg.
2267 Otherwise must actually extend both to the nominal
2268 common type lest different ways of extending
2270 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
2272 else if (unsignedp0
== unsignedp1
&& real1
== real2
2273 && TYPE_PRECISION (TREE_TYPE (primop0
)) < TYPE_PRECISION (*restype_ptr
)
2274 && TYPE_PRECISION (TREE_TYPE (primop1
)) < TYPE_PRECISION (*restype_ptr
))
2276 type
= common_type (TREE_TYPE (primop0
), TREE_TYPE (primop1
));
2277 type
= signed_or_unsigned_type (unsignedp0
2278 || TREE_UNSIGNED (*restype_ptr
),
2280 /* Make sure shorter operand is extended the right way
2281 to match the longer operand. */
2282 primop0
= convert (signed_or_unsigned_type (unsignedp0
, TREE_TYPE (primop0
)),
2284 primop1
= convert (signed_or_unsigned_type (unsignedp1
, TREE_TYPE (primop1
)),
2289 /* Here we must do the comparison on the nominal type
2290 using the args exactly as we received them. */
2291 type
= *restype_ptr
;
2295 if (!real1
&& !real2
&& integer_zerop (primop1
)
2296 && TREE_UNSIGNED (*restype_ptr
))
2302 /* All unsigned values are >= 0, so we warn if extra warnings
2303 are requested. However, if OP0 is a constant that is
2304 >= 0, the signedness of the comparison isn't an issue,
2305 so suppress the warning. */
2307 && ! (TREE_CODE (primop0
) == INTEGER_CST
2308 && ! TREE_OVERFLOW (convert (signed_type (type
),
2310 warning ("unsigned value >= 0 is always 1");
2311 value
= boolean_true_node
;
2316 && ! (TREE_CODE (primop0
) == INTEGER_CST
2317 && ! TREE_OVERFLOW (convert (signed_type (type
),
2319 warning ("unsigned value < 0 is always 0");
2320 value
= boolean_false_node
;
2329 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2330 if (TREE_SIDE_EFFECTS (primop0
))
2331 return build (COMPOUND_EXPR
, TREE_TYPE (value
),
2338 *op0_ptr
= convert (type
, primop0
);
2339 *op1_ptr
= convert (type
, primop1
);
2341 *restype_ptr
= boolean_type_node
;
2346 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
2347 or validate its data type for an `if' or `while' statement or ?..: exp.
2349 This preparation consists of taking the ordinary
2350 representation of an expression expr and producing a valid tree
2351 boolean expression describing whether expr is nonzero. We could
2352 simply always do build_binary_op (NE_EXPR, expr, boolean_false_node, 1),
2353 but we optimize comparisons, &&, ||, and !.
2355 The resulting type should always be `boolean_type_node'. */
2358 truthvalue_conversion (expr
)
2361 if (TREE_CODE (expr
) == ERROR_MARK
)
2364 #if 0 /* This appears to be wrong for C++. */
2365 /* These really should return error_mark_node after 2.4 is stable.
2366 But not all callers handle ERROR_MARK properly. */
2367 switch (TREE_CODE (TREE_TYPE (expr
)))
2370 error ("struct type value used where scalar is required");
2371 return boolean_false_node
;
2374 error ("union type value used where scalar is required");
2375 return boolean_false_node
;
2378 error ("array type value used where scalar is required");
2379 return boolean_false_node
;
2386 switch (TREE_CODE (expr
))
2388 /* It is simpler and generates better code to have only TRUTH_*_EXPR
2389 or comparison expressions as truth values at this level. */
2392 /* A one-bit unsigned bit-field is already acceptable. */
2393 if (1 == TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (expr
, 1)))
2394 && TREE_UNSIGNED (TREE_OPERAND (expr
, 1)))
2400 /* It is simpler and generates better code to have only TRUTH_*_EXPR
2401 or comparison expressions as truth values at this level. */
2403 if (integer_zerop (TREE_OPERAND (expr
, 1)))
2404 return build_unary_op (TRUTH_NOT_EXPR
, TREE_OPERAND (expr
, 0), 0);
2406 case NE_EXPR
: case LE_EXPR
: case GE_EXPR
: case LT_EXPR
: case GT_EXPR
:
2407 case TRUTH_ANDIF_EXPR
:
2408 case TRUTH_ORIF_EXPR
:
2409 case TRUTH_AND_EXPR
:
2411 case TRUTH_XOR_EXPR
:
2412 case TRUTH_NOT_EXPR
:
2413 TREE_TYPE (expr
) = boolean_type_node
;
2420 return integer_zerop (expr
) ? boolean_false_node
: boolean_true_node
;
2423 return real_zerop (expr
) ? boolean_false_node
: boolean_true_node
;
2426 /* If we are taking the address of a external decl, it might be zero
2427 if it is weak, so we cannot optimize. */
2428 if (TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (expr
, 0))) == 'd'
2429 && DECL_EXTERNAL (TREE_OPERAND (expr
, 0)))
2432 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr
, 0)))
2433 return build (COMPOUND_EXPR
, boolean_type_node
,
2434 TREE_OPERAND (expr
, 0), boolean_true_node
);
2436 return boolean_true_node
;
2439 return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr
, 1))
2440 ? TRUTH_OR_EXPR
: TRUTH_ORIF_EXPR
),
2441 truthvalue_conversion (TREE_OPERAND (expr
, 0)),
2442 truthvalue_conversion (TREE_OPERAND (expr
, 1)),
2449 /* These don't change whether an object is non-zero or zero. */
2450 return truthvalue_conversion (TREE_OPERAND (expr
, 0));
2454 /* These don't change whether an object is zero or non-zero, but
2455 we can't ignore them if their second arg has side-effects. */
2456 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr
, 1)))
2457 return build (COMPOUND_EXPR
, boolean_type_node
, TREE_OPERAND (expr
, 1),
2458 truthvalue_conversion (TREE_OPERAND (expr
, 0)));
2460 return truthvalue_conversion (TREE_OPERAND (expr
, 0));
2463 /* Distribute the conversion into the arms of a COND_EXPR. */
2464 return fold (build (COND_EXPR
, boolean_type_node
, TREE_OPERAND (expr
, 0),
2465 truthvalue_conversion (TREE_OPERAND (expr
, 1)),
2466 truthvalue_conversion (TREE_OPERAND (expr
, 2))));
2469 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
2470 since that affects how `default_conversion' will behave. */
2471 if (TREE_CODE (TREE_TYPE (expr
)) == REFERENCE_TYPE
2472 || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0))) == REFERENCE_TYPE
)
2474 /* fall through... */
2476 /* If this is widening the argument, we can ignore it. */
2477 if (TYPE_PRECISION (TREE_TYPE (expr
))
2478 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr
, 0))))
2479 return truthvalue_conversion (TREE_OPERAND (expr
, 0));
2483 /* With IEEE arithmetic, x - x may not equal 0, so we can't optimize
2485 if (TARGET_FLOAT_FORMAT
== IEEE_FLOAT_FORMAT
2486 && TREE_CODE (TREE_TYPE (expr
)) == REAL_TYPE
)
2488 /* fall through... */
2490 /* This and MINUS_EXPR can be changed into a comparison of the
2492 if (TREE_TYPE (TREE_OPERAND (expr
, 0))
2493 == TREE_TYPE (TREE_OPERAND (expr
, 1)))
2494 return build_binary_op (NE_EXPR
, TREE_OPERAND (expr
, 0),
2495 TREE_OPERAND (expr
, 1), 1);
2496 return build_binary_op (NE_EXPR
, TREE_OPERAND (expr
, 0),
2497 fold (build1 (NOP_EXPR
,
2498 TREE_TYPE (TREE_OPERAND (expr
, 0)),
2499 TREE_OPERAND (expr
, 1))), 1);
2502 if (integer_onep (TREE_OPERAND (expr
, 1))
2503 && TREE_TYPE (expr
) != boolean_type_node
)
2504 /* Using convert here would cause infinite recursion. */
2505 return build1 (NOP_EXPR
, boolean_type_node
, expr
);
2509 if (warn_parentheses
&& C_EXP_ORIGINAL_CODE (expr
) == MODIFY_EXPR
)
2510 warning ("suggest parentheses around assignment used as truth value");
2517 if (TREE_CODE (TREE_TYPE (expr
)) == COMPLEX_TYPE
)
2518 return (build_binary_op
2519 ((TREE_SIDE_EFFECTS (expr
)
2520 ? TRUTH_OR_EXPR
: TRUTH_ORIF_EXPR
),
2521 truthvalue_conversion (build_unary_op (REALPART_EXPR
, expr
, 0)),
2522 truthvalue_conversion (build_unary_op (IMAGPART_EXPR
, expr
, 0)),
2525 return build_binary_op (NE_EXPR
, expr
, integer_zero_node
, 1);
2528 /* Read the rest of a #-directive from input stream FINPUT.
2529 In normal use, the directive name and the white space after it
2530 have already been read, so they won't be included in the result.
2531 We allow for the fact that the directive line may contain
2532 a newline embedded within a character or string literal which forms
2533 a part of the directive.
2535 The value is a string in a reusable buffer. It remains valid
2536 only until the next time this function is called.
2538 The terminating character ('\n' or EOF) is left in FINPUT for the
2539 caller to re-read. */
2542 get_directive_line (finput
)
2543 register FILE *finput
;
2545 static char *directive_buffer
= NULL
;
2546 static unsigned buffer_length
= 0;
2548 register char *buffer_limit
;
2549 register int looking_for
= 0;
2550 register int char_escaped
= 0;
2552 if (buffer_length
== 0)
2554 directive_buffer
= (char *)xmalloc (128);
2555 buffer_length
= 128;
2558 buffer_limit
= &directive_buffer
[buffer_length
];
2560 for (p
= directive_buffer
; ; )
2564 /* Make buffer bigger if it is full. */
2565 if (p
>= buffer_limit
)
2567 register unsigned bytes_used
= (p
- directive_buffer
);
2571 = (char *)xrealloc (directive_buffer
, buffer_length
);
2572 p
= &directive_buffer
[bytes_used
];
2573 buffer_limit
= &directive_buffer
[buffer_length
];
2578 /* Discard initial whitespace. */
2579 if ((c
== ' ' || c
== '\t') && p
== directive_buffer
)
2582 /* Detect the end of the directive. */
2583 if (looking_for
== 0
2584 && (c
== '\n' || c
== EOF
))
2593 return directive_buffer
;
2595 /* Handle string and character constant syntax. */
2598 if (looking_for
== c
&& !char_escaped
)
2599 looking_for
= 0; /* Found terminator... stop looking. */
2602 if (c
== '\'' || c
== '"')
2603 looking_for
= c
; /* Don't stop buffering until we see another
2604 another one of these (or an EOF). */
2606 /* Handle backslash. */
2607 char_escaped
= (c
== '\\' && ! char_escaped
);
2611 /* Make a variant type in the proper way for C/C++, propagating qualifiers
2612 down to the element type of an array. */
2615 c_build_type_variant (type
, constp
, volatilep
)
2617 int constp
, volatilep
;
2619 if (TREE_CODE (type
) == ARRAY_TYPE
)
2620 return build_array_type (c_build_type_variant (TREE_TYPE (type
),
2622 TYPE_DOMAIN (type
));
2623 return build_type_variant (type
, constp
, volatilep
);