OSDN Git Service

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