OSDN Git Service

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