OSDN Git Service

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