OSDN Git Service

(NEEDS_UNTYPED_CALL): Define.
[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         
279         if (TREE_CODE (decl) != FUNCTION_DECL)
280           {
281             error_with_decl (decl,
282                              "argument format specified for non-function `%s'");
283             return;
284           }
285         
286         if (format_type == get_identifier ("printf"))
287           is_scan = 0;
288         else if (format_type == get_identifier ("scanf"))
289           is_scan = 1;
290         else
291           {
292             error_with_decl (decl, "unrecognized format specifier for `%s'");
293             return;
294           }
295         
296         if (first_arg_num != 0 && first_arg_num <= format_num)
297           {
298             error_with_decl (decl,
299                 "format string arg follows the args to be formatted, for `%s'");
300             return;
301           }
302         
303         record_format_info (DECL_NAME (decl), is_scan, format_num,
304                             first_arg_num);
305       }
306 }
307 \f
308 /* Print a warning if a constant expression had overflow in folding.
309    Invoke this function on every expression that the language
310    requires to be a constant expression.
311    Note the ANSI C standard says it is erroneous for a
312    constant expression to overflow.  */
313
314 void
315 constant_expression_warning (value)
316      tree value;
317 {
318   if (TREE_CODE (value) == INTEGER_CST && TREE_CONSTANT_OVERFLOW (value))
319     {
320       pedwarn ("overflow in constant expression");
321       /* Suppress duplicate warnings.  */
322       TREE_CONSTANT_OVERFLOW (value) = 0;
323     }
324 }
325
326 /* Print a warning if an expression had overflow in folding.
327    Invoke this function on every expression that
328    (1) appears in the source code, and
329    (2) might be a constant expression that overflowed, and
330    (3) is not already checked by convert_and_check;
331    however, do not invoke this function on operands of explicit casts.  */
332
333 void
334 overflow_warning (value)
335      tree value;
336 {
337   if (TREE_CODE (value) == INTEGER_CST && TREE_CONSTANT_OVERFLOW (value))
338     {
339       pedwarn ("integer overflow in expression");
340       TREE_CONSTANT_OVERFLOW (value) = 0;
341     }
342 }
343
344 /* Print a warning if a large constant is truncated to unsigned,
345    or if -Wconversion is used and a constant < 0 is converted to unsigned.
346    Invoke this function on every expression that might be implicitly
347    converted to an unsigned type.  */
348
349 void
350 unsigned_conversion_warning (result, operand)
351      tree result, operand;
352 {
353   if (TREE_CODE (operand) == INTEGER_CST
354       && TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
355       && TREE_UNSIGNED (TREE_TYPE (result))
356       && !int_fits_type_p (operand, TREE_TYPE (result)))
357     {
358       if (!int_fits_type_p (operand, signed_type (TREE_TYPE (result))))
359         /* This detects cases like converting -129 or 256 to unsigned char.  */
360         pedwarn ("large integer implicitly truncated to unsigned type");
361       else if (warn_conversion)
362         pedwarn ("negative integer implicitly converted to unsigned type");
363     }
364 }
365
366 /* Convert EXPR to TYPE, warning about conversion problems with constants.
367    Invoke this function on every expression that is converted implicitly,
368    i.e. because of language rules and not because of an explicit cast.  */
369
370 tree
371 convert_and_check (type, expr)
372      tree type, expr;
373 {
374   tree t = convert (type, expr);
375   if (TREE_CODE (t) == INTEGER_CST)
376     {
377       if (TREE_UNSIGNED (TREE_TYPE (expr))
378           && !TREE_UNSIGNED (type)
379           && TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
380           && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (expr)))
381         /* No warning for converting 0x80000000 to int.  */
382         TREE_CONSTANT_OVERFLOW (t) = 0;
383       else if (TREE_CONSTANT_OVERFLOW (t))
384         {
385           pedwarn ("overflow in implicit constant conversion");
386           TREE_CONSTANT_OVERFLOW (t) = 0;
387         }
388       else
389         unsigned_conversion_warning (t, expr);
390     }
391   return t;
392 }
393 \f
394 void
395 c_expand_expr_stmt (expr)
396      tree expr;
397 {
398   /* Do default conversion if safe and possibly important,
399      in case within ({...}).  */
400   if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE && lvalue_p (expr))
401       || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
402     expr = default_conversion (expr);
403
404   if (TREE_TYPE (expr) != error_mark_node
405       && TYPE_SIZE (TREE_TYPE (expr)) == 0
406       && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
407     error ("expression statement has incomplete type");
408
409   expand_expr_stmt (expr);
410 }
411 \f
412 /* Validate the expression after `case' and apply default promotions.  */
413
414 tree
415 check_case_value (value)
416      tree value;
417 {
418   if (value == NULL_TREE)
419     return value;
420
421   /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue.  */
422   STRIP_TYPE_NOPS (value);
423
424   if (TREE_CODE (value) != INTEGER_CST
425       && value != error_mark_node)
426     {
427       error ("case label does not reduce to an integer constant");
428       value = error_mark_node;
429     }
430   else
431     /* Promote char or short to int.  */
432     value = default_conversion (value);
433
434   constant_expression_warning (value);
435
436   return value;
437 }
438 \f
439 /* Return an integer type with BITS bits of precision,
440    that is unsigned if UNSIGNEDP is nonzero, otherwise signed.  */
441
442 tree
443 type_for_size (bits, unsignedp)
444      unsigned bits;
445      int unsignedp;
446 {
447   if (bits == TYPE_PRECISION (signed_char_type_node))
448     return unsignedp ? unsigned_char_type_node : signed_char_type_node;
449
450   if (bits == TYPE_PRECISION (short_integer_type_node))
451     return unsignedp ? short_unsigned_type_node : short_integer_type_node;
452
453   if (bits == TYPE_PRECISION (integer_type_node))
454     return unsignedp ? unsigned_type_node : integer_type_node;
455
456   if (bits == TYPE_PRECISION (long_integer_type_node))
457     return unsignedp ? long_unsigned_type_node : long_integer_type_node;
458
459   if (bits == TYPE_PRECISION (long_long_integer_type_node))
460     return (unsignedp ? long_long_unsigned_type_node
461             : long_long_integer_type_node);
462
463   if (bits <= TYPE_PRECISION (intQI_type_node))
464     return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
465
466   if (bits <= TYPE_PRECISION (intHI_type_node))
467     return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
468
469   if (bits <= TYPE_PRECISION (intSI_type_node))
470     return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
471
472   if (bits <= TYPE_PRECISION (intDI_type_node))
473     return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
474
475   return 0;
476 }
477
478 /* Return a data type that has machine mode MODE.
479    If the mode is an integer,
480    then UNSIGNEDP selects between signed and unsigned types.  */
481
482 tree
483 type_for_mode (mode, unsignedp)
484      enum machine_mode mode;
485      int unsignedp;
486 {
487   if (mode == TYPE_MODE (signed_char_type_node))
488     return unsignedp ? unsigned_char_type_node : signed_char_type_node;
489
490   if (mode == TYPE_MODE (short_integer_type_node))
491     return unsignedp ? short_unsigned_type_node : short_integer_type_node;
492
493   if (mode == TYPE_MODE (integer_type_node))
494     return unsignedp ? unsigned_type_node : integer_type_node;
495
496   if (mode == TYPE_MODE (long_integer_type_node))
497     return unsignedp ? long_unsigned_type_node : long_integer_type_node;
498
499   if (mode == TYPE_MODE (long_long_integer_type_node))
500     return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
501
502   if (mode == TYPE_MODE (intQI_type_node))
503     return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
504
505   if (mode == TYPE_MODE (intHI_type_node))
506     return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
507
508   if (mode == TYPE_MODE (intSI_type_node))
509     return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
510
511   if (mode == TYPE_MODE (intDI_type_node))
512     return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
513
514   if (mode == TYPE_MODE (float_type_node))
515     return float_type_node;
516
517   if (mode == TYPE_MODE (double_type_node))
518     return double_type_node;
519
520   if (mode == TYPE_MODE (long_double_type_node))
521     return long_double_type_node;
522
523   if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
524     return build_pointer_type (char_type_node);
525
526   if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
527     return build_pointer_type (integer_type_node);
528
529   return 0;
530 }
531 \f
532 /* Print an error message for invalid operands to arith operation CODE.
533    NOP_EXPR is used as a special case (see truthvalue_conversion).  */
534
535 void
536 binary_op_error (code)
537      enum tree_code code;
538 {
539   register char *opname;
540   switch (code)
541     {
542     case NOP_EXPR:
543       error ("invalid truth-value expression");
544       return;
545
546     case PLUS_EXPR:
547       opname = "+"; break;
548     case MINUS_EXPR:
549       opname = "-"; break;
550     case MULT_EXPR:
551       opname = "*"; break;
552     case MAX_EXPR:
553       opname = "max"; break;
554     case MIN_EXPR:
555       opname = "min"; break;
556     case EQ_EXPR:
557       opname = "=="; break;
558     case NE_EXPR:
559       opname = "!="; break;
560     case LE_EXPR:
561       opname = "<="; break;
562     case GE_EXPR:
563       opname = ">="; break;
564     case LT_EXPR:
565       opname = "<"; break;
566     case GT_EXPR:
567       opname = ">"; break;
568     case LSHIFT_EXPR:
569       opname = "<<"; break;
570     case RSHIFT_EXPR:
571       opname = ">>"; break;
572     case TRUNC_MOD_EXPR:
573     case FLOOR_MOD_EXPR:
574       opname = "%"; break;
575     case TRUNC_DIV_EXPR:
576     case FLOOR_DIV_EXPR:
577       opname = "/"; break;
578     case BIT_AND_EXPR:
579       opname = "&"; break;
580     case BIT_IOR_EXPR:
581       opname = "|"; break;
582     case TRUTH_ANDIF_EXPR:
583       opname = "&&"; break;
584     case TRUTH_ORIF_EXPR:
585       opname = "||"; break;
586     case BIT_XOR_EXPR:
587       opname = "^"; break;
588     case LROTATE_EXPR:
589     case RROTATE_EXPR:
590       opname = "rotate"; break;
591     }
592   error ("invalid operands to binary %s", opname);
593 }
594 \f
595 /* Subroutine of build_binary_op, used for comparison operations.
596    See if the operands have both been converted from subword integer types
597    and, if so, perhaps change them both back to their original type.
598
599    The arguments of this function are all pointers to local variables
600    of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
601    RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
602
603    If this function returns nonzero, it means that the comparison has
604    a constant value.  What this function returns is an expression for
605    that value.  */
606
607 tree
608 shorten_compare (op0_ptr, op1_ptr, restype_ptr, rescode_ptr)
609      tree *op0_ptr, *op1_ptr;
610      tree *restype_ptr;
611      enum tree_code *rescode_ptr;
612 {
613   register tree type;
614   tree op0 = *op0_ptr;
615   tree op1 = *op1_ptr;
616   int unsignedp0, unsignedp1;
617   int real1, real2;
618   tree primop0, primop1;
619   enum tree_code code = *rescode_ptr;
620
621   /* Throw away any conversions to wider types
622      already present in the operands.  */
623
624   primop0 = get_narrower (op0, &unsignedp0);
625   primop1 = get_narrower (op1, &unsignedp1);
626
627   /* Handle the case that OP0 does not *contain* a conversion
628      but it *requires* conversion to FINAL_TYPE.  */
629
630   if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
631     unsignedp0 = TREE_UNSIGNED (TREE_TYPE (op0));
632   if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
633     unsignedp1 = TREE_UNSIGNED (TREE_TYPE (op1));
634
635   /* If one of the operands must be floated, we cannot optimize.  */
636   real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
637   real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
638
639   /* If first arg is constant, swap the args (changing operation
640      so value is preserved), for canonicalization.  */
641
642   if (TREE_CONSTANT (primop0))
643     {
644       register tree tem = primop0;
645       register int temi = unsignedp0;
646       primop0 = primop1;
647       primop1 = tem;
648       tem = op0;
649       op0 = op1;
650       op1 = tem;
651       *op0_ptr = op0;
652       *op1_ptr = op1;
653       unsignedp0 = unsignedp1;
654       unsignedp1 = temi;
655       temi = real1;
656       real1 = real2;
657       real2 = temi;
658
659       switch (code)
660         {
661         case LT_EXPR:
662           code = GT_EXPR;
663           break;
664         case GT_EXPR:
665           code = LT_EXPR;
666           break;
667         case LE_EXPR:
668           code = GE_EXPR;
669           break;
670         case GE_EXPR:
671           code = LE_EXPR;
672           break;
673         }
674       *rescode_ptr = code;
675     }
676
677   /* If comparing an integer against a constant more bits wide,
678      maybe we can deduce a value of 1 or 0 independent of the data.
679      Or else truncate the constant now
680      rather than extend the variable at run time.
681
682      This is only interesting if the constant is the wider arg.
683      Also, it is not safe if the constant is unsigned and the
684      variable arg is signed, since in this case the variable
685      would be sign-extended and then regarded as unsigned.
686      Our technique fails in this case because the lowest/highest
687      possible unsigned results don't follow naturally from the
688      lowest/highest possible values of the variable operand.
689      For just EQ_EXPR and NE_EXPR there is another technique that
690      could be used: see if the constant can be faithfully represented
691      in the other operand's type, by truncating it and reextending it
692      and see if that preserves the constant's value.  */
693
694   if (!real1 && !real2
695       && TREE_CODE (primop1) == INTEGER_CST
696       && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
697     {
698       int min_gt, max_gt, min_lt, max_lt;
699       tree maxval, minval;
700       /* 1 if comparison is nominally unsigned.  */
701       int unsignedp = TREE_UNSIGNED (*restype_ptr);
702       tree val;
703
704       type = signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0));
705
706       maxval = TYPE_MAX_VALUE (type);
707       minval = TYPE_MIN_VALUE (type);
708
709       if (unsignedp && !unsignedp0)
710         *restype_ptr = signed_type (*restype_ptr);
711
712       if (TREE_TYPE (primop1) != *restype_ptr)
713         primop1 = convert (*restype_ptr, primop1);
714       if (type != *restype_ptr)
715         {
716           minval = convert (*restype_ptr, minval);
717           maxval = convert (*restype_ptr, maxval);
718         }
719
720       if (unsignedp && unsignedp0)
721         {
722           min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
723           max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
724           min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
725           max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
726         }
727       else
728         {
729           min_gt = INT_CST_LT (primop1, minval);
730           max_gt = INT_CST_LT (primop1, maxval);
731           min_lt = INT_CST_LT (minval, primop1);
732           max_lt = INT_CST_LT (maxval, primop1);
733         }
734
735       val = 0;
736       /* This used to be a switch, but Genix compiler can't handle that.  */
737       if (code == NE_EXPR)
738         {
739           if (max_lt || min_gt)
740             val = integer_one_node;
741         }
742       else if (code == EQ_EXPR)
743         {
744           if (max_lt || min_gt)
745             val = integer_zero_node;
746         }
747       else if (code == LT_EXPR)
748         {
749           if (max_lt)
750             val = integer_one_node;
751           if (!min_lt)
752             val = integer_zero_node;
753         }
754       else if (code == GT_EXPR)
755         {
756           if (min_gt)
757             val = integer_one_node;
758           if (!max_gt)
759             val = integer_zero_node;
760         }
761       else if (code == LE_EXPR)
762         {
763           if (!max_gt)
764             val = integer_one_node;
765           if (min_gt)
766             val = integer_zero_node;
767         }
768       else if (code == GE_EXPR)
769         {
770           if (!min_lt)
771             val = integer_one_node;
772           if (max_lt)
773             val = integer_zero_node;
774         }
775
776       /* If primop0 was sign-extended and unsigned comparison specd,
777          we did a signed comparison above using the signed type bounds.
778          But the comparison we output must be unsigned.
779
780          Also, for inequalities, VAL is no good; but if the signed
781          comparison had *any* fixed result, it follows that the
782          unsigned comparison just tests the sign in reverse
783          (positive values are LE, negative ones GE).
784          So we can generate an unsigned comparison
785          against an extreme value of the signed type.  */
786
787       if (unsignedp && !unsignedp0)
788         {
789           if (val != 0)
790             switch (code)
791               {
792               case LT_EXPR:
793               case GE_EXPR:
794                 primop1 = TYPE_MIN_VALUE (type);
795                 val = 0;
796                 break;
797
798               case LE_EXPR:
799               case GT_EXPR:
800                 primop1 = TYPE_MAX_VALUE (type);
801                 val = 0;
802                 break;
803               }
804           type = unsigned_type (type);
805         }
806
807       if (!max_gt && !unsignedp0)
808         {
809           /* This is the case of (char)x >?< 0x80, which people used to use
810              expecting old C compilers to change the 0x80 into -0x80.  */
811           if (val == integer_zero_node)
812             warning ("comparison is always 0 due to limited range of data type");
813           if (val == integer_one_node)
814             warning ("comparison is always 1 due to limited range of data type");
815         }
816
817       if (!min_lt && unsignedp0)
818         {
819           /* This is the case of (unsigned char)x >?< -1 or < 0.  */
820           if (val == integer_zero_node)
821             warning ("comparison is always 0 due to limited range of data type");
822           if (val == integer_one_node)
823             warning ("comparison is always 1 due to limited range of data type");
824         }
825
826       if (val != 0)
827         {
828           /* Don't forget to evaluate PRIMOP0 if it has side effects.  */
829           if (TREE_SIDE_EFFECTS (primop0))
830             return build (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
831           return val;
832         }
833
834       /* Value is not predetermined, but do the comparison
835          in the type of the operand that is not constant.
836          TYPE is already properly set.  */
837     }
838   else if (real1 && real2
839            && (TYPE_PRECISION (TREE_TYPE (primop0))
840                == TYPE_PRECISION (TREE_TYPE (primop1))))
841     type = TREE_TYPE (primop0);
842
843   /* If args' natural types are both narrower than nominal type
844      and both extend in the same manner, compare them
845      in the type of the wider arg.
846      Otherwise must actually extend both to the nominal
847      common type lest different ways of extending
848      alter the result.
849      (eg, (short)-1 == (unsigned short)-1  should be 0.)  */
850
851   else if (unsignedp0 == unsignedp1 && real1 == real2
852            && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
853            && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
854     {
855       type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
856       type = signed_or_unsigned_type (unsignedp0
857                                       || TREE_UNSIGNED (*restype_ptr),
858                                       type);
859       /* Make sure shorter operand is extended the right way
860          to match the longer operand.  */
861       primop0 = convert (signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0)),
862                          primop0);
863       primop1 = convert (signed_or_unsigned_type (unsignedp1, TREE_TYPE (primop1)),
864                          primop1);
865     }
866   else
867     {
868       /* Here we must do the comparison on the nominal type
869          using the args exactly as we received them.  */
870       type = *restype_ptr;
871       primop0 = op0;
872       primop1 = op1;
873
874       if (!real1 && !real2 && integer_zerop (primop1)
875           && TREE_UNSIGNED (TREE_TYPE (primop0)))
876         {
877           tree value = 0;
878           switch (code)
879             {
880             case GE_EXPR:
881               if (extra_warnings)
882                 warning ("unsigned value >= 0 is always 1");
883               value = integer_one_node;
884               break;
885
886             case LT_EXPR:
887               if (extra_warnings)
888                 warning ("unsigned value < 0 is always 0");
889               value = integer_zero_node;
890             }
891
892           if (value != 0)
893             {
894               /* Don't forget to evaluate PRIMOP0 if it has side effects.  */
895               if (TREE_SIDE_EFFECTS (primop0))
896                 return build (COMPOUND_EXPR, TREE_TYPE (value),
897                               primop0, value);
898               return value;
899             }
900         }
901     }
902
903   *op0_ptr = convert (type, primop0);
904   *op1_ptr = convert (type, primop1);
905
906   *restype_ptr = integer_type_node;
907
908   return 0;
909 }
910 \f
911 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
912    or validate its data type for an `if' or `while' statement or ?..: exp.
913
914    This preparation consists of taking the ordinary
915    representation of an expression expr and producing a valid tree
916    boolean expression describing whether expr is nonzero.  We could
917    simply always do build_binary_op (NE_EXPR, expr, integer_zero_node, 1),
918    but we optimize comparisons, &&, ||, and !.
919
920    The resulting type should always be `integer_type_node'.  */
921
922 tree
923 truthvalue_conversion (expr)
924      tree expr;
925 {
926   register enum tree_code code;
927
928   switch (TREE_CODE (expr))
929     {
930       /* It is simpler and generates better code to have only TRUTH_*_EXPR
931          or comparison expressions as truth values at this level.  */
932 #if 0
933     case COMPONENT_REF:
934       /* A one-bit unsigned bit-field is already acceptable.  */
935       if (1 == TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (expr, 1)))
936           && TREE_UNSIGNED (TREE_OPERAND (expr, 1)))
937         return expr;
938       break;
939 #endif
940
941     case EQ_EXPR:
942       /* It is simpler and generates better code to have only TRUTH_*_EXPR
943          or comparison expressions as truth values at this level.  */
944 #if 0
945       if (integer_zerop (TREE_OPERAND (expr, 1)))
946         return build_unary_op (TRUTH_NOT_EXPR, TREE_OPERAND (expr, 0), 0);
947 #endif
948     case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
949     case TRUTH_ANDIF_EXPR:
950     case TRUTH_ORIF_EXPR:
951     case TRUTH_AND_EXPR:
952     case TRUTH_OR_EXPR:
953     case TRUTH_XOR_EXPR:
954     case ERROR_MARK:
955       return expr;
956
957     case INTEGER_CST:
958       return integer_zerop (expr) ? integer_zero_node : integer_one_node;
959
960     case REAL_CST:
961       return real_zerop (expr) ? integer_zero_node : integer_one_node;
962
963     case ADDR_EXPR:
964       if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0)))
965         return build (COMPOUND_EXPR, integer_type_node,
966                       TREE_OPERAND (expr, 0), integer_one_node);
967       else
968         return integer_one_node;
969
970     case COMPLEX_EXPR:
971       return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
972                                ? TRUTH_AND_EXPR : TRUTH_ANDIF_EXPR),
973                               truthvalue_conversion (TREE_OPERAND (expr, 0)),
974                               truthvalue_conversion (TREE_OPERAND (expr, 1)),
975                               0);
976
977     case NEGATE_EXPR:
978     case ABS_EXPR:
979     case FLOAT_EXPR:
980     case FFS_EXPR:
981       /* These don't change whether an object is non-zero or zero.  */
982       return truthvalue_conversion (TREE_OPERAND (expr, 0));
983
984     case LROTATE_EXPR:
985     case RROTATE_EXPR:
986       /* These don't change whether an object is zero or non-zero, but
987          we can't ignore them if their second arg has side-effects.  */
988       if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
989         return build (COMPOUND_EXPR, integer_type_node, TREE_OPERAND (expr, 1),
990                       truthvalue_conversion (TREE_OPERAND (expr, 0)));
991       else
992         return truthvalue_conversion (TREE_OPERAND (expr, 0));
993       
994     case COND_EXPR:
995       /* Distribute the conversion into the arms of a COND_EXPR.  */
996       return fold (build (COND_EXPR, integer_type_node, TREE_OPERAND (expr, 0),
997                           truthvalue_conversion (TREE_OPERAND (expr, 1)),
998                           truthvalue_conversion (TREE_OPERAND (expr, 2))));
999
1000     case CONVERT_EXPR:
1001       /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
1002          since that affects how `default_conversion' will behave.  */
1003       if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE
1004           || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
1005         break;
1006       /* fall through... */
1007     case NOP_EXPR:
1008       /* If this is widening the argument, we can ignore it.  */
1009       if (TYPE_PRECISION (TREE_TYPE (expr))
1010           >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
1011         return truthvalue_conversion (TREE_OPERAND (expr, 0));
1012       break;
1013
1014     case BIT_XOR_EXPR:
1015     case MINUS_EXPR:
1016       /* These can be changed into a comparison of the two objects.  */
1017       if (TREE_TYPE (TREE_OPERAND (expr, 0))
1018           == TREE_TYPE (TREE_OPERAND (expr, 1)))
1019         return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
1020                                 TREE_OPERAND (expr, 1), 1);
1021       return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
1022                               fold (build1 (NOP_EXPR,
1023                                             TREE_TYPE (TREE_OPERAND (expr, 0)),
1024                                             TREE_OPERAND (expr, 1))), 1);
1025
1026     case MODIFY_EXPR:
1027       if (warn_parentheses && C_EXP_ORIGINAL_CODE (expr) == MODIFY_EXPR)
1028         warning ("suggest parentheses around assignment used as truth value");
1029       break;
1030     }
1031
1032   if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
1033     return (build_binary_op
1034             ((TREE_SIDE_EFFECTS (expr)
1035               ? TRUTH_AND_EXPR : TRUTH_ANDIF_EXPR),
1036              truthvalue_conversion (build_unary_op (REALPART_EXPR, expr, 0)),
1037              truthvalue_conversion (build_unary_op (IMAGPART_EXPR, expr, 0)),
1038              0));
1039
1040   return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
1041 }
1042 \f
1043 /* Read the rest of a #-directive from input stream FINPUT.
1044    In normal use, the directive name and the white space after it
1045    have already been read, so they won't be included in the result.
1046    We allow for the fact that the directive line may contain
1047    a newline embedded within a character or string literal which forms
1048    a part of the directive.
1049
1050    The value is a string in a reusable buffer.  It remains valid
1051    only until the next time this function is called.  */
1052
1053 char *
1054 get_directive_line (finput)
1055      register FILE *finput;
1056 {
1057   static char *directive_buffer = NULL;
1058   static unsigned buffer_length = 0;
1059   register char *p;
1060   register char *buffer_limit;
1061   register int looking_for = 0;
1062   register int char_escaped = 0;
1063
1064   if (buffer_length == 0)
1065     {
1066       directive_buffer = (char *)xmalloc (128);
1067       buffer_length = 128;
1068     }
1069
1070   buffer_limit = &directive_buffer[buffer_length];
1071
1072   for (p = directive_buffer; ; )
1073     {
1074       int c;
1075
1076       /* Make buffer bigger if it is full.  */
1077       if (p >= buffer_limit)
1078         {
1079           register unsigned bytes_used = (p - directive_buffer);
1080
1081           buffer_length *= 2;
1082           directive_buffer
1083             = (char *)xrealloc (directive_buffer, buffer_length);
1084           p = &directive_buffer[bytes_used];
1085           buffer_limit = &directive_buffer[buffer_length];
1086         }
1087
1088       c = getc (finput);
1089
1090       /* Discard initial whitespace.  */
1091       if ((c == ' ' || c == '\t') && p == directive_buffer)
1092         continue;
1093
1094       /* Detect the end of the directive.  */
1095       if (c == '\n' && looking_for == 0)
1096         {
1097           ungetc (c, finput);
1098           c = '\0';
1099         }
1100
1101       *p++ = c;
1102
1103       if (c == 0)
1104         return directive_buffer;
1105
1106       /* Handle string and character constant syntax.  */
1107       if (looking_for)
1108         {
1109           if (looking_for == c && !char_escaped)
1110             looking_for = 0;    /* Found terminator... stop looking.  */
1111         }
1112       else
1113         if (c == '\'' || c == '"')
1114           looking_for = c;      /* Don't stop buffering until we see another
1115                                    another one of these (or an EOF).  */
1116
1117       /* Handle backslash.  */
1118       char_escaped = (c == '\\' && ! char_escaped);
1119     }
1120 }
1121 \f
1122 /* Make a variant type in the proper way for C/C++, propagating qualifiers
1123    down to the element type of an array.  */
1124
1125 tree
1126 c_build_type_variant (type, constp, volatilep)
1127      tree type;
1128      int constp, volatilep;
1129 {
1130   if (TREE_CODE (type) == ARRAY_TYPE)
1131     {
1132       tree real_main_variant = TYPE_MAIN_VARIANT (type);
1133       int permanent = TREE_PERMANENT (type);
1134
1135       if (permanent)
1136         push_obstacks (&permanent_obstack, &permanent_obstack);
1137       type = build_array_type (c_build_type_variant (TREE_TYPE (type),
1138                                                      constp, volatilep),
1139                                TYPE_DOMAIN (type));
1140       TYPE_MAIN_VARIANT (type) = real_main_variant;
1141       if (permanent)
1142         pop_obstacks ();
1143     }
1144   return build_type_variant (type, constp, volatilep);
1145 }