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