source: trunk/third/gcc/c-common.c @ 8834

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