(check_format_info): Avoid ?: conditional for function to be called.
[platform/upstream/gcc.git] / gcc / c-common.c
1 /* Subroutines shared by all languages that are variants of C.
2    Copyright (C) 1992, 1993 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 #include <ctype.h>
28
29 extern struct obstack permanent_obstack;
30
31 static void declare_hidden_char_array PROTO((char *, char *));
32
33 /* Make bindings for __FUNCTION__ and __PRETTY_FUNCTION__.  */
34
35 void
36 declare_function_name ()
37 {
38   char *name, *printable_name;
39
40   if (current_function_decl == NULL)
41     {
42       name = "";
43       printable_name = "top level";
44     }
45   else
46     {
47       char *kind = "function";
48       if (TREE_CODE (TREE_TYPE (current_function_decl)) == METHOD_TYPE)
49         kind = "method";
50       /* Allow functions to be nameless (such as artificial ones).  */
51       if (DECL_NAME (current_function_decl))
52         name = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
53       else
54         name = "";
55       printable_name = (*decl_printable_name) (current_function_decl, &kind);
56     }
57
58   declare_hidden_char_array ("__FUNCTION__", name);
59   declare_hidden_char_array ("__PRETTY_FUNCTION__", printable_name);
60 }
61
62 static void
63 declare_hidden_char_array (name, value)
64      char *name, *value;
65 {
66   tree decl, type, init;
67   int vlen;
68
69   /* If the default size of char arrays isn't big enough for the name,
70      make a bigger one.  */
71   vlen = strlen (value) + 1;
72   type = char_array_type_node;
73   if (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TREE_TYPE (type))) < vlen)
74     type = build_array_type (char_type_node,
75                              build_index_type (build_int_2 (vlen, 0)));
76
77   push_obstacks_nochange ();
78   decl = build_decl (VAR_DECL, get_identifier (name), type);
79   TREE_STATIC (decl) = 1;
80   TREE_READONLY (decl) = 1;
81   TREE_ASM_WRITTEN (decl) = 1;
82   DECL_SOURCE_LINE (decl) = 0;
83   DECL_IN_SYSTEM_HEADER (decl) = 1;
84   DECL_IGNORED_P (decl) = 1;
85   init = build_string (vlen, value);
86   TREE_TYPE (init) = type;
87   DECL_INITIAL (decl) = init;
88   finish_decl (pushdecl (decl), init, NULL_TREE);
89 }
90
91 /* Given a chain of STRING_CST nodes,
92    concatenate them into one STRING_CST
93    and give it a suitable array-of-chars data type.  */
94
95 tree
96 combine_strings (strings)
97      tree strings;
98 {
99   register tree value, t;
100   register int length = 1;
101   int wide_length = 0;
102   int wide_flag = 0;
103   int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
104   int nchars;
105
106   if (TREE_CHAIN (strings))
107     {
108       /* More than one in the chain, so concatenate.  */
109       register char *p, *q;
110
111       /* Don't include the \0 at the end of each substring,
112          except for the last one.
113          Count wide strings and ordinary strings separately.  */
114       for (t = strings; t; t = TREE_CHAIN (t))
115         {
116           if (TREE_TYPE (t) == wchar_array_type_node)
117             {
118               wide_length += (TREE_STRING_LENGTH (t) - wchar_bytes);
119               wide_flag = 1;
120             }
121           else
122             length += (TREE_STRING_LENGTH (t) - 1);
123         }
124
125       /* If anything is wide, the non-wides will be converted,
126          which makes them take more space.  */
127       if (wide_flag)
128         length = length * wchar_bytes + wide_length;
129
130       p = savealloc (length);
131
132       /* Copy the individual strings into the new combined string.
133          If the combined string is wide, convert the chars to ints
134          for any individual strings that are not wide.  */
135
136       q = p;
137       for (t = strings; t; t = TREE_CHAIN (t))
138         {
139           int len = (TREE_STRING_LENGTH (t)
140                      - ((TREE_TYPE (t) == wchar_array_type_node)
141                         ? wchar_bytes : 1));
142           if ((TREE_TYPE (t) == wchar_array_type_node) == wide_flag)
143             {
144               bcopy (TREE_STRING_POINTER (t), q, len);
145               q += len;
146             }
147           else
148             {
149               int i;
150               for (i = 0; i < len; i++)
151                 ((int *) q)[i] = TREE_STRING_POINTER (t)[i];
152               q += len * wchar_bytes;
153             }
154         }
155       if (wide_flag)
156         {
157           int i;
158           for (i = 0; i < wchar_bytes; i++)
159             *q++ = 0;
160         }
161       else
162         *q = 0;
163
164       value = make_node (STRING_CST);
165       TREE_STRING_POINTER (value) = p;
166       TREE_STRING_LENGTH (value) = length;
167       TREE_CONSTANT (value) = 1;
168     }
169   else
170     {
171       value = strings;
172       length = TREE_STRING_LENGTH (value);
173       if (TREE_TYPE (value) == wchar_array_type_node)
174         wide_flag = 1;
175     }
176
177   /* Compute the number of elements, for the array type.  */ 
178   nchars = wide_flag ? length / wchar_bytes : length;
179
180   /* Create the array type for the string constant.
181      -Wwrite-strings says make the string constant an array of const char
182      so that copying it to a non-const pointer will get a warning.  */
183   if (warn_write_strings
184       && (! flag_traditional  && ! flag_writable_strings))
185     {
186       tree elements
187         = build_type_variant (wide_flag ? wchar_type_node : char_type_node,
188                               1, 0);
189       TREE_TYPE (value)
190         = build_array_type (elements,
191                             build_index_type (build_int_2 (nchars - 1, 0)));
192     }
193   else
194     TREE_TYPE (value)
195       = build_array_type (wide_flag ? wchar_type_node : char_type_node,
196                           build_index_type (build_int_2 (nchars - 1, 0)));
197   TREE_CONSTANT (value) = 1;
198   TREE_STATIC (value) = 1;
199   return value;
200 }
201 \f
202 /* Process the attributes listed in ATTRIBUTES
203    and install them in DECL.  */
204
205 void
206 decl_attributes (decl, attributes)
207      tree decl, attributes;
208 {
209   tree a;
210   for (a = attributes; a; a = TREE_CHAIN (a))
211     if (TREE_VALUE (a) == get_identifier ("packed"))
212       {
213         if (TREE_CODE (decl) == FIELD_DECL)
214           DECL_PACKED (decl) = 1;
215         /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
216            used for DECL_REGISTER.  It wouldn't mean anything anyway.  */
217       }
218     else if (TREE_VALUE (a) == get_identifier ("noreturn")
219              || TREE_VALUE (a) == get_identifier ("volatile"))
220       {
221         if (TREE_CODE (decl) == FUNCTION_DECL)
222           TREE_THIS_VOLATILE (decl) = 1;
223       }
224     else if (TREE_VALUE (a) == get_identifier ("const"))
225       {
226         if (TREE_CODE (decl) == FUNCTION_DECL)
227           TREE_READONLY (decl) = 1;
228       }
229     else if (TREE_VALUE (a) != 0
230              && TREE_CODE (TREE_VALUE (a)) == TREE_LIST
231              && TREE_PURPOSE (TREE_VALUE (a)) == get_identifier ("mode"))
232       {
233         int i;
234         char *specified_name
235           = IDENTIFIER_POINTER (TREE_VALUE (TREE_VALUE (a)));
236
237         /* Give this decl a type with the specified mode.  */
238         for (i = 0; i < NUM_MACHINE_MODES; i++)
239           if (!strcmp (specified_name, GET_MODE_NAME (i)))
240             {
241               tree type
242                 = type_for_mode (i, TREE_UNSIGNED (TREE_TYPE (decl)));
243               if (type != 0)
244                 {
245                   TREE_TYPE (decl) = type;
246                   DECL_SIZE (decl) = 0;
247                   layout_decl (decl, 0);
248                 }
249               else
250                 error ("no data type for mode `%s'", specified_name);
251               break;
252             }
253         if (i == NUM_MACHINE_MODES)
254           error ("unknown machine mode `%s'", specified_name);
255       }
256     else if (TREE_VALUE (a) != 0
257              && TREE_CODE (TREE_VALUE (a)) == TREE_LIST
258              && TREE_PURPOSE (TREE_VALUE (a)) == get_identifier ("aligned"))
259       {
260         int align = TREE_INT_CST_LOW (TREE_VALUE (TREE_VALUE (a)))
261                     * BITS_PER_UNIT;
262         
263         if (exact_log2 (align) == -1)
264           error_with_decl (decl,
265                            "requested alignment of `%s' is not a power of 2");
266         else if (TREE_CODE (decl) != VAR_DECL
267                  && TREE_CODE (decl) != FIELD_DECL)
268           error_with_decl (decl,
269                            "alignment specified for `%s'");
270         else
271           DECL_ALIGN (decl) = align;
272       }
273     else if (TREE_VALUE (a) != 0
274              && TREE_CODE (TREE_VALUE (a)) == TREE_LIST
275              && TREE_PURPOSE (TREE_VALUE (a)) == get_identifier ("format"))
276       {
277         tree list = TREE_VALUE (TREE_VALUE (a));
278         tree format_type = TREE_PURPOSE (list);
279         int format_num = TREE_INT_CST_LOW (TREE_PURPOSE (TREE_VALUE (list)));
280         int first_arg_num = TREE_INT_CST_LOW (TREE_VALUE (TREE_VALUE (list)));
281         int is_scan;
282         tree argument;
283         int arg_num;
284         
285         if (TREE_CODE (decl) != FUNCTION_DECL)
286           {
287             error_with_decl (decl,
288                              "argument format specified for non-function `%s'");
289             return;
290           }
291         
292         if (format_type == get_identifier ("printf"))
293           is_scan = 0;
294         else if (format_type == get_identifier ("scanf"))
295           is_scan = 1;
296         else
297           {
298             error_with_decl (decl, "unrecognized format specifier for `%s'");
299             return;
300           }
301         
302         if (first_arg_num != 0 && first_arg_num <= format_num)
303           {
304             error_with_decl (decl,
305                 "format string arg follows the args to be formatted, for `%s'");
306             return;
307           }
308
309         /* Verify that the format_num argument is actually a string, in case
310            the format attribute is in error.  */
311         argument = TYPE_ARG_TYPES (TREE_TYPE (decl));
312         for (arg_num = 1; ; ++arg_num)
313           {
314             if (argument == 0 || arg_num == format_num)
315               break;
316             argument = TREE_CHAIN (argument);
317           }
318         if (! argument
319             || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
320             || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
321                 != char_type_node))
322           {
323             error_with_decl (decl,
324                              "format string arg not a string type, for `%s'");
325             return;
326           }
327         if (first_arg_num != 0)
328           {
329             /* Verify that first_arg_num points to the last arg, the ... */
330             while (argument)
331               arg_num++, argument = TREE_CHAIN (argument);
332             if (arg_num != first_arg_num)
333               {
334                 error_with_decl (decl,
335                                  "args to be formatted is not ..., for `%s'");
336                 return;
337               }
338           }
339
340         record_function_format (DECL_NAME (decl), DECL_ASSEMBLER_NAME (decl),
341                                 is_scan, format_num, first_arg_num);
342       }
343 }
344 \f
345 /* Check a printf/fprintf/sprintf/scanf/fscanf/sscanf format against
346    a parameter list.  */
347
348 #define T_I     &integer_type_node
349 #define T_L     &long_integer_type_node
350 #define T_S     &short_integer_type_node
351 #define T_UI    &unsigned_type_node
352 #define T_UL    &long_unsigned_type_node
353 #define T_US    &short_unsigned_type_node
354 #define T_F     &float_type_node
355 #define T_D     &double_type_node
356 #define T_LD    &long_double_type_node
357 #define T_C     &char_type_node
358 #define T_V     &void_type_node
359 #define T_W     &wchar_type_node
360
361 typedef struct {
362   char *format_chars;
363   int pointer_count;
364   /* Type of argument if no length modifier is used.  */
365   tree *nolen;
366   /* Type of argument if length modifier for shortening is used.
367      If NULL, then this modifier is not allowed.  */
368   tree *hlen;
369   /* Type of argument if length modifier `l' is used.
370      If NULL, then this modifier is not allowed.  */
371   tree *llen;
372   /* Type of argument if length modifier `L' is used.
373      If NULL, then this modifier is not allowed.  */
374   tree *bigllen;
375   /* List of other modifier characters allowed with these options.  */
376   char *flag_chars;
377 } format_char_info;
378
379 static format_char_info print_char_table[] = {
380   { "di",       0,      T_I,    T_I,    T_L,    NULL,   "-wp0 +"        },
381   { "oxX",      0,      T_UI,   T_UI,   T_UL,   NULL,   "-wp0#"         },
382   { "u",        0,      T_UI,   T_UI,   T_UL,   NULL,   "-wp0"          },
383   { "feEgG",    0,      T_D,    NULL,   NULL,   T_LD,   "-wp0 +#"       },
384   { "c",        0,      T_I,    NULL,   T_W,    NULL,   "-w"            },
385   { "C",        0,      T_W,    NULL,   NULL,   NULL,   "-w"            },
386   { "s",        1,      T_C,    NULL,   T_W,    NULL,   "-wp"           },
387   { "S",        1,      T_W,    NULL,   NULL,   NULL,   "-wp"           },
388   { "p",        1,      T_V,    NULL,   NULL,   NULL,   "-w"            },
389   { "n",        1,      T_I,    T_S,    T_L,    NULL,   ""              },
390   { NULL }
391 };
392
393 static format_char_info scan_char_table[] = {
394   { "di",       1,      T_I,    T_S,    T_L,    NULL,   "*"     },
395   { "ouxX",     1,      T_UI,   T_US,   T_UL,   NULL,   "*"     },      
396   { "efgEG",    1,      T_F,    NULL,   T_D,    T_LD,   "*"     },
397   { "sc",       1,      T_C,    NULL,   T_W,    NULL,   "*"     },
398   { "[",        1,      T_C,    NULL,   NULL,   NULL,   "*"     },
399   { "C",        1,      T_W,    NULL,   NULL,   NULL,   "*"     },
400   { "S",        1,      T_W,    NULL,   NULL,   NULL,   "*"     },
401   { "p",        2,      T_V,    NULL,   NULL,   NULL,   "*"     },
402   { "n",        1,      T_I,    T_S,    T_L,    NULL,   ""      },
403   { NULL }
404 };
405
406 typedef struct function_format_info {
407   struct function_format_info *next;  /* next structure on the list */
408   tree name;                    /* identifier such as "printf" */
409   tree assembler_name;          /* optional mangled identifier (for C++) */
410   int is_scan;                  /* TRUE if *scanf */
411   int format_num;               /* number of format argument */
412   int first_arg_num;            /* number of first arg (zero for varargs) */
413 } function_format_info;
414
415 static function_format_info *function_format_list = NULL;
416
417 static void check_format_info PROTO((function_format_info *, tree));
418
419 /* Initialize the table of functions to perform format checking on.
420    The ANSI functions are always checked (whether <stdio.h> is
421    included or not), since it is common to call printf without
422    including <stdio.h>.  There shouldn't be a problem with this,
423    since ANSI reserves these function names whether you include the
424    header file or not.  In any case, the checking is harmless.  */
425
426 void
427 init_function_format_info ()
428 {
429   record_function_format (get_identifier ("printf"), NULL_TREE, 0, 1, 2);
430   record_function_format (get_identifier ("fprintf"), NULL_TREE, 0, 2, 3);
431   record_function_format (get_identifier ("sprintf"), NULL_TREE, 0, 2, 3);
432   record_function_format (get_identifier ("scanf"), NULL_TREE, 1, 1, 2);
433   record_function_format (get_identifier ("fscanf"), NULL_TREE, 1, 2, 3);
434   record_function_format (get_identifier ("sscanf"), NULL_TREE, 1, 2, 3);
435   record_function_format (get_identifier ("vprintf"), NULL_TREE, 0, 1, 0);
436   record_function_format (get_identifier ("vfprintf"), NULL_TREE, 0, 2, 0);
437   record_function_format (get_identifier ("vsprintf"), NULL_TREE, 0, 2, 0);
438 }
439
440 /* Record information for argument format checking.  FUNCTION_IDENT is
441    the identifier node for the name of the function to check (its decl
442    need not exist yet).  IS_SCAN is true for scanf-type format checking;
443    false indicates printf-style format checking.  FORMAT_NUM is the number
444    of the argument which is the format control string (starting from 1).
445    FIRST_ARG_NUM is the number of the first actual argument to check
446    against teh format string, or zero if no checking is not be done
447    (e.g. for varargs such as vfprintf).  */
448
449 void
450 record_function_format (name, assembler_name, is_scan,
451                         format_num, first_arg_num)
452       tree name;
453       tree assembler_name;
454       int is_scan;
455       int format_num;
456       int first_arg_num;
457 {
458   function_format_info *info;
459
460   /* Re-use existing structure if it's there.  */
461
462   for (info = function_format_list; info; info = info->next)
463     {
464       if (info->name == name && info->assembler_name == assembler_name)
465         break;
466     }
467   if (! info)
468     {
469       info = (function_format_info *) xmalloc (sizeof (function_format_info));
470       info->next = function_format_list;
471       function_format_list = info;
472
473       info->name = name;
474       info->assembler_name = assembler_name;
475     }
476
477   info->is_scan = is_scan;
478   info->format_num = format_num;
479   info->first_arg_num = first_arg_num;
480 }
481
482 static char     tfaff[] = "too few arguments for format";
483 \f
484 /* Check the argument list of a call to printf, scanf, etc.
485    NAME is the function identifier.
486    ASSEMBLER_NAME is the function's assembler identifier.
487    (Either NAME or ASSEMBLER_NAME, but not both, may be NULL_TREE.)
488    PARAMS is the list of argument values.  */
489
490 void
491 check_function_format (name, assembler_name, params)
492      tree name;
493      tree assembler_name;
494      tree params;
495 {
496   function_format_info *info;
497
498   /* See if this function is a format function.  */
499   for (info = function_format_list; info; info = info->next)
500     {
501       if (info->assembler_name
502           ? (info->assembler_name == assembler_name)
503           : (info->name == name))
504         {
505           /* Yup; check it.  */
506           check_format_info (info, params);
507           break;
508         }
509     }
510 }
511
512 /* Check the argument list of a call to printf, scanf, etc.
513    INFO points to the function_format_info structure.
514    PARAMS is the list of argument values.  */
515
516 static void
517 check_format_info (info, params)
518      function_format_info *info;
519      tree params;
520 {
521   int i;
522   int arg_num;
523   int suppressed, wide, precise;
524   int length_char;
525   int format_char;
526   int format_length;
527   tree format_tree;
528   tree cur_param;
529   tree cur_type;
530   tree wanted_type;
531   char *format_chars;
532   format_char_info *fci;
533   static char message[132];
534   char flag_chars[8];
535
536   /* Skip to format argument.  If the argument isn't available, there's
537      no work for us to do; prototype checking will catch the problem.  */
538   for (arg_num = 1; ; ++arg_num)
539     {
540       if (params == 0)
541         return;
542       if (arg_num == info->format_num)
543         break;
544       params = TREE_CHAIN (params);
545     }
546   format_tree = TREE_VALUE (params);
547   params = TREE_CHAIN (params);
548   if (format_tree == 0)
549     return;
550   /* We can only check the format if it's a string constant.  */
551   while (TREE_CODE (format_tree) == NOP_EXPR)
552     format_tree = TREE_OPERAND (format_tree, 0); /* strip coercion */
553   if (format_tree == null_pointer_node)
554     {
555       warning ("null format string");
556       return;
557     }
558   if (TREE_CODE (format_tree) != ADDR_EXPR)
559     return;
560   format_tree = TREE_OPERAND (format_tree, 0);
561   if (TREE_CODE (format_tree) != STRING_CST)
562     return;
563   format_chars = TREE_STRING_POINTER (format_tree);
564   format_length = TREE_STRING_LENGTH (format_tree);
565   if (format_length <= 1)
566     warning ("zero-length format string");
567   if (format_chars[--format_length] != 0)
568     {
569       warning ("unterminated format string");
570       return;
571     }
572   /* Skip to first argument to check.  */
573   while (arg_num + 1 < info->first_arg_num)
574     {
575       if (params == 0)
576         return;
577       params = TREE_CHAIN (params);
578       ++arg_num;
579     }
580   while (1)
581     {
582       if (*format_chars == 0)
583         {
584           if (format_chars - TREE_STRING_POINTER (format_tree) != format_length)
585             warning ("embedded `\\0' in format");
586           if (info->first_arg_num != 0 && params != 0)
587             warning ("too many arguments for format");
588           return;
589         }
590       if (*format_chars++ != '%')
591         continue;
592       if (*format_chars == 0)
593         {
594           warning ("spurious trailing `%%' in format");
595           continue;
596         }
597       if (*format_chars == '%')
598         {
599           ++format_chars;
600           continue;
601         }
602       flag_chars[0] = 0;
603       suppressed = wide = precise = FALSE;
604       if (info->is_scan)
605         {
606           suppressed = *format_chars == '*';
607           if (suppressed)
608             ++format_chars;
609           while (isdigit (*format_chars))
610             ++format_chars;
611         }
612       else
613         {
614           while (*format_chars != 0 && index (" +#0-", *format_chars) != 0)
615             {
616               if (index (flag_chars, *format_chars) != 0)
617                 {
618                   sprintf (message, "repeated `%c' flag in format",
619                            *format_chars);
620                   warning (message);
621                 }
622               i = strlen (flag_chars);
623               flag_chars[i++] = *format_chars++;
624               flag_chars[i] = 0;
625             }
626           /* "If the space and + flags both appear, 
627              the space flag will be ignored."  */
628           if (index (flag_chars, ' ') != 0
629               && index (flag_chars, '+') != 0)
630             warning ("use of both ` ' and `+' flags in format");
631           /* "If the 0 and - flags both appear,
632              the 0 flag will be ignored."  */
633           if (index (flag_chars, '0') != 0
634               && index (flag_chars, '-') != 0)
635             warning ("use of both `0' and `-' flags in format");
636           if (*format_chars == '*')
637             {
638               wide = TRUE;
639               /* "...a field width...may be indicated by an asterisk.
640                  In this case, an int argument supplies the field width..."  */
641               ++format_chars;
642               if (params == 0)
643                 {
644                   warning (tfaff);
645                   return;
646                 }
647               if (info->first_arg_num != 0)
648                 {
649                   cur_param = TREE_VALUE (params);
650                   params = TREE_CHAIN (params);
651                   ++arg_num;
652                   /* size_t is generally not valid here.
653                      It will work on most machines, because size_t and int
654                      have the same mode.  But might as well warn anyway,
655                      since it will fail on other machines.  */
656                   if (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
657                       != integer_type_node)
658                     {
659                       sprintf (message,
660                                "field width is not type int (arg %d)",
661                                arg_num);
662                       warning (message);
663                     }
664                 }
665             }
666           else
667             {
668               while (isdigit (*format_chars))
669                 {
670                   wide = TRUE;
671                   ++format_chars;
672                 }
673             }
674           if (*format_chars == '.')
675             {
676               precise = TRUE;
677               ++format_chars;
678               if (*format_chars != '*' && !isdigit (*format_chars))
679                 warning ("`.' not followed by `*' or digit in format");
680               /* "...a...precision...may be indicated by an asterisk.
681                  In this case, an int argument supplies the...precision."  */
682               if (*format_chars == '*')
683                 {
684                   if (info->first_arg_num != 0)
685                     {
686                       ++format_chars;
687                       if (params == 0)
688                         {
689                           warning (tfaff);
690                           return;
691                         }
692                       cur_param = TREE_VALUE (params);
693                       params = TREE_CHAIN (params);
694                       ++arg_num;
695                       if (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
696                           != integer_type_node)
697                         {
698                           sprintf (message,
699                                    "field width is not type int (arg %d)",
700                                    arg_num);
701                           warning (message);
702                         }
703                     }
704                 }
705               else
706                 {
707                   while (isdigit (*format_chars))
708                     ++format_chars;
709                 }
710             }
711         }
712       if (*format_chars == 'h' || *format_chars == 'l' || *format_chars == 'L')
713         length_char = *format_chars++;
714       else
715         length_char = 0;
716       if (suppressed && length_char != 0)
717         {
718           sprintf (message,
719                    "use of `*' and `%c' together in format",
720                    length_char);
721           warning (message);
722         }
723       format_char = *format_chars;
724       if (format_char == 0)
725         {
726           warning ("conversion lacks type at end of format");
727           continue;
728         }
729       format_chars++;
730       fci = info->is_scan ? scan_char_table : print_char_table;
731       while (fci->format_chars != 0
732              && index (fci->format_chars, format_char) == 0)
733           ++fci;
734       if (fci->format_chars == 0)
735         {
736           if (format_char >= 040 && format_char < 0177)
737             sprintf (message,
738                      "unknown conversion type character `%c' in format",
739                      format_char);
740           else
741             sprintf (message,
742                      "unknown conversion type character 0x%x in format",
743                      format_char);
744           warning (message);
745           continue;
746         }
747       if (wide && index (fci->flag_chars, 'w') == 0)
748         {
749           sprintf (message, "width used with `%c' format",
750                    format_char);
751           warning (message);
752         }
753       if (precise && index (fci->flag_chars, 'p') == 0)
754         {
755           sprintf (message, "precision used with `%c' format",
756                    format_char);
757           warning (message);
758         }
759       if (info->is_scan && format_char == '[')
760         {
761           /* Skip over scan set, in case it happens to have '%' in it.  */
762           if (*format_chars == '^')
763             ++format_chars;
764           /* Find closing bracket; if one is hit immediately, then
765              it's part of the scan set rather than a terminator.  */
766           if (*format_chars == ']')
767             ++format_chars;
768           while (*format_chars && *format_chars != ']')
769             ++format_chars;
770           if (*format_chars != ']')
771               /* The end of the format string was reached.  */
772               warning ("no closing `]' for `%%[' format");
773         }
774       if (suppressed)
775         {
776           if (index (fci->flag_chars, '*') == 0)
777             {
778               sprintf (message,
779                        "suppression of `%c' conversion in format",
780                        format_char);
781               warning (message);
782             }
783           continue;
784         }
785       for (i = 0; flag_chars[i] != 0; ++i)
786         {
787           if (index (fci->flag_chars, flag_chars[i]) == 0)
788             {
789               sprintf (message, "flag `%c' used with type `%c'",
790                        flag_chars[i], format_char);
791               warning (message);
792             }
793         }
794       if (precise && index (flag_chars, '0') != 0
795           && (format_char == 'd' || format_char == 'i'
796               || format_char == 'o' || format_char == 'u'
797               || format_char == 'x' || format_char == 'x'))
798         {
799           sprintf (message,
800                    "precision and `0' flag not both allowed with `%c' format",
801                    format_char);
802           warning (message);
803         }
804       switch (length_char)
805         {
806         default: wanted_type = fci->nolen ? *(fci->nolen) : 0; break;
807         case 'h': wanted_type = fci->hlen ? *(fci->hlen) : 0; break;
808         case 'l': wanted_type = fci->llen ? *(fci->llen) : 0; break;
809         case 'L': wanted_type = fci->bigllen ? *(fci->bigllen) : 0; break;
810         }
811       if (wanted_type == 0)
812         {
813           sprintf (message,
814                    "use of `%c' length character with `%c' type character",
815                    length_char, format_char);
816           warning (message);
817         }
818
819       /*
820        ** XXX -- should kvetch about stuff such as
821        **       {
822        **               const int       i;
823        **
824        **               scanf ("%d", &i);
825        **       }
826        */
827
828       /* Finally. . .check type of argument against desired type!  */
829       if (info->first_arg_num == 0)
830         continue;
831       if (params == 0)
832         {
833           warning (tfaff);
834           return;
835         }
836       cur_param = TREE_VALUE (params);
837       params = TREE_CHAIN (params);
838       ++arg_num;
839       cur_type = TREE_TYPE (cur_param);
840
841       /* Check the types of any additional pointer arguments
842          that precede the "real" argument.  */
843       for (i = 0; i < fci->pointer_count; ++i)
844         {
845           if (TREE_CODE (cur_type) == POINTER_TYPE)
846             {
847               cur_type = TREE_TYPE (cur_type);
848               continue;
849             }
850           sprintf (message,
851                    "format argument is not a %s (arg %d)",
852                    ((fci->pointer_count == 1) ? "pointer" : "pointer to a pointer"),
853                    arg_num);
854           warning (message);
855           break;
856         }
857
858       /* Check the type of the "real" argument, if there's a type we want.  */
859       if (i == fci->pointer_count && wanted_type != 0
860           && wanted_type != TYPE_MAIN_VARIANT (cur_type)
861           /* If we want `void *', allow any pointer type.
862              (Anything else would already have got a warning.)  */
863           && ! (wanted_type == void_type_node
864                 && fci->pointer_count > 0)
865           /* Don't warn about differences merely in signedness.  */
866           && !(TREE_CODE (wanted_type) == INTEGER_TYPE
867                && TREE_CODE (cur_type) == INTEGER_TYPE
868                && (TREE_UNSIGNED (wanted_type)
869                    ? wanted_type == unsigned_type (cur_type)
870                    : wanted_type == signed_type (cur_type))))
871         {
872           register char *this;
873           register char *that;
874   
875           this = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (wanted_type)));
876           that = 0;
877           if (TREE_CODE (cur_type) != ERROR_MARK
878               && TYPE_NAME (cur_type) != 0
879               && TREE_CODE (cur_type) != INTEGER_TYPE
880               && !(TREE_CODE (cur_type) == POINTER_TYPE
881                    && TREE_CODE (TREE_TYPE (cur_type)) == INTEGER_TYPE))
882             {
883               if (TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL
884                   && DECL_NAME (TYPE_NAME (cur_type)) != 0)
885                 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
886               else
887                 that = IDENTIFIER_POINTER (TYPE_NAME (cur_type));
888             }
889
890           /* A nameless type can't possibly match what the format wants.
891              So there will be a warning for it.
892              Make up a string to describe vaguely what it is.  */
893           if (that == 0)
894             {
895               if (TREE_CODE (cur_type) == POINTER_TYPE)
896                 that = "pointer";
897               else
898                 that = "different type";
899             }
900
901           if (strcmp (this, that) != 0)
902             {
903               sprintf (message, "%s format, %s arg (arg %d)",
904                         this, that, arg_num);
905               warning (message);
906             }
907         }
908     }
909 }
910 \f
911 /* Print a warning if a constant expression had overflow in folding.
912    Invoke this function on every expression that the language
913    requires to be a constant expression.
914    Note the ANSI C standard says it is erroneous for a
915    constant expression to overflow.  */
916
917 void
918 constant_expression_warning (value)
919      tree value;
920 {
921   if (TREE_CODE (value) == INTEGER_CST && TREE_CONSTANT_OVERFLOW (value))
922     if (pedantic)
923       pedwarn ("overflow in constant expression");
924 }
925
926 /* Print a warning if an expression had overflow in folding.
927    Invoke this function on every expression that
928    (1) appears in the source code, and
929    (2) might be a constant expression that overflowed, and
930    (3) is not already checked by convert_and_check;
931    however, do not invoke this function on operands of explicit casts.  */
932
933 void
934 overflow_warning (value)
935      tree value;
936 {
937   if (TREE_CODE (value) == INTEGER_CST && TREE_OVERFLOW (value))
938     {
939       TREE_OVERFLOW (value) = 0;
940       warning ("integer overflow in expression");
941     }
942 }
943
944 /* Print a warning if a large constant is truncated to unsigned,
945    or if -Wconversion is used and a constant < 0 is converted to unsigned.
946    Invoke this function on every expression that might be implicitly
947    converted to an unsigned type.  */
948
949 void
950 unsigned_conversion_warning (result, operand)
951      tree result, operand;
952 {
953   if (TREE_CODE (operand) == INTEGER_CST
954       && TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
955       && TREE_UNSIGNED (TREE_TYPE (result))
956       && !int_fits_type_p (operand, TREE_TYPE (result)))
957     {
958       if (!int_fits_type_p (operand, signed_type (TREE_TYPE (result))))
959         /* This detects cases like converting -129 or 256 to unsigned char.  */
960         warning ("large integer implicitly truncated to unsigned type");
961       else if (warn_conversion)
962         warning ("negative integer implicitly converted to unsigned type");
963     }
964 }
965
966 /* Convert EXPR to TYPE, warning about conversion problems with constants.
967    Invoke this function on every expression that is converted implicitly,
968    i.e. because of language rules and not because of an explicit cast.  */
969
970 tree
971 convert_and_check (type, expr)
972      tree type, expr;
973 {
974   tree t = convert (type, expr);
975   if (TREE_CODE (t) == INTEGER_CST)
976     {
977       if (TREE_OVERFLOW (t))
978         {
979           TREE_OVERFLOW (t) = 0;
980
981           /* No warning for converting 0x80000000 to int.  */
982           if (!(TREE_UNSIGNED (type) < TREE_UNSIGNED (TREE_TYPE (expr))
983                 && TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
984                 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (expr))))
985             warning ("overflow in implicit constant conversion");
986         }
987       else
988         unsigned_conversion_warning (t, expr);
989     }
990   return t;
991 }
992 \f
993 void
994 c_expand_expr_stmt (expr)
995      tree expr;
996 {
997   /* Do default conversion if safe and possibly important,
998      in case within ({...}).  */
999   if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE && lvalue_p (expr))
1000       || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
1001     expr = default_conversion (expr);
1002
1003   if (TREE_TYPE (expr) != error_mark_node
1004       && TYPE_SIZE (TREE_TYPE (expr)) == 0
1005       && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
1006     error ("expression statement has incomplete type");
1007
1008   expand_expr_stmt (expr);
1009 }
1010 \f
1011 /* Validate the expression after `case' and apply default promotions.  */
1012
1013 tree
1014 check_case_value (value)
1015      tree value;
1016 {
1017   if (value == NULL_TREE)
1018     return value;
1019
1020   /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue.  */
1021   STRIP_TYPE_NOPS (value);
1022
1023   if (TREE_CODE (value) != INTEGER_CST
1024       && value != error_mark_node)
1025     {
1026       error ("case label does not reduce to an integer constant");
1027       value = error_mark_node;
1028     }
1029   else
1030     /* Promote char or short to int.  */
1031     value = default_conversion (value);
1032
1033   constant_expression_warning (value);
1034
1035   return value;
1036 }
1037 \f
1038 /* Return an integer type with BITS bits of precision,
1039    that is unsigned if UNSIGNEDP is nonzero, otherwise signed.  */
1040
1041 tree
1042 type_for_size (bits, unsignedp)
1043      unsigned bits;
1044      int unsignedp;
1045 {
1046   if (bits == TYPE_PRECISION (signed_char_type_node))
1047     return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1048
1049   if (bits == TYPE_PRECISION (short_integer_type_node))
1050     return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1051
1052   if (bits == TYPE_PRECISION (integer_type_node))
1053     return unsignedp ? unsigned_type_node : integer_type_node;
1054
1055   if (bits == TYPE_PRECISION (long_integer_type_node))
1056     return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1057
1058   if (bits == TYPE_PRECISION (long_long_integer_type_node))
1059     return (unsignedp ? long_long_unsigned_type_node
1060             : long_long_integer_type_node);
1061
1062   if (bits <= TYPE_PRECISION (intQI_type_node))
1063     return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
1064
1065   if (bits <= TYPE_PRECISION (intHI_type_node))
1066     return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
1067
1068   if (bits <= TYPE_PRECISION (intSI_type_node))
1069     return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
1070
1071   if (bits <= TYPE_PRECISION (intDI_type_node))
1072     return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
1073
1074   return 0;
1075 }
1076
1077 /* Return a data type that has machine mode MODE.
1078    If the mode is an integer,
1079    then UNSIGNEDP selects between signed and unsigned types.  */
1080
1081 tree
1082 type_for_mode (mode, unsignedp)
1083      enum machine_mode mode;
1084      int unsignedp;
1085 {
1086   if (mode == TYPE_MODE (signed_char_type_node))
1087     return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1088
1089   if (mode == TYPE_MODE (short_integer_type_node))
1090     return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1091
1092   if (mode == TYPE_MODE (integer_type_node))
1093     return unsignedp ? unsigned_type_node : integer_type_node;
1094
1095   if (mode == TYPE_MODE (long_integer_type_node))
1096     return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1097
1098   if (mode == TYPE_MODE (long_long_integer_type_node))
1099     return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
1100
1101   if (mode == TYPE_MODE (intQI_type_node))
1102     return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
1103
1104   if (mode == TYPE_MODE (intHI_type_node))
1105     return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
1106
1107   if (mode == TYPE_MODE (intSI_type_node))
1108     return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
1109
1110   if (mode == TYPE_MODE (intDI_type_node))
1111     return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
1112
1113   if (mode == TYPE_MODE (float_type_node))
1114     return float_type_node;
1115
1116   if (mode == TYPE_MODE (double_type_node))
1117     return double_type_node;
1118
1119   if (mode == TYPE_MODE (long_double_type_node))
1120     return long_double_type_node;
1121
1122   if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
1123     return build_pointer_type (char_type_node);
1124
1125   if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
1126     return build_pointer_type (integer_type_node);
1127
1128   return 0;
1129 }
1130 \f
1131 /* Print an error message for invalid operands to arith operation CODE.
1132    NOP_EXPR is used as a special case (see truthvalue_conversion).  */
1133
1134 void
1135 binary_op_error (code)
1136      enum tree_code code;
1137 {
1138   register char *opname;
1139   switch (code)
1140     {
1141     case NOP_EXPR:
1142       error ("invalid truth-value expression");
1143       return;
1144
1145     case PLUS_EXPR:
1146       opname = "+"; break;
1147     case MINUS_EXPR:
1148       opname = "-"; break;
1149     case MULT_EXPR:
1150       opname = "*"; break;
1151     case MAX_EXPR:
1152       opname = "max"; break;
1153     case MIN_EXPR:
1154       opname = "min"; break;
1155     case EQ_EXPR:
1156       opname = "=="; break;
1157     case NE_EXPR:
1158       opname = "!="; break;
1159     case LE_EXPR:
1160       opname = "<="; break;
1161     case GE_EXPR:
1162       opname = ">="; break;
1163     case LT_EXPR:
1164       opname = "<"; break;
1165     case GT_EXPR:
1166       opname = ">"; break;
1167     case LSHIFT_EXPR:
1168       opname = "<<"; break;
1169     case RSHIFT_EXPR:
1170       opname = ">>"; break;
1171     case TRUNC_MOD_EXPR:
1172     case FLOOR_MOD_EXPR:
1173       opname = "%"; break;
1174     case TRUNC_DIV_EXPR:
1175     case FLOOR_DIV_EXPR:
1176       opname = "/"; break;
1177     case BIT_AND_EXPR:
1178       opname = "&"; break;
1179     case BIT_IOR_EXPR:
1180       opname = "|"; break;
1181     case TRUTH_ANDIF_EXPR:
1182       opname = "&&"; break;
1183     case TRUTH_ORIF_EXPR:
1184       opname = "||"; break;
1185     case BIT_XOR_EXPR:
1186       opname = "^"; break;
1187     case LROTATE_EXPR:
1188     case RROTATE_EXPR:
1189       opname = "rotate"; break;
1190     }
1191   error ("invalid operands to binary %s", opname);
1192 }
1193 \f
1194 /* Subroutine of build_binary_op, used for comparison operations.
1195    See if the operands have both been converted from subword integer types
1196    and, if so, perhaps change them both back to their original type.
1197
1198    The arguments of this function are all pointers to local variables
1199    of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
1200    RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
1201
1202    If this function returns nonzero, it means that the comparison has
1203    a constant value.  What this function returns is an expression for
1204    that value.  */
1205
1206 tree
1207 shorten_compare (op0_ptr, op1_ptr, restype_ptr, rescode_ptr)
1208      tree *op0_ptr, *op1_ptr;
1209      tree *restype_ptr;
1210      enum tree_code *rescode_ptr;
1211 {
1212   register tree type;
1213   tree op0 = *op0_ptr;
1214   tree op1 = *op1_ptr;
1215   int unsignedp0, unsignedp1;
1216   int real1, real2;
1217   tree primop0, primop1;
1218   enum tree_code code = *rescode_ptr;
1219
1220   /* Throw away any conversions to wider types
1221      already present in the operands.  */
1222
1223   primop0 = get_narrower (op0, &unsignedp0);
1224   primop1 = get_narrower (op1, &unsignedp1);
1225
1226   /* Handle the case that OP0 does not *contain* a conversion
1227      but it *requires* conversion to FINAL_TYPE.  */
1228
1229   if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
1230     unsignedp0 = TREE_UNSIGNED (TREE_TYPE (op0));
1231   if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
1232     unsignedp1 = TREE_UNSIGNED (TREE_TYPE (op1));
1233
1234   /* If one of the operands must be floated, we cannot optimize.  */
1235   real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
1236   real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
1237
1238   /* If first arg is constant, swap the args (changing operation
1239      so value is preserved), for canonicalization.  */
1240
1241   if (TREE_CONSTANT (primop0))
1242     {
1243       register tree tem = primop0;
1244       register int temi = unsignedp0;
1245       primop0 = primop1;
1246       primop1 = tem;
1247       tem = op0;
1248       op0 = op1;
1249       op1 = tem;
1250       *op0_ptr = op0;
1251       *op1_ptr = op1;
1252       unsignedp0 = unsignedp1;
1253       unsignedp1 = temi;
1254       temi = real1;
1255       real1 = real2;
1256       real2 = temi;
1257
1258       switch (code)
1259         {
1260         case LT_EXPR:
1261           code = GT_EXPR;
1262           break;
1263         case GT_EXPR:
1264           code = LT_EXPR;
1265           break;
1266         case LE_EXPR:
1267           code = GE_EXPR;
1268           break;
1269         case GE_EXPR:
1270           code = LE_EXPR;
1271           break;
1272         }
1273       *rescode_ptr = code;
1274     }
1275
1276   /* If comparing an integer against a constant more bits wide,
1277      maybe we can deduce a value of 1 or 0 independent of the data.
1278      Or else truncate the constant now
1279      rather than extend the variable at run time.
1280
1281      This is only interesting if the constant is the wider arg.
1282      Also, it is not safe if the constant is unsigned and the
1283      variable arg is signed, since in this case the variable
1284      would be sign-extended and then regarded as unsigned.
1285      Our technique fails in this case because the lowest/highest
1286      possible unsigned results don't follow naturally from the
1287      lowest/highest possible values of the variable operand.
1288      For just EQ_EXPR and NE_EXPR there is another technique that
1289      could be used: see if the constant can be faithfully represented
1290      in the other operand's type, by truncating it and reextending it
1291      and see if that preserves the constant's value.  */
1292
1293   if (!real1 && !real2
1294       && TREE_CODE (primop1) == INTEGER_CST
1295       && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
1296     {
1297       int min_gt, max_gt, min_lt, max_lt;
1298       tree maxval, minval;
1299       /* 1 if comparison is nominally unsigned.  */
1300       int unsignedp = TREE_UNSIGNED (*restype_ptr);
1301       tree val;
1302
1303       type = signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0));
1304
1305       maxval = TYPE_MAX_VALUE (type);
1306       minval = TYPE_MIN_VALUE (type);
1307
1308       if (unsignedp && !unsignedp0)
1309         *restype_ptr = signed_type (*restype_ptr);
1310
1311       if (TREE_TYPE (primop1) != *restype_ptr)
1312         primop1 = convert (*restype_ptr, primop1);
1313       if (type != *restype_ptr)
1314         {
1315           minval = convert (*restype_ptr, minval);
1316           maxval = convert (*restype_ptr, maxval);
1317         }
1318
1319       if (unsignedp && unsignedp0)
1320         {
1321           min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
1322           max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
1323           min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
1324           max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
1325         }
1326       else
1327         {
1328           min_gt = INT_CST_LT (primop1, minval);
1329           max_gt = INT_CST_LT (primop1, maxval);
1330           min_lt = INT_CST_LT (minval, primop1);
1331           max_lt = INT_CST_LT (maxval, primop1);
1332         }
1333
1334       val = 0;
1335       /* This used to be a switch, but Genix compiler can't handle that.  */
1336       if (code == NE_EXPR)
1337         {
1338           if (max_lt || min_gt)
1339             val = integer_one_node;
1340         }
1341       else if (code == EQ_EXPR)
1342         {
1343           if (max_lt || min_gt)
1344             val = integer_zero_node;
1345         }
1346       else if (code == LT_EXPR)
1347         {
1348           if (max_lt)
1349             val = integer_one_node;
1350           if (!min_lt)
1351             val = integer_zero_node;
1352         }
1353       else if (code == GT_EXPR)
1354         {
1355           if (min_gt)
1356             val = integer_one_node;
1357           if (!max_gt)
1358             val = integer_zero_node;
1359         }
1360       else if (code == LE_EXPR)
1361         {
1362           if (!max_gt)
1363             val = integer_one_node;
1364           if (min_gt)
1365             val = integer_zero_node;
1366         }
1367       else if (code == GE_EXPR)
1368         {
1369           if (!min_lt)
1370             val = integer_one_node;
1371           if (max_lt)
1372             val = integer_zero_node;
1373         }
1374
1375       /* If primop0 was sign-extended and unsigned comparison specd,
1376          we did a signed comparison above using the signed type bounds.
1377          But the comparison we output must be unsigned.
1378
1379          Also, for inequalities, VAL is no good; but if the signed
1380          comparison had *any* fixed result, it follows that the
1381          unsigned comparison just tests the sign in reverse
1382          (positive values are LE, negative ones GE).
1383          So we can generate an unsigned comparison
1384          against an extreme value of the signed type.  */
1385
1386       if (unsignedp && !unsignedp0)
1387         {
1388           if (val != 0)
1389             switch (code)
1390               {
1391               case LT_EXPR:
1392               case GE_EXPR:
1393                 primop1 = TYPE_MIN_VALUE (type);
1394                 val = 0;
1395                 break;
1396
1397               case LE_EXPR:
1398               case GT_EXPR:
1399                 primop1 = TYPE_MAX_VALUE (type);
1400                 val = 0;
1401                 break;
1402               }
1403           type = unsigned_type (type);
1404         }
1405
1406       if (!max_gt && !unsignedp0)
1407         {
1408           /* This is the case of (char)x >?< 0x80, which people used to use
1409              expecting old C compilers to change the 0x80 into -0x80.  */
1410           if (val == integer_zero_node)
1411             warning ("comparison is always 0 due to limited range of data type");
1412           if (val == integer_one_node)
1413             warning ("comparison is always 1 due to limited range of data type");
1414         }
1415
1416       if (!min_lt && unsignedp0)
1417         {
1418           /* This is the case of (unsigned char)x >?< -1 or < 0.  */
1419           if (val == integer_zero_node)
1420             warning ("comparison is always 0 due to limited range of data type");
1421           if (val == integer_one_node)
1422             warning ("comparison is always 1 due to limited range of data type");
1423         }
1424
1425       if (val != 0)
1426         {
1427           /* Don't forget to evaluate PRIMOP0 if it has side effects.  */
1428           if (TREE_SIDE_EFFECTS (primop0))
1429             return build (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
1430           return val;
1431         }
1432
1433       /* Value is not predetermined, but do the comparison
1434          in the type of the operand that is not constant.
1435          TYPE is already properly set.  */
1436     }
1437   else if (real1 && real2
1438            && (TYPE_PRECISION (TREE_TYPE (primop0))
1439                == TYPE_PRECISION (TREE_TYPE (primop1))))
1440     type = TREE_TYPE (primop0);
1441
1442   /* If args' natural types are both narrower than nominal type
1443      and both extend in the same manner, compare them
1444      in the type of the wider arg.
1445      Otherwise must actually extend both to the nominal
1446      common type lest different ways of extending
1447      alter the result.
1448      (eg, (short)-1 == (unsigned short)-1  should be 0.)  */
1449
1450   else if (unsignedp0 == unsignedp1 && real1 == real2
1451            && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
1452            && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
1453     {
1454       type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
1455       type = signed_or_unsigned_type (unsignedp0
1456                                       || TREE_UNSIGNED (*restype_ptr),
1457                                       type);
1458       /* Make sure shorter operand is extended the right way
1459          to match the longer operand.  */
1460       primop0 = convert (signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0)),
1461                          primop0);
1462       primop1 = convert (signed_or_unsigned_type (unsignedp1, TREE_TYPE (primop1)),
1463                          primop1);
1464     }
1465   else
1466     {
1467       /* Here we must do the comparison on the nominal type
1468          using the args exactly as we received them.  */
1469       type = *restype_ptr;
1470       primop0 = op0;
1471       primop1 = op1;
1472
1473       if (!real1 && !real2 && integer_zerop (primop1)
1474           && TREE_UNSIGNED (TREE_TYPE (primop0)))
1475         {
1476           tree value = 0;
1477           switch (code)
1478             {
1479             case GE_EXPR:
1480               if (extra_warnings)
1481                 warning ("unsigned value >= 0 is always 1");
1482               value = integer_one_node;
1483               break;
1484
1485             case LT_EXPR:
1486               if (extra_warnings)
1487                 warning ("unsigned value < 0 is always 0");
1488               value = integer_zero_node;
1489             }
1490
1491           if (value != 0)
1492             {
1493               /* Don't forget to evaluate PRIMOP0 if it has side effects.  */
1494               if (TREE_SIDE_EFFECTS (primop0))
1495                 return build (COMPOUND_EXPR, TREE_TYPE (value),
1496                               primop0, value);
1497               return value;
1498             }
1499         }
1500     }
1501
1502   *op0_ptr = convert (type, primop0);
1503   *op1_ptr = convert (type, primop1);
1504
1505   *restype_ptr = integer_type_node;
1506
1507   return 0;
1508 }
1509 \f
1510 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
1511    or validate its data type for an `if' or `while' statement or ?..: exp.
1512
1513    This preparation consists of taking the ordinary
1514    representation of an expression expr and producing a valid tree
1515    boolean expression describing whether expr is nonzero.  We could
1516    simply always do build_binary_op (NE_EXPR, expr, integer_zero_node, 1),
1517    but we optimize comparisons, &&, ||, and !.
1518
1519    The resulting type should always be `integer_type_node'.  */
1520
1521 tree
1522 truthvalue_conversion (expr)
1523      tree expr;
1524 {
1525   register enum tree_code code;
1526
1527   if (TREE_CODE (expr) == ERROR_MARK)
1528     return expr;
1529
1530 #if 0 /* This appears to be wrong for C++.  */
1531   /* These really should return error_mark_node after 2.4 is stable.
1532      But not all callers handle ERROR_MARK properly.  */
1533   switch (TREE_CODE (TREE_TYPE (expr)))
1534     {
1535     case RECORD_TYPE:
1536       error ("struct type value used where scalar is required");
1537       return integer_zero_node;
1538
1539     case UNION_TYPE:
1540       error ("union type value used where scalar is required");
1541       return integer_zero_node;
1542
1543     case ARRAY_TYPE:
1544       error ("array type value used where scalar is required");
1545       return integer_zero_node;
1546
1547     default:
1548       break;
1549     }
1550 #endif /* 0 */
1551
1552   switch (TREE_CODE (expr))
1553     {
1554       /* It is simpler and generates better code to have only TRUTH_*_EXPR
1555          or comparison expressions as truth values at this level.  */
1556 #if 0
1557     case COMPONENT_REF:
1558       /* A one-bit unsigned bit-field is already acceptable.  */
1559       if (1 == TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (expr, 1)))
1560           && TREE_UNSIGNED (TREE_OPERAND (expr, 1)))
1561         return expr;
1562       break;
1563 #endif
1564
1565     case EQ_EXPR:
1566       /* It is simpler and generates better code to have only TRUTH_*_EXPR
1567          or comparison expressions as truth values at this level.  */
1568 #if 0
1569       if (integer_zerop (TREE_OPERAND (expr, 1)))
1570         return build_unary_op (TRUTH_NOT_EXPR, TREE_OPERAND (expr, 0), 0);
1571 #endif
1572     case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
1573     case TRUTH_ANDIF_EXPR:
1574     case TRUTH_ORIF_EXPR:
1575     case TRUTH_AND_EXPR:
1576     case TRUTH_OR_EXPR:
1577     case TRUTH_XOR_EXPR:
1578     case ERROR_MARK:
1579       return expr;
1580
1581     case INTEGER_CST:
1582       return integer_zerop (expr) ? integer_zero_node : integer_one_node;
1583
1584     case REAL_CST:
1585       return real_zerop (expr) ? integer_zero_node : integer_one_node;
1586
1587     case ADDR_EXPR:
1588       if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0)))
1589         return build (COMPOUND_EXPR, integer_type_node,
1590                       TREE_OPERAND (expr, 0), integer_one_node);
1591       else
1592         return integer_one_node;
1593
1594     case COMPLEX_EXPR:
1595       return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
1596                                ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
1597                               truthvalue_conversion (TREE_OPERAND (expr, 0)),
1598                               truthvalue_conversion (TREE_OPERAND (expr, 1)),
1599                               0);
1600
1601     case NEGATE_EXPR:
1602     case ABS_EXPR:
1603     case FLOAT_EXPR:
1604     case FFS_EXPR:
1605       /* These don't change whether an object is non-zero or zero.  */
1606       return truthvalue_conversion (TREE_OPERAND (expr, 0));
1607
1608     case LROTATE_EXPR:
1609     case RROTATE_EXPR:
1610       /* These don't change whether an object is zero or non-zero, but
1611          we can't ignore them if their second arg has side-effects.  */
1612       if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
1613         return build (COMPOUND_EXPR, integer_type_node, TREE_OPERAND (expr, 1),
1614                       truthvalue_conversion (TREE_OPERAND (expr, 0)));
1615       else
1616         return truthvalue_conversion (TREE_OPERAND (expr, 0));
1617       
1618     case COND_EXPR:
1619       /* Distribute the conversion into the arms of a COND_EXPR.  */
1620       return fold (build (COND_EXPR, integer_type_node, TREE_OPERAND (expr, 0),
1621                           truthvalue_conversion (TREE_OPERAND (expr, 1)),
1622                           truthvalue_conversion (TREE_OPERAND (expr, 2))));
1623
1624     case CONVERT_EXPR:
1625       /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
1626          since that affects how `default_conversion' will behave.  */
1627       if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE
1628           || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
1629         break;
1630       /* fall through... */
1631     case NOP_EXPR:
1632       /* If this is widening the argument, we can ignore it.  */
1633       if (TYPE_PRECISION (TREE_TYPE (expr))
1634           >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
1635         return truthvalue_conversion (TREE_OPERAND (expr, 0));
1636       break;
1637
1638     case MINUS_EXPR:
1639       /* With IEEE arithmetic, x - x may not equal 0, so we can't optimize
1640          this case.  */
1641       if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
1642           && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE)
1643         break;
1644       /* fall through... */
1645     case BIT_XOR_EXPR:
1646       /* This and MINUS_EXPR can be changed into a comparison of the
1647          two objects.  */
1648       if (TREE_TYPE (TREE_OPERAND (expr, 0))
1649           == TREE_TYPE (TREE_OPERAND (expr, 1)))
1650         return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
1651                                 TREE_OPERAND (expr, 1), 1);
1652       return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
1653                               fold (build1 (NOP_EXPR,
1654                                             TREE_TYPE (TREE_OPERAND (expr, 0)),
1655                                             TREE_OPERAND (expr, 1))), 1);
1656
1657     case MODIFY_EXPR:
1658       if (warn_parentheses && C_EXP_ORIGINAL_CODE (expr) == MODIFY_EXPR)
1659         warning ("suggest parentheses around assignment used as truth value");
1660       break;
1661     }
1662
1663   if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
1664     return (build_binary_op
1665             ((TREE_SIDE_EFFECTS (expr)
1666               ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
1667              truthvalue_conversion (build_unary_op (REALPART_EXPR, expr, 0)),
1668              truthvalue_conversion (build_unary_op (IMAGPART_EXPR, expr, 0)),
1669              0));
1670
1671   return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
1672 }
1673 \f
1674 /* Read the rest of a #-directive from input stream FINPUT.
1675    In normal use, the directive name and the white space after it
1676    have already been read, so they won't be included in the result.
1677    We allow for the fact that the directive line may contain
1678    a newline embedded within a character or string literal which forms
1679    a part of the directive.
1680
1681    The value is a string in a reusable buffer.  It remains valid
1682    only until the next time this function is called.  */
1683
1684 char *
1685 get_directive_line (finput)
1686      register FILE *finput;
1687 {
1688   static char *directive_buffer = NULL;
1689   static unsigned buffer_length = 0;
1690   register char *p;
1691   register char *buffer_limit;
1692   register int looking_for = 0;
1693   register int char_escaped = 0;
1694
1695   if (buffer_length == 0)
1696     {
1697       directive_buffer = (char *)xmalloc (128);
1698       buffer_length = 128;
1699     }
1700
1701   buffer_limit = &directive_buffer[buffer_length];
1702
1703   for (p = directive_buffer; ; )
1704     {
1705       int c;
1706
1707       /* Make buffer bigger if it is full.  */
1708       if (p >= buffer_limit)
1709         {
1710           register unsigned bytes_used = (p - directive_buffer);
1711
1712           buffer_length *= 2;
1713           directive_buffer
1714             = (char *)xrealloc (directive_buffer, buffer_length);
1715           p = &directive_buffer[bytes_used];
1716           buffer_limit = &directive_buffer[buffer_length];
1717         }
1718
1719       c = getc (finput);
1720
1721       /* Discard initial whitespace.  */
1722       if ((c == ' ' || c == '\t') && p == directive_buffer)
1723         continue;
1724
1725       /* Detect the end of the directive.  */
1726       if (c == '\n' && looking_for == 0)
1727         {
1728           ungetc (c, finput);
1729           c = '\0';
1730         }
1731
1732       *p++ = c;
1733
1734       if (c == 0)
1735         return directive_buffer;
1736
1737       /* Handle string and character constant syntax.  */
1738       if (looking_for)
1739         {
1740           if (looking_for == c && !char_escaped)
1741             looking_for = 0;    /* Found terminator... stop looking.  */
1742         }
1743       else
1744         if (c == '\'' || c == '"')
1745           looking_for = c;      /* Don't stop buffering until we see another
1746                                    another one of these (or an EOF).  */
1747
1748       /* Handle backslash.  */
1749       char_escaped = (c == '\\' && ! char_escaped);
1750     }
1751 }
1752 \f
1753 /* Make a variant type in the proper way for C/C++, propagating qualifiers
1754    down to the element type of an array.  */
1755
1756 tree
1757 c_build_type_variant (type, constp, volatilep)
1758      tree type;
1759      int constp, volatilep;
1760 {
1761   if (TREE_CODE (type) == ARRAY_TYPE)
1762     {
1763       tree real_main_variant = TYPE_MAIN_VARIANT (type);
1764
1765       push_obstacks (TYPE_OBSTACK (type), TYPE_OBSTACK (type));
1766       type = build_array_type (c_build_type_variant (TREE_TYPE (type),
1767                                                      constp, volatilep),
1768                                TYPE_DOMAIN (type));
1769       TYPE_MAIN_VARIANT (type) = real_main_variant;
1770       pop_obstacks ();
1771     }
1772   return build_type_variant (type, constp, volatilep);
1773 }