OSDN Git Service

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