OSDN Git Service

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