]> gcc.gnu.org Git - gcc.git/blob - gcc/c-common.c
(init_decl_processing): Fix type of __builtin_constant_p.
[gcc.git] / gcc / c-common.c
1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
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)
9 any later version.
10
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.
15
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. */
19
20 #include "config.h"
21 #include "tree.h"
22 #include "c-lex.h"
23 #include "c-tree.h"
24 #include "flags.h"
25 #include <stdio.h>
26
27 /* Make bindings for __FUNCTION__ and __PRETTY_FUNCTION__. */
28
29 void
30 declare_function_name ()
31 {
32 tree decl, init;
33 char *name, *printable_name;
34
35 if (current_function_decl == NULL)
36 {
37 name = "";
38 printable_name = "top level";
39 }
40 else
41 {
42 char *kind = "function";
43 if (TREE_CODE (TREE_TYPE (current_function_decl)) == METHOD_TYPE)
44 kind = "method";
45 name = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
46 printable_name = (*decl_printable_name) (current_function_decl, &kind);
47 }
48
49 push_obstacks_nochange ();
50 decl = build_decl (VAR_DECL, get_identifier ("__FUNCTION__"),
51 char_array_type_node);
52 TREE_STATIC (decl) = 1;
53 TREE_READONLY (decl) = 1;
54 DECL_IN_SYSTEM_HEADER (decl) = 1;
55 DECL_IGNORED_P (decl) = 1;
56 init = build_string (strlen (name) + 1, name);
57 TREE_TYPE (init) = char_array_type_node;
58 DECL_INITIAL (decl) = init;
59 finish_decl (pushdecl (decl), init, NULL_TREE);
60
61 push_obstacks_nochange ();
62 decl = build_decl (VAR_DECL, get_identifier ("__PRETTY_FUNCTION__"),
63 char_array_type_node);
64 TREE_STATIC (decl) = 1;
65 TREE_READONLY (decl) = 1;
66 DECL_IN_SYSTEM_HEADER (decl) = 1;
67 DECL_IGNORED_P (decl) = 1;
68 init = build_string (strlen (printable_name) + 1, printable_name);
69 TREE_TYPE (init) = char_array_type_node;
70 DECL_INITIAL (decl) = init;
71 finish_decl (pushdecl (decl), init, NULL_TREE);
72 }
73
74 /* Given a chain of STRING_CST nodes,
75 concatenate them into one STRING_CST
76 and give it a suitable array-of-chars data type. */
77
78 tree
79 combine_strings (strings)
80 tree strings;
81 {
82 register tree value, t;
83 register int length = 1;
84 int wide_length = 0;
85 int wide_flag = 0;
86 int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
87 int nchars;
88
89 if (TREE_CHAIN (strings))
90 {
91 /* More than one in the chain, so concatenate. */
92 register char *p, *q;
93
94 /* Don't include the \0 at the end of each substring,
95 except for the last one.
96 Count wide strings and ordinary strings separately. */
97 for (t = strings; t; t = TREE_CHAIN (t))
98 {
99 if (TREE_TYPE (t) == wchar_array_type_node)
100 {
101 wide_length += (TREE_STRING_LENGTH (t) - wchar_bytes);
102 wide_flag = 1;
103 }
104 else
105 length += (TREE_STRING_LENGTH (t) - 1);
106 }
107
108 /* If anything is wide, the non-wides will be converted,
109 which makes them take more space. */
110 if (wide_flag)
111 length = length * wchar_bytes + wide_length;
112
113 p = savealloc (length);
114
115 /* Copy the individual strings into the new combined string.
116 If the combined string is wide, convert the chars to ints
117 for any individual strings that are not wide. */
118
119 q = p;
120 for (t = strings; t; t = TREE_CHAIN (t))
121 {
122 int len = (TREE_STRING_LENGTH (t)
123 - ((TREE_TYPE (t) == wchar_array_type_node)
124 ? wchar_bytes : 1));
125 if ((TREE_TYPE (t) == wchar_array_type_node) == wide_flag)
126 {
127 bcopy (TREE_STRING_POINTER (t), q, len);
128 q += len;
129 }
130 else
131 {
132 int i;
133 for (i = 0; i < len; i++)
134 ((int *) q)[i] = TREE_STRING_POINTER (t)[i];
135 q += len * wchar_bytes;
136 }
137 }
138 if (wide_flag)
139 {
140 int i;
141 for (i = 0; i < wchar_bytes; i++)
142 *q++ = 0;
143 }
144 else
145 *q = 0;
146
147 value = make_node (STRING_CST);
148 TREE_STRING_POINTER (value) = p;
149 TREE_STRING_LENGTH (value) = length;
150 TREE_CONSTANT (value) = 1;
151 }
152 else
153 {
154 value = strings;
155 length = TREE_STRING_LENGTH (value);
156 if (TREE_TYPE (value) == wchar_array_type_node)
157 wide_flag = 1;
158 }
159
160 /* Compute the number of elements, for the array type. */
161 nchars = wide_flag ? length / wchar_bytes : length;
162
163 /* Create the array type for the string constant.
164 -Wwrite-strings says make the string constant an array of const char
165 so that copying it to a non-const pointer will get a warning. */
166 if (warn_write_strings
167 && (! flag_traditional && ! flag_writable_strings))
168 {
169 tree elements
170 = build_type_variant (wide_flag ? wchar_type_node : char_type_node,
171 1, 0);
172 TREE_TYPE (value)
173 = build_array_type (elements,
174 build_index_type (build_int_2 (nchars - 1, 0)));
175 }
176 else
177 TREE_TYPE (value)
178 = build_array_type (wide_flag ? wchar_type_node : char_type_node,
179 build_index_type (build_int_2 (nchars - 1, 0)));
180 TREE_CONSTANT (value) = 1;
181 TREE_STATIC (value) = 1;
182 return value;
183 }
184 \f
185 /* Process the attributes listed in ATTRIBUTES
186 and install them in DECL. */
187
188 void
189 decl_attributes (decl, attributes)
190 tree decl, attributes;
191 {
192 tree a;
193 for (a = attributes; a; a = TREE_CHAIN (a))
194 if (TREE_VALUE (a) == get_identifier ("packed"))
195 {
196 if (TREE_CODE (decl) == FIELD_DECL)
197 DECL_PACKED (decl) = 1;
198 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
199 used for DECL_REGISTER. It wouldn't mean anything anyway. */
200 }
201 else if (TREE_VALUE (a) != 0
202 && TREE_CODE (TREE_VALUE (a)) == TREE_LIST
203 && TREE_PURPOSE (TREE_VALUE (a)) == get_identifier ("mode"))
204 {
205 int i;
206 char *specified_name
207 = IDENTIFIER_POINTER (TREE_VALUE (TREE_VALUE (a)));
208
209 /* Give this decl a type with the specified mode. */
210 for (i = 0; i < NUM_MACHINE_MODES; i++)
211 if (!strcmp (specified_name, GET_MODE_NAME (i)))
212 {
213 tree type
214 = type_for_mode (i, TREE_UNSIGNED (TREE_TYPE (decl)));
215 if (type != 0)
216 {
217 TREE_TYPE (decl) = type;
218 DECL_SIZE (decl) = 0;
219 layout_decl (decl, 0);
220 }
221 else
222 error ("no data type for mode `%s'", specified_name);
223 break;
224 }
225 if (i == NUM_MACHINE_MODES)
226 error ("unknown machine mode `%s'", specified_name);
227 }
228 else if (TREE_VALUE (a) != 0
229 && TREE_CODE (TREE_VALUE (a)) == TREE_LIST
230 && TREE_PURPOSE (TREE_VALUE (a)) == get_identifier ("aligned"))
231 {
232 int align = TREE_INT_CST_LOW (TREE_VALUE (TREE_VALUE (a)))
233 * BITS_PER_UNIT;
234
235 if (exact_log2 (align) == -1)
236 error_with_decl (decl,
237 "requested alignment of `%s' is not a power of 2");
238 else if (TREE_CODE (decl) != VAR_DECL
239 && TREE_CODE (decl) != FIELD_DECL)
240 error_with_decl (decl,
241 "alignment specified for `%s'");
242 else
243 DECL_ALIGN (decl) = align;
244 }
245 else if (TREE_VALUE (a) != 0
246 && TREE_CODE (TREE_VALUE (a)) == TREE_LIST
247 && TREE_PURPOSE (TREE_VALUE (a)) == get_identifier ("format"))
248 {
249 tree list = TREE_VALUE (TREE_VALUE (a));
250 tree format_type = TREE_PURPOSE (list);
251 int format_num = TREE_INT_CST_LOW (TREE_PURPOSE (TREE_VALUE (list)));
252 int first_arg_num = TREE_INT_CST_LOW (TREE_VALUE (TREE_VALUE (list)));
253 int is_scan;
254
255 if (TREE_CODE (decl) != FUNCTION_DECL)
256 {
257 error_with_decl (decl,
258 "argument format specified for non-function `%s'");
259 return;
260 }
261
262 if (format_type == get_identifier ("printf"))
263 is_scan = 0;
264 else if (format_type == get_identifier ("scanf"))
265 is_scan = 1;
266 else
267 {
268 error_with_decl (decl, "unrecognized format specifier for `%s'");
269 return;
270 }
271
272 if (first_arg_num != 0 && first_arg_num <= format_num)
273 {
274 error_with_decl (decl,
275 "format string arg follows the args to be formatted, for `%s'");
276 return;
277 }
278
279 record_format_info (DECL_NAME (decl), is_scan, format_num,
280 first_arg_num);
281 }
282 }
283 \f
284 void
285 c_expand_expr_stmt (expr)
286 tree expr;
287 {
288 /* Do default conversion if safe and possibly important,
289 in case within ({...}). */
290 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE && lvalue_p (expr))
291 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
292 expr = default_conversion (expr);
293
294 if (TREE_TYPE (expr) != error_mark_node
295 && TYPE_SIZE (TREE_TYPE (expr)) == 0
296 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
297 error ("expression statement has incomplete type");
298
299 expand_expr_stmt (expr);
300 }
301 \f
302 /* Validate the expression after `case' and apply default promotions. */
303
304 tree
305 check_case_value (value)
306 tree value;
307 {
308 if (value == NULL_TREE)
309 return value;
310
311 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
312 STRIP_TYPE_NOPS (value);
313
314 if (TREE_CODE (value) != INTEGER_CST
315 && value != error_mark_node)
316 {
317 error ("case label does not reduce to an integer constant");
318 value = error_mark_node;
319 }
320 else
321 /* Promote char or short to int. */
322 value = default_conversion (value);
323
324 return value;
325 }
326 \f
327 /* Return an integer type with BITS bits of precision,
328 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
329
330 tree
331 type_for_size (bits, unsignedp)
332 unsigned bits;
333 int unsignedp;
334 {
335 if (bits == TYPE_PRECISION (signed_char_type_node))
336 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
337
338 if (bits == TYPE_PRECISION (short_integer_type_node))
339 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
340
341 if (bits == TYPE_PRECISION (integer_type_node))
342 return unsignedp ? unsigned_type_node : integer_type_node;
343
344 if (bits == TYPE_PRECISION (long_integer_type_node))
345 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
346
347 if (bits == TYPE_PRECISION (long_long_integer_type_node))
348 return (unsignedp ? long_long_unsigned_type_node
349 : long_long_integer_type_node);
350
351 if (bits <= TYPE_PRECISION (intQI_type_node))
352 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
353
354 if (bits <= TYPE_PRECISION (intHI_type_node))
355 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
356
357 if (bits <= TYPE_PRECISION (intSI_type_node))
358 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
359
360 if (bits <= TYPE_PRECISION (intDI_type_node))
361 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
362
363 return 0;
364 }
365
366 /* Return a data type that has machine mode MODE.
367 If the mode is an integer,
368 then UNSIGNEDP selects between signed and unsigned types. */
369
370 tree
371 type_for_mode (mode, unsignedp)
372 enum machine_mode mode;
373 int unsignedp;
374 {
375 if (mode == TYPE_MODE (signed_char_type_node))
376 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
377
378 if (mode == TYPE_MODE (short_integer_type_node))
379 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
380
381 if (mode == TYPE_MODE (integer_type_node))
382 return unsignedp ? unsigned_type_node : integer_type_node;
383
384 if (mode == TYPE_MODE (long_integer_type_node))
385 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
386
387 if (mode == TYPE_MODE (long_long_integer_type_node))
388 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
389
390 if (mode == TYPE_MODE (intQI_type_node))
391 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
392
393 if (mode == TYPE_MODE (intHI_type_node))
394 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
395
396 if (mode == TYPE_MODE (intSI_type_node))
397 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
398
399 if (mode == TYPE_MODE (intDI_type_node))
400 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
401
402 if (mode == TYPE_MODE (float_type_node))
403 return float_type_node;
404
405 if (mode == TYPE_MODE (double_type_node))
406 return double_type_node;
407
408 if (mode == TYPE_MODE (long_double_type_node))
409 return long_double_type_node;
410
411 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
412 return build_pointer_type (char_type_node);
413
414 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
415 return build_pointer_type (integer_type_node);
416
417 return 0;
418 }
419 \f
420 /* Print an error message for invalid operands to arith operation CODE.
421 NOP_EXPR is used as a special case (see truthvalue_conversion). */
422
423 void
424 binary_op_error (code)
425 enum tree_code code;
426 {
427 register char *opname;
428 switch (code)
429 {
430 case NOP_EXPR:
431 error ("invalid truth-value expression");
432 return;
433
434 case PLUS_EXPR:
435 opname = "+"; break;
436 case MINUS_EXPR:
437 opname = "-"; break;
438 case MULT_EXPR:
439 opname = "*"; break;
440 case MAX_EXPR:
441 opname = "max"; break;
442 case MIN_EXPR:
443 opname = "min"; break;
444 case EQ_EXPR:
445 opname = "=="; break;
446 case NE_EXPR:
447 opname = "!="; break;
448 case LE_EXPR:
449 opname = "<="; break;
450 case GE_EXPR:
451 opname = ">="; break;
452 case LT_EXPR:
453 opname = "<"; break;
454 case GT_EXPR:
455 opname = ">"; break;
456 case LSHIFT_EXPR:
457 opname = "<<"; break;
458 case RSHIFT_EXPR:
459 opname = ">>"; break;
460 case TRUNC_MOD_EXPR:
461 case FLOOR_MOD_EXPR:
462 opname = "%"; break;
463 case TRUNC_DIV_EXPR:
464 case FLOOR_DIV_EXPR:
465 opname = "/"; break;
466 case BIT_AND_EXPR:
467 opname = "&"; break;
468 case BIT_IOR_EXPR:
469 opname = "|"; break;
470 case TRUTH_ANDIF_EXPR:
471 opname = "&&"; break;
472 case TRUTH_ORIF_EXPR:
473 opname = "||"; break;
474 case BIT_XOR_EXPR:
475 opname = "^"; break;
476 case LROTATE_EXPR:
477 case RROTATE_EXPR:
478 opname = "rotate"; break;
479 }
480 error ("invalid operands to binary %s", opname);
481 }
482 \f
483 /* Subroutine of build_binary_op, used for comparison operations.
484 See if the operands have both been converted from subword integer types
485 and, if so, perhaps change them both back to their original type.
486
487 The arguments of this function are all pointers to local variables
488 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
489 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
490
491 If this function returns nonzero, it means that the comparison has
492 a constant value. What this function returns is an expression for
493 that value. */
494
495 tree
496 shorten_compare (op0_ptr, op1_ptr, restype_ptr, rescode_ptr)
497 tree *op0_ptr, *op1_ptr;
498 tree *restype_ptr;
499 enum tree_code *rescode_ptr;
500 {
501 register tree type;
502 tree op0 = *op0_ptr;
503 tree op1 = *op1_ptr;
504 int unsignedp0, unsignedp1;
505 int real1, real2;
506 tree primop0, primop1;
507 enum tree_code code = *rescode_ptr;
508
509 /* Throw away any conversions to wider types
510 already present in the operands. */
511
512 primop0 = get_narrower (op0, &unsignedp0);
513 primop1 = get_narrower (op1, &unsignedp1);
514
515 /* Handle the case that OP0 does not *contain* a conversion
516 but it *requires* conversion to FINAL_TYPE. */
517
518 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
519 unsignedp0 = TREE_UNSIGNED (TREE_TYPE (op0));
520 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
521 unsignedp1 = TREE_UNSIGNED (TREE_TYPE (op1));
522
523 /* If one of the operands must be floated, we cannot optimize. */
524 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
525 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
526
527 /* If first arg is constant, swap the args (changing operation
528 so value is preserved), for canonicalization. */
529
530 if (TREE_CONSTANT (primop0))
531 {
532 register tree tem = primop0;
533 register int temi = unsignedp0;
534 primop0 = primop1;
535 primop1 = tem;
536 tem = op0;
537 op0 = op1;
538 op1 = tem;
539 *op0_ptr = op0;
540 *op1_ptr = op1;
541 unsignedp0 = unsignedp1;
542 unsignedp1 = temi;
543 temi = real1;
544 real1 = real2;
545 real2 = temi;
546
547 switch (code)
548 {
549 case LT_EXPR:
550 code = GT_EXPR;
551 break;
552 case GT_EXPR:
553 code = LT_EXPR;
554 break;
555 case LE_EXPR:
556 code = GE_EXPR;
557 break;
558 case GE_EXPR:
559 code = LE_EXPR;
560 break;
561 }
562 *rescode_ptr = code;
563 }
564
565 /* If comparing an integer against a constant more bits wide,
566 maybe we can deduce a value of 1 or 0 independent of the data.
567 Or else truncate the constant now
568 rather than extend the variable at run time.
569
570 This is only interesting if the constant is the wider arg.
571 Also, it is not safe if the constant is unsigned and the
572 variable arg is signed, since in this case the variable
573 would be sign-extended and then regarded as unsigned.
574 Our technique fails in this case because the lowest/highest
575 possible unsigned results don't follow naturally from the
576 lowest/highest possible values of the variable operand.
577 For just EQ_EXPR and NE_EXPR there is another technique that
578 could be used: see if the constant can be faithfully represented
579 in the other operand's type, by truncating it and reextending it
580 and see if that preserves the constant's value. */
581
582 if (!real1 && !real2
583 && TREE_CODE (primop1) == INTEGER_CST
584 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
585 {
586 int min_gt, max_gt, min_lt, max_lt;
587 tree maxval, minval;
588 /* 1 if comparison is nominally unsigned. */
589 int unsignedp = TREE_UNSIGNED (*restype_ptr);
590 tree val;
591
592 type = signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0));
593
594 maxval = TYPE_MAX_VALUE (type);
595 minval = TYPE_MIN_VALUE (type);
596
597 if (unsignedp && !unsignedp0)
598 *restype_ptr = signed_type (*restype_ptr);
599
600 if (TREE_TYPE (primop1) != *restype_ptr)
601 primop1 = convert (*restype_ptr, primop1);
602 if (type != *restype_ptr)
603 {
604 minval = convert (*restype_ptr, minval);
605 maxval = convert (*restype_ptr, maxval);
606 }
607
608 if (unsignedp && unsignedp0)
609 {
610 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
611 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
612 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
613 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
614 }
615 else
616 {
617 min_gt = INT_CST_LT (primop1, minval);
618 max_gt = INT_CST_LT (primop1, maxval);
619 min_lt = INT_CST_LT (minval, primop1);
620 max_lt = INT_CST_LT (maxval, primop1);
621 }
622
623 val = 0;
624 /* This used to be a switch, but Genix compiler can't handle that. */
625 if (code == NE_EXPR)
626 {
627 if (max_lt || min_gt)
628 val = integer_one_node;
629 }
630 else if (code == EQ_EXPR)
631 {
632 if (max_lt || min_gt)
633 val = integer_zero_node;
634 }
635 else if (code == LT_EXPR)
636 {
637 if (max_lt)
638 val = integer_one_node;
639 if (!min_lt)
640 val = integer_zero_node;
641 }
642 else if (code == GT_EXPR)
643 {
644 if (min_gt)
645 val = integer_one_node;
646 if (!max_gt)
647 val = integer_zero_node;
648 }
649 else if (code == LE_EXPR)
650 {
651 if (!max_gt)
652 val = integer_one_node;
653 if (min_gt)
654 val = integer_zero_node;
655 }
656 else if (code == GE_EXPR)
657 {
658 if (!min_lt)
659 val = integer_one_node;
660 if (max_lt)
661 val = integer_zero_node;
662 }
663
664 /* If primop0 was sign-extended and unsigned comparison specd,
665 we did a signed comparison above using the signed type bounds.
666 But the comparison we output must be unsigned.
667
668 Also, for inequalities, VAL is no good; but if the signed
669 comparison had *any* fixed result, it follows that the
670 unsigned comparison just tests the sign in reverse
671 (positive values are LE, negative ones GE).
672 So we can generate an unsigned comparison
673 against an extreme value of the signed type. */
674
675 if (unsignedp && !unsignedp0)
676 {
677 if (val != 0)
678 switch (code)
679 {
680 case LT_EXPR:
681 case GE_EXPR:
682 primop1 = TYPE_MIN_VALUE (type);
683 val = 0;
684 break;
685
686 case LE_EXPR:
687 case GT_EXPR:
688 primop1 = TYPE_MAX_VALUE (type);
689 val = 0;
690 break;
691 }
692 type = unsigned_type (type);
693 }
694
695 if (!max_gt && !unsignedp0)
696 {
697 /* This is the case of (char)x >?< 0x80, which people used to use
698 expecting old C compilers to change the 0x80 into -0x80. */
699 if (val == integer_zero_node)
700 warning ("comparison is always 0 due to limited range of data type");
701 if (val == integer_one_node)
702 warning ("comparison is always 1 due to limited range of data type");
703 }
704
705 if (!min_lt && unsignedp0)
706 {
707 /* This is the case of (unsigned char)x >?< -1 or < 0. */
708 if (val == integer_zero_node)
709 warning ("comparison is always 0 due to limited range of data type");
710 if (val == integer_one_node)
711 warning ("comparison is always 1 due to limited range of data type");
712 }
713
714 if (val != 0)
715 {
716 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
717 if (TREE_SIDE_EFFECTS (primop0))
718 return build (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
719 return val;
720 }
721
722 /* Value is not predetermined, but do the comparison
723 in the type of the operand that is not constant.
724 TYPE is already properly set. */
725 }
726 else if (real1 && real2
727 && TYPE_PRECISION (TREE_TYPE (primop0)) == TYPE_PRECISION (TREE_TYPE (primop1)))
728 type = TREE_TYPE (primop0);
729
730 /* If args' natural types are both narrower than nominal type
731 and both extend in the same manner, compare them
732 in the type of the wider arg.
733 Otherwise must actually extend both to the nominal
734 common type lest different ways of extending
735 alter the result.
736 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
737
738 else if (unsignedp0 == unsignedp1 && real1 == real2
739 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
740 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
741 {
742 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
743 type = signed_or_unsigned_type (unsignedp0
744 || TREE_UNSIGNED (*restype_ptr),
745 type);
746 /* Make sure shorter operand is extended the right way
747 to match the longer operand. */
748 primop0 = convert (signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0)),
749 primop0);
750 primop1 = convert (signed_or_unsigned_type (unsignedp1, TREE_TYPE (primop1)),
751 primop1);
752 }
753 else
754 {
755 /* Here we must do the comparison on the nominal type
756 using the args exactly as we received them. */
757 type = *restype_ptr;
758 primop0 = op0;
759 primop1 = op1;
760
761 if (!real1 && !real2 && integer_zerop (primop1)
762 && TREE_UNSIGNED (TREE_TYPE (primop0)))
763 {
764 tree value = 0;
765 switch (code)
766 {
767 case GE_EXPR:
768 if (extra_warnings)
769 warning ("unsigned value >= 0 is always 1");
770 value = integer_one_node;
771 break;
772
773 case LT_EXPR:
774 if (extra_warnings)
775 warning ("unsigned value < 0 is always 0");
776 value = integer_zero_node;
777 }
778
779 if (value != 0)
780 {
781 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
782 if (TREE_SIDE_EFFECTS (primop0))
783 return build (COMPOUND_EXPR, TREE_TYPE (value),
784 primop0, value);
785 return value;
786 }
787 }
788 }
789
790 *op0_ptr = convert (type, primop0);
791 *op1_ptr = convert (type, primop1);
792
793 *restype_ptr = integer_type_node;
794
795 return 0;
796 }
797 \f
798 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
799 or validate its data type for an `if' or `while' statement or ?..: exp.
800
801 This preparation consists of taking the ordinary
802 representation of an expression expr and producing a valid tree
803 boolean expression describing whether expr is nonzero. We could
804 simply always do build_binary_op (NE_EXPR, expr, integer_zero_node, 1),
805 but we optimize comparisons, &&, ||, and !.
806
807 The resulting type should always be `integer_type_node'. */
808
809 tree
810 truthvalue_conversion (expr)
811 tree expr;
812 {
813 register enum tree_code code;
814
815 switch (TREE_CODE (expr))
816 {
817 /* It is simpler and generates better code to have only TRUTH_*_EXPR
818 or comparison expressions as truth values at this level. */
819 #if 0
820 case COMPONENT_REF:
821 /* A one-bit unsigned bit-field is already acceptable. */
822 if (1 == TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (expr, 1)))
823 && TREE_UNSIGNED (TREE_OPERAND (expr, 1)))
824 return expr;
825 break;
826 #endif
827
828 case EQ_EXPR:
829 /* It is simpler and generates better code to have only TRUTH_*_EXPR
830 or comparison expressions as truth values at this level. */
831 #if 0
832 if (integer_zerop (TREE_OPERAND (expr, 1)))
833 return build_unary_op (TRUTH_NOT_EXPR, TREE_OPERAND (expr, 0), 0);
834 #endif
835 case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
836 case TRUTH_ANDIF_EXPR:
837 case TRUTH_ORIF_EXPR:
838 case TRUTH_AND_EXPR:
839 case TRUTH_OR_EXPR:
840 case ERROR_MARK:
841 return expr;
842
843 case INTEGER_CST:
844 return integer_zerop (expr) ? integer_zero_node : integer_one_node;
845
846 case REAL_CST:
847 return real_zerop (expr) ? integer_zero_node : integer_one_node;
848
849 case ADDR_EXPR:
850 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0)))
851 return build (COMPOUND_EXPR, integer_type_node,
852 TREE_OPERAND (expr, 0), integer_one_node);
853 else
854 return integer_one_node;
855
856 case NEGATE_EXPR:
857 case ABS_EXPR:
858 case FLOAT_EXPR:
859 case FFS_EXPR:
860 /* These don't change whether an object is non-zero or zero. */
861 return truthvalue_conversion (TREE_OPERAND (expr, 0));
862
863 case LROTATE_EXPR:
864 case RROTATE_EXPR:
865 /* These don't change whether an object is zero or non-zero, but
866 we can't ignore them if their second arg has side-effects. */
867 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
868 return build (COMPOUND_EXPR, integer_type_node, TREE_OPERAND (expr, 1),
869 truthvalue_conversion (TREE_OPERAND (expr, 0)));
870 else
871 return truthvalue_conversion (TREE_OPERAND (expr, 0));
872
873 case COND_EXPR:
874 /* Distribute the conversion into the arms of a COND_EXPR. */
875 return fold (build (COND_EXPR, integer_type_node, TREE_OPERAND (expr, 0),
876 truthvalue_conversion (TREE_OPERAND (expr, 1)),
877 truthvalue_conversion (TREE_OPERAND (expr, 2))));
878
879 case CONVERT_EXPR:
880 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
881 since that affects how `default_conversion' will behave. */
882 if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE
883 || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
884 break;
885 /* fall through... */
886 case NOP_EXPR:
887 /* If this is widening the argument, we can ignore it. */
888 if (TYPE_PRECISION (TREE_TYPE (expr))
889 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
890 return truthvalue_conversion (TREE_OPERAND (expr, 0));
891 break;
892
893 case BIT_XOR_EXPR:
894 case MINUS_EXPR:
895 /* These can be changed into a comparison of the two objects. */
896 if (TREE_TYPE (TREE_OPERAND (expr, 0))
897 == TREE_TYPE (TREE_OPERAND (expr, 1)))
898 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
899 TREE_OPERAND (expr, 1), 1);
900 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
901 fold (build1 (NOP_EXPR,
902 TREE_TYPE (TREE_OPERAND (expr, 0)),
903 TREE_OPERAND (expr, 1))), 1);
904
905 case MODIFY_EXPR:
906 if (warn_parentheses && C_EXP_ORIGINAL_CODE (expr) == MODIFY_EXPR)
907 warning ("suggest parentheses around assignment used as truth value");
908 break;
909 }
910
911 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
912 }
913 \f
914 /* Read the rest of a #-directive from input stream FINPUT.
915 In normal use, the directive name and the white space after it
916 have already been read, so they won't be included in the result.
917 We allow for the fact that the directive line may contain
918 a newline embedded within a character or string literal which forms
919 a part of the directive.
920
921 The value is a string in a reusable buffer. It remains valid
922 only until the next time this function is called. */
923
924 char *
925 get_directive_line (finput)
926 register FILE *finput;
927 {
928 static char *directive_buffer = NULL;
929 static unsigned buffer_length = 0;
930 register char *p;
931 register char *buffer_limit;
932 register int looking_for = 0;
933 register int char_escaped = 0;
934
935 if (buffer_length == 0)
936 {
937 directive_buffer = (char *)xmalloc (128);
938 buffer_length = 128;
939 }
940
941 buffer_limit = &directive_buffer[buffer_length];
942
943 for (p = directive_buffer; ; )
944 {
945 int c;
946
947 /* Make buffer bigger if it is full. */
948 if (p >= buffer_limit)
949 {
950 register unsigned bytes_used = (p - directive_buffer);
951
952 buffer_length *= 2;
953 directive_buffer
954 = (char *)xrealloc (directive_buffer, buffer_length);
955 p = &directive_buffer[bytes_used];
956 buffer_limit = &directive_buffer[buffer_length];
957 }
958
959 c = getc (finput);
960
961 /* Discard initial whitespace. */
962 if ((c == ' ' || c == '\t') && p == directive_buffer)
963 continue;
964
965 /* Detect the end of the directive. */
966 if (c == '\n' && looking_for == 0)
967 {
968 ungetc (c, finput);
969 c = '\0';
970 }
971
972 *p++ = c;
973
974 if (c == 0)
975 return directive_buffer;
976
977 /* Handle string and character constant syntax. */
978 if (looking_for)
979 {
980 if (looking_for == c && !char_escaped)
981 looking_for = 0; /* Found terminator... stop looking. */
982 }
983 else
984 if (c == '\'' || c == '"')
985 looking_for = c; /* Don't stop buffering until we see another
986 another one of these (or an EOF). */
987
988 /* Handle backslash. */
989 char_escaped = (c == '\\' && ! char_escaped);
990 }
991 }
This page took 0.113482 seconds and 5 git commands to generate.