OSDN Git Service

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