OSDN Git Service

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