OSDN Git Service

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