OSDN Git Service

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