OSDN Git Service

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