OSDN Git Service

* builtins.c (is_valid_printf_arglist, expand_builtin_printf):
[pf3gnuchains/gcc-fork.git] / gcc / c-common.c
1 /* Subroutines shared by all languages that are variants of C.
2    Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000
3    Free Software Foundation, Inc.
4
5 This file is part of GNU CC.
6
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "tree.h"
25 #include "flags.h"
26 #include "toplev.h"
27 #include "output.h"
28 #include "c-pragma.h"
29 #include "rtl.h"
30 #include "ggc.h"
31 #include "expr.h"
32 #include "c-common.h"
33 #include "tm_p.h"
34 #include "intl.h"
35 #include "diagnostic.h"
36
37 #if USE_CPPLIB
38 #include "cpplib.h"
39 cpp_reader  parse_in;
40 #endif
41
42 #undef WCHAR_TYPE_SIZE
43 #define WCHAR_TYPE_SIZE TYPE_PRECISION (wchar_type_node)
44
45 /* The following symbols are subsumed in the c_global_trees array, and
46    listed here individually for documentation purposes.
47
48    INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
49
50         tree short_integer_type_node;
51         tree long_integer_type_node;
52         tree long_long_integer_type_node;
53
54         tree short_unsigned_type_node;
55         tree long_unsigned_type_node;
56         tree long_long_unsigned_type_node;
57
58         tree boolean_type_node;
59         tree boolean_false_node;
60         tree boolean_true_node;
61
62         tree ptrdiff_type_node;
63
64         tree unsigned_char_type_node;
65         tree signed_char_type_node;
66         tree wchar_type_node;
67         tree signed_wchar_type_node;
68         tree unsigned_wchar_type_node;
69
70         tree float_type_node;
71         tree double_type_node;
72         tree long_double_type_node;
73
74         tree complex_integer_type_node;
75         tree complex_float_type_node;
76         tree complex_double_type_node;
77         tree complex_long_double_type_node;
78
79         tree intQI_type_node;
80         tree intHI_type_node;
81         tree intSI_type_node;
82         tree intDI_type_node;
83         tree intTI_type_node;
84
85         tree unsigned_intQI_type_node;
86         tree unsigned_intHI_type_node;
87         tree unsigned_intSI_type_node;
88         tree unsigned_intDI_type_node;
89         tree unsigned_intTI_type_node;
90
91         tree widest_integer_literal_type_node;
92         tree widest_unsigned_literal_type_node;
93
94    Nodes for types `void *' and `const void *'.
95
96         tree ptr_type_node, const_ptr_type_node;
97
98    Nodes for types `char *' and `const char *'.
99
100         tree string_type_node, const_string_type_node;
101
102    Type `char[SOMENUMBER]'.
103    Used when an array of char is needed and the size is irrelevant.
104
105         tree char_array_type_node;
106
107    Type `int[SOMENUMBER]' or something like it.
108    Used when an array of int needed and the size is irrelevant.
109
110         tree int_array_type_node;
111
112    Type `wchar_t[SOMENUMBER]' or something like it.
113    Used when a wide string literal is created.
114
115         tree wchar_array_type_node;
116
117    Type `int ()' -- used for implicit declaration of functions.
118
119         tree default_function_type;
120
121    Function types `int (int)', etc.
122
123         tree int_ftype_int;
124         tree void_ftype;
125         tree void_ftype_ptr;
126         tree int_ftype_int;
127         tree ptr_ftype_sizetype;
128
129    A VOID_TYPE node, packaged in a TREE_LIST.
130
131         tree void_list_node;
132
133   The identifiers __FUNCTION__, __PRETTY_FUNCTION__, and __func__.
134
135         tree function_id_node;
136         tree pretty_function_id_node;
137         tree func_id_node;
138
139 */
140
141 tree c_global_trees[CTI_MAX];
142
143 /* The elements of `ridpointers' are identifier nodes for the reserved
144    type names and storage classes.  It is indexed by a RID_... value.  */
145 tree *ridpointers;
146
147 tree (*make_fname_decl)                PARAMS ((tree, const char *, int));
148
149 /* If non-NULL, the address of a language-specific function that
150    returns 1 for language-specific statement codes.  */
151 int (*lang_statement_code_p)           PARAMS ((enum tree_code));
152
153 /* If non-NULL, the address of a language-specific function that takes
154    any action required right before expand_function_end is called.  */
155 void (*lang_expand_function_end)       PARAMS ((void));
156
157 /* Nonzero means the expression being parsed will never be evaluated.
158    This is a count, since unevaluated expressions can nest.  */
159 int skip_evaluation;
160
161 enum attrs {A_PACKED, A_NOCOMMON, A_COMMON, A_NORETURN, A_CONST, A_T_UNION,
162             A_NO_CHECK_MEMORY_USAGE, A_NO_INSTRUMENT_FUNCTION,
163             A_CONSTRUCTOR, A_DESTRUCTOR, A_MODE, A_SECTION, A_ALIGNED,
164             A_UNUSED, A_FORMAT, A_FORMAT_ARG, A_WEAK, A_ALIAS, A_MALLOC,
165             A_NO_LIMIT_STACK, A_PURE};
166
167 enum format_type { printf_format_type, scanf_format_type,
168                    strftime_format_type };
169
170 static void add_attribute               PARAMS ((enum attrs, const char *,
171                                                  int, int, int));
172 static void init_attributes             PARAMS ((void));
173 static void record_function_format      PARAMS ((tree, tree, enum format_type,
174                                                  int, int));
175 static void record_international_format PARAMS ((tree, tree, int));
176 static int default_valid_lang_attribute PARAMS ((tree, tree, tree, tree));
177
178 /* Keep a stack of if statements.  We record the number of compound
179    statements seen up to the if keyword, as well as the line number
180    and file of the if.  If a potentially ambiguous else is seen, that
181    fact is recorded; the warning is issued when we can be sure that
182    the enclosing if statement does not have an else branch.  */
183 typedef struct
184 {
185   int compstmt_count;
186   int line;
187   const char *file;
188   int needs_warning;
189   tree if_stmt;
190 } if_elt;
191
192 static if_elt *if_stack;
193
194 /* Amount of space in the if statement stack.  */
195 static int if_stack_space = 0;
196
197 /* Stack pointer.  */
198 static int if_stack_pointer = 0;
199
200 /* Record the start of an if-then, and record the start of it
201    for ambiguous else detection.  */
202
203 void
204 c_expand_start_cond (cond, compstmt_count)
205      tree cond;
206      int compstmt_count;
207 {
208   tree if_stmt;
209
210   /* Make sure there is enough space on the stack.  */
211   if (if_stack_space == 0)
212     {
213       if_stack_space = 10;
214       if_stack = (if_elt *)xmalloc (10 * sizeof (if_elt));
215     }
216   else if (if_stack_space == if_stack_pointer)
217     {
218       if_stack_space += 10;
219       if_stack = (if_elt *)xrealloc (if_stack, if_stack_space * sizeof (if_elt));
220     }
221
222   if_stmt = build_stmt (IF_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
223   IF_COND (if_stmt) = cond;
224   add_stmt (if_stmt);
225
226   /* Record this if statement.  */
227   if_stack[if_stack_pointer].compstmt_count = compstmt_count;
228   if_stack[if_stack_pointer].file = input_filename;
229   if_stack[if_stack_pointer].line = lineno;
230   if_stack[if_stack_pointer].needs_warning = 0;
231   if_stack[if_stack_pointer].if_stmt = if_stmt;
232   if_stack_pointer++;
233 }
234
235 /* Called after the then-clause for an if-statement is processed.  */
236
237 void
238 c_finish_then ()
239 {
240   tree if_stmt = if_stack[if_stack_pointer - 1].if_stmt;
241   RECHAIN_STMTS (if_stmt, THEN_CLAUSE (if_stmt));
242 }
243
244 /* Record the end of an if-then.  Optionally warn if a nested
245    if statement had an ambiguous else clause.  */
246
247 void
248 c_expand_end_cond ()
249 {
250   if_stack_pointer--;
251   if (if_stack[if_stack_pointer].needs_warning)
252     warning_with_file_and_line (if_stack[if_stack_pointer].file,
253                                 if_stack[if_stack_pointer].line,
254                                 "suggest explicit braces to avoid ambiguous `else'");
255   last_expr_type = NULL_TREE;
256 }
257
258 /* Called between the then-clause and the else-clause
259    of an if-then-else.  */
260
261 void
262 c_expand_start_else ()
263 {
264   /* An ambiguous else warning must be generated for the enclosing if
265      statement, unless we see an else branch for that one, too.  */
266   if (warn_parentheses
267       && if_stack_pointer > 1
268       && (if_stack[if_stack_pointer - 1].compstmt_count
269           == if_stack[if_stack_pointer - 2].compstmt_count))
270     if_stack[if_stack_pointer - 2].needs_warning = 1;
271
272   /* Even if a nested if statement had an else branch, it can't be
273      ambiguous if this one also has an else.  So don't warn in that
274      case.  Also don't warn for any if statements nested in this else.  */
275   if_stack[if_stack_pointer - 1].needs_warning = 0;
276   if_stack[if_stack_pointer - 1].compstmt_count--;
277 }
278
279 /* Called after the else-clause for an if-statement is processed.  */
280
281 void
282 c_finish_else ()
283 {
284   tree if_stmt = if_stack[if_stack_pointer - 1].if_stmt;
285   RECHAIN_STMTS (if_stmt, ELSE_CLAUSE (if_stmt));
286 }
287
288 /* Make bindings for __FUNCTION__, __PRETTY_FUNCTION__, and __func__.  */
289
290 void
291 declare_function_name ()
292 {
293   const char *name, *printable_name;
294
295   if (current_function_decl == NULL)
296     {
297       name = "";
298       printable_name = "top level";
299     }
300   else
301     {
302       /* Allow functions to be nameless (such as artificial ones).  */
303       if (DECL_NAME (current_function_decl))
304         name = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
305       else
306         name = "";
307       printable_name = (*decl_printable_name) (current_function_decl, 2);
308
309       /* ISO C99 defines __func__, which is a variable, not a string
310          constant, and which is not a defined symbol at file scope.  */
311       (*make_fname_decl) (func_id_node, name, 0);
312     }
313   
314   (*make_fname_decl) (function_id_node, name, 0);
315   (*make_fname_decl) (pretty_function_id_node, printable_name, 1);
316 }
317
318 /* Given a chain of STRING_CST nodes,
319    concatenate them into one STRING_CST
320    and give it a suitable array-of-chars data type.  */
321
322 tree
323 combine_strings (strings)
324      tree strings;
325 {
326   register tree value, t;
327   register int length = 1;
328   int wide_length = 0;
329   int wide_flag = 0;
330   int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
331   int nchars;
332   const int nchars_max = flag_isoc99 ? 4095 : 509;
333
334   if (TREE_CHAIN (strings))
335     {
336       /* More than one in the chain, so concatenate.  */
337       register char *p, *q;
338
339       /* Don't include the \0 at the end of each substring,
340          except for the last one.
341          Count wide strings and ordinary strings separately.  */
342       for (t = strings; t; t = TREE_CHAIN (t))
343         {
344           if (TREE_TYPE (t) == wchar_array_type_node)
345             {
346               wide_length += (TREE_STRING_LENGTH (t) - wchar_bytes);
347               wide_flag = 1;
348             }
349           else
350             length += (TREE_STRING_LENGTH (t) - 1);
351         }
352
353       /* If anything is wide, the non-wides will be converted,
354          which makes them take more space.  */
355       if (wide_flag)
356         length = length * wchar_bytes + wide_length;
357
358       p = ggc_alloc_string (NULL, length);
359
360       /* Copy the individual strings into the new combined string.
361          If the combined string is wide, convert the chars to ints
362          for any individual strings that are not wide.  */
363
364       q = p;
365       for (t = strings; t; t = TREE_CHAIN (t))
366         {
367           int len = (TREE_STRING_LENGTH (t)
368                      - ((TREE_TYPE (t) == wchar_array_type_node)
369                         ? wchar_bytes : 1));
370           if ((TREE_TYPE (t) == wchar_array_type_node) == wide_flag)
371             {
372               memcpy (q, TREE_STRING_POINTER (t), len);
373               q += len;
374             }
375           else
376             {
377               int i;
378               for (i = 0; i < len; i++)
379                 {
380                   if (WCHAR_TYPE_SIZE == HOST_BITS_PER_SHORT)
381                     ((short *) q)[i] = TREE_STRING_POINTER (t)[i];
382                   else
383                     ((int *) q)[i] = TREE_STRING_POINTER (t)[i];
384                 }
385               q += len * wchar_bytes;
386             }
387         }
388       if (wide_flag)
389         {
390           int i;
391           for (i = 0; i < wchar_bytes; i++)
392             *q++ = 0;
393         }
394       else
395         *q = 0;
396
397       value = make_node (STRING_CST);
398       TREE_STRING_POINTER (value) = p;
399       TREE_STRING_LENGTH (value) = length;
400     }
401   else
402     {
403       value = strings;
404       length = TREE_STRING_LENGTH (value);
405       if (TREE_TYPE (value) == wchar_array_type_node)
406         wide_flag = 1;
407     }
408
409   /* Compute the number of elements, for the array type.  */
410   nchars = wide_flag ? length / wchar_bytes : length;
411
412   if (pedantic && nchars > nchars_max)
413     pedwarn ("string length `%d' is greater than the minimum length `%d' ISO C%d is required to support",
414              nchars, nchars_max, flag_isoc99 ? 99 : 89);
415
416   /* Create the array type for the string constant.
417      -Wwrite-strings says make the string constant an array of const char
418      so that copying it to a non-const pointer will get a warning.
419      For C++, this is the standard behavior.  */
420   if (flag_const_strings
421       && (! flag_traditional  && ! flag_writable_strings))
422     {
423       tree elements
424         = build_type_variant (wide_flag ? wchar_type_node : char_type_node,
425                               1, 0);
426       TREE_TYPE (value)
427         = build_array_type (elements,
428                             build_index_type (build_int_2 (nchars - 1, 0)));
429     }
430   else
431     TREE_TYPE (value)
432       = build_array_type (wide_flag ? wchar_type_node : char_type_node,
433                           build_index_type (build_int_2 (nchars - 1, 0)));
434
435   TREE_CONSTANT (value) = 1;
436   TREE_READONLY (value) = ! flag_writable_strings;
437   TREE_STATIC (value) = 1;
438   return value;
439 }
440 \f
441 /* To speed up processing of attributes, we maintain an array of
442    IDENTIFIER_NODES and the corresponding attribute types.  */
443
444 /* Array to hold attribute information.  */
445
446 static struct {enum attrs id; tree name; int min, max, decl_req;} attrtab[50];
447
448 static int attrtab_idx = 0;
449
450 /* Add an entry to the attribute table above.  */
451
452 static void
453 add_attribute (id, string, min_len, max_len, decl_req)
454      enum attrs id;
455      const char *string;
456      int min_len, max_len;
457      int decl_req;
458 {
459   char buf[100];
460
461   attrtab[attrtab_idx].id = id;
462   attrtab[attrtab_idx].name = get_identifier (string);
463   attrtab[attrtab_idx].min = min_len;
464   attrtab[attrtab_idx].max = max_len;
465   attrtab[attrtab_idx++].decl_req = decl_req;
466
467   sprintf (buf, "__%s__", string);
468
469   attrtab[attrtab_idx].id = id;
470   attrtab[attrtab_idx].name = get_identifier (buf);
471   attrtab[attrtab_idx].min = min_len;
472   attrtab[attrtab_idx].max = max_len;
473   attrtab[attrtab_idx++].decl_req = decl_req;
474 }
475
476 /* Initialize attribute table.  */
477
478 static void
479 init_attributes ()
480 {
481   add_attribute (A_PACKED, "packed", 0, 0, 0);
482   add_attribute (A_NOCOMMON, "nocommon", 0, 0, 1);
483   add_attribute (A_COMMON, "common", 0, 0, 1);
484   add_attribute (A_NORETURN, "noreturn", 0, 0, 1);
485   add_attribute (A_NORETURN, "volatile", 0, 0, 1);
486   add_attribute (A_UNUSED, "unused", 0, 0, 0);
487   add_attribute (A_CONST, "const", 0, 0, 1);
488   add_attribute (A_T_UNION, "transparent_union", 0, 0, 0);
489   add_attribute (A_CONSTRUCTOR, "constructor", 0, 0, 1);
490   add_attribute (A_DESTRUCTOR, "destructor", 0, 0, 1);
491   add_attribute (A_MODE, "mode", 1, 1, 1);
492   add_attribute (A_SECTION, "section", 1, 1, 1);
493   add_attribute (A_ALIGNED, "aligned", 0, 1, 0);
494   add_attribute (A_FORMAT, "format", 3, 3, 1);
495   add_attribute (A_FORMAT_ARG, "format_arg", 1, 1, 1);
496   add_attribute (A_WEAK, "weak", 0, 0, 1);
497   add_attribute (A_ALIAS, "alias", 1, 1, 1);
498   add_attribute (A_NO_INSTRUMENT_FUNCTION, "no_instrument_function", 0, 0, 1);
499   add_attribute (A_NO_CHECK_MEMORY_USAGE, "no_check_memory_usage", 0, 0, 1);
500   add_attribute (A_MALLOC, "malloc", 0, 0, 1);
501   add_attribute (A_NO_LIMIT_STACK, "no_stack_limit", 0, 0, 1);
502   add_attribute (A_PURE, "pure", 0, 0, 1);
503 }
504 \f
505 /* Default implementation of valid_lang_attribute, below.  By default, there
506    are no language-specific attributes.  */
507
508 static int
509 default_valid_lang_attribute (attr_name, attr_args, decl, type)
510   tree attr_name ATTRIBUTE_UNUSED;
511   tree attr_args ATTRIBUTE_UNUSED;
512   tree decl ATTRIBUTE_UNUSED;
513   tree type ATTRIBUTE_UNUSED;
514 {
515   return 0;
516 }
517
518 /* Return a 1 if ATTR_NAME and ATTR_ARGS denote a valid language-specific
519    attribute for either declaration DECL or type TYPE and 0 otherwise.  */
520
521 int (*valid_lang_attribute) PARAMS ((tree, tree, tree, tree))
522      = default_valid_lang_attribute;
523
524 /* Process the attributes listed in ATTRIBUTES and PREFIX_ATTRIBUTES
525    and install them in NODE, which is either a DECL (including a TYPE_DECL)
526    or a TYPE.  PREFIX_ATTRIBUTES can appear after the declaration specifiers
527    and declaration modifiers but before the declaration proper.  */
528
529 void
530 decl_attributes (node, attributes, prefix_attributes)
531      tree node, attributes, prefix_attributes;
532 {
533   tree decl = 0, type = 0;
534   int is_type = 0;
535   tree a;
536
537   if (attrtab_idx == 0)
538     init_attributes ();
539
540   if (DECL_P (node))
541     {
542       decl = node;
543       type = TREE_TYPE (decl);
544       is_type = TREE_CODE (node) == TYPE_DECL;
545     }
546   else if (TYPE_P (node))
547     type = node, is_type = 1;
548
549 #ifdef PRAGMA_INSERT_ATTRIBUTES
550   /* If the code in c-pragma.c wants to insert some attributes then
551      allow it to do so.  Do this before allowing machine back ends to
552      insert attributes, so that they have the opportunity to override
553      anything done here.  */
554   PRAGMA_INSERT_ATTRIBUTES (node, & attributes, & prefix_attributes);
555 #endif
556
557 #ifdef INSERT_ATTRIBUTES
558   INSERT_ATTRIBUTES (node, & attributes, & prefix_attributes);
559 #endif
560
561   attributes = chainon (prefix_attributes, attributes);
562
563   for (a = attributes; a; a = TREE_CHAIN (a))
564     {
565       tree name = TREE_PURPOSE (a);
566       tree args = TREE_VALUE (a);
567       int i;
568       enum attrs id;
569
570       for (i = 0; i < attrtab_idx; i++)
571         if (attrtab[i].name == name)
572           break;
573
574       if (i == attrtab_idx)
575         {
576           if (! valid_machine_attribute (name, args, decl, type)
577               && ! (* valid_lang_attribute) (name, args, decl, type))
578             warning ("`%s' attribute directive ignored",
579                      IDENTIFIER_POINTER (name));
580           else if (decl != 0)
581             type = TREE_TYPE (decl);
582           continue;
583         }
584       else if (attrtab[i].decl_req && decl == 0)
585         {
586           warning ("`%s' attribute does not apply to types",
587                    IDENTIFIER_POINTER (name));
588           continue;
589         }
590       else if (list_length (args) < attrtab[i].min
591                || list_length (args) > attrtab[i].max)
592         {
593           error ("wrong number of arguments specified for `%s' attribute",
594                  IDENTIFIER_POINTER (name));
595           continue;
596         }
597
598       id = attrtab[i].id;
599       switch (id)
600         {
601         case A_PACKED:
602           if (is_type)
603             TYPE_PACKED (type) = 1;
604           else if (TREE_CODE (decl) == FIELD_DECL)
605             DECL_PACKED (decl) = 1;
606           /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
607              used for DECL_REGISTER.  It wouldn't mean anything anyway.  */
608           else
609             warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
610           break;
611
612         case A_NOCOMMON:
613           if (TREE_CODE (decl) == VAR_DECL)
614             DECL_COMMON (decl) = 0;
615           else
616             warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
617           break;
618
619         case A_COMMON:
620           if (TREE_CODE (decl) == VAR_DECL)
621             DECL_COMMON (decl) = 1;
622           else
623             warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
624           break;
625
626         case A_NORETURN:
627           if (TREE_CODE (decl) == FUNCTION_DECL)
628             TREE_THIS_VOLATILE (decl) = 1;
629           else if (TREE_CODE (type) == POINTER_TYPE
630                    && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
631             TREE_TYPE (decl) = type
632               = build_pointer_type
633                 (build_type_variant (TREE_TYPE (type),
634                                      TREE_READONLY (TREE_TYPE (type)), 1));
635           else
636             warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
637           break;
638
639         case A_MALLOC:
640           if (TREE_CODE (decl) == FUNCTION_DECL)
641             DECL_IS_MALLOC (decl) = 1;
642           /* ??? TODO: Support types.  */
643           else
644             warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
645           break;
646
647         case A_UNUSED:
648           if (is_type)
649             if (decl)
650               TREE_USED (decl) = 1;
651             else
652               TREE_USED (type) = 1;
653           else if (TREE_CODE (decl) == PARM_DECL
654                    || TREE_CODE (decl) == VAR_DECL
655                    || TREE_CODE (decl) == FUNCTION_DECL
656                    || TREE_CODE (decl) == LABEL_DECL)
657             TREE_USED (decl) = 1;
658           else
659             warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
660           break;
661
662         case A_CONST:
663           if (TREE_CODE (decl) == FUNCTION_DECL)
664             TREE_READONLY (decl) = 1;
665           else if (TREE_CODE (type) == POINTER_TYPE
666                    && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
667             TREE_TYPE (decl) = type
668               = build_pointer_type
669                 (build_type_variant (TREE_TYPE (type), 1,
670                                      TREE_THIS_VOLATILE (TREE_TYPE (type))));
671           else
672             warning ( "`%s' attribute ignored", IDENTIFIER_POINTER (name));
673           break;
674
675         case A_PURE:
676           if (TREE_CODE (decl) == FUNCTION_DECL)
677             DECL_IS_PURE (decl) = 1;
678           /* ??? TODO: Support types.  */
679           else
680             warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
681           break;
682
683
684         case A_T_UNION:
685           if (is_type
686               && TREE_CODE (type) == UNION_TYPE
687               && (decl == 0
688                   || (TYPE_FIELDS (type) != 0
689                       && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))))
690             TYPE_TRANSPARENT_UNION (type) = 1;
691           else if (decl != 0 && TREE_CODE (decl) == PARM_DECL
692                    && TREE_CODE (type) == UNION_TYPE
693                    && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))
694             DECL_TRANSPARENT_UNION (decl) = 1;
695           else
696             warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
697           break;
698
699         case A_CONSTRUCTOR:
700           if (TREE_CODE (decl) == FUNCTION_DECL
701               && TREE_CODE (type) == FUNCTION_TYPE
702               && decl_function_context (decl) == 0)
703             {
704               DECL_STATIC_CONSTRUCTOR (decl) = 1;
705               TREE_USED (decl) = 1;
706             }
707           else
708             warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
709           break;
710
711         case A_DESTRUCTOR:
712           if (TREE_CODE (decl) == FUNCTION_DECL
713               && TREE_CODE (type) == FUNCTION_TYPE
714               && decl_function_context (decl) == 0)
715             {
716               DECL_STATIC_DESTRUCTOR (decl) = 1;
717               TREE_USED (decl) = 1;
718             }
719           else
720             warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
721           break;
722
723         case A_MODE:
724           if (TREE_CODE (TREE_VALUE (args)) != IDENTIFIER_NODE)
725             warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
726           else
727             {
728               int j;
729               const char *p = IDENTIFIER_POINTER (TREE_VALUE (args));
730               int len = strlen (p);
731               enum machine_mode mode = VOIDmode;
732               tree typefm;
733
734               if (len > 4 && p[0] == '_' && p[1] == '_'
735                   && p[len - 1] == '_' && p[len - 2] == '_')
736                 {
737                   char *newp = (char *) alloca (len - 1);
738
739                   strcpy (newp, &p[2]);
740                   newp[len - 4] = '\0';
741                   p = newp;
742                 }
743
744               /* Give this decl a type with the specified mode.
745                  First check for the special modes.  */
746               if (! strcmp (p, "byte"))
747                 mode = byte_mode;
748               else if (!strcmp (p, "word"))
749                 mode = word_mode;
750               else if (! strcmp (p, "pointer"))
751                 mode = ptr_mode;
752               else
753                 for (j = 0; j < NUM_MACHINE_MODES; j++)
754                   if (!strcmp (p, GET_MODE_NAME (j)))
755                     mode = (enum machine_mode) j;
756
757               if (mode == VOIDmode)
758                 error ("unknown machine mode `%s'", p);
759               else if (0 == (typefm = type_for_mode (mode,
760                                                      TREE_UNSIGNED (type))))
761                 error ("no data type for mode `%s'", p);
762               else
763                 {
764                   TREE_TYPE (decl) = type = typefm;
765                   DECL_SIZE (decl) = DECL_SIZE_UNIT (decl) = 0;
766                   layout_decl (decl, 0);
767                 }
768             }
769           break;
770
771         case A_SECTION:
772 #ifdef ASM_OUTPUT_SECTION_NAME
773           if ((TREE_CODE (decl) == FUNCTION_DECL
774                || TREE_CODE (decl) == VAR_DECL)
775               && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
776             {
777               if (TREE_CODE (decl) == VAR_DECL
778                   && current_function_decl != NULL_TREE
779                   && ! TREE_STATIC (decl))
780                 error_with_decl (decl,
781                   "section attribute cannot be specified for local variables");
782               /* The decl may have already been given a section attribute from
783                  a previous declaration.  Ensure they match.  */
784               else if (DECL_SECTION_NAME (decl) != NULL_TREE
785                        && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
786                                   TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
787                 error_with_decl (node,
788                                  "section of `%s' conflicts with previous declaration");
789               else
790                 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
791             }
792           else
793             error_with_decl (node,
794                            "section attribute not allowed for `%s'");
795 #else
796           error_with_decl (node,
797                   "section attributes are not supported for this target");
798 #endif
799           break;
800
801         case A_ALIGNED:
802           {
803             tree align_expr
804               = (args ? TREE_VALUE (args)
805                  : size_int (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
806             int i;
807
808             /* Strip any NOPs of any kind.  */
809             while (TREE_CODE (align_expr) == NOP_EXPR
810                    || TREE_CODE (align_expr) == CONVERT_EXPR
811                    || TREE_CODE (align_expr) == NON_LVALUE_EXPR)
812               align_expr = TREE_OPERAND (align_expr, 0);
813
814             if (TREE_CODE (align_expr) != INTEGER_CST)
815               {
816                 error ("requested alignment is not a constant");
817                 continue;
818               }
819
820             if ((i = tree_log2 (align_expr)) == -1)
821               error ("requested alignment is not a power of 2");
822             else if (i > HOST_BITS_PER_INT - 2)
823               error ("requested alignment is too large");
824             else if (is_type)
825               {
826                 /* If we have a TYPE_DECL, then copy the type, so that we
827                    don't accidentally modify a builtin type.  See pushdecl.  */
828                 if (decl && TREE_TYPE (decl) != error_mark_node
829                     && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
830                   {
831                     tree tt = TREE_TYPE (decl);
832                     DECL_ORIGINAL_TYPE (decl) = tt;
833                     tt = build_type_copy (tt);
834                     TYPE_NAME (tt) = decl;
835                     TREE_USED (tt) = TREE_USED (decl);
836                     TREE_TYPE (decl) = tt;
837                     type = tt;
838                   }
839
840                 TYPE_ALIGN (type) = (1 << i) * BITS_PER_UNIT;
841                 TYPE_USER_ALIGN (type) = 1;
842               }
843             else if (TREE_CODE (decl) != VAR_DECL
844                      && TREE_CODE (decl) != FIELD_DECL)
845               error_with_decl (decl,
846                                "alignment may not be specified for `%s'");
847             else
848               {
849                 DECL_ALIGN (decl) = (1 << i) * BITS_PER_UNIT;
850                 DECL_USER_ALIGN (decl) = 1;
851               }
852           }
853           break;
854
855         case A_FORMAT:
856           {
857             tree format_type_id = TREE_VALUE (args);
858             tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
859             tree first_arg_num_expr
860               = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
861             unsigned HOST_WIDE_INT format_num, first_arg_num;
862             enum format_type format_type;
863             tree argument;
864             unsigned int arg_num;
865
866             if (TREE_CODE (decl) != FUNCTION_DECL)
867               {
868                 error_with_decl (decl,
869                          "argument format specified for non-function `%s'");
870                 continue;
871               }
872
873             if (TREE_CODE (format_type_id) != IDENTIFIER_NODE)
874               {
875                 error ("unrecognized format specifier");
876                 continue;
877               }
878             else
879               {
880                 const char *p = IDENTIFIER_POINTER (format_type_id);
881
882                 if (!strcmp (p, "printf") || !strcmp (p, "__printf__"))
883                   format_type = printf_format_type;
884                 else if (!strcmp (p, "scanf") || !strcmp (p, "__scanf__"))
885                   format_type = scanf_format_type;
886                 else if (!strcmp (p, "strftime")
887                          || !strcmp (p, "__strftime__"))
888                   format_type = strftime_format_type;
889                 else
890                   {
891                     warning ("`%s' is an unrecognized format function type", p);
892                     continue;
893                   }
894               }
895
896             /* Strip any conversions from the string index and first arg number
897                and verify they are constants.  */
898             while (TREE_CODE (format_num_expr) == NOP_EXPR
899                    || TREE_CODE (format_num_expr) == CONVERT_EXPR
900                    || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
901               format_num_expr = TREE_OPERAND (format_num_expr, 0);
902
903             while (TREE_CODE (first_arg_num_expr) == NOP_EXPR
904                    || TREE_CODE (first_arg_num_expr) == CONVERT_EXPR
905                    || TREE_CODE (first_arg_num_expr) == NON_LVALUE_EXPR)
906               first_arg_num_expr = TREE_OPERAND (first_arg_num_expr, 0);
907
908             if (TREE_CODE (format_num_expr) != INTEGER_CST
909                 || TREE_INT_CST_HIGH (format_num_expr) != 0
910                 || TREE_CODE (first_arg_num_expr) != INTEGER_CST
911                 || TREE_INT_CST_HIGH (first_arg_num_expr) != 0)
912               {
913                 error ("format string has invalid operand number");
914                 continue;
915               }
916
917             format_num = TREE_INT_CST_LOW (format_num_expr);
918             first_arg_num = TREE_INT_CST_LOW (first_arg_num_expr);
919             if (first_arg_num != 0 && first_arg_num <= format_num)
920               {
921                 error ("format string arg follows the args to be formatted");
922                 continue;
923               }
924
925             /* If a parameter list is specified, verify that the format_num
926                argument is actually a string, in case the format attribute
927                is in error.  */
928             argument = TYPE_ARG_TYPES (type);
929             if (argument)
930               {
931                 for (arg_num = 1; argument != 0 && arg_num != format_num;
932                      ++arg_num, argument = TREE_CHAIN (argument))
933                   ;
934
935                 if (! argument
936                     || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
937                   || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
938                       != char_type_node))
939                   {
940                     error ("format string arg not a string type");
941                     continue;
942                   }
943
944                 else if (first_arg_num != 0)
945                   {
946                     /* Verify that first_arg_num points to the last arg,
947                        the ...  */
948                     while (argument)
949                       arg_num++, argument = TREE_CHAIN (argument);
950
951                     if (arg_num != first_arg_num)
952                       {
953                         error ("args to be formatted is not '...'");
954                         continue;
955                       }
956                   }
957               }
958
959             record_function_format (DECL_NAME (decl),
960                                     DECL_ASSEMBLER_NAME (decl),
961                                     format_type, format_num, first_arg_num);
962             break;
963           }
964
965         case A_FORMAT_ARG:
966           {
967             tree format_num_expr = TREE_VALUE (args);
968             unsigned HOST_WIDE_INT format_num;
969             unsigned int arg_num;
970             tree argument;
971
972             if (TREE_CODE (decl) != FUNCTION_DECL)
973               {
974                 error_with_decl (decl,
975                          "argument format specified for non-function `%s'");
976                 continue;
977               }
978
979             /* Strip any conversions from the first arg number and verify it
980                is a constant.  */
981             while (TREE_CODE (format_num_expr) == NOP_EXPR
982                    || TREE_CODE (format_num_expr) == CONVERT_EXPR
983                    || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
984               format_num_expr = TREE_OPERAND (format_num_expr, 0);
985
986             if (TREE_CODE (format_num_expr) != INTEGER_CST
987                 || TREE_INT_CST_HIGH (format_num_expr) != 0)
988               {
989                 error ("format string has invalid operand number");
990                 continue;
991               }
992
993             format_num = TREE_INT_CST_LOW (format_num_expr);
994
995             /* If a parameter list is specified, verify that the format_num
996                argument is actually a string, in case the format attribute
997                is in error.  */
998             argument = TYPE_ARG_TYPES (type);
999             if (argument)
1000               {
1001                 for (arg_num = 1; argument != 0 && arg_num != format_num;
1002                      ++arg_num, argument = TREE_CHAIN (argument))
1003                   ;
1004
1005                 if (! argument
1006                     || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
1007                   || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
1008                       != char_type_node))
1009                   {
1010                     error ("format string arg not a string type");
1011                     continue;
1012                   }
1013               }
1014
1015             if (TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) != POINTER_TYPE
1016                 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_TYPE (decl))))
1017                     != char_type_node))
1018               {
1019                 error ("function does not return string type");
1020                 continue;
1021               }
1022
1023             record_international_format (DECL_NAME (decl),
1024                                          DECL_ASSEMBLER_NAME (decl),
1025                                          format_num);
1026             break;
1027           }
1028
1029         case A_WEAK:
1030           declare_weak (decl);
1031           break;
1032
1033         case A_ALIAS:
1034           if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
1035               || (TREE_CODE (decl) != FUNCTION_DECL && ! DECL_EXTERNAL (decl)))
1036             error_with_decl (decl,
1037                              "`%s' defined both normally and as an alias");
1038           else if (decl_function_context (decl) == 0)
1039             {
1040               tree id;
1041
1042               id = TREE_VALUE (args);
1043               if (TREE_CODE (id) != STRING_CST)
1044                 {
1045                   error ("alias arg not a string");
1046                   break;
1047                 }
1048               id = get_identifier (TREE_STRING_POINTER (id));
1049               /* This counts as a use of the object pointed to.  */
1050               TREE_USED (id) = 1;
1051
1052               if (TREE_CODE (decl) == FUNCTION_DECL)
1053                 DECL_INITIAL (decl) = error_mark_node;
1054               else
1055                 DECL_EXTERNAL (decl) = 0;
1056               assemble_alias (decl, id);
1057             }
1058           else
1059             warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
1060           break;
1061
1062         case A_NO_CHECK_MEMORY_USAGE:
1063           if (TREE_CODE (decl) != FUNCTION_DECL)
1064             {
1065               error_with_decl (decl,
1066                                "`%s' attribute applies only to functions",
1067                                IDENTIFIER_POINTER (name));
1068             }
1069           else if (DECL_INITIAL (decl))
1070             {
1071               error_with_decl (decl,
1072                                "can't set `%s' attribute after definition",
1073                                IDENTIFIER_POINTER (name));
1074             }
1075           else
1076             DECL_NO_CHECK_MEMORY_USAGE (decl) = 1;
1077           break;
1078
1079         case A_NO_INSTRUMENT_FUNCTION:
1080           if (TREE_CODE (decl) != FUNCTION_DECL)
1081             {
1082               error_with_decl (decl,
1083                                "`%s' attribute applies only to functions",
1084                                IDENTIFIER_POINTER (name));
1085             }
1086           else if (DECL_INITIAL (decl))
1087             {
1088               error_with_decl (decl,
1089                                "can't set `%s' attribute after definition",
1090                                IDENTIFIER_POINTER (name));
1091             }
1092           else
1093             DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1094           break;
1095
1096         case A_NO_LIMIT_STACK:
1097           if (TREE_CODE (decl) != FUNCTION_DECL)
1098             {
1099               error_with_decl (decl,
1100                                "`%s' attribute applies only to functions",
1101                                IDENTIFIER_POINTER (name));
1102             }
1103           else if (DECL_INITIAL (decl))
1104             {
1105               error_with_decl (decl,
1106                                "can't set `%s' attribute after definition",
1107                                IDENTIFIER_POINTER (name));
1108             }
1109           else
1110             DECL_NO_LIMIT_STACK (decl) = 1;
1111           break;
1112         }
1113     }
1114 }
1115
1116 /* Split SPECS_ATTRS, a list of declspecs and prefix attributes, into two
1117    lists.  SPECS_ATTRS may also be just a typespec (eg: RECORD_TYPE).
1118
1119    The head of the declspec list is stored in DECLSPECS.
1120    The head of the attribute list is stored in PREFIX_ATTRIBUTES.
1121
1122    Note that attributes in SPECS_ATTRS are stored in the TREE_PURPOSE of
1123    the list elements.  We drop the containing TREE_LIST nodes and link the
1124    resulting attributes together the way decl_attributes expects them.  */
1125
1126 void
1127 split_specs_attrs (specs_attrs, declspecs, prefix_attributes)
1128      tree specs_attrs;
1129      tree *declspecs, *prefix_attributes;
1130 {
1131   tree t, s, a, next, specs, attrs;
1132
1133   /* This can happen after an __extension__ in pedantic mode.  */
1134   if (specs_attrs != NULL_TREE 
1135       && TREE_CODE (specs_attrs) == INTEGER_CST)
1136     {
1137       *declspecs = NULL_TREE;
1138       *prefix_attributes = NULL_TREE;
1139       return;
1140     }
1141
1142   /* This can happen in c++ (eg: decl: typespec initdecls ';').  */
1143   if (specs_attrs != NULL_TREE
1144       && TREE_CODE (specs_attrs) != TREE_LIST)
1145     {
1146       *declspecs = specs_attrs;
1147       *prefix_attributes = NULL_TREE;
1148       return;
1149     }
1150
1151   /* Remember to keep the lists in the same order, element-wise.  */
1152
1153   specs = s = NULL_TREE;
1154   attrs = a = NULL_TREE;
1155   for (t = specs_attrs; t; t = next)
1156     {
1157       next = TREE_CHAIN (t);
1158       /* Declspecs have a non-NULL TREE_VALUE.  */
1159       if (TREE_VALUE (t) != NULL_TREE)
1160         {
1161           if (specs == NULL_TREE)
1162             specs = s = t;
1163           else
1164             {
1165               TREE_CHAIN (s) = t;
1166               s = t;
1167             }
1168         }
1169       else
1170         {
1171           if (attrs == NULL_TREE)
1172             attrs = a = TREE_PURPOSE (t);
1173           else
1174             {
1175               TREE_CHAIN (a) = TREE_PURPOSE (t);
1176               a = TREE_PURPOSE (t);
1177             }
1178           /* More attrs can be linked here, move A to the end.  */
1179           while (TREE_CHAIN (a) != NULL_TREE)
1180             a = TREE_CHAIN (a);
1181         }
1182     }
1183
1184   /* Terminate the lists.  */
1185   if (s != NULL_TREE)
1186     TREE_CHAIN (s) = NULL_TREE;
1187   if (a != NULL_TREE)
1188     TREE_CHAIN (a) = NULL_TREE;
1189
1190   /* All done.  */
1191   *declspecs = specs;
1192   *prefix_attributes = attrs;
1193 }
1194
1195 /* Strip attributes from SPECS_ATTRS, a list of declspecs and attributes.
1196    This function is used by the parser when a rule will accept attributes
1197    in a particular position, but we don't want to support that just yet.
1198
1199    A warning is issued for every ignored attribute.  */
1200
1201 tree
1202 strip_attrs (specs_attrs)
1203      tree specs_attrs;
1204 {
1205   tree specs, attrs;
1206
1207   split_specs_attrs (specs_attrs, &specs, &attrs);
1208
1209   while (attrs)
1210     {
1211       warning ("`%s' attribute ignored",
1212                IDENTIFIER_POINTER (TREE_PURPOSE (attrs)));
1213       attrs = TREE_CHAIN (attrs);
1214     }
1215
1216   return specs;
1217 }
1218 \f
1219 /* Check a printf/fprintf/sprintf/scanf/fscanf/sscanf format against
1220    a parameter list.  */
1221
1222 /* The meaningfully distinct length modifiers for format checking recognised
1223    by GCC.  */
1224 enum format_lengths
1225 {
1226   FMT_LEN_none,
1227   FMT_LEN_hh,
1228   FMT_LEN_h,
1229   FMT_LEN_l,
1230   FMT_LEN_ll,
1231   FMT_LEN_L,
1232   FMT_LEN_z,
1233   FMT_LEN_t,
1234   FMT_LEN_j,
1235   FMT_LEN_MAX
1236 };
1237
1238
1239 /* The standard versions in which various format features appeared.  */
1240 enum format_std_version
1241 {
1242   STD_C89,
1243   STD_C94,
1244   STD_C99,
1245   STD_EXT
1246 };
1247
1248
1249 /* Structure describing a length modifier supported in format checking, and
1250    possibly a doubled version such as "hh".  */
1251 typedef struct
1252 {
1253   /* Name of the single-character length modifier.  */
1254   const char *name;
1255   /* Index into a format_char_info.types array.  */
1256   enum format_lengths index;
1257   /* Standard version this length appears in.  */
1258   enum format_std_version std;
1259   /* Same, if the modifier can be repeated, or NULL if it can't.  */
1260   const char *double_name;
1261   enum format_lengths double_index;
1262   enum format_std_version double_std;
1263 } format_length_info;
1264
1265
1266 /* Structure desribing the combination of a conversion specifier
1267    (or a set of specifiers which act identically) and a length modifier.  */
1268 typedef struct
1269 {
1270   /* The standard version this combination of length and type appeared in.
1271      This is only relevant if greater than those for length and type
1272      individually; otherwise it is ignored.  */
1273   enum format_std_version std;
1274   /* The name to use for the type, if different from that generated internally
1275      (e.g., "signed size_t").  */
1276   const char *name;
1277   /* The type itself.  */
1278   tree *type;
1279 } format_type_detail;
1280
1281
1282 /* Macros to fill out tables of these.  */
1283 #define BADLEN  { 0, NULL, NULL }
1284 #define NOLENGTHS       { BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }
1285
1286
1287 /* Structure desribing a format conversion specifier (or a set of specifiers
1288    which act identically), and the length modifiers used with it.  */
1289 typedef struct
1290 {
1291   const char *format_chars;
1292   int pointer_count;
1293   enum format_std_version std;
1294   /* Types accepted for each length modifier.  */
1295   format_type_detail types[FMT_LEN_MAX];
1296   /* List of other modifier characters allowed with these specifiers.
1297      This lists flags, and additionally "w" for width, "p" for precision,
1298      "a" for scanf "a" allocation extension (not applicable in C99 mode),
1299      "*" for scanf suppression, and "E" and "O" for those strftime
1300      modifiers.  */
1301   const char *flag_chars;
1302   /* List of additional flags describing these conversion specifiers.
1303      "c" for generic character pointers being allowed, "2" for strftime
1304      two digit year formats, "3" for strftime formats giving two digit
1305      years in some locales, "o" if use of strftime "O"
1306      is a GNU extension beyond C99, "W" if the argument is a pointer
1307      which is dereferenced and written into, "i" for printf integer
1308      formats where the '0' flag is ignored with precision, and "["
1309      for the starting character of a scanf scanset.  */
1310   const char *flags2;
1311 } format_char_info;
1312
1313
1314 /* Structure describing a particular kind of format processed by GCC.  */
1315 typedef struct
1316 {
1317   /* The name of this kind of format, for use in diagnostics.  */
1318   const char *name;
1319   /* Specifications of the length modifiers accepted; possibly NULL.  */
1320   const format_length_info *length_char_specs;
1321   /* Details of the conversion specification characters accepted.  */
1322   const format_char_info *conversion_specs;
1323 } format_kind_info;
1324
1325
1326 /* Structure describing details of a type expected in format checking,
1327    and the type to check against it.  */
1328 typedef struct format_wanted_type
1329 {
1330   /* The type wanted.  */
1331   tree wanted_type;
1332   /* The name of this type to use in diagnostics.  */
1333   const char *wanted_type_name;
1334   /* The level of indirection through pointers at which this type occurs.  */
1335   int pointer_count;
1336   /* Whether, when pointer_count is 1, to allow any character type when
1337      pedantic, rather than just the character or void type specified.  */
1338   int char_lenient_flag;
1339   /* Whether the argument, dereferenced once, is written into and so the
1340      argument must not be a pointer to a const-qualified type.  */
1341   int writing_in_flag;
1342   /* If warnings should be of the form "field precision is not type int",
1343      the name to use (in this case "field precision"), otherwise NULL,
1344      for "%s format, %s arg" type messages.  If (in an extension), this
1345      is a pointer type, wanted_type_name should be set to include the
1346      terminating '*' characters of the type name to give a correct
1347      message.  */
1348   const char *name;
1349   /* The actual parameter to check against the wanted type.  */
1350   tree param;
1351   /* The argument number of that parameter.  */
1352   int arg_num;
1353   /* The next type to check for this format conversion, or NULL if none.  */
1354   struct format_wanted_type *next;
1355 } format_wanted_type;
1356
1357
1358 static const format_length_info printf_length_specs[] =
1359 {
1360   { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99 },
1361   { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C99 },
1362   { "q", FMT_LEN_ll, STD_EXT, NULL, 0, 0 },
1363   { "L", FMT_LEN_L, STD_C89, NULL, 0, 0 },
1364   { "z", FMT_LEN_z, STD_C99, NULL, 0, 0 },
1365   { "Z", FMT_LEN_z, STD_EXT, NULL, 0, 0 },
1366   { "t", FMT_LEN_t, STD_C99, NULL, 0, 0 },
1367   { "j", FMT_LEN_j, STD_C99, NULL, 0, 0 },
1368   { NULL, 0, 0, NULL, 0, 0 }
1369 };
1370
1371
1372 /* This differs from printf_length_specs only in that "Z" is not accepted.  */
1373 static const format_length_info scanf_length_specs[] =
1374 {
1375   { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99 },
1376   { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C99 },
1377   { "q", FMT_LEN_ll, STD_EXT, NULL, 0, 0 },
1378   { "L", FMT_LEN_L, STD_C89, NULL, 0, 0 },
1379   { "z", FMT_LEN_z, STD_C99, NULL, 0, 0 },
1380   { "t", FMT_LEN_t, STD_C99, NULL, 0, 0 },
1381   { "j", FMT_LEN_j, STD_C99, NULL, 0, 0 },
1382   { NULL, 0, 0, NULL, 0, 0 }
1383 };
1384
1385
1386 #define T_I     &integer_type_node
1387 #define T89_I   { STD_C89, NULL, T_I }
1388 #define T99_I   { STD_C99, NULL, T_I }
1389 #define T_L     &long_integer_type_node
1390 #define T89_L   { STD_C89, NULL, T_L }
1391 #define T_LL    &long_long_integer_type_node
1392 #define T99_LL  { STD_C99, NULL, T_LL }
1393 #define TEX_LL  { STD_EXT, NULL, T_LL }
1394 #define T_S     &short_integer_type_node
1395 #define T89_S   { STD_C89, NULL, T_S }
1396 #define T_UI    &unsigned_type_node
1397 #define T89_UI  { STD_C89, NULL, T_UI }
1398 #define T99_UI  { STD_C99, NULL, T_UI }
1399 #define T_UL    &long_unsigned_type_node
1400 #define T89_UL  { STD_C89, NULL, T_UL }
1401 #define T_ULL   &long_long_unsigned_type_node
1402 #define T99_ULL { STD_C99, NULL, T_ULL }
1403 #define TEX_ULL { STD_EXT, NULL, T_ULL }
1404 #define T_US    &short_unsigned_type_node
1405 #define T89_US  { STD_C89, NULL, T_US }
1406 #define T_F     &float_type_node
1407 #define T89_F   { STD_C89, NULL, T_F }
1408 #define T99_F   { STD_C99, NULL, T_F }
1409 #define T_D     &double_type_node
1410 #define T89_D   { STD_C89, NULL, T_D }
1411 #define T99_D   { STD_C99, NULL, T_D }
1412 #define T_LD    &long_double_type_node
1413 #define T89_LD  { STD_C89, NULL, T_LD }
1414 #define T99_LD  { STD_C99, NULL, T_LD }
1415 #define T_C     &char_type_node
1416 #define T89_C   { STD_C89, NULL, T_C }
1417 #define T_SC    &signed_char_type_node
1418 #define T99_SC  { STD_C99, NULL, T_SC }
1419 #define T_UC    &unsigned_char_type_node
1420 #define T99_UC  { STD_C99, NULL, T_UC }
1421 #define T_V     &void_type_node
1422 #define T89_V   { STD_C89, NULL, T_V }
1423 #define T_W     &wchar_type_node
1424 #define T94_W   { STD_C94, "wchar_t", T_W }
1425 #define TEX_W   { STD_EXT, "wchar_t", T_W }
1426 #define T_WI    &wint_type_node
1427 #define T94_WI  { STD_C94, "wint_t", T_WI }
1428 #define TEX_WI  { STD_EXT, "wint_t", T_WI }
1429 #define T_ST    &c_size_type_node
1430 #define T99_ST  { STD_C99, "size_t", T_ST }
1431 #define T_SST   &signed_size_type_node
1432 #define T99_SST { STD_C99, "signed size_t", T_SST }
1433 #define T_PD    &ptrdiff_type_node
1434 #define T99_PD  { STD_C99, "ptrdiff_t", T_PD }
1435 #define T_UPD   &unsigned_ptrdiff_type_node
1436 #define T99_UPD { STD_C99, "unsigned ptrdiff_t", T_UPD }
1437 #define T_IM    NULL /* intmax_t not yet implemented.  */
1438 #define T99_IM  { STD_C99, "intmax_t", T_IM }
1439 #define T_UIM   NULL /* uintmax_t not yet implemented.  */
1440 #define T99_UIM { STD_C99, "uintmax_t", T_UIM }
1441
1442 static const format_char_info print_char_table[] =
1443 {
1444   /* C89 conversion specifiers.  */
1445   { "di",  0, STD_C89, { T89_I,   T99_I,   T89_I,   T89_L,   T99_LL,  TEX_LL,  T99_SST, T99_PD,  T99_IM  }, "-wp0 +'I", "i" },
1446   { "oxX", 0, STD_C89, { T89_UI,  T99_UI,  T89_UI,  T89_UL,  T99_ULL, TEX_ULL, T99_ST,  T99_UPD, T99_UIM }, "-wp0#",    "i" },
1447   { "u",   0, STD_C89, { T89_UI,  T99_UI,  T89_UI,  T89_UL,  T99_ULL, TEX_ULL, T99_ST,  T99_UPD, T99_UIM }, "-wp0'I",   "i" },
1448   { "fgG", 0, STD_C89, { T89_D,   BADLEN,  BADLEN,  T99_D,   BADLEN,  T89_LD,  BADLEN,  BADLEN,  BADLEN  }, "-wp0 +#'", ""  },
1449   { "eE",  0, STD_C89, { T89_D,   BADLEN,  BADLEN,  T99_D,   BADLEN,  T89_LD,  BADLEN,  BADLEN,  BADLEN  }, "-wp0 +#",  ""  },
1450   { "c",   0, STD_C89, { T89_I,   BADLEN,  BADLEN,  T94_WI,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "-w",       ""  },
1451   { "s",   1, STD_C89, { T89_C,   BADLEN,  BADLEN,  T94_W,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "-wp",      "c" },
1452   { "p",   1, STD_C89, { T89_V,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "-w",       "c" },
1453   { "n",   1, STD_C89, { T89_I,   T99_SC,  T89_S,   T89_L,   T99_LL,  BADLEN,  T99_SST, T99_PD,  T99_IM  }, "",         "W" },
1454   /* C99 conversion specifiers.  */
1455   { "F",   0, STD_C99, { T99_D,   BADLEN,  BADLEN,  T99_D,   BADLEN,  T99_LD,  BADLEN,  BADLEN,  BADLEN  }, "-wp0 +#'", ""  },
1456   { "aA",  0, STD_C99, { T99_D,   BADLEN,  BADLEN,  T99_D,   BADLEN,  T99_LD,  BADLEN,  BADLEN,  BADLEN  }, "-wp0 +#",  ""  },
1457   /* X/Open conversion specifiers.  */
1458   { "C",   0, STD_EXT, { TEX_WI,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "-w",       ""  },
1459   { "S",   1, STD_EXT, { TEX_W,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "-wp",      ""  },
1460   /* GNU conversion specifiers.  */
1461   { "m",   0, STD_EXT, { T89_V,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "-wp",      ""  },
1462   { NULL,  0, 0, NOLENGTHS, NULL, NULL }
1463 };
1464
1465 static const format_char_info scan_char_table[] =
1466 {
1467   /* C89 conversion specifiers.  */
1468   { "di",    1, STD_C89, { T89_I,   T99_SC,  T89_S,   T89_L,   T99_LL,  TEX_LL,  T99_SST, T99_PD,  T99_IM  }, "*w",  "W"   },
1469   { "ouxX",  1, STD_C89, { T89_UI,  T99_UC,  T89_US,  T89_UL,  T99_ULL, TEX_ULL, T99_ST,  T99_UPD, T99_UIM }, "*w",  "W"   },
1470   { "efgEG", 1, STD_C89, { T89_F,   BADLEN,  BADLEN,  T89_D,   BADLEN,  T89_LD,  BADLEN,  BADLEN,  BADLEN  }, "*w",  "W"   },
1471   { "c",     1, STD_C89, { T89_C,   BADLEN,  BADLEN,  T94_W,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "*w",  "cW"  },
1472   { "s",     1, STD_C89, { T89_C,   BADLEN,  BADLEN,  T94_W,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "*aw", "cW"  },
1473   { "[",     1, STD_C89, { T89_C,   BADLEN,  BADLEN,  T94_W,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "*aw", "cW[" },
1474   { "p",     2, STD_C89, { T89_V,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "*w",  "W"   },
1475   { "n",     1, STD_C89, { T89_I,   T99_SC,  T89_S,   T89_L,   T99_LL,  BADLEN,  T99_SST, T99_PD,  T99_IM  }, "",    "W"   },
1476   /* C99 conversion specifiers.  */
1477   { "FaA",   1, STD_C99, { T99_F,   BADLEN,  BADLEN,  T99_D,   BADLEN,  T99_LD,  BADLEN,  BADLEN,  BADLEN  }, "*w",  "W"   },
1478   /* X/Open conversion specifiers.  */
1479   { "C",     1, STD_EXT, { TEX_W,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "*w",  "W"   },
1480   { "S",     1, STD_EXT, { TEX_W,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "*aw", "W"   },
1481   { NULL, 0, 0, NOLENGTHS, NULL, NULL }
1482 };
1483
1484 static format_char_info time_char_table[] =
1485 {
1486   /* C89 conversion specifiers.  */
1487   { "ABZab",            0, STD_C89, NOLENGTHS, "^#",     ""   },
1488   { "cx",               0, STD_C89, NOLENGTHS, "E",      "3"  },
1489   { "HIMSUWdmw",        0, STD_C89, NOLENGTHS, "-_0Ow",  ""   },
1490   { "j",                0, STD_C89, NOLENGTHS, "-_0Ow",  "o"  },
1491   { "p",                0, STD_C89, NOLENGTHS, "#",      ""   },
1492   { "X",                0, STD_C89, NOLENGTHS, "E",      ""   },
1493   { "y",                0, STD_C89, NOLENGTHS, "EO-_0w", "2"  },
1494   { "Y",                0, STD_C89, NOLENGTHS, "-_0EOw", "o"  },
1495   { "%",                0, STD_C89, NOLENGTHS, "",       ""   },
1496   /* C99 conversion specifiers.  */
1497   { "C",                0, STD_C99, NOLENGTHS, "-_0EOw", "o"  },
1498   { "D",                0, STD_C99, NOLENGTHS, "",       "2"  },
1499   { "eVu",              0, STD_C99, NOLENGTHS, "-_0Ow",  ""   },
1500   { "FRTnrt",           0, STD_C99, NOLENGTHS, "",       ""   },
1501   { "g",                0, STD_C99, NOLENGTHS, "O-_0w",  "2o" },
1502   { "G",                0, STD_C99, NOLENGTHS, "-_0Ow",  "o"  },
1503   { "h",                0, STD_C99, NOLENGTHS, "^#",     ""   },
1504   { "z",                0, STD_C99, NOLENGTHS, "O",      "o"  },
1505   /* GNU conversion specifiers.  */
1506   { "kls",              0, STD_EXT, NOLENGTHS, "-_0Ow",  ""   },
1507   { "P",                0, STD_EXT, NOLENGTHS, "",       ""   },
1508   { NULL,               0, 0, NOLENGTHS, NULL, NULL }
1509 };
1510
1511
1512 /* This must be in the same order as enum format_type.  */
1513 static const format_kind_info format_types[] =
1514 {
1515   { "printf",   printf_length_specs, print_char_table },
1516   { "scanf",    scanf_length_specs,  scan_char_table  },
1517   { "strftime", NULL,                time_char_table  }
1518 };
1519
1520
1521 typedef struct function_format_info
1522 {
1523   struct function_format_info *next;  /* next structure on the list */
1524   tree name;                    /* identifier such as "printf" */
1525   tree assembler_name;          /* optional mangled identifier (for C++) */
1526   enum format_type format_type; /* type of format (printf, scanf, etc.) */
1527   int format_num;               /* number of format argument */
1528   int first_arg_num;            /* number of first arg (zero for varargs) */
1529 } function_format_info;
1530
1531 static function_format_info *function_format_list = NULL;
1532
1533 typedef struct international_format_info
1534 {
1535   struct international_format_info *next;  /* next structure on the list */
1536   tree name;                    /* identifier such as "gettext" */
1537   tree assembler_name;          /* optional mangled identifier (for C++) */
1538   int format_num;               /* number of format argument */
1539 } international_format_info;
1540
1541 static international_format_info *international_format_list = NULL;
1542
1543 static void check_format_info   PARAMS ((int *, function_format_info *, tree));
1544 static void status_warning PARAMS ((int *, const char *, ...))
1545      ATTRIBUTE_PRINTF_2;
1546
1547 static void init_dollar_format_checking         PARAMS ((int, tree));
1548 static int maybe_read_dollar_number             PARAMS ((int *, const char **, int,
1549                                                          tree, tree *));
1550 static void finish_dollar_format_checking       PARAMS ((int *));
1551
1552 static void check_format_types  PARAMS ((int *, format_wanted_type *));
1553 static int is_valid_printf_arglist PARAMS ((tree));
1554 static rtx c_expand_builtin (tree, rtx, enum machine_mode, enum expand_modifier);
1555 static rtx c_expand_builtin_printf PARAMS ((tree, rtx, enum machine_mode,
1556                                             enum expand_modifier, int));
1557
1558 /* Initialize the table of functions to perform format checking on.
1559    The ISO C functions are always checked (whether <stdio.h> is
1560    included or not), since it is common to call printf without
1561    including <stdio.h>.  There shouldn't be a problem with this,
1562    since ISO C reserves these function names whether you include the
1563    header file or not.  In any case, the checking is harmless.  With
1564    -ffreestanding, these default attributes are disabled, and must be
1565    specified manually if desired.
1566
1567    Also initialize the name of function that modify the format string for
1568    internationalization purposes.  */
1569
1570 void
1571 init_function_format_info ()
1572 {
1573   if (flag_hosted)
1574     {
1575       /* Functions from ISO/IEC 9899:1990.  */
1576       record_function_format (get_identifier ("printf"), NULL_TREE,
1577                               printf_format_type, 1, 2);
1578       record_function_format (get_identifier ("__builtin_printf"), NULL_TREE,
1579                               printf_format_type, 1, 2);
1580       record_function_format (get_identifier ("fprintf"), NULL_TREE,
1581                               printf_format_type, 2, 3);
1582       record_function_format (get_identifier ("sprintf"), NULL_TREE,
1583                               printf_format_type, 2, 3);
1584       record_function_format (get_identifier ("scanf"), NULL_TREE,
1585                               scanf_format_type, 1, 2);
1586       record_function_format (get_identifier ("fscanf"), NULL_TREE,
1587                               scanf_format_type, 2, 3);
1588       record_function_format (get_identifier ("sscanf"), NULL_TREE,
1589                               scanf_format_type, 2, 3);
1590       record_function_format (get_identifier ("vprintf"), NULL_TREE,
1591                               printf_format_type, 1, 0);
1592       record_function_format (get_identifier ("vfprintf"), NULL_TREE,
1593                               printf_format_type, 2, 0);
1594       record_function_format (get_identifier ("vsprintf"), NULL_TREE,
1595                               printf_format_type, 2, 0);
1596       record_function_format (get_identifier ("strftime"), NULL_TREE,
1597                               strftime_format_type, 3, 0);
1598     }
1599
1600   if (flag_hosted && flag_isoc99)
1601     {
1602       /* ISO C99 adds the snprintf and vscanf family functions.  */
1603       record_function_format (get_identifier ("snprintf"), NULL_TREE,
1604                               printf_format_type, 3, 4);
1605       record_function_format (get_identifier ("vsnprintf"), NULL_TREE,
1606                               printf_format_type, 3, 0);
1607       record_function_format (get_identifier ("vscanf"), NULL_TREE,
1608                               scanf_format_type, 1, 0);
1609       record_function_format (get_identifier ("vfscanf"), NULL_TREE,
1610                               scanf_format_type, 2, 0);
1611       record_function_format (get_identifier ("vsscanf"), NULL_TREE,
1612                               scanf_format_type, 2, 0);
1613     }
1614
1615   if (flag_hosted && flag_noniso_default_format_attributes)
1616     {
1617       /* Uniforum/GNU gettext functions, not in ISO C.  */
1618       record_international_format (get_identifier ("gettext"), NULL_TREE, 1);
1619       record_international_format (get_identifier ("dgettext"), NULL_TREE, 2);
1620       record_international_format (get_identifier ("dcgettext"), NULL_TREE, 2);
1621     }
1622 }
1623
1624 /* Record information for argument format checking.  FUNCTION_IDENT is
1625    the identifier node for the name of the function to check (its decl
1626    need not exist yet).
1627    FORMAT_TYPE specifies the type of format checking.  FORMAT_NUM is the number
1628    of the argument which is the format control string (starting from 1).
1629    FIRST_ARG_NUM is the number of the first actual argument to check
1630    against the format string, or zero if no checking is not be done
1631    (e.g. for varargs such as vfprintf).  */
1632
1633 static void
1634 record_function_format (name, assembler_name, format_type,
1635                         format_num, first_arg_num)
1636       tree name;
1637       tree assembler_name;
1638       enum format_type format_type;
1639       int format_num;
1640       int first_arg_num;
1641 {
1642   function_format_info *info;
1643
1644   /* Re-use existing structure if it's there.  */
1645
1646   for (info = function_format_list; info; info = info->next)
1647     {
1648       if (info->name == name && info->assembler_name == assembler_name)
1649         break;
1650     }
1651   if (! info)
1652     {
1653       info = (function_format_info *) xmalloc (sizeof (function_format_info));
1654       info->next = function_format_list;
1655       function_format_list = info;
1656
1657       info->name = name;
1658       info->assembler_name = assembler_name;
1659     }
1660
1661   info->format_type = format_type;
1662   info->format_num = format_num;
1663   info->first_arg_num = first_arg_num;
1664 }
1665
1666 /* Record information for the names of function that modify the format
1667    argument to format functions.  FUNCTION_IDENT is the identifier node for
1668    the name of the function (its decl need not exist yet) and FORMAT_NUM is
1669    the number of the argument which is the format control string (starting
1670    from 1).  */
1671
1672 static void
1673 record_international_format (name, assembler_name, format_num)
1674       tree name;
1675       tree assembler_name;
1676       int format_num;
1677 {
1678   international_format_info *info;
1679
1680   /* Re-use existing structure if it's there.  */
1681
1682   for (info = international_format_list; info; info = info->next)
1683     {
1684       if (info->name == name && info->assembler_name == assembler_name)
1685         break;
1686     }
1687
1688   if (! info)
1689     {
1690       info
1691         = (international_format_info *)
1692           xmalloc (sizeof (international_format_info));
1693       info->next = international_format_list;
1694       international_format_list = info;
1695
1696       info->name = name;
1697       info->assembler_name = assembler_name;
1698     }
1699
1700   info->format_num = format_num;
1701 }
1702 \f
1703 /* Check the argument list of a call to printf, scanf, etc.
1704    NAME is the function identifier.
1705    ASSEMBLER_NAME is the function's assembler identifier.
1706    (Either NAME or ASSEMBLER_NAME, but not both, may be NULL_TREE.)
1707    PARAMS is the list of argument values.  */
1708
1709 void
1710 check_function_format (status, name, assembler_name, params)
1711      int *status;
1712      tree name;
1713      tree assembler_name;
1714      tree params;
1715 {
1716   function_format_info *info;
1717
1718   /* See if this function is a format function.  */
1719   for (info = function_format_list; info; info = info->next)
1720     {
1721       if (info->assembler_name
1722           ? (info->assembler_name == assembler_name)
1723           : (info->name == name))
1724         {
1725           /* Yup; check it.  */
1726           check_format_info (status, info, params);
1727           break;
1728         }
1729     }
1730 }
1731
1732 /* This function replaces `warning' inside the printf format checking
1733    functions.  If the `status' parameter is non-NULL, then it is
1734    dereferenced and set to 1 whenever a warning is caught.  Otherwise
1735    it warns as usual by replicating the innards of the warning
1736    function from diagnostic.c.  */
1737 static void
1738 status_warning VPARAMS ((int *status, const char *msgid, ...))
1739 {
1740 #ifndef ANSI_PROTOTYPES
1741   int *status;
1742   const char *msgid;
1743 #endif
1744   va_list ap;
1745   diagnostic_context dc;
1746
1747   if (status)
1748     *status = 1;
1749   else
1750     {
1751       VA_START (ap, msgid);
1752
1753 #ifndef ANSI_PROTOTYPES
1754       status = va_arg (ap, int *);
1755       msgid = va_arg (ap, const char *);
1756 #endif
1757
1758       /* This duplicates the warning function behavior.  */
1759       set_diagnostic_context
1760         (&dc, msgid, &ap, input_filename, lineno, /* warn = */ 1);
1761       report_diagnostic (&dc);
1762
1763       va_end (ap);
1764     }
1765 }
1766
1767 /* Variables used by the checking of $ operand number formats.  */
1768 static char *dollar_arguments_used = NULL;
1769 static int dollar_arguments_alloc = 0;
1770 static int dollar_arguments_count;
1771 static int dollar_first_arg_num;
1772 static int dollar_max_arg_used;
1773 static int dollar_format_warned;
1774
1775 /* Initialize the checking for a format string that may contain $
1776    parameter number specifications; we will need to keep track of whether
1777    each parameter has been used.  FIRST_ARG_NUM is the number of the first
1778    argument that is a parameter to the format, or 0 for a vprintf-style
1779    function; PARAMS is the list of arguments starting at this argument.  */
1780
1781 static void
1782 init_dollar_format_checking (first_arg_num, params)
1783      int first_arg_num;
1784      tree params;
1785 {
1786   dollar_first_arg_num = first_arg_num;
1787   dollar_arguments_count = 0;
1788   dollar_max_arg_used = 0;
1789   dollar_format_warned = 0;
1790   if (first_arg_num > 0)
1791     {
1792       while (params)
1793         {
1794           dollar_arguments_count++;
1795           params = TREE_CHAIN (params);
1796         }
1797     }
1798   if (dollar_arguments_alloc < dollar_arguments_count)
1799     {
1800       if (dollar_arguments_used)
1801         free (dollar_arguments_used);
1802       dollar_arguments_alloc = dollar_arguments_count;
1803       dollar_arguments_used = xmalloc (dollar_arguments_alloc);
1804     }
1805   if (dollar_arguments_alloc)
1806     memset (dollar_arguments_used, 0, dollar_arguments_alloc);
1807 }
1808
1809
1810 /* Look for a decimal number followed by a $ in *FORMAT.  If DOLLAR_NEEDED
1811    is set, it is an error if one is not found; otherwise, it is OK.  If
1812    such a number is found, check whether it is within range and mark that
1813    numbered operand as being used for later checking.  Returns the operand
1814    number if found and within range, zero if no such number was found and
1815    this is OK, or -1 on error.  PARAMS points to the first operand of the
1816    format; PARAM_PTR is made to point to the parameter referred to.  If
1817    a $ format is found, *FORMAT is updated to point just after it.  */
1818
1819 static int
1820 maybe_read_dollar_number (status, format, dollar_needed, params, param_ptr)
1821      int *status;
1822      const char **format;
1823      int dollar_needed;
1824      tree params;
1825      tree *param_ptr;
1826 {
1827   int argnum;
1828   int overflow_flag;
1829   const char *fcp = *format;
1830   if (*fcp < '0' || *fcp > '9')
1831     {
1832       if (dollar_needed)
1833         {
1834           status_warning (status, "missing $ operand number in format");
1835           return -1;
1836         }
1837       else
1838         return 0;
1839     }
1840   argnum = 0;
1841   overflow_flag = 0;
1842   while (*fcp >= '0' && *fcp <= '9')
1843     {
1844       int nargnum;
1845       nargnum = 10 * argnum + (*fcp - '0');
1846       if (nargnum < 0 || nargnum / 10 != argnum)
1847         overflow_flag = 1;
1848       argnum = nargnum;
1849       fcp++;
1850     }
1851   if (*fcp != '$')
1852     {
1853       if (dollar_needed)
1854         {
1855           status_warning (status, "missing $ operand number in format");
1856           return -1;
1857         }
1858       else
1859         return 0;
1860     }
1861   *format = fcp + 1;
1862   if (pedantic && !dollar_format_warned)
1863     {
1864       status_warning (status, "ISO C does not support %%n$ operand number formats");
1865       dollar_format_warned = 1;
1866     }
1867   if (overflow_flag || argnum == 0
1868       || (dollar_first_arg_num && argnum > dollar_arguments_count))
1869     {
1870       status_warning (status, "operand number out of range in format");
1871       return -1;
1872     }
1873   if (argnum > dollar_max_arg_used)
1874     dollar_max_arg_used = argnum;
1875   /* For vprintf-style functions we may need to allocate more memory to
1876      track which arguments are used.  */
1877   while (dollar_arguments_alloc < dollar_max_arg_used)
1878     {
1879       int nalloc;
1880       nalloc = 2 * dollar_arguments_alloc + 16;
1881       dollar_arguments_used = xrealloc (dollar_arguments_used, nalloc);
1882       memset (dollar_arguments_used + dollar_arguments_alloc, 0,
1883               nalloc - dollar_arguments_alloc);
1884       dollar_arguments_alloc = nalloc;
1885     }
1886   dollar_arguments_used[argnum - 1] = 1;
1887   if (dollar_first_arg_num)
1888     {
1889       int i;
1890       *param_ptr = params;
1891       for (i = 1; i < argnum && *param_ptr != 0; i++)
1892         *param_ptr = TREE_CHAIN (*param_ptr);
1893
1894       if (*param_ptr == 0)
1895         {
1896           /* This case shouldn't be caught here.  */
1897           abort ();
1898         }
1899     }
1900   else
1901     *param_ptr = 0;
1902   return argnum;
1903 }
1904
1905
1906 /* Finish the checking for a format string that used $ operand number formats
1907    instead of non-$ formats.  We check for unused operands before used ones
1908    (a serious error, since the implementation of the format function
1909    can't know what types to pass to va_arg to find the later arguments).
1910    and for unused operands at the end of the format (if we know how many
1911    arguments the format had, so not for vprintf).  If there were operand
1912    numbers out of range on a non-vprintf-style format, we won't have reached
1913    here.  */
1914
1915 static void
1916 finish_dollar_format_checking (status)
1917      int *status;
1918 {
1919   int i;
1920   for (i = 0; i < dollar_max_arg_used; i++)
1921     {
1922       if (!dollar_arguments_used[i])
1923         status_warning (status, "format argument %d unused before used argument %d in $-style format",
1924                  i + 1, dollar_max_arg_used);
1925     }
1926   if (dollar_first_arg_num && dollar_max_arg_used < dollar_arguments_count)
1927     status_warning (status, "unused arguments in $-style format");
1928 }
1929
1930
1931 /* Check the argument list of a call to printf, scanf, etc.
1932    INFO points to the function_format_info structure.
1933    PARAMS is the list of argument values.  */
1934
1935 static void
1936 check_format_info (status, info, params)
1937      int *status;
1938      function_format_info *info;
1939      tree params;
1940 {
1941   int i;
1942   int arg_num;
1943   int suppressed, wide, precise;
1944   const char *length_chars = NULL;
1945   enum format_lengths length_chars_val = FMT_LEN_none;
1946   enum format_std_version length_chars_std = STD_C89;
1947   int format_char;
1948   int format_length;
1949   tree format_tree;
1950   tree cur_param;
1951   tree wanted_type;
1952   int main_arg_num;
1953   tree main_arg_params;
1954   enum format_std_version wanted_type_std;
1955   const char *wanted_type_name;
1956   format_wanted_type width_wanted_type;
1957   format_wanted_type precision_wanted_type;
1958   format_wanted_type main_wanted_type;
1959   format_wanted_type *first_wanted_type;
1960   format_wanted_type *last_wanted_type;
1961   tree first_fillin_param;
1962   const char *format_chars;
1963   const format_kind_info *fki = NULL;
1964   const format_length_info *fli = NULL;
1965   const format_char_info *fci = NULL;
1966   char flag_chars[8];
1967   /* -1 if no conversions taking an operand have been found; 0 if one has
1968      and it didn't use $; 1 if $ formats are in use.  */
1969   int has_operand_number = -1;
1970
1971   /* Skip to format argument.  If the argument isn't available, there's
1972      no work for us to do; prototype checking will catch the problem.  */
1973   for (arg_num = 1; ; ++arg_num)
1974     {
1975       if (params == 0)
1976         return;
1977       if (arg_num == info->format_num)
1978         break;
1979       params = TREE_CHAIN (params);
1980     }
1981   format_tree = TREE_VALUE (params);
1982   params = TREE_CHAIN (params);
1983   if (format_tree == 0)
1984     return;
1985
1986   /* We can only check the format if it's a string constant.  */
1987   while (TREE_CODE (format_tree) == NOP_EXPR)
1988     format_tree = TREE_OPERAND (format_tree, 0); /* strip coercion */
1989
1990   if (TREE_CODE (format_tree) == CALL_EXPR
1991       && TREE_CODE (TREE_OPERAND (format_tree, 0)) == ADDR_EXPR
1992       && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0))
1993           == FUNCTION_DECL))
1994     {
1995       tree function = TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0);
1996
1997       /* See if this is a call to a known internationalization function
1998          that modifies the format arg.  */
1999       international_format_info *info;
2000
2001       for (info = international_format_list; info; info = info->next)
2002         if (info->assembler_name
2003             ? (info->assembler_name == DECL_ASSEMBLER_NAME (function))
2004             : (info->name == DECL_NAME (function)))
2005           {
2006             tree inner_args;
2007             int i;
2008
2009             for (inner_args = TREE_OPERAND (format_tree, 1), i = 1;
2010                  inner_args != 0;
2011                  inner_args = TREE_CHAIN (inner_args), i++)
2012               if (i == info->format_num)
2013                 {
2014                   format_tree = TREE_VALUE (inner_args);
2015
2016                   while (TREE_CODE (format_tree) == NOP_EXPR)
2017                     format_tree = TREE_OPERAND (format_tree, 0);
2018                 }
2019           }
2020     }
2021
2022   if (integer_zerop (format_tree))
2023     {
2024       status_warning (status, "null format string");
2025       return;
2026     }
2027   if (TREE_CODE (format_tree) != ADDR_EXPR)
2028     {
2029       /* The user may get multiple warnings if the supplied argument
2030          isn't even a string pointer.  */
2031       /* Functions taking a va_list normally pass a non-literal format
2032          string.  These functions typically are declared with
2033          first_arg_num == 0, so avoid warning in those cases.  */
2034       if (info->first_arg_num != 0 && warn_format > 1)
2035         status_warning (status, "format not a string literal, argument types not checked");
2036       return;
2037     }
2038   format_tree = TREE_OPERAND (format_tree, 0);
2039   if (TREE_CODE (format_tree) != STRING_CST)
2040     {
2041       /* The user may get multiple warnings if the supplied argument
2042          isn't even a string pointer.  */
2043       /* Functions taking a va_list normally pass a non-literal format
2044          string.  These functions typically are declared with
2045          first_arg_num == 0, so avoid warning in those cases.  */
2046       if (info->first_arg_num != 0 && warn_format > 1)
2047         status_warning (status, "format not a string literal, argument types not checked");
2048       return;
2049     }
2050   if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (format_tree))) != char_type_node)
2051     {
2052       status_warning (status, "format is a wide character string");
2053       return;
2054     }
2055   format_chars = TREE_STRING_POINTER (format_tree);
2056   format_length = TREE_STRING_LENGTH (format_tree);
2057   if (format_length <= 1)
2058     status_warning (status, "zero-length format string");
2059   if (format_chars[--format_length] != 0)
2060     {
2061       status_warning (status, "unterminated format string");
2062       return;
2063     }
2064   /* Skip to first argument to check.  */
2065   while (arg_num + 1 < info->first_arg_num)
2066     {
2067       if (params == 0)
2068         return;
2069       params = TREE_CHAIN (params);
2070       ++arg_num;
2071     }
2072
2073   first_fillin_param = params;
2074   init_dollar_format_checking (info->first_arg_num, first_fillin_param);
2075   fki = &format_types[info->format_type];
2076   while (1)
2077     {
2078       int aflag;
2079       first_wanted_type = NULL;
2080       last_wanted_type = NULL;
2081       if (*format_chars == 0)
2082         {
2083           if (format_chars - TREE_STRING_POINTER (format_tree) != format_length)
2084             status_warning (status, "embedded `\\0' in format");
2085           if (info->first_arg_num != 0 && params != 0
2086               && has_operand_number <= 0)
2087             status_warning (status, "too many arguments for format");
2088           if (has_operand_number > 0)
2089             finish_dollar_format_checking (status);
2090           return;
2091         }
2092       if (*format_chars++ != '%')
2093         continue;
2094       if (*format_chars == 0)
2095         {
2096           status_warning (status, "spurious trailing `%%' in format");
2097           continue;
2098         }
2099       if (*format_chars == '%')
2100         {
2101           ++format_chars;
2102           continue;
2103         }
2104       flag_chars[0] = 0;
2105       suppressed = wide = precise = FALSE;
2106       main_arg_num = 0;
2107       main_arg_params = 0;
2108       if (info->format_type == scanf_format_type)
2109         {
2110           int non_zero_width_char = FALSE;
2111           suppressed = *format_chars == '*';
2112           if (suppressed)
2113             ++format_chars;
2114           else if (has_operand_number != 0)
2115             {
2116               int opnum;
2117               opnum = maybe_read_dollar_number (status, &format_chars,
2118                                                 has_operand_number == 1,
2119                                                 first_fillin_param,
2120                                                 &main_arg_params);
2121               if (opnum == -1)
2122                 return;
2123               else if (opnum > 0)
2124                 {
2125                   has_operand_number = 1;
2126                   main_arg_num = opnum + info->first_arg_num - 1;
2127                 }
2128               else
2129                 has_operand_number = 0;
2130             }
2131           while (ISDIGIT (*format_chars))
2132             {
2133               wide = TRUE;
2134               if (*format_chars != '0')
2135                 non_zero_width_char = TRUE;
2136               ++format_chars;
2137             }
2138           if (wide && !non_zero_width_char)
2139             status_warning (status, "zero width in scanf format");
2140         }
2141       else if (info->format_type == strftime_format_type)
2142         {
2143           while (*format_chars != 0 && index ("_-0^#", *format_chars) != 0)
2144             {
2145               if (pedantic)
2146                 status_warning (status, "ISO C does not support the strftime `%c' flag",
2147                          *format_chars);
2148               if (index (flag_chars, *format_chars) != 0)
2149                 {
2150                   status_warning (status, "repeated `%c' flag in format",
2151                            *format_chars);
2152                   ++format_chars;
2153                 }
2154               else
2155                 {
2156                   i = strlen (flag_chars);
2157                   flag_chars[i++] = *format_chars++;
2158                   flag_chars[i] = 0;
2159                 }
2160             }
2161           while (ISDIGIT ((unsigned char) *format_chars))
2162             {
2163               wide = TRUE;
2164               ++format_chars;
2165             }
2166           if (wide && pedantic)
2167             status_warning (status, "ISO C does not support strftime format width");
2168           if (*format_chars == 'E' || *format_chars == 'O')
2169             {
2170               i = strlen (flag_chars);
2171               flag_chars[i++] = *format_chars++;
2172               flag_chars[i] = 0;
2173               if (*format_chars == 'E' || *format_chars == 'O')
2174                 {
2175                   status_warning (status, "multiple E/O modifiers in format");
2176                   while (*format_chars == 'E' || *format_chars == 'O')
2177                     ++format_chars;
2178                 }
2179             }
2180         }
2181       else if (info->format_type == printf_format_type)
2182         {
2183           if (has_operand_number != 0)
2184             {
2185               int opnum;
2186               opnum = maybe_read_dollar_number (status, &format_chars,
2187                                                 0, first_fillin_param,
2188                                                 &main_arg_params);
2189               if (opnum == -1)
2190                 return;
2191               else if (opnum > 0)
2192                 {
2193                   has_operand_number = 1;
2194                   main_arg_num = opnum + info->first_arg_num - 1;
2195                 }
2196             }
2197
2198           while (*format_chars != 0 && index (" +#0-'I", *format_chars) != 0)
2199             {
2200               if (index (flag_chars, *format_chars) != 0)
2201                 status_warning (status, "repeated `%c' flag in format", *format_chars++);
2202               else
2203                 {
2204                   i = strlen (flag_chars);
2205                   flag_chars[i++] = *format_chars++;
2206                   flag_chars[i] = 0;
2207                 }
2208             }
2209           /* "If the space and + flags both appear,
2210              the space flag will be ignored."  */
2211           if (index (flag_chars, ' ') != 0
2212               && index (flag_chars, '+') != 0)
2213             status_warning (status, "use of both ` ' and `+' flags in format");
2214           /* "If the 0 and - flags both appear,
2215              the 0 flag will be ignored."  */
2216           if (index (flag_chars, '0') != 0
2217               && index (flag_chars, '-') != 0)
2218             status_warning (status, "use of both `0' and `-' flags in format");
2219           if (index (flag_chars, '\'') && pedantic)
2220             status_warning (status, "ISO C does not support the `'' format flag");
2221           if (index (flag_chars, 'I') && pedantic)
2222             status_warning (status, "ISO C does not support the `I' format flag");
2223           if (*format_chars == '*')
2224             {
2225               wide = TRUE;
2226               /* "...a field width...may be indicated by an asterisk.
2227                  In this case, an int argument supplies the field width..."  */
2228               ++format_chars;
2229               if (params == 0)
2230                 {
2231                   status_warning (status, "too few arguments for format");
2232                   return;
2233                 }
2234               if (has_operand_number != 0)
2235                 {
2236                   int opnum;
2237                   opnum = maybe_read_dollar_number (status, &format_chars,
2238                                                     has_operand_number == 1,
2239                                                     first_fillin_param,
2240                                                     &params);
2241                   if (opnum == -1)
2242                     return;
2243                   else if (opnum > 0)
2244                     {
2245                       has_operand_number = 1;
2246                       arg_num = opnum + info->first_arg_num - 1;
2247                     }
2248                   else
2249                     has_operand_number = 0;
2250                 }
2251               if (info->first_arg_num != 0)
2252                 {
2253                   cur_param = TREE_VALUE (params);
2254                   if (has_operand_number <= 0)
2255                     {
2256                       params = TREE_CHAIN (params);
2257                       ++arg_num;
2258                     }
2259                   width_wanted_type.wanted_type = integer_type_node;
2260                   width_wanted_type.wanted_type_name = NULL;
2261                   width_wanted_type.pointer_count = 0;
2262                   width_wanted_type.char_lenient_flag = 0;
2263                   width_wanted_type.writing_in_flag = 0;
2264                   width_wanted_type.name = _("field width");
2265                   width_wanted_type.param = cur_param;
2266                   width_wanted_type.arg_num = arg_num;
2267                   width_wanted_type.next = NULL;
2268                   if (last_wanted_type != 0)
2269                     last_wanted_type->next = &width_wanted_type;
2270                   if (first_wanted_type == 0)
2271                     first_wanted_type = &width_wanted_type;
2272                   last_wanted_type = &width_wanted_type;
2273                 }
2274             }
2275           else
2276             {
2277               while (ISDIGIT (*format_chars))
2278                 {
2279                   wide = TRUE;
2280                   ++format_chars;
2281                 }
2282             }
2283           if (*format_chars == '.')
2284             {
2285               precise = TRUE;
2286               ++format_chars;
2287               /* "...a...precision...may be indicated by an asterisk.
2288                  In this case, an int argument supplies the...precision."  */
2289               if (*format_chars == '*')
2290                 {
2291                   ++format_chars;
2292                   if (has_operand_number != 0)
2293                     {
2294                       int opnum;
2295                       opnum = maybe_read_dollar_number (status, &format_chars,
2296                                                         has_operand_number == 1,
2297                                                         first_fillin_param,
2298                                                         &params);
2299                       if (opnum == -1)
2300                         return;
2301                       else if (opnum > 0)
2302                         {
2303                           has_operand_number = 1;
2304                           arg_num = opnum + info->first_arg_num - 1;
2305                         }
2306                       else
2307                         has_operand_number = 0;
2308                     }
2309                   if (info->first_arg_num != 0)
2310                     {
2311                       if (params == 0)
2312                         {
2313                           status_warning (status, "too few arguments for format");
2314                           return;
2315                         }
2316                       cur_param = TREE_VALUE (params);
2317                       if (has_operand_number <= 0)
2318                         {
2319                           params = TREE_CHAIN (params);
2320                           ++arg_num;
2321                         }
2322                       precision_wanted_type.wanted_type = integer_type_node;
2323                       precision_wanted_type.wanted_type_name = NULL;
2324                       precision_wanted_type.pointer_count = 0;
2325                       precision_wanted_type.char_lenient_flag = 0;
2326                       precision_wanted_type.writing_in_flag = 0;
2327                       precision_wanted_type.name = _("field precision");
2328                       precision_wanted_type.param = cur_param;
2329                       precision_wanted_type.arg_num = arg_num;
2330                       precision_wanted_type.next = NULL;
2331                       if (last_wanted_type != 0)
2332                         last_wanted_type->next = &precision_wanted_type;
2333                       if (first_wanted_type == 0)
2334                         first_wanted_type = &precision_wanted_type;
2335                       last_wanted_type = &precision_wanted_type;
2336                     }
2337                 }
2338               else
2339                 {
2340                   while (ISDIGIT (*format_chars))
2341                     ++format_chars;
2342                 }
2343             }
2344         }
2345
2346       aflag = 0;
2347
2348       fli = fki->length_char_specs;
2349       if (fli)
2350         {
2351           while (fli->name != 0 && fli->name[0] != *format_chars)
2352             fli++;
2353           if (fli->name != 0)
2354             {
2355               format_chars++;
2356               if (fli->double_name != 0 && fli->name[0] == *format_chars)
2357                 {
2358                   format_chars++;
2359                   length_chars = fli->double_name;
2360                   length_chars_val = fli->double_index;
2361                   length_chars_std = fli->double_std;
2362                 }
2363               else
2364                 {
2365                   length_chars = fli->name;
2366                   length_chars_val = fli->index;
2367                   length_chars_std = fli->std;
2368                 }
2369             }
2370           else
2371             {
2372               length_chars = NULL;
2373               length_chars_val = FMT_LEN_none;
2374               length_chars_std = STD_C89;
2375             }
2376           if (pedantic)
2377             {
2378               /* Warn if the length modifier is non-standard.  */
2379               if (length_chars_std == STD_EXT)
2380                 status_warning (status, "ISO C does not support the `%s' %s length modifier",
2381                          length_chars, fki->name);
2382               else if ((length_chars_std == STD_C99 && !flag_isoc99)
2383                        || (length_chars_std == STD_C94 && !flag_isoc94))
2384                 status_warning (status, "ISO C89 does not support the `%s' %s length modifier",
2385                          length_chars, fki->name);
2386             }
2387           if (*format_chars == 'a' && info->format_type == scanf_format_type
2388               && !flag_isoc99)
2389             {
2390               if (format_chars[1] == 's' || format_chars[1] == 'S'
2391                   || format_chars[1] == '[')
2392                 {
2393                   /* `a' is used as a flag.  */
2394                   aflag = 1;
2395                   format_chars++;
2396                 }
2397             }
2398           if (suppressed && length_chars_val != FMT_LEN_none)
2399             status_warning (status, "use of `*' and `%s' together in format", length_chars);
2400         }
2401       format_char = *format_chars;
2402       if (format_char == 0
2403           || (info->format_type != strftime_format_type && format_char == '%'))
2404         {
2405           status_warning (status, "conversion lacks type at end of format");
2406           continue;
2407         }
2408       format_chars++;
2409       fci = fki->conversion_specs;
2410       while (fci->format_chars != 0
2411              && index (fci->format_chars, format_char) == 0)
2412           ++fci;
2413       if (fci->format_chars == 0)
2414         {
2415           if (ISGRAPH(format_char))
2416             status_warning (status, "unknown conversion type character `%c' in format",
2417                      format_char);
2418           else
2419             status_warning (status, "unknown conversion type character 0x%x in format",
2420                      format_char);
2421           continue;
2422         }
2423       if (pedantic)
2424         {
2425           if (fci->std == STD_EXT)
2426             status_warning (status, "ISO C does not support the `%%%c' %s format",
2427                      format_char, fki->name);
2428           else if ((fci->std == STD_C99 && !flag_isoc99)
2429                    || (fci->std == STD_C94 && !flag_isoc94))
2430             status_warning (status, "ISO C89 does not support the `%%%c' %s format",
2431                      format_char, fki->name);
2432           if (index (flag_chars, 'O') != 0)
2433             {
2434               if (index (fci->flags2, 'o') != 0)
2435                 status_warning (status, "ISO C does not support `%%O%c'", format_char);
2436               else if (!flag_isoc99 && index (fci->flag_chars, 'O') != 0)
2437                 status_warning (status, "ISO C89 does not support `%%O%c'", format_char);
2438             }
2439           if (!flag_isoc99 && index (flag_chars, 'E'))
2440             status_warning (status, "ISO C89 does not support `%%E%c'", format_char);
2441         }
2442       if (wide && index (fci->flag_chars, 'w') == 0)
2443         status_warning (status, "width used with `%c' format", format_char);
2444       if (index (fci->flags2, '3') != 0
2445           || (format_char == 'y' && index (flag_chars, 'E')))
2446         status_warning (status, "`%%%c' yields only last 2 digits of year in some locales",
2447                  format_char);
2448       else if (index (fci->flags2, '2') != 0)
2449         status_warning (status, "`%%%c' yields only last 2 digits of year", format_char);
2450       if (precise && index (fci->flag_chars, 'p') == 0)
2451         status_warning (status, "precision used with `%c' format", format_char);
2452       if (aflag && index (fci->flag_chars, 'a') == 0)
2453         {
2454           status_warning (status, "`a' flag used with `%c' format", format_char);
2455           /* To simplify the following code.  */
2456           aflag = 0;
2457         }
2458       /* The a flag is a GNU extension.  */
2459       else if (pedantic && aflag)
2460         status_warning (status, "ISO C does not support the `a' flag");
2461       if (index (fci->flags2, '[') != 0)
2462         {
2463           /* Skip over scan set, in case it happens to have '%' in it.  */
2464           if (*format_chars == '^')
2465             ++format_chars;
2466           /* Find closing bracket; if one is hit immediately, then
2467              it's part of the scan set rather than a terminator.  */
2468           if (*format_chars == ']')
2469             ++format_chars;
2470           while (*format_chars && *format_chars != ']')
2471             ++format_chars;
2472           if (*format_chars != ']')
2473             /* The end of the format string was reached.  */
2474             status_warning (status, "no closing `]' for `%%[' format");
2475         }
2476       if (suppressed)
2477         {
2478           if (index (fci->flag_chars, '*') == 0)
2479             status_warning (status, "suppression of `%c' conversion in format", format_char);
2480           continue;
2481         }
2482       for (i = 0; flag_chars[i] != 0; ++i)
2483         {
2484           if (index (fci->flag_chars, flag_chars[i]) == 0)
2485             status_warning (status, "flag `%c' used with type `%c'",
2486                      flag_chars[i], format_char);
2487         }
2488       if (info->format_type == strftime_format_type)
2489         continue;
2490       if (precise && index (flag_chars, '0') != 0
2491           && (index (fci->flags2, 'i') != 0))
2492         status_warning (status, "`0' flag ignored with precision specifier and `%c' format",
2493                  format_char);
2494       wanted_type = (fci->types[length_chars_val].type
2495                      ? *fci->types[length_chars_val].type : 0);
2496       wanted_type_name = fci->types[length_chars_val].name;
2497       wanted_type_std = fci->types[length_chars_val].std;
2498       if (wanted_type == 0)
2499         {
2500           status_warning (status, "use of `%s' length modifier with `%c' type character",
2501                    length_chars, format_char);
2502           /* Heuristic: skip one argument when an invalid length/type
2503              combination is encountered.  */
2504           arg_num++;
2505           if (params == 0)
2506             {
2507               status_warning (status, "too few arguments for format");
2508               return;
2509             }
2510           params = TREE_CHAIN (params);
2511           continue;
2512         }
2513       else if (pedantic
2514                /* Warn if non-standard, provided it is more non-standard
2515                   than the length and type characters that may already
2516                   have been warned for.  */
2517                && wanted_type_std > length_chars_std
2518                && wanted_type_std > fci->std)
2519         {
2520           if (wanted_type_std == STD_EXT)
2521             status_warning (status, "ISO C does not support the `%%%s%c' %s format",
2522                      length_chars, format_char, fki->name);
2523           else if ((wanted_type_std == STD_C99 && !flag_isoc99)
2524                    || (wanted_type_std == STD_C94 && !flag_isoc94))
2525             status_warning (status, "ISO C89 does not support the `%%%s%c' %s format",
2526                      length_chars, format_char, fki->name);
2527         }
2528
2529       /* Finally. . .check type of argument against desired type!  */
2530       if (info->first_arg_num == 0)
2531         continue;
2532       if (fci->pointer_count == 0 && wanted_type == void_type_node)
2533         {
2534           if (main_arg_num != 0)
2535             status_warning (status, "operand number specified for format taking no argument");
2536         }
2537       else
2538         {
2539           if (main_arg_num != 0)
2540             {
2541               arg_num = main_arg_num;
2542               params = main_arg_params;
2543             }
2544           else
2545             {
2546               ++arg_num;
2547               if (has_operand_number > 0)
2548                 {
2549                   status_warning (status, "missing $ operand number in format");
2550                   return;
2551                 }
2552               else
2553                 has_operand_number = 0;
2554               if (params == 0)
2555                 {
2556                   status_warning (status, "too few arguments for format");
2557                   return;
2558                 }
2559             }
2560           cur_param = TREE_VALUE (params);
2561           params = TREE_CHAIN (params);
2562           main_wanted_type.wanted_type = wanted_type;
2563           main_wanted_type.wanted_type_name = wanted_type_name;
2564           main_wanted_type.pointer_count = fci->pointer_count + aflag;
2565           main_wanted_type.char_lenient_flag = 0;
2566           if (index (fci->flags2, 'c') != 0)
2567             main_wanted_type.char_lenient_flag = 1;
2568           main_wanted_type.writing_in_flag = 0;
2569           if (index (fci->flags2, 'W') != 0)
2570             main_wanted_type.writing_in_flag = 1;
2571           main_wanted_type.name = NULL;
2572           main_wanted_type.param = cur_param;
2573           main_wanted_type.arg_num = arg_num;
2574           main_wanted_type.next = NULL;
2575           if (last_wanted_type != 0)
2576             last_wanted_type->next = &main_wanted_type;
2577           if (first_wanted_type == 0)
2578             first_wanted_type = &main_wanted_type;
2579           last_wanted_type = &main_wanted_type;
2580         }
2581
2582       if (first_wanted_type != 0)
2583         check_format_types (status, first_wanted_type);
2584
2585     }
2586 }
2587
2588
2589 /* Check the argument types from a single format conversion (possibly
2590    including width and precision arguments).  */
2591 static void
2592 check_format_types (status, types)
2593      int *status;
2594      format_wanted_type *types;
2595 {
2596   for (; types != 0; types = types->next)
2597     {
2598       tree cur_param;
2599       tree cur_type;
2600       tree orig_cur_type;
2601       tree wanted_type;
2602       int arg_num;
2603       int i;
2604       int char_type_flag;
2605       cur_param = types->param;
2606       cur_type = TREE_TYPE (cur_param);
2607       if (TREE_CODE (cur_type) == ERROR_MARK)
2608         continue;
2609       char_type_flag = 0;
2610       wanted_type = types->wanted_type;
2611       arg_num = types->arg_num;
2612
2613       /* The following should not occur here.  */
2614       if (wanted_type == 0)
2615         abort ();
2616       if (wanted_type == void_type_node && types->pointer_count == 0)
2617         abort ();
2618
2619       STRIP_NOPS (cur_param);
2620
2621       /* Check the types of any additional pointer arguments
2622          that precede the "real" argument.  */
2623       for (i = 0; i < types->pointer_count; ++i)
2624         {
2625           if (TREE_CODE (cur_type) == POINTER_TYPE)
2626             {
2627               cur_type = TREE_TYPE (cur_type);
2628               if (TREE_CODE (cur_type) == ERROR_MARK)
2629                 break;
2630
2631               if (cur_param != 0 && TREE_CODE (cur_param) == ADDR_EXPR)
2632                 cur_param = TREE_OPERAND (cur_param, 0);
2633               else
2634                 cur_param = 0;
2635
2636               /* See if this is an attempt to write into a const type with
2637                  scanf or with printf "%n".  Note: the writing in happens
2638                  at the first indirection only, if for example
2639                  void * const * is passed to scanf %p; passing
2640                  const void ** is simply passing an incompatible type.  */
2641               if (types->writing_in_flag
2642                   && i == 0
2643                   && (TYPE_READONLY (cur_type)
2644                       || (cur_param != 0
2645                           && (TREE_CODE_CLASS (TREE_CODE (cur_param)) == 'c'
2646                               || (DECL_P (cur_param)
2647                                   && TREE_READONLY (cur_param))))))
2648                 status_warning (status, "writing into constant object (arg %d)", arg_num);
2649
2650               /* If there are extra type qualifiers beyond the first
2651                  indirection, then this makes the types technically
2652                  incompatible.  */
2653               if (i > 0
2654                   && pedantic
2655                   && (TYPE_READONLY (cur_type)
2656                       || TYPE_VOLATILE (cur_type)
2657                       || TYPE_RESTRICT (cur_type)))
2658                 status_warning (status, "extra type qualifiers in format argument (arg %d)",
2659                          arg_num);
2660
2661             }
2662           else
2663             {
2664               if (types->pointer_count == 1)
2665                 status_warning (status, "format argument is not a pointer (arg %d)", arg_num);
2666               else
2667                 status_warning (status, "format argument is not a pointer to a pointer (arg %d)", arg_num);
2668               break;
2669             }
2670         }
2671
2672       if (i < types->pointer_count)
2673         continue;
2674
2675       orig_cur_type = cur_type;
2676       cur_type = TYPE_MAIN_VARIANT (cur_type);
2677
2678       /* Check whether the argument type is a character type.  This leniency
2679          only applies to certain formats, flagged with 'c'.
2680       */
2681       if (types->char_lenient_flag)
2682         char_type_flag = (cur_type == char_type_node
2683                           || cur_type == signed_char_type_node
2684                           || cur_type == unsigned_char_type_node);
2685
2686       /* Check the type of the "real" argument, if there's a type we want.  */
2687       if (wanted_type == cur_type)
2688         continue;
2689       /* If we want `void *', allow any pointer type.
2690          (Anything else would already have got a warning.)
2691          With -pedantic, only allow pointers to void and to character
2692          types.  */
2693       if (wanted_type == void_type_node
2694           && (!pedantic || (i == 1 && char_type_flag)))
2695         continue;
2696       /* Don't warn about differences merely in signedness, unless
2697          -pedantic.  With -pedantic, warn if the type is a pointer
2698          target and not a character type, and for character types at
2699          a second level of indirection.  */
2700       if (TREE_CODE (wanted_type) == INTEGER_TYPE
2701           && TREE_CODE (cur_type) == INTEGER_TYPE
2702           && (! pedantic || i == 0 || (i == 1 && char_type_flag))
2703           && (TREE_UNSIGNED (wanted_type)
2704               ? wanted_type == unsigned_type (cur_type)
2705               : wanted_type == signed_type (cur_type)))
2706         continue;
2707       /* Likewise, "signed char", "unsigned char" and "char" are
2708          equivalent but the above test won't consider them equivalent.  */
2709       if (wanted_type == char_type_node
2710           && (! pedantic || i < 2)
2711           && char_type_flag)
2712         continue;
2713       /* Now we have a type mismatch.  */
2714       {
2715         register const char *this;
2716         register const char *that;
2717
2718         this = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (wanted_type)));
2719         that = 0;
2720         if (TYPE_NAME (orig_cur_type) != 0
2721             && TREE_CODE (orig_cur_type) != INTEGER_TYPE
2722             && !(TREE_CODE (orig_cur_type) == POINTER_TYPE
2723                  && TREE_CODE (TREE_TYPE (orig_cur_type)) == INTEGER_TYPE))
2724           {
2725             if (TREE_CODE (TYPE_NAME (orig_cur_type)) == TYPE_DECL
2726                 && DECL_NAME (TYPE_NAME (orig_cur_type)) != 0)
2727               that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (orig_cur_type)));
2728             else
2729               that = IDENTIFIER_POINTER (TYPE_NAME (orig_cur_type));
2730           }
2731
2732         /* A nameless type can't possibly match what the format wants.
2733            So there will be a warning for it.
2734            Make up a string to describe vaguely what it is.  */
2735         if (that == 0)
2736           {
2737             if (TREE_CODE (orig_cur_type) == POINTER_TYPE)
2738               that = "pointer";
2739             else
2740               that = "different type";
2741           }
2742
2743         /* Make the warning better in case of mismatch of int vs long.  */
2744         if (TREE_CODE (orig_cur_type) == INTEGER_TYPE
2745             && TREE_CODE (wanted_type) == INTEGER_TYPE
2746             && TYPE_PRECISION (orig_cur_type) == TYPE_PRECISION (wanted_type)
2747             && TYPE_NAME (orig_cur_type) != 0
2748             && TREE_CODE (TYPE_NAME (orig_cur_type)) == TYPE_DECL)
2749           that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (orig_cur_type)));
2750
2751         if (strcmp (this, that) != 0)
2752           {
2753             /* There may be a better name for the format, e.g. size_t,
2754                but we should allow for programs with a perverse typedef
2755                making size_t something other than what the compiler
2756                thinks.  */
2757             if (types->wanted_type_name != 0
2758                 && strcmp (types->wanted_type_name, that) != 0)
2759               this = types->wanted_type_name;
2760             if (types->name != 0)
2761               status_warning (status, "%s is not type %s (arg %d)", types->name, this,
2762                        arg_num);
2763             else
2764               status_warning (status, "%s format, %s arg (arg %d)", this, that, arg_num);
2765           }
2766       }
2767     }
2768 }
2769 \f
2770 /* Print a warning if a constant expression had overflow in folding.
2771    Invoke this function on every expression that the language
2772    requires to be a constant expression.
2773    Note the ANSI C standard says it is erroneous for a
2774    constant expression to overflow.  */
2775
2776 void
2777 constant_expression_warning (value)
2778      tree value;
2779 {
2780   if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
2781        || TREE_CODE (value) == COMPLEX_CST)
2782       && TREE_CONSTANT_OVERFLOW (value) && pedantic)
2783     pedwarn ("overflow in constant expression");
2784 }
2785
2786 /* Print a warning if an expression had overflow in folding.
2787    Invoke this function on every expression that
2788    (1) appears in the source code, and
2789    (2) might be a constant expression that overflowed, and
2790    (3) is not already checked by convert_and_check;
2791    however, do not invoke this function on operands of explicit casts.  */
2792
2793 void
2794 overflow_warning (value)
2795      tree value;
2796 {
2797   if ((TREE_CODE (value) == INTEGER_CST
2798        || (TREE_CODE (value) == COMPLEX_CST
2799            && TREE_CODE (TREE_REALPART (value)) == INTEGER_CST))
2800       && TREE_OVERFLOW (value))
2801     {
2802       TREE_OVERFLOW (value) = 0;
2803       if (skip_evaluation == 0)
2804         warning ("integer overflow in expression");
2805     }
2806   else if ((TREE_CODE (value) == REAL_CST
2807             || (TREE_CODE (value) == COMPLEX_CST
2808                 && TREE_CODE (TREE_REALPART (value)) == REAL_CST))
2809            && TREE_OVERFLOW (value))
2810     {
2811       TREE_OVERFLOW (value) = 0;
2812       if (skip_evaluation == 0)
2813         warning ("floating point overflow in expression");
2814     }
2815 }
2816
2817 /* Print a warning if a large constant is truncated to unsigned,
2818    or if -Wconversion is used and a constant < 0 is converted to unsigned.
2819    Invoke this function on every expression that might be implicitly
2820    converted to an unsigned type.  */
2821
2822 void
2823 unsigned_conversion_warning (result, operand)
2824      tree result, operand;
2825 {
2826   if (TREE_CODE (operand) == INTEGER_CST
2827       && TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
2828       && TREE_UNSIGNED (TREE_TYPE (result))
2829       && skip_evaluation == 0
2830       && !int_fits_type_p (operand, TREE_TYPE (result)))
2831     {
2832       if (!int_fits_type_p (operand, signed_type (TREE_TYPE (result))))
2833         /* This detects cases like converting -129 or 256 to unsigned char.  */
2834         warning ("large integer implicitly truncated to unsigned type");
2835       else if (warn_conversion)
2836         warning ("negative integer implicitly converted to unsigned type");
2837     }
2838 }
2839
2840 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2841    Invoke this function on every expression that is converted implicitly,
2842    i.e. because of language rules and not because of an explicit cast.  */
2843
2844 tree
2845 convert_and_check (type, expr)
2846      tree type, expr;
2847 {
2848   tree t = convert (type, expr);
2849   if (TREE_CODE (t) == INTEGER_CST)
2850     {
2851       if (TREE_OVERFLOW (t))
2852         {
2853           TREE_OVERFLOW (t) = 0;
2854
2855           /* Do not diagnose overflow in a constant expression merely
2856              because a conversion overflowed.  */
2857           TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (expr);
2858
2859           /* No warning for converting 0x80000000 to int.  */
2860           if (!(TREE_UNSIGNED (type) < TREE_UNSIGNED (TREE_TYPE (expr))
2861                 && TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
2862                 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (expr))))
2863             /* If EXPR fits in the unsigned version of TYPE,
2864                don't warn unless pedantic.  */
2865             if ((pedantic
2866                  || TREE_UNSIGNED (type)
2867                  || ! int_fits_type_p (expr, unsigned_type (type)))
2868                 && skip_evaluation == 0)
2869               warning ("overflow in implicit constant conversion");
2870         }
2871       else
2872         unsigned_conversion_warning (t, expr);
2873     }
2874   return t;
2875 }
2876 \f
2877 void
2878 c_expand_expr_stmt (expr)
2879      tree expr;
2880 {
2881   /* Do default conversion if safe and possibly important,
2882      in case within ({...}).  */
2883   if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE && lvalue_p (expr))
2884       || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
2885     expr = default_conversion (expr);
2886
2887   if (TREE_TYPE (expr) != error_mark_node
2888       && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
2889       && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
2890     error ("expression statement has incomplete type");
2891
2892   last_expr_type = TREE_TYPE (expr); 
2893   add_stmt (build_stmt (EXPR_STMT, expr));
2894 }
2895 \f
2896 /* Validate the expression after `case' and apply default promotions.  */
2897
2898 tree
2899 check_case_value (value)
2900      tree value;
2901 {
2902   if (value == NULL_TREE)
2903     return value;
2904
2905   /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue.  */
2906   STRIP_TYPE_NOPS (value);
2907   /* In C++, the following is allowed:
2908
2909        const int i = 3;
2910        switch (...) { case i: ... }
2911
2912      So, we try to reduce the VALUE to a constant that way.  */
2913   if (c_language == clk_cplusplus)
2914     {
2915       value = decl_constant_value (value);
2916       STRIP_TYPE_NOPS (value);
2917       value = fold (value);
2918     }
2919
2920   if (TREE_CODE (value) != INTEGER_CST
2921       && value != error_mark_node)
2922     {
2923       error ("case label does not reduce to an integer constant");
2924       value = error_mark_node;
2925     }
2926   else
2927     /* Promote char or short to int.  */
2928     value = default_conversion (value);
2929
2930   constant_expression_warning (value);
2931
2932   return value;
2933 }
2934 \f
2935 /* Return an integer type with BITS bits of precision,
2936    that is unsigned if UNSIGNEDP is nonzero, otherwise signed.  */
2937
2938 tree
2939 type_for_size (bits, unsignedp)
2940      unsigned bits;
2941      int unsignedp;
2942 {
2943   if (bits == TYPE_PRECISION (integer_type_node))
2944     return unsignedp ? unsigned_type_node : integer_type_node;
2945
2946   if (bits == TYPE_PRECISION (signed_char_type_node))
2947     return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2948
2949   if (bits == TYPE_PRECISION (short_integer_type_node))
2950     return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2951
2952   if (bits == TYPE_PRECISION (long_integer_type_node))
2953     return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2954
2955   if (bits == TYPE_PRECISION (long_long_integer_type_node))
2956     return (unsignedp ? long_long_unsigned_type_node
2957             : long_long_integer_type_node);
2958
2959   if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
2960     return (unsignedp ? widest_unsigned_literal_type_node
2961             : widest_integer_literal_type_node);
2962
2963   if (bits <= TYPE_PRECISION (intQI_type_node))
2964     return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2965
2966   if (bits <= TYPE_PRECISION (intHI_type_node))
2967     return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2968
2969   if (bits <= TYPE_PRECISION (intSI_type_node))
2970     return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2971
2972   if (bits <= TYPE_PRECISION (intDI_type_node))
2973     return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2974
2975   return 0;
2976 }
2977
2978 /* Return a data type that has machine mode MODE.
2979    If the mode is an integer,
2980    then UNSIGNEDP selects between signed and unsigned types.  */
2981
2982 tree
2983 type_for_mode (mode, unsignedp)
2984      enum machine_mode mode;
2985      int unsignedp;
2986 {
2987   if (mode == TYPE_MODE (integer_type_node))
2988     return unsignedp ? unsigned_type_node : integer_type_node;
2989
2990   if (mode == TYPE_MODE (signed_char_type_node))
2991     return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2992
2993   if (mode == TYPE_MODE (short_integer_type_node))
2994     return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2995
2996   if (mode == TYPE_MODE (long_integer_type_node))
2997     return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2998
2999   if (mode == TYPE_MODE (long_long_integer_type_node))
3000     return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3001
3002   if (mode == TYPE_MODE (widest_integer_literal_type_node))
3003     return unsignedp ? widest_unsigned_literal_type_node
3004                      : widest_integer_literal_type_node;
3005
3006   if (mode == TYPE_MODE (intQI_type_node))
3007     return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3008
3009   if (mode == TYPE_MODE (intHI_type_node))
3010     return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3011
3012   if (mode == TYPE_MODE (intSI_type_node))
3013     return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3014
3015   if (mode == TYPE_MODE (intDI_type_node))
3016     return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3017
3018 #if HOST_BITS_PER_WIDE_INT >= 64
3019   if (mode == TYPE_MODE (intTI_type_node))
3020     return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3021 #endif
3022
3023   if (mode == TYPE_MODE (float_type_node))
3024     return float_type_node;
3025
3026   if (mode == TYPE_MODE (double_type_node))
3027     return double_type_node;
3028
3029   if (mode == TYPE_MODE (long_double_type_node))
3030     return long_double_type_node;
3031
3032   if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
3033     return build_pointer_type (char_type_node);
3034
3035   if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
3036     return build_pointer_type (integer_type_node);
3037
3038 #ifdef VECTOR_MODE_SUPPORTED_P
3039   if (mode == TYPE_MODE (V4SF_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
3040     return V4SF_type_node;
3041   if (mode == TYPE_MODE (V4SI_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
3042     return V4SI_type_node;
3043   if (mode == TYPE_MODE (V2SI_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
3044     return V2SI_type_node;
3045   if (mode == TYPE_MODE (V4HI_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
3046     return V4HI_type_node;
3047   if (mode == TYPE_MODE (V8QI_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
3048     return V8QI_type_node;
3049 #endif
3050
3051   return 0;
3052 }
3053
3054 /* Return an unsigned type the same as TYPE in other respects. */
3055 tree
3056 unsigned_type (type)
3057      tree type;
3058 {
3059   tree type1 = TYPE_MAIN_VARIANT (type);
3060   if (type1 == signed_char_type_node || type1 == char_type_node)
3061     return unsigned_char_type_node;
3062   if (type1 == integer_type_node)
3063     return unsigned_type_node;
3064   if (type1 == short_integer_type_node)
3065     return short_unsigned_type_node;
3066   if (type1 == long_integer_type_node)
3067     return long_unsigned_type_node;
3068   if (type1 == long_long_integer_type_node)
3069     return long_long_unsigned_type_node;
3070   if (type1 == widest_integer_literal_type_node)
3071     return widest_unsigned_literal_type_node;
3072 #if HOST_BITS_PER_WIDE_INT >= 64
3073   if (type1 == intTI_type_node)
3074     return unsigned_intTI_type_node;
3075 #endif
3076   if (type1 == intDI_type_node)
3077     return unsigned_intDI_type_node;
3078   if (type1 == intSI_type_node)
3079     return unsigned_intSI_type_node;
3080   if (type1 == intHI_type_node)
3081     return unsigned_intHI_type_node;
3082   if (type1 == intQI_type_node)
3083     return unsigned_intQI_type_node;
3084
3085   return signed_or_unsigned_type (1, type);
3086 }
3087
3088 /* Return a signed type the same as TYPE in other respects.  */
3089
3090 tree
3091 signed_type (type)
3092      tree type;
3093 {
3094   tree type1 = TYPE_MAIN_VARIANT (type);
3095   if (type1 == unsigned_char_type_node || type1 == char_type_node)
3096     return signed_char_type_node;
3097   if (type1 == unsigned_type_node)
3098     return integer_type_node;
3099   if (type1 == short_unsigned_type_node)
3100     return short_integer_type_node;
3101   if (type1 == long_unsigned_type_node)
3102     return long_integer_type_node;
3103   if (type1 == long_long_unsigned_type_node)
3104     return long_long_integer_type_node;
3105   if (type1 == widest_unsigned_literal_type_node)
3106     return widest_integer_literal_type_node;
3107 #if HOST_BITS_PER_WIDE_INT >= 64
3108   if (type1 == unsigned_intTI_type_node)
3109     return intTI_type_node;
3110 #endif
3111   if (type1 == unsigned_intDI_type_node)
3112     return intDI_type_node;
3113   if (type1 == unsigned_intSI_type_node)
3114     return intSI_type_node;
3115   if (type1 == unsigned_intHI_type_node)
3116     return intHI_type_node;
3117   if (type1 == unsigned_intQI_type_node)
3118     return intQI_type_node;
3119
3120   return signed_or_unsigned_type (0, type);
3121 }
3122
3123 /* Return a type the same as TYPE except unsigned or
3124    signed according to UNSIGNEDP.  */
3125
3126 tree
3127 signed_or_unsigned_type (unsignedp, type)
3128      int unsignedp;
3129      tree type;
3130 {
3131   if (! INTEGRAL_TYPE_P (type)
3132       || TREE_UNSIGNED (type) == unsignedp)
3133     return type;
3134
3135   if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node))
3136     return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3137   if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
3138     return unsignedp ? unsigned_type_node : integer_type_node;
3139   if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node))
3140     return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3141   if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node))
3142     return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3143   if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node))
3144     return (unsignedp ? long_long_unsigned_type_node
3145             : long_long_integer_type_node);
3146   if (TYPE_PRECISION (type) == TYPE_PRECISION (widest_integer_literal_type_node))
3147     return (unsignedp ? widest_unsigned_literal_type_node
3148             : widest_integer_literal_type_node);
3149   return type;
3150 }
3151 \f
3152 /* Return the minimum number of bits needed to represent VALUE in a
3153    signed or unsigned type, UNSIGNEDP says which.  */
3154
3155 unsigned int
3156 min_precision (value, unsignedp)
3157      tree value;
3158      int unsignedp;
3159 {
3160   int log;
3161
3162   /* If the value is negative, compute its negative minus 1.  The latter
3163      adjustment is because the absolute value of the largest negative value
3164      is one larger than the largest positive value.  This is equivalent to
3165      a bit-wise negation, so use that operation instead.  */
3166
3167   if (tree_int_cst_sgn (value) < 0)
3168     value = fold (build1 (BIT_NOT_EXPR, TREE_TYPE (value), value));
3169
3170   /* Return the number of bits needed, taking into account the fact
3171      that we need one more bit for a signed than unsigned type.  */
3172
3173   if (integer_zerop (value))
3174     log = 0;
3175   else
3176     log = tree_floor_log2 (value);
3177
3178   return log + 1 + ! unsignedp;
3179 }
3180 \f
3181 /* Print an error message for invalid operands to arith operation CODE.
3182    NOP_EXPR is used as a special case (see truthvalue_conversion).  */
3183
3184 void
3185 binary_op_error (code)
3186      enum tree_code code;
3187 {
3188   register const char *opname;
3189
3190   switch (code)
3191     {
3192     case NOP_EXPR:
3193       error ("invalid truth-value expression");
3194       return;
3195
3196     case PLUS_EXPR:
3197       opname = "+"; break;
3198     case MINUS_EXPR:
3199       opname = "-"; break;
3200     case MULT_EXPR:
3201       opname = "*"; break;
3202     case MAX_EXPR:
3203       opname = "max"; break;
3204     case MIN_EXPR:
3205       opname = "min"; break;
3206     case EQ_EXPR:
3207       opname = "=="; break;
3208     case NE_EXPR:
3209       opname = "!="; break;
3210     case LE_EXPR:
3211       opname = "<="; break;
3212     case GE_EXPR:
3213       opname = ">="; break;
3214     case LT_EXPR:
3215       opname = "<"; break;
3216     case GT_EXPR:
3217       opname = ">"; break;
3218     case LSHIFT_EXPR:
3219       opname = "<<"; break;
3220     case RSHIFT_EXPR:
3221       opname = ">>"; break;
3222     case TRUNC_MOD_EXPR:
3223     case FLOOR_MOD_EXPR:
3224       opname = "%"; break;
3225     case TRUNC_DIV_EXPR:
3226     case FLOOR_DIV_EXPR:
3227       opname = "/"; break;
3228     case BIT_AND_EXPR:
3229       opname = "&"; break;
3230     case BIT_IOR_EXPR:
3231       opname = "|"; break;
3232     case TRUTH_ANDIF_EXPR:
3233       opname = "&&"; break;
3234     case TRUTH_ORIF_EXPR:
3235       opname = "||"; break;
3236     case BIT_XOR_EXPR:
3237       opname = "^"; break;
3238     case LROTATE_EXPR:
3239     case RROTATE_EXPR:
3240       opname = "rotate"; break;
3241     default:
3242       opname = "unknown"; break;
3243     }
3244   error ("invalid operands to binary %s", opname);
3245 }
3246 \f
3247 /* Subroutine of build_binary_op, used for comparison operations.
3248    See if the operands have both been converted from subword integer types
3249    and, if so, perhaps change them both back to their original type.
3250    This function is also responsible for converting the two operands
3251    to the proper common type for comparison.
3252
3253    The arguments of this function are all pointers to local variables
3254    of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
3255    RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
3256
3257    If this function returns nonzero, it means that the comparison has
3258    a constant value.  What this function returns is an expression for
3259    that value.  */
3260
3261 tree
3262 shorten_compare (op0_ptr, op1_ptr, restype_ptr, rescode_ptr)
3263      tree *op0_ptr, *op1_ptr;
3264      tree *restype_ptr;
3265      enum tree_code *rescode_ptr;
3266 {
3267   register tree type;
3268   tree op0 = *op0_ptr;
3269   tree op1 = *op1_ptr;
3270   int unsignedp0, unsignedp1;
3271   int real1, real2;
3272   tree primop0, primop1;
3273   enum tree_code code = *rescode_ptr;
3274
3275   /* Throw away any conversions to wider types
3276      already present in the operands.  */
3277
3278   primop0 = get_narrower (op0, &unsignedp0);
3279   primop1 = get_narrower (op1, &unsignedp1);
3280
3281   /* Handle the case that OP0 does not *contain* a conversion
3282      but it *requires* conversion to FINAL_TYPE.  */
3283
3284   if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
3285     unsignedp0 = TREE_UNSIGNED (TREE_TYPE (op0));
3286   if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
3287     unsignedp1 = TREE_UNSIGNED (TREE_TYPE (op1));
3288
3289   /* If one of the operands must be floated, we cannot optimize.  */
3290   real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
3291   real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
3292
3293   /* If first arg is constant, swap the args (changing operation
3294      so value is preserved), for canonicalization.  Don't do this if
3295      the second arg is 0.  */
3296
3297   if (TREE_CONSTANT (primop0)
3298       && ! integer_zerop (primop1) && ! real_zerop (primop1))
3299     {
3300       register tree tem = primop0;
3301       register int temi = unsignedp0;
3302       primop0 = primop1;
3303       primop1 = tem;
3304       tem = op0;
3305       op0 = op1;
3306       op1 = tem;
3307       *op0_ptr = op0;
3308       *op1_ptr = op1;
3309       unsignedp0 = unsignedp1;
3310       unsignedp1 = temi;
3311       temi = real1;
3312       real1 = real2;
3313       real2 = temi;
3314
3315       switch (code)
3316         {
3317         case LT_EXPR:
3318           code = GT_EXPR;
3319           break;
3320         case GT_EXPR:
3321           code = LT_EXPR;
3322           break;
3323         case LE_EXPR:
3324           code = GE_EXPR;
3325           break;
3326         case GE_EXPR:
3327           code = LE_EXPR;
3328           break;
3329         default:
3330           break;
3331         }
3332       *rescode_ptr = code;
3333     }
3334
3335   /* If comparing an integer against a constant more bits wide,
3336      maybe we can deduce a value of 1 or 0 independent of the data.
3337      Or else truncate the constant now
3338      rather than extend the variable at run time.
3339
3340      This is only interesting if the constant is the wider arg.
3341      Also, it is not safe if the constant is unsigned and the
3342      variable arg is signed, since in this case the variable
3343      would be sign-extended and then regarded as unsigned.
3344      Our technique fails in this case because the lowest/highest
3345      possible unsigned results don't follow naturally from the
3346      lowest/highest possible values of the variable operand.
3347      For just EQ_EXPR and NE_EXPR there is another technique that
3348      could be used: see if the constant can be faithfully represented
3349      in the other operand's type, by truncating it and reextending it
3350      and see if that preserves the constant's value.  */
3351
3352   if (!real1 && !real2
3353       && TREE_CODE (primop1) == INTEGER_CST
3354       && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
3355     {
3356       int min_gt, max_gt, min_lt, max_lt;
3357       tree maxval, minval;
3358       /* 1 if comparison is nominally unsigned.  */
3359       int unsignedp = TREE_UNSIGNED (*restype_ptr);
3360       tree val;
3361
3362       type = signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0));
3363
3364       /* If TYPE is an enumeration, then we need to get its min/max
3365          values from it's underlying integral type, not the enumerated
3366          type itself.  */
3367       if (TREE_CODE (type) == ENUMERAL_TYPE)
3368         type = type_for_size (TYPE_PRECISION (type), unsignedp0);
3369
3370       maxval = TYPE_MAX_VALUE (type);
3371       minval = TYPE_MIN_VALUE (type);
3372
3373       if (unsignedp && !unsignedp0)
3374         *restype_ptr = signed_type (*restype_ptr);
3375
3376       if (TREE_TYPE (primop1) != *restype_ptr)
3377         primop1 = convert (*restype_ptr, primop1);
3378       if (type != *restype_ptr)
3379         {
3380           minval = convert (*restype_ptr, minval);
3381           maxval = convert (*restype_ptr, maxval);
3382         }
3383
3384       if (unsignedp && unsignedp0)
3385         {
3386           min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
3387           max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
3388           min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
3389           max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
3390         }
3391       else
3392         {
3393           min_gt = INT_CST_LT (primop1, minval);
3394           max_gt = INT_CST_LT (primop1, maxval);
3395           min_lt = INT_CST_LT (minval, primop1);
3396           max_lt = INT_CST_LT (maxval, primop1);
3397         }
3398
3399       val = 0;
3400       /* This used to be a switch, but Genix compiler can't handle that.  */
3401       if (code == NE_EXPR)
3402         {
3403           if (max_lt || min_gt)
3404             val = boolean_true_node;
3405         }
3406       else if (code == EQ_EXPR)
3407         {
3408           if (max_lt || min_gt)
3409             val = boolean_false_node;
3410         }
3411       else if (code == LT_EXPR)
3412         {
3413           if (max_lt)
3414             val = boolean_true_node;
3415           if (!min_lt)
3416             val = boolean_false_node;
3417         }
3418       else if (code == GT_EXPR)
3419         {
3420           if (min_gt)
3421             val = boolean_true_node;
3422           if (!max_gt)
3423             val = boolean_false_node;
3424         }
3425       else if (code == LE_EXPR)
3426         {
3427           if (!max_gt)
3428             val = boolean_true_node;
3429           if (min_gt)
3430             val = boolean_false_node;
3431         }
3432       else if (code == GE_EXPR)
3433         {
3434           if (!min_lt)
3435             val = boolean_true_node;
3436           if (max_lt)
3437             val = boolean_false_node;
3438         }
3439
3440       /* If primop0 was sign-extended and unsigned comparison specd,
3441          we did a signed comparison above using the signed type bounds.
3442          But the comparison we output must be unsigned.
3443
3444          Also, for inequalities, VAL is no good; but if the signed
3445          comparison had *any* fixed result, it follows that the
3446          unsigned comparison just tests the sign in reverse
3447          (positive values are LE, negative ones GE).
3448          So we can generate an unsigned comparison
3449          against an extreme value of the signed type.  */
3450
3451       if (unsignedp && !unsignedp0)
3452         {
3453           if (val != 0)
3454             switch (code)
3455               {
3456               case LT_EXPR:
3457               case GE_EXPR:
3458                 primop1 = TYPE_MIN_VALUE (type);
3459                 val = 0;
3460                 break;
3461
3462               case LE_EXPR:
3463               case GT_EXPR:
3464                 primop1 = TYPE_MAX_VALUE (type);
3465                 val = 0;
3466                 break;
3467
3468               default:
3469                 break;
3470               }
3471           type = unsigned_type (type);
3472         }
3473
3474       if (!max_gt && !unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
3475         {
3476           /* This is the case of (char)x >?< 0x80, which people used to use
3477              expecting old C compilers to change the 0x80 into -0x80.  */
3478           if (val == boolean_false_node)
3479             warning ("comparison is always false due to limited range of data type");
3480           if (val == boolean_true_node)
3481             warning ("comparison is always true due to limited range of data type");
3482         }
3483
3484       if (!min_lt && unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
3485         {
3486           /* This is the case of (unsigned char)x >?< -1 or < 0.  */
3487           if (val == boolean_false_node)
3488             warning ("comparison is always false due to limited range of data type");
3489           if (val == boolean_true_node)
3490             warning ("comparison is always true due to limited range of data type");
3491         }
3492
3493       if (val != 0)
3494         {
3495           /* Don't forget to evaluate PRIMOP0 if it has side effects.  */
3496           if (TREE_SIDE_EFFECTS (primop0))
3497             return build (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
3498           return val;
3499         }
3500
3501       /* Value is not predetermined, but do the comparison
3502          in the type of the operand that is not constant.
3503          TYPE is already properly set.  */
3504     }
3505   else if (real1 && real2
3506            && (TYPE_PRECISION (TREE_TYPE (primop0))
3507                == TYPE_PRECISION (TREE_TYPE (primop1))))
3508     type = TREE_TYPE (primop0);
3509
3510   /* If args' natural types are both narrower than nominal type
3511      and both extend in the same manner, compare them
3512      in the type of the wider arg.
3513      Otherwise must actually extend both to the nominal
3514      common type lest different ways of extending
3515      alter the result.
3516      (eg, (short)-1 == (unsigned short)-1  should be 0.)  */
3517
3518   else if (unsignedp0 == unsignedp1 && real1 == real2
3519            && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
3520            && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
3521     {
3522       type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
3523       type = signed_or_unsigned_type (unsignedp0
3524                                       || TREE_UNSIGNED (*restype_ptr),
3525                                       type);
3526       /* Make sure shorter operand is extended the right way
3527          to match the longer operand.  */
3528       primop0 = convert (signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0)),
3529                          primop0);
3530       primop1 = convert (signed_or_unsigned_type (unsignedp1, TREE_TYPE (primop1)),
3531                          primop1);
3532     }
3533   else
3534     {
3535       /* Here we must do the comparison on the nominal type
3536          using the args exactly as we received them.  */
3537       type = *restype_ptr;
3538       primop0 = op0;
3539       primop1 = op1;
3540
3541       if (!real1 && !real2 && integer_zerop (primop1)
3542           && TREE_UNSIGNED (*restype_ptr))
3543         {
3544           tree value = 0;
3545           switch (code)
3546             {
3547             case GE_EXPR:
3548               /* All unsigned values are >= 0, so we warn if extra warnings
3549                  are requested.  However, if OP0 is a constant that is
3550                  >= 0, the signedness of the comparison isn't an issue,
3551                  so suppress the warning.  */
3552               if (extra_warnings && !in_system_header
3553                   && ! (TREE_CODE (primop0) == INTEGER_CST
3554                         && ! TREE_OVERFLOW (convert (signed_type (type),
3555                                                      primop0))))
3556                 warning ("comparison of unsigned expression >= 0 is always true");
3557               value = boolean_true_node;
3558               break;
3559
3560             case LT_EXPR:
3561               if (extra_warnings && !in_system_header
3562                   && ! (TREE_CODE (primop0) == INTEGER_CST
3563                         && ! TREE_OVERFLOW (convert (signed_type (type),
3564                                                      primop0))))
3565                 warning ("comparison of unsigned expression < 0 is always false");
3566               value = boolean_false_node;
3567               break;
3568
3569             default:
3570               break;
3571             }
3572
3573           if (value != 0)
3574             {
3575               /* Don't forget to evaluate PRIMOP0 if it has side effects.  */
3576               if (TREE_SIDE_EFFECTS (primop0))
3577                 return build (COMPOUND_EXPR, TREE_TYPE (value),
3578                               primop0, value);
3579               return value;
3580             }
3581         }
3582     }
3583
3584   *op0_ptr = convert (type, primop0);
3585   *op1_ptr = convert (type, primop1);
3586
3587   *restype_ptr = boolean_type_node;
3588
3589   return 0;
3590 }
3591 \f
3592 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
3593    or validate its data type for an `if' or `while' statement or ?..: exp.
3594
3595    This preparation consists of taking the ordinary
3596    representation of an expression expr and producing a valid tree
3597    boolean expression describing whether expr is nonzero.  We could
3598    simply always do build_binary_op (NE_EXPR, expr, boolean_false_node, 1),
3599    but we optimize comparisons, &&, ||, and !.
3600
3601    The resulting type should always be `boolean_type_node'.  */
3602
3603 tree
3604 truthvalue_conversion (expr)
3605      tree expr;
3606 {
3607   if (TREE_CODE (expr) == ERROR_MARK)
3608     return expr;
3609
3610 #if 0 /* This appears to be wrong for C++.  */
3611   /* These really should return error_mark_node after 2.4 is stable.
3612      But not all callers handle ERROR_MARK properly.  */
3613   switch (TREE_CODE (TREE_TYPE (expr)))
3614     {
3615     case RECORD_TYPE:
3616       error ("struct type value used where scalar is required");
3617       return boolean_false_node;
3618
3619     case UNION_TYPE:
3620       error ("union type value used where scalar is required");
3621       return boolean_false_node;
3622
3623     case ARRAY_TYPE:
3624       error ("array type value used where scalar is required");
3625       return boolean_false_node;
3626
3627     default:
3628       break;
3629     }
3630 #endif /* 0 */
3631
3632   switch (TREE_CODE (expr))
3633     {
3634     case EQ_EXPR:
3635     case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
3636     case TRUTH_ANDIF_EXPR:
3637     case TRUTH_ORIF_EXPR:
3638     case TRUTH_AND_EXPR:
3639     case TRUTH_OR_EXPR:
3640     case TRUTH_XOR_EXPR:
3641     case TRUTH_NOT_EXPR:
3642       TREE_TYPE (expr) = boolean_type_node;
3643       return expr;
3644
3645     case ERROR_MARK:
3646       return expr;
3647
3648     case INTEGER_CST:
3649       return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
3650
3651     case REAL_CST:
3652       return real_zerop (expr) ? boolean_false_node : boolean_true_node;
3653
3654     case ADDR_EXPR:
3655       /* If we are taking the address of a external decl, it might be zero
3656          if it is weak, so we cannot optimize.  */
3657       if (DECL_P (TREE_OPERAND (expr, 0))
3658           && DECL_EXTERNAL (TREE_OPERAND (expr, 0)))
3659         break;
3660
3661       if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0)))
3662         return build (COMPOUND_EXPR, boolean_type_node,
3663                       TREE_OPERAND (expr, 0), boolean_true_node);
3664       else
3665         return boolean_true_node;
3666
3667     case COMPLEX_EXPR:
3668       return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
3669                                ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
3670                               truthvalue_conversion (TREE_OPERAND (expr, 0)),
3671                               truthvalue_conversion (TREE_OPERAND (expr, 1)),
3672                               0);
3673
3674     case NEGATE_EXPR:
3675     case ABS_EXPR:
3676     case FLOAT_EXPR:
3677     case FFS_EXPR:
3678       /* These don't change whether an object is non-zero or zero.  */
3679       return truthvalue_conversion (TREE_OPERAND (expr, 0));
3680
3681     case LROTATE_EXPR:
3682     case RROTATE_EXPR:
3683       /* These don't change whether an object is zero or non-zero, but
3684          we can't ignore them if their second arg has side-effects.  */
3685       if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
3686         return build (COMPOUND_EXPR, boolean_type_node, TREE_OPERAND (expr, 1),
3687                       truthvalue_conversion (TREE_OPERAND (expr, 0)));
3688       else
3689         return truthvalue_conversion (TREE_OPERAND (expr, 0));
3690
3691     case COND_EXPR:
3692       /* Distribute the conversion into the arms of a COND_EXPR.  */
3693       return fold (build (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
3694                           truthvalue_conversion (TREE_OPERAND (expr, 1)),
3695                           truthvalue_conversion (TREE_OPERAND (expr, 2))));
3696
3697     case CONVERT_EXPR:
3698       /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
3699          since that affects how `default_conversion' will behave.  */
3700       if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE
3701           || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
3702         break;
3703       /* fall through...  */
3704     case NOP_EXPR:
3705       /* If this is widening the argument, we can ignore it.  */
3706       if (TYPE_PRECISION (TREE_TYPE (expr))
3707           >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
3708         return truthvalue_conversion (TREE_OPERAND (expr, 0));
3709       break;
3710
3711     case MINUS_EXPR:
3712       /* With IEEE arithmetic, x - x may not equal 0, so we can't optimize
3713          this case.  */
3714       if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
3715           && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE)
3716         break;
3717       /* fall through...  */
3718     case BIT_XOR_EXPR:
3719       /* This and MINUS_EXPR can be changed into a comparison of the
3720          two objects.  */
3721       if (TREE_TYPE (TREE_OPERAND (expr, 0))
3722           == TREE_TYPE (TREE_OPERAND (expr, 1)))
3723         return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
3724                                 TREE_OPERAND (expr, 1), 1);
3725       return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
3726                               fold (build1 (NOP_EXPR,
3727                                             TREE_TYPE (TREE_OPERAND (expr, 0)),
3728                                             TREE_OPERAND (expr, 1))), 1);
3729
3730     case BIT_AND_EXPR:
3731       if (integer_onep (TREE_OPERAND (expr, 1))
3732           && TREE_TYPE (expr) != boolean_type_node)
3733         /* Using convert here would cause infinite recursion.  */
3734         return build1 (NOP_EXPR, boolean_type_node, expr);
3735       break;
3736
3737     case MODIFY_EXPR:
3738       if (warn_parentheses && C_EXP_ORIGINAL_CODE (expr) == MODIFY_EXPR)
3739         warning ("suggest parentheses around assignment used as truth value");
3740       break;
3741
3742     default:
3743       break;
3744     }
3745
3746   if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
3747     {
3748       tree tem = save_expr (expr);
3749       return (build_binary_op
3750               ((TREE_SIDE_EFFECTS (expr)
3751                 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
3752                truthvalue_conversion (build_unary_op (REALPART_EXPR, tem, 0)),
3753                truthvalue_conversion (build_unary_op (IMAGPART_EXPR, tem, 0)),
3754                0));
3755     }
3756
3757   return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
3758 }
3759 \f
3760 #if !USE_CPPLIB
3761 /* Read the rest of a #-directive from input stream FINPUT.
3762    In normal use, the directive name and the white space after it
3763    have already been read, so they won't be included in the result.
3764    We allow for the fact that the directive line may contain
3765    a newline embedded within a character or string literal which forms
3766    a part of the directive.
3767
3768    The value is a string in a reusable buffer.  It remains valid
3769    only until the next time this function is called.
3770
3771    The terminating character ('\n' or EOF) is left in FINPUT for the
3772    caller to re-read.  */
3773
3774 char *
3775 get_directive_line (finput)
3776      register FILE *finput;
3777 {
3778   static char *directive_buffer = NULL;
3779   static unsigned buffer_length = 0;
3780   register char *p;
3781   register char *buffer_limit;
3782   register int looking_for = 0;
3783   register int char_escaped = 0;
3784
3785   if (buffer_length == 0)
3786     {
3787       directive_buffer = (char *)xmalloc (128);
3788       buffer_length = 128;
3789     }
3790
3791   buffer_limit = &directive_buffer[buffer_length];
3792
3793   for (p = directive_buffer; ; )
3794     {
3795       int c;
3796
3797       /* Make buffer bigger if it is full.  */
3798       if (p >= buffer_limit)
3799         {
3800           register unsigned bytes_used = (p - directive_buffer);
3801
3802           buffer_length *= 2;
3803           directive_buffer
3804             = (char *)xrealloc (directive_buffer, buffer_length);
3805           p = &directive_buffer[bytes_used];
3806           buffer_limit = &directive_buffer[buffer_length];
3807         }
3808
3809       c = getc (finput);
3810
3811       /* Discard initial whitespace.  */
3812       if ((c == ' ' || c == '\t') && p == directive_buffer)
3813         continue;
3814
3815       /* Detect the end of the directive.  */
3816       if (looking_for == 0
3817           && (c == '\n' || c == EOF))
3818         {
3819           ungetc (c, finput);
3820           c = '\0';
3821         }
3822
3823       *p++ = c;
3824
3825       if (c == 0)
3826         return directive_buffer;
3827
3828       /* Handle string and character constant syntax.  */
3829       if (looking_for)
3830         {
3831           if (looking_for == c && !char_escaped)
3832             looking_for = 0;    /* Found terminator... stop looking.  */
3833         }
3834       else
3835         if (c == '\'' || c == '"')
3836           looking_for = c;      /* Don't stop buffering until we see another
3837                                    one of these (or an EOF).  */
3838
3839       /* Handle backslash.  */
3840       char_escaped = (c == '\\' && ! char_escaped);
3841     }
3842 }
3843 #endif /* USE_CPPLIB */
3844 \f
3845 /* Make a variant type in the proper way for C/C++, propagating qualifiers
3846    down to the element type of an array.  */
3847
3848 tree
3849 c_build_qualified_type (type, type_quals)
3850      tree type;
3851      int type_quals;
3852 {
3853   /* A restrict-qualified pointer type must be a pointer to object or
3854      incomplete type.  Note that the use of POINTER_TYPE_P also allows
3855      REFERENCE_TYPEs, which is appropriate for C++.  Unfortunately,
3856      the C++ front-end also use POINTER_TYPE for pointer-to-member
3857      values, so even though it should be illegal to use `restrict'
3858      with such an entity we don't flag that here.  Thus, special case
3859      code for that case is required in the C++ front-end.  */
3860   if ((type_quals & TYPE_QUAL_RESTRICT)
3861       && (!POINTER_TYPE_P (type)
3862           || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
3863     {
3864       error ("invalid use of `restrict'");
3865       type_quals &= ~TYPE_QUAL_RESTRICT;
3866     }
3867
3868   if (TREE_CODE (type) == ARRAY_TYPE)
3869     return build_array_type (c_build_qualified_type (TREE_TYPE (type),
3870                                                      type_quals),
3871                              TYPE_DOMAIN (type));
3872   return build_qualified_type (type, type_quals);
3873 }
3874
3875 /* Apply the TYPE_QUALS to the new DECL.  */
3876
3877 void
3878 c_apply_type_quals_to_decl (type_quals, decl)
3879      int type_quals;
3880      tree decl;
3881 {
3882   if ((type_quals & TYPE_QUAL_CONST)
3883       || (TREE_TYPE (decl) 
3884           && TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE))
3885     TREE_READONLY (decl) = 1;
3886   if (type_quals & TYPE_QUAL_VOLATILE)
3887     {
3888       TREE_SIDE_EFFECTS (decl) = 1;
3889       TREE_THIS_VOLATILE (decl) = 1;
3890     }
3891   if (type_quals & TYPE_QUAL_RESTRICT)
3892     {
3893       if (!TREE_TYPE (decl)
3894           || !POINTER_TYPE_P (TREE_TYPE (decl))
3895           || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (TREE_TYPE (decl))))
3896         error ("invalid use of `restrict'");
3897       else if (flag_strict_aliasing)
3898         {
3899           /* No two restricted pointers can point at the same thing.
3900              However, a restricted pointer can point at the same thing
3901              as an unrestricted pointer, if that unrestricted pointer
3902              is based on the restricted pointer.  So, we make the
3903              alias set for the restricted pointer a subset of the
3904              alias set for the type pointed to by the type of the
3905              decl.  */
3906
3907           HOST_WIDE_INT pointed_to_alias_set
3908             = get_alias_set (TREE_TYPE (TREE_TYPE (decl)));
3909
3910           if (pointed_to_alias_set == 0)
3911             /* It's not legal to make a subset of alias set zero.  */
3912             ;
3913           else
3914             {
3915               DECL_POINTER_ALIAS_SET (decl) = new_alias_set ();
3916               record_alias_subset  (pointed_to_alias_set,
3917                                     DECL_POINTER_ALIAS_SET (decl));
3918             }
3919         }
3920     }
3921 }
3922
3923
3924 /* Return the typed-based alias set for T, which may be an expression
3925    or a type.  Return -1 if we don't do anything special.  */
3926
3927 HOST_WIDE_INT
3928 lang_get_alias_set (t)
3929      tree t;
3930 {
3931   tree u;
3932
3933   /* Permit type-punning when accessing a union, provided the access
3934      is directly through the union.  For example, this code does not
3935      permit taking the address of a union member and then storing
3936      through it.  Even the type-punning allowed here is a GCC
3937      extension, albeit a common and useful one; the C standard says
3938      that such accesses have implementation-defined behavior.  */
3939   for (u = t;
3940        TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
3941        u = TREE_OPERAND (u, 0))
3942     if (TREE_CODE (u) == COMPONENT_REF
3943         && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
3944       return 0;
3945
3946   /* If this is a char *, the ANSI C standard says it can alias
3947      anything.  Note that all references need do this.  */
3948   if (TREE_CODE_CLASS (TREE_CODE (t)) == 'r'
3949       && TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
3950       && TYPE_PRECISION (TREE_TYPE (t)) == TYPE_PRECISION (char_type_node))
3951     return 0;
3952
3953   /* That's all the expressions we handle specially.  */
3954   if (! TYPE_P (t))
3955     return -1;
3956
3957   /* The C standard specifically allows aliasing between signed and
3958      unsigned variants of the same type.  We treat the signed
3959      variant as canonical.  */
3960   if (TREE_CODE (t) == INTEGER_TYPE && TREE_UNSIGNED (t))
3961     {
3962       tree t1 = signed_type (t);
3963
3964       /* t1 == t can happen for boolean nodes which are always unsigned.  */
3965       if (t1 != t)
3966         return get_alias_set (t1);
3967     }
3968   else if (POINTER_TYPE_P (t))
3969     {
3970       tree t1;
3971
3972       /* Unfortunately, there is no canonical form of a pointer type.
3973          In particular, if we have `typedef int I', then `int *', and
3974          `I *' are different types.  So, we have to pick a canonical
3975          representative.  We do this below.
3976
3977          Technically, this approach is actually more conservative that
3978          it needs to be.  In particular, `const int *' and `int *'
3979          chould be in different alias sets, according to the C and C++
3980          standard, since their types are not the same, and so,
3981          technically, an `int **' and `const int **' cannot point at
3982          the same thing.
3983
3984          But, the standard is wrong.  In particular, this code is
3985          legal C++:
3986
3987             int *ip;
3988             int **ipp = &ip;
3989             const int* const* cipp = &ip;
3990
3991          And, it doesn't make sense for that to be legal unless you
3992          can dereference IPP and CIPP.  So, we ignore cv-qualifiers on
3993          the pointed-to types.  This issue has been reported to the
3994          C++ committee.  */
3995       t1 = TYPE_MAIN_VARIANT (TREE_TYPE (t));
3996       t1 = ((TREE_CODE (t) == POINTER_TYPE)
3997            ? build_pointer_type (t1) : build_reference_type (t1));
3998       if (t1 != t)
3999         return get_alias_set (t1);
4000     }
4001   /* It's not yet safe to use alias sets for classes in C++ because
4002      the TYPE_FIELDs list for a class doesn't mention base classes.  */
4003   else if (c_language == clk_cplusplus && AGGREGATE_TYPE_P (t))
4004     return 0;
4005
4006   return -1;
4007 }
4008
4009 /* Build tree nodes and builtin functions common to both C and C++ language
4010    frontends.
4011    CPLUS_MODE is nonzero if we are called from the C++ frontend, we generate
4012    some stricter prototypes in that case.
4013    NO_BUILTINS and NO_NONANSI_BUILTINS contain the respective values of
4014    the language frontend flags flag_no_builtin and
4015    flag_no_nonansi_builtin.  */
4016
4017 void
4018 c_common_nodes_and_builtins (cplus_mode, no_builtins, no_nonansi_builtins)
4019     int cplus_mode, no_builtins, no_nonansi_builtins;
4020 {
4021   tree temp;
4022   tree memcpy_ftype, memset_ftype, strlen_ftype;
4023   tree bzero_ftype, bcmp_ftype, puts_ftype, printf_ftype;
4024   tree endlink, int_endlink, double_endlink, unsigned_endlink;
4025   tree sizetype_endlink;
4026   tree ptr_ftype, ptr_ftype_unsigned;
4027   tree void_ftype_any, void_ftype_int, int_ftype_any, sizet_ftype_any;
4028   tree double_ftype_double, double_ftype_double_double;
4029   tree float_ftype_float, ldouble_ftype_ldouble;
4030   tree int_ftype_cptr_cptr_sizet;
4031   tree int_ftype_string_string, string_ftype_ptr_ptr;
4032   tree long_ftype_long;
4033   tree longlong_ftype_longlong;
4034   /* Either char* or void*.  */
4035   tree traditional_ptr_type_node;
4036   /* Either const char* or const void*.  */
4037   tree traditional_cptr_type_node;
4038   tree traditional_len_type_node;
4039   tree traditional_len_endlink;
4040   tree va_list_ref_type_node;
4041   tree va_list_arg_type_node;
4042
4043   pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_va_list"),
4044                         va_list_type_node));
4045
4046   pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_ptrdiff_t"),
4047                         ptrdiff_type_node));
4048
4049   pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_size_t"),
4050                         sizetype));
4051
4052   if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
4053     {
4054       va_list_arg_type_node = va_list_ref_type_node =
4055         build_pointer_type (TREE_TYPE (va_list_type_node));
4056     }
4057   else
4058     {
4059       va_list_arg_type_node = va_list_type_node;
4060       va_list_ref_type_node = build_reference_type (va_list_type_node);
4061     }
4062  
4063   endlink = void_list_node;
4064   int_endlink = tree_cons (NULL_TREE, integer_type_node, endlink);
4065   double_endlink = tree_cons (NULL_TREE, double_type_node, endlink);
4066   unsigned_endlink = tree_cons (NULL_TREE, unsigned_type_node, endlink);
4067
4068   ptr_ftype = build_function_type (ptr_type_node, NULL_TREE);
4069   ptr_ftype_unsigned = build_function_type (ptr_type_node, unsigned_endlink);
4070   sizetype_endlink = tree_cons (NULL_TREE, TYPE_DOMAIN (sizetype), endlink);
4071   /* We realloc here because sizetype could be int or unsigned.  S'ok.  */
4072   ptr_ftype_sizetype = build_function_type (ptr_type_node, sizetype_endlink);
4073
4074   sizet_ftype_any = build_function_type (sizetype, NULL_TREE);
4075   int_ftype_any = build_function_type (integer_type_node, NULL_TREE);
4076   void_ftype_any = build_function_type (void_type_node, NULL_TREE);
4077   void_ftype = build_function_type (void_type_node, endlink);
4078   void_ftype_int = build_function_type (void_type_node, int_endlink);
4079   void_ftype_ptr
4080     = build_function_type (void_type_node,
4081                            tree_cons (NULL_TREE, ptr_type_node, endlink));
4082
4083   float_ftype_float
4084     = build_function_type (float_type_node,
4085                            tree_cons (NULL_TREE, float_type_node, endlink));
4086
4087   double_ftype_double
4088     = build_function_type (double_type_node, double_endlink);
4089
4090   ldouble_ftype_ldouble
4091     = build_function_type (long_double_type_node,
4092                            tree_cons (NULL_TREE, long_double_type_node,
4093                                       endlink));
4094
4095   double_ftype_double_double
4096     = build_function_type (double_type_node,
4097                            tree_cons (NULL_TREE, double_type_node,
4098                                       double_endlink));
4099
4100   int_ftype_int
4101     = build_function_type (integer_type_node, int_endlink);
4102
4103   long_ftype_long
4104     = build_function_type (long_integer_type_node,
4105                            tree_cons (NULL_TREE, long_integer_type_node,
4106                                       endlink));
4107
4108   longlong_ftype_longlong
4109     = build_function_type (long_long_integer_type_node,
4110                            tree_cons (NULL_TREE, long_long_integer_type_node,
4111                                       endlink));
4112
4113   int_ftype_cptr_cptr_sizet
4114     = build_function_type (integer_type_node,
4115                            tree_cons (NULL_TREE, const_ptr_type_node,
4116                                       tree_cons (NULL_TREE, const_ptr_type_node,
4117                                                  tree_cons (NULL_TREE,
4118                                                             sizetype,
4119                                                             endlink))));
4120
4121   void_zero_node = build_int_2 (0, 0);
4122   TREE_TYPE (void_zero_node) = void_type_node;
4123
4124   /* Prototype for strcpy.  */
4125   string_ftype_ptr_ptr
4126     = build_function_type (string_type_node,
4127                            tree_cons (NULL_TREE, string_type_node,
4128                                       tree_cons (NULL_TREE,
4129                                                  const_string_type_node,
4130                                                  endlink)));
4131
4132   traditional_len_type_node = (flag_traditional && ! cplus_mode
4133                                ? integer_type_node : sizetype);
4134   traditional_len_endlink = tree_cons (NULL_TREE, traditional_len_type_node,
4135                                        endlink);
4136
4137   /* Prototype for strcmp.  */
4138   int_ftype_string_string
4139     = build_function_type (integer_type_node,
4140                            tree_cons (NULL_TREE, const_string_type_node,
4141                                       tree_cons (NULL_TREE,
4142                                                  const_string_type_node,
4143                                                  endlink)));
4144
4145   /* Prototype for strlen.  */
4146   strlen_ftype
4147     = build_function_type (traditional_len_type_node,
4148                            tree_cons (NULL_TREE, const_string_type_node,
4149                                       endlink));
4150
4151   traditional_ptr_type_node = (flag_traditional && ! cplus_mode
4152                                ? string_type_node : ptr_type_node);
4153   traditional_cptr_type_node = (flag_traditional && ! cplus_mode
4154                                ? const_string_type_node : const_ptr_type_node);
4155
4156   /* Prototype for memcpy.  */
4157   memcpy_ftype
4158     = build_function_type (traditional_ptr_type_node,
4159                            tree_cons (NULL_TREE, ptr_type_node,
4160                                       tree_cons (NULL_TREE, const_ptr_type_node,
4161                                                  sizetype_endlink)));
4162
4163   /* Prototype for memset.  */
4164   memset_ftype
4165     = build_function_type (traditional_ptr_type_node,
4166                            tree_cons (NULL_TREE, ptr_type_node,
4167                                       tree_cons (NULL_TREE, integer_type_node,
4168                                                  tree_cons (NULL_TREE,
4169                                                             sizetype,
4170                                                             endlink))));
4171
4172   /* Prototype for bzero.  */
4173   bzero_ftype
4174     = build_function_type (void_type_node,
4175                            tree_cons (NULL_TREE, traditional_ptr_type_node,
4176                                       traditional_len_endlink));
4177
4178   /* Prototype for bcmp.  */
4179   bcmp_ftype
4180     = build_function_type (integer_type_node,
4181                            tree_cons (NULL_TREE, traditional_cptr_type_node,
4182                                       tree_cons (NULL_TREE,
4183                                                  traditional_cptr_type_node,
4184                                                  traditional_len_endlink)));
4185
4186   /* Prototype for puts.  */
4187   puts_ftype
4188     = build_function_type (integer_type_node,
4189                            tree_cons (NULL_TREE, const_string_type_node,
4190                                       endlink));
4191
4192   /* Prototype for printf.  */
4193   printf_ftype
4194     = build_function_type (integer_type_node,
4195                            tree_cons (NULL_TREE, const_string_type_node,
4196                                       NULL_TREE));
4197
4198   builtin_function ("__builtin_constant_p", default_function_type,
4199                     BUILT_IN_CONSTANT_P, BUILT_IN_NORMAL, NULL_PTR);
4200
4201   builtin_function ("__builtin_return_address", ptr_ftype_unsigned,
4202                     BUILT_IN_RETURN_ADDRESS, BUILT_IN_NORMAL, NULL_PTR);
4203
4204   builtin_function ("__builtin_frame_address", ptr_ftype_unsigned,
4205                     BUILT_IN_FRAME_ADDRESS, BUILT_IN_NORMAL, NULL_PTR);
4206
4207   builtin_function ("__builtin_alloca", ptr_ftype_sizetype,
4208                     BUILT_IN_ALLOCA, BUILT_IN_NORMAL, "alloca");
4209   builtin_function ("__builtin_ffs", int_ftype_int, BUILT_IN_FFS,
4210                     BUILT_IN_NORMAL, NULL_PTR);
4211   /* Define alloca, ffs as builtins.
4212      Declare _exit just to mark it as volatile.  */
4213   if (! no_builtins && ! no_nonansi_builtins)
4214     {
4215 #ifndef SMALL_STACK
4216       temp = builtin_function ("alloca", ptr_ftype_sizetype,
4217                                BUILT_IN_ALLOCA, BUILT_IN_NORMAL, NULL_PTR);
4218       /* Suppress error if redefined as a non-function.  */
4219       DECL_BUILT_IN_NONANSI (temp) = 1;
4220 #endif
4221       temp = builtin_function ("ffs", int_ftype_int, BUILT_IN_FFS,
4222                                BUILT_IN_NORMAL, NULL_PTR);
4223       /* Suppress error if redefined as a non-function.  */
4224       DECL_BUILT_IN_NONANSI (temp) = 1;
4225       temp = builtin_function ("_exit", void_ftype_int,
4226                                0, NOT_BUILT_IN, NULL_PTR);
4227       TREE_THIS_VOLATILE (temp) = 1;
4228       TREE_SIDE_EFFECTS (temp) = 1;
4229       /* Suppress error if redefined as a non-function.  */
4230       DECL_BUILT_IN_NONANSI (temp) = 1;
4231
4232       /* The system prototypes for these functions have many
4233          variations, so don't specify parameters to avoid conflicts.
4234          The expand_* functions check the argument types anyway.  */
4235       temp = builtin_function ("bzero", void_ftype_any,
4236                                BUILT_IN_BZERO, BUILT_IN_NORMAL, NULL_PTR);
4237       DECL_BUILT_IN_NONANSI (temp) = 1;
4238       temp = builtin_function ("bcmp", int_ftype_any,
4239                                BUILT_IN_BCMP, BUILT_IN_NORMAL, NULL_PTR);
4240       DECL_BUILT_IN_NONANSI (temp) = 1;
4241     }
4242
4243   builtin_function ("__builtin_abs", int_ftype_int, BUILT_IN_ABS,
4244                     BUILT_IN_NORMAL, NULL_PTR);
4245   builtin_function ("__builtin_fabsf", float_ftype_float, BUILT_IN_FABS,
4246                     BUILT_IN_NORMAL, NULL_PTR);
4247   builtin_function ("__builtin_fabs", double_ftype_double, BUILT_IN_FABS,
4248                     BUILT_IN_NORMAL, NULL_PTR);
4249   builtin_function ("__builtin_fabsl", ldouble_ftype_ldouble, BUILT_IN_FABS,
4250                     BUILT_IN_NORMAL, NULL_PTR);
4251   builtin_function ("__builtin_labs", long_ftype_long, BUILT_IN_LABS,
4252                     BUILT_IN_NORMAL, NULL_PTR);
4253   builtin_function ("__builtin_llabs", longlong_ftype_longlong, BUILT_IN_LLABS,
4254                     BUILT_IN_NORMAL, NULL_PTR);
4255   builtin_function ("__builtin_saveregs", ptr_ftype, BUILT_IN_SAVEREGS,
4256                     BUILT_IN_NORMAL, NULL_PTR);
4257   builtin_function ("__builtin_classify_type", default_function_type,
4258                     BUILT_IN_CLASSIFY_TYPE, BUILT_IN_NORMAL, NULL_PTR);
4259   builtin_function ("__builtin_next_arg", ptr_ftype, BUILT_IN_NEXT_ARG,
4260                     BUILT_IN_NORMAL, NULL_PTR);
4261   builtin_function ("__builtin_args_info", int_ftype_int, BUILT_IN_ARGS_INFO,
4262                     BUILT_IN_NORMAL, NULL_PTR);
4263   builtin_function ("__builtin_setjmp",
4264                     build_function_type (integer_type_node,
4265                                          tree_cons (NULL_TREE, ptr_type_node,
4266                                                     endlink)),
4267                     BUILT_IN_SETJMP, BUILT_IN_NORMAL, NULL_PTR);
4268   builtin_function ("__builtin_longjmp",
4269                     build_function_type (void_type_node,
4270                                          tree_cons (NULL_TREE, ptr_type_node,
4271                                                     tree_cons (NULL_TREE,
4272                                                                integer_type_node,
4273                                                                endlink))),
4274                     BUILT_IN_LONGJMP, BUILT_IN_NORMAL, NULL_PTR);
4275   builtin_function ("__builtin_trap", void_ftype, BUILT_IN_TRAP,
4276                     BUILT_IN_NORMAL, NULL_PTR);
4277
4278   /* ISO C99 IEEE Unordered compares.  */
4279   builtin_function ("__builtin_isgreater", default_function_type,
4280                     BUILT_IN_ISGREATER, BUILT_IN_NORMAL, NULL_PTR);
4281   builtin_function ("__builtin_isgreaterequal", default_function_type,
4282                     BUILT_IN_ISGREATEREQUAL, BUILT_IN_NORMAL, NULL_PTR);
4283   builtin_function ("__builtin_isless", default_function_type,
4284                     BUILT_IN_ISLESS, BUILT_IN_NORMAL, NULL_PTR);
4285   builtin_function ("__builtin_islessequal", default_function_type,
4286                     BUILT_IN_ISLESSEQUAL, BUILT_IN_NORMAL, NULL_PTR);
4287   builtin_function ("__builtin_islessgreater", default_function_type,
4288                     BUILT_IN_ISLESSGREATER, BUILT_IN_NORMAL, NULL_PTR);
4289   builtin_function ("__builtin_isunordered", default_function_type,
4290                     BUILT_IN_ISUNORDERED, BUILT_IN_NORMAL, NULL_PTR);
4291
4292   /* Untyped call and return.  */
4293   builtin_function ("__builtin_apply_args", ptr_ftype,
4294                     BUILT_IN_APPLY_ARGS, BUILT_IN_NORMAL, NULL_PTR);
4295
4296   temp = tree_cons (NULL_TREE,
4297                     build_pointer_type (build_function_type (void_type_node,
4298                                                              NULL_TREE)),
4299                     tree_cons (NULL_TREE,
4300                                ptr_type_node,
4301                                tree_cons (NULL_TREE,
4302                                           sizetype,
4303                                           endlink)));
4304   builtin_function ("__builtin_apply",
4305                     build_function_type (ptr_type_node, temp),
4306                     BUILT_IN_APPLY, BUILT_IN_NORMAL, NULL_PTR);
4307   builtin_function ("__builtin_return", void_ftype_ptr,
4308                     BUILT_IN_RETURN, BUILT_IN_NORMAL, NULL_PTR);
4309
4310   /* Support for varargs.h and stdarg.h.  */
4311   builtin_function ("__builtin_varargs_start",
4312                     build_function_type (void_type_node,
4313                                          tree_cons (NULL_TREE,
4314                                                     va_list_ref_type_node,
4315                                                     endlink)),
4316                     BUILT_IN_VARARGS_START, BUILT_IN_NORMAL, NULL_PTR);
4317
4318   builtin_function ("__builtin_stdarg_start",
4319                     build_function_type (void_type_node,
4320                                          tree_cons (NULL_TREE,
4321                                                     va_list_ref_type_node,
4322                                                     NULL_TREE)),
4323                     BUILT_IN_STDARG_START, BUILT_IN_NORMAL, NULL_PTR);
4324
4325   builtin_function ("__builtin_va_end",
4326                     build_function_type (void_type_node,
4327                                          tree_cons (NULL_TREE,
4328                                                     va_list_ref_type_node,
4329                                                     endlink)),
4330                     BUILT_IN_VA_END, BUILT_IN_NORMAL, NULL_PTR);
4331
4332   builtin_function ("__builtin_va_copy",
4333                     build_function_type (void_type_node,
4334                                          tree_cons (NULL_TREE,
4335                                                     va_list_ref_type_node,
4336                                                     tree_cons (NULL_TREE,
4337                                                       va_list_arg_type_node,
4338                                                       endlink))),
4339                     BUILT_IN_VA_COPY, BUILT_IN_NORMAL, NULL_PTR);
4340
4341   /* ??? Ought to be `T __builtin_expect(T, T)' for any type T.  */
4342   builtin_function ("__builtin_expect",
4343                     build_function_type (long_integer_type_node,
4344                                          tree_cons (NULL_TREE,
4345                                                     long_integer_type_node,
4346                                                     tree_cons (NULL_TREE,
4347                                                         long_integer_type_node,
4348                                                         endlink))),
4349                     BUILT_IN_EXPECT, BUILT_IN_NORMAL, NULL_PTR);
4350
4351   /* Currently under experimentation.  */
4352   builtin_function ("__builtin_memcpy", memcpy_ftype, BUILT_IN_MEMCPY,
4353                     BUILT_IN_NORMAL, "memcpy");
4354   builtin_function ("__builtin_memcmp", int_ftype_cptr_cptr_sizet,
4355                     BUILT_IN_MEMCMP, BUILT_IN_NORMAL, "memcmp");
4356   builtin_function ("__builtin_memset", memset_ftype,
4357                     BUILT_IN_MEMSET, BUILT_IN_NORMAL, "memset");
4358   builtin_function ("__builtin_bzero", bzero_ftype,
4359                     BUILT_IN_BZERO, BUILT_IN_NORMAL, "bzero");
4360   builtin_function ("__builtin_bcmp", bcmp_ftype,
4361                     BUILT_IN_BCMP, BUILT_IN_NORMAL, "bcmp");
4362   builtin_function ("__builtin_strcmp", int_ftype_string_string,
4363                     BUILT_IN_STRCMP, BUILT_IN_NORMAL, "strcmp");
4364   builtin_function ("__builtin_strcpy", string_ftype_ptr_ptr,
4365                     BUILT_IN_STRCPY, BUILT_IN_NORMAL, "strcpy");
4366   builtin_function ("__builtin_strlen", strlen_ftype,
4367                     BUILT_IN_STRLEN, BUILT_IN_NORMAL, "strlen");
4368   builtin_function ("__builtin_sqrtf", float_ftype_float,
4369                     BUILT_IN_FSQRT, BUILT_IN_NORMAL, "sqrtf");
4370   builtin_function ("__builtin_fsqrt", double_ftype_double,
4371                     BUILT_IN_FSQRT, BUILT_IN_NORMAL, "sqrt");
4372   builtin_function ("__builtin_sqrtl", ldouble_ftype_ldouble,
4373                     BUILT_IN_FSQRT, BUILT_IN_NORMAL, "sqrtl");
4374   builtin_function ("__builtin_sinf", float_ftype_float,
4375                     BUILT_IN_SIN, BUILT_IN_NORMAL, "sinf");
4376   builtin_function ("__builtin_sin", double_ftype_double,
4377                     BUILT_IN_SIN, BUILT_IN_NORMAL, "sin");
4378   builtin_function ("__builtin_sinl", ldouble_ftype_ldouble,
4379                     BUILT_IN_SIN, BUILT_IN_NORMAL, "sinl");
4380   builtin_function ("__builtin_cosf", float_ftype_float,
4381                     BUILT_IN_COS, BUILT_IN_NORMAL, "cosf");
4382   builtin_function ("__builtin_cos", double_ftype_double,
4383                     BUILT_IN_COS, BUILT_IN_NORMAL, "cos");
4384   builtin_function ("__builtin_cosl", ldouble_ftype_ldouble,
4385                     BUILT_IN_COS, BUILT_IN_NORMAL, "cosl");
4386   built_in_decls[BUILT_IN_PUTCHAR] =
4387     builtin_function ("__builtin_putchar", int_ftype_int,
4388                       BUILT_IN_PUTCHAR, BUILT_IN_NORMAL, "putchar");
4389   built_in_decls[BUILT_IN_PUTS] =
4390     builtin_function ("__builtin_puts", puts_ftype,
4391                       BUILT_IN_PUTS, BUILT_IN_NORMAL, "puts");
4392   builtin_function ("__builtin_printf", printf_ftype,
4393                     BUILT_IN_PRINTF, BUILT_IN_FRONTEND, "printf");
4394   /* We declare these without argument so that the initial declaration
4395      for these identifiers is a builtin.  That allows us to redeclare
4396      them later with argument without worrying about the explicit
4397      declarations in stdio.h being taken as the initial declaration.
4398      Also, save the _DECL for these so we can use them later.  */
4399   built_in_decls[BUILT_IN_FWRITE] =
4400     builtin_function ("__builtin_fwrite", sizet_ftype_any,
4401                       BUILT_IN_FWRITE, BUILT_IN_NORMAL, "fwrite");
4402   built_in_decls[BUILT_IN_FPUTC] =
4403     builtin_function ("__builtin_fputc", int_ftype_any,
4404                       BUILT_IN_FPUTC, BUILT_IN_NORMAL, "fputc");
4405   built_in_decls[BUILT_IN_FPUTS] =
4406     builtin_function ("__builtin_fputs", int_ftype_any,
4407                       BUILT_IN_FPUTS, BUILT_IN_NORMAL, "fputs");
4408
4409   if (! no_builtins)
4410     {
4411       builtin_function ("abs", int_ftype_int, BUILT_IN_ABS,
4412                         BUILT_IN_NORMAL, NULL_PTR);
4413       builtin_function ("fabsf", float_ftype_float, BUILT_IN_FABS,
4414                         BUILT_IN_NORMAL, NULL_PTR);
4415       builtin_function ("fabs", double_ftype_double, BUILT_IN_FABS,
4416                         BUILT_IN_NORMAL, NULL_PTR);
4417       builtin_function ("fabsl", ldouble_ftype_ldouble, BUILT_IN_FABS,
4418                         BUILT_IN_NORMAL, NULL_PTR);
4419       builtin_function ("labs", long_ftype_long, BUILT_IN_LABS,
4420                         BUILT_IN_NORMAL, NULL_PTR);
4421       if (flag_isoc99 || ! no_nonansi_builtins)
4422         builtin_function ("llabs", longlong_ftype_longlong, BUILT_IN_LLABS,
4423                           BUILT_IN_NORMAL, NULL_PTR);
4424       builtin_function ("memcpy", memcpy_ftype, BUILT_IN_MEMCPY,
4425                         BUILT_IN_NORMAL, NULL_PTR);
4426       builtin_function ("memcmp", int_ftype_cptr_cptr_sizet, BUILT_IN_MEMCMP,
4427                         BUILT_IN_NORMAL, NULL_PTR);
4428       builtin_function ("memset", memset_ftype, BUILT_IN_MEMSET,
4429                         BUILT_IN_NORMAL, NULL_PTR);
4430       builtin_function ("strcmp", int_ftype_string_string, BUILT_IN_STRCMP,
4431                         BUILT_IN_NORMAL, NULL_PTR);
4432       builtin_function ("strcpy", string_ftype_ptr_ptr, BUILT_IN_STRCPY,
4433                         BUILT_IN_NORMAL, NULL_PTR);
4434       builtin_function ("strlen", strlen_ftype, BUILT_IN_STRLEN,
4435                         BUILT_IN_NORMAL, NULL_PTR);
4436       builtin_function ("sqrtf", float_ftype_float, BUILT_IN_FSQRT,
4437                         BUILT_IN_NORMAL, NULL_PTR);
4438       builtin_function ("sqrt", double_ftype_double, BUILT_IN_FSQRT,
4439                         BUILT_IN_NORMAL, NULL_PTR);
4440       builtin_function ("sqrtl", ldouble_ftype_ldouble, BUILT_IN_FSQRT,
4441                         BUILT_IN_NORMAL, NULL_PTR);
4442       builtin_function ("sinf", float_ftype_float, BUILT_IN_SIN,
4443                         BUILT_IN_NORMAL, NULL_PTR);
4444       builtin_function ("sin", double_ftype_double, BUILT_IN_SIN,
4445                         BUILT_IN_NORMAL, NULL_PTR);
4446       builtin_function ("sinl", ldouble_ftype_ldouble, BUILT_IN_SIN,
4447                         BUILT_IN_NORMAL, NULL_PTR);
4448       builtin_function ("cosf", float_ftype_float, BUILT_IN_COS,
4449                         BUILT_IN_NORMAL, NULL_PTR);
4450       builtin_function ("cos", double_ftype_double, BUILT_IN_COS,
4451                         BUILT_IN_NORMAL, NULL_PTR);
4452       builtin_function ("cosl", ldouble_ftype_ldouble, BUILT_IN_COS,
4453                         BUILT_IN_NORMAL, NULL_PTR);
4454       builtin_function ("printf", printf_ftype, BUILT_IN_PRINTF,
4455                         BUILT_IN_FRONTEND, NULL_PTR);
4456       /* We declare these without argument so that the initial
4457          declaration for these identifiers is a builtin.  That allows
4458          us to redeclare them later with argument without worrying
4459          about the explicit declarations in stdio.h being taken as the
4460          initial declaration.  */
4461       builtin_function ("fputc", int_ftype_any, BUILT_IN_FPUTC,
4462                         BUILT_IN_NORMAL, NULL_PTR);
4463       builtin_function ("fputs", int_ftype_any, BUILT_IN_FPUTS,
4464                         BUILT_IN_NORMAL, NULL_PTR);
4465
4466       /* Declare these functions volatile
4467          to avoid spurious "control drops through" warnings.  */
4468       temp = builtin_function ("abort", cplus_mode ? void_ftype : void_ftype_any,
4469                                0, NOT_BUILT_IN, NULL_PTR);
4470       TREE_THIS_VOLATILE (temp) = 1;
4471       TREE_SIDE_EFFECTS (temp) = 1;
4472
4473 #if 0 /* ??? The C++ frontend used to do this.  */
4474       /* Well, these are actually ANSI, but we can't set DECL_BUILT_IN on
4475          them...  */
4476       DECL_BUILT_IN_NONANSI (temp) = 1;
4477 #endif
4478       temp = builtin_function ("exit",
4479                                cplus_mode ? void_ftype_int : void_ftype_any,
4480                                0, NOT_BUILT_IN, NULL_PTR);
4481       TREE_THIS_VOLATILE (temp) = 1;
4482       TREE_SIDE_EFFECTS (temp) = 1;
4483
4484 #if 0 /* ??? The C++ frontend used to do this.  */
4485       /* Well, these are actually ANSI, but we can't set DECL_BUILT_IN on
4486          them...  */
4487       DECL_BUILT_IN_NONANSI (temp) = 1;
4488 #endif
4489     }
4490
4491 #if 0
4492   /* Support for these has not been written in either expand_builtin
4493      or build_function_call.  */
4494   builtin_function ("__builtin_div", default_ftype, BUILT_IN_DIV,
4495                     BUILT_IN_NORMAL, NULL_PTR);
4496   builtin_function ("__builtin_ldiv", default_ftype, BUILT_IN_LDIV,
4497                     BUILT_IN_NORMAL, NULL_PTR);
4498   builtin_function ("__builtin_ffloor", double_ftype_double, BUILT_IN_FFLOOR,
4499                     BUILT_IN_NORMAL, NULL_PTR);
4500   builtin_function ("__builtin_fceil", double_ftype_double, BUILT_IN_FCEIL,
4501                     BUILT_IN_NORMAL, NULL_PTR);
4502   builtin_function ("__builtin_fmod", double_ftype_double_double,
4503                     BUILT_IN_FMOD, BUILT_IN_NORMAL, NULL_PTR);
4504   builtin_function ("__builtin_frem", double_ftype_double_double,
4505                     BUILT_IN_FREM, BUILT_IN_NORMAL, NULL_PTR);
4506   builtin_function ("__builtin_getexp", double_ftype_double, BUILT_IN_GETEXP,
4507                     BUILT_IN_NORMAL, NULL_PTR);
4508   builtin_function ("__builtin_getman", double_ftype_double, BUILT_IN_GETMAN,
4509                     BUILT_IN_NORMAL, NULL_PTR);
4510 #endif
4511
4512   main_identifier_node = get_identifier ("main");
4513
4514   /* ??? Perhaps there's a better place to do this.  But it is related
4515      to __builtin_va_arg, so it isn't that off-the-wall.  */
4516   lang_type_promotes_to = simple_type_promotes_to;
4517 }
4518
4519 tree
4520 build_va_arg (expr, type)
4521      tree expr, type;
4522 {
4523   return build1 (VA_ARG_EXPR, type, expr);
4524 }
4525 \f
4526 /* Given a type, apply default promotions wrt unnamed function arguments
4527    and return the new type.  Return NULL_TREE if no change.  */
4528 /* ??? There is a function of the same name in the C++ front end that
4529    does something similar, but is more thorough and does not return NULL
4530    if no change.  We could perhaps share code, but it would make the
4531    self_promoting_type property harder to identify.  */
4532
4533 tree
4534 simple_type_promotes_to (type)
4535      tree type;
4536 {
4537   if (TYPE_MAIN_VARIANT (type) == float_type_node)
4538     return double_type_node;
4539
4540   if (C_PROMOTING_INTEGER_TYPE_P (type))
4541     {
4542       /* Traditionally, unsignedness is preserved in default promotions.
4543          Also preserve unsignedness if not really getting any wider.  */
4544       if (TREE_UNSIGNED (type)
4545           && (flag_traditional
4546               || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
4547         return unsigned_type_node;
4548       return integer_type_node;
4549     }
4550
4551   return NULL_TREE;
4552 }
4553
4554 /* Return 1 if PARMS specifies a fixed number of parameters
4555    and none of their types is affected by default promotions.  */
4556
4557 int
4558 self_promoting_args_p (parms)
4559      tree parms;
4560 {
4561   register tree t;
4562   for (t = parms; t; t = TREE_CHAIN (t))
4563     {
4564       register tree type = TREE_VALUE (t);
4565
4566       if (TREE_CHAIN (t) == 0 && type != void_type_node)
4567         return 0;
4568
4569       if (type == 0)
4570         return 0;
4571
4572       if (TYPE_MAIN_VARIANT (type) == float_type_node)
4573         return 0;
4574
4575       if (C_PROMOTING_INTEGER_TYPE_P (type))
4576         return 0;
4577     }
4578   return 1;
4579 }
4580
4581 /* Recognize certain built-in functions so we can make tree-codes
4582    other than CALL_EXPR.  We do this when it enables fold-const.c
4583    to do something useful.  */
4584 /* ??? By rights this should go in builtins.c, but only C and C++
4585    implement build_{binary,unary}_op.  Not exactly sure what bits
4586    of functionality are actually needed from those functions, or
4587    where the similar functionality exists in the other front ends.  */
4588
4589 tree
4590 expand_tree_builtin (function, params, coerced_params)
4591      tree function, params, coerced_params;
4592 {
4593   enum tree_code code;
4594
4595   if (DECL_BUILT_IN_CLASS (function) != BUILT_IN_NORMAL)
4596     return NULL_TREE;
4597
4598   switch (DECL_FUNCTION_CODE (function))
4599     {
4600     case BUILT_IN_ABS:
4601     case BUILT_IN_LABS:
4602     case BUILT_IN_LLABS:
4603     case BUILT_IN_FABS:
4604       if (coerced_params == 0)
4605         return integer_zero_node;
4606       return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
4607
4608     case BUILT_IN_ISGREATER:
4609       if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
4610         code = UNLE_EXPR;
4611       else
4612         code = LE_EXPR;
4613       goto unordered_cmp;
4614
4615     case BUILT_IN_ISGREATEREQUAL:
4616       if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
4617         code = UNLT_EXPR;
4618       else
4619         code = LT_EXPR;
4620       goto unordered_cmp;
4621
4622     case BUILT_IN_ISLESS:
4623       if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
4624         code = UNGE_EXPR;
4625       else
4626         code = GE_EXPR;
4627       goto unordered_cmp;
4628
4629     case BUILT_IN_ISLESSEQUAL:
4630       if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
4631         code = UNGT_EXPR;
4632       else
4633         code = GT_EXPR;
4634       goto unordered_cmp;
4635
4636     case BUILT_IN_ISLESSGREATER:
4637       if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
4638         code = UNEQ_EXPR;
4639       else
4640         code = EQ_EXPR;
4641       goto unordered_cmp;
4642
4643     case BUILT_IN_ISUNORDERED:
4644       if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT)
4645         return integer_zero_node;
4646       code = UNORDERED_EXPR;
4647       goto unordered_cmp;
4648
4649     unordered_cmp:
4650       {
4651         tree arg0, arg1;
4652
4653         if (params == 0
4654             || TREE_CHAIN (params) == 0)
4655           {
4656             error ("too few arguments to function `%s'",
4657                    IDENTIFIER_POINTER (DECL_NAME (function)));
4658             return error_mark_node;
4659           }
4660         else if (TREE_CHAIN (TREE_CHAIN (params)) != 0)
4661           {
4662             error ("too many arguments to function `%s'",
4663                    IDENTIFIER_POINTER (DECL_NAME (function)));
4664             return error_mark_node;
4665           }
4666
4667         arg0 = TREE_VALUE (params);
4668         arg1 = TREE_VALUE (TREE_CHAIN (params));
4669         arg0 = build_binary_op (code, arg0, arg1, 0);
4670         if (code != UNORDERED_EXPR)
4671           arg0 = build_unary_op (TRUTH_NOT_EXPR, arg0, 0);
4672         return arg0;
4673       }
4674       break;
4675
4676     default:
4677       break;
4678     }
4679
4680   return NULL_TREE;
4681 }
4682
4683 /* Returns non-zero if CODE is the code for a statement.  */
4684
4685 int
4686 statement_code_p (code)
4687      enum tree_code code;
4688 {
4689   switch (code)
4690     {
4691     case EXPR_STMT:
4692     case COMPOUND_STMT:
4693     case DECL_STMT:
4694     case IF_STMT:
4695     case FOR_STMT:
4696     case WHILE_STMT:
4697     case DO_STMT:
4698     case RETURN_STMT:
4699     case BREAK_STMT:
4700     case CONTINUE_STMT:
4701     case SCOPE_STMT:
4702     case SWITCH_STMT:
4703     case GOTO_STMT:
4704     case LABEL_STMT:
4705     case ASM_STMT:
4706     case CASE_LABEL:
4707       return 1;
4708
4709     default:
4710       if (lang_statement_code_p)
4711         return (*lang_statement_code_p) (code);
4712       return 0;
4713     }
4714 }
4715
4716 /* Walk the statemen tree, rooted at *tp.  Apply FUNC to all the
4717    sub-trees of *TP in a pre-order traversal.  FUNC is called with the
4718    DATA and the address of each sub-tree.  If FUNC returns a non-NULL
4719    value, the traversal is aborted, and the value returned by FUNC is
4720    returned.  If FUNC sets WALK_SUBTREES to zero, then the subtrees of
4721    the node being visited are not walked.
4722
4723    We don't need a without_duplicates variant of this one because the
4724    statement tree is a tree, not a graph.  */
4725
4726 tree 
4727 walk_stmt_tree (tp, func, data)
4728      tree *tp;
4729      walk_tree_fn func;
4730      void *data;
4731 {
4732   enum tree_code code;
4733   int walk_subtrees;
4734   tree result;
4735   int i, len;
4736
4737 #define WALK_SUBTREE(NODE)                              \
4738   do                                                    \
4739     {                                                   \
4740       result = walk_stmt_tree (&(NODE), func, data);    \
4741       if (result)                                       \
4742         return result;                                  \
4743     }                                                   \
4744   while (0)
4745
4746   /* Skip empty subtrees.  */
4747   if (!*tp)
4748     return NULL_TREE;
4749
4750   /* Skip subtrees below non-statement nodes.  */
4751   if (!statement_code_p (TREE_CODE (*tp)))
4752     return NULL_TREE;
4753
4754   /* Call the function.  */
4755   walk_subtrees = 1;
4756   result = (*func) (tp, &walk_subtrees, data);
4757
4758   /* If we found something, return it.  */
4759   if (result)
4760     return result;
4761
4762   /* Even if we didn't, FUNC may have decided that there was nothing
4763      interesting below this point in the tree.  */
4764   if (!walk_subtrees)
4765     return NULL_TREE;
4766
4767   /* FUNC may have modified the tree, recheck that we're looking at a
4768      statement node.  */
4769   code = TREE_CODE (*tp);
4770   if (!statement_code_p (code))
4771     return NULL_TREE;
4772
4773   /* Walk over all the sub-trees of this operand.  Statement nodes never
4774      contain RTL, and we needn't worry about TARGET_EXPRs.  */
4775   len = TREE_CODE_LENGTH (code);
4776
4777   /* Go through the subtrees.  We need to do this in forward order so
4778      that the scope of a FOR_EXPR is handled properly.  */
4779   for (i = 0; i < len; ++i)
4780     WALK_SUBTREE (TREE_OPERAND (*tp, i));
4781
4782   /* Finally visit the chain.  This can be tail-recursion optimized if
4783      we write it this way.  */
4784   return walk_stmt_tree (&TREE_CHAIN (*tp), func, data);
4785
4786 #undef WALK_SUBTREE
4787 }
4788
4789 /* Used to compare case labels.  K1 and K2 are actually tree nodes
4790    representing case labels, or NULL_TREE for a `default' label.
4791    Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
4792    K2, and 0 if K1 and K2 are equal.  */
4793
4794 int
4795 case_compare (k1, k2)
4796      splay_tree_key k1;
4797      splay_tree_key k2;
4798 {
4799   /* Consider a NULL key (such as arises with a `default' label) to be
4800      smaller than anything else.  */
4801   if (!k1)
4802     return k2 ? -1 : 0;
4803   else if (!k2)
4804     return k1 ? 1 : 0;
4805
4806   return tree_int_cst_compare ((tree) k1, (tree) k2);
4807 }
4808
4809 /* Process a case label for the range LOW_VALUE ... HIGH_VALUE.  If
4810    LOW_VALUE and HIGH_VALUE are both NULL_TREE then this case label is
4811    actually a `default' label.  If only HIGH_VALUE is NULL_TREE, then
4812    case label was declared using the usual C/C++ syntax, rather than
4813    the GNU case range extension.  CASES is a tree containing all the
4814    case ranges processed so far; COND is the condition for the
4815    switch-statement itself.  Returns the CASE_LABEL created, or
4816    ERROR_MARK_NODE if no CASE_LABEL is created.  */
4817
4818 tree
4819 c_add_case_label (cases, cond, low_value, high_value)
4820      splay_tree cases;
4821      tree cond;
4822      tree low_value;
4823      tree high_value;
4824 {
4825   tree type;
4826   tree label;
4827   tree case_label;
4828   splay_tree_node node;
4829
4830   /* Create the LABEL_DECL itself.  */
4831   label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
4832   DECL_CONTEXT (label) = current_function_decl;
4833
4834   /* If there was an error processing the switch condition, bail now
4835      before we get more confused.  */
4836   if (!cond || cond == error_mark_node)
4837     {
4838       /* Add a label anyhow so that the back-end doesn't think that
4839          the beginning of the switch is unreachable.  */
4840       if (!cases->root)
4841         add_stmt (build_case_label (NULL_TREE, NULL_TREE, label));
4842       return error_mark_node;
4843     }
4844
4845   if ((low_value && TREE_TYPE (low_value) 
4846        && POINTER_TYPE_P (TREE_TYPE (low_value))) 
4847       || (high_value && TREE_TYPE (high_value)
4848           && POINTER_TYPE_P (TREE_TYPE (high_value))))
4849     error ("pointers are not permitted as case values");
4850
4851   /* Case ranges are a GNU extension.  */
4852   if (high_value && pedantic)
4853     {
4854       if (c_language == clk_cplusplus)
4855         pedwarn ("ISO C++ forbids range expressions in switch statements");
4856       else
4857         pedwarn ("ISO C forbids range expressions in switch statements");
4858     }
4859
4860   type = TREE_TYPE (cond);
4861   if (low_value)
4862     {
4863       low_value = check_case_value (low_value);
4864       low_value = convert_and_check (type, low_value);
4865     }
4866   if (high_value)
4867     {
4868       high_value = check_case_value (high_value);
4869       high_value = convert_and_check (type, high_value);
4870     }
4871
4872   /* If an error has occurred, bail out now.  */
4873   if (low_value == error_mark_node || high_value == error_mark_node)
4874     {
4875       if (!cases->root)
4876         add_stmt (build_case_label (NULL_TREE, NULL_TREE, label));
4877       return error_mark_node;
4878     }
4879
4880   /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
4881      really a case range, even though it was written that way.  Remove
4882      the HIGH_VALUE to simplify later processing.  */
4883   if (tree_int_cst_equal (low_value, high_value))
4884     high_value = NULL_TREE;
4885   if (low_value && high_value 
4886       && !tree_int_cst_lt (low_value, high_value)) 
4887     warning ("empty range specified");
4888
4889   /* Look up the LOW_VALUE in the table of case labels we already
4890      have.  */
4891   node = splay_tree_lookup (cases, (splay_tree_key) low_value);
4892   /* If there was not an exact match, check for overlapping ranges.
4893      There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
4894      that's a `default' label and the only overlap is an exact match.  */
4895   if (!node && (low_value || high_value))
4896     {
4897       splay_tree_node low_bound;
4898       splay_tree_node high_bound;
4899
4900       /* Even though there wasn't an exact match, there might be an
4901          overlap between this case range and another case range.
4902          Since we've (inductively) not allowed any overlapping case
4903          ranges, we simply need to find the greatest low case label
4904          that is smaller that LOW_VALUE, and the smallest low case
4905          label that is greater than LOW_VALUE.  If there is an overlap
4906          it will occur in one of these two ranges.  */
4907       low_bound = splay_tree_predecessor (cases,
4908                                           (splay_tree_key) low_value);
4909       high_bound = splay_tree_successor (cases,
4910                                          (splay_tree_key) low_value);
4911
4912       /* Check to see if the LOW_BOUND overlaps.  It is smaller than
4913          the LOW_VALUE, so there is no need to check unless the
4914          LOW_BOUND is in fact itself a case range.  */
4915       if (low_bound
4916           && CASE_HIGH ((tree) low_bound->value)
4917           && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
4918                                     low_value) >= 0)
4919         node = low_bound;
4920       /* Check to see if the HIGH_BOUND overlaps.  The low end of that
4921          range is bigger than the low end of the current range, so we
4922          are only interested if the current range is a real range, and
4923          not an ordinary case label.  */
4924       else if (high_bound 
4925                && high_value
4926                && (tree_int_cst_compare ((tree) high_bound->key,
4927                                          high_value)
4928                    <= 0))
4929         node = high_bound;
4930     }
4931   /* If there was an overlap, issue an error.  */
4932   if (node)
4933     {
4934       tree duplicate = CASE_LABEL_DECL ((tree) node->value);
4935
4936       if (high_value)
4937         {
4938           error ("duplicate (or overlapping) case value");
4939           error_with_decl (duplicate, 
4940                            "this is the first entry overlapping that value");
4941         }
4942       else if (low_value)
4943         {
4944           error ("duplicate case value") ;
4945           error_with_decl (duplicate, "previously used here");
4946         }
4947       else
4948         {
4949           error ("multiple default labels in one switch");
4950           error_with_decl (duplicate, "this is the first default label");
4951         }
4952       if (!cases->root)
4953         add_stmt (build_case_label (NULL_TREE, NULL_TREE, label));
4954     }
4955
4956   /* Add a CASE_LABEL to the statement-tree.  */
4957   case_label = add_stmt (build_case_label (low_value, high_value, label));
4958   /* Register this case label in the splay tree.  */
4959   splay_tree_insert (cases, 
4960                      (splay_tree_key) low_value,
4961                      (splay_tree_value) case_label);
4962
4963   return case_label;
4964 }
4965
4966 /* Mark P (a stmt_tree) for GC.  The use of a `void *' for the
4967    parameter allows this function to be used as a GC-marking
4968    function.  */
4969
4970 void
4971 mark_stmt_tree (p)
4972      void *p;
4973 {
4974   stmt_tree st = (stmt_tree) p;
4975
4976   ggc_mark_tree (st->x_last_stmt);
4977   ggc_mark_tree (st->x_last_expr_type);
4978 }
4979
4980 /* Mark LD for GC.  */
4981
4982 void
4983 c_mark_lang_decl (c)
4984      struct c_lang_decl *c;
4985 {
4986   ggc_mark_tree (c->saved_tree);
4987 }
4988
4989 /* Mark F for GC.  */
4990
4991 void
4992 mark_c_language_function (f)
4993      struct language_function *f;
4994 {
4995   if (!f)
4996     return;
4997
4998   mark_stmt_tree (&f->x_stmt_tree);
4999   ggc_mark_tree (f->x_scope_stmt_stack);
5000 }
5001
5002 /* Hook used by expand_expr to expand language-specific tree codes.  */
5003
5004 rtx
5005 c_expand_expr (exp, target, tmode, modifier)
5006      tree exp;
5007      rtx target;
5008      enum machine_mode tmode;
5009      enum expand_modifier modifier;
5010 {
5011   switch (TREE_CODE (exp))
5012     {
5013     case STMT_EXPR:
5014       {
5015         tree rtl_expr;
5016         rtx result;
5017
5018         /* Since expand_expr_stmt calls free_temp_slots after every
5019            expression statement, we must call push_temp_slots here.
5020            Otherwise, any temporaries in use now would be considered
5021            out-of-scope after the first EXPR_STMT from within the
5022            STMT_EXPR.  */
5023         push_temp_slots ();
5024         rtl_expr = expand_start_stmt_expr ();
5025         expand_stmt (STMT_EXPR_STMT (exp));
5026         expand_end_stmt_expr (rtl_expr);
5027         result = expand_expr (rtl_expr, target, tmode, modifier);
5028         pop_temp_slots ();
5029         return result;
5030       }
5031       break;
5032       
5033     case CALL_EXPR:
5034       {
5035         if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
5036             && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
5037                 == FUNCTION_DECL)
5038             && DECL_BUILT_IN (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
5039             && (DECL_BUILT_IN_CLASS (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
5040                 == BUILT_IN_FRONTEND))
5041           return c_expand_builtin (exp, target, tmode, modifier);
5042         else
5043           abort();
5044       }
5045       break;
5046
5047     default:
5048       abort ();
5049     }
5050
5051   abort ();
5052   return NULL;
5053 }
5054
5055 /* Hook used by safe_from_p to handle language-specific tree codes.  */
5056
5057 int
5058 c_safe_from_p (target, exp)
5059      rtx target;
5060      tree exp;
5061 {
5062   /* We can see statements here when processing the body of a
5063      statement-expression.  For a declaration statement declaring a
5064      variable, look at the variable's initializer.  */
5065   if (TREE_CODE (exp) == DECL_STMT) 
5066     {
5067       tree decl = DECL_STMT_DECL (exp);
5068
5069       if (TREE_CODE (decl) == VAR_DECL
5070           && DECL_INITIAL (decl)
5071           && !safe_from_p (target, DECL_INITIAL (decl), /*top_p=*/0))
5072         return 0;
5073     }
5074
5075   /* For any statement, we must follow the statement-chain.  */
5076   if (statement_code_p (TREE_CODE (exp)) && TREE_CHAIN (exp))
5077     return safe_from_p (target, TREE_CHAIN (exp), /*top_p=*/0);
5078
5079   /* Assume everything else is safe.  */
5080   return 1;
5081 }
5082
5083 /* Tree code classes. */
5084
5085 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
5086
5087 static char c_tree_code_type[] = {
5088   'x',
5089 #include "c-common.def"
5090 };
5091 #undef DEFTREECODE
5092
5093 /* Table indexed by tree code giving number of expression
5094    operands beyond the fixed part of the node structure.
5095    Not used for types or decls.  */
5096
5097 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
5098
5099 static int c_tree_code_length[] = {
5100   0,
5101 #include "c-common.def"
5102 };
5103 #undef DEFTREECODE
5104
5105 /* Names of tree components.
5106    Used for printing out the tree and error messages.  */
5107 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
5108
5109 static const char *c_tree_code_name[] = {
5110   "@@dummy",
5111 #include "c-common.def"
5112 };
5113 #undef DEFTREECODE
5114
5115 /* Adds the tree codes specific to the C front end to the list of all
5116    tree codes. */
5117
5118 void
5119 add_c_tree_codes ()
5120 {
5121   memcpy (tree_code_type + (int) LAST_AND_UNUSED_TREE_CODE,
5122           c_tree_code_type,
5123           (int)LAST_C_TREE_CODE - (int)LAST_AND_UNUSED_TREE_CODE);
5124   memcpy (tree_code_length + (int) LAST_AND_UNUSED_TREE_CODE,
5125           c_tree_code_length,
5126           (LAST_C_TREE_CODE - (int)LAST_AND_UNUSED_TREE_CODE) * sizeof (int));
5127   memcpy (tree_code_name + (int) LAST_AND_UNUSED_TREE_CODE,
5128           c_tree_code_name,
5129           (LAST_C_TREE_CODE - (int)LAST_AND_UNUSED_TREE_CODE) * sizeof (char *));
5130 }
5131
5132 #define CALLED_AS_BUILT_IN(NODE) \
5133    (!strncmp (IDENTIFIER_POINTER (DECL_NAME (NODE)), "__builtin_", 10))
5134
5135 static rtx
5136 c_expand_builtin (exp, target, tmode, modifier)
5137      tree exp;
5138      rtx target;
5139      enum machine_mode tmode;
5140      enum expand_modifier modifier;
5141 {
5142   tree type = TREE_TYPE (exp);
5143   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
5144   tree arglist = TREE_OPERAND (exp, 1);
5145   enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5146   enum tree_code code = TREE_CODE (exp);
5147   const int ignore = (target == const0_rtx
5148                       || ((code == NON_LVALUE_EXPR || code == NOP_EXPR
5149                            || code == CONVERT_EXPR || code == REFERENCE_EXPR
5150                            || code == COND_EXPR)
5151                           && TREE_CODE (type) == VOID_TYPE));
5152
5153   if (! optimize && ! CALLED_AS_BUILT_IN (fndecl))
5154     return expand_call (exp, target, ignore);
5155
5156   switch (fcode)
5157     {
5158     case BUILT_IN_PRINTF:
5159       target = c_expand_builtin_printf (arglist, target, tmode,
5160                                         modifier, ignore);
5161       if (target)
5162         return target;
5163       break;
5164
5165     default:                    /* just do library call, if unknown builtin */
5166       error ("built-in function `%s' not currently supported",
5167              IDENTIFIER_POINTER (DECL_NAME (fndecl)));
5168     }
5169
5170   /* The switch statement above can drop through to cause the function
5171      to be called normally.  */
5172   return expand_call (exp, target, ignore);
5173 }
5174
5175 /* Check an arglist to *printf for problems.  The arglist should start
5176    at the format specifier, with the remaining arguments immediately
5177    following it. */
5178 static int
5179 is_valid_printf_arglist (arglist)
5180   tree arglist;
5181 {
5182   /* Save this value so we can restore it later. */
5183   const int SAVE_pedantic = pedantic;
5184   int diagnostic_occurred = 0;
5185
5186   /* Set this to a known value so the user setting won't affect code
5187      generation.  */
5188   pedantic = 1;
5189   /* Check to make sure there are no format specifier errors. */
5190   check_function_format (&diagnostic_occurred,
5191                          maybe_get_identifier("printf"),
5192                          NULL_TREE, arglist);
5193
5194   /* Restore the value of `pedantic'. */
5195   pedantic = SAVE_pedantic;
5196
5197   /* If calling `check_function_format_ptr' produces a warning, we
5198      return false, otherwise we return true. */
5199   return ! diagnostic_occurred;
5200 }
5201
5202 /* If the arguments passed to printf are suitable for optimizations,
5203    we attempt to transform the call. */
5204 static rtx
5205 c_expand_builtin_printf (arglist, target, tmode, modifier, ignore)
5206      tree arglist;
5207      rtx target;
5208      enum machine_mode tmode;
5209      enum expand_modifier modifier;
5210      int ignore;
5211 {
5212   tree fn_putchar = built_in_decls[BUILT_IN_PUTCHAR],
5213     fn_puts = built_in_decls[BUILT_IN_PUTS];
5214   tree fn, format_arg, stripped_string;
5215
5216   /* If the return value is used, or the replacement _DECL isn't
5217      initialized, don't do the transformation. */
5218   if (!ignore || !fn_putchar || !fn_puts)
5219     return 0;
5220
5221   /* Verify the required arguments in the original call. */
5222   if (arglist == 0
5223       || (TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE))
5224     return 0;
5225   
5226   /* Check the specifier vs. the parameters. */
5227   if (!is_valid_printf_arglist (arglist))
5228     return 0;
5229   
5230   format_arg = TREE_VALUE (arglist);
5231   stripped_string = format_arg;
5232   STRIP_NOPS (stripped_string);
5233   if (stripped_string && TREE_CODE (stripped_string) == ADDR_EXPR)
5234     stripped_string = TREE_OPERAND (stripped_string, 0);
5235
5236   /* If the format specifier isn't a STRING_CST, punt.  */
5237   if (TREE_CODE (stripped_string) != STRING_CST)
5238     return 0;
5239   
5240   /* OK!  We can attempt optimization.  */
5241
5242   /* If the format specifier was "%s\n", call __builtin_puts(arg2). */
5243   if (strcmp (TREE_STRING_POINTER (stripped_string), "%s\n") == 0)
5244     {
5245       arglist = TREE_CHAIN (arglist);
5246       fn = fn_puts;
5247     }
5248   /* If the format specifier was "%c", call __builtin_putchar (arg2). */
5249   else if (strcmp (TREE_STRING_POINTER (stripped_string), "%c") == 0)
5250     {
5251       arglist = TREE_CHAIN (arglist);
5252       fn = fn_putchar;
5253     }
5254   else
5255     {
5256      /* We can't handle anything else with % args or %% ... yet. */
5257       if (strchr (TREE_STRING_POINTER (stripped_string), '%'))
5258         return 0;
5259       
5260       /* If the resulting constant string has a length of 1, call
5261          putchar.  Note, TREE_STRING_LENGTH includes the terminating
5262          NULL in its count.  */
5263       if (TREE_STRING_LENGTH (stripped_string) == 2)
5264         {
5265           /* Given printf("c"), (where c is any one character,)
5266              convert "c"[0] to an int and pass that to the replacement
5267              function. */
5268           arglist = build_int_2 (TREE_STRING_POINTER (stripped_string)[0], 0);
5269           arglist = build_tree_list (NULL_TREE, arglist);
5270           
5271           fn = fn_putchar;
5272         }
5273       /* If the resulting constant was "string\n", call
5274          __builtin_puts("string").  Ensure "string" has at least one
5275          character besides the trailing \n.  Note, TREE_STRING_LENGTH
5276          includes the terminating NULL in its count.  */
5277       else if (TREE_STRING_LENGTH (stripped_string) > 2
5278                && TREE_STRING_POINTER (stripped_string)
5279                [TREE_STRING_LENGTH (stripped_string) - 2] == '\n')
5280         {
5281           /* Create a NULL-terminated string that's one char shorter
5282              than the original, stripping off the trailing '\n'.  */
5283           const int newlen = TREE_STRING_LENGTH (stripped_string) - 1;
5284           char *newstr = (char *) alloca (newlen);
5285           memcpy (newstr, TREE_STRING_POINTER (stripped_string), newlen - 1);
5286           newstr[newlen - 1] = 0;
5287           
5288           arglist = build_string (newlen, newstr);
5289           TREE_TYPE (arglist) = 
5290             build_type_variant (char_array_type_node, 1, 0);
5291           arglist = build_tree_list (NULL_TREE, arglist);
5292           fn = fn_puts;
5293         }
5294       else
5295         /* We'd like to arrange to call fputs(string) here, but we
5296            need stdout and don't have a way to get it ... yet.  */
5297         return 0;
5298     }
5299   
5300   return expand_expr (build_function_call (fn, arglist),
5301                       (ignore ? const0_rtx : target),
5302                       tmode, modifier);
5303 }