OSDN Git Service

fae1e44550b000c8a072dbef6f76cedcab2cff11
[pf3gnuchains/gcc-fork.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 "obstack.h"
26 #include <stdio.h>
27
28 extern struct obstack permanent_obstack;
29
30 /* Make bindings for __FUNCTION__ and __PRETTY_FUNCTION__.  */
31
32 void
33 declare_function_name ()
34 {
35   tree decl, type, init;
36   char *name, *printable_name;
37   int len;
38
39   if (current_function_decl == NULL)
40     {
41       name = "";
42       printable_name = "top level";
43     }
44   else
45     {
46       char *kind = "function";
47       if (TREE_CODE (TREE_TYPE (current_function_decl)) == METHOD_TYPE)
48         kind = "method";
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));
52       else
53         name = "";
54       printable_name = (*decl_printable_name) (current_function_decl, &kind);
55     }
56
57   /* If the default size of char arrays isn't big enough for the name,
58      make a bigger one.  */
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)))
62       < len)
63     type = build_array_type (char_type_node,
64                              build_index_type (build_int_2 (len, 0)));
65
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);
77
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)))
81       < len)
82     type = build_array_type (char_type_node,
83                              build_index_type (build_int_2 (len, 0)));
84
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);
96 }
97
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.  */
101
102 tree
103 combine_strings (strings)
104      tree strings;
105 {
106   register tree value, t;
107   register int length = 1;
108   int wide_length = 0;
109   int wide_flag = 0;
110   int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
111   int nchars;
112
113   if (TREE_CHAIN (strings))
114     {
115       /* More than one in the chain, so concatenate.  */
116       register char *p, *q;
117
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))
122         {
123           if (TREE_TYPE (t) == wchar_array_type_node)
124             {
125               wide_length += (TREE_STRING_LENGTH (t) - wchar_bytes);
126               wide_flag = 1;
127             }
128           else
129             length += (TREE_STRING_LENGTH (t) - 1);
130         }
131
132       /* If anything is wide, the non-wides will be converted,
133          which makes them take more space.  */
134       if (wide_flag)
135         length = length * wchar_bytes + wide_length;
136
137       p = savealloc (length);
138
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.  */
142
143       q = p;
144       for (t = strings; t; t = TREE_CHAIN (t))
145         {
146           int len = (TREE_STRING_LENGTH (t)
147                      - ((TREE_TYPE (t) == wchar_array_type_node)
148                         ? wchar_bytes : 1));
149           if ((TREE_TYPE (t) == wchar_array_type_node) == wide_flag)
150             {
151               bcopy (TREE_STRING_POINTER (t), q, len);
152               q += len;
153             }
154           else
155             {
156               int i;
157               for (i = 0; i < len; i++)
158                 ((int *) q)[i] = TREE_STRING_POINTER (t)[i];
159               q += len * wchar_bytes;
160             }
161         }
162       if (wide_flag)
163         {
164           int i;
165           for (i = 0; i < wchar_bytes; i++)
166             *q++ = 0;
167         }
168       else
169         *q = 0;
170
171       value = make_node (STRING_CST);
172       TREE_STRING_POINTER (value) = p;
173       TREE_STRING_LENGTH (value) = length;
174       TREE_CONSTANT (value) = 1;
175     }
176   else
177     {
178       value = strings;
179       length = TREE_STRING_LENGTH (value);
180       if (TREE_TYPE (value) == wchar_array_type_node)
181         wide_flag = 1;
182     }
183
184   /* Compute the number of elements, for the array type.  */ 
185   nchars = wide_flag ? length / wchar_bytes : length;
186
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))
192     {
193       tree elements
194         = build_type_variant (wide_flag ? wchar_type_node : char_type_node,
195                               1, 0);
196       TREE_TYPE (value)
197         = build_array_type (elements,
198                             build_index_type (build_int_2 (nchars - 1, 0)));
199     }
200   else
201     TREE_TYPE (value)
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;
206   return value;
207 }
208 \f
209 /* Process the attributes listed in ATTRIBUTES
210    and install them in DECL.  */
211
212 void
213 decl_attributes (decl, attributes)
214      tree decl, attributes;
215 {
216   tree a;
217   for (a = attributes; a; a = TREE_CHAIN (a))
218     if (TREE_VALUE (a) == get_identifier ("packed"))
219       {
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.  */
224       }
225     else if (TREE_VALUE (a) != 0
226              && TREE_CODE (TREE_VALUE (a)) == TREE_LIST
227              && TREE_PURPOSE (TREE_VALUE (a)) == get_identifier ("mode"))
228       {
229         int i;
230         char *specified_name
231           = IDENTIFIER_POINTER (TREE_VALUE (TREE_VALUE (a)));
232
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)))
236             {
237               tree type
238                 = type_for_mode (i, TREE_UNSIGNED (TREE_TYPE (decl)));
239               if (type != 0)
240                 {
241                   TREE_TYPE (decl) = type;
242                   DECL_SIZE (decl) = 0;
243                   layout_decl (decl, 0);
244                 }
245               else
246                 error ("no data type for mode `%s'", specified_name);
247               break;
248             }
249         if (i == NUM_MACHINE_MODES)
250           error ("unknown machine mode `%s'", specified_name);
251       }
252     else if (TREE_VALUE (a) != 0
253              && TREE_CODE (TREE_VALUE (a)) == TREE_LIST
254              && TREE_PURPOSE (TREE_VALUE (a)) == get_identifier ("aligned"))
255       {
256         int align = TREE_INT_CST_LOW (TREE_VALUE (TREE_VALUE (a)))
257                     * BITS_PER_UNIT;
258         
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'");
266         else
267           DECL_ALIGN (decl) = align;
268       }
269     else if (TREE_VALUE (a) != 0
270              && TREE_CODE (TREE_VALUE (a)) == TREE_LIST
271              && TREE_PURPOSE (TREE_VALUE (a)) == get_identifier ("format"))
272       {
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)));
277         int is_scan;
278         tree argument;
279         int arg_num;
280         
281         if (TREE_CODE (decl) != FUNCTION_DECL)
282           {
283             error_with_decl (decl,
284                              "argument format specified for non-function `%s'");
285             return;
286           }
287         
288         if (format_type == get_identifier ("printf"))
289           is_scan = 0;
290         else if (format_type == get_identifier ("scanf"))
291           is_scan = 1;
292         else
293           {
294             error_with_decl (decl, "unrecognized format specifier for `%s'");
295             return;
296           }
297         
298         if (first_arg_num != 0 && first_arg_num <= format_num)
299           {
300             error_with_decl (decl,
301                 "format string arg follows the args to be formatted, for `%s'");
302             return;
303           }
304
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)
309           {
310             if (argument == 0 || arg_num == format_num)
311               break;
312             argument = TREE_CHAIN (argument);
313           }
314         if (! argument
315             || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
316             || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
317                 != char_type_node))
318           {
319             error_with_decl (decl,
320                              "format string arg not a string type, for `%s'");
321             return;
322           }
323         if (first_arg_num != 0)
324           {
325             /* Verify that first_arg_num points to the last arg, the ... */
326             while (argument)
327               arg_num++, argument = TREE_CHAIN (argument);
328             if (arg_num != first_arg_num)
329               {
330                 error_with_decl (decl,
331                                  "args to be formatted is not ..., for `%s'");
332                 return;
333               }
334           }
335         
336         record_format_info (DECL_NAME (decl), is_scan, format_num,
337                             first_arg_num);
338       }
339 }
340 \f
341 /* Print a warning if a constant expression had overflow in folding.
342    Invoke this function on every expression that the language
343    requires to be a constant expression.
344    Note the ANSI C standard says it is erroneous for a
345    constant expression to overflow.  */
346
347 void
348 constant_expression_warning (value)
349      tree value;
350 {
351   if (TREE_CODE (value) == INTEGER_CST && TREE_CONSTANT_OVERFLOW (value))
352     if (pedantic)
353       pedwarn ("overflow in constant expression");
354 }
355
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.  */
362
363 void
364 overflow_warning (value)
365      tree value;
366 {
367   if (TREE_CODE (value) == INTEGER_CST && TREE_OVERFLOW (value))
368     {
369       TREE_OVERFLOW (value) = 0;
370       warning ("integer overflow in expression");
371     }
372 }
373
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.  */
378
379 void
380 unsigned_conversion_warning (result, operand)
381      tree result, operand;
382 {
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)))
387     {
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         warning ("large integer implicitly truncated to unsigned type");
391       else if (warn_conversion)
392         warning ("negative integer implicitly converted to unsigned type");
393     }
394 }
395
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.  */
399
400 tree
401 convert_and_check (type, expr)
402      tree type, expr;
403 {
404   tree t = convert (type, expr);
405   if (TREE_CODE (t) == INTEGER_CST)
406     {
407       if (TREE_OVERFLOW (t))
408         {
409           TREE_OVERFLOW (t) = 0;
410
411           /* No warning for converting 0x80000000 to int.  */
412           if (!(TREE_UNSIGNED (type) < TREE_UNSIGNED (TREE_TYPE (expr))
413                 && TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
414                 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (expr))))
415             warning ("overflow in implicit constant conversion");
416         }
417       else
418         unsigned_conversion_warning (t, expr);
419     }
420   return t;
421 }
422 \f
423 void
424 c_expand_expr_stmt (expr)
425      tree expr;
426 {
427   /* Do default conversion if safe and possibly important,
428      in case within ({...}).  */
429   if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE && lvalue_p (expr))
430       || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
431     expr = default_conversion (expr);
432
433   if (TREE_TYPE (expr) != error_mark_node
434       && TYPE_SIZE (TREE_TYPE (expr)) == 0
435       && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
436     error ("expression statement has incomplete type");
437
438   expand_expr_stmt (expr);
439 }
440 \f
441 /* Validate the expression after `case' and apply default promotions.  */
442
443 tree
444 check_case_value (value)
445      tree value;
446 {
447   if (value == NULL_TREE)
448     return value;
449
450   /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue.  */
451   STRIP_TYPE_NOPS (value);
452
453   if (TREE_CODE (value) != INTEGER_CST
454       && value != error_mark_node)
455     {
456       error ("case label does not reduce to an integer constant");
457       value = error_mark_node;
458     }
459   else
460     /* Promote char or short to int.  */
461     value = default_conversion (value);
462
463   constant_expression_warning (value);
464
465   return value;
466 }
467 \f
468 /* Return an integer type with BITS bits of precision,
469    that is unsigned if UNSIGNEDP is nonzero, otherwise signed.  */
470
471 tree
472 type_for_size (bits, unsignedp)
473      unsigned bits;
474      int unsignedp;
475 {
476   if (bits == TYPE_PRECISION (signed_char_type_node))
477     return unsignedp ? unsigned_char_type_node : signed_char_type_node;
478
479   if (bits == TYPE_PRECISION (short_integer_type_node))
480     return unsignedp ? short_unsigned_type_node : short_integer_type_node;
481
482   if (bits == TYPE_PRECISION (integer_type_node))
483     return unsignedp ? unsigned_type_node : integer_type_node;
484
485   if (bits == TYPE_PRECISION (long_integer_type_node))
486     return unsignedp ? long_unsigned_type_node : long_integer_type_node;
487
488   if (bits == TYPE_PRECISION (long_long_integer_type_node))
489     return (unsignedp ? long_long_unsigned_type_node
490             : long_long_integer_type_node);
491
492   if (bits <= TYPE_PRECISION (intQI_type_node))
493     return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
494
495   if (bits <= TYPE_PRECISION (intHI_type_node))
496     return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
497
498   if (bits <= TYPE_PRECISION (intSI_type_node))
499     return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
500
501   if (bits <= TYPE_PRECISION (intDI_type_node))
502     return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
503
504   return 0;
505 }
506
507 /* Return a data type that has machine mode MODE.
508    If the mode is an integer,
509    then UNSIGNEDP selects between signed and unsigned types.  */
510
511 tree
512 type_for_mode (mode, unsignedp)
513      enum machine_mode mode;
514      int unsignedp;
515 {
516   if (mode == TYPE_MODE (signed_char_type_node))
517     return unsignedp ? unsigned_char_type_node : signed_char_type_node;
518
519   if (mode == TYPE_MODE (short_integer_type_node))
520     return unsignedp ? short_unsigned_type_node : short_integer_type_node;
521
522   if (mode == TYPE_MODE (integer_type_node))
523     return unsignedp ? unsigned_type_node : integer_type_node;
524
525   if (mode == TYPE_MODE (long_integer_type_node))
526     return unsignedp ? long_unsigned_type_node : long_integer_type_node;
527
528   if (mode == TYPE_MODE (long_long_integer_type_node))
529     return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
530
531   if (mode == TYPE_MODE (intQI_type_node))
532     return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
533
534   if (mode == TYPE_MODE (intHI_type_node))
535     return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
536
537   if (mode == TYPE_MODE (intSI_type_node))
538     return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
539
540   if (mode == TYPE_MODE (intDI_type_node))
541     return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
542
543   if (mode == TYPE_MODE (float_type_node))
544     return float_type_node;
545
546   if (mode == TYPE_MODE (double_type_node))
547     return double_type_node;
548
549   if (mode == TYPE_MODE (long_double_type_node))
550     return long_double_type_node;
551
552   if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
553     return build_pointer_type (char_type_node);
554
555   if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
556     return build_pointer_type (integer_type_node);
557
558   return 0;
559 }
560 \f
561 /* Print an error message for invalid operands to arith operation CODE.
562    NOP_EXPR is used as a special case (see truthvalue_conversion).  */
563
564 void
565 binary_op_error (code)
566      enum tree_code code;
567 {
568   register char *opname;
569   switch (code)
570     {
571     case NOP_EXPR:
572       error ("invalid truth-value expression");
573       return;
574
575     case PLUS_EXPR:
576       opname = "+"; break;
577     case MINUS_EXPR:
578       opname = "-"; break;
579     case MULT_EXPR:
580       opname = "*"; break;
581     case MAX_EXPR:
582       opname = "max"; break;
583     case MIN_EXPR:
584       opname = "min"; break;
585     case EQ_EXPR:
586       opname = "=="; break;
587     case NE_EXPR:
588       opname = "!="; break;
589     case LE_EXPR:
590       opname = "<="; break;
591     case GE_EXPR:
592       opname = ">="; break;
593     case LT_EXPR:
594       opname = "<"; break;
595     case GT_EXPR:
596       opname = ">"; break;
597     case LSHIFT_EXPR:
598       opname = "<<"; break;
599     case RSHIFT_EXPR:
600       opname = ">>"; break;
601     case TRUNC_MOD_EXPR:
602     case FLOOR_MOD_EXPR:
603       opname = "%"; break;
604     case TRUNC_DIV_EXPR:
605     case FLOOR_DIV_EXPR:
606       opname = "/"; break;
607     case BIT_AND_EXPR:
608       opname = "&"; break;
609     case BIT_IOR_EXPR:
610       opname = "|"; break;
611     case TRUTH_ANDIF_EXPR:
612       opname = "&&"; break;
613     case TRUTH_ORIF_EXPR:
614       opname = "||"; break;
615     case BIT_XOR_EXPR:
616       opname = "^"; break;
617     case LROTATE_EXPR:
618     case RROTATE_EXPR:
619       opname = "rotate"; break;
620     }
621   error ("invalid operands to binary %s", opname);
622 }
623 \f
624 /* Subroutine of build_binary_op, used for comparison operations.
625    See if the operands have both been converted from subword integer types
626    and, if so, perhaps change them both back to their original type.
627
628    The arguments of this function are all pointers to local variables
629    of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
630    RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
631
632    If this function returns nonzero, it means that the comparison has
633    a constant value.  What this function returns is an expression for
634    that value.  */
635
636 tree
637 shorten_compare (op0_ptr, op1_ptr, restype_ptr, rescode_ptr)
638      tree *op0_ptr, *op1_ptr;
639      tree *restype_ptr;
640      enum tree_code *rescode_ptr;
641 {
642   register tree type;
643   tree op0 = *op0_ptr;
644   tree op1 = *op1_ptr;
645   int unsignedp0, unsignedp1;
646   int real1, real2;
647   tree primop0, primop1;
648   enum tree_code code = *rescode_ptr;
649
650   /* Throw away any conversions to wider types
651      already present in the operands.  */
652
653   primop0 = get_narrower (op0, &unsignedp0);
654   primop1 = get_narrower (op1, &unsignedp1);
655
656   /* Handle the case that OP0 does not *contain* a conversion
657      but it *requires* conversion to FINAL_TYPE.  */
658
659   if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
660     unsignedp0 = TREE_UNSIGNED (TREE_TYPE (op0));
661   if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
662     unsignedp1 = TREE_UNSIGNED (TREE_TYPE (op1));
663
664   /* If one of the operands must be floated, we cannot optimize.  */
665   real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
666   real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
667
668   /* If first arg is constant, swap the args (changing operation
669      so value is preserved), for canonicalization.  */
670
671   if (TREE_CONSTANT (primop0))
672     {
673       register tree tem = primop0;
674       register int temi = unsignedp0;
675       primop0 = primop1;
676       primop1 = tem;
677       tem = op0;
678       op0 = op1;
679       op1 = tem;
680       *op0_ptr = op0;
681       *op1_ptr = op1;
682       unsignedp0 = unsignedp1;
683       unsignedp1 = temi;
684       temi = real1;
685       real1 = real2;
686       real2 = temi;
687
688       switch (code)
689         {
690         case LT_EXPR:
691           code = GT_EXPR;
692           break;
693         case GT_EXPR:
694           code = LT_EXPR;
695           break;
696         case LE_EXPR:
697           code = GE_EXPR;
698           break;
699         case GE_EXPR:
700           code = LE_EXPR;
701           break;
702         }
703       *rescode_ptr = code;
704     }
705
706   /* If comparing an integer against a constant more bits wide,
707      maybe we can deduce a value of 1 or 0 independent of the data.
708      Or else truncate the constant now
709      rather than extend the variable at run time.
710
711      This is only interesting if the constant is the wider arg.
712      Also, it is not safe if the constant is unsigned and the
713      variable arg is signed, since in this case the variable
714      would be sign-extended and then regarded as unsigned.
715      Our technique fails in this case because the lowest/highest
716      possible unsigned results don't follow naturally from the
717      lowest/highest possible values of the variable operand.
718      For just EQ_EXPR and NE_EXPR there is another technique that
719      could be used: see if the constant can be faithfully represented
720      in the other operand's type, by truncating it and reextending it
721      and see if that preserves the constant's value.  */
722
723   if (!real1 && !real2
724       && TREE_CODE (primop1) == INTEGER_CST
725       && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
726     {
727       int min_gt, max_gt, min_lt, max_lt;
728       tree maxval, minval;
729       /* 1 if comparison is nominally unsigned.  */
730       int unsignedp = TREE_UNSIGNED (*restype_ptr);
731       tree val;
732
733       type = signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0));
734
735       maxval = TYPE_MAX_VALUE (type);
736       minval = TYPE_MIN_VALUE (type);
737
738       if (unsignedp && !unsignedp0)
739         *restype_ptr = signed_type (*restype_ptr);
740
741       if (TREE_TYPE (primop1) != *restype_ptr)
742         primop1 = convert (*restype_ptr, primop1);
743       if (type != *restype_ptr)
744         {
745           minval = convert (*restype_ptr, minval);
746           maxval = convert (*restype_ptr, maxval);
747         }
748
749       if (unsignedp && unsignedp0)
750         {
751           min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
752           max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
753           min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
754           max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
755         }
756       else
757         {
758           min_gt = INT_CST_LT (primop1, minval);
759           max_gt = INT_CST_LT (primop1, maxval);
760           min_lt = INT_CST_LT (minval, primop1);
761           max_lt = INT_CST_LT (maxval, primop1);
762         }
763
764       val = 0;
765       /* This used to be a switch, but Genix compiler can't handle that.  */
766       if (code == NE_EXPR)
767         {
768           if (max_lt || min_gt)
769             val = integer_one_node;
770         }
771       else if (code == EQ_EXPR)
772         {
773           if (max_lt || min_gt)
774             val = integer_zero_node;
775         }
776       else if (code == LT_EXPR)
777         {
778           if (max_lt)
779             val = integer_one_node;
780           if (!min_lt)
781             val = integer_zero_node;
782         }
783       else if (code == GT_EXPR)
784         {
785           if (min_gt)
786             val = integer_one_node;
787           if (!max_gt)
788             val = integer_zero_node;
789         }
790       else if (code == LE_EXPR)
791         {
792           if (!max_gt)
793             val = integer_one_node;
794           if (min_gt)
795             val = integer_zero_node;
796         }
797       else if (code == GE_EXPR)
798         {
799           if (!min_lt)
800             val = integer_one_node;
801           if (max_lt)
802             val = integer_zero_node;
803         }
804
805       /* If primop0 was sign-extended and unsigned comparison specd,
806          we did a signed comparison above using the signed type bounds.
807          But the comparison we output must be unsigned.
808
809          Also, for inequalities, VAL is no good; but if the signed
810          comparison had *any* fixed result, it follows that the
811          unsigned comparison just tests the sign in reverse
812          (positive values are LE, negative ones GE).
813          So we can generate an unsigned comparison
814          against an extreme value of the signed type.  */
815
816       if (unsignedp && !unsignedp0)
817         {
818           if (val != 0)
819             switch (code)
820               {
821               case LT_EXPR:
822               case GE_EXPR:
823                 primop1 = TYPE_MIN_VALUE (type);
824                 val = 0;
825                 break;
826
827               case LE_EXPR:
828               case GT_EXPR:
829                 primop1 = TYPE_MAX_VALUE (type);
830                 val = 0;
831                 break;
832               }
833           type = unsigned_type (type);
834         }
835
836       if (!max_gt && !unsignedp0)
837         {
838           /* This is the case of (char)x >?< 0x80, which people used to use
839              expecting old C compilers to change the 0x80 into -0x80.  */
840           if (val == integer_zero_node)
841             warning ("comparison is always 0 due to limited range of data type");
842           if (val == integer_one_node)
843             warning ("comparison is always 1 due to limited range of data type");
844         }
845
846       if (!min_lt && unsignedp0)
847         {
848           /* This is the case of (unsigned char)x >?< -1 or < 0.  */
849           if (val == integer_zero_node)
850             warning ("comparison is always 0 due to limited range of data type");
851           if (val == integer_one_node)
852             warning ("comparison is always 1 due to limited range of data type");
853         }
854
855       if (val != 0)
856         {
857           /* Don't forget to evaluate PRIMOP0 if it has side effects.  */
858           if (TREE_SIDE_EFFECTS (primop0))
859             return build (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
860           return val;
861         }
862
863       /* Value is not predetermined, but do the comparison
864          in the type of the operand that is not constant.
865          TYPE is already properly set.  */
866     }
867   else if (real1 && real2
868            && (TYPE_PRECISION (TREE_TYPE (primop0))
869                == TYPE_PRECISION (TREE_TYPE (primop1))))
870     type = TREE_TYPE (primop0);
871
872   /* If args' natural types are both narrower than nominal type
873      and both extend in the same manner, compare them
874      in the type of the wider arg.
875      Otherwise must actually extend both to the nominal
876      common type lest different ways of extending
877      alter the result.
878      (eg, (short)-1 == (unsigned short)-1  should be 0.)  */
879
880   else if (unsignedp0 == unsignedp1 && real1 == real2
881            && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
882            && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
883     {
884       type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
885       type = signed_or_unsigned_type (unsignedp0
886                                       || TREE_UNSIGNED (*restype_ptr),
887                                       type);
888       /* Make sure shorter operand is extended the right way
889          to match the longer operand.  */
890       primop0 = convert (signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0)),
891                          primop0);
892       primop1 = convert (signed_or_unsigned_type (unsignedp1, TREE_TYPE (primop1)),
893                          primop1);
894     }
895   else
896     {
897       /* Here we must do the comparison on the nominal type
898          using the args exactly as we received them.  */
899       type = *restype_ptr;
900       primop0 = op0;
901       primop1 = op1;
902
903       if (!real1 && !real2 && integer_zerop (primop1)
904           && TREE_UNSIGNED (TREE_TYPE (primop0)))
905         {
906           tree value = 0;
907           switch (code)
908             {
909             case GE_EXPR:
910               if (extra_warnings)
911                 warning ("unsigned value >= 0 is always 1");
912               value = integer_one_node;
913               break;
914
915             case LT_EXPR:
916               if (extra_warnings)
917                 warning ("unsigned value < 0 is always 0");
918               value = integer_zero_node;
919             }
920
921           if (value != 0)
922             {
923               /* Don't forget to evaluate PRIMOP0 if it has side effects.  */
924               if (TREE_SIDE_EFFECTS (primop0))
925                 return build (COMPOUND_EXPR, TREE_TYPE (value),
926                               primop0, value);
927               return value;
928             }
929         }
930     }
931
932   *op0_ptr = convert (type, primop0);
933   *op1_ptr = convert (type, primop1);
934
935   *restype_ptr = integer_type_node;
936
937   return 0;
938 }
939 \f
940 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
941    or validate its data type for an `if' or `while' statement or ?..: exp.
942
943    This preparation consists of taking the ordinary
944    representation of an expression expr and producing a valid tree
945    boolean expression describing whether expr is nonzero.  We could
946    simply always do build_binary_op (NE_EXPR, expr, integer_zero_node, 1),
947    but we optimize comparisons, &&, ||, and !.
948
949    The resulting type should always be `integer_type_node'.  */
950
951 tree
952 truthvalue_conversion (expr)
953      tree expr;
954 {
955   register enum tree_code code;
956
957   if (TREE_CODE (expr) == ERROR_MARK)
958     return expr;
959
960 #if 0 /* This appears to be wrong for C++.  */
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)))
964     {
965     case RECORD_TYPE:
966       error ("struct type value used where scalar is required");
967       return integer_zero_node;
968
969     case UNION_TYPE:
970       error ("union type value used where scalar is required");
971       return integer_zero_node;
972
973     case ARRAY_TYPE:
974       error ("array type value used where scalar is required");
975       return integer_zero_node;
976
977     default:
978       break;
979     }
980 #endif /* 0 */
981
982   switch (TREE_CODE (expr))
983     {
984       /* It is simpler and generates better code to have only TRUTH_*_EXPR
985          or comparison expressions as truth values at this level.  */
986 #if 0
987     case COMPONENT_REF:
988       /* A one-bit unsigned bit-field is already acceptable.  */
989       if (1 == TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (expr, 1)))
990           && TREE_UNSIGNED (TREE_OPERAND (expr, 1)))
991         return expr;
992       break;
993 #endif
994
995     case EQ_EXPR:
996       /* It is simpler and generates better code to have only TRUTH_*_EXPR
997          or comparison expressions as truth values at this level.  */
998 #if 0
999       if (integer_zerop (TREE_OPERAND (expr, 1)))
1000         return build_unary_op (TRUTH_NOT_EXPR, TREE_OPERAND (expr, 0), 0);
1001 #endif
1002     case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
1003     case TRUTH_ANDIF_EXPR:
1004     case TRUTH_ORIF_EXPR:
1005     case TRUTH_AND_EXPR:
1006     case TRUTH_OR_EXPR:
1007     case TRUTH_XOR_EXPR:
1008     case ERROR_MARK:
1009       return expr;
1010
1011     case INTEGER_CST:
1012       return integer_zerop (expr) ? integer_zero_node : integer_one_node;
1013
1014     case REAL_CST:
1015       return real_zerop (expr) ? integer_zero_node : integer_one_node;
1016
1017     case ADDR_EXPR:
1018       if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0)))
1019         return build (COMPOUND_EXPR, integer_type_node,
1020                       TREE_OPERAND (expr, 0), integer_one_node);
1021       else
1022         return integer_one_node;
1023
1024     case COMPLEX_EXPR:
1025       return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
1026                                ? TRUTH_AND_EXPR : TRUTH_ANDIF_EXPR),
1027                               truthvalue_conversion (TREE_OPERAND (expr, 0)),
1028                               truthvalue_conversion (TREE_OPERAND (expr, 1)),
1029                               0);
1030
1031     case NEGATE_EXPR:
1032     case ABS_EXPR:
1033     case FLOAT_EXPR:
1034     case FFS_EXPR:
1035       /* These don't change whether an object is non-zero or zero.  */
1036       return truthvalue_conversion (TREE_OPERAND (expr, 0));
1037
1038     case LROTATE_EXPR:
1039     case RROTATE_EXPR:
1040       /* These don't change whether an object is zero or non-zero, but
1041          we can't ignore them if their second arg has side-effects.  */
1042       if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
1043         return build (COMPOUND_EXPR, integer_type_node, TREE_OPERAND (expr, 1),
1044                       truthvalue_conversion (TREE_OPERAND (expr, 0)));
1045       else
1046         return truthvalue_conversion (TREE_OPERAND (expr, 0));
1047       
1048     case COND_EXPR:
1049       /* Distribute the conversion into the arms of a COND_EXPR.  */
1050       return fold (build (COND_EXPR, integer_type_node, TREE_OPERAND (expr, 0),
1051                           truthvalue_conversion (TREE_OPERAND (expr, 1)),
1052                           truthvalue_conversion (TREE_OPERAND (expr, 2))));
1053
1054     case CONVERT_EXPR:
1055       /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
1056          since that affects how `default_conversion' will behave.  */
1057       if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE
1058           || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
1059         break;
1060       /* fall through... */
1061     case NOP_EXPR:
1062       /* If this is widening the argument, we can ignore it.  */
1063       if (TYPE_PRECISION (TREE_TYPE (expr))
1064           >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
1065         return truthvalue_conversion (TREE_OPERAND (expr, 0));
1066       break;
1067
1068     case MINUS_EXPR:
1069       /* With IEEE arithmetic, x - x may not equal 0, so we can't optimize
1070          this case.  */
1071       if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
1072           && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE)
1073         break;
1074       /* fall through... */
1075     case BIT_XOR_EXPR:
1076       /* This and MINUS_EXPR can be changed into a comparison of the
1077          two objects.  */
1078       if (TREE_TYPE (TREE_OPERAND (expr, 0))
1079           == TREE_TYPE (TREE_OPERAND (expr, 1)))
1080         return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
1081                                 TREE_OPERAND (expr, 1), 1);
1082       return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
1083                               fold (build1 (NOP_EXPR,
1084                                             TREE_TYPE (TREE_OPERAND (expr, 0)),
1085                                             TREE_OPERAND (expr, 1))), 1);
1086
1087     case MODIFY_EXPR:
1088       if (warn_parentheses && C_EXP_ORIGINAL_CODE (expr) == MODIFY_EXPR)
1089         warning ("suggest parentheses around assignment used as truth value");
1090       break;
1091     }
1092
1093   if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
1094     return (build_binary_op
1095             ((TREE_SIDE_EFFECTS (expr)
1096               ? TRUTH_AND_EXPR : TRUTH_ANDIF_EXPR),
1097              truthvalue_conversion (build_unary_op (REALPART_EXPR, expr, 0)),
1098              truthvalue_conversion (build_unary_op (IMAGPART_EXPR, expr, 0)),
1099              0));
1100
1101   return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
1102 }
1103 \f
1104 /* Read the rest of a #-directive from input stream FINPUT.
1105    In normal use, the directive name and the white space after it
1106    have already been read, so they won't be included in the result.
1107    We allow for the fact that the directive line may contain
1108    a newline embedded within a character or string literal which forms
1109    a part of the directive.
1110
1111    The value is a string in a reusable buffer.  It remains valid
1112    only until the next time this function is called.  */
1113
1114 char *
1115 get_directive_line (finput)
1116      register FILE *finput;
1117 {
1118   static char *directive_buffer = NULL;
1119   static unsigned buffer_length = 0;
1120   register char *p;
1121   register char *buffer_limit;
1122   register int looking_for = 0;
1123   register int char_escaped = 0;
1124
1125   if (buffer_length == 0)
1126     {
1127       directive_buffer = (char *)xmalloc (128);
1128       buffer_length = 128;
1129     }
1130
1131   buffer_limit = &directive_buffer[buffer_length];
1132
1133   for (p = directive_buffer; ; )
1134     {
1135       int c;
1136
1137       /* Make buffer bigger if it is full.  */
1138       if (p >= buffer_limit)
1139         {
1140           register unsigned bytes_used = (p - directive_buffer);
1141
1142           buffer_length *= 2;
1143           directive_buffer
1144             = (char *)xrealloc (directive_buffer, buffer_length);
1145           p = &directive_buffer[bytes_used];
1146           buffer_limit = &directive_buffer[buffer_length];
1147         }
1148
1149       c = getc (finput);
1150
1151       /* Discard initial whitespace.  */
1152       if ((c == ' ' || c == '\t') && p == directive_buffer)
1153         continue;
1154
1155       /* Detect the end of the directive.  */
1156       if (c == '\n' && looking_for == 0)
1157         {
1158           ungetc (c, finput);
1159           c = '\0';
1160         }
1161
1162       *p++ = c;
1163
1164       if (c == 0)
1165         return directive_buffer;
1166
1167       /* Handle string and character constant syntax.  */
1168       if (looking_for)
1169         {
1170           if (looking_for == c && !char_escaped)
1171             looking_for = 0;    /* Found terminator... stop looking.  */
1172         }
1173       else
1174         if (c == '\'' || c == '"')
1175           looking_for = c;      /* Don't stop buffering until we see another
1176                                    another one of these (or an EOF).  */
1177
1178       /* Handle backslash.  */
1179       char_escaped = (c == '\\' && ! char_escaped);
1180     }
1181 }
1182 \f
1183 /* Make a variant type in the proper way for C/C++, propagating qualifiers
1184    down to the element type of an array.  */
1185
1186 tree
1187 c_build_type_variant (type, constp, volatilep)
1188      tree type;
1189      int constp, volatilep;
1190 {
1191   if (TREE_CODE (type) == ARRAY_TYPE)
1192     {
1193       tree real_main_variant = TYPE_MAIN_VARIANT (type);
1194       int permanent = TREE_PERMANENT (type);
1195
1196       if (permanent)
1197         push_obstacks (&permanent_obstack, &permanent_obstack);
1198       type = build_array_type (c_build_type_variant (TREE_TYPE (type),
1199                                                      constp, volatilep),
1200                                TYPE_DOMAIN (type));
1201       TYPE_MAIN_VARIANT (type) = real_main_variant;
1202       if (permanent)
1203         pop_obstacks ();
1204     }
1205   return build_type_variant (type, constp, volatilep);
1206 }