OSDN Git Service

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