OSDN Git Service

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