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