OSDN Git Service

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