OSDN Git Service

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