OSDN Git Service

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