OSDN Git Service

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