1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
28 extern struct obstack permanent_obstack;
30 /* Make bindings for __FUNCTION__ and __PRETTY_FUNCTION__. */
33 declare_function_name ()
35 tree decl, type, init;
36 char *name, *printable_name;
39 if (current_function_decl == NULL)
42 printable_name = "top level";
46 char *kind = "function";
47 if (TREE_CODE (TREE_TYPE (current_function_decl)) == METHOD_TYPE)
49 /* Allow functions to be nameless (such as artificial ones). */
50 if (DECL_NAME (current_function_decl))
51 name = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
54 printable_name = (*decl_printable_name) (current_function_decl, &kind);
57 /* If the default size of char arrays isn't big enough for the name,
59 len = strlen (name) + 1;
60 type = char_array_type_node;
61 if (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TREE_TYPE (char_array_type_node)))
63 type = build_array_type (char_type_node,
64 build_index_type (build_int_2 (len, 0)));
66 push_obstacks_nochange ();
67 decl = build_decl (VAR_DECL, get_identifier ("__FUNCTION__"), type);
68 TREE_STATIC (decl) = 1;
69 TREE_READONLY (decl) = 1;
70 DECL_SOURCE_LINE (decl) = 0;
71 DECL_IN_SYSTEM_HEADER (decl) = 1;
72 DECL_IGNORED_P (decl) = 1;
73 init = build_string (len, name);
74 TREE_TYPE (init) = type;
75 DECL_INITIAL (decl) = init;
76 finish_decl (pushdecl (decl), init, NULL_TREE);
78 len = strlen (printable_name) + 1;
79 type = char_array_type_node;
80 if (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TREE_TYPE (char_array_type_node)))
82 type = build_array_type (char_type_node,
83 build_index_type (build_int_2 (len, 0)));
85 push_obstacks_nochange ();
86 decl = build_decl (VAR_DECL, get_identifier ("__PRETTY_FUNCTION__"), type);
87 TREE_STATIC (decl) = 1;
88 TREE_READONLY (decl) = 1;
89 DECL_SOURCE_LINE (decl) = 0;
90 DECL_IN_SYSTEM_HEADER (decl) = 1;
91 DECL_IGNORED_P (decl) = 1;
92 init = build_string (len, printable_name);
93 TREE_TYPE (init) = type;
94 DECL_INITIAL (decl) = init;
95 finish_decl (pushdecl (decl), init, NULL_TREE);
98 /* Given a chain of STRING_CST nodes,
99 concatenate them into one STRING_CST
100 and give it a suitable array-of-chars data type. */
103 combine_strings (strings)
106 register tree value, t;
107 register int length = 1;
110 int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
113 if (TREE_CHAIN (strings))
115 /* More than one in the chain, so concatenate. */
116 register char *p, *q;
118 /* Don't include the \0 at the end of each substring,
119 except for the last one.
120 Count wide strings and ordinary strings separately. */
121 for (t = strings; t; t = TREE_CHAIN (t))
123 if (TREE_TYPE (t) == wchar_array_type_node)
125 wide_length += (TREE_STRING_LENGTH (t) - wchar_bytes);
129 length += (TREE_STRING_LENGTH (t) - 1);
132 /* If anything is wide, the non-wides will be converted,
133 which makes them take more space. */
135 length = length * wchar_bytes + wide_length;
137 p = savealloc (length);
139 /* Copy the individual strings into the new combined string.
140 If the combined string is wide, convert the chars to ints
141 for any individual strings that are not wide. */
144 for (t = strings; t; t = TREE_CHAIN (t))
146 int len = (TREE_STRING_LENGTH (t)
147 - ((TREE_TYPE (t) == wchar_array_type_node)
149 if ((TREE_TYPE (t) == wchar_array_type_node) == wide_flag)
151 bcopy (TREE_STRING_POINTER (t), q, len);
157 for (i = 0; i < len; i++)
158 ((int *) q)[i] = TREE_STRING_POINTER (t)[i];
159 q += len * wchar_bytes;
165 for (i = 0; i < wchar_bytes; i++)
171 value = make_node (STRING_CST);
172 TREE_STRING_POINTER (value) = p;
173 TREE_STRING_LENGTH (value) = length;
174 TREE_CONSTANT (value) = 1;
179 length = TREE_STRING_LENGTH (value);
180 if (TREE_TYPE (value) == wchar_array_type_node)
184 /* Compute the number of elements, for the array type. */
185 nchars = wide_flag ? length / wchar_bytes : length;
187 /* Create the array type for the string constant.
188 -Wwrite-strings says make the string constant an array of const char
189 so that copying it to a non-const pointer will get a warning. */
190 if (warn_write_strings
191 && (! flag_traditional && ! flag_writable_strings))
194 = build_type_variant (wide_flag ? wchar_type_node : char_type_node,
197 = build_array_type (elements,
198 build_index_type (build_int_2 (nchars - 1, 0)));
202 = build_array_type (wide_flag ? wchar_type_node : char_type_node,
203 build_index_type (build_int_2 (nchars - 1, 0)));
204 TREE_CONSTANT (value) = 1;
205 TREE_STATIC (value) = 1;
209 /* Process the attributes listed in ATTRIBUTES
210 and install them in DECL. */
213 decl_attributes (decl, attributes)
214 tree decl, attributes;
217 for (a = attributes; a; a = TREE_CHAIN (a))
218 if (TREE_VALUE (a) == get_identifier ("packed"))
220 if (TREE_CODE (decl) == FIELD_DECL)
221 DECL_PACKED (decl) = 1;
222 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
223 used for DECL_REGISTER. It wouldn't mean anything anyway. */
225 else if (TREE_VALUE (a) != 0
226 && TREE_CODE (TREE_VALUE (a)) == TREE_LIST
227 && TREE_PURPOSE (TREE_VALUE (a)) == get_identifier ("mode"))
231 = IDENTIFIER_POINTER (TREE_VALUE (TREE_VALUE (a)));
233 /* Give this decl a type with the specified mode. */
234 for (i = 0; i < NUM_MACHINE_MODES; i++)
235 if (!strcmp (specified_name, GET_MODE_NAME (i)))
238 = type_for_mode (i, TREE_UNSIGNED (TREE_TYPE (decl)));
241 TREE_TYPE (decl) = type;
242 DECL_SIZE (decl) = 0;
243 layout_decl (decl, 0);
246 error ("no data type for mode `%s'", specified_name);
249 if (i == NUM_MACHINE_MODES)
250 error ("unknown machine mode `%s'", specified_name);
252 else if (TREE_VALUE (a) != 0
253 && TREE_CODE (TREE_VALUE (a)) == TREE_LIST
254 && TREE_PURPOSE (TREE_VALUE (a)) == get_identifier ("aligned"))
256 int align = TREE_INT_CST_LOW (TREE_VALUE (TREE_VALUE (a)))
259 if (exact_log2 (align) == -1)
260 error_with_decl (decl,
261 "requested alignment of `%s' is not a power of 2");
262 else if (TREE_CODE (decl) != VAR_DECL
263 && TREE_CODE (decl) != FIELD_DECL)
264 error_with_decl (decl,
265 "alignment specified for `%s'");
267 DECL_ALIGN (decl) = align;
269 else if (TREE_VALUE (a) != 0
270 && TREE_CODE (TREE_VALUE (a)) == TREE_LIST
271 && TREE_PURPOSE (TREE_VALUE (a)) == get_identifier ("format"))
273 tree list = TREE_VALUE (TREE_VALUE (a));
274 tree format_type = TREE_PURPOSE (list);
275 int format_num = TREE_INT_CST_LOW (TREE_PURPOSE (TREE_VALUE (list)));
276 int first_arg_num = TREE_INT_CST_LOW (TREE_VALUE (TREE_VALUE (list)));
281 if (TREE_CODE (decl) != FUNCTION_DECL)
283 error_with_decl (decl,
284 "argument format specified for non-function `%s'");
288 if (format_type == get_identifier ("printf"))
290 else if (format_type == get_identifier ("scanf"))
294 error_with_decl (decl, "unrecognized format specifier for `%s'");
298 if (first_arg_num != 0 && first_arg_num <= format_num)
300 error_with_decl (decl,
301 "format string arg follows the args to be formatted, for `%s'");
305 /* Verify that the format_num argument is actually a string, in case
306 the format attribute is in error. */
307 argument = TYPE_ARG_TYPES (TREE_TYPE (decl));
308 for (arg_num = 1; ; ++arg_num)
310 if (argument == 0 || arg_num == format_num)
312 argument = TREE_CHAIN (argument);
315 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
316 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
319 error_with_decl (decl,
320 "format string arg not a string type, for `%s'");
323 /* Verify that first_arg_num points to the last argument, the ... */
325 arg_num++, argument = TREE_CHAIN (argument);
326 if (arg_num != first_arg_num)
328 error_with_decl (decl,
329 "args to be formatted is not ..., for `%s'");
333 record_format_info (DECL_NAME (decl), is_scan, format_num,
338 /* Print a warning if a constant expression had overflow in folding.
339 Invoke this function on every expression that the language
340 requires to be a constant expression.
341 Note the ANSI C standard says it is erroneous for a
342 constant expression to overflow. */
345 constant_expression_warning (value)
348 if (TREE_CODE (value) == INTEGER_CST && TREE_CONSTANT_OVERFLOW (value))
350 pedwarn ("overflow in constant expression");
351 /* Suppress duplicate warnings. */
352 TREE_CONSTANT_OVERFLOW (value) = 0;
356 /* Print a warning if an expression had overflow in folding.
357 Invoke this function on every expression that
358 (1) appears in the source code, and
359 (2) might be a constant expression that overflowed, and
360 (3) is not already checked by convert_and_check;
361 however, do not invoke this function on operands of explicit casts. */
364 overflow_warning (value)
367 if (TREE_CODE (value) == INTEGER_CST && TREE_CONSTANT_OVERFLOW (value))
369 pedwarn ("integer overflow in expression");
370 TREE_CONSTANT_OVERFLOW (value) = 0;
374 /* Print a warning if a large constant is truncated to unsigned,
375 or if -Wconversion is used and a constant < 0 is converted to unsigned.
376 Invoke this function on every expression that might be implicitly
377 converted to an unsigned type. */
380 unsigned_conversion_warning (result, operand)
381 tree result, operand;
383 if (TREE_CODE (operand) == INTEGER_CST
384 && TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
385 && TREE_UNSIGNED (TREE_TYPE (result))
386 && !int_fits_type_p (operand, TREE_TYPE (result)))
388 if (!int_fits_type_p (operand, signed_type (TREE_TYPE (result))))
389 /* This detects cases like converting -129 or 256 to unsigned char. */
390 pedwarn ("large integer implicitly truncated to unsigned type");
391 else if (warn_conversion)
392 pedwarn ("negative integer implicitly converted to unsigned type");
396 /* Convert EXPR to TYPE, warning about conversion problems with constants.
397 Invoke this function on every expression that is converted implicitly,
398 i.e. because of language rules and not because of an explicit cast. */
401 convert_and_check (type, expr)
404 tree t = convert (type, expr);
405 if (TREE_CODE (t) == INTEGER_CST)
407 if (TREE_UNSIGNED (TREE_TYPE (expr))
408 && !TREE_UNSIGNED (type)
409 && TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
410 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (expr)))
411 /* No warning for converting 0x80000000 to int. */
412 TREE_CONSTANT_OVERFLOW (t) = 0;
413 else if (TREE_CONSTANT_OVERFLOW (t))
415 pedwarn ("overflow in implicit constant conversion");
416 TREE_CONSTANT_OVERFLOW (t) = 0;
419 unsigned_conversion_warning (t, expr);
425 c_expand_expr_stmt (expr)
428 /* Do default conversion if safe and possibly important,
429 in case within ({...}). */
430 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE && lvalue_p (expr))
431 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
432 expr = default_conversion (expr);
434 if (TREE_TYPE (expr) != error_mark_node
435 && TYPE_SIZE (TREE_TYPE (expr)) == 0
436 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
437 error ("expression statement has incomplete type");
439 expand_expr_stmt (expr);
442 /* Validate the expression after `case' and apply default promotions. */
445 check_case_value (value)
448 if (value == NULL_TREE)
451 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
452 STRIP_TYPE_NOPS (value);
454 if (TREE_CODE (value) != INTEGER_CST
455 && value != error_mark_node)
457 error ("case label does not reduce to an integer constant");
458 value = error_mark_node;
461 /* Promote char or short to int. */
462 value = default_conversion (value);
464 constant_expression_warning (value);
469 /* Return an integer type with BITS bits of precision,
470 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
473 type_for_size (bits, unsignedp)
477 if (bits == TYPE_PRECISION (signed_char_type_node))
478 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
480 if (bits == TYPE_PRECISION (short_integer_type_node))
481 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
483 if (bits == TYPE_PRECISION (integer_type_node))
484 return unsignedp ? unsigned_type_node : integer_type_node;
486 if (bits == TYPE_PRECISION (long_integer_type_node))
487 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
489 if (bits == TYPE_PRECISION (long_long_integer_type_node))
490 return (unsignedp ? long_long_unsigned_type_node
491 : long_long_integer_type_node);
493 if (bits <= TYPE_PRECISION (intQI_type_node))
494 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
496 if (bits <= TYPE_PRECISION (intHI_type_node))
497 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
499 if (bits <= TYPE_PRECISION (intSI_type_node))
500 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
502 if (bits <= TYPE_PRECISION (intDI_type_node))
503 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
508 /* Return a data type that has machine mode MODE.
509 If the mode is an integer,
510 then UNSIGNEDP selects between signed and unsigned types. */
513 type_for_mode (mode, unsignedp)
514 enum machine_mode mode;
517 if (mode == TYPE_MODE (signed_char_type_node))
518 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
520 if (mode == TYPE_MODE (short_integer_type_node))
521 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
523 if (mode == TYPE_MODE (integer_type_node))
524 return unsignedp ? unsigned_type_node : integer_type_node;
526 if (mode == TYPE_MODE (long_integer_type_node))
527 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
529 if (mode == TYPE_MODE (long_long_integer_type_node))
530 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
532 if (mode == TYPE_MODE (intQI_type_node))
533 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
535 if (mode == TYPE_MODE (intHI_type_node))
536 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
538 if (mode == TYPE_MODE (intSI_type_node))
539 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
541 if (mode == TYPE_MODE (intDI_type_node))
542 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
544 if (mode == TYPE_MODE (float_type_node))
545 return float_type_node;
547 if (mode == TYPE_MODE (double_type_node))
548 return double_type_node;
550 if (mode == TYPE_MODE (long_double_type_node))
551 return long_double_type_node;
553 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
554 return build_pointer_type (char_type_node);
556 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
557 return build_pointer_type (integer_type_node);
562 /* Print an error message for invalid operands to arith operation CODE.
563 NOP_EXPR is used as a special case (see truthvalue_conversion). */
566 binary_op_error (code)
569 register char *opname;
573 error ("invalid truth-value expression");
583 opname = "max"; break;
585 opname = "min"; break;
587 opname = "=="; break;
589 opname = "!="; break;
591 opname = "<="; break;
593 opname = ">="; break;
599 opname = "<<"; break;
601 opname = ">>"; break;
612 case TRUTH_ANDIF_EXPR:
613 opname = "&&"; break;
614 case TRUTH_ORIF_EXPR:
615 opname = "||"; break;
620 opname = "rotate"; break;
622 error ("invalid operands to binary %s", opname);
625 /* Subroutine of build_binary_op, used for comparison operations.
626 See if the operands have both been converted from subword integer types
627 and, if so, perhaps change them both back to their original type.
629 The arguments of this function are all pointers to local variables
630 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
631 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
633 If this function returns nonzero, it means that the comparison has
634 a constant value. What this function returns is an expression for
638 shorten_compare (op0_ptr, op1_ptr, restype_ptr, rescode_ptr)
639 tree *op0_ptr, *op1_ptr;
641 enum tree_code *rescode_ptr;
646 int unsignedp0, unsignedp1;
648 tree primop0, primop1;
649 enum tree_code code = *rescode_ptr;
651 /* Throw away any conversions to wider types
652 already present in the operands. */
654 primop0 = get_narrower (op0, &unsignedp0);
655 primop1 = get_narrower (op1, &unsignedp1);
657 /* Handle the case that OP0 does not *contain* a conversion
658 but it *requires* conversion to FINAL_TYPE. */
660 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
661 unsignedp0 = TREE_UNSIGNED (TREE_TYPE (op0));
662 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
663 unsignedp1 = TREE_UNSIGNED (TREE_TYPE (op1));
665 /* If one of the operands must be floated, we cannot optimize. */
666 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
667 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
669 /* If first arg is constant, swap the args (changing operation
670 so value is preserved), for canonicalization. */
672 if (TREE_CONSTANT (primop0))
674 register tree tem = primop0;
675 register int temi = unsignedp0;
683 unsignedp0 = unsignedp1;
707 /* If comparing an integer against a constant more bits wide,
708 maybe we can deduce a value of 1 or 0 independent of the data.
709 Or else truncate the constant now
710 rather than extend the variable at run time.
712 This is only interesting if the constant is the wider arg.
713 Also, it is not safe if the constant is unsigned and the
714 variable arg is signed, since in this case the variable
715 would be sign-extended and then regarded as unsigned.
716 Our technique fails in this case because the lowest/highest
717 possible unsigned results don't follow naturally from the
718 lowest/highest possible values of the variable operand.
719 For just EQ_EXPR and NE_EXPR there is another technique that
720 could be used: see if the constant can be faithfully represented
721 in the other operand's type, by truncating it and reextending it
722 and see if that preserves the constant's value. */
725 && TREE_CODE (primop1) == INTEGER_CST
726 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
728 int min_gt, max_gt, min_lt, max_lt;
730 /* 1 if comparison is nominally unsigned. */
731 int unsignedp = TREE_UNSIGNED (*restype_ptr);
734 type = signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0));
736 maxval = TYPE_MAX_VALUE (type);
737 minval = TYPE_MIN_VALUE (type);
739 if (unsignedp && !unsignedp0)
740 *restype_ptr = signed_type (*restype_ptr);
742 if (TREE_TYPE (primop1) != *restype_ptr)
743 primop1 = convert (*restype_ptr, primop1);
744 if (type != *restype_ptr)
746 minval = convert (*restype_ptr, minval);
747 maxval = convert (*restype_ptr, maxval);
750 if (unsignedp && unsignedp0)
752 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
753 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
754 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
755 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
759 min_gt = INT_CST_LT (primop1, minval);
760 max_gt = INT_CST_LT (primop1, maxval);
761 min_lt = INT_CST_LT (minval, primop1);
762 max_lt = INT_CST_LT (maxval, primop1);
766 /* This used to be a switch, but Genix compiler can't handle that. */
769 if (max_lt || min_gt)
770 val = integer_one_node;
772 else if (code == EQ_EXPR)
774 if (max_lt || min_gt)
775 val = integer_zero_node;
777 else if (code == LT_EXPR)
780 val = integer_one_node;
782 val = integer_zero_node;
784 else if (code == GT_EXPR)
787 val = integer_one_node;
789 val = integer_zero_node;
791 else if (code == LE_EXPR)
794 val = integer_one_node;
796 val = integer_zero_node;
798 else if (code == GE_EXPR)
801 val = integer_one_node;
803 val = integer_zero_node;
806 /* If primop0 was sign-extended and unsigned comparison specd,
807 we did a signed comparison above using the signed type bounds.
808 But the comparison we output must be unsigned.
810 Also, for inequalities, VAL is no good; but if the signed
811 comparison had *any* fixed result, it follows that the
812 unsigned comparison just tests the sign in reverse
813 (positive values are LE, negative ones GE).
814 So we can generate an unsigned comparison
815 against an extreme value of the signed type. */
817 if (unsignedp && !unsignedp0)
824 primop1 = TYPE_MIN_VALUE (type);
830 primop1 = TYPE_MAX_VALUE (type);
834 type = unsigned_type (type);
837 if (!max_gt && !unsignedp0)
839 /* This is the case of (char)x >?< 0x80, which people used to use
840 expecting old C compilers to change the 0x80 into -0x80. */
841 if (val == integer_zero_node)
842 warning ("comparison is always 0 due to limited range of data type");
843 if (val == integer_one_node)
844 warning ("comparison is always 1 due to limited range of data type");
847 if (!min_lt && unsignedp0)
849 /* This is the case of (unsigned char)x >?< -1 or < 0. */
850 if (val == integer_zero_node)
851 warning ("comparison is always 0 due to limited range of data type");
852 if (val == integer_one_node)
853 warning ("comparison is always 1 due to limited range of data type");
858 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
859 if (TREE_SIDE_EFFECTS (primop0))
860 return build (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
864 /* Value is not predetermined, but do the comparison
865 in the type of the operand that is not constant.
866 TYPE is already properly set. */
868 else if (real1 && real2
869 && (TYPE_PRECISION (TREE_TYPE (primop0))
870 == TYPE_PRECISION (TREE_TYPE (primop1))))
871 type = TREE_TYPE (primop0);
873 /* If args' natural types are both narrower than nominal type
874 and both extend in the same manner, compare them
875 in the type of the wider arg.
876 Otherwise must actually extend both to the nominal
877 common type lest different ways of extending
879 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
881 else if (unsignedp0 == unsignedp1 && real1 == real2
882 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
883 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
885 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
886 type = signed_or_unsigned_type (unsignedp0
887 || TREE_UNSIGNED (*restype_ptr),
889 /* Make sure shorter operand is extended the right way
890 to match the longer operand. */
891 primop0 = convert (signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0)),
893 primop1 = convert (signed_or_unsigned_type (unsignedp1, TREE_TYPE (primop1)),
898 /* Here we must do the comparison on the nominal type
899 using the args exactly as we received them. */
904 if (!real1 && !real2 && integer_zerop (primop1)
905 && TREE_UNSIGNED (TREE_TYPE (primop0)))
912 warning ("unsigned value >= 0 is always 1");
913 value = integer_one_node;
918 warning ("unsigned value < 0 is always 0");
919 value = integer_zero_node;
924 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
925 if (TREE_SIDE_EFFECTS (primop0))
926 return build (COMPOUND_EXPR, TREE_TYPE (value),
933 *op0_ptr = convert (type, primop0);
934 *op1_ptr = convert (type, primop1);
936 *restype_ptr = integer_type_node;
941 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
942 or validate its data type for an `if' or `while' statement or ?..: exp.
944 This preparation consists of taking the ordinary
945 representation of an expression expr and producing a valid tree
946 boolean expression describing whether expr is nonzero. We could
947 simply always do build_binary_op (NE_EXPR, expr, integer_zero_node, 1),
948 but we optimize comparisons, &&, ||, and !.
950 The resulting type should always be `integer_type_node'. */
953 truthvalue_conversion (expr)
956 register enum tree_code code;
958 if (TREE_CODE (expr) == ERROR_MARK)
961 /* These really should return error_mark_node after 2.4 is stable.
962 But not all callers handle ERROR_MARK properly. */
963 switch (TREE_CODE (TREE_TYPE (expr)))
966 error ("struct type value used where scalar is required");
967 return integer_zero_node;
970 error ("union type value used where scalar is required");
971 return integer_zero_node;
974 error ("array type value used where scalar is required");
975 return integer_zero_node;
981 switch (TREE_CODE (expr))
983 /* It is simpler and generates better code to have only TRUTH_*_EXPR
984 or comparison expressions as truth values at this level. */
987 /* A one-bit unsigned bit-field is already acceptable. */
988 if (1 == TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (expr, 1)))
989 && TREE_UNSIGNED (TREE_OPERAND (expr, 1)))
995 /* It is simpler and generates better code to have only TRUTH_*_EXPR
996 or comparison expressions as truth values at this level. */
998 if (integer_zerop (TREE_OPERAND (expr, 1)))
999 return build_unary_op (TRUTH_NOT_EXPR, TREE_OPERAND (expr, 0), 0);
1001 case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
1002 case TRUTH_ANDIF_EXPR:
1003 case TRUTH_ORIF_EXPR:
1004 case TRUTH_AND_EXPR:
1006 case TRUTH_XOR_EXPR:
1011 return integer_zerop (expr) ? integer_zero_node : integer_one_node;
1014 return real_zerop (expr) ? integer_zero_node : integer_one_node;
1017 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0)))
1018 return build (COMPOUND_EXPR, integer_type_node,
1019 TREE_OPERAND (expr, 0), integer_one_node);
1021 return integer_one_node;
1024 return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
1025 ? TRUTH_AND_EXPR : TRUTH_ANDIF_EXPR),
1026 truthvalue_conversion (TREE_OPERAND (expr, 0)),
1027 truthvalue_conversion (TREE_OPERAND (expr, 1)),
1034 /* These don't change whether an object is non-zero or zero. */
1035 return truthvalue_conversion (TREE_OPERAND (expr, 0));
1039 /* These don't change whether an object is zero or non-zero, but
1040 we can't ignore them if their second arg has side-effects. */
1041 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
1042 return build (COMPOUND_EXPR, integer_type_node, TREE_OPERAND (expr, 1),
1043 truthvalue_conversion (TREE_OPERAND (expr, 0)));
1045 return truthvalue_conversion (TREE_OPERAND (expr, 0));
1048 /* Distribute the conversion into the arms of a COND_EXPR. */
1049 return fold (build (COND_EXPR, integer_type_node, TREE_OPERAND (expr, 0),
1050 truthvalue_conversion (TREE_OPERAND (expr, 1)),
1051 truthvalue_conversion (TREE_OPERAND (expr, 2))));
1054 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
1055 since that affects how `default_conversion' will behave. */
1056 if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE
1057 || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
1059 /* fall through... */
1061 /* If this is widening the argument, we can ignore it. */
1062 if (TYPE_PRECISION (TREE_TYPE (expr))
1063 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
1064 return truthvalue_conversion (TREE_OPERAND (expr, 0));
1068 /* With IEEE arithmetic, x - x may not equal 0, so we can't optimize
1070 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
1071 && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE)
1073 /* fall through... */
1075 /* This and MINUR_EXPR can be changed into a comparison of the
1077 if (TREE_TYPE (TREE_OPERAND (expr, 0))
1078 == TREE_TYPE (TREE_OPERAND (expr, 1)))
1079 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
1080 TREE_OPERAND (expr, 1), 1);
1081 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
1082 fold (build1 (NOP_EXPR,
1083 TREE_TYPE (TREE_OPERAND (expr, 0)),
1084 TREE_OPERAND (expr, 1))), 1);
1087 if (warn_parentheses && C_EXP_ORIGINAL_CODE (expr) == MODIFY_EXPR)
1088 warning ("suggest parentheses around assignment used as truth value");
1092 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
1093 return (build_binary_op
1094 ((TREE_SIDE_EFFECTS (expr)
1095 ? TRUTH_AND_EXPR : TRUTH_ANDIF_EXPR),
1096 truthvalue_conversion (build_unary_op (REALPART_EXPR, expr, 0)),
1097 truthvalue_conversion (build_unary_op (IMAGPART_EXPR, expr, 0)),
1100 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
1103 /* Read the rest of a #-directive from input stream FINPUT.
1104 In normal use, the directive name and the white space after it
1105 have already been read, so they won't be included in the result.
1106 We allow for the fact that the directive line may contain
1107 a newline embedded within a character or string literal which forms
1108 a part of the directive.
1110 The value is a string in a reusable buffer. It remains valid
1111 only until the next time this function is called. */
1114 get_directive_line (finput)
1115 register FILE *finput;
1117 static char *directive_buffer = NULL;
1118 static unsigned buffer_length = 0;
1120 register char *buffer_limit;
1121 register int looking_for = 0;
1122 register int char_escaped = 0;
1124 if (buffer_length == 0)
1126 directive_buffer = (char *)xmalloc (128);
1127 buffer_length = 128;
1130 buffer_limit = &directive_buffer[buffer_length];
1132 for (p = directive_buffer; ; )
1136 /* Make buffer bigger if it is full. */
1137 if (p >= buffer_limit)
1139 register unsigned bytes_used = (p - directive_buffer);
1143 = (char *)xrealloc (directive_buffer, buffer_length);
1144 p = &directive_buffer[bytes_used];
1145 buffer_limit = &directive_buffer[buffer_length];
1150 /* Discard initial whitespace. */
1151 if ((c == ' ' || c == '\t') && p == directive_buffer)
1154 /* Detect the end of the directive. */
1155 if (c == '\n' && looking_for == 0)
1164 return directive_buffer;
1166 /* Handle string and character constant syntax. */
1169 if (looking_for == c && !char_escaped)
1170 looking_for = 0; /* Found terminator... stop looking. */
1173 if (c == '\'' || c == '"')
1174 looking_for = c; /* Don't stop buffering until we see another
1175 another one of these (or an EOF). */
1177 /* Handle backslash. */
1178 char_escaped = (c == '\\' && ! char_escaped);
1182 /* Make a variant type in the proper way for C/C++, propagating qualifiers
1183 down to the element type of an array. */
1186 c_build_type_variant (type, constp, volatilep)
1188 int constp, volatilep;
1190 if (TREE_CODE (type) == ARRAY_TYPE)
1192 tree real_main_variant = TYPE_MAIN_VARIANT (type);
1193 int permanent = TREE_PERMANENT (type);
1196 push_obstacks (&permanent_obstack, &permanent_obstack);
1197 type = build_array_type (c_build_type_variant (TREE_TYPE (type),
1199 TYPE_DOMAIN (type));
1200 TYPE_MAIN_VARIANT (type) = real_main_variant;
1204 return build_type_variant (type, constp, volatilep);